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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
media / gpu / android / ndk_video_encode_accelerator_tests.cc [blame]
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/gpu/android/ndk_video_encode_accelerator.h"
#include <map>
#include <optional>
#include <vector>
#include "base/android/build_info.h"
#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/ranges/algorithm.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "media/base/bitstream_buffer.h"
#include "media/base/media_switches.h"
#include "media/base/media_util.h"
#include "media/base/test_helpers.h"
#include "media/base/video_codecs.h"
#include "media/base/video_frame.h"
#include "media/base/video_frame_converter.h"
#include "media/base/video_util.h"
#include "media/parsers/h264_parser.h"
#include "media/parsers/vp9_parser.h"
#include "media/video/fake_gpu_memory_buffer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libyuv/include/libyuv.h"
#include "third_party/libyuv/include/libyuv/convert_from.h"
#pragma clang attribute push DEFAULT_REQUIRES_ANDROID_API( \
NDK_MEDIA_CODEC_MIN_API)
using testing::Return;
namespace media {
struct VideoParams {
VideoCodecProfile profile;
VideoPixelFormat pixel_format;
};
class NdkVideoEncoderAcceleratorTest
: public ::testing::TestWithParam<VideoParams>,
public VideoEncodeAccelerator::Client {
public:
void SetUp() override {
if (__builtin_available(android NDK_MEDIA_CODEC_MIN_API, *)) {
// Negation results in compiler warning.
} else {
GTEST_SKIP() << "Not supported Android version";
}
#if BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)
feature_list_.InitAndEnableFeature(kPlatformHEVCEncoderSupport);
#endif
auto args = GetParam();
profile_ = args.profile;
codec_ = VideoCodecProfileToVideoCodec(profile_);
pixel_format_ = args.pixel_format;
auto profiles = MakeNdkAccelerator()->GetSupportedProfiles();
bool codec_supported = base::Contains(
profiles, profile_, &VideoEncodeAccelerator::SupportedProfile::profile);
if (!codec_supported) {
GTEST_SKIP() << "Device doesn't have hw encoder for: "
<< GetProfileName(profile_);
}
}
void TearDown() override {}
// Implementation for VEA::Client
void RequireBitstreamBuffers(unsigned int input_count,
const gfx::Size& input_coded_size,
size_t output_buffer_size) override {
output_buffer_size_ = output_buffer_size;
input_buffer_size_ =
VideoFrame::AllocationSize(PIXEL_FORMAT_I420, input_coded_size);
SendNewBuffer();
if (!OnRequireBuffer())
loop_.Quit();
}
void BitstreamBufferReady(int32_t bitstream_buffer_id,
const BitstreamBufferMetadata& metadata) override {
outputs_.push_back({bitstream_buffer_id, metadata});
SendNewBuffer();
if (!OnBufferReady())
loop_.Quit();
}
void NotifyErrorStatus(const EncoderStatus& status) override {
CHECK(!status.is_ok());
error_status_ = status;
if (!OnError())
loop_.Quit();
}
MOCK_METHOD(bool, OnRequireBuffer, ());
MOCK_METHOD(bool, OnBufferReady, ());
MOCK_METHOD(bool, OnError, ());
protected:
void SendNewBuffer() {
auto buffer = output_pool_->MaybeAllocateBuffer(output_buffer_size_);
if (!buffer) {
FAIL() << "Can't allocate memory buffer";
}
const base::UnsafeSharedMemoryRegion& region = buffer->GetRegion();
auto mapping = region.Map();
memset(mapping.memory(), 0, mapping.size());
auto id = ++last_buffer_id_;
accelerator_->UseOutputBitstreamBuffer(
BitstreamBuffer(id, region.Duplicate(), region.GetSize()));
id_to_buffer_[id] = std::move(buffer);
}
scoped_refptr<VideoFrame> CreateI420Frame(gfx::Size size,
uint32_t color,
base::TimeDelta timestamp) {
auto frame = VideoFrame::CreateFrame(PIXEL_FORMAT_I420, size,
gfx::Rect(size), size, timestamp);
auto y = color & 0xFF;
auto u = (color >> 8) & 0xFF;
auto v = (color >> 16) & 0xFF;
libyuv::I420Rect(frame->writable_data(VideoFrame::Plane::kY),
frame->stride(VideoFrame::Plane::kY),
frame->writable_data(VideoFrame::Plane::kU),
frame->stride(VideoFrame::Plane::kU),
frame->writable_data(VideoFrame::Plane::kV),
frame->stride(VideoFrame::Plane::kV),
0, // left
0, // top
frame->visible_rect().width(), // right
frame->visible_rect().height(), // bottom
y, // Y color
u, // U color
v); // V color
return frame;
}
scoped_refptr<VideoFrame> CreateNV12Frame(gfx::Size size,
uint32_t color,
base::TimeDelta timestamp) {
auto i420_frame = CreateI420Frame(size, color, timestamp);
auto nv12_frame = VideoFrame::CreateFrame(PIXEL_FORMAT_NV12, size,
gfx::Rect(size), size, timestamp);
auto status = frame_converter_.ConvertAndScale(*i420_frame, *nv12_frame);
EXPECT_TRUE(status.is_ok());
return nv12_frame;
}
scoped_refptr<VideoFrame> CreateRGBFrame(gfx::Size size,
uint32_t color,
base::TimeDelta timestamp) {
auto frame = VideoFrame::CreateFrame(PIXEL_FORMAT_XRGB, size,
gfx::Rect(size), size, timestamp);
libyuv::ARGBRect(frame->writable_data(VideoFrame::Plane::kARGB),
frame->stride(VideoFrame::Plane::kARGB),
0, // left
0, // top
frame->visible_rect().width(), // right
frame->visible_rect().height(), // bottom
color);
return frame;
}
scoped_refptr<VideoFrame> CreateFrame(gfx::Size size,
VideoPixelFormat format,
base::TimeDelta timestamp,
uint32_t color = 0x964050) {
switch (format) {
case PIXEL_FORMAT_I420:
return CreateI420Frame(size, color, timestamp);
case PIXEL_FORMAT_NV12:
return CreateNV12Frame(size, color, timestamp);
case PIXEL_FORMAT_XRGB:
return CreateRGBFrame(size, color, timestamp);
default:
EXPECT_TRUE(false) << "not supported pixel format";
return nullptr;
}
}
VideoEncodeAccelerator::Config GetDefaultConfig() {
gfx::Size frame_size(640, 480);
uint32_t framerate = 30;
auto bitrate = Bitrate::ConstantBitrate(1000000u);
auto config = VideoEncodeAccelerator::Config(
pixel_format_, frame_size, profile_, bitrate, framerate,
VideoEncodeAccelerator::Config::StorageType::kShmem,
VideoEncodeAccelerator::Config::ContentType::kCamera);
config.gop_length = 1000;
config.required_encoder_type =
VideoEncodeAccelerator::Config::EncoderType::kNoPreference;
return config;
}
void Run() { loop_.Run(); }
std::unique_ptr<NullMediaLog> NullLog() {
return std::make_unique<NullMediaLog>();
}
std::unique_ptr<VideoEncodeAccelerator> MakeNdkAccelerator() {
auto runner = task_environment_.GetMainThreadTaskRunner();
return base::WrapUnique<VideoEncodeAccelerator>(
new NdkVideoEncodeAccelerator(runner));
}
void ValidateStream(base::span<uint8_t> data) {
EXPECT_GT(data.size(), 0u);
switch (codec_) {
case VideoCodec::kH264: {
H264Parser parser;
parser.SetStream(data.data(), data.size());
int num_parsed_nalus = 0;
while (true) {
media::H264SliceHeader shdr;
H264NALU nalu;
H264Parser::Result res = parser.AdvanceToNextNALU(&nalu);
if (res == H264Parser::kEOStream) {
EXPECT_GT(num_parsed_nalus, 0);
break;
}
EXPECT_EQ(res, H264Parser::kOk);
++num_parsed_nalus;
int id;
switch (nalu.nal_unit_type) {
case H264NALU::kSPS: {
EXPECT_EQ(parser.ParseSPS(&id), H264Parser::kOk);
const H264SPS* sps = parser.GetSPS(id);
VideoCodecProfile profile =
H264Parser::ProfileIDCToVideoCodecProfile(sps->profile_idc);
EXPECT_EQ(profile, profile_);
break;
}
case H264NALU::kPPS:
EXPECT_EQ(parser.ParsePPS(&id), H264Parser::kOk);
break;
default:
break;
}
}
break;
}
case VideoCodec::kVP9: {
Vp9Parser parser(true);
parser.SetStream(data.data(), data.size(), nullptr);
int num_parsed_frames = 0;
while (true) {
Vp9FrameHeader frame;
gfx::Size size;
std::unique_ptr<DecryptConfig> frame_decrypt_config;
Vp9Parser::Result res =
parser.ParseNextFrame(&frame, &size, &frame_decrypt_config);
if (res == Vp9Parser::kEOStream) {
EXPECT_GT(num_parsed_frames, 0);
break;
}
EXPECT_EQ(res, Vp9Parser::kOk);
++num_parsed_frames;
}
break;
}
default: {
EXPECT_TRUE(
base::ranges::any_of(data, [](uint8_t x) { return x != 0; }));
}
}
}
VideoCodec codec_;
VideoCodecProfile profile_;
VideoPixelFormat pixel_format_;
base::test::TaskEnvironment task_environment_;
base::test::ScopedFeatureList feature_list_;
base::RunLoop loop_;
std::unique_ptr<VideoEncodeAccelerator> accelerator_;
size_t output_buffer_size_ = 0;
scoped_refptr<base::UnsafeSharedMemoryPool> output_pool_ =
base::MakeRefCounted<base::UnsafeSharedMemoryPool>();
std::map<int32_t, std::unique_ptr<base::UnsafeSharedMemoryPool::Handle>>
id_to_buffer_;
struct Output {
int32_t id;
BitstreamBufferMetadata md;
};
std::vector<Output> outputs_;
std::optional<EncoderStatus> error_status_;
size_t input_buffer_size_ = 0;
int32_t last_buffer_id_ = 0;
VideoFrameConverter frame_converter_;
};
TEST_P(NdkVideoEncoderAcceleratorTest, InitializeAndDestroy) {
auto config = GetDefaultConfig();
accelerator_ = MakeNdkAccelerator();
EXPECT_CALL(*this, OnRequireBuffer()).WillOnce(Return(false));
bool result = accelerator_->Initialize(config, this, NullLog());
ASSERT_TRUE(result);
Run();
EXPECT_GE(id_to_buffer_.size(), 1u);
accelerator_.reset();
EXPECT_FALSE(error_status_.has_value());
}
TEST_P(NdkVideoEncoderAcceleratorTest, HandleEncodingError) {
auto config = GetDefaultConfig();
accelerator_ = MakeNdkAccelerator();
EXPECT_CALL(*this, OnRequireBuffer()).WillOnce(Return(true));
EXPECT_CALL(*this, OnError()).WillOnce(Return(false));
bool result = accelerator_->Initialize(config, this, NullLog());
ASSERT_TRUE(result);
auto size = config.input_visible_size;
// A frame with unsupported pixel format works as a way to induce a error.
auto frame = VideoFrame::CreateFrame(PIXEL_FORMAT_NV21, size, gfx::Rect(size),
size, {});
accelerator_->Encode(frame, true);
Run();
EXPECT_EQ(outputs_.size(), 0u);
EXPECT_TRUE(error_status_.has_value());
}
TEST_P(NdkVideoEncoderAcceleratorTest, EncodeSeveralFrames) {
const size_t total_frames_count = 10;
const size_t key_frame_index = 7;
auto config = GetDefaultConfig();
accelerator_ = MakeNdkAccelerator();
EXPECT_CALL(*this, OnRequireBuffer()).WillRepeatedly(Return(true));
EXPECT_CALL(*this, OnBufferReady()).WillRepeatedly([this]() {
if (outputs_.size() < total_frames_count)
return true;
return false;
});
bool result = accelerator_->Initialize(config, this, NullLog());
ASSERT_TRUE(result);
uint32_t color = 0x964050;
auto duration = base::Milliseconds(16);
for (auto frame_index = 0u; frame_index < total_frames_count; frame_index++) {
auto timestamp = frame_index * duration;
auto frame =
CreateFrame(config.input_visible_size, pixel_format_, timestamp, color);
color = (color << 1) + frame_index;
bool key_frame = (frame_index == key_frame_index);
accelerator_->Encode(frame, key_frame);
}
Run();
EXPECT_FALSE(error_status_.has_value());
EXPECT_GE(outputs_.size(), total_frames_count);
// Here we'd like to test that an output with at `key_frame_index`
// has a keyframe flag set to true, but because MediaCodec
// is unreliable in inserting keyframes at our request we can't test
// for it. In practice it usually works, just not always.
std::vector<uint8_t> stream;
for (auto& output : outputs_) {
auto& mapping = id_to_buffer_[output.id]->GetMapping();
EXPECT_GE(mapping.size(), output.md.payload_size_bytes);
EXPECT_GT(output.md.payload_size_bytes, 0u);
auto span =
mapping.GetMemoryAsSpan<uint8_t>().first(output.md.payload_size_bytes);
stream.insert(stream.end(), span.begin(), span.end());
}
ValidateStream(stream);
}
std::string PrintTestParams(const testing::TestParamInfo<VideoParams>& info) {
auto result = GetProfileName(info.param.profile) + "__" +
VideoPixelFormatToString(info.param.pixel_format);
// GTest doesn't like spaces, but profile names have spaces, so we need
// to replace them with underscores.
std::replace(result.begin(), result.end(), ' ', '_');
return result;
}
VideoParams kParams[] = {
{VP8PROFILE_MIN, PIXEL_FORMAT_I420},
{VP8PROFILE_MIN, PIXEL_FORMAT_NV12},
{VP9PROFILE_PROFILE0, PIXEL_FORMAT_I420},
{VP9PROFILE_PROFILE0, PIXEL_FORMAT_NV12},
{AV1PROFILE_PROFILE_MAIN, PIXEL_FORMAT_I420},
{AV1PROFILE_PROFILE_MAIN, PIXEL_FORMAT_NV12},
{H264PROFILE_BASELINE, PIXEL_FORMAT_I420},
{H264PROFILE_MAIN, PIXEL_FORMAT_I420},
{H264PROFILE_HIGH, PIXEL_FORMAT_I420},
{H264PROFILE_BASELINE, PIXEL_FORMAT_NV12},
#if BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)
{HEVCPROFILE_MAIN, PIXEL_FORMAT_I420},
{HEVCPROFILE_MAIN, PIXEL_FORMAT_NV12},
#endif
};
INSTANTIATE_TEST_SUITE_P(AllNdkEncoderTests,
NdkVideoEncoderAcceleratorTest,
::testing::ValuesIn(kParams),
PrintTestParams);
} // namespace media
#pragma clang attribute pop