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.
31 #undef WMA_DEBUG /* enable when debugging wma */
37 #include "bitstream.h"
38 #include <string.h> /* memcpy() */
40 #define VLCBITS 7 /*7 is the lowest without glitching*/
41 #define VLCMAX ((22+VLCBITS-1)/VLCBITS)
44 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
46 #define HGAINVLCBITS 9
47 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
50 typedef struct CoefVLCTable
52 int n
; /* total number of codes */
53 const uint32_t *huffcodes
; /* VLC bit values */
54 const uint8_t *huffbits
; /* VLC bit size */
55 const uint16_t *levels
; /* table to build run/level tables */
59 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
);
61 int32_t coefsarray
[MAX_CHANNELS
][BLOCK_MAX_SIZE
] IBSS_ATTR
;
63 /* static variables that replace malloced stuff */
64 /* these are the MDCT reconstruction windows */
65 int32_t stat0
[2048], stat1
[1024], stat2
[512], stat3
[256], stat4
[128];
67 /* these are VLC lookup tables */
68 uint16_t *runtabarray
[2], *levtabarray
[2];
70 /* these could be made smaller since only one can be 1336 */
71 uint16_t runtab0
[1336], runtab1
[1336], levtab0
[1336], levtab1
[1336];
73 #define VLCBUF1SIZE 4598
74 #define VLCBUF2SIZE 3574
75 #define VLCBUF3SIZE 360
76 #define VLCBUF4SIZE 540
78 /*putting these in IRAM actually makes PP slower*/
80 VLC_TYPE vlcbuf1
[VLCBUF1SIZE
][2];
81 VLC_TYPE vlcbuf2
[VLCBUF2SIZE
][2];
82 VLC_TYPE vlcbuf3
[VLCBUF3SIZE
][2];
83 VLC_TYPE vlcbuf4
[VLCBUF4SIZE
][2];
85 #include "wmadata.h" // PJJ
88 * Helper functions for wma_window.
95 void vector_fmul_add_add(int32_t *dst
, const int32_t *data
,
96 const int32_t *window
, int n
)
98 /* Block sizes are always power of two */
101 "ldmia %[d]!, {r0, r1};"
102 "ldmia %[w]!, {r4, r5};"
103 /* consume the first data and window value so we can use those
105 "smull r8, r9, r0, r4;"
106 "ldmia %[dst], {r0, r4};"
107 "add r0, r0, r9, lsl #1;" /* *dst=*dst+(r9<<1)*/
108 "smull r8, r9, r1, r5;"
109 "add r1, r4, r9, lsl #1;"
110 "stmia %[dst]!, {r0, r1};"
111 "subs %[n], %[n], #2;"
113 : [d
] "+r" (data
), [w
] "+r" (window
), [dst
] "+r" (dst
), [n
] "+r" (n
)
114 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
118 void vector_fmul_reverse(int32_t *dst
, const int32_t *src0
, const int32_t *src1
,
121 /* Block sizes are always power of two */
123 "add %[s1], %[s1], %[n], lsl #2;"
125 "ldmia %[s0]!, {r0, r1};"
126 "ldmdb %[s1]!, {r4, r5};"
127 "smull r8, r9, r0, r5;"
128 "mov r0, r9, lsl #1;"
129 "smull r8, r9, r1, r4;"
130 "mov r1, r9, lsl #1;"
131 "stmia %[dst]!, {r0, r1};"
132 "subs %[n], %[n], #2;"
134 : [s0
] "+r" (src0
), [s1
] "+r" (src1
), [dst
] "+r" (dst
), [n
] "+r" (len
)
135 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
138 #elif defined(CPU_COLDFIRE)
141 void vector_fmul_add_add(int32_t *dst
, const int32_t *data
,
142 const int32_t *window
, int n
)
144 /* Block sizes are always power of two. Smallest block is always way bigger
148 "movem.l (%[d]), %%d0-%%d3;"
149 "movem.l (%[w]), %%d4-%%d5/%%a0-%%a1;"
150 "mac.l %%d0, %%d4, %%acc0;"
151 "mac.l %%d1, %%d5, %%acc1;"
152 "mac.l %%d2, %%a0, %%acc2;"
153 "mac.l %%d3, %%a1, %%acc3;"
154 "lea.l (16, %[d]), %[d];"
155 "lea.l (16, %[w]), %[w];"
156 "movclr.l %%acc0, %%d0;"
157 "movclr.l %%acc1, %%d1;"
158 "movclr.l %%acc2, %%d2;"
159 "movclr.l %%acc3, %%d3;"
160 "add.l %%d0, (%[dst])+;"
161 "add.l %%d1, (%[dst])+;"
162 "add.l %%d2, (%[dst])+;"
163 "add.l %%d3, (%[dst])+;"
166 : [d
] "+a" (data
), [w
] "+a" (window
), [dst
] "+a" (dst
), [n
] "+d" (n
)
167 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
171 void vector_fmul_reverse(int32_t *dst
, const int32_t *src0
, const int32_t *src1
,
174 /* Block sizes are always power of two. Smallest block is always way bigger
177 "lea.l (-16, %[s1], %[n]*4), %[s1];"
179 "movem.l (%[s0]), %%d0-%%d3;"
180 "movem.l (%[s1]), %%d4-%%d5/%%a0-%%a1;"
181 "mac.l %%d0, %%a1, %%acc0;"
182 "mac.l %%d1, %%a0, %%acc1;"
183 "mac.l %%d2, %%d5, %%acc2;"
184 "mac.l %%d3, %%d4, %%acc3;"
185 "lea.l (16, %[s0]), %[s0];"
186 "lea.l (-16, %[s1]), %[s1];"
187 "movclr.l %%acc0, %%d0;"
188 "movclr.l %%acc1, %%d1;"
189 "movclr.l %%acc2, %%d2;"
190 "movclr.l %%acc3, %%d3;"
191 "movem.l %%d0-%%d3, (%[dst]);"
192 "lea.l (16, %[dst]), %[dst];"
195 : [s0
] "+a" (src0
), [s1
] "+a" (src1
), [dst
] "+a" (dst
), [n
] "+d" (len
)
196 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
201 static inline void vector_fmul_add_add(int32_t *dst
, const int32_t *src0
, const int32_t *src1
, int len
){
204 dst
[i
] = fixmul32b(src0
[i
], src1
[i
]) + dst
[i
];
207 static inline void vector_fmul_reverse(int32_t *dst
, const int32_t *src0
, const int32_t *src1
, int len
){
211 dst
[i
] = fixmul32b(src0
[i
], src1
[-i
]);
217 * Apply MDCT window and add into output.
219 * We ensure that when the windows overlap their squared sum
220 * is always 1 (MDCT reconstruction rule).
222 * The Vorbis I spec has a great diagram explaining this process.
223 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
225 static void wma_window(WMADecodeContext
*s
, int32_t *in
, int32_t *out
)
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
) >> 1;
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(int32_t));
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
) >> 1;
265 bsize
= s
->frame_len_bits
- s
->next_block_len_bits
;
267 memcpy(out
, in
, n
*sizeof(int32_t));
269 vector_fmul_reverse(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
271 memset(out
+n
+block_len
, 0, n
*sizeof(int32_t));
275 /* XXX: use same run/length optimization as mpeg decoders */
276 static void init_coef_vlc(VLC
*vlc
,
277 uint16_t **prun_table
, uint16_t **plevel_table
,
278 const CoefVLCTable
*vlc_table
, int tab
)
280 int n
= vlc_table
->n
;
281 const uint8_t *table_bits
= vlc_table
->huffbits
;
282 const uint32_t *table_codes
= vlc_table
->huffcodes
;
283 const uint16_t *levels_table
= vlc_table
->levels
;
284 uint16_t *run_table
, *level_table
;
288 init_vlc(vlc
, VLCBITS
, n
, table_bits
, 1, 1, table_codes
, 4, 4, 0);
290 run_table
= runtabarray
[tab
];
291 level_table
= levtabarray
[tab
];
302 level_table
[i
] = level
;
307 *prun_table
= run_table
;
308 *plevel_table
= level_table
;
311 int wma_decode_init(WMADecodeContext
* s
, asf_waveformatex_t
*wfx
)
313 int i
, flags1
, flags2
;
323 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
326 s
->sample_rate
= wfx
->rate
;
327 s
->nb_channels
= wfx
->channels
;
328 s
->bit_rate
= wfx
->bitrate
;
329 s
->block_align
= wfx
->blockalign
;
331 s
->coefs
= &coefsarray
;
333 if (wfx
->codec_id
== ASF_CODEC_ID_WMAV1
) {
335 } else if (wfx
->codec_id
== ASF_CODEC_ID_WMAV2
) {
338 /*one of those other wma flavors that don't have GPLed decoders */
342 /* extract flag infos */
345 extradata
= wfx
->data
;
346 if (s
->version
== 1 && wfx
->datalen
>= 4) {
347 flags1
= extradata
[0] | (extradata
[1] << 8);
348 flags2
= extradata
[2] | (extradata
[3] << 8);
349 }else if (s
->version
== 2 && wfx
->datalen
>= 6){
350 flags1
= extradata
[0] | (extradata
[1] << 8) |
351 (extradata
[2] << 16) | (extradata
[3] << 24);
352 flags2
= extradata
[4] | (extradata
[5] << 8);
354 s
->use_exp_vlc
= flags2
& 0x0001;
355 s
->use_bit_reservoir
= flags2
& 0x0002;
356 s
->use_variable_block_len
= flags2
& 0x0004;
358 /* compute MDCT block size */
359 if (s
->sample_rate
<= 16000){
360 s
->frame_len_bits
= 9;
361 }else if (s
->sample_rate
<= 22050 ||
362 (s
->sample_rate
<= 32000 && s
->version
== 1)){
363 s
->frame_len_bits
= 10;
365 s
->frame_len_bits
= 11;
367 s
->frame_len
= 1 << s
->frame_len_bits
;
368 if (s
-> use_variable_block_len
)
371 nb
= ((flags2
>> 3) & 3) + 1;
372 if ((s
->bit_rate
/ s
->nb_channels
) >= 32000)
376 nb_max
= s
->frame_len_bits
- BLOCK_MIN_BITS
; /* max is 11-7 */
379 s
->nb_block_sizes
= nb
+ 1;
383 s
->nb_block_sizes
= 1;
386 /* init rate dependent parameters */
387 s
->use_noise_coding
= 1;
388 high_freq
= itofix64(s
->sample_rate
) >> 1;
390 /* if version 2, then the rates are normalized */
391 sample_rate1
= s
->sample_rate
;
394 if (sample_rate1
>= 44100)
395 sample_rate1
= 44100;
396 else if (sample_rate1
>= 22050)
397 sample_rate1
= 22050;
398 else if (sample_rate1
>= 16000)
399 sample_rate1
= 16000;
400 else if (sample_rate1
>= 11025)
401 sample_rate1
= 11025;
402 else if (sample_rate1
>= 8000)
406 int64_t tmp
= itofix64(s
->bit_rate
);
407 int64_t tmp2
= itofix64(s
->nb_channels
* s
->sample_rate
);
408 bps
= fixdiv64(tmp
, tmp2
);
409 int64_t tim
= bps
* s
->frame_len
;
410 int64_t tmpi
= fixdiv64(tim
,itofix64(8));
411 s
->byte_offset_bits
= av_log2(fixtoi64(tmpi
+0x8000)) + 2;
413 /* compute high frequency value and choose if noise coding should
416 if (s
->nb_channels
== 2)
417 bps1
= fixmul32(bps
,0x1999a);
418 if (sample_rate1
== 44100)
421 s
->use_noise_coding
= 0;
423 high_freq
= fixmul32(high_freq
,0x6666);
425 else if (sample_rate1
== 22050)
428 s
->use_noise_coding
= 0;
429 else if (bps1
>= 0xb852)
430 high_freq
= fixmul32(high_freq
,0xb333);
432 high_freq
= fixmul32(high_freq
,0x999a);
434 else if (sample_rate1
== 16000)
437 high_freq
= fixmul32(high_freq
,0x8000);
439 high_freq
= fixmul32(high_freq
,0x4ccd);
441 else if (sample_rate1
== 11025)
443 high_freq
= fixmul32(high_freq
,0xb333);
445 else if (sample_rate1
== 8000)
449 high_freq
= fixmul32(high_freq
,0x8000);
451 else if (bps
> 0xc000)
453 s
->use_noise_coding
= 0;
457 high_freq
= fixmul32(high_freq
,0xa666);
464 high_freq
= fixmul32(high_freq
,0xc000);
466 else if (bps
>= 0x999a)
468 high_freq
= fixmul32(high_freq
,0x999a);
472 high_freq
= fixmul32(high_freq
,0x8000);
476 /* compute the scale factor band sizes for each MDCT block size */
478 int a
, b
, pos
, lpos
, k
, block_len
, i
, j
, n
;
479 const uint8_t *table
;
489 for(k
= 0; k
< s
->nb_block_sizes
; ++k
)
491 block_len
= s
->frame_len
>> k
;
498 a
= wma_critical_freqs
[i
];
500 pos
= ((block_len
* 2 * a
) + (b
>> 1)) / b
;
503 s
->exponent_bands
[0][i
] = pos
- lpos
;
504 if (pos
>= block_len
)
511 s
->exponent_sizes
[0] = i
;
515 /* hardcoded tables */
517 a
= s
->frame_len_bits
- BLOCK_MIN_BITS
- k
;
520 if (s
->sample_rate
>= 44100)
521 table
= exponent_band_44100
[a
];
522 else if (s
->sample_rate
>= 32000)
523 table
= exponent_band_32000
[a
];
524 else if (s
->sample_rate
>= 22050)
525 table
= exponent_band_22050
[a
];
531 s
->exponent_bands
[k
][i
] = table
[i
];
532 s
->exponent_sizes
[k
] = n
;
540 a
= wma_critical_freqs
[i
];
542 pos
= ((block_len
* 2 * a
) + (b
<< 1)) / (4 * b
);
547 s
->exponent_bands
[k
][j
++] = pos
- lpos
;
548 if (pos
>= block_len
)
552 s
->exponent_sizes
[k
] = j
;
556 /* max number of coefs */
557 s
->coefs_end
[k
] = (s
->frame_len
- ((s
->frame_len
* 9) / 100)) >> k
;
558 /* high freq computation */
560 int32_t tmp1
= high_freq
*2; /* high_freq is a int32_t!*/
561 int32_t tmp2
=itofix32(s
->sample_rate
>>1);
562 s
->high_band_start
[k
] = fixtoi32( fixdiv32(tmp1
, tmp2
) * (block_len
>>1) +0x8000);
565 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
566 s->sample_rate + 0.5);*/
568 n
= s
->exponent_sizes
[k
];
575 pos
+= s
->exponent_bands
[k
][i
];
577 if (start
< s
->high_band_start
[k
])
578 start
= s
->high_band_start
[k
];
579 if (end
> s
->coefs_end
[k
])
580 end
= s
->coefs_end
[k
];
582 s
->exponent_high_bands
[k
][j
++] = end
- start
;
584 s
->exponent_high_sizes
[k
] = j
;
590 for(i
= 0; i
< s
->nb_block_sizes
; ++i
)
592 ff_mdct_init(&s
->mdct_ctx
[i
], s
->frame_len_bits
- i
+ 1, 1);
595 /*ffmpeg uses malloc to only allocate as many window sizes as needed.
596 * However, we're really only interested in the worst case memory usage.
597 * In the worst case you can have 5 window sizes, 128 doubling up 2048
598 * Smaller windows are handled differently.
599 * Since we don't have malloc, just statically allocate this
608 /* init MDCT windows : simple sinus window */
609 for(i
= 0; i
< s
->nb_block_sizes
; i
++)
613 n
= 1 << (s
->frame_len_bits
- i
);
616 alpha
= (1<<15)>>(s
->frame_len_bits
- i
+1); /* this calculates 0.5/(2*n) */
619 int32_t j2
= itofix32(j
) + 0x8000;
620 window
[j
] = fsincos(fixmul32(j2
,alpha
)<<16, 0); /* alpha between 0 and pi/2 */
623 s
->windows
[i
] = window
;
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 s
->hgain_vlc
.table_allocated
= VLCBUF4SIZE
;
661 init_vlc(&s
->hgain_vlc
, HGAINVLCBITS
, sizeof(hgain_huffbits
),
662 hgain_huffbits
, 1, 1,
663 hgain_huffcodes
, 2, 2, 0);
669 s
->exp_vlc
.table
= vlcbuf3
;
670 s
->exp_vlc
.table_allocated
= VLCBUF3SIZE
;
672 init_vlc(&s
->exp_vlc
, EXPVLCBITS
, sizeof(scale_huffbits
),
673 scale_huffbits
, 1, 1,
674 scale_huffcodes
, 4, 4, 0);
678 wma_lsp_to_curve_init(s
, s
->frame_len
);
681 /* choose the VLC tables for the coefficients */
683 if (s
->sample_rate
>= 32000)
687 else if (bps1
< 0x128f6)
691 runtabarray
[0] = runtab0
; runtabarray
[1] = runtab1
;
692 levtabarray
[0] = levtab0
; levtabarray
[1] = levtab1
;
694 s
->coef_vlc
[0].table
= vlcbuf1
;
695 s
->coef_vlc
[0].table_allocated
= VLCBUF1SIZE
;
696 s
->coef_vlc
[1].table
= vlcbuf2
;
697 s
->coef_vlc
[1].table_allocated
= VLCBUF2SIZE
;
699 init_coef_vlc(&s
->coef_vlc
[0], &s
->run_table
[0], &s
->level_table
[0],
700 &coef_vlcs
[coef_vlc_table
* 2], 0);
701 init_coef_vlc(&s
->coef_vlc
[1], &s
->run_table
[1], &s
->level_table
[1],
702 &coef_vlcs
[coef_vlc_table
* 2 + 1], 1);
704 s
->last_superframe_len
= 0;
705 s
->last_bitoffset
= 0;
711 /* compute x^-0.25 with an exponent and mantissa table. We use linear
712 interpolation to reduce the mantissa table size at a small speed
713 expense (linear interpolation approximately doubles the number of
714 bits of precision). */
715 static inline int32_t pow_m1_4(WMADecodeContext
*s
, int32_t x
)
726 m
= (u
.v
>> (23 - LSP_POW_BITS
)) & ((1 << LSP_POW_BITS
) - 1);
727 /* build interpolation scale: 1 <= t < 2. */
728 t
.v
= ((u
.v
<< LSP_POW_BITS
) & ((1 << 23) - 1)) | (127 << 23);
729 a
= s
->lsp_pow_m_table1
[m
];
730 b
= s
->lsp_pow_m_table2
[m
];
732 /* lsp_pow_e_table contains 32.32 format */
733 /* TODO: Since we're unlikely have value that cover the whole
734 * IEEE754 range, we probably don't need to have all possible exponents */
736 return (lsp_pow_e_table
[e
] * (a
+ fixmul32(b
, ftofix32(t
.f
))) >>32);
739 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
)
741 int32_t wdel
, a
, b
, temp
, temp2
;
744 wdel
= fixdiv32(M_PI_F
, itofix32(frame_len
));
745 temp
= fixdiv32(itofix32(1), itofix32(frame_len
));
746 for (i
=0; i
<frame_len
; ++i
)
748 /* TODO: can probably reuse the trig_init values here */
749 fsincos((temp
*i
)<<15, &temp2
);
750 /* get 3 bits headroom + 1 bit from not doubleing the values */
751 s
->lsp_cos_table
[i
] = temp2
>>3;
754 /* NOTE: these two tables are needed to avoid two operations in
759 /*double check this later*/
760 for(i
=(1 << LSP_POW_BITS
) - 1;i
>=0;i
--)
762 m
= (1 << LSP_POW_BITS
) + i
;
763 a
= pow_a_table
[ix
++]<<4;
764 s
->lsp_pow_m_table1
[i
] = 2 * a
- b
;
765 s
->lsp_pow_m_table2
[i
] = b
- a
;
771 /* NOTE: We use the same code as Vorbis here */
772 /* XXX: optimize it further with SSE/3Dnow */
773 static void wma_lsp_to_curve(WMADecodeContext
*s
,
775 int32_t *val_max_ptr
,
780 int32_t p
, q
, w
, v
, val_max
, temp
, temp2
;
785 /* shift by 2 now to reduce rounding error,
786 * we can renormalize right before pow_m1_4
791 w
= s
->lsp_cos_table
[i
];
793 for (j
=1;j
<NB_LSP_COEFS
;j
+=2)
795 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
796 temp2
= ((w
- (lsp
[j
- 1]<<11)));
799 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
800 q
= fixmul32b(q
, temp2
)<<4;
801 p
= fixmul32b(p
, (w
- (lsp
[j
]<<11)))<<4;
804 /* 2 in 5.27 format is 0x10000000 */
805 p
= fixmul32(p
, fixmul32b(p
, (0x10000000 - w
)))<<3;
806 q
= fixmul32(q
, fixmul32b(q
, (0x10000000 + w
)))<<3;
808 v
= (p
+ q
) >>9; /* p/q end up as 16.16 */
815 *val_max_ptr
= val_max
;
818 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
819 static void decode_exp_lsp(WMADecodeContext
*s
, int ch
)
821 int32_t lsp_coefs
[NB_LSP_COEFS
];
824 for (i
= 0; i
< NB_LSP_COEFS
; ++i
)
826 if (i
== 0 || i
>= 8)
827 val
= get_bits(&s
->gb
, 3);
829 val
= get_bits(&s
->gb
, 4);
830 lsp_coefs
[i
] = lsp_codebook
[i
][val
];
835 &s
->max_exponent
[ch
],
840 /* decode exponents coded with VLC codes */
841 static int decode_exp_vlc(WMADecodeContext
*s
, int ch
)
843 int last_exp
, n
, code
;
844 const uint16_t *ptr
, *band_ptr
;
845 int32_t v
, max_scale
;
848 /*accommodate the 60 negative indices */
849 const int32_t *pow_10_to_yover16_ptr
= &pow_10_to_yover16
[61];
851 band_ptr
= s
->exponent_bands
[s
->frame_len_bits
- s
->block_len_bits
];
853 q
= s
->exponents
[ch
];
854 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
];
875 code
= get_vlc2(&s
->gb
, s
->exp_vlc
.table
, EXPVLCBITS
, EXPMAX
);
880 /* NOTE: this offset is the same as MPEG4 AAC ! */
881 last_exp
+= code
- 60;
882 /* XXX: use a table */
883 v
= pow_10_to_yover16_ptr
[last_exp
];
897 s
->max_exponent
[ch
] = max_scale
;
901 /* return 0 if OK. return 1 if last block of frame. return -1 if
902 unrecorrable error. */
903 static int wma_decode_block(WMADecodeContext
*s
)
905 int n
, v
, a
, ch
, code
, bsize
;
906 int coef_nb_bits
, total_gain
;
907 int nb_coefs
[MAX_CHANNELS
];
910 /* compute current block length */
911 if (s
->use_variable_block_len
)
913 n
= av_log2(s
->nb_block_sizes
- 1) + 1;
915 if (s
->reset_block_lengths
)
917 s
->reset_block_lengths
= 0;
918 v
= get_bits(&s
->gb
, n
);
919 if (v
>= s
->nb_block_sizes
)
923 s
->prev_block_len_bits
= s
->frame_len_bits
- v
;
924 v
= get_bits(&s
->gb
, n
);
925 if (v
>= s
->nb_block_sizes
)
929 s
->block_len_bits
= s
->frame_len_bits
- v
;
933 /* update block lengths */
934 s
->prev_block_len_bits
= s
->block_len_bits
;
935 s
->block_len_bits
= s
->next_block_len_bits
;
937 v
= get_bits(&s
->gb
, n
);
939 if (v
>= s
->nb_block_sizes
)
942 s
->next_block_len_bits
= s
->frame_len_bits
- v
;
946 /* fixed block len */
947 s
->next_block_len_bits
= s
->frame_len_bits
;
948 s
->prev_block_len_bits
= s
->frame_len_bits
;
949 s
->block_len_bits
= s
->frame_len_bits
;
951 /* now check if the block length is coherent with the frame length */
952 s
->block_len
= 1 << s
->block_len_bits
;
954 if ((s
->block_pos
+ s
->block_len
) > s
->frame_len
)
956 return -5; /* oddly 32k sample from tracker fails here */
959 if (s
->nb_channels
== 2)
961 s
->ms_stereo
= get_bits(&s
->gb
, 1);
964 for (ch
= 0; ch
< s
->nb_channels
; ++ch
)
966 a
= get_bits(&s
->gb
, 1);
967 s
->channel_coded
[ch
] = a
;
970 /* if no channel coded, no need to go further */
971 /* XXX: fix potential framing problems */
977 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
979 /* read total gain and extract corresponding number of bits for
980 coef escape coding */
984 a
= get_bits(&s
->gb
, 7);
994 else if (total_gain
< 32)
996 else if (total_gain
< 40)
998 else if (total_gain
< 45)
1003 /* compute number of coefficients */
1004 n
= s
->coefs_end
[bsize
] - s
->coefs_start
;
1006 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1010 /* complex coding */
1011 if (s
->use_noise_coding
)
1014 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1016 if (s
->channel_coded
[ch
])
1019 n
= s
->exponent_high_sizes
[bsize
];
1022 a
= get_bits(&s
->gb
, 1);
1023 s
->high_band_coded
[ch
][i
] = a
;
1024 /* if noise coding, the coefficients are not transmitted */
1026 nb_coefs
[ch
] -= s
->exponent_high_bands
[bsize
][i
];
1030 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1032 if (s
->channel_coded
[ch
])
1034 int i
, n
, val
, code
;
1036 n
= s
->exponent_high_sizes
[bsize
];
1037 val
= (int)0x80000000;
1040 if (s
->high_band_coded
[ch
][i
])
1042 if (val
== (int)0x80000000)
1044 val
= get_bits(&s
->gb
, 7) - 19;
1048 //code = get_vlc(&s->gb, &s->hgain_vlc);
1049 code
= get_vlc2(&s
->gb
, s
->hgain_vlc
.table
, HGAINVLCBITS
, HGAINMAX
);
1056 s
->high_band_values
[ch
][i
] = val
;
1063 /* exponents can be reused in short blocks. */
1064 if ((s
->block_len_bits
== s
->frame_len_bits
) || get_bits(&s
->gb
, 1))
1066 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1068 if (s
->channel_coded
[ch
])
1072 if (decode_exp_vlc(s
, ch
) < 0)
1079 decode_exp_lsp(s
, ch
);
1081 s
->exponents_bsize
[ch
] = bsize
;
1086 /* parse spectral coefficients : just RLE encoding */
1087 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1089 if (s
->channel_coded
[ch
])
1092 int level
, run
, sign
, tindex
;
1093 int16_t *ptr
, *eptr
;
1094 const uint16_t *level_table
, *run_table
;
1096 /* special VLC tables are used for ms stereo because
1097 there is potentially less energy there */
1098 tindex
= (ch
== 1 && s
->ms_stereo
);
1099 coef_vlc
= &s
->coef_vlc
[tindex
];
1100 run_table
= s
->run_table
[tindex
];
1101 level_table
= s
->level_table
[tindex
];
1103 ptr
= &s
->coefs1
[ch
][0];
1104 eptr
= ptr
+ nb_coefs
[ch
];
1105 memset(ptr
, 0, s
->block_len
* sizeof(int16_t));
1109 code
= get_vlc2(&s
->gb
, coef_vlc
->table
, VLCBITS
, VLCMAX
);
1110 //code = get_vlc(&s->gb, coef_vlc);
1123 level
= get_bits(&s
->gb
, coef_nb_bits
);
1124 /* NOTE: this is rather suboptimal. reading
1125 block_len_bits would be better */
1126 run
= get_bits(&s
->gb
, s
->frame_len_bits
);
1131 run
= run_table
[code
];
1132 level
= level_table
[code
];
1134 sign
= get_bits(&s
->gb
, 1);
1145 /* NOTE: EOB can be omitted */
1150 if (s
->version
== 1 && s
->nb_channels
>= 2)
1152 align_get_bits(&s
->gb
);
1157 int n4
= s
->block_len
>> 1;
1159 /* theres no reason to do a divide by two in fixed precision ... */
1160 mdct_norm
= 0x10000>>(s
->block_len_bits
-1);
1162 if (s
->version
== 1)
1164 mdct_norm
*= fixtoi32(fixsqrt32(itofix32(n4
))); /* PJJ : exercise this path */
1168 /* finally compute the MDCT coefficients */
1169 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1171 if (s
->channel_coded
[ch
])
1174 int32_t *exponents
, *exp_ptr
;
1175 int32_t *coefs
, atemp
;
1178 int32_t noise
, temp1
, temp2
, mult2
;
1179 int i
, j
, n
, n1
, last_high_band
, esize
;
1180 int32_t exp_power
[HIGH_BAND_MAX_SIZE
];
1182 coefs1
= s
->coefs1
[ch
];
1183 exponents
= s
->exponents
[ch
];
1184 esize
= s
->exponents_bsize
[ch
];
1185 coefs
= (*(s
->coefs
))[ch
];
1190 * Previously the IMDCT was run in 17.15 precision to avoid overflow. However rare files could
1191 * overflow here as well, so switch to 17.15 during coefs calculation.
1195 if (s
->use_noise_coding
)
1197 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1199 mult
= fixdiv64(pow_table
[total_gain
+20],Fixed32To64(s
->max_exponent
[ch
]));
1200 mult
= mult
* mdct_norm
; //what the hell? This is actually int64_t*2^16!
1203 /* very low freqs : noise */
1204 for(i
= 0;i
< s
->coefs_start
; ++i
)
1206 *coefs
++ = fixmul32((fixmul32(s
->noise_table
[s
->noise_index
],
1207 (*exponents
++))>>4),Fixed32From64(mult1
)) >>1;
1208 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1211 n1
= s
->exponent_high_sizes
[bsize
];
1213 /* compute power of high bands */
1214 exp_ptr
= exponents
+
1215 s
->high_band_start
[bsize
] -
1217 last_high_band
= 0; /* avoid warning */
1220 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1221 s
->block_len_bits
][j
];
1222 if (s
->high_band_coded
[ch
][j
])
1226 for(i
= 0;i
< n
; ++i
)
1228 /*v is noramlized later on so its fixed format is irrelevant*/
1230 e2
+= fixmul32(v
, v
)>>3;
1232 exp_power
[j
] = e2
/n
; /*n is an int...*/
1238 /* main freqs and high freqs */
1243 n
= s
->high_band_start
[bsize
] -
1248 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1249 s
->block_len_bits
][j
];
1251 if (j
>= 0 && s
->high_band_coded
[ch
][j
])
1253 /* use noise with specified power */
1254 int32_t tmp
= fixdiv32(exp_power
[j
],exp_power
[last_high_band
]);
1255 mult1
= (int64_t)fixsqrt32(tmp
);
1256 /* XXX: use a table */
1257 /*mult1 is 48.16, pow_table is 48.16*/
1258 mult1
= mult1
* pow_table
[s
->high_band_values
[ch
][j
]+20] >> PRECISION
;
1260 /*this step has a fairly high degree of error for some reason*/
1261 mult1
= fixdiv64(mult1
,fixmul32(s
->max_exponent
[ch
],s
->noise_mult
));
1263 mult1
= mult1
*mdct_norm
>>PRECISION
;
1264 for(i
= 0;i
< n
; ++i
)
1266 noise
= s
->noise_table
[s
->noise_index
];
1267 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1268 *coefs
++ = fixmul32((fixmul32(*exponents
,noise
)>>4),Fixed32From64(mult1
)) >>1;
1274 /* coded values + small noise */
1275 for(i
= 0;i
< n
; ++i
)
1277 // PJJ: check code path
1278 noise
= s
->noise_table
[s
->noise_index
];
1279 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1281 /*don't forget to renormalize the noise*/
1282 temp1
= (((int32_t)*coefs1
++)<<16) + (noise
>>4);
1283 temp2
= fixmul32(*exponents
, mult
>>17);
1284 *coefs
++ = fixmul32(temp1
, temp2
);
1290 /* very high freqs : noise */
1291 n
= s
->block_len
- s
->coefs_end
[bsize
];
1292 mult2
= fixmul32(mult
>>16,exponents
[-1]) ; /*the work around for 32.32 vars are getting stupid*/
1293 for (i
= 0; i
< n
; ++i
)
1295 /*renormalize the noise product and then reduce to 17.15 precison*/
1296 *coefs
++ = fixmul32(s
->noise_table
[s
->noise_index
],mult2
) >>5;
1298 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1303 /*Noise coding not used, simply convert from exp to fixed representation*/
1304 int32_t mult3
= (int32_t)(fixdiv64(pow_table
[total_gain
+20],Fixed32To64(s
->max_exponent
[ch
])));
1305 mult3
= fixmul32(mult3
, mdct_norm
);
1309 /* XXX: optimize more, unrolling this loop in asm might be a good idea */
1310 for(i
= 0;i
< s
->coefs_start
; i
++)
1312 for(i
= 0;i
< n
; ++i
)
1314 atemp
= (coefs1
[i
] * mult3
)>>1;
1315 *coefs
++=fixmul32(atemp
,exponents
[i
<<bsize
>>esize
]);
1317 n
= s
->block_len
- s
->coefs_end
[bsize
];
1318 memset(coefs
, 0, n
*sizeof(int32_t));
1323 if (s
->ms_stereo
&& s
->channel_coded
[1])
1327 int32_t (*coefs
)[MAX_CHANNELS
][BLOCK_MAX_SIZE
] = (s
->coefs
);
1329 /* nominal case for ms stereo: we do it before mdct */
1330 /* no need to optimize this case because it should almost
1332 if (!s
->channel_coded
[0])
1334 memset((*(s
->coefs
))[0], 0, sizeof(int32_t) * s
->block_len
);
1335 s
->channel_coded
[0] = 1;
1338 for(i
= 0; i
< s
->block_len
; ++i
)
1342 (*coefs
)[0][i
] = a
+ b
;
1343 (*coefs
)[1][i
] = a
- b
;
1347 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1349 if (s
->channel_coded
[ch
])
1351 static int32_t output
[BLOCK_MAX_SIZE
* 2] IBSS_ATTR
;
1356 n4
= s
->block_len
>>1;
1358 ff_imdct_calc(&s
->mdct_ctx
[bsize
],
1362 /* add in the frame */
1363 index
= (s
->frame_len
/ 2) + s
->block_pos
- n4
;
1365 wma_window(s
, output
, &s
->frame_out
[ch
][index
]);
1367 /* specific fast case for ms-stereo : add to second
1368 channel if it is not coded */
1369 if (s
->ms_stereo
&& !s
->channel_coded
[1])
1371 wma_window(s
, output
, &s
->frame_out
[1][index
]);
1377 /* update block number */
1379 s
->block_pos
+= s
->block_len
;
1380 if (s
->block_pos
>= s
->frame_len
)
1390 /* decode a frame of frame_len samples */
1391 static int wma_decode_frame(WMADecodeContext
*s
, int32_t *samples
)
1393 int ret
, i
, n
, ch
, incr
;
1397 /* read each block */
1403 ret
= wma_decode_block(s
);
1407 printf("wma_decode_block failed with code %d\n", ret
);
1417 /* return frame with full 30-bit precision */
1419 incr
= s
->nb_channels
;
1420 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1423 iptr
= s
->frame_out
[ch
];
1430 /* prepare for next block */
1431 memmove(&s
->frame_out
[ch
][0], &s
->frame_out
[ch
][s
->frame_len
],
1432 s
->frame_len
* sizeof(int32_t));
1439 /* Initialise the superframe decoding */
1441 int wma_decode_superframe_init(WMADecodeContext
* s
,
1442 uint8_t *buf
, /*input*/
1447 s
->last_superframe_len
= 0;
1451 s
->current_frame
= 0;
1453 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1455 if (s
->use_bit_reservoir
)
1457 /* read super frame header */
1458 get_bits(&s
->gb
, 4); /* super frame index */
1459 s
->nb_frames
= get_bits(&s
->gb
, 4);
1461 if (s
->last_superframe_len
== 0)
1463 else if (s
->nb_frames
== 0)
1466 s
->bit_offset
= get_bits(&s
->gb
, s
->byte_offset_bits
+ 3);
1475 /* Decode a single frame in the current superframe - return -1 if
1476 there was a decoding error, or the number of samples decoded.
1479 int wma_decode_superframe_frame(WMADecodeContext
* s
,
1480 int32_t* samples
, /*output*/
1481 uint8_t *buf
, /*input*/
1487 if ((s
->use_bit_reservoir
) && (s
->current_frame
== 0))
1489 if (s
->last_superframe_len
> 0)
1491 /* add s->bit_offset bits to last frame */
1492 if ((s
->last_superframe_len
+ ((s
->bit_offset
+ 7) >> 3)) >
1493 MAX_CODED_SUPERFRAME_SIZE
)
1496 printf("superframe size too large error\n");
1500 q
= s
->last_superframe
+ s
->last_superframe_len
;
1501 len
= s
->bit_offset
;
1504 *q
++ = (get_bits
)(&s
->gb
, 8);
1509 *q
++ = (get_bits
)(&s
->gb
, len
) << (8 - len
);
1512 /* XXX: s->bit_offset bits into last frame */
1513 init_get_bits(&s
->gb
, s
->last_superframe
, MAX_CODED_SUPERFRAME_SIZE
*8);
1514 /* skip unused bits */
1515 if (s
->last_bitoffset
> 0)
1516 skip_bits(&s
->gb
, s
->last_bitoffset
);
1518 /* this frame is stored in the last superframe and in the
1520 if (wma_decode_frame(s
, samples
) < 0)
1527 /* read each frame starting from s->bit_offset */
1528 pos
= s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3;
1529 init_get_bits(&s
->gb
, buf
+ (pos
>> 3), (MAX_CODED_SUPERFRAME_SIZE
- (pos
>> 3))*8);
1532 skip_bits(&s
->gb
, len
);
1534 s
->reset_block_lengths
= 1;
1537 /* If we haven't decoded a frame yet, do it now */
1540 if (wma_decode_frame(s
, samples
) < 0)
1548 if ((s
->use_bit_reservoir
) && (s
->current_frame
== s
->nb_frames
))
1550 /* we copy the end of the frame in the last frame buffer */
1551 pos
= get_bits_count(&s
->gb
) + ((s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3) & ~7);
1552 s
->last_bitoffset
= pos
& 7;
1554 len
= buf_size
- pos
;
1555 if (len
> MAX_CODED_SUPERFRAME_SIZE
|| len
< 0)
1558 printf("superframe size too large error after decodeing\n");
1562 s
->last_superframe_len
= len
;
1563 memcpy(s
->last_superframe
, buf
+ pos
, len
);
1566 return s
->frame_len
;
1569 /* when error, we reset the bit reservoir */
1571 s
->last_superframe_len
= 0;