Coverage Report

Created: 2026-04-01 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aom/av1/decoder/decodetxb.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/decoder/decodetxb.h"
13
14
#include "aom_ports/mem.h"
15
#include "av1/common/idct.h"
16
#include "av1/common/scan.h"
17
#include "av1/common/txb_common.h"
18
#include "av1/decoder/decodemv.h"
19
20
#define ACCT_STR __func__
21
22
2.29M
static int read_golomb(MACROBLOCKD *xd, aom_reader *r) {
23
2.29M
  int x = 1;
24
2.29M
  int length = 0;
25
2.29M
  int i = 0;
26
27
10.0M
  while (!i) {
28
7.71M
    i = aom_read_bit(r, ACCT_STR);
29
7.71M
    ++length;
30
7.71M
    if (length > 20) {
31
255
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
32
255
                         "Invalid length in read_golomb");
33
255
      break;
34
255
    }
35
7.71M
  }
36
37
7.72M
  for (i = 0; i < length - 1; ++i) {
38
5.42M
    x <<= 1;
39
5.42M
    x += aom_read_bit(r, ACCT_STR);
40
5.42M
  }
41
42
2.29M
  return x - 1;
43
2.29M
}
44
45
4.89M
static inline int rec_eob_pos(const int eob_token, const int extra) {
46
4.89M
  int eob = av1_eob_group_start[eob_token];
47
4.89M
  if (eob > 2) {
48
2.87M
    eob += extra;
49
2.87M
  }
50
4.89M
  return eob;
51
4.89M
}
52
53
static inline int get_dqv(const int16_t *dequant, int coeff_idx,
54
39.6M
                          const qm_val_t *iqmatrix) {
55
39.6M
  int dqv = dequant[!!coeff_idx];
56
39.6M
  if (iqmatrix != NULL)
57
5.04M
    dqv =
58
5.04M
        ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
59
39.6M
  return dqv;
60
39.6M
}
61
62
static inline void read_coeffs_reverse_2d(aom_reader *r, TX_SIZE tx_size,
63
                                          int start_si, int end_si,
64
                                          const int16_t *scan, int bhl,
65
                                          uint8_t *levels,
66
                                          base_cdf_arr base_cdf,
67
2.88M
                                          br_cdf_arr br_cdf) {
68
78.1M
  for (int c = end_si; c >= start_si; --c) {
69
75.2M
    const int pos = scan[c];
70
75.2M
    const int coeff_ctx = get_lower_levels_ctx_2d(levels, pos, bhl, tx_size);
71
75.2M
    const int nsymbs = 4;
72
75.2M
    int level = aom_read_symbol(r, base_cdf[coeff_ctx], nsymbs, ACCT_STR);
73
75.2M
    if (level > NUM_BASE_LEVELS) {
74
10.6M
      const int br_ctx = get_br_ctx_2d(levels, pos, bhl);
75
10.6M
      aom_cdf_prob *cdf = br_cdf[br_ctx];
76
22.1M
      for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
77
20.3M
        const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR);
78
20.3M
        level += k;
79
20.3M
        if (k < BR_CDF_SIZE - 1) break;
80
20.3M
      }
81
10.6M
    }
82
75.2M
    levels[get_padded_idx(pos, bhl)] = level;
83
75.2M
  }
84
2.88M
}
85
86
static inline void read_coeffs_reverse(aom_reader *r, TX_SIZE tx_size,
87
                                       TX_CLASS tx_class, int start_si,
88
                                       int end_si, const int16_t *scan, int bhl,
89
                                       uint8_t *levels, base_cdf_arr base_cdf,
90
2.97M
                                       br_cdf_arr br_cdf) {
91
8.03M
  for (int c = end_si; c >= start_si; --c) {
92
5.06M
    const int pos = scan[c];
93
5.06M
    const int coeff_ctx =
94
5.06M
        get_lower_levels_ctx(levels, pos, bhl, tx_size, tx_class);
95
5.06M
    const int nsymbs = 4;
96
5.06M
    int level = aom_read_symbol(r, base_cdf[coeff_ctx], nsymbs, ACCT_STR);
97
5.06M
    if (level > NUM_BASE_LEVELS) {
98
1.40M
      const int br_ctx = get_br_ctx(levels, pos, bhl, tx_class);
99
1.40M
      aom_cdf_prob *cdf = br_cdf[br_ctx];
100
3.35M
      for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
101
3.04M
        const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR);
102
3.04M
        level += k;
103
3.04M
        if (k < BR_CDF_SIZE - 1) break;
104
3.04M
      }
105
1.40M
    }
106
5.06M
    levels[get_padded_idx(pos, bhl)] = level;
107
5.06M
  }
108
2.97M
}
109
110
static uint8_t read_coeffs_txb(const AV1_COMMON *const cm,
111
                               DecoderCodingBlock *dcb, aom_reader *const r,
112
                               const int blk_row, const int blk_col,
113
                               const int plane, const TXB_CTX *const txb_ctx,
114
13.2M
                               const TX_SIZE tx_size) {
115
13.2M
  MACROBLOCKD *const xd = &dcb->xd;
116
13.2M
  FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
117
13.2M
  const int32_t max_value = (1 << (7 + xd->bd)) - 1;
118
13.2M
  const int32_t min_value = -(1 << (7 + xd->bd));
119
13.2M
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
120
13.2M
  const PLANE_TYPE plane_type = get_plane_type(plane);
121
13.2M
  MB_MODE_INFO *const mbmi = xd->mi[0];
122
13.2M
  struct macroblockd_plane *const pd = &xd->plane[plane];
123
13.2M
  const int16_t *const dequant = pd->seg_dequant_QTX[mbmi->segment_id];
124
13.2M
  tran_low_t *const tcoeffs = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane];
125
13.2M
  const int shift = av1_get_tx_scale(tx_size);
126
13.2M
  const int bhl = get_txb_bhl(tx_size);
127
13.2M
  const int width = get_txb_wide(tx_size);
128
13.2M
  const int height = get_txb_high(tx_size);
129
13.2M
  int cul_level = 0;
130
13.2M
  int dc_val = 0;
131
13.2M
  uint8_t levels_buf[TX_PAD_2D];
132
13.2M
  uint8_t *const levels = set_levels(levels_buf, height);
133
13.2M
  const int all_zero = aom_read_symbol(
134
13.2M
      r, ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2, ACCT_STR);
135
13.2M
  eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
136
13.2M
  uint16_t *const eob = &(eob_data->eob);
137
13.2M
  uint16_t *const max_scan_line = &(eob_data->max_scan_line);
138
13.2M
  *max_scan_line = 0;
139
13.2M
  *eob = 0;
140
141
#if CONFIG_INSPECTION
142
  if (plane == 0) {
143
    const int txk_type_idx =
144
        av1_get_txk_type_index(mbmi->bsize, blk_row, blk_col);
145
    mbmi->tx_skip[txk_type_idx] = all_zero;
146
  }
147
#endif
148
149
13.2M
  if (all_zero) {
150
8.29M
    *max_scan_line = 0;
151
8.29M
    if (plane == 0) {
152
2.55M
      xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col] = DCT_DCT;
153
2.55M
    }
154
8.29M
    return 0;
155
8.29M
  }
156
157
4.97M
  if (plane == AOM_PLANE_Y) {
158
    // only y plane's tx_type is transmitted
159
2.37M
    av1_read_tx_type(cm, xd, blk_row, blk_col, tx_size, r);
160
2.37M
  }
161
4.97M
  const TX_TYPE tx_type =
162
4.97M
      av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
163
4.97M
                      cm->features.reduced_tx_set_used);
164
4.97M
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
165
4.97M
  const qm_val_t *iqmatrix =
166
4.97M
      av1_get_iqmatrix(&cm->quant_params, xd, plane, tx_size, tx_type);
167
4.97M
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
168
4.97M
  const int16_t *const scan = scan_order->scan;
169
4.97M
  int eob_extra = 0;
170
4.97M
  int eob_pt = 1;
171
172
4.97M
  const int eob_multi_size = txsize_log2_minus4[tx_size];
173
4.97M
  const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
174
4.97M
  switch (eob_multi_size) {
175
3.19M
    case 0:
176
3.19M
      eob_pt =
177
3.19M
          aom_read_symbol(r, ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx],
178
3.19M
                          5, ACCT_STR) +
179
3.19M
          1;
180
3.19M
      break;
181
267k
    case 1:
182
267k
      eob_pt =
183
267k
          aom_read_symbol(r, ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx],
184
267k
                          6, ACCT_STR) +
185
267k
          1;
186
267k
      break;
187
633k
    case 2:
188
633k
      eob_pt =
189
633k
          aom_read_symbol(r, ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx],
190
633k
                          7, ACCT_STR) +
191
633k
          1;
192
633k
      break;
193
249k
    case 3:
194
249k
      eob_pt =
195
249k
          aom_read_symbol(r, ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx],
196
249k
                          8, ACCT_STR) +
197
249k
          1;
198
249k
      break;
199
291k
    case 4:
200
291k
      eob_pt =
201
291k
          aom_read_symbol(r, ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx],
202
291k
                          9, ACCT_STR) +
203
291k
          1;
204
291k
      break;
205
96.8k
    case 5:
206
96.8k
      eob_pt =
207
96.8k
          aom_read_symbol(r, ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx],
208
96.8k
                          10, ACCT_STR) +
209
96.8k
          1;
210
96.8k
      break;
211
190k
    case 6:
212
190k
    default:
213
190k
      eob_pt = aom_read_symbol(
214
190k
                   r, ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11,
215
190k
                   ACCT_STR) +
216
190k
               1;
217
190k
      break;
218
4.97M
  }
219
220
4.89M
  const int eob_offset_bits = av1_eob_offset_bits[eob_pt];
221
4.89M
  if (eob_offset_bits > 0) {
222
2.87M
    const int eob_ctx = eob_pt - 3;
223
2.87M
    int bit = aom_read_symbol(
224
2.87M
        r, ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2, ACCT_STR);
225
2.87M
    if (bit) {
226
1.74M
      eob_extra += (1 << (eob_offset_bits - 1));
227
1.74M
    }
228
229
9.55M
    for (int i = 1; i < eob_offset_bits; i++) {
230
6.67M
      bit = aom_read_bit(r, ACCT_STR);
231
6.67M
      if (bit) {
232
3.72M
        eob_extra += (1 << (eob_offset_bits - 1 - i));
233
3.72M
      }
234
6.67M
    }
235
2.87M
  }
236
4.89M
  *eob = rec_eob_pos(eob_pt, eob_extra);
237
238
4.89M
  if (*eob > 1) {
239
2.96M
    memset(levels_buf, 0,
240
2.96M
           sizeof(*levels_buf) *
241
2.96M
               ((height + TX_PAD_HOR) * (width + TX_PAD_VER) + TX_PAD_END));
242
2.96M
  }
243
244
4.89M
  {
245
    // Read the non-zero coefficient with scan index eob-1
246
    // TODO(angiebird): Put this into a function
247
4.89M
    const int c = *eob - 1;
248
4.89M
    const int pos = scan[c];
249
4.89M
    const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, c);
250
4.89M
    const int nsymbs = 3;
251
4.89M
    aom_cdf_prob *cdf =
252
4.89M
        ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx];
253
4.89M
    int level = aom_read_symbol(r, cdf, nsymbs, ACCT_STR) + 1;
254
4.89M
    if (level > NUM_BASE_LEVELS) {
255
531k
      const int br_ctx = get_br_ctx_eob(pos, bhl, tx_class);
256
531k
      cdf = ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx];
257
1.49M
      for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
258
1.30M
        const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR);
259
1.30M
        level += k;
260
1.30M
        if (k < BR_CDF_SIZE - 1) break;
261
1.30M
      }
262
531k
    }
263
4.89M
    levels[get_padded_idx(pos, bhl)] = level;
264
4.89M
  }
265
4.89M
  if (*eob > 1) {
266
2.96M
    base_cdf_arr base_cdf = ec_ctx->coeff_base_cdf[txs_ctx][plane_type];
267
2.96M
    br_cdf_arr br_cdf =
268
2.96M
        ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type];
269
2.96M
    if (tx_class == TX_CLASS_2D) {
270
2.88M
      read_coeffs_reverse_2d(r, tx_size, 1, *eob - 1 - 1, scan, bhl, levels,
271
2.88M
                             base_cdf, br_cdf);
272
2.88M
      read_coeffs_reverse(r, tx_size, tx_class, 0, 0, scan, bhl, levels,
273
2.88M
                          base_cdf, br_cdf);
274
2.88M
    } else {
275
81.8k
      read_coeffs_reverse(r, tx_size, tx_class, 0, *eob - 1 - 1, scan, bhl,
276
81.8k
                          levels, base_cdf, br_cdf);
277
81.8k
    }
278
2.96M
  }
279
280
90.1M
  for (int c = 0; c < *eob; ++c) {
281
85.2M
    const int pos = scan[c];
282
85.2M
    uint8_t sign;
283
85.2M
    tran_low_t level = levels[get_padded_idx(pos, bhl)];
284
85.2M
    if (level) {
285
39.6M
      *max_scan_line = AOMMAX(*max_scan_line, pos);
286
39.6M
      if (c == 0) {
287
4.22M
        const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
288
4.22M
        sign = aom_read_symbol(r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx],
289
4.22M
                               2, ACCT_STR);
290
35.4M
      } else {
291
35.4M
        sign = aom_read_bit(r, ACCT_STR);
292
35.4M
      }
293
39.6M
      if (level >= MAX_BASE_BR_RANGE) {
294
2.29M
        level += read_golomb(xd, r);
295
2.29M
      }
296
297
39.6M
      if (c == 0) dc_val = sign ? -level : level;
298
299
      // Bitmasking to clamp level to valid range:
300
      //   The valid range for 8/10/12 bit vdieo is at most 14/16/18 bit
301
39.6M
      level &= 0xfffff;
302
39.6M
      cul_level += level;
303
39.6M
      tran_low_t dq_coeff;
304
      // Bitmasking to clamp dq_coeff to valid range:
305
      //   The valid range for 8/10/12 bit video is at most 17/19/21 bit
306
39.6M
      dq_coeff =
307
39.6M
          (tran_low_t)((int64_t)level * get_dqv(dequant, scan[c], iqmatrix) &
308
39.6M
                       0xffffff);
309
39.6M
      dq_coeff = dq_coeff >> shift;
310
39.6M
      if (sign) {
311
19.7M
        dq_coeff = -dq_coeff;
312
19.7M
      }
313
39.6M
      tcoeffs[pos] = clamp(dq_coeff, min_value, max_value);
314
39.6M
    }
315
85.2M
  }
316
317
4.89M
  cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
318
319
  // DC value
320
4.89M
  set_dc_sign(&cul_level, dc_val);
321
322
4.89M
  return cul_level;
323
4.97M
}
324
325
void av1_read_coeffs_txb(const AV1_COMMON *const cm, DecoderCodingBlock *dcb,
326
                         aom_reader *const r, const int plane, const int row,
327
13.2M
                         const int col, const TX_SIZE tx_size) {
328
#if TXCOEFF_TIMER
329
  struct aom_usec_timer timer;
330
  aom_usec_timer_start(&timer);
331
#endif
332
13.2M
  MACROBLOCKD *const xd = &dcb->xd;
333
13.2M
  MB_MODE_INFO *const mbmi = xd->mi[0];
334
13.2M
  struct macroblockd_plane *const pd = &xd->plane[plane];
335
336
13.2M
  const BLOCK_SIZE bsize = mbmi->bsize;
337
13.2M
  assert(bsize < BLOCK_SIZES_ALL);
338
13.2M
  const BLOCK_SIZE plane_bsize =
339
13.2M
      get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
340
341
13.2M
  TXB_CTX txb_ctx;
342
13.2M
  get_txb_ctx(plane_bsize, tx_size, plane, pd->above_entropy_context + col,
343
13.2M
              pd->left_entropy_context + row, &txb_ctx);
344
13.2M
  const uint8_t cul_level =
345
13.2M
      read_coeffs_txb(cm, dcb, r, row, col, plane, &txb_ctx, tx_size);
346
13.2M
  av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, col,
347
13.2M
                           row);
348
349
13.2M
  if (is_inter_block(mbmi)) {
350
220k
    const PLANE_TYPE plane_type = get_plane_type(plane);
351
    // tx_type will be read out in av1_read_coeffs_txb_facade
352
220k
    const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, row, col, tx_size,
353
220k
                                            cm->features.reduced_tx_set_used);
354
355
220k
    if (plane == 0) {
356
99.1k
      const int txw = tx_size_wide_unit[tx_size];
357
99.1k
      const int txh = tx_size_high_unit[tx_size];
358
      // The 16x16 unit is due to the constraint from tx_64x64 which sets the
359
      // maximum tx size for chroma as 32x32. Coupled with 4x1 transform block
360
      // size, the constraint takes effect in 32x16 / 16x32 size too. To solve
361
      // the intricacy, cover all the 16x16 units inside a 64 level transform.
362
99.1k
      if (txw == tx_size_wide_unit[TX_64X64] ||
363
96.3k
          txh == tx_size_high_unit[TX_64X64]) {
364
3.08k
        const int tx_unit = tx_size_wide_unit[TX_16X16];
365
3.08k
        const int stride = xd->tx_type_map_stride;
366
14.3k
        for (int idy = 0; idy < txh; idy += tx_unit) {
367
53.8k
          for (int idx = 0; idx < txw; idx += tx_unit) {
368
42.5k
            xd->tx_type_map[(row + idy) * stride + col + idx] = tx_type;
369
42.5k
          }
370
11.2k
        }
371
3.08k
      }
372
99.1k
    }
373
220k
  }
374
375
#if TXCOEFF_TIMER
376
  aom_usec_timer_mark(&timer);
377
  const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);
378
  cm->txcoeff_timer += elapsed_time;
379
  ++cm->txb_count;
380
#endif
381
13.2M
}