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 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.
9  */
10 
11 
12 #ifndef VP8_DECODER_DBOOLHUFF_H_
13 #define VP8_DECODER_DBOOLHUFF_H_
14 
15 #include <stddef.h>
16 #include <limits.h>
17 
18 #include "./vpx_config.h"
19 #include "vpx_ports/mem.h"
20 #include "vpx/vp8dx.h"
21 #include "vpx/vpx_integer.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 typedef size_t VP8_BD_VALUE;
28 
29 #define VP8_BD_VALUE_SIZE ((int)sizeof(VP8_BD_VALUE)*CHAR_BIT)
30 
31 /*This is meant to be a large, positive constant that can still be efficiently
32    loaded as an immediate (on platforms like ARM, for example).
33   Even relatively modest values like 100 would work fine.*/
34 #define VP8_LOTS_OF_BITS (0x40000000)
35 
36 typedef struct
37 {
38     const unsigned char *user_buffer_end;
39     const unsigned char *user_buffer;
40     VP8_BD_VALUE         value;
41     int                  count;
42     unsigned int         range;
43     vpx_decrypt_cb       decrypt_cb;
44     void                *decrypt_state;
45 } BOOL_DECODER;
46 
47 DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
48 
49 int vp8dx_start_decode(BOOL_DECODER *br,
50                        const unsigned char *source,
51                        unsigned int source_sz,
52                        vpx_decrypt_cb decrypt_cb,
53                        void *decrypt_state);
54 
55 void vp8dx_bool_decoder_fill(BOOL_DECODER *br);
56 
57 
vp8dx_decode_bool(BOOL_DECODER * br,int probability)58 static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) {
59     unsigned int bit = 0;
60     VP8_BD_VALUE value;
61     unsigned int split;
62     VP8_BD_VALUE bigsplit;
63     int count;
64     unsigned int range;
65 
66     split = 1 + (((br->range - 1) * probability) >> 8);
67 
68     if(br->count < 0)
69         vp8dx_bool_decoder_fill(br);
70 
71     value = br->value;
72     count = br->count;
73 
74     bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
75 
76     range = split;
77 
78     if (value >= bigsplit)
79     {
80         range = br->range - split;
81         value = value - bigsplit;
82         bit = 1;
83     }
84 
85     {
86         register int shift = vp8_norm[range];
87         range <<= shift;
88         value <<= shift;
89         count -= shift;
90     }
91     br->value = value;
92     br->count = count;
93     br->range = range;
94 
95     return bit;
96 }
97 
vp8_decode_value(BOOL_DECODER * br,int bits)98 static INLINE int vp8_decode_value(BOOL_DECODER *br, int bits)
99 {
100     int z = 0;
101     int bit;
102 
103     for (bit = bits - 1; bit >= 0; bit--)
104     {
105         z |= (vp8dx_decode_bool(br, 0x80) << bit);
106     }
107 
108     return z;
109 }
110 
vp8dx_bool_error(BOOL_DECODER * br)111 static INLINE int vp8dx_bool_error(BOOL_DECODER *br)
112 {
113     /* Check if we have reached the end of the buffer.
114      *
115      * Variable 'count' stores the number of bits in the 'value' buffer, minus
116      * 8. The top byte is part of the algorithm, and the remainder is buffered
117      * to be shifted into it. So if count == 8, the top 16 bits of 'value' are
118      * occupied, 8 for the algorithm and 8 in the buffer.
119      *
120      * When reading a byte from the user's buffer, count is filled with 8 and
121      * one byte is filled into the value buffer. When we reach the end of the
122      * data, count is additionally filled with VP8_LOTS_OF_BITS. So when
123      * count == VP8_LOTS_OF_BITS - 1, the user's data has been exhausted.
124      */
125     if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS))
126     {
127        /* We have tried to decode bits after the end of
128         * stream was encountered.
129         */
130         return 1;
131     }
132 
133     /* No error. */
134     return 0;
135 }
136 
137 #ifdef __cplusplus
138 }  // extern "C"
139 #endif
140 
141 #endif  // VP8_DECODER_DBOOLHUFF_H_
142