00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/audioconvert.h"
00028 #include "avcodec.h"
00029 #include "get_bits.h"
00030 #include "mathops.h"
00031 #include "mpegaudiodsp.h"
00032
00033
00034
00035
00036
00037
00038 #include "mpegaudio.h"
00039 #include "mpegaudiodecheader.h"
00040
00041 #define BACKSTEP_SIZE 512
00042 #define EXTRABYTES 24
00043
00044
00045 typedef struct GranuleDef {
00046 uint8_t scfsi;
00047 int part2_3_length;
00048 int big_values;
00049 int global_gain;
00050 int scalefac_compress;
00051 uint8_t block_type;
00052 uint8_t switch_point;
00053 int table_select[3];
00054 int subblock_gain[3];
00055 uint8_t scalefac_scale;
00056 uint8_t count1table_select;
00057 int region_size[3];
00058 int preflag;
00059 int short_start, long_end;
00060 uint8_t scale_factors[40];
00061 INTFLOAT sb_hybrid[SBLIMIT * 18];
00062 } GranuleDef;
00063
00064 typedef struct MPADecodeContext {
00065 MPA_DECODE_HEADER
00066 uint8_t last_buf[2*BACKSTEP_SIZE + EXTRABYTES];
00067 int last_buf_size;
00068
00069 uint32_t free_format_next_header;
00070 GetBitContext gb;
00071 GetBitContext in_gb;
00072 DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
00073 int synth_buf_offset[MPA_MAX_CHANNELS];
00074 DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
00075 INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18];
00076 GranuleDef granules[2][2];
00077 #ifdef DEBUG
00078 int frame_count;
00079 #endif
00080 int adu_mode;
00081 int dither_state;
00082 int error_recognition;
00083 AVCodecContext* avctx;
00084 MPADSPContext mpadsp;
00085 } MPADecodeContext;
00086
00087 #if CONFIG_FLOAT
00088 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
00089 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
00090 # define FIXR(x) ((float)(x))
00091 # define FIXHR(x) ((float)(x))
00092 # define MULH3(x, y, s) ((s)*(y)*(x))
00093 # define MULLx(x, y, s) ((y)*(x))
00094 # define RENAME(a) a ## _float
00095 # define OUT_FMT AV_SAMPLE_FMT_FLT
00096 #else
00097 # define SHR(a,b) ((a)>>(b))
00098
00099 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
00100 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
00101 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
00102 # define MULH3(x, y, s) MULH((s)*(x), y)
00103 # define MULLx(x, y, s) MULL(x,y,s)
00104 # define RENAME(a) a ## _fixed
00105 # define OUT_FMT AV_SAMPLE_FMT_S16
00106 #endif
00107
00108
00109
00110 #define HEADER_SIZE 4
00111
00112 #include "mpegaudiodata.h"
00113 #include "mpegaudiodectab.h"
00114
00115
00116 static VLC huff_vlc[16];
00117 static VLC_TYPE huff_vlc_tables[
00118 0+128+128+128+130+128+154+166+
00119 142+204+190+170+542+460+662+414
00120 ][2];
00121 static const int huff_vlc_tables_sizes[16] = {
00122 0, 128, 128, 128, 130, 128, 154, 166,
00123 142, 204, 190, 170, 542, 460, 662, 414
00124 };
00125 static VLC huff_quad_vlc[2];
00126 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
00127 static const int huff_quad_vlc_tables_sizes[2] = {
00128 128, 16
00129 };
00130
00131 static uint16_t band_index_long[9][23];
00132 #include "mpegaudio_tablegen.h"
00133
00134 static INTFLOAT is_table[2][16];
00135 static INTFLOAT is_table_lsf[2][2][16];
00136 static INTFLOAT csa_table[8][4];
00137 static INTFLOAT mdct_win[8][36];
00138
00139 static int16_t division_tab3[1<<6 ];
00140 static int16_t division_tab5[1<<8 ];
00141 static int16_t division_tab9[1<<11];
00142
00143 static int16_t * const division_tabs[4] = {
00144 division_tab3, division_tab5, NULL, division_tab9
00145 };
00146
00147
00148 static uint16_t scale_factor_modshift[64];
00149
00150 static int32_t scale_factor_mult[15][3];
00151
00152
00153 #define SCALE_GEN(v) \
00154 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
00155
00156 static const int32_t scale_factor_mult2[3][3] = {
00157 SCALE_GEN(4.0 / 3.0),
00158 SCALE_GEN(4.0 / 5.0),
00159 SCALE_GEN(4.0 / 9.0),
00160 };
00161
00166 static void ff_region_offset2size(GranuleDef *g){
00167 int i, k, j=0;
00168 g->region_size[2] = (576 / 2);
00169 for(i=0;i<3;i++) {
00170 k = FFMIN(g->region_size[i], g->big_values);
00171 g->region_size[i] = k - j;
00172 j = k;
00173 }
00174 }
00175
00176 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
00177 if (g->block_type == 2)
00178 g->region_size[0] = (36 / 2);
00179 else {
00180 if (s->sample_rate_index <= 2)
00181 g->region_size[0] = (36 / 2);
00182 else if (s->sample_rate_index != 8)
00183 g->region_size[0] = (54 / 2);
00184 else
00185 g->region_size[0] = (108 / 2);
00186 }
00187 g->region_size[1] = (576 / 2);
00188 }
00189
00190 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
00191 int l;
00192 g->region_size[0] =
00193 band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
00194
00195 l = FFMIN(ra1 + ra2 + 2, 22);
00196 g->region_size[1] =
00197 band_index_long[s->sample_rate_index][l] >> 1;
00198 }
00199
00200 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
00201 if (g->block_type == 2) {
00202 if (g->switch_point) {
00203
00204
00205
00206 if (s->sample_rate_index <= 2)
00207 g->long_end = 8;
00208 else if (s->sample_rate_index != 8)
00209 g->long_end = 6;
00210 else
00211 g->long_end = 4;
00212
00213 g->short_start = 2 + (s->sample_rate_index != 8);
00214 } else {
00215 g->long_end = 0;
00216 g->short_start = 0;
00217 }
00218 } else {
00219 g->short_start = 13;
00220 g->long_end = 22;
00221 }
00222 }
00223
00224
00225
00226 static inline int l1_unscale(int n, int mant, int scale_factor)
00227 {
00228 int shift, mod;
00229 int64_t val;
00230
00231 shift = scale_factor_modshift[scale_factor];
00232 mod = shift & 3;
00233 shift >>= 2;
00234 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
00235 shift += n;
00236
00237 return (int)((val + (1LL << (shift - 1))) >> shift);
00238 }
00239
00240 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
00241 {
00242 int shift, mod, val;
00243
00244 shift = scale_factor_modshift[scale_factor];
00245 mod = shift & 3;
00246 shift >>= 2;
00247
00248 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
00249
00250 if (shift > 0)
00251 val = (val + (1 << (shift - 1))) >> shift;
00252 return val;
00253 }
00254
00255
00256 static inline int l3_unscale(int value, int exponent)
00257 {
00258 unsigned int m;
00259 int e;
00260
00261 e = table_4_3_exp [4*value + (exponent&3)];
00262 m = table_4_3_value[4*value + (exponent&3)];
00263 e -= (exponent >> 2);
00264 assert(e>=1);
00265 if (e > 31)
00266 return 0;
00267 m = (m + (1 << (e-1))) >> e;
00268
00269 return m;
00270 }
00271
00272 static av_cold int decode_init(AVCodecContext * avctx)
00273 {
00274 MPADecodeContext *s = avctx->priv_data;
00275 static int init=0;
00276 int i, j, k;
00277
00278 s->avctx = avctx;
00279
00280 ff_mpadsp_init(&s->mpadsp);
00281
00282 avctx->sample_fmt= OUT_FMT;
00283 s->error_recognition= avctx->error_recognition;
00284
00285 if (!init && !avctx->parse_only) {
00286 int offset;
00287
00288
00289 for(i=0;i<64;i++) {
00290 int shift, mod;
00291
00292 shift = (i / 3);
00293 mod = i % 3;
00294 scale_factor_modshift[i] = mod | (shift << 2);
00295 }
00296
00297
00298 for(i=0;i<15;i++) {
00299 int n, norm;
00300 n = i + 2;
00301 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
00302 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
00303 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
00304 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
00305 av_dlog(avctx, "%d: norm=%x s=%x %x %x\n",
00306 i, norm,
00307 scale_factor_mult[i][0],
00308 scale_factor_mult[i][1],
00309 scale_factor_mult[i][2]);
00310 }
00311
00312 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
00313
00314
00315 offset = 0;
00316 for(i=1;i<16;i++) {
00317 const HuffTable *h = &mpa_huff_tables[i];
00318 int xsize, x, y;
00319 uint8_t tmp_bits [512];
00320 uint16_t tmp_codes[512];
00321
00322 memset(tmp_bits , 0, sizeof(tmp_bits ));
00323 memset(tmp_codes, 0, sizeof(tmp_codes));
00324
00325 xsize = h->xsize;
00326
00327 j = 0;
00328 for(x=0;x<xsize;x++) {
00329 for(y=0;y<xsize;y++){
00330 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
00331 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
00332 }
00333 }
00334
00335
00336 huff_vlc[i].table = huff_vlc_tables+offset;
00337 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
00338 init_vlc(&huff_vlc[i], 7, 512,
00339 tmp_bits, 1, 1, tmp_codes, 2, 2,
00340 INIT_VLC_USE_NEW_STATIC);
00341 offset += huff_vlc_tables_sizes[i];
00342 }
00343 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
00344
00345 offset = 0;
00346 for(i=0;i<2;i++) {
00347 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
00348 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
00349 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
00350 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
00351 INIT_VLC_USE_NEW_STATIC);
00352 offset += huff_quad_vlc_tables_sizes[i];
00353 }
00354 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
00355
00356 for(i=0;i<9;i++) {
00357 k = 0;
00358 for(j=0;j<22;j++) {
00359 band_index_long[i][j] = k;
00360 k += band_size_long[i][j];
00361 }
00362 band_index_long[i][22] = k;
00363 }
00364
00365
00366
00367 mpegaudio_tableinit();
00368
00369 for (i = 0; i < 4; i++)
00370 if (ff_mpa_quant_bits[i] < 0)
00371 for (j = 0; j < (1<<(-ff_mpa_quant_bits[i]+1)); j++) {
00372 int val1, val2, val3, steps;
00373 int val = j;
00374 steps = ff_mpa_quant_steps[i];
00375 val1 = val % steps;
00376 val /= steps;
00377 val2 = val % steps;
00378 val3 = val / steps;
00379 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
00380 }
00381
00382
00383 for(i=0;i<7;i++) {
00384 float f;
00385 INTFLOAT v;
00386 if (i != 6) {
00387 f = tan((double)i * M_PI / 12.0);
00388 v = FIXR(f / (1.0 + f));
00389 } else {
00390 v = FIXR(1.0);
00391 }
00392 is_table[0][i] = v;
00393 is_table[1][6 - i] = v;
00394 }
00395
00396 for(i=7;i<16;i++)
00397 is_table[0][i] = is_table[1][i] = 0.0;
00398
00399 for(i=0;i<16;i++) {
00400 double f;
00401 int e, k;
00402
00403 for(j=0;j<2;j++) {
00404 e = -(j + 1) * ((i + 1) >> 1);
00405 f = pow(2.0, e / 4.0);
00406 k = i & 1;
00407 is_table_lsf[j][k ^ 1][i] = FIXR(f);
00408 is_table_lsf[j][k][i] = FIXR(1.0);
00409 av_dlog(avctx, "is_table_lsf %d %d: %f %f\n",
00410 i, j, (float) is_table_lsf[j][0][i],
00411 (float) is_table_lsf[j][1][i]);
00412 }
00413 }
00414
00415 for(i=0;i<8;i++) {
00416 float ci, cs, ca;
00417 ci = ci_table[i];
00418 cs = 1.0 / sqrt(1.0 + ci * ci);
00419 ca = cs * ci;
00420 #if !CONFIG_FLOAT
00421 csa_table[i][0] = FIXHR(cs/4);
00422 csa_table[i][1] = FIXHR(ca/4);
00423 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
00424 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
00425 #else
00426 csa_table[i][0] = cs;
00427 csa_table[i][1] = ca;
00428 csa_table[i][2] = ca + cs;
00429 csa_table[i][3] = ca - cs;
00430 #endif
00431 }
00432
00433
00434 for(i=0;i<36;i++) {
00435 for(j=0; j<4; j++){
00436 double d;
00437
00438 if(j==2 && i%3 != 1)
00439 continue;
00440
00441 d= sin(M_PI * (i + 0.5) / 36.0);
00442 if(j==1){
00443 if (i>=30) d= 0;
00444 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
00445 else if(i>=18) d= 1;
00446 }else if(j==3){
00447 if (i< 6) d= 0;
00448 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
00449 else if(i< 18) d= 1;
00450 }
00451
00452 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
00453
00454 if(j==2)
00455 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
00456 else
00457 mdct_win[j][i ] = FIXHR((d / (1<<5)));
00458 }
00459 }
00460
00461
00462
00463 for(j=0;j<4;j++) {
00464 for(i=0;i<36;i+=2) {
00465 mdct_win[j + 4][i] = mdct_win[j][i];
00466 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
00467 }
00468 }
00469
00470 init = 1;
00471 }
00472
00473 if (avctx->codec_id == CODEC_ID_MP3ADU)
00474 s->adu_mode = 1;
00475 return 0;
00476 }
00477
00478 #define C3 FIXHR(0.86602540378443864676/2)
00479
00480
00481 static const INTFLOAT icos36[9] = {
00482 FIXR(0.50190991877167369479),
00483 FIXR(0.51763809020504152469),
00484 FIXR(0.55168895948124587824),
00485 FIXR(0.61038729438072803416),
00486 FIXR(0.70710678118654752439),
00487 FIXR(0.87172339781054900991),
00488 FIXR(1.18310079157624925896),
00489 FIXR(1.93185165257813657349),
00490 FIXR(5.73685662283492756461),
00491 };
00492
00493
00494 static const INTFLOAT icos36h[9] = {
00495 FIXHR(0.50190991877167369479/2),
00496 FIXHR(0.51763809020504152469/2),
00497 FIXHR(0.55168895948124587824/2),
00498 FIXHR(0.61038729438072803416/2),
00499 FIXHR(0.70710678118654752439/2),
00500 FIXHR(0.87172339781054900991/2),
00501 FIXHR(1.18310079157624925896/4),
00502 FIXHR(1.93185165257813657349/4),
00503
00504 };
00505
00506
00507
00508 static void imdct12(INTFLOAT *out, INTFLOAT *in)
00509 {
00510 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
00511
00512 in0= in[0*3];
00513 in1= in[1*3] + in[0*3];
00514 in2= in[2*3] + in[1*3];
00515 in3= in[3*3] + in[2*3];
00516 in4= in[4*3] + in[3*3];
00517 in5= in[5*3] + in[4*3];
00518 in5 += in3;
00519 in3 += in1;
00520
00521 in2= MULH3(in2, C3, 2);
00522 in3= MULH3(in3, C3, 4);
00523
00524 t1 = in0 - in4;
00525 t2 = MULH3(in1 - in5, icos36h[4], 2);
00526
00527 out[ 7]=
00528 out[10]= t1 + t2;
00529 out[ 1]=
00530 out[ 4]= t1 - t2;
00531
00532 in0 += SHR(in4, 1);
00533 in4 = in0 + in2;
00534 in5 += 2*in1;
00535 in1 = MULH3(in5 + in3, icos36h[1], 1);
00536 out[ 8]=
00537 out[ 9]= in4 + in1;
00538 out[ 2]=
00539 out[ 3]= in4 - in1;
00540
00541 in0 -= in2;
00542 in5 = MULH3(in5 - in3, icos36h[7], 2);
00543 out[ 0]=
00544 out[ 5]= in0 - in5;
00545 out[ 6]=
00546 out[11]= in0 + in5;
00547 }
00548
00549
00550 #define C1 FIXHR(0.98480775301220805936/2)
00551 #define C2 FIXHR(0.93969262078590838405/2)
00552 #define C3 FIXHR(0.86602540378443864676/2)
00553 #define C4 FIXHR(0.76604444311897803520/2)
00554 #define C5 FIXHR(0.64278760968653932632/2)
00555 #define C6 FIXHR(0.5/2)
00556 #define C7 FIXHR(0.34202014332566873304/2)
00557 #define C8 FIXHR(0.17364817766693034885/2)
00558
00559
00560
00561 static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
00562 {
00563 int i, j;
00564 INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
00565 INTFLOAT tmp[18], *tmp1, *in1;
00566
00567 for(i=17;i>=1;i--)
00568 in[i] += in[i-1];
00569 for(i=17;i>=3;i-=2)
00570 in[i] += in[i-2];
00571
00572 for(j=0;j<2;j++) {
00573 tmp1 = tmp + j;
00574 in1 = in + j;
00575
00576 t2 = in1[2*4] + in1[2*8] - in1[2*2];
00577
00578 t3 = in1[2*0] + SHR(in1[2*6],1);
00579 t1 = in1[2*0] - in1[2*6];
00580 tmp1[ 6] = t1 - SHR(t2,1);
00581 tmp1[16] = t1 + t2;
00582
00583 t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
00584 t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
00585 t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
00586
00587 tmp1[10] = t3 - t0 - t2;
00588 tmp1[ 2] = t3 + t0 + t1;
00589 tmp1[14] = t3 + t2 - t1;
00590
00591 tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
00592 t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
00593 t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
00594 t0 = MULH3(in1[2*3], C3, 2);
00595
00596 t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
00597
00598 tmp1[ 0] = t2 + t3 + t0;
00599 tmp1[12] = t2 + t1 - t0;
00600 tmp1[ 8] = t3 - t1 - t0;
00601 }
00602
00603 i = 0;
00604 for(j=0;j<4;j++) {
00605 t0 = tmp[i];
00606 t1 = tmp[i + 2];
00607 s0 = t1 + t0;
00608 s2 = t1 - t0;
00609
00610 t2 = tmp[i + 1];
00611 t3 = tmp[i + 3];
00612 s1 = MULH3(t3 + t2, icos36h[j], 2);
00613 s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
00614
00615 t0 = s0 + s1;
00616 t1 = s0 - s1;
00617 out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j];
00618 out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j];
00619 buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
00620 buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
00621
00622 t0 = s2 + s3;
00623 t1 = s2 - s3;
00624 out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
00625 out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
00626 buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
00627 buf[ + j] = MULH3(t0, win[18 + j], 1);
00628 i += 4;
00629 }
00630
00631 s0 = tmp[16];
00632 s1 = MULH3(tmp[17], icos36h[4], 2);
00633 t0 = s0 + s1;
00634 t1 = s0 - s1;
00635 out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
00636 out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
00637 buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
00638 buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
00639 }
00640
00641
00642 static int mp_decode_layer1(MPADecodeContext *s)
00643 {
00644 int bound, i, v, n, ch, j, mant;
00645 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
00646 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
00647
00648 if (s->mode == MPA_JSTEREO)
00649 bound = (s->mode_ext + 1) * 4;
00650 else
00651 bound = SBLIMIT;
00652
00653
00654 for(i=0;i<bound;i++) {
00655 for(ch=0;ch<s->nb_channels;ch++) {
00656 allocation[ch][i] = get_bits(&s->gb, 4);
00657 }
00658 }
00659 for(i=bound;i<SBLIMIT;i++) {
00660 allocation[0][i] = get_bits(&s->gb, 4);
00661 }
00662
00663
00664 for(i=0;i<bound;i++) {
00665 for(ch=0;ch<s->nb_channels;ch++) {
00666 if (allocation[ch][i])
00667 scale_factors[ch][i] = get_bits(&s->gb, 6);
00668 }
00669 }
00670 for(i=bound;i<SBLIMIT;i++) {
00671 if (allocation[0][i]) {
00672 scale_factors[0][i] = get_bits(&s->gb, 6);
00673 scale_factors[1][i] = get_bits(&s->gb, 6);
00674 }
00675 }
00676
00677
00678 for(j=0;j<12;j++) {
00679 for(i=0;i<bound;i++) {
00680 for(ch=0;ch<s->nb_channels;ch++) {
00681 n = allocation[ch][i];
00682 if (n) {
00683 mant = get_bits(&s->gb, n + 1);
00684 v = l1_unscale(n, mant, scale_factors[ch][i]);
00685 } else {
00686 v = 0;
00687 }
00688 s->sb_samples[ch][j][i] = v;
00689 }
00690 }
00691 for(i=bound;i<SBLIMIT;i++) {
00692 n = allocation[0][i];
00693 if (n) {
00694 mant = get_bits(&s->gb, n + 1);
00695 v = l1_unscale(n, mant, scale_factors[0][i]);
00696 s->sb_samples[0][j][i] = v;
00697 v = l1_unscale(n, mant, scale_factors[1][i]);
00698 s->sb_samples[1][j][i] = v;
00699 } else {
00700 s->sb_samples[0][j][i] = 0;
00701 s->sb_samples[1][j][i] = 0;
00702 }
00703 }
00704 }
00705 return 12;
00706 }
00707
00708 static int mp_decode_layer2(MPADecodeContext *s)
00709 {
00710 int sblimit;
00711 const unsigned char *alloc_table;
00712 int table, bit_alloc_bits, i, j, ch, bound, v;
00713 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
00714 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
00715 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
00716 int scale, qindex, bits, steps, k, l, m, b;
00717
00718
00719 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
00720 s->sample_rate, s->lsf);
00721 sblimit = ff_mpa_sblimit_table[table];
00722 alloc_table = ff_mpa_alloc_tables[table];
00723
00724 if (s->mode == MPA_JSTEREO)
00725 bound = (s->mode_ext + 1) * 4;
00726 else
00727 bound = sblimit;
00728
00729 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
00730
00731
00732 if( bound > sblimit ) bound = sblimit;
00733
00734
00735 j = 0;
00736 for(i=0;i<bound;i++) {
00737 bit_alloc_bits = alloc_table[j];
00738 for(ch=0;ch<s->nb_channels;ch++) {
00739 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
00740 }
00741 j += 1 << bit_alloc_bits;
00742 }
00743 for(i=bound;i<sblimit;i++) {
00744 bit_alloc_bits = alloc_table[j];
00745 v = get_bits(&s->gb, bit_alloc_bits);
00746 bit_alloc[0][i] = v;
00747 bit_alloc[1][i] = v;
00748 j += 1 << bit_alloc_bits;
00749 }
00750
00751
00752 for(i=0;i<sblimit;i++) {
00753 for(ch=0;ch<s->nb_channels;ch++) {
00754 if (bit_alloc[ch][i])
00755 scale_code[ch][i] = get_bits(&s->gb, 2);
00756 }
00757 }
00758
00759
00760 for(i=0;i<sblimit;i++) {
00761 for(ch=0;ch<s->nb_channels;ch++) {
00762 if (bit_alloc[ch][i]) {
00763 sf = scale_factors[ch][i];
00764 switch(scale_code[ch][i]) {
00765 default:
00766 case 0:
00767 sf[0] = get_bits(&s->gb, 6);
00768 sf[1] = get_bits(&s->gb, 6);
00769 sf[2] = get_bits(&s->gb, 6);
00770 break;
00771 case 2:
00772 sf[0] = get_bits(&s->gb, 6);
00773 sf[1] = sf[0];
00774 sf[2] = sf[0];
00775 break;
00776 case 1:
00777 sf[0] = get_bits(&s->gb, 6);
00778 sf[2] = get_bits(&s->gb, 6);
00779 sf[1] = sf[0];
00780 break;
00781 case 3:
00782 sf[0] = get_bits(&s->gb, 6);
00783 sf[2] = get_bits(&s->gb, 6);
00784 sf[1] = sf[2];
00785 break;
00786 }
00787 }
00788 }
00789 }
00790
00791
00792 for(k=0;k<3;k++) {
00793 for(l=0;l<12;l+=3) {
00794 j = 0;
00795 for(i=0;i<bound;i++) {
00796 bit_alloc_bits = alloc_table[j];
00797 for(ch=0;ch<s->nb_channels;ch++) {
00798 b = bit_alloc[ch][i];
00799 if (b) {
00800 scale = scale_factors[ch][i][k];
00801 qindex = alloc_table[j+b];
00802 bits = ff_mpa_quant_bits[qindex];
00803 if (bits < 0) {
00804 int v2;
00805
00806 v = get_bits(&s->gb, -bits);
00807 v2 = division_tabs[qindex][v];
00808 steps = ff_mpa_quant_steps[qindex];
00809
00810 s->sb_samples[ch][k * 12 + l + 0][i] =
00811 l2_unscale_group(steps, v2 & 15, scale);
00812 s->sb_samples[ch][k * 12 + l + 1][i] =
00813 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
00814 s->sb_samples[ch][k * 12 + l + 2][i] =
00815 l2_unscale_group(steps, v2 >> 8 , scale);
00816 } else {
00817 for(m=0;m<3;m++) {
00818 v = get_bits(&s->gb, bits);
00819 v = l1_unscale(bits - 1, v, scale);
00820 s->sb_samples[ch][k * 12 + l + m][i] = v;
00821 }
00822 }
00823 } else {
00824 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
00825 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
00826 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
00827 }
00828 }
00829
00830 j += 1 << bit_alloc_bits;
00831 }
00832
00833 for(i=bound;i<sblimit;i++) {
00834 bit_alloc_bits = alloc_table[j];
00835 b = bit_alloc[0][i];
00836 if (b) {
00837 int mant, scale0, scale1;
00838 scale0 = scale_factors[0][i][k];
00839 scale1 = scale_factors[1][i][k];
00840 qindex = alloc_table[j+b];
00841 bits = ff_mpa_quant_bits[qindex];
00842 if (bits < 0) {
00843
00844 v = get_bits(&s->gb, -bits);
00845 steps = ff_mpa_quant_steps[qindex];
00846 mant = v % steps;
00847 v = v / steps;
00848 s->sb_samples[0][k * 12 + l + 0][i] =
00849 l2_unscale_group(steps, mant, scale0);
00850 s->sb_samples[1][k * 12 + l + 0][i] =
00851 l2_unscale_group(steps, mant, scale1);
00852 mant = v % steps;
00853 v = v / steps;
00854 s->sb_samples[0][k * 12 + l + 1][i] =
00855 l2_unscale_group(steps, mant, scale0);
00856 s->sb_samples[1][k * 12 + l + 1][i] =
00857 l2_unscale_group(steps, mant, scale1);
00858 s->sb_samples[0][k * 12 + l + 2][i] =
00859 l2_unscale_group(steps, v, scale0);
00860 s->sb_samples[1][k * 12 + l + 2][i] =
00861 l2_unscale_group(steps, v, scale1);
00862 } else {
00863 for(m=0;m<3;m++) {
00864 mant = get_bits(&s->gb, bits);
00865 s->sb_samples[0][k * 12 + l + m][i] =
00866 l1_unscale(bits - 1, mant, scale0);
00867 s->sb_samples[1][k * 12 + l + m][i] =
00868 l1_unscale(bits - 1, mant, scale1);
00869 }
00870 }
00871 } else {
00872 s->sb_samples[0][k * 12 + l + 0][i] = 0;
00873 s->sb_samples[0][k * 12 + l + 1][i] = 0;
00874 s->sb_samples[0][k * 12 + l + 2][i] = 0;
00875 s->sb_samples[1][k * 12 + l + 0][i] = 0;
00876 s->sb_samples[1][k * 12 + l + 1][i] = 0;
00877 s->sb_samples[1][k * 12 + l + 2][i] = 0;
00878 }
00879
00880 j += 1 << bit_alloc_bits;
00881 }
00882
00883 for(i=sblimit;i<SBLIMIT;i++) {
00884 for(ch=0;ch<s->nb_channels;ch++) {
00885 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
00886 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
00887 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
00888 }
00889 }
00890 }
00891 }
00892 return 3 * 12;
00893 }
00894
00895 #define SPLIT(dst,sf,n)\
00896 if(n==3){\
00897 int m= (sf*171)>>9;\
00898 dst= sf - 3*m;\
00899 sf=m;\
00900 }else if(n==4){\
00901 dst= sf&3;\
00902 sf>>=2;\
00903 }else if(n==5){\
00904 int m= (sf*205)>>10;\
00905 dst= sf - 5*m;\
00906 sf=m;\
00907 }else if(n==6){\
00908 int m= (sf*171)>>10;\
00909 dst= sf - 6*m;\
00910 sf=m;\
00911 }else{\
00912 dst=0;\
00913 }
00914
00915 static av_always_inline void lsf_sf_expand(int *slen,
00916 int sf, int n1, int n2, int n3)
00917 {
00918 SPLIT(slen[3], sf, n3)
00919 SPLIT(slen[2], sf, n2)
00920 SPLIT(slen[1], sf, n1)
00921 slen[0] = sf;
00922 }
00923
00924 static void exponents_from_scale_factors(MPADecodeContext *s,
00925 GranuleDef *g,
00926 int16_t *exponents)
00927 {
00928 const uint8_t *bstab, *pretab;
00929 int len, i, j, k, l, v0, shift, gain, gains[3];
00930 int16_t *exp_ptr;
00931
00932 exp_ptr = exponents;
00933 gain = g->global_gain - 210;
00934 shift = g->scalefac_scale + 1;
00935
00936 bstab = band_size_long[s->sample_rate_index];
00937 pretab = mpa_pretab[g->preflag];
00938 for(i=0;i<g->long_end;i++) {
00939 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
00940 len = bstab[i];
00941 for(j=len;j>0;j--)
00942 *exp_ptr++ = v0;
00943 }
00944
00945 if (g->short_start < 13) {
00946 bstab = band_size_short[s->sample_rate_index];
00947 gains[0] = gain - (g->subblock_gain[0] << 3);
00948 gains[1] = gain - (g->subblock_gain[1] << 3);
00949 gains[2] = gain - (g->subblock_gain[2] << 3);
00950 k = g->long_end;
00951 for(i=g->short_start;i<13;i++) {
00952 len = bstab[i];
00953 for(l=0;l<3;l++) {
00954 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
00955 for(j=len;j>0;j--)
00956 *exp_ptr++ = v0;
00957 }
00958 }
00959 }
00960 }
00961
00962
00963 static inline int get_bitsz(GetBitContext *s, int n)
00964 {
00965 if (n == 0)
00966 return 0;
00967 else
00968 return get_bits(s, n);
00969 }
00970
00971
00972 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
00973 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
00974 s->gb= s->in_gb;
00975 s->in_gb.buffer=NULL;
00976 assert((get_bits_count(&s->gb) & 7) == 0);
00977 skip_bits_long(&s->gb, *pos - *end_pos);
00978 *end_pos2=
00979 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
00980 *pos= get_bits_count(&s->gb);
00981 }
00982 }
00983
00984
00985
00986
00987
00988
00989
00990 #if CONFIG_FLOAT
00991 #define READ_FLIP_SIGN(dst,src)\
00992 v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
00993 AV_WN32A(dst, v);
00994 #else
00995 #define READ_FLIP_SIGN(dst,src)\
00996 v= -get_bits1(&s->gb);\
00997 *(dst) = (*(src) ^ v) - v;
00998 #endif
00999
01000 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
01001 int16_t *exponents, int end_pos2)
01002 {
01003 int s_index;
01004 int i;
01005 int last_pos, bits_left;
01006 VLC *vlc;
01007 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
01008
01009
01010 s_index = 0;
01011 for(i=0;i<3;i++) {
01012 int j, k, l, linbits;
01013 j = g->region_size[i];
01014 if (j == 0)
01015 continue;
01016
01017 k = g->table_select[i];
01018 l = mpa_huff_data[k][0];
01019 linbits = mpa_huff_data[k][1];
01020 vlc = &huff_vlc[l];
01021
01022 if(!l){
01023 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
01024 s_index += 2*j;
01025 continue;
01026 }
01027
01028
01029 for(;j>0;j--) {
01030 int exponent, x, y;
01031 int v;
01032 int pos= get_bits_count(&s->gb);
01033
01034 if (pos >= end_pos){
01035
01036 switch_buffer(s, &pos, &end_pos, &end_pos2);
01037
01038 if(pos >= end_pos)
01039 break;
01040 }
01041 y = get_vlc2(&s->gb, vlc->table, 7, 3);
01042
01043 if(!y){
01044 g->sb_hybrid[s_index ] =
01045 g->sb_hybrid[s_index+1] = 0;
01046 s_index += 2;
01047 continue;
01048 }
01049
01050 exponent= exponents[s_index];
01051
01052 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
01053 i, g->region_size[i] - j, x, y, exponent);
01054 if(y&16){
01055 x = y >> 5;
01056 y = y & 0x0f;
01057 if (x < 15){
01058 READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
01059 }else{
01060 x += get_bitsz(&s->gb, linbits);
01061 v = l3_unscale(x, exponent);
01062 if (get_bits1(&s->gb))
01063 v = -v;
01064 g->sb_hybrid[s_index] = v;
01065 }
01066 if (y < 15){
01067 READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
01068 }else{
01069 y += get_bitsz(&s->gb, linbits);
01070 v = l3_unscale(y, exponent);
01071 if (get_bits1(&s->gb))
01072 v = -v;
01073 g->sb_hybrid[s_index+1] = v;
01074 }
01075 }else{
01076 x = y >> 5;
01077 y = y & 0x0f;
01078 x += y;
01079 if (x < 15){
01080 READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
01081 }else{
01082 x += get_bitsz(&s->gb, linbits);
01083 v = l3_unscale(x, exponent);
01084 if (get_bits1(&s->gb))
01085 v = -v;
01086 g->sb_hybrid[s_index+!!y] = v;
01087 }
01088 g->sb_hybrid[s_index+ !y] = 0;
01089 }
01090 s_index+=2;
01091 }
01092 }
01093
01094
01095 vlc = &huff_quad_vlc[g->count1table_select];
01096 last_pos=0;
01097 while (s_index <= 572) {
01098 int pos, code;
01099 pos = get_bits_count(&s->gb);
01100 if (pos >= end_pos) {
01101 if (pos > end_pos2 && last_pos){
01102
01103
01104 s_index -= 4;
01105 skip_bits_long(&s->gb, last_pos - pos);
01106 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
01107 if(s->error_recognition >= FF_ER_COMPLIANT)
01108 s_index=0;
01109 break;
01110 }
01111
01112 switch_buffer(s, &pos, &end_pos, &end_pos2);
01113
01114 if(pos >= end_pos)
01115 break;
01116 }
01117 last_pos= pos;
01118
01119 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
01120 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
01121 g->sb_hybrid[s_index+0]=
01122 g->sb_hybrid[s_index+1]=
01123 g->sb_hybrid[s_index+2]=
01124 g->sb_hybrid[s_index+3]= 0;
01125 while(code){
01126 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
01127 int v;
01128 int pos= s_index+idxtab[code];
01129 code ^= 8>>idxtab[code];
01130 READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
01131 }
01132 s_index+=4;
01133 }
01134
01135 bits_left = end_pos2 - get_bits_count(&s->gb);
01136
01137 if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
01138 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
01139 s_index=0;
01140 }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
01141 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
01142 s_index=0;
01143 }
01144 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
01145 skip_bits_long(&s->gb, bits_left);
01146
01147 i= get_bits_count(&s->gb);
01148 switch_buffer(s, &i, &end_pos, &end_pos2);
01149
01150 return 0;
01151 }
01152
01153
01154
01155
01156 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
01157 {
01158 int i, j, len;
01159 INTFLOAT *ptr, *dst, *ptr1;
01160 INTFLOAT tmp[576];
01161
01162 if (g->block_type != 2)
01163 return;
01164
01165 if (g->switch_point) {
01166 if (s->sample_rate_index != 8) {
01167 ptr = g->sb_hybrid + 36;
01168 } else {
01169 ptr = g->sb_hybrid + 48;
01170 }
01171 } else {
01172 ptr = g->sb_hybrid;
01173 }
01174
01175 for(i=g->short_start;i<13;i++) {
01176 len = band_size_short[s->sample_rate_index][i];
01177 ptr1 = ptr;
01178 dst = tmp;
01179 for(j=len;j>0;j--) {
01180 *dst++ = ptr[0*len];
01181 *dst++ = ptr[1*len];
01182 *dst++ = ptr[2*len];
01183 ptr++;
01184 }
01185 ptr+=2*len;
01186 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
01187 }
01188 }
01189
01190 #define ISQRT2 FIXR(0.70710678118654752440)
01191
01192 static void compute_stereo(MPADecodeContext *s,
01193 GranuleDef *g0, GranuleDef *g1)
01194 {
01195 int i, j, k, l;
01196 int sf_max, sf, len, non_zero_found;
01197 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
01198 int non_zero_found_short[3];
01199
01200
01201 if (s->mode_ext & MODE_EXT_I_STEREO) {
01202 if (!s->lsf) {
01203 is_tab = is_table;
01204 sf_max = 7;
01205 } else {
01206 is_tab = is_table_lsf[g1->scalefac_compress & 1];
01207 sf_max = 16;
01208 }
01209
01210 tab0 = g0->sb_hybrid + 576;
01211 tab1 = g1->sb_hybrid + 576;
01212
01213 non_zero_found_short[0] = 0;
01214 non_zero_found_short[1] = 0;
01215 non_zero_found_short[2] = 0;
01216 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
01217 for(i = 12;i >= g1->short_start;i--) {
01218
01219 if (i != 11)
01220 k -= 3;
01221 len = band_size_short[s->sample_rate_index][i];
01222 for(l=2;l>=0;l--) {
01223 tab0 -= len;
01224 tab1 -= len;
01225 if (!non_zero_found_short[l]) {
01226
01227 for(j=0;j<len;j++) {
01228 if (tab1[j] != 0) {
01229 non_zero_found_short[l] = 1;
01230 goto found1;
01231 }
01232 }
01233 sf = g1->scale_factors[k + l];
01234 if (sf >= sf_max)
01235 goto found1;
01236
01237 v1 = is_tab[0][sf];
01238 v2 = is_tab[1][sf];
01239 for(j=0;j<len;j++) {
01240 tmp0 = tab0[j];
01241 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
01242 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
01243 }
01244 } else {
01245 found1:
01246 if (s->mode_ext & MODE_EXT_MS_STEREO) {
01247
01248
01249 for(j=0;j<len;j++) {
01250 tmp0 = tab0[j];
01251 tmp1 = tab1[j];
01252 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
01253 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
01254 }
01255 }
01256 }
01257 }
01258 }
01259
01260 non_zero_found = non_zero_found_short[0] |
01261 non_zero_found_short[1] |
01262 non_zero_found_short[2];
01263
01264 for(i = g1->long_end - 1;i >= 0;i--) {
01265 len = band_size_long[s->sample_rate_index][i];
01266 tab0 -= len;
01267 tab1 -= len;
01268
01269 if (!non_zero_found) {
01270 for(j=0;j<len;j++) {
01271 if (tab1[j] != 0) {
01272 non_zero_found = 1;
01273 goto found2;
01274 }
01275 }
01276
01277 k = (i == 21) ? 20 : i;
01278 sf = g1->scale_factors[k];
01279 if (sf >= sf_max)
01280 goto found2;
01281 v1 = is_tab[0][sf];
01282 v2 = is_tab[1][sf];
01283 for(j=0;j<len;j++) {
01284 tmp0 = tab0[j];
01285 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
01286 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
01287 }
01288 } else {
01289 found2:
01290 if (s->mode_ext & MODE_EXT_MS_STEREO) {
01291
01292
01293 for(j=0;j<len;j++) {
01294 tmp0 = tab0[j];
01295 tmp1 = tab1[j];
01296 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
01297 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
01298 }
01299 }
01300 }
01301 }
01302 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
01303
01304
01305
01306 tab0 = g0->sb_hybrid;
01307 tab1 = g1->sb_hybrid;
01308 for(i=0;i<576;i++) {
01309 tmp0 = tab0[i];
01310 tmp1 = tab1[i];
01311 tab0[i] = tmp0 + tmp1;
01312 tab1[i] = tmp0 - tmp1;
01313 }
01314 }
01315 }
01316
01317 #if CONFIG_FLOAT
01318 #define AA(j) do { \
01319 float tmp0 = ptr[-1-j]; \
01320 float tmp1 = ptr[ j]; \
01321 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
01322 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
01323 } while (0)
01324 #else
01325 #define AA(j) do { \
01326 int tmp0 = ptr[-1-j]; \
01327 int tmp1 = ptr[ j]; \
01328 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
01329 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa_table[j][2])); \
01330 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa_table[j][3])); \
01331 } while (0)
01332 #endif
01333
01334 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
01335 {
01336 INTFLOAT *ptr;
01337 int n, i;
01338
01339
01340 if (g->block_type == 2) {
01341 if (!g->switch_point)
01342 return;
01343
01344 n = 1;
01345 } else {
01346 n = SBLIMIT - 1;
01347 }
01348
01349 ptr = g->sb_hybrid + 18;
01350 for(i = n;i > 0;i--) {
01351 AA(0);
01352 AA(1);
01353 AA(2);
01354 AA(3);
01355 AA(4);
01356 AA(5);
01357 AA(6);
01358 AA(7);
01359
01360 ptr += 18;
01361 }
01362 }
01363
01364 static void compute_imdct(MPADecodeContext *s,
01365 GranuleDef *g,
01366 INTFLOAT *sb_samples,
01367 INTFLOAT *mdct_buf)
01368 {
01369 INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
01370 INTFLOAT out2[12];
01371 int i, j, mdct_long_end, sblimit;
01372
01373
01374 ptr = g->sb_hybrid + 576;
01375 ptr1 = g->sb_hybrid + 2 * 18;
01376 while (ptr >= ptr1) {
01377 int32_t *p;
01378 ptr -= 6;
01379 p= (int32_t*)ptr;
01380 if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
01381 break;
01382 }
01383 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
01384
01385 if (g->block_type == 2) {
01386
01387 if (g->switch_point)
01388 mdct_long_end = 2;
01389 else
01390 mdct_long_end = 0;
01391 } else {
01392 mdct_long_end = sblimit;
01393 }
01394
01395 buf = mdct_buf;
01396 ptr = g->sb_hybrid;
01397 for(j=0;j<mdct_long_end;j++) {
01398
01399 out_ptr = sb_samples + j;
01400
01401 if (g->switch_point && j < 2)
01402 win1 = mdct_win[0];
01403 else
01404 win1 = mdct_win[g->block_type];
01405
01406 win = win1 + ((4 * 36) & -(j & 1));
01407 imdct36(out_ptr, buf, ptr, win);
01408 out_ptr += 18*SBLIMIT;
01409 ptr += 18;
01410 buf += 18;
01411 }
01412 for(j=mdct_long_end;j<sblimit;j++) {
01413
01414 win = mdct_win[2] + ((4 * 36) & -(j & 1));
01415 out_ptr = sb_samples + j;
01416
01417 for(i=0; i<6; i++){
01418 *out_ptr = buf[i];
01419 out_ptr += SBLIMIT;
01420 }
01421 imdct12(out2, ptr + 0);
01422 for(i=0;i<6;i++) {
01423 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
01424 buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
01425 out_ptr += SBLIMIT;
01426 }
01427 imdct12(out2, ptr + 1);
01428 for(i=0;i<6;i++) {
01429 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
01430 buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
01431 out_ptr += SBLIMIT;
01432 }
01433 imdct12(out2, ptr + 2);
01434 for(i=0;i<6;i++) {
01435 buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
01436 buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
01437 buf[i + 6*2] = 0;
01438 }
01439 ptr += 18;
01440 buf += 18;
01441 }
01442
01443 for(j=sblimit;j<SBLIMIT;j++) {
01444
01445 out_ptr = sb_samples + j;
01446 for(i=0;i<18;i++) {
01447 *out_ptr = buf[i];
01448 buf[i] = 0;
01449 out_ptr += SBLIMIT;
01450 }
01451 buf += 18;
01452 }
01453 }
01454
01455
01456 static int mp_decode_layer3(MPADecodeContext *s)
01457 {
01458 int nb_granules, main_data_begin;
01459 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
01460 GranuleDef *g;
01461 int16_t exponents[576];
01462
01463
01464 if (s->lsf) {
01465 main_data_begin = get_bits(&s->gb, 8);
01466 skip_bits(&s->gb, s->nb_channels);
01467 nb_granules = 1;
01468 } else {
01469 main_data_begin = get_bits(&s->gb, 9);
01470 if (s->nb_channels == 2)
01471 skip_bits(&s->gb, 3);
01472 else
01473 skip_bits(&s->gb, 5);
01474 nb_granules = 2;
01475 for(ch=0;ch<s->nb_channels;ch++) {
01476 s->granules[ch][0].scfsi = 0;
01477 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
01478 }
01479 }
01480
01481 for(gr=0;gr<nb_granules;gr++) {
01482 for(ch=0;ch<s->nb_channels;ch++) {
01483 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
01484 g = &s->granules[ch][gr];
01485 g->part2_3_length = get_bits(&s->gb, 12);
01486 g->big_values = get_bits(&s->gb, 9);
01487 if(g->big_values > 288){
01488 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
01489 return -1;
01490 }
01491
01492 g->global_gain = get_bits(&s->gb, 8);
01493
01494
01495 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
01496 MODE_EXT_MS_STEREO)
01497 g->global_gain -= 2;
01498 if (s->lsf)
01499 g->scalefac_compress = get_bits(&s->gb, 9);
01500 else
01501 g->scalefac_compress = get_bits(&s->gb, 4);
01502 blocksplit_flag = get_bits1(&s->gb);
01503 if (blocksplit_flag) {
01504 g->block_type = get_bits(&s->gb, 2);
01505 if (g->block_type == 0){
01506 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
01507 return -1;
01508 }
01509 g->switch_point = get_bits1(&s->gb);
01510 for(i=0;i<2;i++)
01511 g->table_select[i] = get_bits(&s->gb, 5);
01512 for(i=0;i<3;i++)
01513 g->subblock_gain[i] = get_bits(&s->gb, 3);
01514 ff_init_short_region(s, g);
01515 } else {
01516 int region_address1, region_address2;
01517 g->block_type = 0;
01518 g->switch_point = 0;
01519 for(i=0;i<3;i++)
01520 g->table_select[i] = get_bits(&s->gb, 5);
01521
01522 region_address1 = get_bits(&s->gb, 4);
01523 region_address2 = get_bits(&s->gb, 3);
01524 av_dlog(s->avctx, "region1=%d region2=%d\n",
01525 region_address1, region_address2);
01526 ff_init_long_region(s, g, region_address1, region_address2);
01527 }
01528 ff_region_offset2size(g);
01529 ff_compute_band_indexes(s, g);
01530
01531 g->preflag = 0;
01532 if (!s->lsf)
01533 g->preflag = get_bits1(&s->gb);
01534 g->scalefac_scale = get_bits1(&s->gb);
01535 g->count1table_select = get_bits1(&s->gb);
01536 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
01537 g->block_type, g->switch_point);
01538 }
01539 }
01540
01541 if (!s->adu_mode) {
01542 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
01543 assert((get_bits_count(&s->gb) & 7) == 0);
01544
01545 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
01546
01547
01548 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
01549 s->in_gb= s->gb;
01550 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
01551 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
01552 }
01553
01554 for(gr=0;gr<nb_granules;gr++) {
01555 for(ch=0;ch<s->nb_channels;ch++) {
01556 g = &s->granules[ch][gr];
01557 if(get_bits_count(&s->gb)<0){
01558 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
01559 main_data_begin, s->last_buf_size, gr);
01560 skip_bits_long(&s->gb, g->part2_3_length);
01561 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
01562 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
01563 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
01564 s->gb= s->in_gb;
01565 s->in_gb.buffer=NULL;
01566 }
01567 continue;
01568 }
01569
01570 bits_pos = get_bits_count(&s->gb);
01571
01572 if (!s->lsf) {
01573 uint8_t *sc;
01574 int slen, slen1, slen2;
01575
01576
01577 slen1 = slen_table[0][g->scalefac_compress];
01578 slen2 = slen_table[1][g->scalefac_compress];
01579 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
01580 if (g->block_type == 2) {
01581 n = g->switch_point ? 17 : 18;
01582 j = 0;
01583 if(slen1){
01584 for(i=0;i<n;i++)
01585 g->scale_factors[j++] = get_bits(&s->gb, slen1);
01586 }else{
01587 for(i=0;i<n;i++)
01588 g->scale_factors[j++] = 0;
01589 }
01590 if(slen2){
01591 for(i=0;i<18;i++)
01592 g->scale_factors[j++] = get_bits(&s->gb, slen2);
01593 for(i=0;i<3;i++)
01594 g->scale_factors[j++] = 0;
01595 }else{
01596 for(i=0;i<21;i++)
01597 g->scale_factors[j++] = 0;
01598 }
01599 } else {
01600 sc = s->granules[ch][0].scale_factors;
01601 j = 0;
01602 for(k=0;k<4;k++) {
01603 n = (k == 0 ? 6 : 5);
01604 if ((g->scfsi & (0x8 >> k)) == 0) {
01605 slen = (k < 2) ? slen1 : slen2;
01606 if(slen){
01607 for(i=0;i<n;i++)
01608 g->scale_factors[j++] = get_bits(&s->gb, slen);
01609 }else{
01610 for(i=0;i<n;i++)
01611 g->scale_factors[j++] = 0;
01612 }
01613 } else {
01614
01615 for(i=0;i<n;i++) {
01616 g->scale_factors[j] = sc[j];
01617 j++;
01618 }
01619 }
01620 }
01621 g->scale_factors[j++] = 0;
01622 }
01623 } else {
01624 int tindex, tindex2, slen[4], sl, sf;
01625
01626
01627 if (g->block_type == 2) {
01628 tindex = g->switch_point ? 2 : 1;
01629 } else {
01630 tindex = 0;
01631 }
01632 sf = g->scalefac_compress;
01633 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
01634
01635 sf >>= 1;
01636 if (sf < 180) {
01637 lsf_sf_expand(slen, sf, 6, 6, 0);
01638 tindex2 = 3;
01639 } else if (sf < 244) {
01640 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
01641 tindex2 = 4;
01642 } else {
01643 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
01644 tindex2 = 5;
01645 }
01646 } else {
01647
01648 if (sf < 400) {
01649 lsf_sf_expand(slen, sf, 5, 4, 4);
01650 tindex2 = 0;
01651 } else if (sf < 500) {
01652 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
01653 tindex2 = 1;
01654 } else {
01655 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
01656 tindex2 = 2;
01657 g->preflag = 1;
01658 }
01659 }
01660
01661 j = 0;
01662 for(k=0;k<4;k++) {
01663 n = lsf_nsf_table[tindex2][tindex][k];
01664 sl = slen[k];
01665 if(sl){
01666 for(i=0;i<n;i++)
01667 g->scale_factors[j++] = get_bits(&s->gb, sl);
01668 }else{
01669 for(i=0;i<n;i++)
01670 g->scale_factors[j++] = 0;
01671 }
01672 }
01673
01674 for(;j<40;j++)
01675 g->scale_factors[j] = 0;
01676 }
01677
01678 exponents_from_scale_factors(s, g, exponents);
01679
01680
01681 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
01682 }
01683
01684 if (s->nb_channels == 2)
01685 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
01686
01687 for(ch=0;ch<s->nb_channels;ch++) {
01688 g = &s->granules[ch][gr];
01689
01690 reorder_block(s, g);
01691 compute_antialias(s, g);
01692 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
01693 }
01694 }
01695 if(get_bits_count(&s->gb)<0)
01696 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
01697 return nb_granules * 18;
01698 }
01699
01700 static int mp_decode_frame(MPADecodeContext *s,
01701 OUT_INT *samples, const uint8_t *buf, int buf_size)
01702 {
01703 int i, nb_frames, ch;
01704 OUT_INT *samples_ptr;
01705
01706 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
01707
01708
01709 if (s->error_protection)
01710 skip_bits(&s->gb, 16);
01711
01712 switch(s->layer) {
01713 case 1:
01714 s->avctx->frame_size = 384;
01715 nb_frames = mp_decode_layer1(s);
01716 break;
01717 case 2:
01718 s->avctx->frame_size = 1152;
01719 nb_frames = mp_decode_layer2(s);
01720 break;
01721 case 3:
01722 s->avctx->frame_size = s->lsf ? 576 : 1152;
01723 default:
01724 nb_frames = mp_decode_layer3(s);
01725
01726 s->last_buf_size=0;
01727 if(s->in_gb.buffer){
01728 align_get_bits(&s->gb);
01729 i= get_bits_left(&s->gb)>>3;
01730 if(i >= 0 && i <= BACKSTEP_SIZE){
01731 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
01732 s->last_buf_size=i;
01733 }else
01734 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
01735 s->gb= s->in_gb;
01736 s->in_gb.buffer= NULL;
01737 }
01738
01739 align_get_bits(&s->gb);
01740 assert((get_bits_count(&s->gb) & 7) == 0);
01741 i= get_bits_left(&s->gb)>>3;
01742
01743 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
01744 if(i<0)
01745 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
01746 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
01747 }
01748 assert(i <= buf_size - HEADER_SIZE && i>= 0);
01749 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
01750 s->last_buf_size += i;
01751
01752 break;
01753 }
01754
01755
01756 for(ch=0;ch<s->nb_channels;ch++) {
01757 samples_ptr = samples + ch;
01758 for(i=0;i<nb_frames;i++) {
01759 RENAME(ff_mpa_synth_filter)(
01760 &s->mpadsp,
01761 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
01762 RENAME(ff_mpa_synth_window), &s->dither_state,
01763 samples_ptr, s->nb_channels,
01764 s->sb_samples[ch][i]);
01765 samples_ptr += 32 * s->nb_channels;
01766 }
01767 }
01768
01769 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
01770 }
01771
01772 static int decode_frame(AVCodecContext * avctx,
01773 void *data, int *data_size,
01774 AVPacket *avpkt)
01775 {
01776 const uint8_t *buf = avpkt->data;
01777 int buf_size = avpkt->size;
01778 MPADecodeContext *s = avctx->priv_data;
01779 uint32_t header;
01780 int out_size;
01781 OUT_INT *out_samples = data;
01782
01783 if(buf_size < HEADER_SIZE)
01784 return -1;
01785
01786 header = AV_RB32(buf);
01787 if(ff_mpa_check_header(header) < 0){
01788 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
01789 return -1;
01790 }
01791
01792 if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
01793
01794 s->frame_size = -1;
01795 return -1;
01796 }
01797
01798 avctx->channels = s->nb_channels;
01799 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
01800 if (!avctx->bit_rate)
01801 avctx->bit_rate = s->bit_rate;
01802 avctx->sub_id = s->layer;
01803
01804 if (*data_size < avctx->frame_size * avctx->channels * sizeof(OUT_INT))
01805 return AVERROR(EINVAL);
01806 *data_size = 0;
01807
01808 if(s->frame_size<=0 || s->frame_size > buf_size){
01809 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
01810 return -1;
01811 }else if(s->frame_size < buf_size){
01812 av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
01813 buf_size= s->frame_size;
01814 }
01815
01816 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
01817 if(out_size>=0){
01818 *data_size = out_size;
01819 avctx->sample_rate = s->sample_rate;
01820
01821 }else
01822 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n");
01823 s->frame_size = 0;
01824 return buf_size;
01825 }
01826
01827 static void flush(AVCodecContext *avctx){
01828 MPADecodeContext *s = avctx->priv_data;
01829 memset(s->synth_buf, 0, sizeof(s->synth_buf));
01830 s->last_buf_size= 0;
01831 }
01832
01833 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
01834 static int decode_frame_adu(AVCodecContext * avctx,
01835 void *data, int *data_size,
01836 AVPacket *avpkt)
01837 {
01838 const uint8_t *buf = avpkt->data;
01839 int buf_size = avpkt->size;
01840 MPADecodeContext *s = avctx->priv_data;
01841 uint32_t header;
01842 int len, out_size;
01843 OUT_INT *out_samples = data;
01844
01845 len = buf_size;
01846
01847
01848 if (buf_size < HEADER_SIZE) {
01849 *data_size = 0;
01850 return buf_size;
01851 }
01852
01853
01854 if (len > MPA_MAX_CODED_FRAME_SIZE)
01855 len = MPA_MAX_CODED_FRAME_SIZE;
01856
01857
01858 header = AV_RB32(buf) | 0xffe00000;
01859
01860 if (ff_mpa_check_header(header) < 0) {
01861 *data_size = 0;
01862 return buf_size;
01863 }
01864
01865 ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
01866
01867 avctx->sample_rate = s->sample_rate;
01868 avctx->channels = s->nb_channels;
01869 if (!avctx->bit_rate)
01870 avctx->bit_rate = s->bit_rate;
01871 avctx->sub_id = s->layer;
01872
01873 if (*data_size < avctx->frame_size * avctx->channels * sizeof(OUT_INT))
01874 return AVERROR(EINVAL);
01875
01876 s->frame_size = len;
01877
01878 if (avctx->parse_only) {
01879 out_size = buf_size;
01880 } else {
01881 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
01882 }
01883
01884 *data_size = out_size;
01885 return buf_size;
01886 }
01887 #endif
01888
01889 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
01890
01894 typedef struct MP3On4DecodeContext {
01895 int frames;
01896 int syncword;
01897 const uint8_t *coff;
01898 MPADecodeContext *mp3decctx[5];
01899 } MP3On4DecodeContext;
01900
01901 #include "mpeg4audio.h"
01902
01903
01904 static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5};
01905
01906 static const uint8_t chan_offset[8][5] = {
01907 {0},
01908 {0},
01909 {0},
01910 {2,0},
01911 {2,0,3},
01912 {4,0,2},
01913 {4,0,2,5},
01914 {4,0,2,6,5},
01915 };
01916
01917
01918 static int decode_init_mp3on4(AVCodecContext * avctx)
01919 {
01920 MP3On4DecodeContext *s = avctx->priv_data;
01921 MPEG4AudioConfig cfg;
01922 int i;
01923
01924 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
01925 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
01926 return -1;
01927 }
01928
01929 ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
01930 if (!cfg.chan_config || cfg.chan_config > 7) {
01931 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
01932 return -1;
01933 }
01934 s->frames = mp3Frames[cfg.chan_config];
01935 s->coff = chan_offset[cfg.chan_config];
01936 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
01937
01938 if (cfg.sample_rate < 16000)
01939 s->syncword = 0xffe00000;
01940 else
01941 s->syncword = 0xfff00000;
01942
01943
01944
01945
01946
01947
01948
01949 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
01950
01951 avctx->priv_data = s->mp3decctx[0];
01952 decode_init(avctx);
01953
01954 avctx->priv_data = s;
01955 s->mp3decctx[0]->adu_mode = 1;
01956
01957
01958
01959
01960 for (i = 1; i < s->frames; i++) {
01961 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
01962 s->mp3decctx[i]->adu_mode = 1;
01963 s->mp3decctx[i]->avctx = avctx;
01964 }
01965
01966 return 0;
01967 }
01968
01969
01970 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
01971 {
01972 MP3On4DecodeContext *s = avctx->priv_data;
01973 int i;
01974
01975 for (i = 0; i < s->frames; i++)
01976 av_free(s->mp3decctx[i]);
01977
01978 return 0;
01979 }
01980
01981
01982 static int decode_frame_mp3on4(AVCodecContext * avctx,
01983 void *data, int *data_size,
01984 AVPacket *avpkt)
01985 {
01986 const uint8_t *buf = avpkt->data;
01987 int buf_size = avpkt->size;
01988 MP3On4DecodeContext *s = avctx->priv_data;
01989 MPADecodeContext *m;
01990 int fsize, len = buf_size, out_size = 0;
01991 uint32_t header;
01992 OUT_INT *out_samples = data;
01993 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
01994 OUT_INT *outptr, *bp;
01995 int fr, j, n;
01996
01997 if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
01998 return -1;
01999
02000 *data_size = 0;
02001
02002 if (buf_size < HEADER_SIZE)
02003 return -1;
02004
02005
02006 outptr = s->frames == 1 ? out_samples : decoded_buf;
02007
02008 avctx->bit_rate = 0;
02009
02010 for (fr = 0; fr < s->frames; fr++) {
02011 fsize = AV_RB16(buf) >> 4;
02012 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
02013 m = s->mp3decctx[fr];
02014 assert (m != NULL);
02015
02016 header = (AV_RB32(buf) & 0x000fffff) | s->syncword;
02017
02018 if (ff_mpa_check_header(header) < 0)
02019 break;
02020
02021 ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
02022 out_size += mp_decode_frame(m, outptr, buf, fsize);
02023 buf += fsize;
02024 len -= fsize;
02025
02026 if(s->frames > 1) {
02027 n = m->avctx->frame_size*m->nb_channels;
02028
02029 bp = out_samples + s->coff[fr];
02030 if(m->nb_channels == 1) {
02031 for(j = 0; j < n; j++) {
02032 *bp = decoded_buf[j];
02033 bp += avctx->channels;
02034 }
02035 } else {
02036 for(j = 0; j < n; j++) {
02037 bp[0] = decoded_buf[j++];
02038 bp[1] = decoded_buf[j];
02039 bp += avctx->channels;
02040 }
02041 }
02042 }
02043 avctx->bit_rate += m->bit_rate;
02044 }
02045
02046
02047 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
02048
02049 *data_size = out_size;
02050 return buf_size;
02051 }
02052 #endif
02053
02054 #if !CONFIG_FLOAT
02055 #if CONFIG_MP1_DECODER
02056 AVCodec ff_mp1_decoder =
02057 {
02058 "mp1",
02059 AVMEDIA_TYPE_AUDIO,
02060 CODEC_ID_MP1,
02061 sizeof(MPADecodeContext),
02062 decode_init,
02063 NULL,
02064 NULL,
02065 decode_frame,
02066 CODEC_CAP_PARSE_ONLY,
02067 .flush= flush,
02068 .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
02069 };
02070 #endif
02071 #if CONFIG_MP2_DECODER
02072 AVCodec ff_mp2_decoder =
02073 {
02074 "mp2",
02075 AVMEDIA_TYPE_AUDIO,
02076 CODEC_ID_MP2,
02077 sizeof(MPADecodeContext),
02078 decode_init,
02079 NULL,
02080 NULL,
02081 decode_frame,
02082 CODEC_CAP_PARSE_ONLY,
02083 .flush= flush,
02084 .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
02085 };
02086 #endif
02087 #if CONFIG_MP3_DECODER
02088 AVCodec ff_mp3_decoder =
02089 {
02090 "mp3",
02091 AVMEDIA_TYPE_AUDIO,
02092 CODEC_ID_MP3,
02093 sizeof(MPADecodeContext),
02094 decode_init,
02095 NULL,
02096 NULL,
02097 decode_frame,
02098 CODEC_CAP_PARSE_ONLY,
02099 .flush= flush,
02100 .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
02101 };
02102 #endif
02103 #if CONFIG_MP3ADU_DECODER
02104 AVCodec ff_mp3adu_decoder =
02105 {
02106 "mp3adu",
02107 AVMEDIA_TYPE_AUDIO,
02108 CODEC_ID_MP3ADU,
02109 sizeof(MPADecodeContext),
02110 decode_init,
02111 NULL,
02112 NULL,
02113 decode_frame_adu,
02114 CODEC_CAP_PARSE_ONLY,
02115 .flush= flush,
02116 .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
02117 };
02118 #endif
02119 #if CONFIG_MP3ON4_DECODER
02120 AVCodec ff_mp3on4_decoder =
02121 {
02122 "mp3on4",
02123 AVMEDIA_TYPE_AUDIO,
02124 CODEC_ID_MP3ON4,
02125 sizeof(MP3On4DecodeContext),
02126 decode_init_mp3on4,
02127 NULL,
02128 decode_close_mp3on4,
02129 decode_frame_mp3on4,
02130 .flush= flush,
02131 .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),
02132 };
02133 #endif
02134 #endif