1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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.
8  */
9 
10 
11 #ifndef DBOOLHUFF_H
12 #define DBOOLHUFF_H
13 #include <stddef.h>
14 #include <limits.h>
15 #include <glib.h>
16 
17 typedef size_t VP8_BD_VALUE;
18 
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)
24 
25 typedef struct
26 {
27     const unsigned char *user_buffer_end;
28     const unsigned char *user_buffer;
29     VP8_BD_VALUE         value;
30     int                  count;
31     unsigned int         range;
32 } BOOL_DECODER;
33 
34 #ifdef _MSC_VER
35 __declspec(align(16)) extern const unsigned char vp8_norm[256];
36 #else
37 extern const unsigned char vp8_norm[256] __attribute__((aligned(16)));
38 #endif
39 
40 int vp8dx_start_decode(BOOL_DECODER *br,
41                        const unsigned char *source,
42                        unsigned int source_sz);
43 
44 void vp8dx_bool_decoder_fill(BOOL_DECODER *br);
45 
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) \
53     do \
54     { \
55         int shift = VP8_BD_VALUE_SIZE - 8 - ((_count) + 8); \
56         int loop_end, x; \
57         size_t bits_left = ((_bufend)-(_bufptr))*CHAR_BIT; \
58         \
59         x = shift + CHAR_BIT - bits_left; \
60         loop_end = 0; \
61         if(x >= 0) \
62         { \
63             (_count) += VP8_LOTS_OF_BITS; \
64             loop_end = x; \
65             if(!bits_left) break; \
66         } \
67         while(shift >= loop_end) \
68         { \
69             (_count) += CHAR_BIT; \
70             (_value) |= (VP8_BD_VALUE)*(_bufptr)++ << shift; \
71             shift -= CHAR_BIT; \
72         } \
73     } \
74     while(0) \
75 
76 
vp8dx_decode_bool(BOOL_DECODER * br,int probability)77 static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) {
78     unsigned int bit = 0;
79     VP8_BD_VALUE value;
80     unsigned int split;
81     VP8_BD_VALUE bigsplit;
82     int count;
83     unsigned int range;
84 
85     split = 1 + (((br->range - 1) * probability) >> 8);
86 
87     if(br->count < 0)
88         vp8dx_bool_decoder_fill(br);
89 
90     value = br->value;
91     count = br->count;
92 
93     bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
94 
95     range = split;
96 
97     if (value >= bigsplit)
98     {
99         range = br->range - split;
100         value = value - bigsplit;
101         bit = 1;
102     }
103 
104     {
105         register unsigned int shift = vp8_norm[range];
106         range <<= shift;
107         value <<= shift;
108         count -= shift;
109     }
110     br->value = value;
111     br->count = count;
112     br->range = range;
113 
114     return bit;
115 }
116 
vp8_decode_value(BOOL_DECODER * br,int bits)117 static G_GNUC_UNUSED int vp8_decode_value(BOOL_DECODER *br, int bits)
118 {
119     int z = 0;
120     int bit;
121 
122     for (bit = bits - 1; bit >= 0; bit--)
123     {
124         z |= (vp8dx_decode_bool(br, 0x80) << bit);
125     }
126 
127     return z;
128 }
129 
vp8dx_bool_error(BOOL_DECODER * br)130 static G_GNUC_UNUSED int vp8dx_bool_error(BOOL_DECODER *br)
131 {
132     /* Check if we have reached the end of the buffer.
133      *
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.
138      *
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.
143      */
144     if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS))
145     {
146        /* We have tried to decode bits after the end of
147         * stream was encountered.
148         */
149         return 1;
150     }
151 
152     /* No error. */
153     return 0;
154 }
155 #endif
156