Coverage Report

Created: 2025-06-22 08:04

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