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
22 * WMA compatible decoder.
26 #include <codecs/lib/codeclib.h>
30 #include "bitstream.h"
33 #define VLCBITS 7 /*7 is the lowest without glitching*/
34 #define VLCMAX ((22+VLCBITS-1)/VLCBITS)
37 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
39 #define HGAINVLCBITS 9
40 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
45 void CMUL(fixed32
*x
, fixed32
*y
,
49 /* This version loses one bit of precision. Could be solved at the cost
50 * of 2 extra cycles if it becomes an issue. */
53 "smull %[l], %[y1], %[b], %[t] \n"
54 "smlal %[l], %[y1], %[a], %[v] \n"
55 "rsb %[b], %[b], #0 \n"
56 "smull %[l], %[x1], %[a], %[t] \n"
57 "smlal %[l], %[x1], %[b], %[v] \n"
58 : [l
] "=&r" (l
), [x1
]"=&r" (x1
), [y1
]"=&r" (y1
), [b
] "+r" (b
)
59 : [a
] "r" (a
), [t
] "r" (t
), [v
] "r" (v
)
65 #elif defined CPU_COLDFIRE
67 void CMUL(fixed32
*x
, fixed32
*y
,
71 asm volatile ("mac.l %[a], %[t], %%acc0;"
72 "msac.l %[b], %[v], %%acc0;"
73 "mac.l %[b], %[t], %%acc1;"
74 "mac.l %[a], %[v], %%acc1;"
75 "movclr.l %%acc0, %[a];"
76 "move.l %[a], (%[x]);"
77 "movclr.l %%acc1, %[a];"
78 "move.l %[a], (%[y]);"
80 : [x
] "a" (x
), [y
] "a" (y
),
81 [b
] "r" (b
), [t
] "r" (t
), [v
] "r" (v
)
85 // PJJ : reinstate macro
86 void CMUL(fixed32
*pre
,
98 fixed32 _r1
= fixmul32b(_bref
, _aref
);
99 fixed32 _r2
= fixmul32b(_bimf
, _aimf
);
100 fixed32 _r3
= fixmul32b(_bref
, _aimf
);
101 fixed32 _r4
= fixmul32b(_bimf
, _aref
);
108 typedef struct CoefVLCTable
110 int n
; /* total number of codes */
111 const uint32_t *huffcodes
; /* VLC bit values */
112 const uint8_t *huffbits
; /* VLC bit size */
113 const uint16_t *levels
; /* table to build run/level tables */
117 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
);
118 int fft_calc(FFTContext
*s
, FFTComplex
*z
);
121 fixed32 coefsarray
[MAX_CHANNELS
][BLOCK_MAX_SIZE
] IBSS_ATTR
;
123 //static variables that replace malloced stuff
124 fixed32 stat0
[2048], stat1
[1024], stat2
[512], stat3
[256], stat4
[128]; //these are the MDCT reconstruction windows
126 fixed32
*tcosarray
[5], *tsinarray
[5];
127 fixed32 tcos0
[1024], tcos1
[512], tcos2
[256], tcos3
[128], tcos4
[64]; //these are the sin and cos rotations used by the MDCT
128 fixed32 tsin0
[1024], tsin1
[512], tsin2
[256], tsin3
[128], tsin4
[64];
130 FFTComplex
*exparray
[5]; //these are the fft lookup tables
132 uint16_t *revarray
[5];
134 FFTComplex exptab0
[512] IBSS_ATTR
;
135 uint16_t revtab0
[1024];
137 uint16_t *runtabarray
[2], *levtabarray
[2]; //these are VLC lookup tables
139 uint16_t runtab0
[1336], runtab1
[1336], levtab0
[1336], levtab1
[1336]; //these could be made smaller since only one can be 1336
141 FFTComplex mdct_tmp
[1] ; /* dummy var */
144 /*putting these in IRAM actually makes PP slower*/
145 VLC_TYPE vlcbuf1
[2550][2];
146 VLC_TYPE vlcbuf2
[2550][2];
147 VLC_TYPE vlcbuf3
[360][2];
148 VLC_TYPE vlcbuf4
[540][2];
152 #include "wmadata.h" // PJJ
156 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
158 fixed32 ax, ay, bx, by;\
170 int fft_calc_unscaled(FFTContext
*s
, FFTComplex
*z
)
175 register FFTComplex
*p
, *q
;
176 // FFTComplex *exptab = s->exptab;
178 fixed32 tmp_re
, tmp_im
;
179 int tabshift
= 10-ln
;
190 BF(p
[0].re
, p
[0].im
, p
[1].re
, p
[1].im
,
191 p
[0].re
, p
[0].im
, p
[1].re
, p
[1].im
);
205 BF(p
[0].re
, p
[0].im
, p
[2].re
, p
[2].im
,
206 p
[0].re
, p
[0].im
, p
[2].re
, p
[2].im
);
207 BF(p
[1].re
, p
[1].im
, p
[3].re
, p
[3].im
,
208 p
[1].re
, p
[1].im
, -p
[3].im
, p
[3].re
);
217 BF(p
[0].re
, p
[0].im
, p
[2].re
, p
[2].im
,
218 p
[0].re
, p
[0].im
, p
[2].re
, p
[2].im
);
219 BF(p
[1].re
, p
[1].im
, p
[3].re
, p
[3].im
,
220 p
[1].re
, p
[1].im
, p
[3].im
, -p
[3].re
);
234 for (j
= 0; j
< nblocks
; ++j
)
236 BF(p
->re
, p
->im
, q
->re
, q
->im
,
237 p
->re
, p
->im
, q
->re
, q
->im
);
241 for(l
= nblocks
; l
< np2
; l
+= nblocks
)
243 CMUL(&tmp_re
, &tmp_im
, exptab0
[(l
<<tabshift
)].re
, exptab0
[(l
<<tabshift
)].im
, q
->re
, q
->im
);
244 //CMUL(&tmp_re, &tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
245 BF(p
->re
, p
->im
, q
->re
, q
->im
,
246 p
->re
, p
->im
, tmp_re
, tmp_im
);
254 nblocks
= nblocks
>> 1;
255 nloops
= nloops
<< 1;
257 while (nblocks
!= 0);
262 * init MDCT or IMDCT computation.
264 int ff_mdct_init(MDCTContext
*s
, int nbits
, int inverse
)
270 memset(s
, 0, sizeof(*s
));
271 n
= 1 << nbits
; //nbits ranges from 12 to 8 inclusive
275 s
->tcos
= tcosarray
[12-nbits
];
276 s
->tsin
= tsinarray
[12-nbits
];
279 //fixed32 pi2 = fixmul32(0x20000, M_PI_F);
280 fixed32 ip
= itofix32(i
) + 0x2000;
282 //ip = fixdiv32(ip,itofix32(n)); // PJJ optimize
283 //alpha = fixmul32(TWO_M_PI_F, ip);
284 //s->tcos[i] = -fixcos32(alpha); //alpha between 0 and pi/2
285 //s->tsin[i] = -fixsin32(alpha);
287 s
->tsin
[i
] = - fsincos(ip
<<16, &(s
->tcos
[i
])); //I can't remember why this works, but it seems to agree for ~24 bits, maybe more!
290 (&s
->fft
)->nbits
= nbits
-2;
292 (&s
->fft
)->inverse
= inverse
;
299 * Compute inverse MDCT of size N = 2^nbits
300 * @param output N samples
301 * @param input N/2 samples
302 * @param tmp N/2 samples
304 void ff_imdct_calc(MDCTContext
*s
,
308 int k
, n8
, n4
, n2
, n
, j
,scale
;
309 const fixed32
*tcos
= s
->tcos
;
310 const fixed32
*tsin
= s
->tsin
;
311 const fixed32
*in1
, *in2
;
312 FFTComplex
*z1
= (FFTComplex
*)output
;
313 FFTComplex
*z2
= (FFTComplex
*)input
;
314 int revtabshift
= 12 - s
->nbits
;
325 in2
= input
+ n2
- 1;
327 for(k
= 0; k
< n4
; k
++)
329 j
=revtab0
[k
<<revtabshift
];
330 CMUL(&z1
[j
].re
, &z1
[j
].im
, *in2
, *in1
, tcos
[k
], tsin
[k
]);
335 scale
= fft_calc_unscaled(&s
->fft
, z1
);
337 /* post rotation + reordering */
339 for(k
= 0; k
< n4
; k
++)
341 CMUL(&z2
[k
].re
, &z2
[k
].im
, (z1
[k
].re
), (z1
[k
].im
), tcos
[k
], tsin
[k
]);
344 for(k
= 0; k
< n8
; k
++)
346 fixed32 r1
,r2
,r3
,r4
,r1n
,r2n
,r3n
;
357 output
[n2
-1-2*k
] = r1
;
360 output
[n2
-1-2*k
-1] = r2n
;
362 output
[n2
+ 2*k
]= r3n
;
363 output
[n
-1- 2*k
]= r3n
;
365 output
[n2
+ 2*k
+1]= r4
;
366 output
[n
-2 - 2 * k
] = r4
;
376 * Helper functions for wma_window.
381 static inline void vector_fmul_add_add(fixed32
*dst
, const fixed32
*src0
, const fixed32
*src1
, int len
){
384 dst
[i
] = fixmul32b(src0
[i
], src1
[i
]) + dst
[i
];
387 static inline void vector_fmul_reverse(fixed32
*dst
, const fixed32
*src0
, const fixed32
*src1
, int len
){
391 dst
[i
] = fixmul32b(src0
[i
], src1
[-i
]);
395 * Apply MDCT window and add into output.
397 * We ensure that when the windows overlap their squared sum
398 * is always 1 (MDCT reconstruction rule).
400 static void wma_window(WMADecodeContext
*s
, fixed32
*in
, fixed32
*out
)
402 //float *in = s->output;
403 int block_len
, bsize
, n
;
406 if (s
->block_len_bits
<= s
->prev_block_len_bits
) {
407 block_len
= s
->block_len
;
408 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
410 vector_fmul_add_add(out
, in
, s
->windows
[bsize
], block_len
);
413 block_len
= 1 << s
->prev_block_len_bits
;
414 n
= (s
->block_len
- block_len
) / 2;
415 bsize
= s
->frame_len_bits
- s
->prev_block_len_bits
;
417 vector_fmul_add_add(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
419 memcpy(out
+n
+block_len
, in
+n
+block_len
, n
*sizeof(fixed32
));
426 if (s
->block_len_bits
<= s
->next_block_len_bits
) {
427 block_len
= s
->block_len
;
428 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
430 vector_fmul_reverse(out
, in
, s
->windows
[bsize
], block_len
);
433 block_len
= 1 << s
->next_block_len_bits
;
434 n
= (s
->block_len
- block_len
) / 2;
435 bsize
= s
->frame_len_bits
- s
->next_block_len_bits
;
437 memcpy(out
, in
, n
*sizeof(fixed32
));
439 vector_fmul_reverse(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
441 memset(out
+n
+block_len
, 0, n
*sizeof(fixed32
));
448 /* XXX: use same run/length optimization as mpeg decoders */
449 static void init_coef_vlc(VLC
*vlc
,
450 uint16_t **prun_table
, uint16_t **plevel_table
,
451 const CoefVLCTable
*vlc_table
, int tab
)
453 int n
= vlc_table
->n
;
454 const uint8_t *table_bits
= vlc_table
->huffbits
;
455 const uint32_t *table_codes
= vlc_table
->huffcodes
;
456 const uint16_t *levels_table
= vlc_table
->levels
;
457 uint16_t *run_table
, *level_table
;
462 init_vlc(vlc
, VLCBITS
, n
, table_bits
, 1, 1, table_codes
, 4, 4, 0);
464 run_table
= runtabarray
[tab
];
465 level_table
= levtabarray
[tab
];
476 level_table
[i
] = level
;
481 *prun_table
= run_table
;
482 *plevel_table
= level_table
;
485 int wma_decode_init(WMADecodeContext
* s
, asf_waveformatex_t
*wfx
)
487 //WMADecodeContext *s = avctx->priv_data;
488 int i
, m
, j
, flags1
, flags2
;
498 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
501 s
->sample_rate
= wfx
->rate
;
502 s
->nb_channels
= wfx
->channels
;
503 s
->bit_rate
= wfx
->bitrate
;
504 s
->block_align
= wfx
->blockalign
;
506 s
->coefs
= &coefsarray
;
508 if (wfx
->codec_id
== ASF_CODEC_ID_WMAV1
) {
510 } else if (wfx
->codec_id
== ASF_CODEC_ID_WMAV2
) {
513 /*one of those other wma flavors that don't have GPLed decoders */
517 /* extract flag infos */
520 extradata
= wfx
->data
;
521 if (s
->version
== 1 && wfx
->datalen
>= 4) {
522 flags1
= extradata
[0] | (extradata
[1] << 8);
523 flags2
= extradata
[2] | (extradata
[3] << 8);
524 }else if (s
->version
== 2 && wfx
->datalen
>= 6){
525 flags1
= extradata
[0] | (extradata
[1] << 8) |
526 (extradata
[2] << 16) | (extradata
[3] << 24);
527 flags2
= extradata
[4] | (extradata
[5] << 8);
529 s
->use_exp_vlc
= flags2
& 0x0001;
530 s
->use_bit_reservoir
= flags2
& 0x0002;
531 s
->use_variable_block_len
= flags2
& 0x0004;
533 /* compute MDCT block size */
534 if (s
->sample_rate
<= 16000){
535 s
->frame_len_bits
= 9;
536 }else if (s
->sample_rate
<= 22050 ||
537 (s
->sample_rate
<= 32000 && s
->version
== 1)){
538 s
->frame_len_bits
= 10;
540 s
->frame_len_bits
= 11;
542 s
->frame_len
= 1 << s
->frame_len_bits
;
543 if (s
-> use_variable_block_len
)
546 nb
= ((flags2
>> 3) & 3) + 1;
547 if ((s
->bit_rate
/ s
->nb_channels
) >= 32000)
551 nb_max
= s
->frame_len_bits
- BLOCK_MIN_BITS
; //max is 11-7
554 s
->nb_block_sizes
= nb
+ 1;
558 s
->nb_block_sizes
= 1;
561 /* init rate dependant parameters */
562 s
->use_noise_coding
= 1;
563 high_freq
= itofix64(s
->sample_rate
) >> 1;
566 /* if version 2, then the rates are normalized */
567 sample_rate1
= s
->sample_rate
;
570 if (sample_rate1
>= 44100)
571 sample_rate1
= 44100;
572 else if (sample_rate1
>= 22050)
573 sample_rate1
= 22050;
574 else if (sample_rate1
>= 16000)
575 sample_rate1
= 16000;
576 else if (sample_rate1
>= 11025)
577 sample_rate1
= 11025;
578 else if (sample_rate1
>= 8000)
582 fixed64 tmp
= itofix64(s
->bit_rate
);
583 fixed64 tmp2
= itofix64(s
->nb_channels
* s
->sample_rate
);
584 bps
= fixdiv64(tmp
, tmp2
);
585 fixed64 tim
= bps
* s
->frame_len
;
586 fixed64 tmpi
= fixdiv64(tim
,itofix64(8));
587 s
->byte_offset_bits
= av_log2(fixtoi64(tmpi
+0x8000)) + 2;
589 /* compute high frequency value and choose if noise coding should
592 if (s
->nb_channels
== 2)
593 bps1
= fixmul32(bps
,0x1999a);
594 if (sample_rate1
== 44100)
597 s
->use_noise_coding
= 0;
599 high_freq
= fixmul32(high_freq
,0x6666);
601 else if (sample_rate1
== 22050)
604 s
->use_noise_coding
= 0;
605 else if (bps1
>= 0xb852)
606 high_freq
= fixmul32(high_freq
,0xb333);
608 high_freq
= fixmul32(high_freq
,0x999a);
610 else if (sample_rate1
== 16000)
613 high_freq
= fixmul32(high_freq
,0x8000);
615 high_freq
= fixmul32(high_freq
,0x4ccd);
617 else if (sample_rate1
== 11025)
619 high_freq
= fixmul32(high_freq
,0xb333);
621 else if (sample_rate1
== 8000)
625 high_freq
= fixmul32(high_freq
,0x8000);
627 else if (bps
> 0xc000)
629 s
->use_noise_coding
= 0;
633 high_freq
= fixmul32(high_freq
,0xa666);
640 high_freq
= fixmul32(high_freq
,0xc000);
642 else if (bps
>= 0x999a)
644 high_freq
= fixmul32(high_freq
,0x999a);
648 high_freq
= fixmul32(high_freq
,0x8000);
652 /* compute the scale factor band sizes for each MDCT block size */
654 int a
, b
, pos
, lpos
, k
, block_len
, i
, j
, n
;
655 const uint8_t *table
;
665 for(k
= 0; k
< s
->nb_block_sizes
; ++k
)
667 block_len
= s
->frame_len
>> k
;
674 a
= wma_critical_freqs
[i
];
676 pos
= ((block_len
* 2 * a
) + (b
>> 1)) / b
;
679 s
->exponent_bands
[0][i
] = pos
- lpos
;
680 if (pos
>= block_len
)
687 s
->exponent_sizes
[0] = i
;
691 /* hardcoded tables */
693 a
= s
->frame_len_bits
- BLOCK_MIN_BITS
- k
;
696 if (s
->sample_rate
>= 44100)
697 table
= exponent_band_44100
[a
];
698 else if (s
->sample_rate
>= 32000)
699 table
= exponent_band_32000
[a
];
700 else if (s
->sample_rate
>= 22050)
701 table
= exponent_band_22050
[a
];
707 s
->exponent_bands
[k
][i
] = table
[i
];
708 s
->exponent_sizes
[k
] = n
;
716 a
= wma_critical_freqs
[i
];
718 pos
= ((block_len
* 2 * a
) + (b
<< 1)) / (4 * b
);
723 s
->exponent_bands
[k
][j
++] = pos
- lpos
;
724 if (pos
>= block_len
)
728 s
->exponent_sizes
[k
] = j
;
732 /* max number of coefs */
733 s
->coefs_end
[k
] = (s
->frame_len
- ((s
->frame_len
* 9) / 100)) >> k
;
734 /* high freq computation */
736 fixed32 tmp1
= high_freq
*2; /* high_freq is a fixed32!*/
737 fixed32 tmp2
=itofix32(s
->sample_rate
>>1);
738 s
->high_band_start
[k
] = fixtoi32( fixdiv32(tmp1
, tmp2
) * (block_len
>>1) +0x8000);
741 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
742 s->sample_rate + 0.5);*/
744 n
= s
->exponent_sizes
[k
];
751 pos
+= s
->exponent_bands
[k
][i
];
753 if (start
< s
->high_band_start
[k
])
754 start
= s
->high_band_start
[k
];
755 if (end
> s
->coefs_end
[k
])
756 end
= s
->coefs_end
[k
];
758 s
->exponent_high_bands
[k
][j
++] = end
- start
;
760 s
->exponent_high_sizes
[k
] = j
;
765 /*TODO: figure out how to fold this up into one array*/
766 tcosarray
[0] = tcos0
; tcosarray
[1] = tcos1
; tcosarray
[2] = tcos2
; tcosarray
[3] = tcos3
;tcosarray
[4] = tcos4
;
767 tsinarray
[0] = tsin0
; tsinarray
[1] = tsin1
; tsinarray
[2] = tsin2
; tsinarray
[3] = tsin3
;tsinarray
[4] = tsin4
;
769 /*these are folded up now*/
770 exparray
[0] = exptab0
; //exparray[1] = exptab1; exparray[2] = exptab2; exparray[3] = exptab3; exparray[4] = exptab4;
771 revarray
[0]=revtab0
; //revarray[1]=revtab1; revarray[2]=revtab2; revarray[3]=revtab3; revarray[4]=revtab4;
773 s
->mdct_tmp
= mdct_tmp
; /* temporary storage for imdct */
774 for(i
= 0; i
< s
->nb_block_sizes
; ++i
)
776 ff_mdct_init(&s
->mdct_ctx
[i
], s
->frame_len_bits
- i
+ 1, 1);
787 fixed32 ifix
= itofix32(i
);
788 fixed32 nfix
= itofix32(n
);
789 fixed32 res
= fixdiv32(ifix
,nfix
);
791 s1
= fsincos(res
<<16, &c1
);
794 exptab0
[i
].im
= s1
*s2
;
798 /* init the MDCT bit reverse table here rather then in fft_init */
800 for(i
=0;i
<1024;i
++) /*hard coded to a 2048 bit rotation*/
801 { /*smaller sizes can reuse the largest*/
805 m
|= ((i
>> j
) & 1) << (10-j
-1);
811 /*ffmpeg uses malloc to only allocate as many window sizes as needed. However, we're really only interested in the worst case memory usage.
812 * In the worst case you can have 5 window sizes, 128 doubling up 2048
813 * Smaller windows are handled differently.
814 * Since we don't have malloc, just statically allocate this
823 /* init MDCT windows : simple sinus window */
824 for(i
= 0; i
< s
->nb_block_sizes
; i
++)
828 n
= 1 << (s
->frame_len_bits
- i
);
829 //window = av_malloc(sizeof(fixed32) * n);
832 //fixed32 n2 = itofix32(n<<1); //2x the window length
833 //alpha = fixdiv32(M_PI_F, n2); //PI / (2x Window length) == PI<<(s->frame_len_bits - i+1)
835 //alpha = M_PI_F>>(s->frame_len_bits - i+1);
836 alpha
= (1<<15)>>(s
->frame_len_bits
- i
+1); /* this calculates 0.5/(2*n) */
839 fixed32 j2
= itofix32(j
) + 0x8000;
840 window
[j
] = fsincos(fixmul32(j2
,alpha
)<<16, 0); //alpha between 0 and pi/2
843 //printf("created window\n");
844 s
->windows
[i
] = window
;
845 //printf("assigned window\n");
848 s
->reset_block_lengths
= 1;
850 if (s
->use_noise_coding
)
852 /* init the noise generator */
855 s
->noise_mult
= 0x51f;
856 s
->noise_table
= noisetable_exp
;
860 s
->noise_mult
= 0xa3d;
861 /* LSP values are simply 2x the EXP values */
862 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
863 noisetable_exp
[i
] = noisetable_exp
[i
]<< 1;
864 s
->noise_table
= noisetable_exp
;
871 norm
= 0; // PJJ: near as makes any diff to 0!
872 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
874 seed
= seed
* 314159 + 1;
875 s
->noise_table
[i
] = itofix32((int)seed
) * norm
;
880 s
->hgain_vlc
.table
= vlcbuf4
;
881 init_vlc(&s
->hgain_vlc
, HGAINVLCBITS
, sizeof(hgain_huffbits
),
882 hgain_huffbits
, 1, 1,
883 hgain_huffcodes
, 2, 2, 0);
889 s
->exp_vlc
.table
= vlcbuf3
;
891 init_vlc(&s
->exp_vlc
, EXPVLCBITS
, sizeof(scale_huffbits
),
892 scale_huffbits
, 1, 1,
893 scale_huffcodes
, 4, 4, 0);
897 wma_lsp_to_curve_init(s
, s
->frame_len
);
900 /* choose the VLC tables for the coefficients */
902 if (s
->sample_rate
>= 32000)
906 else if (bps1
< 0x128f6)
910 runtabarray
[0] = runtab0
; runtabarray
[1] = runtab1
;
911 levtabarray
[0] = levtab0
; levtabarray
[1] = levtab1
;
913 s
->coef_vlc
[0].table
= vlcbuf1
;
914 s
->coef_vlc
[0].table_allocated
= 24576/4;
915 s
->coef_vlc
[1].table
= vlcbuf2
;
916 s
->coef_vlc
[1].table_allocated
= 14336/4;
919 init_coef_vlc(&s
->coef_vlc
[0], &s
->run_table
[0], &s
->level_table
[0],
920 &coef_vlcs
[coef_vlc_table
* 2], 0);
921 init_coef_vlc(&s
->coef_vlc
[1], &s
->run_table
[1], &s
->level_table
[1],
922 &coef_vlcs
[coef_vlc_table
* 2 + 1], 1);
924 s
->last_superframe_len
= 0;
925 s
->last_bitoffset
= 0;
931 /* compute x^-0.25 with an exponent and mantissa table. We use linear
932 interpolation to reduce the mantissa table size at a small speed
933 expense (linear interpolation approximately doubles the number of
934 bits of precision). */
935 static inline fixed32
pow_m1_4(WMADecodeContext
*s
, fixed32 x
)
946 m
= (u
.v
>> (23 - LSP_POW_BITS
)) & ((1 << LSP_POW_BITS
) - 1);
947 /* build interpolation scale: 1 <= t < 2. */
948 t
.v
= ((u
.v
<< LSP_POW_BITS
) & ((1 << 23) - 1)) | (127 << 23);
949 a
= s
->lsp_pow_m_table1
[m
];
950 b
= s
->lsp_pow_m_table2
[m
];
952 /* lsp_pow_e_table contains 32.32 format */
953 /* TODO: Since we're unlikely have value that cover the whole
954 * IEEE754 range, we probably don't need to have all possible exponents */
956 return (lsp_pow_e_table
[e
] * (a
+ fixmul32(b
, ftofix32(t
.f
))) >>32);
959 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
)
961 fixed32 wdel
, a
, b
, temp
, temp2
;
964 wdel
= fixdiv32(M_PI_F
, itofix32(frame_len
));
965 temp
= fixdiv32(itofix32(1), itofix32(frame_len
));
966 for (i
=0; i
<frame_len
; ++i
)
968 /* TODO: can probably reuse the trig_init values here */
969 fsincos((temp
*i
)<<15, &temp2
);
970 /* get 3 bits headroom + 1 bit from not doubleing the values */
971 s
->lsp_cos_table
[i
] = temp2
>>3;
974 /* NOTE: these two tables are needed to avoid two operations in
979 /*double check this later*/
980 for(i
=(1 << LSP_POW_BITS
) - 1;i
>=0;i
--)
982 m
= (1 << LSP_POW_BITS
) + i
;
983 a
= pow_a_table
[ix
++]<<4;
984 s
->lsp_pow_m_table1
[i
] = 2 * a
- b
;
985 s
->lsp_pow_m_table2
[i
] = b
- a
;
991 /* NOTE: We use the same code as Vorbis here */
992 /* XXX: optimize it further with SSE/3Dnow */
993 static void wma_lsp_to_curve(WMADecodeContext
*s
,
995 fixed32
*val_max_ptr
,
1000 fixed32 p
, q
, w
, v
, val_max
, temp
, temp2
;
1005 /* shift by 2 now to reduce rounding error,
1006 * we can renormalize right before pow_m1_4
1011 w
= s
->lsp_cos_table
[i
];
1013 for (j
=1;j
<NB_LSP_COEFS
;j
+=2)
1015 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
1016 temp2
= ((w
- (lsp
[j
- 1]<<11)));
1019 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
1020 q
= fixmul32b(q
, temp2
)<<4;
1021 p
= fixmul32b(p
, (w
- (lsp
[j
]<<11)))<<4;
1024 /* 2 in 5.27 format is 0x10000000 */
1025 p
= fixmul32(p
, fixmul32b(p
, (0x10000000 - w
)))<<3;
1026 q
= fixmul32(q
, fixmul32b(q
, (0x10000000 + w
)))<<3;
1028 v
= (p
+ q
) >>9; /* p/q end up as 16.16 */
1035 *val_max_ptr
= val_max
;
1038 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
1039 static void decode_exp_lsp(WMADecodeContext
*s
, int ch
)
1041 fixed32 lsp_coefs
[NB_LSP_COEFS
];
1044 for (i
= 0; i
< NB_LSP_COEFS
; ++i
)
1046 if (i
== 0 || i
>= 8)
1047 val
= get_bits(&s
->gb
, 3);
1049 val
= get_bits(&s
->gb
, 4);
1050 lsp_coefs
[i
] = lsp_codebook
[i
][val
];
1055 &s
->max_exponent
[ch
],
1060 /* decode exponents coded with VLC codes */
1061 static int decode_exp_vlc(WMADecodeContext
*s
, int ch
)
1063 int last_exp
, n
, code
;
1064 const uint16_t *ptr
, *band_ptr
;
1065 fixed32 v
, max_scale
;
1068 band_ptr
= s
->exponent_bands
[s
->frame_len_bits
- s
->block_len_bits
];
1070 q
= s
->exponents
[ch
];
1071 q_end
= q
+ s
->block_len
;
1075 if (s
->version
== 1) //wmav1 only
1077 last_exp
= get_bits(&s
->gb
, 5) + 10;
1078 /* XXX: use a table */
1079 v
= pow_10_to_yover16
[last_exp
];
1092 code
= get_vlc2(&s
->gb
, s
->exp_vlc
.table
, EXPVLCBITS
, EXPMAX
);
1097 /* NOTE: this offset is the same as MPEG4 AAC ! */
1098 last_exp
+= code
- 60;
1099 /* XXX: use a table */
1100 v
= pow_10_to_yover16
[last_exp
];
1114 s
->max_exponent
[ch
] = max_scale
;
1118 /* return 0 if OK. return 1 if last block of frame. return -1 if
1119 unrecorrable error. */
1120 static int wma_decode_block(WMADecodeContext
*s
)
1122 int n
, v
, a
, ch
, code
, bsize
;
1123 int coef_nb_bits
, total_gain
;
1124 int nb_coefs
[MAX_CHANNELS
];
1127 // printf("***decode_block: %d:%d (%d)\n", s->frame_count - 1, s->block_num, s->block_len);
1129 /* compute current block length */
1130 if (s
->use_variable_block_len
)
1132 n
= av_log2(s
->nb_block_sizes
- 1) + 1;
1134 if (s
->reset_block_lengths
)
1136 s
->reset_block_lengths
= 0;
1137 v
= get_bits(&s
->gb
, n
);
1138 if (v
>= s
->nb_block_sizes
)
1142 s
->prev_block_len_bits
= s
->frame_len_bits
- v
;
1143 v
= get_bits(&s
->gb
, n
);
1144 if (v
>= s
->nb_block_sizes
)
1148 s
->block_len_bits
= s
->frame_len_bits
- v
;
1152 /* update block lengths */
1153 s
->prev_block_len_bits
= s
->block_len_bits
;
1154 s
->block_len_bits
= s
->next_block_len_bits
;
1156 v
= get_bits(&s
->gb
, n
);
1158 if (v
>= s
->nb_block_sizes
)
1160 // rb->splash(HZ*4, "v was %d", v); //5, 7
1161 return -4; //this is it
1164 //rb->splash(HZ, "passed v block (%d)!", v);
1166 s
->next_block_len_bits
= s
->frame_len_bits
- v
;
1170 /* fixed block len */
1171 s
->next_block_len_bits
= s
->frame_len_bits
;
1172 s
->prev_block_len_bits
= s
->frame_len_bits
;
1173 s
->block_len_bits
= s
->frame_len_bits
;
1175 /* now check if the block length is coherent with the frame length */
1176 s
->block_len
= 1 << s
->block_len_bits
;
1178 if ((s
->block_pos
+ s
->block_len
) > s
->frame_len
)
1183 if (s
->nb_channels
== 2)
1185 s
->ms_stereo
= get_bits(&s
->gb
, 1);
1188 for (ch
= 0; ch
< s
->nb_channels
; ++ch
)
1190 a
= get_bits(&s
->gb
, 1);
1191 s
->channel_coded
[ch
] = a
;
1194 /* if no channel coded, no need to go further */
1195 /* XXX: fix potential framing problems */
1201 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
1203 /* read total gain and extract corresponding number of bits for
1204 coef escape coding */
1208 a
= get_bits(&s
->gb
, 7);
1216 if (total_gain
< 15)
1218 else if (total_gain
< 32)
1220 else if (total_gain
< 40)
1222 else if (total_gain
< 45)
1227 /* compute number of coefficients */
1228 n
= s
->coefs_end
[bsize
] - s
->coefs_start
;
1230 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1234 /* complex coding */
1235 if (s
->use_noise_coding
)
1238 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1240 if (s
->channel_coded
[ch
])
1243 n
= s
->exponent_high_sizes
[bsize
];
1246 a
= get_bits(&s
->gb
, 1);
1247 s
->high_band_coded
[ch
][i
] = a
;
1248 /* if noise coding, the coefficients are not transmitted */
1250 nb_coefs
[ch
] -= s
->exponent_high_bands
[bsize
][i
];
1254 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1256 if (s
->channel_coded
[ch
])
1258 int i
, n
, val
, code
;
1260 n
= s
->exponent_high_sizes
[bsize
];
1261 val
= (int)0x80000000;
1264 if (s
->high_band_coded
[ch
][i
])
1266 if (val
== (int)0x80000000)
1268 val
= get_bits(&s
->gb
, 7) - 19;
1272 //code = get_vlc(&s->gb, &s->hgain_vlc);
1273 code
= get_vlc2(&s
->gb
, s
->hgain_vlc
.table
, HGAINVLCBITS
, HGAINMAX
);
1280 s
->high_band_values
[ch
][i
] = val
;
1287 /* exponents can be reused in short blocks. */
1288 if ((s
->block_len_bits
== s
->frame_len_bits
) || get_bits(&s
->gb
, 1))
1290 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1292 if (s
->channel_coded
[ch
])
1296 if (decode_exp_vlc(s
, ch
) < 0)
1303 decode_exp_lsp(s
, ch
);
1305 s
->exponents_bsize
[ch
] = bsize
;
1310 /* parse spectral coefficients : just RLE encoding */
1311 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1313 if (s
->channel_coded
[ch
])
1316 int level
, run
, sign
, tindex
;
1317 int16_t *ptr
, *eptr
;
1318 const int16_t *level_table
, *run_table
;
1320 /* special VLC tables are used for ms stereo because
1321 there is potentially less energy there */
1322 tindex
= (ch
== 1 && s
->ms_stereo
);
1323 coef_vlc
= &s
->coef_vlc
[tindex
];
1324 run_table
= s
->run_table
[tindex
];
1325 level_table
= s
->level_table
[tindex
];
1327 ptr
= &s
->coefs1
[ch
][0];
1328 eptr
= ptr
+ nb_coefs
[ch
];
1329 memset(ptr
, 0, s
->block_len
* sizeof(int16_t));
1333 code
= get_vlc2(&s
->gb
, coef_vlc
->table
, VLCBITS
, VLCMAX
);
1334 //code = get_vlc(&s->gb, coef_vlc);
1347 level
= get_bits(&s
->gb
, coef_nb_bits
);
1348 /* NOTE: this is rather suboptimal. reading
1349 block_len_bits would be better */
1350 run
= get_bits(&s
->gb
, s
->frame_len_bits
);
1355 run
= run_table
[code
];
1356 level
= level_table
[code
];
1358 sign
= get_bits(&s
->gb
, 1);
1369 /* NOTE: EOB can be omitted */
1374 if (s
->version
== 1 && s
->nb_channels
>= 2)
1376 align_get_bits(&s
->gb
);
1381 int n4
= s
->block_len
>> 1;
1382 //mdct_norm = 0x10000;
1383 //mdct_norm = fixdiv32(mdct_norm,itofix32(n4));
1385 mdct_norm
= 0x10000>>(s
->block_len_bits
-1); //theres no reason to do a divide by two in fixed precision ...
1387 if (s
->version
== 1)
1389 fixed32 tmp
= fixsqrt32(itofix32(n4
));
1390 mdct_norm
*= tmp
; // PJJ : exercise this path
1395 /* finally compute the MDCT coefficients */
1396 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1398 if (s
->channel_coded
[ch
])
1401 fixed32
*exponents
, *exp_ptr
;
1402 fixed32
*coefs
, atemp
;
1405 fixed32 noise
, temp1
, temp2
, mult2
;
1406 int i
, j
, n
, n1
, last_high_band
, esize
;
1407 fixed32 exp_power
[HIGH_BAND_MAX_SIZE
];
1409 //total_gain, coefs1, mdctnorm are lossless
1411 coefs1
= s
->coefs1
[ch
];
1412 exponents
= s
->exponents
[ch
];
1413 esize
= s
->exponents_bsize
[ch
];
1414 mult
= fixdiv64(pow_table
[total_gain
+20],Fixed32To64(s
->max_exponent
[ch
]));
1415 mult
= fixmul64byfixed(mult
, mdct_norm
); //what the hell? This is actually fixed64*2^16!
1416 coefs
= (*(s
->coefs
))[ch
];
1420 if (s
->use_noise_coding
)
1424 /* very low freqs : noise */
1425 for(i
= 0;i
< s
->coefs_start
; ++i
)
1427 *coefs
++ = fixmul32( (fixmul32(s
->noise_table
[s
->noise_index
],(*exponents
++))>>4),Fixed32From64(mult1
)) >>1;
1428 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1431 n1
= s
->exponent_high_sizes
[bsize
];
1433 /* compute power of high bands */
1434 exp_ptr
= exponents
+
1435 s
->high_band_start
[bsize
] -
1437 last_high_band
= 0; /* avoid warning */
1440 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1441 s
->block_len_bits
][j
];
1442 if (s
->high_band_coded
[ch
][j
])
1446 for(i
= 0;i
< n
; ++i
)
1448 /*v is noramlized later on so its fixed format is irrelevant*/
1450 e2
+= fixmul32(v
, v
)>>3;
1452 exp_power
[j
] = e2
/n
; /*n is an int...*/
1458 /* main freqs and high freqs */
1463 n
= s
->high_band_start
[bsize
] -
1468 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1469 s
->block_len_bits
][j
];
1471 if (j
>= 0 && s
->high_band_coded
[ch
][j
])
1473 /* use noise with specified power */
1474 fixed32 tmp
= fixdiv32(exp_power
[j
],exp_power
[last_high_band
]);
1475 mult1
= (fixed64
)fixsqrt32(tmp
);
1476 /* XXX: use a table */
1477 /*mult1 is 48.16, pow_table is 48.16*/
1478 mult1
= mult1
* pow_table
[s
->high_band_values
[ch
][j
]+20] >> PRECISION
;
1480 /*this step has a fairly high degree of error for some reason*/
1481 mult1
= fixdiv64(mult1
,fixmul32(s
->max_exponent
[ch
],s
->noise_mult
));
1483 mult1
= mult1
*mdct_norm
>>PRECISION
;
1484 for(i
= 0;i
< n
; ++i
)
1486 noise
= s
->noise_table
[s
->noise_index
];
1487 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1488 *coefs
++ = fixmul32((fixmul32(*exponents
,noise
)>>4),Fixed32From64(mult1
)) >>1;
1494 /* coded values + small noise */
1495 for(i
= 0;i
< n
; ++i
)
1497 // PJJ: check code path
1498 noise
= s
->noise_table
[s
->noise_index
];
1499 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1501 /*don't forget to renormalize the noise*/
1502 temp1
= (((int32_t)*coefs1
++)<<16) + (noise
>>4);
1503 temp2
= fixmul32(*exponents
, mult
>>17);
1504 *coefs
++ = fixmul32(temp1
, temp2
);
1510 /* very high freqs : noise */
1511 n
= s
->block_len
- s
->coefs_end
[bsize
];
1512 mult2
= fixmul32(mult
>>16,exponents
[-1]) ; /*the work around for 32.32 vars are getting stupid*/
1513 for (i
= 0; i
< n
; ++i
)
1515 /*renormalize the noise product and then reduce to 17.15 precison*/
1516 *coefs
++ = fixmul32(s
->noise_table
[s
->noise_index
],mult2
) >>5;
1518 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1524 /* XXX: optimize more */
1528 for(i
= 0;i
< n
; ++i
)
1531 * Previously the IMDCT was run in 17.15 precision to avoid overflow. However rare files could
1532 * overflow here as well, so switch to 17.15 now. As a bonus, this saves us a shift later on.
1536 atemp
= (fixed32
)(coefs1
[i
]*mult
>>17);
1537 //this "works" in the sense that the mdcts converge
1538 //atemp= ftofix32(coefs1[i] * fixtof64(exponents[i]) * fixtof64(mult>>16));
1540 *coefs
++=fixmul32(atemp
,exponents
[i
<<bsize
>>esize
]);
1543 n
= s
->block_len
- s
->coefs_end
[bsize
];
1544 for(i
= 0;i
< n
; ++i
)
1552 if (s
->ms_stereo
&& s
->channel_coded
[1])
1556 fixed32 (*coefs
)[MAX_CHANNELS
][BLOCK_MAX_SIZE
] = (s
->coefs
);
1558 /* nominal case for ms stereo: we do it before mdct */
1559 /* no need to optimize this case because it should almost
1561 if (!s
->channel_coded
[0])
1563 memset((*(s
->coefs
))[0], 0, sizeof(fixed32
) * s
->block_len
);
1564 s
->channel_coded
[0] = 1;
1567 for(i
= 0; i
< s
->block_len
; ++i
)
1571 (*coefs
)[0][i
] = a
+ b
;
1572 (*coefs
)[1][i
] = a
- b
;
1576 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1578 if (s
->channel_coded
[ch
])
1580 static fixed32 output
[BLOCK_MAX_SIZE
* 2] IBSS_ATTR
;
1585 n4
= s
->block_len
>>1;
1587 ff_imdct_calc(&s
->mdct_ctx
[bsize
],
1592 /* add in the frame */
1593 index
= (s
->frame_len
/ 2) + s
->block_pos
- n4
;
1595 wma_window(s
, output
, &s
->frame_out
[ch
][index
]);
1599 /* specific fast case for ms-stereo : add to second
1600 channel if it is not coded */
1601 if (s
->ms_stereo
&& !s
->channel_coded
[1])
1603 wma_window(s
, output
, &s
->frame_out
[1][index
]);
1608 /* update block number */
1610 s
->block_pos
+= s
->block_len
;
1611 if (s
->block_pos
>= s
->frame_len
)
1621 /* decode a frame of frame_len samples */
1622 static int wma_decode_frame(WMADecodeContext
*s
, int16_t *samples
)
1624 int ret
, i
, n
, a
, ch
, incr
;
1627 // rb->splash(HZ, "in wma_decode_frame");
1629 /* read each block */
1636 ret
= wma_decode_block(s
);
1640 //rb->splash(HZ*4, "wma_decode_block failed with ret %d", ret);
1649 /* convert frame to integer */
1651 incr
= s
->nb_channels
;
1652 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1655 iptr
= s
->frame_out
[ch
];
1659 a
= fixtoi32(*iptr
++)<<1; //ugly but good enough for now
1665 else if (a
< -32768)
1672 /* prepare for next block */
1673 memmove(&s
->frame_out
[ch
][0], &s
->frame_out
[ch
][s
->frame_len
],
1674 s
->frame_len
* sizeof(fixed32
));
1681 /* Initialise the superframe decoding */
1683 int wma_decode_superframe_init(WMADecodeContext
* s
,
1684 uint8_t *buf
, /*input*/
1689 s
->last_superframe_len
= 0;
1693 s
->current_frame
= 0;
1695 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1697 if (s
->use_bit_reservoir
)
1699 /* read super frame header */
1700 get_bits(&s
->gb
, 4); /* super frame index */
1701 s
->nb_frames
= get_bits(&s
->gb
, 4);
1703 if (s
->last_superframe_len
== 0)
1705 else if (s
->nb_frames
== 0)
1708 s
->bit_offset
= get_bits(&s
->gb
, s
->byte_offset_bits
+ 3);
1717 /* Decode a single frame in the current superframe - return -1 if
1718 there was a decoding error, or the number of samples decoded.
1721 int wma_decode_superframe_frame(WMADecodeContext
* s
,
1722 int16_t* samples
, /*output*/
1723 uint8_t *buf
, /*input*/
1730 if ((s
->use_bit_reservoir
) && (s
->current_frame
== 0))
1732 if (s
->last_superframe_len
> 0)
1734 /* add s->bit_offset bits to last frame */
1735 if ((s
->last_superframe_len
+ ((s
->bit_offset
+ 7) >> 3)) >
1736 MAX_CODED_SUPERFRAME_SIZE
)
1740 q
= s
->last_superframe
+ s
->last_superframe_len
;
1741 len
= s
->bit_offset
;
1744 *q
++ = (get_bits
)(&s
->gb
, 8);
1749 *q
++ = (get_bits
)(&s
->gb
, len
) << (8 - len
);
1752 /* XXX: s->bit_offset bits into last frame */
1753 init_get_bits(&s
->gb
, s
->last_superframe
, MAX_CODED_SUPERFRAME_SIZE
*8);
1754 /* skip unused bits */
1755 if (s
->last_bitoffset
> 0)
1756 skip_bits(&s
->gb
, s
->last_bitoffset
);
1758 /* this frame is stored in the last superframe and in the
1760 if (wma_decode_frame(s
, samples
) < 0)
1767 /* read each frame starting from s->bit_offset */
1768 pos
= s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3;
1769 init_get_bits(&s
->gb
, buf
+ (pos
>> 3), (MAX_CODED_SUPERFRAME_SIZE
- (pos
>> 3))*8);
1772 skip_bits(&s
->gb
, len
);
1774 s
->reset_block_lengths
= 1;
1777 /* If we haven't decoded a frame yet, do it now */
1780 if (wma_decode_frame(s
, samples
) < 0)
1788 if ((s
->use_bit_reservoir
) && (s
->current_frame
== s
->nb_frames
))
1790 /* we copy the end of the frame in the last frame buffer */
1791 pos
= get_bits_count(&s
->gb
) + ((s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3) & ~7);
1792 s
->last_bitoffset
= pos
& 7;
1794 len
= buf_size
- pos
;
1795 if (len
> MAX_CODED_SUPERFRAME_SIZE
|| len
< 0)
1799 s
->last_superframe_len
= len
;
1800 memcpy(s
->last_superframe
, buf
+ pos
, len
);
1803 return s
->frame_len
;
1806 /* when error, we reset the bit reservoir */
1807 s
->last_superframe_len
= 0;