Coverage Report

Created: 2025-11-14 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/utils/bit_reader_utils.c
Line
Count
Source
1
// Copyright 2010 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
// Boolean decoder non-inlined methods
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#ifdef HAVE_CONFIG_H
15
#include "src/webp/config.h"
16
#endif
17
18
#include <assert.h>
19
#include <stddef.h>
20
21
#include "src/dsp/cpu.h"
22
#include "src/utils/bit_reader_inl_utils.h"
23
#include "src/utils/bit_reader_utils.h"
24
#include "src/utils/bounds_safety.h"
25
#include "src/utils/endian_inl_utils.h"
26
#include "src/utils/utils.h"
27
#include "src/webp/types.h"
28
29
WEBP_ASSUME_UNSAFE_INDEXABLE_ABI
30
31
//------------------------------------------------------------------------------
32
// VP8BitReader
33
34
void VP8BitReaderSetBuffer(VP8BitReader* const br,
35
                           const uint8_t* const WEBP_COUNTED_BY(size) start,
36
4.71k
                           size_t size) {
37
4.71k
  assert(start != NULL);
38
4.71k
  br->buf = start;
39
4.71k
  br->buf_end = start + size;
40
4.71k
  br->buf_max =
41
4.71k
      (size >= sizeof(lbit_t)) ? start + size - sizeof(lbit_t) + 1 : start;
42
4.71k
}
43
44
void VP8InitBitReader(VP8BitReader* const br,
45
                      const uint8_t* const WEBP_COUNTED_BY(size) start,
46
4.71k
                      size_t size) {
47
4.71k
  assert(br != NULL);
48
4.71k
  assert(start != NULL);
49
4.71k
  assert(size < (1u << 31));  // limit ensured by format and upstream checks
50
4.71k
  br->range = 255 - 1;
51
4.71k
  br->value = 0;
52
4.71k
  br->bits = -8;  // to load the very first 8bits
53
4.71k
  br->eof = 0;
54
4.71k
  VP8BitReaderSetBuffer(br, start, size);
55
4.71k
  VP8LoadNewBytes(br);
56
4.71k
}
57
58
0
void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset) {
59
0
  if (br->buf != NULL) {
60
0
    br->buf += offset;
61
0
    br->buf_end += offset;
62
0
    br->buf_max += offset;
63
0
  }
64
0
}
65
66
const uint8_t kVP8Log2Range[128] = {
67
    7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3,
68
    3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
69
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
70
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
71
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
72
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0};
73
74
// range = ((range - 1) << kVP8Log2Range[range]) + 1
75
const uint8_t kVP8NewRange[128] = {
76
    127, 127, 191, 127, 159, 191, 223, 127, 143, 159, 175, 191, 207, 223, 239,
77
    127, 135, 143, 151, 159, 167, 175, 183, 191, 199, 207, 215, 223, 231, 239,
78
    247, 127, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167, 171, 175, 179,
79
    183, 187, 191, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239,
80
    243, 247, 251, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149,
81
    151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179,
82
    181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209,
83
    211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239,
84
    241, 243, 245, 247, 249, 251, 253, 127};
85
86
1.24M
void VP8LoadFinalBytes(VP8BitReader* const br) {
87
1.24M
  assert(br != NULL && br->buf != NULL);
88
  // Only read 8bits at a time
89
1.24M
  if (br->buf < br->buf_end) {
90
7.92k
    br->bits += 8;
91
7.92k
    br->value = (bit_t)(*br->buf++) | (br->value << 8);
92
7.92k
    WEBP_SELF_ASSIGN(br->buf_end);
93
1.23M
  } else if (!br->eof) {
94
1.73k
    br->value <<= 8;
95
1.73k
    br->bits += 8;
96
1.73k
    br->eof = 1;
97
1.23M
  } else {
98
1.23M
    br->bits = 0;  // This is to avoid undefined behaviour with shifts.
99
1.23M
  }
100
1.24M
}
101
102
//------------------------------------------------------------------------------
103
// Higher-level calls
104
105
89.6k
uint32_t VP8GetValue(VP8BitReader* const br, int bits, const char label[]) {
106
89.6k
  uint32_t v = 0;
107
485k
  while (bits-- > 0) {
108
395k
    v |= VP8GetBit(br, 0x80, label) << bits;
109
395k
  }
110
89.6k
  return v;
111
89.6k
}
112
113
int32_t VP8GetSignedValue(VP8BitReader* const br, int bits,
114
5.33k
                          const char label[]) {
115
5.33k
  const int value = VP8GetValue(br, bits, label);
116
5.33k
  return VP8Get(br, label) ? -value : value;
117
5.33k
}
118
119
//------------------------------------------------------------------------------
120
// VP8LBitReader
121
122
19.9k
#define VP8L_LOG8_WBITS 4  // Number of bytes needed to store VP8L_WBITS bits.
123
124
#if defined(__arm__) || defined(_M_ARM) || WEBP_AARCH64 ||          \
125
    defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || \
126
    defined(_M_X64) || defined(__wasm__)
127
#define VP8L_USE_FAST_LOAD
128
#endif
129
130
static const uint32_t kBitMask[VP8L_MAX_NUM_BIT_READ + 1] = {
131
    0,        0x000001, 0x000003, 0x000007, 0x00000f, 0x00001f, 0x00003f,
132
    0x00007f, 0x0000ff, 0x0001ff, 0x0003ff, 0x0007ff, 0x000fff, 0x001fff,
133
    0x003fff, 0x007fff, 0x00ffff, 0x01ffff, 0x03ffff, 0x07ffff, 0x0fffff,
134
    0x1fffff, 0x3fffff, 0x7fffff, 0xffffff};
135
136
void VP8LInitBitReader(VP8LBitReader* const br,
137
                       const uint8_t* const WEBP_COUNTED_BY(length) start,
138
24.2k
                       size_t length) {
139
24.2k
  size_t i;
140
24.2k
  vp8l_val_t value = 0;
141
24.2k
  assert(br != NULL);
142
24.2k
  assert(start != NULL);
143
24.2k
  assert(length < 0xfffffff8u);  // can't happen with a RIFF chunk.
144
145
24.2k
  br->buf = start;
146
24.2k
  br->len = length;
147
24.2k
  br->bit_pos = 0;
148
24.2k
  br->eos = 0;
149
150
24.2k
  if (length > sizeof(br->val)) {
151
21.5k
    length = sizeof(br->val);
152
21.5k
  }
153
217k
  for (i = 0; i < length; ++i) {
154
192k
    value |= (vp8l_val_t)start[i] << (8 * i);
155
192k
  }
156
24.2k
  br->val = value;
157
24.2k
  br->pos = length;
158
24.2k
}
159
160
void VP8LBitReaderSetBuffer(VP8LBitReader* const br,
161
                            const uint8_t* const WEBP_COUNTED_BY(len) buf,
162
0
                            size_t len) {
163
0
  assert(br != NULL);
164
0
  assert(buf != NULL);
165
0
  assert(len < 0xfffffff8u);  // can't happen with a RIFF chunk.
166
0
  br->buf = buf;
167
0
  br->len = len;
168
  // 'pos' > 'len' should be considered a param error.
169
0
  br->eos = (br->pos > br->len) || VP8LIsEndOfStream(br);
170
0
}
171
172
5.78k
static void VP8LSetEndOfStream(VP8LBitReader* const br) {
173
5.78k
  br->eos = 1;
174
5.78k
  br->bit_pos = 0;  // To avoid undefined behaviour with shifts.
175
5.78k
}
176
177
// If not at EOS, reload up to VP8L_LBITS byte-by-byte
178
31.0M
static void ShiftBytes(VP8LBitReader* const br) {
179
31.2M
  while (br->bit_pos >= 8 && br->pos < br->len) {
180
205k
    br->val >>= 8;
181
205k
    br->val |= ((vp8l_val_t)br->buf[br->pos]) << (VP8L_LBITS - 8);
182
205k
    ++br->pos;
183
205k
    br->bit_pos -= 8;
184
205k
  }
185
31.0M
  if (VP8LIsEndOfStream(br)) {
186
601
    VP8LSetEndOfStream(br);
187
601
  }
188
31.0M
}
189
190
30.6M
void VP8LDoFillBitWindow(VP8LBitReader* const br) {
191
30.6M
  assert(br->bit_pos >= VP8L_WBITS);
192
30.6M
#if defined(VP8L_USE_FAST_LOAD)
193
30.6M
  if (br->pos + sizeof(br->val) < br->len) {
194
19.9k
    br->val >>= VP8L_WBITS;
195
19.9k
    br->bit_pos -= VP8L_WBITS;
196
19.9k
    br->val |= (vp8l_val_t)HToLE32(WebPMemToUint32(br->buf + br->pos))
197
19.9k
               << (VP8L_LBITS - VP8L_WBITS);
198
19.9k
    br->pos += VP8L_LOG8_WBITS;
199
19.9k
    return;
200
19.9k
  }
201
30.6M
#endif
202
30.6M
  ShiftBytes(br);  // Slow path.
203
30.6M
}
204
205
464k
uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits) {
206
464k
  assert(n_bits >= 0);
207
  // Flag an error if end_of_stream or n_bits is more than allowed limit.
208
464k
  if (!br->eos && n_bits <= VP8L_MAX_NUM_BIT_READ) {
209
459k
    const uint32_t val = VP8LPrefetchBits(br) & kBitMask[n_bits];
210
459k
    const int new_bits = br->bit_pos + n_bits;
211
459k
    br->bit_pos = new_bits;
212
459k
    ShiftBytes(br);
213
459k
    return val;
214
459k
  } else {
215
5.18k
    VP8LSetEndOfStream(br);
216
5.18k
    return 0;
217
5.18k
  }
218
464k
}
219
220
//------------------------------------------------------------------------------
221
// Bit-tracing tool
222
223
#if (BITTRACE > 0)
224
225
#include <stdio.h>
226
#include <stdlib.h>  // for atexit()
227
#include <string.h>
228
229
#define MAX_NUM_LABELS 32
230
static struct {
231
  const char* label;
232
  int size;
233
  int count;
234
} kLabels[MAX_NUM_LABELS];
235
236
static int last_label = 0;
237
static int last_pos = 0;
238
static const uint8_t* buf_start = NULL;
239
static int init_done = 0;
240
241
static void PrintBitTraces(void) {
242
  int i;
243
  int scale = 1;
244
  int total = 0;
245
  const char* units = "bits";
246
#if (BITTRACE == 2)
247
  scale = 8;
248
  units = "bytes";
249
#endif
250
  for (i = 0; i < last_label; ++i) total += kLabels[i].size;
251
  if (total < 1) total = 1;  // avoid rounding errors
252
  printf("=== Bit traces ===\n");
253
  for (i = 0; i < last_label; ++i) {
254
    const int skip = 16 - (int)strlen(kLabels[i].label);
255
    const int value = (kLabels[i].size + scale - 1) / scale;
256
    assert(skip > 0);
257
    printf("%s \%*s: %6d %s   \t[%5.2f%%] [count: %7d]\n", kLabels[i].label,
258
           skip, "", value, units, 100.f * kLabels[i].size / total,
259
           kLabels[i].count);
260
  }
261
  total = (total + scale - 1) / scale;
262
  printf("Total: %d %s\n", total, units);
263
}
264
265
void BitTrace(const struct VP8BitReader* const br, const char label[]) {
266
  int i, pos;
267
  if (!init_done) {
268
    WEBP_UNSAFE_MEMSET(kLabels, 0, sizeof(kLabels));
269
    atexit(PrintBitTraces);
270
    buf_start = br->buf;
271
    init_done = 1;
272
  }
273
  pos = (int)(br->buf - buf_start) * 8 - br->bits;
274
  // if there's a too large jump, we've changed partition -> reset counter
275
  if (abs(pos - last_pos) > 32) {
276
    buf_start = br->buf;
277
    pos = 0;
278
    last_pos = 0;
279
  }
280
  if (br->range >= 0x7f) pos += kVP8Log2Range[br->range - 0x7f];
281
  for (i = 0; i < last_label; ++i) {
282
    if (!strcmp(label, kLabels[i].label)) break;
283
  }
284
  if (i == MAX_NUM_LABELS) abort();  // overflow!
285
  kLabels[i].label = label;
286
  kLabels[i].size += pos - last_pos;
287
  kLabels[i].count += 1;
288
  if (i == last_label) ++last_label;
289
  last_pos = pos;
290
}
291
292
#endif  // BITTRACE > 0
293
294
//------------------------------------------------------------------------------