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