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 = blk_row * bw + blk_col;
407 if (!is_blk_skip(x->txfm_search_info.blk_skip, plane, blk_skip_idx) &&
408 !mbmi->skip_mode) {
409 tx_type = av1_get_tx_type(xd, pd->plane_type, blk_row, blk_col, tx_size,
410 cm->features.reduced_tx_set_used);
411 TxfmParam txfm_param;
412 QUANT_PARAM quant_param;
413 const int use_trellis = is_trellis_used(args->enable_optimize_b, dry_run);
414 int quant_idx;
415 if (use_trellis)
416 quant_idx = AV1_XFORM_QUANT_FP;
417 else
418 quant_idx =
419 USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP;
420 av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param);
421 av1_setup_quant(tx_size, use_trellis, quant_idx,
422 cpi->oxcf.q_cfg.quant_b_adapt, &quant_param);
423 av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, tx_type,
424 &quant_param);
425 av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
426 &quant_param);
427
428 // Whether trellis or dropout optimization is required for inter frames.
429 const bool do_trellis = INTER_BLOCK_OPT_TYPE == TRELLIS_OPT ||
430 INTER_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT;
431 const bool do_dropout = INTER_BLOCK_OPT_TYPE == DROPOUT_OPT ||
432 INTER_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT;
433
434 if (quant_param.use_optimize_b && do_trellis) {
435 TXB_CTX txb_ctx;
436 get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
437 av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx,
438 &dummy_rate_cost);
439 }
440 if (!quant_param.use_optimize_b && do_dropout) {
441 av1_dropout_qcoeff(x, plane, block, tx_size, tx_type,
442 cm->quant_params.base_qindex);
443 }
444 } else {
445 p->eobs[block] = 0;
446 p->txb_entropy_ctx[block] = 0;
447 }
448
449 av1_set_txb_context(x, plane, block, tx_size, a, l);
450
451 if (p->eobs[block]) {
452 // As long as any YUV plane has non-zero quantized transform coefficients,
453 // mbmi->skip_txfm flag is set to 0.
454 mbmi->skip_txfm = 0;
455 av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
456 pd->dst.stride, p->eobs[block],
457 cm->features.reduced_tx_set_used);
458 } else {
459 // Only when YUV planes all have zero quantized transform coefficients,
460 // mbmi->skip_txfm flag is set to 1.
461 mbmi->skip_txfm &= 1;
462 }
463
464 // TODO(debargha, jingning): Temporarily disable txk_type check for eob=0
465 // case. It is possible that certain collision in hash index would cause
466 // the assertion failure. To further optimize the rate-distortion
467 // performance, we need to re-visit this part and enable this assert
468 // again.
469 if (p->eobs[block] == 0 && plane == 0) {
470 #if 0
471 if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ &&
472 args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) {
473 // TODO(jingning,angiebird,huisu@google.com): enable txk_check when
474 // enable_optimize_b is true to detect potential RD bug.
475 const uint8_t disable_txk_check = args->enable_optimize_b;
476 if (!disable_txk_check) {
477 assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] ==
478 DCT_DCT);
479 }
480 }
481 #endif
482 update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT);
483 }
484
485 #if CONFIG_MISMATCH_DEBUG
486 if (dry_run == OUTPUT_ENABLED) {
487 int pixel_c, pixel_r;
488 BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
489 int blk_w = block_size_wide[bsize];
490 int blk_h = block_size_high[bsize];
491 mi_to_pixel_loc(&pixel_c, &pixel_r, xd->mi_col, xd->mi_row, blk_col,
492 blk_row, pd->subsampling_x, pd->subsampling_y);
493 mismatch_record_block_tx(dst, pd->dst.stride, cm->current_frame.order_hint,
494 plane, pixel_c, pixel_r, blk_w, blk_h,
495 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
496 }
497 #endif
498 }
499
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)500 static void encode_block_inter(int plane, int block, int blk_row, int blk_col,
501 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
502 void *arg, RUN_TYPE dry_run) {
503 struct encode_b_args *const args = arg;
504 MACROBLOCK *const x = args->x;
505 MACROBLOCKD *const xd = &x->e_mbd;
506 MB_MODE_INFO *const mbmi = xd->mi[0];
507 const struct macroblockd_plane *const pd = &xd->plane[plane];
508 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
509 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
510
511 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
512
513 const TX_SIZE plane_tx_size =
514 plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x,
515 pd->subsampling_y)
516 : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
517 blk_col)];
518 if (!plane) {
519 assert(tx_size_wide[tx_size] >= tx_size_wide[plane_tx_size] &&
520 tx_size_high[tx_size] >= tx_size_high[plane_tx_size]);
521 }
522
523 if (tx_size == plane_tx_size || plane) {
524 encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg,
525 dry_run);
526 } else {
527 assert(tx_size < TX_SIZES_ALL);
528 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
529 assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
530 assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
531 // This is the square transform block partition entry point.
532 const int bsw = tx_size_wide_unit[sub_txs];
533 const int bsh = tx_size_high_unit[sub_txs];
534 const int step = bsh * bsw;
535 const int row_end =
536 AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
537 const int col_end =
538 AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
539 assert(bsw > 0 && bsh > 0);
540
541 for (int row = 0; row < row_end; row += bsh) {
542 const int offsetr = blk_row + row;
543 for (int col = 0; col < col_end; col += bsw) {
544 const int offsetc = blk_col + col;
545
546 encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs,
547 arg, dry_run);
548 block += step;
549 }
550 }
551 }
552 }
553
av1_foreach_transformed_block_in_plane(const MACROBLOCKD * const xd,BLOCK_SIZE plane_bsize,int plane,foreach_transformed_block_visitor visit,void * arg)554 void av1_foreach_transformed_block_in_plane(
555 const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane,
556 foreach_transformed_block_visitor visit, void *arg) {
557 const struct macroblockd_plane *const pd = &xd->plane[plane];
558 // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
559 // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
560 // transform size varies per plane, look it up in a common way.
561 const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
562 const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size];
563 // Call visit() directly with zero offsets if the current block size is the
564 // same as the transform block size.
565 if (plane_bsize == tx_bsize) {
566 visit(plane, 0, 0, 0, plane_bsize, tx_size, arg);
567 return;
568 }
569 const uint8_t txw_unit = tx_size_wide_unit[tx_size];
570 const uint8_t txh_unit = tx_size_high_unit[tx_size];
571 const int step = txw_unit * txh_unit;
572
573 // If mb_to_right_edge is < 0 we are in a situation in which
574 // the current block size extends into the UMV and we won't
575 // visit the sub blocks that are wholly within the UMV.
576 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
577 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
578 const BLOCK_SIZE max_unit_bsize =
579 get_plane_block_size(BLOCK_64X64, pd->subsampling_x, pd->subsampling_y);
580 const int mu_blocks_wide =
581 AOMMIN(mi_size_wide[max_unit_bsize], max_blocks_wide);
582 const int mu_blocks_high =
583 AOMMIN(mi_size_high[max_unit_bsize], max_blocks_high);
584
585 // Keep track of the row and column of the blocks we use so that we know
586 // if we are in the unrestricted motion border.
587 int i = 0;
588 for (int r = 0; r < max_blocks_high; r += mu_blocks_high) {
589 const int unit_height = AOMMIN(mu_blocks_high + r, max_blocks_high);
590 // Skip visiting the sub blocks that are wholly within the UMV.
591 for (int c = 0; c < max_blocks_wide; c += mu_blocks_wide) {
592 const int unit_width = AOMMIN(mu_blocks_wide + c, max_blocks_wide);
593 for (int blk_row = r; blk_row < unit_height; blk_row += txh_unit) {
594 for (int blk_col = c; blk_col < unit_width; blk_col += txw_unit) {
595 visit(plane, i, blk_row, blk_col, plane_bsize, tx_size, arg);
596 i += step;
597 }
598 }
599 }
600 }
601 // Check if visit() is invoked at least once.
602 assert(i >= 1);
603 }
604
605 typedef struct encode_block_pass1_args {
606 AV1_COMP *cpi;
607 MACROBLOCK *x;
608 } encode_block_pass1_args;
609
encode_block_pass1(int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)610 static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
611 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
612 void *arg) {
613 encode_block_pass1_args *args = (encode_block_pass1_args *)arg;
614 AV1_COMP *cpi = args->cpi;
615 AV1_COMMON *cm = &cpi->common;
616 MACROBLOCK *const x = args->x;
617 MACROBLOCKD *const xd = &x->e_mbd;
618 struct macroblock_plane *const p = &x->plane[plane];
619 struct macroblockd_plane *const pd = &xd->plane[plane];
620 tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
621
622 uint8_t *dst;
623 dst = &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
624
625 TxfmParam txfm_param;
626 QUANT_PARAM quant_param;
627
628 av1_setup_xform(cm, x, tx_size, DCT_DCT, &txfm_param);
629 av1_setup_quant(tx_size, 0, AV1_XFORM_QUANT_B, cpi->oxcf.q_cfg.quant_b_adapt,
630 &quant_param);
631 av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, DCT_DCT,
632 &quant_param);
633
634 av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
635 &quant_param);
636
637 if (p->eobs[block] > 0) {
638 txfm_param.eob = p->eobs[block];
639 if (txfm_param.is_hbd) {
640 av1_highbd_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
641 return;
642 }
643 av1_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
644 }
645 }
646
av1_encode_sby_pass1(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize)647 void av1_encode_sby_pass1(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize) {
648 encode_block_pass1_args args = { cpi, x };
649 av1_subtract_plane(x, bsize, 0);
650 av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
651 encode_block_pass1, &args);
652 }
653
av1_encode_sb(const struct AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,RUN_TYPE dry_run)654 void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
655 RUN_TYPE dry_run) {
656 assert(bsize < BLOCK_SIZES_ALL);
657 MACROBLOCKD *const xd = &x->e_mbd;
658 MB_MODE_INFO *mbmi = xd->mi[0];
659 // In the current encoder implementation, for inter blocks,
660 // only when YUV planes all have zero quantized transform coefficients,
661 // mbmi->skip_txfm flag is set to 1.
662 // For intra blocks, this flag is set to 0 since skipped blocks are so rare
663 // that transmitting skip_txfm = 1 is very expensive.
664 // mbmi->skip_txfm is init to 1, and will be modified in encode_block() based
665 // on transform, quantization, and (if exists) trellis optimization.
666 mbmi->skip_txfm = 1;
667 if (x->txfm_search_info.skip_txfm) return;
668
669 struct optimize_ctx ctx;
670 struct encode_b_args arg = {
671 cpi, x, &ctx, NULL, NULL, dry_run, cpi->optimize_seg_arr[mbmi->segment_id]
672 };
673 const AV1_COMMON *const cm = &cpi->common;
674 const int num_planes = av1_num_planes(cm);
675 for (int plane = 0; plane < num_planes; ++plane) {
676 const struct macroblockd_plane *const pd = &xd->plane[plane];
677 const int subsampling_x = pd->subsampling_x;
678 const int subsampling_y = pd->subsampling_y;
679 if (plane && !xd->is_chroma_ref) break;
680 const BLOCK_SIZE plane_bsize =
681 get_plane_block_size(bsize, subsampling_x, subsampling_y);
682 assert(plane_bsize < BLOCK_SIZES_ALL);
683 const int mi_width = mi_size_wide[plane_bsize];
684 const int mi_height = mi_size_high[plane_bsize];
685 const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane);
686 const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
687 const int bw = mi_size_wide[txb_size];
688 const int bh = mi_size_high[txb_size];
689 int block = 0;
690 const int step =
691 tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
692 av1_get_entropy_contexts(plane_bsize, pd, ctx.ta[plane], ctx.tl[plane]);
693 av1_subtract_plane(x, plane_bsize, plane);
694 arg.ta = ctx.ta[plane];
695 arg.tl = ctx.tl[plane];
696 const BLOCK_SIZE max_unit_bsize =
697 get_plane_block_size(BLOCK_64X64, subsampling_x, subsampling_y);
698 int mu_blocks_wide = mi_size_wide[max_unit_bsize];
699 int mu_blocks_high = mi_size_high[max_unit_bsize];
700 mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide);
701 mu_blocks_high = AOMMIN(mi_height, mu_blocks_high);
702
703 for (int idy = 0; idy < mi_height; idy += mu_blocks_high) {
704 for (int idx = 0; idx < mi_width; idx += mu_blocks_wide) {
705 int blk_row, blk_col;
706 const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height);
707 const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width);
708 for (blk_row = idy; blk_row < unit_height; blk_row += bh) {
709 for (blk_col = idx; blk_col < unit_width; blk_col += bw) {
710 encode_block_inter(plane, block, blk_row, blk_col, plane_bsize,
711 max_tx_size, &arg, dry_run);
712 block += step;
713 }
714 }
715 }
716 }
717 }
718 }
719
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)720 static void encode_block_intra_and_set_context(int plane, int block,
721 int blk_row, int blk_col,
722 BLOCK_SIZE plane_bsize,
723 TX_SIZE tx_size, void *arg) {
724 av1_encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size,
725 arg);
726
727 struct encode_b_args *const args = arg;
728 MACROBLOCK *x = args->x;
729 ENTROPY_CONTEXT *a = &args->ta[blk_col];
730 ENTROPY_CONTEXT *l = &args->tl[blk_row];
731 av1_set_txb_context(x, plane, block, tx_size, a, l);
732 }
733
av1_encode_block_intra(int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)734 void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
735 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
736 void *arg) {
737 struct encode_b_args *const args = arg;
738 const AV1_COMP *const cpi = args->cpi;
739 const AV1_COMMON *const cm = &cpi->common;
740 MACROBLOCK *const x = args->x;
741 MACROBLOCKD *const xd = &x->e_mbd;
742 MB_MODE_INFO *mbmi = xd->mi[0];
743 struct macroblock_plane *const p = &x->plane[plane];
744 struct macroblockd_plane *const pd = &xd->plane[plane];
745 tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
746 PLANE_TYPE plane_type = get_plane_type(plane);
747 uint16_t *eob = &p->eobs[block];
748 const int dst_stride = pd->dst.stride;
749 uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2];
750 int dummy_rate_cost = 0;
751
752 av1_predict_intra_block_facade(cm, xd, plane, blk_col, blk_row, tx_size);
753
754 TX_TYPE tx_type = DCT_DCT;
755 const int bw = mi_size_wide[plane_bsize];
756 if (plane == 0 && is_blk_skip(x->txfm_search_info.blk_skip, plane,
757 blk_row * bw + blk_col)) {
758 *eob = 0;
759 p->txb_entropy_ctx[block] = 0;
760 } else {
761 av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size);
762
763 const ENTROPY_CONTEXT *a = &args->ta[blk_col];
764 const ENTROPY_CONTEXT *l = &args->tl[blk_row];
765 tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
766 cm->features.reduced_tx_set_used);
767 TxfmParam txfm_param;
768 QUANT_PARAM quant_param;
769 const int use_trellis =
770 is_trellis_used(args->enable_optimize_b, args->dry_run);
771 int quant_idx;
772 if (use_trellis)
773 quant_idx = AV1_XFORM_QUANT_FP;
774 else
775 quant_idx =
776 USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP;
777
778 av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param);
779 av1_setup_quant(tx_size, use_trellis, quant_idx,
780 cpi->oxcf.q_cfg.quant_b_adapt, &quant_param);
781 av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, tx_type,
782 &quant_param);
783
784 av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
785 &quant_param);
786
787 // Whether trellis or dropout optimization is required for key frames and
788 // intra frames.
789 const bool do_trellis = (frame_is_intra_only(cm) &&
790 (KEY_BLOCK_OPT_TYPE == TRELLIS_OPT ||
791 KEY_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT)) ||
792 (!frame_is_intra_only(cm) &&
793 (INTRA_BLOCK_OPT_TYPE == TRELLIS_OPT ||
794 INTRA_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT));
795 const bool do_dropout = (frame_is_intra_only(cm) &&
796 (KEY_BLOCK_OPT_TYPE == DROPOUT_OPT ||
797 KEY_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT)) ||
798 (!frame_is_intra_only(cm) &&
799 (INTRA_BLOCK_OPT_TYPE == DROPOUT_OPT ||
800 INTRA_BLOCK_OPT_TYPE == TRELLIS_DROPOUT_OPT));
801
802 if (quant_param.use_optimize_b && do_trellis) {
803 TXB_CTX txb_ctx;
804 get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
805 av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx,
806 &dummy_rate_cost);
807 }
808 if (do_dropout) {
809 av1_dropout_qcoeff(x, plane, block, tx_size, tx_type,
810 cm->quant_params.base_qindex);
811 }
812 }
813
814 if (*eob) {
815 av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
816 dst_stride, *eob,
817 cm->features.reduced_tx_set_used);
818 }
819
820 // TODO(jingning): Temporarily disable txk_type check for eob=0 case.
821 // It is possible that certain collision in hash index would cause
822 // the assertion failure. To further optimize the rate-distortion
823 // performance, we need to re-visit this part and enable this assert
824 // again.
825 if (*eob == 0 && plane == 0) {
826 #if 0
827 if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ
828 && args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) {
829 assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] ==
830 DCT_DCT);
831 }
832 #endif
833 update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT);
834 }
835
836 // For intra mode, skipped blocks are so rare that transmitting
837 // skip_txfm = 1 is very expensive.
838 mbmi->skip_txfm = 0;
839
840 if (plane == AOM_PLANE_Y && xd->cfl.store_y) {
841 cfl_store_tx(xd, blk_row, blk_col, tx_size, plane_bsize);
842 }
843 }
844
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)845 void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
846 BLOCK_SIZE bsize, int plane, RUN_TYPE dry_run,
847 TRELLIS_OPT_TYPE enable_optimize_b) {
848 assert(bsize < BLOCK_SIZES_ALL);
849 const MACROBLOCKD *const xd = &x->e_mbd;
850 if (plane && !xd->is_chroma_ref) return;
851
852 const struct macroblockd_plane *const pd = &xd->plane[plane];
853 const int ss_x = pd->subsampling_x;
854 const int ss_y = pd->subsampling_y;
855 ENTROPY_CONTEXT ta[MAX_MIB_SIZE] = { 0 };
856 ENTROPY_CONTEXT tl[MAX_MIB_SIZE] = { 0 };
857 struct encode_b_args arg = {
858 cpi, x, NULL, ta, tl, dry_run, enable_optimize_b
859 };
860 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
861 if (enable_optimize_b) {
862 av1_get_entropy_contexts(plane_bsize, pd, ta, tl);
863 }
864 av1_foreach_transformed_block_in_plane(
865 xd, plane_bsize, plane, encode_block_intra_and_set_context, &arg);
866 }
867