/src/libwebp/src/utils/bit_reader_inl_utils.h
Line | Count | Source (jump to first uncovered line) |
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/endian_inl_utils.h" |
30 | | #include "src/utils/utils.h" |
31 | | #include "src/webp/types.h" |
32 | | |
33 | | #ifdef __cplusplus |
34 | | extern "C" { |
35 | | #endif |
36 | | |
37 | | //------------------------------------------------------------------------------ |
38 | | // Derived type lbit_t = natural type for memory I/O |
39 | | |
40 | | #if (BITS > 32) |
41 | | typedef uint64_t lbit_t; |
42 | | #elif (BITS > 16) |
43 | | typedef uint32_t lbit_t; |
44 | | #elif (BITS > 8) |
45 | | typedef uint16_t lbit_t; |
46 | | #else |
47 | | typedef uint8_t lbit_t; |
48 | | #endif |
49 | | |
50 | | extern const uint8_t kVP8Log2Range[128]; |
51 | | extern const uint8_t kVP8NewRange[128]; |
52 | | |
53 | | // special case for the tail byte-reading |
54 | | void VP8LoadFinalBytes(VP8BitReader* const br); |
55 | | |
56 | | //------------------------------------------------------------------------------ |
57 | | // Inlined critical functions |
58 | | |
59 | | // makes sure br->value has at least BITS bits worth of data |
60 | | static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE |
61 | 1.69M | void VP8LoadNewBytes(VP8BitReader* WEBP_RESTRICT const br) { |
62 | 1.69M | assert(br != NULL && br->buf != NULL); |
63 | | // Read 'BITS' bits at a time if possible. |
64 | 1.69M | if (br->buf < br->buf_max) { |
65 | | // convert memory type to register type (with some zero'ing!) |
66 | 328k | bit_t bits; |
67 | | #if defined(WEBP_USE_MIPS32) |
68 | | // This is needed because of un-aligned read. |
69 | | lbit_t in_bits; |
70 | | lbit_t* p_buf = (lbit_t*)br->buf; |
71 | | __asm__ volatile( |
72 | | ".set push \n\t" |
73 | | ".set at \n\t" |
74 | | ".set macro \n\t" |
75 | | "ulw %[in_bits], 0(%[p_buf]) \n\t" |
76 | | ".set pop \n\t" |
77 | | : [in_bits]"=r"(in_bits) |
78 | | : [p_buf]"r"(p_buf) |
79 | | : "memory", "at" |
80 | | ); |
81 | | #else |
82 | 328k | lbit_t in_bits; |
83 | 328k | memcpy(&in_bits, br->buf, sizeof(in_bits)); |
84 | 328k | #endif |
85 | 328k | br->buf += BITS >> 3; |
86 | 328k | #if !defined(WORDS_BIGENDIAN) |
87 | 328k | #if (BITS > 32) |
88 | 328k | bits = BSwap64(in_bits); |
89 | 328k | bits >>= 64 - BITS; |
90 | | #elif (BITS >= 24) |
91 | | bits = BSwap32(in_bits); |
92 | | bits >>= (32 - BITS); |
93 | | #elif (BITS == 16) |
94 | | bits = BSwap16(in_bits); |
95 | | #else // BITS == 8 |
96 | | bits = (bit_t)in_bits; |
97 | | #endif // BITS > 32 |
98 | | #else // WORDS_BIGENDIAN |
99 | | bits = (bit_t)in_bits; |
100 | | if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS); |
101 | | #endif |
102 | 328k | br->value = bits | (br->value << BITS); |
103 | 328k | br->bits += BITS; |
104 | 1.37M | } else { |
105 | 1.37M | VP8LoadFinalBytes(br); // no need to be inlined |
106 | 1.37M | } |
107 | 1.69M | } vp8_dec.c:VP8LoadNewBytes Line | Count | Source | 61 | 743k | void VP8LoadNewBytes(VP8BitReader* WEBP_RESTRICT const br) { | 62 | 743k | assert(br != NULL && br->buf != NULL); | 63 | | // Read 'BITS' bits at a time if possible. | 64 | 743k | if (br->buf < br->buf_max) { | 65 | | // convert memory type to register type (with some zero'ing!) | 66 | 270k | bit_t bits; | 67 | | #if defined(WEBP_USE_MIPS32) | 68 | | // This is needed because of un-aligned read. | 69 | | lbit_t in_bits; | 70 | | lbit_t* p_buf = (lbit_t*)br->buf; | 71 | | __asm__ volatile( | 72 | | ".set push \n\t" | 73 | | ".set at \n\t" | 74 | | ".set macro \n\t" | 75 | | "ulw %[in_bits], 0(%[p_buf]) \n\t" | 76 | | ".set pop \n\t" | 77 | | : [in_bits]"=r"(in_bits) | 78 | | : [p_buf]"r"(p_buf) | 79 | | : "memory", "at" | 80 | | ); | 81 | | #else | 82 | 270k | lbit_t in_bits; | 83 | 270k | memcpy(&in_bits, br->buf, sizeof(in_bits)); | 84 | 270k | #endif | 85 | 270k | br->buf += BITS >> 3; | 86 | 270k | #if !defined(WORDS_BIGENDIAN) | 87 | 270k | #if (BITS > 32) | 88 | 270k | bits = BSwap64(in_bits); | 89 | 270k | bits >>= 64 - BITS; | 90 | | #elif (BITS >= 24) | 91 | | bits = BSwap32(in_bits); | 92 | | bits >>= (32 - BITS); | 93 | | #elif (BITS == 16) | 94 | | bits = BSwap16(in_bits); | 95 | | #else // BITS == 8 | 96 | | bits = (bit_t)in_bits; | 97 | | #endif // BITS > 32 | 98 | | #else // WORDS_BIGENDIAN | 99 | | bits = (bit_t)in_bits; | 100 | | if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS); | 101 | | #endif | 102 | 270k | br->value = bits | (br->value << BITS); | 103 | 270k | br->bits += BITS; | 104 | 473k | } else { | 105 | 473k | VP8LoadFinalBytes(br); // no need to be inlined | 106 | 473k | } | 107 | 743k | } |
bit_reader_utils.c:VP8LoadNewBytes Line | Count | Source | 61 | 244k | void VP8LoadNewBytes(VP8BitReader* WEBP_RESTRICT const br) { | 62 | 244k | assert(br != NULL && br->buf != NULL); | 63 | | // Read 'BITS' bits at a time if possible. | 64 | 244k | if (br->buf < br->buf_max) { | 65 | | // convert memory type to register type (with some zero'ing!) | 66 | 4.45k | bit_t bits; | 67 | | #if defined(WEBP_USE_MIPS32) | 68 | | // This is needed because of un-aligned read. | 69 | | lbit_t in_bits; | 70 | | lbit_t* p_buf = (lbit_t*)br->buf; | 71 | | __asm__ volatile( | 72 | | ".set push \n\t" | 73 | | ".set at \n\t" | 74 | | ".set macro \n\t" | 75 | | "ulw %[in_bits], 0(%[p_buf]) \n\t" | 76 | | ".set pop \n\t" | 77 | | : [in_bits]"=r"(in_bits) | 78 | | : [p_buf]"r"(p_buf) | 79 | | : "memory", "at" | 80 | | ); | 81 | | #else | 82 | 4.45k | lbit_t in_bits; | 83 | 4.45k | memcpy(&in_bits, br->buf, sizeof(in_bits)); | 84 | 4.45k | #endif | 85 | 4.45k | br->buf += BITS >> 3; | 86 | 4.45k | #if !defined(WORDS_BIGENDIAN) | 87 | 4.45k | #if (BITS > 32) | 88 | 4.45k | bits = BSwap64(in_bits); | 89 | 4.45k | bits >>= 64 - BITS; | 90 | | #elif (BITS >= 24) | 91 | | bits = BSwap32(in_bits); | 92 | | bits >>= (32 - BITS); | 93 | | #elif (BITS == 16) | 94 | | bits = BSwap16(in_bits); | 95 | | #else // BITS == 8 | 96 | | bits = (bit_t)in_bits; | 97 | | #endif // BITS > 32 | 98 | | #else // WORDS_BIGENDIAN | 99 | | bits = (bit_t)in_bits; | 100 | | if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS); | 101 | | #endif | 102 | 4.45k | br->value = bits | (br->value << BITS); | 103 | 4.45k | br->bits += BITS; | 104 | 240k | } else { | 105 | 240k | VP8LoadFinalBytes(br); // no need to be inlined | 106 | 240k | } | 107 | 244k | } |
tree_dec.c:VP8LoadNewBytes Line | Count | Source | 61 | 710k | void VP8LoadNewBytes(VP8BitReader* WEBP_RESTRICT const br) { | 62 | 710k | assert(br != NULL && br->buf != NULL); | 63 | | // Read 'BITS' bits at a time if possible. | 64 | 710k | if (br->buf < br->buf_max) { | 65 | | // convert memory type to register type (with some zero'ing!) | 66 | 53.7k | bit_t bits; | 67 | | #if defined(WEBP_USE_MIPS32) | 68 | | // This is needed because of un-aligned read. | 69 | | lbit_t in_bits; | 70 | | lbit_t* p_buf = (lbit_t*)br->buf; | 71 | | __asm__ volatile( | 72 | | ".set push \n\t" | 73 | | ".set at \n\t" | 74 | | ".set macro \n\t" | 75 | | "ulw %[in_bits], 0(%[p_buf]) \n\t" | 76 | | ".set pop \n\t" | 77 | | : [in_bits]"=r"(in_bits) | 78 | | : [p_buf]"r"(p_buf) | 79 | | : "memory", "at" | 80 | | ); | 81 | | #else | 82 | 53.7k | lbit_t in_bits; | 83 | 53.7k | memcpy(&in_bits, br->buf, sizeof(in_bits)); | 84 | 53.7k | #endif | 85 | 53.7k | br->buf += BITS >> 3; | 86 | 53.7k | #if !defined(WORDS_BIGENDIAN) | 87 | 53.7k | #if (BITS > 32) | 88 | 53.7k | bits = BSwap64(in_bits); | 89 | 53.7k | bits >>= 64 - BITS; | 90 | | #elif (BITS >= 24) | 91 | | bits = BSwap32(in_bits); | 92 | | bits >>= (32 - BITS); | 93 | | #elif (BITS == 16) | 94 | | bits = BSwap16(in_bits); | 95 | | #else // BITS == 8 | 96 | | bits = (bit_t)in_bits; | 97 | | #endif // BITS > 32 | 98 | | #else // WORDS_BIGENDIAN | 99 | | bits = (bit_t)in_bits; | 100 | | if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS); | 101 | | #endif | 102 | 53.7k | br->value = bits | (br->value << BITS); | 103 | 53.7k | br->bits += BITS; | 104 | 657k | } else { | 105 | 657k | VP8LoadFinalBytes(br); // no need to be inlined | 106 | 657k | } | 107 | 710k | } |
|
108 | | |
109 | | // Read a bit with proba 'prob'. Speed-critical function! |
110 | | static WEBP_INLINE int VP8GetBit(VP8BitReader* WEBP_RESTRICT const br, |
111 | 28.7M | int prob, const char label[]) { |
112 | | // Don't move this declaration! It makes a big speed difference to store |
113 | | // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't |
114 | | // alter br->range value. |
115 | 28.7M | range_t range = br->range; |
116 | 28.7M | if (br->bits < 0) { |
117 | 1.64M | VP8LoadNewBytes(br); |
118 | 1.64M | } |
119 | 28.7M | { |
120 | 28.7M | const int pos = br->bits; |
121 | 28.7M | const range_t split = (range * prob) >> 8; |
122 | 28.7M | const range_t value = (range_t)(br->value >> pos); |
123 | 28.7M | const int bit = (value > split); |
124 | 28.7M | if (bit) { |
125 | 8.75M | range -= split; |
126 | 8.75M | br->value -= (bit_t)(split + 1) << pos; |
127 | 19.9M | } else { |
128 | 19.9M | range = split + 1; |
129 | 19.9M | } |
130 | 28.7M | { |
131 | 28.7M | const int shift = 7 ^ BitsLog2Floor(range); |
132 | 28.7M | range <<= shift; |
133 | 28.7M | br->bits -= shift; |
134 | 28.7M | } |
135 | 28.7M | br->range = range - 1; |
136 | 28.7M | BT_TRACK(br); |
137 | 28.7M | return bit; |
138 | 28.7M | } |
139 | 28.7M | } Line | Count | Source | 111 | 18.2M | int prob, const char label[]) { | 112 | | // Don't move this declaration! It makes a big speed difference to store | 113 | | // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't | 114 | | // alter br->range value. | 115 | 18.2M | range_t range = br->range; | 116 | 18.2M | if (br->bits < 0) { | 117 | 696k | VP8LoadNewBytes(br); | 118 | 696k | } | 119 | 18.2M | { | 120 | 18.2M | const int pos = br->bits; | 121 | 18.2M | const range_t split = (range * prob) >> 8; | 122 | 18.2M | const range_t value = (range_t)(br->value >> pos); | 123 | 18.2M | const int bit = (value > split); | 124 | 18.2M | if (bit) { | 125 | 7.32M | range -= split; | 126 | 7.32M | br->value -= (bit_t)(split + 1) << pos; | 127 | 10.8M | } else { | 128 | 10.8M | range = split + 1; | 129 | 10.8M | } | 130 | 18.2M | { | 131 | 18.2M | const int shift = 7 ^ BitsLog2Floor(range); | 132 | 18.2M | range <<= shift; | 133 | 18.2M | br->bits -= shift; | 134 | 18.2M | } | 135 | 18.2M | br->range = range - 1; | 136 | 18.2M | BT_TRACK(br); | 137 | 18.2M | return bit; | 138 | 18.2M | } | 139 | 18.2M | } |
bit_reader_utils.c:VP8GetBit Line | Count | Source | 111 | 383k | int prob, const char label[]) { | 112 | | // Don't move this declaration! It makes a big speed difference to store | 113 | | // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't | 114 | | // alter br->range value. | 115 | 383k | range_t range = br->range; | 116 | 383k | if (br->bits < 0) { | 117 | 240k | VP8LoadNewBytes(br); | 118 | 240k | } | 119 | 383k | { | 120 | 383k | const int pos = br->bits; | 121 | 383k | const range_t split = (range * prob) >> 8; | 122 | 383k | const range_t value = (range_t)(br->value >> pos); | 123 | 383k | const int bit = (value > split); | 124 | 383k | if (bit) { | 125 | 314k | range -= split; | 126 | 314k | br->value -= (bit_t)(split + 1) << pos; | 127 | 314k | } else { | 128 | 68.6k | range = split + 1; | 129 | 68.6k | } | 130 | 383k | { | 131 | 383k | const int shift = 7 ^ BitsLog2Floor(range); | 132 | 383k | range <<= shift; | 133 | 383k | br->bits -= shift; | 134 | 383k | } | 135 | 383k | br->range = range - 1; | 136 | 383k | BT_TRACK(br); | 137 | 383k | return bit; | 138 | 383k | } | 139 | 383k | } |
Line | Count | Source | 111 | 10.1M | int prob, const char label[]) { | 112 | | // Don't move this declaration! It makes a big speed difference to store | 113 | | // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't | 114 | | // alter br->range value. | 115 | 10.1M | range_t range = br->range; | 116 | 10.1M | if (br->bits < 0) { | 117 | 710k | VP8LoadNewBytes(br); | 118 | 710k | } | 119 | 10.1M | { | 120 | 10.1M | const int pos = br->bits; | 121 | 10.1M | const range_t split = (range * prob) >> 8; | 122 | 10.1M | const range_t value = (range_t)(br->value >> pos); | 123 | 10.1M | const int bit = (value > split); | 124 | 10.1M | if (bit) { | 125 | 1.11M | range -= split; | 126 | 1.11M | br->value -= (bit_t)(split + 1) << pos; | 127 | 9.03M | } else { | 128 | 9.03M | range = split + 1; | 129 | 9.03M | } | 130 | 10.1M | { | 131 | 10.1M | const int shift = 7 ^ BitsLog2Floor(range); | 132 | 10.1M | range <<= shift; | 133 | 10.1M | br->bits -= shift; | 134 | 10.1M | } | 135 | 10.1M | br->range = range - 1; | 136 | 10.1M | BT_TRACK(br); | 137 | 10.1M | return bit; | 138 | 10.1M | } | 139 | 10.1M | } |
|
140 | | |
141 | | // simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here) |
142 | | static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE |
143 | | int VP8GetSigned(VP8BitReader* WEBP_RESTRICT const br, int v, |
144 | 2.79M | const char label[]) { |
145 | 2.79M | if (br->bits < 0) { |
146 | 47.2k | VP8LoadNewBytes(br); |
147 | 47.2k | } |
148 | 2.79M | { |
149 | 2.79M | const int pos = br->bits; |
150 | 2.79M | const range_t split = br->range >> 1; |
151 | 2.79M | const range_t value = (range_t)(br->value >> pos); |
152 | 2.79M | const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0 |
153 | 2.79M | br->bits -= 1; |
154 | 2.79M | br->range += (range_t)mask; |
155 | 2.79M | br->range |= 1; |
156 | 2.79M | br->value -= (bit_t)((split + 1) & (uint32_t)mask) << pos; |
157 | 2.79M | BT_TRACK(br); |
158 | 2.79M | return (v ^ mask) - mask; |
159 | 2.79M | } |
160 | 2.79M | } Line | Count | Source | 144 | 2.79M | const char label[]) { | 145 | 2.79M | if (br->bits < 0) { | 146 | 47.2k | VP8LoadNewBytes(br); | 147 | 47.2k | } | 148 | 2.79M | { | 149 | 2.79M | const int pos = br->bits; | 150 | 2.79M | const range_t split = br->range >> 1; | 151 | 2.79M | const range_t value = (range_t)(br->value >> pos); | 152 | 2.79M | const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0 | 153 | 2.79M | br->bits -= 1; | 154 | 2.79M | br->range += (range_t)mask; | 155 | 2.79M | br->range |= 1; | 156 | 2.79M | br->value -= (bit_t)((split + 1) & (uint32_t)mask) << pos; | 157 | 2.79M | BT_TRACK(br); | 158 | 2.79M | return (v ^ mask) - mask; | 159 | 2.79M | } | 160 | 2.79M | } |
Unexecuted instantiation: bit_reader_utils.c:VP8GetSigned Unexecuted instantiation: tree_dec.c:VP8GetSigned |
161 | | |
162 | | static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* WEBP_RESTRICT const br, |
163 | 0 | int prob, const char label[]) { |
164 | | // Don't move this declaration! It makes a big speed difference to store |
165 | | // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't |
166 | | // alter br->range value. |
167 | 0 | range_t range = br->range; |
168 | 0 | if (br->bits < 0) { |
169 | 0 | VP8LoadNewBytes(br); |
170 | 0 | } |
171 | 0 | { |
172 | 0 | const int pos = br->bits; |
173 | 0 | const range_t split = (range * prob) >> 8; |
174 | 0 | const range_t value = (range_t)(br->value >> pos); |
175 | 0 | int bit; // Don't use 'const int bit = (value > split);", it's slower. |
176 | 0 | if (value > split) { |
177 | 0 | range -= split + 1; |
178 | 0 | br->value -= (bit_t)(split + 1) << pos; |
179 | 0 | bit = 1; |
180 | 0 | } else { |
181 | 0 | range = split; |
182 | 0 | bit = 0; |
183 | 0 | } |
184 | 0 | if (range <= (range_t)0x7e) { |
185 | 0 | const int shift = kVP8Log2Range[range]; |
186 | 0 | range = kVP8NewRange[range]; |
187 | 0 | br->bits -= shift; |
188 | 0 | } |
189 | 0 | br->range = range; |
190 | 0 | BT_TRACK(br); |
191 | 0 | return bit; |
192 | 0 | } |
193 | 0 | } Unexecuted instantiation: vp8_dec.c:VP8GetBitAlt Unexecuted instantiation: bit_reader_utils.c:VP8GetBitAlt Unexecuted instantiation: tree_dec.c:VP8GetBitAlt |
194 | | |
195 | | #ifdef __cplusplus |
196 | | } // extern "C" |
197 | | #endif |
198 | | |
199 | | #endif // WEBP_UTILS_BIT_READER_INL_UTILS_H_ |