Skip to content

Truly independent web browser

LibMediaCodecs: Replace the Vulkan codec context for larger AV1 frames

Zaggy1024284c724c60bd
diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp
index 8a59dbbb66f..7b1cafeb11c 100644
--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp
+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp
@@ -567,6 +567,16 @@ AVPixelFormat FFmpegVulkanVideoDecoder::negotiate_pixel_format(AVCodecContext* c
int FFmpegVulkanVideoDecoder::get_buffer(AVCodecContext* codec_context, AVFrame* frame, int flags)
{
auto& decoder = *static_cast<FFmpegVulkanVideoDecoder*>(codec_context->opaque);
+
+ // FFmpeg's AV1 decoder keeps its frames across a new sequence with larger frames, which would decode out of bounds.
+ if (codec_context->hw_frames_ctx != nullptr) {
+ auto const& frames_context = *reinterpret_cast<AVHWFramesContext const*>(codec_context->hw_frames_ctx->data);
+ if (frame->width > frames_context.width || frame->height > frames_context.height) {
+ decoder.m_codec_context_must_be_replaced = true;
+ return AVERROR(EINVAL);
+ }
+ }
+
MutexLocker locker { decoder.m_frames_context_mutex };
return decoder.m_functions.avcodec_default_get_buffer2(codec_context, frame, flags);
}
diff --git a/Tests/LibMedia/TestFFmpegVulkanDecode.cpp b/Tests/LibMedia/TestFFmpegVulkanDecode.cpp
index 6ff90ef0c11..bce8e67d1e9 100644
--- a/Tests/LibMedia/TestFFmpegVulkanDecode.cpp
+++ b/Tests/LibMedia/TestFFmpegVulkanDecode.cpp
@@ -185,6 +185,38 @@ TEST_CASE(vp9_inter_frame_size_change_needs_another_decoder)
EXPECT_EQ(frame.size, Gfx::Size<u32>(1280, 720));
}
+// A quality switch can start a sequence with larger frames, which FFmpeg does not renegotiate while the pixel format stays
+// the same.
+TEST_CASE(av1_sequence_with_larger_frames_continues_in_a_new_codec_context)
+{
+ auto decoding = open_decoding("./av1_sequence_size_increase.webm"sv, Media::CodecID::AV1);
+ if (!decoding.has_value())
+ return;
+
+ Vector<DecodedFrame> frames;
+ while (true) {
+ auto frame_result = decoding->next_frame();
+ if (frame_result.is_error()) {
+ EXPECT_EQ(frame_result.error().category(), Media::DecoderErrorCategory::EndOfStream);
+ break;
+ }
+ auto frame = frame_result.release_value();
+ auto surface = frame->surface();
+ EXPECT(surface);
+ if (surface) {
+ EXPECT(frame->width() <= static_cast<u32>(surface->size().width()));
+ EXPECT(frame->height() <= static_cast<u32>(surface->size().height()));
+ }
+ frames.append({ frame->timestamp(), frame->size() });
+ }
+
+ EXPECT_EQ(frames.size(), 12u);
+ if (frames.size() == 12) {
+ EXPECT_EQ(frames.first().size, Gfx::Size<u32>(320, 240));
+ EXPECT_EQ(frames.last().size, Gfx::Size<u32>(640, 480));
+ }
+}
+
// The file's mastering display and content light levels are only in metadata OBUs, which must survive the copy of a frame
// made for export.
TEST_CASE(frames_carry_the_high_dynamic_range_metadata_in_their_bitstream)
diff --git a/Tests/LibMedia/av1_sequence_size_increase.webm b/Tests/LibMedia/av1_sequence_size_increase.webm
new file mode 100644
index 00000000000..8403606e675
Binary files /dev/null and b/Tests/LibMedia/av1_sequence_size_increase.webm differ