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