Coverage Report

Created: 2026-09-28 06:47

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