1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include "config/aom_config.h"
13 #include "config/av1_rtcd.h"
14 #include "config/aom_dsp_rtcd.h"
15
16 #include "aom_dsp/bitwriter.h"
17 #include "aom_dsp/quantize.h"
18 #include "aom_mem/aom_mem.h"
19 #include "aom_ports/mem.h"
20
21 #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
22 #include "aom_util/debug_util.h"
23 #endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
24
25 #include "av1/common/cfl.h"
26 #include "av1/common/idct.h"
27 #include "av1/common/reconinter.h"
28 #include "av1/common/reconintra.h"
29 #include "av1/common/scan.h"
30
31 #include "av1/encoder/av1_quantize.h"
32 #include "av1/encoder/encodemb.h"
33 #include "av1/encoder/hybrid_fwd_txfm.h"
34 #include "av1/encoder/txb_rdopt.h"
35 #include "av1/encoder/rd.h"
36 #include "av1/encoder/rdopt.h"
37
av1_subtract_block(BitDepthInfo bd_info,int rows,int cols,int16_t * diff,ptrdiff_t diff_stride,const uint8_t * src8,ptrdiff_t src_stride,const uint8_t * pred8,ptrdiff_t pred_stride)38 void av1_subtract_block(BitDepthInfo bd_info, int rows, int cols, int16_t *diff,
39 ptrdiff_t diff_stride, const uint8_t *src8,
40 ptrdiff_t src_stride, const uint8_t *pred8,
41 ptrdiff_t pred_stride) {
42 assert(rows >= 4 && cols >= 4);
43 #if CONFIG_AV1_HIGHBITDEPTH
44 if (bd_info.use_highbitdepth_buf) {
45 aom_highbd_subtract_block(rows, cols, diff, diff_stride, src8, src_stride,
46 pred8, pred_stride);
47 return;
48 }
49 #endif
50 (void)bd_info;
51 aom_subtract_block(rows, cols, diff, diff_stride, src8, src_stride, pred8,
52 pred_stride);
53 }
54
av1_subtract_txb(MACROBLOCK * x,int plane,BLOCK_SIZE plane_bsize,int blk_col,int blk_row,TX_SIZE tx_size)55 void av1_subtract_txb(MACROBLOCK *x, int plane, BLOCK_SIZE plane_bsize,
56 int blk_col, int blk_row, TX_SIZE tx_size) {
57 MACROBLOCKD *const xd = &x->e_mbd;
58 const BitDepthInfo bd_info = get_bit_depth_info(xd);
59 struct macroblock_plane *const p = &x->plane[plane];
60 const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
61 const int diff_stride = block_size_wide[plane_bsize];
62 const int src_stride = p->src.stride;
63 const int dst_stride = pd->dst.stride;
64 const int tx1d_width = tx_size_wide[tx_size];
65 const int tx1d_height = tx_size_high[tx_size];
66 uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2];
67 uint8_t *src = &p->src.buf[(blk_row * src_stride + blk_col) << MI_SIZE_LOG2];
68 int16_t *src_diff =
69 &p->src_diff[(blk_row * diff_stride + blk_col) << MI_SIZE_LOG2];
70 av1_subtract_block(bd_info, tx1d_height, tx1d_width, src_diff, diff_stride,
71 src, src_stride, dst, dst_stride);
72 }
73
av1_subtract_plane(MACROBLOCK * x,BLOCK_SIZE plane_bsize,int plane)74 void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE plane_bsize, int plane) {
75 struct macroblock_plane *const p = &x->plane[plane];
76 const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
77 assert(plane_bsize < BLOCK_SIZES_ALL);
78 const int bw = block_size_wide[plane_bsize];
79 const int bh = block_size_high[plane_bsize];
80 const MACROBLOCKD *xd = &x->e_mbd;
81 const BitDepthInfo bd_info = get_bit_depth_info(xd);
82
83 av1_subtract_block(bd_info, bh, bw, p->src_diff, bw, p->src.buf,
84 p->src.stride, pd->dst.buf, pd->dst.stride);
85 }
86
av1_optimize_b(const struct AV1_COMP * cpi,MACROBLOCK * x,int plane,int block,TX_SIZE tx_size,TX_TYPE tx_type,const TXB_CTX * const txb_ctx,int * rate_cost)87 int av1_optimize_b(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
88 int block, TX_SIZE tx_size, TX_TYPE tx_type,
89 const TXB_CTX *const txb_ctx, int *rate_cost) {
90 MACROBLOCKD *const xd = &x->e_mbd;
91 struct macroblock_plane *const p = &x->plane[plane];
92 const int eob = p->eobs[block];
93 const int segment_id = xd->mi[0]->segment_id;
94
95 if (eob == 0 || !cpi->optimize_seg_arr[segment_id] ||
96 xd->lossless[segment_id]) {
97 *rate_cost = av1_cost_skip_txb(&x->coeff_costs, txb_ctx, plane, tx_size);
98 return eob;
99 }
100
101 return av1_optimize_txb(cpi, x, plane, block, tx_size, tx_type, txb_ctx,
102 rate_cost, cpi->oxcf.algo_cfg.sharpness);
103 }
104
105 // Hyper-parameters for dropout optimization, based on following logics.
106 // TODO(yjshen): These settings are tuned by experiments. They may still be
107 // optimized for better performance.
108 // (1) Coefficients which are large enough will ALWAYS be kept.
109 const tran_low_t DROPOUT_COEFF_MAX = 2; // Max dropout-able coefficient.
110 // (2) Continuous coefficients will ALWAYS be kept. Here rigorous continuity is
111 // NOT required. For example, `5 0 0 0 7` is treated as two continuous
112 // coefficients if three zeros do not fulfill the dropout condition.
113 const int DROPOUT_CONTINUITY_MAX = 2; // Max dropout-able continuous coeff.
114 // (3) Dropout operation is NOT applicable to blocks with large or small
115 // quantization index.
116 const int DROPOUT_Q_MAX = 128;
117 const int DROPOUT_Q_MIN = 16;
118 // (4) Recall that dropout optimization will forcibly set some quantized
119 // coefficients to zero. The key logic on determining whether a coefficient
120 // should be dropped is to check the number of continuous zeros before AND
121 // after this coefficient. The exact number of zeros for judgement depends
122 // on block size and quantization index. More concretely, block size
123 // determines the base number of zeros, while quantization index determines
124 // the multiplier. Intuitively, larger block requires more zeros and larger
125 // quantization index also requires more zeros (more information is lost
126 // when using larger quantization index).
127 const int DROPOUT_BEFORE_BASE_MAX = 32; // Max base number for leading zeros.
128 const int DROPOUT_BEFORE_BASE_MIN = 16; // Min base number for leading zeros.
129 const int DROPOUT_AFTER_BASE_MAX = 32; // Max base number for trailing zeros.
130 const int DROPOUT_AFTER_BASE_MIN = 16; // Min base number for trailing zeros.
131 const int DROPOUT_MULTIPLIER_MAX = 8; // Max multiplier on number of zeros.
132 const int DROPOUT_MULTIPLIER_MIN = 2; // Min multiplier on number of zeros.
133 const int DROPOUT_MULTIPLIER_Q_BASE = 32; // Base Q to compute multiplier.
134
av1_dropout_qcoeff(MACROBLOCK * mb,int plane,int block,TX_SIZE tx_size,TX_TYPE tx_type,int qindex)135 void av1_dropout_qcoeff(MACROBLOCK *mb, int plane, int block, TX_SIZE tx_size,
136 TX_TYPE tx_type, int qindex) {
137 const int tx_width = tx_size_wide[tx_size];
138 const int tx_height = tx_size_high[tx_size];
139
140 // Early return if `qindex` is out of range.
141 if (qindex > DROPOUT_Q_MAX || qindex < DROPOUT_Q_MIN) {
142 return;
143 }
144
145 // Compute number of zeros used for dropout judgement.
146 const int base_size = AOMMAX(tx_width, tx_height);
147 const int multiplier = CLIP(qindex / DROPOUT_MULTIPLIER_Q_BASE,
148 DROPOUT_MULTIPLIER_MIN, DROPOUT_MULTIPLIER_MAX);
149 const int dropout_num_before =
150 multiplier *
151 CLIP(base_size, DROPOUT_BEFORE_BASE_MIN, DROPOUT_BEFORE_BASE_MAX);
152 const int dropout_num_after =
153 multiplier *
154 CLIP(base_size, DROPOUT_AFTER_BASE_MIN, DROPOUT_AFTER_BASE_MAX);
155
156 av1_dropout_qcoeff_num(mb, plane, block, tx_size, tx_type, dropout_num_before,
157 dropout_num_after);
158 }
159
av1_dropout_qcoeff_num(MACROBLOCK * mb,int plane,int block,TX_SIZE tx_size,TX_TYPE tx_type,int dropout_num_before,int dropout_num_after)160 void av1_dropout_qcoeff_num(MACROBLOCK *mb, int plane, int block,
161 TX_SIZE tx_size, TX_TYPE tx_type,
162 int dropout_num_before, int dropout_num_after) {
163 const struct macroblock_plane *const p = &mb->plane[plane];
164 tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block);
165 tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
166 const int max_eob = av1_get_max_eob(tx_size);
167 const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
168
169 // Early return if there are not enough non-zero coefficients.
170 if (p->eobs[block] == 0 || p->eobs[block] <= dropout_num_before ||
171 max_eob <= dropout_num_before + dropout_num_after) {
172 return;
173 }
174
175 int count_zeros_before = 0;
176 int count_zeros_after = 0;
177 int count_nonzeros = 0;
178 // Index of the first non-zero coefficient after sufficient number of
179 // continuous zeros. If equals to `-1`, it means number of leading zeros
180 // hasn't reach `dropout_num_before`.
181 int idx = -1;
182 int eob = 0; // New end of block.
183
184 for (int i = 0; i < p->eobs[block]; ++i) {
185 const int scan_idx = scan_order->scan[i];
186 if (abs(qcoeff[scan_idx]) > DROPOUT_COEFF_MAX) {
187 // Keep large coefficients.
188 count_zeros_before = 0;
189 count_zeros_after = 0;
190 idx = -1;
191 eob = i + 1;
192 } else if (qcoeff[scan_idx] == 0) { // Count zeros.
193 if (idx == -1) {
194 ++count_zeros_before;
195 } else {
196 ++count_zeros_after;
197 }
198 } else { // Count non-zeros.
199 if (count_zeros_before >= dropout_num_before) {
200 idx = (idx == -1) ? i : idx;
201 ++count_nonzeros;
202 } else {
203 count_zeros_before = 0;
204 eob = i + 1;
205 }
206 }
207
208 // Handle continuity.
209 if (count_nonzeros > DROPOUT_CONTINUITY_MAX) {
210 count_zeros_before = 0;
211 count_zeros_after = 0;
212 count_nonzeros = 0;
213 idx = -1;
214 eob = i + 1;
215 }
216
217 // Handle the trailing zeros after original end of block.
218 if (idx != -1 && i == p->eobs[block] - 1) {
219 count_zeros_after += (max_eob - p->eobs[block]);
220 }
221
222 // Set redundant coefficients to zeros if needed.
223 if (count_zeros_after >= dropout_num_after) {
224 for (int j = idx; j <= i; ++j) {
225 qcoeff[scan_order->scan[j]] = 0;
226 dqcoeff[scan_order->scan[j]] = 0;
227 }
228 count_zeros_before += (i - idx + 1);
229 count_zeros_after = 0;
230 count_nonzeros = 0;
231 } else if (i == p->eobs[block] - 1) {
232 eob = i + 1;
233 }
234 }
235
236 if (eob != p->eobs[block]) {
237 p->eobs[block] = eob;
238 p->txb_entropy_ctx[block] =
239 av1_get_txb_entropy_context(qcoeff, scan_order, eob);
240 }
241 }
242
243 // Settings for optimization type. NOTE: To set optimization type for all intra
244 // frames, both `KEY_BLOCK_OPT_TYPE` and `INTRA_BLOCK_OPT_TYPE` should be set.
245 // TODO(yjshen): These settings are hard-coded and look okay for now. They
246 // should be made configurable later.
247 // Blocks of key frames ONLY.
248 const OPT_TYPE KEY_BLOCK_OPT_TYPE = TRELLIS_DROPOUT_OPT;
249 // Blocks of intra frames (key frames EXCLUSIVE).
250 const OPT_TYPE INTRA_BLOCK_OPT_TYPE = TRELLIS_DROPOUT_OPT;
251 // Blocks of inter frames. (NOTE: Dropout optimization is DISABLED by default
252 // if trellis optimization is on for inter frames.)
253 const OPT_TYPE INTER_BLOCK_OPT_TYPE = TRELLIS_DROPOUT_OPT;
254
255 enum {
256 QUANT_FUNC_LOWBD = 0,
257 QUANT_FUNC_HIGHBD = 1,
258 QUANT_FUNC_TYPES = 2
259 } UENUM1BYTE(QUANT_FUNC);
260
261 #if CONFIG_AV1_HIGHBITDEPTH
262 static AV1_QUANT_FACADE
263 quant_func_list[AV1_XFORM_QUANT_TYPES][QUANT_FUNC_TYPES] = {
264 { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade },
265 { av1_quantize_b_facade, av1_highbd_quantize_b_facade },
266 { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade },
267 { NULL, NULL }
268 };
269 #else
270 static AV1_QUANT_FACADE quant_func_list[AV1_XFORM_QUANT_TYPES] = {
271 av1_quantize_fp_facade, av1_quantize_b_facade, av1_quantize_dc_facade, NULL
272 };
273 #endif
274
275 // Computes the transform for DC only blocks
av1_xform_dc_only(MACROBLOCK * x,int plane,int block,TxfmParam * txfm_param,int64_t per_px_mean)276 void av1_xform_dc_only(MACROBLOCK *x, int plane, int block,
277 TxfmParam *txfm_param, int64_t per_px_mean) {
278 assert(per_px_mean != INT64_MAX);
279 const struct macroblock_plane *const p = &x->plane[plane];
280 const int block_offset = BLOCK_OFFSET(block);
281 tran_low_t *const coeff = p->coeff + block_offset;
282 const int n_coeffs = av1_get_max_eob(txfm_param->tx_size);
283 memset(coeff, 0, sizeof(*coeff) * n_coeffs);
284 coeff[0] =
285 (tran_low_t)((per_px_mean * dc_coeff_scale[txfm_param->tx_size]) >> 12);
286 }
287
av1_xform_quant(MACROBLOCK * x,int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TxfmParam * txfm_param,const QUANT_PARAM * qparam)288 void av1_xform_quant(MACROBLOCK *x, int plane, int block, int blk_row,
289 int blk_col, BLOCK_SIZE plane_bsize, TxfmParam *txfm_param,
290 const QUANT_PARAM *qparam) {
291 av1_xform(x, plane, block, blk_row, blk_col, plane_bsize, txfm_param);
292 av1_quant(x, plane, block, txfm_param, qparam);
293 }
294
av1_xform(MACROBLOCK * x,int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TxfmParam * txfm_param)295 void av1_xform(MACROBLOCK *x, int plane, int block, int blk_row, int blk_col,
296 BLOCK_SIZE plane_bsize, TxfmParam *txfm_param) {
297 const struct macroblock_plane *const p = &x->plane[plane];
298 const int block_offset = BLOCK_OFFSET(block);
299 tran_low_t *const coeff = p->coeff + block_offset;
300 const int diff_stride = block_size_wide[plane_bsize];
301
302 const int src_offset = (blk_row * diff_stride + blk_col);
303 const int16_t *src_diff = &p->src_diff[src_offset << MI_SIZE_LOG2];
304
305 av1_fwd_txfm(src_diff, coeff, diff_stride, txfm_param);
306 }
307
av1_quant(MACROBLOCK * x,int plane,int block,TxfmParam * txfm_param,const QUANT_PARAM * qparam)308 void av1_quant(MACROBLOCK *x, int plane, int block, TxfmParam *txfm_param,
309 const QUANT_PARAM *qparam) {
310 const struct macroblock_plane *const p = &x->plane[plane];
311 const SCAN_ORDER *const scan_order =
312 get_scan(txfm_param->tx_size, txfm_param->tx_type);
313 const int block_offset = BLOCK_OFFSET(block);
314 tran_low_t *const coeff = p->coeff + block_offset;
315 tran_low_t *const qcoeff = p->qcoeff + block_offset;
316 tran_low_t *const dqcoeff = p->dqcoeff + block_offset;
317 uint16_t *const eob = &p->eobs[block];
318
319 if (qparam->xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
320 const int n_coeffs = av1_get_max_eob(txfm_param->tx_size);
321 if (LIKELY(!x->seg_skip_block)) {
322 #if CONFIG_AV1_HIGHBITDEPTH
323 quant_func_list[qparam->xform_quant_idx][txfm_param->is_hbd](
324 coeff, n_coeffs, p, qcoeff, dqcoeff, eob, scan_order, qparam);
325 #else
326 quant_func_list[qparam->xform_quant_idx](
327 coeff, n_coeffs, p, qcoeff, dqcoeff, eob, scan_order, qparam);
328 #endif
329 } else {
330 av1_quantize_skip(n_coeffs, qcoeff, dqcoeff, eob);
331 }
332 }
333 // use_optimize_b is true means av1_optimze_b will be called,
334 // thus cannot update entropy ctx now (performed in optimize_b)
335 if (qparam->use_optimize_b) {
336 p->txb_entropy_ctx[block] = 0;
337 } else {
338 p->txb_entropy_ctx[block] =
339 av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
340 }
341 }
342
av1_setup_xform(const AV1_COMMON * cm,MACROBLOCK * x,TX_SIZE tx_size,TX_TYPE tx_type,TxfmParam * txfm_param)343 void av1_setup_xform(const AV1_COMMON *cm, MACROBLOCK *x, TX_SIZE tx_size,
344 TX_TYPE tx_type, TxfmParam *txfm_param) {
345 MACROBLOCKD *const xd = &x->e_mbd;
346 MB_MODE_INFO *const mbmi = xd->mi[0];
347
348 txfm_param->tx_type = tx_type;
349 txfm_param->tx_size = tx_size;
350 txfm_param->lossless = xd->lossless[mbmi->segment_id];
351 txfm_param->tx_set_type = av1_get_ext_tx_set_type(
352 tx_size, is_inter_block(mbmi), cm->features.reduced_tx_set_used);
353
354 txfm_param->bd = xd->bd;
355 txfm_param->is_hbd = is_cur_buf_hbd(xd);
356 }
av1_setup_quant(TX_SIZE tx_size,int use_optimize_b,int xform_quant_idx,int use_quant_b_adapt,QUANT_PARAM * qparam)357 void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx,
358 int use_quant_b_adapt, QUANT_PARAM *qparam) {
359 qparam->log_scale = av1_get_tx_scale(tx_size);
360 qparam->tx_size = tx_size;
361
362 qparam->use_quant_b_adapt = use_quant_b_adapt;
363
364 // TODO(bohanli): optimize_b and quantization idx has relationship,
365 // but is kind of buried and complicated in different encoding stages.
366 // Should have a unified function to derive quant_idx, rather than
367 // determine and pass in the quant_idx
368 qparam->use_optimize_b = use_optimize_b;
369 qparam->xform_quant_idx = xform_quant_idx;
370
371 qparam->qmatrix = NULL;
372 qparam->iqmatrix = NULL;
373 }
av1_setup_qmatrix(const CommonQuantParams * quant_params,const MACROBLOCKD * xd,int plane,TX_SIZE tx_size,TX_TYPE tx_type,QUANT_PARAM * qparam)374 void av1_setup_qmatrix(const CommonQuantParams *quant_params,
375 const MACROBLOCKD *xd, int plane, TX_SIZE tx_size,
376 TX_TYPE tx_type, QUANT_PARAM *qparam) {
377 qparam->qmatrix = av1_get_qmatrix(quant_params, xd, plane, tx_size, tx_type);
378 qparam->iqmatrix =
379 av1_get_iqmatrix(quant_params, xd, plane, tx_size, tx_type);
380 }
381
encode_block(int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg,RUN_TYPE dry_run)382 static void encode_block(int plane, int block, int blk_row, int blk_col,
383 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg,
384 RUN_TYPE dry_run) {
385 (void)dry_run;
386 struct encode_b_args *const args = arg;
387 const AV1_COMP *const cpi = args->cpi;
388 const AV1_COMMON *const cm = &cpi->common;
389 MACROBLOCK *const x = args->x;
390 MACROBLOCKD *const xd = &x->e_mbd;
391 MB_MODE_INFO *mbmi = xd->mi[0];
392 struct macroblock_plane *const p = &x->plane[plane];
393 struct macroblockd_plane *const pd = &xd->plane[plane];
394 tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
395 uint8_t *dst;
396 ENTROPY_CONTEXT *a, *l;
397 int dummy_rate_cost = 0;
398
399 const int bw = mi_size_wide[plane_bsize];
400 dst = &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
401
402 a = &args->ta[blk_col];
403 l = &args->tl[blk_row];
404
405 TX_TYPE tx_type = DCT_DCT;
406 const int blk_skip_idx =
407 (cpi->sf.rt_sf.use_nonrd_pick_mode && is_inter_block(mbmi))
408 ? blk_row * bw / 4 + blk_col / 2
409 : blk_row * bw + blk_col;
410 if (!is_blk_skip(x->txfm_search_info.blk_skip, plane, blk_skip_idx) &&
411 !mbmi->skip_mode) {
412 tx_type = av1_get_tx_type(xd, pd->plane_type, blk_row, blk_col, tx_size,
413 cm->features.reduced_tx_set_used);
414 TxfmParam txfm_param;
415 QUANT_PARAM quant_param;
416 const int use_trellis = is_trellis_used(args->enable_optimize_b, dry_run);
417 int quant_idx;
418 if (use_trellis)
419 quant_idx = AV1_XFORM_QUANT_FP;
420 else
421 quant_idx =
422 USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP;
423 av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param);
424 av1_setup_quant(tx_size, use_trellis, quant_idx,
425 cpi->oxcf.q_cfg.quant_b_adapt, &quant_param);
426 av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, tx_type,
427 &quant_param);
428 av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
429 &quant_param);
430
431 // Whether trellis or dropout optimization is required for inter frames.
432 const bool do_trellis = INTER_BLOCK_OPT_TYPE == TRELLIS_OPT ||
433 INTER_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT;
434 const bool do_dropout = INTER_BLOCK_OPT_TYPE == DROPOUT_OPT ||
435 INTER_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT;
436
437 if (quant_param.use_optimize_b && do_trellis) {
438 TXB_CTX txb_ctx;
439 get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
440 av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx,
441 &dummy_rate_cost);
442 }
443 if (!quant_param.use_optimize_b && do_dropout) {
444 av1_dropout_qcoeff(x, plane, block, tx_size, tx_type,
445 cm->quant_params.base_qindex);
446 }
447 } else {
448 p->eobs[block] = 0;
449 p->txb_entropy_ctx[block] = 0;
450 }
451
452 av1_set_txb_context(x, plane, block, tx_size, a, l);
453
454 if (p->eobs[block]) {
455 *(args->skip) = 0;
456 av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
457 pd->dst.stride, p->eobs[block],
458 cm->features.reduced_tx_set_used);
459 }
460
461 // TODO(debargha, jingning): Temporarily disable txk_type check for eob=0
462 // case. It is possible that certain collision in hash index would cause
463 // the assertion failure. To further optimize the rate-distortion
464 // performance, we need to re-visit this part and enable this assert
465 // again.
466 if (p->eobs[block] == 0 && plane == 0) {
467 #if 0
468 if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ &&
469 args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) {
470 // TODO(jingning,angiebird,huisu@google.com): enable txk_check when
471 // enable_optimize_b is true to detect potential RD bug.
472 const uint8_t disable_txk_check = args->enable_optimize_b;
473 if (!disable_txk_check) {
474 assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] ==
475 DCT_DCT);
476 }
477 }
478 #endif
479 update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT);
480 }
481
482 #if CONFIG_MISMATCH_DEBUG
483 if (dry_run == OUTPUT_ENABLED) {
484 int pixel_c, pixel_r;
485 BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
486 int blk_w = block_size_wide[bsize];
487 int blk_h = block_size_high[bsize];
488 mi_to_pixel_loc(&pixel_c, &pixel_r, xd->mi_col, xd->mi_row, blk_col,
489 blk_row, pd->subsampling_x, pd->subsampling_y);
490 mismatch_record_block_tx(dst, pd->dst.stride, cm->current_frame.order_hint,
491 plane, pixel_c, pixel_r, blk_w, blk_h,
492 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
493 }
494 #endif
495 }
496
encode_block_inter(int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg,RUN_TYPE dry_run)497 static void encode_block_inter(int plane, int block, int blk_row, int blk_col,
498 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
499 void *arg, RUN_TYPE dry_run) {
500 struct encode_b_args *const args = arg;
501 MACROBLOCK *const x = args->x;
502 MACROBLOCKD *const xd = &x->e_mbd;
503 MB_MODE_INFO *const mbmi = xd->mi[0];
504 const struct macroblockd_plane *const pd = &xd->plane[plane];
505 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
506 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
507
508 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
509
510 const TX_SIZE plane_tx_size =
511 plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x,
512 pd->subsampling_y)
513 : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
514 blk_col)];
515 if (!plane) {
516 assert(tx_size_wide[tx_size] >= tx_size_wide[plane_tx_size] &&
517 tx_size_high[tx_size] >= tx_size_high[plane_tx_size]);
518 }
519
520 if (tx_size == plane_tx_size || plane) {
521 encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg,
522 dry_run);
523 } else {
524 assert(tx_size < TX_SIZES_ALL);
525 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
526 assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
527 assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
528 // This is the square transform block partition entry point.
529 const int bsw = tx_size_wide_unit[sub_txs];
530 const int bsh = tx_size_high_unit[sub_txs];
531 const int step = bsh * bsw;
532 const int row_end =
533 AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
534 const int col_end =
535 AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
536 assert(bsw > 0 && bsh > 0);
537
538 for (int row = 0; row < row_end; row += bsh) {
539 const int offsetr = blk_row + row;
540 for (int col = 0; col < col_end; col += bsw) {
541 const int offsetc = blk_col + col;
542
543 encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs,
544 arg, dry_run);
545 block += step;
546 }
547 }
548 }
549 }
550
av1_foreach_transformed_block_in_plane(const MACROBLOCKD * const xd,BLOCK_SIZE plane_bsize,int plane,foreach_transformed_block_visitor visit,void * arg)551 void av1_foreach_transformed_block_in_plane(
552 const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane,
553 foreach_transformed_block_visitor visit, void *arg) {
554 const struct macroblockd_plane *const pd = &xd->plane[plane];
555 // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
556 // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
557 // transform size varies per plane, look it up in a common way.
558 const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
559 const uint8_t txw_unit = tx_size_wide_unit[tx_size];
560 const uint8_t txh_unit = tx_size_high_unit[tx_size];
561 const int step = txw_unit * txh_unit;
562
563 // If mb_to_right_edge is < 0 we are in a situation in which
564 // the current block size extends into the UMV and we won't
565 // visit the sub blocks that are wholly within the UMV.
566 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
567 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
568 const BLOCK_SIZE max_unit_bsize =
569 get_plane_block_size(BLOCK_64X64, pd->subsampling_x, pd->subsampling_y);
570 const int mu_blocks_wide =
571 AOMMIN(mi_size_wide[max_unit_bsize], max_blocks_wide);
572 const int mu_blocks_high =
573 AOMMIN(mi_size_high[max_unit_bsize], max_blocks_high);
574
575 // Keep track of the row and column of the blocks we use so that we know
576 // if we are in the unrestricted motion border.
577 int i = 0;
578 for (int r = 0; r < max_blocks_high; r += mu_blocks_high) {
579 const int unit_height = AOMMIN(mu_blocks_high + r, max_blocks_high);
580 // Skip visiting the sub blocks that are wholly within the UMV.
581 for (int c = 0; c < max_blocks_wide; c += mu_blocks_wide) {
582 const int unit_width = AOMMIN(mu_blocks_wide + c, max_blocks_wide);
583 for (int blk_row = r; blk_row < unit_height; blk_row += txh_unit) {
584 for (int blk_col = c; blk_col < unit_width; blk_col += txw_unit) {
585 visit(plane, i, blk_row, blk_col, plane_bsize, tx_size, arg);
586 i += step;
587 }
588 }
589 }
590 }
591 }
592
593 typedef struct encode_block_pass1_args {
594 AV1_COMP *cpi;
595 MACROBLOCK *x;
596 } encode_block_pass1_args;
597
encode_block_pass1(int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)598 static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
599 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
600 void *arg) {
601 encode_block_pass1_args *args = (encode_block_pass1_args *)arg;
602 AV1_COMP *cpi = args->cpi;
603 AV1_COMMON *cm = &cpi->common;
604 MACROBLOCK *const x = args->x;
605 MACROBLOCKD *const xd = &x->e_mbd;
606 struct macroblock_plane *const p = &x->plane[plane];
607 struct macroblockd_plane *const pd = &xd->plane[plane];
608 tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
609
610 uint8_t *dst;
611 dst = &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
612
613 TxfmParam txfm_param;
614 QUANT_PARAM quant_param;
615
616 av1_setup_xform(cm, x, tx_size, DCT_DCT, &txfm_param);
617 av1_setup_quant(tx_size, 0, AV1_XFORM_QUANT_B, cpi->oxcf.q_cfg.quant_b_adapt,
618 &quant_param);
619 av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, DCT_DCT,
620 &quant_param);
621
622 av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
623 &quant_param);
624
625 if (p->eobs[block] > 0) {
626 txfm_param.eob = p->eobs[block];
627 if (txfm_param.is_hbd) {
628 av1_highbd_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
629 return;
630 }
631 av1_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
632 }
633 }
634
av1_encode_sby_pass1(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize)635 void av1_encode_sby_pass1(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize) {
636 encode_block_pass1_args args = { cpi, x };
637 av1_subtract_plane(x, bsize, 0);
638 av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
639 encode_block_pass1, &args);
640 }
641
av1_encode_sb(const struct AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,RUN_TYPE dry_run)642 void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
643 RUN_TYPE dry_run) {
644 assert(bsize < BLOCK_SIZES_ALL);
645 MACROBLOCKD *const xd = &x->e_mbd;
646 MB_MODE_INFO *mbmi = xd->mi[0];
647 mbmi->skip_txfm = 1;
648 if (x->txfm_search_info.skip_txfm) return;
649
650 struct optimize_ctx ctx;
651 struct encode_b_args arg = {
652 cpi, x, &ctx, &mbmi->skip_txfm,
653 NULL, NULL, dry_run, cpi->optimize_seg_arr[mbmi->segment_id]
654 };
655 const AV1_COMMON *const cm = &cpi->common;
656 const int num_planes = av1_num_planes(cm);
657 for (int plane = 0; plane < num_planes; ++plane) {
658 const struct macroblockd_plane *const pd = &xd->plane[plane];
659 const int subsampling_x = pd->subsampling_x;
660 const int subsampling_y = pd->subsampling_y;
661 if (plane && !xd->is_chroma_ref) break;
662 const BLOCK_SIZE plane_bsize =
663 get_plane_block_size(bsize, subsampling_x, subsampling_y);
664 assert(plane_bsize < BLOCK_SIZES_ALL);
665 const int mi_width = mi_size_wide[plane_bsize];
666 const int mi_height = mi_size_high[plane_bsize];
667 const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane);
668 const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
669 const int bw = mi_size_wide[txb_size];
670 const int bh = mi_size_high[txb_size];
671 int block = 0;
672 const int step =
673 tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
674 av1_get_entropy_contexts(plane_bsize, pd, ctx.ta[plane], ctx.tl[plane]);
675 av1_subtract_plane(x, plane_bsize, plane);
676 arg.ta = ctx.ta[plane];
677 arg.tl = ctx.tl[plane];
678 const BLOCK_SIZE max_unit_bsize =
679 get_plane_block_size(BLOCK_64X64, subsampling_x, subsampling_y);
680 int mu_blocks_wide = mi_size_wide[max_unit_bsize];
681 int mu_blocks_high = mi_size_high[max_unit_bsize];
682 mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide);
683 mu_blocks_high = AOMMIN(mi_height, mu_blocks_high);
684
685 for (int idy = 0; idy < mi_height; idy += mu_blocks_high) {
686 for (int idx = 0; idx < mi_width; idx += mu_blocks_wide) {
687 int blk_row, blk_col;
688 const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height);
689 const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width);
690 for (blk_row = idy; blk_row < unit_height; blk_row += bh) {
691 for (blk_col = idx; blk_col < unit_width; blk_col += bw) {
692 encode_block_inter(plane, block, blk_row, blk_col, plane_bsize,
693 max_tx_size, &arg, dry_run);
694 block += step;
695 }
696 }
697 }
698 }
699 }
700 }
701
encode_block_intra_and_set_context(int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)702 static void encode_block_intra_and_set_context(int plane, int block,
703 int blk_row, int blk_col,
704 BLOCK_SIZE plane_bsize,
705 TX_SIZE tx_size, void *arg) {
706 av1_encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size,
707 arg);
708
709 struct encode_b_args *const args = arg;
710 MACROBLOCK *x = args->x;
711 ENTROPY_CONTEXT *a = &args->ta[blk_col];
712 ENTROPY_CONTEXT *l = &args->tl[blk_row];
713 av1_set_txb_context(x, plane, block, tx_size, a, l);
714 }
715
av1_encode_block_intra(int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)716 void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
717 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
718 void *arg) {
719 struct encode_b_args *const args = arg;
720 const AV1_COMP *const cpi = args->cpi;
721 const AV1_COMMON *const cm = &cpi->common;
722 MACROBLOCK *const x = args->x;
723 MACROBLOCKD *const xd = &x->e_mbd;
724 struct macroblock_plane *const p = &x->plane[plane];
725 struct macroblockd_plane *const pd = &xd->plane[plane];
726 tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
727 PLANE_TYPE plane_type = get_plane_type(plane);
728 uint16_t *eob = &p->eobs[block];
729 const int dst_stride = pd->dst.stride;
730 uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2];
731 int dummy_rate_cost = 0;
732
733 av1_predict_intra_block_facade(cm, xd, plane, blk_col, blk_row, tx_size);
734
735 TX_TYPE tx_type = DCT_DCT;
736 const int bw = mi_size_wide[plane_bsize];
737 if (plane == 0 && is_blk_skip(x->txfm_search_info.blk_skip, plane,
738 blk_row * bw + blk_col)) {
739 *eob = 0;
740 p->txb_entropy_ctx[block] = 0;
741 } else {
742 av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size);
743
744 const ENTROPY_CONTEXT *a = &args->ta[blk_col];
745 const ENTROPY_CONTEXT *l = &args->tl[blk_row];
746 tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
747 cm->features.reduced_tx_set_used);
748 TxfmParam txfm_param;
749 QUANT_PARAM quant_param;
750 const int use_trellis =
751 is_trellis_used(args->enable_optimize_b, args->dry_run);
752 int quant_idx;
753 if (use_trellis)
754 quant_idx = AV1_XFORM_QUANT_FP;
755 else
756 quant_idx =
757 USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP;
758
759 av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param);
760 av1_setup_quant(tx_size, use_trellis, quant_idx,
761 cpi->oxcf.q_cfg.quant_b_adapt, &quant_param);
762 av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, tx_type,
763 &quant_param);
764
765 av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
766 &quant_param);
767
768 // Whether trellis or dropout optimization is required for key frames and
769 // intra frames.
770 const bool do_trellis = (frame_is_intra_only(cm) &&
771 (KEY_BLOCK_OPT_TYPE == TRELLIS_OPT ||
772 KEY_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT)) ||
773 (!frame_is_intra_only(cm) &&
774 (INTRA_BLOCK_OPT_TYPE == TRELLIS_OPT ||
775 INTRA_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT));
776 const bool do_dropout = (frame_is_intra_only(cm) &&
777 (KEY_BLOCK_OPT_TYPE == DROPOUT_OPT ||
778 KEY_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT)) ||
779 (!frame_is_intra_only(cm) &&
780 (INTRA_BLOCK_OPT_TYPE == DROPOUT_OPT ||
781 INTRA_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT));
782
783 if (quant_param.use_optimize_b && do_trellis) {
784 TXB_CTX txb_ctx;
785 get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
786 av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx,
787 &dummy_rate_cost);
788 }
789 if (do_dropout) {
790 av1_dropout_qcoeff(x, plane, block, tx_size, tx_type,
791 cm->quant_params.base_qindex);
792 }
793 }
794
795 if (*eob) {
796 av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
797 dst_stride, *eob,
798 cm->features.reduced_tx_set_used);
799 }
800
801 // TODO(jingning): Temporarily disable txk_type check for eob=0 case.
802 // It is possible that certain collision in hash index would cause
803 // the assertion failure. To further optimize the rate-distortion
804 // performance, we need to re-visit this part and enable this assert
805 // again.
806 if (*eob == 0 && plane == 0) {
807 #if 0
808 if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ
809 && args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) {
810 assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] ==
811 DCT_DCT);
812 }
813 #endif
814 update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT);
815 }
816
817 // For intra mode, skipped blocks are so rare that transmitting skip=1 is
818 // very expensive.
819 *(args->skip) = 0;
820
821 if (plane == AOM_PLANE_Y && xd->cfl.store_y) {
822 cfl_store_tx(xd, blk_row, blk_col, tx_size, plane_bsize);
823 }
824 }
825
av1_encode_intra_block_plane(const struct AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int plane,RUN_TYPE dry_run,TRELLIS_OPT_TYPE enable_optimize_b)826 void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
827 BLOCK_SIZE bsize, int plane, RUN_TYPE dry_run,
828 TRELLIS_OPT_TYPE enable_optimize_b) {
829 assert(bsize < BLOCK_SIZES_ALL);
830 const MACROBLOCKD *const xd = &x->e_mbd;
831 if (plane && !xd->is_chroma_ref) return;
832
833 const struct macroblockd_plane *const pd = &xd->plane[plane];
834 const int ss_x = pd->subsampling_x;
835 const int ss_y = pd->subsampling_y;
836 ENTROPY_CONTEXT ta[MAX_MIB_SIZE] = { 0 };
837 ENTROPY_CONTEXT tl[MAX_MIB_SIZE] = { 0 };
838 struct encode_b_args arg = { cpi, x, NULL, &(xd->mi[0]->skip_txfm),
839 ta, tl, dry_run, enable_optimize_b };
840 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
841 if (enable_optimize_b) {
842 av1_get_entropy_contexts(plane_bsize, pd, ta, tl);
843 }
844 av1_foreach_transformed_block_in_plane(
845 xd, plane_bsize, plane, encode_block_intra_and_set_context, &arg);
846 }
847