1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "media/base/data_buffer.h"
#include "media/base/pipeline.h"
#include "media/base/test_data_util.h"
#include "media/filters/ffmpeg_glue.h"
#include "media/video/ffmpeg_video_decode_engine.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::DoAll;
using ::testing::Return;
using ::testing::ReturnNull;
using ::testing::SaveArg;
using ::testing::SetArgumentPointee;
using ::testing::StrictMock;
namespace media {
static const VideoFrame::Format kVideoFormat = VideoFrame::YV12;
static const gfx::Size kCodedSize(320, 240);
static const gfx::Rect kVisibleRect(320, 240);
static const gfx::Size kNaturalSize(522, 288);
static const AVRational kFrameRate = { 100, 1 };
static const AVRational kAspectRatio = { 1, 1 };
ACTION_P2(DemuxComplete, engine, buffer) {
engine->ConsumeVideoSample(buffer);
}
class FFmpegVideoDecodeEngineTest
: public testing::Test,
public VideoDecodeEngine::EventHandler {
public:
FFmpegVideoDecodeEngineTest()
: config_(kCodecVP8, kVideoFormat, kCodedSize, kVisibleRect,
kFrameRate.num, kFrameRate.den,
kAspectRatio.num, kAspectRatio.den,
NULL, 0) {
CHECK(FFmpegGlue::GetInstance());
// Setup FFmpeg structures.
frame_buffer_.reset(new uint8[kCodedSize.GetArea()]);
test_engine_.reset(new FFmpegVideoDecodeEngine());
ReadTestDataFile("vp8-I-frame-320x240", &i_frame_buffer_);
ReadTestDataFile("vp8-corrupt-I-frame", &corrupt_i_frame_buffer_);
end_of_stream_buffer_ = new DataBuffer(0);
}
~FFmpegVideoDecodeEngineTest() {
test_engine_.reset();
}
void Initialize() {
EXPECT_CALL(*this, OnInitializeComplete(true));
test_engine_->Initialize(MessageLoop::current(), this, NULL, config_);
}
// Decodes the single compressed frame in |buffer| and writes the
// uncompressed output to |video_frame|. This method works with single
// and multithreaded decoders. End of stream buffers are used to trigger
// the frame to be returned in the multithreaded decoder case.
void DecodeASingleFrame(const scoped_refptr<Buffer>& buffer,
scoped_refptr<VideoFrame>* video_frame) {
EXPECT_CALL(*this, ProduceVideoSample(_))
.WillOnce(DemuxComplete(test_engine_.get(), buffer))
.WillRepeatedly(DemuxComplete(test_engine_.get(),
end_of_stream_buffer_));
EXPECT_CALL(*this, ConsumeVideoFrame(_, _))
.WillOnce(SaveArg<0>(video_frame));
CallProduceVideoFrame();
}
// Decodes |i_frame_buffer_| and then decodes the data contained in
// the file named |test_file_name|. This function expects both buffers
// to decode to frames that are the same size.
void DecodeIFrameThenTestFile(const std::string& test_file_name) {
Initialize();
scoped_refptr<VideoFrame> video_frame_a;
scoped_refptr<VideoFrame> video_frame_b;
scoped_refptr<Buffer> buffer;
ReadTestDataFile(test_file_name, &buffer);
EXPECT_CALL(*this, ProduceVideoSample(_))
.WillOnce(DemuxComplete(test_engine_.get(), i_frame_buffer_))
.WillOnce(DemuxComplete(test_engine_.get(), buffer))
.WillRepeatedly(DemuxComplete(test_engine_.get(),
end_of_stream_buffer_));
EXPECT_CALL(*this, ConsumeVideoFrame(_, _))
.WillOnce(SaveArg<0>(&video_frame_a))
.WillOnce(SaveArg<0>(&video_frame_b));
CallProduceVideoFrame();
CallProduceVideoFrame();
size_t expected_width = static_cast<size_t>(kVisibleRect.width());
size_t expected_height = static_cast<size_t>(kVisibleRect.height());
EXPECT_EQ(expected_width, video_frame_a->width());
EXPECT_EQ(expected_height, video_frame_a->height());
EXPECT_EQ(expected_width, video_frame_b->width());
EXPECT_EQ(expected_height, video_frame_b->height());
}
// VideoDecodeEngine::EventHandler implementation.
MOCK_METHOD2(ConsumeVideoFrame,
void(scoped_refptr<VideoFrame>, const PipelineStatistics&));
MOCK_METHOD1(ProduceVideoSample, void(scoped_refptr<Buffer>));
MOCK_METHOD1(OnInitializeComplete, void(bool));
MOCK_METHOD0(OnUninitializeComplete, void());
MOCK_METHOD0(OnFlushComplete, void());
MOCK_METHOD0(OnSeekComplete, void());
MOCK_METHOD0(OnError, void());
void CallProduceVideoFrame() {
test_engine_->ProduceVideoFrame(VideoFrame::CreateFrame(
VideoFrame::YV12, kVisibleRect.width(), kVisibleRect.height(),
kNoTimestamp, kNoTimestamp));
}
protected:
VideoDecoderConfig config_;
scoped_ptr<FFmpegVideoDecodeEngine> test_engine_;
scoped_array<uint8_t> frame_buffer_;
scoped_refptr<Buffer> i_frame_buffer_;
scoped_refptr<Buffer> corrupt_i_frame_buffer_;
scoped_refptr<Buffer> end_of_stream_buffer_;
private:
DISALLOW_COPY_AND_ASSIGN(FFmpegVideoDecodeEngineTest);
};
TEST_F(FFmpegVideoDecodeEngineTest, Initialize_Normal) {
Initialize();
}
TEST_F(FFmpegVideoDecodeEngineTest, Initialize_FindDecoderFails) {
VideoDecoderConfig config(kUnknownVideoCodec, kVideoFormat,
kCodedSize, kVisibleRect,
kFrameRate.num, kFrameRate.den,
kAspectRatio.num, kAspectRatio.den,
NULL, 0);
// Test avcodec_find_decoder() returning NULL.
EXPECT_CALL(*this, OnInitializeComplete(false));
test_engine_->Initialize(MessageLoop::current(), this, NULL, config);
}
TEST_F(FFmpegVideoDecodeEngineTest, Initialize_OpenDecoderFails) {
// Specify Theora w/o extra data so that avcodec_open() fails.
VideoDecoderConfig config(kCodecTheora, kVideoFormat,
kCodedSize, kVisibleRect,
kFrameRate.num, kFrameRate.den,
kAspectRatio.num, kAspectRatio.den,
NULL, 0);
EXPECT_CALL(*this, OnInitializeComplete(false));
test_engine_->Initialize(MessageLoop::current(), this, NULL, config);
}
TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_Normal) {
Initialize();
// Simulate decoding a single frame.
scoped_refptr<VideoFrame> video_frame;
DecodeASingleFrame(i_frame_buffer_, &video_frame);
// |video_frame| timestamp is 0 because we set the timestamp based off
// the buffer timestamp.
ASSERT_TRUE(video_frame);
EXPECT_EQ(0, video_frame->GetTimestamp().ToInternalValue());
EXPECT_EQ(10000, video_frame->GetDuration().ToInternalValue());
}
// Verify current behavior for 0 byte frames. FFmpeg simply ignores
// the 0 byte frames.
TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_0ByteFrame) {
Initialize();
scoped_refptr<DataBuffer> zero_byte_buffer = new DataBuffer(1);
scoped_refptr<VideoFrame> video_frame_a;
scoped_refptr<VideoFrame> video_frame_b;
scoped_refptr<VideoFrame> video_frame_c;
EXPECT_CALL(*this, ProduceVideoSample(_))
.WillOnce(DemuxComplete(test_engine_.get(), i_frame_buffer_))
.WillOnce(DemuxComplete(test_engine_.get(), zero_byte_buffer))
.WillOnce(DemuxComplete(test_engine_.get(), i_frame_buffer_))
.WillRepeatedly(DemuxComplete(test_engine_.get(),
end_of_stream_buffer_));
EXPECT_CALL(*this, ConsumeVideoFrame(_, _))
.WillOnce(SaveArg<0>(&video_frame_a))
.WillOnce(SaveArg<0>(&video_frame_b))
.WillOnce(SaveArg<0>(&video_frame_c));
CallProduceVideoFrame();
CallProduceVideoFrame();
CallProduceVideoFrame();
EXPECT_TRUE(video_frame_a);
EXPECT_TRUE(video_frame_b);
EXPECT_FALSE(video_frame_c);
}
TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_DecodeError) {
Initialize();
EXPECT_CALL(*this, ProduceVideoSample(_))
.WillOnce(DemuxComplete(test_engine_.get(), corrupt_i_frame_buffer_))
.WillRepeatedly(DemuxComplete(test_engine_.get(), i_frame_buffer_));
EXPECT_CALL(*this, OnError());
CallProduceVideoFrame();
}
// Multi-threaded decoders have different behavior than single-threaded
// decoders at the end of the stream. Multithreaded decoders hide errors
// that happen on the last |codec_context_->thread_count| frames to avoid
// prematurely signalling EOS. This test just exposes that behavior so we can
// detect if it changes.
TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_DecodeErrorAtEndOfStream) {
Initialize();
EXPECT_CALL(*this, ProduceVideoSample(_))
.WillOnce(DemuxComplete(test_engine_.get(), corrupt_i_frame_buffer_))
.WillRepeatedly(DemuxComplete(test_engine_.get(), end_of_stream_buffer_));
scoped_refptr<VideoFrame> video_frame;
EXPECT_CALL(*this, ConsumeVideoFrame(_, _))
.WillOnce(SaveArg<0>(&video_frame));
CallProduceVideoFrame();
EXPECT_FALSE(video_frame);
}
// Decode |i_frame_buffer_| and then a frame with a larger width and verify
// the output size didn't change.
// TODO(acolwell): Fix InvalidRead detected by Valgrind
//TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_LargerWidth) {
// DecodeIFrameThenTestFile("vp8-I-frame-640x240");
//}
// Decode |i_frame_buffer_| and then a frame with a smaller width and verify
// the output size didn't change.
TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_SmallerWidth) {
DecodeIFrameThenTestFile("vp8-I-frame-160x240");
}
// Decode |i_frame_buffer_| and then a frame with a larger height and verify
// the output size didn't change.
// TODO(acolwell): Fix InvalidRead detected by Valgrind
//TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_LargerHeight) {
// DecodeIFrameThenTestFile("vp8-I-frame-320x480");
//}
// Decode |i_frame_buffer_| and then a frame with a smaller height and verify
// the output size didn't change.
TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_SmallerHeight) {
DecodeIFrameThenTestFile("vp8-I-frame-320x120");
}
} // namespace media
|