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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
media / video / video_encode_accelerator_adapter_test.cc [blame]
// Copyright 2020 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/video/video_encode_accelerator_adapter.h"
#include <memory>
#include <string>
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/viz/test/test_context_provider.h"
#include "media/base/bitrate.h"
#include "media/base/media_util.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 "media/video/video_encoder_info.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libyuv/include/libyuv.h"
#include "ui/gfx/color_space.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_);
sii_ = base::MakeRefCounted<gpu::TestSharedImageInterface>();
sii_->UseTestGMBInSharedImageCreationWithBufferUsage();
gpu_factories_ =
std::make_unique<MockGpuVideoAcceleratorFactories>(sii_.get());
supported_profiles_ = {
VideoEncodeAccelerator::SupportedProfile(
profile_,
/*max_resolution=*/gfx::Size(3840, 2160),
/*max_framerate_numerator=*/30,
/*max_framerate_denominator=*/1,
/*rc_modes=*/VideoEncodeAccelerator::kConstantMode |
VideoEncodeAccelerator::kVariableMode),
};
EXPECT_CALL(*gpu_factories_.get(),
GetVideoEncodeAcceleratorSupportedProfiles())
.WillRepeatedly(Return(supported_profiles_));
EXPECT_CALL(*gpu_factories_.get(), DoCreateVideoEncodeAccelerator())
.WillRepeatedly(Return(vea_.get()));
EXPECT_CALL(*gpu_factories_.get(), GetTaskRunner())
.WillRepeatedly(Return(vea_runner_));
auto media_log = std::make_unique<NullMediaLog>();
callback_runner_ = base::SequencedTaskRunner::GetCurrentDefault();
vae_adapter_ = std::make_unique<VideoEncodeAcceleratorAdapter>(
gpu_factories_.get(), media_log->Clone(), 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) {
// Define shared image usage for a mappable shared image.
constexpr auto si_usage = gpu::SHARED_IMAGE_USAGE_CPU_WRITE_ONLY |
gpu::SHARED_IMAGE_USAGE_DISPLAY_READ;
auto shared_image = sii_->CreateSharedImage(
{viz::MultiPlaneFormat::kNV12, size, kYUVColorSpace,
gpu::SharedImageUsageSet(si_usage),
"VideoEncodeAcceleratorAdapterTest"},
gpu::kNullSurfaceHandle,
gfx::BufferUsage::VEA_READ_CAMERA_AND_CPU_READ_WRITE);
if (!shared_image) {
return nullptr;
}
auto scoped_mapping = shared_image->Map();
if (!scoped_mapping) {
return nullptr;
}
std::ranges::fill(scoped_mapping->GetMemoryForPlane(0), 0x96);
std::ranges::fill(scoped_mapping->GetMemoryForPlane(1), 0x28);
auto frame = VideoFrame::WrapMappableSharedImage(
std::move(shared_image), sii_->GenVerifiedSyncToken(),
base::NullCallback(), gfx::Rect(size), size, timestamp);
frame->set_color_space(kYUVColorSpace);
return frame;
}
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->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
0x96, // Y color
0x40, // U color
0x40); // V color
frame->set_color_space(kYUVColorSpace);
return frame;
}
scoped_refptr<VideoFrame> CreateGreenCpuFrameARGB(gfx::Size size,
base::TimeDelta timestamp) {
auto frame = VideoFrame::CreateFrame(PIXEL_FORMAT_XRGB, size,
gfx::Rect(size), size, timestamp);
// Green XRGB frame (R:0x3B, G:0xD9, B:0x24)
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
0x24D93B00); // V color
frame->set_color_space(kRGBColorSpace);
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);
case PIXEL_FORMAT_XRGB:
return CreateGreenCpuFrameARGB(size, timestamp);
default:
EXPECT_TRUE(false) << "not supported pixel format";
return nullptr;
}
}
gfx::ColorSpace ExpectedColorSpace(VideoPixelFormat src_format,
VideoPixelFormat dst_format) {
// Converting between YUV formats doesn't change the color space.
if (IsYuvPlanar(src_format) && IsYuvPlanar(dst_format)) {
return kYUVColorSpace;
}
// libyuv's RGB to YUV methods always output BT.601.
if (IsRGB(src_format) && IsYuvPlanar(dst_format)) {
return gfx::ColorSpace::CreateREC601();
}
EXPECT_TRUE(false) << "unexpected formats: src=" << src_format
<< ", dst=" << dst_format;
return gfx::ColorSpace();
}
VideoEncoder::EncoderStatusCB 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)}](EncoderStatus 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;
static constexpr gfx::ColorSpace kRGBColorSpace =
gfx::ColorSpace::CreateSRGB();
static constexpr gfx::ColorSpace kYUVColorSpace =
gfx::ColorSpace::CreateREC709();
std::vector<VideoEncodeAccelerator::SupportedProfile> supported_profiles_;
base::test::TaskEnvironment task_environment_;
raw_ptr<FakeVideoEncodeAccelerator, AcrossTasksDanglingUntriaged>
vea_; // owned by |vae_adapter_|
scoped_refptr<gpu::TestSharedImageInterface> sii_;
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);
adapter()->Initialize(profile_, options, /*info_cb=*/base::DoNothing(),
/*output_cb=*/base::DoNothing(), ValidatingStatusCB());
RunUntilIdle();
}
TEST_F(VideoEncodeAcceleratorAdapterTest, InitializeAfterFirstFrame) {
VideoEncoder::Options options;
options.frame_size = gfx::Size(640, 480);
auto pixel_format = PIXEL_FORMAT_I420;
const gfx::ColorSpace expected_color_space =
ExpectedColorSpace(pixel_format, pixel_format);
bool info_cb_called = false;
VideoEncoder::EncoderInfoCB info_cb = base::BindLambdaForTesting(
[&](const VideoEncoderInfo& info) { info_cb_called = true; });
int outputs_count = 0;
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
EXPECT_EQ(output.color_space, expected_color_space);
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(info_cb),
std::move(output_cb), ValidatingStatusCB());
auto frame =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(1));
adapter()->Encode(frame, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
RunUntilIdle();
vea()->NotifyEncoderInfoChange(VideoEncoderInfo());
RunUntilIdle();
EXPECT_TRUE(info_cb_called);
EXPECT_EQ(outputs_count, 1);
}
TEST_F(VideoEncodeAcceleratorAdapterTest, TemporalSvc) {
VideoEncoder::Options options;
options.frame_size = gfx::Size(640, 480);
options.scalability_mode = SVCScalabilityMode::kL1T3;
int outputs_count = 0;
auto pixel_format = PIXEL_FORMAT_I420;
const gfx::ColorSpace expected_color_space =
ExpectedColorSpace(pixel_format, pixel_format);
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
if (output.timestamp == base::Milliseconds(1))
EXPECT_EQ(output.temporal_id, 1);
else if (output.timestamp == base::Milliseconds(2))
EXPECT_EQ(output.temporal_id, 1);
else if (output.timestamp == base::Milliseconds(3))
EXPECT_EQ(output.temporal_id, 2);
else if (output.timestamp == base::Milliseconds(4))
EXPECT_EQ(output.temporal_id, 2);
EXPECT_EQ(output.color_space, expected_color_space);
outputs_count++;
});
vea()->SetEncodingCallback(base::BindLambdaForTesting(
[&](BitstreamBuffer&, bool keyframe, scoped_refptr<VideoFrame> frame) {
BitstreamBufferMetadata result(1, keyframe, frame->timestamp());
if (frame->timestamp() == base::Milliseconds(1)) {
result.h264 = H264Metadata();
result.h264->temporal_idx = 1;
} else if (frame->timestamp() == base::Milliseconds(2)) {
result.vp8 = Vp8Metadata();
result.vp8->temporal_idx = 1;
} else if (frame->timestamp() == base::Milliseconds(3)) {
result.vp9 = Vp9Metadata();
result.vp9->temporal_idx = 2;
} else if (frame->timestamp() == base::Milliseconds(4)) {
result.svc_generic = SVCGenericMetadata();
result.svc_generic->temporal_idx = 2;
}
return result;
}));
adapter()->Initialize(profile_, options, /*info_cb=*/base::DoNothing(),
std::move(output_cb), ValidatingStatusCB());
auto frame1 =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(1));
auto frame2 =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(2));
auto frame3 =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(3));
auto frame4 =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(4));
adapter()->Encode(frame1, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
RunUntilIdle();
adapter()->Encode(frame2, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
RunUntilIdle();
adapter()->Encode(frame3, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
RunUntilIdle();
adapter()->Encode(frame4, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
RunUntilIdle();
EXPECT_EQ(outputs_count, 4);
}
TEST_F(VideoEncodeAcceleratorAdapterTest, FlushDuringInitialize) {
VideoEncoder::Options options;
options.frame_size = gfx::Size(640, 480);
int outputs_count = 0;
auto pixel_format = PIXEL_FORMAT_I420;
const gfx::ColorSpace expected_color_space =
ExpectedColorSpace(pixel_format, pixel_format);
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
EXPECT_EQ(output.color_space, expected_color_space);
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, /*info_cb=*/base::DoNothing(),
std::move(output_cb), ValidatingStatusCB());
auto frame =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(1));
adapter()->Encode(frame, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
adapter()->Flush(base::BindLambdaForTesting([&](EncoderStatus s) {
EXPECT_TRUE(s.is_ok());
EXPECT_EQ(outputs_count, 1);
}));
RunUntilIdle();
}
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, std::optional<VideoEncoder::CodecDescription>) {
outputs_count++;
});
VideoEncoder::EncoderStatusCB expect_error_done_cb =
base::BindLambdaForTesting(
[&](EncoderStatus 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, /*info_cb=*/base::DoNothing(),
std::move(output_cb), base::BindLambdaForTesting([](EncoderStatus s) {
EXPECT_EQ(s.code(), EncoderStatus::Codes::kEncoderUnsupportedProfile);
}));
auto frame =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(1));
adapter()->Encode(frame, VideoEncoder::EncodeOptions(true),
std::move(expect_error_done_cb));
RunUntilIdle();
EXPECT_EQ(outputs_count, 0);
}
TEST_F(VideoEncodeAcceleratorAdapterTest, EncodingError) {
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, std::optional<VideoEncoder::CodecDescription>) {
outputs_count++;
});
VideoEncoder::EncoderStatusCB expect_error_done_cb =
base::BindLambdaForTesting(
[&](EncoderStatus s) { EXPECT_FALSE(s.is_ok()); });
adapter()->Initialize(profile_, options, /*info_cb=*/base::DoNothing(),
std::move(output_cb), ValidatingStatusCB());
vea()->SetWillEncodingSucceed(false);
auto frame =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(1));
adapter()->Encode(frame, VideoEncoder::EncodeOptions(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();
auto small_frame =
CreateGreenFrame(small_size, pixel_format, base::Milliseconds(1));
auto large_frame =
CreateGreenFrame(large_size, pixel_format, base::Milliseconds(2));
VideoPixelFormat expected_input_format = PIXEL_FORMAT_I420;
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
if (pixel_format != PIXEL_FORMAT_I420 || !small_frame->IsMappable())
expected_input_format = PIXEL_FORMAT_NV12;
#endif
const gfx::ColorSpace expected_color_space =
ExpectedColorSpace(pixel_format, expected_input_format);
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
EXPECT_EQ(output.color_space, expected_color_space);
outputs_count++;
});
vea()->SetEncodingCallback(base::BindLambdaForTesting(
[&](BitstreamBuffer&, bool keyframe, scoped_refptr<VideoFrame> frame) {
EXPECT_EQ(frame->format(), expected_input_format);
EXPECT_EQ(frame->coded_size(), options.frame_size);
return BitstreamBufferMetadata(1, keyframe, frame->timestamp());
}));
adapter()->Initialize(profile_, options, /*info_cb=*/base::DoNothing(),
std::move(output_cb), ValidatingStatusCB());
adapter()->Encode(small_frame, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
adapter()->Encode(large_frame, VideoEncoder::EncodeOptions(false),
ValidatingStatusCB());
RunUntilIdle();
EXPECT_EQ(outputs_count, 2);
}
TEST_F(VideoEncodeAcceleratorAdapterTest, AutomaticResizeSupport) {
VideoEncoder::Options options;
options.frame_size = gfx::Size(640, 480);
int outputs_count = 0;
gfx::Size small_size(480, 320);
auto pixel_format = PIXEL_FORMAT_NV12;
const gfx::ColorSpace expected_color_space =
ExpectedColorSpace(pixel_format, pixel_format);
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
EXPECT_EQ(output.color_space, expected_color_space);
outputs_count++;
});
vea()->SetEncodingCallback(base::BindLambdaForTesting(
[&](BitstreamBuffer&, bool keyframe, scoped_refptr<VideoFrame> frame) {
EXPECT_EQ(frame->coded_size(), small_size);
return BitstreamBufferMetadata(1, keyframe, frame->timestamp());
}));
vea()->SupportResize();
adapter()->Initialize(profile_, options, /*info_cb=*/base::DoNothing(),
std::move(output_cb), ValidatingStatusCB());
auto frame1 =
CreateGreenFrame(small_size, pixel_format, base::Milliseconds(1));
auto frame2 =
CreateGreenFrame(small_size, pixel_format, base::Milliseconds(2));
adapter()->Encode(frame1, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
adapter()->Encode(frame2, VideoEncoder::EncodeOptions(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);
const VideoPixelFormat expected_input_format =
input_kind == VideoEncodeAcceleratorAdapter::InputBufferKind::GpuMemBuf
? PIXEL_FORMAT_NV12
: PIXEL_FORMAT_I420;
constexpr auto get_source_format = [](int i) {
// Every 4 frames switch between the 3 supported formats.
const int rem = i % 12;
auto format = PIXEL_FORMAT_XRGB;
if (rem < 4)
format = PIXEL_FORMAT_I420;
else if (rem < 8)
format = PIXEL_FORMAT_NV12;
return format;
};
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
VideoPixelFormat source_frame_format = get_source_format(outputs_count);
const gfx::ColorSpace expected_color_space =
ExpectedColorSpace(source_frame_format, expected_input_format);
EXPECT_EQ(output.color_space, expected_color_space);
outputs_count++;
});
vea()->SetEncodingCallback(base::BindLambdaForTesting(
[&](BitstreamBuffer&, bool keyframe, scoped_refptr<VideoFrame> frame) {
EXPECT_EQ(frame->format(), expected_input_format);
EXPECT_EQ(frame->coded_size(), options.frame_size);
return BitstreamBufferMetadata(1, keyframe, frame->timestamp());
}));
adapter()->Initialize(profile_, options, /*info_cb=*/base::DoNothing(),
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;
const auto format = get_source_format(frame_index);
bool key = frame_index % 9 == 0;
auto frame =
CreateGreenFrame(size, format, base::Milliseconds(frame_index));
adapter()->Encode(frame, VideoEncoder::EncodeOptions(key),
ValidatingStatusCB());
}
RunUntilIdle();
EXPECT_EQ(outputs_count, frames_to_encode);
}
TEST_F(VideoEncodeAcceleratorAdapterTest, DroppedFrame) {
VideoEncoder::Options options;
options.frame_size = gfx::Size(640, 480);
auto pixel_format = PIXEL_FORMAT_I420;
std::vector<base::TimeDelta> output_timestamps;
std::vector<base::TimeDelta> dropped_output_timestamps;
const gfx::ColorSpace expected_color_space =
ExpectedColorSpace(pixel_format, pixel_format);
VideoEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
if (output.data.empty()) {
dropped_output_timestamps.push_back(output.timestamp);
return;
}
EXPECT_EQ(output.color_space, expected_color_space);
output_timestamps.push_back(output.timestamp);
});
vea()->SetEncodingCallback(base::BindLambdaForTesting(
[&](BitstreamBuffer&, bool keyframe, scoped_refptr<VideoFrame> frame) {
size_t size = keyframe ? 1 : 0; // Drop non-key frame
return BitstreamBufferMetadata(size, keyframe, frame->timestamp());
}));
adapter()->Initialize(profile_, options, /*info_cb=*/base::DoNothing(),
std::move(output_cb), ValidatingStatusCB());
auto frame1 =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(1));
auto frame2 =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(2));
auto frame3 =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(3));
adapter()->Encode(frame1, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
adapter()->Encode(frame2, VideoEncoder::EncodeOptions(false),
ValidatingStatusCB());
adapter()->Encode(frame3, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
RunUntilIdle();
ASSERT_EQ(output_timestamps.size(), 2u);
EXPECT_EQ(output_timestamps[0], base::Milliseconds(1));
EXPECT_EQ(output_timestamps[1], base::Milliseconds(3));
ASSERT_EQ(dropped_output_timestamps.size(), 1u);
EXPECT_EQ(dropped_output_timestamps[0], base::Milliseconds(2));
}
TEST_F(VideoEncodeAcceleratorAdapterTest,
ChangeOptions_ChangeVariableBitrateSmokeTest) {
VideoEncoder::Options options;
options.frame_size = gfx::Size(640, 480);
options.bitrate = Bitrate::VariableBitrate(1111u, 2222u);
auto pixel_format = PIXEL_FORMAT_I420;
int output_count_before_change = 0;
int output_count_after_change = 0;
const gfx::ColorSpace expected_color_space =
ExpectedColorSpace(pixel_format, pixel_format);
VideoEncoder::OutputCB first_output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
EXPECT_EQ(output.color_space, expected_color_space);
output_count_before_change++;
});
VideoEncoder::OutputCB second_output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
EXPECT_EQ(output.color_space, expected_color_space);
output_count_after_change++;
});
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, /*info_cb=*/base::DoNothing(),
std::move(first_output_cb), ValidatingStatusCB());
// We must encode one frame before we can change options.
auto first_frame =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(1));
adapter()->Encode(first_frame, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
RunUntilIdle();
options.bitrate = Bitrate::VariableBitrate(12345u, 23456u);
adapter()->ChangeOptions(
options, std::move(second_output_cb),
base::BindLambdaForTesting([&](EncoderStatus s) {
auto second_frame = CreateGreenFrame(options.frame_size, pixel_format,
base::Milliseconds(2));
adapter()->Encode(second_frame, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
}));
RunUntilIdle();
EXPECT_EQ(output_count_before_change, 1);
EXPECT_EQ(output_count_after_change, 1);
}
TEST_F(VideoEncodeAcceleratorAdapterTest, ChangeOptions_ChangeFrameSize) {
VideoEncoder::Options options;
auto first_frame_size = gfx::Size(640, 480);
auto second_frame_size = gfx::Size(1280, 720);
auto pixel_format = PIXEL_FORMAT_I420;
int output_count_before_change = 0;
int output_count_after_change = 0;
options.frame_size = first_frame_size;
options.bitrate = Bitrate::VariableBitrate(1111u, 2222u);
VideoEncoder::OutputCB first_output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
EXPECT_EQ(output.encoded_size, first_frame_size);
output_count_before_change++;
});
VideoEncoder::OutputCB second_output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
EXPECT_EQ(output.encoded_size, second_frame_size);
output_count_after_change++;
});
adapter()->Initialize(profile_, options, /*info_cb=*/base::DoNothing(),
std::move(first_output_cb), ValidatingStatusCB());
auto first_frame =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(1));
adapter()->Encode(first_frame, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
RunUntilIdle();
options.frame_size = second_frame_size;
adapter()->ChangeOptions(
options, std::move(second_output_cb),
base::BindLambdaForTesting([&](EncoderStatus s) {
EXPECT_TRUE(s.is_ok());
auto second_frame = CreateGreenFrame(options.frame_size, pixel_format,
base::Milliseconds(2));
adapter()->Encode(second_frame, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
}));
RunUntilIdle();
EXPECT_EQ(output_count_before_change, 1);
EXPECT_EQ(output_count_after_change, 1);
}
TEST_F(VideoEncodeAcceleratorAdapterTest,
ChangeOptions_ChangeFrameSizeNotSupported) {
VideoEncoder::Options options;
auto first_frame_size = gfx::Size(640, 480);
auto second_frame_size = gfx::Size(1280, 720);
auto pixel_format = PIXEL_FORMAT_I420;
int output_count_before_change = 0;
int output_count_after_change = 0;
options.frame_size = first_frame_size;
options.bitrate = Bitrate::VariableBitrate(1111u, 2222u);
VideoEncoder::OutputCB first_output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
EXPECT_EQ(output.encoded_size, first_frame_size);
output_count_before_change++;
});
VideoEncoder::OutputCB second_output_cb = base::BindLambdaForTesting(
[&](VideoEncoderOutput output,
std::optional<VideoEncoder::CodecDescription>) {
EXPECT_EQ(output.encoded_size, second_frame_size);
output_count_after_change++;
});
adapter()->Initialize(profile_, options, /*info_cb=*/base::DoNothing(),
std::move(first_output_cb), ValidatingStatusCB());
auto first_frame =
CreateGreenFrame(options.frame_size, pixel_format, base::Milliseconds(1));
adapter()->Encode(first_frame, VideoEncoder::EncodeOptions(true),
ValidatingStatusCB());
RunUntilIdle();
vea()->SetSupportFrameSizeChange(false);
options.frame_size = second_frame_size;
adapter()->ChangeOptions(options, std::move(second_output_cb),
base::BindLambdaForTesting([](EncoderStatus s) {
EXPECT_FALSE(s.is_ok());
}));
EXPECT_EQ(output_count_before_change, 1);
EXPECT_EQ(output_count_after_change, 0);
}
INSTANTIATE_TEST_SUITE_P(VideoEncodeAcceleratorAdapterTest,
VideoEncodeAcceleratorAdapterTest,
::testing::Values(PIXEL_FORMAT_I420,
PIXEL_FORMAT_NV12,
PIXEL_FORMAT_XRGB));
} // namespace media