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) \
54 int shift = VP8_BD_VALUE_SIZE - 8 - ((_count) + 8); \
56 size_t bits_left = ((_bufend)-(_bufptr))*CHAR_BIT; \
58 x = shift + CHAR_BIT - bits_left; \
62 (_count) += VP8_LOTS_OF_BITS; \
64 if(!bits_left) break; \
66 while(shift >= loop_end) \
68 (_count) += CHAR_BIT; \
69 (_value) |= (VP8_BD_VALUE)*(_bufptr)++ << shift; \
76 static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) {
80 VP8_BD_VALUE bigsplit;
84 split = 1 + (((br->range - 1) * probability) >> 8);
87 vp8dx_bool_decoder_fill(br);
92 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
96 if (value >= bigsplit)
98 range = br->range - split;
99 value = value - bigsplit;
104 register unsigned int shift = vp8dx_bitreader_norm[range];
116 static int vp8_decode_value(BOOL_DECODER *br, int bits)
121 for (bit = bits - 1; bit >= 0; bit--)
123 z |= (vp8dx_decode_bool(br, 0x80) << bit);
129 static int vp8dx_bool_error(BOOL_DECODER *br)
131 /* Check if we have reached the end of the buffer.
133 * Variable 'count' stores the number of bits in the 'value' buffer, minus
134 * 8. The top byte is part of the algorithm, and the remainder is buffered
135 * to be shifted into it. So if count == 8, the top 16 bits of 'value' are
136 * occupied, 8 for the algorithm and 8 in the buffer.
138 * When reading a byte from the user's buffer, count is filled with 8 and
139 * one byte is filled into the value buffer. When we reach the end of the
140 * data, count is additionally filled with VP8_LOTS_OF_BITS. So when
141 * count == VP8_LOTS_OF_BITS - 1, the user's data has been exhausted.
143 if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS))
145 /* We have tried to decode bits after the end of
146 * stream was encountered.