1 // Copyright 2014 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
10 // Specific inlined methods for boolean decoder [VP8GetBit() ...]
11 // This file should be included by the .c sources that actually need to call
14 // Author: Skal (pascal.massimino@gmail.com)
16 #ifndef WEBP_UTILS_BIT_READER_INL_H_
17 #define WEBP_UTILS_BIT_READER_INL_H_
20 #include "../webp/config.h"
23 #ifdef WEBP_FORCE_ALIGNED
24 #include <string.h> // memcpy
27 #include "../dsp/dsp.h"
28 #include "./bit_reader.h"
29 #include "./endian_inl.h"
35 //------------------------------------------------------------------------------
36 // Derived type lbit_t = natural type for memory I/O
39 typedef uint64_t lbit_t
;
41 typedef uint32_t lbit_t
;
43 typedef uint16_t lbit_t
;
45 typedef uint8_t lbit_t
;
48 extern const uint8_t kVP8Log2Range
[128];
49 extern const range_t kVP8NewRange
[128];
51 // special case for the tail byte-reading
52 void VP8LoadFinalBytes(VP8BitReader
* const br
);
54 //------------------------------------------------------------------------------
55 // Inlined critical functions
57 // makes sure br->value_ has at least BITS bits worth of data
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!)
64 #if defined(WEBP_FORCE_ALIGNED)
66 memcpy(&in_bits
, br
->buf_
, sizeof(in_bits
));
67 #elif defined(WEBP_USE_MIPS32)
68 // This is needed because of un-aligned read.
70 lbit_t
* p_buf_
= (lbit_t
*)br
->buf_
;
75 "ulw %[in_bits], 0(%[p_buf_]) \n\t"
77 : [in_bits
]"=r"(in_bits
)
82 const lbit_t in_bits
= *(const lbit_t
*)br
->buf_
;
84 br
->buf_
+= BITS
>> 3;
85 #if !defined(WORDS_BIGENDIAN)
87 bits
= BSwap64(in_bits
);
90 bits
= BSwap32(in_bits
);
93 bits
= BSwap16(in_bits
);
95 bits
= (bit_t
)in_bits
;
97 #else // WORDS_BIGENDIAN
98 bits
= (bit_t
)in_bits
;
99 if (BITS
!= 8 * sizeof(bit_t
)) bits
>>= (8 * sizeof(bit_t
) - BITS
);
101 br
->value_
= bits
| (br
->value_
<< BITS
);
104 VP8LoadFinalBytes(br
); // no need to be inlined
108 // Read a bit with proba 'prob'. Speed-critical function!
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_
;
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;
125 br
->value_
-= (bit_t
)(split
+ 1) << pos
;
129 #else // faster version on x86
130 int bit
; // Don't use 'const int bit = (value > split);", it's slower.
133 br
->value_
-= (bit_t
)(split
+ 1) << pos
;
140 if (range
<= (range_t
)0x7e) {
141 const int shift
= kVP8Log2Range
[range
];
142 range
= kVP8NewRange
[range
];
150 // simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)
151 static WEBP_INLINE
int VP8GetSigned(VP8BitReader
* const br
, int v
) {
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
163 br
->value_
-= (bit_t
)((split
+ 1) & mask
) << pos
;
164 return (v
^ mask
) - mask
;
172 #endif // WEBP_UTILS_BIT_READER_INL_H_