eb7fe7c25e9f9576ec3d89e2b5dbca98c8a92348
[libav.git] / libavcodec / wmadec.c
1 /*
2 * WMA compatible decoder
3 * Copyright (c) 2002 The Libav Project
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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
36 #include "avcodec.h"
37 #include "wma.h"
38
39 #undef NDEBUG
40 #include <assert.h>
41
42 #define EXPVLCBITS 8
43 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
44
45 #define HGAINVLCBITS 9
46 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
47
48 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len);
49
50 #ifdef TRACE
51 static void dump_floats(WMACodecContext *s, const char *name, int prec, const float *tab, int n)
52 {
53 int i;
54
55 tprintf(s->avctx, "%s[%d]:\n", name, n);
56 for(i=0;i<n;i++) {
57 if ((i & 7) == 0)
58 tprintf(s->avctx, "%4d: ", i);
59 tprintf(s->avctx, " %8.*f", prec, tab[i]);
60 if ((i & 7) == 7)
61 tprintf(s->avctx, "\n");
62 }
63 if ((i & 7) != 0)
64 tprintf(s->avctx, "\n");
65 }
66 #endif
67
68 static int wma_decode_init(AVCodecContext * avctx)
69 {
70 WMACodecContext *s = avctx->priv_data;
71 int i, flags2;
72 uint8_t *extradata;
73
74 s->avctx = avctx;
75
76 /* extract flag infos */
77 flags2 = 0;
78 extradata = avctx->extradata;
79 if (avctx->codec->id == AV_CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
80 flags2 = AV_RL16(extradata+2);
81 } else if (avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
82 flags2 = AV_RL16(extradata+4);
83 }
84
85 s->use_exp_vlc = flags2 & 0x0001;
86 s->use_bit_reservoir = flags2 & 0x0002;
87 s->use_variable_block_len = flags2 & 0x0004;
88
89 if(ff_wma_init(avctx, flags2)<0)
90 return -1;
91
92 /* init MDCT */
93 for(i = 0; i < s->nb_block_sizes; i++)
94 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1, 1.0 / 32768.0);
95
96 if (s->use_noise_coding) {
97 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits),
98 ff_wma_hgain_huffbits, 1, 1,
99 ff_wma_hgain_huffcodes, 2, 2, 0);
100 }
101
102 if (s->use_exp_vlc) {
103 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), //FIXME move out of context
104 ff_aac_scalefactor_bits, 1, 1,
105 ff_aac_scalefactor_code, 4, 4, 0);
106 } else {
107 wma_lsp_to_curve_init(s, s->frame_len);
108 }
109
110 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
111
112 avcodec_get_frame_defaults(&s->frame);
113 avctx->coded_frame = &s->frame;
114
115 return 0;
116 }
117
118 /**
119 * compute x^-0.25 with an exponent and mantissa table. We use linear
120 * interpolation to reduce the mantissa table size at a small speed
121 * expense (linear interpolation approximately doubles the number of
122 * bits of precision).
123 */
124 static inline float pow_m1_4(WMACodecContext *s, float x)
125 {
126 union {
127 float f;
128 unsigned int v;
129 } u, t;
130 unsigned int e, m;
131 float a, b;
132
133 u.f = x;
134 e = u.v >> 23;
135 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
136 /* build interpolation scale: 1 <= t < 2. */
137 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
138 a = s->lsp_pow_m_table1[m];
139 b = s->lsp_pow_m_table2[m];
140 return s->lsp_pow_e_table[e] * (a + b * t.f);
141 }
142
143 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
144 {
145 float wdel, a, b;
146 int i, e, m;
147
148 wdel = M_PI / frame_len;
149 for(i=0;i<frame_len;i++)
150 s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
151
152 /* tables for x^-0.25 computation */
153 for(i=0;i<256;i++) {
154 e = i - 126;
155 s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
156 }
157
158 /* NOTE: these two tables are needed to avoid two operations in
159 pow_m1_4 */
160 b = 1.0;
161 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
162 m = (1 << LSP_POW_BITS) + i;
163 a = (float)m * (0.5 / (1 << LSP_POW_BITS));
164 a = pow(a, -0.25);
165 s->lsp_pow_m_table1[i] = 2 * a - b;
166 s->lsp_pow_m_table2[i] = b - a;
167 b = a;
168 }
169 }
170
171 /**
172 * NOTE: We use the same code as Vorbis here
173 * @todo optimize it further with SSE/3Dnow
174 */
175 static void wma_lsp_to_curve(WMACodecContext *s,
176 float *out, float *val_max_ptr,
177 int n, float *lsp)
178 {
179 int i, j;
180 float p, q, w, v, val_max;
181
182 val_max = 0;
183 for(i=0;i<n;i++) {
184 p = 0.5f;
185 q = 0.5f;
186 w = s->lsp_cos_table[i];
187 for(j=1;j<NB_LSP_COEFS;j+=2){
188 q *= w - lsp[j - 1];
189 p *= w - lsp[j];
190 }
191 p *= p * (2.0f - w);
192 q *= q * (2.0f + w);
193 v = p + q;
194 v = pow_m1_4(s, v);
195 if (v > val_max)
196 val_max = v;
197 out[i] = v;
198 }
199 *val_max_ptr = val_max;
200 }
201
202 /**
203 * decode exponents coded with LSP coefficients (same idea as Vorbis)
204 */
205 static void decode_exp_lsp(WMACodecContext *s, int ch)
206 {
207 float lsp_coefs[NB_LSP_COEFS];
208 int val, i;
209
210 for(i = 0; i < NB_LSP_COEFS; i++) {
211 if (i == 0 || i >= 8)
212 val = get_bits(&s->gb, 3);
213 else
214 val = get_bits(&s->gb, 4);
215 lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
216 }
217
218 wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
219 s->block_len, lsp_coefs);
220 }
221
222 /** pow(10, i / 16.0) for i in -60..95 */
223 static const float pow_tab[] = {
224 1.7782794100389e-04, 2.0535250264571e-04,
225 2.3713737056617e-04, 2.7384196342644e-04,
226 3.1622776601684e-04, 3.6517412725484e-04,
227 4.2169650342858e-04, 4.8696752516586e-04,
228 5.6234132519035e-04, 6.4938163157621e-04,
229 7.4989420933246e-04, 8.6596432336006e-04,
230 1.0000000000000e-03, 1.1547819846895e-03,
231 1.3335214321633e-03, 1.5399265260595e-03,
232 1.7782794100389e-03, 2.0535250264571e-03,
233 2.3713737056617e-03, 2.7384196342644e-03,
234 3.1622776601684e-03, 3.6517412725484e-03,
235 4.2169650342858e-03, 4.8696752516586e-03,
236 5.6234132519035e-03, 6.4938163157621e-03,
237 7.4989420933246e-03, 8.6596432336006e-03,
238 1.0000000000000e-02, 1.1547819846895e-02,
239 1.3335214321633e-02, 1.5399265260595e-02,
240 1.7782794100389e-02, 2.0535250264571e-02,
241 2.3713737056617e-02, 2.7384196342644e-02,
242 3.1622776601684e-02, 3.6517412725484e-02,
243 4.2169650342858e-02, 4.8696752516586e-02,
244 5.6234132519035e-02, 6.4938163157621e-02,
245 7.4989420933246e-02, 8.6596432336007e-02,
246 1.0000000000000e-01, 1.1547819846895e-01,
247 1.3335214321633e-01, 1.5399265260595e-01,
248 1.7782794100389e-01, 2.0535250264571e-01,
249 2.3713737056617e-01, 2.7384196342644e-01,
250 3.1622776601684e-01, 3.6517412725484e-01,
251 4.2169650342858e-01, 4.8696752516586e-01,
252 5.6234132519035e-01, 6.4938163157621e-01,
253 7.4989420933246e-01, 8.6596432336007e-01,
254 1.0000000000000e+00, 1.1547819846895e+00,
255 1.3335214321633e+00, 1.5399265260595e+00,
256 1.7782794100389e+00, 2.0535250264571e+00,
257 2.3713737056617e+00, 2.7384196342644e+00,
258 3.1622776601684e+00, 3.6517412725484e+00,
259 4.2169650342858e+00, 4.8696752516586e+00,
260 5.6234132519035e+00, 6.4938163157621e+00,
261 7.4989420933246e+00, 8.6596432336007e+00,
262 1.0000000000000e+01, 1.1547819846895e+01,
263 1.3335214321633e+01, 1.5399265260595e+01,
264 1.7782794100389e+01, 2.0535250264571e+01,
265 2.3713737056617e+01, 2.7384196342644e+01,
266 3.1622776601684e+01, 3.6517412725484e+01,
267 4.2169650342858e+01, 4.8696752516586e+01,
268 5.6234132519035e+01, 6.4938163157621e+01,
269 7.4989420933246e+01, 8.6596432336007e+01,
270 1.0000000000000e+02, 1.1547819846895e+02,
271 1.3335214321633e+02, 1.5399265260595e+02,
272 1.7782794100389e+02, 2.0535250264571e+02,
273 2.3713737056617e+02, 2.7384196342644e+02,
274 3.1622776601684e+02, 3.6517412725484e+02,
275 4.2169650342858e+02, 4.8696752516586e+02,
276 5.6234132519035e+02, 6.4938163157621e+02,
277 7.4989420933246e+02, 8.6596432336007e+02,
278 1.0000000000000e+03, 1.1547819846895e+03,
279 1.3335214321633e+03, 1.5399265260595e+03,
280 1.7782794100389e+03, 2.0535250264571e+03,
281 2.3713737056617e+03, 2.7384196342644e+03,
282 3.1622776601684e+03, 3.6517412725484e+03,
283 4.2169650342858e+03, 4.8696752516586e+03,
284 5.6234132519035e+03, 6.4938163157621e+03,
285 7.4989420933246e+03, 8.6596432336007e+03,
286 1.0000000000000e+04, 1.1547819846895e+04,
287 1.3335214321633e+04, 1.5399265260595e+04,
288 1.7782794100389e+04, 2.0535250264571e+04,
289 2.3713737056617e+04, 2.7384196342644e+04,
290 3.1622776601684e+04, 3.6517412725484e+04,
291 4.2169650342858e+04, 4.8696752516586e+04,
292 5.6234132519035e+04, 6.4938163157621e+04,
293 7.4989420933246e+04, 8.6596432336007e+04,
294 1.0000000000000e+05, 1.1547819846895e+05,
295 1.3335214321633e+05, 1.5399265260595e+05,
296 1.7782794100389e+05, 2.0535250264571e+05,
297 2.3713737056617e+05, 2.7384196342644e+05,
298 3.1622776601684e+05, 3.6517412725484e+05,
299 4.2169650342858e+05, 4.8696752516586e+05,
300 5.6234132519035e+05, 6.4938163157621e+05,
301 7.4989420933246e+05, 8.6596432336007e+05,
302 };
303
304 /**
305 * decode exponents coded with VLC codes
306 */
307 static int decode_exp_vlc(WMACodecContext *s, int ch)
308 {
309 int last_exp, n, code;
310 const uint16_t *ptr;
311 float v, max_scale;
312 uint32_t *q, *q_end, iv;
313 const float *ptab = pow_tab + 60;
314 const uint32_t *iptab = (const uint32_t*)ptab;
315
316 ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
317 q = (uint32_t *)s->exponents[ch];
318 q_end = q + s->block_len;
319 max_scale = 0;
320 if (s->version == 1) {
321 last_exp = get_bits(&s->gb, 5) + 10;
322 v = ptab[last_exp];
323 iv = iptab[last_exp];
324 max_scale = v;
325 n = *ptr++;
326 switch (n & 3) do {
327 case 0: *q++ = iv;
328 case 3: *q++ = iv;
329 case 2: *q++ = iv;
330 case 1: *q++ = iv;
331 } while ((n -= 4) > 0);
332 }else
333 last_exp = 36;
334
335 while (q < q_end) {
336 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
337 if (code < 0){
338 av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
339 return -1;
340 }
341 /* NOTE: this offset is the same as MPEG4 AAC ! */
342 last_exp += code - 60;
343 if ((unsigned)last_exp + 60 >= FF_ARRAY_ELEMS(pow_tab)) {
344 av_log(s->avctx, AV_LOG_ERROR, "Exponent out of range: %d\n",
345 last_exp);
346 return -1;
347 }
348 v = ptab[last_exp];
349 iv = iptab[last_exp];
350 if (v > max_scale)
351 max_scale = v;
352 n = *ptr++;
353 switch (n & 3) do {
354 case 0: *q++ = iv;
355 case 3: *q++ = iv;
356 case 2: *q++ = iv;
357 case 1: *q++ = iv;
358 } while ((n -= 4) > 0);
359 }
360 s->max_exponent[ch] = max_scale;
361 return 0;
362 }
363
364
365 /**
366 * Apply MDCT window and add into output.
367 *
368 * We ensure that when the windows overlap their squared sum
369 * is always 1 (MDCT reconstruction rule).
370 */
371 static void wma_window(WMACodecContext *s, float *out)
372 {
373 float *in = s->output;
374 int block_len, bsize, n;
375
376 /* left part */
377 if (s->block_len_bits <= s->prev_block_len_bits) {
378 block_len = s->block_len;
379 bsize = s->frame_len_bits - s->block_len_bits;
380
381 s->dsp.vector_fmul_add(out, in, s->windows[bsize],
382 out, block_len);
383
384 } else {
385 block_len = 1 << s->prev_block_len_bits;
386 n = (s->block_len - block_len) / 2;
387 bsize = s->frame_len_bits - s->prev_block_len_bits;
388
389 s->dsp.vector_fmul_add(out+n, in+n, s->windows[bsize],
390 out+n, block_len);
391
392 memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
393 }
394
395 out += s->block_len;
396 in += s->block_len;
397
398 /* right part */
399 if (s->block_len_bits <= s->next_block_len_bits) {
400 block_len = s->block_len;
401 bsize = s->frame_len_bits - s->block_len_bits;
402
403 s->dsp.vector_fmul_reverse(out, in, s->windows[bsize], block_len);
404
405 } else {
406 block_len = 1 << s->next_block_len_bits;
407 n = (s->block_len - block_len) / 2;
408 bsize = s->frame_len_bits - s->next_block_len_bits;
409
410 memcpy(out, in, n*sizeof(float));
411
412 s->dsp.vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
413
414 memset(out+n+block_len, 0, n*sizeof(float));
415 }
416 }
417
418
419 /**
420 * @return 0 if OK. 1 if last block of frame. return -1 if
421 * unrecorrable error.
422 */
423 static int wma_decode_block(WMACodecContext *s)
424 {
425 int n, v, a, ch, bsize;
426 int coef_nb_bits, total_gain;
427 int nb_coefs[MAX_CHANNELS];
428 float mdct_norm;
429 FFTContext *mdct;
430
431 #ifdef TRACE
432 tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
433 #endif
434
435 /* compute current block length */
436 if (s->use_variable_block_len) {
437 n = av_log2(s->nb_block_sizes - 1) + 1;
438
439 if (s->reset_block_lengths) {
440 s->reset_block_lengths = 0;
441 v = get_bits(&s->gb, n);
442 if (v >= s->nb_block_sizes){
443 av_log(s->avctx, AV_LOG_ERROR, "prev_block_len_bits %d out of range\n", s->frame_len_bits - v);
444 return -1;
445 }
446 s->prev_block_len_bits = s->frame_len_bits - v;
447 v = get_bits(&s->gb, n);
448 if (v >= s->nb_block_sizes){
449 av_log(s->avctx, AV_LOG_ERROR, "block_len_bits %d out of range\n", s->frame_len_bits - v);
450 return -1;
451 }
452 s->block_len_bits = s->frame_len_bits - v;
453 } else {
454 /* update block lengths */
455 s->prev_block_len_bits = s->block_len_bits;
456 s->block_len_bits = s->next_block_len_bits;
457 }
458 v = get_bits(&s->gb, n);
459 if (v >= s->nb_block_sizes){
460 av_log(s->avctx, AV_LOG_ERROR, "next_block_len_bits %d out of range\n", s->frame_len_bits - v);
461 return -1;
462 }
463 s->next_block_len_bits = s->frame_len_bits - v;
464 } else {
465 /* fixed block len */
466 s->next_block_len_bits = s->frame_len_bits;
467 s->prev_block_len_bits = s->frame_len_bits;
468 s->block_len_bits = s->frame_len_bits;
469 }
470
471 /* now check if the block length is coherent with the frame length */
472 s->block_len = 1 << s->block_len_bits;
473 if ((s->block_pos + s->block_len) > s->frame_len){
474 av_log(s->avctx, AV_LOG_ERROR, "frame_len overflow\n");
475 return -1;
476 }
477
478 if (s->avctx->channels == 2) {
479 s->ms_stereo = get_bits1(&s->gb);
480 }
481 v = 0;
482 for(ch = 0; ch < s->avctx->channels; ch++) {
483 a = get_bits1(&s->gb);
484 s->channel_coded[ch] = a;
485 v |= a;
486 }
487
488 bsize = s->frame_len_bits - s->block_len_bits;
489
490 /* if no channel coded, no need to go further */
491 /* XXX: fix potential framing problems */
492 if (!v)
493 goto next;
494
495 /* read total gain and extract corresponding number of bits for
496 coef escape coding */
497 total_gain = 1;
498 for(;;) {
499 a = get_bits(&s->gb, 7);
500 total_gain += a;
501 if (a != 127)
502 break;
503 }
504
505 coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
506
507 /* compute number of coefficients */
508 n = s->coefs_end[bsize] - s->coefs_start;
509 for(ch = 0; ch < s->avctx->channels; ch++)
510 nb_coefs[ch] = n;
511
512 /* complex coding */
513 if (s->use_noise_coding) {
514
515 for(ch = 0; ch < s->avctx->channels; ch++) {
516 if (s->channel_coded[ch]) {
517 int i, n, a;
518 n = s->exponent_high_sizes[bsize];
519 for(i=0;i<n;i++) {
520 a = get_bits1(&s->gb);
521 s->high_band_coded[ch][i] = a;
522 /* if noise coding, the coefficients are not transmitted */
523 if (a)
524 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
525 }
526 }
527 }
528 for(ch = 0; ch < s->avctx->channels; ch++) {
529 if (s->channel_coded[ch]) {
530 int i, n, val, code;
531
532 n = s->exponent_high_sizes[bsize];
533 val = (int)0x80000000;
534 for(i=0;i<n;i++) {
535 if (s->high_band_coded[ch][i]) {
536 if (val == (int)0x80000000) {
537 val = get_bits(&s->gb, 7) - 19;
538 } else {
539 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
540 if (code < 0){
541 av_log(s->avctx, AV_LOG_ERROR, "hgain vlc invalid\n");
542 return -1;
543 }
544 val += code - 18;
545 }
546 s->high_band_values[ch][i] = val;
547 }
548 }
549 }
550 }
551 }
552
553 /* exponents can be reused in short blocks. */
554 if ((s->block_len_bits == s->frame_len_bits) ||
555 get_bits1(&s->gb)) {
556 for(ch = 0; ch < s->avctx->channels; ch++) {
557 if (s->channel_coded[ch]) {
558 if (s->use_exp_vlc) {
559 if (decode_exp_vlc(s, ch) < 0)
560 return -1;
561 } else {
562 decode_exp_lsp(s, ch);
563 }
564 s->exponents_bsize[ch] = bsize;
565 }
566 }
567 }
568
569 /* parse spectral coefficients : just RLE encoding */
570 for (ch = 0; ch < s->avctx->channels; ch++) {
571 if (s->channel_coded[ch]) {
572 int tindex;
573 WMACoef* ptr = &s->coefs1[ch][0];
574
575 /* special VLC tables are used for ms stereo because
576 there is potentially less energy there */
577 tindex = (ch == 1 && s->ms_stereo);
578 memset(ptr, 0, s->block_len * sizeof(WMACoef));
579 ff_wma_run_level_decode(s->avctx, &s->gb, &s->coef_vlc[tindex],
580 s->level_table[tindex], s->run_table[tindex],
581 0, ptr, 0, nb_coefs[ch],
582 s->block_len, s->frame_len_bits, coef_nb_bits);
583 }
584 if (s->version == 1 && s->avctx->channels >= 2) {
585 align_get_bits(&s->gb);
586 }
587 }
588
589 /* normalize */
590 {
591 int n4 = s->block_len / 2;
592 mdct_norm = 1.0 / (float)n4;
593 if (s->version == 1) {
594 mdct_norm *= sqrt(n4);
595 }
596 }
597
598 /* finally compute the MDCT coefficients */
599 for (ch = 0; ch < s->avctx->channels; ch++) {
600 if (s->channel_coded[ch]) {
601 WMACoef *coefs1;
602 float *coefs, *exponents, mult, mult1, noise;
603 int i, j, n, n1, last_high_band, esize;
604 float exp_power[HIGH_BAND_MAX_SIZE];
605
606 coefs1 = s->coefs1[ch];
607 exponents = s->exponents[ch];
608 esize = s->exponents_bsize[ch];
609 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
610 mult *= mdct_norm;
611 coefs = s->coefs[ch];
612 if (s->use_noise_coding) {
613 mult1 = mult;
614 /* very low freqs : noise */
615 for(i = 0;i < s->coefs_start; i++) {
616 *coefs++ = s->noise_table[s->noise_index] *
617 exponents[i<<bsize>>esize] * mult1;
618 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
619 }
620
621 n1 = s->exponent_high_sizes[bsize];
622
623 /* compute power of high bands */
624 exponents = s->exponents[ch] +
625 (s->high_band_start[bsize]<<bsize>>esize);
626 last_high_band = 0; /* avoid warning */
627 for(j=0;j<n1;j++) {
628 n = s->exponent_high_bands[s->frame_len_bits -
629 s->block_len_bits][j];
630 if (s->high_band_coded[ch][j]) {
631 float e2, v;
632 e2 = 0;
633 for(i = 0;i < n; i++) {
634 v = exponents[i<<bsize>>esize];
635 e2 += v * v;
636 }
637 exp_power[j] = e2 / n;
638 last_high_band = j;
639 tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
640 }
641 exponents += n<<bsize>>esize;
642 }
643
644 /* main freqs and high freqs */
645 exponents = s->exponents[ch] + (s->coefs_start<<bsize>>esize);
646 for(j=-1;j<n1;j++) {
647 if (j < 0) {
648 n = s->high_band_start[bsize] -
649 s->coefs_start;
650 } else {
651 n = s->exponent_high_bands[s->frame_len_bits -
652 s->block_len_bits][j];
653 }
654 if (j >= 0 && s->high_band_coded[ch][j]) {
655 /* use noise with specified power */
656 mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
657 /* XXX: use a table */
658 mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
659 mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
660 mult1 *= mdct_norm;
661 for(i = 0;i < n; i++) {
662 noise = s->noise_table[s->noise_index];
663 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
664 *coefs++ = noise *
665 exponents[i<<bsize>>esize] * mult1;
666 }
667 exponents += n<<bsize>>esize;
668 } else {
669 /* coded values + small noise */
670 for(i = 0;i < n; i++) {
671 noise = s->noise_table[s->noise_index];
672 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
673 *coefs++ = ((*coefs1++) + noise) *
674 exponents[i<<bsize>>esize] * mult;
675 }
676 exponents += n<<bsize>>esize;
677 }
678 }
679
680 /* very high freqs : noise */
681 n = s->block_len - s->coefs_end[bsize];
682 mult1 = mult * exponents[((-1<<bsize))>>esize];
683 for(i = 0; i < n; i++) {
684 *coefs++ = s->noise_table[s->noise_index] * mult1;
685 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
686 }
687 } else {
688 /* XXX: optimize more */
689 for(i = 0;i < s->coefs_start; i++)
690 *coefs++ = 0.0;
691 n = nb_coefs[ch];
692 for(i = 0;i < n; i++) {
693 *coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
694 }
695 n = s->block_len - s->coefs_end[bsize];
696 for(i = 0;i < n; i++)
697 *coefs++ = 0.0;
698 }
699 }
700 }
701
702 #ifdef TRACE
703 for (ch = 0; ch < s->avctx->channels; ch++) {
704 if (s->channel_coded[ch]) {
705 dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
706 dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
707 }
708 }
709 #endif
710
711 if (s->ms_stereo && s->channel_coded[1]) {
712 /* nominal case for ms stereo: we do it before mdct */
713 /* no need to optimize this case because it should almost
714 never happen */
715 if (!s->channel_coded[0]) {
716 tprintf(s->avctx, "rare ms-stereo case happened\n");
717 memset(s->coefs[0], 0, sizeof(float) * s->block_len);
718 s->channel_coded[0] = 1;
719 }
720
721 s->dsp.butterflies_float(s->coefs[0], s->coefs[1], s->block_len);
722 }
723
724 next:
725 mdct = &s->mdct_ctx[bsize];
726
727 for (ch = 0; ch < s->avctx->channels; ch++) {
728 int n4, index;
729
730 n4 = s->block_len / 2;
731 if(s->channel_coded[ch]){
732 mdct->imdct_calc(mdct, s->output, s->coefs[ch]);
733 }else if(!(s->ms_stereo && ch==1))
734 memset(s->output, 0, sizeof(s->output));
735
736 /* multiply by the window and add in the frame */
737 index = (s->frame_len / 2) + s->block_pos - n4;
738 wma_window(s, &s->frame_out[ch][index]);
739 }
740
741 /* update block number */
742 s->block_num++;
743 s->block_pos += s->block_len;
744 if (s->block_pos >= s->frame_len)
745 return 1;
746 else
747 return 0;
748 }
749
750 /* decode a frame of frame_len samples */
751 static int wma_decode_frame(WMACodecContext *s, float **samples,
752 int samples_offset)
753 {
754 int ret, ch;
755
756 #ifdef TRACE
757 tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
758 #endif
759
760 /* read each block */
761 s->block_num = 0;
762 s->block_pos = 0;
763 for(;;) {
764 ret = wma_decode_block(s);
765 if (ret < 0)
766 return -1;
767 if (ret)
768 break;
769 }
770
771 for (ch = 0; ch < s->avctx->channels; ch++) {
772 /* copy current block to output */
773 memcpy(samples[ch] + samples_offset, s->frame_out[ch],
774 s->frame_len * sizeof(*s->frame_out[ch]));
775 /* prepare for next block */
776 memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
777 s->frame_len * sizeof(*s->frame_out[ch]));
778
779 #ifdef TRACE
780 dump_floats(s, "samples", 6, samples[ch] + samples_offset, s->frame_len);
781 #endif
782 }
783
784 return 0;
785 }
786
787 static int wma_decode_superframe(AVCodecContext *avctx, void *data,
788 int *got_frame_ptr, AVPacket *avpkt)
789 {
790 const uint8_t *buf = avpkt->data;
791 int buf_size = avpkt->size;
792 WMACodecContext *s = avctx->priv_data;
793 int nb_frames, bit_offset, i, pos, len, ret;
794 uint8_t *q;
795 float **samples;
796 int samples_offset;
797
798 tprintf(avctx, "***decode_superframe:\n");
799
800 if(buf_size==0){
801 s->last_superframe_len = 0;
802 return 0;
803 }
804 if (buf_size < avctx->block_align) {
805 av_log(avctx, AV_LOG_ERROR,
806 "Input packet size too small (%d < %d)\n",
807 buf_size, avctx->block_align);
808 return AVERROR_INVALIDDATA;
809 }
810 buf_size = avctx->block_align;
811
812 init_get_bits(&s->gb, buf, buf_size*8);
813
814 if (s->use_bit_reservoir) {
815 /* read super frame header */
816 skip_bits(&s->gb, 4); /* super frame index */
817 nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0);
818 } else {
819 nb_frames = 1;
820 }
821
822 /* get output buffer */
823 s->frame.nb_samples = nb_frames * s->frame_len;
824 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
825 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
826 return ret;
827 }
828 samples = (float **)s->frame.extended_data;
829 samples_offset = 0;
830
831 if (s->use_bit_reservoir) {
832 bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
833 if (bit_offset > get_bits_left(&s->gb)) {
834 av_log(avctx, AV_LOG_ERROR,
835 "Invalid last frame bit offset %d > buf size %d (%d)\n",
836 bit_offset, get_bits_left(&s->gb), buf_size);
837 goto fail;
838 }
839
840 if (s->last_superframe_len > 0) {
841 /* add bit_offset bits to last frame */
842 if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
843 MAX_CODED_SUPERFRAME_SIZE)
844 goto fail;
845 q = s->last_superframe + s->last_superframe_len;
846 len = bit_offset;
847 while (len > 7) {
848 *q++ = (get_bits)(&s->gb, 8);
849 len -= 8;
850 }
851 if (len > 0) {
852 *q++ = (get_bits)(&s->gb, len) << (8 - len);
853 }
854 memset(q, 0, FF_INPUT_BUFFER_PADDING_SIZE);
855
856 /* XXX: bit_offset bits into last frame */
857 init_get_bits(&s->gb, s->last_superframe, s->last_superframe_len * 8 + bit_offset);
858 /* skip unused bits */
859 if (s->last_bitoffset > 0)
860 skip_bits(&s->gb, s->last_bitoffset);
861 /* this frame is stored in the last superframe and in the
862 current one */
863 if (wma_decode_frame(s, samples, samples_offset) < 0)
864 goto fail;
865 samples_offset += s->frame_len;
866 nb_frames--;
867 }
868
869 /* read each frame starting from bit_offset */
870 pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
871 if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)
872 return AVERROR_INVALIDDATA;
873 init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3))*8);
874 len = pos & 7;
875 if (len > 0)
876 skip_bits(&s->gb, len);
877
878 s->reset_block_lengths = 1;
879 for(i=0;i<nb_frames;i++) {
880 if (wma_decode_frame(s, samples, samples_offset) < 0)
881 goto fail;
882 samples_offset += s->frame_len;
883 }
884
885 /* we copy the end of the frame in the last frame buffer */
886 pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
887 s->last_bitoffset = pos & 7;
888 pos >>= 3;
889 len = buf_size - pos;
890 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
891 av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\n", len);
892 goto fail;
893 }
894 s->last_superframe_len = len;
895 memcpy(s->last_superframe, buf + pos, len);
896 } else {
897 /* single frame decode */
898 if (wma_decode_frame(s, samples, samples_offset) < 0)
899 goto fail;
900 samples_offset += s->frame_len;
901 }
902
903 av_dlog(s->avctx, "%d %d %d %d outbytes:%td eaten:%d\n",
904 s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len,
905 (int8_t *)samples - (int8_t *)data, avctx->block_align);
906
907 *got_frame_ptr = 1;
908 *(AVFrame *)data = s->frame;
909
910 return avctx->block_align;
911 fail:
912 /* when error, we reset the bit reservoir */
913 s->last_superframe_len = 0;
914 return -1;
915 }
916
917 static av_cold void flush(AVCodecContext *avctx)
918 {
919 WMACodecContext *s = avctx->priv_data;
920
921 s->last_bitoffset=
922 s->last_superframe_len= 0;
923 }
924
925 AVCodec ff_wmav1_decoder = {
926 .name = "wmav1",
927 .type = AVMEDIA_TYPE_AUDIO,
928 .id = AV_CODEC_ID_WMAV1,
929 .priv_data_size = sizeof(WMACodecContext),
930 .init = wma_decode_init,
931 .close = ff_wma_end,
932 .decode = wma_decode_superframe,
933 .flush = flush,
934 .capabilities = CODEC_CAP_DR1,
935 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
936 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
937 AV_SAMPLE_FMT_NONE },
938 };
939
940 AVCodec ff_wmav2_decoder = {
941 .name = "wmav2",
942 .type = AVMEDIA_TYPE_AUDIO,
943 .id = AV_CODEC_ID_WMAV2,
944 .priv_data_size = sizeof(WMACodecContext),
945 .init = wma_decode_init,
946 .close = ff_wma_end,
947 .decode = wma_decode_superframe,
948 .flush = flush,
949 .capabilities = CODEC_CAP_DR1,
950 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
951 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
952 AV_SAMPLE_FMT_NONE },
953 };