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