Do not reuse a previous alpha frame in aom and avm (#3420) aomCodecGetNextImage() kept the aom_image_t returned by aom_codec_get_frame() in codec->internal->image so that an alpha sample that outputs no frame could reuse the previous one. libaom only keeps that image valid until the next aom_codec_decode() call, and the reuse happened right after aom_codec_decode() was called for the new sample, so the alpha plane pointed to a frame buffer libaom had released and could reuse. avmCodecGetNextImage() did the same with libavm. Fail when a sample outputs no frame, as dav1d and libgav1 already do, and use the frame of the current call only, so the aom and avm codecs no longer keep a decoder image across calls. The same branch in codec_dav1d.c and codec_libgav1.c could not be reached anymore and is removed.
diff --git a/CHANGELOG.md b/CHANGELOG.md index fd1b203..6d299a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md
@@ -65,6 +65,9 @@ tile if colour tiles exist. * Fix a use-after-free in avifEncoderAddImageGrid() with a Sample Transform recipe when the right-most or bottom-most cells are smaller than the others. +* Fail to decode an alpha sample that outputs no frame with the aom and avm + decoders, as with dav1d and libgav1. The previous alpha frame was reused, + although libaom and libavm invalidate it when the next sample is decoded. ## [1.4.2] - 2026-05-26
diff --git a/src/codec_aom.c b/src/codec_aom.c index 7725db4..399c7bb 100644 --- a/src/codec_aom.c +++ b/src/codec_aom.c
@@ -58,7 +58,6 @@ avifBool decoderInitialized; aom_codec_ctx_t decoder; aom_codec_iter_t iter; - aom_image_t * image; #endif #if defined(AVIF_CODEC_AOM_ENCODE) @@ -193,14 +192,12 @@ } } - if (nextFrame) { - codec->internal->image = nextFrame; - } else { - if (alpha && codec->internal->image) { - // Special case: reuse last alpha frame - } else { - return AVIF_FALSE; - } + // The frames returned by aom_codec_get_frame() are only valid until the next + // aom_codec_decode() call, so a frame output for a previous sample cannot be + // reused. A sample must output a frame. + if (!nextFrame) { + avifDiagnosticsPrintf(codec->diag, "The AV1 sample did not output a frame"); + return AVIF_FALSE; } avifBool isColor = !alpha; @@ -208,7 +205,7 @@ // Color (YUV) planes - set image to correct size / format, fill color avifPixelFormat yuvFormat = AVIF_PIXEL_FORMAT_NONE; - switch (codec->internal->image->fmt) { + switch (nextFrame->fmt) { case AOM_IMG_FMT_I420: case AOM_IMG_FMT_I42016: yuvFormat = AVIF_PIXEL_FORMAT_YUV420; @@ -232,41 +229,41 @@ default: return AVIF_FALSE; } - if (codec->internal->image->monochrome) { + if (nextFrame->monochrome) { yuvFormat = AVIF_PIXEL_FORMAT_YUV400; } - image->width = codec->internal->image->d_w; - image->height = codec->internal->image->d_h; - image->depth = codec->internal->image->bit_depth; + image->width = nextFrame->d_w; + image->height = nextFrame->d_h; + image->depth = nextFrame->bit_depth; image->yuvFormat = yuvFormat; - image->yuvRange = (codec->internal->image->range == AOM_CR_STUDIO_RANGE) ? AVIF_RANGE_LIMITED : AVIF_RANGE_FULL; - image->yuvChromaSamplePosition = (avifChromaSamplePosition)codec->internal->image->csp; + image->yuvRange = (nextFrame->range == AOM_CR_STUDIO_RANGE) ? AVIF_RANGE_LIMITED : AVIF_RANGE_FULL; + image->yuvChromaSamplePosition = (avifChromaSamplePosition)nextFrame->csp; - image->colorPrimaries = (avifColorPrimaries)codec->internal->image->cp; - image->transferCharacteristics = (avifTransferCharacteristics)codec->internal->image->tc; - image->matrixCoefficients = (avifMatrixCoefficients)codec->internal->image->mc; + image->colorPrimaries = (avifColorPrimaries)nextFrame->cp; + image->transferCharacteristics = (avifTransferCharacteristics)nextFrame->tc; + image->matrixCoefficients = (avifMatrixCoefficients)nextFrame->mc; // Steal the pointers from the decoder's image directly avifImageFreePlanes(image, AVIF_PLANES_YUV); int yuvPlaneCount = (yuvFormat == AVIF_PIXEL_FORMAT_YUV400) ? 1 : 3; for (int yuvPlane = 0; yuvPlane < yuvPlaneCount; ++yuvPlane) { - image->yuvPlanes[yuvPlane] = codec->internal->image->planes[yuvPlane]; - image->yuvRowBytes[yuvPlane] = codec->internal->image->stride[yuvPlane]; + image->yuvPlanes[yuvPlane] = nextFrame->planes[yuvPlane]; + image->yuvRowBytes[yuvPlane] = nextFrame->stride[yuvPlane]; } image->imageOwnsYUVPlanes = AVIF_FALSE; } else { // Alpha plane - set image to correct size, fill alpha - image->width = codec->internal->image->d_w; - image->height = codec->internal->image->d_h; - image->depth = codec->internal->image->bit_depth; + image->width = nextFrame->d_w; + image->height = nextFrame->d_h; + image->depth = nextFrame->bit_depth; avifImageFreePlanes(image, AVIF_PLANES_A); - image->alphaPlane = codec->internal->image->planes[0]; - image->alphaRowBytes = codec->internal->image->stride[0]; - *isLimitedRangeAlpha = (codec->internal->image->range == AOM_CR_STUDIO_RANGE); + image->alphaPlane = nextFrame->planes[0]; + image->alphaRowBytes = nextFrame->stride[0]; + *isLimitedRangeAlpha = (nextFrame->range == AOM_CR_STUDIO_RANGE); image->imageOwnsAlphaPlane = AVIF_FALSE; }
diff --git a/src/codec_avm.c b/src/codec_avm.c index 77b795a..a027bb5 100644 --- a/src/codec_avm.c +++ b/src/codec_avm.c
@@ -21,7 +21,6 @@ avifBool decoderInitialized; avm_codec_ctx_t decoder; avm_codec_iter_t iter; - avm_image_t * image; avifBool encoderInitialized; avm_codec_ctx_t encoder; @@ -104,14 +103,12 @@ } } - if (nextFrame) { - codec->internal->image = nextFrame; - } else { - if (alpha && codec->internal->image) { - // Special case: reuse last alpha frame - } else { - return AVIF_FALSE; - } + // The frames returned by avm_codec_get_frame() are only valid until the next + // avm_codec_decode() call, so a frame output for a previous sample cannot be + // reused. A sample must output a frame. + if (!nextFrame) { + avifDiagnosticsPrintf(codec->diag, "The AV2 sample did not output a frame"); + return AVIF_FALSE; } avifBool isColor = !alpha; @@ -119,7 +116,7 @@ // Color (YUV) planes - set image to correct size / format, fill color avifPixelFormat yuvFormat = AVIF_PIXEL_FORMAT_NONE; - switch (codec->internal->image->fmt) { + switch (nextFrame->fmt) { case AVM_IMG_FMT_I420: case AVM_IMG_FMT_I42016: yuvFormat = AVIF_PIXEL_FORMAT_YUV420; @@ -138,44 +135,44 @@ default: return AVIF_FALSE; } - if (codec->internal->image->monochrome) { + if (nextFrame->monochrome) { yuvFormat = AVIF_PIXEL_FORMAT_YUV400; } - image->width = codec->internal->image->d_w; - image->height = codec->internal->image->d_h; - image->depth = codec->internal->image->bit_depth; + image->width = nextFrame->d_w; + image->height = nextFrame->d_h; + image->depth = nextFrame->bit_depth; image->yuvFormat = yuvFormat; - image->yuvRange = (codec->internal->image->range == AVM_CR_STUDIO_RANGE) ? AVIF_RANGE_LIMITED : AVIF_RANGE_FULL; - if (codec->internal->image->csp == AVM_CSP_LEFT) { + image->yuvRange = (nextFrame->range == AVM_CR_STUDIO_RANGE) ? AVIF_RANGE_LIMITED : AVIF_RANGE_FULL; + if (nextFrame->csp == AVM_CSP_LEFT) { // CSP_LEFT: Horizontal offset 0, vertical offset 0.5 image->yuvChromaSamplePosition = AVIF_CHROMA_SAMPLE_POSITION_VERTICAL; - } else if (codec->internal->image->csp == AVM_CSP_CENTER) { + } else if (nextFrame->csp == AVM_CSP_CENTER) { // CSP_CENTER: Horizontal offset 0.5, vertical offset 0.5 image->yuvChromaSamplePosition = AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN; - } else if (codec->internal->image->csp == AVM_CSP_TOPLEFT) { + } else if (nextFrame->csp == AVM_CSP_TOPLEFT) { // CSP_TOPLEFT: Horizontal offset 0, vertical offset 0 image->yuvChromaSamplePosition = AVIF_CHROMA_SAMPLE_POSITION_COLOCATED; } else { image->yuvChromaSamplePosition = AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN; } - image->colorPrimaries = (avifColorPrimaries)codec->internal->image->cp; - image->transferCharacteristics = (avifTransferCharacteristics)codec->internal->image->tc; - image->matrixCoefficients = (avifMatrixCoefficients)codec->internal->image->mc; + image->colorPrimaries = (avifColorPrimaries)nextFrame->cp; + image->transferCharacteristics = (avifTransferCharacteristics)nextFrame->tc; + image->matrixCoefficients = (avifMatrixCoefficients)nextFrame->mc; avifImageFreePlanes(image, AVIF_PLANES_YUV); int yuvPlaneCount = (yuvFormat == AVIF_PIXEL_FORMAT_YUV400) ? 1 : 3; // avifImage assumes that a depth of 8 bits means an 8-bit buffer. // avm_image does not. The buffer depth depends on fmt|AVM_IMG_FMT_HIGHBITDEPTH, even for 8-bit values. - if (!avifImageUsesU16(image) && (codec->internal->image->fmt & AVM_IMG_FMT_HIGHBITDEPTH)) { + if (!avifImageUsesU16(image) && (nextFrame->fmt & AVM_IMG_FMT_HIGHBITDEPTH)) { AVIF_CHECK(avifImageAllocatePlanes(image, AVIF_PLANES_YUV) == AVIF_RESULT_OK); for (int yuvPlane = 0; yuvPlane < yuvPlaneCount; ++yuvPlane) { const uint32_t planeWidth = avifImagePlaneWidth(image, yuvPlane); const uint32_t planeHeight = avifImagePlaneHeight(image, yuvPlane); - const uint8_t * srcRow = codec->internal->image->planes[yuvPlane]; + const uint8_t * srcRow = nextFrame->planes[yuvPlane]; uint8_t * dstRow = avifImagePlane(image, yuvPlane); const uint32_t dstRowBytes = avifImagePlaneRowBytes(image, yuvPlane); for (uint32_t y = 0; y < planeHeight; ++y) { @@ -183,45 +180,45 @@ for (uint32_t x = 0; x < planeWidth; ++x) { dstRow[x] = (uint8_t)srcRow16[x]; } - srcRow += codec->internal->image->stride[yuvPlane]; + srcRow += nextFrame->stride[yuvPlane]; dstRow += dstRowBytes; } } } else { // Steal the pointers from the decoder's image directly for (int yuvPlane = 0; yuvPlane < yuvPlaneCount; ++yuvPlane) { - image->yuvPlanes[yuvPlane] = codec->internal->image->planes[yuvPlane]; - image->yuvRowBytes[yuvPlane] = codec->internal->image->stride[yuvPlane]; + image->yuvPlanes[yuvPlane] = nextFrame->planes[yuvPlane]; + image->yuvRowBytes[yuvPlane] = nextFrame->stride[yuvPlane]; } image->imageOwnsYUVPlanes = AVIF_FALSE; } } else { // Alpha plane - set image to correct size, fill alpha - image->width = codec->internal->image->d_w; - image->height = codec->internal->image->d_h; - image->depth = codec->internal->image->bit_depth; + image->width = nextFrame->d_w; + image->height = nextFrame->d_h; + image->depth = nextFrame->bit_depth; avifImageFreePlanes(image, AVIF_PLANES_A); - if (!avifImageUsesU16(image) && (codec->internal->image->fmt & AVM_IMG_FMT_HIGHBITDEPTH)) { + if (!avifImageUsesU16(image) && (nextFrame->fmt & AVM_IMG_FMT_HIGHBITDEPTH)) { AVIF_CHECK(avifImageAllocatePlanes(image, AVIF_PLANES_A) == AVIF_RESULT_OK); - const uint8_t * srcRow = codec->internal->image->planes[0]; + const uint8_t * srcRow = nextFrame->planes[0]; uint8_t * dstRow = image->alphaPlane; for (uint32_t y = 0; y < image->height; ++y) { const uint16_t * srcRow16 = (const uint16_t *)srcRow; for (uint32_t x = 0; x < image->width; ++x) { dstRow[x] = (uint8_t)srcRow16[x]; } - srcRow += codec->internal->image->stride[0]; + srcRow += nextFrame->stride[0]; dstRow += image->alphaRowBytes; } } else { - image->alphaPlane = codec->internal->image->planes[0]; - image->alphaRowBytes = codec->internal->image->stride[0]; + image->alphaPlane = nextFrame->planes[0]; + image->alphaRowBytes = nextFrame->stride[0]; image->imageOwnsAlphaPlane = AVIF_FALSE; } - *isLimitedRangeAlpha = (codec->internal->image->range == AVM_CR_STUDIO_RANGE); + *isLimitedRangeAlpha = (nextFrame->range == AVM_CR_STUDIO_RANGE); } return AVIF_TRUE;
diff --git a/src/codec_dav1d.c b/src/codec_dav1d.c index 2f1210a..4830e8f 100644 --- a/src/codec_dav1d.c +++ b/src/codec_dav1d.c
@@ -155,18 +155,13 @@ } } while (res == 0); - if (gotPicture) { - dav1d_picture_unref(&codec->internal->dav1dPicture); - codec->internal->dav1dPicture = nextFrame; - codec->internal->colorRange = codec->internal->dav1dPicture.seq_hdr->color_range ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; - codec->internal->hasPicture = AVIF_TRUE; - } else { - if (alpha && codec->internal->hasPicture) { - // Special case: reuse last alpha frame - } else { - return AVIF_FALSE; - } + if (!gotPicture) { + return AVIF_FALSE; } + dav1d_picture_unref(&codec->internal->dav1dPicture); + codec->internal->dav1dPicture = nextFrame; + codec->internal->colorRange = codec->internal->dav1dPicture.seq_hdr->color_range ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; + codec->internal->hasPicture = AVIF_TRUE; Dav1dPicture * dav1dImage = &codec->internal->dav1dPicture; avifBool isColor = !alpha;
diff --git a/src/codec_libgav1.c b/src/codec_libgav1.c index a529b5a..28e7c67 100644 --- a/src/codec_libgav1.c +++ b/src/codec_libgav1.c
@@ -64,16 +64,11 @@ } // Got an image! - if (nextFrame) { - codec->internal->gav1Image = nextFrame; - codec->internal->colorRange = (nextFrame->color_range == kLibgav1ColorRangeStudio) ? AVIF_RANGE_LIMITED : AVIF_RANGE_FULL; - } else { - if (alpha && codec->internal->gav1Image) { - // Special case: reuse last alpha frame - } else { - return AVIF_FALSE; - } + if (!nextFrame) { + return AVIF_FALSE; } + codec->internal->gav1Image = nextFrame; + codec->internal->colorRange = (nextFrame->color_range == kLibgav1ColorRangeStudio) ? AVIF_RANGE_LIMITED : AVIF_RANGE_FULL; const Libgav1DecoderBuffer * gav1Image = codec->internal->gav1Image; avifBool isColor = !alpha;
diff --git a/tests/data/README.md b/tests/data/README.md index 655bc22..b1a3ef6 100644 --- a/tests/data/README.md +++ b/tests/data/README.md
@@ -1066,6 +1066,21 @@ Source: Random frames generated with libavif. +### File [animated_alpha_sample_without_shown_frame.avif](animated_alpha_sample_without_shown_frame.avif) + + + +License: [same as libavif](https://github.com/AOMediaCodec/libavif/blob/main/LICENSE) + +Source: two 64x64 frames (YUV444 with alpha) encoded with `avifenc -c aom -s 6`. +The two samples of the alpha track were then replaced with the first two samples +of six 64x64 gray frames encoded with `avifenc -c aom -s 6 -a film-grain-test=1`, +the second one cut before its shown frame. These samples were appended to the +`mdat` box, and the `stsz` and `stco` boxes of the alpha track were updated. + +The second alpha sample only holds a temporal delimiter and two hidden frames, +so it outputs no frame and the second frame cannot be decoded. + ### File [poc_b_506387278.avif](poc_b_506387278.avif) 
diff --git a/tests/data/animated_alpha_sample_without_shown_frame.avif b/tests/data/animated_alpha_sample_without_shown_frame.avif new file mode 100644 index 0000000..7d1636b --- /dev/null +++ b/tests/data/animated_alpha_sample_without_shown_frame.avif Binary files differ
diff --git a/tests/gtest/avifanimationtest.cc b/tests/gtest/avifanimationtest.cc index 5e67583..6eb37e2 100644 --- a/tests/gtest/avifanimationtest.cc +++ b/tests/gtest/avifanimationtest.cc
@@ -101,6 +101,38 @@ AVIF_RESULT_NO_IMAGES_REMAINING); } +// The second sample of the alpha track only holds hidden frames, so it outputs +// no frame. The alpha plane of the first frame must not be reused for the +// second frame: the AV1 decoder may have released its buffer meanwhile. The +// alpha is in limited range, so avifDecoderNextImage() reads it. +TEST(AvifDecodeTest, AnimatedImageWithAlphaSampleWithoutShownFrame) { + const char* file_name = "animated_alpha_sample_without_shown_frame.avif"; + for (avifCodecChoice codec_choice : + {AVIF_CODEC_CHOICE_AOM, AVIF_CODEC_CHOICE_DAV1D, + AVIF_CODEC_CHOICE_LIBGAV1}) { + const char* codec_name = + avifCodecName(codec_choice, AVIF_CODEC_FLAG_CAN_DECODE); + if (codec_name == nullptr) { + continue; + } + SCOPED_TRACE(codec_name); + DecoderPtr decoder(avifDecoderCreate()); + ASSERT_NE(decoder, nullptr); + decoder->codecChoice = codec_choice; + ASSERT_EQ(avifDecoderSetIOFile( + decoder.get(), (std::string(data_path) + file_name).c_str()), + AVIF_RESULT_OK); + ASSERT_EQ(avifDecoderParse(decoder.get()), AVIF_RESULT_OK); + EXPECT_EQ(decoder->alphaPresent, AVIF_TRUE); + EXPECT_EQ(decoder->imageCount, 2); + + ASSERT_EQ(avifDecoderNextImage(decoder.get()), AVIF_RESULT_OK); + EXPECT_NE(decoder->image->alphaPlane, nullptr); + EXPECT_EQ(avifDecoderNextImage(decoder.get()), + AVIF_RESULT_DECODE_ALPHA_FAILED); + } +} + //------------------------------------------------------------------------------ // The alpha track of colors-animated-8bpc-alpha-exif-xmp.avif holds a 20 byte // 'tref' box at offset 1586, with a single 'auxl' box naming track 1, directly