Align various libwma buffers. Saves about 1 MHz on the Gigabeat S.
[maemo-rb.git] / apps / codecs / libwma / wmadeci.c
blob98c10e486db4ceb624fc1e050cab665a5b6a8c7b
1 /*
2 * WMA compatible decoder
3 * Copyright (c) 2002 The FFmpeg Project.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /**
21 * @file wmadec.c
22 * WMA compatible decoder.
25 #include <codecs.h>
26 #include <codecs/lib/codeclib.h>
27 #include <codecs/libasf/asf.h>
28 #include "wmadec.h"
29 #include "wmafixed.h"
30 #include "wmadata.h"
32 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
34 /*declarations of statically allocated variables used to remove malloc calls*/
36 fixed32 coefsarray[MAX_CHANNELS][BLOCK_MAX_SIZE] IBSS_ATTR MEM_ALIGN_ATTR;
37 /*decode and window into IRAM on targets with at least 80KB of codec IRAM*/
38 fixed32 frame_out_buf[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] IBSS_ATTR_WMA_LARGE_IRAM MEM_ALIGN_ATTR;
40 /*MDCT reconstruction windows*/
41 fixed32 stat0[2048] MEM_ALIGN_ATTR, stat1[1024] MEM_ALIGN_ATTR,
42 stat2[512] MEM_ALIGN_ATTR, stat3[256] MEM_ALIGN_ATTR, stat4[128] MEM_ALIGN_ATTR;
44 /*VLC lookup tables*/
45 uint16_t *runtabarray[2], *levtabarray[2];
47 uint16_t runtab_big[1336] MEM_ALIGN_ATTR, runtab_small[1072] MEM_ALIGN_ATTR,
48 levtab_big[1336] MEM_ALIGN_ATTR, levtab_small[1072] MEM_ALIGN_ATTR;
50 #define VLCBUF1SIZE 4598
51 #define VLCBUF2SIZE 3574
52 #define VLCBUF3SIZE 360
53 #define VLCBUF4SIZE 540
55 /*putting these in IRAM actually makes PP slower*/
57 VLC_TYPE vlcbuf1[VLCBUF1SIZE][2] MEM_ALIGN_ATTR;
58 VLC_TYPE vlcbuf2[VLCBUF2SIZE][2] MEM_ALIGN_ATTR;
59 /* This buffer gets reused for lsp tables */
60 VLC_TYPE vlcbuf3[VLCBUF3SIZE][2] MEM_ALIGN_ATTR;
61 VLC_TYPE vlcbuf4[VLCBUF4SIZE][2] MEM_ALIGN_ATTR;
66 /**
67 * Apply MDCT window and add into output.
69 * We ensure that when the windows overlap their squared sum
70 * is always 1 (MDCT reconstruction rule).
72 * The Vorbis I spec has a great diagram explaining this process.
73 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
75 static void wma_window(WMADecodeContext *s, fixed32 *in, fixed32 *out)
77 //float *in = s->output;
78 int block_len, bsize, n;
80 /* left part */
82 /* previous block was larger, so we'll use the size of the current
83 * block to set the window size*/
84 if (s->block_len_bits <= s->prev_block_len_bits) {
85 block_len = s->block_len;
86 bsize = s->frame_len_bits - s->block_len_bits;
88 vector_fmul_add_add(out, in, s->windows[bsize], block_len);
90 } else {
91 /*previous block was smaller or the same size, so use it's size to set the window length*/
92 block_len = 1 << s->prev_block_len_bits;
93 /*find the middle of the two overlapped blocks, this will be the first overlapped sample*/
94 n = (s->block_len - block_len) / 2;
95 bsize = s->frame_len_bits - s->prev_block_len_bits;
97 vector_fmul_add_add(out+n, in+n, s->windows[bsize], block_len);
99 memcpy(out+n+block_len, in+n+block_len, n*sizeof(fixed32));
101 /* Advance to the end of the current block and prepare to window it for the next block.
102 * Since the window function needs to be reversed, we do it backwards starting with the
103 * last sample and moving towards the first
105 out += s->block_len;
106 in += s->block_len;
108 /* right part */
109 if (s->block_len_bits <= s->next_block_len_bits) {
110 block_len = s->block_len;
111 bsize = s->frame_len_bits - s->block_len_bits;
113 vector_fmul_reverse(out, in, s->windows[bsize], block_len);
115 } else {
116 block_len = 1 << s->next_block_len_bits;
117 n = (s->block_len - block_len) / 2;
118 bsize = s->frame_len_bits - s->next_block_len_bits;
120 memcpy(out, in, n*sizeof(fixed32));
122 vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
124 memset(out+n+block_len, 0, n*sizeof(fixed32));
131 /* XXX: use same run/length optimization as mpeg decoders */
132 static void init_coef_vlc(VLC *vlc,
133 uint16_t **prun_table, uint16_t **plevel_table,
134 const CoefVLCTable *vlc_table, int tab)
136 int n = vlc_table->n;
137 const uint8_t *table_bits = vlc_table->huffbits;
138 const uint32_t *table_codes = vlc_table->huffcodes;
139 const uint16_t *levels_table = vlc_table->levels;
140 uint16_t *run_table, *level_table;
141 const uint16_t *p;
142 int i, l, j, level;
145 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, INIT_VLC_USE_NEW_STATIC);
147 run_table = runtabarray[tab];
148 level_table= levtabarray[tab];
150 p = levels_table;
151 i = 2;
152 level = 1;
153 while (i < n)
155 l = *p++;
156 for(j=0;j<l;++j)
158 run_table[i] = j;
159 level_table[i] = level;
160 ++i;
162 ++level;
164 *prun_table = run_table;
165 *plevel_table = level_table;
168 int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx)
171 int i, flags2;
172 fixed32 *window;
173 uint8_t *extradata;
174 fixed64 bps1;
175 fixed32 high_freq;
176 fixed64 bps;
177 int sample_rate1;
178 int coef_vlc_table;
179 // int filehandle;
180 #ifdef CPU_COLDFIRE
181 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
182 #endif
184 /*clear stereo setting to avoid glitches when switching stereo->mono*/
185 s->channel_coded[0]=0;
186 s->channel_coded[1]=0;
187 s->ms_stereo=0;
189 s->sample_rate = wfx->rate;
190 s->nb_channels = wfx->channels;
191 s->bit_rate = wfx->bitrate;
192 s->block_align = wfx->blockalign;
194 s->coefs = &coefsarray;
195 s->frame_out = &frame_out_buf;
197 if (wfx->codec_id == ASF_CODEC_ID_WMAV1) {
198 s->version = 1;
199 } else if (wfx->codec_id == ASF_CODEC_ID_WMAV2 ) {
200 s->version = 2;
201 } else {
202 /*one of those other wma flavors that don't have GPLed decoders */
203 return -1;
206 /* extract flag infos */
207 flags2 = 0;
208 extradata = wfx->data;
209 if (s->version == 1 && wfx->datalen >= 4) {
210 flags2 = extradata[2] | (extradata[3] << 8);
211 }else if (s->version == 2 && wfx->datalen >= 6){
212 flags2 = extradata[4] | (extradata[5] << 8);
214 s->use_exp_vlc = flags2 & 0x0001;
215 s->use_bit_reservoir = flags2 & 0x0002;
216 s->use_variable_block_len = flags2 & 0x0004;
218 /* compute MDCT block size */
219 if (s->sample_rate <= 16000){
220 s->frame_len_bits = 9;
221 }else if (s->sample_rate <= 22050 ||
222 (s->sample_rate <= 32000 && s->version == 1)){
223 s->frame_len_bits = 10;
224 }else{
225 s->frame_len_bits = 11;
227 s->frame_len = 1 << s->frame_len_bits;
228 if (s-> use_variable_block_len)
230 int nb_max, nb;
231 nb = ((flags2 >> 3) & 3) + 1;
232 if ((s->bit_rate / s->nb_channels) >= 32000)
234 nb += 2;
236 nb_max = s->frame_len_bits - BLOCK_MIN_BITS; //max is 11-7
237 if (nb > nb_max)
238 nb = nb_max;
239 s->nb_block_sizes = nb + 1;
241 else
243 s->nb_block_sizes = 1;
246 /* init rate dependant parameters */
247 s->use_noise_coding = 1;
248 high_freq = itofix64(s->sample_rate) >> 1;
251 /* if version 2, then the rates are normalized */
252 sample_rate1 = s->sample_rate;
253 if (s->version == 2)
255 if (sample_rate1 >= 44100)
256 sample_rate1 = 44100;
257 else if (sample_rate1 >= 22050)
258 sample_rate1 = 22050;
259 else if (sample_rate1 >= 16000)
260 sample_rate1 = 16000;
261 else if (sample_rate1 >= 11025)
262 sample_rate1 = 11025;
263 else if (sample_rate1 >= 8000)
264 sample_rate1 = 8000;
267 fixed64 tmp = itofix64(s->bit_rate);
268 fixed64 tmp2 = itofix64(s->nb_channels * s->sample_rate);
269 bps = fixdiv64(tmp, tmp2);
270 fixed64 tim = bps * s->frame_len;
271 fixed64 tmpi = fixdiv64(tim,itofix64(8));
272 s->byte_offset_bits = av_log2(fixtoi64(tmpi+0x8000)) + 2;
274 /* compute high frequency value and choose if noise coding should
275 be activated */
276 bps1 = bps;
277 if (s->nb_channels == 2)
278 bps1 = fixmul32(bps,0x1999a);
279 if (sample_rate1 == 44100)
281 if (bps1 >= 0x9c29)
282 s->use_noise_coding = 0;
283 else
284 high_freq = fixmul32(high_freq,0x6666);
286 else if (sample_rate1 == 22050)
288 if (bps1 >= 0x128f6)
289 s->use_noise_coding = 0;
290 else if (bps1 >= 0xb852)
291 high_freq = fixmul32(high_freq,0xb333);
292 else
293 high_freq = fixmul32(high_freq,0x999a);
295 else if (sample_rate1 == 16000)
297 if (bps > 0x8000)
298 high_freq = fixmul32(high_freq,0x8000);
299 else
300 high_freq = fixmul32(high_freq,0x4ccd);
302 else if (sample_rate1 == 11025)
304 high_freq = fixmul32(high_freq,0xb333);
306 else if (sample_rate1 == 8000)
308 if (bps <= 0xa000)
310 high_freq = fixmul32(high_freq,0x8000);
312 else if (bps > 0xc000)
314 s->use_noise_coding = 0;
316 else
318 high_freq = fixmul32(high_freq,0xa666);
321 else
323 if (bps >= 0xcccd)
325 high_freq = fixmul32(high_freq,0xc000);
327 else if (bps >= 0x999a)
329 high_freq = fixmul32(high_freq,0x999a);
331 else
333 high_freq = fixmul32(high_freq,0x8000);
337 /* compute the scale factor band sizes for each MDCT block size */
339 int a, b, pos, lpos, k, block_len, i, j, n;
340 const uint8_t *table;
342 if (s->version == 1)
344 s->coefs_start = 3;
346 else
348 s->coefs_start = 0;
350 for(k = 0; k < s->nb_block_sizes; ++k)
352 block_len = s->frame_len >> k;
354 if (s->version == 1)
356 lpos = 0;
357 for(i=0;i<25;++i)
359 a = wma_critical_freqs[i];
360 b = s->sample_rate;
361 pos = ((block_len * 2 * a) + (b >> 1)) / b;
362 if (pos > block_len)
363 pos = block_len;
364 s->exponent_bands[0][i] = pos - lpos;
365 if (pos >= block_len)
367 ++i;
368 break;
370 lpos = pos;
372 s->exponent_sizes[0] = i;
374 else
376 /* hardcoded tables */
377 table = NULL;
378 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
379 if (a < 3)
381 if (s->sample_rate >= 44100)
382 table = exponent_band_44100[a];
383 else if (s->sample_rate >= 32000)
384 table = exponent_band_32000[a];
385 else if (s->sample_rate >= 22050)
386 table = exponent_band_22050[a];
388 if (table)
390 n = *table++;
391 for(i=0;i<n;++i)
392 s->exponent_bands[k][i] = table[i];
393 s->exponent_sizes[k] = n;
395 else
397 j = 0;
398 lpos = 0;
399 for(i=0;i<25;++i)
401 a = wma_critical_freqs[i];
402 b = s->sample_rate;
403 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
404 pos <<= 2;
405 if (pos > block_len)
406 pos = block_len;
407 if (pos > lpos)
408 s->exponent_bands[k][j++] = pos - lpos;
409 if (pos >= block_len)
410 break;
411 lpos = pos;
413 s->exponent_sizes[k] = j;
417 /* max number of coefs */
418 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
419 /* high freq computation */
421 fixed32 tmp1 = high_freq*2; /* high_freq is a fixed32!*/
422 fixed32 tmp2=itofix32(s->sample_rate>>1);
423 s->high_band_start[k] = fixtoi32( fixdiv32(tmp1, tmp2) * (block_len>>1) +0x8000);
426 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
427 s->sample_rate + 0.5);*/
429 n = s->exponent_sizes[k];
430 j = 0;
431 pos = 0;
432 for(i=0;i<n;++i)
434 int start, end;
435 start = pos;
436 pos += s->exponent_bands[k][i];
437 end = pos;
438 if (start < s->high_band_start[k])
439 start = s->high_band_start[k];
440 if (end > s->coefs_end[k])
441 end = s->coefs_end[k];
442 if (end > start)
443 s->exponent_high_bands[k][j++] = end - start;
445 s->exponent_high_sizes[k] = j;
449 /* ffmpeg uses malloc to only allocate as many window sizes as needed.
450 * However, we're really only interested in the worst case memory usage.
451 * In the worst case you can have 5 window sizes, 128 doubling up 2048
452 * Smaller windows are handled differently.
453 * Since we don't have malloc, just statically allocate this
455 fixed32 *temp[5];
456 temp[0] = stat0;
457 temp[1] = stat1;
458 temp[2] = stat2;
459 temp[3] = stat3;
460 temp[4] = stat4;
462 /* init MDCT windows : simple sinus window */
463 for(i = 0; i < s->nb_block_sizes; i++)
465 int n, j;
466 fixed32 alpha;
467 n = 1 << (s->frame_len_bits - i);
468 window = temp[i];
470 /* this calculates 0.5/(2*n) */
471 alpha = (1<<15)>>(s->frame_len_bits - i+1);
472 for(j=0;j<n;++j)
474 fixed32 j2 = itofix32(j) + 0x8000;
475 /*alpha between 0 and pi/2*/
476 window[j] = fsincos(fixmul32(j2,alpha)<<16, 0);
478 s->windows[i] = window;
482 s->reset_block_lengths = 1;
484 if (s->use_noise_coding)
486 /* init the noise generator */
487 if (s->use_exp_vlc)
489 s->noise_mult = 0x51f;
490 s->noise_table = noisetable_exp;
492 else
494 s->noise_mult = 0xa3d;
495 /* LSP values are simply 2x the EXP values */
496 for (i=0;i<NOISE_TAB_SIZE;++i)
497 noisetable_exp[i] = noisetable_exp[i]<< 1;
498 s->noise_table = noisetable_exp;
500 #if 0
501 /* We use a lookup table computered in advance, so no need to do this*/
503 unsigned int seed;
504 fixed32 norm;
505 seed = 1;
506 norm = 0; // PJJ: near as makes any diff to 0!
507 for (i=0;i<NOISE_TAB_SIZE;++i)
509 seed = seed * 314159 + 1;
510 s->noise_table[i] = itofix32((int)seed) * norm;
513 #endif
515 s->hgain_vlc.table = vlcbuf4;
516 s->hgain_vlc.table_allocated = VLCBUF4SIZE;
517 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(hgain_huffbits),
518 hgain_huffbits, 1, 1,
519 hgain_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC);
522 if (s->use_exp_vlc)
525 s->exp_vlc.table = vlcbuf3;
526 s->exp_vlc.table_allocated = VLCBUF3SIZE;
528 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(scale_huffbits),
529 scale_huffbits, 1, 1,
530 scale_huffcodes, 4, 4, INIT_VLC_USE_NEW_STATIC);
532 else
534 wma_lsp_to_curve_init(s, s->frame_len);
537 /* choose the VLC tables for the coefficients */
538 coef_vlc_table = 2;
539 if (s->sample_rate >= 32000)
541 if (bps1 < 0xb852)
542 coef_vlc_table = 0;
543 else if (bps1 < 0x128f6)
544 coef_vlc_table = 1;
547 /* since the coef2 table is the biggest and that has index 2 in coef_vlcs
548 it's safe to always assign like this */
549 runtabarray[0] = runtab_big; runtabarray[1] = runtab_small;
550 levtabarray[0] = levtab_big; levtabarray[1] = levtab_small;
552 s->coef_vlc[0].table = vlcbuf1;
553 s->coef_vlc[0].table_allocated = VLCBUF1SIZE;
554 s->coef_vlc[1].table = vlcbuf2;
555 s->coef_vlc[1].table_allocated = VLCBUF2SIZE;
558 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
559 &coef_vlcs[coef_vlc_table * 2], 0);
560 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
561 &coef_vlcs[coef_vlc_table * 2 + 1], 1);
563 s->last_superframe_len = 0;
564 s->last_bitoffset = 0;
566 return 0;
570 /* compute x^-0.25 with an exponent and mantissa table. We use linear
571 interpolation to reduce the mantissa table size at a small speed
572 expense (linear interpolation approximately doubles the number of
573 bits of precision). */
574 static inline fixed32 pow_m1_4(WMADecodeContext *s, fixed32 x)
576 union {
577 float f;
578 unsigned int v;
579 } u, t;
580 unsigned int e, m;
581 fixed32 a, b;
583 u.f = fixtof64(x);
584 e = u.v >> 23;
585 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
586 /* build interpolation scale: 1 <= t < 2. */
587 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
588 a = ((fixed32*)s->lsp_pow_m_table1)[m];
589 b = ((fixed32*)s->lsp_pow_m_table2)[m];
591 /* lsp_pow_e_table contains 32.32 format */
592 /* TODO: Since we're unlikely have value that cover the whole
593 * IEEE754 range, we probably don't need to have all possible exponents */
595 return (lsp_pow_e_table[e] * (a + fixmul32(b, ftofix32(t.f))) >>32);
598 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
600 fixed32 wdel, a, b, temp2;
601 int i, m;
603 wdel = fixdiv32(itofix32(1), itofix32(frame_len));
604 for (i=0; i<frame_len; ++i)
606 /* TODO: can probably reuse the trig_init values here */
607 fsincos((wdel*i)<<15, &temp2);
608 /* get 3 bits headroom + 1 bit from not doubleing the values */
609 s->lsp_cos_table[i] = temp2>>3;
612 /* NOTE: these two tables are needed to avoid two operations in
613 pow_m1_4 */
614 b = itofix32(1);
615 int ix = 0;
617 s->lsp_pow_m_table1 = &vlcbuf3[0];
618 s->lsp_pow_m_table2 = &vlcbuf3[VLCBUF3SIZE];
620 /*double check this later*/
621 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--)
623 m = (1 << LSP_POW_BITS) + i;
624 a = pow_a_table[ix++]<<4;
625 ((fixed32*)s->lsp_pow_m_table1)[i] = 2 * a - b;
626 ((fixed32*)s->lsp_pow_m_table2)[i] = b - a;
627 b = a;
632 /* NOTE: We use the same code as Vorbis here */
633 /* XXX: optimize it further with SSE/3Dnow */
634 static void wma_lsp_to_curve(WMADecodeContext *s,
635 fixed32 *out,
636 fixed32 *val_max_ptr,
637 int n,
638 fixed32 *lsp)
640 int i, j;
641 fixed32 p, q, w, v, val_max, temp2;
643 val_max = 0;
644 for(i=0;i<n;++i)
646 /* shift by 2 now to reduce rounding error,
647 * we can renormalize right before pow_m1_4
650 p = 0x8000<<5;
651 q = 0x8000<<5;
652 w = s->lsp_cos_table[i];
654 for (j=1;j<NB_LSP_COEFS;j+=2)
656 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
657 temp2 = ((w - (lsp[j - 1]<<11)));
659 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
660 q = fixmul32b(q, temp2 )<<4;
661 p = fixmul32b(p, (w - (lsp[j]<<11)))<<4;
664 /* 2 in 5.27 format is 0x10000000 */
665 p = fixmul32(p, fixmul32b(p, (0x10000000 - w)))<<3;
666 q = fixmul32(q, fixmul32b(q, (0x10000000 + w)))<<3;
668 v = (p + q) >>9; /* p/q end up as 16.16 */
669 v = pow_m1_4(s, v);
670 if (v > val_max)
671 val_max = v;
672 out[i] = v;
675 *val_max_ptr = val_max;
678 /* decode exponents coded with LSP coefficients (same idea as Vorbis)
679 * only used for low bitrate (< 16kbps) files
681 static void decode_exp_lsp(WMADecodeContext *s, int ch)
683 fixed32 lsp_coefs[NB_LSP_COEFS];
684 int val, i;
686 for (i = 0; i < NB_LSP_COEFS; ++i)
688 if (i == 0 || i >= 8)
689 val = get_bits(&s->gb, 3);
690 else
691 val = get_bits(&s->gb, 4);
692 lsp_coefs[i] = lsp_codebook[i][val];
695 wma_lsp_to_curve(s,
696 s->exponents[ch],
697 &s->max_exponent[ch],
698 s->block_len,
699 lsp_coefs);
702 /* decode exponents coded with VLC codes - used for bitrate >= 32kbps*/
703 static int decode_exp_vlc(WMADecodeContext *s, int ch)
705 int last_exp, n, code;
706 const uint16_t *ptr, *band_ptr;
707 fixed32 v, max_scale;
708 fixed32 *q,*q_end;
710 /*accommodate the 60 negative indices */
711 const fixed32 *pow_10_to_yover16_ptr = &pow_10_to_yover16[61];
713 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
714 ptr = band_ptr;
715 q = s->exponents[ch];
716 q_end = q + s->block_len;
717 max_scale = 0;
720 if (s->version == 1) //wmav1 only
722 last_exp = get_bits(&s->gb, 5) + 10;
724 v = pow_10_to_yover16_ptr[last_exp];
725 max_scale = v;
726 n = *ptr++;
727 switch (n & 3) do {
728 case 0: *q++ = v;
729 case 3: *q++ = v;
730 case 2: *q++ = v;
731 case 1: *q++ = v;
732 } while ((n -= 4) > 0);
733 } else {
734 last_exp = 36;
737 while (q < q_end)
739 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
740 if (code < 0)
742 return -1;
744 /* NOTE: this offset is the same as MPEG4 AAC ! */
745 last_exp += code - 60;
747 v = pow_10_to_yover16_ptr[last_exp];
748 if (v > max_scale)
750 max_scale = v;
752 n = *ptr++;
753 switch (n & 3) do {
754 case 0: *q++ = v;
755 case 3: *q++ = v;
756 case 2: *q++ = v;
757 case 1: *q++ = v;
758 } while ((n -= 4) > 0);
761 s->max_exponent[ch] = max_scale;
762 return 0;
765 /* return 0 if OK. return 1 if last block of frame. return -1 if
766 unrecorrable error. */
767 static int wma_decode_block(WMADecodeContext *s)
769 int n, v, a, ch, code, bsize;
770 int coef_nb_bits, total_gain;
771 int nb_coefs[MAX_CHANNELS];
772 fixed32 mdct_norm;
774 /*DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s->block_num, s->block_len, s->frame_len);*/
776 /* compute current block length */
777 if (s->use_variable_block_len)
779 n = av_log2(s->nb_block_sizes - 1) + 1;
781 if (s->reset_block_lengths)
783 s->reset_block_lengths = 0;
784 v = get_bits(&s->gb, n);
785 if (v >= s->nb_block_sizes)
787 return -2;
789 s->prev_block_len_bits = s->frame_len_bits - v;
790 v = get_bits(&s->gb, n);
791 if (v >= s->nb_block_sizes)
793 return -3;
795 s->block_len_bits = s->frame_len_bits - v;
797 else
799 /* update block lengths */
800 s->prev_block_len_bits = s->block_len_bits;
801 s->block_len_bits = s->next_block_len_bits;
803 v = get_bits(&s->gb, n);
805 if (v >= s->nb_block_sizes)
807 // rb->splash(HZ*4, "v was %d", v); //5, 7
808 return -4; //this is it
810 else{
811 //rb->splash(HZ, "passed v block (%d)!", v);
813 s->next_block_len_bits = s->frame_len_bits - v;
815 else
817 /* fixed block len */
818 s->next_block_len_bits = s->frame_len_bits;
819 s->prev_block_len_bits = s->frame_len_bits;
820 s->block_len_bits = s->frame_len_bits;
822 /* now check if the block length is coherent with the frame length */
823 s->block_len = 1 << s->block_len_bits;
825 if ((s->block_pos + s->block_len) > s->frame_len)
827 return -5; //oddly 32k sample from tracker fails here
830 if (s->nb_channels == 2)
832 s->ms_stereo = get_bits1(&s->gb);
834 v = 0;
835 for (ch = 0; ch < s->nb_channels; ++ch)
837 a = get_bits1(&s->gb);
838 s->channel_coded[ch] = a;
839 v |= a;
841 /* if no channel coded, no need to go further */
842 /* XXX: fix potential framing problems */
843 if (!v)
845 goto next;
848 bsize = s->frame_len_bits - s->block_len_bits;
850 /* read total gain and extract corresponding number of bits for
851 coef escape coding */
852 total_gain = 1;
853 for(;;)
855 a = get_bits(&s->gb, 7);
856 total_gain += a;
857 if (a != 127)
859 break;
863 if (total_gain < 15)
864 coef_nb_bits = 13;
865 else if (total_gain < 32)
866 coef_nb_bits = 12;
867 else if (total_gain < 40)
868 coef_nb_bits = 11;
869 else if (total_gain < 45)
870 coef_nb_bits = 10;
871 else
872 coef_nb_bits = 9;
874 /* compute number of coefficients */
875 n = s->coefs_end[bsize] - s->coefs_start;
877 for(ch = 0; ch < s->nb_channels; ++ch)
879 nb_coefs[ch] = n;
881 /* complex coding */
882 if (s->use_noise_coding)
885 for(ch = 0; ch < s->nb_channels; ++ch)
887 if (s->channel_coded[ch])
889 int i, n, a;
890 n = s->exponent_high_sizes[bsize];
891 for(i=0;i<n;++i)
893 a = get_bits1(&s->gb);
894 s->high_band_coded[ch][i] = a;
895 /* if noise coding, the coefficients are not transmitted */
896 if (a)
897 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
901 for(ch = 0; ch < s->nb_channels; ++ch)
903 if (s->channel_coded[ch])
905 int i, n, val, code;
907 n = s->exponent_high_sizes[bsize];
908 val = (int)0x80000000;
909 for(i=0;i<n;++i)
911 if (s->high_band_coded[ch][i])
913 if (val == (int)0x80000000)
915 val = get_bits(&s->gb, 7) - 19;
917 else
919 //code = get_vlc(&s->gb, &s->hgain_vlc);
920 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
921 if (code < 0)
923 return -6;
925 val += code - 18;
927 s->high_band_values[ch][i] = val;
934 /* exponents can be reused in short blocks. */
935 if ((s->block_len_bits == s->frame_len_bits) || get_bits1(&s->gb))
937 for(ch = 0; ch < s->nb_channels; ++ch)
939 if (s->channel_coded[ch])
941 if (s->use_exp_vlc)
943 if (decode_exp_vlc(s, ch) < 0)
945 return -7;
948 else
950 decode_exp_lsp(s, ch);
952 s->exponents_bsize[ch] = bsize;
957 /* parse spectral coefficients : just RLE encoding */
958 for(ch = 0; ch < s->nb_channels; ++ch)
960 if (s->channel_coded[ch])
962 VLC *coef_vlc;
963 int level, run, sign, tindex;
964 int16_t *ptr, *eptr;
965 const int16_t *level_table, *run_table;
967 /* special VLC tables are used for ms stereo because
968 there is potentially less energy there */
969 tindex = (ch == 1 && s->ms_stereo);
970 coef_vlc = &s->coef_vlc[tindex];
971 run_table = s->run_table[tindex];
972 level_table = s->level_table[tindex];
973 /* XXX: optimize */
974 ptr = &s->coefs1[ch][0];
975 eptr = ptr + nb_coefs[ch];
976 memset(ptr, 0, s->block_len * sizeof(int16_t));
978 for(;;)
980 code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
982 if (code < 0)
984 return -8;
986 if (code == 1)
988 /* EOB */
989 break;
991 else if (code == 0)
993 /* escape */
994 level = get_bits(&s->gb, coef_nb_bits);
995 /* NOTE: this is rather suboptimal. reading
996 block_len_bits would be better */
997 run = get_bits(&s->gb, s->frame_len_bits);
999 else
1001 /* normal code */
1002 run = run_table[code];
1003 level = level_table[code];
1005 sign = get_bits1(&s->gb);
1006 if (!sign)
1007 level = -level;
1008 ptr += run;
1009 if (ptr >= eptr)
1011 break;
1013 *ptr++ = level;
1016 /* NOTE: EOB can be omitted */
1017 if (ptr >= eptr)
1018 break;
1021 if (s->version == 1 && s->nb_channels >= 2)
1023 align_get_bits(&s->gb);
1028 int n4 = s->block_len >> 1;
1031 mdct_norm = 0x10000>>(s->block_len_bits-1);
1033 if (s->version == 1)
1035 mdct_norm *= fixtoi32(fixsqrt32(itofix32(n4)));
1040 /* finally compute the MDCT coefficients */
1041 for(ch = 0; ch < s->nb_channels; ++ch)
1043 if (s->channel_coded[ch])
1045 int16_t *coefs1;
1046 fixed32 *exponents;
1047 fixed32 *coefs, atemp;
1048 fixed64 mult;
1049 fixed64 mult1;
1050 fixed32 noise, temp1, temp2, mult2;
1051 int i, j, n, n1, last_high_band, esize;
1052 fixed32 exp_power[HIGH_BAND_MAX_SIZE];
1054 //total_gain, coefs1, mdctnorm are lossless
1056 coefs1 = s->coefs1[ch];
1057 exponents = s->exponents[ch];
1058 esize = s->exponents_bsize[ch];
1059 coefs = (*(s->coefs))[ch];
1060 n=0;
1063 * The calculation of coefs has a shift right by 2 built in. This
1064 * prepares samples for the Tremor IMDCT which uses a slightly
1065 * different fixed format then the ffmpeg one. If the old ffmpeg
1066 * imdct is used, each shift storing into coefs should be reduced
1067 * by 1.
1068 * See SVN logs for details.
1072 if (s->use_noise_coding)
1074 /*This case is only used for low bitrates (typically less then 32kbps)*/
1076 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1078 mult = fixdiv64(pow_table[total_gain+20],Fixed32To64(s->max_exponent[ch]));
1079 mult = mult* mdct_norm;
1080 mult1 = mult;
1082 /* very low freqs : noise */
1083 for(i = 0;i < s->coefs_start; ++i)
1085 *coefs++ = fixmul32( (fixmul32(s->noise_table[s->noise_index],
1086 exponents[i<<bsize>>esize])>>4),Fixed32From64(mult1)) >>2;
1087 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1090 n1 = s->exponent_high_sizes[bsize];
1092 /* compute power of high bands */
1093 exponents = s->exponents[ch] +(s->high_band_start[bsize]<<bsize);
1094 last_high_band = 0; /* avoid warning */
1095 for (j=0;j<n1;++j)
1097 n = s->exponent_high_bands[s->frame_len_bits -
1098 s->block_len_bits][j];
1099 if (s->high_band_coded[ch][j])
1101 fixed32 e2, v;
1102 e2 = 0;
1103 for(i = 0;i < n; ++i)
1105 /*v is normalized later on so its fixed format is irrelevant*/
1106 v = exponents[i<<bsize>>esize]>>4;
1107 e2 += fixmul32(v, v)>>3;
1109 exp_power[j] = e2/n; /*n is an int...*/
1110 last_high_band = j;
1112 exponents += n<<bsize;
1115 /* main freqs and high freqs */
1116 exponents = s->exponents[ch] + (s->coefs_start<<bsize);
1117 for(j=-1;j<n1;++j)
1119 if (j < 0)
1121 n = s->high_band_start[bsize] -
1122 s->coefs_start;
1124 else
1126 n = s->exponent_high_bands[s->frame_len_bits -
1127 s->block_len_bits][j];
1129 if (j >= 0 && s->high_band_coded[ch][j])
1131 /* use noise with specified power */
1132 fixed32 tmp = fixdiv32(exp_power[j],exp_power[last_high_band]);
1134 /*mult1 is 48.16, pow_table is 48.16*/
1135 mult1 = fixmul32(fixsqrt32(tmp),
1136 pow_table[s->high_band_values[ch][j]+20]) >> 16;
1138 /*this step has a fairly high degree of error for some reason*/
1139 mult1 = fixdiv64(mult1,fixmul32(s->max_exponent[ch],s->noise_mult));
1140 mult1 = mult1*mdct_norm>>PRECISION;
1141 for(i = 0;i < n; ++i)
1143 noise = s->noise_table[s->noise_index];
1144 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1145 *coefs++ = fixmul32((fixmul32(exponents[i<<bsize>>esize],noise)>>4),
1146 Fixed32From64(mult1)) >>2;
1149 exponents += n<<bsize;
1151 else
1153 /* coded values + small noise */
1154 for(i = 0;i < n; ++i)
1156 noise = s->noise_table[s->noise_index];
1157 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1159 /*don't forget to renormalize the noise*/
1160 temp1 = (((int32_t)*coefs1++)<<16) + (noise>>4);
1161 temp2 = fixmul32(exponents[i<<bsize>>esize], mult>>18);
1162 *coefs++ = fixmul32(temp1, temp2);
1164 exponents += n<<bsize;
1168 /* very high freqs : noise */
1169 n = s->block_len - s->coefs_end[bsize];
1170 mult2 = fixmul32(mult>>16,exponents[((-1<<bsize))>>esize]) ;
1171 for (i = 0; i < n; ++i)
1173 /*renormalize the noise product and then reduce to 14.18 precison*/
1174 *coefs++ = fixmul32(s->noise_table[s->noise_index],mult2) >>6;
1176 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1179 else
1181 /*Noise coding not used, simply convert from exp to fixed representation*/
1183 fixed32 mult3 = (fixed32)(fixdiv64(pow_table[total_gain+20],
1184 Fixed32To64(s->max_exponent[ch])));
1185 mult3 = fixmul32(mult3, mdct_norm);
1187 /*zero the first 3 coefficients for WMA V1, does nothing otherwise*/
1188 for(i=0; i<s->coefs_start; i++)
1189 *coefs++=0;
1191 n = nb_coefs[ch];
1193 /* XXX: optimize more, unrolling this loop in asm
1194 might be a good idea */
1196 for(i = 0;i < n; ++i)
1198 /*ffmpeg imdct needs 15.17, while tremor 14.18*/
1199 atemp = (coefs1[i] * mult3)>>2;
1200 *coefs++=fixmul32(atemp,exponents[i<<bsize>>esize]);
1202 n = s->block_len - s->coefs_end[bsize];
1203 memset(coefs, 0, n*sizeof(fixed32));
1210 if (s->ms_stereo && s->channel_coded[1])
1212 fixed32 a, b;
1213 int i;
1214 fixed32 (*coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE] = (s->coefs);
1216 /* nominal case for ms stereo: we do it before mdct */
1217 /* no need to optimize this case because it should almost
1218 never happen */
1219 if (!s->channel_coded[0])
1221 memset((*(s->coefs))[0], 0, sizeof(fixed32) * s->block_len);
1222 s->channel_coded[0] = 1;
1225 for(i = 0; i < s->block_len; ++i)
1227 a = (*coefs)[0][i];
1228 b = (*coefs)[1][i];
1229 (*coefs)[0][i] = a + b;
1230 (*coefs)[1][i] = a - b;
1234 for(ch = 0; ch < s->nb_channels; ++ch)
1236 /* BLOCK_MAX_SIZE is 2048 (samples) and MAX_CHANNELS is 2. */
1237 static uint32_t scratch_buf[BLOCK_MAX_SIZE * MAX_CHANNELS] IBSS_ATTR MEM_ALIGN_ATTR;
1238 if (s->channel_coded[ch])
1240 int n4, index;
1242 n4 = s->block_len >>1;
1244 ff_imdct_calc((s->frame_len_bits - bsize + 1),
1245 scratch_buf,
1246 (*(s->coefs))[ch]);
1248 /* add in the frame */
1249 index = (s->frame_len / 2) + s->block_pos - n4;
1250 wma_window(s, scratch_buf, &((*s->frame_out)[ch][index]));
1254 /* specific fast case for ms-stereo : add to second
1255 channel if it is not coded */
1256 if (s->ms_stereo && !s->channel_coded[1])
1258 wma_window(s, scratch_buf, &((*s->frame_out)[1][index]));
1262 next:
1263 /* update block number */
1264 ++s->block_num;
1265 s->block_pos += s->block_len;
1266 if (s->block_pos >= s->frame_len)
1268 return 1;
1270 else
1272 return 0;
1276 /* decode a frame of frame_len samples */
1277 static int wma_decode_frame(WMADecodeContext *s)
1279 int ret;
1281 /* read each block */
1282 s->block_num = 0;
1283 s->block_pos = 0;
1286 for(;;)
1288 ret = wma_decode_block(s);
1289 if (ret < 0)
1292 DEBUGF("wma_decode_block failed with code %d\n", ret);
1293 return -1;
1295 if (ret)
1297 break;
1301 return 0;
1304 /* Initialise the superframe decoding */
1306 int wma_decode_superframe_init(WMADecodeContext* s,
1307 const uint8_t *buf, /*input*/
1308 int buf_size)
1310 if (buf_size==0)
1312 s->last_superframe_len = 0;
1313 return 0;
1316 s->current_frame = 0;
1318 init_get_bits(&s->gb, buf, buf_size*8);
1320 if (s->use_bit_reservoir)
1322 /* read super frame header */
1323 skip_bits(&s->gb, 4); /* super frame index */
1324 s->nb_frames = get_bits(&s->gb, 4);
1326 if (s->last_superframe_len == 0)
1327 s->nb_frames --;
1328 else if (s->nb_frames == 0)
1329 s->nb_frames++;
1331 s->bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
1332 } else {
1333 s->nb_frames = 1;
1336 return 1;
1340 /* Decode a single frame in the current superframe - return -1 if
1341 there was a decoding error, or the number of samples decoded.
1344 int wma_decode_superframe_frame(WMADecodeContext* s,
1345 const uint8_t *buf, /*input*/
1346 int buf_size)
1348 int pos, len, ch;
1349 uint8_t *q;
1350 int done = 0;
1352 for(ch = 0; ch < s->nb_channels; ch++)
1353 memmove(&((*s->frame_out)[ch][0]),
1354 &((*s->frame_out)[ch][s->frame_len]),
1355 s->frame_len * sizeof(fixed32));
1357 if ((s->use_bit_reservoir) && (s->current_frame == 0))
1359 if (s->last_superframe_len > 0)
1361 /* add s->bit_offset bits to last frame */
1362 if ((s->last_superframe_len + ((s->bit_offset + 7) >> 3)) >
1363 MAX_CODED_SUPERFRAME_SIZE)
1365 DEBUGF("superframe size too large error\n");
1366 goto fail;
1368 q = s->last_superframe + s->last_superframe_len;
1369 len = s->bit_offset;
1370 while (len > 7)
1372 *q++ = (get_bits)(&s->gb, 8);
1373 len -= 8;
1375 if (len > 0)
1377 *q++ = (get_bits)(&s->gb, len) << (8 - len);
1380 /* XXX: s->bit_offset bits into last frame */
1381 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
1382 /* skip unused bits */
1383 if (s->last_bitoffset > 0)
1384 skip_bits(&s->gb, s->last_bitoffset);
1386 /* this frame is stored in the last superframe and in the
1387 current one */
1388 if (wma_decode_frame(s) < 0)
1390 goto fail;
1392 done = 1;
1395 /* read each frame starting from s->bit_offset */
1396 pos = s->bit_offset + 4 + 4 + s->byte_offset_bits + 3;
1397 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
1398 len = pos & 7;
1399 if (len > 0)
1400 skip_bits(&s->gb, len);
1402 s->reset_block_lengths = 1;
1405 /* If we haven't decoded a frame yet, do it now */
1406 if (!done)
1408 if (wma_decode_frame(s) < 0)
1410 goto fail;
1414 s->current_frame++;
1416 if ((s->use_bit_reservoir) && (s->current_frame == s->nb_frames))
1418 /* we copy the end of the frame in the last frame buffer */
1419 pos = get_bits_count(&s->gb) + ((s->bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
1420 s->last_bitoffset = pos & 7;
1421 pos >>= 3;
1422 len = buf_size - pos;
1423 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0)
1425 DEBUGF("superframe size too large error after decoding\n");
1426 goto fail;
1428 s->last_superframe_len = len;
1429 memcpy(s->last_superframe, buf + pos, len);
1432 return s->frame_len;
1434 fail:
1435 /* when error, we reset the bit reservoir */
1437 s->last_superframe_len = 0;
1438 return -1;