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