1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2021, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include <fstream>
13*77c1e3ccSAndroid Build Coastguard Worker #include <new>
14*77c1e3ccSAndroid Build Coastguard Worker #include <sstream>
15*77c1e3ccSAndroid Build Coastguard Worker #include <string>
16*77c1e3ccSAndroid Build Coastguard Worker
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_codec.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_external_partition.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/blockd.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodeframe_utils.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "test/codec_factory.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "test/encode_test_driver.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "test/y4m_video_source.h"
25*77c1e3ccSAndroid Build Coastguard Worker #include "test/util.h"
26*77c1e3ccSAndroid Build Coastguard Worker
27*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_ENCODER
28*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
29*77c1e3ccSAndroid Build Coastguard Worker namespace {
30*77c1e3ccSAndroid Build Coastguard Worker
31*77c1e3ccSAndroid Build Coastguard Worker constexpr int kFrameNum = 8;
32*77c1e3ccSAndroid Build Coastguard Worker constexpr int kVersion = 1;
33*77c1e3ccSAndroid Build Coastguard Worker
34*77c1e3ccSAndroid Build Coastguard Worker typedef struct TestData {
35*77c1e3ccSAndroid Build Coastguard Worker int version = kVersion;
36*77c1e3ccSAndroid Build Coastguard Worker } TestData;
37*77c1e3ccSAndroid Build Coastguard Worker
38*77c1e3ccSAndroid Build Coastguard Worker typedef struct ToyModel {
39*77c1e3ccSAndroid Build Coastguard Worker TestData *data;
40*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_config_t config;
41*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_funcs_t funcs;
42*77c1e3ccSAndroid Build Coastguard Worker int mi_row;
43*77c1e3ccSAndroid Build Coastguard Worker int mi_col;
44*77c1e3ccSAndroid Build Coastguard Worker int frame_width;
45*77c1e3ccSAndroid Build Coastguard Worker int frame_height;
46*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE block_size;
47*77c1e3ccSAndroid Build Coastguard Worker } ToyModel;
48*77c1e3ccSAndroid Build Coastguard Worker
49*77c1e3ccSAndroid Build Coastguard Worker // Note:
50*77c1e3ccSAndroid Build Coastguard Worker // if CONFIG_PARTITION_SEARCH_ORDER = 0, we test APIs designed for the baseline
51*77c1e3ccSAndroid Build Coastguard Worker // encoder's DFS partition search workflow.
52*77c1e3ccSAndroid Build Coastguard Worker // if CONFIG_PARTITION_SEARCH_ORDER = 1, we test APIs designed for the new
53*77c1e3ccSAndroid Build Coastguard Worker // ML model's partition search workflow.
54*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_PARTITION_SEARCH_ORDER
ext_part_create_model(void * priv,const aom_ext_part_config_t * part_config,aom_ext_part_model_t * ext_part_model)55*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_create_model(
56*77c1e3ccSAndroid Build Coastguard Worker void *priv, const aom_ext_part_config_t *part_config,
57*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t *ext_part_model) {
58*77c1e3ccSAndroid Build Coastguard Worker TestData *received_data = reinterpret_cast<TestData *>(priv);
59*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(received_data->version, kVersion);
60*77c1e3ccSAndroid Build Coastguard Worker ToyModel *toy_model = new (std::nothrow) ToyModel;
61*77c1e3ccSAndroid Build Coastguard Worker if (toy_model == nullptr) {
62*77c1e3ccSAndroid Build Coastguard Worker EXPECT_NE(toy_model, nullptr);
63*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_ERROR;
64*77c1e3ccSAndroid Build Coastguard Worker }
65*77c1e3ccSAndroid Build Coastguard Worker toy_model->data = received_data;
66*77c1e3ccSAndroid Build Coastguard Worker *ext_part_model = toy_model;
67*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(part_config->superblock_size, BLOCK_64X64);
68*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_OK;
69*77c1e3ccSAndroid Build Coastguard Worker }
70*77c1e3ccSAndroid Build Coastguard Worker
ext_part_send_features(aom_ext_part_model_t ext_part_model,const aom_partition_features_t * part_features)71*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_send_features(
72*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t ext_part_model,
73*77c1e3ccSAndroid Build Coastguard Worker const aom_partition_features_t *part_features) {
74*77c1e3ccSAndroid Build Coastguard Worker ToyModel *toy_model = static_cast<ToyModel *>(ext_part_model);
75*77c1e3ccSAndroid Build Coastguard Worker toy_model->mi_row = part_features->mi_row;
76*77c1e3ccSAndroid Build Coastguard Worker toy_model->mi_col = part_features->mi_col;
77*77c1e3ccSAndroid Build Coastguard Worker toy_model->frame_width = part_features->frame_width;
78*77c1e3ccSAndroid Build Coastguard Worker toy_model->frame_height = part_features->frame_height;
79*77c1e3ccSAndroid Build Coastguard Worker toy_model->block_size = static_cast<BLOCK_SIZE>(part_features->block_size);
80*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_OK;
81*77c1e3ccSAndroid Build Coastguard Worker }
82*77c1e3ccSAndroid Build Coastguard Worker
83*77c1e3ccSAndroid Build Coastguard Worker // The model provide the whole decision tree to the encoder.
ext_part_get_partition_decision_whole_tree(aom_ext_part_model_t ext_part_model,aom_partition_decision_t * ext_part_decision)84*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_get_partition_decision_whole_tree(
85*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t ext_part_model,
86*77c1e3ccSAndroid Build Coastguard Worker aom_partition_decision_t *ext_part_decision) {
87*77c1e3ccSAndroid Build Coastguard Worker ToyModel *toy_model = static_cast<ToyModel *>(ext_part_model);
88*77c1e3ccSAndroid Build Coastguard Worker // A toy model that always asks the encoder to encode with
89*77c1e3ccSAndroid Build Coastguard Worker // 4x4 blocks (the smallest).
90*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->is_final_decision = 1;
91*77c1e3ccSAndroid Build Coastguard Worker // Note: super block size is fixed to BLOCK_64X64 for the
92*77c1e3ccSAndroid Build Coastguard Worker // input video. It is determined inside the encoder, see the
93*77c1e3ccSAndroid Build Coastguard Worker // check in "ext_part_create_model".
94*77c1e3ccSAndroid Build Coastguard Worker const int is_last_sb_col =
95*77c1e3ccSAndroid Build Coastguard Worker toy_model->mi_col * 4 + 64 > toy_model->frame_width;
96*77c1e3ccSAndroid Build Coastguard Worker const int is_last_sb_row =
97*77c1e3ccSAndroid Build Coastguard Worker toy_model->mi_row * 4 + 64 > toy_model->frame_height;
98*77c1e3ccSAndroid Build Coastguard Worker if (is_last_sb_row && is_last_sb_col) {
99*77c1e3ccSAndroid Build Coastguard Worker // 64x64: 1 node
100*77c1e3ccSAndroid Build Coastguard Worker // 32x32: 4 nodes (only the first one will further split)
101*77c1e3ccSAndroid Build Coastguard Worker // 16x16: 4 nodes
102*77c1e3ccSAndroid Build Coastguard Worker // 8x8: 4 * 4 nodes
103*77c1e3ccSAndroid Build Coastguard Worker // 4x4: 4 * 4 * 4 nodes
104*77c1e3ccSAndroid Build Coastguard Worker const int num_blocks = 1 + 4 + 4 + 4 * 4 + 4 * 4 * 4;
105*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_blocks = 4 * 4 * 4;
106*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->num_nodes = num_blocks;
107*77c1e3ccSAndroid Build Coastguard Worker // 64x64
108*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[0] = PARTITION_SPLIT;
109*77c1e3ccSAndroid Build Coastguard Worker // 32x32, only the first one will split, the other three are
110*77c1e3ccSAndroid Build Coastguard Worker // out of frame boundary.
111*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[1] = PARTITION_SPLIT;
112*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[2] = PARTITION_NONE;
113*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[3] = PARTITION_NONE;
114*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[4] = PARTITION_NONE;
115*77c1e3ccSAndroid Build Coastguard Worker // The rest blocks inside the top-left 32x32 block.
116*77c1e3ccSAndroid Build Coastguard Worker for (int i = 5; i < num_blocks - num_4x4_blocks; ++i) {
117*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[0] = PARTITION_SPLIT;
118*77c1e3ccSAndroid Build Coastguard Worker }
119*77c1e3ccSAndroid Build Coastguard Worker for (int i = num_blocks - num_4x4_blocks; i < num_blocks; ++i) {
120*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[i] = PARTITION_NONE;
121*77c1e3ccSAndroid Build Coastguard Worker }
122*77c1e3ccSAndroid Build Coastguard Worker } else if (is_last_sb_row) {
123*77c1e3ccSAndroid Build Coastguard Worker // 64x64: 1 node
124*77c1e3ccSAndroid Build Coastguard Worker // 32x32: 4 nodes (only the first two will further split)
125*77c1e3ccSAndroid Build Coastguard Worker // 16x16: 2 * 4 nodes
126*77c1e3ccSAndroid Build Coastguard Worker // 8x8: 2 * 4 * 4 nodes
127*77c1e3ccSAndroid Build Coastguard Worker // 4x4: 2 * 4 * 4 * 4 nodes
128*77c1e3ccSAndroid Build Coastguard Worker const int num_blocks = 1 + 4 + 2 * 4 + 2 * 4 * 4 + 2 * 4 * 4 * 4;
129*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_blocks = 2 * 4 * 4 * 4;
130*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->num_nodes = num_blocks;
131*77c1e3ccSAndroid Build Coastguard Worker // 64x64
132*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[0] = PARTITION_SPLIT;
133*77c1e3ccSAndroid Build Coastguard Worker // 32x32, only the first two will split, the other two are out
134*77c1e3ccSAndroid Build Coastguard Worker // of frame boundary.
135*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[1] = PARTITION_SPLIT;
136*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[2] = PARTITION_SPLIT;
137*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[3] = PARTITION_NONE;
138*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[4] = PARTITION_NONE;
139*77c1e3ccSAndroid Build Coastguard Worker // The rest blocks.
140*77c1e3ccSAndroid Build Coastguard Worker for (int i = 5; i < num_blocks - num_4x4_blocks; ++i) {
141*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[0] = PARTITION_SPLIT;
142*77c1e3ccSAndroid Build Coastguard Worker }
143*77c1e3ccSAndroid Build Coastguard Worker for (int i = num_blocks - num_4x4_blocks; i < num_blocks; ++i) {
144*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[i] = PARTITION_NONE;
145*77c1e3ccSAndroid Build Coastguard Worker }
146*77c1e3ccSAndroid Build Coastguard Worker } else if (is_last_sb_col) {
147*77c1e3ccSAndroid Build Coastguard Worker // 64x64: 1 node
148*77c1e3ccSAndroid Build Coastguard Worker // 32x32: 4 nodes (only the top-left and bottom-left will further split)
149*77c1e3ccSAndroid Build Coastguard Worker // 16x16: 2 * 4 nodes
150*77c1e3ccSAndroid Build Coastguard Worker // 8x8: 2 * 4 * 4 nodes
151*77c1e3ccSAndroid Build Coastguard Worker // 4x4: 2 * 4 * 4 * 4 nodes
152*77c1e3ccSAndroid Build Coastguard Worker const int num_blocks = 1 + 4 + 2 * 4 + 2 * 4 * 4 + 2 * 4 * 4 * 4;
153*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_blocks = 2 * 4 * 4 * 4;
154*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->num_nodes = num_blocks;
155*77c1e3ccSAndroid Build Coastguard Worker // 64x64
156*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[0] = PARTITION_SPLIT;
157*77c1e3ccSAndroid Build Coastguard Worker // 32x32, only the top-left and bottom-left will split, the other two are
158*77c1e3ccSAndroid Build Coastguard Worker // out of frame boundary.
159*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[1] = PARTITION_SPLIT;
160*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[2] = PARTITION_NONE;
161*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[3] = PARTITION_SPLIT;
162*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[4] = PARTITION_NONE;
163*77c1e3ccSAndroid Build Coastguard Worker // The rest blocks.
164*77c1e3ccSAndroid Build Coastguard Worker for (int i = 5; i < num_blocks - num_4x4_blocks; ++i) {
165*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[0] = PARTITION_SPLIT;
166*77c1e3ccSAndroid Build Coastguard Worker }
167*77c1e3ccSAndroid Build Coastguard Worker for (int i = num_blocks - num_4x4_blocks; i < num_blocks; ++i) {
168*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[i] = PARTITION_NONE;
169*77c1e3ccSAndroid Build Coastguard Worker }
170*77c1e3ccSAndroid Build Coastguard Worker } else {
171*77c1e3ccSAndroid Build Coastguard Worker // 64x64: 1 node
172*77c1e3ccSAndroid Build Coastguard Worker // 32x32: 4 nodes
173*77c1e3ccSAndroid Build Coastguard Worker // 16x16: 4 * 4 nodes
174*77c1e3ccSAndroid Build Coastguard Worker // 8x8: 4 * 4 * 4 nodes
175*77c1e3ccSAndroid Build Coastguard Worker // 4x4: 4 * 4 * 4 * 4 nodes
176*77c1e3ccSAndroid Build Coastguard Worker const int num_blocks = 1 + 4 + 4 * 4 + 4 * 4 * 4 + 4 * 4 * 4 * 4;
177*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_blocks = 4 * 4 * 4 * 4;
178*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->num_nodes = num_blocks;
179*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num_blocks - num_4x4_blocks; ++i) {
180*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[i] = PARTITION_SPLIT;
181*77c1e3ccSAndroid Build Coastguard Worker }
182*77c1e3ccSAndroid Build Coastguard Worker for (int i = num_blocks - num_4x4_blocks; i < num_blocks; ++i) {
183*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->partition_decision[i] = PARTITION_NONE;
184*77c1e3ccSAndroid Build Coastguard Worker }
185*77c1e3ccSAndroid Build Coastguard Worker }
186*77c1e3ccSAndroid Build Coastguard Worker
187*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_OK;
188*77c1e3ccSAndroid Build Coastguard Worker }
189*77c1e3ccSAndroid Build Coastguard Worker
ext_part_get_partition_decision_recursive(aom_ext_part_model_t ext_part_model,aom_partition_decision_t * ext_part_decision)190*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_get_partition_decision_recursive(
191*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t ext_part_model,
192*77c1e3ccSAndroid Build Coastguard Worker aom_partition_decision_t *ext_part_decision) {
193*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->current_decision = PARTITION_NONE;
194*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->is_final_decision = 1;
195*77c1e3ccSAndroid Build Coastguard Worker ToyModel *toy_model = static_cast<ToyModel *>(ext_part_model);
196*77c1e3ccSAndroid Build Coastguard Worker // Note: super block size is fixed to BLOCK_64X64 for the
197*77c1e3ccSAndroid Build Coastguard Worker // input video. It is determined inside the encoder, see the
198*77c1e3ccSAndroid Build Coastguard Worker // check in "ext_part_create_model".
199*77c1e3ccSAndroid Build Coastguard Worker const int is_last_sb_col =
200*77c1e3ccSAndroid Build Coastguard Worker toy_model->mi_col * 4 + 64 > toy_model->frame_width;
201*77c1e3ccSAndroid Build Coastguard Worker const int is_last_sb_row =
202*77c1e3ccSAndroid Build Coastguard Worker toy_model->mi_row * 4 + 64 > toy_model->frame_height;
203*77c1e3ccSAndroid Build Coastguard Worker if (is_last_sb_row && is_last_sb_col) {
204*77c1e3ccSAndroid Build Coastguard Worker if (block_size_wide[toy_model->block_size] == 64) {
205*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->current_decision = PARTITION_SPLIT;
206*77c1e3ccSAndroid Build Coastguard Worker } else {
207*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->current_decision = PARTITION_NONE;
208*77c1e3ccSAndroid Build Coastguard Worker }
209*77c1e3ccSAndroid Build Coastguard Worker } else if (is_last_sb_row) {
210*77c1e3ccSAndroid Build Coastguard Worker if (block_size_wide[toy_model->block_size] == 64) {
211*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->current_decision = PARTITION_SPLIT;
212*77c1e3ccSAndroid Build Coastguard Worker } else {
213*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->current_decision = PARTITION_NONE;
214*77c1e3ccSAndroid Build Coastguard Worker }
215*77c1e3ccSAndroid Build Coastguard Worker } else if (is_last_sb_col) {
216*77c1e3ccSAndroid Build Coastguard Worker if (block_size_wide[toy_model->block_size] == 64) {
217*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->current_decision = PARTITION_SPLIT;
218*77c1e3ccSAndroid Build Coastguard Worker } else {
219*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->current_decision = PARTITION_NONE;
220*77c1e3ccSAndroid Build Coastguard Worker }
221*77c1e3ccSAndroid Build Coastguard Worker } else {
222*77c1e3ccSAndroid Build Coastguard Worker ext_part_decision->current_decision = PARTITION_NONE;
223*77c1e3ccSAndroid Build Coastguard Worker }
224*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_OK;
225*77c1e3ccSAndroid Build Coastguard Worker }
226*77c1e3ccSAndroid Build Coastguard Worker
ext_part_send_partition_stats(aom_ext_part_model_t ext_part_model,const aom_partition_stats_t * ext_part_stats)227*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_send_partition_stats(
228*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t ext_part_model,
229*77c1e3ccSAndroid Build Coastguard Worker const aom_partition_stats_t *ext_part_stats) {
230*77c1e3ccSAndroid Build Coastguard Worker (void)ext_part_model;
231*77c1e3ccSAndroid Build Coastguard Worker (void)ext_part_stats;
232*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_OK;
233*77c1e3ccSAndroid Build Coastguard Worker }
234*77c1e3ccSAndroid Build Coastguard Worker
ext_part_delete_model(aom_ext_part_model_t ext_part_model)235*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_delete_model(
236*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t ext_part_model) {
237*77c1e3ccSAndroid Build Coastguard Worker ToyModel *toy_model = static_cast<ToyModel *>(ext_part_model);
238*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(toy_model->data->version, kVersion);
239*77c1e3ccSAndroid Build Coastguard Worker delete toy_model;
240*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_OK;
241*77c1e3ccSAndroid Build Coastguard Worker }
242*77c1e3ccSAndroid Build Coastguard Worker
243*77c1e3ccSAndroid Build Coastguard Worker class ExternalPartitionTestAPI
244*77c1e3ccSAndroid Build Coastguard Worker : public ::libaom_test::CodecTestWith2Params<libaom_test::TestMode, int>,
245*77c1e3ccSAndroid Build Coastguard Worker public ::libaom_test::EncoderTest {
246*77c1e3ccSAndroid Build Coastguard Worker protected:
ExternalPartitionTestAPI()247*77c1e3ccSAndroid Build Coastguard Worker ExternalPartitionTestAPI()
248*77c1e3ccSAndroid Build Coastguard Worker : EncoderTest(GET_PARAM(0)), encoding_mode_(GET_PARAM(1)),
249*77c1e3ccSAndroid Build Coastguard Worker cpu_used_(GET_PARAM(2)), psnr_(0.0), nframes_(0) {}
~ExternalPartitionTestAPI()250*77c1e3ccSAndroid Build Coastguard Worker ~ExternalPartitionTestAPI() override {}
251*77c1e3ccSAndroid Build Coastguard Worker
SetUp()252*77c1e3ccSAndroid Build Coastguard Worker void SetUp() override {
253*77c1e3ccSAndroid Build Coastguard Worker InitializeConfig(encoding_mode_);
254*77c1e3ccSAndroid Build Coastguard Worker const aom_rational timebase = { 1, 30 };
255*77c1e3ccSAndroid Build Coastguard Worker cfg_.g_timebase = timebase;
256*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_end_usage = AOM_VBR;
257*77c1e3ccSAndroid Build Coastguard Worker cfg_.g_threads = 1;
258*77c1e3ccSAndroid Build Coastguard Worker cfg_.g_lag_in_frames = 4;
259*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_target_bitrate = 400;
260*77c1e3ccSAndroid Build Coastguard Worker init_flags_ = AOM_CODEC_USE_PSNR;
261*77c1e3ccSAndroid Build Coastguard Worker }
262*77c1e3ccSAndroid Build Coastguard Worker
DoDecode() const263*77c1e3ccSAndroid Build Coastguard Worker bool DoDecode() const override { return false; }
264*77c1e3ccSAndroid Build Coastguard Worker
BeginPassHook(unsigned int)265*77c1e3ccSAndroid Build Coastguard Worker void BeginPassHook(unsigned int) override {
266*77c1e3ccSAndroid Build Coastguard Worker psnr_ = 0.0;
267*77c1e3ccSAndroid Build Coastguard Worker nframes_ = 0;
268*77c1e3ccSAndroid Build Coastguard Worker }
269*77c1e3ccSAndroid Build Coastguard Worker
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)270*77c1e3ccSAndroid Build Coastguard Worker void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) override {
271*77c1e3ccSAndroid Build Coastguard Worker psnr_ += pkt->data.psnr.psnr[0];
272*77c1e3ccSAndroid Build Coastguard Worker nframes_++;
273*77c1e3ccSAndroid Build Coastguard Worker }
274*77c1e3ccSAndroid Build Coastguard Worker
GetAveragePsnr() const275*77c1e3ccSAndroid Build Coastguard Worker double GetAveragePsnr() const {
276*77c1e3ccSAndroid Build Coastguard Worker if (nframes_) return psnr_ / nframes_;
277*77c1e3ccSAndroid Build Coastguard Worker return 0.0;
278*77c1e3ccSAndroid Build Coastguard Worker }
279*77c1e3ccSAndroid Build Coastguard Worker
SetExternalPartition(bool use_external_partition)280*77c1e3ccSAndroid Build Coastguard Worker void SetExternalPartition(bool use_external_partition) {
281*77c1e3ccSAndroid Build Coastguard Worker use_external_partition_ = use_external_partition;
282*77c1e3ccSAndroid Build Coastguard Worker }
283*77c1e3ccSAndroid Build Coastguard Worker
SetPartitionControlMode(int mode)284*77c1e3ccSAndroid Build Coastguard Worker void SetPartitionControlMode(int mode) { partition_control_mode_ = mode; }
285*77c1e3ccSAndroid Build Coastguard Worker
SetDecisionMode(aom_ext_part_decision_mode_t mode)286*77c1e3ccSAndroid Build Coastguard Worker void SetDecisionMode(aom_ext_part_decision_mode_t mode) {
287*77c1e3ccSAndroid Build Coastguard Worker decision_mode_ = mode;
288*77c1e3ccSAndroid Build Coastguard Worker }
289*77c1e3ccSAndroid Build Coastguard Worker
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)290*77c1e3ccSAndroid Build Coastguard Worker void PreEncodeFrameHook(::libaom_test::VideoSource *video,
291*77c1e3ccSAndroid Build Coastguard Worker ::libaom_test::Encoder *encoder) override {
292*77c1e3ccSAndroid Build Coastguard Worker if (video->frame() == 0) {
293*77c1e3ccSAndroid Build Coastguard Worker if (decision_mode_ == AOM_EXT_PART_WHOLE_TREE) {
294*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_funcs_t ext_part_funcs;
295*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.priv = reinterpret_cast<void *>(&test_data_);
296*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.decision_mode = AOM_EXT_PART_WHOLE_TREE;
297*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.create_model = ext_part_create_model;
298*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.send_features = ext_part_send_features;
299*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.get_partition_decision =
300*77c1e3ccSAndroid Build Coastguard Worker ext_part_get_partition_decision_whole_tree;
301*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.send_partition_stats = ext_part_send_partition_stats;
302*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.delete_model = ext_part_delete_model;
303*77c1e3ccSAndroid Build Coastguard Worker
304*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AOME_SET_CPUUSED, cpu_used_);
305*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AOME_SET_ENABLEAUTOALTREF, 1);
306*77c1e3ccSAndroid Build Coastguard Worker if (use_external_partition_) {
307*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_EXTERNAL_PARTITION, &ext_part_funcs);
308*77c1e3ccSAndroid Build Coastguard Worker }
309*77c1e3ccSAndroid Build Coastguard Worker if (partition_control_mode_ == -1) {
310*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MAX_PARTITION_SIZE, 128);
311*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MIN_PARTITION_SIZE, 4);
312*77c1e3ccSAndroid Build Coastguard Worker } else {
313*77c1e3ccSAndroid Build Coastguard Worker switch (partition_control_mode_) {
314*77c1e3ccSAndroid Build Coastguard Worker case 1:
315*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MAX_PARTITION_SIZE, 64);
316*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MIN_PARTITION_SIZE, 64);
317*77c1e3ccSAndroid Build Coastguard Worker break;
318*77c1e3ccSAndroid Build Coastguard Worker case 2:
319*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MAX_PARTITION_SIZE, 4);
320*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MIN_PARTITION_SIZE, 4);
321*77c1e3ccSAndroid Build Coastguard Worker break;
322*77c1e3ccSAndroid Build Coastguard Worker default: assert(0 && "Invalid partition control mode."); break;
323*77c1e3ccSAndroid Build Coastguard Worker }
324*77c1e3ccSAndroid Build Coastguard Worker }
325*77c1e3ccSAndroid Build Coastguard Worker } else if (decision_mode_ == AOM_EXT_PART_RECURSIVE) {
326*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_funcs_t ext_part_funcs;
327*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.priv = reinterpret_cast<void *>(&test_data_);
328*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.decision_mode = AOM_EXT_PART_RECURSIVE;
329*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.create_model = ext_part_create_model;
330*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.send_features = ext_part_send_features;
331*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.get_partition_decision =
332*77c1e3ccSAndroid Build Coastguard Worker ext_part_get_partition_decision_recursive;
333*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.send_partition_stats = ext_part_send_partition_stats;
334*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.delete_model = ext_part_delete_model;
335*77c1e3ccSAndroid Build Coastguard Worker
336*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AOME_SET_CPUUSED, cpu_used_);
337*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AOME_SET_ENABLEAUTOALTREF, 1);
338*77c1e3ccSAndroid Build Coastguard Worker if (use_external_partition_) {
339*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_EXTERNAL_PARTITION, &ext_part_funcs);
340*77c1e3ccSAndroid Build Coastguard Worker }
341*77c1e3ccSAndroid Build Coastguard Worker if (partition_control_mode_ == -1) {
342*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MAX_PARTITION_SIZE, 128);
343*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MIN_PARTITION_SIZE, 4);
344*77c1e3ccSAndroid Build Coastguard Worker } else {
345*77c1e3ccSAndroid Build Coastguard Worker switch (partition_control_mode_) {
346*77c1e3ccSAndroid Build Coastguard Worker case 1:
347*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MAX_PARTITION_SIZE, 64);
348*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MIN_PARTITION_SIZE, 64);
349*77c1e3ccSAndroid Build Coastguard Worker break;
350*77c1e3ccSAndroid Build Coastguard Worker case 2:
351*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MAX_PARTITION_SIZE, 4);
352*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_MIN_PARTITION_SIZE, 4);
353*77c1e3ccSAndroid Build Coastguard Worker break;
354*77c1e3ccSAndroid Build Coastguard Worker default: assert(0 && "Invalid partition control mode."); break;
355*77c1e3ccSAndroid Build Coastguard Worker }
356*77c1e3ccSAndroid Build Coastguard Worker }
357*77c1e3ccSAndroid Build Coastguard Worker } else {
358*77c1e3ccSAndroid Build Coastguard Worker assert(0 && "Invalid decision mode.");
359*77c1e3ccSAndroid Build Coastguard Worker }
360*77c1e3ccSAndroid Build Coastguard Worker }
361*77c1e3ccSAndroid Build Coastguard Worker }
362*77c1e3ccSAndroid Build Coastguard Worker
363*77c1e3ccSAndroid Build Coastguard Worker private:
364*77c1e3ccSAndroid Build Coastguard Worker libaom_test::TestMode encoding_mode_;
365*77c1e3ccSAndroid Build Coastguard Worker int cpu_used_;
366*77c1e3ccSAndroid Build Coastguard Worker double psnr_;
367*77c1e3ccSAndroid Build Coastguard Worker unsigned int nframes_;
368*77c1e3ccSAndroid Build Coastguard Worker bool use_external_partition_ = false;
369*77c1e3ccSAndroid Build Coastguard Worker TestData test_data_;
370*77c1e3ccSAndroid Build Coastguard Worker int partition_control_mode_ = -1;
371*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_decision_mode_t decision_mode_;
372*77c1e3ccSAndroid Build Coastguard Worker };
373*77c1e3ccSAndroid Build Coastguard Worker
374*77c1e3ccSAndroid Build Coastguard Worker // Encode twice and expect the same psnr value.
375*77c1e3ccSAndroid Build Coastguard Worker // The first run is a normal encoding run with restricted partition types,
376*77c1e3ccSAndroid Build Coastguard Worker // i.e., we use control flags to force the encoder to encode with the
377*77c1e3ccSAndroid Build Coastguard Worker // 4x4 block size.
378*77c1e3ccSAndroid Build Coastguard Worker // The second run is to get partition decisions from a toy model that we
379*77c1e3ccSAndroid Build Coastguard Worker // built, which will asks the encoder to encode with the 4x4 blocks.
380*77c1e3ccSAndroid Build Coastguard Worker // We expect the encoding results are the same.
TEST_P(ExternalPartitionTestAPI,WholePartitionTree4x4Block)381*77c1e3ccSAndroid Build Coastguard Worker TEST_P(ExternalPartitionTestAPI, WholePartitionTree4x4Block) {
382*77c1e3ccSAndroid Build Coastguard Worker ::libaom_test::Y4mVideoSource video("paris_352_288_30.y4m", 0, kFrameNum);
383*77c1e3ccSAndroid Build Coastguard Worker SetExternalPartition(false);
384*77c1e3ccSAndroid Build Coastguard Worker SetPartitionControlMode(2);
385*77c1e3ccSAndroid Build Coastguard Worker SetDecisionMode(AOM_EXT_PART_WHOLE_TREE);
386*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
387*77c1e3ccSAndroid Build Coastguard Worker const double psnr = GetAveragePsnr();
388*77c1e3ccSAndroid Build Coastguard Worker
389*77c1e3ccSAndroid Build Coastguard Worker SetExternalPartition(true);
390*77c1e3ccSAndroid Build Coastguard Worker SetPartitionControlMode(2);
391*77c1e3ccSAndroid Build Coastguard Worker SetDecisionMode(AOM_EXT_PART_WHOLE_TREE);
392*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
393*77c1e3ccSAndroid Build Coastguard Worker const double psnr2 = GetAveragePsnr();
394*77c1e3ccSAndroid Build Coastguard Worker
395*77c1e3ccSAndroid Build Coastguard Worker EXPECT_DOUBLE_EQ(psnr, psnr2);
396*77c1e3ccSAndroid Build Coastguard Worker }
397*77c1e3ccSAndroid Build Coastguard Worker
TEST_P(ExternalPartitionTestAPI,RecursivePartition)398*77c1e3ccSAndroid Build Coastguard Worker TEST_P(ExternalPartitionTestAPI, RecursivePartition) {
399*77c1e3ccSAndroid Build Coastguard Worker ::libaom_test::Y4mVideoSource video("paris_352_288_30.y4m", 0, kFrameNum);
400*77c1e3ccSAndroid Build Coastguard Worker SetExternalPartition(false);
401*77c1e3ccSAndroid Build Coastguard Worker SetPartitionControlMode(1);
402*77c1e3ccSAndroid Build Coastguard Worker SetDecisionMode(AOM_EXT_PART_RECURSIVE);
403*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
404*77c1e3ccSAndroid Build Coastguard Worker const double psnr = GetAveragePsnr();
405*77c1e3ccSAndroid Build Coastguard Worker
406*77c1e3ccSAndroid Build Coastguard Worker SetExternalPartition(true);
407*77c1e3ccSAndroid Build Coastguard Worker SetPartitionControlMode(1);
408*77c1e3ccSAndroid Build Coastguard Worker SetDecisionMode(AOM_EXT_PART_RECURSIVE);
409*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
410*77c1e3ccSAndroid Build Coastguard Worker const double psnr2 = GetAveragePsnr();
411*77c1e3ccSAndroid Build Coastguard Worker
412*77c1e3ccSAndroid Build Coastguard Worker const double psnr_thresh = 0.02;
413*77c1e3ccSAndroid Build Coastguard Worker EXPECT_NEAR(psnr, psnr2, psnr_thresh);
414*77c1e3ccSAndroid Build Coastguard Worker }
415*77c1e3ccSAndroid Build Coastguard Worker
416*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(ExternalPartitionTestAPI,
417*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(::libaom_test::kTwoPassGood),
418*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(4)); // cpu_used
419*77c1e3ccSAndroid Build Coastguard Worker
420*77c1e3ccSAndroid Build Coastguard Worker #else // !CONFIG_PARTITION_SEARCH_ORDER
421*77c1e3ccSAndroid Build Coastguard Worker // Feature files written during encoding, as defined in partition_strategy.c.
422*77c1e3ccSAndroid Build Coastguard Worker std::string feature_file_names[] = {
423*77c1e3ccSAndroid Build Coastguard Worker "feature_before_partition_none",
424*77c1e3ccSAndroid Build Coastguard Worker "feature_before_partition_none_prune_rect",
425*77c1e3ccSAndroid Build Coastguard Worker "feature_after_partition_none_prune",
426*77c1e3ccSAndroid Build Coastguard Worker "feature_after_partition_none_terminate",
427*77c1e3ccSAndroid Build Coastguard Worker "feature_after_partition_split_terminate",
428*77c1e3ccSAndroid Build Coastguard Worker "feature_after_partition_split_prune_rect",
429*77c1e3ccSAndroid Build Coastguard Worker "feature_after_partition_rect",
430*77c1e3ccSAndroid Build Coastguard Worker "feature_after_partition_ab",
431*77c1e3ccSAndroid Build Coastguard Worker };
432*77c1e3ccSAndroid Build Coastguard Worker
433*77c1e3ccSAndroid Build Coastguard Worker // Files written here in the test, where the feature data is received
434*77c1e3ccSAndroid Build Coastguard Worker // from the API.
435*77c1e3ccSAndroid Build Coastguard Worker std::string test_feature_file_names[] = {
436*77c1e3ccSAndroid Build Coastguard Worker "test_feature_before_partition_none",
437*77c1e3ccSAndroid Build Coastguard Worker "test_feature_before_partition_none_prune_rect",
438*77c1e3ccSAndroid Build Coastguard Worker "test_feature_after_partition_none_prune",
439*77c1e3ccSAndroid Build Coastguard Worker "test_feature_after_partition_none_terminate",
440*77c1e3ccSAndroid Build Coastguard Worker "test_feature_after_partition_split_terminate",
441*77c1e3ccSAndroid Build Coastguard Worker "test_feature_after_partition_split_prune_rect",
442*77c1e3ccSAndroid Build Coastguard Worker "test_feature_after_partition_rect",
443*77c1e3ccSAndroid Build Coastguard Worker "test_feature_after_partition_ab",
444*77c1e3ccSAndroid Build Coastguard Worker };
445*77c1e3ccSAndroid Build Coastguard Worker
write_features_to_file(const float * features,const int feature_size,const int id)446*77c1e3ccSAndroid Build Coastguard Worker static void write_features_to_file(const float *features,
447*77c1e3ccSAndroid Build Coastguard Worker const int feature_size, const int id) {
448*77c1e3ccSAndroid Build Coastguard Worker if (!WRITE_FEATURE_TO_FILE) return;
449*77c1e3ccSAndroid Build Coastguard Worker char filename[256];
450*77c1e3ccSAndroid Build Coastguard Worker snprintf(filename, sizeof(filename), "%s",
451*77c1e3ccSAndroid Build Coastguard Worker test_feature_file_names[id].c_str());
452*77c1e3ccSAndroid Build Coastguard Worker FILE *pfile = fopen(filename, "a");
453*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NE(pfile, nullptr);
454*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < feature_size; ++i) {
455*77c1e3ccSAndroid Build Coastguard Worker fprintf(pfile, "%.6f", features[i]);
456*77c1e3ccSAndroid Build Coastguard Worker if (i < feature_size - 1) fprintf(pfile, ",");
457*77c1e3ccSAndroid Build Coastguard Worker }
458*77c1e3ccSAndroid Build Coastguard Worker fprintf(pfile, "\n");
459*77c1e3ccSAndroid Build Coastguard Worker fclose(pfile);
460*77c1e3ccSAndroid Build Coastguard Worker }
461*77c1e3ccSAndroid Build Coastguard Worker
ext_part_create_model(void * priv,const aom_ext_part_config_t * part_config,aom_ext_part_model_t * ext_part_model)462*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_create_model(
463*77c1e3ccSAndroid Build Coastguard Worker void *priv, const aom_ext_part_config_t *part_config,
464*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t *ext_part_model) {
465*77c1e3ccSAndroid Build Coastguard Worker TestData *received_data = reinterpret_cast<TestData *>(priv);
466*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(received_data->version, kVersion);
467*77c1e3ccSAndroid Build Coastguard Worker ToyModel *toy_model = new (std::nothrow) ToyModel;
468*77c1e3ccSAndroid Build Coastguard Worker if (toy_model == nullptr) {
469*77c1e3ccSAndroid Build Coastguard Worker EXPECT_NE(toy_model, nullptr);
470*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_ERROR;
471*77c1e3ccSAndroid Build Coastguard Worker }
472*77c1e3ccSAndroid Build Coastguard Worker toy_model->data = received_data;
473*77c1e3ccSAndroid Build Coastguard Worker *ext_part_model = toy_model;
474*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(part_config->superblock_size, BLOCK_64X64);
475*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_OK;
476*77c1e3ccSAndroid Build Coastguard Worker }
477*77c1e3ccSAndroid Build Coastguard Worker
ext_part_create_model_test(void * priv,const aom_ext_part_config_t * part_config,aom_ext_part_model_t * ext_part_model)478*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_create_model_test(
479*77c1e3ccSAndroid Build Coastguard Worker void *priv, const aom_ext_part_config_t *part_config,
480*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t *ext_part_model) {
481*77c1e3ccSAndroid Build Coastguard Worker (void)priv;
482*77c1e3ccSAndroid Build Coastguard Worker (void)ext_part_model;
483*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(part_config->superblock_size, BLOCK_64X64);
484*77c1e3ccSAndroid Build Coastguard Worker // Return status indicates it's a encoder test. It lets the encoder
485*77c1e3ccSAndroid Build Coastguard Worker // set a flag and write partition features to text files.
486*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_TEST;
487*77c1e3ccSAndroid Build Coastguard Worker }
488*77c1e3ccSAndroid Build Coastguard Worker
ext_part_send_features(aom_ext_part_model_t ext_part_model,const aom_partition_features_t * part_features)489*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_send_features(
490*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t ext_part_model,
491*77c1e3ccSAndroid Build Coastguard Worker const aom_partition_features_t *part_features) {
492*77c1e3ccSAndroid Build Coastguard Worker (void)ext_part_model;
493*77c1e3ccSAndroid Build Coastguard Worker (void)part_features;
494*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_OK;
495*77c1e3ccSAndroid Build Coastguard Worker }
496*77c1e3ccSAndroid Build Coastguard Worker
ext_part_send_features_test(aom_ext_part_model_t ext_part_model,const aom_partition_features_t * part_features)497*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_send_features_test(
498*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t ext_part_model,
499*77c1e3ccSAndroid Build Coastguard Worker const aom_partition_features_t *part_features) {
500*77c1e3ccSAndroid Build Coastguard Worker (void)ext_part_model;
501*77c1e3ccSAndroid Build Coastguard Worker if (part_features->id == AOM_EXT_PART_FEATURE_BEFORE_NONE) {
502*77c1e3ccSAndroid Build Coastguard Worker write_features_to_file(part_features->before_part_none.f,
503*77c1e3ccSAndroid Build Coastguard Worker AOM_EXT_PART_SIZE_DIRECT_SPLIT, 0);
504*77c1e3ccSAndroid Build Coastguard Worker } else if (part_features->id == AOM_EXT_PART_FEATURE_BEFORE_NONE_PART2) {
505*77c1e3ccSAndroid Build Coastguard Worker write_features_to_file(part_features->before_part_none.f_part2,
506*77c1e3ccSAndroid Build Coastguard Worker AOM_EXT_PART_SIZE_PRUNE_PART, 1);
507*77c1e3ccSAndroid Build Coastguard Worker } else if (part_features->id == AOM_EXT_PART_FEATURE_AFTER_NONE) {
508*77c1e3ccSAndroid Build Coastguard Worker write_features_to_file(part_features->after_part_none.f,
509*77c1e3ccSAndroid Build Coastguard Worker AOM_EXT_PART_SIZE_PRUNE_NONE, 2);
510*77c1e3ccSAndroid Build Coastguard Worker } else if (part_features->id == AOM_EXT_PART_FEATURE_AFTER_NONE_PART2) {
511*77c1e3ccSAndroid Build Coastguard Worker write_features_to_file(part_features->after_part_none.f_terminate,
512*77c1e3ccSAndroid Build Coastguard Worker AOM_EXT_PART_SIZE_TERM_NONE, 3);
513*77c1e3ccSAndroid Build Coastguard Worker } else if (part_features->id == AOM_EXT_PART_FEATURE_AFTER_SPLIT) {
514*77c1e3ccSAndroid Build Coastguard Worker write_features_to_file(part_features->after_part_split.f_terminate,
515*77c1e3ccSAndroid Build Coastguard Worker AOM_EXT_PART_SIZE_TERM_SPLIT, 4);
516*77c1e3ccSAndroid Build Coastguard Worker } else if (part_features->id == AOM_EXT_PART_FEATURE_AFTER_SPLIT_PART2) {
517*77c1e3ccSAndroid Build Coastguard Worker write_features_to_file(part_features->after_part_split.f_prune_rect,
518*77c1e3ccSAndroid Build Coastguard Worker AOM_EXT_PART_SIZE_PRUNE_RECT, 5);
519*77c1e3ccSAndroid Build Coastguard Worker } else if (part_features->id == AOM_EXT_PART_FEATURE_AFTER_RECT) {
520*77c1e3ccSAndroid Build Coastguard Worker write_features_to_file(part_features->after_part_rect.f,
521*77c1e3ccSAndroid Build Coastguard Worker AOM_EXT_PART_SIZE_PRUNE_AB, 6);
522*77c1e3ccSAndroid Build Coastguard Worker } else if (part_features->id == AOM_EXT_PART_FEATURE_AFTER_AB) {
523*77c1e3ccSAndroid Build Coastguard Worker write_features_to_file(part_features->after_part_ab.f,
524*77c1e3ccSAndroid Build Coastguard Worker AOM_EXT_PART_SIZE_PRUNE_4_WAY, 7);
525*77c1e3ccSAndroid Build Coastguard Worker }
526*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_TEST;
527*77c1e3ccSAndroid Build Coastguard Worker }
528*77c1e3ccSAndroid Build Coastguard Worker
ext_part_get_partition_decision(aom_ext_part_model_t ext_part_model,aom_partition_decision_t * ext_part_decision)529*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_get_partition_decision(
530*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t ext_part_model,
531*77c1e3ccSAndroid Build Coastguard Worker aom_partition_decision_t *ext_part_decision) {
532*77c1e3ccSAndroid Build Coastguard Worker (void)ext_part_model;
533*77c1e3ccSAndroid Build Coastguard Worker (void)ext_part_decision;
534*77c1e3ccSAndroid Build Coastguard Worker // Return an invalid decision such that the encoder doesn't take any
535*77c1e3ccSAndroid Build Coastguard Worker // partition decision from the ml model.
536*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_ERROR;
537*77c1e3ccSAndroid Build Coastguard Worker }
538*77c1e3ccSAndroid Build Coastguard Worker
ext_part_send_partition_stats(aom_ext_part_model_t ext_part_model,const aom_partition_stats_t * ext_part_stats)539*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_send_partition_stats(
540*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t ext_part_model,
541*77c1e3ccSAndroid Build Coastguard Worker const aom_partition_stats_t *ext_part_stats) {
542*77c1e3ccSAndroid Build Coastguard Worker (void)ext_part_model;
543*77c1e3ccSAndroid Build Coastguard Worker (void)ext_part_stats;
544*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_OK;
545*77c1e3ccSAndroid Build Coastguard Worker }
546*77c1e3ccSAndroid Build Coastguard Worker
ext_part_delete_model(aom_ext_part_model_t ext_part_model)547*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_status_t ext_part_delete_model(
548*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_model_t ext_part_model) {
549*77c1e3ccSAndroid Build Coastguard Worker ToyModel *toy_model = static_cast<ToyModel *>(ext_part_model);
550*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(toy_model->data->version, kVersion);
551*77c1e3ccSAndroid Build Coastguard Worker delete toy_model;
552*77c1e3ccSAndroid Build Coastguard Worker return AOM_EXT_PART_OK;
553*77c1e3ccSAndroid Build Coastguard Worker }
554*77c1e3ccSAndroid Build Coastguard Worker
555*77c1e3ccSAndroid Build Coastguard Worker class ExternalPartitionTestDfsAPI
556*77c1e3ccSAndroid Build Coastguard Worker : public ::libaom_test::CodecTestWith2Params<libaom_test::TestMode, int>,
557*77c1e3ccSAndroid Build Coastguard Worker public ::libaom_test::EncoderTest {
558*77c1e3ccSAndroid Build Coastguard Worker protected:
ExternalPartitionTestDfsAPI()559*77c1e3ccSAndroid Build Coastguard Worker ExternalPartitionTestDfsAPI()
560*77c1e3ccSAndroid Build Coastguard Worker : EncoderTest(GET_PARAM(0)), encoding_mode_(GET_PARAM(1)),
561*77c1e3ccSAndroid Build Coastguard Worker cpu_used_(GET_PARAM(2)), psnr_(0.0), nframes_(0) {}
562*77c1e3ccSAndroid Build Coastguard Worker ~ExternalPartitionTestDfsAPI() override = default;
563*77c1e3ccSAndroid Build Coastguard Worker
SetUp()564*77c1e3ccSAndroid Build Coastguard Worker void SetUp() override {
565*77c1e3ccSAndroid Build Coastguard Worker InitializeConfig(encoding_mode_);
566*77c1e3ccSAndroid Build Coastguard Worker const aom_rational timebase = { 1, 30 };
567*77c1e3ccSAndroid Build Coastguard Worker cfg_.g_timebase = timebase;
568*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_end_usage = AOM_VBR;
569*77c1e3ccSAndroid Build Coastguard Worker cfg_.g_threads = 1;
570*77c1e3ccSAndroid Build Coastguard Worker cfg_.g_lag_in_frames = 4;
571*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_target_bitrate = 400;
572*77c1e3ccSAndroid Build Coastguard Worker init_flags_ = AOM_CODEC_USE_PSNR;
573*77c1e3ccSAndroid Build Coastguard Worker }
574*77c1e3ccSAndroid Build Coastguard Worker
DoDecode() const575*77c1e3ccSAndroid Build Coastguard Worker bool DoDecode() const override { return false; }
576*77c1e3ccSAndroid Build Coastguard Worker
BeginPassHook(unsigned int)577*77c1e3ccSAndroid Build Coastguard Worker void BeginPassHook(unsigned int) override {
578*77c1e3ccSAndroid Build Coastguard Worker psnr_ = 0.0;
579*77c1e3ccSAndroid Build Coastguard Worker nframes_ = 0;
580*77c1e3ccSAndroid Build Coastguard Worker }
581*77c1e3ccSAndroid Build Coastguard Worker
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)582*77c1e3ccSAndroid Build Coastguard Worker void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) override {
583*77c1e3ccSAndroid Build Coastguard Worker psnr_ += pkt->data.psnr.psnr[0];
584*77c1e3ccSAndroid Build Coastguard Worker nframes_++;
585*77c1e3ccSAndroid Build Coastguard Worker }
586*77c1e3ccSAndroid Build Coastguard Worker
GetAveragePsnr() const587*77c1e3ccSAndroid Build Coastguard Worker double GetAveragePsnr() const {
588*77c1e3ccSAndroid Build Coastguard Worker if (nframes_) return psnr_ / nframes_;
589*77c1e3ccSAndroid Build Coastguard Worker return 0.0;
590*77c1e3ccSAndroid Build Coastguard Worker }
591*77c1e3ccSAndroid Build Coastguard Worker
SetExternalPartition(bool use_external_partition)592*77c1e3ccSAndroid Build Coastguard Worker void SetExternalPartition(bool use_external_partition) {
593*77c1e3ccSAndroid Build Coastguard Worker use_external_partition_ = use_external_partition;
594*77c1e3ccSAndroid Build Coastguard Worker }
595*77c1e3ccSAndroid Build Coastguard Worker
SetTestSendFeatures(int test_send_features)596*77c1e3ccSAndroid Build Coastguard Worker void SetTestSendFeatures(int test_send_features) {
597*77c1e3ccSAndroid Build Coastguard Worker test_send_features_ = test_send_features;
598*77c1e3ccSAndroid Build Coastguard Worker }
599*77c1e3ccSAndroid Build Coastguard Worker
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)600*77c1e3ccSAndroid Build Coastguard Worker void PreEncodeFrameHook(::libaom_test::VideoSource *video,
601*77c1e3ccSAndroid Build Coastguard Worker ::libaom_test::Encoder *encoder) override {
602*77c1e3ccSAndroid Build Coastguard Worker if (video->frame() == 0) {
603*77c1e3ccSAndroid Build Coastguard Worker aom_ext_part_funcs_t ext_part_funcs;
604*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.priv = reinterpret_cast<void *>(&test_data_);
605*77c1e3ccSAndroid Build Coastguard Worker if (use_external_partition_) {
606*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.create_model = ext_part_create_model;
607*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.send_features = ext_part_send_features;
608*77c1e3ccSAndroid Build Coastguard Worker }
609*77c1e3ccSAndroid Build Coastguard Worker if (test_send_features_ == 1) {
610*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.create_model = ext_part_create_model;
611*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.send_features = ext_part_send_features_test;
612*77c1e3ccSAndroid Build Coastguard Worker } else if (test_send_features_ == 0) {
613*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.create_model = ext_part_create_model_test;
614*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.send_features = ext_part_send_features;
615*77c1e3ccSAndroid Build Coastguard Worker }
616*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.get_partition_decision = ext_part_get_partition_decision;
617*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.send_partition_stats = ext_part_send_partition_stats;
618*77c1e3ccSAndroid Build Coastguard Worker ext_part_funcs.delete_model = ext_part_delete_model;
619*77c1e3ccSAndroid Build Coastguard Worker
620*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AOME_SET_CPUUSED, cpu_used_);
621*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AOME_SET_ENABLEAUTOALTREF, 1);
622*77c1e3ccSAndroid Build Coastguard Worker if (use_external_partition_) {
623*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_EXTERNAL_PARTITION, &ext_part_funcs);
624*77c1e3ccSAndroid Build Coastguard Worker }
625*77c1e3ccSAndroid Build Coastguard Worker }
626*77c1e3ccSAndroid Build Coastguard Worker }
627*77c1e3ccSAndroid Build Coastguard Worker
628*77c1e3ccSAndroid Build Coastguard Worker private:
629*77c1e3ccSAndroid Build Coastguard Worker libaom_test::TestMode encoding_mode_;
630*77c1e3ccSAndroid Build Coastguard Worker int cpu_used_;
631*77c1e3ccSAndroid Build Coastguard Worker double psnr_;
632*77c1e3ccSAndroid Build Coastguard Worker unsigned int nframes_;
633*77c1e3ccSAndroid Build Coastguard Worker bool use_external_partition_ = false;
634*77c1e3ccSAndroid Build Coastguard Worker int test_send_features_ = -1;
635*77c1e3ccSAndroid Build Coastguard Worker TestData test_data_;
636*77c1e3ccSAndroid Build Coastguard Worker };
637*77c1e3ccSAndroid Build Coastguard Worker
638*77c1e3ccSAndroid Build Coastguard Worker // Encode twice and expect the same psnr value.
639*77c1e3ccSAndroid Build Coastguard Worker // The first run is the baseline without external partition.
640*77c1e3ccSAndroid Build Coastguard Worker // The second run is to get partition decisions from the toy model we defined.
641*77c1e3ccSAndroid Build Coastguard Worker // Here, we let the partition decision return invalid for all stages.
642*77c1e3ccSAndroid Build Coastguard Worker // In this case, the external partition doesn't alter the original encoder
643*77c1e3ccSAndroid Build Coastguard Worker // behavior. So we expect the same encoding results.
TEST_P(ExternalPartitionTestDfsAPI,EncodeMatch)644*77c1e3ccSAndroid Build Coastguard Worker TEST_P(ExternalPartitionTestDfsAPI, EncodeMatch) {
645*77c1e3ccSAndroid Build Coastguard Worker ::libaom_test::Y4mVideoSource video("paris_352_288_30.y4m", 0, kFrameNum);
646*77c1e3ccSAndroid Build Coastguard Worker SetExternalPartition(false);
647*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
648*77c1e3ccSAndroid Build Coastguard Worker const double psnr = GetAveragePsnr();
649*77c1e3ccSAndroid Build Coastguard Worker
650*77c1e3ccSAndroid Build Coastguard Worker SetExternalPartition(true);
651*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
652*77c1e3ccSAndroid Build Coastguard Worker const double psnr2 = GetAveragePsnr();
653*77c1e3ccSAndroid Build Coastguard Worker
654*77c1e3ccSAndroid Build Coastguard Worker EXPECT_DOUBLE_EQ(psnr, psnr2);
655*77c1e3ccSAndroid Build Coastguard Worker }
656*77c1e3ccSAndroid Build Coastguard Worker
657*77c1e3ccSAndroid Build Coastguard Worker // Encode twice to compare generated feature files.
658*77c1e3ccSAndroid Build Coastguard Worker // The first run let the encoder write partition features to file.
659*77c1e3ccSAndroid Build Coastguard Worker // The second run calls send partition features function to send features to
660*77c1e3ccSAndroid Build Coastguard Worker // the external model, and we write them to file.
661*77c1e3ccSAndroid Build Coastguard Worker // The generated files should match each other.
TEST_P(ExternalPartitionTestDfsAPI,SendFeatures)662*77c1e3ccSAndroid Build Coastguard Worker TEST_P(ExternalPartitionTestDfsAPI, SendFeatures) {
663*77c1e3ccSAndroid Build Coastguard Worker ::libaom_test::Y4mVideoSource video("paris_352_288_30.y4m", 0, kFrameNum);
664*77c1e3ccSAndroid Build Coastguard Worker SetExternalPartition(true);
665*77c1e3ccSAndroid Build Coastguard Worker SetTestSendFeatures(0);
666*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
667*77c1e3ccSAndroid Build Coastguard Worker
668*77c1e3ccSAndroid Build Coastguard Worker SetExternalPartition(true);
669*77c1e3ccSAndroid Build Coastguard Worker SetTestSendFeatures(1);
670*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
671*77c1e3ccSAndroid Build Coastguard Worker if (!WRITE_FEATURE_TO_FILE) return;
672*77c1e3ccSAndroid Build Coastguard Worker
673*77c1e3ccSAndroid Build Coastguard Worker // Compare feature files by reading them into strings.
674*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < 8; ++i) {
675*77c1e3ccSAndroid Build Coastguard Worker std::ifstream base_file(feature_file_names[i]);
676*77c1e3ccSAndroid Build Coastguard Worker ASSERT_TRUE(base_file.good());
677*77c1e3ccSAndroid Build Coastguard Worker std::stringstream base_stream;
678*77c1e3ccSAndroid Build Coastguard Worker base_stream << base_file.rdbuf();
679*77c1e3ccSAndroid Build Coastguard Worker std::string base_string = base_stream.str();
680*77c1e3ccSAndroid Build Coastguard Worker
681*77c1e3ccSAndroid Build Coastguard Worker std::ifstream test_file(test_feature_file_names[i]);
682*77c1e3ccSAndroid Build Coastguard Worker ASSERT_TRUE(test_file.good());
683*77c1e3ccSAndroid Build Coastguard Worker std::stringstream test_stream;
684*77c1e3ccSAndroid Build Coastguard Worker test_stream << test_file.rdbuf();
685*77c1e3ccSAndroid Build Coastguard Worker std::string test_string = test_stream.str();
686*77c1e3ccSAndroid Build Coastguard Worker
687*77c1e3ccSAndroid Build Coastguard Worker EXPECT_STREQ(base_string.c_str(), test_string.c_str());
688*77c1e3ccSAndroid Build Coastguard Worker }
689*77c1e3ccSAndroid Build Coastguard Worker
690*77c1e3ccSAndroid Build Coastguard Worker // Remove files.
691*77c1e3ccSAndroid Build Coastguard Worker std::string command("rm -f feature_* test_feature_*");
692*77c1e3ccSAndroid Build Coastguard Worker system(command.c_str());
693*77c1e3ccSAndroid Build Coastguard Worker }
694*77c1e3ccSAndroid Build Coastguard Worker
695*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(ExternalPartitionTestDfsAPI,
696*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(::libaom_test::kTwoPassGood),
697*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(4)); // cpu_used
698*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_PARTITION_SEARCH_ORDER
699*77c1e3ccSAndroid Build Coastguard Worker
700*77c1e3ccSAndroid Build Coastguard Worker } // namespace
701*77c1e3ccSAndroid Build Coastguard Worker #endif // !CONFIG_REALTIME_ONLY
702*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_AV1_ENCODER
703