2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #ifndef VPX_DSP_BITREADER_H_
12 #define VPX_DSP_BITREADER_H_
17 #include "./vpx_config.h"
18 #include "vpx_ports/mem.h"
19 #include "vpx/vp8dx.h"
20 #include "vpx/vpx_integer.h"
21 #include "vpx_dsp/prob.h"
27 typedef size_t BD_VALUE
;
29 #define BD_VALUE_SIZE ((int)sizeof(BD_VALUE) * CHAR_BIT)
31 // This is meant to be a large, positive constant that can still be efficiently
32 // loaded as an immediate (on platforms like ARM, for example).
33 // Even relatively modest values like 100 would work fine.
34 #define LOTS_OF_BITS 0x40000000
37 // Be careful when reordering this struct, it may impact the cache negatively.
41 const uint8_t *buffer_end
;
42 const uint8_t *buffer
;
43 vpx_decrypt_cb decrypt_cb
;
45 uint8_t clear_buffer
[sizeof(BD_VALUE
) + 1];
48 int vpx_reader_init(vpx_reader
*r
,
49 const uint8_t *buffer
,
51 vpx_decrypt_cb decrypt_cb
,
54 void vpx_reader_fill(vpx_reader
*r
);
56 const uint8_t *vpx_reader_find_end(vpx_reader
*r
);
58 static INLINE
int vpx_reader_has_error(vpx_reader
*r
) {
59 // Check if we have reached the end of the buffer.
61 // Variable 'count' stores the number of bits in the 'value' buffer, minus
62 // 8. The top byte is part of the algorithm, and the remainder is buffered
63 // to be shifted into it. So if count == 8, the top 16 bits of 'value' are
64 // occupied, 8 for the algorithm and 8 in the buffer.
66 // When reading a byte from the user's buffer, count is filled with 8 and
67 // one byte is filled into the value buffer. When we reach the end of the
68 // data, count is additionally filled with LOTS_OF_BITS. So when
69 // count == LOTS_OF_BITS - 1, the user's data has been exhausted.
71 // 1 if we have tried to decode bits after the end of stream was encountered.
73 return r
->count
> BD_VALUE_SIZE
&& r
->count
< LOTS_OF_BITS
;
76 static INLINE
int vpx_read(vpx_reader
*r
, int prob
) {
82 unsigned int split
= (r
->range
* prob
+ (256 - prob
)) >> CHAR_BIT
;
90 bigsplit
= (BD_VALUE
)split
<< (BD_VALUE_SIZE
- CHAR_BIT
);
94 if (value
>= bigsplit
) {
95 range
= r
->range
- split
;
96 value
= value
- bigsplit
;
101 register int shift
= vpx_norm
[range
];
113 static INLINE
int vpx_read_bit(vpx_reader
*r
) {
114 return vpx_read(r
, 128); // vpx_prob_half
117 static INLINE
int vpx_read_literal(vpx_reader
*r
, int bits
) {
118 int literal
= 0, bit
;
120 for (bit
= bits
- 1; bit
>= 0; bit
--)
121 literal
|= vpx_read_bit(r
) << bit
;
126 static INLINE
int vpx_read_tree(vpx_reader
*r
, const vpx_tree_index
*tree
,
127 const vpx_prob
*probs
) {
128 vpx_tree_index i
= 0;
130 while ((i
= tree
[i
+ vpx_read(r
, probs
[i
>> 1])]) > 0)
140 #endif // VPX_DSP_BITREADER_H_