Coverage Report

Created: 2026-02-14 07:08

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
402k
#define MIN_PAGE_SIZE 8192  // minimum number of token per page
35
608M
#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
312k
#define TOKEN_DATA(p) ((const token_t*)&(p)[1])
46
47
//------------------------------------------------------------------------------
48
49
310k
void VP8TBufferInit(VP8TBuffer* const b, int page_size) {
50
310k
  b->tokens = NULL;
51
310k
  b->pages = NULL;
52
310k
  b->last_page = &b->pages;
53
310k
  b->left = 0;
54
310k
  b->page_size = (page_size < MIN_PAGE_SIZE) ? MIN_PAGE_SIZE : page_size;
55
310k
  b->error = 0;
56
310k
}
57
58
218k
void VP8TBufferClear(VP8TBuffer* const b) {
59
218k
  if (b != NULL) {
60
218k
    VP8Tokens* p = b->pages;
61
427k
    while (p != NULL) {
62
209k
      VP8Tokens* const next = p->next;
63
209k
      WebPSafeFree(p);
64
209k
      p = next;
65
209k
    }
66
218k
    VP8TBufferInit(b, b->page_size);
67
218k
  }
68
218k
}
69
70
260k
static int TBufferNewPage(VP8TBuffer* const b) {
71
260k
  VP8Tokens* page = NULL;
72
260k
  if (!b->error) {
73
260k
    const size_t size = sizeof(*page) + b->page_size * sizeof(token_t);
74
260k
    page = (VP8Tokens*)WebPSafeMalloc(1ULL, size);
75
260k
  }
76
260k
  if (page == NULL) {
77
0
    b->error = 1;
78
0
    return 0;
79
0
  }
80
260k
  page->next = NULL;
81
82
260k
  *b->last_page = page;
83
260k
  b->last_page = &page->next;
84
260k
  b->left = b->page_size;
85
260k
  b->tokens = (token_t*)TOKEN_DATA(page);
86
260k
  return 1;
87
260k
}
88
89
//------------------------------------------------------------------------------
90
91
#define TOKEN_ID(t, b, ctx) \
92
266M
  (NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t))))
93
94
static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b, uint32_t bit,
95
926M
                                     uint32_t proba_idx, proba_t* const stats) {
96
926M
  assert(proba_idx < FIXED_PROBA_BIT);
97
926M
  assert(bit <= 1);
98
926M
  if (b->left > 0 || TBufferNewPage(b)) {
99
926M
    const int slot = --b->left;
100
926M
    b->tokens[slot] = (bit << 15) | proba_idx;
101
926M
  }
102
926M
  VP8RecordStats(bit, stats);
103
926M
  return bit;
104
926M
}
105
106
static WEBP_INLINE void AddConstantToken(VP8TBuffer* const b, uint32_t bit,
107
370M
                                         uint32_t proba) {
108
370M
  assert(proba < 256);
109
370M
  assert(bit <= 1);
110
370M
  if (b->left > 0 || TBufferNewPage(b)) {
111
370M
    const int slot = --b->left;
112
370M
    b->tokens[slot] = (bit << 15) | FIXED_PROBA_BIT | proba;
113
370M
  }
114
370M
}
115
116
int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res,
117
38.1M
                         VP8TBuffer* const tokens) {
118
38.1M
  const int16_t* const coeffs = res->coeffs;
119
38.1M
  const int coeff_type = res->coeff_type;
120
38.1M
  const int last = res->last;
121
38.1M
  int n = res->first;
122
38.1M
  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
38.1M
  proba_t* s = res->stats[n][ctx];
125
38.1M
  if (!AddToken(tokens, last >= 0, base_id + 0, s + 0)) {
126
15.0M
    return 0;
127
15.0M
  }
128
129
228M
  while (n < 16) {
130
228M
    const int c = coeffs[n++];
131
228M
    const int sign = c < 0;
132
228M
    const uint32_t v = sign ? -c : c;
133
228M
    if (!AddToken(tokens, v != 0, base_id + 1, s + 1)) {
134
66.6M
      base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 0);  // ctx=0
135
66.6M
      s = res->stats[VP8EncBands[n]][0];
136
66.6M
      continue;
137
66.6M
    }
138
161M
    if (!AddToken(tokens, v > 1, base_id + 2, s + 2)) {
139
52.7M
      base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 1);  // ctx=1
140
52.7M
      s = res->stats[VP8EncBands[n]][1];
141
109M
    } else {
142
109M
      if (!AddToken(tokens, v > 4, base_id + 3, s + 3)) {
143
44.0M
        if (AddToken(tokens, v != 2, base_id + 4, s + 4)) {
144
22.0M
          AddToken(tokens, v == 4, base_id + 5, s + 5);
145
22.0M
        }
146
65.0M
      } else if (!AddToken(tokens, v > 10, base_id + 6, s + 6)) {
147
26.5M
        if (!AddToken(tokens, v > 6, base_id + 7, s + 7)) {
148
12.1M
          AddConstantToken(tokens, v == 6, 159);
149
14.3M
        } else {
150
14.3M
          AddConstantToken(tokens, v >= 9, 165);
151
14.3M
          AddConstantToken(tokens, !(v & 1), 145);
152
14.3M
        }
153
38.4M
      } else {
154
38.4M
        int mask;
155
38.4M
        const uint8_t* tab;
156
38.4M
        uint32_t residue = v - 3;
157
38.4M
        if (residue < (8 << 1)) {  // VP8Cat3  (3b)
158
14.0M
          AddToken(tokens, 0, base_id + 8, s + 8);
159
14.0M
          AddToken(tokens, 0, base_id + 9, s + 9);
160
14.0M
          residue -= (8 << 0);
161
14.0M
          mask = 1 << 2;
162
14.0M
          tab = VP8Cat3;
163
24.3M
        } else if (residue < (8 << 2)) {  // VP8Cat4  (4b)
164
12.9M
          AddToken(tokens, 0, base_id + 8, s + 8);
165
12.9M
          AddToken(tokens, 1, base_id + 9, s + 9);
166
12.9M
          residue -= (8 << 1);
167
12.9M
          mask = 1 << 3;
168
12.9M
          tab = VP8Cat4;
169
12.9M
        } else if (residue < (8 << 3)) {  // VP8Cat5  (5b)
170
8.66M
          AddToken(tokens, 1, base_id + 8, s + 8);
171
8.66M
          AddToken(tokens, 0, base_id + 10, s + 9);
172
8.66M
          residue -= (8 << 2);
173
8.66M
          mask = 1 << 4;
174
8.66M
          tab = VP8Cat5;
175
8.66M
        } else {  // VP8Cat6 (11b)
176
2.76M
          AddToken(tokens, 1, base_id + 8, s + 8);
177
2.76M
          AddToken(tokens, 1, base_id + 10, s + 9);
178
2.76M
          residue -= (8 << 3);
179
2.76M
          mask = 1 << 10;
180
2.76M
          tab = VP8Cat6;
181
2.76M
        }
182
206M
        while (mask) {
183
167M
          AddConstantToken(tokens, !!(residue & mask), *tab++);
184
167M
          mask >>= 1;
185
167M
        }
186
38.4M
      }
187
109M
      base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 2);  // ctx=2
188
109M
      s = res->stats[VP8EncBands[n]][2];
189
109M
    }
190
161M
    AddConstantToken(tokens, sign, 128);
191
161M
    if (n == 16 || !AddToken(tokens, n <= last, base_id + 0, s + 0)) {
192
23.0M
      return 1;  // EOB
193
23.0M
    }
194
161M
  }
195
0
  return 1;
196
23.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
27.6k
                  const uint8_t* const probas, int final_pass) {
205
27.6k
  const VP8Tokens* p = b->pages;
206
27.6k
  assert(!b->error);
207
79.0k
  while (p != NULL) {
208
51.3k
    const VP8Tokens* const next = p->next;
209
51.3k
    const int N = (next == NULL) ? b->left : 0;
210
51.3k
    int n = b->page_size;
211
51.3k
    const token_t* const tokens = TOKEN_DATA(p);
212
238M
    while (n-- > N) {
213
238M
      const token_t token = tokens[n];
214
238M
      const int bit = (token >> 15) & 1;
215
238M
      if (token & FIXED_PROBA_BIT) {
216
65.1M
        VP8PutBit(bw, bit, token & 0xffu);  // constant proba
217
172M
      } else {
218
172M
        VP8PutBit(bw, bit, probas[token & 0x3fffu]);
219
172M
      }
220
238M
    }
221
51.3k
    if (final_pass) WebPSafeFree((void*)p);
222
51.3k
    p = next;
223
51.3k
  }
224
27.6k
  if (final_pass) b->pages = NULL;
225
27.6k
  return 1;
226
27.6k
}
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