Skip to content

Commit

Permalink
Use default yuv format and depth for the gain map when converting jpe…
Browse files Browse the repository at this point in the history
…gs. (AOMediaCodec#1789)

When converting jpeg files with gain maps, the same depth and pixelFormat
were used as for the main image. However, increasing the gain map's
bit depth or changing its pixelFormat would be rarely needed, and retaining
gain map's actual depth (8 ) and pixelFormat (i.e. 400 if it's grayscale) is
a more sensible default. Different input paramters could also be added to allow
finer control if needed.
  • Loading branch information
maryla-uc authored Nov 21, 2023
1 parent 9de0ee8 commit 9dae382
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 37 deletions.
3 changes: 1 addition & 2 deletions apps/avifgainmaputil/program_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ struct ImageReadArgs {
void Init(argparse::ArgumentParser& argparse) {
argparse
.add_argument<int, PixelFormatConverter>(pixel_format, "--yuv", "-y")
.help("Output YUV format for avif")
.default_value("444");
.help("Output YUV format for avif (default = automatic)");
argparse.add_argument(depth, "--depth", "-d")
.choices({"0", "8", "10", "12"})
.help("Output depth (0 = automatic)");
Expand Down
29 changes: 10 additions & 19 deletions apps/shared/avifjpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,13 +669,7 @@ avifBool avifJPEGParseGainMapXMP(const uint8_t * xmpData, size_t xmpSize, avifGa
// See CIPA DC-007-Translation-2021 Multi-Picture Format at https://www.cipa.jp/e/std/std-sec.html
// and https://helpx.adobe.com/camera-raw/using/gain-map.html in particular Figures 1 to 6.
// Returns AVIF_FALSE if no gain map was found.
static avifBool avifJPEGExtractGainMapImageFromMpf(FILE * f,
const char * inputFilename,
const avifROData * segmentData,
avifImage * avif,
avifPixelFormat requestedFormat,
uint32_t requestedDepth,
avifChromaDownsampling chromaDownsampling)
static avifBool avifJPEGExtractGainMapImageFromMpf(FILE * f, const avifROData * segmentData, avifImage * avif, avifChromaDownsampling chromaDownsampling)
{
uint32_t offset = 0;

Expand Down Expand Up @@ -768,10 +762,10 @@ static avifBool avifJPEGExtractGainMapImageFromMpf(FILE * f,
// be gain maps. This could be fixed by having a helper function to get just the XMP without
// decoding the whole image.
if (!avifJPEGReadInternal(f,
inputFilename,
"gain map",
avif,
requestedFormat,
requestedDepth,
/*requestedFormat=*/AVIF_PIXEL_FORMAT_NONE, // automatic
/*requestedDepth=*/0, // automatic
chromaDownsampling,
/*ignoreColorProfile=*/AVIF_TRUE,
/*ignoreExif=*/AVIF_TRUE,
Expand All @@ -793,13 +787,7 @@ static avifBool avifJPEGExtractGainMapImageFromMpf(FILE * f,
// See CIPA DC-007-Translation-2021 Multi-Picture Format at https://www.cipa.jp/e/std/std-sec.html
// and https://helpx.adobe.com/camera-raw/using/gain-map.html
// Returns AVIF_TRUE if a gain map was found.
static avifBool avifJPEGExtractGainMapImage(FILE * f,
const char * inputFilename,
struct jpeg_decompress_struct * cinfo,
avifGainMap * gainMap,
avifPixelFormat requestedFormat,
uint32_t requestedDepth,
avifChromaDownsampling chromaDownsampling)
static avifBool avifJPEGExtractGainMapImage(FILE * f, struct jpeg_decompress_struct * cinfo, avifGainMap * gainMap, avifChromaDownsampling chromaDownsampling)
{
const avifROData tagMpf = { (const uint8_t *)AVIF_JPEG_MPF_HEADER, AVIF_JPEG_MPF_HEADER_LENGTH };
for (jpeg_saved_marker_ptr marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
Expand All @@ -809,9 +797,12 @@ static avifBool avifJPEGExtractGainMapImage(FILE * f,
if ((marker->marker == (JPEG_APP0 + 2)) && (marker->data_length > tagMpf.size) &&
!memcmp(marker->data, tagMpf.data, tagMpf.size)) {
avifImage * image = avifImageCreateEmpty();
// Set jpeg native matrix coefficients to allow copying YUV values directly.
image->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_BT601;
assert(avifJPEGHasCompatibleMatrixCoefficients(image->matrixCoefficients));

const avifROData mpfData = { (const uint8_t *)marker->data + tagMpf.size, marker->data_length - tagMpf.size };
if (!avifJPEGExtractGainMapImageFromMpf(f, inputFilename, &mpfData, image, requestedFormat, requestedDepth, chromaDownsampling)) {
if (!avifJPEGExtractGainMapImageFromMpf(f, &mpfData, image, chromaDownsampling)) {
fprintf(stderr, "Note: XMP metadata indicated the presence of a gain map, but it could not be found or decoded\n");
avifImageDestroy(image);
return AVIF_FALSE;
Expand Down Expand Up @@ -1145,7 +1136,7 @@ static avifBool avifJPEGReadInternal(FILE * f,
// The primary XMP block (for the main image) must contain a node with an hdrgm:Version field if and only if a gain map is present.
if (!ignoreGainMap && avifJPEGHasGainMapXMPNode(avif->xmp.data, avif->xmp.size)) {
// Ignore the return value: continue even if we fail to find/parse/decode the gain map.
avifJPEGExtractGainMapImage(f, inputFilename, &cinfo, &avif->gainMap, requestedFormat, requestedDepth, chromaDownsampling);
avifJPEGExtractGainMapImage(f, &cinfo, &avif->gainMap, chromaDownsampling);
}

if (avif->xmp.size > 0 && ignoreXMP) {
Expand Down
19 changes: 11 additions & 8 deletions tests/data/goldens/paris_exif_xmp_gainmap_bigendian.jpg.avif.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<BrandEntry AlternateBrand="miaf"/>
<BrandEntry AlternateBrand="MA1B"/>
</FileTypeBox>
<MetaBox Size="573" Type="meta" Version="0" Flags="0" Specification="p12" Container="file moov trak moof traf udta" >
<MetaBox Size="587" Type="meta" Version="0" Flags="0" Specification="p12" Container="file moov trak moof traf udta" >
<HandlerBox Size="40" Type="hdlr" Version="0" Flags="0" Specification="p12" Container="mdia meta minf" hdlrType="pict" Name="libavif" 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">
Expand Down Expand Up @@ -54,8 +54,8 @@
<ItemReferenceBoxEntry ItemID="1"/>
</ItemReferenceBox>
</ItemReferenceBox>
<ItemPropertiesBox Size="170" Type="iprp" Specification="iff" Container="meta" >
<ItemPropertyContainerBox Size="126" Type="ipco" Specification="iff" Container="iprp" >
<ItemPropertiesBox Size="184" Type="iprp" Specification="iff" Container="meta" >
<ItemPropertyContainerBox Size="140" Type="ipco" Specification="iff" Container="iprp" >
<ImageSpatialExtentsPropertyBox Size="20" Type="ispe" Version="0" Flags="0" Specification="iff" Container="ipco" image_width="403" image_height="302">
</ImageSpatialExtentsPropertyBox>
<PixelInformationPropertyBox Size="16" Type="pixi" Version="0" Flags="0" Specification="iff" Container="ipco" >
Expand All @@ -71,11 +71,14 @@
</ColourInformationBox>
<ImageSpatialExtentsPropertyBox Size="20" Type="ispe" Version="0" Flags="0" Specification="iff" Container="ipco" image_width="512" image_height="384">
</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="1" level_idx0="1" tier="0" high_bitdepth="0" twelve_bit="0" monochrome="0" chroma_subsampling_x="0" chroma_subsampling_y="0" chroma_sample_position="0" initial_presentation_delay="1" OBUs_count="0">
<AV1Config version="1" profile="0" level_idx0="1" 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>
<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="2" full_range_flag="1">
<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>
</ItemPropertyContainerBox>
<ItemPropertyAssociationBox Size="36" Type="ipma" Version="0" Flags="0" Specification="iff" Container="iprp" entry_count="3">
Expand All @@ -92,9 +95,9 @@
</AssociationEntry>
<AssociationEntry item_ID="3" association_count="4">
<Property index="5" essential="0"/>
<Property index="2" essential="0"/>
<Property index="6" essential="1"/>
<Property index="7" essential="0"/>
<Property index="6" essential="0"/>
<Property index="7" essential="1"/>
<Property index="8" essential="0"/>
</AssociationEntry>
</ItemPropertyAssociationBox>
</ItemPropertiesBox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<BrandEntry AlternateBrand="miaf"/>
<BrandEntry AlternateBrand="MA1B"/>
</FileTypeBox>
<MetaBox Size="573" Type="meta" Version="0" Flags="0" Specification="p12" Container="file moov trak moof traf udta" >
<MetaBox Size="587" Type="meta" Version="0" Flags="0" Specification="p12" Container="file moov trak moof traf udta" >
<HandlerBox Size="40" Type="hdlr" Version="0" Flags="0" Specification="p12" Container="mdia meta minf" hdlrType="pict" Name="libavif" 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">
Expand Down Expand Up @@ -54,8 +54,8 @@
<ItemReferenceBoxEntry ItemID="1"/>
</ItemReferenceBox>
</ItemReferenceBox>
<ItemPropertiesBox Size="170" Type="iprp" Specification="iff" Container="meta" >
<ItemPropertyContainerBox Size="126" Type="ipco" Specification="iff" Container="iprp" >
<ItemPropertiesBox Size="184" Type="iprp" Specification="iff" Container="meta" >
<ItemPropertyContainerBox Size="140" Type="ipco" Specification="iff" Container="iprp" >
<ImageSpatialExtentsPropertyBox Size="20" Type="ispe" Version="0" Flags="0" Specification="iff" Container="ipco" image_width="403" image_height="302">
</ImageSpatialExtentsPropertyBox>
<PixelInformationPropertyBox Size="16" Type="pixi" Version="0" Flags="0" Specification="iff" Container="ipco" >
Expand All @@ -71,11 +71,14 @@
</ColourInformationBox>
<ImageSpatialExtentsPropertyBox Size="20" Type="ispe" Version="0" Flags="0" Specification="iff" Container="ipco" image_width="512" image_height="384">
</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="1" level_idx0="1" tier="0" high_bitdepth="0" twelve_bit="0" monochrome="0" chroma_subsampling_x="0" chroma_subsampling_y="0" chroma_sample_position="0" initial_presentation_delay="1" OBUs_count="0">
<AV1Config version="1" profile="0" level_idx0="1" 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>
<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="2" full_range_flag="1">
<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>
</ItemPropertyContainerBox>
<ItemPropertyAssociationBox Size="36" Type="ipma" Version="0" Flags="0" Specification="iff" Container="iprp" entry_count="3">
Expand All @@ -92,9 +95,9 @@
</AssociationEntry>
<AssociationEntry item_ID="3" association_count="4">
<Property index="5" essential="0"/>
<Property index="2" essential="0"/>
<Property index="6" essential="1"/>
<Property index="7" essential="0"/>
<Property index="6" essential="0"/>
<Property index="7" essential="1"/>
<Property index="8" essential="0"/>
</AssociationEntry>
</ItemPropertyAssociationBox>
</ItemPropertiesBox>
Expand Down

0 comments on commit 9dae382

Please sign in to comment.