Skip to content

Fix JPEG segment ordering per JFIF and CIPA DC-007 standards#394

Open
minatoAI wants to merge 3 commits into
google:mainfrom
minatoAI:main
Open

Fix JPEG segment ordering per JFIF and CIPA DC-007 standards#394
minatoAI wants to merge 3 commits into
google:mainfrom
minatoAI:main

Conversation

@minatoAI

Copy link
Copy Markdown

Summary

The appendGainMap function builds JPEG output with incorrect APP marker ordering:

  1. JFIF (APP0) is written after XMP, ISO, MPF and ICC segments — it should be the first APP marker after SOI (per JFIF/JPEG specification)
  2. MPF (APP2) is written before JFIF and ICC, far from SOS — it should be placed right before SOS (per CIPA DC-007 Multi-Picture Format standard)

This causes HDR decoders that follow the standard ordering (e.g., Xiaohongshu/小红书) to fail to recognize the Ultra HDR gain map.

Changes

  1. lib/src/jpegr.cpp: Extract APP0(JFIF) from the base JPEG data and write it immediately after SOI. Parse the base JPEG marker segments (DQT, SOF, DHT) and write them before MPF. Write MPF right before SOS per CIPA DC-007.

  2. lib/src/jpegrutils.cpp: Add rdf:about="" attribute and hdrgm:OplusScale="-1" field to the gain map XMP metadata block, matching the Android Ultra HDR specification (developer.android.com/media/platform/hdr-image-format).

  3. CMakeLists.txt: Add -DWITH_CRT_DLL=ON to turbojpeg's ExternalProject CMAKE_ARGS when building on MSVC, to prevent CRT mismatch when the parent project uses dynamic CRT (/MD).

Testing

Verified that the fix produces a JPEG segment order identical to camera-captured Ultra HDR images (e.g., OPPO Find X8 Ultra), and the reordered file is correctly recognized as HDR by the Xiaohongshu platform.

@google-cla

google-cla Bot commented May 27, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

The appendGainMap function built JPEG output with incorrect marker ordering:
- JFIF (APP0) was written after XMP, ISO, MPF and ICC segments
  (must be the first APP marker after SOI per JFIF spec)
- MPF was written before JFIF and ICC, far from SOS
  (must be placed right before SOS per CIPA DC-007)

This caused platforms like Xiaohongshu to fail HDR recognition
because their decoders follow the standard marker ordering.

Fix:
1. Extract APP0(JFIF) from the base JPEG data and write it right after SOI
2. Parse the base JPEG marker segments (DQT, SOF, DHT) and write them before MPF
3. Write MPF right before SOS per CIPA DC-007
4. Write SOS + compressed data after MPF

Also:
- Added rdf:about="" attribute and hdrgm:OplusScale field to the gain map
  XMP metadata block (required by Android Ultra HDR specification)
- Added -DWITH_CRT_DLL=ON to turbojpeg ExternalProject for MSVC compatibility
- When BUILD_FOR_WINUI is defined, skip the forced /MT CRT override
Comment thread lib/src/jpegrutils.cpp Outdated
const string kMapHDRCapacityMin = Name(kGainMapPrefix, "HDRCapacityMin");
const string kMapHDRCapacityMax = Name(kGainMapPrefix, "HDRCapacityMax");
const string kMapBaseRenditionIsHDR = Name(kGainMapPrefix, "BaseRenditionIsHDR");
const string kMapOplusScale = Name(kGainMapPrefix, "OplusScale");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review! Let me clarify the two XMP changes separately:

  1. rdf:about=""
    This attribute is actually present in the official Android Ultra HDR spec example: https://developer.android.com/media/platform/hdr-image-format#example_gain_map_XMP . The example shows <rdf:Description rdf:about="" ...>. Including it ensures better compatibility with strict XMP parsers.

  2. hdrgm:OplusScale="-1"
    You're right — this field is NOT part of the Android Ultra HDR specification. I added it because some real-world camera-captured Ultra HDR images (e.g., from OPPO devices) include this field. However, since the goal of this PR is to align with the standard, I will remove OplusScale from the XMP output. If needed, this vendor-specific extension can be revisited separately.

hdrgm:OplusScale is not part of the Android Ultra HDR specification
(developer.android.com/media/platform/hdr-image-format). It appears
to be a vendor extension from OPPO devices. Removing it to keep XMP
output strictly conformant to the standard.

The rest of the PR (JPEG segment reordering per JFIF/CIPA DC-007,
MSVC build fix, and rdf:about attribute) remains unchanged.
Comment thread lib/src/jpegr.cpp

// ── Write marker segments (DQT, SOF, DHT) up to SOS ──
size_t sos_offset = 0;
while (base_pos < base_size) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the while loop has the risk of duplicating existing APP markers (EXIF/XMP/...), maybe we need to add code

      if (marker >= 0xE0 && marker <= 0xEF) {
          base_pos += 2 + seg_len;
          continue;
      }

Also it has risk of OOB for seg_len, we'd better add a boundary check

      if (base_pos + 4 > base_size) {
          return status_invalid_param; // Bounds check before reading segment length
      }
      int seg_len = (base_data[base_pos+2] << 8) | base_data[base_pos+3];
      if (base_pos + 2 + static_cast<size_t>(seg_len) > base_size) {
          return status_invalid_param; // Bounds check before copying segment
      }

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the thorough review! I've addressed both issues:

1. APP marker duplication — Added a check to skip APP markers (0xE0–0xEF) in the parsing loop. Since JFIF/XMP/ICC/ISO have already been written to the output earlier in the function, any APP markers encountered when walking the base JPEG are now safely skipped with continue:

if (marker >= 0xE0 && marker <= 0xEF) { base_pos += 2 + seg_len; continue; }

2. OOB bounds checks for seg_len — Added three boundary guards in the while loop:

  • Before reading the marker byte: if (base_pos + 1 >= base_size) break;
  • Before reading seg_len (need 2 bytes for the length field): if (base_pos + 4 > base_size) break;
  • After reading seg_len, ensuring the segment fits within remaining data: if (base_pos + 2 + static_cast<size_t>(seg_len) > base_size) break;

These prevent out-of-bounds reads on malformed or truncated base JPEG input. The loop uses break (not continue) for OOB conditions, consistent with the existing error-handling style in the function.

PTAL when you have a chance — thanks!

- Skip APP markers (0xE0-0xEF) in base JPEG parsing loop to avoid duplicating metadata (JFIF/XMP/ICC/ISO) that was already written to output

- Add boundary checks before and after reading segment length to prevent OOB read and OOB advance on malformed input

- Add guard for truncated data before reading marker byte

Addresses second round review comments on PR google#394.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants