summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/codecs/libwma/common.c161
-rw-r--r--apps/codecs/libwma/common.h61
-rw-r--r--apps/codecs/libwma/wmadec.h10
-rw-r--r--apps/codecs/libwma/wmadeci.c211
4 files changed, 222 insertions, 221 deletions
diff --git a/apps/codecs/libwma/common.c b/apps/codecs/libwma/common.c
index 619ce47197..b4577a4c74 100644
--- a/apps/codecs/libwma/common.c
+++ b/apps/codecs/libwma/common.c
@@ -105,3 +105,164 @@ int check_marker(GetBitContext *s, const char *msg)
105 return bit; 105 return bit;
106} 106}
107 107
108/* VLC decoding */
109
110//#define DEBUG_VLC
111
112#define GET_DATA(v, table, i, wrap, size) \
113{\
114 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
115 switch(size) {\
116 case 1:\
117 v = *(const uint8_t *)ptr;\
118 break;\
119 case 2:\
120 v = *(const uint16_t *)ptr;\
121 break;\
122 default:\
123 v = *(const uint32_t *)ptr;\
124 break;\
125 }\
126}
127
128static int alloc_table(VLC *vlc, int size)
129{
130 int index;
131 index = vlc->table_size;
132 vlc->table_size += size;
133 if (vlc->table_size > vlc->table_allocated) {
134 vlc->table_allocated += (1 << vlc->bits);
135 if (!vlc->table)
136 return -1;
137 }
138 return index;
139}
140
141static int build_table(VLC *vlc, int table_nb_bits,
142 int nb_codes,
143 const void *bits, int bits_wrap, int bits_size,
144 const void *codes, int codes_wrap, int codes_size,
145 uint32_t code_prefix, int n_prefix)
146{
147 int i, j, k, n, table_size, table_index, nb, n1, index;
148 uint32_t code;
149 VLC_TYPE (*table)[2];
150
151 table_size = 1 << table_nb_bits;
152 table_index = alloc_table(vlc, table_size);
153 if (table_index < 0)
154 return -1;
155 table = &vlc->table[table_index];
156
157 for(i=0;i<table_size;i++)
158 {
159 table[i][1] = 0; //bits
160 table[i][0] = -1; //codes
161 }
162
163 /* first pass: map codes and compute auxillary table sizes */
164 for(i=0;i<nb_codes;i++)
165 {
166 GET_DATA(n, bits, i, bits_wrap, bits_size);
167 GET_DATA(code, codes, i, codes_wrap, codes_size);
168 /* we accept tables with holes */
169 if (n <= 0)
170 continue;
171 /* if code matches the prefix, it is in the table */
172 n -= n_prefix;
173 if (n > 0 && (code >> n) == code_prefix)
174 {
175 if (n <= table_nb_bits)
176 {
177 /* no need to add another table */
178 j = (code << (table_nb_bits - n)) & (table_size - 1);
179 nb = 1 << (table_nb_bits - n);
180 for(k=0;k<nb;k++)
181 {
182 if (table[j][1] /*bits*/ != 0)
183 {
184 // PJJ exit(-1);
185 }
186 table[j][1] = n; //bits
187 table[j][0] = i; //code
188 j++;
189 }
190 }
191 else
192 {
193 n -= table_nb_bits;
194 j = (code >> n) & ((1 << table_nb_bits) - 1);
195 /* compute table size */
196 n1 = -table[j][1]; //bits
197 if (n > n1)
198 n1 = n;
199 table[j][1] = -n1; //bits
200 }
201 }
202 }
203
204 /* second pass : fill auxillary tables recursively */
205 for(i=0;i<table_size;i++)
206 {
207 n = table[i][1]; //bits
208 if (n < 0)
209 {
210 n = -n;
211 if (n > table_nb_bits)
212 {
213 n = table_nb_bits;
214 table[i][1] = -n; //bits
215 }
216 index = build_table(vlc, n, nb_codes,
217 bits, bits_wrap, bits_size,
218 codes, codes_wrap, codes_size,
219 (code_prefix << table_nb_bits) | i,
220 n_prefix + table_nb_bits);
221 if (index < 0)
222 return -1;
223 /* note: realloc has been done, so reload tables */
224 table = &vlc->table[table_index];
225 table[i][0] = index; //code
226 }
227 }
228 return table_index;
229}
230
231/* Build VLC decoding tables suitable for use with get_vlc().
232
233 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
234 bigger it is, the faster is the decoding. But it should not be too
235 big to save memory and L1 cache. '9' is a good compromise.
236
237 'nb_codes' : number of vlcs codes
238
239 'bits' : table which gives the size (in bits) of each vlc code.
240
241 'codes' : table which gives the bit pattern of of each vlc code.
242
243 'xxx_wrap' : give the number of bytes between each entry of the
244 'bits' or 'codes' tables.
245
246 'xxx_size' : gives the number of bytes of each entry of the 'bits'
247 or 'codes' tables.
248
249 'wrap' and 'size' allows to use any memory configuration and types
250 (byte/word/long) to store the 'bits' and 'codes' tables.
251*/
252int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
253 const void *bits, int bits_wrap, int bits_size,
254 const void *codes, int codes_wrap, int codes_size)
255{
256 vlc->bits = nb_bits;
257 vlc->table_size = 0;
258
259 if (build_table(vlc, nb_bits, nb_codes,
260 bits, bits_wrap, bits_size,
261 codes, codes_wrap, codes_size,
262 0, 0) < 0)
263 {
264
265 return -1;
266 }
267 return 0;
268}
diff --git a/apps/codecs/libwma/common.h b/apps/codecs/libwma/common.h
index b627bfb710..0efecf0571 100644
--- a/apps/codecs/libwma/common.h
+++ b/apps/codecs/libwma/common.h
@@ -22,6 +22,14 @@ typedef struct GetBitContext {
22 22
23static inline int get_bits_count(GetBitContext *s); 23static inline int get_bits_count(GetBitContext *s);
24 24
25#define VLC_TYPE int16_t
26
27typedef struct VLC {
28 int bits;
29 VLC_TYPE (*table)[2]; ///< code, bits
30 int table_size, table_allocated;
31} VLC;
32
25/* used to avoid missaligned exceptions on some archs (alpha, ...) */ 33/* used to avoid missaligned exceptions on some archs (alpha, ...) */
26static inline uint32_t unaligned32(const void *v) { 34static inline uint32_t unaligned32(const void *v) {
27 struct Unaligned { 35 struct Unaligned {
@@ -191,6 +199,59 @@ void init_get_bits(GetBitContext *s,
191 199
192int check_marker(GetBitContext *s, const char *msg); 200int check_marker(GetBitContext *s, const char *msg);
193void align_get_bits(GetBitContext *s); 201void align_get_bits(GetBitContext *s);
202int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
203 const void *bits, int bits_wrap, int bits_size,
204 const void *codes, int codes_wrap, int codes_size);
205
206#define GET_VLC(code, name, gb, table, bits, max_depth)\
207{\
208 int n, index, nb_bits;\
209\
210 index= SHOW_UBITS(name, gb, bits);\
211 code = table[index][0];\
212 n = table[index][1];\
213\
214 if(max_depth > 1 && n < 0){\
215 LAST_SKIP_BITS(name, gb, bits)\
216 UPDATE_CACHE(name, gb)\
217\
218 nb_bits = -n;\
219\
220 index= SHOW_UBITS(name, gb, nb_bits) + code;\
221 code = table[index][0];\
222 n = table[index][1];\
223 if(max_depth > 2 && n < 0){\
224 LAST_SKIP_BITS(name, gb, nb_bits)\
225 UPDATE_CACHE(name, gb)\
226\
227 nb_bits = -n;\
228\
229 index= SHOW_UBITS(name, gb, nb_bits) + code;\
230 code = table[index][0];\
231 n = table[index][1];\
232 }\
233 }\
234 SKIP_BITS(name, gb, n)\
235}
236
237
238// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
239static inline int get_vlc(GetBitContext *s, VLC *vlc)
240{
241 int code;
242 VLC_TYPE (*table)[2]= vlc->table;
243
244 OPEN_READER(re, s)
245 UPDATE_CACHE(re, s)
246
247 GET_VLC(code, re, s, table, vlc->bits, 3)
248
249 CLOSE_READER(re, s)
250 return code;
251}
252
253
254
194 255
195//#define TRACE 256//#define TRACE
196 257
diff --git a/apps/codecs/libwma/wmadec.h b/apps/codecs/libwma/wmadec.h
index 56f935bbb4..e23d7c20b0 100644
--- a/apps/codecs/libwma/wmadec.h
+++ b/apps/codecs/libwma/wmadec.h
@@ -33,16 +33,6 @@
33 33
34#define LSP_POW_BITS 7 34#define LSP_POW_BITS 7
35 35
36#define VLC_TYPE int16_t
37
38typedef struct VLC
39{
40 int bits;
41 VLC_TYPE (*table)[2]; ///< code, bits
42 int table_size, table_allocated;
43}
44VLC;
45
46#define fixed32 int32_t 36#define fixed32 int32_t
47#define fixed64 int64_t 37#define fixed64 int64_t
48 38
diff --git a/apps/codecs/libwma/wmadeci.c b/apps/codecs/libwma/wmadeci.c
index dc45e64042..c266316cf9 100644
--- a/apps/codecs/libwma/wmadeci.c
+++ b/apps/codecs/libwma/wmadeci.c
@@ -297,217 +297,6 @@ int fft_calc_unscaled(FFTContext *s, FFTComplex *z)
297 return 0; 297 return 0;
298} 298}
299 299
300
301/* VLC decoding */
302
303#define GET_VLC(code, name, gb, table, bits, max_depth)\
304{\
305 int n, index, nb_bits;\
306\
307 index= SHOW_UBITS(name, gb, bits);\
308 code = table[index][0];\
309 n = table[index][1];\
310\
311 if(max_depth > 1 && n < 0){\
312 LAST_SKIP_BITS(name, gb, bits)\
313 UPDATE_CACHE(name, gb)\
314\
315 nb_bits = -n;\
316\
317 index= SHOW_UBITS(name, gb, nb_bits) + code;\
318 code = table[index][0];\
319 n = table[index][1];\
320 if(max_depth > 2 && n < 0){\
321 LAST_SKIP_BITS(name, gb, nb_bits)\
322 UPDATE_CACHE(name, gb)\
323\
324 nb_bits = -n;\
325\
326 index= SHOW_UBITS(name, gb, nb_bits) + code;\
327 code = table[index][0];\
328 n = table[index][1];\
329 }\
330 }\
331 SKIP_BITS(name, gb, n)\
332}
333
334
335//#define DEBUG_VLC
336
337#define GET_DATA(v, table, i, wrap, size) \
338{\
339 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
340 switch(size) {\
341 case 1:\
342 v = *(const uint8_t *)ptr;\
343 break;\
344 case 2:\
345 v = *(const uint16_t *)ptr;\
346 break;\
347 default:\
348 v = *(const uint32_t *)ptr;\
349 break;\
350 }\
351}
352
353// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
354static inline int get_vlc(GetBitContext *s, VLC *vlc)
355{
356 int code;
357 VLC_TYPE (*table)[2]= vlc->table;
358
359 OPEN_READER(re, s)
360 UPDATE_CACHE(re, s)
361
362 GET_VLC(code, re, s, table, vlc->bits, 3)
363
364 CLOSE_READER(re, s)
365 return code;
366}
367
368static int alloc_table(VLC *vlc, int size)
369{
370 int index;
371 index = vlc->table_size;
372 vlc->table_size += size;
373 if (vlc->table_size > vlc->table_allocated)
374 {
375 vlc->table_allocated += (1 << vlc->bits);
376 if (!vlc->table)
377 return -1;
378 }
379 return index;
380}
381
382static int build_table(VLC *vlc, int table_nb_bits,
383 int nb_codes,
384 const void *bits, int bits_wrap, int bits_size,
385 const void *codes, int codes_wrap, int codes_size,
386 uint32_t code_prefix, int n_prefix)
387{
388 int i, j, k, n, table_size, table_index, nb, n1, index;
389 uint32_t code;
390 VLC_TYPE (*table)[2];
391
392 table_size = 1 << table_nb_bits;
393 table_index = alloc_table(vlc, table_size);
394 if (table_index < 0)
395 return -1;
396 table = &vlc->table[table_index];
397
398 for(i=0;i<table_size;i++)
399 {
400 table[i][1] = 0; //bits
401 table[i][0] = -1; //codes
402 }
403
404 /* first pass: map codes and compute auxillary table sizes */
405 for(i=0;i<nb_codes;i++)
406 {
407 GET_DATA(n, bits, i, bits_wrap, bits_size);
408 GET_DATA(code, codes, i, codes_wrap, codes_size);
409 /* we accept tables with holes */
410 if (n <= 0)
411 continue;
412 /* if code matches the prefix, it is in the table */
413 n -= n_prefix;
414 if (n > 0 && (code >> n) == code_prefix)
415 {
416 if (n <= table_nb_bits)
417 {
418 /* no need to add another table */
419 j = (code << (table_nb_bits - n)) & (table_size - 1);
420 nb = 1 << (table_nb_bits - n);
421 for(k=0;k<nb;k++)
422 {
423 if (table[j][1] /*bits*/ != 0)
424 {
425 // PJJ exit(-1);
426 }
427 table[j][1] = n; //bits
428 table[j][0] = i; //code
429 j++;
430 }
431 }
432 else
433 {
434 n -= table_nb_bits;
435 j = (code >> n) & ((1 << table_nb_bits) - 1);
436 /* compute table size */
437 n1 = -table[j][1]; //bits
438 if (n > n1)
439 n1 = n;
440 table[j][1] = -n1; //bits
441 }
442 }
443 }
444
445 /* second pass : fill auxillary tables recursively */
446 for(i=0;i<table_size;i++)
447 {
448 n = table[i][1]; //bits
449 if (n < 0)
450 {
451 n = -n;
452 if (n > table_nb_bits)
453 {
454 n = table_nb_bits;
455 table[i][1] = -n; //bits
456 }
457 index = build_table(vlc, n, nb_codes,
458 bits, bits_wrap, bits_size,
459 codes, codes_wrap, codes_size,
460 (code_prefix << table_nb_bits) | i,
461 n_prefix + table_nb_bits);
462 if (index < 0)
463 return -1;
464 /* note: realloc has been done, so reload tables */
465 table = &vlc->table[table_index];
466 table[i][0] = index; //code
467 }
468 }
469 return table_index;
470}
471
472/* Build VLC decoding tables suitable for use with get_vlc().
473
474 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
475 bigger it is, the faster is the decoding. But it should not be too
476 big to save memory and L1 cache. '9' is a good compromise.
477
478 'nb_codes' : number of vlcs codes
479
480 'bits' : table which gives the size (in bits) of each vlc code.
481
482 'codes' : table which gives the bit pattern of of each vlc code.
483
484 'xxx_wrap' : give the number of bytes between each entry of the
485 'bits' or 'codes' tables.
486
487 'xxx_size' : gives the number of bytes of each entry of the 'bits'
488 or 'codes' tables.
489
490 'wrap' and 'size' allows to use any memory configuration and types
491 (byte/word/long) to store the 'bits' and 'codes' tables.
492*/
493int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
494 const void *bits, int bits_wrap, int bits_size,
495 const void *codes, int codes_wrap, int codes_size)
496{
497 vlc->bits = nb_bits;
498 vlc->table_size = 0;
499
500 if (build_table(vlc, nb_bits, nb_codes,
501 bits, bits_wrap, bits_size,
502 codes, codes_wrap, codes_size,
503 0, 0) < 0)
504 {
505
506 return -1;
507 }
508 return 0;
509}
510
511/** 300/**
512 * init MDCT or IMDCT computation. 301 * init MDCT or IMDCT computation.
513 */ 302 */