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
39
40 #undef NDEBUG
41 #include <assert.h>
42
44 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
45
46 #define HGAINVLCBITS 9
47 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
48
50
51 #ifdef TRACE
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
68
70 {
72 int i, flags2;
74
78 }
79
81
82 /* extract flag infos */
83 flags2 = 0;
89 }
90
94
97 av_log(avctx,
AV_LOG_WARNING,
"Disabling use_variable_block_len, if this fails contact the ffmpeg developers and send us the file\n");
99 }
100 }
101
103 return -1;
104
105 /* init MDCT */
108
113 }
114
119 } else {
121 }
122
124
127
128 return 0;
129 }
130
131 /**
132 * compute x^-0.25 with an exponent and mantissa table. We use linear
133 * interpolation to reduce the mantissa table size at a small speed
134 * expense (linear interpolation approximately doubles the number of
135 * bits of precision).
136 */
138 {
139 union {
140 float f;
141 unsigned int v;
145
146 u.f = x;
147 e = u.v >> 23;
149 /* build interpolation scale: 1 <= t < 2. */
150 t.v = ((u.v <<
LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
154 }
155
157 {
160
161 wdel =
M_PI / frame_len;
162 for(i=0;i<frame_len;i++)
164
165 /* tables for x^-0.25 computation */
166 for(i=0;i<256;i++) {
167 e = i - 126;
169 }
170
171 /* NOTE: these two tables are needed to avoid two operations in
172 pow_m1_4 */
173 b = 1.0;
177 a = pow(a, -0.25);
181 }
182 }
183
184 /**
185 * NOTE: We use the same code as Vorbis here
186 * @todo optimize it further with SSE/3Dnow
187 */
189 float *out, float *val_max_ptr,
190 int n, float *lsp)
191 {
192 int i, j;
193 float p, q, w, v, val_max;
194
195 val_max = 0;
196 for(i=0;i<n;i++) {
197 p = 0.5f;
198 q = 0.5f;
201 q *= w - lsp[j - 1];
202 p *= w - lsp[j];
203 }
204 p *= p * (2.0f - w);
205 q *= q * (2.0f + w);
206 v = p + q;
208 if (v > val_max)
209 val_max = v;
210 out[i] = v;
211 }
212 *val_max_ptr = val_max;
213 }
214
215 /**
216 * decode exponents coded with LSP coefficients (same idea as Vorbis)
217 */
219 {
221 int val, i;
222
224 if (i == 0 || i >= 8)
226 else
229 }
230
233 }
234
235 /** pow(10, i / 16.0) for i in -60..95 */
237 1.7782794100389e-04, 2.0535250264571e-04,
238 2.3713737056617e-04, 2.7384196342644e-04,
239 3.1622776601684e-04, 3.6517412725484e-04,
240 4.2169650342858e-04, 4.8696752516586e-04,
241 5.6234132519035e-04, 6.4938163157621e-04,
242 7.4989420933246e-04, 8.6596432336006e-04,
243 1.0000000000000e-03, 1.1547819846895e-03,
244 1.3335214321633e-03, 1.5399265260595e-03,
245 1.7782794100389e-03, 2.0535250264571e-03,
246 2.3713737056617e-03, 2.7384196342644e-03,
247 3.1622776601684e-03, 3.6517412725484e-03,
248 4.2169650342858e-03, 4.8696752516586e-03,
249 5.6234132519035e-03, 6.4938163157621e-03,
250 7.4989420933246e-03, 8.6596432336006e-03,
251 1.0000000000000e-02, 1.1547819846895e-02,
252 1.3335214321633e-02, 1.5399265260595e-02,
253 1.7782794100389e-02, 2.0535250264571e-02,
254 2.3713737056617e-02, 2.7384196342644e-02,
255 3.1622776601684e-02, 3.6517412725484e-02,
256 4.2169650342858e-02, 4.8696752516586e-02,
257 5.6234132519035e-02, 6.4938163157621e-02,
258 7.4989420933246e-02, 8.6596432336007e-02,
259 1.0000000000000e-01, 1.1547819846895e-01,
260 1.3335214321633e-01, 1.5399265260595e-01,
261 1.7782794100389e-01, 2.0535250264571e-01,
262 2.3713737056617e-01, 2.7384196342644e-01,
263 3.1622776601684e-01, 3.6517412725484e-01,
264 4.2169650342858e-01, 4.8696752516586e-01,
265 5.6234132519035e-01, 6.4938163157621e-01,
266 7.4989420933246e-01, 8.6596432336007e-01,
267 1.0000000000000e+00, 1.1547819846895e+00,
268 1.3335214321633e+00, 1.5399265260595e+00,
269 1.7782794100389e+00, 2.0535250264571e+00,
270 2.3713737056617e+00, 2.7384196342644e+00,
271 3.1622776601684e+00, 3.6517412725484e+00,
272 4.2169650342858e+00, 4.8696752516586e+00,
273 5.6234132519035e+00, 6.4938163157621e+00,
274 7.4989420933246e+00, 8.6596432336007e+00,
275 1.0000000000000e+01, 1.1547819846895e+01,
276 1.3335214321633e+01, 1.5399265260595e+01,
277 1.7782794100389e+01, 2.0535250264571e+01,
278 2.3713737056617e+01, 2.7384196342644e+01,
279 3.1622776601684e+01, 3.6517412725484e+01,
280 4.2169650342858e+01, 4.8696752516586e+01,
281 5.6234132519035e+01, 6.4938163157621e+01,
282 7.4989420933246e+01, 8.6596432336007e+01,
283 1.0000000000000e+02, 1.1547819846895e+02,
284 1.3335214321633e+02, 1.5399265260595e+02,
285 1.7782794100389e+02, 2.0535250264571e+02,
286 2.3713737056617e+02, 2.7384196342644e+02,
287 3.1622776601684e+02, 3.6517412725484e+02,
288 4.2169650342858e+02, 4.8696752516586e+02,
289 5.6234132519035e+02, 6.4938163157621e+02,
290 7.4989420933246e+02, 8.6596432336007e+02,
291 1.0000000000000e+03, 1.1547819846895e+03,
292 1.3335214321633e+03, 1.5399265260595e+03,
293 1.7782794100389e+03, 2.0535250264571e+03,
294 2.3713737056617e+03, 2.7384196342644e+03,
295 3.1622776601684e+03, 3.6517412725484e+03,
296 4.2169650342858e+03, 4.8696752516586e+03,
297 5.6234132519035e+03, 6.4938163157621e+03,
298 7.4989420933246e+03, 8.6596432336007e+03,
299 1.0000000000000e+04, 1.1547819846895e+04,
300 1.3335214321633e+04, 1.5399265260595e+04,
301 1.7782794100389e+04, 2.0535250264571e+04,
302 2.3713737056617e+04, 2.7384196342644e+04,
303 3.1622776601684e+04, 3.6517412725484e+04,
304 4.2169650342858e+04, 4.8696752516586e+04,
305 5.6234132519035e+04, 6.4938163157621e+04,
306 7.4989420933246e+04, 8.6596432336007e+04,
307 1.0000000000000e+05, 1.1547819846895e+05,
308 1.3335214321633e+05, 1.5399265260595e+05,
309 1.7782794100389e+05, 2.0535250264571e+05,
310 2.3713737056617e+05, 2.7384196342644e+05,
311 3.1622776601684e+05, 3.6517412725484e+05,
312 4.2169650342858e+05, 4.8696752516586e+05,
313 5.6234132519035e+05, 6.4938163157621e+05,
314 7.4989420933246e+05, 8.6596432336007e+05,
315 };
316
317 /**
318 * decode exponents coded with VLC codes
319 */
321 {
322 int last_exp, n, code;
323 const uint16_t *ptr;
324 float v, max_scale;
325 uint32_t *q, *q_end, iv;
326 const float *ptab = pow_tab + 60;
327 const uint32_t *iptab = (const uint32_t*)ptab;
328
332 max_scale = 0;
335 v = ptab[last_exp];
336 iv = iptab[last_exp];
337 max_scale = v;
338 n = *ptr++;
339 switch (n & 3) do {
340 case 0: *q++ = iv;
341 case 3: *q++ = iv;
342 case 2: *q++ = iv;
343 case 1: *q++ = iv;
344 } while ((n -= 4) > 0);
345 }else
346 last_exp = 36;
347
348 while (q < q_end) {
350 if (code < 0){
352 return -1;
353 }
354 /* NOTE: this offset is the same as MPEG4 AAC ! */
355 last_exp += code - 60;
358 last_exp);
359 return -1;
360 }
361 v = ptab[last_exp];
362 iv = iptab[last_exp];
363 if (v > max_scale)
364 max_scale = v;
365 n = *ptr++;
366 switch (n & 3) do {
367 case 0: *q++ = iv;
368 case 3: *q++ = iv;
369 case 2: *q++ = iv;
370 case 1: *q++ = iv;
371 } while ((n -= 4) > 0);
372 }
374 return 0;
375 }
376
377
378 /**
379 * Apply MDCT window and add into output.
380 *
381 * We ensure that when the windows overlap their squared sum
382 * is always 1 (MDCT reconstruction rule).
383 */
385 {
387 int block_len, bsize, n;
388
389 /* left part */
393
395 out, block_len);
396
397 } else {
401
403 out+n, block_len);
404
405 memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
406 }
407
410
411 /* right part */
415
417
418 } else {
422
423 memcpy(out, in, n*sizeof(float));
424
426
427 memset(out+n+block_len, 0, n*sizeof(float));
428 }
429 }
430
431
432 /**
433 * @return 0 if OK. 1 if last block of frame. return -1 if
434 * unrecorrable error.
435 */
437 {
438 int n, v,
a, ch, bsize;
439 int coef_nb_bits, total_gain;
441 float mdct_norm;
443
444 #ifdef TRACE
446 #endif
447
448 /* compute current block length */
451
457 return -1;
458 }
463 return -1;
464 }
466 } else {
467 /* update block lengths */
470 }
474 return -1;
475 }
477 } else {
478 /* fixed block len */
482 }
483
486 return -1;
487 }
488
489 /* now check if the block length is coherent with the frame length */
493 return -1;
494 }
495
498 }
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(;;) {
519 if (a != 127)
520 break;
521 }
522
524
525 /* compute number of coefficients */
528 nb_coefs[ch] = n;
529
530 /* complex coding */
532
537 for(i=0;i<n;i++) {
540 /* if noise coding, the coefficients are not transmitted */
541 if (a)
543 }
544 }
545 }
548 int i, n, val, code;
549
551 val = (int)0x80000000;
552 for(i=0;i<n;i++) {
554 if (val == (int)0x80000000) {
556 } else {
558 if (code < 0){
560 return -1;
561 }
562 val += code - 18;
563 }
565 }
566 }
567 }
568 }
569 }
570
571 /* exponents can be reused in short blocks. */
578 return -1;
579 } else {
581 }
583 }
584 }
585 }
586
587 /* parse spectral coefficients : just RLE encoding */
590 int tindex;
592
593 /* special VLC tables are used for ms stereo because
594 there is potentially less energy there */
599 0, ptr, 0, nb_coefs[ch],
601 }
604 }
605 }
606
607 /* normalize */
608 {
610 mdct_norm = 1.0 / (float)n4;
612 mdct_norm *= sqrt(n4);
613 }
614 }
615
616 /* finally compute the MDCT coefficients */
620 float *coefs, *exponents,
mult, mult1,
noise;
621 int i, j, n, n1, last_high_band, esize;
623
627 mult = pow(10, total_gain * 0.05) / s->
max_exponent[ch];
628 mult *= mdct_norm;
629 coefs = s->
coefs[ch];
632 /* very low freqs : noise */
635 exponents[i<<bsize>>esize] * mult1;
637 }
638
640
641 /* compute power of high bands */
644 last_high_band = 0; /* avoid warning */
645 for(j=0;j<n1;j++) {
649 float e2, v;
650 e2 = 0;
651 for(i = 0;i < n; i++) {
652 v = exponents[i<<bsize>>esize];
653 e2 += v * v;
654 }
655 exp_power[j] = e2 / n;
656 last_high_band = j;
657 tprintf(s->
avctx,
"%d: power=%f (%d)\n", j, exp_power[j], n);
658 }
659 exponents += n<<bsize>>esize;
660 }
661
662 /* main freqs and high freqs */
664 for(j=-1;j<n1;j++) {
665 if (j < 0) {
668 } else {
671 }
673 /* use noise with specified power */
674 mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
675 /* XXX: use a table */
678 mult1 *= mdct_norm;
679 for(i = 0;i < n; i++) {
682 *coefs++ = noise *
683 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;
712 }
714 for(i = 0;i < n; i++)
715 *coefs++ = 0.0;
716 }
717 }
718 }
719
720 #ifdef TRACE
725 }
726 }
727 #endif
728
730 /* nominal case for ms stereo: we do it before mdct */
731 /* no need to optimize this case because it should almost
732 never happen */
737 }
738
740 }
741
742 next:
744
747
753
754 /* multiply by the window and add in the frame */
757 }
758
759 /* update block number */
763 return 1;
764 else
765 return 0;
766 }
767
768 /* decode a frame of frame_len samples */
770 int samples_offset)
771 {
772 int ret, ch;
773
774 #ifdef TRACE
776 #endif
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, s->
frame_len);
799 #endif
800 }
801
802 return 0;
803 }
804
806 int *got_frame_ptr,
AVPacket *avpkt)
807 {
809 int buf_size = avpkt->
size;
811 int nb_frames, bit_offset, i, pos,
len, ret;
814 int samples_offset;
815
816 tprintf(avctx,
"***decode_superframe:\n");
817
818 if(buf_size==0){
820 return 0;
821 }
822 if (buf_size < avctx->block_align) {
824 "Input packet size too small (%d < %d)\n",
827 }
830
832
834 /* read super frame header */
837 } else {
838 nb_frames = 1;
839 }
840
841 /* get output buffer */
845 return ret;
846 }
848 samples_offset = 0;
849
854 "Invalid last frame bit offset %d > buf size %d (%d)\n",
856 goto fail;
857 }
858
860 /* add bit_offset bits to last frame */
863 goto fail;
865 len = bit_offset;
866 while (len > 7) {
868 len -= 8;
869 }
870 if (len > 0) {
872 }
874
875 /* XXX: bit_offset bits into last frame */
877 /* skip unused bits */
880 /* this frame is stored in the last superframe and in the
881 current one */
883 goto fail;
885 nb_frames--;
886 }
887
888 /* read each frame starting from bit_offset */
893 len = pos & 7;
894 if (len > 0)
896
898 for(i=0;i<nb_frames;i++) {
900 goto fail;
902 }
903
904 /* we copy the end of the frame in the last frame buffer */
907 pos >>= 3;
908 len = buf_size - pos;
911 goto fail;
912 }
915 } else {
916 /* single frame decode */
918 goto fail;
920 }
921
922 av_dlog(s->
avctx,
"%d %d %d %d outbytes:%td eaten:%d\n",
924 (int8_t *)samples - (int8_t *)data, avctx->
block_align);
925
926 *got_frame_ptr = 1;
928
929 return buf_size;
930 fail:
931 /* when error, we reset the bit reservoir */
933 return -1;
934 }
935
937 {
939
942 }
943
944 #if CONFIG_WMAV1_DECODER
958 };
959 #endif
960 #if CONFIG_WMAV2_DECODER
974 };
975 #endif