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