Coverage Report

Created: 2026-09-13 07:02

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