• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017, 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 "av1/encoder/encodetxb.h"
13 
14 #include "aom_ports/mem.h"
15 #include "av1/common/blockd.h"
16 #include "av1/common/idct.h"
17 #include "av1/common/pred_common.h"
18 #include "av1/common/scan.h"
19 #include "av1/encoder/bitstream.h"
20 #include "av1/encoder/cost.h"
21 #include "av1/encoder/encodeframe.h"
22 #include "av1/encoder/hash.h"
23 #include "av1/encoder/rdopt.h"
24 #include "av1/encoder/tokenize.h"
25 
av1_alloc_txb_buf(AV1_COMP * cpi)26 void av1_alloc_txb_buf(AV1_COMP *cpi) {
27   AV1_COMMON *cm = &cpi->common;
28   CoeffBufferPool *coeff_buf_pool = &cpi->coeff_buffer_pool;
29   int size = ((cm->mi_params.mi_rows >> cm->seq_params->mib_size_log2) + 1) *
30              ((cm->mi_params.mi_cols >> cm->seq_params->mib_size_log2) + 1);
31   const int num_planes = av1_num_planes(cm);
32   const int subsampling_x = cm->seq_params->subsampling_x;
33   const int subsampling_y = cm->seq_params->subsampling_y;
34   const int luma_max_sb_square =
35       1 << num_pels_log2_lookup[cm->seq_params->sb_size];
36   const int chroma_max_sb_square =
37       luma_max_sb_square >> (subsampling_x + subsampling_y);
38   const int num_tcoeffs =
39       size * (luma_max_sb_square + (num_planes - 1) * chroma_max_sb_square);
40   const int txb_unit_size = TX_SIZE_W_MIN * TX_SIZE_H_MIN;
41 
42   av1_free_txb_buf(cpi);
43   // TODO(jingning): This should be further reduced.
44   cpi->coeff_buffer_base = aom_malloc(sizeof(*cpi->coeff_buffer_base) * size);
45   CHECK_MEM_ERROR(
46       cm, coeff_buf_pool->tcoeff,
47       aom_memalign(32, sizeof(*coeff_buf_pool->tcoeff) * num_tcoeffs));
48   coeff_buf_pool->eobs =
49       aom_malloc(sizeof(*coeff_buf_pool->eobs) * num_tcoeffs / txb_unit_size);
50   coeff_buf_pool->entropy_ctx = aom_malloc(
51       sizeof(*coeff_buf_pool->entropy_ctx) * num_tcoeffs / txb_unit_size);
52 
53   tran_low_t *tcoeff_ptr = coeff_buf_pool->tcoeff;
54   uint16_t *eob_ptr = coeff_buf_pool->eobs;
55   uint8_t *entropy_ctx_ptr = coeff_buf_pool->entropy_ctx;
56   for (int i = 0; i < size; i++) {
57     for (int plane = 0; plane < num_planes; plane++) {
58       const int max_sb_square =
59           (plane == AOM_PLANE_Y) ? luma_max_sb_square : chroma_max_sb_square;
60       cpi->coeff_buffer_base[i].tcoeff[plane] = tcoeff_ptr;
61       cpi->coeff_buffer_base[i].eobs[plane] = eob_ptr;
62       cpi->coeff_buffer_base[i].entropy_ctx[plane] = entropy_ctx_ptr;
63       tcoeff_ptr += max_sb_square;
64       eob_ptr += max_sb_square / txb_unit_size;
65       entropy_ctx_ptr += max_sb_square / txb_unit_size;
66     }
67   }
68 }
69 
av1_free_txb_buf(AV1_COMP * cpi)70 void av1_free_txb_buf(AV1_COMP *cpi) {
71   CoeffBufferPool *coeff_buf_pool = &cpi->coeff_buffer_pool;
72   aom_free(cpi->coeff_buffer_base);
73   aom_free(coeff_buf_pool->tcoeff);
74   aom_free(coeff_buf_pool->eobs);
75   aom_free(coeff_buf_pool->entropy_ctx);
76 }
77 
write_golomb(aom_writer * w,int level)78 static void write_golomb(aom_writer *w, int level) {
79   int x = level + 1;
80   int i = x;
81   int length = 0;
82 
83   while (i) {
84     i >>= 1;
85     ++length;
86   }
87   assert(length > 0);
88 
89   for (i = 0; i < length - 1; ++i) aom_write_bit(w, 0);
90 
91   for (i = length - 1; i >= 0; --i) aom_write_bit(w, (x >> i) & 0x01);
92 }
93 
94 static const int8_t eob_to_pos_small[33] = {
95   0, 1, 2,                                        // 0-2
96   3, 3,                                           // 3-4
97   4, 4, 4, 4,                                     // 5-8
98   5, 5, 5, 5, 5, 5, 5, 5,                         // 9-16
99   6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6  // 17-32
100 };
101 
102 static const int8_t eob_to_pos_large[17] = {
103   6,                               // place holder
104   7,                               // 33-64
105   8,  8,                           // 65-128
106   9,  9,  9,  9,                   // 129-256
107   10, 10, 10, 10, 10, 10, 10, 10,  // 257-512
108   11                               // 513-
109 };
110 
av1_get_eob_pos_token(const int eob,int * const extra)111 int av1_get_eob_pos_token(const int eob, int *const extra) {
112   int t;
113 
114   if (eob < 33) {
115     t = eob_to_pos_small[eob];
116   } else {
117     const int e = AOMMIN((eob - 1) >> 5, 16);
118     t = eob_to_pos_large[e];
119   }
120 
121   *extra = eob - av1_eob_group_start[t];
122 
123   return t;
124 }
125 
126 #if CONFIG_ENTROPY_STATS
av1_update_eob_context(int cdf_idx,int eob,TX_SIZE tx_size,TX_CLASS tx_class,PLANE_TYPE plane,FRAME_CONTEXT * ec_ctx,FRAME_COUNTS * counts,uint8_t allow_update_cdf)127 void av1_update_eob_context(int cdf_idx, int eob, TX_SIZE tx_size,
128                             TX_CLASS tx_class, PLANE_TYPE plane,
129                             FRAME_CONTEXT *ec_ctx, FRAME_COUNTS *counts,
130                             uint8_t allow_update_cdf) {
131 #else
132 void av1_update_eob_context(int eob, TX_SIZE tx_size, TX_CLASS tx_class,
133                             PLANE_TYPE plane, FRAME_CONTEXT *ec_ctx,
134                             uint8_t allow_update_cdf) {
135 #endif
136   int eob_extra;
137   const int eob_pt = av1_get_eob_pos_token(eob, &eob_extra);
138   TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
139 
140   const int eob_multi_size = txsize_log2_minus4[tx_size];
141   const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
142 
143   switch (eob_multi_size) {
144     case 0:
145 #if CONFIG_ENTROPY_STATS
146       ++counts->eob_multi16[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
147 #endif
148       if (allow_update_cdf)
149         update_cdf(ec_ctx->eob_flag_cdf16[plane][eob_multi_ctx], eob_pt - 1, 5);
150       break;
151     case 1:
152 #if CONFIG_ENTROPY_STATS
153       ++counts->eob_multi32[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
154 #endif
155       if (allow_update_cdf)
156         update_cdf(ec_ctx->eob_flag_cdf32[plane][eob_multi_ctx], eob_pt - 1, 6);
157       break;
158     case 2:
159 #if CONFIG_ENTROPY_STATS
160       ++counts->eob_multi64[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
161 #endif
162       if (allow_update_cdf)
163         update_cdf(ec_ctx->eob_flag_cdf64[plane][eob_multi_ctx], eob_pt - 1, 7);
164       break;
165     case 3:
166 #if CONFIG_ENTROPY_STATS
167       ++counts->eob_multi128[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
168 #endif
169       if (allow_update_cdf) {
170         update_cdf(ec_ctx->eob_flag_cdf128[plane][eob_multi_ctx], eob_pt - 1,
171                    8);
172       }
173       break;
174     case 4:
175 #if CONFIG_ENTROPY_STATS
176       ++counts->eob_multi256[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
177 #endif
178       if (allow_update_cdf) {
179         update_cdf(ec_ctx->eob_flag_cdf256[plane][eob_multi_ctx], eob_pt - 1,
180                    9);
181       }
182       break;
183     case 5:
184 #if CONFIG_ENTROPY_STATS
185       ++counts->eob_multi512[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
186 #endif
187       if (allow_update_cdf) {
188         update_cdf(ec_ctx->eob_flag_cdf512[plane][eob_multi_ctx], eob_pt - 1,
189                    10);
190       }
191       break;
192     case 6:
193     default:
194 #if CONFIG_ENTROPY_STATS
195       ++counts->eob_multi1024[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
196 #endif
197       if (allow_update_cdf) {
198         update_cdf(ec_ctx->eob_flag_cdf1024[plane][eob_multi_ctx], eob_pt - 1,
199                    11);
200       }
201       break;
202   }
203 
204   if (av1_eob_offset_bits[eob_pt] > 0) {
205     int eob_ctx = eob_pt - 3;
206     int eob_shift = av1_eob_offset_bits[eob_pt] - 1;
207     int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
208 #if CONFIG_ENTROPY_STATS
209     counts->eob_extra[cdf_idx][txs_ctx][plane][eob_pt][bit]++;
210 #endif  // CONFIG_ENTROPY_STATS
211     if (allow_update_cdf)
212       update_cdf(ec_ctx->eob_extra_cdf[txs_ctx][plane][eob_ctx], bit, 2);
213   }
214 }
215 
216 static INLINE int get_nz_map_ctx(const uint8_t *const levels,
217                                  const int coeff_idx, const int bwl,
218                                  const int height, const int scan_idx,
219                                  const int is_eob, const TX_SIZE tx_size,
220                                  const TX_CLASS tx_class) {
221   if (is_eob) {
222     if (scan_idx == 0) return 0;
223     if (scan_idx <= (height << bwl) / 8) return 1;
224     if (scan_idx <= (height << bwl) / 4) return 2;
225     return 3;
226   }
227   const int stats =
228       get_nz_mag(levels + get_padded_idx(coeff_idx, bwl), bwl, tx_class);
229   return get_nz_map_ctx_from_stats(stats, coeff_idx, bwl, tx_size, tx_class);
230 }
231 
232 void av1_txb_init_levels_c(const tran_low_t *const coeff, const int width,
233                            const int height, uint8_t *const levels) {
234   const int stride = width + TX_PAD_HOR;
235   uint8_t *ls = levels;
236 
237   memset(levels + stride * height, 0,
238          sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END));
239 
240   for (int i = 0; i < height; i++) {
241     for (int j = 0; j < width; j++) {
242       *ls++ = (uint8_t)clamp(abs(coeff[i * width + j]), 0, INT8_MAX);
243     }
244     for (int j = 0; j < TX_PAD_HOR; j++) {
245       *ls++ = 0;
246     }
247   }
248 }
249 
250 void av1_get_nz_map_contexts_c(const uint8_t *const levels,
251                                const int16_t *const scan, const uint16_t eob,
252                                const TX_SIZE tx_size, const TX_CLASS tx_class,
253                                int8_t *const coeff_contexts) {
254   const int bwl = get_txb_bwl(tx_size);
255   const int height = get_txb_high(tx_size);
256   for (int i = 0; i < eob; ++i) {
257     const int pos = scan[i];
258     coeff_contexts[pos] = get_nz_map_ctx(levels, pos, bwl, height, i,
259                                          i == eob - 1, tx_size, tx_class);
260   }
261 }
262 
263 void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x,
264                           aom_writer *w, int blk_row, int blk_col, int plane,
265                           int block, TX_SIZE tx_size) {
266   MACROBLOCKD *xd = &x->e_mbd;
267   const CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff;
268   const PLANE_TYPE plane_type = get_plane_type(plane);
269   const int txb_offset = x->mbmi_ext_frame->cb_offset[plane_type] /
270                          (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
271   const uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset;
272   const uint16_t eob = eob_txb[block];
273   const uint8_t *entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset;
274   const int txb_skip_ctx = entropy_ctx[block] & TXB_SKIP_CTX_MASK;
275   const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
276   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
277   aom_write_symbol(w, eob == 0, ec_ctx->txb_skip_cdf[txs_ctx][txb_skip_ctx], 2);
278   if (eob == 0) return;
279 
280   const TX_TYPE tx_type =
281       av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
282                       cm->features.reduced_tx_set_used);
283   // Only y plane's tx_type is transmitted
284   if (plane == 0) {
285     av1_write_tx_type(cm, xd, tx_type, tx_size, w);
286   }
287 
288   int eob_extra;
289   const int eob_pt = av1_get_eob_pos_token(eob, &eob_extra);
290   const int eob_multi_size = txsize_log2_minus4[tx_size];
291   const TX_CLASS tx_class = tx_type_to_class[tx_type];
292   const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
293   switch (eob_multi_size) {
294     case 0:
295       aom_write_symbol(w, eob_pt - 1,
296                        ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx], 5);
297       break;
298     case 1:
299       aom_write_symbol(w, eob_pt - 1,
300                        ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx], 6);
301       break;
302     case 2:
303       aom_write_symbol(w, eob_pt - 1,
304                        ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx], 7);
305       break;
306     case 3:
307       aom_write_symbol(w, eob_pt - 1,
308                        ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx], 8);
309       break;
310     case 4:
311       aom_write_symbol(w, eob_pt - 1,
312                        ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx], 9);
313       break;
314     case 5:
315       aom_write_symbol(w, eob_pt - 1,
316                        ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx], 10);
317       break;
318     default:
319       aom_write_symbol(w, eob_pt - 1,
320                        ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11);
321       break;
322   }
323 
324   const int eob_offset_bits = av1_eob_offset_bits[eob_pt];
325   if (eob_offset_bits > 0) {
326     const int eob_ctx = eob_pt - 3;
327     int eob_shift = eob_offset_bits - 1;
328     int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
329     aom_write_symbol(w, bit,
330                      ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2);
331     for (int i = 1; i < eob_offset_bits; i++) {
332       eob_shift = eob_offset_bits - 1 - i;
333       bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
334       aom_write_bit(w, bit);
335     }
336   }
337 
338   const int width = get_txb_wide(tx_size);
339   const int height = get_txb_high(tx_size);
340   uint8_t levels_buf[TX_PAD_2D];
341   uint8_t *const levels = set_levels(levels_buf, width);
342   const tran_low_t *tcoeff_txb =
343       cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type];
344   const tran_low_t *tcoeff = tcoeff_txb + BLOCK_OFFSET(block);
345   av1_txb_init_levels(tcoeff, width, height, levels);
346   const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
347   const int16_t *const scan = scan_order->scan;
348   DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
349   av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
350 
351   const int bwl = get_txb_bwl(tx_size);
352   for (int c = eob - 1; c >= 0; --c) {
353     const int pos = scan[c];
354     const int coeff_ctx = coeff_contexts[pos];
355     const tran_low_t v = tcoeff[pos];
356     const tran_low_t level = abs(v);
357 
358     if (c == eob - 1) {
359       aom_write_symbol(
360           w, AOMMIN(level, 3) - 1,
361           ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3);
362     } else {
363       aom_write_symbol(w, AOMMIN(level, 3),
364                        ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx],
365                        4);
366     }
367     if (level > NUM_BASE_LEVELS) {
368       // level is above 1.
369       const int base_range = level - 1 - NUM_BASE_LEVELS;
370       const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
371       aom_cdf_prob *cdf =
372           ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx];
373       for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
374         const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
375         aom_write_symbol(w, k, cdf, BR_CDF_SIZE);
376         if (k < BR_CDF_SIZE - 1) break;
377       }
378     }
379   }
380 
381   // Loop to code all signs in the transform block,
382   // starting with the sign of DC (if applicable)
383   for (int c = 0; c < eob; ++c) {
384     const tran_low_t v = tcoeff[scan[c]];
385     const tran_low_t level = abs(v);
386     const int sign = (v < 0) ? 1 : 0;
387     if (level) {
388       if (c == 0) {
389         const int dc_sign_ctx =
390             (entropy_ctx[block] >> DC_SIGN_CTX_SHIFT) & DC_SIGN_CTX_MASK;
391         aom_write_symbol(w, sign, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx],
392                          2);
393       } else {
394         aom_write_bit(w, sign);
395       }
396       if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS)
397         write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS);
398     }
399   }
400 }
401 
402 void av1_write_intra_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x,
403                                aom_writer *w, BLOCK_SIZE bsize) {
404   MACROBLOCKD *xd = &x->e_mbd;
405   const int num_planes = av1_num_planes(cm);
406   int block[MAX_MB_PLANE] = { 0 };
407   int row, col;
408   assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
409                                        xd->plane[0].subsampling_y));
410   const int max_blocks_wide = max_block_wide(xd, bsize, 0);
411   const int max_blocks_high = max_block_high(xd, bsize, 0);
412   const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
413   int mu_blocks_wide = mi_size_wide[max_unit_bsize];
414   int mu_blocks_high = mi_size_high[max_unit_bsize];
415   mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
416   mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
417 
418   for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
419     for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
420       for (int plane = 0; plane < num_planes; ++plane) {
421         if (plane && !xd->is_chroma_ref) break;
422         const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
423         const int stepr = tx_size_high_unit[tx_size];
424         const int stepc = tx_size_wide_unit[tx_size];
425         const int step = stepr * stepc;
426         const struct macroblockd_plane *const pd = &xd->plane[plane];
427         const int unit_height = ROUND_POWER_OF_TWO(
428             AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y);
429         const int unit_width = ROUND_POWER_OF_TWO(
430             AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x);
431         for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height;
432              blk_row += stepr) {
433           for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width;
434                blk_col += stepc) {
435             av1_write_coeffs_txb(cm, x, w, blk_row, blk_col, plane,
436                                  block[plane], tx_size);
437             block[plane] += step;
438           }
439         }
440       }
441     }
442   }
443 }
444 
445 uint8_t av1_get_txb_entropy_context(const tran_low_t *qcoeff,
446                                     const SCAN_ORDER *scan_order, int eob) {
447   const int16_t *const scan = scan_order->scan;
448   int cul_level = 0;
449   int c;
450 
451   if (eob == 0) return 0;
452   for (c = 0; c < eob; ++c) {
453     cul_level += abs(qcoeff[scan[c]]);
454     if (cul_level > COEFF_CONTEXT_MASK) break;
455   }
456 
457   cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
458   set_dc_sign(&cul_level, qcoeff[0]);
459 
460   return (uint8_t)cul_level;
461 }
462 
463 static void update_tx_type_count(const AV1_COMP *cpi, const AV1_COMMON *cm,
464                                  MACROBLOCKD *xd, int blk_row, int blk_col,
465                                  int plane, TX_SIZE tx_size,
466                                  FRAME_COUNTS *counts,
467                                  uint8_t allow_update_cdf) {
468   MB_MODE_INFO *mbmi = xd->mi[0];
469   int is_inter = is_inter_block(mbmi);
470   const int reduced_tx_set_used = cm->features.reduced_tx_set_used;
471   FRAME_CONTEXT *fc = xd->tile_ctx;
472 #if !CONFIG_ENTROPY_STATS
473   (void)counts;
474 #endif  // !CONFIG_ENTROPY_STATS
475 
476   // Only y plane's tx_type is updated
477   if (plane > 0) return;
478   const TX_TYPE tx_type = av1_get_tx_type(xd, PLANE_TYPE_Y, blk_row, blk_col,
479                                           tx_size, reduced_tx_set_used);
480   if (is_inter) {
481     if (cpi->oxcf.txfm_cfg.use_inter_dct_only) {
482       assert(tx_type == DCT_DCT);
483     }
484   } else {
485     if (cpi->oxcf.txfm_cfg.use_intra_dct_only) {
486       assert(tx_type == DCT_DCT);
487     } else if (cpi->oxcf.txfm_cfg.use_intra_default_tx_only) {
488       const TX_TYPE default_type = get_default_tx_type(
489           PLANE_TYPE_Y, xd, tx_size, cpi->use_screen_content_tools);
490       (void)default_type;
491       // TODO(kyslov): We don't always respect use_intra_default_tx_only flag in
492       // NonRD and REALTIME case. Specifically we ignore it in hybrid inta mode
493       // search, when picking up intra mode in nonRD inter mode search and in RD
494       // REALTIME mode when we limit TX type usage.
495       // We need to fix txfm cfg for these cases. Meanwhile relieving the
496       // assert.
497       assert(tx_type == default_type || cpi->sf.rt_sf.use_nonrd_pick_mode ||
498              cpi->oxcf.mode == REALTIME);
499     }
500   }
501 
502   if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 &&
503       cm->quant_params.base_qindex > 0 && !mbmi->skip_txfm &&
504       !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
505     const int eset = get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used);
506     if (eset > 0) {
507       const TxSetType tx_set_type =
508           av1_get_ext_tx_set_type(tx_size, is_inter, reduced_tx_set_used);
509       if (is_inter) {
510         if (allow_update_cdf) {
511           update_cdf(fc->inter_ext_tx_cdf[eset][txsize_sqr_map[tx_size]],
512                      av1_ext_tx_ind[tx_set_type][tx_type],
513                      av1_num_ext_tx_set[tx_set_type]);
514         }
515 #if CONFIG_ENTROPY_STATS
516         ++counts->inter_ext_tx[eset][txsize_sqr_map[tx_size]]
517                               [av1_ext_tx_ind[tx_set_type][tx_type]];
518 #endif  // CONFIG_ENTROPY_STATS
519       } else {
520         PREDICTION_MODE intra_dir;
521         if (mbmi->filter_intra_mode_info.use_filter_intra)
522           intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
523                                              .filter_intra_mode];
524         else
525           intra_dir = mbmi->mode;
526 #if CONFIG_ENTROPY_STATS
527         ++counts->intra_ext_tx[eset][txsize_sqr_map[tx_size]][intra_dir]
528                               [av1_ext_tx_ind[tx_set_type][tx_type]];
529 #endif  // CONFIG_ENTROPY_STATS
530         if (allow_update_cdf) {
531           update_cdf(
532               fc->intra_ext_tx_cdf[eset][txsize_sqr_map[tx_size]][intra_dir],
533               av1_ext_tx_ind[tx_set_type][tx_type],
534               av1_num_ext_tx_set[tx_set_type]);
535         }
536       }
537     }
538   }
539 }
540 
541 void av1_update_and_record_txb_context(int plane, int block, int blk_row,
542                                        int blk_col, BLOCK_SIZE plane_bsize,
543                                        TX_SIZE tx_size, void *arg) {
544   struct tokenize_b_args *const args = arg;
545   const AV1_COMP *cpi = args->cpi;
546   const AV1_COMMON *cm = &cpi->common;
547   ThreadData *const td = args->td;
548   MACROBLOCK *const x = &td->mb;
549   MACROBLOCKD *const xd = &x->e_mbd;
550   struct macroblock_plane *p = &x->plane[plane];
551   struct macroblockd_plane *pd = &xd->plane[plane];
552   const int eob = p->eobs[block];
553   const int block_offset = BLOCK_OFFSET(block);
554   tran_low_t *qcoeff = p->qcoeff + block_offset;
555   const PLANE_TYPE plane_type = pd->plane_type;
556   const TX_TYPE tx_type =
557       av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
558                       cm->features.reduced_tx_set_used);
559   const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
560   tran_low_t *tcoeff;
561   assert(args->dry_run != DRY_RUN_COSTCOEFFS);
562   if (args->dry_run == OUTPUT_ENABLED) {
563     MB_MODE_INFO *mbmi = xd->mi[0];
564     TXB_CTX txb_ctx;
565     get_txb_ctx(plane_bsize, tx_size, plane,
566                 pd->above_entropy_context + blk_col,
567                 pd->left_entropy_context + blk_row, &txb_ctx);
568     const int bwl = get_txb_bwl(tx_size);
569     const int width = get_txb_wide(tx_size);
570     const int height = get_txb_high(tx_size);
571     const uint8_t allow_update_cdf = args->allow_update_cdf;
572     const TX_SIZE txsize_ctx = get_txsize_entropy_ctx(tx_size);
573     FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
574 #if CONFIG_ENTROPY_STATS
575     int cdf_idx = cm->coef_cdf_category;
576     ++td->counts->txb_skip[cdf_idx][txsize_ctx][txb_ctx.txb_skip_ctx][eob == 0];
577 #endif  // CONFIG_ENTROPY_STATS
578     if (allow_update_cdf) {
579       update_cdf(ec_ctx->txb_skip_cdf[txsize_ctx][txb_ctx.txb_skip_ctx],
580                  eob == 0, 2);
581     }
582 
583     CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff;
584     const int txb_offset = x->mbmi_ext_frame->cb_offset[plane_type] /
585                            (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
586     uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset;
587     uint8_t *const entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset;
588     entropy_ctx[block] = txb_ctx.txb_skip_ctx;
589     eob_txb[block] = eob;
590 
591     if (eob == 0) {
592       av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, 0, blk_col,
593                                blk_row);
594       return;
595     }
596     const int segment_id = mbmi->segment_id;
597     const int seg_eob = av1_get_tx_eob(&cpi->common.seg, segment_id, tx_size);
598     tran_low_t *tcoeff_txb =
599         cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type];
600     tcoeff = tcoeff_txb + block_offset;
601     memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
602 
603     uint8_t levels_buf[TX_PAD_2D];
604     uint8_t *const levels = set_levels(levels_buf, width);
605     av1_txb_init_levels(tcoeff, width, height, levels);
606     update_tx_type_count(cpi, cm, xd, blk_row, blk_col, plane, tx_size,
607                          td->counts, allow_update_cdf);
608 
609     const TX_CLASS tx_class = tx_type_to_class[tx_type];
610     const int16_t *const scan = scan_order->scan;
611 
612     // record tx type usage
613     td->rd_counts.tx_type_used[tx_size][tx_type]++;
614 
615 #if CONFIG_ENTROPY_STATS
616     av1_update_eob_context(cdf_idx, eob, tx_size, tx_class, plane_type, ec_ctx,
617                            td->counts, allow_update_cdf);
618 #else
619     av1_update_eob_context(eob, tx_size, tx_class, plane_type, ec_ctx,
620                            allow_update_cdf);
621 #endif
622 
623     DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
624     av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class,
625                             coeff_contexts);
626 
627     for (int c = eob - 1; c >= 0; --c) {
628       const int pos = scan[c];
629       const int coeff_ctx = coeff_contexts[pos];
630       const tran_low_t v = qcoeff[pos];
631       const tran_low_t level = abs(v);
632       td->abs_sum_level += level;
633 
634       if (allow_update_cdf) {
635         if (c == eob - 1) {
636           assert(coeff_ctx < 4);
637           update_cdf(
638               ec_ctx->coeff_base_eob_cdf[txsize_ctx][plane_type][coeff_ctx],
639               AOMMIN(level, 3) - 1, 3);
640         } else {
641           update_cdf(ec_ctx->coeff_base_cdf[txsize_ctx][plane_type][coeff_ctx],
642                      AOMMIN(level, 3), 4);
643         }
644       }
645       if (c == eob - 1) {
646         assert(coeff_ctx < 4);
647 #if CONFIG_ENTROPY_STATS
648         ++td->counts->coeff_base_eob_multi[cdf_idx][txsize_ctx][plane_type]
649                                           [coeff_ctx][AOMMIN(level, 3) - 1];
650       } else {
651         ++td->counts->coeff_base_multi[cdf_idx][txsize_ctx][plane_type]
652                                       [coeff_ctx][AOMMIN(level, 3)];
653 #endif
654       }
655       if (level > NUM_BASE_LEVELS) {
656         const int base_range = level - 1 - NUM_BASE_LEVELS;
657         const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
658         for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
659           const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
660           if (allow_update_cdf) {
661             update_cdf(ec_ctx->coeff_br_cdf[AOMMIN(txsize_ctx, TX_32X32)]
662                                            [plane_type][br_ctx],
663                        k, BR_CDF_SIZE);
664           }
665           for (int lps = 0; lps < BR_CDF_SIZE - 1; lps++) {
666 #if CONFIG_ENTROPY_STATS
667             ++td->counts->coeff_lps[AOMMIN(txsize_ctx, TX_32X32)][plane_type]
668                                    [lps][br_ctx][lps == k];
669 #endif  // CONFIG_ENTROPY_STATS
670             if (lps == k) break;
671           }
672 #if CONFIG_ENTROPY_STATS
673           ++td->counts->coeff_lps_multi[cdf_idx][AOMMIN(txsize_ctx, TX_32X32)]
674                                        [plane_type][br_ctx][k];
675 #endif
676           if (k < BR_CDF_SIZE - 1) break;
677         }
678       }
679     }
680     // Update the context needed to code the DC sign (if applicable)
681     if (tcoeff[0] != 0) {
682       const int dc_sign = (tcoeff[0] < 0) ? 1 : 0;
683       const int dc_sign_ctx = txb_ctx.dc_sign_ctx;
684 #if CONFIG_ENTROPY_STATS
685       ++td->counts->dc_sign[plane_type][dc_sign_ctx][dc_sign];
686 #endif  // CONFIG_ENTROPY_STATS
687       if (allow_update_cdf)
688         update_cdf(ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], dc_sign, 2);
689       entropy_ctx[block] |= dc_sign_ctx << DC_SIGN_CTX_SHIFT;
690     }
691   } else {
692     tcoeff = qcoeff;
693   }
694   const uint8_t cul_level =
695       av1_get_txb_entropy_context(tcoeff, scan_order, eob);
696   av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level,
697                            blk_col, blk_row);
698 }
699 
700 void av1_update_intra_mb_txb_context(const AV1_COMP *cpi, ThreadData *td,
701                                      RUN_TYPE dry_run, BLOCK_SIZE bsize,
702                                      uint8_t allow_update_cdf) {
703   const AV1_COMMON *const cm = &cpi->common;
704   const int num_planes = av1_num_planes(cm);
705   MACROBLOCK *const x = &td->mb;
706   MACROBLOCKD *const xd = &x->e_mbd;
707   MB_MODE_INFO *const mbmi = xd->mi[0];
708   struct tokenize_b_args arg = { cpi, td, 0, allow_update_cdf, dry_run };
709   if (mbmi->skip_txfm) {
710     av1_reset_entropy_context(xd, bsize, num_planes);
711     return;
712   }
713 
714   for (int plane = 0; plane < num_planes; ++plane) {
715     if (plane && !xd->is_chroma_ref) break;
716     const struct macroblockd_plane *const pd = &xd->plane[plane];
717     const int ss_x = pd->subsampling_x;
718     const int ss_y = pd->subsampling_y;
719     const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
720     av1_foreach_transformed_block_in_plane(
721         xd, plane_bsize, plane, av1_update_and_record_txb_context, &arg);
722   }
723 }
724 
725 CB_COEFF_BUFFER *av1_get_cb_coeff_buffer(const struct AV1_COMP *cpi, int mi_row,
726                                          int mi_col) {
727   const AV1_COMMON *const cm = &cpi->common;
728   const int mib_size_log2 = cm->seq_params->mib_size_log2;
729   const int stride = (cm->mi_params.mi_cols >> mib_size_log2) + 1;
730   const int offset =
731       (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2);
732   return cpi->coeff_buffer_base + offset;
733 }
734