Coverage Report

Created: 2026-10-28 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/enc/token_enc.c
Line
Count
Source
1
// Copyright 2011 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
// Paginated token buffer
11
//
12
//  A 'token' is a bit value associated with a probability, either fixed
13
// or a later-to-be-determined after statistics have been collected.
14
// For dynamic probability, we just record the slot id (idx) for the probability
15
// value in the final probability array (uint8_t* probas in VP8EmitTokens).
16
//
17
// Author: Skal (pascal.massimino@gmail.com)
18
19
#include <assert.h>
20
#include <stdlib.h>
21
#include <string.h>
22
23
#include "src/dec/common_dec.h"
24
#include "src/dsp/dsp.h"
25
#include "src/enc/cost_enc.h"
26
#include "src/enc/vp8i_enc.h"
27
#include "src/utils/bit_writer_utils.h"
28
#include "src/utils/utils.h"
29
#include "src/webp/types.h"
30
31
#if !defined(DISABLE_TOKEN_BUFFER)
32
33
// we use pages to reduce the number of memcpy()
34
25.8k
#define MIN_PAGE_SIZE 8192  // minimum number of token per page
35
1.65G
#define FIXED_PROBA_BIT (1u << 14)
36
37
typedef uint16_t token_t;  // bit #15: bit value
38
                           // bit #14: flags for constant proba or idx
39
                           // bits #0..13: slot or constant proba
40
struct VP8Tokens {
41
  VP8Tokens* next;  // pointer to next page
42
};
43
// Token data is located in memory just after the 'next' field.
44
// This macro is used to return their address and hide the trick.
45
49.4k
#define TOKEN_DATA(p) ((const token_t*)&(p)[1])
46
47
//------------------------------------------------------------------------------
48
49
20.4k
void VP8TBufferInit(VP8TBuffer* const b, int page_size) {
50
20.4k
  b->tokens = NULL;
51
20.4k
  b->pages = NULL;
52
20.4k
  b->last_page = &b->pages;
53
20.4k
  b->left = 0;
54
20.4k
  b->page_size = (page_size < MIN_PAGE_SIZE) ? MIN_PAGE_SIZE : page_size;
55
20.4k
  b->error = 0;
56
20.4k
}
57
58
13.6k
void VP8TBufferClear(VP8TBuffer* const b) {
59
13.6k
  if (b != NULL) {
60
13.6k
    VP8Tokens* p = b->pages;
61
13.6k
    while (p != NULL) {
62
0
      VP8Tokens* const next = p->next;
63
0
      WebPSafeFree(p);
64
0
      p = next;
65
0
    }
66
13.6k
    VP8TBufferInit(b, b->page_size);
67
13.6k
  }
68
13.6k
}
69
70
24.7k
WEBP_NODISCARD static int TBufferNewPage(VP8TBuffer* const b) {
71
24.7k
  VP8Tokens* page = NULL;
72
24.7k
  if (!b->error) {
73
24.7k
    const size_t size = sizeof(*page) + b->page_size * sizeof(token_t);
74
24.7k
    page = (VP8Tokens*)WebPSafeMalloc(1ULL, size);
75
24.7k
  }
76
24.7k
  if (page == NULL) {
77
0
    b->error = 1;
78
0
    return 0;
79
0
  }
80
24.7k
  page->next = NULL;
81
82
24.7k
  *b->last_page = page;
83
24.7k
  b->last_page = &page->next;
84
24.7k
  b->left = b->page_size;
85
24.7k
  b->tokens = (token_t*)TOKEN_DATA(page);
86
24.7k
  return 1;
87
24.7k
}
88
89
//------------------------------------------------------------------------------
90
91
#define TOKEN_ID(t, b, ctx) \
92
481M
  (NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t))))
93
94
static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b, uint32_t bit,
95
1.16G
                                     uint32_t proba_idx, proba_t* const stats) {
96
1.16G
  assert(proba_idx < FIXED_PROBA_BIT);
97
1.16G
  assert(bit <= 1);
98
1.16G
  if (b->left > 0 || TBufferNewPage(b)) {
99
1.16G
    const int slot = --b->left;
100
1.16G
    b->tokens[slot] = (bit << 15) | proba_idx;
101
1.16G
  }
102
1.16G
  VP8RecordStats(bit, stats);
103
1.16G
  return bit;
104
1.16G
}
105
106
static WEBP_INLINE void AddConstantToken(VP8TBuffer* const b, uint32_t bit,
107
247M
                                         uint32_t proba) {
108
247M
  assert(proba < 256);
109
247M
  assert(bit <= 1);
110
247M
  if (b->left > 0 || TBufferNewPage(b)) {
111
247M
    const int slot = --b->left;
112
247M
    b->tokens[slot] = (bit << 15) | FIXED_PROBA_BIT | proba;
113
247M
  }
114
247M
}
115
116
int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res,
117
140M
                         VP8TBuffer* const tokens) {
118
140M
  const int16_t* const coeffs = res->coeffs;
119
140M
  const int coeff_type = res->coeff_type;
120
140M
  const int last = res->last;
121
140M
  int n = res->first;
122
140M
  uint32_t base_id = TOKEN_ID(coeff_type, n, ctx);
123
  // should be stats[VP8EncBands[n]], but it's equivalent for n=0 or 1
124
140M
  proba_t* s = res->stats[n][ctx];
125
140M
  if (!AddToken(tokens, last >= 0, base_id + 0, s + 0)) {
126
101M
    return 0;
127
101M
  }
128
129
341M
  while (n < 16) {
130
341M
    const int c = coeffs[n++];
131
341M
    const int sign = c < 0;
132
341M
    const uint32_t v = sign ? -c : c;
133
341M
    if (!AddToken(tokens, v != 0, base_id + 1, s + 1)) {
134
128M
      base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 0);  // ctx=0
135
128M
      s = res->stats[VP8EncBands[n]][0];
136
128M
      continue;
137
128M
    }
138
213M
    if (!AddToken(tokens, v > 1, base_id + 2, s + 2)) {
139
110M
      base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 1);  // ctx=1
140
110M
      s = res->stats[VP8EncBands[n]][1];
141
110M
    } else {
142
103M
      if (!AddToken(tokens, v > 4, base_id + 3, s + 3)) {
143
81.9M
        if (AddToken(tokens, v != 2, base_id + 4, s + 4)) {
144
36.5M
          AddToken(tokens, v == 4, base_id + 5, s + 5);
145
36.5M
        }
146
81.9M
      } else if (!AddToken(tokens, v > 10, base_id + 6, s + 6)) {
147
18.5M
        if (!AddToken(tokens, v > 6, base_id + 7, s + 7)) {
148
11.7M
          AddConstantToken(tokens, v == 6, 159);
149
11.7M
        } else {
150
6.80M
          AddConstantToken(tokens, v >= 9, 165);
151
6.80M
          AddConstantToken(tokens, !(v & 1), 145);
152
6.80M
        }
153
18.5M
      } else {
154
2.67M
        int mask;
155
2.67M
        const uint8_t* tab;
156
2.67M
        uint32_t residue = v - 3;
157
2.67M
        if (residue < (8 << 1)) {  // VP8Cat3  (3b)
158
2.07M
          AddToken(tokens, 0, base_id + 8, s + 8);
159
2.07M
          AddToken(tokens, 0, base_id + 9, s + 9);
160
2.07M
          residue -= (8 << 0);
161
2.07M
          mask = 1 << 2;
162
2.07M
          tab = VP8Cat3;
163
2.07M
        } else if (residue < (8 << 2)) {  // VP8Cat4  (4b)
164
513k
          AddToken(tokens, 0, base_id + 8, s + 8);
165
513k
          AddToken(tokens, 1, base_id + 9, s + 9);
166
513k
          residue -= (8 << 1);
167
513k
          mask = 1 << 3;
168
513k
          tab = VP8Cat4;
169
513k
        } else if (residue < (8 << 3)) {  // VP8Cat5  (5b)
170
83.4k
          AddToken(tokens, 1, base_id + 8, s + 8);
171
83.4k
          AddToken(tokens, 0, base_id + 10, s + 10);
172
83.4k
          residue -= (8 << 2);
173
83.4k
          mask = 1 << 4;
174
83.4k
          tab = VP8Cat5;
175
83.4k
        } else {  // VP8Cat6 (11b)
176
5.41k
          AddToken(tokens, 1, base_id + 8, s + 8);
177
5.41k
          AddToken(tokens, 1, base_id + 10, s + 10);
178
5.41k
          residue -= (8 << 3);
179
5.41k
          mask = 1 << 10;
180
5.41k
          tab = VP8Cat6;
181
5.41k
        }
182
11.4M
        while (mask) {
183
8.74M
          AddConstantToken(tokens, !!(residue & mask), *tab++);
184
8.74M
          mask >>= 1;
185
8.74M
        }
186
2.67M
      }
187
103M
      base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 2);  // ctx=2
188
103M
      s = res->stats[VP8EncBands[n]][2];
189
103M
    }
190
213M
    AddConstantToken(tokens, sign, 128);
191
213M
    if (n == 16 || !AddToken(tokens, n <= last, base_id + 0, s + 0)) {
192
38.0M
      return 1;  // EOB
193
38.0M
    }
194
213M
  }
195
0
  return 1;
196
38.0M
}
197
198
#undef TOKEN_ID
199
200
//------------------------------------------------------------------------------
201
// Final coding pass, with known probabilities
202
203
int VP8EmitTokens(VP8TBuffer* const b, VP8BitWriter* const bw,
204
6.80k
                  const uint8_t* const probas, int final_pass) {
205
6.80k
  const VP8Tokens* p = b->pages;
206
6.80k
  assert(!b->error);
207
31.5k
  while (p != NULL) {
208
24.7k
    const VP8Tokens* const next = p->next;
209
24.7k
    const int N = (next == NULL) ? b->left : 0;
210
24.7k
    int n = b->page_size;
211
24.7k
    const token_t* const tokens = TOKEN_DATA(p);
212
1.41G
    while (n-- > N) {
213
1.41G
      const token_t token = tokens[n];
214
1.41G
      const int bit = (token >> 15) & 1;
215
1.41G
      if (token & FIXED_PROBA_BIT) {
216
247M
        VP8PutBit(bw, bit, token & 0xffu);  // constant proba
217
1.16G
      } else {
218
1.16G
        VP8PutBit(bw, bit, probas[token & 0x3fffu]);
219
1.16G
      }
220
1.41G
    }
221
24.7k
    if (final_pass) WebPSafeFree((void*)p);
222
24.7k
    p = next;
223
24.7k
  }
224
6.80k
  if (final_pass) b->pages = NULL;
225
6.80k
  return 1;
226
6.80k
}
227
228
// Size estimation
229
0
size_t VP8EstimateTokenSize(VP8TBuffer* const b, const uint8_t* const probas) {
230
0
  size_t size = 0;
231
0
  const VP8Tokens* p = b->pages;
232
0
  assert(!b->error);
233
0
  while (p != NULL) {
234
0
    const VP8Tokens* const next = p->next;
235
0
    const int N = (next == NULL) ? b->left : 0;
236
0
    int n = b->page_size;
237
0
    const token_t* const tokens = TOKEN_DATA(p);
238
0
    while (n-- > N) {
239
0
      const token_t token = tokens[n];
240
0
      const int bit = token & (1 << 15);
241
0
      if (token & FIXED_PROBA_BIT) {
242
0
        size += VP8BitCost(bit, token & 0xffu);
243
0
      } else {
244
0
        size += VP8BitCost(bit, probas[token & 0x3fffu]);
245
0
      }
246
0
    }
247
0
    p = next;
248
0
  }
249
0
  return size;
250
0
}
251
252
//------------------------------------------------------------------------------
253
254
#else  // DISABLE_TOKEN_BUFFER
255
256
void VP8TBufferInit(VP8TBuffer* const b, int page_size) {
257
  (void)b;
258
  (void)page_size;
259
}
260
void VP8TBufferClear(VP8TBuffer* const b) { (void)b; }
261
262
#endif  // !DISABLE_TOKEN_BUFFER