forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_encode_accelerator_adapter_test.cc
317 lines (277 loc) · 12.1 KB
/
video_encode_accelerator_adapter_test.cc
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2020 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 "media/video/video_encode_accelerator_adapter.h"
#include <memory>
#include <string>
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "media/base/video_frame.h"
#include "media/base/video_util.h"
#include "media/video/fake_video_encode_accelerator.h"
#include "media/video/gpu_video_accelerator_factories.h"
#include "media/video/mock_gpu_video_accelerator_factories.h"
#include "media/video/mock_video_encode_accelerator.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libyuv/include/libyuv.h"
using ::testing::_;
using ::testing::AtLeast;
using ::testing::Field;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::Values;
using ::testing::WithArgs;
namespace media {
class VideoEncodeAcceleratorAdapterTest
: public ::testing::TestWithParam<VideoPixelFormat> {
public:
VideoEncodeAcceleratorAdapterTest() = default;
void SetUp() override {
vea_runner_ = base::ThreadPool::CreateSequencedTaskRunner({});
vea_ = new FakeVideoEncodeAccelerator(vea_runner_);
gpu_factories_ =
std::make_unique<MockGpuVideoAcceleratorFactories>(nullptr);
EXPECT_CALL(*gpu_factories_.get(), DoCreateVideoEncodeAccelerator())
.WillRepeatedly(Return(vea_));
EXPECT_CALL(*gpu_factories_.get(), GetTaskRunner())
.WillRepeatedly(Return(vea_runner_));
callback_runner_ = base::SequencedTaskRunnerHandle::Get();
vae_adapter_ = std::make_unique<VideoEncodeAcceleratorAdapter>(
gpu_factories_.get(), callback_runner_);
}
void TearDown() override {
vea_runner_->DeleteSoon(FROM_HERE, std::move(vae_adapter_));
RunUntilIdle();
}
void RunUntilIdle() { task_environment_.RunUntilIdle(); }
VideoEncodeAcceleratorAdapter* adapter() { return vae_adapter_.get(); }
FakeVideoEncodeAccelerator* vea() { return vea_; }
scoped_refptr<VideoFrame> CreateGreenGpuFrame(gfx::Size size,
base::TimeDelta timestamp) {
auto gmb = gpu_factories_->CreateGpuMemoryBuffer(
size, gfx::BufferFormat::YUV_420_BIPLANAR,
gfx::BufferUsage::SCANOUT_VEA_READ_CAMERA_AND_CPU_READ_WRITE);
if (!gmb || !gmb->Map())
return nullptr;
// Green NV12 frame (Y:0x96, U:0x40, V:0x40)
const auto gmb_size = gmb->GetSize();
memset(static_cast<uint8_t*>(gmb->memory(0)), 0x96,
gmb->stride(0) * gmb_size.height());
memset(static_cast<uint8_t*>(gmb->memory(1)), 0x28,
gmb->stride(1) * gmb_size.height() / 2);
gmb->Unmap();
gpu::MailboxHolder empty_mailboxes[media::VideoFrame::kMaxPlanes];
return VideoFrame::WrapExternalGpuMemoryBuffer(
gfx::Rect(gmb_size), size, std::move(gmb), empty_mailboxes,
base::NullCallback(), timestamp);
}
scoped_refptr<VideoFrame> CreateGreenCpuFrame(gfx::Size size,
base::TimeDelta timestamp) {
auto frame = VideoFrame::CreateFrame(PIXEL_FORMAT_I420, size,
gfx::Rect(size), size, timestamp);
// Green I420 frame (Y:0x96, U:0x40, V:0x40)
libyuv::I420Rect(
frame->data(VideoFrame::kYPlane), frame->stride(VideoFrame::kYPlane),
frame->data(VideoFrame::kUPlane), frame->stride(VideoFrame::kUPlane),
frame->data(VideoFrame::kVPlane), frame->stride(VideoFrame::kVPlane),
0, // left
0, // top
frame->visible_rect().width(), // right
frame->visible_rect().height(), // bottom
0x96, // Y color
0x40, // U color
0x40); // V color
return frame;
}
scoped_refptr<VideoFrame> CreateGreenFrame(gfx::Size size,
VideoPixelFormat format,
base::TimeDelta timestamp) {
switch (format) {
case PIXEL_FORMAT_I420:
return CreateGreenCpuFrame(size, timestamp);
case PIXEL_FORMAT_NV12:
return CreateGreenGpuFrame(size, timestamp);
default:
EXPECT_TRUE(false) << "not supported pixel format";
return nullptr;
}
}
VideoEncoder::StatusCB ValidatingStatusCB(base::Location loc = FROM_HERE) {
struct CallEnforcer {
bool called = false;
std::string location;
~CallEnforcer() {
EXPECT_TRUE(called) << "Callback created: " << location;
}
};
auto enforcer = std::make_unique<CallEnforcer>();
enforcer->location = loc.ToString();
return base::BindLambdaForTesting(
[this, enforcer{std::move(enforcer)}](Status s) {
EXPECT_TRUE(callback_runner_->RunsTasksInCurrentSequence());
EXPECT_TRUE(s.is_ok()) << " Callback created: " << enforcer->location
<< " Error: " << s.message();
enforcer->called = true;
});
}
protected:
VideoCodecProfile profile_ = VP8PROFILE_ANY;
base::test::TaskEnvironment task_environment_;
FakeVideoEncodeAccelerator* vea_; // owned by |vae_adapter_|
std::unique_ptr<MockGpuVideoAcceleratorFactories> gpu_factories_;
std::unique_ptr<VideoEncodeAcceleratorAdapter> vae_adapter_;
scoped_refptr<base::SequencedTaskRunner> vea_runner_;
scoped_refptr<base::SequencedTaskRunner> callback_runner_;
};
TEST_F(VideoEncodeAcceleratorAdapterTest, PreInitialize) {
VideoEncoder::Options options;
options.frame_size = gfx::Size(640, 480);
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput, base::Optional<VideoEncoder::CodecDescription>) {
});
adapter()->Initialize(profile_, options, std::move(output_cb),
ValidatingStatusCB());
RunUntilIdle();
}
TEST_F(VideoEncodeAcceleratorAdapterTest, InitializeAfterFirstFrame) {
VideoEncoder::Options options;
options.frame_size = gfx::Size(640, 480);
int outputs_count = 0;
auto pixel_format = PIXEL_FORMAT_I420;
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput, base::Optional<VideoEncoder::CodecDescription>) {
outputs_count++;
});
vea()->SetEncodingCallback(base::BindLambdaForTesting(
[&](BitstreamBuffer&, bool keyframe, scoped_refptr<VideoFrame> frame) {
EXPECT_EQ(keyframe, true);
EXPECT_EQ(frame->format(), pixel_format);
EXPECT_EQ(frame->coded_size(), options.frame_size);
return BitstreamBufferMetadata(1, keyframe, frame->timestamp());
}));
adapter()->Initialize(profile_, options, std::move(output_cb),
ValidatingStatusCB());
auto frame = CreateGreenFrame(options.frame_size, pixel_format,
base::TimeDelta::FromMilliseconds(1));
adapter()->Encode(frame, true, ValidatingStatusCB());
RunUntilIdle();
EXPECT_EQ(outputs_count, 1);
}
TEST_F(VideoEncodeAcceleratorAdapterTest, InitializationError) {
VideoEncoder::Options options;
options.frame_size = gfx::Size(640, 480);
int outputs_count = 0;
auto pixel_format = PIXEL_FORMAT_I420;
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput, base::Optional<VideoEncoder::CodecDescription>) {
outputs_count++;
});
VideoEncoder::StatusCB expect_error_done_cb =
base::BindLambdaForTesting([&](Status s) { EXPECT_FALSE(s.is_ok()); });
vea()->SetEncodingCallback(base::BindLambdaForTesting(
[&](BitstreamBuffer&, bool keyframe, scoped_refptr<VideoFrame> frame) {
EXPECT_TRUE(false) << "should never come here";
return BitstreamBufferMetadata(1, keyframe, frame->timestamp());
}));
adapter()->Initialize(VIDEO_CODEC_PROFILE_UNKNOWN, options,
std::move(output_cb), ValidatingStatusCB());
auto frame = CreateGreenFrame(options.frame_size, pixel_format,
base::TimeDelta::FromMilliseconds(1));
adapter()->Encode(frame, true, std::move(expect_error_done_cb));
RunUntilIdle();
EXPECT_EQ(outputs_count, 0);
}
TEST_P(VideoEncodeAcceleratorAdapterTest, TwoFramesResize) {
VideoEncoder::Options options;
options.frame_size = gfx::Size(640, 480);
int outputs_count = 0;
gfx::Size small_size(480, 320);
gfx::Size large_size(800, 600);
auto pixel_format = GetParam();
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput, base::Optional<VideoEncoder::CodecDescription>) {
outputs_count++;
});
vea()->SetEncodingCallback(base::BindLambdaForTesting(
[&](BitstreamBuffer&, bool keyframe, scoped_refptr<VideoFrame> frame) {
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
EXPECT_EQ(frame->format(), pixel_format);
#else
// Everywhere except on Linux resize switches frame into CPU mode.
EXPECT_EQ(frame->format(), PIXEL_FORMAT_I420);
#endif
EXPECT_EQ(frame->coded_size(), options.frame_size);
return BitstreamBufferMetadata(1, keyframe, frame->timestamp());
}));
adapter()->Initialize(profile_, options, std::move(output_cb),
ValidatingStatusCB());
auto small_frame = CreateGreenFrame(small_size, pixel_format,
base::TimeDelta::FromMilliseconds(1));
auto large_frame = CreateGreenFrame(large_size, pixel_format,
base::TimeDelta::FromMilliseconds(2));
adapter()->Encode(small_frame, true, ValidatingStatusCB());
adapter()->Encode(large_frame, false, ValidatingStatusCB());
RunUntilIdle();
EXPECT_EQ(outputs_count, 2);
}
TEST_P(VideoEncodeAcceleratorAdapterTest, RunWithAllPossibleInputConversions) {
VideoEncoder::Options options;
options.frame_size = gfx::Size(640, 480);
int outputs_count = 0;
gfx::Size small_size(480, 320);
gfx::Size same_size = options.frame_size;
gfx::Size large_size(800, 600);
int frames_to_encode = 33;
auto pixel_format = GetParam();
auto input_kind =
(pixel_format == PIXEL_FORMAT_NV12)
? VideoEncodeAcceleratorAdapter::InputBufferKind::GpuMemBuf
: VideoEncodeAcceleratorAdapter::InputBufferKind::CpuMemBuf;
adapter()->SetInputBufferPreferenceForTesting(input_kind);
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput, base::Optional<VideoEncoder::CodecDescription>) {
outputs_count++;
});
vea()->SetEncodingCallback(base::BindLambdaForTesting(
[&](BitstreamBuffer&, bool keyframe, scoped_refptr<VideoFrame> frame) {
EXPECT_EQ(frame->format(), pixel_format);
EXPECT_EQ(frame->coded_size(), options.frame_size);
return BitstreamBufferMetadata(1, keyframe, frame->timestamp());
}));
adapter()->Initialize(profile_, options, std::move(output_cb),
ValidatingStatusCB());
for (int frame_index = 0; frame_index < frames_to_encode; frame_index++) {
gfx::Size size;
if (frame_index % 4 == 0)
size = large_size;
else if (frame_index % 4 == 1)
size = small_size;
else
size = same_size;
auto create_func =
(frame_index & 4)
? &VideoEncodeAcceleratorAdapterTest::CreateGreenGpuFrame
: &VideoEncodeAcceleratorAdapterTest::CreateGreenCpuFrame;
bool key = frame_index % 9 == 0;
auto frame = (this->*create_func)(
size, base::TimeDelta::FromMilliseconds(frame_index));
adapter()->Encode(frame, key, ValidatingStatusCB());
}
RunUntilIdle();
EXPECT_EQ(outputs_count, frames_to_encode);
}
INSTANTIATE_TEST_SUITE_P(VideoEncodeAcceleratorAdapterTest,
VideoEncodeAcceleratorAdapterTest,
::testing::Values(PIXEL_FORMAT_I420,
PIXEL_FORMAT_NV12));
} // namespace media