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>
27 #include <codecs/libasf/asf.h>
32 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
);
33 inline void vector_fmul_add_add(fixed32
*dst
, const fixed32
*data
,
34 const fixed32
*window
, int n
);
35 inline void vector_fmul_reverse(fixed32
*dst
, const fixed32
*src0
,
36 const fixed32
*src1
, int len
);
38 /*declarations of statically allocated variables used to remove malloc calls*/
40 fixed32 coefsarray
[MAX_CHANNELS
][BLOCK_MAX_SIZE
] IBSS_ATTR
;
41 /*decode and window into IRAM on targets with at least 80KB of codec IRAM*/
42 fixed32 frame_out_buf
[MAX_CHANNELS
][BLOCK_MAX_SIZE
* 2] IBSS_ATTR_WMA_LARGE_IRAM
;
44 /*MDCT reconstruction windows*/
45 fixed32 stat0
[2048], stat1
[1024], stat2
[512], stat3
[256], stat4
[128];
48 uint16_t *runtabarray
[2], *levtabarray
[2];
50 /*these could be made smaller since only one can be 1336*/
51 uint16_t runtab0
[1336], runtab1
[1336], levtab0
[1336], levtab1
[1336];
53 #define VLCBUF1SIZE 4598
54 #define VLCBUF2SIZE 3574
55 #define VLCBUF3SIZE 360
56 #define VLCBUF4SIZE 540
58 /*putting these in IRAM actually makes PP slower*/
60 VLC_TYPE vlcbuf1
[VLCBUF1SIZE
][2];
61 VLC_TYPE vlcbuf2
[VLCBUF2SIZE
][2];
62 VLC_TYPE vlcbuf3
[VLCBUF3SIZE
][2];
63 VLC_TYPE vlcbuf4
[VLCBUF4SIZE
][2];
69 * Apply MDCT window and add into output.
71 * We ensure that when the windows overlap their squared sum
72 * is always 1 (MDCT reconstruction rule).
74 * The Vorbis I spec has a great diagram explaining this process.
75 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
77 static void wma_window(WMADecodeContext
*s
, fixed32
*in
, fixed32
*out
)
79 //float *in = s->output;
80 int block_len
, bsize
, n
;
84 /* previous block was larger, so we'll use the size of the current
85 * block to set the window size*/
86 if (s
->block_len_bits
<= s
->prev_block_len_bits
) {
87 block_len
= s
->block_len
;
88 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
90 vector_fmul_add_add(out
, in
, s
->windows
[bsize
], block_len
);
93 /*previous block was smaller or the same size, so use it's size to set the window length*/
94 block_len
= 1 << s
->prev_block_len_bits
;
95 /*find the middle of the two overlapped blocks, this will be the first overlapped sample*/
96 n
= (s
->block_len
- block_len
) / 2;
97 bsize
= s
->frame_len_bits
- s
->prev_block_len_bits
;
99 vector_fmul_add_add(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
101 memcpy(out
+n
+block_len
, in
+n
+block_len
, n
*sizeof(fixed32
));
103 /* Advance to the end of the current block and prepare to window it for the next block.
104 * Since the window function needs to be reversed, we do it backwards starting with the
105 * last sample and moving towards the first
111 if (s
->block_len_bits
<= s
->next_block_len_bits
) {
112 block_len
= s
->block_len
;
113 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
115 vector_fmul_reverse(out
, in
, s
->windows
[bsize
], block_len
);
118 block_len
= 1 << s
->next_block_len_bits
;
119 n
= (s
->block_len
- block_len
) / 2;
120 bsize
= s
->frame_len_bits
- s
->next_block_len_bits
;
122 memcpy(out
, in
, n
*sizeof(fixed32
));
124 vector_fmul_reverse(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
126 memset(out
+n
+block_len
, 0, n
*sizeof(fixed32
));
133 /* XXX: use same run/length optimization as mpeg decoders */
134 static void init_coef_vlc(VLC
*vlc
,
135 uint16_t **prun_table
, uint16_t **plevel_table
,
136 const CoefVLCTable
*vlc_table
, int tab
)
138 int n
= vlc_table
->n
;
139 const uint8_t *table_bits
= vlc_table
->huffbits
;
140 const uint32_t *table_codes
= vlc_table
->huffcodes
;
141 const uint16_t *levels_table
= vlc_table
->levels
;
142 uint16_t *run_table
, *level_table
;
147 init_vlc(vlc
, VLCBITS
, n
, table_bits
, 1, 1, table_codes
, 4, 4, INIT_VLC_USE_NEW_STATIC
);
149 run_table
= runtabarray
[tab
];
150 level_table
= levtabarray
[tab
];
161 level_table
[i
] = level
;
166 *prun_table
= run_table
;
167 *plevel_table
= level_table
;
170 int wma_decode_init(WMADecodeContext
* s
, asf_waveformatex_t
*wfx
)
173 int i
, flags1
, flags2
;
183 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
186 /*clear stereo setting to avoid glitches when switching stereo->mono*/
187 s
->channel_coded
[0]=0;
188 s
->channel_coded
[1]=0;
191 s
->sample_rate
= wfx
->rate
;
192 s
->nb_channels
= wfx
->channels
;
193 s
->bit_rate
= wfx
->bitrate
;
194 s
->block_align
= wfx
->blockalign
;
196 s
->coefs
= &coefsarray
;
197 s
->frame_out
= &frame_out_buf
;
199 if (wfx
->codec_id
== ASF_CODEC_ID_WMAV1
) {
201 } else if (wfx
->codec_id
== ASF_CODEC_ID_WMAV2
) {
204 /*one of those other wma flavors that don't have GPLed decoders */
208 /* extract flag infos */
211 extradata
= wfx
->data
;
212 if (s
->version
== 1 && wfx
->datalen
>= 4) {
213 flags1
= extradata
[0] | (extradata
[1] << 8);
214 flags2
= extradata
[2] | (extradata
[3] << 8);
215 }else if (s
->version
== 2 && wfx
->datalen
>= 6){
216 flags1
= extradata
[0] | (extradata
[1] << 8) |
217 (extradata
[2] << 16) | (extradata
[3] << 24);
218 flags2
= extradata
[4] | (extradata
[5] << 8);
220 s
->use_exp_vlc
= flags2
& 0x0001;
221 s
->use_bit_reservoir
= flags2
& 0x0002;
222 s
->use_variable_block_len
= flags2
& 0x0004;
224 /* compute MDCT block size */
225 if (s
->sample_rate
<= 16000){
226 s
->frame_len_bits
= 9;
227 }else if (s
->sample_rate
<= 22050 ||
228 (s
->sample_rate
<= 32000 && s
->version
== 1)){
229 s
->frame_len_bits
= 10;
231 s
->frame_len_bits
= 11;
233 s
->frame_len
= 1 << s
->frame_len_bits
;
234 if (s
-> use_variable_block_len
)
237 nb
= ((flags2
>> 3) & 3) + 1;
238 if ((s
->bit_rate
/ s
->nb_channels
) >= 32000)
242 nb_max
= s
->frame_len_bits
- BLOCK_MIN_BITS
; //max is 11-7
245 s
->nb_block_sizes
= nb
+ 1;
249 s
->nb_block_sizes
= 1;
252 /* init rate dependant parameters */
253 s
->use_noise_coding
= 1;
254 high_freq
= itofix64(s
->sample_rate
) >> 1;
257 /* if version 2, then the rates are normalized */
258 sample_rate1
= s
->sample_rate
;
261 if (sample_rate1
>= 44100)
262 sample_rate1
= 44100;
263 else if (sample_rate1
>= 22050)
264 sample_rate1
= 22050;
265 else if (sample_rate1
>= 16000)
266 sample_rate1
= 16000;
267 else if (sample_rate1
>= 11025)
268 sample_rate1
= 11025;
269 else if (sample_rate1
>= 8000)
273 fixed64 tmp
= itofix64(s
->bit_rate
);
274 fixed64 tmp2
= itofix64(s
->nb_channels
* s
->sample_rate
);
275 bps
= fixdiv64(tmp
, tmp2
);
276 fixed64 tim
= bps
* s
->frame_len
;
277 fixed64 tmpi
= fixdiv64(tim
,itofix64(8));
278 s
->byte_offset_bits
= av_log2(fixtoi64(tmpi
+0x8000)) + 2;
280 /* compute high frequency value and choose if noise coding should
283 if (s
->nb_channels
== 2)
284 bps1
= fixmul32(bps
,0x1999a);
285 if (sample_rate1
== 44100)
288 s
->use_noise_coding
= 0;
290 high_freq
= fixmul32(high_freq
,0x6666);
292 else if (sample_rate1
== 22050)
295 s
->use_noise_coding
= 0;
296 else if (bps1
>= 0xb852)
297 high_freq
= fixmul32(high_freq
,0xb333);
299 high_freq
= fixmul32(high_freq
,0x999a);
301 else if (sample_rate1
== 16000)
304 high_freq
= fixmul32(high_freq
,0x8000);
306 high_freq
= fixmul32(high_freq
,0x4ccd);
308 else if (sample_rate1
== 11025)
310 high_freq
= fixmul32(high_freq
,0xb333);
312 else if (sample_rate1
== 8000)
316 high_freq
= fixmul32(high_freq
,0x8000);
318 else if (bps
> 0xc000)
320 s
->use_noise_coding
= 0;
324 high_freq
= fixmul32(high_freq
,0xa666);
331 high_freq
= fixmul32(high_freq
,0xc000);
333 else if (bps
>= 0x999a)
335 high_freq
= fixmul32(high_freq
,0x999a);
339 high_freq
= fixmul32(high_freq
,0x8000);
343 /* compute the scale factor band sizes for each MDCT block size */
345 int a
, b
, pos
, lpos
, k
, block_len
, i
, j
, n
;
346 const uint8_t *table
;
356 for(k
= 0; k
< s
->nb_block_sizes
; ++k
)
358 block_len
= s
->frame_len
>> k
;
365 a
= wma_critical_freqs
[i
];
367 pos
= ((block_len
* 2 * a
) + (b
>> 1)) / b
;
370 s
->exponent_bands
[0][i
] = pos
- lpos
;
371 if (pos
>= block_len
)
378 s
->exponent_sizes
[0] = i
;
382 /* hardcoded tables */
384 a
= s
->frame_len_bits
- BLOCK_MIN_BITS
- k
;
387 if (s
->sample_rate
>= 44100)
388 table
= exponent_band_44100
[a
];
389 else if (s
->sample_rate
>= 32000)
390 table
= exponent_band_32000
[a
];
391 else if (s
->sample_rate
>= 22050)
392 table
= exponent_band_22050
[a
];
398 s
->exponent_bands
[k
][i
] = table
[i
];
399 s
->exponent_sizes
[k
] = n
;
407 a
= wma_critical_freqs
[i
];
409 pos
= ((block_len
* 2 * a
) + (b
<< 1)) / (4 * b
);
414 s
->exponent_bands
[k
][j
++] = pos
- lpos
;
415 if (pos
>= block_len
)
419 s
->exponent_sizes
[k
] = j
;
423 /* max number of coefs */
424 s
->coefs_end
[k
] = (s
->frame_len
- ((s
->frame_len
* 9) / 100)) >> k
;
425 /* high freq computation */
427 fixed32 tmp1
= high_freq
*2; /* high_freq is a fixed32!*/
428 fixed32 tmp2
=itofix32(s
->sample_rate
>>1);
429 s
->high_band_start
[k
] = fixtoi32( fixdiv32(tmp1
, tmp2
) * (block_len
>>1) +0x8000);
432 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
433 s->sample_rate + 0.5);*/
435 n
= s
->exponent_sizes
[k
];
442 pos
+= s
->exponent_bands
[k
][i
];
444 if (start
< s
->high_band_start
[k
])
445 start
= s
->high_band_start
[k
];
446 if (end
> s
->coefs_end
[k
])
447 end
= s
->coefs_end
[k
];
449 s
->exponent_high_bands
[k
][j
++] = end
- start
;
451 s
->exponent_high_sizes
[k
] = j
;
455 /* ffmpeg uses malloc to only allocate as many window sizes as needed.
456 * However, we're really only interested in the worst case memory usage.
457 * In the worst case you can have 5 window sizes, 128 doubling up 2048
458 * Smaller windows are handled differently.
459 * Since we don't have malloc, just statically allocate this
468 /* init MDCT windows : simple sinus window */
469 for(i
= 0; i
< s
->nb_block_sizes
; i
++)
473 n
= 1 << (s
->frame_len_bits
- i
);
476 /* this calculates 0.5/(2*n) */
477 alpha
= (1<<15)>>(s
->frame_len_bits
- i
+1);
480 fixed32 j2
= itofix32(j
) + 0x8000;
481 /*alpha between 0 and pi/2*/
482 window
[j
] = fsincos(fixmul32(j2
,alpha
)<<16, 0);
484 s
->windows
[i
] = window
;
488 s
->reset_block_lengths
= 1;
490 if (s
->use_noise_coding
)
492 /* init the noise generator */
495 s
->noise_mult
= 0x51f;
496 s
->noise_table
= noisetable_exp
;
500 s
->noise_mult
= 0xa3d;
501 /* LSP values are simply 2x the EXP values */
502 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
503 noisetable_exp
[i
] = noisetable_exp
[i
]<< 1;
504 s
->noise_table
= noisetable_exp
;
507 /* We use a lookup table computered in advance, so no need to do this*/
512 norm
= 0; // PJJ: near as makes any diff to 0!
513 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
515 seed
= seed
* 314159 + 1;
516 s
->noise_table
[i
] = itofix32((int)seed
) * norm
;
521 s
->hgain_vlc
.table
= vlcbuf4
;
522 s
->hgain_vlc
.table_allocated
= VLCBUF4SIZE
;
523 init_vlc(&s
->hgain_vlc
, HGAINVLCBITS
, sizeof(hgain_huffbits
),
524 hgain_huffbits
, 1, 1,
525 hgain_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
);
531 s
->exp_vlc
.table
= vlcbuf3
;
532 s
->exp_vlc
.table_allocated
= VLCBUF3SIZE
;
534 init_vlc(&s
->exp_vlc
, EXPVLCBITS
, sizeof(scale_huffbits
),
535 scale_huffbits
, 1, 1,
536 scale_huffcodes
, 4, 4, INIT_VLC_USE_NEW_STATIC
);
540 wma_lsp_to_curve_init(s
, s
->frame_len
);
543 /* choose the VLC tables for the coefficients */
545 if (s
->sample_rate
>= 32000)
549 else if (bps1
< 0x128f6)
553 runtabarray
[0] = runtab0
; runtabarray
[1] = runtab1
;
554 levtabarray
[0] = levtab0
; levtabarray
[1] = levtab1
;
556 s
->coef_vlc
[0].table
= vlcbuf1
;
557 s
->coef_vlc
[0].table_allocated
= VLCBUF1SIZE
;
558 s
->coef_vlc
[1].table
= vlcbuf2
;
559 s
->coef_vlc
[1].table_allocated
= VLCBUF2SIZE
;
562 init_coef_vlc(&s
->coef_vlc
[0], &s
->run_table
[0], &s
->level_table
[0],
563 &coef_vlcs
[coef_vlc_table
* 2], 0);
564 init_coef_vlc(&s
->coef_vlc
[1], &s
->run_table
[1], &s
->level_table
[1],
565 &coef_vlcs
[coef_vlc_table
* 2 + 1], 1);
567 s
->last_superframe_len
= 0;
568 s
->last_bitoffset
= 0;
574 /* compute x^-0.25 with an exponent and mantissa table. We use linear
575 interpolation to reduce the mantissa table size at a small speed
576 expense (linear interpolation approximately doubles the number of
577 bits of precision). */
578 static inline fixed32
pow_m1_4(WMADecodeContext
*s
, fixed32 x
)
589 m
= (u
.v
>> (23 - LSP_POW_BITS
)) & ((1 << LSP_POW_BITS
) - 1);
590 /* build interpolation scale: 1 <= t < 2. */
591 t
.v
= ((u
.v
<< LSP_POW_BITS
) & ((1 << 23) - 1)) | (127 << 23);
592 a
= s
->lsp_pow_m_table1
[m
];
593 b
= s
->lsp_pow_m_table2
[m
];
595 /* lsp_pow_e_table contains 32.32 format */
596 /* TODO: Since we're unlikely have value that cover the whole
597 * IEEE754 range, we probably don't need to have all possible exponents */
599 return (lsp_pow_e_table
[e
] * (a
+ fixmul32(b
, ftofix32(t
.f
))) >>32);
602 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
)
604 fixed32 wdel
, a
, b
, temp
, temp2
;
607 wdel
= fixdiv32(M_PI_F
, itofix32(frame_len
));
608 temp
= fixdiv32(itofix32(1), itofix32(frame_len
));
609 for (i
=0; i
<frame_len
; ++i
)
611 /* TODO: can probably reuse the trig_init values here */
612 fsincos((temp
*i
)<<15, &temp2
);
613 /* get 3 bits headroom + 1 bit from not doubleing the values */
614 s
->lsp_cos_table
[i
] = temp2
>>3;
617 /* NOTE: these two tables are needed to avoid two operations in
622 /*double check this later*/
623 for(i
=(1 << LSP_POW_BITS
) - 1;i
>=0;i
--)
625 m
= (1 << LSP_POW_BITS
) + i
;
626 a
= pow_a_table
[ix
++]<<4;
627 s
->lsp_pow_m_table1
[i
] = 2 * a
- b
;
628 s
->lsp_pow_m_table2
[i
] = b
- a
;
634 /* NOTE: We use the same code as Vorbis here */
635 /* XXX: optimize it further with SSE/3Dnow */
636 static void wma_lsp_to_curve(WMADecodeContext
*s
,
638 fixed32
*val_max_ptr
,
643 fixed32 p
, q
, w
, v
, val_max
, temp
, temp2
;
648 /* shift by 2 now to reduce rounding error,
649 * we can renormalize right before pow_m1_4
654 w
= s
->lsp_cos_table
[i
];
656 for (j
=1;j
<NB_LSP_COEFS
;j
+=2)
658 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
659 temp2
= ((w
- (lsp
[j
- 1]<<11)));
662 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
663 q
= fixmul32b(q
, temp2
)<<4;
664 p
= fixmul32b(p
, (w
- (lsp
[j
]<<11)))<<4;
667 /* 2 in 5.27 format is 0x10000000 */
668 p
= fixmul32(p
, fixmul32b(p
, (0x10000000 - w
)))<<3;
669 q
= fixmul32(q
, fixmul32b(q
, (0x10000000 + w
)))<<3;
671 v
= (p
+ q
) >>9; /* p/q end up as 16.16 */
678 *val_max_ptr
= val_max
;
681 /* decode exponents coded with LSP coefficients (same idea as Vorbis)
682 * only used for low bitrate (< 16kbps) files
684 static void decode_exp_lsp(WMADecodeContext
*s
, int ch
)
686 fixed32 lsp_coefs
[NB_LSP_COEFS
];
689 for (i
= 0; i
< NB_LSP_COEFS
; ++i
)
691 if (i
== 0 || i
>= 8)
692 val
= get_bits(&s
->gb
, 3);
694 val
= get_bits(&s
->gb
, 4);
695 lsp_coefs
[i
] = lsp_codebook
[i
][val
];
700 &s
->max_exponent
[ch
],
705 /* decode exponents coded with VLC codes - used for bitrate >= 32kbps*/
706 static int decode_exp_vlc(WMADecodeContext
*s
, int ch
)
708 int last_exp
, n
, code
;
709 const uint16_t *ptr
, *band_ptr
;
710 fixed32 v
, max_scale
;
713 /*accommodate the 60 negative indices */
714 const fixed32
*pow_10_to_yover16_ptr
= &pow_10_to_yover16
[61];
716 band_ptr
= s
->exponent_bands
[s
->frame_len_bits
- s
->block_len_bits
];
718 q
= s
->exponents
[ch
];
719 q_end
= q
+ s
->block_len
;
723 if (s
->version
== 1) //wmav1 only
725 last_exp
= get_bits(&s
->gb
, 5) + 10;
727 v
= pow_10_to_yover16_ptr
[last_exp
];
735 } while ((n
-= 4) > 0);
742 code
= get_vlc2(&s
->gb
, s
->exp_vlc
.table
, EXPVLCBITS
, EXPMAX
);
747 /* NOTE: this offset is the same as MPEG4 AAC ! */
748 last_exp
+= code
- 60;
750 v
= pow_10_to_yover16_ptr
[last_exp
];
761 } while ((n
-= 4) > 0);
764 s
->max_exponent
[ch
] = max_scale
;
768 /* return 0 if OK. return 1 if last block of frame. return -1 if
769 unrecorrable error. */
770 static int wma_decode_block(WMADecodeContext
*s
, int32_t *scratch_buffer
)
772 int n
, v
, a
, ch
, code
, bsize
;
773 int coef_nb_bits
, total_gain
;
774 int nb_coefs
[MAX_CHANNELS
];
777 /*DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s->block_num, s->block_len, s->frame_len);*/
779 /* compute current block length */
780 if (s
->use_variable_block_len
)
782 n
= av_log2(s
->nb_block_sizes
- 1) + 1;
784 if (s
->reset_block_lengths
)
786 s
->reset_block_lengths
= 0;
787 v
= get_bits(&s
->gb
, n
);
788 if (v
>= s
->nb_block_sizes
)
792 s
->prev_block_len_bits
= s
->frame_len_bits
- v
;
793 v
= get_bits(&s
->gb
, n
);
794 if (v
>= s
->nb_block_sizes
)
798 s
->block_len_bits
= s
->frame_len_bits
- v
;
802 /* update block lengths */
803 s
->prev_block_len_bits
= s
->block_len_bits
;
804 s
->block_len_bits
= s
->next_block_len_bits
;
806 v
= get_bits(&s
->gb
, n
);
808 if (v
>= s
->nb_block_sizes
)
810 // rb->splash(HZ*4, "v was %d", v); //5, 7
811 return -4; //this is it
814 //rb->splash(HZ, "passed v block (%d)!", v);
816 s
->next_block_len_bits
= s
->frame_len_bits
- v
;
820 /* fixed block len */
821 s
->next_block_len_bits
= s
->frame_len_bits
;
822 s
->prev_block_len_bits
= s
->frame_len_bits
;
823 s
->block_len_bits
= s
->frame_len_bits
;
825 /* now check if the block length is coherent with the frame length */
826 s
->block_len
= 1 << s
->block_len_bits
;
828 if ((s
->block_pos
+ s
->block_len
) > s
->frame_len
)
830 return -5; //oddly 32k sample from tracker fails here
833 if (s
->nb_channels
== 2)
835 s
->ms_stereo
= get_bits1(&s
->gb
);
838 for (ch
= 0; ch
< s
->nb_channels
; ++ch
)
840 a
= get_bits1(&s
->gb
);
841 s
->channel_coded
[ch
] = a
;
844 /* if no channel coded, no need to go further */
845 /* XXX: fix potential framing problems */
851 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
853 /* read total gain and extract corresponding number of bits for
854 coef escape coding */
858 a
= get_bits(&s
->gb
, 7);
868 else if (total_gain
< 32)
870 else if (total_gain
< 40)
872 else if (total_gain
< 45)
877 /* compute number of coefficients */
878 n
= s
->coefs_end
[bsize
] - s
->coefs_start
;
880 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
885 if (s
->use_noise_coding
)
888 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
890 if (s
->channel_coded
[ch
])
893 n
= s
->exponent_high_sizes
[bsize
];
896 a
= get_bits1(&s
->gb
);
897 s
->high_band_coded
[ch
][i
] = a
;
898 /* if noise coding, the coefficients are not transmitted */
900 nb_coefs
[ch
] -= s
->exponent_high_bands
[bsize
][i
];
904 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
906 if (s
->channel_coded
[ch
])
910 n
= s
->exponent_high_sizes
[bsize
];
911 val
= (int)0x80000000;
914 if (s
->high_band_coded
[ch
][i
])
916 if (val
== (int)0x80000000)
918 val
= get_bits(&s
->gb
, 7) - 19;
922 //code = get_vlc(&s->gb, &s->hgain_vlc);
923 code
= get_vlc2(&s
->gb
, s
->hgain_vlc
.table
, HGAINVLCBITS
, HGAINMAX
);
930 s
->high_band_values
[ch
][i
] = val
;
937 /* exponents can be reused in short blocks. */
938 if ((s
->block_len_bits
== s
->frame_len_bits
) || get_bits1(&s
->gb
))
940 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
942 if (s
->channel_coded
[ch
])
946 if (decode_exp_vlc(s
, ch
) < 0)
953 decode_exp_lsp(s
, ch
);
955 s
->exponents_bsize
[ch
] = bsize
;
960 /* parse spectral coefficients : just RLE encoding */
961 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
963 if (s
->channel_coded
[ch
])
966 int level
, run
, sign
, tindex
;
968 const int16_t *level_table
, *run_table
;
970 /* special VLC tables are used for ms stereo because
971 there is potentially less energy there */
972 tindex
= (ch
== 1 && s
->ms_stereo
);
973 coef_vlc
= &s
->coef_vlc
[tindex
];
974 run_table
= s
->run_table
[tindex
];
975 level_table
= s
->level_table
[tindex
];
977 ptr
= &s
->coefs1
[ch
][0];
978 eptr
= ptr
+ nb_coefs
[ch
];
979 memset(ptr
, 0, s
->block_len
* sizeof(int16_t));
983 code
= get_vlc2(&s
->gb
, coef_vlc
->table
, VLCBITS
, VLCMAX
);
997 level
= get_bits(&s
->gb
, coef_nb_bits
);
998 /* NOTE: this is rather suboptimal. reading
999 block_len_bits would be better */
1000 run
= get_bits(&s
->gb
, s
->frame_len_bits
);
1005 run
= run_table
[code
];
1006 level
= level_table
[code
];
1008 sign
= get_bits1(&s
->gb
);
1019 /* NOTE: EOB can be omitted */
1024 if (s
->version
== 1 && s
->nb_channels
>= 2)
1026 align_get_bits(&s
->gb
);
1031 int n4
= s
->block_len
>> 1;
1034 mdct_norm
= 0x10000>>(s
->block_len_bits
-1);
1036 if (s
->version
== 1)
1038 mdct_norm
*= fixtoi32(fixsqrt32(itofix32(n4
)));
1043 /* finally compute the MDCT coefficients */
1044 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1046 if (s
->channel_coded
[ch
])
1050 fixed32
*coefs
, atemp
;
1053 fixed32 noise
, temp1
, temp2
, mult2
;
1054 int i
, j
, n
, n1
, last_high_band
, esize
;
1055 fixed32 exp_power
[HIGH_BAND_MAX_SIZE
];
1057 //total_gain, coefs1, mdctnorm are lossless
1059 coefs1
= s
->coefs1
[ch
];
1060 exponents
= s
->exponents
[ch
];
1061 esize
= s
->exponents_bsize
[ch
];
1062 coefs
= (*(s
->coefs
))[ch
];
1066 * The calculation of coefs has a shift right by 2 built in. This
1067 * prepares samples for the Tremor IMDCT which uses a slightly
1068 * different fixed format then the ffmpeg one. If the old ffmpeg
1069 * imdct is used, each shift storing into coefs should be reduced
1071 * See SVN logs for details.
1075 if (s
->use_noise_coding
)
1077 /*This case is only used for low bitrates (typically less then 32kbps)*/
1079 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1081 mult
= fixdiv64(pow_table
[total_gain
+20],Fixed32To64(s
->max_exponent
[ch
]));
1082 mult
= mult
* mdct_norm
;
1085 /* very low freqs : noise */
1086 for(i
= 0;i
< s
->coefs_start
; ++i
)
1088 *coefs
++ = fixmul32( (fixmul32(s
->noise_table
[s
->noise_index
],
1089 exponents
[i
<<bsize
>>esize
])>>4),Fixed32From64(mult1
)) >>2;
1090 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1093 n1
= s
->exponent_high_sizes
[bsize
];
1095 /* compute power of high bands */
1096 exponents
= s
->exponents
[ch
] +(s
->high_band_start
[bsize
]<<bsize
);
1097 last_high_band
= 0; /* avoid warning */
1100 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1101 s
->block_len_bits
][j
];
1102 if (s
->high_band_coded
[ch
][j
])
1106 for(i
= 0;i
< n
; ++i
)
1108 /*v is normalized later on so its fixed format is irrelevant*/
1109 v
= exponents
[i
<<bsize
>>esize
]>>4;
1110 e2
+= fixmul32(v
, v
)>>3;
1112 exp_power
[j
] = e2
/n
; /*n is an int...*/
1115 exponents
+= n
<<bsize
;
1118 /* main freqs and high freqs */
1119 exponents
= s
->exponents
[ch
] + (s
->coefs_start
<<bsize
);
1124 n
= s
->high_band_start
[bsize
] -
1129 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1130 s
->block_len_bits
][j
];
1132 if (j
>= 0 && s
->high_band_coded
[ch
][j
])
1134 /* use noise with specified power */
1135 fixed32 tmp
= fixdiv32(exp_power
[j
],exp_power
[last_high_band
]);
1137 /*mult1 is 48.16, pow_table is 48.16*/
1138 mult1
= fixmul32(fixsqrt32(tmp
),
1139 pow_table
[s
->high_band_values
[ch
][j
]+20]) >> 16;
1141 /*this step has a fairly high degree of error for some reason*/
1142 mult1
= fixdiv64(mult1
,fixmul32(s
->max_exponent
[ch
],s
->noise_mult
));
1143 mult1
= mult1
*mdct_norm
>>PRECISION
;
1144 for(i
= 0;i
< n
; ++i
)
1146 noise
= s
->noise_table
[s
->noise_index
];
1147 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1148 *coefs
++ = fixmul32((fixmul32(exponents
[i
<<bsize
>>esize
],noise
)>>4),
1149 Fixed32From64(mult1
)) >>2;
1152 exponents
+= n
<<bsize
;
1156 /* coded values + small noise */
1157 for(i
= 0;i
< n
; ++i
)
1159 noise
= s
->noise_table
[s
->noise_index
];
1160 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1162 /*don't forget to renormalize the noise*/
1163 temp1
= (((int32_t)*coefs1
++)<<16) + (noise
>>4);
1164 temp2
= fixmul32(exponents
[i
<<bsize
>>esize
], mult
>>18);
1165 *coefs
++ = fixmul32(temp1
, temp2
);
1167 exponents
+= n
<<bsize
;
1171 /* very high freqs : noise */
1172 n
= s
->block_len
- s
->coefs_end
[bsize
];
1173 mult2
= fixmul32(mult
>>16,exponents
[((-1<<bsize
))>>esize
]) ;
1174 for (i
= 0; i
< n
; ++i
)
1176 /*renormalize the noise product and then reduce to 14.18 precison*/
1177 *coefs
++ = fixmul32(s
->noise_table
[s
->noise_index
],mult2
) >>6;
1179 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1184 /*Noise coding not used, simply convert from exp to fixed representation*/
1186 fixed32 mult3
= (fixed32
)(fixdiv64(pow_table
[total_gain
+20],
1187 Fixed32To64(s
->max_exponent
[ch
])));
1188 mult3
= fixmul32(mult3
, mdct_norm
);
1190 /*zero the first 3 coefficients for WMA V1, does nothing otherwise*/
1191 for(i
=0; i
<s
->coefs_start
; i
++)
1196 /* XXX: optimize more, unrolling this loop in asm
1197 might be a good idea */
1199 for(i
= 0;i
< n
; ++i
)
1201 /*ffmpeg imdct needs 15.17, while tremor 14.18*/
1202 atemp
= (coefs1
[i
] * mult3
)>>2;
1203 *coefs
++=fixmul32(atemp
,exponents
[i
<<bsize
>>esize
]);
1205 n
= s
->block_len
- s
->coefs_end
[bsize
];
1206 memset(coefs
, 0, n
*sizeof(fixed32
));
1213 if (s
->ms_stereo
&& s
->channel_coded
[1])
1217 fixed32 (*coefs
)[MAX_CHANNELS
][BLOCK_MAX_SIZE
] = (s
->coefs
);
1219 /* nominal case for ms stereo: we do it before mdct */
1220 /* no need to optimize this case because it should almost
1222 if (!s
->channel_coded
[0])
1224 memset((*(s
->coefs
))[0], 0, sizeof(fixed32
) * s
->block_len
);
1225 s
->channel_coded
[0] = 1;
1228 for(i
= 0; i
< s
->block_len
; ++i
)
1232 (*coefs
)[0][i
] = a
+ b
;
1233 (*coefs
)[1][i
] = a
- b
;
1237 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1239 if (s
->channel_coded
[ch
])
1243 n4
= s
->block_len
>>1;
1245 ff_imdct_calc( (s
->frame_len_bits
- bsize
+ 1),
1246 (int32_t*)scratch_buffer
,
1249 /* add in the frame */
1250 index
= (s
->frame_len
/ 2) + s
->block_pos
- n4
;
1251 wma_window(s
, scratch_buffer
, &((*s
->frame_out
)[ch
][index
]));
1255 /* specific fast case for ms-stereo : add to second
1256 channel if it is not coded */
1257 if (s
->ms_stereo
&& !s
->channel_coded
[1])
1259 wma_window(s
, scratch_buffer
, &((*s
->frame_out
)[1][index
]));
1264 /* update block number */
1266 s
->block_pos
+= s
->block_len
;
1267 if (s
->block_pos
>= s
->frame_len
)
1277 /* decode a frame of frame_len samples */
1278 static int wma_decode_frame(WMADecodeContext
*s
, int32_t *samples
)
1280 int ret
, i
, n
, ch
, incr
;
1284 /* read each block */
1291 ret
= wma_decode_block(s
, samples
);
1295 DEBUGF("wma_decode_block failed with code %d\n", ret
);
1304 /* return frame with full 30-bit precision */
1306 incr
= s
->nb_channels
;
1307 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1310 iptr
= &((*s
->frame_out
)[ch
][0]);
1318 memmove(&((*s
->frame_out
)[ch
][0]), &((*s
->frame_out
)[ch
][s
->frame_len
]),
1319 s
->frame_len
* sizeof(fixed32
));
1325 /* Initialise the superframe decoding */
1327 int wma_decode_superframe_init(WMADecodeContext
* s
,
1328 const uint8_t *buf
, /*input*/
1333 s
->last_superframe_len
= 0;
1337 s
->current_frame
= 0;
1339 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1341 if (s
->use_bit_reservoir
)
1343 /* read super frame header */
1344 skip_bits(&s
->gb
, 4); /* super frame index */
1345 s
->nb_frames
= get_bits(&s
->gb
, 4);
1347 if (s
->last_superframe_len
== 0)
1349 else if (s
->nb_frames
== 0)
1352 s
->bit_offset
= get_bits(&s
->gb
, s
->byte_offset_bits
+ 3);
1361 /* Decode a single frame in the current superframe - return -1 if
1362 there was a decoding error, or the number of samples decoded.
1365 int wma_decode_superframe_frame(WMADecodeContext
* s
,
1366 int32_t* samples
, /*output*/
1367 const uint8_t *buf
, /*input*/
1373 if ((s
->use_bit_reservoir
) && (s
->current_frame
== 0))
1375 if (s
->last_superframe_len
> 0)
1377 /* add s->bit_offset bits to last frame */
1378 if ((s
->last_superframe_len
+ ((s
->bit_offset
+ 7) >> 3)) >
1379 MAX_CODED_SUPERFRAME_SIZE
)
1381 DEBUGF("superframe size too large error\n");
1384 q
= s
->last_superframe
+ s
->last_superframe_len
;
1385 len
= s
->bit_offset
;
1388 *q
++ = (get_bits
)(&s
->gb
, 8);
1393 *q
++ = (get_bits
)(&s
->gb
, len
) << (8 - len
);
1396 /* XXX: s->bit_offset bits into last frame */
1397 init_get_bits(&s
->gb
, s
->last_superframe
, MAX_CODED_SUPERFRAME_SIZE
*8);
1398 /* skip unused bits */
1399 if (s
->last_bitoffset
> 0)
1400 skip_bits(&s
->gb
, s
->last_bitoffset
);
1402 /* this frame is stored in the last superframe and in the
1404 if (wma_decode_frame(s
, samples
) < 0)
1411 /* read each frame starting from s->bit_offset */
1412 pos
= s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3;
1413 init_get_bits(&s
->gb
, buf
+ (pos
>> 3), (MAX_CODED_SUPERFRAME_SIZE
- (pos
>> 3))*8);
1416 skip_bits(&s
->gb
, len
);
1418 s
->reset_block_lengths
= 1;
1421 /* If we haven't decoded a frame yet, do it now */
1424 if (wma_decode_frame(s
, samples
) < 0)
1432 if ((s
->use_bit_reservoir
) && (s
->current_frame
== s
->nb_frames
))
1434 /* we copy the end of the frame in the last frame buffer */
1435 pos
= get_bits_count(&s
->gb
) + ((s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3) & ~7);
1436 s
->last_bitoffset
= pos
& 7;
1438 len
= buf_size
- pos
;
1439 if (len
> MAX_CODED_SUPERFRAME_SIZE
|| len
< 0)
1441 DEBUGF("superframe size too large error after decoding\n");
1444 s
->last_superframe_len
= len
;
1445 memcpy(s
->last_superframe
, buf
+ pos
, len
);
1448 return s
->frame_len
;
1451 /* when error, we reset the bit reservoir */
1453 s
->last_superframe_len
= 0;