• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <stdint.h>
13 #include <float.h>
14 
15 #include "av1/encoder/thirdpass.h"
16 #include "config/aom_config.h"
17 #include "config/aom_dsp_rtcd.h"
18 #include "config/aom_scale_rtcd.h"
19 
20 #include "aom/aom_codec.h"
21 
22 #include "av1/common/av1_common_int.h"
23 #include "av1/common/enums.h"
24 #include "av1/common/idct.h"
25 #include "av1/common/reconintra.h"
26 
27 #include "av1/encoder/encoder.h"
28 #include "av1/encoder/ethread.h"
29 #include "av1/encoder/encodeframe_utils.h"
30 #include "av1/encoder/encode_strategy.h"
31 #include "av1/encoder/hybrid_fwd_txfm.h"
32 #include "av1/encoder/motion_search_facade.h"
33 #include "av1/encoder/rd.h"
34 #include "av1/encoder/rdopt.h"
35 #include "av1/encoder/reconinter_enc.h"
36 #include "av1/encoder/tpl_model.h"
37 
exp_bounded(double v)38 static INLINE double exp_bounded(double v) {
39   // When v > 700 or <-700, the exp function will be close to overflow
40   // For details, see the "Notes" in the following link.
41   // https://en.cppreference.com/w/c/numeric/math/exp
42   if (v > 700) {
43     return DBL_MAX;
44   } else if (v < -700) {
45     return 0;
46   }
47   return exp(v);
48 }
49 
av1_init_tpl_txfm_stats(TplTxfmStats * tpl_txfm_stats)50 void av1_init_tpl_txfm_stats(TplTxfmStats *tpl_txfm_stats) {
51   tpl_txfm_stats->ready = 0;
52   tpl_txfm_stats->coeff_num = 256;
53   tpl_txfm_stats->txfm_block_count = 0;
54   memset(tpl_txfm_stats->abs_coeff_sum, 0,
55          sizeof(tpl_txfm_stats->abs_coeff_sum[0]) * tpl_txfm_stats->coeff_num);
56   memset(tpl_txfm_stats->abs_coeff_mean, 0,
57          sizeof(tpl_txfm_stats->abs_coeff_mean[0]) * tpl_txfm_stats->coeff_num);
58 }
59 
av1_accumulate_tpl_txfm_stats(const TplTxfmStats * sub_stats,TplTxfmStats * accumulated_stats)60 void av1_accumulate_tpl_txfm_stats(const TplTxfmStats *sub_stats,
61                                    TplTxfmStats *accumulated_stats) {
62   accumulated_stats->txfm_block_count += sub_stats->txfm_block_count;
63   for (int i = 0; i < accumulated_stats->coeff_num; ++i) {
64     accumulated_stats->abs_coeff_sum[i] += sub_stats->abs_coeff_sum[i];
65   }
66 }
67 
av1_record_tpl_txfm_block(TplTxfmStats * tpl_txfm_stats,const tran_low_t * coeff)68 void av1_record_tpl_txfm_block(TplTxfmStats *tpl_txfm_stats,
69                                const tran_low_t *coeff) {
70   // For transform larger than 16x16, the scale of coeff need to be adjusted.
71   // It's not LOSSLESS_Q_STEP.
72   assert(tpl_txfm_stats->coeff_num <= 256);
73   for (int i = 0; i < tpl_txfm_stats->coeff_num; ++i) {
74     tpl_txfm_stats->abs_coeff_sum[i] += abs(coeff[i]) / (double)LOSSLESS_Q_STEP;
75   }
76   ++tpl_txfm_stats->txfm_block_count;
77 }
78 
av1_tpl_txfm_stats_update_abs_coeff_mean(TplTxfmStats * txfm_stats)79 void av1_tpl_txfm_stats_update_abs_coeff_mean(TplTxfmStats *txfm_stats) {
80   if (txfm_stats->txfm_block_count > 0) {
81     for (int j = 0; j < txfm_stats->coeff_num; j++) {
82       txfm_stats->abs_coeff_mean[j] =
83           txfm_stats->abs_coeff_sum[j] / txfm_stats->txfm_block_count;
84     }
85     txfm_stats->ready = 1;
86   } else {
87     txfm_stats->ready = 0;
88   }
89 }
90 
av1_tpl_store_txfm_stats(TplParams * tpl_data,const TplTxfmStats * tpl_txfm_stats,const int frame_index)91 static AOM_INLINE void av1_tpl_store_txfm_stats(
92     TplParams *tpl_data, const TplTxfmStats *tpl_txfm_stats,
93     const int frame_index) {
94   tpl_data->txfm_stats_list[frame_index] = *tpl_txfm_stats;
95 }
96 
get_quantize_error(const MACROBLOCK * x,int plane,const tran_low_t * coeff,tran_low_t * qcoeff,tran_low_t * dqcoeff,TX_SIZE tx_size,uint16_t * eob,int64_t * recon_error,int64_t * sse)97 static AOM_INLINE void get_quantize_error(const MACROBLOCK *x, int plane,
98                                           const tran_low_t *coeff,
99                                           tran_low_t *qcoeff,
100                                           tran_low_t *dqcoeff, TX_SIZE tx_size,
101                                           uint16_t *eob, int64_t *recon_error,
102                                           int64_t *sse) {
103   const struct macroblock_plane *const p = &x->plane[plane];
104   const MACROBLOCKD *xd = &x->e_mbd;
105   const SCAN_ORDER *const scan_order = &av1_scan_orders[tx_size][DCT_DCT];
106   int pix_num = 1 << num_pels_log2_lookup[txsize_to_bsize[tx_size]];
107   const int shift = tx_size == TX_32X32 ? 0 : 2;
108 
109   QUANT_PARAM quant_param;
110   av1_setup_quant(tx_size, 0, AV1_XFORM_QUANT_FP, 0, &quant_param);
111 
112 #if CONFIG_AV1_HIGHBITDEPTH
113   if (is_cur_buf_hbd(xd)) {
114     av1_highbd_quantize_fp_facade(coeff, pix_num, p, qcoeff, dqcoeff, eob,
115                                   scan_order, &quant_param);
116     *recon_error =
117         av1_highbd_block_error(coeff, dqcoeff, pix_num, sse, xd->bd) >> shift;
118   } else {
119     av1_quantize_fp_facade(coeff, pix_num, p, qcoeff, dqcoeff, eob, scan_order,
120                            &quant_param);
121     *recon_error = av1_block_error(coeff, dqcoeff, pix_num, sse) >> shift;
122   }
123 #else
124   (void)xd;
125   av1_quantize_fp_facade(coeff, pix_num, p, qcoeff, dqcoeff, eob, scan_order,
126                          &quant_param);
127   *recon_error = av1_block_error(coeff, dqcoeff, pix_num, sse) >> shift;
128 #endif  // CONFIG_AV1_HIGHBITDEPTH
129 
130   *recon_error = AOMMAX(*recon_error, 1);
131 
132   *sse = (*sse) >> shift;
133   *sse = AOMMAX(*sse, 1);
134 }
135 
set_tpl_stats_block_size(uint8_t * block_mis_log2,uint8_t * tpl_bsize_1d)136 static AOM_INLINE void set_tpl_stats_block_size(uint8_t *block_mis_log2,
137                                                 uint8_t *tpl_bsize_1d) {
138   // tpl stats bsize: 2 means 16x16
139   *block_mis_log2 = 2;
140   // Block size used in tpl motion estimation
141   *tpl_bsize_1d = 16;
142   // MIN_TPL_BSIZE_1D = 16;
143   assert(*tpl_bsize_1d >= 16);
144 }
145 
av1_setup_tpl_buffers(AV1_PRIMARY * const ppi,CommonModeInfoParams * const mi_params,int width,int height,int byte_alignment,int lag_in_frames)146 void av1_setup_tpl_buffers(AV1_PRIMARY *const ppi,
147                            CommonModeInfoParams *const mi_params, int width,
148                            int height, int byte_alignment, int lag_in_frames) {
149   SequenceHeader *const seq_params = &ppi->seq_params;
150   TplParams *const tpl_data = &ppi->tpl_data;
151   set_tpl_stats_block_size(&tpl_data->tpl_stats_block_mis_log2,
152                            &tpl_data->tpl_bsize_1d);
153   const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
154   tpl_data->border_in_pixels =
155       ALIGN_POWER_OF_TWO(tpl_data->tpl_bsize_1d + 2 * AOM_INTERP_EXTEND, 5);
156 
157   const int alloc_y_plane_only =
158       ppi->cpi->sf.tpl_sf.use_y_only_rate_distortion ? 1 : 0;
159   for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
160     const int mi_cols =
161         ALIGN_POWER_OF_TWO(mi_params->mi_cols, MAX_MIB_SIZE_LOG2);
162     const int mi_rows =
163         ALIGN_POWER_OF_TWO(mi_params->mi_rows, MAX_MIB_SIZE_LOG2);
164     TplDepFrame *tpl_frame = &tpl_data->tpl_stats_buffer[frame];
165     tpl_frame->is_valid = 0;
166     tpl_frame->width = mi_cols >> block_mis_log2;
167     tpl_frame->height = mi_rows >> block_mis_log2;
168     tpl_frame->stride = tpl_data->tpl_stats_buffer[frame].width;
169     tpl_frame->mi_rows = mi_params->mi_rows;
170     tpl_frame->mi_cols = mi_params->mi_cols;
171   }
172   tpl_data->tpl_frame = &tpl_data->tpl_stats_buffer[REF_FRAMES + 1];
173 
174   // If lag_in_frames <= 1, TPL module is not invoked. Hence dynamic memory
175   // allocations are avoided for buffers in tpl_data.
176   if (lag_in_frames <= 1) return;
177 
178   AOM_CHECK_MEM_ERROR(&ppi->error, tpl_data->txfm_stats_list,
179                       aom_calloc(MAX_LENGTH_TPL_FRAME_STATS,
180                                  sizeof(*tpl_data->txfm_stats_list)));
181 
182   for (int frame = 0; frame < lag_in_frames; ++frame) {
183     AOM_CHECK_MEM_ERROR(
184         &ppi->error, tpl_data->tpl_stats_pool[frame],
185         aom_calloc(tpl_data->tpl_stats_buffer[frame].width *
186                        tpl_data->tpl_stats_buffer[frame].height,
187                    sizeof(*tpl_data->tpl_stats_buffer[frame].tpl_stats_ptr)));
188 
189     if (aom_alloc_frame_buffer(
190             &tpl_data->tpl_rec_pool[frame], width, height,
191             seq_params->subsampling_x, seq_params->subsampling_y,
192             seq_params->use_highbitdepth, tpl_data->border_in_pixels,
193             byte_alignment, alloc_y_plane_only))
194       aom_internal_error(&ppi->error, AOM_CODEC_MEM_ERROR,
195                          "Failed to allocate frame buffer");
196   }
197 }
198 
tpl_get_satd_cost(BitDepthInfo bd_info,int16_t * src_diff,int diff_stride,const uint8_t * src,int src_stride,const uint8_t * dst,int dst_stride,tran_low_t * coeff,int bw,int bh,TX_SIZE tx_size)199 static AOM_INLINE int32_t tpl_get_satd_cost(BitDepthInfo bd_info,
200                                             int16_t *src_diff, int diff_stride,
201                                             const uint8_t *src, int src_stride,
202                                             const uint8_t *dst, int dst_stride,
203                                             tran_low_t *coeff, int bw, int bh,
204                                             TX_SIZE tx_size) {
205   const int pix_num = bw * bh;
206 
207   av1_subtract_block(bd_info, bh, bw, src_diff, diff_stride, src, src_stride,
208                      dst, dst_stride);
209   av1_quick_txfm(/*use_hadamard=*/0, tx_size, bd_info, src_diff, bw, coeff);
210   return aom_satd(coeff, pix_num);
211 }
212 
rate_estimator(const tran_low_t * qcoeff,int eob,TX_SIZE tx_size)213 static int rate_estimator(const tran_low_t *qcoeff, int eob, TX_SIZE tx_size) {
214   const SCAN_ORDER *const scan_order = &av1_scan_orders[tx_size][DCT_DCT];
215 
216   assert((1 << num_pels_log2_lookup[txsize_to_bsize[tx_size]]) >= eob);
217   int rate_cost = 1;
218 
219   for (int idx = 0; idx < eob; ++idx) {
220     int abs_level = abs(qcoeff[scan_order->scan[idx]]);
221     rate_cost += (int)(log(abs_level + 1.0) / log(2.0)) + 1 + (abs_level > 0);
222   }
223 
224   return (rate_cost << AV1_PROB_COST_SHIFT);
225 }
226 
txfm_quant_rdcost(const MACROBLOCK * x,int16_t * src_diff,int diff_stride,uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,tran_low_t * coeff,tran_low_t * qcoeff,tran_low_t * dqcoeff,int bw,int bh,TX_SIZE tx_size,int * rate_cost,int64_t * recon_error,int64_t * sse)227 static AOM_INLINE void txfm_quant_rdcost(
228     const MACROBLOCK *x, int16_t *src_diff, int diff_stride, uint8_t *src,
229     int src_stride, uint8_t *dst, int dst_stride, tran_low_t *coeff,
230     tran_low_t *qcoeff, tran_low_t *dqcoeff, int bw, int bh, TX_SIZE tx_size,
231     int *rate_cost, int64_t *recon_error, int64_t *sse) {
232   const MACROBLOCKD *xd = &x->e_mbd;
233   const BitDepthInfo bd_info = get_bit_depth_info(xd);
234   uint16_t eob;
235   av1_subtract_block(bd_info, bh, bw, src_diff, diff_stride, src, src_stride,
236                      dst, dst_stride);
237   av1_quick_txfm(/*use_hadamard=*/0, tx_size, bd_info, src_diff, bw, coeff);
238 
239   get_quantize_error(x, 0, coeff, qcoeff, dqcoeff, tx_size, &eob, recon_error,
240                      sse);
241 
242   *rate_cost = rate_estimator(qcoeff, eob, tx_size);
243 
244   av1_inverse_transform_block(xd, dqcoeff, 0, DCT_DCT, tx_size, dst, dst_stride,
245                               eob, 0);
246 }
247 
motion_estimation(AV1_COMP * cpi,MACROBLOCK * x,uint8_t * cur_frame_buf,uint8_t * ref_frame_buf,int stride,int stride_ref,BLOCK_SIZE bsize,MV center_mv,int_mv * best_mv)248 static uint32_t motion_estimation(AV1_COMP *cpi, MACROBLOCK *x,
249                                   uint8_t *cur_frame_buf,
250                                   uint8_t *ref_frame_buf, int stride,
251                                   int stride_ref, BLOCK_SIZE bsize,
252                                   MV center_mv, int_mv *best_mv) {
253   AV1_COMMON *cm = &cpi->common;
254   MACROBLOCKD *const xd = &x->e_mbd;
255   TPL_SPEED_FEATURES *tpl_sf = &cpi->sf.tpl_sf;
256   int step_param;
257   uint32_t bestsme = UINT_MAX;
258   int distortion;
259   uint32_t sse;
260   int cost_list[5];
261   FULLPEL_MV start_mv = get_fullmv_from_mv(&center_mv);
262 
263   // Setup frame pointers
264   x->plane[0].src.buf = cur_frame_buf;
265   x->plane[0].src.stride = stride;
266   xd->plane[0].pre[0].buf = ref_frame_buf;
267   xd->plane[0].pre[0].stride = stride_ref;
268 
269   step_param = tpl_sf->reduce_first_step_size;
270   step_param = AOMMIN(step_param, MAX_MVSEARCH_STEPS - 2);
271 
272   const search_site_config *search_site_cfg =
273       cpi->mv_search_params.search_site_cfg[SS_CFG_SRC];
274   if (search_site_cfg->stride != stride_ref)
275     search_site_cfg = cpi->mv_search_params.search_site_cfg[SS_CFG_LOOKAHEAD];
276   assert(search_site_cfg->stride == stride_ref);
277 
278   FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
279   av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, &center_mv,
280                                      search_site_cfg,
281                                      /*fine_search_interval=*/0);
282   av1_set_mv_search_method(&full_ms_params, search_site_cfg,
283                            tpl_sf->search_method);
284 
285   av1_full_pixel_search(start_mv, &full_ms_params, step_param,
286                         cond_cost_list(cpi, cost_list), &best_mv->as_fullmv,
287                         NULL);
288 
289   SUBPEL_MOTION_SEARCH_PARAMS ms_params;
290   av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &center_mv,
291                                     cost_list);
292   ms_params.forced_stop = tpl_sf->subpel_force_stop;
293   ms_params.var_params.subpel_search_type = USE_2_TAPS;
294   ms_params.mv_cost_params.mv_cost_type = MV_COST_NONE;
295   MV subpel_start_mv = get_mv_from_fullmv(&best_mv->as_fullmv);
296   bestsme = cpi->mv_search_params.find_fractional_mv_step(
297       xd, cm, &ms_params, subpel_start_mv, &best_mv->as_mv, &distortion, &sse,
298       NULL);
299 
300   return bestsme;
301 }
302 
303 typedef struct {
304   int_mv mv;
305   int sad;
306 } center_mv_t;
307 
compare_sad(const void * a,const void * b)308 static int compare_sad(const void *a, const void *b) {
309   const int diff = ((center_mv_t *)a)->sad - ((center_mv_t *)b)->sad;
310   if (diff < 0)
311     return -1;
312   else if (diff > 0)
313     return 1;
314   return 0;
315 }
316 
is_alike_mv(int_mv candidate_mv,center_mv_t * center_mvs,int center_mvs_count,int skip_alike_starting_mv)317 static int is_alike_mv(int_mv candidate_mv, center_mv_t *center_mvs,
318                        int center_mvs_count, int skip_alike_starting_mv) {
319   // MV difference threshold is in 1/8 precision.
320   const int mv_diff_thr[3] = { 1, (8 << 3), (16 << 3) };
321   int thr = mv_diff_thr[skip_alike_starting_mv];
322   int i;
323 
324   for (i = 0; i < center_mvs_count; i++) {
325     if (abs(center_mvs[i].mv.as_mv.col - candidate_mv.as_mv.col) < thr &&
326         abs(center_mvs[i].mv.as_mv.row - candidate_mv.as_mv.row) < thr)
327       return 1;
328   }
329 
330   return 0;
331 }
332 
get_rate_distortion(int * rate_cost,int64_t * recon_error,int64_t * pred_error,int16_t * src_diff,tran_low_t * coeff,tran_low_t * qcoeff,tran_low_t * dqcoeff,AV1_COMMON * cm,MACROBLOCK * x,const YV12_BUFFER_CONFIG * ref_frame_ptr[2],uint8_t * rec_buffer_pool[3],const int rec_stride_pool[3],TX_SIZE tx_size,PREDICTION_MODE best_mode,int mi_row,int mi_col,int use_y_only_rate_distortion,TplTxfmStats * tpl_txfm_stats)333 static void get_rate_distortion(
334     int *rate_cost, int64_t *recon_error, int64_t *pred_error,
335     int16_t *src_diff, tran_low_t *coeff, tran_low_t *qcoeff,
336     tran_low_t *dqcoeff, AV1_COMMON *cm, MACROBLOCK *x,
337     const YV12_BUFFER_CONFIG *ref_frame_ptr[2], uint8_t *rec_buffer_pool[3],
338     const int rec_stride_pool[3], TX_SIZE tx_size, PREDICTION_MODE best_mode,
339     int mi_row, int mi_col, int use_y_only_rate_distortion,
340     TplTxfmStats *tpl_txfm_stats) {
341   const SequenceHeader *seq_params = cm->seq_params;
342   *rate_cost = 0;
343   *recon_error = 1;
344   *pred_error = 1;
345 
346   MACROBLOCKD *xd = &x->e_mbd;
347   int is_compound = (best_mode == NEW_NEWMV);
348   int num_planes = use_y_only_rate_distortion ? 1 : MAX_MB_PLANE;
349 
350   uint8_t *src_buffer_pool[MAX_MB_PLANE] = {
351     xd->cur_buf->y_buffer,
352     xd->cur_buf->u_buffer,
353     xd->cur_buf->v_buffer,
354   };
355   const int src_stride_pool[MAX_MB_PLANE] = {
356     xd->cur_buf->y_stride,
357     xd->cur_buf->uv_stride,
358     xd->cur_buf->uv_stride,
359   };
360 
361   const int_interpfilters kernel =
362       av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
363 
364   for (int plane = 0; plane < num_planes; ++plane) {
365     struct macroblockd_plane *pd = &xd->plane[plane];
366     BLOCK_SIZE bsize_plane =
367         av1_ss_size_lookup[txsize_to_bsize[tx_size]][pd->subsampling_x]
368                           [pd->subsampling_y];
369 
370     int dst_buffer_stride = rec_stride_pool[plane];
371     int dst_mb_offset =
372         ((mi_row * MI_SIZE * dst_buffer_stride) >> pd->subsampling_y) +
373         ((mi_col * MI_SIZE) >> pd->subsampling_x);
374     uint8_t *dst_buffer = rec_buffer_pool[plane] + dst_mb_offset;
375     for (int ref = 0; ref < 1 + is_compound; ++ref) {
376       if (!is_inter_mode(best_mode)) {
377         av1_predict_intra_block(
378             xd, seq_params->sb_size, seq_params->enable_intra_edge_filter,
379             block_size_wide[bsize_plane], block_size_high[bsize_plane],
380             max_txsize_rect_lookup[bsize_plane], best_mode, 0, 0,
381             FILTER_INTRA_MODES, dst_buffer, dst_buffer_stride, dst_buffer,
382             dst_buffer_stride, 0, 0, plane);
383       } else {
384         int_mv best_mv = xd->mi[0]->mv[ref];
385         uint8_t *ref_buffer_pool[MAX_MB_PLANE] = {
386           ref_frame_ptr[ref]->y_buffer,
387           ref_frame_ptr[ref]->u_buffer,
388           ref_frame_ptr[ref]->v_buffer,
389         };
390         InterPredParams inter_pred_params;
391         struct buf_2d ref_buf = {
392           NULL, ref_buffer_pool[plane],
393           plane ? ref_frame_ptr[ref]->uv_width : ref_frame_ptr[ref]->y_width,
394           plane ? ref_frame_ptr[ref]->uv_height : ref_frame_ptr[ref]->y_height,
395           plane ? ref_frame_ptr[ref]->uv_stride : ref_frame_ptr[ref]->y_stride
396         };
397         av1_init_inter_params(&inter_pred_params, block_size_wide[bsize_plane],
398                               block_size_high[bsize_plane],
399                               (mi_row * MI_SIZE) >> pd->subsampling_y,
400                               (mi_col * MI_SIZE) >> pd->subsampling_x,
401                               pd->subsampling_x, pd->subsampling_y, xd->bd,
402                               is_cur_buf_hbd(xd), 0,
403                               xd->block_ref_scale_factors[0], &ref_buf, kernel);
404         if (is_compound) av1_init_comp_mode(&inter_pred_params);
405         inter_pred_params.conv_params = get_conv_params_no_round(
406             ref, plane, xd->tmp_conv_dst, MAX_SB_SIZE, is_compound, xd->bd);
407 
408         av1_enc_build_one_inter_predictor(dst_buffer, dst_buffer_stride,
409                                           &best_mv.as_mv, &inter_pred_params);
410       }
411     }
412 
413     int src_stride = src_stride_pool[plane];
414     int src_mb_offset = ((mi_row * MI_SIZE * src_stride) >> pd->subsampling_y) +
415                         ((mi_col * MI_SIZE) >> pd->subsampling_x);
416 
417     int this_rate = 1;
418     int64_t this_recon_error = 1;
419     int64_t sse;
420     txfm_quant_rdcost(
421         x, src_diff, block_size_wide[bsize_plane],
422         src_buffer_pool[plane] + src_mb_offset, src_stride, dst_buffer,
423         dst_buffer_stride, coeff, qcoeff, dqcoeff, block_size_wide[bsize_plane],
424         block_size_high[bsize_plane], max_txsize_rect_lookup[bsize_plane],
425         &this_rate, &this_recon_error, &sse);
426 
427     if (plane == 0 && tpl_txfm_stats) {
428       // We only collect Y plane's transform coefficient
429       av1_record_tpl_txfm_block(tpl_txfm_stats, coeff);
430     }
431 
432     *recon_error += this_recon_error;
433     *pred_error += sse;
434     *rate_cost += this_rate;
435   }
436 }
437 
mode_estimation(AV1_COMP * cpi,TplTxfmStats * tpl_txfm_stats,MACROBLOCK * x,int mi_row,int mi_col,BLOCK_SIZE bsize,TX_SIZE tx_size,TplDepStats * tpl_stats)438 static AOM_INLINE void mode_estimation(AV1_COMP *cpi,
439                                        TplTxfmStats *tpl_txfm_stats,
440                                        MACROBLOCK *x, int mi_row, int mi_col,
441                                        BLOCK_SIZE bsize, TX_SIZE tx_size,
442                                        TplDepStats *tpl_stats) {
443   AV1_COMMON *cm = &cpi->common;
444   const GF_GROUP *gf_group = &cpi->ppi->gf_group;
445 
446   (void)gf_group;
447 
448   MACROBLOCKD *xd = &x->e_mbd;
449   const BitDepthInfo bd_info = get_bit_depth_info(xd);
450   TplParams *tpl_data = &cpi->ppi->tpl_data;
451   TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_data->frame_idx];
452   const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
453 
454   const int bw = 4 << mi_size_wide_log2[bsize];
455   const int bh = 4 << mi_size_high_log2[bsize];
456   const int_interpfilters kernel =
457       av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
458 
459   int frame_offset = tpl_data->frame_idx - cpi->gf_frame_index;
460 
461   int32_t best_intra_cost = INT32_MAX;
462   int32_t intra_cost;
463   PREDICTION_MODE best_mode = DC_PRED;
464 
465   int mb_y_offset = mi_row * MI_SIZE * xd->cur_buf->y_stride + mi_col * MI_SIZE;
466   uint8_t *src_mb_buffer = xd->cur_buf->y_buffer + mb_y_offset;
467   int src_stride = xd->cur_buf->y_stride;
468 
469   int dst_mb_offset =
470       mi_row * MI_SIZE * tpl_frame->rec_picture->y_stride + mi_col * MI_SIZE;
471   uint8_t *dst_buffer = tpl_frame->rec_picture->y_buffer + dst_mb_offset;
472   int dst_buffer_stride = tpl_frame->rec_picture->y_stride;
473   int use_y_only_rate_distortion = cpi->sf.tpl_sf.use_y_only_rate_distortion;
474 
475   uint8_t *rec_buffer_pool[3] = {
476     tpl_frame->rec_picture->y_buffer,
477     tpl_frame->rec_picture->u_buffer,
478     tpl_frame->rec_picture->v_buffer,
479   };
480 
481   const int rec_stride_pool[3] = {
482     tpl_frame->rec_picture->y_stride,
483     tpl_frame->rec_picture->uv_stride,
484     tpl_frame->rec_picture->uv_stride,
485   };
486 
487   for (int plane = 1; plane < MAX_MB_PLANE; ++plane) {
488     struct macroblockd_plane *pd = &xd->plane[plane];
489     pd->subsampling_x = xd->cur_buf->subsampling_x;
490     pd->subsampling_y = xd->cur_buf->subsampling_y;
491   }
492 
493   // Number of pixels in a tpl block
494   const int tpl_block_pels = tpl_data->tpl_bsize_1d * tpl_data->tpl_bsize_1d;
495   // Allocate temporary buffers used in motion estimation.
496   uint8_t *predictor8 = aom_memalign(32, tpl_block_pels * 2 * sizeof(uint8_t));
497   int16_t *src_diff = aom_memalign(32, tpl_block_pels * sizeof(int16_t));
498   tran_low_t *coeff = aom_memalign(32, tpl_block_pels * sizeof(tran_low_t));
499   tran_low_t *qcoeff = aom_memalign(32, tpl_block_pels * sizeof(tran_low_t));
500   tran_low_t *dqcoeff = aom_memalign(32, tpl_block_pels * sizeof(tran_low_t));
501   uint8_t *predictor =
502       is_cur_buf_hbd(xd) ? CONVERT_TO_BYTEPTR(predictor8) : predictor8;
503   int64_t recon_error = 1;
504   int64_t pred_error = 1;
505 
506   if (!(predictor8 && src_diff && coeff && qcoeff && dqcoeff)) {
507     aom_free(predictor8);
508     aom_free(src_diff);
509     aom_free(coeff);
510     aom_free(qcoeff);
511     aom_free(dqcoeff);
512     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
513                        "Error allocating tpl data");
514   }
515 
516   memset(tpl_stats, 0, sizeof(*tpl_stats));
517   tpl_stats->ref_frame_index[0] = -1;
518   tpl_stats->ref_frame_index[1] = -1;
519 
520   const int mi_width = mi_size_wide[bsize];
521   const int mi_height = mi_size_high[bsize];
522   set_mode_info_offsets(&cpi->common.mi_params, &cpi->mbmi_ext_info, x, xd,
523                         mi_row, mi_col);
524   set_mi_row_col(xd, &xd->tile, mi_row, mi_height, mi_col, mi_width,
525                  cm->mi_params.mi_rows, cm->mi_params.mi_cols);
526   set_plane_n4(xd, mi_size_wide[bsize], mi_size_high[bsize],
527                av1_num_planes(cm));
528   xd->mi[0]->bsize = bsize;
529   xd->mi[0]->motion_mode = SIMPLE_TRANSLATION;
530 
531   // Intra prediction search
532   xd->mi[0]->ref_frame[0] = INTRA_FRAME;
533 
534   // Pre-load the bottom left line.
535   if (xd->left_available &&
536       mi_row + tx_size_high_unit[tx_size] < xd->tile.mi_row_end) {
537     if (is_cur_buf_hbd(xd)) {
538       uint16_t *dst = CONVERT_TO_SHORTPTR(dst_buffer);
539       for (int i = 0; i < bw; ++i)
540         dst[(bw + i) * dst_buffer_stride - 1] =
541             dst[(bw - 1) * dst_buffer_stride - 1];
542     } else {
543       for (int i = 0; i < bw; ++i)
544         dst_buffer[(bw + i) * dst_buffer_stride - 1] =
545             dst_buffer[(bw - 1) * dst_buffer_stride - 1];
546     }
547   }
548 
549   // if cpi->sf.tpl_sf.prune_intra_modes is on, then search only DC_PRED,
550   // H_PRED, and V_PRED
551   const PREDICTION_MODE last_intra_mode =
552       cpi->sf.tpl_sf.prune_intra_modes ? D45_PRED : INTRA_MODE_END;
553   const SequenceHeader *seq_params = cm->seq_params;
554   for (PREDICTION_MODE mode = INTRA_MODE_START; mode < last_intra_mode;
555        ++mode) {
556     av1_predict_intra_block(xd, seq_params->sb_size,
557                             seq_params->enable_intra_edge_filter,
558                             block_size_wide[bsize], block_size_high[bsize],
559                             tx_size, mode, 0, 0, FILTER_INTRA_MODES, dst_buffer,
560                             dst_buffer_stride, predictor, bw, 0, 0, 0);
561 
562     intra_cost =
563         tpl_get_satd_cost(bd_info, src_diff, bw, src_mb_buffer, src_stride,
564                           predictor, bw, coeff, bw, bh, tx_size);
565 
566     if (intra_cost < best_intra_cost) {
567       best_intra_cost = intra_cost;
568       best_mode = mode;
569     }
570   }
571 
572   int rate_cost = 1;
573 
574   if (cpi->use_ducky_encode) {
575     get_rate_distortion(&rate_cost, &recon_error, &pred_error, src_diff, coeff,
576                         qcoeff, dqcoeff, cm, x, NULL, rec_buffer_pool,
577                         rec_stride_pool, tx_size, best_mode, mi_row, mi_col,
578                         use_y_only_rate_distortion, NULL);
579 
580     tpl_stats->intra_dist = recon_error << TPL_DEP_COST_SCALE_LOG2;
581     tpl_stats->intra_sse = pred_error << TPL_DEP_COST_SCALE_LOG2;
582     tpl_stats->intra_rate = rate_cost;
583   }
584 
585   if (cpi->third_pass_ctx &&
586       frame_offset < cpi->third_pass_ctx->frame_info_count &&
587       tpl_data->frame_idx < gf_group->size) {
588     double ratio_h, ratio_w;
589     av1_get_third_pass_ratio(cpi->third_pass_ctx, frame_offset, cm->height,
590                              cm->width, &ratio_h, &ratio_w);
591     THIRD_PASS_MI_INFO *this_mi = av1_get_third_pass_mi(
592         cpi->third_pass_ctx, frame_offset, mi_row, mi_col, ratio_h, ratio_w);
593 
594     PREDICTION_MODE third_pass_mode = this_mi->pred_mode;
595 
596     if (third_pass_mode >= last_intra_mode &&
597         third_pass_mode < INTRA_MODE_END) {
598       av1_predict_intra_block(
599           xd, seq_params->sb_size, seq_params->enable_intra_edge_filter,
600           block_size_wide[bsize], block_size_high[bsize], tx_size,
601           third_pass_mode, 0, 0, FILTER_INTRA_MODES, dst_buffer,
602           dst_buffer_stride, predictor, bw, 0, 0, 0);
603 
604       intra_cost =
605           tpl_get_satd_cost(bd_info, src_diff, bw, src_mb_buffer, src_stride,
606                             predictor, bw, coeff, bw, bh, tx_size);
607 
608       if (intra_cost < best_intra_cost) {
609         best_intra_cost = intra_cost;
610         best_mode = third_pass_mode;
611       }
612     }
613   }
614 
615   // Motion compensated prediction
616   xd->mi[0]->ref_frame[0] = INTRA_FRAME;
617   xd->mi[0]->ref_frame[1] = NONE_FRAME;
618   xd->mi[0]->compound_idx = 1;
619 
620   int best_rf_idx = -1;
621   int_mv best_mv[2];
622   int32_t inter_cost;
623   int32_t best_inter_cost = INT32_MAX;
624   int rf_idx;
625   int_mv single_mv[INTER_REFS_PER_FRAME];
626 
627   best_mv[0].as_int = INVALID_MV;
628   best_mv[1].as_int = INVALID_MV;
629 
630   for (rf_idx = 0; rf_idx < INTER_REFS_PER_FRAME; ++rf_idx) {
631     single_mv[rf_idx].as_int = INVALID_MV;
632     if (tpl_data->ref_frame[rf_idx] == NULL ||
633         tpl_data->src_ref_frame[rf_idx] == NULL) {
634       tpl_stats->mv[rf_idx].as_int = INVALID_MV;
635       continue;
636     }
637 
638     const YV12_BUFFER_CONFIG *ref_frame_ptr = tpl_data->src_ref_frame[rf_idx];
639     int ref_mb_offset =
640         mi_row * MI_SIZE * ref_frame_ptr->y_stride + mi_col * MI_SIZE;
641     uint8_t *ref_mb = ref_frame_ptr->y_buffer + ref_mb_offset;
642     int ref_stride = ref_frame_ptr->y_stride;
643 
644     int_mv best_rfidx_mv = { 0 };
645     uint32_t bestsme = UINT32_MAX;
646 
647     center_mv_t center_mvs[4] = { { { 0 }, INT_MAX },
648                                   { { 0 }, INT_MAX },
649                                   { { 0 }, INT_MAX },
650                                   { { 0 }, INT_MAX } };
651     int refmv_count = 1;
652     int idx;
653 
654     if (xd->up_available) {
655       TplDepStats *ref_tpl_stats = &tpl_frame->tpl_stats_ptr[av1_tpl_ptr_pos(
656           mi_row - mi_height, mi_col, tpl_frame->stride, block_mis_log2)];
657       if (!is_alike_mv(ref_tpl_stats->mv[rf_idx], center_mvs, refmv_count,
658                        cpi->sf.tpl_sf.skip_alike_starting_mv)) {
659         center_mvs[refmv_count].mv.as_int = ref_tpl_stats->mv[rf_idx].as_int;
660         ++refmv_count;
661       }
662     }
663 
664     if (xd->left_available) {
665       TplDepStats *ref_tpl_stats = &tpl_frame->tpl_stats_ptr[av1_tpl_ptr_pos(
666           mi_row, mi_col - mi_width, tpl_frame->stride, block_mis_log2)];
667       if (!is_alike_mv(ref_tpl_stats->mv[rf_idx], center_mvs, refmv_count,
668                        cpi->sf.tpl_sf.skip_alike_starting_mv)) {
669         center_mvs[refmv_count].mv.as_int = ref_tpl_stats->mv[rf_idx].as_int;
670         ++refmv_count;
671       }
672     }
673 
674     if (xd->up_available && mi_col + mi_width < xd->tile.mi_col_end) {
675       TplDepStats *ref_tpl_stats = &tpl_frame->tpl_stats_ptr[av1_tpl_ptr_pos(
676           mi_row - mi_height, mi_col + mi_width, tpl_frame->stride,
677           block_mis_log2)];
678       if (!is_alike_mv(ref_tpl_stats->mv[rf_idx], center_mvs, refmv_count,
679                        cpi->sf.tpl_sf.skip_alike_starting_mv)) {
680         center_mvs[refmv_count].mv.as_int = ref_tpl_stats->mv[rf_idx].as_int;
681         ++refmv_count;
682       }
683     }
684 
685     if (cpi->third_pass_ctx &&
686         frame_offset < cpi->third_pass_ctx->frame_info_count &&
687         tpl_data->frame_idx < gf_group->size) {
688       double ratio_h, ratio_w;
689       av1_get_third_pass_ratio(cpi->third_pass_ctx, frame_offset, cm->height,
690                                cm->width, &ratio_h, &ratio_w);
691       THIRD_PASS_MI_INFO *this_mi = av1_get_third_pass_mi(
692           cpi->third_pass_ctx, frame_offset, mi_row, mi_col, ratio_h, ratio_w);
693 
694       int_mv tp_mv = av1_get_third_pass_adjusted_mv(this_mi, ratio_h, ratio_w,
695                                                     rf_idx + LAST_FRAME);
696       if (tp_mv.as_int != INVALID_MV &&
697           !is_alike_mv(tp_mv, center_mvs + 1, refmv_count - 1,
698                        cpi->sf.tpl_sf.skip_alike_starting_mv)) {
699         center_mvs[0].mv = tp_mv;
700       }
701     }
702 
703     // Prune starting mvs
704     if (cpi->sf.tpl_sf.prune_starting_mv) {
705       // Get each center mv's sad.
706       for (idx = 0; idx < refmv_count; ++idx) {
707         FULLPEL_MV mv = get_fullmv_from_mv(&center_mvs[idx].mv.as_mv);
708         clamp_fullmv(&mv, &x->mv_limits);
709         center_mvs[idx].sad = (int)cpi->ppi->fn_ptr[bsize].sdf(
710             src_mb_buffer, src_stride, &ref_mb[mv.row * ref_stride + mv.col],
711             ref_stride);
712       }
713 
714       // Rank center_mv using sad.
715       if (refmv_count > 1) {
716         qsort(center_mvs, refmv_count, sizeof(center_mvs[0]), compare_sad);
717       }
718       refmv_count = AOMMIN(4 - cpi->sf.tpl_sf.prune_starting_mv, refmv_count);
719       // Further reduce number of refmv based on sad difference.
720       if (refmv_count > 1) {
721         int last_sad = center_mvs[refmv_count - 1].sad;
722         int second_to_last_sad = center_mvs[refmv_count - 2].sad;
723         if ((last_sad - second_to_last_sad) * 5 > second_to_last_sad)
724           refmv_count--;
725       }
726     }
727 
728     for (idx = 0; idx < refmv_count; ++idx) {
729       int_mv this_mv;
730       uint32_t thissme = motion_estimation(cpi, x, src_mb_buffer, ref_mb,
731                                            src_stride, ref_stride, bsize,
732                                            center_mvs[idx].mv.as_mv, &this_mv);
733 
734       if (thissme < bestsme) {
735         bestsme = thissme;
736         best_rfidx_mv = this_mv;
737       }
738     }
739 
740     tpl_stats->mv[rf_idx].as_int = best_rfidx_mv.as_int;
741     single_mv[rf_idx] = best_rfidx_mv;
742 
743     struct buf_2d ref_buf = { NULL, ref_frame_ptr->y_buffer,
744                               ref_frame_ptr->y_width, ref_frame_ptr->y_height,
745                               ref_frame_ptr->y_stride };
746     InterPredParams inter_pred_params;
747     av1_init_inter_params(&inter_pred_params, bw, bh, mi_row * MI_SIZE,
748                           mi_col * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd), 0,
749                           &tpl_data->sf, &ref_buf, kernel);
750     inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
751 
752     av1_enc_build_one_inter_predictor(predictor, bw, &best_rfidx_mv.as_mv,
753                                       &inter_pred_params);
754 
755     inter_cost =
756         tpl_get_satd_cost(bd_info, src_diff, bw, src_mb_buffer, src_stride,
757                           predictor, bw, coeff, bw, bh, tx_size);
758     // Store inter cost for each ref frame
759     tpl_stats->pred_error[rf_idx] = AOMMAX(1, inter_cost);
760 
761     if (inter_cost < best_inter_cost) {
762       best_rf_idx = rf_idx;
763 
764       best_inter_cost = inter_cost;
765       best_mv[0].as_int = best_rfidx_mv.as_int;
766     }
767   }
768 
769   if (best_rf_idx != -1 && best_inter_cost < best_intra_cost) {
770     best_mode = NEWMV;
771     xd->mi[0]->ref_frame[0] = best_rf_idx + LAST_FRAME;
772     xd->mi[0]->mv[0].as_int = best_mv[0].as_int;
773   }
774 
775   // Start compound predition search.
776   int comp_ref_frames[3][2] = {
777     { 0, 4 },
778     { 0, 6 },
779     { 3, 6 },
780   };
781 
782   int start_rf = 0;
783   int end_rf = 3;
784   if (!cpi->sf.tpl_sf.allow_compound_pred) end_rf = 0;
785   if (cpi->third_pass_ctx &&
786       frame_offset < cpi->third_pass_ctx->frame_info_count &&
787       tpl_data->frame_idx < gf_group->size) {
788     double ratio_h, ratio_w;
789     av1_get_third_pass_ratio(cpi->third_pass_ctx, frame_offset, cm->height,
790                              cm->width, &ratio_h, &ratio_w);
791     THIRD_PASS_MI_INFO *this_mi = av1_get_third_pass_mi(
792         cpi->third_pass_ctx, frame_offset, mi_row, mi_col, ratio_h, ratio_w);
793 
794     if (this_mi->ref_frame[0] >= LAST_FRAME &&
795         this_mi->ref_frame[1] >= LAST_FRAME) {
796       int found = 0;
797       for (int i = 0; i < 3; i++) {
798         if (comp_ref_frames[i][0] + LAST_FRAME == this_mi->ref_frame[0] &&
799             comp_ref_frames[i][1] + LAST_FRAME == this_mi->ref_frame[1]) {
800           found = 1;
801           break;
802         }
803       }
804       if (!found || !cpi->sf.tpl_sf.allow_compound_pred) {
805         comp_ref_frames[2][0] = this_mi->ref_frame[0] - LAST_FRAME;
806         comp_ref_frames[2][1] = this_mi->ref_frame[1] - LAST_FRAME;
807         if (!cpi->sf.tpl_sf.allow_compound_pred) {
808           start_rf = 2;
809           end_rf = 3;
810         }
811       }
812     }
813   }
814 
815   xd->mi_row = mi_row;
816   xd->mi_col = mi_col;
817   int best_cmp_rf_idx = -1;
818   for (int cmp_rf_idx = start_rf; cmp_rf_idx < end_rf; ++cmp_rf_idx) {
819     int rf_idx0 = comp_ref_frames[cmp_rf_idx][0];
820     int rf_idx1 = comp_ref_frames[cmp_rf_idx][1];
821 
822     if (tpl_data->ref_frame[rf_idx0] == NULL ||
823         tpl_data->src_ref_frame[rf_idx0] == NULL ||
824         tpl_data->ref_frame[rf_idx1] == NULL ||
825         tpl_data->src_ref_frame[rf_idx1] == NULL) {
826       continue;
827     }
828 
829     const YV12_BUFFER_CONFIG *ref_frame_ptr[2] = {
830       tpl_data->src_ref_frame[rf_idx0],
831       tpl_data->src_ref_frame[rf_idx1],
832     };
833 
834     xd->mi[0]->ref_frame[0] = rf_idx0 + LAST_FRAME;
835     xd->mi[0]->ref_frame[1] = rf_idx1 + LAST_FRAME;
836     xd->mi[0]->mode = NEW_NEWMV;
837     const int8_t ref_frame_type = av1_ref_frame_type(xd->mi[0]->ref_frame);
838     // Set up ref_mv for av1_joint_motion_search().
839     CANDIDATE_MV *this_ref_mv_stack = x->mbmi_ext.ref_mv_stack[ref_frame_type];
840     this_ref_mv_stack[xd->mi[0]->ref_mv_idx].this_mv = single_mv[rf_idx0];
841     this_ref_mv_stack[xd->mi[0]->ref_mv_idx].comp_mv = single_mv[rf_idx1];
842 
843     struct buf_2d yv12_mb[2][MAX_MB_PLANE];
844     for (int i = 0; i < 2; ++i) {
845       av1_setup_pred_block(xd, yv12_mb[i], ref_frame_ptr[i],
846                            xd->block_ref_scale_factors[i],
847                            xd->block_ref_scale_factors[i], MAX_MB_PLANE);
848       for (int plane = 0; plane < MAX_MB_PLANE; ++plane) {
849         xd->plane[plane].pre[i] = yv12_mb[i][plane];
850       }
851     }
852 
853     int_mv tmp_mv[2] = { single_mv[rf_idx0], single_mv[rf_idx1] };
854     int rate_mv;
855     av1_joint_motion_search(cpi, x, bsize, tmp_mv, NULL, 0, &rate_mv,
856                             !cpi->sf.mv_sf.disable_second_mv);
857 
858     for (int ref = 0; ref < 2; ++ref) {
859       struct buf_2d ref_buf = { NULL, ref_frame_ptr[ref]->y_buffer,
860                                 ref_frame_ptr[ref]->y_width,
861                                 ref_frame_ptr[ref]->y_height,
862                                 ref_frame_ptr[ref]->y_stride };
863       InterPredParams inter_pred_params;
864       av1_init_inter_params(&inter_pred_params, bw, bh, mi_row * MI_SIZE,
865                             mi_col * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd),
866                             0, &tpl_data->sf, &ref_buf, kernel);
867       av1_init_comp_mode(&inter_pred_params);
868 
869       inter_pred_params.conv_params = get_conv_params_no_round(
870           ref, 0, xd->tmp_conv_dst, MAX_SB_SIZE, 1, xd->bd);
871 
872       av1_enc_build_one_inter_predictor(predictor, bw, &tmp_mv[ref].as_mv,
873                                         &inter_pred_params);
874     }
875     inter_cost =
876         tpl_get_satd_cost(bd_info, src_diff, bw, src_mb_buffer, src_stride,
877                           predictor, bw, coeff, bw, bh, tx_size);
878     if (inter_cost < best_inter_cost) {
879       best_cmp_rf_idx = cmp_rf_idx;
880       best_inter_cost = inter_cost;
881       best_mv[0] = tmp_mv[0];
882       best_mv[1] = tmp_mv[1];
883     }
884   }
885 
886   if (best_cmp_rf_idx != -1 && best_inter_cost < best_intra_cost) {
887     best_mode = NEW_NEWMV;
888     const int best_rf_idx0 = comp_ref_frames[best_cmp_rf_idx][0];
889     const int best_rf_idx1 = comp_ref_frames[best_cmp_rf_idx][1];
890     xd->mi[0]->ref_frame[0] = best_rf_idx0 + LAST_FRAME;
891     xd->mi[0]->ref_frame[1] = best_rf_idx1 + LAST_FRAME;
892   }
893 
894   if (best_inter_cost < INT32_MAX) {
895     xd->mi[0]->mv[0].as_int = best_mv[0].as_int;
896     xd->mi[0]->mv[1].as_int = best_mv[1].as_int;
897     const YV12_BUFFER_CONFIG *ref_frame_ptr[2] = {
898       best_cmp_rf_idx >= 0
899           ? tpl_data->src_ref_frame[comp_ref_frames[best_cmp_rf_idx][0]]
900           : tpl_data->src_ref_frame[best_rf_idx],
901       best_cmp_rf_idx >= 0
902           ? tpl_data->src_ref_frame[comp_ref_frames[best_cmp_rf_idx][1]]
903           : NULL,
904     };
905     rate_cost = 1;
906     get_rate_distortion(&rate_cost, &recon_error, &pred_error, src_diff, coeff,
907                         qcoeff, dqcoeff, cm, x, ref_frame_ptr, rec_buffer_pool,
908                         rec_stride_pool, tx_size, best_mode, mi_row, mi_col,
909                         use_y_only_rate_distortion, NULL);
910     tpl_stats->srcrf_rate = rate_cost;
911   }
912 
913   best_intra_cost = AOMMAX(best_intra_cost, 1);
914   best_inter_cost = AOMMIN(best_intra_cost, best_inter_cost);
915   tpl_stats->inter_cost = best_inter_cost;
916   tpl_stats->intra_cost = best_intra_cost;
917 
918   tpl_stats->srcrf_dist = recon_error << TPL_DEP_COST_SCALE_LOG2;
919   tpl_stats->srcrf_sse = pred_error << TPL_DEP_COST_SCALE_LOG2;
920 
921   // Final encode
922   rate_cost = 0;
923   const YV12_BUFFER_CONFIG *ref_frame_ptr[2];
924 
925   ref_frame_ptr[0] =
926       best_mode == NEW_NEWMV
927           ? tpl_data->ref_frame[comp_ref_frames[best_cmp_rf_idx][0]]
928       : best_rf_idx >= 0 ? tpl_data->ref_frame[best_rf_idx]
929                          : NULL;
930   ref_frame_ptr[1] =
931       best_mode == NEW_NEWMV
932           ? tpl_data->ref_frame[comp_ref_frames[best_cmp_rf_idx][1]]
933           : NULL;
934   get_rate_distortion(&rate_cost, &recon_error, &pred_error, src_diff, coeff,
935                       qcoeff, dqcoeff, cm, x, ref_frame_ptr, rec_buffer_pool,
936                       rec_stride_pool, tx_size, best_mode, mi_row, mi_col,
937                       use_y_only_rate_distortion, tpl_txfm_stats);
938 
939   tpl_stats->recrf_dist = recon_error << TPL_DEP_COST_SCALE_LOG2;
940   tpl_stats->recrf_sse = pred_error << TPL_DEP_COST_SCALE_LOG2;
941   tpl_stats->recrf_rate = rate_cost;
942 
943   if (!is_inter_mode(best_mode)) {
944     tpl_stats->srcrf_dist = recon_error << TPL_DEP_COST_SCALE_LOG2;
945     tpl_stats->srcrf_rate = rate_cost;
946     tpl_stats->srcrf_sse = pred_error << TPL_DEP_COST_SCALE_LOG2;
947   }
948 
949   tpl_stats->recrf_dist = AOMMAX(tpl_stats->srcrf_dist, tpl_stats->recrf_dist);
950   tpl_stats->recrf_rate = AOMMAX(tpl_stats->srcrf_rate, tpl_stats->recrf_rate);
951 
952   if (best_mode == NEW_NEWMV) {
953     ref_frame_ptr[0] = tpl_data->ref_frame[comp_ref_frames[best_cmp_rf_idx][0]];
954     ref_frame_ptr[1] =
955         tpl_data->src_ref_frame[comp_ref_frames[best_cmp_rf_idx][1]];
956     get_rate_distortion(&rate_cost, &recon_error, &pred_error, src_diff, coeff,
957                         qcoeff, dqcoeff, cm, x, ref_frame_ptr, rec_buffer_pool,
958                         rec_stride_pool, tx_size, best_mode, mi_row, mi_col,
959                         use_y_only_rate_distortion, NULL);
960     tpl_stats->cmp_recrf_dist[0] = recon_error << TPL_DEP_COST_SCALE_LOG2;
961     tpl_stats->cmp_recrf_rate[0] = rate_cost;
962 
963     tpl_stats->cmp_recrf_dist[0] =
964         AOMMAX(tpl_stats->srcrf_dist, tpl_stats->cmp_recrf_dist[0]);
965     tpl_stats->cmp_recrf_rate[0] =
966         AOMMAX(tpl_stats->srcrf_rate, tpl_stats->cmp_recrf_rate[0]);
967 
968     tpl_stats->cmp_recrf_dist[0] =
969         AOMMIN(tpl_stats->recrf_dist, tpl_stats->cmp_recrf_dist[0]);
970     tpl_stats->cmp_recrf_rate[0] =
971         AOMMIN(tpl_stats->recrf_rate, tpl_stats->cmp_recrf_rate[0]);
972 
973     rate_cost = 0;
974     ref_frame_ptr[0] =
975         tpl_data->src_ref_frame[comp_ref_frames[best_cmp_rf_idx][0]];
976     ref_frame_ptr[1] = tpl_data->ref_frame[comp_ref_frames[best_cmp_rf_idx][1]];
977     get_rate_distortion(&rate_cost, &recon_error, &pred_error, src_diff, coeff,
978                         qcoeff, dqcoeff, cm, x, ref_frame_ptr, rec_buffer_pool,
979                         rec_stride_pool, tx_size, best_mode, mi_row, mi_col,
980                         use_y_only_rate_distortion, NULL);
981     tpl_stats->cmp_recrf_dist[1] = recon_error << TPL_DEP_COST_SCALE_LOG2;
982     tpl_stats->cmp_recrf_rate[1] = rate_cost;
983 
984     tpl_stats->cmp_recrf_dist[1] =
985         AOMMAX(tpl_stats->srcrf_dist, tpl_stats->cmp_recrf_dist[1]);
986     tpl_stats->cmp_recrf_rate[1] =
987         AOMMAX(tpl_stats->srcrf_rate, tpl_stats->cmp_recrf_rate[1]);
988 
989     tpl_stats->cmp_recrf_dist[1] =
990         AOMMIN(tpl_stats->recrf_dist, tpl_stats->cmp_recrf_dist[1]);
991     tpl_stats->cmp_recrf_rate[1] =
992         AOMMIN(tpl_stats->recrf_rate, tpl_stats->cmp_recrf_rate[1]);
993   }
994 
995   if (best_mode == NEWMV) {
996     tpl_stats->mv[best_rf_idx] = best_mv[0];
997     tpl_stats->ref_frame_index[0] = best_rf_idx;
998     tpl_stats->ref_frame_index[1] = NONE_FRAME;
999   } else if (best_mode == NEW_NEWMV) {
1000     tpl_stats->ref_frame_index[0] = comp_ref_frames[best_cmp_rf_idx][0];
1001     tpl_stats->ref_frame_index[1] = comp_ref_frames[best_cmp_rf_idx][1];
1002     tpl_stats->mv[tpl_stats->ref_frame_index[0]] = best_mv[0];
1003     tpl_stats->mv[tpl_stats->ref_frame_index[1]] = best_mv[1];
1004   }
1005 
1006   for (int idy = 0; idy < mi_height; ++idy) {
1007     for (int idx = 0; idx < mi_width; ++idx) {
1008       if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > idx &&
1009           (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > idy) {
1010         xd->mi[idx + idy * cm->mi_params.mi_stride] = xd->mi[0];
1011       }
1012     }
1013   }
1014 
1015   // Free temporary buffers.
1016   aom_free(predictor8);
1017   aom_free(src_diff);
1018   aom_free(coeff);
1019   aom_free(qcoeff);
1020   aom_free(dqcoeff);
1021 }
1022 
round_floor(int ref_pos,int bsize_pix)1023 static int round_floor(int ref_pos, int bsize_pix) {
1024   int round;
1025   if (ref_pos < 0)
1026     round = -(1 + (-ref_pos - 1) / bsize_pix);
1027   else
1028     round = ref_pos / bsize_pix;
1029 
1030   return round;
1031 }
1032 
av1_get_overlap_area(int row_a,int col_a,int row_b,int col_b,int width,int height)1033 int av1_get_overlap_area(int row_a, int col_a, int row_b, int col_b, int width,
1034                          int height) {
1035   int min_row = AOMMAX(row_a, row_b);
1036   int max_row = AOMMIN(row_a + height, row_b + height);
1037   int min_col = AOMMAX(col_a, col_b);
1038   int max_col = AOMMIN(col_a + width, col_b + width);
1039   if (min_row < max_row && min_col < max_col) {
1040     return (max_row - min_row) * (max_col - min_col);
1041   }
1042   return 0;
1043 }
1044 
av1_tpl_ptr_pos(int mi_row,int mi_col,int stride,uint8_t right_shift)1045 int av1_tpl_ptr_pos(int mi_row, int mi_col, int stride, uint8_t right_shift) {
1046   return (mi_row >> right_shift) * stride + (mi_col >> right_shift);
1047 }
1048 
av1_delta_rate_cost(int64_t delta_rate,int64_t recrf_dist,int64_t srcrf_dist,int pix_num)1049 int64_t av1_delta_rate_cost(int64_t delta_rate, int64_t recrf_dist,
1050                             int64_t srcrf_dist, int pix_num) {
1051   double beta = (double)srcrf_dist / recrf_dist;
1052   int64_t rate_cost = delta_rate;
1053 
1054   if (srcrf_dist <= 128) return rate_cost;
1055 
1056   double dr =
1057       (double)(delta_rate >> (TPL_DEP_COST_SCALE_LOG2 + AV1_PROB_COST_SHIFT)) /
1058       pix_num;
1059 
1060   double log_den = log(beta) / log(2.0) + 2.0 * dr;
1061 
1062   if (log_den > log(10.0) / log(2.0)) {
1063     rate_cost = (int64_t)((log(1.0 / beta) * pix_num) / log(2.0) / 2.0);
1064     rate_cost <<= (TPL_DEP_COST_SCALE_LOG2 + AV1_PROB_COST_SHIFT);
1065     return rate_cost;
1066   }
1067 
1068   double num = pow(2.0, log_den);
1069   double den = num * beta + (1 - beta) * beta;
1070 
1071   rate_cost = (int64_t)((pix_num * log(num / den)) / log(2.0) / 2.0);
1072 
1073   rate_cost <<= (TPL_DEP_COST_SCALE_LOG2 + AV1_PROB_COST_SHIFT);
1074 
1075   return rate_cost;
1076 }
1077 
tpl_model_update_b(TplParams * const tpl_data,int mi_row,int mi_col,const BLOCK_SIZE bsize,int frame_idx,int ref)1078 static AOM_INLINE void tpl_model_update_b(TplParams *const tpl_data, int mi_row,
1079                                           int mi_col, const BLOCK_SIZE bsize,
1080                                           int frame_idx, int ref) {
1081   TplDepFrame *tpl_frame_ptr = &tpl_data->tpl_frame[frame_idx];
1082   TplDepStats *tpl_ptr = tpl_frame_ptr->tpl_stats_ptr;
1083   TplDepFrame *tpl_frame = tpl_data->tpl_frame;
1084   const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
1085   TplDepStats *tpl_stats_ptr = &tpl_ptr[av1_tpl_ptr_pos(
1086       mi_row, mi_col, tpl_frame->stride, block_mis_log2)];
1087 
1088   int is_compound = tpl_stats_ptr->ref_frame_index[1] >= 0;
1089 
1090   if (tpl_stats_ptr->ref_frame_index[ref] < 0) return;
1091   const int ref_frame_index = tpl_stats_ptr->ref_frame_index[ref];
1092   TplDepFrame *ref_tpl_frame =
1093       &tpl_frame[tpl_frame[frame_idx].ref_map_index[ref_frame_index]];
1094   TplDepStats *ref_stats_ptr = ref_tpl_frame->tpl_stats_ptr;
1095 
1096   if (tpl_frame[frame_idx].ref_map_index[ref_frame_index] < 0) return;
1097 
1098   const FULLPEL_MV full_mv =
1099       get_fullmv_from_mv(&tpl_stats_ptr->mv[ref_frame_index].as_mv);
1100   const int ref_pos_row = mi_row * MI_SIZE + full_mv.row;
1101   const int ref_pos_col = mi_col * MI_SIZE + full_mv.col;
1102 
1103   const int bw = 4 << mi_size_wide_log2[bsize];
1104   const int bh = 4 << mi_size_high_log2[bsize];
1105   const int mi_height = mi_size_high[bsize];
1106   const int mi_width = mi_size_wide[bsize];
1107   const int pix_num = bw * bh;
1108 
1109   // top-left on grid block location in pixel
1110   int grid_pos_row_base = round_floor(ref_pos_row, bh) * bh;
1111   int grid_pos_col_base = round_floor(ref_pos_col, bw) * bw;
1112   int block;
1113 
1114   int64_t srcrf_dist = is_compound ? tpl_stats_ptr->cmp_recrf_dist[!ref]
1115                                    : tpl_stats_ptr->srcrf_dist;
1116   int64_t srcrf_rate =
1117       is_compound
1118           ? (tpl_stats_ptr->cmp_recrf_rate[!ref] << TPL_DEP_COST_SCALE_LOG2)
1119           : (tpl_stats_ptr->srcrf_rate << TPL_DEP_COST_SCALE_LOG2);
1120 
1121   int64_t cur_dep_dist = tpl_stats_ptr->recrf_dist - srcrf_dist;
1122   int64_t mc_dep_dist =
1123       (int64_t)(tpl_stats_ptr->mc_dep_dist *
1124                 ((double)(tpl_stats_ptr->recrf_dist - srcrf_dist) /
1125                  tpl_stats_ptr->recrf_dist));
1126   int64_t delta_rate =
1127       (tpl_stats_ptr->recrf_rate << TPL_DEP_COST_SCALE_LOG2) - srcrf_rate;
1128   int64_t mc_dep_rate =
1129       av1_delta_rate_cost(tpl_stats_ptr->mc_dep_rate, tpl_stats_ptr->recrf_dist,
1130                           srcrf_dist, pix_num);
1131 
1132   for (block = 0; block < 4; ++block) {
1133     int grid_pos_row = grid_pos_row_base + bh * (block >> 1);
1134     int grid_pos_col = grid_pos_col_base + bw * (block & 0x01);
1135 
1136     if (grid_pos_row >= 0 && grid_pos_row < ref_tpl_frame->mi_rows * MI_SIZE &&
1137         grid_pos_col >= 0 && grid_pos_col < ref_tpl_frame->mi_cols * MI_SIZE) {
1138       int overlap_area = av1_get_overlap_area(grid_pos_row, grid_pos_col,
1139                                               ref_pos_row, ref_pos_col, bw, bh);
1140       int ref_mi_row = round_floor(grid_pos_row, bh) * mi_height;
1141       int ref_mi_col = round_floor(grid_pos_col, bw) * mi_width;
1142       assert((1 << block_mis_log2) == mi_height);
1143       assert((1 << block_mis_log2) == mi_width);
1144       TplDepStats *des_stats = &ref_stats_ptr[av1_tpl_ptr_pos(
1145           ref_mi_row, ref_mi_col, ref_tpl_frame->stride, block_mis_log2)];
1146       des_stats->mc_dep_dist +=
1147           ((cur_dep_dist + mc_dep_dist) * overlap_area) / pix_num;
1148       des_stats->mc_dep_rate +=
1149           ((delta_rate + mc_dep_rate) * overlap_area) / pix_num;
1150     }
1151   }
1152 }
1153 
tpl_model_update(TplParams * const tpl_data,int mi_row,int mi_col,int frame_idx)1154 static AOM_INLINE void tpl_model_update(TplParams *const tpl_data, int mi_row,
1155                                         int mi_col, int frame_idx) {
1156   const BLOCK_SIZE tpl_stats_block_size =
1157       convert_length_to_bsize(MI_SIZE << tpl_data->tpl_stats_block_mis_log2);
1158   tpl_model_update_b(tpl_data, mi_row, mi_col, tpl_stats_block_size, frame_idx,
1159                      0);
1160   tpl_model_update_b(tpl_data, mi_row, mi_col, tpl_stats_block_size, frame_idx,
1161                      1);
1162 }
1163 
tpl_model_store(TplDepStats * tpl_stats_ptr,int mi_row,int mi_col,int stride,const TplDepStats * src_stats,uint8_t block_mis_log2)1164 static AOM_INLINE void tpl_model_store(TplDepStats *tpl_stats_ptr, int mi_row,
1165                                        int mi_col, int stride,
1166                                        const TplDepStats *src_stats,
1167                                        uint8_t block_mis_log2) {
1168   int index = av1_tpl_ptr_pos(mi_row, mi_col, stride, block_mis_log2);
1169   TplDepStats *tpl_ptr = &tpl_stats_ptr[index];
1170   *tpl_ptr = *src_stats;
1171   tpl_ptr->intra_cost = AOMMAX(1, tpl_ptr->intra_cost);
1172   tpl_ptr->inter_cost = AOMMAX(1, tpl_ptr->inter_cost);
1173   tpl_ptr->srcrf_dist = AOMMAX(1, tpl_ptr->srcrf_dist);
1174   tpl_ptr->srcrf_sse = AOMMAX(1, tpl_ptr->srcrf_sse);
1175   tpl_ptr->recrf_dist = AOMMAX(1, tpl_ptr->recrf_dist);
1176   tpl_ptr->srcrf_rate = AOMMAX(1, tpl_ptr->srcrf_rate);
1177   tpl_ptr->recrf_rate = AOMMAX(1, tpl_ptr->recrf_rate);
1178   tpl_ptr->cmp_recrf_dist[0] = AOMMAX(1, tpl_ptr->cmp_recrf_dist[0]);
1179   tpl_ptr->cmp_recrf_dist[1] = AOMMAX(1, tpl_ptr->cmp_recrf_dist[1]);
1180   tpl_ptr->cmp_recrf_rate[0] = AOMMAX(1, tpl_ptr->cmp_recrf_rate[0]);
1181   tpl_ptr->cmp_recrf_rate[1] = AOMMAX(1, tpl_ptr->cmp_recrf_rate[1]);
1182 }
1183 
1184 // Reset the ref and source frame pointers of tpl_data.
tpl_reset_src_ref_frames(TplParams * tpl_data)1185 static AOM_INLINE void tpl_reset_src_ref_frames(TplParams *tpl_data) {
1186   for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1187     tpl_data->ref_frame[i] = NULL;
1188     tpl_data->src_ref_frame[i] = NULL;
1189   }
1190 }
1191 
get_gop_length(const GF_GROUP * gf_group)1192 static AOM_INLINE int get_gop_length(const GF_GROUP *gf_group) {
1193   int gop_length = AOMMIN(gf_group->size, MAX_TPL_FRAME_IDX - 1);
1194   return gop_length;
1195 }
1196 
1197 // Initialize the mc_flow parameters used in computing tpl data.
init_mc_flow_dispenser(AV1_COMP * cpi,int frame_idx,int pframe_qindex)1198 static AOM_INLINE void init_mc_flow_dispenser(AV1_COMP *cpi, int frame_idx,
1199                                               int pframe_qindex) {
1200   TplParams *const tpl_data = &cpi->ppi->tpl_data;
1201   TplDepFrame *tpl_frame = &tpl_data->tpl_frame[frame_idx];
1202   const YV12_BUFFER_CONFIG *this_frame = tpl_frame->gf_picture;
1203   const YV12_BUFFER_CONFIG *ref_frames_ordered[INTER_REFS_PER_FRAME];
1204   uint32_t ref_frame_display_indices[INTER_REFS_PER_FRAME];
1205   const GF_GROUP *gf_group = &cpi->ppi->gf_group;
1206   int ref_pruning_enabled = is_frame_eligible_for_ref_pruning(
1207       gf_group, cpi->sf.inter_sf.selective_ref_frame,
1208       cpi->sf.tpl_sf.prune_ref_frames_in_tpl, frame_idx);
1209   int gop_length = get_gop_length(gf_group);
1210   int ref_frame_flags;
1211   AV1_COMMON *cm = &cpi->common;
1212   int rdmult, idx;
1213   ThreadData *td = &cpi->td;
1214   MACROBLOCK *x = &td->mb;
1215   MACROBLOCKD *xd = &x->e_mbd;
1216   TplTxfmStats *tpl_txfm_stats = &td->tpl_txfm_stats;
1217   tpl_data->frame_idx = frame_idx;
1218   tpl_reset_src_ref_frames(tpl_data);
1219   av1_tile_init(&xd->tile, cm, 0, 0);
1220 
1221   const int boost_index = AOMMIN(15, (cpi->ppi->p_rc.gfu_boost / 100));
1222   const int layer_depth = AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], 6);
1223   const FRAME_TYPE frame_type = cm->current_frame.frame_type;
1224 
1225   // Setup scaling factor
1226   av1_setup_scale_factors_for_frame(
1227       &tpl_data->sf, this_frame->y_crop_width, this_frame->y_crop_height,
1228       this_frame->y_crop_width, this_frame->y_crop_height);
1229 
1230   xd->cur_buf = this_frame;
1231 
1232   for (idx = 0; idx < INTER_REFS_PER_FRAME; ++idx) {
1233     TplDepFrame *tpl_ref_frame =
1234         &tpl_data->tpl_frame[tpl_frame->ref_map_index[idx]];
1235     tpl_data->ref_frame[idx] = tpl_ref_frame->rec_picture;
1236     tpl_data->src_ref_frame[idx] = tpl_ref_frame->gf_picture;
1237     ref_frame_display_indices[idx] = tpl_ref_frame->frame_display_index;
1238   }
1239 
1240   // Store the reference frames based on priority order
1241   for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1242     ref_frames_ordered[i] =
1243         tpl_data->ref_frame[ref_frame_priority_order[i] - 1];
1244   }
1245 
1246   // Work out which reference frame slots may be used.
1247   ref_frame_flags =
1248       get_ref_frame_flags(&cpi->sf, is_one_pass_rt_params(cpi),
1249                           ref_frames_ordered, cpi->ext_flags.ref_frame_flags);
1250 
1251   enforce_max_ref_frames(cpi, &ref_frame_flags, ref_frame_display_indices,
1252                          tpl_frame->frame_display_index);
1253 
1254   // Prune reference frames
1255   for (idx = 0; idx < INTER_REFS_PER_FRAME; ++idx) {
1256     if ((ref_frame_flags & (1 << idx)) == 0) {
1257       tpl_data->ref_frame[idx] = NULL;
1258     }
1259   }
1260 
1261   // Skip motion estimation w.r.t. reference frames which are not
1262   // considered in RD search, using "selective_ref_frame" speed feature.
1263   // The reference frame pruning is not enabled for frames beyond the gop
1264   // length, as there are fewer reference frames and the reference frames
1265   // differ from the frames considered during RD search.
1266   if (ref_pruning_enabled && (frame_idx < gop_length)) {
1267     for (idx = 0; idx < INTER_REFS_PER_FRAME; ++idx) {
1268       const MV_REFERENCE_FRAME refs[2] = { idx + 1, NONE_FRAME };
1269       if (prune_ref_by_selective_ref_frame(cpi, NULL, refs,
1270                                            ref_frame_display_indices)) {
1271         tpl_data->ref_frame[idx] = NULL;
1272       }
1273     }
1274   }
1275 
1276   // Make a temporary mbmi for tpl model
1277   MB_MODE_INFO mbmi;
1278   memset(&mbmi, 0, sizeof(mbmi));
1279   MB_MODE_INFO *mbmi_ptr = &mbmi;
1280   xd->mi = &mbmi_ptr;
1281 
1282   xd->block_ref_scale_factors[0] = &tpl_data->sf;
1283   xd->block_ref_scale_factors[1] = &tpl_data->sf;
1284 
1285   const int base_qindex =
1286       cpi->use_ducky_encode ? gf_group->q_val[frame_idx] : pframe_qindex;
1287   // Get rd multiplier set up.
1288   rdmult = (int)av1_compute_rd_mult(
1289       base_qindex, cm->seq_params->bit_depth,
1290       cpi->ppi->gf_group.update_type[cpi->gf_frame_index], layer_depth,
1291       boost_index, frame_type, cpi->oxcf.q_cfg.use_fixed_qp_offsets,
1292       is_stat_consumption_stage(cpi));
1293 
1294   if (rdmult < 1) rdmult = 1;
1295   av1_set_error_per_bit(&x->errorperbit, rdmult);
1296   av1_set_sad_per_bit(cpi, &x->sadperbit, base_qindex);
1297 
1298   tpl_frame->is_valid = 1;
1299 
1300   cm->quant_params.base_qindex = base_qindex;
1301   av1_frame_init_quantizer(cpi);
1302 
1303   const BitDepthInfo bd_info = get_bit_depth_info(xd);
1304   const FRAME_UPDATE_TYPE update_type =
1305       gf_group->update_type[cpi->gf_frame_index];
1306   tpl_frame->base_rdmult = av1_compute_rd_mult_based_on_qindex(
1307                                bd_info.bit_depth, update_type, base_qindex) /
1308                            6;
1309 
1310   if (cpi->use_ducky_encode)
1311     tpl_frame->base_rdmult = gf_group->rdmult_val[frame_idx];
1312 
1313   av1_init_tpl_txfm_stats(tpl_txfm_stats);
1314 
1315   // Initialize x->mbmi_ext when compound predictions are enabled.
1316   if (cpi->sf.tpl_sf.allow_compound_pred) av1_zero(x->mbmi_ext);
1317 }
1318 
1319 // This function stores the motion estimation dependencies of all the blocks in
1320 // a row
av1_mc_flow_dispenser_row(AV1_COMP * cpi,TplTxfmStats * tpl_txfm_stats,MACROBLOCK * x,int mi_row,BLOCK_SIZE bsize,TX_SIZE tx_size)1321 void av1_mc_flow_dispenser_row(AV1_COMP *cpi, TplTxfmStats *tpl_txfm_stats,
1322                                MACROBLOCK *x, int mi_row, BLOCK_SIZE bsize,
1323                                TX_SIZE tx_size) {
1324   AV1_COMMON *const cm = &cpi->common;
1325   MultiThreadInfo *const mt_info = &cpi->mt_info;
1326   AV1TplRowMultiThreadInfo *const tpl_row_mt = &mt_info->tpl_row_mt;
1327   const CommonModeInfoParams *const mi_params = &cm->mi_params;
1328   const int mi_width = mi_size_wide[bsize];
1329   TplParams *const tpl_data = &cpi->ppi->tpl_data;
1330   TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_data->frame_idx];
1331   MACROBLOCKD *xd = &x->e_mbd;
1332 
1333   const int tplb_cols_in_tile =
1334       ROUND_POWER_OF_TWO(mi_params->mi_cols, mi_size_wide_log2[bsize]);
1335   const int tplb_row = ROUND_POWER_OF_TWO(mi_row, mi_size_high_log2[bsize]);
1336   assert(mi_size_high[bsize] == (1 << tpl_data->tpl_stats_block_mis_log2));
1337   assert(mi_size_wide[bsize] == (1 << tpl_data->tpl_stats_block_mis_log2));
1338 
1339   for (int mi_col = 0, tplb_col_in_tile = 0; mi_col < mi_params->mi_cols;
1340        mi_col += mi_width, tplb_col_in_tile++) {
1341     (*tpl_row_mt->sync_read_ptr)(&tpl_data->tpl_mt_sync, tplb_row,
1342                                  tplb_col_in_tile);
1343     TplDepStats tpl_stats;
1344 
1345     // Motion estimation column boundary
1346     av1_set_mv_col_limits(mi_params, &x->mv_limits, mi_col, mi_width,
1347                           tpl_data->border_in_pixels);
1348     xd->mb_to_left_edge = -GET_MV_SUBPEL(mi_col * MI_SIZE);
1349     xd->mb_to_right_edge =
1350         GET_MV_SUBPEL(mi_params->mi_cols - mi_width - mi_col);
1351     mode_estimation(cpi, tpl_txfm_stats, x, mi_row, mi_col, bsize, tx_size,
1352                     &tpl_stats);
1353 
1354     // Motion flow dependency dispenser.
1355     tpl_model_store(tpl_frame->tpl_stats_ptr, mi_row, mi_col, tpl_frame->stride,
1356                     &tpl_stats, tpl_data->tpl_stats_block_mis_log2);
1357     (*tpl_row_mt->sync_write_ptr)(&tpl_data->tpl_mt_sync, tplb_row,
1358                                   tplb_col_in_tile, tplb_cols_in_tile);
1359   }
1360 }
1361 
mc_flow_dispenser(AV1_COMP * cpi)1362 static AOM_INLINE void mc_flow_dispenser(AV1_COMP *cpi) {
1363   AV1_COMMON *cm = &cpi->common;
1364   const CommonModeInfoParams *const mi_params = &cm->mi_params;
1365   ThreadData *td = &cpi->td;
1366   MACROBLOCK *x = &td->mb;
1367   MACROBLOCKD *xd = &x->e_mbd;
1368   const BLOCK_SIZE bsize =
1369       convert_length_to_bsize(cpi->ppi->tpl_data.tpl_bsize_1d);
1370   const TX_SIZE tx_size = max_txsize_lookup[bsize];
1371   const int mi_height = mi_size_high[bsize];
1372   for (int mi_row = 0; mi_row < mi_params->mi_rows; mi_row += mi_height) {
1373     // Motion estimation row boundary
1374     av1_set_mv_row_limits(mi_params, &x->mv_limits, mi_row, mi_height,
1375                           cpi->ppi->tpl_data.border_in_pixels);
1376     xd->mb_to_top_edge = -GET_MV_SUBPEL(mi_row * MI_SIZE);
1377     xd->mb_to_bottom_edge =
1378         GET_MV_SUBPEL((mi_params->mi_rows - mi_height - mi_row) * MI_SIZE);
1379     av1_mc_flow_dispenser_row(cpi, &td->tpl_txfm_stats, x, mi_row, bsize,
1380                               tx_size);
1381   }
1382 }
1383 
mc_flow_synthesizer(TplParams * tpl_data,int frame_idx,int mi_rows,int mi_cols)1384 static void mc_flow_synthesizer(TplParams *tpl_data, int frame_idx, int mi_rows,
1385                                 int mi_cols) {
1386   if (!frame_idx) {
1387     return;
1388   }
1389   const BLOCK_SIZE bsize = convert_length_to_bsize(tpl_data->tpl_bsize_1d);
1390   const int mi_height = mi_size_high[bsize];
1391   const int mi_width = mi_size_wide[bsize];
1392   assert(mi_height == (1 << tpl_data->tpl_stats_block_mis_log2));
1393   assert(mi_width == (1 << tpl_data->tpl_stats_block_mis_log2));
1394 
1395   for (int mi_row = 0; mi_row < mi_rows; mi_row += mi_height) {
1396     for (int mi_col = 0; mi_col < mi_cols; mi_col += mi_width) {
1397       tpl_model_update(tpl_data, mi_row, mi_col, frame_idx);
1398     }
1399   }
1400 }
1401 
init_gop_frames_for_tpl(AV1_COMP * cpi,const EncodeFrameParams * const init_frame_params,GF_GROUP * gf_group,int * tpl_group_frames,int * pframe_qindex)1402 static AOM_INLINE void init_gop_frames_for_tpl(
1403     AV1_COMP *cpi, const EncodeFrameParams *const init_frame_params,
1404     GF_GROUP *gf_group, int *tpl_group_frames, int *pframe_qindex) {
1405   AV1_COMMON *cm = &cpi->common;
1406   assert(cpi->gf_frame_index == 0);
1407   *pframe_qindex = 0;
1408 
1409   RefFrameMapPair ref_frame_map_pairs[REF_FRAMES];
1410   init_ref_map_pair(cpi, ref_frame_map_pairs);
1411 
1412   int remapped_ref_idx[REF_FRAMES];
1413 
1414   EncodeFrameParams frame_params = *init_frame_params;
1415   TplParams *const tpl_data = &cpi->ppi->tpl_data;
1416 
1417   int ref_picture_map[REF_FRAMES];
1418 
1419   for (int i = 0; i < REF_FRAMES; ++i) {
1420     if (frame_params.frame_type == KEY_FRAME) {
1421       tpl_data->tpl_frame[-i - 1].gf_picture = NULL;
1422       tpl_data->tpl_frame[-i - 1].rec_picture = NULL;
1423       tpl_data->tpl_frame[-i - 1].frame_display_index = 0;
1424     } else {
1425       tpl_data->tpl_frame[-i - 1].gf_picture = &cm->ref_frame_map[i]->buf;
1426       tpl_data->tpl_frame[-i - 1].rec_picture = &cm->ref_frame_map[i]->buf;
1427       tpl_data->tpl_frame[-i - 1].frame_display_index =
1428           cm->ref_frame_map[i]->display_order_hint;
1429     }
1430 
1431     ref_picture_map[i] = -i - 1;
1432   }
1433 
1434   *tpl_group_frames = 0;
1435 
1436   int gf_index;
1437   int process_frame_count = 0;
1438   const int gop_length = get_gop_length(gf_group);
1439 
1440   for (gf_index = 0; gf_index < gop_length; ++gf_index) {
1441     TplDepFrame *tpl_frame = &tpl_data->tpl_frame[gf_index];
1442     FRAME_UPDATE_TYPE frame_update_type = gf_group->update_type[gf_index];
1443     int lookahead_index =
1444         gf_group->cur_frame_idx[gf_index] + gf_group->arf_src_offset[gf_index];
1445     frame_params.show_frame = frame_update_type != ARF_UPDATE &&
1446                               frame_update_type != INTNL_ARF_UPDATE;
1447     frame_params.show_existing_frame =
1448         frame_update_type == INTNL_OVERLAY_UPDATE ||
1449         frame_update_type == OVERLAY_UPDATE;
1450     frame_params.frame_type = gf_group->frame_type[gf_index];
1451 
1452     if (frame_update_type == LF_UPDATE)
1453       *pframe_qindex = gf_group->q_val[gf_index];
1454 
1455     const struct lookahead_entry *buf = av1_lookahead_peek(
1456         cpi->ppi->lookahead, lookahead_index, cpi->compressor_stage);
1457     if (buf == NULL) break;
1458     tpl_frame->gf_picture = &buf->img;
1459 
1460     // Use filtered frame buffer if available. This will make tpl stats more
1461     // precise.
1462     FRAME_DIFF frame_diff;
1463     const YV12_BUFFER_CONFIG *tf_buf =
1464         av1_tf_info_get_filtered_buf(&cpi->ppi->tf_info, gf_index, &frame_diff);
1465     if (tf_buf != NULL) {
1466       tpl_frame->gf_picture = tf_buf;
1467     }
1468 
1469     // 'cm->current_frame.frame_number' is the display number
1470     // of the current frame.
1471     // 'lookahead_index' is frame offset within the gf group.
1472     // 'lookahead_index + cm->current_frame.frame_number'
1473     // is the display index of the frame.
1474     tpl_frame->frame_display_index =
1475         lookahead_index + cm->current_frame.frame_number;
1476     assert(buf->display_idx ==
1477            cpi->frame_index_set.show_frame_count + lookahead_index);
1478 
1479     if (frame_update_type != OVERLAY_UPDATE &&
1480         frame_update_type != INTNL_OVERLAY_UPDATE) {
1481       tpl_frame->rec_picture = &tpl_data->tpl_rec_pool[process_frame_count];
1482       tpl_frame->tpl_stats_ptr = tpl_data->tpl_stats_pool[process_frame_count];
1483       ++process_frame_count;
1484     }
1485     const int true_disp = (int)(tpl_frame->frame_display_index);
1486 
1487     av1_get_ref_frames(ref_frame_map_pairs, true_disp, cpi, gf_index, 0,
1488                        remapped_ref_idx);
1489 
1490     int refresh_mask =
1491         av1_get_refresh_frame_flags(cpi, &frame_params, frame_update_type,
1492                                     gf_index, true_disp, ref_frame_map_pairs);
1493 
1494     // Make the frames marked as is_frame_non_ref to non-reference frames.
1495     if (cpi->ppi->gf_group.is_frame_non_ref[gf_index]) refresh_mask = 0;
1496 
1497     int refresh_frame_map_index = av1_get_refresh_ref_frame_map(refresh_mask);
1498 
1499     if (refresh_frame_map_index < REF_FRAMES &&
1500         refresh_frame_map_index != INVALID_IDX) {
1501       ref_frame_map_pairs[refresh_frame_map_index].disp_order =
1502           AOMMAX(0, true_disp);
1503       ref_frame_map_pairs[refresh_frame_map_index].pyr_level =
1504           get_true_pyr_level(gf_group->layer_depth[gf_index], true_disp,
1505                              cpi->ppi->gf_group.max_layer_depth);
1506     }
1507 
1508     for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i)
1509       tpl_frame->ref_map_index[i - LAST_FRAME] =
1510           ref_picture_map[remapped_ref_idx[i - LAST_FRAME]];
1511 
1512     if (refresh_mask) ref_picture_map[refresh_frame_map_index] = gf_index;
1513 
1514     ++*tpl_group_frames;
1515   }
1516 
1517   const int tpl_extend = cpi->oxcf.gf_cfg.lag_in_frames - MAX_GF_INTERVAL;
1518   int extend_frame_count = 0;
1519   int extend_frame_length = AOMMIN(
1520       tpl_extend, cpi->rc.frames_to_key - cpi->ppi->p_rc.baseline_gf_interval);
1521 
1522   int frame_display_index = gf_group->cur_frame_idx[gop_length - 1] +
1523                             gf_group->arf_src_offset[gop_length - 1] + 1;
1524 
1525   for (;
1526        gf_index < MAX_TPL_FRAME_IDX && extend_frame_count < extend_frame_length;
1527        ++gf_index) {
1528     TplDepFrame *tpl_frame = &tpl_data->tpl_frame[gf_index];
1529     FRAME_UPDATE_TYPE frame_update_type = LF_UPDATE;
1530     frame_params.show_frame = frame_update_type != ARF_UPDATE &&
1531                               frame_update_type != INTNL_ARF_UPDATE;
1532     frame_params.show_existing_frame =
1533         frame_update_type == INTNL_OVERLAY_UPDATE;
1534     frame_params.frame_type = INTER_FRAME;
1535 
1536     int lookahead_index = frame_display_index;
1537     struct lookahead_entry *buf = av1_lookahead_peek(
1538         cpi->ppi->lookahead, lookahead_index, cpi->compressor_stage);
1539 
1540     if (buf == NULL) break;
1541 
1542     tpl_frame->gf_picture = &buf->img;
1543     tpl_frame->rec_picture = &tpl_data->tpl_rec_pool[process_frame_count];
1544     tpl_frame->tpl_stats_ptr = tpl_data->tpl_stats_pool[process_frame_count];
1545     // 'cm->current_frame.frame_number' is the display number
1546     // of the current frame.
1547     // 'frame_display_index' is frame offset within the gf group.
1548     // 'frame_display_index + cm->current_frame.frame_number'
1549     // is the display index of the frame.
1550     tpl_frame->frame_display_index =
1551         frame_display_index + cm->current_frame.frame_number;
1552 
1553     ++process_frame_count;
1554 
1555     gf_group->update_type[gf_index] = LF_UPDATE;
1556 
1557 #if CONFIG_BITRATE_ACCURACY && CONFIG_THREE_PASS
1558     if (cpi->oxcf.pass == AOM_RC_SECOND_PASS) {
1559       if (cpi->oxcf.rc_cfg.mode == AOM_Q) {
1560         *pframe_qindex = cpi->oxcf.rc_cfg.cq_level;
1561       } else if (cpi->oxcf.rc_cfg.mode == AOM_VBR) {
1562         // TODO(angiebird): Find a more adaptive method to decide pframe_qindex
1563         // override the pframe_qindex in the second pass when bitrate accuracy
1564         // is on. We found that setting this pframe_qindex make the tpl stats
1565         // more stable.
1566         *pframe_qindex = 128;
1567       }
1568     }
1569 #endif  // CONFIG_BITRATE_ACCURACY && CONFIG_THREE_PASS
1570     gf_group->q_val[gf_index] = *pframe_qindex;
1571     const int true_disp = (int)(tpl_frame->frame_display_index);
1572     av1_get_ref_frames(ref_frame_map_pairs, true_disp, cpi, gf_index, 0,
1573                        remapped_ref_idx);
1574     int refresh_mask =
1575         av1_get_refresh_frame_flags(cpi, &frame_params, frame_update_type,
1576                                     gf_index, true_disp, ref_frame_map_pairs);
1577     int refresh_frame_map_index = av1_get_refresh_ref_frame_map(refresh_mask);
1578 
1579     if (refresh_frame_map_index < REF_FRAMES &&
1580         refresh_frame_map_index != INVALID_IDX) {
1581       ref_frame_map_pairs[refresh_frame_map_index].disp_order =
1582           AOMMAX(0, true_disp);
1583       ref_frame_map_pairs[refresh_frame_map_index].pyr_level =
1584           get_true_pyr_level(gf_group->layer_depth[gf_index], true_disp,
1585                              cpi->ppi->gf_group.max_layer_depth);
1586     }
1587 
1588     for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i)
1589       tpl_frame->ref_map_index[i - LAST_FRAME] =
1590           ref_picture_map[remapped_ref_idx[i - LAST_FRAME]];
1591 
1592     tpl_frame->ref_map_index[ALTREF_FRAME - LAST_FRAME] = -1;
1593     tpl_frame->ref_map_index[LAST3_FRAME - LAST_FRAME] = -1;
1594     tpl_frame->ref_map_index[BWDREF_FRAME - LAST_FRAME] = -1;
1595     tpl_frame->ref_map_index[ALTREF2_FRAME - LAST_FRAME] = -1;
1596 
1597     if (refresh_mask) ref_picture_map[refresh_frame_map_index] = gf_index;
1598 
1599     ++*tpl_group_frames;
1600     ++extend_frame_count;
1601     ++frame_display_index;
1602   }
1603 }
1604 
av1_init_tpl_stats(TplParams * const tpl_data)1605 void av1_init_tpl_stats(TplParams *const tpl_data) {
1606   tpl_data->ready = 0;
1607   set_tpl_stats_block_size(&tpl_data->tpl_stats_block_mis_log2,
1608                            &tpl_data->tpl_bsize_1d);
1609   for (int frame_idx = 0; frame_idx < MAX_LENGTH_TPL_FRAME_STATS; ++frame_idx) {
1610     TplDepFrame *tpl_frame = &tpl_data->tpl_stats_buffer[frame_idx];
1611     tpl_frame->is_valid = 0;
1612   }
1613   for (int frame_idx = 0; frame_idx < MAX_LAG_BUFFERS; ++frame_idx) {
1614     TplDepFrame *tpl_frame = &tpl_data->tpl_stats_buffer[frame_idx];
1615     if (tpl_data->tpl_stats_pool[frame_idx] == NULL) continue;
1616     memset(tpl_data->tpl_stats_pool[frame_idx], 0,
1617            tpl_frame->height * tpl_frame->width *
1618                sizeof(*tpl_frame->tpl_stats_ptr));
1619   }
1620 }
1621 
av1_tpl_stats_ready(const TplParams * tpl_data,int gf_frame_index)1622 int av1_tpl_stats_ready(const TplParams *tpl_data, int gf_frame_index) {
1623   if (tpl_data->ready == 0) {
1624     return 0;
1625   }
1626   if (gf_frame_index >= MAX_TPL_FRAME_IDX) {
1627     // The sub-GOP length exceeds the TPL buffer capacity.
1628     // Hence the TPL related functions are disabled hereafter.
1629     return 0;
1630   }
1631   return tpl_data->tpl_frame[gf_frame_index].is_valid;
1632 }
1633 
eval_gop_length(double * beta,int gop_eval)1634 static AOM_INLINE int eval_gop_length(double *beta, int gop_eval) {
1635   switch (gop_eval) {
1636     case 1:
1637       // Allow larger GOP size if the base layer ARF has higher dependency
1638       // factor than the intermediate ARF and both ARFs have reasonably high
1639       // dependency factors.
1640       return (beta[0] >= beta[1] + 0.7) && beta[0] > 3.0;
1641     case 2:
1642       if ((beta[0] >= beta[1] + 0.4) && beta[0] > 1.6)
1643         return 1;  // Don't shorten the gf interval
1644       else if ((beta[0] < beta[1] + 0.1) || beta[0] <= 1.4)
1645         return 0;  // Shorten the gf interval
1646       else
1647         return 2;  // Cannot decide the gf interval, so redo the
1648                    // tpl stats calculation.
1649     case 3: return beta[0] > 1.1;
1650     default: return 2;
1651   }
1652 }
1653 
1654 // TODO(jingning): Restructure av1_rc_pick_q_and_bounds() to narrow down
1655 // the scope of input arguments.
av1_tpl_preload_rc_estimate(AV1_COMP * cpi,const EncodeFrameParams * const frame_params)1656 void av1_tpl_preload_rc_estimate(AV1_COMP *cpi,
1657                                  const EncodeFrameParams *const frame_params) {
1658   AV1_COMMON *cm = &cpi->common;
1659   GF_GROUP *gf_group = &cpi->ppi->gf_group;
1660   int bottom_index, top_index;
1661   if (cpi->use_ducky_encode) return;
1662 
1663   cm->current_frame.frame_type = frame_params->frame_type;
1664   for (int gf_index = cpi->gf_frame_index; gf_index < gf_group->size;
1665        ++gf_index) {
1666     cm->current_frame.frame_type = gf_group->frame_type[gf_index];
1667     cm->show_frame = gf_group->update_type[gf_index] != ARF_UPDATE &&
1668                      gf_group->update_type[gf_index] != INTNL_ARF_UPDATE;
1669     gf_group->q_val[gf_index] = av1_rc_pick_q_and_bounds(
1670         cpi, cm->width, cm->height, gf_index, &bottom_index, &top_index);
1671   }
1672 }
1673 
av1_tpl_setup_stats(AV1_COMP * cpi,int gop_eval,const EncodeFrameParams * const frame_params)1674 int av1_tpl_setup_stats(AV1_COMP *cpi, int gop_eval,
1675                         const EncodeFrameParams *const frame_params) {
1676 #if CONFIG_COLLECT_COMPONENT_TIMING
1677   start_timing(cpi, av1_tpl_setup_stats_time);
1678 #endif
1679   assert(cpi->gf_frame_index == 0);
1680   AV1_COMMON *cm = &cpi->common;
1681   MultiThreadInfo *const mt_info = &cpi->mt_info;
1682   AV1TplRowMultiThreadInfo *const tpl_row_mt = &mt_info->tpl_row_mt;
1683   GF_GROUP *gf_group = &cpi->ppi->gf_group;
1684   EncodeFrameParams this_frame_params = *frame_params;
1685   TplParams *const tpl_data = &cpi->ppi->tpl_data;
1686   int approx_gop_eval = (gop_eval > 1);
1687   int num_arf_layers = MAX_ARF_LAYERS;
1688 
1689   // When gop_eval is set to 2, tpl stats calculation is done for ARFs from base
1690   // layer, (base+1) layer and (base+2) layer. When gop_eval is set to 3,
1691   // tpl stats calculation is limited to ARFs from base layer and (base+1)
1692   // layer.
1693   if (approx_gop_eval) num_arf_layers = (gop_eval == 2) ? 3 : 2;
1694 
1695   if (cpi->superres_mode != AOM_SUPERRES_NONE) {
1696     assert(cpi->superres_mode != AOM_SUPERRES_AUTO);
1697     av1_init_tpl_stats(tpl_data);
1698     return 0;
1699   }
1700 
1701   cm->current_frame.frame_type = frame_params->frame_type;
1702   for (int gf_index = cpi->gf_frame_index; gf_index < gf_group->size;
1703        ++gf_index) {
1704     cm->current_frame.frame_type = gf_group->frame_type[gf_index];
1705     av1_configure_buffer_updates(cpi, &this_frame_params.refresh_frame,
1706                                  gf_group->update_type[gf_index],
1707                                  gf_group->refbuf_state[gf_index], 0);
1708 
1709     memcpy(&cpi->refresh_frame, &this_frame_params.refresh_frame,
1710            sizeof(cpi->refresh_frame));
1711   }
1712 
1713   int pframe_qindex;
1714   int tpl_gf_group_frames;
1715   init_gop_frames_for_tpl(cpi, frame_params, gf_group, &tpl_gf_group_frames,
1716                           &pframe_qindex);
1717 
1718   cpi->ppi->p_rc.base_layer_qp = pframe_qindex;
1719 
1720   av1_init_tpl_stats(tpl_data);
1721 
1722   tpl_row_mt->sync_read_ptr = av1_tpl_row_mt_sync_read_dummy;
1723   tpl_row_mt->sync_write_ptr = av1_tpl_row_mt_sync_write_dummy;
1724 
1725   av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
1726                                     cm->width, cm->height);
1727 
1728   if (frame_params->frame_type == KEY_FRAME) {
1729     av1_init_mv_probs(cm);
1730   }
1731   av1_fill_mv_costs(&cm->fc->nmvc, cm->features.cur_frame_force_integer_mv,
1732                     cm->features.allow_high_precision_mv, cpi->td.mb.mv_costs);
1733 
1734   const int gop_length = get_gop_length(gf_group);
1735   const int num_planes =
1736       cpi->sf.tpl_sf.use_y_only_rate_distortion ? 1 : av1_num_planes(cm);
1737   // Backward propagation from tpl_group_frames to 1.
1738   for (int frame_idx = cpi->gf_frame_index; frame_idx < tpl_gf_group_frames;
1739        ++frame_idx) {
1740     if (gf_group->update_type[frame_idx] == INTNL_OVERLAY_UPDATE ||
1741         gf_group->update_type[frame_idx] == OVERLAY_UPDATE)
1742       continue;
1743 
1744     // When approx_gop_eval = 1, skip tpl stats calculation for higher layer
1745     // frames and for frames beyond gop length.
1746     if (approx_gop_eval && (gf_group->layer_depth[frame_idx] > num_arf_layers ||
1747                             frame_idx >= gop_length))
1748       continue;
1749 
1750     init_mc_flow_dispenser(cpi, frame_idx, pframe_qindex);
1751     if (mt_info->num_workers > 1) {
1752       tpl_row_mt->sync_read_ptr = av1_tpl_row_mt_sync_read;
1753       tpl_row_mt->sync_write_ptr = av1_tpl_row_mt_sync_write;
1754       av1_mc_flow_dispenser_mt(cpi);
1755     } else {
1756       mc_flow_dispenser(cpi);
1757     }
1758     av1_tpl_txfm_stats_update_abs_coeff_mean(&cpi->td.tpl_txfm_stats);
1759     av1_tpl_store_txfm_stats(tpl_data, &cpi->td.tpl_txfm_stats, frame_idx);
1760 #if CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY
1761     if (cpi->oxcf.pass == AOM_RC_THIRD_PASS) {
1762       int frame_coding_idx =
1763           av1_vbr_rc_frame_coding_idx(&cpi->vbr_rc_info, frame_idx);
1764       rc_log_frame_stats(&cpi->rc_log, frame_coding_idx,
1765                          &cpi->td.tpl_txfm_stats);
1766     }
1767 #endif  // CONFIG_RATECTRL_LOG
1768 
1769     aom_extend_frame_borders(tpl_data->tpl_frame[frame_idx].rec_picture,
1770                              num_planes);
1771   }
1772 
1773   for (int frame_idx = tpl_gf_group_frames - 1;
1774        frame_idx >= cpi->gf_frame_index; --frame_idx) {
1775     if (gf_group->update_type[frame_idx] == INTNL_OVERLAY_UPDATE ||
1776         gf_group->update_type[frame_idx] == OVERLAY_UPDATE)
1777       continue;
1778 
1779     if (approx_gop_eval && (gf_group->layer_depth[frame_idx] > num_arf_layers ||
1780                             frame_idx >= gop_length))
1781       continue;
1782 
1783     mc_flow_synthesizer(tpl_data, frame_idx, cm->mi_params.mi_rows,
1784                         cm->mi_params.mi_cols);
1785   }
1786 
1787   av1_configure_buffer_updates(cpi, &this_frame_params.refresh_frame,
1788                                gf_group->update_type[cpi->gf_frame_index],
1789                                gf_group->update_type[cpi->gf_frame_index], 0);
1790   cm->current_frame.frame_type = frame_params->frame_type;
1791   cm->show_frame = frame_params->show_frame;
1792 
1793 #if CONFIG_COLLECT_COMPONENT_TIMING
1794   // Record the time if the function returns.
1795   if (cpi->common.tiles.large_scale || gf_group->max_layer_depth_allowed == 0 ||
1796       !gop_eval)
1797     end_timing(cpi, av1_tpl_setup_stats_time);
1798 #endif
1799 
1800   if (!approx_gop_eval) {
1801     tpl_data->ready = 1;
1802   }
1803   if (cpi->common.tiles.large_scale) return 0;
1804   if (gf_group->max_layer_depth_allowed == 0) return 1;
1805   if (!gop_eval) return 0;
1806   assert(gf_group->arf_index >= 0);
1807 
1808   double beta[2] = { 0.0 };
1809   const int frame_idx_0 = gf_group->arf_index;
1810   const int frame_idx_1 =
1811       AOMMIN(tpl_gf_group_frames - 1, gf_group->arf_index + 1);
1812   beta[0] = av1_tpl_get_frame_importance(tpl_data, frame_idx_0);
1813   beta[1] = av1_tpl_get_frame_importance(tpl_data, frame_idx_1);
1814 #if CONFIG_COLLECT_COMPONENT_TIMING
1815   end_timing(cpi, av1_tpl_setup_stats_time);
1816 #endif
1817   return eval_gop_length(beta, gop_eval);
1818 }
1819 
av1_tpl_rdmult_setup(AV1_COMP * cpi)1820 void av1_tpl_rdmult_setup(AV1_COMP *cpi) {
1821   const AV1_COMMON *const cm = &cpi->common;
1822   const int tpl_idx = cpi->gf_frame_index;
1823 
1824   assert(
1825       IMPLIES(cpi->ppi->gf_group.size > 0, tpl_idx < cpi->ppi->gf_group.size));
1826 
1827   TplParams *const tpl_data = &cpi->ppi->tpl_data;
1828   const TplDepFrame *const tpl_frame = &tpl_data->tpl_frame[tpl_idx];
1829 
1830   if (!tpl_frame->is_valid) return;
1831 
1832   const TplDepStats *const tpl_stats = tpl_frame->tpl_stats_ptr;
1833   const int tpl_stride = tpl_frame->stride;
1834   const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
1835 
1836   const int block_size = BLOCK_16X16;
1837   const int num_mi_w = mi_size_wide[block_size];
1838   const int num_mi_h = mi_size_high[block_size];
1839   const int num_cols = (mi_cols_sr + num_mi_w - 1) / num_mi_w;
1840   const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h;
1841   const double c = 1.2;
1842   const int step = 1 << tpl_data->tpl_stats_block_mis_log2;
1843 
1844   // Loop through each 'block_size' X 'block_size' block.
1845   for (int row = 0; row < num_rows; row++) {
1846     for (int col = 0; col < num_cols; col++) {
1847       double intra_cost = 0.0, mc_dep_cost = 0.0;
1848       // Loop through each mi block.
1849       for (int mi_row = row * num_mi_h; mi_row < (row + 1) * num_mi_h;
1850            mi_row += step) {
1851         for (int mi_col = col * num_mi_w; mi_col < (col + 1) * num_mi_w;
1852              mi_col += step) {
1853           if (mi_row >= cm->mi_params.mi_rows || mi_col >= mi_cols_sr) continue;
1854           const TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
1855               mi_row, mi_col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
1856           int64_t mc_dep_delta =
1857               RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
1858                      this_stats->mc_dep_dist);
1859           intra_cost += (double)(this_stats->recrf_dist << RDDIV_BITS);
1860           mc_dep_cost +=
1861               (double)(this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
1862         }
1863       }
1864       const double rk = intra_cost / mc_dep_cost;
1865       const int index = row * num_cols + col;
1866       cpi->tpl_rdmult_scaling_factors[index] = rk / cpi->rd.r0 + c;
1867     }
1868   }
1869 }
1870 
av1_tpl_rdmult_setup_sb(AV1_COMP * cpi,MACROBLOCK * const x,BLOCK_SIZE sb_size,int mi_row,int mi_col)1871 void av1_tpl_rdmult_setup_sb(AV1_COMP *cpi, MACROBLOCK *const x,
1872                              BLOCK_SIZE sb_size, int mi_row, int mi_col) {
1873   AV1_COMMON *const cm = &cpi->common;
1874   GF_GROUP *gf_group = &cpi->ppi->gf_group;
1875   assert(IMPLIES(cpi->ppi->gf_group.size > 0,
1876                  cpi->gf_frame_index < cpi->ppi->gf_group.size));
1877   const int tpl_idx = cpi->gf_frame_index;
1878 
1879   const int boost_index = AOMMIN(15, (cpi->ppi->p_rc.gfu_boost / 100));
1880   const int layer_depth = AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], 6);
1881   const FRAME_TYPE frame_type = cm->current_frame.frame_type;
1882 
1883   if (tpl_idx >= MAX_TPL_FRAME_IDX) return;
1884   TplDepFrame *tpl_frame = &cpi->ppi->tpl_data.tpl_frame[tpl_idx];
1885   if (!tpl_frame->is_valid) return;
1886   if (!is_frame_tpl_eligible(gf_group, cpi->gf_frame_index)) return;
1887   if (cpi->oxcf.q_cfg.aq_mode != NO_AQ) return;
1888 
1889   const int mi_col_sr =
1890       coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
1891   const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
1892   const int sb_mi_width_sr = coded_to_superres_mi(
1893       mi_size_wide[sb_size], cm->superres_scale_denominator);
1894 
1895   const int bsize_base = BLOCK_16X16;
1896   const int num_mi_w = mi_size_wide[bsize_base];
1897   const int num_mi_h = mi_size_high[bsize_base];
1898   const int num_cols = (mi_cols_sr + num_mi_w - 1) / num_mi_w;
1899   const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h;
1900   const int num_bcols = (sb_mi_width_sr + num_mi_w - 1) / num_mi_w;
1901   const int num_brows = (mi_size_high[sb_size] + num_mi_h - 1) / num_mi_h;
1902   int row, col;
1903 
1904   double base_block_count = 0.0;
1905   double log_sum = 0.0;
1906 
1907   for (row = mi_row / num_mi_w;
1908        row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
1909     for (col = mi_col_sr / num_mi_h;
1910          col < num_cols && col < mi_col_sr / num_mi_h + num_bcols; ++col) {
1911       const int index = row * num_cols + col;
1912       log_sum += log(cpi->tpl_rdmult_scaling_factors[index]);
1913       base_block_count += 1.0;
1914     }
1915   }
1916 
1917   const CommonQuantParams *quant_params = &cm->quant_params;
1918 
1919   const int orig_qindex_rdmult =
1920       quant_params->base_qindex + quant_params->y_dc_delta_q;
1921   const int orig_rdmult = av1_compute_rd_mult(
1922       orig_qindex_rdmult, cm->seq_params->bit_depth,
1923       cpi->ppi->gf_group.update_type[cpi->gf_frame_index], layer_depth,
1924       boost_index, frame_type, cpi->oxcf.q_cfg.use_fixed_qp_offsets,
1925       is_stat_consumption_stage(cpi));
1926 
1927   const int new_qindex_rdmult = quant_params->base_qindex +
1928                                 x->rdmult_delta_qindex +
1929                                 quant_params->y_dc_delta_q;
1930   const int new_rdmult = av1_compute_rd_mult(
1931       new_qindex_rdmult, cm->seq_params->bit_depth,
1932       cpi->ppi->gf_group.update_type[cpi->gf_frame_index], layer_depth,
1933       boost_index, frame_type, cpi->oxcf.q_cfg.use_fixed_qp_offsets,
1934       is_stat_consumption_stage(cpi));
1935 
1936   const double scaling_factor = (double)new_rdmult / (double)orig_rdmult;
1937 
1938   double scale_adj = log(scaling_factor) - log_sum / base_block_count;
1939   scale_adj = exp_bounded(scale_adj);
1940 
1941   for (row = mi_row / num_mi_w;
1942        row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
1943     for (col = mi_col_sr / num_mi_h;
1944          col < num_cols && col < mi_col_sr / num_mi_h + num_bcols; ++col) {
1945       const int index = row * num_cols + col;
1946       cpi->ppi->tpl_sb_rdmult_scaling_factors[index] =
1947           scale_adj * cpi->tpl_rdmult_scaling_factors[index];
1948     }
1949   }
1950 }
1951 
av1_exponential_entropy(double q_step,double b)1952 double av1_exponential_entropy(double q_step, double b) {
1953   b = AOMMAX(b, TPL_EPSILON);
1954   double z = fmax(exp_bounded(-q_step / b), TPL_EPSILON);
1955   return -log2(1 - z) - z * log2(z) / (1 - z);
1956 }
1957 
av1_laplace_entropy(double q_step,double b,double zero_bin_ratio)1958 double av1_laplace_entropy(double q_step, double b, double zero_bin_ratio) {
1959   // zero bin's size is zero_bin_ratio * q_step
1960   // non-zero bin's size is q_step
1961   b = AOMMAX(b, TPL_EPSILON);
1962   double z = fmax(exp_bounded(-zero_bin_ratio / 2 * q_step / b), TPL_EPSILON);
1963   double h = av1_exponential_entropy(q_step, b);
1964   double r = -(1 - z) * log2(1 - z) - z * log2(z) + z * (h + 1);
1965   return r;
1966 }
1967 
av1_laplace_estimate_frame_rate(int q_index,int block_count,const double * abs_coeff_mean,int coeff_num)1968 double av1_laplace_estimate_frame_rate(int q_index, int block_count,
1969                                        const double *abs_coeff_mean,
1970                                        int coeff_num) {
1971   double zero_bin_ratio = 2;
1972   double dc_q_step = av1_dc_quant_QTX(q_index, 0, AOM_BITS_8) / 4.;
1973   double ac_q_step = av1_ac_quant_QTX(q_index, 0, AOM_BITS_8) / 4.;
1974   double est_rate = 0;
1975   // dc coeff
1976   est_rate += av1_laplace_entropy(dc_q_step, abs_coeff_mean[0], zero_bin_ratio);
1977   // ac coeff
1978   for (int i = 1; i < coeff_num; ++i) {
1979     est_rate +=
1980         av1_laplace_entropy(ac_q_step, abs_coeff_mean[i], zero_bin_ratio);
1981   }
1982   est_rate *= block_count;
1983   return est_rate;
1984 }
1985 
av1_estimate_coeff_entropy(double q_step,double b,double zero_bin_ratio,int qcoeff)1986 double av1_estimate_coeff_entropy(double q_step, double b,
1987                                   double zero_bin_ratio, int qcoeff) {
1988   b = AOMMAX(b, TPL_EPSILON);
1989   int abs_qcoeff = abs(qcoeff);
1990   double z0 = fmax(exp_bounded(-zero_bin_ratio / 2 * q_step / b), TPL_EPSILON);
1991   if (abs_qcoeff == 0) {
1992     double r = -log2(1 - z0);
1993     return r;
1994   } else {
1995     double z = fmax(exp_bounded(-q_step / b), TPL_EPSILON);
1996     double r = 1 - log2(z0) - log2(1 - z) - (abs_qcoeff - 1) * log2(z);
1997     return r;
1998   }
1999 }
2000 
av1_estimate_txfm_block_entropy(int q_index,const double * abs_coeff_mean,int * qcoeff_arr,int coeff_num)2001 double av1_estimate_txfm_block_entropy(int q_index,
2002                                        const double *abs_coeff_mean,
2003                                        int *qcoeff_arr, int coeff_num) {
2004   double zero_bin_ratio = 2;
2005   double dc_q_step = av1_dc_quant_QTX(q_index, 0, AOM_BITS_8) / 4.;
2006   double ac_q_step = av1_ac_quant_QTX(q_index, 0, AOM_BITS_8) / 4.;
2007   double est_rate = 0;
2008   // dc coeff
2009   est_rate += av1_estimate_coeff_entropy(dc_q_step, abs_coeff_mean[0],
2010                                          zero_bin_ratio, qcoeff_arr[0]);
2011   // ac coeff
2012   for (int i = 1; i < coeff_num; ++i) {
2013     est_rate += av1_estimate_coeff_entropy(ac_q_step, abs_coeff_mean[i],
2014                                            zero_bin_ratio, qcoeff_arr[i]);
2015   }
2016   return est_rate;
2017 }
2018 
2019 #if CONFIG_RD_COMMAND
av1_read_rd_command(const char * filepath,RD_COMMAND * rd_command)2020 void av1_read_rd_command(const char *filepath, RD_COMMAND *rd_command) {
2021   FILE *fptr = fopen(filepath, "r");
2022   fscanf(fptr, "%d", &rd_command->frame_count);
2023   rd_command->frame_index = 0;
2024   for (int i = 0; i < rd_command->frame_count; ++i) {
2025     int option;
2026     fscanf(fptr, "%d", &option);
2027     rd_command->option_ls[i] = (RD_OPTION)option;
2028     if (option == RD_OPTION_SET_Q) {
2029       fscanf(fptr, "%d", &rd_command->q_index_ls[i]);
2030     } else if (option == RD_OPTION_SET_Q_RDMULT) {
2031       fscanf(fptr, "%d", &rd_command->q_index_ls[i]);
2032       fscanf(fptr, "%d", &rd_command->rdmult_ls[i]);
2033     }
2034   }
2035   fclose(fptr);
2036 }
2037 #endif  // CONFIG_RD_COMMAND
2038 
av1_tpl_get_frame_importance(const TplParams * tpl_data,int gf_frame_index)2039 double av1_tpl_get_frame_importance(const TplParams *tpl_data,
2040                                     int gf_frame_index) {
2041   const TplDepFrame *tpl_frame = &tpl_data->tpl_frame[gf_frame_index];
2042   const TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
2043 
2044   const int tpl_stride = tpl_frame->stride;
2045   double intra_cost_base = 0;
2046   double mc_dep_cost_base = 0;
2047   double cbcmp_base = 1;
2048   const int step = 1 << tpl_data->tpl_stats_block_mis_log2;
2049 
2050   for (int row = 0; row < tpl_frame->mi_rows; row += step) {
2051     for (int col = 0; col < tpl_frame->mi_cols; col += step) {
2052       const TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
2053           row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
2054       double cbcmp = (double)this_stats->srcrf_dist;
2055       const int64_t mc_dep_delta =
2056           RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
2057                  this_stats->mc_dep_dist);
2058       double dist_scaled = (double)(this_stats->recrf_dist << RDDIV_BITS);
2059       intra_cost_base += log(dist_scaled) * cbcmp;
2060       mc_dep_cost_base += log(dist_scaled + mc_dep_delta) * cbcmp;
2061       cbcmp_base += cbcmp;
2062     }
2063   }
2064   return exp((mc_dep_cost_base - intra_cost_base) / cbcmp_base);
2065 }
2066 
av1_tpl_get_qstep_ratio(const TplParams * tpl_data,int gf_frame_index)2067 double av1_tpl_get_qstep_ratio(const TplParams *tpl_data, int gf_frame_index) {
2068   if (!av1_tpl_stats_ready(tpl_data, gf_frame_index)) {
2069     return 1;
2070   }
2071   const double frame_importance =
2072       av1_tpl_get_frame_importance(tpl_data, gf_frame_index);
2073   return sqrt(1 / frame_importance);
2074 }
2075 
av1_get_q_index_from_qstep_ratio(int leaf_qindex,double qstep_ratio,aom_bit_depth_t bit_depth)2076 int av1_get_q_index_from_qstep_ratio(int leaf_qindex, double qstep_ratio,
2077                                      aom_bit_depth_t bit_depth) {
2078   const double leaf_qstep = av1_dc_quant_QTX(leaf_qindex, 0, bit_depth);
2079   const double target_qstep = leaf_qstep * qstep_ratio;
2080   int qindex = leaf_qindex;
2081   if (qstep_ratio < 1.0) {
2082     for (qindex = leaf_qindex; qindex > 0; --qindex) {
2083       const double qstep = av1_dc_quant_QTX(qindex, 0, bit_depth);
2084       if (qstep <= target_qstep) break;
2085     }
2086   } else {
2087     for (qindex = leaf_qindex; qindex <= MAXQ; ++qindex) {
2088       const double qstep = av1_dc_quant_QTX(qindex, 0, bit_depth);
2089       if (qstep >= target_qstep) break;
2090     }
2091   }
2092   return qindex;
2093 }
2094 
av1_tpl_get_q_index(const TplParams * tpl_data,int gf_frame_index,int leaf_qindex,aom_bit_depth_t bit_depth)2095 int av1_tpl_get_q_index(const TplParams *tpl_data, int gf_frame_index,
2096                         int leaf_qindex, aom_bit_depth_t bit_depth) {
2097   const double qstep_ratio = av1_tpl_get_qstep_ratio(tpl_data, gf_frame_index);
2098   return av1_get_q_index_from_qstep_ratio(leaf_qindex, qstep_ratio, bit_depth);
2099 }
2100 
2101 #if CONFIG_BITRATE_ACCURACY
av1_vbr_rc_init(VBR_RATECTRL_INFO * vbr_rc_info,double total_bit_budget,int show_frame_count)2102 void av1_vbr_rc_init(VBR_RATECTRL_INFO *vbr_rc_info, double total_bit_budget,
2103                      int show_frame_count) {
2104   av1_zero(*vbr_rc_info);
2105   vbr_rc_info->ready = 0;
2106   vbr_rc_info->total_bit_budget = total_bit_budget;
2107   vbr_rc_info->show_frame_count = show_frame_count;
2108   const double scale_factors[FRAME_UPDATE_TYPES] = { 0.94559, 0.94559, 1,
2109                                                      0.94559, 1,       1,
2110                                                      0.94559 };
2111 
2112   // TODO(angiebird): Based on the previous code, only the scale factor 0.94559
2113   // will be used in most of the cases with --limi=17. Figure out if the
2114   // following scale factors works better.
2115   // const double scale_factors[FRAME_UPDATE_TYPES] = { 0.94559, 0.12040, 1,
2116   //                                                    1.10199, 1,       1,
2117   //                                                    0.16393 };
2118 
2119   const double mv_scale_factors[FRAME_UPDATE_TYPES] = { 3, 3, 3, 3, 3, 3, 3 };
2120   memcpy(vbr_rc_info->scale_factors, scale_factors,
2121          sizeof(scale_factors[0]) * FRAME_UPDATE_TYPES);
2122   memcpy(vbr_rc_info->mv_scale_factors, mv_scale_factors,
2123          sizeof(mv_scale_factors[0]) * FRAME_UPDATE_TYPES);
2124 
2125   vbr_rc_reset_gop_data(vbr_rc_info);
2126 #if CONFIG_THREE_PASS
2127   // TODO(angiebird): Explain why we use -1 here
2128   vbr_rc_info->cur_gop_idx = -1;
2129   vbr_rc_info->gop_count = 0;
2130   vbr_rc_info->total_frame_count = 0;
2131 #endif  // CONFIG_THREE_PASS
2132 }
2133 
2134 #if CONFIG_THREE_PASS
av1_vbr_rc_frame_coding_idx(const VBR_RATECTRL_INFO * vbr_rc_info,int gf_frame_index)2135 int av1_vbr_rc_frame_coding_idx(const VBR_RATECTRL_INFO *vbr_rc_info,
2136                                 int gf_frame_index) {
2137   int gop_idx = vbr_rc_info->cur_gop_idx;
2138   int gop_start_idx = vbr_rc_info->gop_start_idx_list[gop_idx];
2139   return gop_start_idx + gf_frame_index;
2140 }
2141 
av1_vbr_rc_append_tpl_info(VBR_RATECTRL_INFO * vbr_rc_info,const TPL_INFO * tpl_info)2142 void av1_vbr_rc_append_tpl_info(VBR_RATECTRL_INFO *vbr_rc_info,
2143                                 const TPL_INFO *tpl_info) {
2144   int gop_start_idx = vbr_rc_info->total_frame_count;
2145   vbr_rc_info->gop_start_idx_list[vbr_rc_info->gop_count] = gop_start_idx;
2146   vbr_rc_info->gop_length_list[vbr_rc_info->gop_count] = tpl_info->gf_length;
2147   assert(gop_start_idx + tpl_info->gf_length <= VBR_RC_INFO_MAX_FRAMES);
2148   for (int i = 0; i < tpl_info->gf_length; ++i) {
2149     vbr_rc_info->txfm_stats_list[gop_start_idx + i] =
2150         tpl_info->txfm_stats_list[i];
2151     vbr_rc_info->qstep_ratio_list[gop_start_idx + i] =
2152         tpl_info->qstep_ratio_ls[i];
2153     vbr_rc_info->update_type_list[gop_start_idx + i] =
2154         tpl_info->update_type_list[i];
2155   }
2156   vbr_rc_info->total_frame_count += tpl_info->gf_length;
2157   vbr_rc_info->gop_count++;
2158 }
2159 #endif  // CONFIG_THREE_PASS
2160 
av1_vbr_rc_set_gop_bit_budget(VBR_RATECTRL_INFO * vbr_rc_info,int gop_showframe_count)2161 void av1_vbr_rc_set_gop_bit_budget(VBR_RATECTRL_INFO *vbr_rc_info,
2162                                    int gop_showframe_count) {
2163   vbr_rc_info->gop_showframe_count = gop_showframe_count;
2164   vbr_rc_info->gop_bit_budget = vbr_rc_info->total_bit_budget *
2165                                 gop_showframe_count /
2166                                 vbr_rc_info->show_frame_count;
2167 }
2168 
av1_vbr_rc_compute_q_indices(int base_q_index,int frame_count,const double * qstep_ratio_list,aom_bit_depth_t bit_depth,int * q_index_list)2169 void av1_vbr_rc_compute_q_indices(int base_q_index, int frame_count,
2170                                   const double *qstep_ratio_list,
2171                                   aom_bit_depth_t bit_depth,
2172                                   int *q_index_list) {
2173   for (int i = 0; i < frame_count; ++i) {
2174     q_index_list[i] = av1_get_q_index_from_qstep_ratio(
2175         base_q_index, qstep_ratio_list[i], bit_depth);
2176   }
2177 }
2178 
av1_vbr_rc_info_estimate_gop_bitrate(int base_q_index,aom_bit_depth_t bit_depth,const double * update_type_scale_factors,int frame_count,const FRAME_UPDATE_TYPE * update_type_list,const double * qstep_ratio_list,const TplTxfmStats * stats_list,int * q_index_list,double * estimated_bitrate_byframe)2179 double av1_vbr_rc_info_estimate_gop_bitrate(
2180     int base_q_index, aom_bit_depth_t bit_depth,
2181     const double *update_type_scale_factors, int frame_count,
2182     const FRAME_UPDATE_TYPE *update_type_list, const double *qstep_ratio_list,
2183     const TplTxfmStats *stats_list, int *q_index_list,
2184     double *estimated_bitrate_byframe) {
2185   av1_vbr_rc_compute_q_indices(base_q_index, frame_count, qstep_ratio_list,
2186                                bit_depth, q_index_list);
2187   double estimated_gop_bitrate = 0;
2188   for (int frame_index = 0; frame_index < frame_count; frame_index++) {
2189     const TplTxfmStats *frame_stats = &stats_list[frame_index];
2190     double frame_bitrate = 0;
2191     if (frame_stats->ready) {
2192       int q_index = q_index_list[frame_index];
2193 
2194       frame_bitrate = av1_laplace_estimate_frame_rate(
2195           q_index, frame_stats->txfm_block_count, frame_stats->abs_coeff_mean,
2196           frame_stats->coeff_num);
2197     }
2198     FRAME_UPDATE_TYPE update_type = update_type_list[frame_index];
2199     estimated_gop_bitrate +=
2200         frame_bitrate * update_type_scale_factors[update_type];
2201     if (estimated_bitrate_byframe != NULL) {
2202       estimated_bitrate_byframe[frame_index] = frame_bitrate;
2203     }
2204   }
2205   return estimated_gop_bitrate;
2206 }
2207 
av1_vbr_rc_info_estimate_base_q(double bit_budget,aom_bit_depth_t bit_depth,const double * update_type_scale_factors,int frame_count,const FRAME_UPDATE_TYPE * update_type_list,const double * qstep_ratio_list,const TplTxfmStats * stats_list,int * q_index_list,double * estimated_bitrate_byframe)2208 int av1_vbr_rc_info_estimate_base_q(
2209     double bit_budget, aom_bit_depth_t bit_depth,
2210     const double *update_type_scale_factors, int frame_count,
2211     const FRAME_UPDATE_TYPE *update_type_list, const double *qstep_ratio_list,
2212     const TplTxfmStats *stats_list, int *q_index_list,
2213     double *estimated_bitrate_byframe) {
2214   int q_max = 255;  // Maximum q value.
2215   int q_min = 0;    // Minimum q value.
2216   int q = (q_max + q_min) / 2;
2217 
2218   double q_max_estimate = av1_vbr_rc_info_estimate_gop_bitrate(
2219       q_max, bit_depth, update_type_scale_factors, frame_count,
2220       update_type_list, qstep_ratio_list, stats_list, q_index_list,
2221       estimated_bitrate_byframe);
2222 
2223   double q_min_estimate = av1_vbr_rc_info_estimate_gop_bitrate(
2224       q_min, bit_depth, update_type_scale_factors, frame_count,
2225       update_type_list, qstep_ratio_list, stats_list, q_index_list,
2226       estimated_bitrate_byframe);
2227   while (q_min + 1 < q_max) {
2228     double estimate = av1_vbr_rc_info_estimate_gop_bitrate(
2229         q, bit_depth, update_type_scale_factors, frame_count, update_type_list,
2230         qstep_ratio_list, stats_list, q_index_list, estimated_bitrate_byframe);
2231     if (estimate > bit_budget) {
2232       q_min = q;
2233       q_min_estimate = estimate;
2234     } else {
2235       q_max = q;
2236       q_max_estimate = estimate;
2237     }
2238     q = (q_max + q_min) / 2;
2239   }
2240   // Pick the estimate that lands closest to the budget.
2241   if (fabs(q_max_estimate - bit_budget) < fabs(q_min_estimate - bit_budget)) {
2242     q = q_max;
2243   } else {
2244     q = q_min;
2245   }
2246   // Update q_index_list and vbr_rc_info.
2247   av1_vbr_rc_info_estimate_gop_bitrate(
2248       q, bit_depth, update_type_scale_factors, frame_count, update_type_list,
2249       qstep_ratio_list, stats_list, q_index_list, estimated_bitrate_byframe);
2250   return q;
2251 }
av1_vbr_rc_update_q_index_list(VBR_RATECTRL_INFO * vbr_rc_info,const TplParams * tpl_data,const GF_GROUP * gf_group,aom_bit_depth_t bit_depth)2252 void av1_vbr_rc_update_q_index_list(VBR_RATECTRL_INFO *vbr_rc_info,
2253                                     const TplParams *tpl_data,
2254                                     const GF_GROUP *gf_group,
2255                                     aom_bit_depth_t bit_depth) {
2256   vbr_rc_info->q_index_list_ready = 1;
2257   double gop_bit_budget = vbr_rc_info->gop_bit_budget;
2258 
2259   for (int i = 0; i < gf_group->size; i++) {
2260     vbr_rc_info->qstep_ratio_list[i] = av1_tpl_get_qstep_ratio(tpl_data, i);
2261   }
2262 
2263   double mv_bits = 0;
2264   for (int i = 0; i < gf_group->size; i++) {
2265     double frame_mv_bits = 0;
2266     if (av1_tpl_stats_ready(tpl_data, i)) {
2267       TplDepFrame *tpl_frame = &tpl_data->tpl_frame[i];
2268       frame_mv_bits = av1_tpl_compute_frame_mv_entropy(
2269           tpl_frame, tpl_data->tpl_stats_block_mis_log2);
2270       FRAME_UPDATE_TYPE updae_type = gf_group->update_type[i];
2271       mv_bits += frame_mv_bits * vbr_rc_info->mv_scale_factors[updae_type];
2272     }
2273   }
2274 
2275   mv_bits = AOMMIN(mv_bits, 0.6 * gop_bit_budget);
2276   gop_bit_budget -= mv_bits;
2277 
2278   vbr_rc_info->base_q_index = av1_vbr_rc_info_estimate_base_q(
2279       gop_bit_budget, bit_depth, vbr_rc_info->scale_factors, gf_group->size,
2280       gf_group->update_type, vbr_rc_info->qstep_ratio_list,
2281       tpl_data->txfm_stats_list, vbr_rc_info->q_index_list, NULL);
2282 }
2283 
2284 #endif  // CONFIG_BITRATE_ACCURACY
2285 
2286 // Use upper and left neighbor block as the reference MVs.
2287 // Compute the minimum difference between current MV and reference MV.
av1_compute_mv_difference(const TplDepFrame * tpl_frame,int row,int col,int step,int tpl_stride,int right_shift)2288 int_mv av1_compute_mv_difference(const TplDepFrame *tpl_frame, int row, int col,
2289                                  int step, int tpl_stride, int right_shift) {
2290   const TplDepStats *tpl_stats =
2291       &tpl_frame
2292            ->tpl_stats_ptr[av1_tpl_ptr_pos(row, col, tpl_stride, right_shift)];
2293   int_mv current_mv = tpl_stats->mv[tpl_stats->ref_frame_index[0]];
2294   int current_mv_magnitude =
2295       abs(current_mv.as_mv.row) + abs(current_mv.as_mv.col);
2296 
2297   // Retrieve the up and left neighbors.
2298   int up_error = INT_MAX;
2299   int_mv up_mv_diff;
2300   if (row - step >= 0) {
2301     tpl_stats = &tpl_frame->tpl_stats_ptr[av1_tpl_ptr_pos(
2302         row - step, col, tpl_stride, right_shift)];
2303     up_mv_diff = tpl_stats->mv[tpl_stats->ref_frame_index[0]];
2304     up_mv_diff.as_mv.row = current_mv.as_mv.row - up_mv_diff.as_mv.row;
2305     up_mv_diff.as_mv.col = current_mv.as_mv.col - up_mv_diff.as_mv.col;
2306     up_error = abs(up_mv_diff.as_mv.row) + abs(up_mv_diff.as_mv.col);
2307   }
2308 
2309   int left_error = INT_MAX;
2310   int_mv left_mv_diff;
2311   if (col - step >= 0) {
2312     tpl_stats = &tpl_frame->tpl_stats_ptr[av1_tpl_ptr_pos(
2313         row, col - step, tpl_stride, right_shift)];
2314     left_mv_diff = tpl_stats->mv[tpl_stats->ref_frame_index[0]];
2315     left_mv_diff.as_mv.row = current_mv.as_mv.row - left_mv_diff.as_mv.row;
2316     left_mv_diff.as_mv.col = current_mv.as_mv.col - left_mv_diff.as_mv.col;
2317     left_error = abs(left_mv_diff.as_mv.row) + abs(left_mv_diff.as_mv.col);
2318   }
2319 
2320   // Return the MV with the minimum distance from current.
2321   if (up_error < left_error && up_error < current_mv_magnitude) {
2322     return up_mv_diff;
2323   } else if (left_error < up_error && left_error < current_mv_magnitude) {
2324     return left_mv_diff;
2325   }
2326   return current_mv;
2327 }
2328 
2329 /* Compute the entropy of motion vectors for a single frame. */
av1_tpl_compute_frame_mv_entropy(const TplDepFrame * tpl_frame,uint8_t right_shift)2330 double av1_tpl_compute_frame_mv_entropy(const TplDepFrame *tpl_frame,
2331                                         uint8_t right_shift) {
2332   if (!tpl_frame->is_valid) {
2333     return 0;
2334   }
2335 
2336   int count_row[500] = { 0 };
2337   int count_col[500] = { 0 };
2338   int n = 0;  // number of MVs to process
2339 
2340   const int tpl_stride = tpl_frame->stride;
2341   const int step = 1 << right_shift;
2342 
2343   for (int row = 0; row < tpl_frame->mi_rows; row += step) {
2344     for (int col = 0; col < tpl_frame->mi_cols; col += step) {
2345       int_mv mv = av1_compute_mv_difference(tpl_frame, row, col, step,
2346                                             tpl_stride, right_shift);
2347       count_row[clamp(mv.as_mv.row, 0, 499)] += 1;
2348       count_col[clamp(mv.as_mv.row, 0, 499)] += 1;
2349       n += 1;
2350     }
2351   }
2352 
2353   // Estimate the bits used using the entropy formula.
2354   double rate_row = 0;
2355   double rate_col = 0;
2356   for (int i = 0; i < 500; i++) {
2357     if (count_row[i] != 0) {
2358       double p = count_row[i] / (double)n;
2359       rate_row += count_row[i] * -log2(p);
2360     }
2361     if (count_col[i] != 0) {
2362       double p = count_col[i] / (double)n;
2363       rate_col += count_col[i] * -log2(p);
2364     }
2365   }
2366 
2367   return rate_row + rate_col;
2368 }
2369