Coverage Report

Created: 2026-11-12 08:20

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