Truly independent web browser
LibGfx: Decode AVIF frames on demand instead of all up front
Andreas Kling57214fb5211c
diff --git a/Libraries/LibGfx/ImageFormats/AVIFLoader.cpp b/Libraries/LibGfx/ImageFormats/AVIFLoader.cppindex 4c9307870e7..c5d128cc73c 100644--- a/Libraries/LibGfx/ImageFormats/AVIFLoader.cpp+++ b/Libraries/LibGfx/ImageFormats/AVIFLoader.cpp@@ -7,6 +7,7 @@#include <AK/ByteBuffer.h>#include <AK/Error.h>+#include <AK/Math.h>#include <LibGfx/ImageFormats/AVIFLoader.h>#include <avif/avif.h>@@ -22,9 +23,7 @@ public:enum State {NotDecoded = 0,- Error,HeaderDecoded,- BitmapDecoded,};State state { State::NotDecoded };@@ -39,8 +38,6 @@ public:size_t repetition_count { 0 };ByteBuffer icc_data;- Vector<ImageFrameDescriptor> frame_descriptors;-AVIFLoadingContext() = default;~AVIFLoadingContext(){@@ -100,32 +97,38 @@ static ErrorOr<void> decode_avif_header(AVIFLoadingContext& context)return {};}-static ErrorOr<void> decode_avif_image(AVIFLoadingContext& context)+static int frame_duration_in_milliseconds(AVIFLoadingContext const& context, double duration_in_seconds){- VERIFY(context.state >= AVIFLoadingContext::State::HeaderDecoded);+ // NB: The duration comes from the file, so reject NaN and negative values and clamp huge ones.+ if (context.image_count == 1 || !(duration_in_seconds >= 0))+ return 0;+ return AK::clamp_to<int>(duration_in_seconds * 1000);+}- avifRGBImage rgb;- while (avifDecoderNextImage(context.decoder) == AVIF_RESULT_OK) {- auto bitmap_format = context.has_alpha ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888;- auto bitmap = TRY(Bitmap::create_shareable_with_gpu_access(bitmap_format, AlphaType::Unpremultiplied, context.size.value()));+static ErrorOr<ImageFrameDescriptor> decode_avif_frame(AVIFLoadingContext& context, size_t index)+{+ VERIFY(context.state >= AVIFLoadingContext::State::HeaderDecoded);- avifRGBImageSetDefaults(&rgb, context.decoder->image);- rgb.pixels = bitmap->scanline_u8(0);- rgb.rowBytes = bitmap->pitch();- rgb.format = avifRGBFormat::AVIF_RGB_FORMAT_BGRA;- rgb.depth = 8;+ // NB: libavif decodes the next frame directly when frames are requested in order, does no decoding when the+ // current frame is requested again, and otherwise decodes forward from the nearest keyframe.+ if (avifDecoderNthImage(context.decoder, index) != AVIF_RESULT_OK)+ return Error::from_string_literal("Failed to decode AVIF frame");- avifResult result = avifImageYUVToRGB(context.decoder->image, &rgb);- if (result != AVIF_RESULT_OK)- return Error::from_string_literal("Conversion from YUV to RGB failed");+ auto bitmap_format = context.has_alpha ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888;+ auto bitmap = TRY(Bitmap::create_shareable_with_gpu_access(bitmap_format, AlphaType::Unpremultiplied, context.size.value()));- auto duration = context.decoder->imageCount == 1 ? 0 : static_cast<int>(context.decoder->imageTiming.duration * 1000);- context.frame_descriptors.append(ImageFrameDescriptor { bitmap, duration });+ avifRGBImage rgb;+ avifRGBImageSetDefaults(&rgb, context.decoder->image);+ rgb.pixels = bitmap->scanline_u8(0);+ rgb.rowBytes = bitmap->pitch();+ rgb.format = avifRGBFormat::AVIF_RGB_FORMAT_BGRA;+ rgb.depth = 8;- context.state = AVIFLoadingContext::BitmapDecoded;- }+ avifResult result = avifImageYUVToRGB(context.decoder->image, &rgb);+ if (result != AVIF_RESULT_OK)+ return Error::from_string_literal("Conversion from YUV to RGB failed");- return {};+ return ImageFrameDescriptor { bitmap, frame_duration_in_milliseconds(context, context.decoder->imageTiming.duration) };}IntSize AVIFImageDecoderPlugin::size()@@ -175,31 +178,22 @@ ErrorOr<ImageFrameDescriptor> AVIFImageDecoderPlugin::frame(size_t index, Optionif (index >= frame_count())return Error::from_string_literal("AVIFImageDecoderPlugin: Invalid frame index");- if (m_context->state == AVIFLoadingContext::State::Error)- return Error::from_string_literal("AVIFImageDecoderPlugin: Decoding failed");-- if (m_context->state < AVIFLoadingContext::State::BitmapDecoded) {- TRY(decode_avif_image(*m_context));- m_context->state = AVIFLoadingContext::State::BitmapDecoded;- }-- if (index >= m_context->frame_descriptors.size())- return Error::from_string_literal("AVIFImageDecoderPlugin: Invalid frame index");- return m_context->frame_descriptors[index];+ return decode_avif_frame(*m_context, index);}int AVIFImageDecoderPlugin::frame_duration(size_t index){- if (index >= m_context->frame_descriptors.size())+ if (index >= frame_count())+ return 0;++ avifImageTiming timing;+ if (avifDecoderNthImageTiming(m_context->decoder, index, &timing) != AVIF_RESULT_OK)return 0;- return m_context->frame_descriptors[index].duration;+ return frame_duration_in_milliseconds(*m_context, timing.duration);}ErrorOr<Optional<ReadonlyBytes>> AVIFImageDecoderPlugin::icc_data(){- if (m_context->state < AVIFLoadingContext::State::HeaderDecoded)- (void)frame(0);-if (!m_context->icc_data.is_empty())return m_context->icc_data;return OptionalNone {};diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cppindex 40ccb8f1c8f..4f5121749dd 100644--- a/Tests/LibGfx/TestImageDecoder.cpp+++ b/Tests/LibGfx/TestImageDecoder.cpp@@ -1564,6 +1564,36 @@ TEST_CASE(test_avif_frame_out_of_bounds)EXPECT(plugin_decoder->frame(1).is_error());}+TEST_CASE(test_avif_animated)+{+ auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("loop_forever.avif"sv)));+ EXPECT(Gfx::AVIFImageDecoderPlugin::sniff(file->bytes()));+ auto plugin_decoder = TRY_OR_FAIL(Gfx::AVIFImageDecoderPlugin::create(file->bytes()));++ EXPECT(plugin_decoder->is_animated());+ EXPECT_EQ(plugin_decoder->frame_count(), 2u);+ EXPECT_EQ(plugin_decoder->loop_count(), 0u);++ // Durations are known before any frame is decoded.+ EXPECT_EQ(plugin_decoder->frame_duration(0), 500);+ EXPECT_EQ(plugin_decoder->frame_duration(1), 500);++ // Frames can be decoded out of order and more than once.+ auto second_frame = TRY_OR_FAIL(plugin_decoder->frame(1));+ EXPECT_EQ(second_frame.image->size(), Gfx::IntSize(128, 128));+ EXPECT_EQ(second_frame.duration, 500);+ EXPECT_EQ(second_frame.image->get_pixel(64, 64), Gfx::Color(253, 253, 255));++ auto first_frame = TRY_OR_FAIL(plugin_decoder->frame(0));+ EXPECT_EQ(first_frame.duration, 500);+ EXPECT_EQ(first_frame.image->get_pixel(64, 64), Gfx::Color(1, 0, 0));++ auto second_frame_again = TRY_OR_FAIL(plugin_decoder->frame(1));+ EXPECT_EQ(second_frame_again.image->get_pixel(64, 64), Gfx::Color(253, 253, 255));++ EXPECT(plugin_decoder->frame(2).is_error());+}+TEST_CASE(test_avif_missing_pixi_property){auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("avif/missing-pixi-property.avif"sv)));