Skip to content

Make decoder reset preserve generated alpha grids - #3310

Open
carrerasdarren-cell wants to merge 1 commit into
AOMediaCodec:mainfrom
carrerasdarren-cell:fix/decoder-reset-generated-alpha-grid
Open

Make decoder reset preserve generated alpha grids#3310
carrerasdarren-cell wants to merge 1 commit into
AOMediaCodec:mainfrom
carrerasdarren-cell:fix/decoder-reset-generated-alpha-grid

Conversation

@carrerasdarren-cell

Copy link
Copy Markdown

avifMetaFindAlphaItem() synthesizes an alpha grid when a color grid's
individual tiles each have alpha auxiliaries. The first reset rewrites those
alpha tile dimgForID associations to the generated grid. A later
avifDecoderReset() then mistakes the rewritten associations for malformed
input and returns AVIF_RESULT_INVALID_IMAGE_GRID.

Record and reuse the generated alpha grid on later resets, and avoid appending
duplicate adopted codec configuration properties to grid items. Extend the
existing color_grid_alpha_nogrid.avif regression test to decode successfully
across three passes with two resets.

Tests:

  • Full CTest suite with system AOM and dav1d: 50/50 passed
  • avifdecodetest with UndefinedBehaviorSanitizer: 8/8 passed
  • 10,000 alternating full/color-only resets on the affected fixture

@y-guyon

y-guyon commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Thank you for your interest in libavif.

This behavior may no longer be compliant (assuming it was so far): #1203 (comment)

@carrerasdarren-cell

Copy link
Copy Markdown
Author

Thanks for flagging this. This PR only makes repeated decoder resets consistent with libavif's existing synthesized-alpha-grid behavior; the generated item is internal and the patch does not introduce a new association in the input file.

The cited draft does make the premise of that compatibility path questionable. If libavif removes support for carrying tile alpha into a derived color grid, this patch and its reset regression should not land as-is. I suggest holding #3310 until the direction in #1203 is decided. If the behavior remains as an optional compatibility path, the reset failure still needs this kind of fix and I can adjust the patch to the preferred design.

Comment thread src/read.c
Comment on lines 1649 to +1662
// Adopt the configuration property of the first image item tile, so that it can be queried from
// the top-level color/alpha item during avifDecoderReset().
const avifCodecType codecType = avifGetCodecType(item->type);
const char * configPropName = avifGetConfigurationPropertyName(codecType);
const avifProperty * srcProp = avifPropertyArrayFind(&item->properties, configPropName);
if (!srcProp) {
avifDiagnosticsPrintf(&decoder->diag, "Grid image's first tile is missing an %s property", configPropName);
return AVIF_RESULT_INVALID_IMAGE_GRID;
}
avifProperty * dstProp = (avifProperty *)avifArrayPush(&gridItem->properties);
AVIF_CHECKERR(dstProp != NULL, AVIF_RESULT_OUT_OF_MEMORY);
*dstProp = *srcProp;
if (!avifPropertyArrayFind(&gridItem->properties, configPropName)) {
avifProperty * dstProp = (avifProperty *)avifArrayPush(&gridItem->properties);
AVIF_CHECKERR(dstProp != NULL, AVIF_RESULT_OUT_OF_MEMORY);
*dstProp = *srcProp;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change is unrelated to alpha.

If I understand correctly, at head every consecutive call to avifDecoderReset() will allocate a new property if the input file contains a grid. This is why a check is added here to avoid adding the property if it already exists. Is this correct?

This is a bit late in the libavif development process, but maybe avifDecoderReset() should not create "fake" properties. That should be done in avifDecoderParse() only in my opinion.

I sent #3311 to avoid having to create that fake property at all. That does not fix the other issue about alpha.

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.

Yes, that is correct: the added check prevents each avifDecoderReset() call from appending another copied codec property. I agree that #3311 removes the need for that part of this patch entirely. I will keep #3310 on hold pending the direction in #1203; if the compatibility behavior remains, I will rebase on #3311 and retain only the generated-alpha identity/reset handling and its regression test.

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