1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "base/functional/bind.h"
11 #include "base/functional/callback_helpers.h"
12 #include "base/hash/md5.h"
13 #include "base/run_loop.h"
14 #include "base/test/task_environment.h"
15 #include "build/build_config.h"
16 #include "media/base/decoder_buffer.h"
17 #include "media/base/limits.h"
18 #include "media/base/media_util.h"
19 #include "media/base/test_data_util.h"
20 #include "media/base/test_helpers.h"
21 #include "media/base/video_frame.h"
22 #include "media/ffmpeg/ffmpeg_common.h"
23 #include "media/filters/dav1d_video_decoder.h"
24 #include "media/filters/in_memory_url_protocol.h"
25 #include "testing/gmock/include/gmock/gmock.h"
33 MATCHER(ContainsDecoderErrorLog, "") {
34 return CONTAINS_STRING(arg, "dav1d_send_data() failed");
39 class Dav1dVideoDecoderTest : public testing::Test {
41 Dav1dVideoDecoderTest()
42 : decoder_(std::make_unique<Dav1dVideoDecoder>(
43 std::make_unique<NullMediaLog>())),
44 i_frame_buffer_(ReadTestDataFile("av1-I-frame-320x240")) {}
46 Dav1dVideoDecoderTest(const Dav1dVideoDecoderTest&) = delete;
47 Dav1dVideoDecoderTest& operator=(const Dav1dVideoDecoderTest&) = delete;
49 ~Dav1dVideoDecoderTest() override { Destroy(); }
52 InitializeWithConfig(TestVideoConfig::Normal(VideoCodec::kAV1));
55 void InitializeWithConfigWithResult(const VideoDecoderConfig& config,
58 config, true, // Use low delay so we get 1 frame out for each frame in.
61 [](bool success, DecoderStatus status) {
62 EXPECT_EQ(status.is_ok(), success);
65 base::BindRepeating(&Dav1dVideoDecoderTest::FrameReady,
66 base::Unretained(this)),
67 base::NullCallback());
68 base::RunLoop().RunUntilIdle();
71 void InitializeWithConfig(const VideoDecoderConfig& config) {
72 InitializeWithConfigWithResult(config, true);
76 InitializeWithConfig(TestVideoConfig::Large(VideoCodec::kAV1));
80 decoder_->Reset(NewExpectedClosure());
81 base::RunLoop().RunUntilIdle();
86 base::RunLoop().RunUntilIdle();
89 // Sets up expectations and actions to put Dav1dVideoDecoder in an active
91 void ExpectDecodingState() {
92 EXPECT_TRUE(DecodeSingleFrame(i_frame_buffer_).is_ok());
93 ASSERT_EQ(1U, output_frames_.size());
96 // Sets up expectations and actions to put Dav1dVideoDecoder in an end
98 void ExpectEndOfStreamState() {
99 EXPECT_TRUE(DecodeSingleFrame(DecoderBuffer::CreateEOSBuffer()).is_ok());
100 ASSERT_FALSE(output_frames_.empty());
103 using InputBuffers = std::vector<scoped_refptr<DecoderBuffer>>;
104 using OutputFrames = std::vector<scoped_refptr<VideoFrame>>;
106 // Decodes all buffers in |input_buffers| and push all successfully decoded
107 // output frames into |output_frames|. Returns the last decode status returned
109 DecoderStatus DecodeMultipleFrames(const InputBuffers& input_buffers) {
110 for (auto iter = input_buffers.begin(); iter != input_buffers.end();
112 DecoderStatus status = Decode(*iter);
113 switch (status.code()) {
114 case DecoderStatus::Codes::kOk:
116 case DecoderStatus::Codes::kAborted:
120 DCHECK(output_frames_.empty());
124 return DecoderStatus::Codes::kOk;
127 // Decodes the single compressed frame in |buffer|.
128 DecoderStatus DecodeSingleFrame(scoped_refptr<DecoderBuffer> buffer) {
129 InputBuffers input_buffers;
130 input_buffers.push_back(std::move(buffer));
131 return DecodeMultipleFrames(input_buffers);
134 // Decodes |i_frame_buffer_| and then decodes the data contained in the file
135 // named |test_file_name|. This function expects both buffers to decode to
136 // frames that are the same size.
137 void DecodeIFrameThenTestFile(const std::string& test_file_name,
138 const gfx::Size& expected_size) {
140 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(test_file_name);
142 InputBuffers input_buffers;
143 input_buffers.push_back(i_frame_buffer_);
144 input_buffers.push_back(buffer);
145 input_buffers.push_back(DecoderBuffer::CreateEOSBuffer());
147 DecoderStatus status = DecodeMultipleFrames(input_buffers);
149 EXPECT_TRUE(status.is_ok());
150 ASSERT_EQ(2U, output_frames_.size());
152 gfx::Size original_size = TestVideoConfig::NormalCodedSize();
153 EXPECT_EQ(original_size.width(),
154 output_frames_[0]->visible_rect().size().width());
155 EXPECT_EQ(original_size.height(),
156 output_frames_[0]->visible_rect().size().height());
157 EXPECT_EQ(expected_size.width(),
158 output_frames_[1]->visible_rect().size().width());
159 EXPECT_EQ(expected_size.height(),
160 output_frames_[1]->visible_rect().size().height());
163 DecoderStatus Decode(scoped_refptr<DecoderBuffer> buffer) {
164 DecoderStatus status;
165 EXPECT_CALL(*this, DecodeDone(_)).WillOnce(testing::SaveArg<0>(&status));
167 decoder_->Decode(std::move(buffer),
168 base::BindOnce(&Dav1dVideoDecoderTest::DecodeDone,
169 base::Unretained(this)));
170 base::RunLoop().RunUntilIdle();
175 void FrameReady(scoped_refptr<VideoFrame> frame) {
176 DCHECK(!frame->metadata().end_of_stream);
177 output_frames_.push_back(std::move(frame));
180 std::string GetVideoFrameHash(const VideoFrame& frame) {
181 base::MD5Context md5_context;
182 base::MD5Init(&md5_context);
183 VideoFrame::HashFrameForTesting(&md5_context, frame);
184 base::MD5Digest digest;
185 base::MD5Final(&digest, &md5_context);
186 return base::MD5DigestToBase16(digest);
189 MOCK_METHOD1(DecodeDone, void(DecoderStatus));
191 base::test::SingleThreadTaskEnvironment task_environment_;
192 std::unique_ptr<Dav1dVideoDecoder> decoder_;
194 scoped_refptr<DecoderBuffer> i_frame_buffer_;
195 OutputFrames output_frames_;
198 TEST_F(Dav1dVideoDecoderTest, Initialize_Normal) {
202 TEST_F(Dav1dVideoDecoderTest, Reinitialize_Normal) {
207 TEST_F(Dav1dVideoDecoderTest, Reinitialize_AfterDecodeFrame) {
209 ExpectDecodingState();
213 TEST_F(Dav1dVideoDecoderTest, Reinitialize_AfterReset) {
215 ExpectDecodingState();
220 TEST_F(Dav1dVideoDecoderTest, DecodeFrame_Normal) {
223 // Simulate decoding a single frame.
224 EXPECT_TRUE(DecodeSingleFrame(i_frame_buffer_).is_ok());
225 ASSERT_EQ(1U, output_frames_.size());
227 const auto& frame = output_frames_.front();
228 EXPECT_EQ(PIXEL_FORMAT_I420, frame->format());
229 EXPECT_EQ("589dc641b7742ffe7a2b0d4c16aa3e86", GetVideoFrameHash(*frame));
232 TEST_F(Dav1dVideoDecoderTest, DecodeFrame_8bitMono) {
235 DecodeSingleFrame(ReadTestDataFile("av1-monochrome-I-frame-320x240-8bpp"))
237 ASSERT_EQ(1U, output_frames_.size());
239 const auto& frame = output_frames_.front();
240 EXPECT_EQ(PIXEL_FORMAT_I420, frame->format());
241 EXPECT_EQ(frame->data(VideoFrame::kUPlane), frame->data(VideoFrame::kVPlane));
242 EXPECT_EQ("eeba03dcc9c22c4632bf74b481db36b2", GetVideoFrameHash(*frame));
245 TEST_F(Dav1dVideoDecoderTest, DecodeFrame_10bitMono) {
247 EXPECT_TRUE(DecodeSingleFrame(
248 ReadTestDataFile("av1-monochrome-I-frame-320x240-10bpp"))
250 ASSERT_EQ(1U, output_frames_.size());
252 const auto& frame = output_frames_.front();
253 EXPECT_EQ(PIXEL_FORMAT_YUV420P10, frame->format());
254 EXPECT_EQ(frame->data(VideoFrame::kUPlane), frame->data(VideoFrame::kVPlane));
255 EXPECT_EQ("026c1fed9e161f09d816ac7278458a80", GetVideoFrameHash(*frame));
258 TEST_F(Dav1dVideoDecoderTest, DecodeFrame_12bitMono) {
260 EXPECT_TRUE(DecodeSingleFrame(
261 ReadTestDataFile("av1-monochrome-I-frame-320x240-12bpp"))
263 ASSERT_EQ(1U, output_frames_.size());
265 const auto& frame = output_frames_.front();
266 EXPECT_EQ(PIXEL_FORMAT_YUV420P12, frame->format());
267 EXPECT_EQ(frame->data(VideoFrame::kUPlane), frame->data(VideoFrame::kVPlane));
268 EXPECT_EQ("32115092dc00fbe86823b0b714a0f63e", GetVideoFrameHash(*frame));
271 // Decode |i_frame_buffer_| and then a frame with a larger width and verify
272 // the output size was adjusted.
273 TEST_F(Dav1dVideoDecoderTest, DecodeFrame_LargerWidth) {
274 DecodeIFrameThenTestFile("av1-I-frame-1280x720", gfx::Size(1280, 720));
277 // Decode a VP9 frame which should trigger a decoder error.
278 TEST_F(Dav1dVideoDecoderTest, DecodeFrame_Error) {
281 DecodeSingleFrame(ReadTestDataFile("vp9-I-frame-320x240")).is_ok());
284 // Test resetting when decoder has initialized but not decoded.
285 TEST_F(Dav1dVideoDecoderTest, Reset_Initialized) {
290 // Test resetting when decoder has decoded single frame.
291 TEST_F(Dav1dVideoDecoderTest, Reset_Decoding) {
293 ExpectDecodingState();
297 // Test resetting when decoder has hit end of stream.
298 TEST_F(Dav1dVideoDecoderTest, Reset_EndOfStream) {
300 ExpectDecodingState();
301 ExpectEndOfStreamState();
305 // Test destruction when decoder has initialized but not decoded.
306 TEST_F(Dav1dVideoDecoderTest, Destroy_Initialized) {
311 // Test destruction when decoder has decoded single frame.
312 TEST_F(Dav1dVideoDecoderTest, Destroy_Decoding) {
314 ExpectDecodingState();
318 // Test destruction when decoder has hit end of stream.
319 TEST_F(Dav1dVideoDecoderTest, Destroy_EndOfStream) {
321 ExpectDecodingState();
322 ExpectEndOfStreamState();
326 TEST_F(Dav1dVideoDecoderTest, FrameValidAfterPoolDestruction) {
328 Decode(i_frame_buffer_);
331 ASSERT_FALSE(output_frames_.empty());
333 // Write to the Y plane. The memory tools should detect a
334 // use-after-free if the storage was actually removed by pool destruction.
335 memset(output_frames_.front()->writable_data(VideoFrame::kYPlane), 0xff,
336 output_frames_.front()->rows(VideoFrame::kYPlane) *
337 output_frames_.front()->stride(VideoFrame::kYPlane));