Coverage Report

Created: 2026-10-28 07:26

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