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)
43 typedef struct CoefVLCTable
45 int n
; /* total number of codes */
46 const uint32_t *huffcodes
; /* VLC bit values */
47 const uint8_t *huffbits
; /* VLC bit size */
48 const uint16_t *levels
; /* table to build run/level tables */
52 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
);
54 fixed32 coefsarray
[MAX_CHANNELS
][BLOCK_MAX_SIZE
] IBSS_ATTR
;
55 /*decode and window into IRAM on targets with at least 80KB of codec IRAM*/
56 fixed32 frame_out_buf
[MAX_CHANNELS
][BLOCK_MAX_SIZE
* 2] IBSS_ATTR_WMA_LARGE_IRAM
;
58 //static variables that replace malloced stuff
59 fixed32 stat0
[2048], stat1
[1024], stat2
[512], stat3
[256], stat4
[128]; //these are the MDCT reconstruction windows
61 uint16_t *runtabarray
[2], *levtabarray
[2]; //these are VLC lookup tables
63 uint16_t runtab0
[1336], runtab1
[1336], levtab0
[1336], levtab1
[1336]; //these could be made smaller since only one can be 1336
65 #define VLCBUF1SIZE 4598
66 #define VLCBUF2SIZE 3574
67 #define VLCBUF3SIZE 360
68 #define VLCBUF4SIZE 540
70 /*putting these in IRAM actually makes PP slower*/
72 VLC_TYPE vlcbuf1
[VLCBUF1SIZE
][2];
73 VLC_TYPE vlcbuf2
[VLCBUF2SIZE
][2];
74 VLC_TYPE vlcbuf3
[VLCBUF3SIZE
][2];
75 VLC_TYPE vlcbuf4
[VLCBUF4SIZE
][2];
79 #include "wmadata.h" // PJJ
84 * Helper functions for wma_window.
91 void vector_fmul_add_add(fixed32
*dst
, const fixed32
*data
,
92 const fixed32
*window
, int n
)
94 /* Block sizes are always power of two */
97 "ldmia %[d]!, {r0, r1};"
98 "ldmia %[w]!, {r4, r5};"
99 /* consume the first data and window value so we can use those
101 "smull r8, r9, r0, r4;"
102 "ldmia %[dst], {r0, r4};"
103 "add r0, r0, r9, lsl #1;" /* *dst=*dst+(r9<<1)*/
104 "smull r8, r9, r1, r5;"
105 "add r1, r4, r9, lsl #1;"
106 "stmia %[dst]!, {r0, r1};"
107 "subs %[n], %[n], #2;"
109 : [d
] "+r" (data
), [w
] "+r" (window
), [dst
] "+r" (dst
), [n
] "+r" (n
)
110 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
114 void vector_fmul_reverse(fixed32
*dst
, const fixed32
*src0
, const fixed32
*src1
,
117 /* Block sizes are always power of two */
119 "add %[s1], %[s1], %[n], lsl #2;"
121 "ldmia %[s0]!, {r0, r1};"
122 "ldmdb %[s1]!, {r4, r5};"
123 "smull r8, r9, r0, r5;"
124 "mov r0, r9, lsl #1;"
125 "smull r8, r9, r1, r4;"
126 "mov r1, r9, lsl #1;"
127 "stmia %[dst]!, {r0, r1};"
128 "subs %[n], %[n], #2;"
130 : [s0
] "+r" (src0
), [s1
] "+r" (src1
), [dst
] "+r" (dst
), [n
] "+r" (len
)
131 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
134 #elif defined(CPU_COLDFIRE)
137 void vector_fmul_add_add(fixed32
*dst
, const fixed32
*data
,
138 const fixed32
*window
, int n
)
140 /* Block sizes are always power of two. Smallest block is always way bigger
144 "movem.l (%[d]), %%d0-%%d3;"
145 "movem.l (%[w]), %%d4-%%d5/%%a0-%%a1;"
146 "mac.l %%d0, %%d4, %%acc0;"
147 "mac.l %%d1, %%d5, %%acc1;"
148 "mac.l %%d2, %%a0, %%acc2;"
149 "mac.l %%d3, %%a1, %%acc3;"
150 "lea.l (16, %[d]), %[d];"
151 "lea.l (16, %[w]), %[w];"
152 "movclr.l %%acc0, %%d0;"
153 "movclr.l %%acc1, %%d1;"
154 "movclr.l %%acc2, %%d2;"
155 "movclr.l %%acc3, %%d3;"
156 "movem.l (%[dst]), %%d4-%%d5/%%a0-%%a1;"
161 "movem.l %%d0-%%d3, (%[dst]);"
162 "lea.l (16, %[dst]), %[dst];"
165 : [d
] "+a" (data
), [w
] "+a" (window
), [dst
] "+a" (dst
), [n
] "+d" (n
)
166 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
170 void vector_fmul_reverse(fixed32
*dst
, const fixed32
*src0
, const fixed32
*src1
,
173 /* Block sizes are always power of two. Smallest block is always way bigger
176 "lea.l (-16, %[s1], %[n]*4), %[s1];"
178 "movem.l (%[s0]), %%d0-%%d3;"
179 "movem.l (%[s1]), %%d4-%%d5/%%a0-%%a1;"
180 "mac.l %%d0, %%a1, %%acc0;"
181 "mac.l %%d1, %%a0, %%acc1;"
182 "mac.l %%d2, %%d5, %%acc2;"
183 "mac.l %%d3, %%d4, %%acc3;"
184 "lea.l (16, %[s0]), %[s0];"
185 "lea.l (-16, %[s1]), %[s1];"
186 "movclr.l %%acc0, %%d0;"
187 "movclr.l %%acc1, %%d1;"
188 "movclr.l %%acc2, %%d2;"
189 "movclr.l %%acc3, %%d3;"
190 "movem.l %%d0-%%d3, (%[dst]);"
191 "lea.l (16, %[dst]), %[dst];"
194 : [s0
] "+a" (src0
), [s1
] "+a" (src1
), [dst
] "+a" (dst
), [n
] "+d" (len
)
195 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
200 static inline void vector_fmul_add_add(fixed32
*dst
, const fixed32
*src0
, const fixed32
*src1
, int len
){
203 dst
[i
] = fixmul32b(src0
[i
], src1
[i
]) + dst
[i
];
206 static inline void vector_fmul_reverse(fixed32
*dst
, const fixed32
*src0
, const fixed32
*src1
, int len
){
210 dst
[i
] = fixmul32b(src0
[i
], src1
[-i
]);
216 * Apply MDCT window and add into output.
218 * We ensure that when the windows overlap their squared sum
219 * is always 1 (MDCT reconstruction rule).
221 * The Vorbis I spec has a great diagram explaining this process.
222 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
224 static void wma_window(WMADecodeContext
*s
, fixed32
*in
, fixed32
*out
)
226 //float *in = s->output;
227 int block_len
, bsize
, n
;
230 /*previous block was larger, so we'll use the size of the current block to set the window size*/
231 if (s
->block_len_bits
<= s
->prev_block_len_bits
) {
232 block_len
= s
->block_len
;
233 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
235 vector_fmul_add_add(out
, in
, s
->windows
[bsize
], block_len
);
238 /*previous block was smaller or the same size, so use it's size to set the window length*/
239 block_len
= 1 << s
->prev_block_len_bits
;
240 /*find the middle of the two overlapped blocks, this will be the first overlapped sample*/
241 n
= (s
->block_len
- block_len
) / 2;
242 bsize
= s
->frame_len_bits
- s
->prev_block_len_bits
;
244 vector_fmul_add_add(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
246 memcpy(out
+n
+block_len
, in
+n
+block_len
, n
*sizeof(fixed32
));
248 /* Advance to the end of the current block and prepare to window it for the next block.
249 * Since the window function needs to be reversed, we do it backwards starting with the
250 * last sample and moving towards the first
256 if (s
->block_len_bits
<= s
->next_block_len_bits
) {
257 block_len
= s
->block_len
;
258 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
260 vector_fmul_reverse(out
, in
, s
->windows
[bsize
], block_len
);
263 block_len
= 1 << s
->next_block_len_bits
;
264 n
= (s
->block_len
- block_len
) / 2;
265 bsize
= s
->frame_len_bits
- s
->next_block_len_bits
;
267 memcpy(out
, in
, n
*sizeof(fixed32
));
269 vector_fmul_reverse(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
271 memset(out
+n
+block_len
, 0, n
*sizeof(fixed32
));
278 /* XXX: use same run/length optimization as mpeg decoders */
279 static void init_coef_vlc(VLC
*vlc
,
280 uint16_t **prun_table
, uint16_t **plevel_table
,
281 const CoefVLCTable
*vlc_table
, int tab
)
283 int n
= vlc_table
->n
;
284 const uint8_t *table_bits
= vlc_table
->huffbits
;
285 const uint32_t *table_codes
= vlc_table
->huffcodes
;
286 const uint16_t *levels_table
= vlc_table
->levels
;
287 uint16_t *run_table
, *level_table
;
292 init_vlc(vlc
, VLCBITS
, n
, table_bits
, 1, 1, table_codes
, 4, 4, 0);
294 run_table
= runtabarray
[tab
];
295 level_table
= levtabarray
[tab
];
306 level_table
[i
] = level
;
311 *prun_table
= run_table
;
312 *plevel_table
= level_table
;
315 int wma_decode_init(WMADecodeContext
* s
, asf_waveformatex_t
*wfx
)
317 //WMADecodeContext *s = avctx->priv_data;
318 int i
, flags1
, flags2
;
328 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
331 /*clear stereo setting to avoid glitches when switching stereo->mono*/
332 s
->channel_coded
[0]=0;
333 s
->channel_coded
[1]=0;
336 s
->sample_rate
= wfx
->rate
;
337 s
->nb_channels
= wfx
->channels
;
338 s
->bit_rate
= wfx
->bitrate
;
339 s
->block_align
= wfx
->blockalign
;
341 s
->coefs
= &coefsarray
;
342 s
->frame_out
= &frame_out_buf
;
344 if (wfx
->codec_id
== ASF_CODEC_ID_WMAV1
) {
346 } else if (wfx
->codec_id
== ASF_CODEC_ID_WMAV2
) {
349 /*one of those other wma flavors that don't have GPLed decoders */
353 /* extract flag infos */
356 extradata
= wfx
->data
;
357 if (s
->version
== 1 && wfx
->datalen
>= 4) {
358 flags1
= extradata
[0] | (extradata
[1] << 8);
359 flags2
= extradata
[2] | (extradata
[3] << 8);
360 }else if (s
->version
== 2 && wfx
->datalen
>= 6){
361 flags1
= extradata
[0] | (extradata
[1] << 8) |
362 (extradata
[2] << 16) | (extradata
[3] << 24);
363 flags2
= extradata
[4] | (extradata
[5] << 8);
365 s
->use_exp_vlc
= flags2
& 0x0001;
366 s
->use_bit_reservoir
= flags2
& 0x0002;
367 s
->use_variable_block_len
= flags2
& 0x0004;
369 /* compute MDCT block size */
370 if (s
->sample_rate
<= 16000){
371 s
->frame_len_bits
= 9;
372 }else if (s
->sample_rate
<= 22050 ||
373 (s
->sample_rate
<= 32000 && s
->version
== 1)){
374 s
->frame_len_bits
= 10;
376 s
->frame_len_bits
= 11;
378 s
->frame_len
= 1 << s
->frame_len_bits
;
379 if (s
-> use_variable_block_len
)
382 nb
= ((flags2
>> 3) & 3) + 1;
383 if ((s
->bit_rate
/ s
->nb_channels
) >= 32000)
387 nb_max
= s
->frame_len_bits
- BLOCK_MIN_BITS
; //max is 11-7
390 s
->nb_block_sizes
= nb
+ 1;
394 s
->nb_block_sizes
= 1;
397 /* init rate dependant parameters */
398 s
->use_noise_coding
= 1;
399 high_freq
= itofix64(s
->sample_rate
) >> 1;
402 /* if version 2, then the rates are normalized */
403 sample_rate1
= s
->sample_rate
;
406 if (sample_rate1
>= 44100)
407 sample_rate1
= 44100;
408 else if (sample_rate1
>= 22050)
409 sample_rate1
= 22050;
410 else if (sample_rate1
>= 16000)
411 sample_rate1
= 16000;
412 else if (sample_rate1
>= 11025)
413 sample_rate1
= 11025;
414 else if (sample_rate1
>= 8000)
418 fixed64 tmp
= itofix64(s
->bit_rate
);
419 fixed64 tmp2
= itofix64(s
->nb_channels
* s
->sample_rate
);
420 bps
= fixdiv64(tmp
, tmp2
);
421 fixed64 tim
= bps
* s
->frame_len
;
422 fixed64 tmpi
= fixdiv64(tim
,itofix64(8));
423 s
->byte_offset_bits
= av_log2(fixtoi64(tmpi
+0x8000)) + 2;
425 /* compute high frequency value and choose if noise coding should
428 if (s
->nb_channels
== 2)
429 bps1
= fixmul32(bps
,0x1999a);
430 if (sample_rate1
== 44100)
433 s
->use_noise_coding
= 0;
435 high_freq
= fixmul32(high_freq
,0x6666);
437 else if (sample_rate1
== 22050)
440 s
->use_noise_coding
= 0;
441 else if (bps1
>= 0xb852)
442 high_freq
= fixmul32(high_freq
,0xb333);
444 high_freq
= fixmul32(high_freq
,0x999a);
446 else if (sample_rate1
== 16000)
449 high_freq
= fixmul32(high_freq
,0x8000);
451 high_freq
= fixmul32(high_freq
,0x4ccd);
453 else if (sample_rate1
== 11025)
455 high_freq
= fixmul32(high_freq
,0xb333);
457 else if (sample_rate1
== 8000)
461 high_freq
= fixmul32(high_freq
,0x8000);
463 else if (bps
> 0xc000)
465 s
->use_noise_coding
= 0;
469 high_freq
= fixmul32(high_freq
,0xa666);
476 high_freq
= fixmul32(high_freq
,0xc000);
478 else if (bps
>= 0x999a)
480 high_freq
= fixmul32(high_freq
,0x999a);
484 high_freq
= fixmul32(high_freq
,0x8000);
488 /* compute the scale factor band sizes for each MDCT block size */
490 int a
, b
, pos
, lpos
, k
, block_len
, i
, j
, n
;
491 const uint8_t *table
;
501 for(k
= 0; k
< s
->nb_block_sizes
; ++k
)
503 block_len
= s
->frame_len
>> k
;
510 a
= wma_critical_freqs
[i
];
512 pos
= ((block_len
* 2 * a
) + (b
>> 1)) / b
;
515 s
->exponent_bands
[0][i
] = pos
- lpos
;
516 if (pos
>= block_len
)
523 s
->exponent_sizes
[0] = i
;
527 /* hardcoded tables */
529 a
= s
->frame_len_bits
- BLOCK_MIN_BITS
- k
;
532 if (s
->sample_rate
>= 44100)
533 table
= exponent_band_44100
[a
];
534 else if (s
->sample_rate
>= 32000)
535 table
= exponent_band_32000
[a
];
536 else if (s
->sample_rate
>= 22050)
537 table
= exponent_band_22050
[a
];
543 s
->exponent_bands
[k
][i
] = table
[i
];
544 s
->exponent_sizes
[k
] = n
;
552 a
= wma_critical_freqs
[i
];
554 pos
= ((block_len
* 2 * a
) + (b
<< 1)) / (4 * b
);
559 s
->exponent_bands
[k
][j
++] = pos
- lpos
;
560 if (pos
>= block_len
)
564 s
->exponent_sizes
[k
] = j
;
568 /* max number of coefs */
569 s
->coefs_end
[k
] = (s
->frame_len
- ((s
->frame_len
* 9) / 100)) >> k
;
570 /* high freq computation */
572 fixed32 tmp1
= high_freq
*2; /* high_freq is a fixed32!*/
573 fixed32 tmp2
=itofix32(s
->sample_rate
>>1);
574 s
->high_band_start
[k
] = fixtoi32( fixdiv32(tmp1
, tmp2
) * (block_len
>>1) +0x8000);
577 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
578 s->sample_rate + 0.5);*/
580 n
= s
->exponent_sizes
[k
];
587 pos
+= s
->exponent_bands
[k
][i
];
589 if (start
< s
->high_band_start
[k
])
590 start
= s
->high_band_start
[k
];
591 if (end
> s
->coefs_end
[k
])
592 end
= s
->coefs_end
[k
];
594 s
->exponent_high_bands
[k
][j
++] = end
- start
;
596 s
->exponent_high_sizes
[k
] = j
;
600 /*Not using the ffmpeg IMDCT anymore*/
602 /* mdct_init_global();
604 for(i = 0; i < s->nb_block_sizes; ++i)
606 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
611 /*ffmpeg uses malloc to only allocate as many window sizes as needed. However, we're really only interested in the worst case memory usage.
612 * In the worst case you can have 5 window sizes, 128 doubling up 2048
613 * Smaller windows are handled differently.
614 * Since we don't have malloc, just statically allocate this
623 /* init MDCT windows : simple sinus window */
624 for(i
= 0; i
< s
->nb_block_sizes
; i
++)
628 n
= 1 << (s
->frame_len_bits
- i
);
629 //window = av_malloc(sizeof(fixed32) * n);
632 //fixed32 n2 = itofix32(n<<1); //2x the window length
633 //alpha = fixdiv32(M_PI_F, n2); //PI / (2x Window length) == PI<<(s->frame_len_bits - i+1)
635 //alpha = M_PI_F>>(s->frame_len_bits - i+1);
636 alpha
= (1<<15)>>(s
->frame_len_bits
- i
+1); /* this calculates 0.5/(2*n) */
639 fixed32 j2
= itofix32(j
) + 0x8000;
640 window
[j
] = fsincos(fixmul32(j2
,alpha
)<<16, 0); //alpha between 0 and pi/2
643 s
->windows
[i
] = window
;
647 s
->reset_block_lengths
= 1;
649 if (s
->use_noise_coding
)
651 /* init the noise generator */
654 s
->noise_mult
= 0x51f;
655 s
->noise_table
= noisetable_exp
;
659 s
->noise_mult
= 0xa3d;
660 /* LSP values are simply 2x the EXP values */
661 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
662 noisetable_exp
[i
] = noisetable_exp
[i
]<< 1;
663 s
->noise_table
= noisetable_exp
;
670 norm
= 0; // PJJ: near as makes any diff to 0!
671 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
673 seed
= seed
* 314159 + 1;
674 s
->noise_table
[i
] = itofix32((int)seed
) * norm
;
679 s
->hgain_vlc
.table
= vlcbuf4
;
680 s
->hgain_vlc
.table_allocated
= VLCBUF4SIZE
;
681 init_vlc(&s
->hgain_vlc
, HGAINVLCBITS
, sizeof(hgain_huffbits
),
682 hgain_huffbits
, 1, 1,
683 hgain_huffcodes
, 2, 2, 0);
689 s
->exp_vlc
.table
= vlcbuf3
;
690 s
->exp_vlc
.table_allocated
= VLCBUF3SIZE
;
692 init_vlc(&s
->exp_vlc
, EXPVLCBITS
, sizeof(scale_huffbits
),
693 scale_huffbits
, 1, 1,
694 scale_huffcodes
, 4, 4, 0);
698 wma_lsp_to_curve_init(s
, s
->frame_len
);
701 /* choose the VLC tables for the coefficients */
703 if (s
->sample_rate
>= 32000)
707 else if (bps1
< 0x128f6)
711 runtabarray
[0] = runtab0
; runtabarray
[1] = runtab1
;
712 levtabarray
[0] = levtab0
; levtabarray
[1] = levtab1
;
714 s
->coef_vlc
[0].table
= vlcbuf1
;
715 s
->coef_vlc
[0].table_allocated
= VLCBUF1SIZE
;
716 s
->coef_vlc
[1].table
= vlcbuf2
;
717 s
->coef_vlc
[1].table_allocated
= VLCBUF2SIZE
;
720 init_coef_vlc(&s
->coef_vlc
[0], &s
->run_table
[0], &s
->level_table
[0],
721 &coef_vlcs
[coef_vlc_table
* 2], 0);
722 init_coef_vlc(&s
->coef_vlc
[1], &s
->run_table
[1], &s
->level_table
[1],
723 &coef_vlcs
[coef_vlc_table
* 2 + 1], 1);
725 s
->last_superframe_len
= 0;
726 s
->last_bitoffset
= 0;
732 /* compute x^-0.25 with an exponent and mantissa table. We use linear
733 interpolation to reduce the mantissa table size at a small speed
734 expense (linear interpolation approximately doubles the number of
735 bits of precision). */
736 static inline fixed32
pow_m1_4(WMADecodeContext
*s
, fixed32 x
)
747 m
= (u
.v
>> (23 - LSP_POW_BITS
)) & ((1 << LSP_POW_BITS
) - 1);
748 /* build interpolation scale: 1 <= t < 2. */
749 t
.v
= ((u
.v
<< LSP_POW_BITS
) & ((1 << 23) - 1)) | (127 << 23);
750 a
= s
->lsp_pow_m_table1
[m
];
751 b
= s
->lsp_pow_m_table2
[m
];
753 /* lsp_pow_e_table contains 32.32 format */
754 /* TODO: Since we're unlikely have value that cover the whole
755 * IEEE754 range, we probably don't need to have all possible exponents */
757 return (lsp_pow_e_table
[e
] * (a
+ fixmul32(b
, ftofix32(t
.f
))) >>32);
760 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
)
762 fixed32 wdel
, a
, b
, temp
, temp2
;
765 wdel
= fixdiv32(M_PI_F
, itofix32(frame_len
));
766 temp
= fixdiv32(itofix32(1), itofix32(frame_len
));
767 for (i
=0; i
<frame_len
; ++i
)
769 /* TODO: can probably reuse the trig_init values here */
770 fsincos((temp
*i
)<<15, &temp2
);
771 /* get 3 bits headroom + 1 bit from not doubleing the values */
772 s
->lsp_cos_table
[i
] = temp2
>>3;
775 /* NOTE: these two tables are needed to avoid two operations in
780 /*double check this later*/
781 for(i
=(1 << LSP_POW_BITS
) - 1;i
>=0;i
--)
783 m
= (1 << LSP_POW_BITS
) + i
;
784 a
= pow_a_table
[ix
++]<<4;
785 s
->lsp_pow_m_table1
[i
] = 2 * a
- b
;
786 s
->lsp_pow_m_table2
[i
] = b
- a
;
792 /* NOTE: We use the same code as Vorbis here */
793 /* XXX: optimize it further with SSE/3Dnow */
794 static void wma_lsp_to_curve(WMADecodeContext
*s
,
796 fixed32
*val_max_ptr
,
801 fixed32 p
, q
, w
, v
, val_max
, temp
, temp2
;
806 /* shift by 2 now to reduce rounding error,
807 * we can renormalize right before pow_m1_4
812 w
= s
->lsp_cos_table
[i
];
814 for (j
=1;j
<NB_LSP_COEFS
;j
+=2)
816 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
817 temp2
= ((w
- (lsp
[j
- 1]<<11)));
820 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
821 q
= fixmul32b(q
, temp2
)<<4;
822 p
= fixmul32b(p
, (w
- (lsp
[j
]<<11)))<<4;
825 /* 2 in 5.27 format is 0x10000000 */
826 p
= fixmul32(p
, fixmul32b(p
, (0x10000000 - w
)))<<3;
827 q
= fixmul32(q
, fixmul32b(q
, (0x10000000 + w
)))<<3;
829 v
= (p
+ q
) >>9; /* p/q end up as 16.16 */
836 *val_max_ptr
= val_max
;
839 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
840 static void decode_exp_lsp(WMADecodeContext
*s
, int ch
)
842 fixed32 lsp_coefs
[NB_LSP_COEFS
];
845 for (i
= 0; i
< NB_LSP_COEFS
; ++i
)
847 if (i
== 0 || i
>= 8)
848 val
= get_bits(&s
->gb
, 3);
850 val
= get_bits(&s
->gb
, 4);
851 lsp_coefs
[i
] = lsp_codebook
[i
][val
];
856 &s
->max_exponent
[ch
],
861 /* decode exponents coded with VLC codes */
862 static int decode_exp_vlc(WMADecodeContext
*s
, int ch
)
864 int last_exp
, n
, code
;
865 const uint16_t *ptr
, *band_ptr
;
866 fixed32 v
, max_scale
;
869 /*accommodate the 60 negative indices */
870 const fixed32
*pow_10_to_yover16_ptr
= &pow_10_to_yover16
[61];
872 band_ptr
= s
->exponent_bands
[s
->frame_len_bits
- s
->block_len_bits
];
874 q
= s
->exponents
[ch
];
875 q_end
= q
+ s
->block_len
;
879 if (s
->version
== 1) //wmav1 only
881 last_exp
= get_bits(&s
->gb
, 5) + 10;
882 /* XXX: use a table */
883 v
= pow_10_to_yover16_ptr
[last_exp
];
897 code
= get_vlc2(&s
->gb
, s
->exp_vlc
.table
, EXPVLCBITS
, EXPMAX
);
902 /* NOTE: this offset is the same as MPEG4 AAC ! */
903 last_exp
+= code
- 60;
904 /* XXX: use a table */
905 v
= pow_10_to_yover16_ptr
[last_exp
];
919 s
->max_exponent
[ch
] = max_scale
;
923 /* return 0 if OK. return 1 if last block of frame. return -1 if
924 unrecorrable error. */
925 static int wma_decode_block(WMADecodeContext
*s
, int32_t *scratch_buffer
)
927 int n
, v
, a
, ch
, code
, bsize
;
928 int coef_nb_bits
, total_gain
;
929 int nb_coefs
[MAX_CHANNELS
];
932 /*DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s->block_num, s->block_len, s->frame_len);*/
934 /* compute current block length */
935 if (s
->use_variable_block_len
)
937 n
= av_log2(s
->nb_block_sizes
- 1) + 1;
939 if (s
->reset_block_lengths
)
941 s
->reset_block_lengths
= 0;
942 v
= get_bits(&s
->gb
, n
);
943 if (v
>= s
->nb_block_sizes
)
947 s
->prev_block_len_bits
= s
->frame_len_bits
- v
;
948 v
= get_bits(&s
->gb
, n
);
949 if (v
>= s
->nb_block_sizes
)
953 s
->block_len_bits
= s
->frame_len_bits
- v
;
957 /* update block lengths */
958 s
->prev_block_len_bits
= s
->block_len_bits
;
959 s
->block_len_bits
= s
->next_block_len_bits
;
961 v
= get_bits(&s
->gb
, n
);
963 if (v
>= s
->nb_block_sizes
)
965 // rb->splash(HZ*4, "v was %d", v); //5, 7
966 return -4; //this is it
969 //rb->splash(HZ, "passed v block (%d)!", v);
971 s
->next_block_len_bits
= s
->frame_len_bits
- v
;
975 /* fixed block len */
976 s
->next_block_len_bits
= s
->frame_len_bits
;
977 s
->prev_block_len_bits
= s
->frame_len_bits
;
978 s
->block_len_bits
= s
->frame_len_bits
;
980 /* now check if the block length is coherent with the frame length */
981 s
->block_len
= 1 << s
->block_len_bits
;
983 if ((s
->block_pos
+ s
->block_len
) > s
->frame_len
)
985 return -5; //oddly 32k sample from tracker fails here
988 if (s
->nb_channels
== 2)
990 s
->ms_stereo
= get_bits(&s
->gb
, 1);
993 for (ch
= 0; ch
< s
->nb_channels
; ++ch
)
995 a
= get_bits(&s
->gb
, 1);
996 s
->channel_coded
[ch
] = a
;
999 /* if no channel coded, no need to go further */
1000 /* XXX: fix potential framing problems */
1006 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
1008 /* read total gain and extract corresponding number of bits for
1009 coef escape coding */
1013 a
= get_bits(&s
->gb
, 7);
1021 if (total_gain
< 15)
1023 else if (total_gain
< 32)
1025 else if (total_gain
< 40)
1027 else if (total_gain
< 45)
1032 /* compute number of coefficients */
1033 n
= s
->coefs_end
[bsize
] - s
->coefs_start
;
1035 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1039 /* complex coding */
1040 if (s
->use_noise_coding
)
1043 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1045 if (s
->channel_coded
[ch
])
1048 n
= s
->exponent_high_sizes
[bsize
];
1051 a
= get_bits(&s
->gb
, 1);
1052 s
->high_band_coded
[ch
][i
] = a
;
1053 /* if noise coding, the coefficients are not transmitted */
1055 nb_coefs
[ch
] -= s
->exponent_high_bands
[bsize
][i
];
1059 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1061 if (s
->channel_coded
[ch
])
1063 int i
, n
, val
, code
;
1065 n
= s
->exponent_high_sizes
[bsize
];
1066 val
= (int)0x80000000;
1069 if (s
->high_band_coded
[ch
][i
])
1071 if (val
== (int)0x80000000)
1073 val
= get_bits(&s
->gb
, 7) - 19;
1077 //code = get_vlc(&s->gb, &s->hgain_vlc);
1078 code
= get_vlc2(&s
->gb
, s
->hgain_vlc
.table
, HGAINVLCBITS
, HGAINMAX
);
1085 s
->high_band_values
[ch
][i
] = val
;
1092 /* exponents can be reused in short blocks. */
1093 if ((s
->block_len_bits
== s
->frame_len_bits
) || get_bits(&s
->gb
, 1))
1095 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1097 if (s
->channel_coded
[ch
])
1101 if (decode_exp_vlc(s
, ch
) < 0)
1108 decode_exp_lsp(s
, ch
);
1110 s
->exponents_bsize
[ch
] = bsize
;
1115 /* parse spectral coefficients : just RLE encoding */
1116 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1118 if (s
->channel_coded
[ch
])
1121 int level
, run
, sign
, tindex
;
1122 int16_t *ptr
, *eptr
;
1123 const int16_t *level_table
, *run_table
;
1125 /* special VLC tables are used for ms stereo because
1126 there is potentially less energy there */
1127 tindex
= (ch
== 1 && s
->ms_stereo
);
1128 coef_vlc
= &s
->coef_vlc
[tindex
];
1129 run_table
= s
->run_table
[tindex
];
1130 level_table
= s
->level_table
[tindex
];
1132 ptr
= &s
->coefs1
[ch
][0];
1133 eptr
= ptr
+ nb_coefs
[ch
];
1134 memset(ptr
, 0, s
->block_len
* sizeof(int16_t));
1138 code
= get_vlc2(&s
->gb
, coef_vlc
->table
, VLCBITS
, VLCMAX
);
1139 //code = get_vlc(&s->gb, coef_vlc);
1152 level
= get_bits(&s
->gb
, coef_nb_bits
);
1153 /* NOTE: this is rather suboptimal. reading
1154 block_len_bits would be better */
1155 run
= get_bits(&s
->gb
, s
->frame_len_bits
);
1160 run
= run_table
[code
];
1161 level
= level_table
[code
];
1163 sign
= get_bits(&s
->gb
, 1);
1174 /* NOTE: EOB can be omitted */
1179 if (s
->version
== 1 && s
->nb_channels
>= 2)
1181 align_get_bits(&s
->gb
);
1186 int n4
= s
->block_len
>> 1;
1189 mdct_norm
= 0x10000>>(s
->block_len_bits
-1);
1191 if (s
->version
== 1)
1193 mdct_norm
*= fixtoi32(fixsqrt32(itofix32(n4
)));
1198 /* finally compute the MDCT coefficients */
1199 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1201 if (s
->channel_coded
[ch
])
1205 fixed32
*coefs
, atemp
;
1208 fixed32 noise
, temp1
, temp2
, mult2
;
1209 int i
, j
, n
, n1
, last_high_band
, esize
;
1210 fixed32 exp_power
[HIGH_BAND_MAX_SIZE
];
1212 //total_gain, coefs1, mdctnorm are lossless
1214 coefs1
= s
->coefs1
[ch
];
1215 exponents
= s
->exponents
[ch
];
1216 esize
= s
->exponents_bsize
[ch
];
1217 coefs
= (*(s
->coefs
))[ch
];
1221 * The calculation of coefs has a shift right by 2 built in. This
1222 * prepares samples for the Tremor IMDCT which uses a slightly
1223 * different fixed format then the ffmpeg one. If the old ffmpeg
1224 * imdct is used, each shift storing into coefs should be reduced
1226 * See SVN logs for details.
1230 if (s
->use_noise_coding
)
1232 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1234 mult
= fixdiv64(pow_table
[total_gain
+20],Fixed32To64(s
->max_exponent
[ch
]));
1235 mult
= mult
* mdct_norm
;
1238 /* very low freqs : noise */
1239 for(i
= 0;i
< s
->coefs_start
; ++i
)
1241 *coefs
++ = fixmul32( (fixmul32(s
->noise_table
[s
->noise_index
],
1242 exponents
[i
<<bsize
>>esize
])>>4),Fixed32From64(mult1
)) >>2;
1243 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1246 n1
= s
->exponent_high_sizes
[bsize
];
1248 /* compute power of high bands */
1249 exponents
= s
->exponents
[ch
] +(s
->high_band_start
[bsize
]<<bsize
);
1250 last_high_band
= 0; /* avoid warning */
1253 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1254 s
->block_len_bits
][j
];
1255 if (s
->high_band_coded
[ch
][j
])
1259 for(i
= 0;i
< n
; ++i
)
1261 /*v is noramlized later on so its fixed format is irrelevant*/
1262 v
= exponents
[i
<<bsize
>>esize
]>>4;
1263 e2
+= fixmul32(v
, v
)>>3;
1265 exp_power
[j
] = e2
/n
; /*n is an int...*/
1268 exponents
+= n
<<bsize
;
1271 /* main freqs and high freqs */
1272 exponents
= s
->exponents
[ch
] + (s
->coefs_start
<<bsize
);
1277 n
= s
->high_band_start
[bsize
] -
1282 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1283 s
->block_len_bits
][j
];
1285 if (j
>= 0 && s
->high_band_coded
[ch
][j
])
1287 /* use noise with specified power */
1288 fixed32 tmp
= fixdiv32(exp_power
[j
],exp_power
[last_high_band
]);
1290 /*mult1 is 48.16, pow_table is 48.16*/
1291 mult1
= fixmul32(fixsqrt32(tmp
),
1292 pow_table
[s
->high_band_values
[ch
][j
]+20]) >> 16;
1294 /*this step has a fairly high degree of error for some reason*/
1295 mult1
= fixdiv64(mult1
,fixmul32(s
->max_exponent
[ch
],s
->noise_mult
));
1296 mult1
= mult1
*mdct_norm
>>PRECISION
;
1297 for(i
= 0;i
< n
; ++i
)
1299 noise
= s
->noise_table
[s
->noise_index
];
1300 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1301 *coefs
++ = fixmul32((fixmul32(exponents
[i
<<bsize
>>esize
],noise
)>>4),
1302 Fixed32From64(mult1
)) >>2;
1305 exponents
+= n
<<bsize
;
1309 /* coded values + small noise */
1310 for(i
= 0;i
< n
; ++i
)
1312 noise
= s
->noise_table
[s
->noise_index
];
1313 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1315 /*don't forget to renormalize the noise*/
1316 temp1
= (((int32_t)*coefs1
++)<<16) + (noise
>>4);
1317 temp2
= fixmul32(exponents
[i
<<bsize
>>esize
], mult
>>18);
1318 *coefs
++ = fixmul32(temp1
, temp2
);
1320 exponents
+= n
<<bsize
;
1324 /* very high freqs : noise */
1325 n
= s
->block_len
- s
->coefs_end
[bsize
];
1326 mult2
= fixmul32(mult
>>16,exponents
[((-1<<bsize
))>>esize
]) ;
1327 for (i
= 0; i
< n
; ++i
)
1329 /*renormalize the noise product and then reduce to 14.18 precison*/
1330 *coefs
++ = fixmul32(s
->noise_table
[s
->noise_index
],mult2
) >>6;
1332 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1337 /*Noise coding not used, simply convert from exp to fixed representation*/
1339 fixed32 mult3
= (fixed32
)(fixdiv64(pow_table
[total_gain
+20],
1340 Fixed32To64(s
->max_exponent
[ch
])));
1341 mult3
= fixmul32(mult3
, mdct_norm
);
1343 /*zero the first 3 coefficients for WMA V1, does nothing otherwise*/
1344 for(i
=0; i
<s
->coefs_start
; i
++)
1349 /* XXX: optimize more, unrolling this loop in asm
1350 might be a good idea */
1352 for(i
= 0;i
< n
; ++i
)
1354 /*ffmpeg imdct needs 15.17, while tremor 14.18*/
1355 atemp
= (coefs1
[i
] * mult3
)>>2;
1356 *coefs
++=fixmul32(atemp
,exponents
[i
<<bsize
>>esize
]);
1358 n
= s
->block_len
- s
->coefs_end
[bsize
];
1359 memset(coefs
, 0, n
*sizeof(fixed32
));
1366 if (s
->ms_stereo
&& s
->channel_coded
[1])
1370 fixed32 (*coefs
)[MAX_CHANNELS
][BLOCK_MAX_SIZE
] = (s
->coefs
);
1372 /* nominal case for ms stereo: we do it before mdct */
1373 /* no need to optimize this case because it should almost
1375 if (!s
->channel_coded
[0])
1377 memset((*(s
->coefs
))[0], 0, sizeof(fixed32
) * s
->block_len
);
1378 s
->channel_coded
[0] = 1;
1381 for(i
= 0; i
< s
->block_len
; ++i
)
1385 (*coefs
)[0][i
] = a
+ b
;
1386 (*coefs
)[1][i
] = a
- b
;
1390 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1392 if (s
->channel_coded
[ch
])
1397 n4
= s
->block_len
>>1;
1399 /*faster IMDCT from Vorbis*/
1400 mdct_backward( (1 << (s
->block_len_bits
+1)), (int32_t*)(*(s
->coefs
))[ch
], (int32_t*)scratch_buffer
);
1402 /*slower but more easily understood IMDCT from FFMPEG*/
1403 //ff_imdct_calc(&s->mdct_ctx[bsize],
1405 // (*(s->coefs))[ch]);
1408 /* add in the frame */
1409 index
= (s
->frame_len
/ 2) + s
->block_pos
- n4
;
1410 wma_window(s
, scratch_buffer
, &((*s
->frame_out
)[ch
][index
]));
1414 /* specific fast case for ms-stereo : add to second
1415 channel if it is not coded */
1416 if (s
->ms_stereo
&& !s
->channel_coded
[1])
1418 wma_window(s
, scratch_buffer
, &((*s
->frame_out
)[1][index
]));
1423 /* update block number */
1425 s
->block_pos
+= s
->block_len
;
1426 if (s
->block_pos
>= s
->frame_len
)
1436 /* decode a frame of frame_len samples */
1437 static int wma_decode_frame(WMADecodeContext
*s
, int32_t *samples
)
1439 int ret
, i
, n
, ch
, incr
;
1443 /* read each block */
1450 ret
= wma_decode_block(s
, samples
);
1454 DEBUGF("wma_decode_block failed with code %d\n", ret
);
1463 /* return frame with full 30-bit precision */
1465 incr
= s
->nb_channels
;
1466 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1469 iptr
= &((*s
->frame_out
)[ch
][0]);
1477 memmove(&((*s
->frame_out
)[ch
][0]), &((*s
->frame_out
)[ch
][s
->frame_len
]),
1478 s
->frame_len
* sizeof(fixed32
));
1484 /* Initialise the superframe decoding */
1486 int wma_decode_superframe_init(WMADecodeContext
* s
,
1487 uint8_t *buf
, /*input*/
1492 s
->last_superframe_len
= 0;
1496 s
->current_frame
= 0;
1498 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1500 if (s
->use_bit_reservoir
)
1502 /* read super frame header */
1503 get_bits(&s
->gb
, 4); /* super frame index */
1504 s
->nb_frames
= get_bits(&s
->gb
, 4);
1506 if (s
->last_superframe_len
== 0)
1508 else if (s
->nb_frames
== 0)
1511 s
->bit_offset
= get_bits(&s
->gb
, s
->byte_offset_bits
+ 3);
1520 /* Decode a single frame in the current superframe - return -1 if
1521 there was a decoding error, or the number of samples decoded.
1524 int wma_decode_superframe_frame(WMADecodeContext
* s
,
1525 int32_t* samples
, /*output*/
1526 uint8_t *buf
, /*input*/
1532 if ((s
->use_bit_reservoir
) && (s
->current_frame
== 0))
1534 if (s
->last_superframe_len
> 0)
1536 /* add s->bit_offset bits to last frame */
1537 if ((s
->last_superframe_len
+ ((s
->bit_offset
+ 7) >> 3)) >
1538 MAX_CODED_SUPERFRAME_SIZE
)
1540 DEBUGF("superframe size too large error\n");
1543 q
= s
->last_superframe
+ s
->last_superframe_len
;
1544 len
= s
->bit_offset
;
1547 *q
++ = (get_bits
)(&s
->gb
, 8);
1552 *q
++ = (get_bits
)(&s
->gb
, len
) << (8 - len
);
1555 /* XXX: s->bit_offset bits into last frame */
1556 init_get_bits(&s
->gb
, s
->last_superframe
, MAX_CODED_SUPERFRAME_SIZE
*8);
1557 /* skip unused bits */
1558 if (s
->last_bitoffset
> 0)
1559 skip_bits(&s
->gb
, s
->last_bitoffset
);
1561 /* this frame is stored in the last superframe and in the
1563 if (wma_decode_frame(s
, samples
) < 0)
1570 /* read each frame starting from s->bit_offset */
1571 pos
= s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3;
1572 init_get_bits(&s
->gb
, buf
+ (pos
>> 3), (MAX_CODED_SUPERFRAME_SIZE
- (pos
>> 3))*8);
1575 skip_bits(&s
->gb
, len
);
1577 s
->reset_block_lengths
= 1;
1580 /* If we haven't decoded a frame yet, do it now */
1583 if (wma_decode_frame(s
, samples
) < 0)
1591 if ((s
->use_bit_reservoir
) && (s
->current_frame
== s
->nb_frames
))
1593 /* we copy the end of the frame in the last frame buffer */
1594 pos
= get_bits_count(&s
->gb
) + ((s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3) & ~7);
1595 s
->last_bitoffset
= pos
& 7;
1597 len
= buf_size
- pos
;
1598 if (len
> MAX_CODED_SUPERFRAME_SIZE
|| len
< 0)
1600 DEBUGF("superframe size too large error after decodeing\n");
1603 s
->last_superframe_len
= len
;
1604 memcpy(s
->last_superframe
, buf
+ pos
, len
);
1607 return s
->frame_len
;
1610 /* when error, we reset the bit reservoir */
1612 s
->last_superframe_len
= 0;