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