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 s
->sample_rate
= wfx
->rate
;
332 s
->nb_channels
= wfx
->channels
;
333 s
->bit_rate
= wfx
->bitrate
;
334 s
->block_align
= wfx
->blockalign
;
336 s
->coefs
= &coefsarray
;
337 s
->frame_out
= &frame_out_buf
;
339 if (wfx
->codec_id
== ASF_CODEC_ID_WMAV1
) {
341 } else if (wfx
->codec_id
== ASF_CODEC_ID_WMAV2
) {
344 /*one of those other wma flavors that don't have GPLed decoders */
348 /* extract flag infos */
351 extradata
= wfx
->data
;
352 if (s
->version
== 1 && wfx
->datalen
>= 4) {
353 flags1
= extradata
[0] | (extradata
[1] << 8);
354 flags2
= extradata
[2] | (extradata
[3] << 8);
355 }else if (s
->version
== 2 && wfx
->datalen
>= 6){
356 flags1
= extradata
[0] | (extradata
[1] << 8) |
357 (extradata
[2] << 16) | (extradata
[3] << 24);
358 flags2
= extradata
[4] | (extradata
[5] << 8);
360 s
->use_exp_vlc
= flags2
& 0x0001;
361 s
->use_bit_reservoir
= flags2
& 0x0002;
362 s
->use_variable_block_len
= flags2
& 0x0004;
364 /* compute MDCT block size */
365 if (s
->sample_rate
<= 16000){
366 s
->frame_len_bits
= 9;
367 }else if (s
->sample_rate
<= 22050 ||
368 (s
->sample_rate
<= 32000 && s
->version
== 1)){
369 s
->frame_len_bits
= 10;
371 s
->frame_len_bits
= 11;
373 s
->frame_len
= 1 << s
->frame_len_bits
;
374 if (s
-> use_variable_block_len
)
377 nb
= ((flags2
>> 3) & 3) + 1;
378 if ((s
->bit_rate
/ s
->nb_channels
) >= 32000)
382 nb_max
= s
->frame_len_bits
- BLOCK_MIN_BITS
; //max is 11-7
385 s
->nb_block_sizes
= nb
+ 1;
389 s
->nb_block_sizes
= 1;
392 /* init rate dependant parameters */
393 s
->use_noise_coding
= 1;
394 high_freq
= itofix64(s
->sample_rate
) >> 1;
397 /* if version 2, then the rates are normalized */
398 sample_rate1
= s
->sample_rate
;
401 if (sample_rate1
>= 44100)
402 sample_rate1
= 44100;
403 else if (sample_rate1
>= 22050)
404 sample_rate1
= 22050;
405 else if (sample_rate1
>= 16000)
406 sample_rate1
= 16000;
407 else if (sample_rate1
>= 11025)
408 sample_rate1
= 11025;
409 else if (sample_rate1
>= 8000)
413 fixed64 tmp
= itofix64(s
->bit_rate
);
414 fixed64 tmp2
= itofix64(s
->nb_channels
* s
->sample_rate
);
415 bps
= fixdiv64(tmp
, tmp2
);
416 fixed64 tim
= bps
* s
->frame_len
;
417 fixed64 tmpi
= fixdiv64(tim
,itofix64(8));
418 s
->byte_offset_bits
= av_log2(fixtoi64(tmpi
+0x8000)) + 2;
420 /* compute high frequency value and choose if noise coding should
423 if (s
->nb_channels
== 2)
424 bps1
= fixmul32(bps
,0x1999a);
425 if (sample_rate1
== 44100)
428 s
->use_noise_coding
= 0;
430 high_freq
= fixmul32(high_freq
,0x6666);
432 else if (sample_rate1
== 22050)
435 s
->use_noise_coding
= 0;
436 else if (bps1
>= 0xb852)
437 high_freq
= fixmul32(high_freq
,0xb333);
439 high_freq
= fixmul32(high_freq
,0x999a);
441 else if (sample_rate1
== 16000)
444 high_freq
= fixmul32(high_freq
,0x8000);
446 high_freq
= fixmul32(high_freq
,0x4ccd);
448 else if (sample_rate1
== 11025)
450 high_freq
= fixmul32(high_freq
,0xb333);
452 else if (sample_rate1
== 8000)
456 high_freq
= fixmul32(high_freq
,0x8000);
458 else if (bps
> 0xc000)
460 s
->use_noise_coding
= 0;
464 high_freq
= fixmul32(high_freq
,0xa666);
471 high_freq
= fixmul32(high_freq
,0xc000);
473 else if (bps
>= 0x999a)
475 high_freq
= fixmul32(high_freq
,0x999a);
479 high_freq
= fixmul32(high_freq
,0x8000);
483 /* compute the scale factor band sizes for each MDCT block size */
485 int a
, b
, pos
, lpos
, k
, block_len
, i
, j
, n
;
486 const uint8_t *table
;
496 for(k
= 0; k
< s
->nb_block_sizes
; ++k
)
498 block_len
= s
->frame_len
>> k
;
505 a
= wma_critical_freqs
[i
];
507 pos
= ((block_len
* 2 * a
) + (b
>> 1)) / b
;
510 s
->exponent_bands
[0][i
] = pos
- lpos
;
511 if (pos
>= block_len
)
518 s
->exponent_sizes
[0] = i
;
522 /* hardcoded tables */
524 a
= s
->frame_len_bits
- BLOCK_MIN_BITS
- k
;
527 if (s
->sample_rate
>= 44100)
528 table
= exponent_band_44100
[a
];
529 else if (s
->sample_rate
>= 32000)
530 table
= exponent_band_32000
[a
];
531 else if (s
->sample_rate
>= 22050)
532 table
= exponent_band_22050
[a
];
538 s
->exponent_bands
[k
][i
] = table
[i
];
539 s
->exponent_sizes
[k
] = n
;
547 a
= wma_critical_freqs
[i
];
549 pos
= ((block_len
* 2 * a
) + (b
<< 1)) / (4 * b
);
554 s
->exponent_bands
[k
][j
++] = pos
- lpos
;
555 if (pos
>= block_len
)
559 s
->exponent_sizes
[k
] = j
;
563 /* max number of coefs */
564 s
->coefs_end
[k
] = (s
->frame_len
- ((s
->frame_len
* 9) / 100)) >> k
;
565 /* high freq computation */
567 fixed32 tmp1
= high_freq
*2; /* high_freq is a fixed32!*/
568 fixed32 tmp2
=itofix32(s
->sample_rate
>>1);
569 s
->high_band_start
[k
] = fixtoi32( fixdiv32(tmp1
, tmp2
) * (block_len
>>1) +0x8000);
572 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
573 s->sample_rate + 0.5);*/
575 n
= s
->exponent_sizes
[k
];
582 pos
+= s
->exponent_bands
[k
][i
];
584 if (start
< s
->high_band_start
[k
])
585 start
= s
->high_band_start
[k
];
586 if (end
> s
->coefs_end
[k
])
587 end
= s
->coefs_end
[k
];
589 s
->exponent_high_bands
[k
][j
++] = end
- start
;
591 s
->exponent_high_sizes
[k
] = j
;
595 /*Not using the ffmpeg IMDCT anymore*/
597 /* mdct_init_global();
599 for(i = 0; i < s->nb_block_sizes; ++i)
601 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
606 /*ffmpeg uses malloc to only allocate as many window sizes as needed. However, we're really only interested in the worst case memory usage.
607 * In the worst case you can have 5 window sizes, 128 doubling up 2048
608 * Smaller windows are handled differently.
609 * Since we don't have malloc, just statically allocate this
618 /* init MDCT windows : simple sinus window */
619 for(i
= 0; i
< s
->nb_block_sizes
; i
++)
623 n
= 1 << (s
->frame_len_bits
- i
);
624 //window = av_malloc(sizeof(fixed32) * n);
627 //fixed32 n2 = itofix32(n<<1); //2x the window length
628 //alpha = fixdiv32(M_PI_F, n2); //PI / (2x Window length) == PI<<(s->frame_len_bits - i+1)
630 //alpha = M_PI_F>>(s->frame_len_bits - i+1);
631 alpha
= (1<<15)>>(s
->frame_len_bits
- i
+1); /* this calculates 0.5/(2*n) */
634 fixed32 j2
= itofix32(j
) + 0x8000;
635 window
[j
] = fsincos(fixmul32(j2
,alpha
)<<16, 0); //alpha between 0 and pi/2
638 s
->windows
[i
] = window
;
642 s
->reset_block_lengths
= 1;
644 if (s
->use_noise_coding
)
646 /* init the noise generator */
649 s
->noise_mult
= 0x51f;
650 s
->noise_table
= noisetable_exp
;
654 s
->noise_mult
= 0xa3d;
655 /* LSP values are simply 2x the EXP values */
656 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
657 noisetable_exp
[i
] = noisetable_exp
[i
]<< 1;
658 s
->noise_table
= noisetable_exp
;
665 norm
= 0; // PJJ: near as makes any diff to 0!
666 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
668 seed
= seed
* 314159 + 1;
669 s
->noise_table
[i
] = itofix32((int)seed
) * norm
;
674 s
->hgain_vlc
.table
= vlcbuf4
;
675 s
->hgain_vlc
.table_allocated
= VLCBUF4SIZE
;
676 init_vlc(&s
->hgain_vlc
, HGAINVLCBITS
, sizeof(hgain_huffbits
),
677 hgain_huffbits
, 1, 1,
678 hgain_huffcodes
, 2, 2, 0);
684 s
->exp_vlc
.table
= vlcbuf3
;
685 s
->exp_vlc
.table_allocated
= VLCBUF3SIZE
;
687 init_vlc(&s
->exp_vlc
, EXPVLCBITS
, sizeof(scale_huffbits
),
688 scale_huffbits
, 1, 1,
689 scale_huffcodes
, 4, 4, 0);
693 wma_lsp_to_curve_init(s
, s
->frame_len
);
696 /* choose the VLC tables for the coefficients */
698 if (s
->sample_rate
>= 32000)
702 else if (bps1
< 0x128f6)
706 runtabarray
[0] = runtab0
; runtabarray
[1] = runtab1
;
707 levtabarray
[0] = levtab0
; levtabarray
[1] = levtab1
;
709 s
->coef_vlc
[0].table
= vlcbuf1
;
710 s
->coef_vlc
[0].table_allocated
= VLCBUF1SIZE
;
711 s
->coef_vlc
[1].table
= vlcbuf2
;
712 s
->coef_vlc
[1].table_allocated
= VLCBUF2SIZE
;
715 init_coef_vlc(&s
->coef_vlc
[0], &s
->run_table
[0], &s
->level_table
[0],
716 &coef_vlcs
[coef_vlc_table
* 2], 0);
717 init_coef_vlc(&s
->coef_vlc
[1], &s
->run_table
[1], &s
->level_table
[1],
718 &coef_vlcs
[coef_vlc_table
* 2 + 1], 1);
720 s
->last_superframe_len
= 0;
721 s
->last_bitoffset
= 0;
727 /* compute x^-0.25 with an exponent and mantissa table. We use linear
728 interpolation to reduce the mantissa table size at a small speed
729 expense (linear interpolation approximately doubles the number of
730 bits of precision). */
731 static inline fixed32
pow_m1_4(WMADecodeContext
*s
, fixed32 x
)
742 m
= (u
.v
>> (23 - LSP_POW_BITS
)) & ((1 << LSP_POW_BITS
) - 1);
743 /* build interpolation scale: 1 <= t < 2. */
744 t
.v
= ((u
.v
<< LSP_POW_BITS
) & ((1 << 23) - 1)) | (127 << 23);
745 a
= s
->lsp_pow_m_table1
[m
];
746 b
= s
->lsp_pow_m_table2
[m
];
748 /* lsp_pow_e_table contains 32.32 format */
749 /* TODO: Since we're unlikely have value that cover the whole
750 * IEEE754 range, we probably don't need to have all possible exponents */
752 return (lsp_pow_e_table
[e
] * (a
+ fixmul32(b
, ftofix32(t
.f
))) >>32);
755 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
)
757 fixed32 wdel
, a
, b
, temp
, temp2
;
760 wdel
= fixdiv32(M_PI_F
, itofix32(frame_len
));
761 temp
= fixdiv32(itofix32(1), itofix32(frame_len
));
762 for (i
=0; i
<frame_len
; ++i
)
764 /* TODO: can probably reuse the trig_init values here */
765 fsincos((temp
*i
)<<15, &temp2
);
766 /* get 3 bits headroom + 1 bit from not doubleing the values */
767 s
->lsp_cos_table
[i
] = temp2
>>3;
770 /* NOTE: these two tables are needed to avoid two operations in
775 /*double check this later*/
776 for(i
=(1 << LSP_POW_BITS
) - 1;i
>=0;i
--)
778 m
= (1 << LSP_POW_BITS
) + i
;
779 a
= pow_a_table
[ix
++]<<4;
780 s
->lsp_pow_m_table1
[i
] = 2 * a
- b
;
781 s
->lsp_pow_m_table2
[i
] = b
- a
;
787 /* NOTE: We use the same code as Vorbis here */
788 /* XXX: optimize it further with SSE/3Dnow */
789 static void wma_lsp_to_curve(WMADecodeContext
*s
,
791 fixed32
*val_max_ptr
,
796 fixed32 p
, q
, w
, v
, val_max
, temp
, temp2
;
801 /* shift by 2 now to reduce rounding error,
802 * we can renormalize right before pow_m1_4
807 w
= s
->lsp_cos_table
[i
];
809 for (j
=1;j
<NB_LSP_COEFS
;j
+=2)
811 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
812 temp2
= ((w
- (lsp
[j
- 1]<<11)));
815 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
816 q
= fixmul32b(q
, temp2
)<<4;
817 p
= fixmul32b(p
, (w
- (lsp
[j
]<<11)))<<4;
820 /* 2 in 5.27 format is 0x10000000 */
821 p
= fixmul32(p
, fixmul32b(p
, (0x10000000 - w
)))<<3;
822 q
= fixmul32(q
, fixmul32b(q
, (0x10000000 + w
)))<<3;
824 v
= (p
+ q
) >>9; /* p/q end up as 16.16 */
831 *val_max_ptr
= val_max
;
834 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
835 static void decode_exp_lsp(WMADecodeContext
*s
, int ch
)
837 fixed32 lsp_coefs
[NB_LSP_COEFS
];
840 for (i
= 0; i
< NB_LSP_COEFS
; ++i
)
842 if (i
== 0 || i
>= 8)
843 val
= get_bits(&s
->gb
, 3);
845 val
= get_bits(&s
->gb
, 4);
846 lsp_coefs
[i
] = lsp_codebook
[i
][val
];
851 &s
->max_exponent
[ch
],
856 /* decode exponents coded with VLC codes */
857 static int decode_exp_vlc(WMADecodeContext
*s
, int ch
)
859 int last_exp
, n
, code
;
860 const uint16_t *ptr
, *band_ptr
;
861 fixed32 v
, max_scale
;
864 /*accommodate the 60 negative indices */
865 const fixed32
*pow_10_to_yover16_ptr
= &pow_10_to_yover16
[61];
867 band_ptr
= s
->exponent_bands
[s
->frame_len_bits
- s
->block_len_bits
];
869 q
= s
->exponents
[ch
];
870 q_end
= q
+ s
->block_len
;
874 if (s
->version
== 1) //wmav1 only
876 last_exp
= get_bits(&s
->gb
, 5) + 10;
877 /* XXX: use a table */
878 v
= pow_10_to_yover16_ptr
[last_exp
];
892 code
= get_vlc2(&s
->gb
, s
->exp_vlc
.table
, EXPVLCBITS
, EXPMAX
);
897 /* NOTE: this offset is the same as MPEG4 AAC ! */
898 last_exp
+= code
- 60;
899 /* XXX: use a table */
900 v
= pow_10_to_yover16_ptr
[last_exp
];
914 s
->max_exponent
[ch
] = max_scale
;
918 /* return 0 if OK. return 1 if last block of frame. return -1 if
919 unrecorrable error. */
920 static int wma_decode_block(WMADecodeContext
*s
, int32_t *scratch_buffer
)
922 int n
, v
, a
, ch
, code
, bsize
;
923 int coef_nb_bits
, total_gain
;
924 int nb_coefs
[MAX_CHANNELS
];
927 /*DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s->block_num, s->block_len, s->frame_len);*/
929 /* compute current block length */
930 if (s
->use_variable_block_len
)
932 n
= av_log2(s
->nb_block_sizes
- 1) + 1;
934 if (s
->reset_block_lengths
)
936 s
->reset_block_lengths
= 0;
937 v
= get_bits(&s
->gb
, n
);
938 if (v
>= s
->nb_block_sizes
)
942 s
->prev_block_len_bits
= s
->frame_len_bits
- v
;
943 v
= get_bits(&s
->gb
, n
);
944 if (v
>= s
->nb_block_sizes
)
948 s
->block_len_bits
= s
->frame_len_bits
- v
;
952 /* update block lengths */
953 s
->prev_block_len_bits
= s
->block_len_bits
;
954 s
->block_len_bits
= s
->next_block_len_bits
;
956 v
= get_bits(&s
->gb
, n
);
958 if (v
>= s
->nb_block_sizes
)
960 // rb->splash(HZ*4, "v was %d", v); //5, 7
961 return -4; //this is it
964 //rb->splash(HZ, "passed v block (%d)!", v);
966 s
->next_block_len_bits
= s
->frame_len_bits
- v
;
970 /* fixed block len */
971 s
->next_block_len_bits
= s
->frame_len_bits
;
972 s
->prev_block_len_bits
= s
->frame_len_bits
;
973 s
->block_len_bits
= s
->frame_len_bits
;
975 /* now check if the block length is coherent with the frame length */
976 s
->block_len
= 1 << s
->block_len_bits
;
978 if ((s
->block_pos
+ s
->block_len
) > s
->frame_len
)
980 return -5; //oddly 32k sample from tracker fails here
983 if (s
->nb_channels
== 2)
985 s
->ms_stereo
= get_bits(&s
->gb
, 1);
988 for (ch
= 0; ch
< s
->nb_channels
; ++ch
)
990 a
= get_bits(&s
->gb
, 1);
991 s
->channel_coded
[ch
] = a
;
994 /* if no channel coded, no need to go further */
995 /* XXX: fix potential framing problems */
1001 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
1003 /* read total gain and extract corresponding number of bits for
1004 coef escape coding */
1008 a
= get_bits(&s
->gb
, 7);
1016 if (total_gain
< 15)
1018 else if (total_gain
< 32)
1020 else if (total_gain
< 40)
1022 else if (total_gain
< 45)
1027 /* compute number of coefficients */
1028 n
= s
->coefs_end
[bsize
] - s
->coefs_start
;
1030 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1034 /* complex coding */
1035 if (s
->use_noise_coding
)
1038 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1040 if (s
->channel_coded
[ch
])
1043 n
= s
->exponent_high_sizes
[bsize
];
1046 a
= get_bits(&s
->gb
, 1);
1047 s
->high_band_coded
[ch
][i
] = a
;
1048 /* if noise coding, the coefficients are not transmitted */
1050 nb_coefs
[ch
] -= s
->exponent_high_bands
[bsize
][i
];
1054 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1056 if (s
->channel_coded
[ch
])
1058 int i
, n
, val
, code
;
1060 n
= s
->exponent_high_sizes
[bsize
];
1061 val
= (int)0x80000000;
1064 if (s
->high_band_coded
[ch
][i
])
1066 if (val
== (int)0x80000000)
1068 val
= get_bits(&s
->gb
, 7) - 19;
1072 //code = get_vlc(&s->gb, &s->hgain_vlc);
1073 code
= get_vlc2(&s
->gb
, s
->hgain_vlc
.table
, HGAINVLCBITS
, HGAINMAX
);
1080 s
->high_band_values
[ch
][i
] = val
;
1087 /* exponents can be reused in short blocks. */
1088 if ((s
->block_len_bits
== s
->frame_len_bits
) || get_bits(&s
->gb
, 1))
1090 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1092 if (s
->channel_coded
[ch
])
1096 if (decode_exp_vlc(s
, ch
) < 0)
1103 decode_exp_lsp(s
, ch
);
1105 s
->exponents_bsize
[ch
] = bsize
;
1110 /* parse spectral coefficients : just RLE encoding */
1111 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1113 if (s
->channel_coded
[ch
])
1116 int level
, run
, sign
, tindex
;
1117 int16_t *ptr
, *eptr
;
1118 const int16_t *level_table
, *run_table
;
1120 /* special VLC tables are used for ms stereo because
1121 there is potentially less energy there */
1122 tindex
= (ch
== 1 && s
->ms_stereo
);
1123 coef_vlc
= &s
->coef_vlc
[tindex
];
1124 run_table
= s
->run_table
[tindex
];
1125 level_table
= s
->level_table
[tindex
];
1127 ptr
= &s
->coefs1
[ch
][0];
1128 eptr
= ptr
+ nb_coefs
[ch
];
1129 memset(ptr
, 0, s
->block_len
* sizeof(int16_t));
1133 code
= get_vlc2(&s
->gb
, coef_vlc
->table
, VLCBITS
, VLCMAX
);
1134 //code = get_vlc(&s->gb, coef_vlc);
1147 level
= get_bits(&s
->gb
, coef_nb_bits
);
1148 /* NOTE: this is rather suboptimal. reading
1149 block_len_bits would be better */
1150 run
= get_bits(&s
->gb
, s
->frame_len_bits
);
1155 run
= run_table
[code
];
1156 level
= level_table
[code
];
1158 sign
= get_bits(&s
->gb
, 1);
1169 /* NOTE: EOB can be omitted */
1174 if (s
->version
== 1 && s
->nb_channels
>= 2)
1176 align_get_bits(&s
->gb
);
1181 int n4
= s
->block_len
>> 1;
1184 mdct_norm
= 0x10000>>(s
->block_len_bits
-1);
1186 if (s
->version
== 1)
1188 mdct_norm
*= fixtoi32(fixsqrt32(itofix32(n4
)));
1193 /* finally compute the MDCT coefficients */
1194 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1196 if (s
->channel_coded
[ch
])
1199 fixed32
*exponents
, *exp_ptr
;
1200 fixed32
*coefs
, atemp
;
1203 fixed32 noise
, temp1
, temp2
, mult2
;
1204 int i
, j
, n
, n1
, last_high_band
, esize
;
1205 fixed32 exp_power
[HIGH_BAND_MAX_SIZE
];
1207 //total_gain, coefs1, mdctnorm are lossless
1209 coefs1
= s
->coefs1
[ch
];
1210 exponents
= s
->exponents
[ch
];
1211 esize
= s
->exponents_bsize
[ch
];
1212 coefs
= (*(s
->coefs
))[ch
];
1217 * The calculation of coefs has a shift right by 2 built in. This prepares samples
1218 * for the Tremor IMDCT which uses a slightly different fixed format then the ffmpeg one.
1219 * If the old ffmpeg imdct is used, each shift storing into coefs should be reduced by 1.
1220 * See SVN logs for details.
1224 if (s
->use_noise_coding
)
1226 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1228 mult
= fixdiv64(pow_table
[total_gain
+20],Fixed32To64(s
->max_exponent
[ch
]));
1229 mult
= mult
* mdct_norm
; //what the hell? This is actually fixed64*2^16!
1232 /* very low freqs : noise */
1233 for(i
= 0;i
< s
->coefs_start
; ++i
)
1235 *coefs
++ = fixmul32( (fixmul32(s
->noise_table
[s
->noise_index
],(*exponents
++))>>4),Fixed32From64(mult1
)) >>2;
1236 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1239 n1
= s
->exponent_high_sizes
[bsize
];
1241 /* compute power of high bands */
1242 exp_ptr
= exponents
+
1243 s
->high_band_start
[bsize
] -
1245 last_high_band
= 0; /* avoid warning */
1248 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1249 s
->block_len_bits
][j
];
1250 if (s
->high_band_coded
[ch
][j
])
1254 for(i
= 0;i
< n
; ++i
)
1256 /*v is noramlized later on so its fixed format is irrelevant*/
1258 e2
+= fixmul32(v
, v
)>>3;
1260 exp_power
[j
] = e2
/n
; /*n is an int...*/
1266 /* main freqs and high freqs */
1271 n
= s
->high_band_start
[bsize
] -
1276 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1277 s
->block_len_bits
][j
];
1279 if (j
>= 0 && s
->high_band_coded
[ch
][j
])
1281 /* use noise with specified power */
1282 fixed32 tmp
= fixdiv32(exp_power
[j
],exp_power
[last_high_band
]);
1283 mult1
= (fixed64
)fixsqrt32(tmp
);
1284 /* XXX: use a table */
1285 /*mult1 is 48.16, pow_table is 48.16*/
1286 mult1
= mult1
* pow_table
[s
->high_band_values
[ch
][j
]+20] >> PRECISION
;
1288 /*this step has a fairly high degree of error for some reason*/
1289 mult1
= fixdiv64(mult1
,fixmul32(s
->max_exponent
[ch
],s
->noise_mult
));
1291 mult1
= mult1
*mdct_norm
>>PRECISION
;
1292 for(i
= 0;i
< n
; ++i
)
1294 noise
= s
->noise_table
[s
->noise_index
];
1295 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1296 *coefs
++ = fixmul32((fixmul32(*exponents
,noise
)>>4),Fixed32From64(mult1
)) >>2;
1302 /* coded values + small noise */
1303 for(i
= 0;i
< n
; ++i
)
1305 // PJJ: check code path
1306 noise
= s
->noise_table
[s
->noise_index
];
1307 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1309 /*don't forget to renormalize the noise*/
1310 temp1
= (((int32_t)*coefs1
++)<<16) + (noise
>>4);
1311 temp2
= fixmul32(*exponents
, mult
>>18);
1312 *coefs
++ = fixmul32(temp1
, temp2
);
1318 /* very high freqs : noise */
1319 n
= s
->block_len
- s
->coefs_end
[bsize
];
1320 mult2
= fixmul32(mult
>>16,exponents
[-1]) ; /*the work around for 32.32 vars are getting stupid*/
1321 for (i
= 0; i
< n
; ++i
)
1323 /*renormalize the noise product and then reduce to 14.18 precison*/
1324 *coefs
++ = fixmul32(s
->noise_table
[s
->noise_index
],mult2
) >>6;
1326 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1331 /*Noise coding not used, simply convert from exp to fixed representation*/
1333 fixed32 mult3
= (fixed32
)(fixdiv64(pow_table
[total_gain
+20],Fixed32To64(s
->max_exponent
[ch
])));
1334 mult3
= fixmul32(mult3
, mdct_norm
);
1336 /*zero the first 3 coefficients for WMA V1, does nothing otherwise*/
1337 for(i
=0; i
<s
->coefs_start
; i
++)
1342 /* XXX: optimize more, unrolling this loop in asm might be a good idea */
1344 for(i
= 0;i
< n
; ++i
)
1346 atemp
= (coefs1
[i
] * mult3
)>>2; /*ffmpeg imdct needs 15.17, while tremor 14.18*/
1347 *coefs
++=fixmul32(atemp
,exponents
[i
<<bsize
>>esize
]);
1349 n
= s
->block_len
- s
->coefs_end
[bsize
];
1350 memset(coefs
, 0, n
*sizeof(fixed32
));
1357 if (s
->ms_stereo
&& s
->channel_coded
[1])
1361 fixed32 (*coefs
)[MAX_CHANNELS
][BLOCK_MAX_SIZE
] = (s
->coefs
);
1363 /* nominal case for ms stereo: we do it before mdct */
1364 /* no need to optimize this case because it should almost
1366 if (!s
->channel_coded
[0])
1368 memset((*(s
->coefs
))[0], 0, sizeof(fixed32
) * s
->block_len
);
1369 s
->channel_coded
[0] = 1;
1372 for(i
= 0; i
< s
->block_len
; ++i
)
1376 (*coefs
)[0][i
] = a
+ b
;
1377 (*coefs
)[1][i
] = a
- b
;
1381 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1383 if (s
->channel_coded
[ch
])
1388 n4
= s
->block_len
>>1;
1390 /*faster IMDCT from Vorbis*/
1391 mdct_backward( (1 << (s
->block_len_bits
+1)), (int32_t*)(*(s
->coefs
))[ch
], (int32_t*)scratch_buffer
);
1393 /*slower but more easily understood IMDCT from FFMPEG*/
1394 //ff_imdct_calc(&s->mdct_ctx[bsize],
1396 // (*(s->coefs))[ch]);
1399 /* add in the frame */
1400 index
= (s
->frame_len
/ 2) + s
->block_pos
- n4
;
1401 wma_window(s
, scratch_buffer
, &((*s
->frame_out
)[ch
][index
]));
1405 /* specific fast case for ms-stereo : add to second
1406 channel if it is not coded */
1407 if (s
->ms_stereo
&& !s
->channel_coded
[1])
1409 wma_window(s
, scratch_buffer
, &((*s
->frame_out
)[1][index
]));
1414 /* update block number */
1416 s
->block_pos
+= s
->block_len
;
1417 if (s
->block_pos
>= s
->frame_len
)
1427 /* decode a frame of frame_len samples */
1428 static int wma_decode_frame(WMADecodeContext
*s
, int32_t *samples
)
1430 int ret
, i
, n
, ch
, incr
;
1434 /* read each block */
1441 ret
= wma_decode_block(s
, samples
);
1445 DEBUGF("wma_decode_block failed with code %d\n", ret
);
1454 /* return frame with full 30-bit precision */
1456 incr
= s
->nb_channels
;
1457 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1460 iptr
= &((*s
->frame_out
)[ch
][0]);
1468 memmove(&((*s
->frame_out
)[ch
][0]), &((*s
->frame_out
)[ch
][s
->frame_len
]),
1469 s
->frame_len
* sizeof(fixed32
));
1475 /* Initialise the superframe decoding */
1477 int wma_decode_superframe_init(WMADecodeContext
* s
,
1478 uint8_t *buf
, /*input*/
1483 s
->last_superframe_len
= 0;
1487 s
->current_frame
= 0;
1489 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1491 if (s
->use_bit_reservoir
)
1493 /* read super frame header */
1494 get_bits(&s
->gb
, 4); /* super frame index */
1495 s
->nb_frames
= get_bits(&s
->gb
, 4);
1497 if (s
->last_superframe_len
== 0)
1499 else if (s
->nb_frames
== 0)
1502 s
->bit_offset
= get_bits(&s
->gb
, s
->byte_offset_bits
+ 3);
1511 /* Decode a single frame in the current superframe - return -1 if
1512 there was a decoding error, or the number of samples decoded.
1515 int wma_decode_superframe_frame(WMADecodeContext
* s
,
1516 int32_t* samples
, /*output*/
1517 uint8_t *buf
, /*input*/
1523 if ((s
->use_bit_reservoir
) && (s
->current_frame
== 0))
1525 if (s
->last_superframe_len
> 0)
1527 /* add s->bit_offset bits to last frame */
1528 if ((s
->last_superframe_len
+ ((s
->bit_offset
+ 7) >> 3)) >
1529 MAX_CODED_SUPERFRAME_SIZE
)
1531 DEBUGF("superframe size too large error\n");
1534 q
= s
->last_superframe
+ s
->last_superframe_len
;
1535 len
= s
->bit_offset
;
1538 *q
++ = (get_bits
)(&s
->gb
, 8);
1543 *q
++ = (get_bits
)(&s
->gb
, len
) << (8 - len
);
1546 /* XXX: s->bit_offset bits into last frame */
1547 init_get_bits(&s
->gb
, s
->last_superframe
, MAX_CODED_SUPERFRAME_SIZE
*8);
1548 /* skip unused bits */
1549 if (s
->last_bitoffset
> 0)
1550 skip_bits(&s
->gb
, s
->last_bitoffset
);
1552 /* this frame is stored in the last superframe and in the
1554 if (wma_decode_frame(s
, samples
) < 0)
1561 /* read each frame starting from s->bit_offset */
1562 pos
= s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3;
1563 init_get_bits(&s
->gb
, buf
+ (pos
>> 3), (MAX_CODED_SUPERFRAME_SIZE
- (pos
>> 3))*8);
1566 skip_bits(&s
->gb
, len
);
1568 s
->reset_block_lengths
= 1;
1571 /* If we haven't decoded a frame yet, do it now */
1574 if (wma_decode_frame(s
, samples
) < 0)
1582 if ((s
->use_bit_reservoir
) && (s
->current_frame
== s
->nb_frames
))
1584 /* we copy the end of the frame in the last frame buffer */
1585 pos
= get_bits_count(&s
->gb
) + ((s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3) & ~7);
1586 s
->last_bitoffset
= pos
& 7;
1588 len
= buf_size
- pos
;
1589 if (len
> MAX_CODED_SUPERFRAME_SIZE
|| len
< 0)
1591 DEBUGF("superframe size too large error after decodeing\n");
1594 s
->last_superframe_len
= len
;
1595 memcpy(s
->last_superframe
, buf
+ pos
, len
);
1598 return s
->frame_len
;
1601 /* when error, we reset the bit reservoir */
1603 s
->last_superframe_len
= 0;