Statistics
| Branch: | Revision:

ffmpeg / libavcodec / wmadec.c @ a9c9a240

History | View | Annotate | Download (28.2 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(WMADecodeContext *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
    WMADecodeContext *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(WMADecodeContext *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(WMADecodeContext *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(WMADecodeContext *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(WMADecodeContext *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(WMADecodeContext *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
 * @return 0 if OK. 1 if last block of frame. return -1 if
321
 * unrecorrable error.
322
 */
323
static int wma_decode_block(WMADecodeContext *s)
324
{
325
    int n, v, a, ch, code, bsize;
326
    int coef_nb_bits, total_gain, parse_exponents;
327
    int nb_coefs[MAX_CHANNELS];
328
    float mdct_norm;
329

    
330
#ifdef TRACE
331
    tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
332
#endif
333

    
334
    /* compute current block length */
335
    if (s->use_variable_block_len) {
336
        n = av_log2(s->nb_block_sizes - 1) + 1;
337

    
338
        if (s->reset_block_lengths) {
339
            s->reset_block_lengths = 0;
340
            v = get_bits(&s->gb, n);
341
            if (v >= s->nb_block_sizes)
342
                return -1;
343
            s->prev_block_len_bits = s->frame_len_bits - v;
344
            v = get_bits(&s->gb, n);
345
            if (v >= s->nb_block_sizes)
346
                return -1;
347
            s->block_len_bits = s->frame_len_bits - v;
348
        } else {
349
            /* update block lengths */
350
            s->prev_block_len_bits = s->block_len_bits;
351
            s->block_len_bits = s->next_block_len_bits;
352
        }
353
        v = get_bits(&s->gb, n);
354
        if (v >= s->nb_block_sizes)
355
            return -1;
356
        s->next_block_len_bits = s->frame_len_bits - v;
357
    } else {
358
        /* fixed block len */
359
        s->next_block_len_bits = s->frame_len_bits;
360
        s->prev_block_len_bits = s->frame_len_bits;
361
        s->block_len_bits = s->frame_len_bits;
362
    }
363

    
364
    /* now check if the block length is coherent with the frame length */
365
    s->block_len = 1 << s->block_len_bits;
366
    if ((s->block_pos + s->block_len) > s->frame_len)
367
        return -1;
368

    
369
    if (s->nb_channels == 2) {
370
        s->ms_stereo = get_bits(&s->gb, 1);
371
    }
372
    v = 0;
373
    for(ch = 0; ch < s->nb_channels; ch++) {
374
        a = get_bits(&s->gb, 1);
375
        s->channel_coded[ch] = a;
376
        v |= a;
377
    }
378
    /* if no channel coded, no need to go further */
379
    /* XXX: fix potential framing problems */
380
    if (!v)
381
        goto next;
382

    
383
    bsize = s->frame_len_bits - s->block_len_bits;
384

    
385
    /* read total gain and extract corresponding number of bits for
386
       coef escape coding */
387
    total_gain = 1;
388
    for(;;) {
389
        a = get_bits(&s->gb, 7);
390
        total_gain += a;
391
        if (a != 127)
392
            break;
393
    }
394

    
395
    coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
396

    
397
    /* compute number of coefficients */
398
    n = s->coefs_end[bsize] - s->coefs_start;
399
    for(ch = 0; ch < s->nb_channels; ch++)
400
        nb_coefs[ch] = n;
401

    
402
    /* complex coding */
403
    if (s->use_noise_coding) {
404

    
405
        for(ch = 0; ch < s->nb_channels; ch++) {
406
            if (s->channel_coded[ch]) {
407
                int i, n, a;
408
                n = s->exponent_high_sizes[bsize];
409
                for(i=0;i<n;i++) {
410
                    a = get_bits(&s->gb, 1);
411
                    s->high_band_coded[ch][i] = a;
412
                    /* if noise coding, the coefficients are not transmitted */
413
                    if (a)
414
                        nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
415
                }
416
            }
417
        }
418
        for(ch = 0; ch < s->nb_channels; ch++) {
419
            if (s->channel_coded[ch]) {
420
                int i, n, val, code;
421

    
422
                n = s->exponent_high_sizes[bsize];
423
                val = (int)0x80000000;
424
                for(i=0;i<n;i++) {
425
                    if (s->high_band_coded[ch][i]) {
426
                        if (val == (int)0x80000000) {
427
                            val = get_bits(&s->gb, 7) - 19;
428
                        } else {
429
                            code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
430
                            if (code < 0)
431
                                return -1;
432
                            val += code - 18;
433
                        }
434
                        s->high_band_values[ch][i] = val;
435
                    }
436
                }
437
            }
438
        }
439
    }
440

    
441
    /* exposant can be interpolated in short blocks. */
442
    parse_exponents = 1;
443
    if (s->block_len_bits != s->frame_len_bits) {
444
        parse_exponents = get_bits(&s->gb, 1);
445
    }
446

    
447
    if (parse_exponents) {
448
        for(ch = 0; ch < s->nb_channels; ch++) {
449
            if (s->channel_coded[ch]) {
450
                if (s->use_exp_vlc) {
451
                    if (decode_exp_vlc(s, ch) < 0)
452
                        return -1;
453
                } else {
454
                    decode_exp_lsp(s, ch);
455
                }
456
            }
457
        }
458
    } else {
459
        for(ch = 0; ch < s->nb_channels; ch++) {
460
            if (s->channel_coded[ch]) {
461
                interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits,
462
                                  s->block_len);
463
            }
464
        }
465
    }
466

    
467
    /* parse spectral coefficients : just RLE encoding */
468
    for(ch = 0; ch < s->nb_channels; ch++) {
469
        if (s->channel_coded[ch]) {
470
            VLC *coef_vlc;
471
            int level, run, sign, tindex;
472
            int16_t *ptr, *eptr;
473
            const uint16_t *level_table, *run_table;
474

    
475
            /* special VLC tables are used for ms stereo because
476
               there is potentially less energy there */
477
            tindex = (ch == 1 && s->ms_stereo);
478
            coef_vlc = &s->coef_vlc[tindex];
479
            run_table = s->run_table[tindex];
480
            level_table = s->level_table[tindex];
481
            /* XXX: optimize */
482
            ptr = &s->coefs1[ch][0];
483
            eptr = ptr + nb_coefs[ch];
484
            memset(ptr, 0, s->block_len * sizeof(int16_t));
485
            for(;;) {
486
                code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
487
                if (code < 0)
488
                    return -1;
489
                if (code == 1) {
490
                    /* EOB */
491
                    break;
492
                } else if (code == 0) {
493
                    /* escape */
494
                    level = get_bits(&s->gb, coef_nb_bits);
495
                    /* NOTE: this is rather suboptimal. reading
496
                       block_len_bits would be better */
497
                    run = get_bits(&s->gb, s->frame_len_bits);
498
                } else {
499
                    /* normal code */
500
                    run = run_table[code];
501
                    level = level_table[code];
502
                }
503
                sign = get_bits(&s->gb, 1);
504
                if (!sign)
505
                    level = -level;
506
                ptr += run;
507
                if (ptr >= eptr)
508
                {
509
                    av_log(NULL, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");
510
                    break;
511
                }
512
                *ptr++ = level;
513
                /* NOTE: EOB can be omitted */
514
                if (ptr >= eptr)
515
                    break;
516
            }
517
        }
518
        if (s->version == 1 && s->nb_channels >= 2) {
519
            align_get_bits(&s->gb);
520
        }
521
    }
522

    
523
    /* normalize */
524
    {
525
        int n4 = s->block_len / 2;
526
        mdct_norm = 1.0 / (float)n4;
527
        if (s->version == 1) {
528
            mdct_norm *= sqrt(n4);
529
        }
530
    }
531

    
532
    /* finally compute the MDCT coefficients */
533
    for(ch = 0; ch < s->nb_channels; ch++) {
534
        if (s->channel_coded[ch]) {
535
            int16_t *coefs1;
536
            float *coefs, *exponents, mult, mult1, noise, *exp_ptr;
537
            int i, j, n, n1, last_high_band;
538
            float exp_power[HIGH_BAND_MAX_SIZE];
539

    
540
            coefs1 = s->coefs1[ch];
541
            exponents = s->exponents[ch];
542
            mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
543
            mult *= mdct_norm;
544
            coefs = s->coefs[ch];
545
            if (s->use_noise_coding) {
546
                mult1 = mult;
547
                /* very low freqs : noise */
548
                for(i = 0;i < s->coefs_start; i++) {
549
                    *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1;
550
                    s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
551
                }
552

    
553
                n1 = s->exponent_high_sizes[bsize];
554

    
555
                /* compute power of high bands */
556
                exp_ptr = exponents +
557
                    s->high_band_start[bsize] -
558
                    s->coefs_start;
559
                last_high_band = 0; /* avoid warning */
560
                for(j=0;j<n1;j++) {
561
                    n = s->exponent_high_bands[s->frame_len_bits -
562
                                              s->block_len_bits][j];
563
                    if (s->high_band_coded[ch][j]) {
564
                        float e2, v;
565
                        e2 = 0;
566
                        for(i = 0;i < n; i++) {
567
                            v = exp_ptr[i];
568
                            e2 += v * v;
569
                        }
570
                        exp_power[j] = e2 / n;
571
                        last_high_band = j;
572
                        tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
573
                    }
574
                    exp_ptr += n;
575
                }
576

    
577
                /* main freqs and high freqs */
578
                for(j=-1;j<n1;j++) {
579
                    if (j < 0) {
580
                        n = s->high_band_start[bsize] -
581
                            s->coefs_start;
582
                    } else {
583
                        n = s->exponent_high_bands[s->frame_len_bits -
584
                                                  s->block_len_bits][j];
585
                    }
586
                    if (j >= 0 && s->high_band_coded[ch][j]) {
587
                        /* use noise with specified power */
588
                        mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
589
                        /* XXX: use a table */
590
                        mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
591
                        mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
592
                        mult1 *= mdct_norm;
593
                        for(i = 0;i < n; i++) {
594
                            noise = s->noise_table[s->noise_index];
595
                            s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
596
                            *coefs++ = (*exponents++) * noise * mult1;
597
                        }
598
                    } else {
599
                        /* coded values + small noise */
600
                        for(i = 0;i < n; i++) {
601
                            noise = s->noise_table[s->noise_index];
602
                            s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
603
                            *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult;
604
                        }
605
                    }
606
                }
607

    
608
                /* very high freqs : noise */
609
                n = s->block_len - s->coefs_end[bsize];
610
                mult1 = mult * exponents[-1];
611
                for(i = 0; i < n; i++) {
612
                    *coefs++ = s->noise_table[s->noise_index] * mult1;
613
                    s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
614
                }
615
            } else {
616
                /* XXX: optimize more */
617
                for(i = 0;i < s->coefs_start; i++)
618
                    *coefs++ = 0.0;
619
                n = nb_coefs[ch];
620
                for(i = 0;i < n; i++) {
621
                    *coefs++ = coefs1[i] * exponents[i] * mult;
622
                }
623
                n = s->block_len - s->coefs_end[bsize];
624
                for(i = 0;i < n; i++)
625
                    *coefs++ = 0.0;
626
            }
627
        }
628
    }
629

    
630
#ifdef TRACE
631
    for(ch = 0; ch < s->nb_channels; ch++) {
632
        if (s->channel_coded[ch]) {
633
            dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
634
            dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
635
        }
636
    }
637
#endif
638

    
639
    if (s->ms_stereo && s->channel_coded[1]) {
640
        float a, b;
641
        int i;
642

    
643
        /* nominal case for ms stereo: we do it before mdct */
644
        /* no need to optimize this case because it should almost
645
           never happen */
646
        if (!s->channel_coded[0]) {
647
            tprintf(s->avctx, "rare ms-stereo case happened\n");
648
            memset(s->coefs[0], 0, sizeof(float) * s->block_len);
649
            s->channel_coded[0] = 1;
650
        }
651

    
652
        for(i = 0; i < s->block_len; i++) {
653
            a = s->coefs[0][i];
654
            b = s->coefs[1][i];
655
            s->coefs[0][i] = a + b;
656
            s->coefs[1][i] = a - b;
657
        }
658
    }
659

    
660
    /* build the window : we ensure that when the windows overlap
661
       their squared sum is always 1 (MDCT reconstruction rule) */
662
    /* XXX: merge with output */
663
    {
664
        int i, next_block_len, block_len, prev_block_len, n;
665
        float *wptr;
666

    
667
        block_len = s->block_len;
668
        prev_block_len = 1 << s->prev_block_len_bits;
669
        next_block_len = 1 << s->next_block_len_bits;
670

    
671
        /* right part */
672
        wptr = s->window + block_len;
673
        if (block_len <= next_block_len) {
674
            for(i=0;i<block_len;i++)
675
                *wptr++ = s->windows[bsize][i];
676
        } else {
677
            /* overlap */
678
            n = (block_len / 2) - (next_block_len / 2);
679
            for(i=0;i<n;i++)
680
                *wptr++ = 1.0;
681
            for(i=0;i<next_block_len;i++)
682
                *wptr++ = s->windows[s->frame_len_bits - s->next_block_len_bits][i];
683
            for(i=0;i<n;i++)
684
                *wptr++ = 0.0;
685
        }
686

    
687
        /* left part */
688
        wptr = s->window + block_len;
689
        if (block_len <= prev_block_len) {
690
            for(i=0;i<block_len;i++)
691
                *--wptr = s->windows[bsize][i];
692
        } else {
693
            /* overlap */
694
            n = (block_len / 2) - (prev_block_len / 2);
695
            for(i=0;i<n;i++)
696
                *--wptr = 1.0;
697
            for(i=0;i<prev_block_len;i++)
698
                *--wptr = s->windows[s->frame_len_bits - s->prev_block_len_bits][i];
699
            for(i=0;i<n;i++)
700
                *--wptr = 0.0;
701
        }
702
    }
703

    
704

    
705
    for(ch = 0; ch < s->nb_channels; ch++) {
706
        if (s->channel_coded[ch]) {
707
            float *ptr;
708
            int n4, index, n;
709

    
710
            n = s->block_len;
711
            n4 = s->block_len / 2;
712
            s->mdct_ctx[bsize].fft.imdct_calc(&s->mdct_ctx[bsize],
713
                          s->output, s->coefs[ch], s->mdct_tmp);
714

    
715
            /* XXX: optimize all that by build the window and
716
               multipying/adding at the same time */
717

    
718
            /* multiply by the window and add in the frame */
719
            index = (s->frame_len / 2) + s->block_pos - n4;
720
            ptr = &s->frame_out[ch][index];
721
            s->dsp.vector_fmul_add_add(ptr,s->window,s->output,ptr,0,2*n,1);
722

    
723
            /* specific fast case for ms-stereo : add to second
724
               channel if it is not coded */
725
            if (s->ms_stereo && !s->channel_coded[1]) {
726
                ptr = &s->frame_out[1][index];
727
                s->dsp.vector_fmul_add_add(ptr,s->window,s->output,ptr,0,2*n,1);
728
            }
729
        }
730
    }
731
 next:
732
    /* update block number */
733
    s->block_num++;
734
    s->block_pos += s->block_len;
735
    if (s->block_pos >= s->frame_len)
736
        return 1;
737
    else
738
        return 0;
739
}
740

    
741
/* decode a frame of frame_len samples */
742
static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
743
{
744
    int ret, i, n, a, ch, incr;
745
    int16_t *ptr;
746
    float *iptr;
747

    
748
#ifdef TRACE
749
    tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
750
#endif
751

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

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

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

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

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

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

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

    
809
    samples = data;
810

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

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

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

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

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

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

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

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

    
879
//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);
880

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

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

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