Coverage Report

Created: 2025-12-03 07:28

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
35.3M
    VP8BitReader* WEBP_RESTRICT const br) {
65
35.3M
  assert(br != NULL && br->buf != NULL);
66
  // Read 'BITS' bits at a time if possible.
67
35.3M
  if (br->buf < br->buf_max) {
68
    // convert memory type to register type (with some zero'ing!)
69
2.24M
    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
2.24M
    lbit_t in_bits;
85
2.24M
    WEBP_UNSAFE_MEMCPY(&in_bits, br->buf, sizeof(in_bits));
86
2.24M
#endif
87
2.24M
    br->buf += BITS >> 3;
88
2.24M
    WEBP_SELF_ASSIGN(br->buf_end);
89
2.24M
#if !defined(WORDS_BIGENDIAN)
90
2.24M
#if (BITS > 32)
91
2.24M
    bits = BSwap64(in_bits);
92
2.24M
    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
2.24M
    br->value = bits | (br->value << BITS);
106
2.24M
    br->bits += BITS;
107
33.1M
  } else {
108
33.1M
    VP8LoadFinalBytes(br);  // no need to be inlined
109
33.1M
  }
110
35.3M
}
tree_dec.c:VP8LoadNewBytes
Line
Count
Source
64
524k
    VP8BitReader* WEBP_RESTRICT const br) {
65
524k
  assert(br != NULL && br->buf != NULL);
66
  // Read 'BITS' bits at a time if possible.
67
524k
  if (br->buf < br->buf_max) {
68
    // convert memory type to register type (with some zero'ing!)
69
125k
    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
125k
    lbit_t in_bits;
85
125k
    WEBP_UNSAFE_MEMCPY(&in_bits, br->buf, sizeof(in_bits));
86
125k
#endif
87
125k
    br->buf += BITS >> 3;
88
125k
    WEBP_SELF_ASSIGN(br->buf_end);
89
125k
#if !defined(WORDS_BIGENDIAN)
90
125k
#if (BITS > 32)
91
125k
    bits = BSwap64(in_bits);
92
125k
    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
125k
    br->value = bits | (br->value << BITS);
106
125k
    br->bits += BITS;
107
399k
  } else {
108
399k
    VP8LoadFinalBytes(br);  // no need to be inlined
109
399k
  }
110
524k
}
vp8_dec.c:VP8LoadNewBytes
Line
Count
Source
64
31.6M
    VP8BitReader* WEBP_RESTRICT const br) {
65
31.6M
  assert(br != NULL && br->buf != NULL);
66
  // Read 'BITS' bits at a time if possible.
67
31.6M
  if (br->buf < br->buf_max) {
68
    // convert memory type to register type (with some zero'ing!)
69
1.39M
    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
1.39M
    lbit_t in_bits;
85
1.39M
    WEBP_UNSAFE_MEMCPY(&in_bits, br->buf, sizeof(in_bits));
86
1.39M
#endif
87
1.39M
    br->buf += BITS >> 3;
88
1.39M
    WEBP_SELF_ASSIGN(br->buf_end);
89
1.39M
#if !defined(WORDS_BIGENDIAN)
90
1.39M
#if (BITS > 32)
91
1.39M
    bits = BSwap64(in_bits);
92
1.39M
    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
1.39M
    br->value = bits | (br->value << BITS);
106
1.39M
    br->bits += BITS;
107
30.2M
  } else {
108
30.2M
    VP8LoadFinalBytes(br);  // no need to be inlined
109
30.2M
  }
110
31.6M
}
bit_reader_utils.c:VP8LoadNewBytes
Line
Count
Source
64
3.20M
    VP8BitReader* WEBP_RESTRICT const br) {
65
3.20M
  assert(br != NULL && br->buf != NULL);
66
  // Read 'BITS' bits at a time if possible.
67
3.20M
  if (br->buf < br->buf_max) {
68
    // convert memory type to register type (with some zero'ing!)
69
720k
    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
720k
    lbit_t in_bits;
85
720k
    WEBP_UNSAFE_MEMCPY(&in_bits, br->buf, sizeof(in_bits));
86
720k
#endif
87
720k
    br->buf += BITS >> 3;
88
720k
    WEBP_SELF_ASSIGN(br->buf_end);
89
720k
#if !defined(WORDS_BIGENDIAN)
90
720k
#if (BITS > 32)
91
720k
    bits = BSwap64(in_bits);
92
720k
    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
720k
    br->value = bits | (br->value << BITS);
106
720k
    br->bits += BITS;
107
2.48M
  } else {
108
2.48M
    VP8LoadFinalBytes(br);  // no need to be inlined
109
2.48M
  }
110
3.20M
}
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
257M
                                 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
257M
  range_t range = br->range;
119
257M
  if (br->bits < 0) {
120
33.2M
    VP8LoadNewBytes(br);
121
33.2M
  }
122
257M
  {
123
257M
    const int pos = br->bits;
124
257M
    const range_t split = (range * prob) >> 8;
125
257M
    const range_t value = (range_t)(br->value >> pos);
126
257M
    const int bit = (value > split);
127
257M
    if (bit) {
128
77.7M
      range -= split;
129
77.7M
      br->value -= (bit_t)(split + 1) << pos;
130
180M
    } else {
131
180M
      range = split + 1;
132
180M
    }
133
257M
    {
134
257M
      const int shift = 7 ^ BitsLog2Floor(range);
135
257M
      range <<= shift;
136
257M
      br->bits -= shift;
137
257M
    }
138
257M
    br->range = range - 1;
139
257M
    BT_TRACK(br);
140
257M
    return bit;
141
257M
  }
142
257M
}
tree_dec.c:VP8GetBit
Line
Count
Source
114
160M
                                 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
160M
  range_t range = br->range;
119
160M
  if (br->bits < 0) {
120
524k
    VP8LoadNewBytes(br);
121
524k
  }
122
160M
  {
123
160M
    const int pos = br->bits;
124
160M
    const range_t split = (range * prob) >> 8;
125
160M
    const range_t value = (range_t)(br->value >> pos);
126
160M
    const int bit = (value > split);
127
160M
    if (bit) {
128
3.23M
      range -= split;
129
3.23M
      br->value -= (bit_t)(split + 1) << pos;
130
156M
    } else {
131
156M
      range = split + 1;
132
156M
    }
133
160M
    {
134
160M
      const int shift = 7 ^ BitsLog2Floor(range);
135
160M
      range <<= shift;
136
160M
      br->bits -= shift;
137
160M
    }
138
160M
    br->range = range - 1;
139
160M
    BT_TRACK(br);
140
160M
    return bit;
141
160M
  }
142
160M
}
vp8_dec.c:VP8GetBit
Line
Count
Source
114
72.5M
                                 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
72.5M
  range_t range = br->range;
119
72.5M
  if (br->bits < 0) {
120
29.9M
    VP8LoadNewBytes(br);
121
29.9M
  }
122
72.5M
  {
123
72.5M
    const int pos = br->bits;
124
72.5M
    const range_t split = (range * prob) >> 8;
125
72.5M
    const range_t value = (range_t)(br->value >> pos);
126
72.5M
    const int bit = (value > split);
127
72.5M
    if (bit) {
128
61.0M
      range -= split;
129
61.0M
      br->value -= (bit_t)(split + 1) << pos;
130
61.0M
    } else {
131
11.5M
      range = split + 1;
132
11.5M
    }
133
72.5M
    {
134
72.5M
      const int shift = 7 ^ BitsLog2Floor(range);
135
72.5M
      range <<= shift;
136
72.5M
      br->bits -= shift;
137
72.5M
    }
138
72.5M
    br->range = range - 1;
139
72.5M
    BT_TRACK(br);
140
72.5M
    return bit;
141
72.5M
  }
142
72.5M
}
bit_reader_utils.c:VP8GetBit
Line
Count
Source
114
25.2M
                                 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
25.2M
  range_t range = br->range;
119
25.2M
  if (br->bits < 0) {
120
2.78M
    VP8LoadNewBytes(br);
121
2.78M
  }
122
25.2M
  {
123
25.2M
    const int pos = br->bits;
124
25.2M
    const range_t split = (range * prob) >> 8;
125
25.2M
    const range_t value = (range_t)(br->value >> pos);
126
25.2M
    const int bit = (value > split);
127
25.2M
    if (bit) {
128
13.4M
      range -= split;
129
13.4M
      br->value -= (bit_t)(split + 1) << pos;
130
13.4M
    } else {
131
11.8M
      range = split + 1;
132
11.8M
    }
133
25.2M
    {
134
25.2M
      const int shift = 7 ^ BitsLog2Floor(range);
135
25.2M
      range <<= shift;
136
25.2M
      br->bits -= shift;
137
25.2M
    }
138
25.2M
    br->range = range - 1;
139
25.2M
    BT_TRACK(br);
140
25.2M
    return bit;
141
25.2M
  }
142
25.2M
}
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
5.16M
    VP8BitReader* WEBP_RESTRICT const br, int v, const char label[]) {
147
5.16M
  if (br->bits < 0) {
148
1.70M
    VP8LoadNewBytes(br);
149
1.70M
  }
150
5.16M
  {
151
5.16M
    const int pos = br->bits;
152
5.16M
    const range_t split = br->range >> 1;
153
5.16M
    const range_t value = (range_t)(br->value >> pos);
154
5.16M
    const int32_t mask = (int32_t)(split - value) >> 31;  // -1 or 0
155
5.16M
    br->bits -= 1;
156
5.16M
    br->range += (range_t)mask;
157
5.16M
    br->range |= 1;
158
5.16M
    br->value -= (bit_t)((split + 1) & (uint32_t)mask) << pos;
159
5.16M
    BT_TRACK(br);
160
5.16M
    return (v ^ mask) - mask;
161
5.16M
  }
162
5.16M
}
Unexecuted instantiation: tree_dec.c:VP8GetSigned
vp8_dec.c:VP8GetSigned
Line
Count
Source
146
5.16M
    VP8BitReader* WEBP_RESTRICT const br, int v, const char label[]) {
147
5.16M
  if (br->bits < 0) {
148
1.70M
    VP8LoadNewBytes(br);
149
1.70M
  }
150
5.16M
  {
151
5.16M
    const int pos = br->bits;
152
5.16M
    const range_t split = br->range >> 1;
153
5.16M
    const range_t value = (range_t)(br->value >> pos);
154
5.16M
    const int32_t mask = (int32_t)(split - value) >> 31;  // -1 or 0
155
5.16M
    br->bits -= 1;
156
5.16M
    br->range += (range_t)mask;
157
5.16M
    br->range |= 1;
158
5.16M
    br->value -= (bit_t)((split + 1) & (uint32_t)mask) << pos;
159
5.16M
    BT_TRACK(br);
160
5.16M
    return (v ^ mask) - mask;
161
5.16M
  }
162
5.16M
}
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: tree_dec.c:VP8GetBitAlt
Unexecuted instantiation: vp8_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_