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 non-inlined methods | 10 // Boolean decoder non-inlined methods |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } | 98 } |
99 | 99 |
100 int32_t VP8GetSignedValue(VP8BitReader* const br, int bits) { | 100 int32_t VP8GetSignedValue(VP8BitReader* const br, int bits) { |
101 const int value = VP8GetValue(br, bits); | 101 const int value = VP8GetValue(br, bits); |
102 return VP8Get(br) ? -value : value; | 102 return VP8Get(br) ? -value : value; |
103 } | 103 } |
104 | 104 |
105 //------------------------------------------------------------------------------ | 105 //------------------------------------------------------------------------------ |
106 // VP8LBitReader | 106 // VP8LBitReader |
107 | 107 |
108 #define LBITS 64 // Number of bits prefetched. | 108 #define VP8L_LOG8_WBITS 4 // Number of bytes needed to store VP8L_WBITS bits. |
109 #define WBITS 32 // Minimum number of bytes needed after VP8LFillBitWindow. | |
110 #define LOG8_WBITS 4 // Number of bytes needed to store WBITS bits. | |
111 | 109 |
112 #if !defined(WEBP_FORCE_ALIGNED) && \ | 110 #if !defined(WEBP_FORCE_ALIGNED) && \ |
113 (defined(__arm__) || defined(_M_ARM) || defined(__aarch64__) || \ | 111 (defined(__arm__) || defined(_M_ARM) || defined(__aarch64__) || \ |
114 defined(__i386__) || defined(_M_IX86) || \ | 112 defined(__i386__) || defined(_M_IX86) || \ |
115 defined(__x86_64__) || defined(_M_X64)) | 113 defined(__x86_64__) || defined(_M_X64)) |
116 #define VP8L_USE_UNALIGNED_LOAD | 114 #define VP8L_USE_UNALIGNED_LOAD |
117 #endif | 115 #endif |
118 | 116 |
119 static const uint32_t kBitMask[VP8L_MAX_NUM_BIT_READ + 1] = { | 117 static const uint32_t kBitMask[VP8L_MAX_NUM_BIT_READ + 1] = { |
120 0, | 118 0, |
(...skipping 23 matching lines...) Expand all Loading... |
144 length = sizeof(br->val_); | 142 length = sizeof(br->val_); |
145 } | 143 } |
146 for (i = 0; i < length; ++i) { | 144 for (i = 0; i < length; ++i) { |
147 value |= (vp8l_val_t)start[i] << (8 * i); | 145 value |= (vp8l_val_t)start[i] << (8 * i); |
148 } | 146 } |
149 br->val_ = value; | 147 br->val_ = value; |
150 br->pos_ = length; | 148 br->pos_ = length; |
151 br->buf_ = start; | 149 br->buf_ = start; |
152 } | 150 } |
153 | 151 |
154 // Special version that assumes br->pos_ <= br_len_. | |
155 static int IsEndOfStreamSpecial(const VP8LBitReader* const br) { | |
156 assert(br->pos_ <= br->len_); | |
157 return br->pos_ == br->len_ && br->bit_pos_ >= LBITS; | |
158 } | |
159 | |
160 static int IsEndOfStream(const VP8LBitReader* const br) { | |
161 return (br->pos_ > br->len_) || IsEndOfStreamSpecial(br); | |
162 } | |
163 | |
164 void VP8LBitReaderSetBuffer(VP8LBitReader* const br, | 152 void VP8LBitReaderSetBuffer(VP8LBitReader* const br, |
165 const uint8_t* const buf, size_t len) { | 153 const uint8_t* const buf, size_t len) { |
166 assert(br != NULL); | 154 assert(br != NULL); |
167 assert(buf != NULL); | 155 assert(buf != NULL); |
168 assert(len < 0xfffffff8u); // can't happen with a RIFF chunk. | 156 assert(len < 0xfffffff8u); // can't happen with a RIFF chunk. |
169 br->buf_ = buf; | 157 br->buf_ = buf; |
170 br->len_ = len; | 158 br->len_ = len; |
171 br->eos_ = IsEndOfStream(br); | 159 // pos_ > len_ should be considered a param error. |
| 160 br->error_ = (br->pos_ > br->len_); |
| 161 br->eos_ = br->error_ || VP8LIsEndOfStream(br); |
172 } | 162 } |
173 | 163 |
174 // If not at EOS, reload up to LBITS byte-by-byte | 164 // If not at EOS, reload up to VP8L_LBITS byte-by-byte |
175 static void ShiftBytes(VP8LBitReader* const br) { | 165 static void ShiftBytes(VP8LBitReader* const br) { |
176 while (br->bit_pos_ >= 8 && br->pos_ < br->len_) { | 166 while (br->bit_pos_ >= 8 && br->pos_ < br->len_) { |
177 br->val_ >>= 8; | 167 br->val_ >>= 8; |
178 br->val_ |= ((vp8l_val_t)br->buf_[br->pos_]) << (LBITS - 8); | 168 br->val_ |= ((vp8l_val_t)br->buf_[br->pos_]) << (VP8L_LBITS - 8); |
179 ++br->pos_; | 169 ++br->pos_; |
180 br->bit_pos_ -= 8; | 170 br->bit_pos_ -= 8; |
181 } | 171 } |
| 172 br->eos_ = VP8LIsEndOfStream(br); |
182 } | 173 } |
183 | 174 |
184 void VP8LFillBitWindow(VP8LBitReader* const br) { | 175 void VP8LDoFillBitWindow(VP8LBitReader* const br) { |
185 if (br->bit_pos_ >= WBITS) { | 176 assert(br->bit_pos_ >= VP8L_WBITS); |
186 // TODO(jzern): given the fixed read size it may be possible to force | 177 // TODO(jzern): given the fixed read size it may be possible to force |
187 // alignment in this block. | 178 // alignment in this block. |
188 #if defined(VP8L_USE_UNALIGNED_LOAD) | 179 #if defined(VP8L_USE_UNALIGNED_LOAD) |
189 if (br->pos_ + sizeof(br->val_) < br->len_) { | 180 if (br->pos_ + sizeof(br->val_) < br->len_) { |
190 br->val_ >>= WBITS; | 181 br->val_ >>= VP8L_WBITS; |
191 br->bit_pos_ -= WBITS; | 182 br->bit_pos_ -= VP8L_WBITS; |
192 // The expression below needs a little-endian arch to work correctly. | 183 // The expression below needs a little-endian arch to work correctly. |
193 // This gives a large speedup for decoding speed. | 184 // This gives a large speedup for decoding speed. |
194 br->val_ |= (vp8l_val_t)*(const uint32_t*)(br->buf_ + br->pos_) << | 185 br->val_ |= (vp8l_val_t)*(const uint32_t*)(br->buf_ + br->pos_) << |
195 (LBITS - WBITS); | 186 (VP8L_LBITS - VP8L_WBITS); |
196 br->pos_ += LOG8_WBITS; | 187 br->pos_ += VP8L_LOG8_WBITS; |
197 return; | 188 return; |
198 } | 189 } |
199 #endif | 190 #endif |
200 ShiftBytes(br); // Slow path. | 191 ShiftBytes(br); // Slow path. |
201 br->eos_ = IsEndOfStreamSpecial(br); | |
202 } | |
203 } | 192 } |
204 | 193 |
205 uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits) { | 194 uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits) { |
206 assert(n_bits >= 0); | 195 assert(n_bits >= 0); |
207 // Flag an error if end_of_stream or n_bits is more than allowed limit. | 196 // Flag an error if end_of_stream or n_bits is more than allowed limit. |
208 if (!br->eos_ && n_bits <= VP8L_MAX_NUM_BIT_READ) { | 197 if (!br->eos_ && n_bits <= VP8L_MAX_NUM_BIT_READ) { |
209 const uint32_t val = | 198 const uint32_t val = |
210 (uint32_t)(br->val_ >> br->bit_pos_) & kBitMask[n_bits]; | 199 (uint32_t)(br->val_ >> br->bit_pos_) & kBitMask[n_bits]; |
211 const int new_bits = br->bit_pos_ + n_bits; | 200 const int new_bits = br->bit_pos_ + n_bits; |
212 br->bit_pos_ = new_bits; | 201 br->bit_pos_ = new_bits; |
213 // If this read is going to cross the read buffer, set the eos flag. | |
214 br->eos_ = IsEndOfStreamSpecial(br); | |
215 ShiftBytes(br); | 202 ShiftBytes(br); |
216 return val; | 203 return val; |
217 } else { | 204 } else { |
218 br->error_ = 1; | 205 br->error_ = 1; |
219 return 0; | 206 return 0; |
220 } | 207 } |
221 } | 208 } |
222 | 209 |
223 //------------------------------------------------------------------------------ | 210 //------------------------------------------------------------------------------ |
OLD | NEW |