1 /*
2 * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/video_coding/codecs/av1/libaom_av1_encoder.h"
12
13 #include <memory>
14 #include <vector>
15
16 #include "absl/types/optional.h"
17 #include "api/video_codecs/video_codec.h"
18 #include "api/video_codecs/video_encoder.h"
19 #include "modules/video_coding/codecs/av1/scalability_structure_l1t2.h"
20 #include "modules/video_coding/codecs/test/encoded_video_frame_producer.h"
21 #include "modules/video_coding/include/video_error_codes.h"
22 #include "test/gmock.h"
23 #include "test/gtest.h"
24
25 namespace webrtc {
26 namespace {
27
28 using ::testing::SizeIs;
29
DefaultCodecSettings()30 VideoCodec DefaultCodecSettings() {
31 VideoCodec codec_settings;
32 codec_settings.width = 320;
33 codec_settings.height = 180;
34 codec_settings.maxFramerate = 30;
35 codec_settings.maxBitrate = 1000;
36 codec_settings.qpMax = 63;
37 return codec_settings;
38 }
39
DefaultEncoderSettings()40 VideoEncoder::Settings DefaultEncoderSettings() {
41 return VideoEncoder::Settings(
42 VideoEncoder::Capabilities(/*loss_notification=*/false),
43 /*number_of_cores=*/1, /*max_payload_size=*/1200);
44 }
45
TEST(LibaomAv1EncoderTest,CanCreate)46 TEST(LibaomAv1EncoderTest, CanCreate) {
47 std::unique_ptr<VideoEncoder> encoder = CreateLibaomAv1Encoder();
48 EXPECT_TRUE(encoder);
49 }
50
TEST(LibaomAv1EncoderTest,InitAndRelease)51 TEST(LibaomAv1EncoderTest, InitAndRelease) {
52 std::unique_ptr<VideoEncoder> encoder = CreateLibaomAv1Encoder();
53 ASSERT_TRUE(encoder);
54 VideoCodec codec_settings = DefaultCodecSettings();
55 EXPECT_EQ(encoder->InitEncode(&codec_settings, DefaultEncoderSettings()),
56 WEBRTC_VIDEO_CODEC_OK);
57 EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_OK);
58 }
59
TEST(LibaomAv1EncoderTest,NoBitrateOnTopLayerRefecltedInActiveDecodeTargets)60 TEST(LibaomAv1EncoderTest, NoBitrateOnTopLayerRefecltedInActiveDecodeTargets) {
61 // Configure encoder with 2 temporal layers.
62 std::unique_ptr<VideoEncoder> encoder =
63 CreateLibaomAv1Encoder(std::make_unique<ScalabilityStructureL1T2>());
64 VideoCodec codec_settings = DefaultCodecSettings();
65 ASSERT_EQ(encoder->InitEncode(&codec_settings, DefaultEncoderSettings()),
66 WEBRTC_VIDEO_CODEC_OK);
67
68 VideoEncoder::RateControlParameters rate_parameters;
69 rate_parameters.framerate_fps = 30;
70 rate_parameters.bitrate.SetBitrate(0, /*temporal_index=*/0, 300'000);
71 rate_parameters.bitrate.SetBitrate(0, /*temporal_index=*/1, 0);
72 encoder->SetRates(rate_parameters);
73
74 std::vector<EncodedVideoFrameProducer::EncodedFrame> encoded_frames =
75 EncodedVideoFrameProducer(*encoder).SetNumInputFrames(1).Encode();
76 ASSERT_THAT(encoded_frames, SizeIs(1));
77 ASSERT_NE(encoded_frames[0].codec_specific_info.generic_frame_info,
78 absl::nullopt);
79 // Assuming L1T2 structure uses 1st decode target for T0 and 2nd decode target
80 // for T0+T1 frames, expect only 1st decode target is active.
81 EXPECT_EQ(encoded_frames[0]
82 .codec_specific_info.generic_frame_info->active_decode_targets,
83 0b01);
84 }
85
86 } // namespace
87 } // namespace webrtc
88