~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/wmadec.c

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2004-08-29 10:53:42 UTC
  • Revision ID: james.westby@ubuntu.com-20040829105342-qgmnry37eadfkoxx
Tags: upstream-1.1.3
ImportĀ upstreamĀ versionĀ 1.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
20
/**
 
21
 * @file wmadec.c
 
22
 * WMA compatible decoder.
 
23
 */
 
24
 
 
25
#include "avcodec.h"
 
26
#include "dsputil.h"
 
27
 
 
28
/* size of blocks */
 
29
#define BLOCK_MIN_BITS 7
 
30
#define BLOCK_MAX_BITS 11
 
31
#define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS)
 
32
 
 
33
#define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
 
34
 
 
35
/* XXX: find exact max size */
 
36
#define HIGH_BAND_MAX_SIZE 16
 
37
 
 
38
#define NB_LSP_COEFS 10
 
39
 
 
40
/* XXX: is it a suitable value ? */
 
41
#define MAX_CODED_SUPERFRAME_SIZE 4096
 
42
 
 
43
#define MAX_CHANNELS 2
 
44
 
 
45
#define NOISE_TAB_SIZE 8192
 
46
 
 
47
#define LSP_POW_BITS 7
 
48
 
 
49
typedef struct WMADecodeContext {
 
50
    GetBitContext gb;
 
51
    int sample_rate;
 
52
    int nb_channels;
 
53
    int bit_rate;
 
54
    int version; /* 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) */
 
55
    int block_align;
 
56
    int use_bit_reservoir;
 
57
    int use_variable_block_len;
 
58
    int use_exp_vlc;  /* exponent coding: 0 = lsp, 1 = vlc + delta */
 
59
    int use_noise_coding; /* true if perceptual noise is added */
 
60
    int byte_offset_bits;
 
61
    VLC exp_vlc;
 
62
    int exponent_sizes[BLOCK_NB_SIZES];
 
63
    uint16_t exponent_bands[BLOCK_NB_SIZES][25];
 
64
    int high_band_start[BLOCK_NB_SIZES]; /* index of first coef in high band */
 
65
    int coefs_start;               /* first coded coef */
 
66
    int coefs_end[BLOCK_NB_SIZES]; /* max number of coded coefficients */
 
67
    int exponent_high_sizes[BLOCK_NB_SIZES];
 
68
    int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE]; 
 
69
    VLC hgain_vlc;
 
70
    
 
71
    /* coded values in high bands */
 
72
    int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
 
73
    int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
 
74
 
 
75
    /* there are two possible tables for spectral coefficients */
 
76
    VLC coef_vlc[2];
 
77
    uint16_t *run_table[2];
 
78
    uint16_t *level_table[2];
 
79
    /* frame info */
 
80
    int frame_len;       /* frame length in samples */
 
81
    int frame_len_bits;  /* frame_len = 1 << frame_len_bits */
 
82
    int nb_block_sizes;  /* number of block sizes */
 
83
    /* block info */
 
84
    int reset_block_lengths;
 
85
    int block_len_bits; /* log2 of current block length */
 
86
    int next_block_len_bits; /* log2 of next block length */
 
87
    int prev_block_len_bits; /* log2 of prev block length */
 
88
    int block_len; /* block length in samples */
 
89
    int block_num; /* block number in current frame */
 
90
    int block_pos; /* current position in frame */
 
91
    uint8_t ms_stereo; /* true if mid/side stereo mode */
 
92
    uint8_t channel_coded[MAX_CHANNELS]; /* true if channel is coded */
 
93
    float exponents[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16)));
 
94
    float max_exponent[MAX_CHANNELS];
 
95
    int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
 
96
    float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16)));
 
97
    MDCTContext mdct_ctx[BLOCK_NB_SIZES];
 
98
    float *windows[BLOCK_NB_SIZES];
 
99
    FFTSample mdct_tmp[BLOCK_MAX_SIZE] __attribute__((aligned(16))); /* temporary storage for imdct */
 
100
    /* output buffer for one frame and the last for IMDCT windowing */
 
101
    float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] __attribute__((aligned(16)));
 
102
    /* last frame info */
 
103
    uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + 4]; /* padding added */
 
104
    int last_bitoffset;
 
105
    int last_superframe_len;
 
106
    float noise_table[NOISE_TAB_SIZE];
 
107
    int noise_index;
 
108
    float noise_mult; /* XXX: suppress that and integrate it in the noise array */
 
109
    /* lsp_to_curve tables */
 
110
    float lsp_cos_table[BLOCK_MAX_SIZE];
 
111
    float lsp_pow_e_table[256];
 
112
    float lsp_pow_m_table1[(1 << LSP_POW_BITS)];
 
113
    float lsp_pow_m_table2[(1 << LSP_POW_BITS)];
 
114
 
 
115
#ifdef TRACE
 
116
    int frame_count;
 
117
#endif
 
118
} WMADecodeContext;
 
119
 
 
120
typedef struct CoefVLCTable {
 
121
    int n; /* total number of codes */
 
122
    const uint32_t *huffcodes; /* VLC bit values */
 
123
    const uint8_t *huffbits;   /* VLC bit size */
 
124
    const uint16_t *levels; /* table to build run/level tables */
 
125
} CoefVLCTable;
 
126
 
 
127
static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
 
128
 
 
129
#include "wmadata.h"
 
130
 
 
131
#ifdef TRACE
 
132
static void dump_shorts(const char *name, const short *tab, int n)
 
133
{
 
134
    int i;
 
135
 
 
136
    tprintf("%s[%d]:\n", name, n);
 
137
    for(i=0;i<n;i++) {
 
138
        if ((i & 7) == 0)
 
139
            tprintf("%4d: ", i);
 
140
        tprintf(" %5d.0", tab[i]);
 
141
        if ((i & 7) == 7)
 
142
            tprintf("\n");
 
143
    }
 
144
}
 
145
 
 
146
static void dump_floats(const char *name, int prec, const float *tab, int n)
 
147
{
 
148
    int i;
 
149
 
 
150
    tprintf("%s[%d]:\n", name, n);
 
151
    for(i=0;i<n;i++) {
 
152
        if ((i & 7) == 0)
 
153
            tprintf("%4d: ", i);
 
154
        tprintf(" %8.*f", prec, tab[i]);
 
155
        if ((i & 7) == 7)
 
156
            tprintf("\n");
 
157
    }
 
158
    if ((i & 7) != 0)
 
159
        tprintf("\n");
 
160
}
 
161
#endif
 
162
 
 
163
/* XXX: use same run/length optimization as mpeg decoders */
 
164
static void init_coef_vlc(VLC *vlc, 
 
165
                          uint16_t **prun_table, uint16_t **plevel_table,
 
166
                          const CoefVLCTable *vlc_table)
 
167
{
 
168
    int n = vlc_table->n;
 
169
    const uint8_t *table_bits = vlc_table->huffbits;
 
170
    const uint32_t *table_codes = vlc_table->huffcodes;
 
171
    const uint16_t *levels_table = vlc_table->levels;
 
172
    uint16_t *run_table, *level_table;
 
173
    const uint16_t *p;
 
174
    int i, l, j, level;
 
175
 
 
176
    init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4);
 
177
 
 
178
    run_table = av_malloc(n * sizeof(uint16_t));
 
179
    level_table = av_malloc(n * sizeof(uint16_t));
 
180
    p = levels_table;
 
181
    i = 2;
 
182
    level = 1;
 
183
    while (i < n) {
 
184
        l = *p++;
 
185
        for(j=0;j<l;j++) {
 
186
            run_table[i] = j;
 
187
            level_table[i] = level;
 
188
            i++;
 
189
        }
 
190
        level++;
 
191
    }
 
192
    *prun_table = run_table;
 
193
    *plevel_table = level_table;
 
194
}
 
195
 
 
196
static int wma_decode_init(AVCodecContext * avctx)
 
197
{
 
198
    WMADecodeContext *s = avctx->priv_data;
 
199
    int i, flags1, flags2;
 
200
    float *window;
 
201
    uint8_t *extradata;
 
202
    float bps1, high_freq, bps;
 
203
    int sample_rate1;
 
204
    int coef_vlc_table;
 
205
    
 
206
    s->sample_rate = avctx->sample_rate;
 
207
    s->nb_channels = avctx->channels;
 
208
    s->bit_rate = avctx->bit_rate;
 
209
    s->block_align = avctx->block_align;
 
210
 
 
211
    if (avctx->codec->id == CODEC_ID_WMAV1) {
 
212
        s->version = 1;
 
213
    } else {
 
214
        s->version = 2;
 
215
    }
 
216
    
 
217
    /* extract flag infos */
 
218
    flags1 = 0;
 
219
    flags2 = 0;
 
220
    extradata = avctx->extradata;
 
221
    if (s->version == 1 && avctx->extradata_size >= 4) {
 
222
        flags1 = extradata[0] | (extradata[1] << 8);
 
223
        flags2 = extradata[2] | (extradata[3] << 8);
 
224
    } else if (s->version == 2 && avctx->extradata_size >= 6) {
 
225
        flags1 = extradata[0] | (extradata[1] << 8) | 
 
226
            (extradata[2] << 16) | (extradata[3] << 24);
 
227
        flags2 = extradata[4] | (extradata[5] << 8);
 
228
    }
 
229
    s->use_exp_vlc = flags2 & 0x0001;
 
230
    s->use_bit_reservoir = flags2 & 0x0002;
 
231
    s->use_variable_block_len = flags2 & 0x0004;
 
232
 
 
233
    /* compute MDCT block size */
 
234
    if (s->sample_rate <= 16000) {
 
235
        s->frame_len_bits = 9;
 
236
    } else if (s->sample_rate <= 22050 || 
 
237
               (s->sample_rate <= 32000 && s->version == 1)) {
 
238
        s->frame_len_bits = 10;
 
239
    } else {
 
240
        s->frame_len_bits = 11;
 
241
    }
 
242
    s->frame_len = 1 << s->frame_len_bits;
 
243
    if (s->use_variable_block_len) {
 
244
        int nb_max, nb;
 
245
        nb = ((flags2 >> 3) & 3) + 1;
 
246
        if ((s->bit_rate / s->nb_channels) >= 32000)
 
247
            nb += 2;
 
248
        nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
 
249
        if (nb > nb_max)
 
250
            nb = nb_max;
 
251
        s->nb_block_sizes = nb + 1;
 
252
    } else {
 
253
        s->nb_block_sizes = 1;
 
254
    }
 
255
 
 
256
    /* init rate dependant parameters */
 
257
    s->use_noise_coding = 1;
 
258
    high_freq = s->sample_rate * 0.5;
 
259
 
 
260
    /* if version 2, then the rates are normalized */
 
261
    sample_rate1 = s->sample_rate;
 
262
    if (s->version == 2) {
 
263
        if (sample_rate1 >= 44100) 
 
264
            sample_rate1 = 44100;
 
265
        else if (sample_rate1 >= 22050) 
 
266
            sample_rate1 = 22050;
 
267
        else if (sample_rate1 >= 16000) 
 
268
            sample_rate1 = 16000;
 
269
        else if (sample_rate1 >= 11025) 
 
270
            sample_rate1 = 11025;
 
271
        else if (sample_rate1 >= 8000) 
 
272
            sample_rate1 = 8000;
 
273
    }
 
274
 
 
275
    bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
 
276
    s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0)) + 2;
 
277
 
 
278
    /* compute high frequency value and choose if noise coding should
 
279
       be activated */
 
280
    bps1 = bps;
 
281
    if (s->nb_channels == 2)
 
282
        bps1 = bps * 1.6;
 
283
    if (sample_rate1 == 44100) {
 
284
        if (bps1 >= 0.61)
 
285
            s->use_noise_coding = 0;
 
286
        else
 
287
            high_freq = high_freq * 0.4;
 
288
    } else if (sample_rate1 == 22050) {
 
289
        if (bps1 >= 1.16)
 
290
            s->use_noise_coding = 0;
 
291
        else if (bps1 >= 0.72) 
 
292
            high_freq = high_freq * 0.7;
 
293
        else
 
294
            high_freq = high_freq * 0.6;
 
295
    } else if (sample_rate1 == 16000) {
 
296
        if (bps > 0.5)
 
297
            high_freq = high_freq * 0.5;
 
298
        else
 
299
            high_freq = high_freq * 0.3;
 
300
    } else if (sample_rate1 == 11025) {
 
301
        high_freq = high_freq * 0.7;
 
302
    } else if (sample_rate1 == 8000) {
 
303
        if (bps <= 0.625) {
 
304
            high_freq = high_freq * 0.5;
 
305
        } else if (bps > 0.75) {
 
306
            s->use_noise_coding = 0;
 
307
        } else {
 
308
            high_freq = high_freq * 0.65;
 
309
        }
 
310
    } else {
 
311
        if (bps >= 0.8) {
 
312
            high_freq = high_freq * 0.75;
 
313
        } else if (bps >= 0.6) {
 
314
            high_freq = high_freq * 0.6;
 
315
        } else {
 
316
            high_freq = high_freq * 0.5;
 
317
        }
 
318
    }
 
319
    dprintf("flags1=0x%x flags2=0x%x\n", flags1, flags2);
 
320
    dprintf("version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
 
321
           s->version, s->nb_channels, s->sample_rate, s->bit_rate, 
 
322
           s->block_align);
 
323
    dprintf("bps=%f bps1=%f high_freq=%f bitoffset=%d\n", 
 
324
           bps, bps1, high_freq, s->byte_offset_bits);
 
325
    dprintf("use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
 
326
           s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
 
327
 
 
328
    /* compute the scale factor band sizes for each MDCT block size */
 
329
    {
 
330
        int a, b, pos, lpos, k, block_len, i, j, n;
 
331
        const uint8_t *table;
 
332
        
 
333
        if (s->version == 1) {
 
334
            s->coefs_start = 3;
 
335
        } else {
 
336
            s->coefs_start = 0;
 
337
        }
 
338
        for(k = 0; k < s->nb_block_sizes; k++) {
 
339
            block_len = s->frame_len >> k;
 
340
 
 
341
            if (s->version == 1) {
 
342
                lpos = 0;
 
343
                for(i=0;i<25;i++) {
 
344
                    a = wma_critical_freqs[i];
 
345
                    b = s->sample_rate;
 
346
                    pos = ((block_len * 2 * a)  + (b >> 1)) / b;
 
347
                    if (pos > block_len) 
 
348
                        pos = block_len;
 
349
                    s->exponent_bands[0][i] = pos - lpos;
 
350
                    if (pos >= block_len) {
 
351
                        i++;
 
352
                        break;
 
353
                    }
 
354
                    lpos = pos;
 
355
                }
 
356
                s->exponent_sizes[0] = i;
 
357
            } else {
 
358
                /* hardcoded tables */
 
359
                table = NULL;
 
360
                a = s->frame_len_bits - BLOCK_MIN_BITS - k;
 
361
                if (a < 3) {
 
362
                    if (s->sample_rate >= 44100)
 
363
                        table = exponent_band_44100[a];
 
364
                    else if (s->sample_rate >= 32000)
 
365
                        table = exponent_band_32000[a];
 
366
                    else if (s->sample_rate >= 22050)
 
367
                        table = exponent_band_22050[a];
 
368
                }
 
369
                if (table) {
 
370
                    n = *table++;
 
371
                    for(i=0;i<n;i++)
 
372
                        s->exponent_bands[k][i] = table[i];
 
373
                    s->exponent_sizes[k] = n;
 
374
                } else {
 
375
                    j = 0;
 
376
                    lpos = 0;
 
377
                    for(i=0;i<25;i++) {
 
378
                        a = wma_critical_freqs[i];
 
379
                        b = s->sample_rate;
 
380
                        pos = ((block_len * 2 * a)  + (b << 1)) / (4 * b);
 
381
                        pos <<= 2;
 
382
                        if (pos > block_len) 
 
383
                            pos = block_len;
 
384
                        if (pos > lpos)
 
385
                            s->exponent_bands[k][j++] = pos - lpos;
 
386
                        if (pos >= block_len)
 
387
                            break;
 
388
                        lpos = pos;
 
389
                    }
 
390
                    s->exponent_sizes[k] = j;
 
391
                }
 
392
            }
 
393
 
 
394
            /* max number of coefs */
 
395
            s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
 
396
            /* high freq computation */
 
397
            s->high_band_start[k] = (int)((block_len * 2 * high_freq) / 
 
398
                                          s->sample_rate + 0.5);
 
399
            n = s->exponent_sizes[k];
 
400
            j = 0;
 
401
            pos = 0;
 
402
            for(i=0;i<n;i++) {
 
403
                int start, end;
 
404
                start = pos;
 
405
                pos += s->exponent_bands[k][i];
 
406
                end = pos;
 
407
                if (start < s->high_band_start[k])
 
408
                    start = s->high_band_start[k];
 
409
                if (end > s->coefs_end[k])
 
410
                    end = s->coefs_end[k];
 
411
                if (end > start)
 
412
                    s->exponent_high_bands[k][j++] = end - start;
 
413
            }
 
414
            s->exponent_high_sizes[k] = j;
 
415
#if 0
 
416
            tprintf("%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
 
417
                  s->frame_len >> k, 
 
418
                  s->coefs_end[k],
 
419
                  s->high_band_start[k],
 
420
                  s->exponent_high_sizes[k]);
 
421
            for(j=0;j<s->exponent_high_sizes[k];j++)
 
422
                tprintf(" %d", s->exponent_high_bands[k][j]);
 
423
            tprintf("\n");
 
424
#endif
 
425
        }
 
426
    }
 
427
 
 
428
#ifdef TRACE
 
429
    {
 
430
        int i, j;
 
431
        for(i = 0; i < s->nb_block_sizes; i++) {
 
432
            tprintf("%5d: n=%2d:", 
 
433
                   s->frame_len >> i, 
 
434
                   s->exponent_sizes[i]);
 
435
            for(j=0;j<s->exponent_sizes[i];j++)
 
436
                tprintf(" %d", s->exponent_bands[i][j]);
 
437
            tprintf("\n");
 
438
        }
 
439
    }
 
440
#endif
 
441
 
 
442
    /* init MDCT */
 
443
    for(i = 0; i < s->nb_block_sizes; i++)
 
444
        ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
 
445
    
 
446
    /* init MDCT windows : simple sinus window */
 
447
    for(i = 0; i < s->nb_block_sizes; i++) {
 
448
        int n, j;
 
449
        float alpha;
 
450
        n = 1 << (s->frame_len_bits - i);
 
451
        window = av_malloc(sizeof(float) * n);
 
452
        alpha = M_PI / (2.0 * n);
 
453
        for(j=0;j<n;j++) {
 
454
            window[n - j - 1] = sin((j + 0.5) * alpha);
 
455
        }
 
456
        s->windows[i] = window;
 
457
    }
 
458
 
 
459
    s->reset_block_lengths = 1;
 
460
    
 
461
    if (s->use_noise_coding) {
 
462
 
 
463
        /* init the noise generator */
 
464
        if (s->use_exp_vlc)
 
465
            s->noise_mult = 0.02;
 
466
        else
 
467
            s->noise_mult = 0.04;
 
468
               
 
469
#ifdef TRACE
 
470
        for(i=0;i<NOISE_TAB_SIZE;i++)
 
471
            s->noise_table[i] = 1.0 * s->noise_mult;
 
472
#else
 
473
        {
 
474
            unsigned int seed;
 
475
            float norm;
 
476
            seed = 1;
 
477
            norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
 
478
            for(i=0;i<NOISE_TAB_SIZE;i++) {
 
479
                seed = seed * 314159 + 1;
 
480
                s->noise_table[i] = (float)((int)seed) * norm;
 
481
            }
 
482
        }
 
483
#endif
 
484
        init_vlc(&s->hgain_vlc, 9, sizeof(hgain_huffbits), 
 
485
                 hgain_huffbits, 1, 1,
 
486
                 hgain_huffcodes, 2, 2);
 
487
    }
 
488
 
 
489
    if (s->use_exp_vlc) {
 
490
        init_vlc(&s->exp_vlc, 9, sizeof(scale_huffbits), 
 
491
                 scale_huffbits, 1, 1,
 
492
                 scale_huffcodes, 4, 4);
 
493
    } else {
 
494
        wma_lsp_to_curve_init(s, s->frame_len);
 
495
    }
 
496
 
 
497
    /* choose the VLC tables for the coefficients */
 
498
    coef_vlc_table = 2;
 
499
    if (s->sample_rate >= 32000) {
 
500
        if (bps1 < 0.72)
 
501
            coef_vlc_table = 0;
 
502
        else if (bps1 < 1.16)
 
503
            coef_vlc_table = 1;
 
504
    }
 
505
 
 
506
    init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
 
507
                  &coef_vlcs[coef_vlc_table * 2]);
 
508
    init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
 
509
                  &coef_vlcs[coef_vlc_table * 2 + 1]);
 
510
    return 0;
 
511
}
 
512
 
 
513
/* interpolate values for a bigger or smaller block. The block must
 
514
   have multiple sizes */
 
515
static void interpolate_array(float *scale, int old_size, int new_size)
 
516
{
 
517
    int i, j, jincr, k;
 
518
    float v;
 
519
 
 
520
    if (new_size > old_size) {
 
521
        jincr = new_size / old_size;
 
522
        j = new_size;
 
523
        for(i = old_size - 1; i >=0; i--) {
 
524
            v = scale[i];
 
525
            k = jincr;
 
526
            do {
 
527
                scale[--j] = v;
 
528
            } while (--k);
 
529
        }
 
530
    } else if (new_size < old_size) {
 
531
        j = 0;
 
532
        jincr = old_size / new_size;
 
533
        for(i = 0; i < new_size; i++) {
 
534
            scale[i] = scale[j];
 
535
            j += jincr;
 
536
        }
 
537
    }
 
538
}
 
539
 
 
540
/* compute x^-0.25 with an exponent and mantissa table. We use linear
 
541
   interpolation to reduce the mantissa table size at a small speed
 
542
   expense (linear interpolation approximately doubles the number of
 
543
   bits of precision). */
 
544
static inline float pow_m1_4(WMADecodeContext *s, float x)
 
545
{
 
546
    union {
 
547
        float f;
 
548
        unsigned int v;
 
549
    } u, t;
 
550
    unsigned int e, m;
 
551
    float a, b;
 
552
 
 
553
    u.f = x;
 
554
    e = u.v >> 23;
 
555
    m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
 
556
    /* build interpolation scale: 1 <= t < 2. */
 
557
    t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
 
558
    a = s->lsp_pow_m_table1[m];
 
559
    b = s->lsp_pow_m_table2[m];
 
560
    return s->lsp_pow_e_table[e] * (a + b * t.f);
 
561
}
 
562
 
 
563
static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
 
564
{  
 
565
    float wdel, a, b;
 
566
    int i, e, m;
 
567
 
 
568
    wdel = M_PI / frame_len;
 
569
    for(i=0;i<frame_len;i++)
 
570
        s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
 
571
 
 
572
    /* tables for x^-0.25 computation */
 
573
    for(i=0;i<256;i++) {
 
574
        e = i - 126;
 
575
        s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
 
576
    }
 
577
 
 
578
    /* NOTE: these two tables are needed to avoid two operations in
 
579
       pow_m1_4 */
 
580
    b = 1.0;
 
581
    for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
 
582
        m = (1 << LSP_POW_BITS) + i;
 
583
        a = (float)m * (0.5 / (1 << LSP_POW_BITS));
 
584
        a = pow(a, -0.25);
 
585
        s->lsp_pow_m_table1[i] = 2 * a - b;
 
586
        s->lsp_pow_m_table2[i] = b - a;
 
587
        b = a;
 
588
    }
 
589
#if 0
 
590
    for(i=1;i<20;i++) {
 
591
        float v, r1, r2;
 
592
        v = 5.0 / i;
 
593
        r1 = pow_m1_4(s, v);
 
594
        r2 = pow(v,-0.25);
 
595
        printf("%f^-0.25=%f e=%f\n", v, r1, r2 - r1);
 
596
    }
 
597
#endif
 
598
}
 
599
 
 
600
/* NOTE: We use the same code as Vorbis here */
 
601
/* XXX: optimize it further with SSE/3Dnow */
 
602
static void wma_lsp_to_curve(WMADecodeContext *s, 
 
603
                             float *out, float *val_max_ptr, 
 
604
                             int n, float *lsp)
 
605
{
 
606
    int i, j;
 
607
    float p, q, w, v, val_max;
 
608
 
 
609
    val_max = 0;
 
610
    for(i=0;i<n;i++) {
 
611
        p = 0.5f;
 
612
        q = 0.5f;
 
613
        w = s->lsp_cos_table[i];
 
614
        for(j=1;j<NB_LSP_COEFS;j+=2){
 
615
            q *= w - lsp[j - 1];
 
616
            p *= w - lsp[j];
 
617
        }
 
618
        p *= p * (2.0f - w);
 
619
        q *= q * (2.0f + w);
 
620
        v = p + q;
 
621
        v = pow_m1_4(s, v);
 
622
        if (v > val_max)
 
623
            val_max = v;
 
624
        out[i] = v;
 
625
    }
 
626
    *val_max_ptr = val_max;
 
627
}
 
628
 
 
629
/* decode exponents coded with LSP coefficients (same idea as Vorbis) */
 
630
static void decode_exp_lsp(WMADecodeContext *s, int ch)
 
631
{
 
632
    float lsp_coefs[NB_LSP_COEFS];
 
633
    int val, i;
 
634
 
 
635
    for(i = 0; i < NB_LSP_COEFS; i++) {
 
636
        if (i == 0 || i >= 8)
 
637
            val = get_bits(&s->gb, 3);
 
638
        else
 
639
            val = get_bits(&s->gb, 4);
 
640
        lsp_coefs[i] = lsp_codebook[i][val];
 
641
    }
 
642
 
 
643
    wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
 
644
                     s->block_len, lsp_coefs);
 
645
}
 
646
 
 
647
/* decode exponents coded with VLC codes */
 
648
static int decode_exp_vlc(WMADecodeContext *s, int ch)
 
649
{
 
650
    int last_exp, n, code;
 
651
    const uint16_t *ptr, *band_ptr;
 
652
    float v, *q, max_scale, *q_end;
 
653
    
 
654
    band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
 
655
    ptr = band_ptr;
 
656
    q = s->exponents[ch];
 
657
    q_end = q + s->block_len;
 
658
    max_scale = 0;
 
659
    if (s->version == 1) {
 
660
        last_exp = get_bits(&s->gb, 5) + 10;
 
661
        /* XXX: use a table */
 
662
        v = pow(10, last_exp * (1.0 / 16.0));
 
663
        max_scale = v;
 
664
        n = *ptr++;
 
665
        do {
 
666
            *q++ = v;
 
667
        } while (--n);
 
668
    }
 
669
    last_exp = 36;
 
670
    while (q < q_end) {
 
671
        code = get_vlc(&s->gb, &s->exp_vlc);
 
672
        if (code < 0)
 
673
            return -1;
 
674
        /* NOTE: this offset is the same as MPEG4 AAC ! */
 
675
        last_exp += code - 60;
 
676
        /* XXX: use a table */
 
677
        v = pow(10, last_exp * (1.0 / 16.0));
 
678
        if (v > max_scale)
 
679
            max_scale = v;
 
680
        n = *ptr++;
 
681
        do {
 
682
            *q++ = v;
 
683
        } while (--n);
 
684
    }
 
685
    s->max_exponent[ch] = max_scale;
 
686
    return 0;
 
687
}
 
688
 
 
689
/* return 0 if OK. return 1 if last block of frame. return -1 if
 
690
   unrecorrable error. */
 
691
static int wma_decode_block(WMADecodeContext *s)
 
692
{
 
693
    int n, v, a, ch, code, bsize;
 
694
    int coef_nb_bits, total_gain, parse_exponents;
 
695
    float window[BLOCK_MAX_SIZE * 2];
 
696
    int nb_coefs[MAX_CHANNELS];
 
697
    float mdct_norm;
 
698
 
 
699
#ifdef TRACE
 
700
    tprintf("***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
 
701
#endif
 
702
 
 
703
    /* compute current block length */
 
704
    if (s->use_variable_block_len) {
 
705
        n = av_log2(s->nb_block_sizes - 1) + 1;
 
706
    
 
707
        if (s->reset_block_lengths) {
 
708
            s->reset_block_lengths = 0;
 
709
            v = get_bits(&s->gb, n);
 
710
            if (v >= s->nb_block_sizes)
 
711
                return -1;
 
712
            s->prev_block_len_bits = s->frame_len_bits - v;
 
713
            v = get_bits(&s->gb, n);
 
714
            if (v >= s->nb_block_sizes)
 
715
                return -1;
 
716
            s->block_len_bits = s->frame_len_bits - v;
 
717
        } else {
 
718
            /* update block lengths */
 
719
            s->prev_block_len_bits = s->block_len_bits;
 
720
            s->block_len_bits = s->next_block_len_bits;
 
721
        }
 
722
        v = get_bits(&s->gb, n);
 
723
        if (v >= s->nb_block_sizes)
 
724
            return -1;
 
725
        s->next_block_len_bits = s->frame_len_bits - v;
 
726
    } else {
 
727
        /* fixed block len */
 
728
        s->next_block_len_bits = s->frame_len_bits;
 
729
        s->prev_block_len_bits = s->frame_len_bits;
 
730
        s->block_len_bits = s->frame_len_bits;
 
731
    }
 
732
 
 
733
    /* now check if the block length is coherent with the frame length */
 
734
    s->block_len = 1 << s->block_len_bits;
 
735
    if ((s->block_pos + s->block_len) > s->frame_len)
 
736
        return -1;
 
737
 
 
738
    if (s->nb_channels == 2) {
 
739
        s->ms_stereo = get_bits(&s->gb, 1);
 
740
    }
 
741
    v = 0;
 
742
    for(ch = 0; ch < s->nb_channels; ch++) {
 
743
        a = get_bits(&s->gb, 1);
 
744
        s->channel_coded[ch] = a;
 
745
        v |= a;
 
746
    }
 
747
    /* if no channel coded, no need to go further */
 
748
    /* XXX: fix potential framing problems */
 
749
    if (!v)
 
750
        goto next;
 
751
 
 
752
    bsize = s->frame_len_bits - s->block_len_bits;
 
753
 
 
754
    /* read total gain and extract corresponding number of bits for
 
755
       coef escape coding */
 
756
    total_gain = 1;
 
757
    for(;;) {
 
758
        a = get_bits(&s->gb, 7);
 
759
        total_gain += a;
 
760
        if (a != 127)
 
761
            break;
 
762
    }
 
763
    
 
764
    if (total_gain < 15)
 
765
        coef_nb_bits = 13;
 
766
    else if (total_gain < 32)
 
767
        coef_nb_bits = 12;
 
768
    else if (total_gain < 40)
 
769
        coef_nb_bits = 11;
 
770
    else if (total_gain < 45)
 
771
        coef_nb_bits = 10;
 
772
    else
 
773
        coef_nb_bits = 9;
 
774
 
 
775
    /* compute number of coefficients */
 
776
    n = s->coefs_end[bsize] - s->coefs_start;
 
777
    for(ch = 0; ch < s->nb_channels; ch++)
 
778
        nb_coefs[ch] = n;
 
779
 
 
780
    /* complex coding */
 
781
    if (s->use_noise_coding) {
 
782
 
 
783
        for(ch = 0; ch < s->nb_channels; ch++) {
 
784
            if (s->channel_coded[ch]) {
 
785
                int i, n, a;
 
786
                n = s->exponent_high_sizes[bsize];
 
787
                for(i=0;i<n;i++) {
 
788
                    a = get_bits(&s->gb, 1);
 
789
                    s->high_band_coded[ch][i] = a;
 
790
                    /* if noise coding, the coefficients are not transmitted */
 
791
                    if (a)
 
792
                        nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
 
793
                }
 
794
            }
 
795
        }
 
796
        for(ch = 0; ch < s->nb_channels; ch++) {
 
797
            if (s->channel_coded[ch]) {
 
798
                int i, n, val, code;
 
799
 
 
800
                n = s->exponent_high_sizes[bsize];
 
801
                val = (int)0x80000000;
 
802
                for(i=0;i<n;i++) {
 
803
                    if (s->high_band_coded[ch][i]) {
 
804
                        if (val == (int)0x80000000) {
 
805
                            val = get_bits(&s->gb, 7) - 19;
 
806
                        } else {
 
807
                            code = get_vlc(&s->gb, &s->hgain_vlc);
 
808
                            if (code < 0)
 
809
                                return -1;
 
810
                            val += code - 18;
 
811
                        }
 
812
                        s->high_band_values[ch][i] = val;
 
813
                    }
 
814
                }
 
815
            }
 
816
        }
 
817
    }
 
818
           
 
819
    /* exposant can be interpolated in short blocks. */
 
820
    parse_exponents = 1;
 
821
    if (s->block_len_bits != s->frame_len_bits) {
 
822
        parse_exponents = get_bits(&s->gb, 1);
 
823
    }
 
824
    
 
825
    if (parse_exponents) {
 
826
        for(ch = 0; ch < s->nb_channels; ch++) {
 
827
            if (s->channel_coded[ch]) {
 
828
                if (s->use_exp_vlc) {
 
829
                    if (decode_exp_vlc(s, ch) < 0)
 
830
                        return -1;
 
831
                } else {
 
832
                    decode_exp_lsp(s, ch);
 
833
                }
 
834
            }
 
835
        }
 
836
    } else {
 
837
        for(ch = 0; ch < s->nb_channels; ch++) {
 
838
            if (s->channel_coded[ch]) {
 
839
                interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits, 
 
840
                                  s->block_len);
 
841
            }
 
842
        }
 
843
    }
 
844
 
 
845
    /* parse spectral coefficients : just RLE encoding */
 
846
    for(ch = 0; ch < s->nb_channels; ch++) {
 
847
        if (s->channel_coded[ch]) {
 
848
            VLC *coef_vlc;
 
849
            int level, run, sign, tindex;
 
850
            int16_t *ptr, *eptr;
 
851
            const int16_t *level_table, *run_table;
 
852
 
 
853
            /* special VLC tables are used for ms stereo because
 
854
               there is potentially less energy there */
 
855
            tindex = (ch == 1 && s->ms_stereo);
 
856
            coef_vlc = &s->coef_vlc[tindex];
 
857
            run_table = s->run_table[tindex];
 
858
            level_table = s->level_table[tindex];
 
859
            /* XXX: optimize */
 
860
            ptr = &s->coefs1[ch][0];
 
861
            eptr = ptr + nb_coefs[ch];
 
862
            memset(ptr, 0, s->block_len * sizeof(int16_t));
 
863
            for(;;) {
 
864
                code = get_vlc(&s->gb, coef_vlc);
 
865
                if (code < 0)
 
866
                    return -1;
 
867
                if (code == 1) {
 
868
                    /* EOB */
 
869
                    break;
 
870
                } else if (code == 0) {
 
871
                    /* escape */
 
872
                    level = get_bits(&s->gb, coef_nb_bits);
 
873
                    /* NOTE: this is rather suboptimal. reading
 
874
                       block_len_bits would be better */
 
875
                    run = get_bits(&s->gb, s->frame_len_bits);
 
876
                } else {
 
877
                    /* normal code */
 
878
                    run = run_table[code];
 
879
                    level = level_table[code];
 
880
                }
 
881
                sign = get_bits(&s->gb, 1);
 
882
                if (!sign)
 
883
                    level = -level;
 
884
                ptr += run;
 
885
                if (ptr >= eptr)
 
886
                    return -1;
 
887
                *ptr++ = level;
 
888
                /* NOTE: EOB can be omitted */
 
889
                if (ptr >= eptr)
 
890
                    break;
 
891
            }
 
892
        }
 
893
        if (s->version == 1 && s->nb_channels >= 2) {
 
894
            align_get_bits(&s->gb);
 
895
        }
 
896
    }
 
897
     
 
898
    /* normalize */
 
899
    {
 
900
        int n4 = s->block_len / 2;
 
901
        mdct_norm = 1.0 / (float)n4;
 
902
        if (s->version == 1) {
 
903
            mdct_norm *= sqrt(n4);
 
904
        }
 
905
    }
 
906
 
 
907
    /* finally compute the MDCT coefficients */
 
908
    for(ch = 0; ch < s->nb_channels; ch++) {
 
909
        if (s->channel_coded[ch]) {
 
910
            int16_t *coefs1;
 
911
            float *coefs, *exponents, mult, mult1, noise, *exp_ptr;
 
912
            int i, j, n, n1, last_high_band;
 
913
            float exp_power[HIGH_BAND_MAX_SIZE];
 
914
 
 
915
            coefs1 = s->coefs1[ch];
 
916
            exponents = s->exponents[ch];
 
917
            mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
 
918
            mult *= mdct_norm;
 
919
            coefs = s->coefs[ch];
 
920
            if (s->use_noise_coding) {
 
921
                mult1 = mult;
 
922
                /* very low freqs : noise */
 
923
                for(i = 0;i < s->coefs_start; i++) {
 
924
                    *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1;
 
925
                    s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
 
926
                }
 
927
                
 
928
                n1 = s->exponent_high_sizes[bsize];
 
929
 
 
930
                /* compute power of high bands */
 
931
                exp_ptr = exponents + 
 
932
                    s->high_band_start[bsize] - 
 
933
                    s->coefs_start;
 
934
                last_high_band = 0; /* avoid warning */
 
935
                for(j=0;j<n1;j++) {
 
936
                    n = s->exponent_high_bands[s->frame_len_bits - 
 
937
                                              s->block_len_bits][j];
 
938
                    if (s->high_band_coded[ch][j]) {
 
939
                        float e2, v;
 
940
                        e2 = 0;
 
941
                        for(i = 0;i < n; i++) {
 
942
                            v = exp_ptr[i];
 
943
                            e2 += v * v;
 
944
                        }
 
945
                        exp_power[j] = e2 / n;
 
946
                        last_high_band = j;
 
947
                        tprintf("%d: power=%f (%d)\n", j, exp_power[j], n);
 
948
                    }
 
949
                    exp_ptr += n;
 
950
                }
 
951
 
 
952
                /* main freqs and high freqs */
 
953
                for(j=-1;j<n1;j++) {
 
954
                    if (j < 0) {
 
955
                        n = s->high_band_start[bsize] - 
 
956
                            s->coefs_start;
 
957
                    } else {
 
958
                        n = s->exponent_high_bands[s->frame_len_bits - 
 
959
                                                  s->block_len_bits][j];
 
960
                    }
 
961
                    if (j >= 0 && s->high_band_coded[ch][j]) {
 
962
                        /* use noise with specified power */
 
963
                        mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
 
964
                        /* XXX: use a table */
 
965
                        mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
 
966
                        mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
 
967
                        mult1 *= mdct_norm;
 
968
                        for(i = 0;i < n; i++) {
 
969
                            noise = s->noise_table[s->noise_index];
 
970
                            s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
 
971
                            *coefs++ = (*exponents++) * noise * mult1;
 
972
                        }
 
973
                    } else {
 
974
                        /* coded values + small noise */
 
975
                        for(i = 0;i < n; i++) {
 
976
                            noise = s->noise_table[s->noise_index];
 
977
                            s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
 
978
                            *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult;
 
979
                        }
 
980
                    }
 
981
                }
 
982
 
 
983
                /* very high freqs : noise */
 
984
                n = s->block_len - s->coefs_end[bsize];
 
985
                mult1 = mult * exponents[-1];
 
986
                for(i = 0; i < n; i++) {
 
987
                    *coefs++ = s->noise_table[s->noise_index] * mult1;
 
988
                    s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
 
989
                }
 
990
            } else {
 
991
                /* XXX: optimize more */
 
992
                for(i = 0;i < s->coefs_start; i++)
 
993
                    *coefs++ = 0.0;
 
994
                n = nb_coefs[ch];
 
995
                for(i = 0;i < n; i++) {
 
996
                    *coefs++ = coefs1[i] * exponents[i] * mult;
 
997
                }
 
998
                n = s->block_len - s->coefs_end[bsize];
 
999
                for(i = 0;i < n; i++)
 
1000
                    *coefs++ = 0.0;
 
1001
            }
 
1002
        }
 
1003
    }
 
1004
 
 
1005
#ifdef TRACE
 
1006
    for(ch = 0; ch < s->nb_channels; ch++) {
 
1007
        if (s->channel_coded[ch]) {
 
1008
            dump_floats("exponents", 3, s->exponents[ch], s->block_len);
 
1009
            dump_floats("coefs", 1, s->coefs[ch], s->block_len);
 
1010
        }
 
1011
    }
 
1012
#endif
 
1013
    
 
1014
    if (s->ms_stereo && s->channel_coded[1]) {
 
1015
        float a, b;
 
1016
        int i;
 
1017
 
 
1018
        /* nominal case for ms stereo: we do it before mdct */
 
1019
        /* no need to optimize this case because it should almost
 
1020
           never happen */
 
1021
        if (!s->channel_coded[0]) {
 
1022
            tprintf("rare ms-stereo case happened\n");
 
1023
            memset(s->coefs[0], 0, sizeof(float) * s->block_len);
 
1024
            s->channel_coded[0] = 1;
 
1025
        }
 
1026
        
 
1027
        for(i = 0; i < s->block_len; i++) {
 
1028
            a = s->coefs[0][i];
 
1029
            b = s->coefs[1][i];
 
1030
            s->coefs[0][i] = a + b;
 
1031
            s->coefs[1][i] = a - b;
 
1032
        }
 
1033
    }
 
1034
 
 
1035
    /* build the window : we ensure that when the windows overlap
 
1036
       their squared sum is always 1 (MDCT reconstruction rule) */
 
1037
    /* XXX: merge with output */
 
1038
    {
 
1039
        int i, next_block_len, block_len, prev_block_len, n;
 
1040
        float *wptr;
 
1041
 
 
1042
        block_len = s->block_len;
 
1043
        prev_block_len = 1 << s->prev_block_len_bits;
 
1044
        next_block_len = 1 << s->next_block_len_bits;
 
1045
 
 
1046
        /* right part */
 
1047
        wptr = window + block_len;
 
1048
        if (block_len <= next_block_len) {
 
1049
            for(i=0;i<block_len;i++)
 
1050
                *wptr++ = s->windows[bsize][i];
 
1051
        } else {
 
1052
            /* overlap */
 
1053
            n = (block_len / 2) - (next_block_len / 2);
 
1054
            for(i=0;i<n;i++)
 
1055
                *wptr++ = 1.0;
 
1056
            for(i=0;i<next_block_len;i++)
 
1057
                *wptr++ = s->windows[s->frame_len_bits - s->next_block_len_bits][i];
 
1058
            for(i=0;i<n;i++)
 
1059
                *wptr++ = 0.0;
 
1060
        }
 
1061
 
 
1062
        /* left part */
 
1063
        wptr = window + block_len;
 
1064
        if (block_len <= prev_block_len) {
 
1065
            for(i=0;i<block_len;i++)
 
1066
                *--wptr = s->windows[bsize][i];
 
1067
        } else {
 
1068
            /* overlap */
 
1069
            n = (block_len / 2) - (prev_block_len / 2);
 
1070
            for(i=0;i<n;i++)
 
1071
                *--wptr = 1.0;
 
1072
            for(i=0;i<prev_block_len;i++)
 
1073
                *--wptr = s->windows[s->frame_len_bits - s->prev_block_len_bits][i];
 
1074
            for(i=0;i<n;i++)
 
1075
                *--wptr = 0.0;
 
1076
        }
 
1077
    }
 
1078
 
 
1079
    
 
1080
    for(ch = 0; ch < s->nb_channels; ch++) {
 
1081
        if (s->channel_coded[ch]) {
 
1082
            FFTSample output[BLOCK_MAX_SIZE * 2] __attribute__((aligned(16)));
 
1083
            float *ptr;
 
1084
            int i, n4, index, n;
 
1085
 
 
1086
            n = s->block_len;
 
1087
            n4 = s->block_len / 2;
 
1088
            ff_imdct_calc(&s->mdct_ctx[bsize], 
 
1089
                          output, s->coefs[ch], s->mdct_tmp);
 
1090
 
 
1091
            /* XXX: optimize all that by build the window and
 
1092
               multipying/adding at the same time */
 
1093
            /* multiply by the window */
 
1094
            for(i=0;i<n * 2;i++) {
 
1095
                output[i] *= window[i];
 
1096
            }
 
1097
 
 
1098
            /* add in the frame */
 
1099
            index = (s->frame_len / 2) + s->block_pos - n4;
 
1100
            ptr = &s->frame_out[ch][index];
 
1101
            for(i=0;i<n * 2;i++) {
 
1102
                *ptr += output[i];
 
1103
                ptr++;
 
1104
            }
 
1105
 
 
1106
            /* specific fast case for ms-stereo : add to second
 
1107
               channel if it is not coded */
 
1108
            if (s->ms_stereo && !s->channel_coded[1]) {
 
1109
                ptr = &s->frame_out[1][index];
 
1110
                for(i=0;i<n * 2;i++) {
 
1111
                    *ptr += output[i];
 
1112
                    ptr++;
 
1113
                }
 
1114
            }
 
1115
        }
 
1116
    }
 
1117
 next:
 
1118
    /* update block number */
 
1119
    s->block_num++;
 
1120
    s->block_pos += s->block_len;
 
1121
    if (s->block_pos >= s->frame_len)
 
1122
        return 1;
 
1123
    else
 
1124
        return 0;
 
1125
}
 
1126
 
 
1127
/* decode a frame of frame_len samples */
 
1128
static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
 
1129
{
 
1130
    int ret, i, n, a, ch, incr;
 
1131
    int16_t *ptr;
 
1132
    float *iptr;
 
1133
 
 
1134
#ifdef TRACE
 
1135
    tprintf("***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
 
1136
#endif
 
1137
 
 
1138
    /* read each block */
 
1139
    s->block_num = 0;
 
1140
    s->block_pos = 0;
 
1141
    for(;;) {
 
1142
        ret = wma_decode_block(s);
 
1143
        if (ret < 0) 
 
1144
            return -1;
 
1145
        if (ret)
 
1146
            break;
 
1147
    }
 
1148
 
 
1149
    /* convert frame to integer */
 
1150
    n = s->frame_len;
 
1151
    incr = s->nb_channels;
 
1152
    for(ch = 0; ch < s->nb_channels; ch++) {
 
1153
        ptr = samples + ch;
 
1154
        iptr = s->frame_out[ch];
 
1155
 
 
1156
        for(i=0;i<n;i++) {
 
1157
            a = lrintf(*iptr++);
 
1158
            if (a > 32767)
 
1159
                a = 32767;
 
1160
            else if (a < -32768)
 
1161
                a = -32768;
 
1162
            *ptr = a;
 
1163
            ptr += incr;
 
1164
        }
 
1165
        /* prepare for next block */
 
1166
        memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
 
1167
                s->frame_len * sizeof(float));
 
1168
        /* XXX: suppress this */
 
1169
        memset(&s->frame_out[ch][s->frame_len], 0, 
 
1170
               s->frame_len * sizeof(float));
 
1171
    }
 
1172
 
 
1173
#ifdef TRACE
 
1174
    dump_shorts("samples", samples, n * s->nb_channels);
 
1175
#endif
 
1176
    return 0;
 
1177
}
 
1178
 
 
1179
static int wma_decode_superframe(AVCodecContext *avctx, 
 
1180
                                 void *data, int *data_size,
 
1181
                                 uint8_t *buf, int buf_size)
 
1182
{
 
1183
    WMADecodeContext *s = avctx->priv_data;
 
1184
    int nb_frames, bit_offset, i, pos, len;
 
1185
    uint8_t *q;
 
1186
    int16_t *samples;
 
1187
    
 
1188
    tprintf("***decode_superframe:\n");
 
1189
 
 
1190
    samples = data;
 
1191
 
 
1192
    init_get_bits(&s->gb, buf, buf_size*8);
 
1193
    
 
1194
    if (s->use_bit_reservoir) {
 
1195
        /* read super frame header */
 
1196
        get_bits(&s->gb, 4); /* super frame index */
 
1197
        nb_frames = get_bits(&s->gb, 4) - 1;
 
1198
 
 
1199
        bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
 
1200
 
 
1201
        if (s->last_superframe_len > 0) {
 
1202
            //        printf("skip=%d\n", s->last_bitoffset);
 
1203
            /* add bit_offset bits to last frame */
 
1204
            if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) > 
 
1205
                MAX_CODED_SUPERFRAME_SIZE)
 
1206
                goto fail;
 
1207
            q = s->last_superframe + s->last_superframe_len;
 
1208
            len = bit_offset;
 
1209
            while (len > 0) {
 
1210
                *q++ = (get_bits)(&s->gb, 8);
 
1211
                len -= 8;
 
1212
            }
 
1213
            if (len > 0) {
 
1214
                *q++ = (get_bits)(&s->gb, len) << (8 - len);
 
1215
            }
 
1216
            
 
1217
            /* XXX: bit_offset bits into last frame */
 
1218
            init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
 
1219
            /* skip unused bits */
 
1220
            if (s->last_bitoffset > 0)
 
1221
                skip_bits(&s->gb, s->last_bitoffset);
 
1222
            /* this frame is stored in the last superframe and in the
 
1223
               current one */
 
1224
            if (wma_decode_frame(s, samples) < 0)
 
1225
                goto fail;
 
1226
            samples += s->nb_channels * s->frame_len;
 
1227
        }
 
1228
 
 
1229
        /* read each frame starting from bit_offset */
 
1230
        pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
 
1231
        init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
 
1232
        len = pos & 7;
 
1233
        if (len > 0)
 
1234
            skip_bits(&s->gb, len);
 
1235
    
 
1236
        s->reset_block_lengths = 1;
 
1237
        for(i=0;i<nb_frames;i++) {
 
1238
            if (wma_decode_frame(s, samples) < 0)
 
1239
                goto fail;
 
1240
            samples += s->nb_channels * s->frame_len;
 
1241
        }
 
1242
 
 
1243
        /* we copy the end of the frame in the last frame buffer */
 
1244
        pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
 
1245
        s->last_bitoffset = pos & 7;
 
1246
        pos >>= 3;
 
1247
        len = buf_size - pos;
 
1248
        if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
 
1249
            goto fail;
 
1250
        }
 
1251
        s->last_superframe_len = len;
 
1252
        memcpy(s->last_superframe, buf + pos, len);
 
1253
    } else {
 
1254
        /* single frame decode */
 
1255
        if (wma_decode_frame(s, samples) < 0)
 
1256
            goto fail;
 
1257
        samples += s->nb_channels * s->frame_len;
 
1258
    }
 
1259
    *data_size = (int8_t *)samples - (int8_t *)data;
 
1260
    return s->block_align;
 
1261
 fail:
 
1262
    /* when error, we reset the bit reservoir */
 
1263
    s->last_superframe_len = 0;
 
1264
    return -1;
 
1265
}
 
1266
 
 
1267
static int wma_decode_end(AVCodecContext *avctx)
 
1268
{
 
1269
    WMADecodeContext *s = avctx->priv_data;
 
1270
    int i;
 
1271
 
 
1272
    for(i = 0; i < s->nb_block_sizes; i++)
 
1273
        ff_mdct_end(&s->mdct_ctx[i]);
 
1274
    for(i = 0; i < s->nb_block_sizes; i++)
 
1275
        av_free(s->windows[i]);
 
1276
 
 
1277
    if (s->use_exp_vlc) {
 
1278
        free_vlc(&s->exp_vlc);
 
1279
    }
 
1280
    if (s->use_noise_coding) {
 
1281
        free_vlc(&s->hgain_vlc);
 
1282
    }
 
1283
    for(i = 0;i < 2; i++) {
 
1284
        free_vlc(&s->coef_vlc[i]);
 
1285
        av_free(s->run_table[i]);
 
1286
        av_free(s->level_table[i]);
 
1287
    }
 
1288
    
 
1289
    return 0;
 
1290
}
 
1291
 
 
1292
AVCodec wmav1_decoder =
 
1293
{
 
1294
    "wmav1",
 
1295
    CODEC_TYPE_AUDIO,
 
1296
    CODEC_ID_WMAV1,
 
1297
    sizeof(WMADecodeContext),
 
1298
    wma_decode_init,
 
1299
    NULL,
 
1300
    wma_decode_end,
 
1301
    wma_decode_superframe,
 
1302
};
 
1303
 
 
1304
AVCodec wmav2_decoder =
 
1305
{
 
1306
    "wmav2",
 
1307
    CODEC_TYPE_AUDIO,
 
1308
    CODEC_ID_WMAV2,
 
1309
    sizeof(WMADecodeContext),
 
1310
    wma_decode_init,
 
1311
    NULL,
 
1312
    wma_decode_end,
 
1313
    wma_decode_superframe,
 
1314
};