1 /*
2 * Copyright (c) 2021, Alliance for Open Media. All rights reserved.
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include <string>
13 #include <vector>
14 #include "gtest/gtest.h"
15 #include "test/codec_factory.h"
16 #include "test/encode_test_driver.h"
17 #include "test/md5_helper.h"
18 #include "test/util.h"
19 #include "test/y4m_video_source.h"
20 #include "test/yuv_video_source.h"
21
22 namespace {
23
24 #if CONFIG_FPMT_TEST && !CONFIG_REALTIME_ONLY
25 class AVxFrameParallelThreadEncodeTest
26 : public ::libaom_test::CodecTestWith3Params<int, int, int>,
27 public ::libaom_test::EncoderTest {
28 protected:
AVxFrameParallelThreadEncodeTest()29 AVxFrameParallelThreadEncodeTest()
30 : EncoderTest(GET_PARAM(0)), encoder_initialized_(false),
31 set_cpu_used_(GET_PARAM(1)), tile_cols_(GET_PARAM(2)),
32 tile_rows_(GET_PARAM(3)) {
33 aom_codec_dec_cfg_t cfg = aom_codec_dec_cfg_t();
34 cfg.w = 1280;
35 cfg.h = 720;
36 cfg.allow_lowbitdepth = 1;
37 decoder_ = codec_->CreateDecoder(cfg, 0);
38 }
~AVxFrameParallelThreadEncodeTest()39 ~AVxFrameParallelThreadEncodeTest() override { delete decoder_; }
40
SetUp()41 void SetUp() override {
42 InitializeConfig(::libaom_test::kTwoPassGood);
43 cfg_.rc_end_usage = AOM_VBR;
44 cfg_.g_lag_in_frames = 35;
45 cfg_.rc_2pass_vbr_minsection_pct = 5;
46 cfg_.rc_2pass_vbr_maxsection_pct = 2000;
47 cfg_.rc_max_quantizer = 63;
48 cfg_.rc_min_quantizer = 0;
49 cfg_.g_threads = 16;
50 }
51
BeginPassHook(unsigned int)52 void BeginPassHook(unsigned int /*pass*/) override {
53 encoder_initialized_ = false;
54 }
55
PreEncodeFrameHook(::libaom_test::VideoSource *,::libaom_test::Encoder * encoder)56 void PreEncodeFrameHook(::libaom_test::VideoSource * /*video*/,
57 ::libaom_test::Encoder *encoder) override {
58 if (encoder_initialized_) return;
59 SetTileSize(encoder);
60 encoder->Control(AOME_SET_CPUUSED, set_cpu_used_);
61 encoder->Control(AV1E_SET_FP_MT, 1);
62 encoder->Control(AV1E_SET_FP_MT_UNIT_TEST, enable_actual_parallel_encode_);
63 encoder->Control(AOME_SET_ENABLEAUTOALTREF, 1);
64 encoder->Control(AOME_SET_ARNR_MAXFRAMES, 7);
65 encoder->Control(AOME_SET_ARNR_STRENGTH, 5);
66 encoder->Control(AV1E_SET_FRAME_PARALLEL_DECODING, 0);
67
68 encoder_initialized_ = true;
69 }
70
SetTileSize(libaom_test::Encoder * encoder)71 virtual void SetTileSize(libaom_test::Encoder *encoder) {
72 encoder->Control(AV1E_SET_TILE_COLUMNS, tile_cols_);
73 encoder->Control(AV1E_SET_TILE_ROWS, tile_rows_);
74 }
75
FramePktHook(const aom_codec_cx_pkt_t * pkt)76 void FramePktHook(const aom_codec_cx_pkt_t *pkt) override {
77 size_enc_.push_back(pkt->data.frame.sz);
78
79 ::libaom_test::MD5 md5_enc;
80 md5_enc.Add(reinterpret_cast<uint8_t *>(pkt->data.frame.buf),
81 pkt->data.frame.sz);
82 md5_enc_.push_back(md5_enc.Get());
83
84 const aom_codec_err_t res = decoder_->DecodeFrame(
85 reinterpret_cast<uint8_t *>(pkt->data.frame.buf), pkt->data.frame.sz);
86 if (res != AOM_CODEC_OK) {
87 abort_ = true;
88 ASSERT_EQ(AOM_CODEC_OK, res);
89 }
90 const aom_image_t *img = decoder_->GetDxData().Next();
91
92 if (img) {
93 ::libaom_test::MD5 md5_res;
94 md5_res.Add(img);
95 md5_dec_.push_back(md5_res.Get());
96 }
97 }
98
DoTest(::libaom_test::VideoSource * input_video)99 void DoTest(::libaom_test::VideoSource *input_video) {
100 /* This is the actual parallel encode of frames using multiple cpis.
101 * The parallel frames are independently encoded.
102 * Threads are distributed among the parallel frames whereas non-parallel
103 * frames use all the threads. Example: for 8 threads, in case of 4 frames
104 * in a parallel encode set, each frame gets 2 threads. In case of 3 frames
105 * in a parallel encode set, threads are distributed as 2, 3 ,3.
106 */
107 enable_actual_parallel_encode_ = 1;
108 ASSERT_NO_FATAL_FAILURE(RunLoop(input_video));
109 std::vector<size_t> enc_stream_fpmt_size;
110 std::vector<std::string> enc_stream_fpmt;
111 std::vector<std::string> dec_stream_fpmt;
112 enc_stream_fpmt_size = size_enc_;
113 enc_stream_fpmt = md5_enc_;
114 dec_stream_fpmt = md5_dec_;
115 size_enc_.clear();
116 md5_enc_.clear();
117 md5_dec_.clear();
118
119 /* This is the simulation of parallel encode of frames using single cpi.
120 * In simulation, it should be ensured to have no dependency across frames
121 * (similar to parallel encode).
122 * Each frame uses all the threads configured.
123 */
124 enable_actual_parallel_encode_ = 0;
125 ASSERT_NO_FATAL_FAILURE(RunLoop(input_video));
126 std::vector<size_t> enc_stream_sim_size;
127 std::vector<std::string> enc_stream_sim;
128 std::vector<std::string> dec_stream_sim;
129 enc_stream_sim_size = size_enc_;
130 enc_stream_sim = md5_enc_;
131 dec_stream_sim = md5_dec_;
132 size_enc_.clear();
133 md5_enc_.clear();
134 md5_dec_.clear();
135
136 // Check that the vectors are equal.
137 ASSERT_EQ(enc_stream_sim_size, enc_stream_fpmt_size);
138 ASSERT_EQ(enc_stream_sim, enc_stream_fpmt);
139 ASSERT_EQ(dec_stream_sim, dec_stream_fpmt);
140 }
141
142 bool encoder_initialized_;
143 int set_cpu_used_;
144 int tile_cols_;
145 int tile_rows_;
146 int enable_actual_parallel_encode_;
147 ::libaom_test::Decoder *decoder_;
148 std::vector<size_t> size_enc_;
149 std::vector<std::string> md5_enc_;
150 std::vector<std::string> md5_dec_;
151 };
152
153 class AVxFrameParallelThreadEncodeHDResTestLarge
154 : public AVxFrameParallelThreadEncodeTest {};
155
TEST_P(AVxFrameParallelThreadEncodeHDResTestLarge,FrameParallelThreadEncodeTest)156 TEST_P(AVxFrameParallelThreadEncodeHDResTestLarge,
157 FrameParallelThreadEncodeTest) {
158 ::libaom_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
159 cfg_.rc_target_bitrate = 500;
160 DoTest(&video);
161 }
162
163 class AVxFrameParallelThreadEncodeLowResTestLarge
164 : public AVxFrameParallelThreadEncodeTest {};
165
TEST_P(AVxFrameParallelThreadEncodeLowResTestLarge,FrameParallelThreadEncodeTest)166 TEST_P(AVxFrameParallelThreadEncodeLowResTestLarge,
167 FrameParallelThreadEncodeTest) {
168 ::libaom_test::YUVVideoSource video("hantro_collage_w352h288.yuv",
169 AOM_IMG_FMT_I420, 352, 288, 30, 1, 0, 60);
170 cfg_.rc_target_bitrate = 200;
171 DoTest(&video);
172 }
173
174 class AVxFrameParallelThreadEncodeLowResTest
175 : public AVxFrameParallelThreadEncodeTest {};
176
TEST_P(AVxFrameParallelThreadEncodeLowResTest,FrameParallelThreadEncodeTest)177 TEST_P(AVxFrameParallelThreadEncodeLowResTest, FrameParallelThreadEncodeTest) {
178 ::libaom_test::YUVVideoSource video("hantro_collage_w352h288.yuv",
179 AOM_IMG_FMT_I420, 352, 288, 30, 1, 0, 60);
180 cfg_.rc_target_bitrate = 200;
181 DoTest(&video);
182 }
183
184 AV1_INSTANTIATE_TEST_SUITE(AVxFrameParallelThreadEncodeHDResTestLarge,
185 ::testing::Values(2, 3, 4, 5, 6),
186 ::testing::Values(0, 1, 2), ::testing::Values(0, 1));
187
188 AV1_INSTANTIATE_TEST_SUITE(AVxFrameParallelThreadEncodeLowResTestLarge,
189 ::testing::Values(2, 3), ::testing::Values(0, 1, 2),
190 ::testing::Values(0, 1));
191
192 AV1_INSTANTIATE_TEST_SUITE(AVxFrameParallelThreadEncodeLowResTest,
193 ::testing::Values(4, 5, 6), ::testing::Values(1),
194 ::testing::Values(0));
195 #endif // CONFIG_FPMT_TEST && !CONFIG_REALTIME_ONLY
196
197 } // namespace
198