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
276
277
278
|
// Copyright (c) 2010 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 <d3d9.h>
#include <dxva2api.h>
#include <mfapi.h>
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/scoped_ptr.h"
#include "base/string_util.h"
#include "media/base/video_frame.h"
#include "media/mf/d3d_util.h"
#include "media/mf/file_reader_util.h"
#include "media/mf/mft_h264_decoder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
static const int kDecoderMaxWidth = 1920;
static const int kDecoderMaxHeight = 1088;
class FakeMftReader {
public:
FakeMftReader() : frames_remaining_(20) {}
explicit FakeMftReader(int count) : frames_remaining_(count) {}
~FakeMftReader() {}
// Provides garbage input to the decoder.
void ReadCallback(uint8** buf, int* sz, int64* ts, int64* dur) {
if (frames_remaining_ > 0) {
*sz = 4096;
*buf = new uint8[*sz];
memset(*buf, 42, *sz);
*ts = 50000000 - frames_remaining_ * 10000;
*dur = 5000;
--frames_remaining_;
} else {
// Emulate end of stream on the last "frame".
*buf = NULL;
*sz = 0;
}
}
int frames_remaining() const { return frames_remaining_; }
private:
int frames_remaining_;
};
class FakeMftRenderer : public base::RefCountedThreadSafe<FakeMftRenderer> {
public:
explicit FakeMftRenderer(scoped_refptr<MftH264Decoder> decoder)
: decoder_(decoder),
count_(0) {
}
~FakeMftRenderer() {}
void WriteCallback(scoped_refptr<VideoFrame> frame) {
static_cast<IMFMediaBuffer*>(frame->private_buffer())->Release();
++count_;
MessageLoop::current()->PostTask(
FROM_HERE,
NewRunnableMethod(decoder_.get(), &MftH264Decoder::GetOutput));
}
void Start() {
MessageLoop::current()->PostTask(
FROM_HERE,
NewRunnableMethod(decoder_.get(), &MftH264Decoder::GetOutput));
}
int count() const { return count_; }
private:
scoped_refptr<MftH264Decoder> decoder_;
int count_;
};
class MftH264DecoderTest : public testing::Test {
public:
MftH264DecoderTest() {}
virtual ~MftH264DecoderTest() {}
protected:
virtual void SetUp() {}
virtual void TearDown() {}
};
// A simple test case for init/deinit of MF/COM libraries.
TEST_F(MftH264DecoderTest, SimpleInit) {
EXPECT_HRESULT_SUCCEEDED(
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE));
EXPECT_HRESULT_SUCCEEDED(MFStartup(MF_VERSION, MFSTARTUP_FULL));
EXPECT_HRESULT_SUCCEEDED(MFShutdown());
CoUninitialize();
}
TEST_F(MftH264DecoderTest, InitWithDxvaButNoD3DDevice) {
scoped_refptr<MftH264Decoder> decoder(new MftH264Decoder(true));
ASSERT_TRUE(decoder.get() != NULL);
FakeMftReader reader;
scoped_refptr<FakeMftRenderer> renderer(new FakeMftRenderer(decoder));
EXPECT_FALSE(
decoder->Init(NULL, 6, 7, 111, 222, 3, 1,
NewCallback(&reader, &FakeMftReader::ReadCallback),
NewCallback(renderer.get(),
&FakeMftRenderer::WriteCallback)));
}
TEST_F(MftH264DecoderTest, InitMissingCallbacks) {
scoped_refptr<MftH264Decoder> decoder(new MftH264Decoder(false));
ASSERT_TRUE(decoder.get() != NULL);
EXPECT_FALSE(decoder->Init(NULL, 1, 3, 111, 222, 56, 34, NULL, NULL));
}
TEST_F(MftH264DecoderTest, InitWithNegativeDimensions) {
scoped_refptr<MftH264Decoder> decoder(new MftH264Decoder(false));
ASSERT_TRUE(decoder.get() != NULL);
FakeMftReader reader;
scoped_refptr<FakeMftRenderer> renderer(new FakeMftRenderer(decoder));
EXPECT_TRUE(decoder->Init(NULL, 0, 6, -123, -456, 22, 4787,
NewCallback(&reader, &FakeMftReader::ReadCallback),
NewCallback(renderer.get(),
&FakeMftRenderer::WriteCallback)));
// By default, decoder should "guess" the dimensions to be the maximum.
EXPECT_EQ(kDecoderMaxWidth, decoder->width());
EXPECT_EQ(kDecoderMaxHeight, decoder->height());
}
TEST_F(MftH264DecoderTest, InitWithTooHighDimensions) {
scoped_refptr<MftH264Decoder> decoder(new MftH264Decoder(false));
ASSERT_TRUE(decoder.get() != NULL);
FakeMftReader reader;
scoped_refptr<FakeMftRenderer> renderer(new FakeMftRenderer(decoder));
EXPECT_TRUE(decoder->Init(NULL, 0, 0,
kDecoderMaxWidth + 1, kDecoderMaxHeight + 1,
0, 0,
NewCallback(&reader, &FakeMftReader::ReadCallback),
NewCallback(renderer.get(),
&FakeMftRenderer::WriteCallback)));
// Decoder should truncate the dimensions to the maximum supported.
EXPECT_EQ(kDecoderMaxWidth, decoder->width());
EXPECT_EQ(kDecoderMaxHeight, decoder->height());
}
TEST_F(MftH264DecoderTest, InitWithNormalDimensions) {
scoped_refptr<MftH264Decoder> decoder(new MftH264Decoder(false));
ASSERT_TRUE(decoder.get() != NULL);
FakeMftReader reader;
scoped_refptr<FakeMftRenderer> renderer(new FakeMftRenderer(decoder));
int width = 1024, height = 768;
EXPECT_TRUE(decoder->Init(NULL, 0, 0, width, height, 0, 0,
NewCallback(&reader, &FakeMftReader::ReadCallback),
NewCallback(renderer.get(),
&FakeMftRenderer::WriteCallback)));
EXPECT_EQ(width, decoder->width());
EXPECT_EQ(height, decoder->height());
}
// SendDrainMessage() is not a public method. Nonetheless it does not hurt
// to test that the decoder should not do things before it is initialized.
TEST_F(MftH264DecoderTest, SendDrainMessageBeforeInitDeathTest) {
scoped_refptr<MftH264Decoder> decoder(new MftH264Decoder(false));
ASSERT_TRUE(decoder.get() != NULL);
EXPECT_DEATH({ decoder->SendDrainMessage(); }, ".*initialized_.*");
}
// Tests draining after init, but before any input is sent.
TEST_F(MftH264DecoderTest, SendDrainMessageAtInit) {
scoped_refptr<MftH264Decoder> decoder(new MftH264Decoder(false));
ASSERT_TRUE(decoder.get() != NULL);
FakeMftReader reader;
scoped_refptr<FakeMftRenderer> renderer(new FakeMftRenderer(decoder));
ASSERT_TRUE(decoder->Init(NULL, 0, 0, 111, 222, 0, 0,
NewCallback(&reader, &FakeMftReader::ReadCallback),
NewCallback(renderer.get(),
&FakeMftRenderer::WriteCallback)));
EXPECT_TRUE(decoder->SendDrainMessage());
EXPECT_TRUE(decoder->drain_message_sent_);
}
TEST_F(MftH264DecoderTest, DrainOnEndOfInputStream) {
MessageLoop loop;
scoped_refptr<MftH264Decoder> decoder(new MftH264Decoder(false));
ASSERT_TRUE(decoder.get() != NULL);
// No frames, outputs a NULL indicating end-of-stream
FakeMftReader reader(0);
scoped_refptr<FakeMftRenderer> renderer(new FakeMftRenderer(decoder));
ASSERT_TRUE(decoder->Init(NULL, 0, 0, 111, 222, 0, 0,
NewCallback(&reader, &FakeMftReader::ReadCallback),
NewCallback(renderer.get(),
&FakeMftRenderer::WriteCallback)));
MessageLoop::current()->PostTask(
FROM_HERE,
NewRunnableMethod(renderer.get(), &FakeMftRenderer::Start));
MessageLoop::current()->Run();
EXPECT_TRUE(decoder->drain_message_sent());
}
// 100 input garbage samples should be enough to test whether the decoder
// will output decoded garbage frames.
TEST_F(MftH264DecoderTest, NoOutputOnGarbageInput) {
MessageLoop loop;
scoped_refptr<MftH264Decoder> decoder(new MftH264Decoder(false));
ASSERT_TRUE(decoder.get() != NULL);
int num_frames = 100;
FakeMftReader reader(num_frames);
scoped_refptr<FakeMftRenderer> renderer(new FakeMftRenderer(decoder));
ASSERT_TRUE(decoder->Init(NULL, 0, 0, 111, 222, 0, 0,
NewCallback(&reader, &FakeMftReader::ReadCallback),
NewCallback(renderer.get(),
&FakeMftRenderer::WriteCallback)));
MessageLoop::current()->PostTask(
FROM_HERE, NewRunnableMethod(renderer.get(), &FakeMftRenderer::Start));
MessageLoop::current()->Run();
// Decoder should accept corrupt input data and silently ignore it.
EXPECT_EQ(num_frames, decoder->frames_read());
// Decoder should not have output anything if input is corrupt.
EXPECT_EQ(0, decoder->frames_decoded());
EXPECT_EQ(0, renderer->count());
}
FilePath GetBearVideoFilePath(const std::string& file_name) {
FilePath path;
PathService::Get(base::DIR_SOURCE_ROOT, &path);
path = path.AppendASCII("media")
.AppendASCII("test")
.AppendASCII("data")
.AppendASCII(file_name.c_str());
return path;
}
// Decodes media/test/data/bear.1280x720.mp4 which is expected to be a valid
// H.264 video.
TEST_F(MftH264DecoderTest, DecodeValidVideoDxva) {
MessageLoop loop;
FilePath path = GetBearVideoFilePath("bear.1280x720.mp4");
ASSERT_TRUE(file_util::PathExists(path));
ScopedComPtr<IDirect3D9> d3d9;
ScopedComPtr<IDirect3DDevice9> device;
ScopedComPtr<IDirect3DDeviceManager9> dev_manager;
dev_manager.Attach(CreateD3DDevManager(GetDesktopWindow(),
d3d9.Receive(),
device.Receive()));
ASSERT_TRUE(dev_manager.get() != NULL);
scoped_refptr<MftH264Decoder> decoder(new MftH264Decoder(true));
ASSERT_TRUE(decoder.get() != NULL);
FFmpegFileReader reader(WideToASCII(path.value()));
ASSERT_TRUE(reader.Initialize());
scoped_refptr<FakeMftRenderer> renderer(new FakeMftRenderer(decoder));
ASSERT_TRUE(decoder->Init(dev_manager.get(), 0, 0, 111, 222, 0, 0,
NewCallback(&reader, &FFmpegFileReader::Read2),
NewCallback(renderer.get(),
&FakeMftRenderer::WriteCallback)));
MessageLoop::current()->PostTask(
FROM_HERE,
NewRunnableMethod(renderer.get(), &FakeMftRenderer::Start));
MessageLoop::current()->Run();
// If the video is valid, then it should output frames. However, for some
// videos, the number of frames decoded is one-off.
EXPECT_EQ(82, decoder->frames_read());
EXPECT_LE(decoder->frames_read() - decoder->frames_decoded(), 1);
}
} // namespace media
|