1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2023, 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 "config/aom_dsp_rtcd.h"
13*77c1e3ccSAndroid Build Coastguard Worker #include "config/av1_rtcd.h"
14*77c1e3ccSAndroid Build Coastguard Worker
15*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconinter.h"
16*77c1e3ccSAndroid Build Coastguard Worker
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodemv.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/nonrd_opt.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/rdopt.h"
20*77c1e3ccSAndroid Build Coastguard Worker
21*77c1e3ccSAndroid Build Coastguard Worker static const SCAN_ORDER av1_fast_idtx_scan_order_16x16 = {
22*77c1e3ccSAndroid Build Coastguard Worker av1_fast_idtx_scan_16x16, av1_fast_idtx_iscan_16x16
23*77c1e3ccSAndroid Build Coastguard Worker };
24*77c1e3ccSAndroid Build Coastguard Worker
25*77c1e3ccSAndroid Build Coastguard Worker #define DECLARE_BLOCK_YRD_BUFFERS() \
26*77c1e3ccSAndroid Build Coastguard Worker DECLARE_ALIGNED(64, tran_low_t, dqcoeff_buf[16 * 16]); \
27*77c1e3ccSAndroid Build Coastguard Worker DECLARE_ALIGNED(64, tran_low_t, qcoeff_buf[16 * 16]); \
28*77c1e3ccSAndroid Build Coastguard Worker DECLARE_ALIGNED(64, tran_low_t, coeff_buf[16 * 16]); \
29*77c1e3ccSAndroid Build Coastguard Worker uint16_t eob[1];
30*77c1e3ccSAndroid Build Coastguard Worker
31*77c1e3ccSAndroid Build Coastguard Worker #define DECLARE_BLOCK_YRD_VARS() \
32*77c1e3ccSAndroid Build Coastguard Worker /* When is_tx_8x8_dual_applicable is true, we compute the txfm for the \
33*77c1e3ccSAndroid Build Coastguard Worker * entire bsize and write macroblock_plane::coeff. So low_coeff is kept \
34*77c1e3ccSAndroid Build Coastguard Worker * as a non-const so we can reassign it to macroblock_plane::coeff. */ \
35*77c1e3ccSAndroid Build Coastguard Worker int16_t *low_coeff = (int16_t *)coeff_buf; \
36*77c1e3ccSAndroid Build Coastguard Worker int16_t *const low_qcoeff = (int16_t *)qcoeff_buf; \
37*77c1e3ccSAndroid Build Coastguard Worker int16_t *const low_dqcoeff = (int16_t *)dqcoeff_buf; \
38*77c1e3ccSAndroid Build Coastguard Worker const int diff_stride = bw;
39*77c1e3ccSAndroid Build Coastguard Worker
40*77c1e3ccSAndroid Build Coastguard Worker #define DECLARE_LOOP_VARS_BLOCK_YRD() \
41*77c1e3ccSAndroid Build Coastguard Worker const int16_t *src_diff = &p->src_diff[(r * diff_stride + c) << 2];
42*77c1e3ccSAndroid Build Coastguard Worker
update_yrd_loop_vars(MACROBLOCK * x,int * skippable,int step,int ncoeffs,int16_t * const low_coeff,int16_t * const low_qcoeff,int16_t * const low_dqcoeff,RD_STATS * this_rdc,int * eob_cost,int tx_blk_id)43*77c1e3ccSAndroid Build Coastguard Worker static AOM_FORCE_INLINE void update_yrd_loop_vars(
44*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCK *x, int *skippable, int step, int ncoeffs,
45*77c1e3ccSAndroid Build Coastguard Worker int16_t *const low_coeff, int16_t *const low_qcoeff,
46*77c1e3ccSAndroid Build Coastguard Worker int16_t *const low_dqcoeff, RD_STATS *this_rdc, int *eob_cost,
47*77c1e3ccSAndroid Build Coastguard Worker int tx_blk_id) {
48*77c1e3ccSAndroid Build Coastguard Worker const int is_txfm_skip = (ncoeffs == 0);
49*77c1e3ccSAndroid Build Coastguard Worker *skippable &= is_txfm_skip;
50*77c1e3ccSAndroid Build Coastguard Worker x->txfm_search_info.blk_skip[tx_blk_id] = is_txfm_skip;
51*77c1e3ccSAndroid Build Coastguard Worker *eob_cost += get_msb(ncoeffs + 1);
52*77c1e3ccSAndroid Build Coastguard Worker if (ncoeffs == 1)
53*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate += (int)abs(low_qcoeff[0]);
54*77c1e3ccSAndroid Build Coastguard Worker else if (ncoeffs > 1)
55*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate += aom_satd_lp(low_qcoeff, step << 4);
56*77c1e3ccSAndroid Build Coastguard Worker
57*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist += av1_block_error_lp(low_coeff, low_dqcoeff, step << 4) >> 2;
58*77c1e3ccSAndroid Build Coastguard Worker }
59*77c1e3ccSAndroid Build Coastguard Worker
aom_process_hadamard_lp_8x16(MACROBLOCK * x,int max_blocks_high,int max_blocks_wide,int num_4x4_w,int step,int block_step)60*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_process_hadamard_lp_8x16(MACROBLOCK *x,
61*77c1e3ccSAndroid Build Coastguard Worker int max_blocks_high,
62*77c1e3ccSAndroid Build Coastguard Worker int max_blocks_wide,
63*77c1e3ccSAndroid Build Coastguard Worker int num_4x4_w, int step,
64*77c1e3ccSAndroid Build Coastguard Worker int block_step) {
65*77c1e3ccSAndroid Build Coastguard Worker struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
66*77c1e3ccSAndroid Build Coastguard Worker const int bw = 4 * num_4x4_w;
67*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4 = AOMMIN(num_4x4_w, max_blocks_wide);
68*77c1e3ccSAndroid Build Coastguard Worker int block = 0;
69*77c1e3ccSAndroid Build Coastguard Worker
70*77c1e3ccSAndroid Build Coastguard Worker for (int r = 0; r < max_blocks_high; r += block_step) {
71*77c1e3ccSAndroid Build Coastguard Worker for (int c = 0; c < num_4x4; c += 2 * block_step) {
72*77c1e3ccSAndroid Build Coastguard Worker const int16_t *src_diff = &p->src_diff[(r * bw + c) << 2];
73*77c1e3ccSAndroid Build Coastguard Worker int16_t *low_coeff = (int16_t *)p->coeff + BLOCK_OFFSET(block);
74*77c1e3ccSAndroid Build Coastguard Worker aom_hadamard_lp_8x8_dual(src_diff, (ptrdiff_t)bw, low_coeff);
75*77c1e3ccSAndroid Build Coastguard Worker block += 2 * step;
76*77c1e3ccSAndroid Build Coastguard Worker }
77*77c1e3ccSAndroid Build Coastguard Worker }
78*77c1e3ccSAndroid Build Coastguard Worker }
79*77c1e3ccSAndroid Build Coastguard Worker
80*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
81*77c1e3ccSAndroid Build Coastguard Worker #define DECLARE_BLOCK_YRD_HBD_VARS() \
82*77c1e3ccSAndroid Build Coastguard Worker tran_low_t *const coeff = coeff_buf; \
83*77c1e3ccSAndroid Build Coastguard Worker tran_low_t *const qcoeff = qcoeff_buf; \
84*77c1e3ccSAndroid Build Coastguard Worker tran_low_t *const dqcoeff = dqcoeff_buf;
85*77c1e3ccSAndroid Build Coastguard Worker
update_yrd_loop_vars_hbd(MACROBLOCK * x,int * skippable,int step,int ncoeffs,tran_low_t * const coeff,tran_low_t * const qcoeff,tran_low_t * const dqcoeff,RD_STATS * this_rdc,int * eob_cost,int tx_blk_id)86*77c1e3ccSAndroid Build Coastguard Worker static AOM_FORCE_INLINE void update_yrd_loop_vars_hbd(
87*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCK *x, int *skippable, int step, int ncoeffs,
88*77c1e3ccSAndroid Build Coastguard Worker tran_low_t *const coeff, tran_low_t *const qcoeff,
89*77c1e3ccSAndroid Build Coastguard Worker tran_low_t *const dqcoeff, RD_STATS *this_rdc, int *eob_cost,
90*77c1e3ccSAndroid Build Coastguard Worker int tx_blk_id) {
91*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCKD *xd = &x->e_mbd;
92*77c1e3ccSAndroid Build Coastguard Worker const int is_txfm_skip = (ncoeffs == 0);
93*77c1e3ccSAndroid Build Coastguard Worker *skippable &= is_txfm_skip;
94*77c1e3ccSAndroid Build Coastguard Worker x->txfm_search_info.blk_skip[tx_blk_id] = is_txfm_skip;
95*77c1e3ccSAndroid Build Coastguard Worker *eob_cost += get_msb(ncoeffs + 1);
96*77c1e3ccSAndroid Build Coastguard Worker
97*77c1e3ccSAndroid Build Coastguard Worker int64_t dummy;
98*77c1e3ccSAndroid Build Coastguard Worker if (ncoeffs == 1)
99*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate += (int)abs(qcoeff[0]);
100*77c1e3ccSAndroid Build Coastguard Worker else if (ncoeffs > 1)
101*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate += aom_satd(qcoeff, step << 4);
102*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist +=
103*77c1e3ccSAndroid Build Coastguard Worker av1_highbd_block_error(coeff, dqcoeff, step << 4, &dummy, xd->bd) >> 2;
104*77c1e3ccSAndroid Build Coastguard Worker }
105*77c1e3ccSAndroid Build Coastguard Worker #endif
106*77c1e3ccSAndroid Build Coastguard Worker
107*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Calculates RD Cost using Hadamard transform.
108*77c1e3ccSAndroid Build Coastguard Worker *
109*77c1e3ccSAndroid Build Coastguard Worker * \ingroup nonrd_mode_search
110*77c1e3ccSAndroid Build Coastguard Worker * \callgraph
111*77c1e3ccSAndroid Build Coastguard Worker * \callergraph
112*77c1e3ccSAndroid Build Coastguard Worker * Calculates RD Cost using Hadamard transform. For low bit depth this function
113*77c1e3ccSAndroid Build Coastguard Worker * uses low-precision set of functions (16-bit) and 32 bit for high bit depth
114*77c1e3ccSAndroid Build Coastguard Worker * \param[in] x Pointer to structure holding all the data for
115*77c1e3ccSAndroid Build Coastguard Worker the current macroblock
116*77c1e3ccSAndroid Build Coastguard Worker * \param[in] this_rdc Pointer to calculated RD Cost
117*77c1e3ccSAndroid Build Coastguard Worker * \param[in] skippable Pointer to a flag indicating possible tx skip
118*77c1e3ccSAndroid Build Coastguard Worker * \param[in] bsize Current block size
119*77c1e3ccSAndroid Build Coastguard Worker * \param[in] tx_size Transform size
120*77c1e3ccSAndroid Build Coastguard Worker * \param[in] is_inter_mode Flag to indicate inter mode
121*77c1e3ccSAndroid Build Coastguard Worker *
122*77c1e3ccSAndroid Build Coastguard Worker * \remark Nothing is returned. Instead, calculated RD cost is placed to
123*77c1e3ccSAndroid Build Coastguard Worker * \c this_rdc. \c skippable flag is set if there is no non-zero quantized
124*77c1e3ccSAndroid Build Coastguard Worker * coefficients for Hadamard transform
125*77c1e3ccSAndroid Build Coastguard Worker */
av1_block_yrd(MACROBLOCK * x,RD_STATS * this_rdc,int * skippable,BLOCK_SIZE bsize,TX_SIZE tx_size)126*77c1e3ccSAndroid Build Coastguard Worker void av1_block_yrd(MACROBLOCK *x, RD_STATS *this_rdc, int *skippable,
127*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE bsize, TX_SIZE tx_size) {
128*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *xd = &x->e_mbd;
129*77c1e3ccSAndroid Build Coastguard Worker const struct macroblockd_plane *pd = &xd->plane[AOM_PLANE_Y];
130*77c1e3ccSAndroid Build Coastguard Worker struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
131*77c1e3ccSAndroid Build Coastguard Worker assert(bsize < BLOCK_SIZES_ALL);
132*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_w = mi_size_wide[bsize];
133*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_h = mi_size_high[bsize];
134*77c1e3ccSAndroid Build Coastguard Worker const int step = 1 << (tx_size << 1);
135*77c1e3ccSAndroid Build Coastguard Worker const int block_step = (1 << tx_size);
136*77c1e3ccSAndroid Build Coastguard Worker const int row_step = step * num_4x4_w >> tx_size;
137*77c1e3ccSAndroid Build Coastguard Worker int block = 0;
138*77c1e3ccSAndroid Build Coastguard Worker const int max_blocks_wide =
139*77c1e3ccSAndroid Build Coastguard Worker num_4x4_w + (xd->mb_to_right_edge >= 0 ? 0 : xd->mb_to_right_edge >> 5);
140*77c1e3ccSAndroid Build Coastguard Worker const int max_blocks_high =
141*77c1e3ccSAndroid Build Coastguard Worker num_4x4_h + (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >> 5);
142*77c1e3ccSAndroid Build Coastguard Worker int eob_cost = 0;
143*77c1e3ccSAndroid Build Coastguard Worker const int bw = 4 * num_4x4_w;
144*77c1e3ccSAndroid Build Coastguard Worker const int bh = 4 * num_4x4_h;
145*77c1e3ccSAndroid Build Coastguard Worker const int use_hbd = is_cur_buf_hbd(xd);
146*77c1e3ccSAndroid Build Coastguard Worker int num_blk_skip_w = num_4x4_w;
147*77c1e3ccSAndroid Build Coastguard Worker
148*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
149*77c1e3ccSAndroid Build Coastguard Worker if (use_hbd) {
150*77c1e3ccSAndroid Build Coastguard Worker aom_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf,
151*77c1e3ccSAndroid Build Coastguard Worker p->src.stride, pd->dst.buf, pd->dst.stride);
152*77c1e3ccSAndroid Build Coastguard Worker } else {
153*77c1e3ccSAndroid Build Coastguard Worker aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
154*77c1e3ccSAndroid Build Coastguard Worker pd->dst.buf, pd->dst.stride);
155*77c1e3ccSAndroid Build Coastguard Worker }
156*77c1e3ccSAndroid Build Coastguard Worker #else
157*77c1e3ccSAndroid Build Coastguard Worker aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
158*77c1e3ccSAndroid Build Coastguard Worker pd->dst.buf, pd->dst.stride);
159*77c1e3ccSAndroid Build Coastguard Worker #endif
160*77c1e3ccSAndroid Build Coastguard Worker
161*77c1e3ccSAndroid Build Coastguard Worker // Keep the intermediate value on the stack here. Writing directly to
162*77c1e3ccSAndroid Build Coastguard Worker // skippable causes speed regression due to load-and-store issues in
163*77c1e3ccSAndroid Build Coastguard Worker // update_yrd_loop_vars.
164*77c1e3ccSAndroid Build Coastguard Worker int temp_skippable = 1;
165*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist = 0;
166*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate = 0;
167*77c1e3ccSAndroid Build Coastguard Worker // For block sizes 8x16 or above, Hadamard txfm of two adjacent 8x8 blocks
168*77c1e3ccSAndroid Build Coastguard Worker // can be done per function call. Hence the call of Hadamard txfm is
169*77c1e3ccSAndroid Build Coastguard Worker // abstracted here for the specified cases.
170*77c1e3ccSAndroid Build Coastguard Worker int is_tx_8x8_dual_applicable =
171*77c1e3ccSAndroid Build Coastguard Worker (tx_size == TX_8X8 && block_size_wide[bsize] >= 16 &&
172*77c1e3ccSAndroid Build Coastguard Worker block_size_high[bsize] >= 8);
173*77c1e3ccSAndroid Build Coastguard Worker
174*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
175*77c1e3ccSAndroid Build Coastguard Worker // As of now, dual implementation of hadamard txfm is available for low
176*77c1e3ccSAndroid Build Coastguard Worker // bitdepth.
177*77c1e3ccSAndroid Build Coastguard Worker if (use_hbd) is_tx_8x8_dual_applicable = 0;
178*77c1e3ccSAndroid Build Coastguard Worker #endif
179*77c1e3ccSAndroid Build Coastguard Worker
180*77c1e3ccSAndroid Build Coastguard Worker if (is_tx_8x8_dual_applicable) {
181*77c1e3ccSAndroid Build Coastguard Worker aom_process_hadamard_lp_8x16(x, max_blocks_high, max_blocks_wide, num_4x4_w,
182*77c1e3ccSAndroid Build Coastguard Worker step, block_step);
183*77c1e3ccSAndroid Build Coastguard Worker }
184*77c1e3ccSAndroid Build Coastguard Worker
185*77c1e3ccSAndroid Build Coastguard Worker const SCAN_ORDER *const scan_order = &av1_scan_orders[tx_size][DCT_DCT];
186*77c1e3ccSAndroid Build Coastguard Worker DECLARE_BLOCK_YRD_BUFFERS()
187*77c1e3ccSAndroid Build Coastguard Worker DECLARE_BLOCK_YRD_VARS()
188*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
189*77c1e3ccSAndroid Build Coastguard Worker DECLARE_BLOCK_YRD_HBD_VARS()
190*77c1e3ccSAndroid Build Coastguard Worker #else
191*77c1e3ccSAndroid Build Coastguard Worker (void)use_hbd;
192*77c1e3ccSAndroid Build Coastguard Worker #endif
193*77c1e3ccSAndroid Build Coastguard Worker
194*77c1e3ccSAndroid Build Coastguard Worker // Keep track of the row and column of the blocks we use so that we know
195*77c1e3ccSAndroid Build Coastguard Worker // if we are in the unrestricted motion border.
196*77c1e3ccSAndroid Build Coastguard Worker for (int r = 0; r < max_blocks_high; r += block_step) {
197*77c1e3ccSAndroid Build Coastguard Worker for (int c = 0, s = 0; c < max_blocks_wide; c += block_step, s += step) {
198*77c1e3ccSAndroid Build Coastguard Worker DECLARE_LOOP_VARS_BLOCK_YRD()
199*77c1e3ccSAndroid Build Coastguard Worker
200*77c1e3ccSAndroid Build Coastguard Worker switch (tx_size) {
201*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
202*77c1e3ccSAndroid Build Coastguard Worker case TX_16X16:
203*77c1e3ccSAndroid Build Coastguard Worker if (use_hbd) {
204*77c1e3ccSAndroid Build Coastguard Worker aom_hadamard_16x16(src_diff, diff_stride, coeff);
205*77c1e3ccSAndroid Build Coastguard Worker av1_quantize_fp(coeff, 16 * 16, p->zbin_QTX, p->round_fp_QTX,
206*77c1e3ccSAndroid Build Coastguard Worker p->quant_fp_QTX, p->quant_shift_QTX, qcoeff,
207*77c1e3ccSAndroid Build Coastguard Worker dqcoeff, p->dequant_QTX, eob,
208*77c1e3ccSAndroid Build Coastguard Worker // default_scan_fp_16x16_transpose and
209*77c1e3ccSAndroid Build Coastguard Worker // av1_default_iscan_fp_16x16_transpose have to be
210*77c1e3ccSAndroid Build Coastguard Worker // used together.
211*77c1e3ccSAndroid Build Coastguard Worker default_scan_fp_16x16_transpose,
212*77c1e3ccSAndroid Build Coastguard Worker av1_default_iscan_fp_16x16_transpose);
213*77c1e3ccSAndroid Build Coastguard Worker } else {
214*77c1e3ccSAndroid Build Coastguard Worker aom_hadamard_lp_16x16(src_diff, diff_stride, low_coeff);
215*77c1e3ccSAndroid Build Coastguard Worker av1_quantize_lp(low_coeff, 16 * 16, p->round_fp_QTX,
216*77c1e3ccSAndroid Build Coastguard Worker p->quant_fp_QTX, low_qcoeff, low_dqcoeff,
217*77c1e3ccSAndroid Build Coastguard Worker p->dequant_QTX, eob,
218*77c1e3ccSAndroid Build Coastguard Worker // default_scan_lp_16x16_transpose and
219*77c1e3ccSAndroid Build Coastguard Worker // av1_default_iscan_lp_16x16_transpose have to be
220*77c1e3ccSAndroid Build Coastguard Worker // used together.
221*77c1e3ccSAndroid Build Coastguard Worker default_scan_lp_16x16_transpose,
222*77c1e3ccSAndroid Build Coastguard Worker av1_default_iscan_lp_16x16_transpose);
223*77c1e3ccSAndroid Build Coastguard Worker }
224*77c1e3ccSAndroid Build Coastguard Worker break;
225*77c1e3ccSAndroid Build Coastguard Worker case TX_8X8:
226*77c1e3ccSAndroid Build Coastguard Worker if (use_hbd) {
227*77c1e3ccSAndroid Build Coastguard Worker aom_hadamard_8x8(src_diff, diff_stride, coeff);
228*77c1e3ccSAndroid Build Coastguard Worker av1_quantize_fp(
229*77c1e3ccSAndroid Build Coastguard Worker coeff, 8 * 8, p->zbin_QTX, p->round_fp_QTX, p->quant_fp_QTX,
230*77c1e3ccSAndroid Build Coastguard Worker p->quant_shift_QTX, qcoeff, dqcoeff, p->dequant_QTX, eob,
231*77c1e3ccSAndroid Build Coastguard Worker default_scan_8x8_transpose, av1_default_iscan_8x8_transpose);
232*77c1e3ccSAndroid Build Coastguard Worker } else {
233*77c1e3ccSAndroid Build Coastguard Worker if (is_tx_8x8_dual_applicable) {
234*77c1e3ccSAndroid Build Coastguard Worker // The coeffs are pre-computed for the whole block, so re-assign
235*77c1e3ccSAndroid Build Coastguard Worker // low_coeff to the appropriate location.
236*77c1e3ccSAndroid Build Coastguard Worker const int block_offset = BLOCK_OFFSET(block + s);
237*77c1e3ccSAndroid Build Coastguard Worker low_coeff = (int16_t *)p->coeff + block_offset;
238*77c1e3ccSAndroid Build Coastguard Worker } else {
239*77c1e3ccSAndroid Build Coastguard Worker aom_hadamard_lp_8x8(src_diff, diff_stride, low_coeff);
240*77c1e3ccSAndroid Build Coastguard Worker }
241*77c1e3ccSAndroid Build Coastguard Worker av1_quantize_lp(
242*77c1e3ccSAndroid Build Coastguard Worker low_coeff, 8 * 8, p->round_fp_QTX, p->quant_fp_QTX, low_qcoeff,
243*77c1e3ccSAndroid Build Coastguard Worker low_dqcoeff, p->dequant_QTX, eob,
244*77c1e3ccSAndroid Build Coastguard Worker // default_scan_8x8_transpose and
245*77c1e3ccSAndroid Build Coastguard Worker // av1_default_iscan_8x8_transpose have to be used together.
246*77c1e3ccSAndroid Build Coastguard Worker default_scan_8x8_transpose, av1_default_iscan_8x8_transpose);
247*77c1e3ccSAndroid Build Coastguard Worker }
248*77c1e3ccSAndroid Build Coastguard Worker break;
249*77c1e3ccSAndroid Build Coastguard Worker default:
250*77c1e3ccSAndroid Build Coastguard Worker assert(tx_size == TX_4X4);
251*77c1e3ccSAndroid Build Coastguard Worker // In tx_size=4x4 case, aom_fdct4x4 and aom_fdct4x4_lp generate
252*77c1e3ccSAndroid Build Coastguard Worker // normal coefficients order, so we don't need to change the scan
253*77c1e3ccSAndroid Build Coastguard Worker // order here.
254*77c1e3ccSAndroid Build Coastguard Worker if (use_hbd) {
255*77c1e3ccSAndroid Build Coastguard Worker aom_fdct4x4(src_diff, coeff, diff_stride);
256*77c1e3ccSAndroid Build Coastguard Worker av1_quantize_fp(coeff, 4 * 4, p->zbin_QTX, p->round_fp_QTX,
257*77c1e3ccSAndroid Build Coastguard Worker p->quant_fp_QTX, p->quant_shift_QTX, qcoeff,
258*77c1e3ccSAndroid Build Coastguard Worker dqcoeff, p->dequant_QTX, eob, scan_order->scan,
259*77c1e3ccSAndroid Build Coastguard Worker scan_order->iscan);
260*77c1e3ccSAndroid Build Coastguard Worker } else {
261*77c1e3ccSAndroid Build Coastguard Worker aom_fdct4x4_lp(src_diff, low_coeff, diff_stride);
262*77c1e3ccSAndroid Build Coastguard Worker av1_quantize_lp(low_coeff, 4 * 4, p->round_fp_QTX, p->quant_fp_QTX,
263*77c1e3ccSAndroid Build Coastguard Worker low_qcoeff, low_dqcoeff, p->dequant_QTX, eob,
264*77c1e3ccSAndroid Build Coastguard Worker scan_order->scan, scan_order->iscan);
265*77c1e3ccSAndroid Build Coastguard Worker }
266*77c1e3ccSAndroid Build Coastguard Worker break;
267*77c1e3ccSAndroid Build Coastguard Worker #else
268*77c1e3ccSAndroid Build Coastguard Worker case TX_16X16:
269*77c1e3ccSAndroid Build Coastguard Worker aom_hadamard_lp_16x16(src_diff, diff_stride, low_coeff);
270*77c1e3ccSAndroid Build Coastguard Worker av1_quantize_lp(low_coeff, 16 * 16, p->round_fp_QTX, p->quant_fp_QTX,
271*77c1e3ccSAndroid Build Coastguard Worker low_qcoeff, low_dqcoeff, p->dequant_QTX, eob,
272*77c1e3ccSAndroid Build Coastguard Worker default_scan_lp_16x16_transpose,
273*77c1e3ccSAndroid Build Coastguard Worker av1_default_iscan_lp_16x16_transpose);
274*77c1e3ccSAndroid Build Coastguard Worker break;
275*77c1e3ccSAndroid Build Coastguard Worker case TX_8X8:
276*77c1e3ccSAndroid Build Coastguard Worker if (is_tx_8x8_dual_applicable) {
277*77c1e3ccSAndroid Build Coastguard Worker // The coeffs are pre-computed for the whole block, so re-assign
278*77c1e3ccSAndroid Build Coastguard Worker // low_coeff to the appropriate location.
279*77c1e3ccSAndroid Build Coastguard Worker const int block_offset = BLOCK_OFFSET(block + s);
280*77c1e3ccSAndroid Build Coastguard Worker low_coeff = (int16_t *)p->coeff + block_offset;
281*77c1e3ccSAndroid Build Coastguard Worker } else {
282*77c1e3ccSAndroid Build Coastguard Worker aom_hadamard_lp_8x8(src_diff, diff_stride, low_coeff);
283*77c1e3ccSAndroid Build Coastguard Worker }
284*77c1e3ccSAndroid Build Coastguard Worker av1_quantize_lp(low_coeff, 8 * 8, p->round_fp_QTX, p->quant_fp_QTX,
285*77c1e3ccSAndroid Build Coastguard Worker low_qcoeff, low_dqcoeff, p->dequant_QTX, eob,
286*77c1e3ccSAndroid Build Coastguard Worker default_scan_8x8_transpose,
287*77c1e3ccSAndroid Build Coastguard Worker av1_default_iscan_8x8_transpose);
288*77c1e3ccSAndroid Build Coastguard Worker break;
289*77c1e3ccSAndroid Build Coastguard Worker default:
290*77c1e3ccSAndroid Build Coastguard Worker aom_fdct4x4_lp(src_diff, low_coeff, diff_stride);
291*77c1e3ccSAndroid Build Coastguard Worker av1_quantize_lp(low_coeff, 4 * 4, p->round_fp_QTX, p->quant_fp_QTX,
292*77c1e3ccSAndroid Build Coastguard Worker low_qcoeff, low_dqcoeff, p->dequant_QTX, eob,
293*77c1e3ccSAndroid Build Coastguard Worker scan_order->scan, scan_order->iscan);
294*77c1e3ccSAndroid Build Coastguard Worker break;
295*77c1e3ccSAndroid Build Coastguard Worker #endif
296*77c1e3ccSAndroid Build Coastguard Worker }
297*77c1e3ccSAndroid Build Coastguard Worker assert(*eob <= 1024);
298*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
299*77c1e3ccSAndroid Build Coastguard Worker if (use_hbd)
300*77c1e3ccSAndroid Build Coastguard Worker update_yrd_loop_vars_hbd(x, &temp_skippable, step, *eob, coeff, qcoeff,
301*77c1e3ccSAndroid Build Coastguard Worker dqcoeff, this_rdc, &eob_cost,
302*77c1e3ccSAndroid Build Coastguard Worker r * num_blk_skip_w + c);
303*77c1e3ccSAndroid Build Coastguard Worker else
304*77c1e3ccSAndroid Build Coastguard Worker #endif
305*77c1e3ccSAndroid Build Coastguard Worker update_yrd_loop_vars(x, &temp_skippable, step, *eob, low_coeff,
306*77c1e3ccSAndroid Build Coastguard Worker low_qcoeff, low_dqcoeff, this_rdc, &eob_cost,
307*77c1e3ccSAndroid Build Coastguard Worker r * num_blk_skip_w + c);
308*77c1e3ccSAndroid Build Coastguard Worker }
309*77c1e3ccSAndroid Build Coastguard Worker block += row_step;
310*77c1e3ccSAndroid Build Coastguard Worker }
311*77c1e3ccSAndroid Build Coastguard Worker
312*77c1e3ccSAndroid Build Coastguard Worker this_rdc->skip_txfm = *skippable = temp_skippable;
313*77c1e3ccSAndroid Build Coastguard Worker if (this_rdc->sse < INT64_MAX) {
314*77c1e3ccSAndroid Build Coastguard Worker this_rdc->sse = (this_rdc->sse << 6) >> 2;
315*77c1e3ccSAndroid Build Coastguard Worker if (temp_skippable) {
316*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist = 0;
317*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist = this_rdc->sse;
318*77c1e3ccSAndroid Build Coastguard Worker return;
319*77c1e3ccSAndroid Build Coastguard Worker }
320*77c1e3ccSAndroid Build Coastguard Worker }
321*77c1e3ccSAndroid Build Coastguard Worker
322*77c1e3ccSAndroid Build Coastguard Worker // If skippable is set, rate gets clobbered later.
323*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate <<= (2 + AV1_PROB_COST_SHIFT);
324*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate += (eob_cost << AV1_PROB_COST_SHIFT);
325*77c1e3ccSAndroid Build Coastguard Worker }
326*77c1e3ccSAndroid Build Coastguard Worker
327*77c1e3ccSAndroid Build Coastguard Worker // Explicitly enumerate the cases so the compiler can generate SIMD for the
328*77c1e3ccSAndroid Build Coastguard Worker // function. According to the disassembler, gcc generates SSE codes for each of
329*77c1e3ccSAndroid Build Coastguard Worker // the possible block sizes. The hottest case is tx_width 16, which takes up
330*77c1e3ccSAndroid Build Coastguard Worker // about 8% of the self cycle of av1_nonrd_pick_inter_mode_sb. Since
331*77c1e3ccSAndroid Build Coastguard Worker // av1_nonrd_pick_inter_mode_sb takes up about 3% of total encoding time, the
332*77c1e3ccSAndroid Build Coastguard Worker // potential room of improvement for writing AVX2 optimization is only 3% * 8% =
333*77c1e3ccSAndroid Build Coastguard Worker // 0.24% of total encoding time.
scale_square_buf_vals(int16_t * dst,int tx_width,const int16_t * src,int src_stride)334*77c1e3ccSAndroid Build Coastguard Worker static inline void scale_square_buf_vals(int16_t *dst, int tx_width,
335*77c1e3ccSAndroid Build Coastguard Worker const int16_t *src, int src_stride) {
336*77c1e3ccSAndroid Build Coastguard Worker #define DO_SCALING \
337*77c1e3ccSAndroid Build Coastguard Worker do { \
338*77c1e3ccSAndroid Build Coastguard Worker for (int idy = 0; idy < tx_width; ++idy) { \
339*77c1e3ccSAndroid Build Coastguard Worker for (int idx = 0; idx < tx_width; ++idx) { \
340*77c1e3ccSAndroid Build Coastguard Worker dst[idy * tx_width + idx] = src[idy * src_stride + idx] * 8; \
341*77c1e3ccSAndroid Build Coastguard Worker } \
342*77c1e3ccSAndroid Build Coastguard Worker } \
343*77c1e3ccSAndroid Build Coastguard Worker } while (0)
344*77c1e3ccSAndroid Build Coastguard Worker
345*77c1e3ccSAndroid Build Coastguard Worker if (tx_width == 4) {
346*77c1e3ccSAndroid Build Coastguard Worker DO_SCALING;
347*77c1e3ccSAndroid Build Coastguard Worker } else if (tx_width == 8) {
348*77c1e3ccSAndroid Build Coastguard Worker DO_SCALING;
349*77c1e3ccSAndroid Build Coastguard Worker } else if (tx_width == 16) {
350*77c1e3ccSAndroid Build Coastguard Worker DO_SCALING;
351*77c1e3ccSAndroid Build Coastguard Worker } else {
352*77c1e3ccSAndroid Build Coastguard Worker assert(0);
353*77c1e3ccSAndroid Build Coastguard Worker }
354*77c1e3ccSAndroid Build Coastguard Worker
355*77c1e3ccSAndroid Build Coastguard Worker #undef DO_SCALING
356*77c1e3ccSAndroid Build Coastguard Worker }
357*77c1e3ccSAndroid Build Coastguard Worker
358*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Calculates RD Cost when the block uses Identity transform.
359*77c1e3ccSAndroid Build Coastguard Worker * Note that this function is only for low bit depth encoding, since it
360*77c1e3ccSAndroid Build Coastguard Worker * is called in real-time mode for now, which sets high bit depth to 0:
361*77c1e3ccSAndroid Build Coastguard Worker * -DCONFIG_AV1_HIGHBITDEPTH=0
362*77c1e3ccSAndroid Build Coastguard Worker *
363*77c1e3ccSAndroid Build Coastguard Worker * \ingroup nonrd_mode_search
364*77c1e3ccSAndroid Build Coastguard Worker * \callgraph
365*77c1e3ccSAndroid Build Coastguard Worker * \callergraph
366*77c1e3ccSAndroid Build Coastguard Worker * Calculates RD Cost. For low bit depth this function
367*77c1e3ccSAndroid Build Coastguard Worker * uses low-precision set of functions (16-bit) and 32 bit for high bit depth
368*77c1e3ccSAndroid Build Coastguard Worker * \param[in] x Pointer to structure holding all the data for
369*77c1e3ccSAndroid Build Coastguard Worker the current macroblock
370*77c1e3ccSAndroid Build Coastguard Worker * \param[in] pred_buf Pointer to the prediction buffer
371*77c1e3ccSAndroid Build Coastguard Worker * \param[in] pred_stride Stride for the prediction buffer
372*77c1e3ccSAndroid Build Coastguard Worker * \param[in] this_rdc Pointer to calculated RD Cost
373*77c1e3ccSAndroid Build Coastguard Worker * \param[in] skippable Pointer to a flag indicating possible tx skip
374*77c1e3ccSAndroid Build Coastguard Worker * \param[in] bsize Current block size
375*77c1e3ccSAndroid Build Coastguard Worker * \param[in] tx_size Transform size
376*77c1e3ccSAndroid Build Coastguard Worker *
377*77c1e3ccSAndroid Build Coastguard Worker * \remark Nothing is returned. Instead, calculated RD cost is placed to
378*77c1e3ccSAndroid Build Coastguard Worker * \c this_rdc. \c skippable flag is set if all coefficients are zero.
379*77c1e3ccSAndroid Build Coastguard Worker */
av1_block_yrd_idtx(MACROBLOCK * x,const uint8_t * const pred_buf,int pred_stride,RD_STATS * this_rdc,int * skippable,BLOCK_SIZE bsize,TX_SIZE tx_size)380*77c1e3ccSAndroid Build Coastguard Worker void av1_block_yrd_idtx(MACROBLOCK *x, const uint8_t *const pred_buf,
381*77c1e3ccSAndroid Build Coastguard Worker int pred_stride, RD_STATS *this_rdc, int *skippable,
382*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE bsize, TX_SIZE tx_size) {
383*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *xd = &x->e_mbd;
384*77c1e3ccSAndroid Build Coastguard Worker struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
385*77c1e3ccSAndroid Build Coastguard Worker assert(bsize < BLOCK_SIZES_ALL);
386*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_w = mi_size_wide[bsize];
387*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_h = mi_size_high[bsize];
388*77c1e3ccSAndroid Build Coastguard Worker const int step = 1 << (tx_size << 1);
389*77c1e3ccSAndroid Build Coastguard Worker const int block_step = (1 << tx_size);
390*77c1e3ccSAndroid Build Coastguard Worker const int max_blocks_wide =
391*77c1e3ccSAndroid Build Coastguard Worker num_4x4_w + (xd->mb_to_right_edge >= 0 ? 0 : xd->mb_to_right_edge >> 5);
392*77c1e3ccSAndroid Build Coastguard Worker const int max_blocks_high =
393*77c1e3ccSAndroid Build Coastguard Worker num_4x4_h + (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >> 5);
394*77c1e3ccSAndroid Build Coastguard Worker int eob_cost = 0;
395*77c1e3ccSAndroid Build Coastguard Worker const int bw = 4 * num_4x4_w;
396*77c1e3ccSAndroid Build Coastguard Worker const int bh = 4 * num_4x4_h;
397*77c1e3ccSAndroid Build Coastguard Worker const int num_blk_skip_w = num_4x4_w;
398*77c1e3ccSAndroid Build Coastguard Worker // Keep the intermediate value on the stack here. Writing directly to
399*77c1e3ccSAndroid Build Coastguard Worker // skippable causes speed regression due to load-and-store issues in
400*77c1e3ccSAndroid Build Coastguard Worker // update_yrd_loop_vars.
401*77c1e3ccSAndroid Build Coastguard Worker int temp_skippable = 1;
402*77c1e3ccSAndroid Build Coastguard Worker int tx_wd = 0;
403*77c1e3ccSAndroid Build Coastguard Worker const SCAN_ORDER *scan_order = NULL;
404*77c1e3ccSAndroid Build Coastguard Worker switch (tx_size) {
405*77c1e3ccSAndroid Build Coastguard Worker case TX_64X64:
406*77c1e3ccSAndroid Build Coastguard Worker assert(0); // Not implemented
407*77c1e3ccSAndroid Build Coastguard Worker break;
408*77c1e3ccSAndroid Build Coastguard Worker case TX_32X32:
409*77c1e3ccSAndroid Build Coastguard Worker assert(0); // Not used
410*77c1e3ccSAndroid Build Coastguard Worker break;
411*77c1e3ccSAndroid Build Coastguard Worker case TX_16X16:
412*77c1e3ccSAndroid Build Coastguard Worker scan_order = &av1_fast_idtx_scan_order_16x16;
413*77c1e3ccSAndroid Build Coastguard Worker tx_wd = 16;
414*77c1e3ccSAndroid Build Coastguard Worker break;
415*77c1e3ccSAndroid Build Coastguard Worker case TX_8X8:
416*77c1e3ccSAndroid Build Coastguard Worker scan_order = &av1_fast_idtx_scan_order_8x8;
417*77c1e3ccSAndroid Build Coastguard Worker tx_wd = 8;
418*77c1e3ccSAndroid Build Coastguard Worker break;
419*77c1e3ccSAndroid Build Coastguard Worker default:
420*77c1e3ccSAndroid Build Coastguard Worker assert(tx_size == TX_4X4);
421*77c1e3ccSAndroid Build Coastguard Worker scan_order = &av1_fast_idtx_scan_order_4x4;
422*77c1e3ccSAndroid Build Coastguard Worker tx_wd = 4;
423*77c1e3ccSAndroid Build Coastguard Worker break;
424*77c1e3ccSAndroid Build Coastguard Worker }
425*77c1e3ccSAndroid Build Coastguard Worker assert(scan_order != NULL);
426*77c1e3ccSAndroid Build Coastguard Worker
427*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist = 0;
428*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate = 0;
429*77c1e3ccSAndroid Build Coastguard Worker aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
430*77c1e3ccSAndroid Build Coastguard Worker pred_buf, pred_stride);
431*77c1e3ccSAndroid Build Coastguard Worker // Keep track of the row and column of the blocks we use so that we know
432*77c1e3ccSAndroid Build Coastguard Worker // if we are in the unrestricted motion border.
433*77c1e3ccSAndroid Build Coastguard Worker DECLARE_BLOCK_YRD_BUFFERS()
434*77c1e3ccSAndroid Build Coastguard Worker DECLARE_BLOCK_YRD_VARS()
435*77c1e3ccSAndroid Build Coastguard Worker for (int r = 0; r < max_blocks_high; r += block_step) {
436*77c1e3ccSAndroid Build Coastguard Worker for (int c = 0, s = 0; c < max_blocks_wide; c += block_step, s += step) {
437*77c1e3ccSAndroid Build Coastguard Worker DECLARE_LOOP_VARS_BLOCK_YRD()
438*77c1e3ccSAndroid Build Coastguard Worker scale_square_buf_vals(low_coeff, tx_wd, src_diff, diff_stride);
439*77c1e3ccSAndroid Build Coastguard Worker av1_quantize_lp(low_coeff, tx_wd * tx_wd, p->round_fp_QTX,
440*77c1e3ccSAndroid Build Coastguard Worker p->quant_fp_QTX, low_qcoeff, low_dqcoeff, p->dequant_QTX,
441*77c1e3ccSAndroid Build Coastguard Worker eob, scan_order->scan, scan_order->iscan);
442*77c1e3ccSAndroid Build Coastguard Worker assert(*eob <= 1024);
443*77c1e3ccSAndroid Build Coastguard Worker update_yrd_loop_vars(x, &temp_skippable, step, *eob, low_coeff,
444*77c1e3ccSAndroid Build Coastguard Worker low_qcoeff, low_dqcoeff, this_rdc, &eob_cost,
445*77c1e3ccSAndroid Build Coastguard Worker r * num_blk_skip_w + c);
446*77c1e3ccSAndroid Build Coastguard Worker }
447*77c1e3ccSAndroid Build Coastguard Worker }
448*77c1e3ccSAndroid Build Coastguard Worker this_rdc->skip_txfm = *skippable = temp_skippable;
449*77c1e3ccSAndroid Build Coastguard Worker if (this_rdc->sse < INT64_MAX) {
450*77c1e3ccSAndroid Build Coastguard Worker this_rdc->sse = (this_rdc->sse << 6) >> 2;
451*77c1e3ccSAndroid Build Coastguard Worker if (temp_skippable) {
452*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist = 0;
453*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist = this_rdc->sse;
454*77c1e3ccSAndroid Build Coastguard Worker return;
455*77c1e3ccSAndroid Build Coastguard Worker }
456*77c1e3ccSAndroid Build Coastguard Worker }
457*77c1e3ccSAndroid Build Coastguard Worker // If skippable is set, rate gets clobbered later.
458*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate <<= (2 + AV1_PROB_COST_SHIFT);
459*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate += (eob_cost << AV1_PROB_COST_SHIFT);
460*77c1e3ccSAndroid Build Coastguard Worker }
461*77c1e3ccSAndroid Build Coastguard Worker
av1_model_rd_for_sb_uv(AV1_COMP * cpi,BLOCK_SIZE plane_bsize,MACROBLOCK * x,MACROBLOCKD * xd,RD_STATS * this_rdc,int start_plane,int stop_plane)462*77c1e3ccSAndroid Build Coastguard Worker int64_t av1_model_rd_for_sb_uv(AV1_COMP *cpi, BLOCK_SIZE plane_bsize,
463*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCK *x, MACROBLOCKD *xd,
464*77c1e3ccSAndroid Build Coastguard Worker RD_STATS *this_rdc, int start_plane,
465*77c1e3ccSAndroid Build Coastguard Worker int stop_plane) {
466*77c1e3ccSAndroid Build Coastguard Worker // Note our transform coeffs are 8 times an orthogonal transform.
467*77c1e3ccSAndroid Build Coastguard Worker // Hence quantizer step is also 8 times. To get effective quantizer
468*77c1e3ccSAndroid Build Coastguard Worker // we need to divide by 8 before sending to modeling function.
469*77c1e3ccSAndroid Build Coastguard Worker unsigned int sse;
470*77c1e3ccSAndroid Build Coastguard Worker int rate;
471*77c1e3ccSAndroid Build Coastguard Worker int64_t dist;
472*77c1e3ccSAndroid Build Coastguard Worker int plane;
473*77c1e3ccSAndroid Build Coastguard Worker int64_t tot_sse = 0;
474*77c1e3ccSAndroid Build Coastguard Worker
475*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate = 0;
476*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist = 0;
477*77c1e3ccSAndroid Build Coastguard Worker this_rdc->skip_txfm = 0;
478*77c1e3ccSAndroid Build Coastguard Worker
479*77c1e3ccSAndroid Build Coastguard Worker for (plane = start_plane; plane <= stop_plane; ++plane) {
480*77c1e3ccSAndroid Build Coastguard Worker struct macroblock_plane *const p = &x->plane[plane];
481*77c1e3ccSAndroid Build Coastguard Worker struct macroblockd_plane *const pd = &xd->plane[plane];
482*77c1e3ccSAndroid Build Coastguard Worker const uint32_t dc_quant = p->dequant_QTX[0];
483*77c1e3ccSAndroid Build Coastguard Worker const uint32_t ac_quant = p->dequant_QTX[1];
484*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bs = plane_bsize;
485*77c1e3ccSAndroid Build Coastguard Worker unsigned int var;
486*77c1e3ccSAndroid Build Coastguard Worker if (!x->color_sensitivity[COLOR_SENS_IDX(plane)]) continue;
487*77c1e3ccSAndroid Build Coastguard Worker
488*77c1e3ccSAndroid Build Coastguard Worker var = cpi->ppi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf,
489*77c1e3ccSAndroid Build Coastguard Worker pd->dst.stride, &sse);
490*77c1e3ccSAndroid Build Coastguard Worker assert(sse >= var);
491*77c1e3ccSAndroid Build Coastguard Worker tot_sse += sse;
492*77c1e3ccSAndroid Build Coastguard Worker
493*77c1e3ccSAndroid Build Coastguard Worker av1_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
494*77c1e3ccSAndroid Build Coastguard Worker dc_quant >> 3, &rate, &dist);
495*77c1e3ccSAndroid Build Coastguard Worker
496*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate += rate >> 1;
497*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist += dist << 3;
498*77c1e3ccSAndroid Build Coastguard Worker
499*77c1e3ccSAndroid Build Coastguard Worker av1_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs], ac_quant >> 3,
500*77c1e3ccSAndroid Build Coastguard Worker &rate, &dist);
501*77c1e3ccSAndroid Build Coastguard Worker
502*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate += rate;
503*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist += dist << 4;
504*77c1e3ccSAndroid Build Coastguard Worker }
505*77c1e3ccSAndroid Build Coastguard Worker
506*77c1e3ccSAndroid Build Coastguard Worker if (this_rdc->rate == 0) {
507*77c1e3ccSAndroid Build Coastguard Worker this_rdc->skip_txfm = 1;
508*77c1e3ccSAndroid Build Coastguard Worker }
509*77c1e3ccSAndroid Build Coastguard Worker
510*77c1e3ccSAndroid Build Coastguard Worker if (RDCOST(x->rdmult, this_rdc->rate, this_rdc->dist) >=
511*77c1e3ccSAndroid Build Coastguard Worker RDCOST(x->rdmult, 0, tot_sse << 4)) {
512*77c1e3ccSAndroid Build Coastguard Worker this_rdc->rate = 0;
513*77c1e3ccSAndroid Build Coastguard Worker this_rdc->dist = tot_sse << 4;
514*77c1e3ccSAndroid Build Coastguard Worker this_rdc->skip_txfm = 1;
515*77c1e3ccSAndroid Build Coastguard Worker }
516*77c1e3ccSAndroid Build Coastguard Worker
517*77c1e3ccSAndroid Build Coastguard Worker return tot_sse;
518*77c1e3ccSAndroid Build Coastguard Worker }
519*77c1e3ccSAndroid Build Coastguard Worker
compute_intra_yprediction(const AV1_COMMON * cm,PREDICTION_MODE mode,BLOCK_SIZE bsize,MACROBLOCK * x,MACROBLOCKD * xd)520*77c1e3ccSAndroid Build Coastguard Worker static void compute_intra_yprediction(const AV1_COMMON *cm,
521*77c1e3ccSAndroid Build Coastguard Worker PREDICTION_MODE mode, BLOCK_SIZE bsize,
522*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCK *x, MACROBLOCKD *xd) {
523*77c1e3ccSAndroid Build Coastguard Worker const SequenceHeader *seq_params = cm->seq_params;
524*77c1e3ccSAndroid Build Coastguard Worker struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
525*77c1e3ccSAndroid Build Coastguard Worker struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
526*77c1e3ccSAndroid Build Coastguard Worker uint8_t *const src_buf_base = p->src.buf;
527*77c1e3ccSAndroid Build Coastguard Worker uint8_t *const dst_buf_base = pd->dst.buf;
528*77c1e3ccSAndroid Build Coastguard Worker const int src_stride = p->src.stride;
529*77c1e3ccSAndroid Build Coastguard Worker const int dst_stride = pd->dst.stride;
530*77c1e3ccSAndroid Build Coastguard Worker int plane = 0;
531*77c1e3ccSAndroid Build Coastguard Worker int row, col;
532*77c1e3ccSAndroid Build Coastguard Worker // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
533*77c1e3ccSAndroid Build Coastguard Worker // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
534*77c1e3ccSAndroid Build Coastguard Worker // transform size varies per plane, look it up in a common way.
535*77c1e3ccSAndroid Build Coastguard Worker const TX_SIZE tx_size = max_txsize_lookup[bsize];
536*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE plane_bsize =
537*77c1e3ccSAndroid Build Coastguard Worker get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
538*77c1e3ccSAndroid Build Coastguard Worker // If mb_to_right_edge is < 0 we are in a situation in which
539*77c1e3ccSAndroid Build Coastguard Worker // the current block size extends into the UMV and we won't
540*77c1e3ccSAndroid Build Coastguard Worker // visit the sub blocks that are wholly within the UMV.
541*77c1e3ccSAndroid Build Coastguard Worker const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
542*77c1e3ccSAndroid Build Coastguard Worker const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
543*77c1e3ccSAndroid Build Coastguard Worker // Keep track of the row and column of the blocks we use so that we know
544*77c1e3ccSAndroid Build Coastguard Worker // if we are in the unrestricted motion border.
545*77c1e3ccSAndroid Build Coastguard Worker for (row = 0; row < max_blocks_high; row += (1 << tx_size)) {
546*77c1e3ccSAndroid Build Coastguard Worker // Skip visiting the sub blocks that are wholly within the UMV.
547*77c1e3ccSAndroid Build Coastguard Worker for (col = 0; col < max_blocks_wide; col += (1 << tx_size)) {
548*77c1e3ccSAndroid Build Coastguard Worker p->src.buf = &src_buf_base[4 * (row * (int64_t)src_stride + col)];
549*77c1e3ccSAndroid Build Coastguard Worker pd->dst.buf = &dst_buf_base[4 * (row * (int64_t)dst_stride + col)];
550*77c1e3ccSAndroid Build Coastguard Worker av1_predict_intra_block(
551*77c1e3ccSAndroid Build Coastguard Worker xd, seq_params->sb_size, seq_params->enable_intra_edge_filter,
552*77c1e3ccSAndroid Build Coastguard Worker block_size_wide[bsize], block_size_high[bsize], tx_size, mode, 0, 0,
553*77c1e3ccSAndroid Build Coastguard Worker FILTER_INTRA_MODES, pd->dst.buf, dst_stride, pd->dst.buf, dst_stride,
554*77c1e3ccSAndroid Build Coastguard Worker 0, 0, plane);
555*77c1e3ccSAndroid Build Coastguard Worker }
556*77c1e3ccSAndroid Build Coastguard Worker }
557*77c1e3ccSAndroid Build Coastguard Worker p->src.buf = src_buf_base;
558*77c1e3ccSAndroid Build Coastguard Worker pd->dst.buf = dst_buf_base;
559*77c1e3ccSAndroid Build Coastguard Worker }
560*77c1e3ccSAndroid Build Coastguard Worker
561*77c1e3ccSAndroid Build Coastguard Worker // Checks whether Intra mode needs to be pruned based on
562*77c1e3ccSAndroid Build Coastguard Worker // 'intra_y_mode_bsize_mask_nrd' and 'prune_hv_pred_modes_using_blksad'
563*77c1e3ccSAndroid Build Coastguard Worker // speed features.
is_prune_intra_mode(AV1_COMP * cpi,int mode_index,int force_intra_check,BLOCK_SIZE bsize,uint8_t segment_id,SOURCE_SAD source_sad_nonrd,uint8_t color_sensitivity[MAX_MB_PLANE-1])564*77c1e3ccSAndroid Build Coastguard Worker static inline bool is_prune_intra_mode(
565*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *cpi, int mode_index, int force_intra_check, BLOCK_SIZE bsize,
566*77c1e3ccSAndroid Build Coastguard Worker uint8_t segment_id, SOURCE_SAD source_sad_nonrd,
567*77c1e3ccSAndroid Build Coastguard Worker uint8_t color_sensitivity[MAX_MB_PLANE - 1]) {
568*77c1e3ccSAndroid Build Coastguard Worker const PREDICTION_MODE this_mode = intra_mode_list[mode_index];
569*77c1e3ccSAndroid Build Coastguard Worker if (mode_index > 2 || force_intra_check == 0) {
570*77c1e3ccSAndroid Build Coastguard Worker if (!((1 << this_mode) & cpi->sf.rt_sf.intra_y_mode_bsize_mask_nrd[bsize]))
571*77c1e3ccSAndroid Build Coastguard Worker return true;
572*77c1e3ccSAndroid Build Coastguard Worker
573*77c1e3ccSAndroid Build Coastguard Worker if (this_mode == DC_PRED) return false;
574*77c1e3ccSAndroid Build Coastguard Worker
575*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->sf.rt_sf.prune_hv_pred_modes_using_src_sad) return false;
576*77c1e3ccSAndroid Build Coastguard Worker
577*77c1e3ccSAndroid Build Coastguard Worker const bool has_color_sensitivity =
578*77c1e3ccSAndroid Build Coastguard Worker color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] &&
579*77c1e3ccSAndroid Build Coastguard Worker color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)];
580*77c1e3ccSAndroid Build Coastguard Worker if (has_color_sensitivity &&
581*77c1e3ccSAndroid Build Coastguard Worker (cpi->rc.frame_source_sad > 1.1 * cpi->rc.avg_source_sad ||
582*77c1e3ccSAndroid Build Coastguard Worker cyclic_refresh_segment_id_boosted(segment_id) ||
583*77c1e3ccSAndroid Build Coastguard Worker source_sad_nonrd > kMedSad))
584*77c1e3ccSAndroid Build Coastguard Worker return false;
585*77c1e3ccSAndroid Build Coastguard Worker
586*77c1e3ccSAndroid Build Coastguard Worker return true;
587*77c1e3ccSAndroid Build Coastguard Worker }
588*77c1e3ccSAndroid Build Coastguard Worker return false;
589*77c1e3ccSAndroid Build Coastguard Worker }
590*77c1e3ccSAndroid Build Coastguard Worker
591*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Estimation of RD cost of an intra mode for Non-RD optimized case.
592*77c1e3ccSAndroid Build Coastguard Worker *
593*77c1e3ccSAndroid Build Coastguard Worker * \ingroup nonrd_mode_search
594*77c1e3ccSAndroid Build Coastguard Worker * \callgraph
595*77c1e3ccSAndroid Build Coastguard Worker * \callergraph
596*77c1e3ccSAndroid Build Coastguard Worker * Calculates RD Cost for an intra mode for a single TX block using Hadamard
597*77c1e3ccSAndroid Build Coastguard Worker * transform.
598*77c1e3ccSAndroid Build Coastguard Worker * \param[in] plane Color plane
599*77c1e3ccSAndroid Build Coastguard Worker * \param[in] block Index of a TX block in a prediction block
600*77c1e3ccSAndroid Build Coastguard Worker * \param[in] row Row of a current TX block
601*77c1e3ccSAndroid Build Coastguard Worker * \param[in] col Column of a current TX block
602*77c1e3ccSAndroid Build Coastguard Worker * \param[in] plane_bsize Block size of a current prediction block
603*77c1e3ccSAndroid Build Coastguard Worker * \param[in] tx_size Transform size
604*77c1e3ccSAndroid Build Coastguard Worker * \param[in] arg Pointer to a structure that holds parameters
605*77c1e3ccSAndroid Build Coastguard Worker * for intra mode search
606*77c1e3ccSAndroid Build Coastguard Worker *
607*77c1e3ccSAndroid Build Coastguard Worker * \remark Nothing is returned. Instead, best mode and RD Cost of the best mode
608*77c1e3ccSAndroid Build Coastguard Worker * are set in \c args->rdc and \c args->mode
609*77c1e3ccSAndroid Build Coastguard Worker */
av1_estimate_block_intra(int plane,int block,int row,int col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)610*77c1e3ccSAndroid Build Coastguard Worker void av1_estimate_block_intra(int plane, int block, int row, int col,
611*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
612*77c1e3ccSAndroid Build Coastguard Worker void *arg) {
613*77c1e3ccSAndroid Build Coastguard Worker struct estimate_block_intra_args *const args = arg;
614*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *const cpi = args->cpi;
615*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
616*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCK *const x = args->x;
617*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd = &x->e_mbd;
618*77c1e3ccSAndroid Build Coastguard Worker struct macroblock_plane *const p = &x->plane[plane];
619*77c1e3ccSAndroid Build Coastguard Worker struct macroblockd_plane *const pd = &xd->plane[plane];
620*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size];
621*77c1e3ccSAndroid Build Coastguard Worker uint8_t *const src_buf_base = p->src.buf;
622*77c1e3ccSAndroid Build Coastguard Worker uint8_t *const dst_buf_base = pd->dst.buf;
623*77c1e3ccSAndroid Build Coastguard Worker const int64_t src_stride = p->src.stride;
624*77c1e3ccSAndroid Build Coastguard Worker const int64_t dst_stride = pd->dst.stride;
625*77c1e3ccSAndroid Build Coastguard Worker
626*77c1e3ccSAndroid Build Coastguard Worker (void)block;
627*77c1e3ccSAndroid Build Coastguard Worker
628*77c1e3ccSAndroid Build Coastguard Worker av1_predict_intra_block_facade(cm, xd, plane, col, row, tx_size);
629*77c1e3ccSAndroid Build Coastguard Worker
630*77c1e3ccSAndroid Build Coastguard Worker if (args->prune_mode_based_on_sad) {
631*77c1e3ccSAndroid Build Coastguard Worker unsigned int this_sad = cpi->ppi->fn_ptr[plane_bsize].sdf(
632*77c1e3ccSAndroid Build Coastguard Worker p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride);
633*77c1e3ccSAndroid Build Coastguard Worker const unsigned int sad_threshold =
634*77c1e3ccSAndroid Build Coastguard Worker args->best_sad != UINT_MAX ? args->best_sad + (args->best_sad >> 4)
635*77c1e3ccSAndroid Build Coastguard Worker : UINT_MAX;
636*77c1e3ccSAndroid Build Coastguard Worker // Skip the evaluation of current mode if its SAD is more than a threshold.
637*77c1e3ccSAndroid Build Coastguard Worker if (this_sad > sad_threshold) {
638*77c1e3ccSAndroid Build Coastguard Worker // For the current mode, set rate and distortion to maximum possible
639*77c1e3ccSAndroid Build Coastguard Worker // values and return.
640*77c1e3ccSAndroid Build Coastguard Worker // Note: args->rdc->rate is checked in av1_nonrd_pick_intra_mode() to skip
641*77c1e3ccSAndroid Build Coastguard Worker // the evaluation of the current mode.
642*77c1e3ccSAndroid Build Coastguard Worker args->rdc->rate = INT_MAX;
643*77c1e3ccSAndroid Build Coastguard Worker args->rdc->dist = INT64_MAX;
644*77c1e3ccSAndroid Build Coastguard Worker return;
645*77c1e3ccSAndroid Build Coastguard Worker }
646*77c1e3ccSAndroid Build Coastguard Worker if (this_sad < args->best_sad) {
647*77c1e3ccSAndroid Build Coastguard Worker args->best_sad = this_sad;
648*77c1e3ccSAndroid Build Coastguard Worker }
649*77c1e3ccSAndroid Build Coastguard Worker }
650*77c1e3ccSAndroid Build Coastguard Worker
651*77c1e3ccSAndroid Build Coastguard Worker RD_STATS this_rdc;
652*77c1e3ccSAndroid Build Coastguard Worker av1_invalid_rd_stats(&this_rdc);
653*77c1e3ccSAndroid Build Coastguard Worker
654*77c1e3ccSAndroid Build Coastguard Worker p->src.buf = &src_buf_base[4 * (row * src_stride + col)];
655*77c1e3ccSAndroid Build Coastguard Worker pd->dst.buf = &dst_buf_base[4 * (row * dst_stride + col)];
656*77c1e3ccSAndroid Build Coastguard Worker
657*77c1e3ccSAndroid Build Coastguard Worker if (plane == 0) {
658*77c1e3ccSAndroid Build Coastguard Worker av1_block_yrd(x, &this_rdc, &args->skippable, bsize_tx,
659*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(tx_size, TX_16X16));
660*77c1e3ccSAndroid Build Coastguard Worker } else {
661*77c1e3ccSAndroid Build Coastguard Worker av1_model_rd_for_sb_uv(cpi, bsize_tx, x, xd, &this_rdc, plane, plane);
662*77c1e3ccSAndroid Build Coastguard Worker }
663*77c1e3ccSAndroid Build Coastguard Worker
664*77c1e3ccSAndroid Build Coastguard Worker p->src.buf = src_buf_base;
665*77c1e3ccSAndroid Build Coastguard Worker pd->dst.buf = dst_buf_base;
666*77c1e3ccSAndroid Build Coastguard Worker assert(args->rdc->rate != INT_MAX && args->rdc->dist != INT64_MAX);
667*77c1e3ccSAndroid Build Coastguard Worker args->rdc->rate += this_rdc.rate;
668*77c1e3ccSAndroid Build Coastguard Worker args->rdc->dist += this_rdc.dist;
669*77c1e3ccSAndroid Build Coastguard Worker }
670*77c1e3ccSAndroid Build Coastguard Worker
671*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Estimates best intra mode for inter mode search
672*77c1e3ccSAndroid Build Coastguard Worker *
673*77c1e3ccSAndroid Build Coastguard Worker * \ingroup nonrd_mode_search
674*77c1e3ccSAndroid Build Coastguard Worker * \callgraph
675*77c1e3ccSAndroid Build Coastguard Worker * \callergraph
676*77c1e3ccSAndroid Build Coastguard Worker *
677*77c1e3ccSAndroid Build Coastguard Worker * Using heuristics based on best inter mode, block size, and other decides
678*77c1e3ccSAndroid Build Coastguard Worker * whether to check intra modes. If so, estimates and selects best intra mode
679*77c1e3ccSAndroid Build Coastguard Worker * from the reduced set of intra modes (max 4 intra modes checked)
680*77c1e3ccSAndroid Build Coastguard Worker *
681*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top-level encoder structure
682*77c1e3ccSAndroid Build Coastguard Worker * \param[in] x Pointer to structure holding all the
683*77c1e3ccSAndroid Build Coastguard Worker * data for the current macroblock
684*77c1e3ccSAndroid Build Coastguard Worker * \param[in] bsize Current block size
685*77c1e3ccSAndroid Build Coastguard Worker * \param[in] best_early_term Flag, indicating that TX for the
686*77c1e3ccSAndroid Build Coastguard Worker * best inter mode was skipped
687*77c1e3ccSAndroid Build Coastguard Worker * \param[in] ref_cost_intra Cost of signalling intra mode
688*77c1e3ccSAndroid Build Coastguard Worker * \param[in] reuse_prediction Flag, indicating prediction re-use
689*77c1e3ccSAndroid Build Coastguard Worker * \param[in] orig_dst Original destination buffer
690*77c1e3ccSAndroid Build Coastguard Worker * \param[in] tmp_buffers Pointer to a temporary buffers for
691*77c1e3ccSAndroid Build Coastguard Worker * prediction re-use
692*77c1e3ccSAndroid Build Coastguard Worker * \param[out] this_mode_pred Pointer to store prediction buffer
693*77c1e3ccSAndroid Build Coastguard Worker * for prediction re-use
694*77c1e3ccSAndroid Build Coastguard Worker * \param[in] best_rdc Pointer to RD cost for the best
695*77c1e3ccSAndroid Build Coastguard Worker * selected intra mode
696*77c1e3ccSAndroid Build Coastguard Worker * \param[in] best_pickmode Pointer to a structure containing
697*77c1e3ccSAndroid Build Coastguard Worker * best mode picked so far
698*77c1e3ccSAndroid Build Coastguard Worker * \param[in] ctx Pointer to structure holding coding
699*77c1e3ccSAndroid Build Coastguard Worker * contexts and modes for the block
700*77c1e3ccSAndroid Build Coastguard Worker *
701*77c1e3ccSAndroid Build Coastguard Worker * \remark Nothing is returned. Instead, calculated RD cost is placed to
702*77c1e3ccSAndroid Build Coastguard Worker * \c best_rdc and best selected mode is placed to \c best_pickmode
703*77c1e3ccSAndroid Build Coastguard Worker *
704*77c1e3ccSAndroid Build Coastguard Worker */
av1_estimate_intra_mode(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int best_early_term,unsigned int ref_cost_intra,int reuse_prediction,struct buf_2d * orig_dst,PRED_BUFFER * tmp_buffers,PRED_BUFFER ** this_mode_pred,RD_STATS * best_rdc,BEST_PICKMODE * best_pickmode,PICK_MODE_CONTEXT * ctx)705*77c1e3ccSAndroid Build Coastguard Worker void av1_estimate_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
706*77c1e3ccSAndroid Build Coastguard Worker int best_early_term, unsigned int ref_cost_intra,
707*77c1e3ccSAndroid Build Coastguard Worker int reuse_prediction, struct buf_2d *orig_dst,
708*77c1e3ccSAndroid Build Coastguard Worker PRED_BUFFER *tmp_buffers,
709*77c1e3ccSAndroid Build Coastguard Worker PRED_BUFFER **this_mode_pred, RD_STATS *best_rdc,
710*77c1e3ccSAndroid Build Coastguard Worker BEST_PICKMODE *best_pickmode,
711*77c1e3ccSAndroid Build Coastguard Worker PICK_MODE_CONTEXT *ctx) {
712*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
713*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd = &x->e_mbd;
714*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *const mi = xd->mi[0];
715*77c1e3ccSAndroid Build Coastguard Worker const TxfmSearchParams *txfm_params = &x->txfm_search_params;
716*77c1e3ccSAndroid Build Coastguard Worker const unsigned char segment_id = mi->segment_id;
717*77c1e3ccSAndroid Build Coastguard Worker const int *const rd_threshes = cpi->rd.threshes[segment_id][bsize];
718*77c1e3ccSAndroid Build Coastguard Worker const int *const rd_thresh_freq_fact = x->thresh_freq_fact[bsize];
719*77c1e3ccSAndroid Build Coastguard Worker const bool is_screen_content =
720*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN;
721*77c1e3ccSAndroid Build Coastguard Worker struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
722*77c1e3ccSAndroid Build Coastguard Worker const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
723*77c1e3ccSAndroid Build Coastguard Worker
724*77c1e3ccSAndroid Build Coastguard Worker const CommonQuantParams *quant_params = &cm->quant_params;
725*77c1e3ccSAndroid Build Coastguard Worker
726*77c1e3ccSAndroid Build Coastguard Worker RD_STATS this_rdc;
727*77c1e3ccSAndroid Build Coastguard Worker
728*77c1e3ccSAndroid Build Coastguard Worker int intra_cost_penalty = av1_get_intra_cost_penalty(
729*77c1e3ccSAndroid Build Coastguard Worker quant_params->base_qindex, quant_params->y_dc_delta_q,
730*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->bit_depth);
731*77c1e3ccSAndroid Build Coastguard Worker int64_t inter_mode_thresh =
732*77c1e3ccSAndroid Build Coastguard Worker RDCOST(x->rdmult, ref_cost_intra + intra_cost_penalty, 0);
733*77c1e3ccSAndroid Build Coastguard Worker int perform_intra_pred = rt_sf->check_intra_pred_nonrd;
734*77c1e3ccSAndroid Build Coastguard Worker int force_intra_check = 0;
735*77c1e3ccSAndroid Build Coastguard Worker // For spatial enhancement layer: turn off intra prediction if the
736*77c1e3ccSAndroid Build Coastguard Worker // previous spatial layer as golden ref is not chosen as best reference.
737*77c1e3ccSAndroid Build Coastguard Worker // only do this for temporal enhancement layer and on non-key frames.
738*77c1e3ccSAndroid Build Coastguard Worker if (cpi->svc.spatial_layer_id > 0 &&
739*77c1e3ccSAndroid Build Coastguard Worker best_pickmode->best_ref_frame != GOLDEN_FRAME &&
740*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.temporal_layer_id > 0 &&
741*77c1e3ccSAndroid Build Coastguard Worker !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame)
742*77c1e3ccSAndroid Build Coastguard Worker perform_intra_pred = 0;
743*77c1e3ccSAndroid Build Coastguard Worker
744*77c1e3ccSAndroid Build Coastguard Worker int do_early_exit_rdthresh = 1;
745*77c1e3ccSAndroid Build Coastguard Worker
746*77c1e3ccSAndroid Build Coastguard Worker uint32_t spatial_var_thresh = 50;
747*77c1e3ccSAndroid Build Coastguard Worker int motion_thresh = 32;
748*77c1e3ccSAndroid Build Coastguard Worker // Adjust thresholds to make intra mode likely tested if the other
749*77c1e3ccSAndroid Build Coastguard Worker // references (golden, alt) are skipped/not checked. For now always
750*77c1e3ccSAndroid Build Coastguard Worker // adjust for svc mode.
751*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->use_svc || (rt_sf->use_nonrd_altref_frame == 0 &&
752*77c1e3ccSAndroid Build Coastguard Worker rt_sf->nonrd_prune_ref_frame_search > 0)) {
753*77c1e3ccSAndroid Build Coastguard Worker spatial_var_thresh = 150;
754*77c1e3ccSAndroid Build Coastguard Worker motion_thresh = 0;
755*77c1e3ccSAndroid Build Coastguard Worker }
756*77c1e3ccSAndroid Build Coastguard Worker
757*77c1e3ccSAndroid Build Coastguard Worker // Some adjustments to checking intra mode based on source variance.
758*77c1e3ccSAndroid Build Coastguard Worker if (x->source_variance < spatial_var_thresh) {
759*77c1e3ccSAndroid Build Coastguard Worker // If the best inter mode is large motion or non-LAST ref reduce intra cost
760*77c1e3ccSAndroid Build Coastguard Worker // penalty, so intra mode is more likely tested.
761*77c1e3ccSAndroid Build Coastguard Worker if (best_rdc->rdcost != INT64_MAX &&
762*77c1e3ccSAndroid Build Coastguard Worker (best_pickmode->best_ref_frame != LAST_FRAME ||
763*77c1e3ccSAndroid Build Coastguard Worker abs(mi->mv[0].as_mv.row) >= motion_thresh ||
764*77c1e3ccSAndroid Build Coastguard Worker abs(mi->mv[0].as_mv.col) >= motion_thresh)) {
765*77c1e3ccSAndroid Build Coastguard Worker intra_cost_penalty = intra_cost_penalty >> 2;
766*77c1e3ccSAndroid Build Coastguard Worker inter_mode_thresh =
767*77c1e3ccSAndroid Build Coastguard Worker RDCOST(x->rdmult, ref_cost_intra + intra_cost_penalty, 0);
768*77c1e3ccSAndroid Build Coastguard Worker do_early_exit_rdthresh = 0;
769*77c1e3ccSAndroid Build Coastguard Worker }
770*77c1e3ccSAndroid Build Coastguard Worker if ((x->source_variance < AOMMAX(50, (spatial_var_thresh >> 1)) &&
771*77c1e3ccSAndroid Build Coastguard Worker x->content_state_sb.source_sad_nonrd >= kHighSad) ||
772*77c1e3ccSAndroid Build Coastguard Worker (is_screen_content && x->source_variance < 50 &&
773*77c1e3ccSAndroid Build Coastguard Worker ((bsize >= BLOCK_32X32 &&
774*77c1e3ccSAndroid Build Coastguard Worker x->content_state_sb.source_sad_nonrd != kZeroSad) ||
775*77c1e3ccSAndroid Build Coastguard Worker x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
776*77c1e3ccSAndroid Build Coastguard Worker x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)))
777*77c1e3ccSAndroid Build Coastguard Worker force_intra_check = 1;
778*77c1e3ccSAndroid Build Coastguard Worker // For big blocks worth checking intra (since only DC will be checked),
779*77c1e3ccSAndroid Build Coastguard Worker // even if best_early_term is set.
780*77c1e3ccSAndroid Build Coastguard Worker if (bsize >= BLOCK_32X32) best_early_term = 0;
781*77c1e3ccSAndroid Build Coastguard Worker } else if (rt_sf->source_metrics_sb_nonrd &&
782*77c1e3ccSAndroid Build Coastguard Worker x->content_state_sb.source_sad_nonrd <= kLowSad) {
783*77c1e3ccSAndroid Build Coastguard Worker perform_intra_pred = 0;
784*77c1e3ccSAndroid Build Coastguard Worker }
785*77c1e3ccSAndroid Build Coastguard Worker
786*77c1e3ccSAndroid Build Coastguard Worker if (best_rdc->skip_txfm && best_pickmode->best_mode_initial_skip_flag) {
787*77c1e3ccSAndroid Build Coastguard Worker if (rt_sf->skip_intra_pred == 1 && best_pickmode->best_mode != NEWMV)
788*77c1e3ccSAndroid Build Coastguard Worker perform_intra_pred = 0;
789*77c1e3ccSAndroid Build Coastguard Worker else if (rt_sf->skip_intra_pred == 2)
790*77c1e3ccSAndroid Build Coastguard Worker perform_intra_pred = 0;
791*77c1e3ccSAndroid Build Coastguard Worker }
792*77c1e3ccSAndroid Build Coastguard Worker
793*77c1e3ccSAndroid Build Coastguard Worker if (!(best_rdc->rdcost == INT64_MAX || force_intra_check ||
794*77c1e3ccSAndroid Build Coastguard Worker (perform_intra_pred && !best_early_term &&
795*77c1e3ccSAndroid Build Coastguard Worker bsize <= cpi->sf.part_sf.max_intra_bsize))) {
796*77c1e3ccSAndroid Build Coastguard Worker return;
797*77c1e3ccSAndroid Build Coastguard Worker }
798*77c1e3ccSAndroid Build Coastguard Worker
799*77c1e3ccSAndroid Build Coastguard Worker // Early exit based on RD cost calculated using known rate. When
800*77c1e3ccSAndroid Build Coastguard Worker // is_screen_content is true, more bias is given to intra modes. Hence,
801*77c1e3ccSAndroid Build Coastguard Worker // considered conservative threshold in early exit for the same.
802*77c1e3ccSAndroid Build Coastguard Worker const int64_t known_rd = is_screen_content
803*77c1e3ccSAndroid Build Coastguard Worker ? CALC_BIASED_RDCOST(inter_mode_thresh)
804*77c1e3ccSAndroid Build Coastguard Worker : inter_mode_thresh;
805*77c1e3ccSAndroid Build Coastguard Worker if (known_rd > best_rdc->rdcost) return;
806*77c1e3ccSAndroid Build Coastguard Worker
807*77c1e3ccSAndroid Build Coastguard Worker struct estimate_block_intra_args args;
808*77c1e3ccSAndroid Build Coastguard Worker init_estimate_block_intra_args(&args, cpi, x);
809*77c1e3ccSAndroid Build Coastguard Worker TX_SIZE intra_tx_size = AOMMIN(
810*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(max_txsize_lookup[bsize],
811*77c1e3ccSAndroid Build Coastguard Worker tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]),
812*77c1e3ccSAndroid Build Coastguard Worker TX_16X16);
813*77c1e3ccSAndroid Build Coastguard Worker if (is_screen_content && cpi->rc.high_source_sad &&
814*77c1e3ccSAndroid Build Coastguard Worker x->source_variance > spatial_var_thresh && bsize <= BLOCK_16X16)
815*77c1e3ccSAndroid Build Coastguard Worker intra_tx_size = TX_4X4;
816*77c1e3ccSAndroid Build Coastguard Worker
817*77c1e3ccSAndroid Build Coastguard Worker PRED_BUFFER *const best_pred = best_pickmode->best_pred;
818*77c1e3ccSAndroid Build Coastguard Worker if (reuse_prediction && best_pred != NULL) {
819*77c1e3ccSAndroid Build Coastguard Worker const int bh = block_size_high[bsize];
820*77c1e3ccSAndroid Build Coastguard Worker const int bw = block_size_wide[bsize];
821*77c1e3ccSAndroid Build Coastguard Worker if (best_pred->data == orig_dst->buf) {
822*77c1e3ccSAndroid Build Coastguard Worker *this_mode_pred = &tmp_buffers[get_pred_buffer(tmp_buffers, 3)];
823*77c1e3ccSAndroid Build Coastguard Worker aom_convolve_copy(best_pred->data, best_pred->stride,
824*77c1e3ccSAndroid Build Coastguard Worker (*this_mode_pred)->data, (*this_mode_pred)->stride, bw,
825*77c1e3ccSAndroid Build Coastguard Worker bh);
826*77c1e3ccSAndroid Build Coastguard Worker best_pickmode->best_pred = *this_mode_pred;
827*77c1e3ccSAndroid Build Coastguard Worker }
828*77c1e3ccSAndroid Build Coastguard Worker }
829*77c1e3ccSAndroid Build Coastguard Worker pd->dst = *orig_dst;
830*77c1e3ccSAndroid Build Coastguard Worker
831*77c1e3ccSAndroid Build Coastguard Worker for (int midx = 0; midx < RTC_INTRA_MODES; ++midx) {
832*77c1e3ccSAndroid Build Coastguard Worker const PREDICTION_MODE this_mode = intra_mode_list[midx];
833*77c1e3ccSAndroid Build Coastguard Worker const THR_MODES mode_index = mode_idx[INTRA_FRAME][mode_offset(this_mode)];
834*77c1e3ccSAndroid Build Coastguard Worker const int64_t mode_rd_thresh = rd_threshes[mode_index];
835*77c1e3ccSAndroid Build Coastguard Worker
836*77c1e3ccSAndroid Build Coastguard Worker if (is_prune_intra_mode(cpi, midx, force_intra_check, bsize, segment_id,
837*77c1e3ccSAndroid Build Coastguard Worker x->content_state_sb.source_sad_nonrd,
838*77c1e3ccSAndroid Build Coastguard Worker x->color_sensitivity))
839*77c1e3ccSAndroid Build Coastguard Worker continue;
840*77c1e3ccSAndroid Build Coastguard Worker
841*77c1e3ccSAndroid Build Coastguard Worker if (is_screen_content && rt_sf->source_metrics_sb_nonrd) {
842*77c1e3ccSAndroid Build Coastguard Worker // For spatially flat blocks with zero motion only check
843*77c1e3ccSAndroid Build Coastguard Worker // DC mode.
844*77c1e3ccSAndroid Build Coastguard Worker if (x->content_state_sb.source_sad_nonrd == kZeroSad &&
845*77c1e3ccSAndroid Build Coastguard Worker x->source_variance == 0 && this_mode != DC_PRED)
846*77c1e3ccSAndroid Build Coastguard Worker continue;
847*77c1e3ccSAndroid Build Coastguard Worker // Only test Intra for big blocks if spatial_variance is small.
848*77c1e3ccSAndroid Build Coastguard Worker else if (bsize > BLOCK_32X32 && x->source_variance > 50)
849*77c1e3ccSAndroid Build Coastguard Worker continue;
850*77c1e3ccSAndroid Build Coastguard Worker }
851*77c1e3ccSAndroid Build Coastguard Worker
852*77c1e3ccSAndroid Build Coastguard Worker if (rd_less_than_thresh(best_rdc->rdcost, mode_rd_thresh,
853*77c1e3ccSAndroid Build Coastguard Worker rd_thresh_freq_fact[mode_index]) &&
854*77c1e3ccSAndroid Build Coastguard Worker (do_early_exit_rdthresh || this_mode == SMOOTH_PRED)) {
855*77c1e3ccSAndroid Build Coastguard Worker continue;
856*77c1e3ccSAndroid Build Coastguard Worker }
857*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE uv_bsize =
858*77c1e3ccSAndroid Build Coastguard Worker get_plane_block_size(bsize, xd->plane[AOM_PLANE_U].subsampling_x,
859*77c1e3ccSAndroid Build Coastguard Worker xd->plane[AOM_PLANE_U].subsampling_y);
860*77c1e3ccSAndroid Build Coastguard Worker
861*77c1e3ccSAndroid Build Coastguard Worker mi->mode = this_mode;
862*77c1e3ccSAndroid Build Coastguard Worker mi->ref_frame[0] = INTRA_FRAME;
863*77c1e3ccSAndroid Build Coastguard Worker mi->ref_frame[1] = NONE_FRAME;
864*77c1e3ccSAndroid Build Coastguard Worker
865*77c1e3ccSAndroid Build Coastguard Worker av1_invalid_rd_stats(&this_rdc);
866*77c1e3ccSAndroid Build Coastguard Worker args.mode = this_mode;
867*77c1e3ccSAndroid Build Coastguard Worker args.skippable = 1;
868*77c1e3ccSAndroid Build Coastguard Worker args.rdc = &this_rdc;
869*77c1e3ccSAndroid Build Coastguard Worker mi->tx_size = intra_tx_size;
870*77c1e3ccSAndroid Build Coastguard Worker compute_intra_yprediction(cm, this_mode, bsize, x, xd);
871*77c1e3ccSAndroid Build Coastguard Worker // Look into selecting tx_size here, based on prediction residual.
872*77c1e3ccSAndroid Build Coastguard Worker av1_block_yrd(x, &this_rdc, &args.skippable, bsize, mi->tx_size);
873*77c1e3ccSAndroid Build Coastguard Worker // TODO(kyslov@) Need to account for skippable
874*77c1e3ccSAndroid Build Coastguard Worker if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)]) {
875*77c1e3ccSAndroid Build Coastguard Worker av1_foreach_transformed_block_in_plane(xd, uv_bsize, AOM_PLANE_U,
876*77c1e3ccSAndroid Build Coastguard Worker av1_estimate_block_intra, &args);
877*77c1e3ccSAndroid Build Coastguard Worker }
878*77c1e3ccSAndroid Build Coastguard Worker if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) {
879*77c1e3ccSAndroid Build Coastguard Worker av1_foreach_transformed_block_in_plane(xd, uv_bsize, AOM_PLANE_V,
880*77c1e3ccSAndroid Build Coastguard Worker av1_estimate_block_intra, &args);
881*77c1e3ccSAndroid Build Coastguard Worker }
882*77c1e3ccSAndroid Build Coastguard Worker
883*77c1e3ccSAndroid Build Coastguard Worker int mode_cost = 0;
884*77c1e3ccSAndroid Build Coastguard Worker if (av1_is_directional_mode(this_mode) && av1_use_angle_delta(bsize)) {
885*77c1e3ccSAndroid Build Coastguard Worker mode_cost +=
886*77c1e3ccSAndroid Build Coastguard Worker x->mode_costs.angle_delta_cost[this_mode - V_PRED]
887*77c1e3ccSAndroid Build Coastguard Worker [MAX_ANGLE_DELTA +
888*77c1e3ccSAndroid Build Coastguard Worker mi->angle_delta[PLANE_TYPE_Y]];
889*77c1e3ccSAndroid Build Coastguard Worker }
890*77c1e3ccSAndroid Build Coastguard Worker if (this_mode == DC_PRED && av1_filter_intra_allowed_bsize(cm, bsize)) {
891*77c1e3ccSAndroid Build Coastguard Worker mode_cost += x->mode_costs.filter_intra_cost[bsize][0];
892*77c1e3ccSAndroid Build Coastguard Worker }
893*77c1e3ccSAndroid Build Coastguard Worker this_rdc.rate += ref_cost_intra;
894*77c1e3ccSAndroid Build Coastguard Worker this_rdc.rate += intra_cost_penalty;
895*77c1e3ccSAndroid Build Coastguard Worker this_rdc.rate += mode_cost;
896*77c1e3ccSAndroid Build Coastguard Worker this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
897*77c1e3ccSAndroid Build Coastguard Worker
898*77c1e3ccSAndroid Build Coastguard Worker if (is_screen_content && rt_sf->source_metrics_sb_nonrd) {
899*77c1e3ccSAndroid Build Coastguard Worker // For blocks with low spatial variance and color sad,
900*77c1e3ccSAndroid Build Coastguard Worker // favor the intra-modes, only on scene/slide change.
901*77c1e3ccSAndroid Build Coastguard Worker if (cpi->rc.high_source_sad && x->source_variance < 800 &&
902*77c1e3ccSAndroid Build Coastguard Worker (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
903*77c1e3ccSAndroid Build Coastguard Worker x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]))
904*77c1e3ccSAndroid Build Coastguard Worker this_rdc.rdcost = CALC_BIASED_RDCOST(this_rdc.rdcost);
905*77c1e3ccSAndroid Build Coastguard Worker // Otherwise bias against intra for blocks with zero
906*77c1e3ccSAndroid Build Coastguard Worker // motion and no color, on non-scene/slide changes.
907*77c1e3ccSAndroid Build Coastguard Worker else if (!cpi->rc.high_source_sad && x->source_variance > 0 &&
908*77c1e3ccSAndroid Build Coastguard Worker x->content_state_sb.source_sad_nonrd == kZeroSad &&
909*77c1e3ccSAndroid Build Coastguard Worker x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
910*77c1e3ccSAndroid Build Coastguard Worker x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 0)
911*77c1e3ccSAndroid Build Coastguard Worker this_rdc.rdcost = (3 * this_rdc.rdcost) >> 1;
912*77c1e3ccSAndroid Build Coastguard Worker }
913*77c1e3ccSAndroid Build Coastguard Worker
914*77c1e3ccSAndroid Build Coastguard Worker if (this_rdc.rdcost < best_rdc->rdcost) {
915*77c1e3ccSAndroid Build Coastguard Worker *best_rdc = this_rdc;
916*77c1e3ccSAndroid Build Coastguard Worker best_pickmode->best_mode = this_mode;
917*77c1e3ccSAndroid Build Coastguard Worker best_pickmode->best_tx_size = mi->tx_size;
918*77c1e3ccSAndroid Build Coastguard Worker best_pickmode->best_ref_frame = INTRA_FRAME;
919*77c1e3ccSAndroid Build Coastguard Worker best_pickmode->best_second_ref_frame = NONE;
920*77c1e3ccSAndroid Build Coastguard Worker best_pickmode->best_mode_skip_txfm = this_rdc.skip_txfm;
921*77c1e3ccSAndroid Build Coastguard Worker mi->uv_mode = this_mode;
922*77c1e3ccSAndroid Build Coastguard Worker mi->mv[0].as_int = INVALID_MV;
923*77c1e3ccSAndroid Build Coastguard Worker mi->mv[1].as_int = INVALID_MV;
924*77c1e3ccSAndroid Build Coastguard Worker if (!this_rdc.skip_txfm)
925*77c1e3ccSAndroid Build Coastguard Worker memset(ctx->blk_skip, 0,
926*77c1e3ccSAndroid Build Coastguard Worker sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk);
927*77c1e3ccSAndroid Build Coastguard Worker }
928*77c1e3ccSAndroid Build Coastguard Worker }
929*77c1e3ccSAndroid Build Coastguard Worker if (best_pickmode->best_ref_frame == INTRA_FRAME)
930*77c1e3ccSAndroid Build Coastguard Worker memset(ctx->blk_skip, 0,
931*77c1e3ccSAndroid Build Coastguard Worker sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk);
932*77c1e3ccSAndroid Build Coastguard Worker mi->tx_size = best_pickmode->best_tx_size;
933*77c1e3ccSAndroid Build Coastguard Worker }
934