Coverage Report

Created: 2026-08-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/utils/bit_reader_utils.h
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
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
//         Vikas Arora (vikaas.arora@gmail.com)
14
15
#ifndef WEBP_UTILS_BIT_READER_UTILS_H_
16
#define WEBP_UTILS_BIT_READER_UTILS_H_
17
18
#include <assert.h>
19
#include <stddef.h>
20
21
#ifdef _MSC_VER
22
#include <stdlib.h>  // _byteswap_ulong
23
#endif
24
#include "src/dsp/cpu.h"
25
#include "src/utils/bounds_safety.h"
26
#include "src/webp/types.h"
27
28
WEBP_ASSUME_UNSAFE_INDEXABLE_ABI
29
30
// Warning! This macro triggers quite some MACRO wizardry around func signature!
31
#if !defined(BITTRACE)
32
#define BITTRACE 0  // 0 = off, 1 = print bits, 2 = print bytes
33
#endif
34
35
#if (BITTRACE > 0)
36
struct VP8BitReader;
37
extern void BitTrace(const struct VP8BitReader* const br, const char label[]);
38
#define BT_TRACK(br) BitTrace(br, label)
39
#define VP8Get(BR, L) VP8GetValue(BR, 1, L)
40
#else
41
#define BT_TRACK(br)
42
// We'll REMOVE the 'const char label[]' from all signatures and calls (!!):
43
75.3k
#define VP8GetValue(BR, N, L) VP8GetValue(BR, N)
44
28.6k
#define VP8Get(BR, L) VP8GetValue(BR, 1, L)
45
3.15k
#define VP8GetSignedValue(BR, N, L) VP8GetSignedValue(BR, N)
46
20.0M
#define VP8GetBit(BR, P, L) VP8GetBit(BR, P)
47
0
#define VP8GetBitAlt(BR, P, L) VP8GetBitAlt(BR, P)
48
1.48M
#define VP8GetSigned(BR, V, L) VP8GetSigned(BR, V)
49
#endif
50
51
#ifdef __cplusplus
52
extern "C" {
53
#endif
54
55
// The Boolean decoder needs to maintain infinite precision on the 'value'
56
// field. However, since 'range' is only 8bit, we only need an active window of
57
// 8 bits for 'value". Left bits (MSB) gets zeroed and shifted away when
58
// 'value' falls below 128, 'range' is updated, and fresh bits read from the
59
// bitstream are brought in as LSB. To avoid reading the fresh bits one by one
60
// (slow), we cache BITS of them ahead. The total of (BITS + 8) bits must fit
61
// into a natural register (with type bit_t). To fetch BITS bits from bitstream
62
// we use a type lbit_t.
63
//
64
// BITS can be any multiple of 8 from 8 to 56 (inclusive).
65
// Pick values that fit natural register size.
66
67
#if defined(__i386__) || defined(_M_IX86)  // x86 32bit
68
#define BITS 24
69
#elif defined(__x86_64__) || defined(_M_X64)  // x86 64bit
70
800k
#define BITS 56
71
#elif defined(__arm__) || defined(_M_ARM)  // ARM
72
#define BITS 24
73
#elif WEBP_AARCH64  // ARM 64bit
74
#define BITS 56
75
#elif defined(__mips__)  // MIPS
76
#define BITS 24
77
#elif defined(__wasm__)  // WASM
78
#define BITS 56
79
#else  // reasonable default
80
#define BITS 24
81
#endif
82
83
//------------------------------------------------------------------------------
84
// Derived types and constants:
85
//   bit_t = natural register type for storing 'value' (which is BITS+8 bits)
86
//   range_t = register for 'range' (which is 8bits only)
87
88
#if (BITS > 24)
89
typedef uint64_t bit_t;
90
#else
91
typedef uint32_t bit_t;
92
#endif
93
94
typedef uint32_t range_t;
95
96
//------------------------------------------------------------------------------
97
// Bitreader
98
99
typedef struct VP8BitReader VP8BitReader;
100
struct VP8BitReader {
101
  // boolean decoder  (keep the field ordering as is!)
102
  bit_t value;    // current value
103
  range_t range;  // current range minus 1. In [127, 254] interval.
104
  int bits;       // number of valid bits left
105
  // read buffer
106
  const uint8_t* WEBP_ENDED_BY(buf_end) buf;  // next byte to be read
107
  const uint8_t* buf_end;                     // end of read buffer
108
  // max packed-read position on buffer
109
  const uint8_t* WEBP_UNSAFE_INDEXABLE buf_max;
110
  int eof;  // true if input is exhausted
111
};
112
113
// Initialize the bit reader and the boolean decoder.
114
void VP8InitBitReader(VP8BitReader* const br,
115
                      const uint8_t* const WEBP_COUNTED_BY(size) start,
116
                      size_t size);
117
// Sets the working read buffer.
118
void VP8BitReaderSetBuffer(VP8BitReader* const br,
119
                           const uint8_t* const WEBP_COUNTED_BY(size) start,
120
                           size_t size);
121
122
// Update internal pointers to displace the byte buffer by the
123
// relative offset 'offset'.
124
void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset);
125
126
// return the next value made of 'num_bits' bits
127
uint32_t VP8GetValue(VP8BitReader* const br, int num_bits, const char label[]);
128
129
// return the next value with sign-extension.
130
int32_t VP8GetSignedValue(VP8BitReader* const br, int num_bits,
131
                          const char label[]);
132
133
// bit_reader_inl.h will implement the following methods:
134
//   static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob, ...)
135
//   static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v, ...)
136
// and should be included by the .c files that actually need them.
137
// This is to avoid recompiling the whole library whenever this file is touched,
138
// and also allowing platform-specific ad-hoc hacks.
139
140
// -----------------------------------------------------------------------------
141
// Bitreader for lossless format
142
143
// maximum number of bits (inclusive) the bit-reader can handle:
144
5.08M
#define VP8L_MAX_NUM_BIT_READ 24
145
146
381M
#define VP8L_LBITS 64  // Number of bits prefetched (= bit-size of vp8l_val_t).
147
206M
#define VP8L_WBITS 32  // Minimum number of bytes ready after VP8LFillBitWindow.
148
149
typedef uint64_t vp8l_val_t;  // right now, this bit-reader can only use 64bit.
150
151
typedef struct {
152
  vp8l_val_t val;                           // pre-fetched bits
153
  const uint8_t* WEBP_COUNTED_BY(len) buf;  // input byte buffer
154
  size_t len;                               // buffer length
155
  size_t pos;                               // byte position in buf
156
  int bit_pos;  // current bit-reading position in val
157
  int eos;      // true if a bit was read past the end of buffer
158
} VP8LBitReader;
159
160
void VP8LInitBitReader(VP8LBitReader* const br,
161
                       const uint8_t* const WEBP_COUNTED_BY(length) start,
162
                       size_t length);
163
164
//  Sets a new data buffer.
165
void VP8LBitReaderSetBuffer(VP8LBitReader* const br,
166
                            const uint8_t* const WEBP_COUNTED_BY(length) buffer,
167
                            size_t length);
168
169
// Reads the specified number of bits from read buffer.
170
// Flags an error in case end_of_stream or n_bits is more than the allowed limit
171
// of VP8L_MAX_NUM_BIT_READ (inclusive).
172
// Flags 'eos' if this read attempt is going to cross the read buffer.
173
uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits);
174
175
// Return the prefetched bits, so they can be looked up.
176
214M
static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) {
177
214M
  return (uint32_t)(br->val >> (br->bit_pos & (VP8L_LBITS - 1)));
178
214M
}
Unexecuted instantiation: webp_dec.c:VP8LPrefetchBits
Unexecuted instantiation: buffer_dec.c:VP8LPrefetchBits
Unexecuted instantiation: frame_dec.c:VP8LPrefetchBits
Unexecuted instantiation: io_dec.c:VP8LPrefetchBits
Unexecuted instantiation: vp8_dec.c:VP8LPrefetchBits
vp8l_dec.c:VP8LPrefetchBits
Line
Count
Source
176
209M
static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) {
177
209M
  return (uint32_t)(br->val >> (br->bit_pos & (VP8L_LBITS - 1)));
178
209M
}
Unexecuted instantiation: dec.c:VP8LPrefetchBits
Unexecuted instantiation: lossless.c:VP8LPrefetchBits
Unexecuted instantiation: dec_sse2.c:VP8LPrefetchBits
Unexecuted instantiation: dec_sse41.c:VP8LPrefetchBits
bit_reader_utils.c:VP8LPrefetchBits
Line
Count
Source
176
5.08M
static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) {
177
5.08M
  return (uint32_t)(br->val >> (br->bit_pos & (VP8L_LBITS - 1)));
178
5.08M
}
Unexecuted instantiation: alpha_dec.c:VP8LPrefetchBits
Unexecuted instantiation: quant_dec.c:VP8LPrefetchBits
Unexecuted instantiation: tree_dec.c:VP8LPrefetchBits
179
180
// Returns true if there was an attempt at reading bit past the end of
181
// the buffer. Doesn't set br->eos flag.
182
264M
static WEBP_INLINE int VP8LIsEndOfStream(const VP8LBitReader* const br) {
183
264M
  assert(br->pos <= br->len);
184
264M
  return br->eos || ((br->pos == br->len) && (br->bit_pos > VP8L_LBITS));
185
264M
}
Unexecuted instantiation: webp_dec.c:VP8LIsEndOfStream
Unexecuted instantiation: buffer_dec.c:VP8LIsEndOfStream
Unexecuted instantiation: frame_dec.c:VP8LIsEndOfStream
Unexecuted instantiation: io_dec.c:VP8LIsEndOfStream
Unexecuted instantiation: vp8_dec.c:VP8LIsEndOfStream
vp8l_dec.c:VP8LIsEndOfStream
Line
Count
Source
182
212M
static WEBP_INLINE int VP8LIsEndOfStream(const VP8LBitReader* const br) {
183
212M
  assert(br->pos <= br->len);
184
212M
  return br->eos || ((br->pos == br->len) && (br->bit_pos > VP8L_LBITS));
185
212M
}
Unexecuted instantiation: dec.c:VP8LIsEndOfStream
Unexecuted instantiation: lossless.c:VP8LIsEndOfStream
Unexecuted instantiation: dec_sse2.c:VP8LIsEndOfStream
Unexecuted instantiation: dec_sse41.c:VP8LIsEndOfStream
bit_reader_utils.c:VP8LIsEndOfStream
Line
Count
Source
182
51.5M
static WEBP_INLINE int VP8LIsEndOfStream(const VP8LBitReader* const br) {
183
51.5M
  assert(br->pos <= br->len);
184
51.5M
  return br->eos || ((br->pos == br->len) && (br->bit_pos > VP8L_LBITS));
185
51.5M
}
Unexecuted instantiation: alpha_dec.c:VP8LIsEndOfStream
Unexecuted instantiation: quant_dec.c:VP8LIsEndOfStream
Unexecuted instantiation: tree_dec.c:VP8LIsEndOfStream
186
187
// For jumping over a number of bits in the bit stream when accessed with
188
// VP8LPrefetchBits and VP8LFillBitWindow.
189
// This function does *not* set br->eos, since it's speed-critical.
190
// Use with extreme care!
191
209M
static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) {
192
209M
  br->bit_pos = val;
193
209M
}
Unexecuted instantiation: webp_dec.c:VP8LSetBitPos
Unexecuted instantiation: buffer_dec.c:VP8LSetBitPos
Unexecuted instantiation: frame_dec.c:VP8LSetBitPos
Unexecuted instantiation: io_dec.c:VP8LSetBitPos
Unexecuted instantiation: vp8_dec.c:VP8LSetBitPos
vp8l_dec.c:VP8LSetBitPos
Line
Count
Source
191
209M
static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) {
192
209M
  br->bit_pos = val;
193
209M
}
Unexecuted instantiation: dec.c:VP8LSetBitPos
Unexecuted instantiation: lossless.c:VP8LSetBitPos
Unexecuted instantiation: dec_sse2.c:VP8LSetBitPos
Unexecuted instantiation: dec_sse41.c:VP8LSetBitPos
Unexecuted instantiation: bit_reader_utils.c:VP8LSetBitPos
Unexecuted instantiation: alpha_dec.c:VP8LSetBitPos
Unexecuted instantiation: quant_dec.c:VP8LSetBitPos
Unexecuted instantiation: tree_dec.c:VP8LSetBitPos
194
195
// Advances the read buffer by 4 bytes to make room for reading next 32 bits.
196
// Speed critical, but infrequent part of the code can be non-inlined.
197
extern void VP8LDoFillBitWindow(VP8LBitReader* const br);
198
200M
static WEBP_INLINE void VP8LFillBitWindow(VP8LBitReader* const br) {
199
200M
  if (br->bit_pos >= VP8L_WBITS) VP8LDoFillBitWindow(br);
200
200M
}
Unexecuted instantiation: webp_dec.c:VP8LFillBitWindow
Unexecuted instantiation: buffer_dec.c:VP8LFillBitWindow
Unexecuted instantiation: frame_dec.c:VP8LFillBitWindow
Unexecuted instantiation: io_dec.c:VP8LFillBitWindow
Unexecuted instantiation: vp8_dec.c:VP8LFillBitWindow
vp8l_dec.c:VP8LFillBitWindow
Line
Count
Source
198
200M
static WEBP_INLINE void VP8LFillBitWindow(VP8LBitReader* const br) {
199
200M
  if (br->bit_pos >= VP8L_WBITS) VP8LDoFillBitWindow(br);
200
200M
}
Unexecuted instantiation: dec.c:VP8LFillBitWindow
Unexecuted instantiation: lossless.c:VP8LFillBitWindow
Unexecuted instantiation: dec_sse2.c:VP8LFillBitWindow
Unexecuted instantiation: dec_sse41.c:VP8LFillBitWindow
Unexecuted instantiation: bit_reader_utils.c:VP8LFillBitWindow
Unexecuted instantiation: alpha_dec.c:VP8LFillBitWindow
Unexecuted instantiation: quant_dec.c:VP8LFillBitWindow
Unexecuted instantiation: tree_dec.c:VP8LFillBitWindow
201
202
#ifdef __cplusplus
203
}  // extern "C"
204
#endif
205
206
#endif  // WEBP_UTILS_BIT_READER_UTILS_H_