diff options
Diffstat (limited to 'apps/codecs/libwma/bitstream.c')
-rw-r--r-- | apps/codecs/libwma/bitstream.c | 274 |
1 files changed, 274 insertions, 0 deletions
diff --git a/apps/codecs/libwma/bitstream.c b/apps/codecs/libwma/bitstream.c new file mode 100644 index 0000000000..12cf0518ee --- /dev/null +++ b/apps/codecs/libwma/bitstream.c | |||
@@ -0,0 +1,274 @@ | |||
1 | /* | ||
2 | * Common bit i/o utils | ||
3 | * Copyright (c) 2000, 2001 Fabrice Bellard. | ||
4 | * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> | ||
5 | * | ||
6 | * This file is part of FFmpeg. | ||
7 | * | ||
8 | * FFmpeg is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * FFmpeg is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with FFmpeg; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | * | ||
22 | * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at> | ||
23 | */ | ||
24 | |||
25 | /** | ||
26 | * @file bitstream.c | ||
27 | * bitstream api. | ||
28 | */ | ||
29 | |||
30 | #include "bitstream.h" | ||
31 | |||
32 | /** | ||
33 | * Same as av_mallocz_static(), but does a realloc. | ||
34 | * | ||
35 | * @param[in] ptr The block of memory to reallocate. | ||
36 | * @param[in] size The requested size. | ||
37 | * @return Block of memory of requested size. | ||
38 | * @deprecated. Code which uses ff_realloc_static is broken/missdesigned | ||
39 | * and should correctly use static arrays | ||
40 | */ | ||
41 | attribute_deprecated void *ff_realloc_static(void *ptr, unsigned int size); | ||
42 | |||
43 | |||
44 | const uint8_t ff_sqrt_tab[128]={ | ||
45 | 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, | ||
46 | 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
47 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, | ||
48 | 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11 | ||
49 | }; | ||
50 | |||
51 | const uint8_t ff_log2_tab[256]={ | ||
52 | 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, | ||
53 | 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, | ||
54 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, | ||
55 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, | ||
56 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, | ||
57 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, | ||
58 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, | ||
59 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 | ||
60 | }; | ||
61 | |||
62 | |||
63 | void align_put_bits(PutBitContext *s) | ||
64 | { | ||
65 | #ifdef ALT_BITSTREAM_WRITER | ||
66 | put_bits(s,( - s->index) & 7,0); | ||
67 | #else | ||
68 | put_bits(s,s->bit_left & 7,0); | ||
69 | #endif | ||
70 | } | ||
71 | |||
72 | void ff_put_string(PutBitContext * pbc, char *s, int put_zero) | ||
73 | { | ||
74 | while(*s){ | ||
75 | put_bits(pbc, 8, *s); | ||
76 | s++; | ||
77 | } | ||
78 | if(put_zero) | ||
79 | put_bits(pbc, 8, 0); | ||
80 | } | ||
81 | |||
82 | /* VLC decoding */ | ||
83 | |||
84 | //#define DEBUG_VLC | ||
85 | |||
86 | #define GET_DATA(v, table, i, wrap, size) \ | ||
87 | {\ | ||
88 | const uint8_t *ptr = (const uint8_t *)table + i * wrap;\ | ||
89 | switch(size) {\ | ||
90 | case 1:\ | ||
91 | v = *(const uint8_t *)ptr;\ | ||
92 | break;\ | ||
93 | case 2:\ | ||
94 | v = *(const uint16_t *)ptr;\ | ||
95 | break;\ | ||
96 | default:\ | ||
97 | v = *(const uint32_t *)ptr;\ | ||
98 | break;\ | ||
99 | }\ | ||
100 | } | ||
101 | |||
102 | |||
103 | static int alloc_table(VLC *vlc, int size) | ||
104 | { | ||
105 | int index; | ||
106 | index = vlc->table_size; | ||
107 | vlc->table_size += size; | ||
108 | if (vlc->table_size > vlc->table_allocated) { | ||
109 | vlc->table_allocated += (1 << vlc->bits); | ||
110 | if (!vlc->table) | ||
111 | return -1; | ||
112 | } | ||
113 | return index; | ||
114 | } | ||
115 | |||
116 | static int build_table(VLC *vlc, int table_nb_bits, | ||
117 | int nb_codes, | ||
118 | const void *bits, int bits_wrap, int bits_size, | ||
119 | const void *codes, int codes_wrap, int codes_size, | ||
120 | uint32_t code_prefix, int n_prefix) | ||
121 | { | ||
122 | int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2; | ||
123 | uint32_t code; | ||
124 | int flags = 0; | ||
125 | VLC_TYPE (*table)[2]; | ||
126 | |||
127 | table_size = 1 << table_nb_bits; | ||
128 | table_index = alloc_table(vlc, table_size); | ||
129 | #ifdef DEBUG_VLC | ||
130 | printf("new table index=%d size=%d code_prefix=%x n=%d\n", | ||
131 | table_index, table_size, code_prefix, n_prefix); | ||
132 | #endif | ||
133 | if (table_index < 0) | ||
134 | return -1; | ||
135 | table = &vlc->table[table_index]; | ||
136 | |||
137 | for(i=0;i<table_size;i++) { | ||
138 | table[i][1] = 0; //bits | ||
139 | table[i][0] = -1; //codes | ||
140 | } | ||
141 | |||
142 | /* first pass: map codes and compute auxillary table sizes */ | ||
143 | for(i=0;i<nb_codes;i++) { | ||
144 | GET_DATA(n, bits, i, bits_wrap, bits_size); | ||
145 | GET_DATA(code, codes, i, codes_wrap, codes_size); | ||
146 | /* we accept tables with holes */ | ||
147 | if (n <= 0) | ||
148 | continue; | ||
149 | #if defined(DEBUG_VLC) && 0 | ||
150 | printf("i=%d n=%d code=0x%x\n", i, n, code); | ||
151 | #endif | ||
152 | /* if code matches the prefix, it is in the table */ | ||
153 | n -= n_prefix; | ||
154 | if(flags & INIT_VLC_LE) | ||
155 | code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1); | ||
156 | else | ||
157 | code_prefix2= code >> n; | ||
158 | if (n > 0 && code_prefix2 == code_prefix) { | ||
159 | if (n <= table_nb_bits) { | ||
160 | /* no need to add another table */ | ||
161 | j = (code << (table_nb_bits - n)) & (table_size - 1); | ||
162 | nb = 1 << (table_nb_bits - n); | ||
163 | for(k=0;k<nb;k++) { | ||
164 | if(flags & INIT_VLC_LE) | ||
165 | j = (code >> n_prefix) + (k<<n); | ||
166 | #ifdef DEBUG_VLC | ||
167 | av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n", | ||
168 | j, i, n); | ||
169 | #endif | ||
170 | if (table[j][1] /*bits*/ != 0) { | ||
171 | // av_log(NULL, AV_LOG_ERROR, "incorrect codes\n"); | ||
172 | return -1; | ||
173 | } | ||
174 | table[j][1] = n; //bits | ||
175 | table[j][0] = i; //code | ||
176 | j++; | ||
177 | } | ||
178 | } else { | ||
179 | n -= table_nb_bits; | ||
180 | j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1); | ||
181 | #ifdef DEBUG_VLC | ||
182 | printf("%4x: n=%d (subtable)\n", | ||
183 | j, n); | ||
184 | #endif | ||
185 | /* compute table size */ | ||
186 | n1 = -table[j][1]; //bits | ||
187 | if (n > n1) | ||
188 | n1 = n; | ||
189 | table[j][1] = -n1; //bits | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | |||
194 | /* second pass : fill auxillary tables recursively */ | ||
195 | for(i=0;i<table_size;i++) { | ||
196 | n = table[i][1]; //bits | ||
197 | if (n < 0) { | ||
198 | n = -n; | ||
199 | if (n > table_nb_bits) { | ||
200 | n = table_nb_bits; | ||
201 | table[i][1] = -n; //bits | ||
202 | } | ||
203 | index = build_table(vlc, n, nb_codes, | ||
204 | bits, bits_wrap, bits_size, | ||
205 | codes, codes_wrap, codes_size, | ||
206 | (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i), | ||
207 | n_prefix + table_nb_bits); | ||
208 | if (index < 0) | ||
209 | return -1; | ||
210 | /* note: realloc has been done, so reload tables */ | ||
211 | table = &vlc->table[table_index]; | ||
212 | table[i][0] = index; //code | ||
213 | } | ||
214 | } | ||
215 | return table_index; | ||
216 | } | ||
217 | |||
218 | |||
219 | /* Build VLC decoding tables suitable for use with get_vlc(). | ||
220 | |||
221 | 'nb_bits' set thee decoding table size (2^nb_bits) entries. The | ||
222 | bigger it is, the faster is the decoding. But it should not be too | ||
223 | big to save memory and L1 cache. '9' is a good compromise. | ||
224 | |||
225 | 'nb_codes' : number of vlcs codes | ||
226 | |||
227 | 'bits' : table which gives the size (in bits) of each vlc code. | ||
228 | |||
229 | 'codes' : table which gives the bit pattern of of each vlc code. | ||
230 | |||
231 | 'xxx_wrap' : give the number of bytes between each entry of the | ||
232 | 'bits' or 'codes' tables. | ||
233 | |||
234 | 'xxx_size' : gives the number of bytes of each entry of the 'bits' | ||
235 | or 'codes' tables. | ||
236 | |||
237 | 'wrap' and 'size' allows to use any memory configuration and types | ||
238 | (byte/word/long) to store the 'bits' and 'codes' tables. | ||
239 | |||
240 | 'use_static' should be set to 1 for tables, which should be freed | ||
241 | with av_free_static(), 0 if free_vlc() will be used. | ||
242 | */ | ||
243 | int init_vlc(VLC *vlc, int nb_bits, int nb_codes, | ||
244 | const void *bits, int bits_wrap, int bits_size, | ||
245 | const void *codes, int codes_wrap, int codes_size, | ||
246 | int flags) | ||
247 | { | ||
248 | |||
249 | vlc->bits = nb_bits; | ||
250 | vlc->table_size = 0; | ||
251 | |||
252 | #ifdef DEBUG_VLC | ||
253 | printf("build table nb_codes=%d\n", nb_codes); | ||
254 | #endif | ||
255 | |||
256 | if (build_table(vlc, nb_bits, nb_codes, | ||
257 | bits, bits_wrap, bits_size, | ||
258 | codes, codes_wrap, codes_size, | ||
259 | 0, 0) < 0) { | ||
260 | //av_free(vlc->table); | ||
261 | return -1; | ||
262 | } | ||
263 | /* return flags to block gcc warning while allowing us to keep | ||
264 | * consistent with ffmpeg's function parameters | ||
265 | */ | ||
266 | return flags; | ||
267 | } | ||
268 | |||
269 | /* | ||
270 | void free_vlc(VLC *vlc) | ||
271 | { | ||
272 | av_free(vlc->table); | ||
273 | } | ||
274 | */ | ||