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.
16 #include "vpx_ports/config.h"
17 #include "vpx_ports/mem.h"
18 #include "vpx/vpx_integer.h"
20 typedef size_t VP8_BD_VALUE
;
22 # define VP8_BD_VALUE_SIZE ((int)sizeof(VP8_BD_VALUE)*CHAR_BIT)
23 /*This is meant to be a large, positive constant that can still be efficiently
24 loaded as an immediate (on platforms like ARM, for example).
25 Even relatively modest values like 100 would work fine.*/
26 # define VP8_LOTS_OF_BITS (0x40000000)
30 const unsigned char *user_buffer_end
;
31 const unsigned char *user_buffer
;
37 DECLARE_ALIGNED(16, extern const unsigned char, vp8dx_bitreader_norm
[256]);
39 int vp8dx_start_decode(BOOL_DECODER
*br
,
40 const unsigned char *source
,
41 unsigned int source_sz
);
43 void vp8dx_bool_decoder_fill(BOOL_DECODER
*br
);
45 /*The refill loop is used in several places, so define it in a macro to make
46 sure they're all consistent.
47 An inline function would be cleaner, but has a significant penalty, because
48 multiple BOOL_DECODER fields must be modified, and the compiler is not smart
49 enough to eliminate the stores to those fields and the subsequent reloads
50 from them when inlining the function.*/
51 #define VP8DX_BOOL_DECODER_FILL(_count,_value,_bufptr,_bufend) \
55 for(shift = VP8_BD_VALUE_SIZE - 8 - ((_count) + 8); shift >= 0; ) \
57 if((_bufptr) >= (_bufend)) { \
58 (_count) = VP8_LOTS_OF_BITS; \
62 (_value) |= (VP8_BD_VALUE)*(_bufptr)++ << shift; \
69 static int vp8dx_decode_bool(BOOL_DECODER
*br
, int probability
) {
73 VP8_BD_VALUE bigsplit
;
81 split
= 1 + (((range
- 1) * probability
) >> 8);
82 bigsplit
= (VP8_BD_VALUE
)split
<< (VP8_BD_VALUE_SIZE
- 8);
86 if (value
>= bigsplit
)
88 range
= br
->range
- split
;
89 value
= value
- bigsplit
;
94 register unsigned int shift
= vp8dx_bitreader_norm
[range
];
103 vp8dx_bool_decoder_fill(br
);
107 static int vp8_decode_value(BOOL_DECODER
*br
, int bits
)
112 for (bit
= bits
- 1; bit
>= 0; bit
--)
114 z
|= (vp8dx_decode_bool(br
, 0x80) << bit
);
120 static int vp8dx_bool_error(BOOL_DECODER
*br
)
122 /* Check if we have reached the end of the buffer.
124 * Variable 'count' stores the number of bits in the 'value' buffer,
125 * minus 8. So if count == 8, there are 16 bits available to be read.
126 * Normally, count is filled with 8 and one byte is filled into the
127 * value buffer. When we reach the end of the buffer, count is instead
128 * filled with VP8_LOTS_OF_BITS, 8 of which represent the last 8 real
129 * bits from the bitstream. So the last bit in the bitstream will be
130 * represented by count == VP8_LOTS_OF_BITS - 16.
132 if ((br
->count
> VP8_BD_VALUE_SIZE
)
133 && (br
->count
<= VP8_LOTS_OF_BITS
- 16))
135 /* We have tried to decode bits after the end of
136 * stream was encountered.