Fix JPEG segment ordering per JFIF and CIPA DC-007 standards#394
Fix JPEG segment ordering per JFIF and CIPA DC-007 standards#394minatoAI wants to merge 3 commits into
Conversation
|
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
| const string kMapHDRCapacityMin = Name(kGainMapPrefix, "HDRCapacityMin"); | ||
| const string kMapHDRCapacityMax = Name(kGainMapPrefix, "HDRCapacityMax"); | ||
| const string kMapBaseRenditionIsHDR = Name(kGainMapPrefix, "BaseRenditionIsHDR"); | ||
| const string kMapOplusScale = Name(kGainMapPrefix, "OplusScale"); |
There was a problem hiding this comment.
I didn't see this as the standard in https://developer.android.com/media/platform/hdr-image-format#example_gain_map_XMP.
There was a problem hiding this comment.
Thank you for the review! Let me clarify the two XMP changes separately:
-
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. -
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.
|
|
||
| // ── Write marker segments (DQT, SOF, DHT) up to SOS ── | ||
| size_t sos_offset = 0; | ||
| while (base_pos < base_size) { |
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
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.
Summary
The
appendGainMapfunction builds JPEG output with incorrect APP marker ordering:This causes HDR decoders that follow the standard ordering (e.g., Xiaohongshu/小红书) to fail to recognize the Ultra HDR gain map.
Changes
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.
lib/src/jpegrutils.cpp: Add
rdf:about=""attribute andhdrgm:OplusScale="-1"field to the gain map XMP metadata block, matching the Android Ultra HDR specification (developer.android.com/media/platform/hdr-image-format).CMakeLists.txt: Add
-DWITH_CRT_DLL=ONto 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.