]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegaudiodec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / mpegaudiodec.c
1 /*
2  * MPEG Audio decoder
3  * Copyright (c) 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * MPEG Audio decoder
25  */
26
27 #define UNCHECKED_BITSTREAM_READER 1
28
29 #include "libavutil/audioconvert.h"
30 #include "avcodec.h"
31 #include "get_bits.h"
32 #include "mathops.h"
33 #include "mpegaudiodsp.h"
34
35 /*
36  * TODO:
37  *  - test lsf / mpeg25 extensively.
38  */
39
40 #include "mpegaudio.h"
41 #include "mpegaudiodecheader.h"
42
43 #define BACKSTEP_SIZE 512
44 #define EXTRABYTES 24
45
46 /* layer 3 "granule" */
47 typedef struct GranuleDef {
48     uint8_t scfsi;
49     int part2_3_length;
50     int big_values;
51     int global_gain;
52     int scalefac_compress;
53     uint8_t block_type;
54     uint8_t switch_point;
55     int table_select[3];
56     int subblock_gain[3];
57     uint8_t scalefac_scale;
58     uint8_t count1table_select;
59     int region_size[3]; /* number of huffman codes in each region */
60     int preflag;
61     int short_start, long_end; /* long/short band indexes */
62     uint8_t scale_factors[40];
63     DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
64 } GranuleDef;
65
66 typedef struct MPADecodeContext {
67     MPA_DECODE_HEADER
68     uint8_t last_buf[2 * BACKSTEP_SIZE + EXTRABYTES];
69     int last_buf_size;
70     /* next header (used in free format parsing) */
71     uint32_t free_format_next_header;
72     GetBitContext gb;
73     GetBitContext in_gb;
74     DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
75     int synth_buf_offset[MPA_MAX_CHANNELS];
76     DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
77     INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
78     GranuleDef granules[2][2]; /* Used in Layer 3 */
79     int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
80     int dither_state;
81     int err_recognition;
82     AVCodecContext* avctx;
83     MPADSPContext mpadsp;
84     AVFrame frame;
85 } MPADecodeContext;
86
87 #if CONFIG_FLOAT
88 #   define SHR(a,b)       ((a)*(1.0f/(1<<(b))))
89 #   define FIXR_OLD(a)    ((int)((a) * FRAC_ONE + 0.5))
90 #   define FIXR(x)        ((float)(x))
91 #   define FIXHR(x)       ((float)(x))
92 #   define MULH3(x, y, s) ((s)*(y)*(x))
93 #   define MULLx(x, y, s) ((y)*(x))
94 #   define RENAME(a) a ## _float
95 #   define OUT_FMT AV_SAMPLE_FMT_FLT
96 #else
97 #   define SHR(a,b)       ((a)>>(b))
98 /* WARNING: only correct for positive numbers */
99 #   define FIXR_OLD(a)    ((int)((a) * FRAC_ONE + 0.5))
100 #   define FIXR(a)        ((int)((a) * FRAC_ONE + 0.5))
101 #   define FIXHR(a)       ((int)((a) * (1LL<<32) + 0.5))
102 #   define MULH3(x, y, s) MULH((s)*(x), y)
103 #   define MULLx(x, y, s) MULL(x,y,s)
104 #   define RENAME(a)      a ## _fixed
105 #   define OUT_FMT AV_SAMPLE_FMT_S16
106 #endif
107
108 /****************/
109
110 #define HEADER_SIZE 4
111
112 #include "mpegaudiodata.h"
113 #include "mpegaudiodectab.h"
114
115 /* vlc structure for decoding layer 3 huffman tables */
116 static VLC huff_vlc[16];
117 static VLC_TYPE huff_vlc_tables[
118     0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
119   142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
120   ][2];
121 static const int huff_vlc_tables_sizes[16] = {
122     0,  128,  128,  128,  130,  128,  154,  166,
123   142,  204,  190,  170,  542,  460,  662,  414
124 };
125 static VLC huff_quad_vlc[2];
126 static VLC_TYPE  huff_quad_vlc_tables[128+16][2];
127 static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
128 /* computed from band_size_long */
129 static uint16_t band_index_long[9][23];
130 #include "mpegaudio_tablegen.h"
131 /* intensity stereo coef table */
132 static INTFLOAT is_table[2][16];
133 static INTFLOAT is_table_lsf[2][2][16];
134 static INTFLOAT csa_table[8][4];
135 /** Window for MDCT. Note that only the component [0,17] and [20,37] are used,
136     the components 18 and 19 are there only to assure 128-bit alignment for asm
137  */
138 DECLARE_ALIGNED(16, static INTFLOAT, mdct_win)[8][40];
139
140 static int16_t division_tab3[1<<6 ];
141 static int16_t division_tab5[1<<8 ];
142 static int16_t division_tab9[1<<11];
143
144 static int16_t * const division_tabs[4] = {
145     division_tab3, division_tab5, NULL, division_tab9
146 };
147
148 /* lower 2 bits: modulo 3, higher bits: shift */
149 static uint16_t scale_factor_modshift[64];
150 /* [i][j]:  2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
151 static int32_t scale_factor_mult[15][3];
152 /* mult table for layer 2 group quantization */
153
154 #define SCALE_GEN(v) \
155 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
156
157 static const int32_t scale_factor_mult2[3][3] = {
158     SCALE_GEN(4.0 / 3.0), /* 3 steps */
159     SCALE_GEN(4.0 / 5.0), /* 5 steps */
160     SCALE_GEN(4.0 / 9.0), /* 9 steps */
161 };
162
163 /**
164  * Convert region offsets to region sizes and truncate
165  * size to big_values.
166  */
167 static void ff_region_offset2size(GranuleDef *g)
168 {
169     int i, k, j = 0;
170     g->region_size[2] = 576 / 2;
171     for (i = 0; i < 3; i++) {
172         k = FFMIN(g->region_size[i], g->big_values);
173         g->region_size[i] = k - j;
174         j = k;
175     }
176 }
177
178 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g)
179 {
180     if (g->block_type == 2)
181         g->region_size[0] = (36 / 2);
182     else {
183         if (s->sample_rate_index <= 2)
184             g->region_size[0] = (36 / 2);
185         else if (s->sample_rate_index != 8)
186             g->region_size[0] = (54 / 2);
187         else
188             g->region_size[0] = (108 / 2);
189     }
190     g->region_size[1] = (576 / 2);
191 }
192
193 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
194 {
195     int l;
196     g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
197     /* should not overflow */
198     l = FFMIN(ra1 + ra2 + 2, 22);
199     g->region_size[1] = band_index_long[s->sample_rate_index][      l] >> 1;
200 }
201
202 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
203 {
204     if (g->block_type == 2) {
205         if (g->switch_point) {
206             /* if switched mode, we handle the 36 first samples as
207                 long blocks.  For 8000Hz, we handle the 48 first
208                 exponents as long blocks (XXX: check this!) */
209             if (s->sample_rate_index <= 2)
210                 g->long_end = 8;
211             else if (s->sample_rate_index != 8)
212                 g->long_end = 6;
213             else
214                 g->long_end = 4; /* 8000 Hz */
215
216             g->short_start = 2 + (s->sample_rate_index != 8);
217         } else {
218             g->long_end    = 0;
219             g->short_start = 0;
220         }
221     } else {
222         g->short_start = 13;
223         g->long_end    = 22;
224     }
225 }
226
227 /* layer 1 unscaling */
228 /* n = number of bits of the mantissa minus 1 */
229 static inline int l1_unscale(int n, int mant, int scale_factor)
230 {
231     int shift, mod;
232     int64_t val;
233
234     shift   = scale_factor_modshift[scale_factor];
235     mod     = shift & 3;
236     shift >>= 2;
237     val     = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
238     shift  += n;
239     /* NOTE: at this point, 1 <= shift >= 21 + 15 */
240     return (int)((val + (1LL << (shift - 1))) >> shift);
241 }
242
243 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
244 {
245     int shift, mod, val;
246
247     shift   = scale_factor_modshift[scale_factor];
248     mod     = shift & 3;
249     shift >>= 2;
250
251     val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
252     /* NOTE: at this point, 0 <= shift <= 21 */
253     if (shift > 0)
254         val = (val + (1 << (shift - 1))) >> shift;
255     return val;
256 }
257
258 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
259 static inline int l3_unscale(int value, int exponent)
260 {
261     unsigned int m;
262     int e;
263
264     e  = table_4_3_exp  [4 * value + (exponent & 3)];
265     m  = table_4_3_value[4 * value + (exponent & 3)];
266     e -= exponent >> 2;
267     assert(e >= 1);
268     if (e > 31)
269         return 0;
270     m = (m + (1 << (e - 1))) >> e;
271
272     return m;
273 }
274
275 static av_cold void decode_init_static(void)
276 {
277     int i, j, k;
278     int offset;
279
280     /* scale factors table for layer 1/2 */
281     for (i = 0; i < 64; i++) {
282         int shift, mod;
283         /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
284         shift = i / 3;
285         mod   = i % 3;
286         scale_factor_modshift[i] = mod | (shift << 2);
287     }
288
289     /* scale factor multiply for layer 1 */
290     for (i = 0; i < 15; i++) {
291         int n, norm;
292         n = i + 2;
293         norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
294         scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0          * 2.0), FRAC_BITS);
295         scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
296         scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
297         av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
298                 scale_factor_mult[i][0],
299                 scale_factor_mult[i][1],
300                 scale_factor_mult[i][2]);
301     }
302
303     RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
304
305     /* huffman decode tables */
306     offset = 0;
307     for (i = 1; i < 16; i++) {
308         const HuffTable *h = &mpa_huff_tables[i];
309         int xsize, x, y;
310         uint8_t  tmp_bits [512];
311         uint16_t tmp_codes[512];
312
313         memset(tmp_bits , 0, sizeof(tmp_bits ));
314         memset(tmp_codes, 0, sizeof(tmp_codes));
315
316         xsize = h->xsize;
317
318         j = 0;
319         for (x = 0; x < xsize; x++) {
320             for (y = 0; y < xsize; y++) {
321                 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j  ];
322                 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
323             }
324         }
325
326         /* XXX: fail test */
327         huff_vlc[i].table = huff_vlc_tables+offset;
328         huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
329         init_vlc(&huff_vlc[i], 7, 512,
330                  tmp_bits, 1, 1, tmp_codes, 2, 2,
331                  INIT_VLC_USE_NEW_STATIC);
332         offset += huff_vlc_tables_sizes[i];
333     }
334     assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
335
336     offset = 0;
337     for (i = 0; i < 2; i++) {
338         huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
339         huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
340         init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
341                  mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
342                  INIT_VLC_USE_NEW_STATIC);
343         offset += huff_quad_vlc_tables_sizes[i];
344     }
345     assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
346
347     for (i = 0; i < 9; i++) {
348         k = 0;
349         for (j = 0; j < 22; j++) {
350             band_index_long[i][j] = k;
351             k += band_size_long[i][j];
352         }
353         band_index_long[i][22] = k;
354     }
355
356     /* compute n ^ (4/3) and store it in mantissa/exp format */
357
358     mpegaudio_tableinit();
359
360     for (i = 0; i < 4; i++) {
361         if (ff_mpa_quant_bits[i] < 0) {
362             for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
363                 int val1, val2, val3, steps;
364                 int val = j;
365                 steps   = ff_mpa_quant_steps[i];
366                 val1    = val % steps;
367                 val    /= steps;
368                 val2    = val % steps;
369                 val3    = val / steps;
370                 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
371             }
372         }
373     }
374
375
376     for (i = 0; i < 7; i++) {
377         float f;
378         INTFLOAT v;
379         if (i != 6) {
380             f = tan((double)i * M_PI / 12.0);
381             v = FIXR(f / (1.0 + f));
382         } else {
383             v = FIXR(1.0);
384         }
385         is_table[0][    i] = v;
386         is_table[1][6 - i] = v;
387     }
388     /* invalid values */
389     for (i = 7; i < 16; i++)
390         is_table[0][i] = is_table[1][i] = 0.0;
391
392     for (i = 0; i < 16; i++) {
393         double f;
394         int e, k;
395
396         for (j = 0; j < 2; j++) {
397             e = -(j + 1) * ((i + 1) >> 1);
398             f = pow(2.0, e / 4.0);
399             k = i & 1;
400             is_table_lsf[j][k ^ 1][i] = FIXR(f);
401             is_table_lsf[j][k    ][i] = FIXR(1.0);
402             av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
403                     i, j, (float) is_table_lsf[j][0][i],
404                     (float) is_table_lsf[j][1][i]);
405         }
406     }
407
408     for (i = 0; i < 8; i++) {
409         float ci, cs, ca;
410         ci = ci_table[i];
411         cs = 1.0 / sqrt(1.0 + ci * ci);
412         ca = cs * ci;
413 #if !CONFIG_FLOAT
414         csa_table[i][0] = FIXHR(cs/4);
415         csa_table[i][1] = FIXHR(ca/4);
416         csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
417         csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
418 #else
419         csa_table[i][0] = cs;
420         csa_table[i][1] = ca;
421         csa_table[i][2] = ca + cs;
422         csa_table[i][3] = ca - cs;
423 #endif
424     }
425
426     /* compute mdct windows */
427     for (i = 0; i < 36; i++) {
428         for (j = 0; j < 4; j++) {
429             double d;
430
431             if (j == 2 && i % 3 != 1)
432                 continue;
433
434             d = sin(M_PI * (i + 0.5) / 36.0);
435             if (j == 1) {
436                 if      (i >= 30) d = 0;
437                 else if (i >= 24) d = sin(M_PI * (i - 18 + 0.5) / 12.0);
438                 else if (i >= 18) d = 1;
439             } else if (j == 3) {
440                 if      (i <   6) d = 0;
441                 else if (i <  12) d = sin(M_PI * (i -  6 + 0.5) / 12.0);
442                 else if (i <  18) d = 1;
443             }
444             //merge last stage of imdct into the window coefficients
445             d *= 0.5 / cos(M_PI * (2 * i + 19) / 72);
446
447             if (j == 2)
448                 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
449             else {
450                 int idx = i < 18 ? i : i + 2;
451                 mdct_win[j][idx] = FIXHR((d / (1<<5)));
452             }
453         }
454     }
455
456     /* NOTE: we do frequency inversion adter the MDCT by changing
457         the sign of the right window coefs */
458     for (j = 0; j < 4; j++) {
459         for (i = 0; i < 40; i += 2) {
460             mdct_win[j + 4][i    ] =  mdct_win[j][i    ];
461             mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
462         }
463     }
464 }
465
466 static av_cold int decode_init(AVCodecContext * avctx)
467 {
468     static int initialized_tables = 0;
469     MPADecodeContext *s = avctx->priv_data;
470
471     if (!initialized_tables) {
472         decode_init_static();
473         initialized_tables = 1;
474     }
475
476     s->avctx = avctx;
477
478     ff_mpadsp_init(&s->mpadsp);
479
480     avctx->sample_fmt= OUT_FMT;
481     s->err_recognition = avctx->err_recognition;
482
483     if (avctx->codec_id == CODEC_ID_MP3ADU)
484         s->adu_mode = 1;
485
486     avcodec_get_frame_defaults(&s->frame);
487     avctx->coded_frame = &s->frame;
488
489     return 0;
490 }
491
492 #define C3 FIXHR(0.86602540378443864676/2)
493 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
494 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
495 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
496
497 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
498    cases. */
499 static void imdct12(INTFLOAT *out, INTFLOAT *in)
500 {
501     INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
502
503     in0  = in[0*3];
504     in1  = in[1*3] + in[0*3];
505     in2  = in[2*3] + in[1*3];
506     in3  = in[3*3] + in[2*3];
507     in4  = in[4*3] + in[3*3];
508     in5  = in[5*3] + in[4*3];
509     in5 += in3;
510     in3 += in1;
511
512     in2  = MULH3(in2, C3, 2);
513     in3  = MULH3(in3, C3, 4);
514
515     t1   = in0 - in4;
516     t2   = MULH3(in1 - in5, C4, 2);
517
518     out[ 7] =
519     out[10] = t1 + t2;
520     out[ 1] =
521     out[ 4] = t1 - t2;
522
523     in0    += SHR(in4, 1);
524     in4     = in0 + in2;
525     in5    += 2*in1;
526     in1     = MULH3(in5 + in3, C5, 1);
527     out[ 8] =
528     out[ 9] = in4 + in1;
529     out[ 2] =
530     out[ 3] = in4 - in1;
531
532     in0    -= in2;
533     in5     = MULH3(in5 - in3, C6, 2);
534     out[ 0] =
535     out[ 5] = in0 - in5;
536     out[ 6] =
537     out[11] = in0 + in5;
538 }
539
540 /* return the number of decoded frames */
541 static int mp_decode_layer1(MPADecodeContext *s)
542 {
543     int bound, i, v, n, ch, j, mant;
544     uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
545     uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
546
547     if (s->mode == MPA_JSTEREO)
548         bound = (s->mode_ext + 1) * 4;
549     else
550         bound = SBLIMIT;
551
552     /* allocation bits */
553     for (i = 0; i < bound; i++) {
554         for (ch = 0; ch < s->nb_channels; ch++) {
555             allocation[ch][i] = get_bits(&s->gb, 4);
556         }
557     }
558     for (i = bound; i < SBLIMIT; i++)
559         allocation[0][i] = get_bits(&s->gb, 4);
560
561     /* scale factors */
562     for (i = 0; i < bound; i++) {
563         for (ch = 0; ch < s->nb_channels; ch++) {
564             if (allocation[ch][i])
565                 scale_factors[ch][i] = get_bits(&s->gb, 6);
566         }
567     }
568     for (i = bound; i < SBLIMIT; i++) {
569         if (allocation[0][i]) {
570             scale_factors[0][i] = get_bits(&s->gb, 6);
571             scale_factors[1][i] = get_bits(&s->gb, 6);
572         }
573     }
574
575     /* compute samples */
576     for (j = 0; j < 12; j++) {
577         for (i = 0; i < bound; i++) {
578             for (ch = 0; ch < s->nb_channels; ch++) {
579                 n = allocation[ch][i];
580                 if (n) {
581                     mant = get_bits(&s->gb, n + 1);
582                     v = l1_unscale(n, mant, scale_factors[ch][i]);
583                 } else {
584                     v = 0;
585                 }
586                 s->sb_samples[ch][j][i] = v;
587             }
588         }
589         for (i = bound; i < SBLIMIT; i++) {
590             n = allocation[0][i];
591             if (n) {
592                 mant = get_bits(&s->gb, n + 1);
593                 v = l1_unscale(n, mant, scale_factors[0][i]);
594                 s->sb_samples[0][j][i] = v;
595                 v = l1_unscale(n, mant, scale_factors[1][i]);
596                 s->sb_samples[1][j][i] = v;
597             } else {
598                 s->sb_samples[0][j][i] = 0;
599                 s->sb_samples[1][j][i] = 0;
600             }
601         }
602     }
603     return 12;
604 }
605
606 static int mp_decode_layer2(MPADecodeContext *s)
607 {
608     int sblimit; /* number of used subbands */
609     const unsigned char *alloc_table;
610     int table, bit_alloc_bits, i, j, ch, bound, v;
611     unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
612     unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
613     unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
614     int scale, qindex, bits, steps, k, l, m, b;
615
616     /* select decoding table */
617     table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
618                                    s->sample_rate, s->lsf);
619     sblimit     = ff_mpa_sblimit_table[table];
620     alloc_table = ff_mpa_alloc_tables[table];
621
622     if (s->mode == MPA_JSTEREO)
623         bound = (s->mode_ext + 1) * 4;
624     else
625         bound = sblimit;
626
627     av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
628
629     /* sanity check */
630     if (bound > sblimit)
631         bound = sblimit;
632
633     /* parse bit allocation */
634     j = 0;
635     for (i = 0; i < bound; i++) {
636         bit_alloc_bits = alloc_table[j];
637         for (ch = 0; ch < s->nb_channels; ch++)
638             bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
639         j += 1 << bit_alloc_bits;
640     }
641     for (i = bound; i < sblimit; i++) {
642         bit_alloc_bits = alloc_table[j];
643         v = get_bits(&s->gb, bit_alloc_bits);
644         bit_alloc[0][i] = v;
645         bit_alloc[1][i] = v;
646         j += 1 << bit_alloc_bits;
647     }
648
649     /* scale codes */
650     for (i = 0; i < sblimit; i++) {
651         for (ch = 0; ch < s->nb_channels; ch++) {
652             if (bit_alloc[ch][i])
653                 scale_code[ch][i] = get_bits(&s->gb, 2);
654         }
655     }
656
657     /* scale factors */
658     for (i = 0; i < sblimit; i++) {
659         for (ch = 0; ch < s->nb_channels; ch++) {
660             if (bit_alloc[ch][i]) {
661                 sf = scale_factors[ch][i];
662                 switch (scale_code[ch][i]) {
663                 default:
664                 case 0:
665                     sf[0] = get_bits(&s->gb, 6);
666                     sf[1] = get_bits(&s->gb, 6);
667                     sf[2] = get_bits(&s->gb, 6);
668                     break;
669                 case 2:
670                     sf[0] = get_bits(&s->gb, 6);
671                     sf[1] = sf[0];
672                     sf[2] = sf[0];
673                     break;
674                 case 1:
675                     sf[0] = get_bits(&s->gb, 6);
676                     sf[2] = get_bits(&s->gb, 6);
677                     sf[1] = sf[0];
678                     break;
679                 case 3:
680                     sf[0] = get_bits(&s->gb, 6);
681                     sf[2] = get_bits(&s->gb, 6);
682                     sf[1] = sf[2];
683                     break;
684                 }
685             }
686         }
687     }
688
689     /* samples */
690     for (k = 0; k < 3; k++) {
691         for (l = 0; l < 12; l += 3) {
692             j = 0;
693             for (i = 0; i < bound; i++) {
694                 bit_alloc_bits = alloc_table[j];
695                 for (ch = 0; ch < s->nb_channels; ch++) {
696                     b = bit_alloc[ch][i];
697                     if (b) {
698                         scale = scale_factors[ch][i][k];
699                         qindex = alloc_table[j+b];
700                         bits = ff_mpa_quant_bits[qindex];
701                         if (bits < 0) {
702                             int v2;
703                             /* 3 values at the same time */
704                             v = get_bits(&s->gb, -bits);
705                             v2 = division_tabs[qindex][v];
706                             steps  = ff_mpa_quant_steps[qindex];
707
708                             s->sb_samples[ch][k * 12 + l + 0][i] =
709                                 l2_unscale_group(steps,  v2       & 15, scale);
710                             s->sb_samples[ch][k * 12 + l + 1][i] =
711                                 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
712                             s->sb_samples[ch][k * 12 + l + 2][i] =
713                                 l2_unscale_group(steps,  v2 >> 8      , scale);
714                         } else {
715                             for (m = 0; m < 3; m++) {
716                                 v = get_bits(&s->gb, bits);
717                                 v = l1_unscale(bits - 1, v, scale);
718                                 s->sb_samples[ch][k * 12 + l + m][i] = v;
719                             }
720                         }
721                     } else {
722                         s->sb_samples[ch][k * 12 + l + 0][i] = 0;
723                         s->sb_samples[ch][k * 12 + l + 1][i] = 0;
724                         s->sb_samples[ch][k * 12 + l + 2][i] = 0;
725                     }
726                 }
727                 /* next subband in alloc table */
728                 j += 1 << bit_alloc_bits;
729             }
730             /* XXX: find a way to avoid this duplication of code */
731             for (i = bound; i < sblimit; i++) {
732                 bit_alloc_bits = alloc_table[j];
733                 b = bit_alloc[0][i];
734                 if (b) {
735                     int mant, scale0, scale1;
736                     scale0 = scale_factors[0][i][k];
737                     scale1 = scale_factors[1][i][k];
738                     qindex = alloc_table[j+b];
739                     bits = ff_mpa_quant_bits[qindex];
740                     if (bits < 0) {
741                         /* 3 values at the same time */
742                         v = get_bits(&s->gb, -bits);
743                         steps = ff_mpa_quant_steps[qindex];
744                         mant = v % steps;
745                         v = v / steps;
746                         s->sb_samples[0][k * 12 + l + 0][i] =
747                             l2_unscale_group(steps, mant, scale0);
748                         s->sb_samples[1][k * 12 + l + 0][i] =
749                             l2_unscale_group(steps, mant, scale1);
750                         mant = v % steps;
751                         v = v / steps;
752                         s->sb_samples[0][k * 12 + l + 1][i] =
753                             l2_unscale_group(steps, mant, scale0);
754                         s->sb_samples[1][k * 12 + l + 1][i] =
755                             l2_unscale_group(steps, mant, scale1);
756                         s->sb_samples[0][k * 12 + l + 2][i] =
757                             l2_unscale_group(steps, v, scale0);
758                         s->sb_samples[1][k * 12 + l + 2][i] =
759                             l2_unscale_group(steps, v, scale1);
760                     } else {
761                         for (m = 0; m < 3; m++) {
762                             mant = get_bits(&s->gb, bits);
763                             s->sb_samples[0][k * 12 + l + m][i] =
764                                 l1_unscale(bits - 1, mant, scale0);
765                             s->sb_samples[1][k * 12 + l + m][i] =
766                                 l1_unscale(bits - 1, mant, scale1);
767                         }
768                     }
769                 } else {
770                     s->sb_samples[0][k * 12 + l + 0][i] = 0;
771                     s->sb_samples[0][k * 12 + l + 1][i] = 0;
772                     s->sb_samples[0][k * 12 + l + 2][i] = 0;
773                     s->sb_samples[1][k * 12 + l + 0][i] = 0;
774                     s->sb_samples[1][k * 12 + l + 1][i] = 0;
775                     s->sb_samples[1][k * 12 + l + 2][i] = 0;
776                 }
777                 /* next subband in alloc table */
778                 j += 1 << bit_alloc_bits;
779             }
780             /* fill remaining samples to zero */
781             for (i = sblimit; i < SBLIMIT; i++) {
782                 for (ch = 0; ch < s->nb_channels; ch++) {
783                     s->sb_samples[ch][k * 12 + l + 0][i] = 0;
784                     s->sb_samples[ch][k * 12 + l + 1][i] = 0;
785                     s->sb_samples[ch][k * 12 + l + 2][i] = 0;
786                 }
787             }
788         }
789     }
790     return 3 * 12;
791 }
792
793 #define SPLIT(dst,sf,n)             \
794     if (n == 3) {                   \
795         int m = (sf * 171) >> 9;    \
796         dst   = sf - 3 * m;         \
797         sf    = m;                  \
798     } else if (n == 4) {            \
799         dst  = sf & 3;              \
800         sf >>= 2;                   \
801     } else if (n == 5) {            \
802         int m = (sf * 205) >> 10;   \
803         dst   = sf - 5 * m;         \
804         sf    = m;                  \
805     } else if (n == 6) {            \
806         int m = (sf * 171) >> 10;   \
807         dst   = sf - 6 * m;         \
808         sf    = m;                  \
809     } else {                        \
810         dst = 0;                    \
811     }
812
813 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
814                                            int n3)
815 {
816     SPLIT(slen[3], sf, n3)
817     SPLIT(slen[2], sf, n2)
818     SPLIT(slen[1], sf, n1)
819     slen[0] = sf;
820 }
821
822 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
823                                          int16_t *exponents)
824 {
825     const uint8_t *bstab, *pretab;
826     int len, i, j, k, l, v0, shift, gain, gains[3];
827     int16_t *exp_ptr;
828
829     exp_ptr = exponents;
830     gain    = g->global_gain - 210;
831     shift   = g->scalefac_scale + 1;
832
833     bstab  = band_size_long[s->sample_rate_index];
834     pretab = mpa_pretab[g->preflag];
835     for (i = 0; i < g->long_end; i++) {
836         v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
837         len = bstab[i];
838         for (j = len; j > 0; j--)
839             *exp_ptr++ = v0;
840     }
841
842     if (g->short_start < 13) {
843         bstab    = band_size_short[s->sample_rate_index];
844         gains[0] = gain - (g->subblock_gain[0] << 3);
845         gains[1] = gain - (g->subblock_gain[1] << 3);
846         gains[2] = gain - (g->subblock_gain[2] << 3);
847         k        = g->long_end;
848         for (i = g->short_start; i < 13; i++) {
849             len = bstab[i];
850             for (l = 0; l < 3; l++) {
851                 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
852                 for (j = len; j > 0; j--)
853                     *exp_ptr++ = v0;
854             }
855         }
856     }
857 }
858
859 /* handle n = 0 too */
860 static inline int get_bitsz(GetBitContext *s, int n)
861 {
862     return n ? get_bits(s, n) : 0;
863 }
864
865
866 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
867                           int *end_pos2)
868 {
869     if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
870         s->gb           = s->in_gb;
871         s->in_gb.buffer = NULL;
872         assert((get_bits_count(&s->gb) & 7) == 0);
873         skip_bits_long(&s->gb, *pos - *end_pos);
874         *end_pos2 =
875         *end_pos  = *end_pos2 + get_bits_count(&s->gb) - *pos;
876         *pos      = get_bits_count(&s->gb);
877     }
878 }
879
880 /* Following is a optimized code for
881             INTFLOAT v = *src
882             if(get_bits1(&s->gb))
883                 v = -v;
884             *dst = v;
885 */
886 #if CONFIG_FLOAT
887 #define READ_FLIP_SIGN(dst,src)                     \
888     v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31);  \
889     AV_WN32A(dst, v);
890 #else
891 #define READ_FLIP_SIGN(dst,src)     \
892     v      = -get_bits1(&s->gb);    \
893     *(dst) = (*(src) ^ v) - v;
894 #endif
895
896 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
897                           int16_t *exponents, int end_pos2)
898 {
899     int s_index;
900     int i;
901     int last_pos, bits_left;
902     VLC *vlc;
903     int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
904
905     /* low frequencies (called big values) */
906     s_index = 0;
907     for (i = 0; i < 3; i++) {
908         int j, k, l, linbits;
909         j = g->region_size[i];
910         if (j == 0)
911             continue;
912         /* select vlc table */
913         k       = g->table_select[i];
914         l       = mpa_huff_data[k][0];
915         linbits = mpa_huff_data[k][1];
916         vlc     = &huff_vlc[l];
917
918         if (!l) {
919             memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
920             s_index += 2 * j;
921             continue;
922         }
923
924         /* read huffcode and compute each couple */
925         for (; j > 0; j--) {
926             int exponent, x, y;
927             int v;
928             int pos = get_bits_count(&s->gb);
929
930             if (pos >= end_pos){
931 //                av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
932                 switch_buffer(s, &pos, &end_pos, &end_pos2);
933 //                av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
934                 if (pos >= end_pos)
935                     break;
936             }
937             y = get_vlc2(&s->gb, vlc->table, 7, 3);
938
939             if (!y) {
940                 g->sb_hybrid[s_index  ] =
941                 g->sb_hybrid[s_index+1] = 0;
942                 s_index += 2;
943                 continue;
944             }
945
946             exponent= exponents[s_index];
947
948             av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
949                     i, g->region_size[i] - j, x, y, exponent);
950             if (y & 16) {
951                 x = y >> 5;
952                 y = y & 0x0f;
953                 if (x < 15) {
954                     READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
955                 } else {
956                     x += get_bitsz(&s->gb, linbits);
957                     v  = l3_unscale(x, exponent);
958                     if (get_bits1(&s->gb))
959                         v = -v;
960                     g->sb_hybrid[s_index] = v;
961                 }
962                 if (y < 15) {
963                     READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
964                 } else {
965                     y += get_bitsz(&s->gb, linbits);
966                     v  = l3_unscale(y, exponent);
967                     if (get_bits1(&s->gb))
968                         v = -v;
969                     g->sb_hybrid[s_index+1] = v;
970                 }
971             } else {
972                 x = y >> 5;
973                 y = y & 0x0f;
974                 x += y;
975                 if (x < 15) {
976                     READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
977                 } else {
978                     x += get_bitsz(&s->gb, linbits);
979                     v  = l3_unscale(x, exponent);
980                     if (get_bits1(&s->gb))
981                         v = -v;
982                     g->sb_hybrid[s_index+!!y] = v;
983                 }
984                 g->sb_hybrid[s_index + !y] = 0;
985             }
986             s_index += 2;
987         }
988     }
989
990     /* high frequencies */
991     vlc = &huff_quad_vlc[g->count1table_select];
992     last_pos = 0;
993     while (s_index <= 572) {
994         int pos, code;
995         pos = get_bits_count(&s->gb);
996         if (pos >= end_pos) {
997             if (pos > end_pos2 && last_pos) {
998                 /* some encoders generate an incorrect size for this
999                    part. We must go back into the data */
1000                 s_index -= 4;
1001                 skip_bits_long(&s->gb, last_pos - pos);
1002                 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1003                 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
1004                     s_index=0;
1005                 break;
1006             }
1007 //                av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1008             switch_buffer(s, &pos, &end_pos, &end_pos2);
1009 //                av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1010             if (pos >= end_pos)
1011                 break;
1012         }
1013         last_pos = pos;
1014
1015         code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1016         av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1017         g->sb_hybrid[s_index+0] =
1018         g->sb_hybrid[s_index+1] =
1019         g->sb_hybrid[s_index+2] =
1020         g->sb_hybrid[s_index+3] = 0;
1021         while (code) {
1022             static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
1023             int v;
1024             int pos = s_index + idxtab[code];
1025             code   ^= 8 >> idxtab[code];
1026             READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
1027         }
1028         s_index += 4;
1029     }
1030     /* skip extension bits */
1031     bits_left = end_pos2 - get_bits_count(&s->gb);
1032 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1033     if (bits_left < 0 && (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) {
1034         av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1035         s_index=0;
1036     } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE))) {
1037         av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1038         s_index = 0;
1039     }
1040     memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
1041     skip_bits_long(&s->gb, bits_left);
1042
1043     i = get_bits_count(&s->gb);
1044     switch_buffer(s, &i, &end_pos, &end_pos2);
1045
1046     return 0;
1047 }
1048
1049 /* Reorder short blocks from bitstream order to interleaved order. It
1050    would be faster to do it in parsing, but the code would be far more
1051    complicated */
1052 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1053 {
1054     int i, j, len;
1055     INTFLOAT *ptr, *dst, *ptr1;
1056     INTFLOAT tmp[576];
1057
1058     if (g->block_type != 2)
1059         return;
1060
1061     if (g->switch_point) {
1062         if (s->sample_rate_index != 8)
1063             ptr = g->sb_hybrid + 36;
1064         else
1065             ptr = g->sb_hybrid + 48;
1066     } else {
1067         ptr = g->sb_hybrid;
1068     }
1069
1070     for (i = g->short_start; i < 13; i++) {
1071         len  = band_size_short[s->sample_rate_index][i];
1072         ptr1 = ptr;
1073         dst  = tmp;
1074         for (j = len; j > 0; j--) {
1075             *dst++ = ptr[0*len];
1076             *dst++ = ptr[1*len];
1077             *dst++ = ptr[2*len];
1078             ptr++;
1079         }
1080         ptr += 2 * len;
1081         memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1082     }
1083 }
1084
1085 #define ISQRT2 FIXR(0.70710678118654752440)
1086
1087 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1088 {
1089     int i, j, k, l;
1090     int sf_max, sf, len, non_zero_found;
1091     INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1092     int non_zero_found_short[3];
1093
1094     /* intensity stereo */
1095     if (s->mode_ext & MODE_EXT_I_STEREO) {
1096         if (!s->lsf) {
1097             is_tab = is_table;
1098             sf_max = 7;
1099         } else {
1100             is_tab = is_table_lsf[g1->scalefac_compress & 1];
1101             sf_max = 16;
1102         }
1103
1104         tab0 = g0->sb_hybrid + 576;
1105         tab1 = g1->sb_hybrid + 576;
1106
1107         non_zero_found_short[0] = 0;
1108         non_zero_found_short[1] = 0;
1109         non_zero_found_short[2] = 0;
1110         k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1111         for (i = 12; i >= g1->short_start; i--) {
1112             /* for last band, use previous scale factor */
1113             if (i != 11)
1114                 k -= 3;
1115             len = band_size_short[s->sample_rate_index][i];
1116             for (l = 2; l >= 0; l--) {
1117                 tab0 -= len;
1118                 tab1 -= len;
1119                 if (!non_zero_found_short[l]) {
1120                     /* test if non zero band. if so, stop doing i-stereo */
1121                     for (j = 0; j < len; j++) {
1122                         if (tab1[j] != 0) {
1123                             non_zero_found_short[l] = 1;
1124                             goto found1;
1125                         }
1126                     }
1127                     sf = g1->scale_factors[k + l];
1128                     if (sf >= sf_max)
1129                         goto found1;
1130
1131                     v1 = is_tab[0][sf];
1132                     v2 = is_tab[1][sf];
1133                     for (j = 0; j < len; j++) {
1134                         tmp0    = tab0[j];
1135                         tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1136                         tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1137                     }
1138                 } else {
1139 found1:
1140                     if (s->mode_ext & MODE_EXT_MS_STEREO) {
1141                         /* lower part of the spectrum : do ms stereo
1142                            if enabled */
1143                         for (j = 0; j < len; j++) {
1144                             tmp0    = tab0[j];
1145                             tmp1    = tab1[j];
1146                             tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1147                             tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1148                         }
1149                     }
1150                 }
1151             }
1152         }
1153
1154         non_zero_found = non_zero_found_short[0] |
1155                          non_zero_found_short[1] |
1156                          non_zero_found_short[2];
1157
1158         for (i = g1->long_end - 1;i >= 0;i--) {
1159             len   = band_size_long[s->sample_rate_index][i];
1160             tab0 -= len;
1161             tab1 -= len;
1162             /* test if non zero band. if so, stop doing i-stereo */
1163             if (!non_zero_found) {
1164                 for (j = 0; j < len; j++) {
1165                     if (tab1[j] != 0) {
1166                         non_zero_found = 1;
1167                         goto found2;
1168                     }
1169                 }
1170                 /* for last band, use previous scale factor */
1171                 k  = (i == 21) ? 20 : i;
1172                 sf = g1->scale_factors[k];
1173                 if (sf >= sf_max)
1174                     goto found2;
1175                 v1 = is_tab[0][sf];
1176                 v2 = is_tab[1][sf];
1177                 for (j = 0; j < len; j++) {
1178                     tmp0    = tab0[j];
1179                     tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1180                     tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1181                 }
1182             } else {
1183 found2:
1184                 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1185                     /* lower part of the spectrum : do ms stereo
1186                        if enabled */
1187                     for (j = 0; j < len; j++) {
1188                         tmp0    = tab0[j];
1189                         tmp1    = tab1[j];
1190                         tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1191                         tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1192                     }
1193                 }
1194             }
1195         }
1196     } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1197         /* ms stereo ONLY */
1198         /* NOTE: the 1/sqrt(2) normalization factor is included in the
1199            global gain */
1200         tab0 = g0->sb_hybrid;
1201         tab1 = g1->sb_hybrid;
1202         for (i = 0; i < 576; i++) {
1203             tmp0    = tab0[i];
1204             tmp1    = tab1[i];
1205             tab0[i] = tmp0 + tmp1;
1206             tab1[i] = tmp0 - tmp1;
1207         }
1208     }
1209 }
1210
1211 #if CONFIG_FLOAT
1212 #define AA(j) do {                                                      \
1213         float tmp0 = ptr[-1-j];                                         \
1214         float tmp1 = ptr[   j];                                         \
1215         ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1];    \
1216         ptr[   j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0];    \
1217     } while (0)
1218 #else
1219 #define AA(j) do {                                              \
1220         int tmp0 = ptr[-1-j];                                   \
1221         int tmp1 = ptr[   j];                                   \
1222         int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]);          \
1223         ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2]));   \
1224         ptr[   j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3]));   \
1225     } while (0)
1226 #endif
1227
1228 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1229 {
1230     INTFLOAT *ptr;
1231     int n, i;
1232
1233     /* we antialias only "long" bands */
1234     if (g->block_type == 2) {
1235         if (!g->switch_point)
1236             return;
1237         /* XXX: check this for 8000Hz case */
1238         n = 1;
1239     } else {
1240         n = SBLIMIT - 1;
1241     }
1242
1243     ptr = g->sb_hybrid + 18;
1244     for (i = n; i > 0; i--) {
1245         AA(0);
1246         AA(1);
1247         AA(2);
1248         AA(3);
1249         AA(4);
1250         AA(5);
1251         AA(6);
1252         AA(7);
1253
1254         ptr += 18;
1255     }
1256 }
1257
1258 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1259                           INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1260 {
1261     INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1262     INTFLOAT out2[12];
1263     int i, j, mdct_long_end, sblimit;
1264
1265     /* find last non zero block */
1266     ptr  = g->sb_hybrid + 576;
1267     ptr1 = g->sb_hybrid + 2 * 18;
1268     while (ptr >= ptr1) {
1269         int32_t *p;
1270         ptr -= 6;
1271         p    = (int32_t*)ptr;
1272         if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1273             break;
1274     }
1275     sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1276
1277     if (g->block_type == 2) {
1278         /* XXX: check for 8000 Hz */
1279         if (g->switch_point)
1280             mdct_long_end = 2;
1281         else
1282             mdct_long_end = 0;
1283     } else {
1284         mdct_long_end = sblimit;
1285     }
1286
1287     buf = mdct_buf;
1288     ptr = g->sb_hybrid;
1289     for (j = 0; j < mdct_long_end; j++) {
1290         int win_idx = (g->switch_point && j < 2) ? 0 : g->block_type;
1291         /* apply window & overlap with previous buffer */
1292         out_ptr = sb_samples + j;
1293         /* select window */
1294         win = mdct_win[win_idx + (4 & -(j & 1))];
1295         s->mpadsp.RENAME(imdct36)(out_ptr, buf, ptr, win);
1296         out_ptr += 18 * SBLIMIT;
1297         ptr     += 18;
1298         buf     += 18;
1299     }
1300     for (j = mdct_long_end; j < sblimit; j++) {
1301         /* select frequency inversion */
1302         win     = mdct_win[2 + (4  & -(j & 1))];
1303         out_ptr = sb_samples + j;
1304
1305         for (i = 0; i < 6; i++) {
1306             *out_ptr = buf[i];
1307             out_ptr += SBLIMIT;
1308         }
1309         imdct12(out2, ptr + 0);
1310         for (i = 0; i < 6; i++) {
1311             *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*1];
1312             buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1313             out_ptr += SBLIMIT;
1314         }
1315         imdct12(out2, ptr + 1);
1316         for (i = 0; i < 6; i++) {
1317             *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*2];
1318             buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1319             out_ptr += SBLIMIT;
1320         }
1321         imdct12(out2, ptr + 2);
1322         for (i = 0; i < 6; i++) {
1323             buf[i + 6*0] = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*0];
1324             buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1325             buf[i + 6*2] = 0;
1326         }
1327         ptr += 18;
1328         buf += 18;
1329     }
1330     /* zero bands */
1331     for (j = sblimit; j < SBLIMIT; j++) {
1332         /* overlap */
1333         out_ptr = sb_samples + j;
1334         for (i = 0; i < 18; i++) {
1335             *out_ptr = buf[i];
1336             buf[i]   = 0;
1337             out_ptr += SBLIMIT;
1338         }
1339         buf += 18;
1340     }
1341 }
1342
1343 /* main layer3 decoding function */
1344 static int mp_decode_layer3(MPADecodeContext *s)
1345 {
1346     int nb_granules, main_data_begin;
1347     int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1348     GranuleDef *g;
1349     int16_t exponents[576]; //FIXME try INTFLOAT
1350
1351     /* read side info */
1352     if (s->lsf) {
1353         main_data_begin = get_bits(&s->gb, 8);
1354         skip_bits(&s->gb, s->nb_channels);
1355         nb_granules = 1;
1356     } else {
1357         main_data_begin = get_bits(&s->gb, 9);
1358         if (s->nb_channels == 2)
1359             skip_bits(&s->gb, 3);
1360         else
1361             skip_bits(&s->gb, 5);
1362         nb_granules = 2;
1363         for (ch = 0; ch < s->nb_channels; ch++) {
1364             s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1365             s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1366         }
1367     }
1368
1369     for (gr = 0; gr < nb_granules; gr++) {
1370         for (ch = 0; ch < s->nb_channels; ch++) {
1371             av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1372             g = &s->granules[ch][gr];
1373             g->part2_3_length = get_bits(&s->gb, 12);
1374             g->big_values     = get_bits(&s->gb,  9);
1375             if (g->big_values > 288) {
1376                 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1377                 return AVERROR_INVALIDDATA;
1378             }
1379
1380             g->global_gain = get_bits(&s->gb, 8);
1381             /* if MS stereo only is selected, we precompute the
1382                1/sqrt(2) renormalization factor */
1383             if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1384                 MODE_EXT_MS_STEREO)
1385                 g->global_gain -= 2;
1386             if (s->lsf)
1387                 g->scalefac_compress = get_bits(&s->gb, 9);
1388             else
1389                 g->scalefac_compress = get_bits(&s->gb, 4);
1390             blocksplit_flag = get_bits1(&s->gb);
1391             if (blocksplit_flag) {
1392                 g->block_type = get_bits(&s->gb, 2);
1393                 if (g->block_type == 0) {
1394                     av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1395                     return AVERROR_INVALIDDATA;
1396                 }
1397                 g->switch_point = get_bits1(&s->gb);
1398                 for (i = 0; i < 2; i++)
1399                     g->table_select[i] = get_bits(&s->gb, 5);
1400                 for (i = 0; i < 3; i++)
1401                     g->subblock_gain[i] = get_bits(&s->gb, 3);
1402                 ff_init_short_region(s, g);
1403             } else {
1404                 int region_address1, region_address2;
1405                 g->block_type = 0;
1406                 g->switch_point = 0;
1407                 for (i = 0; i < 3; i++)
1408                     g->table_select[i] = get_bits(&s->gb, 5);
1409                 /* compute huffman coded region sizes */
1410                 region_address1 = get_bits(&s->gb, 4);
1411                 region_address2 = get_bits(&s->gb, 3);
1412                 av_dlog(s->avctx, "region1=%d region2=%d\n",
1413                         region_address1, region_address2);
1414                 ff_init_long_region(s, g, region_address1, region_address2);
1415             }
1416             ff_region_offset2size(g);
1417             ff_compute_band_indexes(s, g);
1418
1419             g->preflag = 0;
1420             if (!s->lsf)
1421                 g->preflag = get_bits1(&s->gb);
1422             g->scalefac_scale     = get_bits1(&s->gb);
1423             g->count1table_select = get_bits1(&s->gb);
1424             av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1425                     g->block_type, g->switch_point);
1426         }
1427     }
1428
1429     if (!s->adu_mode) {
1430         const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1431         assert((get_bits_count(&s->gb) & 7) == 0);
1432         /* now we get bits from the main_data_begin offset */
1433         av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1434     //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1435
1436         memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1437         s->in_gb = s->gb;
1438         init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1439 #if !UNCHECKED_BITSTREAM_READER
1440         s->gb.size_in_bits_plus8 += EXTRABYTES * 8;
1441 #endif
1442         s->last_buf_size <<= 3;
1443         for (gr = 0, ch = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++, ch = 0) {
1444             for (; ch < s->nb_channels && (s->last_buf_size >> 3) < main_data_begin; ch++) {
1445                 g = &s->granules[ch][gr];
1446                 s->last_buf_size += g->part2_3_length;
1447                 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1448             }
1449         }
1450         skip_bits_long(&s->gb, s->last_buf_size - 8 * main_data_begin);
1451         if (get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer) {
1452             skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1453             s->gb           = s->in_gb;
1454             s->in_gb.buffer = NULL;
1455         }
1456     } else {
1457         gr = ch = 0;
1458     }
1459
1460     for (; gr < nb_granules; gr++, ch = 0) {
1461         for (; ch < s->nb_channels; ch++) {
1462             g = &s->granules[ch][gr];
1463             bits_pos = get_bits_count(&s->gb);
1464
1465             if (!s->lsf) {
1466                 uint8_t *sc;
1467                 int slen, slen1, slen2;
1468
1469                 /* MPEG1 scale factors */
1470                 slen1 = slen_table[0][g->scalefac_compress];
1471                 slen2 = slen_table[1][g->scalefac_compress];
1472                 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1473                 if (g->block_type == 2) {
1474                     n = g->switch_point ? 17 : 18;
1475                     j = 0;
1476                     if (slen1) {
1477                         for (i = 0; i < n; i++)
1478                             g->scale_factors[j++] = get_bits(&s->gb, slen1);
1479                     } else {
1480                         for (i = 0; i < n; i++)
1481                             g->scale_factors[j++] = 0;
1482                     }
1483                     if (slen2) {
1484                         for (i = 0; i < 18; i++)
1485                             g->scale_factors[j++] = get_bits(&s->gb, slen2);
1486                         for (i = 0; i < 3; i++)
1487                             g->scale_factors[j++] = 0;
1488                     } else {
1489                         for (i = 0; i < 21; i++)
1490                             g->scale_factors[j++] = 0;
1491                     }
1492                 } else {
1493                     sc = s->granules[ch][0].scale_factors;
1494                     j = 0;
1495                     for (k = 0; k < 4; k++) {
1496                         n = k == 0 ? 6 : 5;
1497                         if ((g->scfsi & (0x8 >> k)) == 0) {
1498                             slen = (k < 2) ? slen1 : slen2;
1499                             if (slen) {
1500                                 for (i = 0; i < n; i++)
1501                                     g->scale_factors[j++] = get_bits(&s->gb, slen);
1502                             } else {
1503                                 for (i = 0; i < n; i++)
1504                                     g->scale_factors[j++] = 0;
1505                             }
1506                         } else {
1507                             /* simply copy from last granule */
1508                             for (i = 0; i < n; i++) {
1509                                 g->scale_factors[j] = sc[j];
1510                                 j++;
1511                             }
1512                         }
1513                     }
1514                     g->scale_factors[j++] = 0;
1515                 }
1516             } else {
1517                 int tindex, tindex2, slen[4], sl, sf;
1518
1519                 /* LSF scale factors */
1520                 if (g->block_type == 2)
1521                     tindex = g->switch_point ? 2 : 1;
1522                 else
1523                     tindex = 0;
1524
1525                 sf = g->scalefac_compress;
1526                 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1527                     /* intensity stereo case */
1528                     sf >>= 1;
1529                     if (sf < 180) {
1530                         lsf_sf_expand(slen, sf, 6, 6, 0);
1531                         tindex2 = 3;
1532                     } else if (sf < 244) {
1533                         lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1534                         tindex2 = 4;
1535                     } else {
1536                         lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1537                         tindex2 = 5;
1538                     }
1539                 } else {
1540                     /* normal case */
1541                     if (sf < 400) {
1542                         lsf_sf_expand(slen, sf, 5, 4, 4);
1543                         tindex2 = 0;
1544                     } else if (sf < 500) {
1545                         lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1546                         tindex2 = 1;
1547                     } else {
1548                         lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1549                         tindex2 = 2;
1550                         g->preflag = 1;
1551                     }
1552                 }
1553
1554                 j = 0;
1555                 for (k = 0; k < 4; k++) {
1556                     n  = lsf_nsf_table[tindex2][tindex][k];
1557                     sl = slen[k];
1558                     if (sl) {
1559                         for (i = 0; i < n; i++)
1560                             g->scale_factors[j++] = get_bits(&s->gb, sl);
1561                     } else {
1562                         for (i = 0; i < n; i++)
1563                             g->scale_factors[j++] = 0;
1564                     }
1565                 }
1566                 /* XXX: should compute exact size */
1567                 for (; j < 40; j++)
1568                     g->scale_factors[j] = 0;
1569             }
1570
1571             exponents_from_scale_factors(s, g, exponents);
1572
1573             /* read Huffman coded residue */
1574             huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1575         } /* ch */
1576
1577         if (s->nb_channels == 2)
1578             compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1579
1580         for (ch = 0; ch < s->nb_channels; ch++) {
1581             g = &s->granules[ch][gr];
1582
1583             reorder_block(s, g);
1584             compute_antialias(s, g);
1585             compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1586         }
1587     } /* gr */
1588     if (get_bits_count(&s->gb) < 0)
1589         skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1590     return nb_granules * 18;
1591 }
1592
1593 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1594                            const uint8_t *buf, int buf_size)
1595 {
1596     int i, nb_frames, ch, ret;
1597     OUT_INT *samples_ptr;
1598
1599     init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1600
1601     /* skip error protection field */
1602     if (s->error_protection)
1603         skip_bits(&s->gb, 16);
1604
1605     switch(s->layer) {
1606     case 1:
1607         s->avctx->frame_size = 384;
1608         nb_frames = mp_decode_layer1(s);
1609         break;
1610     case 2:
1611         s->avctx->frame_size = 1152;
1612         nb_frames = mp_decode_layer2(s);
1613         break;
1614     case 3:
1615         s->avctx->frame_size = s->lsf ? 576 : 1152;
1616     default:
1617         nb_frames = mp_decode_layer3(s);
1618
1619         s->last_buf_size=0;
1620         if (s->in_gb.buffer) {
1621             align_get_bits(&s->gb);
1622             i = get_bits_left(&s->gb)>>3;
1623             if (i >= 0 && i <= BACKSTEP_SIZE) {
1624                 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1625                 s->last_buf_size=i;
1626             } else
1627                 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1628             s->gb           = s->in_gb;
1629             s->in_gb.buffer = NULL;
1630         }
1631
1632         align_get_bits(&s->gb);
1633         assert((get_bits_count(&s->gb) & 7) == 0);
1634         i = get_bits_left(&s->gb) >> 3;
1635
1636         if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1637             if (i < 0)
1638                 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1639             i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1640         }
1641         assert(i <= buf_size - HEADER_SIZE && i >= 0);
1642         memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1643         s->last_buf_size += i;
1644     }
1645
1646     /* get output buffer */
1647     if (!samples) {
1648         s->frame.nb_samples = s->avctx->frame_size;
1649         if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1650             av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1651             return ret;
1652         }
1653         samples = (OUT_INT *)s->frame.data[0];
1654     }
1655
1656     /* apply the synthesis filter */
1657     for (ch = 0; ch < s->nb_channels; ch++) {
1658         samples_ptr = samples + ch;
1659         for (i = 0; i < nb_frames; i++) {
1660             RENAME(ff_mpa_synth_filter)(
1661                          &s->mpadsp,
1662                          s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1663                          RENAME(ff_mpa_synth_window), &s->dither_state,
1664                          samples_ptr, s->nb_channels,
1665                          s->sb_samples[ch][i]);
1666             samples_ptr += 32 * s->nb_channels;
1667         }
1668     }
1669
1670     return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1671 }
1672
1673 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1674                         AVPacket *avpkt)
1675 {
1676     const uint8_t *buf  = avpkt->data;
1677     int buf_size        = avpkt->size;
1678     MPADecodeContext *s = avctx->priv_data;
1679     uint32_t header;
1680     int out_size;
1681
1682     if (buf_size < HEADER_SIZE)
1683         return AVERROR_INVALIDDATA;
1684
1685     header = AV_RB32(buf);
1686     if (ff_mpa_check_header(header) < 0) {
1687         av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1688         return AVERROR_INVALIDDATA;
1689     }
1690
1691     if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1692         /* free format: prepare to compute frame size */
1693         s->frame_size = -1;
1694         return AVERROR_INVALIDDATA;
1695     }
1696     /* update codec info */
1697     avctx->channels       = s->nb_channels;
1698     avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1699     if (!avctx->bit_rate)
1700         avctx->bit_rate = s->bit_rate;
1701     avctx->sub_id = s->layer;
1702
1703     if (s->frame_size <= 0 || s->frame_size > buf_size) {
1704         av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1705         return AVERROR_INVALIDDATA;
1706     }else if(s->frame_size < buf_size){
1707         av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1708         buf_size= s->frame_size;
1709     }
1710
1711     out_size = mp_decode_frame(s, NULL, buf, buf_size);
1712     if (out_size >= 0) {
1713         *got_frame_ptr   = 1;
1714         *(AVFrame *)data = s->frame;
1715         avctx->sample_rate = s->sample_rate;
1716         //FIXME maybe move the other codec info stuff from above here too
1717     } else {
1718         av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1719         /* Only return an error if the bad frame makes up the whole packet.
1720            If there is more data in the packet, just consume the bad frame
1721            instead of returning an error, which would discard the whole
1722            packet. */
1723         *got_frame_ptr = 0;
1724         if (buf_size == avpkt->size)
1725             return out_size;
1726     }
1727     s->frame_size = 0;
1728     return buf_size;
1729 }
1730
1731 static void flush(AVCodecContext *avctx)
1732 {
1733     MPADecodeContext *s = avctx->priv_data;
1734     memset(s->synth_buf, 0, sizeof(s->synth_buf));
1735     s->last_buf_size = 0;
1736 }
1737
1738 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1739 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1740                             int *got_frame_ptr, AVPacket *avpkt)
1741 {
1742     const uint8_t *buf  = avpkt->data;
1743     int buf_size        = avpkt->size;
1744     MPADecodeContext *s = avctx->priv_data;
1745     uint32_t header;
1746     int len, out_size;
1747
1748     len = buf_size;
1749
1750     // Discard too short frames
1751     if (buf_size < HEADER_SIZE) {
1752         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1753         return AVERROR_INVALIDDATA;
1754     }
1755
1756
1757     if (len > MPA_MAX_CODED_FRAME_SIZE)
1758         len = MPA_MAX_CODED_FRAME_SIZE;
1759
1760     // Get header and restore sync word
1761     header = AV_RB32(buf) | 0xffe00000;
1762
1763     if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1764         av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1765         return AVERROR_INVALIDDATA;
1766     }
1767
1768     avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1769     /* update codec info */
1770     avctx->sample_rate = s->sample_rate;
1771     avctx->channels    = s->nb_channels;
1772     if (!avctx->bit_rate)
1773         avctx->bit_rate = s->bit_rate;
1774     avctx->sub_id = s->layer;
1775
1776     s->frame_size = len;
1777
1778 #if FF_API_PARSE_FRAME
1779     if (avctx->parse_only)
1780         out_size = buf_size;
1781     else
1782 #endif
1783     out_size = mp_decode_frame(s, NULL, buf, buf_size);
1784
1785     *got_frame_ptr   = 1;
1786     *(AVFrame *)data = s->frame;
1787
1788     return buf_size;
1789 }
1790 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1791
1792 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1793
1794 /**
1795  * Context for MP3On4 decoder
1796  */
1797 typedef struct MP3On4DecodeContext {
1798     AVFrame *frame;
1799     int frames;                     ///< number of mp3 frames per block (number of mp3 decoder instances)
1800     int syncword;                   ///< syncword patch
1801     const uint8_t *coff;            ///< channel offsets in output buffer
1802     MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1803     OUT_INT *decoded_buf;           ///< output buffer for decoded samples
1804 } MP3On4DecodeContext;
1805
1806 #include "mpeg4audio.h"
1807
1808 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1809
1810 /* number of mp3 decoder instances */
1811 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1812
1813 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1814 static const uint8_t chan_offset[8][5] = {
1815     { 0             },
1816     { 0             },  // C
1817     { 0             },  // FLR
1818     { 2, 0          },  // C FLR
1819     { 2, 0, 3       },  // C FLR BS
1820     { 2, 0, 3       },  // C FLR BLRS
1821     { 2, 0, 4, 3    },  // C FLR BLRS LFE
1822     { 2, 0, 6, 4, 3 },  // C FLR BLRS BLR LFE
1823 };
1824
1825 /* mp3on4 channel layouts */
1826 static const int16_t chan_layout[8] = {
1827     0,
1828     AV_CH_LAYOUT_MONO,
1829     AV_CH_LAYOUT_STEREO,
1830     AV_CH_LAYOUT_SURROUND,
1831     AV_CH_LAYOUT_4POINT0,
1832     AV_CH_LAYOUT_5POINT0,
1833     AV_CH_LAYOUT_5POINT1,
1834     AV_CH_LAYOUT_7POINT1
1835 };
1836
1837 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1838 {
1839     MP3On4DecodeContext *s = avctx->priv_data;
1840     int i;
1841
1842     for (i = 0; i < s->frames; i++)
1843         av_free(s->mp3decctx[i]);
1844
1845     av_freep(&s->decoded_buf);
1846
1847     return 0;
1848 }
1849
1850
1851 static int decode_init_mp3on4(AVCodecContext * avctx)
1852 {
1853     MP3On4DecodeContext *s = avctx->priv_data;
1854     MPEG4AudioConfig cfg;
1855     int i;
1856
1857     if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1858         av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1859         return AVERROR_INVALIDDATA;
1860     }
1861
1862     avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1863                                  avctx->extradata_size * 8, 1);
1864     if (!cfg.chan_config || cfg.chan_config > 7) {
1865         av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1866         return AVERROR_INVALIDDATA;
1867     }
1868     s->frames             = mp3Frames[cfg.chan_config];
1869     s->coff               = chan_offset[cfg.chan_config];
1870     avctx->channels       = ff_mpeg4audio_channels[cfg.chan_config];
1871     avctx->channel_layout = chan_layout[cfg.chan_config];
1872
1873     if (cfg.sample_rate < 16000)
1874         s->syncword = 0xffe00000;
1875     else
1876         s->syncword = 0xfff00000;
1877
1878     /* Init the first mp3 decoder in standard way, so that all tables get builded
1879      * We replace avctx->priv_data with the context of the first decoder so that
1880      * decode_init() does not have to be changed.
1881      * Other decoders will be initialized here copying data from the first context
1882      */
1883     // Allocate zeroed memory for the first decoder context
1884     s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1885     if (!s->mp3decctx[0])
1886         goto alloc_fail;
1887     // Put decoder context in place to make init_decode() happy
1888     avctx->priv_data = s->mp3decctx[0];
1889     decode_init(avctx);
1890     s->frame = avctx->coded_frame;
1891     // Restore mp3on4 context pointer
1892     avctx->priv_data = s;
1893     s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1894
1895     /* Create a separate codec/context for each frame (first is already ok).
1896      * Each frame is 1 or 2 channels - up to 5 frames allowed
1897      */
1898     for (i = 1; i < s->frames; i++) {
1899         s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1900         if (!s->mp3decctx[i])
1901             goto alloc_fail;
1902         s->mp3decctx[i]->adu_mode = 1;
1903         s->mp3decctx[i]->avctx = avctx;
1904         s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1905     }
1906
1907     /* Allocate buffer for multi-channel output if needed */
1908     if (s->frames > 1) {
1909         s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1910                                    sizeof(*s->decoded_buf));
1911         if (!s->decoded_buf)
1912             goto alloc_fail;
1913     }
1914
1915     return 0;
1916 alloc_fail:
1917     decode_close_mp3on4(avctx);
1918     return AVERROR(ENOMEM);
1919 }
1920
1921
1922 static void flush_mp3on4(AVCodecContext *avctx)
1923 {
1924     int i;
1925     MP3On4DecodeContext *s = avctx->priv_data;
1926
1927     for (i = 0; i < s->frames; i++) {
1928         MPADecodeContext *m = s->mp3decctx[i];
1929         memset(m->synth_buf, 0, sizeof(m->synth_buf));
1930         m->last_buf_size = 0;
1931     }
1932 }
1933
1934
1935 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1936                                int *got_frame_ptr, AVPacket *avpkt)
1937 {
1938     const uint8_t *buf     = avpkt->data;
1939     int buf_size           = avpkt->size;
1940     MP3On4DecodeContext *s = avctx->priv_data;
1941     MPADecodeContext *m;
1942     int fsize, len = buf_size, out_size = 0;
1943     uint32_t header;
1944     OUT_INT *out_samples;
1945     OUT_INT *outptr, *bp;
1946     int fr, j, n, ch, ret;
1947
1948     /* get output buffer */
1949     s->frame->nb_samples = MPA_FRAME_SIZE;
1950     if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1951         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1952         return ret;
1953     }
1954     out_samples = (OUT_INT *)s->frame->data[0];
1955
1956     // Discard too short frames
1957     if (buf_size < HEADER_SIZE)
1958         return AVERROR_INVALIDDATA;
1959
1960     // If only one decoder interleave is not needed
1961     outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1962
1963     avctx->bit_rate = 0;
1964
1965     ch = 0;
1966     for (fr = 0; fr < s->frames; fr++) {
1967         fsize = AV_RB16(buf) >> 4;
1968         fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1969         m     = s->mp3decctx[fr];
1970         assert(m != NULL);
1971
1972         header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1973
1974         if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1975             break;
1976
1977         avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1978
1979         if (ch + m->nb_channels > avctx->channels) {
1980             av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1981                                         "channel count\n");
1982             return AVERROR_INVALIDDATA;
1983         }
1984         ch += m->nb_channels;
1985
1986         out_size += mp_decode_frame(m, outptr, buf, fsize);
1987         buf      += fsize;
1988         len      -= fsize;
1989
1990         if (s->frames > 1) {
1991             n = m->avctx->frame_size*m->nb_channels;
1992             /* interleave output data */
1993             bp = out_samples + s->coff[fr];
1994             if (m->nb_channels == 1) {
1995                 for (j = 0; j < n; j++) {
1996                     *bp = s->decoded_buf[j];
1997                     bp += avctx->channels;
1998                 }
1999             } else {
2000                 for (j = 0; j < n; j++) {
2001                     bp[0] = s->decoded_buf[j++];
2002                     bp[1] = s->decoded_buf[j];
2003                     bp   += avctx->channels;
2004                 }
2005             }
2006         }
2007         avctx->bit_rate += m->bit_rate;
2008     }
2009
2010     /* update codec info */
2011     avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2012
2013     s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
2014     *got_frame_ptr   = 1;
2015     *(AVFrame *)data = *s->frame;
2016
2017     return buf_size;
2018 }
2019 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2020
2021 #if !CONFIG_FLOAT
2022 #if CONFIG_MP1_DECODER
2023 AVCodec ff_mp1_decoder = {
2024     .name           = "mp1",
2025     .type           = AVMEDIA_TYPE_AUDIO,
2026     .id             = CODEC_ID_MP1,
2027     .priv_data_size = sizeof(MPADecodeContext),
2028     .init           = decode_init,
2029     .decode         = decode_frame,
2030 #if FF_API_PARSE_FRAME
2031     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2032 #else
2033     .capabilities   = CODEC_CAP_DR1,
2034 #endif
2035     .flush          = flush,
2036     .long_name      = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2037 };
2038 #endif
2039 #if CONFIG_MP2_DECODER
2040 AVCodec ff_mp2_decoder = {
2041     .name           = "mp2",
2042     .type           = AVMEDIA_TYPE_AUDIO,
2043     .id             = CODEC_ID_MP2,
2044     .priv_data_size = sizeof(MPADecodeContext),
2045     .init           = decode_init,
2046     .decode         = decode_frame,
2047 #if FF_API_PARSE_FRAME
2048     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2049 #else
2050     .capabilities   = CODEC_CAP_DR1,
2051 #endif
2052     .flush          = flush,
2053     .long_name      = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2054 };
2055 #endif
2056 #if CONFIG_MP3_DECODER
2057 AVCodec ff_mp3_decoder = {
2058     .name           = "mp3",
2059     .type           = AVMEDIA_TYPE_AUDIO,
2060     .id             = CODEC_ID_MP3,
2061     .priv_data_size = sizeof(MPADecodeContext),
2062     .init           = decode_init,
2063     .decode         = decode_frame,
2064 #if FF_API_PARSE_FRAME
2065     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2066 #else
2067     .capabilities   = CODEC_CAP_DR1,
2068 #endif
2069     .flush          = flush,
2070     .long_name      = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2071 };
2072 #endif
2073 #if CONFIG_MP3ADU_DECODER
2074 AVCodec ff_mp3adu_decoder = {
2075     .name           = "mp3adu",
2076     .type           = AVMEDIA_TYPE_AUDIO,
2077     .id             = CODEC_ID_MP3ADU,
2078     .priv_data_size = sizeof(MPADecodeContext),
2079     .init           = decode_init,
2080     .decode         = decode_frame_adu,
2081 #if FF_API_PARSE_FRAME
2082     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2083 #else
2084     .capabilities   = CODEC_CAP_DR1,
2085 #endif
2086     .flush          = flush,
2087     .long_name      = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2088 };
2089 #endif
2090 #if CONFIG_MP3ON4_DECODER
2091 AVCodec ff_mp3on4_decoder = {
2092     .name           = "mp3on4",
2093     .type           = AVMEDIA_TYPE_AUDIO,
2094     .id             = CODEC_ID_MP3ON4,
2095     .priv_data_size = sizeof(MP3On4DecodeContext),
2096     .init           = decode_init_mp3on4,
2097     .close          = decode_close_mp3on4,
2098     .decode         = decode_frame_mp3on4,
2099     .capabilities   = CODEC_CAP_DR1,
2100     .flush          = flush_mp3on4,
2101     .long_name      = NULL_IF_CONFIG_SMALL("MP3onMP4"),
2102 };
2103 #endif
2104 #endif