1 /*
2 * WMA compatible decoder
3 * Copyright (c) 2002 The FFmpeg Project
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * WMA compatible decoder.
25 * This decoder handles Microsoft Windows Media Audio data, versions 1 & 2.
26 * WMA v1 is identified by audio format 0x160 in Microsoft media files
27 * (ASF/AVI/WAV). WMA v2 is identified by audio format 0x161.
28 *
29 * To use this decoder, a calling application must supply the extra data
30 * bytes provided with the WMA data. These are the extra, codec-specific
31 * bytes at the end of a WAVEFORMATEX data structure. Transmit these bytes
32 * to the decoder using the extradata[_size] fields in AVCodecContext. There
33 * should be 4 extra bytes for v1 data and 6 extra bytes for v2 data.
34 */
35
37
41
43 #define EXPMAX ((19 + EXPVLCBITS - 1) / EXPVLCBITS)
44
45 #define HGAINVLCBITS 9
46 #define HGAINMAX ((13 + HGAINVLCBITS - 1) / HGAINVLCBITS)
47
49
50 #ifdef TRACE
52 int prec,
const float *
tab,
int n)
53 {
54 int i;
55
57 for (i = 0; i <
n; i++) {
58 if ((i & 7) == 0)
61 if ((i & 7) == 7)
63 }
64 if ((i & 7) != 0)
66 }
67 #endif /* TRACE */
68
70 {
72 int i, flags2;
74
78 }
79
81
82 /* extract flag infos */
83 flags2 = 0;
86 flags2 =
AV_RL16(extradata + 2);
88 flags2 =
AV_RL16(extradata + 4);
89
93
96 av_log(avctx,
AV_LOG_WARNING,
"Disabling use_variable_block_len, if this fails contact the ffmpeg developers and send us the file\n");
98 }
99 }
100
103
105 return -1;
106
107 /* init MDCT */
110
115 }
116
121 else
123
125
126 return 0;
127 }
128
129 /**
130 * compute x^-0.25 with an exponent and mantissa table. We use linear
131 * interpolation to reduce the mantissa table size at a small speed
132 * expense (linear interpolation approximately doubles the number of
133 * bits of precision).
134 */
136 {
137 union {
138 float f;
143
147 /* build interpolation scale: 1 <= t < 2. */
148 t.v = ((
u.v <<
LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
152 }
153
155 {
158
159 wdel =
M_PI / frame_len;
160 for (i = 0; i < frame_len; i++)
162
163 /* tables for x^-0.25 computation */
164 for (i = 0; i < 256; i++) {
165 e = i - 126;
167 }
168
169 /* NOTE: these two tables are needed to avoid two operations in
170 * pow_m1_4 */
171 b = 1.0;
175 a = pow(a, -0.25);
179 }
180 }
181
182 /**
183 * NOTE: We use the same code as Vorbis here
184 * @todo optimize it further with SSE/3Dnow
185 */
187 int n, float *lsp)
188 {
189 int i, j;
190 float p, q, w,
v, val_max;
191
192 val_max = 0;
193 for (i = 0; i <
n; i++) {
194 p = 0.5f;
195 q = 0.5f;
198 q *= w - lsp[j - 1];
199 p *= w - lsp[j];
200 }
201 p *= p * (2.0f - w);
202 q *= q * (2.0f + w);
203 v = p + q;
205 if (v > val_max)
208 }
209 *val_max_ptr = val_max;
210 }
211
212 /**
213 * decode exponents coded with LSP coefficients (same idea as Vorbis)
214 */
216 {
219
221 if (i == 0 || i >= 8)
223 else
226 }
227
230 }
231
232 /** pow(10, i / 16.0) for i in -60..95 */
234 1.7782794100389e-04, 2.0535250264571e-04,
235 2.3713737056617e-04, 2.7384196342644e-04,
236 3.1622776601684e-04, 3.6517412725484e-04,
237 4.2169650342858e-04, 4.8696752516586e-04,
238 5.6234132519035e-04, 6.4938163157621e-04,
239 7.4989420933246e-04, 8.6596432336006e-04,
240 1.0000000000000e-03, 1.1547819846895e-03,
241 1.3335214321633e-03, 1.5399265260595e-03,
242 1.7782794100389e-03, 2.0535250264571e-03,
243 2.3713737056617e-03, 2.7384196342644e-03,
244 3.1622776601684e-03, 3.6517412725484e-03,
245 4.2169650342858e-03, 4.8696752516586e-03,
246 5.6234132519035e-03, 6.4938163157621e-03,
247 7.4989420933246e-03, 8.6596432336006e-03,
248 1.0000000000000e-02, 1.1547819846895e-02,
249 1.3335214321633e-02, 1.5399265260595e-02,
250 1.7782794100389e-02, 2.0535250264571e-02,
251 2.3713737056617e-02, 2.7384196342644e-02,
252 3.1622776601684e-02, 3.6517412725484e-02,
253 4.2169650342858e-02, 4.8696752516586e-02,
254 5.6234132519035e-02, 6.4938163157621e-02,
255 7.4989420933246e-02, 8.6596432336007e-02,
256 1.0000000000000e-01, 1.1547819846895e-01,
257 1.3335214321633e-01, 1.5399265260595e-01,
258 1.7782794100389e-01, 2.0535250264571e-01,
259 2.3713737056617e-01, 2.7384196342644e-01,
260 3.1622776601684e-01, 3.6517412725484e-01,
261 4.2169650342858e-01, 4.8696752516586e-01,
262 5.6234132519035e-01, 6.4938163157621e-01,
263 7.4989420933246e-01, 8.6596432336007e-01,
264 1.0000000000000e+00, 1.1547819846895e+00,
265 1.3335214321633e+00, 1.5399265260595e+00,
266 1.7782794100389e+00, 2.0535250264571e+00,
267 2.3713737056617e+00, 2.7384196342644e+00,
268 3.1622776601684e+00, 3.6517412725484e+00,
269 4.2169650342858e+00, 4.8696752516586e+00,
270 5.6234132519035e+00, 6.4938163157621e+00,
271 7.4989420933246e+00, 8.6596432336007e+00,
272 1.0000000000000e+01, 1.1547819846895e+01,
273 1.3335214321633e+01, 1.5399265260595e+01,
274 1.7782794100389e+01, 2.0535250264571e+01,
275 2.3713737056617e+01, 2.7384196342644e+01,
276 3.1622776601684e+01, 3.6517412725484e+01,
277 4.2169650342858e+01, 4.8696752516586e+01,
278 5.6234132519035e+01, 6.4938163157621e+01,
279 7.4989420933246e+01, 8.6596432336007e+01,
280 1.0000000000000e+02, 1.1547819846895e+02,
281 1.3335214321633e+02, 1.5399265260595e+02,
282 1.7782794100389e+02, 2.0535250264571e+02,
283 2.3713737056617e+02, 2.7384196342644e+02,
284 3.1622776601684e+02, 3.6517412725484e+02,
285 4.2169650342858e+02, 4.8696752516586e+02,
286 5.6234132519035e+02, 6.4938163157621e+02,
287 7.4989420933246e+02, 8.6596432336007e+02,
288 1.0000000000000e+03, 1.1547819846895e+03,
289 1.3335214321633e+03, 1.5399265260595e+03,
290 1.7782794100389e+03, 2.0535250264571e+03,
291 2.3713737056617e+03, 2.7384196342644e+03,
292 3.1622776601684e+03, 3.6517412725484e+03,
293 4.2169650342858e+03, 4.8696752516586e+03,
294 5.6234132519035e+03, 6.4938163157621e+03,
295 7.4989420933246e+03, 8.6596432336007e+03,
296 1.0000000000000e+04, 1.1547819846895e+04,
297 1.3335214321633e+04, 1.5399265260595e+04,
298 1.7782794100389e+04, 2.0535250264571e+04,
299 2.3713737056617e+04, 2.7384196342644e+04,
300 3.1622776601684e+04, 3.6517412725484e+04,
301 4.2169650342858e+04, 4.8696752516586e+04,
302 5.6234132519035e+04, 6.4938163157621e+04,
303 7.4989420933246e+04, 8.6596432336007e+04,
304 1.0000000000000e+05, 1.1547819846895e+05,
305 1.3335214321633e+05, 1.5399265260595e+05,
306 1.7782794100389e+05, 2.0535250264571e+05,
307 2.3713737056617e+05, 2.7384196342644e+05,
308 3.1622776601684e+05, 3.6517412725484e+05,
309 4.2169650342858e+05, 4.8696752516586e+05,
310 5.6234132519035e+05, 6.4938163157621e+05,
311 7.4989420933246e+05, 8.6596432336007e+05,
312 };
313
314 /**
315 * decode exponents coded with VLC codes
316 */
318 {
319 int last_exp,
n, code;
320 const uint16_t *ptr;
322 uint32_t *q, *q_end, iv;
323 const float *ptab = pow_tab + 60;
324 const uint32_t *iptab = (const uint32_t *) ptab;
325
329 max_scale = 0;
332 v = ptab[last_exp];
333 iv = iptab[last_exp];
335 n = *ptr++;
336 switch (n & 3) do {
337 case 0: *q++ = iv;
338 case 3: *q++ = iv;
339 case 2: *q++ = iv;
340 case 1: *q++ = iv;
341 } while ((n -= 4) > 0);
342 } else
343 last_exp = 36;
344
345 while (q < q_end) {
347 if (code < 0) {
349 return -1;
350 }
351 /* NOTE: this offset is the same as MPEG4 AAC ! */
352 last_exp += code - 60;
355 last_exp);
356 return -1;
357 }
358 v = ptab[last_exp];
359 iv = iptab[last_exp];
360 if (v > max_scale)
362 n = *ptr++;
363 switch (n & 3) do {
364 case 0: *q++ = iv;
365 case 3: *q++ = iv;
366 case 2: *q++ = iv;
367 case 1: *q++ = iv;
368 } while ((n -= 4) > 0);
369 }
371 return 0;
372 }
373
374 /**
375 * Apply MDCT window and add into output.
376 *
377 * We ensure that when the windows overlap their squared sum
378 * is always 1 (MDCT reconstruction rule).
379 */
381 {
383 int block_len, bsize,
n;
384
385 /* left part */
389
391 out, block_len);
392 } else {
396
398 out + n, block_len);
399
400 memcpy(out + n + block_len, in + n + block_len, n * sizeof(float));
401 }
402
405
406 /* right part */
410
412 } else {
416
417 memcpy(out, in, n * sizeof(float));
418
420 block_len);
421
422 memset(out + n + block_len, 0, n * sizeof(float));
423 }
424 }
425
426 /**
427 * @return 0 if OK. 1 if last block of frame. return -1 if
428 * unrecorrable error.
429 */
431 {
432 int n,
v,
a, ch, bsize;
433 int coef_nb_bits, total_gain;
435 float mdct_norm;
437
438 #ifdef TRACE
441 #endif /* TRACE */
442
443 /* compute current block length */
446
452 "prev_block_len_bits %d out of range\n",
454 return -1;
455 }
460 "block_len_bits %d out of range\n",
462 return -1;
463 }
465 } else {
466 /* update block lengths */
469 }
473 "next_block_len_bits %d out of range\n",
475 return -1;
476 }
478 } else {
479 /* fixed block len */
483 }
484
487 return -1;
488 }
489
490 /* now check if the block length is coherent with the frame length */
494 return -1;
495 }
496
499 v = 0;
504 }
505
507
508 /* if no channel coded, no need to go further */
509 /* XXX: fix potential framing problems */
510 if (!v)
511 goto next;
512
513 /* read total gain and extract corresponding number of bits for
514 * coef escape coding */
515 total_gain = 1;
516 for (;;) {
520 }
523 if (a != 127)
524 break;
525 }
526
528
529 /* compute number of coefficients */
532 nb_coefs[ch] = n;
533
534 /* complex coding */
540 for (i = 0; i <
n; i++) {
543 /* if noise coding, the coefficients are not transmitted */
544 if (a)
546 }
547 }
548 }
552
554 val = (int) 0x80000000;
555 for (i = 0; i <
n; i++) {
557 if (val == (int) 0x80000000) {
559 } else {
562 if (code < 0) {
564 "hgain vlc invalid\n");
565 return -1;
566 }
567 val += code - 18;
568 }
570 }
571 }
572 }
573 }
574 }
575
576 /* exponents can be reused in short blocks. */
582 return -1;
583 } else {
585 }
587 }
588 }
589 }
590
591 /* parse spectral coefficients : just RLE encoding */
594 int tindex;
596
597 /* special VLC tables are used for ms stereo because
598 * there is potentially less energy there */
603 0, ptr, 0, nb_coefs[ch],
605 }
608 }
609
610 /* normalize */
611 {
613 mdct_norm = 1.0 / (float) n4;
615 mdct_norm *= sqrt(n4);
616 }
617
618 /* finally compute the MDCT coefficients */
622 float *coefs, *exponents,
mult, mult1,
noise;
623 int i, j,
n, n1, last_high_band, esize;
625
629 mult = pow(10, total_gain * 0.05) / s->
max_exponent[ch];
630 mult *= mdct_norm;
631 coefs = s->
coefs[ch];
634 /* very low freqs : noise */
637 exponents[i << bsize >> esize] * mult1;
640 }
641
643
644 /* compute power of high bands */
647 last_high_band = 0; /* avoid warning */
648 for (j = 0; j < n1; j++) {
653 e2 = 0;
654 for (i = 0; i <
n; i++) {
655 v = exponents[i << bsize >> esize];
657 }
658 exp_power[j] = e2 /
n;
659 last_high_band = j;
660 tprintf(s->
avctx,
"%d: power=%f (%d)\n", j, exp_power[j], n);
661 }
662 exponents += n << bsize >> esize;
663 }
664
665 /* main freqs and high freqs */
667 for (j = -1; j < n1; j++) {
668 if (j < 0)
670 else
674 /* use noise with specified power */
675 mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
676 /* XXX: use a table */
679 mult1 *= mdct_norm;
680 for (i = 0; i <
n; i++) {
683 *coefs++ = noise * exponents[i << bsize >> esize] * mult1;
684 }
685 exponents += n << bsize >> esize;
686 } else {
687 /* coded values + small noise */
688 for (i = 0; i <
n; i++) {
691 *coefs++ = ((*coefs1++) + noise) *
692 exponents[i << bsize >> esize] *
mult;
693 }
694 exponents += n << bsize >> esize;
695 }
696 }
697
698 /* very high freqs : noise */
700 mult1 = mult * exponents[((-1 << bsize)) >> esize];
701 for (i = 0; i <
n; i++) {
704 }
705 } else {
706 /* XXX: optimize more */
708 *coefs++ = 0.0;
709 n = nb_coefs[ch];
710 for (i = 0; i <
n; i++)
711 *coefs++ = coefs1[i] * exponents[i << bsize >> esize] * mult;
713 for (i = 0; i <
n; i++)
714 *coefs++ = 0.0;
715 }
716 }
717 }
718
719 #ifdef TRACE
724 }
725 }
726 #endif /* TRACE */
727
729 /* nominal case for ms stereo: we do it before mdct */
730 /* no need to optimize this case because it should almost
731 * never happen */
736 }
737
739 }
740
741 next:
743
746
752
753 /* multiply by the window and add in the frame */
756 }
757
758 /* update block number */
762 return 1;
763 else
764 return 0;
765 }
766
767 /* decode a frame of frame_len samples */
769 int samples_offset)
770 {
772
773 #ifdef TRACE
776 #endif /* TRACE */
777
778 /* read each block */
781 for (;;) {
783 if (ret < 0)
784 return -1;
785 if (ret)
786 break;
787 }
788
790 /* copy current block to output */
791 memcpy(samples[ch] + samples_offset, s->
frame_out[ch],
793 /* prepare for next block */
796
797 #ifdef TRACE
798 dump_floats(s, "samples", 6, samples[ch] + samples_offset,
800 #endif /* TRACE */
801 }
802
803 return 0;
804 }
805
807 int *got_frame_ptr,
AVPacket *avpkt)
808 {
811 int buf_size = avpkt->
size;
813 int nb_frames, bit_offset, i, pos,
len,
ret;
815 float **samples;
816 int samples_offset;
817
818 tprintf(avctx,
"***decode_superframe:\n");
819
820 if (buf_size == 0) {
822 return 0;
823 }
824 if (buf_size < avctx->block_align) {
826 "Input packet size too small (%d < %d)\n",
829 }
832
834
836 /* read super frame header */
839 if (nb_frames <= 0) {
842 "nb_frames is %d bits left %d\n",
844 if (is_error)
846
849 goto fail;
850
852 len = buf_size - 1;
853 while (len > 0) {
855 len --;
856 }
858
860 // s->reset_block_lengths = 1; //XXX is this needed ?
861 *got_frame_ptr = 0;
862 return buf_size;
863 }
864 } else
865 nb_frames = 1;
866
867 /* get output buffer */
872 samples_offset = 0;
873
878 "Invalid last frame bit offset %d > buf size %d (%d)\n",
880 goto fail;
881 }
882
884 /* add bit_offset bits to last frame */
887 goto fail;
889 len = bit_offset;
890 while (len > 7) {
892 len -= 8;
893 }
894 if (len > 0)
897
898 /* XXX: bit_offset bits into last frame */
901 /* skip unused bits */
904 /* this frame is stored in the last superframe and in the
905 * current one */
907 goto fail;
909 nb_frames--;
910 }
911
912 /* read each frame starting from bit_offset */
917 len = pos & 7;
918 if (len > 0)
920
922 for (i = 0; i < nb_frames; i++) {
924 goto fail;
926 }
927
928 /* we copy the end of the frame in the last frame buffer */
932 pos >>= 3;
933 len = buf_size - pos;
936 goto fail;
937 }
940 } else {
941 /* single frame decode */
943 goto fail;
945 }
946
949 (int8_t *) samples - (int8_t *) data, avctx->
block_align);
950
951 *got_frame_ptr = 1;
952
953 return buf_size;
954
955 fail:
956 /* when error, we reset the bit reservoir */
958 return -1;
959 }
960
962 {
964
967 }
968
969 #if CONFIG_WMAV1_DECODER
983 };
984 #endif
985 #if CONFIG_WMAV2_DECODER
999 };
1000 #endif