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