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>
32 static const uint8_t ff_log2_tab
[256]={
33 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
34 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
35 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
36 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
37 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
38 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
39 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
40 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
43 static inline int av_log2(unsigned int v
)
61 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
);
62 inline void vector_fmul_add_add(fixed32
*dst
, const fixed32
*data
,
63 const fixed32
*window
, int n
);
64 inline void vector_fmul_reverse(fixed32
*dst
, const fixed32
*src0
,
65 const fixed32
*src1
, int len
);
67 /*declarations of statically allocated variables used to remove malloc calls*/
69 fixed32 coefsarray
[MAX_CHANNELS
][BLOCK_MAX_SIZE
] IBSS_ATTR
;
70 /*decode and window into IRAM on targets with at least 80KB of codec IRAM*/
71 fixed32 frame_out_buf
[MAX_CHANNELS
][BLOCK_MAX_SIZE
* 2] IBSS_ATTR_WMA_LARGE_IRAM
;
73 /*MDCT reconstruction windows*/
74 fixed32 stat0
[2048], stat1
[1024], stat2
[512], stat3
[256], stat4
[128];
77 uint16_t *runtabarray
[2], *levtabarray
[2];
79 /*these could be made smaller since only one can be 1336*/
80 uint16_t runtab0
[1336], runtab1
[1336], levtab0
[1336], levtab1
[1336];
82 #define VLCBUF1SIZE 4598
83 #define VLCBUF2SIZE 3574
84 #define VLCBUF3SIZE 360
85 #define VLCBUF4SIZE 540
87 /*putting these in IRAM actually makes PP slower*/
89 VLC_TYPE vlcbuf1
[VLCBUF1SIZE
][2];
90 VLC_TYPE vlcbuf2
[VLCBUF2SIZE
][2];
91 VLC_TYPE vlcbuf3
[VLCBUF3SIZE
][2];
92 VLC_TYPE vlcbuf4
[VLCBUF4SIZE
][2];
98 * Apply MDCT window and add into output.
100 * We ensure that when the windows overlap their squared sum
101 * is always 1 (MDCT reconstruction rule).
103 * The Vorbis I spec has a great diagram explaining this process.
104 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
106 static void wma_window(WMADecodeContext
*s
, fixed32
*in
, fixed32
*out
)
108 //float *in = s->output;
109 int block_len
, bsize
, n
;
113 /* previous block was larger, so we'll use the size of the current
114 * block to set the window size*/
115 if (s
->block_len_bits
<= s
->prev_block_len_bits
) {
116 block_len
= s
->block_len
;
117 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
119 vector_fmul_add_add(out
, in
, s
->windows
[bsize
], block_len
);
122 /*previous block was smaller or the same size, so use it's size to set the window length*/
123 block_len
= 1 << s
->prev_block_len_bits
;
124 /*find the middle of the two overlapped blocks, this will be the first overlapped sample*/
125 n
= (s
->block_len
- block_len
) / 2;
126 bsize
= s
->frame_len_bits
- s
->prev_block_len_bits
;
128 vector_fmul_add_add(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
130 memcpy(out
+n
+block_len
, in
+n
+block_len
, n
*sizeof(fixed32
));
132 /* Advance to the end of the current block and prepare to window it for the next block.
133 * Since the window function needs to be reversed, we do it backwards starting with the
134 * last sample and moving towards the first
140 if (s
->block_len_bits
<= s
->next_block_len_bits
) {
141 block_len
= s
->block_len
;
142 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
144 vector_fmul_reverse(out
, in
, s
->windows
[bsize
], block_len
);
147 block_len
= 1 << s
->next_block_len_bits
;
148 n
= (s
->block_len
- block_len
) / 2;
149 bsize
= s
->frame_len_bits
- s
->next_block_len_bits
;
151 memcpy(out
, in
, n
*sizeof(fixed32
));
153 vector_fmul_reverse(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
155 memset(out
+n
+block_len
, 0, n
*sizeof(fixed32
));
162 /* XXX: use same run/length optimization as mpeg decoders */
163 static void init_coef_vlc(VLC
*vlc
,
164 uint16_t **prun_table
, uint16_t **plevel_table
,
165 const CoefVLCTable
*vlc_table
, int tab
)
167 int n
= vlc_table
->n
;
168 const uint8_t *table_bits
= vlc_table
->huffbits
;
169 const uint32_t *table_codes
= vlc_table
->huffcodes
;
170 const uint16_t *levels_table
= vlc_table
->levels
;
171 uint16_t *run_table
, *level_table
;
176 init_vlc(vlc
, VLCBITS
, n
, table_bits
, 1, 1, table_codes
, 4, 4, 0);
178 run_table
= runtabarray
[tab
];
179 level_table
= levtabarray
[tab
];
190 level_table
[i
] = level
;
195 *prun_table
= run_table
;
196 *plevel_table
= level_table
;
199 int wma_decode_init(WMADecodeContext
* s
, asf_waveformatex_t
*wfx
)
202 int i
, flags1
, flags2
;
212 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
215 /*clear stereo setting to avoid glitches when switching stereo->mono*/
216 s
->channel_coded
[0]=0;
217 s
->channel_coded
[1]=0;
220 s
->sample_rate
= wfx
->rate
;
221 s
->nb_channels
= wfx
->channels
;
222 s
->bit_rate
= wfx
->bitrate
;
223 s
->block_align
= wfx
->blockalign
;
225 s
->coefs
= &coefsarray
;
226 s
->frame_out
= &frame_out_buf
;
228 if (wfx
->codec_id
== ASF_CODEC_ID_WMAV1
) {
230 } else if (wfx
->codec_id
== ASF_CODEC_ID_WMAV2
) {
233 /*one of those other wma flavors that don't have GPLed decoders */
237 /* extract flag infos */
240 extradata
= wfx
->data
;
241 if (s
->version
== 1 && wfx
->datalen
>= 4) {
242 flags1
= extradata
[0] | (extradata
[1] << 8);
243 flags2
= extradata
[2] | (extradata
[3] << 8);
244 }else if (s
->version
== 2 && wfx
->datalen
>= 6){
245 flags1
= extradata
[0] | (extradata
[1] << 8) |
246 (extradata
[2] << 16) | (extradata
[3] << 24);
247 flags2
= extradata
[4] | (extradata
[5] << 8);
249 s
->use_exp_vlc
= flags2
& 0x0001;
250 s
->use_bit_reservoir
= flags2
& 0x0002;
251 s
->use_variable_block_len
= flags2
& 0x0004;
253 /* compute MDCT block size */
254 if (s
->sample_rate
<= 16000){
255 s
->frame_len_bits
= 9;
256 }else if (s
->sample_rate
<= 22050 ||
257 (s
->sample_rate
<= 32000 && s
->version
== 1)){
258 s
->frame_len_bits
= 10;
260 s
->frame_len_bits
= 11;
262 s
->frame_len
= 1 << s
->frame_len_bits
;
263 if (s
-> use_variable_block_len
)
266 nb
= ((flags2
>> 3) & 3) + 1;
267 if ((s
->bit_rate
/ s
->nb_channels
) >= 32000)
271 nb_max
= s
->frame_len_bits
- BLOCK_MIN_BITS
; //max is 11-7
274 s
->nb_block_sizes
= nb
+ 1;
278 s
->nb_block_sizes
= 1;
281 /* init rate dependant parameters */
282 s
->use_noise_coding
= 1;
283 high_freq
= itofix64(s
->sample_rate
) >> 1;
286 /* if version 2, then the rates are normalized */
287 sample_rate1
= s
->sample_rate
;
290 if (sample_rate1
>= 44100)
291 sample_rate1
= 44100;
292 else if (sample_rate1
>= 22050)
293 sample_rate1
= 22050;
294 else if (sample_rate1
>= 16000)
295 sample_rate1
= 16000;
296 else if (sample_rate1
>= 11025)
297 sample_rate1
= 11025;
298 else if (sample_rate1
>= 8000)
302 fixed64 tmp
= itofix64(s
->bit_rate
);
303 fixed64 tmp2
= itofix64(s
->nb_channels
* s
->sample_rate
);
304 bps
= fixdiv64(tmp
, tmp2
);
305 fixed64 tim
= bps
* s
->frame_len
;
306 fixed64 tmpi
= fixdiv64(tim
,itofix64(8));
307 s
->byte_offset_bits
= av_log2(fixtoi64(tmpi
+0x8000)) + 2;
309 /* compute high frequency value and choose if noise coding should
312 if (s
->nb_channels
== 2)
313 bps1
= fixmul32(bps
,0x1999a);
314 if (sample_rate1
== 44100)
317 s
->use_noise_coding
= 0;
319 high_freq
= fixmul32(high_freq
,0x6666);
321 else if (sample_rate1
== 22050)
324 s
->use_noise_coding
= 0;
325 else if (bps1
>= 0xb852)
326 high_freq
= fixmul32(high_freq
,0xb333);
328 high_freq
= fixmul32(high_freq
,0x999a);
330 else if (sample_rate1
== 16000)
333 high_freq
= fixmul32(high_freq
,0x8000);
335 high_freq
= fixmul32(high_freq
,0x4ccd);
337 else if (sample_rate1
== 11025)
339 high_freq
= fixmul32(high_freq
,0xb333);
341 else if (sample_rate1
== 8000)
345 high_freq
= fixmul32(high_freq
,0x8000);
347 else if (bps
> 0xc000)
349 s
->use_noise_coding
= 0;
353 high_freq
= fixmul32(high_freq
,0xa666);
360 high_freq
= fixmul32(high_freq
,0xc000);
362 else if (bps
>= 0x999a)
364 high_freq
= fixmul32(high_freq
,0x999a);
368 high_freq
= fixmul32(high_freq
,0x8000);
372 /* compute the scale factor band sizes for each MDCT block size */
374 int a
, b
, pos
, lpos
, k
, block_len
, i
, j
, n
;
375 const uint8_t *table
;
385 for(k
= 0; k
< s
->nb_block_sizes
; ++k
)
387 block_len
= s
->frame_len
>> k
;
394 a
= wma_critical_freqs
[i
];
396 pos
= ((block_len
* 2 * a
) + (b
>> 1)) / b
;
399 s
->exponent_bands
[0][i
] = pos
- lpos
;
400 if (pos
>= block_len
)
407 s
->exponent_sizes
[0] = i
;
411 /* hardcoded tables */
413 a
= s
->frame_len_bits
- BLOCK_MIN_BITS
- k
;
416 if (s
->sample_rate
>= 44100)
417 table
= exponent_band_44100
[a
];
418 else if (s
->sample_rate
>= 32000)
419 table
= exponent_band_32000
[a
];
420 else if (s
->sample_rate
>= 22050)
421 table
= exponent_band_22050
[a
];
427 s
->exponent_bands
[k
][i
] = table
[i
];
428 s
->exponent_sizes
[k
] = n
;
436 a
= wma_critical_freqs
[i
];
438 pos
= ((block_len
* 2 * a
) + (b
<< 1)) / (4 * b
);
443 s
->exponent_bands
[k
][j
++] = pos
- lpos
;
444 if (pos
>= block_len
)
448 s
->exponent_sizes
[k
] = j
;
452 /* max number of coefs */
453 s
->coefs_end
[k
] = (s
->frame_len
- ((s
->frame_len
* 9) / 100)) >> k
;
454 /* high freq computation */
456 fixed32 tmp1
= high_freq
*2; /* high_freq is a fixed32!*/
457 fixed32 tmp2
=itofix32(s
->sample_rate
>>1);
458 s
->high_band_start
[k
] = fixtoi32( fixdiv32(tmp1
, tmp2
) * (block_len
>>1) +0x8000);
461 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
462 s->sample_rate + 0.5);*/
464 n
= s
->exponent_sizes
[k
];
471 pos
+= s
->exponent_bands
[k
][i
];
473 if (start
< s
->high_band_start
[k
])
474 start
= s
->high_band_start
[k
];
475 if (end
> s
->coefs_end
[k
])
476 end
= s
->coefs_end
[k
];
478 s
->exponent_high_bands
[k
][j
++] = end
- start
;
480 s
->exponent_high_sizes
[k
] = j
;
484 /*Not using the ffmpeg IMDCT anymore*/
486 /* mdct_init_global();
488 for(i = 0; i < s->nb_block_sizes; ++i)
490 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
495 /* ffmpeg uses malloc to only allocate as many window sizes as needed.
496 * However, we're really only interested in the worst case memory usage.
497 * In the worst case you can have 5 window sizes, 128 doubling up 2048
498 * Smaller windows are handled differently.
499 * Since we don't have malloc, just statically allocate this
508 /* init MDCT windows : simple sinus window */
509 for(i
= 0; i
< s
->nb_block_sizes
; i
++)
513 n
= 1 << (s
->frame_len_bits
- i
);
516 /* this calculates 0.5/(2*n) */
517 alpha
= (1<<15)>>(s
->frame_len_bits
- i
+1);
520 fixed32 j2
= itofix32(j
) + 0x8000;
521 /*alpha between 0 and pi/2*/
522 window
[j
] = fsincos(fixmul32(j2
,alpha
)<<16, 0);
524 s
->windows
[i
] = window
;
528 s
->reset_block_lengths
= 1;
530 if (s
->use_noise_coding
)
532 /* init the noise generator */
535 s
->noise_mult
= 0x51f;
536 s
->noise_table
= noisetable_exp
;
540 s
->noise_mult
= 0xa3d;
541 /* LSP values are simply 2x the EXP values */
542 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
543 noisetable_exp
[i
] = noisetable_exp
[i
]<< 1;
544 s
->noise_table
= noisetable_exp
;
547 /* We use a lookup table computered in advance, so no need to do this*/
552 norm
= 0; // PJJ: near as makes any diff to 0!
553 for (i
=0;i
<NOISE_TAB_SIZE
;++i
)
555 seed
= seed
* 314159 + 1;
556 s
->noise_table
[i
] = itofix32((int)seed
) * norm
;
561 s
->hgain_vlc
.table
= vlcbuf4
;
562 s
->hgain_vlc
.table_allocated
= VLCBUF4SIZE
;
563 init_vlc(&s
->hgain_vlc
, HGAINVLCBITS
, sizeof(hgain_huffbits
),
564 hgain_huffbits
, 1, 1,
565 hgain_huffcodes
, 2, 2, 0);
571 s
->exp_vlc
.table
= vlcbuf3
;
572 s
->exp_vlc
.table_allocated
= VLCBUF3SIZE
;
574 init_vlc(&s
->exp_vlc
, EXPVLCBITS
, sizeof(scale_huffbits
),
575 scale_huffbits
, 1, 1,
576 scale_huffcodes
, 4, 4, 0);
580 wma_lsp_to_curve_init(s
, s
->frame_len
);
583 /* choose the VLC tables for the coefficients */
585 if (s
->sample_rate
>= 32000)
589 else if (bps1
< 0x128f6)
593 runtabarray
[0] = runtab0
; runtabarray
[1] = runtab1
;
594 levtabarray
[0] = levtab0
; levtabarray
[1] = levtab1
;
596 s
->coef_vlc
[0].table
= vlcbuf1
;
597 s
->coef_vlc
[0].table_allocated
= VLCBUF1SIZE
;
598 s
->coef_vlc
[1].table
= vlcbuf2
;
599 s
->coef_vlc
[1].table_allocated
= VLCBUF2SIZE
;
602 init_coef_vlc(&s
->coef_vlc
[0], &s
->run_table
[0], &s
->level_table
[0],
603 &coef_vlcs
[coef_vlc_table
* 2], 0);
604 init_coef_vlc(&s
->coef_vlc
[1], &s
->run_table
[1], &s
->level_table
[1],
605 &coef_vlcs
[coef_vlc_table
* 2 + 1], 1);
607 s
->last_superframe_len
= 0;
608 s
->last_bitoffset
= 0;
614 /* compute x^-0.25 with an exponent and mantissa table. We use linear
615 interpolation to reduce the mantissa table size at a small speed
616 expense (linear interpolation approximately doubles the number of
617 bits of precision). */
618 static inline fixed32
pow_m1_4(WMADecodeContext
*s
, fixed32 x
)
629 m
= (u
.v
>> (23 - LSP_POW_BITS
)) & ((1 << LSP_POW_BITS
) - 1);
630 /* build interpolation scale: 1 <= t < 2. */
631 t
.v
= ((u
.v
<< LSP_POW_BITS
) & ((1 << 23) - 1)) | (127 << 23);
632 a
= s
->lsp_pow_m_table1
[m
];
633 b
= s
->lsp_pow_m_table2
[m
];
635 /* lsp_pow_e_table contains 32.32 format */
636 /* TODO: Since we're unlikely have value that cover the whole
637 * IEEE754 range, we probably don't need to have all possible exponents */
639 return (lsp_pow_e_table
[e
] * (a
+ fixmul32(b
, ftofix32(t
.f
))) >>32);
642 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
)
644 fixed32 wdel
, a
, b
, temp
, temp2
;
647 wdel
= fixdiv32(M_PI_F
, itofix32(frame_len
));
648 temp
= fixdiv32(itofix32(1), itofix32(frame_len
));
649 for (i
=0; i
<frame_len
; ++i
)
651 /* TODO: can probably reuse the trig_init values here */
652 fsincos((temp
*i
)<<15, &temp2
);
653 /* get 3 bits headroom + 1 bit from not doubleing the values */
654 s
->lsp_cos_table
[i
] = temp2
>>3;
657 /* NOTE: these two tables are needed to avoid two operations in
662 /*double check this later*/
663 for(i
=(1 << LSP_POW_BITS
) - 1;i
>=0;i
--)
665 m
= (1 << LSP_POW_BITS
) + i
;
666 a
= pow_a_table
[ix
++]<<4;
667 s
->lsp_pow_m_table1
[i
] = 2 * a
- b
;
668 s
->lsp_pow_m_table2
[i
] = b
- a
;
674 /* NOTE: We use the same code as Vorbis here */
675 /* XXX: optimize it further with SSE/3Dnow */
676 static void wma_lsp_to_curve(WMADecodeContext
*s
,
678 fixed32
*val_max_ptr
,
683 fixed32 p
, q
, w
, v
, val_max
, temp
, temp2
;
688 /* shift by 2 now to reduce rounding error,
689 * we can renormalize right before pow_m1_4
694 w
= s
->lsp_cos_table
[i
];
696 for (j
=1;j
<NB_LSP_COEFS
;j
+=2)
698 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
699 temp2
= ((w
- (lsp
[j
- 1]<<11)));
702 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
703 q
= fixmul32b(q
, temp2
)<<4;
704 p
= fixmul32b(p
, (w
- (lsp
[j
]<<11)))<<4;
707 /* 2 in 5.27 format is 0x10000000 */
708 p
= fixmul32(p
, fixmul32b(p
, (0x10000000 - w
)))<<3;
709 q
= fixmul32(q
, fixmul32b(q
, (0x10000000 + w
)))<<3;
711 v
= (p
+ q
) >>9; /* p/q end up as 16.16 */
718 *val_max_ptr
= val_max
;
721 /* decode exponents coded with LSP coefficients (same idea as Vorbis)
722 * only used for low bitrate (< 16kbps) files
724 static void decode_exp_lsp(WMADecodeContext
*s
, int ch
)
726 fixed32 lsp_coefs
[NB_LSP_COEFS
];
729 for (i
= 0; i
< NB_LSP_COEFS
; ++i
)
731 if (i
== 0 || i
>= 8)
732 val
= get_bits(&s
->gb
, 3);
734 val
= get_bits(&s
->gb
, 4);
735 lsp_coefs
[i
] = lsp_codebook
[i
][val
];
740 &s
->max_exponent
[ch
],
745 /* decode exponents coded with VLC codes - used for bitrate >= 32kbps*/
746 static int decode_exp_vlc(WMADecodeContext
*s
, int ch
)
748 int last_exp
, n
, code
;
749 const uint16_t *ptr
, *band_ptr
;
750 fixed32 v
, max_scale
;
753 /*accommodate the 60 negative indices */
754 const fixed32
*pow_10_to_yover16_ptr
= &pow_10_to_yover16
[61];
756 band_ptr
= s
->exponent_bands
[s
->frame_len_bits
- s
->block_len_bits
];
758 q
= s
->exponents
[ch
];
759 q_end
= q
+ s
->block_len
;
763 if (s
->version
== 1) //wmav1 only
765 last_exp
= get_bits(&s
->gb
, 5) + 10;
767 v
= pow_10_to_yover16_ptr
[last_exp
];
781 code
= get_vlc2(&s
->gb
, s
->exp_vlc
.table
, EXPVLCBITS
, EXPMAX
);
786 /* NOTE: this offset is the same as MPEG4 AAC ! */
787 last_exp
+= code
- 60;
789 v
= pow_10_to_yover16_ptr
[last_exp
];
803 s
->max_exponent
[ch
] = max_scale
;
807 /* return 0 if OK. return 1 if last block of frame. return -1 if
808 unrecorrable error. */
809 static int wma_decode_block(WMADecodeContext
*s
, int32_t *scratch_buffer
)
811 int n
, v
, a
, ch
, code
, bsize
;
812 int coef_nb_bits
, total_gain
;
813 int nb_coefs
[MAX_CHANNELS
];
816 /*DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s->block_num, s->block_len, s->frame_len);*/
818 /* compute current block length */
819 if (s
->use_variable_block_len
)
821 n
= av_log2(s
->nb_block_sizes
- 1) + 1;
823 if (s
->reset_block_lengths
)
825 s
->reset_block_lengths
= 0;
826 v
= get_bits(&s
->gb
, n
);
827 if (v
>= s
->nb_block_sizes
)
831 s
->prev_block_len_bits
= s
->frame_len_bits
- v
;
832 v
= get_bits(&s
->gb
, n
);
833 if (v
>= s
->nb_block_sizes
)
837 s
->block_len_bits
= s
->frame_len_bits
- v
;
841 /* update block lengths */
842 s
->prev_block_len_bits
= s
->block_len_bits
;
843 s
->block_len_bits
= s
->next_block_len_bits
;
845 v
= get_bits(&s
->gb
, n
);
847 if (v
>= s
->nb_block_sizes
)
849 // rb->splash(HZ*4, "v was %d", v); //5, 7
850 return -4; //this is it
853 //rb->splash(HZ, "passed v block (%d)!", v);
855 s
->next_block_len_bits
= s
->frame_len_bits
- v
;
859 /* fixed block len */
860 s
->next_block_len_bits
= s
->frame_len_bits
;
861 s
->prev_block_len_bits
= s
->frame_len_bits
;
862 s
->block_len_bits
= s
->frame_len_bits
;
864 /* now check if the block length is coherent with the frame length */
865 s
->block_len
= 1 << s
->block_len_bits
;
867 if ((s
->block_pos
+ s
->block_len
) > s
->frame_len
)
869 return -5; //oddly 32k sample from tracker fails here
872 if (s
->nb_channels
== 2)
874 s
->ms_stereo
= get_bits1(&s
->gb
);
877 for (ch
= 0; ch
< s
->nb_channels
; ++ch
)
879 a
= get_bits1(&s
->gb
);
880 s
->channel_coded
[ch
] = a
;
883 /* if no channel coded, no need to go further */
884 /* XXX: fix potential framing problems */
890 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
892 /* read total gain and extract corresponding number of bits for
893 coef escape coding */
897 a
= get_bits(&s
->gb
, 7);
907 else if (total_gain
< 32)
909 else if (total_gain
< 40)
911 else if (total_gain
< 45)
916 /* compute number of coefficients */
917 n
= s
->coefs_end
[bsize
] - s
->coefs_start
;
919 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
924 if (s
->use_noise_coding
)
927 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
929 if (s
->channel_coded
[ch
])
932 n
= s
->exponent_high_sizes
[bsize
];
935 a
= get_bits1(&s
->gb
);
936 s
->high_band_coded
[ch
][i
] = a
;
937 /* if noise coding, the coefficients are not transmitted */
939 nb_coefs
[ch
] -= s
->exponent_high_bands
[bsize
][i
];
943 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
945 if (s
->channel_coded
[ch
])
949 n
= s
->exponent_high_sizes
[bsize
];
950 val
= (int)0x80000000;
953 if (s
->high_band_coded
[ch
][i
])
955 if (val
== (int)0x80000000)
957 val
= get_bits(&s
->gb
, 7) - 19;
961 //code = get_vlc(&s->gb, &s->hgain_vlc);
962 code
= get_vlc2(&s
->gb
, s
->hgain_vlc
.table
, HGAINVLCBITS
, HGAINMAX
);
969 s
->high_band_values
[ch
][i
] = val
;
976 /* exponents can be reused in short blocks. */
977 if ((s
->block_len_bits
== s
->frame_len_bits
) || get_bits1(&s
->gb
))
979 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
981 if (s
->channel_coded
[ch
])
985 if (decode_exp_vlc(s
, ch
) < 0)
992 decode_exp_lsp(s
, ch
);
994 s
->exponents_bsize
[ch
] = bsize
;
999 /* parse spectral coefficients : just RLE encoding */
1000 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1002 if (s
->channel_coded
[ch
])
1005 int level
, run
, sign
, tindex
;
1006 int16_t *ptr
, *eptr
;
1007 const int16_t *level_table
, *run_table
;
1009 /* special VLC tables are used for ms stereo because
1010 there is potentially less energy there */
1011 tindex
= (ch
== 1 && s
->ms_stereo
);
1012 coef_vlc
= &s
->coef_vlc
[tindex
];
1013 run_table
= s
->run_table
[tindex
];
1014 level_table
= s
->level_table
[tindex
];
1016 ptr
= &s
->coefs1
[ch
][0];
1017 eptr
= ptr
+ nb_coefs
[ch
];
1018 memset(ptr
, 0, s
->block_len
* sizeof(int16_t));
1022 code
= get_vlc2(&s
->gb
, coef_vlc
->table
, VLCBITS
, VLCMAX
);
1036 level
= get_bits(&s
->gb
, coef_nb_bits
);
1037 /* NOTE: this is rather suboptimal. reading
1038 block_len_bits would be better */
1039 run
= get_bits(&s
->gb
, s
->frame_len_bits
);
1044 run
= run_table
[code
];
1045 level
= level_table
[code
];
1047 sign
= get_bits1(&s
->gb
);
1058 /* NOTE: EOB can be omitted */
1063 if (s
->version
== 1 && s
->nb_channels
>= 2)
1065 align_get_bits(&s
->gb
);
1070 int n4
= s
->block_len
>> 1;
1073 mdct_norm
= 0x10000>>(s
->block_len_bits
-1);
1075 if (s
->version
== 1)
1077 mdct_norm
*= fixtoi32(fixsqrt32(itofix32(n4
)));
1082 /* finally compute the MDCT coefficients */
1083 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1085 if (s
->channel_coded
[ch
])
1089 fixed32
*coefs
, atemp
;
1092 fixed32 noise
, temp1
, temp2
, mult2
;
1093 int i
, j
, n
, n1
, last_high_band
, esize
;
1094 fixed32 exp_power
[HIGH_BAND_MAX_SIZE
];
1096 //total_gain, coefs1, mdctnorm are lossless
1098 coefs1
= s
->coefs1
[ch
];
1099 exponents
= s
->exponents
[ch
];
1100 esize
= s
->exponents_bsize
[ch
];
1101 coefs
= (*(s
->coefs
))[ch
];
1105 * The calculation of coefs has a shift right by 2 built in. This
1106 * prepares samples for the Tremor IMDCT which uses a slightly
1107 * different fixed format then the ffmpeg one. If the old ffmpeg
1108 * imdct is used, each shift storing into coefs should be reduced
1110 * See SVN logs for details.
1114 if (s
->use_noise_coding
)
1116 /*This case is only used for low bitrates (typically less then 32kbps)*/
1118 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1120 mult
= fixdiv64(pow_table
[total_gain
+20],Fixed32To64(s
->max_exponent
[ch
]));
1121 mult
= mult
* mdct_norm
;
1124 /* very low freqs : noise */
1125 for(i
= 0;i
< s
->coefs_start
; ++i
)
1127 *coefs
++ = fixmul32( (fixmul32(s
->noise_table
[s
->noise_index
],
1128 exponents
[i
<<bsize
>>esize
])>>4),Fixed32From64(mult1
)) >>2;
1129 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1132 n1
= s
->exponent_high_sizes
[bsize
];
1134 /* compute power of high bands */
1135 exponents
= s
->exponents
[ch
] +(s
->high_band_start
[bsize
]<<bsize
);
1136 last_high_band
= 0; /* avoid warning */
1139 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1140 s
->block_len_bits
][j
];
1141 if (s
->high_band_coded
[ch
][j
])
1145 for(i
= 0;i
< n
; ++i
)
1147 /*v is normalized later on so its fixed format is irrelevant*/
1148 v
= exponents
[i
<<bsize
>>esize
]>>4;
1149 e2
+= fixmul32(v
, v
)>>3;
1151 exp_power
[j
] = e2
/n
; /*n is an int...*/
1154 exponents
+= n
<<bsize
;
1157 /* main freqs and high freqs */
1158 exponents
= s
->exponents
[ch
] + (s
->coefs_start
<<bsize
);
1163 n
= s
->high_band_start
[bsize
] -
1168 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1169 s
->block_len_bits
][j
];
1171 if (j
>= 0 && s
->high_band_coded
[ch
][j
])
1173 /* use noise with specified power */
1174 fixed32 tmp
= fixdiv32(exp_power
[j
],exp_power
[last_high_band
]);
1176 /*mult1 is 48.16, pow_table is 48.16*/
1177 mult1
= fixmul32(fixsqrt32(tmp
),
1178 pow_table
[s
->high_band_values
[ch
][j
]+20]) >> 16;
1180 /*this step has a fairly high degree of error for some reason*/
1181 mult1
= fixdiv64(mult1
,fixmul32(s
->max_exponent
[ch
],s
->noise_mult
));
1182 mult1
= mult1
*mdct_norm
>>PRECISION
;
1183 for(i
= 0;i
< n
; ++i
)
1185 noise
= s
->noise_table
[s
->noise_index
];
1186 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1187 *coefs
++ = fixmul32((fixmul32(exponents
[i
<<bsize
>>esize
],noise
)>>4),
1188 Fixed32From64(mult1
)) >>2;
1191 exponents
+= n
<<bsize
;
1195 /* coded values + small noise */
1196 for(i
= 0;i
< n
; ++i
)
1198 noise
= s
->noise_table
[s
->noise_index
];
1199 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1201 /*don't forget to renormalize the noise*/
1202 temp1
= (((int32_t)*coefs1
++)<<16) + (noise
>>4);
1203 temp2
= fixmul32(exponents
[i
<<bsize
>>esize
], mult
>>18);
1204 *coefs
++ = fixmul32(temp1
, temp2
);
1206 exponents
+= n
<<bsize
;
1210 /* very high freqs : noise */
1211 n
= s
->block_len
- s
->coefs_end
[bsize
];
1212 mult2
= fixmul32(mult
>>16,exponents
[((-1<<bsize
))>>esize
]) ;
1213 for (i
= 0; i
< n
; ++i
)
1215 /*renormalize the noise product and then reduce to 14.18 precison*/
1216 *coefs
++ = fixmul32(s
->noise_table
[s
->noise_index
],mult2
) >>6;
1218 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1223 /*Noise coding not used, simply convert from exp to fixed representation*/
1225 fixed32 mult3
= (fixed32
)(fixdiv64(pow_table
[total_gain
+20],
1226 Fixed32To64(s
->max_exponent
[ch
])));
1227 mult3
= fixmul32(mult3
, mdct_norm
);
1229 /*zero the first 3 coefficients for WMA V1, does nothing otherwise*/
1230 for(i
=0; i
<s
->coefs_start
; i
++)
1235 /* XXX: optimize more, unrolling this loop in asm
1236 might be a good idea */
1238 for(i
= 0;i
< n
; ++i
)
1240 /*ffmpeg imdct needs 15.17, while tremor 14.18*/
1241 atemp
= (coefs1
[i
] * mult3
)>>2;
1242 *coefs
++=fixmul32(atemp
,exponents
[i
<<bsize
>>esize
]);
1244 n
= s
->block_len
- s
->coefs_end
[bsize
];
1245 memset(coefs
, 0, n
*sizeof(fixed32
));
1252 if (s
->ms_stereo
&& s
->channel_coded
[1])
1256 fixed32 (*coefs
)[MAX_CHANNELS
][BLOCK_MAX_SIZE
] = (s
->coefs
);
1258 /* nominal case for ms stereo: we do it before mdct */
1259 /* no need to optimize this case because it should almost
1261 if (!s
->channel_coded
[0])
1263 memset((*(s
->coefs
))[0], 0, sizeof(fixed32
) * s
->block_len
);
1264 s
->channel_coded
[0] = 1;
1267 for(i
= 0; i
< s
->block_len
; ++i
)
1271 (*coefs
)[0][i
] = a
+ b
;
1272 (*coefs
)[1][i
] = a
- b
;
1276 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1278 if (s
->channel_coded
[ch
])
1282 n4
= s
->block_len
>>1;
1284 /*faster IMDCT from Vorbis*/
1285 mdct_backward( (1 << (s
->block_len_bits
+1)), (int32_t*)(*(s
->coefs
))[ch
], (int32_t*)scratch_buffer
);
1287 /*slower but more easily understood IMDCT from FFMPEG*/
1288 //ff_imdct_calc(&s->mdct_ctx[bsize],
1290 // (*(s->coefs))[ch]);
1293 /* add in the frame */
1294 index
= (s
->frame_len
/ 2) + s
->block_pos
- n4
;
1295 wma_window(s
, scratch_buffer
, &((*s
->frame_out
)[ch
][index
]));
1299 /* specific fast case for ms-stereo : add to second
1300 channel if it is not coded */
1301 if (s
->ms_stereo
&& !s
->channel_coded
[1])
1303 wma_window(s
, scratch_buffer
, &((*s
->frame_out
)[1][index
]));
1308 /* update block number */
1310 s
->block_pos
+= s
->block_len
;
1311 if (s
->block_pos
>= s
->frame_len
)
1321 /* decode a frame of frame_len samples */
1322 static int wma_decode_frame(WMADecodeContext
*s
, int32_t *samples
)
1324 int ret
, i
, n
, ch
, incr
;
1328 /* read each block */
1335 ret
= wma_decode_block(s
, samples
);
1339 DEBUGF("wma_decode_block failed with code %d\n", ret
);
1348 /* return frame with full 30-bit precision */
1350 incr
= s
->nb_channels
;
1351 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1354 iptr
= &((*s
->frame_out
)[ch
][0]);
1362 memmove(&((*s
->frame_out
)[ch
][0]), &((*s
->frame_out
)[ch
][s
->frame_len
]),
1363 s
->frame_len
* sizeof(fixed32
));
1369 /* Initialise the superframe decoding */
1371 int wma_decode_superframe_init(WMADecodeContext
* s
,
1372 const uint8_t *buf
, /*input*/
1377 s
->last_superframe_len
= 0;
1381 s
->current_frame
= 0;
1383 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1385 if (s
->use_bit_reservoir
)
1387 /* read super frame header */
1388 skip_bits(&s
->gb
, 4); /* super frame index */
1389 s
->nb_frames
= get_bits(&s
->gb
, 4);
1391 if (s
->last_superframe_len
== 0)
1393 else if (s
->nb_frames
== 0)
1396 s
->bit_offset
= get_bits(&s
->gb
, s
->byte_offset_bits
+ 3);
1405 /* Decode a single frame in the current superframe - return -1 if
1406 there was a decoding error, or the number of samples decoded.
1409 int wma_decode_superframe_frame(WMADecodeContext
* s
,
1410 int32_t* samples
, /*output*/
1411 const uint8_t *buf
, /*input*/
1417 if ((s
->use_bit_reservoir
) && (s
->current_frame
== 0))
1419 if (s
->last_superframe_len
> 0)
1421 /* add s->bit_offset bits to last frame */
1422 if ((s
->last_superframe_len
+ ((s
->bit_offset
+ 7) >> 3)) >
1423 MAX_CODED_SUPERFRAME_SIZE
)
1425 DEBUGF("superframe size too large error\n");
1428 q
= s
->last_superframe
+ s
->last_superframe_len
;
1429 len
= s
->bit_offset
;
1432 *q
++ = (get_bits
)(&s
->gb
, 8);
1437 *q
++ = (get_bits
)(&s
->gb
, len
) << (8 - len
);
1440 /* XXX: s->bit_offset bits into last frame */
1441 init_get_bits(&s
->gb
, s
->last_superframe
, MAX_CODED_SUPERFRAME_SIZE
*8);
1442 /* skip unused bits */
1443 if (s
->last_bitoffset
> 0)
1444 skip_bits(&s
->gb
, s
->last_bitoffset
);
1446 /* this frame is stored in the last superframe and in the
1448 if (wma_decode_frame(s
, samples
) < 0)
1455 /* read each frame starting from s->bit_offset */
1456 pos
= s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3;
1457 init_get_bits(&s
->gb
, buf
+ (pos
>> 3), (MAX_CODED_SUPERFRAME_SIZE
- (pos
>> 3))*8);
1460 skip_bits(&s
->gb
, len
);
1462 s
->reset_block_lengths
= 1;
1465 /* If we haven't decoded a frame yet, do it now */
1468 if (wma_decode_frame(s
, samples
) < 0)
1476 if ((s
->use_bit_reservoir
) && (s
->current_frame
== s
->nb_frames
))
1478 /* we copy the end of the frame in the last frame buffer */
1479 pos
= get_bits_count(&s
->gb
) + ((s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3) & ~7);
1480 s
->last_bitoffset
= pos
& 7;
1482 len
= buf_size
- pos
;
1483 if (len
> MAX_CODED_SUPERFRAME_SIZE
|| len
< 0)
1485 DEBUGF("superframe size too large error after decoding\n");
1488 s
->last_superframe_len
= len
;
1489 memcpy(s
->last_superframe
, buf
+ pos
, len
);
1492 return s
->frame_len
;
1495 /* when error, we reset the bit reservoir */
1497 s
->last_superframe_len
= 0;