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