Coverage Report

Created: 2025-12-31 07:53

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.32M
static int read_golomb(MACROBLOCKD *xd, aom_reader *r) {
23
2.32M
  int x = 1;
24
2.32M
  int length = 0;
25
2.32M
  int i = 0;
26
27
7.21M
  while (!i) {
28
4.89M
    i = aom_read_bit(r, ACCT_STR);
29
4.89M
    ++length;
30
4.89M
    if (length > 20) {
31
536
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
32
536
                         "Invalid length in read_golomb");
33
536
      break;
34
536
    }
35
4.89M
  }
36
37
4.88M
  for (i = 0; i < length - 1; ++i) {
38
2.56M
    x <<= 1;
39
2.56M
    x += aom_read_bit(r, ACCT_STR);
40
2.56M
  }
41
42
2.32M
  return x - 1;
43
2.32M
}
44
45
6.15M
static INLINE int rec_eob_pos(const int eob_token, const int extra) {
46
6.15M
  int eob = av1_eob_group_start[eob_token];
47
6.15M
  if (eob > 2) {
48
3.91M
    eob += extra;
49
3.91M
  }
50
6.15M
  return eob;
51
6.15M
}
52
53
static INLINE int get_dqv(const int16_t *dequant, int coeff_idx,
54
55.0M
                          const qm_val_t *iqmatrix) {
55
55.0M
  int dqv = dequant[!!coeff_idx];
56
55.0M
  if (iqmatrix != NULL)
57
11.8M
    dqv =
58
11.8M
        ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
59
55.0M
  return dqv;
60
55.0M
}
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 bwl,
65
                                          uint8_t *levels,
66
                                          base_cdf_arr base_cdf,
67
3.85M
                                          br_cdf_arr br_cdf) {
68
120M
  for (int c = end_si; c >= start_si; --c) {
69
116M
    const int pos = scan[c];
70
116M
    const int coeff_ctx = get_lower_levels_ctx_2d(levels, pos, bwl, tx_size);
71
116M
    const int nsymbs = 4;
72
116M
    int level = aom_read_symbol(r, base_cdf[coeff_ctx], nsymbs, ACCT_STR);
73
116M
    if (level > NUM_BASE_LEVELS) {
74
12.7M
      const int br_ctx = get_br_ctx_2d(levels, pos, bwl);
75
12.7M
      aom_cdf_prob *cdf = br_cdf[br_ctx];
76
25.6M
      for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
77
23.5M
        const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR);
78
23.5M
        level += k;
79
23.5M
        if (k < BR_CDF_SIZE - 1) break;
80
23.5M
      }
81
12.7M
    }
82
116M
    levels[get_padded_idx(pos, bwl)] = level;
83
116M
  }
84
3.85M
}
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 bwl,
89
                                       uint8_t *levels, base_cdf_arr base_cdf,
90
4.11M
                                       br_cdf_arr br_cdf) {
91
14.1M
  for (int c = end_si; c >= start_si; --c) {
92
10.0M
    const int pos = scan[c];
93
10.0M
    const int coeff_ctx =
94
10.0M
        get_lower_levels_ctx(levels, pos, bwl, tx_size, tx_class);
95
10.0M
    const int nsymbs = 4;
96
10.0M
    int level = aom_read_symbol(r, base_cdf[coeff_ctx], nsymbs, ACCT_STR);
97
10.0M
    if (level > NUM_BASE_LEVELS) {
98
1.80M
      const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
99
1.80M
      aom_cdf_prob *cdf = br_cdf[br_ctx];
100
3.46M
      for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
101
3.25M
        const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR);
102
3.25M
        level += k;
103
3.25M
        if (k < BR_CDF_SIZE - 1) break;
104
3.25M
      }
105
1.80M
    }
106
10.0M
    levels[get_padded_idx(pos, bwl)] = level;
107
10.0M
  }
108
4.11M
}
109
110
uint8_t av1_read_coeffs_txb(const AV1_COMMON *const cm, DecoderCodingBlock *dcb,
111
                            aom_reader *const r, const int blk_row,
112
                            const int blk_col, const int plane,
113
                            const TXB_CTX *const txb_ctx,
114
12.0M
                            const TX_SIZE tx_size) {
115
12.0M
  MACROBLOCKD *const xd = &dcb->xd;
116
12.0M
  FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
117
12.0M
  const int32_t max_value = (1 << (7 + xd->bd)) - 1;
118
12.0M
  const int32_t min_value = -(1 << (7 + xd->bd));
119
12.0M
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
120
12.0M
  const PLANE_TYPE plane_type = get_plane_type(plane);
121
12.0M
  MB_MODE_INFO *const mbmi = xd->mi[0];
122
12.0M
  struct macroblockd_plane *const pd = &xd->plane[plane];
123
12.0M
  const int16_t *const dequant = pd->seg_dequant_QTX[mbmi->segment_id];
124
12.0M
  tran_low_t *const tcoeffs = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane];
125
12.0M
  const int shift = av1_get_tx_scale(tx_size);
126
12.0M
  const int bwl = get_txb_bwl(tx_size);
127
12.0M
  const int width = get_txb_wide(tx_size);
128
12.0M
  const int height = get_txb_high(tx_size);
129
12.0M
  int cul_level = 0;
130
12.0M
  int dc_val = 0;
131
12.0M
  uint8_t levels_buf[TX_PAD_2D];
132
12.0M
  uint8_t *const levels = set_levels(levels_buf, width);
133
12.0M
  const int all_zero = aom_read_symbol(
134
12.0M
      r, ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2, ACCT_STR);
135
12.0M
  eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
136
12.0M
  uint16_t *const eob = &(eob_data->eob);
137
12.0M
  uint16_t *const max_scan_line = &(eob_data->max_scan_line);
138
12.0M
  *max_scan_line = 0;
139
12.0M
  *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
12.0M
  if (all_zero) {
150
5.88M
    *max_scan_line = 0;
151
5.88M
    if (plane == 0) {
152
1.44M
      xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col] = DCT_DCT;
153
1.44M
    }
154
5.88M
    return 0;
155
5.88M
  }
156
157
6.15M
  if (plane == AOM_PLANE_Y) {
158
    // only y plane's tx_type is transmitted
159
3.51M
    av1_read_tx_type(cm, xd, blk_row, blk_col, tx_size, r);
160
3.51M
  }
161
6.15M
  const TX_TYPE tx_type =
162
6.15M
      av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
163
6.15M
                      cm->features.reduced_tx_set_used);
164
6.15M
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
165
6.15M
  const qm_val_t *iqmatrix =
166
6.15M
      av1_get_iqmatrix(&cm->quant_params, xd, plane, tx_size, tx_type);
167
6.15M
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
168
6.15M
  const int16_t *const scan = scan_order->scan;
169
6.15M
  int eob_extra = 0;
170
6.15M
  int eob_pt = 1;
171
172
6.15M
  const int eob_multi_size = txsize_log2_minus4[tx_size];
173
6.15M
  const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
174
6.15M
  switch (eob_multi_size) {
175
2.13M
    case 0:
176
2.13M
      eob_pt =
177
2.13M
          aom_read_symbol(r, ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx],
178
2.13M
                          5, ACCT_STR) +
179
2.13M
          1;
180
2.13M
      break;
181
558k
    case 1:
182
558k
      eob_pt =
183
558k
          aom_read_symbol(r, ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx],
184
558k
                          6, ACCT_STR) +
185
558k
          1;
186
558k
      break;
187
1.34M
    case 2:
188
1.34M
      eob_pt =
189
1.34M
          aom_read_symbol(r, ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx],
190
1.34M
                          7, ACCT_STR) +
191
1.34M
          1;
192
1.34M
      break;
193
588k
    case 3:
194
588k
      eob_pt =
195
588k
          aom_read_symbol(r, ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx],
196
588k
                          8, ACCT_STR) +
197
588k
          1;
198
588k
      break;
199
745k
    case 4:
200
745k
      eob_pt =
201
745k
          aom_read_symbol(r, ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx],
202
745k
                          9, ACCT_STR) +
203
745k
          1;
204
745k
      break;
205
223k
    case 5:
206
223k
      eob_pt =
207
223k
          aom_read_symbol(r, ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx],
208
223k
                          10, ACCT_STR) +
209
223k
          1;
210
223k
      break;
211
558k
    case 6:
212
558k
    default:
213
558k
      eob_pt = aom_read_symbol(
214
558k
                   r, ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11,
215
558k
                   ACCT_STR) +
216
558k
               1;
217
558k
      break;
218
6.15M
  }
219
220
6.15M
  const int eob_offset_bits = av1_eob_offset_bits[eob_pt];
221
6.15M
  if (eob_offset_bits > 0) {
222
3.91M
    const int eob_ctx = eob_pt - 3;
223
3.91M
    int bit = aom_read_symbol(
224
3.91M
        r, ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2, ACCT_STR);
225
3.91M
    if (bit) {
226
1.77M
      eob_extra += (1 << (eob_offset_bits - 1));
227
1.77M
    }
228
229
13.3M
    for (int i = 1; i < eob_offset_bits; i++) {
230
9.46M
      bit = aom_read_bit(r, ACCT_STR);
231
9.46M
      if (bit) {
232
4.75M
        eob_extra += (1 << (eob_offset_bits - 1 - i));
233
4.75M
      }
234
9.46M
    }
235
3.91M
  }
236
6.15M
  *eob = rec_eob_pos(eob_pt, eob_extra);
237
238
6.15M
  if (*eob > 1) {
239
4.11M
    memset(levels_buf, 0,
240
4.11M
           sizeof(*levels_buf) *
241
4.11M
               ((width + TX_PAD_HOR) * (height + TX_PAD_VER) + TX_PAD_END));
242
4.11M
  }
243
244
6.15M
  {
245
    // Read the non-zero coefficient with scan index eob-1
246
    // TODO(angiebird): Put this into a function
247
6.15M
    const int c = *eob - 1;
248
6.15M
    const int pos = scan[c];
249
6.15M
    const int coeff_ctx = get_lower_levels_ctx_eob(bwl, height, c);
250
6.15M
    const int nsymbs = 3;
251
6.15M
    aom_cdf_prob *cdf =
252
6.15M
        ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx];
253
6.15M
    int level = aom_read_symbol(r, cdf, nsymbs, ACCT_STR) + 1;
254
6.15M
    if (level > NUM_BASE_LEVELS) {
255
235k
      const int br_ctx = get_br_ctx_eob(pos, bwl, tx_class);
256
235k
      cdf = ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx];
257
400k
      for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
258
381k
        const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR);
259
381k
        level += k;
260
381k
        if (k < BR_CDF_SIZE - 1) break;
261
381k
      }
262
235k
    }
263
6.15M
    levels[get_padded_idx(pos, bwl)] = level;
264
6.15M
  }
265
6.15M
  if (*eob > 1) {
266
4.11M
    base_cdf_arr base_cdf = ec_ctx->coeff_base_cdf[txs_ctx][plane_type];
267
4.11M
    br_cdf_arr br_cdf =
268
4.11M
        ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type];
269
4.11M
    if (tx_class == TX_CLASS_2D) {
270
3.85M
      read_coeffs_reverse_2d(r, tx_size, 1, *eob - 1 - 1, scan, bwl, levels,
271
3.85M
                             base_cdf, br_cdf);
272
3.85M
      read_coeffs_reverse(r, tx_size, tx_class, 0, 0, scan, bwl, levels,
273
3.85M
                          base_cdf, br_cdf);
274
3.85M
    } else {
275
256k
      read_coeffs_reverse(r, tx_size, tx_class, 0, *eob - 1 - 1, scan, bwl,
276
256k
                          levels, base_cdf, br_cdf);
277
256k
    }
278
4.11M
  }
279
280
139M
  for (int c = 0; c < *eob; ++c) {
281
132M
    const int pos = scan[c];
282
132M
    uint8_t sign;
283
132M
    tran_low_t level = levels[get_padded_idx(pos, bwl)];
284
132M
    if (level) {
285
55.0M
      *max_scan_line = AOMMAX(*max_scan_line, pos);
286
55.0M
      if (c == 0) {
287
5.09M
        const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
288
5.09M
        sign = aom_read_symbol(r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx],
289
5.09M
                               2, ACCT_STR);
290
49.9M
      } else {
291
49.9M
        sign = aom_read_bit(r, ACCT_STR);
292
49.9M
      }
293
55.0M
      if (level >= MAX_BASE_BR_RANGE) {
294
2.32M
        level += read_golomb(xd, r);
295
2.32M
      }
296
297
55.0M
      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
55.0M
      level &= 0xfffff;
302
55.0M
      cul_level += level;
303
55.0M
      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
55.0M
      dq_coeff = (tran_low_t)(
307
55.0M
          (int64_t)level * get_dqv(dequant, scan[c], iqmatrix) & 0xffffff);
308
55.0M
      dq_coeff = dq_coeff >> shift;
309
55.0M
      if (sign) {
310
27.0M
        dq_coeff = -dq_coeff;
311
27.0M
      }
312
55.0M
      tcoeffs[pos] = clamp(dq_coeff, min_value, max_value);
313
55.0M
    }
314
132M
  }
315
316
6.15M
  cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
317
318
  // DC value
319
6.15M
  set_dc_sign(&cul_level, dc_val);
320
321
6.15M
  return cul_level;
322
6.15M
}
323
324
void av1_read_coeffs_txb_facade(const AV1_COMMON *const cm,
325
                                DecoderCodingBlock *dcb, aom_reader *const r,
326
                                const int plane, const int row, const int col,
327
12.0M
                                const TX_SIZE tx_size) {
328
#if TXCOEFF_TIMER
329
  struct aom_usec_timer timer;
330
  aom_usec_timer_start(&timer);
331
#endif
332
12.0M
  MACROBLOCKD *const xd = &dcb->xd;
333
12.0M
  MB_MODE_INFO *const mbmi = xd->mi[0];
334
12.0M
  struct macroblockd_plane *const pd = &xd->plane[plane];
335
336
12.0M
  const BLOCK_SIZE bsize = mbmi->bsize;
337
12.0M
  assert(bsize < BLOCK_SIZES_ALL);
338
12.0M
  const BLOCK_SIZE plane_bsize =
339
12.0M
      get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
340
341
12.0M
  TXB_CTX txb_ctx;
342
12.0M
  get_txb_ctx(plane_bsize, tx_size, plane, pd->above_entropy_context + col,
343
12.0M
              pd->left_entropy_context + row, &txb_ctx);
344
12.0M
  const uint8_t cul_level =
345
12.0M
      av1_read_coeffs_txb(cm, dcb, r, row, col, plane, &txb_ctx, tx_size);
346
12.0M
  av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, col,
347
12.0M
                           row);
348
349
12.0M
  if (is_inter_block(mbmi)) {
350
260k
    const PLANE_TYPE plane_type = get_plane_type(plane);
351
    // tx_type will be read out in av1_read_coeffs_txb_facade
352
260k
    const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, row, col, tx_size,
353
260k
                                            cm->features.reduced_tx_set_used);
354
355
260k
    if (plane == 0) {
356
123k
      const int txw = tx_size_wide_unit[tx_size];
357
123k
      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
123k
      if (txw == tx_size_wide_unit[TX_64X64] ||
363
121k
          txh == tx_size_high_unit[TX_64X64]) {
364
2.14k
        const int tx_unit = tx_size_wide_unit[TX_16X16];
365
2.14k
        const int stride = xd->tx_type_map_stride;
366
9.87k
        for (int idy = 0; idy < txh; idy += tx_unit) {
367
35.3k
          for (int idx = 0; idx < txw; idx += tx_unit) {
368
27.6k
            xd->tx_type_map[(row + idy) * stride + col + idx] = tx_type;
369
27.6k
          }
370
7.73k
        }
371
2.14k
      }
372
123k
    }
373
260k
  }
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
12.0M
}