1 // Copyright 2014 Google Inc. All Rights Reserved.
2 //
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
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // Specific inlined methods for boolean decoder [VP8GetBit() ...]
11 // This file should be included by the .c sources that actually need to call
12 // these methods.
13 //
14 // Author: Skal (pascal.massimino@gmail.com)
15 
16 #ifndef WEBP_UTILS_BIT_READER_INL_H_
17 #define WEBP_UTILS_BIT_READER_INL_H_
18 
19 #ifdef HAVE_CONFIG_H
20 #include "../webp/config.h"
21 #endif
22 
23 #ifdef WEBP_FORCE_ALIGNED
24 #include <string.h>  // memcpy
25 #endif
26 
27 #include "../dsp/dsp.h"
28 #include "./bit_reader.h"
29 #include "./endian_inl.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 //------------------------------------------------------------------------------
36 // Derived type lbit_t = natural type for memory I/O
37 
38 #if   (BITS > 32)
39 typedef uint64_t lbit_t;
40 #elif (BITS > 16)
41 typedef uint32_t lbit_t;
42 #elif (BITS >  8)
43 typedef uint16_t lbit_t;
44 #else
45 typedef uint8_t lbit_t;
46 #endif
47 
48 extern const uint8_t kVP8Log2Range[128];
49 extern const range_t kVP8NewRange[128];
50 
51 // special case for the tail byte-reading
52 void VP8LoadFinalBytes(VP8BitReader* const br);
53 
54 //------------------------------------------------------------------------------
55 // Inlined critical functions
56 
57 // makes sure br->value_ has at least BITS bits worth of data
VP8LoadNewBytes(VP8BitReader * const br)58 static WEBP_INLINE void VP8LoadNewBytes(VP8BitReader* const br) {
59   assert(br != NULL && br->buf_ != NULL);
60   // Read 'BITS' bits at a time if possible.
61   if (br->buf_ + sizeof(lbit_t) <= br->buf_end_) {
62     // convert memory type to register type (with some zero'ing!)
63     bit_t bits;
64 #if defined(WEBP_FORCE_ALIGNED)
65     lbit_t in_bits;
66     memcpy(&in_bits, br->buf_, sizeof(in_bits));
67 #elif defined(WEBP_USE_MIPS32)
68     // This is needed because of un-aligned read.
69     lbit_t in_bits;
70     lbit_t* p_buf_ = (lbit_t*)br->buf_;
71     __asm__ volatile(
72       ".set   push                             \n\t"
73       ".set   at                               \n\t"
74       ".set   macro                            \n\t"
75       "ulw    %[in_bits], 0(%[p_buf_])         \n\t"
76       ".set   pop                              \n\t"
77       : [in_bits]"=r"(in_bits)
78       : [p_buf_]"r"(p_buf_)
79       : "memory", "at"
80     );
81 #else
82     const lbit_t in_bits = *(const lbit_t*)br->buf_;
83 #endif
84     br->buf_ += BITS >> 3;
85 #if !defined(WORDS_BIGENDIAN)
86 #if (BITS > 32)
87     bits = BSwap64(in_bits);
88     bits >>= 64 - BITS;
89 #elif (BITS >= 24)
90     bits = BSwap32(in_bits);
91     bits >>= (32 - BITS);
92 #elif (BITS == 16)
93     bits = BSwap16(in_bits);
94 #else   // BITS == 8
95     bits = (bit_t)in_bits;
96 #endif  // BITS > 32
97 #else    // WORDS_BIGENDIAN
98     bits = (bit_t)in_bits;
99     if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS);
100 #endif
101     br->value_ = bits | (br->value_ << BITS);
102     br->bits_ += BITS;
103   } else {
104     VP8LoadFinalBytes(br);    // no need to be inlined
105   }
106 }
107 
108 // Read a bit with proba 'prob'. Speed-critical function!
VP8GetBit(VP8BitReader * const br,int prob)109 static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) {
110   // Don't move this declaration! It makes a big speed difference to store
111   // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
112   // alter br->range_ value.
113   range_t range = br->range_;
114   if (br->bits_ < 0) {
115     VP8LoadNewBytes(br);
116   }
117   {
118     const int pos = br->bits_;
119     const range_t split = (range * prob) >> 8;
120     const range_t value = (range_t)(br->value_ >> pos);
121 #if defined(__arm__) || defined(_M_ARM)      // ARM-specific
122     const int bit = ((int)(split - value) >> 31) & 1;
123     if (value > split) {
124       range -= split + 1;
125       br->value_ -= (bit_t)(split + 1) << pos;
126     } else {
127       range = split;
128     }
129 #else  // faster version on x86
130     int bit;  // Don't use 'const int bit = (value > split);", it's slower.
131     if (value > split) {
132       range -= split + 1;
133       br->value_ -= (bit_t)(split + 1) << pos;
134       bit = 1;
135     } else {
136       range = split;
137       bit = 0;
138     }
139 #endif
140     if (range <= (range_t)0x7e) {
141       const int shift = kVP8Log2Range[range];
142       range = kVP8NewRange[range];
143       br->bits_ -= shift;
144     }
145     br->range_ = range;
146     return bit;
147   }
148 }
149 
150 // simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)
VP8GetSigned(VP8BitReader * const br,int v)151 static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) {
152   if (br->bits_ < 0) {
153     VP8LoadNewBytes(br);
154   }
155   {
156     const int pos = br->bits_;
157     const range_t split = br->range_ >> 1;
158     const range_t value = (range_t)(br->value_ >> pos);
159     const int32_t mask = (int32_t)(split - value) >> 31;  // -1 or 0
160     br->bits_ -= 1;
161     br->range_ += mask;
162     br->range_ |= 1;
163     br->value_ -= (bit_t)((split + 1) & mask) << pos;
164     return (v ^ mask) - mask;
165   }
166 }
167 
168 #ifdef __cplusplus
169 }    // extern "C"
170 #endif
171 
172 #endif   // WEBP_UTILS_BIT_READER_INL_H_
173