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