xref: /aosp_15_r20/external/libaom/av1/encoder/partition_strategy.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2019, 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 <float.h>
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
15*77c1e3ccSAndroid Build Coastguard Worker 
16*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodeframe_utils.h"
17*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_THREE_PASS
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/thirdpass.h"
19*77c1e3ccSAndroid Build Coastguard Worker #endif
20*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_dsp_rtcd.h"
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/enums.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconinter.h"
24*77c1e3ccSAndroid Build Coastguard Worker 
25*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
26*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/cnn.h"
27*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/partition_model_weights.h"
28*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/partition_cnn_weights.h"
29*77c1e3ccSAndroid Build Coastguard Worker #endif
30*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
31*77c1e3ccSAndroid Build Coastguard Worker 
32*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/motion_search_facade.h"
33*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/partition_strategy.h"
34*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/partition_search.h"
35*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/rdopt.h"
36*77c1e3ccSAndroid Build Coastguard Worker 
37*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
38*77c1e3ccSAndroid Build Coastguard Worker static inline void simple_motion_search_prune_part_features(
39*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, MACROBLOCK *x, SIMPLE_MOTION_DATA_TREE *sms_tree,
40*77c1e3ccSAndroid Build Coastguard Worker     int mi_row, int mi_col, BLOCK_SIZE bsize, float *features,
41*77c1e3ccSAndroid Build Coastguard Worker     int features_to_get);
42*77c1e3ccSAndroid Build Coastguard Worker 
43*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_before_none(
44*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *cpi, const float features_from_motion[FEATURE_SIZE_SMS_SPLIT],
45*77c1e3ccSAndroid Build Coastguard Worker     int *partition_none_allowed, int *partition_horz_allowed,
46*77c1e3ccSAndroid Build Coastguard Worker     int *partition_vert_allowed, int *do_rectangular_split,
47*77c1e3ccSAndroid Build Coastguard Worker     int *do_square_split);
48*77c1e3ccSAndroid Build Coastguard Worker 
49*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_before_none_part2(
50*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *cpi,
51*77c1e3ccSAndroid Build Coastguard Worker     const float features_from_motion[FEATURE_SIZE_SMS_PRUNE_PART],
52*77c1e3ccSAndroid Build Coastguard Worker     int *prune_horz, int *prune_vert);
53*77c1e3ccSAndroid Build Coastguard Worker 
54*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_after_none(
55*77c1e3ccSAndroid Build Coastguard Worker     ExtPartController *const ext_part_controller, const int is_intra_frame,
56*77c1e3ccSAndroid Build Coastguard Worker     const float *const features_after_none, int *do_square_split,
57*77c1e3ccSAndroid Build Coastguard Worker     int *do_rectangular_split);
58*77c1e3ccSAndroid Build Coastguard Worker 
59*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_after_none_part2(
60*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, const float *const features_terminate,
61*77c1e3ccSAndroid Build Coastguard Worker     int *terminate_partition_search);
62*77c1e3ccSAndroid Build Coastguard Worker 
63*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_after_split(
64*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, const float *const features_terminate,
65*77c1e3ccSAndroid Build Coastguard Worker     int *terminate_partition_search);
66*77c1e3ccSAndroid Build Coastguard Worker 
67*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_after_split_part2(
68*77c1e3ccSAndroid Build Coastguard Worker     ExtPartController *const ext_part_controller, const int is_intra_frame,
69*77c1e3ccSAndroid Build Coastguard Worker     const float *const features_prune, int *prune_rect_part_horz,
70*77c1e3ccSAndroid Build Coastguard Worker     int *prune_rect_part_vert);
71*77c1e3ccSAndroid Build Coastguard Worker 
72*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_after_rect(
73*77c1e3ccSAndroid Build Coastguard Worker     ExtPartController *const ext_part_controller, const int is_intra_frame,
74*77c1e3ccSAndroid Build Coastguard Worker     const float *const features_after_rect, int *horza_partition_allowed,
75*77c1e3ccSAndroid Build Coastguard Worker     int *horzb_partition_allowed, int *verta_partition_allowed,
76*77c1e3ccSAndroid Build Coastguard Worker     int *vertb_partition_allowed);
77*77c1e3ccSAndroid Build Coastguard Worker 
78*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_after_part_ab(
79*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, MACROBLOCK *const x, BLOCK_SIZE bsize, int part_ctx,
80*77c1e3ccSAndroid Build Coastguard Worker     int64_t best_rd, int64_t rect_part_rd[NUM_RECT_PARTS][SUB_PARTITIONS_RECT],
81*77c1e3ccSAndroid Build Coastguard Worker     int64_t split_rd[SUB_PARTITIONS_SPLIT], int *const partition_horz4_allowed,
82*77c1e3ccSAndroid Build Coastguard Worker     int *const partition_vert4_allowed, unsigned int pb_source_variance,
83*77c1e3ccSAndroid Build Coastguard Worker     int mi_row, int mi_col);
84*77c1e3ccSAndroid Build Coastguard Worker 
convert_bsize_to_idx(BLOCK_SIZE bsize)85*77c1e3ccSAndroid Build Coastguard Worker static inline int convert_bsize_to_idx(BLOCK_SIZE bsize) {
86*77c1e3ccSAndroid Build Coastguard Worker   switch (bsize) {
87*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_128X128: return 0;
88*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_64X64: return 1;
89*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_32X32: return 2;
90*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_16X16: return 3;
91*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_8X8: return 4;
92*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0 && "Invalid bsize"); return -1;
93*77c1e3ccSAndroid Build Coastguard Worker   }
94*77c1e3ccSAndroid Build Coastguard Worker }
95*77c1e3ccSAndroid Build Coastguard Worker 
get_feature_file_name(int id)96*77c1e3ccSAndroid Build Coastguard Worker static char *get_feature_file_name(int id) {
97*77c1e3ccSAndroid Build Coastguard Worker   static char *feature_file_names[] = {
98*77c1e3ccSAndroid Build Coastguard Worker     "feature_before_partition_none",
99*77c1e3ccSAndroid Build Coastguard Worker     "feature_before_partition_none_prune_rect",
100*77c1e3ccSAndroid Build Coastguard Worker     "feature_after_partition_none_prune",
101*77c1e3ccSAndroid Build Coastguard Worker     "feature_after_partition_none_terminate",
102*77c1e3ccSAndroid Build Coastguard Worker     "feature_after_partition_split_terminate",
103*77c1e3ccSAndroid Build Coastguard Worker     "feature_after_partition_split_prune_rect",
104*77c1e3ccSAndroid Build Coastguard Worker     "feature_after_partition_rect",
105*77c1e3ccSAndroid Build Coastguard Worker     "feature_after_partition_ab",
106*77c1e3ccSAndroid Build Coastguard Worker   };
107*77c1e3ccSAndroid Build Coastguard Worker 
108*77c1e3ccSAndroid Build Coastguard Worker   return feature_file_names[id];
109*77c1e3ccSAndroid Build Coastguard Worker }
110*77c1e3ccSAndroid Build Coastguard Worker 
write_features_to_file(const char * const path,const bool is_test_mode,const float * features,const int feature_size,const int id,const BLOCK_SIZE bsize,const int mi_row,const int mi_col)111*77c1e3ccSAndroid Build Coastguard Worker static void write_features_to_file(const char *const path,
112*77c1e3ccSAndroid Build Coastguard Worker                                    const bool is_test_mode,
113*77c1e3ccSAndroid Build Coastguard Worker                                    const float *features,
114*77c1e3ccSAndroid Build Coastguard Worker                                    const int feature_size, const int id,
115*77c1e3ccSAndroid Build Coastguard Worker                                    const BLOCK_SIZE bsize, const int mi_row,
116*77c1e3ccSAndroid Build Coastguard Worker                                    const int mi_col) {
117*77c1e3ccSAndroid Build Coastguard Worker   if (!WRITE_FEATURE_TO_FILE && !is_test_mode) return;
118*77c1e3ccSAndroid Build Coastguard Worker 
119*77c1e3ccSAndroid Build Coastguard Worker   char filename[256];
120*77c1e3ccSAndroid Build Coastguard Worker   snprintf(filename, sizeof(filename), "%s/%s", path,
121*77c1e3ccSAndroid Build Coastguard Worker            get_feature_file_name(id));
122*77c1e3ccSAndroid Build Coastguard Worker   FILE *pfile = fopen(filename, "a");
123*77c1e3ccSAndroid Build Coastguard Worker   if (pfile == NULL) return;
124*77c1e3ccSAndroid Build Coastguard Worker   if (!is_test_mode) {
125*77c1e3ccSAndroid Build Coastguard Worker     fprintf(pfile, "%d,%d,%d,%d,%d\n", id, (int)bsize, mi_row, mi_col,
126*77c1e3ccSAndroid Build Coastguard Worker             feature_size);
127*77c1e3ccSAndroid Build Coastguard Worker   }
128*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < feature_size; ++i) {
129*77c1e3ccSAndroid Build Coastguard Worker     fprintf(pfile, "%.6f", features[i]);
130*77c1e3ccSAndroid Build Coastguard Worker     if (i < feature_size - 1) fprintf(pfile, ",");
131*77c1e3ccSAndroid Build Coastguard Worker   }
132*77c1e3ccSAndroid Build Coastguard Worker   fprintf(pfile, "\n");
133*77c1e3ccSAndroid Build Coastguard Worker   fclose(pfile);
134*77c1e3ccSAndroid Build Coastguard Worker }
135*77c1e3ccSAndroid Build Coastguard Worker 
136*77c1e3ccSAndroid Build Coastguard Worker // TODO([email protected]): This is very much a work in progress. We still
137*77c1e3ccSAndroid Build Coastguard Worker // need to the following:
138*77c1e3ccSAndroid Build Coastguard Worker //   -- add support for hdres
139*77c1e3ccSAndroid Build Coastguard Worker //   -- add support for pruning rectangular partitions
140*77c1e3ccSAndroid Build Coastguard Worker //   -- use reconstructed pixels instead of source pixels for padding
141*77c1e3ccSAndroid Build Coastguard Worker //   -- use chroma pixels in addition to luma pixels
intra_mode_cnn_partition(const AV1_COMMON * const cm,MACROBLOCK * x,int quad_tree_idx,int intra_cnn_based_part_prune_level,PartitionSearchState * part_state)142*77c1e3ccSAndroid Build Coastguard Worker static void intra_mode_cnn_partition(const AV1_COMMON *const cm, MACROBLOCK *x,
143*77c1e3ccSAndroid Build Coastguard Worker                                      int quad_tree_idx,
144*77c1e3ccSAndroid Build Coastguard Worker                                      int intra_cnn_based_part_prune_level,
145*77c1e3ccSAndroid Build Coastguard Worker                                      PartitionSearchState *part_state) {
146*77c1e3ccSAndroid Build Coastguard Worker   assert(cm->seq_params->sb_size >= BLOCK_64X64 &&
147*77c1e3ccSAndroid Build Coastguard Worker          "Invalid sb_size for intra_cnn!");
148*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams *blk_params = &part_state->part_blk_params;
149*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params->bsize;
150*77c1e3ccSAndroid Build Coastguard Worker 
151*77c1e3ccSAndroid Build Coastguard Worker   const int bsize_idx = convert_bsize_to_idx(bsize);
152*77c1e3ccSAndroid Build Coastguard Worker 
153*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == BLOCK_128X128) {
154*77c1e3ccSAndroid Build Coastguard Worker     return;
155*77c1e3ccSAndroid Build Coastguard Worker   }
156*77c1e3ccSAndroid Build Coastguard Worker 
157*77c1e3ccSAndroid Build Coastguard Worker   PartitionSearchInfo *part_info = &x->part_search_info;
158*77c1e3ccSAndroid Build Coastguard Worker 
159*77c1e3ccSAndroid Build Coastguard Worker   // Precompute the CNN part and cache the result in MACROBLOCK
160*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == BLOCK_64X64 && !part_info->cnn_output_valid) {
161*77c1e3ccSAndroid Build Coastguard Worker     const CNN_CONFIG *cnn_config = &av1_intra_mode_cnn_partition_cnn_config;
162*77c1e3ccSAndroid Build Coastguard Worker 
163*77c1e3ccSAndroid Build Coastguard Worker     // Prepare the output
164*77c1e3ccSAndroid Build Coastguard Worker     const CNN_THREAD_DATA thread_data = { .num_workers = 1, .workers = NULL };
165*77c1e3ccSAndroid Build Coastguard Worker     const int num_outputs = 4;
166*77c1e3ccSAndroid Build Coastguard Worker     const int output_dims[4] = { 1, 2, 4, 8 };
167*77c1e3ccSAndroid Build Coastguard Worker     const int out_chs[4] = { CNN_BRANCH_0_OUT_CH, CNN_BRANCH_1_OUT_CH,
168*77c1e3ccSAndroid Build Coastguard Worker                              CNN_BRANCH_2_OUT_CH, CNN_BRANCH_3_OUT_CH };
169*77c1e3ccSAndroid Build Coastguard Worker     float *output_buffer[CNN_TOT_OUT_CH];
170*77c1e3ccSAndroid Build Coastguard Worker 
171*77c1e3ccSAndroid Build Coastguard Worker     float **cur_output_buf = output_buffer;
172*77c1e3ccSAndroid Build Coastguard Worker     float *curr_buf_ptr = part_info->cnn_buffer;
173*77c1e3ccSAndroid Build Coastguard Worker     for (int output_idx = 0; output_idx < num_outputs; output_idx++) {
174*77c1e3ccSAndroid Build Coastguard Worker       const int num_chs = out_chs[output_idx];
175*77c1e3ccSAndroid Build Coastguard Worker       const int ch_size = output_dims[output_idx] * output_dims[output_idx];
176*77c1e3ccSAndroid Build Coastguard Worker       for (int ch = 0; ch < num_chs; ch++) {
177*77c1e3ccSAndroid Build Coastguard Worker         cur_output_buf[ch] = curr_buf_ptr;
178*77c1e3ccSAndroid Build Coastguard Worker         curr_buf_ptr += ch_size;
179*77c1e3ccSAndroid Build Coastguard Worker       }
180*77c1e3ccSAndroid Build Coastguard Worker       cur_output_buf += num_chs;
181*77c1e3ccSAndroid Build Coastguard Worker     }
182*77c1e3ccSAndroid Build Coastguard Worker 
183*77c1e3ccSAndroid Build Coastguard Worker     CNN_MULTI_OUT output = {
184*77c1e3ccSAndroid Build Coastguard Worker       .num_outputs = 4,
185*77c1e3ccSAndroid Build Coastguard Worker       .output_channels = out_chs,
186*77c1e3ccSAndroid Build Coastguard Worker       .output_strides = output_dims,
187*77c1e3ccSAndroid Build Coastguard Worker       .output_buffer = output_buffer,
188*77c1e3ccSAndroid Build Coastguard Worker     };
189*77c1e3ccSAndroid Build Coastguard Worker 
190*77c1e3ccSAndroid Build Coastguard Worker     // Prepare the input
191*77c1e3ccSAndroid Build Coastguard Worker     const MACROBLOCKD *xd = &x->e_mbd;
192*77c1e3ccSAndroid Build Coastguard Worker     const int bit_depth = xd->bd;
193*77c1e3ccSAndroid Build Coastguard Worker     const int dc_q =
194*77c1e3ccSAndroid Build Coastguard Worker         av1_dc_quant_QTX(x->qindex, 0, bit_depth) >> (bit_depth - 8);
195*77c1e3ccSAndroid Build Coastguard Worker     part_info->log_q = log1pf((float)(dc_q * dc_q) / 256.0f);
196*77c1e3ccSAndroid Build Coastguard Worker     part_info->log_q =
197*77c1e3ccSAndroid Build Coastguard Worker         (part_info->log_q - av1_intra_mode_cnn_partition_mean[0]) /
198*77c1e3ccSAndroid Build Coastguard Worker         av1_intra_mode_cnn_partition_std[0];
199*77c1e3ccSAndroid Build Coastguard Worker 
200*77c1e3ccSAndroid Build Coastguard Worker     const int width = 65, height = 65,
201*77c1e3ccSAndroid Build Coastguard Worker               stride = x->plane[AOM_PLANE_Y].src.stride;
202*77c1e3ccSAndroid Build Coastguard Worker 
203*77c1e3ccSAndroid Build Coastguard Worker     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
204*77c1e3ccSAndroid Build Coastguard Worker       uint16_t *image[1] = {
205*77c1e3ccSAndroid Build Coastguard Worker         CONVERT_TO_SHORTPTR(x->plane[AOM_PLANE_Y].src.buf) - stride - 1
206*77c1e3ccSAndroid Build Coastguard Worker       };
207*77c1e3ccSAndroid Build Coastguard Worker 
208*77c1e3ccSAndroid Build Coastguard Worker       if (!av1_cnn_predict_img_multi_out_highbd(image, width, height, stride,
209*77c1e3ccSAndroid Build Coastguard Worker                                                 cnn_config, &thread_data,
210*77c1e3ccSAndroid Build Coastguard Worker                                                 bit_depth, &output)) {
211*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
212*77c1e3ccSAndroid Build Coastguard Worker                            "Error allocating CNN data");
213*77c1e3ccSAndroid Build Coastguard Worker         return;
214*77c1e3ccSAndroid Build Coastguard Worker       }
215*77c1e3ccSAndroid Build Coastguard Worker     } else {
216*77c1e3ccSAndroid Build Coastguard Worker       uint8_t *image[1] = { x->plane[AOM_PLANE_Y].src.buf - stride - 1 };
217*77c1e3ccSAndroid Build Coastguard Worker 
218*77c1e3ccSAndroid Build Coastguard Worker       if (!av1_cnn_predict_img_multi_out(image, width, height, stride,
219*77c1e3ccSAndroid Build Coastguard Worker                                          cnn_config, &thread_data, &output)) {
220*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
221*77c1e3ccSAndroid Build Coastguard Worker                            "Error allocating CNN data");
222*77c1e3ccSAndroid Build Coastguard Worker         return;
223*77c1e3ccSAndroid Build Coastguard Worker       }
224*77c1e3ccSAndroid Build Coastguard Worker     }
225*77c1e3ccSAndroid Build Coastguard Worker 
226*77c1e3ccSAndroid Build Coastguard Worker     part_info->cnn_output_valid = 1;
227*77c1e3ccSAndroid Build Coastguard Worker   }
228*77c1e3ccSAndroid Build Coastguard Worker 
229*77c1e3ccSAndroid Build Coastguard Worker   if (!part_info->cnn_output_valid) {
230*77c1e3ccSAndroid Build Coastguard Worker     return;
231*77c1e3ccSAndroid Build Coastguard Worker   }
232*77c1e3ccSAndroid Build Coastguard Worker 
233*77c1e3ccSAndroid Build Coastguard Worker   const NN_CONFIG *dnn_configs[5] = {
234*77c1e3ccSAndroid Build Coastguard Worker     NULL,
235*77c1e3ccSAndroid Build Coastguard Worker     &av1_intra_mode_cnn_partition_branch_0_dnn_config,
236*77c1e3ccSAndroid Build Coastguard Worker     &av1_intra_mode_cnn_partition_branch_1_dnn_config,
237*77c1e3ccSAndroid Build Coastguard Worker     &av1_intra_mode_cnn_partition_branch_2_dnn_config,
238*77c1e3ccSAndroid Build Coastguard Worker     &av1_intra_mode_cnn_partition_branch_3_dnn_config,
239*77c1e3ccSAndroid Build Coastguard Worker   };
240*77c1e3ccSAndroid Build Coastguard Worker 
241*77c1e3ccSAndroid Build Coastguard Worker   const NN_CONFIG *dnn_config = dnn_configs[bsize_idx];
242*77c1e3ccSAndroid Build Coastguard Worker 
243*77c1e3ccSAndroid Build Coastguard Worker   float dnn_features[100];
244*77c1e3ccSAndroid Build Coastguard Worker   float logits[4] = { 0.0f };
245*77c1e3ccSAndroid Build Coastguard Worker 
246*77c1e3ccSAndroid Build Coastguard Worker   const float *branch_0 = part_info->cnn_buffer;
247*77c1e3ccSAndroid Build Coastguard Worker   const float *branch_1 = branch_0 + CNN_BRANCH_0_OUT_SIZE;
248*77c1e3ccSAndroid Build Coastguard Worker   const float *branch_2 = branch_1 + CNN_BRANCH_1_OUT_SIZE;
249*77c1e3ccSAndroid Build Coastguard Worker   const float *branch_3 = branch_2 + CNN_BRANCH_2_OUT_SIZE;
250*77c1e3ccSAndroid Build Coastguard Worker 
251*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == BLOCK_64X64) {
252*77c1e3ccSAndroid Build Coastguard Worker     int f_idx = 0;
253*77c1e3ccSAndroid Build Coastguard Worker     for (int ch_idx = 0; ch_idx < CNN_BRANCH_0_OUT_CH; ch_idx++) {
254*77c1e3ccSAndroid Build Coastguard Worker       dnn_features[f_idx++] = branch_0[ch_idx];
255*77c1e3ccSAndroid Build Coastguard Worker     }
256*77c1e3ccSAndroid Build Coastguard Worker 
257*77c1e3ccSAndroid Build Coastguard Worker     const int spa_stride = 2 * 2;
258*77c1e3ccSAndroid Build Coastguard Worker     for (int lin_idx = 0; lin_idx < spa_stride; lin_idx++) {
259*77c1e3ccSAndroid Build Coastguard Worker       for (int ch_idx = 0; ch_idx < CNN_BRANCH_1_OUT_CH; ch_idx++) {
260*77c1e3ccSAndroid Build Coastguard Worker         dnn_features[f_idx++] = branch_1[lin_idx + ch_idx * spa_stride];
261*77c1e3ccSAndroid Build Coastguard Worker       }
262*77c1e3ccSAndroid Build Coastguard Worker     }
263*77c1e3ccSAndroid Build Coastguard Worker     dnn_features[f_idx++] = part_info->log_q;
264*77c1e3ccSAndroid Build Coastguard Worker   } else if (bsize == BLOCK_32X32) {
265*77c1e3ccSAndroid Build Coastguard Worker     int f_idx = 0;
266*77c1e3ccSAndroid Build Coastguard Worker     for (int idx = 0; idx < CNN_BRANCH_0_OUT_CH; idx++) {
267*77c1e3ccSAndroid Build Coastguard Worker       dnn_features[f_idx++] = branch_0[idx];
268*77c1e3ccSAndroid Build Coastguard Worker     }
269*77c1e3ccSAndroid Build Coastguard Worker 
270*77c1e3ccSAndroid Build Coastguard Worker     const int curr_lin_idx = quad_to_linear_1[quad_tree_idx - 1];
271*77c1e3ccSAndroid Build Coastguard Worker     const int spa_stride = 2 * 2;
272*77c1e3ccSAndroid Build Coastguard Worker     for (int ch_idx = 0; ch_idx < CNN_BRANCH_1_OUT_CH; ch_idx++) {
273*77c1e3ccSAndroid Build Coastguard Worker       dnn_features[f_idx++] = branch_1[curr_lin_idx + ch_idx * spa_stride];
274*77c1e3ccSAndroid Build Coastguard Worker     }
275*77c1e3ccSAndroid Build Coastguard Worker     dnn_features[f_idx++] = part_info->log_q;
276*77c1e3ccSAndroid Build Coastguard Worker   } else if (bsize == BLOCK_16X16) {
277*77c1e3ccSAndroid Build Coastguard Worker     int f_idx = 0;
278*77c1e3ccSAndroid Build Coastguard Worker     const int prev_quad_idx = (quad_tree_idx - 1) / 4;
279*77c1e3ccSAndroid Build Coastguard Worker     const int prev_lin_idx = quad_to_linear_1[prev_quad_idx - 1];
280*77c1e3ccSAndroid Build Coastguard Worker     const int prev_spa_stride = 2 * 2;
281*77c1e3ccSAndroid Build Coastguard Worker     for (int ch_idx = 0; ch_idx < CNN_BRANCH_1_OUT_CH; ch_idx++) {
282*77c1e3ccSAndroid Build Coastguard Worker       dnn_features[f_idx++] = branch_1[prev_lin_idx + ch_idx * prev_spa_stride];
283*77c1e3ccSAndroid Build Coastguard Worker     }
284*77c1e3ccSAndroid Build Coastguard Worker 
285*77c1e3ccSAndroid Build Coastguard Worker     const int curr_lin_idx = quad_to_linear_2[quad_tree_idx - 5];
286*77c1e3ccSAndroid Build Coastguard Worker     const int spa_stride = 4 * 4;
287*77c1e3ccSAndroid Build Coastguard Worker     for (int ch_idx = 0; ch_idx < CNN_BRANCH_2_OUT_CH; ch_idx++) {
288*77c1e3ccSAndroid Build Coastguard Worker       dnn_features[f_idx++] = branch_2[curr_lin_idx + ch_idx * spa_stride];
289*77c1e3ccSAndroid Build Coastguard Worker     }
290*77c1e3ccSAndroid Build Coastguard Worker     dnn_features[f_idx++] = part_info->log_q;
291*77c1e3ccSAndroid Build Coastguard Worker   } else if (bsize == BLOCK_8X8) {
292*77c1e3ccSAndroid Build Coastguard Worker     int f_idx = 0;
293*77c1e3ccSAndroid Build Coastguard Worker     const int prev_quad_idx = (quad_tree_idx - 1) / 4;
294*77c1e3ccSAndroid Build Coastguard Worker     const int prev_lin_idx = quad_to_linear_2[prev_quad_idx - 5];
295*77c1e3ccSAndroid Build Coastguard Worker     const int prev_spa_stride = 4 * 4;
296*77c1e3ccSAndroid Build Coastguard Worker     for (int ch_idx = 0; ch_idx < CNN_BRANCH_2_OUT_CH; ch_idx++) {
297*77c1e3ccSAndroid Build Coastguard Worker       dnn_features[f_idx++] = branch_2[prev_lin_idx + ch_idx * prev_spa_stride];
298*77c1e3ccSAndroid Build Coastguard Worker     }
299*77c1e3ccSAndroid Build Coastguard Worker 
300*77c1e3ccSAndroid Build Coastguard Worker     const int curr_lin_idx = quad_to_linear_3[quad_tree_idx - 21];
301*77c1e3ccSAndroid Build Coastguard Worker     const int spa_stride = 8 * 8;
302*77c1e3ccSAndroid Build Coastguard Worker     for (int ch_idx = 0; ch_idx < CNN_BRANCH_3_OUT_CH; ch_idx++) {
303*77c1e3ccSAndroid Build Coastguard Worker       dnn_features[f_idx++] = branch_3[curr_lin_idx + ch_idx * spa_stride];
304*77c1e3ccSAndroid Build Coastguard Worker     }
305*77c1e3ccSAndroid Build Coastguard Worker     dnn_features[f_idx++] = part_info->log_q;
306*77c1e3ccSAndroid Build Coastguard Worker   } else {
307*77c1e3ccSAndroid Build Coastguard Worker     assert(0 && "Invalid bsize in intra_cnn partition");
308*77c1e3ccSAndroid Build Coastguard Worker   }
309*77c1e3ccSAndroid Build Coastguard Worker 
310*77c1e3ccSAndroid Build Coastguard Worker   // Make decision
311*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_predict(dnn_features, dnn_config, 1, logits);
312*77c1e3ccSAndroid Build Coastguard Worker 
313*77c1e3ccSAndroid Build Coastguard Worker   const int is_720p_or_larger = AOMMIN(cm->width, cm->height) >= 720;
314*77c1e3ccSAndroid Build Coastguard Worker   const int is_480p_or_larger = AOMMIN(cm->width, cm->height) >= 480;
315*77c1e3ccSAndroid Build Coastguard Worker   float split_only_thresh = 100.0f, no_split_thresh = -100.0f;
316*77c1e3ccSAndroid Build Coastguard Worker   if (is_720p_or_larger) {
317*77c1e3ccSAndroid Build Coastguard Worker     split_only_thresh =
318*77c1e3ccSAndroid Build Coastguard Worker         av1_intra_mode_cnn_partition_split_thresh_hdres[bsize_idx];
319*77c1e3ccSAndroid Build Coastguard Worker     no_split_thresh =
320*77c1e3ccSAndroid Build Coastguard Worker         av1_intra_mode_cnn_partition_no_split_thresh_hdres[bsize_idx];
321*77c1e3ccSAndroid Build Coastguard Worker   } else if (is_480p_or_larger) {
322*77c1e3ccSAndroid Build Coastguard Worker     split_only_thresh =
323*77c1e3ccSAndroid Build Coastguard Worker         av1_intra_mode_cnn_partition_split_thresh_midres[bsize_idx];
324*77c1e3ccSAndroid Build Coastguard Worker     no_split_thresh =
325*77c1e3ccSAndroid Build Coastguard Worker         av1_intra_mode_cnn_partition_no_split_thresh_midres[bsize_idx];
326*77c1e3ccSAndroid Build Coastguard Worker   } else {
327*77c1e3ccSAndroid Build Coastguard Worker     split_only_thresh =
328*77c1e3ccSAndroid Build Coastguard Worker         av1_intra_mode_cnn_partition_split_thresh_lowres[bsize_idx];
329*77c1e3ccSAndroid Build Coastguard Worker     no_split_thresh =
330*77c1e3ccSAndroid Build Coastguard Worker         av1_intra_mode_cnn_partition_no_split_thresh_lowres[bsize_idx];
331*77c1e3ccSAndroid Build Coastguard Worker   }
332*77c1e3ccSAndroid Build Coastguard Worker 
333*77c1e3ccSAndroid Build Coastguard Worker   if (logits[0] > split_only_thresh) {
334*77c1e3ccSAndroid Build Coastguard Worker     // As screen contents tend to choose larger partitions, do not prune
335*77c1e3ccSAndroid Build Coastguard Worker     // PARTITION_NONE when intra_cnn_based_part_prune_level=1.
336*77c1e3ccSAndroid Build Coastguard Worker     if (intra_cnn_based_part_prune_level != 1) {
337*77c1e3ccSAndroid Build Coastguard Worker       part_state->partition_none_allowed = 0;
338*77c1e3ccSAndroid Build Coastguard Worker     }
339*77c1e3ccSAndroid Build Coastguard Worker     part_state->do_square_split = 1;
340*77c1e3ccSAndroid Build Coastguard Worker     av1_disable_rect_partitions(part_state);
341*77c1e3ccSAndroid Build Coastguard Worker   }
342*77c1e3ccSAndroid Build Coastguard Worker 
343*77c1e3ccSAndroid Build Coastguard Worker   if (logits[0] < no_split_thresh) {
344*77c1e3ccSAndroid Build Coastguard Worker     av1_disable_square_split_partition(part_state);
345*77c1e3ccSAndroid Build Coastguard Worker   }
346*77c1e3ccSAndroid Build Coastguard Worker }
347*77c1e3ccSAndroid Build Coastguard Worker 
get_simple_motion_search_prune_agg(int qindex,int prune_level,int is_rect_part)348*77c1e3ccSAndroid Build Coastguard Worker static inline int get_simple_motion_search_prune_agg(int qindex,
349*77c1e3ccSAndroid Build Coastguard Worker                                                      int prune_level,
350*77c1e3ccSAndroid Build Coastguard Worker                                                      int is_rect_part) {
351*77c1e3ccSAndroid Build Coastguard Worker   assert(prune_level < TOTAL_AGG_LVLS);
352*77c1e3ccSAndroid Build Coastguard Worker   if (prune_level == NO_PRUNING) {
353*77c1e3ccSAndroid Build Coastguard Worker     return -1;
354*77c1e3ccSAndroid Build Coastguard Worker   }
355*77c1e3ccSAndroid Build Coastguard Worker 
356*77c1e3ccSAndroid Build Coastguard Worker   // Aggressiveness value for SIMPLE_MOTION_SEARCH_PRUNE_LEVEL except
357*77c1e3ccSAndroid Build Coastguard Worker   // QIDX_BASED_AGG_LVL
358*77c1e3ccSAndroid Build Coastguard Worker   const int sms_prune_agg_levels[TOTAL_SIMPLE_AGG_LVLS] = { 0, 1, 2, 3 };
359*77c1e3ccSAndroid Build Coastguard Worker   if (prune_level < TOTAL_SIMPLE_AGG_LVLS) {
360*77c1e3ccSAndroid Build Coastguard Worker     return sms_prune_agg_levels[prune_level];
361*77c1e3ccSAndroid Build Coastguard Worker   }
362*77c1e3ccSAndroid Build Coastguard Worker 
363*77c1e3ccSAndroid Build Coastguard Worker   // Map the QIDX_BASED_AGG_LVL to corresponding aggressiveness value.
364*77c1e3ccSAndroid Build Coastguard Worker   // Aggressive pruning for lower quantizers in non-boosted frames to prune
365*77c1e3ccSAndroid Build Coastguard Worker   // rectangular partitions.
366*77c1e3ccSAndroid Build Coastguard Worker   const int qband = is_rect_part ? (qindex <= 90 ? 1 : 0) : 0;
367*77c1e3ccSAndroid Build Coastguard Worker   const int sms_prune_agg_qindex_based[2] = { 1, 2 };
368*77c1e3ccSAndroid Build Coastguard Worker   return sms_prune_agg_qindex_based[qband];
369*77c1e3ccSAndroid Build Coastguard Worker }
370*77c1e3ccSAndroid Build Coastguard Worker 
371*77c1e3ccSAndroid Build Coastguard Worker // Performs a simple_motion_search with a single reference frame and extract
372*77c1e3ccSAndroid Build Coastguard Worker // the variance of residues. Then use the features to determine whether we want
373*77c1e3ccSAndroid Build Coastguard Worker // to go straight to splitting without trying PARTITION_NONE
simple_motion_search_based_split(AV1_COMP * const cpi,MACROBLOCK * x,SIMPLE_MOTION_DATA_TREE * sms_tree,PartitionSearchState * part_state)374*77c1e3ccSAndroid Build Coastguard Worker static void simple_motion_search_based_split(AV1_COMP *const cpi, MACROBLOCK *x,
375*77c1e3ccSAndroid Build Coastguard Worker                                              SIMPLE_MOTION_DATA_TREE *sms_tree,
376*77c1e3ccSAndroid Build Coastguard Worker                                              PartitionSearchState *part_state) {
377*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
378*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams *blk_params = &part_state->part_blk_params;
379*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params->mi_row, mi_col = blk_params->mi_col;
380*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params->bsize;
381*77c1e3ccSAndroid Build Coastguard Worker 
382*77c1e3ccSAndroid Build Coastguard Worker   const int bsize_idx = convert_bsize_to_idx(bsize);
383*77c1e3ccSAndroid Build Coastguard Worker   const int is_720p_or_larger = AOMMIN(cm->width, cm->height) >= 720;
384*77c1e3ccSAndroid Build Coastguard Worker   const int is_480p_or_larger = AOMMIN(cm->width, cm->height) >= 480;
385*77c1e3ccSAndroid Build Coastguard Worker   // res_idx is 0 for res < 480p, 1 for 480p, 2 for 720p+
386*77c1e3ccSAndroid Build Coastguard Worker   const int res_idx = is_480p_or_larger + is_720p_or_larger;
387*77c1e3ccSAndroid Build Coastguard Worker 
388*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize_idx >= 0 && bsize_idx <= 4 &&
389*77c1e3ccSAndroid Build Coastguard Worker          "Invalid bsize in simple_motion_search_based_split");
390*77c1e3ccSAndroid Build Coastguard Worker 
391*77c1e3ccSAndroid Build Coastguard Worker   const float *ml_mean = av1_simple_motion_search_split_mean[bsize_idx];
392*77c1e3ccSAndroid Build Coastguard Worker   const float *ml_std = av1_simple_motion_search_split_std[bsize_idx];
393*77c1e3ccSAndroid Build Coastguard Worker   const NN_CONFIG *nn_config =
394*77c1e3ccSAndroid Build Coastguard Worker       av1_simple_motion_search_split_nn_config[bsize_idx];
395*77c1e3ccSAndroid Build Coastguard Worker 
396*77c1e3ccSAndroid Build Coastguard Worker   const int agg = get_simple_motion_search_prune_agg(
397*77c1e3ccSAndroid Build Coastguard Worker       x->qindex, cpi->sf.part_sf.simple_motion_search_prune_agg, 0);
398*77c1e3ccSAndroid Build Coastguard Worker   if (agg < 0) {
399*77c1e3ccSAndroid Build Coastguard Worker     return;
400*77c1e3ccSAndroid Build Coastguard Worker   }
401*77c1e3ccSAndroid Build Coastguard Worker 
402*77c1e3ccSAndroid Build Coastguard Worker   const float split_only_thresh =
403*77c1e3ccSAndroid Build Coastguard Worker       av1_simple_motion_search_split_thresh[agg][res_idx][bsize_idx];
404*77c1e3ccSAndroid Build Coastguard Worker   const float no_split_thresh =
405*77c1e3ccSAndroid Build Coastguard Worker       av1_simple_motion_search_no_split_thresh[agg][res_idx][bsize_idx];
406*77c1e3ccSAndroid Build Coastguard Worker 
407*77c1e3ccSAndroid Build Coastguard Worker   float features[FEATURE_SIZE_SMS_SPLIT] = { 0.0f };
408*77c1e3ccSAndroid Build Coastguard Worker   simple_motion_search_prune_part_features(cpi, x, sms_tree, mi_row, mi_col,
409*77c1e3ccSAndroid Build Coastguard Worker                                            bsize, features,
410*77c1e3ccSAndroid Build Coastguard Worker                                            FEATURE_SMS_SPLIT_MODEL_FLAG);
411*77c1e3ccSAndroid Build Coastguard Worker 
412*77c1e3ccSAndroid Build Coastguard Worker   // Write features to file
413*77c1e3ccSAndroid Build Coastguard Worker   write_features_to_file(cpi->oxcf.partition_info_path,
414*77c1e3ccSAndroid Build Coastguard Worker                          cpi->ext_part_controller.test_mode, features,
415*77c1e3ccSAndroid Build Coastguard Worker                          FEATURE_SIZE_SMS_SPLIT, 0, bsize, mi_row, mi_col);
416*77c1e3ccSAndroid Build Coastguard Worker 
417*77c1e3ccSAndroid Build Coastguard Worker   // Note: it is intended to not normalize the features here, to keep it
418*77c1e3ccSAndroid Build Coastguard Worker   // consistent for all features collected and passed to the external model.
419*77c1e3ccSAndroid Build Coastguard Worker   if (ext_ml_model_decision_before_none(
420*77c1e3ccSAndroid Build Coastguard Worker           cpi, features, &part_state->partition_none_allowed,
421*77c1e3ccSAndroid Build Coastguard Worker           &part_state->partition_rect_allowed[HORZ],
422*77c1e3ccSAndroid Build Coastguard Worker           &part_state->partition_rect_allowed[VERT],
423*77c1e3ccSAndroid Build Coastguard Worker           &part_state->do_rectangular_split, &part_state->do_square_split)) {
424*77c1e3ccSAndroid Build Coastguard Worker     return;
425*77c1e3ccSAndroid Build Coastguard Worker   }
426*77c1e3ccSAndroid Build Coastguard Worker 
427*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < FEATURE_SIZE_SMS_SPLIT; idx++) {
428*77c1e3ccSAndroid Build Coastguard Worker     features[idx] = (features[idx] - ml_mean[idx]) / ml_std[idx];
429*77c1e3ccSAndroid Build Coastguard Worker   }
430*77c1e3ccSAndroid Build Coastguard Worker 
431*77c1e3ccSAndroid Build Coastguard Worker   float score = 0.0f;
432*77c1e3ccSAndroid Build Coastguard Worker 
433*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_predict(features, nn_config, 1, &score);
434*77c1e3ccSAndroid Build Coastguard Worker 
435*77c1e3ccSAndroid Build Coastguard Worker   if (score > split_only_thresh) {
436*77c1e3ccSAndroid Build Coastguard Worker     av1_set_square_split_only(part_state);
437*77c1e3ccSAndroid Build Coastguard Worker   }
438*77c1e3ccSAndroid Build Coastguard Worker 
439*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.simple_motion_search_split >= 2 &&
440*77c1e3ccSAndroid Build Coastguard Worker       score < no_split_thresh) {
441*77c1e3ccSAndroid Build Coastguard Worker     av1_disable_square_split_partition(part_state);
442*77c1e3ccSAndroid Build Coastguard Worker   }
443*77c1e3ccSAndroid Build Coastguard Worker 
444*77c1e3ccSAndroid Build Coastguard Worker   // If the score is very low, prune rectangular split since it is unlikely to
445*77c1e3ccSAndroid Build Coastguard Worker   // occur.
446*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.simple_motion_search_rect_split) {
447*77c1e3ccSAndroid Build Coastguard Worker     const float scale = res_idx >= 2 ? 3.0f : 2.0f;
448*77c1e3ccSAndroid Build Coastguard Worker     const float rect_split_thresh =
449*77c1e3ccSAndroid Build Coastguard Worker         scale * av1_simple_motion_search_no_split_thresh
450*77c1e3ccSAndroid Build Coastguard Worker                     [cpi->sf.part_sf.simple_motion_search_rect_split][res_idx]
451*77c1e3ccSAndroid Build Coastguard Worker                     [bsize_idx];
452*77c1e3ccSAndroid Build Coastguard Worker     if (score < rect_split_thresh) {
453*77c1e3ccSAndroid Build Coastguard Worker       part_state->do_rectangular_split = 0;
454*77c1e3ccSAndroid Build Coastguard Worker     }
455*77c1e3ccSAndroid Build Coastguard Worker   }
456*77c1e3ccSAndroid Build Coastguard Worker }
457*77c1e3ccSAndroid Build Coastguard Worker 
458*77c1e3ccSAndroid Build Coastguard Worker // Given a list of ref frames in refs, performs simple_motion_search on each of
459*77c1e3ccSAndroid Build Coastguard Worker // the refs and returns the ref with the smallest sse. Returns -1 if none of the
460*77c1e3ccSAndroid Build Coastguard Worker // ref in the list is available. Also stores the best sse and var in best_sse,
461*77c1e3ccSAndroid Build Coastguard Worker // best_var, respectively. If save_mv is 0, don't update mv_ref_fulls in
462*77c1e3ccSAndroid Build Coastguard Worker // sms_tree. If save_mv is 1, update mv_ref_fulls under sms_tree and the
463*77c1e3ccSAndroid Build Coastguard Worker // subtrees.
simple_motion_search_get_best_ref(AV1_COMP * const cpi,MACROBLOCK * x,SIMPLE_MOTION_DATA_TREE * sms_tree,int mi_row,int mi_col,BLOCK_SIZE bsize,const int * const refs,int num_refs,int use_subpixel,int save_mv,unsigned int * best_sse,unsigned int * best_var)464*77c1e3ccSAndroid Build Coastguard Worker static int simple_motion_search_get_best_ref(
465*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, MACROBLOCK *x, SIMPLE_MOTION_DATA_TREE *sms_tree,
466*77c1e3ccSAndroid Build Coastguard Worker     int mi_row, int mi_col, BLOCK_SIZE bsize, const int *const refs,
467*77c1e3ccSAndroid Build Coastguard Worker     int num_refs, int use_subpixel, int save_mv, unsigned int *best_sse,
468*77c1e3ccSAndroid Build Coastguard Worker     unsigned int *best_var) {
469*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
470*77c1e3ccSAndroid Build Coastguard Worker   int best_ref = -1;
471*77c1e3ccSAndroid Build Coastguard Worker 
472*77c1e3ccSAndroid Build Coastguard Worker   if (mi_col >= cm->mi_params.mi_cols || mi_row >= cm->mi_params.mi_rows) {
473*77c1e3ccSAndroid Build Coastguard Worker     // If the whole block is outside of the image, set the var and sse to 0.
474*77c1e3ccSAndroid Build Coastguard Worker     *best_var = 0;
475*77c1e3ccSAndroid Build Coastguard Worker     *best_sse = 0;
476*77c1e3ccSAndroid Build Coastguard Worker 
477*77c1e3ccSAndroid Build Coastguard Worker     return best_ref;
478*77c1e3ccSAndroid Build Coastguard Worker   }
479*77c1e3ccSAndroid Build Coastguard Worker 
480*77c1e3ccSAndroid Build Coastguard Worker   // Otherwise do loop through the reference frames and find the one with the
481*77c1e3ccSAndroid Build Coastguard Worker   // minimum SSE
482*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = 1;
483*77c1e3ccSAndroid Build Coastguard Worker 
484*77c1e3ccSAndroid Build Coastguard Worker   *best_sse = INT_MAX;
485*77c1e3ccSAndroid Build Coastguard Worker 
486*77c1e3ccSAndroid Build Coastguard Worker   for (int ref_idx = 0; ref_idx < num_refs; ref_idx++) {
487*77c1e3ccSAndroid Build Coastguard Worker     const int ref = refs[ref_idx];
488*77c1e3ccSAndroid Build Coastguard Worker 
489*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref]) {
490*77c1e3ccSAndroid Build Coastguard Worker       const FULLPEL_MV *start_mvs = sms_tree->start_mvs;
491*77c1e3ccSAndroid Build Coastguard Worker       unsigned int curr_sse = 0, curr_var = 0;
492*77c1e3ccSAndroid Build Coastguard Worker       const int_mv best_mv = av1_simple_motion_search_sse_var(
493*77c1e3ccSAndroid Build Coastguard Worker           cpi, x, mi_row, mi_col, bsize, ref, start_mvs[ref], num_planes,
494*77c1e3ccSAndroid Build Coastguard Worker           use_subpixel, &curr_sse, &curr_var);
495*77c1e3ccSAndroid Build Coastguard Worker       if (curr_sse < *best_sse) {
496*77c1e3ccSAndroid Build Coastguard Worker         *best_sse = curr_sse;
497*77c1e3ccSAndroid Build Coastguard Worker         *best_var = curr_var;
498*77c1e3ccSAndroid Build Coastguard Worker         best_ref = ref;
499*77c1e3ccSAndroid Build Coastguard Worker       }
500*77c1e3ccSAndroid Build Coastguard Worker 
501*77c1e3ccSAndroid Build Coastguard Worker       if (save_mv) {
502*77c1e3ccSAndroid Build Coastguard Worker         sms_tree->start_mvs[ref].row = best_mv.as_mv.row / 8;
503*77c1e3ccSAndroid Build Coastguard Worker         sms_tree->start_mvs[ref].col = best_mv.as_mv.col / 8;
504*77c1e3ccSAndroid Build Coastguard Worker 
505*77c1e3ccSAndroid Build Coastguard Worker         if (bsize >= BLOCK_8X8) {
506*77c1e3ccSAndroid Build Coastguard Worker           for (int r_idx = 0; r_idx < SUB_PARTITIONS_SPLIT; r_idx++) {
507*77c1e3ccSAndroid Build Coastguard Worker             // Propagate the new motion vectors to a lower level
508*77c1e3ccSAndroid Build Coastguard Worker             SIMPLE_MOTION_DATA_TREE *sub_tree = sms_tree->split[r_idx];
509*77c1e3ccSAndroid Build Coastguard Worker             sub_tree->start_mvs[ref] = sms_tree->start_mvs[ref];
510*77c1e3ccSAndroid Build Coastguard Worker           }
511*77c1e3ccSAndroid Build Coastguard Worker         }
512*77c1e3ccSAndroid Build Coastguard Worker       }
513*77c1e3ccSAndroid Build Coastguard Worker     }
514*77c1e3ccSAndroid Build Coastguard Worker   }
515*77c1e3ccSAndroid Build Coastguard Worker 
516*77c1e3ccSAndroid Build Coastguard Worker   return best_ref;
517*77c1e3ccSAndroid Build Coastguard Worker }
518*77c1e3ccSAndroid Build Coastguard Worker 
519*77c1e3ccSAndroid Build Coastguard Worker // Collects features using simple_motion_search and store them in features. The
520*77c1e3ccSAndroid Build Coastguard Worker // features are also cached in SIMPLE_MOTION_DATA_TREE. By default, the features
521*77c1e3ccSAndroid Build Coastguard Worker // collected are the sse and var from the subblocks flagged by features_to_get.
522*77c1e3ccSAndroid Build Coastguard Worker // Furthermore, if features is not NULL, then 7 more features are appended to
523*77c1e3ccSAndroid Build Coastguard Worker // the end of features:
524*77c1e3ccSAndroid Build Coastguard Worker //  - log(1.0 + dc_q ** 2)
525*77c1e3ccSAndroid Build Coastguard Worker //  - whether an above macroblock exists
526*77c1e3ccSAndroid Build Coastguard Worker //  - width of above macroblock
527*77c1e3ccSAndroid Build Coastguard Worker //  - height of above macroblock
528*77c1e3ccSAndroid Build Coastguard Worker //  - whether a left marcoblock exists
529*77c1e3ccSAndroid Build Coastguard Worker //  - width of left macroblock
530*77c1e3ccSAndroid Build Coastguard Worker //  - height of left macroblock
simple_motion_search_prune_part_features(AV1_COMP * const cpi,MACROBLOCK * x,SIMPLE_MOTION_DATA_TREE * sms_tree,int mi_row,int mi_col,BLOCK_SIZE bsize,float * features,int features_to_get)531*77c1e3ccSAndroid Build Coastguard Worker static inline void simple_motion_search_prune_part_features(
532*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, MACROBLOCK *x, SIMPLE_MOTION_DATA_TREE *sms_tree,
533*77c1e3ccSAndroid Build Coastguard Worker     int mi_row, int mi_col, BLOCK_SIZE bsize, float *features,
534*77c1e3ccSAndroid Build Coastguard Worker     int features_to_get) {
535*77c1e3ccSAndroid Build Coastguard Worker   const int w_mi = mi_size_wide[bsize];
536*77c1e3ccSAndroid Build Coastguard Worker   const int h_mi = mi_size_high[bsize];
537*77c1e3ccSAndroid Build Coastguard Worker   assert(mi_size_wide[bsize] == mi_size_high[bsize]);
538*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize >= BLOCK_8X8);
539*77c1e3ccSAndroid Build Coastguard Worker   assert(cpi->ref_frame_flags & av1_ref_frame_flag_list[LAST_FRAME] ||
540*77c1e3ccSAndroid Build Coastguard Worker          cpi->ref_frame_flags & av1_ref_frame_flag_list[ALTREF_FRAME]);
541*77c1e3ccSAndroid Build Coastguard Worker 
542*77c1e3ccSAndroid Build Coastguard Worker   // Setting up motion search
543*77c1e3ccSAndroid Build Coastguard Worker   const int ref_list[] = { cpi->rc.is_src_frame_alt_ref ? ALTREF_FRAME
544*77c1e3ccSAndroid Build Coastguard Worker                                                         : LAST_FRAME };
545*77c1e3ccSAndroid Build Coastguard Worker   const int num_refs = 1;
546*77c1e3ccSAndroid Build Coastguard Worker   const int use_subpixel = 1;
547*77c1e3ccSAndroid Build Coastguard Worker 
548*77c1e3ccSAndroid Build Coastguard Worker   // Doing whole block first to update the mv
549*77c1e3ccSAndroid Build Coastguard Worker   if (!sms_tree->sms_none_valid && features_to_get & FEATURE_SMS_NONE_FLAG) {
550*77c1e3ccSAndroid Build Coastguard Worker     simple_motion_search_get_best_ref(cpi, x, sms_tree, mi_row, mi_col, bsize,
551*77c1e3ccSAndroid Build Coastguard Worker                                       ref_list, num_refs, use_subpixel, 1,
552*77c1e3ccSAndroid Build Coastguard Worker                                       &sms_tree->sms_none_feat[0],
553*77c1e3ccSAndroid Build Coastguard Worker                                       &sms_tree->sms_none_feat[1]);
554*77c1e3ccSAndroid Build Coastguard Worker     sms_tree->sms_none_valid = 1;
555*77c1e3ccSAndroid Build Coastguard Worker   }
556*77c1e3ccSAndroid Build Coastguard Worker 
557*77c1e3ccSAndroid Build Coastguard Worker   // Split subblocks
558*77c1e3ccSAndroid Build Coastguard Worker   if (features_to_get & FEATURE_SMS_SPLIT_FLAG) {
559*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
560*77c1e3ccSAndroid Build Coastguard Worker     for (int r_idx = 0; r_idx < SUB_PARTITIONS_SPLIT; r_idx++) {
561*77c1e3ccSAndroid Build Coastguard Worker       const int sub_mi_col = mi_col + (r_idx & 1) * w_mi / 2;
562*77c1e3ccSAndroid Build Coastguard Worker       const int sub_mi_row = mi_row + (r_idx >> 1) * h_mi / 2;
563*77c1e3ccSAndroid Build Coastguard Worker       SIMPLE_MOTION_DATA_TREE *sub_tree = sms_tree->split[r_idx];
564*77c1e3ccSAndroid Build Coastguard Worker 
565*77c1e3ccSAndroid Build Coastguard Worker       if (!sub_tree->sms_none_valid) {
566*77c1e3ccSAndroid Build Coastguard Worker         simple_motion_search_get_best_ref(
567*77c1e3ccSAndroid Build Coastguard Worker             cpi, x, sub_tree, sub_mi_row, sub_mi_col, subsize, ref_list,
568*77c1e3ccSAndroid Build Coastguard Worker             num_refs, use_subpixel, 1, &sub_tree->sms_none_feat[0],
569*77c1e3ccSAndroid Build Coastguard Worker             &sub_tree->sms_none_feat[1]);
570*77c1e3ccSAndroid Build Coastguard Worker         sub_tree->sms_none_valid = 1;
571*77c1e3ccSAndroid Build Coastguard Worker       }
572*77c1e3ccSAndroid Build Coastguard Worker     }
573*77c1e3ccSAndroid Build Coastguard Worker   }
574*77c1e3ccSAndroid Build Coastguard Worker 
575*77c1e3ccSAndroid Build Coastguard Worker   // Rectangular subblocks
576*77c1e3ccSAndroid Build Coastguard Worker   if (!sms_tree->sms_rect_valid && features_to_get & FEATURE_SMS_RECT_FLAG) {
577*77c1e3ccSAndroid Build Coastguard Worker     // Horz subblock
578*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_HORZ);
579*77c1e3ccSAndroid Build Coastguard Worker     for (int r_idx = 0; r_idx < SUB_PARTITIONS_RECT; r_idx++) {
580*77c1e3ccSAndroid Build Coastguard Worker       const int sub_mi_col = mi_col + 0;
581*77c1e3ccSAndroid Build Coastguard Worker       const int sub_mi_row = mi_row + r_idx * h_mi / 2;
582*77c1e3ccSAndroid Build Coastguard Worker 
583*77c1e3ccSAndroid Build Coastguard Worker       simple_motion_search_get_best_ref(
584*77c1e3ccSAndroid Build Coastguard Worker           cpi, x, sms_tree, sub_mi_row, sub_mi_col, subsize, ref_list, num_refs,
585*77c1e3ccSAndroid Build Coastguard Worker           use_subpixel, 0, &sms_tree->sms_rect_feat[2 * r_idx],
586*77c1e3ccSAndroid Build Coastguard Worker           &sms_tree->sms_rect_feat[2 * r_idx + 1]);
587*77c1e3ccSAndroid Build Coastguard Worker     }
588*77c1e3ccSAndroid Build Coastguard Worker 
589*77c1e3ccSAndroid Build Coastguard Worker     // Vert subblock
590*77c1e3ccSAndroid Build Coastguard Worker     subsize = get_partition_subsize(bsize, PARTITION_VERT);
591*77c1e3ccSAndroid Build Coastguard Worker     for (int r_idx = 0; r_idx < SUB_PARTITIONS_RECT; r_idx++) {
592*77c1e3ccSAndroid Build Coastguard Worker       const int sub_mi_col = mi_col + r_idx * w_mi / 2;
593*77c1e3ccSAndroid Build Coastguard Worker       const int sub_mi_row = mi_row + 0;
594*77c1e3ccSAndroid Build Coastguard Worker 
595*77c1e3ccSAndroid Build Coastguard Worker       simple_motion_search_get_best_ref(
596*77c1e3ccSAndroid Build Coastguard Worker           cpi, x, sms_tree, sub_mi_row, sub_mi_col, subsize, ref_list, num_refs,
597*77c1e3ccSAndroid Build Coastguard Worker           use_subpixel, 0, &sms_tree->sms_rect_feat[4 + 2 * r_idx],
598*77c1e3ccSAndroid Build Coastguard Worker           &sms_tree->sms_rect_feat[4 + 2 * r_idx + 1]);
599*77c1e3ccSAndroid Build Coastguard Worker     }
600*77c1e3ccSAndroid Build Coastguard Worker     sms_tree->sms_rect_valid = 1;
601*77c1e3ccSAndroid Build Coastguard Worker   }
602*77c1e3ccSAndroid Build Coastguard Worker 
603*77c1e3ccSAndroid Build Coastguard Worker   if (!features) return;
604*77c1e3ccSAndroid Build Coastguard Worker 
605*77c1e3ccSAndroid Build Coastguard Worker   int f_idx = 0;
606*77c1e3ccSAndroid Build Coastguard Worker   if (features_to_get & FEATURE_SMS_NONE_FLAG) {
607*77c1e3ccSAndroid Build Coastguard Worker     for (int sub_idx = 0; sub_idx < 2; sub_idx++) {
608*77c1e3ccSAndroid Build Coastguard Worker       features[f_idx++] = log1pf((float)sms_tree->sms_none_feat[sub_idx]);
609*77c1e3ccSAndroid Build Coastguard Worker     }
610*77c1e3ccSAndroid Build Coastguard Worker   }
611*77c1e3ccSAndroid Build Coastguard Worker 
612*77c1e3ccSAndroid Build Coastguard Worker   if (features_to_get & FEATURE_SMS_SPLIT_FLAG) {
613*77c1e3ccSAndroid Build Coastguard Worker     for (int sub_idx = 0; sub_idx < SUB_PARTITIONS_SPLIT; sub_idx++) {
614*77c1e3ccSAndroid Build Coastguard Worker       SIMPLE_MOTION_DATA_TREE *sub_tree = sms_tree->split[sub_idx];
615*77c1e3ccSAndroid Build Coastguard Worker       features[f_idx++] = log1pf((float)sub_tree->sms_none_feat[0]);
616*77c1e3ccSAndroid Build Coastguard Worker       features[f_idx++] = log1pf((float)sub_tree->sms_none_feat[1]);
617*77c1e3ccSAndroid Build Coastguard Worker     }
618*77c1e3ccSAndroid Build Coastguard Worker   }
619*77c1e3ccSAndroid Build Coastguard Worker 
620*77c1e3ccSAndroid Build Coastguard Worker   if (features_to_get & FEATURE_SMS_RECT_FLAG) {
621*77c1e3ccSAndroid Build Coastguard Worker     for (int sub_idx = 0; sub_idx < 8; sub_idx++) {
622*77c1e3ccSAndroid Build Coastguard Worker       features[f_idx++] = log1pf((float)sms_tree->sms_rect_feat[sub_idx]);
623*77c1e3ccSAndroid Build Coastguard Worker     }
624*77c1e3ccSAndroid Build Coastguard Worker   }
625*77c1e3ccSAndroid Build Coastguard Worker 
626*77c1e3ccSAndroid Build Coastguard Worker   const MACROBLOCKD *xd = &x->e_mbd;
627*77c1e3ccSAndroid Build Coastguard Worker   set_offsets_for_motion_search(cpi, x, mi_row, mi_col, bsize);
628*77c1e3ccSAndroid Build Coastguard Worker 
629*77c1e3ccSAndroid Build Coastguard Worker   // Q_INDEX
630*77c1e3ccSAndroid Build Coastguard Worker   const int dc_q = av1_dc_quant_QTX(x->qindex, 0, xd->bd) >> (xd->bd - 8);
631*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)(dc_q * dc_q) / 256.0f);
632*77c1e3ccSAndroid Build Coastguard Worker 
633*77c1e3ccSAndroid Build Coastguard Worker   // Neighbor stuff
634*77c1e3ccSAndroid Build Coastguard Worker   const int has_above = !!xd->above_mbmi;
635*77c1e3ccSAndroid Build Coastguard Worker   const int has_left = !!xd->left_mbmi;
636*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE above_bsize = has_above ? xd->above_mbmi->bsize : bsize;
637*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE left_bsize = has_left ? xd->left_mbmi->bsize : bsize;
638*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = (float)has_above;
639*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = (float)mi_size_wide_log2[above_bsize];
640*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = (float)mi_size_high_log2[above_bsize];
641*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = (float)has_left;
642*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = (float)mi_size_wide_log2[left_bsize];
643*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = (float)mi_size_high_log2[left_bsize];
644*77c1e3ccSAndroid Build Coastguard Worker }
645*77c1e3ccSAndroid Build Coastguard Worker 
646*77c1e3ccSAndroid Build Coastguard Worker // Performs a simple_motion_search with two reference frames and extract
647*77c1e3ccSAndroid Build Coastguard Worker // the variance of residues. Then use the features to determine whether we want
648*77c1e3ccSAndroid Build Coastguard Worker // to prune some partitions.
simple_motion_search_prune_rect(AV1_COMP * const cpi,MACROBLOCK * x,SIMPLE_MOTION_DATA_TREE * sms_tree,PartitionSearchState * part_state)649*77c1e3ccSAndroid Build Coastguard Worker static void simple_motion_search_prune_rect(AV1_COMP *const cpi, MACROBLOCK *x,
650*77c1e3ccSAndroid Build Coastguard Worker                                             SIMPLE_MOTION_DATA_TREE *sms_tree,
651*77c1e3ccSAndroid Build Coastguard Worker                                             PartitionSearchState *part_state) {
652*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
653*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams *blk_params = &part_state->part_blk_params;
654*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params->mi_row, mi_col = blk_params->mi_col;
655*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params->bsize;
656*77c1e3ccSAndroid Build Coastguard Worker 
657*77c1e3ccSAndroid Build Coastguard Worker   const int bsize_idx = convert_bsize_to_idx(bsize);
658*77c1e3ccSAndroid Build Coastguard Worker   const int is_720p_or_larger = AOMMIN(cm->width, cm->height) >= 720;
659*77c1e3ccSAndroid Build Coastguard Worker   const int is_480p_or_larger = AOMMIN(cm->width, cm->height) >= 480;
660*77c1e3ccSAndroid Build Coastguard Worker   // res_idx is 0 for lowres, 1 for 48p, 2 for 720p+
661*77c1e3ccSAndroid Build Coastguard Worker   const int res_idx = is_480p_or_larger + is_720p_or_larger;
662*77c1e3ccSAndroid Build Coastguard Worker 
663*77c1e3ccSAndroid Build Coastguard Worker   // Get model parameters
664*77c1e3ccSAndroid Build Coastguard Worker   const NN_CONFIG *nn_config =
665*77c1e3ccSAndroid Build Coastguard Worker       av1_simple_motion_search_prune_rect_nn_config[bsize_idx];
666*77c1e3ccSAndroid Build Coastguard Worker   const float *ml_mean = av1_simple_motion_search_prune_rect_mean[bsize_idx],
667*77c1e3ccSAndroid Build Coastguard Worker               *ml_std = av1_simple_motion_search_prune_rect_std[bsize_idx];
668*77c1e3ccSAndroid Build Coastguard Worker 
669*77c1e3ccSAndroid Build Coastguard Worker   const int agg = get_simple_motion_search_prune_agg(
670*77c1e3ccSAndroid Build Coastguard Worker       x->qindex, cpi->sf.part_sf.simple_motion_search_prune_agg, 1);
671*77c1e3ccSAndroid Build Coastguard Worker   if (agg < 0) {
672*77c1e3ccSAndroid Build Coastguard Worker     return;
673*77c1e3ccSAndroid Build Coastguard Worker   }
674*77c1e3ccSAndroid Build Coastguard Worker 
675*77c1e3ccSAndroid Build Coastguard Worker   const float prune_thresh =
676*77c1e3ccSAndroid Build Coastguard Worker       av1_simple_motion_search_prune_rect_thresh[agg][res_idx][bsize_idx];
677*77c1e3ccSAndroid Build Coastguard Worker 
678*77c1e3ccSAndroid Build Coastguard Worker   // If there is no valid threshold, return immediately.
679*77c1e3ccSAndroid Build Coastguard Worker   if (!nn_config || prune_thresh == 0.0f) {
680*77c1e3ccSAndroid Build Coastguard Worker     return;
681*77c1e3ccSAndroid Build Coastguard Worker   }
682*77c1e3ccSAndroid Build Coastguard Worker 
683*77c1e3ccSAndroid Build Coastguard Worker   // Get features
684*77c1e3ccSAndroid Build Coastguard Worker   float features[FEATURE_SIZE_SMS_PRUNE_PART] = { 0.0f };
685*77c1e3ccSAndroid Build Coastguard Worker   simple_motion_search_prune_part_features(cpi, x, sms_tree, mi_row, mi_col,
686*77c1e3ccSAndroid Build Coastguard Worker                                            bsize, features,
687*77c1e3ccSAndroid Build Coastguard Worker                                            FEATURE_SMS_PRUNE_PART_FLAG);
688*77c1e3ccSAndroid Build Coastguard Worker 
689*77c1e3ccSAndroid Build Coastguard Worker   // Note: it is intended to not normalize the features here, to keep it
690*77c1e3ccSAndroid Build Coastguard Worker   // consistent for all features collected and passed to the external model.
691*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.simple_motion_search_prune_rect &&
692*77c1e3ccSAndroid Build Coastguard Worker       !frame_is_intra_only(cm) &&
693*77c1e3ccSAndroid Build Coastguard Worker       (part_state->partition_rect_allowed[HORZ] ||
694*77c1e3ccSAndroid Build Coastguard Worker        part_state->partition_rect_allowed[VERT]) &&
695*77c1e3ccSAndroid Build Coastguard Worker       bsize >= BLOCK_8X8 && !av1_superres_scaled(cm)) {
696*77c1e3ccSAndroid Build Coastguard Worker     // Write features to file
697*77c1e3ccSAndroid Build Coastguard Worker     write_features_to_file(
698*77c1e3ccSAndroid Build Coastguard Worker         cpi->oxcf.partition_info_path, cpi->ext_part_controller.test_mode,
699*77c1e3ccSAndroid Build Coastguard Worker         features, FEATURE_SIZE_SMS_PRUNE_PART, 1, bsize, mi_row, mi_col);
700*77c1e3ccSAndroid Build Coastguard Worker 
701*77c1e3ccSAndroid Build Coastguard Worker     if (ext_ml_model_decision_before_none_part2(
702*77c1e3ccSAndroid Build Coastguard Worker             cpi, features, &part_state->prune_rect_part[HORZ],
703*77c1e3ccSAndroid Build Coastguard Worker             &part_state->prune_rect_part[VERT])) {
704*77c1e3ccSAndroid Build Coastguard Worker       return;
705*77c1e3ccSAndroid Build Coastguard Worker     }
706*77c1e3ccSAndroid Build Coastguard Worker   }
707*77c1e3ccSAndroid Build Coastguard Worker 
708*77c1e3ccSAndroid Build Coastguard Worker   for (int f_idx = 0; f_idx < FEATURE_SIZE_SMS_PRUNE_PART; f_idx++) {
709*77c1e3ccSAndroid Build Coastguard Worker     features[f_idx] = (features[f_idx] - ml_mean[f_idx]) / ml_std[f_idx];
710*77c1e3ccSAndroid Build Coastguard Worker   }
711*77c1e3ccSAndroid Build Coastguard Worker 
712*77c1e3ccSAndroid Build Coastguard Worker   // Get probabilities
713*77c1e3ccSAndroid Build Coastguard Worker   float scores[EXT_PARTITION_TYPES] = { 0.0f },
714*77c1e3ccSAndroid Build Coastguard Worker         probs[EXT_PARTITION_TYPES] = { 0.0f };
715*77c1e3ccSAndroid Build Coastguard Worker   const int num_classes = (bsize == BLOCK_128X128 || bsize == BLOCK_8X8)
716*77c1e3ccSAndroid Build Coastguard Worker                               ? PARTITION_TYPES
717*77c1e3ccSAndroid Build Coastguard Worker                               : EXT_PARTITION_TYPES;
718*77c1e3ccSAndroid Build Coastguard Worker 
719*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_predict(features, nn_config, 1, scores);
720*77c1e3ccSAndroid Build Coastguard Worker 
721*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_softmax(scores, probs, num_classes);
722*77c1e3ccSAndroid Build Coastguard Worker 
723*77c1e3ccSAndroid Build Coastguard Worker   // Determine if we should prune rectangular partitions.
724*77c1e3ccSAndroid Build Coastguard Worker   if (probs[PARTITION_HORZ] <= prune_thresh) {
725*77c1e3ccSAndroid Build Coastguard Worker     part_state->prune_rect_part[HORZ] = 1;
726*77c1e3ccSAndroid Build Coastguard Worker   }
727*77c1e3ccSAndroid Build Coastguard Worker   if (probs[PARTITION_VERT] <= prune_thresh) {
728*77c1e3ccSAndroid Build Coastguard Worker     part_state->prune_rect_part[VERT] = 1;
729*77c1e3ccSAndroid Build Coastguard Worker   }
730*77c1e3ccSAndroid Build Coastguard Worker }
731*77c1e3ccSAndroid Build Coastguard Worker 
732*77c1e3ccSAndroid Build Coastguard Worker // Early terminates PARTITION_NONE using simple_motion_search features and the
733*77c1e3ccSAndroid Build Coastguard Worker // rate, distortion, and rdcost of PARTITION_NONE. This is only called when:
734*77c1e3ccSAndroid Build Coastguard Worker //  - The frame is a show frame
735*77c1e3ccSAndroid Build Coastguard Worker //  - The frame is not intra only
736*77c1e3ccSAndroid Build Coastguard Worker //  - The current bsize is > BLOCK_8X8
737*77c1e3ccSAndroid Build Coastguard Worker //  - blk_row + blk_height/2 < total_rows and blk_col + blk_width/2 < total_cols
av1_simple_motion_search_early_term_none(AV1_COMP * const cpi,MACROBLOCK * x,SIMPLE_MOTION_DATA_TREE * sms_tree,const RD_STATS * none_rdc,PartitionSearchState * part_state)738*77c1e3ccSAndroid Build Coastguard Worker void av1_simple_motion_search_early_term_none(
739*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, MACROBLOCK *x, SIMPLE_MOTION_DATA_TREE *sms_tree,
740*77c1e3ccSAndroid Build Coastguard Worker     const RD_STATS *none_rdc, PartitionSearchState *part_state) {
741*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams *blk_params = &part_state->part_blk_params;
742*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params->mi_row, mi_col = blk_params->mi_col;
743*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params->bsize;
744*77c1e3ccSAndroid Build Coastguard Worker 
745*77c1e3ccSAndroid Build Coastguard Worker   float features[FEATURE_SIZE_SMS_TERM_NONE] = { 0.0f };
746*77c1e3ccSAndroid Build Coastguard Worker   simple_motion_search_prune_part_features(cpi, x, sms_tree, mi_row, mi_col,
747*77c1e3ccSAndroid Build Coastguard Worker                                            bsize, features,
748*77c1e3ccSAndroid Build Coastguard Worker                                            FEATURE_SMS_PRUNE_PART_FLAG);
749*77c1e3ccSAndroid Build Coastguard Worker   int f_idx = FEATURE_SIZE_SMS_PRUNE_PART;
750*77c1e3ccSAndroid Build Coastguard Worker 
751*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)none_rdc->rate);
752*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)none_rdc->dist);
753*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)none_rdc->rdcost);
754*77c1e3ccSAndroid Build Coastguard Worker 
755*77c1e3ccSAndroid Build Coastguard Worker   assert(f_idx == FEATURE_SIZE_SMS_TERM_NONE);
756*77c1e3ccSAndroid Build Coastguard Worker 
757*77c1e3ccSAndroid Build Coastguard Worker   const float *ml_mean = NULL;
758*77c1e3ccSAndroid Build Coastguard Worker   const float *ml_std = NULL;
759*77c1e3ccSAndroid Build Coastguard Worker   const float *ml_model = NULL;
760*77c1e3ccSAndroid Build Coastguard Worker 
761*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == BLOCK_128X128) {
762*77c1e3ccSAndroid Build Coastguard Worker     ml_mean = av1_simple_motion_search_term_none_mean_128;
763*77c1e3ccSAndroid Build Coastguard Worker     ml_std = av1_simple_motion_search_term_none_std_128;
764*77c1e3ccSAndroid Build Coastguard Worker     ml_model = av1_simple_motion_search_term_none_model_128;
765*77c1e3ccSAndroid Build Coastguard Worker   } else if (bsize == BLOCK_64X64) {
766*77c1e3ccSAndroid Build Coastguard Worker     ml_mean = av1_simple_motion_search_term_none_mean_64;
767*77c1e3ccSAndroid Build Coastguard Worker     ml_std = av1_simple_motion_search_term_none_std_64;
768*77c1e3ccSAndroid Build Coastguard Worker     ml_model = av1_simple_motion_search_term_none_model_64;
769*77c1e3ccSAndroid Build Coastguard Worker   } else if (bsize == BLOCK_32X32) {
770*77c1e3ccSAndroid Build Coastguard Worker     ml_mean = av1_simple_motion_search_term_none_mean_32;
771*77c1e3ccSAndroid Build Coastguard Worker     ml_std = av1_simple_motion_search_term_none_std_32;
772*77c1e3ccSAndroid Build Coastguard Worker     ml_model = av1_simple_motion_search_term_none_model_32;
773*77c1e3ccSAndroid Build Coastguard Worker   } else if (bsize == BLOCK_16X16) {
774*77c1e3ccSAndroid Build Coastguard Worker     ml_mean = av1_simple_motion_search_term_none_mean_16;
775*77c1e3ccSAndroid Build Coastguard Worker     ml_std = av1_simple_motion_search_term_none_std_16;
776*77c1e3ccSAndroid Build Coastguard Worker     ml_model = av1_simple_motion_search_term_none_model_16;
777*77c1e3ccSAndroid Build Coastguard Worker   } else {
778*77c1e3ccSAndroid Build Coastguard Worker     assert(0 && "Unexpected block size in simple_motion_term_none");
779*77c1e3ccSAndroid Build Coastguard Worker   }
780*77c1e3ccSAndroid Build Coastguard Worker 
781*77c1e3ccSAndroid Build Coastguard Worker   // Write features to file
782*77c1e3ccSAndroid Build Coastguard Worker   write_features_to_file(cpi->oxcf.partition_info_path,
783*77c1e3ccSAndroid Build Coastguard Worker                          cpi->ext_part_controller.test_mode, features,
784*77c1e3ccSAndroid Build Coastguard Worker                          FEATURE_SIZE_SMS_TERM_NONE, 3, bsize, mi_row, mi_col);
785*77c1e3ccSAndroid Build Coastguard Worker 
786*77c1e3ccSAndroid Build Coastguard Worker   if (ext_ml_model_decision_after_none_part2(
787*77c1e3ccSAndroid Build Coastguard Worker           cpi, features, &part_state->terminate_partition_search)) {
788*77c1e3ccSAndroid Build Coastguard Worker     return;
789*77c1e3ccSAndroid Build Coastguard Worker   }
790*77c1e3ccSAndroid Build Coastguard Worker 
791*77c1e3ccSAndroid Build Coastguard Worker   if (ml_model) {
792*77c1e3ccSAndroid Build Coastguard Worker     float score = 0.0f;
793*77c1e3ccSAndroid Build Coastguard Worker     for (f_idx = 0; f_idx < FEATURE_SIZE_SMS_TERM_NONE; f_idx++) {
794*77c1e3ccSAndroid Build Coastguard Worker       score +=
795*77c1e3ccSAndroid Build Coastguard Worker           ml_model[f_idx] * (features[f_idx] - ml_mean[f_idx]) / ml_std[f_idx];
796*77c1e3ccSAndroid Build Coastguard Worker     }
797*77c1e3ccSAndroid Build Coastguard Worker     score += ml_model[FEATURE_SIZE_SMS_TERM_NONE];
798*77c1e3ccSAndroid Build Coastguard Worker 
799*77c1e3ccSAndroid Build Coastguard Worker     if (score >= 0.0f) {
800*77c1e3ccSAndroid Build Coastguard Worker       part_state->terminate_partition_search = 1;
801*77c1e3ccSAndroid Build Coastguard Worker     }
802*77c1e3ccSAndroid Build Coastguard Worker   }
803*77c1e3ccSAndroid Build Coastguard Worker }
804*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_max_min_partition_features(AV1_COMP * const cpi,MACROBLOCK * x,int mi_row,int mi_col,float * features)805*77c1e3ccSAndroid Build Coastguard Worker void av1_get_max_min_partition_features(AV1_COMP *const cpi, MACROBLOCK *x,
806*77c1e3ccSAndroid Build Coastguard Worker                                         int mi_row, int mi_col,
807*77c1e3ccSAndroid Build Coastguard Worker                                         float *features) {
808*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
809*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *xd = &x->e_mbd;
810*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
811*77c1e3ccSAndroid Build Coastguard Worker 
812*77c1e3ccSAndroid Build Coastguard Worker   // Currently this only allows 128X128 SB size. May extend it to 64X64 SB size.
813*77c1e3ccSAndroid Build Coastguard Worker   assert(sb_size == BLOCK_128X128);
814*77c1e3ccSAndroid Build Coastguard Worker 
815*77c1e3ccSAndroid Build Coastguard Worker   int f_idx = 0;
816*77c1e3ccSAndroid Build Coastguard Worker 
817*77c1e3ccSAndroid Build Coastguard Worker   const int dc_q = av1_dc_quant_QTX(x->qindex, 0, xd->bd) >> (xd->bd - 8);
818*77c1e3ccSAndroid Build Coastguard Worker   const float log_q_sq = log1pf((float)(dc_q * dc_q) / 256.0f);
819*77c1e3ccSAndroid Build Coastguard Worker 
820*77c1e3ccSAndroid Build Coastguard Worker   // Perform full-pixel single motion search in Y plane of 16x16 mbs in the sb
821*77c1e3ccSAndroid Build Coastguard Worker   float sum_mv_row_sq = 0;
822*77c1e3ccSAndroid Build Coastguard Worker   float sum_mv_row = 0;
823*77c1e3ccSAndroid Build Coastguard Worker   float min_abs_mv_row = FLT_MAX;
824*77c1e3ccSAndroid Build Coastguard Worker   float max_abs_mv_row = 0;
825*77c1e3ccSAndroid Build Coastguard Worker 
826*77c1e3ccSAndroid Build Coastguard Worker   float sum_mv_col_sq = 0;
827*77c1e3ccSAndroid Build Coastguard Worker   float sum_mv_col = 0;
828*77c1e3ccSAndroid Build Coastguard Worker   float min_abs_mv_col = FLT_MAX;
829*77c1e3ccSAndroid Build Coastguard Worker   float max_abs_mv_col = 0;
830*77c1e3ccSAndroid Build Coastguard Worker 
831*77c1e3ccSAndroid Build Coastguard Worker   float sum_log_sse_sq = 0;
832*77c1e3ccSAndroid Build Coastguard Worker   float sum_log_sse = 0;
833*77c1e3ccSAndroid Build Coastguard Worker   float min_log_sse = FLT_MAX;
834*77c1e3ccSAndroid Build Coastguard Worker   float max_log_sse = 0;
835*77c1e3ccSAndroid Build Coastguard Worker 
836*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE mb_size = BLOCK_16X16;
837*77c1e3ccSAndroid Build Coastguard Worker   const int mb_rows = block_size_high[sb_size] / block_size_high[mb_size];
838*77c1e3ccSAndroid Build Coastguard Worker   const int mb_cols = block_size_wide[sb_size] / block_size_wide[mb_size];
839*77c1e3ccSAndroid Build Coastguard Worker   const int mb_in_mi_size_high_log2 = mi_size_high_log2[mb_size];
840*77c1e3ccSAndroid Build Coastguard Worker   const int mb_in_mi_size_wide_log2 = mi_size_wide_log2[mb_size];
841*77c1e3ccSAndroid Build Coastguard Worker 
842*77c1e3ccSAndroid Build Coastguard Worker   for (int mb_row = 0; mb_row < mb_rows; mb_row++)
843*77c1e3ccSAndroid Build Coastguard Worker     for (int mb_col = 0; mb_col < mb_cols; mb_col++) {
844*77c1e3ccSAndroid Build Coastguard Worker       const int this_mi_row = mi_row + (mb_row << mb_in_mi_size_high_log2);
845*77c1e3ccSAndroid Build Coastguard Worker       const int this_mi_col = mi_col + (mb_col << mb_in_mi_size_wide_log2);
846*77c1e3ccSAndroid Build Coastguard Worker       unsigned int sse = 0;
847*77c1e3ccSAndroid Build Coastguard Worker       unsigned int var = 0;
848*77c1e3ccSAndroid Build Coastguard Worker       const FULLPEL_MV start_mv = kZeroFullMv;
849*77c1e3ccSAndroid Build Coastguard Worker       const MV_REFERENCE_FRAME ref =
850*77c1e3ccSAndroid Build Coastguard Worker           cpi->rc.is_src_frame_alt_ref ? ALTREF_FRAME : LAST_FRAME;
851*77c1e3ccSAndroid Build Coastguard Worker       const int_mv best_mv = av1_simple_motion_search_sse_var(
852*77c1e3ccSAndroid Build Coastguard Worker           cpi, x, this_mi_row, this_mi_col, mb_size, ref, start_mv, 1, 0, &sse,
853*77c1e3ccSAndroid Build Coastguard Worker           &var);
854*77c1e3ccSAndroid Build Coastguard Worker 
855*77c1e3ccSAndroid Build Coastguard Worker       const float mv_row = (float)(best_mv.as_mv.row / 8);
856*77c1e3ccSAndroid Build Coastguard Worker       const float mv_col = (float)(best_mv.as_mv.col / 8);
857*77c1e3ccSAndroid Build Coastguard Worker       const float log_sse = log1pf((float)sse);
858*77c1e3ccSAndroid Build Coastguard Worker       const float abs_mv_row = fabsf(mv_row);
859*77c1e3ccSAndroid Build Coastguard Worker       const float abs_mv_col = fabsf(mv_col);
860*77c1e3ccSAndroid Build Coastguard Worker 
861*77c1e3ccSAndroid Build Coastguard Worker       sum_mv_row_sq += mv_row * mv_row;
862*77c1e3ccSAndroid Build Coastguard Worker       sum_mv_row += mv_row;
863*77c1e3ccSAndroid Build Coastguard Worker       sum_mv_col_sq += mv_col * mv_col;
864*77c1e3ccSAndroid Build Coastguard Worker       sum_mv_col += mv_col;
865*77c1e3ccSAndroid Build Coastguard Worker 
866*77c1e3ccSAndroid Build Coastguard Worker       if (abs_mv_row < min_abs_mv_row) min_abs_mv_row = abs_mv_row;
867*77c1e3ccSAndroid Build Coastguard Worker       if (abs_mv_row > max_abs_mv_row) max_abs_mv_row = abs_mv_row;
868*77c1e3ccSAndroid Build Coastguard Worker       if (abs_mv_col < min_abs_mv_col) min_abs_mv_col = abs_mv_col;
869*77c1e3ccSAndroid Build Coastguard Worker       if (abs_mv_col > max_abs_mv_col) max_abs_mv_col = abs_mv_col;
870*77c1e3ccSAndroid Build Coastguard Worker 
871*77c1e3ccSAndroid Build Coastguard Worker       sum_log_sse_sq += log_sse * log_sse;
872*77c1e3ccSAndroid Build Coastguard Worker       sum_log_sse += log_sse;
873*77c1e3ccSAndroid Build Coastguard Worker       if (log_sse < min_log_sse) min_log_sse = log_sse;
874*77c1e3ccSAndroid Build Coastguard Worker       if (log_sse > max_log_sse) max_log_sse = log_sse;
875*77c1e3ccSAndroid Build Coastguard Worker     }
876*77c1e3ccSAndroid Build Coastguard Worker   const int blks = mb_rows * mb_cols;
877*77c1e3ccSAndroid Build Coastguard Worker   const float avg_mv_row = sum_mv_row / (float)blks;
878*77c1e3ccSAndroid Build Coastguard Worker   const float var_mv_row =
879*77c1e3ccSAndroid Build Coastguard Worker       sum_mv_row_sq / (float)blks - avg_mv_row * avg_mv_row;
880*77c1e3ccSAndroid Build Coastguard Worker 
881*77c1e3ccSAndroid Build Coastguard Worker   const float avg_mv_col = sum_mv_col / (float)blks;
882*77c1e3ccSAndroid Build Coastguard Worker   const float var_mv_col =
883*77c1e3ccSAndroid Build Coastguard Worker       sum_mv_col_sq / (float)blks - avg_mv_col * avg_mv_col;
884*77c1e3ccSAndroid Build Coastguard Worker 
885*77c1e3ccSAndroid Build Coastguard Worker   const float avg_log_sse = sum_log_sse / (float)blks;
886*77c1e3ccSAndroid Build Coastguard Worker   const float var_log_sse =
887*77c1e3ccSAndroid Build Coastguard Worker       sum_log_sse_sq / (float)blks - avg_log_sse * avg_log_sse;
888*77c1e3ccSAndroid Build Coastguard Worker 
889*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = avg_log_sse;
890*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = avg_mv_col;
891*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = avg_mv_row;
892*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log_q_sq;
893*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = max_abs_mv_col;
894*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = max_abs_mv_row;
895*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = max_log_sse;
896*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = min_abs_mv_col;
897*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = min_abs_mv_row;
898*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = min_log_sse;
899*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = var_log_sse;
900*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = var_mv_col;
901*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = var_mv_row;
902*77c1e3ccSAndroid Build Coastguard Worker 
903*77c1e3ccSAndroid Build Coastguard Worker   assert(f_idx == FEATURE_SIZE_MAX_MIN_PART_PRED);
904*77c1e3ccSAndroid Build Coastguard Worker }
905*77c1e3ccSAndroid Build Coastguard Worker 
906*77c1e3ccSAndroid Build Coastguard Worker // Convert result index to block size.
907*77c1e3ccSAndroid Build Coastguard Worker // result idx     block size
908*77c1e3ccSAndroid Build Coastguard Worker //     0          BLOCK_16X16
909*77c1e3ccSAndroid Build Coastguard Worker //     1          BLOCK_32X32
910*77c1e3ccSAndroid Build Coastguard Worker //     2          BLOCK_64X64
911*77c1e3ccSAndroid Build Coastguard Worker //     3          BLOCK_128X128
get_block_size(int idx)912*77c1e3ccSAndroid Build Coastguard Worker static BLOCK_SIZE get_block_size(int idx) {
913*77c1e3ccSAndroid Build Coastguard Worker   return (BLOCK_SIZE)((idx + 2) * 3);
914*77c1e3ccSAndroid Build Coastguard Worker }
915*77c1e3ccSAndroid Build Coastguard Worker 
av1_predict_max_partition(const AV1_COMP * const cpi,const MACROBLOCK * const x,const float * features)916*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE av1_predict_max_partition(const AV1_COMP *const cpi,
917*77c1e3ccSAndroid Build Coastguard Worker                                      const MACROBLOCK *const x,
918*77c1e3ccSAndroid Build Coastguard Worker                                      const float *features) {
919*77c1e3ccSAndroid Build Coastguard Worker   float scores[MAX_NUM_CLASSES_MAX_MIN_PART_PRED] = { 0.0f };
920*77c1e3ccSAndroid Build Coastguard Worker   const NN_CONFIG *nn_config = &av1_max_part_pred_nn_config;
921*77c1e3ccSAndroid Build Coastguard Worker 
922*77c1e3ccSAndroid Build Coastguard Worker   assert(cpi->sf.part_sf.auto_max_partition_based_on_simple_motion !=
923*77c1e3ccSAndroid Build Coastguard Worker          NOT_IN_USE);
924*77c1e3ccSAndroid Build Coastguard Worker 
925*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_predict(features, nn_config, 1, scores);
926*77c1e3ccSAndroid Build Coastguard Worker 
927*77c1e3ccSAndroid Build Coastguard Worker   int result = MAX_NUM_CLASSES_MAX_MIN_PART_PRED - 1;
928*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.auto_max_partition_based_on_simple_motion ==
929*77c1e3ccSAndroid Build Coastguard Worker       DIRECT_PRED) {
930*77c1e3ccSAndroid Build Coastguard Worker     result = 0;
931*77c1e3ccSAndroid Build Coastguard Worker     float max_score = scores[0];
932*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 1; i < MAX_NUM_CLASSES_MAX_MIN_PART_PRED; ++i) {
933*77c1e3ccSAndroid Build Coastguard Worker       if (scores[i] > max_score) {
934*77c1e3ccSAndroid Build Coastguard Worker         max_score = scores[i];
935*77c1e3ccSAndroid Build Coastguard Worker         result = i;
936*77c1e3ccSAndroid Build Coastguard Worker       }
937*77c1e3ccSAndroid Build Coastguard Worker     }
938*77c1e3ccSAndroid Build Coastguard Worker     return get_block_size(result);
939*77c1e3ccSAndroid Build Coastguard Worker   }
940*77c1e3ccSAndroid Build Coastguard Worker 
941*77c1e3ccSAndroid Build Coastguard Worker   float probs[MAX_NUM_CLASSES_MAX_MIN_PART_PRED] = { 0.0f };
942*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_softmax(scores, probs, MAX_NUM_CLASSES_MAX_MIN_PART_PRED);
943*77c1e3ccSAndroid Build Coastguard Worker 
944*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.auto_max_partition_based_on_simple_motion ==
945*77c1e3ccSAndroid Build Coastguard Worker       RELAXED_PRED) {
946*77c1e3ccSAndroid Build Coastguard Worker     for (result = MAX_NUM_CLASSES_MAX_MIN_PART_PRED - 1; result >= 0;
947*77c1e3ccSAndroid Build Coastguard Worker          --result) {
948*77c1e3ccSAndroid Build Coastguard Worker       if (result < MAX_NUM_CLASSES_MAX_MIN_PART_PRED - 1) {
949*77c1e3ccSAndroid Build Coastguard Worker         probs[result] += probs[result + 1];
950*77c1e3ccSAndroid Build Coastguard Worker       }
951*77c1e3ccSAndroid Build Coastguard Worker       if (probs[result] > 0.2) break;
952*77c1e3ccSAndroid Build Coastguard Worker     }
953*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->sf.part_sf.auto_max_partition_based_on_simple_motion ==
954*77c1e3ccSAndroid Build Coastguard Worker              ADAPT_PRED) {
955*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE sb_size = cpi->common.seq_params->sb_size;
956*77c1e3ccSAndroid Build Coastguard Worker     // TODO(debargha): x->source_variance is unavailable at this point,
957*77c1e3ccSAndroid Build Coastguard Worker     // so compute. The redundant recomputation later can be removed.
958*77c1e3ccSAndroid Build Coastguard Worker     const unsigned int source_variance = av1_get_perpixel_variance_facade(
959*77c1e3ccSAndroid Build Coastguard Worker         cpi, &x->e_mbd, &x->plane[0].src, sb_size, AOM_PLANE_Y);
960*77c1e3ccSAndroid Build Coastguard Worker     if (source_variance > 16) {
961*77c1e3ccSAndroid Build Coastguard Worker       const double thresh = source_variance < 128 ? 0.05 : 0.1;
962*77c1e3ccSAndroid Build Coastguard Worker       for (result = MAX_NUM_CLASSES_MAX_MIN_PART_PRED - 1; result >= 0;
963*77c1e3ccSAndroid Build Coastguard Worker            --result) {
964*77c1e3ccSAndroid Build Coastguard Worker         if (result < MAX_NUM_CLASSES_MAX_MIN_PART_PRED - 1) {
965*77c1e3ccSAndroid Build Coastguard Worker           probs[result] += probs[result + 1];
966*77c1e3ccSAndroid Build Coastguard Worker         }
967*77c1e3ccSAndroid Build Coastguard Worker         if (probs[result] > thresh) break;
968*77c1e3ccSAndroid Build Coastguard Worker       }
969*77c1e3ccSAndroid Build Coastguard Worker     }
970*77c1e3ccSAndroid Build Coastguard Worker   }
971*77c1e3ccSAndroid Build Coastguard Worker 
972*77c1e3ccSAndroid Build Coastguard Worker   return get_block_size(result);
973*77c1e3ccSAndroid Build Coastguard Worker }
974*77c1e3ccSAndroid Build Coastguard Worker 
975*77c1e3ccSAndroid Build Coastguard Worker // Get the minimum partition block width and height(in log scale) under a
976*77c1e3ccSAndroid Build Coastguard Worker // SIMPLE_MOTION_DATA_TREE.
get_min_bsize(const SIMPLE_MOTION_DATA_TREE * sms_tree,int * min_bw,int * min_bh)977*77c1e3ccSAndroid Build Coastguard Worker static inline void get_min_bsize(const SIMPLE_MOTION_DATA_TREE *sms_tree,
978*77c1e3ccSAndroid Build Coastguard Worker                                  int *min_bw, int *min_bh) {
979*77c1e3ccSAndroid Build Coastguard Worker   if (!sms_tree) return;
980*77c1e3ccSAndroid Build Coastguard Worker 
981*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = sms_tree->block_size;
982*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == BLOCK_4X4) {
983*77c1e3ccSAndroid Build Coastguard Worker     *min_bw = 0;
984*77c1e3ccSAndroid Build Coastguard Worker     *min_bh = 0;
985*77c1e3ccSAndroid Build Coastguard Worker     return;
986*77c1e3ccSAndroid Build Coastguard Worker   }
987*77c1e3ccSAndroid Build Coastguard Worker 
988*77c1e3ccSAndroid Build Coastguard Worker   PARTITION_TYPE part_type = sms_tree->partitioning;
989*77c1e3ccSAndroid Build Coastguard Worker   if (part_type == PARTITION_INVALID) return;
990*77c1e3ccSAndroid Build Coastguard Worker 
991*77c1e3ccSAndroid Build Coastguard Worker   if (part_type == PARTITION_SPLIT) {
992*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
993*77c1e3ccSAndroid Build Coastguard Worker       get_min_bsize(sms_tree->split[i], min_bw, min_bh);
994*77c1e3ccSAndroid Build Coastguard Worker     }
995*77c1e3ccSAndroid Build Coastguard Worker   } else {
996*77c1e3ccSAndroid Build Coastguard Worker     if (part_type == PARTITION_HORZ_A || part_type == PARTITION_HORZ_B ||
997*77c1e3ccSAndroid Build Coastguard Worker         part_type == PARTITION_VERT_A || part_type == PARTITION_VERT_B)
998*77c1e3ccSAndroid Build Coastguard Worker       part_type = PARTITION_SPLIT;
999*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE subsize = get_partition_subsize(bsize, part_type);
1000*77c1e3ccSAndroid Build Coastguard Worker     if (subsize != BLOCK_INVALID) {
1001*77c1e3ccSAndroid Build Coastguard Worker       *min_bw = AOMMIN(*min_bw, mi_size_wide_log2[subsize]);
1002*77c1e3ccSAndroid Build Coastguard Worker       *min_bh = AOMMIN(*min_bh, mi_size_high_log2[subsize]);
1003*77c1e3ccSAndroid Build Coastguard Worker     }
1004*77c1e3ccSAndroid Build Coastguard Worker   }
1005*77c1e3ccSAndroid Build Coastguard Worker }
1006*77c1e3ccSAndroid Build Coastguard Worker 
add_rd_feature(int64_t rd,int64_t best_rd,float * features,int * feature_idx)1007*77c1e3ccSAndroid Build Coastguard Worker static inline void add_rd_feature(int64_t rd, int64_t best_rd, float *features,
1008*77c1e3ccSAndroid Build Coastguard Worker                                   int *feature_idx) {
1009*77c1e3ccSAndroid Build Coastguard Worker   const int rd_valid = rd > 0 && rd < INT64_MAX;
1010*77c1e3ccSAndroid Build Coastguard Worker   const float rd_ratio = rd_valid ? (float)rd / best_rd : 1.0f;
1011*77c1e3ccSAndroid Build Coastguard Worker   features[(*feature_idx)++] = (float)rd_valid;
1012*77c1e3ccSAndroid Build Coastguard Worker   features[(*feature_idx)++] = rd_ratio;
1013*77c1e3ccSAndroid Build Coastguard Worker }
1014*77c1e3ccSAndroid Build Coastguard Worker 
1015*77c1e3ccSAndroid Build Coastguard Worker #define FEATURES 31
av1_ml_early_term_after_split(AV1_COMP * const cpi,MACROBLOCK * const x,SIMPLE_MOTION_DATA_TREE * const sms_tree,int64_t best_rd,int64_t part_none_rd,int64_t part_split_rd,int64_t * split_block_rd,PartitionSearchState * part_state)1016*77c1e3ccSAndroid Build Coastguard Worker void av1_ml_early_term_after_split(AV1_COMP *const cpi, MACROBLOCK *const x,
1017*77c1e3ccSAndroid Build Coastguard Worker                                    SIMPLE_MOTION_DATA_TREE *const sms_tree,
1018*77c1e3ccSAndroid Build Coastguard Worker                                    int64_t best_rd, int64_t part_none_rd,
1019*77c1e3ccSAndroid Build Coastguard Worker                                    int64_t part_split_rd,
1020*77c1e3ccSAndroid Build Coastguard Worker                                    int64_t *split_block_rd,
1021*77c1e3ccSAndroid Build Coastguard Worker                                    PartitionSearchState *part_state) {
1022*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams *blk_params = &part_state->part_blk_params;
1023*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params->mi_row, mi_col = blk_params->mi_col;
1024*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params->bsize;
1025*77c1e3ccSAndroid Build Coastguard Worker 
1026*77c1e3ccSAndroid Build Coastguard Worker   if (best_rd <= 0 || best_rd == INT64_MAX ||
1027*77c1e3ccSAndroid Build Coastguard Worker       part_state->terminate_partition_search)
1028*77c1e3ccSAndroid Build Coastguard Worker     return;
1029*77c1e3ccSAndroid Build Coastguard Worker 
1030*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
1031*77c1e3ccSAndroid Build Coastguard Worker   const int is_480p_or_larger = AOMMIN(cm->width, cm->height) >= 480;
1032*77c1e3ccSAndroid Build Coastguard Worker   const NN_CONFIG *nn_config = NULL;
1033*77c1e3ccSAndroid Build Coastguard Worker   float thresh = -1e6;
1034*77c1e3ccSAndroid Build Coastguard Worker   switch (bsize) {
1035*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_128X128: break;
1036*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_64X64:
1037*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_early_term_after_split_nnconfig_64;
1038*77c1e3ccSAndroid Build Coastguard Worker       thresh = is_480p_or_larger ? -2.0f : -1.2f;
1039*77c1e3ccSAndroid Build Coastguard Worker       break;
1040*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_32X32:
1041*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_early_term_after_split_nnconfig_32;
1042*77c1e3ccSAndroid Build Coastguard Worker       thresh = is_480p_or_larger ? -2.6f : -2.3f;
1043*77c1e3ccSAndroid Build Coastguard Worker       break;
1044*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_16X16:
1045*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_early_term_after_split_nnconfig_16;
1046*77c1e3ccSAndroid Build Coastguard Worker       thresh = is_480p_or_larger ? -2.0f : -2.4f;
1047*77c1e3ccSAndroid Build Coastguard Worker       break;
1048*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_8X8:
1049*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_early_term_after_split_nnconfig_8;
1050*77c1e3ccSAndroid Build Coastguard Worker       thresh = is_480p_or_larger ? -1.0f : -1.4f;
1051*77c1e3ccSAndroid Build Coastguard Worker       break;
1052*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_4X4: break;
1053*77c1e3ccSAndroid Build Coastguard Worker     default:
1054*77c1e3ccSAndroid Build Coastguard Worker       assert(0 && "Invalid block size in av1_ml_early_term_after_split().");
1055*77c1e3ccSAndroid Build Coastguard Worker       break;
1056*77c1e3ccSAndroid Build Coastguard Worker   }
1057*77c1e3ccSAndroid Build Coastguard Worker   if (!nn_config) return;
1058*77c1e3ccSAndroid Build Coastguard Worker 
1059*77c1e3ccSAndroid Build Coastguard Worker   // Use more conservative threshold for level 1.
1060*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.ml_early_term_after_part_split_level < 2) thresh -= 0.3f;
1061*77c1e3ccSAndroid Build Coastguard Worker 
1062*77c1e3ccSAndroid Build Coastguard Worker   const MACROBLOCKD *const xd = &x->e_mbd;
1063*77c1e3ccSAndroid Build Coastguard Worker   const int dc_q = av1_dc_quant_QTX(x->qindex, 0, xd->bd) >> (xd->bd - 8);
1064*77c1e3ccSAndroid Build Coastguard Worker   const int bs = block_size_wide[bsize];
1065*77c1e3ccSAndroid Build Coastguard Worker   int f_idx = 0;
1066*77c1e3ccSAndroid Build Coastguard Worker   float features[FEATURES] = { 0.0f };
1067*77c1e3ccSAndroid Build Coastguard Worker 
1068*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)dc_q / 4.0f);
1069*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)best_rd / bs / bs / 1024.0f);
1070*77c1e3ccSAndroid Build Coastguard Worker 
1071*77c1e3ccSAndroid Build Coastguard Worker   add_rd_feature(part_none_rd, best_rd, features, &f_idx);
1072*77c1e3ccSAndroid Build Coastguard Worker   add_rd_feature(part_split_rd, best_rd, features, &f_idx);
1073*77c1e3ccSAndroid Build Coastguard Worker 
1074*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
1075*77c1e3ccSAndroid Build Coastguard Worker     add_rd_feature(split_block_rd[i], best_rd, features, &f_idx);
1076*77c1e3ccSAndroid Build Coastguard Worker     int min_bw = MAX_SB_SIZE_LOG2;
1077*77c1e3ccSAndroid Build Coastguard Worker     int min_bh = MAX_SB_SIZE_LOG2;
1078*77c1e3ccSAndroid Build Coastguard Worker     get_min_bsize(sms_tree->split[i], &min_bw, &min_bh);
1079*77c1e3ccSAndroid Build Coastguard Worker     features[f_idx++] = (float)min_bw;
1080*77c1e3ccSAndroid Build Coastguard Worker     features[f_idx++] = (float)min_bh;
1081*77c1e3ccSAndroid Build Coastguard Worker   }
1082*77c1e3ccSAndroid Build Coastguard Worker 
1083*77c1e3ccSAndroid Build Coastguard Worker   simple_motion_search_prune_part_features(cpi, x, sms_tree, mi_row, mi_col,
1084*77c1e3ccSAndroid Build Coastguard Worker                                            bsize, NULL,
1085*77c1e3ccSAndroid Build Coastguard Worker                                            FEATURE_SMS_PRUNE_PART_FLAG);
1086*77c1e3ccSAndroid Build Coastguard Worker 
1087*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)sms_tree->sms_none_feat[1]);
1088*77c1e3ccSAndroid Build Coastguard Worker 
1089*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)sms_tree->split[0]->sms_none_feat[1]);
1090*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)sms_tree->split[1]->sms_none_feat[1]);
1091*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)sms_tree->split[2]->sms_none_feat[1]);
1092*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)sms_tree->split[3]->sms_none_feat[1]);
1093*77c1e3ccSAndroid Build Coastguard Worker 
1094*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)sms_tree->sms_rect_feat[1]);
1095*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)sms_tree->sms_rect_feat[3]);
1096*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)sms_tree->sms_rect_feat[5]);
1097*77c1e3ccSAndroid Build Coastguard Worker   features[f_idx++] = log1pf((float)sms_tree->sms_rect_feat[7]);
1098*77c1e3ccSAndroid Build Coastguard Worker 
1099*77c1e3ccSAndroid Build Coastguard Worker   assert(f_idx == FEATURES);
1100*77c1e3ccSAndroid Build Coastguard Worker 
1101*77c1e3ccSAndroid Build Coastguard Worker   // Write features to file
1102*77c1e3ccSAndroid Build Coastguard Worker   write_features_to_file(cpi->oxcf.partition_info_path,
1103*77c1e3ccSAndroid Build Coastguard Worker                          cpi->ext_part_controller.test_mode, features, FEATURES,
1104*77c1e3ccSAndroid Build Coastguard Worker                          4, bsize, mi_row, mi_col);
1105*77c1e3ccSAndroid Build Coastguard Worker 
1106*77c1e3ccSAndroid Build Coastguard Worker   if (ext_ml_model_decision_after_split(
1107*77c1e3ccSAndroid Build Coastguard Worker           cpi, features, &part_state->terminate_partition_search)) {
1108*77c1e3ccSAndroid Build Coastguard Worker     return;
1109*77c1e3ccSAndroid Build Coastguard Worker   }
1110*77c1e3ccSAndroid Build Coastguard Worker 
1111*77c1e3ccSAndroid Build Coastguard Worker   float score = 0.0f;
1112*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_predict(features, nn_config, 1, &score);
1113*77c1e3ccSAndroid Build Coastguard Worker   // Score is indicator of confidence that we should NOT terminate.
1114*77c1e3ccSAndroid Build Coastguard Worker   if (score < thresh) {
1115*77c1e3ccSAndroid Build Coastguard Worker     part_state->terminate_partition_search = 1;
1116*77c1e3ccSAndroid Build Coastguard Worker   }
1117*77c1e3ccSAndroid Build Coastguard Worker }
1118*77c1e3ccSAndroid Build Coastguard Worker #undef FEATURES
1119*77c1e3ccSAndroid Build Coastguard Worker 
av1_ml_prune_rect_partition(AV1_COMP * const cpi,const MACROBLOCK * const x,int64_t best_rd,int64_t none_rd,const int64_t * split_rd,PartitionSearchState * part_state)1120*77c1e3ccSAndroid Build Coastguard Worker void av1_ml_prune_rect_partition(AV1_COMP *const cpi, const MACROBLOCK *const x,
1121*77c1e3ccSAndroid Build Coastguard Worker                                  int64_t best_rd, int64_t none_rd,
1122*77c1e3ccSAndroid Build Coastguard Worker                                  const int64_t *split_rd,
1123*77c1e3ccSAndroid Build Coastguard Worker                                  PartitionSearchState *part_state) {
1124*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams *blk_params = &part_state->part_blk_params;
1125*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params->mi_row, mi_col = blk_params->mi_col;
1126*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params->bsize;
1127*77c1e3ccSAndroid Build Coastguard Worker 
1128*77c1e3ccSAndroid Build Coastguard Worker   if (bsize < BLOCK_8X8 || best_rd >= 1000000000) return;
1129*77c1e3ccSAndroid Build Coastguard Worker   best_rd = AOMMAX(best_rd, 1);
1130*77c1e3ccSAndroid Build Coastguard Worker   const NN_CONFIG *nn_config = NULL;
1131*77c1e3ccSAndroid Build Coastguard Worker   const float prob_thresholds[5] = { 0.01f, 0.01f, 0.004f, 0.002f, 0.002f };
1132*77c1e3ccSAndroid Build Coastguard Worker   float cur_thresh = 0.0f;
1133*77c1e3ccSAndroid Build Coastguard Worker   switch (bsize) {
1134*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_8X8:
1135*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_rect_partition_nnconfig_8;
1136*77c1e3ccSAndroid Build Coastguard Worker       cur_thresh = prob_thresholds[0];
1137*77c1e3ccSAndroid Build Coastguard Worker       break;
1138*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_16X16:
1139*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_rect_partition_nnconfig_16;
1140*77c1e3ccSAndroid Build Coastguard Worker       cur_thresh = prob_thresholds[1];
1141*77c1e3ccSAndroid Build Coastguard Worker       break;
1142*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_32X32:
1143*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_rect_partition_nnconfig_32;
1144*77c1e3ccSAndroid Build Coastguard Worker       cur_thresh = prob_thresholds[2];
1145*77c1e3ccSAndroid Build Coastguard Worker       break;
1146*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_64X64:
1147*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_rect_partition_nnconfig_64;
1148*77c1e3ccSAndroid Build Coastguard Worker       cur_thresh = prob_thresholds[3];
1149*77c1e3ccSAndroid Build Coastguard Worker       break;
1150*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_128X128:
1151*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_rect_partition_nnconfig_128;
1152*77c1e3ccSAndroid Build Coastguard Worker       cur_thresh = prob_thresholds[4];
1153*77c1e3ccSAndroid Build Coastguard Worker       break;
1154*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0 && "Unexpected bsize.");
1155*77c1e3ccSAndroid Build Coastguard Worker   }
1156*77c1e3ccSAndroid Build Coastguard Worker   if (!nn_config) return;
1157*77c1e3ccSAndroid Build Coastguard Worker 
1158*77c1e3ccSAndroid Build Coastguard Worker   // 1. Compute input features
1159*77c1e3ccSAndroid Build Coastguard Worker   float features[9];
1160*77c1e3ccSAndroid Build Coastguard Worker 
1161*77c1e3ccSAndroid Build Coastguard Worker   // RD cost ratios
1162*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 5; i++) features[i] = 1.0f;
1163*77c1e3ccSAndroid Build Coastguard Worker   if (none_rd > 0 && none_rd < 1000000000)
1164*77c1e3ccSAndroid Build Coastguard Worker     features[0] = (float)none_rd / (float)best_rd;
1165*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; i++) {
1166*77c1e3ccSAndroid Build Coastguard Worker     if (split_rd[i] > 0 && split_rd[i] < 1000000000)
1167*77c1e3ccSAndroid Build Coastguard Worker       features[1 + i] = (float)split_rd[i] / (float)best_rd;
1168*77c1e3ccSAndroid Build Coastguard Worker   }
1169*77c1e3ccSAndroid Build Coastguard Worker 
1170*77c1e3ccSAndroid Build Coastguard Worker   // Variance ratios
1171*77c1e3ccSAndroid Build Coastguard Worker   const MACROBLOCKD *const xd = &x->e_mbd;
1172*77c1e3ccSAndroid Build Coastguard Worker   int whole_block_variance;
1173*77c1e3ccSAndroid Build Coastguard Worker   whole_block_variance = av1_get_perpixel_variance_facade(
1174*77c1e3ccSAndroid Build Coastguard Worker       cpi, xd, &x->plane[0].src, bsize, AOM_PLANE_Y);
1175*77c1e3ccSAndroid Build Coastguard Worker   whole_block_variance = AOMMAX(whole_block_variance, 1);
1176*77c1e3ccSAndroid Build Coastguard Worker 
1177*77c1e3ccSAndroid Build Coastguard Worker   int split_variance[SUB_PARTITIONS_SPLIT];
1178*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
1179*77c1e3ccSAndroid Build Coastguard Worker   struct buf_2d buf;
1180*77c1e3ccSAndroid Build Coastguard Worker   buf.stride = x->plane[0].src.stride;
1181*77c1e3ccSAndroid Build Coastguard Worker   const int bw = block_size_wide[bsize];
1182*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
1183*77c1e3ccSAndroid Build Coastguard Worker     const int x_idx = (i & 1) * bw / 2;
1184*77c1e3ccSAndroid Build Coastguard Worker     const int y_idx = (i >> 1) * bw / 2;
1185*77c1e3ccSAndroid Build Coastguard Worker     buf.buf = x->plane[0].src.buf + x_idx + y_idx * buf.stride;
1186*77c1e3ccSAndroid Build Coastguard Worker     split_variance[i] =
1187*77c1e3ccSAndroid Build Coastguard Worker         av1_get_perpixel_variance_facade(cpi, xd, &buf, subsize, AOM_PLANE_Y);
1188*77c1e3ccSAndroid Build Coastguard Worker   }
1189*77c1e3ccSAndroid Build Coastguard Worker 
1190*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; i++)
1191*77c1e3ccSAndroid Build Coastguard Worker     features[5 + i] = (float)split_variance[i] / (float)whole_block_variance;
1192*77c1e3ccSAndroid Build Coastguard Worker 
1193*77c1e3ccSAndroid Build Coastguard Worker   // Write features to file
1194*77c1e3ccSAndroid Build Coastguard Worker   write_features_to_file(cpi->oxcf.partition_info_path,
1195*77c1e3ccSAndroid Build Coastguard Worker                          cpi->ext_part_controller.test_mode, features,
1196*77c1e3ccSAndroid Build Coastguard Worker                          /*feature_size=*/9, 5, bsize, mi_row, mi_col);
1197*77c1e3ccSAndroid Build Coastguard Worker 
1198*77c1e3ccSAndroid Build Coastguard Worker   if (ext_ml_model_decision_after_split_part2(
1199*77c1e3ccSAndroid Build Coastguard Worker           &cpi->ext_part_controller, frame_is_intra_only(&cpi->common),
1200*77c1e3ccSAndroid Build Coastguard Worker           features, &part_state->prune_rect_part[HORZ],
1201*77c1e3ccSAndroid Build Coastguard Worker           &part_state->prune_rect_part[VERT])) {
1202*77c1e3ccSAndroid Build Coastguard Worker     return;
1203*77c1e3ccSAndroid Build Coastguard Worker   }
1204*77c1e3ccSAndroid Build Coastguard Worker 
1205*77c1e3ccSAndroid Build Coastguard Worker   // 2. Do the prediction and prune 0-2 partitions based on their probabilities
1206*77c1e3ccSAndroid Build Coastguard Worker   float raw_scores[3] = { 0.0f };
1207*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_predict(features, nn_config, 1, raw_scores);
1208*77c1e3ccSAndroid Build Coastguard Worker   float probs[3] = { 0.0f };
1209*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_softmax(raw_scores, probs, 3);
1210*77c1e3ccSAndroid Build Coastguard Worker 
1211*77c1e3ccSAndroid Build Coastguard Worker   // probs[0] is the probability of the fact that both rectangular partitions
1212*77c1e3ccSAndroid Build Coastguard Worker   // are worse than current best_rd
1213*77c1e3ccSAndroid Build Coastguard Worker   if (probs[1] <= cur_thresh) part_state->prune_rect_part[HORZ] = 1;
1214*77c1e3ccSAndroid Build Coastguard Worker   if (probs[2] <= cur_thresh) part_state->prune_rect_part[VERT] = 1;
1215*77c1e3ccSAndroid Build Coastguard Worker }
1216*77c1e3ccSAndroid Build Coastguard Worker 
1217*77c1e3ccSAndroid Build Coastguard Worker // Use a ML model to predict if horz_a, horz_b, vert_a, and vert_b should be
1218*77c1e3ccSAndroid Build Coastguard Worker // considered.
ml_prune_ab_partition(AV1_COMP * const cpi,int part_ctx,int var_ctx,int64_t best_rd,PartitionSearchState * part_state,int * ab_partitions_allowed)1219*77c1e3ccSAndroid Build Coastguard Worker static void ml_prune_ab_partition(AV1_COMP *const cpi, int part_ctx,
1220*77c1e3ccSAndroid Build Coastguard Worker                                   int var_ctx, int64_t best_rd,
1221*77c1e3ccSAndroid Build Coastguard Worker                                   PartitionSearchState *part_state,
1222*77c1e3ccSAndroid Build Coastguard Worker                                   int *ab_partitions_allowed) {
1223*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams blk_params = part_state->part_blk_params;
1224*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params.mi_row;
1225*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col = blk_params.mi_col;
1226*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params.bsize;
1227*77c1e3ccSAndroid Build Coastguard Worker 
1228*77c1e3ccSAndroid Build Coastguard Worker   if (bsize < BLOCK_8X8 || best_rd >= 1000000000) return;
1229*77c1e3ccSAndroid Build Coastguard Worker   const NN_CONFIG *nn_config = NULL;
1230*77c1e3ccSAndroid Build Coastguard Worker   switch (bsize) {
1231*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_8X8: nn_config = NULL; break;
1232*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_16X16: nn_config = &av1_ab_partition_nnconfig_16; break;
1233*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_32X32: nn_config = &av1_ab_partition_nnconfig_32; break;
1234*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_64X64: nn_config = &av1_ab_partition_nnconfig_64; break;
1235*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_128X128: nn_config = &av1_ab_partition_nnconfig_128; break;
1236*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0 && "Unexpected bsize.");
1237*77c1e3ccSAndroid Build Coastguard Worker   }
1238*77c1e3ccSAndroid Build Coastguard Worker   if (!nn_config) return;
1239*77c1e3ccSAndroid Build Coastguard Worker 
1240*77c1e3ccSAndroid Build Coastguard Worker   // Generate features.
1241*77c1e3ccSAndroid Build Coastguard Worker   float features[10];
1242*77c1e3ccSAndroid Build Coastguard Worker   int feature_index = 0;
1243*77c1e3ccSAndroid Build Coastguard Worker   features[feature_index++] = (float)part_ctx;
1244*77c1e3ccSAndroid Build Coastguard Worker   features[feature_index++] = (float)var_ctx;
1245*77c1e3ccSAndroid Build Coastguard Worker   const int rdcost = (int)AOMMIN(INT_MAX, best_rd);
1246*77c1e3ccSAndroid Build Coastguard Worker   int sub_block_rdcost[8] = { 0 };
1247*77c1e3ccSAndroid Build Coastguard Worker   int rd_index = 0;
1248*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
1249*77c1e3ccSAndroid Build Coastguard Worker     const int64_t *horz_rd = part_state->rect_part_rd[HORZ];
1250*77c1e3ccSAndroid Build Coastguard Worker     if (horz_rd[i] > 0 && horz_rd[i] < 1000000000)
1251*77c1e3ccSAndroid Build Coastguard Worker       sub_block_rdcost[rd_index] = (int)horz_rd[i];
1252*77c1e3ccSAndroid Build Coastguard Worker     ++rd_index;
1253*77c1e3ccSAndroid Build Coastguard Worker   }
1254*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
1255*77c1e3ccSAndroid Build Coastguard Worker     const int64_t *vert_rd = part_state->rect_part_rd[VERT];
1256*77c1e3ccSAndroid Build Coastguard Worker     if (vert_rd[i] > 0 && vert_rd[i] < 1000000000)
1257*77c1e3ccSAndroid Build Coastguard Worker       sub_block_rdcost[rd_index] = (int)vert_rd[i];
1258*77c1e3ccSAndroid Build Coastguard Worker     ++rd_index;
1259*77c1e3ccSAndroid Build Coastguard Worker   }
1260*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
1261*77c1e3ccSAndroid Build Coastguard Worker     const int64_t *split_rd = part_state->split_rd;
1262*77c1e3ccSAndroid Build Coastguard Worker     if (split_rd[i] > 0 && split_rd[i] < 1000000000)
1263*77c1e3ccSAndroid Build Coastguard Worker       sub_block_rdcost[rd_index] = (int)split_rd[i];
1264*77c1e3ccSAndroid Build Coastguard Worker     ++rd_index;
1265*77c1e3ccSAndroid Build Coastguard Worker   }
1266*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 8; ++i) {
1267*77c1e3ccSAndroid Build Coastguard Worker     // Ratio between the sub-block RD and the whole-block RD.
1268*77c1e3ccSAndroid Build Coastguard Worker     float rd_ratio = 1.0f;
1269*77c1e3ccSAndroid Build Coastguard Worker     if (sub_block_rdcost[i] > 0 && sub_block_rdcost[i] < rdcost)
1270*77c1e3ccSAndroid Build Coastguard Worker       rd_ratio = (float)sub_block_rdcost[i] / (float)rdcost;
1271*77c1e3ccSAndroid Build Coastguard Worker     features[feature_index++] = rd_ratio;
1272*77c1e3ccSAndroid Build Coastguard Worker   }
1273*77c1e3ccSAndroid Build Coastguard Worker   assert(feature_index == 10);
1274*77c1e3ccSAndroid Build Coastguard Worker 
1275*77c1e3ccSAndroid Build Coastguard Worker   // Write features to file
1276*77c1e3ccSAndroid Build Coastguard Worker   if (!frame_is_intra_only(&cpi->common)) {
1277*77c1e3ccSAndroid Build Coastguard Worker     write_features_to_file(cpi->oxcf.partition_info_path,
1278*77c1e3ccSAndroid Build Coastguard Worker                            cpi->ext_part_controller.test_mode, features,
1279*77c1e3ccSAndroid Build Coastguard Worker                            /*feature_size=*/10, 6, bsize, mi_row, mi_col);
1280*77c1e3ccSAndroid Build Coastguard Worker   }
1281*77c1e3ccSAndroid Build Coastguard Worker 
1282*77c1e3ccSAndroid Build Coastguard Worker   if (ext_ml_model_decision_after_rect(
1283*77c1e3ccSAndroid Build Coastguard Worker           &cpi->ext_part_controller, frame_is_intra_only(&cpi->common),
1284*77c1e3ccSAndroid Build Coastguard Worker           features, &ab_partitions_allowed[HORZ_A],
1285*77c1e3ccSAndroid Build Coastguard Worker           &ab_partitions_allowed[HORZ_B], &ab_partitions_allowed[VERT_A],
1286*77c1e3ccSAndroid Build Coastguard Worker           &ab_partitions_allowed[VERT_B])) {
1287*77c1e3ccSAndroid Build Coastguard Worker     return;
1288*77c1e3ccSAndroid Build Coastguard Worker   }
1289*77c1e3ccSAndroid Build Coastguard Worker 
1290*77c1e3ccSAndroid Build Coastguard Worker   // Calculate scores using the NN model.
1291*77c1e3ccSAndroid Build Coastguard Worker   float score[16] = { 0.0f };
1292*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_predict(features, nn_config, 1, score);
1293*77c1e3ccSAndroid Build Coastguard Worker   int int_score[16];
1294*77c1e3ccSAndroid Build Coastguard Worker   int max_score = -1000;
1295*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 16; ++i) {
1296*77c1e3ccSAndroid Build Coastguard Worker     int_score[i] = (int)(100 * score[i]);
1297*77c1e3ccSAndroid Build Coastguard Worker     max_score = AOMMAX(int_score[i], max_score);
1298*77c1e3ccSAndroid Build Coastguard Worker   }
1299*77c1e3ccSAndroid Build Coastguard Worker 
1300*77c1e3ccSAndroid Build Coastguard Worker   // Make decisions based on the model scores.
1301*77c1e3ccSAndroid Build Coastguard Worker   int thresh = max_score;
1302*77c1e3ccSAndroid Build Coastguard Worker   switch (bsize) {
1303*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_16X16: thresh -= 150; break;
1304*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_32X32: thresh -= 100; break;
1305*77c1e3ccSAndroid Build Coastguard Worker     default: break;
1306*77c1e3ccSAndroid Build Coastguard Worker   }
1307*77c1e3ccSAndroid Build Coastguard Worker   av1_zero_array(ab_partitions_allowed, NUM_AB_PARTS);
1308*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 16; ++i) {
1309*77c1e3ccSAndroid Build Coastguard Worker     if (int_score[i] >= thresh) {
1310*77c1e3ccSAndroid Build Coastguard Worker       if ((i >> 0) & 1) ab_partitions_allowed[HORZ_A] = 1;
1311*77c1e3ccSAndroid Build Coastguard Worker       if ((i >> 1) & 1) ab_partitions_allowed[HORZ_B] = 1;
1312*77c1e3ccSAndroid Build Coastguard Worker       if ((i >> 2) & 1) ab_partitions_allowed[VERT_A] = 1;
1313*77c1e3ccSAndroid Build Coastguard Worker       if ((i >> 3) & 1) ab_partitions_allowed[VERT_B] = 1;
1314*77c1e3ccSAndroid Build Coastguard Worker     }
1315*77c1e3ccSAndroid Build Coastguard Worker   }
1316*77c1e3ccSAndroid Build Coastguard Worker }
1317*77c1e3ccSAndroid Build Coastguard Worker 
1318*77c1e3ccSAndroid Build Coastguard Worker #define FEATURES 18
1319*77c1e3ccSAndroid Build Coastguard Worker #define LABELS 4
1320*77c1e3ccSAndroid Build Coastguard Worker // Use a ML model to predict if horz4 and vert4 should be considered.
av1_ml_prune_4_partition(AV1_COMP * const cpi,MACROBLOCK * const x,int part_ctx,int64_t best_rd,PartitionSearchState * part_state,int * part4_allowed,unsigned int pb_source_variance)1321*77c1e3ccSAndroid Build Coastguard Worker void av1_ml_prune_4_partition(AV1_COMP *const cpi, MACROBLOCK *const x,
1322*77c1e3ccSAndroid Build Coastguard Worker                               int part_ctx, int64_t best_rd,
1323*77c1e3ccSAndroid Build Coastguard Worker                               PartitionSearchState *part_state,
1324*77c1e3ccSAndroid Build Coastguard Worker                               int *part4_allowed,
1325*77c1e3ccSAndroid Build Coastguard Worker                               unsigned int pb_source_variance) {
1326*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams blk_params = part_state->part_blk_params;
1327*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params.mi_row;
1328*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col = blk_params.mi_col;
1329*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params.bsize;
1330*77c1e3ccSAndroid Build Coastguard Worker 
1331*77c1e3ccSAndroid Build Coastguard Worker   int64_t(*rect_part_rd)[SUB_PARTITIONS_RECT] = part_state->rect_part_rd;
1332*77c1e3ccSAndroid Build Coastguard Worker   int64_t *split_rd = part_state->split_rd;
1333*77c1e3ccSAndroid Build Coastguard Worker   if (ext_ml_model_decision_after_part_ab(
1334*77c1e3ccSAndroid Build Coastguard Worker           cpi, x, bsize, part_ctx, best_rd, rect_part_rd, split_rd,
1335*77c1e3ccSAndroid Build Coastguard Worker           &part4_allowed[HORZ4], &part4_allowed[VERT4], pb_source_variance,
1336*77c1e3ccSAndroid Build Coastguard Worker           mi_row, mi_col))
1337*77c1e3ccSAndroid Build Coastguard Worker     return;
1338*77c1e3ccSAndroid Build Coastguard Worker 
1339*77c1e3ccSAndroid Build Coastguard Worker   if (best_rd >= 1000000000) return;
1340*77c1e3ccSAndroid Build Coastguard Worker   int64_t *horz_rd = rect_part_rd[HORZ4];
1341*77c1e3ccSAndroid Build Coastguard Worker   int64_t *vert_rd = rect_part_rd[VERT4];
1342*77c1e3ccSAndroid Build Coastguard Worker   const NN_CONFIG *nn_config = NULL;
1343*77c1e3ccSAndroid Build Coastguard Worker   // 4-way partitions are only allowed for these three square block sizes.
1344*77c1e3ccSAndroid Build Coastguard Worker   switch (bsize) {
1345*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_16X16: nn_config = &av1_4_partition_nnconfig_16; break;
1346*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_32X32: nn_config = &av1_4_partition_nnconfig_32; break;
1347*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_64X64: nn_config = &av1_4_partition_nnconfig_64; break;
1348*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0 && "Unexpected bsize.");
1349*77c1e3ccSAndroid Build Coastguard Worker   }
1350*77c1e3ccSAndroid Build Coastguard Worker   if (!nn_config) return;
1351*77c1e3ccSAndroid Build Coastguard Worker 
1352*77c1e3ccSAndroid Build Coastguard Worker   // Generate features.
1353*77c1e3ccSAndroid Build Coastguard Worker   float features[FEATURES];
1354*77c1e3ccSAndroid Build Coastguard Worker   int feature_index = 0;
1355*77c1e3ccSAndroid Build Coastguard Worker   features[feature_index++] = (float)part_ctx;
1356*77c1e3ccSAndroid Build Coastguard Worker   features[feature_index++] = (float)get_unsigned_bits(pb_source_variance);
1357*77c1e3ccSAndroid Build Coastguard Worker 
1358*77c1e3ccSAndroid Build Coastguard Worker   const int rdcost = (int)AOMMIN(INT_MAX, best_rd);
1359*77c1e3ccSAndroid Build Coastguard Worker   int sub_block_rdcost[8] = { 0 };
1360*77c1e3ccSAndroid Build Coastguard Worker   int rd_index = 0;
1361*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
1362*77c1e3ccSAndroid Build Coastguard Worker     if (horz_rd[i] > 0 && horz_rd[i] < 1000000000)
1363*77c1e3ccSAndroid Build Coastguard Worker       sub_block_rdcost[rd_index] = (int)horz_rd[i];
1364*77c1e3ccSAndroid Build Coastguard Worker     ++rd_index;
1365*77c1e3ccSAndroid Build Coastguard Worker   }
1366*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
1367*77c1e3ccSAndroid Build Coastguard Worker     if (vert_rd[i] > 0 && vert_rd[i] < 1000000000)
1368*77c1e3ccSAndroid Build Coastguard Worker       sub_block_rdcost[rd_index] = (int)vert_rd[i];
1369*77c1e3ccSAndroid Build Coastguard Worker     ++rd_index;
1370*77c1e3ccSAndroid Build Coastguard Worker   }
1371*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
1372*77c1e3ccSAndroid Build Coastguard Worker     if (split_rd[i] > 0 && split_rd[i] < 1000000000)
1373*77c1e3ccSAndroid Build Coastguard Worker       sub_block_rdcost[rd_index] = (int)split_rd[i];
1374*77c1e3ccSAndroid Build Coastguard Worker     ++rd_index;
1375*77c1e3ccSAndroid Build Coastguard Worker   }
1376*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 8; ++i) {
1377*77c1e3ccSAndroid Build Coastguard Worker     // Ratio between the sub-block RD and the whole-block RD.
1378*77c1e3ccSAndroid Build Coastguard Worker     float rd_ratio = 1.0f;
1379*77c1e3ccSAndroid Build Coastguard Worker     if (sub_block_rdcost[i] > 0 && sub_block_rdcost[i] < rdcost)
1380*77c1e3ccSAndroid Build Coastguard Worker       rd_ratio = (float)sub_block_rdcost[i] / (float)rdcost;
1381*77c1e3ccSAndroid Build Coastguard Worker     features[feature_index++] = rd_ratio;
1382*77c1e3ccSAndroid Build Coastguard Worker   }
1383*77c1e3ccSAndroid Build Coastguard Worker 
1384*77c1e3ccSAndroid Build Coastguard Worker   // Get variance of the 1:4 and 4:1 sub-blocks.
1385*77c1e3ccSAndroid Build Coastguard Worker   unsigned int horz_4_source_var[SUB_PARTITIONS_PART4] = { 0 };
1386*77c1e3ccSAndroid Build Coastguard Worker   unsigned int vert_4_source_var[SUB_PARTITIONS_PART4] = { 0 };
1387*77c1e3ccSAndroid Build Coastguard Worker   {
1388*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE horz_4_bs = get_partition_subsize(bsize, PARTITION_HORZ_4);
1389*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE vert_4_bs = get_partition_subsize(bsize, PARTITION_VERT_4);
1390*77c1e3ccSAndroid Build Coastguard Worker 
1391*77c1e3ccSAndroid Build Coastguard Worker     assert(horz_4_bs != BLOCK_INVALID);
1392*77c1e3ccSAndroid Build Coastguard Worker     assert(vert_4_bs != BLOCK_INVALID);
1393*77c1e3ccSAndroid Build Coastguard Worker 
1394*77c1e3ccSAndroid Build Coastguard Worker     av1_setup_src_planes(x, cpi->source, mi_row, mi_col,
1395*77c1e3ccSAndroid Build Coastguard Worker                          av1_num_planes(&cpi->common), bsize);
1396*77c1e3ccSAndroid Build Coastguard Worker     const int src_stride = x->plane[0].src.stride;
1397*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *src = x->plane[0].src.buf;
1398*77c1e3ccSAndroid Build Coastguard Worker     const MACROBLOCKD *const xd = &x->e_mbd;
1399*77c1e3ccSAndroid Build Coastguard Worker 
1400*77c1e3ccSAndroid Build Coastguard Worker     struct buf_2d horz_4_src, vert_4_src;
1401*77c1e3ccSAndroid Build Coastguard Worker     horz_4_src.stride = src_stride;
1402*77c1e3ccSAndroid Build Coastguard Worker     vert_4_src.stride = src_stride;
1403*77c1e3ccSAndroid Build Coastguard Worker 
1404*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < SUB_PARTITIONS_PART4; ++i) {
1405*77c1e3ccSAndroid Build Coastguard Worker       horz_4_src.buf = src + i * block_size_high[horz_4_bs] * src_stride;
1406*77c1e3ccSAndroid Build Coastguard Worker       vert_4_src.buf = src + i * block_size_wide[vert_4_bs];
1407*77c1e3ccSAndroid Build Coastguard Worker 
1408*77c1e3ccSAndroid Build Coastguard Worker       horz_4_source_var[i] = av1_get_perpixel_variance_facade(
1409*77c1e3ccSAndroid Build Coastguard Worker           cpi, xd, &horz_4_src, horz_4_bs, AOM_PLANE_Y);
1410*77c1e3ccSAndroid Build Coastguard Worker       vert_4_source_var[i] = av1_get_perpixel_variance_facade(
1411*77c1e3ccSAndroid Build Coastguard Worker           cpi, xd, &vert_4_src, vert_4_bs, AOM_PLANE_Y);
1412*77c1e3ccSAndroid Build Coastguard Worker     }
1413*77c1e3ccSAndroid Build Coastguard Worker   }
1414*77c1e3ccSAndroid Build Coastguard Worker 
1415*77c1e3ccSAndroid Build Coastguard Worker   const float denom = (float)(pb_source_variance + 1);
1416*77c1e3ccSAndroid Build Coastguard Worker   const float low_b = 0.1f;
1417*77c1e3ccSAndroid Build Coastguard Worker   const float high_b = 10.0f;
1418*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_PART4; ++i) {
1419*77c1e3ccSAndroid Build Coastguard Worker     // Ratio between the 4:1 sub-block variance and the whole-block variance.
1420*77c1e3ccSAndroid Build Coastguard Worker     float var_ratio = (float)(horz_4_source_var[i] + 1) / denom;
1421*77c1e3ccSAndroid Build Coastguard Worker     if (var_ratio < low_b) var_ratio = low_b;
1422*77c1e3ccSAndroid Build Coastguard Worker     if (var_ratio > high_b) var_ratio = high_b;
1423*77c1e3ccSAndroid Build Coastguard Worker     features[feature_index++] = var_ratio;
1424*77c1e3ccSAndroid Build Coastguard Worker   }
1425*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_PART4; ++i) {
1426*77c1e3ccSAndroid Build Coastguard Worker     // Ratio between the 1:4 sub-block RD and the whole-block RD.
1427*77c1e3ccSAndroid Build Coastguard Worker     float var_ratio = (float)(vert_4_source_var[i] + 1) / denom;
1428*77c1e3ccSAndroid Build Coastguard Worker     if (var_ratio < low_b) var_ratio = low_b;
1429*77c1e3ccSAndroid Build Coastguard Worker     if (var_ratio > high_b) var_ratio = high_b;
1430*77c1e3ccSAndroid Build Coastguard Worker     features[feature_index++] = var_ratio;
1431*77c1e3ccSAndroid Build Coastguard Worker   }
1432*77c1e3ccSAndroid Build Coastguard Worker   assert(feature_index == FEATURES);
1433*77c1e3ccSAndroid Build Coastguard Worker 
1434*77c1e3ccSAndroid Build Coastguard Worker   // Write features to file
1435*77c1e3ccSAndroid Build Coastguard Worker   if (!frame_is_intra_only(&cpi->common)) {
1436*77c1e3ccSAndroid Build Coastguard Worker     write_features_to_file(cpi->oxcf.partition_info_path,
1437*77c1e3ccSAndroid Build Coastguard Worker                            cpi->ext_part_controller.test_mode, features,
1438*77c1e3ccSAndroid Build Coastguard Worker                            FEATURES, 7, bsize, mi_row, mi_col);
1439*77c1e3ccSAndroid Build Coastguard Worker   }
1440*77c1e3ccSAndroid Build Coastguard Worker 
1441*77c1e3ccSAndroid Build Coastguard Worker   // Calculate scores using the NN model.
1442*77c1e3ccSAndroid Build Coastguard Worker   float score[LABELS] = { 0.0f };
1443*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_predict(features, nn_config, 1, score);
1444*77c1e3ccSAndroid Build Coastguard Worker   int int_score[LABELS];
1445*77c1e3ccSAndroid Build Coastguard Worker   int max_score = -1000;
1446*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < LABELS; ++i) {
1447*77c1e3ccSAndroid Build Coastguard Worker     int_score[i] = (int)(100 * score[i]);
1448*77c1e3ccSAndroid Build Coastguard Worker     max_score = AOMMAX(int_score[i], max_score);
1449*77c1e3ccSAndroid Build Coastguard Worker   }
1450*77c1e3ccSAndroid Build Coastguard Worker 
1451*77c1e3ccSAndroid Build Coastguard Worker   // Make decisions based on the model scores.
1452*77c1e3ccSAndroid Build Coastguard Worker   int thresh = max_score;
1453*77c1e3ccSAndroid Build Coastguard Worker   switch (bsize) {
1454*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_16X16: thresh -= 500; break;
1455*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_32X32: thresh -= 500; break;
1456*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_64X64: thresh -= 200; break;
1457*77c1e3ccSAndroid Build Coastguard Worker     default: break;
1458*77c1e3ccSAndroid Build Coastguard Worker   }
1459*77c1e3ccSAndroid Build Coastguard Worker   av1_zero_array(part4_allowed, NUM_PART4_TYPES);
1460*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < LABELS; ++i) {
1461*77c1e3ccSAndroid Build Coastguard Worker     if (int_score[i] >= thresh) {
1462*77c1e3ccSAndroid Build Coastguard Worker       if ((i >> 0) & 1) part4_allowed[HORZ4] = 1;
1463*77c1e3ccSAndroid Build Coastguard Worker       if ((i >> 1) & 1) part4_allowed[VERT4] = 1;
1464*77c1e3ccSAndroid Build Coastguard Worker     }
1465*77c1e3ccSAndroid Build Coastguard Worker   }
1466*77c1e3ccSAndroid Build Coastguard Worker }
1467*77c1e3ccSAndroid Build Coastguard Worker #undef FEATURES
1468*77c1e3ccSAndroid Build Coastguard Worker #undef LABELS
1469*77c1e3ccSAndroid Build Coastguard Worker 
1470*77c1e3ccSAndroid Build Coastguard Worker #define FEATURES 4
av1_ml_predict_breakout(AV1_COMP * const cpi,const MACROBLOCK * const x,const RD_STATS * const rd_stats,unsigned int pb_source_variance,int bit_depth,PartitionSearchState * part_state)1471*77c1e3ccSAndroid Build Coastguard Worker void av1_ml_predict_breakout(AV1_COMP *const cpi, const MACROBLOCK *const x,
1472*77c1e3ccSAndroid Build Coastguard Worker                              const RD_STATS *const rd_stats,
1473*77c1e3ccSAndroid Build Coastguard Worker                              unsigned int pb_source_variance, int bit_depth,
1474*77c1e3ccSAndroid Build Coastguard Worker                              PartitionSearchState *part_state) {
1475*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams *blk_params = &part_state->part_blk_params;
1476*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params->mi_row, mi_col = blk_params->mi_col;
1477*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params->bsize;
1478*77c1e3ccSAndroid Build Coastguard Worker 
1479*77c1e3ccSAndroid Build Coastguard Worker   const NN_CONFIG *nn_config = NULL;
1480*77c1e3ccSAndroid Build Coastguard Worker   int thresh = 0;
1481*77c1e3ccSAndroid Build Coastguard Worker   switch (bsize) {
1482*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_8X8:
1483*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_partition_breakout_nnconfig_8;
1484*77c1e3ccSAndroid Build Coastguard Worker       thresh = cpi->sf.part_sf.ml_partition_search_breakout_thresh[0];
1485*77c1e3ccSAndroid Build Coastguard Worker       break;
1486*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_16X16:
1487*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_partition_breakout_nnconfig_16;
1488*77c1e3ccSAndroid Build Coastguard Worker       thresh = cpi->sf.part_sf.ml_partition_search_breakout_thresh[1];
1489*77c1e3ccSAndroid Build Coastguard Worker       break;
1490*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_32X32:
1491*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_partition_breakout_nnconfig_32;
1492*77c1e3ccSAndroid Build Coastguard Worker       thresh = cpi->sf.part_sf.ml_partition_search_breakout_thresh[2];
1493*77c1e3ccSAndroid Build Coastguard Worker       break;
1494*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_64X64:
1495*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_partition_breakout_nnconfig_64;
1496*77c1e3ccSAndroid Build Coastguard Worker       thresh = cpi->sf.part_sf.ml_partition_search_breakout_thresh[3];
1497*77c1e3ccSAndroid Build Coastguard Worker       break;
1498*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_128X128:
1499*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_partition_breakout_nnconfig_128;
1500*77c1e3ccSAndroid Build Coastguard Worker       thresh = cpi->sf.part_sf.ml_partition_search_breakout_thresh[4];
1501*77c1e3ccSAndroid Build Coastguard Worker       break;
1502*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0 && "Unexpected bsize.");
1503*77c1e3ccSAndroid Build Coastguard Worker   }
1504*77c1e3ccSAndroid Build Coastguard Worker   if (!nn_config || thresh < 0) return;
1505*77c1e3ccSAndroid Build Coastguard Worker 
1506*77c1e3ccSAndroid Build Coastguard Worker   const float ml_predict_breakout_thresh_scale[3] = { 1.15f, 1.05f, 1.0f };
1507*77c1e3ccSAndroid Build Coastguard Worker   thresh = (int)((float)thresh *
1508*77c1e3ccSAndroid Build Coastguard Worker                  ml_predict_breakout_thresh_scale
1509*77c1e3ccSAndroid Build Coastguard Worker                      [cpi->sf.part_sf.ml_predict_breakout_level - 1]);
1510*77c1e3ccSAndroid Build Coastguard Worker 
1511*77c1e3ccSAndroid Build Coastguard Worker   // Generate feature values.
1512*77c1e3ccSAndroid Build Coastguard Worker   float features[FEATURES];
1513*77c1e3ccSAndroid Build Coastguard Worker   int feature_index = 0;
1514*77c1e3ccSAndroid Build Coastguard Worker 
1515*77c1e3ccSAndroid Build Coastguard Worker   const int num_pels_log2 = num_pels_log2_lookup[bsize];
1516*77c1e3ccSAndroid Build Coastguard Worker   float rate_f = (float)AOMMIN(rd_stats->rate, INT_MAX);
1517*77c1e3ccSAndroid Build Coastguard Worker   rate_f = ((float)x->rdmult / 128.0f / 512.0f / (float)(1 << num_pels_log2)) *
1518*77c1e3ccSAndroid Build Coastguard Worker            rate_f;
1519*77c1e3ccSAndroid Build Coastguard Worker   features[feature_index++] = rate_f;
1520*77c1e3ccSAndroid Build Coastguard Worker 
1521*77c1e3ccSAndroid Build Coastguard Worker   const float dist_f =
1522*77c1e3ccSAndroid Build Coastguard Worker       (float)(AOMMIN(rd_stats->dist, INT_MAX) >> num_pels_log2);
1523*77c1e3ccSAndroid Build Coastguard Worker   features[feature_index++] = dist_f;
1524*77c1e3ccSAndroid Build Coastguard Worker 
1525*77c1e3ccSAndroid Build Coastguard Worker   features[feature_index++] = (float)pb_source_variance;
1526*77c1e3ccSAndroid Build Coastguard Worker 
1527*77c1e3ccSAndroid Build Coastguard Worker   const int dc_q = (int)x->plane[0].dequant_QTX[0] >> (bit_depth - 8);
1528*77c1e3ccSAndroid Build Coastguard Worker   features[feature_index++] = (float)(dc_q * dc_q) / 256.0f;
1529*77c1e3ccSAndroid Build Coastguard Worker   assert(feature_index == FEATURES);
1530*77c1e3ccSAndroid Build Coastguard Worker 
1531*77c1e3ccSAndroid Build Coastguard Worker   // Write features to file
1532*77c1e3ccSAndroid Build Coastguard Worker   write_features_to_file(cpi->oxcf.partition_info_path,
1533*77c1e3ccSAndroid Build Coastguard Worker                          cpi->ext_part_controller.test_mode, features, FEATURES,
1534*77c1e3ccSAndroid Build Coastguard Worker                          2, bsize, mi_row, mi_col);
1535*77c1e3ccSAndroid Build Coastguard Worker 
1536*77c1e3ccSAndroid Build Coastguard Worker   if (ext_ml_model_decision_after_none(&cpi->ext_part_controller,
1537*77c1e3ccSAndroid Build Coastguard Worker                                        frame_is_intra_only(&cpi->common),
1538*77c1e3ccSAndroid Build Coastguard Worker                                        features, &part_state->do_square_split,
1539*77c1e3ccSAndroid Build Coastguard Worker                                        &part_state->do_rectangular_split)) {
1540*77c1e3ccSAndroid Build Coastguard Worker     return;
1541*77c1e3ccSAndroid Build Coastguard Worker   }
1542*77c1e3ccSAndroid Build Coastguard Worker 
1543*77c1e3ccSAndroid Build Coastguard Worker   // Calculate score using the NN model.
1544*77c1e3ccSAndroid Build Coastguard Worker   float score = 0.0f;
1545*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_predict(features, nn_config, 1, &score);
1546*77c1e3ccSAndroid Build Coastguard Worker 
1547*77c1e3ccSAndroid Build Coastguard Worker   // Make decision.
1548*77c1e3ccSAndroid Build Coastguard Worker   if ((int)(score * 100) >= thresh) {
1549*77c1e3ccSAndroid Build Coastguard Worker     part_state->do_square_split = 0;
1550*77c1e3ccSAndroid Build Coastguard Worker     part_state->do_rectangular_split = 0;
1551*77c1e3ccSAndroid Build Coastguard Worker   }
1552*77c1e3ccSAndroid Build Coastguard Worker }
1553*77c1e3ccSAndroid Build Coastguard Worker #undef FEATURES
1554*77c1e3ccSAndroid Build Coastguard Worker 
av1_prune_partitions_before_search(AV1_COMP * const cpi,MACROBLOCK * const x,SIMPLE_MOTION_DATA_TREE * const sms_tree,PartitionSearchState * part_state)1555*77c1e3ccSAndroid Build Coastguard Worker void av1_prune_partitions_before_search(AV1_COMP *const cpi,
1556*77c1e3ccSAndroid Build Coastguard Worker                                         MACROBLOCK *const x,
1557*77c1e3ccSAndroid Build Coastguard Worker                                         SIMPLE_MOTION_DATA_TREE *const sms_tree,
1558*77c1e3ccSAndroid Build Coastguard Worker                                         PartitionSearchState *part_state) {
1559*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
1560*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
1561*77c1e3ccSAndroid Build Coastguard Worker 
1562*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams *blk_params = &part_state->part_blk_params;
1563*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params->bsize;
1564*77c1e3ccSAndroid Build Coastguard Worker 
1565*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_THREE_PASS
1566*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->third_pass_ctx) {
1567*77c1e3ccSAndroid Build Coastguard Worker     int mi_row = blk_params->mi_row;
1568*77c1e3ccSAndroid Build Coastguard Worker     int mi_col = blk_params->mi_col;
1569*77c1e3ccSAndroid Build Coastguard Worker     double ratio_h, ratio_w;
1570*77c1e3ccSAndroid Build Coastguard Worker     av1_get_third_pass_ratio(cpi->third_pass_ctx, 0, cm->height, cm->width,
1571*77c1e3ccSAndroid Build Coastguard Worker                              &ratio_h, &ratio_w);
1572*77c1e3ccSAndroid Build Coastguard Worker     THIRD_PASS_MI_INFO *this_mi = av1_get_third_pass_mi(
1573*77c1e3ccSAndroid Build Coastguard Worker         cpi->third_pass_ctx, 0, mi_row, mi_col, ratio_h, ratio_w);
1574*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE third_pass_bsize =
1575*77c1e3ccSAndroid Build Coastguard Worker         av1_get_third_pass_adjusted_blk_size(this_mi, ratio_h, ratio_w);
1576*77c1e3ccSAndroid Build Coastguard Worker     // check the actual partition of this block in the second pass
1577*77c1e3ccSAndroid Build Coastguard Worker     PARTITION_TYPE third_pass_part =
1578*77c1e3ccSAndroid Build Coastguard Worker         av1_third_pass_get_sb_part_type(cpi->third_pass_ctx, this_mi);
1579*77c1e3ccSAndroid Build Coastguard Worker 
1580*77c1e3ccSAndroid Build Coastguard Worker     int is_edge = (mi_row + mi_size_high[bsize] >= cm->mi_params.mi_rows) ||
1581*77c1e3ccSAndroid Build Coastguard Worker                   (mi_col + mi_size_wide[bsize] >= cm->mi_params.mi_cols);
1582*77c1e3ccSAndroid Build Coastguard Worker 
1583*77c1e3ccSAndroid Build Coastguard Worker     if (!is_edge && block_size_wide[bsize] >= 16) {
1584*77c1e3ccSAndroid Build Coastguard Worker       // If in second pass we used rectangular partition, then do not search for
1585*77c1e3ccSAndroid Build Coastguard Worker       // rectangular partition in the different direction.
1586*77c1e3ccSAndroid Build Coastguard Worker       if (third_pass_part != PARTITION_NONE) {
1587*77c1e3ccSAndroid Build Coastguard Worker         if (third_pass_part == PARTITION_HORZ ||
1588*77c1e3ccSAndroid Build Coastguard Worker             third_pass_part == PARTITION_HORZ_4 ||
1589*77c1e3ccSAndroid Build Coastguard Worker             third_pass_part == PARTITION_HORZ_A ||
1590*77c1e3ccSAndroid Build Coastguard Worker             third_pass_part == PARTITION_HORZ_B) {
1591*77c1e3ccSAndroid Build Coastguard Worker           part_state->partition_rect_allowed[VERT] = 0;
1592*77c1e3ccSAndroid Build Coastguard Worker         } else if (third_pass_part == PARTITION_VERT ||
1593*77c1e3ccSAndroid Build Coastguard Worker                    third_pass_part == PARTITION_VERT_4 ||
1594*77c1e3ccSAndroid Build Coastguard Worker                    third_pass_part == PARTITION_VERT_A ||
1595*77c1e3ccSAndroid Build Coastguard Worker                    third_pass_part == PARTITION_VERT_B) {
1596*77c1e3ccSAndroid Build Coastguard Worker           part_state->partition_rect_allowed[HORZ] = 0;
1597*77c1e3ccSAndroid Build Coastguard Worker         }
1598*77c1e3ccSAndroid Build Coastguard Worker       }
1599*77c1e3ccSAndroid Build Coastguard Worker 
1600*77c1e3ccSAndroid Build Coastguard Worker       int minSize = AOMMIN(block_size_wide[third_pass_bsize],
1601*77c1e3ccSAndroid Build Coastguard Worker                            block_size_high[third_pass_bsize]);
1602*77c1e3ccSAndroid Build Coastguard Worker       int maxSize = AOMMAX(block_size_wide[third_pass_bsize],
1603*77c1e3ccSAndroid Build Coastguard Worker                            block_size_high[third_pass_bsize]);
1604*77c1e3ccSAndroid Build Coastguard Worker       if (block_size_wide[bsize] < minSize / 4) {
1605*77c1e3ccSAndroid Build Coastguard Worker         // Current partition is too small, just terminate
1606*77c1e3ccSAndroid Build Coastguard Worker         part_state->terminate_partition_search = 1;
1607*77c1e3ccSAndroid Build Coastguard Worker         return;
1608*77c1e3ccSAndroid Build Coastguard Worker       } else if (block_size_wide[bsize] < minSize / 2) {
1609*77c1e3ccSAndroid Build Coastguard Worker         if (third_pass_part != PARTITION_NONE) {
1610*77c1e3ccSAndroid Build Coastguard Worker           // Current partition is very small, and in second pass we used
1611*77c1e3ccSAndroid Build Coastguard Worker           // rectangular partition. Terminate the search here then.
1612*77c1e3ccSAndroid Build Coastguard Worker           part_state->terminate_partition_search = 1;
1613*77c1e3ccSAndroid Build Coastguard Worker           return;
1614*77c1e3ccSAndroid Build Coastguard Worker         } else {
1615*77c1e3ccSAndroid Build Coastguard Worker           // Partition is small, but we still check this partition, only disable
1616*77c1e3ccSAndroid Build Coastguard Worker           // further splits.
1617*77c1e3ccSAndroid Build Coastguard Worker           // TODO(any): check why this is not covered by the termination for <
1618*77c1e3ccSAndroid Build Coastguard Worker           // minSize/4.
1619*77c1e3ccSAndroid Build Coastguard Worker           av1_disable_square_split_partition(part_state);
1620*77c1e3ccSAndroid Build Coastguard Worker           av1_disable_rect_partitions(part_state);
1621*77c1e3ccSAndroid Build Coastguard Worker           return;
1622*77c1e3ccSAndroid Build Coastguard Worker         }
1623*77c1e3ccSAndroid Build Coastguard Worker       } else if (block_size_wide[bsize] > maxSize) {
1624*77c1e3ccSAndroid Build Coastguard Worker         // Partition is larger than in the second pass. Only allow split.
1625*77c1e3ccSAndroid Build Coastguard Worker         av1_set_square_split_only(part_state);
1626*77c1e3ccSAndroid Build Coastguard Worker         return;
1627*77c1e3ccSAndroid Build Coastguard Worker       } else if (block_size_wide[bsize] >= minSize &&
1628*77c1e3ccSAndroid Build Coastguard Worker                  block_size_wide[bsize] <= maxSize) {
1629*77c1e3ccSAndroid Build Coastguard Worker         // Partition is within a range where it is very likely to find a good
1630*77c1e3ccSAndroid Build Coastguard Worker         // choice, so do not prune anything.
1631*77c1e3ccSAndroid Build Coastguard Worker         return;
1632*77c1e3ccSAndroid Build Coastguard Worker       }
1633*77c1e3ccSAndroid Build Coastguard Worker     }
1634*77c1e3ccSAndroid Build Coastguard Worker   }
1635*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_THREE_PASS
1636*77c1e3ccSAndroid Build Coastguard Worker 
1637*77c1e3ccSAndroid Build Coastguard Worker   // Prune rectangular partitions for larger blocks.
1638*77c1e3ccSAndroid Build Coastguard Worker   if (bsize > cpi->sf.part_sf.rect_partition_eval_thresh) {
1639*77c1e3ccSAndroid Build Coastguard Worker     part_state->do_rectangular_split = 0;
1640*77c1e3ccSAndroid Build Coastguard Worker     part_state->partition_rect_allowed[HORZ] = 0;
1641*77c1e3ccSAndroid Build Coastguard Worker     part_state->partition_rect_allowed[VERT] = 0;
1642*77c1e3ccSAndroid Build Coastguard Worker   }
1643*77c1e3ccSAndroid Build Coastguard Worker 
1644*77c1e3ccSAndroid Build Coastguard Worker   // Prune rectangular, AB and 4-way partition based on q index and block size
1645*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.prune_rectangular_split_based_on_qidx == 1) {
1646*77c1e3ccSAndroid Build Coastguard Worker     if (bsize == BLOCK_8X8 && x->qindex < 35)
1647*77c1e3ccSAndroid Build Coastguard Worker       av1_disable_rect_partitions(part_state);
1648*77c1e3ccSAndroid Build Coastguard Worker 
1649*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->sf.part_sf.prune_rectangular_split_based_on_qidx == 2) {
1650*77c1e3ccSAndroid Build Coastguard Worker     // Enumeration difference between two square partitions
1651*77c1e3ccSAndroid Build Coastguard Worker     const int sqr_bsize_step = BLOCK_32X32 - BLOCK_16X16;
1652*77c1e3ccSAndroid Build Coastguard Worker     int max_bsize =
1653*77c1e3ccSAndroid Build Coastguard Worker         BLOCK_32X32 - (x->qindex * 3 / QINDEX_RANGE) * sqr_bsize_step;
1654*77c1e3ccSAndroid Build Coastguard Worker     max_bsize = AOMMAX(max_bsize, BLOCK_4X4);
1655*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE max_prune_bsize =
1656*77c1e3ccSAndroid Build Coastguard Worker         (BLOCK_SIZE)AOMMIN(max_bsize, BLOCK_32X32);
1657*77c1e3ccSAndroid Build Coastguard Worker 
1658*77c1e3ccSAndroid Build Coastguard Worker     // Prune partition
1659*77c1e3ccSAndroid Build Coastguard Worker     // qidx 0 to 85: prune bsize below BLOCK_32X32
1660*77c1e3ccSAndroid Build Coastguard Worker     // qidx 86 to 170: prune bsize below BLOCK_16X16
1661*77c1e3ccSAndroid Build Coastguard Worker     // qidx 171 to 255: prune bsize below BLOCK_8X8
1662*77c1e3ccSAndroid Build Coastguard Worker     if (bsize < max_prune_bsize) {
1663*77c1e3ccSAndroid Build Coastguard Worker       av1_disable_rect_partitions(part_state);
1664*77c1e3ccSAndroid Build Coastguard Worker     }
1665*77c1e3ccSAndroid Build Coastguard Worker   }
1666*77c1e3ccSAndroid Build Coastguard Worker 
1667*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.prune_sub_8x8_partition_level && (bsize == BLOCK_8X8)) {
1668*77c1e3ccSAndroid Build Coastguard Worker     const MACROBLOCKD *const xd = &x->e_mbd;
1669*77c1e3ccSAndroid Build Coastguard Worker     int prune_sub_8x8;
1670*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->sf.part_sf.prune_sub_8x8_partition_level == 2) {
1671*77c1e3ccSAndroid Build Coastguard Worker       prune_sub_8x8 = 1;
1672*77c1e3ccSAndroid Build Coastguard Worker     } else {
1673*77c1e3ccSAndroid Build Coastguard Worker       assert(cpi->sf.part_sf.prune_sub_8x8_partition_level == 1);
1674*77c1e3ccSAndroid Build Coastguard Worker       // Prune if both neighbors are available and either is > BLOCK_8X8
1675*77c1e3ccSAndroid Build Coastguard Worker       prune_sub_8x8 = xd->left_available && xd->up_available &&
1676*77c1e3ccSAndroid Build Coastguard Worker                       (xd->left_mbmi->bsize > BLOCK_8X8 ||
1677*77c1e3ccSAndroid Build Coastguard Worker                        xd->above_mbmi->bsize > BLOCK_8X8);
1678*77c1e3ccSAndroid Build Coastguard Worker     }
1679*77c1e3ccSAndroid Build Coastguard Worker     if (prune_sub_8x8) {
1680*77c1e3ccSAndroid Build Coastguard Worker       av1_disable_all_splits(part_state);
1681*77c1e3ccSAndroid Build Coastguard Worker     }
1682*77c1e3ccSAndroid Build Coastguard Worker   }
1683*77c1e3ccSAndroid Build Coastguard Worker 
1684*77c1e3ccSAndroid Build Coastguard Worker   // A CNN-based speed feature pruning out either split or all non-split
1685*77c1e3ccSAndroid Build Coastguard Worker   // partition in INTRA frame coding.
1686*77c1e3ccSAndroid Build Coastguard Worker   const int try_intra_cnn_based_part_prune =
1687*77c1e3ccSAndroid Build Coastguard Worker       frame_is_intra_only(cm) &&
1688*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.part_sf.intra_cnn_based_part_prune_level &&
1689*77c1e3ccSAndroid Build Coastguard Worker       cm->seq_params->sb_size >= BLOCK_64X64 && bsize <= BLOCK_64X64 &&
1690*77c1e3ccSAndroid Build Coastguard Worker       blk_params->bsize_at_least_8x8 &&
1691*77c1e3ccSAndroid Build Coastguard Worker       av1_is_whole_blk_in_frame(blk_params, mi_params);
1692*77c1e3ccSAndroid Build Coastguard Worker 
1693*77c1e3ccSAndroid Build Coastguard Worker   if (try_intra_cnn_based_part_prune) {
1694*77c1e3ccSAndroid Build Coastguard Worker     intra_mode_cnn_partition(&cpi->common, x, x->part_search_info.quad_tree_idx,
1695*77c1e3ccSAndroid Build Coastguard Worker                              cpi->sf.part_sf.intra_cnn_based_part_prune_level,
1696*77c1e3ccSAndroid Build Coastguard Worker                              part_state);
1697*77c1e3ccSAndroid Build Coastguard Worker   }
1698*77c1e3ccSAndroid Build Coastguard Worker 
1699*77c1e3ccSAndroid Build Coastguard Worker   // Use simple motion search to prune out split or non-split partitions. This
1700*77c1e3ccSAndroid Build Coastguard Worker   // must be done prior to PARTITION_SPLIT to propagate the initial mvs to a
1701*77c1e3ccSAndroid Build Coastguard Worker   // smaller blocksize.
1702*77c1e3ccSAndroid Build Coastguard Worker   const int try_split_only =
1703*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.part_sf.simple_motion_search_split &&
1704*77c1e3ccSAndroid Build Coastguard Worker       part_state->do_square_split && blk_params->bsize_at_least_8x8 &&
1705*77c1e3ccSAndroid Build Coastguard Worker       av1_is_whole_blk_in_frame(blk_params, mi_params) &&
1706*77c1e3ccSAndroid Build Coastguard Worker       !frame_is_intra_only(cm) && !av1_superres_scaled(cm);
1707*77c1e3ccSAndroid Build Coastguard Worker 
1708*77c1e3ccSAndroid Build Coastguard Worker   if (try_split_only) {
1709*77c1e3ccSAndroid Build Coastguard Worker     simple_motion_search_based_split(cpi, x, sms_tree, part_state);
1710*77c1e3ccSAndroid Build Coastguard Worker   }
1711*77c1e3ccSAndroid Build Coastguard Worker 
1712*77c1e3ccSAndroid Build Coastguard Worker   // Use simple motion search to prune out rectangular partition in some
1713*77c1e3ccSAndroid Build Coastguard Worker   // direction. The results are stored in prune_horz and prune_vert in order to
1714*77c1e3ccSAndroid Build Coastguard Worker   // bypass future related pruning checks if a pruning decision has been made.
1715*77c1e3ccSAndroid Build Coastguard Worker 
1716*77c1e3ccSAndroid Build Coastguard Worker   // We want to search at least one partition mode, so don't prune if NONE and
1717*77c1e3ccSAndroid Build Coastguard Worker   // SPLIT are disabled.
1718*77c1e3ccSAndroid Build Coastguard Worker   const int non_rect_part_allowed =
1719*77c1e3ccSAndroid Build Coastguard Worker       part_state->do_square_split || part_state->partition_none_allowed;
1720*77c1e3ccSAndroid Build Coastguard Worker   // Only run the model if the partitions are not already pruned.
1721*77c1e3ccSAndroid Build Coastguard Worker   const int rect_part_allowed = part_state->do_rectangular_split &&
1722*77c1e3ccSAndroid Build Coastguard Worker                                 ((part_state->partition_rect_allowed[HORZ] &&
1723*77c1e3ccSAndroid Build Coastguard Worker                                   !part_state->prune_rect_part[HORZ]) ||
1724*77c1e3ccSAndroid Build Coastguard Worker                                  (part_state->partition_rect_allowed[VERT] &&
1725*77c1e3ccSAndroid Build Coastguard Worker                                   !part_state->prune_rect_part[VERT]));
1726*77c1e3ccSAndroid Build Coastguard Worker 
1727*77c1e3ccSAndroid Build Coastguard Worker   const int try_prune_rect = cpi->sf.part_sf.simple_motion_search_prune_rect &&
1728*77c1e3ccSAndroid Build Coastguard Worker                              !frame_is_intra_only(cm) &&
1729*77c1e3ccSAndroid Build Coastguard Worker                              non_rect_part_allowed && rect_part_allowed &&
1730*77c1e3ccSAndroid Build Coastguard Worker                              !av1_superres_scaled(cm);
1731*77c1e3ccSAndroid Build Coastguard Worker 
1732*77c1e3ccSAndroid Build Coastguard Worker   if (try_prune_rect) {
1733*77c1e3ccSAndroid Build Coastguard Worker     simple_motion_search_prune_rect(cpi, x, sms_tree, part_state);
1734*77c1e3ccSAndroid Build Coastguard Worker   }
1735*77c1e3ccSAndroid Build Coastguard Worker }
1736*77c1e3ccSAndroid Build Coastguard Worker 
1737*77c1e3ccSAndroid Build Coastguard Worker #ifndef NDEBUG
is_bsize_square(BLOCK_SIZE bsize)1738*77c1e3ccSAndroid Build Coastguard Worker static inline int is_bsize_square(BLOCK_SIZE bsize) {
1739*77c1e3ccSAndroid Build Coastguard Worker   return block_size_wide[bsize] == block_size_high[bsize];
1740*77c1e3ccSAndroid Build Coastguard Worker }
1741*77c1e3ccSAndroid Build Coastguard Worker #endif  // NDEBUG
1742*77c1e3ccSAndroid Build Coastguard Worker 
av1_prune_partitions_by_max_min_bsize(SuperBlockEnc * sb_enc,PartitionSearchState * part_state)1743*77c1e3ccSAndroid Build Coastguard Worker void av1_prune_partitions_by_max_min_bsize(SuperBlockEnc *sb_enc,
1744*77c1e3ccSAndroid Build Coastguard Worker                                            PartitionSearchState *part_state) {
1745*77c1e3ccSAndroid Build Coastguard Worker   assert(is_bsize_square(sb_enc->max_partition_size));
1746*77c1e3ccSAndroid Build Coastguard Worker   assert(is_bsize_square(sb_enc->min_partition_size));
1747*77c1e3ccSAndroid Build Coastguard Worker   assert(sb_enc->min_partition_size <= sb_enc->max_partition_size);
1748*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams *blk_params = &part_state->part_blk_params;
1749*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params->bsize;
1750*77c1e3ccSAndroid Build Coastguard Worker   assert(is_bsize_square(bsize));
1751*77c1e3ccSAndroid Build Coastguard Worker   const int max_partition_size_1d = block_size_wide[sb_enc->max_partition_size];
1752*77c1e3ccSAndroid Build Coastguard Worker   const int min_partition_size_1d = block_size_wide[sb_enc->min_partition_size];
1753*77c1e3ccSAndroid Build Coastguard Worker   const int bsize_1d = block_size_wide[bsize];
1754*77c1e3ccSAndroid Build Coastguard Worker   assert(min_partition_size_1d <= max_partition_size_1d);
1755*77c1e3ccSAndroid Build Coastguard Worker   const int is_le_min_sq_part = bsize_1d <= min_partition_size_1d;
1756*77c1e3ccSAndroid Build Coastguard Worker   const int is_gt_max_sq_part = bsize_1d > max_partition_size_1d;
1757*77c1e3ccSAndroid Build Coastguard Worker   if (is_gt_max_sq_part) {
1758*77c1e3ccSAndroid Build Coastguard Worker     // If current block size is larger than max, only allow split.
1759*77c1e3ccSAndroid Build Coastguard Worker     av1_set_square_split_only(part_state);
1760*77c1e3ccSAndroid Build Coastguard Worker   } else if (is_le_min_sq_part) {
1761*77c1e3ccSAndroid Build Coastguard Worker     // If current block size is less or equal to min, only allow none if valid
1762*77c1e3ccSAndroid Build Coastguard Worker     // block large enough; only allow split otherwise.
1763*77c1e3ccSAndroid Build Coastguard Worker     av1_disable_rect_partitions(part_state);
1764*77c1e3ccSAndroid Build Coastguard Worker 
1765*77c1e3ccSAndroid Build Coastguard Worker     // only disable square split when current block is not at the picture
1766*77c1e3ccSAndroid Build Coastguard Worker     // boundary. otherwise, inherit the square split flag from previous logic
1767*77c1e3ccSAndroid Build Coastguard Worker     if (av1_blk_has_rows_and_cols(blk_params)) {
1768*77c1e3ccSAndroid Build Coastguard Worker       part_state->do_square_split = 0;
1769*77c1e3ccSAndroid Build Coastguard Worker     }
1770*77c1e3ccSAndroid Build Coastguard Worker     part_state->partition_none_allowed = !(part_state->do_square_split);
1771*77c1e3ccSAndroid Build Coastguard Worker   }
1772*77c1e3ccSAndroid Build Coastguard Worker }
1773*77c1e3ccSAndroid Build Coastguard Worker 
1774*77c1e3ccSAndroid Build Coastguard Worker // Decide whether to evaluate the AB partition specified by part_type based on
1775*77c1e3ccSAndroid Build Coastguard Worker // split and HORZ/VERT info
evaluate_ab_partition_based_on_split(const PC_TREE * pc_tree,PARTITION_TYPE rect_part,const RD_RECT_PART_WIN_INFO * rect_part_win_info,int qindex,int split_idx1,int split_idx2)1776*77c1e3ccSAndroid Build Coastguard Worker static int evaluate_ab_partition_based_on_split(
1777*77c1e3ccSAndroid Build Coastguard Worker     const PC_TREE *pc_tree, PARTITION_TYPE rect_part,
1778*77c1e3ccSAndroid Build Coastguard Worker     const RD_RECT_PART_WIN_INFO *rect_part_win_info, int qindex, int split_idx1,
1779*77c1e3ccSAndroid Build Coastguard Worker     int split_idx2) {
1780*77c1e3ccSAndroid Build Coastguard Worker   int num_win = 0;
1781*77c1e3ccSAndroid Build Coastguard Worker   // Threshold for number of winners
1782*77c1e3ccSAndroid Build Coastguard Worker   // Conservative pruning for high quantizers
1783*77c1e3ccSAndroid Build Coastguard Worker   const int num_win_thresh = AOMMIN(3 * (2 * (MAXQ - qindex) / MAXQ), 3);
1784*77c1e3ccSAndroid Build Coastguard Worker   int sub_part_win =
1785*77c1e3ccSAndroid Build Coastguard Worker       (rect_part_win_info == NULL)    ? (pc_tree->partitioning == rect_part)
1786*77c1e3ccSAndroid Build Coastguard Worker       : (rect_part == PARTITION_HORZ) ? rect_part_win_info->rect_part_win[HORZ]
1787*77c1e3ccSAndroid Build Coastguard Worker                                       : rect_part_win_info->rect_part_win[VERT];
1788*77c1e3ccSAndroid Build Coastguard Worker   num_win += (sub_part_win) ? 1 : 0;
1789*77c1e3ccSAndroid Build Coastguard Worker   if (pc_tree->split[split_idx1]) {
1790*77c1e3ccSAndroid Build Coastguard Worker     num_win +=
1791*77c1e3ccSAndroid Build Coastguard Worker         (pc_tree->split[split_idx1]->partitioning == PARTITION_NONE) ? 1 : 0;
1792*77c1e3ccSAndroid Build Coastguard Worker   } else {
1793*77c1e3ccSAndroid Build Coastguard Worker     num_win += 1;
1794*77c1e3ccSAndroid Build Coastguard Worker   }
1795*77c1e3ccSAndroid Build Coastguard Worker   if (pc_tree->split[split_idx2]) {
1796*77c1e3ccSAndroid Build Coastguard Worker     num_win +=
1797*77c1e3ccSAndroid Build Coastguard Worker         (pc_tree->split[split_idx2]->partitioning == PARTITION_NONE) ? 1 : 0;
1798*77c1e3ccSAndroid Build Coastguard Worker   } else {
1799*77c1e3ccSAndroid Build Coastguard Worker     num_win += 1;
1800*77c1e3ccSAndroid Build Coastguard Worker   }
1801*77c1e3ccSAndroid Build Coastguard Worker   if (num_win < num_win_thresh) {
1802*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1803*77c1e3ccSAndroid Build Coastguard Worker   }
1804*77c1e3ccSAndroid Build Coastguard Worker   return 1;
1805*77c1e3ccSAndroid Build Coastguard Worker }
1806*77c1e3ccSAndroid Build Coastguard Worker 
av1_prune_ab_partitions(AV1_COMP * cpi,const MACROBLOCK * x,const PC_TREE * pc_tree,int pb_source_variance,int64_t best_rdcost,const RD_RECT_PART_WIN_INFO * rect_part_win_info,bool ext_partition_allowed,PartitionSearchState * part_state,int * ab_partitions_allowed)1807*77c1e3ccSAndroid Build Coastguard Worker void av1_prune_ab_partitions(AV1_COMP *cpi, const MACROBLOCK *x,
1808*77c1e3ccSAndroid Build Coastguard Worker                              const PC_TREE *pc_tree, int pb_source_variance,
1809*77c1e3ccSAndroid Build Coastguard Worker                              int64_t best_rdcost,
1810*77c1e3ccSAndroid Build Coastguard Worker                              const RD_RECT_PART_WIN_INFO *rect_part_win_info,
1811*77c1e3ccSAndroid Build Coastguard Worker                              bool ext_partition_allowed,
1812*77c1e3ccSAndroid Build Coastguard Worker                              PartitionSearchState *part_state,
1813*77c1e3ccSAndroid Build Coastguard Worker                              int *ab_partitions_allowed) {
1814*77c1e3ccSAndroid Build Coastguard Worker   int64_t *horz_rd = part_state->rect_part_rd[HORZ];
1815*77c1e3ccSAndroid Build Coastguard Worker   int64_t *vert_rd = part_state->rect_part_rd[VERT];
1816*77c1e3ccSAndroid Build Coastguard Worker   int64_t *split_rd = part_state->split_rd;
1817*77c1e3ccSAndroid Build Coastguard Worker   const PartitionCfg *const part_cfg = &cpi->oxcf.part_cfg;
1818*77c1e3ccSAndroid Build Coastguard Worker   // The standard AB partitions are allowed initially if ext-partition-types are
1819*77c1e3ccSAndroid Build Coastguard Worker   // allowed.
1820*77c1e3ccSAndroid Build Coastguard Worker   int horzab_partition_allowed = ext_partition_allowed &&
1821*77c1e3ccSAndroid Build Coastguard Worker                                  part_cfg->enable_ab_partitions &&
1822*77c1e3ccSAndroid Build Coastguard Worker                                  part_state->partition_rect_allowed[HORZ];
1823*77c1e3ccSAndroid Build Coastguard Worker   int vertab_partition_allowed = ext_partition_allowed &&
1824*77c1e3ccSAndroid Build Coastguard Worker                                  part_cfg->enable_ab_partitions &&
1825*77c1e3ccSAndroid Build Coastguard Worker                                  part_state->partition_rect_allowed[VERT];
1826*77c1e3ccSAndroid Build Coastguard Worker 
1827*77c1e3ccSAndroid Build Coastguard Worker   // Pruning: pruning out AB partitions on one main direction based on the
1828*77c1e3ccSAndroid Build Coastguard Worker   // current best partition and source variance.
1829*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.prune_ext_partition_types_search_level) {
1830*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->sf.part_sf.prune_ext_partition_types_search_level == 1) {
1831*77c1e3ccSAndroid Build Coastguard Worker       // TODO(debargha,[email protected]): may need to tune the threshold for
1832*77c1e3ccSAndroid Build Coastguard Worker       // pb_source_variance.
1833*77c1e3ccSAndroid Build Coastguard Worker       horzab_partition_allowed &= (pc_tree->partitioning == PARTITION_HORZ ||
1834*77c1e3ccSAndroid Build Coastguard Worker                                    (pc_tree->partitioning == PARTITION_NONE &&
1835*77c1e3ccSAndroid Build Coastguard Worker                                     pb_source_variance < 32) ||
1836*77c1e3ccSAndroid Build Coastguard Worker                                    pc_tree->partitioning == PARTITION_SPLIT);
1837*77c1e3ccSAndroid Build Coastguard Worker       vertab_partition_allowed &= (pc_tree->partitioning == PARTITION_VERT ||
1838*77c1e3ccSAndroid Build Coastguard Worker                                    (pc_tree->partitioning == PARTITION_NONE &&
1839*77c1e3ccSAndroid Build Coastguard Worker                                     pb_source_variance < 32) ||
1840*77c1e3ccSAndroid Build Coastguard Worker                                    pc_tree->partitioning == PARTITION_SPLIT);
1841*77c1e3ccSAndroid Build Coastguard Worker     } else {
1842*77c1e3ccSAndroid Build Coastguard Worker       horzab_partition_allowed &= (pc_tree->partitioning == PARTITION_HORZ ||
1843*77c1e3ccSAndroid Build Coastguard Worker                                    pc_tree->partitioning == PARTITION_SPLIT);
1844*77c1e3ccSAndroid Build Coastguard Worker       vertab_partition_allowed &= (pc_tree->partitioning == PARTITION_VERT ||
1845*77c1e3ccSAndroid Build Coastguard Worker                                    pc_tree->partitioning == PARTITION_SPLIT);
1846*77c1e3ccSAndroid Build Coastguard Worker     }
1847*77c1e3ccSAndroid Build Coastguard Worker     horz_rd[0] = (horz_rd[0] < INT64_MAX ? horz_rd[0] : 0);
1848*77c1e3ccSAndroid Build Coastguard Worker     horz_rd[1] = (horz_rd[1] < INT64_MAX ? horz_rd[1] : 0);
1849*77c1e3ccSAndroid Build Coastguard Worker     vert_rd[0] = (vert_rd[0] < INT64_MAX ? vert_rd[0] : 0);
1850*77c1e3ccSAndroid Build Coastguard Worker     vert_rd[1] = (vert_rd[1] < INT64_MAX ? vert_rd[1] : 0);
1851*77c1e3ccSAndroid Build Coastguard Worker     split_rd[0] = (split_rd[0] < INT64_MAX ? split_rd[0] : 0);
1852*77c1e3ccSAndroid Build Coastguard Worker     split_rd[1] = (split_rd[1] < INT64_MAX ? split_rd[1] : 0);
1853*77c1e3ccSAndroid Build Coastguard Worker     split_rd[2] = (split_rd[2] < INT64_MAX ? split_rd[2] : 0);
1854*77c1e3ccSAndroid Build Coastguard Worker     split_rd[3] = (split_rd[3] < INT64_MAX ? split_rd[3] : 0);
1855*77c1e3ccSAndroid Build Coastguard Worker   }
1856*77c1e3ccSAndroid Build Coastguard Worker 
1857*77c1e3ccSAndroid Build Coastguard Worker   // Pruning: pruning out horz_a or horz_b if the combined rdcost of its
1858*77c1e3ccSAndroid Build Coastguard Worker   // subblocks estimated from previous partitions is much higher than the best
1859*77c1e3ccSAndroid Build Coastguard Worker   // rd so far.
1860*77c1e3ccSAndroid Build Coastguard Worker   ab_partitions_allowed[HORZ_A] = horzab_partition_allowed;
1861*77c1e3ccSAndroid Build Coastguard Worker   ab_partitions_allowed[HORZ_B] = horzab_partition_allowed;
1862*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.prune_ext_partition_types_search_level) {
1863*77c1e3ccSAndroid Build Coastguard Worker     const int64_t horz_a_rd = horz_rd[1] + split_rd[0] + split_rd[1];
1864*77c1e3ccSAndroid Build Coastguard Worker     const int64_t horz_b_rd = horz_rd[0] + split_rd[2] + split_rd[3];
1865*77c1e3ccSAndroid Build Coastguard Worker     switch (cpi->sf.part_sf.prune_ext_partition_types_search_level) {
1866*77c1e3ccSAndroid Build Coastguard Worker       case 1:
1867*77c1e3ccSAndroid Build Coastguard Worker         ab_partitions_allowed[HORZ_A] &= (horz_a_rd / 16 * 14 < best_rdcost);
1868*77c1e3ccSAndroid Build Coastguard Worker         ab_partitions_allowed[HORZ_B] &= (horz_b_rd / 16 * 14 < best_rdcost);
1869*77c1e3ccSAndroid Build Coastguard Worker         break;
1870*77c1e3ccSAndroid Build Coastguard Worker       case 2:
1871*77c1e3ccSAndroid Build Coastguard Worker       default:
1872*77c1e3ccSAndroid Build Coastguard Worker         ab_partitions_allowed[HORZ_A] &= (horz_a_rd / 16 * 15 < best_rdcost);
1873*77c1e3ccSAndroid Build Coastguard Worker         ab_partitions_allowed[HORZ_B] &= (horz_b_rd / 16 * 15 < best_rdcost);
1874*77c1e3ccSAndroid Build Coastguard Worker         break;
1875*77c1e3ccSAndroid Build Coastguard Worker     }
1876*77c1e3ccSAndroid Build Coastguard Worker   }
1877*77c1e3ccSAndroid Build Coastguard Worker 
1878*77c1e3ccSAndroid Build Coastguard Worker   // Pruning: pruning out vert_a or vert_b if the combined rdcost of its
1879*77c1e3ccSAndroid Build Coastguard Worker   // subblocks estimated from previous partitions is much higher than the best
1880*77c1e3ccSAndroid Build Coastguard Worker   // rd so far.
1881*77c1e3ccSAndroid Build Coastguard Worker   ab_partitions_allowed[VERT_A] = vertab_partition_allowed;
1882*77c1e3ccSAndroid Build Coastguard Worker   ab_partitions_allowed[VERT_B] = vertab_partition_allowed;
1883*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.prune_ext_partition_types_search_level) {
1884*77c1e3ccSAndroid Build Coastguard Worker     const int64_t vert_a_rd = vert_rd[1] + split_rd[0] + split_rd[2];
1885*77c1e3ccSAndroid Build Coastguard Worker     const int64_t vert_b_rd = vert_rd[0] + split_rd[1] + split_rd[3];
1886*77c1e3ccSAndroid Build Coastguard Worker     switch (cpi->sf.part_sf.prune_ext_partition_types_search_level) {
1887*77c1e3ccSAndroid Build Coastguard Worker       case 1:
1888*77c1e3ccSAndroid Build Coastguard Worker         ab_partitions_allowed[VERT_A] &= (vert_a_rd / 16 * 14 < best_rdcost);
1889*77c1e3ccSAndroid Build Coastguard Worker         ab_partitions_allowed[VERT_B] &= (vert_b_rd / 16 * 14 < best_rdcost);
1890*77c1e3ccSAndroid Build Coastguard Worker         break;
1891*77c1e3ccSAndroid Build Coastguard Worker       case 2:
1892*77c1e3ccSAndroid Build Coastguard Worker       default:
1893*77c1e3ccSAndroid Build Coastguard Worker         ab_partitions_allowed[VERT_A] &= (vert_a_rd / 16 * 15 < best_rdcost);
1894*77c1e3ccSAndroid Build Coastguard Worker         ab_partitions_allowed[VERT_B] &= (vert_b_rd / 16 * 15 < best_rdcost);
1895*77c1e3ccSAndroid Build Coastguard Worker         break;
1896*77c1e3ccSAndroid Build Coastguard Worker     }
1897*77c1e3ccSAndroid Build Coastguard Worker   }
1898*77c1e3ccSAndroid Build Coastguard Worker 
1899*77c1e3ccSAndroid Build Coastguard Worker   // Pruning: pruning out some ab partitions using a DNN taking rd costs of
1900*77c1e3ccSAndroid Build Coastguard Worker   // sub-blocks from previous basic partition types.
1901*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.ml_prune_partition && ext_partition_allowed &&
1902*77c1e3ccSAndroid Build Coastguard Worker       part_state->partition_rect_allowed[HORZ] &&
1903*77c1e3ccSAndroid Build Coastguard Worker       part_state->partition_rect_allowed[VERT]) {
1904*77c1e3ccSAndroid Build Coastguard Worker     // TODO([email protected]): x->source_variance may not be the current
1905*77c1e3ccSAndroid Build Coastguard Worker     // block's variance. The correct one to use is pb_source_variance. Need to
1906*77c1e3ccSAndroid Build Coastguard Worker     // re-train the model to fix it.
1907*77c1e3ccSAndroid Build Coastguard Worker     ml_prune_ab_partition(cpi, pc_tree->partitioning,
1908*77c1e3ccSAndroid Build Coastguard Worker                           get_unsigned_bits(x->source_variance), best_rdcost,
1909*77c1e3ccSAndroid Build Coastguard Worker                           part_state, ab_partitions_allowed);
1910*77c1e3ccSAndroid Build Coastguard Worker   }
1911*77c1e3ccSAndroid Build Coastguard Worker 
1912*77c1e3ccSAndroid Build Coastguard Worker   // Pruning: pruning AB partitions based on the number of horz/vert wins
1913*77c1e3ccSAndroid Build Coastguard Worker   // in the current block and sub-blocks in PARTITION_SPLIT.
1914*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.prune_ext_part_using_split_info >= 2 &&
1915*77c1e3ccSAndroid Build Coastguard Worker       ab_partitions_allowed[HORZ_A]) {
1916*77c1e3ccSAndroid Build Coastguard Worker     ab_partitions_allowed[HORZ_A] &= evaluate_ab_partition_based_on_split(
1917*77c1e3ccSAndroid Build Coastguard Worker         pc_tree, PARTITION_HORZ, rect_part_win_info, x->qindex, 0, 1);
1918*77c1e3ccSAndroid Build Coastguard Worker   }
1919*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.prune_ext_part_using_split_info >= 2 &&
1920*77c1e3ccSAndroid Build Coastguard Worker       ab_partitions_allowed[HORZ_B]) {
1921*77c1e3ccSAndroid Build Coastguard Worker     ab_partitions_allowed[HORZ_B] &= evaluate_ab_partition_based_on_split(
1922*77c1e3ccSAndroid Build Coastguard Worker         pc_tree, PARTITION_HORZ, rect_part_win_info, x->qindex, 2, 3);
1923*77c1e3ccSAndroid Build Coastguard Worker   }
1924*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.prune_ext_part_using_split_info >= 2 &&
1925*77c1e3ccSAndroid Build Coastguard Worker       ab_partitions_allowed[VERT_A]) {
1926*77c1e3ccSAndroid Build Coastguard Worker     ab_partitions_allowed[VERT_A] &= evaluate_ab_partition_based_on_split(
1927*77c1e3ccSAndroid Build Coastguard Worker         pc_tree, PARTITION_VERT, rect_part_win_info, x->qindex, 0, 2);
1928*77c1e3ccSAndroid Build Coastguard Worker   }
1929*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.prune_ext_part_using_split_info >= 2 &&
1930*77c1e3ccSAndroid Build Coastguard Worker       ab_partitions_allowed[VERT_B]) {
1931*77c1e3ccSAndroid Build Coastguard Worker     ab_partitions_allowed[VERT_B] &= evaluate_ab_partition_based_on_split(
1932*77c1e3ccSAndroid Build Coastguard Worker         pc_tree, PARTITION_VERT, rect_part_win_info, x->qindex, 1, 3);
1933*77c1e3ccSAndroid Build Coastguard Worker   }
1934*77c1e3ccSAndroid Build Coastguard Worker }
1935*77c1e3ccSAndroid Build Coastguard Worker 
1936*77c1e3ccSAndroid Build Coastguard Worker // Prepare features for the external model. Specifically, features after
1937*77c1e3ccSAndroid Build Coastguard Worker // ab partition is searched.
prepare_features_after_part_ab(const AV1_COMP * const cpi,MACROBLOCK * const x,BLOCK_SIZE bsize,int part_ctx,int64_t best_rd,int64_t rect_part_rd[NUM_RECT_PARTS][SUB_PARTITIONS_RECT],int64_t split_rd[SUB_PARTITIONS_SPLIT],unsigned int pb_source_variance,int mi_row,int mi_col,aom_partition_features_t * const features)1938*77c1e3ccSAndroid Build Coastguard Worker static void prepare_features_after_part_ab(
1939*77c1e3ccSAndroid Build Coastguard Worker     const AV1_COMP *const cpi, MACROBLOCK *const x, BLOCK_SIZE bsize,
1940*77c1e3ccSAndroid Build Coastguard Worker     int part_ctx, int64_t best_rd,
1941*77c1e3ccSAndroid Build Coastguard Worker     int64_t rect_part_rd[NUM_RECT_PARTS][SUB_PARTITIONS_RECT],
1942*77c1e3ccSAndroid Build Coastguard Worker     int64_t split_rd[SUB_PARTITIONS_SPLIT], unsigned int pb_source_variance,
1943*77c1e3ccSAndroid Build Coastguard Worker     int mi_row, int mi_col, aom_partition_features_t *const features) {
1944*77c1e3ccSAndroid Build Coastguard Worker   int64_t *horz_rd = rect_part_rd[HORZ];
1945*77c1e3ccSAndroid Build Coastguard Worker   int64_t *vert_rd = rect_part_rd[VERT];
1946*77c1e3ccSAndroid Build Coastguard Worker 
1947*77c1e3ccSAndroid Build Coastguard Worker   // Generate features.
1948*77c1e3ccSAndroid Build Coastguard Worker   int feature_index = 0;
1949*77c1e3ccSAndroid Build Coastguard Worker   features->after_part_ab.f[feature_index++] = (float)part_ctx;
1950*77c1e3ccSAndroid Build Coastguard Worker   features->after_part_ab.f[feature_index++] =
1951*77c1e3ccSAndroid Build Coastguard Worker       (float)get_unsigned_bits(pb_source_variance);
1952*77c1e3ccSAndroid Build Coastguard Worker 
1953*77c1e3ccSAndroid Build Coastguard Worker   const int rdcost = (int)AOMMIN(INT_MAX, best_rd);
1954*77c1e3ccSAndroid Build Coastguard Worker   int sub_block_rdcost[8] = { 0 };
1955*77c1e3ccSAndroid Build Coastguard Worker   int rd_index = 0;
1956*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
1957*77c1e3ccSAndroid Build Coastguard Worker     if (horz_rd[i] > 0 && horz_rd[i] < 1000000000)
1958*77c1e3ccSAndroid Build Coastguard Worker       sub_block_rdcost[rd_index] = (int)horz_rd[i];
1959*77c1e3ccSAndroid Build Coastguard Worker     ++rd_index;
1960*77c1e3ccSAndroid Build Coastguard Worker   }
1961*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
1962*77c1e3ccSAndroid Build Coastguard Worker     if (vert_rd[i] > 0 && vert_rd[i] < 1000000000)
1963*77c1e3ccSAndroid Build Coastguard Worker       sub_block_rdcost[rd_index] = (int)vert_rd[i];
1964*77c1e3ccSAndroid Build Coastguard Worker     ++rd_index;
1965*77c1e3ccSAndroid Build Coastguard Worker   }
1966*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
1967*77c1e3ccSAndroid Build Coastguard Worker     if (split_rd[i] > 0 && split_rd[i] < 1000000000)
1968*77c1e3ccSAndroid Build Coastguard Worker       sub_block_rdcost[rd_index] = (int)split_rd[i];
1969*77c1e3ccSAndroid Build Coastguard Worker     ++rd_index;
1970*77c1e3ccSAndroid Build Coastguard Worker   }
1971*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 8; ++i) {
1972*77c1e3ccSAndroid Build Coastguard Worker     // Ratio between the sub-block RD and the whole-block RD.
1973*77c1e3ccSAndroid Build Coastguard Worker     float rd_ratio = 1.0f;
1974*77c1e3ccSAndroid Build Coastguard Worker     if (sub_block_rdcost[i] > 0 && sub_block_rdcost[i] < rdcost)
1975*77c1e3ccSAndroid Build Coastguard Worker       rd_ratio = (float)sub_block_rdcost[i] / (float)rdcost;
1976*77c1e3ccSAndroid Build Coastguard Worker     features->after_part_ab.f[feature_index++] = rd_ratio;
1977*77c1e3ccSAndroid Build Coastguard Worker   }
1978*77c1e3ccSAndroid Build Coastguard Worker 
1979*77c1e3ccSAndroid Build Coastguard Worker   // 4-way partitions are only allowed for these three square block sizes.
1980*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize == BLOCK_16X16 || bsize == BLOCK_32X32 || bsize == BLOCK_64X64);
1981*77c1e3ccSAndroid Build Coastguard Worker 
1982*77c1e3ccSAndroid Build Coastguard Worker   // Get variance of the 1:4 and 4:1 sub-blocks.
1983*77c1e3ccSAndroid Build Coastguard Worker   unsigned int horz_4_source_var[SUB_PARTITIONS_PART4] = { 0 };
1984*77c1e3ccSAndroid Build Coastguard Worker   unsigned int vert_4_source_var[SUB_PARTITIONS_PART4] = { 0 };
1985*77c1e3ccSAndroid Build Coastguard Worker   {
1986*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE horz_4_bs = get_partition_subsize(bsize, PARTITION_HORZ_4);
1987*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE vert_4_bs = get_partition_subsize(bsize, PARTITION_VERT_4);
1988*77c1e3ccSAndroid Build Coastguard Worker 
1989*77c1e3ccSAndroid Build Coastguard Worker     assert(horz_4_bs != BLOCK_INVALID);
1990*77c1e3ccSAndroid Build Coastguard Worker     assert(vert_4_bs != BLOCK_INVALID);
1991*77c1e3ccSAndroid Build Coastguard Worker 
1992*77c1e3ccSAndroid Build Coastguard Worker     av1_setup_src_planes(x, cpi->source, mi_row, mi_col,
1993*77c1e3ccSAndroid Build Coastguard Worker                          av1_num_planes(&cpi->common), bsize);
1994*77c1e3ccSAndroid Build Coastguard Worker     const int src_stride = x->plane[0].src.stride;
1995*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *src = x->plane[0].src.buf;
1996*77c1e3ccSAndroid Build Coastguard Worker     const MACROBLOCKD *const xd = &x->e_mbd;
1997*77c1e3ccSAndroid Build Coastguard Worker 
1998*77c1e3ccSAndroid Build Coastguard Worker     struct buf_2d horz_4_src, vert_4_src;
1999*77c1e3ccSAndroid Build Coastguard Worker     horz_4_src.stride = src_stride;
2000*77c1e3ccSAndroid Build Coastguard Worker     vert_4_src.stride = src_stride;
2001*77c1e3ccSAndroid Build Coastguard Worker 
2002*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < SUB_PARTITIONS_PART4; ++i) {
2003*77c1e3ccSAndroid Build Coastguard Worker       horz_4_src.buf = src + i * block_size_high[horz_4_bs] * src_stride;
2004*77c1e3ccSAndroid Build Coastguard Worker       vert_4_src.buf = src + i * block_size_wide[vert_4_bs];
2005*77c1e3ccSAndroid Build Coastguard Worker 
2006*77c1e3ccSAndroid Build Coastguard Worker       horz_4_source_var[i] = av1_get_perpixel_variance_facade(
2007*77c1e3ccSAndroid Build Coastguard Worker           cpi, xd, &horz_4_src, horz_4_bs, AOM_PLANE_Y);
2008*77c1e3ccSAndroid Build Coastguard Worker       vert_4_source_var[i] = av1_get_perpixel_variance_facade(
2009*77c1e3ccSAndroid Build Coastguard Worker           cpi, xd, &vert_4_src, vert_4_bs, AOM_PLANE_Y);
2010*77c1e3ccSAndroid Build Coastguard Worker     }
2011*77c1e3ccSAndroid Build Coastguard Worker   }
2012*77c1e3ccSAndroid Build Coastguard Worker 
2013*77c1e3ccSAndroid Build Coastguard Worker   const float denom = (float)(pb_source_variance + 1);
2014*77c1e3ccSAndroid Build Coastguard Worker   const float low_b = 0.1f;
2015*77c1e3ccSAndroid Build Coastguard Worker   const float high_b = 10.0f;
2016*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_PART4; ++i) {
2017*77c1e3ccSAndroid Build Coastguard Worker     // Ratio between the 4:1 sub-block variance and the whole-block variance.
2018*77c1e3ccSAndroid Build Coastguard Worker     float var_ratio = (float)(horz_4_source_var[i] + 1) / denom;
2019*77c1e3ccSAndroid Build Coastguard Worker     if (var_ratio < low_b) var_ratio = low_b;
2020*77c1e3ccSAndroid Build Coastguard Worker     if (var_ratio > high_b) var_ratio = high_b;
2021*77c1e3ccSAndroid Build Coastguard Worker     features->after_part_ab.f[feature_index++] = var_ratio;
2022*77c1e3ccSAndroid Build Coastguard Worker   }
2023*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_PART4; ++i) {
2024*77c1e3ccSAndroid Build Coastguard Worker     // Ratio between the 1:4 sub-block RD and the whole-block RD.
2025*77c1e3ccSAndroid Build Coastguard Worker     float var_ratio = (float)(vert_4_source_var[i] + 1) / denom;
2026*77c1e3ccSAndroid Build Coastguard Worker     if (var_ratio < low_b) var_ratio = low_b;
2027*77c1e3ccSAndroid Build Coastguard Worker     if (var_ratio > high_b) var_ratio = high_b;
2028*77c1e3ccSAndroid Build Coastguard Worker     features->after_part_ab.f[feature_index++] = var_ratio;
2029*77c1e3ccSAndroid Build Coastguard Worker   }
2030*77c1e3ccSAndroid Build Coastguard Worker   assert(feature_index == 18);
2031*77c1e3ccSAndroid Build Coastguard Worker }
2032*77c1e3ccSAndroid Build Coastguard Worker 
2033*77c1e3ccSAndroid Build Coastguard Worker // If the external partition model is used, we let it determine partition
2034*77c1e3ccSAndroid Build Coastguard Worker // decisions before partition none. Specifically, these parameters:
2035*77c1e3ccSAndroid Build Coastguard Worker // partition_none_allowed
2036*77c1e3ccSAndroid Build Coastguard Worker // partition_horz_allowed
2037*77c1e3ccSAndroid Build Coastguard Worker // partition_vert_allowed
2038*77c1e3ccSAndroid Build Coastguard Worker // do_rectangular_split
2039*77c1e3ccSAndroid Build Coastguard Worker // do_square_split
ext_ml_model_decision_before_none(AV1_COMP * cpi,const float features_from_motion[FEATURE_SIZE_SMS_SPLIT],int * partition_none_allowed,int * partition_horz_allowed,int * partition_vert_allowed,int * do_rectangular_split,int * do_square_split)2040*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_before_none(
2041*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *cpi, const float features_from_motion[FEATURE_SIZE_SMS_SPLIT],
2042*77c1e3ccSAndroid Build Coastguard Worker     int *partition_none_allowed, int *partition_horz_allowed,
2043*77c1e3ccSAndroid Build Coastguard Worker     int *partition_vert_allowed, int *do_rectangular_split,
2044*77c1e3ccSAndroid Build Coastguard Worker     int *do_square_split) {
2045*77c1e3ccSAndroid Build Coastguard Worker   ExtPartController *const ext_part_controller = &cpi->ext_part_controller;
2046*77c1e3ccSAndroid Build Coastguard Worker   if (!ext_part_controller->ready) return false;
2047*77c1e3ccSAndroid Build Coastguard Worker 
2048*77c1e3ccSAndroid Build Coastguard Worker   // Setup features.
2049*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_features_t features;
2050*77c1e3ccSAndroid Build Coastguard Worker   features.id = AOM_EXT_PART_FEATURE_BEFORE_NONE;
2051*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < FEATURE_SIZE_SMS_SPLIT; ++i) {
2052*77c1e3ccSAndroid Build Coastguard Worker     features.before_part_none.f[i] = features_from_motion[i];
2053*77c1e3ccSAndroid Build Coastguard Worker   }
2054*77c1e3ccSAndroid Build Coastguard Worker 
2055*77c1e3ccSAndroid Build Coastguard Worker   // Send necessary features to the external model.
2056*77c1e3ccSAndroid Build Coastguard Worker   av1_ext_part_send_features(ext_part_controller, &features);
2057*77c1e3ccSAndroid Build Coastguard Worker 
2058*77c1e3ccSAndroid Build Coastguard Worker   // Get partition decisions from the external model.
2059*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_decision_t decision;
2060*77c1e3ccSAndroid Build Coastguard Worker   const bool valid_decision =
2061*77c1e3ccSAndroid Build Coastguard Worker       av1_ext_part_get_partition_decision(ext_part_controller, &decision);
2062*77c1e3ccSAndroid Build Coastguard Worker   if (!valid_decision) return false;
2063*77c1e3ccSAndroid Build Coastguard Worker 
2064*77c1e3ccSAndroid Build Coastguard Worker   // Populate decisions
2065*77c1e3ccSAndroid Build Coastguard Worker   *partition_none_allowed = decision.partition_none_allowed;
2066*77c1e3ccSAndroid Build Coastguard Worker   *partition_horz_allowed = decision.partition_rect_allowed[HORZ];
2067*77c1e3ccSAndroid Build Coastguard Worker   *partition_vert_allowed = decision.partition_rect_allowed[VERT];
2068*77c1e3ccSAndroid Build Coastguard Worker   *do_rectangular_split = decision.do_rectangular_split;
2069*77c1e3ccSAndroid Build Coastguard Worker   *do_square_split = decision.do_square_split;
2070*77c1e3ccSAndroid Build Coastguard Worker 
2071*77c1e3ccSAndroid Build Coastguard Worker   return true;
2072*77c1e3ccSAndroid Build Coastguard Worker }
2073*77c1e3ccSAndroid Build Coastguard Worker 
2074*77c1e3ccSAndroid Build Coastguard Worker // If the external partition model is used, we let it determine partition
2075*77c1e3ccSAndroid Build Coastguard Worker // decisions before partition none. Specifically, these parameters:
2076*77c1e3ccSAndroid Build Coastguard Worker // prune_horz
2077*77c1e3ccSAndroid Build Coastguard Worker // prune_vert
ext_ml_model_decision_before_none_part2(AV1_COMP * cpi,const float features_from_motion[FEATURE_SIZE_SMS_PRUNE_PART],int * prune_horz,int * prune_vert)2078*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_before_none_part2(
2079*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *cpi,
2080*77c1e3ccSAndroid Build Coastguard Worker     const float features_from_motion[FEATURE_SIZE_SMS_PRUNE_PART],
2081*77c1e3ccSAndroid Build Coastguard Worker     int *prune_horz, int *prune_vert) {
2082*77c1e3ccSAndroid Build Coastguard Worker   ExtPartController *const ext_part_controller = &cpi->ext_part_controller;
2083*77c1e3ccSAndroid Build Coastguard Worker   if (!ext_part_controller->ready) return false;
2084*77c1e3ccSAndroid Build Coastguard Worker 
2085*77c1e3ccSAndroid Build Coastguard Worker   // Setup features.
2086*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_features_t features;
2087*77c1e3ccSAndroid Build Coastguard Worker   features.id = AOM_EXT_PART_FEATURE_BEFORE_NONE_PART2;
2088*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < FEATURE_SIZE_SMS_PRUNE_PART; ++i) {
2089*77c1e3ccSAndroid Build Coastguard Worker     features.before_part_none.f_part2[i] = features_from_motion[i];
2090*77c1e3ccSAndroid Build Coastguard Worker   }
2091*77c1e3ccSAndroid Build Coastguard Worker 
2092*77c1e3ccSAndroid Build Coastguard Worker   // Send necessary features to the external model.
2093*77c1e3ccSAndroid Build Coastguard Worker   av1_ext_part_send_features(ext_part_controller, &features);
2094*77c1e3ccSAndroid Build Coastguard Worker 
2095*77c1e3ccSAndroid Build Coastguard Worker   // Get partition decisions from the external model.
2096*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_decision_t decision;
2097*77c1e3ccSAndroid Build Coastguard Worker   const bool valid_decision =
2098*77c1e3ccSAndroid Build Coastguard Worker       av1_ext_part_get_partition_decision(ext_part_controller, &decision);
2099*77c1e3ccSAndroid Build Coastguard Worker   if (!valid_decision) return false;
2100*77c1e3ccSAndroid Build Coastguard Worker 
2101*77c1e3ccSAndroid Build Coastguard Worker   // Populate decisions
2102*77c1e3ccSAndroid Build Coastguard Worker   *prune_horz = decision.prune_rect_part[HORZ];
2103*77c1e3ccSAndroid Build Coastguard Worker   *prune_vert = decision.prune_rect_part[VERT];
2104*77c1e3ccSAndroid Build Coastguard Worker 
2105*77c1e3ccSAndroid Build Coastguard Worker   return true;
2106*77c1e3ccSAndroid Build Coastguard Worker }
2107*77c1e3ccSAndroid Build Coastguard Worker 
2108*77c1e3ccSAndroid Build Coastguard Worker // If the external partition model is used, we let it determine partition
2109*77c1e3ccSAndroid Build Coastguard Worker // decisions after none partition. Specifically, these parameters:
2110*77c1e3ccSAndroid Build Coastguard Worker // do_square_split
2111*77c1e3ccSAndroid Build Coastguard Worker // do_rectangular_split
ext_ml_model_decision_after_none(ExtPartController * const ext_part_controller,const int is_intra_frame,const float * const features_after_none,int * do_square_split,int * do_rectangular_split)2112*77c1e3ccSAndroid Build Coastguard Worker bool ext_ml_model_decision_after_none(
2113*77c1e3ccSAndroid Build Coastguard Worker     ExtPartController *const ext_part_controller, const int is_intra_frame,
2114*77c1e3ccSAndroid Build Coastguard Worker     const float *const features_after_none, int *do_square_split,
2115*77c1e3ccSAndroid Build Coastguard Worker     int *do_rectangular_split) {
2116*77c1e3ccSAndroid Build Coastguard Worker   if (!ext_part_controller->ready || is_intra_frame) return false;
2117*77c1e3ccSAndroid Build Coastguard Worker 
2118*77c1e3ccSAndroid Build Coastguard Worker   // Setup features.
2119*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_features_t features;
2120*77c1e3ccSAndroid Build Coastguard Worker   features.id = AOM_EXT_PART_FEATURE_AFTER_NONE;
2121*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 4; ++i) {
2122*77c1e3ccSAndroid Build Coastguard Worker     features.after_part_none.f[i] = features_after_none[i];
2123*77c1e3ccSAndroid Build Coastguard Worker   }
2124*77c1e3ccSAndroid Build Coastguard Worker 
2125*77c1e3ccSAndroid Build Coastguard Worker   // Send necessary features to the external model.
2126*77c1e3ccSAndroid Build Coastguard Worker   av1_ext_part_send_features(ext_part_controller, &features);
2127*77c1e3ccSAndroid Build Coastguard Worker 
2128*77c1e3ccSAndroid Build Coastguard Worker   // Get partition decisions from the external model.
2129*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_decision_t decision;
2130*77c1e3ccSAndroid Build Coastguard Worker   const bool valid_decision =
2131*77c1e3ccSAndroid Build Coastguard Worker       av1_ext_part_get_partition_decision(ext_part_controller, &decision);
2132*77c1e3ccSAndroid Build Coastguard Worker   if (!valid_decision) return false;
2133*77c1e3ccSAndroid Build Coastguard Worker 
2134*77c1e3ccSAndroid Build Coastguard Worker   // Populate decisions
2135*77c1e3ccSAndroid Build Coastguard Worker   *do_square_split = decision.do_square_split;
2136*77c1e3ccSAndroid Build Coastguard Worker   *do_rectangular_split = decision.do_rectangular_split;
2137*77c1e3ccSAndroid Build Coastguard Worker 
2138*77c1e3ccSAndroid Build Coastguard Worker   return true;
2139*77c1e3ccSAndroid Build Coastguard Worker }
2140*77c1e3ccSAndroid Build Coastguard Worker 
2141*77c1e3ccSAndroid Build Coastguard Worker // If the external partition model is used, we let it determine partition
2142*77c1e3ccSAndroid Build Coastguard Worker // decisions after none partition. Specifically, these parameters:
2143*77c1e3ccSAndroid Build Coastguard Worker // terminate_partition_search
ext_ml_model_decision_after_none_part2(AV1_COMP * const cpi,const float * const features_terminate,int * terminate_partition_search)2144*77c1e3ccSAndroid Build Coastguard Worker bool ext_ml_model_decision_after_none_part2(
2145*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, const float *const features_terminate,
2146*77c1e3ccSAndroid Build Coastguard Worker     int *terminate_partition_search) {
2147*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2148*77c1e3ccSAndroid Build Coastguard Worker   ExtPartController *const ext_part_controller = &cpi->ext_part_controller;
2149*77c1e3ccSAndroid Build Coastguard Worker   if (!ext_part_controller->ready || frame_is_intra_only(cm)) return false;
2150*77c1e3ccSAndroid Build Coastguard Worker 
2151*77c1e3ccSAndroid Build Coastguard Worker   // Setup features.
2152*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_features_t features;
2153*77c1e3ccSAndroid Build Coastguard Worker   features.id = AOM_EXT_PART_FEATURE_AFTER_NONE_PART2;
2154*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < FEATURE_SIZE_SMS_TERM_NONE; ++i) {
2155*77c1e3ccSAndroid Build Coastguard Worker     features.after_part_none.f_terminate[i] = features_terminate[i];
2156*77c1e3ccSAndroid Build Coastguard Worker   }
2157*77c1e3ccSAndroid Build Coastguard Worker 
2158*77c1e3ccSAndroid Build Coastguard Worker   // Send necessary features to the external model.
2159*77c1e3ccSAndroid Build Coastguard Worker   av1_ext_part_send_features(ext_part_controller, &features);
2160*77c1e3ccSAndroid Build Coastguard Worker 
2161*77c1e3ccSAndroid Build Coastguard Worker   // Get partition decisions from the external model.
2162*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_decision_t decision;
2163*77c1e3ccSAndroid Build Coastguard Worker   const bool valid_decision =
2164*77c1e3ccSAndroid Build Coastguard Worker       av1_ext_part_get_partition_decision(ext_part_controller, &decision);
2165*77c1e3ccSAndroid Build Coastguard Worker   if (!valid_decision) return false;
2166*77c1e3ccSAndroid Build Coastguard Worker 
2167*77c1e3ccSAndroid Build Coastguard Worker   // Populate decisions
2168*77c1e3ccSAndroid Build Coastguard Worker   *terminate_partition_search = decision.terminate_partition_search;
2169*77c1e3ccSAndroid Build Coastguard Worker 
2170*77c1e3ccSAndroid Build Coastguard Worker   return true;
2171*77c1e3ccSAndroid Build Coastguard Worker }
2172*77c1e3ccSAndroid Build Coastguard Worker 
2173*77c1e3ccSAndroid Build Coastguard Worker // If the external partition model is used, we let it determine partition
2174*77c1e3ccSAndroid Build Coastguard Worker // decisions after none partition. Specifically, these parameters:
2175*77c1e3ccSAndroid Build Coastguard Worker // terminate_partition_search
ext_ml_model_decision_after_split(AV1_COMP * const cpi,const float * const features_terminate,int * terminate_partition_search)2176*77c1e3ccSAndroid Build Coastguard Worker bool ext_ml_model_decision_after_split(AV1_COMP *const cpi,
2177*77c1e3ccSAndroid Build Coastguard Worker                                        const float *const features_terminate,
2178*77c1e3ccSAndroid Build Coastguard Worker                                        int *terminate_partition_search) {
2179*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
2180*77c1e3ccSAndroid Build Coastguard Worker   ExtPartController *const ext_part_controller = &cpi->ext_part_controller;
2181*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm) || !cpi->ext_part_controller.ready) {
2182*77c1e3ccSAndroid Build Coastguard Worker     return false;
2183*77c1e3ccSAndroid Build Coastguard Worker   }
2184*77c1e3ccSAndroid Build Coastguard Worker 
2185*77c1e3ccSAndroid Build Coastguard Worker   // Setup features.
2186*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_features_t features;
2187*77c1e3ccSAndroid Build Coastguard Worker   features.id = AOM_EXT_PART_FEATURE_AFTER_SPLIT;
2188*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 31; ++i) {
2189*77c1e3ccSAndroid Build Coastguard Worker     features.after_part_split.f_terminate[i] = features_terminate[i];
2190*77c1e3ccSAndroid Build Coastguard Worker   }
2191*77c1e3ccSAndroid Build Coastguard Worker 
2192*77c1e3ccSAndroid Build Coastguard Worker   // Send necessary features to the external model.
2193*77c1e3ccSAndroid Build Coastguard Worker   av1_ext_part_send_features(ext_part_controller, &features);
2194*77c1e3ccSAndroid Build Coastguard Worker 
2195*77c1e3ccSAndroid Build Coastguard Worker   // Get partition decisions from the external model.
2196*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_decision_t decision;
2197*77c1e3ccSAndroid Build Coastguard Worker   const bool valid_decision =
2198*77c1e3ccSAndroid Build Coastguard Worker       av1_ext_part_get_partition_decision(ext_part_controller, &decision);
2199*77c1e3ccSAndroid Build Coastguard Worker   if (!valid_decision) return false;
2200*77c1e3ccSAndroid Build Coastguard Worker 
2201*77c1e3ccSAndroid Build Coastguard Worker   // Populate decisions
2202*77c1e3ccSAndroid Build Coastguard Worker   *terminate_partition_search = decision.terminate_partition_search;
2203*77c1e3ccSAndroid Build Coastguard Worker 
2204*77c1e3ccSAndroid Build Coastguard Worker   return true;
2205*77c1e3ccSAndroid Build Coastguard Worker }
2206*77c1e3ccSAndroid Build Coastguard Worker 
2207*77c1e3ccSAndroid Build Coastguard Worker // If the external partition model is used, we let it determine partition
2208*77c1e3ccSAndroid Build Coastguard Worker // decisions after none partition. Specifically, these parameters:
2209*77c1e3ccSAndroid Build Coastguard Worker // prune_rect_part[HORZ]
2210*77c1e3ccSAndroid Build Coastguard Worker // prune_rect_part[VERT]
ext_ml_model_decision_after_split_part2(ExtPartController * const ext_part_controller,const int is_intra_frame,const float * const features_prune,int * prune_rect_part_horz,int * prune_rect_part_vert)2211*77c1e3ccSAndroid Build Coastguard Worker bool ext_ml_model_decision_after_split_part2(
2212*77c1e3ccSAndroid Build Coastguard Worker     ExtPartController *const ext_part_controller, const int is_intra_frame,
2213*77c1e3ccSAndroid Build Coastguard Worker     const float *const features_prune, int *prune_rect_part_horz,
2214*77c1e3ccSAndroid Build Coastguard Worker     int *prune_rect_part_vert) {
2215*77c1e3ccSAndroid Build Coastguard Worker   if (is_intra_frame || !ext_part_controller->ready) {
2216*77c1e3ccSAndroid Build Coastguard Worker     return false;
2217*77c1e3ccSAndroid Build Coastguard Worker   }
2218*77c1e3ccSAndroid Build Coastguard Worker 
2219*77c1e3ccSAndroid Build Coastguard Worker   // Setup features.
2220*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_features_t features;
2221*77c1e3ccSAndroid Build Coastguard Worker   features.id = AOM_EXT_PART_FEATURE_AFTER_SPLIT_PART2;
2222*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 9; ++i) {
2223*77c1e3ccSAndroid Build Coastguard Worker     features.after_part_split.f_prune_rect[i] = features_prune[i];
2224*77c1e3ccSAndroid Build Coastguard Worker   }
2225*77c1e3ccSAndroid Build Coastguard Worker 
2226*77c1e3ccSAndroid Build Coastguard Worker   // Send necessary features to the external model.
2227*77c1e3ccSAndroid Build Coastguard Worker   av1_ext_part_send_features(ext_part_controller, &features);
2228*77c1e3ccSAndroid Build Coastguard Worker 
2229*77c1e3ccSAndroid Build Coastguard Worker   // Get partition decisions from the external model.
2230*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_decision_t decision;
2231*77c1e3ccSAndroid Build Coastguard Worker   const bool valid_decision =
2232*77c1e3ccSAndroid Build Coastguard Worker       av1_ext_part_get_partition_decision(ext_part_controller, &decision);
2233*77c1e3ccSAndroid Build Coastguard Worker   if (!valid_decision) return false;
2234*77c1e3ccSAndroid Build Coastguard Worker 
2235*77c1e3ccSAndroid Build Coastguard Worker   // Populate decisions
2236*77c1e3ccSAndroid Build Coastguard Worker   *prune_rect_part_horz = decision.prune_rect_part[0];
2237*77c1e3ccSAndroid Build Coastguard Worker   *prune_rect_part_vert = decision.prune_rect_part[1];
2238*77c1e3ccSAndroid Build Coastguard Worker 
2239*77c1e3ccSAndroid Build Coastguard Worker   return true;
2240*77c1e3ccSAndroid Build Coastguard Worker }
2241*77c1e3ccSAndroid Build Coastguard Worker 
2242*77c1e3ccSAndroid Build Coastguard Worker // If the external partition model is used, we let it determine partition
2243*77c1e3ccSAndroid Build Coastguard Worker // decisions after rectangular partition. Specifically, these parameters:
2244*77c1e3ccSAndroid Build Coastguard Worker // horza_partition_allowed
2245*77c1e3ccSAndroid Build Coastguard Worker // horzb_partition_allowed
2246*77c1e3ccSAndroid Build Coastguard Worker // verta_partition_allowed
2247*77c1e3ccSAndroid Build Coastguard Worker // vertb_partition_allowed
ext_ml_model_decision_after_rect(ExtPartController * const ext_part_controller,const int is_intra_frame,const float * const features_after_rect,int * horza_partition_allowed,int * horzb_partition_allowed,int * verta_partition_allowed,int * vertb_partition_allowed)2248*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_after_rect(
2249*77c1e3ccSAndroid Build Coastguard Worker     ExtPartController *const ext_part_controller, const int is_intra_frame,
2250*77c1e3ccSAndroid Build Coastguard Worker     const float *const features_after_rect, int *horza_partition_allowed,
2251*77c1e3ccSAndroid Build Coastguard Worker     int *horzb_partition_allowed, int *verta_partition_allowed,
2252*77c1e3ccSAndroid Build Coastguard Worker     int *vertb_partition_allowed) {
2253*77c1e3ccSAndroid Build Coastguard Worker   if (is_intra_frame || !ext_part_controller->ready) return false;
2254*77c1e3ccSAndroid Build Coastguard Worker 
2255*77c1e3ccSAndroid Build Coastguard Worker   // Setup features.
2256*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_features_t features;
2257*77c1e3ccSAndroid Build Coastguard Worker   features.id = AOM_EXT_PART_FEATURE_AFTER_RECT;
2258*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 10; ++i) {
2259*77c1e3ccSAndroid Build Coastguard Worker     features.after_part_rect.f[i] = features_after_rect[i];
2260*77c1e3ccSAndroid Build Coastguard Worker   }
2261*77c1e3ccSAndroid Build Coastguard Worker 
2262*77c1e3ccSAndroid Build Coastguard Worker   // Send necessary features to the external model.
2263*77c1e3ccSAndroid Build Coastguard Worker   av1_ext_part_send_features(ext_part_controller, &features);
2264*77c1e3ccSAndroid Build Coastguard Worker 
2265*77c1e3ccSAndroid Build Coastguard Worker   // Get partition decisions from the external model.
2266*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_decision_t decision;
2267*77c1e3ccSAndroid Build Coastguard Worker   const bool valid_decision =
2268*77c1e3ccSAndroid Build Coastguard Worker       av1_ext_part_get_partition_decision(ext_part_controller, &decision);
2269*77c1e3ccSAndroid Build Coastguard Worker   if (!valid_decision) return false;
2270*77c1e3ccSAndroid Build Coastguard Worker 
2271*77c1e3ccSAndroid Build Coastguard Worker   // Populate decisions
2272*77c1e3ccSAndroid Build Coastguard Worker   *horza_partition_allowed = decision.horza_partition_allowed;
2273*77c1e3ccSAndroid Build Coastguard Worker   *horzb_partition_allowed = decision.horzb_partition_allowed;
2274*77c1e3ccSAndroid Build Coastguard Worker   *verta_partition_allowed = decision.verta_partition_allowed;
2275*77c1e3ccSAndroid Build Coastguard Worker   *vertb_partition_allowed = decision.vertb_partition_allowed;
2276*77c1e3ccSAndroid Build Coastguard Worker 
2277*77c1e3ccSAndroid Build Coastguard Worker   return true;
2278*77c1e3ccSAndroid Build Coastguard Worker }
2279*77c1e3ccSAndroid Build Coastguard Worker 
2280*77c1e3ccSAndroid Build Coastguard Worker // If the external partition model is used, we let it determine partition
2281*77c1e3ccSAndroid Build Coastguard Worker // decisions after AB partition. Specifically, these parameters:
2282*77c1e3ccSAndroid Build Coastguard Worker // partition_vert4_allowed
2283*77c1e3ccSAndroid Build Coastguard Worker // partition_horz4_allowed
ext_ml_model_decision_after_part_ab(AV1_COMP * const cpi,MACROBLOCK * const x,BLOCK_SIZE bsize,int part_ctx,int64_t best_rd,int64_t rect_part_rd[NUM_RECT_PARTS][SUB_PARTITIONS_RECT],int64_t split_rd[SUB_PARTITIONS_SPLIT],int * const partition_horz4_allowed,int * const partition_vert4_allowed,unsigned int pb_source_variance,int mi_row,int mi_col)2284*77c1e3ccSAndroid Build Coastguard Worker static bool ext_ml_model_decision_after_part_ab(
2285*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, MACROBLOCK *const x, BLOCK_SIZE bsize, int part_ctx,
2286*77c1e3ccSAndroid Build Coastguard Worker     int64_t best_rd, int64_t rect_part_rd[NUM_RECT_PARTS][SUB_PARTITIONS_RECT],
2287*77c1e3ccSAndroid Build Coastguard Worker     int64_t split_rd[SUB_PARTITIONS_SPLIT], int *const partition_horz4_allowed,
2288*77c1e3ccSAndroid Build Coastguard Worker     int *const partition_vert4_allowed, unsigned int pb_source_variance,
2289*77c1e3ccSAndroid Build Coastguard Worker     int mi_row, int mi_col) {
2290*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
2291*77c1e3ccSAndroid Build Coastguard Worker   ExtPartController *const ext_part_controller = &cpi->ext_part_controller;
2292*77c1e3ccSAndroid Build Coastguard Worker 
2293*77c1e3ccSAndroid Build Coastguard Worker   if (!frame_is_intra_only(cm) && ext_part_controller->ready) {
2294*77c1e3ccSAndroid Build Coastguard Worker     // Setup features.
2295*77c1e3ccSAndroid Build Coastguard Worker     aom_partition_features_t features;
2296*77c1e3ccSAndroid Build Coastguard Worker     features.id = AOM_EXT_PART_FEATURE_AFTER_AB;
2297*77c1e3ccSAndroid Build Coastguard Worker     prepare_features_after_part_ab(cpi, x, bsize, part_ctx, best_rd,
2298*77c1e3ccSAndroid Build Coastguard Worker                                    rect_part_rd, split_rd, pb_source_variance,
2299*77c1e3ccSAndroid Build Coastguard Worker                                    mi_row, mi_col, &features);
2300*77c1e3ccSAndroid Build Coastguard Worker 
2301*77c1e3ccSAndroid Build Coastguard Worker     // Send necessary features to the external model.
2302*77c1e3ccSAndroid Build Coastguard Worker     av1_ext_part_send_features(ext_part_controller, &features);
2303*77c1e3ccSAndroid Build Coastguard Worker 
2304*77c1e3ccSAndroid Build Coastguard Worker     // Get partition decisions from the external model.
2305*77c1e3ccSAndroid Build Coastguard Worker     aom_partition_decision_t decision;
2306*77c1e3ccSAndroid Build Coastguard Worker     const bool valid_decision =
2307*77c1e3ccSAndroid Build Coastguard Worker         av1_ext_part_get_partition_decision(ext_part_controller, &decision);
2308*77c1e3ccSAndroid Build Coastguard Worker     if (!valid_decision) return false;
2309*77c1e3ccSAndroid Build Coastguard Worker 
2310*77c1e3ccSAndroid Build Coastguard Worker     // Populate decisions
2311*77c1e3ccSAndroid Build Coastguard Worker     *partition_horz4_allowed = decision.partition_horz4_allowed;
2312*77c1e3ccSAndroid Build Coastguard Worker     *partition_vert4_allowed = decision.partition_vert4_allowed;
2313*77c1e3ccSAndroid Build Coastguard Worker 
2314*77c1e3ccSAndroid Build Coastguard Worker     return true;
2315*77c1e3ccSAndroid Build Coastguard Worker   }
2316*77c1e3ccSAndroid Build Coastguard Worker 
2317*77c1e3ccSAndroid Build Coastguard Worker   return false;
2318*77c1e3ccSAndroid Build Coastguard Worker }
2319*77c1e3ccSAndroid Build Coastguard Worker 
2320*77c1e3ccSAndroid Build Coastguard Worker // This function resembles "av1_setup_sms_tree()" in context_tree.c
2321*77c1e3ccSAndroid Build Coastguard Worker // with function signature change.
setup_sms_tree(AV1_COMP * const cpi,SIMPLE_MOTION_DATA_TREE * sms_tree)2322*77c1e3ccSAndroid Build Coastguard Worker static SIMPLE_MOTION_DATA_TREE *setup_sms_tree(
2323*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, SIMPLE_MOTION_DATA_TREE *sms_tree) {
2324*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2325*77c1e3ccSAndroid Build Coastguard Worker   const int stat_generation_stage = is_stat_generation_stage(cpi);
2326*77c1e3ccSAndroid Build Coastguard Worker   const int is_sb_size_128 = cm->seq_params->sb_size == BLOCK_128X128;
2327*77c1e3ccSAndroid Build Coastguard Worker   const int tree_nodes =
2328*77c1e3ccSAndroid Build Coastguard Worker       av1_get_pc_tree_nodes(is_sb_size_128, stat_generation_stage);
2329*77c1e3ccSAndroid Build Coastguard Worker   int sms_tree_index = 0;
2330*77c1e3ccSAndroid Build Coastguard Worker   SIMPLE_MOTION_DATA_TREE *this_sms;
2331*77c1e3ccSAndroid Build Coastguard Worker   int square_index = 1;
2332*77c1e3ccSAndroid Build Coastguard Worker   int nodes;
2333*77c1e3ccSAndroid Build Coastguard Worker   this_sms = &sms_tree[0];
2334*77c1e3ccSAndroid Build Coastguard Worker 
2335*77c1e3ccSAndroid Build Coastguard Worker   if (!stat_generation_stage) {
2336*77c1e3ccSAndroid Build Coastguard Worker     const int leaf_factor = is_sb_size_128 ? 4 : 1;
2337*77c1e3ccSAndroid Build Coastguard Worker     const int leaf_nodes = 256 * leaf_factor;
2338*77c1e3ccSAndroid Build Coastguard Worker 
2339*77c1e3ccSAndroid Build Coastguard Worker     // Sets up all the leaf nodes in the tree.
2340*77c1e3ccSAndroid Build Coastguard Worker     for (sms_tree_index = 0; sms_tree_index < leaf_nodes; ++sms_tree_index) {
2341*77c1e3ccSAndroid Build Coastguard Worker       SIMPLE_MOTION_DATA_TREE *const tree = &sms_tree[sms_tree_index];
2342*77c1e3ccSAndroid Build Coastguard Worker       tree->block_size = square[0];
2343*77c1e3ccSAndroid Build Coastguard Worker     }
2344*77c1e3ccSAndroid Build Coastguard Worker 
2345*77c1e3ccSAndroid Build Coastguard Worker     // Each node has 4 leaf nodes, fill each block_size level of the tree
2346*77c1e3ccSAndroid Build Coastguard Worker     // from leafs to the root.
2347*77c1e3ccSAndroid Build Coastguard Worker     for (nodes = leaf_nodes >> 2; nodes > 0; nodes >>= 2) {
2348*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < nodes; ++i) {
2349*77c1e3ccSAndroid Build Coastguard Worker         SIMPLE_MOTION_DATA_TREE *const tree = &sms_tree[sms_tree_index];
2350*77c1e3ccSAndroid Build Coastguard Worker         tree->block_size = square[square_index];
2351*77c1e3ccSAndroid Build Coastguard Worker         for (int j = 0; j < 4; j++) tree->split[j] = this_sms++;
2352*77c1e3ccSAndroid Build Coastguard Worker         ++sms_tree_index;
2353*77c1e3ccSAndroid Build Coastguard Worker       }
2354*77c1e3ccSAndroid Build Coastguard Worker       ++square_index;
2355*77c1e3ccSAndroid Build Coastguard Worker     }
2356*77c1e3ccSAndroid Build Coastguard Worker   } else {
2357*77c1e3ccSAndroid Build Coastguard Worker     // Allocation for firstpass/LAP stage
2358*77c1e3ccSAndroid Build Coastguard Worker     // TODO(Mufaddal): refactor square_index to use a common block_size macro
2359*77c1e3ccSAndroid Build Coastguard Worker     // from firstpass.c
2360*77c1e3ccSAndroid Build Coastguard Worker     SIMPLE_MOTION_DATA_TREE *const tree = &sms_tree[sms_tree_index];
2361*77c1e3ccSAndroid Build Coastguard Worker     square_index = 2;
2362*77c1e3ccSAndroid Build Coastguard Worker     tree->block_size = square[square_index];
2363*77c1e3ccSAndroid Build Coastguard Worker   }
2364*77c1e3ccSAndroid Build Coastguard Worker 
2365*77c1e3ccSAndroid Build Coastguard Worker   // Set up the root node for the largest superblock size
2366*77c1e3ccSAndroid Build Coastguard Worker   return &sms_tree[tree_nodes - 1];
2367*77c1e3ccSAndroid Build Coastguard Worker }
2368*77c1e3ccSAndroid Build Coastguard Worker 
write_motion_feature_to_file(const char * const path,const int sb_counter,const unsigned int * block_sse,const unsigned int * block_var,const int num_blocks,const BLOCK_SIZE bsize,const BLOCK_SIZE fixed_block_size,const int mi_row,const int mi_col)2369*77c1e3ccSAndroid Build Coastguard Worker static void write_motion_feature_to_file(
2370*77c1e3ccSAndroid Build Coastguard Worker     const char *const path, const int sb_counter, const unsigned int *block_sse,
2371*77c1e3ccSAndroid Build Coastguard Worker     const unsigned int *block_var, const int num_blocks, const BLOCK_SIZE bsize,
2372*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE fixed_block_size, const int mi_row, const int mi_col) {
2373*77c1e3ccSAndroid Build Coastguard Worker   char filename[256];
2374*77c1e3ccSAndroid Build Coastguard Worker   snprintf(filename, sizeof(filename), "%s/motion_search_feature_sb%d", path,
2375*77c1e3ccSAndroid Build Coastguard Worker            sb_counter);
2376*77c1e3ccSAndroid Build Coastguard Worker   FILE *pfile = fopen(filename, "w");
2377*77c1e3ccSAndroid Build Coastguard Worker   fprintf(pfile, "%d,%d,%d,%d,%d\n", mi_row, mi_col, bsize,
2378*77c1e3ccSAndroid Build Coastguard Worker           block_size_wide[fixed_block_size], num_blocks);
2379*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < num_blocks; ++i) {
2380*77c1e3ccSAndroid Build Coastguard Worker     fprintf(pfile, "%d", block_sse[i]);
2381*77c1e3ccSAndroid Build Coastguard Worker     if (i < num_blocks - 1) fprintf(pfile, ",");
2382*77c1e3ccSAndroid Build Coastguard Worker   }
2383*77c1e3ccSAndroid Build Coastguard Worker   fprintf(pfile, "\n");
2384*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < num_blocks; ++i) {
2385*77c1e3ccSAndroid Build Coastguard Worker     fprintf(pfile, "%d", block_var[i]);
2386*77c1e3ccSAndroid Build Coastguard Worker     if (i < num_blocks - 1) fprintf(pfile, ",");
2387*77c1e3ccSAndroid Build Coastguard Worker   }
2388*77c1e3ccSAndroid Build Coastguard Worker   fprintf(pfile, "\n");
2389*77c1e3ccSAndroid Build Coastguard Worker   fclose(pfile);
2390*77c1e3ccSAndroid Build Coastguard Worker }
2391*77c1e3ccSAndroid Build Coastguard Worker 
av1_collect_motion_search_features_sb(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,const int mi_row,const int mi_col,const BLOCK_SIZE bsize,aom_partition_features_t * features)2392*77c1e3ccSAndroid Build Coastguard Worker void av1_collect_motion_search_features_sb(AV1_COMP *const cpi, ThreadData *td,
2393*77c1e3ccSAndroid Build Coastguard Worker                                            TileDataEnc *tile_data,
2394*77c1e3ccSAndroid Build Coastguard Worker                                            const int mi_row, const int mi_col,
2395*77c1e3ccSAndroid Build Coastguard Worker                                            const BLOCK_SIZE bsize,
2396*77c1e3ccSAndroid Build Coastguard Worker                                            aom_partition_features_t *features) {
2397*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
2398*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm)) return;
2399*77c1e3ccSAndroid Build Coastguard Worker 
2400*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
2401*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE fixed_block_size = BLOCK_16X16;
2402*77c1e3ccSAndroid Build Coastguard Worker   const int col_step = mi_size_wide[fixed_block_size];
2403*77c1e3ccSAndroid Build Coastguard Worker   const int row_step = mi_size_high[fixed_block_size];
2404*77c1e3ccSAndroid Build Coastguard Worker   SIMPLE_MOTION_DATA_TREE *sms_tree = NULL;
2405*77c1e3ccSAndroid Build Coastguard Worker   const int stat_generation_stage = is_stat_generation_stage(cpi);
2406*77c1e3ccSAndroid Build Coastguard Worker   const int is_sb_size_128 = cm->seq_params->sb_size == BLOCK_128X128;
2407*77c1e3ccSAndroid Build Coastguard Worker   const int tree_nodes =
2408*77c1e3ccSAndroid Build Coastguard Worker       av1_get_pc_tree_nodes(is_sb_size_128, stat_generation_stage);
2409*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(cm, sms_tree, aom_calloc(tree_nodes, sizeof(*sms_tree)));
2410*77c1e3ccSAndroid Build Coastguard Worker   SIMPLE_MOTION_DATA_TREE *sms_root = setup_sms_tree(cpi, sms_tree);
2411*77c1e3ccSAndroid Build Coastguard Worker   TileInfo *const tile_info = &tile_data->tile_info;
2412*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets_without_segment_id(cpi, tile_info, x, mi_row, mi_col, bsize);
2413*77c1e3ccSAndroid Build Coastguard Worker   av1_init_simple_motion_search_mvs_for_sb(cpi, NULL, x, sms_root, mi_row,
2414*77c1e3ccSAndroid Build Coastguard Worker                                            mi_col);
2415*77c1e3ccSAndroid Build Coastguard Worker   av1_reset_simple_motion_tree_partition(sms_root, bsize);
2416*77c1e3ccSAndroid Build Coastguard Worker   const int ref_list[] = { cpi->rc.is_src_frame_alt_ref ? ALTREF_FRAME
2417*77c1e3ccSAndroid Build Coastguard Worker                                                         : LAST_FRAME };
2418*77c1e3ccSAndroid Build Coastguard Worker   const int mi_width =
2419*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(mi_size_wide[bsize], cm->mi_params.mi_cols - mi_col);
2420*77c1e3ccSAndroid Build Coastguard Worker   const int mi_height =
2421*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(mi_size_high[bsize], cm->mi_params.mi_rows - mi_row);
2422*77c1e3ccSAndroid Build Coastguard Worker   const int col_steps = (mi_width / col_step) + ((mi_width % col_step) > 0);
2423*77c1e3ccSAndroid Build Coastguard Worker   const int row_steps = (mi_height / row_step) + ((mi_height % row_step) > 0);
2424*77c1e3ccSAndroid Build Coastguard Worker   const int num_blocks = col_steps * row_steps;
2425*77c1e3ccSAndroid Build Coastguard Worker   unsigned int *block_sse = aom_calloc(num_blocks, sizeof(*block_sse));
2426*77c1e3ccSAndroid Build Coastguard Worker   unsigned int *block_var = aom_calloc(num_blocks, sizeof(*block_var));
2427*77c1e3ccSAndroid Build Coastguard Worker   if (!(block_sse && block_var)) {
2428*77c1e3ccSAndroid Build Coastguard Worker     aom_free(sms_tree);
2429*77c1e3ccSAndroid Build Coastguard Worker     aom_free(block_sse);
2430*77c1e3ccSAndroid Build Coastguard Worker     aom_free(block_var);
2431*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
2432*77c1e3ccSAndroid Build Coastguard Worker                        "Error allocating block_sse & block_var");
2433*77c1e3ccSAndroid Build Coastguard Worker   }
2434*77c1e3ccSAndroid Build Coastguard Worker   int idx = 0;
2435*77c1e3ccSAndroid Build Coastguard Worker 
2436*77c1e3ccSAndroid Build Coastguard Worker   for (int row = mi_row;
2437*77c1e3ccSAndroid Build Coastguard Worker        row < AOMMIN(mi_row + mi_size_high[bsize], cm->mi_params.mi_rows);
2438*77c1e3ccSAndroid Build Coastguard Worker        row += row_step) {
2439*77c1e3ccSAndroid Build Coastguard Worker     for (int col = mi_col;
2440*77c1e3ccSAndroid Build Coastguard Worker          col < AOMMIN(mi_col + mi_size_wide[bsize], cm->mi_params.mi_cols);
2441*77c1e3ccSAndroid Build Coastguard Worker          col += col_step) {
2442*77c1e3ccSAndroid Build Coastguard Worker       simple_motion_search_get_best_ref(
2443*77c1e3ccSAndroid Build Coastguard Worker           cpi, x, sms_root, row, col, fixed_block_size, ref_list,
2444*77c1e3ccSAndroid Build Coastguard Worker           /*num_refs=*/1, /*use_subpixel=*/1,
2445*77c1e3ccSAndroid Build Coastguard Worker           /*save_mv=*/1, &block_sse[idx], &block_var[idx]);
2446*77c1e3ccSAndroid Build Coastguard Worker       ++idx;
2447*77c1e3ccSAndroid Build Coastguard Worker     }
2448*77c1e3ccSAndroid Build Coastguard Worker   }
2449*77c1e3ccSAndroid Build Coastguard Worker   if (features == NULL) {
2450*77c1e3ccSAndroid Build Coastguard Worker     write_motion_feature_to_file(cpi->oxcf.partition_info_path, cpi->sb_counter,
2451*77c1e3ccSAndroid Build Coastguard Worker                                  block_sse, block_var, idx, bsize,
2452*77c1e3ccSAndroid Build Coastguard Worker                                  fixed_block_size, mi_row, mi_col);
2453*77c1e3ccSAndroid Build Coastguard Worker   } else {
2454*77c1e3ccSAndroid Build Coastguard Worker     features->sb_features.motion_features.unit_length =
2455*77c1e3ccSAndroid Build Coastguard Worker         block_size_wide[fixed_block_size];
2456*77c1e3ccSAndroid Build Coastguard Worker     features->sb_features.motion_features.num_units = idx;
2457*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < idx; ++i) {
2458*77c1e3ccSAndroid Build Coastguard Worker       features->sb_features.motion_features.block_sse[i] = block_sse[i];
2459*77c1e3ccSAndroid Build Coastguard Worker       features->sb_features.motion_features.block_var[i] = block_var[i];
2460*77c1e3ccSAndroid Build Coastguard Worker     }
2461*77c1e3ccSAndroid Build Coastguard Worker   }
2462*77c1e3ccSAndroid Build Coastguard Worker 
2463*77c1e3ccSAndroid Build Coastguard Worker   aom_free(block_sse);
2464*77c1e3ccSAndroid Build Coastguard Worker   aom_free(block_var);
2465*77c1e3ccSAndroid Build Coastguard Worker   aom_free(sms_tree);
2466*77c1e3ccSAndroid Build Coastguard Worker }
2467*77c1e3ccSAndroid Build Coastguard Worker 
av1_prepare_motion_search_features_block(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,const int mi_row,const int mi_col,const BLOCK_SIZE bsize,const int valid_partition_types,unsigned int * block_sse,unsigned int * block_var,unsigned int sub_block_sse[4],unsigned int sub_block_var[4],unsigned int horz_block_sse[2],unsigned int horz_block_var[2],unsigned int vert_block_sse[2],unsigned int vert_block_var[2])2468*77c1e3ccSAndroid Build Coastguard Worker void av1_prepare_motion_search_features_block(
2469*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, ThreadData *td, TileDataEnc *tile_data,
2470*77c1e3ccSAndroid Build Coastguard Worker     const int mi_row, const int mi_col, const BLOCK_SIZE bsize,
2471*77c1e3ccSAndroid Build Coastguard Worker     const int valid_partition_types, unsigned int *block_sse,
2472*77c1e3ccSAndroid Build Coastguard Worker     unsigned int *block_var, unsigned int sub_block_sse[4],
2473*77c1e3ccSAndroid Build Coastguard Worker     unsigned int sub_block_var[4], unsigned int horz_block_sse[2],
2474*77c1e3ccSAndroid Build Coastguard Worker     unsigned int horz_block_var[2], unsigned int vert_block_sse[2],
2475*77c1e3ccSAndroid Build Coastguard Worker     unsigned int vert_block_var[2]) {
2476*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
2477*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm)) return;
2478*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
2479*77c1e3ccSAndroid Build Coastguard Worker   SIMPLE_MOTION_DATA_TREE *sms_tree = NULL;
2480*77c1e3ccSAndroid Build Coastguard Worker   const int stat_generation_stage = is_stat_generation_stage(cpi);
2481*77c1e3ccSAndroid Build Coastguard Worker   const int is_sb_size_128 = cm->seq_params->sb_size == BLOCK_128X128;
2482*77c1e3ccSAndroid Build Coastguard Worker   const int tree_nodes =
2483*77c1e3ccSAndroid Build Coastguard Worker       av1_get_pc_tree_nodes(is_sb_size_128, stat_generation_stage);
2484*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(cm, sms_tree, aom_calloc(tree_nodes, sizeof(*sms_tree)));
2485*77c1e3ccSAndroid Build Coastguard Worker   SIMPLE_MOTION_DATA_TREE *sms_root = setup_sms_tree(cpi, sms_tree);
2486*77c1e3ccSAndroid Build Coastguard Worker   TileInfo *const tile_info = &tile_data->tile_info;
2487*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets_without_segment_id(cpi, tile_info, x, mi_row, mi_col, bsize);
2488*77c1e3ccSAndroid Build Coastguard Worker   av1_reset_simple_motion_tree_partition(sms_root, bsize);
2489*77c1e3ccSAndroid Build Coastguard Worker   const int ref_list[] = { cpi->rc.is_src_frame_alt_ref ? ALTREF_FRAME
2490*77c1e3ccSAndroid Build Coastguard Worker                                                         : LAST_FRAME };
2491*77c1e3ccSAndroid Build Coastguard Worker   const int sub_mi_width = mi_size_wide[bsize] / 2;
2492*77c1e3ccSAndroid Build Coastguard Worker   const int sub_mi_height = sub_mi_width;
2493*77c1e3ccSAndroid Build Coastguard Worker   simple_motion_search_get_best_ref(
2494*77c1e3ccSAndroid Build Coastguard Worker       cpi, x, sms_root, mi_row, mi_col, bsize, ref_list, /*num_refs=*/1,
2495*77c1e3ccSAndroid Build Coastguard Worker       /*use_subpixel=*/1, /*save_mv=*/1, block_sse, block_var);
2496*77c1e3ccSAndroid Build Coastguard Worker   // Split to 4 sub blocks.
2497*77c1e3ccSAndroid Build Coastguard Worker   if (valid_partition_types & (1 << PARTITION_SPLIT)) {
2498*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
2499*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < 4; ++i) {
2500*77c1e3ccSAndroid Build Coastguard Worker       const int row = mi_row + (i >> 1) * sub_mi_height;
2501*77c1e3ccSAndroid Build Coastguard Worker       const int col = mi_col + (i & 1) * sub_mi_width;
2502*77c1e3ccSAndroid Build Coastguard Worker       simple_motion_search_get_best_ref(cpi, x, sms_root, row, col, subsize,
2503*77c1e3ccSAndroid Build Coastguard Worker                                         ref_list, /*num_refs=*/1,
2504*77c1e3ccSAndroid Build Coastguard Worker                                         /*use_subpixel=*/1, /*save_mv=*/1,
2505*77c1e3ccSAndroid Build Coastguard Worker                                         &sub_block_sse[i], &sub_block_var[i]);
2506*77c1e3ccSAndroid Build Coastguard Worker     }
2507*77c1e3ccSAndroid Build Coastguard Worker   }
2508*77c1e3ccSAndroid Build Coastguard Worker   // Horizontal split
2509*77c1e3ccSAndroid Build Coastguard Worker   if (valid_partition_types & (1 << PARTITION_HORZ)) {
2510*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_HORZ);
2511*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < 2; ++i) {
2512*77c1e3ccSAndroid Build Coastguard Worker       const int row = mi_row + (i & 1) * sub_mi_height;
2513*77c1e3ccSAndroid Build Coastguard Worker       const int col = mi_col;
2514*77c1e3ccSAndroid Build Coastguard Worker       simple_motion_search_get_best_ref(cpi, x, sms_root, row, col, subsize,
2515*77c1e3ccSAndroid Build Coastguard Worker                                         ref_list, /*num_refs=*/1,
2516*77c1e3ccSAndroid Build Coastguard Worker                                         /*use_subpixel=*/1, /*save_mv=*/1,
2517*77c1e3ccSAndroid Build Coastguard Worker                                         &horz_block_sse[i], &horz_block_var[i]);
2518*77c1e3ccSAndroid Build Coastguard Worker     }
2519*77c1e3ccSAndroid Build Coastguard Worker   }
2520*77c1e3ccSAndroid Build Coastguard Worker   // Vertical split
2521*77c1e3ccSAndroid Build Coastguard Worker   if (valid_partition_types & (1 << PARTITION_VERT)) {
2522*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_VERT);
2523*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < 2; ++i) {
2524*77c1e3ccSAndroid Build Coastguard Worker       const int row = mi_row;
2525*77c1e3ccSAndroid Build Coastguard Worker       const int col = mi_col + (i & 1) * sub_mi_width;
2526*77c1e3ccSAndroid Build Coastguard Worker       simple_motion_search_get_best_ref(cpi, x, sms_root, row, col, subsize,
2527*77c1e3ccSAndroid Build Coastguard Worker                                         ref_list, /*num_refs=*/1,
2528*77c1e3ccSAndroid Build Coastguard Worker                                         /*use_subpixel=*/1, /*save_mv=*/1,
2529*77c1e3ccSAndroid Build Coastguard Worker                                         &vert_block_sse[i], &vert_block_var[i]);
2530*77c1e3ccSAndroid Build Coastguard Worker     }
2531*77c1e3ccSAndroid Build Coastguard Worker   }
2532*77c1e3ccSAndroid Build Coastguard Worker 
2533*77c1e3ccSAndroid Build Coastguard Worker   aom_free(sms_tree);
2534*77c1e3ccSAndroid Build Coastguard Worker }
2535*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
2536*77c1e3ccSAndroid Build Coastguard Worker 
init_simple_motion_search_mvs(SIMPLE_MOTION_DATA_TREE * sms_tree,const FULLPEL_MV * start_mvs)2537*77c1e3ccSAndroid Build Coastguard Worker static inline void init_simple_motion_search_mvs(
2538*77c1e3ccSAndroid Build Coastguard Worker     SIMPLE_MOTION_DATA_TREE *sms_tree, const FULLPEL_MV *start_mvs) {
2539*77c1e3ccSAndroid Build Coastguard Worker   memcpy(sms_tree->start_mvs, start_mvs, sizeof(sms_tree->start_mvs));
2540*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(sms_tree->sms_none_feat);
2541*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(sms_tree->sms_rect_feat);
2542*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(sms_tree->sms_none_valid);
2543*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(sms_tree->sms_rect_valid);
2544*77c1e3ccSAndroid Build Coastguard Worker 
2545*77c1e3ccSAndroid Build Coastguard Worker   if (sms_tree->block_size >= BLOCK_8X8) {
2546*77c1e3ccSAndroid Build Coastguard Worker     init_simple_motion_search_mvs(sms_tree->split[0], start_mvs);
2547*77c1e3ccSAndroid Build Coastguard Worker     init_simple_motion_search_mvs(sms_tree->split[1], start_mvs);
2548*77c1e3ccSAndroid Build Coastguard Worker     init_simple_motion_search_mvs(sms_tree->split[2], start_mvs);
2549*77c1e3ccSAndroid Build Coastguard Worker     init_simple_motion_search_mvs(sms_tree->split[3], start_mvs);
2550*77c1e3ccSAndroid Build Coastguard Worker   }
2551*77c1e3ccSAndroid Build Coastguard Worker }
2552*77c1e3ccSAndroid Build Coastguard Worker 
av1_init_simple_motion_search_mvs_for_sb(const AV1_COMP * cpi,const TileInfo * tile_info,MACROBLOCK * x,SIMPLE_MOTION_DATA_TREE * sms_root,int mi_row,int mi_col)2553*77c1e3ccSAndroid Build Coastguard Worker void av1_init_simple_motion_search_mvs_for_sb(const AV1_COMP *cpi,
2554*77c1e3ccSAndroid Build Coastguard Worker                                               const TileInfo *tile_info,
2555*77c1e3ccSAndroid Build Coastguard Worker                                               MACROBLOCK *x,
2556*77c1e3ccSAndroid Build Coastguard Worker                                               SIMPLE_MOTION_DATA_TREE *sms_root,
2557*77c1e3ccSAndroid Build Coastguard Worker                                               int mi_row, int mi_col) {
2558*77c1e3ccSAndroid Build Coastguard Worker   // Use the NEARESTMV of the sb as the start mv
2559*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *cm = &cpi->common;
2560*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
2561*77c1e3ccSAndroid Build Coastguard Worker   FULLPEL_MV ref_mvs[REF_FRAMES];
2562*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
2563*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(ref_mvs);
2564*77c1e3ccSAndroid Build Coastguard Worker   // If tile_info is NULL, assume that the offsets have already been set.
2565*77c1e3ccSAndroid Build Coastguard Worker   if (tile_info) {
2566*77c1e3ccSAndroid Build Coastguard Worker     av1_set_offsets_without_segment_id(cpi, tile_info, x, mi_row, mi_col,
2567*77c1e3ccSAndroid Build Coastguard Worker                                        sb_size);
2568*77c1e3ccSAndroid Build Coastguard Worker   }
2569*77c1e3ccSAndroid Build Coastguard Worker 
2570*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO_EXT mbmi_ext;
2571*77c1e3ccSAndroid Build Coastguard Worker   const int ref_frame =
2572*77c1e3ccSAndroid Build Coastguard Worker       cpi->rc.is_src_frame_alt_ref ? ALTREF_FRAME : LAST_FRAME;
2573*77c1e3ccSAndroid Build Coastguard Worker   av1_find_mv_refs(cm, xd, xd->mi[0], ref_frame, mbmi_ext.ref_mv_count,
2574*77c1e3ccSAndroid Build Coastguard Worker                    xd->ref_mv_stack, xd->weight, NULL, mbmi_ext.global_mvs,
2575*77c1e3ccSAndroid Build Coastguard Worker                    mbmi_ext.mode_context);
2576*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi_ext.ref_mv_count[ref_frame] > 0) {
2577*77c1e3ccSAndroid Build Coastguard Worker     ref_mvs[ref_frame] =
2578*77c1e3ccSAndroid Build Coastguard Worker         get_fullmv_from_mv(&xd->ref_mv_stack[ref_frame][0].this_mv.as_mv);
2579*77c1e3ccSAndroid Build Coastguard Worker   } else {
2580*77c1e3ccSAndroid Build Coastguard Worker     ref_mvs[ref_frame] =
2581*77c1e3ccSAndroid Build Coastguard Worker         get_fullmv_from_mv(&mbmi_ext.global_mvs[ref_frame].as_mv);
2582*77c1e3ccSAndroid Build Coastguard Worker   }
2583*77c1e3ccSAndroid Build Coastguard Worker 
2584*77c1e3ccSAndroid Build Coastguard Worker   init_simple_motion_search_mvs(sms_root, ref_mvs);
2585*77c1e3ccSAndroid Build Coastguard Worker }
2586