Coverage Report

Created: 2025-11-29 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/decoder/vp9_detokenize.c
Line
Count
Source
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
313M
#define EOB_CONTEXT_NODE 0
24
235M
#define ZERO_CONTEXT_NODE 1
25
97.3M
#define ONE_CONTEXT_NODE 2
26
27
#define INCREMENT_COUNT(token)                   \
28
451M
  do {                                           \
29
451M
    if (counts) ++coef_counts[band][ctx][token]; \
30
451M
  } while (0)
31
32
static INLINE int read_bool(vpx_reader *r, int prob, BD_VALUE *value,
33
867M
                            int *count, unsigned int *range) {
34
867M
  const unsigned int split = (*range * prob + (256 - prob)) >> CHAR_BIT;
35
867M
  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
867M
  if (*count < 0) {
52
9.34M
    r->value = *value;
53
9.34M
    r->count = *count;
54
9.34M
    vpx_reader_fill(r);
55
9.34M
    *value = r->value;
56
9.34M
    *count = r->count;
57
9.34M
  }
58
59
867M
  if (*value >= bigsplit) {
60
316M
    *range = *range - split;
61
316M
    *value = *value - bigsplit;
62
316M
    {
63
316M
      const int shift = vpx_norm[*range];
64
316M
      *range <<= shift;
65
316M
      *value <<= shift;
66
316M
      *count -= shift;
67
316M
    }
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
316M
    return 1;
83
316M
  }
84
550M
  *range = split;
85
550M
  {
86
550M
    const int shift = vpx_norm[*range];
87
550M
    *range <<= shift;
88
550M
    *value <<= shift;
89
550M
    *count -= shift;
90
550M
  }
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
550M
  return 0;
105
867M
}
106
107
static INLINE int read_coeff(vpx_reader *r, const vpx_prob *probs, int n,
108
8.86M
                             BD_VALUE *value, int *count, unsigned int *range) {
109
8.86M
  int i, val = 0;
110
47.5M
  for (i = 0; i < n; ++i)
111
38.6M
    val = (val << 1) | read_bool(r, probs[i], value, count, range);
112
8.86M
  return val;
113
8.86M
}
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
218M
                        vpx_reader *r) {
119
218M
  FRAME_COUNTS *counts = xd->counts;
120
218M
  const int max_eob = 16 << (tx_size << 1);
121
218M
  const FRAME_CONTEXT *const fc = xd->fc;
122
218M
  const int ref = is_inter_block(xd->mi[0]);
123
218M
  int band, c = 0;
124
218M
  const vpx_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
125
218M
      fc->coef_probs[tx_size][type][ref];
126
218M
  const vpx_prob *prob;
127
218M
  unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
128
218M
  unsigned int(*eob_branch_count)[COEFF_CONTEXTS];
129
218M
  uint8_t token_cache[32 * 32];
130
218M
  const uint8_t *band_translate = get_band_translate(tx_size);
131
218M
  const int dq_shift = (tx_size == TX_32X32);
132
218M
  int v;
133
218M
  int16_t dqv = dq[0];
134
218M
  const uint8_t *const cat6_prob =
135
218M
#if CONFIG_VP9_HIGHBITDEPTH
136
218M
      (xd->bd == VPX_BITS_12)   ? vp9_cat6_prob_high12
137
218M
      : (xd->bd == VPX_BITS_10) ? vp9_cat6_prob_high12 + 2
138
197M
                                :
139
197M
#endif  // CONFIG_VP9_HIGHBITDEPTH
140
197M
                                vp9_cat6_prob;
141
218M
  const int cat6_bits =
142
218M
#if CONFIG_VP9_HIGHBITDEPTH
143
218M
      (xd->bd == VPX_BITS_12)   ? 18
144
218M
      : (xd->bd == VPX_BITS_10) ? 16
145
197M
                                :
146
197M
#endif  // CONFIG_VP9_HIGHBITDEPTH
147
197M
                                14;
148
  // Keep value, range, and count as locals.  The compiler produces better
149
  // results with the locals than using r directly.
150
218M
  BD_VALUE value = r->value;
151
218M
  unsigned int range = r->range;
152
218M
  int count = r->count;
153
154
218M
  if (counts) {
155
141M
    coef_counts = counts->coef[tx_size][type][ref];
156
141M
    eob_branch_count = counts->eob_branch[tx_size][type][ref];
157
141M
  }
158
159
316M
  while (c < max_eob) {
160
313M
    int val = -1;
161
313M
    band = *band_translate++;
162
313M
    prob = coef_probs[band][ctx];
163
313M
    if (counts) ++eob_branch_count[band][ctx];
164
313M
    if (!read_bool(r, prob[EOB_CONTEXT_NODE], &value, &count, &range)) {
165
215M
      INCREMENT_COUNT(EOB_MODEL_TOKEN);
166
215M
      break;
167
215M
    }
168
169
235M
    while (!read_bool(r, prob[ZERO_CONTEXT_NODE], &value, &count, &range)) {
170
137M
      INCREMENT_COUNT(ZERO_TOKEN);
171
137M
      dqv = dq[1];
172
137M
      token_cache[scan[c]] = 0;
173
137M
      ++c;
174
137M
      if (c >= max_eob) {
175
355k
        r->value = value;
176
355k
        r->range = range;
177
355k
        r->count = count;
178
355k
        return c;  // zero tokens at the end (no eob token)
179
355k
      }
180
137M
      ctx = get_coef_context(nb, token_cache, c);
181
137M
      band = *band_translate++;
182
137M
      prob = coef_probs[band][ctx];
183
137M
    }
184
185
97.3M
    if (read_bool(r, prob[ONE_CONTEXT_NODE], &value, &count, &range)) {
186
33.0M
      const vpx_prob *p = vp9_pareto8_full[prob[PIVOT_NODE] - 1];
187
33.0M
      INCREMENT_COUNT(TWO_TOKEN);
188
33.0M
      if (read_bool(r, p[0], &value, &count, &range)) {
189
8.86M
        if (read_bool(r, p[3], &value, &count, &range)) {
190
3.96M
          token_cache[scan[c]] = 5;
191
3.96M
          if (read_bool(r, p[5], &value, &count, &range)) {
192
1.77M
            if (read_bool(r, p[7], &value, &count, &range)) {
193
1.23M
              val = CAT6_MIN_VAL +
194
1.23M
                    read_coeff(r, cat6_prob, cat6_bits, &value, &count, &range);
195
1.23M
            } else {
196
543k
              val = CAT5_MIN_VAL +
197
543k
                    read_coeff(r, vp9_cat5_prob, 5, &value, &count, &range);
198
543k
            }
199
2.18M
          } else if (read_bool(r, p[6], &value, &count, &range)) {
200
810k
            val = CAT4_MIN_VAL +
201
810k
                  read_coeff(r, vp9_cat4_prob, 4, &value, &count, &range);
202
1.37M
          } else {
203
1.37M
            val = CAT3_MIN_VAL +
204
1.37M
                  read_coeff(r, vp9_cat3_prob, 3, &value, &count, &range);
205
1.37M
          }
206
4.90M
        } else {
207
4.90M
          token_cache[scan[c]] = 4;
208
4.90M
          if (read_bool(r, p[4], &value, &count, &range)) {
209
2.09M
            val = CAT2_MIN_VAL +
210
2.09M
                  read_coeff(r, vp9_cat2_prob, 2, &value, &count, &range);
211
2.80M
          } else {
212
2.80M
            val = CAT1_MIN_VAL +
213
2.80M
                  read_coeff(r, vp9_cat1_prob, 1, &value, &count, &range);
214
2.80M
          }
215
4.90M
        }
216
8.86M
#if CONFIG_VP9_HIGHBITDEPTH
217
        // val may use 18-bits
218
8.86M
        v = (int)(((int64_t)val * dqv) >> dq_shift);
219
#else
220
        v = (val * dqv) >> dq_shift;
221
#endif
222
24.1M
      } else {
223
24.1M
        if (read_bool(r, p[1], &value, &count, &range)) {
224
8.73M
          token_cache[scan[c]] = 3;
225
8.73M
          v = ((3 + read_bool(r, p[2], &value, &count, &range)) * dqv) >>
226
8.73M
              dq_shift;
227
15.4M
        } else {
228
15.4M
          token_cache[scan[c]] = 2;
229
15.4M
          v = (2 * dqv) >> dq_shift;
230
15.4M
        }
231
24.1M
      }
232
64.2M
    } else {
233
64.2M
      INCREMENT_COUNT(ONE_TOKEN);
234
64.2M
      token_cache[scan[c]] = 1;
235
64.2M
      v = dqv >> dq_shift;
236
64.2M
    }
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
97.3M
    if (read_bool(r, 128, &value, &count, &range)) {
247
48.5M
      dqcoeff[scan[c]] = (tran_low_t)-v;
248
48.8M
    } else {
249
48.8M
      dqcoeff[scan[c]] = (tran_low_t)v;
250
48.8M
    }
251
97.3M
#endif  // CONFIG_COEFFICIENT_RANGE_CHECKING
252
97.3M
    ++c;
253
97.3M
    ctx = get_coef_context(nb, token_cache, c);
254
97.3M
    dqv = dq[1];
255
97.3M
  }
256
257
218M
  r->value = value;
258
218M
  r->range = range;
259
218M
  r->count = count;
260
218M
  return c;
261
218M
}
262
263
static void get_ctx_shift(MACROBLOCKD *xd, int *ctx_shift_a, int *ctx_shift_l,
264
19.3M
                          int x, int y, unsigned int tx_size_in_blocks) {
265
19.3M
  if (xd->max_blocks_wide) {
266
509k
    if (tx_size_in_blocks + x > xd->max_blocks_wide)
267
317k
      *ctx_shift_a = (tx_size_in_blocks - (xd->max_blocks_wide - x)) * 8;
268
509k
  }
269
19.3M
  if (xd->max_blocks_high) {
270
1.34M
    if (tx_size_in_blocks + y > xd->max_blocks_high)
271
853k
      *ctx_shift_l = (tx_size_in_blocks - (xd->max_blocks_high - y)) * 8;
272
1.34M
  }
273
19.3M
}
274
275
int vp9_decode_block_tokens(TileWorkerData *twd, int plane, const ScanOrder *sc,
276
218M
                            int x, int y, TX_SIZE tx_size, int seg_id) {
277
218M
  vpx_reader *r = &twd->bit_reader;
278
218M
  MACROBLOCKD *xd = &twd->xd;
279
218M
  struct macroblockd_plane *const pd = &xd->plane[plane];
280
218M
  const int16_t *const dequant = pd->seg_dequant[seg_id];
281
218M
  int eob;
282
218M
  ENTROPY_CONTEXT *a = pd->above_context + x;
283
218M
  ENTROPY_CONTEXT *l = pd->left_context + y;
284
218M
  int ctx;
285
218M
  int ctx_shift_a = 0;
286
218M
  int ctx_shift_l = 0;
287
288
218M
  switch (tx_size) {
289
199M
    case TX_4X4:
290
199M
      ctx = a[0] != 0;
291
199M
      ctx += l[0] != 0;
292
199M
      eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
293
199M
                         dequant, ctx, sc->scan, sc->neighbors, r);
294
199M
      a[0] = l[0] = (eob > 0);
295
199M
      break;
296
10.3M
    case TX_8X8:
297
10.3M
      get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_8X8);
298
10.3M
      ctx = !!*(const uint16_t *)a;
299
10.3M
      ctx += !!*(const uint16_t *)l;
300
10.3M
      eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
301
10.3M
                         dequant, ctx, sc->scan, sc->neighbors, r);
302
10.3M
      *(uint16_t *)a = ((eob > 0) * 0x0101) >> ctx_shift_a;
303
10.3M
      *(uint16_t *)l = ((eob > 0) * 0x0101) >> ctx_shift_l;
304
10.3M
      break;
305
3.93M
    case TX_16X16:
306
3.93M
      get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_16X16);
307
3.93M
      ctx = !!*(const uint32_t *)a;
308
3.93M
      ctx += !!*(const uint32_t *)l;
309
3.93M
      eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
310
3.93M
                         dequant, ctx, sc->scan, sc->neighbors, r);
311
3.93M
      *(uint32_t *)a = ((eob > 0) * 0x01010101) >> ctx_shift_a;
312
3.93M
      *(uint32_t *)l = ((eob > 0) * 0x01010101) >> ctx_shift_l;
313
3.93M
      break;
314
5.05M
    case TX_32X32:
315
5.05M
      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
5.05M
      ctx = !!*(const uint64_t *)a;
320
5.05M
      ctx += !!*(const uint64_t *)l;
321
5.05M
      eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
322
5.05M
                         dequant, ctx, sc->scan, sc->neighbors, r);
323
5.05M
      *(uint64_t *)a = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_a;
324
5.05M
      *(uint64_t *)l = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_l;
325
5.05M
      break;
326
0
    default:
327
0
      assert(0 && "Invalid transform size.");
328
0
      eob = 0;
329
0
      break;
330
218M
  }
331
332
217M
  return eob;
333
218M
}