Support gain map XMP metadata from recent Apple devices (#3444) Based on the the sample file from #3440 shot on an iPhone 17, it seems that recent iPhones now encode gain map metadata using (mostly) the standard XMP format from the Android spec, but slightly different from what the libavif code expected, and was therefore not detected. The metadata ALSO includes the legacy Apple XML gain map "HDRGainMapHeadroom" property, which was detected, but this format does not provide values for other metadata properties which where then wrongly assumed to have default values. The XML did not match expectations because: - it uses a child element for the Version where we expected a property - it uses version "1" where we expected "1.0" - it uses Base/AlternateHeadroom instead of the standard HDRCapacityMin/Max - it uses "0" and "1" for booleans where we expected "true"/"false" Fixes #3440
diff --git a/apps/shared/avifjpeg.c b/apps/shared/avifjpeg.c index cd9e061..2b96fee 100644 --- a/apps/shared/avifjpeg.c +++ b/apps/shared/avifjpeg.c
@@ -589,7 +589,116 @@ return xmlReadMemory((const char *)data, (int)size, url, NULL, /*options=*/0); } -// Finds an 'rdf:Description' node containing a gain map version attribute (hdrgm:Version="1.0"). +// Finds the value of a gain map metadata property, that can be either stored as an attribute of 'descriptionNode' +// (which should point to a <rdf:Description> node) or as a child node. +// 'maxValues' is the maximum number of expected values, and the size of the 'values' array. 'numValues' is set to the number +// of values actually found (which may be smaller or larger, but only up to 'maxValues' are stored in 'values'). +// Returns AVIF_TRUE if the property was found. +static avifBool avifJPEGFindGainMapProperty(const xmlNode * descriptionNode, + const char * propertyName, + uint32_t maxValues, + const char * values[], + uint32_t * numValues, + const char * nameSpace) +{ + *numValues = 0; + + // Search attributes. + for (xmlAttr * prop = descriptionNode->properties; prop != NULL; prop = prop->next) { + if (prop->ns && !xmlStrcmp(prop->ns->href, (const xmlChar *)nameSpace) && + !xmlStrcmp(prop->name, (const xmlChar *)propertyName) && prop->children != NULL && prop->children->content != NULL) { + // Properties should have just one child containing the property's value + // (in fact the 'children' field is documented as "the value of the property"). + if (maxValues > 0) { + values[0] = (const char *)prop->children->content; + } + *numValues = 1; + return AVIF_TRUE; + } + } + + // Search child nodes. + for (const xmlNode * node = descriptionNode->children; node != NULL; node = node->next) { + if (node->ns && !xmlStrcmp(node->ns->href, (const xmlChar *)nameSpace) && + !xmlStrcmp(node->name, (const xmlChar *)propertyName) && node->children) { + // Multiple values can be specified with a Seq tag: <rdf:Seq><rdf:li>value1</rdf:li><rdf:li>value2</rdf:li>...</rdf:Seq> + const xmlNode * seq = avifJPEGFindXMLNodeByName(node, XML_NAME_SPACE_RDF, "Seq", /*recursive=*/AVIF_FALSE); + if (seq) { + for (xmlNode * seqChild = seq->children; seqChild; seqChild = seqChild->next) { + if (!xmlStrcmp(seqChild->name, (const xmlChar *)"li") && seqChild->children != NULL && + seqChild->children->content != NULL) { + if (*numValues < maxValues) { + values[*numValues] = (const char *)seqChild->children->content; + } + ++(*numValues); + } + } + return *numValues > 0 ? AVIF_TRUE : AVIF_FALSE; + } else if (node->children->next == NULL && + (node->children->type == XML_TEXT_NODE || node->children->type == XML_CDATA_SECTION_NODE) && + node->children->content != NULL) { // Only one child and it's text. + if (maxValues > 0) { + values[0] = (const char *)node->children->content; + } + *numValues = 1; + return AVIF_TRUE; + } + // We found a tag for this property but no valid content. + return AVIF_FALSE; + } + } + + return AVIF_FALSE; // Property not found. +} + +static avifBool avifGainMapVersionIsSupported(const char * version) +{ + if (version == NULL) { + return AVIF_FALSE; + } + int major = 0; + int minor = 0; + char dot = 0; + const int numRead = sscanf(version, " %d %c %d", &major, &dot, &minor); + if (major != 1) { + return AVIF_FALSE; + } + if (numRead == 1) { + // Just "1" + return AVIF_TRUE; + } + if (numRead == 3 && dot == '.' && minor >= 0) { + // "1.x" + return AVIF_TRUE; + } + return AVIF_FALSE; +} + +static avifBool avifParseBoolean(const char * str, avifBool * value) +{ + if (str == NULL || value == NULL) { + return AVIF_FALSE; + } + // Trim. + while (isspace((unsigned char)*str)) { + ++str; + } + size_t len = strlen(str); + while (len > 0 && isspace((unsigned char)str[len - 1])) { + --len; + } + if ((len == 1 && str[0] == '1') || (len == 4 && (!strncmp(str, "True", 4) || !strncmp(str, "true", 4)))) { + *value = AVIF_TRUE; + return AVIF_TRUE; + } + if ((len == 1 && str[0] == '0') || (len == 5 && (!strncmp(str, "False", 5) || !strncmp(str, "false", 5)))) { + *value = AVIF_FALSE; + return AVIF_TRUE; + } + return AVIF_FALSE; +} + +// Finds an 'rdf:Description' node containing a gain map version (e.g. hdrgm:Version="1.0" or "1"). // Returns NULL if not found. static const xmlNode * avifJPEGFindIsoGainMapXMPNode(const xmlNode * rootNode) { @@ -606,20 +715,18 @@ // shall consist of only zero or more rdf:Description elements." if (node->ns && !xmlStrcmp(node->ns->href, (const xmlChar *)XML_NAME_SPACE_RDF) && !xmlStrcmp(node->name, (const xmlChar *)"Description")) { - // Look for the gain map version attribute: hdrgm:Version="1.0" - for (xmlAttr * prop = node->properties; prop != NULL; prop = prop->next) { - if (prop->ns && !xmlStrcmp(prop->ns->href, (const xmlChar *)XML_NAME_SPACE_GAIN_MAP) && - !xmlStrcmp(prop->name, (const xmlChar *)"Version") && prop->children != NULL && - !xmlStrcmp(prop->children->content, (const xmlChar *)"1.0")) { - return node; - } + const char * version; + uint32_t numValues; + if (avifJPEGFindGainMapProperty(node, "Version", /*maxValues=*/1, &version, &numValues, XML_NAME_SPACE_GAIN_MAP) && + avifGainMapVersionIsSupported(version)) { + return node; } } } return NULL; } -// Finds an 'rdf:Description' node containing a <HDRGainMap:HDRGainMapVersion> child. +// Finds an 'rdf:Description' node containing an Apple HDRGainMapVersion property. static const xmlNode * avifJPEGFindAppleGainMapXMPNode(const xmlNode * rootNode) { // See XMP specification https://github.com/adobe/XMP-Toolkit-SDK/blob/main/docs/XMPSpecificationPart1.pdf @@ -635,12 +742,10 @@ // shall consist of only zero or more rdf:Description elements." if (node->ns && !xmlStrcmp(node->ns->href, (const xmlChar *)XML_NAME_SPACE_RDF) && !xmlStrcmp(node->name, (const xmlChar *)"Description")) { - // Look for a <HDRGainMap:HDRGainMapVersion> child. - for (const xmlNode * child = node->children; child != NULL; child = child->next) { - if (child->ns && !xmlStrcmp(child->ns->href, (const xmlChar *)XML_NAME_SPACE_APPLE_GAIN_MAP) && - !xmlStrcmp(child->name, (const xmlChar *)"HDRGainMapVersion")) { - return node; - } + const char * version; + uint32_t numValues; + if (avifJPEGFindGainMapProperty(node, "HDRGainMapVersion", /*maxValues=*/1, &version, &numValues, XML_NAME_SPACE_APPLE_GAIN_MAP)) { + return node; } } } @@ -685,82 +790,34 @@ return found; } -// Finds the value of a gain map metadata property, that can be either stored as an attribute of 'descriptionNode' -// (which should point to a <rdf:Description> node) or as a child node. -// 'maxValues' is the maximum number of expected values, and the size of the 'values' array. 'numValues' is set to the number -// of values actually found (which may be smaller or larger, but only up to 'maxValues' are stored in 'values'). -// Returns AVIF_TRUE if the property was found. -static avifBool avifJPEGFindGainMapProperty(const xmlNode * descriptionNode, - const char * propertyName, - uint32_t maxValues, - const char * values[], - uint32_t * numValues, - const char * nameSpace) -{ - *numValues = 0; - - // Search attributes. - for (xmlAttr * prop = descriptionNode->properties; prop != NULL; prop = prop->next) { - if (prop->ns && !xmlStrcmp(prop->ns->href, (const xmlChar *)nameSpace) && - !xmlStrcmp(prop->name, (const xmlChar *)propertyName) && prop->children != NULL && prop->children->content != NULL) { - // Properties should have just one child containing the property's value - // (in fact the 'children' field is documented as "the value of the property"). - values[0] = (const char *)prop->children->content; - *numValues = 1; - return AVIF_TRUE; - } - } - - // Search child nodes. - for (const xmlNode * node = descriptionNode->children; node != NULL; node = node->next) { - if (node->ns && !xmlStrcmp(node->ns->href, (const xmlChar *)nameSpace) && - !xmlStrcmp(node->name, (const xmlChar *)propertyName) && node->children) { - // Multiple values can be specified with a Seq tag: <rdf:Seq><rdf:li>value1</rdf:li><rdf:li>value2</rdf:li>...</rdf:Seq> - const xmlNode * seq = avifJPEGFindXMLNodeByName(node, XML_NAME_SPACE_RDF, "Seq", /*recursive=*/AVIF_FALSE); - if (seq) { - for (xmlNode * seqChild = seq->children; seqChild; seqChild = seqChild->next) { - if (!xmlStrcmp(seqChild->name, (const xmlChar *)"li") && seqChild->children != NULL && - seqChild->children->content != NULL) { - if (*numValues < maxValues) { - values[*numValues] = (const char *)seqChild->children->content; - } - ++(*numValues); - } - } - return *numValues > 0 ? AVIF_TRUE : AVIF_FALSE; - } else if (node->children->next == NULL && node->children->type == XML_TEXT_NODE) { // Only one child and it's text. - values[0] = (const char *)node->children->content; - *numValues = 1; - return AVIF_TRUE; - } - // We found a tag for this property but no valid content. - return AVIF_FALSE; - } - } - - return AVIF_FALSE; // Property not found. -} - // Up to 3 values per property (one for each RGB channel). #define GAIN_MAP_PROPERTY_MAX_VALUES 3 // Looks for a given gain map property's double value(s), and if found, stores them in 'values'. // The 'values' array should have size at least 'numDoubles', and should be initialized with default // values for this property, since the array will be left untouched if the property is not found. +// If 'propertyFound' is not null, it is set to AVIF_TRUE if the property was present, or AVIF_FALSE otherwise. // Returns AVIF_TRUE if the property was successfully parsed, or if it was not found, since all properties // are optional. Returns AVIF_FALSE in case of error (invalid metadata XMP). static avifBool avifJPEGFindGainMapPropertyDoubles(const xmlNode * descriptionNode, const char * propertyName, double * values, uint32_t numDoubles, - const char * nameSpace) + const char * nameSpace, + avifBool * propertyFound) { + if (propertyFound != NULL) { + *propertyFound = AVIF_FALSE; + } assert(numDoubles <= GAIN_MAP_PROPERTY_MAX_VALUES); const char * textValues[GAIN_MAP_PROPERTY_MAX_VALUES]; uint32_t numValues; if (!avifJPEGFindGainMapProperty(descriptionNode, propertyName, /*maxValues=*/numDoubles, &textValues[0], &numValues, nameSpace)) { return AVIF_TRUE; // Property was not found, but it's not an error since they're all optional. } + if (propertyFound != NULL) { + *propertyFound = AVIF_TRUE; + } if (numValues != 1 && numValues != numDoubles) { return AVIF_FALSE; // Invalid, we expect either 1 or exactly numDoubles values. } @@ -787,17 +844,11 @@ return AVIF_TRUE; } -static inline void SwapDoubles(double * x, double * y) -{ - double tmp = *x; - *x = *y; - *y = tmp; -} - static avifBool avifJPEGParseGainMapXMPPropertiesAppleFormat(const xmlNode * descNode, avifGainMap * gainMap) { double hdrHeadroomLinear = 1.0; - AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "HDRGainMapHeadroom", &hdrHeadroomLinear, /*numDoubles=*/1, XML_NAME_SPACE_APPLE_GAIN_MAP)); + AVIF_CHECK( + avifJPEGFindGainMapPropertyDoubles(descNode, "HDRGainMapHeadroom", &hdrHeadroomLinear, /*numDoubles=*/1, XML_NAME_SPACE_APPLE_GAIN_MAP, NULL)); if (hdrHeadroomLinear <= 0) { return AVIF_FALSE; } @@ -837,44 +888,57 @@ return avifJPEGParseGainMapXMPPropertiesAppleFormat(descNode, gainMap); } - double baseHdrHeadroom = 0.0; - double alternateHdrHeadroom = 1.0; + const char * ns = XML_NAME_SPACE_GAIN_MAP; + uint32_t numValues; + const char * baseRenditionIsHDR; + avifBool isHDR = AVIF_FALSE; + if (avifJPEGFindGainMapProperty(descNode, "BaseRenditionIsHDR", /*maxValues=*/1, &baseRenditionIsHDR, &numValues, ns)) { + if (!avifParseBoolean(baseRenditionIsHDR, &isHDR)) { + return AVIF_FALSE; // Unexpected value. + } + } + + double baseHdrHeadroom = isHDR ? 1.0 : 0.0; + double alternateHdrHeadroom = isHDR ? 0.0 : 1.0; double gainMapMin[3] = { 0.0, 0.0, 0.0 }; double gainMapMax[3] = { 1.0, 1.0, 1.0 }; double gainMapGamma[3] = { 1.0, 1.0, 1.0 }; double baseOffset[3] = { 1.0 / 64.0, 1.0 / 64.0, 1.0 / 64.0 }; double alternateOffset[3] = { 1.0 / 64.0, 1.0 / 64.0, 1.0 / 64.0 }; - const char * ns = XML_NAME_SPACE_GAIN_MAP; - AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "HDRCapacityMin", &baseHdrHeadroom, /*numDoubles=*/1, ns)); - AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "HDRCapacityMax", &alternateHdrHeadroom, /*numDoubles=*/1, ns)); - AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "OffsetSDR", baseOffset, /*numDoubles=*/3, ns)); - AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "OffsetHDR", alternateOffset, /*numDoubles=*/3, ns)); - AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "GainMapMin", gainMapMin, /*numDoubles=*/3, ns)); - AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "GainMapMax", gainMapMax, /*numDoubles=*/3, ns)); - AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "Gamma", gainMapGamma, /*numDoubles=*/3, ns)); + avifBool hasHdrCapacityMin = AVIF_FALSE; + avifBool hasHdrCapacityMax = AVIF_FALSE; + AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, + "HDRCapacityMin", + isHDR ? &alternateHdrHeadroom : &baseHdrHeadroom, + /*numDoubles=*/1, + ns, + &hasHdrCapacityMin)); + AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, + "HDRCapacityMax", + isHDR ? &baseHdrHeadroom : &alternateHdrHeadroom, + /*numDoubles=*/1, + ns, + &hasHdrCapacityMax)); + if (!hasHdrCapacityMin && !hasHdrCapacityMax) { + // (Recent as of 2026) Apple devices use BaseHeadroom/AlternateHeadroom instead of HDRCapacityMin/HDRCapacityMax. + AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "AlternateHeadroom", &alternateHdrHeadroom, /*numDoubles=*/1, ns, NULL)); + AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "BaseHeadroom", &baseHdrHeadroom, /*numDoubles=*/1, ns, NULL)); + } + AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "OffsetSDR", isHDR ? alternateOffset : baseOffset, /*numDoubles=*/3, ns, NULL)); + AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "OffsetHDR", isHDR ? baseOffset : alternateOffset, /*numDoubles=*/3, ns, NULL)); + AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "GainMapMin", gainMapMin, /*numDoubles=*/3, ns, NULL)); + AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "GainMapMax", gainMapMax, /*numDoubles=*/3, ns, NULL)); + AVIF_CHECK(avifJPEGFindGainMapPropertyDoubles(descNode, "Gamma", gainMapGamma, /*numDoubles=*/3, ns, NULL)); - AVIF_CHECK(alternateHdrHeadroom > baseHdrHeadroom); - AVIF_CHECK(baseHdrHeadroom >= 0); for (int i = 0; i < 3; ++i) { AVIF_CHECK(gainMapMax[i] >= gainMapMin[i]); AVIF_CHECK(baseOffset[i] >= 0.0); AVIF_CHECK(alternateOffset[i] >= 0.0); AVIF_CHECK(gainMapGamma[i] > 0.0); } - - uint32_t numValues; - const char * baseRenditionIsHDR; - if (avifJPEGFindGainMapProperty(descNode, "BaseRenditionIsHDR", /*maxValues=*/1, &baseRenditionIsHDR, &numValues, ns)) { - if (!strcmp(baseRenditionIsHDR, "True")) { - SwapDoubles(&baseHdrHeadroom, &alternateHdrHeadroom); - for (int c = 0; c < 3; ++c) { - SwapDoubles(&baseOffset[c], &alternateOffset[c]); - } - } else if (!strcmp(baseRenditionIsHDR, "False")) { - } else { - return AVIF_FALSE; // Unexpected value. - } - } + AVIF_CHECK(baseHdrHeadroom >= 0.0); + AVIF_CHECK(alternateHdrHeadroom >= 0.0); + AVIF_CHECK(baseHdrHeadroom != alternateHdrHeadroom); for (int i = 0; i < 3; ++i) { AVIF_CHECK(avifDoubleToSignedFraction(gainMapMin[i], &gainMap->gainMapMin[i])); @@ -1601,8 +1665,9 @@ #if defined(AVIF_ENABLE_JPEG_GAIN_MAP_CONVERSION) if (!ignoreGainMap && hasMpfSegment(&cinfo)) { - avifBool expectIsoGainMap = AVIF_FALSE; - avifJPEGHasGainMapXMPNode(avif->xmp.data, avif->xmp.size, &expectIsoGainMap); + avifBool isAppleGainMap = AVIF_FALSE; + const avifBool hasGainMapXmp = avifJPEGHasGainMapXMPNode(avif->xmp.data, avif->xmp.size, &isAppleGainMap); + const avifBool expectIsoGainMap = hasGainMapXmp && !isAppleGainMap; avifGainMap * gainMap = avifGainMapCreate(); if (gainMap == NULL) { fprintf(stderr, "Creating gain map failed: out of memory\n");
diff --git a/tests/data/README.md b/tests/data/README.md index 31499f5..6bc284c 100644 --- a/tests/data/README.md +++ b/tests/data/README.md
@@ -648,9 +648,9 @@ | 33507 | 0xffe1 APP1 | 571 | `http://ns.adobe.com/xap/1.0/.<?x` | | | | | ... | -### File [apple_gainmap_old.jpg](apple_gainmap_old.jpg) +### File [apple_gainmap_v1.jpg](apple_gainmap_v1.jpg) - + License: [same as libavif](https://github.com/AOMediaCodec/libavif/blob/main/LICENSE) @@ -658,16 +658,24 @@ to save space. The XMP does not contain the HDR headroom, instead it has to be read from Exif metadata. -### File [apple_gainmap_new.jpg](apple_gainmap_new.jpg) +### File [apple_gainmap_v2.jpg](apple_gainmap_v2.jpg) - + License: [same as libavif](https://github.com/AOMediaCodec/libavif/blob/main/LICENSE) -Source: Same as apple_gainmap_old.jpg, but with XMP data manually copied from a +Source: Same as apple_gainmap_v1.jpg, but with XMP data manually copied from a picture taken on a more recent iPhone. The difference is that the XMP metadata contains the <HDRGainMap:HDRGainMapHeadroom> element. +### File [apple_gainmap_v3.jpg](apple_gainmap_v3.jpg) + + + +License: [same as libavif](https://github.com/AOMediaCodec/libavif/blob/main/LICENSE) + +Source: Same as apple_gainmap_v2.jpg, but with XMP copied from a picture taken on an iPhone 17. + ### File [paris_exif_xmp_icc_gainmap_bigendian.jpg](paris_exif_xmp_icc_gainmap_bigendian.jpg) 
diff --git a/tests/data/apple_gainmap_old.jpg b/tests/data/apple_gainmap_v1.jpg similarity index 100% rename from tests/data/apple_gainmap_old.jpg rename to tests/data/apple_gainmap_v1.jpg Binary files differ
diff --git a/tests/data/apple_gainmap_new.jpg b/tests/data/apple_gainmap_v2.jpg similarity index 100% rename from tests/data/apple_gainmap_new.jpg rename to tests/data/apple_gainmap_v2.jpg Binary files differ
diff --git a/tests/data/apple_gainmap_v3.jpg b/tests/data/apple_gainmap_v3.jpg new file mode 100644 index 0000000..06c51ef --- /dev/null +++ b/tests/data/apple_gainmap_v3.jpg Binary files differ
diff --git a/tests/data/goldens/apple_gainmap_old.jpg.avif.xml b/tests/data/goldens/apple_gainmap_old.jpg.avif.xml deleted file mode 100644 index 0413d8b..0000000 --- a/tests/data/goldens/apple_gainmap_old.jpg.avif.xml +++ /dev/null
@@ -1,113 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<ISOBaseMediaFileTrace> -<!--MP4Box dump trace--> -<IsoMediaFile xmlns="urn:mpeg:isobmff:schema:file:2016" Name="apple_gainmap_old.jpg.avif"> -<FileTypeBox Size="36" Type="ftyp" Specification="p12" Container="file otyp" MajorBrand="avif" MinorVersion="0"> -<BrandEntry AlternateBrand="avif"/> -<BrandEntry AlternateBrand="mif1"/> -<BrandEntry AlternateBrand="miaf"/> -<BrandEntry AlternateBrand="MA1B"/> -<BrandEntry AlternateBrand="tmap"/> -</FileTypeBox> -<MetaBox Size="1058" Type="meta" Version="0" Flags="0" Specification="p12" Container="file moov trak moof traf udta" > -<HandlerBox Size="33" Type="hdlr" Version="0" Flags="0" Specification="p12" Container="mdia meta minf" hdlrType="pict" Name="" reserved1="0" reserved2="data:application/octet-string,000000000000000000000000"> -</HandlerBox> -<PrimaryItemBox Size="14" Type="pitm" Version="0" Flags="0" Specification="p12" Container="meta" item_ID="1"> -</PrimaryItemBox> -<ItemLocationBox Size="72" Type="iloc" Version="0" Flags="0" Specification="p12" Container="meta" offset_size="4" length_size="4" base_offset_size="0" index_size="0"> -<ItemLocationEntry item_ID="1" data_reference_index="0" base_offset="0" construction_method="0"> -<ItemExtentEntry extent_offset="REDACTED" extent_length="REDACTED" extent_index="0" /> -</ItemLocationEntry> -<ItemLocationEntry item_ID="2" data_reference_index="0" base_offset="0" construction_method="0"> -<ItemExtentEntry extent_offset="REDACTED" extent_length="REDACTED" extent_index="0" /> -</ItemLocationEntry> -<ItemLocationEntry item_ID="3" data_reference_index="0" base_offset="0" construction_method="0"> -<ItemExtentEntry extent_offset="REDACTED" extent_length="REDACTED" extent_index="0" /> -</ItemLocationEntry> -<ItemLocationEntry item_ID="4" data_reference_index="0" base_offset="0" construction_method="0"> -<ItemExtentEntry extent_offset="REDACTED" extent_length="REDACTED" extent_index="0" /> -</ItemLocationEntry> -</ItemLocationBox> -<ItemInfoBox Size="115" Type="iinf" Version="0" Flags="0" Specification="p12" Container="meta" > -<ItemInfoEntryBox Size="26" Type="infe" Version="2" Flags="0" Specification="p12" Container="iinf" item_ID="1" item_protection_index="0" item_name="Color" content_type="(null)" content_encoding="(null)" item_type="av01"> -</ItemInfoEntryBox> -<ItemInfoEntryBox Size="25" Type="infe" Version="2" Flags="0" Specification="p12" Container="iinf" item_ID="2" item_protection_index="0" item_name="GMap" content_type="(null)" content_encoding="(null)" item_type="tmap"> -</ItemInfoEntryBox> -<ItemInfoEntryBox Size="25" Type="infe" Version="2" Flags="1" Specification="p12" Container="iinf" item_ID="3" item_protection_index="0" item_name="GMap" content_type="(null)" content_encoding="(null)" item_type="av01"> -</ItemInfoEntryBox> -<ItemInfoEntryBox Size="25" Type="infe" Version="2" Flags="0" Specification="p12" Container="iinf" item_ID="4" item_protection_index="0" item_name="Exif" content_type="(null)" content_encoding="(null)" item_type="Exif"> -</ItemInfoEntryBox> -</ItemInfoBox> -<ItemReferenceBox Size="42" Type="iref" Version="0" Flags="0" Specification="p12" Container="meta" > -<ItemReferenceBox Size="16" Type="dimg" Specification="p12" Container="iref" from_item_id="2"> -<ItemReferenceBoxEntry ItemID="1"/> -<ItemReferenceBoxEntry ItemID="3"/> -</ItemReferenceBox> -<ItemReferenceBox Size="14" Type="cdsc" Specification="p12" Container="iref" from_item_id="4"> -<ItemReferenceBoxEntry ItemID="1"/> -</ItemReferenceBox> -</ItemReferenceBox> -<ItemPropertiesBox Size="734" Type="iprp" Specification="iff" Container="meta" > -<ItemPropertyContainerBox Size="688" Type="ipco" Specification="iff" Container="iprp" > -<ImageSpatialExtentsPropertyBox Size="20" Type="ispe" Version="0" Flags="0" Specification="iff" Container="ipco" image_width="384" image_height="512"> -</ImageSpatialExtentsPropertyBox> -<PixelInformationPropertyBox Size="16" Type="pixi" Version="0" Flags="0" Specification="iff" Container="ipco" > -<BitPerChannel bits_per_channel="8"/> -<BitPerChannel bits_per_channel="8"/> -<BitPerChannel bits_per_channel="8"/> -</PixelInformationPropertyBox> -<AV1ConfigurationBox> -<AV1Config version="1" profile="0" level_idx0="1" tier="0" high_bitdepth="0" twelve_bit="0" monochrome="0" chroma_subsampling_x="1" chroma_subsampling_y="1" chroma_sample_position="0" initial_presentation_delay="1" OBUs_count="0"> -</AV1Config> -</AV1ConfigurationBox> -<ColourInformationBox Size="548" Type="colr" Specification="iff" Container="video_sample_entry ipco encv resv" colour_type="prof"> -<profile><![CDATA[AAACGGFwcGwEAAAAbW50clJHQiBYWVogB+YAAQABAAAAAAAAYWNzcEFQUEwAAAAAQVBQTAAAAAAAAAAAAAAAAAAAAAAAAPbWAAEAAAAA0y1hcHBs7P2jjjiFR8NttL1PetoYLwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKZGVzYwAAAPwAAAAwY3BydAAAASwAAABQd3RwdAAAAXwAAAAUclhZWgAAAZAAAAAUZ1hZWgAAAaQAAAAUYlhZWgAAAbgAAAAUclRSQwAAAcwAAAAgY2hhZAAAAewAAAAsYlRSQwAAAcwAAAAgZ1RSQwAAAcwAAAAgbWx1YwAAAAAAAAABAAAADGVuVVMAAAAUAAAAHABEAGkAcwBwAGwAYQB5ACAAUAAzbWx1YwAAAAAAAAABAAAADGVuVVMAAAA0AAAAHABDAG8AcAB5AHIAaQBnAGgAdAAgAEEAcABwAGwAZQAgAEkAbgBjAC4ALAAgADIAMAAyADJYWVogAAAAAAAA9tUAAQAAAADTLFhZWiAAAAAAAACD3wAAPb////+7WFlaIAAAAAAAAEq/AACxNwAACrlYWVogAAAAAAAAKDgAABELAADIuXBhcmEAAAAAAAMAAAACZmYAAPKnAAANWQAAE9AAAApbc2YzMgAAAAAAAQxCAAAF3v//8yYAAAeTAAD9kP//+6L///2jAAAD3AAAwG4=]]></profile></ColourInformationBox> -<ColourInformationBox Size="19" Type="colr" Specification="iff" Container="video_sample_entry ipco encv resv" colour_type="nclx" colour_primaries="2" transfer_characteristics="2" matrix_coefficients="6" full_range_flag="1"> -</ColourInformationBox> -<ColourInformationBox Size="19" Type="colr" Specification="iff" Container="video_sample_entry ipco encv resv" colour_type="nclx" colour_primaries="2" transfer_characteristics="16" matrix_coefficients="6" full_range_flag="1"> -</ColourInformationBox> -<ImageSpatialExtentsPropertyBox Size="20" Type="ispe" Version="0" Flags="0" Specification="iff" Container="ipco" image_width="192" image_height="256"> -</ImageSpatialExtentsPropertyBox> -<PixelInformationPropertyBox Size="14" Type="pixi" Version="0" Flags="0" Specification="iff" Container="ipco" > -<BitPerChannel bits_per_channel="8"/> -</PixelInformationPropertyBox> -<AV1ConfigurationBox> -<AV1Config version="1" profile="0" level_idx0="0" tier="0" high_bitdepth="0" twelve_bit="0" monochrome="1" chroma_subsampling_x="1" chroma_subsampling_y="1" chroma_sample_position="0" initial_presentation_delay="1" OBUs_count="0"> -</AV1Config> -</AV1ConfigurationBox> -</ItemPropertyContainerBox> -<ItemPropertyAssociationBox Size="38" Type="ipma" Version="0" Flags="0" Specification="iff" Container="iprp" entry_count="3"> -<AssociationEntry item_ID="1" association_count="5"> -<Property index="1" essential="0"/> -<Property index="2" essential="0"/> -<Property index="3" essential="1"/> -<Property index="4" essential="0"/> -<Property index="5" essential="0"/> -</AssociationEntry> -<AssociationEntry item_ID="2" association_count="4"> -<Property index="1" essential="0"/> -<Property index="2" essential="0"/> -<Property index="4" essential="0"/> -<Property index="6" essential="0"/> -</AssociationEntry> -<AssociationEntry item_ID="3" association_count="4"> -<Property index="7" essential="0"/> -<Property index="8" essential="0"/> -<Property index="9" essential="1"/> -<Property index="5" essential="0"/> -</AssociationEntry> -</ItemPropertyAssociationBox> -</ItemPropertiesBox> -<GroupListBox Size="36" Type="grpl" Specification="iff" Container="meta" > -<EntityToGroupTypeBox Size="28" Type="altr" Version="0" Flags="0" Specification="iff" Container="grpl" group_id="5"> -<EntityToGroupTypeBoxEntry EntityID="2"/> -<EntityToGroupTypeBoxEntry EntityID="1"/> -</EntityToGroupTypeBox> -</GroupListBox> -</MetaBox> -<MediaDataBox Size="REDACTED" Type="mdat" Specification="p12" Container="file" dataSize="REDACTED"> -</MediaDataBox> -</IsoMediaFile> -<Tracks> -</Tracks> -</ISOBaseMediaFileTrace>
diff --git a/tests/data/goldens/apple_gainmap_new.jpg.avif.xml b/tests/data/goldens/apple_gainmap_v1.jpg.avif.xml similarity index 99% copy from tests/data/goldens/apple_gainmap_new.jpg.avif.xml copy to tests/data/goldens/apple_gainmap_v1.jpg.avif.xml index 734854d..bfe1994 100644 --- a/tests/data/goldens/apple_gainmap_new.jpg.avif.xml +++ b/tests/data/goldens/apple_gainmap_v1.jpg.avif.xml
@@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <ISOBaseMediaFileTrace> <!--MP4Box dump trace--> -<IsoMediaFile xmlns="urn:mpeg:isobmff:schema:file:2016" Name="apple_gainmap_new.jpg.avif"> +<IsoMediaFile xmlns="urn:mpeg:isobmff:schema:file:2016" Name="apple_gainmap_v1.jpg.avif"> <FileTypeBox Size="36" Type="ftyp" Specification="p12" Container="file otyp" MajorBrand="avif" MinorVersion="0"> <BrandEntry AlternateBrand="avif"/> <BrandEntry AlternateBrand="mif1"/>
diff --git a/tests/data/goldens/apple_gainmap_new.jpg.avif.xml b/tests/data/goldens/apple_gainmap_v2.jpg.avif.xml similarity index 99% copy from tests/data/goldens/apple_gainmap_new.jpg.avif.xml copy to tests/data/goldens/apple_gainmap_v2.jpg.avif.xml index 734854d..ff9b3ee 100644 --- a/tests/data/goldens/apple_gainmap_new.jpg.avif.xml +++ b/tests/data/goldens/apple_gainmap_v2.jpg.avif.xml
@@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <ISOBaseMediaFileTrace> <!--MP4Box dump trace--> -<IsoMediaFile xmlns="urn:mpeg:isobmff:schema:file:2016" Name="apple_gainmap_new.jpg.avif"> +<IsoMediaFile xmlns="urn:mpeg:isobmff:schema:file:2016" Name="apple_gainmap_v2.jpg.avif"> <FileTypeBox Size="36" Type="ftyp" Specification="p12" Container="file otyp" MajorBrand="avif" MinorVersion="0"> <BrandEntry AlternateBrand="avif"/> <BrandEntry AlternateBrand="mif1"/>
diff --git a/tests/data/goldens/apple_gainmap_new.jpg.avif.xml b/tests/data/goldens/apple_gainmap_v3.jpg.avif.xml similarity index 99% rename from tests/data/goldens/apple_gainmap_new.jpg.avif.xml rename to tests/data/goldens/apple_gainmap_v3.jpg.avif.xml index 734854d..0aa7dd9 100644 --- a/tests/data/goldens/apple_gainmap_new.jpg.avif.xml +++ b/tests/data/goldens/apple_gainmap_v3.jpg.avif.xml
@@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <ISOBaseMediaFileTrace> <!--MP4Box dump trace--> -<IsoMediaFile xmlns="urn:mpeg:isobmff:schema:file:2016" Name="apple_gainmap_new.jpg.avif"> +<IsoMediaFile xmlns="urn:mpeg:isobmff:schema:file:2016" Name="apple_gainmap_v3.jpg.avif"> <FileTypeBox Size="36" Type="ftyp" Specification="p12" Container="file otyp" MajorBrand="avif" MinorVersion="0"> <BrandEntry AlternateBrand="avif"/> <BrandEntry AlternateBrand="mif1"/>
diff --git a/tests/gtest/avifjpeggainmaptest.cc b/tests/gtest/avifjpeggainmaptest.cc index 12fa468..97fbdf8 100644 --- a/tests/gtest/avifjpeggainmaptest.cc +++ b/tests/gtest/avifjpeggainmaptest.cc
@@ -108,7 +108,8 @@ TEST(JpegTest, ReadAppleJpegWithGainMap) { for (const std::string filename : - {"apple_gainmap_old.jpg", "apple_gainmap_new.jpg"}) { + {"apple_gainmap_v1.jpg", "apple_gainmap_v2.jpg", + "apple_gainmap_v3.jpg"}) { SCOPED_TRACE(filename); const ImagePtr image = testutil::ReadImage( @@ -126,16 +127,27 @@ // be read to parse the gain map. EXPECT_EQ(image->xmp.size, 0u); - const double headroom = - filename == "apple_gainmap_old.jpg" ? 3.0 : log2(4.532783); - CheckGainMapMetadata(*image->gainMap, - /*gain_map_min=*/{0.0, 0.0, 0.0}, - /*gain_map_max=*/{headroom, headroom, headroom}, - /*gain_map_gamma=*/{1.0, 1.0, 1.0}, - /*base_offset=*/{0.0, 0.0, 0.0}, - /*alternate_offset=*/{0.0, 0.0, 0.0}, - /*base_hdr_headroom=*/0.0, - /*alternate_hdr_headroom=*/headroom); + if (filename == "apple_gainmap_v3.jpg") { + CheckGainMapMetadata(*image->gainMap, + /*gain_map_min=*/{-0.332520, -0.332520, -0.332520}, + /*gain_map_max=*/{1.757545, 1.757545, 1.757545}, + /*gain_map_gamma=*/{0.873047, 0.873047, 0.873047}, + /*base_offset=*/{0.000010, 0.000010, 0.000010}, + /*alternate_offset=*/{0.000010, 0.000010, 0.000010}, + /*base_hdr_headroom=*/0.0, + /*alternate_hdr_headroom=*/1.757545); + } else { + const double headroom = + filename == "apple_gainmap_v1.jpg" ? 3.0 : log2(4.532783); + CheckGainMapMetadata(*image->gainMap, + /*gain_map_min=*/{0.0, 0.0, 0.0}, + /*gain_map_max=*/{headroom, headroom, headroom}, + /*gain_map_gamma=*/{1.0, 1.0, 1.0}, + /*base_offset=*/{0.0, 0.0, 0.0}, + /*alternate_offset=*/{0.0, 0.0, 0.0}, + /*base_hdr_headroom=*/0.0, + /*alternate_hdr_headroom=*/headroom); + } } } @@ -300,6 +312,98 @@ gain_map.get(), &is_avif_gain_map)); } +TEST(JpegTest, ParseXMPAppleToneMap) { + // Metadata matching modern iPhone photos with gain maps (e.g. iPhone photo + // from https://github.com/AOMediaCodec/libavif/issues/3440). + // These contain both Adobe Gain Map XMP metadata (with Version="1", + // BaseHeadroom, AlternateHeadroom, BaseRenditionIsHDR="0", and negative + // GainMapMin) as well as Apple HDRGainMap metadata. Note that Apple uses the + // namespace prefix HDRToneMap rather than hdrgm and writes properties as + // child elements. + const std::string xmp = R"( +<x:xmpmeta xmlns:x="adobe:ns:meta/"> + <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> + <rdf:Description rdf:about="" + xmlns:apdi="http://ns.apple.com/pixeldatainfo/1.0/" + xmlns:HDRToneMap="http://ns.adobe.com/hdr-gain-map/1.0/" + xmlns:HDRGainMap="http://ns.apple.com/HDRGainMap/1.0/"> + <apdi:NativeFormat>1278226488</apdi:NativeFormat> + <apdi:AuxiliaryImageType>urn:com:apple:photo:2020:aux:hdrgainmap</apdi:AuxiliaryImageType> + <apdi:StoredFormat>1278226488</apdi:StoredFormat> + <HDRToneMap:Version>1</HDRToneMap:Version> + <HDRToneMap:GainMapMax>1.757545</HDRToneMap:GainMapMax> + <HDRToneMap:OffsetHDR>0.000010</HDRToneMap:OffsetHDR> + <HDRToneMap:Gamma>0.873047</HDRToneMap:Gamma> + <HDRToneMap:OffsetSDR>0.000010</HDRToneMap:OffsetSDR> + <HDRToneMap:BaseRenditionIsHDR>0</HDRToneMap:BaseRenditionIsHDR> + <HDRToneMap:BaseHeadroom>0.000000</HDRToneMap:BaseHeadroom> + <HDRToneMap:AlternateHeadroom>1.757545</HDRToneMap:AlternateHeadroom> + <HDRToneMap:GainMapMin>-0.332520</HDRToneMap:GainMapMin> + <HDRGainMap:HDRGainMapVersion>131072</HDRGainMap:HDRGainMapVersion> + <HDRGainMap:HDRGainMapHeadroom>3.381224</HDRGainMap:HDRGainMapHeadroom> + </rdf:Description> + </rdf:RDF> +</x:xmpmeta> + )"; + GainMapPtr gain_map(avifGainMapCreate()); + avifBool is_apple_gain_map = AVIF_TRUE; + ASSERT_TRUE(avifJPEGParseGainMapXMP((const uint8_t*)xmp.data(), xmp.size(), + gain_map.get(), &is_apple_gain_map)); + + // The Adobe gain map metadata should take precedence over the legacy Apple + // metadata. + EXPECT_FALSE(is_apple_gain_map); + + CheckGainMapMetadata(*gain_map, + /*gain_map_min=*/{-0.332520, -0.332520, -0.332520}, + /*gain_map_max=*/{1.757545, 1.757545, 1.757545}, + /*gain_map_gamma=*/{0.873047, 0.873047, 0.873047}, + /*base_offset=*/{0.000010, 0.000010, 0.000010}, + /*alternate_offset=*/{0.000010, 0.000010, 0.000010}, + /*base_hdr_headroom=*/0.0, + /*alternate_hdr_headroom=*/1.757545); +} + +TEST(JpegTest, ParseXMPChildElements) { + // Test gain map properties specified as child elements instead of attributes, + // and BaseRenditionIsHDR="1". + // Because BaseHeadroom and AlternateHeadroom already specify the headroom of + // the base and alternate renditions, they should NOT be swapped when + // BaseRenditionIsHDR="1". However, OffsetSDR and OffsetHDR should still be + // swapped. + const std::string xmp = R"( +<x:xmpmeta xmlns:x="adobe:ns:meta/"> + <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> + <rdf:Description xmlns:hdrgm="http://ns.adobe.com/hdr-gain-map/1.0/"> + <hdrgm:Version>1</hdrgm:Version> + <hdrgm:BaseHeadroom>2.5</hdrgm:BaseHeadroom> + <hdrgm:AlternateHeadroom>0.5</hdrgm:AlternateHeadroom> + <hdrgm:GainMapMin>-0.2</hdrgm:GainMapMin> + <hdrgm:GainMapMax>2.0</hdrgm:GainMapMax> + <hdrgm:Gamma>0.9</hdrgm:Gamma> + <hdrgm:OffsetSDR>0.02</hdrgm:OffsetSDR> + <hdrgm:OffsetHDR>0.04</hdrgm:OffsetHDR> + <hdrgm:BaseRenditionIsHDR>1</hdrgm:BaseRenditionIsHDR> + </rdf:Description> + </rdf:RDF> +</x:xmpmeta> + )"; + GainMapPtr gain_map(avifGainMapCreate()); + avifBool is_apple_gain_map = AVIF_TRUE; + ASSERT_TRUE(avifJPEGParseGainMapXMP((const uint8_t*)xmp.data(), xmp.size(), + gain_map.get(), &is_apple_gain_map)); + EXPECT_FALSE(is_apple_gain_map); + + CheckGainMapMetadata(*gain_map, + /*gain_map_min=*/{-0.2, -0.2, -0.2}, + /*gain_map_max=*/{2.0, 2.0, 2.0}, + /*gain_map_gamma=*/{0.9, 0.9, 0.9}, + /*base_offset=*/{0.04, 0.04, 0.04}, + /*alternate_offset=*/{0.02, 0.02, 0.02}, + /*base_hdr_headroom=*/2.5, + /*alternate_hdr_headroom=*/0.5); +} + TEST(JpegTest, WrongVersion) { const std::string xmp = R"( <x:xmpmeta xmlns:x="adobe:ns:meta/"> @@ -316,6 +420,35 @@ gain_map.get(), &is_avif_gain_map)); } +TEST(JpegTest, VersionVariations) { + auto test_version = [](const char* version_str) { + const std::string xmp = std::string(R"( +<x:xmpmeta xmlns:x="adobe:ns:meta/"> + <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> + <rdf:Description rdf:about="" + xmlns:hdrgm="http://ns.adobe.com/hdr-gain-map/1.0/" hdrgm:Version=")") + + version_str + R"("> + </rdf:Description> + </rdf:RDF> +</x:xmpmeta> + )"; + GainMapPtr gain_map(avifGainMapCreate()); + avifBool is_apple_gain_map; + return avifJPEGParseGainMapXMP((const uint8_t*)xmp.data(), xmp.size(), + gain_map.get(), &is_apple_gain_map); + }; + + EXPECT_TRUE(test_version("1")); + EXPECT_TRUE(test_version("1.0")); + EXPECT_TRUE(test_version("1.1")); + EXPECT_FALSE(test_version("10")); + EXPECT_FALSE(test_version("10.0")); + EXPECT_FALSE(test_version("2.0")); + EXPECT_FALSE(test_version("1.")); + EXPECT_FALSE(test_version("1.abc")); + EXPECT_FALSE(test_version("")); +} + TEST(JpegTest, InvalidXMP) { const std::string xmp = R"( <x:xmpmeta xmlns:x="adobe:ns:meta/">
diff --git a/tests/test_cmd_enc_gainmap_boxes_golden.sh b/tests/test_cmd_enc_gainmap_boxes_golden.sh index b138b0e..e4ba9a1 100644 --- a/tests/test_cmd_enc_gainmap_boxes_golden.sh +++ b/tests/test_cmd_enc_gainmap_boxes_golden.sh
@@ -25,8 +25,9 @@ for f in "paris_exif_xmp_gainmap_bigendian.jpg" \ "paris_exif_xmp_icc_gainmap_bigendian.jpg" \ "paris_exif_xmp_gainmap_littleendian.jpg" \ - "apple_gainmap_old.jpg" \ - "apple_gainmap_new.jpg"; do + "apple_gainmap_v1.jpg" \ + "apple_gainmap_v2.jpg" \ + "apple_gainmap_v3.jpg"; do "${AVIFENC}" -s 9 "${TESTDATA_DIR}/$f" -o "$f.avif" done