Statistics
| Branch: | Revision:

ffmpeg / libavcodec / wmadec.c @ bc423886

History | View | Annotate | Download (41.3 KB)

1
/*
2
 * WMA compatible decoder
3
 * Copyright (c) 2002 The FFmpeg Project.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
#include "avcodec.h"
20
#include "dsputil.h"
21

    
22
//#define DEBUG_PARAMS
23
//#define DEBUG_TRACE
24

    
25
/* size of blocks */
26
#define BLOCK_MIN_BITS 7
27
#define BLOCK_MAX_BITS 11
28
#define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS)
29

    
30
#define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
31

    
32
/* XXX: find exact max size */
33
#define HIGH_BAND_MAX_SIZE 16
34

    
35
#define NB_LSP_COEFS 10
36

    
37
/* XXX: is it a suitable value ? */
38
#define MAX_CODED_SUPERFRAME_SIZE 4096
39

    
40
#define MAX_CHANNELS 2
41

    
42
#define NOISE_TAB_SIZE 8192
43

    
44
#define LSP_POW_BITS 7
45

    
46
typedef struct WMADecodeContext {
47
    GetBitContext gb;
48
    int sample_rate;
49
    int nb_channels;
50
    int bit_rate;
51
    int version; /* 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) */
52
    int block_align;
53
    int use_bit_reservoir;
54
    int use_variable_block_len;
55
    int use_exp_vlc;  /* exponent coding: 0 = lsp, 1 = vlc + delta */
56
    int use_noise_coding; /* true if perceptual noise is added */
57
    int byte_offset_bits;
58
    VLC exp_vlc;
59
    int exponent_sizes[BLOCK_NB_SIZES];
60
    uint16_t exponent_bands[BLOCK_NB_SIZES][25];
61
    int high_band_start[BLOCK_NB_SIZES]; /* index of first coef in high band */
62
    int coefs_start;               /* first coded coef */
63
    int coefs_end[BLOCK_NB_SIZES]; /* max number of coded coefficients */
64
    int exponent_high_sizes[BLOCK_NB_SIZES];
65
    int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE]; 
66
    VLC hgain_vlc;
67
    
68
    /* coded values in high bands */
69
    int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
70
    int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
71

    
72
    /* there are two possible tables for spectral coefficients */
73
    VLC coef_vlc[2];
74
    uint16_t *run_table[2];
75
    uint16_t *level_table[2];
76
    /* frame info */
77
    int frame_len;       /* frame length in samples */
78
    int frame_len_bits;  /* frame_len = 1 << frame_len_bits */
79
    int nb_block_sizes;  /* number of block sizes */
80
    /* block info */
81
    int reset_block_lengths;
82
    int block_len_bits; /* log2 of current block length */
83
    int next_block_len_bits; /* log2 of next block length */
84
    int prev_block_len_bits; /* log2 of prev block length */
85
    int block_len; /* block length in samples */
86
    int block_num; /* block number in current frame */
87
    int block_pos; /* current position in frame */
88
    uint8_t ms_stereo; /* true if mid/side stereo mode */
89
    uint8_t channel_coded[MAX_CHANNELS]; /* true if channel is coded */
90
    float exponents[MAX_CHANNELS][BLOCK_MAX_SIZE];
91
    float max_exponent[MAX_CHANNELS];
92
    int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
93
    float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE];
94
    MDCTContext mdct_ctx[BLOCK_NB_SIZES];
95
    float *windows[BLOCK_NB_SIZES];
96
    FFTSample mdct_tmp[BLOCK_MAX_SIZE]; /* temporary storage for imdct */
97
    /* output buffer for one frame and the last for IMDCT windowing */
98
    float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2];
99
    /* last frame info */
100
    uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + 4]; /* padding added */
101
    int last_bitoffset;
102
    int last_superframe_len;
103
    float noise_table[NOISE_TAB_SIZE];
104
    int noise_index;
105
    float noise_mult; /* XXX: suppress that and integrate it in the noise array */
106
    /* lsp_to_curve tables */
107
    float lsp_cos_table[BLOCK_MAX_SIZE];
108
    float lsp_pow_e_table[256];
109
    float lsp_pow_m_table1[(1 << LSP_POW_BITS)];
110
    float lsp_pow_m_table2[(1 << LSP_POW_BITS)];
111
} WMADecodeContext;
112

    
113
typedef struct CoefVLCTable {
114
    int n; /* total number of codes */
115
    const uint32_t *huffcodes; /* VLC bit values */
116
    const uint8_t *huffbits;   /* VLC bit size */
117
    const uint16_t *levels; /* table to build run/level tables */
118
} CoefVLCTable;
119

    
120
static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
121

    
122
#include "wmadata.h"
123

    
124
#ifdef DEBUG_TRACE
125

    
126
int frame_count;
127

    
128
static FILE *flog;
129

    
130
void trace(const char *fmt, ...)
131
{
132
    va_list ap;
133
    
134

    
135
    if (!flog) {
136
        flog = fopen("/tmp/out.log", "w");
137
        setlinebuf(flog);
138
    }
139

    
140
    va_start(ap, fmt);
141
    vfprintf(flog, fmt, ap);
142
    va_end(ap);
143
}
144

    
145
#define get_bits(s, n) get_bits_trace(s, n)
146
#define get_vlc(s, vlc) get_vlc_trace(s, vlc)
147

    
148
unsigned int get_bits_trace(GetBitContext *s, int n)
149
{
150
    unsigned int val;
151
    val = (get_bits)(s, n);
152
    trace("get_bits(%d) : 0x%x\n", n, val);
153
    return val;
154
}
155

    
156
static int get_vlc_trace(GetBitContext *s, VLC *vlc)
157
{
158
    int code;
159
    code = (get_vlc)(s, vlc);
160
    trace("get_vlc() : %d\n", code);
161
    return code;
162
}
163

    
164
static void dump_shorts(const char *name, const short *tab, int n)
165
{
166
    int i;
167

    
168
    trace("%s[%d]:\n", name, n);
169
    for(i=0;i<n;i++) {
170
        if ((i & 7) == 0)
171
            trace("%4d: ", i);
172
        trace(" %5d.0", tab[i]);
173
        if ((i & 7) == 7)
174
            trace("\n");
175
    }
176
}
177

    
178
static void dump_floats(const char *name, int prec, const float *tab, int n)
179
{
180
    int i;
181

    
182
    trace("%s[%d]:\n", name, n);
183
    for(i=0;i<n;i++) {
184
        if ((i & 7) == 0)
185
            trace("%4d: ", i);
186
        trace(" %8.*f", prec, tab[i]);
187
        if ((i & 7) == 7)
188
            trace("\n");
189
    }
190
    if ((i & 7) != 0)
191
        trace("\n");
192
}
193

    
194
#else
195

    
196
#define trace(fmt, ...)
197

    
198
#endif
199

    
200
/* XXX: use same run/length optimization as mpeg decoders */
201
static void init_coef_vlc(VLC *vlc, 
202
                          uint16_t **prun_table, uint16_t **plevel_table,
203
                          const CoefVLCTable *vlc_table)
204
{
205
    int n = vlc_table->n;
206
    const uint8_t *table_bits = vlc_table->huffbits;
207
    const uint32_t *table_codes = vlc_table->huffcodes;
208
    const uint16_t *levels_table = vlc_table->levels;
209
    uint16_t *run_table, *level_table;
210
    const uint16_t *p;
211
    int i, l, j, level;
212

    
213
    init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4);
214

    
215
    run_table = malloc(n * sizeof(uint16_t));
216
    level_table = malloc(n * sizeof(uint16_t));
217
    p = levels_table;
218
    i = 2;
219
    level = 1;
220
    while (i < n) {
221
        l = *p++;
222
        for(j=0;j<l;j++) {
223
            run_table[i] = j;
224
            level_table[i] = level;
225
            i++;
226
        }
227
        level++;
228
    }
229
    *prun_table = run_table;
230
    *plevel_table = level_table;
231
}
232

    
233
static int wma_decode_init(AVCodecContext * avctx)
234
{
235
    WMADecodeContext *s = avctx->priv_data;
236
    int i, flags1, flags2;
237
    float *window;
238
    uint8_t *extradata;
239
    float bps1, high_freq, bps;
240
    int sample_rate1;
241
    int coef_vlc_table;
242
    
243
    s->sample_rate = avctx->sample_rate;
244
    s->nb_channels = avctx->channels;
245
    s->bit_rate = avctx->bit_rate;
246
    s->block_align = avctx->block_align;
247

    
248
    if (avctx->codec_id == CODEC_ID_WMAV1) {
249
        s->version = 1;
250
    } else {
251
        s->version = 2;
252
    }
253
    
254
    /* extract flag infos */
255
    flags1 = 0;
256
    flags2 = 0;
257
    extradata = avctx->extradata;
258
    if (s->version == 1 && avctx->extradata_size >= 4) {
259
        flags1 = extradata[0] | (extradata[1] << 8);
260
        flags2 = extradata[2] | (extradata[3] << 8);
261
    } else if (s->version == 2 && avctx->extradata_size >= 6) {
262
        flags1 = extradata[0] | (extradata[1] << 8) | 
263
            (extradata[2] << 16) | (extradata[3] << 24);
264
        flags2 = extradata[4] | (extradata[5] << 8);
265
    }
266
    s->use_exp_vlc = flags2 & 0x0001;
267
    s->use_bit_reservoir = flags2 & 0x0002;
268
    s->use_variable_block_len = flags2 & 0x0004;
269

    
270
    /* compute MDCT block size */
271
    if (s->sample_rate <= 16000) {
272
        s->frame_len_bits = 9;
273
    } else if (s->sample_rate <= 22050 || 
274
               (s->sample_rate <= 32000 && s->version == 1)) {
275
        s->frame_len_bits = 10;
276
    } else {
277
        s->frame_len_bits = 11;
278
    }
279
    s->frame_len = 1 << s->frame_len_bits;
280
    if (s->use_variable_block_len) {
281
        s->nb_block_sizes = s->frame_len_bits - BLOCK_MIN_BITS + 1;
282
    } else {
283
        s->nb_block_sizes = 1;
284
    }
285

    
286
    /* init rate dependant parameters */
287
    s->use_noise_coding = 1;
288
    high_freq = s->sample_rate * 0.5;
289

    
290
    /* if version 2, then the rates are normalized */
291
    sample_rate1 = s->sample_rate;
292
    if (s->version == 2) {
293
        if (sample_rate1 >= 44100) 
294
            sample_rate1 = 44100;
295
        else if (sample_rate1 >= 22050) 
296
            sample_rate1 = 22050;
297
        else if (sample_rate1 >= 16000) 
298
            sample_rate1 = 16000;
299
        else if (sample_rate1 >= 11025) 
300
            sample_rate1 = 11025;
301
        else if (sample_rate1 >= 8000) 
302
            sample_rate1 = 8000;
303
    }
304

    
305
    bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
306
    s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0)) + 2;
307

    
308
    /* compute high frequency value and choose if noise coding should
309
       be activated */
310
    bps1 = bps;
311
    if (s->nb_channels == 2)
312
        bps1 = bps * 1.6;
313
    if (sample_rate1 == 44100) {
314
        if (bps1 >= 0.61)
315
            s->use_noise_coding = 0;
316
        else
317
            high_freq = high_freq * 0.4;
318
    } else if (sample_rate1 == 22050) {
319
        if (bps1 >= 1.16)
320
            s->use_noise_coding = 0;
321
        else if (bps1 >= 0.72) 
322
            high_freq = high_freq * 0.7;
323
        else
324
            high_freq = high_freq * 0.6;
325
    } else if (sample_rate1 == 16000) {
326
        if (bps > 0.5)
327
            high_freq = high_freq * 0.5;
328
        else
329
            high_freq = high_freq * 0.3;
330
    } else if (sample_rate1 == 11025) {
331
        high_freq = high_freq * 0.7;
332
    } else if (sample_rate1 == 8000) {
333
        if (bps <= 0.625) {
334
            high_freq = high_freq * 0.5;
335
        } else if (bps > 0.75) {
336
            s->use_noise_coding = 0;
337
        } else {
338
            high_freq = high_freq * 0.65;
339
        }
340
    } else {
341
        if (bps >= 0.8) {
342
            high_freq = high_freq * 0.75;
343
        } else if (bps >= 0.6) {
344
            high_freq = high_freq * 0.6;
345
        } else {
346
            high_freq = high_freq * 0.5;
347
        }
348
    }
349
#ifdef DEBUG_PARAMS
350
    printf("flags1=0x%x flags2=0x%x\n", flags1, flags2);
351
    printf("version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
352
           s->version, s->nb_channels, s->sample_rate, s->bit_rate, 
353
           s->block_align);
354
    printf("bps=%f bps1=%f high_freq=%f bitoffset=%d\n", 
355
           bps, bps1, high_freq, s->byte_offset_bits);
356
    printf("use_noise_coding=%d use_exp_vlc=%d\n",
357
           s->use_noise_coding, s->use_exp_vlc);
358
#endif
359

    
360
    /* compute the scale factor band sizes for each MDCT block size */
361
    {
362
        int a, b, pos, lpos, k, block_len, i, j, n;
363
        const uint8_t *table;
364
        
365
        if (s->version == 1) {
366
            s->coefs_start = 3;
367
        } else {
368
            s->coefs_start = 0;
369
        }
370
        for(k = 0; k < s->nb_block_sizes; k++) {
371
            block_len = s->frame_len >> k;
372

    
373
            if (s->version == 1) {
374
                lpos = 0;
375
                for(i=0;i<25;i++) {
376
                    a = wma_critical_freqs[i];
377
                    b = s->sample_rate;
378
                    pos = ((block_len * 2 * a)  + (b >> 1)) / b;
379
                    if (pos > block_len) 
380
                        pos = block_len;
381
                    s->exponent_bands[0][i] = pos - lpos;
382
                    if (pos >= block_len) {
383
                        i++;
384
                        break;
385
                    }
386
                    lpos = pos;
387
                }
388
                s->exponent_sizes[0] = i;
389
            } else {
390
                /* hardcoded tables */
391
                table = NULL;
392
                a = s->frame_len_bits - BLOCK_MIN_BITS - k;
393
                if (a < 3) {
394
                    if (s->sample_rate >= 44100)
395
                        table = exponent_band_44100[a];
396
                    else if (s->sample_rate >= 32000)
397
                        table = exponent_band_32000[a];
398
                    else if (s->sample_rate >= 22050)
399
                        table = exponent_band_22050[a];
400
                }
401
                if (table) {
402
                    n = *table++;
403
                    for(i=0;i<n;i++)
404
                        s->exponent_bands[k][i] = table[i];
405
                    s->exponent_sizes[k] = n;
406
                } else {
407
                    j = 0;
408
                    lpos = 0;
409
                    for(i=0;i<25;i++) {
410
                        a = wma_critical_freqs[i];
411
                        b = s->sample_rate;
412
                        pos = ((block_len * 2 * a)  + (b << 1)) / (4 * b);
413
                        pos <<= 2;
414
                        if (pos > block_len) 
415
                            pos = block_len;
416
                        if (pos > lpos)
417
                            s->exponent_bands[k][j++] = pos - lpos;
418
                        if (pos >= block_len)
419
                            break;
420
                        lpos = pos;
421
                    }
422
                    s->exponent_sizes[k] = j;
423
                }
424
            }
425

    
426
            /* max number of coefs */
427
            s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
428
            /* high freq computation */
429
            s->high_band_start[k] = (int)((block_len * 2 * high_freq) / 
430
                                          s->sample_rate + 0.5);
431
            n = s->exponent_sizes[k];
432
            j = 0;
433
            pos = 0;
434
            for(i=0;i<n;i++) {
435
                int start, end;
436
                start = pos;
437
                pos += s->exponent_bands[k][i];
438
                end = pos;
439
                if (start < s->high_band_start[k])
440
                    start = s->high_band_start[k];
441
                if (end > s->coefs_end[k])
442
                    end = s->coefs_end[k];
443
                if (end > start)
444
                    s->exponent_high_bands[k][j++] = end - start;
445
            }
446
            s->exponent_high_sizes[k] = j;
447
#if 0
448
            trace("%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
449
                  s->frame_len >> k, 
450
                  s->coefs_end[k],
451
                  s->high_band_start[k],
452
                  s->exponent_high_sizes[k]);
453
            for(j=0;j<s->exponent_high_sizes[k];j++)
454
                trace(" %d", s->exponent_high_bands[k][j]);
455
            trace("\n");
456
#endif
457
        }
458
    }
459

    
460
#ifdef DEBUG_TRACE
461
    {
462
        int i, j;
463
        for(i = 0; i < s->nb_block_sizes; i++) {
464
            trace("%5d: n=%2d:", 
465
                   s->frame_len >> i, 
466
                   s->exponent_sizes[i]);
467
            for(j=0;j<s->exponent_sizes[i];j++)
468
                trace(" %d", s->exponent_bands[i][j]);
469
            trace("\n");
470
        }
471
    }
472
#endif
473

    
474
    /* init MDCT */
475
    for(i = 0; i < s->nb_block_sizes; i++)
476
        ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
477
    
478
    /* init MDCT windows : simple sinus window */
479
    for(i = 0; i < s->nb_block_sizes; i++) {
480
        int n, j;
481
        float alpha;
482
        n = 1 << (s->frame_len_bits - i);
483
        window = av_malloc(sizeof(float) * n);
484
        alpha = M_PI / (2.0 * n);
485
        for(j=0;j<n;j++) {
486
            window[n - j - 1] = sin((j + 0.5) * alpha);
487
        }
488
        s->windows[i] = window;
489
    }
490

    
491
    s->reset_block_lengths = 1;
492
    
493
    if (s->use_noise_coding) {
494

    
495
        /* init the noise generator */
496
        if (s->use_exp_vlc)
497
            s->noise_mult = 0.02;
498
        else
499
            s->noise_mult = 0.04;
500
               
501
#if defined(DEBUG_TRACE)
502
        for(i=0;i<NOISE_TAB_SIZE;i++)
503
            s->noise_table[i] = 1.0 * s->noise_mult;
504
#else
505
        {
506
            unsigned int seed;
507
            float norm;
508
            seed = 1;
509
            norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
510
            for(i=0;i<NOISE_TAB_SIZE;i++) {
511
                seed = seed * 314159 + 1;
512
                s->noise_table[i] = (float)((int)seed) * norm;
513
            }
514
        }
515
#endif
516
        init_vlc(&s->hgain_vlc, 9, sizeof(hgain_huffbits), 
517
                 hgain_huffbits, 1, 1,
518
                 hgain_huffcodes, 2, 2);
519
    }
520

    
521
    if (s->use_exp_vlc) {
522
        init_vlc(&s->exp_vlc, 9, sizeof(scale_huffbits), 
523
                 scale_huffbits, 1, 1,
524
                 scale_huffcodes, 4, 4);
525
    } else {
526
        wma_lsp_to_curve_init(s, s->frame_len);
527
    }
528

    
529
    /* choose the VLC tables for the coefficients */
530
    coef_vlc_table = 2;
531
    if (s->sample_rate >= 32000) {
532
        if (bps1 < 0.72)
533
            coef_vlc_table = 0;
534
        else if (bps1 < 1.16)
535
            coef_vlc_table = 1;
536
    }
537

    
538
    init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
539
                  &coef_vlcs[coef_vlc_table * 2]);
540
    init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
541
                  &coef_vlcs[coef_vlc_table * 2 + 1]);
542
    return 0;
543
}
544

    
545
/* interpolate values for a bigger or smaller block. The block must
546
   have multiple sizes */
547
static void interpolate_array(float *scale, int old_size, int new_size)
548
{
549
    int i, j, jincr, k;
550
    float v;
551

    
552
    if (new_size > old_size) {
553
        jincr = new_size / old_size;
554
        j = new_size;
555
        for(i = old_size - 1; i >=0; i--) {
556
            v = scale[i];
557
            k = jincr;
558
            do {
559
                scale[--j] = v;
560
            } while (--k);
561
        }
562
    } else if (new_size < old_size) {
563
        j = 0;
564
        jincr = old_size / new_size;
565
        for(i = 0; i < new_size; i++) {
566
            scale[i] = scale[j];
567
            j += jincr;
568
        }
569
    }
570
}
571

    
572
/* compute x^-0.25 with an exponent and mantissa table. We use linear
573
   interpolation to reduce the mantissa table size at a small speed
574
   expense (linear interpolation approximately doubles the number of
575
   bits of precision). */
576
static inline float pow_m1_4(WMADecodeContext *s, float x)
577
{
578
    union {
579
        float f;
580
        unsigned int v;
581
    } u, t;
582
    unsigned int e, m;
583
    float a, b;
584

    
585
    u.f = x;
586
    e = u.v >> 23;
587
    m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
588
    /* build interpolation scale: 1 <= t < 2. */
589
    t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
590
    a = s->lsp_pow_m_table1[m];
591
    b = s->lsp_pow_m_table2[m];
592
    return s->lsp_pow_e_table[e] * (a + b * t.f);
593
}
594

    
595
static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
596
{  
597
    float wdel, a, b;
598
    int i, e, m;
599

    
600
    wdel = M_PI / frame_len;
601
    for(i=0;i<frame_len;i++)
602
        s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
603

    
604
    /* tables for x^-0.25 computation */
605
    for(i=0;i<256;i++) {
606
        e = i - 126;
607
        s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
608
    }
609

    
610
    /* NOTE: these two tables are needed to avoid two operations in
611
       pow_m1_4 */
612
    b = 1.0;
613
    for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
614
        m = (1 << LSP_POW_BITS) + i;
615
        a = (float)m * (0.5 / (1 << LSP_POW_BITS));
616
        a = pow(a, -0.25);
617
        s->lsp_pow_m_table1[i] = 2 * a - b;
618
        s->lsp_pow_m_table2[i] = b - a;
619
        b = a;
620
    }
621
#if 0
622
    for(i=1;i<20;i++) {
623
        float v, r1, r2;
624
        v = 5.0 / i;
625
        r1 = pow_m1_4(s, v);
626
        r2 = pow(v,-0.25);
627
        printf("%f^-0.25=%f e=%f\n", v, r1, r2 - r1);
628
    }
629
#endif
630
}
631

    
632
/* NOTE: We use the same code as Vorbis here */
633
/* XXX: optimize it further with SSE/3Dnow */
634
static void wma_lsp_to_curve(WMADecodeContext *s, 
635
                             float *out, float *val_max_ptr, 
636
                             int n, float *lsp)
637
{
638
    int i, j;
639
    float p, q, w, v, val_max;
640

    
641
    val_max = 0;
642
    for(i=0;i<n;i++) {
643
        p = 0.5f;
644
        q = 0.5f;
645
        w = s->lsp_cos_table[i];
646
        for(j=1;j<NB_LSP_COEFS;j+=2){
647
            q *= w - lsp[j - 1];
648
            p *= w - lsp[j];
649
        }
650
        p *= p * (2.0f - w);
651
        q *= q * (2.0f + w);
652
        v = p + q;
653
        v = pow_m1_4(s, v);
654
        if (v > val_max)
655
            val_max = v;
656
        out[i] = v;
657
    }
658
    *val_max_ptr = val_max;
659
}
660

    
661
/* decode exponents coded with LSP coefficients (same idea as Vorbis) */
662
static void decode_exp_lsp(WMADecodeContext *s, int ch)
663
{
664
    float lsp_coefs[NB_LSP_COEFS];
665
    int val, i;
666

    
667
    for(i = 0; i < NB_LSP_COEFS; i++) {
668
        if (i == 0 || i >= 8)
669
            val = get_bits(&s->gb, 3);
670
        else
671
            val = get_bits(&s->gb, 4);
672
        lsp_coefs[i] = lsp_codebook[i][val];
673
    }
674

    
675
    wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
676
                     s->block_len, lsp_coefs);
677
}
678

    
679
/* decode exponents coded with VLC codes */
680
static int decode_exp_vlc(WMADecodeContext *s, int ch)
681
{
682
    int last_exp, n, code;
683
    const uint16_t *ptr, *band_ptr;
684
    float v, *q, max_scale, *q_end;
685
    
686
    band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
687
    ptr = band_ptr;
688
    q = s->exponents[ch];
689
    q_end = q + s->block_len;
690
    max_scale = 0;
691
    if (s->version == 1) {
692
        last_exp = get_bits(&s->gb, 5) + 10;
693
        /* XXX: use a table */
694
        v = pow(10, last_exp * (1.0 / 16.0));
695
        max_scale = v;
696
        n = *ptr++;
697
        do {
698
            *q++ = v;
699
        } while (--n);
700
    }
701
    last_exp = 36;
702
    while (q < q_end) {
703
        code = get_vlc(&s->gb, &s->exp_vlc);
704
        if (code < 0)
705
            return -1;
706
        /* NOTE: this offset is the same as MPEG4 AAC ! */
707
        last_exp += code - 60;
708
        /* XXX: use a table */
709
        v = pow(10, last_exp * (1.0 / 16.0));
710
        if (v > max_scale)
711
            max_scale = v;
712
        n = *ptr++;
713
        do {
714
            *q++ = v;
715
        } while (--n);
716
    }
717
    s->max_exponent[ch] = max_scale;
718
    return 0;
719
}
720

    
721
/* return 0 if OK. return 1 if last block of frame. return -1 if
722
   unrecorrable error. */
723
static int wma_decode_block(WMADecodeContext *s)
724
{
725
    int n, v, a, ch, code, bsize;
726
    int coef_nb_bits, total_gain, parse_exponents;
727
    float window[BLOCK_MAX_SIZE * 2];
728
    int nb_coefs[MAX_CHANNELS];
729
    float mdct_norm;
730

    
731
    trace("***decode_block: %d:%d\n", frame_count - 1, s->block_num);
732

    
733
    /* compute current block length */
734
    if (s->use_variable_block_len) {
735
        n = av_log2(s->nb_block_sizes - 1) + 1;
736
    
737
        if (s->reset_block_lengths) {
738
            s->reset_block_lengths = 0;
739
            v = get_bits(&s->gb, n);
740
            if (v >= s->nb_block_sizes)
741
                return -1;
742
            s->prev_block_len_bits = s->frame_len_bits - v;
743
            v = get_bits(&s->gb, n);
744
            if (v >= s->nb_block_sizes)
745
                return -1;
746
            s->block_len_bits = s->frame_len_bits - v;
747
        } else {
748
            /* update block lengths */
749
            s->prev_block_len_bits = s->block_len_bits;
750
            s->block_len_bits = s->next_block_len_bits;
751
        }
752
        v = get_bits(&s->gb, n);
753
        if (v >= s->nb_block_sizes)
754
            return -1;
755
        s->next_block_len_bits = s->frame_len_bits - v;
756
    } else {
757
        /* fixed block len */
758
        s->next_block_len_bits = s->frame_len_bits;
759
        s->prev_block_len_bits = s->frame_len_bits;
760
        s->block_len_bits = s->frame_len_bits;
761
    }
762

    
763
    /* now check if the block length is coherent with the frame length */
764
    s->block_len = 1 << s->block_len_bits;
765
    if ((s->block_pos + s->block_len) > s->frame_len)
766
        return -1;
767

    
768
    if (s->nb_channels == 2) {
769
        s->ms_stereo = get_bits(&s->gb, 1);
770
    }
771
    v = 0;
772
    for(ch = 0; ch < s->nb_channels; ch++) {
773
        a = get_bits(&s->gb, 1);
774
        s->channel_coded[ch] = a;
775
        v |= a;
776
    }
777
    /* if no channel coded, no need to go further */
778
    /* XXX: fix potential framing problems */
779
    if (!v)
780
        goto next;
781

    
782
    bsize = s->frame_len_bits - s->block_len_bits;
783

    
784
    /* read total gain and extract corresponding number of bits for
785
       coef escape coding */
786
    total_gain = 1;
787
    for(;;) {
788
        a = get_bits(&s->gb, 7);
789
        total_gain += a;
790
        if (a != 127)
791
            break;
792
    }
793
    
794
    if (total_gain < 15)
795
        coef_nb_bits = 13;
796
    else if (total_gain < 32)
797
        coef_nb_bits = 12;
798
    else if (total_gain < 40)
799
        coef_nb_bits = 11;
800
    else if (total_gain < 45)
801
        coef_nb_bits = 10;
802
    else
803
        coef_nb_bits = 9;
804

    
805
    /* compute number of coefficients */
806
    n = s->coefs_end[bsize] - s->coefs_start;
807
    for(ch = 0; ch < s->nb_channels; ch++)
808
        nb_coefs[ch] = n;
809

    
810
    /* complex coding */
811
    if (s->use_noise_coding) {
812

    
813
        for(ch = 0; ch < s->nb_channels; ch++) {
814
            if (s->channel_coded[ch]) {
815
                int i, n, a;
816
                n = s->exponent_high_sizes[bsize];
817
                for(i=0;i<n;i++) {
818
                    a = get_bits(&s->gb, 1);
819
                    s->high_band_coded[ch][i] = a;
820
                    /* if noise coding, the coefficients are not transmitted */
821
                    if (a)
822
                        nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
823
                }
824
            }
825
        }
826
        for(ch = 0; ch < s->nb_channels; ch++) {
827
            if (s->channel_coded[ch]) {
828
                int i, n, val, code;
829

    
830
                n = s->exponent_high_sizes[bsize];
831
                val = (int)0x80000000;
832
                for(i=0;i<n;i++) {
833
                    if (s->high_band_coded[ch][i]) {
834
                        if (val == (int)0x80000000) {
835
                            val = get_bits(&s->gb, 7) - 19;
836
                        } else {
837
                            code = get_vlc(&s->gb, &s->hgain_vlc);
838
                            if (code < 0)
839
                                return -1;
840
                            val += code - 18;
841
                        }
842
                        s->high_band_values[ch][i] = val;
843
                    }
844
                }
845
            }
846
        }
847
    }
848
           
849
    /* exposant can be interpolated in short blocks. */
850
    parse_exponents = 1;
851
    if (s->block_len_bits != s->frame_len_bits) {
852
        parse_exponents = get_bits(&s->gb, 1);
853
    }
854
    
855
    if (parse_exponents) {
856
        for(ch = 0; ch < s->nb_channels; ch++) {
857
            if (s->channel_coded[ch]) {
858
                if (s->use_exp_vlc) {
859
                    if (decode_exp_vlc(s, ch) < 0)
860
                        return -1;
861
                } else {
862
                    decode_exp_lsp(s, ch);
863
                }
864
            }
865
        }
866
    } else {
867
        for(ch = 0; ch < s->nb_channels; ch++) {
868
            if (s->channel_coded[ch]) {
869
                interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits, 
870
                                  s->block_len);
871
            }
872
        }
873
    }
874

    
875
    /* parse spectral coefficients : just RLE encoding */
876
    for(ch = 0; ch < s->nb_channels; ch++) {
877
        if (s->channel_coded[ch]) {
878
            VLC *coef_vlc;
879
            int level, run, sign, tindex;
880
            int16_t *ptr, *eptr;
881
            const int16_t *level_table, *run_table;
882

    
883
            /* special VLC tables are used for ms stereo because
884
               there is potentially less energy there */
885
            tindex = (ch == 1 && s->ms_stereo);
886
            coef_vlc = &s->coef_vlc[tindex];
887
            run_table = s->run_table[tindex];
888
            level_table = s->level_table[tindex];
889
            /* XXX: optimize */
890
            ptr = &s->coefs1[ch][0];
891
            eptr = ptr + nb_coefs[ch];
892
            memset(ptr, 0, s->block_len * sizeof(int16_t));
893
            for(;;) {
894
                code = get_vlc(&s->gb, coef_vlc);
895
                if (code < 0)
896
                    return -1;
897
                if (code == 1) {
898
                    /* EOB */
899
                    break;
900
                } else if (code == 0) {
901
                    /* escape */
902
                    level = get_bits(&s->gb, coef_nb_bits);
903
                    /* NOTE: this is rather suboptimal. reading
904
                       block_len_bits would be better */
905
                    run = get_bits(&s->gb, s->frame_len_bits);
906
                } else {
907
                    /* normal code */
908
                    run = run_table[code];
909
                    level = level_table[code];
910
                }
911
                sign = get_bits(&s->gb, 1);
912
                if (!sign)
913
                    level = -level;
914
                ptr += run;
915
                if (ptr >= eptr)
916
                    return -1;
917
                *ptr++ = level;
918
                /* NOTE: EOB can be omitted */
919
                if (ptr >= eptr)
920
                    break;
921
            }
922
        }
923
        if (s->version == 1 && s->nb_channels >= 2) {
924
            align_get_bits(&s->gb);
925
        }
926
    }
927
     
928
    /* normalize */
929
    {
930
        int n4 = s->block_len / 2;
931
        mdct_norm = 1.0 / (float)n4;
932
        if (s->version == 1) {
933
            mdct_norm *= sqrt(n4);
934
        }
935
    }
936

    
937
    /* finally compute the MDCT coefficients */
938
    for(ch = 0; ch < s->nb_channels; ch++) {
939
        if (s->channel_coded[ch]) {
940
            int16_t *coefs1;
941
            float *coefs, *exponents, mult, mult1, noise, *exp_ptr;
942
            int i, j, n, n1, last_high_band;
943
            float exp_power[HIGH_BAND_MAX_SIZE];
944

    
945
            coefs1 = s->coefs1[ch];
946
            exponents = s->exponents[ch];
947
            mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
948
            mult *= mdct_norm;
949
            coefs = s->coefs[ch];
950
            if (s->use_noise_coding) {
951
                mult1 = mult;
952
                /* very low freqs : noise */
953
                for(i = 0;i < s->coefs_start; i++) {
954
                    *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1;
955
                    s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
956
                }
957
                
958
                n1 = s->exponent_high_sizes[bsize];
959

    
960
                /* compute power of high bands */
961
                exp_ptr = exponents + 
962
                    s->high_band_start[bsize] - 
963
                    s->coefs_start;
964
                last_high_band = 0; /* avoid warning */
965
                for(j=0;j<n1;j++) {
966
                    n = s->exponent_high_bands[s->frame_len_bits - 
967
                                              s->block_len_bits][j];
968
                    if (s->high_band_coded[ch][j]) {
969
                        float e2, v;
970
                        e2 = 0;
971
                        for(i = 0;i < n; i++) {
972
                            v = exp_ptr[i];
973
                            e2 += v * v;
974
                        }
975
                        exp_power[j] = e2 / n;
976
                        last_high_band = j;
977
                        trace("%d: power=%f (%d)\n", j, exp_power[j], n);
978
                    }
979
                    exp_ptr += n;
980
                }
981

    
982
                /* main freqs and high freqs */
983
                for(j=-1;j<n1;j++) {
984
                    if (j < 0) {
985
                        n = s->high_band_start[bsize] - 
986
                            s->coefs_start;
987
                    } else {
988
                        n = s->exponent_high_bands[s->frame_len_bits - 
989
                                                  s->block_len_bits][j];
990
                    }
991
                    if (j >= 0 && s->high_band_coded[ch][j]) {
992
                        /* use noise with specified power */
993
                        mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
994
                        /* XXX: use a table */
995
                        mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
996
                        mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
997
                        mult1 *= mdct_norm;
998
                        for(i = 0;i < n; i++) {
999
                            noise = s->noise_table[s->noise_index];
1000
                            s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1001
                            *coefs++ = (*exponents++) * noise * mult1;
1002
                        }
1003
                    } else {
1004
                        /* coded values + small noise */
1005
                        for(i = 0;i < n; i++) {
1006
                            noise = s->noise_table[s->noise_index];
1007
                            s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1008
                            *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult;
1009
                        }
1010
                    }
1011
                }
1012

    
1013
                /* very high freqs : noise */
1014
                n = s->block_len - s->coefs_end[bsize];
1015
                mult1 = mult * exponents[-1];
1016
                for(i = 0; i < n; i++) {
1017
                    *coefs++ = s->noise_table[s->noise_index] * mult1;
1018
                    s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1019
                }
1020
            } else {
1021
                /* XXX: optimize more */
1022
                for(i = 0;i < s->coefs_start; i++)
1023
                    *coefs++ = 0.0;
1024
                n = nb_coefs[ch];
1025
                for(i = 0;i < n; i++) {
1026
                    *coefs++ = coefs1[i] * exponents[i] * mult;
1027
                }
1028
                n = s->block_len - s->coefs_end[bsize];
1029
                for(i = 0;i < n; i++)
1030
                    *coefs++ = 0.0;
1031
            }
1032
        }
1033
    }
1034

    
1035
#ifdef DEBUG_TRACE
1036
    for(ch = 0; ch < s->nb_channels; ch++) {
1037
        if (s->channel_coded[ch]) {
1038
            dump_floats("exponents", 3, s->exponents[ch], s->block_len);
1039
            dump_floats("coefs", 1, s->coefs[ch], s->block_len);
1040
        }
1041
    }
1042
#endif
1043
    
1044
    if (s->ms_stereo && s->channel_coded[1]) {
1045
        float a, b;
1046
        int i;
1047

    
1048
        /* nominal case for ms stereo: we do it before mdct */
1049
        /* no need to optimize this case because it should almost
1050
           never happen */
1051
        if (!s->channel_coded[0]) {
1052
#ifdef DEBUG_TRACE
1053
            trace("rare ms-stereo case happened\n");
1054
#endif
1055
            memset(s->coefs[0], 0, sizeof(float) * s->block_len);
1056
            s->channel_coded[0] = 1;
1057
        }
1058
        
1059
        for(i = 0; i < s->block_len; i++) {
1060
            a = s->coefs[0][i];
1061
            b = s->coefs[1][i];
1062
            s->coefs[0][i] = a + b;
1063
            s->coefs[1][i] = a - b;
1064
        }
1065
    }
1066

    
1067
    /* build the window : we ensure that when the windows overlap
1068
       their squared sum is always 1 (MDCT reconstruction rule) */
1069
    /* XXX: merge with output */
1070
    {
1071
        int i, next_block_len, block_len, prev_block_len, n;
1072
        float *wptr;
1073

    
1074
        block_len = s->block_len;
1075
        prev_block_len = 1 << s->prev_block_len_bits;
1076
        next_block_len = 1 << s->next_block_len_bits;
1077

    
1078
        /* right part */
1079
        wptr = window + block_len;
1080
        if (block_len <= next_block_len) {
1081
            for(i=0;i<block_len;i++)
1082
                *wptr++ = s->windows[bsize][i];
1083
        } else {
1084
            /* overlap */
1085
            n = (block_len / 2) - (next_block_len / 2);
1086
            for(i=0;i<n;i++)
1087
                *wptr++ = 1.0;
1088
            for(i=0;i<next_block_len;i++)
1089
                *wptr++ = s->windows[s->frame_len_bits - s->next_block_len_bits][i];
1090
            for(i=0;i<n;i++)
1091
                *wptr++ = 0.0;
1092
        }
1093

    
1094
        /* left part */
1095
        wptr = window + block_len;
1096
        if (block_len <= prev_block_len) {
1097
            for(i=0;i<block_len;i++)
1098
                *--wptr = s->windows[bsize][i];
1099
        } else {
1100
            /* overlap */
1101
            n = (block_len / 2) - (prev_block_len / 2);
1102
            for(i=0;i<n;i++)
1103
                *--wptr = 1.0;
1104
            for(i=0;i<prev_block_len;i++)
1105
                *--wptr = s->windows[s->frame_len_bits - s->prev_block_len_bits][i];
1106
            for(i=0;i<n;i++)
1107
                *--wptr = 0.0;
1108
        }
1109
    }
1110

    
1111
    
1112
    for(ch = 0; ch < s->nb_channels; ch++) {
1113
        if (s->channel_coded[ch]) {
1114
            FFTSample output[BLOCK_MAX_SIZE * 2];
1115
            float *ptr;
1116
            int i, n4, index, n;
1117

    
1118
            n = s->block_len;
1119
            n4 = s->block_len / 2;
1120
            ff_imdct_calc(&s->mdct_ctx[bsize], 
1121
                          output, s->coefs[ch], s->mdct_tmp);
1122

    
1123
            /* XXX: optimize all that by build the window and
1124
               multipying/adding at the same time */
1125
            /* multiply by the window */
1126
            for(i=0;i<n * 2;i++) {
1127
                output[i] *= window[i];
1128
            }
1129

    
1130
            /* add in the frame */
1131
            index = (s->frame_len / 2) + s->block_pos - n4;
1132
            ptr = &s->frame_out[ch][index];
1133
            for(i=0;i<n * 2;i++) {
1134
                *ptr += output[i];
1135
                ptr++;
1136
            }
1137

    
1138
            /* specific fast case for ms-stereo : add to second
1139
               channel if it is not coded */
1140
            if (s->ms_stereo && !s->channel_coded[1]) {
1141
                ptr = &s->frame_out[1][index];
1142
                for(i=0;i<n * 2;i++) {
1143
                    *ptr += output[i];
1144
                    ptr++;
1145
                }
1146
            }
1147
        }
1148
    }
1149
 next:
1150
    /* update block number */
1151
    s->block_num++;
1152
    s->block_pos += s->block_len;
1153
    if (s->block_pos >= s->frame_len)
1154
        return 1;
1155
    else
1156
        return 0;
1157
}
1158

    
1159
/* decode a frame of frame_len samples */
1160
static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
1161
{
1162
    int ret, i, n, a, ch, incr;
1163
    int16_t *ptr;
1164
    float *iptr;
1165

    
1166
    trace("***decode_frame: %d size=%d\n", frame_count++, s->frame_len);
1167

    
1168
    /* read each block */
1169
    s->block_num = 0;
1170
    s->block_pos = 0;
1171
    for(;;) {
1172
        ret = wma_decode_block(s);
1173
        if (ret < 0) 
1174
            return -1;
1175
        if (ret)
1176
            break;
1177
    }
1178

    
1179
    /* convert frame to integer */
1180
    n = s->frame_len;
1181
    incr = s->nb_channels;
1182
    for(ch = 0; ch < s->nb_channels; ch++) {
1183
        ptr = samples + ch;
1184
        iptr = s->frame_out[ch];
1185

    
1186
        for(i=0;i<n;i++) {
1187
            a = lrintf(*iptr++);
1188
            if (a > 32767)
1189
                a = 32767;
1190
            else if (a < -32768)
1191
                a = -32768;
1192
            *ptr = a;
1193
            ptr += incr;
1194
        }
1195
        /* prepare for next block */
1196
        memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
1197
                s->frame_len * sizeof(float));
1198
        /* XXX: suppress this */
1199
        memset(&s->frame_out[ch][s->frame_len], 0, 
1200
               s->frame_len * sizeof(float));
1201
    }
1202

    
1203
#ifdef DEBUG_TRACE
1204
    dump_shorts("samples", samples, n * s->nb_channels);
1205
#endif
1206
    return 0;
1207
}
1208

    
1209
static int wma_decode_superframe(AVCodecContext *avctx, 
1210
                                 void *data, int *data_size,
1211
                                 UINT8 *buf, int buf_size)
1212
{
1213
    WMADecodeContext *s = avctx->priv_data;
1214
    int nb_frames, bit_offset, i, pos, len;
1215
    uint8_t *q;
1216
    int16_t *samples;
1217
    
1218
    trace("***decode_superframe:\n");
1219

    
1220
    samples = data;
1221

    
1222
    init_get_bits(&s->gb, buf, buf_size);
1223
    
1224
    if (s->use_bit_reservoir) {
1225
        /* read super frame header */
1226
        get_bits(&s->gb, 4); /* super frame index */
1227
        nb_frames = get_bits(&s->gb, 4) - 1;
1228

    
1229
        bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
1230

    
1231
        if (s->last_superframe_len > 0) {
1232
            //        printf("skip=%d\n", s->last_bitoffset);
1233
            /* add bit_offset bits to last frame */
1234
            if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) > 
1235
                MAX_CODED_SUPERFRAME_SIZE)
1236
                return -1;
1237
            q = s->last_superframe + s->last_superframe_len;
1238
            len = bit_offset;
1239
            while (len > 0) {
1240
                *q++ = (get_bits)(&s->gb, 8);
1241
                len -= 8;
1242
            }
1243
            if (len > 0) {
1244
                *q++ = (get_bits)(&s->gb, len) << (8 - len);
1245
            }
1246
            
1247
            /* XXX: bit_offset bits into last frame */
1248
            init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE);
1249
            /* skip unused bits */
1250
            if (s->last_bitoffset > 0)
1251
                skip_bits(&s->gb, s->last_bitoffset);
1252
            /* this frame is stored in the last superframe and in the
1253
               current one */
1254
            if (wma_decode_frame(s, samples) < 0)
1255
                return -1;
1256
            samples += s->nb_channels * s->frame_len;
1257
        }
1258

    
1259
        /* read each frame starting from bit_offset */
1260
        pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
1261
        init_get_bits(&s->gb, buf + (pos >> 3), MAX_CODED_SUPERFRAME_SIZE - (pos >> 3));
1262
        len = pos & 7;
1263
        if (len > 0)
1264
            skip_bits(&s->gb, len);
1265
    
1266
        s->reset_block_lengths = 1;
1267
        for(i=0;i<nb_frames;i++) {
1268
            if (wma_decode_frame(s, samples) < 0)
1269
                return -1;
1270
            samples += s->nb_channels * s->frame_len;
1271
        }
1272

    
1273
        /* we copy the end of the frame in the last frame buffer */
1274
        pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
1275
        s->last_bitoffset = pos & 7;
1276
        pos >>= 3;
1277
        len = buf_size - pos;
1278
        if (len > MAX_CODED_SUPERFRAME_SIZE) {
1279
            return -1;
1280
        }
1281
        s->last_superframe_len = len;
1282
        memcpy(s->last_superframe, buf + pos, len);
1283
    } else {
1284
        /* single frame decode */
1285
        if (wma_decode_frame(s, samples) < 0)
1286
            return -1;
1287
        samples += s->nb_channels * s->frame_len;
1288
    }
1289
    *data_size = (int8_t *)samples - (int8_t *)data;
1290
    return s->block_align;
1291
}
1292

    
1293
static int wma_decode_end(AVCodecContext *avctx)
1294
{
1295
    WMADecodeContext *s = avctx->priv_data;
1296
    int i;
1297

    
1298
    for(i = 0; i < s->nb_block_sizes; i++)
1299
        ff_mdct_end(&s->mdct_ctx[i]);
1300
    for(i = 0; i < s->nb_block_sizes; i++)
1301
        av_free(s->windows[i]);
1302

    
1303
    if (s->use_exp_vlc) {
1304
        free_vlc(&s->exp_vlc);
1305
    }
1306
    if (s->use_noise_coding) {
1307
        free_vlc(&s->hgain_vlc);
1308
    }
1309
    for(i = 0;i < 2; i++) {
1310
        free_vlc(&s->coef_vlc[i]);
1311
        av_free(s->run_table[i]);
1312
        av_free(s->level_table[i]);
1313
    }
1314
    
1315
    return 0;
1316
}
1317

    
1318
AVCodec wmav1_decoder =
1319
{
1320
    "wmav1",
1321
    CODEC_TYPE_AUDIO,
1322
    CODEC_ID_WMAV1,
1323
    sizeof(WMADecodeContext),
1324
    wma_decode_init,
1325
    NULL,
1326
    wma_decode_end,
1327
    wma_decode_superframe,
1328
};
1329

    
1330
AVCodec wmav2_decoder =
1331
{
1332
    "wmav2",
1333
    CODEC_TYPE_AUDIO,
1334
    CODEC_ID_WMAV2,
1335
    sizeof(WMADecodeContext),
1336
    wma_decode_init,
1337
    NULL,
1338
    wma_decode_end,
1339
    wma_decode_superframe,
1340
};