OLD | NEW |
1 // Copyright 2010 Google Inc. All Rights Reserved. | 1 // Copyright 2010 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Boolean decoder | 10 // Boolean decoder |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // and should be included by the .c files that actually need them. | 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, | 101 // This is to avoid recompiling the whole library whenever this file is touched, |
102 // and also allowing platform-specific ad-hoc hacks. | 102 // and also allowing platform-specific ad-hoc hacks. |
103 | 103 |
104 // ----------------------------------------------------------------------------- | 104 // ----------------------------------------------------------------------------- |
105 // Bitreader for lossless format | 105 // Bitreader for lossless format |
106 | 106 |
107 // maximum number of bits (inclusive) the bit-reader can handle: | 107 // maximum number of bits (inclusive) the bit-reader can handle: |
108 #define VP8L_MAX_NUM_BIT_READ 24 | 108 #define VP8L_MAX_NUM_BIT_READ 24 |
109 | 109 |
| 110 #define VP8L_LBITS 64 // Number of bits prefetched. |
| 111 #define VP8L_WBITS 32 // Minimum number of bytes ready after VP8LFillBitWindow. |
| 112 |
110 typedef uint64_t vp8l_val_t; // right now, this bit-reader can only use 64bit. | 113 typedef uint64_t vp8l_val_t; // right now, this bit-reader can only use 64bit. |
111 | 114 |
112 typedef struct { | 115 typedef struct { |
113 vp8l_val_t val_; // pre-fetched bits | 116 vp8l_val_t val_; // pre-fetched bits |
114 const uint8_t* buf_; // input byte buffer | 117 const uint8_t* buf_; // input byte buffer |
115 size_t len_; // buffer length | 118 size_t len_; // buffer length |
116 size_t pos_; // byte position in buf_ | 119 size_t pos_; // byte position in buf_ |
117 int bit_pos_; // current bit-reading position in val_ | 120 int bit_pos_; // current bit-reading position in val_ |
118 int eos_; // bitstream is finished | 121 int eos_; // bitstream is finished |
119 int error_; // an error occurred (buffer overflow attempt...) | 122 int error_; // an error occurred (buffer overflow attempt...) |
(...skipping 11 matching lines...) Expand all Loading... |
131 // Flags an error in case end_of_stream or n_bits is more than the allowed limit | 134 // 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). | 135 // of VP8L_MAX_NUM_BIT_READ (inclusive). |
133 // Flags eos_ if this read attempt is going to cross the read buffer. | 136 // Flags eos_ if this read attempt is going to cross the read buffer. |
134 uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits); | 137 uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits); |
135 | 138 |
136 // Return the prefetched bits, so they can be looked up. | 139 // Return the prefetched bits, so they can be looked up. |
137 static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) { | 140 static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) { |
138 return (uint32_t)(br->val_ >> br->bit_pos_); | 141 return (uint32_t)(br->val_ >> br->bit_pos_); |
139 } | 142 } |
140 | 143 |
| 144 // Returns true if there was an attempt at reading bit past the end of |
| 145 // the buffer. Doesn't set br->eos_ flag. |
| 146 static WEBP_INLINE int VP8LIsEndOfStream(const VP8LBitReader* const br) { |
| 147 assert(br->pos_ <= br->len_); |
| 148 return (br->pos_ == br->len_) && (br->bit_pos_ > VP8L_LBITS); |
| 149 } |
| 150 |
141 // For jumping over a number of bits in the bit stream when accessed with | 151 // For jumping over a number of bits in the bit stream when accessed with |
142 // VP8LPrefetchBits and VP8LFillBitWindow. | 152 // VP8LPrefetchBits and VP8LFillBitWindow. |
143 static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) { | 153 static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) { |
144 br->bit_pos_ = val; | 154 br->bit_pos_ = val; |
| 155 br->eos_ = VP8LIsEndOfStream(br); |
145 } | 156 } |
146 | 157 |
147 // Advances the read buffer by 4 bytes to make room for reading next 32 bits. | 158 // Advances the read buffer by 4 bytes to make room for reading next 32 bits. |
148 void VP8LFillBitWindow(VP8LBitReader* const br); | 159 // Speed critical, but infrequent part of the code can be non-inligned. |
| 160 extern void VP8LDoFillBitWindow(VP8LBitReader* const br); |
| 161 static WEBP_INLINE void VP8LFillBitWindow(VP8LBitReader* const br) { |
| 162 if (br->bit_pos_ >= VP8L_WBITS) VP8LDoFillBitWindow(br); |
| 163 } |
149 | 164 |
150 #ifdef __cplusplus | 165 #ifdef __cplusplus |
151 } // extern "C" | 166 } // extern "C" |
152 #endif | 167 #endif |
153 | 168 |
154 #endif /* WEBP_UTILS_BIT_READER_H_ */ | 169 #endif /* WEBP_UTILS_BIT_READER_H_ */ |
OLD | NEW |