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;
35 __declspec(align(16)) extern const unsigned char vp8_norm[256];
37 extern const unsigned char vp8_norm[256] __attribute__((aligned(16)));
40 int vp8dx_start_decode(BOOL_DECODER *br,
41 const unsigned char *source,
42 unsigned int source_sz);
44 void vp8dx_bool_decoder_fill(BOOL_DECODER *br);
46 /*The refill loop is used in several places, so define it in a macro to make
47 sure they're all consistent.
48 An inline function would be cleaner, but has a significant penalty, because
49 multiple BOOL_DECODER fields must be modified, and the compiler is not smart
50 enough to eliminate the stores to those fields and the subsequent reloads
51 from them when inlining the function.*/
52 #define VP8DX_BOOL_DECODER_FILL(_count,_value,_bufptr,_bufend) \
55 int shift = VP8_BD_VALUE_SIZE - 8 - ((_count) + 8); \
57 size_t bits_left = ((_bufend)-(_bufptr))*CHAR_BIT; \
59 x = shift + CHAR_BIT - bits_left; \
63 (_count) += VP8_LOTS_OF_BITS; \
65 if(!bits_left) break; \
67 while(shift >= loop_end) \
69 (_count) += CHAR_BIT; \
70 (_value) |= (VP8_BD_VALUE)*(_bufptr)++ << shift; \
77 static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) {
81 VP8_BD_VALUE bigsplit;
85 split = 1 + (((br->range - 1) * probability) >> 8);
88 vp8dx_bool_decoder_fill(br);
93 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
97 if (value >= bigsplit)
99 range = br->range - split;
100 value = value - bigsplit;
105 register unsigned int shift = vp8_norm[range];
117 static G_GNUC_UNUSED int vp8_decode_value(BOOL_DECODER *br, int bits)
122 for (bit = bits - 1; bit >= 0; bit--)
124 z |= (vp8dx_decode_bool(br, 0x80) << bit);
130 static G_GNUC_UNUSED int vp8dx_bool_error(BOOL_DECODER *br)
132 /* Check if we have reached the end of the buffer.
134 * Variable 'count' stores the number of bits in the 'value' buffer, minus
135 * 8. The top byte is part of the algorithm, and the remainder is buffered
136 * to be shifted into it. So if count == 8, the top 16 bits of 'value' are
137 * occupied, 8 for the algorithm and 8 in the buffer.
139 * When reading a byte from the user's buffer, count is filled with 8 and
140 * one byte is filled into the value buffer. When we reach the end of the
141 * data, count is additionally filled with VP8_LOTS_OF_BITS. So when
142 * count == VP8_LOTS_OF_BITS - 1, the user's data has been exhausted.
144 if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS))
146 /* We have tried to decode bits after the end of
147 * stream was encountered.