Coverage Report

Created: 2026-10-28 06:18

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