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
38 // Check if one needs to use c version subtraction.
check_subtract_block_size(int w,int h)39 static int check_subtract_block_size(int w, int h) { return w < 4 || h < 4; }
40
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)41 static void subtract_block(const MACROBLOCKD *xd, int rows, int cols,
42 int16_t *diff, ptrdiff_t diff_stride,
43 const uint8_t *src8, ptrdiff_t src_stride,
44 const uint8_t *pred8, ptrdiff_t pred_stride) {
45 if (check_subtract_block_size(rows, cols)) {
46 if (is_cur_buf_hbd(xd)) {
47 aom_highbd_subtract_block_c(rows, cols, diff, diff_stride, src8,
48 src_stride, pred8, pred_stride, xd->bd);
49 return;
50 }
51 aom_subtract_block_c(rows, cols, diff, diff_stride, src8, src_stride, pred8,
52 pred_stride);
53
54 return;
55 }
56
57 if (is_cur_buf_hbd(xd)) {
58 aom_highbd_subtract_block(rows, cols, diff, diff_stride, src8, src_stride,
59 pred8, pred_stride, xd->bd);
60 return;
61 }
62 aom_subtract_block(rows, cols, diff, diff_stride, src8, src_stride, pred8,
63 pred_stride);
64 }
65
av1_subtract_txb(MACROBLOCK * x,int plane,BLOCK_SIZE plane_bsize,int blk_col,int blk_row,TX_SIZE tx_size)66 void av1_subtract_txb(MACROBLOCK *x, int plane, BLOCK_SIZE plane_bsize,
67 int blk_col, int blk_row, TX_SIZE tx_size) {
68 MACROBLOCKD *const xd = &x->e_mbd;
69 struct macroblock_plane *const p = &x->plane[plane];
70 const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
71 const int diff_stride = block_size_wide[plane_bsize];
72 const int src_stride = p->src.stride;
73 const int dst_stride = pd->dst.stride;
74 const int tx1d_width = tx_size_wide[tx_size];
75 const int tx1d_height = tx_size_high[tx_size];
76 uint8_t *dst =
77 &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
78 uint8_t *src =
79 &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]];
80 int16_t *src_diff =
81 &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
82 subtract_block(xd, tx1d_height, tx1d_width, src_diff, diff_stride, src,
83 src_stride, dst, dst_stride);
84 }
85
av1_subtract_plane(MACROBLOCK * x,BLOCK_SIZE bsize,int plane)86 void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
87 struct macroblock_plane *const p = &x->plane[plane];
88 const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
89 const BLOCK_SIZE plane_bsize =
90 get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
91 const int bw = block_size_wide[plane_bsize];
92 const int bh = block_size_high[plane_bsize];
93 const MACROBLOCKD *xd = &x->e_mbd;
94
95 subtract_block(xd, bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
96 pd->dst.buf, pd->dst.stride);
97 }
98
av1_optimize_b(const struct AV1_COMP * cpi,MACROBLOCK * mb,int plane,int block,TX_SIZE tx_size,TX_TYPE tx_type,const TXB_CTX * const txb_ctx,int fast_mode,int * rate_cost)99 int av1_optimize_b(const struct AV1_COMP *cpi, MACROBLOCK *mb, int plane,
100 int block, TX_SIZE tx_size, TX_TYPE tx_type,
101 const TXB_CTX *const txb_ctx, int fast_mode,
102 int *rate_cost) {
103 MACROBLOCKD *const xd = &mb->e_mbd;
104 struct macroblock_plane *const p = &mb->plane[plane];
105 const int eob = p->eobs[block];
106 const int segment_id = xd->mi[0]->segment_id;
107
108 if (eob == 0 || !cpi->optimize_seg_arr[segment_id] ||
109 xd->lossless[segment_id]) {
110 *rate_cost = av1_cost_skip_txb(mb, txb_ctx, plane, tx_size);
111 return eob;
112 }
113
114 return av1_optimize_txb_new(cpi, mb, plane, block, tx_size, tx_type, txb_ctx,
115 rate_cost, cpi->oxcf.sharpness, fast_mode);
116 }
117
118 enum {
119 QUANT_FUNC_LOWBD = 0,
120 QUANT_FUNC_HIGHBD = 1,
121 QUANT_FUNC_TYPES = 2
122 } UENUM1BYTE(QUANT_FUNC);
123
124 static AV1_QUANT_FACADE
125 quant_func_list[AV1_XFORM_QUANT_TYPES][QUANT_FUNC_TYPES] = {
126 { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade },
127 { av1_quantize_b_facade, av1_highbd_quantize_b_facade },
128 { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade },
129 { NULL, NULL }
130 };
131
av1_xform_quant(const AV1_COMMON * cm,MACROBLOCK * x,int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,TX_TYPE tx_type,AV1_XFORM_QUANT xform_quant_idx)132 void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
133 int blk_row, int blk_col, BLOCK_SIZE plane_bsize,
134 TX_SIZE tx_size, TX_TYPE tx_type,
135 AV1_XFORM_QUANT xform_quant_idx) {
136 MACROBLOCKD *const xd = &x->e_mbd;
137 MB_MODE_INFO *const mbmi = xd->mi[0];
138 const struct macroblock_plane *const p = &x->plane[plane];
139 const struct macroblockd_plane *const pd = &xd->plane[plane];
140 const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
141
142 tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
143 tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
144 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
145 uint16_t *const eob = &p->eobs[block];
146 const int diff_stride = block_size_wide[plane_bsize];
147 int seg_id = mbmi->segment_id;
148 const TX_SIZE qm_tx_size = av1_get_adjusted_tx_size(tx_size);
149 // Use a flat matrix (i.e. no weighting) for 1D and Identity transforms
150 const qm_val_t *qmatrix =
151 IS_2D_TRANSFORM(tx_type) ? pd->seg_qmatrix[seg_id][qm_tx_size]
152 : cm->gqmatrix[NUM_QM_LEVELS - 1][0][qm_tx_size];
153 const qm_val_t *iqmatrix =
154 IS_2D_TRANSFORM(tx_type)
155 ? pd->seg_iqmatrix[seg_id][qm_tx_size]
156 : cm->giqmatrix[NUM_QM_LEVELS - 1][0][qm_tx_size];
157
158 const int src_offset = (blk_row * diff_stride + blk_col);
159 const int16_t *src_diff = &p->src_diff[src_offset << tx_size_wide_log2[0]];
160 QUANT_PARAM qparam;
161 qparam.log_scale = av1_get_tx_scale(tx_size);
162 qparam.tx_size = tx_size;
163 qparam.qmatrix = qmatrix;
164 qparam.iqmatrix = iqmatrix;
165 qparam.use_quant_b_adapt = cm->use_quant_b_adapt;
166 TxfmParam txfm_param;
167 txfm_param.tx_type = tx_type;
168 txfm_param.tx_size = tx_size;
169 txfm_param.lossless = xd->lossless[mbmi->segment_id];
170 txfm_param.tx_set_type = av1_get_ext_tx_set_type(
171 txfm_param.tx_size, is_inter_block(mbmi), cm->reduced_tx_set_used);
172
173 txfm_param.bd = xd->bd;
174 txfm_param.is_hbd = is_cur_buf_hbd(xd);
175
176 av1_fwd_txfm(src_diff, coeff, diff_stride, &txfm_param);
177
178 if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
179 const int n_coeffs = av1_get_max_eob(tx_size);
180 if (LIKELY(!x->skip_block)) {
181 quant_func_list[xform_quant_idx][txfm_param.is_hbd](
182 coeff, n_coeffs, p, qcoeff, dqcoeff, eob, scan_order, &qparam);
183 } else {
184 av1_quantize_skip(n_coeffs, qcoeff, dqcoeff, eob);
185 }
186 }
187 // NOTE: optimize_b_following is true means av1_optimze_b will be called
188 // When the condition of doing optimize_b is changed,
189 // this flag need update simultaneously
190 const int optimize_b_following =
191 (xform_quant_idx != AV1_XFORM_QUANT_FP) || (txfm_param.lossless);
192 if (optimize_b_following) {
193 p->txb_entropy_ctx[block] =
194 (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
195 } else {
196 p->txb_entropy_ctx[block] = 0;
197 }
198 return;
199 }
200
encode_block(int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg,int mi_row,int mi_col,RUN_TYPE dry_run)201 static void encode_block(int plane, int block, int blk_row, int blk_col,
202 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg,
203 int mi_row, int mi_col, RUN_TYPE dry_run) {
204 (void)mi_row;
205 (void)mi_col;
206 (void)dry_run;
207 struct encode_b_args *const args = arg;
208 const AV1_COMMON *const cm = &args->cpi->common;
209 MACROBLOCK *const x = args->x;
210 MACROBLOCKD *const xd = &x->e_mbd;
211 MB_MODE_INFO *mbmi = xd->mi[0];
212 struct macroblock_plane *const p = &x->plane[plane];
213 struct macroblockd_plane *const pd = &xd->plane[plane];
214 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
215 uint8_t *dst;
216 ENTROPY_CONTEXT *a, *l;
217 int dummy_rate_cost = 0;
218
219 const int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
220 dst = &pd->dst
221 .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
222
223 a = &args->ta[blk_col];
224 l = &args->tl[blk_row];
225
226 if (!is_blk_skip(x, plane, blk_row * bw + blk_col) && !mbmi->skip_mode) {
227 TX_TYPE tx_type = av1_get_tx_type(pd->plane_type, xd, blk_row, blk_col,
228 tx_size, cm->reduced_tx_set_used);
229 if (args->enable_optimize_b != NO_TRELLIS_OPT) {
230 av1_xform_quant(
231 cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, tx_type,
232 USE_B_QUANT_NO_TRELLIS &&
233 (args->enable_optimize_b == FINAL_PASS_TRELLIS_OPT)
234 ? AV1_XFORM_QUANT_B
235 : AV1_XFORM_QUANT_FP);
236 TXB_CTX txb_ctx;
237 get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
238 av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx,
239 args->cpi->sf.trellis_eob_fast, &dummy_rate_cost);
240 } else {
241 av1_xform_quant(
242 cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, tx_type,
243 USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP);
244 }
245 } else {
246 p->eobs[block] = 0;
247 p->txb_entropy_ctx[block] = 0;
248 }
249
250 av1_set_txb_context(x, plane, block, tx_size, a, l);
251
252 if (p->eobs[block]) {
253 *(args->skip) = 0;
254
255 TX_TYPE tx_type = av1_get_tx_type(pd->plane_type, xd, blk_row, blk_col,
256 tx_size, cm->reduced_tx_set_used);
257 av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
258 pd->dst.stride, p->eobs[block],
259 cm->reduced_tx_set_used);
260 }
261
262 // TODO(debargha, jingning): Temporarily disable txk_type check for eob=0
263 // case. It is possible that certain collision in hash index would cause
264 // the assertion failure. To further optimize the rate-distortion
265 // performance, we need to re-visit this part and enable this assert
266 // again.
267 if (p->eobs[block] == 0 && plane == 0) {
268 #if 0
269 if (args->cpi->oxcf.aq_mode == NO_AQ &&
270 args->cpi->oxcf.deltaq_mode == NO_DELTA_Q) {
271 // TODO(jingning,angiebird,huisu@google.com): enable txk_check when
272 // enable_optimize_b is true to detect potential RD bug.
273 const uint8_t disable_txk_check = args->enable_optimize_b;
274 if (!disable_txk_check) {
275 assert(mbmi->txk_type[av1_get_txk_type_index(plane_bsize, blk_row,
276 blk_col)] == DCT_DCT);
277 }
278 }
279 #endif
280 update_txk_array(mbmi->txk_type, plane_bsize, blk_row, blk_col, tx_size,
281 DCT_DCT);
282 }
283
284 #if CONFIG_MISMATCH_DEBUG
285 if (dry_run == OUTPUT_ENABLED) {
286 int pixel_c, pixel_r;
287 BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
288 int blk_w = block_size_wide[bsize];
289 int blk_h = block_size_high[bsize];
290 mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, blk_col, blk_row,
291 pd->subsampling_x, pd->subsampling_y);
292 mismatch_record_block_tx(dst, pd->dst.stride, cm->current_frame.order_hint,
293 plane, pixel_c, pixel_r, blk_w, blk_h,
294 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
295 }
296 #endif
297 }
298
encode_block_inter(int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg,int mi_row,int mi_col,RUN_TYPE dry_run)299 static void encode_block_inter(int plane, int block, int blk_row, int blk_col,
300 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
301 void *arg, int mi_row, int mi_col,
302 RUN_TYPE dry_run) {
303 (void)mi_row;
304 (void)mi_col;
305 struct encode_b_args *const args = arg;
306 MACROBLOCK *const x = args->x;
307 MACROBLOCKD *const xd = &x->e_mbd;
308 MB_MODE_INFO *const mbmi = xd->mi[0];
309 const struct macroblockd_plane *const pd = &xd->plane[plane];
310 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
311 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
312
313 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
314
315 const TX_SIZE plane_tx_size =
316 plane ? av1_get_max_uv_txsize(mbmi->sb_type, pd->subsampling_x,
317 pd->subsampling_y)
318 : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
319 blk_col)];
320 if (!plane) {
321 assert(tx_size_wide[tx_size] >= tx_size_wide[plane_tx_size] &&
322 tx_size_high[tx_size] >= tx_size_high[plane_tx_size]);
323 }
324
325 if (tx_size == plane_tx_size || plane) {
326 encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg,
327 mi_row, mi_col, dry_run);
328 } else {
329 assert(tx_size < TX_SIZES_ALL);
330 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
331 assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
332 assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
333 // This is the square transform block partition entry point.
334 const int bsw = tx_size_wide_unit[sub_txs];
335 const int bsh = tx_size_high_unit[sub_txs];
336 const int step = bsh * bsw;
337 assert(bsw > 0 && bsh > 0);
338
339 for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
340 for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
341 const int offsetr = blk_row + row;
342 const int offsetc = blk_col + col;
343
344 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
345
346 encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs,
347 arg, mi_row, mi_col, dry_run);
348 block += step;
349 }
350 }
351 }
352 }
353
av1_foreach_transformed_block_in_plane(const MACROBLOCKD * const xd,BLOCK_SIZE bsize,int plane,foreach_transformed_block_visitor visit,void * arg)354 void av1_foreach_transformed_block_in_plane(
355 const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane,
356 foreach_transformed_block_visitor visit, void *arg) {
357 const struct macroblockd_plane *const pd = &xd->plane[plane];
358 // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
359 // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
360 // transform size varies per plane, look it up in a common way.
361 const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
362 const BLOCK_SIZE plane_bsize =
363 get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
364 const uint8_t txw_unit = tx_size_wide_unit[tx_size];
365 const uint8_t txh_unit = tx_size_high_unit[tx_size];
366 const int step = txw_unit * txh_unit;
367 int i = 0, r, c;
368
369 // If mb_to_right_edge is < 0 we are in a situation in which
370 // the current block size extends into the UMV and we won't
371 // visit the sub blocks that are wholly within the UMV.
372 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
373 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
374
375 int blk_row, blk_col;
376
377 const BLOCK_SIZE max_unit_bsize =
378 get_plane_block_size(BLOCK_64X64, pd->subsampling_x, pd->subsampling_y);
379 int mu_blocks_wide = block_size_wide[max_unit_bsize] >> tx_size_wide_log2[0];
380 int mu_blocks_high = block_size_high[max_unit_bsize] >> tx_size_high_log2[0];
381 mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
382 mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
383
384 // Keep track of the row and column of the blocks we use so that we know
385 // if we are in the unrestricted motion border.
386 for (r = 0; r < max_blocks_high; r += mu_blocks_high) {
387 const int unit_height = AOMMIN(mu_blocks_high + r, max_blocks_high);
388 // Skip visiting the sub blocks that are wholly within the UMV.
389 for (c = 0; c < max_blocks_wide; c += mu_blocks_wide) {
390 const int unit_width = AOMMIN(mu_blocks_wide + c, max_blocks_wide);
391 for (blk_row = r; blk_row < unit_height; blk_row += txh_unit) {
392 for (blk_col = c; blk_col < unit_width; blk_col += txw_unit) {
393 visit(plane, i, blk_row, blk_col, plane_bsize, tx_size, arg);
394 i += step;
395 }
396 }
397 }
398 }
399 }
400
av1_foreach_transformed_block(const MACROBLOCKD * const xd,BLOCK_SIZE bsize,int mi_row,int mi_col,foreach_transformed_block_visitor visit,void * arg,const int num_planes)401 void av1_foreach_transformed_block(const MACROBLOCKD *const xd,
402 BLOCK_SIZE bsize, int mi_row, int mi_col,
403 foreach_transformed_block_visitor visit,
404 void *arg, const int num_planes) {
405 for (int plane = 0; plane < num_planes; ++plane) {
406 if (!is_chroma_reference(mi_row, mi_col, bsize,
407 xd->plane[plane].subsampling_x,
408 xd->plane[plane].subsampling_y))
409 continue;
410 av1_foreach_transformed_block_in_plane(xd, bsize, plane, visit, arg);
411 }
412 }
413
414 typedef struct encode_block_pass1_args {
415 AV1_COMMON *cm;
416 MACROBLOCK *x;
417 } encode_block_pass1_args;
418
encode_block_pass1(int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)419 static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
420 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
421 void *arg) {
422 encode_block_pass1_args *args = (encode_block_pass1_args *)arg;
423 AV1_COMMON *cm = args->cm;
424 MACROBLOCK *const x = args->x;
425 MACROBLOCKD *const xd = &x->e_mbd;
426 struct macroblock_plane *const p = &x->plane[plane];
427 struct macroblockd_plane *const pd = &xd->plane[plane];
428 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
429 TxfmParam txfm_param;
430 uint8_t *dst;
431 dst = &pd->dst
432 .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
433 av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
434 DCT_DCT, AV1_XFORM_QUANT_B);
435
436 if (p->eobs[block] > 0) {
437 txfm_param.bd = xd->bd;
438 txfm_param.is_hbd = is_cur_buf_hbd(xd);
439 txfm_param.tx_type = DCT_DCT;
440 txfm_param.tx_size = tx_size;
441 txfm_param.eob = p->eobs[block];
442 txfm_param.lossless = xd->lossless[xd->mi[0]->segment_id];
443 txfm_param.tx_set_type = av1_get_ext_tx_set_type(
444 txfm_param.tx_size, is_inter_block(xd->mi[0]), cm->reduced_tx_set_used);
445 if (txfm_param.is_hbd) {
446 av1_highbd_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
447 return;
448 }
449 av1_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
450 }
451 }
452
av1_encode_sby_pass1(AV1_COMMON * cm,MACROBLOCK * x,BLOCK_SIZE bsize)453 void av1_encode_sby_pass1(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize) {
454 encode_block_pass1_args args = { cm, x };
455 av1_subtract_plane(x, bsize, 0);
456 av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
457 encode_block_pass1, &args);
458 }
459
av1_encode_sb(const struct AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int mi_row,int mi_col,RUN_TYPE dry_run)460 void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
461 int mi_row, int mi_col, RUN_TYPE dry_run) {
462 (void)dry_run;
463 const AV1_COMMON *const cm = &cpi->common;
464 const int num_planes = av1_num_planes(cm);
465 MACROBLOCKD *const xd = &x->e_mbd;
466 struct optimize_ctx ctx;
467 MB_MODE_INFO *mbmi = xd->mi[0];
468 struct encode_b_args arg = { cpi,
469 x,
470 &ctx,
471 &mbmi->skip,
472 NULL,
473 NULL,
474 cpi->optimize_seg_arr[mbmi->segment_id] };
475 int plane;
476
477 mbmi->skip = 1;
478
479 if (x->skip) return;
480
481 for (plane = 0; plane < num_planes; ++plane) {
482 const int subsampling_x = xd->plane[plane].subsampling_x;
483 const int subsampling_y = xd->plane[plane].subsampling_y;
484
485 if (!is_chroma_reference(mi_row, mi_col, bsize, subsampling_x,
486 subsampling_y))
487 continue;
488
489 const BLOCK_SIZE bsizec =
490 scale_chroma_bsize(bsize, subsampling_x, subsampling_y);
491
492 // TODO(jingning): Clean this up.
493 const struct macroblockd_plane *const pd = &xd->plane[plane];
494 const BLOCK_SIZE plane_bsize =
495 get_plane_block_size(bsizec, pd->subsampling_x, pd->subsampling_y);
496 const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
497 const int mi_height = block_size_high[plane_bsize] >> tx_size_high_log2[0];
498 const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane);
499
500 const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
501 const int bw = block_size_wide[txb_size] >> tx_size_wide_log2[0];
502 const int bh = block_size_high[txb_size] >> tx_size_high_log2[0];
503 int idx, idy;
504 int block = 0;
505 int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
506 av1_get_entropy_contexts(bsizec, pd, ctx.ta[plane], ctx.tl[plane]);
507
508 av1_subtract_plane(x, bsizec, plane);
509
510 arg.ta = ctx.ta[plane];
511 arg.tl = ctx.tl[plane];
512
513 const BLOCK_SIZE max_unit_bsize =
514 get_plane_block_size(BLOCK_64X64, pd->subsampling_x, pd->subsampling_y);
515 int mu_blocks_wide =
516 block_size_wide[max_unit_bsize] >> tx_size_wide_log2[0];
517 int mu_blocks_high =
518 block_size_high[max_unit_bsize] >> tx_size_high_log2[0];
519
520 mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide);
521 mu_blocks_high = AOMMIN(mi_height, mu_blocks_high);
522
523 for (idy = 0; idy < mi_height; idy += mu_blocks_high) {
524 for (idx = 0; idx < mi_width; idx += mu_blocks_wide) {
525 int blk_row, blk_col;
526 const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height);
527 const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width);
528 for (blk_row = idy; blk_row < unit_height; blk_row += bh) {
529 for (blk_col = idx; blk_col < unit_width; blk_col += bw) {
530 encode_block_inter(plane, block, blk_row, blk_col, plane_bsize,
531 max_tx_size, &arg, mi_row, mi_col, dry_run);
532 block += step;
533 }
534 }
535 }
536 }
537 }
538 }
539
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)540 static void encode_block_intra_and_set_context(int plane, int block,
541 int blk_row, int blk_col,
542 BLOCK_SIZE plane_bsize,
543 TX_SIZE tx_size, void *arg) {
544 av1_encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size,
545 arg);
546
547 struct encode_b_args *const args = arg;
548 MACROBLOCK *x = args->x;
549 ENTROPY_CONTEXT *a = &args->ta[blk_col];
550 ENTROPY_CONTEXT *l = &args->tl[blk_row];
551 av1_set_txb_context(x, plane, block, tx_size, a, l);
552 }
553
av1_encode_block_intra(int plane,int block,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)554 void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
555 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
556 void *arg) {
557 struct encode_b_args *const args = arg;
558 const AV1_COMMON *const cm = &args->cpi->common;
559 MACROBLOCK *const x = args->x;
560 MACROBLOCKD *const xd = &x->e_mbd;
561 MB_MODE_INFO *mbmi = xd->mi[0];
562 struct macroblock_plane *const p = &x->plane[plane];
563 struct macroblockd_plane *const pd = &xd->plane[plane];
564 tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
565 PLANE_TYPE plane_type = get_plane_type(plane);
566 const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col,
567 tx_size, cm->reduced_tx_set_used);
568 uint16_t *eob = &p->eobs[block];
569 const int dst_stride = pd->dst.stride;
570 uint8_t *dst =
571 &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
572 int dummy_rate_cost = 0;
573
574 av1_predict_intra_block_facade(cm, xd, plane, blk_col, blk_row, tx_size);
575
576 const int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
577 if (plane == 0 && is_blk_skip(x, plane, blk_row * bw + blk_col)) {
578 *eob = 0;
579 p->txb_entropy_ctx[block] = 0;
580 } else {
581 av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size);
582
583 const ENTROPY_CONTEXT *a = &args->ta[blk_col];
584 const ENTROPY_CONTEXT *l = &args->tl[blk_row];
585 if (args->enable_optimize_b != NO_TRELLIS_OPT) {
586 av1_xform_quant(
587 cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, tx_type,
588 USE_B_QUANT_NO_TRELLIS &&
589 (args->enable_optimize_b == FINAL_PASS_TRELLIS_OPT)
590 ? AV1_XFORM_QUANT_B
591 : AV1_XFORM_QUANT_FP);
592 TXB_CTX txb_ctx;
593 get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
594 av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx,
595 args->cpi->sf.trellis_eob_fast, &dummy_rate_cost);
596 } else {
597 av1_xform_quant(
598 cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, tx_type,
599 USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP);
600 }
601 }
602
603 if (*eob) {
604 av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
605 dst_stride, *eob, cm->reduced_tx_set_used);
606 }
607
608 // TODO(jingning): Temporarily disable txk_type check for eob=0 case.
609 // It is possible that certain collision in hash index would cause
610 // the assertion failure. To further optimize the rate-distortion
611 // performance, we need to re-visit this part and enable this assert
612 // again.
613 if (*eob == 0 && plane == 0) {
614 #if 0
615 if (args->cpi->oxcf.aq_mode == NO_AQ
616 && args->cpi->oxcf.deltaq_mode == NO_DELTA_Q) {
617 assert(mbmi->txk_type[av1_get_txk_type_index(plane_bsize, blk_row,
618 blk_col)] == DCT_DCT);
619 }
620 #endif
621 update_txk_array(mbmi->txk_type, plane_bsize, blk_row, blk_col, tx_size,
622 DCT_DCT);
623 }
624
625 // For intra mode, skipped blocks are so rare that transmitting skip=1 is
626 // very expensive.
627 *(args->skip) = 0;
628
629 if (plane == AOM_PLANE_Y && xd->cfl.store_y) {
630 cfl_store_tx(xd, blk_row, blk_col, tx_size, plane_bsize);
631 }
632 }
633
av1_encode_intra_block_plane(const struct AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int plane,int enable_optimize_b,int mi_row,int mi_col)634 void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
635 BLOCK_SIZE bsize, int plane,
636 int enable_optimize_b, int mi_row,
637 int mi_col) {
638 const MACROBLOCKD *const xd = &x->e_mbd;
639 ENTROPY_CONTEXT ta[MAX_MIB_SIZE] = { 0 };
640 ENTROPY_CONTEXT tl[MAX_MIB_SIZE] = { 0 };
641
642 struct encode_b_args arg = {
643 cpi, x, NULL, &(xd->mi[0]->skip), ta, tl, enable_optimize_b
644 };
645
646 if (!is_chroma_reference(mi_row, mi_col, bsize,
647 xd->plane[plane].subsampling_x,
648 xd->plane[plane].subsampling_y))
649 return;
650
651 if (enable_optimize_b) {
652 const struct macroblockd_plane *const pd = &xd->plane[plane];
653 av1_get_entropy_contexts(bsize, pd, ta, tl);
654 }
655 av1_foreach_transformed_block_in_plane(
656 xd, bsize, plane, encode_block_intra_and_set_context, &arg);
657 }
658