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