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
;
56 //static variables that replace malloced stuff
57 fixed32 stat0
[2048], stat1
[1024], stat2
[512], stat3
[256], stat4
[128]; //these are the MDCT reconstruction windows
59 uint16_t *runtabarray
[2], *levtabarray
[2]; //these are VLC lookup tables
61 uint16_t runtab0
[1336], runtab1
[1336], levtab0
[1336], levtab1
[1336]; //these could be made smaller since only one can be 1336
63 /*putting these in IRAM actually makes PP slower*/
64 VLC_TYPE vlcbuf1
[2550][2];
65 VLC_TYPE vlcbuf2
[2550][2];
66 VLC_TYPE vlcbuf3
[360][2];
67 VLC_TYPE vlcbuf4
[540][2];
71 #include "wmadata.h" // PJJ
76 * Helper functions for wma_window.
83 void vector_fmul_add_add(fixed32
*dst
, const fixed32
*data
,
84 const fixed32
*window
, int n
)
86 /* Block sizes are always power of two */
89 "ldmia %[d]!, {r0, r1};"
90 "ldmia %[w]!, {r4, r5};"
91 /* consume the first data and window value so we can use those
93 "smull r8, r9, r0, r4;"
94 "ldmia %[dst], {r0, r4};"
95 "add r0, r0, r9, lsl #1;" /* *dst=*dst+(r9<<1)*/
96 "smull r8, r9, r1, r5;"
97 "add r1, r4, r9, lsl #1;"
98 "stmia %[dst]!, {r0, r1};"
99 "subs %[n], %[n], #2;"
101 : [d
] "+r" (data
), [w
] "+r" (window
), [dst
] "+r" (dst
), [n
] "+r" (n
)
102 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
106 void vector_fmul_reverse(fixed32
*dst
, const fixed32
*src0
, const fixed32
*src1
,
109 /* Block sizes are always power of two */
111 "add %[s1], %[s1], %[n], lsl #2;"
113 "ldmia %[s0]!, {r0, r1};"
114 "ldmdb %[s1]!, {r4, r5};"
115 "smull r8, r9, r0, r5;"
116 "mov r0, r9, lsl #1;"
117 "smull r8, r9, r1, r4;"
118 "mov r1, r9, lsl #1;"
119 "stmia %[dst]!, {r0, r1};"
120 "subs %[n], %[n], #2;"
122 : [s0
] "+r" (src0
), [s1
] "+r" (src1
), [dst
] "+r" (dst
), [n
] "+r" (len
)
123 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
126 #elif defined(CPU_COLDFIRE)
129 void vector_fmul_add_add(fixed32
*dst
, const fixed32
*data
,
130 const fixed32
*window
, int n
)
132 /* Block sizes are always power of two. Smallest block is always way bigger
136 "movem.l (%[d]), %%d0-%%d3;"
137 "movem.l (%[w]), %%d4-%%d5/%%a0-%%a1;"
138 "mac.l %%d0, %%d4, %%acc0;"
139 "mac.l %%d1, %%d5, %%acc1;"
140 "mac.l %%d2, %%a0, %%acc2;"
141 "mac.l %%d3, %%a1, %%acc3;"
142 "lea.l (16, %[d]), %[d];"
143 "lea.l (16, %[w]), %[w];"
144 "movclr.l %%acc0, %%d0;"
145 "movclr.l %%acc1, %%d1;"
146 "movclr.l %%acc2, %%d2;"
147 "movclr.l %%acc3, %%d3;"
148 "add.l %%d0, (%[dst])+;"
149 "add.l %%d1, (%[dst])+;"
150 "add.l %%d2, (%[dst])+;"
151 "add.l %%d3, (%[dst])+;"
154 : [d
] "+a" (data
), [w
] "+a" (window
), [dst
] "+a" (dst
), [n
] "+d" (n
)
155 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
159 void vector_fmul_reverse(fixed32
*dst
, const fixed32
*src0
, const fixed32
*src1
,
162 /* Block sizes are always power of two. Smallest block is always way bigger
165 "lea.l (-16, %[s1], %[n]*4), %[s1];"
167 "movem.l (%[s0]), %%d0-%%d3;"
168 "movem.l (%[s1]), %%d4-%%d5/%%a0-%%a1;"
169 "mac.l %%d0, %%a1, %%acc0;"
170 "mac.l %%d1, %%a0, %%acc1;"
171 "mac.l %%d2, %%d5, %%acc2;"
172 "mac.l %%d3, %%d4, %%acc3;"
173 "lea.l (16, %[s0]), %[s0];"
174 "lea.l (-16, %[s1]), %[s1];"
175 "movclr.l %%acc0, %%d0;"
176 "movclr.l %%acc1, %%d1;"
177 "movclr.l %%acc2, %%d2;"
178 "movclr.l %%acc3, %%d3;"
179 "movem.l %%d0-%%d3, (%[dst]);"
180 "lea.l (16, %[dst]), %[dst];"
183 : [s0
] "+a" (src0
), [s1
] "+a" (src1
), [dst
] "+a" (dst
), [n
] "+d" (len
)
184 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
189 static inline void vector_fmul_add_add(fixed32
*dst
, const fixed32
*src0
, const fixed32
*src1
, int len
){
192 dst
[i
] = fixmul32b(src0
[i
], src1
[i
]) + dst
[i
];
195 static inline void vector_fmul_reverse(fixed32
*dst
, const fixed32
*src0
, const fixed32
*src1
, int len
){
199 dst
[i
] = fixmul32b(src0
[i
], src1
[-i
]);
205 * Apply MDCT window and add into output.
207 * We ensure that when the windows overlap their squared sum
208 * is always 1 (MDCT reconstruction rule).
210 * The Vorbis I spec has a great diagram explaining this process.
211 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
213 static void wma_window(WMADecodeContext
*s
, fixed32
*in
, fixed32
*out
)
215 //float *in = s->output;
216 int block_len
, bsize
, n
;
219 /*previous block was larger, so we'll use the size of the current block to set the window size*/
220 if (s
->block_len_bits
<= s
->prev_block_len_bits
) {
221 block_len
= s
->block_len
;
222 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
224 vector_fmul_add_add(out
, in
, s
->windows
[bsize
], block_len
);
227 /*previous block was smaller or the same size, so use it's size to set the window length*/
228 block_len
= 1 << s
->prev_block_len_bits
;
229 /*find the middle of the two overlapped blocks, this will be the first overlapped sample*/
230 n
= (s
->block_len
- block_len
) / 2;
231 bsize
= s
->frame_len_bits
- s
->prev_block_len_bits
;
233 vector_fmul_add_add(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
235 memcpy(out
+n
+block_len
, in
+n
+block_len
, n
*sizeof(fixed32
));
237 /* Advance to the end of the current block and prepare to window it for the next block.
238 * Since the window function needs to be reversed, we do it backwards starting with the
239 * last sample and moving towards the first
245 if (s
->block_len_bits
<= s
->next_block_len_bits
) {
246 block_len
= s
->block_len
;
247 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
249 vector_fmul_reverse(out
, in
, s
->windows
[bsize
], block_len
);
252 block_len
= 1 << s
->next_block_len_bits
;
253 n
= (s
->block_len
- block_len
) / 2;
254 bsize
= s
->frame_len_bits
- s
->next_block_len_bits
;
256 memcpy(out
, in
, n
*sizeof(fixed32
));
258 vector_fmul_reverse(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
260 memset(out
+n
+block_len
, 0, n
*sizeof(fixed32
));
267 /* XXX: use same run/length optimization as mpeg decoders */
268 static void init_coef_vlc(VLC
*vlc
,
269 uint16_t **prun_table
, uint16_t **plevel_table
,
270 const CoefVLCTable
*vlc_table
, int tab
)
272 int n
= vlc_table
->n
;
273 const uint8_t *table_bits
= vlc_table
->huffbits
;
274 const uint32_t *table_codes
= vlc_table
->huffcodes
;
275 const uint16_t *levels_table
= vlc_table
->levels
;
276 uint16_t *run_table
, *level_table
;
281 init_vlc(vlc
, VLCBITS
, n
, table_bits
, 1, 1, table_codes
, 4, 4, 0);
283 run_table
= runtabarray
[tab
];
284 level_table
= levtabarray
[tab
];
295 level_table
[i
] = level
;
300 *prun_table
= run_table
;
301 *plevel_table
= level_table
;
304 int wma_decode_init(WMADecodeContext
* s
, asf_waveformatex_t
*wfx
)
306 //WMADecodeContext *s = avctx->priv_data;
307 int i
, flags1
, flags2
;
317 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
320 s
->sample_rate
= wfx
->rate
;
321 s
->nb_channels
= wfx
->channels
;
322 s
->bit_rate
= wfx
->bitrate
;
323 s
->block_align
= wfx
->blockalign
;
325 s
->coefs
= &coefsarray
;
327 if (wfx
->codec_id
== ASF_CODEC_ID_WMAV1
) {
329 } else if (wfx
->codec_id
== ASF_CODEC_ID_WMAV2
) {
332 /*one of those other wma flavors that don't have GPLed decoders */
336 /* extract flag infos */
339 extradata
= wfx
->data
;
340 if (s
->version
== 1 && wfx
->datalen
>= 4) {
341 flags1
= extradata
[0] | (extradata
[1] << 8);
342 flags2
= extradata
[2] | (extradata
[3] << 8);
343 }else if (s
->version
== 2 && wfx
->datalen
>= 6){
344 flags1
= extradata
[0] | (extradata
[1] << 8) |
345 (extradata
[2] << 16) | (extradata
[3] << 24);
346 flags2
= extradata
[4] | (extradata
[5] << 8);
348 s
->use_exp_vlc
= flags2
& 0x0001;
349 s
->use_bit_reservoir
= flags2
& 0x0002;
350 s
->use_variable_block_len
= flags2
& 0x0004;
352 /* compute MDCT block size */
353 if (s
->sample_rate
<= 16000){
354 s
->frame_len_bits
= 9;
355 }else if (s
->sample_rate
<= 22050 ||
356 (s
->sample_rate
<= 32000 && s
->version
== 1)){
357 s
->frame_len_bits
= 10;
359 s
->frame_len_bits
= 11;
361 s
->frame_len
= 1 << s
->frame_len_bits
;
362 if (s
-> use_variable_block_len
)
365 nb
= ((flags2
>> 3) & 3) + 1;
366 if ((s
->bit_rate
/ s
->nb_channels
) >= 32000)
370 nb_max
= s
->frame_len_bits
- BLOCK_MIN_BITS
; //max is 11-7
373 s
->nb_block_sizes
= nb
+ 1;
377 s
->nb_block_sizes
= 1;
380 /* init rate dependant parameters */
381 s
->use_noise_coding
= 1;
382 high_freq
= itofix64(s
->sample_rate
) >> 1;
385 /* if version 2, then the rates are normalized */
386 sample_rate1
= s
->sample_rate
;
389 if (sample_rate1
>= 44100)
390 sample_rate1
= 44100;
391 else if (sample_rate1
>= 22050)
392 sample_rate1
= 22050;
393 else if (sample_rate1
>= 16000)
394 sample_rate1
= 16000;
395 else if (sample_rate1
>= 11025)
396 sample_rate1
= 11025;
397 else if (sample_rate1
>= 8000)
401 fixed64 tmp
= itofix64(s
->bit_rate
);
402 fixed64 tmp2
= itofix64(s
->nb_channels
* s
->sample_rate
);
403 bps
= fixdiv64(tmp
, tmp2
);
404 fixed64 tim
= bps
* s
->frame_len
;
405 fixed64 tmpi
= fixdiv64(tim
,itofix64(8));
406 s
->byte_offset_bits
= av_log2(fixtoi64(tmpi
+0x8000)) + 2;
408 /* compute high frequency value and choose if noise coding should
411 if (s
->nb_channels
== 2)
412 bps1
= fixmul32(bps
,0x1999a);
413 if (sample_rate1
== 44100)
416 s
->use_noise_coding
= 0;
418 high_freq
= fixmul32(high_freq
,0x6666);
420 else if (sample_rate1
== 22050)
423 s
->use_noise_coding
= 0;
424 else if (bps1
>= 0xb852)
425 high_freq
= fixmul32(high_freq
,0xb333);
427 high_freq
= fixmul32(high_freq
,0x999a);
429 else if (sample_rate1
== 16000)
432 high_freq
= fixmul32(high_freq
,0x8000);
434 high_freq
= fixmul32(high_freq
,0x4ccd);
436 else if (sample_rate1
== 11025)
438 high_freq
= fixmul32(high_freq
,0xb333);
440 else if (sample_rate1
== 8000)
444 high_freq
= fixmul32(high_freq
,0x8000);
446 else if (bps
> 0xc000)
448 s
->use_noise_coding
= 0;
452 high_freq
= fixmul32(high_freq
,0xa666);
459 high_freq
= fixmul32(high_freq
,0xc000);
461 else if (bps
>= 0x999a)
463 high_freq
= fixmul32(high_freq
,0x999a);
467 high_freq
= fixmul32(high_freq
,0x8000);
471 /* compute the scale factor band sizes for each MDCT block size */
473 int a
, b
, pos
, lpos
, k
, block_len
, i
, j
, n
;
474 const uint8_t *table
;
484 for(k
= 0; k
< s
->nb_block_sizes
; ++k
)
486 block_len
= s
->frame_len
>> k
;
493 a
= wma_critical_freqs
[i
];
495 pos
= ((block_len
* 2 * a
) + (b
>> 1)) / b
;
498 s
->exponent_bands
[0][i
] = pos
- lpos
;
499 if (pos
>= block_len
)
506 s
->exponent_sizes
[0] = i
;
510 /* hardcoded tables */
512 a
= s
->frame_len_bits
- BLOCK_MIN_BITS
- k
;
515 if (s
->sample_rate
>= 44100)
516 table
= exponent_band_44100
[a
];
517 else if (s
->sample_rate
>= 32000)
518 table
= exponent_band_32000
[a
];
519 else if (s
->sample_rate
>= 22050)
520 table
= exponent_band_22050
[a
];
526 s
->exponent_bands
[k
][i
] = table
[i
];
527 s
->exponent_sizes
[k
] = n
;
535 a
= wma_critical_freqs
[i
];
537 pos
= ((block_len
* 2 * a
) + (b
<< 1)) / (4 * b
);
542 s
->exponent_bands
[k
][j
++] = pos
- lpos
;
543 if (pos
>= block_len
)
547 s
->exponent_sizes
[k
] = j
;
551 /* max number of coefs */
552 s
->coefs_end
[k
] = (s
->frame_len
- ((s
->frame_len
* 9) / 100)) >> k
;
553 /* high freq computation */
555 fixed32 tmp1
= high_freq
*2; /* high_freq is a fixed32!*/
556 fixed32 tmp2
=itofix32(s
->sample_rate
>>1);
557 s
->high_band_start
[k
] = fixtoi32( fixdiv32(tmp1
, tmp2
) * (block_len
>>1) +0x8000);
560 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
561 s->sample_rate + 0.5);*/
563 n
= s
->exponent_sizes
[k
];
570 pos
+= s
->exponent_bands
[k
][i
];
572 if (start
< s
->high_band_start
[k
])
573 start
= s
->high_band_start
[k
];
574 if (end
> s
->coefs_end
[k
])
575 end
= s
->coefs_end
[k
];
577 s
->exponent_high_bands
[k
][j
++] = end
- start
;
579 s
->exponent_high_sizes
[k
] = j
;
585 for(i
= 0; i
< s
->nb_block_sizes
; ++i
)
587 ff_mdct_init(&s
->mdct_ctx
[i
], s
->frame_len_bits
- i
+ 1, 1);
590 /*ffmpeg uses malloc to only allocate as many window sizes as needed. However, we're really only interested in the worst case memory usage.
591 * In the worst case you can have 5 window sizes, 128 doubling up 2048
592 * Smaller windows are handled differently.
593 * Since we don't have malloc, just statically allocate this
602 /* init MDCT windows : simple sinus window */
603 for(i
= 0; i
< s
->nb_block_sizes
; i
++)
607 n
= 1 << (s
->frame_len_bits
- i
);
608 //window = av_malloc(sizeof(fixed32) * n);
611 //fixed32 n2 = itofix32(n<<1); //2x the window length
612 //alpha = fixdiv32(M_PI_F, n2); //PI / (2x Window length) == PI<<(s->frame_len_bits - i+1)
614 //alpha = M_PI_F>>(s->frame_len_bits - i+1);
615 alpha
= (1<<15)>>(s
->frame_len_bits
- i
+1); /* this calculates 0.5/(2*n) */
618 fixed32 j2
= itofix32(j
) + 0x8000;
619 window
[j
] = fsincos(fixmul32(j2
,alpha
)<<16, 0); //alpha between 0 and pi/2
622 //printf("created window\n");
623 s
->windows
[i
] = window
;
624 //printf("assigned window\n");
627 s
->reset_block_lengths
= 1;
629 if (s
->use_noise_coding
)
631 /* init the noise generator */
634 s
->noise_mult
= 0x51f;
635 s
->noise_table
= noisetable_exp
;
639 s
->noise_mult
= 0xa3d;
640 /* LSP values are simply 2x the EXP values */
641 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
642 noisetable_exp
[i
] = noisetable_exp
[i
]<< 1;
643 s
->noise_table
= noisetable_exp
;
650 norm
= 0; // PJJ: near as makes any diff to 0!
651 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
653 seed
= seed
* 314159 + 1;
654 s
->noise_table
[i
] = itofix32((int)seed
) * norm
;
659 s
->hgain_vlc
.table
= vlcbuf4
;
660 init_vlc(&s
->hgain_vlc
, HGAINVLCBITS
, sizeof(hgain_huffbits
),
661 hgain_huffbits
, 1, 1,
662 hgain_huffcodes
, 2, 2, 0);
668 s
->exp_vlc
.table
= vlcbuf3
;
670 init_vlc(&s
->exp_vlc
, EXPVLCBITS
, sizeof(scale_huffbits
),
671 scale_huffbits
, 1, 1,
672 scale_huffcodes
, 4, 4, 0);
676 wma_lsp_to_curve_init(s
, s
->frame_len
);
679 /* choose the VLC tables for the coefficients */
681 if (s
->sample_rate
>= 32000)
685 else if (bps1
< 0x128f6)
689 runtabarray
[0] = runtab0
; runtabarray
[1] = runtab1
;
690 levtabarray
[0] = levtab0
; levtabarray
[1] = levtab1
;
692 s
->coef_vlc
[0].table
= vlcbuf1
;
693 s
->coef_vlc
[0].table_allocated
= 24576/4;
694 s
->coef_vlc
[1].table
= vlcbuf2
;
695 s
->coef_vlc
[1].table_allocated
= 14336/4;
698 init_coef_vlc(&s
->coef_vlc
[0], &s
->run_table
[0], &s
->level_table
[0],
699 &coef_vlcs
[coef_vlc_table
* 2], 0);
700 init_coef_vlc(&s
->coef_vlc
[1], &s
->run_table
[1], &s
->level_table
[1],
701 &coef_vlcs
[coef_vlc_table
* 2 + 1], 1);
703 s
->last_superframe_len
= 0;
704 s
->last_bitoffset
= 0;
710 /* compute x^-0.25 with an exponent and mantissa table. We use linear
711 interpolation to reduce the mantissa table size at a small speed
712 expense (linear interpolation approximately doubles the number of
713 bits of precision). */
714 static inline fixed32
pow_m1_4(WMADecodeContext
*s
, fixed32 x
)
725 m
= (u
.v
>> (23 - LSP_POW_BITS
)) & ((1 << LSP_POW_BITS
) - 1);
726 /* build interpolation scale: 1 <= t < 2. */
727 t
.v
= ((u
.v
<< LSP_POW_BITS
) & ((1 << 23) - 1)) | (127 << 23);
728 a
= s
->lsp_pow_m_table1
[m
];
729 b
= s
->lsp_pow_m_table2
[m
];
731 /* lsp_pow_e_table contains 32.32 format */
732 /* TODO: Since we're unlikely have value that cover the whole
733 * IEEE754 range, we probably don't need to have all possible exponents */
735 return (lsp_pow_e_table
[e
] * (a
+ fixmul32(b
, ftofix32(t
.f
))) >>32);
738 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
)
740 fixed32 wdel
, a
, b
, temp
, temp2
;
743 wdel
= fixdiv32(M_PI_F
, itofix32(frame_len
));
744 temp
= fixdiv32(itofix32(1), itofix32(frame_len
));
745 for (i
=0; i
<frame_len
; ++i
)
747 /* TODO: can probably reuse the trig_init values here */
748 fsincos((temp
*i
)<<15, &temp2
);
749 /* get 3 bits headroom + 1 bit from not doubleing the values */
750 s
->lsp_cos_table
[i
] = temp2
>>3;
753 /* NOTE: these two tables are needed to avoid two operations in
758 /*double check this later*/
759 for(i
=(1 << LSP_POW_BITS
) - 1;i
>=0;i
--)
761 m
= (1 << LSP_POW_BITS
) + i
;
762 a
= pow_a_table
[ix
++]<<4;
763 s
->lsp_pow_m_table1
[i
] = 2 * a
- b
;
764 s
->lsp_pow_m_table2
[i
] = b
- a
;
770 /* NOTE: We use the same code as Vorbis here */
771 /* XXX: optimize it further with SSE/3Dnow */
772 static void wma_lsp_to_curve(WMADecodeContext
*s
,
774 fixed32
*val_max_ptr
,
779 fixed32 p
, q
, w
, v
, val_max
, temp
, temp2
;
784 /* shift by 2 now to reduce rounding error,
785 * we can renormalize right before pow_m1_4
790 w
= s
->lsp_cos_table
[i
];
792 for (j
=1;j
<NB_LSP_COEFS
;j
+=2)
794 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
795 temp2
= ((w
- (lsp
[j
- 1]<<11)));
798 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
799 q
= fixmul32b(q
, temp2
)<<4;
800 p
= fixmul32b(p
, (w
- (lsp
[j
]<<11)))<<4;
803 /* 2 in 5.27 format is 0x10000000 */
804 p
= fixmul32(p
, fixmul32b(p
, (0x10000000 - w
)))<<3;
805 q
= fixmul32(q
, fixmul32b(q
, (0x10000000 + w
)))<<3;
807 v
= (p
+ q
) >>9; /* p/q end up as 16.16 */
814 *val_max_ptr
= val_max
;
817 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
818 static void decode_exp_lsp(WMADecodeContext
*s
, int ch
)
820 fixed32 lsp_coefs
[NB_LSP_COEFS
];
823 for (i
= 0; i
< NB_LSP_COEFS
; ++i
)
825 if (i
== 0 || i
>= 8)
826 val
= get_bits(&s
->gb
, 3);
828 val
= get_bits(&s
->gb
, 4);
829 lsp_coefs
[i
] = lsp_codebook
[i
][val
];
834 &s
->max_exponent
[ch
],
839 /* decode exponents coded with VLC codes */
840 static int decode_exp_vlc(WMADecodeContext
*s
, int ch
)
842 int last_exp
, n
, code
;
843 const uint16_t *ptr
, *band_ptr
;
844 fixed32 v
, max_scale
;
847 /*accommodate the 16 negative indices */
848 const fixed32
*pow_10_to_yover16_ptr
= &pow_10_to_yover16
[16];
850 band_ptr
= s
->exponent_bands
[s
->frame_len_bits
- s
->block_len_bits
];
852 q
= s
->exponents
[ch
];
853 q_end
= q
+ s
->block_len
;
857 if (s
->version
== 1) //wmav1 only
859 last_exp
= get_bits(&s
->gb
, 5) + 10;
860 /* XXX: use a table */
861 v
= pow_10_to_yover16_ptr
[last_exp
];
874 code
= get_vlc2(&s
->gb
, s
->exp_vlc
.table
, EXPVLCBITS
, EXPMAX
);
879 /* NOTE: this offset is the same as MPEG4 AAC ! */
880 last_exp
+= code
- 60;
881 /* XXX: use a table */
882 v
= pow_10_to_yover16_ptr
[last_exp
];
896 s
->max_exponent
[ch
] = max_scale
;
900 /* return 0 if OK. return 1 if last block of frame. return -1 if
901 unrecorrable error. */
902 static int wma_decode_block(WMADecodeContext
*s
)
904 int n
, v
, a
, ch
, code
, bsize
;
905 int coef_nb_bits
, total_gain
;
906 int nb_coefs
[MAX_CHANNELS
];
909 DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s
->block_num
, s
->block_len
, s
->frame_len
);
911 /* compute current block length */
912 if (s
->use_variable_block_len
)
914 n
= av_log2(s
->nb_block_sizes
- 1) + 1;
916 if (s
->reset_block_lengths
)
918 s
->reset_block_lengths
= 0;
919 v
= get_bits(&s
->gb
, n
);
920 if (v
>= s
->nb_block_sizes
)
924 s
->prev_block_len_bits
= s
->frame_len_bits
- v
;
925 v
= get_bits(&s
->gb
, n
);
926 if (v
>= s
->nb_block_sizes
)
930 s
->block_len_bits
= s
->frame_len_bits
- v
;
934 /* update block lengths */
935 s
->prev_block_len_bits
= s
->block_len_bits
;
936 s
->block_len_bits
= s
->next_block_len_bits
;
938 v
= get_bits(&s
->gb
, n
);
940 if (v
>= s
->nb_block_sizes
)
942 // rb->splash(HZ*4, "v was %d", v); //5, 7
943 return -4; //this is it
946 //rb->splash(HZ, "passed v block (%d)!", v);
948 s
->next_block_len_bits
= s
->frame_len_bits
- v
;
952 /* fixed block len */
953 s
->next_block_len_bits
= s
->frame_len_bits
;
954 s
->prev_block_len_bits
= s
->frame_len_bits
;
955 s
->block_len_bits
= s
->frame_len_bits
;
957 /* now check if the block length is coherent with the frame length */
958 s
->block_len
= 1 << s
->block_len_bits
;
960 if ((s
->block_pos
+ s
->block_len
) > s
->frame_len
)
962 return -5; //oddly 32k sample from tracker fails here
965 if (s
->nb_channels
== 2)
967 s
->ms_stereo
= get_bits(&s
->gb
, 1);
970 for (ch
= 0; ch
< s
->nb_channels
; ++ch
)
972 a
= get_bits(&s
->gb
, 1);
973 s
->channel_coded
[ch
] = a
;
976 /* if no channel coded, no need to go further */
977 /* XXX: fix potential framing problems */
983 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
985 /* read total gain and extract corresponding number of bits for
986 coef escape coding */
990 a
= get_bits(&s
->gb
, 7);
1000 else if (total_gain
< 32)
1002 else if (total_gain
< 40)
1004 else if (total_gain
< 45)
1009 /* compute number of coefficients */
1010 n
= s
->coefs_end
[bsize
] - s
->coefs_start
;
1012 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1016 /* complex coding */
1017 if (s
->use_noise_coding
)
1020 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1022 if (s
->channel_coded
[ch
])
1025 n
= s
->exponent_high_sizes
[bsize
];
1028 a
= get_bits(&s
->gb
, 1);
1029 s
->high_band_coded
[ch
][i
] = a
;
1030 /* if noise coding, the coefficients are not transmitted */
1032 nb_coefs
[ch
] -= s
->exponent_high_bands
[bsize
][i
];
1036 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1038 if (s
->channel_coded
[ch
])
1040 int i
, n
, val
, code
;
1042 n
= s
->exponent_high_sizes
[bsize
];
1043 val
= (int)0x80000000;
1046 if (s
->high_band_coded
[ch
][i
])
1048 if (val
== (int)0x80000000)
1050 val
= get_bits(&s
->gb
, 7) - 19;
1054 //code = get_vlc(&s->gb, &s->hgain_vlc);
1055 code
= get_vlc2(&s
->gb
, s
->hgain_vlc
.table
, HGAINVLCBITS
, HGAINMAX
);
1062 s
->high_band_values
[ch
][i
] = val
;
1069 /* exponents can be reused in short blocks. */
1070 if ((s
->block_len_bits
== s
->frame_len_bits
) || get_bits(&s
->gb
, 1))
1072 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1074 if (s
->channel_coded
[ch
])
1078 if (decode_exp_vlc(s
, ch
) < 0)
1085 decode_exp_lsp(s
, ch
);
1087 s
->exponents_bsize
[ch
] = bsize
;
1092 /* parse spectral coefficients : just RLE encoding */
1093 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1095 if (s
->channel_coded
[ch
])
1098 int level
, run
, sign
, tindex
;
1099 int16_t *ptr
, *eptr
;
1100 const int16_t *level_table
, *run_table
;
1102 /* special VLC tables are used for ms stereo because
1103 there is potentially less energy there */
1104 tindex
= (ch
== 1 && s
->ms_stereo
);
1105 coef_vlc
= &s
->coef_vlc
[tindex
];
1106 run_table
= s
->run_table
[tindex
];
1107 level_table
= s
->level_table
[tindex
];
1109 ptr
= &s
->coefs1
[ch
][0];
1110 eptr
= ptr
+ nb_coefs
[ch
];
1111 memset(ptr
, 0, s
->block_len
* sizeof(int16_t));
1115 code
= get_vlc2(&s
->gb
, coef_vlc
->table
, VLCBITS
, VLCMAX
);
1116 //code = get_vlc(&s->gb, coef_vlc);
1129 level
= get_bits(&s
->gb
, coef_nb_bits
);
1130 /* NOTE: this is rather suboptimal. reading
1131 block_len_bits would be better */
1132 run
= get_bits(&s
->gb
, s
->frame_len_bits
);
1137 run
= run_table
[code
];
1138 level
= level_table
[code
];
1140 sign
= get_bits(&s
->gb
, 1);
1151 /* NOTE: EOB can be omitted */
1156 if (s
->version
== 1 && s
->nb_channels
>= 2)
1158 align_get_bits(&s
->gb
);
1163 int n4
= s
->block_len
>> 1;
1164 //mdct_norm = 0x10000;
1165 //mdct_norm = fixdiv32(mdct_norm,itofix32(n4));
1167 mdct_norm
= 0x10000>>(s
->block_len_bits
-1); //theres no reason to do a divide by two in fixed precision ...
1169 if (s
->version
== 1)
1171 fixed32 tmp
= fixsqrt32(itofix32(n4
));
1172 mdct_norm
*= tmp
; // PJJ : exercise this path
1177 /* finally compute the MDCT coefficients */
1178 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1180 if (s
->channel_coded
[ch
])
1183 fixed32
*exponents
, *exp_ptr
;
1184 fixed32
*coefs
, atemp
;
1187 fixed32 noise
, temp1
, temp2
, mult2
;
1188 int i
, j
, n
, n1
, last_high_band
, esize
;
1189 fixed32 exp_power
[HIGH_BAND_MAX_SIZE
];
1191 //total_gain, coefs1, mdctnorm are lossless
1193 coefs1
= s
->coefs1
[ch
];
1194 exponents
= s
->exponents
[ch
];
1195 esize
= s
->exponents_bsize
[ch
];
1196 coefs
= (*(s
->coefs
))[ch
];
1201 * Previously the IMDCT was run in 17.15 precision to avoid overflow. However rare files could
1202 * overflow here as well, so switch to 17.15 during coefs calculation.
1206 if (s
->use_noise_coding
)
1208 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1210 mult
= fixdiv64(pow_table
[total_gain
+20],Fixed32To64(s
->max_exponent
[ch
]));
1211 mult
= mult
* mdct_norm
; //what the hell? This is actually fixed64*2^16!
1214 /* very low freqs : noise */
1215 for(i
= 0;i
< s
->coefs_start
; ++i
)
1217 *coefs
++ = fixmul32( (fixmul32(s
->noise_table
[s
->noise_index
],(*exponents
++))>>4),Fixed32From64(mult1
)) >>1;
1218 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1221 n1
= s
->exponent_high_sizes
[bsize
];
1223 /* compute power of high bands */
1224 exp_ptr
= exponents
+
1225 s
->high_band_start
[bsize
] -
1227 last_high_band
= 0; /* avoid warning */
1230 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1231 s
->block_len_bits
][j
];
1232 if (s
->high_band_coded
[ch
][j
])
1236 for(i
= 0;i
< n
; ++i
)
1238 /*v is noramlized later on so its fixed format is irrelevant*/
1240 e2
+= fixmul32(v
, v
)>>3;
1242 exp_power
[j
] = e2
/n
; /*n is an int...*/
1248 /* main freqs and high freqs */
1253 n
= s
->high_band_start
[bsize
] -
1258 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1259 s
->block_len_bits
][j
];
1261 if (j
>= 0 && s
->high_band_coded
[ch
][j
])
1263 /* use noise with specified power */
1264 fixed32 tmp
= fixdiv32(exp_power
[j
],exp_power
[last_high_band
]);
1265 mult1
= (fixed64
)fixsqrt32(tmp
);
1266 /* XXX: use a table */
1267 /*mult1 is 48.16, pow_table is 48.16*/
1268 mult1
= mult1
* pow_table
[s
->high_band_values
[ch
][j
]+20] >> PRECISION
;
1270 /*this step has a fairly high degree of error for some reason*/
1271 mult1
= fixdiv64(mult1
,fixmul32(s
->max_exponent
[ch
],s
->noise_mult
));
1273 mult1
= mult1
*mdct_norm
>>PRECISION
;
1274 for(i
= 0;i
< n
; ++i
)
1276 noise
= s
->noise_table
[s
->noise_index
];
1277 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1278 *coefs
++ = fixmul32((fixmul32(*exponents
,noise
)>>4),Fixed32From64(mult1
)) >>1;
1284 /* coded values + small noise */
1285 for(i
= 0;i
< n
; ++i
)
1287 // PJJ: check code path
1288 noise
= s
->noise_table
[s
->noise_index
];
1289 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1291 /*don't forget to renormalize the noise*/
1292 temp1
= (((int32_t)*coefs1
++)<<16) + (noise
>>4);
1293 temp2
= fixmul32(*exponents
, mult
>>17);
1294 *coefs
++ = fixmul32(temp1
, temp2
);
1300 /* very high freqs : noise */
1301 n
= s
->block_len
- s
->coefs_end
[bsize
];
1302 mult2
= fixmul32(mult
>>16,exponents
[-1]) ; /*the work around for 32.32 vars are getting stupid*/
1303 for (i
= 0; i
< n
; ++i
)
1305 /*renormalize the noise product and then reduce to 17.15 precison*/
1306 *coefs
++ = fixmul32(s
->noise_table
[s
->noise_index
],mult2
) >>5;
1308 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1313 /*Noise coding not used, simply convert from exp to fixed representation*/
1316 fixed32 mult3
= (fixed32
)(fixdiv64(pow_table
[total_gain
+20],Fixed32To64(s
->max_exponent
[ch
])));
1317 mult3
= fixmul32(mult3
, mdct_norm
);
1321 /* XXX: optimize more, unrolling this loop in asm might be a good idea */
1323 for(i
= 0;i
< n
; ++i
)
1325 atemp
= (coefs1
[i
] * mult3
)>>1;
1326 *coefs
++=fixmul32(atemp
,exponents
[i
<<bsize
>>esize
]);
1328 n
= s
->block_len
- s
->coefs_end
[bsize
];
1329 memset(coefs
, 0, n
*sizeof(fixed32
));
1336 if (s
->ms_stereo
&& s
->channel_coded
[1])
1340 fixed32 (*coefs
)[MAX_CHANNELS
][BLOCK_MAX_SIZE
] = (s
->coefs
);
1342 /* nominal case for ms stereo: we do it before mdct */
1343 /* no need to optimize this case because it should almost
1345 if (!s
->channel_coded
[0])
1347 memset((*(s
->coefs
))[0], 0, sizeof(fixed32
) * s
->block_len
);
1348 s
->channel_coded
[0] = 1;
1351 for(i
= 0; i
< s
->block_len
; ++i
)
1355 (*coefs
)[0][i
] = a
+ b
;
1356 (*coefs
)[1][i
] = a
- b
;
1360 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1362 if (s
->channel_coded
[ch
])
1364 static fixed32 output
[BLOCK_MAX_SIZE
* 2] IBSS_ATTR
;
1369 n4
= s
->block_len
>>1;
1371 ff_imdct_calc(&s
->mdct_ctx
[bsize
],
1376 /* add in the frame */
1377 index
= (s
->frame_len
/ 2) + s
->block_pos
- n4
;
1379 wma_window(s
, output
, &s
->frame_out
[ch
][index
]);
1383 /* specific fast case for ms-stereo : add to second
1384 channel if it is not coded */
1385 if (s
->ms_stereo
&& !s
->channel_coded
[1])
1387 wma_window(s
, output
, &s
->frame_out
[1][index
]);
1392 /* update block number */
1394 s
->block_pos
+= s
->block_len
;
1395 if (s
->block_pos
>= s
->frame_len
)
1405 /* decode a frame of frame_len samples */
1406 static int wma_decode_frame(WMADecodeContext
*s
, int32_t *samples
)
1408 int ret
, i
, n
, ch
, incr
;
1411 // rb->splash(HZ, "in wma_decode_frame");
1413 /* read each block */
1420 ret
= wma_decode_block(s
);
1424 DEBUGF("wma_decode_block failed with code %d\n", ret
);
1433 /* return frame with full 30-bit precision */
1435 incr
= s
->nb_channels
;
1436 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1439 iptr
= s
->frame_out
[ch
];
1446 /* prepare for next block */
1447 memmove(&s
->frame_out
[ch
][0], &s
->frame_out
[ch
][s
->frame_len
],
1448 s
->frame_len
* sizeof(fixed32
));
1455 /* Initialise the superframe decoding */
1457 int wma_decode_superframe_init(WMADecodeContext
* s
,
1458 uint8_t *buf
, /*input*/
1463 s
->last_superframe_len
= 0;
1467 s
->current_frame
= 0;
1469 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1471 if (s
->use_bit_reservoir
)
1473 /* read super frame header */
1474 get_bits(&s
->gb
, 4); /* super frame index */
1475 s
->nb_frames
= get_bits(&s
->gb
, 4);
1477 if (s
->last_superframe_len
== 0)
1479 else if (s
->nb_frames
== 0)
1482 s
->bit_offset
= get_bits(&s
->gb
, s
->byte_offset_bits
+ 3);
1491 /* Decode a single frame in the current superframe - return -1 if
1492 there was a decoding error, or the number of samples decoded.
1495 int wma_decode_superframe_frame(WMADecodeContext
* s
,
1496 int32_t* samples
, /*output*/
1497 uint8_t *buf
, /*input*/
1503 if ((s
->use_bit_reservoir
) && (s
->current_frame
== 0))
1505 if (s
->last_superframe_len
> 0)
1507 /* add s->bit_offset bits to last frame */
1508 if ((s
->last_superframe_len
+ ((s
->bit_offset
+ 7) >> 3)) >
1509 MAX_CODED_SUPERFRAME_SIZE
)
1511 DEBUGF("superframe size too large error\n");
1514 q
= s
->last_superframe
+ s
->last_superframe_len
;
1515 len
= s
->bit_offset
;
1518 *q
++ = (get_bits
)(&s
->gb
, 8);
1523 *q
++ = (get_bits
)(&s
->gb
, len
) << (8 - len
);
1526 /* XXX: s->bit_offset bits into last frame */
1527 init_get_bits(&s
->gb
, s
->last_superframe
, MAX_CODED_SUPERFRAME_SIZE
*8);
1528 /* skip unused bits */
1529 if (s
->last_bitoffset
> 0)
1530 skip_bits(&s
->gb
, s
->last_bitoffset
);
1532 /* this frame is stored in the last superframe and in the
1534 if (wma_decode_frame(s
, samples
) < 0)
1541 /* read each frame starting from s->bit_offset */
1542 pos
= s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3;
1543 init_get_bits(&s
->gb
, buf
+ (pos
>> 3), (MAX_CODED_SUPERFRAME_SIZE
- (pos
>> 3))*8);
1546 skip_bits(&s
->gb
, len
);
1548 s
->reset_block_lengths
= 1;
1551 /* If we haven't decoded a frame yet, do it now */
1554 if (wma_decode_frame(s
, samples
) < 0)
1562 if ((s
->use_bit_reservoir
) && (s
->current_frame
== s
->nb_frames
))
1564 /* we copy the end of the frame in the last frame buffer */
1565 pos
= get_bits_count(&s
->gb
) + ((s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3) & ~7);
1566 s
->last_bitoffset
= pos
& 7;
1568 len
= buf_size
- pos
;
1569 if (len
> MAX_CODED_SUPERFRAME_SIZE
|| len
< 0)
1571 DEBUGF("superframe size too large error after decodeing\n");
1574 s
->last_superframe_len
= len
;
1575 memcpy(s
->last_superframe
, buf
+ pos
, len
);
1578 return s
->frame_len
;
1581 /* when error, we reset the bit reservoir */
1583 s
->last_superframe_len
= 0;