Truly independent web browser
LibMediaCodecs+Tests: Convert FFmpeg's gray formats to 4:2:0
Zaggy10243d6d3bb3be21
diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.cpp b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.cppindex 1d35c24f0b5..f43d002687e 100644--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.cpp+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.cpp@@ -4,6 +4,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/GenericShorthands.h>#include <LibCore/System.h>#include <LibGfx/YUVData.h>#include <LibMediaCodecs/CodedFrame.h>@@ -156,16 +157,19 @@ DecoderErrorOr<NonnullRefPtr<VideoFrame>> FFmpegVideoDecoder::take_next_output(Ccase AV_PIX_FMT_YUVJ422P:case AV_PIX_FMT_YUVJ444P:case AV_PIX_FMT_GBRP:+ case AV_PIX_FMT_GRAY8:return 8;case AV_PIX_FMT_YUV420P10:case AV_PIX_FMT_YUV422P10:case AV_PIX_FMT_YUV444P10:case AV_PIX_FMT_GBRP10:+ case AV_PIX_FMT_GRAY10:return 10;case AV_PIX_FMT_YUV420P12:case AV_PIX_FMT_YUV422P12:case AV_PIX_FMT_YUV444P12:case AV_PIX_FMT_GBRP12:+ case AV_PIX_FMT_GRAY12:return 12;default:return {};@@ -180,6 +184,10 @@ DecoderErrorOr<NonnullRefPtr<VideoFrame>> FFmpegVideoDecoder::take_next_output(Ccase AV_PIX_FMT_YUV420P10:case AV_PIX_FMT_YUV420P12:case AV_PIX_FMT_YUVJ420P:+ // A picture with no chroma is given the smallest chroma planes there are.+ case AV_PIX_FMT_GRAY8:+ case AV_PIX_FMT_GRAY10:+ case AV_PIX_FMT_GRAY12:return Subsampling::yuv420();case AV_PIX_FMT_YUV422P:case AV_PIX_FMT_YUV422P10:@@ -223,7 +231,20 @@ DecoderErrorOr<void> FFmpegVideoDecoder::copy_pending_frame_into(Gfx::YUVData& yauto component_size = yuv_data.bit_depth() <= 8 ? 1 : 2;+ auto pixel_format = static_cast<AVPixelFormat>(m_frame->format);+ auto has_chroma = !first_is_one_of(pixel_format, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12);+for (u32 plane = 0; plane < 3; plane++) {+ if (plane > 0 && !has_chroma) {+ // The chroma planes of a picture without chroma are filled with the level that adds no color.+ auto neutral_chroma = 1u << (yuv_data.bit_depth() - 1);+ if (component_size == 1)+ buffers[plane].fill(static_cast<u8>(neutral_chroma));+ else+ buffers[plane].reinterpret<u16>().fill(static_cast<u16>(neutral_chroma));+ continue;+ }+VERIFY(m_frame->linesize[plane] != 0);if (m_frame->linesize[plane] < 0)return DecoderError::with_description(DecoderErrorCategory::NotImplemented, "Reversed scanlines are not supported"sv);diff --git a/Tests/LibMedia/TestAV1Decode.cpp b/Tests/LibMedia/TestAV1Decode.cppindex e649b22488c..3ed1a9167a6 100644--- a/Tests/LibMedia/TestAV1Decode.cpp+++ b/Tests/LibMedia/TestAV1Decode.cpp@@ -78,3 +78,37 @@ TEST_CASE(a_flush_forgets_the_high_dynamic_range_metadata_of_earlier_frames)key = MUST(frames.add(coded_frame(temporal_delimiter, 0), Media::DecodeIntent::Output));EXPECT(!frames.take(static_cast<i64>(key))->high_dynamic_range_metadata.content_light_level_information.has_value());}++static NonnullRefPtr<Media::VideoFrame> first_frame_of(StringView path)+{+ auto file = MUST(Core::File::open(path, Core::File::OpenMode::Read));+ auto stream = Media::IncrementallyPopulatedStream::create_from_buffer(MUST(file->read_until_eof()));+ auto demuxer = MUST(Media::create_demuxer(stream));+ auto track = MUST(demuxer->get_preferred_track_for_type(Media::TrackType::Video));+ VERIFY(track.has_value());+ MUST(demuxer->create_context_for_track(*track));++ auto first_frame = MUST(demuxer->get_next_sample_for_track(*track));+ auto decoder = MUST(Media::FFmpeg::FFmpegVideoDecoder::try_create(first_frame.codec_id(), first_frame.new_codec_configuration().value_or({})));+ MUST(decoder->receive_coded_data(first_frame, Media::DecodeIntent::Output));+ decoder->signal_end_of_stream();+ return MUST(decoder->take_next_output({}));+}++TEST_CASE(a_picture_without_chroma_is_given_planes_of_neutral_chroma)+{+ auto frame = first_frame_of("./av1_gray_8_bit.webm"sv);+ EXPECT_EQ(frame->bit_depth(), 8);+ EXPECT_EQ(frame->subsampling(), Media::Subsampling::yuv420());+ auto yuv_data = frame->yuv_data().release_value();+ EXPECT(all_of(yuv_data.u_data(), [](u8 sample) { return sample == 128; }));+ EXPECT(all_of(yuv_data.v_data(), [](u8 sample) { return sample == 128; }));+ EXPECT(any_of(yuv_data.y_data(), [&](u8 sample) { return sample != yuv_data.y_data()[0]; }));++ frame = first_frame_of("./av1_gray_12_bit.webm"sv);+ EXPECT_EQ(frame->bit_depth(), 12);+ EXPECT_EQ(frame->subsampling(), Media::Subsampling::yuv420());+ yuv_data = frame->yuv_data().release_value();+ EXPECT(all_of(yuv_data.u_data().reinterpret<u16 const>(), [](u16 sample) { return sample == 2048; }));+ EXPECT(all_of(yuv_data.v_data().reinterpret<u16 const>(), [](u16 sample) { return sample == 2048; }));+}diff --git a/Tests/LibMedia/av1_gray_12_bit.webm b/Tests/LibMedia/av1_gray_12_bit.webmnew file mode 100644index 00000000000..4fb256148e7Binary files /dev/null and b/Tests/LibMedia/av1_gray_12_bit.webm differdiff --git a/Tests/LibMedia/av1_gray_8_bit.webm b/Tests/LibMedia/av1_gray_8_bit.webmnew file mode 100644index 00000000000..8cd6782f0c0Binary files /dev/null and b/Tests/LibMedia/av1_gray_8_bit.webm differ