Coverage Report

Created: 2026-02-26 06:25

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
1.91k
void av1_alloc_txb_buf(AV1_COMP *cpi) {
27
1.91k
  AV1_COMMON *cm = &cpi->common;
28
1.91k
  CoeffBufferPool *coeff_buf_pool = &cpi->coeff_buffer_pool;
29
1.91k
  int size = ((cm->mi_params.mi_rows >> cm->seq_params->mib_size_log2) + 1) *
30
1.91k
             ((cm->mi_params.mi_cols >> cm->seq_params->mib_size_log2) + 1);
31
1.91k
  const int num_planes = av1_num_planes(cm);
32
1.91k
  const int subsampling_x = cm->seq_params->subsampling_x;
33
1.91k
  const int subsampling_y = cm->seq_params->subsampling_y;
34
1.91k
  const int luma_max_sb_square =
35
1.91k
      1 << num_pels_log2_lookup[cm->seq_params->sb_size];
36
1.91k
  const int chroma_max_sb_square =
37
1.91k
      luma_max_sb_square >> (subsampling_x + subsampling_y);
38
1.91k
  const int num_tcoeffs =
39
1.91k
      size * (luma_max_sb_square + (num_planes - 1) * chroma_max_sb_square);
40
1.91k
  const int txb_unit_size = TX_SIZE_W_MIN * TX_SIZE_H_MIN;
41
42
1.91k
  av1_free_txb_buf(cpi);
43
  // TODO(jingning): This should be further reduced.
44
1.91k
  cpi->coeff_buffer_base = aom_malloc(sizeof(*cpi->coeff_buffer_base) * size);
45
1.91k
  CHECK_MEM_ERROR(
46
1.91k
      cm, coeff_buf_pool->tcoeff,
47
1.91k
      aom_memalign(32, sizeof(*coeff_buf_pool->tcoeff) * num_tcoeffs));
48
1.91k
  coeff_buf_pool->eobs =
49
1.91k
      aom_malloc(sizeof(*coeff_buf_pool->eobs) * num_tcoeffs / txb_unit_size);
50
1.91k
  coeff_buf_pool->entropy_ctx = aom_malloc(
51
1.91k
      sizeof(*coeff_buf_pool->entropy_ctx) * num_tcoeffs / txb_unit_size);
52
53
1.91k
  tran_low_t *tcoeff_ptr = coeff_buf_pool->tcoeff;
54
1.91k
  uint16_t *eob_ptr = coeff_buf_pool->eobs;
55
1.91k
  uint8_t *entropy_ctx_ptr = coeff_buf_pool->entropy_ctx;
56
13.9k
  for (int i = 0; i < size; i++) {
57
48.1k
    for (int plane = 0; plane < num_planes; plane++) {
58
36.0k
      const int max_sb_square =
59
36.0k
          (plane == AOM_PLANE_Y) ? luma_max_sb_square : chroma_max_sb_square;
60
36.0k
      cpi->coeff_buffer_base[i].tcoeff[plane] = tcoeff_ptr;
61
36.0k
      cpi->coeff_buffer_base[i].eobs[plane] = eob_ptr;
62
36.0k
      cpi->coeff_buffer_base[i].entropy_ctx[plane] = entropy_ctx_ptr;
63
36.0k
      tcoeff_ptr += max_sb_square;
64
36.0k
      eob_ptr += max_sb_square / txb_unit_size;
65
36.0k
      entropy_ctx_ptr += max_sb_square / txb_unit_size;
66
36.0k
    }
67
12.0k
  }
68
1.91k
}
69
70
2.86k
void av1_free_txb_buf(AV1_COMP *cpi) {
71
2.86k
  CoeffBufferPool *coeff_buf_pool = &cpi->coeff_buffer_pool;
72
2.86k
  aom_free(cpi->coeff_buffer_base);
73
2.86k
  aom_free(coeff_buf_pool->tcoeff);
74
2.86k
  aom_free(coeff_buf_pool->eobs);
75
2.86k
  aom_free(coeff_buf_pool->entropy_ctx);
76
2.86k
}
77
78
2.75k
static void write_golomb(aom_writer *w, int level) {
79
2.75k
  int x = level + 1;
80
2.75k
  int i = x;
81
2.75k
  int length = 0;
82
83
23.0k
  while (i) {
84
20.3k
    i >>= 1;
85
20.3k
    ++length;
86
20.3k
  }
87
2.75k
  assert(length > 0);
88
89
20.3k
  for (i = 0; i < length - 1; ++i) aom_write_bit(w, 0);
90
91
23.0k
  for (i = length - 1; i >= 0; --i) aom_write_bit(w, (x >> i) & 0x01);
92
2.75k
}
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
77.4k
int av1_get_eob_pos_token(const int eob, int *const extra) {
112
77.4k
  int t;
113
114
77.4k
  if (eob < 33) {
115
72.7k
    t = eob_to_pos_small[eob];
116
72.7k
  } else {
117
4.71k
    const int e = AOMMIN((eob - 1) >> 5, 16);
118
4.71k
    t = eob_to_pos_large[e];
119
4.71k
  }
120
121
77.4k
  *extra = eob - av1_eob_group_start[t];
122
123
77.4k
  return t;
124
77.4k
}
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
3.72k
                            uint8_t allow_update_cdf) {
135
3.72k
#endif
136
3.72k
  int eob_extra;
137
3.72k
  const int eob_pt = av1_get_eob_pos_token(eob, &eob_extra);
138
3.72k
  TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
139
140
3.72k
  const int eob_multi_size = txsize_log2_minus4[tx_size];
141
3.72k
  const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
142
143
3.72k
  switch (eob_multi_size) {
144
873
    case 0:
145
#if CONFIG_ENTROPY_STATS
146
      ++counts->eob_multi16[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
147
#endif
148
873
      if (allow_update_cdf)
149
873
        update_cdf(ec_ctx->eob_flag_cdf16[plane][eob_multi_ctx], eob_pt - 1, 5);
150
873
      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
32
    case 2:
159
#if CONFIG_ENTROPY_STATS
160
      ++counts->eob_multi64[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
161
#endif
162
32
      if (allow_update_cdf)
163
32
        update_cdf(ec_ctx->eob_flag_cdf64[plane][eob_multi_ctx], eob_pt - 1, 7);
164
32
      break;
165
273
    case 3:
166
#if CONFIG_ENTROPY_STATS
167
      ++counts->eob_multi128[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
168
#endif
169
273
      if (allow_update_cdf) {
170
273
        update_cdf(ec_ctx->eob_flag_cdf128[plane][eob_multi_ctx], eob_pt - 1,
171
273
                   8);
172
273
      }
173
273
      break;
174
1.52k
    case 4:
175
#if CONFIG_ENTROPY_STATS
176
      ++counts->eob_multi256[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
177
#endif
178
1.52k
      if (allow_update_cdf) {
179
1.52k
        update_cdf(ec_ctx->eob_flag_cdf256[plane][eob_multi_ctx], eob_pt - 1,
180
1.52k
                   9);
181
1.52k
      }
182
1.52k
      break;
183
147
    case 5:
184
#if CONFIG_ENTROPY_STATS
185
      ++counts->eob_multi512[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
186
#endif
187
147
      if (allow_update_cdf) {
188
147
        update_cdf(ec_ctx->eob_flag_cdf512[plane][eob_multi_ctx], eob_pt - 1,
189
147
                   10);
190
147
      }
191
147
      break;
192
877
    case 6:
193
877
    default:
194
#if CONFIG_ENTROPY_STATS
195
      ++counts->eob_multi1024[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
196
#endif
197
877
      if (allow_update_cdf) {
198
877
        update_cdf(ec_ctx->eob_flag_cdf1024[plane][eob_multi_ctx], eob_pt - 1,
199
877
                   11);
200
877
      }
201
877
      break;
202
3.72k
  }
203
204
3.72k
  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
3.72k
}
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
421k
                                 const TX_CLASS tx_class) {
221
421k
  if (is_eob) {
222
32.6k
    if (scan_idx == 0) return 0;
223
6.07k
    if (scan_idx <= (height << bwl) / 8) return 1;
224
6.02k
    if (scan_idx <= (height << bwl) / 4) return 2;
225
5.70k
    return 3;
226
6.02k
  }
227
389k
  const int stats =
228
389k
      get_nz_mag(levels + get_padded_idx(coeff_idx, bwl), bwl, tx_class);
229
389k
  return get_nz_map_ctx_from_stats(stats, coeff_idx, bwl, tx_size, tx_class);
230
421k
}
231
232
void av1_txb_init_levels_c(const tran_low_t *const coeff, const int width,
233
33.3k
                           const int height, uint8_t *const levels) {
234
33.3k
  const int stride = width + TX_PAD_HOR;
235
33.3k
  uint8_t *ls = levels;
236
237
33.3k
  memset(levels + stride * height, 0,
238
33.3k
         sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END));
239
240
470k
  for (int i = 0; i < height; i++) {
241
9.19M
    for (int j = 0; j < width; j++) {
242
8.75M
      *ls++ = (uint8_t)clamp(abs(coeff[i * width + j]), 0, INT8_MAX);
243
8.75M
    }
244
2.18M
    for (int j = 0; j < TX_PAD_HOR; j++) {
245
1.75M
      *ls++ = 0;
246
1.75M
    }
247
437k
  }
248
33.3k
}
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
32.6k
                               int8_t *const coeff_contexts) {
254
32.6k
  const int bwl = get_txb_bwl(tx_size);
255
32.6k
  const int height = get_txb_high(tx_size);
256
454k
  for (int i = 0; i < eob; ++i) {
257
421k
    const int pos = scan[i];
258
421k
    coeff_contexts[pos] = get_nz_map_ctx(levels, pos, bwl, height, i,
259
421k
                                         i == eob - 1, tx_size, tx_class);
260
421k
  }
261
32.6k
}
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
877k
                          int block, TX_SIZE tx_size) {
266
877k
  MACROBLOCKD *xd = &x->e_mbd;
267
877k
  const CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff;
268
877k
  const PLANE_TYPE plane_type = get_plane_type(plane);
269
877k
  const int txb_offset = x->mbmi_ext_frame->cb_offset[plane_type] /
270
877k
                         (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
271
877k
  const uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset;
272
877k
  const uint16_t eob = eob_txb[block];
273
877k
  const uint8_t *entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset;
274
877k
  const int txb_skip_ctx = entropy_ctx[block] & TXB_SKIP_CTX_MASK;
275
877k
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
276
877k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
277
877k
  aom_write_symbol(w, eob == 0, ec_ctx->txb_skip_cdf[txs_ctx][txb_skip_ctx], 2);
278
877k
  if (eob == 0) return;
279
280
3.72k
  const TX_TYPE tx_type =
281
3.72k
      av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
282
3.72k
                      cm->features.reduced_tx_set_used);
283
  // Only y plane's tx_type is transmitted
284
3.72k
  if (plane == 0) {
285
1.33k
    av1_write_tx_type(cm, xd, tx_type, tx_size, w);
286
1.33k
  }
287
288
3.72k
  int eob_extra;
289
3.72k
  const int eob_pt = av1_get_eob_pos_token(eob, &eob_extra);
290
3.72k
  const int eob_multi_size = txsize_log2_minus4[tx_size];
291
3.72k
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
292
3.72k
  const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
293
3.72k
  switch (eob_multi_size) {
294
873
    case 0:
295
873
      aom_write_symbol(w, eob_pt - 1,
296
873
                       ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx], 5);
297
873
      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
32
    case 2:
303
32
      aom_write_symbol(w, eob_pt - 1,
304
32
                       ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx], 7);
305
32
      break;
306
273
    case 3:
307
273
      aom_write_symbol(w, eob_pt - 1,
308
273
                       ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx], 8);
309
273
      break;
310
1.52k
    case 4:
311
1.52k
      aom_write_symbol(w, eob_pt - 1,
312
1.52k
                       ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx], 9);
313
1.52k
      break;
314
147
    case 5:
315
147
      aom_write_symbol(w, eob_pt - 1,
316
147
                       ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx], 10);
317
147
      break;
318
877
    default:
319
877
      aom_write_symbol(w, eob_pt - 1,
320
877
                       ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11);
321
877
      break;
322
3.72k
  }
323
324
3.72k
  const int eob_offset_bits = av1_eob_offset_bits[eob_pt];
325
3.72k
  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
3.72k
  const int width = get_txb_wide(tx_size);
339
3.72k
  const int height = get_txb_high(tx_size);
340
3.72k
  uint8_t levels_buf[TX_PAD_2D];
341
3.72k
  uint8_t *const levels = set_levels(levels_buf, width);
342
3.72k
  const tran_low_t *tcoeff_txb =
343
3.72k
      cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type];
344
3.72k
  const tran_low_t *tcoeff = tcoeff_txb + BLOCK_OFFSET(block);
345
3.72k
  av1_txb_init_levels(tcoeff, width, height, levels);
346
3.72k
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
347
3.72k
  const int16_t *const scan = scan_order->scan;
348
3.72k
  DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
349
3.72k
  av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
350
351
3.72k
  const int bwl = get_txb_bwl(tx_size);
352
7.44k
  for (int c = eob - 1; c >= 0; --c) {
353
3.72k
    const int pos = scan[c];
354
3.72k
    const int coeff_ctx = coeff_contexts[pos];
355
3.72k
    const tran_low_t v = tcoeff[pos];
356
3.72k
    const tran_low_t level = abs(v);
357
358
3.72k
    if (c == eob - 1) {
359
3.72k
      aom_write_symbol(
360
3.72k
          w, AOMMIN(level, 3) - 1,
361
3.72k
          ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3);
362
3.72k
    } 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
3.72k
    if (level > NUM_BASE_LEVELS) {
368
      // level is above 1.
369
2.86k
      const int base_range = level - 1 - NUM_BASE_LEVELS;
370
2.86k
      const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
371
2.86k
      aom_cdf_prob *cdf =
372
2.86k
          ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx];
373
14.1k
      for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
374
11.4k
        const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
375
11.4k
        aom_write_symbol(w, k, cdf, BR_CDF_SIZE);
376
11.4k
        if (k < BR_CDF_SIZE - 1) break;
377
11.4k
      }
378
2.86k
    }
379
3.72k
  }
380
381
  // Loop to code all signs in the transform block,
382
  // starting with the sign of DC (if applicable)
383
7.44k
  for (int c = 0; c < eob; ++c) {
384
3.72k
    const tran_low_t v = tcoeff[scan[c]];
385
3.72k
    const tran_low_t level = abs(v);
386
3.72k
    const int sign = (v < 0) ? 1 : 0;
387
3.72k
    if (level) {
388
3.72k
      if (c == 0) {
389
3.72k
        const int dc_sign_ctx =
390
3.72k
            (entropy_ctx[block] >> DC_SIGN_CTX_SHIFT) & DC_SIGN_CTX_MASK;
391
3.72k
        aom_write_symbol(w, sign, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx],
392
3.72k
                         2);
393
3.72k
      } else {
394
0
        aom_write_bit(w, sign);
395
0
      }
396
3.72k
      if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS)
397
2.75k
        write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS);
398
3.72k
    }
399
3.72k
  }
400
3.72k
}
401
402
void av1_write_intra_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x,
403
27.6k
                               aom_writer *w, BLOCK_SIZE bsize) {
404
27.6k
  MACROBLOCKD *xd = &x->e_mbd;
405
27.6k
  const int num_planes = av1_num_planes(cm);
406
27.6k
  int block[MAX_MB_PLANE] = { 0 };
407
27.6k
  int row, col;
408
27.6k
  assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
409
27.6k
                                       xd->plane[0].subsampling_y));
410
27.6k
  const int max_blocks_wide = max_block_wide(xd, bsize, 0);
411
27.6k
  const int max_blocks_high = max_block_high(xd, bsize, 0);
412
27.6k
  const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
413
27.6k
  int mu_blocks_wide = mi_size_wide[max_unit_bsize];
414
27.6k
  int mu_blocks_high = mi_size_high[max_unit_bsize];
415
27.6k
  mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
416
27.6k
  mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
417
418
55.3k
  for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
419
55.3k
    for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
420
110k
      for (int plane = 0; plane < num_planes; ++plane) {
421
83.0k
        if (plane && !xd->is_chroma_ref) break;
422
83.0k
        const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
423
83.0k
        const int stepr = tx_size_high_unit[tx_size];
424
83.0k
        const int stepc = tx_size_wide_unit[tx_size];
425
83.0k
        const int step = stepr * stepc;
426
83.0k
        const struct macroblockd_plane *const pd = &xd->plane[plane];
427
83.0k
        const int unit_height = ROUND_POWER_OF_TWO(
428
83.0k
            AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y);
429
83.0k
        const int unit_width = ROUND_POWER_OF_TWO(
430
83.0k
            AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x);
431
285k
        for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height;
432
202k
             blk_row += stepr) {
433
1.07M
          for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width;
434
877k
               blk_col += stepc) {
435
877k
            av1_write_coeffs_txb(cm, x, w, blk_row, blk_col, plane,
436
877k
                                 block[plane], tx_size);
437
877k
            block[plane] += step;
438
877k
          }
439
202k
        }
440
83.0k
      }
441
27.6k
    }
442
27.6k
  }
443
27.6k
}
444
445
uint8_t av1_get_txb_entropy_context(const tran_low_t *qcoeff,
446
13.1M
                                    const SCAN_ORDER *scan_order, int eob) {
447
13.1M
  const int16_t *const scan = scan_order->scan;
448
13.1M
  int cul_level = 0;
449
13.1M
  int c;
450
451
13.1M
  if (eob == 0) return 0;
452
82.9k
  for (c = 0; c < eob; ++c) {
453
64.9k
    cul_level += abs(qcoeff[scan[c]]);
454
64.9k
    if (cul_level > COEFF_CONTEXT_MASK) break;
455
64.9k
  }
456
457
65.2k
  cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
458
65.2k
  set_dc_sign(&cul_level, qcoeff[0]);
459
460
65.2k
  return (uint8_t)cul_level;
461
13.1M
}
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
3.72k
                                 uint8_t allow_update_cdf) {
468
3.72k
  MB_MODE_INFO *mbmi = xd->mi[0];
469
3.72k
  int is_inter = is_inter_block(mbmi);
470
3.72k
  const int reduced_tx_set_used = cm->features.reduced_tx_set_used;
471
3.72k
  FRAME_CONTEXT *fc = xd->tile_ctx;
472
3.72k
#if !CONFIG_ENTROPY_STATS
473
3.72k
  (void)counts;
474
3.72k
#endif  // !CONFIG_ENTROPY_STATS
475
476
  // Only y plane's tx_type is updated
477
3.72k
  if (plane > 0) return;
478
1.33k
  const TX_TYPE tx_type = av1_get_tx_type(xd, PLANE_TYPE_Y, blk_row, blk_col,
479
1.33k
                                          tx_size, reduced_tx_set_used);
480
1.33k
  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.33k
  } else {
485
1.33k
    if (cpi->oxcf.txfm_cfg.use_intra_dct_only) {
486
0
      assert(tx_type == DCT_DCT);
487
1.33k
    } 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.33k
  }
501
502
1.33k
  if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 &&
503
308
      cm->quant_params.base_qindex > 0 && !mbmi->skip_txfm &&
504
17
      !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
505
17
    const int eset = get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used);
506
17
    if (eset > 0) {
507
17
      const TxSetType tx_set_type =
508
17
          av1_get_ext_tx_set_type(tx_size, is_inter, reduced_tx_set_used);
509
17
      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
17
      } else {
520
17
        PREDICTION_MODE intra_dir;
521
17
        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
17
        else
525
17
          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
17
        if (allow_update_cdf) {
531
17
          update_cdf(
532
17
              fc->intra_ext_tx_cdf[eset][txsize_sqr_map[tx_size]][intra_dir],
533
17
              av1_ext_tx_ind[tx_set_type][tx_type],
534
17
              av1_num_ext_tx_set[tx_set_type]);
535
17
        }
536
17
      }
537
17
    }
538
17
  }
539
1.33k
}
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
1.76M
                                       TX_SIZE tx_size, void *arg) {
544
1.76M
  struct tokenize_b_args *const args = arg;
545
1.76M
  const AV1_COMP *cpi = args->cpi;
546
1.76M
  const AV1_COMMON *cm = &cpi->common;
547
1.76M
  ThreadData *const td = args->td;
548
1.76M
  MACROBLOCK *const x = &td->mb;
549
1.76M
  MACROBLOCKD *const xd = &x->e_mbd;
550
1.76M
  struct macroblock_plane *p = &x->plane[plane];
551
1.76M
  struct macroblockd_plane *pd = &xd->plane[plane];
552
1.76M
  const int eob = p->eobs[block];
553
1.76M
  const int block_offset = BLOCK_OFFSET(block);
554
1.76M
  tran_low_t *qcoeff = p->qcoeff + block_offset;
555
1.76M
  const PLANE_TYPE plane_type = pd->plane_type;
556
1.76M
  const TX_TYPE tx_type =
557
1.76M
      av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
558
1.76M
                      cm->features.reduced_tx_set_used);
559
1.76M
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
560
1.76M
  tran_low_t *tcoeff;
561
1.76M
  assert(args->dry_run != DRY_RUN_COSTCOEFFS);
562
1.76M
  if (args->dry_run == OUTPUT_ENABLED) {
563
877k
    MB_MODE_INFO *mbmi = xd->mi[0];
564
877k
    TXB_CTX txb_ctx;
565
877k
    get_txb_ctx(plane_bsize, tx_size, plane,
566
877k
                pd->above_entropy_context + blk_col,
567
877k
                pd->left_entropy_context + blk_row, &txb_ctx);
568
877k
    const int bwl = get_txb_bwl(tx_size);
569
877k
    const int width = get_txb_wide(tx_size);
570
877k
    const int height = get_txb_high(tx_size);
571
877k
    const uint8_t allow_update_cdf = args->allow_update_cdf;
572
877k
    const TX_SIZE txsize_ctx = get_txsize_entropy_ctx(tx_size);
573
877k
    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
877k
    if (allow_update_cdf) {
579
877k
      update_cdf(ec_ctx->txb_skip_cdf[txsize_ctx][txb_ctx.txb_skip_ctx],
580
877k
                 eob == 0, 2);
581
877k
    }
582
583
877k
    CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff;
584
877k
    const int txb_offset = x->mbmi_ext_frame->cb_offset[plane_type] /
585
877k
                           (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
586
877k
    uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset;
587
877k
    uint8_t *const entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset;
588
877k
    entropy_ctx[block] = txb_ctx.txb_skip_ctx;
589
877k
    eob_txb[block] = eob;
590
591
877k
    if (eob == 0) {
592
873k
      av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, 0, blk_col,
593
873k
                               blk_row);
594
873k
      return;
595
873k
    }
596
3.81k
    const int segment_id = mbmi->segment_id;
597
3.81k
    const int seg_eob = av1_get_tx_eob(&cpi->common.seg, segment_id, tx_size);
598
3.81k
    tran_low_t *tcoeff_txb =
599
3.81k
        cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type];
600
3.81k
    tcoeff = tcoeff_txb + block_offset;
601
3.81k
    memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
602
603
3.81k
    uint8_t levels_buf[TX_PAD_2D];
604
3.81k
    uint8_t *const levels = set_levels(levels_buf, width);
605
3.81k
    av1_txb_init_levels(tcoeff, width, height, levels);
606
3.81k
    update_tx_type_count(cpi, cm, xd, blk_row, blk_col, plane, tx_size,
607
3.81k
                         td->counts, allow_update_cdf);
608
609
3.81k
    const TX_CLASS tx_class = tx_type_to_class[tx_type];
610
3.81k
    const int16_t *const scan = scan_order->scan;
611
612
    // record tx type usage
613
3.81k
    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
3.81k
    av1_update_eob_context(eob, tx_size, tx_class, plane_type, ec_ctx,
620
3.81k
                           allow_update_cdf);
621
3.81k
#endif
622
623
3.81k
    DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
624
3.81k
    av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class,
625
3.81k
                            coeff_contexts);
626
627
7.53k
    for (int c = eob - 1; c >= 0; --c) {
628
3.72k
      const int pos = scan[c];
629
3.72k
      const int coeff_ctx = coeff_contexts[pos];
630
3.72k
      const tran_low_t v = qcoeff[pos];
631
3.72k
      const tran_low_t level = abs(v);
632
3.72k
      td->abs_sum_level += level;
633
634
3.72k
      if (allow_update_cdf) {
635
3.72k
        if (c == eob - 1) {
636
3.72k
          assert(coeff_ctx < 4);
637
3.72k
          update_cdf(
638
3.72k
              ec_ctx->coeff_base_eob_cdf[txsize_ctx][plane_type][coeff_ctx],
639
3.72k
              AOMMIN(level, 3) - 1, 3);
640
3.72k
        } 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
3.72k
      }
645
3.72k
      if (c == eob - 1) {
646
3.72k
        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
3.72k
      }
655
3.72k
      if (level > NUM_BASE_LEVELS) {
656
2.86k
        const int base_range = level - 1 - NUM_BASE_LEVELS;
657
2.86k
        const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
658
14.1k
        for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
659
11.4k
          const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
660
11.4k
          if (allow_update_cdf) {
661
11.4k
            update_cdf(ec_ctx->coeff_br_cdf[AOMMIN(txsize_ctx, TX_32X32)]
662
11.4k
                                           [plane_type][br_ctx],
663
11.4k
                       k, BR_CDF_SIZE);
664
11.4k
          }
665
45.4k
          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
34.1k
            if (lps == k) break;
671
34.1k
          }
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
11.4k
          if (k < BR_CDF_SIZE - 1) break;
677
11.4k
        }
678
2.86k
      }
679
3.72k
    }
680
    // Update the context needed to code the DC sign (if applicable)
681
3.81k
    if (tcoeff[0] != 0) {
682
3.72k
      const int dc_sign = (tcoeff[0] < 0) ? 1 : 0;
683
3.72k
      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
3.72k
      if (allow_update_cdf)
688
3.72k
        update_cdf(ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], dc_sign, 2);
689
3.72k
      entropy_ctx[block] |= dc_sign_ctx << DC_SIGN_CTX_SHIFT;
690
3.72k
    }
691
891k
  } else {
692
891k
    tcoeff = qcoeff;
693
891k
  }
694
895k
  const uint8_t cul_level =
695
895k
      av1_get_txb_entropy_context(tcoeff, scan_order, eob);
696
895k
  av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level,
697
895k
                           blk_col, blk_row);
698
895k
}
699
700
void av1_update_intra_mb_txb_context(const AV1_COMP *cpi, ThreadData *td,
701
                                     RUN_TYPE dry_run, BLOCK_SIZE bsize,
702
78.4k
                                     uint8_t allow_update_cdf) {
703
78.4k
  const AV1_COMMON *const cm = &cpi->common;
704
78.4k
  const int num_planes = av1_num_planes(cm);
705
78.4k
  MACROBLOCK *const x = &td->mb;
706
78.4k
  MACROBLOCKD *const xd = &x->e_mbd;
707
78.4k
  MB_MODE_INFO *const mbmi = xd->mi[0];
708
78.4k
  struct tokenize_b_args arg = { cpi, td, 0, allow_update_cdf, dry_run };
709
78.4k
  if (mbmi->skip_txfm) {
710
0
    av1_reset_entropy_context(xd, bsize, num_planes);
711
0
    return;
712
0
  }
713
714
313k
  for (int plane = 0; plane < num_planes; ++plane) {
715
235k
    if (plane && !xd->is_chroma_ref) break;
716
235k
    const struct macroblockd_plane *const pd = &xd->plane[plane];
717
235k
    const int ss_x = pd->subsampling_x;
718
235k
    const int ss_y = pd->subsampling_y;
719
235k
    const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
720
235k
    av1_foreach_transformed_block_in_plane(
721
235k
        xd, plane_bsize, plane, av1_update_and_record_txb_context, &arg);
722
235k
  }
723
78.4k
}
724
725
CB_COEFF_BUFFER *av1_get_cb_coeff_buffer(const struct AV1_COMP *cpi, int mi_row,
726
16.9k
                                         int mi_col) {
727
16.9k
  const AV1_COMMON *const cm = &cpi->common;
728
16.9k
  const int mib_size_log2 = cm->seq_params->mib_size_log2;
729
16.9k
  const int stride = (cm->mi_params.mi_cols >> mib_size_log2) + 1;
730
16.9k
  const int offset =
731
16.9k
      (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2);
732
16.9k
  return cpi->coeff_buffer_base + offset;
733
16.9k
}