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