1 // Copyright 2010 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
12 // Author: Skal (pascal.massimino@gmail.com)
13 // Vikas Arora (vikaas.arora@gmail.com)
15 #ifndef WEBP_UTILS_BIT_READER_H_
16 #define WEBP_UTILS_BIT_READER_H_
20 #include <stdlib.h> // _byteswap_ulong
22 #include "../webp/types.h"
28 // The Boolean decoder needs to maintain infinite precision on the value_ field.
29 // However, since range_ is only 8bit, we only need an active window of 8 bits
30 // for value_. Left bits (MSB) gets zeroed and shifted away when value_ falls
31 // below 128, range_ is updated, and fresh bits read from the bitstream are
32 // brought in as LSB. To avoid reading the fresh bits one by one (slow), we
33 // cache BITS of them ahead. The total of (BITS + 8) bits must fit into a
34 // natural register (with type bit_t). To fetch BITS bits from bitstream we
37 // BITS can be any multiple of 8 from 8 to 56 (inclusive).
38 // Pick values that fit natural register size.
40 #if defined(__i386__) || defined(_M_IX86) // x86 32bit
42 #elif defined(__x86_64__) || defined(_M_X64) // x86 64bit
44 #elif defined(__arm__) || defined(_M_ARM) // ARM
46 #elif defined(__mips__) // MIPS
48 #else // reasonable default
49 #define BITS 24 // TODO(skal): test aarch64 and find the proper BITS value.
52 //------------------------------------------------------------------------------
53 // Derived types and constants:
54 // bit_t = natural register type for storing 'value_' (which is BITS+8 bits)
55 // range_t = register for 'range_' (which is 8bits only)
58 typedef uint64_t bit_t
;
60 typedef uint32_t bit_t
;
63 typedef uint32_t range_t
;
65 //------------------------------------------------------------------------------
68 typedef struct VP8BitReader VP8BitReader
;
70 // boolean decoder (keep the field ordering as is!)
71 bit_t value_
; // current value
72 range_t range_
; // current range minus 1. In [127, 254] interval.
73 int bits_
; // number of valid bits left
75 const uint8_t* buf_
; // next byte to be read
76 const uint8_t* buf_end_
; // end of read buffer
77 int eof_
; // true if input is exhausted
80 // Initialize the bit reader and the boolean decoder.
81 void VP8InitBitReader(VP8BitReader
* const br
,
82 const uint8_t* const start
, const uint8_t* const end
);
84 // Update internal pointers to displace the byte buffer by the
85 // relative offset 'offset'.
86 void VP8RemapBitReader(VP8BitReader
* const br
, ptrdiff_t offset
);
88 // return the next value made of 'num_bits' bits
89 uint32_t VP8GetValue(VP8BitReader
* const br
, int num_bits
);
90 static WEBP_INLINE
uint32_t VP8Get(VP8BitReader
* const br
) {
91 return VP8GetValue(br
, 1);
94 // return the next value with sign-extension.
95 int32_t VP8GetSignedValue(VP8BitReader
* const br
, int num_bits
);
97 // bit_reader_inl.h will implement the following methods:
98 // static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob)
99 // static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v)
100 // and should be included by the .c files that actually need them.
101 // This is to avoid recompiling the whole library whenever this file is touched,
102 // and also allowing platform-specific ad-hoc hacks.
104 // -----------------------------------------------------------------------------
105 // Bitreader for lossless format
107 // maximum number of bits (inclusive) the bit-reader can handle:
108 #define VP8L_MAX_NUM_BIT_READ 24
110 typedef uint64_t vp8l_val_t
; // right now, this bit-reader can only use 64bit.
113 vp8l_val_t val_
; // pre-fetched bits
114 const uint8_t* buf_
; // input byte buffer
115 size_t len_
; // buffer length
116 size_t pos_
; // byte position in buf_
117 int bit_pos_
; // current bit-reading position in val_
118 int eos_
; // bitstream is finished
119 int error_
; // an error occurred (buffer overflow attempt...)
122 void VP8LInitBitReader(VP8LBitReader
* const br
,
123 const uint8_t* const start
,
126 // Sets a new data buffer.
127 void VP8LBitReaderSetBuffer(VP8LBitReader
* const br
,
128 const uint8_t* const buffer
, size_t length
);
130 // Reads the specified number of bits from read buffer.
131 // Flags an error in case end_of_stream or n_bits is more than the allowed limit
132 // of VP8L_MAX_NUM_BIT_READ (inclusive).
133 // Flags eos_ if this read attempt is going to cross the read buffer.
134 uint32_t VP8LReadBits(VP8LBitReader
* const br
, int n_bits
);
136 // Return the prefetched bits, so they can be looked up.
137 static WEBP_INLINE
uint32_t VP8LPrefetchBits(VP8LBitReader
* const br
) {
138 return (uint32_t)(br
->val_
>> br
->bit_pos_
);
141 // For jumping over a number of bits in the bit stream when accessed with
142 // VP8LPrefetchBits and VP8LFillBitWindow.
143 static WEBP_INLINE
void VP8LSetBitPos(VP8LBitReader
* const br
, int val
) {
147 // Advances the read buffer by 4 bytes to make room for reading next 32 bits.
148 void VP8LFillBitWindow(VP8LBitReader
* const br
);
154 #endif /* WEBP_UTILS_BIT_READER_H_ */