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 dboolhuff.LICENSE file in this directory.
6 * See the libvpx original distribution for more information,
7 * including patent information, and author information.
17 typedef size_t VP8_BD_VALUE;
19 # define VP8_BD_VALUE_SIZE ((int)sizeof(VP8_BD_VALUE)*CHAR_BIT)
20 /*This is meant to be a large, positive constant that can still be efficiently
21 loaded as an immediate (on platforms like ARM, for example).
22 Even relatively modest values like 100 would work fine.*/
23 # define VP8_LOTS_OF_BITS (0x40000000)
27 const unsigned char *user_buffer_end;
28 const unsigned char *user_buffer;
34 extern const unsigned char vp8_norm[256] __attribute__((aligned(16)));
36 int vp8dx_start_decode(BOOL_DECODER *br,
37 const unsigned char *source,
38 unsigned int source_sz);
40 void vp8dx_bool_decoder_fill(BOOL_DECODER *br);
42 /*The refill loop is used in several places, so define it in a macro to make
43 sure they're all consistent.
44 An inline function would be cleaner, but has a significant penalty, because
45 multiple BOOL_DECODER fields must be modified, and the compiler is not smart
46 enough to eliminate the stores to those fields and the subsequent reloads
47 from them when inlining the function.*/
48 #define VP8DX_BOOL_DECODER_FILL(_count,_value,_bufptr,_bufend) \
51 int shift = VP8_BD_VALUE_SIZE - 8 - ((_count) + 8); \
53 size_t bits_left = ((_bufend)-(_bufptr))*CHAR_BIT; \
55 x = shift + CHAR_BIT - bits_left; \
59 (_count) += VP8_LOTS_OF_BITS; \
61 if(!bits_left) break; \
63 while(shift >= loop_end) \
65 (_count) += CHAR_BIT; \
66 (_value) |= (VP8_BD_VALUE)*(_bufptr)++ << shift; \
73 static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) {
77 VP8_BD_VALUE bigsplit;
81 split = 1 + (((br->range - 1) * probability) >> 8);
84 vp8dx_bool_decoder_fill(br);
89 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
93 if (value >= bigsplit)
95 range = br->range - split;
96 value = value - bigsplit;
101 register unsigned int shift = vp8_norm[range];
113 static G_GNUC_UNUSED int vp8_decode_value(BOOL_DECODER *br, int bits)
118 for (bit = bits - 1; bit >= 0; bit--)
120 z |= (vp8dx_decode_bool(br, 0x80) << bit);
126 static G_GNUC_UNUSED int vp8dx_bool_error(BOOL_DECODER *br)
128 /* Check if we have reached the end of the buffer.
130 * Variable 'count' stores the number of bits in the 'value' buffer, minus
131 * 8. The top byte is part of the algorithm, and the remainder is buffered
132 * to be shifted into it. So if count == 8, the top 16 bits of 'value' are
133 * occupied, 8 for the algorithm and 8 in the buffer.
135 * When reading a byte from the user's buffer, count is filled with 8 and
136 * one byte is filled into the value buffer. When we reach the end of the
137 * data, count is additionally filled with VP8_LOTS_OF_BITS. So when
138 * count == VP8_LOTS_OF_BITS - 1, the user's data has been exhausted.
140 if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS))
142 /* We have tried to decode bits after the end of
143 * stream was encountered.