Mercurial > audlegacy
comparison Plugins/Input/wma/libffwma/common.c @ 210:12004b385a96 trunk
[svn] Sync with xmms-wma instead of bmp-wma & GThreadify. Does not explode, but does not play either.
author | chainsaw |
---|---|
date | Sat, 19 Nov 2005 14:42:28 -0800 |
parents | b8d4c1faa6d7 |
children | 0bea7509d6ba |
comparison
equal
deleted
inserted
replaced
209:bd8457b077cf | 210:12004b385a96 |
---|---|
1 /* | 1 /* |
2 * Common bit i/o utils | 2 * Common bit i/o utils |
3 * Copyright (c) 2000, 2001 Fabrice Bellard. | 3 * Copyright (c) 2000, 2001 Fabrice Bellard. |
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> | 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> |
5 * Copyright (c) 2004 Roman Bogorodskiy (bmp-wma specific stuff) | |
6 * | 5 * |
7 * This library is free software; you can redistribute it and/or | 6 * This library is free software; you can redistribute it and/or |
8 * modify it under the terms of the GNU Lesser General Public | 7 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | 8 * License as published by the Free Software Foundation; either |
10 * version 2 of the License, or (at your option) any later version. | 9 * version 2 of the License, or (at your option) any later version. |
48 | 47 |
49 void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size) | 48 void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size) |
50 { | 49 { |
51 s->buf = buffer; | 50 s->buf = buffer; |
52 s->buf_end = s->buf + buffer_size; | 51 s->buf_end = s->buf + buffer_size; |
52 #ifdef ALT_BITSTREAM_WRITER | |
53 s->index=0; | |
54 ((uint32_t*)(s->buf))[0]=0; | |
55 // memset(buffer, 0, buffer_size); | |
56 #else | |
53 s->buf_ptr = s->buf; | 57 s->buf_ptr = s->buf; |
54 s->bit_left=32; | 58 s->bit_left=32; |
55 s->bit_buf=0; | 59 s->bit_buf=0; |
56 } | 60 #endif |
61 } | |
62 | |
63 //#ifdef CONFIG_ENCODERS | |
64 #if 1 | |
65 | |
66 /* return the number of bits output */ | |
67 int get_bit_count(PutBitContext *s) | |
68 { | |
69 #ifdef ALT_BITSTREAM_WRITER | |
70 return s->index; | |
71 #else | |
72 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left; | |
73 #endif | |
74 } | |
75 | |
76 void align_put_bits(PutBitContext *s) | |
77 { | |
78 #ifdef ALT_BITSTREAM_WRITER | |
79 put_bits(s,( - s->index) & 7,0); | |
80 #else | |
81 put_bits(s,s->bit_left & 7,0); | |
82 #endif | |
83 } | |
84 | |
85 #endif //CONFIG_ENCODERS | |
86 | |
87 /* pad the end of the output stream with zeros */ | |
88 void flush_put_bits(PutBitContext *s) | |
89 { | |
90 #ifdef ALT_BITSTREAM_WRITER | |
91 align_put_bits(s); | |
92 #else | |
93 s->bit_buf<<= s->bit_left; | |
94 while (s->bit_left < 32) { | |
95 /* XXX: should test end of buffer */ | |
96 *s->buf_ptr++=s->bit_buf >> 24; | |
97 s->bit_buf<<=8; | |
98 s->bit_left+=8; | |
99 } | |
100 s->bit_left=32; | |
101 s->bit_buf=0; | |
102 #endif | |
103 } | |
104 | |
105 #ifdef CONFIG_ENCODERS | |
106 | |
107 void put_string(PutBitContext * pbc, char *s) | |
108 { | |
109 while(*s){ | |
110 put_bits(pbc, 8, *s); | |
111 s++; | |
112 } | |
113 put_bits(pbc, 8, 0); | |
114 } | |
115 | |
116 /* bit input functions */ | |
117 | |
118 #endif //CONFIG_ENCODERS | |
57 | 119 |
58 /** | 120 /** |
59 * init GetBitContext. | 121 * init GetBitContext. |
60 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits | 122 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits |
61 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end | 123 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end |
167 int index; | 229 int index; |
168 index = vlc->table_size; | 230 index = vlc->table_size; |
169 vlc->table_size += size; | 231 vlc->table_size += size; |
170 if (vlc->table_size > vlc->table_allocated) { | 232 if (vlc->table_size > vlc->table_allocated) { |
171 vlc->table_allocated += (1 << vlc->bits); | 233 vlc->table_allocated += (1 << vlc->bits); |
172 vlc->table = realloc(vlc->table, | 234 vlc->table = av_realloc(vlc->table, |
173 sizeof(VLC_TYPE) * 2 * vlc->table_allocated); | 235 sizeof(VLC_TYPE) * 2 * vlc->table_allocated); |
174 if (!vlc->table) | 236 if (!vlc->table) |
175 return -1; | 237 return -1; |
176 } | 238 } |
177 return index; | 239 return index; |
187 uint32_t code; | 249 uint32_t code; |
188 VLC_TYPE (*table)[2]; | 250 VLC_TYPE (*table)[2]; |
189 | 251 |
190 table_size = 1 << table_nb_bits; | 252 table_size = 1 << table_nb_bits; |
191 table_index = alloc_table(vlc, table_size); | 253 table_index = alloc_table(vlc, table_size); |
192 | 254 #ifdef DEBUG_VLC |
255 printf("new table index=%d size=%d code_prefix=%x n=%d\n", | |
256 table_index, table_size, code_prefix, n_prefix); | |
257 #endif | |
193 if (table_index < 0) | 258 if (table_index < 0) |
194 return -1; | 259 return -1; |
195 table = &vlc->table[table_index]; | 260 table = &vlc->table[table_index]; |
196 | 261 |
197 for(i=0;i<table_size;i++) { | 262 for(i=0;i<table_size;i++) { |
204 GET_DATA(n, bits, i, bits_wrap, bits_size); | 269 GET_DATA(n, bits, i, bits_wrap, bits_size); |
205 GET_DATA(code, codes, i, codes_wrap, codes_size); | 270 GET_DATA(code, codes, i, codes_wrap, codes_size); |
206 /* we accept tables with holes */ | 271 /* we accept tables with holes */ |
207 if (n <= 0) | 272 if (n <= 0) |
208 continue; | 273 continue; |
274 #if defined(DEBUG_VLC) && 0 | |
275 printf("i=%d n=%d code=0x%x\n", i, n, code); | |
276 #endif | |
209 /* if code matches the prefix, it is in the table */ | 277 /* if code matches the prefix, it is in the table */ |
210 n -= n_prefix; | 278 n -= n_prefix; |
211 if (n > 0 && (code >> n) == code_prefix) { | 279 if (n > 0 && (code >> n) == code_prefix) { |
212 if (n <= table_nb_bits) { | 280 if (n <= table_nb_bits) { |
213 /* no need to add another table */ | 281 /* no need to add another table */ |
214 j = (code << (table_nb_bits - n)) & (table_size - 1); | 282 j = (code << (table_nb_bits - n)) & (table_size - 1); |
215 nb = 1 << (table_nb_bits - n); | 283 nb = 1 << (table_nb_bits - n); |
216 for(k=0;k<nb;k++) { | 284 for(k=0;k<nb;k++) { |
285 #ifdef DEBUG_VLC | |
286 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n", | |
287 j, i, n); | |
288 #endif | |
217 if (table[j][1] /*bits*/ != 0) { | 289 if (table[j][1] /*bits*/ != 0) { |
218 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n"); | 290 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n"); |
219 av_abort(); | 291 av_abort(); |
220 } | 292 } |
221 table[j][1] = n; //bits | 293 table[j][1] = n; //bits |
223 j++; | 295 j++; |
224 } | 296 } |
225 } else { | 297 } else { |
226 n -= table_nb_bits; | 298 n -= table_nb_bits; |
227 j = (code >> n) & ((1 << table_nb_bits) - 1); | 299 j = (code >> n) & ((1 << table_nb_bits) - 1); |
300 #ifdef DEBUG_VLC | |
301 printf("%4x: n=%d (subtable)\n", | |
302 j, n); | |
303 #endif | |
228 /* compute table size */ | 304 /* compute table size */ |
229 n1 = -table[j][1]; //bits | 305 n1 = -table[j][1]; //bits |
230 if (n > n1) | 306 if (n > n1) |
231 n1 = n; | 307 n1 = n; |
232 table[j][1] = -n1; //bits | 308 table[j][1] = -n1; //bits |
286 { | 362 { |
287 vlc->bits = nb_bits; | 363 vlc->bits = nb_bits; |
288 vlc->table = NULL; | 364 vlc->table = NULL; |
289 vlc->table_allocated = 0; | 365 vlc->table_allocated = 0; |
290 vlc->table_size = 0; | 366 vlc->table_size = 0; |
367 #ifdef DEBUG_VLC | |
368 printf("build table nb_codes=%d\n", nb_codes); | |
369 #endif | |
291 | 370 |
292 if (build_table(vlc, nb_bits, nb_codes, | 371 if (build_table(vlc, nb_bits, nb_codes, |
293 bits, bits_wrap, bits_size, | 372 bits, bits_wrap, bits_size, |
294 codes, codes_wrap, codes_size, | 373 codes, codes_wrap, codes_size, |
295 0, 0) < 0) { | 374 0, 0) < 0) { |
296 free(vlc->table); | 375 av_free(vlc->table); |
297 return -1; | 376 return -1; |
298 } | 377 } |
299 return 0; | 378 return 0; |
300 } | 379 } |
301 | 380 |
302 | 381 |
303 void free_vlc(VLC *vlc) | 382 void free_vlc(VLC *vlc) |
304 { | 383 { |
305 free(vlc->table); | 384 av_free(vlc->table); |
306 } | 385 } |
307 | 386 |
308 int64_t ff_gcd(int64_t a, int64_t b){ | 387 int64_t ff_gcd(int64_t a, int64_t b){ |
309 if(b) return ff_gcd(b, a%b); | 388 if(b) return ff_gcd(b, a%b); |
310 else return a; | 389 else return a; |