Statistics
| Branch: | Revision:

ffmpeg / libavcodec / wmadec.c @ 1890c2ac

History | View | Annotate | Download (27.9 KB)

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 wmadec.c
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_shorts(WMADecodeContext *s, const char *name, const short *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, " %5d.0", tab[i]);
60
        if ((i & 7) == 7)
61
            tprintf(s->avctx, "\n");
62
    }
63
}
64

    
65
static void dump_floats(WMADecodeContext *s, const char *name, int prec, const float *tab, int n)
66
{
67
    int i;
68

    
69
    tprintf(s->avctx, "%s[%d]:\n", name, n);
70
    for(i=0;i<n;i++) {
71
        if ((i & 7) == 0)
72
            tprintf(s->avctx, "%4d: ", i);
73
        tprintf(s->avctx, " %8.*f", prec, tab[i]);
74
        if ((i & 7) == 7)
75
            tprintf(s->avctx, "\n");
76
    }
77
    if ((i & 7) != 0)
78
        tprintf(s->avctx, "\n");
79
}
80
#endif
81

    
82
static int wma_decode_init(AVCodecContext * avctx)
83
{
84
    WMACodecContext *s = avctx->priv_data;
85
    int i, flags1, flags2;
86
    uint8_t *extradata;
87

    
88
    s->avctx = avctx;
89

    
90
    /* extract flag infos */
91
    flags1 = 0;
92
    flags2 = 0;
93
    extradata = avctx->extradata;
94
    if (avctx->codec->id == CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
95
        flags1 = extradata[0] | (extradata[1] << 8);
96
        flags2 = extradata[2] | (extradata[3] << 8);
97
    } else if (avctx->codec->id == CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
98
        flags1 = extradata[0] | (extradata[1] << 8) |
99
            (extradata[2] << 16) | (extradata[3] << 24);
100
        flags2 = extradata[4] | (extradata[5] << 8);
101
    }
102
// for(i=0; i<avctx->extradata_size; i++)
103
//     av_log(NULL, AV_LOG_ERROR, "%02X ", extradata[i]);
104

    
105
    s->use_exp_vlc = flags2 & 0x0001;
106
    s->use_bit_reservoir = flags2 & 0x0002;
107
    s->use_variable_block_len = flags2 & 0x0004;
108

    
109
    ff_wma_init(avctx, flags2);
110

    
111
    /* init MDCT */
112
    for(i = 0; i < s->nb_block_sizes; i++)
113
        ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
114

    
115
    if (s->use_noise_coding) {
116
        init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits),
117
                 ff_wma_hgain_huffbits, 1, 1,
118
                 ff_wma_hgain_huffcodes, 2, 2, 0);
119
    }
120

    
121
    if (s->use_exp_vlc) {
122
        init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_wma_scale_huffbits), //FIXME move out of context
123
                 ff_wma_scale_huffbits, 1, 1,
124
                 ff_wma_scale_huffcodes, 4, 4, 0);
125
    } else {
126
        wma_lsp_to_curve_init(s, s->frame_len);
127
    }
128

    
129
    return 0;
130
}
131

    
132
/**
133
 * interpolate values for a bigger or smaller block. The block must
134
 * have multiple sizes
135
 */
136
static void interpolate_array(float *scale, int old_size, int new_size)
137
{
138
    int i, j, jincr, k;
139
    float v;
140

    
141
    if (new_size > old_size) {
142
        jincr = new_size / old_size;
143
        j = new_size;
144
        for(i = old_size - 1; i >=0; i--) {
145
            v = scale[i];
146
            k = jincr;
147
            do {
148
                scale[--j] = v;
149
            } while (--k);
150
        }
151
    } else if (new_size < old_size) {
152
        j = 0;
153
        jincr = old_size / new_size;
154
        for(i = 0; i < new_size; i++) {
155
            scale[i] = scale[j];
156
            j += jincr;
157
        }
158
    }
159
}
160

    
161
/**
162
 * compute x^-0.25 with an exponent and mantissa table. We use linear
163
 * interpolation to reduce the mantissa table size at a small speed
164
 * expense (linear interpolation approximately doubles the number of
165
 * bits of precision).
166
 */
167
static inline float pow_m1_4(WMACodecContext *s, float x)
168
{
169
    union {
170
        float f;
171
        unsigned int v;
172
    } u, t;
173
    unsigned int e, m;
174
    float a, b;
175

    
176
    u.f = x;
177
    e = u.v >> 23;
178
    m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
179
    /* build interpolation scale: 1 <= t < 2. */
180
    t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
181
    a = s->lsp_pow_m_table1[m];
182
    b = s->lsp_pow_m_table2[m];
183
    return s->lsp_pow_e_table[e] * (a + b * t.f);
184
}
185

    
186
static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
187
{
188
    float wdel, a, b;
189
    int i, e, m;
190

    
191
    wdel = M_PI / frame_len;
192
    for(i=0;i<frame_len;i++)
193
        s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
194

    
195
    /* tables for x^-0.25 computation */
196
    for(i=0;i<256;i++) {
197
        e = i - 126;
198
        s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
199
    }
200

    
201
    /* NOTE: these two tables are needed to avoid two operations in
202
       pow_m1_4 */
203
    b = 1.0;
204
    for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
205
        m = (1 << LSP_POW_BITS) + i;
206
        a = (float)m * (0.5 / (1 << LSP_POW_BITS));
207
        a = pow(a, -0.25);
208
        s->lsp_pow_m_table1[i] = 2 * a - b;
209
        s->lsp_pow_m_table2[i] = b - a;
210
        b = a;
211
    }
212
#if 0
213
    for(i=1;i<20;i++) {
214
        float v, r1, r2;
215
        v = 5.0 / i;
216
        r1 = pow_m1_4(s, v);
217
        r2 = pow(v,-0.25);
218
        printf("%f^-0.25=%f e=%f\n", v, r1, r2 - r1);
219
    }
220
#endif
221
}
222

    
223
/**
224
 * NOTE: We use the same code as Vorbis here
225
 * @todo optimize it further with SSE/3Dnow
226
 */
227
static void wma_lsp_to_curve(WMACodecContext *s,
228
                             float *out, float *val_max_ptr,
229
                             int n, float *lsp)
230
{
231
    int i, j;
232
    float p, q, w, v, val_max;
233

    
234
    val_max = 0;
235
    for(i=0;i<n;i++) {
236
        p = 0.5f;
237
        q = 0.5f;
238
        w = s->lsp_cos_table[i];
239
        for(j=1;j<NB_LSP_COEFS;j+=2){
240
            q *= w - lsp[j - 1];
241
            p *= w - lsp[j];
242
        }
243
        p *= p * (2.0f - w);
244
        q *= q * (2.0f + w);
245
        v = p + q;
246
        v = pow_m1_4(s, v);
247
        if (v > val_max)
248
            val_max = v;
249
        out[i] = v;
250
    }
251
    *val_max_ptr = val_max;
252
}
253

    
254
/**
255
 * decode exponents coded with LSP coefficients (same idea as Vorbis)
256
 */
257
static void decode_exp_lsp(WMACodecContext *s, int ch)
258
{
259
    float lsp_coefs[NB_LSP_COEFS];
260
    int val, i;
261

    
262
    for(i = 0; i < NB_LSP_COEFS; i++) {
263
        if (i == 0 || i >= 8)
264
            val = get_bits(&s->gb, 3);
265
        else
266
            val = get_bits(&s->gb, 4);
267
        lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
268
    }
269

    
270
    wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
271
                     s->block_len, lsp_coefs);
272
}
273

    
274
/**
275
 * decode exponents coded with VLC codes
276
 */
277
static int decode_exp_vlc(WMACodecContext *s, int ch)
278
{
279
    int last_exp, n, code;
280
    const uint16_t *ptr, *band_ptr;
281
    float v, *q, max_scale, *q_end;
282

    
283
    band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
284
    ptr = band_ptr;
285
    q = s->exponents[ch];
286
    q_end = q + s->block_len;
287
    max_scale = 0;
288
    if (s->version == 1) {
289
        last_exp = get_bits(&s->gb, 5) + 10;
290
        /* XXX: use a table */
291
        v = pow(10, last_exp * (1.0 / 16.0));
292
        max_scale = v;
293
        n = *ptr++;
294
        do {
295
            *q++ = v;
296
        } while (--n);
297
    }else
298
        last_exp = 36;
299

    
300
    while (q < q_end) {
301
        code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
302
        if (code < 0)
303
            return -1;
304
        /* NOTE: this offset is the same as MPEG4 AAC ! */
305
        last_exp += code - 60;
306
        /* XXX: use a table */
307
        v = pow(10, last_exp * (1.0 / 16.0));
308
        if (v > max_scale)
309
            max_scale = v;
310
        n = *ptr++;
311
        do {
312
            *q++ = v;
313
        } while (--n);
314
    }
315
    s->max_exponent[ch] = max_scale;
316
    return 0;
317
}
318

    
319

    
320
/**
321
 * Apply MDCT window and add into output.
322
 *
323
 * We ensure that when the windows overlap their squared sum
324
 * is always 1 (MDCT reconstruction rule).
325
 */
326
static void wma_window(WMACodecContext *s, float *out)
327
{
328
    float *in = s->output;
329
    int block_len, bsize, n;
330

    
331
    /* left part */
332
    if (s->block_len_bits <= s->prev_block_len_bits) {
333
        block_len = s->block_len;
334
        bsize = s->frame_len_bits - s->block_len_bits;
335

    
336
        s->dsp.vector_fmul_add_add(out, in, s->windows[bsize],
337
                                   out, 0, block_len, 1);
338

    
339
    } else {
340
        block_len = 1 << s->prev_block_len_bits;
341
        n = (s->block_len - block_len) / 2;
342
        bsize = s->frame_len_bits - s->prev_block_len_bits;
343

    
344
        s->dsp.vector_fmul_add_add(out+n, in+n, s->windows[bsize],
345
                                   out+n, 0, block_len, 1);
346

    
347
        memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
348
    }
349

    
350
    out += s->block_len;
351
    in += s->block_len;
352

    
353
    /* right part */
354
    if (s->block_len_bits <= s->next_block_len_bits) {
355
        block_len = s->block_len;
356
        bsize = s->frame_len_bits - s->block_len_bits;
357

    
358
        s->dsp.vector_fmul_reverse(out, in, s->windows[bsize], block_len);
359

    
360
    } else {
361
        block_len = 1 << s->next_block_len_bits;
362
        n = (s->block_len - block_len) / 2;
363
        bsize = s->frame_len_bits - s->next_block_len_bits;
364

    
365
        memcpy(out, in, n*sizeof(float));
366

    
367
        s->dsp.vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
368

    
369
        memset(out+n+block_len, 0, n*sizeof(float));
370
    }
371
}
372

    
373

    
374
/**
375
 * @return 0 if OK. 1 if last block of frame. return -1 if
376
 * unrecorrable error.
377
 */
378
static int wma_decode_block(WMACodecContext *s)
379
{
380
    int n, v, a, ch, code, bsize;
381
    int coef_nb_bits, total_gain, parse_exponents;
382
    int nb_coefs[MAX_CHANNELS];
383
    float mdct_norm;
384

    
385
#ifdef TRACE
386
    tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
387
#endif
388

    
389
    /* compute current block length */
390
    if (s->use_variable_block_len) {
391
        n = av_log2(s->nb_block_sizes - 1) + 1;
392

    
393
        if (s->reset_block_lengths) {
394
            s->reset_block_lengths = 0;
395
            v = get_bits(&s->gb, n);
396
            if (v >= s->nb_block_sizes)
397
                return -1;
398
            s->prev_block_len_bits = s->frame_len_bits - v;
399
            v = get_bits(&s->gb, n);
400
            if (v >= s->nb_block_sizes)
401
                return -1;
402
            s->block_len_bits = s->frame_len_bits - v;
403
        } else {
404
            /* update block lengths */
405
            s->prev_block_len_bits = s->block_len_bits;
406
            s->block_len_bits = s->next_block_len_bits;
407
        }
408
        v = get_bits(&s->gb, n);
409
        if (v >= s->nb_block_sizes)
410
            return -1;
411
        s->next_block_len_bits = s->frame_len_bits - v;
412
    } else {
413
        /* fixed block len */
414
        s->next_block_len_bits = s->frame_len_bits;
415
        s->prev_block_len_bits = s->frame_len_bits;
416
        s->block_len_bits = s->frame_len_bits;
417
    }
418

    
419
    /* now check if the block length is coherent with the frame length */
420
    s->block_len = 1 << s->block_len_bits;
421
    if ((s->block_pos + s->block_len) > s->frame_len)
422
        return -1;
423

    
424
    if (s->nb_channels == 2) {
425
        s->ms_stereo = get_bits(&s->gb, 1);
426
    }
427
    v = 0;
428
    for(ch = 0; ch < s->nb_channels; ch++) {
429
        a = get_bits(&s->gb, 1);
430
        s->channel_coded[ch] = a;
431
        v |= a;
432
    }
433
    /* if no channel coded, no need to go further */
434
    /* XXX: fix potential framing problems */
435
    if (!v)
436
        goto next;
437

    
438
    bsize = s->frame_len_bits - s->block_len_bits;
439

    
440
    /* read total gain and extract corresponding number of bits for
441
       coef escape coding */
442
    total_gain = 1;
443
    for(;;) {
444
        a = get_bits(&s->gb, 7);
445
        total_gain += a;
446
        if (a != 127)
447
            break;
448
    }
449

    
450
    coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
451

    
452
    /* compute number of coefficients */
453
    n = s->coefs_end[bsize] - s->coefs_start;
454
    for(ch = 0; ch < s->nb_channels; ch++)
455
        nb_coefs[ch] = n;
456

    
457
    /* complex coding */
458
    if (s->use_noise_coding) {
459

    
460
        for(ch = 0; ch < s->nb_channels; ch++) {
461
            if (s->channel_coded[ch]) {
462
                int i, n, a;
463
                n = s->exponent_high_sizes[bsize];
464
                for(i=0;i<n;i++) {
465
                    a = get_bits(&s->gb, 1);
466
                    s->high_band_coded[ch][i] = a;
467
                    /* if noise coding, the coefficients are not transmitted */
468
                    if (a)
469
                        nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
470
                }
471
            }
472
        }
473
        for(ch = 0; ch < s->nb_channels; ch++) {
474
            if (s->channel_coded[ch]) {
475
                int i, n, val, code;
476

    
477
                n = s->exponent_high_sizes[bsize];
478
                val = (int)0x80000000;
479
                for(i=0;i<n;i++) {
480
                    if (s->high_band_coded[ch][i]) {
481
                        if (val == (int)0x80000000) {
482
                            val = get_bits(&s->gb, 7) - 19;
483
                        } else {
484
                            code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
485
                            if (code < 0)
486
                                return -1;
487
                            val += code - 18;
488
                        }
489
                        s->high_band_values[ch][i] = val;
490
                    }
491
                }
492
            }
493
        }
494
    }
495

    
496
    /* exposant can be interpolated in short blocks. */
497
    parse_exponents = 1;
498
    if (s->block_len_bits != s->frame_len_bits) {
499
        parse_exponents = get_bits(&s->gb, 1);
500
    }
501

    
502
    if (parse_exponents) {
503
        for(ch = 0; ch < s->nb_channels; ch++) {
504
            if (s->channel_coded[ch]) {
505
                if (s->use_exp_vlc) {
506
                    if (decode_exp_vlc(s, ch) < 0)
507
                        return -1;
508
                } else {
509
                    decode_exp_lsp(s, ch);
510
                }
511
            }
512
        }
513
    } else {
514
        for(ch = 0; ch < s->nb_channels; ch++) {
515
            if (s->channel_coded[ch]) {
516
                interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits,
517
                                  s->block_len);
518
            }
519
        }
520
    }
521

    
522
    /* parse spectral coefficients : just RLE encoding */
523
    for(ch = 0; ch < s->nb_channels; ch++) {
524
        if (s->channel_coded[ch]) {
525
            VLC *coef_vlc;
526
            int level, run, sign, tindex;
527
            int16_t *ptr, *eptr;
528
            const uint16_t *level_table, *run_table;
529

    
530
            /* special VLC tables are used for ms stereo because
531
               there is potentially less energy there */
532
            tindex = (ch == 1 && s->ms_stereo);
533
            coef_vlc = &s->coef_vlc[tindex];
534
            run_table = s->run_table[tindex];
535
            level_table = s->level_table[tindex];
536
            /* XXX: optimize */
537
            ptr = &s->coefs1[ch][0];
538
            eptr = ptr + nb_coefs[ch];
539
            memset(ptr, 0, s->block_len * sizeof(int16_t));
540
            for(;;) {
541
                code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
542
                if (code < 0)
543
                    return -1;
544
                if (code == 1) {
545
                    /* EOB */
546
                    break;
547
                } else if (code == 0) {
548
                    /* escape */
549
                    level = get_bits(&s->gb, coef_nb_bits);
550
                    /* NOTE: this is rather suboptimal. reading
551
                       block_len_bits would be better */
552
                    run = get_bits(&s->gb, s->frame_len_bits);
553
                } else {
554
                    /* normal code */
555
                    run = run_table[code];
556
                    level = level_table[code];
557
                }
558
                sign = get_bits(&s->gb, 1);
559
                if (!sign)
560
                    level = -level;
561
                ptr += run;
562
                if (ptr >= eptr)
563
                {
564
                    av_log(NULL, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");
565
                    break;
566
                }
567
                *ptr++ = level;
568
                /* NOTE: EOB can be omitted */
569
                if (ptr >= eptr)
570
                    break;
571
            }
572
        }
573
        if (s->version == 1 && s->nb_channels >= 2) {
574
            align_get_bits(&s->gb);
575
        }
576
    }
577

    
578
    /* normalize */
579
    {
580
        int n4 = s->block_len / 2;
581
        mdct_norm = 1.0 / (float)n4;
582
        if (s->version == 1) {
583
            mdct_norm *= sqrt(n4);
584
        }
585
    }
586

    
587
    /* finally compute the MDCT coefficients */
588
    for(ch = 0; ch < s->nb_channels; ch++) {
589
        if (s->channel_coded[ch]) {
590
            int16_t *coefs1;
591
            float *coefs, *exponents, mult, mult1, noise, *exp_ptr;
592
            int i, j, n, n1, last_high_band;
593
            float exp_power[HIGH_BAND_MAX_SIZE];
594

    
595
            coefs1 = s->coefs1[ch];
596
            exponents = s->exponents[ch];
597
            mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
598
            mult *= mdct_norm;
599
            coefs = s->coefs[ch];
600
            if (s->use_noise_coding) {
601
                mult1 = mult;
602
                /* very low freqs : noise */
603
                for(i = 0;i < s->coefs_start; i++) {
604
                    *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1;
605
                    s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
606
                }
607

    
608
                n1 = s->exponent_high_sizes[bsize];
609

    
610
                /* compute power of high bands */
611
                exp_ptr = exponents +
612
                    s->high_band_start[bsize] -
613
                    s->coefs_start;
614
                last_high_band = 0; /* avoid warning */
615
                for(j=0;j<n1;j++) {
616
                    n = s->exponent_high_bands[s->frame_len_bits -
617
                                              s->block_len_bits][j];
618
                    if (s->high_band_coded[ch][j]) {
619
                        float e2, v;
620
                        e2 = 0;
621
                        for(i = 0;i < n; i++) {
622
                            v = exp_ptr[i];
623
                            e2 += v * v;
624
                        }
625
                        exp_power[j] = e2 / n;
626
                        last_high_band = j;
627
                        tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
628
                    }
629
                    exp_ptr += n;
630
                }
631

    
632
                /* main freqs and high freqs */
633
                for(j=-1;j<n1;j++) {
634
                    if (j < 0) {
635
                        n = s->high_band_start[bsize] -
636
                            s->coefs_start;
637
                    } else {
638
                        n = s->exponent_high_bands[s->frame_len_bits -
639
                                                  s->block_len_bits][j];
640
                    }
641
                    if (j >= 0 && s->high_band_coded[ch][j]) {
642
                        /* use noise with specified power */
643
                        mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
644
                        /* XXX: use a table */
645
                        mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
646
                        mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
647
                        mult1 *= mdct_norm;
648
                        for(i = 0;i < n; i++) {
649
                            noise = s->noise_table[s->noise_index];
650
                            s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
651
                            *coefs++ = (*exponents++) * noise * mult1;
652
                        }
653
                    } else {
654
                        /* coded values + small noise */
655
                        for(i = 0;i < n; i++) {
656
                            noise = s->noise_table[s->noise_index];
657
                            s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
658
                            *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult;
659
                        }
660
                    }
661
                }
662

    
663
                /* very high freqs : noise */
664
                n = s->block_len - s->coefs_end[bsize];
665
                mult1 = mult * exponents[-1];
666
                for(i = 0; i < n; i++) {
667
                    *coefs++ = s->noise_table[s->noise_index] * mult1;
668
                    s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
669
                }
670
            } else {
671
                /* XXX: optimize more */
672
                for(i = 0;i < s->coefs_start; i++)
673
                    *coefs++ = 0.0;
674
                n = nb_coefs[ch];
675
                for(i = 0;i < n; i++) {
676
                    *coefs++ = coefs1[i] * exponents[i] * mult;
677
                }
678
                n = s->block_len - s->coefs_end[bsize];
679
                for(i = 0;i < n; i++)
680
                    *coefs++ = 0.0;
681
            }
682
        }
683
    }
684

    
685
#ifdef TRACE
686
    for(ch = 0; ch < s->nb_channels; ch++) {
687
        if (s->channel_coded[ch]) {
688
            dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
689
            dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
690
        }
691
    }
692
#endif
693

    
694
    if (s->ms_stereo && s->channel_coded[1]) {
695
        float a, b;
696
        int i;
697

    
698
        /* nominal case for ms stereo: we do it before mdct */
699
        /* no need to optimize this case because it should almost
700
           never happen */
701
        if (!s->channel_coded[0]) {
702
            tprintf(s->avctx, "rare ms-stereo case happened\n");
703
            memset(s->coefs[0], 0, sizeof(float) * s->block_len);
704
            s->channel_coded[0] = 1;
705
        }
706

    
707
        for(i = 0; i < s->block_len; i++) {
708
            a = s->coefs[0][i];
709
            b = s->coefs[1][i];
710
            s->coefs[0][i] = a + b;
711
            s->coefs[1][i] = a - b;
712
        }
713
    }
714

    
715
    for(ch = 0; ch < s->nb_channels; ch++) {
716
        if (s->channel_coded[ch]) {
717
            int n4, index, n;
718

    
719
            n = s->block_len;
720
            n4 = s->block_len / 2;
721
            s->mdct_ctx[bsize].fft.imdct_calc(&s->mdct_ctx[bsize],
722
                          s->output, s->coefs[ch], s->mdct_tmp);
723

    
724
            /* multiply by the window and add in the frame */
725
            index = (s->frame_len / 2) + s->block_pos - n4;
726
            wma_window(s, &s->frame_out[ch][index]);
727

    
728
            /* specific fast case for ms-stereo : add to second
729
               channel if it is not coded */
730
            if (s->ms_stereo && !s->channel_coded[1]) {
731
                wma_window(s, &s->frame_out[1][index]);
732
            }
733
        }
734
    }
735
 next:
736
    /* update block number */
737
    s->block_num++;
738
    s->block_pos += s->block_len;
739
    if (s->block_pos >= s->frame_len)
740
        return 1;
741
    else
742
        return 0;
743
}
744

    
745
/* decode a frame of frame_len samples */
746
static int wma_decode_frame(WMACodecContext *s, int16_t *samples)
747
{
748
    int ret, i, n, a, ch, incr;
749
    int16_t *ptr;
750
    float *iptr;
751

    
752
#ifdef TRACE
753
    tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
754
#endif
755

    
756
    /* read each block */
757
    s->block_num = 0;
758
    s->block_pos = 0;
759
    for(;;) {
760
        ret = wma_decode_block(s);
761
        if (ret < 0)
762
            return -1;
763
        if (ret)
764
            break;
765
    }
766

    
767
    /* convert frame to integer */
768
    n = s->frame_len;
769
    incr = s->nb_channels;
770
    for(ch = 0; ch < s->nb_channels; ch++) {
771
        ptr = samples + ch;
772
        iptr = s->frame_out[ch];
773

    
774
        for(i=0;i<n;i++) {
775
            a = lrintf(*iptr++);
776
            if (a > 32767)
777
                a = 32767;
778
            else if (a < -32768)
779
                a = -32768;
780
            *ptr = a;
781
            ptr += incr;
782
        }
783
        /* prepare for next block */
784
        memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
785
                s->frame_len * sizeof(float));
786
    }
787

    
788
#ifdef TRACE
789
    dump_shorts(s, "samples", samples, n * s->nb_channels);
790
#endif
791
    return 0;
792
}
793

    
794
static int wma_decode_superframe(AVCodecContext *avctx,
795
                                 void *data, int *data_size,
796
                                 uint8_t *buf, int buf_size)
797
{
798
    WMACodecContext *s = avctx->priv_data;
799
    int nb_frames, bit_offset, i, pos, len;
800
    uint8_t *q;
801
    int16_t *samples;
802

    
803
    tprintf(avctx, "***decode_superframe:\n");
804

    
805
    if(buf_size==0){
806
        s->last_superframe_len = 0;
807
        return 0;
808
    }
809

    
810
    samples = data;
811

    
812
    init_get_bits(&s->gb, buf, buf_size*8);
813

    
814
    if (s->use_bit_reservoir) {
815
        /* read super frame header */
816
        get_bits(&s->gb, 4); /* super frame index */
817
        nb_frames = get_bits(&s->gb, 4) - 1;
818

    
819
        bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
820

    
821
        if (s->last_superframe_len > 0) {
822
            //        printf("skip=%d\n", s->last_bitoffset);
823
            /* add bit_offset bits to last frame */
824
            if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
825
                MAX_CODED_SUPERFRAME_SIZE)
826
                goto fail;
827
            q = s->last_superframe + s->last_superframe_len;
828
            len = bit_offset;
829
            while (len > 7) {
830
                *q++ = (get_bits)(&s->gb, 8);
831
                len -= 8;
832
            }
833
            if (len > 0) {
834
                *q++ = (get_bits)(&s->gb, len) << (8 - len);
835
            }
836

    
837
            /* XXX: bit_offset bits into last frame */
838
            init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
839
            /* skip unused bits */
840
            if (s->last_bitoffset > 0)
841
                skip_bits(&s->gb, s->last_bitoffset);
842
            /* this frame is stored in the last superframe and in the
843
               current one */
844
            if (wma_decode_frame(s, samples) < 0)
845
                goto fail;
846
            samples += s->nb_channels * s->frame_len;
847
        }
848

    
849
        /* read each frame starting from bit_offset */
850
        pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
851
        init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
852
        len = pos & 7;
853
        if (len > 0)
854
            skip_bits(&s->gb, len);
855

    
856
        s->reset_block_lengths = 1;
857
        for(i=0;i<nb_frames;i++) {
858
            if (wma_decode_frame(s, samples) < 0)
859
                goto fail;
860
            samples += s->nb_channels * s->frame_len;
861
        }
862

    
863
        /* we copy the end of the frame in the last frame buffer */
864
        pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
865
        s->last_bitoffset = pos & 7;
866
        pos >>= 3;
867
        len = buf_size - pos;
868
        if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
869
            goto fail;
870
        }
871
        s->last_superframe_len = len;
872
        memcpy(s->last_superframe, buf + pos, len);
873
    } else {
874
        /* single frame decode */
875
        if (wma_decode_frame(s, samples) < 0)
876
            goto fail;
877
        samples += s->nb_channels * s->frame_len;
878
    }
879

    
880
//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d outbytes:%d eaten:%d\n", s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len,        (int8_t *)samples - (int8_t *)data, s->block_align);
881

    
882
    *data_size = (int8_t *)samples - (int8_t *)data;
883
    return s->block_align;
884
 fail:
885
    /* when error, we reset the bit reservoir */
886
    s->last_superframe_len = 0;
887
    return -1;
888
}
889

    
890
AVCodec wmav1_decoder =
891
{
892
    "wmav1",
893
    CODEC_TYPE_AUDIO,
894
    CODEC_ID_WMAV1,
895
    sizeof(WMACodecContext),
896
    wma_decode_init,
897
    NULL,
898
    ff_wma_end,
899
    wma_decode_superframe,
900
};
901

    
902
AVCodec wmav2_decoder =
903
{
904
    "wmav2",
905
    CODEC_TYPE_AUDIO,
906
    CODEC_ID_WMAV2,
907
    sizeof(WMACodecContext),
908
    wma_decode_init,
909
    NULL,
910
    ff_wma_end,
911
    wma_decode_superframe,
912
};