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

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

Issue 2651883004: libwebp-0.6.0-rc1 (Closed)
Patch Set: Created 3 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
OLDNEW
1 // Copyright 2014 Google Inc. All Rights Reserved. 1 // Copyright 2014 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 // Specific inlined methods for boolean decoder [VP8GetBit() ...] 10 // Specific inlined methods for boolean decoder [VP8GetBit() ...]
11 // This file should be included by the .c sources that actually need to call 11 // This file should be included by the .c sources that actually need to call
12 // these methods. 12 // these methods.
13 // 13 //
14 // Author: Skal (pascal.massimino@gmail.com) 14 // Author: Skal (pascal.massimino@gmail.com)
15 15
16 #ifndef WEBP_UTILS_BIT_READER_INL_H_ 16 #ifndef WEBP_UTILS_BIT_READER_INL_H_
17 #define WEBP_UTILS_BIT_READER_INL_H_ 17 #define WEBP_UTILS_BIT_READER_INL_H_
18 18
19 #ifdef HAVE_CONFIG_H 19 #ifdef HAVE_CONFIG_H
20 #include "../webp/config.h" 20 #include "../webp/config.h"
21 #endif 21 #endif
22 22
23 #ifdef WEBP_FORCE_ALIGNED 23 #include <string.h> // for memcpy
24 #include <string.h> // memcpy
25 #endif
26 24
27 #include "../dsp/dsp.h" 25 #include "../dsp/dsp.h"
28 #include "./bit_reader.h" 26 #include "./bit_reader_utils.h"
29 #include "./endian_inl.h" 27 #include "./endian_inl_utils.h"
28 #include "./utils.h"
30 29
31 #ifdef __cplusplus 30 #ifdef __cplusplus
32 extern "C" { 31 extern "C" {
33 #endif 32 #endif
34 33
35 //------------------------------------------------------------------------------ 34 //------------------------------------------------------------------------------
36 // Derived type lbit_t = natural type for memory I/O 35 // Derived type lbit_t = natural type for memory I/O
37 36
38 #if (BITS > 32) 37 #if (BITS > 32)
39 typedef uint64_t lbit_t; 38 typedef uint64_t lbit_t;
(...skipping 15 matching lines...) Expand all
55 // Inlined critical functions 54 // Inlined critical functions
56 55
57 // makes sure br->value_ has at least BITS bits worth of data 56 // makes sure br->value_ has at least BITS bits worth of data
58 static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE 57 static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE
59 void VP8LoadNewBytes(VP8BitReader* const br) { 58 void VP8LoadNewBytes(VP8BitReader* const br) {
60 assert(br != NULL && br->buf_ != NULL); 59 assert(br != NULL && br->buf_ != NULL);
61 // Read 'BITS' bits at a time if possible. 60 // Read 'BITS' bits at a time if possible.
62 if (br->buf_ < br->buf_max_) { 61 if (br->buf_ < br->buf_max_) {
63 // convert memory type to register type (with some zero'ing!) 62 // convert memory type to register type (with some zero'ing!)
64 bit_t bits; 63 bit_t bits;
65 #if defined(WEBP_FORCE_ALIGNED) 64 #if defined(WEBP_USE_MIPS32)
66 lbit_t in_bits;
67 memcpy(&in_bits, br->buf_, sizeof(in_bits));
68 #elif defined(WEBP_USE_MIPS32)
69 // This is needed because of un-aligned read. 65 // This is needed because of un-aligned read.
70 lbit_t in_bits; 66 lbit_t in_bits;
71 lbit_t* p_buf_ = (lbit_t*)br->buf_; 67 lbit_t* p_buf_ = (lbit_t*)br->buf_;
72 __asm__ volatile( 68 __asm__ volatile(
73 ".set push \n\t" 69 ".set push \n\t"
74 ".set at \n\t" 70 ".set at \n\t"
75 ".set macro \n\t" 71 ".set macro \n\t"
76 "ulw %[in_bits], 0(%[p_buf_]) \n\t" 72 "ulw %[in_bits], 0(%[p_buf_]) \n\t"
77 ".set pop \n\t" 73 ".set pop \n\t"
78 : [in_bits]"=r"(in_bits) 74 : [in_bits]"=r"(in_bits)
79 : [p_buf_]"r"(p_buf_) 75 : [p_buf_]"r"(p_buf_)
80 : "memory", "at" 76 : "memory", "at"
81 ); 77 );
82 #else 78 #else
83 const lbit_t in_bits = *(const lbit_t*)br->buf_; 79 lbit_t in_bits;
80 memcpy(&in_bits, br->buf_, sizeof(in_bits));
84 #endif 81 #endif
85 br->buf_ += BITS >> 3; 82 br->buf_ += BITS >> 3;
86 #if !defined(WORDS_BIGENDIAN) 83 #if !defined(WORDS_BIGENDIAN)
87 #if (BITS > 32) 84 #if (BITS > 32)
88 bits = BSwap64(in_bits); 85 bits = BSwap64(in_bits);
89 bits >>= 64 - BITS; 86 bits >>= 64 - BITS;
90 #elif (BITS >= 24) 87 #elif (BITS >= 24)
91 bits = BSwap32(in_bits); 88 bits = BSwap32(in_bits);
92 bits >>= (32 - BITS); 89 bits >>= (32 - BITS);
93 #elif (BITS == 16) 90 #elif (BITS == 16)
(...skipping 18 matching lines...) Expand all
112 // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't 109 // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
113 // alter br->range_ value. 110 // alter br->range_ value.
114 range_t range = br->range_; 111 range_t range = br->range_;
115 if (br->bits_ < 0) { 112 if (br->bits_ < 0) {
116 VP8LoadNewBytes(br); 113 VP8LoadNewBytes(br);
117 } 114 }
118 { 115 {
119 const int pos = br->bits_; 116 const int pos = br->bits_;
120 const range_t split = (range * prob) >> 8; 117 const range_t split = (range * prob) >> 8;
121 const range_t value = (range_t)(br->value_ >> pos); 118 const range_t value = (range_t)(br->value_ >> pos);
122 #if defined(__arm__) || defined(_M_ARM) // ARM-specific 119 const int bit = (value > split);
123 const int bit = ((int)(split - value) >> 31) & 1; 120 if (bit) {
124 if (value > split) { 121 range -= split;
125 range -= split + 1;
126 br->value_ -= (bit_t)(split + 1) << pos; 122 br->value_ -= (bit_t)(split + 1) << pos;
127 } else { 123 } else {
128 range = split; 124 range = split + 1;
129 } 125 }
130 #else // faster version on x86 126 {
131 int bit; // Don't use 'const int bit = (value > split);", it's slower. 127 const int shift = 7 ^ BitsLog2Floor(range);
132 if (value > split) { 128 range <<= shift;
133 range -= split + 1;
134 br->value_ -= (bit_t)(split + 1) << pos;
135 bit = 1;
136 } else {
137 range = split;
138 bit = 0;
139 }
140 #endif
141 if (range <= (range_t)0x7e) {
142 const int shift = kVP8Log2Range[range];
143 range = kVP8NewRange[range];
144 br->bits_ -= shift; 129 br->bits_ -= shift;
145 } 130 }
146 br->range_ = range; 131 br->range_ = range - 1;
147 return bit; 132 return bit;
148 } 133 }
149 } 134 }
150 135
151 // simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here) 136 // simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)
152 static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) { 137 static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
138 int VP8GetSigned(VP8BitReader* const br, int v) {
153 if (br->bits_ < 0) { 139 if (br->bits_ < 0) {
154 VP8LoadNewBytes(br); 140 VP8LoadNewBytes(br);
155 } 141 }
156 { 142 {
157 const int pos = br->bits_; 143 const int pos = br->bits_;
158 const range_t split = br->range_ >> 1; 144 const range_t split = br->range_ >> 1;
159 const range_t value = (range_t)(br->value_ >> pos); 145 const range_t value = (range_t)(br->value_ >> pos);
160 const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0 146 const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0
161 br->bits_ -= 1; 147 br->bits_ -= 1;
162 br->range_ += mask; 148 br->range_ += mask;
163 br->range_ |= 1; 149 br->range_ |= 1;
164 br->value_ -= (bit_t)((split + 1) & mask) << pos; 150 br->value_ -= (bit_t)((split + 1) & mask) << pos;
165 return (v ^ mask) - mask; 151 return (v ^ mask) - mask;
166 } 152 }
167 } 153 }
168 154
155 static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* const br, int prob) {
156 // Don't move this declaration! It makes a big speed difference to store
157 // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
158 // alter br->range_ value.
159 range_t range = br->range_;
160 if (br->bits_ < 0) {
161 VP8LoadNewBytes(br);
162 }
163 {
164 const int pos = br->bits_;
165 const range_t split = (range * prob) >> 8;
166 const range_t value = (range_t)(br->value_ >> pos);
167 int bit; // Don't use 'const int bit = (value > split);", it's slower.
168 if (value > split) {
169 range -= split + 1;
170 br->value_ -= (bit_t)(split + 1) << pos;
171 bit = 1;
172 } else {
173 range = split;
174 bit = 0;
175 }
176 if (range <= (range_t)0x7e) {
177 const int shift = kVP8Log2Range[range];
178 range = kVP8NewRange[range];
179 br->bits_ -= shift;
180 }
181 br->range_ = range;
182 return bit;
183 }
184 }
185
169 #ifdef __cplusplus 186 #ifdef __cplusplus
170 } // extern "C" 187 } // extern "C"
171 #endif 188 #endif
172 189
173 #endif // WEBP_UTILS_BIT_READER_INL_H_ 190 #endif // WEBP_UTILS_BIT_READER_INL_H_
OLDNEW
« no previous file with comments | « third_party/libwebp/utils/bit_reader_inl.h ('k') | third_party/libwebp/utils/bit_reader_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698