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