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