Coverage Report

Created: 2026-01-16 07:04

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