xref: /aosp_15_r20/external/libaom/av1/encoder/partition_search.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2020, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <float.h>
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/txfm_common.h"
15*77c1e3ccSAndroid Build Coastguard Worker 
16*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/av1_common_int.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/blockd.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/enums.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconintra.h"
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/aq_complexity.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/aq_variance.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/context_tree.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
25*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodeframe.h"
26*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodeframe_utils.h"
27*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodemv.h"
28*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/intra_mode_search_utils.h"
29*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/motion_search_facade.h"
30*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/nonrd_opt.h"
31*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/partition_search.h"
32*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/partition_strategy.h"
33*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/reconinter_enc.h"
34*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tokenize.h"
35*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/var_based_part.h"
36*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/av1_ml_partition_models.h"
37*77c1e3ccSAndroid Build Coastguard Worker 
38*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
39*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tune_vmaf.h"
40*77c1e3ccSAndroid Build Coastguard Worker #endif
41*77c1e3ccSAndroid Build Coastguard Worker 
42*77c1e3ccSAndroid Build Coastguard Worker #define COLLECT_MOTION_SEARCH_FEATURE_SB 0
43*77c1e3ccSAndroid Build Coastguard Worker 
av1_reset_part_sf(PARTITION_SPEED_FEATURES * part_sf)44*77c1e3ccSAndroid Build Coastguard Worker void av1_reset_part_sf(PARTITION_SPEED_FEATURES *part_sf) {
45*77c1e3ccSAndroid Build Coastguard Worker   part_sf->partition_search_type = SEARCH_PARTITION;
46*77c1e3ccSAndroid Build Coastguard Worker   part_sf->less_rectangular_check_level = 0;
47*77c1e3ccSAndroid Build Coastguard Worker   part_sf->use_square_partition_only_threshold = BLOCK_128X128;
48*77c1e3ccSAndroid Build Coastguard Worker   part_sf->auto_max_partition_based_on_simple_motion = NOT_IN_USE;
49*77c1e3ccSAndroid Build Coastguard Worker   part_sf->default_max_partition_size = BLOCK_LARGEST;
50*77c1e3ccSAndroid Build Coastguard Worker   part_sf->default_min_partition_size = BLOCK_4X4;
51*77c1e3ccSAndroid Build Coastguard Worker   part_sf->adjust_var_based_rd_partitioning = 0;
52*77c1e3ccSAndroid Build Coastguard Worker   part_sf->max_intra_bsize = BLOCK_LARGEST;
53*77c1e3ccSAndroid Build Coastguard Worker   // This setting only takes effect when partition_search_type is set
54*77c1e3ccSAndroid Build Coastguard Worker   // to FIXED_PARTITION.
55*77c1e3ccSAndroid Build Coastguard Worker   part_sf->fixed_partition_size = BLOCK_16X16;
56*77c1e3ccSAndroid Build Coastguard Worker   // Recode loop tolerance %.
57*77c1e3ccSAndroid Build Coastguard Worker   part_sf->partition_search_breakout_dist_thr = 0;
58*77c1e3ccSAndroid Build Coastguard Worker   part_sf->partition_search_breakout_rate_thr = 0;
59*77c1e3ccSAndroid Build Coastguard Worker   part_sf->prune_ext_partition_types_search_level = 0;
60*77c1e3ccSAndroid Build Coastguard Worker   part_sf->prune_part4_search = 0;
61*77c1e3ccSAndroid Build Coastguard Worker   part_sf->ml_prune_partition = 0;
62*77c1e3ccSAndroid Build Coastguard Worker   part_sf->ml_early_term_after_part_split_level = 0;
63*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < PARTITION_BLOCK_SIZES; ++i) {
64*77c1e3ccSAndroid Build Coastguard Worker     part_sf->ml_partition_search_breakout_thresh[i] =
65*77c1e3ccSAndroid Build Coastguard Worker         -1;  // -1 means not enabled.
66*77c1e3ccSAndroid Build Coastguard Worker   }
67*77c1e3ccSAndroid Build Coastguard Worker   part_sf->simple_motion_search_prune_agg = SIMPLE_AGG_LVL0;
68*77c1e3ccSAndroid Build Coastguard Worker   part_sf->simple_motion_search_split = 0;
69*77c1e3ccSAndroid Build Coastguard Worker   part_sf->simple_motion_search_prune_rect = 0;
70*77c1e3ccSAndroid Build Coastguard Worker   part_sf->simple_motion_search_early_term_none = 0;
71*77c1e3ccSAndroid Build Coastguard Worker   part_sf->simple_motion_search_reduce_search_steps = 0;
72*77c1e3ccSAndroid Build Coastguard Worker   part_sf->intra_cnn_based_part_prune_level = 0;
73*77c1e3ccSAndroid Build Coastguard Worker   part_sf->ext_partition_eval_thresh = BLOCK_8X8;
74*77c1e3ccSAndroid Build Coastguard Worker   part_sf->rect_partition_eval_thresh = BLOCK_128X128;
75*77c1e3ccSAndroid Build Coastguard Worker   part_sf->ext_part_eval_based_on_cur_best = 0;
76*77c1e3ccSAndroid Build Coastguard Worker   part_sf->prune_ext_part_using_split_info = 0;
77*77c1e3ccSAndroid Build Coastguard Worker   part_sf->prune_rectangular_split_based_on_qidx = 0;
78*77c1e3ccSAndroid Build Coastguard Worker   part_sf->early_term_after_none_split = 0;
79*77c1e3ccSAndroid Build Coastguard Worker   part_sf->ml_predict_breakout_level = 0;
80*77c1e3ccSAndroid Build Coastguard Worker   part_sf->prune_sub_8x8_partition_level = 0;
81*77c1e3ccSAndroid Build Coastguard Worker   part_sf->simple_motion_search_rect_split = 0;
82*77c1e3ccSAndroid Build Coastguard Worker   part_sf->reuse_prev_rd_results_for_part_ab = 0;
83*77c1e3ccSAndroid Build Coastguard Worker   part_sf->reuse_best_prediction_for_part_ab = 0;
84*77c1e3ccSAndroid Build Coastguard Worker   part_sf->use_best_rd_for_pruning = 0;
85*77c1e3ccSAndroid Build Coastguard Worker   part_sf->skip_non_sq_part_based_on_none = 0;
86*77c1e3ccSAndroid Build Coastguard Worker }
87*77c1e3ccSAndroid Build Coastguard Worker 
88*77c1e3ccSAndroid Build Coastguard Worker // Reset speed features that works for the baseline encoding, but
89*77c1e3ccSAndroid Build Coastguard Worker // blocks the external partition search.
av1_reset_sf_for_ext_part(AV1_COMP * const cpi)90*77c1e3ccSAndroid Build Coastguard Worker void av1_reset_sf_for_ext_part(AV1_COMP *const cpi) {
91*77c1e3ccSAndroid Build Coastguard Worker   cpi->sf.inter_sf.prune_ref_frame_for_rect_partitions = 0;
92*77c1e3ccSAndroid Build Coastguard Worker }
93*77c1e3ccSAndroid Build Coastguard Worker 
94*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
95*77c1e3ccSAndroid Build Coastguard Worker // If input |features| is NULL, write tpl stats to file for each super block.
96*77c1e3ccSAndroid Build Coastguard Worker // Otherwise, store tpl stats to |features|.
97*77c1e3ccSAndroid Build Coastguard Worker // The tpl stats is computed in the unit of tpl_bsize_1d (16x16).
98*77c1e3ccSAndroid Build Coastguard Worker // When writing to text file:
99*77c1e3ccSAndroid Build Coastguard Worker // The first row contains super block position, super block size,
100*77c1e3ccSAndroid Build Coastguard Worker // tpl unit length, number of units in the super block.
101*77c1e3ccSAndroid Build Coastguard Worker // The second row contains the intra prediction cost for each unit.
102*77c1e3ccSAndroid Build Coastguard Worker // The third row contains the inter prediction cost for each unit.
103*77c1e3ccSAndroid Build Coastguard Worker // The forth row contains the motion compensated dependency cost for each unit.
collect_tpl_stats_sb(const AV1_COMP * const cpi,const BLOCK_SIZE bsize,const int mi_row,const int mi_col,aom_partition_features_t * features)104*77c1e3ccSAndroid Build Coastguard Worker static void collect_tpl_stats_sb(const AV1_COMP *const cpi,
105*77c1e3ccSAndroid Build Coastguard Worker                                  const BLOCK_SIZE bsize, const int mi_row,
106*77c1e3ccSAndroid Build Coastguard Worker                                  const int mi_col,
107*77c1e3ccSAndroid Build Coastguard Worker                                  aom_partition_features_t *features) {
108*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
109*77c1e3ccSAndroid Build Coastguard Worker   GF_GROUP *gf_group = &cpi->ppi->gf_group;
110*77c1e3ccSAndroid Build Coastguard Worker   if (gf_group->update_type[cpi->gf_frame_index] == INTNL_OVERLAY_UPDATE ||
111*77c1e3ccSAndroid Build Coastguard Worker       gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE) {
112*77c1e3ccSAndroid Build Coastguard Worker     return;
113*77c1e3ccSAndroid Build Coastguard Worker   }
114*77c1e3ccSAndroid Build Coastguard Worker 
115*77c1e3ccSAndroid Build Coastguard Worker   TplParams *const tpl_data = &cpi->ppi->tpl_data;
116*77c1e3ccSAndroid Build Coastguard Worker   TplDepFrame *tpl_frame = &tpl_data->tpl_frame[cpi->gf_frame_index];
117*77c1e3ccSAndroid Build Coastguard Worker   TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
118*77c1e3ccSAndroid Build Coastguard Worker   // If tpl stats is not established, early return
119*77c1e3ccSAndroid Build Coastguard Worker   if (!tpl_data->ready || gf_group->max_layer_depth_allowed == 0) {
120*77c1e3ccSAndroid Build Coastguard Worker     if (features != NULL) features->sb_features.tpl_features.available = 0;
121*77c1e3ccSAndroid Build Coastguard Worker     return;
122*77c1e3ccSAndroid Build Coastguard Worker   }
123*77c1e3ccSAndroid Build Coastguard Worker 
124*77c1e3ccSAndroid Build Coastguard Worker   const int tpl_stride = tpl_frame->stride;
125*77c1e3ccSAndroid Build Coastguard Worker   const int step = 1 << tpl_data->tpl_stats_block_mis_log2;
126*77c1e3ccSAndroid Build Coastguard Worker   const int mi_width =
127*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(mi_size_wide[bsize], cm->mi_params.mi_cols - mi_col);
128*77c1e3ccSAndroid Build Coastguard Worker   const int mi_height =
129*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(mi_size_high[bsize], cm->mi_params.mi_rows - mi_row);
130*77c1e3ccSAndroid Build Coastguard Worker   const int col_steps = (mi_width / step) + ((mi_width % step) > 0);
131*77c1e3ccSAndroid Build Coastguard Worker   const int row_steps = (mi_height / step) + ((mi_height % step) > 0);
132*77c1e3ccSAndroid Build Coastguard Worker   const int num_blocks = col_steps * row_steps;
133*77c1e3ccSAndroid Build Coastguard Worker 
134*77c1e3ccSAndroid Build Coastguard Worker   if (features == NULL) {
135*77c1e3ccSAndroid Build Coastguard Worker     char filename[256];
136*77c1e3ccSAndroid Build Coastguard Worker     snprintf(filename, sizeof(filename), "%s/tpl_feature_sb%d",
137*77c1e3ccSAndroid Build Coastguard Worker              cpi->oxcf.partition_info_path, cpi->sb_counter);
138*77c1e3ccSAndroid Build Coastguard Worker     FILE *pfile = fopen(filename, "w");
139*77c1e3ccSAndroid Build Coastguard Worker     fprintf(pfile, "%d,%d,%d,%d,%d\n", mi_row, mi_col, bsize,
140*77c1e3ccSAndroid Build Coastguard Worker             tpl_data->tpl_bsize_1d, num_blocks);
141*77c1e3ccSAndroid Build Coastguard Worker     int count = 0;
142*77c1e3ccSAndroid Build Coastguard Worker     for (int row = 0; row < mi_height; row += step) {
143*77c1e3ccSAndroid Build Coastguard Worker       for (int col = 0; col < mi_width; col += step) {
144*77c1e3ccSAndroid Build Coastguard Worker         TplDepStats *this_stats =
145*77c1e3ccSAndroid Build Coastguard Worker             &tpl_stats[av1_tpl_ptr_pos(mi_row + row, mi_col + col, tpl_stride,
146*77c1e3ccSAndroid Build Coastguard Worker                                        tpl_data->tpl_stats_block_mis_log2)];
147*77c1e3ccSAndroid Build Coastguard Worker         fprintf(pfile, "%.0f", (double)this_stats->intra_cost);
148*77c1e3ccSAndroid Build Coastguard Worker         if (count < num_blocks - 1) fprintf(pfile, ",");
149*77c1e3ccSAndroid Build Coastguard Worker         ++count;
150*77c1e3ccSAndroid Build Coastguard Worker       }
151*77c1e3ccSAndroid Build Coastguard Worker     }
152*77c1e3ccSAndroid Build Coastguard Worker     fprintf(pfile, "\n");
153*77c1e3ccSAndroid Build Coastguard Worker     count = 0;
154*77c1e3ccSAndroid Build Coastguard Worker     for (int row = 0; row < mi_height; row += step) {
155*77c1e3ccSAndroid Build Coastguard Worker       for (int col = 0; col < mi_width; col += step) {
156*77c1e3ccSAndroid Build Coastguard Worker         TplDepStats *this_stats =
157*77c1e3ccSAndroid Build Coastguard Worker             &tpl_stats[av1_tpl_ptr_pos(mi_row + row, mi_col + col, tpl_stride,
158*77c1e3ccSAndroid Build Coastguard Worker                                        tpl_data->tpl_stats_block_mis_log2)];
159*77c1e3ccSAndroid Build Coastguard Worker         fprintf(pfile, "%.0f", (double)this_stats->inter_cost);
160*77c1e3ccSAndroid Build Coastguard Worker         if (count < num_blocks - 1) fprintf(pfile, ",");
161*77c1e3ccSAndroid Build Coastguard Worker         ++count;
162*77c1e3ccSAndroid Build Coastguard Worker       }
163*77c1e3ccSAndroid Build Coastguard Worker     }
164*77c1e3ccSAndroid Build Coastguard Worker     fprintf(pfile, "\n");
165*77c1e3ccSAndroid Build Coastguard Worker     count = 0;
166*77c1e3ccSAndroid Build Coastguard Worker     for (int row = 0; row < mi_height; row += step) {
167*77c1e3ccSAndroid Build Coastguard Worker       for (int col = 0; col < mi_width; col += step) {
168*77c1e3ccSAndroid Build Coastguard Worker         TplDepStats *this_stats =
169*77c1e3ccSAndroid Build Coastguard Worker             &tpl_stats[av1_tpl_ptr_pos(mi_row + row, mi_col + col, tpl_stride,
170*77c1e3ccSAndroid Build Coastguard Worker                                        tpl_data->tpl_stats_block_mis_log2)];
171*77c1e3ccSAndroid Build Coastguard Worker         const int64_t mc_dep_delta =
172*77c1e3ccSAndroid Build Coastguard Worker             RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
173*77c1e3ccSAndroid Build Coastguard Worker                    this_stats->mc_dep_dist);
174*77c1e3ccSAndroid Build Coastguard Worker         fprintf(pfile, "%.0f", (double)mc_dep_delta);
175*77c1e3ccSAndroid Build Coastguard Worker         if (count < num_blocks - 1) fprintf(pfile, ",");
176*77c1e3ccSAndroid Build Coastguard Worker         ++count;
177*77c1e3ccSAndroid Build Coastguard Worker       }
178*77c1e3ccSAndroid Build Coastguard Worker     }
179*77c1e3ccSAndroid Build Coastguard Worker     fclose(pfile);
180*77c1e3ccSAndroid Build Coastguard Worker   } else {
181*77c1e3ccSAndroid Build Coastguard Worker     features->sb_features.tpl_features.available = 1;
182*77c1e3ccSAndroid Build Coastguard Worker     features->sb_features.tpl_features.tpl_unit_length = tpl_data->tpl_bsize_1d;
183*77c1e3ccSAndroid Build Coastguard Worker     features->sb_features.tpl_features.num_units = num_blocks;
184*77c1e3ccSAndroid Build Coastguard Worker     int count = 0;
185*77c1e3ccSAndroid Build Coastguard Worker     for (int row = 0; row < mi_height; row += step) {
186*77c1e3ccSAndroid Build Coastguard Worker       for (int col = 0; col < mi_width; col += step) {
187*77c1e3ccSAndroid Build Coastguard Worker         TplDepStats *this_stats =
188*77c1e3ccSAndroid Build Coastguard Worker             &tpl_stats[av1_tpl_ptr_pos(mi_row + row, mi_col + col, tpl_stride,
189*77c1e3ccSAndroid Build Coastguard Worker                                        tpl_data->tpl_stats_block_mis_log2)];
190*77c1e3ccSAndroid Build Coastguard Worker         const int64_t mc_dep_delta =
191*77c1e3ccSAndroid Build Coastguard Worker             RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
192*77c1e3ccSAndroid Build Coastguard Worker                    this_stats->mc_dep_dist);
193*77c1e3ccSAndroid Build Coastguard Worker         features->sb_features.tpl_features.intra_cost[count] =
194*77c1e3ccSAndroid Build Coastguard Worker             this_stats->intra_cost;
195*77c1e3ccSAndroid Build Coastguard Worker         features->sb_features.tpl_features.inter_cost[count] =
196*77c1e3ccSAndroid Build Coastguard Worker             this_stats->inter_cost;
197*77c1e3ccSAndroid Build Coastguard Worker         features->sb_features.tpl_features.mc_dep_cost[count] = mc_dep_delta;
198*77c1e3ccSAndroid Build Coastguard Worker         ++count;
199*77c1e3ccSAndroid Build Coastguard Worker       }
200*77c1e3ccSAndroid Build Coastguard Worker     }
201*77c1e3ccSAndroid Build Coastguard Worker   }
202*77c1e3ccSAndroid Build Coastguard Worker }
203*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
204*77c1e3ccSAndroid Build Coastguard Worker 
update_txfm_count(MACROBLOCK * x,MACROBLOCKD * xd,FRAME_COUNTS * counts,TX_SIZE tx_size,int depth,int blk_row,int blk_col,uint8_t allow_update_cdf)205*77c1e3ccSAndroid Build Coastguard Worker static void update_txfm_count(MACROBLOCK *x, MACROBLOCKD *xd,
206*77c1e3ccSAndroid Build Coastguard Worker                               FRAME_COUNTS *counts, TX_SIZE tx_size, int depth,
207*77c1e3ccSAndroid Build Coastguard Worker                               int blk_row, int blk_col,
208*77c1e3ccSAndroid Build Coastguard Worker                               uint8_t allow_update_cdf) {
209*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *mbmi = xd->mi[0];
210*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = mbmi->bsize;
211*77c1e3ccSAndroid Build Coastguard Worker   const int max_blocks_high = max_block_high(xd, bsize, 0);
212*77c1e3ccSAndroid Build Coastguard Worker   const int max_blocks_wide = max_block_wide(xd, bsize, 0);
213*77c1e3ccSAndroid Build Coastguard Worker   int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
214*77c1e3ccSAndroid Build Coastguard Worker                                    xd->left_txfm_context + blk_row, mbmi->bsize,
215*77c1e3ccSAndroid Build Coastguard Worker                                    tx_size);
216*77c1e3ccSAndroid Build Coastguard Worker   const int txb_size_index = av1_get_txb_size_index(bsize, blk_row, blk_col);
217*77c1e3ccSAndroid Build Coastguard Worker   const TX_SIZE plane_tx_size = mbmi->inter_tx_size[txb_size_index];
218*77c1e3ccSAndroid Build Coastguard Worker 
219*77c1e3ccSAndroid Build Coastguard Worker   if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
220*77c1e3ccSAndroid Build Coastguard Worker   assert(tx_size > TX_4X4);
221*77c1e3ccSAndroid Build Coastguard Worker 
222*77c1e3ccSAndroid Build Coastguard Worker   if (depth == MAX_VARTX_DEPTH) {
223*77c1e3ccSAndroid Build Coastguard Worker     // Don't add to counts in this case
224*77c1e3ccSAndroid Build Coastguard Worker     mbmi->tx_size = tx_size;
225*77c1e3ccSAndroid Build Coastguard Worker     txfm_partition_update(xd->above_txfm_context + blk_col,
226*77c1e3ccSAndroid Build Coastguard Worker                           xd->left_txfm_context + blk_row, tx_size, tx_size);
227*77c1e3ccSAndroid Build Coastguard Worker     return;
228*77c1e3ccSAndroid Build Coastguard Worker   }
229*77c1e3ccSAndroid Build Coastguard Worker 
230*77c1e3ccSAndroid Build Coastguard Worker   if (tx_size == plane_tx_size) {
231*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
232*77c1e3ccSAndroid Build Coastguard Worker     ++counts->txfm_partition[ctx][0];
233*77c1e3ccSAndroid Build Coastguard Worker #endif
234*77c1e3ccSAndroid Build Coastguard Worker     if (allow_update_cdf)
235*77c1e3ccSAndroid Build Coastguard Worker       update_cdf(xd->tile_ctx->txfm_partition_cdf[ctx], 0, 2);
236*77c1e3ccSAndroid Build Coastguard Worker     mbmi->tx_size = tx_size;
237*77c1e3ccSAndroid Build Coastguard Worker     txfm_partition_update(xd->above_txfm_context + blk_col,
238*77c1e3ccSAndroid Build Coastguard Worker                           xd->left_txfm_context + blk_row, tx_size, tx_size);
239*77c1e3ccSAndroid Build Coastguard Worker   } else {
240*77c1e3ccSAndroid Build Coastguard Worker     const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
241*77c1e3ccSAndroid Build Coastguard Worker     const int bsw = tx_size_wide_unit[sub_txs];
242*77c1e3ccSAndroid Build Coastguard Worker     const int bsh = tx_size_high_unit[sub_txs];
243*77c1e3ccSAndroid Build Coastguard Worker 
244*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
245*77c1e3ccSAndroid Build Coastguard Worker     ++counts->txfm_partition[ctx][1];
246*77c1e3ccSAndroid Build Coastguard Worker #endif
247*77c1e3ccSAndroid Build Coastguard Worker     if (allow_update_cdf)
248*77c1e3ccSAndroid Build Coastguard Worker       update_cdf(xd->tile_ctx->txfm_partition_cdf[ctx], 1, 2);
249*77c1e3ccSAndroid Build Coastguard Worker     ++x->txfm_search_info.txb_split_count;
250*77c1e3ccSAndroid Build Coastguard Worker 
251*77c1e3ccSAndroid Build Coastguard Worker     if (sub_txs == TX_4X4) {
252*77c1e3ccSAndroid Build Coastguard Worker       mbmi->inter_tx_size[txb_size_index] = TX_4X4;
253*77c1e3ccSAndroid Build Coastguard Worker       mbmi->tx_size = TX_4X4;
254*77c1e3ccSAndroid Build Coastguard Worker       txfm_partition_update(xd->above_txfm_context + blk_col,
255*77c1e3ccSAndroid Build Coastguard Worker                             xd->left_txfm_context + blk_row, TX_4X4, tx_size);
256*77c1e3ccSAndroid Build Coastguard Worker       return;
257*77c1e3ccSAndroid Build Coastguard Worker     }
258*77c1e3ccSAndroid Build Coastguard Worker 
259*77c1e3ccSAndroid Build Coastguard Worker     for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
260*77c1e3ccSAndroid Build Coastguard Worker       for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
261*77c1e3ccSAndroid Build Coastguard Worker         int offsetr = row;
262*77c1e3ccSAndroid Build Coastguard Worker         int offsetc = col;
263*77c1e3ccSAndroid Build Coastguard Worker 
264*77c1e3ccSAndroid Build Coastguard Worker         update_txfm_count(x, xd, counts, sub_txs, depth + 1, blk_row + offsetr,
265*77c1e3ccSAndroid Build Coastguard Worker                           blk_col + offsetc, allow_update_cdf);
266*77c1e3ccSAndroid Build Coastguard Worker       }
267*77c1e3ccSAndroid Build Coastguard Worker     }
268*77c1e3ccSAndroid Build Coastguard Worker   }
269*77c1e3ccSAndroid Build Coastguard Worker }
270*77c1e3ccSAndroid Build Coastguard Worker 
tx_partition_count_update(const AV1_COMMON * const cm,MACROBLOCK * x,BLOCK_SIZE plane_bsize,FRAME_COUNTS * td_counts,uint8_t allow_update_cdf)271*77c1e3ccSAndroid Build Coastguard Worker static void tx_partition_count_update(const AV1_COMMON *const cm, MACROBLOCK *x,
272*77c1e3ccSAndroid Build Coastguard Worker                                       BLOCK_SIZE plane_bsize,
273*77c1e3ccSAndroid Build Coastguard Worker                                       FRAME_COUNTS *td_counts,
274*77c1e3ccSAndroid Build Coastguard Worker                                       uint8_t allow_update_cdf) {
275*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *xd = &x->e_mbd;
276*77c1e3ccSAndroid Build Coastguard Worker   const int mi_width = mi_size_wide[plane_bsize];
277*77c1e3ccSAndroid Build Coastguard Worker   const int mi_height = mi_size_high[plane_bsize];
278*77c1e3ccSAndroid Build Coastguard Worker   const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, 0);
279*77c1e3ccSAndroid Build Coastguard Worker   const int bh = tx_size_high_unit[max_tx_size];
280*77c1e3ccSAndroid Build Coastguard Worker   const int bw = tx_size_wide_unit[max_tx_size];
281*77c1e3ccSAndroid Build Coastguard Worker 
282*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context =
283*77c1e3ccSAndroid Build Coastguard Worker       cm->above_contexts.txfm[xd->tile.tile_row] + xd->mi_col;
284*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
285*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (xd->mi_row & MAX_MIB_MASK);
286*77c1e3ccSAndroid Build Coastguard Worker 
287*77c1e3ccSAndroid Build Coastguard Worker   for (int idy = 0; idy < mi_height; idy += bh) {
288*77c1e3ccSAndroid Build Coastguard Worker     for (int idx = 0; idx < mi_width; idx += bw) {
289*77c1e3ccSAndroid Build Coastguard Worker       update_txfm_count(x, xd, td_counts, max_tx_size, 0, idy, idx,
290*77c1e3ccSAndroid Build Coastguard Worker                         allow_update_cdf);
291*77c1e3ccSAndroid Build Coastguard Worker     }
292*77c1e3ccSAndroid Build Coastguard Worker   }
293*77c1e3ccSAndroid Build Coastguard Worker }
294*77c1e3ccSAndroid Build Coastguard Worker 
set_txfm_context(MACROBLOCKD * xd,TX_SIZE tx_size,int blk_row,int blk_col)295*77c1e3ccSAndroid Build Coastguard Worker static void set_txfm_context(MACROBLOCKD *xd, TX_SIZE tx_size, int blk_row,
296*77c1e3ccSAndroid Build Coastguard Worker                              int blk_col) {
297*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *mbmi = xd->mi[0];
298*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = mbmi->bsize;
299*77c1e3ccSAndroid Build Coastguard Worker   const int max_blocks_high = max_block_high(xd, bsize, 0);
300*77c1e3ccSAndroid Build Coastguard Worker   const int max_blocks_wide = max_block_wide(xd, bsize, 0);
301*77c1e3ccSAndroid Build Coastguard Worker   const int txb_size_index = av1_get_txb_size_index(bsize, blk_row, blk_col);
302*77c1e3ccSAndroid Build Coastguard Worker   const TX_SIZE plane_tx_size = mbmi->inter_tx_size[txb_size_index];
303*77c1e3ccSAndroid Build Coastguard Worker 
304*77c1e3ccSAndroid Build Coastguard Worker   if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
305*77c1e3ccSAndroid Build Coastguard Worker 
306*77c1e3ccSAndroid Build Coastguard Worker   if (tx_size == plane_tx_size) {
307*77c1e3ccSAndroid Build Coastguard Worker     mbmi->tx_size = tx_size;
308*77c1e3ccSAndroid Build Coastguard Worker     txfm_partition_update(xd->above_txfm_context + blk_col,
309*77c1e3ccSAndroid Build Coastguard Worker                           xd->left_txfm_context + blk_row, tx_size, tx_size);
310*77c1e3ccSAndroid Build Coastguard Worker 
311*77c1e3ccSAndroid Build Coastguard Worker   } else {
312*77c1e3ccSAndroid Build Coastguard Worker     if (tx_size == TX_8X8) {
313*77c1e3ccSAndroid Build Coastguard Worker       mbmi->inter_tx_size[txb_size_index] = TX_4X4;
314*77c1e3ccSAndroid Build Coastguard Worker       mbmi->tx_size = TX_4X4;
315*77c1e3ccSAndroid Build Coastguard Worker       txfm_partition_update(xd->above_txfm_context + blk_col,
316*77c1e3ccSAndroid Build Coastguard Worker                             xd->left_txfm_context + blk_row, TX_4X4, tx_size);
317*77c1e3ccSAndroid Build Coastguard Worker       return;
318*77c1e3ccSAndroid Build Coastguard Worker     }
319*77c1e3ccSAndroid Build Coastguard Worker     const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
320*77c1e3ccSAndroid Build Coastguard Worker     const int bsw = tx_size_wide_unit[sub_txs];
321*77c1e3ccSAndroid Build Coastguard Worker     const int bsh = tx_size_high_unit[sub_txs];
322*77c1e3ccSAndroid Build Coastguard Worker     const int row_end =
323*77c1e3ccSAndroid Build Coastguard Worker         AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
324*77c1e3ccSAndroid Build Coastguard Worker     const int col_end =
325*77c1e3ccSAndroid Build Coastguard Worker         AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
326*77c1e3ccSAndroid Build Coastguard Worker     for (int row = 0; row < row_end; row += bsh) {
327*77c1e3ccSAndroid Build Coastguard Worker       const int offsetr = blk_row + row;
328*77c1e3ccSAndroid Build Coastguard Worker       for (int col = 0; col < col_end; col += bsw) {
329*77c1e3ccSAndroid Build Coastguard Worker         const int offsetc = blk_col + col;
330*77c1e3ccSAndroid Build Coastguard Worker         set_txfm_context(xd, sub_txs, offsetr, offsetc);
331*77c1e3ccSAndroid Build Coastguard Worker       }
332*77c1e3ccSAndroid Build Coastguard Worker     }
333*77c1e3ccSAndroid Build Coastguard Worker   }
334*77c1e3ccSAndroid Build Coastguard Worker }
335*77c1e3ccSAndroid Build Coastguard Worker 
tx_partition_set_contexts(const AV1_COMMON * const cm,MACROBLOCKD * xd,BLOCK_SIZE plane_bsize)336*77c1e3ccSAndroid Build Coastguard Worker static void tx_partition_set_contexts(const AV1_COMMON *const cm,
337*77c1e3ccSAndroid Build Coastguard Worker                                       MACROBLOCKD *xd, BLOCK_SIZE plane_bsize) {
338*77c1e3ccSAndroid Build Coastguard Worker   const int mi_width = mi_size_wide[plane_bsize];
339*77c1e3ccSAndroid Build Coastguard Worker   const int mi_height = mi_size_high[plane_bsize];
340*77c1e3ccSAndroid Build Coastguard Worker   const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, 0);
341*77c1e3ccSAndroid Build Coastguard Worker   const int bh = tx_size_high_unit[max_tx_size];
342*77c1e3ccSAndroid Build Coastguard Worker   const int bw = tx_size_wide_unit[max_tx_size];
343*77c1e3ccSAndroid Build Coastguard Worker 
344*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context =
345*77c1e3ccSAndroid Build Coastguard Worker       cm->above_contexts.txfm[xd->tile.tile_row] + xd->mi_col;
346*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
347*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (xd->mi_row & MAX_MIB_MASK);
348*77c1e3ccSAndroid Build Coastguard Worker 
349*77c1e3ccSAndroid Build Coastguard Worker   for (int idy = 0; idy < mi_height; idy += bh) {
350*77c1e3ccSAndroid Build Coastguard Worker     for (int idx = 0; idx < mi_width; idx += bw) {
351*77c1e3ccSAndroid Build Coastguard Worker       set_txfm_context(xd, max_tx_size, idy, idx);
352*77c1e3ccSAndroid Build Coastguard Worker     }
353*77c1e3ccSAndroid Build Coastguard Worker   }
354*77c1e3ccSAndroid Build Coastguard Worker }
355*77c1e3ccSAndroid Build Coastguard Worker 
update_zeromv_cnt(const AV1_COMP * const cpi,const MB_MODE_INFO * const mi,int mi_row,int mi_col,BLOCK_SIZE bsize)356*77c1e3ccSAndroid Build Coastguard Worker static void update_zeromv_cnt(const AV1_COMP *const cpi,
357*77c1e3ccSAndroid Build Coastguard Worker                               const MB_MODE_INFO *const mi, int mi_row,
358*77c1e3ccSAndroid Build Coastguard Worker                               int mi_col, BLOCK_SIZE bsize) {
359*77c1e3ccSAndroid Build Coastguard Worker   if (mi->ref_frame[0] != LAST_FRAME || !is_inter_block(mi) ||
360*77c1e3ccSAndroid Build Coastguard Worker       mi->segment_id > CR_SEGMENT_ID_BOOST2) {
361*77c1e3ccSAndroid Build Coastguard Worker     return;
362*77c1e3ccSAndroid Build Coastguard Worker   }
363*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
364*77c1e3ccSAndroid Build Coastguard Worker   const MV mv = mi->mv[0].as_mv;
365*77c1e3ccSAndroid Build Coastguard Worker   const int bw = mi_size_wide[bsize] >> 1;
366*77c1e3ccSAndroid Build Coastguard Worker   const int bh = mi_size_high[bsize] >> 1;
367*77c1e3ccSAndroid Build Coastguard Worker   const int xmis = AOMMIN((cm->mi_params.mi_cols - mi_col) >> 1, bw);
368*77c1e3ccSAndroid Build Coastguard Worker   const int ymis = AOMMIN((cm->mi_params.mi_rows - mi_row) >> 1, bh);
369*77c1e3ccSAndroid Build Coastguard Worker   const int block_index =
370*77c1e3ccSAndroid Build Coastguard Worker       (mi_row >> 1) * (cm->mi_params.mi_cols >> 1) + (mi_col >> 1);
371*77c1e3ccSAndroid Build Coastguard Worker   for (int y = 0; y < ymis; y++) {
372*77c1e3ccSAndroid Build Coastguard Worker     for (int x = 0; x < xmis; x++) {
373*77c1e3ccSAndroid Build Coastguard Worker       // consec_zero_mv is in the scale of 8x8 blocks
374*77c1e3ccSAndroid Build Coastguard Worker       const int map_offset = block_index + y * (cm->mi_params.mi_cols >> 1) + x;
375*77c1e3ccSAndroid Build Coastguard Worker       if (abs(mv.row) < 10 && abs(mv.col) < 10) {
376*77c1e3ccSAndroid Build Coastguard Worker         if (cpi->consec_zero_mv[map_offset] < 255)
377*77c1e3ccSAndroid Build Coastguard Worker           cpi->consec_zero_mv[map_offset]++;
378*77c1e3ccSAndroid Build Coastguard Worker       } else {
379*77c1e3ccSAndroid Build Coastguard Worker         cpi->consec_zero_mv[map_offset] = 0;
380*77c1e3ccSAndroid Build Coastguard Worker       }
381*77c1e3ccSAndroid Build Coastguard Worker     }
382*77c1e3ccSAndroid Build Coastguard Worker   }
383*77c1e3ccSAndroid Build Coastguard Worker }
384*77c1e3ccSAndroid Build Coastguard Worker 
encode_superblock(const AV1_COMP * const cpi,TileDataEnc * tile_data,ThreadData * td,TokenExtra ** t,RUN_TYPE dry_run,BLOCK_SIZE bsize,int * rate)385*77c1e3ccSAndroid Build Coastguard Worker static void encode_superblock(const AV1_COMP *const cpi, TileDataEnc *tile_data,
386*77c1e3ccSAndroid Build Coastguard Worker                               ThreadData *td, TokenExtra **t, RUN_TYPE dry_run,
387*77c1e3ccSAndroid Build Coastguard Worker                               BLOCK_SIZE bsize, int *rate) {
388*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
389*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
390*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
391*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
392*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO **mi_4x4 = xd->mi;
393*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *mbmi = mi_4x4[0];
394*77c1e3ccSAndroid Build Coastguard Worker   const int seg_skip =
395*77c1e3ccSAndroid Build Coastguard Worker       segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP);
396*77c1e3ccSAndroid Build Coastguard Worker   const int mis = cm->mi_params.mi_stride;
397*77c1e3ccSAndroid Build Coastguard Worker   const int mi_width = mi_size_wide[bsize];
398*77c1e3ccSAndroid Build Coastguard Worker   const int mi_height = mi_size_high[bsize];
399*77c1e3ccSAndroid Build Coastguard Worker   const int is_inter = is_inter_block(mbmi);
400*77c1e3ccSAndroid Build Coastguard Worker 
401*77c1e3ccSAndroid Build Coastguard Worker   // Initialize tx_mode and tx_size_search_method
402*77c1e3ccSAndroid Build Coastguard Worker   TxfmSearchParams *txfm_params = &x->txfm_search_params;
403*77c1e3ccSAndroid Build Coastguard Worker   set_tx_size_search_method(
404*77c1e3ccSAndroid Build Coastguard Worker       cm, &cpi->winner_mode_params, txfm_params,
405*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.winner_mode_sf.enable_winner_mode_for_tx_size_srch, 1);
406*77c1e3ccSAndroid Build Coastguard Worker 
407*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = xd->mi_row;
408*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col = xd->mi_col;
409*77c1e3ccSAndroid Build Coastguard Worker   if (!is_inter) {
410*77c1e3ccSAndroid Build Coastguard Worker     xd->cfl.store_y = store_cfl_required(cm, xd);
411*77c1e3ccSAndroid Build Coastguard Worker     mbmi->skip_txfm = 1;
412*77c1e3ccSAndroid Build Coastguard Worker     for (int plane = 0; plane < num_planes; ++plane) {
413*77c1e3ccSAndroid Build Coastguard Worker       av1_encode_intra_block_plane(cpi, x, bsize, plane, dry_run,
414*77c1e3ccSAndroid Build Coastguard Worker                                    cpi->optimize_seg_arr[mbmi->segment_id]);
415*77c1e3ccSAndroid Build Coastguard Worker     }
416*77c1e3ccSAndroid Build Coastguard Worker 
417*77c1e3ccSAndroid Build Coastguard Worker     // If there is at least one lossless segment, force the skip for intra
418*77c1e3ccSAndroid Build Coastguard Worker     // block to be 0, in order to avoid the segment_id to be changed by in
419*77c1e3ccSAndroid Build Coastguard Worker     // write_segment_id().
420*77c1e3ccSAndroid Build Coastguard Worker     if (!cpi->common.seg.segid_preskip && cpi->common.seg.update_map &&
421*77c1e3ccSAndroid Build Coastguard Worker         cpi->enc_seg.has_lossless_segment)
422*77c1e3ccSAndroid Build Coastguard Worker       mbmi->skip_txfm = 0;
423*77c1e3ccSAndroid Build Coastguard Worker 
424*77c1e3ccSAndroid Build Coastguard Worker     xd->cfl.store_y = 0;
425*77c1e3ccSAndroid Build Coastguard Worker     if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize)) {
426*77c1e3ccSAndroid Build Coastguard Worker       for (int plane = 0; plane < AOMMIN(2, num_planes); ++plane) {
427*77c1e3ccSAndroid Build Coastguard Worker         if (mbmi->palette_mode_info.palette_size[plane] > 0) {
428*77c1e3ccSAndroid Build Coastguard Worker           if (!dry_run) {
429*77c1e3ccSAndroid Build Coastguard Worker             av1_tokenize_color_map(x, plane, t, bsize, mbmi->tx_size,
430*77c1e3ccSAndroid Build Coastguard Worker                                    PALETTE_MAP, tile_data->allow_update_cdf,
431*77c1e3ccSAndroid Build Coastguard Worker                                    td->counts);
432*77c1e3ccSAndroid Build Coastguard Worker           } else if (dry_run == DRY_RUN_COSTCOEFFS) {
433*77c1e3ccSAndroid Build Coastguard Worker             *rate +=
434*77c1e3ccSAndroid Build Coastguard Worker                 av1_cost_color_map(x, plane, bsize, mbmi->tx_size, PALETTE_MAP);
435*77c1e3ccSAndroid Build Coastguard Worker           }
436*77c1e3ccSAndroid Build Coastguard Worker         }
437*77c1e3ccSAndroid Build Coastguard Worker       }
438*77c1e3ccSAndroid Build Coastguard Worker     }
439*77c1e3ccSAndroid Build Coastguard Worker 
440*77c1e3ccSAndroid Build Coastguard Worker     av1_update_intra_mb_txb_context(cpi, td, dry_run, bsize,
441*77c1e3ccSAndroid Build Coastguard Worker                                     tile_data->allow_update_cdf);
442*77c1e3ccSAndroid Build Coastguard Worker   } else {
443*77c1e3ccSAndroid Build Coastguard Worker     int ref;
444*77c1e3ccSAndroid Build Coastguard Worker     const int is_compound = has_second_ref(mbmi);
445*77c1e3ccSAndroid Build Coastguard Worker 
446*77c1e3ccSAndroid Build Coastguard Worker     set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
447*77c1e3ccSAndroid Build Coastguard Worker     for (ref = 0; ref < 1 + is_compound; ++ref) {
448*77c1e3ccSAndroid Build Coastguard Worker       const YV12_BUFFER_CONFIG *cfg =
449*77c1e3ccSAndroid Build Coastguard Worker           get_ref_frame_yv12_buf(cm, mbmi->ref_frame[ref]);
450*77c1e3ccSAndroid Build Coastguard Worker       assert(IMPLIES(!is_intrabc_block(mbmi), cfg));
451*77c1e3ccSAndroid Build Coastguard Worker       av1_setup_pre_planes(xd, ref, cfg, mi_row, mi_col,
452*77c1e3ccSAndroid Build Coastguard Worker                            xd->block_ref_scale_factors[ref], num_planes);
453*77c1e3ccSAndroid Build Coastguard Worker     }
454*77c1e3ccSAndroid Build Coastguard Worker     // Predicted sample of inter mode (for Luma plane) cannot be reused if
455*77c1e3ccSAndroid Build Coastguard Worker     // nonrd_check_partition_split speed feature is enabled, Since in such cases
456*77c1e3ccSAndroid Build Coastguard Worker     // the buffer may not contain the predicted sample of best mode.
457*77c1e3ccSAndroid Build Coastguard Worker     const int start_plane =
458*77c1e3ccSAndroid Build Coastguard Worker         (x->reuse_inter_pred && (!cpi->sf.rt_sf.nonrd_check_partition_split) &&
459*77c1e3ccSAndroid Build Coastguard Worker          cm->seq_params->bit_depth == AOM_BITS_8)
460*77c1e3ccSAndroid Build Coastguard Worker             ? 1
461*77c1e3ccSAndroid Build Coastguard Worker             : 0;
462*77c1e3ccSAndroid Build Coastguard Worker     av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
463*77c1e3ccSAndroid Build Coastguard Worker                                   start_plane, av1_num_planes(cm) - 1);
464*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->motion_mode == OBMC_CAUSAL) {
465*77c1e3ccSAndroid Build Coastguard Worker       assert(cpi->oxcf.motion_mode_cfg.enable_obmc);
466*77c1e3ccSAndroid Build Coastguard Worker       av1_build_obmc_inter_predictors_sb(cm, xd);
467*77c1e3ccSAndroid Build Coastguard Worker     }
468*77c1e3ccSAndroid Build Coastguard Worker 
469*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MISMATCH_DEBUG
470*77c1e3ccSAndroid Build Coastguard Worker     if (dry_run == OUTPUT_ENABLED) {
471*77c1e3ccSAndroid Build Coastguard Worker       for (int plane = 0; plane < num_planes; ++plane) {
472*77c1e3ccSAndroid Build Coastguard Worker         const struct macroblockd_plane *pd = &xd->plane[plane];
473*77c1e3ccSAndroid Build Coastguard Worker         int pixel_c, pixel_r;
474*77c1e3ccSAndroid Build Coastguard Worker         mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, 0, 0,
475*77c1e3ccSAndroid Build Coastguard Worker                         pd->subsampling_x, pd->subsampling_y);
476*77c1e3ccSAndroid Build Coastguard Worker         if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
477*77c1e3ccSAndroid Build Coastguard Worker                                  pd->subsampling_y))
478*77c1e3ccSAndroid Build Coastguard Worker           continue;
479*77c1e3ccSAndroid Build Coastguard Worker         mismatch_record_block_pre(pd->dst.buf, pd->dst.stride,
480*77c1e3ccSAndroid Build Coastguard Worker                                   cm->current_frame.order_hint, plane, pixel_c,
481*77c1e3ccSAndroid Build Coastguard Worker                                   pixel_r, pd->width, pd->height,
482*77c1e3ccSAndroid Build Coastguard Worker                                   xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
483*77c1e3ccSAndroid Build Coastguard Worker       }
484*77c1e3ccSAndroid Build Coastguard Worker     }
485*77c1e3ccSAndroid Build Coastguard Worker #else
486*77c1e3ccSAndroid Build Coastguard Worker     (void)num_planes;
487*77c1e3ccSAndroid Build Coastguard Worker #endif
488*77c1e3ccSAndroid Build Coastguard Worker 
489*77c1e3ccSAndroid Build Coastguard Worker     av1_encode_sb(cpi, x, bsize, dry_run);
490*77c1e3ccSAndroid Build Coastguard Worker     av1_tokenize_sb_vartx(cpi, td, dry_run, bsize, rate,
491*77c1e3ccSAndroid Build Coastguard Worker                           tile_data->allow_update_cdf);
492*77c1e3ccSAndroid Build Coastguard Worker   }
493*77c1e3ccSAndroid Build Coastguard Worker 
494*77c1e3ccSAndroid Build Coastguard Worker   if (!dry_run) {
495*77c1e3ccSAndroid Build Coastguard Worker     if (av1_allow_intrabc(cm) && is_intrabc_block(mbmi)) td->intrabc_used = 1;
496*77c1e3ccSAndroid Build Coastguard Worker     if (txfm_params->tx_mode_search_type == TX_MODE_SELECT &&
497*77c1e3ccSAndroid Build Coastguard Worker         !xd->lossless[mbmi->segment_id] && mbmi->bsize > BLOCK_4X4 &&
498*77c1e3ccSAndroid Build Coastguard Worker         !(is_inter && (mbmi->skip_txfm || seg_skip))) {
499*77c1e3ccSAndroid Build Coastguard Worker       if (is_inter) {
500*77c1e3ccSAndroid Build Coastguard Worker         tx_partition_count_update(cm, x, bsize, td->counts,
501*77c1e3ccSAndroid Build Coastguard Worker                                   tile_data->allow_update_cdf);
502*77c1e3ccSAndroid Build Coastguard Worker       } else {
503*77c1e3ccSAndroid Build Coastguard Worker         if (mbmi->tx_size != max_txsize_rect_lookup[bsize])
504*77c1e3ccSAndroid Build Coastguard Worker           ++x->txfm_search_info.txb_split_count;
505*77c1e3ccSAndroid Build Coastguard Worker         if (block_signals_txsize(bsize)) {
506*77c1e3ccSAndroid Build Coastguard Worker           const int tx_size_ctx = get_tx_size_context(xd);
507*77c1e3ccSAndroid Build Coastguard Worker           const int32_t tx_size_cat = bsize_to_tx_size_cat(bsize);
508*77c1e3ccSAndroid Build Coastguard Worker           const int depth = tx_size_to_depth(mbmi->tx_size, bsize);
509*77c1e3ccSAndroid Build Coastguard Worker           const int max_depths = bsize_to_max_depth(bsize);
510*77c1e3ccSAndroid Build Coastguard Worker 
511*77c1e3ccSAndroid Build Coastguard Worker           if (tile_data->allow_update_cdf)
512*77c1e3ccSAndroid Build Coastguard Worker             update_cdf(xd->tile_ctx->tx_size_cdf[tx_size_cat][tx_size_ctx],
513*77c1e3ccSAndroid Build Coastguard Worker                        depth, max_depths + 1);
514*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
515*77c1e3ccSAndroid Build Coastguard Worker           ++td->counts->intra_tx_size[tx_size_cat][tx_size_ctx][depth];
516*77c1e3ccSAndroid Build Coastguard Worker #endif
517*77c1e3ccSAndroid Build Coastguard Worker         }
518*77c1e3ccSAndroid Build Coastguard Worker       }
519*77c1e3ccSAndroid Build Coastguard Worker       assert(IMPLIES(is_rect_tx(mbmi->tx_size), is_rect_tx_allowed(xd, mbmi)));
520*77c1e3ccSAndroid Build Coastguard Worker     } else {
521*77c1e3ccSAndroid Build Coastguard Worker       int i, j;
522*77c1e3ccSAndroid Build Coastguard Worker       TX_SIZE intra_tx_size;
523*77c1e3ccSAndroid Build Coastguard Worker       // The new intra coding scheme requires no change of transform size
524*77c1e3ccSAndroid Build Coastguard Worker       if (is_inter) {
525*77c1e3ccSAndroid Build Coastguard Worker         if (xd->lossless[mbmi->segment_id]) {
526*77c1e3ccSAndroid Build Coastguard Worker           intra_tx_size = TX_4X4;
527*77c1e3ccSAndroid Build Coastguard Worker         } else {
528*77c1e3ccSAndroid Build Coastguard Worker           intra_tx_size =
529*77c1e3ccSAndroid Build Coastguard Worker               tx_size_from_tx_mode(bsize, txfm_params->tx_mode_search_type);
530*77c1e3ccSAndroid Build Coastguard Worker         }
531*77c1e3ccSAndroid Build Coastguard Worker       } else {
532*77c1e3ccSAndroid Build Coastguard Worker         intra_tx_size = mbmi->tx_size;
533*77c1e3ccSAndroid Build Coastguard Worker       }
534*77c1e3ccSAndroid Build Coastguard Worker 
535*77c1e3ccSAndroid Build Coastguard Worker       const int cols = AOMMIN(cm->mi_params.mi_cols - mi_col, mi_width);
536*77c1e3ccSAndroid Build Coastguard Worker       const int rows = AOMMIN(cm->mi_params.mi_rows - mi_row, mi_height);
537*77c1e3ccSAndroid Build Coastguard Worker       for (j = 0; j < rows; j++) {
538*77c1e3ccSAndroid Build Coastguard Worker         for (i = 0; i < cols; i++) mi_4x4[mis * j + i]->tx_size = intra_tx_size;
539*77c1e3ccSAndroid Build Coastguard Worker       }
540*77c1e3ccSAndroid Build Coastguard Worker 
541*77c1e3ccSAndroid Build Coastguard Worker       if (intra_tx_size != max_txsize_rect_lookup[bsize])
542*77c1e3ccSAndroid Build Coastguard Worker         ++x->txfm_search_info.txb_split_count;
543*77c1e3ccSAndroid Build Coastguard Worker     }
544*77c1e3ccSAndroid Build Coastguard Worker   }
545*77c1e3ccSAndroid Build Coastguard Worker 
546*77c1e3ccSAndroid Build Coastguard Worker   if (txfm_params->tx_mode_search_type == TX_MODE_SELECT &&
547*77c1e3ccSAndroid Build Coastguard Worker       block_signals_txsize(mbmi->bsize) && is_inter &&
548*77c1e3ccSAndroid Build Coastguard Worker       !(mbmi->skip_txfm || seg_skip) && !xd->lossless[mbmi->segment_id]) {
549*77c1e3ccSAndroid Build Coastguard Worker     if (dry_run) tx_partition_set_contexts(cm, xd, bsize);
550*77c1e3ccSAndroid Build Coastguard Worker   } else {
551*77c1e3ccSAndroid Build Coastguard Worker     TX_SIZE tx_size = mbmi->tx_size;
552*77c1e3ccSAndroid Build Coastguard Worker     // The new intra coding scheme requires no change of transform size
553*77c1e3ccSAndroid Build Coastguard Worker     if (is_inter) {
554*77c1e3ccSAndroid Build Coastguard Worker       if (xd->lossless[mbmi->segment_id]) {
555*77c1e3ccSAndroid Build Coastguard Worker         tx_size = TX_4X4;
556*77c1e3ccSAndroid Build Coastguard Worker       } else {
557*77c1e3ccSAndroid Build Coastguard Worker         tx_size = tx_size_from_tx_mode(bsize, txfm_params->tx_mode_search_type);
558*77c1e3ccSAndroid Build Coastguard Worker       }
559*77c1e3ccSAndroid Build Coastguard Worker     } else {
560*77c1e3ccSAndroid Build Coastguard Worker       tx_size = (bsize > BLOCK_4X4) ? tx_size : TX_4X4;
561*77c1e3ccSAndroid Build Coastguard Worker     }
562*77c1e3ccSAndroid Build Coastguard Worker     mbmi->tx_size = tx_size;
563*77c1e3ccSAndroid Build Coastguard Worker     set_txfm_ctxs(tx_size, xd->width, xd->height,
564*77c1e3ccSAndroid Build Coastguard Worker                   (mbmi->skip_txfm || seg_skip) && is_inter_block(mbmi), xd);
565*77c1e3ccSAndroid Build Coastguard Worker   }
566*77c1e3ccSAndroid Build Coastguard Worker 
567*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
568*77c1e3ccSAndroid Build Coastguard Worker   if (is_inter_block(mbmi) && !xd->is_chroma_ref && is_cfl_allowed(xd)) {
569*77c1e3ccSAndroid Build Coastguard Worker     cfl_store_block(xd, mbmi->bsize, mbmi->tx_size);
570*77c1e3ccSAndroid Build Coastguard Worker   }
571*77c1e3ccSAndroid Build Coastguard Worker #endif
572*77c1e3ccSAndroid Build Coastguard Worker   if (!dry_run) {
573*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->oxcf.pass == AOM_RC_ONE_PASS && cpi->svc.temporal_layer_id == 0 &&
574*77c1e3ccSAndroid Build Coastguard Worker         cpi->sf.rt_sf.use_temporal_noise_estimate &&
575*77c1e3ccSAndroid Build Coastguard Worker         (!cpi->ppi->use_svc ||
576*77c1e3ccSAndroid Build Coastguard Worker          (cpi->ppi->use_svc &&
577*77c1e3ccSAndroid Build Coastguard Worker           !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame &&
578*77c1e3ccSAndroid Build Coastguard Worker           cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)))
579*77c1e3ccSAndroid Build Coastguard Worker       update_zeromv_cnt(cpi, mbmi, mi_row, mi_col, bsize);
580*77c1e3ccSAndroid Build Coastguard Worker   }
581*77c1e3ccSAndroid Build Coastguard Worker }
582*77c1e3ccSAndroid Build Coastguard Worker 
setup_block_rdmult(const AV1_COMP * const cpi,MACROBLOCK * const x,int mi_row,int mi_col,BLOCK_SIZE bsize,AQ_MODE aq_mode,MB_MODE_INFO * mbmi)583*77c1e3ccSAndroid Build Coastguard Worker static void setup_block_rdmult(const AV1_COMP *const cpi, MACROBLOCK *const x,
584*77c1e3ccSAndroid Build Coastguard Worker                                int mi_row, int mi_col, BLOCK_SIZE bsize,
585*77c1e3ccSAndroid Build Coastguard Worker                                AQ_MODE aq_mode, MB_MODE_INFO *mbmi) {
586*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult = cpi->rd.RDMULT;
587*77c1e3ccSAndroid Build Coastguard Worker 
588*77c1e3ccSAndroid Build Coastguard Worker   if (aq_mode != NO_AQ) {
589*77c1e3ccSAndroid Build Coastguard Worker     assert(mbmi != NULL);
590*77c1e3ccSAndroid Build Coastguard Worker     if (aq_mode == VARIANCE_AQ) {
591*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->vaq_refresh) {
592*77c1e3ccSAndroid Build Coastguard Worker         const int energy = bsize <= BLOCK_16X16
593*77c1e3ccSAndroid Build Coastguard Worker                                ? x->mb_energy
594*77c1e3ccSAndroid Build Coastguard Worker                                : av1_log_block_var(cpi, x, bsize);
595*77c1e3ccSAndroid Build Coastguard Worker         mbmi->segment_id = energy;
596*77c1e3ccSAndroid Build Coastguard Worker       }
597*77c1e3ccSAndroid Build Coastguard Worker       x->rdmult = set_rdmult(cpi, x, mbmi->segment_id);
598*77c1e3ccSAndroid Build Coastguard Worker     } else if (aq_mode == COMPLEXITY_AQ) {
599*77c1e3ccSAndroid Build Coastguard Worker       x->rdmult = set_rdmult(cpi, x, mbmi->segment_id);
600*77c1e3ccSAndroid Build Coastguard Worker     } else if (aq_mode == CYCLIC_REFRESH_AQ) {
601*77c1e3ccSAndroid Build Coastguard Worker       // If segment is boosted, use rdmult for that segment.
602*77c1e3ccSAndroid Build Coastguard Worker       if (cyclic_refresh_segment_id_boosted(mbmi->segment_id))
603*77c1e3ccSAndroid Build Coastguard Worker         x->rdmult = av1_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
604*77c1e3ccSAndroid Build Coastguard Worker     }
605*77c1e3ccSAndroid Build Coastguard Worker   }
606*77c1e3ccSAndroid Build Coastguard Worker 
607*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
608*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->common.delta_q_info.delta_q_present_flag &&
609*77c1e3ccSAndroid Build Coastguard Worker       !cpi->sf.rt_sf.use_nonrd_pick_mode) {
610*77c1e3ccSAndroid Build Coastguard Worker     x->rdmult = av1_get_cb_rdmult(cpi, x, bsize, mi_row, mi_col);
611*77c1e3ccSAndroid Build Coastguard Worker   }
612*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
613*77c1e3ccSAndroid Build Coastguard Worker 
614*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_SSIM) {
615*77c1e3ccSAndroid Build Coastguard Worker     av1_set_ssim_rdmult(cpi, &x->errorperbit, bsize, mi_row, mi_col,
616*77c1e3ccSAndroid Build Coastguard Worker                         &x->rdmult);
617*77c1e3ccSAndroid Build Coastguard Worker   }
618*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_SALIENCY_MAP
619*77c1e3ccSAndroid Build Coastguard Worker   else if (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_SALIENCY_MAP) {
620*77c1e3ccSAndroid Build Coastguard Worker     av1_set_saliency_map_vmaf_rdmult(cpi, &x->errorperbit,
621*77c1e3ccSAndroid Build Coastguard Worker                                      cpi->common.seq_params->sb_size, mi_row,
622*77c1e3ccSAndroid Build Coastguard Worker                                      mi_col, &x->rdmult);
623*77c1e3ccSAndroid Build Coastguard Worker   }
624*77c1e3ccSAndroid Build Coastguard Worker #endif
625*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
626*77c1e3ccSAndroid Build Coastguard Worker   else if (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
627*77c1e3ccSAndroid Build Coastguard Worker            cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN ||
628*77c1e3ccSAndroid Build Coastguard Worker            cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
629*77c1e3ccSAndroid Build Coastguard Worker     av1_set_vmaf_rdmult(cpi, x, bsize, mi_row, mi_col, &x->rdmult);
630*77c1e3ccSAndroid Build Coastguard Worker   }
631*77c1e3ccSAndroid Build Coastguard Worker #endif
632*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_BUTTERAUGLI
633*77c1e3ccSAndroid Build Coastguard Worker   else if (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_BUTTERAUGLI) {
634*77c1e3ccSAndroid Build Coastguard Worker     av1_set_butteraugli_rdmult(cpi, x, bsize, mi_row, mi_col, &x->rdmult);
635*77c1e3ccSAndroid Build Coastguard Worker   }
636*77c1e3ccSAndroid Build Coastguard Worker #endif
637*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.mode == ALLINTRA) {
638*77c1e3ccSAndroid Build Coastguard Worker     x->rdmult = (int)(((int64_t)x->rdmult * x->intra_sb_rdmult_modifier) >> 7);
639*77c1e3ccSAndroid Build Coastguard Worker   }
640*77c1e3ccSAndroid Build Coastguard Worker 
641*77c1e3ccSAndroid Build Coastguard Worker   // Check to make sure that the adjustments above have not caused the
642*77c1e3ccSAndroid Build Coastguard Worker   // rd multiplier to be truncated to 0.
643*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult = (x->rdmult > 0) ? x->rdmult : 1;
644*77c1e3ccSAndroid Build Coastguard Worker }
645*77c1e3ccSAndroid Build Coastguard Worker 
av1_set_offsets_without_segment_id(const AV1_COMP * const cpi,const TileInfo * const tile,MACROBLOCK * const x,int mi_row,int mi_col,BLOCK_SIZE bsize)646*77c1e3ccSAndroid Build Coastguard Worker void av1_set_offsets_without_segment_id(const AV1_COMP *const cpi,
647*77c1e3ccSAndroid Build Coastguard Worker                                         const TileInfo *const tile,
648*77c1e3ccSAndroid Build Coastguard Worker                                         MACROBLOCK *const x, int mi_row,
649*77c1e3ccSAndroid Build Coastguard Worker                                         int mi_col, BLOCK_SIZE bsize) {
650*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
651*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
652*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
653*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize < BLOCK_SIZES_ALL);
654*77c1e3ccSAndroid Build Coastguard Worker   const int mi_width = mi_size_wide[bsize];
655*77c1e3ccSAndroid Build Coastguard Worker   const int mi_height = mi_size_high[bsize];
656*77c1e3ccSAndroid Build Coastguard Worker 
657*77c1e3ccSAndroid Build Coastguard Worker   set_mode_info_offsets(&cpi->common.mi_params, &cpi->mbmi_ext_info, x, xd,
658*77c1e3ccSAndroid Build Coastguard Worker                         mi_row, mi_col);
659*77c1e3ccSAndroid Build Coastguard Worker 
660*77c1e3ccSAndroid Build Coastguard Worker   set_entropy_context(xd, mi_row, mi_col, num_planes);
661*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context = cm->above_contexts.txfm[tile->tile_row] + mi_col;
662*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
663*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
664*77c1e3ccSAndroid Build Coastguard Worker 
665*77c1e3ccSAndroid Build Coastguard Worker   // Set up destination pointers.
666*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
667*77c1e3ccSAndroid Build Coastguard Worker                        num_planes);
668*77c1e3ccSAndroid Build Coastguard Worker 
669*77c1e3ccSAndroid Build Coastguard Worker   // Set up limit values for MV components.
670*77c1e3ccSAndroid Build Coastguard Worker   // Mv beyond the range do not produce new/different prediction block.
671*77c1e3ccSAndroid Build Coastguard Worker   av1_set_mv_limits(&cm->mi_params, &x->mv_limits, mi_row, mi_col, mi_height,
672*77c1e3ccSAndroid Build Coastguard Worker                     mi_width, cpi->oxcf.border_in_pixels);
673*77c1e3ccSAndroid Build Coastguard Worker 
674*77c1e3ccSAndroid Build Coastguard Worker   set_plane_n4(xd, mi_width, mi_height, num_planes);
675*77c1e3ccSAndroid Build Coastguard Worker 
676*77c1e3ccSAndroid Build Coastguard Worker   // Set up distance of MB to edge of frame in 1/8th pel units.
677*77c1e3ccSAndroid Build Coastguard Worker   assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
678*77c1e3ccSAndroid Build Coastguard Worker   set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
679*77c1e3ccSAndroid Build Coastguard Worker                  cm->mi_params.mi_rows, cm->mi_params.mi_cols);
680*77c1e3ccSAndroid Build Coastguard Worker 
681*77c1e3ccSAndroid Build Coastguard Worker   // Set up source buffers.
682*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_src_planes(x, cpi->source, mi_row, mi_col, num_planes, bsize);
683*77c1e3ccSAndroid Build Coastguard Worker 
684*77c1e3ccSAndroid Build Coastguard Worker   // required by av1_append_sub8x8_mvs_for_idx() and av1_find_best_ref_mvs()
685*77c1e3ccSAndroid Build Coastguard Worker   xd->tile = *tile;
686*77c1e3ccSAndroid Build Coastguard Worker }
687*77c1e3ccSAndroid Build Coastguard Worker 
av1_set_offsets(const AV1_COMP * const cpi,const TileInfo * const tile,MACROBLOCK * const x,int mi_row,int mi_col,BLOCK_SIZE bsize)688*77c1e3ccSAndroid Build Coastguard Worker void av1_set_offsets(const AV1_COMP *const cpi, const TileInfo *const tile,
689*77c1e3ccSAndroid Build Coastguard Worker                      MACROBLOCK *const x, int mi_row, int mi_col,
690*77c1e3ccSAndroid Build Coastguard Worker                      BLOCK_SIZE bsize) {
691*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
692*77c1e3ccSAndroid Build Coastguard Worker   const struct segmentation *const seg = &cm->seg;
693*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
694*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *mbmi;
695*77c1e3ccSAndroid Build Coastguard Worker 
696*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets_without_segment_id(cpi, tile, x, mi_row, mi_col, bsize);
697*77c1e3ccSAndroid Build Coastguard Worker 
698*77c1e3ccSAndroid Build Coastguard Worker   // Setup segment ID.
699*77c1e3ccSAndroid Build Coastguard Worker   mbmi = xd->mi[0];
700*77c1e3ccSAndroid Build Coastguard Worker   mbmi->segment_id = 0;
701*77c1e3ccSAndroid Build Coastguard Worker   if (seg->enabled) {
702*77c1e3ccSAndroid Build Coastguard Worker     if (seg->enabled && !cpi->vaq_refresh) {
703*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t *const map =
704*77c1e3ccSAndroid Build Coastguard Worker           seg->update_map ? cpi->enc_seg.map : cm->last_frame_seg_map;
705*77c1e3ccSAndroid Build Coastguard Worker       mbmi->segment_id =
706*77c1e3ccSAndroid Build Coastguard Worker           map ? get_segment_id(&cm->mi_params, map, bsize, mi_row, mi_col) : 0;
707*77c1e3ccSAndroid Build Coastguard Worker     }
708*77c1e3ccSAndroid Build Coastguard Worker     av1_init_plane_quantizers(cpi, x, mbmi->segment_id, 0);
709*77c1e3ccSAndroid Build Coastguard Worker   }
710*77c1e3ccSAndroid Build Coastguard Worker #ifndef NDEBUG
711*77c1e3ccSAndroid Build Coastguard Worker   x->last_set_offsets_loc.mi_row = mi_row;
712*77c1e3ccSAndroid Build Coastguard Worker   x->last_set_offsets_loc.mi_col = mi_col;
713*77c1e3ccSAndroid Build Coastguard Worker   x->last_set_offsets_loc.bsize = bsize;
714*77c1e3ccSAndroid Build Coastguard Worker #endif  // NDEBUG
715*77c1e3ccSAndroid Build Coastguard Worker }
716*77c1e3ccSAndroid Build Coastguard Worker 
717*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Hybrid intra mode search.
718*77c1e3ccSAndroid Build Coastguard Worker  *
719*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup intra_mode_search
720*77c1e3ccSAndroid Build Coastguard Worker  * \callgraph
721*77c1e3ccSAndroid Build Coastguard Worker  * \callergraph
722*77c1e3ccSAndroid Build Coastguard Worker  * This is top level function for mode search for intra frames in non-RD
723*77c1e3ccSAndroid Build Coastguard Worker  * optimized case. Depending on speed feature and block size it calls
724*77c1e3ccSAndroid Build Coastguard Worker  * either non-RD or RD optimized intra mode search.
725*77c1e3ccSAndroid Build Coastguard Worker  *
726*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi            Top-level encoder structure
727*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding all the data for
728*77c1e3ccSAndroid Build Coastguard Worker                                 the current macroblock
729*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rd_cost        Struct to keep track of the RD information
730*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    bsize          Current block size
731*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ctx            Structure to hold snapshot of coding context
732*77c1e3ccSAndroid Build Coastguard Worker                                 during the mode picking process
733*77c1e3ccSAndroid Build Coastguard Worker  *
734*77c1e3ccSAndroid Build Coastguard Worker  * \remark Nothing is returned. Instead, the MB_MODE_INFO struct inside x
735*77c1e3ccSAndroid Build Coastguard Worker  * is modified to store information about the best mode computed
736*77c1e3ccSAndroid Build Coastguard Worker  * in this function. The rd_cost struct is also updated with the RD stats
737*77c1e3ccSAndroid Build Coastguard Worker  * corresponding to the best mode found.
738*77c1e3ccSAndroid Build Coastguard Worker  */
739*77c1e3ccSAndroid Build Coastguard Worker 
hybrid_intra_mode_search(AV1_COMP * cpi,MACROBLOCK * const x,RD_STATS * rd_cost,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx)740*77c1e3ccSAndroid Build Coastguard Worker static inline void hybrid_intra_mode_search(AV1_COMP *cpi, MACROBLOCK *const x,
741*77c1e3ccSAndroid Build Coastguard Worker                                             RD_STATS *rd_cost, BLOCK_SIZE bsize,
742*77c1e3ccSAndroid Build Coastguard Worker                                             PICK_MODE_CONTEXT *ctx) {
743*77c1e3ccSAndroid Build Coastguard Worker   int use_rdopt = 0;
744*77c1e3ccSAndroid Build Coastguard Worker   const int hybrid_intra_pickmode = cpi->sf.rt_sf.hybrid_intra_pickmode;
745*77c1e3ccSAndroid Build Coastguard Worker   // Use rd pick for intra mode search based on block size and variance.
746*77c1e3ccSAndroid Build Coastguard Worker   if (hybrid_intra_pickmode && bsize < BLOCK_16X16) {
747*77c1e3ccSAndroid Build Coastguard Worker     unsigned int var_thresh[3] = { 0, 101, 201 };
748*77c1e3ccSAndroid Build Coastguard Worker     assert(hybrid_intra_pickmode <= 3);
749*77c1e3ccSAndroid Build Coastguard Worker     if (x->source_variance >= var_thresh[hybrid_intra_pickmode - 1])
750*77c1e3ccSAndroid Build Coastguard Worker       use_rdopt = 1;
751*77c1e3ccSAndroid Build Coastguard Worker   }
752*77c1e3ccSAndroid Build Coastguard Worker 
753*77c1e3ccSAndroid Build Coastguard Worker   if (use_rdopt)
754*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, INT64_MAX);
755*77c1e3ccSAndroid Build Coastguard Worker   else
756*77c1e3ccSAndroid Build Coastguard Worker     av1_nonrd_pick_intra_mode(cpi, x, rd_cost, bsize, ctx);
757*77c1e3ccSAndroid Build Coastguard Worker }
758*77c1e3ccSAndroid Build Coastguard Worker 
759*77c1e3ccSAndroid Build Coastguard Worker // For real time/allintra row-mt enabled multi-threaded encoding with cost
760*77c1e3ccSAndroid Build Coastguard Worker // update frequency set to COST_UPD_TILE/COST_UPD_OFF, tile ctxt is not updated
761*77c1e3ccSAndroid Build Coastguard Worker // at superblock level. Thus, it is not required for the encoding of top-right
762*77c1e3ccSAndroid Build Coastguard Worker // superblock be complete for updating tile ctxt. However, when encoding a block
763*77c1e3ccSAndroid Build Coastguard Worker // whose right edge is also the superblock edge, intra and inter mode evaluation
764*77c1e3ccSAndroid Build Coastguard Worker // (ref mv list population) require the encoding of the top-right superblock to
765*77c1e3ccSAndroid Build Coastguard Worker // be complete. So, here, we delay the waiting of threads until the need for the
766*77c1e3ccSAndroid Build Coastguard Worker // data from the top-right superblock region.
wait_for_top_right_sb(AV1EncRowMultiThreadInfo * enc_row_mt,AV1EncRowMultiThreadSync * row_mt_sync,TileInfo * tile_info,BLOCK_SIZE sb_size,int sb_mi_size_log2,BLOCK_SIZE bsize,int mi_row,int mi_col)767*77c1e3ccSAndroid Build Coastguard Worker static inline void wait_for_top_right_sb(AV1EncRowMultiThreadInfo *enc_row_mt,
768*77c1e3ccSAndroid Build Coastguard Worker                                          AV1EncRowMultiThreadSync *row_mt_sync,
769*77c1e3ccSAndroid Build Coastguard Worker                                          TileInfo *tile_info,
770*77c1e3ccSAndroid Build Coastguard Worker                                          BLOCK_SIZE sb_size,
771*77c1e3ccSAndroid Build Coastguard Worker                                          int sb_mi_size_log2, BLOCK_SIZE bsize,
772*77c1e3ccSAndroid Build Coastguard Worker                                          int mi_row, int mi_col) {
773*77c1e3ccSAndroid Build Coastguard Worker   const int sb_size_in_mi = mi_size_wide[sb_size];
774*77c1e3ccSAndroid Build Coastguard Worker   const int bw_in_mi = mi_size_wide[bsize];
775*77c1e3ccSAndroid Build Coastguard Worker   const int blk_row_in_sb = mi_row & (sb_size_in_mi - 1);
776*77c1e3ccSAndroid Build Coastguard Worker   const int blk_col_in_sb = mi_col & (sb_size_in_mi - 1);
777*77c1e3ccSAndroid Build Coastguard Worker   const int top_right_block_in_sb =
778*77c1e3ccSAndroid Build Coastguard Worker       (blk_row_in_sb == 0) && (blk_col_in_sb + bw_in_mi >= sb_size_in_mi);
779*77c1e3ccSAndroid Build Coastguard Worker 
780*77c1e3ccSAndroid Build Coastguard Worker   // Don't wait if the block is the not the top-right block in the superblock.
781*77c1e3ccSAndroid Build Coastguard Worker   if (!top_right_block_in_sb) return;
782*77c1e3ccSAndroid Build Coastguard Worker 
783*77c1e3ccSAndroid Build Coastguard Worker   // Wait for the top-right superblock to finish encoding.
784*77c1e3ccSAndroid Build Coastguard Worker   const int sb_row_in_tile =
785*77c1e3ccSAndroid Build Coastguard Worker       (mi_row - tile_info->mi_row_start) >> sb_mi_size_log2;
786*77c1e3ccSAndroid Build Coastguard Worker   const int sb_col_in_tile =
787*77c1e3ccSAndroid Build Coastguard Worker       (mi_col - tile_info->mi_col_start) >> sb_mi_size_log2;
788*77c1e3ccSAndroid Build Coastguard Worker 
789*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->sync_read_ptr(row_mt_sync, sb_row_in_tile, sb_col_in_tile);
790*77c1e3ccSAndroid Build Coastguard Worker }
791*77c1e3ccSAndroid Build Coastguard Worker 
792*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Interface for AV1 mode search for an individual coding block
793*77c1e3ccSAndroid Build Coastguard Worker  *
794*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
795*77c1e3ccSAndroid Build Coastguard Worker  * \callgraph
796*77c1e3ccSAndroid Build Coastguard Worker  * \callergraph
797*77c1e3ccSAndroid Build Coastguard Worker  * Searches prediction modes, transform, and coefficient coding modes for an
798*77c1e3ccSAndroid Build Coastguard Worker  * individual coding block. This function is the top-level interface that
799*77c1e3ccSAndroid Build Coastguard Worker  * directs the encoder to the proper mode search function, among these
800*77c1e3ccSAndroid Build Coastguard Worker  * implemented for inter/intra + rd/non-rd + non-skip segment/skip segment.
801*77c1e3ccSAndroid Build Coastguard Worker  *
802*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi            Top-level encoder structure
803*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tile_data      Pointer to struct holding adaptive
804*77c1e3ccSAndroid Build Coastguard Worker  *                              data/contexts/models for the tile during
805*77c1e3ccSAndroid Build Coastguard Worker  *                              encoding
806*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding all the data for
807*77c1e3ccSAndroid Build Coastguard Worker  *                              the current macroblock
808*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    mi_row         Row coordinate of the block in a step size of
809*77c1e3ccSAndroid Build Coastguard Worker  *                              MI_SIZE
810*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    mi_col         Column coordinate of the block in a step size of
811*77c1e3ccSAndroid Build Coastguard Worker  *                              MI_SIZE
812*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rd_cost        Pointer to structure holding rate and distortion
813*77c1e3ccSAndroid Build Coastguard Worker  *                              stats for the current block
814*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    partition      Partition mode of the parent block
815*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    bsize          Current block size
816*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ctx            Pointer to structure holding coding contexts and
817*77c1e3ccSAndroid Build Coastguard Worker  *                              chosen modes for the current block
818*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    best_rd        Upper bound of rd cost of a valid partition
819*77c1e3ccSAndroid Build Coastguard Worker  *
820*77c1e3ccSAndroid Build Coastguard Worker  * \remark Nothing is returned. Instead, the chosen modes and contexts necessary
821*77c1e3ccSAndroid Build Coastguard Worker  * for reconstruction are stored in ctx, the rate-distortion stats are stored in
822*77c1e3ccSAndroid Build Coastguard Worker  * rd_cost. If no valid mode leading to rd_cost <= best_rd, the status will be
823*77c1e3ccSAndroid Build Coastguard Worker  * signalled by an INT64_MAX rd_cost->rdcost.
824*77c1e3ccSAndroid Build Coastguard Worker  */
pick_sb_modes(AV1_COMP * const cpi,TileDataEnc * tile_data,MACROBLOCK * const x,int mi_row,int mi_col,RD_STATS * rd_cost,PARTITION_TYPE partition,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx,RD_STATS best_rd)825*77c1e3ccSAndroid Build Coastguard Worker static void pick_sb_modes(AV1_COMP *const cpi, TileDataEnc *tile_data,
826*77c1e3ccSAndroid Build Coastguard Worker                           MACROBLOCK *const x, int mi_row, int mi_col,
827*77c1e3ccSAndroid Build Coastguard Worker                           RD_STATS *rd_cost, PARTITION_TYPE partition,
828*77c1e3ccSAndroid Build Coastguard Worker                           BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
829*77c1e3ccSAndroid Build Coastguard Worker                           RD_STATS best_rd) {
830*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.use_best_rd_for_pruning && best_rd.rdcost < 0) {
831*77c1e3ccSAndroid Build Coastguard Worker     ctx->rd_stats.rdcost = INT64_MAX;
832*77c1e3ccSAndroid Build Coastguard Worker     ctx->rd_stats.skip_txfm = 0;
833*77c1e3ccSAndroid Build Coastguard Worker     av1_invalid_rd_stats(rd_cost);
834*77c1e3ccSAndroid Build Coastguard Worker     return;
835*77c1e3ccSAndroid Build Coastguard Worker   }
836*77c1e3ccSAndroid Build Coastguard Worker 
837*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets(cpi, &tile_data->tile_info, x, mi_row, mi_col, bsize);
838*77c1e3ccSAndroid Build Coastguard Worker 
839*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.reuse_prev_rd_results_for_part_ab &&
840*77c1e3ccSAndroid Build Coastguard Worker       ctx->rd_mode_is_ready) {
841*77c1e3ccSAndroid Build Coastguard Worker     assert(ctx->mic.bsize == bsize);
842*77c1e3ccSAndroid Build Coastguard Worker     assert(ctx->mic.partition == partition);
843*77c1e3ccSAndroid Build Coastguard Worker     rd_cost->rate = ctx->rd_stats.rate;
844*77c1e3ccSAndroid Build Coastguard Worker     rd_cost->dist = ctx->rd_stats.dist;
845*77c1e3ccSAndroid Build Coastguard Worker     rd_cost->rdcost = ctx->rd_stats.rdcost;
846*77c1e3ccSAndroid Build Coastguard Worker     return;
847*77c1e3ccSAndroid Build Coastguard Worker   }
848*77c1e3ccSAndroid Build Coastguard Worker 
849*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
850*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
851*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
852*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *mbmi;
853*77c1e3ccSAndroid Build Coastguard Worker   struct macroblock_plane *const p = x->plane;
854*77c1e3ccSAndroid Build Coastguard Worker   struct macroblockd_plane *const pd = xd->plane;
855*77c1e3ccSAndroid Build Coastguard Worker   const AQ_MODE aq_mode = cpi->oxcf.q_cfg.aq_mode;
856*77c1e3ccSAndroid Build Coastguard Worker   TxfmSearchInfo *txfm_info = &x->txfm_search_info;
857*77c1e3ccSAndroid Build Coastguard Worker 
858*77c1e3ccSAndroid Build Coastguard Worker   int i;
859*77c1e3ccSAndroid Build Coastguard Worker 
860*77c1e3ccSAndroid Build Coastguard Worker   // This is only needed for real time/allintra row-mt enabled multi-threaded
861*77c1e3ccSAndroid Build Coastguard Worker   // encoding with cost update frequency set to COST_UPD_TILE/COST_UPD_OFF.
862*77c1e3ccSAndroid Build Coastguard Worker   wait_for_top_right_sb(&cpi->mt_info.enc_row_mt, &tile_data->row_mt_sync,
863*77c1e3ccSAndroid Build Coastguard Worker                         &tile_data->tile_info, cm->seq_params->sb_size,
864*77c1e3ccSAndroid Build Coastguard Worker                         cm->seq_params->mib_size_log2, bsize, mi_row, mi_col);
865*77c1e3ccSAndroid Build Coastguard Worker 
866*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
867*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, rd_pick_sb_modes_time);
868*77c1e3ccSAndroid Build Coastguard Worker #endif
869*77c1e3ccSAndroid Build Coastguard Worker 
870*77c1e3ccSAndroid Build Coastguard Worker   mbmi = xd->mi[0];
871*77c1e3ccSAndroid Build Coastguard Worker   mbmi->bsize = bsize;
872*77c1e3ccSAndroid Build Coastguard Worker   mbmi->partition = partition;
873*77c1e3ccSAndroid Build Coastguard Worker 
874*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RD_DEBUG
875*77c1e3ccSAndroid Build Coastguard Worker   mbmi->mi_row = mi_row;
876*77c1e3ccSAndroid Build Coastguard Worker   mbmi->mi_col = mi_col;
877*77c1e3ccSAndroid Build Coastguard Worker #endif
878*77c1e3ccSAndroid Build Coastguard Worker 
879*77c1e3ccSAndroid Build Coastguard Worker   // Sets up the tx_type_map buffer in MACROBLOCKD.
880*77c1e3ccSAndroid Build Coastguard Worker   xd->tx_type_map = txfm_info->tx_type_map_;
881*77c1e3ccSAndroid Build Coastguard Worker   xd->tx_type_map_stride = mi_size_wide[bsize];
882*77c1e3ccSAndroid Build Coastguard Worker 
883*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < num_planes; ++i) {
884*77c1e3ccSAndroid Build Coastguard Worker     p[i].coeff = ctx->coeff[i];
885*77c1e3ccSAndroid Build Coastguard Worker     p[i].qcoeff = ctx->qcoeff[i];
886*77c1e3ccSAndroid Build Coastguard Worker     p[i].dqcoeff = ctx->dqcoeff[i];
887*77c1e3ccSAndroid Build Coastguard Worker     p[i].eobs = ctx->eobs[i];
888*77c1e3ccSAndroid Build Coastguard Worker     p[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i];
889*77c1e3ccSAndroid Build Coastguard Worker   }
890*77c1e3ccSAndroid Build Coastguard Worker 
891*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < 2; ++i) pd[i].color_index_map = ctx->color_index_map[i];
892*77c1e3ccSAndroid Build Coastguard Worker 
893*77c1e3ccSAndroid Build Coastguard Worker   ctx->skippable = 0;
894*77c1e3ccSAndroid Build Coastguard Worker   // Set to zero to make sure we do not use the previous encoded frame stats
895*77c1e3ccSAndroid Build Coastguard Worker   mbmi->skip_txfm = 0;
896*77c1e3ccSAndroid Build Coastguard Worker   // Reset skip mode flag.
897*77c1e3ccSAndroid Build Coastguard Worker   mbmi->skip_mode = 0;
898*77c1e3ccSAndroid Build Coastguard Worker 
899*77c1e3ccSAndroid Build Coastguard Worker   x->source_variance = av1_get_perpixel_variance_facade(
900*77c1e3ccSAndroid Build Coastguard Worker       cpi, xd, &x->plane[0].src, bsize, AOM_PLANE_Y);
901*77c1e3ccSAndroid Build Coastguard Worker 
902*77c1e3ccSAndroid Build Coastguard Worker   // Initialize default mode evaluation params
903*77c1e3ccSAndroid Build Coastguard Worker   set_mode_eval_params(cpi, x, DEFAULT_EVAL);
904*77c1e3ccSAndroid Build Coastguard Worker 
905*77c1e3ccSAndroid Build Coastguard Worker   // Save rdmult before it might be changed, so it can be restored later.
906*77c1e3ccSAndroid Build Coastguard Worker   const int orig_rdmult = x->rdmult;
907*77c1e3ccSAndroid Build Coastguard Worker   setup_block_rdmult(cpi, x, mi_row, mi_col, bsize, aq_mode, mbmi);
908*77c1e3ccSAndroid Build Coastguard Worker   // Set error per bit for current rdmult
909*77c1e3ccSAndroid Build Coastguard Worker   av1_set_error_per_bit(&x->errorperbit, x->rdmult);
910*77c1e3ccSAndroid Build Coastguard Worker   av1_rd_cost_update(x->rdmult, &best_rd);
911*77c1e3ccSAndroid Build Coastguard Worker 
912*77c1e3ccSAndroid Build Coastguard Worker   // If set best_rd.rdcost to INT64_MAX, the encoder will not use any previous
913*77c1e3ccSAndroid Build Coastguard Worker   // rdcost information for the following mode search.
914*77c1e3ccSAndroid Build Coastguard Worker   // Disabling the feature could get some coding gain, with encoder slowdown.
915*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->sf.part_sf.use_best_rd_for_pruning) {
916*77c1e3ccSAndroid Build Coastguard Worker     av1_invalid_rd_stats(&best_rd);
917*77c1e3ccSAndroid Build Coastguard Worker   }
918*77c1e3ccSAndroid Build Coastguard Worker 
919*77c1e3ccSAndroid Build Coastguard Worker   // Find best coding mode & reconstruct the MB so it is available
920*77c1e3ccSAndroid Build Coastguard Worker   // as a predictor for MBs that follow in the SB
921*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm)) {
922*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
923*77c1e3ccSAndroid Build Coastguard Worker     start_timing(cpi, av1_rd_pick_intra_mode_sb_time);
924*77c1e3ccSAndroid Build Coastguard Worker #endif
925*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, best_rd.rdcost);
926*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
927*77c1e3ccSAndroid Build Coastguard Worker     end_timing(cpi, av1_rd_pick_intra_mode_sb_time);
928*77c1e3ccSAndroid Build Coastguard Worker #endif
929*77c1e3ccSAndroid Build Coastguard Worker   } else {
930*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
931*77c1e3ccSAndroid Build Coastguard Worker     start_timing(cpi, av1_rd_pick_inter_mode_sb_time);
932*77c1e3ccSAndroid Build Coastguard Worker #endif
933*77c1e3ccSAndroid Build Coastguard Worker     if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
934*77c1e3ccSAndroid Build Coastguard Worker       av1_rd_pick_inter_mode_sb_seg_skip(cpi, tile_data, x, mi_row, mi_col,
935*77c1e3ccSAndroid Build Coastguard Worker                                          rd_cost, bsize, ctx, best_rd.rdcost);
936*77c1e3ccSAndroid Build Coastguard Worker     } else {
937*77c1e3ccSAndroid Build Coastguard Worker       av1_rd_pick_inter_mode(cpi, tile_data, x, rd_cost, bsize, ctx,
938*77c1e3ccSAndroid Build Coastguard Worker                              best_rd.rdcost);
939*77c1e3ccSAndroid Build Coastguard Worker     }
940*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
941*77c1e3ccSAndroid Build Coastguard Worker     end_timing(cpi, av1_rd_pick_inter_mode_sb_time);
942*77c1e3ccSAndroid Build Coastguard Worker #endif
943*77c1e3ccSAndroid Build Coastguard Worker   }
944*77c1e3ccSAndroid Build Coastguard Worker 
945*77c1e3ccSAndroid Build Coastguard Worker   // Examine the resulting rate and for AQ mode 2 make a segment choice.
946*77c1e3ccSAndroid Build Coastguard Worker   if (rd_cost->rate != INT_MAX && aq_mode == COMPLEXITY_AQ &&
947*77c1e3ccSAndroid Build Coastguard Worker       bsize >= BLOCK_16X16) {
948*77c1e3ccSAndroid Build Coastguard Worker     av1_caq_select_segment(cpi, x, bsize, mi_row, mi_col, rd_cost->rate);
949*77c1e3ccSAndroid Build Coastguard Worker   }
950*77c1e3ccSAndroid Build Coastguard Worker 
951*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult = orig_rdmult;
952*77c1e3ccSAndroid Build Coastguard Worker 
953*77c1e3ccSAndroid Build Coastguard Worker   // TODO(jingning) The rate-distortion optimization flow needs to be
954*77c1e3ccSAndroid Build Coastguard Worker   // refactored to provide proper exit/return handle.
955*77c1e3ccSAndroid Build Coastguard Worker   if (rd_cost->rate == INT_MAX) rd_cost->rdcost = INT64_MAX;
956*77c1e3ccSAndroid Build Coastguard Worker 
957*77c1e3ccSAndroid Build Coastguard Worker   ctx->rd_stats.rate = rd_cost->rate;
958*77c1e3ccSAndroid Build Coastguard Worker   ctx->rd_stats.dist = rd_cost->dist;
959*77c1e3ccSAndroid Build Coastguard Worker   ctx->rd_stats.rdcost = rd_cost->rdcost;
960*77c1e3ccSAndroid Build Coastguard Worker 
961*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
962*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, rd_pick_sb_modes_time);
963*77c1e3ccSAndroid Build Coastguard Worker #endif
964*77c1e3ccSAndroid Build Coastguard Worker }
965*77c1e3ccSAndroid Build Coastguard Worker 
update_stats(const AV1_COMMON * const cm,ThreadData * td)966*77c1e3ccSAndroid Build Coastguard Worker static void update_stats(const AV1_COMMON *const cm, ThreadData *td) {
967*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *x = &td->mb;
968*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
969*77c1e3ccSAndroid Build Coastguard Worker   const MB_MODE_INFO *const mbmi = xd->mi[0];
970*77c1e3ccSAndroid Build Coastguard Worker   const MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
971*77c1e3ccSAndroid Build Coastguard Worker   const CurrentFrame *const current_frame = &cm->current_frame;
972*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = mbmi->bsize;
973*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *fc = xd->tile_ctx;
974*77c1e3ccSAndroid Build Coastguard Worker   const int seg_ref_active =
975*77c1e3ccSAndroid Build Coastguard Worker       segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_REF_FRAME);
976*77c1e3ccSAndroid Build Coastguard Worker 
977*77c1e3ccSAndroid Build Coastguard Worker   if (current_frame->skip_mode_info.skip_mode_flag && !seg_ref_active &&
978*77c1e3ccSAndroid Build Coastguard Worker       is_comp_ref_allowed(bsize)) {
979*77c1e3ccSAndroid Build Coastguard Worker     const int skip_mode_ctx = av1_get_skip_mode_context(xd);
980*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
981*77c1e3ccSAndroid Build Coastguard Worker     td->counts->skip_mode[skip_mode_ctx][mbmi->skip_mode]++;
982*77c1e3ccSAndroid Build Coastguard Worker #endif
983*77c1e3ccSAndroid Build Coastguard Worker     update_cdf(fc->skip_mode_cdfs[skip_mode_ctx], mbmi->skip_mode, 2);
984*77c1e3ccSAndroid Build Coastguard Worker   }
985*77c1e3ccSAndroid Build Coastguard Worker 
986*77c1e3ccSAndroid Build Coastguard Worker   if (!mbmi->skip_mode && !seg_ref_active) {
987*77c1e3ccSAndroid Build Coastguard Worker     const int skip_ctx = av1_get_skip_txfm_context(xd);
988*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
989*77c1e3ccSAndroid Build Coastguard Worker     td->counts->skip_txfm[skip_ctx][mbmi->skip_txfm]++;
990*77c1e3ccSAndroid Build Coastguard Worker #endif
991*77c1e3ccSAndroid Build Coastguard Worker     update_cdf(fc->skip_txfm_cdfs[skip_ctx], mbmi->skip_txfm, 2);
992*77c1e3ccSAndroid Build Coastguard Worker   }
993*77c1e3ccSAndroid Build Coastguard Worker 
994*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
995*77c1e3ccSAndroid Build Coastguard Worker   // delta quant applies to both intra and inter
996*77c1e3ccSAndroid Build Coastguard Worker   const int super_block_upper_left =
997*77c1e3ccSAndroid Build Coastguard Worker       ((xd->mi_row & (cm->seq_params->mib_size - 1)) == 0) &&
998*77c1e3ccSAndroid Build Coastguard Worker       ((xd->mi_col & (cm->seq_params->mib_size - 1)) == 0);
999*77c1e3ccSAndroid Build Coastguard Worker   const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
1000*77c1e3ccSAndroid Build Coastguard Worker   if (delta_q_info->delta_q_present_flag &&
1001*77c1e3ccSAndroid Build Coastguard Worker       (bsize != cm->seq_params->sb_size || !mbmi->skip_txfm) &&
1002*77c1e3ccSAndroid Build Coastguard Worker       super_block_upper_left) {
1003*77c1e3ccSAndroid Build Coastguard Worker     const int dq = (mbmi->current_qindex - xd->current_base_qindex) /
1004*77c1e3ccSAndroid Build Coastguard Worker                    delta_q_info->delta_q_res;
1005*77c1e3ccSAndroid Build Coastguard Worker     const int absdq = abs(dq);
1006*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < AOMMIN(absdq, DELTA_Q_SMALL); ++i) {
1007*77c1e3ccSAndroid Build Coastguard Worker       td->counts->delta_q[i][1]++;
1008*77c1e3ccSAndroid Build Coastguard Worker     }
1009*77c1e3ccSAndroid Build Coastguard Worker     if (absdq < DELTA_Q_SMALL) td->counts->delta_q[absdq][0]++;
1010*77c1e3ccSAndroid Build Coastguard Worker     if (delta_q_info->delta_lf_present_flag) {
1011*77c1e3ccSAndroid Build Coastguard Worker       if (delta_q_info->delta_lf_multi) {
1012*77c1e3ccSAndroid Build Coastguard Worker         const int frame_lf_count =
1013*77c1e3ccSAndroid Build Coastguard Worker             av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
1014*77c1e3ccSAndroid Build Coastguard Worker         for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
1015*77c1e3ccSAndroid Build Coastguard Worker           const int delta_lf = (mbmi->delta_lf[lf_id] - xd->delta_lf[lf_id]) /
1016*77c1e3ccSAndroid Build Coastguard Worker                                delta_q_info->delta_lf_res;
1017*77c1e3ccSAndroid Build Coastguard Worker           const int abs_delta_lf = abs(delta_lf);
1018*77c1e3ccSAndroid Build Coastguard Worker           for (int i = 0; i < AOMMIN(abs_delta_lf, DELTA_LF_SMALL); ++i) {
1019*77c1e3ccSAndroid Build Coastguard Worker             td->counts->delta_lf_multi[lf_id][i][1]++;
1020*77c1e3ccSAndroid Build Coastguard Worker           }
1021*77c1e3ccSAndroid Build Coastguard Worker           if (abs_delta_lf < DELTA_LF_SMALL)
1022*77c1e3ccSAndroid Build Coastguard Worker             td->counts->delta_lf_multi[lf_id][abs_delta_lf][0]++;
1023*77c1e3ccSAndroid Build Coastguard Worker         }
1024*77c1e3ccSAndroid Build Coastguard Worker       } else {
1025*77c1e3ccSAndroid Build Coastguard Worker         const int delta_lf =
1026*77c1e3ccSAndroid Build Coastguard Worker             (mbmi->delta_lf_from_base - xd->delta_lf_from_base) /
1027*77c1e3ccSAndroid Build Coastguard Worker             delta_q_info->delta_lf_res;
1028*77c1e3ccSAndroid Build Coastguard Worker         const int abs_delta_lf = abs(delta_lf);
1029*77c1e3ccSAndroid Build Coastguard Worker         for (int i = 0; i < AOMMIN(abs_delta_lf, DELTA_LF_SMALL); ++i) {
1030*77c1e3ccSAndroid Build Coastguard Worker           td->counts->delta_lf[i][1]++;
1031*77c1e3ccSAndroid Build Coastguard Worker         }
1032*77c1e3ccSAndroid Build Coastguard Worker         if (abs_delta_lf < DELTA_LF_SMALL)
1033*77c1e3ccSAndroid Build Coastguard Worker           td->counts->delta_lf[abs_delta_lf][0]++;
1034*77c1e3ccSAndroid Build Coastguard Worker       }
1035*77c1e3ccSAndroid Build Coastguard Worker     }
1036*77c1e3ccSAndroid Build Coastguard Worker   }
1037*77c1e3ccSAndroid Build Coastguard Worker #endif
1038*77c1e3ccSAndroid Build Coastguard Worker 
1039*77c1e3ccSAndroid Build Coastguard Worker   if (!is_inter_block(mbmi)) {
1040*77c1e3ccSAndroid Build Coastguard Worker     av1_sum_intra_stats(cm, td->counts, xd, mbmi, xd->above_mbmi, xd->left_mbmi,
1041*77c1e3ccSAndroid Build Coastguard Worker                         frame_is_intra_only(cm));
1042*77c1e3ccSAndroid Build Coastguard Worker   }
1043*77c1e3ccSAndroid Build Coastguard Worker 
1044*77c1e3ccSAndroid Build Coastguard Worker   if (av1_allow_intrabc(cm)) {
1045*77c1e3ccSAndroid Build Coastguard Worker     const int is_intrabc = is_intrabc_block(mbmi);
1046*77c1e3ccSAndroid Build Coastguard Worker     update_cdf(fc->intrabc_cdf, is_intrabc, 2);
1047*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1048*77c1e3ccSAndroid Build Coastguard Worker     ++td->counts->intrabc[is_intrabc];
1049*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1050*77c1e3ccSAndroid Build Coastguard Worker     if (is_intrabc) {
1051*77c1e3ccSAndroid Build Coastguard Worker       const int8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
1052*77c1e3ccSAndroid Build Coastguard Worker       const int_mv dv_ref = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv;
1053*77c1e3ccSAndroid Build Coastguard Worker       av1_update_mv_stats(&mbmi->mv[0].as_mv, &dv_ref.as_mv, &fc->ndvc,
1054*77c1e3ccSAndroid Build Coastguard Worker                           MV_SUBPEL_NONE);
1055*77c1e3ccSAndroid Build Coastguard Worker     }
1056*77c1e3ccSAndroid Build Coastguard Worker   }
1057*77c1e3ccSAndroid Build Coastguard Worker 
1058*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm) || mbmi->skip_mode) return;
1059*77c1e3ccSAndroid Build Coastguard Worker 
1060*77c1e3ccSAndroid Build Coastguard Worker   FRAME_COUNTS *const counts = td->counts;
1061*77c1e3ccSAndroid Build Coastguard Worker   const int inter_block = is_inter_block(mbmi);
1062*77c1e3ccSAndroid Build Coastguard Worker 
1063*77c1e3ccSAndroid Build Coastguard Worker   if (!seg_ref_active) {
1064*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1065*77c1e3ccSAndroid Build Coastguard Worker     counts->intra_inter[av1_get_intra_inter_context(xd)][inter_block]++;
1066*77c1e3ccSAndroid Build Coastguard Worker #endif
1067*77c1e3ccSAndroid Build Coastguard Worker     update_cdf(fc->intra_inter_cdf[av1_get_intra_inter_context(xd)],
1068*77c1e3ccSAndroid Build Coastguard Worker                inter_block, 2);
1069*77c1e3ccSAndroid Build Coastguard Worker     // If the segment reference feature is enabled we have only a single
1070*77c1e3ccSAndroid Build Coastguard Worker     // reference frame allowed for the segment so exclude it from
1071*77c1e3ccSAndroid Build Coastguard Worker     // the reference frame counts used to work out probabilities.
1072*77c1e3ccSAndroid Build Coastguard Worker     if (inter_block) {
1073*77c1e3ccSAndroid Build Coastguard Worker       const MV_REFERENCE_FRAME ref0 = mbmi->ref_frame[0];
1074*77c1e3ccSAndroid Build Coastguard Worker       const MV_REFERENCE_FRAME ref1 = mbmi->ref_frame[1];
1075*77c1e3ccSAndroid Build Coastguard Worker       if (current_frame->reference_mode == REFERENCE_MODE_SELECT) {
1076*77c1e3ccSAndroid Build Coastguard Worker         if (is_comp_ref_allowed(bsize)) {
1077*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1078*77c1e3ccSAndroid Build Coastguard Worker           counts->comp_inter[av1_get_reference_mode_context(xd)]
1079*77c1e3ccSAndroid Build Coastguard Worker                             [has_second_ref(mbmi)]++;
1080*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1081*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(av1_get_reference_mode_cdf(xd), has_second_ref(mbmi), 2);
1082*77c1e3ccSAndroid Build Coastguard Worker         }
1083*77c1e3ccSAndroid Build Coastguard Worker       }
1084*77c1e3ccSAndroid Build Coastguard Worker 
1085*77c1e3ccSAndroid Build Coastguard Worker       if (has_second_ref(mbmi)) {
1086*77c1e3ccSAndroid Build Coastguard Worker         const COMP_REFERENCE_TYPE comp_ref_type = has_uni_comp_refs(mbmi)
1087*77c1e3ccSAndroid Build Coastguard Worker                                                       ? UNIDIR_COMP_REFERENCE
1088*77c1e3ccSAndroid Build Coastguard Worker                                                       : BIDIR_COMP_REFERENCE;
1089*77c1e3ccSAndroid Build Coastguard Worker         update_cdf(av1_get_comp_reference_type_cdf(xd), comp_ref_type,
1090*77c1e3ccSAndroid Build Coastguard Worker                    COMP_REFERENCE_TYPES);
1091*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1092*77c1e3ccSAndroid Build Coastguard Worker         counts->comp_ref_type[av1_get_comp_reference_type_context(xd)]
1093*77c1e3ccSAndroid Build Coastguard Worker                              [comp_ref_type]++;
1094*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1095*77c1e3ccSAndroid Build Coastguard Worker 
1096*77c1e3ccSAndroid Build Coastguard Worker         if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
1097*77c1e3ccSAndroid Build Coastguard Worker           const int bit = (ref0 == BWDREF_FRAME);
1098*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(av1_get_pred_cdf_uni_comp_ref_p(xd), bit, 2);
1099*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1100*77c1e3ccSAndroid Build Coastguard Worker           counts
1101*77c1e3ccSAndroid Build Coastguard Worker               ->uni_comp_ref[av1_get_pred_context_uni_comp_ref_p(xd)][0][bit]++;
1102*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1103*77c1e3ccSAndroid Build Coastguard Worker           if (!bit) {
1104*77c1e3ccSAndroid Build Coastguard Worker             const int bit1 = (ref1 == LAST3_FRAME || ref1 == GOLDEN_FRAME);
1105*77c1e3ccSAndroid Build Coastguard Worker             update_cdf(av1_get_pred_cdf_uni_comp_ref_p1(xd), bit1, 2);
1106*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1107*77c1e3ccSAndroid Build Coastguard Worker             counts->uni_comp_ref[av1_get_pred_context_uni_comp_ref_p1(xd)][1]
1108*77c1e3ccSAndroid Build Coastguard Worker                                 [bit1]++;
1109*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1110*77c1e3ccSAndroid Build Coastguard Worker             if (bit1) {
1111*77c1e3ccSAndroid Build Coastguard Worker               update_cdf(av1_get_pred_cdf_uni_comp_ref_p2(xd),
1112*77c1e3ccSAndroid Build Coastguard Worker                          ref1 == GOLDEN_FRAME, 2);
1113*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1114*77c1e3ccSAndroid Build Coastguard Worker               counts->uni_comp_ref[av1_get_pred_context_uni_comp_ref_p2(xd)][2]
1115*77c1e3ccSAndroid Build Coastguard Worker                                   [ref1 == GOLDEN_FRAME]++;
1116*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1117*77c1e3ccSAndroid Build Coastguard Worker             }
1118*77c1e3ccSAndroid Build Coastguard Worker           }
1119*77c1e3ccSAndroid Build Coastguard Worker         } else {
1120*77c1e3ccSAndroid Build Coastguard Worker           const int bit = (ref0 == GOLDEN_FRAME || ref0 == LAST3_FRAME);
1121*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(av1_get_pred_cdf_comp_ref_p(xd), bit, 2);
1122*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1123*77c1e3ccSAndroid Build Coastguard Worker           counts->comp_ref[av1_get_pred_context_comp_ref_p(xd)][0][bit]++;
1124*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1125*77c1e3ccSAndroid Build Coastguard Worker           if (!bit) {
1126*77c1e3ccSAndroid Build Coastguard Worker             update_cdf(av1_get_pred_cdf_comp_ref_p1(xd), ref0 == LAST2_FRAME,
1127*77c1e3ccSAndroid Build Coastguard Worker                        2);
1128*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1129*77c1e3ccSAndroid Build Coastguard Worker             counts->comp_ref[av1_get_pred_context_comp_ref_p1(xd)][1]
1130*77c1e3ccSAndroid Build Coastguard Worker                             [ref0 == LAST2_FRAME]++;
1131*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1132*77c1e3ccSAndroid Build Coastguard Worker           } else {
1133*77c1e3ccSAndroid Build Coastguard Worker             update_cdf(av1_get_pred_cdf_comp_ref_p2(xd), ref0 == GOLDEN_FRAME,
1134*77c1e3ccSAndroid Build Coastguard Worker                        2);
1135*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1136*77c1e3ccSAndroid Build Coastguard Worker             counts->comp_ref[av1_get_pred_context_comp_ref_p2(xd)][2]
1137*77c1e3ccSAndroid Build Coastguard Worker                             [ref0 == GOLDEN_FRAME]++;
1138*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1139*77c1e3ccSAndroid Build Coastguard Worker           }
1140*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(av1_get_pred_cdf_comp_bwdref_p(xd), ref1 == ALTREF_FRAME,
1141*77c1e3ccSAndroid Build Coastguard Worker                      2);
1142*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1143*77c1e3ccSAndroid Build Coastguard Worker           counts->comp_bwdref[av1_get_pred_context_comp_bwdref_p(xd)][0]
1144*77c1e3ccSAndroid Build Coastguard Worker                              [ref1 == ALTREF_FRAME]++;
1145*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1146*77c1e3ccSAndroid Build Coastguard Worker           if (ref1 != ALTREF_FRAME) {
1147*77c1e3ccSAndroid Build Coastguard Worker             update_cdf(av1_get_pred_cdf_comp_bwdref_p1(xd),
1148*77c1e3ccSAndroid Build Coastguard Worker                        ref1 == ALTREF2_FRAME, 2);
1149*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1150*77c1e3ccSAndroid Build Coastguard Worker             counts->comp_bwdref[av1_get_pred_context_comp_bwdref_p1(xd)][1]
1151*77c1e3ccSAndroid Build Coastguard Worker                                [ref1 == ALTREF2_FRAME]++;
1152*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1153*77c1e3ccSAndroid Build Coastguard Worker           }
1154*77c1e3ccSAndroid Build Coastguard Worker         }
1155*77c1e3ccSAndroid Build Coastguard Worker       } else {
1156*77c1e3ccSAndroid Build Coastguard Worker         const int bit = (ref0 >= BWDREF_FRAME);
1157*77c1e3ccSAndroid Build Coastguard Worker         update_cdf(av1_get_pred_cdf_single_ref_p1(xd), bit, 2);
1158*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1159*77c1e3ccSAndroid Build Coastguard Worker         counts->single_ref[av1_get_pred_context_single_ref_p1(xd)][0][bit]++;
1160*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1161*77c1e3ccSAndroid Build Coastguard Worker         if (bit) {
1162*77c1e3ccSAndroid Build Coastguard Worker           assert(ref0 <= ALTREF_FRAME);
1163*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(av1_get_pred_cdf_single_ref_p2(xd), ref0 == ALTREF_FRAME,
1164*77c1e3ccSAndroid Build Coastguard Worker                      2);
1165*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1166*77c1e3ccSAndroid Build Coastguard Worker           counts->single_ref[av1_get_pred_context_single_ref_p2(xd)][1]
1167*77c1e3ccSAndroid Build Coastguard Worker                             [ref0 == ALTREF_FRAME]++;
1168*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1169*77c1e3ccSAndroid Build Coastguard Worker           if (ref0 != ALTREF_FRAME) {
1170*77c1e3ccSAndroid Build Coastguard Worker             update_cdf(av1_get_pred_cdf_single_ref_p6(xd),
1171*77c1e3ccSAndroid Build Coastguard Worker                        ref0 == ALTREF2_FRAME, 2);
1172*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1173*77c1e3ccSAndroid Build Coastguard Worker             counts->single_ref[av1_get_pred_context_single_ref_p6(xd)][5]
1174*77c1e3ccSAndroid Build Coastguard Worker                               [ref0 == ALTREF2_FRAME]++;
1175*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1176*77c1e3ccSAndroid Build Coastguard Worker           }
1177*77c1e3ccSAndroid Build Coastguard Worker         } else {
1178*77c1e3ccSAndroid Build Coastguard Worker           const int bit1 = !(ref0 == LAST2_FRAME || ref0 == LAST_FRAME);
1179*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(av1_get_pred_cdf_single_ref_p3(xd), bit1, 2);
1180*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1181*77c1e3ccSAndroid Build Coastguard Worker           counts->single_ref[av1_get_pred_context_single_ref_p3(xd)][2][bit1]++;
1182*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1183*77c1e3ccSAndroid Build Coastguard Worker           if (!bit1) {
1184*77c1e3ccSAndroid Build Coastguard Worker             update_cdf(av1_get_pred_cdf_single_ref_p4(xd), ref0 != LAST_FRAME,
1185*77c1e3ccSAndroid Build Coastguard Worker                        2);
1186*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1187*77c1e3ccSAndroid Build Coastguard Worker             counts->single_ref[av1_get_pred_context_single_ref_p4(xd)][3]
1188*77c1e3ccSAndroid Build Coastguard Worker                               [ref0 != LAST_FRAME]++;
1189*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1190*77c1e3ccSAndroid Build Coastguard Worker           } else {
1191*77c1e3ccSAndroid Build Coastguard Worker             update_cdf(av1_get_pred_cdf_single_ref_p5(xd), ref0 != LAST3_FRAME,
1192*77c1e3ccSAndroid Build Coastguard Worker                        2);
1193*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1194*77c1e3ccSAndroid Build Coastguard Worker             counts->single_ref[av1_get_pred_context_single_ref_p5(xd)][4]
1195*77c1e3ccSAndroid Build Coastguard Worker                               [ref0 != LAST3_FRAME]++;
1196*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1197*77c1e3ccSAndroid Build Coastguard Worker           }
1198*77c1e3ccSAndroid Build Coastguard Worker         }
1199*77c1e3ccSAndroid Build Coastguard Worker       }
1200*77c1e3ccSAndroid Build Coastguard Worker 
1201*77c1e3ccSAndroid Build Coastguard Worker       if (cm->seq_params->enable_interintra_compound &&
1202*77c1e3ccSAndroid Build Coastguard Worker           is_interintra_allowed(mbmi)) {
1203*77c1e3ccSAndroid Build Coastguard Worker         const int bsize_group = size_group_lookup[bsize];
1204*77c1e3ccSAndroid Build Coastguard Worker         if (mbmi->ref_frame[1] == INTRA_FRAME) {
1205*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1206*77c1e3ccSAndroid Build Coastguard Worker           counts->interintra[bsize_group][1]++;
1207*77c1e3ccSAndroid Build Coastguard Worker #endif
1208*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(fc->interintra_cdf[bsize_group], 1, 2);
1209*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1210*77c1e3ccSAndroid Build Coastguard Worker           counts->interintra_mode[bsize_group][mbmi->interintra_mode]++;
1211*77c1e3ccSAndroid Build Coastguard Worker #endif
1212*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(fc->interintra_mode_cdf[bsize_group],
1213*77c1e3ccSAndroid Build Coastguard Worker                      mbmi->interintra_mode, INTERINTRA_MODES);
1214*77c1e3ccSAndroid Build Coastguard Worker           if (av1_is_wedge_used(bsize)) {
1215*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1216*77c1e3ccSAndroid Build Coastguard Worker             counts->wedge_interintra[bsize][mbmi->use_wedge_interintra]++;
1217*77c1e3ccSAndroid Build Coastguard Worker #endif
1218*77c1e3ccSAndroid Build Coastguard Worker             update_cdf(fc->wedge_interintra_cdf[bsize],
1219*77c1e3ccSAndroid Build Coastguard Worker                        mbmi->use_wedge_interintra, 2);
1220*77c1e3ccSAndroid Build Coastguard Worker             if (mbmi->use_wedge_interintra) {
1221*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1222*77c1e3ccSAndroid Build Coastguard Worker               counts->wedge_idx[bsize][mbmi->interintra_wedge_index]++;
1223*77c1e3ccSAndroid Build Coastguard Worker #endif
1224*77c1e3ccSAndroid Build Coastguard Worker               update_cdf(fc->wedge_idx_cdf[bsize], mbmi->interintra_wedge_index,
1225*77c1e3ccSAndroid Build Coastguard Worker                          16);
1226*77c1e3ccSAndroid Build Coastguard Worker             }
1227*77c1e3ccSAndroid Build Coastguard Worker           }
1228*77c1e3ccSAndroid Build Coastguard Worker         } else {
1229*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1230*77c1e3ccSAndroid Build Coastguard Worker           counts->interintra[bsize_group][0]++;
1231*77c1e3ccSAndroid Build Coastguard Worker #endif
1232*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(fc->interintra_cdf[bsize_group], 0, 2);
1233*77c1e3ccSAndroid Build Coastguard Worker         }
1234*77c1e3ccSAndroid Build Coastguard Worker       }
1235*77c1e3ccSAndroid Build Coastguard Worker 
1236*77c1e3ccSAndroid Build Coastguard Worker       const MOTION_MODE motion_allowed =
1237*77c1e3ccSAndroid Build Coastguard Worker           cm->features.switchable_motion_mode
1238*77c1e3ccSAndroid Build Coastguard Worker               ? motion_mode_allowed(xd->global_motion, xd, mbmi,
1239*77c1e3ccSAndroid Build Coastguard Worker                                     cm->features.allow_warped_motion)
1240*77c1e3ccSAndroid Build Coastguard Worker               : SIMPLE_TRANSLATION;
1241*77c1e3ccSAndroid Build Coastguard Worker       if (mbmi->ref_frame[1] != INTRA_FRAME) {
1242*77c1e3ccSAndroid Build Coastguard Worker         if (motion_allowed == WARPED_CAUSAL) {
1243*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1244*77c1e3ccSAndroid Build Coastguard Worker           counts->motion_mode[bsize][mbmi->motion_mode]++;
1245*77c1e3ccSAndroid Build Coastguard Worker #endif
1246*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(fc->motion_mode_cdf[bsize], mbmi->motion_mode,
1247*77c1e3ccSAndroid Build Coastguard Worker                      MOTION_MODES);
1248*77c1e3ccSAndroid Build Coastguard Worker         } else if (motion_allowed == OBMC_CAUSAL) {
1249*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1250*77c1e3ccSAndroid Build Coastguard Worker           counts->obmc[bsize][mbmi->motion_mode == OBMC_CAUSAL]++;
1251*77c1e3ccSAndroid Build Coastguard Worker #endif
1252*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(fc->obmc_cdf[bsize], mbmi->motion_mode == OBMC_CAUSAL, 2);
1253*77c1e3ccSAndroid Build Coastguard Worker         }
1254*77c1e3ccSAndroid Build Coastguard Worker       }
1255*77c1e3ccSAndroid Build Coastguard Worker 
1256*77c1e3ccSAndroid Build Coastguard Worker       if (has_second_ref(mbmi)) {
1257*77c1e3ccSAndroid Build Coastguard Worker         assert(current_frame->reference_mode != SINGLE_REFERENCE &&
1258*77c1e3ccSAndroid Build Coastguard Worker                is_inter_compound_mode(mbmi->mode) &&
1259*77c1e3ccSAndroid Build Coastguard Worker                mbmi->motion_mode == SIMPLE_TRANSLATION);
1260*77c1e3ccSAndroid Build Coastguard Worker 
1261*77c1e3ccSAndroid Build Coastguard Worker         const int masked_compound_used = is_any_masked_compound_used(bsize) &&
1262*77c1e3ccSAndroid Build Coastguard Worker                                          cm->seq_params->enable_masked_compound;
1263*77c1e3ccSAndroid Build Coastguard Worker         if (masked_compound_used) {
1264*77c1e3ccSAndroid Build Coastguard Worker           const int comp_group_idx_ctx = get_comp_group_idx_context(xd);
1265*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1266*77c1e3ccSAndroid Build Coastguard Worker           ++counts->comp_group_idx[comp_group_idx_ctx][mbmi->comp_group_idx];
1267*77c1e3ccSAndroid Build Coastguard Worker #endif
1268*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(fc->comp_group_idx_cdf[comp_group_idx_ctx],
1269*77c1e3ccSAndroid Build Coastguard Worker                      mbmi->comp_group_idx, 2);
1270*77c1e3ccSAndroid Build Coastguard Worker         }
1271*77c1e3ccSAndroid Build Coastguard Worker 
1272*77c1e3ccSAndroid Build Coastguard Worker         if (mbmi->comp_group_idx == 0) {
1273*77c1e3ccSAndroid Build Coastguard Worker           const int comp_index_ctx = get_comp_index_context(cm, xd);
1274*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1275*77c1e3ccSAndroid Build Coastguard Worker           ++counts->compound_index[comp_index_ctx][mbmi->compound_idx];
1276*77c1e3ccSAndroid Build Coastguard Worker #endif
1277*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(fc->compound_index_cdf[comp_index_ctx], mbmi->compound_idx,
1278*77c1e3ccSAndroid Build Coastguard Worker                      2);
1279*77c1e3ccSAndroid Build Coastguard Worker         } else {
1280*77c1e3ccSAndroid Build Coastguard Worker           assert(masked_compound_used);
1281*77c1e3ccSAndroid Build Coastguard Worker           if (is_interinter_compound_used(COMPOUND_WEDGE, bsize)) {
1282*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1283*77c1e3ccSAndroid Build Coastguard Worker             ++counts->compound_type[bsize][mbmi->interinter_comp.type -
1284*77c1e3ccSAndroid Build Coastguard Worker                                            COMPOUND_WEDGE];
1285*77c1e3ccSAndroid Build Coastguard Worker #endif
1286*77c1e3ccSAndroid Build Coastguard Worker             update_cdf(fc->compound_type_cdf[bsize],
1287*77c1e3ccSAndroid Build Coastguard Worker                        mbmi->interinter_comp.type - COMPOUND_WEDGE,
1288*77c1e3ccSAndroid Build Coastguard Worker                        MASKED_COMPOUND_TYPES);
1289*77c1e3ccSAndroid Build Coastguard Worker           }
1290*77c1e3ccSAndroid Build Coastguard Worker         }
1291*77c1e3ccSAndroid Build Coastguard Worker       }
1292*77c1e3ccSAndroid Build Coastguard Worker       if (mbmi->interinter_comp.type == COMPOUND_WEDGE) {
1293*77c1e3ccSAndroid Build Coastguard Worker         if (is_interinter_compound_used(COMPOUND_WEDGE, bsize)) {
1294*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1295*77c1e3ccSAndroid Build Coastguard Worker           counts->wedge_idx[bsize][mbmi->interinter_comp.wedge_index]++;
1296*77c1e3ccSAndroid Build Coastguard Worker #endif
1297*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(fc->wedge_idx_cdf[bsize],
1298*77c1e3ccSAndroid Build Coastguard Worker                      mbmi->interinter_comp.wedge_index, 16);
1299*77c1e3ccSAndroid Build Coastguard Worker         }
1300*77c1e3ccSAndroid Build Coastguard Worker       }
1301*77c1e3ccSAndroid Build Coastguard Worker     }
1302*77c1e3ccSAndroid Build Coastguard Worker   }
1303*77c1e3ccSAndroid Build Coastguard Worker 
1304*77c1e3ccSAndroid Build Coastguard Worker   if (inter_block && cm->features.interp_filter == SWITCHABLE &&
1305*77c1e3ccSAndroid Build Coastguard Worker       av1_is_interp_needed(xd)) {
1306*77c1e3ccSAndroid Build Coastguard Worker     update_filter_type_cdf(xd, mbmi, cm->seq_params->enable_dual_filter);
1307*77c1e3ccSAndroid Build Coastguard Worker   }
1308*77c1e3ccSAndroid Build Coastguard Worker   if (inter_block &&
1309*77c1e3ccSAndroid Build Coastguard Worker       !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
1310*77c1e3ccSAndroid Build Coastguard Worker     const PREDICTION_MODE mode = mbmi->mode;
1311*77c1e3ccSAndroid Build Coastguard Worker     const int16_t mode_ctx =
1312*77c1e3ccSAndroid Build Coastguard Worker         av1_mode_context_analyzer(mbmi_ext->mode_context, mbmi->ref_frame);
1313*77c1e3ccSAndroid Build Coastguard Worker     if (has_second_ref(mbmi)) {
1314*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1315*77c1e3ccSAndroid Build Coastguard Worker       ++counts->inter_compound_mode[mode_ctx][INTER_COMPOUND_OFFSET(mode)];
1316*77c1e3ccSAndroid Build Coastguard Worker #endif
1317*77c1e3ccSAndroid Build Coastguard Worker       update_cdf(fc->inter_compound_mode_cdf[mode_ctx],
1318*77c1e3ccSAndroid Build Coastguard Worker                  INTER_COMPOUND_OFFSET(mode), INTER_COMPOUND_MODES);
1319*77c1e3ccSAndroid Build Coastguard Worker     } else {
1320*77c1e3ccSAndroid Build Coastguard Worker       av1_update_inter_mode_stats(fc, counts, mode, mode_ctx);
1321*77c1e3ccSAndroid Build Coastguard Worker     }
1322*77c1e3ccSAndroid Build Coastguard Worker 
1323*77c1e3ccSAndroid Build Coastguard Worker     const int new_mv = mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV;
1324*77c1e3ccSAndroid Build Coastguard Worker     if (new_mv) {
1325*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
1326*77c1e3ccSAndroid Build Coastguard Worker       for (int idx = 0; idx < 2; ++idx) {
1327*77c1e3ccSAndroid Build Coastguard Worker         if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
1328*77c1e3ccSAndroid Build Coastguard Worker           const uint8_t drl_ctx =
1329*77c1e3ccSAndroid Build Coastguard Worker               av1_drl_ctx(mbmi_ext->weight[ref_frame_type], idx);
1330*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(fc->drl_cdf[drl_ctx], mbmi->ref_mv_idx != idx, 2);
1331*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1332*77c1e3ccSAndroid Build Coastguard Worker           ++counts->drl_mode[drl_ctx][mbmi->ref_mv_idx != idx];
1333*77c1e3ccSAndroid Build Coastguard Worker #endif
1334*77c1e3ccSAndroid Build Coastguard Worker           if (mbmi->ref_mv_idx == idx) break;
1335*77c1e3ccSAndroid Build Coastguard Worker         }
1336*77c1e3ccSAndroid Build Coastguard Worker       }
1337*77c1e3ccSAndroid Build Coastguard Worker     }
1338*77c1e3ccSAndroid Build Coastguard Worker 
1339*77c1e3ccSAndroid Build Coastguard Worker     if (have_nearmv_in_inter_mode(mbmi->mode)) {
1340*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
1341*77c1e3ccSAndroid Build Coastguard Worker       for (int idx = 1; idx < 3; ++idx) {
1342*77c1e3ccSAndroid Build Coastguard Worker         if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
1343*77c1e3ccSAndroid Build Coastguard Worker           const uint8_t drl_ctx =
1344*77c1e3ccSAndroid Build Coastguard Worker               av1_drl_ctx(mbmi_ext->weight[ref_frame_type], idx);
1345*77c1e3ccSAndroid Build Coastguard Worker           update_cdf(fc->drl_cdf[drl_ctx], mbmi->ref_mv_idx != idx - 1, 2);
1346*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1347*77c1e3ccSAndroid Build Coastguard Worker           ++counts->drl_mode[drl_ctx][mbmi->ref_mv_idx != idx - 1];
1348*77c1e3ccSAndroid Build Coastguard Worker #endif
1349*77c1e3ccSAndroid Build Coastguard Worker           if (mbmi->ref_mv_idx == idx - 1) break;
1350*77c1e3ccSAndroid Build Coastguard Worker         }
1351*77c1e3ccSAndroid Build Coastguard Worker       }
1352*77c1e3ccSAndroid Build Coastguard Worker     }
1353*77c1e3ccSAndroid Build Coastguard Worker     if (have_newmv_in_inter_mode(mbmi->mode)) {
1354*77c1e3ccSAndroid Build Coastguard Worker       const int allow_hp = cm->features.cur_frame_force_integer_mv
1355*77c1e3ccSAndroid Build Coastguard Worker                                ? MV_SUBPEL_NONE
1356*77c1e3ccSAndroid Build Coastguard Worker                                : cm->features.allow_high_precision_mv;
1357*77c1e3ccSAndroid Build Coastguard Worker       if (new_mv) {
1358*77c1e3ccSAndroid Build Coastguard Worker         for (int ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1359*77c1e3ccSAndroid Build Coastguard Worker           const int_mv ref_mv = av1_get_ref_mv(x, ref);
1360*77c1e3ccSAndroid Build Coastguard Worker           av1_update_mv_stats(&mbmi->mv[ref].as_mv, &ref_mv.as_mv, &fc->nmvc,
1361*77c1e3ccSAndroid Build Coastguard Worker                               allow_hp);
1362*77c1e3ccSAndroid Build Coastguard Worker         }
1363*77c1e3ccSAndroid Build Coastguard Worker       } else if (mbmi->mode == NEAREST_NEWMV || mbmi->mode == NEAR_NEWMV) {
1364*77c1e3ccSAndroid Build Coastguard Worker         const int ref = 1;
1365*77c1e3ccSAndroid Build Coastguard Worker         const int_mv ref_mv = av1_get_ref_mv(x, ref);
1366*77c1e3ccSAndroid Build Coastguard Worker         av1_update_mv_stats(&mbmi->mv[ref].as_mv, &ref_mv.as_mv, &fc->nmvc,
1367*77c1e3ccSAndroid Build Coastguard Worker                             allow_hp);
1368*77c1e3ccSAndroid Build Coastguard Worker       } else if (mbmi->mode == NEW_NEARESTMV || mbmi->mode == NEW_NEARMV) {
1369*77c1e3ccSAndroid Build Coastguard Worker         const int ref = 0;
1370*77c1e3ccSAndroid Build Coastguard Worker         const int_mv ref_mv = av1_get_ref_mv(x, ref);
1371*77c1e3ccSAndroid Build Coastguard Worker         av1_update_mv_stats(&mbmi->mv[ref].as_mv, &ref_mv.as_mv, &fc->nmvc,
1372*77c1e3ccSAndroid Build Coastguard Worker                             allow_hp);
1373*77c1e3ccSAndroid Build Coastguard Worker       }
1374*77c1e3ccSAndroid Build Coastguard Worker     }
1375*77c1e3ccSAndroid Build Coastguard Worker   }
1376*77c1e3ccSAndroid Build Coastguard Worker }
1377*77c1e3ccSAndroid Build Coastguard Worker 
1378*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Reconstructs an individual coding block
1379*77c1e3ccSAndroid Build Coastguard Worker  *
1380*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
1381*77c1e3ccSAndroid Build Coastguard Worker  * Reconstructs an individual coding block by applying the chosen modes stored
1382*77c1e3ccSAndroid Build Coastguard Worker  * in ctx, also updates mode counts and entropy models.
1383*77c1e3ccSAndroid Build Coastguard Worker  *
1384*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi       Top-level encoder structure
1385*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tile_data Pointer to struct holding adaptive
1386*77c1e3ccSAndroid Build Coastguard Worker  *                         data/contexts/models for the tile during encoding
1387*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    td        Pointer to thread data
1388*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tp        Pointer to the starting token
1389*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    mi_row    Row coordinate of the block in a step size of MI_SIZE
1390*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    mi_col    Column coordinate of the block in a step size of
1391*77c1e3ccSAndroid Build Coastguard Worker  *                         MI_SIZE
1392*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    dry_run   A code indicating whether it is part of the final
1393*77c1e3ccSAndroid Build Coastguard Worker  *                         pass for reconstructing the superblock
1394*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    bsize     Current block size
1395*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    partition Partition mode of the parent block
1396*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ctx       Pointer to structure holding coding contexts and the
1397*77c1e3ccSAndroid Build Coastguard Worker  *                         chosen modes for the current block
1398*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rate      Pointer to the total rate for the current block
1399*77c1e3ccSAndroid Build Coastguard Worker  *
1400*77c1e3ccSAndroid Build Coastguard Worker  * \remark Nothing is returned. Instead, reconstructions (w/o in-loop filters)
1401*77c1e3ccSAndroid Build Coastguard Worker  * will be updated in the pixel buffers in td->mb.e_mbd. Also, the chosen modes
1402*77c1e3ccSAndroid Build Coastguard Worker  * will be stored in the MB_MODE_INFO buffer td->mb.e_mbd.mi[0].
1403*77c1e3ccSAndroid Build Coastguard Worker  */
encode_b(const AV1_COMP * const cpi,TileDataEnc * tile_data,ThreadData * td,TokenExtra ** tp,int mi_row,int mi_col,RUN_TYPE dry_run,BLOCK_SIZE bsize,PARTITION_TYPE partition,PICK_MODE_CONTEXT * const ctx,int * rate)1404*77c1e3ccSAndroid Build Coastguard Worker static void encode_b(const AV1_COMP *const cpi, TileDataEnc *tile_data,
1405*77c1e3ccSAndroid Build Coastguard Worker                      ThreadData *td, TokenExtra **tp, int mi_row, int mi_col,
1406*77c1e3ccSAndroid Build Coastguard Worker                      RUN_TYPE dry_run, BLOCK_SIZE bsize,
1407*77c1e3ccSAndroid Build Coastguard Worker                      PARTITION_TYPE partition, PICK_MODE_CONTEXT *const ctx,
1408*77c1e3ccSAndroid Build Coastguard Worker                      int *rate) {
1409*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
1410*77c1e3ccSAndroid Build Coastguard Worker   TileInfo *const tile = &tile_data->tile_info;
1411*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
1412*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *xd = &x->e_mbd;
1413*77c1e3ccSAndroid Build Coastguard Worker   const int subsampling_x = cm->seq_params->subsampling_x;
1414*77c1e3ccSAndroid Build Coastguard Worker   const int subsampling_y = cm->seq_params->subsampling_y;
1415*77c1e3ccSAndroid Build Coastguard Worker 
1416*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets_without_segment_id(cpi, tile, x, mi_row, mi_col, bsize);
1417*77c1e3ccSAndroid Build Coastguard Worker   const int origin_mult = x->rdmult;
1418*77c1e3ccSAndroid Build Coastguard Worker   setup_block_rdmult(cpi, x, mi_row, mi_col, bsize, NO_AQ, NULL);
1419*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *mbmi = xd->mi[0];
1420*77c1e3ccSAndroid Build Coastguard Worker   mbmi->partition = partition;
1421*77c1e3ccSAndroid Build Coastguard Worker   av1_update_state(cpi, td, ctx, mi_row, mi_col, bsize, dry_run);
1422*77c1e3ccSAndroid Build Coastguard Worker 
1423*77c1e3ccSAndroid Build Coastguard Worker   if (!dry_run) {
1424*77c1e3ccSAndroid Build Coastguard Worker     set_cb_offsets(x->mbmi_ext_frame->cb_offset, x->cb_offset[PLANE_TYPE_Y],
1425*77c1e3ccSAndroid Build Coastguard Worker                    x->cb_offset[PLANE_TYPE_UV]);
1426*77c1e3ccSAndroid Build Coastguard Worker     assert(x->cb_offset[PLANE_TYPE_Y] <
1427*77c1e3ccSAndroid Build Coastguard Worker            (1 << num_pels_log2_lookup[cpi->common.seq_params->sb_size]));
1428*77c1e3ccSAndroid Build Coastguard Worker     assert(x->cb_offset[PLANE_TYPE_UV] <
1429*77c1e3ccSAndroid Build Coastguard Worker            ((1 << num_pels_log2_lookup[cpi->common.seq_params->sb_size]) >>
1430*77c1e3ccSAndroid Build Coastguard Worker             (subsampling_x + subsampling_y)));
1431*77c1e3ccSAndroid Build Coastguard Worker   }
1432*77c1e3ccSAndroid Build Coastguard Worker 
1433*77c1e3ccSAndroid Build Coastguard Worker   encode_superblock(cpi, tile_data, td, tp, dry_run, bsize, rate);
1434*77c1e3ccSAndroid Build Coastguard Worker 
1435*77c1e3ccSAndroid Build Coastguard Worker   if (!dry_run) {
1436*77c1e3ccSAndroid Build Coastguard Worker     update_cb_offsets(x, bsize, subsampling_x, subsampling_y);
1437*77c1e3ccSAndroid Build Coastguard Worker     if (bsize == cpi->common.seq_params->sb_size && mbmi->skip_txfm == 1 &&
1438*77c1e3ccSAndroid Build Coastguard Worker         cm->delta_q_info.delta_lf_present_flag) {
1439*77c1e3ccSAndroid Build Coastguard Worker       const int frame_lf_count =
1440*77c1e3ccSAndroid Build Coastguard Worker           av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
1441*77c1e3ccSAndroid Build Coastguard Worker       for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id)
1442*77c1e3ccSAndroid Build Coastguard Worker         mbmi->delta_lf[lf_id] = xd->delta_lf[lf_id];
1443*77c1e3ccSAndroid Build Coastguard Worker       mbmi->delta_lf_from_base = xd->delta_lf_from_base;
1444*77c1e3ccSAndroid Build Coastguard Worker     }
1445*77c1e3ccSAndroid Build Coastguard Worker     if (has_second_ref(mbmi)) {
1446*77c1e3ccSAndroid Build Coastguard Worker       if (mbmi->compound_idx == 0 ||
1447*77c1e3ccSAndroid Build Coastguard Worker           mbmi->interinter_comp.type == COMPOUND_AVERAGE)
1448*77c1e3ccSAndroid Build Coastguard Worker         mbmi->comp_group_idx = 0;
1449*77c1e3ccSAndroid Build Coastguard Worker       else
1450*77c1e3ccSAndroid Build Coastguard Worker         mbmi->comp_group_idx = 1;
1451*77c1e3ccSAndroid Build Coastguard Worker     }
1452*77c1e3ccSAndroid Build Coastguard Worker 
1453*77c1e3ccSAndroid Build Coastguard Worker     // delta quant applies to both intra and inter
1454*77c1e3ccSAndroid Build Coastguard Worker     const int super_block_upper_left =
1455*77c1e3ccSAndroid Build Coastguard Worker         ((mi_row & (cm->seq_params->mib_size - 1)) == 0) &&
1456*77c1e3ccSAndroid Build Coastguard Worker         ((mi_col & (cm->seq_params->mib_size - 1)) == 0);
1457*77c1e3ccSAndroid Build Coastguard Worker     const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
1458*77c1e3ccSAndroid Build Coastguard Worker     if (delta_q_info->delta_q_present_flag &&
1459*77c1e3ccSAndroid Build Coastguard Worker         (bsize != cm->seq_params->sb_size || !mbmi->skip_txfm) &&
1460*77c1e3ccSAndroid Build Coastguard Worker         super_block_upper_left) {
1461*77c1e3ccSAndroid Build Coastguard Worker       xd->current_base_qindex = mbmi->current_qindex;
1462*77c1e3ccSAndroid Build Coastguard Worker       if (delta_q_info->delta_lf_present_flag) {
1463*77c1e3ccSAndroid Build Coastguard Worker         if (delta_q_info->delta_lf_multi) {
1464*77c1e3ccSAndroid Build Coastguard Worker           const int frame_lf_count =
1465*77c1e3ccSAndroid Build Coastguard Worker               av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
1466*77c1e3ccSAndroid Build Coastguard Worker           for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
1467*77c1e3ccSAndroid Build Coastguard Worker             xd->delta_lf[lf_id] = mbmi->delta_lf[lf_id];
1468*77c1e3ccSAndroid Build Coastguard Worker           }
1469*77c1e3ccSAndroid Build Coastguard Worker         } else {
1470*77c1e3ccSAndroid Build Coastguard Worker           xd->delta_lf_from_base = mbmi->delta_lf_from_base;
1471*77c1e3ccSAndroid Build Coastguard Worker         }
1472*77c1e3ccSAndroid Build Coastguard Worker       }
1473*77c1e3ccSAndroid Build Coastguard Worker     }
1474*77c1e3ccSAndroid Build Coastguard Worker 
1475*77c1e3ccSAndroid Build Coastguard Worker     RD_COUNTS *rdc = &td->rd_counts;
1476*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->skip_mode) {
1477*77c1e3ccSAndroid Build Coastguard Worker       assert(!frame_is_intra_only(cm));
1478*77c1e3ccSAndroid Build Coastguard Worker       rdc->skip_mode_used_flag = 1;
1479*77c1e3ccSAndroid Build Coastguard Worker       if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT) {
1480*77c1e3ccSAndroid Build Coastguard Worker         assert(has_second_ref(mbmi));
1481*77c1e3ccSAndroid Build Coastguard Worker         rdc->compound_ref_used_flag = 1;
1482*77c1e3ccSAndroid Build Coastguard Worker       }
1483*77c1e3ccSAndroid Build Coastguard Worker       set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
1484*77c1e3ccSAndroid Build Coastguard Worker     } else {
1485*77c1e3ccSAndroid Build Coastguard Worker       const int seg_ref_active =
1486*77c1e3ccSAndroid Build Coastguard Worker           segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_REF_FRAME);
1487*77c1e3ccSAndroid Build Coastguard Worker       if (!seg_ref_active) {
1488*77c1e3ccSAndroid Build Coastguard Worker         // If the segment reference feature is enabled we have only a single
1489*77c1e3ccSAndroid Build Coastguard Worker         // reference frame allowed for the segment so exclude it from
1490*77c1e3ccSAndroid Build Coastguard Worker         // the reference frame counts used to work out probabilities.
1491*77c1e3ccSAndroid Build Coastguard Worker         if (is_inter_block(mbmi)) {
1492*77c1e3ccSAndroid Build Coastguard Worker           av1_collect_neighbors_ref_counts(xd);
1493*77c1e3ccSAndroid Build Coastguard Worker           if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT) {
1494*77c1e3ccSAndroid Build Coastguard Worker             if (has_second_ref(mbmi)) {
1495*77c1e3ccSAndroid Build Coastguard Worker               // This flag is also updated for 4x4 blocks
1496*77c1e3ccSAndroid Build Coastguard Worker               rdc->compound_ref_used_flag = 1;
1497*77c1e3ccSAndroid Build Coastguard Worker             }
1498*77c1e3ccSAndroid Build Coastguard Worker           }
1499*77c1e3ccSAndroid Build Coastguard Worker           set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
1500*77c1e3ccSAndroid Build Coastguard Worker         }
1501*77c1e3ccSAndroid Build Coastguard Worker       }
1502*77c1e3ccSAndroid Build Coastguard Worker     }
1503*77c1e3ccSAndroid Build Coastguard Worker 
1504*77c1e3ccSAndroid Build Coastguard Worker     if (tile_data->allow_update_cdf) update_stats(&cpi->common, td);
1505*77c1e3ccSAndroid Build Coastguard Worker 
1506*77c1e3ccSAndroid Build Coastguard Worker     // Gather obmc and warped motion count to update the probability.
1507*77c1e3ccSAndroid Build Coastguard Worker     if ((cpi->sf.inter_sf.prune_obmc_prob_thresh > 0 &&
1508*77c1e3ccSAndroid Build Coastguard Worker          cpi->sf.inter_sf.prune_obmc_prob_thresh < INT_MAX) ||
1509*77c1e3ccSAndroid Build Coastguard Worker         (cm->features.allow_warped_motion &&
1510*77c1e3ccSAndroid Build Coastguard Worker          cpi->sf.inter_sf.prune_warped_prob_thresh > 0)) {
1511*77c1e3ccSAndroid Build Coastguard Worker       const int inter_block = is_inter_block(mbmi);
1512*77c1e3ccSAndroid Build Coastguard Worker       const int seg_ref_active =
1513*77c1e3ccSAndroid Build Coastguard Worker           segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_REF_FRAME);
1514*77c1e3ccSAndroid Build Coastguard Worker       if (!seg_ref_active && inter_block) {
1515*77c1e3ccSAndroid Build Coastguard Worker         const MOTION_MODE motion_allowed =
1516*77c1e3ccSAndroid Build Coastguard Worker             cm->features.switchable_motion_mode
1517*77c1e3ccSAndroid Build Coastguard Worker                 ? motion_mode_allowed(xd->global_motion, xd, mbmi,
1518*77c1e3ccSAndroid Build Coastguard Worker                                       cm->features.allow_warped_motion)
1519*77c1e3ccSAndroid Build Coastguard Worker                 : SIMPLE_TRANSLATION;
1520*77c1e3ccSAndroid Build Coastguard Worker 
1521*77c1e3ccSAndroid Build Coastguard Worker         if (mbmi->ref_frame[1] != INTRA_FRAME) {
1522*77c1e3ccSAndroid Build Coastguard Worker           if (motion_allowed >= OBMC_CAUSAL) {
1523*77c1e3ccSAndroid Build Coastguard Worker             td->rd_counts.obmc_used[bsize][mbmi->motion_mode == OBMC_CAUSAL]++;
1524*77c1e3ccSAndroid Build Coastguard Worker           }
1525*77c1e3ccSAndroid Build Coastguard Worker           if (motion_allowed == WARPED_CAUSAL) {
1526*77c1e3ccSAndroid Build Coastguard Worker             td->rd_counts.warped_used[mbmi->motion_mode == WARPED_CAUSAL]++;
1527*77c1e3ccSAndroid Build Coastguard Worker           }
1528*77c1e3ccSAndroid Build Coastguard Worker         }
1529*77c1e3ccSAndroid Build Coastguard Worker       }
1530*77c1e3ccSAndroid Build Coastguard Worker     }
1531*77c1e3ccSAndroid Build Coastguard Worker   }
1532*77c1e3ccSAndroid Build Coastguard Worker   // TODO(Ravi/Remya): Move this copy function to a better logical place
1533*77c1e3ccSAndroid Build Coastguard Worker   // This function will copy the best mode information from block
1534*77c1e3ccSAndroid Build Coastguard Worker   // level (x->mbmi_ext) to frame level (cpi->mbmi_ext_info.frame_base). This
1535*77c1e3ccSAndroid Build Coastguard Worker   // frame level buffer (cpi->mbmi_ext_info.frame_base) will be used during
1536*77c1e3ccSAndroid Build Coastguard Worker   // bitstream preparation.
1537*77c1e3ccSAndroid Build Coastguard Worker   av1_copy_mbmi_ext_to_mbmi_ext_frame(x->mbmi_ext_frame, &x->mbmi_ext,
1538*77c1e3ccSAndroid Build Coastguard Worker                                       av1_ref_frame_type(xd->mi[0]->ref_frame));
1539*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult = origin_mult;
1540*77c1e3ccSAndroid Build Coastguard Worker }
1541*77c1e3ccSAndroid Build Coastguard Worker 
1542*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Reconstructs a partition (may contain multiple coding blocks)
1543*77c1e3ccSAndroid Build Coastguard Worker  *
1544*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
1545*77c1e3ccSAndroid Build Coastguard Worker  * Reconstructs a sub-partition of the superblock by applying the chosen modes
1546*77c1e3ccSAndroid Build Coastguard Worker  * and partition trees stored in pc_tree.
1547*77c1e3ccSAndroid Build Coastguard Worker  *
1548*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi       Top-level encoder structure
1549*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    td        Pointer to thread data
1550*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tile_data Pointer to struct holding adaptive
1551*77c1e3ccSAndroid Build Coastguard Worker  *                         data/contexts/models for the tile during encoding
1552*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tp        Pointer to the starting token
1553*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    mi_row    Row coordinate of the block in a step size of MI_SIZE
1554*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    mi_col    Column coordinate of the block in a step size of
1555*77c1e3ccSAndroid Build Coastguard Worker  *                         MI_SIZE
1556*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    dry_run   A code indicating whether it is part of the final
1557*77c1e3ccSAndroid Build Coastguard Worker  *                         pass for reconstructing the superblock
1558*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    bsize     Current block size
1559*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    pc_tree   Pointer to the PC_TREE node storing the picked
1560*77c1e3ccSAndroid Build Coastguard Worker  *                         partitions and mode info for the current block
1561*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rate      Pointer to the total rate for the current block
1562*77c1e3ccSAndroid Build Coastguard Worker  *
1563*77c1e3ccSAndroid Build Coastguard Worker  * \remark Nothing is returned. Instead, reconstructions (w/o in-loop filters)
1564*77c1e3ccSAndroid Build Coastguard Worker  * will be updated in the pixel buffers in td->mb.e_mbd.
1565*77c1e3ccSAndroid Build Coastguard Worker  */
encode_sb(const AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,int mi_row,int mi_col,RUN_TYPE dry_run,BLOCK_SIZE bsize,PC_TREE * pc_tree,int * rate)1566*77c1e3ccSAndroid Build Coastguard Worker static void encode_sb(const AV1_COMP *const cpi, ThreadData *td,
1567*77c1e3ccSAndroid Build Coastguard Worker                       TileDataEnc *tile_data, TokenExtra **tp, int mi_row,
1568*77c1e3ccSAndroid Build Coastguard Worker                       int mi_col, RUN_TYPE dry_run, BLOCK_SIZE bsize,
1569*77c1e3ccSAndroid Build Coastguard Worker                       PC_TREE *pc_tree, int *rate) {
1570*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize < BLOCK_SIZES_ALL);
1571*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
1572*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
1573*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
1574*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
1575*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize < BLOCK_SIZES_ALL);
1576*77c1e3ccSAndroid Build Coastguard Worker   const int hbs = mi_size_wide[bsize] / 2;
1577*77c1e3ccSAndroid Build Coastguard Worker   const int is_partition_root = bsize >= BLOCK_8X8;
1578*77c1e3ccSAndroid Build Coastguard Worker   const int ctx = is_partition_root
1579*77c1e3ccSAndroid Build Coastguard Worker                       ? partition_plane_context(xd, mi_row, mi_col, bsize)
1580*77c1e3ccSAndroid Build Coastguard Worker                       : -1;
1581*77c1e3ccSAndroid Build Coastguard Worker   const PARTITION_TYPE partition = pc_tree->partitioning;
1582*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE subsize = get_partition_subsize(bsize, partition);
1583*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1584*77c1e3ccSAndroid Build Coastguard Worker   int quarter_step = mi_size_wide[bsize] / 4;
1585*77c1e3ccSAndroid Build Coastguard Worker   int i;
1586*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT);
1587*77c1e3ccSAndroid Build Coastguard Worker #endif
1588*77c1e3ccSAndroid Build Coastguard Worker 
1589*77c1e3ccSAndroid Build Coastguard Worker   if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return;
1590*77c1e3ccSAndroid Build Coastguard Worker   if (subsize == BLOCK_INVALID) return;
1591*77c1e3ccSAndroid Build Coastguard Worker 
1592*77c1e3ccSAndroid Build Coastguard Worker   if (!dry_run && ctx >= 0) {
1593*77c1e3ccSAndroid Build Coastguard Worker     const int has_rows = (mi_row + hbs) < mi_params->mi_rows;
1594*77c1e3ccSAndroid Build Coastguard Worker     const int has_cols = (mi_col + hbs) < mi_params->mi_cols;
1595*77c1e3ccSAndroid Build Coastguard Worker 
1596*77c1e3ccSAndroid Build Coastguard Worker     if (has_rows && has_cols) {
1597*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1598*77c1e3ccSAndroid Build Coastguard Worker       td->counts->partition[ctx][partition]++;
1599*77c1e3ccSAndroid Build Coastguard Worker #endif
1600*77c1e3ccSAndroid Build Coastguard Worker 
1601*77c1e3ccSAndroid Build Coastguard Worker       if (tile_data->allow_update_cdf) {
1602*77c1e3ccSAndroid Build Coastguard Worker         FRAME_CONTEXT *fc = xd->tile_ctx;
1603*77c1e3ccSAndroid Build Coastguard Worker         update_cdf(fc->partition_cdf[ctx], partition,
1604*77c1e3ccSAndroid Build Coastguard Worker                    partition_cdf_length(bsize));
1605*77c1e3ccSAndroid Build Coastguard Worker       }
1606*77c1e3ccSAndroid Build Coastguard Worker     }
1607*77c1e3ccSAndroid Build Coastguard Worker   }
1608*77c1e3ccSAndroid Build Coastguard Worker 
1609*77c1e3ccSAndroid Build Coastguard Worker   switch (partition) {
1610*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_NONE:
1611*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row, mi_col, dry_run, subsize,
1612*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->none, rate);
1613*77c1e3ccSAndroid Build Coastguard Worker       break;
1614*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT:
1615*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row, mi_col, dry_run, subsize,
1616*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->vertical[0], rate);
1617*77c1e3ccSAndroid Build Coastguard Worker       if (mi_col + hbs < mi_params->mi_cols) {
1618*77c1e3ccSAndroid Build Coastguard Worker         encode_b(cpi, tile_data, td, tp, mi_row, mi_col + hbs, dry_run, subsize,
1619*77c1e3ccSAndroid Build Coastguard Worker                  partition, pc_tree->vertical[1], rate);
1620*77c1e3ccSAndroid Build Coastguard Worker       }
1621*77c1e3ccSAndroid Build Coastguard Worker       break;
1622*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ:
1623*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row, mi_col, dry_run, subsize,
1624*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->horizontal[0], rate);
1625*77c1e3ccSAndroid Build Coastguard Worker       if (mi_row + hbs < mi_params->mi_rows) {
1626*77c1e3ccSAndroid Build Coastguard Worker         encode_b(cpi, tile_data, td, tp, mi_row + hbs, mi_col, dry_run, subsize,
1627*77c1e3ccSAndroid Build Coastguard Worker                  partition, pc_tree->horizontal[1], rate);
1628*77c1e3ccSAndroid Build Coastguard Worker       }
1629*77c1e3ccSAndroid Build Coastguard Worker       break;
1630*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_SPLIT:
1631*77c1e3ccSAndroid Build Coastguard Worker       encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, dry_run, subsize,
1632*77c1e3ccSAndroid Build Coastguard Worker                 pc_tree->split[0], rate);
1633*77c1e3ccSAndroid Build Coastguard Worker       encode_sb(cpi, td, tile_data, tp, mi_row, mi_col + hbs, dry_run, subsize,
1634*77c1e3ccSAndroid Build Coastguard Worker                 pc_tree->split[1], rate);
1635*77c1e3ccSAndroid Build Coastguard Worker       encode_sb(cpi, td, tile_data, tp, mi_row + hbs, mi_col, dry_run, subsize,
1636*77c1e3ccSAndroid Build Coastguard Worker                 pc_tree->split[2], rate);
1637*77c1e3ccSAndroid Build Coastguard Worker       encode_sb(cpi, td, tile_data, tp, mi_row + hbs, mi_col + hbs, dry_run,
1638*77c1e3ccSAndroid Build Coastguard Worker                 subsize, pc_tree->split[3], rate);
1639*77c1e3ccSAndroid Build Coastguard Worker       break;
1640*77c1e3ccSAndroid Build Coastguard Worker 
1641*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1642*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_A:
1643*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row, mi_col, dry_run, bsize2,
1644*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->horizontala[0], rate);
1645*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row, mi_col + hbs, dry_run, bsize2,
1646*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->horizontala[1], rate);
1647*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row + hbs, mi_col, dry_run, subsize,
1648*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->horizontala[2], rate);
1649*77c1e3ccSAndroid Build Coastguard Worker       break;
1650*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_B:
1651*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row, mi_col, dry_run, subsize,
1652*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->horizontalb[0], rate);
1653*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row + hbs, mi_col, dry_run, bsize2,
1654*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->horizontalb[1], rate);
1655*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row + hbs, mi_col + hbs, dry_run,
1656*77c1e3ccSAndroid Build Coastguard Worker                bsize2, partition, pc_tree->horizontalb[2], rate);
1657*77c1e3ccSAndroid Build Coastguard Worker       break;
1658*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_A:
1659*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row, mi_col, dry_run, bsize2,
1660*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->verticala[0], rate);
1661*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row + hbs, mi_col, dry_run, bsize2,
1662*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->verticala[1], rate);
1663*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row, mi_col + hbs, dry_run, subsize,
1664*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->verticala[2], rate);
1665*77c1e3ccSAndroid Build Coastguard Worker 
1666*77c1e3ccSAndroid Build Coastguard Worker       break;
1667*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_B:
1668*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row, mi_col, dry_run, subsize,
1669*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->verticalb[0], rate);
1670*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row, mi_col + hbs, dry_run, bsize2,
1671*77c1e3ccSAndroid Build Coastguard Worker                partition, pc_tree->verticalb[1], rate);
1672*77c1e3ccSAndroid Build Coastguard Worker       encode_b(cpi, tile_data, td, tp, mi_row + hbs, mi_col + hbs, dry_run,
1673*77c1e3ccSAndroid Build Coastguard Worker                bsize2, partition, pc_tree->verticalb[2], rate);
1674*77c1e3ccSAndroid Build Coastguard Worker       break;
1675*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_4:
1676*77c1e3ccSAndroid Build Coastguard Worker       for (i = 0; i < SUB_PARTITIONS_PART4; ++i) {
1677*77c1e3ccSAndroid Build Coastguard Worker         int this_mi_row = mi_row + i * quarter_step;
1678*77c1e3ccSAndroid Build Coastguard Worker         if (i > 0 && this_mi_row >= mi_params->mi_rows) break;
1679*77c1e3ccSAndroid Build Coastguard Worker 
1680*77c1e3ccSAndroid Build Coastguard Worker         encode_b(cpi, tile_data, td, tp, this_mi_row, mi_col, dry_run, subsize,
1681*77c1e3ccSAndroid Build Coastguard Worker                  partition, pc_tree->horizontal4[i], rate);
1682*77c1e3ccSAndroid Build Coastguard Worker       }
1683*77c1e3ccSAndroid Build Coastguard Worker       break;
1684*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_4:
1685*77c1e3ccSAndroid Build Coastguard Worker       for (i = 0; i < SUB_PARTITIONS_PART4; ++i) {
1686*77c1e3ccSAndroid Build Coastguard Worker         int this_mi_col = mi_col + i * quarter_step;
1687*77c1e3ccSAndroid Build Coastguard Worker         if (i > 0 && this_mi_col >= mi_params->mi_cols) break;
1688*77c1e3ccSAndroid Build Coastguard Worker         encode_b(cpi, tile_data, td, tp, mi_row, this_mi_col, dry_run, subsize,
1689*77c1e3ccSAndroid Build Coastguard Worker                  partition, pc_tree->vertical4[i], rate);
1690*77c1e3ccSAndroid Build Coastguard Worker       }
1691*77c1e3ccSAndroid Build Coastguard Worker       break;
1692*77c1e3ccSAndroid Build Coastguard Worker #endif
1693*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0 && "Invalid partition type."); break;
1694*77c1e3ccSAndroid Build Coastguard Worker   }
1695*77c1e3ccSAndroid Build Coastguard Worker 
1696*77c1e3ccSAndroid Build Coastguard Worker   update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
1697*77c1e3ccSAndroid Build Coastguard Worker }
1698*77c1e3ccSAndroid Build Coastguard Worker 
is_adjust_var_based_part_enabled(AV1_COMMON * const cm,const PARTITION_SPEED_FEATURES * const part_sf,BLOCK_SIZE bsize)1699*77c1e3ccSAndroid Build Coastguard Worker static inline int is_adjust_var_based_part_enabled(
1700*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMMON *const cm, const PARTITION_SPEED_FEATURES *const part_sf,
1701*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE bsize) {
1702*77c1e3ccSAndroid Build Coastguard Worker   if (part_sf->partition_search_type != VAR_BASED_PARTITION) return 0;
1703*77c1e3ccSAndroid Build Coastguard Worker   if (part_sf->adjust_var_based_rd_partitioning == 0 ||
1704*77c1e3ccSAndroid Build Coastguard Worker       part_sf->adjust_var_based_rd_partitioning > 2)
1705*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1706*77c1e3ccSAndroid Build Coastguard Worker 
1707*77c1e3ccSAndroid Build Coastguard Worker   if (bsize <= BLOCK_32X32) return 1;
1708*77c1e3ccSAndroid Build Coastguard Worker   if (part_sf->adjust_var_based_rd_partitioning == 2) {
1709*77c1e3ccSAndroid Build Coastguard Worker     const int is_larger_qindex = cm->quant_params.base_qindex > 190;
1710*77c1e3ccSAndroid Build Coastguard Worker     const int is_360p_or_larger = AOMMIN(cm->width, cm->height) >= 360;
1711*77c1e3ccSAndroid Build Coastguard Worker     return is_360p_or_larger && is_larger_qindex && bsize == BLOCK_64X64;
1712*77c1e3ccSAndroid Build Coastguard Worker   }
1713*77c1e3ccSAndroid Build Coastguard Worker   return 0;
1714*77c1e3ccSAndroid Build Coastguard Worker }
1715*77c1e3ccSAndroid Build Coastguard Worker 
1716*77c1e3ccSAndroid Build Coastguard Worker /*!\brief AV1 block partition search (partition estimation and partial search).
1717*77c1e3ccSAndroid Build Coastguard Worker *
1718*77c1e3ccSAndroid Build Coastguard Worker * \ingroup partition_search
1719*77c1e3ccSAndroid Build Coastguard Worker * Encode the block by applying pre-calculated partition patterns that are
1720*77c1e3ccSAndroid Build Coastguard Worker * represented by coding block sizes stored in the mbmi array. Minor partition
1721*77c1e3ccSAndroid Build Coastguard Worker * adjustments are tested and applied if they lead to lower rd costs. The
1722*77c1e3ccSAndroid Build Coastguard Worker * partition types are limited to a basic set: none, horz, vert, and split.
1723*77c1e3ccSAndroid Build Coastguard Worker *
1724*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    cpi       Top-level encoder structure
1725*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    td        Pointer to thread data
1726*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    tile_data Pointer to struct holding adaptive
1727*77c1e3ccSAndroid Build Coastguard Worker data/contexts/models for the tile during encoding
1728*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    mib       Array representing MB_MODE_INFO pointers for mi
1729*77c1e3ccSAndroid Build Coastguard Worker blocks starting from the first pixel of the current
1730*77c1e3ccSAndroid Build Coastguard Worker block
1731*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    tp        Pointer to the starting token
1732*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    mi_row    Row coordinate of the block in a step size of MI_SIZE
1733*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    mi_col    Column coordinate of the block in a step size of
1734*77c1e3ccSAndroid Build Coastguard Worker MI_SIZE
1735*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    bsize     Current block size
1736*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    rate      Pointer to the final rate for encoding the current
1737*77c1e3ccSAndroid Build Coastguard Worker block
1738*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    dist      Pointer to the final distortion of the current block
1739*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    do_recon  Whether the reconstruction function needs to be run,
1740*77c1e3ccSAndroid Build Coastguard Worker either for finalizing a superblock or providing
1741*77c1e3ccSAndroid Build Coastguard Worker reference for future sub-partitions
1742*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    pc_tree   Pointer to the PC_TREE node holding the picked
1743*77c1e3ccSAndroid Build Coastguard Worker partitions and mode info for the current block
1744*77c1e3ccSAndroid Build Coastguard Worker *
1745*77c1e3ccSAndroid Build Coastguard Worker * \remark Nothing is returned. The pc_tree struct is modified to store the
1746*77c1e3ccSAndroid Build Coastguard Worker * picked partition and modes. The rate and dist are also updated with those
1747*77c1e3ccSAndroid Build Coastguard Worker * corresponding to the best partition found.
1748*77c1e3ccSAndroid Build Coastguard Worker */
av1_rd_use_partition(AV1_COMP * cpi,ThreadData * td,TileDataEnc * tile_data,MB_MODE_INFO ** mib,TokenExtra ** tp,int mi_row,int mi_col,BLOCK_SIZE bsize,int * rate,int64_t * dist,int do_recon,PC_TREE * pc_tree)1749*77c1e3ccSAndroid Build Coastguard Worker void av1_rd_use_partition(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data,
1750*77c1e3ccSAndroid Build Coastguard Worker                           MB_MODE_INFO **mib, TokenExtra **tp, int mi_row,
1751*77c1e3ccSAndroid Build Coastguard Worker                           int mi_col, BLOCK_SIZE bsize, int *rate,
1752*77c1e3ccSAndroid Build Coastguard Worker                           int64_t *dist, int do_recon, PC_TREE *pc_tree) {
1753*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1754*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
1755*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
1756*77c1e3ccSAndroid Build Coastguard Worker   TileInfo *const tile_info = &tile_data->tile_info;
1757*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
1758*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
1759*77c1e3ccSAndroid Build Coastguard Worker   const ModeCosts *mode_costs = &x->mode_costs;
1760*77c1e3ccSAndroid Build Coastguard Worker   const int bs = mi_size_wide[bsize];
1761*77c1e3ccSAndroid Build Coastguard Worker   const int hbs = bs / 2;
1762*77c1e3ccSAndroid Build Coastguard Worker   const int pl = (bsize >= BLOCK_8X8)
1763*77c1e3ccSAndroid Build Coastguard Worker                      ? partition_plane_context(xd, mi_row, mi_col, bsize)
1764*77c1e3ccSAndroid Build Coastguard Worker                      : 0;
1765*77c1e3ccSAndroid Build Coastguard Worker   const PARTITION_TYPE partition =
1766*77c1e3ccSAndroid Build Coastguard Worker       (bsize >= BLOCK_8X8) ? get_partition(cm, mi_row, mi_col, bsize)
1767*77c1e3ccSAndroid Build Coastguard Worker                            : PARTITION_NONE;
1768*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE subsize = get_partition_subsize(bsize, partition);
1769*77c1e3ccSAndroid Build Coastguard Worker   RD_SEARCH_MACROBLOCK_CONTEXT x_ctx;
1770*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS last_part_rdc, none_rdc, chosen_rdc, invalid_rdc;
1771*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE bs_type = mib[0]->bsize;
1772*77c1e3ccSAndroid Build Coastguard Worker   int use_partition_none = 0;
1773*77c1e3ccSAndroid Build Coastguard Worker   x->try_merge_partition = 0;
1774*77c1e3ccSAndroid Build Coastguard Worker 
1775*77c1e3ccSAndroid Build Coastguard Worker   if (pc_tree->none == NULL) {
1776*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->none = av1_alloc_pmc(cpi, bsize, &td->shared_coeff_buf);
1777*77c1e3ccSAndroid Build Coastguard Worker     if (!pc_tree->none)
1778*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
1779*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PICK_MODE_CONTEXT");
1780*77c1e3ccSAndroid Build Coastguard Worker   }
1781*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT *ctx_none = pc_tree->none;
1782*77c1e3ccSAndroid Build Coastguard Worker 
1783*77c1e3ccSAndroid Build Coastguard Worker   if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return;
1784*77c1e3ccSAndroid Build Coastguard Worker 
1785*77c1e3ccSAndroid Build Coastguard Worker   assert(mi_size_wide[bsize] == mi_size_high[bsize]);
1786*77c1e3ccSAndroid Build Coastguard Worker   // In rt mode, currently the min partition size is BLOCK_8X8.
1787*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize >= cpi->sf.part_sf.default_min_partition_size);
1788*77c1e3ccSAndroid Build Coastguard Worker 
1789*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&last_part_rdc);
1790*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&none_rdc);
1791*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&chosen_rdc);
1792*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&invalid_rdc);
1793*77c1e3ccSAndroid Build Coastguard Worker 
1794*77c1e3ccSAndroid Build Coastguard Worker   pc_tree->partitioning = partition;
1795*77c1e3ccSAndroid Build Coastguard Worker 
1796*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context =
1797*77c1e3ccSAndroid Build Coastguard Worker       cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
1798*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
1799*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
1800*77c1e3ccSAndroid Build Coastguard Worker   av1_save_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
1801*77c1e3ccSAndroid Build Coastguard Worker 
1802*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == BLOCK_16X16 && cpi->vaq_refresh) {
1803*77c1e3ccSAndroid Build Coastguard Worker     av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
1804*77c1e3ccSAndroid Build Coastguard Worker     x->mb_energy = av1_log_block_var(cpi, x, bsize);
1805*77c1e3ccSAndroid Build Coastguard Worker   }
1806*77c1e3ccSAndroid Build Coastguard Worker 
1807*77c1e3ccSAndroid Build Coastguard Worker   // Save rdmult before it might be changed, so it can be restored later.
1808*77c1e3ccSAndroid Build Coastguard Worker   const int orig_rdmult = x->rdmult;
1809*77c1e3ccSAndroid Build Coastguard Worker   setup_block_rdmult(cpi, x, mi_row, mi_col, bsize, NO_AQ, NULL);
1810*77c1e3ccSAndroid Build Coastguard Worker 
1811*77c1e3ccSAndroid Build Coastguard Worker   if (partition != PARTITION_NONE &&
1812*77c1e3ccSAndroid Build Coastguard Worker       is_adjust_var_based_part_enabled(cm, &cpi->sf.part_sf, bsize) &&
1813*77c1e3ccSAndroid Build Coastguard Worker       (mi_row + hbs < mi_params->mi_rows &&
1814*77c1e3ccSAndroid Build Coastguard Worker        mi_col + hbs < mi_params->mi_cols)) {
1815*77c1e3ccSAndroid Build Coastguard Worker     assert(bsize > cpi->sf.part_sf.default_min_partition_size);
1816*77c1e3ccSAndroid Build Coastguard Worker     mib[0]->bsize = bsize;
1817*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->partitioning = PARTITION_NONE;
1818*77c1e3ccSAndroid Build Coastguard Worker     x->try_merge_partition = 1;
1819*77c1e3ccSAndroid Build Coastguard Worker     pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &none_rdc, PARTITION_NONE,
1820*77c1e3ccSAndroid Build Coastguard Worker                   bsize, ctx_none, invalid_rdc);
1821*77c1e3ccSAndroid Build Coastguard Worker 
1822*77c1e3ccSAndroid Build Coastguard Worker     if (none_rdc.rate < INT_MAX) {
1823*77c1e3ccSAndroid Build Coastguard Worker       none_rdc.rate += mode_costs->partition_cost[pl][PARTITION_NONE];
1824*77c1e3ccSAndroid Build Coastguard Worker       none_rdc.rdcost = RDCOST(x->rdmult, none_rdc.rate, none_rdc.dist);
1825*77c1e3ccSAndroid Build Coastguard Worker     }
1826*77c1e3ccSAndroid Build Coastguard Worker 
1827*77c1e3ccSAndroid Build Coastguard Worker     // Try to skip split partition evaluation based on none partition
1828*77c1e3ccSAndroid Build Coastguard Worker     // characteristics.
1829*77c1e3ccSAndroid Build Coastguard Worker     if (none_rdc.rate < INT_MAX && none_rdc.skip_txfm == 1) {
1830*77c1e3ccSAndroid Build Coastguard Worker       use_partition_none = 1;
1831*77c1e3ccSAndroid Build Coastguard Worker     }
1832*77c1e3ccSAndroid Build Coastguard Worker 
1833*77c1e3ccSAndroid Build Coastguard Worker     av1_restore_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
1834*77c1e3ccSAndroid Build Coastguard Worker     mib[0]->bsize = bs_type;
1835*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->partitioning = partition;
1836*77c1e3ccSAndroid Build Coastguard Worker   }
1837*77c1e3ccSAndroid Build Coastguard Worker 
1838*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
1839*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->split[i] = av1_alloc_pc_tree_node(subsize);
1840*77c1e3ccSAndroid Build Coastguard Worker     if (!pc_tree->split[i])
1841*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
1842*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PC_TREE");
1843*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->split[i]->index = i;
1844*77c1e3ccSAndroid Build Coastguard Worker   }
1845*77c1e3ccSAndroid Build Coastguard Worker   switch (partition) {
1846*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_NONE:
1847*77c1e3ccSAndroid Build Coastguard Worker       pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
1848*77c1e3ccSAndroid Build Coastguard Worker                     PARTITION_NONE, bsize, ctx_none, invalid_rdc);
1849*77c1e3ccSAndroid Build Coastguard Worker       break;
1850*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ:
1851*77c1e3ccSAndroid Build Coastguard Worker       if (use_partition_none) {
1852*77c1e3ccSAndroid Build Coastguard Worker         av1_invalid_rd_stats(&last_part_rdc);
1853*77c1e3ccSAndroid Build Coastguard Worker         break;
1854*77c1e3ccSAndroid Build Coastguard Worker       }
1855*77c1e3ccSAndroid Build Coastguard Worker 
1856*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
1857*77c1e3ccSAndroid Build Coastguard Worker         pc_tree->horizontal[i] =
1858*77c1e3ccSAndroid Build Coastguard Worker             av1_alloc_pmc(cpi, subsize, &td->shared_coeff_buf);
1859*77c1e3ccSAndroid Build Coastguard Worker         if (!pc_tree->horizontal[i])
1860*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
1861*77c1e3ccSAndroid Build Coastguard Worker                              "Failed to allocate PICK_MODE_CONTEXT");
1862*77c1e3ccSAndroid Build Coastguard Worker       }
1863*77c1e3ccSAndroid Build Coastguard Worker       pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
1864*77c1e3ccSAndroid Build Coastguard Worker                     PARTITION_HORZ, subsize, pc_tree->horizontal[0],
1865*77c1e3ccSAndroid Build Coastguard Worker                     invalid_rdc);
1866*77c1e3ccSAndroid Build Coastguard Worker       if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
1867*77c1e3ccSAndroid Build Coastguard Worker           mi_row + hbs < mi_params->mi_rows) {
1868*77c1e3ccSAndroid Build Coastguard Worker         RD_STATS tmp_rdc;
1869*77c1e3ccSAndroid Build Coastguard Worker         const PICK_MODE_CONTEXT *const ctx_h = pc_tree->horizontal[0];
1870*77c1e3ccSAndroid Build Coastguard Worker         av1_init_rd_stats(&tmp_rdc);
1871*77c1e3ccSAndroid Build Coastguard Worker         av1_update_state(cpi, td, ctx_h, mi_row, mi_col, subsize, 1);
1872*77c1e3ccSAndroid Build Coastguard Worker         encode_superblock(cpi, tile_data, td, tp, DRY_RUN_NORMAL, subsize,
1873*77c1e3ccSAndroid Build Coastguard Worker                           NULL);
1874*77c1e3ccSAndroid Build Coastguard Worker         pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col, &tmp_rdc,
1875*77c1e3ccSAndroid Build Coastguard Worker                       PARTITION_HORZ, subsize, pc_tree->horizontal[1],
1876*77c1e3ccSAndroid Build Coastguard Worker                       invalid_rdc);
1877*77c1e3ccSAndroid Build Coastguard Worker         if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
1878*77c1e3ccSAndroid Build Coastguard Worker           av1_invalid_rd_stats(&last_part_rdc);
1879*77c1e3ccSAndroid Build Coastguard Worker           break;
1880*77c1e3ccSAndroid Build Coastguard Worker         }
1881*77c1e3ccSAndroid Build Coastguard Worker         last_part_rdc.rate += tmp_rdc.rate;
1882*77c1e3ccSAndroid Build Coastguard Worker         last_part_rdc.dist += tmp_rdc.dist;
1883*77c1e3ccSAndroid Build Coastguard Worker         last_part_rdc.rdcost += tmp_rdc.rdcost;
1884*77c1e3ccSAndroid Build Coastguard Worker       }
1885*77c1e3ccSAndroid Build Coastguard Worker       break;
1886*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT:
1887*77c1e3ccSAndroid Build Coastguard Worker       if (use_partition_none) {
1888*77c1e3ccSAndroid Build Coastguard Worker         av1_invalid_rd_stats(&last_part_rdc);
1889*77c1e3ccSAndroid Build Coastguard Worker         break;
1890*77c1e3ccSAndroid Build Coastguard Worker       }
1891*77c1e3ccSAndroid Build Coastguard Worker 
1892*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
1893*77c1e3ccSAndroid Build Coastguard Worker         pc_tree->vertical[i] =
1894*77c1e3ccSAndroid Build Coastguard Worker             av1_alloc_pmc(cpi, subsize, &td->shared_coeff_buf);
1895*77c1e3ccSAndroid Build Coastguard Worker         if (!pc_tree->vertical[i])
1896*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
1897*77c1e3ccSAndroid Build Coastguard Worker                              "Failed to allocate PICK_MODE_CONTEXT");
1898*77c1e3ccSAndroid Build Coastguard Worker       }
1899*77c1e3ccSAndroid Build Coastguard Worker       pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
1900*77c1e3ccSAndroid Build Coastguard Worker                     PARTITION_VERT, subsize, pc_tree->vertical[0], invalid_rdc);
1901*77c1e3ccSAndroid Build Coastguard Worker       if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
1902*77c1e3ccSAndroid Build Coastguard Worker           mi_col + hbs < mi_params->mi_cols) {
1903*77c1e3ccSAndroid Build Coastguard Worker         RD_STATS tmp_rdc;
1904*77c1e3ccSAndroid Build Coastguard Worker         const PICK_MODE_CONTEXT *const ctx_v = pc_tree->vertical[0];
1905*77c1e3ccSAndroid Build Coastguard Worker         av1_init_rd_stats(&tmp_rdc);
1906*77c1e3ccSAndroid Build Coastguard Worker         av1_update_state(cpi, td, ctx_v, mi_row, mi_col, subsize, 1);
1907*77c1e3ccSAndroid Build Coastguard Worker         encode_superblock(cpi, tile_data, td, tp, DRY_RUN_NORMAL, subsize,
1908*77c1e3ccSAndroid Build Coastguard Worker                           NULL);
1909*77c1e3ccSAndroid Build Coastguard Worker         pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs, &tmp_rdc,
1910*77c1e3ccSAndroid Build Coastguard Worker                       PARTITION_VERT, subsize,
1911*77c1e3ccSAndroid Build Coastguard Worker                       pc_tree->vertical[bsize > BLOCK_8X8], invalid_rdc);
1912*77c1e3ccSAndroid Build Coastguard Worker         if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
1913*77c1e3ccSAndroid Build Coastguard Worker           av1_invalid_rd_stats(&last_part_rdc);
1914*77c1e3ccSAndroid Build Coastguard Worker           break;
1915*77c1e3ccSAndroid Build Coastguard Worker         }
1916*77c1e3ccSAndroid Build Coastguard Worker         last_part_rdc.rate += tmp_rdc.rate;
1917*77c1e3ccSAndroid Build Coastguard Worker         last_part_rdc.dist += tmp_rdc.dist;
1918*77c1e3ccSAndroid Build Coastguard Worker         last_part_rdc.rdcost += tmp_rdc.rdcost;
1919*77c1e3ccSAndroid Build Coastguard Worker       }
1920*77c1e3ccSAndroid Build Coastguard Worker       break;
1921*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_SPLIT:
1922*77c1e3ccSAndroid Build Coastguard Worker       if (use_partition_none) {
1923*77c1e3ccSAndroid Build Coastguard Worker         av1_invalid_rd_stats(&last_part_rdc);
1924*77c1e3ccSAndroid Build Coastguard Worker         break;
1925*77c1e3ccSAndroid Build Coastguard Worker       }
1926*77c1e3ccSAndroid Build Coastguard Worker 
1927*77c1e3ccSAndroid Build Coastguard Worker       last_part_rdc.rate = 0;
1928*77c1e3ccSAndroid Build Coastguard Worker       last_part_rdc.dist = 0;
1929*77c1e3ccSAndroid Build Coastguard Worker       last_part_rdc.rdcost = 0;
1930*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < SUB_PARTITIONS_SPLIT; i++) {
1931*77c1e3ccSAndroid Build Coastguard Worker         int x_idx = (i & 1) * hbs;
1932*77c1e3ccSAndroid Build Coastguard Worker         int y_idx = (i >> 1) * hbs;
1933*77c1e3ccSAndroid Build Coastguard Worker         int jj = i >> 1, ii = i & 0x01;
1934*77c1e3ccSAndroid Build Coastguard Worker         RD_STATS tmp_rdc;
1935*77c1e3ccSAndroid Build Coastguard Worker         if ((mi_row + y_idx >= mi_params->mi_rows) ||
1936*77c1e3ccSAndroid Build Coastguard Worker             (mi_col + x_idx >= mi_params->mi_cols))
1937*77c1e3ccSAndroid Build Coastguard Worker           continue;
1938*77c1e3ccSAndroid Build Coastguard Worker 
1939*77c1e3ccSAndroid Build Coastguard Worker         av1_init_rd_stats(&tmp_rdc);
1940*77c1e3ccSAndroid Build Coastguard Worker         av1_rd_use_partition(
1941*77c1e3ccSAndroid Build Coastguard Worker             cpi, td, tile_data,
1942*77c1e3ccSAndroid Build Coastguard Worker             mib + jj * hbs * mi_params->mi_stride + ii * hbs, tp,
1943*77c1e3ccSAndroid Build Coastguard Worker             mi_row + y_idx, mi_col + x_idx, subsize, &tmp_rdc.rate,
1944*77c1e3ccSAndroid Build Coastguard Worker             &tmp_rdc.dist, i != (SUB_PARTITIONS_SPLIT - 1), pc_tree->split[i]);
1945*77c1e3ccSAndroid Build Coastguard Worker         if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
1946*77c1e3ccSAndroid Build Coastguard Worker           av1_invalid_rd_stats(&last_part_rdc);
1947*77c1e3ccSAndroid Build Coastguard Worker           break;
1948*77c1e3ccSAndroid Build Coastguard Worker         }
1949*77c1e3ccSAndroid Build Coastguard Worker         last_part_rdc.rate += tmp_rdc.rate;
1950*77c1e3ccSAndroid Build Coastguard Worker         last_part_rdc.dist += tmp_rdc.dist;
1951*77c1e3ccSAndroid Build Coastguard Worker       }
1952*77c1e3ccSAndroid Build Coastguard Worker       break;
1953*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_A:
1954*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_B:
1955*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_A:
1956*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_B:
1957*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_4:
1958*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_4:
1959*77c1e3ccSAndroid Build Coastguard Worker       assert(0 && "Cannot handle extended partition types");
1960*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0); break;
1961*77c1e3ccSAndroid Build Coastguard Worker   }
1962*77c1e3ccSAndroid Build Coastguard Worker 
1963*77c1e3ccSAndroid Build Coastguard Worker   if (last_part_rdc.rate < INT_MAX) {
1964*77c1e3ccSAndroid Build Coastguard Worker     last_part_rdc.rate += mode_costs->partition_cost[pl][partition];
1965*77c1e3ccSAndroid Build Coastguard Worker     last_part_rdc.rdcost =
1966*77c1e3ccSAndroid Build Coastguard Worker         RDCOST(x->rdmult, last_part_rdc.rate, last_part_rdc.dist);
1967*77c1e3ccSAndroid Build Coastguard Worker   }
1968*77c1e3ccSAndroid Build Coastguard Worker 
1969*77c1e3ccSAndroid Build Coastguard Worker   if ((cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION &&
1970*77c1e3ccSAndroid Build Coastguard Worker        cpi->sf.part_sf.adjust_var_based_rd_partitioning > 2) &&
1971*77c1e3ccSAndroid Build Coastguard Worker       partition != PARTITION_SPLIT && bsize > BLOCK_8X8 &&
1972*77c1e3ccSAndroid Build Coastguard Worker       (mi_row + bs < mi_params->mi_rows ||
1973*77c1e3ccSAndroid Build Coastguard Worker        mi_row + hbs == mi_params->mi_rows) &&
1974*77c1e3ccSAndroid Build Coastguard Worker       (mi_col + bs < mi_params->mi_cols ||
1975*77c1e3ccSAndroid Build Coastguard Worker        mi_col + hbs == mi_params->mi_cols)) {
1976*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE split_subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
1977*77c1e3ccSAndroid Build Coastguard Worker     chosen_rdc.rate = 0;
1978*77c1e3ccSAndroid Build Coastguard Worker     chosen_rdc.dist = 0;
1979*77c1e3ccSAndroid Build Coastguard Worker 
1980*77c1e3ccSAndroid Build Coastguard Worker     av1_restore_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
1981*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->partitioning = PARTITION_SPLIT;
1982*77c1e3ccSAndroid Build Coastguard Worker 
1983*77c1e3ccSAndroid Build Coastguard Worker     // Split partition.
1984*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < SUB_PARTITIONS_SPLIT; i++) {
1985*77c1e3ccSAndroid Build Coastguard Worker       int x_idx = (i & 1) * hbs;
1986*77c1e3ccSAndroid Build Coastguard Worker       int y_idx = (i >> 1) * hbs;
1987*77c1e3ccSAndroid Build Coastguard Worker       RD_STATS tmp_rdc;
1988*77c1e3ccSAndroid Build Coastguard Worker 
1989*77c1e3ccSAndroid Build Coastguard Worker       if ((mi_row + y_idx >= mi_params->mi_rows) ||
1990*77c1e3ccSAndroid Build Coastguard Worker           (mi_col + x_idx >= mi_params->mi_cols))
1991*77c1e3ccSAndroid Build Coastguard Worker         continue;
1992*77c1e3ccSAndroid Build Coastguard Worker 
1993*77c1e3ccSAndroid Build Coastguard Worker       av1_save_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
1994*77c1e3ccSAndroid Build Coastguard Worker       pc_tree->split[i]->partitioning = PARTITION_NONE;
1995*77c1e3ccSAndroid Build Coastguard Worker       if (pc_tree->split[i]->none == NULL)
1996*77c1e3ccSAndroid Build Coastguard Worker         pc_tree->split[i]->none =
1997*77c1e3ccSAndroid Build Coastguard Worker             av1_alloc_pmc(cpi, split_subsize, &td->shared_coeff_buf);
1998*77c1e3ccSAndroid Build Coastguard Worker       if (!pc_tree->split[i]->none)
1999*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
2000*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to allocate PICK_MODE_CONTEXT");
2001*77c1e3ccSAndroid Build Coastguard Worker       pick_sb_modes(cpi, tile_data, x, mi_row + y_idx, mi_col + x_idx, &tmp_rdc,
2002*77c1e3ccSAndroid Build Coastguard Worker                     PARTITION_SPLIT, split_subsize, pc_tree->split[i]->none,
2003*77c1e3ccSAndroid Build Coastguard Worker                     invalid_rdc);
2004*77c1e3ccSAndroid Build Coastguard Worker 
2005*77c1e3ccSAndroid Build Coastguard Worker       av1_restore_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
2006*77c1e3ccSAndroid Build Coastguard Worker       if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2007*77c1e3ccSAndroid Build Coastguard Worker         av1_invalid_rd_stats(&chosen_rdc);
2008*77c1e3ccSAndroid Build Coastguard Worker         break;
2009*77c1e3ccSAndroid Build Coastguard Worker       }
2010*77c1e3ccSAndroid Build Coastguard Worker 
2011*77c1e3ccSAndroid Build Coastguard Worker       chosen_rdc.rate += tmp_rdc.rate;
2012*77c1e3ccSAndroid Build Coastguard Worker       chosen_rdc.dist += tmp_rdc.dist;
2013*77c1e3ccSAndroid Build Coastguard Worker 
2014*77c1e3ccSAndroid Build Coastguard Worker       if (i != SUB_PARTITIONS_SPLIT - 1)
2015*77c1e3ccSAndroid Build Coastguard Worker         encode_sb(cpi, td, tile_data, tp, mi_row + y_idx, mi_col + x_idx,
2016*77c1e3ccSAndroid Build Coastguard Worker                   OUTPUT_ENABLED, split_subsize, pc_tree->split[i], NULL);
2017*77c1e3ccSAndroid Build Coastguard Worker 
2018*77c1e3ccSAndroid Build Coastguard Worker       chosen_rdc.rate += mode_costs->partition_cost[pl][PARTITION_NONE];
2019*77c1e3ccSAndroid Build Coastguard Worker     }
2020*77c1e3ccSAndroid Build Coastguard Worker     if (chosen_rdc.rate < INT_MAX) {
2021*77c1e3ccSAndroid Build Coastguard Worker       chosen_rdc.rate += mode_costs->partition_cost[pl][PARTITION_SPLIT];
2022*77c1e3ccSAndroid Build Coastguard Worker       chosen_rdc.rdcost = RDCOST(x->rdmult, chosen_rdc.rate, chosen_rdc.dist);
2023*77c1e3ccSAndroid Build Coastguard Worker     }
2024*77c1e3ccSAndroid Build Coastguard Worker   }
2025*77c1e3ccSAndroid Build Coastguard Worker 
2026*77c1e3ccSAndroid Build Coastguard Worker   // If last_part is better set the partitioning to that.
2027*77c1e3ccSAndroid Build Coastguard Worker   if (last_part_rdc.rdcost < chosen_rdc.rdcost) {
2028*77c1e3ccSAndroid Build Coastguard Worker     mib[0]->bsize = bs_type;
2029*77c1e3ccSAndroid Build Coastguard Worker     if (bsize >= BLOCK_8X8) pc_tree->partitioning = partition;
2030*77c1e3ccSAndroid Build Coastguard Worker 
2031*77c1e3ccSAndroid Build Coastguard Worker     chosen_rdc = last_part_rdc;
2032*77c1e3ccSAndroid Build Coastguard Worker   }
2033*77c1e3ccSAndroid Build Coastguard Worker   // If none was better set the partitioning to that.
2034*77c1e3ccSAndroid Build Coastguard Worker   if (none_rdc.rdcost < INT64_MAX &&
2035*77c1e3ccSAndroid Build Coastguard Worker       none_rdc.rdcost - (none_rdc.rdcost >> 9) < chosen_rdc.rdcost) {
2036*77c1e3ccSAndroid Build Coastguard Worker     mib[0]->bsize = bsize;
2037*77c1e3ccSAndroid Build Coastguard Worker     if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
2038*77c1e3ccSAndroid Build Coastguard Worker     chosen_rdc = none_rdc;
2039*77c1e3ccSAndroid Build Coastguard Worker   }
2040*77c1e3ccSAndroid Build Coastguard Worker 
2041*77c1e3ccSAndroid Build Coastguard Worker   av1_restore_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
2042*77c1e3ccSAndroid Build Coastguard Worker 
2043*77c1e3ccSAndroid Build Coastguard Worker   // We must have chosen a partitioning and encoding or we'll fail later on.
2044*77c1e3ccSAndroid Build Coastguard Worker   // No other opportunities for success.
2045*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == cm->seq_params->sb_size)
2046*77c1e3ccSAndroid Build Coastguard Worker     assert(chosen_rdc.rate < INT_MAX && chosen_rdc.dist < INT64_MAX);
2047*77c1e3ccSAndroid Build Coastguard Worker 
2048*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2049*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, encode_sb_time);
2050*77c1e3ccSAndroid Build Coastguard Worker #endif
2051*77c1e3ccSAndroid Build Coastguard Worker   if (do_recon) {
2052*77c1e3ccSAndroid Build Coastguard Worker     if (bsize == cm->seq_params->sb_size) {
2053*77c1e3ccSAndroid Build Coastguard Worker       // NOTE: To get estimate for rate due to the tokens, use:
2054*77c1e3ccSAndroid Build Coastguard Worker       // int rate_coeffs = 0;
2055*77c1e3ccSAndroid Build Coastguard Worker       // encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, DRY_RUN_COSTCOEFFS,
2056*77c1e3ccSAndroid Build Coastguard Worker       //           bsize, pc_tree, &rate_coeffs);
2057*77c1e3ccSAndroid Build Coastguard Worker       set_cb_offsets(x->cb_offset, 0, 0);
2058*77c1e3ccSAndroid Build Coastguard Worker       encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, OUTPUT_ENABLED, bsize,
2059*77c1e3ccSAndroid Build Coastguard Worker                 pc_tree, NULL);
2060*77c1e3ccSAndroid Build Coastguard Worker     } else {
2061*77c1e3ccSAndroid Build Coastguard Worker       encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, DRY_RUN_NORMAL, bsize,
2062*77c1e3ccSAndroid Build Coastguard Worker                 pc_tree, NULL);
2063*77c1e3ccSAndroid Build Coastguard Worker     }
2064*77c1e3ccSAndroid Build Coastguard Worker   }
2065*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2066*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, encode_sb_time);
2067*77c1e3ccSAndroid Build Coastguard Worker #endif
2068*77c1e3ccSAndroid Build Coastguard Worker 
2069*77c1e3ccSAndroid Build Coastguard Worker   *rate = chosen_rdc.rate;
2070*77c1e3ccSAndroid Build Coastguard Worker   *dist = chosen_rdc.dist;
2071*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult = orig_rdmult;
2072*77c1e3ccSAndroid Build Coastguard Worker }
2073*77c1e3ccSAndroid Build Coastguard Worker 
encode_b_nonrd(const AV1_COMP * const cpi,TileDataEnc * tile_data,ThreadData * td,TokenExtra ** tp,int mi_row,int mi_col,RUN_TYPE dry_run,BLOCK_SIZE bsize,PARTITION_TYPE partition,PICK_MODE_CONTEXT * const ctx,int * rate)2074*77c1e3ccSAndroid Build Coastguard Worker static void encode_b_nonrd(const AV1_COMP *const cpi, TileDataEnc *tile_data,
2075*77c1e3ccSAndroid Build Coastguard Worker                            ThreadData *td, TokenExtra **tp, int mi_row,
2076*77c1e3ccSAndroid Build Coastguard Worker                            int mi_col, RUN_TYPE dry_run, BLOCK_SIZE bsize,
2077*77c1e3ccSAndroid Build Coastguard Worker                            PARTITION_TYPE partition,
2078*77c1e3ccSAndroid Build Coastguard Worker                            PICK_MODE_CONTEXT *const ctx, int *rate) {
2079*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2080*77c1e3ccSAndroid Build Coastguard Worker   start_timing((AV1_COMP *)cpi, encode_b_nonrd_time);
2081*77c1e3ccSAndroid Build Coastguard Worker #endif
2082*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
2083*77c1e3ccSAndroid Build Coastguard Worker   TileInfo *const tile = &tile_data->tile_info;
2084*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
2085*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *xd = &x->e_mbd;
2086*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets_without_segment_id(cpi, tile, x, mi_row, mi_col, bsize);
2087*77c1e3ccSAndroid Build Coastguard Worker   const int origin_mult = x->rdmult;
2088*77c1e3ccSAndroid Build Coastguard Worker   setup_block_rdmult(cpi, x, mi_row, mi_col, bsize, NO_AQ, NULL);
2089*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *mbmi = xd->mi[0];
2090*77c1e3ccSAndroid Build Coastguard Worker   mbmi->partition = partition;
2091*77c1e3ccSAndroid Build Coastguard Worker   av1_update_state(cpi, td, ctx, mi_row, mi_col, bsize, dry_run);
2092*77c1e3ccSAndroid Build Coastguard Worker   const int subsampling_x = cpi->common.seq_params->subsampling_x;
2093*77c1e3ccSAndroid Build Coastguard Worker   const int subsampling_y = cpi->common.seq_params->subsampling_y;
2094*77c1e3ccSAndroid Build Coastguard Worker   if (!dry_run) {
2095*77c1e3ccSAndroid Build Coastguard Worker     set_cb_offsets(x->mbmi_ext_frame->cb_offset, x->cb_offset[PLANE_TYPE_Y],
2096*77c1e3ccSAndroid Build Coastguard Worker                    x->cb_offset[PLANE_TYPE_UV]);
2097*77c1e3ccSAndroid Build Coastguard Worker     assert(x->cb_offset[PLANE_TYPE_Y] <
2098*77c1e3ccSAndroid Build Coastguard Worker            (1 << num_pels_log2_lookup[cpi->common.seq_params->sb_size]));
2099*77c1e3ccSAndroid Build Coastguard Worker     assert(x->cb_offset[PLANE_TYPE_UV] <
2100*77c1e3ccSAndroid Build Coastguard Worker            ((1 << num_pels_log2_lookup[cpi->common.seq_params->sb_size]) >>
2101*77c1e3ccSAndroid Build Coastguard Worker             (subsampling_x + subsampling_y)));
2102*77c1e3ccSAndroid Build Coastguard Worker   }
2103*77c1e3ccSAndroid Build Coastguard Worker 
2104*77c1e3ccSAndroid Build Coastguard Worker   encode_superblock(cpi, tile_data, td, tp, dry_run, bsize, rate);
2105*77c1e3ccSAndroid Build Coastguard Worker   if (!dry_run) {
2106*77c1e3ccSAndroid Build Coastguard Worker     update_cb_offsets(x, bsize, subsampling_x, subsampling_y);
2107*77c1e3ccSAndroid Build Coastguard Worker     if (has_second_ref(mbmi)) {
2108*77c1e3ccSAndroid Build Coastguard Worker       if (mbmi->compound_idx == 0 ||
2109*77c1e3ccSAndroid Build Coastguard Worker           mbmi->interinter_comp.type == COMPOUND_AVERAGE)
2110*77c1e3ccSAndroid Build Coastguard Worker         mbmi->comp_group_idx = 0;
2111*77c1e3ccSAndroid Build Coastguard Worker       else
2112*77c1e3ccSAndroid Build Coastguard Worker         mbmi->comp_group_idx = 1;
2113*77c1e3ccSAndroid Build Coastguard Worker       mbmi->compound_idx = 1;
2114*77c1e3ccSAndroid Build Coastguard Worker     }
2115*77c1e3ccSAndroid Build Coastguard Worker     RD_COUNTS *const rdc = &td->rd_counts;
2116*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->skip_mode) {
2117*77c1e3ccSAndroid Build Coastguard Worker       assert(!frame_is_intra_only(cm));
2118*77c1e3ccSAndroid Build Coastguard Worker       rdc->skip_mode_used_flag = 1;
2119*77c1e3ccSAndroid Build Coastguard Worker       if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT &&
2120*77c1e3ccSAndroid Build Coastguard Worker           has_second_ref(mbmi)) {
2121*77c1e3ccSAndroid Build Coastguard Worker         rdc->compound_ref_used_flag = 1;
2122*77c1e3ccSAndroid Build Coastguard Worker       }
2123*77c1e3ccSAndroid Build Coastguard Worker       set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
2124*77c1e3ccSAndroid Build Coastguard Worker     } else {
2125*77c1e3ccSAndroid Build Coastguard Worker       const int seg_ref_active =
2126*77c1e3ccSAndroid Build Coastguard Worker           segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_REF_FRAME);
2127*77c1e3ccSAndroid Build Coastguard Worker       if (!seg_ref_active) {
2128*77c1e3ccSAndroid Build Coastguard Worker         // If the segment reference feature is enabled we have only a single
2129*77c1e3ccSAndroid Build Coastguard Worker         // reference frame allowed for the segment so exclude it from
2130*77c1e3ccSAndroid Build Coastguard Worker         // the reference frame counts used to work out probabilities.
2131*77c1e3ccSAndroid Build Coastguard Worker         if (is_inter_block(mbmi)) {
2132*77c1e3ccSAndroid Build Coastguard Worker           av1_collect_neighbors_ref_counts(xd);
2133*77c1e3ccSAndroid Build Coastguard Worker           if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT &&
2134*77c1e3ccSAndroid Build Coastguard Worker               has_second_ref(mbmi)) {
2135*77c1e3ccSAndroid Build Coastguard Worker             // This flag is also updated for 4x4 blocks
2136*77c1e3ccSAndroid Build Coastguard Worker             rdc->compound_ref_used_flag = 1;
2137*77c1e3ccSAndroid Build Coastguard Worker           }
2138*77c1e3ccSAndroid Build Coastguard Worker           set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
2139*77c1e3ccSAndroid Build Coastguard Worker         }
2140*77c1e3ccSAndroid Build Coastguard Worker       }
2141*77c1e3ccSAndroid Build Coastguard Worker     }
2142*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->oxcf.algo_cfg.loopfilter_control == LOOPFILTER_SELECTIVELY &&
2143*77c1e3ccSAndroid Build Coastguard Worker         (mbmi->mode == NEWMV || mbmi->mode < INTRA_MODE_END)) {
2144*77c1e3ccSAndroid Build Coastguard Worker       int32_t blocks = mi_size_high[bsize] * mi_size_wide[bsize];
2145*77c1e3ccSAndroid Build Coastguard Worker       rdc->newmv_or_intra_blocks += blocks;
2146*77c1e3ccSAndroid Build Coastguard Worker     }
2147*77c1e3ccSAndroid Build Coastguard Worker     if (tile_data->allow_update_cdf) update_stats(&cpi->common, td);
2148*77c1e3ccSAndroid Build Coastguard Worker   }
2149*77c1e3ccSAndroid Build Coastguard Worker   if ((cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ ||
2150*77c1e3ccSAndroid Build Coastguard Worker        cpi->active_map.enabled) &&
2151*77c1e3ccSAndroid Build Coastguard Worker       mbmi->skip_txfm && !cpi->rc.rtc_external_ratectrl && cm->seg.enabled)
2152*77c1e3ccSAndroid Build Coastguard Worker     av1_cyclic_reset_segment_skip(cpi, x, mi_row, mi_col, bsize, dry_run);
2153*77c1e3ccSAndroid Build Coastguard Worker   // TODO(Ravi/Remya): Move this copy function to a better logical place
2154*77c1e3ccSAndroid Build Coastguard Worker   // This function will copy the best mode information from block
2155*77c1e3ccSAndroid Build Coastguard Worker   // level (x->mbmi_ext) to frame level (cpi->mbmi_ext_info.frame_base). This
2156*77c1e3ccSAndroid Build Coastguard Worker   // frame level buffer (cpi->mbmi_ext_info.frame_base) will be used during
2157*77c1e3ccSAndroid Build Coastguard Worker   // bitstream preparation.
2158*77c1e3ccSAndroid Build Coastguard Worker   av1_copy_mbmi_ext_to_mbmi_ext_frame(x->mbmi_ext_frame, &x->mbmi_ext,
2159*77c1e3ccSAndroid Build Coastguard Worker                                       av1_ref_frame_type(xd->mi[0]->ref_frame));
2160*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult = origin_mult;
2161*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2162*77c1e3ccSAndroid Build Coastguard Worker   end_timing((AV1_COMP *)cpi, encode_b_nonrd_time);
2163*77c1e3ccSAndroid Build Coastguard Worker #endif
2164*77c1e3ccSAndroid Build Coastguard Worker }
2165*77c1e3ccSAndroid Build Coastguard Worker 
get_force_zeromv_skip_flag_for_blk(const AV1_COMP * cpi,const MACROBLOCK * x,BLOCK_SIZE bsize)2166*77c1e3ccSAndroid Build Coastguard Worker static int get_force_zeromv_skip_flag_for_blk(const AV1_COMP *cpi,
2167*77c1e3ccSAndroid Build Coastguard Worker                                               const MACROBLOCK *x,
2168*77c1e3ccSAndroid Build Coastguard Worker                                               BLOCK_SIZE bsize) {
2169*77c1e3ccSAndroid Build Coastguard Worker   // Force zero MV skip based on SB level decision
2170*77c1e3ccSAndroid Build Coastguard Worker   if (x->force_zeromv_skip_for_sb < 2) return x->force_zeromv_skip_for_sb;
2171*77c1e3ccSAndroid Build Coastguard Worker 
2172*77c1e3ccSAndroid Build Coastguard Worker   // For blocks of size equal to superblock size, the decision would have been
2173*77c1e3ccSAndroid Build Coastguard Worker   // already done at superblock level. Hence zeromv-skip decision is skipped.
2174*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
2175*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == cm->seq_params->sb_size) return 0;
2176*77c1e3ccSAndroid Build Coastguard Worker 
2177*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
2178*77c1e3ccSAndroid Build Coastguard Worker   const MACROBLOCKD *const xd = &x->e_mbd;
2179*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int thresh_exit_part_y =
2180*77c1e3ccSAndroid Build Coastguard Worker       cpi->zeromv_skip_thresh_exit_part[bsize];
2181*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int thresh_exit_part_uv =
2182*77c1e3ccSAndroid Build Coastguard Worker       CALC_CHROMA_THRESH_FOR_ZEROMV_SKIP(thresh_exit_part_y);
2183*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int thresh_exit_part[MAX_MB_PLANE] = { thresh_exit_part_y,
2184*77c1e3ccSAndroid Build Coastguard Worker                                                         thresh_exit_part_uv,
2185*77c1e3ccSAndroid Build Coastguard Worker                                                         thresh_exit_part_uv };
2186*77c1e3ccSAndroid Build Coastguard Worker   const YV12_BUFFER_CONFIG *const yv12 = get_ref_frame_yv12_buf(cm, LAST_FRAME);
2187*77c1e3ccSAndroid Build Coastguard Worker   const struct scale_factors *const sf =
2188*77c1e3ccSAndroid Build Coastguard Worker       get_ref_scale_factors_const(cm, LAST_FRAME);
2189*77c1e3ccSAndroid Build Coastguard Worker 
2190*77c1e3ccSAndroid Build Coastguard Worker   struct buf_2d yv12_mb[MAX_MB_PLANE];
2191*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_pred_block(xd, yv12_mb, yv12, sf, sf, num_planes);
2192*77c1e3ccSAndroid Build Coastguard Worker 
2193*77c1e3ccSAndroid Build Coastguard Worker   for (int plane = 0; plane < num_planes; ++plane) {
2194*77c1e3ccSAndroid Build Coastguard Worker     const struct macroblock_plane *const p = &x->plane[plane];
2195*77c1e3ccSAndroid Build Coastguard Worker     const struct macroblockd_plane *const pd = &xd->plane[plane];
2196*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE bs =
2197*77c1e3ccSAndroid Build Coastguard Worker         get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
2198*77c1e3ccSAndroid Build Coastguard Worker     const unsigned int plane_sad = cpi->ppi->fn_ptr[bs].sdf(
2199*77c1e3ccSAndroid Build Coastguard Worker         p->src.buf, p->src.stride, yv12_mb[plane].buf, yv12_mb[plane].stride);
2200*77c1e3ccSAndroid Build Coastguard Worker     assert(plane < MAX_MB_PLANE);
2201*77c1e3ccSAndroid Build Coastguard Worker     if (plane_sad >= thresh_exit_part[plane]) return 0;
2202*77c1e3ccSAndroid Build Coastguard Worker   }
2203*77c1e3ccSAndroid Build Coastguard Worker   return 1;
2204*77c1e3ccSAndroid Build Coastguard Worker }
2205*77c1e3ccSAndroid Build Coastguard Worker 
2206*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Top level function to pick block mode for non-RD optimized case
2207*77c1e3ccSAndroid Build Coastguard Worker  *
2208*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
2209*77c1e3ccSAndroid Build Coastguard Worker  * \callgraph
2210*77c1e3ccSAndroid Build Coastguard Worker  * \callergraph
2211*77c1e3ccSAndroid Build Coastguard Worker  * Searches prediction modes, transform, and coefficient coding modes for an
2212*77c1e3ccSAndroid Build Coastguard Worker  * individual coding block. This function is the top-level function that is
2213*77c1e3ccSAndroid Build Coastguard Worker  * used for non-RD optimized mode search (controlled by
2214*77c1e3ccSAndroid Build Coastguard Worker  * \c cpi->sf.rt_sf.use_nonrd_pick_mode). Depending on frame type it calls
2215*77c1e3ccSAndroid Build Coastguard Worker  * inter/skip/hybrid-intra mode search functions
2216*77c1e3ccSAndroid Build Coastguard Worker  *
2217*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi            Top-level encoder structure
2218*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tile_data      Pointer to struct holding adaptive
2219*77c1e3ccSAndroid Build Coastguard Worker  *                              data/contexts/models for the tile during
2220*77c1e3ccSAndroid Build Coastguard Worker  *                              encoding
2221*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding all the data for
2222*77c1e3ccSAndroid Build Coastguard Worker  *                              the current macroblock
2223*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    mi_row         Row coordinate of the block in a step size of
2224*77c1e3ccSAndroid Build Coastguard Worker  *                              MI_SIZE
2225*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    mi_col         Column coordinate of the block in a step size of
2226*77c1e3ccSAndroid Build Coastguard Worker  *                              MI_SIZE
2227*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rd_cost        Pointer to structure holding rate and distortion
2228*77c1e3ccSAndroid Build Coastguard Worker  *                              stats for the current block
2229*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    bsize          Current block size
2230*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ctx            Pointer to structure holding coding contexts and
2231*77c1e3ccSAndroid Build Coastguard Worker  *                              chosen modes for the current block
2232*77c1e3ccSAndroid Build Coastguard Worker  *
2233*77c1e3ccSAndroid Build Coastguard Worker  * \remark Nothing is returned. Instead, the chosen modes and contexts necessary
2234*77c1e3ccSAndroid Build Coastguard Worker  * for reconstruction are stored in ctx, the rate-distortion stats are stored in
2235*77c1e3ccSAndroid Build Coastguard Worker  * rd_cost. If no valid mode leading to rd_cost <= best_rd, the status will be
2236*77c1e3ccSAndroid Build Coastguard Worker  * signalled by an INT64_MAX rd_cost->rdcost.
2237*77c1e3ccSAndroid Build Coastguard Worker  */
pick_sb_modes_nonrd(AV1_COMP * const cpi,TileDataEnc * tile_data,MACROBLOCK * const x,int mi_row,int mi_col,RD_STATS * rd_cost,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx)2238*77c1e3ccSAndroid Build Coastguard Worker static void pick_sb_modes_nonrd(AV1_COMP *const cpi, TileDataEnc *tile_data,
2239*77c1e3ccSAndroid Build Coastguard Worker                                 MACROBLOCK *const x, int mi_row, int mi_col,
2240*77c1e3ccSAndroid Build Coastguard Worker                                 RD_STATS *rd_cost, BLOCK_SIZE bsize,
2241*77c1e3ccSAndroid Build Coastguard Worker                                 PICK_MODE_CONTEXT *ctx) {
2242*77c1e3ccSAndroid Build Coastguard Worker   // For nonrd mode, av1_set_offsets is already called at the superblock level
2243*77c1e3ccSAndroid Build Coastguard Worker   // in encode_nonrd_sb when we determine the partitioning.
2244*77c1e3ccSAndroid Build Coastguard Worker   if (bsize != cpi->common.seq_params->sb_size ||
2245*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.rt_sf.nonrd_check_partition_split == 1) {
2246*77c1e3ccSAndroid Build Coastguard Worker     av1_set_offsets(cpi, &tile_data->tile_info, x, mi_row, mi_col, bsize);
2247*77c1e3ccSAndroid Build Coastguard Worker   }
2248*77c1e3ccSAndroid Build Coastguard Worker   assert(x->last_set_offsets_loc.mi_row == mi_row &&
2249*77c1e3ccSAndroid Build Coastguard Worker          x->last_set_offsets_loc.mi_col == mi_col &&
2250*77c1e3ccSAndroid Build Coastguard Worker          x->last_set_offsets_loc.bsize == bsize);
2251*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2252*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
2253*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
2254*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *mbmi = xd->mi[0];
2255*77c1e3ccSAndroid Build Coastguard Worker   struct macroblock_plane *const p = x->plane;
2256*77c1e3ccSAndroid Build Coastguard Worker   struct macroblockd_plane *const pd = xd->plane;
2257*77c1e3ccSAndroid Build Coastguard Worker   const AQ_MODE aq_mode = cpi->oxcf.q_cfg.aq_mode;
2258*77c1e3ccSAndroid Build Coastguard Worker   TxfmSearchInfo *txfm_info = &x->txfm_search_info;
2259*77c1e3ccSAndroid Build Coastguard Worker   int i;
2260*77c1e3ccSAndroid Build Coastguard Worker   const int seg_skip =
2261*77c1e3ccSAndroid Build Coastguard Worker       segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP);
2262*77c1e3ccSAndroid Build Coastguard Worker 
2263*77c1e3ccSAndroid Build Coastguard Worker   // This is only needed for real time/allintra row-mt enabled multi-threaded
2264*77c1e3ccSAndroid Build Coastguard Worker   // encoding with cost update frequency set to COST_UPD_TILE/COST_UPD_OFF.
2265*77c1e3ccSAndroid Build Coastguard Worker   wait_for_top_right_sb(&cpi->mt_info.enc_row_mt, &tile_data->row_mt_sync,
2266*77c1e3ccSAndroid Build Coastguard Worker                         &tile_data->tile_info, cm->seq_params->sb_size,
2267*77c1e3ccSAndroid Build Coastguard Worker                         cm->seq_params->mib_size_log2, bsize, mi_row, mi_col);
2268*77c1e3ccSAndroid Build Coastguard Worker 
2269*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2270*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, pick_sb_modes_nonrd_time);
2271*77c1e3ccSAndroid Build Coastguard Worker #endif
2272*77c1e3ccSAndroid Build Coastguard Worker   // Sets up the tx_type_map buffer in MACROBLOCKD.
2273*77c1e3ccSAndroid Build Coastguard Worker   xd->tx_type_map = txfm_info->tx_type_map_;
2274*77c1e3ccSAndroid Build Coastguard Worker   xd->tx_type_map_stride = mi_size_wide[bsize];
2275*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < num_planes; ++i) {
2276*77c1e3ccSAndroid Build Coastguard Worker     p[i].coeff = ctx->coeff[i];
2277*77c1e3ccSAndroid Build Coastguard Worker     p[i].qcoeff = ctx->qcoeff[i];
2278*77c1e3ccSAndroid Build Coastguard Worker     p[i].dqcoeff = ctx->dqcoeff[i];
2279*77c1e3ccSAndroid Build Coastguard Worker     p[i].eobs = ctx->eobs[i];
2280*77c1e3ccSAndroid Build Coastguard Worker     p[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i];
2281*77c1e3ccSAndroid Build Coastguard Worker   }
2282*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < 2; ++i) pd[i].color_index_map = ctx->color_index_map[i];
2283*77c1e3ccSAndroid Build Coastguard Worker 
2284*77c1e3ccSAndroid Build Coastguard Worker   if (!seg_skip) {
2285*77c1e3ccSAndroid Build Coastguard Worker     x->force_zeromv_skip_for_blk =
2286*77c1e3ccSAndroid Build Coastguard Worker         get_force_zeromv_skip_flag_for_blk(cpi, x, bsize);
2287*77c1e3ccSAndroid Build Coastguard Worker 
2288*77c1e3ccSAndroid Build Coastguard Worker     // Source variance may be already compute at superblock level, so no need
2289*77c1e3ccSAndroid Build Coastguard Worker     // to recompute, unless bsize < sb_size or source_variance is not yet set.
2290*77c1e3ccSAndroid Build Coastguard Worker     if (!x->force_zeromv_skip_for_blk &&
2291*77c1e3ccSAndroid Build Coastguard Worker         (x->source_variance == UINT_MAX || bsize < cm->seq_params->sb_size))
2292*77c1e3ccSAndroid Build Coastguard Worker       x->source_variance = av1_get_perpixel_variance_facade(
2293*77c1e3ccSAndroid Build Coastguard Worker           cpi, xd, &x->plane[0].src, bsize, AOM_PLANE_Y);
2294*77c1e3ccSAndroid Build Coastguard Worker   }
2295*77c1e3ccSAndroid Build Coastguard Worker 
2296*77c1e3ccSAndroid Build Coastguard Worker   // Save rdmult before it might be changed, so it can be restored later.
2297*77c1e3ccSAndroid Build Coastguard Worker   const int orig_rdmult = x->rdmult;
2298*77c1e3ccSAndroid Build Coastguard Worker   setup_block_rdmult(cpi, x, mi_row, mi_col, bsize, aq_mode, mbmi);
2299*77c1e3ccSAndroid Build Coastguard Worker   // Set error per bit for current rdmult
2300*77c1e3ccSAndroid Build Coastguard Worker   av1_set_error_per_bit(&x->errorperbit, x->rdmult);
2301*77c1e3ccSAndroid Build Coastguard Worker   // Find best coding mode & reconstruct the MB so it is available
2302*77c1e3ccSAndroid Build Coastguard Worker   // as a predictor for MBs that follow in the SB
2303*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm)) {
2304*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2305*77c1e3ccSAndroid Build Coastguard Worker     start_timing(cpi, hybrid_intra_mode_search_time);
2306*77c1e3ccSAndroid Build Coastguard Worker #endif
2307*77c1e3ccSAndroid Build Coastguard Worker     hybrid_intra_mode_search(cpi, x, rd_cost, bsize, ctx);
2308*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2309*77c1e3ccSAndroid Build Coastguard Worker     end_timing(cpi, hybrid_intra_mode_search_time);
2310*77c1e3ccSAndroid Build Coastguard Worker #endif
2311*77c1e3ccSAndroid Build Coastguard Worker   } else {
2312*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2313*77c1e3ccSAndroid Build Coastguard Worker     start_timing(cpi, nonrd_pick_inter_mode_sb_time);
2314*77c1e3ccSAndroid Build Coastguard Worker #endif
2315*77c1e3ccSAndroid Build Coastguard Worker     if (seg_skip) {
2316*77c1e3ccSAndroid Build Coastguard Worker       x->force_zeromv_skip_for_blk = 1;
2317*77c1e3ccSAndroid Build Coastguard Worker       // TODO(marpan): Consider adding a function for nonrd:
2318*77c1e3ccSAndroid Build Coastguard Worker       // av1_nonrd_pick_inter_mode_sb_seg_skip(), instead of setting
2319*77c1e3ccSAndroid Build Coastguard Worker       // x->force_zeromv_skip flag and entering av1_nonrd_pick_inter_mode_sb().
2320*77c1e3ccSAndroid Build Coastguard Worker     }
2321*77c1e3ccSAndroid Build Coastguard Worker     av1_nonrd_pick_inter_mode_sb(cpi, tile_data, x, rd_cost, bsize, ctx);
2322*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2323*77c1e3ccSAndroid Build Coastguard Worker     end_timing(cpi, nonrd_pick_inter_mode_sb_time);
2324*77c1e3ccSAndroid Build Coastguard Worker #endif
2325*77c1e3ccSAndroid Build Coastguard Worker   }
2326*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.rt_sf.skip_cdef_sb) {
2327*77c1e3ccSAndroid Build Coastguard Worker     // cdef_strength is initialized to 1 which means skip_cdef, and is updated
2328*77c1e3ccSAndroid Build Coastguard Worker     // here. Check to see is skipping cdef is allowed. Never skip on slide/scene
2329*77c1e3ccSAndroid Build Coastguard Worker     // change, near a key frame, or when color sensitivity is set. Always allow
2330*77c1e3ccSAndroid Build Coastguard Worker     // cdef_skip for seg_skip = 1.
2331*77c1e3ccSAndroid Build Coastguard Worker     const int allow_cdef_skipping =
2332*77c1e3ccSAndroid Build Coastguard Worker         seg_skip ||
2333*77c1e3ccSAndroid Build Coastguard Worker         (cpi->rc.frames_since_key > 10 && !cpi->rc.high_source_sad &&
2334*77c1e3ccSAndroid Build Coastguard Worker          !(x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
2335*77c1e3ccSAndroid Build Coastguard Worker            x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]));
2336*77c1e3ccSAndroid Build Coastguard Worker 
2337*77c1e3ccSAndroid Build Coastguard Worker     // Find the corresponding 64x64 block. It'll be the 128x128 block if that's
2338*77c1e3ccSAndroid Build Coastguard Worker     // the block size.
2339*77c1e3ccSAndroid Build Coastguard Worker     const int mi_row_sb = mi_row - mi_row % MI_SIZE_64X64;
2340*77c1e3ccSAndroid Build Coastguard Worker     const int mi_col_sb = mi_col - mi_col % MI_SIZE_64X64;
2341*77c1e3ccSAndroid Build Coastguard Worker     MB_MODE_INFO **mi_sb =
2342*77c1e3ccSAndroid Build Coastguard Worker         cm->mi_params.mi_grid_base +
2343*77c1e3ccSAndroid Build Coastguard Worker         get_mi_grid_idx(&cm->mi_params, mi_row_sb, mi_col_sb);
2344*77c1e3ccSAndroid Build Coastguard Worker     const int is_720p_or_larger = AOMMIN(cm->width, cm->height) >= 720;
2345*77c1e3ccSAndroid Build Coastguard Worker     unsigned int thresh_spatial_var =
2346*77c1e3ccSAndroid Build Coastguard Worker         (cpi->oxcf.speed >= 11 && !is_720p_or_larger &&
2347*77c1e3ccSAndroid Build Coastguard Worker          cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN)
2348*77c1e3ccSAndroid Build Coastguard Worker             ? 400
2349*77c1e3ccSAndroid Build Coastguard Worker             : UINT_MAX;
2350*77c1e3ccSAndroid Build Coastguard Worker     // For skip_cdef_sb = 1: do not skip if allow_cdef_skipping is false or
2351*77c1e3ccSAndroid Build Coastguard Worker     // intra or new mv is picked, with possible conidition on spatial variance.
2352*77c1e3ccSAndroid Build Coastguard Worker     // For skip_cdef_sb >= 2: more aggressive mode to always skip unless
2353*77c1e3ccSAndroid Build Coastguard Worker     // allow_cdef_skipping is false and source_variance is non-zero.
2354*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->sf.rt_sf.skip_cdef_sb >= 2) {
2355*77c1e3ccSAndroid Build Coastguard Worker       mi_sb[0]->cdef_strength =
2356*77c1e3ccSAndroid Build Coastguard Worker           mi_sb[0]->cdef_strength &&
2357*77c1e3ccSAndroid Build Coastguard Worker           (allow_cdef_skipping || x->source_variance == 0);
2358*77c1e3ccSAndroid Build Coastguard Worker     } else {
2359*77c1e3ccSAndroid Build Coastguard Worker       mi_sb[0]->cdef_strength =
2360*77c1e3ccSAndroid Build Coastguard Worker           mi_sb[0]->cdef_strength && allow_cdef_skipping &&
2361*77c1e3ccSAndroid Build Coastguard Worker           !(x->source_variance < thresh_spatial_var &&
2362*77c1e3ccSAndroid Build Coastguard Worker             (mbmi->mode < INTRA_MODES || mbmi->mode == NEWMV));
2363*77c1e3ccSAndroid Build Coastguard Worker     }
2364*77c1e3ccSAndroid Build Coastguard Worker     // Store in the pickmode context.
2365*77c1e3ccSAndroid Build Coastguard Worker     ctx->mic.cdef_strength = mi_sb[0]->cdef_strength;
2366*77c1e3ccSAndroid Build Coastguard Worker   }
2367*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult = orig_rdmult;
2368*77c1e3ccSAndroid Build Coastguard Worker   ctx->rd_stats.rate = rd_cost->rate;
2369*77c1e3ccSAndroid Build Coastguard Worker   ctx->rd_stats.dist = rd_cost->dist;
2370*77c1e3ccSAndroid Build Coastguard Worker   ctx->rd_stats.rdcost = rd_cost->rdcost;
2371*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2372*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, pick_sb_modes_nonrd_time);
2373*77c1e3ccSAndroid Build Coastguard Worker #endif
2374*77c1e3ccSAndroid Build Coastguard Worker }
2375*77c1e3ccSAndroid Build Coastguard Worker 
try_split_partition(AV1_COMP * const cpi,ThreadData * const td,TileDataEnc * const tile_data,TileInfo * const tile_info,TokenExtra ** tp,MACROBLOCK * const x,MACROBLOCKD * const xd,const CommonModeInfoParams * const mi_params,const int mi_row,const int mi_col,const BLOCK_SIZE bsize,const int pl,PC_TREE * pc_tree)2376*77c1e3ccSAndroid Build Coastguard Worker static int try_split_partition(AV1_COMP *const cpi, ThreadData *const td,
2377*77c1e3ccSAndroid Build Coastguard Worker                                TileDataEnc *const tile_data,
2378*77c1e3ccSAndroid Build Coastguard Worker                                TileInfo *const tile_info, TokenExtra **tp,
2379*77c1e3ccSAndroid Build Coastguard Worker                                MACROBLOCK *const x, MACROBLOCKD *const xd,
2380*77c1e3ccSAndroid Build Coastguard Worker                                const CommonModeInfoParams *const mi_params,
2381*77c1e3ccSAndroid Build Coastguard Worker                                const int mi_row, const int mi_col,
2382*77c1e3ccSAndroid Build Coastguard Worker                                const BLOCK_SIZE bsize, const int pl,
2383*77c1e3ccSAndroid Build Coastguard Worker                                PC_TREE *pc_tree) {
2384*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2385*77c1e3ccSAndroid Build Coastguard Worker   const ModeCosts *mode_costs = &x->mode_costs;
2386*77c1e3ccSAndroid Build Coastguard Worker   const int hbs = mi_size_wide[bsize] / 2;
2387*77c1e3ccSAndroid Build Coastguard Worker   if (mi_row + mi_size_high[bsize] >= mi_params->mi_rows ||
2388*77c1e3ccSAndroid Build Coastguard Worker       mi_col + mi_size_wide[bsize] >= mi_params->mi_cols)
2389*77c1e3ccSAndroid Build Coastguard Worker     return 0;
2390*77c1e3ccSAndroid Build Coastguard Worker   if (bsize <= BLOCK_8X8 || frame_is_intra_only(cm)) return 0;
2391*77c1e3ccSAndroid Build Coastguard Worker   if (x->content_state_sb.source_sad_nonrd <= kLowSad) return 0;
2392*77c1e3ccSAndroid Build Coastguard Worker 
2393*77c1e3ccSAndroid Build Coastguard Worker   // Do not try split partition when the source sad is small, or
2394*77c1e3ccSAndroid Build Coastguard Worker   // the prediction residual is small.
2395*77c1e3ccSAndroid Build Coastguard Worker   const YV12_BUFFER_CONFIG *const yv12 = get_ref_frame_yv12_buf(cm, LAST_FRAME);
2396*77c1e3ccSAndroid Build Coastguard Worker   const struct scale_factors *const sf =
2397*77c1e3ccSAndroid Build Coastguard Worker       get_ref_scale_factors_const(cm, LAST_FRAME);
2398*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
2399*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_src_planes(x, cpi->source, mi_row, mi_col, num_planes, bsize);
2400*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_pre_planes(xd, 0, yv12, mi_row, mi_col, sf, num_planes);
2401*77c1e3ccSAndroid Build Coastguard Worker   int block_sad = 0;
2402*77c1e3ccSAndroid Build Coastguard Worker   for (int plane = 0; plane < num_planes; ++plane) {
2403*77c1e3ccSAndroid Build Coastguard Worker     const struct macroblock_plane *const p = &x->plane[plane];
2404*77c1e3ccSAndroid Build Coastguard Worker     const struct macroblockd_plane *const pd = &xd->plane[plane];
2405*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE bs =
2406*77c1e3ccSAndroid Build Coastguard Worker         get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
2407*77c1e3ccSAndroid Build Coastguard Worker     const unsigned int plane_sad = cpi->ppi->fn_ptr[bs].sdf(
2408*77c1e3ccSAndroid Build Coastguard Worker         p->src.buf, p->src.stride, pd->pre[0].buf, pd->pre[0].stride);
2409*77c1e3ccSAndroid Build Coastguard Worker     block_sad += plane_sad;
2410*77c1e3ccSAndroid Build Coastguard Worker   }
2411*77c1e3ccSAndroid Build Coastguard Worker   const int blk_pix = block_size_wide[bsize] * block_size_high[bsize];
2412*77c1e3ccSAndroid Build Coastguard Worker   const int block_avg_sad = block_sad / blk_pix;
2413*77c1e3ccSAndroid Build Coastguard Worker   // TODO(chengchen): find a proper threshold. It might change according to
2414*77c1e3ccSAndroid Build Coastguard Worker   // q as well.
2415*77c1e3ccSAndroid Build Coastguard Worker   const int threshold = 25;
2416*77c1e3ccSAndroid Build Coastguard Worker   if (block_avg_sad < threshold) return 0;
2417*77c1e3ccSAndroid Build Coastguard Worker 
2418*77c1e3ccSAndroid Build Coastguard Worker   RD_SEARCH_MACROBLOCK_CONTEXT x_ctx;
2419*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS split_rdc, none_rdc;
2420*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&split_rdc);
2421*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&none_rdc);
2422*77c1e3ccSAndroid Build Coastguard Worker   av1_save_context(x, &x_ctx, mi_row, mi_col, bsize, 3);
2423*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context =
2424*77c1e3ccSAndroid Build Coastguard Worker       cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
2425*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
2426*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
2427*77c1e3ccSAndroid Build Coastguard Worker 
2428*77c1e3ccSAndroid Build Coastguard Worker   // Calculate rdcost for none partition
2429*77c1e3ccSAndroid Build Coastguard Worker   pc_tree->partitioning = PARTITION_NONE;
2430*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2431*77c1e3ccSAndroid Build Coastguard Worker   if (!pc_tree->none) {
2432*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->none = av1_alloc_pmc(cpi, bsize, &td->shared_coeff_buf);
2433*77c1e3ccSAndroid Build Coastguard Worker     if (!pc_tree->none)
2434*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
2435*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PICK_MODE_CONTEXT");
2436*77c1e3ccSAndroid Build Coastguard Worker   } else {
2437*77c1e3ccSAndroid Build Coastguard Worker     av1_reset_pmc(pc_tree->none);
2438*77c1e3ccSAndroid Build Coastguard Worker   }
2439*77c1e3ccSAndroid Build Coastguard Worker   pick_sb_modes_nonrd(cpi, tile_data, x, mi_row, mi_col, &none_rdc, bsize,
2440*77c1e3ccSAndroid Build Coastguard Worker                       pc_tree->none);
2441*77c1e3ccSAndroid Build Coastguard Worker   none_rdc.rate += mode_costs->partition_cost[pl][PARTITION_NONE];
2442*77c1e3ccSAndroid Build Coastguard Worker   none_rdc.rdcost = RDCOST(x->rdmult, none_rdc.rate, none_rdc.dist);
2443*77c1e3ccSAndroid Build Coastguard Worker   av1_restore_context(x, &x_ctx, mi_row, mi_col, bsize, 3);
2444*77c1e3ccSAndroid Build Coastguard Worker 
2445*77c1e3ccSAndroid Build Coastguard Worker   // Calculate rdcost for split partition
2446*77c1e3ccSAndroid Build Coastguard Worker   pc_tree->partitioning = PARTITION_SPLIT;
2447*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
2448*77c1e3ccSAndroid Build Coastguard Worker   av1_init_rd_stats(&split_rdc);
2449*77c1e3ccSAndroid Build Coastguard Worker   split_rdc.rate += mode_costs->partition_cost[pl][PARTITION_SPLIT];
2450*77c1e3ccSAndroid Build Coastguard Worker   if (subsize >= BLOCK_8X8) {
2451*77c1e3ccSAndroid Build Coastguard Worker     split_rdc.rate += (mode_costs->partition_cost[pl][PARTITION_NONE] * 4);
2452*77c1e3ccSAndroid Build Coastguard Worker   }
2453*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
2454*77c1e3ccSAndroid Build Coastguard Worker     if (!pc_tree->split[i]) {
2455*77c1e3ccSAndroid Build Coastguard Worker       pc_tree->split[i] = av1_alloc_pc_tree_node(subsize);
2456*77c1e3ccSAndroid Build Coastguard Worker       if (!pc_tree->split[i])
2457*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
2458*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to allocate PC_TREE");
2459*77c1e3ccSAndroid Build Coastguard Worker     }
2460*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->split[i]->index = i;
2461*77c1e3ccSAndroid Build Coastguard Worker   }
2462*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; i++) {
2463*77c1e3ccSAndroid Build Coastguard Worker     RD_STATS block_rdc;
2464*77c1e3ccSAndroid Build Coastguard Worker     av1_invalid_rd_stats(&block_rdc);
2465*77c1e3ccSAndroid Build Coastguard Worker     int x_idx = (i & 1) * hbs;
2466*77c1e3ccSAndroid Build Coastguard Worker     int y_idx = (i >> 1) * hbs;
2467*77c1e3ccSAndroid Build Coastguard Worker     if ((mi_row + y_idx >= mi_params->mi_rows) ||
2468*77c1e3ccSAndroid Build Coastguard Worker         (mi_col + x_idx >= mi_params->mi_cols))
2469*77c1e3ccSAndroid Build Coastguard Worker       continue;
2470*77c1e3ccSAndroid Build Coastguard Worker     xd->above_txfm_context =
2471*77c1e3ccSAndroid Build Coastguard Worker         cm->above_contexts.txfm[tile_info->tile_row] + mi_col + x_idx;
2472*77c1e3ccSAndroid Build Coastguard Worker     xd->left_txfm_context =
2473*77c1e3ccSAndroid Build Coastguard Worker         xd->left_txfm_context_buffer + ((mi_row + y_idx) & MAX_MIB_MASK);
2474*77c1e3ccSAndroid Build Coastguard Worker     if (!pc_tree->split[i]->none) {
2475*77c1e3ccSAndroid Build Coastguard Worker       pc_tree->split[i]->none =
2476*77c1e3ccSAndroid Build Coastguard Worker           av1_alloc_pmc(cpi, subsize, &td->shared_coeff_buf);
2477*77c1e3ccSAndroid Build Coastguard Worker       if (!pc_tree->split[i]->none)
2478*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
2479*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to allocate PICK_MODE_CONTEXT");
2480*77c1e3ccSAndroid Build Coastguard Worker     } else {
2481*77c1e3ccSAndroid Build Coastguard Worker       av1_reset_pmc(pc_tree->split[i]->none);
2482*77c1e3ccSAndroid Build Coastguard Worker     }
2483*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->split[i]->partitioning = PARTITION_NONE;
2484*77c1e3ccSAndroid Build Coastguard Worker     pick_sb_modes_nonrd(cpi, tile_data, x, mi_row + y_idx, mi_col + x_idx,
2485*77c1e3ccSAndroid Build Coastguard Worker                         &block_rdc, subsize, pc_tree->split[i]->none);
2486*77c1e3ccSAndroid Build Coastguard Worker     split_rdc.rate += block_rdc.rate;
2487*77c1e3ccSAndroid Build Coastguard Worker     split_rdc.dist += block_rdc.dist;
2488*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_cost_update(x->rdmult, &split_rdc);
2489*77c1e3ccSAndroid Build Coastguard Worker     if (none_rdc.rdcost < split_rdc.rdcost) break;
2490*77c1e3ccSAndroid Build Coastguard Worker     if (i != SUB_PARTITIONS_SPLIT - 1)
2491*77c1e3ccSAndroid Build Coastguard Worker       encode_b_nonrd(cpi, tile_data, td, tp, mi_row + y_idx, mi_col + x_idx, 1,
2492*77c1e3ccSAndroid Build Coastguard Worker                      subsize, PARTITION_NONE, pc_tree->split[i]->none, NULL);
2493*77c1e3ccSAndroid Build Coastguard Worker   }
2494*77c1e3ccSAndroid Build Coastguard Worker   av1_restore_context(x, &x_ctx, mi_row, mi_col, bsize, 3);
2495*77c1e3ccSAndroid Build Coastguard Worker   split_rdc.rdcost = RDCOST(x->rdmult, split_rdc.rate, split_rdc.dist);
2496*77c1e3ccSAndroid Build Coastguard Worker   const int split = split_rdc.rdcost < none_rdc.rdcost;
2497*77c1e3ccSAndroid Build Coastguard Worker 
2498*77c1e3ccSAndroid Build Coastguard Worker   return split;
2499*77c1e3ccSAndroid Build Coastguard Worker }
2500*77c1e3ccSAndroid Build Coastguard Worker 
2501*77c1e3ccSAndroid Build Coastguard Worker // Returns if SPLIT partitions should be evaluated
calc_do_split_flag(const AV1_COMP * cpi,const MACROBLOCK * x,const PC_TREE * pc_tree,const RD_STATS * none_rdc,const CommonModeInfoParams * mi_params,int mi_row,int mi_col,int hbs,BLOCK_SIZE bsize,PARTITION_TYPE partition)2502*77c1e3ccSAndroid Build Coastguard Worker static bool calc_do_split_flag(const AV1_COMP *cpi, const MACROBLOCK *x,
2503*77c1e3ccSAndroid Build Coastguard Worker                                const PC_TREE *pc_tree, const RD_STATS *none_rdc,
2504*77c1e3ccSAndroid Build Coastguard Worker                                const CommonModeInfoParams *mi_params,
2505*77c1e3ccSAndroid Build Coastguard Worker                                int mi_row, int mi_col, int hbs,
2506*77c1e3ccSAndroid Build Coastguard Worker                                BLOCK_SIZE bsize, PARTITION_TYPE partition) {
2507*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
2508*77c1e3ccSAndroid Build Coastguard Worker   const int is_larger_qindex = cm->quant_params.base_qindex > 100;
2509*77c1e3ccSAndroid Build Coastguard Worker   const MACROBLOCKD *const xd = &x->e_mbd;
2510*77c1e3ccSAndroid Build Coastguard Worker   bool do_split =
2511*77c1e3ccSAndroid Build Coastguard Worker       (cpi->sf.rt_sf.nonrd_check_partition_merge_mode == 3)
2512*77c1e3ccSAndroid Build Coastguard Worker           ? (bsize <= BLOCK_32X32 || (is_larger_qindex && bsize <= BLOCK_64X64))
2513*77c1e3ccSAndroid Build Coastguard Worker           : true;
2514*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN ||
2515*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.rt_sf.nonrd_check_partition_merge_mode < 2 ||
2516*77c1e3ccSAndroid Build Coastguard Worker       cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id) ||
2517*77c1e3ccSAndroid Build Coastguard Worker       !none_rdc->skip_txfm)
2518*77c1e3ccSAndroid Build Coastguard Worker     return do_split;
2519*77c1e3ccSAndroid Build Coastguard Worker 
2520*77c1e3ccSAndroid Build Coastguard Worker   const int use_model_yrd_large = get_model_rd_flag(cpi, xd, bsize);
2521*77c1e3ccSAndroid Build Coastguard Worker 
2522*77c1e3ccSAndroid Build Coastguard Worker   // When model based skip is not used (i.e.,use_model_yrd_large = 0), skip_txfm
2523*77c1e3ccSAndroid Build Coastguard Worker   // would have been populated based on Hadamard transform and skip_txfm flag is
2524*77c1e3ccSAndroid Build Coastguard Worker   // more reliable. Hence SPLIT evaluation is disabled at all quantizers for 8x8
2525*77c1e3ccSAndroid Build Coastguard Worker   // and 16x16 blocks.
2526*77c1e3ccSAndroid Build Coastguard Worker   // When model based skip is used (i.e.,use_model_yrd_large = 1), skip_txfm may
2527*77c1e3ccSAndroid Build Coastguard Worker   // not be reliable. Hence SPLIT evaluation is disabled only at lower
2528*77c1e3ccSAndroid Build Coastguard Worker   // quantizers for blocks >= 32x32.
2529*77c1e3ccSAndroid Build Coastguard Worker   if ((!use_model_yrd_large) || (!is_larger_qindex)) return false;
2530*77c1e3ccSAndroid Build Coastguard Worker 
2531*77c1e3ccSAndroid Build Coastguard Worker   // Use residual statistics to decide if SPLIT partition should be evaluated
2532*77c1e3ccSAndroid Build Coastguard Worker   // for 32x32 blocks. The pruning logic is avoided for larger block size to
2533*77c1e3ccSAndroid Build Coastguard Worker   // avoid the visual artifacts
2534*77c1e3ccSAndroid Build Coastguard Worker   if (pc_tree->none->mic.mode == NEWMV && bsize == BLOCK_32X32 && do_split) {
2535*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE subsize = get_partition_subsize(bsize, partition);
2536*77c1e3ccSAndroid Build Coastguard Worker     assert(subsize < BLOCK_SIZES_ALL);
2537*77c1e3ccSAndroid Build Coastguard Worker     double min_per_pixel_error = DBL_MAX;
2538*77c1e3ccSAndroid Build Coastguard Worker     double max_per_pixel_error = 0.;
2539*77c1e3ccSAndroid Build Coastguard Worker     int i;
2540*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < SUB_PARTITIONS_SPLIT; i++) {
2541*77c1e3ccSAndroid Build Coastguard Worker       const int x_idx = (i & 1) * hbs;
2542*77c1e3ccSAndroid Build Coastguard Worker       const int y_idx = (i >> 1) * hbs;
2543*77c1e3ccSAndroid Build Coastguard Worker       if ((mi_row + y_idx >= mi_params->mi_rows) ||
2544*77c1e3ccSAndroid Build Coastguard Worker           (mi_col + x_idx >= mi_params->mi_cols)) {
2545*77c1e3ccSAndroid Build Coastguard Worker         break;
2546*77c1e3ccSAndroid Build Coastguard Worker       }
2547*77c1e3ccSAndroid Build Coastguard Worker 
2548*77c1e3ccSAndroid Build Coastguard Worker       // Populate the appropriate buffer pointers.
2549*77c1e3ccSAndroid Build Coastguard Worker       // Pass scale factors as NULL as the base pointer of the block would have
2550*77c1e3ccSAndroid Build Coastguard Worker       // been calculated appropriately.
2551*77c1e3ccSAndroid Build Coastguard Worker       struct buf_2d src_split_buf_2d, pred_split_buf_2d;
2552*77c1e3ccSAndroid Build Coastguard Worker       const struct buf_2d *src_none_buf_2d = &x->plane[AOM_PLANE_Y].src;
2553*77c1e3ccSAndroid Build Coastguard Worker       setup_pred_plane(&src_split_buf_2d, subsize, src_none_buf_2d->buf,
2554*77c1e3ccSAndroid Build Coastguard Worker                        src_none_buf_2d->width, src_none_buf_2d->height,
2555*77c1e3ccSAndroid Build Coastguard Worker                        src_none_buf_2d->stride, y_idx, x_idx, NULL, 0, 0);
2556*77c1e3ccSAndroid Build Coastguard Worker       const struct buf_2d *pred_none_buf_2d = &xd->plane[AOM_PLANE_Y].dst;
2557*77c1e3ccSAndroid Build Coastguard Worker       setup_pred_plane(&pred_split_buf_2d, subsize, pred_none_buf_2d->buf,
2558*77c1e3ccSAndroid Build Coastguard Worker                        pred_none_buf_2d->width, pred_none_buf_2d->height,
2559*77c1e3ccSAndroid Build Coastguard Worker                        pred_none_buf_2d->stride, y_idx, x_idx, NULL, 0, 0);
2560*77c1e3ccSAndroid Build Coastguard Worker 
2561*77c1e3ccSAndroid Build Coastguard Worker       unsigned int curr_uint_mse;
2562*77c1e3ccSAndroid Build Coastguard Worker       const unsigned int curr_uint_var = cpi->ppi->fn_ptr[subsize].vf(
2563*77c1e3ccSAndroid Build Coastguard Worker           src_split_buf_2d.buf, src_split_buf_2d.stride, pred_split_buf_2d.buf,
2564*77c1e3ccSAndroid Build Coastguard Worker           pred_split_buf_2d.stride, &curr_uint_mse);
2565*77c1e3ccSAndroid Build Coastguard Worker       const double curr_per_pixel_error =
2566*77c1e3ccSAndroid Build Coastguard Worker           sqrt((double)curr_uint_var / block_size_wide[subsize] /
2567*77c1e3ccSAndroid Build Coastguard Worker                block_size_high[subsize]);
2568*77c1e3ccSAndroid Build Coastguard Worker       if (curr_per_pixel_error < min_per_pixel_error)
2569*77c1e3ccSAndroid Build Coastguard Worker         min_per_pixel_error = curr_per_pixel_error;
2570*77c1e3ccSAndroid Build Coastguard Worker       if (curr_per_pixel_error > max_per_pixel_error)
2571*77c1e3ccSAndroid Build Coastguard Worker         max_per_pixel_error = curr_per_pixel_error;
2572*77c1e3ccSAndroid Build Coastguard Worker     }
2573*77c1e3ccSAndroid Build Coastguard Worker 
2574*77c1e3ccSAndroid Build Coastguard Worker     // Prune based on residual statistics only if all the sub-partitions are
2575*77c1e3ccSAndroid Build Coastguard Worker     // valid.
2576*77c1e3ccSAndroid Build Coastguard Worker     if (i == SUB_PARTITIONS_SPLIT) {
2577*77c1e3ccSAndroid Build Coastguard Worker       if (max_per_pixel_error - min_per_pixel_error <= 1.5) do_split = false;
2578*77c1e3ccSAndroid Build Coastguard Worker     }
2579*77c1e3ccSAndroid Build Coastguard Worker   }
2580*77c1e3ccSAndroid Build Coastguard Worker 
2581*77c1e3ccSAndroid Build Coastguard Worker   return do_split;
2582*77c1e3ccSAndroid Build Coastguard Worker }
2583*77c1e3ccSAndroid Build Coastguard Worker 
try_merge(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,MB_MODE_INFO ** mib,TokenExtra ** tp,const int mi_row,const int mi_col,const BLOCK_SIZE bsize,PC_TREE * const pc_tree,const PARTITION_TYPE partition,const BLOCK_SIZE subsize,const int pl)2584*77c1e3ccSAndroid Build Coastguard Worker static void try_merge(AV1_COMP *const cpi, ThreadData *td,
2585*77c1e3ccSAndroid Build Coastguard Worker                       TileDataEnc *tile_data, MB_MODE_INFO **mib,
2586*77c1e3ccSAndroid Build Coastguard Worker                       TokenExtra **tp, const int mi_row, const int mi_col,
2587*77c1e3ccSAndroid Build Coastguard Worker                       const BLOCK_SIZE bsize, PC_TREE *const pc_tree,
2588*77c1e3ccSAndroid Build Coastguard Worker                       const PARTITION_TYPE partition, const BLOCK_SIZE subsize,
2589*77c1e3ccSAndroid Build Coastguard Worker                       const int pl) {
2590*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2591*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
2592*77c1e3ccSAndroid Build Coastguard Worker   TileInfo *const tile_info = &tile_data->tile_info;
2593*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
2594*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
2595*77c1e3ccSAndroid Build Coastguard Worker   const ModeCosts *mode_costs = &x->mode_costs;
2596*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
2597*77c1e3ccSAndroid Build Coastguard Worker   // Only square blocks from 8x8 to 128x128 are supported
2598*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize >= BLOCK_8X8 && bsize <= BLOCK_128X128);
2599*77c1e3ccSAndroid Build Coastguard Worker   const int bs = mi_size_wide[bsize];
2600*77c1e3ccSAndroid Build Coastguard Worker   const int hbs = bs / 2;
2601*77c1e3ccSAndroid Build Coastguard Worker   bool do_split = false;
2602*77c1e3ccSAndroid Build Coastguard Worker   RD_SEARCH_MACROBLOCK_CONTEXT x_ctx;
2603*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS split_rdc, none_rdc;
2604*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&split_rdc);
2605*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&none_rdc);
2606*77c1e3ccSAndroid Build Coastguard Worker   av1_save_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
2607*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context =
2608*77c1e3ccSAndroid Build Coastguard Worker       cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
2609*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
2610*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
2611*77c1e3ccSAndroid Build Coastguard Worker   pc_tree->partitioning = PARTITION_NONE;
2612*77c1e3ccSAndroid Build Coastguard Worker   if (!pc_tree->none) {
2613*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->none = av1_alloc_pmc(cpi, bsize, &td->shared_coeff_buf);
2614*77c1e3ccSAndroid Build Coastguard Worker     if (!pc_tree->none)
2615*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
2616*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PICK_MODE_CONTEXT");
2617*77c1e3ccSAndroid Build Coastguard Worker   } else {
2618*77c1e3ccSAndroid Build Coastguard Worker     av1_reset_pmc(pc_tree->none);
2619*77c1e3ccSAndroid Build Coastguard Worker   }
2620*77c1e3ccSAndroid Build Coastguard Worker   pick_sb_modes_nonrd(cpi, tile_data, x, mi_row, mi_col, &none_rdc, bsize,
2621*77c1e3ccSAndroid Build Coastguard Worker                       pc_tree->none);
2622*77c1e3ccSAndroid Build Coastguard Worker   none_rdc.rate += mode_costs->partition_cost[pl][PARTITION_NONE];
2623*77c1e3ccSAndroid Build Coastguard Worker   none_rdc.rdcost = RDCOST(x->rdmult, none_rdc.rate, none_rdc.dist);
2624*77c1e3ccSAndroid Build Coastguard Worker   av1_restore_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
2625*77c1e3ccSAndroid Build Coastguard Worker 
2626*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.rt_sf.nonrd_check_partition_merge_mode < 2 ||
2627*77c1e3ccSAndroid Build Coastguard Worker       none_rdc.skip_txfm != 1 || pc_tree->none->mic.mode == NEWMV) {
2628*77c1e3ccSAndroid Build Coastguard Worker     do_split = calc_do_split_flag(cpi, x, pc_tree, &none_rdc, mi_params, mi_row,
2629*77c1e3ccSAndroid Build Coastguard Worker                                   mi_col, hbs, bsize, partition);
2630*77c1e3ccSAndroid Build Coastguard Worker     if (do_split) {
2631*77c1e3ccSAndroid Build Coastguard Worker       av1_init_rd_stats(&split_rdc);
2632*77c1e3ccSAndroid Build Coastguard Worker       split_rdc.rate += mode_costs->partition_cost[pl][PARTITION_SPLIT];
2633*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < SUB_PARTITIONS_SPLIT; i++) {
2634*77c1e3ccSAndroid Build Coastguard Worker         RD_STATS block_rdc;
2635*77c1e3ccSAndroid Build Coastguard Worker         av1_invalid_rd_stats(&block_rdc);
2636*77c1e3ccSAndroid Build Coastguard Worker         int x_idx = (i & 1) * hbs;
2637*77c1e3ccSAndroid Build Coastguard Worker         int y_idx = (i >> 1) * hbs;
2638*77c1e3ccSAndroid Build Coastguard Worker         if ((mi_row + y_idx >= mi_params->mi_rows) ||
2639*77c1e3ccSAndroid Build Coastguard Worker             (mi_col + x_idx >= mi_params->mi_cols))
2640*77c1e3ccSAndroid Build Coastguard Worker           continue;
2641*77c1e3ccSAndroid Build Coastguard Worker         xd->above_txfm_context =
2642*77c1e3ccSAndroid Build Coastguard Worker             cm->above_contexts.txfm[tile_info->tile_row] + mi_col + x_idx;
2643*77c1e3ccSAndroid Build Coastguard Worker         xd->left_txfm_context =
2644*77c1e3ccSAndroid Build Coastguard Worker             xd->left_txfm_context_buffer + ((mi_row + y_idx) & MAX_MIB_MASK);
2645*77c1e3ccSAndroid Build Coastguard Worker         if (!pc_tree->split[i]->none) {
2646*77c1e3ccSAndroid Build Coastguard Worker           pc_tree->split[i]->none =
2647*77c1e3ccSAndroid Build Coastguard Worker               av1_alloc_pmc(cpi, subsize, &td->shared_coeff_buf);
2648*77c1e3ccSAndroid Build Coastguard Worker           if (!pc_tree->split[i]->none)
2649*77c1e3ccSAndroid Build Coastguard Worker             aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
2650*77c1e3ccSAndroid Build Coastguard Worker                                "Failed to allocate PICK_MODE_CONTEXT");
2651*77c1e3ccSAndroid Build Coastguard Worker         } else {
2652*77c1e3ccSAndroid Build Coastguard Worker           av1_reset_pmc(pc_tree->split[i]->none);
2653*77c1e3ccSAndroid Build Coastguard Worker         }
2654*77c1e3ccSAndroid Build Coastguard Worker         pc_tree->split[i]->partitioning = PARTITION_NONE;
2655*77c1e3ccSAndroid Build Coastguard Worker         pick_sb_modes_nonrd(cpi, tile_data, x, mi_row + y_idx, mi_col + x_idx,
2656*77c1e3ccSAndroid Build Coastguard Worker                             &block_rdc, subsize, pc_tree->split[i]->none);
2657*77c1e3ccSAndroid Build Coastguard Worker         // TODO(yunqingwang): The rate here did not include the cost of
2658*77c1e3ccSAndroid Build Coastguard Worker         // signaling PARTITION_NONE token in the sub-blocks.
2659*77c1e3ccSAndroid Build Coastguard Worker         split_rdc.rate += block_rdc.rate;
2660*77c1e3ccSAndroid Build Coastguard Worker         split_rdc.dist += block_rdc.dist;
2661*77c1e3ccSAndroid Build Coastguard Worker 
2662*77c1e3ccSAndroid Build Coastguard Worker         av1_rd_cost_update(x->rdmult, &split_rdc);
2663*77c1e3ccSAndroid Build Coastguard Worker 
2664*77c1e3ccSAndroid Build Coastguard Worker         if (none_rdc.rdcost < split_rdc.rdcost) {
2665*77c1e3ccSAndroid Build Coastguard Worker           break;
2666*77c1e3ccSAndroid Build Coastguard Worker         }
2667*77c1e3ccSAndroid Build Coastguard Worker 
2668*77c1e3ccSAndroid Build Coastguard Worker         if (i != SUB_PARTITIONS_SPLIT - 1)
2669*77c1e3ccSAndroid Build Coastguard Worker           encode_b_nonrd(cpi, tile_data, td, tp, mi_row + y_idx, mi_col + x_idx,
2670*77c1e3ccSAndroid Build Coastguard Worker                          1, subsize, PARTITION_NONE, pc_tree->split[i]->none,
2671*77c1e3ccSAndroid Build Coastguard Worker                          NULL);
2672*77c1e3ccSAndroid Build Coastguard Worker       }
2673*77c1e3ccSAndroid Build Coastguard Worker       av1_restore_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
2674*77c1e3ccSAndroid Build Coastguard Worker       split_rdc.rdcost = RDCOST(x->rdmult, split_rdc.rate, split_rdc.dist);
2675*77c1e3ccSAndroid Build Coastguard Worker     }
2676*77c1e3ccSAndroid Build Coastguard Worker   }
2677*77c1e3ccSAndroid Build Coastguard Worker 
2678*77c1e3ccSAndroid Build Coastguard Worker   if (none_rdc.rdcost < split_rdc.rdcost) {
2679*77c1e3ccSAndroid Build Coastguard Worker     /* Predicted samples can not be reused for PARTITION_NONE since same
2680*77c1e3ccSAndroid Build Coastguard Worker      * buffer is being used to store the reconstructed samples of
2681*77c1e3ccSAndroid Build Coastguard Worker      * PARTITION_SPLIT block. */
2682*77c1e3ccSAndroid Build Coastguard Worker     if (do_split) x->reuse_inter_pred = false;
2683*77c1e3ccSAndroid Build Coastguard Worker 
2684*77c1e3ccSAndroid Build Coastguard Worker     mib[0]->bsize = bsize;
2685*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->partitioning = PARTITION_NONE;
2686*77c1e3ccSAndroid Build Coastguard Worker     encode_b_nonrd(cpi, tile_data, td, tp, mi_row, mi_col, 0, bsize, partition,
2687*77c1e3ccSAndroid Build Coastguard Worker                    pc_tree->none, NULL);
2688*77c1e3ccSAndroid Build Coastguard Worker   } else {
2689*77c1e3ccSAndroid Build Coastguard Worker     mib[0]->bsize = subsize;
2690*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->partitioning = PARTITION_SPLIT;
2691*77c1e3ccSAndroid Build Coastguard Worker     /* Predicted samples can not be reused for PARTITION_SPLIT since same
2692*77c1e3ccSAndroid Build Coastguard Worker      * buffer is being used to write the reconstructed samples. */
2693*77c1e3ccSAndroid Build Coastguard Worker     // TODO(Cherma): Store and reuse predicted samples generated by
2694*77c1e3ccSAndroid Build Coastguard Worker     // encode_b_nonrd() in DRY_RUN_NORMAL mode.
2695*77c1e3ccSAndroid Build Coastguard Worker     x->reuse_inter_pred = false;
2696*77c1e3ccSAndroid Build Coastguard Worker 
2697*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < SUB_PARTITIONS_SPLIT; i++) {
2698*77c1e3ccSAndroid Build Coastguard Worker       int x_idx = (i & 1) * hbs;
2699*77c1e3ccSAndroid Build Coastguard Worker       int y_idx = (i >> 1) * hbs;
2700*77c1e3ccSAndroid Build Coastguard Worker       if ((mi_row + y_idx >= mi_params->mi_rows) ||
2701*77c1e3ccSAndroid Build Coastguard Worker           (mi_col + x_idx >= mi_params->mi_cols))
2702*77c1e3ccSAndroid Build Coastguard Worker         continue;
2703*77c1e3ccSAndroid Build Coastguard Worker 
2704*77c1e3ccSAndroid Build Coastguard Worker       // Note: We don't reset pc_tree->split[i]->none here because it
2705*77c1e3ccSAndroid Build Coastguard Worker       // could contain results from the additional check. Instead, it is
2706*77c1e3ccSAndroid Build Coastguard Worker       // reset before we enter the nonrd_check_partition_merge_mode
2707*77c1e3ccSAndroid Build Coastguard Worker       // condition.
2708*77c1e3ccSAndroid Build Coastguard Worker       if (!pc_tree->split[i]->none) {
2709*77c1e3ccSAndroid Build Coastguard Worker         pc_tree->split[i]->none =
2710*77c1e3ccSAndroid Build Coastguard Worker             av1_alloc_pmc(cpi, subsize, &td->shared_coeff_buf);
2711*77c1e3ccSAndroid Build Coastguard Worker         if (!pc_tree->split[i]->none)
2712*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
2713*77c1e3ccSAndroid Build Coastguard Worker                              "Failed to allocate PICK_MODE_CONTEXT");
2714*77c1e3ccSAndroid Build Coastguard Worker       }
2715*77c1e3ccSAndroid Build Coastguard Worker       encode_b_nonrd(cpi, tile_data, td, tp, mi_row + y_idx, mi_col + x_idx, 0,
2716*77c1e3ccSAndroid Build Coastguard Worker                      subsize, PARTITION_NONE, pc_tree->split[i]->none, NULL);
2717*77c1e3ccSAndroid Build Coastguard Worker     }
2718*77c1e3ccSAndroid Build Coastguard Worker   }
2719*77c1e3ccSAndroid Build Coastguard Worker }
2720*77c1e3ccSAndroid Build Coastguard Worker 
2721*77c1e3ccSAndroid Build Coastguard Worker // Evaluate if the sub-partitions can be merged directly into a large partition
2722*77c1e3ccSAndroid Build Coastguard Worker // without calculating the RD cost.
direct_partition_merging(AV1_COMP * cpi,ThreadData * td,TileDataEnc * tile_data,MB_MODE_INFO ** mib,int mi_row,int mi_col,BLOCK_SIZE bsize)2723*77c1e3ccSAndroid Build Coastguard Worker static void direct_partition_merging(AV1_COMP *cpi, ThreadData *td,
2724*77c1e3ccSAndroid Build Coastguard Worker                                      TileDataEnc *tile_data, MB_MODE_INFO **mib,
2725*77c1e3ccSAndroid Build Coastguard Worker                                      int mi_row, int mi_col, BLOCK_SIZE bsize) {
2726*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2727*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
2728*77c1e3ccSAndroid Build Coastguard Worker   TileInfo *const tile_info = &tile_data->tile_info;
2729*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
2730*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
2731*77c1e3ccSAndroid Build Coastguard Worker   const int bs = mi_size_wide[bsize];
2732*77c1e3ccSAndroid Build Coastguard Worker   const int hbs = bs / 2;
2733*77c1e3ccSAndroid Build Coastguard Worker   const PARTITION_TYPE partition =
2734*77c1e3ccSAndroid Build Coastguard Worker       (bsize >= BLOCK_8X8) ? get_partition(cm, mi_row, mi_col, bsize)
2735*77c1e3ccSAndroid Build Coastguard Worker                            : PARTITION_NONE;
2736*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE subsize = get_partition_subsize(bsize, partition);
2737*77c1e3ccSAndroid Build Coastguard Worker 
2738*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO **b0 = mib;
2739*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO **b1 = mib + hbs;
2740*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO **b2 = mib + hbs * mi_params->mi_stride;
2741*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO **b3 = mib + hbs * mi_params->mi_stride + hbs;
2742*77c1e3ccSAndroid Build Coastguard Worker 
2743*77c1e3ccSAndroid Build Coastguard Worker   // Check if the following conditions are met. This can be updated
2744*77c1e3ccSAndroid Build Coastguard Worker   // later with more support added.
2745*77c1e3ccSAndroid Build Coastguard Worker   const int further_split = b0[0]->bsize < subsize || b1[0]->bsize < subsize ||
2746*77c1e3ccSAndroid Build Coastguard Worker                             b2[0]->bsize < subsize || b3[0]->bsize < subsize;
2747*77c1e3ccSAndroid Build Coastguard Worker   if (further_split) return;
2748*77c1e3ccSAndroid Build Coastguard Worker 
2749*77c1e3ccSAndroid Build Coastguard Worker   const int no_skip = !b0[0]->skip_txfm || !b1[0]->skip_txfm ||
2750*77c1e3ccSAndroid Build Coastguard Worker                       !b2[0]->skip_txfm || !b3[0]->skip_txfm;
2751*77c1e3ccSAndroid Build Coastguard Worker   if (no_skip) return;
2752*77c1e3ccSAndroid Build Coastguard Worker 
2753*77c1e3ccSAndroid Build Coastguard Worker   const int compound = (b0[0]->ref_frame[1] != b1[0]->ref_frame[1] ||
2754*77c1e3ccSAndroid Build Coastguard Worker                         b0[0]->ref_frame[1] != b2[0]->ref_frame[1] ||
2755*77c1e3ccSAndroid Build Coastguard Worker                         b0[0]->ref_frame[1] != b3[0]->ref_frame[1] ||
2756*77c1e3ccSAndroid Build Coastguard Worker                         b0[0]->ref_frame[1] > NONE_FRAME);
2757*77c1e3ccSAndroid Build Coastguard Worker   if (compound) return;
2758*77c1e3ccSAndroid Build Coastguard Worker 
2759*77c1e3ccSAndroid Build Coastguard Worker   // Intra modes aren't considered here.
2760*77c1e3ccSAndroid Build Coastguard Worker   const int different_ref = (b0[0]->ref_frame[0] != b1[0]->ref_frame[0] ||
2761*77c1e3ccSAndroid Build Coastguard Worker                              b0[0]->ref_frame[0] != b2[0]->ref_frame[0] ||
2762*77c1e3ccSAndroid Build Coastguard Worker                              b0[0]->ref_frame[0] != b3[0]->ref_frame[0] ||
2763*77c1e3ccSAndroid Build Coastguard Worker                              b0[0]->ref_frame[0] <= INTRA_FRAME);
2764*77c1e3ccSAndroid Build Coastguard Worker   if (different_ref) return;
2765*77c1e3ccSAndroid Build Coastguard Worker 
2766*77c1e3ccSAndroid Build Coastguard Worker   const int different_mode =
2767*77c1e3ccSAndroid Build Coastguard Worker       (b0[0]->mode != b1[0]->mode || b0[0]->mode != b2[0]->mode ||
2768*77c1e3ccSAndroid Build Coastguard Worker        b0[0]->mode != b3[0]->mode);
2769*77c1e3ccSAndroid Build Coastguard Worker   if (different_mode) return;
2770*77c1e3ccSAndroid Build Coastguard Worker 
2771*77c1e3ccSAndroid Build Coastguard Worker   const int unsupported_mode =
2772*77c1e3ccSAndroid Build Coastguard Worker       (b0[0]->mode != NEARESTMV && b0[0]->mode != GLOBALMV);
2773*77c1e3ccSAndroid Build Coastguard Worker   if (unsupported_mode) return;
2774*77c1e3ccSAndroid Build Coastguard Worker 
2775*77c1e3ccSAndroid Build Coastguard Worker   const int different_mv = (b0[0]->mv[0].as_int != b1[0]->mv[0].as_int ||
2776*77c1e3ccSAndroid Build Coastguard Worker                             b0[0]->mv[0].as_int != b2[0]->mv[0].as_int ||
2777*77c1e3ccSAndroid Build Coastguard Worker                             b0[0]->mv[0].as_int != b3[0]->mv[0].as_int);
2778*77c1e3ccSAndroid Build Coastguard Worker   if (different_mv) return;
2779*77c1e3ccSAndroid Build Coastguard Worker 
2780*77c1e3ccSAndroid Build Coastguard Worker   const int unsupported_motion_mode =
2781*77c1e3ccSAndroid Build Coastguard Worker       (b0[0]->motion_mode != b1[0]->motion_mode ||
2782*77c1e3ccSAndroid Build Coastguard Worker        b0[0]->motion_mode != b2[0]->motion_mode ||
2783*77c1e3ccSAndroid Build Coastguard Worker        b0[0]->motion_mode != b3[0]->motion_mode ||
2784*77c1e3ccSAndroid Build Coastguard Worker        b0[0]->motion_mode != SIMPLE_TRANSLATION);
2785*77c1e3ccSAndroid Build Coastguard Worker   if (unsupported_motion_mode) return;
2786*77c1e3ccSAndroid Build Coastguard Worker 
2787*77c1e3ccSAndroid Build Coastguard Worker   const int diffent_filter =
2788*77c1e3ccSAndroid Build Coastguard Worker       (b0[0]->interp_filters.as_int != b1[0]->interp_filters.as_int ||
2789*77c1e3ccSAndroid Build Coastguard Worker        b0[0]->interp_filters.as_int != b2[0]->interp_filters.as_int ||
2790*77c1e3ccSAndroid Build Coastguard Worker        b0[0]->interp_filters.as_int != b3[0]->interp_filters.as_int);
2791*77c1e3ccSAndroid Build Coastguard Worker   if (diffent_filter) return;
2792*77c1e3ccSAndroid Build Coastguard Worker 
2793*77c1e3ccSAndroid Build Coastguard Worker   const int different_seg = (b0[0]->segment_id != b1[0]->segment_id ||
2794*77c1e3ccSAndroid Build Coastguard Worker                              b0[0]->segment_id != b2[0]->segment_id ||
2795*77c1e3ccSAndroid Build Coastguard Worker                              b0[0]->segment_id != b3[0]->segment_id);
2796*77c1e3ccSAndroid Build Coastguard Worker   if (different_seg) return;
2797*77c1e3ccSAndroid Build Coastguard Worker 
2798*77c1e3ccSAndroid Build Coastguard Worker   // Evaluate the ref_mv.
2799*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO **this_mi = mib;
2800*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE orig_bsize = this_mi[0]->bsize;
2801*77c1e3ccSAndroid Build Coastguard Worker   const PARTITION_TYPE orig_partition = this_mi[0]->partition;
2802*77c1e3ccSAndroid Build Coastguard Worker 
2803*77c1e3ccSAndroid Build Coastguard Worker   this_mi[0]->bsize = bsize;
2804*77c1e3ccSAndroid Build Coastguard Worker   this_mi[0]->partition = PARTITION_NONE;
2805*77c1e3ccSAndroid Build Coastguard Worker   this_mi[0]->skip_txfm = 1;
2806*77c1e3ccSAndroid Build Coastguard Worker 
2807*77c1e3ccSAndroid Build Coastguard Worker   // TODO(yunqing): functions called below can be optimized by
2808*77c1e3ccSAndroid Build Coastguard Worker   // removing unrelated operations.
2809*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets_without_segment_id(cpi, &tile_data->tile_info, x, mi_row,
2810*77c1e3ccSAndroid Build Coastguard Worker                                      mi_col, bsize);
2811*77c1e3ccSAndroid Build Coastguard Worker 
2812*77c1e3ccSAndroid Build Coastguard Worker   const MV_REFERENCE_FRAME ref_frame = this_mi[0]->ref_frame[0];
2813*77c1e3ccSAndroid Build Coastguard Worker   int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES];
2814*77c1e3ccSAndroid Build Coastguard Worker   struct buf_2d yv12_mb[REF_FRAMES][MAX_MB_PLANE];
2815*77c1e3ccSAndroid Build Coastguard Worker   int force_skip_low_temp_var = 0;
2816*77c1e3ccSAndroid Build Coastguard Worker   int skip_pred_mv = 0;
2817*77c1e3ccSAndroid Build Coastguard Worker   bool use_scaled_ref;
2818*77c1e3ccSAndroid Build Coastguard Worker 
2819*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < MB_MODE_COUNT; ++i) {
2820*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < REF_FRAMES; ++j) {
2821*77c1e3ccSAndroid Build Coastguard Worker       frame_mv[i][j].as_int = INVALID_MV;
2822*77c1e3ccSAndroid Build Coastguard Worker     }
2823*77c1e3ccSAndroid Build Coastguard Worker   }
2824*77c1e3ccSAndroid Build Coastguard Worker   av1_copy(x->color_sensitivity, x->color_sensitivity_sb);
2825*77c1e3ccSAndroid Build Coastguard Worker   skip_pred_mv = (x->nonrd_prune_ref_frame_search > 2 &&
2826*77c1e3ccSAndroid Build Coastguard Worker                   x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] != 2 &&
2827*77c1e3ccSAndroid Build Coastguard Worker                   x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] != 2);
2828*77c1e3ccSAndroid Build Coastguard Worker 
2829*77c1e3ccSAndroid Build Coastguard Worker   find_predictors(cpi, x, ref_frame, frame_mv, yv12_mb, bsize,
2830*77c1e3ccSAndroid Build Coastguard Worker                   force_skip_low_temp_var, skip_pred_mv, &use_scaled_ref);
2831*77c1e3ccSAndroid Build Coastguard Worker 
2832*77c1e3ccSAndroid Build Coastguard Worker   int continue_merging = 1;
2833*77c1e3ccSAndroid Build Coastguard Worker   if (frame_mv[NEARESTMV][ref_frame].as_mv.row != b0[0]->mv[0].as_mv.row ||
2834*77c1e3ccSAndroid Build Coastguard Worker       frame_mv[NEARESTMV][ref_frame].as_mv.col != b0[0]->mv[0].as_mv.col)
2835*77c1e3ccSAndroid Build Coastguard Worker     continue_merging = 0;
2836*77c1e3ccSAndroid Build Coastguard Worker 
2837*77c1e3ccSAndroid Build Coastguard Worker   if (!continue_merging) {
2838*77c1e3ccSAndroid Build Coastguard Worker     this_mi[0]->bsize = orig_bsize;
2839*77c1e3ccSAndroid Build Coastguard Worker     this_mi[0]->partition = orig_partition;
2840*77c1e3ccSAndroid Build Coastguard Worker 
2841*77c1e3ccSAndroid Build Coastguard Worker     // TODO(yunqing): Store the results and restore here instead of
2842*77c1e3ccSAndroid Build Coastguard Worker     // calling find_predictors() again.
2843*77c1e3ccSAndroid Build Coastguard Worker     av1_set_offsets_without_segment_id(cpi, &tile_data->tile_info, x, mi_row,
2844*77c1e3ccSAndroid Build Coastguard Worker                                        mi_col, this_mi[0]->bsize);
2845*77c1e3ccSAndroid Build Coastguard Worker     find_predictors(cpi, x, ref_frame, frame_mv, yv12_mb, this_mi[0]->bsize,
2846*77c1e3ccSAndroid Build Coastguard Worker                     force_skip_low_temp_var, skip_pred_mv, &use_scaled_ref);
2847*77c1e3ccSAndroid Build Coastguard Worker   } else {
2848*77c1e3ccSAndroid Build Coastguard Worker     struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
2849*77c1e3ccSAndroid Build Coastguard Worker     const int is_scaled = av1_is_scaled(sf);
2850*77c1e3ccSAndroid Build Coastguard Worker     const int is_y_subpel_mv = (abs(this_mi[0]->mv[0].as_mv.row) % 8) ||
2851*77c1e3ccSAndroid Build Coastguard Worker                                (abs(this_mi[0]->mv[0].as_mv.col) % 8);
2852*77c1e3ccSAndroid Build Coastguard Worker     const int is_uv_subpel_mv = (abs(this_mi[0]->mv[0].as_mv.row) % 16) ||
2853*77c1e3ccSAndroid Build Coastguard Worker                                 (abs(this_mi[0]->mv[0].as_mv.col) % 16);
2854*77c1e3ccSAndroid Build Coastguard Worker 
2855*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ppi->use_svc || is_scaled || is_y_subpel_mv || is_uv_subpel_mv) {
2856*77c1e3ccSAndroid Build Coastguard Worker       const int num_planes = av1_num_planes(cm);
2857*77c1e3ccSAndroid Build Coastguard Worker       set_ref_ptrs(cm, xd, ref_frame, this_mi[0]->ref_frame[1]);
2858*77c1e3ccSAndroid Build Coastguard Worker       const YV12_BUFFER_CONFIG *cfg = get_ref_frame_yv12_buf(cm, ref_frame);
2859*77c1e3ccSAndroid Build Coastguard Worker       av1_setup_pre_planes(xd, 0, cfg, mi_row, mi_col,
2860*77c1e3ccSAndroid Build Coastguard Worker                            xd->block_ref_scale_factors[0], num_planes);
2861*77c1e3ccSAndroid Build Coastguard Worker 
2862*77c1e3ccSAndroid Build Coastguard Worker       if (!cpi->ppi->use_svc && !is_scaled && !is_y_subpel_mv) {
2863*77c1e3ccSAndroid Build Coastguard Worker         assert(is_uv_subpel_mv == 1);
2864*77c1e3ccSAndroid Build Coastguard Worker         av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, 1,
2865*77c1e3ccSAndroid Build Coastguard Worker                                       num_planes - 1);
2866*77c1e3ccSAndroid Build Coastguard Worker       } else {
2867*77c1e3ccSAndroid Build Coastguard Worker         av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, 0,
2868*77c1e3ccSAndroid Build Coastguard Worker                                       num_planes - 1);
2869*77c1e3ccSAndroid Build Coastguard Worker       }
2870*77c1e3ccSAndroid Build Coastguard Worker     }
2871*77c1e3ccSAndroid Build Coastguard Worker 
2872*77c1e3ccSAndroid Build Coastguard Worker     // Copy out mbmi_ext information.
2873*77c1e3ccSAndroid Build Coastguard Worker     MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
2874*77c1e3ccSAndroid Build Coastguard Worker     MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame = x->mbmi_ext_frame;
2875*77c1e3ccSAndroid Build Coastguard Worker     av1_copy_mbmi_ext_to_mbmi_ext_frame(
2876*77c1e3ccSAndroid Build Coastguard Worker         mbmi_ext_frame, mbmi_ext, av1_ref_frame_type(this_mi[0]->ref_frame));
2877*77c1e3ccSAndroid Build Coastguard Worker 
2878*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE this_subsize =
2879*77c1e3ccSAndroid Build Coastguard Worker         get_partition_subsize(bsize, this_mi[0]->partition);
2880*77c1e3ccSAndroid Build Coastguard Worker     // Update partition contexts.
2881*77c1e3ccSAndroid Build Coastguard Worker     update_ext_partition_context(xd, mi_row, mi_col, this_subsize, bsize,
2882*77c1e3ccSAndroid Build Coastguard Worker                                  this_mi[0]->partition);
2883*77c1e3ccSAndroid Build Coastguard Worker 
2884*77c1e3ccSAndroid Build Coastguard Worker     const int num_planes = av1_num_planes(cm);
2885*77c1e3ccSAndroid Build Coastguard Worker     av1_reset_entropy_context(xd, bsize, num_planes);
2886*77c1e3ccSAndroid Build Coastguard Worker 
2887*77c1e3ccSAndroid Build Coastguard Worker     // Note: use x->txfm_search_params.tx_mode_search_type instead of
2888*77c1e3ccSAndroid Build Coastguard Worker     // cm->features.tx_mode here.
2889*77c1e3ccSAndroid Build Coastguard Worker     TX_SIZE tx_size =
2890*77c1e3ccSAndroid Build Coastguard Worker         tx_size_from_tx_mode(bsize, x->txfm_search_params.tx_mode_search_type);
2891*77c1e3ccSAndroid Build Coastguard Worker     if (xd->lossless[this_mi[0]->segment_id]) tx_size = TX_4X4;
2892*77c1e3ccSAndroid Build Coastguard Worker     this_mi[0]->tx_size = tx_size;
2893*77c1e3ccSAndroid Build Coastguard Worker     memset(this_mi[0]->inter_tx_size, this_mi[0]->tx_size,
2894*77c1e3ccSAndroid Build Coastguard Worker            sizeof(this_mi[0]->inter_tx_size));
2895*77c1e3ccSAndroid Build Coastguard Worker 
2896*77c1e3ccSAndroid Build Coastguard Worker     // Update txfm contexts.
2897*77c1e3ccSAndroid Build Coastguard Worker     xd->above_txfm_context =
2898*77c1e3ccSAndroid Build Coastguard Worker         cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
2899*77c1e3ccSAndroid Build Coastguard Worker     xd->left_txfm_context =
2900*77c1e3ccSAndroid Build Coastguard Worker         xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
2901*77c1e3ccSAndroid Build Coastguard Worker     set_txfm_ctxs(this_mi[0]->tx_size, xd->width, xd->height,
2902*77c1e3ccSAndroid Build Coastguard Worker                   this_mi[0]->skip_txfm && is_inter_block(this_mi[0]), xd);
2903*77c1e3ccSAndroid Build Coastguard Worker 
2904*77c1e3ccSAndroid Build Coastguard Worker     // Update mi for this partition block.
2905*77c1e3ccSAndroid Build Coastguard Worker     for (int y = 0; y < bs; y++) {
2906*77c1e3ccSAndroid Build Coastguard Worker       for (int x_idx = 0; x_idx < bs; x_idx++) {
2907*77c1e3ccSAndroid Build Coastguard Worker         this_mi[x_idx + y * mi_params->mi_stride] = this_mi[0];
2908*77c1e3ccSAndroid Build Coastguard Worker       }
2909*77c1e3ccSAndroid Build Coastguard Worker     }
2910*77c1e3ccSAndroid Build Coastguard Worker   }
2911*77c1e3ccSAndroid Build Coastguard Worker }
2912*77c1e3ccSAndroid Build Coastguard Worker 
2913*77c1e3ccSAndroid Build Coastguard Worker /*!\brief AV1 block partition application (minimal RD search).
2914*77c1e3ccSAndroid Build Coastguard Worker *
2915*77c1e3ccSAndroid Build Coastguard Worker * \ingroup partition_search
2916*77c1e3ccSAndroid Build Coastguard Worker * \callgraph
2917*77c1e3ccSAndroid Build Coastguard Worker * \callergraph
2918*77c1e3ccSAndroid Build Coastguard Worker * Encode the block by applying pre-calculated partition patterns that are
2919*77c1e3ccSAndroid Build Coastguard Worker * represented by coding block sizes stored in the mbmi array. The only
2920*77c1e3ccSAndroid Build Coastguard Worker * partition adjustment allowed is merging leaf split nodes if it leads to a
2921*77c1e3ccSAndroid Build Coastguard Worker * lower rd cost. The partition types are limited to a basic set: none, horz,
2922*77c1e3ccSAndroid Build Coastguard Worker * vert, and split. This function is only used in the real-time mode.
2923*77c1e3ccSAndroid Build Coastguard Worker *
2924*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    cpi       Top-level encoder structure
2925*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    td        Pointer to thread data
2926*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    tile_data Pointer to struct holding adaptive
2927*77c1e3ccSAndroid Build Coastguard Worker data/contexts/models for the tile during encoding
2928*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    mib       Array representing MB_MODE_INFO pointers for mi
2929*77c1e3ccSAndroid Build Coastguard Worker blocks starting from the first pixel of the current
2930*77c1e3ccSAndroid Build Coastguard Worker block
2931*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    tp        Pointer to the starting token
2932*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    mi_row    Row coordinate of the block in a step size of MI_SIZE
2933*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    mi_col    Column coordinate of the block in a step size of
2934*77c1e3ccSAndroid Build Coastguard Worker MI_SIZE
2935*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    bsize     Current block size
2936*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    pc_tree   Pointer to the PC_TREE node holding the picked
2937*77c1e3ccSAndroid Build Coastguard Worker partitions and mode info for the current block
2938*77c1e3ccSAndroid Build Coastguard Worker *
2939*77c1e3ccSAndroid Build Coastguard Worker * \remark Nothing is returned. The pc_tree struct is modified to store the
2940*77c1e3ccSAndroid Build Coastguard Worker * picked partition and modes.
2941*77c1e3ccSAndroid Build Coastguard Worker */
av1_nonrd_use_partition(AV1_COMP * cpi,ThreadData * td,TileDataEnc * tile_data,MB_MODE_INFO ** mib,TokenExtra ** tp,int mi_row,int mi_col,BLOCK_SIZE bsize,PC_TREE * pc_tree)2942*77c1e3ccSAndroid Build Coastguard Worker void av1_nonrd_use_partition(AV1_COMP *cpi, ThreadData *td,
2943*77c1e3ccSAndroid Build Coastguard Worker                              TileDataEnc *tile_data, MB_MODE_INFO **mib,
2944*77c1e3ccSAndroid Build Coastguard Worker                              TokenExtra **tp, int mi_row, int mi_col,
2945*77c1e3ccSAndroid Build Coastguard Worker                              BLOCK_SIZE bsize, PC_TREE *pc_tree) {
2946*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2947*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
2948*77c1e3ccSAndroid Build Coastguard Worker   TileInfo *const tile_info = &tile_data->tile_info;
2949*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
2950*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
2951*77c1e3ccSAndroid Build Coastguard Worker   const ModeCosts *mode_costs = &x->mode_costs;
2952*77c1e3ccSAndroid Build Coastguard Worker   // Only square blocks from 8x8 to 128x128 are supported
2953*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize >= BLOCK_8X8 && bsize <= BLOCK_128X128);
2954*77c1e3ccSAndroid Build Coastguard Worker   const int bs = mi_size_wide[bsize];
2955*77c1e3ccSAndroid Build Coastguard Worker   const int hbs = bs / 2;
2956*77c1e3ccSAndroid Build Coastguard Worker   PARTITION_TYPE partition = (bsize >= BLOCK_8X8)
2957*77c1e3ccSAndroid Build Coastguard Worker                                  ? get_partition(cm, mi_row, mi_col, bsize)
2958*77c1e3ccSAndroid Build Coastguard Worker                                  : PARTITION_NONE;
2959*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE subsize = get_partition_subsize(bsize, partition);
2960*77c1e3ccSAndroid Build Coastguard Worker   assert(subsize <= BLOCK_LARGEST);
2961*77c1e3ccSAndroid Build Coastguard Worker   const int pl = (bsize >= BLOCK_8X8)
2962*77c1e3ccSAndroid Build Coastguard Worker                      ? partition_plane_context(xd, mi_row, mi_col, bsize)
2963*77c1e3ccSAndroid Build Coastguard Worker                      : 0;
2964*77c1e3ccSAndroid Build Coastguard Worker 
2965*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS dummy_cost;
2966*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&dummy_cost);
2967*77c1e3ccSAndroid Build Coastguard Worker 
2968*77c1e3ccSAndroid Build Coastguard Worker   if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return;
2969*77c1e3ccSAndroid Build Coastguard Worker 
2970*77c1e3ccSAndroid Build Coastguard Worker   assert(mi_size_wide[bsize] == mi_size_high[bsize]);
2971*77c1e3ccSAndroid Build Coastguard Worker 
2972*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context =
2973*77c1e3ccSAndroid Build Coastguard Worker       cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
2974*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
2975*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
2976*77c1e3ccSAndroid Build Coastguard Worker 
2977*77c1e3ccSAndroid Build Coastguard Worker   // Initialize default mode evaluation params
2978*77c1e3ccSAndroid Build Coastguard Worker   set_mode_eval_params(cpi, x, DEFAULT_EVAL);
2979*77c1e3ccSAndroid Build Coastguard Worker 
2980*77c1e3ccSAndroid Build Coastguard Worker   x->reuse_inter_pred = cpi->sf.rt_sf.reuse_inter_pred_nonrd;
2981*77c1e3ccSAndroid Build Coastguard Worker 
2982*77c1e3ccSAndroid Build Coastguard Worker   int change_none_to_split = 0;
2983*77c1e3ccSAndroid Build Coastguard Worker   if (partition == PARTITION_NONE &&
2984*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.rt_sf.nonrd_check_partition_split == 1) {
2985*77c1e3ccSAndroid Build Coastguard Worker     change_none_to_split =
2986*77c1e3ccSAndroid Build Coastguard Worker         try_split_partition(cpi, td, tile_data, tile_info, tp, x, xd, mi_params,
2987*77c1e3ccSAndroid Build Coastguard Worker                             mi_row, mi_col, bsize, pl, pc_tree);
2988*77c1e3ccSAndroid Build Coastguard Worker     if (change_none_to_split) {
2989*77c1e3ccSAndroid Build Coastguard Worker       partition = PARTITION_SPLIT;
2990*77c1e3ccSAndroid Build Coastguard Worker       subsize = get_partition_subsize(bsize, partition);
2991*77c1e3ccSAndroid Build Coastguard Worker       assert(subsize <= BLOCK_LARGEST);
2992*77c1e3ccSAndroid Build Coastguard Worker     }
2993*77c1e3ccSAndroid Build Coastguard Worker   }
2994*77c1e3ccSAndroid Build Coastguard Worker 
2995*77c1e3ccSAndroid Build Coastguard Worker   pc_tree->partitioning = partition;
2996*77c1e3ccSAndroid Build Coastguard Worker 
2997*77c1e3ccSAndroid Build Coastguard Worker   switch (partition) {
2998*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_NONE:
2999*77c1e3ccSAndroid Build Coastguard Worker       if (!pc_tree->none) {
3000*77c1e3ccSAndroid Build Coastguard Worker         pc_tree->none = av1_alloc_pmc(cpi, bsize, &td->shared_coeff_buf);
3001*77c1e3ccSAndroid Build Coastguard Worker         if (!pc_tree->none)
3002*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
3003*77c1e3ccSAndroid Build Coastguard Worker                              "Failed to allocate PICK_MODE_CONTEXT");
3004*77c1e3ccSAndroid Build Coastguard Worker       } else {
3005*77c1e3ccSAndroid Build Coastguard Worker         av1_reset_pmc(pc_tree->none);
3006*77c1e3ccSAndroid Build Coastguard Worker       }
3007*77c1e3ccSAndroid Build Coastguard Worker       pick_sb_modes_nonrd(cpi, tile_data, x, mi_row, mi_col, &dummy_cost, bsize,
3008*77c1e3ccSAndroid Build Coastguard Worker                           pc_tree->none);
3009*77c1e3ccSAndroid Build Coastguard Worker       encode_b_nonrd(cpi, tile_data, td, tp, mi_row, mi_col, 0, bsize,
3010*77c1e3ccSAndroid Build Coastguard Worker                      partition, pc_tree->none, NULL);
3011*77c1e3ccSAndroid Build Coastguard Worker       break;
3012*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT:
3013*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
3014*77c1e3ccSAndroid Build Coastguard Worker         if (!pc_tree->vertical[i]) {
3015*77c1e3ccSAndroid Build Coastguard Worker           pc_tree->vertical[i] =
3016*77c1e3ccSAndroid Build Coastguard Worker               av1_alloc_pmc(cpi, subsize, &td->shared_coeff_buf);
3017*77c1e3ccSAndroid Build Coastguard Worker           if (!pc_tree->vertical[i])
3018*77c1e3ccSAndroid Build Coastguard Worker             aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
3019*77c1e3ccSAndroid Build Coastguard Worker                                "Failed to allocate PICK_MODE_CONTEXT");
3020*77c1e3ccSAndroid Build Coastguard Worker         } else {
3021*77c1e3ccSAndroid Build Coastguard Worker           av1_reset_pmc(pc_tree->vertical[i]);
3022*77c1e3ccSAndroid Build Coastguard Worker         }
3023*77c1e3ccSAndroid Build Coastguard Worker       }
3024*77c1e3ccSAndroid Build Coastguard Worker       pick_sb_modes_nonrd(cpi, tile_data, x, mi_row, mi_col, &dummy_cost,
3025*77c1e3ccSAndroid Build Coastguard Worker                           subsize, pc_tree->vertical[0]);
3026*77c1e3ccSAndroid Build Coastguard Worker       encode_b_nonrd(cpi, tile_data, td, tp, mi_row, mi_col, 0, subsize,
3027*77c1e3ccSAndroid Build Coastguard Worker                      PARTITION_VERT, pc_tree->vertical[0], NULL);
3028*77c1e3ccSAndroid Build Coastguard Worker       if (mi_col + hbs < mi_params->mi_cols && bsize > BLOCK_8X8) {
3029*77c1e3ccSAndroid Build Coastguard Worker         pick_sb_modes_nonrd(cpi, tile_data, x, mi_row, mi_col + hbs,
3030*77c1e3ccSAndroid Build Coastguard Worker                             &dummy_cost, subsize, pc_tree->vertical[1]);
3031*77c1e3ccSAndroid Build Coastguard Worker         encode_b_nonrd(cpi, tile_data, td, tp, mi_row, mi_col + hbs, 0, subsize,
3032*77c1e3ccSAndroid Build Coastguard Worker                        PARTITION_VERT, pc_tree->vertical[1], NULL);
3033*77c1e3ccSAndroid Build Coastguard Worker       }
3034*77c1e3ccSAndroid Build Coastguard Worker       break;
3035*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ:
3036*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
3037*77c1e3ccSAndroid Build Coastguard Worker         if (!pc_tree->horizontal[i]) {
3038*77c1e3ccSAndroid Build Coastguard Worker           pc_tree->horizontal[i] =
3039*77c1e3ccSAndroid Build Coastguard Worker               av1_alloc_pmc(cpi, subsize, &td->shared_coeff_buf);
3040*77c1e3ccSAndroid Build Coastguard Worker           if (!pc_tree->horizontal[i])
3041*77c1e3ccSAndroid Build Coastguard Worker             aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
3042*77c1e3ccSAndroid Build Coastguard Worker                                "Failed to allocate PICK_MODE_CONTEXT");
3043*77c1e3ccSAndroid Build Coastguard Worker         } else {
3044*77c1e3ccSAndroid Build Coastguard Worker           av1_reset_pmc(pc_tree->horizontal[i]);
3045*77c1e3ccSAndroid Build Coastguard Worker         }
3046*77c1e3ccSAndroid Build Coastguard Worker       }
3047*77c1e3ccSAndroid Build Coastguard Worker       pick_sb_modes_nonrd(cpi, tile_data, x, mi_row, mi_col, &dummy_cost,
3048*77c1e3ccSAndroid Build Coastguard Worker                           subsize, pc_tree->horizontal[0]);
3049*77c1e3ccSAndroid Build Coastguard Worker       encode_b_nonrd(cpi, tile_data, td, tp, mi_row, mi_col, 0, subsize,
3050*77c1e3ccSAndroid Build Coastguard Worker                      PARTITION_HORZ, pc_tree->horizontal[0], NULL);
3051*77c1e3ccSAndroid Build Coastguard Worker 
3052*77c1e3ccSAndroid Build Coastguard Worker       if (mi_row + hbs < mi_params->mi_rows && bsize > BLOCK_8X8) {
3053*77c1e3ccSAndroid Build Coastguard Worker         pick_sb_modes_nonrd(cpi, tile_data, x, mi_row + hbs, mi_col,
3054*77c1e3ccSAndroid Build Coastguard Worker                             &dummy_cost, subsize, pc_tree->horizontal[1]);
3055*77c1e3ccSAndroid Build Coastguard Worker         encode_b_nonrd(cpi, tile_data, td, tp, mi_row + hbs, mi_col, 0, subsize,
3056*77c1e3ccSAndroid Build Coastguard Worker                        PARTITION_HORZ, pc_tree->horizontal[1], NULL);
3057*77c1e3ccSAndroid Build Coastguard Worker       }
3058*77c1e3ccSAndroid Build Coastguard Worker       break;
3059*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_SPLIT:
3060*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
3061*77c1e3ccSAndroid Build Coastguard Worker         if (!pc_tree->split[i]) {
3062*77c1e3ccSAndroid Build Coastguard Worker           pc_tree->split[i] = av1_alloc_pc_tree_node(subsize);
3063*77c1e3ccSAndroid Build Coastguard Worker           if (!pc_tree->split[i])
3064*77c1e3ccSAndroid Build Coastguard Worker             aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
3065*77c1e3ccSAndroid Build Coastguard Worker                                "Failed to allocate PC_TREE");
3066*77c1e3ccSAndroid Build Coastguard Worker         }
3067*77c1e3ccSAndroid Build Coastguard Worker         pc_tree->split[i]->index = i;
3068*77c1e3ccSAndroid Build Coastguard Worker       }
3069*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->sf.rt_sf.nonrd_check_partition_merge_mode &&
3070*77c1e3ccSAndroid Build Coastguard Worker           av1_is_leaf_split_partition(cm, mi_row, mi_col, bsize) &&
3071*77c1e3ccSAndroid Build Coastguard Worker           !frame_is_intra_only(cm) && bsize <= BLOCK_64X64) {
3072*77c1e3ccSAndroid Build Coastguard Worker         try_merge(cpi, td, tile_data, mib, tp, mi_row, mi_col, bsize, pc_tree,
3073*77c1e3ccSAndroid Build Coastguard Worker                   partition, subsize, pl);
3074*77c1e3ccSAndroid Build Coastguard Worker       } else {
3075*77c1e3ccSAndroid Build Coastguard Worker         for (int i = 0; i < SUB_PARTITIONS_SPLIT; i++) {
3076*77c1e3ccSAndroid Build Coastguard Worker           int x_idx = (i & 1) * hbs;
3077*77c1e3ccSAndroid Build Coastguard Worker           int y_idx = (i >> 1) * hbs;
3078*77c1e3ccSAndroid Build Coastguard Worker           int jj = i >> 1, ii = i & 0x01;
3079*77c1e3ccSAndroid Build Coastguard Worker           if ((mi_row + y_idx >= mi_params->mi_rows) ||
3080*77c1e3ccSAndroid Build Coastguard Worker               (mi_col + x_idx >= mi_params->mi_cols))
3081*77c1e3ccSAndroid Build Coastguard Worker             continue;
3082*77c1e3ccSAndroid Build Coastguard Worker           av1_nonrd_use_partition(
3083*77c1e3ccSAndroid Build Coastguard Worker               cpi, td, tile_data,
3084*77c1e3ccSAndroid Build Coastguard Worker               mib + jj * hbs * mi_params->mi_stride + ii * hbs, tp,
3085*77c1e3ccSAndroid Build Coastguard Worker               mi_row + y_idx, mi_col + x_idx, subsize, pc_tree->split[i]);
3086*77c1e3ccSAndroid Build Coastguard Worker         }
3087*77c1e3ccSAndroid Build Coastguard Worker 
3088*77c1e3ccSAndroid Build Coastguard Worker         if (!change_none_to_split) {
3089*77c1e3ccSAndroid Build Coastguard Worker           // Note: Palette, cfl are not supported.
3090*77c1e3ccSAndroid Build Coastguard Worker           if (!frame_is_intra_only(cm) && !tile_data->allow_update_cdf &&
3091*77c1e3ccSAndroid Build Coastguard Worker               cpi->sf.rt_sf.partition_direct_merging &&
3092*77c1e3ccSAndroid Build Coastguard Worker               mode_costs->partition_cost[pl][PARTITION_NONE] <
3093*77c1e3ccSAndroid Build Coastguard Worker                   mode_costs->partition_cost[pl][PARTITION_SPLIT] &&
3094*77c1e3ccSAndroid Build Coastguard Worker               (mi_row + bs <= mi_params->mi_rows) &&
3095*77c1e3ccSAndroid Build Coastguard Worker               (mi_col + bs <= mi_params->mi_cols)) {
3096*77c1e3ccSAndroid Build Coastguard Worker             direct_partition_merging(cpi, td, tile_data, mib, mi_row, mi_col,
3097*77c1e3ccSAndroid Build Coastguard Worker                                      bsize);
3098*77c1e3ccSAndroid Build Coastguard Worker           }
3099*77c1e3ccSAndroid Build Coastguard Worker         }
3100*77c1e3ccSAndroid Build Coastguard Worker       }
3101*77c1e3ccSAndroid Build Coastguard Worker       break;
3102*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_A:
3103*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_B:
3104*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_A:
3105*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_B:
3106*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_4:
3107*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_4:
3108*77c1e3ccSAndroid Build Coastguard Worker       assert(0 && "Cannot handle extended partition types");
3109*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0); break;
3110*77c1e3ccSAndroid Build Coastguard Worker   }
3111*77c1e3ccSAndroid Build Coastguard Worker }
3112*77c1e3ccSAndroid Build Coastguard Worker 
3113*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
3114*77c1e3ccSAndroid Build Coastguard Worker // Try searching for an encoding for the given subblock. Returns zero if the
3115*77c1e3ccSAndroid Build Coastguard Worker // rdcost is already too high (to tell the caller not to bother searching for
3116*77c1e3ccSAndroid Build Coastguard Worker // encodings of further subblocks).
rd_try_subblock(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,int is_last,int mi_row,int mi_col,BLOCK_SIZE subsize,RD_STATS best_rdcost,RD_STATS * sum_rdc,PARTITION_TYPE partition,PICK_MODE_CONTEXT * this_ctx)3117*77c1e3ccSAndroid Build Coastguard Worker static int rd_try_subblock(AV1_COMP *const cpi, ThreadData *td,
3118*77c1e3ccSAndroid Build Coastguard Worker                            TileDataEnc *tile_data, TokenExtra **tp, int is_last,
3119*77c1e3ccSAndroid Build Coastguard Worker                            int mi_row, int mi_col, BLOCK_SIZE subsize,
3120*77c1e3ccSAndroid Build Coastguard Worker                            RD_STATS best_rdcost, RD_STATS *sum_rdc,
3121*77c1e3ccSAndroid Build Coastguard Worker                            PARTITION_TYPE partition,
3122*77c1e3ccSAndroid Build Coastguard Worker                            PICK_MODE_CONTEXT *this_ctx) {
3123*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
3124*77c1e3ccSAndroid Build Coastguard Worker   const int orig_mult = x->rdmult;
3125*77c1e3ccSAndroid Build Coastguard Worker   setup_block_rdmult(cpi, x, mi_row, mi_col, subsize, NO_AQ, NULL);
3126*77c1e3ccSAndroid Build Coastguard Worker 
3127*77c1e3ccSAndroid Build Coastguard Worker   av1_rd_cost_update(x->rdmult, &best_rdcost);
3128*77c1e3ccSAndroid Build Coastguard Worker 
3129*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS rdcost_remaining;
3130*77c1e3ccSAndroid Build Coastguard Worker   av1_rd_stats_subtraction(x->rdmult, &best_rdcost, sum_rdc, &rdcost_remaining);
3131*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS this_rdc;
3132*77c1e3ccSAndroid Build Coastguard Worker   pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &this_rdc, partition,
3133*77c1e3ccSAndroid Build Coastguard Worker                 subsize, this_ctx, rdcost_remaining);
3134*77c1e3ccSAndroid Build Coastguard Worker 
3135*77c1e3ccSAndroid Build Coastguard Worker   if (this_rdc.rate == INT_MAX) {
3136*77c1e3ccSAndroid Build Coastguard Worker     sum_rdc->rdcost = INT64_MAX;
3137*77c1e3ccSAndroid Build Coastguard Worker   } else {
3138*77c1e3ccSAndroid Build Coastguard Worker     sum_rdc->rate += this_rdc.rate;
3139*77c1e3ccSAndroid Build Coastguard Worker     sum_rdc->dist += this_rdc.dist;
3140*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_cost_update(x->rdmult, sum_rdc);
3141*77c1e3ccSAndroid Build Coastguard Worker   }
3142*77c1e3ccSAndroid Build Coastguard Worker 
3143*77c1e3ccSAndroid Build Coastguard Worker   if (sum_rdc->rdcost >= best_rdcost.rdcost) {
3144*77c1e3ccSAndroid Build Coastguard Worker     x->rdmult = orig_mult;
3145*77c1e3ccSAndroid Build Coastguard Worker     return 0;
3146*77c1e3ccSAndroid Build Coastguard Worker   }
3147*77c1e3ccSAndroid Build Coastguard Worker 
3148*77c1e3ccSAndroid Build Coastguard Worker   if (!is_last) {
3149*77c1e3ccSAndroid Build Coastguard Worker     av1_update_state(cpi, td, this_ctx, mi_row, mi_col, subsize, 1);
3150*77c1e3ccSAndroid Build Coastguard Worker     encode_superblock(cpi, tile_data, td, tp, DRY_RUN_NORMAL, subsize, NULL);
3151*77c1e3ccSAndroid Build Coastguard Worker   }
3152*77c1e3ccSAndroid Build Coastguard Worker 
3153*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult = orig_mult;
3154*77c1e3ccSAndroid Build Coastguard Worker   return 1;
3155*77c1e3ccSAndroid Build Coastguard Worker }
3156*77c1e3ccSAndroid Build Coastguard Worker 
3157*77c1e3ccSAndroid Build Coastguard Worker // Tests an AB partition, and updates the encoder status, the pick mode
3158*77c1e3ccSAndroid Build Coastguard Worker // contexts, the best rdcost, and the best partition.
rd_test_partition3(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,PC_TREE * pc_tree,RD_STATS * best_rdc,int64_t * this_rdcost,PICK_MODE_CONTEXT * ctxs[SUB_PARTITIONS_AB],int mi_row,int mi_col,BLOCK_SIZE bsize,PARTITION_TYPE partition,const BLOCK_SIZE ab_subsize[SUB_PARTITIONS_AB],const int ab_mi_pos[SUB_PARTITIONS_AB][2],const MB_MODE_INFO ** mode_cache)3159*77c1e3ccSAndroid Build Coastguard Worker static bool rd_test_partition3(AV1_COMP *const cpi, ThreadData *td,
3160*77c1e3ccSAndroid Build Coastguard Worker                                TileDataEnc *tile_data, TokenExtra **tp,
3161*77c1e3ccSAndroid Build Coastguard Worker                                PC_TREE *pc_tree, RD_STATS *best_rdc,
3162*77c1e3ccSAndroid Build Coastguard Worker                                int64_t *this_rdcost,
3163*77c1e3ccSAndroid Build Coastguard Worker                                PICK_MODE_CONTEXT *ctxs[SUB_PARTITIONS_AB],
3164*77c1e3ccSAndroid Build Coastguard Worker                                int mi_row, int mi_col, BLOCK_SIZE bsize,
3165*77c1e3ccSAndroid Build Coastguard Worker                                PARTITION_TYPE partition,
3166*77c1e3ccSAndroid Build Coastguard Worker                                const BLOCK_SIZE ab_subsize[SUB_PARTITIONS_AB],
3167*77c1e3ccSAndroid Build Coastguard Worker                                const int ab_mi_pos[SUB_PARTITIONS_AB][2],
3168*77c1e3ccSAndroid Build Coastguard Worker                                const MB_MODE_INFO **mode_cache) {
3169*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
3170*77c1e3ccSAndroid Build Coastguard Worker   const MACROBLOCKD *const xd = &x->e_mbd;
3171*77c1e3ccSAndroid Build Coastguard Worker   const int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3172*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS sum_rdc;
3173*77c1e3ccSAndroid Build Coastguard Worker   av1_init_rd_stats(&sum_rdc);
3174*77c1e3ccSAndroid Build Coastguard Worker   sum_rdc.rate = x->mode_costs.partition_cost[pl][partition];
3175*77c1e3ccSAndroid Build Coastguard Worker   sum_rdc.rdcost = RDCOST(x->rdmult, sum_rdc.rate, 0);
3176*77c1e3ccSAndroid Build Coastguard Worker   // Loop over sub-partitions in AB partition type.
3177*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_AB; i++) {
3178*77c1e3ccSAndroid Build Coastguard Worker     if (mode_cache && mode_cache[i]) {
3179*77c1e3ccSAndroid Build Coastguard Worker       x->use_mb_mode_cache = 1;
3180*77c1e3ccSAndroid Build Coastguard Worker       x->mb_mode_cache = mode_cache[i];
3181*77c1e3ccSAndroid Build Coastguard Worker     }
3182*77c1e3ccSAndroid Build Coastguard Worker     const int mode_search_success =
3183*77c1e3ccSAndroid Build Coastguard Worker         rd_try_subblock(cpi, td, tile_data, tp, i == SUB_PARTITIONS_AB - 1,
3184*77c1e3ccSAndroid Build Coastguard Worker                         ab_mi_pos[i][0], ab_mi_pos[i][1], ab_subsize[i],
3185*77c1e3ccSAndroid Build Coastguard Worker                         *best_rdc, &sum_rdc, partition, ctxs[i]);
3186*77c1e3ccSAndroid Build Coastguard Worker     x->use_mb_mode_cache = 0;
3187*77c1e3ccSAndroid Build Coastguard Worker     x->mb_mode_cache = NULL;
3188*77c1e3ccSAndroid Build Coastguard Worker     if (!mode_search_success) {
3189*77c1e3ccSAndroid Build Coastguard Worker       return false;
3190*77c1e3ccSAndroid Build Coastguard Worker     }
3191*77c1e3ccSAndroid Build Coastguard Worker   }
3192*77c1e3ccSAndroid Build Coastguard Worker 
3193*77c1e3ccSAndroid Build Coastguard Worker   av1_rd_cost_update(x->rdmult, &sum_rdc);
3194*77c1e3ccSAndroid Build Coastguard Worker   *this_rdcost = sum_rdc.rdcost;
3195*77c1e3ccSAndroid Build Coastguard Worker   if (sum_rdc.rdcost >= best_rdc->rdcost) return false;
3196*77c1e3ccSAndroid Build Coastguard Worker   sum_rdc.rdcost = RDCOST(x->rdmult, sum_rdc.rate, sum_rdc.dist);
3197*77c1e3ccSAndroid Build Coastguard Worker   *this_rdcost = sum_rdc.rdcost;
3198*77c1e3ccSAndroid Build Coastguard Worker   if (sum_rdc.rdcost >= best_rdc->rdcost) return false;
3199*77c1e3ccSAndroid Build Coastguard Worker 
3200*77c1e3ccSAndroid Build Coastguard Worker   *best_rdc = sum_rdc;
3201*77c1e3ccSAndroid Build Coastguard Worker   pc_tree->partitioning = partition;
3202*77c1e3ccSAndroid Build Coastguard Worker   return true;
3203*77c1e3ccSAndroid Build Coastguard Worker }
3204*77c1e3ccSAndroid Build Coastguard Worker 
3205*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
init_partition_block_timing_stats(PartitionTimingStats * part_timing_stats)3206*77c1e3ccSAndroid Build Coastguard Worker static void init_partition_block_timing_stats(
3207*77c1e3ccSAndroid Build Coastguard Worker     PartitionTimingStats *part_timing_stats) {
3208*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(*part_timing_stats);
3209*77c1e3ccSAndroid Build Coastguard Worker }
3210*77c1e3ccSAndroid Build Coastguard Worker 
start_partition_block_timer(PartitionTimingStats * part_timing_stats,PARTITION_TYPE partition_type)3211*77c1e3ccSAndroid Build Coastguard Worker static inline void start_partition_block_timer(
3212*77c1e3ccSAndroid Build Coastguard Worker     PartitionTimingStats *part_timing_stats, PARTITION_TYPE partition_type) {
3213*77c1e3ccSAndroid Build Coastguard Worker   assert(!part_timing_stats->timer_is_on);
3214*77c1e3ccSAndroid Build Coastguard Worker   part_timing_stats->partition_attempts[partition_type] += 1;
3215*77c1e3ccSAndroid Build Coastguard Worker   aom_usec_timer_start(&part_timing_stats->timer);
3216*77c1e3ccSAndroid Build Coastguard Worker   part_timing_stats->timer_is_on = 1;
3217*77c1e3ccSAndroid Build Coastguard Worker }
3218*77c1e3ccSAndroid Build Coastguard Worker 
end_partition_block_timer(PartitionTimingStats * part_timing_stats,PARTITION_TYPE partition_type,int64_t rdcost)3219*77c1e3ccSAndroid Build Coastguard Worker static inline void end_partition_block_timer(
3220*77c1e3ccSAndroid Build Coastguard Worker     PartitionTimingStats *part_timing_stats, PARTITION_TYPE partition_type,
3221*77c1e3ccSAndroid Build Coastguard Worker     int64_t rdcost) {
3222*77c1e3ccSAndroid Build Coastguard Worker   if (part_timing_stats->timer_is_on) {
3223*77c1e3ccSAndroid Build Coastguard Worker     aom_usec_timer_mark(&part_timing_stats->timer);
3224*77c1e3ccSAndroid Build Coastguard Worker     const int64_t time = aom_usec_timer_elapsed(&part_timing_stats->timer);
3225*77c1e3ccSAndroid Build Coastguard Worker     part_timing_stats->partition_times[partition_type] += time;
3226*77c1e3ccSAndroid Build Coastguard Worker     part_timing_stats->partition_rdcost[partition_type] = rdcost;
3227*77c1e3ccSAndroid Build Coastguard Worker     part_timing_stats->timer_is_on = 0;
3228*77c1e3ccSAndroid Build Coastguard Worker   }
3229*77c1e3ccSAndroid Build Coastguard Worker }
print_partition_timing_stats_with_rdcost(const PartitionTimingStats * part_timing_stats,int mi_row,int mi_col,BLOCK_SIZE bsize,FRAME_UPDATE_TYPE frame_update_type,int frame_number,const RD_STATS * best_rdc,const char * filename)3230*77c1e3ccSAndroid Build Coastguard Worker static inline void print_partition_timing_stats_with_rdcost(
3231*77c1e3ccSAndroid Build Coastguard Worker     const PartitionTimingStats *part_timing_stats, int mi_row, int mi_col,
3232*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE bsize, FRAME_UPDATE_TYPE frame_update_type, int frame_number,
3233*77c1e3ccSAndroid Build Coastguard Worker     const RD_STATS *best_rdc, const char *filename) {
3234*77c1e3ccSAndroid Build Coastguard Worker   FILE *f = fopen(filename, "a");
3235*77c1e3ccSAndroid Build Coastguard Worker   fprintf(f, "%d,%d,%d,%d,%d,%d,%" PRId64 ",%" PRId64 ",", bsize, frame_number,
3236*77c1e3ccSAndroid Build Coastguard Worker           frame_update_type, mi_row, mi_col, best_rdc->rate, best_rdc->dist,
3237*77c1e3ccSAndroid Build Coastguard Worker           best_rdc->rdcost);
3238*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < EXT_PARTITION_TYPES; idx++) {
3239*77c1e3ccSAndroid Build Coastguard Worker     fprintf(f, "%d,", part_timing_stats->partition_decisions[idx]);
3240*77c1e3ccSAndroid Build Coastguard Worker   }
3241*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < EXT_PARTITION_TYPES; idx++) {
3242*77c1e3ccSAndroid Build Coastguard Worker     fprintf(f, "%d,", part_timing_stats->partition_attempts[idx]);
3243*77c1e3ccSAndroid Build Coastguard Worker   }
3244*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < EXT_PARTITION_TYPES; idx++) {
3245*77c1e3ccSAndroid Build Coastguard Worker     fprintf(f, "%" PRId64 ",", part_timing_stats->partition_times[idx]);
3246*77c1e3ccSAndroid Build Coastguard Worker   }
3247*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < EXT_PARTITION_TYPES; idx++) {
3248*77c1e3ccSAndroid Build Coastguard Worker     if (part_timing_stats->partition_rdcost[idx] == INT64_MAX) {
3249*77c1e3ccSAndroid Build Coastguard Worker       fprintf(f, "%d,", -1);
3250*77c1e3ccSAndroid Build Coastguard Worker     } else {
3251*77c1e3ccSAndroid Build Coastguard Worker       fprintf(f, "%" PRId64 ",", part_timing_stats->partition_rdcost[idx]);
3252*77c1e3ccSAndroid Build Coastguard Worker     }
3253*77c1e3ccSAndroid Build Coastguard Worker   }
3254*77c1e3ccSAndroid Build Coastguard Worker   fprintf(f, "\n");
3255*77c1e3ccSAndroid Build Coastguard Worker   fclose(f);
3256*77c1e3ccSAndroid Build Coastguard Worker }
3257*77c1e3ccSAndroid Build Coastguard Worker 
print_partition_timing_stats(const PartitionTimingStats * part_timing_stats,int intra_only,int show_frame,const BLOCK_SIZE bsize,const char * filename)3258*77c1e3ccSAndroid Build Coastguard Worker static inline void print_partition_timing_stats(
3259*77c1e3ccSAndroid Build Coastguard Worker     const PartitionTimingStats *part_timing_stats, int intra_only,
3260*77c1e3ccSAndroid Build Coastguard Worker     int show_frame, const BLOCK_SIZE bsize, const char *filename) {
3261*77c1e3ccSAndroid Build Coastguard Worker   FILE *f = fopen(filename, "a");
3262*77c1e3ccSAndroid Build Coastguard Worker   fprintf(f, "%d,%d,%d,", bsize, show_frame, intra_only);
3263*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < EXT_PARTITION_TYPES; idx++) {
3264*77c1e3ccSAndroid Build Coastguard Worker     fprintf(f, "%d,", part_timing_stats->partition_decisions[idx]);
3265*77c1e3ccSAndroid Build Coastguard Worker   }
3266*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < EXT_PARTITION_TYPES; idx++) {
3267*77c1e3ccSAndroid Build Coastguard Worker     fprintf(f, "%d,", part_timing_stats->partition_attempts[idx]);
3268*77c1e3ccSAndroid Build Coastguard Worker   }
3269*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < EXT_PARTITION_TYPES; idx++) {
3270*77c1e3ccSAndroid Build Coastguard Worker     fprintf(f, "%" PRId64 ",", part_timing_stats->partition_times[idx]);
3271*77c1e3ccSAndroid Build Coastguard Worker   }
3272*77c1e3ccSAndroid Build Coastguard Worker   fprintf(f, "\n");
3273*77c1e3ccSAndroid Build Coastguard Worker   fclose(f);
3274*77c1e3ccSAndroid Build Coastguard Worker }
3275*77c1e3ccSAndroid Build Coastguard Worker 
accumulate_partition_timing_stats(FramePartitionTimingStats * fr_part_timing_stats,const PartitionTimingStats * part_timing_stats,BLOCK_SIZE bsize)3276*77c1e3ccSAndroid Build Coastguard Worker static inline void accumulate_partition_timing_stats(
3277*77c1e3ccSAndroid Build Coastguard Worker     FramePartitionTimingStats *fr_part_timing_stats,
3278*77c1e3ccSAndroid Build Coastguard Worker     const PartitionTimingStats *part_timing_stats, BLOCK_SIZE bsize) {
3279*77c1e3ccSAndroid Build Coastguard Worker   const int bsize_idx = av1_get_bsize_idx_for_part_stats(bsize);
3280*77c1e3ccSAndroid Build Coastguard Worker   int *agg_attempts = fr_part_timing_stats->partition_attempts[bsize_idx];
3281*77c1e3ccSAndroid Build Coastguard Worker   int *agg_decisions = fr_part_timing_stats->partition_decisions[bsize_idx];
3282*77c1e3ccSAndroid Build Coastguard Worker   int64_t *agg_times = fr_part_timing_stats->partition_times[bsize_idx];
3283*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < EXT_PARTITION_TYPES; idx++) {
3284*77c1e3ccSAndroid Build Coastguard Worker     agg_attempts[idx] += part_timing_stats->partition_attempts[idx];
3285*77c1e3ccSAndroid Build Coastguard Worker     agg_decisions[idx] += part_timing_stats->partition_decisions[idx];
3286*77c1e3ccSAndroid Build Coastguard Worker     agg_times[idx] += part_timing_stats->partition_times[idx];
3287*77c1e3ccSAndroid Build Coastguard Worker   }
3288*77c1e3ccSAndroid Build Coastguard Worker }
3289*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_COLLECT_PARTITION_STATS
3290*77c1e3ccSAndroid Build Coastguard Worker 
3291*77c1e3ccSAndroid Build Coastguard Worker // Initialize state variables of partition search used in
3292*77c1e3ccSAndroid Build Coastguard Worker // av1_rd_pick_partition().
init_partition_search_state_params(MACROBLOCK * x,AV1_COMP * const cpi,PartitionSearchState * part_search_state,int mi_row,int mi_col,BLOCK_SIZE bsize)3293*77c1e3ccSAndroid Build Coastguard Worker static void init_partition_search_state_params(
3294*77c1e3ccSAndroid Build Coastguard Worker     MACROBLOCK *x, AV1_COMP *const cpi, PartitionSearchState *part_search_state,
3295*77c1e3ccSAndroid Build Coastguard Worker     int mi_row, int mi_col, BLOCK_SIZE bsize) {
3296*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
3297*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
3298*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams *blk_params = &part_search_state->part_blk_params;
3299*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
3300*77c1e3ccSAndroid Build Coastguard Worker 
3301*77c1e3ccSAndroid Build Coastguard Worker   // Initialization of block size related parameters.
3302*77c1e3ccSAndroid Build Coastguard Worker   blk_params->mi_step = mi_size_wide[bsize] / 2;
3303*77c1e3ccSAndroid Build Coastguard Worker   blk_params->mi_row = mi_row;
3304*77c1e3ccSAndroid Build Coastguard Worker   blk_params->mi_col = mi_col;
3305*77c1e3ccSAndroid Build Coastguard Worker   blk_params->mi_row_edge = mi_row + blk_params->mi_step;
3306*77c1e3ccSAndroid Build Coastguard Worker   blk_params->mi_col_edge = mi_col + blk_params->mi_step;
3307*77c1e3ccSAndroid Build Coastguard Worker   blk_params->width = block_size_wide[bsize];
3308*77c1e3ccSAndroid Build Coastguard Worker   blk_params->min_partition_size_1d =
3309*77c1e3ccSAndroid Build Coastguard Worker       block_size_wide[x->sb_enc.min_partition_size];
3310*77c1e3ccSAndroid Build Coastguard Worker   blk_params->subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
3311*77c1e3ccSAndroid Build Coastguard Worker   blk_params->split_bsize2 = blk_params->subsize;
3312*77c1e3ccSAndroid Build Coastguard Worker   blk_params->bsize_at_least_8x8 = (bsize >= BLOCK_8X8);
3313*77c1e3ccSAndroid Build Coastguard Worker   blk_params->bsize = bsize;
3314*77c1e3ccSAndroid Build Coastguard Worker 
3315*77c1e3ccSAndroid Build Coastguard Worker   // Check if the partition corresponds to edge block.
3316*77c1e3ccSAndroid Build Coastguard Worker   blk_params->has_rows = (blk_params->mi_row_edge < mi_params->mi_rows);
3317*77c1e3ccSAndroid Build Coastguard Worker   blk_params->has_cols = (blk_params->mi_col_edge < mi_params->mi_cols);
3318*77c1e3ccSAndroid Build Coastguard Worker 
3319*77c1e3ccSAndroid Build Coastguard Worker   // Update intra partitioning related info.
3320*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->intra_part_info = &x->part_search_info;
3321*77c1e3ccSAndroid Build Coastguard Worker   // Prepare for segmentation CNN-based partitioning for intra-frame.
3322*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm) && bsize == BLOCK_64X64) {
3323*77c1e3ccSAndroid Build Coastguard Worker     part_search_state->intra_part_info->quad_tree_idx = 0;
3324*77c1e3ccSAndroid Build Coastguard Worker     part_search_state->intra_part_info->cnn_output_valid = 0;
3325*77c1e3ccSAndroid Build Coastguard Worker   }
3326*77c1e3ccSAndroid Build Coastguard Worker 
3327*77c1e3ccSAndroid Build Coastguard Worker   // Set partition plane context index.
3328*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->pl_ctx_idx =
3329*77c1e3ccSAndroid Build Coastguard Worker       blk_params->bsize_at_least_8x8
3330*77c1e3ccSAndroid Build Coastguard Worker           ? partition_plane_context(xd, mi_row, mi_col, bsize)
3331*77c1e3ccSAndroid Build Coastguard Worker           : 0;
3332*77c1e3ccSAndroid Build Coastguard Worker 
3333*77c1e3ccSAndroid Build Coastguard Worker   // Partition cost buffer update
3334*77c1e3ccSAndroid Build Coastguard Worker   ModeCosts *mode_costs = &x->mode_costs;
3335*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->partition_cost =
3336*77c1e3ccSAndroid Build Coastguard Worker       mode_costs->partition_cost[part_search_state->pl_ctx_idx];
3337*77c1e3ccSAndroid Build Coastguard Worker 
3338*77c1e3ccSAndroid Build Coastguard Worker   // Initialize HORZ and VERT win flags as true for all split partitions.
3339*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; i++) {
3340*77c1e3ccSAndroid Build Coastguard Worker     part_search_state->split_part_rect_win[i].rect_part_win[HORZ] = true;
3341*77c1e3ccSAndroid Build Coastguard Worker     part_search_state->split_part_rect_win[i].rect_part_win[VERT] = true;
3342*77c1e3ccSAndroid Build Coastguard Worker   }
3343*77c1e3ccSAndroid Build Coastguard Worker 
3344*77c1e3ccSAndroid Build Coastguard Worker   // Initialize the rd cost.
3345*77c1e3ccSAndroid Build Coastguard Worker   av1_init_rd_stats(&part_search_state->this_rdc);
3346*77c1e3ccSAndroid Build Coastguard Worker 
3347*77c1e3ccSAndroid Build Coastguard Worker   // Initialize RD costs for partition types to 0.
3348*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->none_rd = 0;
3349*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(part_search_state->split_rd);
3350*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(part_search_state->rect_part_rd);
3351*77c1e3ccSAndroid Build Coastguard Worker 
3352*77c1e3ccSAndroid Build Coastguard Worker   // Initialize SPLIT partition to be not ready.
3353*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(part_search_state->is_split_ctx_is_ready);
3354*77c1e3ccSAndroid Build Coastguard Worker   // Initialize HORZ and VERT partitions to be not ready.
3355*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(part_search_state->is_rect_ctx_is_ready);
3356*77c1e3ccSAndroid Build Coastguard Worker 
3357*77c1e3ccSAndroid Build Coastguard Worker   // Chroma subsampling.
3358*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->ss_x = x->e_mbd.plane[1].subsampling_x;
3359*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->ss_y = x->e_mbd.plane[1].subsampling_y;
3360*77c1e3ccSAndroid Build Coastguard Worker 
3361*77c1e3ccSAndroid Build Coastguard Worker   // Initialize partition search flags to defaults.
3362*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->terminate_partition_search = 0;
3363*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->do_square_split = blk_params->bsize_at_least_8x8;
3364*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->do_rectangular_split =
3365*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.part_cfg.enable_rect_partitions &&
3366*77c1e3ccSAndroid Build Coastguard Worker       blk_params->bsize_at_least_8x8;
3367*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(part_search_state->prune_rect_part);
3368*77c1e3ccSAndroid Build Coastguard Worker 
3369*77c1e3ccSAndroid Build Coastguard Worker   // Initialize allowed partition types for the partition block.
3370*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->partition_none_allowed =
3371*77c1e3ccSAndroid Build Coastguard Worker       av1_blk_has_rows_and_cols(blk_params);
3372*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->partition_rect_allowed[HORZ] =
3373*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->do_rectangular_split && blk_params->has_cols &&
3374*77c1e3ccSAndroid Build Coastguard Worker       get_plane_block_size(get_partition_subsize(bsize, PARTITION_HORZ),
3375*77c1e3ccSAndroid Build Coastguard Worker                            part_search_state->ss_x,
3376*77c1e3ccSAndroid Build Coastguard Worker                            part_search_state->ss_y) != BLOCK_INVALID;
3377*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->partition_rect_allowed[VERT] =
3378*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->do_rectangular_split && blk_params->has_rows &&
3379*77c1e3ccSAndroid Build Coastguard Worker       get_plane_block_size(get_partition_subsize(bsize, PARTITION_VERT),
3380*77c1e3ccSAndroid Build Coastguard Worker                            part_search_state->ss_x,
3381*77c1e3ccSAndroid Build Coastguard Worker                            part_search_state->ss_y) != BLOCK_INVALID;
3382*77c1e3ccSAndroid Build Coastguard Worker 
3383*77c1e3ccSAndroid Build Coastguard Worker   // Reset the flag indicating whether a partition leading to a rdcost lower
3384*77c1e3ccSAndroid Build Coastguard Worker   // than the bound best_rdc has been found.
3385*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->found_best_partition = false;
3386*77c1e3ccSAndroid Build Coastguard Worker 
3387*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
3388*77c1e3ccSAndroid Build Coastguard Worker   init_partition_block_timing_stats(&part_search_state->part_timing_stats);
3389*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_COLLECT_PARTITION_STATS
3390*77c1e3ccSAndroid Build Coastguard Worker }
3391*77c1e3ccSAndroid Build Coastguard Worker 
3392*77c1e3ccSAndroid Build Coastguard Worker // Override partition cost buffer for the edge blocks.
set_partition_cost_for_edge_blk(AV1_COMMON const * cm,PartitionSearchState * part_search_state)3393*77c1e3ccSAndroid Build Coastguard Worker static void set_partition_cost_for_edge_blk(
3394*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMMON const *cm, PartitionSearchState *part_search_state) {
3395*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state->part_blk_params;
3396*77c1e3ccSAndroid Build Coastguard Worker   assert(blk_params.bsize_at_least_8x8 && part_search_state->pl_ctx_idx >= 0);
3397*77c1e3ccSAndroid Build Coastguard Worker   const aom_cdf_prob *partition_cdf =
3398*77c1e3ccSAndroid Build Coastguard Worker       cm->fc->partition_cdf[part_search_state->pl_ctx_idx];
3399*77c1e3ccSAndroid Build Coastguard Worker   const int max_cost = av1_cost_symbol(0);
3400*77c1e3ccSAndroid Build Coastguard Worker   for (PARTITION_TYPE i = 0; i < PARTITION_TYPES; ++i)
3401*77c1e3ccSAndroid Build Coastguard Worker     part_search_state->tmp_partition_cost[i] = max_cost;
3402*77c1e3ccSAndroid Build Coastguard Worker   if (blk_params.has_cols) {
3403*77c1e3ccSAndroid Build Coastguard Worker     // At the bottom, the two possibilities are HORZ and SPLIT.
3404*77c1e3ccSAndroid Build Coastguard Worker     aom_cdf_prob bot_cdf[2];
3405*77c1e3ccSAndroid Build Coastguard Worker     partition_gather_vert_alike(bot_cdf, partition_cdf, blk_params.bsize);
3406*77c1e3ccSAndroid Build Coastguard Worker     static const int bot_inv_map[2] = { PARTITION_HORZ, PARTITION_SPLIT };
3407*77c1e3ccSAndroid Build Coastguard Worker     av1_cost_tokens_from_cdf(part_search_state->tmp_partition_cost, bot_cdf,
3408*77c1e3ccSAndroid Build Coastguard Worker                              bot_inv_map);
3409*77c1e3ccSAndroid Build Coastguard Worker   } else if (blk_params.has_rows) {
3410*77c1e3ccSAndroid Build Coastguard Worker     // At the right, the two possibilities are VERT and SPLIT.
3411*77c1e3ccSAndroid Build Coastguard Worker     aom_cdf_prob rhs_cdf[2];
3412*77c1e3ccSAndroid Build Coastguard Worker     partition_gather_horz_alike(rhs_cdf, partition_cdf, blk_params.bsize);
3413*77c1e3ccSAndroid Build Coastguard Worker     static const int rhs_inv_map[2] = { PARTITION_VERT, PARTITION_SPLIT };
3414*77c1e3ccSAndroid Build Coastguard Worker     av1_cost_tokens_from_cdf(part_search_state->tmp_partition_cost, rhs_cdf,
3415*77c1e3ccSAndroid Build Coastguard Worker                              rhs_inv_map);
3416*77c1e3ccSAndroid Build Coastguard Worker   } else {
3417*77c1e3ccSAndroid Build Coastguard Worker     // At the bottom right, we always split.
3418*77c1e3ccSAndroid Build Coastguard Worker     part_search_state->tmp_partition_cost[PARTITION_SPLIT] = 0;
3419*77c1e3ccSAndroid Build Coastguard Worker   }
3420*77c1e3ccSAndroid Build Coastguard Worker   // Override the partition cost buffer.
3421*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->partition_cost = part_search_state->tmp_partition_cost;
3422*77c1e3ccSAndroid Build Coastguard Worker }
3423*77c1e3ccSAndroid Build Coastguard Worker 
3424*77c1e3ccSAndroid Build Coastguard Worker // Reset the partition search state flags when
3425*77c1e3ccSAndroid Build Coastguard Worker // must_find_valid_partition is equal to 1.
reset_part_limitations(AV1_COMP * const cpi,PartitionSearchState * part_search_state)3426*77c1e3ccSAndroid Build Coastguard Worker static inline void reset_part_limitations(
3427*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, PartitionSearchState *part_search_state) {
3428*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state->part_blk_params;
3429*77c1e3ccSAndroid Build Coastguard Worker   const int is_rect_part_allowed =
3430*77c1e3ccSAndroid Build Coastguard Worker       blk_params.bsize_at_least_8x8 &&
3431*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.part_cfg.enable_rect_partitions &&
3432*77c1e3ccSAndroid Build Coastguard Worker       (blk_params.width > blk_params.min_partition_size_1d);
3433*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->do_square_split =
3434*77c1e3ccSAndroid Build Coastguard Worker       blk_params.bsize_at_least_8x8 &&
3435*77c1e3ccSAndroid Build Coastguard Worker       (blk_params.width > blk_params.min_partition_size_1d);
3436*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->partition_none_allowed =
3437*77c1e3ccSAndroid Build Coastguard Worker       av1_blk_has_rows_and_cols(&blk_params) &&
3438*77c1e3ccSAndroid Build Coastguard Worker       (blk_params.width >= blk_params.min_partition_size_1d);
3439*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->partition_rect_allowed[HORZ] =
3440*77c1e3ccSAndroid Build Coastguard Worker       blk_params.has_cols && is_rect_part_allowed &&
3441*77c1e3ccSAndroid Build Coastguard Worker       get_plane_block_size(
3442*77c1e3ccSAndroid Build Coastguard Worker           get_partition_subsize(blk_params.bsize, PARTITION_HORZ),
3443*77c1e3ccSAndroid Build Coastguard Worker           part_search_state->ss_x, part_search_state->ss_y) != BLOCK_INVALID;
3444*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->partition_rect_allowed[VERT] =
3445*77c1e3ccSAndroid Build Coastguard Worker       blk_params.has_rows && is_rect_part_allowed &&
3446*77c1e3ccSAndroid Build Coastguard Worker       get_plane_block_size(
3447*77c1e3ccSAndroid Build Coastguard Worker           get_partition_subsize(blk_params.bsize, PARTITION_VERT),
3448*77c1e3ccSAndroid Build Coastguard Worker           part_search_state->ss_x, part_search_state->ss_y) != BLOCK_INVALID;
3449*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->terminate_partition_search = 0;
3450*77c1e3ccSAndroid Build Coastguard Worker }
3451*77c1e3ccSAndroid Build Coastguard Worker 
3452*77c1e3ccSAndroid Build Coastguard Worker // Rectangular partitions evaluation at sub-block level.
rd_pick_rect_partition(AV1_COMP * const cpi,TileDataEnc * tile_data,MACROBLOCK * x,PICK_MODE_CONTEXT * cur_partition_ctx,PartitionSearchState * part_search_state,RD_STATS * best_rdc,const int idx,int mi_row,int mi_col,BLOCK_SIZE bsize,PARTITION_TYPE partition_type)3453*77c1e3ccSAndroid Build Coastguard Worker static void rd_pick_rect_partition(AV1_COMP *const cpi, TileDataEnc *tile_data,
3454*77c1e3ccSAndroid Build Coastguard Worker                                    MACROBLOCK *x,
3455*77c1e3ccSAndroid Build Coastguard Worker                                    PICK_MODE_CONTEXT *cur_partition_ctx,
3456*77c1e3ccSAndroid Build Coastguard Worker                                    PartitionSearchState *part_search_state,
3457*77c1e3ccSAndroid Build Coastguard Worker                                    RD_STATS *best_rdc, const int idx,
3458*77c1e3ccSAndroid Build Coastguard Worker                                    int mi_row, int mi_col, BLOCK_SIZE bsize,
3459*77c1e3ccSAndroid Build Coastguard Worker                                    PARTITION_TYPE partition_type) {
3460*77c1e3ccSAndroid Build Coastguard Worker   // Obtain the remainder from the best rd cost
3461*77c1e3ccSAndroid Build Coastguard Worker   // for further processing of partition.
3462*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS best_remain_rdcost;
3463*77c1e3ccSAndroid Build Coastguard Worker   av1_rd_stats_subtraction(x->rdmult, best_rdc, &part_search_state->sum_rdc,
3464*77c1e3ccSAndroid Build Coastguard Worker                            &best_remain_rdcost);
3465*77c1e3ccSAndroid Build Coastguard Worker 
3466*77c1e3ccSAndroid Build Coastguard Worker   // Obtain the best mode for the partition sub-block.
3467*77c1e3ccSAndroid Build Coastguard Worker   pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &part_search_state->this_rdc,
3468*77c1e3ccSAndroid Build Coastguard Worker                 partition_type, bsize, cur_partition_ctx, best_remain_rdcost);
3469*77c1e3ccSAndroid Build Coastguard Worker   av1_rd_cost_update(x->rdmult, &part_search_state->this_rdc);
3470*77c1e3ccSAndroid Build Coastguard Worker 
3471*77c1e3ccSAndroid Build Coastguard Worker   // Update the partition rd cost with the current sub-block rd.
3472*77c1e3ccSAndroid Build Coastguard Worker   if (part_search_state->this_rdc.rate == INT_MAX) {
3473*77c1e3ccSAndroid Build Coastguard Worker     part_search_state->sum_rdc.rdcost = INT64_MAX;
3474*77c1e3ccSAndroid Build Coastguard Worker   } else {
3475*77c1e3ccSAndroid Build Coastguard Worker     part_search_state->sum_rdc.rate += part_search_state->this_rdc.rate;
3476*77c1e3ccSAndroid Build Coastguard Worker     part_search_state->sum_rdc.dist += part_search_state->this_rdc.dist;
3477*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_cost_update(x->rdmult, &part_search_state->sum_rdc);
3478*77c1e3ccSAndroid Build Coastguard Worker   }
3479*77c1e3ccSAndroid Build Coastguard Worker   const RECT_PART_TYPE rect_part =
3480*77c1e3ccSAndroid Build Coastguard Worker       partition_type == PARTITION_HORZ ? HORZ : VERT;
3481*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->rect_part_rd[rect_part][idx] =
3482*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->this_rdc.rdcost;
3483*77c1e3ccSAndroid Build Coastguard Worker }
3484*77c1e3ccSAndroid Build Coastguard Worker 
3485*77c1e3ccSAndroid Build Coastguard Worker typedef int (*active_edge_info)(const AV1_COMP *cpi, int mi_col, int mi_step);
3486*77c1e3ccSAndroid Build Coastguard Worker 
3487*77c1e3ccSAndroid Build Coastguard Worker // Checks if HORZ / VERT partition search is allowed.
is_rect_part_allowed(const AV1_COMP * cpi,const PartitionSearchState * part_search_state,const active_edge_info * active_edge,RECT_PART_TYPE rect_part,const int mi_pos)3488*77c1e3ccSAndroid Build Coastguard Worker static inline int is_rect_part_allowed(
3489*77c1e3ccSAndroid Build Coastguard Worker     const AV1_COMP *cpi, const PartitionSearchState *part_search_state,
3490*77c1e3ccSAndroid Build Coastguard Worker     const active_edge_info *active_edge, RECT_PART_TYPE rect_part,
3491*77c1e3ccSAndroid Build Coastguard Worker     const int mi_pos) {
3492*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams *blk_params = &part_search_state->part_blk_params;
3493*77c1e3ccSAndroid Build Coastguard Worker   const int is_part_allowed =
3494*77c1e3ccSAndroid Build Coastguard Worker       (!part_search_state->terminate_partition_search &&
3495*77c1e3ccSAndroid Build Coastguard Worker        part_search_state->partition_rect_allowed[rect_part] &&
3496*77c1e3ccSAndroid Build Coastguard Worker        !part_search_state->prune_rect_part[rect_part] &&
3497*77c1e3ccSAndroid Build Coastguard Worker        (part_search_state->do_rectangular_split ||
3498*77c1e3ccSAndroid Build Coastguard Worker         active_edge[rect_part](cpi, mi_pos, blk_params->mi_step)));
3499*77c1e3ccSAndroid Build Coastguard Worker   return is_part_allowed;
3500*77c1e3ccSAndroid Build Coastguard Worker }
3501*77c1e3ccSAndroid Build Coastguard Worker 
rectangular_partition_search(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,MACROBLOCK * x,PC_TREE * pc_tree,RD_SEARCH_MACROBLOCK_CONTEXT * x_ctx,PartitionSearchState * part_search_state,RD_STATS * best_rdc,RD_RECT_PART_WIN_INFO * rect_part_win_info,const RECT_PART_TYPE start_type,const RECT_PART_TYPE end_type)3502*77c1e3ccSAndroid Build Coastguard Worker static void rectangular_partition_search(
3503*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, ThreadData *td, TileDataEnc *tile_data,
3504*77c1e3ccSAndroid Build Coastguard Worker     TokenExtra **tp, MACROBLOCK *x, PC_TREE *pc_tree,
3505*77c1e3ccSAndroid Build Coastguard Worker     RD_SEARCH_MACROBLOCK_CONTEXT *x_ctx,
3506*77c1e3ccSAndroid Build Coastguard Worker     PartitionSearchState *part_search_state, RD_STATS *best_rdc,
3507*77c1e3ccSAndroid Build Coastguard Worker     RD_RECT_PART_WIN_INFO *rect_part_win_info, const RECT_PART_TYPE start_type,
3508*77c1e3ccSAndroid Build Coastguard Worker     const RECT_PART_TYPE end_type) {
3509*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
3510*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state->part_blk_params;
3511*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS *sum_rdc = &part_search_state->sum_rdc;
3512*77c1e3ccSAndroid Build Coastguard Worker   const int rect_partition_type[NUM_RECT_PARTS] = { PARTITION_HORZ,
3513*77c1e3ccSAndroid Build Coastguard Worker                                                     PARTITION_VERT };
3514*77c1e3ccSAndroid Build Coastguard Worker 
3515*77c1e3ccSAndroid Build Coastguard Worker   // mi_pos_rect[NUM_RECT_PARTS][SUB_PARTITIONS_RECT][0]: mi_row postion of
3516*77c1e3ccSAndroid Build Coastguard Worker   //                                           HORZ and VERT partition types.
3517*77c1e3ccSAndroid Build Coastguard Worker   // mi_pos_rect[NUM_RECT_PARTS][SUB_PARTITIONS_RECT][1]: mi_col postion of
3518*77c1e3ccSAndroid Build Coastguard Worker   //                                           HORZ and VERT partition types.
3519*77c1e3ccSAndroid Build Coastguard Worker   const int mi_pos_rect[NUM_RECT_PARTS][SUB_PARTITIONS_RECT][2] = {
3520*77c1e3ccSAndroid Build Coastguard Worker     { { blk_params.mi_row, blk_params.mi_col },
3521*77c1e3ccSAndroid Build Coastguard Worker       { blk_params.mi_row_edge, blk_params.mi_col } },
3522*77c1e3ccSAndroid Build Coastguard Worker     { { blk_params.mi_row, blk_params.mi_col },
3523*77c1e3ccSAndroid Build Coastguard Worker       { blk_params.mi_row, blk_params.mi_col_edge } }
3524*77c1e3ccSAndroid Build Coastguard Worker   };
3525*77c1e3ccSAndroid Build Coastguard Worker 
3526*77c1e3ccSAndroid Build Coastguard Worker   // Initialize active edge_type function pointer
3527*77c1e3ccSAndroid Build Coastguard Worker   // for HOZR and VERT partition types.
3528*77c1e3ccSAndroid Build Coastguard Worker   active_edge_info active_edge_type[NUM_RECT_PARTS] = { av1_active_h_edge,
3529*77c1e3ccSAndroid Build Coastguard Worker                                                         av1_active_v_edge };
3530*77c1e3ccSAndroid Build Coastguard Worker 
3531*77c1e3ccSAndroid Build Coastguard Worker   // Indicates edge blocks for HORZ and VERT partition types.
3532*77c1e3ccSAndroid Build Coastguard Worker   const int is_not_edge_block[NUM_RECT_PARTS] = { blk_params.has_rows,
3533*77c1e3ccSAndroid Build Coastguard Worker                                                   blk_params.has_cols };
3534*77c1e3ccSAndroid Build Coastguard Worker 
3535*77c1e3ccSAndroid Build Coastguard Worker   // Initialize pc tree context for HORZ and VERT partition types.
3536*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT **cur_ctx[NUM_RECT_PARTS][SUB_PARTITIONS_RECT] = {
3537*77c1e3ccSAndroid Build Coastguard Worker     { &pc_tree->horizontal[0], &pc_tree->horizontal[1] },
3538*77c1e3ccSAndroid Build Coastguard Worker     { &pc_tree->vertical[0], &pc_tree->vertical[1] }
3539*77c1e3ccSAndroid Build Coastguard Worker   };
3540*77c1e3ccSAndroid Build Coastguard Worker 
3541*77c1e3ccSAndroid Build Coastguard Worker   // Loop over rectangular partition types.
3542*77c1e3ccSAndroid Build Coastguard Worker   for (RECT_PART_TYPE i = start_type; i <= end_type; i++) {
3543*77c1e3ccSAndroid Build Coastguard Worker     assert(IMPLIES(!cpi->oxcf.part_cfg.enable_rect_partitions,
3544*77c1e3ccSAndroid Build Coastguard Worker                    !part_search_state->partition_rect_allowed[i]));
3545*77c1e3ccSAndroid Build Coastguard Worker 
3546*77c1e3ccSAndroid Build Coastguard Worker     // Check if the HORZ / VERT partition search is to be performed.
3547*77c1e3ccSAndroid Build Coastguard Worker     if (!is_rect_part_allowed(cpi, part_search_state, active_edge_type, i,
3548*77c1e3ccSAndroid Build Coastguard Worker                               mi_pos_rect[i][0][i]))
3549*77c1e3ccSAndroid Build Coastguard Worker       continue;
3550*77c1e3ccSAndroid Build Coastguard Worker 
3551*77c1e3ccSAndroid Build Coastguard Worker     // Sub-partition idx.
3552*77c1e3ccSAndroid Build Coastguard Worker     int sub_part_idx = 0;
3553*77c1e3ccSAndroid Build Coastguard Worker     PARTITION_TYPE partition_type = rect_partition_type[i];
3554*77c1e3ccSAndroid Build Coastguard Worker     blk_params.subsize =
3555*77c1e3ccSAndroid Build Coastguard Worker         get_partition_subsize(blk_params.bsize, partition_type);
3556*77c1e3ccSAndroid Build Coastguard Worker     assert(blk_params.subsize <= BLOCK_LARGEST);
3557*77c1e3ccSAndroid Build Coastguard Worker     av1_init_rd_stats(sum_rdc);
3558*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < SUB_PARTITIONS_RECT; j++) {
3559*77c1e3ccSAndroid Build Coastguard Worker       if (cur_ctx[i][j][0] == NULL) {
3560*77c1e3ccSAndroid Build Coastguard Worker         cur_ctx[i][j][0] =
3561*77c1e3ccSAndroid Build Coastguard Worker             av1_alloc_pmc(cpi, blk_params.subsize, &td->shared_coeff_buf);
3562*77c1e3ccSAndroid Build Coastguard Worker         if (!cur_ctx[i][j][0])
3563*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(x->e_mbd.error_info, AOM_CODEC_MEM_ERROR,
3564*77c1e3ccSAndroid Build Coastguard Worker                              "Failed to allocate PICK_MODE_CONTEXT");
3565*77c1e3ccSAndroid Build Coastguard Worker       }
3566*77c1e3ccSAndroid Build Coastguard Worker     }
3567*77c1e3ccSAndroid Build Coastguard Worker     sum_rdc->rate = part_search_state->partition_cost[partition_type];
3568*77c1e3ccSAndroid Build Coastguard Worker     sum_rdc->rdcost = RDCOST(x->rdmult, sum_rdc->rate, 0);
3569*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
3570*77c1e3ccSAndroid Build Coastguard Worker     PartitionTimingStats *part_timing_stats =
3571*77c1e3ccSAndroid Build Coastguard Worker         &part_search_state->part_timing_stats;
3572*77c1e3ccSAndroid Build Coastguard Worker     if (best_rdc->rdcost - sum_rdc->rdcost >= 0) {
3573*77c1e3ccSAndroid Build Coastguard Worker       start_partition_block_timer(part_timing_stats, partition_type);
3574*77c1e3ccSAndroid Build Coastguard Worker     }
3575*77c1e3ccSAndroid Build Coastguard Worker #endif
3576*77c1e3ccSAndroid Build Coastguard Worker 
3577*77c1e3ccSAndroid Build Coastguard Worker     // First sub-partition evaluation in HORZ / VERT partition type.
3578*77c1e3ccSAndroid Build Coastguard Worker     rd_pick_rect_partition(
3579*77c1e3ccSAndroid Build Coastguard Worker         cpi, tile_data, x, cur_ctx[i][sub_part_idx][0], part_search_state,
3580*77c1e3ccSAndroid Build Coastguard Worker         best_rdc, 0, mi_pos_rect[i][sub_part_idx][0],
3581*77c1e3ccSAndroid Build Coastguard Worker         mi_pos_rect[i][sub_part_idx][1], blk_params.subsize, partition_type);
3582*77c1e3ccSAndroid Build Coastguard Worker 
3583*77c1e3ccSAndroid Build Coastguard Worker     // Start of second sub-partition evaluation.
3584*77c1e3ccSAndroid Build Coastguard Worker     // Evaluate second sub-partition if the first sub-partition cost
3585*77c1e3ccSAndroid Build Coastguard Worker     // is less than the best cost and if it is not an edge block.
3586*77c1e3ccSAndroid Build Coastguard Worker     if (sum_rdc->rdcost < best_rdc->rdcost && is_not_edge_block[i]) {
3587*77c1e3ccSAndroid Build Coastguard Worker       const MB_MODE_INFO *const mbmi = &cur_ctx[i][sub_part_idx][0]->mic;
3588*77c1e3ccSAndroid Build Coastguard Worker       const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
3589*77c1e3ccSAndroid Build Coastguard Worker       // Neither palette mode nor cfl predicted.
3590*77c1e3ccSAndroid Build Coastguard Worker       if (pmi->palette_size[PLANE_TYPE_Y] == 0 &&
3591*77c1e3ccSAndroid Build Coastguard Worker           pmi->palette_size[PLANE_TYPE_UV] == 0) {
3592*77c1e3ccSAndroid Build Coastguard Worker         if (mbmi->uv_mode != UV_CFL_PRED)
3593*77c1e3ccSAndroid Build Coastguard Worker           part_search_state->is_rect_ctx_is_ready[i] = 1;
3594*77c1e3ccSAndroid Build Coastguard Worker       }
3595*77c1e3ccSAndroid Build Coastguard Worker       av1_update_state(cpi, td, cur_ctx[i][sub_part_idx][0], blk_params.mi_row,
3596*77c1e3ccSAndroid Build Coastguard Worker                        blk_params.mi_col, blk_params.subsize, DRY_RUN_NORMAL);
3597*77c1e3ccSAndroid Build Coastguard Worker       encode_superblock(cpi, tile_data, td, tp, DRY_RUN_NORMAL,
3598*77c1e3ccSAndroid Build Coastguard Worker                         blk_params.subsize, NULL);
3599*77c1e3ccSAndroid Build Coastguard Worker 
3600*77c1e3ccSAndroid Build Coastguard Worker       // Second sub-partition evaluation in HORZ / VERT partition type.
3601*77c1e3ccSAndroid Build Coastguard Worker       sub_part_idx = 1;
3602*77c1e3ccSAndroid Build Coastguard Worker       rd_pick_rect_partition(
3603*77c1e3ccSAndroid Build Coastguard Worker           cpi, tile_data, x, cur_ctx[i][sub_part_idx][0], part_search_state,
3604*77c1e3ccSAndroid Build Coastguard Worker           best_rdc, 1, mi_pos_rect[i][sub_part_idx][0],
3605*77c1e3ccSAndroid Build Coastguard Worker           mi_pos_rect[i][sub_part_idx][1], blk_params.subsize, partition_type);
3606*77c1e3ccSAndroid Build Coastguard Worker     }
3607*77c1e3ccSAndroid Build Coastguard Worker     // Update HORZ / VERT best partition.
3608*77c1e3ccSAndroid Build Coastguard Worker     if (sum_rdc->rdcost < best_rdc->rdcost) {
3609*77c1e3ccSAndroid Build Coastguard Worker       sum_rdc->rdcost = RDCOST(x->rdmult, sum_rdc->rate, sum_rdc->dist);
3610*77c1e3ccSAndroid Build Coastguard Worker       if (sum_rdc->rdcost < best_rdc->rdcost) {
3611*77c1e3ccSAndroid Build Coastguard Worker         *best_rdc = *sum_rdc;
3612*77c1e3ccSAndroid Build Coastguard Worker         part_search_state->found_best_partition = true;
3613*77c1e3ccSAndroid Build Coastguard Worker         pc_tree->partitioning = partition_type;
3614*77c1e3ccSAndroid Build Coastguard Worker       }
3615*77c1e3ccSAndroid Build Coastguard Worker     } else {
3616*77c1e3ccSAndroid Build Coastguard Worker       // Update HORZ / VERT win flag.
3617*77c1e3ccSAndroid Build Coastguard Worker       if (rect_part_win_info != NULL)
3618*77c1e3ccSAndroid Build Coastguard Worker         rect_part_win_info->rect_part_win[i] = false;
3619*77c1e3ccSAndroid Build Coastguard Worker     }
3620*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
3621*77c1e3ccSAndroid Build Coastguard Worker     if (part_timing_stats->timer_is_on) {
3622*77c1e3ccSAndroid Build Coastguard Worker       end_partition_block_timer(part_timing_stats, partition_type,
3623*77c1e3ccSAndroid Build Coastguard Worker                                 sum_rdc->rdcost);
3624*77c1e3ccSAndroid Build Coastguard Worker     }
3625*77c1e3ccSAndroid Build Coastguard Worker #endif
3626*77c1e3ccSAndroid Build Coastguard Worker     av1_restore_context(x, x_ctx, blk_params.mi_row, blk_params.mi_col,
3627*77c1e3ccSAndroid Build Coastguard Worker                         blk_params.bsize, av1_num_planes(cm));
3628*77c1e3ccSAndroid Build Coastguard Worker   }
3629*77c1e3ccSAndroid Build Coastguard Worker }
3630*77c1e3ccSAndroid Build Coastguard Worker 
3631*77c1e3ccSAndroid Build Coastguard Worker // AB partition type evaluation.
rd_pick_ab_part(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,MACROBLOCK * x,RD_SEARCH_MACROBLOCK_CONTEXT * x_ctx,PC_TREE * pc_tree,PICK_MODE_CONTEXT * dst_ctxs[SUB_PARTITIONS_AB],PartitionSearchState * part_search_state,RD_STATS * best_rdc,const BLOCK_SIZE ab_subsize[SUB_PARTITIONS_AB],const int ab_mi_pos[SUB_PARTITIONS_AB][2],const PARTITION_TYPE part_type,const MB_MODE_INFO ** mode_cache)3632*77c1e3ccSAndroid Build Coastguard Worker static void rd_pick_ab_part(
3633*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, ThreadData *td, TileDataEnc *tile_data,
3634*77c1e3ccSAndroid Build Coastguard Worker     TokenExtra **tp, MACROBLOCK *x, RD_SEARCH_MACROBLOCK_CONTEXT *x_ctx,
3635*77c1e3ccSAndroid Build Coastguard Worker     PC_TREE *pc_tree, PICK_MODE_CONTEXT *dst_ctxs[SUB_PARTITIONS_AB],
3636*77c1e3ccSAndroid Build Coastguard Worker     PartitionSearchState *part_search_state, RD_STATS *best_rdc,
3637*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE ab_subsize[SUB_PARTITIONS_AB],
3638*77c1e3ccSAndroid Build Coastguard Worker     const int ab_mi_pos[SUB_PARTITIONS_AB][2], const PARTITION_TYPE part_type,
3639*77c1e3ccSAndroid Build Coastguard Worker     const MB_MODE_INFO **mode_cache) {
3640*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
3641*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state->part_blk_params;
3642*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params.mi_row;
3643*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col = blk_params.mi_col;
3644*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params.bsize;
3645*77c1e3ccSAndroid Build Coastguard Worker   int64_t this_rdcost = 0;
3646*77c1e3ccSAndroid Build Coastguard Worker 
3647*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
3648*77c1e3ccSAndroid Build Coastguard Worker   PartitionTimingStats *part_timing_stats =
3649*77c1e3ccSAndroid Build Coastguard Worker       &part_search_state->part_timing_stats;
3650*77c1e3ccSAndroid Build Coastguard Worker   {
3651*77c1e3ccSAndroid Build Coastguard Worker     RD_STATS tmp_sum_rdc;
3652*77c1e3ccSAndroid Build Coastguard Worker     av1_init_rd_stats(&tmp_sum_rdc);
3653*77c1e3ccSAndroid Build Coastguard Worker     tmp_sum_rdc.rate = part_search_state->partition_cost[part_type];
3654*77c1e3ccSAndroid Build Coastguard Worker     tmp_sum_rdc.rdcost = RDCOST(x->rdmult, tmp_sum_rdc.rate, 0);
3655*77c1e3ccSAndroid Build Coastguard Worker     if (best_rdc->rdcost - tmp_sum_rdc.rdcost >= 0) {
3656*77c1e3ccSAndroid Build Coastguard Worker       start_partition_block_timer(part_timing_stats, part_type);
3657*77c1e3ccSAndroid Build Coastguard Worker     }
3658*77c1e3ccSAndroid Build Coastguard Worker   }
3659*77c1e3ccSAndroid Build Coastguard Worker #endif
3660*77c1e3ccSAndroid Build Coastguard Worker 
3661*77c1e3ccSAndroid Build Coastguard Worker   // Test this partition and update the best partition.
3662*77c1e3ccSAndroid Build Coastguard Worker   const bool find_best_ab_part = rd_test_partition3(
3663*77c1e3ccSAndroid Build Coastguard Worker       cpi, td, tile_data, tp, pc_tree, best_rdc, &this_rdcost, dst_ctxs, mi_row,
3664*77c1e3ccSAndroid Build Coastguard Worker       mi_col, bsize, part_type, ab_subsize, ab_mi_pos, mode_cache);
3665*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->found_best_partition |= find_best_ab_part;
3666*77c1e3ccSAndroid Build Coastguard Worker 
3667*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
3668*77c1e3ccSAndroid Build Coastguard Worker   if (part_timing_stats->timer_is_on) {
3669*77c1e3ccSAndroid Build Coastguard Worker     if (!find_best_ab_part) this_rdcost = INT64_MAX;
3670*77c1e3ccSAndroid Build Coastguard Worker     end_partition_block_timer(part_timing_stats, part_type, this_rdcost);
3671*77c1e3ccSAndroid Build Coastguard Worker   }
3672*77c1e3ccSAndroid Build Coastguard Worker #endif
3673*77c1e3ccSAndroid Build Coastguard Worker   av1_restore_context(x, x_ctx, mi_row, mi_col, bsize, av1_num_planes(cm));
3674*77c1e3ccSAndroid Build Coastguard Worker }
3675*77c1e3ccSAndroid Build Coastguard Worker 
3676*77c1e3ccSAndroid Build Coastguard Worker // Set mode search context.
set_mode_search_ctx(PC_TREE * pc_tree,const int is_ctx_ready[NUM_AB_PARTS][2],PICK_MODE_CONTEXT ** mode_srch_ctx[NUM_AB_PARTS][2])3677*77c1e3ccSAndroid Build Coastguard Worker static inline void set_mode_search_ctx(
3678*77c1e3ccSAndroid Build Coastguard Worker     PC_TREE *pc_tree, const int is_ctx_ready[NUM_AB_PARTS][2],
3679*77c1e3ccSAndroid Build Coastguard Worker     PICK_MODE_CONTEXT **mode_srch_ctx[NUM_AB_PARTS][2]) {
3680*77c1e3ccSAndroid Build Coastguard Worker   mode_srch_ctx[HORZ_B][0] = &pc_tree->horizontal[0];
3681*77c1e3ccSAndroid Build Coastguard Worker   mode_srch_ctx[VERT_B][0] = &pc_tree->vertical[0];
3682*77c1e3ccSAndroid Build Coastguard Worker 
3683*77c1e3ccSAndroid Build Coastguard Worker   if (is_ctx_ready[HORZ_A][0])
3684*77c1e3ccSAndroid Build Coastguard Worker     mode_srch_ctx[HORZ_A][0] = &pc_tree->split[0]->none;
3685*77c1e3ccSAndroid Build Coastguard Worker 
3686*77c1e3ccSAndroid Build Coastguard Worker   if (is_ctx_ready[VERT_A][0])
3687*77c1e3ccSAndroid Build Coastguard Worker     mode_srch_ctx[VERT_A][0] = &pc_tree->split[0]->none;
3688*77c1e3ccSAndroid Build Coastguard Worker 
3689*77c1e3ccSAndroid Build Coastguard Worker   if (is_ctx_ready[HORZ_A][1])
3690*77c1e3ccSAndroid Build Coastguard Worker     mode_srch_ctx[HORZ_A][1] = &pc_tree->split[1]->none;
3691*77c1e3ccSAndroid Build Coastguard Worker }
3692*77c1e3ccSAndroid Build Coastguard Worker 
copy_partition_mode_from_mode_context(const MB_MODE_INFO ** dst_mode,const PICK_MODE_CONTEXT * ctx)3693*77c1e3ccSAndroid Build Coastguard Worker static inline void copy_partition_mode_from_mode_context(
3694*77c1e3ccSAndroid Build Coastguard Worker     const MB_MODE_INFO **dst_mode, const PICK_MODE_CONTEXT *ctx) {
3695*77c1e3ccSAndroid Build Coastguard Worker   if (ctx && ctx->rd_stats.rate < INT_MAX) {
3696*77c1e3ccSAndroid Build Coastguard Worker     *dst_mode = &ctx->mic;
3697*77c1e3ccSAndroid Build Coastguard Worker   } else {
3698*77c1e3ccSAndroid Build Coastguard Worker     *dst_mode = NULL;
3699*77c1e3ccSAndroid Build Coastguard Worker   }
3700*77c1e3ccSAndroid Build Coastguard Worker }
3701*77c1e3ccSAndroid Build Coastguard Worker 
copy_partition_mode_from_pc_tree(const MB_MODE_INFO ** dst_mode,const PC_TREE * pc_tree)3702*77c1e3ccSAndroid Build Coastguard Worker static inline void copy_partition_mode_from_pc_tree(
3703*77c1e3ccSAndroid Build Coastguard Worker     const MB_MODE_INFO **dst_mode, const PC_TREE *pc_tree) {
3704*77c1e3ccSAndroid Build Coastguard Worker   if (pc_tree) {
3705*77c1e3ccSAndroid Build Coastguard Worker     copy_partition_mode_from_mode_context(dst_mode, pc_tree->none);
3706*77c1e3ccSAndroid Build Coastguard Worker   } else {
3707*77c1e3ccSAndroid Build Coastguard Worker     *dst_mode = NULL;
3708*77c1e3ccSAndroid Build Coastguard Worker   }
3709*77c1e3ccSAndroid Build Coastguard Worker }
3710*77c1e3ccSAndroid Build Coastguard Worker 
set_mode_cache_for_partition_ab(const MB_MODE_INFO ** mode_cache,const PC_TREE * pc_tree,AB_PART_TYPE ab_part_type)3711*77c1e3ccSAndroid Build Coastguard Worker static inline void set_mode_cache_for_partition_ab(
3712*77c1e3ccSAndroid Build Coastguard Worker     const MB_MODE_INFO **mode_cache, const PC_TREE *pc_tree,
3713*77c1e3ccSAndroid Build Coastguard Worker     AB_PART_TYPE ab_part_type) {
3714*77c1e3ccSAndroid Build Coastguard Worker   switch (ab_part_type) {
3715*77c1e3ccSAndroid Build Coastguard Worker     case HORZ_A:
3716*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_pc_tree(&mode_cache[0], pc_tree->split[0]);
3717*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_pc_tree(&mode_cache[1], pc_tree->split[1]);
3718*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_mode_context(&mode_cache[2],
3719*77c1e3ccSAndroid Build Coastguard Worker                                             pc_tree->horizontal[1]);
3720*77c1e3ccSAndroid Build Coastguard Worker       break;
3721*77c1e3ccSAndroid Build Coastguard Worker     case HORZ_B:
3722*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_mode_context(&mode_cache[0],
3723*77c1e3ccSAndroid Build Coastguard Worker                                             pc_tree->horizontal[0]);
3724*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_pc_tree(&mode_cache[1], pc_tree->split[2]);
3725*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_pc_tree(&mode_cache[2], pc_tree->split[3]);
3726*77c1e3ccSAndroid Build Coastguard Worker       break;
3727*77c1e3ccSAndroid Build Coastguard Worker     case VERT_A:
3728*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_pc_tree(&mode_cache[0], pc_tree->split[0]);
3729*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_pc_tree(&mode_cache[1], pc_tree->split[2]);
3730*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_mode_context(&mode_cache[2],
3731*77c1e3ccSAndroid Build Coastguard Worker                                             pc_tree->vertical[1]);
3732*77c1e3ccSAndroid Build Coastguard Worker       break;
3733*77c1e3ccSAndroid Build Coastguard Worker     case VERT_B:
3734*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_mode_context(&mode_cache[0],
3735*77c1e3ccSAndroid Build Coastguard Worker                                             pc_tree->vertical[0]);
3736*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_pc_tree(&mode_cache[1], pc_tree->split[1]);
3737*77c1e3ccSAndroid Build Coastguard Worker       copy_partition_mode_from_pc_tree(&mode_cache[2], pc_tree->split[3]);
3738*77c1e3ccSAndroid Build Coastguard Worker       break;
3739*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0 && "Invalid ab partition type!\n");
3740*77c1e3ccSAndroid Build Coastguard Worker   }
3741*77c1e3ccSAndroid Build Coastguard Worker }
3742*77c1e3ccSAndroid Build Coastguard Worker 
3743*77c1e3ccSAndroid Build Coastguard Worker // AB Partitions type search.
ab_partitions_search(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,MACROBLOCK * x,RD_SEARCH_MACROBLOCK_CONTEXT * x_ctx,PC_TREE * pc_tree,PartitionSearchState * part_search_state,RD_STATS * best_rdc,RD_RECT_PART_WIN_INFO * rect_part_win_info,int pb_source_variance,int ext_partition_allowed,const AB_PART_TYPE start_type,const AB_PART_TYPE end_type)3744*77c1e3ccSAndroid Build Coastguard Worker static void ab_partitions_search(
3745*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, ThreadData *td, TileDataEnc *tile_data,
3746*77c1e3ccSAndroid Build Coastguard Worker     TokenExtra **tp, MACROBLOCK *x, RD_SEARCH_MACROBLOCK_CONTEXT *x_ctx,
3747*77c1e3ccSAndroid Build Coastguard Worker     PC_TREE *pc_tree, PartitionSearchState *part_search_state,
3748*77c1e3ccSAndroid Build Coastguard Worker     RD_STATS *best_rdc, RD_RECT_PART_WIN_INFO *rect_part_win_info,
3749*77c1e3ccSAndroid Build Coastguard Worker     int pb_source_variance, int ext_partition_allowed,
3750*77c1e3ccSAndroid Build Coastguard Worker     const AB_PART_TYPE start_type, const AB_PART_TYPE end_type) {
3751*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state->part_blk_params;
3752*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params.mi_row;
3753*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col = blk_params.mi_col;
3754*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params.bsize;
3755*77c1e3ccSAndroid Build Coastguard Worker 
3756*77c1e3ccSAndroid Build Coastguard Worker   if (part_search_state->terminate_partition_search) {
3757*77c1e3ccSAndroid Build Coastguard Worker     return;
3758*77c1e3ccSAndroid Build Coastguard Worker   }
3759*77c1e3ccSAndroid Build Coastguard Worker 
3760*77c1e3ccSAndroid Build Coastguard Worker   int ab_partitions_allowed[NUM_AB_PARTS];
3761*77c1e3ccSAndroid Build Coastguard Worker   // Prune AB partitions
3762*77c1e3ccSAndroid Build Coastguard Worker   av1_prune_ab_partitions(cpi, x, pc_tree, pb_source_variance, best_rdc->rdcost,
3763*77c1e3ccSAndroid Build Coastguard Worker                           rect_part_win_info, ext_partition_allowed,
3764*77c1e3ccSAndroid Build Coastguard Worker                           part_search_state, ab_partitions_allowed);
3765*77c1e3ccSAndroid Build Coastguard Worker 
3766*77c1e3ccSAndroid Build Coastguard Worker   // Flags to indicate whether the mode search is done.
3767*77c1e3ccSAndroid Build Coastguard Worker   const int is_ctx_ready[NUM_AB_PARTS][2] = {
3768*77c1e3ccSAndroid Build Coastguard Worker     { part_search_state->is_split_ctx_is_ready[0],
3769*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->is_split_ctx_is_ready[1] },
3770*77c1e3ccSAndroid Build Coastguard Worker     { part_search_state->is_rect_ctx_is_ready[HORZ], 0 },
3771*77c1e3ccSAndroid Build Coastguard Worker     { part_search_state->is_split_ctx_is_ready[0], 0 },
3772*77c1e3ccSAndroid Build Coastguard Worker     { part_search_state->is_rect_ctx_is_ready[VERT], 0 }
3773*77c1e3ccSAndroid Build Coastguard Worker   };
3774*77c1e3ccSAndroid Build Coastguard Worker 
3775*77c1e3ccSAndroid Build Coastguard Worker   // Current partition context.
3776*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT **cur_part_ctxs[NUM_AB_PARTS] = { pc_tree->horizontala,
3777*77c1e3ccSAndroid Build Coastguard Worker                                                       pc_tree->horizontalb,
3778*77c1e3ccSAndroid Build Coastguard Worker                                                       pc_tree->verticala,
3779*77c1e3ccSAndroid Build Coastguard Worker                                                       pc_tree->verticalb };
3780*77c1e3ccSAndroid Build Coastguard Worker 
3781*77c1e3ccSAndroid Build Coastguard Worker   // Context of already evaluted partition types.
3782*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT **mode_srch_ctx[NUM_AB_PARTS][2];
3783*77c1e3ccSAndroid Build Coastguard Worker   // Set context of already evaluted partition types.
3784*77c1e3ccSAndroid Build Coastguard Worker   set_mode_search_ctx(pc_tree, is_ctx_ready, mode_srch_ctx);
3785*77c1e3ccSAndroid Build Coastguard Worker 
3786*77c1e3ccSAndroid Build Coastguard Worker   // Array of sub-partition size of AB partition types.
3787*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE ab_subsize[NUM_AB_PARTS][SUB_PARTITIONS_AB] = {
3788*77c1e3ccSAndroid Build Coastguard Worker     { blk_params.split_bsize2, blk_params.split_bsize2,
3789*77c1e3ccSAndroid Build Coastguard Worker       get_partition_subsize(bsize, PARTITION_HORZ_A) },
3790*77c1e3ccSAndroid Build Coastguard Worker     { get_partition_subsize(bsize, PARTITION_HORZ_B), blk_params.split_bsize2,
3791*77c1e3ccSAndroid Build Coastguard Worker       blk_params.split_bsize2 },
3792*77c1e3ccSAndroid Build Coastguard Worker     { blk_params.split_bsize2, blk_params.split_bsize2,
3793*77c1e3ccSAndroid Build Coastguard Worker       get_partition_subsize(bsize, PARTITION_VERT_A) },
3794*77c1e3ccSAndroid Build Coastguard Worker     { get_partition_subsize(bsize, PARTITION_VERT_B), blk_params.split_bsize2,
3795*77c1e3ccSAndroid Build Coastguard Worker       blk_params.split_bsize2 }
3796*77c1e3ccSAndroid Build Coastguard Worker   };
3797*77c1e3ccSAndroid Build Coastguard Worker 
3798*77c1e3ccSAndroid Build Coastguard Worker   // Array of mi_row, mi_col positions corresponds to each sub-partition in AB
3799*77c1e3ccSAndroid Build Coastguard Worker   // partition types.
3800*77c1e3ccSAndroid Build Coastguard Worker   const int ab_mi_pos[NUM_AB_PARTS][SUB_PARTITIONS_AB][2] = {
3801*77c1e3ccSAndroid Build Coastguard Worker     { { mi_row, mi_col },
3802*77c1e3ccSAndroid Build Coastguard Worker       { mi_row, blk_params.mi_col_edge },
3803*77c1e3ccSAndroid Build Coastguard Worker       { blk_params.mi_row_edge, mi_col } },
3804*77c1e3ccSAndroid Build Coastguard Worker     { { mi_row, mi_col },
3805*77c1e3ccSAndroid Build Coastguard Worker       { blk_params.mi_row_edge, mi_col },
3806*77c1e3ccSAndroid Build Coastguard Worker       { blk_params.mi_row_edge, blk_params.mi_col_edge } },
3807*77c1e3ccSAndroid Build Coastguard Worker     { { mi_row, mi_col },
3808*77c1e3ccSAndroid Build Coastguard Worker       { blk_params.mi_row_edge, mi_col },
3809*77c1e3ccSAndroid Build Coastguard Worker       { mi_row, blk_params.mi_col_edge } },
3810*77c1e3ccSAndroid Build Coastguard Worker     { { mi_row, mi_col },
3811*77c1e3ccSAndroid Build Coastguard Worker       { mi_row, blk_params.mi_col_edge },
3812*77c1e3ccSAndroid Build Coastguard Worker       { blk_params.mi_row_edge, blk_params.mi_col_edge } }
3813*77c1e3ccSAndroid Build Coastguard Worker   };
3814*77c1e3ccSAndroid Build Coastguard Worker 
3815*77c1e3ccSAndroid Build Coastguard Worker   // Loop over AB partition types.
3816*77c1e3ccSAndroid Build Coastguard Worker   for (AB_PART_TYPE ab_part_type = start_type; ab_part_type <= end_type;
3817*77c1e3ccSAndroid Build Coastguard Worker        ab_part_type++) {
3818*77c1e3ccSAndroid Build Coastguard Worker     const PARTITION_TYPE part_type = ab_part_type + PARTITION_HORZ_A;
3819*77c1e3ccSAndroid Build Coastguard Worker 
3820*77c1e3ccSAndroid Build Coastguard Worker     // Check if the AB partition search is to be performed.
3821*77c1e3ccSAndroid Build Coastguard Worker     if (!ab_partitions_allowed[ab_part_type]) {
3822*77c1e3ccSAndroid Build Coastguard Worker       continue;
3823*77c1e3ccSAndroid Build Coastguard Worker     }
3824*77c1e3ccSAndroid Build Coastguard Worker 
3825*77c1e3ccSAndroid Build Coastguard Worker     blk_params.subsize = get_partition_subsize(bsize, part_type);
3826*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < SUB_PARTITIONS_AB; i++) {
3827*77c1e3ccSAndroid Build Coastguard Worker       // Set AB partition context.
3828*77c1e3ccSAndroid Build Coastguard Worker       cur_part_ctxs[ab_part_type][i] = av1_alloc_pmc(
3829*77c1e3ccSAndroid Build Coastguard Worker           cpi, ab_subsize[ab_part_type][i], &td->shared_coeff_buf);
3830*77c1e3ccSAndroid Build Coastguard Worker       if (!cur_part_ctxs[ab_part_type][i])
3831*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(x->e_mbd.error_info, AOM_CODEC_MEM_ERROR,
3832*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to allocate PICK_MODE_CONTEXT");
3833*77c1e3ccSAndroid Build Coastguard Worker       // Set mode as not ready.
3834*77c1e3ccSAndroid Build Coastguard Worker       cur_part_ctxs[ab_part_type][i]->rd_mode_is_ready = 0;
3835*77c1e3ccSAndroid Build Coastguard Worker     }
3836*77c1e3ccSAndroid Build Coastguard Worker 
3837*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->sf.part_sf.reuse_prev_rd_results_for_part_ab) {
3838*77c1e3ccSAndroid Build Coastguard Worker       // We can copy directly the mode search results if we have already
3839*77c1e3ccSAndroid Build Coastguard Worker       // searched the current block and the contexts match.
3840*77c1e3ccSAndroid Build Coastguard Worker       if (is_ctx_ready[ab_part_type][0]) {
3841*77c1e3ccSAndroid Build Coastguard Worker         av1_copy_tree_context(cur_part_ctxs[ab_part_type][0],
3842*77c1e3ccSAndroid Build Coastguard Worker                               mode_srch_ctx[ab_part_type][0][0]);
3843*77c1e3ccSAndroid Build Coastguard Worker         cur_part_ctxs[ab_part_type][0]->mic.partition = part_type;
3844*77c1e3ccSAndroid Build Coastguard Worker         cur_part_ctxs[ab_part_type][0]->rd_mode_is_ready = 1;
3845*77c1e3ccSAndroid Build Coastguard Worker         if (is_ctx_ready[ab_part_type][1]) {
3846*77c1e3ccSAndroid Build Coastguard Worker           av1_copy_tree_context(cur_part_ctxs[ab_part_type][1],
3847*77c1e3ccSAndroid Build Coastguard Worker                                 mode_srch_ctx[ab_part_type][1][0]);
3848*77c1e3ccSAndroid Build Coastguard Worker           cur_part_ctxs[ab_part_type][1]->mic.partition = part_type;
3849*77c1e3ccSAndroid Build Coastguard Worker           cur_part_ctxs[ab_part_type][1]->rd_mode_is_ready = 1;
3850*77c1e3ccSAndroid Build Coastguard Worker         }
3851*77c1e3ccSAndroid Build Coastguard Worker       }
3852*77c1e3ccSAndroid Build Coastguard Worker     }
3853*77c1e3ccSAndroid Build Coastguard Worker 
3854*77c1e3ccSAndroid Build Coastguard Worker     // Even if the contexts don't match, we can still speed up by reusing the
3855*77c1e3ccSAndroid Build Coastguard Worker     // previous prediction mode.
3856*77c1e3ccSAndroid Build Coastguard Worker     const MB_MODE_INFO *mode_cache[3] = { NULL, NULL, NULL };
3857*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->sf.part_sf.reuse_best_prediction_for_part_ab) {
3858*77c1e3ccSAndroid Build Coastguard Worker       set_mode_cache_for_partition_ab(mode_cache, pc_tree, ab_part_type);
3859*77c1e3ccSAndroid Build Coastguard Worker     }
3860*77c1e3ccSAndroid Build Coastguard Worker 
3861*77c1e3ccSAndroid Build Coastguard Worker     // Evaluation of AB partition type.
3862*77c1e3ccSAndroid Build Coastguard Worker     rd_pick_ab_part(cpi, td, tile_data, tp, x, x_ctx, pc_tree,
3863*77c1e3ccSAndroid Build Coastguard Worker                     cur_part_ctxs[ab_part_type], part_search_state, best_rdc,
3864*77c1e3ccSAndroid Build Coastguard Worker                     ab_subsize[ab_part_type], ab_mi_pos[ab_part_type],
3865*77c1e3ccSAndroid Build Coastguard Worker                     part_type, mode_cache);
3866*77c1e3ccSAndroid Build Coastguard Worker   }
3867*77c1e3ccSAndroid Build Coastguard Worker }
3868*77c1e3ccSAndroid Build Coastguard Worker 
3869*77c1e3ccSAndroid Build Coastguard Worker // Set mi positions for HORZ4 / VERT4 sub-block partitions.
set_mi_pos_partition4(const int inc_step[NUM_PART4_TYPES],int mi_pos[SUB_PARTITIONS_PART4][2],const int mi_row,const int mi_col)3870*77c1e3ccSAndroid Build Coastguard Worker static void set_mi_pos_partition4(const int inc_step[NUM_PART4_TYPES],
3871*77c1e3ccSAndroid Build Coastguard Worker                                   int mi_pos[SUB_PARTITIONS_PART4][2],
3872*77c1e3ccSAndroid Build Coastguard Worker                                   const int mi_row, const int mi_col) {
3873*77c1e3ccSAndroid Build Coastguard Worker   for (PART4_TYPES i = 0; i < SUB_PARTITIONS_PART4; i++) {
3874*77c1e3ccSAndroid Build Coastguard Worker     mi_pos[i][0] = mi_row + i * inc_step[HORZ4];
3875*77c1e3ccSAndroid Build Coastguard Worker     mi_pos[i][1] = mi_col + i * inc_step[VERT4];
3876*77c1e3ccSAndroid Build Coastguard Worker   }
3877*77c1e3ccSAndroid Build Coastguard Worker }
3878*77c1e3ccSAndroid Build Coastguard Worker 
3879*77c1e3ccSAndroid Build Coastguard Worker // Set context and RD cost for HORZ4 / VERT4 partition types.
set_4_part_ctx_and_rdcost(MACROBLOCK * x,const AV1_COMP * const cpi,ThreadData * td,PICK_MODE_CONTEXT * cur_part_ctx[SUB_PARTITIONS_PART4],PartitionSearchState * part_search_state,PARTITION_TYPE partition_type,BLOCK_SIZE bsize)3880*77c1e3ccSAndroid Build Coastguard Worker static void set_4_part_ctx_and_rdcost(
3881*77c1e3ccSAndroid Build Coastguard Worker     MACROBLOCK *x, const AV1_COMP *const cpi, ThreadData *td,
3882*77c1e3ccSAndroid Build Coastguard Worker     PICK_MODE_CONTEXT *cur_part_ctx[SUB_PARTITIONS_PART4],
3883*77c1e3ccSAndroid Build Coastguard Worker     PartitionSearchState *part_search_state, PARTITION_TYPE partition_type,
3884*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE bsize) {
3885*77c1e3ccSAndroid Build Coastguard Worker   // Initialize sum_rdc RD cost structure.
3886*77c1e3ccSAndroid Build Coastguard Worker   av1_init_rd_stats(&part_search_state->sum_rdc);
3887*77c1e3ccSAndroid Build Coastguard Worker   const int subsize = get_partition_subsize(bsize, partition_type);
3888*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->sum_rdc.rate =
3889*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->partition_cost[partition_type];
3890*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->sum_rdc.rdcost =
3891*77c1e3ccSAndroid Build Coastguard Worker       RDCOST(x->rdmult, part_search_state->sum_rdc.rate, 0);
3892*77c1e3ccSAndroid Build Coastguard Worker   for (PART4_TYPES i = 0; i < SUB_PARTITIONS_PART4; ++i) {
3893*77c1e3ccSAndroid Build Coastguard Worker     cur_part_ctx[i] = av1_alloc_pmc(cpi, subsize, &td->shared_coeff_buf);
3894*77c1e3ccSAndroid Build Coastguard Worker     if (!cur_part_ctx[i])
3895*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(x->e_mbd.error_info, AOM_CODEC_MEM_ERROR,
3896*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PICK_MODE_CONTEXT");
3897*77c1e3ccSAndroid Build Coastguard Worker   }
3898*77c1e3ccSAndroid Build Coastguard Worker }
3899*77c1e3ccSAndroid Build Coastguard Worker 
3900*77c1e3ccSAndroid Build Coastguard Worker // Partition search of HORZ4 / VERT4 partition types.
rd_pick_4partition(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,MACROBLOCK * x,RD_SEARCH_MACROBLOCK_CONTEXT * x_ctx,PC_TREE * pc_tree,PICK_MODE_CONTEXT * cur_part_ctx[SUB_PARTITIONS_PART4],PartitionSearchState * part_search_state,RD_STATS * best_rdc,const int inc_step[NUM_PART4_TYPES],PARTITION_TYPE partition_type)3901*77c1e3ccSAndroid Build Coastguard Worker static void rd_pick_4partition(
3902*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, ThreadData *td, TileDataEnc *tile_data,
3903*77c1e3ccSAndroid Build Coastguard Worker     TokenExtra **tp, MACROBLOCK *x, RD_SEARCH_MACROBLOCK_CONTEXT *x_ctx,
3904*77c1e3ccSAndroid Build Coastguard Worker     PC_TREE *pc_tree, PICK_MODE_CONTEXT *cur_part_ctx[SUB_PARTITIONS_PART4],
3905*77c1e3ccSAndroid Build Coastguard Worker     PartitionSearchState *part_search_state, RD_STATS *best_rdc,
3906*77c1e3ccSAndroid Build Coastguard Worker     const int inc_step[NUM_PART4_TYPES], PARTITION_TYPE partition_type) {
3907*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
3908*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state->part_blk_params;
3909*77c1e3ccSAndroid Build Coastguard Worker   // mi positions needed for HORZ4 and VERT4 partition types.
3910*77c1e3ccSAndroid Build Coastguard Worker   int mi_pos_check[NUM_PART4_TYPES] = { cm->mi_params.mi_rows,
3911*77c1e3ccSAndroid Build Coastguard Worker                                         cm->mi_params.mi_cols };
3912*77c1e3ccSAndroid Build Coastguard Worker   const PART4_TYPES part4_idx = (partition_type != PARTITION_HORZ_4);
3913*77c1e3ccSAndroid Build Coastguard Worker   int mi_pos[SUB_PARTITIONS_PART4][2];
3914*77c1e3ccSAndroid Build Coastguard Worker 
3915*77c1e3ccSAndroid Build Coastguard Worker   blk_params.subsize = get_partition_subsize(blk_params.bsize, partition_type);
3916*77c1e3ccSAndroid Build Coastguard Worker   // Set partition context and RD cost.
3917*77c1e3ccSAndroid Build Coastguard Worker   set_4_part_ctx_and_rdcost(x, cpi, td, cur_part_ctx, part_search_state,
3918*77c1e3ccSAndroid Build Coastguard Worker                             partition_type, blk_params.bsize);
3919*77c1e3ccSAndroid Build Coastguard Worker   // Set mi positions for sub-block sizes.
3920*77c1e3ccSAndroid Build Coastguard Worker   set_mi_pos_partition4(inc_step, mi_pos, blk_params.mi_row, blk_params.mi_col);
3921*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
3922*77c1e3ccSAndroid Build Coastguard Worker   PartitionTimingStats *part_timing_stats =
3923*77c1e3ccSAndroid Build Coastguard Worker       &part_search_state->part_timing_stats;
3924*77c1e3ccSAndroid Build Coastguard Worker   if (best_rdc->rdcost - part_search_state->sum_rdc.rdcost >= 0) {
3925*77c1e3ccSAndroid Build Coastguard Worker     start_partition_block_timer(part_timing_stats, partition_type);
3926*77c1e3ccSAndroid Build Coastguard Worker   }
3927*77c1e3ccSAndroid Build Coastguard Worker #endif
3928*77c1e3ccSAndroid Build Coastguard Worker   // Loop over sub-block partitions.
3929*77c1e3ccSAndroid Build Coastguard Worker   for (PART4_TYPES i = 0; i < SUB_PARTITIONS_PART4; ++i) {
3930*77c1e3ccSAndroid Build Coastguard Worker     if (i > 0 && mi_pos[i][part4_idx] >= mi_pos_check[part4_idx]) break;
3931*77c1e3ccSAndroid Build Coastguard Worker 
3932*77c1e3ccSAndroid Build Coastguard Worker     // Sub-block evaluation of Horz4 / Vert4 partition type.
3933*77c1e3ccSAndroid Build Coastguard Worker     cur_part_ctx[i]->rd_mode_is_ready = 0;
3934*77c1e3ccSAndroid Build Coastguard Worker     if (!rd_try_subblock(
3935*77c1e3ccSAndroid Build Coastguard Worker             cpi, td, tile_data, tp, (i == SUB_PARTITIONS_PART4 - 1),
3936*77c1e3ccSAndroid Build Coastguard Worker             mi_pos[i][0], mi_pos[i][1], blk_params.subsize, *best_rdc,
3937*77c1e3ccSAndroid Build Coastguard Worker             &part_search_state->sum_rdc, partition_type, cur_part_ctx[i])) {
3938*77c1e3ccSAndroid Build Coastguard Worker       av1_invalid_rd_stats(&part_search_state->sum_rdc);
3939*77c1e3ccSAndroid Build Coastguard Worker       break;
3940*77c1e3ccSAndroid Build Coastguard Worker     }
3941*77c1e3ccSAndroid Build Coastguard Worker   }
3942*77c1e3ccSAndroid Build Coastguard Worker 
3943*77c1e3ccSAndroid Build Coastguard Worker   // Calculate the total cost and update the best partition.
3944*77c1e3ccSAndroid Build Coastguard Worker   av1_rd_cost_update(x->rdmult, &part_search_state->sum_rdc);
3945*77c1e3ccSAndroid Build Coastguard Worker   if (part_search_state->sum_rdc.rdcost < best_rdc->rdcost) {
3946*77c1e3ccSAndroid Build Coastguard Worker     *best_rdc = part_search_state->sum_rdc;
3947*77c1e3ccSAndroid Build Coastguard Worker     part_search_state->found_best_partition = true;
3948*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->partitioning = partition_type;
3949*77c1e3ccSAndroid Build Coastguard Worker   }
3950*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
3951*77c1e3ccSAndroid Build Coastguard Worker   if (part_timing_stats->timer_is_on) {
3952*77c1e3ccSAndroid Build Coastguard Worker     end_partition_block_timer(part_timing_stats, partition_type,
3953*77c1e3ccSAndroid Build Coastguard Worker                               part_search_state->sum_rdc.rdcost);
3954*77c1e3ccSAndroid Build Coastguard Worker   }
3955*77c1e3ccSAndroid Build Coastguard Worker #endif
3956*77c1e3ccSAndroid Build Coastguard Worker   av1_restore_context(x, x_ctx, blk_params.mi_row, blk_params.mi_col,
3957*77c1e3ccSAndroid Build Coastguard Worker                       blk_params.bsize, av1_num_planes(cm));
3958*77c1e3ccSAndroid Build Coastguard Worker }
3959*77c1e3ccSAndroid Build Coastguard Worker 
3960*77c1e3ccSAndroid Build Coastguard Worker // Do not evaluate extended partitions if NONE partition is skippable.
prune_ext_part_none_skippable(PICK_MODE_CONTEXT * part_none,int must_find_valid_partition,int skip_non_sq_part_based_on_none,BLOCK_SIZE bsize)3961*77c1e3ccSAndroid Build Coastguard Worker static inline int prune_ext_part_none_skippable(
3962*77c1e3ccSAndroid Build Coastguard Worker     PICK_MODE_CONTEXT *part_none, int must_find_valid_partition,
3963*77c1e3ccSAndroid Build Coastguard Worker     int skip_non_sq_part_based_on_none, BLOCK_SIZE bsize) {
3964*77c1e3ccSAndroid Build Coastguard Worker   if ((skip_non_sq_part_based_on_none >= 1) && (part_none != NULL)) {
3965*77c1e3ccSAndroid Build Coastguard Worker     if (part_none->skippable && !must_find_valid_partition &&
3966*77c1e3ccSAndroid Build Coastguard Worker         bsize >= BLOCK_16X16) {
3967*77c1e3ccSAndroid Build Coastguard Worker       return 1;
3968*77c1e3ccSAndroid Build Coastguard Worker     }
3969*77c1e3ccSAndroid Build Coastguard Worker   }
3970*77c1e3ccSAndroid Build Coastguard Worker   return 0;
3971*77c1e3ccSAndroid Build Coastguard Worker }
3972*77c1e3ccSAndroid Build Coastguard Worker 
3973*77c1e3ccSAndroid Build Coastguard Worker // Allow ab partition search
allow_ab_partition_search(PartitionSearchState * part_search_state,PARTITION_SPEED_FEATURES * part_sf,PARTITION_TYPE curr_best_part,int must_find_valid_partition,int prune_ext_part_state,int64_t best_rdcost)3974*77c1e3ccSAndroid Build Coastguard Worker static int allow_ab_partition_search(PartitionSearchState *part_search_state,
3975*77c1e3ccSAndroid Build Coastguard Worker                                      PARTITION_SPEED_FEATURES *part_sf,
3976*77c1e3ccSAndroid Build Coastguard Worker                                      PARTITION_TYPE curr_best_part,
3977*77c1e3ccSAndroid Build Coastguard Worker                                      int must_find_valid_partition,
3978*77c1e3ccSAndroid Build Coastguard Worker                                      int prune_ext_part_state,
3979*77c1e3ccSAndroid Build Coastguard Worker                                      int64_t best_rdcost) {
3980*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams blk_params = part_search_state->part_blk_params;
3981*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params.bsize;
3982*77c1e3ccSAndroid Build Coastguard Worker 
3983*77c1e3ccSAndroid Build Coastguard Worker   // Do not prune if there is no valid partition
3984*77c1e3ccSAndroid Build Coastguard Worker   if (best_rdcost == INT64_MAX) return 1;
3985*77c1e3ccSAndroid Build Coastguard Worker 
3986*77c1e3ccSAndroid Build Coastguard Worker   // Determine bsize threshold to evaluate ab partitions
3987*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE ab_bsize_thresh = part_sf->ext_partition_eval_thresh;
3988*77c1e3ccSAndroid Build Coastguard Worker   if (part_sf->ext_part_eval_based_on_cur_best && !must_find_valid_partition &&
3989*77c1e3ccSAndroid Build Coastguard Worker       !(curr_best_part == PARTITION_HORZ || curr_best_part == PARTITION_VERT))
3990*77c1e3ccSAndroid Build Coastguard Worker     ab_bsize_thresh = BLOCK_128X128;
3991*77c1e3ccSAndroid Build Coastguard Worker 
3992*77c1e3ccSAndroid Build Coastguard Worker   // ab partitions are only allowed for square block sizes BLOCK_16X16 or
3993*77c1e3ccSAndroid Build Coastguard Worker   // higher, so ab_bsize_thresh must be large enough to exclude BLOCK_4X4 and
3994*77c1e3ccSAndroid Build Coastguard Worker   // BLOCK_8X8.
3995*77c1e3ccSAndroid Build Coastguard Worker   assert(ab_bsize_thresh >= BLOCK_8X8);
3996*77c1e3ccSAndroid Build Coastguard Worker 
3997*77c1e3ccSAndroid Build Coastguard Worker   int ab_partition_allowed =
3998*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->do_rectangular_split && bsize > ab_bsize_thresh &&
3999*77c1e3ccSAndroid Build Coastguard Worker       av1_blk_has_rows_and_cols(&blk_params) && !prune_ext_part_state;
4000*77c1e3ccSAndroid Build Coastguard Worker 
4001*77c1e3ccSAndroid Build Coastguard Worker   return ab_partition_allowed;
4002*77c1e3ccSAndroid Build Coastguard Worker }
4003*77c1e3ccSAndroid Build Coastguard Worker 
4004*77c1e3ccSAndroid Build Coastguard Worker // Prune 4-way partitions based on the number of horz/vert wins
4005*77c1e3ccSAndroid Build Coastguard Worker // in the current block and sub-blocks in PARTITION_SPLIT.
prune_4_partition_using_split_info(AV1_COMP * const cpi,MACROBLOCK * x,PartitionSearchState * part_search_state,int part4_search_allowed[NUM_PART4_TYPES])4006*77c1e3ccSAndroid Build Coastguard Worker static void prune_4_partition_using_split_info(
4007*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, MACROBLOCK *x, PartitionSearchState *part_search_state,
4008*77c1e3ccSAndroid Build Coastguard Worker     int part4_search_allowed[NUM_PART4_TYPES]) {
4009*77c1e3ccSAndroid Build Coastguard Worker   PART4_TYPES cur_part[NUM_PART4_TYPES] = { HORZ4, VERT4 };
4010*77c1e3ccSAndroid Build Coastguard Worker   // Count of child blocks in which HORZ or VERT partition has won
4011*77c1e3ccSAndroid Build Coastguard Worker   int num_child_rect_win[NUM_RECT_PARTS] = { 0, 0 };
4012*77c1e3ccSAndroid Build Coastguard Worker   // Prune HORZ4/VERT4 partitions based on number of HORZ/VERT winners of
4013*77c1e3ccSAndroid Build Coastguard Worker   // split partiitons.
4014*77c1e3ccSAndroid Build Coastguard Worker   // Conservative pruning for high quantizers.
4015*77c1e3ccSAndroid Build Coastguard Worker   const int num_win_thresh = AOMMIN(3 * (MAXQ - x->qindex) / MAXQ + 1, 3);
4016*77c1e3ccSAndroid Build Coastguard Worker 
4017*77c1e3ccSAndroid Build Coastguard Worker   for (RECT_PART_TYPE i = HORZ; i < NUM_RECT_PARTS; i++) {
4018*77c1e3ccSAndroid Build Coastguard Worker     if (!(cpi->sf.part_sf.prune_ext_part_using_split_info &&
4019*77c1e3ccSAndroid Build Coastguard Worker           part4_search_allowed[cur_part[i]]))
4020*77c1e3ccSAndroid Build Coastguard Worker       continue;
4021*77c1e3ccSAndroid Build Coastguard Worker     // Loop over split partitions.
4022*77c1e3ccSAndroid Build Coastguard Worker     // Get rectangular partitions winner info of split partitions.
4023*77c1e3ccSAndroid Build Coastguard Worker     for (int idx = 0; idx < SUB_PARTITIONS_SPLIT; idx++)
4024*77c1e3ccSAndroid Build Coastguard Worker       num_child_rect_win[i] +=
4025*77c1e3ccSAndroid Build Coastguard Worker           (part_search_state->split_part_rect_win[idx].rect_part_win[i]) ? 1
4026*77c1e3ccSAndroid Build Coastguard Worker                                                                          : 0;
4027*77c1e3ccSAndroid Build Coastguard Worker     if (num_child_rect_win[i] < num_win_thresh) {
4028*77c1e3ccSAndroid Build Coastguard Worker       part4_search_allowed[cur_part[i]] = 0;
4029*77c1e3ccSAndroid Build Coastguard Worker     }
4030*77c1e3ccSAndroid Build Coastguard Worker   }
4031*77c1e3ccSAndroid Build Coastguard Worker }
4032*77c1e3ccSAndroid Build Coastguard Worker 
4033*77c1e3ccSAndroid Build Coastguard Worker // Prune 4-way partition search.
prune_4_way_partition_search(AV1_COMP * const cpi,MACROBLOCK * x,PC_TREE * pc_tree,PartitionSearchState * part_search_state,RD_STATS * best_rdc,int pb_source_variance,int prune_ext_part_state,int part4_search_allowed[NUM_PART4_TYPES])4034*77c1e3ccSAndroid Build Coastguard Worker static void prune_4_way_partition_search(
4035*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, MACROBLOCK *x, PC_TREE *pc_tree,
4036*77c1e3ccSAndroid Build Coastguard Worker     PartitionSearchState *part_search_state, RD_STATS *best_rdc,
4037*77c1e3ccSAndroid Build Coastguard Worker     int pb_source_variance, int prune_ext_part_state,
4038*77c1e3ccSAndroid Build Coastguard Worker     int part4_search_allowed[NUM_PART4_TYPES]) {
4039*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams blk_params = part_search_state->part_blk_params;
4040*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params.bsize;
4041*77c1e3ccSAndroid Build Coastguard Worker 
4042*77c1e3ccSAndroid Build Coastguard Worker   // Do not prune if there is no valid partition
4043*77c1e3ccSAndroid Build Coastguard Worker   if (best_rdc->rdcost == INT64_MAX) return;
4044*77c1e3ccSAndroid Build Coastguard Worker 
4045*77c1e3ccSAndroid Build Coastguard Worker   // Determine bsize threshold to evaluate 4-way partitions
4046*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE part4_bsize_thresh = cpi->sf.part_sf.ext_partition_eval_thresh;
4047*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.ext_part_eval_based_on_cur_best &&
4048*77c1e3ccSAndroid Build Coastguard Worker       !x->must_find_valid_partition && pc_tree->partitioning == PARTITION_NONE)
4049*77c1e3ccSAndroid Build Coastguard Worker     part4_bsize_thresh = BLOCK_128X128;
4050*77c1e3ccSAndroid Build Coastguard Worker 
4051*77c1e3ccSAndroid Build Coastguard Worker   // 4-way partitions are only allowed for BLOCK_16X16, BLOCK_32X32, and
4052*77c1e3ccSAndroid Build Coastguard Worker   // BLOCK_64X64, so part4_bsize_thresh must be large enough to exclude
4053*77c1e3ccSAndroid Build Coastguard Worker   // BLOCK_4X4 and BLOCK_8X8.
4054*77c1e3ccSAndroid Build Coastguard Worker   assert(part4_bsize_thresh >= BLOCK_8X8);
4055*77c1e3ccSAndroid Build Coastguard Worker 
4056*77c1e3ccSAndroid Build Coastguard Worker   bool partition4_allowed =
4057*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->do_rectangular_split && bsize > part4_bsize_thresh &&
4058*77c1e3ccSAndroid Build Coastguard Worker       av1_blk_has_rows_and_cols(&blk_params) && !prune_ext_part_state;
4059*77c1e3ccSAndroid Build Coastguard Worker 
4060*77c1e3ccSAndroid Build Coastguard Worker   // Disable 4-way partition search flags for width less than a multiple of the
4061*77c1e3ccSAndroid Build Coastguard Worker   // minimum partition width.
4062*77c1e3ccSAndroid Build Coastguard Worker   if (blk_params.width < (blk_params.min_partition_size_1d
4063*77c1e3ccSAndroid Build Coastguard Worker                           << cpi->sf.part_sf.prune_part4_search)) {
4064*77c1e3ccSAndroid Build Coastguard Worker     part4_search_allowed[HORZ4] = 0;
4065*77c1e3ccSAndroid Build Coastguard Worker     part4_search_allowed[VERT4] = 0;
4066*77c1e3ccSAndroid Build Coastguard Worker     return;
4067*77c1e3ccSAndroid Build Coastguard Worker   }
4068*77c1e3ccSAndroid Build Coastguard Worker 
4069*77c1e3ccSAndroid Build Coastguard Worker   PARTITION_TYPE cur_part[NUM_PART4_TYPES] = { PARTITION_HORZ_4,
4070*77c1e3ccSAndroid Build Coastguard Worker                                                PARTITION_VERT_4 };
4071*77c1e3ccSAndroid Build Coastguard Worker   const PartitionCfg *const part_cfg = &cpi->oxcf.part_cfg;
4072*77c1e3ccSAndroid Build Coastguard Worker   // partition4_allowed is 1 if we can use a PARTITION_HORZ_4 or
4073*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_VERT_4 for this block. This is almost the same as
4074*77c1e3ccSAndroid Build Coastguard Worker   // partition4_allowed, except that we don't allow 128x32 or 32x128
4075*77c1e3ccSAndroid Build Coastguard Worker   // blocks, so we require that bsize is not BLOCK_128X128.
4076*77c1e3ccSAndroid Build Coastguard Worker   partition4_allowed &=
4077*77c1e3ccSAndroid Build Coastguard Worker       part_cfg->enable_1to4_partitions && bsize != BLOCK_128X128;
4078*77c1e3ccSAndroid Build Coastguard Worker 
4079*77c1e3ccSAndroid Build Coastguard Worker   for (PART4_TYPES i = HORZ4; i < NUM_PART4_TYPES; i++) {
4080*77c1e3ccSAndroid Build Coastguard Worker     part4_search_allowed[i] =
4081*77c1e3ccSAndroid Build Coastguard Worker         partition4_allowed && part_search_state->partition_rect_allowed[i] &&
4082*77c1e3ccSAndroid Build Coastguard Worker         get_plane_block_size(get_partition_subsize(bsize, cur_part[i]),
4083*77c1e3ccSAndroid Build Coastguard Worker                              part_search_state->ss_x,
4084*77c1e3ccSAndroid Build Coastguard Worker                              part_search_state->ss_y) != BLOCK_INVALID;
4085*77c1e3ccSAndroid Build Coastguard Worker   }
4086*77c1e3ccSAndroid Build Coastguard Worker   // Pruning: pruning out 4-way partitions based on the current best partition.
4087*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.prune_ext_partition_types_search_level == 2) {
4088*77c1e3ccSAndroid Build Coastguard Worker     part4_search_allowed[HORZ4] &= (pc_tree->partitioning == PARTITION_HORZ ||
4089*77c1e3ccSAndroid Build Coastguard Worker                                     pc_tree->partitioning == PARTITION_HORZ_A ||
4090*77c1e3ccSAndroid Build Coastguard Worker                                     pc_tree->partitioning == PARTITION_HORZ_B ||
4091*77c1e3ccSAndroid Build Coastguard Worker                                     pc_tree->partitioning == PARTITION_SPLIT ||
4092*77c1e3ccSAndroid Build Coastguard Worker                                     pc_tree->partitioning == PARTITION_NONE);
4093*77c1e3ccSAndroid Build Coastguard Worker     part4_search_allowed[VERT4] &= (pc_tree->partitioning == PARTITION_VERT ||
4094*77c1e3ccSAndroid Build Coastguard Worker                                     pc_tree->partitioning == PARTITION_VERT_A ||
4095*77c1e3ccSAndroid Build Coastguard Worker                                     pc_tree->partitioning == PARTITION_VERT_B ||
4096*77c1e3ccSAndroid Build Coastguard Worker                                     pc_tree->partitioning == PARTITION_SPLIT ||
4097*77c1e3ccSAndroid Build Coastguard Worker                                     pc_tree->partitioning == PARTITION_NONE);
4098*77c1e3ccSAndroid Build Coastguard Worker   }
4099*77c1e3ccSAndroid Build Coastguard Worker 
4100*77c1e3ccSAndroid Build Coastguard Worker   // Pruning: pruning out some 4-way partitions using a DNN taking rd costs of
4101*77c1e3ccSAndroid Build Coastguard Worker   // sub-blocks from basic partition types.
4102*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.ml_prune_partition && partition4_allowed &&
4103*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->partition_rect_allowed[HORZ] &&
4104*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->partition_rect_allowed[VERT]) {
4105*77c1e3ccSAndroid Build Coastguard Worker     av1_ml_prune_4_partition(cpi, x, pc_tree->partitioning, best_rdc->rdcost,
4106*77c1e3ccSAndroid Build Coastguard Worker                              part_search_state, part4_search_allowed,
4107*77c1e3ccSAndroid Build Coastguard Worker                              pb_source_variance);
4108*77c1e3ccSAndroid Build Coastguard Worker   }
4109*77c1e3ccSAndroid Build Coastguard Worker 
4110*77c1e3ccSAndroid Build Coastguard Worker   // Pruning: pruning out 4-way partitions based on the number of horz/vert wins
4111*77c1e3ccSAndroid Build Coastguard Worker   // in the current block and sub-blocks in PARTITION_SPLIT.
4112*77c1e3ccSAndroid Build Coastguard Worker   prune_4_partition_using_split_info(cpi, x, part_search_state,
4113*77c1e3ccSAndroid Build Coastguard Worker                                      part4_search_allowed);
4114*77c1e3ccSAndroid Build Coastguard Worker }
4115*77c1e3ccSAndroid Build Coastguard Worker 
4116*77c1e3ccSAndroid Build Coastguard Worker // Set params needed for PARTITION_NONE search.
set_none_partition_params(const AV1_COMP * const cpi,ThreadData * td,MACROBLOCK * x,PC_TREE * pc_tree,PartitionSearchState * part_search_state,RD_STATS * best_remain_rdcost,RD_STATS * best_rdc,int * pt_cost)4117*77c1e3ccSAndroid Build Coastguard Worker static void set_none_partition_params(const AV1_COMP *const cpi, ThreadData *td,
4118*77c1e3ccSAndroid Build Coastguard Worker                                       MACROBLOCK *x, PC_TREE *pc_tree,
4119*77c1e3ccSAndroid Build Coastguard Worker                                       PartitionSearchState *part_search_state,
4120*77c1e3ccSAndroid Build Coastguard Worker                                       RD_STATS *best_remain_rdcost,
4121*77c1e3ccSAndroid Build Coastguard Worker                                       RD_STATS *best_rdc, int *pt_cost) {
4122*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state->part_blk_params;
4123*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS partition_rdcost;
4124*77c1e3ccSAndroid Build Coastguard Worker   // Set PARTITION_NONE context.
4125*77c1e3ccSAndroid Build Coastguard Worker   if (pc_tree->none == NULL)
4126*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->none = av1_alloc_pmc(cpi, blk_params.bsize, &td->shared_coeff_buf);
4127*77c1e3ccSAndroid Build Coastguard Worker   if (!pc_tree->none)
4128*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(x->e_mbd.error_info, AOM_CODEC_MEM_ERROR,
4129*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to allocate PICK_MODE_CONTEXT");
4130*77c1e3ccSAndroid Build Coastguard Worker 
4131*77c1e3ccSAndroid Build Coastguard Worker   // Set PARTITION_NONE type cost.
4132*77c1e3ccSAndroid Build Coastguard Worker   if (part_search_state->partition_none_allowed) {
4133*77c1e3ccSAndroid Build Coastguard Worker     if (blk_params.bsize_at_least_8x8) {
4134*77c1e3ccSAndroid Build Coastguard Worker       *pt_cost = part_search_state->partition_cost[PARTITION_NONE] < INT_MAX
4135*77c1e3ccSAndroid Build Coastguard Worker                      ? part_search_state->partition_cost[PARTITION_NONE]
4136*77c1e3ccSAndroid Build Coastguard Worker                      : 0;
4137*77c1e3ccSAndroid Build Coastguard Worker     }
4138*77c1e3ccSAndroid Build Coastguard Worker 
4139*77c1e3ccSAndroid Build Coastguard Worker     // Initialize the RD stats structure.
4140*77c1e3ccSAndroid Build Coastguard Worker     av1_init_rd_stats(&partition_rdcost);
4141*77c1e3ccSAndroid Build Coastguard Worker     partition_rdcost.rate = *pt_cost;
4142*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_cost_update(x->rdmult, &partition_rdcost);
4143*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_stats_subtraction(x->rdmult, best_rdc, &partition_rdcost,
4144*77c1e3ccSAndroid Build Coastguard Worker                              best_remain_rdcost);
4145*77c1e3ccSAndroid Build Coastguard Worker   }
4146*77c1e3ccSAndroid Build Coastguard Worker }
4147*77c1e3ccSAndroid Build Coastguard Worker 
4148*77c1e3ccSAndroid Build Coastguard Worker // Skip other partitions based on PARTITION_NONE rd cost.
prune_partitions_after_none(AV1_COMP * const cpi,MACROBLOCK * x,SIMPLE_MOTION_DATA_TREE * sms_tree,PICK_MODE_CONTEXT * ctx_none,PartitionSearchState * part_search_state,RD_STATS * best_rdc,unsigned int * pb_source_variance)4149*77c1e3ccSAndroid Build Coastguard Worker static void prune_partitions_after_none(AV1_COMP *const cpi, MACROBLOCK *x,
4150*77c1e3ccSAndroid Build Coastguard Worker                                         SIMPLE_MOTION_DATA_TREE *sms_tree,
4151*77c1e3ccSAndroid Build Coastguard Worker                                         PICK_MODE_CONTEXT *ctx_none,
4152*77c1e3ccSAndroid Build Coastguard Worker                                         PartitionSearchState *part_search_state,
4153*77c1e3ccSAndroid Build Coastguard Worker                                         RD_STATS *best_rdc,
4154*77c1e3ccSAndroid Build Coastguard Worker                                         unsigned int *pb_source_variance) {
4155*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
4156*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
4157*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams blk_params = part_search_state->part_blk_params;
4158*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS *this_rdc = &part_search_state->this_rdc;
4159*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params.bsize;
4160*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize < BLOCK_SIZES_ALL);
4161*77c1e3ccSAndroid Build Coastguard Worker 
4162*77c1e3ccSAndroid Build Coastguard Worker   if (!frame_is_intra_only(cm) &&
4163*77c1e3ccSAndroid Build Coastguard Worker       (part_search_state->do_square_split ||
4164*77c1e3ccSAndroid Build Coastguard Worker        part_search_state->do_rectangular_split) &&
4165*77c1e3ccSAndroid Build Coastguard Worker       !x->e_mbd.lossless[xd->mi[0]->segment_id] && ctx_none->skippable) {
4166*77c1e3ccSAndroid Build Coastguard Worker     const int use_ml_based_breakout =
4167*77c1e3ccSAndroid Build Coastguard Worker         bsize <= cpi->sf.part_sf.use_square_partition_only_threshold &&
4168*77c1e3ccSAndroid Build Coastguard Worker         bsize > BLOCK_4X4 && cpi->sf.part_sf.ml_predict_breakout_level >= 1;
4169*77c1e3ccSAndroid Build Coastguard Worker     if (use_ml_based_breakout) {
4170*77c1e3ccSAndroid Build Coastguard Worker       av1_ml_predict_breakout(cpi, x, this_rdc, *pb_source_variance, xd->bd,
4171*77c1e3ccSAndroid Build Coastguard Worker                               part_search_state);
4172*77c1e3ccSAndroid Build Coastguard Worker     }
4173*77c1e3ccSAndroid Build Coastguard Worker 
4174*77c1e3ccSAndroid Build Coastguard Worker     // Adjust dist breakout threshold according to the partition size.
4175*77c1e3ccSAndroid Build Coastguard Worker     const int64_t dist_breakout_thr =
4176*77c1e3ccSAndroid Build Coastguard Worker         cpi->sf.part_sf.partition_search_breakout_dist_thr >>
4177*77c1e3ccSAndroid Build Coastguard Worker         ((2 * (MAX_SB_SIZE_LOG2 - 2)) -
4178*77c1e3ccSAndroid Build Coastguard Worker          (mi_size_wide_log2[bsize] + mi_size_high_log2[bsize]));
4179*77c1e3ccSAndroid Build Coastguard Worker     const int rate_breakout_thr =
4180*77c1e3ccSAndroid Build Coastguard Worker         cpi->sf.part_sf.partition_search_breakout_rate_thr *
4181*77c1e3ccSAndroid Build Coastguard Worker         num_pels_log2_lookup[bsize];
4182*77c1e3ccSAndroid Build Coastguard Worker     // If all y, u, v transform blocks in this partition are skippable,
4183*77c1e3ccSAndroid Build Coastguard Worker     // and the dist & rate are within the thresholds, the partition
4184*77c1e3ccSAndroid Build Coastguard Worker     // search is terminated for current branch of the partition search
4185*77c1e3ccSAndroid Build Coastguard Worker     // tree. The dist & rate thresholds are set to 0 at speed 0 to
4186*77c1e3ccSAndroid Build Coastguard Worker     // disable the early termination at that speed.
4187*77c1e3ccSAndroid Build Coastguard Worker     if (best_rdc->dist < dist_breakout_thr &&
4188*77c1e3ccSAndroid Build Coastguard Worker         best_rdc->rate < rate_breakout_thr) {
4189*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->do_square_split = 0;
4190*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->do_rectangular_split = 0;
4191*77c1e3ccSAndroid Build Coastguard Worker     }
4192*77c1e3ccSAndroid Build Coastguard Worker   }
4193*77c1e3ccSAndroid Build Coastguard Worker 
4194*77c1e3ccSAndroid Build Coastguard Worker   // Early termination: using simple_motion_search features and the
4195*77c1e3ccSAndroid Build Coastguard Worker   // rate, distortion, and rdcost of PARTITION_NONE, a DNN will make a
4196*77c1e3ccSAndroid Build Coastguard Worker   // decision on early terminating at PARTITION_NONE.
4197*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.simple_motion_search_early_term_none && cm->show_frame &&
4198*77c1e3ccSAndroid Build Coastguard Worker       !frame_is_intra_only(cm) && bsize >= BLOCK_16X16 &&
4199*77c1e3ccSAndroid Build Coastguard Worker       av1_blk_has_rows_and_cols(&blk_params) && this_rdc->rdcost < INT64_MAX &&
4200*77c1e3ccSAndroid Build Coastguard Worker       this_rdc->rdcost >= 0 && this_rdc->rate < INT_MAX &&
4201*77c1e3ccSAndroid Build Coastguard Worker       this_rdc->rate >= 0 &&
4202*77c1e3ccSAndroid Build Coastguard Worker       (part_search_state->do_square_split ||
4203*77c1e3ccSAndroid Build Coastguard Worker        part_search_state->do_rectangular_split)) {
4204*77c1e3ccSAndroid Build Coastguard Worker     av1_simple_motion_search_early_term_none(cpi, x, sms_tree, this_rdc,
4205*77c1e3ccSAndroid Build Coastguard Worker                                              part_search_state);
4206*77c1e3ccSAndroid Build Coastguard Worker   }
4207*77c1e3ccSAndroid Build Coastguard Worker }
4208*77c1e3ccSAndroid Build Coastguard Worker 
4209*77c1e3ccSAndroid Build Coastguard Worker // Decide early termination and rectangular partition pruning
4210*77c1e3ccSAndroid Build Coastguard Worker // based on PARTITION_NONE and PARTITION_SPLIT costs.
prune_partitions_after_split(AV1_COMP * const cpi,MACROBLOCK * x,SIMPLE_MOTION_DATA_TREE * sms_tree,PartitionSearchState * part_search_state,RD_STATS * best_rdc,int64_t part_none_rd,int64_t part_split_rd)4211*77c1e3ccSAndroid Build Coastguard Worker static void prune_partitions_after_split(
4212*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, MACROBLOCK *x, SIMPLE_MOTION_DATA_TREE *sms_tree,
4213*77c1e3ccSAndroid Build Coastguard Worker     PartitionSearchState *part_search_state, RD_STATS *best_rdc,
4214*77c1e3ccSAndroid Build Coastguard Worker     int64_t part_none_rd, int64_t part_split_rd) {
4215*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
4216*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state->part_blk_params;
4217*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params.mi_row;
4218*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col = blk_params.mi_col;
4219*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params.bsize;
4220*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize < BLOCK_SIZES_ALL);
4221*77c1e3ccSAndroid Build Coastguard Worker 
4222*77c1e3ccSAndroid Build Coastguard Worker   // Early termination: using the rd costs of PARTITION_NONE and subblocks
4223*77c1e3ccSAndroid Build Coastguard Worker   // from PARTITION_SPLIT to determine an early breakout.
4224*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.ml_early_term_after_part_split_level &&
4225*77c1e3ccSAndroid Build Coastguard Worker       !frame_is_intra_only(cm) &&
4226*77c1e3ccSAndroid Build Coastguard Worker       !part_search_state->terminate_partition_search &&
4227*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->do_rectangular_split &&
4228*77c1e3ccSAndroid Build Coastguard Worker       (part_search_state->partition_rect_allowed[HORZ] ||
4229*77c1e3ccSAndroid Build Coastguard Worker        part_search_state->partition_rect_allowed[VERT])) {
4230*77c1e3ccSAndroid Build Coastguard Worker     av1_ml_early_term_after_split(
4231*77c1e3ccSAndroid Build Coastguard Worker         cpi, x, sms_tree, best_rdc->rdcost, part_none_rd, part_split_rd,
4232*77c1e3ccSAndroid Build Coastguard Worker         part_search_state->split_rd, part_search_state);
4233*77c1e3ccSAndroid Build Coastguard Worker   }
4234*77c1e3ccSAndroid Build Coastguard Worker 
4235*77c1e3ccSAndroid Build Coastguard Worker   // Use the rd costs of PARTITION_NONE and subblocks from PARTITION_SPLIT
4236*77c1e3ccSAndroid Build Coastguard Worker   // to prune out rectangular partitions in some directions.
4237*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->sf.part_sf.ml_early_term_after_part_split_level &&
4238*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.part_sf.ml_prune_partition && !frame_is_intra_only(cm) &&
4239*77c1e3ccSAndroid Build Coastguard Worker       (part_search_state->partition_rect_allowed[HORZ] ||
4240*77c1e3ccSAndroid Build Coastguard Worker        part_search_state->partition_rect_allowed[VERT]) &&
4241*77c1e3ccSAndroid Build Coastguard Worker       !(part_search_state->prune_rect_part[HORZ] ||
4242*77c1e3ccSAndroid Build Coastguard Worker         part_search_state->prune_rect_part[VERT]) &&
4243*77c1e3ccSAndroid Build Coastguard Worker       !part_search_state->terminate_partition_search) {
4244*77c1e3ccSAndroid Build Coastguard Worker     av1_setup_src_planes(x, cpi->source, mi_row, mi_col, av1_num_planes(cm),
4245*77c1e3ccSAndroid Build Coastguard Worker                          bsize);
4246*77c1e3ccSAndroid Build Coastguard Worker     av1_ml_prune_rect_partition(cpi, x, best_rdc->rdcost,
4247*77c1e3ccSAndroid Build Coastguard Worker                                 part_search_state->none_rd,
4248*77c1e3ccSAndroid Build Coastguard Worker                                 part_search_state->split_rd, part_search_state);
4249*77c1e3ccSAndroid Build Coastguard Worker   }
4250*77c1e3ccSAndroid Build Coastguard Worker }
4251*77c1e3ccSAndroid Build Coastguard Worker 
4252*77c1e3ccSAndroid Build Coastguard Worker // Returns true if either of the left and top neighbor blocks is larger than
4253*77c1e3ccSAndroid Build Coastguard Worker // the current block; false otherwise.
is_neighbor_blk_larger_than_cur_blk(const MACROBLOCKD * xd,BLOCK_SIZE bsize)4254*77c1e3ccSAndroid Build Coastguard Worker static inline bool is_neighbor_blk_larger_than_cur_blk(const MACROBLOCKD *xd,
4255*77c1e3ccSAndroid Build Coastguard Worker                                                        BLOCK_SIZE bsize) {
4256*77c1e3ccSAndroid Build Coastguard Worker   const int cur_blk_area = (block_size_high[bsize] * block_size_wide[bsize]);
4257*77c1e3ccSAndroid Build Coastguard Worker   if (xd->left_available) {
4258*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE left_bsize = xd->left_mbmi->bsize;
4259*77c1e3ccSAndroid Build Coastguard Worker     if (block_size_high[left_bsize] * block_size_wide[left_bsize] >
4260*77c1e3ccSAndroid Build Coastguard Worker         cur_blk_area)
4261*77c1e3ccSAndroid Build Coastguard Worker       return true;
4262*77c1e3ccSAndroid Build Coastguard Worker   }
4263*77c1e3ccSAndroid Build Coastguard Worker 
4264*77c1e3ccSAndroid Build Coastguard Worker   if (xd->up_available) {
4265*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE above_bsize = xd->above_mbmi->bsize;
4266*77c1e3ccSAndroid Build Coastguard Worker     if (block_size_high[above_bsize] * block_size_wide[above_bsize] >
4267*77c1e3ccSAndroid Build Coastguard Worker         cur_blk_area)
4268*77c1e3ccSAndroid Build Coastguard Worker       return true;
4269*77c1e3ccSAndroid Build Coastguard Worker   }
4270*77c1e3ccSAndroid Build Coastguard Worker   return false;
4271*77c1e3ccSAndroid Build Coastguard Worker }
4272*77c1e3ccSAndroid Build Coastguard Worker 
prune_rect_part_using_none_pred_mode(const MACROBLOCKD * xd,PartitionSearchState * part_state,PREDICTION_MODE mode,BLOCK_SIZE bsize)4273*77c1e3ccSAndroid Build Coastguard Worker static inline void prune_rect_part_using_none_pred_mode(
4274*77c1e3ccSAndroid Build Coastguard Worker     const MACROBLOCKD *xd, PartitionSearchState *part_state,
4275*77c1e3ccSAndroid Build Coastguard Worker     PREDICTION_MODE mode, BLOCK_SIZE bsize) {
4276*77c1e3ccSAndroid Build Coastguard Worker   if (mode == DC_PRED || mode == SMOOTH_PRED) {
4277*77c1e3ccSAndroid Build Coastguard Worker     // If the prediction mode of NONE partition is either DC_PRED or
4278*77c1e3ccSAndroid Build Coastguard Worker     // SMOOTH_PRED, it indicates that the current block has less variation. In
4279*77c1e3ccSAndroid Build Coastguard Worker     // this case, HORZ and VERT partitions are pruned if at least one of left
4280*77c1e3ccSAndroid Build Coastguard Worker     // and top neighbor blocks is larger than the current block.
4281*77c1e3ccSAndroid Build Coastguard Worker     if (is_neighbor_blk_larger_than_cur_blk(xd, bsize)) {
4282*77c1e3ccSAndroid Build Coastguard Worker       part_state->prune_rect_part[HORZ] = 1;
4283*77c1e3ccSAndroid Build Coastguard Worker       part_state->prune_rect_part[VERT] = 1;
4284*77c1e3ccSAndroid Build Coastguard Worker     }
4285*77c1e3ccSAndroid Build Coastguard Worker   } else if (mode == D67_PRED || mode == V_PRED || mode == D113_PRED) {
4286*77c1e3ccSAndroid Build Coastguard Worker     // If the prediction mode chosen by NONE partition is close to 90 degrees,
4287*77c1e3ccSAndroid Build Coastguard Worker     // it implies a dominant vertical pattern, and the chance of choosing a
4288*77c1e3ccSAndroid Build Coastguard Worker     // vertical rectangular partition is high. Hence, horizontal partition is
4289*77c1e3ccSAndroid Build Coastguard Worker     // pruned in these cases.
4290*77c1e3ccSAndroid Build Coastguard Worker     part_state->prune_rect_part[HORZ] = 1;
4291*77c1e3ccSAndroid Build Coastguard Worker   } else if (mode == D157_PRED || mode == H_PRED || mode == D203_PRED) {
4292*77c1e3ccSAndroid Build Coastguard Worker     // If the prediction mode chosen by NONE partition is close to 180 degrees,
4293*77c1e3ccSAndroid Build Coastguard Worker     // it implies a dominant horizontal pattern, and the chance of choosing a
4294*77c1e3ccSAndroid Build Coastguard Worker     // horizontal rectangular partition is high. Hence, vertical partition is
4295*77c1e3ccSAndroid Build Coastguard Worker     // pruned in these cases.
4296*77c1e3ccSAndroid Build Coastguard Worker     part_state->prune_rect_part[VERT] = 1;
4297*77c1e3ccSAndroid Build Coastguard Worker   }
4298*77c1e3ccSAndroid Build Coastguard Worker }
4299*77c1e3ccSAndroid Build Coastguard Worker 
4300*77c1e3ccSAndroid Build Coastguard Worker // PARTITION_NONE search.
none_partition_search(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,MACROBLOCK * x,PC_TREE * pc_tree,SIMPLE_MOTION_DATA_TREE * sms_tree,RD_SEARCH_MACROBLOCK_CONTEXT * x_ctx,PartitionSearchState * part_search_state,RD_STATS * best_rdc,unsigned int * pb_source_variance,int64_t * none_rd,int64_t * part_none_rd)4301*77c1e3ccSAndroid Build Coastguard Worker static void none_partition_search(
4302*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, ThreadData *td, TileDataEnc *tile_data, MACROBLOCK *x,
4303*77c1e3ccSAndroid Build Coastguard Worker     PC_TREE *pc_tree, SIMPLE_MOTION_DATA_TREE *sms_tree,
4304*77c1e3ccSAndroid Build Coastguard Worker     RD_SEARCH_MACROBLOCK_CONTEXT *x_ctx,
4305*77c1e3ccSAndroid Build Coastguard Worker     PartitionSearchState *part_search_state, RD_STATS *best_rdc,
4306*77c1e3ccSAndroid Build Coastguard Worker     unsigned int *pb_source_variance, int64_t *none_rd, int64_t *part_none_rd) {
4307*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
4308*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state->part_blk_params;
4309*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS *this_rdc = &part_search_state->this_rdc;
4310*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params.mi_row;
4311*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col = blk_params.mi_col;
4312*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params.bsize;
4313*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize < BLOCK_SIZES_ALL);
4314*77c1e3ccSAndroid Build Coastguard Worker 
4315*77c1e3ccSAndroid Build Coastguard Worker   if (part_search_state->terminate_partition_search ||
4316*77c1e3ccSAndroid Build Coastguard Worker       !part_search_state->partition_none_allowed)
4317*77c1e3ccSAndroid Build Coastguard Worker     return;
4318*77c1e3ccSAndroid Build Coastguard Worker 
4319*77c1e3ccSAndroid Build Coastguard Worker   int pt_cost = 0;
4320*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS best_remain_rdcost;
4321*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&best_remain_rdcost);
4322*77c1e3ccSAndroid Build Coastguard Worker 
4323*77c1e3ccSAndroid Build Coastguard Worker   // Set PARTITION_NONE context and cost.
4324*77c1e3ccSAndroid Build Coastguard Worker   set_none_partition_params(cpi, td, x, pc_tree, part_search_state,
4325*77c1e3ccSAndroid Build Coastguard Worker                             &best_remain_rdcost, best_rdc, &pt_cost);
4326*77c1e3ccSAndroid Build Coastguard Worker 
4327*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
4328*77c1e3ccSAndroid Build Coastguard Worker   // Timer start for partition None.
4329*77c1e3ccSAndroid Build Coastguard Worker   PartitionTimingStats *part_timing_stats =
4330*77c1e3ccSAndroid Build Coastguard Worker       &part_search_state->part_timing_stats;
4331*77c1e3ccSAndroid Build Coastguard Worker   if (best_remain_rdcost.rdcost >= 0) {
4332*77c1e3ccSAndroid Build Coastguard Worker     start_partition_block_timer(part_timing_stats, PARTITION_NONE);
4333*77c1e3ccSAndroid Build Coastguard Worker   }
4334*77c1e3ccSAndroid Build Coastguard Worker #endif
4335*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_NONE evaluation and cost update.
4336*77c1e3ccSAndroid Build Coastguard Worker   pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, this_rdc, PARTITION_NONE,
4337*77c1e3ccSAndroid Build Coastguard Worker                 bsize, pc_tree->none, best_remain_rdcost);
4338*77c1e3ccSAndroid Build Coastguard Worker 
4339*77c1e3ccSAndroid Build Coastguard Worker   av1_rd_cost_update(x->rdmult, this_rdc);
4340*77c1e3ccSAndroid Build Coastguard Worker 
4341*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
4342*77c1e3ccSAndroid Build Coastguard Worker   // Timer end for partition None.
4343*77c1e3ccSAndroid Build Coastguard Worker   if (part_timing_stats->timer_is_on) {
4344*77c1e3ccSAndroid Build Coastguard Worker     RD_STATS tmp_rdc;
4345*77c1e3ccSAndroid Build Coastguard Worker     av1_init_rd_stats(&tmp_rdc);
4346*77c1e3ccSAndroid Build Coastguard Worker     if (this_rdc->rate != INT_MAX) {
4347*77c1e3ccSAndroid Build Coastguard Worker       tmp_rdc.rate = this_rdc->rate;
4348*77c1e3ccSAndroid Build Coastguard Worker       tmp_rdc.dist = this_rdc->dist;
4349*77c1e3ccSAndroid Build Coastguard Worker       tmp_rdc.rdcost = this_rdc->rdcost;
4350*77c1e3ccSAndroid Build Coastguard Worker       if (blk_params.bsize_at_least_8x8) {
4351*77c1e3ccSAndroid Build Coastguard Worker         tmp_rdc.rate += pt_cost;
4352*77c1e3ccSAndroid Build Coastguard Worker         tmp_rdc.rdcost = RDCOST(x->rdmult, tmp_rdc.rate, tmp_rdc.dist);
4353*77c1e3ccSAndroid Build Coastguard Worker       }
4354*77c1e3ccSAndroid Build Coastguard Worker     }
4355*77c1e3ccSAndroid Build Coastguard Worker     end_partition_block_timer(part_timing_stats, PARTITION_NONE,
4356*77c1e3ccSAndroid Build Coastguard Worker                               tmp_rdc.rdcost);
4357*77c1e3ccSAndroid Build Coastguard Worker   }
4358*77c1e3ccSAndroid Build Coastguard Worker #endif
4359*77c1e3ccSAndroid Build Coastguard Worker   *pb_source_variance = x->source_variance;
4360*77c1e3ccSAndroid Build Coastguard Worker   if (none_rd) *none_rd = this_rdc->rdcost;
4361*77c1e3ccSAndroid Build Coastguard Worker   part_search_state->none_rd = this_rdc->rdcost;
4362*77c1e3ccSAndroid Build Coastguard Worker   if (this_rdc->rate != INT_MAX) {
4363*77c1e3ccSAndroid Build Coastguard Worker     // Record picked ref frame to prune ref frames for other partition types.
4364*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->sf.inter_sf.prune_ref_frame_for_rect_partitions) {
4365*77c1e3ccSAndroid Build Coastguard Worker       const int ref_type = av1_ref_frame_type(pc_tree->none->mic.ref_frame);
4366*77c1e3ccSAndroid Build Coastguard Worker       av1_update_picked_ref_frames_mask(
4367*77c1e3ccSAndroid Build Coastguard Worker           x, ref_type, bsize, cm->seq_params->mib_size, mi_row, mi_col);
4368*77c1e3ccSAndroid Build Coastguard Worker     }
4369*77c1e3ccSAndroid Build Coastguard Worker 
4370*77c1e3ccSAndroid Build Coastguard Worker     // Calculate the total cost and update the best partition.
4371*77c1e3ccSAndroid Build Coastguard Worker     if (blk_params.bsize_at_least_8x8) {
4372*77c1e3ccSAndroid Build Coastguard Worker       this_rdc->rate += pt_cost;
4373*77c1e3ccSAndroid Build Coastguard Worker       this_rdc->rdcost = RDCOST(x->rdmult, this_rdc->rate, this_rdc->dist);
4374*77c1e3ccSAndroid Build Coastguard Worker     }
4375*77c1e3ccSAndroid Build Coastguard Worker     *part_none_rd = this_rdc->rdcost;
4376*77c1e3ccSAndroid Build Coastguard Worker     if (this_rdc->rdcost < best_rdc->rdcost) {
4377*77c1e3ccSAndroid Build Coastguard Worker       *best_rdc = *this_rdc;
4378*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->found_best_partition = true;
4379*77c1e3ccSAndroid Build Coastguard Worker       if (blk_params.bsize_at_least_8x8) {
4380*77c1e3ccSAndroid Build Coastguard Worker         pc_tree->partitioning = PARTITION_NONE;
4381*77c1e3ccSAndroid Build Coastguard Worker       }
4382*77c1e3ccSAndroid Build Coastguard Worker 
4383*77c1e3ccSAndroid Build Coastguard Worker       // Disable split and rectangular partition search
4384*77c1e3ccSAndroid Build Coastguard Worker       // based on PARTITION_NONE cost.
4385*77c1e3ccSAndroid Build Coastguard Worker       prune_partitions_after_none(cpi, x, sms_tree, pc_tree->none,
4386*77c1e3ccSAndroid Build Coastguard Worker                                   part_search_state, best_rdc,
4387*77c1e3ccSAndroid Build Coastguard Worker                                   pb_source_variance);
4388*77c1e3ccSAndroid Build Coastguard Worker     }
4389*77c1e3ccSAndroid Build Coastguard Worker 
4390*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->sf.part_sf.prune_rect_part_using_none_pred_mode)
4391*77c1e3ccSAndroid Build Coastguard Worker       prune_rect_part_using_none_pred_mode(&x->e_mbd, part_search_state,
4392*77c1e3ccSAndroid Build Coastguard Worker                                            pc_tree->none->mic.mode, bsize);
4393*77c1e3ccSAndroid Build Coastguard Worker   }
4394*77c1e3ccSAndroid Build Coastguard Worker   av1_restore_context(x, x_ctx, mi_row, mi_col, bsize, av1_num_planes(cm));
4395*77c1e3ccSAndroid Build Coastguard Worker }
4396*77c1e3ccSAndroid Build Coastguard Worker 
4397*77c1e3ccSAndroid Build Coastguard Worker // PARTITION_SPLIT search.
split_partition_search(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,MACROBLOCK * x,PC_TREE * pc_tree,SIMPLE_MOTION_DATA_TREE * sms_tree,RD_SEARCH_MACROBLOCK_CONTEXT * x_ctx,PartitionSearchState * part_search_state,RD_STATS * best_rdc,SB_MULTI_PASS_MODE multi_pass_mode,int64_t * part_split_rd)4398*77c1e3ccSAndroid Build Coastguard Worker static void split_partition_search(
4399*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, ThreadData *td, TileDataEnc *tile_data,
4400*77c1e3ccSAndroid Build Coastguard Worker     TokenExtra **tp, MACROBLOCK *x, PC_TREE *pc_tree,
4401*77c1e3ccSAndroid Build Coastguard Worker     SIMPLE_MOTION_DATA_TREE *sms_tree, RD_SEARCH_MACROBLOCK_CONTEXT *x_ctx,
4402*77c1e3ccSAndroid Build Coastguard Worker     PartitionSearchState *part_search_state, RD_STATS *best_rdc,
4403*77c1e3ccSAndroid Build Coastguard Worker     SB_MULTI_PASS_MODE multi_pass_mode, int64_t *part_split_rd) {
4404*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
4405*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state->part_blk_params;
4406*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
4407*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = blk_params.mi_row;
4408*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col = blk_params.mi_col;
4409*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = blk_params.bsize;
4410*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize < BLOCK_SIZES_ALL);
4411*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS sum_rdc = part_search_state->sum_rdc;
4412*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
4413*77c1e3ccSAndroid Build Coastguard Worker 
4414*77c1e3ccSAndroid Build Coastguard Worker   // Check if partition split is allowed.
4415*77c1e3ccSAndroid Build Coastguard Worker   if (part_search_state->terminate_partition_search ||
4416*77c1e3ccSAndroid Build Coastguard Worker       !part_search_state->do_square_split)
4417*77c1e3ccSAndroid Build Coastguard Worker     return;
4418*77c1e3ccSAndroid Build Coastguard Worker 
4419*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
4420*77c1e3ccSAndroid Build Coastguard Worker     if (pc_tree->split[i] == NULL)
4421*77c1e3ccSAndroid Build Coastguard Worker       pc_tree->split[i] = av1_alloc_pc_tree_node(subsize);
4422*77c1e3ccSAndroid Build Coastguard Worker     if (!pc_tree->split[i])
4423*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(x->e_mbd.error_info, AOM_CODEC_MEM_ERROR,
4424*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PC_TREE");
4425*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->split[i]->index = i;
4426*77c1e3ccSAndroid Build Coastguard Worker   }
4427*77c1e3ccSAndroid Build Coastguard Worker 
4428*77c1e3ccSAndroid Build Coastguard Worker   // Initialization of this partition RD stats.
4429*77c1e3ccSAndroid Build Coastguard Worker   av1_init_rd_stats(&sum_rdc);
4430*77c1e3ccSAndroid Build Coastguard Worker   sum_rdc.rate = part_search_state->partition_cost[PARTITION_SPLIT];
4431*77c1e3ccSAndroid Build Coastguard Worker   sum_rdc.rdcost = RDCOST(x->rdmult, sum_rdc.rate, 0);
4432*77c1e3ccSAndroid Build Coastguard Worker 
4433*77c1e3ccSAndroid Build Coastguard Worker   int idx;
4434*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
4435*77c1e3ccSAndroid Build Coastguard Worker   PartitionTimingStats *part_timing_stats =
4436*77c1e3ccSAndroid Build Coastguard Worker       &part_search_state->part_timing_stats;
4437*77c1e3ccSAndroid Build Coastguard Worker   if (best_rdc->rdcost - sum_rdc.rdcost >= 0) {
4438*77c1e3ccSAndroid Build Coastguard Worker     start_partition_block_timer(part_timing_stats, PARTITION_SPLIT);
4439*77c1e3ccSAndroid Build Coastguard Worker   }
4440*77c1e3ccSAndroid Build Coastguard Worker #endif
4441*77c1e3ccSAndroid Build Coastguard Worker   // Recursive partition search on 4 sub-blocks.
4442*77c1e3ccSAndroid Build Coastguard Worker   for (idx = 0; idx < SUB_PARTITIONS_SPLIT && sum_rdc.rdcost < best_rdc->rdcost;
4443*77c1e3ccSAndroid Build Coastguard Worker        ++idx) {
4444*77c1e3ccSAndroid Build Coastguard Worker     const int x_idx = (idx & 1) * blk_params.mi_step;
4445*77c1e3ccSAndroid Build Coastguard Worker     const int y_idx = (idx >> 1) * blk_params.mi_step;
4446*77c1e3ccSAndroid Build Coastguard Worker 
4447*77c1e3ccSAndroid Build Coastguard Worker     if (mi_row + y_idx >= mi_params->mi_rows ||
4448*77c1e3ccSAndroid Build Coastguard Worker         mi_col + x_idx >= mi_params->mi_cols)
4449*77c1e3ccSAndroid Build Coastguard Worker       continue;
4450*77c1e3ccSAndroid Build Coastguard Worker 
4451*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->split[idx]->index = idx;
4452*77c1e3ccSAndroid Build Coastguard Worker     int64_t *p_split_rd = &part_search_state->split_rd[idx];
4453*77c1e3ccSAndroid Build Coastguard Worker     RD_STATS best_remain_rdcost;
4454*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_stats_subtraction(x->rdmult, best_rdc, &sum_rdc,
4455*77c1e3ccSAndroid Build Coastguard Worker                              &best_remain_rdcost);
4456*77c1e3ccSAndroid Build Coastguard Worker 
4457*77c1e3ccSAndroid Build Coastguard Worker     int curr_quad_tree_idx = 0;
4458*77c1e3ccSAndroid Build Coastguard Worker     if (frame_is_intra_only(cm) && bsize <= BLOCK_64X64) {
4459*77c1e3ccSAndroid Build Coastguard Worker       curr_quad_tree_idx = part_search_state->intra_part_info->quad_tree_idx;
4460*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->intra_part_info->quad_tree_idx =
4461*77c1e3ccSAndroid Build Coastguard Worker           4 * curr_quad_tree_idx + idx + 1;
4462*77c1e3ccSAndroid Build Coastguard Worker     }
4463*77c1e3ccSAndroid Build Coastguard Worker     // Split partition evaluation of corresponding idx.
4464*77c1e3ccSAndroid Build Coastguard Worker     // If the RD cost exceeds the best cost then do not
4465*77c1e3ccSAndroid Build Coastguard Worker     // evaluate other split sub-partitions.
4466*77c1e3ccSAndroid Build Coastguard Worker     SIMPLE_MOTION_DATA_TREE *const sms_tree_split =
4467*77c1e3ccSAndroid Build Coastguard Worker         (sms_tree == NULL) ? NULL : sms_tree->split[idx];
4468*77c1e3ccSAndroid Build Coastguard Worker     if (!av1_rd_pick_partition(
4469*77c1e3ccSAndroid Build Coastguard Worker             cpi, td, tile_data, tp, mi_row + y_idx, mi_col + x_idx, subsize,
4470*77c1e3ccSAndroid Build Coastguard Worker             &part_search_state->this_rdc, best_remain_rdcost,
4471*77c1e3ccSAndroid Build Coastguard Worker             pc_tree->split[idx], sms_tree_split, p_split_rd, multi_pass_mode,
4472*77c1e3ccSAndroid Build Coastguard Worker             &part_search_state->split_part_rect_win[idx])) {
4473*77c1e3ccSAndroid Build Coastguard Worker       av1_invalid_rd_stats(&sum_rdc);
4474*77c1e3ccSAndroid Build Coastguard Worker       break;
4475*77c1e3ccSAndroid Build Coastguard Worker     }
4476*77c1e3ccSAndroid Build Coastguard Worker     if (frame_is_intra_only(cm) && bsize <= BLOCK_64X64) {
4477*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->intra_part_info->quad_tree_idx = curr_quad_tree_idx;
4478*77c1e3ccSAndroid Build Coastguard Worker     }
4479*77c1e3ccSAndroid Build Coastguard Worker 
4480*77c1e3ccSAndroid Build Coastguard Worker     sum_rdc.rate += part_search_state->this_rdc.rate;
4481*77c1e3ccSAndroid Build Coastguard Worker     sum_rdc.dist += part_search_state->this_rdc.dist;
4482*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_cost_update(x->rdmult, &sum_rdc);
4483*77c1e3ccSAndroid Build Coastguard Worker 
4484*77c1e3ccSAndroid Build Coastguard Worker     // Set split ctx as ready for use.
4485*77c1e3ccSAndroid Build Coastguard Worker     if (idx <= 1 && (bsize <= BLOCK_8X8 ||
4486*77c1e3ccSAndroid Build Coastguard Worker                      pc_tree->split[idx]->partitioning == PARTITION_NONE)) {
4487*77c1e3ccSAndroid Build Coastguard Worker       const MB_MODE_INFO *const mbmi = &pc_tree->split[idx]->none->mic;
4488*77c1e3ccSAndroid Build Coastguard Worker       const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
4489*77c1e3ccSAndroid Build Coastguard Worker       // Neither palette mode nor cfl predicted.
4490*77c1e3ccSAndroid Build Coastguard Worker       if (pmi->palette_size[0] == 0 && pmi->palette_size[1] == 0) {
4491*77c1e3ccSAndroid Build Coastguard Worker         if (mbmi->uv_mode != UV_CFL_PRED)
4492*77c1e3ccSAndroid Build Coastguard Worker           part_search_state->is_split_ctx_is_ready[idx] = 1;
4493*77c1e3ccSAndroid Build Coastguard Worker       }
4494*77c1e3ccSAndroid Build Coastguard Worker     }
4495*77c1e3ccSAndroid Build Coastguard Worker   }
4496*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
4497*77c1e3ccSAndroid Build Coastguard Worker   if (part_timing_stats->timer_is_on) {
4498*77c1e3ccSAndroid Build Coastguard Worker     end_partition_block_timer(part_timing_stats, PARTITION_SPLIT,
4499*77c1e3ccSAndroid Build Coastguard Worker                               sum_rdc.rdcost);
4500*77c1e3ccSAndroid Build Coastguard Worker   }
4501*77c1e3ccSAndroid Build Coastguard Worker #endif
4502*77c1e3ccSAndroid Build Coastguard Worker   const int reached_last_index = (idx == SUB_PARTITIONS_SPLIT);
4503*77c1e3ccSAndroid Build Coastguard Worker 
4504*77c1e3ccSAndroid Build Coastguard Worker   // Calculate the total cost and update the best partition.
4505*77c1e3ccSAndroid Build Coastguard Worker   *part_split_rd = sum_rdc.rdcost;
4506*77c1e3ccSAndroid Build Coastguard Worker   if (reached_last_index && sum_rdc.rdcost < best_rdc->rdcost) {
4507*77c1e3ccSAndroid Build Coastguard Worker     sum_rdc.rdcost = RDCOST(x->rdmult, sum_rdc.rate, sum_rdc.dist);
4508*77c1e3ccSAndroid Build Coastguard Worker     if (sum_rdc.rdcost < best_rdc->rdcost) {
4509*77c1e3ccSAndroid Build Coastguard Worker       *best_rdc = sum_rdc;
4510*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->found_best_partition = true;
4511*77c1e3ccSAndroid Build Coastguard Worker       pc_tree->partitioning = PARTITION_SPLIT;
4512*77c1e3ccSAndroid Build Coastguard Worker     }
4513*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->sf.part_sf.less_rectangular_check_level > 0) {
4514*77c1e3ccSAndroid Build Coastguard Worker     // Skip rectangular partition test when partition type none gives better
4515*77c1e3ccSAndroid Build Coastguard Worker     // rd than partition type split.
4516*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->sf.part_sf.less_rectangular_check_level == 2 || idx <= 2) {
4517*77c1e3ccSAndroid Build Coastguard Worker       const int partition_none_valid = part_search_state->none_rd > 0;
4518*77c1e3ccSAndroid Build Coastguard Worker       const int partition_none_better =
4519*77c1e3ccSAndroid Build Coastguard Worker           part_search_state->none_rd < sum_rdc.rdcost;
4520*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->do_rectangular_split &=
4521*77c1e3ccSAndroid Build Coastguard Worker           !(partition_none_valid && partition_none_better);
4522*77c1e3ccSAndroid Build Coastguard Worker     }
4523*77c1e3ccSAndroid Build Coastguard Worker   }
4524*77c1e3ccSAndroid Build Coastguard Worker   // Restore the context for the following cases:
4525*77c1e3ccSAndroid Build Coastguard Worker   // 1) Current block size not more than maximum partition size as dry run
4526*77c1e3ccSAndroid Build Coastguard Worker   // encode happens for these cases
4527*77c1e3ccSAndroid Build Coastguard Worker   // 2) Current block size same as superblock size as the final encode
4528*77c1e3ccSAndroid Build Coastguard Worker   // happens for this case
4529*77c1e3ccSAndroid Build Coastguard Worker   if (bsize <= x->sb_enc.max_partition_size || bsize == cm->seq_params->sb_size)
4530*77c1e3ccSAndroid Build Coastguard Worker     av1_restore_context(x, x_ctx, mi_row, mi_col, bsize, av1_num_planes(cm));
4531*77c1e3ccSAndroid Build Coastguard Worker }
4532*77c1e3ccSAndroid Build Coastguard Worker 
4533*77c1e3ccSAndroid Build Coastguard Worker // The max number of nodes in the partition tree.
4534*77c1e3ccSAndroid Build Coastguard Worker // The number of leaf nodes is (128x128) / (4x4) = 1024.
4535*77c1e3ccSAndroid Build Coastguard Worker // The number of All possible parent nodes is 1 + 2 + ... + 512 = 1023.
4536*77c1e3ccSAndroid Build Coastguard Worker #define NUM_NODES 2048
4537*77c1e3ccSAndroid Build Coastguard Worker 
write_partition_tree(AV1_COMP * const cpi,const PC_TREE * const pc_tree,const BLOCK_SIZE bsize,const int mi_row,const int mi_col)4538*77c1e3ccSAndroid Build Coastguard Worker static void write_partition_tree(AV1_COMP *const cpi,
4539*77c1e3ccSAndroid Build Coastguard Worker                                  const PC_TREE *const pc_tree,
4540*77c1e3ccSAndroid Build Coastguard Worker                                  const BLOCK_SIZE bsize, const int mi_row,
4541*77c1e3ccSAndroid Build Coastguard Worker                                  const int mi_col) {
4542*77c1e3ccSAndroid Build Coastguard Worker   (void)mi_row;
4543*77c1e3ccSAndroid Build Coastguard Worker   (void)mi_col;
4544*77c1e3ccSAndroid Build Coastguard Worker   const char *path = cpi->oxcf.partition_info_path;
4545*77c1e3ccSAndroid Build Coastguard Worker   char filename[256];
4546*77c1e3ccSAndroid Build Coastguard Worker   snprintf(filename, sizeof(filename), "%s/partition_tree_sb%d_c%d", path,
4547*77c1e3ccSAndroid Build Coastguard Worker            cpi->sb_counter, 0);
4548*77c1e3ccSAndroid Build Coastguard Worker   FILE *pfile = fopen(filename, "w");
4549*77c1e3ccSAndroid Build Coastguard Worker   fprintf(pfile, "%d", bsize);
4550*77c1e3ccSAndroid Build Coastguard Worker 
4551*77c1e3ccSAndroid Build Coastguard Worker   // Write partition type with BFS order.
4552*77c1e3ccSAndroid Build Coastguard Worker   const PC_TREE *tree_node_queue[NUM_NODES] = { NULL };
4553*77c1e3ccSAndroid Build Coastguard Worker   int q_idx = 0;
4554*77c1e3ccSAndroid Build Coastguard Worker   int last_idx = 1;
4555*77c1e3ccSAndroid Build Coastguard Worker   int num_nodes = 1;
4556*77c1e3ccSAndroid Build Coastguard Worker 
4557*77c1e3ccSAndroid Build Coastguard Worker   // First traversal to get number of leaf nodes.
4558*77c1e3ccSAndroid Build Coastguard Worker   tree_node_queue[q_idx] = pc_tree;
4559*77c1e3ccSAndroid Build Coastguard Worker   while (num_nodes > 0) {
4560*77c1e3ccSAndroid Build Coastguard Worker     const PC_TREE *node = tree_node_queue[q_idx];
4561*77c1e3ccSAndroid Build Coastguard Worker     if (node->partitioning == PARTITION_SPLIT) {
4562*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < 4; ++i) {
4563*77c1e3ccSAndroid Build Coastguard Worker         tree_node_queue[last_idx] = node->split[i];
4564*77c1e3ccSAndroid Build Coastguard Worker         ++last_idx;
4565*77c1e3ccSAndroid Build Coastguard Worker       }
4566*77c1e3ccSAndroid Build Coastguard Worker       num_nodes += 4;
4567*77c1e3ccSAndroid Build Coastguard Worker     }
4568*77c1e3ccSAndroid Build Coastguard Worker     --num_nodes;
4569*77c1e3ccSAndroid Build Coastguard Worker     ++q_idx;
4570*77c1e3ccSAndroid Build Coastguard Worker   }
4571*77c1e3ccSAndroid Build Coastguard Worker   const int num_leafs = last_idx;
4572*77c1e3ccSAndroid Build Coastguard Worker   fprintf(pfile, ",%d,%d", num_leafs, /*num_configs=*/1);
4573*77c1e3ccSAndroid Build Coastguard Worker 
4574*77c1e3ccSAndroid Build Coastguard Worker   // Write partitions for each node.
4575*77c1e3ccSAndroid Build Coastguard Worker   q_idx = 0;
4576*77c1e3ccSAndroid Build Coastguard Worker   last_idx = 1;
4577*77c1e3ccSAndroid Build Coastguard Worker   num_nodes = 1;
4578*77c1e3ccSAndroid Build Coastguard Worker   tree_node_queue[q_idx] = pc_tree;
4579*77c1e3ccSAndroid Build Coastguard Worker   while (num_nodes > 0) {
4580*77c1e3ccSAndroid Build Coastguard Worker     const PC_TREE *node = tree_node_queue[q_idx];
4581*77c1e3ccSAndroid Build Coastguard Worker     fprintf(pfile, ",%d", node->partitioning);
4582*77c1e3ccSAndroid Build Coastguard Worker     if (node->partitioning == PARTITION_SPLIT) {
4583*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < 4; ++i) {
4584*77c1e3ccSAndroid Build Coastguard Worker         tree_node_queue[last_idx] = node->split[i];
4585*77c1e3ccSAndroid Build Coastguard Worker         ++last_idx;
4586*77c1e3ccSAndroid Build Coastguard Worker       }
4587*77c1e3ccSAndroid Build Coastguard Worker       num_nodes += 4;
4588*77c1e3ccSAndroid Build Coastguard Worker     }
4589*77c1e3ccSAndroid Build Coastguard Worker     --num_nodes;
4590*77c1e3ccSAndroid Build Coastguard Worker     ++q_idx;
4591*77c1e3ccSAndroid Build Coastguard Worker   }
4592*77c1e3ccSAndroid Build Coastguard Worker   fprintf(pfile, "\n");
4593*77c1e3ccSAndroid Build Coastguard Worker 
4594*77c1e3ccSAndroid Build Coastguard Worker   fclose(pfile);
4595*77c1e3ccSAndroid Build Coastguard Worker }
4596*77c1e3ccSAndroid Build Coastguard Worker 
4597*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_PARTITION_SEARCH_ORDER
verify_write_partition_tree(const AV1_COMP * const cpi,const PC_TREE * const pc_tree,const BLOCK_SIZE bsize,const int config_id,const int mi_row,const int mi_col)4598*77c1e3ccSAndroid Build Coastguard Worker static void verify_write_partition_tree(const AV1_COMP *const cpi,
4599*77c1e3ccSAndroid Build Coastguard Worker                                         const PC_TREE *const pc_tree,
4600*77c1e3ccSAndroid Build Coastguard Worker                                         const BLOCK_SIZE bsize,
4601*77c1e3ccSAndroid Build Coastguard Worker                                         const int config_id, const int mi_row,
4602*77c1e3ccSAndroid Build Coastguard Worker                                         const int mi_col) {
4603*77c1e3ccSAndroid Build Coastguard Worker   (void)mi_row;
4604*77c1e3ccSAndroid Build Coastguard Worker   (void)mi_col;
4605*77c1e3ccSAndroid Build Coastguard Worker   const char *path = cpi->oxcf.partition_info_path;
4606*77c1e3ccSAndroid Build Coastguard Worker   char filename[256];
4607*77c1e3ccSAndroid Build Coastguard Worker   snprintf(filename, sizeof(filename), "%s/verify_partition_tree_sb%d_c%d",
4608*77c1e3ccSAndroid Build Coastguard Worker            path, cpi->sb_counter, config_id);
4609*77c1e3ccSAndroid Build Coastguard Worker   FILE *pfile = fopen(filename, "w");
4610*77c1e3ccSAndroid Build Coastguard Worker   fprintf(pfile, "%d", bsize);
4611*77c1e3ccSAndroid Build Coastguard Worker 
4612*77c1e3ccSAndroid Build Coastguard Worker   // Write partition type with BFS order.
4613*77c1e3ccSAndroid Build Coastguard Worker   const PC_TREE *tree_node_queue[NUM_NODES] = { NULL };
4614*77c1e3ccSAndroid Build Coastguard Worker   int q_idx = 0;
4615*77c1e3ccSAndroid Build Coastguard Worker   int last_idx = 1;
4616*77c1e3ccSAndroid Build Coastguard Worker   int num_nodes = 1;
4617*77c1e3ccSAndroid Build Coastguard Worker 
4618*77c1e3ccSAndroid Build Coastguard Worker   // First traversal to get number of leaf nodes.
4619*77c1e3ccSAndroid Build Coastguard Worker   tree_node_queue[q_idx] = pc_tree;
4620*77c1e3ccSAndroid Build Coastguard Worker   while (num_nodes > 0) {
4621*77c1e3ccSAndroid Build Coastguard Worker     const PC_TREE *node = tree_node_queue[q_idx];
4622*77c1e3ccSAndroid Build Coastguard Worker     if (node != NULL && node->partitioning == PARTITION_SPLIT) {
4623*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < 4; ++i) {
4624*77c1e3ccSAndroid Build Coastguard Worker         tree_node_queue[last_idx] = node->split[i];
4625*77c1e3ccSAndroid Build Coastguard Worker         ++last_idx;
4626*77c1e3ccSAndroid Build Coastguard Worker       }
4627*77c1e3ccSAndroid Build Coastguard Worker       num_nodes += 4;
4628*77c1e3ccSAndroid Build Coastguard Worker     }
4629*77c1e3ccSAndroid Build Coastguard Worker     --num_nodes;
4630*77c1e3ccSAndroid Build Coastguard Worker     ++q_idx;
4631*77c1e3ccSAndroid Build Coastguard Worker   }
4632*77c1e3ccSAndroid Build Coastguard Worker   const int num_leafs = last_idx;
4633*77c1e3ccSAndroid Build Coastguard Worker   fprintf(pfile, ",%d,%d", num_leafs, /*num_configs=*/1);
4634*77c1e3ccSAndroid Build Coastguard Worker 
4635*77c1e3ccSAndroid Build Coastguard Worker   // Write partitions for each node.
4636*77c1e3ccSAndroid Build Coastguard Worker   q_idx = 0;
4637*77c1e3ccSAndroid Build Coastguard Worker   last_idx = 1;
4638*77c1e3ccSAndroid Build Coastguard Worker   num_nodes = 1;
4639*77c1e3ccSAndroid Build Coastguard Worker   tree_node_queue[q_idx] = pc_tree;
4640*77c1e3ccSAndroid Build Coastguard Worker   while (num_nodes > 0) {
4641*77c1e3ccSAndroid Build Coastguard Worker     const PC_TREE *node = tree_node_queue[q_idx];
4642*77c1e3ccSAndroid Build Coastguard Worker     if (node != NULL) {  // suppress warning
4643*77c1e3ccSAndroid Build Coastguard Worker       fprintf(pfile, ",%d", node->partitioning);
4644*77c1e3ccSAndroid Build Coastguard Worker       if (node->partitioning == PARTITION_SPLIT) {
4645*77c1e3ccSAndroid Build Coastguard Worker         for (int i = 0; i < 4; ++i) {
4646*77c1e3ccSAndroid Build Coastguard Worker           tree_node_queue[last_idx] = node->split[i];
4647*77c1e3ccSAndroid Build Coastguard Worker           ++last_idx;
4648*77c1e3ccSAndroid Build Coastguard Worker         }
4649*77c1e3ccSAndroid Build Coastguard Worker         num_nodes += 4;
4650*77c1e3ccSAndroid Build Coastguard Worker       }
4651*77c1e3ccSAndroid Build Coastguard Worker     }
4652*77c1e3ccSAndroid Build Coastguard Worker     --num_nodes;
4653*77c1e3ccSAndroid Build Coastguard Worker     ++q_idx;
4654*77c1e3ccSAndroid Build Coastguard Worker   }
4655*77c1e3ccSAndroid Build Coastguard Worker   fprintf(pfile, "\n");
4656*77c1e3ccSAndroid Build Coastguard Worker 
4657*77c1e3ccSAndroid Build Coastguard Worker   fclose(pfile);
4658*77c1e3ccSAndroid Build Coastguard Worker }
4659*77c1e3ccSAndroid Build Coastguard Worker 
read_partition_tree(AV1_COMP * const cpi,PC_TREE * const pc_tree,struct aom_internal_error_info * error_info,const int config_id)4660*77c1e3ccSAndroid Build Coastguard Worker static int read_partition_tree(AV1_COMP *const cpi, PC_TREE *const pc_tree,
4661*77c1e3ccSAndroid Build Coastguard Worker                                struct aom_internal_error_info *error_info,
4662*77c1e3ccSAndroid Build Coastguard Worker                                const int config_id) {
4663*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
4664*77c1e3ccSAndroid Build Coastguard Worker   const char *path = cpi->oxcf.partition_info_path;
4665*77c1e3ccSAndroid Build Coastguard Worker   char filename[256];
4666*77c1e3ccSAndroid Build Coastguard Worker   snprintf(filename, sizeof(filename), "%s/partition_tree_sb%d_c%d", path,
4667*77c1e3ccSAndroid Build Coastguard Worker            cpi->sb_counter, config_id);
4668*77c1e3ccSAndroid Build Coastguard Worker   FILE *pfile = fopen(filename, "r");
4669*77c1e3ccSAndroid Build Coastguard Worker   if (pfile == NULL) {
4670*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(cm->error, AOM_CODEC_ERROR, "Can't find input file: %s.",
4671*77c1e3ccSAndroid Build Coastguard Worker                        filename);
4672*77c1e3ccSAndroid Build Coastguard Worker   }
4673*77c1e3ccSAndroid Build Coastguard Worker 
4674*77c1e3ccSAndroid Build Coastguard Worker   int read_bsize;
4675*77c1e3ccSAndroid Build Coastguard Worker   int num_nodes;
4676*77c1e3ccSAndroid Build Coastguard Worker   int num_configs;
4677*77c1e3ccSAndroid Build Coastguard Worker   fscanf(pfile, "%d,%d,%d", &read_bsize, &num_nodes, &num_configs);
4678*77c1e3ccSAndroid Build Coastguard Worker   assert(read_bsize == cpi->common.seq_params->sb_size);
4679*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE bsize = (BLOCK_SIZE)read_bsize;
4680*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize == pc_tree->block_size);
4681*77c1e3ccSAndroid Build Coastguard Worker 
4682*77c1e3ccSAndroid Build Coastguard Worker   PC_TREE *tree_node_queue[NUM_NODES] = { NULL };
4683*77c1e3ccSAndroid Build Coastguard Worker   int last_idx = 1;
4684*77c1e3ccSAndroid Build Coastguard Worker   int q_idx = 0;
4685*77c1e3ccSAndroid Build Coastguard Worker   tree_node_queue[q_idx] = pc_tree;
4686*77c1e3ccSAndroid Build Coastguard Worker   while (num_nodes > 0) {
4687*77c1e3ccSAndroid Build Coastguard Worker     int partitioning;
4688*77c1e3ccSAndroid Build Coastguard Worker     fscanf(pfile, ",%d", &partitioning);
4689*77c1e3ccSAndroid Build Coastguard Worker     assert(partitioning >= PARTITION_NONE &&
4690*77c1e3ccSAndroid Build Coastguard Worker            partitioning < EXT_PARTITION_TYPES);
4691*77c1e3ccSAndroid Build Coastguard Worker     PC_TREE *node = tree_node_queue[q_idx];
4692*77c1e3ccSAndroid Build Coastguard Worker     if (node != NULL) {
4693*77c1e3ccSAndroid Build Coastguard Worker       node->partitioning = partitioning;
4694*77c1e3ccSAndroid Build Coastguard Worker       bsize = node->block_size;
4695*77c1e3ccSAndroid Build Coastguard Worker     }
4696*77c1e3ccSAndroid Build Coastguard Worker     if (partitioning == PARTITION_SPLIT) {
4697*77c1e3ccSAndroid Build Coastguard Worker       const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
4698*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < 4; ++i) {
4699*77c1e3ccSAndroid Build Coastguard Worker         if (node != NULL) {  // Suppress warning
4700*77c1e3ccSAndroid Build Coastguard Worker           node->split[i] = av1_alloc_pc_tree_node(subsize);
4701*77c1e3ccSAndroid Build Coastguard Worker           if (!node->split[i])
4702*77c1e3ccSAndroid Build Coastguard Worker             aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
4703*77c1e3ccSAndroid Build Coastguard Worker                                "Failed to allocate PC_TREE");
4704*77c1e3ccSAndroid Build Coastguard Worker           node->split[i]->index = i;
4705*77c1e3ccSAndroid Build Coastguard Worker           tree_node_queue[last_idx] = node->split[i];
4706*77c1e3ccSAndroid Build Coastguard Worker           ++last_idx;
4707*77c1e3ccSAndroid Build Coastguard Worker         }
4708*77c1e3ccSAndroid Build Coastguard Worker       }
4709*77c1e3ccSAndroid Build Coastguard Worker     }
4710*77c1e3ccSAndroid Build Coastguard Worker     --num_nodes;
4711*77c1e3ccSAndroid Build Coastguard Worker     ++q_idx;
4712*77c1e3ccSAndroid Build Coastguard Worker   }
4713*77c1e3ccSAndroid Build Coastguard Worker   fclose(pfile);
4714*77c1e3ccSAndroid Build Coastguard Worker 
4715*77c1e3ccSAndroid Build Coastguard Worker   return num_configs;
4716*77c1e3ccSAndroid Build Coastguard Worker }
4717*77c1e3ccSAndroid Build Coastguard Worker 
rd_search_for_fixed_partition(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,SIMPLE_MOTION_DATA_TREE * sms_tree,int mi_row,int mi_col,const BLOCK_SIZE bsize,PC_TREE * pc_tree)4718*77c1e3ccSAndroid Build Coastguard Worker static RD_STATS rd_search_for_fixed_partition(
4719*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, ThreadData *td, TileDataEnc *tile_data,
4720*77c1e3ccSAndroid Build Coastguard Worker     TokenExtra **tp, SIMPLE_MOTION_DATA_TREE *sms_tree, int mi_row, int mi_col,
4721*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE bsize, PC_TREE *pc_tree) {
4722*77c1e3ccSAndroid Build Coastguard Worker   const PARTITION_TYPE partition = pc_tree->partitioning;
4723*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
4724*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
4725*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
4726*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
4727*77c1e3ccSAndroid Build Coastguard Worker   TileInfo *const tile_info = &tile_data->tile_info;
4728*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS best_rdc;
4729*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&best_rdc);
4730*77c1e3ccSAndroid Build Coastguard Worker   int sum_subblock_rate = 0;
4731*77c1e3ccSAndroid Build Coastguard Worker   int64_t sum_subblock_dist = 0;
4732*77c1e3ccSAndroid Build Coastguard Worker   PartitionSearchState part_search_state;
4733*77c1e3ccSAndroid Build Coastguard Worker   init_partition_search_state_params(x, cpi, &part_search_state, mi_row, mi_col,
4734*77c1e3ccSAndroid Build Coastguard Worker                                      bsize);
4735*77c1e3ccSAndroid Build Coastguard Worker   // Override partition costs at the edges of the frame in the same
4736*77c1e3ccSAndroid Build Coastguard Worker   // way as in read_partition (see decodeframe.c).
4737*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state.part_blk_params;
4738*77c1e3ccSAndroid Build Coastguard Worker   if (!av1_blk_has_rows_and_cols(&blk_params))
4739*77c1e3ccSAndroid Build Coastguard Worker     set_partition_cost_for_edge_blk(cm, &part_search_state);
4740*77c1e3ccSAndroid Build Coastguard Worker 
4741*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
4742*77c1e3ccSAndroid Build Coastguard Worker 
4743*77c1e3ccSAndroid Build Coastguard Worker   // Save rdmult before it might be changed, so it can be restored later.
4744*77c1e3ccSAndroid Build Coastguard Worker   const int orig_rdmult = x->rdmult;
4745*77c1e3ccSAndroid Build Coastguard Worker   setup_block_rdmult(cpi, x, mi_row, mi_col, bsize, NO_AQ, NULL);
4746*77c1e3ccSAndroid Build Coastguard Worker   (void)orig_rdmult;
4747*77c1e3ccSAndroid Build Coastguard Worker 
4748*77c1e3ccSAndroid Build Coastguard Worker   // Set the context.
4749*77c1e3ccSAndroid Build Coastguard Worker   RD_SEARCH_MACROBLOCK_CONTEXT x_ctx;
4750*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context =
4751*77c1e3ccSAndroid Build Coastguard Worker       cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
4752*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
4753*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
4754*77c1e3ccSAndroid Build Coastguard Worker   av1_save_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
4755*77c1e3ccSAndroid Build Coastguard Worker 
4756*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize < BLOCK_SIZES_ALL);
4757*77c1e3ccSAndroid Build Coastguard Worker   unsigned int pb_source_variance = UINT_MAX;
4758*77c1e3ccSAndroid Build Coastguard Worker   int64_t part_none_rd = INT64_MAX;
4759*77c1e3ccSAndroid Build Coastguard Worker   int64_t none_rd = INT64_MAX;
4760*77c1e3ccSAndroid Build Coastguard Worker   int inc_step[NUM_PART4_TYPES] = { 0 };
4761*77c1e3ccSAndroid Build Coastguard Worker   if (partition == PARTITION_HORZ_4) inc_step[HORZ4] = mi_size_high[bsize] / 4;
4762*77c1e3ccSAndroid Build Coastguard Worker   if (partition == PARTITION_VERT_4) inc_step[VERT4] = mi_size_wide[bsize] / 4;
4763*77c1e3ccSAndroid Build Coastguard Worker 
4764*77c1e3ccSAndroid Build Coastguard Worker   switch (partition) {
4765*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_NONE:
4766*77c1e3ccSAndroid Build Coastguard Worker       none_partition_search(cpi, td, tile_data, x, pc_tree, sms_tree, &x_ctx,
4767*77c1e3ccSAndroid Build Coastguard Worker                             &part_search_state, &best_rdc, &pb_source_variance,
4768*77c1e3ccSAndroid Build Coastguard Worker                             &none_rd, &part_none_rd);
4769*77c1e3ccSAndroid Build Coastguard Worker       break;
4770*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ:
4771*77c1e3ccSAndroid Build Coastguard Worker       rectangular_partition_search(cpi, td, tile_data, tp, x, pc_tree, &x_ctx,
4772*77c1e3ccSAndroid Build Coastguard Worker                                    &part_search_state, &best_rdc, NULL, HORZ,
4773*77c1e3ccSAndroid Build Coastguard Worker                                    HORZ);
4774*77c1e3ccSAndroid Build Coastguard Worker       break;
4775*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT:
4776*77c1e3ccSAndroid Build Coastguard Worker       rectangular_partition_search(cpi, td, tile_data, tp, x, pc_tree, &x_ctx,
4777*77c1e3ccSAndroid Build Coastguard Worker                                    &part_search_state, &best_rdc, NULL, VERT,
4778*77c1e3ccSAndroid Build Coastguard Worker                                    VERT);
4779*77c1e3ccSAndroid Build Coastguard Worker       break;
4780*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_A:
4781*77c1e3ccSAndroid Build Coastguard Worker       ab_partitions_search(cpi, td, tile_data, tp, x, &x_ctx, pc_tree,
4782*77c1e3ccSAndroid Build Coastguard Worker                            &part_search_state, &best_rdc, NULL,
4783*77c1e3ccSAndroid Build Coastguard Worker                            pb_source_variance, 1, HORZ_A, HORZ_A);
4784*77c1e3ccSAndroid Build Coastguard Worker       break;
4785*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_B:
4786*77c1e3ccSAndroid Build Coastguard Worker       ab_partitions_search(cpi, td, tile_data, tp, x, &x_ctx, pc_tree,
4787*77c1e3ccSAndroid Build Coastguard Worker                            &part_search_state, &best_rdc, NULL,
4788*77c1e3ccSAndroid Build Coastguard Worker                            pb_source_variance, 1, HORZ_B, HORZ_B);
4789*77c1e3ccSAndroid Build Coastguard Worker       break;
4790*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_A:
4791*77c1e3ccSAndroid Build Coastguard Worker       ab_partitions_search(cpi, td, tile_data, tp, x, &x_ctx, pc_tree,
4792*77c1e3ccSAndroid Build Coastguard Worker                            &part_search_state, &best_rdc, NULL,
4793*77c1e3ccSAndroid Build Coastguard Worker                            pb_source_variance, 1, VERT_A, VERT_A);
4794*77c1e3ccSAndroid Build Coastguard Worker       break;
4795*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_B:
4796*77c1e3ccSAndroid Build Coastguard Worker       ab_partitions_search(cpi, td, tile_data, tp, x, &x_ctx, pc_tree,
4797*77c1e3ccSAndroid Build Coastguard Worker                            &part_search_state, &best_rdc, NULL,
4798*77c1e3ccSAndroid Build Coastguard Worker                            pb_source_variance, 1, VERT_B, VERT_B);
4799*77c1e3ccSAndroid Build Coastguard Worker       break;
4800*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_HORZ_4:
4801*77c1e3ccSAndroid Build Coastguard Worker       rd_pick_4partition(cpi, td, tile_data, tp, x, &x_ctx, pc_tree,
4802*77c1e3ccSAndroid Build Coastguard Worker                          pc_tree->horizontal4, &part_search_state, &best_rdc,
4803*77c1e3ccSAndroid Build Coastguard Worker                          inc_step, PARTITION_HORZ_4);
4804*77c1e3ccSAndroid Build Coastguard Worker       break;
4805*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_VERT_4:
4806*77c1e3ccSAndroid Build Coastguard Worker       rd_pick_4partition(cpi, td, tile_data, tp, x, &x_ctx, pc_tree,
4807*77c1e3ccSAndroid Build Coastguard Worker                          pc_tree->vertical4, &part_search_state, &best_rdc,
4808*77c1e3ccSAndroid Build Coastguard Worker                          inc_step, PARTITION_VERT_4);
4809*77c1e3ccSAndroid Build Coastguard Worker       break;
4810*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_SPLIT:
4811*77c1e3ccSAndroid Build Coastguard Worker       for (int idx = 0; idx < SUB_PARTITIONS_SPLIT; ++idx) {
4812*77c1e3ccSAndroid Build Coastguard Worker         const BLOCK_SIZE subsize =
4813*77c1e3ccSAndroid Build Coastguard Worker             get_partition_subsize(bsize, PARTITION_SPLIT);
4814*77c1e3ccSAndroid Build Coastguard Worker         assert(subsize < BLOCK_SIZES_ALL);
4815*77c1e3ccSAndroid Build Coastguard Worker         const int next_mi_row =
4816*77c1e3ccSAndroid Build Coastguard Worker             idx < 2 ? mi_row : mi_row + mi_size_high[subsize];
4817*77c1e3ccSAndroid Build Coastguard Worker         const int next_mi_col =
4818*77c1e3ccSAndroid Build Coastguard Worker             idx % 2 == 0 ? mi_col : mi_col + mi_size_wide[subsize];
4819*77c1e3ccSAndroid Build Coastguard Worker         if (next_mi_row >= cm->mi_params.mi_rows ||
4820*77c1e3ccSAndroid Build Coastguard Worker             next_mi_col >= cm->mi_params.mi_cols) {
4821*77c1e3ccSAndroid Build Coastguard Worker           continue;
4822*77c1e3ccSAndroid Build Coastguard Worker         }
4823*77c1e3ccSAndroid Build Coastguard Worker         const RD_STATS subblock_rdc = rd_search_for_fixed_partition(
4824*77c1e3ccSAndroid Build Coastguard Worker             cpi, td, tile_data, tp, sms_tree->split[idx], next_mi_row,
4825*77c1e3ccSAndroid Build Coastguard Worker             next_mi_col, subsize, pc_tree->split[idx]);
4826*77c1e3ccSAndroid Build Coastguard Worker         sum_subblock_rate += subblock_rdc.rate;
4827*77c1e3ccSAndroid Build Coastguard Worker         sum_subblock_dist += subblock_rdc.dist;
4828*77c1e3ccSAndroid Build Coastguard Worker       }
4829*77c1e3ccSAndroid Build Coastguard Worker       best_rdc.rate = sum_subblock_rate;
4830*77c1e3ccSAndroid Build Coastguard Worker       best_rdc.rate += part_search_state.partition_cost[PARTITION_SPLIT];
4831*77c1e3ccSAndroid Build Coastguard Worker       best_rdc.dist = sum_subblock_dist;
4832*77c1e3ccSAndroid Build Coastguard Worker       best_rdc.rdcost = RDCOST(x->rdmult, best_rdc.rate, best_rdc.dist);
4833*77c1e3ccSAndroid Build Coastguard Worker       break;
4834*77c1e3ccSAndroid Build Coastguard Worker     default:
4835*77c1e3ccSAndroid Build Coastguard Worker       assert(0 && "invalid partition type.");
4836*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cm->error, AOM_CODEC_ERROR, "Invalid partition type.");
4837*77c1e3ccSAndroid Build Coastguard Worker   }
4838*77c1e3ccSAndroid Build Coastguard Worker   // Note: it is necessary to restore context information.
4839*77c1e3ccSAndroid Build Coastguard Worker   av1_restore_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
4840*77c1e3ccSAndroid Build Coastguard Worker 
4841*77c1e3ccSAndroid Build Coastguard Worker   if (bsize != cm->seq_params->sb_size) {
4842*77c1e3ccSAndroid Build Coastguard Worker     encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, DRY_RUN_NORMAL, bsize,
4843*77c1e3ccSAndroid Build Coastguard Worker               pc_tree, NULL);
4844*77c1e3ccSAndroid Build Coastguard Worker   }
4845*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult = orig_rdmult;
4846*77c1e3ccSAndroid Build Coastguard Worker 
4847*77c1e3ccSAndroid Build Coastguard Worker   return best_rdc;
4848*77c1e3ccSAndroid Build Coastguard Worker }
4849*77c1e3ccSAndroid Build Coastguard Worker 
prepare_sb_features_before_search(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,int mi_row,int mi_col,const BLOCK_SIZE bsize,aom_partition_features_t * features)4850*77c1e3ccSAndroid Build Coastguard Worker static void prepare_sb_features_before_search(
4851*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, ThreadData *td, TileDataEnc *tile_data, int mi_row,
4852*77c1e3ccSAndroid Build Coastguard Worker     int mi_col, const BLOCK_SIZE bsize, aom_partition_features_t *features) {
4853*77c1e3ccSAndroid Build Coastguard Worker   av1_collect_motion_search_features_sb(cpi, td, tile_data, mi_row, mi_col,
4854*77c1e3ccSAndroid Build Coastguard Worker                                         bsize, features);
4855*77c1e3ccSAndroid Build Coastguard Worker   collect_tpl_stats_sb(cpi, bsize, mi_row, mi_col, features);
4856*77c1e3ccSAndroid Build Coastguard Worker }
4857*77c1e3ccSAndroid Build Coastguard Worker 
update_partition_stats(const RD_STATS * const this_rdcost,aom_partition_stats_t * stats)4858*77c1e3ccSAndroid Build Coastguard Worker static void update_partition_stats(const RD_STATS *const this_rdcost,
4859*77c1e3ccSAndroid Build Coastguard Worker                                    aom_partition_stats_t *stats) {
4860*77c1e3ccSAndroid Build Coastguard Worker   stats->rate = this_rdcost->rate;
4861*77c1e3ccSAndroid Build Coastguard Worker   stats->dist = this_rdcost->dist;
4862*77c1e3ccSAndroid Build Coastguard Worker   stats->rdcost = this_rdcost->rdcost;
4863*77c1e3ccSAndroid Build Coastguard Worker }
4864*77c1e3ccSAndroid Build Coastguard Worker 
build_pc_tree_from_part_decision(const aom_partition_decision_t * partition_decision,const BLOCK_SIZE this_bsize,PC_TREE * pc_tree,struct aom_internal_error_info * error_info)4865*77c1e3ccSAndroid Build Coastguard Worker static void build_pc_tree_from_part_decision(
4866*77c1e3ccSAndroid Build Coastguard Worker     const aom_partition_decision_t *partition_decision,
4867*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE this_bsize, PC_TREE *pc_tree,
4868*77c1e3ccSAndroid Build Coastguard Worker     struct aom_internal_error_info *error_info) {
4869*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE bsize = this_bsize;
4870*77c1e3ccSAndroid Build Coastguard Worker   int num_nodes = partition_decision->num_nodes;
4871*77c1e3ccSAndroid Build Coastguard Worker   PC_TREE *tree_node_queue[NUM_NODES] = { NULL };
4872*77c1e3ccSAndroid Build Coastguard Worker   int last_idx = 1;
4873*77c1e3ccSAndroid Build Coastguard Worker   int q_idx = 0;
4874*77c1e3ccSAndroid Build Coastguard Worker   tree_node_queue[q_idx] = pc_tree;
4875*77c1e3ccSAndroid Build Coastguard Worker   while (num_nodes > 0) {
4876*77c1e3ccSAndroid Build Coastguard Worker     const int partitioning = partition_decision->partition_decision[q_idx];
4877*77c1e3ccSAndroid Build Coastguard Worker     assert(partitioning >= PARTITION_NONE &&
4878*77c1e3ccSAndroid Build Coastguard Worker            partitioning < EXT_PARTITION_TYPES);
4879*77c1e3ccSAndroid Build Coastguard Worker     PC_TREE *node = tree_node_queue[q_idx];
4880*77c1e3ccSAndroid Build Coastguard Worker     if (node != NULL) {
4881*77c1e3ccSAndroid Build Coastguard Worker       node->partitioning = partitioning;
4882*77c1e3ccSAndroid Build Coastguard Worker       bsize = node->block_size;
4883*77c1e3ccSAndroid Build Coastguard Worker     }
4884*77c1e3ccSAndroid Build Coastguard Worker     if (partitioning == PARTITION_SPLIT) {
4885*77c1e3ccSAndroid Build Coastguard Worker       const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
4886*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < 4; ++i) {
4887*77c1e3ccSAndroid Build Coastguard Worker         if (node != NULL) {  // Suppress warning
4888*77c1e3ccSAndroid Build Coastguard Worker           node->split[i] = av1_alloc_pc_tree_node(subsize);
4889*77c1e3ccSAndroid Build Coastguard Worker           if (!node->split[i])
4890*77c1e3ccSAndroid Build Coastguard Worker             aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
4891*77c1e3ccSAndroid Build Coastguard Worker                                "Failed to allocate PC_TREE");
4892*77c1e3ccSAndroid Build Coastguard Worker           node->split[i]->index = i;
4893*77c1e3ccSAndroid Build Coastguard Worker           tree_node_queue[last_idx] = node->split[i];
4894*77c1e3ccSAndroid Build Coastguard Worker           ++last_idx;
4895*77c1e3ccSAndroid Build Coastguard Worker         }
4896*77c1e3ccSAndroid Build Coastguard Worker       }
4897*77c1e3ccSAndroid Build Coastguard Worker     }
4898*77c1e3ccSAndroid Build Coastguard Worker     --num_nodes;
4899*77c1e3ccSAndroid Build Coastguard Worker     ++q_idx;
4900*77c1e3ccSAndroid Build Coastguard Worker   }
4901*77c1e3ccSAndroid Build Coastguard Worker }
4902*77c1e3ccSAndroid Build Coastguard Worker 
4903*77c1e3ccSAndroid Build Coastguard Worker // The ML model needs to provide the whole decision tree for the superblock.
ml_partition_search_whole_tree(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,SIMPLE_MOTION_DATA_TREE * sms_root,int mi_row,int mi_col,const BLOCK_SIZE bsize)4904*77c1e3ccSAndroid Build Coastguard Worker static bool ml_partition_search_whole_tree(AV1_COMP *const cpi, ThreadData *td,
4905*77c1e3ccSAndroid Build Coastguard Worker                                            TileDataEnc *tile_data,
4906*77c1e3ccSAndroid Build Coastguard Worker                                            TokenExtra **tp,
4907*77c1e3ccSAndroid Build Coastguard Worker                                            SIMPLE_MOTION_DATA_TREE *sms_root,
4908*77c1e3ccSAndroid Build Coastguard Worker                                            int mi_row, int mi_col,
4909*77c1e3ccSAndroid Build Coastguard Worker                                            const BLOCK_SIZE bsize) {
4910*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
4911*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
4912*77c1e3ccSAndroid Build Coastguard Worker   ExtPartController *const ext_part_controller = &cpi->ext_part_controller;
4913*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info *error_info = x->e_mbd.error_info;
4914*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_features_t features;
4915*77c1e3ccSAndroid Build Coastguard Worker   prepare_sb_features_before_search(cpi, td, tile_data, mi_row, mi_col, bsize,
4916*77c1e3ccSAndroid Build Coastguard Worker                                     &features);
4917*77c1e3ccSAndroid Build Coastguard Worker   features.mi_row = mi_row;
4918*77c1e3ccSAndroid Build Coastguard Worker   features.mi_col = mi_col;
4919*77c1e3ccSAndroid Build Coastguard Worker   features.frame_width = cpi->frame_info.frame_width;
4920*77c1e3ccSAndroid Build Coastguard Worker   features.frame_height = cpi->frame_info.frame_height;
4921*77c1e3ccSAndroid Build Coastguard Worker   features.block_size = bsize;
4922*77c1e3ccSAndroid Build Coastguard Worker   av1_ext_part_send_features(ext_part_controller, &features);
4923*77c1e3ccSAndroid Build Coastguard Worker 
4924*77c1e3ccSAndroid Build Coastguard Worker   // rd mode search (dry run) for a valid partition decision from the ml model.
4925*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_decision_t partition_decision;
4926*77c1e3ccSAndroid Build Coastguard Worker   do {
4927*77c1e3ccSAndroid Build Coastguard Worker     const bool valid_decision = av1_ext_part_get_partition_decision(
4928*77c1e3ccSAndroid Build Coastguard Worker         ext_part_controller, &partition_decision);
4929*77c1e3ccSAndroid Build Coastguard Worker     if (!valid_decision) return false;
4930*77c1e3ccSAndroid Build Coastguard Worker 
4931*77c1e3ccSAndroid Build Coastguard Worker     // First, let's take the easy approach.
4932*77c1e3ccSAndroid Build Coastguard Worker     // We require that the ml model has to provide partition decisions for the
4933*77c1e3ccSAndroid Build Coastguard Worker     // whole superblock.
4934*77c1e3ccSAndroid Build Coastguard Worker     td->pc_root = av1_alloc_pc_tree_node(bsize);
4935*77c1e3ccSAndroid Build Coastguard Worker     if (!td->pc_root)
4936*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
4937*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PC_TREE");
4938*77c1e3ccSAndroid Build Coastguard Worker     build_pc_tree_from_part_decision(&partition_decision, bsize, td->pc_root,
4939*77c1e3ccSAndroid Build Coastguard Worker                                      error_info);
4940*77c1e3ccSAndroid Build Coastguard Worker 
4941*77c1e3ccSAndroid Build Coastguard Worker     const RD_STATS this_rdcost = rd_search_for_fixed_partition(
4942*77c1e3ccSAndroid Build Coastguard Worker         cpi, td, tile_data, tp, sms_root, mi_row, mi_col, bsize, td->pc_root);
4943*77c1e3ccSAndroid Build Coastguard Worker     aom_partition_stats_t stats;
4944*77c1e3ccSAndroid Build Coastguard Worker     update_partition_stats(&this_rdcost, &stats);
4945*77c1e3ccSAndroid Build Coastguard Worker     av1_ext_part_send_partition_stats(ext_part_controller, &stats);
4946*77c1e3ccSAndroid Build Coastguard Worker     if (!partition_decision.is_final_decision) {
4947*77c1e3ccSAndroid Build Coastguard Worker       av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
4948*77c1e3ccSAndroid Build Coastguard Worker                                  cpi->sf.part_sf.partition_search_type);
4949*77c1e3ccSAndroid Build Coastguard Worker       td->pc_root = NULL;
4950*77c1e3ccSAndroid Build Coastguard Worker     }
4951*77c1e3ccSAndroid Build Coastguard Worker   } while (!partition_decision.is_final_decision);
4952*77c1e3ccSAndroid Build Coastguard Worker 
4953*77c1e3ccSAndroid Build Coastguard Worker   // Encode with the selected mode and partition.
4954*77c1e3ccSAndroid Build Coastguard Worker   set_cb_offsets(x->cb_offset, 0, 0);
4955*77c1e3ccSAndroid Build Coastguard Worker   encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, OUTPUT_ENABLED, bsize,
4956*77c1e3ccSAndroid Build Coastguard Worker             td->pc_root, NULL);
4957*77c1e3ccSAndroid Build Coastguard Worker   av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
4958*77c1e3ccSAndroid Build Coastguard Worker                              cpi->sf.part_sf.partition_search_type);
4959*77c1e3ccSAndroid Build Coastguard Worker   td->pc_root = NULL;
4960*77c1e3ccSAndroid Build Coastguard Worker 
4961*77c1e3ccSAndroid Build Coastguard Worker   return true;
4962*77c1e3ccSAndroid Build Coastguard Worker }
4963*77c1e3ccSAndroid Build Coastguard Worker 
4964*77c1e3ccSAndroid Build Coastguard Worker // Use a bitmask to represent the valid partition types for the current
4965*77c1e3ccSAndroid Build Coastguard Worker // block. "1" represents the corresponding partition type is vaild.
4966*77c1e3ccSAndroid Build Coastguard Worker // The least significant bit represents "PARTITION_NONE", the
4967*77c1e3ccSAndroid Build Coastguard Worker // largest significant bit represents "PARTITION_VERT_4", follow
4968*77c1e3ccSAndroid Build Coastguard Worker // the enum order for PARTITION_TYPE in "enums.h"
get_valid_partition_types(const AV1_COMP * const cpi,const PartitionSearchState * const part_search_state,const BLOCK_SIZE bsize)4969*77c1e3ccSAndroid Build Coastguard Worker static int get_valid_partition_types(
4970*77c1e3ccSAndroid Build Coastguard Worker     const AV1_COMP *const cpi,
4971*77c1e3ccSAndroid Build Coastguard Worker     const PartitionSearchState *const part_search_state,
4972*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE bsize) {
4973*77c1e3ccSAndroid Build Coastguard Worker   const PartitionCfg *const part_cfg = &cpi->oxcf.part_cfg;
4974*77c1e3ccSAndroid Build Coastguard Worker   const PartitionBlkParams blk_params = part_search_state->part_blk_params;
4975*77c1e3ccSAndroid Build Coastguard Worker   int valid_types = 0;
4976*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_NONE
4977*77c1e3ccSAndroid Build Coastguard Worker   valid_types |= (part_search_state->partition_none_allowed << 0);
4978*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_HORZ
4979*77c1e3ccSAndroid Build Coastguard Worker   valid_types |= (part_search_state->partition_rect_allowed[HORZ] << 1);
4980*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_VERT
4981*77c1e3ccSAndroid Build Coastguard Worker   valid_types |= (part_search_state->partition_rect_allowed[VERT] << 2);
4982*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_SPLIT
4983*77c1e3ccSAndroid Build Coastguard Worker   valid_types |= (part_search_state->do_square_split << 3);
4984*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_HORZ_A
4985*77c1e3ccSAndroid Build Coastguard Worker   const int ext_partition_allowed = part_search_state->do_rectangular_split &&
4986*77c1e3ccSAndroid Build Coastguard Worker                                     av1_blk_has_rows_and_cols(&blk_params);
4987*77c1e3ccSAndroid Build Coastguard Worker   const int horzab_partition_allowed =
4988*77c1e3ccSAndroid Build Coastguard Worker       ext_partition_allowed && part_cfg->enable_ab_partitions &&
4989*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->partition_rect_allowed[HORZ];
4990*77c1e3ccSAndroid Build Coastguard Worker   valid_types |= (horzab_partition_allowed << 4);
4991*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_HORZ_B
4992*77c1e3ccSAndroid Build Coastguard Worker   valid_types |= (horzab_partition_allowed << 5);
4993*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_VERT_A
4994*77c1e3ccSAndroid Build Coastguard Worker   const int vertab_partition_allowed =
4995*77c1e3ccSAndroid Build Coastguard Worker       ext_partition_allowed && part_cfg->enable_ab_partitions &&
4996*77c1e3ccSAndroid Build Coastguard Worker       part_search_state->partition_rect_allowed[VERT];
4997*77c1e3ccSAndroid Build Coastguard Worker   valid_types |= (vertab_partition_allowed << 6);
4998*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_VERT_B
4999*77c1e3ccSAndroid Build Coastguard Worker   valid_types |= (vertab_partition_allowed << 7);
5000*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_HORZ_4
5001*77c1e3ccSAndroid Build Coastguard Worker   const int partition4_allowed = part_cfg->enable_1to4_partitions &&
5002*77c1e3ccSAndroid Build Coastguard Worker                                  ext_partition_allowed &&
5003*77c1e3ccSAndroid Build Coastguard Worker                                  bsize != BLOCK_128X128;
5004*77c1e3ccSAndroid Build Coastguard Worker   const int horz4_allowed =
5005*77c1e3ccSAndroid Build Coastguard Worker       partition4_allowed && part_search_state->partition_rect_allowed[HORZ] &&
5006*77c1e3ccSAndroid Build Coastguard Worker       get_plane_block_size(get_partition_subsize(bsize, PARTITION_HORZ_4),
5007*77c1e3ccSAndroid Build Coastguard Worker                            part_search_state->ss_x,
5008*77c1e3ccSAndroid Build Coastguard Worker                            part_search_state->ss_y) != BLOCK_INVALID;
5009*77c1e3ccSAndroid Build Coastguard Worker   valid_types |= (horz4_allowed << 8);
5010*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_VERT_4
5011*77c1e3ccSAndroid Build Coastguard Worker   const int vert4_allowed =
5012*77c1e3ccSAndroid Build Coastguard Worker       partition4_allowed && part_search_state->partition_rect_allowed[HORZ] &&
5013*77c1e3ccSAndroid Build Coastguard Worker       get_plane_block_size(get_partition_subsize(bsize, PARTITION_VERT_4),
5014*77c1e3ccSAndroid Build Coastguard Worker                            part_search_state->ss_x,
5015*77c1e3ccSAndroid Build Coastguard Worker                            part_search_state->ss_y) != BLOCK_INVALID;
5016*77c1e3ccSAndroid Build Coastguard Worker   valid_types |= (vert4_allowed << 9);
5017*77c1e3ccSAndroid Build Coastguard Worker 
5018*77c1e3ccSAndroid Build Coastguard Worker   return valid_types;
5019*77c1e3ccSAndroid Build Coastguard Worker }
5020*77c1e3ccSAndroid Build Coastguard Worker 
prepare_tpl_stats_block(const AV1_COMP * const cpi,const BLOCK_SIZE bsize,const int mi_row,const int mi_col,int64_t * intra_cost,int64_t * inter_cost,int64_t * mc_dep_cost)5021*77c1e3ccSAndroid Build Coastguard Worker static void prepare_tpl_stats_block(const AV1_COMP *const cpi,
5022*77c1e3ccSAndroid Build Coastguard Worker                                     const BLOCK_SIZE bsize, const int mi_row,
5023*77c1e3ccSAndroid Build Coastguard Worker                                     const int mi_col, int64_t *intra_cost,
5024*77c1e3ccSAndroid Build Coastguard Worker                                     int64_t *inter_cost, int64_t *mc_dep_cost) {
5025*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
5026*77c1e3ccSAndroid Build Coastguard Worker   GF_GROUP *gf_group = &cpi->ppi->gf_group;
5027*77c1e3ccSAndroid Build Coastguard Worker   if (gf_group->update_type[cpi->gf_frame_index] == INTNL_OVERLAY_UPDATE ||
5028*77c1e3ccSAndroid Build Coastguard Worker       gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE) {
5029*77c1e3ccSAndroid Build Coastguard Worker     return;
5030*77c1e3ccSAndroid Build Coastguard Worker   }
5031*77c1e3ccSAndroid Build Coastguard Worker 
5032*77c1e3ccSAndroid Build Coastguard Worker   TplParams *const tpl_data = &cpi->ppi->tpl_data;
5033*77c1e3ccSAndroid Build Coastguard Worker   TplDepFrame *tpl_frame = &tpl_data->tpl_frame[cpi->gf_frame_index];
5034*77c1e3ccSAndroid Build Coastguard Worker   TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
5035*77c1e3ccSAndroid Build Coastguard Worker   // If tpl stats is not established, early return
5036*77c1e3ccSAndroid Build Coastguard Worker   if (!tpl_data->ready || gf_group->max_layer_depth_allowed == 0) {
5037*77c1e3ccSAndroid Build Coastguard Worker     return;
5038*77c1e3ccSAndroid Build Coastguard Worker   }
5039*77c1e3ccSAndroid Build Coastguard Worker 
5040*77c1e3ccSAndroid Build Coastguard Worker   const int tpl_stride = tpl_frame->stride;
5041*77c1e3ccSAndroid Build Coastguard Worker   const int step = 1 << tpl_data->tpl_stats_block_mis_log2;
5042*77c1e3ccSAndroid Build Coastguard Worker   const int mi_width =
5043*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(mi_size_wide[bsize], cm->mi_params.mi_cols - mi_col);
5044*77c1e3ccSAndroid Build Coastguard Worker   const int mi_height =
5045*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(mi_size_high[bsize], cm->mi_params.mi_rows - mi_row);
5046*77c1e3ccSAndroid Build Coastguard Worker 
5047*77c1e3ccSAndroid Build Coastguard Worker   int64_t sum_intra_cost = 0;
5048*77c1e3ccSAndroid Build Coastguard Worker   int64_t sum_inter_cost = 0;
5049*77c1e3ccSAndroid Build Coastguard Worker   int64_t sum_mc_dep_cost = 0;
5050*77c1e3ccSAndroid Build Coastguard Worker   for (int row = 0; row < mi_height; row += step) {
5051*77c1e3ccSAndroid Build Coastguard Worker     for (int col = 0; col < mi_width; col += step) {
5052*77c1e3ccSAndroid Build Coastguard Worker       TplDepStats *this_stats =
5053*77c1e3ccSAndroid Build Coastguard Worker           &tpl_stats[av1_tpl_ptr_pos(mi_row + row, mi_col + col, tpl_stride,
5054*77c1e3ccSAndroid Build Coastguard Worker                                      tpl_data->tpl_stats_block_mis_log2)];
5055*77c1e3ccSAndroid Build Coastguard Worker       sum_intra_cost += this_stats->intra_cost;
5056*77c1e3ccSAndroid Build Coastguard Worker       sum_inter_cost += this_stats->inter_cost;
5057*77c1e3ccSAndroid Build Coastguard Worker       const int64_t mc_dep_delta =
5058*77c1e3ccSAndroid Build Coastguard Worker           RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
5059*77c1e3ccSAndroid Build Coastguard Worker                  this_stats->mc_dep_dist);
5060*77c1e3ccSAndroid Build Coastguard Worker       sum_mc_dep_cost += mc_dep_delta;
5061*77c1e3ccSAndroid Build Coastguard Worker     }
5062*77c1e3ccSAndroid Build Coastguard Worker   }
5063*77c1e3ccSAndroid Build Coastguard Worker 
5064*77c1e3ccSAndroid Build Coastguard Worker   *intra_cost = sum_intra_cost;
5065*77c1e3ccSAndroid Build Coastguard Worker   *inter_cost = sum_inter_cost;
5066*77c1e3ccSAndroid Build Coastguard Worker   *mc_dep_cost = sum_mc_dep_cost;
5067*77c1e3ccSAndroid Build Coastguard Worker }
5068*77c1e3ccSAndroid Build Coastguard Worker 
recursive_partition(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,SIMPLE_MOTION_DATA_TREE * sms_root,PC_TREE * pc_tree,int mi_row,int mi_col,const BLOCK_SIZE bsize,RD_STATS * this_rdcost)5069*77c1e3ccSAndroid Build Coastguard Worker static bool recursive_partition(AV1_COMP *const cpi, ThreadData *td,
5070*77c1e3ccSAndroid Build Coastguard Worker                                 TileDataEnc *tile_data, TokenExtra **tp,
5071*77c1e3ccSAndroid Build Coastguard Worker                                 SIMPLE_MOTION_DATA_TREE *sms_root,
5072*77c1e3ccSAndroid Build Coastguard Worker                                 PC_TREE *pc_tree, int mi_row, int mi_col,
5073*77c1e3ccSAndroid Build Coastguard Worker                                 const BLOCK_SIZE bsize, RD_STATS *this_rdcost) {
5074*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
5075*77c1e3ccSAndroid Build Coastguard Worker   ExtPartController *const ext_part_controller = &cpi->ext_part_controller;
5076*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
5077*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
5078*77c1e3ccSAndroid Build Coastguard Worker   if (mi_row >= cm->mi_params.mi_rows || mi_col >= cm->mi_params.mi_cols) {
5079*77c1e3ccSAndroid Build Coastguard Worker     return false;
5080*77c1e3ccSAndroid Build Coastguard Worker   }
5081*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_decision_t partition_decision;
5082*77c1e3ccSAndroid Build Coastguard Worker   do {
5083*77c1e3ccSAndroid Build Coastguard Worker     PartitionSearchState part_search_state;
5084*77c1e3ccSAndroid Build Coastguard Worker     // Initialization of state variables used in partition search.
5085*77c1e3ccSAndroid Build Coastguard Worker     // TODO(chengchen): check if there is hidden conditions that don't allow
5086*77c1e3ccSAndroid Build Coastguard Worker     // all possible partition types.
5087*77c1e3ccSAndroid Build Coastguard Worker     init_partition_search_state_params(x, cpi, &part_search_state, mi_row,
5088*77c1e3ccSAndroid Build Coastguard Worker                                        mi_col, bsize);
5089*77c1e3ccSAndroid Build Coastguard Worker     // Override partition costs at the edges of the frame in the same
5090*77c1e3ccSAndroid Build Coastguard Worker     // way as in read_partition (see decodeframe.c).
5091*77c1e3ccSAndroid Build Coastguard Worker     PartitionBlkParams blk_params = part_search_state.part_blk_params;
5092*77c1e3ccSAndroid Build Coastguard Worker     if (!av1_blk_has_rows_and_cols(&blk_params))
5093*77c1e3ccSAndroid Build Coastguard Worker       set_partition_cost_for_edge_blk(cm, &part_search_state);
5094*77c1e3ccSAndroid Build Coastguard Worker     const int orig_rdmult = x->rdmult;
5095*77c1e3ccSAndroid Build Coastguard Worker     setup_block_rdmult(cpi, x, mi_row, mi_col, bsize, NO_AQ, NULL);
5096*77c1e3ccSAndroid Build Coastguard Worker     const int valid_partition_types =
5097*77c1e3ccSAndroid Build Coastguard Worker         get_valid_partition_types(cpi, &part_search_state, bsize);
5098*77c1e3ccSAndroid Build Coastguard Worker     const FRAME_UPDATE_TYPE update_type =
5099*77c1e3ccSAndroid Build Coastguard Worker         get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
5100*77c1e3ccSAndroid Build Coastguard Worker     const int qindex = av1_get_qindex(&cm->seg, xd->mi[0]->segment_id,
5101*77c1e3ccSAndroid Build Coastguard Worker                                       cm->quant_params.base_qindex);
5102*77c1e3ccSAndroid Build Coastguard Worker     // RD multiplier
5103*77c1e3ccSAndroid Build Coastguard Worker     const int rdmult = x->rdmult;
5104*77c1e3ccSAndroid Build Coastguard Worker     // pyramid level
5105*77c1e3ccSAndroid Build Coastguard Worker     const int pyramid_level =
5106*77c1e3ccSAndroid Build Coastguard Worker         cpi->ppi->gf_group.layer_depth[cpi->gf_frame_index];
5107*77c1e3ccSAndroid Build Coastguard Worker     x->rdmult = orig_rdmult;
5108*77c1e3ccSAndroid Build Coastguard Worker     // Neighbor information
5109*77c1e3ccSAndroid Build Coastguard Worker     const int has_above = !!xd->above_mbmi;
5110*77c1e3ccSAndroid Build Coastguard Worker     const int has_left = !!xd->left_mbmi;
5111*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE above_bsize =
5112*77c1e3ccSAndroid Build Coastguard Worker         has_above ? xd->above_mbmi->bsize : BLOCK_INVALID;
5113*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE left_bsize =
5114*77c1e3ccSAndroid Build Coastguard Worker         has_left ? xd->left_mbmi->bsize : BLOCK_INVALID;
5115*77c1e3ccSAndroid Build Coastguard Worker     const int above_block_width =
5116*77c1e3ccSAndroid Build Coastguard Worker         above_bsize == BLOCK_INVALID ? -1 : block_size_wide[above_bsize];
5117*77c1e3ccSAndroid Build Coastguard Worker     const int above_block_height =
5118*77c1e3ccSAndroid Build Coastguard Worker         above_bsize == BLOCK_INVALID ? -1 : block_size_high[above_bsize];
5119*77c1e3ccSAndroid Build Coastguard Worker     const int left_block_width =
5120*77c1e3ccSAndroid Build Coastguard Worker         left_bsize == BLOCK_INVALID ? -1 : block_size_wide[left_bsize];
5121*77c1e3ccSAndroid Build Coastguard Worker     const int left_block_height =
5122*77c1e3ccSAndroid Build Coastguard Worker         left_bsize == BLOCK_INVALID ? -1 : block_size_high[left_bsize];
5123*77c1e3ccSAndroid Build Coastguard Worker     // Prepare simple motion search stats as features
5124*77c1e3ccSAndroid Build Coastguard Worker     unsigned int block_sse = -1;
5125*77c1e3ccSAndroid Build Coastguard Worker     unsigned int block_var = -1;
5126*77c1e3ccSAndroid Build Coastguard Worker     unsigned int sub_block_sse[4] = { -1, -1, -1, -1 };
5127*77c1e3ccSAndroid Build Coastguard Worker     unsigned int sub_block_var[4] = { -1, -1, -1, -1 };
5128*77c1e3ccSAndroid Build Coastguard Worker     unsigned int horz_block_sse[2] = { -1, -1 };
5129*77c1e3ccSAndroid Build Coastguard Worker     unsigned int horz_block_var[2] = { -1, -1 };
5130*77c1e3ccSAndroid Build Coastguard Worker     unsigned int vert_block_sse[2] = { -1, -1 };
5131*77c1e3ccSAndroid Build Coastguard Worker     unsigned int vert_block_var[2] = { -1, -1 };
5132*77c1e3ccSAndroid Build Coastguard Worker     av1_prepare_motion_search_features_block(
5133*77c1e3ccSAndroid Build Coastguard Worker         cpi, td, tile_data, mi_row, mi_col, bsize, valid_partition_types,
5134*77c1e3ccSAndroid Build Coastguard Worker         &block_sse, &block_var, sub_block_sse, sub_block_var, horz_block_sse,
5135*77c1e3ccSAndroid Build Coastguard Worker         horz_block_var, vert_block_sse, vert_block_var);
5136*77c1e3ccSAndroid Build Coastguard Worker     // Prepare tpl stats for the current block as features
5137*77c1e3ccSAndroid Build Coastguard Worker     int64_t tpl_intra_cost = -1;
5138*77c1e3ccSAndroid Build Coastguard Worker     int64_t tpl_inter_cost = -1;
5139*77c1e3ccSAndroid Build Coastguard Worker     int64_t tpl_mc_dep_cost = -1;
5140*77c1e3ccSAndroid Build Coastguard Worker     prepare_tpl_stats_block(cpi, bsize, mi_row, mi_col, &tpl_intra_cost,
5141*77c1e3ccSAndroid Build Coastguard Worker                             &tpl_inter_cost, &tpl_mc_dep_cost);
5142*77c1e3ccSAndroid Build Coastguard Worker 
5143*77c1e3ccSAndroid Build Coastguard Worker     aom_partition_features_t features;
5144*77c1e3ccSAndroid Build Coastguard Worker     features.mi_row = mi_row;
5145*77c1e3ccSAndroid Build Coastguard Worker     features.mi_col = mi_col;
5146*77c1e3ccSAndroid Build Coastguard Worker     features.frame_width = cpi->frame_info.frame_width;
5147*77c1e3ccSAndroid Build Coastguard Worker     features.frame_height = cpi->frame_info.frame_height;
5148*77c1e3ccSAndroid Build Coastguard Worker     features.block_size = bsize;
5149*77c1e3ccSAndroid Build Coastguard Worker     features.valid_partition_types = valid_partition_types;
5150*77c1e3ccSAndroid Build Coastguard Worker     features.update_type = update_type;
5151*77c1e3ccSAndroid Build Coastguard Worker     features.qindex = qindex;
5152*77c1e3ccSAndroid Build Coastguard Worker     features.rdmult = rdmult;
5153*77c1e3ccSAndroid Build Coastguard Worker     features.pyramid_level = pyramid_level;
5154*77c1e3ccSAndroid Build Coastguard Worker     features.has_above_block = has_above;
5155*77c1e3ccSAndroid Build Coastguard Worker     features.above_block_width = above_block_width;
5156*77c1e3ccSAndroid Build Coastguard Worker     features.above_block_height = above_block_height;
5157*77c1e3ccSAndroid Build Coastguard Worker     features.has_left_block = has_left;
5158*77c1e3ccSAndroid Build Coastguard Worker     features.left_block_width = left_block_width;
5159*77c1e3ccSAndroid Build Coastguard Worker     features.left_block_height = left_block_height;
5160*77c1e3ccSAndroid Build Coastguard Worker     features.block_sse = block_sse;
5161*77c1e3ccSAndroid Build Coastguard Worker     features.block_var = block_var;
5162*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < 4; ++i) {
5163*77c1e3ccSAndroid Build Coastguard Worker       features.sub_block_sse[i] = sub_block_sse[i];
5164*77c1e3ccSAndroid Build Coastguard Worker       features.sub_block_var[i] = sub_block_var[i];
5165*77c1e3ccSAndroid Build Coastguard Worker     }
5166*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < 2; ++i) {
5167*77c1e3ccSAndroid Build Coastguard Worker       features.horz_block_sse[i] = horz_block_sse[i];
5168*77c1e3ccSAndroid Build Coastguard Worker       features.horz_block_var[i] = horz_block_var[i];
5169*77c1e3ccSAndroid Build Coastguard Worker       features.vert_block_sse[i] = vert_block_sse[i];
5170*77c1e3ccSAndroid Build Coastguard Worker       features.vert_block_var[i] = vert_block_var[i];
5171*77c1e3ccSAndroid Build Coastguard Worker     }
5172*77c1e3ccSAndroid Build Coastguard Worker     features.tpl_intra_cost = tpl_intra_cost;
5173*77c1e3ccSAndroid Build Coastguard Worker     features.tpl_inter_cost = tpl_inter_cost;
5174*77c1e3ccSAndroid Build Coastguard Worker     features.tpl_mc_dep_cost = tpl_mc_dep_cost;
5175*77c1e3ccSAndroid Build Coastguard Worker     av1_ext_part_send_features(ext_part_controller, &features);
5176*77c1e3ccSAndroid Build Coastguard Worker     const bool valid_decision = av1_ext_part_get_partition_decision(
5177*77c1e3ccSAndroid Build Coastguard Worker         ext_part_controller, &partition_decision);
5178*77c1e3ccSAndroid Build Coastguard Worker     if (!valid_decision) return false;
5179*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->partitioning = partition_decision.current_decision;
5180*77c1e3ccSAndroid Build Coastguard Worker 
5181*77c1e3ccSAndroid Build Coastguard Worker     av1_init_rd_stats(this_rdcost);
5182*77c1e3ccSAndroid Build Coastguard Worker     if (partition_decision.current_decision == PARTITION_SPLIT) {
5183*77c1e3ccSAndroid Build Coastguard Worker       assert(block_size_wide[bsize] >= 8 && block_size_high[bsize] >= 8);
5184*77c1e3ccSAndroid Build Coastguard Worker       const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
5185*77c1e3ccSAndroid Build Coastguard Worker       RD_STATS split_rdc[SUB_PARTITIONS_SPLIT];
5186*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
5187*77c1e3ccSAndroid Build Coastguard Worker         av1_init_rd_stats(&split_rdc[i]);
5188*77c1e3ccSAndroid Build Coastguard Worker         if (pc_tree->split[i] == NULL)
5189*77c1e3ccSAndroid Build Coastguard Worker           pc_tree->split[i] = av1_alloc_pc_tree_node(subsize);
5190*77c1e3ccSAndroid Build Coastguard Worker         if (!pc_tree->split[i])
5191*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
5192*77c1e3ccSAndroid Build Coastguard Worker                              "Failed to allocate PC_TREE");
5193*77c1e3ccSAndroid Build Coastguard Worker         pc_tree->split[i]->index = i;
5194*77c1e3ccSAndroid Build Coastguard Worker       }
5195*77c1e3ccSAndroid Build Coastguard Worker       const int orig_rdmult_tmp = x->rdmult;
5196*77c1e3ccSAndroid Build Coastguard Worker       setup_block_rdmult(cpi, x, mi_row, mi_col, bsize, NO_AQ, NULL);
5197*77c1e3ccSAndroid Build Coastguard Worker       // TODO(chengchen): check boundary conditions
5198*77c1e3ccSAndroid Build Coastguard Worker       // top-left
5199*77c1e3ccSAndroid Build Coastguard Worker       recursive_partition(cpi, td, tile_data, tp, sms_root, pc_tree->split[0],
5200*77c1e3ccSAndroid Build Coastguard Worker                           mi_row, mi_col, subsize, &split_rdc[0]);
5201*77c1e3ccSAndroid Build Coastguard Worker       // top-right
5202*77c1e3ccSAndroid Build Coastguard Worker       recursive_partition(cpi, td, tile_data, tp, sms_root, pc_tree->split[1],
5203*77c1e3ccSAndroid Build Coastguard Worker                           mi_row, mi_col + mi_size_wide[subsize], subsize,
5204*77c1e3ccSAndroid Build Coastguard Worker                           &split_rdc[1]);
5205*77c1e3ccSAndroid Build Coastguard Worker       // bottom-left
5206*77c1e3ccSAndroid Build Coastguard Worker       recursive_partition(cpi, td, tile_data, tp, sms_root, pc_tree->split[2],
5207*77c1e3ccSAndroid Build Coastguard Worker                           mi_row + mi_size_high[subsize], mi_col, subsize,
5208*77c1e3ccSAndroid Build Coastguard Worker                           &split_rdc[2]);
5209*77c1e3ccSAndroid Build Coastguard Worker       // bottom_right
5210*77c1e3ccSAndroid Build Coastguard Worker       recursive_partition(cpi, td, tile_data, tp, sms_root, pc_tree->split[3],
5211*77c1e3ccSAndroid Build Coastguard Worker                           mi_row + mi_size_high[subsize],
5212*77c1e3ccSAndroid Build Coastguard Worker                           mi_col + mi_size_wide[subsize], subsize,
5213*77c1e3ccSAndroid Build Coastguard Worker                           &split_rdc[3]);
5214*77c1e3ccSAndroid Build Coastguard Worker       this_rdcost->rate += part_search_state.partition_cost[PARTITION_SPLIT];
5215*77c1e3ccSAndroid Build Coastguard Worker       // problem is here, the rdmult is different from the rdmult in sub block.
5216*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
5217*77c1e3ccSAndroid Build Coastguard Worker         this_rdcost->rate += split_rdc[i].rate;
5218*77c1e3ccSAndroid Build Coastguard Worker         this_rdcost->dist += split_rdc[i].dist;
5219*77c1e3ccSAndroid Build Coastguard Worker         av1_rd_cost_update(x->rdmult, this_rdcost);
5220*77c1e3ccSAndroid Build Coastguard Worker       }
5221*77c1e3ccSAndroid Build Coastguard Worker       x->rdmult = orig_rdmult_tmp;
5222*77c1e3ccSAndroid Build Coastguard Worker     } else {
5223*77c1e3ccSAndroid Build Coastguard Worker       *this_rdcost = rd_search_for_fixed_partition(
5224*77c1e3ccSAndroid Build Coastguard Worker           cpi, td, tile_data, tp, sms_root, mi_row, mi_col, bsize, pc_tree);
5225*77c1e3ccSAndroid Build Coastguard Worker     }
5226*77c1e3ccSAndroid Build Coastguard Worker 
5227*77c1e3ccSAndroid Build Coastguard Worker     aom_partition_stats_t stats;
5228*77c1e3ccSAndroid Build Coastguard Worker     update_partition_stats(this_rdcost, &stats);
5229*77c1e3ccSAndroid Build Coastguard Worker     av1_ext_part_send_partition_stats(ext_part_controller, &stats);
5230*77c1e3ccSAndroid Build Coastguard Worker     if (!partition_decision.is_final_decision) {
5231*77c1e3ccSAndroid Build Coastguard Worker       if (partition_decision.current_decision == PARTITION_SPLIT) {
5232*77c1e3ccSAndroid Build Coastguard Worker         for (int i = 0; i < 4; ++i) {
5233*77c1e3ccSAndroid Build Coastguard Worker           if (pc_tree->split[i] != NULL) {
5234*77c1e3ccSAndroid Build Coastguard Worker             av1_free_pc_tree_recursive(pc_tree->split[i], av1_num_planes(cm), 0,
5235*77c1e3ccSAndroid Build Coastguard Worker                                        0,
5236*77c1e3ccSAndroid Build Coastguard Worker                                        cpi->sf.part_sf.partition_search_type);
5237*77c1e3ccSAndroid Build Coastguard Worker             pc_tree->split[i] = NULL;
5238*77c1e3ccSAndroid Build Coastguard Worker           }
5239*77c1e3ccSAndroid Build Coastguard Worker         }
5240*77c1e3ccSAndroid Build Coastguard Worker       }
5241*77c1e3ccSAndroid Build Coastguard Worker     }
5242*77c1e3ccSAndroid Build Coastguard Worker   } while (!partition_decision.is_final_decision);
5243*77c1e3ccSAndroid Build Coastguard Worker 
5244*77c1e3ccSAndroid Build Coastguard Worker   return true;
5245*77c1e3ccSAndroid Build Coastguard Worker }
5246*77c1e3ccSAndroid Build Coastguard Worker 
5247*77c1e3ccSAndroid Build Coastguard Worker // The ML model only needs to make decisions for the current block each time.
ml_partition_search_partial(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,SIMPLE_MOTION_DATA_TREE * sms_root,int mi_row,int mi_col,const BLOCK_SIZE bsize)5248*77c1e3ccSAndroid Build Coastguard Worker static bool ml_partition_search_partial(AV1_COMP *const cpi, ThreadData *td,
5249*77c1e3ccSAndroid Build Coastguard Worker                                         TileDataEnc *tile_data, TokenExtra **tp,
5250*77c1e3ccSAndroid Build Coastguard Worker                                         SIMPLE_MOTION_DATA_TREE *sms_root,
5251*77c1e3ccSAndroid Build Coastguard Worker                                         int mi_row, int mi_col,
5252*77c1e3ccSAndroid Build Coastguard Worker                                         const BLOCK_SIZE bsize) {
5253*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
5254*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
5255*77c1e3ccSAndroid Build Coastguard Worker   ExtPartController *const ext_part_controller = &cpi->ext_part_controller;
5256*77c1e3ccSAndroid Build Coastguard Worker   aom_partition_features_t features;
5257*77c1e3ccSAndroid Build Coastguard Worker   prepare_sb_features_before_search(cpi, td, tile_data, mi_row, mi_col, bsize,
5258*77c1e3ccSAndroid Build Coastguard Worker                                     &features);
5259*77c1e3ccSAndroid Build Coastguard Worker   features.mi_row = mi_row;
5260*77c1e3ccSAndroid Build Coastguard Worker   features.mi_col = mi_col;
5261*77c1e3ccSAndroid Build Coastguard Worker   features.frame_width = cpi->frame_info.frame_width;
5262*77c1e3ccSAndroid Build Coastguard Worker   features.frame_height = cpi->frame_info.frame_height;
5263*77c1e3ccSAndroid Build Coastguard Worker   features.block_size = bsize;
5264*77c1e3ccSAndroid Build Coastguard Worker   av1_ext_part_send_features(ext_part_controller, &features);
5265*77c1e3ccSAndroid Build Coastguard Worker   td->pc_root = av1_alloc_pc_tree_node(bsize);
5266*77c1e3ccSAndroid Build Coastguard Worker   if (!td->pc_root)
5267*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(x->e_mbd.error_info, AOM_CODEC_MEM_ERROR,
5268*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to allocate PC_TREE");
5269*77c1e3ccSAndroid Build Coastguard Worker 
5270*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS rdcost;
5271*77c1e3ccSAndroid Build Coastguard Worker   const bool valid_partition =
5272*77c1e3ccSAndroid Build Coastguard Worker       recursive_partition(cpi, td, tile_data, tp, sms_root, td->pc_root, mi_row,
5273*77c1e3ccSAndroid Build Coastguard Worker                           mi_col, bsize, &rdcost);
5274*77c1e3ccSAndroid Build Coastguard Worker   if (!valid_partition) {
5275*77c1e3ccSAndroid Build Coastguard Worker     return false;
5276*77c1e3ccSAndroid Build Coastguard Worker   }
5277*77c1e3ccSAndroid Build Coastguard Worker 
5278*77c1e3ccSAndroid Build Coastguard Worker   // Encode with the selected mode and partition.
5279*77c1e3ccSAndroid Build Coastguard Worker   set_cb_offsets(x->cb_offset, 0, 0);
5280*77c1e3ccSAndroid Build Coastguard Worker   encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, OUTPUT_ENABLED, bsize,
5281*77c1e3ccSAndroid Build Coastguard Worker             td->pc_root, NULL);
5282*77c1e3ccSAndroid Build Coastguard Worker   av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
5283*77c1e3ccSAndroid Build Coastguard Worker                              cpi->sf.part_sf.partition_search_type);
5284*77c1e3ccSAndroid Build Coastguard Worker   td->pc_root = NULL;
5285*77c1e3ccSAndroid Build Coastguard Worker 
5286*77c1e3ccSAndroid Build Coastguard Worker   return true;
5287*77c1e3ccSAndroid Build Coastguard Worker }
5288*77c1e3ccSAndroid Build Coastguard Worker 
av1_rd_partition_search(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,SIMPLE_MOTION_DATA_TREE * sms_root,int mi_row,int mi_col,const BLOCK_SIZE bsize,RD_STATS * best_rd_cost)5289*77c1e3ccSAndroid Build Coastguard Worker bool av1_rd_partition_search(AV1_COMP *const cpi, ThreadData *td,
5290*77c1e3ccSAndroid Build Coastguard Worker                              TileDataEnc *tile_data, TokenExtra **tp,
5291*77c1e3ccSAndroid Build Coastguard Worker                              SIMPLE_MOTION_DATA_TREE *sms_root, int mi_row,
5292*77c1e3ccSAndroid Build Coastguard Worker                              int mi_col, const BLOCK_SIZE bsize,
5293*77c1e3ccSAndroid Build Coastguard Worker                              RD_STATS *best_rd_cost) {
5294*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
5295*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->ext_part_controller.ready) {
5296*77c1e3ccSAndroid Build Coastguard Worker     bool valid_search = true;
5297*77c1e3ccSAndroid Build Coastguard Worker     const aom_ext_part_decision_mode_t decision_mode =
5298*77c1e3ccSAndroid Build Coastguard Worker         av1_get_ext_part_decision_mode(&cpi->ext_part_controller);
5299*77c1e3ccSAndroid Build Coastguard Worker     if (decision_mode == AOM_EXT_PART_WHOLE_TREE) {
5300*77c1e3ccSAndroid Build Coastguard Worker       valid_search = ml_partition_search_whole_tree(
5301*77c1e3ccSAndroid Build Coastguard Worker           cpi, td, tile_data, tp, sms_root, mi_row, mi_col, bsize);
5302*77c1e3ccSAndroid Build Coastguard Worker     } else if (decision_mode == AOM_EXT_PART_RECURSIVE) {
5303*77c1e3ccSAndroid Build Coastguard Worker       valid_search = ml_partition_search_partial(
5304*77c1e3ccSAndroid Build Coastguard Worker           cpi, td, tile_data, tp, sms_root, mi_row, mi_col, bsize);
5305*77c1e3ccSAndroid Build Coastguard Worker     } else {
5306*77c1e3ccSAndroid Build Coastguard Worker       assert(0 && "Unknown decision mode.");
5307*77c1e3ccSAndroid Build Coastguard Worker       return false;
5308*77c1e3ccSAndroid Build Coastguard Worker     }
5309*77c1e3ccSAndroid Build Coastguard Worker     if (!valid_search) {
5310*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(
5311*77c1e3ccSAndroid Build Coastguard Worker           cm->error, AOM_CODEC_ERROR,
5312*77c1e3ccSAndroid Build Coastguard Worker           "Invalid search from ML model, partition search failed");
5313*77c1e3ccSAndroid Build Coastguard Worker     }
5314*77c1e3ccSAndroid Build Coastguard Worker     return true;
5315*77c1e3ccSAndroid Build Coastguard Worker   }
5316*77c1e3ccSAndroid Build Coastguard Worker 
5317*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
5318*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
5319*77c1e3ccSAndroid Build Coastguard Worker   int best_idx = 0;
5320*77c1e3ccSAndroid Build Coastguard Worker   int64_t min_rdcost = INT64_MAX;
5321*77c1e3ccSAndroid Build Coastguard Worker   int num_configs;
5322*77c1e3ccSAndroid Build Coastguard Worker   int i = 0;
5323*77c1e3ccSAndroid Build Coastguard Worker   do {
5324*77c1e3ccSAndroid Build Coastguard Worker     td->pc_root = av1_alloc_pc_tree_node(bsize);
5325*77c1e3ccSAndroid Build Coastguard Worker     if (!td->pc_root)
5326*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
5327*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PC_TREE");
5328*77c1e3ccSAndroid Build Coastguard Worker     num_configs = read_partition_tree(cpi, td->pc_root, xd->error_info, i);
5329*77c1e3ccSAndroid Build Coastguard Worker     if (num_configs <= 0) {
5330*77c1e3ccSAndroid Build Coastguard Worker       av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
5331*77c1e3ccSAndroid Build Coastguard Worker                                  cpi->sf.part_sf.partition_search_type);
5332*77c1e3ccSAndroid Build Coastguard Worker       td->pc_root = NULL;
5333*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_ERROR, "Invalid configs.");
5334*77c1e3ccSAndroid Build Coastguard Worker     }
5335*77c1e3ccSAndroid Build Coastguard Worker     verify_write_partition_tree(cpi, td->pc_root, bsize, i, mi_row, mi_col);
5336*77c1e3ccSAndroid Build Coastguard Worker     if (i == 0) {
5337*77c1e3ccSAndroid Build Coastguard Worker       AOM_CHECK_MEM_ERROR(xd->error_info, x->rdcost,
5338*77c1e3ccSAndroid Build Coastguard Worker                           aom_calloc(num_configs, sizeof(*x->rdcost)));
5339*77c1e3ccSAndroid Build Coastguard Worker     }
5340*77c1e3ccSAndroid Build Coastguard Worker     // Encode the block with the given partition tree. Get rdcost and encoding
5341*77c1e3ccSAndroid Build Coastguard Worker     // time.
5342*77c1e3ccSAndroid Build Coastguard Worker     x->rdcost[i] = rd_search_for_fixed_partition(
5343*77c1e3ccSAndroid Build Coastguard Worker         cpi, td, tile_data, tp, sms_root, mi_row, mi_col, bsize, td->pc_root);
5344*77c1e3ccSAndroid Build Coastguard Worker 
5345*77c1e3ccSAndroid Build Coastguard Worker     if (x->rdcost[i].rdcost < min_rdcost) {
5346*77c1e3ccSAndroid Build Coastguard Worker       min_rdcost = x->rdcost[i].rdcost;
5347*77c1e3ccSAndroid Build Coastguard Worker       best_idx = i;
5348*77c1e3ccSAndroid Build Coastguard Worker       *best_rd_cost = x->rdcost[i];
5349*77c1e3ccSAndroid Build Coastguard Worker     }
5350*77c1e3ccSAndroid Build Coastguard Worker     av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
5351*77c1e3ccSAndroid Build Coastguard Worker                                cpi->sf.part_sf.partition_search_type);
5352*77c1e3ccSAndroid Build Coastguard Worker     td->pc_root = NULL;
5353*77c1e3ccSAndroid Build Coastguard Worker     ++i;
5354*77c1e3ccSAndroid Build Coastguard Worker   } while (i < num_configs);
5355*77c1e3ccSAndroid Build Coastguard Worker 
5356*77c1e3ccSAndroid Build Coastguard Worker   aom_free(x->rdcost);
5357*77c1e3ccSAndroid Build Coastguard Worker   x->rdcost = NULL;
5358*77c1e3ccSAndroid Build Coastguard Worker   // Encode with the partition configuration with the smallest rdcost.
5359*77c1e3ccSAndroid Build Coastguard Worker   td->pc_root = av1_alloc_pc_tree_node(bsize);
5360*77c1e3ccSAndroid Build Coastguard Worker   if (!td->pc_root)
5361*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
5362*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to allocate PC_TREE");
5363*77c1e3ccSAndroid Build Coastguard Worker   read_partition_tree(cpi, td->pc_root, xd->error_info, best_idx);
5364*77c1e3ccSAndroid Build Coastguard Worker   rd_search_for_fixed_partition(cpi, td, tile_data, tp, sms_root, mi_row,
5365*77c1e3ccSAndroid Build Coastguard Worker                                 mi_col, bsize, td->pc_root);
5366*77c1e3ccSAndroid Build Coastguard Worker   set_cb_offsets(x->cb_offset, 0, 0);
5367*77c1e3ccSAndroid Build Coastguard Worker   encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, OUTPUT_ENABLED, bsize,
5368*77c1e3ccSAndroid Build Coastguard Worker             td->pc_root, NULL);
5369*77c1e3ccSAndroid Build Coastguard Worker   av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
5370*77c1e3ccSAndroid Build Coastguard Worker                              cpi->sf.part_sf.partition_search_type);
5371*77c1e3ccSAndroid Build Coastguard Worker   td->pc_root = NULL;
5372*77c1e3ccSAndroid Build Coastguard Worker   ++cpi->sb_counter;
5373*77c1e3ccSAndroid Build Coastguard Worker 
5374*77c1e3ccSAndroid Build Coastguard Worker   return true;
5375*77c1e3ccSAndroid Build Coastguard Worker }
5376*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_PARTITION_SEARCH_ORDER
5377*77c1e3ccSAndroid Build Coastguard Worker 
should_do_dry_run_encode_for_current_block(BLOCK_SIZE sb_size,BLOCK_SIZE max_partition_size,int curr_block_index,BLOCK_SIZE bsize)5378*77c1e3ccSAndroid Build Coastguard Worker static inline bool should_do_dry_run_encode_for_current_block(
5379*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE sb_size, BLOCK_SIZE max_partition_size, int curr_block_index,
5380*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE bsize) {
5381*77c1e3ccSAndroid Build Coastguard Worker   if (bsize > max_partition_size) return false;
5382*77c1e3ccSAndroid Build Coastguard Worker 
5383*77c1e3ccSAndroid Build Coastguard Worker   // Enable the reconstruction with dry-run for the 4th sub-block only if its
5384*77c1e3ccSAndroid Build Coastguard Worker   // parent block's reconstruction with dry-run is skipped. If
5385*77c1e3ccSAndroid Build Coastguard Worker   // max_partition_size is the same as immediate split of superblock, then avoid
5386*77c1e3ccSAndroid Build Coastguard Worker   // reconstruction of the 4th sub-block, as this data is not consumed.
5387*77c1e3ccSAndroid Build Coastguard Worker   if (curr_block_index != 3) return true;
5388*77c1e3ccSAndroid Build Coastguard Worker 
5389*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE sub_sb_size =
5390*77c1e3ccSAndroid Build Coastguard Worker       get_partition_subsize(sb_size, PARTITION_SPLIT);
5391*77c1e3ccSAndroid Build Coastguard Worker   return bsize == max_partition_size && sub_sb_size != max_partition_size;
5392*77c1e3ccSAndroid Build Coastguard Worker }
5393*77c1e3ccSAndroid Build Coastguard Worker 
log_sub_block_var(const AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bs,double * var_min,double * var_max)5394*77c1e3ccSAndroid Build Coastguard Worker static void log_sub_block_var(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs,
5395*77c1e3ccSAndroid Build Coastguard Worker                               double *var_min, double *var_max) {
5396*77c1e3ccSAndroid Build Coastguard Worker   // This functions returns a the minimum and maximum log variances for 4x4
5397*77c1e3ccSAndroid Build Coastguard Worker   // sub blocks in the current block.
5398*77c1e3ccSAndroid Build Coastguard Worker 
5399*77c1e3ccSAndroid Build Coastguard Worker   const MACROBLOCKD *const xd = &x->e_mbd;
5400*77c1e3ccSAndroid Build Coastguard Worker   const int is_hbd = is_cur_buf_hbd(xd);
5401*77c1e3ccSAndroid Build Coastguard Worker   const int right_overflow =
5402*77c1e3ccSAndroid Build Coastguard Worker       (xd->mb_to_right_edge < 0) ? ((-xd->mb_to_right_edge) >> 3) : 0;
5403*77c1e3ccSAndroid Build Coastguard Worker   const int bottom_overflow =
5404*77c1e3ccSAndroid Build Coastguard Worker       (xd->mb_to_bottom_edge < 0) ? ((-xd->mb_to_bottom_edge) >> 3) : 0;
5405*77c1e3ccSAndroid Build Coastguard Worker   const int bw = MI_SIZE * mi_size_wide[bs] - right_overflow;
5406*77c1e3ccSAndroid Build Coastguard Worker   const int bh = MI_SIZE * mi_size_high[bs] - bottom_overflow;
5407*77c1e3ccSAndroid Build Coastguard Worker 
5408*77c1e3ccSAndroid Build Coastguard Worker   // Initialize minimum variance to a large value and maximum variance to 0.
5409*77c1e3ccSAndroid Build Coastguard Worker   double min_var_4x4 = (double)INT_MAX;
5410*77c1e3ccSAndroid Build Coastguard Worker   double max_var_4x4 = 0.0;
5411*77c1e3ccSAndroid Build Coastguard Worker 
5412*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < bh; i += MI_SIZE) {
5413*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < bw; j += MI_SIZE) {
5414*77c1e3ccSAndroid Build Coastguard Worker       int var;
5415*77c1e3ccSAndroid Build Coastguard Worker       // Calculate the 4x4 sub-block variance.
5416*77c1e3ccSAndroid Build Coastguard Worker       var = av1_calc_normalized_variance(
5417*77c1e3ccSAndroid Build Coastguard Worker           cpi->ppi->fn_ptr[BLOCK_4X4].vf,
5418*77c1e3ccSAndroid Build Coastguard Worker           x->plane[0].src.buf + (i * x->plane[0].src.stride) + j,
5419*77c1e3ccSAndroid Build Coastguard Worker           x->plane[0].src.stride, is_hbd);
5420*77c1e3ccSAndroid Build Coastguard Worker 
5421*77c1e3ccSAndroid Build Coastguard Worker       // Record min and max for over-arching block
5422*77c1e3ccSAndroid Build Coastguard Worker       min_var_4x4 = AOMMIN(min_var_4x4, var);
5423*77c1e3ccSAndroid Build Coastguard Worker       max_var_4x4 = AOMMAX(max_var_4x4, var);
5424*77c1e3ccSAndroid Build Coastguard Worker     }
5425*77c1e3ccSAndroid Build Coastguard Worker   }
5426*77c1e3ccSAndroid Build Coastguard Worker   *var_min = log1p(min_var_4x4 / 16.0);
5427*77c1e3ccSAndroid Build Coastguard Worker   *var_max = log1p(max_var_4x4 / 16.0);
5428*77c1e3ccSAndroid Build Coastguard Worker }
5429*77c1e3ccSAndroid Build Coastguard Worker 
set_sms_tree_partitioning(SIMPLE_MOTION_DATA_TREE * sms_tree,PARTITION_TYPE partition)5430*77c1e3ccSAndroid Build Coastguard Worker static inline void set_sms_tree_partitioning(SIMPLE_MOTION_DATA_TREE *sms_tree,
5431*77c1e3ccSAndroid Build Coastguard Worker                                              PARTITION_TYPE partition) {
5432*77c1e3ccSAndroid Build Coastguard Worker   if (sms_tree == NULL) return;
5433*77c1e3ccSAndroid Build Coastguard Worker   sms_tree->partitioning = partition;
5434*77c1e3ccSAndroid Build Coastguard Worker }
5435*77c1e3ccSAndroid Build Coastguard Worker 
5436*77c1e3ccSAndroid Build Coastguard Worker /*!\brief AV1 block partition search (full search).
5437*77c1e3ccSAndroid Build Coastguard Worker *
5438*77c1e3ccSAndroid Build Coastguard Worker * \ingroup partition_search
5439*77c1e3ccSAndroid Build Coastguard Worker * \callgraph
5440*77c1e3ccSAndroid Build Coastguard Worker * Searches for the best partition pattern for a block based on the
5441*77c1e3ccSAndroid Build Coastguard Worker * rate-distortion cost, and returns a bool value to indicate whether a valid
5442*77c1e3ccSAndroid Build Coastguard Worker * partition pattern is found. The partition can recursively go down to the
5443*77c1e3ccSAndroid Build Coastguard Worker * smallest block size.
5444*77c1e3ccSAndroid Build Coastguard Worker *
5445*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    cpi                Top-level encoder structure
5446*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    td                 Pointer to thread data
5447*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    tile_data          Pointer to struct holding adaptive
5448*77c1e3ccSAndroid Build Coastguard Worker data/contexts/models for the tile during
5449*77c1e3ccSAndroid Build Coastguard Worker encoding
5450*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    tp                 Pointer to the starting token
5451*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    mi_row             Row coordinate of the block in a step size
5452*77c1e3ccSAndroid Build Coastguard Worker of MI_SIZE
5453*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    mi_col             Column coordinate of the block in a step
5454*77c1e3ccSAndroid Build Coastguard Worker size of MI_SIZE
5455*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    bsize              Current block size
5456*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    rd_cost            Pointer to the final rd cost of the block
5457*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    best_rdc           Upper bound of rd cost of a valid partition
5458*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    pc_tree            Pointer to the PC_TREE node storing the
5459*77c1e3ccSAndroid Build Coastguard Worker picked partitions and mode info for the
5460*77c1e3ccSAndroid Build Coastguard Worker current block
5461*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    sms_tree           Pointer to struct holding simple motion
5462*77c1e3ccSAndroid Build Coastguard Worker search data for the current block
5463*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    none_rd            Pointer to the rd cost in the case of not
5464*77c1e3ccSAndroid Build Coastguard Worker splitting the current block
5465*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    multi_pass_mode    SB_SINGLE_PASS/SB_DRY_PASS/SB_WET_PASS
5466*77c1e3ccSAndroid Build Coastguard Worker * \param[in]    rect_part_win_info Pointer to struct storing whether horz/vert
5467*77c1e3ccSAndroid Build Coastguard Worker partition outperforms previously tested
5468*77c1e3ccSAndroid Build Coastguard Worker partitions
5469*77c1e3ccSAndroid Build Coastguard Worker *
5470*77c1e3ccSAndroid Build Coastguard Worker * \return A bool value is returned indicating if a valid partition is found.
5471*77c1e3ccSAndroid Build Coastguard Worker * The pc_tree struct is modified to store the picked partition and modes.
5472*77c1e3ccSAndroid Build Coastguard Worker * The rd_cost struct is also updated with the RD stats corresponding to the
5473*77c1e3ccSAndroid Build Coastguard Worker * best partition found.
5474*77c1e3ccSAndroid Build Coastguard Worker */
av1_rd_pick_partition(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,int mi_row,int mi_col,BLOCK_SIZE bsize,RD_STATS * rd_cost,RD_STATS best_rdc,PC_TREE * pc_tree,SIMPLE_MOTION_DATA_TREE * sms_tree,int64_t * none_rd,SB_MULTI_PASS_MODE multi_pass_mode,RD_RECT_PART_WIN_INFO * rect_part_win_info)5475*77c1e3ccSAndroid Build Coastguard Worker bool av1_rd_pick_partition(AV1_COMP *const cpi, ThreadData *td,
5476*77c1e3ccSAndroid Build Coastguard Worker                            TileDataEnc *tile_data, TokenExtra **tp, int mi_row,
5477*77c1e3ccSAndroid Build Coastguard Worker                            int mi_col, BLOCK_SIZE bsize, RD_STATS *rd_cost,
5478*77c1e3ccSAndroid Build Coastguard Worker                            RD_STATS best_rdc, PC_TREE *pc_tree,
5479*77c1e3ccSAndroid Build Coastguard Worker                            SIMPLE_MOTION_DATA_TREE *sms_tree, int64_t *none_rd,
5480*77c1e3ccSAndroid Build Coastguard Worker                            SB_MULTI_PASS_MODE multi_pass_mode,
5481*77c1e3ccSAndroid Build Coastguard Worker                            RD_RECT_PART_WIN_INFO *rect_part_win_info) {
5482*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
5483*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
5484*77c1e3ccSAndroid Build Coastguard Worker   TileInfo *const tile_info = &tile_data->tile_info;
5485*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
5486*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
5487*77c1e3ccSAndroid Build Coastguard Worker   RD_SEARCH_MACROBLOCK_CONTEXT x_ctx;
5488*77c1e3ccSAndroid Build Coastguard Worker   const TokenExtra *const tp_orig = *tp;
5489*77c1e3ccSAndroid Build Coastguard Worker   PartitionSearchState part_search_state;
5490*77c1e3ccSAndroid Build Coastguard Worker 
5491*77c1e3ccSAndroid Build Coastguard Worker   // Initialization of state variables used in partition search.
5492*77c1e3ccSAndroid Build Coastguard Worker   init_partition_search_state_params(x, cpi, &part_search_state, mi_row, mi_col,
5493*77c1e3ccSAndroid Build Coastguard Worker                                      bsize);
5494*77c1e3ccSAndroid Build Coastguard Worker   PartitionBlkParams blk_params = part_search_state.part_blk_params;
5495*77c1e3ccSAndroid Build Coastguard Worker 
5496*77c1e3ccSAndroid Build Coastguard Worker   set_sms_tree_partitioning(sms_tree, PARTITION_NONE);
5497*77c1e3ccSAndroid Build Coastguard Worker   if (best_rdc.rdcost < 0) {
5498*77c1e3ccSAndroid Build Coastguard Worker     av1_invalid_rd_stats(rd_cost);
5499*77c1e3ccSAndroid Build Coastguard Worker     return part_search_state.found_best_partition;
5500*77c1e3ccSAndroid Build Coastguard Worker   }
5501*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == cm->seq_params->sb_size) x->must_find_valid_partition = 0;
5502*77c1e3ccSAndroid Build Coastguard Worker 
5503*77c1e3ccSAndroid Build Coastguard Worker   // Override skipping rectangular partition operations for edge blocks.
5504*77c1e3ccSAndroid Build Coastguard Worker   if (none_rd) *none_rd = 0;
5505*77c1e3ccSAndroid Build Coastguard Worker   (void)*tp_orig;
5506*77c1e3ccSAndroid Build Coastguard Worker 
5507*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
5508*77c1e3ccSAndroid Build Coastguard Worker   // Stats at the current quad tree
5509*77c1e3ccSAndroid Build Coastguard Worker   PartitionTimingStats *part_timing_stats =
5510*77c1e3ccSAndroid Build Coastguard Worker       &part_search_state.part_timing_stats;
5511*77c1e3ccSAndroid Build Coastguard Worker   // Stats aggregated at frame level
5512*77c1e3ccSAndroid Build Coastguard Worker   FramePartitionTimingStats *fr_part_timing_stats = &cpi->partition_stats;
5513*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_COLLECT_PARTITION_STATS
5514*77c1e3ccSAndroid Build Coastguard Worker 
5515*77c1e3ccSAndroid Build Coastguard Worker   // Override partition costs at the edges of the frame in the same
5516*77c1e3ccSAndroid Build Coastguard Worker   // way as in read_partition (see decodeframe.c).
5517*77c1e3ccSAndroid Build Coastguard Worker   if (!av1_blk_has_rows_and_cols(&blk_params))
5518*77c1e3ccSAndroid Build Coastguard Worker     set_partition_cost_for_edge_blk(cm, &part_search_state);
5519*77c1e3ccSAndroid Build Coastguard Worker 
5520*77c1e3ccSAndroid Build Coastguard Worker   // Disable rectangular partitions for inner blocks when the current block is
5521*77c1e3ccSAndroid Build Coastguard Worker   // forced to only use square partitions.
5522*77c1e3ccSAndroid Build Coastguard Worker   if (bsize > cpi->sf.part_sf.use_square_partition_only_threshold) {
5523*77c1e3ccSAndroid Build Coastguard Worker     part_search_state.partition_rect_allowed[HORZ] &= !blk_params.has_rows;
5524*77c1e3ccSAndroid Build Coastguard Worker     part_search_state.partition_rect_allowed[VERT] &= !blk_params.has_cols;
5525*77c1e3ccSAndroid Build Coastguard Worker   }
5526*77c1e3ccSAndroid Build Coastguard Worker 
5527*77c1e3ccSAndroid Build Coastguard Worker #ifndef NDEBUG
5528*77c1e3ccSAndroid Build Coastguard Worker   // Nothing should rely on the default value of this array (which is just
5529*77c1e3ccSAndroid Build Coastguard Worker   // leftover from encoding the previous block. Setting it to fixed pattern
5530*77c1e3ccSAndroid Build Coastguard Worker   // when debugging.
5531*77c1e3ccSAndroid Build Coastguard Worker   // bit 0, 1, 2 are blk_skip of each plane
5532*77c1e3ccSAndroid Build Coastguard Worker   // bit 4, 5, 6 are initialization checking of each plane
5533*77c1e3ccSAndroid Build Coastguard Worker   memset(x->txfm_search_info.blk_skip, 0x77,
5534*77c1e3ccSAndroid Build Coastguard Worker          sizeof(x->txfm_search_info.blk_skip));
5535*77c1e3ccSAndroid Build Coastguard Worker #endif  // NDEBUG
5536*77c1e3ccSAndroid Build Coastguard Worker 
5537*77c1e3ccSAndroid Build Coastguard Worker   assert(mi_size_wide[bsize] == mi_size_high[bsize]);
5538*77c1e3ccSAndroid Build Coastguard Worker 
5539*77c1e3ccSAndroid Build Coastguard Worker   // Set buffers and offsets.
5540*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
5541*77c1e3ccSAndroid Build Coastguard Worker 
5542*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.mode == ALLINTRA) {
5543*77c1e3ccSAndroid Build Coastguard Worker     if (bsize == cm->seq_params->sb_size) {
5544*77c1e3ccSAndroid Build Coastguard Worker       double var_min, var_max;
5545*77c1e3ccSAndroid Build Coastguard Worker       log_sub_block_var(cpi, x, bsize, &var_min, &var_max);
5546*77c1e3ccSAndroid Build Coastguard Worker 
5547*77c1e3ccSAndroid Build Coastguard Worker       x->intra_sb_rdmult_modifier = 128;
5548*77c1e3ccSAndroid Build Coastguard Worker       if ((var_min < 2.0) && (var_max > 4.0)) {
5549*77c1e3ccSAndroid Build Coastguard Worker         if ((var_max - var_min) > 8.0) {
5550*77c1e3ccSAndroid Build Coastguard Worker           x->intra_sb_rdmult_modifier -= 48;
5551*77c1e3ccSAndroid Build Coastguard Worker         } else {
5552*77c1e3ccSAndroid Build Coastguard Worker           x->intra_sb_rdmult_modifier -= (int)((var_max - var_min) * 6);
5553*77c1e3ccSAndroid Build Coastguard Worker         }
5554*77c1e3ccSAndroid Build Coastguard Worker       }
5555*77c1e3ccSAndroid Build Coastguard Worker     }
5556*77c1e3ccSAndroid Build Coastguard Worker   }
5557*77c1e3ccSAndroid Build Coastguard Worker 
5558*77c1e3ccSAndroid Build Coastguard Worker   // Save rdmult before it might be changed, so it can be restored later.
5559*77c1e3ccSAndroid Build Coastguard Worker   const int orig_rdmult = x->rdmult;
5560*77c1e3ccSAndroid Build Coastguard Worker   setup_block_rdmult(cpi, x, mi_row, mi_col, bsize, NO_AQ, NULL);
5561*77c1e3ccSAndroid Build Coastguard Worker 
5562*77c1e3ccSAndroid Build Coastguard Worker   // Apply simple motion search for the entire super block with fixed block
5563*77c1e3ccSAndroid Build Coastguard Worker   // size, e.g., 16x16, to collect features and write to files for the
5564*77c1e3ccSAndroid Build Coastguard Worker   // external ML model.
5565*77c1e3ccSAndroid Build Coastguard Worker   // TODO(chengchen): reduce motion search. This function is similar to
5566*77c1e3ccSAndroid Build Coastguard Worker   // av1_get_max_min_partition_features().
5567*77c1e3ccSAndroid Build Coastguard Worker   if (COLLECT_MOTION_SEARCH_FEATURE_SB && !frame_is_intra_only(cm) &&
5568*77c1e3ccSAndroid Build Coastguard Worker       bsize == cm->seq_params->sb_size) {
5569*77c1e3ccSAndroid Build Coastguard Worker     av1_collect_motion_search_features_sb(cpi, td, tile_data, mi_row, mi_col,
5570*77c1e3ccSAndroid Build Coastguard Worker                                           bsize, /*features=*/NULL);
5571*77c1e3ccSAndroid Build Coastguard Worker     collect_tpl_stats_sb(cpi, bsize, mi_row, mi_col, /*features=*/NULL);
5572*77c1e3ccSAndroid Build Coastguard Worker   }
5573*77c1e3ccSAndroid Build Coastguard Worker 
5574*77c1e3ccSAndroid Build Coastguard Worker   // Update rd cost of the bound using the current multiplier.
5575*77c1e3ccSAndroid Build Coastguard Worker   av1_rd_cost_update(x->rdmult, &best_rdc);
5576*77c1e3ccSAndroid Build Coastguard Worker 
5577*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == BLOCK_16X16 && cpi->vaq_refresh)
5578*77c1e3ccSAndroid Build Coastguard Worker     x->mb_energy = av1_log_block_var(cpi, x, bsize);
5579*77c1e3ccSAndroid Build Coastguard Worker 
5580*77c1e3ccSAndroid Build Coastguard Worker   // Set the context.
5581*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context =
5582*77c1e3ccSAndroid Build Coastguard Worker       cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
5583*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
5584*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
5585*77c1e3ccSAndroid Build Coastguard Worker   av1_save_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
5586*77c1e3ccSAndroid Build Coastguard Worker 
5587*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5588*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, av1_prune_partitions_time);
5589*77c1e3ccSAndroid Build Coastguard Worker #endif
5590*77c1e3ccSAndroid Build Coastguard Worker   // Pruning: before searching any partition type, using source and simple
5591*77c1e3ccSAndroid Build Coastguard Worker   // motion search results to prune out unlikely partitions.
5592*77c1e3ccSAndroid Build Coastguard Worker   av1_prune_partitions_before_search(cpi, x, sms_tree, &part_search_state);
5593*77c1e3ccSAndroid Build Coastguard Worker 
5594*77c1e3ccSAndroid Build Coastguard Worker   // Pruning: eliminating partition types leading to coding block sizes outside
5595*77c1e3ccSAndroid Build Coastguard Worker   // the min and max bsize limitations set from the encoder.
5596*77c1e3ccSAndroid Build Coastguard Worker   av1_prune_partitions_by_max_min_bsize(&x->sb_enc, &part_search_state);
5597*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5598*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, av1_prune_partitions_time);
5599*77c1e3ccSAndroid Build Coastguard Worker #endif
5600*77c1e3ccSAndroid Build Coastguard Worker 
5601*77c1e3ccSAndroid Build Coastguard Worker   // Partition search
5602*77c1e3ccSAndroid Build Coastguard Worker BEGIN_PARTITION_SEARCH:
5603*77c1e3ccSAndroid Build Coastguard Worker   // If a valid partition is required, usually when the first round cannot find
5604*77c1e3ccSAndroid Build Coastguard Worker   // a valid one under the cost limit after pruning, reset the limitations on
5605*77c1e3ccSAndroid Build Coastguard Worker   // partition types and intra cnn output.
5606*77c1e3ccSAndroid Build Coastguard Worker   if (x->must_find_valid_partition) {
5607*77c1e3ccSAndroid Build Coastguard Worker     reset_part_limitations(cpi, &part_search_state);
5608*77c1e3ccSAndroid Build Coastguard Worker     av1_prune_partitions_by_max_min_bsize(&x->sb_enc, &part_search_state);
5609*77c1e3ccSAndroid Build Coastguard Worker     // Invalidate intra cnn output for key frames.
5610*77c1e3ccSAndroid Build Coastguard Worker     if (frame_is_intra_only(cm) && bsize == BLOCK_64X64) {
5611*77c1e3ccSAndroid Build Coastguard Worker       part_search_state.intra_part_info->quad_tree_idx = 0;
5612*77c1e3ccSAndroid Build Coastguard Worker       part_search_state.intra_part_info->cnn_output_valid = 0;
5613*77c1e3ccSAndroid Build Coastguard Worker     }
5614*77c1e3ccSAndroid Build Coastguard Worker   }
5615*77c1e3ccSAndroid Build Coastguard Worker   // Partition block source pixel variance.
5616*77c1e3ccSAndroid Build Coastguard Worker   unsigned int pb_source_variance = UINT_MAX;
5617*77c1e3ccSAndroid Build Coastguard Worker 
5618*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5619*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, none_partition_search_time);
5620*77c1e3ccSAndroid Build Coastguard Worker #endif
5621*77c1e3ccSAndroid Build Coastguard Worker 
5622*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.mode == ALLINTRA) {
5623*77c1e3ccSAndroid Build Coastguard Worker     const bool bsize_at_least_16x16 = (bsize >= BLOCK_16X16);
5624*77c1e3ccSAndroid Build Coastguard Worker     const bool prune_rect_part_using_4x4_var_deviation =
5625*77c1e3ccSAndroid Build Coastguard Worker         (cpi->sf.part_sf.prune_rect_part_using_4x4_var_deviation &&
5626*77c1e3ccSAndroid Build Coastguard Worker          !x->must_find_valid_partition);
5627*77c1e3ccSAndroid Build Coastguard Worker 
5628*77c1e3ccSAndroid Build Coastguard Worker     if (bsize_at_least_16x16 || prune_rect_part_using_4x4_var_deviation) {
5629*77c1e3ccSAndroid Build Coastguard Worker       double var_min, var_max;
5630*77c1e3ccSAndroid Build Coastguard Worker       log_sub_block_var(cpi, x, bsize, &var_min, &var_max);
5631*77c1e3ccSAndroid Build Coastguard Worker 
5632*77c1e3ccSAndroid Build Coastguard Worker       // Further pruning or in some cases reverse pruning when allintra is set.
5633*77c1e3ccSAndroid Build Coastguard Worker       // This code helps visual and in some cases metrics quality where the
5634*77c1e3ccSAndroid Build Coastguard Worker       // current block comprises at least one very low variance sub-block and at
5635*77c1e3ccSAndroid Build Coastguard Worker       // least one where the variance is much higher.
5636*77c1e3ccSAndroid Build Coastguard Worker       //
5637*77c1e3ccSAndroid Build Coastguard Worker       // The idea is that in such cases there is danger of ringing and other
5638*77c1e3ccSAndroid Build Coastguard Worker       // visual artifacts from a high variance feature such as an edge into a
5639*77c1e3ccSAndroid Build Coastguard Worker       // very low variance region.
5640*77c1e3ccSAndroid Build Coastguard Worker       //
5641*77c1e3ccSAndroid Build Coastguard Worker       // The approach taken is to force break down / split to a smaller block
5642*77c1e3ccSAndroid Build Coastguard Worker       // size to try and separate out the low variance and well predicted blocks
5643*77c1e3ccSAndroid Build Coastguard Worker       // from the more complex ones and to prevent propagation of ringing over a
5644*77c1e3ccSAndroid Build Coastguard Worker       // large region.
5645*77c1e3ccSAndroid Build Coastguard Worker       if (bsize_at_least_16x16 && (var_min < 0.272) &&
5646*77c1e3ccSAndroid Build Coastguard Worker           ((var_max - var_min) > 3.0)) {
5647*77c1e3ccSAndroid Build Coastguard Worker         part_search_state.partition_none_allowed = 0;
5648*77c1e3ccSAndroid Build Coastguard Worker         part_search_state.terminate_partition_search = 0;
5649*77c1e3ccSAndroid Build Coastguard Worker         part_search_state.do_square_split = 1;
5650*77c1e3ccSAndroid Build Coastguard Worker       } else if (prune_rect_part_using_4x4_var_deviation &&
5651*77c1e3ccSAndroid Build Coastguard Worker                  (var_max - var_min < 3.0)) {
5652*77c1e3ccSAndroid Build Coastguard Worker         // Prune rectangular partitions if the variance deviation of 4x4
5653*77c1e3ccSAndroid Build Coastguard Worker         // sub-blocks within the block is less than a threshold (derived
5654*77c1e3ccSAndroid Build Coastguard Worker         // empirically).
5655*77c1e3ccSAndroid Build Coastguard Worker         part_search_state.do_rectangular_split = 0;
5656*77c1e3ccSAndroid Build Coastguard Worker       }
5657*77c1e3ccSAndroid Build Coastguard Worker     }
5658*77c1e3ccSAndroid Build Coastguard Worker   }
5659*77c1e3ccSAndroid Build Coastguard Worker 
5660*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_NONE search stage.
5661*77c1e3ccSAndroid Build Coastguard Worker   int64_t part_none_rd = INT64_MAX;
5662*77c1e3ccSAndroid Build Coastguard Worker   none_partition_search(cpi, td, tile_data, x, pc_tree, sms_tree, &x_ctx,
5663*77c1e3ccSAndroid Build Coastguard Worker                         &part_search_state, &best_rdc, &pb_source_variance,
5664*77c1e3ccSAndroid Build Coastguard Worker                         none_rd, &part_none_rd);
5665*77c1e3ccSAndroid Build Coastguard Worker 
5666*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5667*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, none_partition_search_time);
5668*77c1e3ccSAndroid Build Coastguard Worker #endif
5669*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5670*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, split_partition_search_time);
5671*77c1e3ccSAndroid Build Coastguard Worker #endif
5672*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_SPLIT search stage.
5673*77c1e3ccSAndroid Build Coastguard Worker   int64_t part_split_rd = INT64_MAX;
5674*77c1e3ccSAndroid Build Coastguard Worker   split_partition_search(cpi, td, tile_data, tp, x, pc_tree, sms_tree, &x_ctx,
5675*77c1e3ccSAndroid Build Coastguard Worker                          &part_search_state, &best_rdc, multi_pass_mode,
5676*77c1e3ccSAndroid Build Coastguard Worker                          &part_split_rd);
5677*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5678*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, split_partition_search_time);
5679*77c1e3ccSAndroid Build Coastguard Worker #endif
5680*77c1e3ccSAndroid Build Coastguard Worker   // Terminate partition search for child partition,
5681*77c1e3ccSAndroid Build Coastguard Worker   // when NONE and SPLIT partition rd_costs are INT64_MAX.
5682*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.early_term_after_none_split &&
5683*77c1e3ccSAndroid Build Coastguard Worker       part_none_rd == INT64_MAX && part_split_rd == INT64_MAX &&
5684*77c1e3ccSAndroid Build Coastguard Worker       !x->must_find_valid_partition && (bsize != cm->seq_params->sb_size)) {
5685*77c1e3ccSAndroid Build Coastguard Worker     part_search_state.terminate_partition_search = 1;
5686*77c1e3ccSAndroid Build Coastguard Worker   }
5687*77c1e3ccSAndroid Build Coastguard Worker 
5688*77c1e3ccSAndroid Build Coastguard Worker   // Do not evaluate non-square partitions if NONE partition did not choose a
5689*77c1e3ccSAndroid Build Coastguard Worker   // newmv mode and is skippable.
5690*77c1e3ccSAndroid Build Coastguard Worker   if ((cpi->sf.part_sf.skip_non_sq_part_based_on_none >= 2) &&
5691*77c1e3ccSAndroid Build Coastguard Worker       (pc_tree->none != NULL)) {
5692*77c1e3ccSAndroid Build Coastguard Worker     if (x->qindex <= 200 && is_inter_mode(pc_tree->none->mic.mode) &&
5693*77c1e3ccSAndroid Build Coastguard Worker         !have_newmv_in_inter_mode(pc_tree->none->mic.mode) &&
5694*77c1e3ccSAndroid Build Coastguard Worker         pc_tree->none->skippable && !x->must_find_valid_partition &&
5695*77c1e3ccSAndroid Build Coastguard Worker         bsize >= BLOCK_16X16)
5696*77c1e3ccSAndroid Build Coastguard Worker       part_search_state.do_rectangular_split = 0;
5697*77c1e3ccSAndroid Build Coastguard Worker   }
5698*77c1e3ccSAndroid Build Coastguard Worker 
5699*77c1e3ccSAndroid Build Coastguard Worker   // Prune partitions based on PARTITION_NONE and PARTITION_SPLIT.
5700*77c1e3ccSAndroid Build Coastguard Worker   prune_partitions_after_split(cpi, x, sms_tree, &part_search_state, &best_rdc,
5701*77c1e3ccSAndroid Build Coastguard Worker                                part_none_rd, part_split_rd);
5702*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5703*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, rectangular_partition_search_time);
5704*77c1e3ccSAndroid Build Coastguard Worker #endif
5705*77c1e3ccSAndroid Build Coastguard Worker   // Rectangular partitions search stage.
5706*77c1e3ccSAndroid Build Coastguard Worker   rectangular_partition_search(cpi, td, tile_data, tp, x, pc_tree, &x_ctx,
5707*77c1e3ccSAndroid Build Coastguard Worker                                &part_search_state, &best_rdc,
5708*77c1e3ccSAndroid Build Coastguard Worker                                rect_part_win_info, HORZ, VERT);
5709*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5710*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, rectangular_partition_search_time);
5711*77c1e3ccSAndroid Build Coastguard Worker #endif
5712*77c1e3ccSAndroid Build Coastguard Worker 
5713*77c1e3ccSAndroid Build Coastguard Worker   if (pb_source_variance == UINT_MAX) {
5714*77c1e3ccSAndroid Build Coastguard Worker     av1_setup_src_planes(x, cpi->source, mi_row, mi_col, num_planes, bsize);
5715*77c1e3ccSAndroid Build Coastguard Worker     pb_source_variance = av1_get_perpixel_variance_facade(
5716*77c1e3ccSAndroid Build Coastguard Worker         cpi, xd, &x->plane[0].src, bsize, AOM_PLANE_Y);
5717*77c1e3ccSAndroid Build Coastguard Worker   }
5718*77c1e3ccSAndroid Build Coastguard Worker 
5719*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(!cpi->oxcf.part_cfg.enable_rect_partitions,
5720*77c1e3ccSAndroid Build Coastguard Worker                  !part_search_state.do_rectangular_split));
5721*77c1e3ccSAndroid Build Coastguard Worker 
5722*77c1e3ccSAndroid Build Coastguard Worker   const int prune_ext_part_state = prune_ext_part_none_skippable(
5723*77c1e3ccSAndroid Build Coastguard Worker       pc_tree->none, x->must_find_valid_partition,
5724*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.part_sf.skip_non_sq_part_based_on_none, bsize);
5725*77c1e3ccSAndroid Build Coastguard Worker 
5726*77c1e3ccSAndroid Build Coastguard Worker   const int ab_partition_allowed = allow_ab_partition_search(
5727*77c1e3ccSAndroid Build Coastguard Worker       &part_search_state, &cpi->sf.part_sf, pc_tree->partitioning,
5728*77c1e3ccSAndroid Build Coastguard Worker       x->must_find_valid_partition, prune_ext_part_state, best_rdc.rdcost);
5729*77c1e3ccSAndroid Build Coastguard Worker 
5730*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5731*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, ab_partitions_search_time);
5732*77c1e3ccSAndroid Build Coastguard Worker #endif
5733*77c1e3ccSAndroid Build Coastguard Worker   // AB partitions search stage.
5734*77c1e3ccSAndroid Build Coastguard Worker   ab_partitions_search(cpi, td, tile_data, tp, x, &x_ctx, pc_tree,
5735*77c1e3ccSAndroid Build Coastguard Worker                        &part_search_state, &best_rdc, rect_part_win_info,
5736*77c1e3ccSAndroid Build Coastguard Worker                        pb_source_variance, ab_partition_allowed, HORZ_A,
5737*77c1e3ccSAndroid Build Coastguard Worker                        VERT_B);
5738*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5739*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, ab_partitions_search_time);
5740*77c1e3ccSAndroid Build Coastguard Worker #endif
5741*77c1e3ccSAndroid Build Coastguard Worker 
5742*77c1e3ccSAndroid Build Coastguard Worker   // 4-way partitions search stage.
5743*77c1e3ccSAndroid Build Coastguard Worker   int part4_search_allowed[NUM_PART4_TYPES] = { 1, 1 };
5744*77c1e3ccSAndroid Build Coastguard Worker   // Prune 4-way partition search.
5745*77c1e3ccSAndroid Build Coastguard Worker   prune_4_way_partition_search(cpi, x, pc_tree, &part_search_state, &best_rdc,
5746*77c1e3ccSAndroid Build Coastguard Worker                                pb_source_variance, prune_ext_part_state,
5747*77c1e3ccSAndroid Build Coastguard Worker                                part4_search_allowed);
5748*77c1e3ccSAndroid Build Coastguard Worker 
5749*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5750*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, rd_pick_4partition_time);
5751*77c1e3ccSAndroid Build Coastguard Worker #endif
5752*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_HORZ_4
5753*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(!cpi->oxcf.part_cfg.enable_rect_partitions,
5754*77c1e3ccSAndroid Build Coastguard Worker                  !part4_search_allowed[HORZ4]));
5755*77c1e3ccSAndroid Build Coastguard Worker   if (!part_search_state.terminate_partition_search &&
5756*77c1e3ccSAndroid Build Coastguard Worker       part4_search_allowed[HORZ4]) {
5757*77c1e3ccSAndroid Build Coastguard Worker     const int inc_step[NUM_PART4_TYPES] = { mi_size_high[blk_params.bsize] / 4,
5758*77c1e3ccSAndroid Build Coastguard Worker                                             0 };
5759*77c1e3ccSAndroid Build Coastguard Worker     // Evaluation of Horz4 partition type.
5760*77c1e3ccSAndroid Build Coastguard Worker     rd_pick_4partition(cpi, td, tile_data, tp, x, &x_ctx, pc_tree,
5761*77c1e3ccSAndroid Build Coastguard Worker                        pc_tree->horizontal4, &part_search_state, &best_rdc,
5762*77c1e3ccSAndroid Build Coastguard Worker                        inc_step, PARTITION_HORZ_4);
5763*77c1e3ccSAndroid Build Coastguard Worker   }
5764*77c1e3ccSAndroid Build Coastguard Worker 
5765*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_VERT_4
5766*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(!cpi->oxcf.part_cfg.enable_rect_partitions,
5767*77c1e3ccSAndroid Build Coastguard Worker                  !part4_search_allowed[VERT4]));
5768*77c1e3ccSAndroid Build Coastguard Worker   if (!part_search_state.terminate_partition_search &&
5769*77c1e3ccSAndroid Build Coastguard Worker       part4_search_allowed[VERT4] && blk_params.has_cols) {
5770*77c1e3ccSAndroid Build Coastguard Worker     const int inc_step[NUM_PART4_TYPES] = { 0, mi_size_wide[blk_params.bsize] /
5771*77c1e3ccSAndroid Build Coastguard Worker                                                    4 };
5772*77c1e3ccSAndroid Build Coastguard Worker     // Evaluation of Vert4 partition type.
5773*77c1e3ccSAndroid Build Coastguard Worker     rd_pick_4partition(cpi, td, tile_data, tp, x, &x_ctx, pc_tree,
5774*77c1e3ccSAndroid Build Coastguard Worker                        pc_tree->vertical4, &part_search_state, &best_rdc,
5775*77c1e3ccSAndroid Build Coastguard Worker                        inc_step, PARTITION_VERT_4);
5776*77c1e3ccSAndroid Build Coastguard Worker   }
5777*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5778*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, rd_pick_4partition_time);
5779*77c1e3ccSAndroid Build Coastguard Worker #endif
5780*77c1e3ccSAndroid Build Coastguard Worker 
5781*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == cm->seq_params->sb_size &&
5782*77c1e3ccSAndroid Build Coastguard Worker       !part_search_state.found_best_partition) {
5783*77c1e3ccSAndroid Build Coastguard Worker     // Did not find a valid partition, go back and search again, with less
5784*77c1e3ccSAndroid Build Coastguard Worker     // constraint on which partition types to search.
5785*77c1e3ccSAndroid Build Coastguard Worker     x->must_find_valid_partition = 1;
5786*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
5787*77c1e3ccSAndroid Build Coastguard Worker     fr_part_timing_stats->partition_redo += 1;
5788*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_COLLECT_PARTITION_STATS
5789*77c1e3ccSAndroid Build Coastguard Worker     goto BEGIN_PARTITION_SEARCH;
5790*77c1e3ccSAndroid Build Coastguard Worker   }
5791*77c1e3ccSAndroid Build Coastguard Worker 
5792*77c1e3ccSAndroid Build Coastguard Worker   // Store the final rd cost
5793*77c1e3ccSAndroid Build Coastguard Worker   *rd_cost = best_rdc;
5794*77c1e3ccSAndroid Build Coastguard Worker 
5795*77c1e3ccSAndroid Build Coastguard Worker   // Also record the best partition in simple motion data tree because it is
5796*77c1e3ccSAndroid Build Coastguard Worker   // necessary for the related speed features.
5797*77c1e3ccSAndroid Build Coastguard Worker   set_sms_tree_partitioning(sms_tree, pc_tree->partitioning);
5798*77c1e3ccSAndroid Build Coastguard Worker 
5799*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
5800*77c1e3ccSAndroid Build Coastguard Worker   if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX) {
5801*77c1e3ccSAndroid Build Coastguard Worker     part_timing_stats->partition_decisions[pc_tree->partitioning] += 1;
5802*77c1e3ccSAndroid Build Coastguard Worker   }
5803*77c1e3ccSAndroid Build Coastguard Worker 
5804*77c1e3ccSAndroid Build Coastguard Worker   // If CONFIG_COLLECT_PARTITION_STATS is 1, then print out the stats for each
5805*77c1e3ccSAndroid Build Coastguard Worker   // prediction block.
5806*77c1e3ccSAndroid Build Coastguard Worker   print_partition_timing_stats_with_rdcost(
5807*77c1e3ccSAndroid Build Coastguard Worker       part_timing_stats, mi_row, mi_col, bsize,
5808*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->gf_group.update_type[cpi->gf_frame_index],
5809*77c1e3ccSAndroid Build Coastguard Worker       cm->current_frame.frame_number, &best_rdc, "part_timing.csv");
5810*77c1e3ccSAndroid Build Coastguard Worker   const bool print_timing_stats = false;
5811*77c1e3ccSAndroid Build Coastguard Worker   if (print_timing_stats) {
5812*77c1e3ccSAndroid Build Coastguard Worker     print_partition_timing_stats(part_timing_stats, cm->show_frame,
5813*77c1e3ccSAndroid Build Coastguard Worker                                  frame_is_intra_only(cm), bsize,
5814*77c1e3ccSAndroid Build Coastguard Worker                                  "part_timing_data.csv");
5815*77c1e3ccSAndroid Build Coastguard Worker   }
5816*77c1e3ccSAndroid Build Coastguard Worker   // If CONFIG_COLLECTION_PARTITION_STATS is 2, then we print out the stats for
5817*77c1e3ccSAndroid Build Coastguard Worker   // the whole clip. So we need to pass the information upstream to the encoder.
5818*77c1e3ccSAndroid Build Coastguard Worker   accumulate_partition_timing_stats(fr_part_timing_stats, part_timing_stats,
5819*77c1e3ccSAndroid Build Coastguard Worker                                     bsize);
5820*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_COLLECT_PARTITION_STATS
5821*77c1e3ccSAndroid Build Coastguard Worker 
5822*77c1e3ccSAndroid Build Coastguard Worker   // Reset the PC_TREE deallocation flag.
5823*77c1e3ccSAndroid Build Coastguard Worker   int pc_tree_dealloc = 0;
5824*77c1e3ccSAndroid Build Coastguard Worker 
5825*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5826*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, encode_sb_time);
5827*77c1e3ccSAndroid Build Coastguard Worker #endif
5828*77c1e3ccSAndroid Build Coastguard Worker   if (part_search_state.found_best_partition) {
5829*77c1e3ccSAndroid Build Coastguard Worker     if (bsize == cm->seq_params->sb_size) {
5830*77c1e3ccSAndroid Build Coastguard Worker       // Encode the superblock.
5831*77c1e3ccSAndroid Build Coastguard Worker       const int emit_output = multi_pass_mode != SB_DRY_PASS;
5832*77c1e3ccSAndroid Build Coastguard Worker       const RUN_TYPE run_type = emit_output ? OUTPUT_ENABLED : DRY_RUN_NORMAL;
5833*77c1e3ccSAndroid Build Coastguard Worker 
5834*77c1e3ccSAndroid Build Coastguard Worker       // Write partition tree to file. Not used by default.
5835*77c1e3ccSAndroid Build Coastguard Worker       if (COLLECT_MOTION_SEARCH_FEATURE_SB) {
5836*77c1e3ccSAndroid Build Coastguard Worker         write_partition_tree(cpi, pc_tree, bsize, mi_row, mi_col);
5837*77c1e3ccSAndroid Build Coastguard Worker         ++cpi->sb_counter;
5838*77c1e3ccSAndroid Build Coastguard Worker       }
5839*77c1e3ccSAndroid Build Coastguard Worker 
5840*77c1e3ccSAndroid Build Coastguard Worker       set_cb_offsets(x->cb_offset, 0, 0);
5841*77c1e3ccSAndroid Build Coastguard Worker       encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, run_type, bsize,
5842*77c1e3ccSAndroid Build Coastguard Worker                 pc_tree, NULL);
5843*77c1e3ccSAndroid Build Coastguard Worker       assert(pc_tree == td->pc_root);
5844*77c1e3ccSAndroid Build Coastguard Worker       // Dealloc the whole PC_TREE after a superblock is done.
5845*77c1e3ccSAndroid Build Coastguard Worker       av1_free_pc_tree_recursive(pc_tree, num_planes, 0, 0,
5846*77c1e3ccSAndroid Build Coastguard Worker                                  cpi->sf.part_sf.partition_search_type);
5847*77c1e3ccSAndroid Build Coastguard Worker       pc_tree = NULL;
5848*77c1e3ccSAndroid Build Coastguard Worker       td->pc_root = NULL;
5849*77c1e3ccSAndroid Build Coastguard Worker       pc_tree_dealloc = 1;
5850*77c1e3ccSAndroid Build Coastguard Worker     } else if (should_do_dry_run_encode_for_current_block(
5851*77c1e3ccSAndroid Build Coastguard Worker                    cm->seq_params->sb_size, x->sb_enc.max_partition_size,
5852*77c1e3ccSAndroid Build Coastguard Worker                    pc_tree->index, bsize)) {
5853*77c1e3ccSAndroid Build Coastguard Worker       // Encode the smaller blocks in DRY_RUN mode.
5854*77c1e3ccSAndroid Build Coastguard Worker       encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, DRY_RUN_NORMAL, bsize,
5855*77c1e3ccSAndroid Build Coastguard Worker                 pc_tree, NULL);
5856*77c1e3ccSAndroid Build Coastguard Worker     }
5857*77c1e3ccSAndroid Build Coastguard Worker   }
5858*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
5859*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, encode_sb_time);
5860*77c1e3ccSAndroid Build Coastguard Worker #endif
5861*77c1e3ccSAndroid Build Coastguard Worker 
5862*77c1e3ccSAndroid Build Coastguard Worker   // If the tree still exists (non-superblock), dealloc most nodes, only keep
5863*77c1e3ccSAndroid Build Coastguard Worker   // nodes for the best partition and PARTITION_NONE.
5864*77c1e3ccSAndroid Build Coastguard Worker   if (pc_tree_dealloc == 0)
5865*77c1e3ccSAndroid Build Coastguard Worker     av1_free_pc_tree_recursive(pc_tree, num_planes, 1, 1,
5866*77c1e3ccSAndroid Build Coastguard Worker                                cpi->sf.part_sf.partition_search_type);
5867*77c1e3ccSAndroid Build Coastguard Worker 
5868*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == cm->seq_params->sb_size) {
5869*77c1e3ccSAndroid Build Coastguard Worker     assert(best_rdc.rate < INT_MAX);
5870*77c1e3ccSAndroid Build Coastguard Worker     assert(best_rdc.dist < INT64_MAX);
5871*77c1e3ccSAndroid Build Coastguard Worker   } else {
5872*77c1e3ccSAndroid Build Coastguard Worker     assert(tp_orig == *tp);
5873*77c1e3ccSAndroid Build Coastguard Worker   }
5874*77c1e3ccSAndroid Build Coastguard Worker 
5875*77c1e3ccSAndroid Build Coastguard Worker   // Restore the rd multiplier.
5876*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult = orig_rdmult;
5877*77c1e3ccSAndroid Build Coastguard Worker   return part_search_state.found_best_partition;
5878*77c1e3ccSAndroid Build Coastguard Worker }
5879*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
5880*77c1e3ccSAndroid Build Coastguard Worker 
5881*77c1e3ccSAndroid Build Coastguard Worker #undef COLLECT_MOTION_SEARCH_FEATURE_SB
5882*77c1e3ccSAndroid Build Coastguard Worker 
5883*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RT_ML_PARTITIONING
5884*77c1e3ccSAndroid Build Coastguard Worker #define FEATURES 6
5885*77c1e3ccSAndroid Build Coastguard Worker #define LABELS 2
ml_predict_var_partitioning(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int mi_row,int mi_col)5886*77c1e3ccSAndroid Build Coastguard Worker static int ml_predict_var_partitioning(AV1_COMP *cpi, MACROBLOCK *x,
5887*77c1e3ccSAndroid Build Coastguard Worker                                        BLOCK_SIZE bsize, int mi_row,
5888*77c1e3ccSAndroid Build Coastguard Worker                                        int mi_col) {
5889*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
5890*77c1e3ccSAndroid Build Coastguard Worker   const NN_CONFIG *nn_config = NULL;
5891*77c1e3ccSAndroid Build Coastguard Worker   const float *means = NULL;
5892*77c1e3ccSAndroid Build Coastguard Worker   const float *vars = NULL;
5893*77c1e3ccSAndroid Build Coastguard Worker   switch (bsize) {
5894*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_64X64:
5895*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_var_part_nnconfig_64;
5896*77c1e3ccSAndroid Build Coastguard Worker       means = av1_var_part_means_64;
5897*77c1e3ccSAndroid Build Coastguard Worker       vars = av1_var_part_vars_64;
5898*77c1e3ccSAndroid Build Coastguard Worker       break;
5899*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_32X32:
5900*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_var_part_nnconfig_32;
5901*77c1e3ccSAndroid Build Coastguard Worker       means = av1_var_part_means_32;
5902*77c1e3ccSAndroid Build Coastguard Worker       vars = av1_var_part_vars_32;
5903*77c1e3ccSAndroid Build Coastguard Worker       break;
5904*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_16X16:
5905*77c1e3ccSAndroid Build Coastguard Worker       nn_config = &av1_var_part_nnconfig_16;
5906*77c1e3ccSAndroid Build Coastguard Worker       means = av1_var_part_means_16;
5907*77c1e3ccSAndroid Build Coastguard Worker       vars = av1_var_part_vars_16;
5908*77c1e3ccSAndroid Build Coastguard Worker       break;
5909*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_8X8:
5910*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0 && "Unexpected block size."); return -1;
5911*77c1e3ccSAndroid Build Coastguard Worker   }
5912*77c1e3ccSAndroid Build Coastguard Worker 
5913*77c1e3ccSAndroid Build Coastguard Worker   if (!nn_config) return -1;
5914*77c1e3ccSAndroid Build Coastguard Worker 
5915*77c1e3ccSAndroid Build Coastguard Worker   {
5916*77c1e3ccSAndroid Build Coastguard Worker     const float thresh = cpi->oxcf.speed <= 5 ? 1.25f : 0.0f;
5917*77c1e3ccSAndroid Build Coastguard Worker     float features[FEATURES] = { 0.0f };
5918*77c1e3ccSAndroid Build Coastguard Worker     const int dc_q = av1_dc_quant_QTX(cm->quant_params.base_qindex, 0,
5919*77c1e3ccSAndroid Build Coastguard Worker                                       cm->seq_params->bit_depth);
5920*77c1e3ccSAndroid Build Coastguard Worker     int feature_idx = 0;
5921*77c1e3ccSAndroid Build Coastguard Worker     float score[LABELS];
5922*77c1e3ccSAndroid Build Coastguard Worker 
5923*77c1e3ccSAndroid Build Coastguard Worker     features[feature_idx] =
5924*77c1e3ccSAndroid Build Coastguard Worker         (log1pf((float)(dc_q * dc_q) / 256.0f) - means[feature_idx]) /
5925*77c1e3ccSAndroid Build Coastguard Worker         sqrtf(vars[feature_idx]);
5926*77c1e3ccSAndroid Build Coastguard Worker     feature_idx++;
5927*77c1e3ccSAndroid Build Coastguard Worker     av1_setup_src_planes(x, cpi->source, mi_row, mi_col, 1, bsize);
5928*77c1e3ccSAndroid Build Coastguard Worker     {
5929*77c1e3ccSAndroid Build Coastguard Worker       const int bs = block_size_wide[bsize];
5930*77c1e3ccSAndroid Build Coastguard Worker       const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
5931*77c1e3ccSAndroid Build Coastguard Worker       const int sb_offset_row = 4 * (mi_row & 15);
5932*77c1e3ccSAndroid Build Coastguard Worker       const int sb_offset_col = 4 * (mi_col & 15);
5933*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t *pred = x->est_pred + sb_offset_row * 64 + sb_offset_col;
5934*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t *src = x->plane[0].src.buf;
5935*77c1e3ccSAndroid Build Coastguard Worker       const int src_stride = x->plane[0].src.stride;
5936*77c1e3ccSAndroid Build Coastguard Worker       const int pred_stride = 64;
5937*77c1e3ccSAndroid Build Coastguard Worker       unsigned int sse;
5938*77c1e3ccSAndroid Build Coastguard Worker       int i;
5939*77c1e3ccSAndroid Build Coastguard Worker       // Variance of whole block.
5940*77c1e3ccSAndroid Build Coastguard Worker       const unsigned int var =
5941*77c1e3ccSAndroid Build Coastguard Worker           cpi->ppi->fn_ptr[bsize].vf(src, src_stride, pred, pred_stride, &sse);
5942*77c1e3ccSAndroid Build Coastguard Worker       const float factor = (var == 0) ? 1.0f : (1.0f / (float)var);
5943*77c1e3ccSAndroid Build Coastguard Worker 
5944*77c1e3ccSAndroid Build Coastguard Worker       features[feature_idx] =
5945*77c1e3ccSAndroid Build Coastguard Worker           (log1pf((float)var) - means[feature_idx]) / sqrtf(vars[feature_idx]);
5946*77c1e3ccSAndroid Build Coastguard Worker       feature_idx++;
5947*77c1e3ccSAndroid Build Coastguard Worker       for (i = 0; i < 4; ++i) {
5948*77c1e3ccSAndroid Build Coastguard Worker         const int x_idx = (i & 1) * bs / 2;
5949*77c1e3ccSAndroid Build Coastguard Worker         const int y_idx = (i >> 1) * bs / 2;
5950*77c1e3ccSAndroid Build Coastguard Worker         const int src_offset = y_idx * src_stride + x_idx;
5951*77c1e3ccSAndroid Build Coastguard Worker         const int pred_offset = y_idx * pred_stride + x_idx;
5952*77c1e3ccSAndroid Build Coastguard Worker         // Variance of quarter block.
5953*77c1e3ccSAndroid Build Coastguard Worker         const unsigned int sub_var =
5954*77c1e3ccSAndroid Build Coastguard Worker             cpi->ppi->fn_ptr[subsize].vf(src + src_offset, src_stride,
5955*77c1e3ccSAndroid Build Coastguard Worker                                          pred + pred_offset, pred_stride, &sse);
5956*77c1e3ccSAndroid Build Coastguard Worker         const float var_ratio = (var == 0) ? 1.0f : factor * (float)sub_var;
5957*77c1e3ccSAndroid Build Coastguard Worker         features[feature_idx] =
5958*77c1e3ccSAndroid Build Coastguard Worker             (var_ratio - means[feature_idx]) / sqrtf(vars[feature_idx]);
5959*77c1e3ccSAndroid Build Coastguard Worker         feature_idx++;
5960*77c1e3ccSAndroid Build Coastguard Worker       }
5961*77c1e3ccSAndroid Build Coastguard Worker     }
5962*77c1e3ccSAndroid Build Coastguard Worker     //    for (int i = 0; i<FEATURES; i++)
5963*77c1e3ccSAndroid Build Coastguard Worker     //      printf("F_%d, %f; ", i, features[i]);
5964*77c1e3ccSAndroid Build Coastguard Worker     assert(feature_idx == FEATURES);
5965*77c1e3ccSAndroid Build Coastguard Worker     av1_nn_predict(features, nn_config, 1, score);
5966*77c1e3ccSAndroid Build Coastguard Worker     //    printf("Score %f, thr %f ", (float)score[0], thresh);
5967*77c1e3ccSAndroid Build Coastguard Worker     if (score[0] > thresh) return PARTITION_SPLIT;
5968*77c1e3ccSAndroid Build Coastguard Worker     if (score[0] < -thresh) return PARTITION_NONE;
5969*77c1e3ccSAndroid Build Coastguard Worker     return -1;
5970*77c1e3ccSAndroid Build Coastguard Worker   }
5971*77c1e3ccSAndroid Build Coastguard Worker }
5972*77c1e3ccSAndroid Build Coastguard Worker #undef FEATURES
5973*77c1e3ccSAndroid Build Coastguard Worker #undef LABELS
5974*77c1e3ccSAndroid Build Coastguard Worker 
5975*77c1e3ccSAndroid Build Coastguard Worker // Uncomment for collecting data for ML-based partitioning
5976*77c1e3ccSAndroid Build Coastguard Worker // #define _COLLECT_GROUND_TRUTH_
5977*77c1e3ccSAndroid Build Coastguard Worker 
5978*77c1e3ccSAndroid Build Coastguard Worker #ifdef _COLLECT_GROUND_TRUTH_
store_partition_data(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int mi_row,int mi_col,PARTITION_TYPE part)5979*77c1e3ccSAndroid Build Coastguard Worker static int store_partition_data(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
5980*77c1e3ccSAndroid Build Coastguard Worker                                 int mi_row, int mi_col, PARTITION_TYPE part) {
5981*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
5982*77c1e3ccSAndroid Build Coastguard Worker   char fname[128];
5983*77c1e3ccSAndroid Build Coastguard Worker   switch (bsize) {
5984*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_64X64: sprintf(fname, "data_64x64.txt"); break;
5985*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_32X32: sprintf(fname, "data_32x32.txt"); break;
5986*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_16X16: sprintf(fname, "data_16x16.txt"); break;
5987*77c1e3ccSAndroid Build Coastguard Worker     case BLOCK_8X8: sprintf(fname, "data_8x8.txt"); break;
5988*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0 && "Unexpected block size."); return -1;
5989*77c1e3ccSAndroid Build Coastguard Worker   }
5990*77c1e3ccSAndroid Build Coastguard Worker 
5991*77c1e3ccSAndroid Build Coastguard Worker   float features[6];  // DC_Q, VAR, VAR_RATIO-0..3
5992*77c1e3ccSAndroid Build Coastguard Worker 
5993*77c1e3ccSAndroid Build Coastguard Worker   FILE *f = fopen(fname, "a");
5994*77c1e3ccSAndroid Build Coastguard Worker 
5995*77c1e3ccSAndroid Build Coastguard Worker   {
5996*77c1e3ccSAndroid Build Coastguard Worker     const int dc_q = av1_dc_quant_QTX(cm->quant_params.base_qindex, 0,
5997*77c1e3ccSAndroid Build Coastguard Worker                                       cm->seq_params->bit_depth);
5998*77c1e3ccSAndroid Build Coastguard Worker     int feature_idx = 0;
5999*77c1e3ccSAndroid Build Coastguard Worker 
6000*77c1e3ccSAndroid Build Coastguard Worker     features[feature_idx++] = log1pf((float)(dc_q * dc_q) / 256.0f);
6001*77c1e3ccSAndroid Build Coastguard Worker     av1_setup_src_planes(x, cpi->source, mi_row, mi_col, 1, bsize);
6002*77c1e3ccSAndroid Build Coastguard Worker     {
6003*77c1e3ccSAndroid Build Coastguard Worker       const int bs = block_size_wide[bsize];
6004*77c1e3ccSAndroid Build Coastguard Worker       const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
6005*77c1e3ccSAndroid Build Coastguard Worker       const int sb_offset_row = 4 * (mi_row & 15);
6006*77c1e3ccSAndroid Build Coastguard Worker       const int sb_offset_col = 4 * (mi_col & 15);
6007*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t *pred = x->est_pred + sb_offset_row * 64 + sb_offset_col;
6008*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t *src = x->plane[0].src.buf;
6009*77c1e3ccSAndroid Build Coastguard Worker       const int src_stride = x->plane[0].src.stride;
6010*77c1e3ccSAndroid Build Coastguard Worker       const int pred_stride = 64;
6011*77c1e3ccSAndroid Build Coastguard Worker       unsigned int sse;
6012*77c1e3ccSAndroid Build Coastguard Worker       int i;
6013*77c1e3ccSAndroid Build Coastguard Worker       // Variance of whole block.
6014*77c1e3ccSAndroid Build Coastguard Worker       /*
6015*77c1e3ccSAndroid Build Coastguard Worker                 if (bs == 8)
6016*77c1e3ccSAndroid Build Coastguard Worker                 {
6017*77c1e3ccSAndroid Build Coastguard Worker                   int r, c;
6018*77c1e3ccSAndroid Build Coastguard Worker                   printf("%d %d\n", mi_row, mi_col);
6019*77c1e3ccSAndroid Build Coastguard Worker                   for (r = 0; r < bs; ++r) {
6020*77c1e3ccSAndroid Build Coastguard Worker                     for (c = 0; c < bs; ++c) {
6021*77c1e3ccSAndroid Build Coastguard Worker                       printf("%3d ",
6022*77c1e3ccSAndroid Build Coastguard Worker                              src[r * src_stride + c] - pred[64 * r + c]);
6023*77c1e3ccSAndroid Build Coastguard Worker                     }
6024*77c1e3ccSAndroid Build Coastguard Worker                     printf("\n");
6025*77c1e3ccSAndroid Build Coastguard Worker                   }
6026*77c1e3ccSAndroid Build Coastguard Worker                   printf("\n");
6027*77c1e3ccSAndroid Build Coastguard Worker                 }
6028*77c1e3ccSAndroid Build Coastguard Worker       */
6029*77c1e3ccSAndroid Build Coastguard Worker       const unsigned int var =
6030*77c1e3ccSAndroid Build Coastguard Worker           cpi->fn_ptr[bsize].vf(src, src_stride, pred, pred_stride, &sse);
6031*77c1e3ccSAndroid Build Coastguard Worker       const float factor = (var == 0) ? 1.0f : (1.0f / (float)var);
6032*77c1e3ccSAndroid Build Coastguard Worker 
6033*77c1e3ccSAndroid Build Coastguard Worker       features[feature_idx++] = log1pf((float)var);
6034*77c1e3ccSAndroid Build Coastguard Worker 
6035*77c1e3ccSAndroid Build Coastguard Worker       fprintf(f, "%f,%f,", features[0], features[1]);
6036*77c1e3ccSAndroid Build Coastguard Worker       for (i = 0; i < 4; ++i) {
6037*77c1e3ccSAndroid Build Coastguard Worker         const int x_idx = (i & 1) * bs / 2;
6038*77c1e3ccSAndroid Build Coastguard Worker         const int y_idx = (i >> 1) * bs / 2;
6039*77c1e3ccSAndroid Build Coastguard Worker         const int src_offset = y_idx * src_stride + x_idx;
6040*77c1e3ccSAndroid Build Coastguard Worker         const int pred_offset = y_idx * pred_stride + x_idx;
6041*77c1e3ccSAndroid Build Coastguard Worker         // Variance of quarter block.
6042*77c1e3ccSAndroid Build Coastguard Worker         const unsigned int sub_var =
6043*77c1e3ccSAndroid Build Coastguard Worker             cpi->fn_ptr[subsize].vf(src + src_offset, src_stride,
6044*77c1e3ccSAndroid Build Coastguard Worker                                     pred + pred_offset, pred_stride, &sse);
6045*77c1e3ccSAndroid Build Coastguard Worker         const float var_ratio = (var == 0) ? 1.0f : factor * (float)sub_var;
6046*77c1e3ccSAndroid Build Coastguard Worker         features[feature_idx++] = var_ratio;
6047*77c1e3ccSAndroid Build Coastguard Worker         fprintf(f, "%f,", var_ratio);
6048*77c1e3ccSAndroid Build Coastguard Worker       }
6049*77c1e3ccSAndroid Build Coastguard Worker 
6050*77c1e3ccSAndroid Build Coastguard Worker       fprintf(f, "%d\n", part == PARTITION_NONE ? 0 : 1);
6051*77c1e3ccSAndroid Build Coastguard Worker     }
6052*77c1e3ccSAndroid Build Coastguard Worker 
6053*77c1e3ccSAndroid Build Coastguard Worker     fclose(f);
6054*77c1e3ccSAndroid Build Coastguard Worker     return -1;
6055*77c1e3ccSAndroid Build Coastguard Worker   }
6056*77c1e3ccSAndroid Build Coastguard Worker }
6057*77c1e3ccSAndroid Build Coastguard Worker #endif
6058*77c1e3ccSAndroid Build Coastguard Worker 
duplicate_mode_info_in_sb(AV1_COMMON * cm,MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize)6059*77c1e3ccSAndroid Build Coastguard Worker static void duplicate_mode_info_in_sb(AV1_COMMON *cm, MACROBLOCKD *xd,
6060*77c1e3ccSAndroid Build Coastguard Worker                                       int mi_row, int mi_col,
6061*77c1e3ccSAndroid Build Coastguard Worker                                       BLOCK_SIZE bsize) {
6062*77c1e3ccSAndroid Build Coastguard Worker   const int block_width =
6063*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(mi_size_wide[bsize], cm->mi_params.mi_cols - mi_col);
6064*77c1e3ccSAndroid Build Coastguard Worker   const int block_height =
6065*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(mi_size_high[bsize], cm->mi_params.mi_rows - mi_row);
6066*77c1e3ccSAndroid Build Coastguard Worker   const int mi_stride = xd->mi_stride;
6067*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *const src_mi = xd->mi[0];
6068*77c1e3ccSAndroid Build Coastguard Worker   int i, j;
6069*77c1e3ccSAndroid Build Coastguard Worker 
6070*77c1e3ccSAndroid Build Coastguard Worker   for (j = 0; j < block_height; ++j)
6071*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < block_width; ++i) xd->mi[j * mi_stride + i] = src_mi;
6072*77c1e3ccSAndroid Build Coastguard Worker }
6073*77c1e3ccSAndroid Build Coastguard Worker 
copy_mbmi_ext_frame_to_mbmi_ext(MB_MODE_INFO_EXT * const mbmi_ext,const MB_MODE_INFO_EXT_FRAME * mbmi_ext_best,uint8_t ref_frame_type)6074*77c1e3ccSAndroid Build Coastguard Worker static inline void copy_mbmi_ext_frame_to_mbmi_ext(
6075*77c1e3ccSAndroid Build Coastguard Worker     MB_MODE_INFO_EXT *const mbmi_ext,
6076*77c1e3ccSAndroid Build Coastguard Worker     const MB_MODE_INFO_EXT_FRAME *mbmi_ext_best, uint8_t ref_frame_type) {
6077*77c1e3ccSAndroid Build Coastguard Worker   memcpy(mbmi_ext->ref_mv_stack[ref_frame_type], mbmi_ext_best->ref_mv_stack,
6078*77c1e3ccSAndroid Build Coastguard Worker          sizeof(mbmi_ext->ref_mv_stack[USABLE_REF_MV_STACK_SIZE]));
6079*77c1e3ccSAndroid Build Coastguard Worker   memcpy(mbmi_ext->weight[ref_frame_type], mbmi_ext_best->weight,
6080*77c1e3ccSAndroid Build Coastguard Worker          sizeof(mbmi_ext->weight[USABLE_REF_MV_STACK_SIZE]));
6081*77c1e3ccSAndroid Build Coastguard Worker   mbmi_ext->mode_context[ref_frame_type] = mbmi_ext_best->mode_context;
6082*77c1e3ccSAndroid Build Coastguard Worker   mbmi_ext->ref_mv_count[ref_frame_type] = mbmi_ext_best->ref_mv_count;
6083*77c1e3ccSAndroid Build Coastguard Worker   memcpy(mbmi_ext->global_mvs, mbmi_ext_best->global_mvs,
6084*77c1e3ccSAndroid Build Coastguard Worker          sizeof(mbmi_ext->global_mvs));
6085*77c1e3ccSAndroid Build Coastguard Worker }
6086*77c1e3ccSAndroid Build Coastguard Worker 
fill_mode_info_sb(AV1_COMP * cpi,MACROBLOCK * x,int mi_row,int mi_col,BLOCK_SIZE bsize,PC_TREE * pc_tree)6087*77c1e3ccSAndroid Build Coastguard Worker static void fill_mode_info_sb(AV1_COMP *cpi, MACROBLOCK *x, int mi_row,
6088*77c1e3ccSAndroid Build Coastguard Worker                               int mi_col, BLOCK_SIZE bsize, PC_TREE *pc_tree) {
6089*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
6090*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *xd = &x->e_mbd;
6091*77c1e3ccSAndroid Build Coastguard Worker   int hbs = mi_size_wide[bsize] >> 1;
6092*77c1e3ccSAndroid Build Coastguard Worker   PARTITION_TYPE partition = pc_tree->partitioning;
6093*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE subsize = get_partition_subsize(bsize, partition);
6094*77c1e3ccSAndroid Build Coastguard Worker 
6095*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize >= BLOCK_8X8);
6096*77c1e3ccSAndroid Build Coastguard Worker 
6097*77c1e3ccSAndroid Build Coastguard Worker   if (mi_row >= cm->mi_params.mi_rows || mi_col >= cm->mi_params.mi_cols)
6098*77c1e3ccSAndroid Build Coastguard Worker     return;
6099*77c1e3ccSAndroid Build Coastguard Worker 
6100*77c1e3ccSAndroid Build Coastguard Worker   switch (partition) {
6101*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_NONE:
6102*77c1e3ccSAndroid Build Coastguard Worker       set_mode_info_offsets(&cm->mi_params, &cpi->mbmi_ext_info, x, xd, mi_row,
6103*77c1e3ccSAndroid Build Coastguard Worker                             mi_col);
6104*77c1e3ccSAndroid Build Coastguard Worker       *(xd->mi[0]) = pc_tree->none->mic;
6105*77c1e3ccSAndroid Build Coastguard Worker       copy_mbmi_ext_frame_to_mbmi_ext(
6106*77c1e3ccSAndroid Build Coastguard Worker           &x->mbmi_ext, &pc_tree->none->mbmi_ext_best, LAST_FRAME);
6107*77c1e3ccSAndroid Build Coastguard Worker       duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
6108*77c1e3ccSAndroid Build Coastguard Worker       break;
6109*77c1e3ccSAndroid Build Coastguard Worker     case PARTITION_SPLIT: {
6110*77c1e3ccSAndroid Build Coastguard Worker       fill_mode_info_sb(cpi, x, mi_row, mi_col, subsize, pc_tree->split[0]);
6111*77c1e3ccSAndroid Build Coastguard Worker       fill_mode_info_sb(cpi, x, mi_row, mi_col + hbs, subsize,
6112*77c1e3ccSAndroid Build Coastguard Worker                         pc_tree->split[1]);
6113*77c1e3ccSAndroid Build Coastguard Worker       fill_mode_info_sb(cpi, x, mi_row + hbs, mi_col, subsize,
6114*77c1e3ccSAndroid Build Coastguard Worker                         pc_tree->split[2]);
6115*77c1e3ccSAndroid Build Coastguard Worker       fill_mode_info_sb(cpi, x, mi_row + hbs, mi_col + hbs, subsize,
6116*77c1e3ccSAndroid Build Coastguard Worker                         pc_tree->split[3]);
6117*77c1e3ccSAndroid Build Coastguard Worker       break;
6118*77c1e3ccSAndroid Build Coastguard Worker     }
6119*77c1e3ccSAndroid Build Coastguard Worker     default: break;
6120*77c1e3ccSAndroid Build Coastguard Worker   }
6121*77c1e3ccSAndroid Build Coastguard Worker }
6122*77c1e3ccSAndroid Build Coastguard Worker 
av1_nonrd_pick_partition(AV1_COMP * cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,int mi_row,int mi_col,BLOCK_SIZE bsize,RD_STATS * rd_cost,int do_recon,int64_t best_rd,PC_TREE * pc_tree)6123*77c1e3ccSAndroid Build Coastguard Worker void av1_nonrd_pick_partition(AV1_COMP *cpi, ThreadData *td,
6124*77c1e3ccSAndroid Build Coastguard Worker                               TileDataEnc *tile_data, TokenExtra **tp,
6125*77c1e3ccSAndroid Build Coastguard Worker                               int mi_row, int mi_col, BLOCK_SIZE bsize,
6126*77c1e3ccSAndroid Build Coastguard Worker                               RD_STATS *rd_cost, int do_recon, int64_t best_rd,
6127*77c1e3ccSAndroid Build Coastguard Worker                               PC_TREE *pc_tree) {
6128*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
6129*77c1e3ccSAndroid Build Coastguard Worker   TileInfo *const tile_info = &tile_data->tile_info;
6130*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
6131*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
6132*77c1e3ccSAndroid Build Coastguard Worker   const int hbs = mi_size_wide[bsize] >> 1;
6133*77c1e3ccSAndroid Build Coastguard Worker   TokenExtra *tp_orig = *tp;
6134*77c1e3ccSAndroid Build Coastguard Worker   const ModeCosts *mode_costs = &x->mode_costs;
6135*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS this_rdc, best_rdc;
6136*77c1e3ccSAndroid Build Coastguard Worker   RD_SEARCH_MACROBLOCK_CONTEXT x_ctx;
6137*77c1e3ccSAndroid Build Coastguard Worker   int do_split = bsize > BLOCK_8X8;
6138*77c1e3ccSAndroid Build Coastguard Worker   // Override skipping rectangular partition operations for edge blocks
6139*77c1e3ccSAndroid Build Coastguard Worker   const int force_horz_split = (mi_row + 2 * hbs > cm->mi_params.mi_rows);
6140*77c1e3ccSAndroid Build Coastguard Worker   const int force_vert_split = (mi_col + 2 * hbs > cm->mi_params.mi_cols);
6141*77c1e3ccSAndroid Build Coastguard Worker 
6142*77c1e3ccSAndroid Build Coastguard Worker   int partition_none_allowed = !force_horz_split && !force_vert_split;
6143*77c1e3ccSAndroid Build Coastguard Worker 
6144*77c1e3ccSAndroid Build Coastguard Worker   assert(mi_size_wide[bsize] == mi_size_high[bsize]);  // Square partition only
6145*77c1e3ccSAndroid Build Coastguard Worker   assert(cm->seq_params->sb_size == BLOCK_64X64);      // Small SB so far
6146*77c1e3ccSAndroid Build Coastguard Worker 
6147*77c1e3ccSAndroid Build Coastguard Worker   (void)*tp_orig;
6148*77c1e3ccSAndroid Build Coastguard Worker 
6149*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&best_rdc);
6150*77c1e3ccSAndroid Build Coastguard Worker   best_rdc.rdcost = best_rd;
6151*77c1e3ccSAndroid Build Coastguard Worker #ifndef _COLLECT_GROUND_TRUTH_
6152*77c1e3ccSAndroid Build Coastguard Worker   if (partition_none_allowed && do_split) {
6153*77c1e3ccSAndroid Build Coastguard Worker     const int ml_predicted_partition =
6154*77c1e3ccSAndroid Build Coastguard Worker         ml_predict_var_partitioning(cpi, x, bsize, mi_row, mi_col);
6155*77c1e3ccSAndroid Build Coastguard Worker     if (ml_predicted_partition == PARTITION_NONE) do_split = 0;
6156*77c1e3ccSAndroid Build Coastguard Worker     if (ml_predicted_partition == PARTITION_SPLIT) partition_none_allowed = 0;
6157*77c1e3ccSAndroid Build Coastguard Worker   }
6158*77c1e3ccSAndroid Build Coastguard Worker #endif
6159*77c1e3ccSAndroid Build Coastguard Worker 
6160*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context =
6161*77c1e3ccSAndroid Build Coastguard Worker       cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
6162*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
6163*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
6164*77c1e3ccSAndroid Build Coastguard Worker   av1_save_context(x, &x_ctx, mi_row, mi_col, bsize, 3);
6165*77c1e3ccSAndroid Build Coastguard Worker 
6166*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_NONE
6167*77c1e3ccSAndroid Build Coastguard Worker   if (partition_none_allowed) {
6168*77c1e3ccSAndroid Build Coastguard Worker     pc_tree->none = av1_alloc_pmc(cpi, bsize, &td->shared_coeff_buf);
6169*77c1e3ccSAndroid Build Coastguard Worker     if (!pc_tree->none)
6170*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
6171*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PICK_MODE_CONTEXT");
6172*77c1e3ccSAndroid Build Coastguard Worker     PICK_MODE_CONTEXT *ctx = pc_tree->none;
6173*77c1e3ccSAndroid Build Coastguard Worker 
6174*77c1e3ccSAndroid Build Coastguard Worker // Flip for RDO based pick mode
6175*77c1e3ccSAndroid Build Coastguard Worker #if 0
6176*77c1e3ccSAndroid Build Coastguard Worker     RD_STATS dummy;
6177*77c1e3ccSAndroid Build Coastguard Worker     av1_invalid_rd_stats(&dummy);
6178*77c1e3ccSAndroid Build Coastguard Worker     pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &this_rdc,
6179*77c1e3ccSAndroid Build Coastguard Worker                   PARTITION_NONE, bsize, ctx, dummy);
6180*77c1e3ccSAndroid Build Coastguard Worker #else
6181*77c1e3ccSAndroid Build Coastguard Worker     pick_sb_modes_nonrd(cpi, tile_data, x, mi_row, mi_col, &this_rdc, bsize,
6182*77c1e3ccSAndroid Build Coastguard Worker                         ctx);
6183*77c1e3ccSAndroid Build Coastguard Worker #endif
6184*77c1e3ccSAndroid Build Coastguard Worker     if (this_rdc.rate != INT_MAX) {
6185*77c1e3ccSAndroid Build Coastguard Worker       const int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
6186*77c1e3ccSAndroid Build Coastguard Worker 
6187*77c1e3ccSAndroid Build Coastguard Worker       this_rdc.rate += mode_costs->partition_cost[pl][PARTITION_NONE];
6188*77c1e3ccSAndroid Build Coastguard Worker       this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
6189*77c1e3ccSAndroid Build Coastguard Worker       if (this_rdc.rdcost < best_rdc.rdcost) {
6190*77c1e3ccSAndroid Build Coastguard Worker         best_rdc = this_rdc;
6191*77c1e3ccSAndroid Build Coastguard Worker         if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
6192*77c1e3ccSAndroid Build Coastguard Worker       }
6193*77c1e3ccSAndroid Build Coastguard Worker     }
6194*77c1e3ccSAndroid Build Coastguard Worker   }
6195*77c1e3ccSAndroid Build Coastguard Worker 
6196*77c1e3ccSAndroid Build Coastguard Worker   // PARTITION_SPLIT
6197*77c1e3ccSAndroid Build Coastguard Worker   if (do_split) {
6198*77c1e3ccSAndroid Build Coastguard Worker     RD_STATS sum_rdc;
6199*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
6200*77c1e3ccSAndroid Build Coastguard Worker 
6201*77c1e3ccSAndroid Build Coastguard Worker     av1_init_rd_stats(&sum_rdc);
6202*77c1e3ccSAndroid Build Coastguard Worker 
6203*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
6204*77c1e3ccSAndroid Build Coastguard Worker       pc_tree->split[i] = av1_alloc_pc_tree_node(subsize);
6205*77c1e3ccSAndroid Build Coastguard Worker       if (!pc_tree->split[i])
6206*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
6207*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to allocate PC_TREE");
6208*77c1e3ccSAndroid Build Coastguard Worker       pc_tree->split[i]->index = i;
6209*77c1e3ccSAndroid Build Coastguard Worker     }
6210*77c1e3ccSAndroid Build Coastguard Worker 
6211*77c1e3ccSAndroid Build Coastguard Worker     int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
6212*77c1e3ccSAndroid Build Coastguard Worker     sum_rdc.rate += mode_costs->partition_cost[pl][PARTITION_SPLIT];
6213*77c1e3ccSAndroid Build Coastguard Worker     sum_rdc.rdcost = RDCOST(x->rdmult, sum_rdc.rate, sum_rdc.dist);
6214*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0;
6215*77c1e3ccSAndroid Build Coastguard Worker          i < SUB_PARTITIONS_SPLIT && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
6216*77c1e3ccSAndroid Build Coastguard Worker       const int x_idx = (i & 1) * hbs;
6217*77c1e3ccSAndroid Build Coastguard Worker       const int y_idx = (i >> 1) * hbs;
6218*77c1e3ccSAndroid Build Coastguard Worker 
6219*77c1e3ccSAndroid Build Coastguard Worker       if (mi_row + y_idx >= cm->mi_params.mi_rows ||
6220*77c1e3ccSAndroid Build Coastguard Worker           mi_col + x_idx >= cm->mi_params.mi_cols)
6221*77c1e3ccSAndroid Build Coastguard Worker         continue;
6222*77c1e3ccSAndroid Build Coastguard Worker       av1_nonrd_pick_partition(cpi, td, tile_data, tp, mi_row + y_idx,
6223*77c1e3ccSAndroid Build Coastguard Worker                                mi_col + x_idx, subsize, &this_rdc, i < 3,
6224*77c1e3ccSAndroid Build Coastguard Worker                                best_rdc.rdcost - sum_rdc.rdcost,
6225*77c1e3ccSAndroid Build Coastguard Worker                                pc_tree->split[i]);
6226*77c1e3ccSAndroid Build Coastguard Worker 
6227*77c1e3ccSAndroid Build Coastguard Worker       if (this_rdc.rate == INT_MAX) {
6228*77c1e3ccSAndroid Build Coastguard Worker         av1_invalid_rd_stats(&sum_rdc);
6229*77c1e3ccSAndroid Build Coastguard Worker       } else {
6230*77c1e3ccSAndroid Build Coastguard Worker         sum_rdc.rate += this_rdc.rate;
6231*77c1e3ccSAndroid Build Coastguard Worker         sum_rdc.dist += this_rdc.dist;
6232*77c1e3ccSAndroid Build Coastguard Worker         sum_rdc.rdcost += this_rdc.rdcost;
6233*77c1e3ccSAndroid Build Coastguard Worker       }
6234*77c1e3ccSAndroid Build Coastguard Worker     }
6235*77c1e3ccSAndroid Build Coastguard Worker     if (sum_rdc.rdcost < best_rdc.rdcost) {
6236*77c1e3ccSAndroid Build Coastguard Worker       best_rdc = sum_rdc;
6237*77c1e3ccSAndroid Build Coastguard Worker       pc_tree->partitioning = PARTITION_SPLIT;
6238*77c1e3ccSAndroid Build Coastguard Worker     }
6239*77c1e3ccSAndroid Build Coastguard Worker   }
6240*77c1e3ccSAndroid Build Coastguard Worker 
6241*77c1e3ccSAndroid Build Coastguard Worker #ifdef _COLLECT_GROUND_TRUTH_
6242*77c1e3ccSAndroid Build Coastguard Worker   store_partition_data(cpi, x, bsize, mi_row, mi_col, pc_tree->partitioning);
6243*77c1e3ccSAndroid Build Coastguard Worker #endif
6244*77c1e3ccSAndroid Build Coastguard Worker 
6245*77c1e3ccSAndroid Build Coastguard Worker   *rd_cost = best_rdc;
6246*77c1e3ccSAndroid Build Coastguard Worker 
6247*77c1e3ccSAndroid Build Coastguard Worker   av1_restore_context(x, &x_ctx, mi_row, mi_col, bsize, 3);
6248*77c1e3ccSAndroid Build Coastguard Worker 
6249*77c1e3ccSAndroid Build Coastguard Worker   if (best_rdc.rate == INT_MAX) {
6250*77c1e3ccSAndroid Build Coastguard Worker     av1_invalid_rd_stats(rd_cost);
6251*77c1e3ccSAndroid Build Coastguard Worker     return;
6252*77c1e3ccSAndroid Build Coastguard Worker   }
6253*77c1e3ccSAndroid Build Coastguard Worker 
6254*77c1e3ccSAndroid Build Coastguard Worker   // update mode info array
6255*77c1e3ccSAndroid Build Coastguard Worker   fill_mode_info_sb(cpi, x, mi_row, mi_col, bsize, pc_tree);
6256*77c1e3ccSAndroid Build Coastguard Worker 
6257*77c1e3ccSAndroid Build Coastguard Worker   if (do_recon) {
6258*77c1e3ccSAndroid Build Coastguard Worker     if (bsize == cm->seq_params->sb_size) {
6259*77c1e3ccSAndroid Build Coastguard Worker       // NOTE: To get estimate for rate due to the tokens, use:
6260*77c1e3ccSAndroid Build Coastguard Worker       // int rate_coeffs = 0;
6261*77c1e3ccSAndroid Build Coastguard Worker       // encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, DRY_RUN_COSTCOEFFS,
6262*77c1e3ccSAndroid Build Coastguard Worker       //           bsize, pc_tree, &rate_coeffs);
6263*77c1e3ccSAndroid Build Coastguard Worker       set_cb_offsets(x->cb_offset, 0, 0);
6264*77c1e3ccSAndroid Build Coastguard Worker       encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, OUTPUT_ENABLED, bsize,
6265*77c1e3ccSAndroid Build Coastguard Worker                 pc_tree, NULL);
6266*77c1e3ccSAndroid Build Coastguard Worker     } else {
6267*77c1e3ccSAndroid Build Coastguard Worker       encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, DRY_RUN_NORMAL, bsize,
6268*77c1e3ccSAndroid Build Coastguard Worker                 pc_tree, NULL);
6269*77c1e3ccSAndroid Build Coastguard Worker     }
6270*77c1e3ccSAndroid Build Coastguard Worker   }
6271*77c1e3ccSAndroid Build Coastguard Worker 
6272*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == BLOCK_64X64 && do_recon) {
6273*77c1e3ccSAndroid Build Coastguard Worker     assert(best_rdc.rate < INT_MAX);
6274*77c1e3ccSAndroid Build Coastguard Worker     assert(best_rdc.dist < INT64_MAX);
6275*77c1e3ccSAndroid Build Coastguard Worker   } else {
6276*77c1e3ccSAndroid Build Coastguard Worker     assert(tp_orig == *tp);
6277*77c1e3ccSAndroid Build Coastguard Worker   }
6278*77c1e3ccSAndroid Build Coastguard Worker }
6279*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_RT_ML_PARTITIONING
6280