Coverage Report

Created: 2025-08-03 06:57

/src/libvpx/vp9/decoder/vp9_detokenize.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include "vpx_mem/vpx_mem.h"
12
#include "vpx_ports/mem.h"
13
14
#include "vp9/common/vp9_blockd.h"
15
#include "vp9/common/vp9_common.h"
16
#include "vp9/common/vp9_entropy.h"
17
#if CONFIG_COEFFICIENT_RANGE_CHECKING
18
#include "vp9/common/vp9_idct.h"
19
#endif
20
21
#include "vp9/decoder/vp9_detokenize.h"
22
23
308M
#define EOB_CONTEXT_NODE 0
24
70.6M
#define ZERO_CONTEXT_NODE 1
25
27.7M
#define ONE_CONTEXT_NODE 2
26
27
#define INCREMENT_COUNT(token)                   \
28
350M
  do {                                           \
29
350M
    if (counts) ++coef_counts[band][ctx][token]; \
30
350M
  } while (0)
31
32
static INLINE int read_bool(vpx_reader *r, int prob, BD_VALUE *value,
33
472M
                            int *count, unsigned int *range) {
34
472M
  const unsigned int split = (*range * prob + (256 - prob)) >> CHAR_BIT;
35
472M
  const BD_VALUE bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
36
#if CONFIG_BITSTREAM_DEBUG
37
  const int queue_r = bitstream_queue_get_read();
38
  const int frame_idx = bitstream_queue_get_frame_read();
39
  int ref_result, ref_prob;
40
  bitstream_queue_pop(&ref_result, &ref_prob);
41
  if (prob != ref_prob) {
42
    fprintf(stderr,
43
            "\n *** [bit] prob error, frame_idx_r %d prob %d ref_prob %d "
44
            "queue_r %d\n",
45
            frame_idx, prob, ref_prob, queue_r);
46
47
    assert(0);
48
  }
49
#endif
50
51
472M
  if (*count < 0) {
52
2.88M
    r->value = *value;
53
2.88M
    r->count = *count;
54
2.88M
    vpx_reader_fill(r);
55
2.88M
    *value = r->value;
56
2.88M
    *count = r->count;
57
2.88M
  }
58
59
472M
  if (*value >= bigsplit) {
60
94.7M
    *range = *range - split;
61
94.7M
    *value = *value - bigsplit;
62
94.7M
    {
63
94.7M
      const int shift = vpx_norm[*range];
64
94.7M
      *range <<= shift;
65
94.7M
      *value <<= shift;
66
94.7M
      *count -= shift;
67
94.7M
    }
68
#if CONFIG_BITSTREAM_DEBUG
69
    {
70
      const int bit = 1;
71
      if (bit != ref_result) {
72
        fprintf(
73
            stderr,
74
            "\n *** [bit] result error, frame_idx_r %d bit %d ref_result %d "
75
            "queue_r %d\n",
76
            frame_idx, bit, ref_result, queue_r);
77
78
        assert(0);
79
      }
80
    }
81
#endif
82
94.7M
    return 1;
83
94.7M
  }
84
377M
  *range = split;
85
377M
  {
86
377M
    const int shift = vpx_norm[*range];
87
377M
    *range <<= shift;
88
377M
    *value <<= shift;
89
377M
    *count -= shift;
90
377M
  }
91
#if CONFIG_BITSTREAM_DEBUG
92
  {
93
    const int bit = 0;
94
    if (bit != ref_result) {
95
      fprintf(stderr,
96
              "\n *** [bit] result error, frame_idx_r %d bit %d ref_result %d "
97
              "queue_r %d\n",
98
              frame_idx, bit, ref_result, queue_r);
99
100
      assert(0);
101
    }
102
  }
103
#endif
104
377M
  return 0;
105
472M
}
106
107
static INLINE int read_coeff(vpx_reader *r, const vpx_prob *probs, int n,
108
2.40M
                             BD_VALUE *value, int *count, unsigned int *range) {
109
2.40M
  int i, val = 0;
110
10.0M
  for (i = 0; i < n; ++i)
111
7.69M
    val = (val << 1) | read_bool(r, probs[i], value, count, range);
112
2.40M
  return val;
113
2.40M
}
114
115
static int decode_coefs(const MACROBLOCKD *xd, PLANE_TYPE type,
116
                        tran_low_t *dqcoeff, TX_SIZE tx_size, const int16_t *dq,
117
                        int ctx, const int16_t *scan, const int16_t *nb,
118
281M
                        vpx_reader *r) {
119
281M
  FRAME_COUNTS *counts = xd->counts;
120
281M
  const int max_eob = 16 << (tx_size << 1);
121
281M
  const FRAME_CONTEXT *const fc = xd->fc;
122
281M
  const int ref = is_inter_block(xd->mi[0]);
123
281M
  int band, c = 0;
124
281M
  const vpx_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
125
281M
      fc->coef_probs[tx_size][type][ref];
126
281M
  const vpx_prob *prob;
127
281M
  unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
128
281M
  unsigned int(*eob_branch_count)[COEFF_CONTEXTS];
129
281M
  uint8_t token_cache[32 * 32];
130
281M
  const uint8_t *band_translate = get_band_translate(tx_size);
131
281M
  const int dq_shift = (tx_size == TX_32X32);
132
281M
  int v;
133
281M
  int16_t dqv = dq[0];
134
281M
  const uint8_t *const cat6_prob =
135
281M
#if CONFIG_VP9_HIGHBITDEPTH
136
281M
      (xd->bd == VPX_BITS_12)   ? vp9_cat6_prob_high12
137
281M
      : (xd->bd == VPX_BITS_10) ? vp9_cat6_prob_high12 + 2
138
267M
                                :
139
267M
#endif  // CONFIG_VP9_HIGHBITDEPTH
140
267M
                                vp9_cat6_prob;
141
281M
  const int cat6_bits =
142
281M
#if CONFIG_VP9_HIGHBITDEPTH
143
281M
      (xd->bd == VPX_BITS_12)   ? 18
144
281M
      : (xd->bd == VPX_BITS_10) ? 16
145
267M
                                :
146
267M
#endif  // CONFIG_VP9_HIGHBITDEPTH
147
267M
                                14;
148
  // Keep value, range, and count as locals.  The compiler produces better
149
  // results with the locals than using r directly.
150
281M
  BD_VALUE value = r->value;
151
281M
  unsigned int range = r->range;
152
281M
  int count = r->count;
153
154
281M
  if (counts) {
155
196M
    coef_counts = counts->coef[tx_size][type][ref];
156
196M
    eob_branch_count = counts->eob_branch[tx_size][type][ref];
157
196M
  }
158
159
309M
  while (c < max_eob) {
160
308M
    int val = -1;
161
308M
    band = *band_translate++;
162
308M
    prob = coef_probs[band][ctx];
163
308M
    if (counts) ++eob_branch_count[band][ctx];
164
308M
    if (!read_bool(r, prob[EOB_CONTEXT_NODE], &value, &count, &range)) {
165
280M
      INCREMENT_COUNT(EOB_MODEL_TOKEN);
166
280M
      break;
167
280M
    }
168
169
70.6M
    while (!read_bool(r, prob[ZERO_CONTEXT_NODE], &value, &count, &range)) {
170
42.9M
      INCREMENT_COUNT(ZERO_TOKEN);
171
42.9M
      dqv = dq[1];
172
42.9M
      token_cache[scan[c]] = 0;
173
42.9M
      ++c;
174
42.9M
      if (c >= max_eob) {
175
73.6k
        r->value = value;
176
73.6k
        r->range = range;
177
73.6k
        r->count = count;
178
73.6k
        return c;  // zero tokens at the end (no eob token)
179
73.6k
      }
180
42.8M
      ctx = get_coef_context(nb, token_cache, c);
181
42.8M
      band = *band_translate++;
182
42.8M
      prob = coef_probs[band][ctx];
183
42.8M
    }
184
185
27.7M
    if (read_bool(r, prob[ONE_CONTEXT_NODE], &value, &count, &range)) {
186
9.91M
      const vpx_prob *p = vp9_pareto8_full[prob[PIVOT_NODE] - 1];
187
9.91M
      INCREMENT_COUNT(TWO_TOKEN);
188
9.91M
      if (read_bool(r, p[0], &value, &count, &range)) {
189
2.40M
        if (read_bool(r, p[3], &value, &count, &range)) {
190
888k
          token_cache[scan[c]] = 5;
191
888k
          if (read_bool(r, p[5], &value, &count, &range)) {
192
294k
            if (read_bool(r, p[7], &value, &count, &range)) {
193
179k
              val = CAT6_MIN_VAL +
194
179k
                    read_coeff(r, cat6_prob, cat6_bits, &value, &count, &range);
195
179k
            } else {
196
115k
              val = CAT5_MIN_VAL +
197
115k
                    read_coeff(r, vp9_cat5_prob, 5, &value, &count, &range);
198
115k
            }
199
593k
          } else if (read_bool(r, p[6], &value, &count, &range)) {
200
208k
            val = CAT4_MIN_VAL +
201
208k
                  read_coeff(r, vp9_cat4_prob, 4, &value, &count, &range);
202
385k
          } else {
203
385k
            val = CAT3_MIN_VAL +
204
385k
                  read_coeff(r, vp9_cat3_prob, 3, &value, &count, &range);
205
385k
          }
206
1.51M
        } else {
207
1.51M
          token_cache[scan[c]] = 4;
208
1.51M
          if (read_bool(r, p[4], &value, &count, &range)) {
209
636k
            val = CAT2_MIN_VAL +
210
636k
                  read_coeff(r, vp9_cat2_prob, 2, &value, &count, &range);
211
875k
          } else {
212
875k
            val = CAT1_MIN_VAL +
213
875k
                  read_coeff(r, vp9_cat1_prob, 1, &value, &count, &range);
214
875k
          }
215
1.51M
        }
216
2.40M
#if CONFIG_VP9_HIGHBITDEPTH
217
        // val may use 18-bits
218
2.40M
        v = (int)(((int64_t)val * dqv) >> dq_shift);
219
#else
220
        v = (val * dqv) >> dq_shift;
221
#endif
222
7.51M
      } else {
223
7.51M
        if (read_bool(r, p[1], &value, &count, &range)) {
224
2.74M
          token_cache[scan[c]] = 3;
225
2.74M
          v = ((3 + read_bool(r, p[2], &value, &count, &range)) * dqv) >>
226
2.74M
              dq_shift;
227
4.77M
        } else {
228
4.77M
          token_cache[scan[c]] = 2;
229
4.77M
          v = (2 * dqv) >> dq_shift;
230
4.77M
        }
231
7.51M
      }
232
17.7M
    } else {
233
17.7M
      INCREMENT_COUNT(ONE_TOKEN);
234
17.7M
      token_cache[scan[c]] = 1;
235
17.7M
      v = dqv >> dq_shift;
236
17.7M
    }
237
#if CONFIG_COEFFICIENT_RANGE_CHECKING
238
#if CONFIG_VP9_HIGHBITDEPTH
239
    dqcoeff[scan[c]] = highbd_check_range(
240
        read_bool(r, 128, &value, &count, &range) ? -v : v, xd->bd);
241
#else
242
    dqcoeff[scan[c]] =
243
        check_range(read_bool(r, 128, &value, &count, &range) ? -v : v);
244
#endif  // CONFIG_VP9_HIGHBITDEPTH
245
#else
246
27.7M
    if (read_bool(r, 128, &value, &count, &range)) {
247
14.9M
      dqcoeff[scan[c]] = (tran_low_t)-v;
248
14.9M
    } else {
249
12.7M
      dqcoeff[scan[c]] = (tran_low_t)v;
250
12.7M
    }
251
27.7M
#endif  // CONFIG_COEFFICIENT_RANGE_CHECKING
252
27.7M
    ++c;
253
27.7M
    ctx = get_coef_context(nb, token_cache, c);
254
27.7M
    dqv = dq[1];
255
27.7M
  }
256
257
281M
  r->value = value;
258
281M
  r->range = range;
259
281M
  r->count = count;
260
281M
  return c;
261
281M
}
262
263
static void get_ctx_shift(MACROBLOCKD *xd, int *ctx_shift_a, int *ctx_shift_l,
264
18.7M
                          int x, int y, unsigned int tx_size_in_blocks) {
265
18.7M
  if (xd->max_blocks_wide) {
266
424k
    if (tx_size_in_blocks + x > xd->max_blocks_wide)
267
246k
      *ctx_shift_a = (tx_size_in_blocks - (xd->max_blocks_wide - x)) * 8;
268
424k
  }
269
18.7M
  if (xd->max_blocks_high) {
270
1.08M
    if (tx_size_in_blocks + y > xd->max_blocks_high)
271
751k
      *ctx_shift_l = (tx_size_in_blocks - (xd->max_blocks_high - y)) * 8;
272
1.08M
  }
273
18.7M
}
274
275
int vp9_decode_block_tokens(TileWorkerData *twd, int plane, const ScanOrder *sc,
276
281M
                            int x, int y, TX_SIZE tx_size, int seg_id) {
277
281M
  vpx_reader *r = &twd->bit_reader;
278
281M
  MACROBLOCKD *xd = &twd->xd;
279
281M
  struct macroblockd_plane *const pd = &xd->plane[plane];
280
281M
  const int16_t *const dequant = pd->seg_dequant[seg_id];
281
281M
  int eob;
282
281M
  ENTROPY_CONTEXT *a = pd->above_context + x;
283
281M
  ENTROPY_CONTEXT *l = pd->left_context + y;
284
281M
  int ctx;
285
281M
  int ctx_shift_a = 0;
286
281M
  int ctx_shift_l = 0;
287
288
281M
  switch (tx_size) {
289
262M
    case TX_4X4:
290
262M
      ctx = a[0] != 0;
291
262M
      ctx += l[0] != 0;
292
262M
      eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
293
262M
                         dequant, ctx, sc->scan, sc->neighbors, r);
294
262M
      a[0] = l[0] = (eob > 0);
295
262M
      break;
296
11.2M
    case TX_8X8:
297
11.2M
      get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_8X8);
298
11.2M
      ctx = !!*(const uint16_t *)a;
299
11.2M
      ctx += !!*(const uint16_t *)l;
300
11.2M
      eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
301
11.2M
                         dequant, ctx, sc->scan, sc->neighbors, r);
302
11.2M
      *(uint16_t *)a = ((eob > 0) * 0x0101) >> ctx_shift_a;
303
11.2M
      *(uint16_t *)l = ((eob > 0) * 0x0101) >> ctx_shift_l;
304
11.2M
      break;
305
3.28M
    case TX_16X16:
306
3.28M
      get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_16X16);
307
3.28M
      ctx = !!*(const uint32_t *)a;
308
3.28M
      ctx += !!*(const uint32_t *)l;
309
3.28M
      eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
310
3.28M
                         dequant, ctx, sc->scan, sc->neighbors, r);
311
3.28M
      *(uint32_t *)a = ((eob > 0) * 0x01010101) >> ctx_shift_a;
312
3.28M
      *(uint32_t *)l = ((eob > 0) * 0x01010101) >> ctx_shift_l;
313
3.28M
      break;
314
4.20M
    case TX_32X32:
315
4.20M
      get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_32X32);
316
      // NOTE: casting to uint64_t here is safe because the default memory
317
      // alignment is at least 8 bytes and the TX_32X32 is aligned on 8 byte
318
      // boundaries.
319
4.20M
      ctx = !!*(const uint64_t *)a;
320
4.20M
      ctx += !!*(const uint64_t *)l;
321
4.20M
      eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
322
4.20M
                         dequant, ctx, sc->scan, sc->neighbors, r);
323
4.20M
      *(uint64_t *)a = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_a;
324
4.20M
      *(uint64_t *)l = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_l;
325
4.20M
      break;
326
0
    default:
327
0
      assert(0 && "Invalid transform size.");
328
0
      eob = 0;
329
0
      break;
330
281M
  }
331
332
281M
  return eob;
333
281M
}