Coverage Report

Created: 2026-11-12 07:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/utils/bit_reader_inl_utils.h
Line
Count
Source
1
// Copyright 2014 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
// Specific inlined methods for boolean decoder [VP8GetBit() ...]
11
// This file should be included by the .c sources that actually need to call
12
// these methods.
13
//
14
// Author: Skal (pascal.massimino@gmail.com)
15
16
#ifndef WEBP_UTILS_BIT_READER_INL_UTILS_H_
17
#define WEBP_UTILS_BIT_READER_INL_UTILS_H_
18
19
#ifdef HAVE_CONFIG_H
20
#include "src/webp/config.h"
21
#endif
22
23
#include <assert.h>
24
#include <string.h>  // for memcpy
25
26
#include "src/dsp/cpu.h"
27
#include "src/dsp/dsp.h"
28
#include "src/utils/bit_reader_utils.h"
29
#include "src/utils/bounds_safety.h"
30
#include "src/utils/endian_inl_utils.h"
31
#include "src/utils/utils.h"
32
#include "src/webp/types.h"
33
34
WEBP_ASSUME_UNSAFE_INDEXABLE_ABI
35
36
#ifdef __cplusplus
37
extern "C" {
38
#endif
39
40
//------------------------------------------------------------------------------
41
// Derived type lbit_t = natural type for memory I/O
42
43
#if (BITS > 32)
44
typedef uint64_t lbit_t;
45
#elif (BITS > 16)
46
typedef uint32_t lbit_t;
47
#elif (BITS > 8)
48
typedef uint16_t lbit_t;
49
#else
50
typedef uint8_t lbit_t;
51
#endif
52
53
extern const uint8_t kVP8Log2Range[128];
54
extern const uint8_t kVP8NewRange[128];
55
56
// special case for the tail byte-reading
57
void VP8LoadFinalBytes(VP8BitReader* const br);
58
59
//------------------------------------------------------------------------------
60
// Inlined critical functions
61
62
// makes sure br->value has at least BITS bits worth of data
63
static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE void VP8LoadNewBytes(
64
163k
    VP8BitReader* WEBP_RESTRICT const br) {
65
163k
  assert(br != NULL && br->buf != NULL);
66
  // Read 'BITS' bits at a time if possible.
67
163k
  if (br->buf < br->buf_max) {
68
    // convert memory type to register type (with some zero'ing!)
69
81.2k
    bit_t bits;
70
#if defined(WEBP_USE_MIPS32)
71
    // This is needed because of un-aligned read.
72
    lbit_t in_bits;
73
    lbit_t* p_buf = (lbit_t*)br->buf;
74
    __asm__ volatile(
75
        ".set   push                             \n\t"
76
        ".set   at                               \n\t"
77
        ".set   macro                            \n\t"
78
        "ulw    %[in_bits], 0(%[p_buf])          \n\t"
79
        ".set   pop                              \n\t"
80
        : [in_bits] "=r"(in_bits)
81
        : [p_buf] "r"(p_buf)
82
        : "memory", "at");
83
#else
84
81.2k
    lbit_t in_bits;
85
81.2k
    WEBP_UNSAFE_MEMCPY(&in_bits, br->buf, sizeof(in_bits));
86
81.2k
#endif
87
81.2k
    br->buf += BITS >> 3;
88
81.2k
    WEBP_SELF_ASSIGN(br->buf_end);
89
81.2k
#if !defined(WORDS_BIGENDIAN)
90
81.2k
#if (BITS > 32)
91
81.2k
    bits = BSwap64(in_bits);
92
81.2k
    bits >>= 64 - BITS;
93
#elif (BITS >= 24)
94
    bits = BSwap32(in_bits);
95
    bits >>= (32 - BITS);
96
#elif (BITS == 16)
97
    bits = BSwap16(in_bits);
98
#else   // BITS == 8
99
    bits = (bit_t)in_bits;
100
#endif  // BITS > 32
101
#else   // WORDS_BIGENDIAN
102
    bits = (bit_t)in_bits;
103
    if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS);
104
#endif
105
81.2k
    br->value = bits | (br->value << BITS);
106
81.2k
    br->bits += BITS;
107
82.1k
  } else {
108
82.1k
    VP8LoadFinalBytes(br);  // no need to be inlined
109
82.1k
  }
110
163k
}
vp8_dec.c:VP8LoadNewBytes
Line
Count
Source
64
59.5k
    VP8BitReader* WEBP_RESTRICT const br) {
65
59.5k
  assert(br != NULL && br->buf != NULL);
66
  // Read 'BITS' bits at a time if possible.
67
59.5k
  if (br->buf < br->buf_max) {
68
    // convert memory type to register type (with some zero'ing!)
69
45.4k
    bit_t bits;
70
#if defined(WEBP_USE_MIPS32)
71
    // This is needed because of un-aligned read.
72
    lbit_t in_bits;
73
    lbit_t* p_buf = (lbit_t*)br->buf;
74
    __asm__ volatile(
75
        ".set   push                             \n\t"
76
        ".set   at                               \n\t"
77
        ".set   macro                            \n\t"
78
        "ulw    %[in_bits], 0(%[p_buf])          \n\t"
79
        ".set   pop                              \n\t"
80
        : [in_bits] "=r"(in_bits)
81
        : [p_buf] "r"(p_buf)
82
        : "memory", "at");
83
#else
84
45.4k
    lbit_t in_bits;
85
45.4k
    WEBP_UNSAFE_MEMCPY(&in_bits, br->buf, sizeof(in_bits));
86
45.4k
#endif
87
45.4k
    br->buf += BITS >> 3;
88
45.4k
    WEBP_SELF_ASSIGN(br->buf_end);
89
45.4k
#if !defined(WORDS_BIGENDIAN)
90
45.4k
#if (BITS > 32)
91
45.4k
    bits = BSwap64(in_bits);
92
45.4k
    bits >>= 64 - BITS;
93
#elif (BITS >= 24)
94
    bits = BSwap32(in_bits);
95
    bits >>= (32 - BITS);
96
#elif (BITS == 16)
97
    bits = BSwap16(in_bits);
98
#else   // BITS == 8
99
    bits = (bit_t)in_bits;
100
#endif  // BITS > 32
101
#else   // WORDS_BIGENDIAN
102
    bits = (bit_t)in_bits;
103
    if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS);
104
#endif
105
45.4k
    br->value = bits | (br->value << BITS);
106
45.4k
    br->bits += BITS;
107
45.4k
  } else {
108
14.0k
    VP8LoadFinalBytes(br);  // no need to be inlined
109
14.0k
  }
110
59.5k
}
tree_dec.c:VP8LoadNewBytes
Line
Count
Source
64
89.8k
    VP8BitReader* WEBP_RESTRICT const br) {
65
89.8k
  assert(br != NULL && br->buf != NULL);
66
  // Read 'BITS' bits at a time if possible.
67
89.8k
  if (br->buf < br->buf_max) {
68
    // convert memory type to register type (with some zero'ing!)
69
24.3k
    bit_t bits;
70
#if defined(WEBP_USE_MIPS32)
71
    // This is needed because of un-aligned read.
72
    lbit_t in_bits;
73
    lbit_t* p_buf = (lbit_t*)br->buf;
74
    __asm__ volatile(
75
        ".set   push                             \n\t"
76
        ".set   at                               \n\t"
77
        ".set   macro                            \n\t"
78
        "ulw    %[in_bits], 0(%[p_buf])          \n\t"
79
        ".set   pop                              \n\t"
80
        : [in_bits] "=r"(in_bits)
81
        : [p_buf] "r"(p_buf)
82
        : "memory", "at");
83
#else
84
24.3k
    lbit_t in_bits;
85
24.3k
    WEBP_UNSAFE_MEMCPY(&in_bits, br->buf, sizeof(in_bits));
86
24.3k
#endif
87
24.3k
    br->buf += BITS >> 3;
88
24.3k
    WEBP_SELF_ASSIGN(br->buf_end);
89
24.3k
#if !defined(WORDS_BIGENDIAN)
90
24.3k
#if (BITS > 32)
91
24.3k
    bits = BSwap64(in_bits);
92
24.3k
    bits >>= 64 - BITS;
93
#elif (BITS >= 24)
94
    bits = BSwap32(in_bits);
95
    bits >>= (32 - BITS);
96
#elif (BITS == 16)
97
    bits = BSwap16(in_bits);
98
#else   // BITS == 8
99
    bits = (bit_t)in_bits;
100
#endif  // BITS > 32
101
#else   // WORDS_BIGENDIAN
102
    bits = (bit_t)in_bits;
103
    if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS);
104
#endif
105
24.3k
    br->value = bits | (br->value << BITS);
106
24.3k
    br->bits += BITS;
107
65.4k
  } else {
108
65.4k
    VP8LoadFinalBytes(br);  // no need to be inlined
109
65.4k
  }
110
89.8k
}
bit_reader_utils.c:VP8LoadNewBytes
Line
Count
Source
64
14.1k
    VP8BitReader* WEBP_RESTRICT const br) {
65
14.1k
  assert(br != NULL && br->buf != NULL);
66
  // Read 'BITS' bits at a time if possible.
67
14.1k
  if (br->buf < br->buf_max) {
68
    // convert memory type to register type (with some zero'ing!)
69
11.3k
    bit_t bits;
70
#if defined(WEBP_USE_MIPS32)
71
    // This is needed because of un-aligned read.
72
    lbit_t in_bits;
73
    lbit_t* p_buf = (lbit_t*)br->buf;
74
    __asm__ volatile(
75
        ".set   push                             \n\t"
76
        ".set   at                               \n\t"
77
        ".set   macro                            \n\t"
78
        "ulw    %[in_bits], 0(%[p_buf])          \n\t"
79
        ".set   pop                              \n\t"
80
        : [in_bits] "=r"(in_bits)
81
        : [p_buf] "r"(p_buf)
82
        : "memory", "at");
83
#else
84
11.3k
    lbit_t in_bits;
85
11.3k
    WEBP_UNSAFE_MEMCPY(&in_bits, br->buf, sizeof(in_bits));
86
11.3k
#endif
87
11.3k
    br->buf += BITS >> 3;
88
11.3k
    WEBP_SELF_ASSIGN(br->buf_end);
89
11.3k
#if !defined(WORDS_BIGENDIAN)
90
11.3k
#if (BITS > 32)
91
11.3k
    bits = BSwap64(in_bits);
92
11.3k
    bits >>= 64 - BITS;
93
#elif (BITS >= 24)
94
    bits = BSwap32(in_bits);
95
    bits >>= (32 - BITS);
96
#elif (BITS == 16)
97
    bits = BSwap16(in_bits);
98
#else   // BITS == 8
99
    bits = (bit_t)in_bits;
100
#endif  // BITS > 32
101
#else   // WORDS_BIGENDIAN
102
    bits = (bit_t)in_bits;
103
    if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS);
104
#endif
105
11.3k
    br->value = bits | (br->value << BITS);
106
11.3k
    br->bits += BITS;
107
11.3k
  } else {
108
2.70k
    VP8LoadFinalBytes(br);  // no need to be inlined
109
2.70k
  }
110
14.1k
}
111
112
// Read a bit with proba 'prob'. Speed-critical function!
113
static WEBP_INLINE int VP8GetBit(VP8BitReader* WEBP_RESTRICT const br, int prob,
114
9.77M
                                 const char label[]) {
115
  // Don't move this declaration! It makes a big speed difference to store
116
  // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
117
  // alter br->range value.
118
9.77M
  range_t range = br->range;
119
9.77M
  if (br->bits < 0) {
120
154k
    VP8LoadNewBytes(br);
121
154k
  }
122
9.77M
  {
123
9.77M
    const int pos = br->bits;
124
9.77M
    const range_t split = (range * prob) >> 8;
125
9.77M
    const range_t value = (range_t)(br->value >> pos);
126
9.77M
    const int bit = (value > split);
127
9.77M
    if (bit) {
128
2.17M
      range -= split;
129
2.17M
      br->value -= (bit_t)(split + 1) << pos;
130
7.60M
    } else {
131
7.60M
      range = split + 1;
132
7.60M
    }
133
9.77M
    {
134
9.77M
      const int shift = 7 ^ BitsLog2Floor(range);
135
9.77M
      range <<= shift;
136
9.77M
      br->bits -= shift;
137
9.77M
    }
138
9.77M
    br->range = range - 1;
139
9.77M
    BT_TRACK(br);
140
9.77M
    return bit;
141
9.77M
  }
142
9.77M
}
vp8_dec.c:VP8GetBit
Line
Count
Source
114
4.33M
                                 const char label[]) {
115
  // Don't move this declaration! It makes a big speed difference to store
116
  // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
117
  // alter br->range value.
118
4.33M
  range_t range = br->range;
119
4.33M
  if (br->bits < 0) {
120
56.0k
    VP8LoadNewBytes(br);
121
56.0k
  }
122
4.33M
  {
123
4.33M
    const int pos = br->bits;
124
4.33M
    const range_t split = (range * prob) >> 8;
125
4.33M
    const range_t value = (range_t)(br->value >> pos);
126
4.33M
    const int bit = (value > split);
127
4.33M
    if (bit) {
128
1.14M
      range -= split;
129
1.14M
      br->value -= (bit_t)(split + 1) << pos;
130
3.18M
    } else {
131
3.18M
      range = split + 1;
132
3.18M
    }
133
4.33M
    {
134
4.33M
      const int shift = 7 ^ BitsLog2Floor(range);
135
4.33M
      range <<= shift;
136
4.33M
      br->bits -= shift;
137
4.33M
    }
138
4.33M
    br->range = range - 1;
139
4.33M
    BT_TRACK(br);
140
4.33M
    return bit;
141
4.33M
  }
142
4.33M
}
tree_dec.c:VP8GetBit
Line
Count
Source
114
5.11M
                                 const char label[]) {
115
  // Don't move this declaration! It makes a big speed difference to store
116
  // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
117
  // alter br->range value.
118
5.11M
  range_t range = br->range;
119
5.11M
  if (br->bits < 0) {
120
89.8k
    VP8LoadNewBytes(br);
121
89.8k
  }
122
5.11M
  {
123
5.11M
    const int pos = br->bits;
124
5.11M
    const range_t split = (range * prob) >> 8;
125
5.11M
    const range_t value = (range_t)(br->value >> pos);
126
5.11M
    const int bit = (value > split);
127
5.11M
    if (bit) {
128
832k
      range -= split;
129
832k
      br->value -= (bit_t)(split + 1) << pos;
130
4.28M
    } else {
131
4.28M
      range = split + 1;
132
4.28M
    }
133
5.11M
    {
134
5.11M
      const int shift = 7 ^ BitsLog2Floor(range);
135
5.11M
      range <<= shift;
136
5.11M
      br->bits -= shift;
137
5.11M
    }
138
5.11M
    br->range = range - 1;
139
5.11M
    BT_TRACK(br);
140
5.11M
    return bit;
141
5.11M
  }
142
5.11M
}
bit_reader_utils.c:VP8GetBit
Line
Count
Source
114
326k
                                 const char label[]) {
115
  // Don't move this declaration! It makes a big speed difference to store
116
  // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
117
  // alter br->range value.
118
326k
  range_t range = br->range;
119
326k
  if (br->bits < 0) {
120
8.24k
    VP8LoadNewBytes(br);
121
8.24k
  }
122
326k
  {
123
326k
    const int pos = br->bits;
124
326k
    const range_t split = (range * prob) >> 8;
125
326k
    const range_t value = (range_t)(br->value >> pos);
126
326k
    const int bit = (value > split);
127
326k
    if (bit) {
128
190k
      range -= split;
129
190k
      br->value -= (bit_t)(split + 1) << pos;
130
190k
    } else {
131
135k
      range = split + 1;
132
135k
    }
133
326k
    {
134
326k
      const int shift = 7 ^ BitsLog2Floor(range);
135
326k
      range <<= shift;
136
326k
      br->bits -= shift;
137
326k
    }
138
326k
    br->range = range - 1;
139
326k
    BT_TRACK(br);
140
326k
    return bit;
141
326k
  }
142
326k
}
143
144
// simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)
145
static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE int VP8GetSigned(
146
426k
    VP8BitReader* WEBP_RESTRICT const br, int v, const char label[]) {
147
426k
  if (br->bits < 0) {
148
3.50k
    VP8LoadNewBytes(br);
149
3.50k
  }
150
426k
  {
151
426k
    const int pos = br->bits;
152
426k
    const range_t split = br->range >> 1;
153
426k
    const range_t value = (range_t)(br->value >> pos);
154
426k
    const int32_t mask = (int32_t)(split - value) >> 31;  // -1 or 0
155
426k
    br->bits -= 1;
156
426k
    br->range += (range_t)mask;
157
426k
    br->range |= 1;
158
426k
    br->value -= (bit_t)((split + 1) & (uint32_t)mask) << pos;
159
426k
    BT_TRACK(br);
160
426k
    return (v ^ mask) - mask;
161
426k
  }
162
426k
}
vp8_dec.c:VP8GetSigned
Line
Count
Source
146
426k
    VP8BitReader* WEBP_RESTRICT const br, int v, const char label[]) {
147
426k
  if (br->bits < 0) {
148
3.50k
    VP8LoadNewBytes(br);
149
3.50k
  }
150
426k
  {
151
426k
    const int pos = br->bits;
152
426k
    const range_t split = br->range >> 1;
153
426k
    const range_t value = (range_t)(br->value >> pos);
154
426k
    const int32_t mask = (int32_t)(split - value) >> 31;  // -1 or 0
155
426k
    br->bits -= 1;
156
426k
    br->range += (range_t)mask;
157
426k
    br->range |= 1;
158
426k
    br->value -= (bit_t)((split + 1) & (uint32_t)mask) << pos;
159
426k
    BT_TRACK(br);
160
426k
    return (v ^ mask) - mask;
161
426k
  }
162
426k
}
Unexecuted instantiation: tree_dec.c:VP8GetSigned
Unexecuted instantiation: bit_reader_utils.c:VP8GetSigned
163
164
static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* WEBP_RESTRICT const br,
165
0
                                    int prob, const char label[]) {
166
  // Don't move this declaration! It makes a big speed difference to store
167
  // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
168
  // alter br->range value.
169
0
  range_t range = br->range;
170
0
  if (br->bits < 0) {
171
0
    VP8LoadNewBytes(br);
172
0
  }
173
0
  {
174
0
    const int pos = br->bits;
175
0
    const range_t split = (range * prob) >> 8;
176
0
    const range_t value = (range_t)(br->value >> pos);
177
0
    int bit;  // Don't use 'const int bit = (value > split);", it's slower.
178
0
    if (value > split) {
179
0
      range -= split + 1;
180
0
      br->value -= (bit_t)(split + 1) << pos;
181
0
      bit = 1;
182
0
    } else {
183
0
      range = split;
184
0
      bit = 0;
185
0
    }
186
0
    if (range <= (range_t)0x7e) {
187
0
      const int shift = kVP8Log2Range[range];
188
0
      range = kVP8NewRange[range];
189
0
      br->bits -= shift;
190
0
    }
191
0
    br->range = range;
192
0
    BT_TRACK(br);
193
0
    return bit;
194
0
  }
195
0
}
Unexecuted instantiation: vp8_dec.c:VP8GetBitAlt
Unexecuted instantiation: tree_dec.c:VP8GetBitAlt
Unexecuted instantiation: bit_reader_utils.c:VP8GetBitAlt
196
197
#ifdef __cplusplus
198
}  // extern "C"
199
#endif
200
201
#endif  // WEBP_UTILS_BIT_READER_INL_UTILS_H_