]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegaudiodec.c
Merge remote-tracking branch 'tjoppen/proper_mxf_track_linking'
[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 CONFIG_SAFE_BITSTREAM_READER
1440         s->gb.size_in_bits_plus8 += EXTRABYTES * 8;
1441 #endif
1442         skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1443     }
1444
1445     for (gr = 0; gr < nb_granules; gr++) {
1446         for (ch = 0; ch < s->nb_channels; ch++) {
1447             g = &s->granules[ch][gr];
1448             if (get_bits_count(&s->gb) < 0) {
1449                 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1450                        main_data_begin, s->last_buf_size, gr);
1451                 skip_bits_long(&s->gb, g->part2_3_length);
1452                 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1453                 if (get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer) {
1454                     skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1455                     s->gb           = s->in_gb;
1456                     s->in_gb.buffer = NULL;
1457                 }
1458                 continue;
1459             }
1460
1461             bits_pos = get_bits_count(&s->gb);
1462
1463             if (!s->lsf) {
1464                 uint8_t *sc;
1465                 int slen, slen1, slen2;
1466
1467                 /* MPEG1 scale factors */
1468                 slen1 = slen_table[0][g->scalefac_compress];
1469                 slen2 = slen_table[1][g->scalefac_compress];
1470                 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1471                 if (g->block_type == 2) {
1472                     n = g->switch_point ? 17 : 18;
1473                     j = 0;
1474                     if (slen1) {
1475                         for (i = 0; i < n; i++)
1476                             g->scale_factors[j++] = get_bits(&s->gb, slen1);
1477                     } else {
1478                         for (i = 0; i < n; i++)
1479                             g->scale_factors[j++] = 0;
1480                     }
1481                     if (slen2) {
1482                         for (i = 0; i < 18; i++)
1483                             g->scale_factors[j++] = get_bits(&s->gb, slen2);
1484                         for (i = 0; i < 3; i++)
1485                             g->scale_factors[j++] = 0;
1486                     } else {
1487                         for (i = 0; i < 21; i++)
1488                             g->scale_factors[j++] = 0;
1489                     }
1490                 } else {
1491                     sc = s->granules[ch][0].scale_factors;
1492                     j = 0;
1493                     for (k = 0; k < 4; k++) {
1494                         n = k == 0 ? 6 : 5;
1495                         if ((g->scfsi & (0x8 >> k)) == 0) {
1496                             slen = (k < 2) ? slen1 : slen2;
1497                             if (slen) {
1498                                 for (i = 0; i < n; i++)
1499                                     g->scale_factors[j++] = get_bits(&s->gb, slen);
1500                             } else {
1501                                 for (i = 0; i < n; i++)
1502                                     g->scale_factors[j++] = 0;
1503                             }
1504                         } else {
1505                             /* simply copy from last granule */
1506                             for (i = 0; i < n; i++) {
1507                                 g->scale_factors[j] = sc[j];
1508                                 j++;
1509                             }
1510                         }
1511                     }
1512                     g->scale_factors[j++] = 0;
1513                 }
1514             } else {
1515                 int tindex, tindex2, slen[4], sl, sf;
1516
1517                 /* LSF scale factors */
1518                 if (g->block_type == 2)
1519                     tindex = g->switch_point ? 2 : 1;
1520                 else
1521                     tindex = 0;
1522
1523                 sf = g->scalefac_compress;
1524                 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1525                     /* intensity stereo case */
1526                     sf >>= 1;
1527                     if (sf < 180) {
1528                         lsf_sf_expand(slen, sf, 6, 6, 0);
1529                         tindex2 = 3;
1530                     } else if (sf < 244) {
1531                         lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1532                         tindex2 = 4;
1533                     } else {
1534                         lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1535                         tindex2 = 5;
1536                     }
1537                 } else {
1538                     /* normal case */
1539                     if (sf < 400) {
1540                         lsf_sf_expand(slen, sf, 5, 4, 4);
1541                         tindex2 = 0;
1542                     } else if (sf < 500) {
1543                         lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1544                         tindex2 = 1;
1545                     } else {
1546                         lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1547                         tindex2 = 2;
1548                         g->preflag = 1;
1549                     }
1550                 }
1551
1552                 j = 0;
1553                 for (k = 0; k < 4; k++) {
1554                     n  = lsf_nsf_table[tindex2][tindex][k];
1555                     sl = slen[k];
1556                     if (sl) {
1557                         for (i = 0; i < n; i++)
1558                             g->scale_factors[j++] = get_bits(&s->gb, sl);
1559                     } else {
1560                         for (i = 0; i < n; i++)
1561                             g->scale_factors[j++] = 0;
1562                     }
1563                 }
1564                 /* XXX: should compute exact size */
1565                 for (; j < 40; j++)
1566                     g->scale_factors[j] = 0;
1567             }
1568
1569             exponents_from_scale_factors(s, g, exponents);
1570
1571             /* read Huffman coded residue */
1572             huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1573         } /* ch */
1574
1575         if (s->nb_channels == 2)
1576             compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1577
1578         for (ch = 0; ch < s->nb_channels; ch++) {
1579             g = &s->granules[ch][gr];
1580
1581             reorder_block(s, g);
1582             compute_antialias(s, g);
1583             compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1584         }
1585     } /* gr */
1586     if (get_bits_count(&s->gb) < 0)
1587         skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1588     return nb_granules * 18;
1589 }
1590
1591 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1592                            const uint8_t *buf, int buf_size)
1593 {
1594     int i, nb_frames, ch, ret;
1595     OUT_INT *samples_ptr;
1596
1597     init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1598
1599     /* skip error protection field */
1600     if (s->error_protection)
1601         skip_bits(&s->gb, 16);
1602
1603     switch(s->layer) {
1604     case 1:
1605         s->avctx->frame_size = 384;
1606         nb_frames = mp_decode_layer1(s);
1607         break;
1608     case 2:
1609         s->avctx->frame_size = 1152;
1610         nb_frames = mp_decode_layer2(s);
1611         break;
1612     case 3:
1613         s->avctx->frame_size = s->lsf ? 576 : 1152;
1614     default:
1615         nb_frames = mp_decode_layer3(s);
1616
1617         s->last_buf_size=0;
1618         if (s->in_gb.buffer) {
1619             align_get_bits(&s->gb);
1620             i = get_bits_left(&s->gb)>>3;
1621             if (i >= 0 && i <= BACKSTEP_SIZE) {
1622                 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1623                 s->last_buf_size=i;
1624             } else
1625                 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1626             s->gb           = s->in_gb;
1627             s->in_gb.buffer = NULL;
1628         }
1629
1630         align_get_bits(&s->gb);
1631         assert((get_bits_count(&s->gb) & 7) == 0);
1632         i = get_bits_left(&s->gb) >> 3;
1633
1634         if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1635             if (i < 0)
1636                 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1637             i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1638         }
1639         assert(i <= buf_size - HEADER_SIZE && i >= 0);
1640         memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1641         s->last_buf_size += i;
1642     }
1643
1644     /* get output buffer */
1645     if (!samples) {
1646         s->frame.nb_samples = s->avctx->frame_size;
1647         if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1648             av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1649             return ret;
1650         }
1651         samples = (OUT_INT *)s->frame.data[0];
1652     }
1653
1654     /* apply the synthesis filter */
1655     for (ch = 0; ch < s->nb_channels; ch++) {
1656         samples_ptr = samples + ch;
1657         for (i = 0; i < nb_frames; i++) {
1658             RENAME(ff_mpa_synth_filter)(
1659                          &s->mpadsp,
1660                          s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1661                          RENAME(ff_mpa_synth_window), &s->dither_state,
1662                          samples_ptr, s->nb_channels,
1663                          s->sb_samples[ch][i]);
1664             samples_ptr += 32 * s->nb_channels;
1665         }
1666     }
1667
1668     return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1669 }
1670
1671 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1672                         AVPacket *avpkt)
1673 {
1674     const uint8_t *buf  = avpkt->data;
1675     int buf_size        = avpkt->size;
1676     MPADecodeContext *s = avctx->priv_data;
1677     uint32_t header;
1678     int out_size;
1679
1680     if (buf_size < HEADER_SIZE)
1681         return AVERROR_INVALIDDATA;
1682
1683     header = AV_RB32(buf);
1684     if (ff_mpa_check_header(header) < 0) {
1685         av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1686         return AVERROR_INVALIDDATA;
1687     }
1688
1689     if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1690         /* free format: prepare to compute frame size */
1691         s->frame_size = -1;
1692         return AVERROR_INVALIDDATA;
1693     }
1694     /* update codec info */
1695     avctx->channels       = s->nb_channels;
1696     avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1697     if (!avctx->bit_rate)
1698         avctx->bit_rate = s->bit_rate;
1699     avctx->sub_id = s->layer;
1700
1701     if (s->frame_size <= 0 || s->frame_size > buf_size) {
1702         av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1703         return AVERROR_INVALIDDATA;
1704     }else if(s->frame_size < buf_size){
1705         av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1706         buf_size= s->frame_size;
1707     }
1708
1709     out_size = mp_decode_frame(s, NULL, buf, buf_size);
1710     if (out_size >= 0) {
1711         *got_frame_ptr   = 1;
1712         *(AVFrame *)data = s->frame;
1713         avctx->sample_rate = s->sample_rate;
1714         //FIXME maybe move the other codec info stuff from above here too
1715     } else {
1716         av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1717         /* Only return an error if the bad frame makes up the whole packet.
1718            If there is more data in the packet, just consume the bad frame
1719            instead of returning an error, which would discard the whole
1720            packet. */
1721         *got_frame_ptr = 0;
1722         if (buf_size == avpkt->size)
1723             return out_size;
1724     }
1725     s->frame_size = 0;
1726     return buf_size;
1727 }
1728
1729 static void flush(AVCodecContext *avctx)
1730 {
1731     MPADecodeContext *s = avctx->priv_data;
1732     memset(s->synth_buf, 0, sizeof(s->synth_buf));
1733     s->last_buf_size = 0;
1734 }
1735
1736 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1737 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1738                             int *got_frame_ptr, AVPacket *avpkt)
1739 {
1740     const uint8_t *buf  = avpkt->data;
1741     int buf_size        = avpkt->size;
1742     MPADecodeContext *s = avctx->priv_data;
1743     uint32_t header;
1744     int len, out_size;
1745
1746     len = buf_size;
1747
1748     // Discard too short frames
1749     if (buf_size < HEADER_SIZE) {
1750         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1751         return AVERROR_INVALIDDATA;
1752     }
1753
1754
1755     if (len > MPA_MAX_CODED_FRAME_SIZE)
1756         len = MPA_MAX_CODED_FRAME_SIZE;
1757
1758     // Get header and restore sync word
1759     header = AV_RB32(buf) | 0xffe00000;
1760
1761     if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1762         av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1763         return AVERROR_INVALIDDATA;
1764     }
1765
1766     avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1767     /* update codec info */
1768     avctx->sample_rate = s->sample_rate;
1769     avctx->channels    = s->nb_channels;
1770     if (!avctx->bit_rate)
1771         avctx->bit_rate = s->bit_rate;
1772     avctx->sub_id = s->layer;
1773
1774     s->frame_size = len;
1775
1776 #if FF_API_PARSE_FRAME
1777     if (avctx->parse_only)
1778         out_size = buf_size;
1779     else
1780 #endif
1781     out_size = mp_decode_frame(s, NULL, buf, buf_size);
1782
1783     *got_frame_ptr   = 1;
1784     *(AVFrame *)data = s->frame;
1785
1786     return buf_size;
1787 }
1788 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1789
1790 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1791
1792 /**
1793  * Context for MP3On4 decoder
1794  */
1795 typedef struct MP3On4DecodeContext {
1796     AVFrame *frame;
1797     int frames;                     ///< number of mp3 frames per block (number of mp3 decoder instances)
1798     int syncword;                   ///< syncword patch
1799     const uint8_t *coff;            ///< channel offsets in output buffer
1800     MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1801     OUT_INT *decoded_buf;           ///< output buffer for decoded samples
1802 } MP3On4DecodeContext;
1803
1804 #include "mpeg4audio.h"
1805
1806 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1807
1808 /* number of mp3 decoder instances */
1809 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1810
1811 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1812 static const uint8_t chan_offset[8][5] = {
1813     { 0             },
1814     { 0             },  // C
1815     { 0             },  // FLR
1816     { 2, 0          },  // C FLR
1817     { 2, 0, 3       },  // C FLR BS
1818     { 2, 0, 3       },  // C FLR BLRS
1819     { 2, 0, 4, 3    },  // C FLR BLRS LFE
1820     { 2, 0, 6, 4, 3 },  // C FLR BLRS BLR LFE
1821 };
1822
1823 /* mp3on4 channel layouts */
1824 static const int16_t chan_layout[8] = {
1825     0,
1826     AV_CH_LAYOUT_MONO,
1827     AV_CH_LAYOUT_STEREO,
1828     AV_CH_LAYOUT_SURROUND,
1829     AV_CH_LAYOUT_4POINT0,
1830     AV_CH_LAYOUT_5POINT0,
1831     AV_CH_LAYOUT_5POINT1,
1832     AV_CH_LAYOUT_7POINT1
1833 };
1834
1835 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1836 {
1837     MP3On4DecodeContext *s = avctx->priv_data;
1838     int i;
1839
1840     for (i = 0; i < s->frames; i++)
1841         av_free(s->mp3decctx[i]);
1842
1843     av_freep(&s->decoded_buf);
1844
1845     return 0;
1846 }
1847
1848
1849 static int decode_init_mp3on4(AVCodecContext * avctx)
1850 {
1851     MP3On4DecodeContext *s = avctx->priv_data;
1852     MPEG4AudioConfig cfg;
1853     int i;
1854
1855     if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1856         av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1857         return AVERROR_INVALIDDATA;
1858     }
1859
1860     avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1861                                  avctx->extradata_size * 8, 1);
1862     if (!cfg.chan_config || cfg.chan_config > 7) {
1863         av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1864         return AVERROR_INVALIDDATA;
1865     }
1866     s->frames             = mp3Frames[cfg.chan_config];
1867     s->coff               = chan_offset[cfg.chan_config];
1868     avctx->channels       = ff_mpeg4audio_channels[cfg.chan_config];
1869     avctx->channel_layout = chan_layout[cfg.chan_config];
1870
1871     if (cfg.sample_rate < 16000)
1872         s->syncword = 0xffe00000;
1873     else
1874         s->syncword = 0xfff00000;
1875
1876     /* Init the first mp3 decoder in standard way, so that all tables get builded
1877      * We replace avctx->priv_data with the context of the first decoder so that
1878      * decode_init() does not have to be changed.
1879      * Other decoders will be initialized here copying data from the first context
1880      */
1881     // Allocate zeroed memory for the first decoder context
1882     s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1883     if (!s->mp3decctx[0])
1884         goto alloc_fail;
1885     // Put decoder context in place to make init_decode() happy
1886     avctx->priv_data = s->mp3decctx[0];
1887     decode_init(avctx);
1888     s->frame = avctx->coded_frame;
1889     // Restore mp3on4 context pointer
1890     avctx->priv_data = s;
1891     s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1892
1893     /* Create a separate codec/context for each frame (first is already ok).
1894      * Each frame is 1 or 2 channels - up to 5 frames allowed
1895      */
1896     for (i = 1; i < s->frames; i++) {
1897         s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1898         if (!s->mp3decctx[i])
1899             goto alloc_fail;
1900         s->mp3decctx[i]->adu_mode = 1;
1901         s->mp3decctx[i]->avctx = avctx;
1902         s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1903     }
1904
1905     /* Allocate buffer for multi-channel output if needed */
1906     if (s->frames > 1) {
1907         s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1908                                    sizeof(*s->decoded_buf));
1909         if (!s->decoded_buf)
1910             goto alloc_fail;
1911     }
1912
1913     return 0;
1914 alloc_fail:
1915     decode_close_mp3on4(avctx);
1916     return AVERROR(ENOMEM);
1917 }
1918
1919
1920 static void flush_mp3on4(AVCodecContext *avctx)
1921 {
1922     int i;
1923     MP3On4DecodeContext *s = avctx->priv_data;
1924
1925     for (i = 0; i < s->frames; i++) {
1926         MPADecodeContext *m = s->mp3decctx[i];
1927         memset(m->synth_buf, 0, sizeof(m->synth_buf));
1928         m->last_buf_size = 0;
1929     }
1930 }
1931
1932
1933 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1934                                int *got_frame_ptr, AVPacket *avpkt)
1935 {
1936     const uint8_t *buf     = avpkt->data;
1937     int buf_size           = avpkt->size;
1938     MP3On4DecodeContext *s = avctx->priv_data;
1939     MPADecodeContext *m;
1940     int fsize, len = buf_size, out_size = 0;
1941     uint32_t header;
1942     OUT_INT *out_samples;
1943     OUT_INT *outptr, *bp;
1944     int fr, j, n, ch, ret;
1945
1946     /* get output buffer */
1947     s->frame->nb_samples = MPA_FRAME_SIZE;
1948     if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1949         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1950         return ret;
1951     }
1952     out_samples = (OUT_INT *)s->frame->data[0];
1953
1954     // Discard too short frames
1955     if (buf_size < HEADER_SIZE)
1956         return AVERROR_INVALIDDATA;
1957
1958     // If only one decoder interleave is not needed
1959     outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1960
1961     avctx->bit_rate = 0;
1962
1963     ch = 0;
1964     for (fr = 0; fr < s->frames; fr++) {
1965         fsize = AV_RB16(buf) >> 4;
1966         fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1967         m     = s->mp3decctx[fr];
1968         assert(m != NULL);
1969
1970         header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1971
1972         if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1973             break;
1974
1975         avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1976
1977         if (ch + m->nb_channels > avctx->channels) {
1978             av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1979                                         "channel count\n");
1980             return AVERROR_INVALIDDATA;
1981         }
1982         ch += m->nb_channels;
1983
1984         out_size += mp_decode_frame(m, outptr, buf, fsize);
1985         buf      += fsize;
1986         len      -= fsize;
1987
1988         if (s->frames > 1) {
1989             n = m->avctx->frame_size*m->nb_channels;
1990             /* interleave output data */
1991             bp = out_samples + s->coff[fr];
1992             if (m->nb_channels == 1) {
1993                 for (j = 0; j < n; j++) {
1994                     *bp = s->decoded_buf[j];
1995                     bp += avctx->channels;
1996                 }
1997             } else {
1998                 for (j = 0; j < n; j++) {
1999                     bp[0] = s->decoded_buf[j++];
2000                     bp[1] = s->decoded_buf[j];
2001                     bp   += avctx->channels;
2002                 }
2003             }
2004         }
2005         avctx->bit_rate += m->bit_rate;
2006     }
2007
2008     /* update codec info */
2009     avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2010
2011     s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
2012     *got_frame_ptr   = 1;
2013     *(AVFrame *)data = *s->frame;
2014
2015     return buf_size;
2016 }
2017 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2018
2019 #if !CONFIG_FLOAT
2020 #if CONFIG_MP1_DECODER
2021 AVCodec ff_mp1_decoder = {
2022     .name           = "mp1",
2023     .type           = AVMEDIA_TYPE_AUDIO,
2024     .id             = CODEC_ID_MP1,
2025     .priv_data_size = sizeof(MPADecodeContext),
2026     .init           = decode_init,
2027     .decode         = decode_frame,
2028 #if FF_API_PARSE_FRAME
2029     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2030 #else
2031     .capabilities   = CODEC_CAP_DR1,
2032 #endif
2033     .flush          = flush,
2034     .long_name      = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2035 };
2036 #endif
2037 #if CONFIG_MP2_DECODER
2038 AVCodec ff_mp2_decoder = {
2039     .name           = "mp2",
2040     .type           = AVMEDIA_TYPE_AUDIO,
2041     .id             = CODEC_ID_MP2,
2042     .priv_data_size = sizeof(MPADecodeContext),
2043     .init           = decode_init,
2044     .decode         = decode_frame,
2045 #if FF_API_PARSE_FRAME
2046     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2047 #else
2048     .capabilities   = CODEC_CAP_DR1,
2049 #endif
2050     .flush          = flush,
2051     .long_name      = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2052 };
2053 #endif
2054 #if CONFIG_MP3_DECODER
2055 AVCodec ff_mp3_decoder = {
2056     .name           = "mp3",
2057     .type           = AVMEDIA_TYPE_AUDIO,
2058     .id             = CODEC_ID_MP3,
2059     .priv_data_size = sizeof(MPADecodeContext),
2060     .init           = decode_init,
2061     .decode         = decode_frame,
2062 #if FF_API_PARSE_FRAME
2063     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2064 #else
2065     .capabilities   = CODEC_CAP_DR1,
2066 #endif
2067     .flush          = flush,
2068     .long_name      = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2069 };
2070 #endif
2071 #if CONFIG_MP3ADU_DECODER
2072 AVCodec ff_mp3adu_decoder = {
2073     .name           = "mp3adu",
2074     .type           = AVMEDIA_TYPE_AUDIO,
2075     .id             = CODEC_ID_MP3ADU,
2076     .priv_data_size = sizeof(MPADecodeContext),
2077     .init           = decode_init,
2078     .decode         = decode_frame_adu,
2079 #if FF_API_PARSE_FRAME
2080     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2081 #else
2082     .capabilities   = CODEC_CAP_DR1,
2083 #endif
2084     .flush          = flush,
2085     .long_name      = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2086 };
2087 #endif
2088 #if CONFIG_MP3ON4_DECODER
2089 AVCodec ff_mp3on4_decoder = {
2090     .name           = "mp3on4",
2091     .type           = AVMEDIA_TYPE_AUDIO,
2092     .id             = CODEC_ID_MP3ON4,
2093     .priv_data_size = sizeof(MP3On4DecodeContext),
2094     .init           = decode_init_mp3on4,
2095     .close          = decode_close_mp3on4,
2096     .decode         = decode_frame_mp3on4,
2097     .capabilities   = CODEC_CAP_DR1,
2098     .flush          = flush_mp3on4,
2099     .long_name      = NULL_IF_CONFIG_SMALL("MP3onMP4"),
2100 };
2101 #endif
2102 #endif