Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1000)

Side by Side Diff: third_party/libwebp/utils/bit_reader.h

Issue 116213006: Update libwebp to 0.4.0 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: After Blink Roll Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/libwebp/utils/alpha_processing.c ('k') | third_party/libwebp/utils/bit_reader.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 // 11 //
12 // Author: Skal (pascal.massimino@gmail.com) 12 // Author: Skal (pascal.massimino@gmail.com)
13 // Vikas Arora (vikaas.arora@gmail.com) 13 // Vikas Arora (vikaas.arora@gmail.com)
14 14
15 #ifndef WEBP_UTILS_BIT_READER_H_ 15 #ifndef WEBP_UTILS_BIT_READER_H_
16 #define WEBP_UTILS_BIT_READER_H_ 16 #define WEBP_UTILS_BIT_READER_H_
17 17
18 #include <assert.h> 18 #include <assert.h>
19 #ifdef _MSC_VER 19 #ifdef _MSC_VER
20 #include <stdlib.h> // _byteswap_ulong 20 #include <stdlib.h> // _byteswap_ulong
21 #endif 21 #endif
22 #include <string.h> // For memcpy
23 #include "../webp/types.h" 22 #include "../webp/types.h"
24 23
25 #if defined(__cplusplus) || defined(c_plusplus) 24 #ifdef __cplusplus
26 extern "C" { 25 extern "C" {
27 #endif 26 #endif
28 27
29 // The Boolean decoder needs to maintain infinite precision on the value_ field. 28 // The Boolean decoder needs to maintain infinite precision on the value_ field.
30 // However, since range_ is only 8bit, we only need an active window of 8 bits 29 // However, since range_ is only 8bit, we only need an active window of 8 bits
31 // for value_. Left bits (MSB) gets zeroed and shifted away when value_ falls 30 // for value_. Left bits (MSB) gets zeroed and shifted away when value_ falls
32 // below 128, range_ is updated, and fresh bits read from the bitstream are 31 // below 128, range_ is updated, and fresh bits read from the bitstream are
33 // brought in as LSB. 32 // brought in as LSB.
34 // To avoid reading the fresh bits one by one (slow), we cache a few of them 33 // To avoid reading the fresh bits one by one (slow), we cache a few of them
35 // ahead (actually, we cache BITS of them ahead. See below). There's two 34 // ahead (actually, we cache BITS of them ahead. See below). There's two
(...skipping 12 matching lines...) Expand all
48 // || 47 // ||
49 // After calling VP8Shift(), where we need to shift away two zeros: 48 // After calling VP8Shift(), where we need to shift away two zeros:
50 // [........vvvvvvvvBBBBBBBBBBB00000]LSB || [.............vvvvvvvvBBBBBBBBBBB] 49 // [........vvvvvvvvBBBBBBBBBBB00000]LSB || [.............vvvvvvvvBBBBBBBBBBB]
51 // || 50 // ||
52 // Just before we need to call VP8LoadNewBytes(), the situation is: 51 // Just before we need to call VP8LoadNewBytes(), the situation is:
53 // [........vvvvvv000000000000000000]LSB || [..........................vvvvvv] 52 // [........vvvvvv000000000000000000]LSB || [..........................vvvvvv]
54 // || 53 // ||
55 // And just after calling VP8LoadNewBytes(): 54 // And just after calling VP8LoadNewBytes():
56 // [........vvvvvvvvBBBBBBBBBBBBBBBB]LSB || [........vvvvvvvvBBBBBBBBBBBBBBBB] 55 // [........vvvvvvvvBBBBBBBBBBBBBBBB]LSB || [........vvvvvvvvBBBBBBBBBBBBBBBB]
57 // 56 //
58 // -> we're back to height active 'value_' bits (marked 'v') and BITS cached 57 // -> we're back to eight active 'value_' bits (marked 'v') and BITS cached
59 // bits (marked 'B') 58 // bits (marked 'B')
60 // 59 //
61 // The right-justify strategy tends to use less shifts and is often faster. 60 // The right-justify strategy tends to use less shifts and is often faster.
62 61
63 //------------------------------------------------------------------------------ 62 //------------------------------------------------------------------------------
64 // BITS can be any multiple of 8 from 8 to 56 (inclusive). 63 // BITS can be any multiple of 8 from 8 to 56 (inclusive).
65 // Pick values that fit natural register size. 64 // Pick values that fit natural register size.
66 65
67 #if !defined(WEBP_REFERENCE_IMPLEMENTATION) 66 #if !defined(WEBP_REFERENCE_IMPLEMENTATION)
68 67
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 extern const range_t kVP8NewRange[128]; 147 extern const range_t kVP8NewRange[128];
149 148
150 void VP8LoadFinalBytes(VP8BitReader* const br); // special case for the tail 149 void VP8LoadFinalBytes(VP8BitReader* const br); // special case for the tail
151 150
152 static WEBP_INLINE void VP8LoadNewBytes(VP8BitReader* const br) { 151 static WEBP_INLINE void VP8LoadNewBytes(VP8BitReader* const br) {
153 assert(br != NULL && br->buf_ != NULL); 152 assert(br != NULL && br->buf_ != NULL);
154 // Read 'BITS' bits at a time if possible. 153 // Read 'BITS' bits at a time if possible.
155 if (br->buf_ + sizeof(lbit_t) <= br->buf_end_) { 154 if (br->buf_ + sizeof(lbit_t) <= br->buf_end_) {
156 // convert memory type to register type (with some zero'ing!) 155 // convert memory type to register type (with some zero'ing!)
157 bit_t bits; 156 bit_t bits;
158 lbit_t in_bits = *(lbit_t*)br->buf_; 157 const lbit_t in_bits = *(const lbit_t*)br->buf_;
159 br->buf_ += (BITS) >> 3; 158 br->buf_ += (BITS) >> 3;
160 #if !defined(__BIG_ENDIAN__) 159 #if !defined(__BIG_ENDIAN__)
161 #if (BITS > 32) 160 #if (BITS > 32)
162 // gcc 4.3 has builtin functions for swap32/swap64 161 // gcc 4.3 has builtin functions for swap32/swap64
163 #if defined(__GNUC__) && \ 162 #if defined(__GNUC__) && \
164 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) 163 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
165 bits = (bit_t)__builtin_bswap64(in_bits); 164 bits = (bit_t)__builtin_bswap64(in_bits);
166 #elif defined(_MSC_VER) 165 #elif defined(_MSC_VER)
167 bits = (bit_t)_byteswap_uint64(in_bits); 166 bits = (bit_t)_byteswap_uint64(in_bits);
168 #elif defined(__x86_64__) 167 #elif defined(__x86_64__)
169 __asm__ volatile("bswapq %0" : "=r"(bits) : "0"(in_bits)); 168 __asm__ volatile("bswapq %0" : "=r"(bits) : "0"(in_bits));
170 #else // generic code for swapping 64-bit values (suggested by bdb@) 169 #else // generic code for swapping 64-bit values (suggested by bdb@)
171 bits = (bit_t)in_bits; 170 bits = (bit_t)in_bits;
172 bits = ((bits & 0xffffffff00000000ull) >> 32) | 171 bits = ((bits & 0xffffffff00000000ull) >> 32) |
173 ((bits & 0x00000000ffffffffull) << 32); 172 ((bits & 0x00000000ffffffffull) << 32);
174 bits = ((bits & 0xffff0000ffff0000ull) >> 16) | 173 bits = ((bits & 0xffff0000ffff0000ull) >> 16) |
175 ((bits & 0x0000ffff0000ffffull) << 16); 174 ((bits & 0x0000ffff0000ffffull) << 16);
176 bits = ((bits & 0xff00ff00ff00ff00ull) >> 8) | 175 bits = ((bits & 0xff00ff00ff00ff00ull) >> 8) |
177 ((bits & 0x00ff00ff00ff00ffull) << 8); 176 ((bits & 0x00ff00ff00ff00ffull) << 8);
178 #endif 177 #endif
179 bits >>= 64 - BITS; 178 bits >>= 64 - BITS;
180 #elif (BITS >= 24) 179 #elif (BITS >= 24)
181 #if defined(__i386__) || defined(__x86_64__) 180 #if defined(__i386__) || defined(__x86_64__)
182 __asm__ volatile("bswap %k0" : "=r"(in_bits) : "0"(in_bits)); 181 {
183 bits = (bit_t)in_bits; // 24b/32b -> 32b/64b zero-extension 182 lbit_t swapped_in_bits;
183 __asm__ volatile("bswap %k0" : "=r"(swapped_in_bits) : "0"(in_bits));
184 bits = (bit_t)swapped_in_bits; // 24b/32b -> 32b/64b zero-extension
185 }
184 #elif defined(_MSC_VER) 186 #elif defined(_MSC_VER)
185 bits = (bit_t)_byteswap_ulong(in_bits); 187 bits = (bit_t)_byteswap_ulong(in_bits);
186 #else 188 #else
187 bits = (bit_t)(in_bits >> 24) | ((in_bits >> 8) & 0xff00) 189 bits = (bit_t)(in_bits >> 24) | ((in_bits >> 8) & 0xff00)
188 | ((in_bits << 8) & 0xff0000) | (in_bits << 24); 190 | ((in_bits << 8) & 0xff0000) | (in_bits << 24);
189 #endif // x86 191 #endif // x86
190 bits >>= (32 - BITS); 192 bits >>= (32 - BITS);
191 #elif (BITS == 16) 193 #elif (BITS == 16)
192 // gcc will recognize a 'rorw $8, ...' here: 194 // gcc will recognize a 'rorw $8, ...' here:
193 bits = (bit_t)(in_bits >> 8) | ((in_bits & 0xff) << 8); 195 bits = (bit_t)(in_bits >> 8) | ((in_bits & 0xff) << 8);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 br->range_ = kVP8NewRange[idx]; 249 br->range_ = kVP8NewRange[idx];
248 br->value_ <<= shift; 250 br->value_ <<= shift;
249 br->bits_ -= shift; 251 br->bits_ -= shift;
250 #else 252 #else
251 const int shift = kVP8Log2Range[br->range_]; 253 const int shift = kVP8Log2Range[br->range_];
252 assert(br->range_ < (range_t)128); 254 assert(br->range_ < (range_t)128);
253 br->range_ = kVP8NewRange[br->range_]; 255 br->range_ = kVP8NewRange[br->range_];
254 br->bits_ -= shift; 256 br->bits_ -= shift;
255 #endif 257 #endif
256 } 258 }
259
257 static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) { 260 static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) {
258 #ifndef USE_RIGHT_JUSTIFY 261 #ifndef USE_RIGHT_JUSTIFY
259 // It's important to avoid generating a 64bit x 64bit multiply here. 262 // It's important to avoid generating a 64bit x 64bit multiply here.
260 // We just need an 8b x 8b after all. 263 // We just need an 8b x 8b after all.
261 const range_t split = 264 const range_t split =
262 (range_t)((uint32_t)(br->range_ >> (BITS)) * prob) << ((BITS) - 8); 265 (range_t)((uint32_t)(br->range_ >> (BITS)) * prob) << ((BITS) - 8);
263 const int bit = VP8BitUpdate(br, split); 266 const int bit = VP8BitUpdate(br, split);
264 if (br->range_ <= (((range_t)0x7e << (BITS)) | (MASK))) { 267 if (br->range_ <= (((range_t)0x7e << (BITS)) | (MASK))) {
265 VP8Shift(br); 268 VP8Shift(br);
266 } 269 }
267 return bit; 270 return bit;
268 #else 271 #else
269 const range_t split = (br->range_ * prob) >> 8; 272 const range_t split = (br->range_ * prob) >> 8;
270 const int bit = VP8BitUpdate(br, split); 273 const int bit = VP8BitUpdate(br, split);
271 if (br->range_ <= (range_t)0x7e) { 274 if (br->range_ <= (range_t)0x7e) {
272 VP8Shift(br); 275 VP8Shift(br);
273 } 276 }
274 return bit; 277 return bit;
275 #endif 278 #endif
276 } 279 }
277 280
278 static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) { 281 static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) {
279 const range_t split = (br->range_ >> 1); 282 const range_t split = (br->range_ >> 1);
280 const int bit = VP8BitUpdate(br, split); 283 const int bit = VP8BitUpdate(br, split);
281 VP8Shift(br); 284 VP8Shift(br);
282 return bit ? -v : v; 285 return bit ? -v : v;
283 } 286 }
284 287
285
286 // ----------------------------------------------------------------------------- 288 // -----------------------------------------------------------------------------
287 // Bitreader for lossless format 289 // Bitreader for lossless format
288 290
289 typedef uint64_t vp8l_val_t; // right now, this bit-reader can only use 64bit. 291 typedef uint64_t vp8l_val_t; // right now, this bit-reader can only use 64bit.
290 292
291 typedef struct { 293 typedef struct {
292 vp8l_val_t val_; // pre-fetched bits 294 vp8l_val_t val_; // pre-fetched bits
293 const uint8_t* buf_; // input byte buffer 295 const uint8_t* buf_; // input byte buffer
294 size_t len_; // buffer length 296 size_t len_; // buffer length
295 size_t pos_; // byte position in buf_ 297 size_t pos_; // byte position in buf_
(...skipping 13 matching lines...) Expand all
309 // Reads the specified number of bits from Read Buffer. 311 // Reads the specified number of bits from Read Buffer.
310 // Flags an error in case end_of_stream or n_bits is more than allowed limit. 312 // Flags an error in case end_of_stream or n_bits is more than allowed limit.
311 // Flags eos if this read attempt is going to cross the read buffer. 313 // Flags eos if this read attempt is going to cross the read buffer.
312 uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits); 314 uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits);
313 315
314 // Return the prefetched bits, so they can be looked up. 316 // Return the prefetched bits, so they can be looked up.
315 static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) { 317 static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) {
316 return (uint32_t)(br->val_ >> br->bit_pos_); 318 return (uint32_t)(br->val_ >> br->bit_pos_);
317 } 319 }
318 320
319 // Discard 'num_bits' bits from the cache. 321 // For jumping over a number of bits in the bit stream when accessed with
320 static WEBP_INLINE void VP8LDiscardBits(VP8LBitReader* const br, int num_bits) { 322 // VP8LPrefetchBits and VP8LFillBitWindow.
321 br->bit_pos_ += num_bits; 323 static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) {
324 br->bit_pos_ = val;
322 } 325 }
323 326
324 // Advances the Read buffer by 4 bytes to make room for reading next 32 bits. 327 // Advances the read buffer by 4 bytes to make room for reading next 32 bits.
325 void VP8LFillBitWindow(VP8LBitReader* const br); 328 void VP8LFillBitWindow(VP8LBitReader* const br);
326 329
327 #if defined(__cplusplus) || defined(c_plusplus) 330 #ifdef __cplusplus
328 } // extern "C" 331 } // extern "C"
329 #endif 332 #endif
330 333
331 #endif /* WEBP_UTILS_BIT_READER_H_ */ 334 #endif /* WEBP_UTILS_BIT_READER_H_ */
OLDNEW
« no previous file with comments | « third_party/libwebp/utils/alpha_processing.c ('k') | third_party/libwebp/utils/bit_reader.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698