3 * Copyright (c) 2001, 2002 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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
23 * @file mpegaudiodec.c
29 #include "bitstream.h"
34 * - in low precision mode, use more 16 bit multiplies in synth filter
35 * - test lsf / mpeg25 extensively.
38 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
40 #ifdef CONFIG_MPEGAUDIO_HP
41 # define USE_HIGHPRECISION
44 #include "mpegaudio.h"
45 #include "mpegaudiodecheader.h"
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)
53 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
60 * Context for MP3On4 decoder
62 typedef 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
;
68 /* layer 3 "granule" */
69 typedef struct GranuleDef
{
74 int scalefac_compress
;
79 uint8_t scalefac_scale
;
80 uint8_t count1table_select
;
81 int region_size
[3]; /* number of huffman codes in each region */
83 int short_start
, long_end
; /* long/short band indexes */
84 uint8_t scale_factors
[40];
85 int32_t sb_hybrid
[SBLIMIT
* 18]; /* 576 samples */
88 #include "mpegaudiodata.h"
89 #include "mpegaudiodectab.h"
91 static void compute_antialias_integer(MPADecodeContext
*s
, GranuleDef
*g
);
92 static void compute_antialias_float(MPADecodeContext
*s
, GranuleDef
*g
);
94 /* vlc structure for decoding layer 3 huffman tables */
95 static VLC huff_vlc
[16];
96 static VLC huff_quad_vlc
[2];
97 /* computed from band_size_long */
98 static uint16_t band_index_long
[9][23];
99 /* XXX: free when all decoders are closed */
100 #define TABLE_4_3_SIZE (8191 + 16)*4
101 static int8_t table_4_3_exp
[TABLE_4_3_SIZE
];
102 static uint32_t table_4_3_value
[TABLE_4_3_SIZE
];
103 static uint32_t exp_table
[512];
104 static uint32_t expval_table
[512][16];
105 /* intensity stereo coef table */
106 static int32_t is_table
[2][16];
107 static int32_t is_table_lsf
[2][2][16];
108 static int32_t csa_table
[8][4];
109 static float csa_table_float
[8][4];
110 static int32_t mdct_win
[8][36];
112 /* lower 2 bits: modulo 3, higher bits: shift */
113 static uint16_t scale_factor_modshift
[64];
114 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
115 static int32_t scale_factor_mult
[15][3];
116 /* mult table for layer 2 group quantization */
118 #define SCALE_GEN(v) \
119 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
121 static const int32_t scale_factor_mult2
[3][3] = {
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 */
127 static DECLARE_ALIGNED_16(MPA_INT
, window
[512]);
129 /* layer 1 unscaling */
130 /* n = number of bits of the mantissa minus 1 */
131 static inline int l1_unscale(int n
, int mant
, int scale_factor
)
136 shift
= scale_factor_modshift
[scale_factor
];
139 val
= MUL64(mant
+ (-1 << n
) + 1, scale_factor_mult
[n
-1][mod
]);
141 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
142 return (int)((val
+ (1LL << (shift
- 1))) >> shift
);
145 static inline int l2_unscale_group(int steps
, int mant
, int scale_factor
)
149 shift
= scale_factor_modshift
[scale_factor
];
153 val
= (mant
- (steps
>> 1)) * scale_factor_mult2
[steps
>> 2][mod
];
154 /* NOTE: at this point, 0 <= shift <= 21 */
156 val
= (val
+ (1 << (shift
- 1))) >> shift
;
160 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
161 static inline int l3_unscale(int value
, int exponent
)
166 e
= table_4_3_exp
[4*value
+ (exponent
&3)];
167 m
= table_4_3_value
[4*value
+ (exponent
&3)];
168 e
-= (exponent
>> 2);
172 m
= (m
+ (1 << (e
-1))) >> e
;
177 /* all integer n^(4/3) computation code */
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))
183 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
185 static int dev_4_3_coefs
[DEV_ORDER
];
188 static int pow_mult3
[3] = {
190 POW_FIX(1.25992104989487316476),
191 POW_FIX(1.58740105196819947474),
195 static void int_pow_init(void)
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
;
206 #if 0 /* unused, remove? */
207 /* return the mantissa and the binary exponent */
208 static int int_pow(int i
, int *exp_ptr
)
216 while (a
< (1 << (POW_FRAC_BITS
- 1))) {
220 a
-= (1 << POW_FRAC_BITS
);
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) */
229 a
= POW_MULL(a
, pow_mult3
[er
]);
230 while (a
>= 2 * POW_FRAC_ONE
) {
234 /* convert to float */
235 while (a
< POW_FRAC_ONE
) {
239 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
240 #if POW_FRAC_BITS > FRAC_BITS
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
)) {
253 static int decode_init(AVCodecContext
* avctx
)
255 MPADecodeContext
*s
= avctx
->priv_data
;
261 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
262 avctx
->sample_fmt
= SAMPLE_FMT_S32
;
264 avctx
->sample_fmt
= SAMPLE_FMT_S16
;
266 s
->error_resilience
= avctx
->error_resilience
;
268 if(avctx
->antialias_algo
!= FF_AA_FLOAT
)
269 s
->compute_antialias
= compute_antialias_integer
;
271 s
->compute_antialias
= compute_antialias_float
;
273 if (!init
&& !avctx
->parse_only
) {
274 /* scale factors table for layer 1/2 */
277 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
280 scale_factor_modshift
[i
] = mod
| (shift
<< 2);
283 /* scale factor multiply for layer 1 */
287 norm
= ((INT64_C(1) << n
) * FRAC_ONE
) / ((1 << n
) - 1);
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
);
291 dprintf(avctx
, "%d: norm=%x s=%x %x %x\n",
293 scale_factor_mult
[i
][0],
294 scale_factor_mult
[i
][1],
295 scale_factor_mult
[i
][2]);
298 ff_mpa_synth_init(window
);
300 /* huffman decode tables */
302 const HuffTable
*h
= &mpa_huff_tables
[i
];
305 uint8_t tmp_bits
[512];
306 uint16_t tmp_codes
[512];
308 memset(tmp_bits
, 0, sizeof(tmp_bits
));
309 memset(tmp_codes
, 0, sizeof(tmp_codes
));
315 for(x
=0;x
<xsize
;x
++) {
316 for(y
=0;y
<xsize
;y
++){
317 tmp_bits
[(x
<< 5) | y
| ((x
&&y
)<<4)]= h
->bits
[j
];
318 tmp_codes
[(x
<< 5) | y
| ((x
&&y
)<<4)]= h
->codes
[j
++];
323 init_vlc(&huff_vlc
[i
], 7, 512,
324 tmp_bits
, 1, 1, tmp_codes
, 2, 2, 1);
327 init_vlc(&huff_quad_vlc
[i
], i
== 0 ? 7 : 4, 16,
328 mpa_quad_bits
[i
], 1, 1, mpa_quad_codes
[i
], 1, 1, 1);
334 band_index_long
[i
][j
] = k
;
335 k
+= band_size_long
[i
][j
];
337 band_index_long
[i
][22] = k
;
340 /* compute n ^ (4/3) and store it in mantissa/exp format */
343 for(i
=1;i
<TABLE_4_3_SIZE
;i
++) {
346 f
= pow((double)(i
/4), 4.0 / 3.0) * pow(2, (i
&3)*0.25);
348 m
= (uint32_t)(fm
*(1LL<<31) + 0.5);
349 e
+= FRAC_BITS
- 31 + 5 - 100;
351 /* normalized to FRAC_BITS */
352 table_4_3_value
[i
] = m
;
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
;
356 for(i
=0; i
<512*16; i
++){
357 int exponent
= (i
>>4);
358 double f
= pow(i
&15, 4.0 / 3.0) * pow(2, (exponent
-400)*0.25 + FRAC_BITS
+ 5);
359 expval_table
[exponent
][i
&15]= llrint(f
);
361 exp_table
[exponent
]= llrint(f
);
368 f
= tan((double)i
* M_PI
/ 12.0);
369 v
= FIXR(f
/ (1.0 + f
));
374 is_table
[1][6 - i
] = v
;
378 is_table
[0][i
] = is_table
[1][i
] = 0.0;
385 e
= -(j
+ 1) * ((i
+ 1) >> 1);
386 f
= pow(2.0, e
/ 4.0);
388 is_table_lsf
[j
][k
^ 1][i
] = FIXR(f
);
389 is_table_lsf
[j
][k
][i
] = FIXR(1.0);
390 dprintf(avctx
, "is_table_lsf %d %d: %x %x\n",
391 i
, j
, is_table_lsf
[j
][0][i
], is_table_lsf
[j
][1][i
]);
398 cs
= 1.0 / sqrt(1.0 + ci
* ci
);
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);
403 csa_table
[i
][3] = FIXHR(ca
/4) - FIXHR(cs
/4);
404 csa_table_float
[i
][0] = cs
;
405 csa_table_float
[i
][1] = ca
;
406 csa_table_float
[i
][2] = ca
+ cs
;
407 csa_table_float
[i
][3] = ca
- cs
;
408 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
409 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
412 /* compute mdct windows */
420 d
= sin(M_PI
* (i
+ 0.5) / 36.0);
423 else if(i
>=24) d
= sin(M_PI
* (i
- 18 + 0.5) / 12.0);
427 else if(i
< 12) d
= sin(M_PI
* (i
- 6 + 0.5) / 12.0);
430 //merge last stage of imdct into the window coefficients
431 d
*= 0.5 / cos(M_PI
*(2*i
+ 19)/72);
434 mdct_win
[j
][i
/3] = FIXHR((d
/ (1<<5)));
436 mdct_win
[j
][i
] = FIXHR((d
/ (1<<5)));
437 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
441 /* NOTE: we do frequency inversion adter the MDCT by changing
442 the sign of the right window coefs */
445 mdct_win
[j
+ 4][i
] = mdct_win
[j
][i
];
446 mdct_win
[j
+ 4][i
+ 1] = -mdct_win
[j
][i
+ 1];
452 av_log(avctx
, AV_LOG_DEBUG
, "win%d=\n", j
);
454 av_log(avctx
, AV_LOG_DEBUG
, "%f, ", (double)mdct_win
[j
][i
] / FRAC_ONE
);
455 av_log(avctx
, AV_LOG_DEBUG
, "\n");
464 if (avctx
->codec_id
== CODEC_ID_MP3ADU
)
469 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
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)
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)
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)
504 #define COS3_0 FIXHR(0.54119610014619698439/2)
505 #define COS3_1 FIXHR(1.30656296487637652785/4)
507 #define COS4_0 FIXHR(0.70710678118654752439/2)
509 /* butterfly operator */
510 #define BF(a, b, c, s)\
512 tmp0 = tab[a] + tab[b];\
513 tmp1 = tab[a] - tab[b];\
515 tab[b] = MULH(tmp1<<(s), c);\
518 #define BF1(a, b, c, d)\
520 BF(a, b, COS4_0, 1);\
521 BF(c, d,-COS4_0, 1);\
525 #define BF2(a, b, c, d)\
527 BF(a, b, COS4_0, 1);\
528 BF(c, d,-COS4_0, 1);\
535 #define ADD(a, b) tab[a] += tab[b]
537 /* DCT32 without 1/sqrt(2) coef zero scaling. */
538 static void dct32(int32_t *out
, int32_t *tab
)
543 BF( 0, 31, COS0_0
, 1);
544 BF(15, 16, COS0_15
, 5);
546 BF( 0, 15, COS1_0
, 1);
547 BF(16, 31,-COS1_0
, 1);
549 BF( 7, 24, COS0_7
, 1);
550 BF( 8, 23, COS0_8
, 1);
552 BF( 7, 8, COS1_7
, 4);
553 BF(23, 24,-COS1_7
, 4);
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);
560 BF( 3, 28, COS0_3
, 1);
561 BF(12, 19, COS0_12
, 2);
563 BF( 3, 12, COS1_3
, 1);
564 BF(19, 28,-COS1_3
, 1);
566 BF( 4, 27, COS0_4
, 1);
567 BF(11, 20, COS0_11
, 2);
569 BF( 4, 11, COS1_4
, 1);
570 BF(20, 27,-COS1_4
, 1);
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);
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);
589 BF( 1, 30, COS0_1
, 1);
590 BF(14, 17, COS0_14
, 3);
592 BF( 1, 14, COS1_1
, 1);
593 BF(17, 30,-COS1_1
, 1);
595 BF( 6, 25, COS0_6
, 1);
596 BF( 9, 22, COS0_9
, 1);
598 BF( 6, 9, COS1_6
, 2);
599 BF(22, 25,-COS1_6
, 2);
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);
607 BF( 2, 29, COS0_2
, 1);
608 BF(13, 18, COS0_13
, 3);
610 BF( 2, 13, COS1_2
, 1);
611 BF(18, 29,-COS1_2
, 1);
613 BF( 5, 26, COS0_5
, 1);
614 BF(10, 21, COS0_10
, 1);
616 BF( 5, 10, COS1_5
, 2);
617 BF(21, 26,-COS1_5
, 2);
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);
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);
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];
698 static inline int round_sample(int *sum
)
701 sum1
= (*sum
) >> OUT_SHIFT
;
702 *sum
&= (1<<OUT_SHIFT
)-1;
705 else if (sum1
> OUT_MAX
)
710 /* signed 16x16 -> 32 multiply add accumulate */
711 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
713 /* signed 16x16 -> 32 multiply */
714 #define MULS(ra, rb) MUL16(ra, rb)
718 static inline int round_sample(int64_t *sum
)
721 sum1
= (int)((*sum
) >> OUT_SHIFT
);
722 *sum
&= (1<<OUT_SHIFT
)-1;
725 else if (sum1
> OUT_MAX
)
730 # define MULS(ra, rb) MUL64(ra, rb)
733 #define SUM8(sum, op, w, p) \
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]);\
745 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
749 sum1 op1 MULS((w1)[0 * 64], tmp);\
750 sum2 op2 MULS((w2)[0 * 64], tmp);\
752 sum1 op1 MULS((w1)[1 * 64], tmp);\
753 sum2 op2 MULS((w2)[1 * 64], tmp);\
755 sum1 op1 MULS((w1)[2 * 64], tmp);\
756 sum2 op2 MULS((w2)[2 * 64], tmp);\
758 sum1 op1 MULS((w1)[3 * 64], tmp);\
759 sum2 op2 MULS((w2)[3 * 64], tmp);\
761 sum1 op1 MULS((w1)[4 * 64], tmp);\
762 sum2 op2 MULS((w2)[4 * 64], tmp);\
764 sum1 op1 MULS((w1)[5 * 64], tmp);\
765 sum2 op2 MULS((w2)[5 * 64], tmp);\
767 sum1 op1 MULS((w1)[6 * 64], tmp);\
768 sum2 op2 MULS((w2)[6 * 64], tmp);\
770 sum1 op1 MULS((w1)[7 * 64], tmp);\
771 sum2 op2 MULS((w2)[7 * 64], tmp);\
774 void ff_mpa_synth_init(MPA_INT
*window
)
778 /* max = 18760, max sum over all 16 coefs : 44736 */
781 v
= ff_mpa_enwindow
[i
];
783 v
= (v
+ (1 << (16 - WFRAC_BITS
- 1))) >> (16 - WFRAC_BITS
);
793 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
795 /* XXX: optimize by avoiding ring buffer usage */
796 void ff_mpa_synth_filter(MPA_INT
*synth_buf_ptr
, int *synth_buf_offset
,
797 MPA_INT
*window
, int *dither_state
,
798 OUT_INT
*samples
, int incr
,
799 int32_t sb_samples
[SBLIMIT
])
802 register MPA_INT
*synth_buf
;
803 register const MPA_INT
*w
, *w2
, *p
;
812 dct32(tmp
, sb_samples
);
814 offset
= *synth_buf_offset
;
815 synth_buf
= synth_buf_ptr
+ offset
;
820 /* NOTE: can cause a loss in precision if very high amplitude
822 v
= av_clip_int16(v
);
826 /* copy to avoid wrap */
827 memcpy(synth_buf
+ 512, synth_buf
, 32 * sizeof(MPA_INT
));
829 samples2
= samples
+ 31 * incr
;
837 SUM8(sum
, -=, w
+ 32, p
);
838 *samples
= round_sample(&sum
);
842 /* we calculate two samples at the same time to avoid one memory
843 access per two sample */
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
);
851 *samples
= round_sample(&sum
);
854 *samples2
= round_sample(&sum
);
861 SUM8(sum
, -=, w
+ 32, p
);
862 *samples
= round_sample(&sum
);
865 offset
= (offset
- 32) & 511;
866 *synth_buf_offset
= offset
;
869 #define C3 FIXHR(0.86602540378443864676/2)
871 /* 0.5 / cos(pi*(2*i+1)/36) */
872 static 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),
884 /* 0.5 / cos(pi*(2*i+1)/36) */
885 static 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),
897 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
899 static void imdct12(int *out
, int *in
)
901 int in0
, in1
, in2
, in3
, in4
, in5
, t1
, t2
;
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];
912 in2
= MULH(2*in2
, C3
);
913 in3
= MULH(4*in3
, C3
);
916 t2
= MULH(2*(in1
- in5
), icos36h
[4]);
926 in1
= MULH(in5
+ in3
, icos36h
[1]);
933 in5
= MULH(2*(in5
- in3
), icos36h
[7]);
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)
951 /* using Lee like decomposition followed by hand coded 9 points DCT */
952 static void imdct36(int *out
, int *buf
, int *in
, int *win
)
954 int i
, j
, t0
, t1
, t2
, t3
, s0
, s1
, s2
, s3
;
955 int tmp
[18], *tmp1
, *in1
;
966 //more accurate but slower
967 int64_t t0
, t1
, t2
, t3
;
968 t2
= in1
[2*4] + in1
[2*8] - in1
[2*2];
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);
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
);
979 tmp1
[10] = (t3
- t0
- t2
) >> 32;
980 tmp1
[ 2] = (t3
+ t0
+ t1
) >> 32;
981 tmp1
[14] = (t3
+ t2
- t1
) >> 32;
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
);
988 t1
= MUL64(2*(in1
[2*1] + in1
[2*7]), -C5
);
990 tmp1
[ 0] = (t2
+ t3
+ t0
) >> 32;
991 tmp1
[12] = (t2
+ t1
- t0
) >> 32;
992 tmp1
[ 8] = (t3
- t1
- t0
) >> 32;
994 t2
= in1
[2*4] + in1
[2*8] - in1
[2*2];
996 t3
= in1
[2*0] + (in1
[2*6]>>1);
997 t1
= in1
[2*0] - in1
[2*6];
998 tmp1
[ 6] = t1
- (t2
>>1);
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
);
1005 tmp1
[10] = t3
- t0
- t2
;
1006 tmp1
[ 2] = t3
+ t0
+ t1
;
1007 tmp1
[14] = t3
+ t2
- t1
;
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
);
1014 t1
= MULH(2*(in1
[2*1] + in1
[2*7]), -C5
);
1016 tmp1
[ 0] = t2
+ t3
+ t0
;
1017 tmp1
[12] = t2
+ t1
- t0
;
1018 tmp1
[ 8] = t3
- t1
- t0
;
1031 s1
= MULH(2*(t3
+ t2
), icos36h
[j
]);
1032 s3
= MULL(t3
- t2
, icos36
[8 - j
]);
1036 out
[(9 + j
)*SBLIMIT
] = MULH(t1
, win
[9 + j
]) + buf
[9 + j
];
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
]);
1043 out
[(9 + 8 - j
)*SBLIMIT
] = MULH(t1
, win
[9 + 8 - j
]) + buf
[9 + 8 - j
];
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
]);
1051 s1
= MULH(2*tmp
[17], icos36h
[4]);
1054 out
[(9 + 4)*SBLIMIT
] = MULH(t1
, win
[9 + 4]) + buf
[9 + 4];
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]);
1060 /* return the number of decoded frames */
1061 static int mp_decode_layer1(MPADecodeContext
*s
)
1063 int bound
, i
, v
, n
, ch
, j
, mant
;
1064 uint8_t allocation
[MPA_MAX_CHANNELS
][SBLIMIT
];
1065 uint8_t scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
];
1067 if (s
->mode
== MPA_JSTEREO
)
1068 bound
= (s
->mode_ext
+ 1) * 4;
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);
1078 for(i
=bound
;i
<SBLIMIT
;i
++) {
1079 allocation
[0][i
] = get_bits(&s
->gb
, 4);
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);
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);
1096 /* compute samples */
1098 for(i
=0;i
<bound
;i
++) {
1099 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1100 n
= allocation
[ch
][i
];
1102 mant
= get_bits(&s
->gb
, n
+ 1);
1103 v
= l1_unscale(n
, mant
, scale_factors
[ch
][i
]);
1107 s
->sb_samples
[ch
][j
][i
] = v
;
1110 for(i
=bound
;i
<SBLIMIT
;i
++) {
1111 n
= allocation
[0][i
];
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
;
1119 s
->sb_samples
[0][j
][i
] = 0;
1120 s
->sb_samples
[1][j
][i
] = 0;
1127 static int mp_decode_layer2(MPADecodeContext
*s
)
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
;
1137 /* select decoding table */
1138 table
= ff_mpa_l2_select_table(s
->bit_rate
/ 1000, s
->nb_channels
,
1139 s
->sample_rate
, s
->lsf
);
1140 sblimit
= ff_mpa_sblimit_table
[table
];
1141 alloc_table
= ff_mpa_alloc_tables
[table
];
1143 if (s
->mode
== MPA_JSTEREO
)
1144 bound
= (s
->mode_ext
+ 1) * 4;
1148 dprintf(s
->avctx
, "bound=%d sblimit=%d\n", bound
, sblimit
);
1151 if( bound
> sblimit
) bound
= sblimit
;
1153 /* parse bit allocation */
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
);
1160 j
+= 1 << bit_alloc_bits
;
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
;
1172 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1173 for(i
=0;i
<sblimit
;i
++)
1174 dprintf(s
->avctx
, " %d", bit_alloc
[ch
][i
]);
1175 dprintf(s
->avctx
, "\n");
1181 for(i
=0;i
<sblimit
;i
++) {
1182 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1183 if (bit_alloc
[ch
][i
])
1184 scale_code
[ch
][i
] = get_bits(&s
->gb
, 2);
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
]) {
1196 sf
[0] = get_bits(&s
->gb
, 6);
1197 sf
[1] = get_bits(&s
->gb
, 6);
1198 sf
[2] = get_bits(&s
->gb
, 6);
1201 sf
[0] = get_bits(&s
->gb
, 6);
1206 sf
[0] = get_bits(&s
->gb
, 6);
1207 sf
[2] = get_bits(&s
->gb
, 6);
1211 sf
[0] = get_bits(&s
->gb
, 6);
1212 sf
[2] = get_bits(&s
->gb
, 6);
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
];
1225 dprintf(s
->avctx
, " %d %d %d", sf
[0], sf
[1], sf
[2]);
1227 dprintf(s
->avctx
, " -");
1230 dprintf(s
->avctx
, "\n");
1236 for(l
=0;l
<12;l
+=3) {
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
];
1243 scale
= scale_factors
[ch
][i
][k
];
1244 qindex
= alloc_table
[j
+b
];
1245 bits
= ff_mpa_quant_bits
[qindex
];
1247 /* 3 values at the same time */
1248 v
= get_bits(&s
->gb
, -bits
);
1249 steps
= ff_mpa_quant_steps
[qindex
];
1250 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] =
1251 l2_unscale_group(steps
, v
% steps
, scale
);
1253 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] =
1254 l2_unscale_group(steps
, v
% steps
, scale
);
1256 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] =
1257 l2_unscale_group(steps
, v
, scale
);
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
;
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;
1271 /* next subband in alloc table */
1272 j
+= 1 << bit_alloc_bits
;
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
];
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
];
1283 bits
= ff_mpa_quant_bits
[qindex
];
1285 /* 3 values at the same time */
1286 v
= get_bits(&s
->gb
, -bits
);
1287 steps
= ff_mpa_quant_steps
[qindex
];
1290 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] =
1291 l2_unscale_group(steps
, mant
, scale0
);
1292 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] =
1293 l2_unscale_group(steps
, mant
, scale1
);
1296 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] =
1297 l2_unscale_group(steps
, mant
, scale0
);
1298 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] =
1299 l2_unscale_group(steps
, mant
, scale1
);
1300 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] =
1301 l2_unscale_group(steps
, v
, scale0
);
1302 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] =
1303 l2_unscale_group(steps
, v
, scale1
);
1306 mant
= get_bits(&s
->gb
, bits
);
1307 s
->sb_samples
[0][k
* 12 + l
+ m
][i
] =
1308 l1_unscale(bits
- 1, mant
, scale0
);
1309 s
->sb_samples
[1][k
* 12 + l
+ m
][i
] =
1310 l1_unscale(bits
- 1, mant
, scale1
);
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;
1321 /* next subband in alloc table */
1322 j
+= 1 << bit_alloc_bits
;
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;
1337 static inline void lsf_sf_expand(int *slen
,
1338 int sf
, int n1
, int n2
, int n3
)
1357 static void exponents_from_scale_factors(MPADecodeContext
*s
,
1361 const uint8_t *bstab
, *pretab
;
1362 int len
, i
, j
, k
, l
, v0
, shift
, gain
, gains
[3];
1365 exp_ptr
= exponents
;
1366 gain
= g
->global_gain
- 210;
1367 shift
= g
->scalefac_scale
+ 1;
1369 bstab
= band_size_long
[s
->sample_rate_index
];
1370 pretab
= mpa_pretab
[g
->preflag
];
1371 for(i
=0;i
<g
->long_end
;i
++) {
1372 v0
= gain
- ((g
->scale_factors
[i
] + pretab
[i
]) << shift
) + 400;
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);
1384 for(i
=g
->short_start
;i
<13;i
++) {
1387 v0
= gains
[l
] - (g
->scale_factors
[k
++] << shift
) + 400;
1395 /* handle n = 0 too */
1396 static inline int get_bitsz(GetBitContext
*s
, int n
)
1401 return get_bits(s
, n
);
1405 static 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
){
1408 s
->in_gb
.buffer
=NULL
;
1409 assert((get_bits_count(&s
->gb
) & 7) == 0);
1410 skip_bits_long(&s
->gb
, *pos
- *end_pos
);
1412 *end_pos
= *end_pos2
+ get_bits_count(&s
->gb
) - *pos
;
1413 *pos
= get_bits_count(&s
->gb
);
1417 static int huffman_decode(MPADecodeContext
*s
, GranuleDef
*g
,
1418 int16_t *exponents
, int end_pos2
)
1422 int last_pos
, bits_left
;
1424 int end_pos
= FFMIN(end_pos2
, s
->gb
.size_in_bits
);
1426 /* low frequencies (called big values) */
1429 int j
, k
, l
, linbits
;
1430 j
= g
->region_size
[i
];
1433 /* select vlc table */
1434 k
= g
->table_select
[i
];
1435 l
= mpa_huff_data
[k
][0];
1436 linbits
= mpa_huff_data
[k
][1];
1440 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
)*2*j
);
1445 /* read huffcode and compute each couple */
1447 int exponent
, x
, y
, v
;
1448 int pos
= get_bits_count(&s
->gb
);
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);
1452 switch_buffer(s
, &pos
, &end_pos
, &end_pos2
);
1453 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1457 y
= get_vlc2(&s
->gb
, vlc
->table
, 7, 3);
1460 g
->sb_hybrid
[s_index
] =
1461 g
->sb_hybrid
[s_index
+1] = 0;
1466 exponent
= exponents
[s_index
];
1468 dprintf(s
->avctx
, "region=%d n=%d x=%d y=%d exp=%d\n",
1469 i
, g
->region_size
[i
] - j
, x
, y
, exponent
);
1474 v
= expval_table
[ exponent
][ x
];
1475 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1477 x
+= get_bitsz(&s
->gb
, linbits
);
1478 v
= l3_unscale(x
, exponent
);
1480 if (get_bits1(&s
->gb
))
1482 g
->sb_hybrid
[s_index
] = v
;
1484 v
= expval_table
[ exponent
][ y
];
1486 y
+= get_bitsz(&s
->gb
, linbits
);
1487 v
= l3_unscale(y
, exponent
);
1489 if (get_bits1(&s
->gb
))
1491 g
->sb_hybrid
[s_index
+1] = v
;
1497 v
= expval_table
[ exponent
][ x
];
1499 x
+= get_bitsz(&s
->gb
, linbits
);
1500 v
= l3_unscale(x
, exponent
);
1502 if (get_bits1(&s
->gb
))
1504 g
->sb_hybrid
[s_index
+!!y
] = v
;
1505 g
->sb_hybrid
[s_index
+ !y
] = 0;
1511 /* high frequencies */
1512 vlc
= &huff_quad_vlc
[g
->count1table_select
];
1514 while (s_index
<= 572) {
1516 pos
= get_bits_count(&s
->gb
);
1517 if (pos
>= end_pos
) {
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 */
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
);
1524 if(s
->error_resilience
>= FF_ER_COMPLIANT
)
1528 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1529 switch_buffer(s
, &pos
, &end_pos
, &end_pos2
);
1530 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1536 code
= get_vlc2(&s
->gb
, vlc
->table
, vlc
->bits
, 1);
1537 dprintf(s
->avctx
, "t=%d code=%d\n", g
->count1table_select
, code
);
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;
1543 static const int idxtab
[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1545 int pos
= s_index
+idxtab
[code
];
1546 code
^= 8>>idxtab
[code
];
1547 v
= exp_table
[ exponents
[pos
] ];
1548 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1549 if(get_bits1(&s
->gb
))
1551 g
->sb_hybrid
[pos
] = v
;
1555 /* skip extension bits */
1556 bits_left
= end_pos2
- get_bits_count(&s
->gb
);
1557 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1558 if (bits_left
< 0/* || bits_left > 500*/) {
1559 av_log(NULL
, AV_LOG_ERROR
, "bits_left=%d\n", bits_left
);
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
);
1565 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
)*(576 - s_index
));
1566 skip_bits_long(&s
->gb
, bits_left
);
1568 i
= get_bits_count(&s
->gb
);
1569 switch_buffer(s
, &i
, &end_pos
, &end_pos2
);
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
1577 static void reorder_block(MPADecodeContext
*s
, GranuleDef
*g
)
1580 int32_t *ptr
, *dst
, *ptr1
;
1583 if (g
->block_type
!= 2)
1586 if (g
->switch_point
) {
1587 if (s
->sample_rate_index
!= 8) {
1588 ptr
= g
->sb_hybrid
+ 36;
1590 ptr
= g
->sb_hybrid
+ 48;
1596 for(i
=g
->short_start
;i
<13;i
++) {
1597 len
= band_size_short
[s
->sample_rate_index
][i
];
1600 for(j
=len
;j
>0;j
--) {
1601 *dst
++ = ptr
[0*len
];
1602 *dst
++ = ptr
[1*len
];
1603 *dst
++ = ptr
[2*len
];
1607 memcpy(ptr1
, tmp
, len
* 3 * sizeof(*ptr1
));
1611 #define ISQRT2 FIXR(0.70710678118654752440)
1613 static void compute_stereo(MPADecodeContext
*s
,
1614 GranuleDef
*g0
, GranuleDef
*g1
)
1618 int sf_max
, tmp0
, tmp1
, sf
, len
, non_zero_found
;
1619 int32_t (*is_tab
)[16];
1620 int32_t *tab0
, *tab1
;
1621 int non_zero_found_short
[3];
1623 /* intensity stereo */
1624 if (s
->mode_ext
& MODE_EXT_I_STEREO
) {
1629 is_tab
= is_table_lsf
[g1
->scalefac_compress
& 1];
1633 tab0
= g0
->sb_hybrid
+ 576;
1634 tab1
= g1
->sb_hybrid
+ 576;
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 */
1644 len
= band_size_short
[s
->sample_rate_index
][i
];
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
++) {
1652 non_zero_found_short
[l
] = 1;
1656 sf
= g1
->scale_factors
[k
+ l
];
1662 for(j
=0;j
<len
;j
++) {
1664 tab0
[j
] = MULL(tmp0
, v1
);
1665 tab1
[j
] = MULL(tmp0
, v2
);
1669 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1670 /* lower part of the spectrum : do ms stereo
1672 for(j
=0;j
<len
;j
++) {
1675 tab0
[j
] = MULL(tmp0
+ tmp1
, ISQRT2
);
1676 tab1
[j
] = MULL(tmp0
- tmp1
, ISQRT2
);
1683 non_zero_found
= non_zero_found_short
[0] |
1684 non_zero_found_short
[1] |
1685 non_zero_found_short
[2];
1687 for(i
= g1
->long_end
- 1;i
>= 0;i
--) {
1688 len
= band_size_long
[s
->sample_rate_index
][i
];
1691 /* test if non zero band. if so, stop doing i-stereo */
1692 if (!non_zero_found
) {
1693 for(j
=0;j
<len
;j
++) {
1699 /* for last band, use previous scale factor */
1700 k
= (i
== 21) ? 20 : i
;
1701 sf
= g1
->scale_factors
[k
];
1706 for(j
=0;j
<len
;j
++) {
1708 tab0
[j
] = MULL(tmp0
, v1
);
1709 tab1
[j
] = MULL(tmp0
, v2
);
1713 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1714 /* lower part of the spectrum : do ms stereo
1716 for(j
=0;j
<len
;j
++) {
1719 tab0
[j
] = MULL(tmp0
+ tmp1
, ISQRT2
);
1720 tab1
[j
] = MULL(tmp0
- tmp1
, ISQRT2
);
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
1729 tab0
= g0
->sb_hybrid
;
1730 tab1
= g1
->sb_hybrid
;
1731 for(i
=0;i
<576;i
++) {
1734 tab0
[i
] = tmp0
+ tmp1
;
1735 tab1
[i
] = tmp0
- tmp1
;
1740 static void compute_antialias_integer(MPADecodeContext
*s
,
1746 /* we antialias only "long" bands */
1747 if (g
->block_type
== 2) {
1748 if (!g
->switch_point
)
1750 /* XXX: check this for 8000Hz case */
1756 ptr
= g
->sb_hybrid
+ 18;
1757 for(i
= n
;i
> 0;i
--) {
1758 int tmp0
, tmp1
, tmp2
;
1759 csa
= &csa_table
[0][0];
1763 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1764 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1765 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1780 static void compute_antialias_float(MPADecodeContext
*s
,
1786 /* we antialias only "long" bands */
1787 if (g
->block_type
== 2) {
1788 if (!g
->switch_point
)
1790 /* XXX: check this for 8000Hz case */
1796 ptr
= g
->sb_hybrid
+ 18;
1797 for(i
= n
;i
> 0;i
--) {
1799 float *csa
= &csa_table_float
[0][0];
1800 #define FLOAT_AA(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]);
1819 static void compute_imdct(MPADecodeContext
*s
,
1821 int32_t *sb_samples
,
1824 int32_t *ptr
, *win
, *win1
, *buf
, *out_ptr
, *ptr1
;
1826 int i
, j
, mdct_long_end
, v
, sblimit
;
1828 /* find last non zero block */
1829 ptr
= g
->sb_hybrid
+ 576;
1830 ptr1
= g
->sb_hybrid
+ 2 * 18;
1831 while (ptr
>= ptr1
) {
1833 v
= ptr
[0] | ptr
[1] | ptr
[2] | ptr
[3] | ptr
[4] | ptr
[5];
1837 sblimit
= ((ptr
- g
->sb_hybrid
) / 18) + 1;
1839 if (g
->block_type
== 2) {
1840 /* XXX: check for 8000 Hz */
1841 if (g
->switch_point
)
1846 mdct_long_end
= sblimit
;
1851 for(j
=0;j
<mdct_long_end
;j
++) {
1852 /* apply window & overlap with previous buffer */
1853 out_ptr
= sb_samples
+ j
;
1855 if (g
->switch_point
&& j
< 2)
1858 win1
= mdct_win
[g
->block_type
];
1859 /* select frequency inversion */
1860 win
= win1
+ ((4 * 36) & -(j
& 1));
1861 imdct36(out_ptr
, buf
, ptr
, win
);
1862 out_ptr
+= 18*SBLIMIT
;
1866 for(j
=mdct_long_end
;j
<sblimit
;j
++) {
1867 /* select frequency inversion */
1868 win
= mdct_win
[2] + ((4 * 36) & -(j
& 1));
1869 out_ptr
= sb_samples
+ j
;
1875 imdct12(out2
, ptr
+ 0);
1877 *out_ptr
= MULH(out2
[i
], win
[i
]) + buf
[i
+ 6*1];
1878 buf
[i
+ 6*2] = MULH(out2
[i
+ 6], win
[i
+ 6]);
1881 imdct12(out2
, ptr
+ 1);
1883 *out_ptr
= MULH(out2
[i
], win
[i
]) + buf
[i
+ 6*2];
1884 buf
[i
+ 6*0] = MULH(out2
[i
+ 6], win
[i
+ 6]);
1887 imdct12(out2
, ptr
+ 2);
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]);
1897 for(j
=sblimit
;j
<SBLIMIT
;j
++) {
1899 out_ptr
= sb_samples
+ j
;
1910 void sample_dump(int fnum
, int32_t *tab
, int n
)
1912 static FILE *files
[16], *f
;
1919 snprintf(buf
, sizeof(buf
), "/tmp/out%d.%s.pcm",
1921 #ifdef USE_HIGHPRECISION
1927 f
= fopen(buf
, "w");
1935 av_log(NULL
, AV_LOG_DEBUG
, "pos=%d\n", pos
);
1937 av_log(NULL
, AV_LOG_DEBUG
, " %0.4f", (double)tab
[i
] / FRAC_ONE
);
1939 av_log(NULL
, AV_LOG_DEBUG
, "\n");
1944 /* normalize to 23 frac bits */
1945 v
= tab
[i
] << (23 - FRAC_BITS
);
1946 fwrite(&v
, 1, sizeof(int32_t), f
);
1952 /* main layer3 decoding function */
1953 static int mp_decode_layer3(MPADecodeContext
*s
)
1955 int nb_granules
, main_data_begin
, private_bits
;
1956 int gr
, ch
, blocksplit_flag
, i
, j
, k
, n
, bits_pos
;
1957 GranuleDef granules
[2][2], *g
;
1958 int16_t exponents
[576];
1960 /* read side info */
1962 main_data_begin
= get_bits(&s
->gb
, 8);
1963 private_bits
= get_bits(&s
->gb
, s
->nb_channels
);
1966 main_data_begin
= get_bits(&s
->gb
, 9);
1967 if (s
->nb_channels
== 2)
1968 private_bits
= get_bits(&s
->gb
, 3);
1970 private_bits
= get_bits(&s
->gb
, 5);
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);
1978 for(gr
=0;gr
<nb_granules
;gr
++) {
1979 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1980 dprintf(s
->avctx
, "gr=%d ch=%d: side_info\n", gr
, ch
);
1981 g
= &granules
[ch
][gr
];
1982 g
->part2_3_length
= get_bits(&s
->gb
, 12);
1983 g
->big_values
= get_bits(&s
->gb
, 9);
1984 if(g
->big_values
> 288){
1985 av_log(s
->avctx
, AV_LOG_ERROR
, "big_values too big\n");
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 */
1992 if ((s
->mode_ext
& (MODE_EXT_MS_STEREO
| MODE_EXT_I_STEREO
)) ==
1994 g
->global_gain
-= 2;
1996 g
->scalefac_compress
= get_bits(&s
->gb
, 9);
1998 g
->scalefac_compress
= get_bits(&s
->gb
, 4);
1999 blocksplit_flag
= get_bits1(&s
->gb
);
2000 if (blocksplit_flag
) {
2001 g
->block_type
= get_bits(&s
->gb
, 2);
2002 if (g
->block_type
== 0){
2003 av_log(NULL
, AV_LOG_ERROR
, "invalid block type\n");
2006 g
->switch_point
= get_bits1(&s
->gb
);
2008 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
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);
2015 if (s
->sample_rate_index
<= 2)
2016 g
->region_size
[0] = (36 / 2);
2017 else if (s
->sample_rate_index
!= 8)
2018 g
->region_size
[0] = (54 / 2);
2020 g
->region_size
[0] = (108 / 2);
2022 g
->region_size
[1] = (576 / 2);
2024 int region_address1
, region_address2
, l
;
2026 g
->switch_point
= 0;
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);
2032 dprintf(s
->avctx
, "region1=%d region2=%d\n",
2033 region_address1
, region_address2
);
2035 band_index_long
[s
->sample_rate_index
][region_address1
+ 1] >> 1;
2036 l
= region_address1
+ region_address2
+ 2;
2037 /* should not overflow */
2041 band_index_long
[s
->sample_rate_index
][l
] >> 1;
2043 /* convert region offsets to region sizes and truncate
2044 size to big_values */
2045 g
->region_size
[2] = (576 / 2);
2048 k
= FFMIN(g
->region_size
[i
], g
->big_values
);
2049 g
->region_size
[i
] = k
- j
;
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)
2061 else if (s
->sample_rate_index
!= 8)
2064 g
->long_end
= 4; /* 8000 Hz */
2066 g
->short_start
= 2 + (s
->sample_rate_index
!= 8);
2072 g
->short_start
= 13;
2078 g
->preflag
= get_bits1(&s
->gb
);
2079 g
->scalefac_scale
= get_bits1(&s
->gb
);
2080 g
->count1table_select
= get_bits1(&s
->gb
);
2081 dprintf(s
->avctx
, "block_type=%d switch_point=%d\n",
2082 g
->block_type
, g
->switch_point
);
2087 const uint8_t *ptr
= s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3);
2088 assert((get_bits_count(&s
->gb
) & 7) == 0);
2089 /* now we get bits from the main_data_begin offset */
2090 dprintf(s
->avctx
, "seekback: %d\n", main_data_begin
);
2091 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2093 memcpy(s
->last_buf
+ s
->last_buf_size
, ptr
, EXTRABYTES
);
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
));
2099 for(gr
=0;gr
<nb_granules
;gr
++) {
2100 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2101 g
= &granules
[ch
][gr
];
2102 if(get_bits_count(&s
->gb
)<0){
2103 av_log(NULL
, AV_LOG_ERROR
, "mdb:%d, lastbuf:%d skipping granule %d\n",
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
);
2110 s
->in_gb
.buffer
=NULL
;
2115 bits_pos
= get_bits_count(&s
->gb
);
2119 int slen
, slen1
, slen2
;
2121 /* MPEG1 scale factors */
2122 slen1
= slen_table
[0][g
->scalefac_compress
];
2123 slen2
= slen_table
[1][g
->scalefac_compress
];
2124 dprintf(s
->avctx
, "slen1=%d slen2=%d\n", slen1
, slen2
);
2125 if (g
->block_type
== 2) {
2126 n
= g
->switch_point
? 17 : 18;
2130 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen1
);
2133 g
->scale_factors
[j
++] = 0;
2137 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen2
);
2139 g
->scale_factors
[j
++] = 0;
2142 g
->scale_factors
[j
++] = 0;
2145 sc
= granules
[ch
][0].scale_factors
;
2148 n
= (k
== 0 ? 6 : 5);
2149 if ((g
->scfsi
& (0x8 >> k
)) == 0) {
2150 slen
= (k
< 2) ? slen1
: slen2
;
2153 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen
);
2156 g
->scale_factors
[j
++] = 0;
2159 /* simply copy from last granule */
2161 g
->scale_factors
[j
] = sc
[j
];
2166 g
->scale_factors
[j
++] = 0;
2170 dprintf(s
->avctx
, "scfsi=%x gr=%d ch=%d scale_factors:\n",
2173 dprintf(s
->avctx
, " %d", g
->scale_factors
[i
]);
2174 dprintf(s
->avctx
, "\n");
2178 int tindex
, tindex2
, slen
[4], sl
, sf
;
2180 /* LSF scale factors */
2181 if (g
->block_type
== 2) {
2182 tindex
= g
->switch_point
? 2 : 1;
2186 sf
= g
->scalefac_compress
;
2187 if ((s
->mode_ext
& MODE_EXT_I_STEREO
) && ch
== 1) {
2188 /* intensity stereo case */
2191 lsf_sf_expand(slen
, sf
, 6, 6, 0);
2193 } else if (sf
< 244) {
2194 lsf_sf_expand(slen
, sf
- 180, 4, 4, 0);
2197 lsf_sf_expand(slen
, sf
- 244, 3, 0, 0);
2203 lsf_sf_expand(slen
, sf
, 5, 4, 4);
2205 } else if (sf
< 500) {
2206 lsf_sf_expand(slen
, sf
- 400, 5, 4, 0);
2209 lsf_sf_expand(slen
, sf
- 500, 3, 0, 0);
2217 n
= lsf_nsf_table
[tindex2
][tindex
][k
];
2221 g
->scale_factors
[j
++] = get_bits(&s
->gb
, sl
);
2224 g
->scale_factors
[j
++] = 0;
2227 /* XXX: should compute exact size */
2229 g
->scale_factors
[j
] = 0;
2232 dprintf(s
->avctx
, "gr=%d ch=%d scale_factors:\n",
2235 dprintf(s
->avctx
, " %d", g
->scale_factors
[i
]);
2236 dprintf(s
->avctx
, "\n");
2241 exponents_from_scale_factors(s
, g
, exponents
);
2243 /* read Huffman coded residue */
2244 huffman_decode(s
, g
, exponents
, bits_pos
+ g
->part2_3_length
);
2246 sample_dump(0, g
->sb_hybrid
, 576);
2250 if (s
->nb_channels
== 2)
2251 compute_stereo(s
, &granules
[0][gr
], &granules
[1][gr
]);
2253 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2254 g
= &granules
[ch
][gr
];
2256 reorder_block(s
, g
);
2258 sample_dump(0, g
->sb_hybrid
, 576);
2260 s
->compute_antialias(s
, g
);
2262 sample_dump(1, g
->sb_hybrid
, 576);
2264 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
2266 sample_dump(2, &s
->sb_samples
[ch
][18 * gr
][0], 576);
2270 if(get_bits_count(&s
->gb
)<0)
2271 skip_bits_long(&s
->gb
, -get_bits_count(&s
->gb
));
2272 return nb_granules
* 18;
2275 static int mp_decode_frame(MPADecodeContext
*s
,
2276 OUT_INT
*samples
, const uint8_t *buf
, int buf_size
)
2278 int i
, nb_frames
, ch
;
2279 OUT_INT
*samples_ptr
;
2281 init_get_bits(&s
->gb
, buf
+ HEADER_SIZE
, (buf_size
- HEADER_SIZE
)*8);
2283 /* skip error protection field */
2284 if (s
->error_protection
)
2285 skip_bits(&s
->gb
, 16);
2287 dprintf(s
->avctx
, "frame %d:\n", s
->frame_count
);
2290 nb_frames
= mp_decode_layer1(s
);
2293 nb_frames
= mp_decode_layer2(s
);
2297 nb_frames
= mp_decode_layer3(s
);
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;
2303 if(i
>= 0 && i
<= BACKSTEP_SIZE
){
2304 memmove(s
->last_buf
, s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3), i
);
2307 av_log(NULL
, AV_LOG_ERROR
, "invalid old backstep %d\n", i
);
2309 s
->in_gb
.buffer
= NULL
;
2312 align_get_bits(&s
->gb
);
2313 assert((get_bits_count(&s
->gb
) & 7) == 0);
2314 i
= (s
->gb
.size_in_bits
- get_bits_count(&s
->gb
))>>3;
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
);
2320 assert(i
<= buf_size
- HEADER_SIZE
&& i
>= 0);
2321 memcpy(s
->last_buf
+ s
->last_buf_size
, s
->gb
.buffer
+ buf_size
- HEADER_SIZE
- i
, i
);
2322 s
->last_buf_size
+= i
;
2327 for(i
=0;i
<nb_frames
;i
++) {
2328 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2330 dprintf(s
->avctx
, "%d-%d:", i
, ch
);
2331 for(j
=0;j
<SBLIMIT
;j
++)
2332 dprintf(s
->avctx
, " %0.6f", (double)s
->sb_samples
[ch
][i
][j
] / FRAC_ONE
);
2333 dprintf(s
->avctx
, "\n");
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
++) {
2341 ff_mpa_synth_filter(s
->synth_buf
[ch
], &(s
->synth_buf_offset
[ch
]),
2342 window
, &s
->dither_state
,
2343 samples_ptr
, s
->nb_channels
,
2344 s
->sb_samples
[ch
][i
]);
2345 samples_ptr
+= 32 * s
->nb_channels
;
2351 return nb_frames
* 32 * sizeof(OUT_INT
) * s
->nb_channels
;
2354 static int decode_frame(AVCodecContext
* avctx
,
2355 void *data
, int *data_size
,
2356 uint8_t * buf
, int buf_size
)
2358 MPADecodeContext
*s
= avctx
->priv_data
;
2361 OUT_INT
*out_samples
= data
;
2364 if(buf_size
< HEADER_SIZE
)
2367 header
= AV_RB32(buf
);
2368 if(ff_mpa_check_header(header
) < 0){
2371 av_log(avctx
, AV_LOG_ERROR
, "Header missing skipping one byte.\n");
2375 if (ff_mpegaudio_decode_header(s
, header
) == 1) {
2376 /* free format: prepare to compute frame size */
2380 /* update codec info */
2381 avctx
->channels
= s
->nb_channels
;
2382 avctx
->bit_rate
= s
->bit_rate
;
2383 avctx
->sub_id
= s
->layer
;
2386 avctx
->frame_size
= 384;
2389 avctx
->frame_size
= 1152;
2393 avctx
->frame_size
= 576;
2395 avctx
->frame_size
= 1152;
2399 if(s
->frame_size
<=0 || s
->frame_size
> buf_size
){
2400 av_log(avctx
, AV_LOG_ERROR
, "incomplete frame\n");
2402 }else if(s
->frame_size
< buf_size
){
2403 av_log(avctx
, AV_LOG_ERROR
, "incorrect frame size\n");
2404 buf_size
= s
->frame_size
;
2407 out_size
= mp_decode_frame(s
, out_samples
, buf
, buf_size
);
2409 *data_size
= out_size
;
2410 avctx
->sample_rate
= s
->sample_rate
;
2411 //FIXME maybe move the other codec info stuff from above here too
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
2418 static void flush(AVCodecContext
*avctx
){
2419 MPADecodeContext
*s
= avctx
->priv_data
;
2420 s
->last_buf_size
= 0;
2423 #ifdef CONFIG_MP3ADU_DECODER
2424 static int decode_frame_adu(AVCodecContext
* avctx
,
2425 void *data
, int *data_size
,
2426 uint8_t * buf
, int buf_size
)
2428 MPADecodeContext
*s
= avctx
->priv_data
;
2431 OUT_INT
*out_samples
= data
;
2435 // Discard too short frames
2436 if (buf_size
< HEADER_SIZE
) {
2442 if (len
> MPA_MAX_CODED_FRAME_SIZE
)
2443 len
= MPA_MAX_CODED_FRAME_SIZE
;
2445 // Get header and restore sync word
2446 header
= AV_RB32(buf
) | 0xffe00000;
2448 if (ff_mpa_check_header(header
) < 0) { // Bad header, discard frame
2453 ff_mpegaudio_decode_header(s
, header
);
2454 /* update codec info */
2455 avctx
->sample_rate
= s
->sample_rate
;
2456 avctx
->channels
= s
->nb_channels
;
2457 avctx
->bit_rate
= s
->bit_rate
;
2458 avctx
->sub_id
= s
->layer
;
2460 avctx
->frame_size
=s
->frame_size
= len
;
2462 if (avctx
->parse_only
) {
2463 out_size
= buf_size
;
2465 out_size
= mp_decode_frame(s
, out_samples
, buf
, buf_size
);
2468 *data_size
= out_size
;
2471 #endif /* CONFIG_MP3ADU_DECODER */
2473 #ifdef CONFIG_MP3ON4_DECODER
2474 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2475 static int mp3Frames
[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2476 static int mp3Channels
[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2477 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2478 static int chan_offset
[9][5] = {
2483 {2,0,3}, // C FLR BS
2484 {4,0,2}, // C FLR BLRS
2485 {4,0,2,5}, // C FLR BLRS LFE
2486 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2491 static int decode_init_mp3on4(AVCodecContext
* avctx
)
2493 MP3On4DecodeContext
*s
= avctx
->priv_data
;
2496 if ((avctx
->extradata_size
< 2) || (avctx
->extradata
== NULL
)) {
2497 av_log(avctx
, AV_LOG_ERROR
, "Codec extradata missing or too short.\n");
2501 s
->chan_cfg
= (((unsigned char *)avctx
->extradata
)[1] >> 3) & 0x0f;
2502 s
->frames
= mp3Frames
[s
->chan_cfg
];
2504 av_log(avctx
, AV_LOG_ERROR
, "Invalid channel config number.\n");
2507 avctx
->channels
= mp3Channels
[s
->chan_cfg
];
2509 /* Init the first mp3 decoder in standard way, so that all tables get builded
2510 * We replace avctx->priv_data with the context of the first decoder so that
2511 * decode_init() does not have to be changed.
2512 * Other decoders will be inited here copying data from the first context
2514 // Allocate zeroed memory for the first decoder context
2515 s
->mp3decctx
[0] = av_mallocz(sizeof(MPADecodeContext
));
2516 // Put decoder context in place to make init_decode() happy
2517 avctx
->priv_data
= s
->mp3decctx
[0];
2519 // Restore mp3on4 context pointer
2520 avctx
->priv_data
= s
;
2521 s
->mp3decctx
[0]->adu_mode
= 1; // Set adu mode
2523 /* Create a separate codec/context for each frame (first is already ok).
2524 * Each frame is 1 or 2 channels - up to 5 frames allowed
2526 for (i
= 1; i
< s
->frames
; i
++) {
2527 s
->mp3decctx
[i
] = av_mallocz(sizeof(MPADecodeContext
));
2528 s
->mp3decctx
[i
]->compute_antialias
= s
->mp3decctx
[0]->compute_antialias
;
2529 s
->mp3decctx
[i
]->adu_mode
= 1;
2530 s
->mp3decctx
[i
]->avctx
= avctx
;
2537 static int decode_close_mp3on4(AVCodecContext
* avctx
)
2539 MP3On4DecodeContext
*s
= avctx
->priv_data
;
2542 for (i
= 0; i
< s
->frames
; i
++)
2543 if (s
->mp3decctx
[i
])
2544 av_free(s
->mp3decctx
[i
]);
2550 static int decode_frame_mp3on4(AVCodecContext
* avctx
,
2551 void *data
, int *data_size
,
2552 uint8_t * buf
, int buf_size
)
2554 MP3On4DecodeContext
*s
= avctx
->priv_data
;
2555 MPADecodeContext
*m
;
2556 int len
, out_size
= 0;
2558 OUT_INT
*out_samples
= data
;
2559 OUT_INT decoded_buf
[MPA_FRAME_SIZE
* MPA_MAX_CHANNELS
];
2560 OUT_INT
*outptr
, *bp
;
2562 unsigned char *start2
= buf
, *start
;
2564 int off
= avctx
->channels
;
2565 int *coff
= chan_offset
[s
->chan_cfg
];
2569 // Discard too short frames
2570 if (buf_size
< HEADER_SIZE
) {
2575 // If only one decoder interleave is not needed
2576 outptr
= s
->frames
== 1 ? out_samples
: decoded_buf
;
2578 for (fr
= 0; fr
< s
->frames
; fr
++) {
2580 fsize
= (start
[0] << 4) | (start
[1] >> 4);
2585 if (fsize
> MPA_MAX_CODED_FRAME_SIZE
)
2586 fsize
= MPA_MAX_CODED_FRAME_SIZE
;
2587 m
= s
->mp3decctx
[fr
];
2591 header
= AV_RB32(start
) | 0xfff00000;
2593 if (ff_mpa_check_header(header
) < 0) { // Bad header, discard block
2598 ff_mpegaudio_decode_header(m
, header
);
2599 mp_decode_frame(m
, decoded_buf
, start
, fsize
);
2601 n
= MPA_FRAME_SIZE
* m
->nb_channels
;
2602 out_size
+= n
* sizeof(OUT_INT
);
2604 /* interleave output data */
2605 bp
= out_samples
+ coff
[fr
];
2606 if(m
->nb_channels
== 1) {
2607 for(j
= 0; j
< n
; j
++) {
2608 *bp
= decoded_buf
[j
];
2612 for(j
= 0; j
< n
; j
++) {
2613 bp
[0] = decoded_buf
[j
++];
2614 bp
[1] = decoded_buf
[j
];
2621 /* update codec info */
2622 avctx
->sample_rate
= s
->mp3decctx
[0]->sample_rate
;
2623 avctx
->frame_size
= buf_size
;
2624 avctx
->bit_rate
= 0;
2625 for (i
= 0; i
< s
->frames
; i
++)
2626 avctx
->bit_rate
+= s
->mp3decctx
[i
]->bit_rate
;
2628 *data_size
= out_size
;
2631 #endif /* CONFIG_MP3ON4_DECODER */
2633 #ifdef CONFIG_MP2_DECODER
2634 AVCodec mp2_decoder
=
2639 sizeof(MPADecodeContext
),
2644 CODEC_CAP_PARSE_ONLY
,
2647 #ifdef CONFIG_MP3_DECODER
2648 AVCodec mp3_decoder
=
2653 sizeof(MPADecodeContext
),
2658 CODEC_CAP_PARSE_ONLY
,
2662 #ifdef CONFIG_MP3ADU_DECODER
2663 AVCodec mp3adu_decoder
=
2668 sizeof(MPADecodeContext
),
2673 CODEC_CAP_PARSE_ONLY
,
2677 #ifdef CONFIG_MP3ON4_DECODER
2678 AVCodec mp3on4_decoder
=
2683 sizeof(MP3On4DecodeContext
),
2686 decode_close_mp3on4
,
2687 decode_frame_mp3on4
,