Coverage Report

Created: 2025-08-28 07:12

/src/libvpx/vp8/encoder/tokenize.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 <math.h>
12
#include <stdio.h>
13
#include <string.h>
14
#include <assert.h>
15
#include "onyx_int.h"
16
#include "tokenize.h"
17
#include "vpx_mem/vpx_mem.h"
18
19
/* Global event counters used for accumulating statistics across several
20
   compressions, then generating context.c = initial stats. */
21
22
void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t);
23
void vp8_fix_contexts(MACROBLOCKD *x);
24
25
#include "dct_value_tokens.h"
26
#include "dct_value_cost.h"
27
28
const TOKENVALUE *const vp8_dct_value_tokens_ptr =
29
    dct_value_tokens + DCT_MAX_VALUE;
30
const short *const vp8_dct_value_cost_ptr = dct_value_cost + DCT_MAX_VALUE;
31
32
#if 0
33
int skip_true_count = 0;
34
int skip_false_count = 0;
35
#endif
36
37
/* function used to generate dct_value_tokens and dct_value_cost tables */
38
/*
39
static void fill_value_tokens()
40
{
41
42
    TOKENVALUE *t = dct_value_tokens + DCT_MAX_VALUE;
43
    const vp8_extra_bit_struct *e = vp8_extra_bits;
44
45
    int i = -DCT_MAX_VALUE;
46
    int sign = 1;
47
48
    do
49
    {
50
        if (!i)
51
            sign = 0;
52
53
        {
54
            const int a = sign ? -i : i;
55
            int eb = sign;
56
57
            if (a > 4)
58
            {
59
                int j = 4;
60
61
                while (++j < 11  &&  e[j].base_val <= a) {}
62
63
                t[i].Token = --j;
64
                eb |= (a - e[j].base_val) << 1;
65
            }
66
            else
67
                t[i].Token = a;
68
69
            t[i].Extra = eb;
70
        }
71
72
        // initialize the cost for extra bits for all possible coefficient
73
value.
74
        {
75
            int cost = 0;
76
            const vp8_extra_bit_struct *p = vp8_extra_bits + t[i].Token;
77
78
            if (p->base_val)
79
            {
80
                const int extra = t[i].Extra;
81
                const int Length = p->Len;
82
83
                if (Length)
84
                    cost += vp8_treed_cost(p->tree, p->prob, extra >> 1,
85
Length);
86
87
                cost += vp8_cost_bit(vp8_prob_half, extra & 1); // sign
88
                dct_value_cost[i + DCT_MAX_VALUE] = cost;
89
            }
90
91
        }
92
93
    }
94
    while (++i < DCT_MAX_VALUE);
95
96
    vp8_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE;
97
    vp8_dct_value_cost_ptr   = dct_value_cost + DCT_MAX_VALUE;
98
}
99
*/
100
101
767k
static void tokenize2nd_order_b(MACROBLOCK *x, TOKENEXTRA **tp, VP8_COMP *cpi) {
102
767k
  MACROBLOCKD *xd = &x->e_mbd;
103
767k
  int pt;              /* near block/prev token context index */
104
767k
  int c;               /* start at DC */
105
767k
  TOKENEXTRA *t = *tp; /* store tokens starting here */
106
767k
  const BLOCKD *b;
107
767k
  const short *qcoeff_ptr;
108
767k
  ENTROPY_CONTEXT *a;
109
767k
  ENTROPY_CONTEXT *l;
110
767k
  int band, rc, v, token;
111
767k
  int eob;
112
113
767k
  b = xd->block + 24;
114
767k
  qcoeff_ptr = b->qcoeff;
115
767k
  a = (ENTROPY_CONTEXT *)xd->above_context + 8;
116
767k
  l = (ENTROPY_CONTEXT *)xd->left_context + 8;
117
767k
  eob = xd->eobs[24];
118
767k
  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
119
120
767k
  if (!eob) {
121
    /* c = band for this case */
122
193k
    t->Token = DCT_EOB_TOKEN;
123
193k
    t->context_tree = cpi->common.fc.coef_probs[1][0][pt];
124
193k
    t->skip_eob_node = 0;
125
126
193k
    ++x->coef_counts[1][0][pt][DCT_EOB_TOKEN];
127
193k
    t++;
128
193k
    *tp = t;
129
193k
    *a = *l = 0;
130
193k
    return;
131
193k
  }
132
133
573k
  v = qcoeff_ptr[0];
134
573k
  t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
135
573k
  token = vp8_dct_value_tokens_ptr[v].Token;
136
573k
  t->Token = token;
137
138
573k
  t->context_tree = cpi->common.fc.coef_probs[1][0][pt];
139
573k
  t->skip_eob_node = 0;
140
573k
  ++x->coef_counts[1][0][pt][token];
141
573k
  pt = vp8_prev_token_class[token];
142
573k
  t++;
143
573k
  c = 1;
144
145
6.30M
  for (; c < eob; ++c) {
146
5.73M
    rc = vp8_default_zig_zag1d[c];
147
5.73M
    band = vp8_coef_bands[c];
148
5.73M
    v = qcoeff_ptr[rc];
149
150
5.73M
    t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
151
5.73M
    token = vp8_dct_value_tokens_ptr[v].Token;
152
153
5.73M
    t->Token = token;
154
5.73M
    t->context_tree = cpi->common.fc.coef_probs[1][band][pt];
155
156
5.73M
    t->skip_eob_node = ((pt == 0));
157
158
5.73M
    ++x->coef_counts[1][band][pt][token];
159
160
5.73M
    pt = vp8_prev_token_class[token];
161
5.73M
    t++;
162
5.73M
  }
163
573k
  if (c < 16) {
164
260k
    band = vp8_coef_bands[c];
165
260k
    t->Token = DCT_EOB_TOKEN;
166
260k
    t->context_tree = cpi->common.fc.coef_probs[1][band][pt];
167
168
260k
    t->skip_eob_node = 0;
169
170
260k
    ++x->coef_counts[1][band][pt][DCT_EOB_TOKEN];
171
172
260k
    t++;
173
260k
  }
174
175
573k
  *tp = t;
176
573k
  *a = *l = 1;
177
573k
}
178
179
static void tokenize1st_order_b(
180
    MACROBLOCK *x, TOKENEXTRA **tp,
181
    int type, /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
182
1.91M
    VP8_COMP *cpi) {
183
1.91M
  MACROBLOCKD *xd = &x->e_mbd;
184
1.91M
  unsigned int block;
185
1.91M
  const BLOCKD *b;
186
1.91M
  int pt; /* near block/prev token context index */
187
1.91M
  int c;
188
1.91M
  int token;
189
1.91M
  TOKENEXTRA *t = *tp; /* store tokens starting here */
190
1.91M
  const short *qcoeff_ptr;
191
1.91M
  ENTROPY_CONTEXT *a;
192
1.91M
  ENTROPY_CONTEXT *l;
193
1.91M
  int band, rc, v;
194
1.91M
  int tmp1, tmp2;
195
196
1.91M
  b = xd->block;
197
  /* Luma */
198
32.4M
  for (block = 0; block < 16; block++, b++) {
199
30.5M
    const int eob = *b->eob;
200
30.5M
    tmp1 = vp8_block2above[block];
201
30.5M
    tmp2 = vp8_block2left[block];
202
30.5M
    qcoeff_ptr = b->qcoeff;
203
30.5M
    a = (ENTROPY_CONTEXT *)xd->above_context + tmp1;
204
30.5M
    l = (ENTROPY_CONTEXT *)xd->left_context + tmp2;
205
206
30.5M
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
207
208
30.5M
    c = type ? 0 : 1;
209
210
30.5M
    if (c >= eob) {
211
      /* c = band for this case */
212
11.6M
      t->Token = DCT_EOB_TOKEN;
213
11.6M
      t->context_tree = cpi->common.fc.coef_probs[type][c][pt];
214
11.6M
      t->skip_eob_node = 0;
215
216
11.6M
      ++x->coef_counts[type][c][pt][DCT_EOB_TOKEN];
217
11.6M
      t++;
218
11.6M
      *tp = t;
219
11.6M
      *a = *l = 0;
220
11.6M
      continue;
221
11.6M
    }
222
223
18.9M
    v = qcoeff_ptr[c];
224
225
18.9M
    t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
226
18.9M
    token = vp8_dct_value_tokens_ptr[v].Token;
227
18.9M
    t->Token = token;
228
229
18.9M
    t->context_tree = cpi->common.fc.coef_probs[type][c][pt];
230
18.9M
    t->skip_eob_node = 0;
231
18.9M
    ++x->coef_counts[type][c][pt][token];
232
18.9M
    pt = vp8_prev_token_class[token];
233
18.9M
    t++;
234
18.9M
    c++;
235
236
18.9M
    assert(eob <= 16);
237
261M
    for (; c < eob; ++c) {
238
242M
      rc = vp8_default_zig_zag1d[c];
239
242M
      band = vp8_coef_bands[c];
240
242M
      v = qcoeff_ptr[rc];
241
242
242M
      t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
243
242M
      token = vp8_dct_value_tokens_ptr[v].Token;
244
245
242M
      t->Token = token;
246
242M
      t->context_tree = cpi->common.fc.coef_probs[type][band][pt];
247
248
242M
      t->skip_eob_node = (pt == 0);
249
242M
      ++x->coef_counts[type][band][pt][token];
250
251
242M
      pt = vp8_prev_token_class[token];
252
242M
      t++;
253
242M
    }
254
18.9M
    if (c < 16) {
255
5.64M
      band = vp8_coef_bands[c];
256
5.64M
      t->Token = DCT_EOB_TOKEN;
257
5.64M
      t->context_tree = cpi->common.fc.coef_probs[type][band][pt];
258
259
5.64M
      t->skip_eob_node = 0;
260
5.64M
      ++x->coef_counts[type][band][pt][DCT_EOB_TOKEN];
261
262
5.64M
      t++;
263
5.64M
    }
264
18.9M
    *tp = t;
265
18.9M
    *a = *l = 1;
266
18.9M
  }
267
268
  /* Chroma */
269
17.1M
  for (block = 16; block < 24; block++, b++) {
270
15.2M
    const int eob = *b->eob;
271
15.2M
    tmp1 = vp8_block2above[block];
272
15.2M
    tmp2 = vp8_block2left[block];
273
15.2M
    qcoeff_ptr = b->qcoeff;
274
15.2M
    a = (ENTROPY_CONTEXT *)xd->above_context + tmp1;
275
15.2M
    l = (ENTROPY_CONTEXT *)xd->left_context + tmp2;
276
277
15.2M
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
278
279
15.2M
    if (!eob) {
280
      /* c = band for this case */
281
4.32M
      t->Token = DCT_EOB_TOKEN;
282
4.32M
      t->context_tree = cpi->common.fc.coef_probs[2][0][pt];
283
4.32M
      t->skip_eob_node = 0;
284
285
4.32M
      ++x->coef_counts[2][0][pt][DCT_EOB_TOKEN];
286
4.32M
      t++;
287
4.32M
      *tp = t;
288
4.32M
      *a = *l = 0;
289
4.32M
      continue;
290
4.32M
    }
291
292
10.9M
    v = qcoeff_ptr[0];
293
294
10.9M
    t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
295
10.9M
    token = vp8_dct_value_tokens_ptr[v].Token;
296
10.9M
    t->Token = token;
297
298
10.9M
    t->context_tree = cpi->common.fc.coef_probs[2][0][pt];
299
10.9M
    t->skip_eob_node = 0;
300
10.9M
    ++x->coef_counts[2][0][pt][token];
301
10.9M
    pt = vp8_prev_token_class[token];
302
10.9M
    t++;
303
10.9M
    c = 1;
304
305
10.9M
    assert(eob <= 16);
306
135M
    for (; c < eob; ++c) {
307
124M
      rc = vp8_default_zig_zag1d[c];
308
124M
      band = vp8_coef_bands[c];
309
124M
      v = qcoeff_ptr[rc];
310
311
124M
      t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
312
124M
      token = vp8_dct_value_tokens_ptr[v].Token;
313
314
124M
      t->Token = token;
315
124M
      t->context_tree = cpi->common.fc.coef_probs[2][band][pt];
316
317
124M
      t->skip_eob_node = (pt == 0);
318
319
124M
      ++x->coef_counts[2][band][pt][token];
320
321
124M
      pt = vp8_prev_token_class[token];
322
124M
      t++;
323
124M
    }
324
10.9M
    if (c < 16) {
325
4.67M
      band = vp8_coef_bands[c];
326
4.67M
      t->Token = DCT_EOB_TOKEN;
327
4.67M
      t->context_tree = cpi->common.fc.coef_probs[2][band][pt];
328
329
4.67M
      t->skip_eob_node = 0;
330
331
4.67M
      ++x->coef_counts[2][band][pt][DCT_EOB_TOKEN];
332
333
4.67M
      t++;
334
4.67M
    }
335
10.9M
    *tp = t;
336
10.9M
    *a = *l = 1;
337
10.9M
  }
338
1.91M
}
339
340
2.97M
static int mb_is_skippable(MACROBLOCKD *x, int has_y2_block) {
341
2.97M
  int skip = 1;
342
2.97M
  int i = 0;
343
344
2.97M
  if (has_y2_block) {
345
30.9M
    for (i = 0; i < 16; ++i) skip &= (x->eobs[i] < 2);
346
1.82M
  }
347
348
47.0M
  for (; i < 24 + has_y2_block; ++i) skip &= (!x->eobs[i]);
349
350
2.97M
  return skip;
351
2.97M
}
352
353
2.97M
void vp8_tokenize_mb(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t) {
354
2.97M
  MACROBLOCKD *xd = &x->e_mbd;
355
2.97M
  int plane_type;
356
2.97M
  int has_y2_block;
357
358
2.97M
  has_y2_block = (xd->mode_info_context->mbmi.mode != B_PRED &&
359
2.97M
                  xd->mode_info_context->mbmi.mode != SPLITMV);
360
361
2.97M
  xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable(xd, has_y2_block);
362
2.97M
  if (xd->mode_info_context->mbmi.mb_skip_coeff) {
363
1.06M
    if (!cpi->common.mb_no_coeff_skip) {
364
0
      vp8_stuff_mb(cpi, x, t);
365
1.06M
    } else {
366
1.06M
      vp8_fix_contexts(xd);
367
1.06M
      x->skip_true_count++;
368
1.06M
    }
369
370
1.06M
    return;
371
1.06M
  }
372
373
1.91M
  plane_type = 3;
374
1.91M
  if (has_y2_block) {
375
767k
    tokenize2nd_order_b(x, t, cpi);
376
767k
    plane_type = 0;
377
767k
  }
378
379
1.91M
  tokenize1st_order_b(x, t, plane_type, cpi);
380
1.91M
}
381
382
static void stuff2nd_order_b(TOKENEXTRA **tp, ENTROPY_CONTEXT *a,
383
0
                             ENTROPY_CONTEXT *l, VP8_COMP *cpi, MACROBLOCK *x) {
384
0
  int pt;              /* near block/prev token context index */
385
0
  TOKENEXTRA *t = *tp; /* store tokens starting here */
386
0
  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
387
388
0
  t->Token = DCT_EOB_TOKEN;
389
0
  t->context_tree = cpi->common.fc.coef_probs[1][0][pt];
390
0
  t->skip_eob_node = 0;
391
0
  ++x->coef_counts[1][0][pt][DCT_EOB_TOKEN];
392
0
  ++t;
393
394
0
  *tp = t;
395
0
  pt = 0;
396
0
  *a = *l = pt;
397
0
}
398
399
static void stuff1st_order_b(TOKENEXTRA **tp, ENTROPY_CONTEXT *a,
400
                             ENTROPY_CONTEXT *l, int type, VP8_COMP *cpi,
401
0
                             MACROBLOCK *x) {
402
0
  int pt; /* near block/prev token context index */
403
0
  int band;
404
0
  TOKENEXTRA *t = *tp; /* store tokens starting here */
405
0
  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
406
0
  band = type ? 0 : 1;
407
0
  t->Token = DCT_EOB_TOKEN;
408
0
  t->context_tree = cpi->common.fc.coef_probs[type][band][pt];
409
0
  t->skip_eob_node = 0;
410
0
  ++x->coef_counts[type][band][pt][DCT_EOB_TOKEN];
411
0
  ++t;
412
0
  *tp = t;
413
0
  pt = 0; /* 0 <-> all coeff data is zero */
414
0
  *a = *l = pt;
415
0
}
416
417
static void stuff1st_order_buv(TOKENEXTRA **tp, ENTROPY_CONTEXT *a,
418
                               ENTROPY_CONTEXT *l, VP8_COMP *cpi,
419
0
                               MACROBLOCK *x) {
420
0
  int pt;              /* near block/prev token context index */
421
0
  TOKENEXTRA *t = *tp; /* store tokens starting here */
422
0
  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
423
424
0
  t->Token = DCT_EOB_TOKEN;
425
0
  t->context_tree = cpi->common.fc.coef_probs[2][0][pt];
426
0
  t->skip_eob_node = 0;
427
0
  ++x->coef_counts[2][0][pt][DCT_EOB_TOKEN];
428
0
  ++t;
429
0
  *tp = t;
430
0
  pt = 0; /* 0 <-> all coeff data is zero */
431
0
  *a = *l = pt;
432
0
}
433
434
0
void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t) {
435
0
  MACROBLOCKD *xd = &x->e_mbd;
436
0
  ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
437
0
  ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
438
0
  int plane_type;
439
0
  int b;
440
0
  plane_type = 3;
441
0
  if ((xd->mode_info_context->mbmi.mode != B_PRED &&
442
0
       xd->mode_info_context->mbmi.mode != SPLITMV)) {
443
0
    stuff2nd_order_b(t, A + vp8_block2above[24], L + vp8_block2left[24], cpi,
444
0
                     x);
445
0
    plane_type = 0;
446
0
  }
447
448
0
  for (b = 0; b < 16; ++b) {
449
0
    stuff1st_order_b(t, A + vp8_block2above[b], L + vp8_block2left[b],
450
0
                     plane_type, cpi, x);
451
0
  }
452
453
0
  for (b = 16; b < 24; ++b) {
454
0
    stuff1st_order_buv(t, A + vp8_block2above[b], L + vp8_block2left[b], cpi,
455
0
                       x);
456
0
  }
457
0
}
458
1.06M
void vp8_fix_contexts(MACROBLOCKD *x) {
459
  /* Clear entropy contexts for Y2 blocks */
460
1.06M
  if (x->mode_info_context->mbmi.mode != B_PRED &&
461
1.06M
      x->mode_info_context->mbmi.mode != SPLITMV) {
462
1.05M
    memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
463
1.05M
    memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
464
1.05M
  } else {
465
10.2k
    memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) - 1);
466
10.2k
    memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) - 1);
467
10.2k
  }
468
1.06M
}