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