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

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

Issue 1546003002: libwebp: update to 0.5.0 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 'defines' exists Created 4 years, 12 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
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
(...skipping 25 matching lines...) Expand all
36 // 36 //
37 // BITS can be any multiple of 8 from 8 to 56 (inclusive). 37 // BITS can be any multiple of 8 from 8 to 56 (inclusive).
38 // Pick values that fit natural register size. 38 // Pick values that fit natural register size.
39 39
40 #if defined(__i386__) || defined(_M_IX86) // x86 32bit 40 #if defined(__i386__) || defined(_M_IX86) // x86 32bit
41 #define BITS 24 41 #define BITS 24
42 #elif defined(__x86_64__) || defined(_M_X64) // x86 64bit 42 #elif defined(__x86_64__) || defined(_M_X64) // x86 64bit
43 #define BITS 56 43 #define BITS 56
44 #elif defined(__arm__) || defined(_M_ARM) // ARM 44 #elif defined(__arm__) || defined(_M_ARM) // ARM
45 #define BITS 24 45 #define BITS 24
46 #elif defined(__aarch64__) // ARM 64bit
47 #define BITS 56
46 #elif defined(__mips__) // MIPS 48 #elif defined(__mips__) // MIPS
47 #define BITS 24 49 #define BITS 24
48 #else // reasonable default 50 #else // reasonable default
49 #define BITS 24 // TODO(skal): test aarch64 and find the proper BITS value. 51 #define BITS 24
50 #endif 52 #endif
51 53
52 //------------------------------------------------------------------------------ 54 //------------------------------------------------------------------------------
53 // Derived types and constants: 55 // Derived types and constants:
54 // bit_t = natural register type for storing 'value_' (which is BITS+8 bits) 56 // bit_t = natural register type for storing 'value_' (which is BITS+8 bits)
55 // range_t = register for 'range_' (which is 8bits only) 57 // range_t = register for 'range_' (which is 8bits only)
56 58
57 #if (BITS > 24) 59 #if (BITS > 24)
58 typedef uint64_t bit_t; 60 typedef uint64_t bit_t;
59 #else 61 #else
60 typedef uint32_t bit_t; 62 typedef uint32_t bit_t;
61 #endif 63 #endif
62 64
63 typedef uint32_t range_t; 65 typedef uint32_t range_t;
64 66
65 //------------------------------------------------------------------------------ 67 //------------------------------------------------------------------------------
66 // Bitreader 68 // Bitreader
67 69
68 typedef struct VP8BitReader VP8BitReader; 70 typedef struct VP8BitReader VP8BitReader;
69 struct VP8BitReader { 71 struct VP8BitReader {
70 // boolean decoder (keep the field ordering as is!) 72 // boolean decoder (keep the field ordering as is!)
71 bit_t value_; // current value 73 bit_t value_; // current value
72 range_t range_; // current range minus 1. In [127, 254] interval. 74 range_t range_; // current range minus 1. In [127, 254] interval.
73 int bits_; // number of valid bits left 75 int bits_; // number of valid bits left
74 // read buffer 76 // read buffer
75 const uint8_t* buf_; // next byte to be read 77 const uint8_t* buf_; // next byte to be read
76 const uint8_t* buf_end_; // end of read buffer 78 const uint8_t* buf_end_; // end of read buffer
79 const uint8_t* buf_max_; // max packed-read position on buffer
77 int eof_; // true if input is exhausted 80 int eof_; // true if input is exhausted
78 }; 81 };
79 82
80 // Initialize the bit reader and the boolean decoder. 83 // Initialize the bit reader and the boolean decoder.
81 void VP8InitBitReader(VP8BitReader* const br, 84 void VP8InitBitReader(VP8BitReader* const br,
82 const uint8_t* const start, const uint8_t* const end); 85 const uint8_t* const start, size_t size);
86 // Sets the working read buffer.
87 void VP8BitReaderSetBuffer(VP8BitReader* const br,
88 const uint8_t* const start, size_t size);
83 89
84 // Update internal pointers to displace the byte buffer by the 90 // Update internal pointers to displace the byte buffer by the
85 // relative offset 'offset'. 91 // relative offset 'offset'.
86 void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset); 92 void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset);
87 93
88 // return the next value made of 'num_bits' bits 94 // return the next value made of 'num_bits' bits
89 uint32_t VP8GetValue(VP8BitReader* const br, int num_bits); 95 uint32_t VP8GetValue(VP8BitReader* const br, int num_bits);
90 static WEBP_INLINE uint32_t VP8Get(VP8BitReader* const br) { 96 static WEBP_INLINE uint32_t VP8Get(VP8BitReader* const br) {
91 return VP8GetValue(br, 1); 97 return VP8GetValue(br, 1);
92 } 98 }
93 99
94 // return the next value with sign-extension. 100 // return the next value with sign-extension.
95 int32_t VP8GetSignedValue(VP8BitReader* const br, int num_bits); 101 int32_t VP8GetSignedValue(VP8BitReader* const br, int num_bits);
96 102
97 // bit_reader_inl.h will implement the following methods: 103 // bit_reader_inl.h will implement the following methods:
98 // static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) 104 // static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob)
99 // static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) 105 // static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v)
100 // and should be included by the .c files that actually need them. 106 // 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, 107 // This is to avoid recompiling the whole library whenever this file is touched,
102 // and also allowing platform-specific ad-hoc hacks. 108 // and also allowing platform-specific ad-hoc hacks.
103 109
104 // ----------------------------------------------------------------------------- 110 // -----------------------------------------------------------------------------
105 // Bitreader for lossless format 111 // Bitreader for lossless format
106 112
107 // maximum number of bits (inclusive) the bit-reader can handle: 113 // maximum number of bits (inclusive) the bit-reader can handle:
108 #define VP8L_MAX_NUM_BIT_READ 24 114 #define VP8L_MAX_NUM_BIT_READ 24
109 115
110 #define VP8L_LBITS 64 // Number of bits prefetched. 116 #define VP8L_LBITS 64 // Number of bits prefetched (= bit-size of vp8l_val_t).
111 #define VP8L_WBITS 32 // Minimum number of bytes ready after VP8LFillBitWindow. 117 #define VP8L_WBITS 32 // Minimum number of bytes ready after VP8LFillBitWindow.
112 118
113 typedef uint64_t vp8l_val_t; // right now, this bit-reader can only use 64bit. 119 typedef uint64_t vp8l_val_t; // right now, this bit-reader can only use 64bit.
114 120
115 typedef struct { 121 typedef struct {
116 vp8l_val_t val_; // pre-fetched bits 122 vp8l_val_t val_; // pre-fetched bits
117 const uint8_t* buf_; // input byte buffer 123 const uint8_t* buf_; // input byte buffer
118 size_t len_; // buffer length 124 size_t len_; // buffer length
119 size_t pos_; // byte position in buf_ 125 size_t pos_; // byte position in buf_
120 int bit_pos_; // current bit-reading position in val_ 126 int bit_pos_; // current bit-reading position in val_
121 int eos_; // bitstream is finished 127 int eos_; // true if a bit was read past the end of buffer
122 int error_; // an error occurred (buffer overflow attempt...)
123 } VP8LBitReader; 128 } VP8LBitReader;
124 129
125 void VP8LInitBitReader(VP8LBitReader* const br, 130 void VP8LInitBitReader(VP8LBitReader* const br,
126 const uint8_t* const start, 131 const uint8_t* const start,
127 size_t length); 132 size_t length);
128 133
129 // Sets a new data buffer. 134 // Sets a new data buffer.
130 void VP8LBitReaderSetBuffer(VP8LBitReader* const br, 135 void VP8LBitReaderSetBuffer(VP8LBitReader* const br,
131 const uint8_t* const buffer, size_t length); 136 const uint8_t* const buffer, size_t length);
132 137
133 // Reads the specified number of bits from read buffer. 138 // Reads the specified number of bits from read buffer.
134 // Flags an error in case end_of_stream or n_bits is more than the allowed limit 139 // Flags an error in case end_of_stream or n_bits is more than the allowed limit
135 // of VP8L_MAX_NUM_BIT_READ (inclusive). 140 // of VP8L_MAX_NUM_BIT_READ (inclusive).
136 // Flags eos_ if this read attempt is going to cross the read buffer. 141 // Flags eos_ if this read attempt is going to cross the read buffer.
137 uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits); 142 uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits);
138 143
139 // Return the prefetched bits, so they can be looked up. 144 // Return the prefetched bits, so they can be looked up.
140 static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) { 145 static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) {
141 return (uint32_t)(br->val_ >> br->bit_pos_); 146 return (uint32_t)(br->val_ >> (br->bit_pos_ & (VP8L_LBITS - 1)));
142 } 147 }
143 148
144 // Returns true if there was an attempt at reading bit past the end of 149 // Returns true if there was an attempt at reading bit past the end of
145 // the buffer. Doesn't set br->eos_ flag. 150 // the buffer. Doesn't set br->eos_ flag.
146 static WEBP_INLINE int VP8LIsEndOfStream(const VP8LBitReader* const br) { 151 static WEBP_INLINE int VP8LIsEndOfStream(const VP8LBitReader* const br) {
147 assert(br->pos_ <= br->len_); 152 assert(br->pos_ <= br->len_);
148 return (br->pos_ == br->len_) && (br->bit_pos_ > VP8L_LBITS); 153 return br->eos_ || ((br->pos_ == br->len_) && (br->bit_pos_ > VP8L_LBITS));
149 } 154 }
150 155
151 // For jumping over a number of bits in the bit stream when accessed with 156 // For jumping over a number of bits in the bit stream when accessed with
152 // VP8LPrefetchBits and VP8LFillBitWindow. 157 // VP8LPrefetchBits and VP8LFillBitWindow.
153 static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) { 158 static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) {
154 br->bit_pos_ = val; 159 br->bit_pos_ = val;
155 br->eos_ = VP8LIsEndOfStream(br); 160 br->eos_ = VP8LIsEndOfStream(br);
156 } 161 }
157 162
158 // Advances the read buffer by 4 bytes to make room for reading next 32 bits. 163 // Advances the read buffer by 4 bytes to make room for reading next 32 bits.
159 // Speed critical, but infrequent part of the code can be non-inlined. 164 // Speed critical, but infrequent part of the code can be non-inlined.
160 extern void VP8LDoFillBitWindow(VP8LBitReader* const br); 165 extern void VP8LDoFillBitWindow(VP8LBitReader* const br);
161 static WEBP_INLINE void VP8LFillBitWindow(VP8LBitReader* const br) { 166 static WEBP_INLINE void VP8LFillBitWindow(VP8LBitReader* const br) {
162 if (br->bit_pos_ >= VP8L_WBITS) VP8LDoFillBitWindow(br); 167 if (br->bit_pos_ >= VP8L_WBITS) VP8LDoFillBitWindow(br);
163 } 168 }
164 169
165 #ifdef __cplusplus 170 #ifdef __cplusplus
166 } // extern "C" 171 } // extern "C"
167 #endif 172 #endif
168 173
169 #endif /* WEBP_UTILS_BIT_READER_H_ */ 174 #endif /* WEBP_UTILS_BIT_READER_H_ */
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698