Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegaudiodec.c @ da9c9637

History | View | Annotate | Download (80 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * MPEG Audio decoder
3 ff4ec49e Fabrice Bellard
 * Copyright (c) 2001, 2002 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 ff4ec49e Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 *
10 ff4ec49e Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 ff4ec49e Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 */
19 983e3246 Michael Niedermayer
20
/**
21
 * @file mpegaudiodec.c
22
 * MPEG Audio decoder.
23
 */ 
24
25 239c2f4c Fabrice Bellard
//#define DEBUG
26 de6d9b64 Fabrice Bellard
#include "avcodec.h"
27 239c2f4c Fabrice Bellard
#include "mpegaudio.h"
28 84f986c0 Steven M. Schultz
#include "dsputil.h"
29 de6d9b64 Fabrice Bellard
30
/*
31 239c2f4c Fabrice Bellard
 * TODO:
32
 *  - in low precision mode, use more 16 bit multiplies in synth filter
33
 *  - test lsf / mpeg25 extensively.
34 de6d9b64 Fabrice Bellard
 */
35
36 239c2f4c Fabrice Bellard
/* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
37
   audio decoder */
38 81552334 Fabrice Bellard
#ifdef CONFIG_MPEGAUDIO_HP
39
#define USE_HIGHPRECISION
40
#endif
41 239c2f4c Fabrice Bellard
42
#ifdef USE_HIGHPRECISION
43
#define FRAC_BITS   23   /* fractional bits for sb_samples and dct */
44
#define WFRAC_BITS  16   /* fractional bits for window */
45
#else
46
#define FRAC_BITS   15   /* fractional bits for sb_samples and dct */
47
#define WFRAC_BITS  14   /* fractional bits for window */
48
#endif
49
50
#define FRAC_ONE    (1 << FRAC_BITS)
51
52 0c1a9eda Zdenek Kabelac
#define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
53
#define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
54 239c2f4c Fabrice Bellard
#define FIX(a)   ((int)((a) * FRAC_ONE))
55
/* WARNING: only correct for posititive numbers */
56
#define FIXR(a)   ((int)((a) * FRAC_ONE + 0.5))
57
#define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
58
59
#if FRAC_BITS <= 15
60 0c1a9eda Zdenek Kabelac
typedef int16_t MPA_INT;
61 239c2f4c Fabrice Bellard
#else
62 0c1a9eda Zdenek Kabelac
typedef int32_t MPA_INT;
63 239c2f4c Fabrice Bellard
#endif
64
65
/****************/
66
67 de6d9b64 Fabrice Bellard
#define HEADER_SIZE 4
68
#define BACKSTEP_SIZE 512
69
70 a1e257b2 Michael Niedermayer
struct GranuleDef;
71
72 de6d9b64 Fabrice Bellard
typedef struct MPADecodeContext {
73 0c1a9eda Zdenek Kabelac
    uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE];        /* input buffer */
74 de6d9b64 Fabrice Bellard
    int inbuf_index;
75 0c1a9eda Zdenek Kabelac
    uint8_t *inbuf_ptr, *inbuf;
76 de6d9b64 Fabrice Bellard
    int frame_size;
77 239c2f4c Fabrice Bellard
    int free_format_frame_size; /* frame size in case of free format
78
                                   (zero if currently unknown) */
79
    /* next header (used in free format parsing) */
80 0c1a9eda Zdenek Kabelac
    uint32_t free_format_next_header; 
81 de6d9b64 Fabrice Bellard
    int error_protection;
82
    int layer;
83
    int sample_rate;
84 239c2f4c Fabrice Bellard
    int sample_rate_index; /* between 0 and 8 */
85 de6d9b64 Fabrice Bellard
    int bit_rate;
86
    int old_frame_size;
87
    GetBitContext gb;
88 239c2f4c Fabrice Bellard
    int nb_channels;
89
    int mode;
90
    int mode_ext;
91
    int lsf;
92 a05c8d71 Michael Niedermayer
    MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
93 239c2f4c Fabrice Bellard
    int synth_buf_offset[MPA_MAX_CHANNELS];
94 a05c8d71 Michael Niedermayer
    int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16)));
95 0c1a9eda Zdenek Kabelac
    int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
96 239c2f4c Fabrice Bellard
#ifdef DEBUG
97
    int frame_count;
98
#endif
99 a1e257b2 Michael Niedermayer
    void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
100 de6d9b64 Fabrice Bellard
} MPADecodeContext;
101
102 239c2f4c Fabrice Bellard
/* layer 3 "granule" */
103
typedef struct GranuleDef {
104 0c1a9eda Zdenek Kabelac
    uint8_t scfsi;
105 239c2f4c Fabrice Bellard
    int part2_3_length;
106
    int big_values;
107
    int global_gain;
108
    int scalefac_compress;
109 0c1a9eda Zdenek Kabelac
    uint8_t block_type;
110
    uint8_t switch_point;
111 239c2f4c Fabrice Bellard
    int table_select[3];
112
    int subblock_gain[3];
113 0c1a9eda Zdenek Kabelac
    uint8_t scalefac_scale;
114
    uint8_t count1table_select;
115 239c2f4c Fabrice Bellard
    int region_size[3]; /* number of huffman codes in each region */
116
    int preflag;
117
    int short_start, long_end; /* long/short band indexes */
118 0c1a9eda Zdenek Kabelac
    uint8_t scale_factors[40];
119
    int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
120 239c2f4c Fabrice Bellard
} GranuleDef;
121 de6d9b64 Fabrice Bellard
122 239c2f4c Fabrice Bellard
#define MODE_EXT_MS_STEREO 2
123
#define MODE_EXT_I_STEREO  1
124
125
/* layer 3 huffman tables */
126
typedef struct HuffTable {
127
    int xsize;
128 0c1a9eda Zdenek Kabelac
    const uint8_t *bits;
129
    const uint16_t *codes;
130 239c2f4c Fabrice Bellard
} HuffTable;
131
132
#include "mpegaudiodectab.h"
133
134 a1e257b2 Michael Niedermayer
static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
135
static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
136
137 239c2f4c Fabrice Bellard
/* vlc structure for decoding layer 3 huffman tables */
138
static VLC huff_vlc[16]; 
139 0c1a9eda Zdenek Kabelac
static uint8_t *huff_code_table[16];
140 239c2f4c Fabrice Bellard
static VLC huff_quad_vlc[2];
141
/* computed from band_size_long */
142 0c1a9eda Zdenek Kabelac
static uint16_t band_index_long[9][23];
143 239c2f4c Fabrice Bellard
/* XXX: free when all decoders are closed */
144
#define TABLE_4_3_SIZE (8191 + 16)
145 0c1a9eda Zdenek Kabelac
static int8_t  *table_4_3_exp;
146 239c2f4c Fabrice Bellard
#if FRAC_BITS <= 15
147 0c1a9eda Zdenek Kabelac
static uint16_t *table_4_3_value;
148 239c2f4c Fabrice Bellard
#else
149 0c1a9eda Zdenek Kabelac
static uint32_t *table_4_3_value;
150 239c2f4c Fabrice Bellard
#endif
151
/* intensity stereo coef table */
152 0c1a9eda Zdenek Kabelac
static int32_t is_table[2][16];
153
static int32_t is_table_lsf[2][2][16];
154 a1e257b2 Michael Niedermayer
static int32_t csa_table[8][4];
155
static float csa_table_float[8][4];
156 0c1a9eda Zdenek Kabelac
static int32_t mdct_win[8][36];
157 239c2f4c Fabrice Bellard
158
/* lower 2 bits: modulo 3, higher bits: shift */
159 0c1a9eda Zdenek Kabelac
static uint16_t scale_factor_modshift[64];
160 239c2f4c Fabrice Bellard
/* [i][j]:  2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
161 0c1a9eda Zdenek Kabelac
static int32_t scale_factor_mult[15][3];
162 239c2f4c Fabrice Bellard
/* mult table for layer 2 group quantization */
163
164
#define SCALE_GEN(v) \
165
{ FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
166
167 0c1a9eda Zdenek Kabelac
static int32_t scale_factor_mult2[3][3] = {
168 81552334 Fabrice Bellard
    SCALE_GEN(4.0 / 3.0), /* 3 steps */
169
    SCALE_GEN(4.0 / 5.0), /* 5 steps */
170
    SCALE_GEN(4.0 / 9.0), /* 9 steps */
171 239c2f4c Fabrice Bellard
};
172
173
/* 2^(n/4) */
174 0c1a9eda Zdenek Kabelac
static uint32_t scale_factor_mult3[4] = {
175 239c2f4c Fabrice Bellard
    FIXR(1.0),
176
    FIXR(1.18920711500272106671),
177
    FIXR(1.41421356237309504880),
178
    FIXR(1.68179283050742908605),
179 de6d9b64 Fabrice Bellard
};
180
181 a05c8d71 Michael Niedermayer
static MPA_INT window[512] __attribute__((aligned(16)));
182 239c2f4c Fabrice Bellard
    
183
/* layer 1 unscaling */
184
/* n = number of bits of the mantissa minus 1 */
185
static inline int l1_unscale(int n, int mant, int scale_factor)
186
{
187
    int shift, mod;
188 0c1a9eda Zdenek Kabelac
    int64_t val;
189 239c2f4c Fabrice Bellard
190
    shift = scale_factor_modshift[scale_factor];
191
    mod = shift & 3;
192
    shift >>= 2;
193
    val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
194
    shift += n;
195 81552334 Fabrice Bellard
    /* NOTE: at this point, 1 <= shift >= 21 + 15 */
196
    return (int)((val + (1LL << (shift - 1))) >> shift);
197 239c2f4c Fabrice Bellard
}
198
199
static inline int l2_unscale_group(int steps, int mant, int scale_factor)
200
{
201
    int shift, mod, val;
202
203
    shift = scale_factor_modshift[scale_factor];
204
    mod = shift & 3;
205
    shift >>= 2;
206 81552334 Fabrice Bellard
207
    val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
208
    /* NOTE: at this point, 0 <= shift <= 21 */
209
    if (shift > 0)
210
        val = (val + (1 << (shift - 1))) >> shift;
211
    return val;
212 239c2f4c Fabrice Bellard
}
213
214
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
215
static inline int l3_unscale(int value, int exponent)
216
{
217
#if FRAC_BITS <= 15    
218
    unsigned int m;
219
#else
220 0c1a9eda Zdenek Kabelac
    uint64_t m;
221 239c2f4c Fabrice Bellard
#endif
222
    int e;
223
224
    e = table_4_3_exp[value];
225
    e += (exponent >> 2);
226
    e = FRAC_BITS - e;
227
#if FRAC_BITS <= 15    
228
    if (e > 31)
229
        e = 31;
230
#endif
231
    m = table_4_3_value[value];
232
#if FRAC_BITS <= 15    
233
    m = (m * scale_factor_mult3[exponent & 3]);
234
    m = (m + (1 << (e-1))) >> e;
235
    return m;
236
#else
237
    m = MUL64(m, scale_factor_mult3[exponent & 3]);
238 0c1a9eda Zdenek Kabelac
    m = (m + (uint64_t_C(1) << (e-1))) >> e;
239 239c2f4c Fabrice Bellard
    return m;
240
#endif
241
}
242
243 f9ed4f88 Fabrice Bellard
/* all integer n^(4/3) computation code */
244
#define DEV_ORDER 13
245
246
#define POW_FRAC_BITS 24
247
#define POW_FRAC_ONE    (1 << POW_FRAC_BITS)
248
#define POW_FIX(a)   ((int)((a) * POW_FRAC_ONE))
249 0c1a9eda Zdenek Kabelac
#define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
250 f9ed4f88 Fabrice Bellard
251
static int dev_4_3_coefs[DEV_ORDER];
252
253
static int pow_mult3[3] = {
254
    POW_FIX(1.0),
255
    POW_FIX(1.25992104989487316476),
256
    POW_FIX(1.58740105196819947474),
257
};
258
259
static void int_pow_init(void)
260
{
261
    int i, a;
262
263
    a = POW_FIX(1.0);
264
    for(i=0;i<DEV_ORDER;i++) {
265
        a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
266
        dev_4_3_coefs[i] = a;
267
    }
268
}
269
270
/* return the mantissa and the binary exponent */
271
static int int_pow(int i, int *exp_ptr)
272
{
273
    int e, er, eq, j;
274
    int a, a1;
275
    
276
    /* renormalize */
277
    a = i;
278
    e = POW_FRAC_BITS;
279
    while (a < (1 << (POW_FRAC_BITS - 1))) {
280
        a = a << 1;
281
        e--;
282
    }
283
    a -= (1 << POW_FRAC_BITS);
284
    a1 = 0;
285
    for(j = DEV_ORDER - 1; j >= 0; j--)
286
        a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
287
    a = (1 << POW_FRAC_BITS) + a1;
288
    /* exponent compute (exact) */
289
    e = e * 4;
290
    er = e % 3;
291
    eq = e / 3;
292
    a = POW_MULL(a, pow_mult3[er]);
293
    while (a >= 2 * POW_FRAC_ONE) {
294
        a = a >> 1;
295
        eq++;
296
    }
297
    /* convert to float */
298
    while (a < POW_FRAC_ONE) {
299
        a = a << 1;
300
        eq--;
301
    }
302 59d3e367 Fabrice Bellard
    /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
303 81552334 Fabrice Bellard
#if POW_FRAC_BITS > FRAC_BITS
304 59d3e367 Fabrice Bellard
    a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
305
    /* correct overflow */
306
    if (a >= 2 * (1 << FRAC_BITS)) {
307
        a = a >> 1;
308
        eq++;
309
    }
310
#endif
311 f9ed4f88 Fabrice Bellard
    *exp_ptr = eq;
312
    return a;
313
}
314 de6d9b64 Fabrice Bellard
315
static int decode_init(AVCodecContext * avctx)
316
{
317
    MPADecodeContext *s = avctx->priv_data;
318 b587a7cb Michael Niedermayer
    static int init=0;
319 239c2f4c Fabrice Bellard
    int i, j, k;
320 de6d9b64 Fabrice Bellard
321 ac806113 Michael Niedermayer
    if(avctx->antialias_algo == FF_AA_INT)
322
        s->compute_antialias= compute_antialias_integer;
323
    else
324
        s->compute_antialias= compute_antialias_float;
325
326 8c5b5683 Fabrice Bellard
    if (!init && !avctx->parse_only) {
327 239c2f4c Fabrice Bellard
        /* scale factors table for layer 1/2 */
328
        for(i=0;i<64;i++) {
329
            int shift, mod;
330
            /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
331 81552334 Fabrice Bellard
            shift = (i / 3);
332 239c2f4c Fabrice Bellard
            mod = i % 3;
333
            scale_factor_modshift[i] = mod | (shift << 2);
334
        }
335
336
        /* scale factor multiply for layer 1 */
337
        for(i=0;i<15;i++) {
338
            int n, norm;
339
            n = i + 2;
340 0c1a9eda Zdenek Kabelac
            norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
341 81552334 Fabrice Bellard
            scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
342
            scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
343
            scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
344 239c2f4c Fabrice Bellard
            dprintf("%d: norm=%x s=%x %x %x\n",
345
                    i, norm, 
346
                    scale_factor_mult[i][0],
347
                    scale_factor_mult[i][1],
348
                    scale_factor_mult[i][2]);
349
        }
350
        
351
        /* window */
352
        /* max = 18760, max sum over all 16 coefs : 44736 */
353
        for(i=0;i<257;i++) {
354
            int v;
355
            v = mpa_enwindow[i];
356
#if WFRAC_BITS < 16
357
            v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
358
#endif
359
            window[i] = v;
360
            if ((i & 63) != 0)
361
                v = -v;
362
            if (i != 0)
363
                window[512 - i] = v;
364
        }
365
        
366
        /* huffman decode tables */
367
        huff_code_table[0] = NULL;
368
        for(i=1;i<16;i++) {
369
            const HuffTable *h = &mpa_huff_tables[i];
370 5c91a675 Zdenek Kabelac
            int xsize, x, y;
371
            unsigned int n;
372 0c1a9eda Zdenek Kabelac
            uint8_t *code_table;
373 239c2f4c Fabrice Bellard
374
            xsize = h->xsize;
375
            n = xsize * xsize;
376
            /* XXX: fail test */
377
            init_vlc(&huff_vlc[i], 8, n, 
378
                     h->bits, 1, 1, h->codes, 2, 2);
379
            
380
            code_table = av_mallocz(n);
381
            j = 0;
382
            for(x=0;x<xsize;x++) {
383
                for(y=0;y<xsize;y++)
384
                    code_table[j++] = (x << 4) | y;
385
            }
386
            huff_code_table[i] = code_table;
387
        }
388
        for(i=0;i<2;i++) {
389
            init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16, 
390
                     mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1);
391
        }
392
393
        for(i=0;i<9;i++) {
394
            k = 0;
395
            for(j=0;j<22;j++) {
396
                band_index_long[i][j] = k;
397
                k += band_size_long[i][j];
398
            }
399
            band_index_long[i][22] = k;
400
        }
401
402 855ea723 Zdenek Kabelac
        /* compute n ^ (4/3) and store it in mantissa/exp format */
403 8d1f2ba5 Michael Niedermayer
        table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
404
        if(!table_4_3_exp)
405 855ea723 Zdenek Kabelac
            return -1;
406 8d1f2ba5 Michael Niedermayer
        table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
407
        if(!table_4_3_value)
408 239c2f4c Fabrice Bellard
            return -1;
409
        
410 f9ed4f88 Fabrice Bellard
        int_pow_init();
411 239c2f4c Fabrice Bellard
        for(i=1;i<TABLE_4_3_SIZE;i++) {
412
            int e, m;
413 f9ed4f88 Fabrice Bellard
            m = int_pow(i, &e);
414
#if 0
415
            /* test code */
416
            {
417
                double f, fm;
418
                int e1, m1;
419
                f = pow((double)i, 4.0 / 3.0);
420
                fm = frexp(f, &e1);
421
                m1 = FIXR(2 * fm);
422
#if FRAC_BITS <= 15
423 59d3e367 Fabrice Bellard
                if ((unsigned short)m1 != m1) {
424
                    m1 = m1 >> 1;
425
                    e1++;
426
                }
427 f9ed4f88 Fabrice Bellard
#endif
428
                e1--;
429
                if (m != m1 || e != e1) {
430
                    printf("%4d: m=%x m1=%x e=%d e1=%d\n",
431
                           i, m, m1, e, e1);
432
                }
433
            }
434
#endif
435 239c2f4c Fabrice Bellard
            /* normalized to FRAC_BITS */
436
            table_4_3_value[i] = m;
437 59d3e367 Fabrice Bellard
            table_4_3_exp[i] = e;
438 239c2f4c Fabrice Bellard
        }
439
        
440
        for(i=0;i<7;i++) {
441
            float f;
442
            int v;
443
            if (i != 6) {
444
                f = tan((double)i * M_PI / 12.0);
445
                v = FIXR(f / (1.0 + f));
446
            } else {
447
                v = FIXR(1.0);
448
            }
449
            is_table[0][i] = v;
450
            is_table[1][6 - i] = v;
451
        }
452
        /* invalid values */
453
        for(i=7;i<16;i++)
454
            is_table[0][i] = is_table[1][i] = 0.0;
455
456
        for(i=0;i<16;i++) {
457
            double f;
458
            int e, k;
459
460
            for(j=0;j<2;j++) {
461
                e = -(j + 1) * ((i + 1) >> 1);
462
                f = pow(2.0, e / 4.0);
463
                k = i & 1;
464
                is_table_lsf[j][k ^ 1][i] = FIXR(f);
465
                is_table_lsf[j][k][i] = FIXR(1.0);
466
                dprintf("is_table_lsf %d %d: %x %x\n", 
467
                        i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
468
            }
469
        }
470
471
        for(i=0;i<8;i++) {
472
            float ci, cs, ca;
473
            ci = ci_table[i];
474
            cs = 1.0 / sqrt(1.0 + ci * ci);
475
            ca = cs * ci;
476
            csa_table[i][0] = FIX(cs);
477
            csa_table[i][1] = FIX(ca);
478 a1e257b2 Michael Niedermayer
            csa_table[i][2] = FIX(ca) + FIX(cs);
479
            csa_table[i][3] = FIX(ca) - FIX(cs); 
480
            csa_table_float[i][0] = cs;
481
            csa_table_float[i][1] = ca;
482
            csa_table_float[i][2] = ca + cs;
483
            csa_table_float[i][3] = ca - cs; 
484
//            printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
485 239c2f4c Fabrice Bellard
        }
486
487
        /* compute mdct windows */
488
        for(i=0;i<36;i++) {
489
            int v;
490
            v = FIXR(sin(M_PI * (i + 0.5) / 36.0));
491
            mdct_win[0][i] = v;
492
            mdct_win[1][i] = v;
493
            mdct_win[3][i] = v;
494
        }
495
        for(i=0;i<6;i++) {
496
            mdct_win[1][18 + i] = FIXR(1.0);
497
            mdct_win[1][24 + i] = FIXR(sin(M_PI * ((i + 6) + 0.5) / 12.0));
498
            mdct_win[1][30 + i] = FIXR(0.0);
499
500
            mdct_win[3][i] = FIXR(0.0);
501
            mdct_win[3][6 + i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
502
            mdct_win[3][12 + i] = FIXR(1.0);
503
        }
504
505
        for(i=0;i<12;i++)
506
            mdct_win[2][i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
507
        
508
        /* NOTE: we do frequency inversion adter the MDCT by changing
509
           the sign of the right window coefs */
510
        for(j=0;j<4;j++) {
511
            for(i=0;i<36;i+=2) {
512
                mdct_win[j + 4][i] = mdct_win[j][i];
513
                mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
514
            }
515
        }
516
517
#if defined(DEBUG)
518
        for(j=0;j<8;j++) {
519
            printf("win%d=\n", j);
520
            for(i=0;i<36;i++)
521
                printf("%f, ", (double)mdct_win[j][i] / FRAC_ONE);
522
            printf("\n");
523
        }
524
#endif
525 de6d9b64 Fabrice Bellard
        init = 1;
526
    }
527
528
    s->inbuf_index = 0;
529
    s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
530
    s->inbuf_ptr = s->inbuf;
531 239c2f4c Fabrice Bellard
#ifdef DEBUG
532
    s->frame_count = 0;
533
#endif
534 de6d9b64 Fabrice Bellard
    return 0;
535
}
536
537 ef9f7306 Måns Rullgård
/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
538 239c2f4c Fabrice Bellard
539
/* cos(i*pi/64) */
540
541
#define COS0_0  FIXR(0.50060299823519630134)
542
#define COS0_1  FIXR(0.50547095989754365998)
543
#define COS0_2  FIXR(0.51544730992262454697)
544
#define COS0_3  FIXR(0.53104259108978417447)
545
#define COS0_4  FIXR(0.55310389603444452782)
546
#define COS0_5  FIXR(0.58293496820613387367)
547
#define COS0_6  FIXR(0.62250412303566481615)
548
#define COS0_7  FIXR(0.67480834145500574602)
549
#define COS0_8  FIXR(0.74453627100229844977)
550
#define COS0_9  FIXR(0.83934964541552703873)
551
#define COS0_10 FIXR(0.97256823786196069369)
552
#define COS0_11 FIXR(1.16943993343288495515)
553
#define COS0_12 FIXR(1.48416461631416627724)
554
#define COS0_13 FIXR(2.05778100995341155085)
555
#define COS0_14 FIXR(3.40760841846871878570)
556
#define COS0_15 FIXR(10.19000812354805681150)
557
558
#define COS1_0 FIXR(0.50241928618815570551)
559
#define COS1_1 FIXR(0.52249861493968888062)
560
#define COS1_2 FIXR(0.56694403481635770368)
561
#define COS1_3 FIXR(0.64682178335999012954)
562
#define COS1_4 FIXR(0.78815462345125022473)
563
#define COS1_5 FIXR(1.06067768599034747134)
564
#define COS1_6 FIXR(1.72244709823833392782)
565
#define COS1_7 FIXR(5.10114861868916385802)
566
567
#define COS2_0 FIXR(0.50979557910415916894)
568
#define COS2_1 FIXR(0.60134488693504528054)
569
#define COS2_2 FIXR(0.89997622313641570463)
570
#define COS2_3 FIXR(2.56291544774150617881)
571
572
#define COS3_0 FIXR(0.54119610014619698439)
573
#define COS3_1 FIXR(1.30656296487637652785)
574
575
#define COS4_0 FIXR(0.70710678118654752439)
576
577
/* butterfly operator */
578
#define BF(a, b, c)\
579
{\
580
    tmp0 = tab[a] + tab[b];\
581
    tmp1 = tab[a] - tab[b];\
582
    tab[a] = tmp0;\
583
    tab[b] = MULL(tmp1, c);\
584
}
585
586
#define BF1(a, b, c, d)\
587
{\
588
    BF(a, b, COS4_0);\
589
    BF(c, d, -COS4_0);\
590
    tab[c] += tab[d];\
591
}
592
593
#define BF2(a, b, c, d)\
594
{\
595
    BF(a, b, COS4_0);\
596
    BF(c, d, -COS4_0);\
597
    tab[c] += tab[d];\
598
    tab[a] += tab[c];\
599
    tab[c] += tab[b];\
600
    tab[b] += tab[d];\
601
}
602
603
#define ADD(a, b) tab[a] += tab[b]
604
605
/* DCT32 without 1/sqrt(2) coef zero scaling. */
606 0c1a9eda Zdenek Kabelac
static void dct32(int32_t *out, int32_t *tab)
607 239c2f4c Fabrice Bellard
{
608
    int tmp0, tmp1;
609
610
    /* pass 1 */
611
    BF(0, 31, COS0_0);
612
    BF(1, 30, COS0_1);
613
    BF(2, 29, COS0_2);
614
    BF(3, 28, COS0_3);
615
    BF(4, 27, COS0_4);
616
    BF(5, 26, COS0_5);
617
    BF(6, 25, COS0_6);
618
    BF(7, 24, COS0_7);
619
    BF(8, 23, COS0_8);
620
    BF(9, 22, COS0_9);
621
    BF(10, 21, COS0_10);
622
    BF(11, 20, COS0_11);
623
    BF(12, 19, COS0_12);
624
    BF(13, 18, COS0_13);
625
    BF(14, 17, COS0_14);
626
    BF(15, 16, COS0_15);
627
628
    /* pass 2 */
629
    BF(0, 15, COS1_0);
630
    BF(1, 14, COS1_1);
631
    BF(2, 13, COS1_2);
632
    BF(3, 12, COS1_3);
633
    BF(4, 11, COS1_4);
634
    BF(5, 10, COS1_5);
635
    BF(6,  9, COS1_6);
636
    BF(7,  8, COS1_7);
637
    
638
    BF(16, 31, -COS1_0);
639
    BF(17, 30, -COS1_1);
640
    BF(18, 29, -COS1_2);
641
    BF(19, 28, -COS1_3);
642
    BF(20, 27, -COS1_4);
643
    BF(21, 26, -COS1_5);
644
    BF(22, 25, -COS1_6);
645
    BF(23, 24, -COS1_7);
646
    
647
    /* pass 3 */
648
    BF(0, 7, COS2_0);
649
    BF(1, 6, COS2_1);
650
    BF(2, 5, COS2_2);
651
    BF(3, 4, COS2_3);
652
    
653
    BF(8, 15, -COS2_0);
654
    BF(9, 14, -COS2_1);
655
    BF(10, 13, -COS2_2);
656
    BF(11, 12, -COS2_3);
657
    
658
    BF(16, 23, COS2_0);
659
    BF(17, 22, COS2_1);
660
    BF(18, 21, COS2_2);
661
    BF(19, 20, COS2_3);
662
    
663
    BF(24, 31, -COS2_0);
664
    BF(25, 30, -COS2_1);
665
    BF(26, 29, -COS2_2);
666
    BF(27, 28, -COS2_3);
667
668
    /* pass 4 */
669
    BF(0, 3, COS3_0);
670
    BF(1, 2, COS3_1);
671
    
672
    BF(4, 7, -COS3_0);
673
    BF(5, 6, -COS3_1);
674
    
675
    BF(8, 11, COS3_0);
676
    BF(9, 10, COS3_1);
677
    
678
    BF(12, 15, -COS3_0);
679
    BF(13, 14, -COS3_1);
680
    
681
    BF(16, 19, COS3_0);
682
    BF(17, 18, COS3_1);
683
    
684
    BF(20, 23, -COS3_0);
685
    BF(21, 22, -COS3_1);
686
    
687
    BF(24, 27, COS3_0);
688
    BF(25, 26, COS3_1);
689
    
690
    BF(28, 31, -COS3_0);
691
    BF(29, 30, -COS3_1);
692
    
693
    /* pass 5 */
694
    BF1(0, 1, 2, 3);
695
    BF2(4, 5, 6, 7);
696
    BF1(8, 9, 10, 11);
697
    BF2(12, 13, 14, 15);
698
    BF1(16, 17, 18, 19);
699
    BF2(20, 21, 22, 23);
700
    BF1(24, 25, 26, 27);
701
    BF2(28, 29, 30, 31);
702
    
703
    /* pass 6 */
704
    
705
    ADD( 8, 12);
706
    ADD(12, 10);
707
    ADD(10, 14);
708
    ADD(14,  9);
709
    ADD( 9, 13);
710
    ADD(13, 11);
711
    ADD(11, 15);
712
713
    out[ 0] = tab[0];
714
    out[16] = tab[1];
715
    out[ 8] = tab[2];
716
    out[24] = tab[3];
717
    out[ 4] = tab[4];
718
    out[20] = tab[5];
719
    out[12] = tab[6];
720
    out[28] = tab[7];
721
    out[ 2] = tab[8];
722
    out[18] = tab[9];
723
    out[10] = tab[10];
724
    out[26] = tab[11];
725
    out[ 6] = tab[12];
726
    out[22] = tab[13];
727
    out[14] = tab[14];
728
    out[30] = tab[15];
729
    
730
    ADD(24, 28);
731
    ADD(28, 26);
732
    ADD(26, 30);
733
    ADD(30, 25);
734
    ADD(25, 29);
735
    ADD(29, 27);
736
    ADD(27, 31);
737
738
    out[ 1] = tab[16] + tab[24];
739
    out[17] = tab[17] + tab[25];
740
    out[ 9] = tab[18] + tab[26];
741
    out[25] = tab[19] + tab[27];
742
    out[ 5] = tab[20] + tab[28];
743
    out[21] = tab[21] + tab[29];
744
    out[13] = tab[22] + tab[30];
745
    out[29] = tab[23] + tab[31];
746
    out[ 3] = tab[24] + tab[20];
747
    out[19] = tab[25] + tab[21];
748
    out[11] = tab[26] + tab[22];
749
    out[27] = tab[27] + tab[23];
750
    out[ 7] = tab[28] + tab[18];
751
    out[23] = tab[29] + tab[19];
752
    out[15] = tab[30] + tab[17];
753
    out[31] = tab[31];
754
}
755
756
#define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
757
758
#if FRAC_BITS <= 15
759
760 8c5b5683 Fabrice Bellard
static inline int round_sample(int sum)
761
{
762
    int sum1;
763
    sum1 = (sum + (1 << (OUT_SHIFT - 1))) >> OUT_SHIFT;
764
    if (sum1 < -32768)
765
        sum1 = -32768;
766
    else if (sum1 > 32767)
767
        sum1 = 32767;
768
    return sum1;
769 239c2f4c Fabrice Bellard
}
770
771 8c5b5683 Fabrice Bellard
#if defined(ARCH_POWERPC_405)
772
773
/* signed 16x16 -> 32 multiply add accumulate */
774
#define MACS(rt, ra, rb) \
775
    asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
776
777
/* signed 16x16 -> 32 multiply */
778
#define MULS(ra, rb) \
779
    ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
780 239c2f4c Fabrice Bellard
781
#else
782
783 8c5b5683 Fabrice Bellard
/* signed 16x16 -> 32 multiply add accumulate */
784
#define MACS(rt, ra, rb) rt += (ra) * (rb)
785
786
/* signed 16x16 -> 32 multiply */
787
#define MULS(ra, rb) ((ra) * (rb))
788
789
#endif
790
791
#else
792
793
static inline int round_sample(int64_t sum) 
794
{
795
    int sum1;
796
    sum1 = (int)((sum + (int64_t_C(1) << (OUT_SHIFT - 1))) >> OUT_SHIFT);
797
    if (sum1 < -32768)
798
        sum1 = -32768;
799
    else if (sum1 > 32767)
800
        sum1 = 32767;
801
    return sum1;
802 239c2f4c Fabrice Bellard
}
803
804 8c5b5683 Fabrice Bellard
#define MULS(ra, rb) MUL64(ra, rb)
805
806
#endif
807
808
#define SUM8(sum, op, w, p) \
809 239c2f4c Fabrice Bellard
{                                               \
810 8c5b5683 Fabrice Bellard
    sum op MULS((w)[0 * 64], p[0 * 64]);\
811
    sum op MULS((w)[1 * 64], p[1 * 64]);\
812
    sum op MULS((w)[2 * 64], p[2 * 64]);\
813
    sum op MULS((w)[3 * 64], p[3 * 64]);\
814
    sum op MULS((w)[4 * 64], p[4 * 64]);\
815
    sum op MULS((w)[5 * 64], p[5 * 64]);\
816
    sum op MULS((w)[6 * 64], p[6 * 64]);\
817
    sum op MULS((w)[7 * 64], p[7 * 64]);\
818
}
819
820
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
821
{                                               \
822
    int tmp;\
823
    tmp = p[0 * 64];\
824
    sum1 op1 MULS((w1)[0 * 64], tmp);\
825
    sum2 op2 MULS((w2)[0 * 64], tmp);\
826
    tmp = p[1 * 64];\
827
    sum1 op1 MULS((w1)[1 * 64], tmp);\
828
    sum2 op2 MULS((w2)[1 * 64], tmp);\
829
    tmp = p[2 * 64];\
830
    sum1 op1 MULS((w1)[2 * 64], tmp);\
831
    sum2 op2 MULS((w2)[2 * 64], tmp);\
832
    tmp = p[3 * 64];\
833
    sum1 op1 MULS((w1)[3 * 64], tmp);\
834
    sum2 op2 MULS((w2)[3 * 64], tmp);\
835
    tmp = p[4 * 64];\
836
    sum1 op1 MULS((w1)[4 * 64], tmp);\
837
    sum2 op2 MULS((w2)[4 * 64], tmp);\
838
    tmp = p[5 * 64];\
839
    sum1 op1 MULS((w1)[5 * 64], tmp);\
840
    sum2 op2 MULS((w2)[5 * 64], tmp);\
841
    tmp = p[6 * 64];\
842
    sum1 op1 MULS((w1)[6 * 64], tmp);\
843
    sum2 op2 MULS((w2)[6 * 64], tmp);\
844
    tmp = p[7 * 64];\
845
    sum1 op1 MULS((w1)[7 * 64], tmp);\
846
    sum2 op2 MULS((w2)[7 * 64], tmp);\
847 239c2f4c Fabrice Bellard
}
848
849
850
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
851
   32 samples. */
852
/* XXX: optimize by avoiding ring buffer usage */
853
static void synth_filter(MPADecodeContext *s1,
854 0c1a9eda Zdenek Kabelac
                         int ch, int16_t *samples, int incr, 
855
                         int32_t sb_samples[SBLIMIT])
856 239c2f4c Fabrice Bellard
{
857 0c1a9eda Zdenek Kabelac
    int32_t tmp[32];
858 8c5b5683 Fabrice Bellard
    register MPA_INT *synth_buf;
859 491c4a10 Alex Beregszaszi
    register const MPA_INT *w, *w2, *p;
860 239c2f4c Fabrice Bellard
    int j, offset, v;
861 8c5b5683 Fabrice Bellard
    int16_t *samples2;
862 239c2f4c Fabrice Bellard
#if FRAC_BITS <= 15
863 8c5b5683 Fabrice Bellard
    int sum, sum2;
864 239c2f4c Fabrice Bellard
#else
865 8c5b5683 Fabrice Bellard
    int64_t sum, sum2;
866 239c2f4c Fabrice Bellard
#endif
867 8c5b5683 Fabrice Bellard
    
868 239c2f4c Fabrice Bellard
    dct32(tmp, sb_samples);
869
    
870
    offset = s1->synth_buf_offset[ch];
871
    synth_buf = s1->synth_buf[ch] + offset;
872
873
    for(j=0;j<32;j++) {
874
        v = tmp[j];
875
#if FRAC_BITS <= 15
876 81552334 Fabrice Bellard
        /* NOTE: can cause a loss in precision if very high amplitude
877
           sound */
878 239c2f4c Fabrice Bellard
        if (v > 32767)
879
            v = 32767;
880
        else if (v < -32768)
881
            v = -32768;
882
#endif
883
        synth_buf[j] = v;
884
    }
885
    /* copy to avoid wrap */
886
    memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
887
888 8c5b5683 Fabrice Bellard
    samples2 = samples + 31 * incr;
889 239c2f4c Fabrice Bellard
    w = window;
890 8c5b5683 Fabrice Bellard
    w2 = window + 31;
891
892 239c2f4c Fabrice Bellard
    sum = 0;
893 8c5b5683 Fabrice Bellard
    p = synth_buf + 16;
894
    SUM8(sum, +=, w, p);
895
    p = synth_buf + 48;
896
    SUM8(sum, -=, w + 32, p);
897
    *samples = round_sample(sum);
898
    samples += incr;
899 239c2f4c Fabrice Bellard
    w++;
900
901 8c5b5683 Fabrice Bellard
    /* we calculate two samples at the same time to avoid one memory
902
       access per two sample */
903
    for(j=1;j<16;j++) {
904 239c2f4c Fabrice Bellard
        sum = 0;
905 8c5b5683 Fabrice Bellard
        sum2 = 0;
906
        p = synth_buf + 16 + j;
907
        SUM8P2(sum, +=, sum2, -=, w, w2, p);
908
        p = synth_buf + 48 - j;
909
        SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
910
911
        *samples = round_sample(sum);
912
        samples += incr;
913
        *samples2 = round_sample(sum2);
914
        samples2 -= incr;
915 239c2f4c Fabrice Bellard
        w++;
916 8c5b5683 Fabrice Bellard
        w2--;
917 239c2f4c Fabrice Bellard
    }
918 8c5b5683 Fabrice Bellard
    
919
    p = synth_buf + 32;
920
    sum = 0;
921
    SUM8(sum, -=, w + 32, p);
922
    *samples = round_sample(sum);
923
924 239c2f4c Fabrice Bellard
    offset = (offset - 32) & 511;
925
    s1->synth_buf_offset[ch] = offset;
926
}
927
928
/* cos(pi*i/24) */
929
#define C1  FIXR(0.99144486137381041114)
930
#define C3  FIXR(0.92387953251128675612)
931
#define C5  FIXR(0.79335334029123516458)
932
#define C7  FIXR(0.60876142900872063941)
933
#define C9  FIXR(0.38268343236508977173)
934
#define C11 FIXR(0.13052619222005159154)
935
936
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
937
   cases. */
938
static void imdct12(int *out, int *in)
939
{
940
    int tmp;
941 0c1a9eda Zdenek Kabelac
    int64_t in1_3, in1_9, in4_3, in4_9;
942 239c2f4c Fabrice Bellard
943
    in1_3 = MUL64(in[1], C3);
944
    in1_9 = MUL64(in[1], C9);
945
    in4_3 = MUL64(in[4], C3);
946
    in4_9 = MUL64(in[4], C9);
947
    
948
    tmp = FRAC_RND(MUL64(in[0], C7) - in1_3 - MUL64(in[2], C11) + 
949
                   MUL64(in[3], C1) - in4_9 - MUL64(in[5], C5));
950
    out[0] = tmp;
951
    out[5] = -tmp;
952
    tmp = FRAC_RND(MUL64(in[0] - in[3], C9) - in1_3 + 
953
                   MUL64(in[2] + in[5], C3) - in4_9);
954
    out[1] = tmp;
955
    out[4] = -tmp;
956
    tmp = FRAC_RND(MUL64(in[0], C11) - in1_9 + MUL64(in[2], C7) -
957
                   MUL64(in[3], C5) + in4_3 - MUL64(in[5], C1));
958
    out[2] = tmp;
959
    out[3] = -tmp;
960
    tmp = FRAC_RND(MUL64(-in[0], C5) + in1_9 + MUL64(in[2], C1) + 
961
                   MUL64(in[3], C11) - in4_3 - MUL64(in[5], C7));
962
    out[6] = tmp;
963
    out[11] = tmp;
964
    tmp = FRAC_RND(MUL64(-in[0] + in[3], C3) - in1_9 + 
965
                   MUL64(in[2] + in[5], C9) + in4_3);
966
    out[7] = tmp;
967
    out[10] = tmp;
968
    tmp = FRAC_RND(-MUL64(in[0], C1) - in1_3 - MUL64(in[2], C5) -
969
                   MUL64(in[3], C7) - in4_9 - MUL64(in[5], C11));
970
    out[8] = tmp;
971
    out[9] = tmp;
972
}
973
974
#undef C1
975
#undef C3
976
#undef C5
977
#undef C7
978
#undef C9
979
#undef C11
980
981
/* cos(pi*i/18) */
982
#define C1 FIXR(0.98480775301220805936)
983
#define C2 FIXR(0.93969262078590838405)
984
#define C3 FIXR(0.86602540378443864676)
985
#define C4 FIXR(0.76604444311897803520)
986
#define C5 FIXR(0.64278760968653932632)
987
#define C6 FIXR(0.5)
988
#define C7 FIXR(0.34202014332566873304)
989
#define C8 FIXR(0.17364817766693034885)
990
991
/* 0.5 / cos(pi*(2*i+1)/36) */
992
static const int icos36[9] = {
993
    FIXR(0.50190991877167369479),
994
    FIXR(0.51763809020504152469),
995
    FIXR(0.55168895948124587824),
996
    FIXR(0.61038729438072803416),
997
    FIXR(0.70710678118654752439),
998
    FIXR(0.87172339781054900991),
999
    FIXR(1.18310079157624925896),
1000
    FIXR(1.93185165257813657349),
1001
    FIXR(5.73685662283492756461),
1002
};
1003
1004
static const int icos72[18] = {
1005
    /* 0.5 / cos(pi*(2*i+19)/72) */
1006
    FIXR(0.74009361646113053152),
1007
    FIXR(0.82133981585229078570),
1008
    FIXR(0.93057949835178895673),
1009
    FIXR(1.08284028510010010928),
1010
    FIXR(1.30656296487637652785),
1011
    FIXR(1.66275476171152078719),
1012
    FIXR(2.31011315767264929558),
1013
    FIXR(3.83064878777019433457),
1014
    FIXR(11.46279281302667383546),
1015
1016
    /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
1017
    FIXR(-0.67817085245462840086),
1018
    FIXR(-0.63023620700513223342),
1019
    FIXR(-0.59284452371708034528),
1020
    FIXR(-0.56369097343317117734),
1021
    FIXR(-0.54119610014619698439),
1022
    FIXR(-0.52426456257040533932),
1023
    FIXR(-0.51213975715725461845),
1024
    FIXR(-0.50431448029007636036),
1025
    FIXR(-0.50047634258165998492),
1026
};
1027
1028
/* using Lee like decomposition followed by hand coded 9 points DCT */
1029
static void imdct36(int *out, int *in)
1030
{
1031
    int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1032
    int tmp[18], *tmp1, *in1;
1033 0c1a9eda Zdenek Kabelac
    int64_t in3_3, in6_6;
1034 239c2f4c Fabrice Bellard
1035
    for(i=17;i>=1;i--)
1036
        in[i] += in[i-1];
1037
    for(i=17;i>=3;i-=2)
1038
        in[i] += in[i-2];
1039
1040
    for(j=0;j<2;j++) {
1041
        tmp1 = tmp + j;
1042
        in1 = in + j;
1043
1044
        in3_3 = MUL64(in1[2*3], C3);
1045
        in6_6 = MUL64(in1[2*6], C6);
1046
1047
        tmp1[0] = FRAC_RND(MUL64(in1[2*1], C1) + in3_3 + 
1048
                           MUL64(in1[2*5], C5) + MUL64(in1[2*7], C7));
1049
        tmp1[2] = in1[2*0] + FRAC_RND(MUL64(in1[2*2], C2) + 
1050
                                      MUL64(in1[2*4], C4) + in6_6 + 
1051
                                      MUL64(in1[2*8], C8));
1052
        tmp1[4] = FRAC_RND(MUL64(in1[2*1] - in1[2*5] - in1[2*7], C3));
1053
        tmp1[6] = FRAC_RND(MUL64(in1[2*2] - in1[2*4] - in1[2*8], C6)) - 
1054
            in1[2*6] + in1[2*0];
1055
        tmp1[8] = FRAC_RND(MUL64(in1[2*1], C5) - in3_3 - 
1056
                           MUL64(in1[2*5], C7) + MUL64(in1[2*7], C1));
1057
        tmp1[10] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C8) - 
1058
                                       MUL64(in1[2*4], C2) + in6_6 + 
1059
                                       MUL64(in1[2*8], C4));
1060
        tmp1[12] = FRAC_RND(MUL64(in1[2*1], C7) - in3_3 + 
1061
                            MUL64(in1[2*5], C1) - 
1062
                            MUL64(in1[2*7], C5));
1063
        tmp1[14] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C4) + 
1064
                                       MUL64(in1[2*4], C8) + in6_6 - 
1065
                                       MUL64(in1[2*8], C2));
1066
        tmp1[16] = in1[2*0] - in1[2*2] + in1[2*4] - in1[2*6] + in1[2*8];
1067
    }
1068
1069
    i = 0;
1070
    for(j=0;j<4;j++) {
1071
        t0 = tmp[i];
1072
        t1 = tmp[i + 2];
1073
        s0 = t1 + t0;
1074
        s2 = t1 - t0;
1075
1076
        t2 = tmp[i + 1];
1077
        t3 = tmp[i + 3];
1078
        s1 = MULL(t3 + t2, icos36[j]);
1079
        s3 = MULL(t3 - t2, icos36[8 - j]);
1080
        
1081
        t0 = MULL(s0 + s1, icos72[9 + 8 - j]);
1082
        t1 = MULL(s0 - s1, icos72[8 - j]);
1083
        out[18 + 9 + j] = t0;
1084
        out[18 + 8 - j] = t0;
1085
        out[9 + j] = -t1;
1086
        out[8 - j] = t1;
1087
        
1088
        t0 = MULL(s2 + s3, icos72[9+j]);
1089
        t1 = MULL(s2 - s3, icos72[j]);
1090
        out[18 + 9 + (8 - j)] = t0;
1091
        out[18 + j] = t0;
1092
        out[9 + (8 - j)] = -t1;
1093
        out[j] = t1;
1094
        i += 4;
1095
    }
1096
1097
    s0 = tmp[16];
1098
    s1 = MULL(tmp[17], icos36[4]);
1099
    t0 = MULL(s0 + s1, icos72[9 + 4]);
1100
    t1 = MULL(s0 - s1, icos72[4]);
1101
    out[18 + 9 + 4] = t0;
1102
    out[18 + 8 - 4] = t0;
1103
    out[9 + 4] = -t1;
1104
    out[8 - 4] = t1;
1105
}
1106
1107 de6d9b64 Fabrice Bellard
/* fast header check for resync */
1108 0c1a9eda Zdenek Kabelac
static int check_header(uint32_t header)
1109 de6d9b64 Fabrice Bellard
{
1110
    /* header */
1111
    if ((header & 0xffe00000) != 0xffe00000)
1112
        return -1;
1113
    /* layer check */
1114
    if (((header >> 17) & 3) == 0)
1115
        return -1;
1116 239c2f4c Fabrice Bellard
    /* bit rate */
1117
    if (((header >> 12) & 0xf) == 0xf)
1118 de6d9b64 Fabrice Bellard
        return -1;
1119
    /* frequency */
1120
    if (((header >> 10) & 3) == 3)
1121
        return -1;
1122
    return 0;
1123
}
1124
1125 239c2f4c Fabrice Bellard
/* header + layer + bitrate + freq + lsf/mpeg25 */
1126
#define SAME_HEADER_MASK \
1127
   (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
1128
1129 de6d9b64 Fabrice Bellard
/* header decoding. MUST check the header before because no
1130 239c2f4c Fabrice Bellard
   consistency check is done there. Return 1 if free format found and
1131
   that the frame size must be computed externally */
1132 0c1a9eda Zdenek Kabelac
static int decode_header(MPADecodeContext *s, uint32_t header)
1133 de6d9b64 Fabrice Bellard
{
1134 239c2f4c Fabrice Bellard
    int sample_rate, frame_size, mpeg25, padding;
1135
    int sample_rate_index, bitrate_index;
1136 de6d9b64 Fabrice Bellard
    if (header & (1<<20)) {
1137 239c2f4c Fabrice Bellard
        s->lsf = (header & (1<<19)) ? 0 : 1;
1138
        mpeg25 = 0;
1139 de6d9b64 Fabrice Bellard
    } else {
1140 239c2f4c Fabrice Bellard
        s->lsf = 1;
1141
        mpeg25 = 1;
1142 de6d9b64 Fabrice Bellard
    }
1143
    
1144
    s->layer = 4 - ((header >> 17) & 3);
1145
    /* extract frequency */
1146 239c2f4c Fabrice Bellard
    sample_rate_index = (header >> 10) & 3;
1147
    sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1148
    sample_rate_index += 3 * (s->lsf + mpeg25);
1149
    s->sample_rate_index = sample_rate_index;
1150
    s->error_protection = ((header >> 16) & 1) ^ 1;
1151 81552334 Fabrice Bellard
    s->sample_rate = sample_rate;
1152 de6d9b64 Fabrice Bellard
1153 239c2f4c Fabrice Bellard
    bitrate_index = (header >> 12) & 0xf;
1154
    padding = (header >> 9) & 1;
1155
    //extension = (header >> 8) & 1;
1156
    s->mode = (header >> 6) & 3;
1157
    s->mode_ext = (header >> 4) & 3;
1158
    //copyright = (header >> 3) & 1;
1159
    //original = (header >> 2) & 1;
1160
    //emphasis = header & 3;
1161 de6d9b64 Fabrice Bellard
1162 239c2f4c Fabrice Bellard
    if (s->mode == MPA_MONO)
1163
        s->nb_channels = 1;
1164
    else
1165
        s->nb_channels = 2;
1166 de6d9b64 Fabrice Bellard
    
1167 239c2f4c Fabrice Bellard
    if (bitrate_index != 0) {
1168
        frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1169
        s->bit_rate = frame_size * 1000;
1170
        switch(s->layer) {
1171
        case 1:
1172
            frame_size = (frame_size * 12000) / sample_rate;
1173
            frame_size = (frame_size + padding) * 4;
1174
            break;
1175
        case 2:
1176
            frame_size = (frame_size * 144000) / sample_rate;
1177
            frame_size += padding;
1178
            break;
1179
        default:
1180
        case 3:
1181
            frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1182
            frame_size += padding;
1183
            break;
1184
        }
1185
        s->frame_size = frame_size;
1186
    } else {
1187
        /* if no frame size computed, signal it */
1188
        if (!s->free_format_frame_size)
1189
            return 1;
1190
        /* free format: compute bitrate and real frame size from the
1191
           frame size we extracted by reading the bitstream */
1192
        s->frame_size = s->free_format_frame_size;
1193
        switch(s->layer) {
1194
        case 1:
1195
            s->frame_size += padding  * 4;
1196
            s->bit_rate = (s->frame_size * sample_rate) / 48000;
1197
            break;
1198
        case 2:
1199
            s->frame_size += padding;
1200
            s->bit_rate = (s->frame_size * sample_rate) / 144000;
1201
            break;
1202
        default:
1203
        case 3:
1204
            s->frame_size += padding;
1205
            s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1206
            break;
1207
        }
1208 de6d9b64 Fabrice Bellard
    }
1209 239c2f4c Fabrice Bellard
    
1210 fad9f495 Fabrice Bellard
#if defined(DEBUG)
1211 239c2f4c Fabrice Bellard
    printf("layer%d, %d Hz, %d kbits/s, ",
1212
           s->layer, s->sample_rate, s->bit_rate);
1213
    if (s->nb_channels == 2) {
1214
        if (s->layer == 3) {
1215
            if (s->mode_ext & MODE_EXT_MS_STEREO)
1216
                printf("ms-");
1217
            if (s->mode_ext & MODE_EXT_I_STEREO)
1218
                printf("i-");
1219
        }
1220
        printf("stereo");
1221
    } else {
1222
        printf("mono");
1223
    }
1224
    printf("\n");
1225 de6d9b64 Fabrice Bellard
#endif
1226 239c2f4c Fabrice Bellard
    return 0;
1227 de6d9b64 Fabrice Bellard
}
1228
1229 8c5b5683 Fabrice Bellard
/* useful helper to get mpeg audio stream infos. Return -1 if error in
1230 962d6ae6 Fabrice Bellard
   header, otherwise the coded frame size in bytes */
1231
int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1232 8c5b5683 Fabrice Bellard
{
1233
    MPADecodeContext s1, *s = &s1;
1234 2caa92d9 Michael Niedermayer
    memset( s, 0, sizeof(MPADecodeContext) );
1235 8c5b5683 Fabrice Bellard
1236
    if (check_header(head) != 0)
1237
        return -1;
1238
1239
    if (decode_header(s, head) != 0) {
1240
        return -1;
1241
    }
1242
1243
    switch(s->layer) {
1244
    case 1:
1245 962d6ae6 Fabrice Bellard
        avctx->frame_size = 384;
1246 8c5b5683 Fabrice Bellard
        break;
1247
    case 2:
1248 962d6ae6 Fabrice Bellard
        avctx->frame_size = 1152;
1249 8c5b5683 Fabrice Bellard
        break;
1250
    default:
1251
    case 3:
1252
        if (s->lsf)
1253 962d6ae6 Fabrice Bellard
            avctx->frame_size = 576;
1254 8c5b5683 Fabrice Bellard
        else
1255 962d6ae6 Fabrice Bellard
            avctx->frame_size = 1152;
1256 8c5b5683 Fabrice Bellard
        break;
1257
    }
1258
1259 962d6ae6 Fabrice Bellard
    avctx->sample_rate = s->sample_rate;
1260
    avctx->channels = s->nb_channels;
1261
    avctx->bit_rate = s->bit_rate;
1262
    avctx->sub_id = s->layer;
1263
    return s->frame_size;
1264 8c5b5683 Fabrice Bellard
}
1265
1266 239c2f4c Fabrice Bellard
/* return the number of decoded frames */
1267
static int mp_decode_layer1(MPADecodeContext *s)
1268 de6d9b64 Fabrice Bellard
{
1269 239c2f4c Fabrice Bellard
    int bound, i, v, n, ch, j, mant;
1270 0c1a9eda Zdenek Kabelac
    uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1271
    uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1272 239c2f4c Fabrice Bellard
1273
    if (s->mode == MPA_JSTEREO) 
1274
        bound = (s->mode_ext + 1) * 4;
1275
    else
1276
        bound = SBLIMIT;
1277
1278
    /* allocation bits */
1279
    for(i=0;i<bound;i++) {
1280
        for(ch=0;ch<s->nb_channels;ch++) {
1281
            allocation[ch][i] = get_bits(&s->gb, 4);
1282
        }
1283
    }
1284
    for(i=bound;i<SBLIMIT;i++) {
1285
        allocation[0][i] = get_bits(&s->gb, 4);
1286
    }
1287
1288
    /* scale factors */
1289
    for(i=0;i<bound;i++) {
1290
        for(ch=0;ch<s->nb_channels;ch++) {
1291
            if (allocation[ch][i])
1292
                scale_factors[ch][i] = get_bits(&s->gb, 6);
1293
        }
1294
    }
1295
    for(i=bound;i<SBLIMIT;i++) {
1296
        if (allocation[0][i]) {
1297
            scale_factors[0][i] = get_bits(&s->gb, 6);
1298
            scale_factors[1][i] = get_bits(&s->gb, 6);
1299
        }
1300
    }
1301 de6d9b64 Fabrice Bellard
    
1302 239c2f4c Fabrice Bellard
    /* compute samples */
1303
    for(j=0;j<12;j++) {
1304
        for(i=0;i<bound;i++) {
1305
            for(ch=0;ch<s->nb_channels;ch++) {
1306
                n = allocation[ch][i];
1307
                if (n) {
1308
                    mant = get_bits(&s->gb, n + 1);
1309
                    v = l1_unscale(n, mant, scale_factors[ch][i]);
1310
                } else {
1311
                    v = 0;
1312
                }
1313
                s->sb_samples[ch][j][i] = v;
1314
            }
1315
        }
1316
        for(i=bound;i<SBLIMIT;i++) {
1317
            n = allocation[0][i];
1318
            if (n) {
1319
                mant = get_bits(&s->gb, n + 1);
1320
                v = l1_unscale(n, mant, scale_factors[0][i]);
1321
                s->sb_samples[0][j][i] = v;
1322
                v = l1_unscale(n, mant, scale_factors[1][i]);
1323
                s->sb_samples[1][j][i] = v;
1324
            } else {
1325
                s->sb_samples[0][j][i] = 0;
1326
                s->sb_samples[1][j][i] = 0;
1327
            }
1328
        }
1329
    }
1330
    return 12;
1331
}
1332
1333
/* bitrate is in kb/s */
1334
int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1335
{
1336
    int ch_bitrate, table;
1337 de6d9b64 Fabrice Bellard
    
1338 239c2f4c Fabrice Bellard
    ch_bitrate = bitrate / nb_channels;
1339
    if (!lsf) {
1340
        if ((freq == 48000 && ch_bitrate >= 56) ||
1341
            (ch_bitrate >= 56 && ch_bitrate <= 80)) 
1342
            table = 0;
1343
        else if (freq != 48000 && ch_bitrate >= 96) 
1344
            table = 1;
1345
        else if (freq != 32000 && ch_bitrate <= 48) 
1346
            table = 2;
1347
        else 
1348
            table = 3;
1349
    } else {
1350
        table = 4;
1351
    }
1352
    return table;
1353
}
1354 de6d9b64 Fabrice Bellard
1355 239c2f4c Fabrice Bellard
static int mp_decode_layer2(MPADecodeContext *s)
1356
{
1357
    int sblimit; /* number of used subbands */
1358
    const unsigned char *alloc_table;
1359
    int table, bit_alloc_bits, i, j, ch, bound, v;
1360
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1361
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1362
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1363
    int scale, qindex, bits, steps, k, l, m, b;
1364 de6d9b64 Fabrice Bellard
1365 239c2f4c Fabrice Bellard
    /* select decoding table */
1366
    table = l2_select_table(s->bit_rate / 1000, s->nb_channels, 
1367
                            s->sample_rate, s->lsf);
1368
    sblimit = sblimit_table[table];
1369
    alloc_table = alloc_tables[table];
1370
1371
    if (s->mode == MPA_JSTEREO) 
1372
        bound = (s->mode_ext + 1) * 4;
1373
    else
1374
        bound = sblimit;
1375
1376
    dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1377 2caa92d9 Michael Niedermayer
1378
    /* sanity check */
1379
    if( bound > sblimit ) bound = sblimit;
1380
1381 239c2f4c Fabrice Bellard
    /* parse bit allocation */
1382
    j = 0;
1383
    for(i=0;i<bound;i++) {
1384
        bit_alloc_bits = alloc_table[j];
1385
        for(ch=0;ch<s->nb_channels;ch++) {
1386
            bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1387
        }
1388
        j += 1 << bit_alloc_bits;
1389
    }
1390
    for(i=bound;i<sblimit;i++) {
1391
        bit_alloc_bits = alloc_table[j];
1392
        v = get_bits(&s->gb, bit_alloc_bits);
1393
        bit_alloc[0][i] = v;
1394
        bit_alloc[1][i] = v;
1395
        j += 1 << bit_alloc_bits;
1396 de6d9b64 Fabrice Bellard
    }
1397 239c2f4c Fabrice Bellard
1398
#ifdef DEBUG
1399
    {
1400
        for(ch=0;ch<s->nb_channels;ch++) {
1401
            for(i=0;i<sblimit;i++)
1402
                printf(" %d", bit_alloc[ch][i]);
1403
            printf("\n");
1404
        }
1405
    }
1406
#endif
1407
1408
    /* scale codes */
1409
    for(i=0;i<sblimit;i++) {
1410
        for(ch=0;ch<s->nb_channels;ch++) {
1411
            if (bit_alloc[ch][i]) 
1412
                scale_code[ch][i] = get_bits(&s->gb, 2);
1413
        }
1414
    }
1415
    
1416
    /* scale factors */
1417
    for(i=0;i<sblimit;i++) {
1418
        for(ch=0;ch<s->nb_channels;ch++) {
1419
            if (bit_alloc[ch][i]) {
1420
                sf = scale_factors[ch][i];
1421
                switch(scale_code[ch][i]) {
1422
                default:
1423
                case 0:
1424
                    sf[0] = get_bits(&s->gb, 6);
1425
                    sf[1] = get_bits(&s->gb, 6);
1426
                    sf[2] = get_bits(&s->gb, 6);
1427
                    break;
1428
                case 2:
1429
                    sf[0] = get_bits(&s->gb, 6);
1430
                    sf[1] = sf[0];
1431
                    sf[2] = sf[0];
1432
                    break;
1433
                case 1:
1434
                    sf[0] = get_bits(&s->gb, 6);
1435
                    sf[2] = get_bits(&s->gb, 6);
1436
                    sf[1] = sf[0];
1437
                    break;
1438
                case 3:
1439
                    sf[0] = get_bits(&s->gb, 6);
1440
                    sf[2] = get_bits(&s->gb, 6);
1441
                    sf[1] = sf[2];
1442
                    break;
1443
                }
1444
            }
1445
        }
1446
    }
1447
1448
#ifdef DEBUG
1449
    for(ch=0;ch<s->nb_channels;ch++) {
1450
        for(i=0;i<sblimit;i++) {
1451
            if (bit_alloc[ch][i]) {
1452
                sf = scale_factors[ch][i];
1453
                printf(" %d %d %d", sf[0], sf[1], sf[2]);
1454
            } else {
1455
                printf(" -");
1456
            }
1457
        }
1458
        printf("\n");
1459
    }
1460
#endif
1461
1462
    /* samples */
1463
    for(k=0;k<3;k++) {
1464
        for(l=0;l<12;l+=3) {
1465
            j = 0;
1466
            for(i=0;i<bound;i++) {
1467
                bit_alloc_bits = alloc_table[j];
1468
                for(ch=0;ch<s->nb_channels;ch++) {
1469
                    b = bit_alloc[ch][i];
1470
                    if (b) {
1471
                        scale = scale_factors[ch][i][k];
1472
                        qindex = alloc_table[j+b];
1473
                        bits = quant_bits[qindex];
1474
                        if (bits < 0) {
1475
                            /* 3 values at the same time */
1476
                            v = get_bits(&s->gb, -bits);
1477
                            steps = quant_steps[qindex];
1478
                            s->sb_samples[ch][k * 12 + l + 0][i] = 
1479
                                l2_unscale_group(steps, v % steps, scale);
1480
                            v = v / steps;
1481
                            s->sb_samples[ch][k * 12 + l + 1][i] = 
1482
                                l2_unscale_group(steps, v % steps, scale);
1483
                            v = v / steps;
1484
                            s->sb_samples[ch][k * 12 + l + 2][i] = 
1485
                                l2_unscale_group(steps, v, scale);
1486
                        } else {
1487
                            for(m=0;m<3;m++) {
1488
                                v = get_bits(&s->gb, bits);
1489
                                v = l1_unscale(bits - 1, v, scale);
1490
                                s->sb_samples[ch][k * 12 + l + m][i] = v;
1491
                            }
1492
                        }
1493
                    } else {
1494
                        s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1495
                        s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1496
                        s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1497
                    }
1498
                }
1499
                /* next subband in alloc table */
1500
                j += 1 << bit_alloc_bits; 
1501
            }
1502
            /* XXX: find a way to avoid this duplication of code */
1503
            for(i=bound;i<sblimit;i++) {
1504
                bit_alloc_bits = alloc_table[j];
1505
                b = bit_alloc[0][i];
1506
                if (b) {
1507
                    int mant, scale0, scale1;
1508
                    scale0 = scale_factors[0][i][k];
1509
                    scale1 = scale_factors[1][i][k];
1510
                    qindex = alloc_table[j+b];
1511
                    bits = quant_bits[qindex];
1512
                    if (bits < 0) {
1513
                        /* 3 values at the same time */
1514
                        v = get_bits(&s->gb, -bits);
1515
                        steps = quant_steps[qindex];
1516
                        mant = v % steps;
1517
                        v = v / steps;
1518
                        s->sb_samples[0][k * 12 + l + 0][i] = 
1519
                            l2_unscale_group(steps, mant, scale0);
1520
                        s->sb_samples[1][k * 12 + l + 0][i] = 
1521
                            l2_unscale_group(steps, mant, scale1);
1522
                        mant = v % steps;
1523
                        v = v / steps;
1524
                        s->sb_samples[0][k * 12 + l + 1][i] = 
1525
                            l2_unscale_group(steps, mant, scale0);
1526
                        s->sb_samples[1][k * 12 + l + 1][i] = 
1527
                            l2_unscale_group(steps, mant, scale1);
1528
                        s->sb_samples[0][k * 12 + l + 2][i] = 
1529
                            l2_unscale_group(steps, v, scale0);
1530
                        s->sb_samples[1][k * 12 + l + 2][i] = 
1531
                            l2_unscale_group(steps, v, scale1);
1532
                    } else {
1533
                        for(m=0;m<3;m++) {
1534
                            mant = get_bits(&s->gb, bits);
1535
                            s->sb_samples[0][k * 12 + l + m][i] = 
1536
                                l1_unscale(bits - 1, mant, scale0);
1537
                            s->sb_samples[1][k * 12 + l + m][i] = 
1538
                                l1_unscale(bits - 1, mant, scale1);
1539
                        }
1540
                    }
1541
                } else {
1542
                    s->sb_samples[0][k * 12 + l + 0][i] = 0;
1543
                    s->sb_samples[0][k * 12 + l + 1][i] = 0;
1544
                    s->sb_samples[0][k * 12 + l + 2][i] = 0;
1545
                    s->sb_samples[1][k * 12 + l + 0][i] = 0;
1546
                    s->sb_samples[1][k * 12 + l + 1][i] = 0;
1547
                    s->sb_samples[1][k * 12 + l + 2][i] = 0;
1548
                }
1549
                /* next subband in alloc table */
1550
                j += 1 << bit_alloc_bits; 
1551
            }
1552
            /* fill remaining samples to zero */
1553
            for(i=sblimit;i<SBLIMIT;i++) {
1554
                for(ch=0;ch<s->nb_channels;ch++) {
1555
                    s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1556
                    s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1557
                    s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1558
                }
1559
            }
1560
        }
1561
    }
1562
    return 3 * 12;
1563 de6d9b64 Fabrice Bellard
}
1564
1565
/*
1566 239c2f4c Fabrice Bellard
 * Seek back in the stream for backstep bytes (at most 511 bytes)
1567 de6d9b64 Fabrice Bellard
 */
1568 5c91a675 Zdenek Kabelac
static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1569 de6d9b64 Fabrice Bellard
{
1570 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
1571 de6d9b64 Fabrice Bellard
1572
    /* compute current position in stream */
1573 228ef9dd Fabrice Bellard
    ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1574 8db1a1dd Michael Niedermayer
1575 de6d9b64 Fabrice Bellard
    /* copy old data before current one */
1576
    ptr -= backstep;
1577 239c2f4c Fabrice Bellard
    memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] + 
1578
           BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1579 de6d9b64 Fabrice Bellard
    /* init get bits again */
1580 68f593b4 Michael Niedermayer
    init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1581 de6d9b64 Fabrice Bellard
1582 239c2f4c Fabrice Bellard
    /* prepare next buffer */
1583
    s->inbuf_index ^= 1;
1584
    s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1585
    s->old_frame_size = s->frame_size;
1586
}
1587
1588
static inline void lsf_sf_expand(int *slen,
1589
                                 int sf, int n1, int n2, int n3)
1590
{
1591
    if (n3) {
1592
        slen[3] = sf % n3;
1593
        sf /= n3;
1594
    } else {
1595
        slen[3] = 0;
1596
    }
1597
    if (n2) {
1598
        slen[2] = sf % n2;
1599
        sf /= n2;
1600
    } else {
1601
        slen[2] = 0;
1602
    }
1603
    slen[1] = sf % n1;
1604
    sf /= n1;
1605
    slen[0] = sf;
1606
}
1607
1608
static void exponents_from_scale_factors(MPADecodeContext *s, 
1609
                                         GranuleDef *g,
1610 0c1a9eda Zdenek Kabelac
                                         int16_t *exponents)
1611 239c2f4c Fabrice Bellard
{
1612 0c1a9eda Zdenek Kabelac
    const uint8_t *bstab, *pretab;
1613 239c2f4c Fabrice Bellard
    int len, i, j, k, l, v0, shift, gain, gains[3];
1614 0c1a9eda Zdenek Kabelac
    int16_t *exp_ptr;
1615 239c2f4c Fabrice Bellard
1616
    exp_ptr = exponents;
1617
    gain = g->global_gain - 210;
1618
    shift = g->scalefac_scale + 1;
1619
1620
    bstab = band_size_long[s->sample_rate_index];
1621
    pretab = mpa_pretab[g->preflag];
1622
    for(i=0;i<g->long_end;i++) {
1623
        v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1624
        len = bstab[i];
1625
        for(j=len;j>0;j--)
1626
            *exp_ptr++ = v0;
1627
    }
1628
1629
    if (g->short_start < 13) {
1630
        bstab = band_size_short[s->sample_rate_index];
1631
        gains[0] = gain - (g->subblock_gain[0] << 3);
1632
        gains[1] = gain - (g->subblock_gain[1] << 3);
1633
        gains[2] = gain - (g->subblock_gain[2] << 3);
1634
        k = g->long_end;
1635
        for(i=g->short_start;i<13;i++) {
1636
            len = bstab[i];
1637
            for(l=0;l<3;l++) {
1638
                v0 = gains[l] - (g->scale_factors[k++] << shift);
1639
                for(j=len;j>0;j--)
1640
                *exp_ptr++ = v0;
1641
            }
1642
        }
1643
    }
1644
}
1645
1646
/* handle n = 0 too */
1647
static inline int get_bitsz(GetBitContext *s, int n)
1648
{
1649
    if (n == 0)
1650
        return 0;
1651
    else
1652
        return get_bits(s, n);
1653
}
1654
1655
static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1656 0c1a9eda Zdenek Kabelac
                          int16_t *exponents, int end_pos)
1657 239c2f4c Fabrice Bellard
{
1658
    int s_index;
1659
    int linbits, code, x, y, l, v, i, j, k, pos;
1660 8db1a1dd Michael Niedermayer
    GetBitContext last_gb;
1661 239c2f4c Fabrice Bellard
    VLC *vlc;
1662 0c1a9eda Zdenek Kabelac
    uint8_t *code_table;
1663 239c2f4c Fabrice Bellard
1664
    /* low frequencies (called big values) */
1665
    s_index = 0;
1666
    for(i=0;i<3;i++) {
1667
        j = g->region_size[i];
1668
        if (j == 0)
1669
            continue;
1670
        /* select vlc table */
1671
        k = g->table_select[i];
1672
        l = mpa_huff_data[k][0];
1673
        linbits = mpa_huff_data[k][1];
1674
        vlc = &huff_vlc[l];
1675
        code_table = huff_code_table[l];
1676
1677
        /* read huffcode and compute each couple */
1678
        for(;j>0;j--) {
1679
            if (get_bits_count(&s->gb) >= end_pos)
1680
                break;
1681
            if (code_table) {
1682
                code = get_vlc(&s->gb, vlc);
1683
                if (code < 0)
1684
                    return -1;
1685
                y = code_table[code];
1686
                x = y >> 4;
1687
                y = y & 0x0f;
1688
            } else {
1689
                x = 0;
1690
                y = 0;
1691
            }
1692
            dprintf("region=%d n=%d x=%d y=%d exp=%d\n", 
1693
                    i, g->region_size[i] - j, x, y, exponents[s_index]);
1694
            if (x) {
1695
                if (x == 15)
1696
                    x += get_bitsz(&s->gb, linbits);
1697
                v = l3_unscale(x, exponents[s_index]);
1698
                if (get_bits1(&s->gb))
1699
                    v = -v;
1700
            } else {
1701
                v = 0;
1702
            }
1703
            g->sb_hybrid[s_index++] = v;
1704
            if (y) {
1705
                if (y == 15)
1706
                    y += get_bitsz(&s->gb, linbits);
1707
                v = l3_unscale(y, exponents[s_index]);
1708
                if (get_bits1(&s->gb))
1709
                    v = -v;
1710
            } else {
1711
                v = 0;
1712
            }
1713
            g->sb_hybrid[s_index++] = v;
1714
        }
1715
    }
1716
            
1717
    /* high frequencies */
1718
    vlc = &huff_quad_vlc[g->count1table_select];
1719 8db1a1dd Michael Niedermayer
    last_gb.buffer = NULL;
1720 239c2f4c Fabrice Bellard
    while (s_index <= 572) {
1721
        pos = get_bits_count(&s->gb);
1722
        if (pos >= end_pos) {
1723 8db1a1dd Michael Niedermayer
            if (pos > end_pos && last_gb.buffer != NULL) {
1724 239c2f4c Fabrice Bellard
                /* some encoders generate an incorrect size for this
1725
                   part. We must go back into the data */
1726
                s_index -= 4;
1727 8db1a1dd Michael Niedermayer
                s->gb = last_gb;
1728 239c2f4c Fabrice Bellard
            }
1729
            break;
1730
        }
1731 8db1a1dd Michael Niedermayer
        last_gb= s->gb;
1732
1733 239c2f4c Fabrice Bellard
        code = get_vlc(&s->gb, vlc);
1734
        dprintf("t=%d code=%d\n", g->count1table_select, code);
1735
        if (code < 0)
1736
            return -1;
1737
        for(i=0;i<4;i++) {
1738
            if (code & (8 >> i)) {
1739
                /* non zero value. Could use a hand coded function for
1740
                   'one' value */
1741
                v = l3_unscale(1, exponents[s_index]);
1742
                if(get_bits1(&s->gb))
1743
                    v = -v;
1744
            } else {
1745
                v = 0;
1746
            }
1747
            g->sb_hybrid[s_index++] = v;
1748
        }
1749
    }
1750
    while (s_index < 576)
1751
        g->sb_hybrid[s_index++] = 0;
1752 de6d9b64 Fabrice Bellard
    return 0;
1753
}
1754
1755 239c2f4c Fabrice Bellard
/* Reorder short blocks from bitstream order to interleaved order. It
1756
   would be faster to do it in parsing, but the code would be far more
1757
   complicated */
1758
static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1759
{
1760
    int i, j, k, len;
1761 0c1a9eda Zdenek Kabelac
    int32_t *ptr, *dst, *ptr1;
1762
    int32_t tmp[576];
1763 239c2f4c Fabrice Bellard
1764
    if (g->block_type != 2)
1765
        return;
1766
1767
    if (g->switch_point) {
1768
        if (s->sample_rate_index != 8) {
1769
            ptr = g->sb_hybrid + 36;
1770
        } else {
1771
            ptr = g->sb_hybrid + 48;
1772
        }
1773
    } else {
1774
        ptr = g->sb_hybrid;
1775
    }
1776
    
1777
    for(i=g->short_start;i<13;i++) {
1778
        len = band_size_short[s->sample_rate_index][i];
1779
        ptr1 = ptr;
1780
        for(k=0;k<3;k++) {
1781
            dst = tmp + k;
1782
            for(j=len;j>0;j--) {
1783
                *dst = *ptr++;
1784
                dst += 3;
1785
            }
1786
        }
1787 0c1a9eda Zdenek Kabelac
        memcpy(ptr1, tmp, len * 3 * sizeof(int32_t));
1788 239c2f4c Fabrice Bellard
    }
1789
}
1790
1791
#define ISQRT2 FIXR(0.70710678118654752440)
1792
1793
static void compute_stereo(MPADecodeContext *s,
1794
                           GranuleDef *g0, GranuleDef *g1)
1795
{
1796
    int i, j, k, l;
1797 0c1a9eda Zdenek Kabelac
    int32_t v1, v2;
1798 239c2f4c Fabrice Bellard
    int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1799 0c1a9eda Zdenek Kabelac
    int32_t (*is_tab)[16];
1800
    int32_t *tab0, *tab1;
1801 239c2f4c Fabrice Bellard
    int non_zero_found_short[3];
1802
1803
    /* intensity stereo */
1804
    if (s->mode_ext & MODE_EXT_I_STEREO) {
1805
        if (!s->lsf) {
1806
            is_tab = is_table;
1807
            sf_max = 7;
1808
        } else {
1809
            is_tab = is_table_lsf[g1->scalefac_compress & 1];
1810
            sf_max = 16;
1811
        }
1812
            
1813
        tab0 = g0->sb_hybrid + 576;
1814
        tab1 = g1->sb_hybrid + 576;
1815
1816
        non_zero_found_short[0] = 0;
1817
        non_zero_found_short[1] = 0;
1818
        non_zero_found_short[2] = 0;
1819
        k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1820
        for(i = 12;i >= g1->short_start;i--) {
1821
            /* for last band, use previous scale factor */
1822
            if (i != 11)
1823
                k -= 3;
1824
            len = band_size_short[s->sample_rate_index][i];
1825
            for(l=2;l>=0;l--) {
1826
                tab0 -= len;
1827
                tab1 -= len;
1828
                if (!non_zero_found_short[l]) {
1829
                    /* test if non zero band. if so, stop doing i-stereo */
1830
                    for(j=0;j<len;j++) {
1831
                        if (tab1[j] != 0) {
1832
                            non_zero_found_short[l] = 1;
1833
                            goto found1;
1834
                        }
1835
                    }
1836
                    sf = g1->scale_factors[k + l];
1837
                    if (sf >= sf_max)
1838
                        goto found1;
1839
1840
                    v1 = is_tab[0][sf];
1841
                    v2 = is_tab[1][sf];
1842
                    for(j=0;j<len;j++) {
1843
                        tmp0 = tab0[j];
1844
                        tab0[j] = MULL(tmp0, v1);
1845
                        tab1[j] = MULL(tmp0, v2);
1846
                    }
1847
                } else {
1848
                found1:
1849
                    if (s->mode_ext & MODE_EXT_MS_STEREO) {
1850
                        /* lower part of the spectrum : do ms stereo
1851
                           if enabled */
1852
                        for(j=0;j<len;j++) {
1853
                            tmp0 = tab0[j];
1854
                            tmp1 = tab1[j];
1855
                            tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1856
                            tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1857
                        }
1858
                    }
1859
                }
1860
            }
1861
        }
1862
1863
        non_zero_found = non_zero_found_short[0] | 
1864
            non_zero_found_short[1] | 
1865
            non_zero_found_short[2];
1866
1867
        for(i = g1->long_end - 1;i >= 0;i--) {
1868
            len = band_size_long[s->sample_rate_index][i];
1869
            tab0 -= len;
1870
            tab1 -= len;
1871
            /* test if non zero band. if so, stop doing i-stereo */
1872
            if (!non_zero_found) {
1873
                for(j=0;j<len;j++) {
1874
                    if (tab1[j] != 0) {
1875
                        non_zero_found = 1;
1876
                        goto found2;
1877
                    }
1878
                }
1879
                /* for last band, use previous scale factor */
1880
                k = (i == 21) ? 20 : i;
1881
                sf = g1->scale_factors[k];
1882
                if (sf >= sf_max)
1883
                    goto found2;
1884
                v1 = is_tab[0][sf];
1885
                v2 = is_tab[1][sf];
1886
                for(j=0;j<len;j++) {
1887
                    tmp0 = tab0[j];
1888
                    tab0[j] = MULL(tmp0, v1);
1889
                    tab1[j] = MULL(tmp0, v2);
1890
                }
1891
            } else {
1892
            found2:
1893
                if (s->mode_ext & MODE_EXT_MS_STEREO) {
1894
                    /* lower part of the spectrum : do ms stereo
1895
                       if enabled */
1896
                    for(j=0;j<len;j++) {
1897
                        tmp0 = tab0[j];
1898
                        tmp1 = tab1[j];
1899
                        tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1900
                        tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1901
                    }
1902
                }
1903
            }
1904
        }
1905
    } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1906
        /* ms stereo ONLY */
1907
        /* NOTE: the 1/sqrt(2) normalization factor is included in the
1908
           global gain */
1909
        tab0 = g0->sb_hybrid;
1910
        tab1 = g1->sb_hybrid;
1911
        for(i=0;i<576;i++) {
1912
            tmp0 = tab0[i];
1913
            tmp1 = tab1[i];
1914
            tab0[i] = tmp0 + tmp1;
1915
            tab1[i] = tmp0 - tmp1;
1916
        }
1917
    }
1918
}
1919
1920 a1e257b2 Michael Niedermayer
static void compute_antialias_integer(MPADecodeContext *s,
1921 239c2f4c Fabrice Bellard
                              GranuleDef *g)
1922
{
1923 0c1a9eda Zdenek Kabelac
    int32_t *ptr, *p0, *p1, *csa;
1924 a1e257b2 Michael Niedermayer
    int n, i, j;
1925 239c2f4c Fabrice Bellard
1926
    /* we antialias only "long" bands */
1927
    if (g->block_type == 2) {
1928
        if (!g->switch_point)
1929
            return;
1930
        /* XXX: check this for 8000Hz case */
1931
        n = 1;
1932
    } else {
1933
        n = SBLIMIT - 1;
1934
    }
1935
    
1936
    ptr = g->sb_hybrid + 18;
1937
    for(i = n;i > 0;i--) {
1938
        p0 = ptr - 1;
1939
        p1 = ptr;
1940 a1e257b2 Michael Niedermayer
        csa = &csa_table[0][0];       
1941
        for(j=0;j<4;j++) {
1942
            int tmp0 = *p0;
1943
            int tmp1 = *p1;
1944
#if 0
1945
            *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1946
            *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1947
#else
1948
            int64_t tmp2= MUL64(tmp0 + tmp1, csa[0]);
1949
            *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2]));
1950
            *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3]));
1951
#endif
1952
            p0--; p1++;
1953
            csa += 4;
1954 239c2f4c Fabrice Bellard
            tmp0 = *p0;
1955
            tmp1 = *p1;
1956 a1e257b2 Michael Niedermayer
#if 0
1957 239c2f4c Fabrice Bellard
            *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1958
            *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1959 a1e257b2 Michael Niedermayer
#else
1960
            tmp2= MUL64(tmp0 + tmp1, csa[0]);
1961
            *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2]));
1962
            *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3]));
1963
#endif
1964
            p0--; p1++;
1965
            csa += 4;
1966 239c2f4c Fabrice Bellard
        }
1967 a1e257b2 Michael Niedermayer
        ptr += 18;       
1968
    }
1969
}
1970
1971
static void compute_antialias_float(MPADecodeContext *s,
1972
                              GranuleDef *g)
1973
{
1974
    int32_t *ptr, *p0, *p1;
1975
    int n, i, j;
1976
1977
    /* we antialias only "long" bands */
1978
    if (g->block_type == 2) {
1979
        if (!g->switch_point)
1980
            return;
1981
        /* XXX: check this for 8000Hz case */
1982
        n = 1;
1983
    } else {
1984
        n = SBLIMIT - 1;
1985
    }
1986
    
1987
    ptr = g->sb_hybrid + 18;
1988
    for(i = n;i > 0;i--) {
1989
        float *csa = &csa_table_float[0][0];       
1990
        p0 = ptr - 1;
1991
        p1 = ptr;
1992
        for(j=0;j<4;j++) {
1993
            float tmp0 = *p0;
1994
            float tmp1 = *p1;
1995
#if 1
1996
            *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]);
1997
            *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]);
1998
#else
1999
            float tmp2= (tmp0 + tmp1) * csa[0];
2000
            *p0 = lrintf(tmp2 - tmp1 * csa[2]);
2001
            *p1 = lrintf(tmp2 + tmp0 * csa[3]);
2002
#endif
2003
            p0--; p1++;
2004
            csa += 4;
2005
            tmp0 = *p0;
2006
            tmp1 = *p1;
2007
#if 1
2008
            *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]);
2009
            *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]);
2010
#else
2011
            tmp2= (tmp0 + tmp1) * csa[0];
2012
            *p0 = lrintf(tmp2 - tmp1 * csa[2]);
2013
            *p1 = lrintf(tmp2 + tmp0 * csa[3]);
2014
#endif
2015
            p0--; p1++;
2016
            csa += 4;
2017
        }
2018
        ptr += 18;       
2019 239c2f4c Fabrice Bellard
    }
2020
}
2021
2022
static void compute_imdct(MPADecodeContext *s,
2023
                          GranuleDef *g, 
2024 0c1a9eda Zdenek Kabelac
                          int32_t *sb_samples,
2025
                          int32_t *mdct_buf)
2026 239c2f4c Fabrice Bellard
{
2027 0c1a9eda Zdenek Kabelac
    int32_t *ptr, *win, *win1, *buf, *buf2, *out_ptr, *ptr1;
2028
    int32_t in[6];
2029
    int32_t out[36];
2030
    int32_t out2[12];
2031 239c2f4c Fabrice Bellard
    int i, j, k, mdct_long_end, v, sblimit;
2032
2033
    /* find last non zero block */
2034
    ptr = g->sb_hybrid + 576;
2035
    ptr1 = g->sb_hybrid + 2 * 18;
2036
    while (ptr >= ptr1) {
2037
        ptr -= 6;
2038
        v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2039
        if (v != 0)
2040
            break;
2041
    }
2042
    sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2043
2044
    if (g->block_type == 2) {
2045
        /* XXX: check for 8000 Hz */
2046
        if (g->switch_point)
2047
            mdct_long_end = 2;
2048
        else
2049
            mdct_long_end = 0;
2050
    } else {
2051
        mdct_long_end = sblimit;
2052
    }
2053
2054
    buf = mdct_buf;
2055
    ptr = g->sb_hybrid;
2056
    for(j=0;j<mdct_long_end;j++) {
2057
        imdct36(out, ptr);
2058
        /* apply window & overlap with previous buffer */
2059
        out_ptr = sb_samples + j;
2060
        /* select window */
2061
        if (g->switch_point && j < 2)
2062
            win1 = mdct_win[0];
2063
        else
2064
            win1 = mdct_win[g->block_type];
2065
        /* select frequency inversion */
2066
        win = win1 + ((4 * 36) & -(j & 1));
2067
        for(i=0;i<18;i++) {
2068
            *out_ptr = MULL(out[i], win[i]) + buf[i];
2069
            buf[i] = MULL(out[i + 18], win[i + 18]);
2070
            out_ptr += SBLIMIT;
2071
        }
2072
        ptr += 18;
2073
        buf += 18;
2074
    }
2075
    for(j=mdct_long_end;j<sblimit;j++) {
2076
        for(i=0;i<6;i++) {
2077
            out[i] = 0;
2078
            out[6 + i] = 0;
2079
            out[30+i] = 0;
2080
        }
2081
        /* select frequency inversion */
2082
        win = mdct_win[2] + ((4 * 36) & -(j & 1));
2083
        buf2 = out + 6;
2084
        for(k=0;k<3;k++) {
2085
            /* reorder input for short mdct */
2086
            ptr1 = ptr + k;
2087
            for(i=0;i<6;i++) {
2088
                in[i] = *ptr1;
2089
                ptr1 += 3;
2090
            }
2091
            imdct12(out2, in);
2092
            /* apply 12 point window and do small overlap */
2093
            for(i=0;i<6;i++) {
2094
                buf2[i] = MULL(out2[i], win[i]) + buf2[i];
2095
                buf2[i + 6] = MULL(out2[i + 6], win[i + 6]);
2096
            }
2097
            buf2 += 6;
2098
        }
2099
        /* overlap */
2100
        out_ptr = sb_samples + j;
2101
        for(i=0;i<18;i++) {
2102
            *out_ptr = out[i] + buf[i];
2103
            buf[i] = out[i + 18];
2104
            out_ptr += SBLIMIT;
2105
        }
2106
        ptr += 18;
2107
        buf += 18;
2108
    }
2109
    /* zero bands */
2110
    for(j=sblimit;j<SBLIMIT;j++) {
2111
        /* overlap */
2112
        out_ptr = sb_samples + j;
2113
        for(i=0;i<18;i++) {
2114
            *out_ptr = buf[i];
2115
            buf[i] = 0;
2116
            out_ptr += SBLIMIT;
2117
        }
2118
        buf += 18;
2119
    }
2120
}
2121
2122 747a67fb Fabrice Bellard
#if defined(DEBUG)
2123 0c1a9eda Zdenek Kabelac
void sample_dump(int fnum, int32_t *tab, int n)
2124 239c2f4c Fabrice Bellard
{
2125
    static FILE *files[16], *f;
2126
    char buf[512];
2127 81552334 Fabrice Bellard
    int i;
2128 0c1a9eda Zdenek Kabelac
    int32_t v;
2129 81552334 Fabrice Bellard
    
2130 239c2f4c Fabrice Bellard
    f = files[fnum];
2131
    if (!f) {
2132 81552334 Fabrice Bellard
        sprintf(buf, "/tmp/out%d.%s.pcm", 
2133
                fnum, 
2134
#ifdef USE_HIGHPRECISION
2135
                "hp"
2136
#else
2137
                "lp"
2138
#endif
2139
                );
2140 239c2f4c Fabrice Bellard
        f = fopen(buf, "w");
2141
        if (!f)
2142
            return;
2143
        files[fnum] = f;
2144
    }
2145
    
2146
    if (fnum == 0) {
2147
        static int pos = 0;
2148
        printf("pos=%d\n", pos);
2149
        for(i=0;i<n;i++) {
2150 81552334 Fabrice Bellard
            printf(" %0.4f", (double)tab[i] / FRAC_ONE);
2151 239c2f4c Fabrice Bellard
            if ((i % 18) == 17)
2152
                printf("\n");
2153
        }
2154
        pos += n;
2155
    }
2156 81552334 Fabrice Bellard
    for(i=0;i<n;i++) {
2157
        /* normalize to 23 frac bits */
2158
        v = tab[i] << (23 - FRAC_BITS);
2159 0c1a9eda Zdenek Kabelac
        fwrite(&v, 1, sizeof(int32_t), f);
2160 81552334 Fabrice Bellard
    }
2161 239c2f4c Fabrice Bellard
}
2162
#endif
2163
2164
2165
/* main layer3 decoding function */
2166
static int mp_decode_layer3(MPADecodeContext *s)
2167
{
2168
    int nb_granules, main_data_begin, private_bits;
2169
    int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2170
    GranuleDef granules[2][2], *g;
2171 0c1a9eda Zdenek Kabelac
    int16_t exponents[576];
2172 239c2f4c Fabrice Bellard
2173
    /* read side info */
2174
    if (s->lsf) {
2175
        main_data_begin = get_bits(&s->gb, 8);
2176
        if (s->nb_channels == 2)
2177
            private_bits = get_bits(&s->gb, 2);
2178
        else
2179
            private_bits = get_bits(&s->gb, 1);
2180
        nb_granules = 1;
2181
    } else {
2182
        main_data_begin = get_bits(&s->gb, 9);
2183
        if (s->nb_channels == 2)
2184
            private_bits = get_bits(&s->gb, 3);
2185
        else
2186
            private_bits = get_bits(&s->gb, 5);
2187
        nb_granules = 2;
2188
        for(ch=0;ch<s->nb_channels;ch++) {
2189
            granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2190
            granules[ch][1].scfsi = get_bits(&s->gb, 4);
2191
        }
2192
    }
2193
    
2194
    for(gr=0;gr<nb_granules;gr++) {
2195
        for(ch=0;ch<s->nb_channels;ch++) {
2196
            dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2197
            g = &granules[ch][gr];
2198
            g->part2_3_length = get_bits(&s->gb, 12);
2199
            g->big_values = get_bits(&s->gb, 9);
2200
            g->global_gain = get_bits(&s->gb, 8);
2201
            /* if MS stereo only is selected, we precompute the
2202
               1/sqrt(2) renormalization factor */
2203
            if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) == 
2204
                MODE_EXT_MS_STEREO)
2205
                g->global_gain -= 2;
2206
            if (s->lsf)
2207
                g->scalefac_compress = get_bits(&s->gb, 9);
2208
            else
2209
                g->scalefac_compress = get_bits(&s->gb, 4);
2210
            blocksplit_flag = get_bits(&s->gb, 1);
2211
            if (blocksplit_flag) {
2212
                g->block_type = get_bits(&s->gb, 2);
2213
                if (g->block_type == 0)
2214
                    return -1;
2215
                g->switch_point = get_bits(&s->gb, 1);
2216
                for(i=0;i<2;i++)
2217
                    g->table_select[i] = get_bits(&s->gb, 5);
2218
                for(i=0;i<3;i++) 
2219
                    g->subblock_gain[i] = get_bits(&s->gb, 3);
2220
                /* compute huffman coded region sizes */
2221
                if (g->block_type == 2)
2222
                    g->region_size[0] = (36 / 2);
2223
                else {
2224
                    if (s->sample_rate_index <= 2) 
2225
                        g->region_size[0] = (36 / 2);
2226
                    else if (s->sample_rate_index != 8) 
2227
                        g->region_size[0] = (54 / 2);
2228
                    else
2229
                        g->region_size[0] = (108 / 2);
2230
                }
2231
                g->region_size[1] = (576 / 2);
2232
            } else {
2233
                int region_address1, region_address2, l;
2234
                g->block_type = 0;
2235
                g->switch_point = 0;
2236
                for(i=0;i<3;i++)
2237
                    g->table_select[i] = get_bits(&s->gb, 5);
2238
                /* compute huffman coded region sizes */
2239
                region_address1 = get_bits(&s->gb, 4);
2240
                region_address2 = get_bits(&s->gb, 3);
2241
                dprintf("region1=%d region2=%d\n", 
2242
                        region_address1, region_address2);
2243
                g->region_size[0] = 
2244
                    band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2245
                l = region_address1 + region_address2 + 2;
2246
                /* should not overflow */
2247
                if (l > 22)
2248
                    l = 22;
2249
                g->region_size[1] = 
2250
                    band_index_long[s->sample_rate_index][l] >> 1;
2251
            }
2252
            /* convert region offsets to region sizes and truncate
2253
               size to big_values */
2254
            g->region_size[2] = (576 / 2);
2255
            j = 0;
2256
            for(i=0;i<3;i++) {
2257
                k = g->region_size[i];
2258
                if (k > g->big_values)
2259
                    k = g->big_values;
2260
                g->region_size[i] = k - j;
2261
                j = k;
2262
            }
2263
2264
            /* compute band indexes */
2265
            if (g->block_type == 2) {
2266
                if (g->switch_point) {
2267
                    /* if switched mode, we handle the 36 first samples as
2268
                       long blocks.  For 8000Hz, we handle the 48 first
2269
                       exponents as long blocks (XXX: check this!) */
2270
                    if (s->sample_rate_index <= 2)
2271
                        g->long_end = 8;
2272
                    else if (s->sample_rate_index != 8)
2273
                        g->long_end = 6;
2274
                    else
2275
                        g->long_end = 4; /* 8000 Hz */
2276
                    
2277
                    if (s->sample_rate_index != 8)
2278
                        g->short_start = 3;
2279
                    else
2280
                        g->short_start = 2; 
2281
                } else {
2282
                    g->long_end = 0;
2283
                    g->short_start = 0;
2284
                }
2285
            } else {
2286
                g->short_start = 13;
2287
                g->long_end = 22;
2288
            }
2289
            
2290
            g->preflag = 0;
2291
            if (!s->lsf)
2292
                g->preflag = get_bits(&s->gb, 1);
2293
            g->scalefac_scale = get_bits(&s->gb, 1);
2294
            g->count1table_select = get_bits(&s->gb, 1);
2295
            dprintf("block_type=%d switch_point=%d\n",
2296
                    g->block_type, g->switch_point);
2297
        }
2298
    }
2299
2300
    /* now we get bits from the main_data_begin offset */
2301
    dprintf("seekback: %d\n", main_data_begin);
2302
    seek_to_maindata(s, main_data_begin);
2303
2304
    for(gr=0;gr<nb_granules;gr++) {
2305
        for(ch=0;ch<s->nb_channels;ch++) {
2306
            g = &granules[ch][gr];
2307
            
2308
            bits_pos = get_bits_count(&s->gb);
2309
            
2310
            if (!s->lsf) {
2311 0c1a9eda Zdenek Kabelac
                uint8_t *sc;
2312 239c2f4c Fabrice Bellard
                int slen, slen1, slen2;
2313
2314
                /* MPEG1 scale factors */
2315
                slen1 = slen_table[0][g->scalefac_compress];
2316
                slen2 = slen_table[1][g->scalefac_compress];
2317
                dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2318
                if (g->block_type == 2) {
2319
                    n = g->switch_point ? 17 : 18;
2320
                    j = 0;
2321
                    for(i=0;i<n;i++)
2322
                        g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2323
                    for(i=0;i<18;i++)
2324
                        g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2325
                    for(i=0;i<3;i++)
2326
                        g->scale_factors[j++] = 0;
2327
                } else {
2328
                    sc = granules[ch][0].scale_factors;
2329
                    j = 0;
2330
                    for(k=0;k<4;k++) {
2331
                        n = (k == 0 ? 6 : 5);
2332
                        if ((g->scfsi & (0x8 >> k)) == 0) {
2333
                            slen = (k < 2) ? slen1 : slen2;
2334
                            for(i=0;i<n;i++)
2335
                                g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2336
                        } else {
2337
                            /* simply copy from last granule */
2338
                            for(i=0;i<n;i++) {
2339
                                g->scale_factors[j] = sc[j];
2340
                                j++;
2341
                            }
2342
                        }
2343
                    }
2344
                    g->scale_factors[j++] = 0;
2345
                }
2346 747a67fb Fabrice Bellard
#if defined(DEBUG)
2347 239c2f4c Fabrice Bellard
                {
2348
                    printf("scfsi=%x gr=%d ch=%d scale_factors:\n", 
2349
                           g->scfsi, gr, ch);
2350
                    for(i=0;i<j;i++)
2351
                        printf(" %d", g->scale_factors[i]);
2352
                    printf("\n");
2353
                }
2354
#endif
2355
            } else {
2356
                int tindex, tindex2, slen[4], sl, sf;
2357
2358
                /* LSF scale factors */
2359
                if (g->block_type == 2) {
2360
                    tindex = g->switch_point ? 2 : 1;
2361
                } else {
2362
                    tindex = 0;
2363
                }
2364
                sf = g->scalefac_compress;
2365
                if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2366
                    /* intensity stereo case */
2367
                    sf >>= 1;
2368
                    if (sf < 180) {
2369
                        lsf_sf_expand(slen, sf, 6, 6, 0);
2370
                        tindex2 = 3;
2371
                    } else if (sf < 244) {
2372
                        lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2373
                        tindex2 = 4;
2374
                    } else {
2375
                        lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2376
                        tindex2 = 5;
2377
                    }
2378
                } else {
2379
                    /* normal case */
2380
                    if (sf < 400) {
2381
                        lsf_sf_expand(slen, sf, 5, 4, 4);
2382
                        tindex2 = 0;
2383
                    } else if (sf < 500) {
2384
                        lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2385
                        tindex2 = 1;
2386
                    } else {
2387
                        lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2388
                        tindex2 = 2;
2389
                        g->preflag = 1;
2390
                    }
2391
                }
2392
2393
                j = 0;
2394
                for(k=0;k<4;k++) {
2395
                    n = lsf_nsf_table[tindex2][tindex][k];
2396
                    sl = slen[k];
2397
                    for(i=0;i<n;i++)
2398
                        g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2399
                }
2400
                /* XXX: should compute exact size */
2401
                for(;j<40;j++)
2402
                    g->scale_factors[j] = 0;
2403 747a67fb Fabrice Bellard
#if defined(DEBUG)
2404 239c2f4c Fabrice Bellard
                {
2405
                    printf("gr=%d ch=%d scale_factors:\n", 
2406
                           gr, ch);
2407
                    for(i=0;i<40;i++)
2408
                        printf(" %d", g->scale_factors[i]);
2409
                    printf("\n");
2410
                }
2411
#endif
2412
            }
2413
2414
            exponents_from_scale_factors(s, g, exponents);
2415
2416
            /* read Huffman coded residue */
2417
            if (huffman_decode(s, g, exponents,
2418
                               bits_pos + g->part2_3_length) < 0)
2419
                return -1;
2420 747a67fb Fabrice Bellard
#if defined(DEBUG)
2421
            sample_dump(0, g->sb_hybrid, 576);
2422 239c2f4c Fabrice Bellard
#endif
2423
2424
            /* skip extension bits */
2425
            bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2426
            if (bits_left < 0) {
2427
                dprintf("bits_left=%d\n", bits_left);
2428
                return -1;
2429
            }
2430
            while (bits_left >= 16) {
2431
                skip_bits(&s->gb, 16);
2432
                bits_left -= 16;
2433
            }
2434
            if (bits_left > 0)
2435
                skip_bits(&s->gb, bits_left);
2436
        } /* ch */
2437
2438
        if (s->nb_channels == 2)
2439
            compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2440
2441
        for(ch=0;ch<s->nb_channels;ch++) {
2442
            g = &granules[ch][gr];
2443
2444
            reorder_block(s, g);
2445 747a67fb Fabrice Bellard
#if defined(DEBUG)
2446 239c2f4c Fabrice Bellard
            sample_dump(0, g->sb_hybrid, 576);
2447
#endif
2448 a1e257b2 Michael Niedermayer
            s->compute_antialias(s, g);
2449 81552334 Fabrice Bellard
#if defined(DEBUG)
2450 239c2f4c Fabrice Bellard
            sample_dump(1, g->sb_hybrid, 576);
2451
#endif
2452
            compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]); 
2453 81552334 Fabrice Bellard
#if defined(DEBUG)
2454 239c2f4c Fabrice Bellard
            sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2455
#endif
2456
        }
2457
    } /* gr */
2458
    return nb_granules * 18;
2459
}
2460
2461
static int mp_decode_frame(MPADecodeContext *s, 
2462
                           short *samples)
2463
{
2464
    int i, nb_frames, ch;
2465
    short *samples_ptr;
2466
2467
    init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, 
2468 68f593b4 Michael Niedermayer
                  (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2469 239c2f4c Fabrice Bellard
    
2470
    /* skip error protection field */
2471
    if (s->error_protection)
2472
        get_bits(&s->gb, 16);
2473
2474
    dprintf("frame %d:\n", s->frame_count);
2475
    switch(s->layer) {
2476
    case 1:
2477
        nb_frames = mp_decode_layer1(s);
2478
        break;
2479
    case 2:
2480
        nb_frames = mp_decode_layer2(s);
2481
        break;
2482
    case 3:
2483
    default:
2484
        nb_frames = mp_decode_layer3(s);
2485
        break;
2486
    }
2487
#if defined(DEBUG)
2488
    for(i=0;i<nb_frames;i++) {
2489
        for(ch=0;ch<s->nb_channels;ch++) {
2490
            int j;
2491
            printf("%d-%d:", i, ch);
2492
            for(j=0;j<SBLIMIT;j++)
2493
                printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2494
            printf("\n");
2495
        }
2496
    }
2497
#endif
2498
    /* apply the synthesis filter */
2499
    for(ch=0;ch<s->nb_channels;ch++) {
2500
        samples_ptr = samples + ch;
2501
        for(i=0;i<nb_frames;i++) {
2502
            synth_filter(s, ch, samples_ptr, s->nb_channels,
2503
                         s->sb_samples[ch][i]);
2504
            samples_ptr += 32 * s->nb_channels;
2505
        }
2506
    }
2507
#ifdef DEBUG
2508
    s->frame_count++;        
2509
#endif
2510
    return nb_frames * 32 * sizeof(short) * s->nb_channels;
2511
}
2512
2513 de6d9b64 Fabrice Bellard
static int decode_frame(AVCodecContext * avctx,
2514
                        void *data, int *data_size,
2515 0c1a9eda Zdenek Kabelac
                        uint8_t * buf, int buf_size)
2516 de6d9b64 Fabrice Bellard
{
2517
    MPADecodeContext *s = avctx->priv_data;
2518 0c1a9eda Zdenek Kabelac
    uint32_t header;
2519
    uint8_t *buf_ptr;
2520 de6d9b64 Fabrice Bellard
    int len, out_size;
2521
    short *out_samples = data;
2522
2523
    buf_ptr = buf;
2524
    while (buf_size > 0) {
2525
        len = s->inbuf_ptr - s->inbuf;
2526
        if (s->frame_size == 0) {
2527 239c2f4c Fabrice Bellard
            /* special case for next header for first frame in free
2528
               format case (XXX: find a simpler method) */
2529
            if (s->free_format_next_header != 0) {
2530
                s->inbuf[0] = s->free_format_next_header >> 24;
2531
                s->inbuf[1] = s->free_format_next_header >> 16;
2532
                s->inbuf[2] = s->free_format_next_header >> 8;
2533
                s->inbuf[3] = s->free_format_next_header;
2534
                s->inbuf_ptr = s->inbuf + 4;
2535
                s->free_format_next_header = 0;
2536
                goto got_header;
2537
            }
2538
            /* no header seen : find one. We need at least HEADER_SIZE
2539
               bytes to parse it */
2540 de6d9b64 Fabrice Bellard
            len = HEADER_SIZE - len;
2541
            if (len > buf_size)
2542
                len = buf_size;
2543 92d24f49 Zdenek Kabelac
            if (len > 0) {
2544 2d83f323 Zdenek Kabelac
                memcpy(s->inbuf_ptr, buf_ptr, len);
2545
                buf_ptr += len;
2546
                buf_size -= len;
2547 c152c983 Zdenek Kabelac
                s->inbuf_ptr += len;
2548
            }
2549
            if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2550 239c2f4c Fabrice Bellard
            got_header:
2551 de6d9b64 Fabrice Bellard
                header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2552
                    (s->inbuf[2] << 8) | s->inbuf[3];
2553 92d24f49 Zdenek Kabelac
2554 de6d9b64 Fabrice Bellard
                if (check_header(header) < 0) {
2555
                    /* no sync found : move by one byte (inefficient, but simple!) */
2556 228ef9dd Fabrice Bellard
                    memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2557 de6d9b64 Fabrice Bellard
                    s->inbuf_ptr--;
2558 239c2f4c Fabrice Bellard
                    dprintf("skip %x\n", header);
2559
                    /* reset free format frame size to give a chance
2560
                       to get a new bitrate */
2561
                    s->free_format_frame_size = 0;
2562 de6d9b64 Fabrice Bellard
                } else {
2563 239c2f4c Fabrice Bellard
                    if (decode_header(s, header) == 1) {
2564 81552334 Fabrice Bellard
                        /* free format: prepare to compute frame size */
2565 2d83f323 Zdenek Kabelac
                        s->frame_size = -1;
2566 239c2f4c Fabrice Bellard
                    }
2567 81552334 Fabrice Bellard
                    /* update codec info */
2568
                    avctx->sample_rate = s->sample_rate;
2569
                    avctx->channels = s->nb_channels;
2570
                    avctx->bit_rate = s->bit_rate;
2571 98ce5991 Fabrice Bellard
                    avctx->sub_id = s->layer;
2572 8c5b5683 Fabrice Bellard
                    switch(s->layer) {
2573
                    case 1:
2574
                        avctx->frame_size = 384;
2575
                        break;
2576
                    case 2:
2577
                        avctx->frame_size = 1152;
2578
                        break;
2579
                    case 3:
2580
                        if (s->lsf)
2581
                            avctx->frame_size = 576;
2582
                        else
2583
                            avctx->frame_size = 1152;
2584
                        break;
2585
                    }
2586 de6d9b64 Fabrice Bellard
                }
2587
            }
2588 239c2f4c Fabrice Bellard
        } else if (s->frame_size == -1) {
2589
            /* free format : find next sync to compute frame size */
2590
            len = MPA_MAX_CODED_FRAME_SIZE - len;
2591
            if (len > buf_size)
2592
                len = buf_size;
2593
            if (len == 0) {
2594 3625e88a Zdenek Kabelac
                /* frame too long: resync */
2595 239c2f4c Fabrice Bellard
                s->frame_size = 0;
2596 228ef9dd Fabrice Bellard
                memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2597 3625e88a Zdenek Kabelac
                s->inbuf_ptr--;
2598 239c2f4c Fabrice Bellard
            } else {
2599 0c1a9eda Zdenek Kabelac
                uint8_t *p, *pend;
2600
                uint32_t header1;
2601 239c2f4c Fabrice Bellard
                int padding;
2602
2603
                memcpy(s->inbuf_ptr, buf_ptr, len);
2604
                /* check for header */
2605
                p = s->inbuf_ptr - 3;
2606
                pend = s->inbuf_ptr + len - 4;
2607
                while (p <= pend) {
2608
                    header = (p[0] << 24) | (p[1] << 16) |
2609
                        (p[2] << 8) | p[3];
2610
                    header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2611
                        (s->inbuf[2] << 8) | s->inbuf[3];
2612
                    /* check with high probability that we have a
2613
                       valid header */
2614
                    if ((header & SAME_HEADER_MASK) ==
2615
                        (header1 & SAME_HEADER_MASK)) {
2616
                        /* header found: update pointers */
2617
                        len = (p + 4) - s->inbuf_ptr;
2618
                        buf_ptr += len;
2619
                        buf_size -= len;
2620
                        s->inbuf_ptr = p;
2621
                        /* compute frame size */
2622
                        s->free_format_next_header = header;
2623
                        s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2624
                        padding = (header1 >> 9) & 1;
2625
                        if (s->layer == 1)
2626
                            s->free_format_frame_size -= padding * 4;
2627
                        else
2628
                            s->free_format_frame_size -= padding;
2629
                        dprintf("free frame size=%d padding=%d\n", 
2630
                                s->free_format_frame_size, padding);
2631
                        decode_header(s, header1);
2632
                        goto next_data;
2633
                    }
2634
                    p++;
2635
                }
2636
                /* not found: simply increase pointers */
2637
                buf_ptr += len;
2638
                s->inbuf_ptr += len;
2639
                buf_size -= len;
2640
            }
2641 de6d9b64 Fabrice Bellard
        } else if (len < s->frame_size) {
2642 de5123dc Zdenek Kabelac
            if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2643
                s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2644 de6d9b64 Fabrice Bellard
            len = s->frame_size - len;
2645
            if (len > buf_size)
2646
                len = buf_size;
2647
            memcpy(s->inbuf_ptr, buf_ptr, len);
2648
            buf_ptr += len;
2649
            s->inbuf_ptr += len;
2650
            buf_size -= len;
2651 8c5b5683 Fabrice Bellard
        }
2652
    next_data:
2653
        if (s->frame_size > 0 && 
2654
            (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2655
            if (avctx->parse_only) {
2656
                /* simply return the frame data */
2657
                *(uint8_t **)data = s->inbuf;
2658
                out_size = s->inbuf_ptr - s->inbuf;
2659
            } else {
2660
                out_size = mp_decode_frame(s, out_samples);
2661
            }
2662 de6d9b64 Fabrice Bellard
            s->inbuf_ptr = s->inbuf;
2663
            s->frame_size = 0;
2664
            *data_size = out_size;
2665
            break;
2666
        }
2667
    }
2668
    return buf_ptr - buf;
2669
}
2670
2671 4b1f4f23 Juanjo
AVCodec mp2_decoder =
2672 de6d9b64 Fabrice Bellard
{
2673 4b1f4f23 Juanjo
    "mp2",
2674 de6d9b64 Fabrice Bellard
    CODEC_TYPE_AUDIO,
2675
    CODEC_ID_MP2,
2676
    sizeof(MPADecodeContext),
2677
    decode_init,
2678
    NULL,
2679
    NULL,
2680
    decode_frame,
2681 8c5b5683 Fabrice Bellard
    CODEC_CAP_PARSE_ONLY,
2682 de6d9b64 Fabrice Bellard
};
2683 4b1f4f23 Juanjo
2684
AVCodec mp3_decoder =
2685
{
2686
    "mp3",
2687
    CODEC_TYPE_AUDIO,
2688 80783dc2 Fabrice Bellard
    CODEC_ID_MP3,
2689 4b1f4f23 Juanjo
    sizeof(MPADecodeContext),
2690
    decode_init,
2691
    NULL,
2692
    NULL,
2693
    decode_frame,
2694 8c5b5683 Fabrice Bellard
    CODEC_CAP_PARSE_ONLY,
2695 4b1f4f23 Juanjo
};