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
);
34 /*declarations of statically allocated variables used to remove malloc calls*/
36 static fixed32 coefsarray
[MAX_CHANNELS
][BLOCK_MAX_SIZE
] IBSS_ATTR MEM_ALIGN_ATTR
;
37 /*decode and window into IRAM on targets with at least 80KB of codec IRAM*/
38 static fixed32 frame_out_buf
[MAX_CHANNELS
][BLOCK_MAX_SIZE
* 2] IBSS_ATTR_WMA_LARGE_IRAM MEM_ALIGN_ATTR
;
40 /*MDCT reconstruction windows*/
41 static fixed32 stat0
[2048] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR
;
42 static fixed32 stat1
[1024] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR
;
43 static fixed32 stat2
[ 512] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR
;
44 static fixed32 stat3
[ 256] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR
;
45 static fixed32 stat4
[ 128] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR
;
48 static uint16_t *runtabarray
[2];
49 static uint16_t *levtabarray
[2];
51 static uint16_t runtab_big
[1336] MEM_ALIGN_ATTR
;
52 static uint16_t runtab_small
[1072] MEM_ALIGN_ATTR
;
53 static uint16_t levtab_big
[1336] MEM_ALIGN_ATTR
;
54 static uint16_t levtab_small
[1072] MEM_ALIGN_ATTR
;
56 #define VLCBUF1SIZE 4598
57 #define VLCBUF2SIZE 3574
58 #define VLCBUF3SIZE 360
59 #define VLCBUF4SIZE 540
61 /*putting these in IRAM actually makes PP slower*/
63 static VLC_TYPE vlcbuf1
[VLCBUF1SIZE
][2] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR
;
64 static VLC_TYPE vlcbuf2
[VLCBUF2SIZE
][2] MEM_ALIGN_ATTR
;
65 /* This buffer gets reused for lsp tables */
66 static VLC_TYPE vlcbuf3
[VLCBUF3SIZE
][2] MEM_ALIGN_ATTR
;
67 static VLC_TYPE vlcbuf4
[VLCBUF4SIZE
][2] MEM_ALIGN_ATTR
;
73 * Apply MDCT window and add into output.
75 * We ensure that when the windows overlap their squared sum
76 * is always 1 (MDCT reconstruction rule).
78 * The Vorbis I spec has a great diagram explaining this process.
79 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
81 static void wma_window(WMADecodeContext
*s
, fixed32
*in
, fixed32
*out
)
83 //float *in = s->output;
84 int block_len
, bsize
, n
;
88 /* previous block was larger, so we'll use the size of the current
89 * block to set the window size*/
90 if (s
->block_len_bits
<= s
->prev_block_len_bits
) {
91 block_len
= s
->block_len
;
92 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
94 vector_fmul_add_add(out
, in
, s
->windows
[bsize
], block_len
);
97 /*previous block was smaller or the same size, so use it's size to set the window length*/
98 block_len
= 1 << s
->prev_block_len_bits
;
99 /*find the middle of the two overlapped blocks, this will be the first overlapped sample*/
100 n
= (s
->block_len
- block_len
) / 2;
101 bsize
= s
->frame_len_bits
- s
->prev_block_len_bits
;
103 vector_fmul_add_add(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
105 memcpy(out
+n
+block_len
, in
+n
+block_len
, n
*sizeof(fixed32
));
107 /* Advance to the end of the current block and prepare to window it for the next block.
108 * Since the window function needs to be reversed, we do it backwards starting with the
109 * last sample and moving towards the first
115 if (s
->block_len_bits
<= s
->next_block_len_bits
) {
116 block_len
= s
->block_len
;
117 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
119 vector_fmul_reverse(out
, in
, s
->windows
[bsize
], block_len
);
122 block_len
= 1 << s
->next_block_len_bits
;
123 n
= (s
->block_len
- block_len
) / 2;
124 bsize
= s
->frame_len_bits
- s
->next_block_len_bits
;
126 memcpy(out
, in
, n
*sizeof(fixed32
));
128 vector_fmul_reverse(out
+n
, in
+n
, s
->windows
[bsize
], block_len
);
130 memset(out
+n
+block_len
, 0, n
*sizeof(fixed32
));
137 /* XXX: use same run/length optimization as mpeg decoders */
138 static void init_coef_vlc(VLC
*vlc
,
139 uint16_t **prun_table
, uint16_t **plevel_table
,
140 const CoefVLCTable
*vlc_table
, int tab
)
142 int n
= vlc_table
->n
;
143 const uint8_t *table_bits
= vlc_table
->huffbits
;
144 const uint32_t *table_codes
= vlc_table
->huffcodes
;
145 const uint16_t *levels_table
= vlc_table
->levels
;
146 uint16_t *run_table
, *level_table
;
151 init_vlc(vlc
, VLCBITS
, n
, table_bits
, 1, 1, table_codes
, 4, 4, INIT_VLC_USE_NEW_STATIC
);
153 run_table
= runtabarray
[tab
];
154 level_table
= levtabarray
[tab
];
165 level_table
[i
] = level
;
170 *prun_table
= run_table
;
171 *plevel_table
= level_table
;
174 int wma_decode_init(WMADecodeContext
* s
, asf_waveformatex_t
*wfx
)
187 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
190 /*clear stereo setting to avoid glitches when switching stereo->mono*/
191 s
->channel_coded
[0]=0;
192 s
->channel_coded
[1]=0;
195 s
->sample_rate
= wfx
->rate
;
196 s
->nb_channels
= wfx
->channels
;
197 s
->bit_rate
= wfx
->bitrate
;
198 s
->block_align
= wfx
->blockalign
;
200 s
->coefs
= &coefsarray
;
201 s
->frame_out
= &frame_out_buf
;
203 if (wfx
->codec_id
== ASF_CODEC_ID_WMAV1
) {
205 } else if (wfx
->codec_id
== ASF_CODEC_ID_WMAV2
) {
208 /*one of those other wma flavors that don't have GPLed decoders */
212 /* extract flag infos */
214 extradata
= wfx
->data
;
215 if (s
->version
== 1 && wfx
->datalen
>= 4) {
216 flags2
= extradata
[2] | (extradata
[3] << 8);
217 }else if (s
->version
== 2 && wfx
->datalen
>= 6){
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 /* since the coef2 table is the biggest and that has index 2 in coef_vlcs
554 it's safe to always assign like this */
555 runtabarray
[0] = runtab_big
; runtabarray
[1] = runtab_small
;
556 levtabarray
[0] = levtab_big
; levtabarray
[1] = levtab_small
;
558 s
->coef_vlc
[0].table
= vlcbuf1
;
559 s
->coef_vlc
[0].table_allocated
= VLCBUF1SIZE
;
560 s
->coef_vlc
[1].table
= vlcbuf2
;
561 s
->coef_vlc
[1].table_allocated
= VLCBUF2SIZE
;
564 init_coef_vlc(&s
->coef_vlc
[0], &s
->run_table
[0], &s
->level_table
[0],
565 &coef_vlcs
[coef_vlc_table
* 2], 0);
566 init_coef_vlc(&s
->coef_vlc
[1], &s
->run_table
[1], &s
->level_table
[1],
567 &coef_vlcs
[coef_vlc_table
* 2 + 1], 1);
569 s
->last_superframe_len
= 0;
570 s
->last_bitoffset
= 0;
576 /* compute x^-0.25 with an exponent and mantissa table. We use linear
577 interpolation to reduce the mantissa table size at a small speed
578 expense (linear interpolation approximately doubles the number of
579 bits of precision). */
580 static inline fixed32
pow_m1_4(WMADecodeContext
*s
, fixed32 x
)
591 m
= (u
.v
>> (23 - LSP_POW_BITS
)) & ((1 << LSP_POW_BITS
) - 1);
592 /* build interpolation scale: 1 <= t < 2. */
593 t
.v
= ((u
.v
<< LSP_POW_BITS
) & ((1 << 23) - 1)) | (127 << 23);
594 a
= ((fixed32
*)s
->lsp_pow_m_table1
)[m
];
595 b
= ((fixed32
*)s
->lsp_pow_m_table2
)[m
];
597 /* lsp_pow_e_table contains 32.32 format */
598 /* TODO: Since we're unlikely have value that cover the whole
599 * IEEE754 range, we probably don't need to have all possible exponents */
601 return (lsp_pow_e_table
[e
] * (a
+ fixmul32(b
, ftofix32(t
.f
))) >>32);
604 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
)
606 fixed32 wdel
, a
, b
, temp2
;
609 wdel
= fixdiv32(itofix32(1), itofix32(frame_len
));
610 for (i
=0; i
<frame_len
; ++i
)
612 /* TODO: can probably reuse the trig_init values here */
613 fsincos((wdel
*i
)<<15, &temp2
);
614 /* get 3 bits headroom + 1 bit from not doubleing the values */
615 s
->lsp_cos_table
[i
] = temp2
>>3;
618 /* NOTE: these two tables are needed to avoid two operations in
623 s
->lsp_pow_m_table1
= &vlcbuf3
[0];
624 s
->lsp_pow_m_table2
= &vlcbuf3
[1<<LSP_POW_BITS
];
626 /*double check this later*/
627 for(i
=(1 << LSP_POW_BITS
) - 1;i
>=0;i
--)
629 a
= pow_a_table
[ix
++]<<4;
630 ((fixed32
*)s
->lsp_pow_m_table1
)[i
] = 2 * a
- b
;
631 ((fixed32
*)s
->lsp_pow_m_table2
)[i
] = b
- a
;
637 /* NOTE: We use the same code as Vorbis here */
638 /* XXX: optimize it further with SSE/3Dnow */
639 static void wma_lsp_to_curve(WMADecodeContext
*s
,
641 fixed32
*val_max_ptr
,
646 fixed32 p
, q
, w
, v
, val_max
, temp2
;
651 /* shift by 2 now to reduce rounding error,
652 * we can renormalize right before pow_m1_4
657 w
= s
->lsp_cos_table
[i
];
659 for (j
=1;j
<NB_LSP_COEFS
;j
+=2)
661 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
662 temp2
= ((w
- (lsp
[j
- 1]<<11)));
664 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
665 q
= fixmul32b(q
, temp2
)<<4;
666 p
= fixmul32b(p
, (w
- (lsp
[j
]<<11)))<<4;
669 /* 2 in 5.27 format is 0x10000000 */
670 p
= fixmul32(p
, fixmul32b(p
, (0x10000000 - w
)))<<3;
671 q
= fixmul32(q
, fixmul32b(q
, (0x10000000 + w
)))<<3;
673 v
= (p
+ q
) >>9; /* p/q end up as 16.16 */
680 *val_max_ptr
= val_max
;
683 /* decode exponents coded with LSP coefficients (same idea as Vorbis)
684 * only used for low bitrate (< 16kbps) files
686 static void decode_exp_lsp(WMADecodeContext
*s
, int ch
)
688 fixed32 lsp_coefs
[NB_LSP_COEFS
];
691 for (i
= 0; i
< NB_LSP_COEFS
; ++i
)
693 if (i
== 0 || i
>= 8)
694 val
= get_bits(&s
->gb
, 3);
696 val
= get_bits(&s
->gb
, 4);
697 lsp_coefs
[i
] = lsp_codebook
[i
][val
];
702 &s
->max_exponent
[ch
],
707 /* decode exponents coded with VLC codes - used for bitrate >= 32kbps*/
708 static int decode_exp_vlc(WMADecodeContext
*s
, int ch
)
710 int last_exp
, n
, code
;
711 const uint16_t *ptr
, *band_ptr
;
712 fixed32 v
, max_scale
;
715 /*accommodate the 60 negative indices */
716 const fixed32
*pow_10_to_yover16_ptr
= &pow_10_to_yover16
[61];
718 band_ptr
= s
->exponent_bands
[s
->frame_len_bits
- s
->block_len_bits
];
720 q
= s
->exponents
[ch
];
721 q_end
= q
+ s
->block_len
;
725 if (s
->version
== 1) //wmav1 only
727 last_exp
= get_bits(&s
->gb
, 5) + 10;
729 v
= pow_10_to_yover16_ptr
[last_exp
];
737 } while ((n
-= 4) > 0);
744 code
= get_vlc2(&s
->gb
, s
->exp_vlc
.table
, EXPVLCBITS
, EXPMAX
);
749 /* NOTE: this offset is the same as MPEG4 AAC ! */
750 last_exp
+= code
- 60;
752 v
= pow_10_to_yover16_ptr
[last_exp
];
763 } while ((n
-= 4) > 0);
766 s
->max_exponent
[ch
] = max_scale
;
770 /* return 0 if OK. return 1 if last block of frame. return -1 if
771 unrecorrable error. */
772 static int wma_decode_block(WMADecodeContext
*s
)
774 int n
, v
, a
, ch
, code
, bsize
;
775 int coef_nb_bits
, total_gain
;
776 int nb_coefs
[MAX_CHANNELS
];
779 /*DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s->block_num, s->block_len, s->frame_len);*/
781 /* compute current block length */
782 if (s
->use_variable_block_len
)
784 n
= av_log2(s
->nb_block_sizes
- 1) + 1;
786 if (s
->reset_block_lengths
)
788 s
->reset_block_lengths
= 0;
789 v
= get_bits(&s
->gb
, n
);
790 if (v
>= s
->nb_block_sizes
)
794 s
->prev_block_len_bits
= s
->frame_len_bits
- v
;
795 v
= get_bits(&s
->gb
, n
);
796 if (v
>= s
->nb_block_sizes
)
800 s
->block_len_bits
= s
->frame_len_bits
- v
;
804 /* update block lengths */
805 s
->prev_block_len_bits
= s
->block_len_bits
;
806 s
->block_len_bits
= s
->next_block_len_bits
;
808 v
= get_bits(&s
->gb
, n
);
810 if (v
>= s
->nb_block_sizes
)
812 // rb->splash(HZ*4, "v was %d", v); //5, 7
813 return -4; //this is it
816 //rb->splash(HZ, "passed v block (%d)!", v);
818 s
->next_block_len_bits
= s
->frame_len_bits
- v
;
822 /* fixed block len */
823 s
->next_block_len_bits
= s
->frame_len_bits
;
824 s
->prev_block_len_bits
= s
->frame_len_bits
;
825 s
->block_len_bits
= s
->frame_len_bits
;
827 /* now check if the block length is coherent with the frame length */
828 s
->block_len
= 1 << s
->block_len_bits
;
830 if ((s
->block_pos
+ s
->block_len
) > s
->frame_len
)
832 return -5; //oddly 32k sample from tracker fails here
835 if (s
->nb_channels
== 2)
837 s
->ms_stereo
= get_bits1(&s
->gb
);
840 for (ch
= 0; ch
< s
->nb_channels
; ++ch
)
842 a
= get_bits1(&s
->gb
);
843 s
->channel_coded
[ch
] = a
;
846 /* if no channel coded, no need to go further */
847 /* XXX: fix potential framing problems */
853 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
855 /* read total gain and extract corresponding number of bits for
856 coef escape coding */
860 a
= get_bits(&s
->gb
, 7);
870 else if (total_gain
< 32)
872 else if (total_gain
< 40)
874 else if (total_gain
< 45)
879 /* compute number of coefficients */
880 n
= s
->coefs_end
[bsize
] - s
->coefs_start
;
882 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
887 if (s
->use_noise_coding
)
890 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
892 if (s
->channel_coded
[ch
])
895 n
= s
->exponent_high_sizes
[bsize
];
898 a
= get_bits1(&s
->gb
);
899 s
->high_band_coded
[ch
][i
] = a
;
900 /* if noise coding, the coefficients are not transmitted */
902 nb_coefs
[ch
] -= s
->exponent_high_bands
[bsize
][i
];
906 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
908 if (s
->channel_coded
[ch
])
912 n
= s
->exponent_high_sizes
[bsize
];
913 val
= (int)0x80000000;
916 if (s
->high_band_coded
[ch
][i
])
918 if (val
== (int)0x80000000)
920 val
= get_bits(&s
->gb
, 7) - 19;
924 //code = get_vlc(&s->gb, &s->hgain_vlc);
925 code
= get_vlc2(&s
->gb
, s
->hgain_vlc
.table
, HGAINVLCBITS
, HGAINMAX
);
932 s
->high_band_values
[ch
][i
] = val
;
939 /* exponents can be reused in short blocks. */
940 if ((s
->block_len_bits
== s
->frame_len_bits
) || get_bits1(&s
->gb
))
942 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
944 if (s
->channel_coded
[ch
])
948 if (decode_exp_vlc(s
, ch
) < 0)
955 decode_exp_lsp(s
, ch
);
957 s
->exponents_bsize
[ch
] = bsize
;
962 /* parse spectral coefficients : just RLE encoding */
963 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
965 if (s
->channel_coded
[ch
])
968 int level
, run
, sign
, tindex
;
970 const int16_t *level_table
, *run_table
;
972 /* special VLC tables are used for ms stereo because
973 there is potentially less energy there */
974 tindex
= (ch
== 1 && s
->ms_stereo
);
975 coef_vlc
= &s
->coef_vlc
[tindex
];
976 run_table
= s
->run_table
[tindex
];
977 level_table
= s
->level_table
[tindex
];
979 ptr
= &s
->coefs1
[ch
][0];
980 eptr
= ptr
+ nb_coefs
[ch
];
981 memset(ptr
, 0, s
->block_len
* sizeof(int16_t));
985 code
= get_vlc2(&s
->gb
, coef_vlc
->table
, VLCBITS
, VLCMAX
);
999 level
= get_bits(&s
->gb
, coef_nb_bits
);
1000 /* NOTE: this is rather suboptimal. reading
1001 block_len_bits would be better */
1002 run
= get_bits(&s
->gb
, s
->frame_len_bits
);
1007 run
= run_table
[code
];
1008 level
= level_table
[code
];
1010 sign
= get_bits1(&s
->gb
);
1021 /* NOTE: EOB can be omitted */
1026 if (s
->version
== 1 && s
->nb_channels
>= 2)
1028 align_get_bits(&s
->gb
);
1033 int n4
= s
->block_len
>> 1;
1036 mdct_norm
= 0x10000>>(s
->block_len_bits
-1);
1038 if (s
->version
== 1)
1040 mdct_norm
*= fixtoi32(fixsqrt32(itofix32(n4
)));
1045 /* finally compute the MDCT coefficients */
1046 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1048 if (s
->channel_coded
[ch
])
1052 fixed32
*coefs
, atemp
;
1055 fixed32 noise
, temp1
, temp2
, mult2
;
1056 int i
, j
, n
, n1
, last_high_band
, esize
;
1057 fixed32 exp_power
[HIGH_BAND_MAX_SIZE
];
1059 //total_gain, coefs1, mdctnorm are lossless
1061 coefs1
= s
->coefs1
[ch
];
1062 exponents
= s
->exponents
[ch
];
1063 esize
= s
->exponents_bsize
[ch
];
1064 coefs
= (*(s
->coefs
))[ch
];
1068 * The calculation of coefs has a shift right by 2 built in. This
1069 * prepares samples for the Tremor IMDCT which uses a slightly
1070 * different fixed format then the ffmpeg one. If the old ffmpeg
1071 * imdct is used, each shift storing into coefs should be reduced
1073 * See SVN logs for details.
1077 if (s
->use_noise_coding
)
1079 /*This case is only used for low bitrates (typically less then 32kbps)*/
1081 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1083 mult
= fixdiv64(pow_table
[total_gain
+20],Fixed32To64(s
->max_exponent
[ch
]));
1084 mult
= mult
* mdct_norm
;
1087 /* very low freqs : noise */
1088 for(i
= 0;i
< s
->coefs_start
; ++i
)
1090 *coefs
++ = fixmul32( (fixmul32(s
->noise_table
[s
->noise_index
],
1091 exponents
[i
<<bsize
>>esize
])>>4),Fixed32From64(mult1
)) >>2;
1092 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1095 n1
= s
->exponent_high_sizes
[bsize
];
1097 /* compute power of high bands */
1098 exponents
= s
->exponents
[ch
] +(s
->high_band_start
[bsize
]<<bsize
);
1099 last_high_band
= 0; /* avoid warning */
1102 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1103 s
->block_len_bits
][j
];
1104 if (s
->high_band_coded
[ch
][j
])
1108 for(i
= 0;i
< n
; ++i
)
1110 /*v is normalized later on so its fixed format is irrelevant*/
1111 v
= exponents
[i
<<bsize
>>esize
]>>4;
1112 e2
+= fixmul32(v
, v
)>>3;
1114 exp_power
[j
] = e2
/n
; /*n is an int...*/
1117 exponents
+= n
<<bsize
;
1120 /* main freqs and high freqs */
1121 exponents
= s
->exponents
[ch
] + (s
->coefs_start
<<bsize
);
1126 n
= s
->high_band_start
[bsize
] -
1131 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
1132 s
->block_len_bits
][j
];
1134 if (j
>= 0 && s
->high_band_coded
[ch
][j
])
1136 /* use noise with specified power */
1137 fixed32 tmp
= fixdiv32(exp_power
[j
],exp_power
[last_high_band
]);
1139 /*mult1 is 48.16, pow_table is 48.16*/
1140 mult1
= fixmul32(fixsqrt32(tmp
),
1141 pow_table
[s
->high_band_values
[ch
][j
]+20]) >> 16;
1143 /*this step has a fairly high degree of error for some reason*/
1144 mult1
= fixdiv64(mult1
,fixmul32(s
->max_exponent
[ch
],s
->noise_mult
));
1145 mult1
= mult1
*mdct_norm
>>PRECISION
;
1146 for(i
= 0;i
< n
; ++i
)
1148 noise
= s
->noise_table
[s
->noise_index
];
1149 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1150 *coefs
++ = fixmul32((fixmul32(exponents
[i
<<bsize
>>esize
],noise
)>>4),
1151 Fixed32From64(mult1
)) >>2;
1154 exponents
+= n
<<bsize
;
1158 /* coded values + small noise */
1159 for(i
= 0;i
< n
; ++i
)
1161 noise
= s
->noise_table
[s
->noise_index
];
1162 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1164 /*don't forget to renormalize the noise*/
1165 temp1
= (((int32_t)*coefs1
++)<<16) + (noise
>>4);
1166 temp2
= fixmul32(exponents
[i
<<bsize
>>esize
], mult
>>18);
1167 *coefs
++ = fixmul32(temp1
, temp2
);
1169 exponents
+= n
<<bsize
;
1173 /* very high freqs : noise */
1174 n
= s
->block_len
- s
->coefs_end
[bsize
];
1175 mult2
= fixmul32(mult
>>16,exponents
[((-1<<bsize
))>>esize
]) ;
1176 for (i
= 0; i
< n
; ++i
)
1178 /*renormalize the noise product and then reduce to 14.18 precison*/
1179 *coefs
++ = fixmul32(s
->noise_table
[s
->noise_index
],mult2
) >>6;
1181 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1186 /*Noise coding not used, simply convert from exp to fixed representation*/
1188 fixed32 mult3
= (fixed32
)(fixdiv64(pow_table
[total_gain
+20],
1189 Fixed32To64(s
->max_exponent
[ch
])));
1190 mult3
= fixmul32(mult3
, mdct_norm
);
1192 /*zero the first 3 coefficients for WMA V1, does nothing otherwise*/
1193 for(i
=0; i
<s
->coefs_start
; i
++)
1198 /* XXX: optimize more, unrolling this loop in asm
1199 might be a good idea */
1201 for(i
= 0;i
< n
; ++i
)
1203 /*ffmpeg imdct needs 15.17, while tremor 14.18*/
1204 atemp
= (coefs1
[i
] * mult3
)>>2;
1205 *coefs
++=fixmul32(atemp
,exponents
[i
<<bsize
>>esize
]);
1207 n
= s
->block_len
- s
->coefs_end
[bsize
];
1208 memset(coefs
, 0, n
*sizeof(fixed32
));
1215 if (s
->ms_stereo
&& s
->channel_coded
[1])
1219 fixed32 (*coefs
)[MAX_CHANNELS
][BLOCK_MAX_SIZE
] = (s
->coefs
);
1221 /* nominal case for ms stereo: we do it before mdct */
1222 /* no need to optimize this case because it should almost
1224 if (!s
->channel_coded
[0])
1226 memset((*(s
->coefs
))[0], 0, sizeof(fixed32
) * s
->block_len
);
1227 s
->channel_coded
[0] = 1;
1230 for(i
= 0; i
< s
->block_len
; ++i
)
1234 (*coefs
)[0][i
] = a
+ b
;
1235 (*coefs
)[1][i
] = a
- b
;
1239 for(ch
= 0; ch
< s
->nb_channels
; ++ch
)
1241 /* BLOCK_MAX_SIZE is 2048 (samples) and MAX_CHANNELS is 2. */
1242 static uint32_t scratch_buf
[BLOCK_MAX_SIZE
* MAX_CHANNELS
] IBSS_ATTR MEM_ALIGN_ATTR
;
1243 if (s
->channel_coded
[ch
])
1247 n4
= s
->block_len
>>1;
1249 ff_imdct_calc((s
->frame_len_bits
- bsize
+ 1),
1253 /* add in the frame */
1254 index
= (s
->frame_len
/ 2) + s
->block_pos
- n4
;
1255 wma_window(s
, scratch_buf
, &((*s
->frame_out
)[ch
][index
]));
1259 /* specific fast case for ms-stereo : add to second
1260 channel if it is not coded */
1261 if (s
->ms_stereo
&& !s
->channel_coded
[1])
1263 wma_window(s
, scratch_buf
, &((*s
->frame_out
)[1][index
]));
1268 /* update block number */
1270 s
->block_pos
+= s
->block_len
;
1271 if (s
->block_pos
>= s
->frame_len
)
1281 /* decode a frame of frame_len samples */
1282 static int wma_decode_frame(WMADecodeContext
*s
)
1286 /* read each block */
1293 ret
= wma_decode_block(s
);
1297 DEBUGF("wma_decode_block failed with code %d\n", ret
);
1309 /* Initialise the superframe decoding */
1311 int wma_decode_superframe_init(WMADecodeContext
* s
,
1312 const uint8_t *buf
, /*input*/
1317 s
->last_superframe_len
= 0;
1321 s
->current_frame
= 0;
1323 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1325 if (s
->use_bit_reservoir
)
1327 /* read super frame header */
1328 skip_bits(&s
->gb
, 4); /* super frame index */
1329 s
->nb_frames
= get_bits(&s
->gb
, 4);
1331 if (s
->last_superframe_len
== 0)
1333 else if (s
->nb_frames
== 0)
1336 s
->bit_offset
= get_bits(&s
->gb
, s
->byte_offset_bits
+ 3);
1345 /* Decode a single frame in the current superframe - return -1 if
1346 there was a decoding error, or the number of samples decoded.
1349 int wma_decode_superframe_frame(WMADecodeContext
* s
,
1350 const uint8_t *buf
, /*input*/
1357 for(ch
= 0; ch
< s
->nb_channels
; ch
++)
1358 memmove(&((*s
->frame_out
)[ch
][0]),
1359 &((*s
->frame_out
)[ch
][s
->frame_len
]),
1360 s
->frame_len
* sizeof(fixed32
));
1362 if ((s
->use_bit_reservoir
) && (s
->current_frame
== 0))
1364 if (s
->last_superframe_len
> 0)
1366 /* add s->bit_offset bits to last frame */
1367 if ((s
->last_superframe_len
+ ((s
->bit_offset
+ 7) >> 3)) >
1368 MAX_CODED_SUPERFRAME_SIZE
)
1370 DEBUGF("superframe size too large error\n");
1373 q
= s
->last_superframe
+ s
->last_superframe_len
;
1374 len
= s
->bit_offset
;
1377 *q
++ = (get_bits
)(&s
->gb
, 8);
1382 *q
++ = (get_bits
)(&s
->gb
, len
) << (8 - len
);
1385 /* XXX: s->bit_offset bits into last frame */
1386 init_get_bits(&s
->gb
, s
->last_superframe
, MAX_CODED_SUPERFRAME_SIZE
*8);
1387 /* skip unused bits */
1388 if (s
->last_bitoffset
> 0)
1389 skip_bits(&s
->gb
, s
->last_bitoffset
);
1391 /* this frame is stored in the last superframe and in the
1393 if (wma_decode_frame(s
) < 0)
1400 /* read each frame starting from s->bit_offset */
1401 pos
= s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3;
1402 init_get_bits(&s
->gb
, buf
+ (pos
>> 3), (MAX_CODED_SUPERFRAME_SIZE
- (pos
>> 3))*8);
1405 skip_bits(&s
->gb
, len
);
1407 s
->reset_block_lengths
= 1;
1410 /* If we haven't decoded a frame yet, do it now */
1413 if (wma_decode_frame(s
) < 0)
1421 if ((s
->use_bit_reservoir
) && (s
->current_frame
== s
->nb_frames
))
1423 /* we copy the end of the frame in the last frame buffer */
1424 pos
= get_bits_count(&s
->gb
) + ((s
->bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3) & ~7);
1425 s
->last_bitoffset
= pos
& 7;
1427 len
= buf_size
- pos
;
1428 if (len
> MAX_CODED_SUPERFRAME_SIZE
|| len
< 0)
1430 DEBUGF("superframe size too large error after decoding\n");
1433 s
->last_superframe_len
= len
;
1434 memcpy(s
->last_superframe
, buf
+ pos
, len
);
1437 return s
->frame_len
;
1440 /* when error, we reset the bit reservoir */
1442 s
->last_superframe_len
= 0;