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