00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdio.h>
00031 #include <stddef.h>
00032 #include <math.h>
00033 #include <string.h>
00034
00035 #include "avcodec.h"
00036 #include "ac3_parser.h"
00037 #include "bitstream.h"
00038 #include "crc.h"
00039 #include "dsputil.h"
00040 #include "random.h"
00041
00046 static const uint8_t rematrix_band_tab[5] = { 13, 25, 37, 61, 253 };
00047
00052 static float scale_factors[25];
00053
00055 static uint8_t exp_ungroup_tab[128][3];
00056
00057
00059 static float b1_mantissas[32][3];
00060 static float b2_mantissas[128][3];
00061 static float b3_mantissas[8];
00062 static float b4_mantissas[128][2];
00063 static float b5_mantissas[16];
00064
00069 static const uint8_t qntztab[16] = {
00070 0, 3, 5, 7, 11, 15,
00071 5, 6, 7, 8, 9, 10, 11, 12, 14, 16
00072 };
00073
00075 static float dynrng_tab[256];
00076
00078 static float dialnorm_tab[32];
00079
00081 #define LEVEL_MINUS_3DB 0.7071067811865476
00082 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
00083 #define LEVEL_MINUS_6DB 0.5000000000000000
00084 #define LEVEL_MINUS_9DB 0.3535533905932738
00085 #define LEVEL_ZERO 0.0000000000000000
00086 #define LEVEL_ONE 1.0000000000000000
00087
00088 static const float gain_levels[6] = {
00089 LEVEL_ZERO,
00090 LEVEL_ONE,
00091 LEVEL_MINUS_3DB,
00092 LEVEL_MINUS_4POINT5DB,
00093 LEVEL_MINUS_6DB,
00094 LEVEL_MINUS_9DB
00095 };
00096
00101 static const uint8_t clevs[4] = { 2, 3, 4, 3 };
00102
00107 static const uint8_t slevs[4] = { 2, 4, 0, 4 };
00108
00113 static const uint8_t ac3_default_coeffs[8][5][2] = {
00114 { { 1, 0 }, { 0, 1 }, },
00115 { { 2, 2 }, },
00116 { { 1, 0 }, { 0, 1 }, },
00117 { { 1, 0 }, { 3, 3 }, { 0, 1 }, },
00118 { { 1, 0 }, { 0, 1 }, { 4, 4 }, },
00119 { { 1, 0 }, { 3, 3 }, { 0, 1 }, { 5, 5 }, },
00120 { { 1, 0 }, { 0, 1 }, { 4, 0 }, { 0, 4 }, },
00121 { { 1, 0 }, { 3, 3 }, { 0, 1 }, { 4, 0 }, { 0, 4 }, },
00122 };
00123
00124
00125 #undef AC3_MAX_CHANNELS
00126 #define AC3_MAX_CHANNELS 7
00127 #define CPL_CH 0
00128
00129 #define AC3_OUTPUT_LFEON 8
00130
00131 typedef struct {
00132 int acmod;
00133 int dsurmod;
00134 int blksw[AC3_MAX_CHANNELS];
00135 int dithflag[AC3_MAX_CHANNELS];
00136 int dither_all;
00137 int cplinu;
00138 int chincpl[AC3_MAX_CHANNELS];
00139 int phsflginu;
00140 int cplbndstrc[18];
00141 int rematstr;
00142 int nrematbnd;
00143 int rematflg[4];
00144 int expstr[AC3_MAX_CHANNELS];
00145 int snroffst[AC3_MAX_CHANNELS];
00146 int fgain[AC3_MAX_CHANNELS];
00147 int deltbae[AC3_MAX_CHANNELS];
00148 int deltnseg[AC3_MAX_CHANNELS];
00149 uint8_t deltoffst[AC3_MAX_CHANNELS][8];
00150 uint8_t deltlen[AC3_MAX_CHANNELS][8];
00151 uint8_t deltba[AC3_MAX_CHANNELS][8];
00152
00153 int sampling_rate;
00154 int bit_rate;
00155 int frame_size;
00156
00157 int nchans;
00158 int nfchans;
00159 int lfeon;
00160 int lfe_ch;
00161 int output_mode;
00162 int out_channels;
00163
00164 float downmix_coeffs[AC3_MAX_CHANNELS][2];
00165 float dialnorm[2];
00166 float dynrng[2];
00167 float cplco[AC3_MAX_CHANNELS][18];
00168 int ncplbnd;
00169 int ncplsubnd;
00170 int startmant[AC3_MAX_CHANNELS];
00171 int endmant[AC3_MAX_CHANNELS];
00172 AC3BitAllocParameters bit_alloc_params;
00173
00174 int8_t dexps[AC3_MAX_CHANNELS][256];
00175 uint8_t bap[AC3_MAX_CHANNELS][256];
00176 int16_t psd[AC3_MAX_CHANNELS][256];
00177 int16_t bndpsd[AC3_MAX_CHANNELS][50];
00178 int16_t mask[AC3_MAX_CHANNELS][50];
00179
00180 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]);
00181
00182
00183 MDCTContext imdct_512;
00184 MDCTContext imdct_256;
00185 DSPContext dsp;
00186 float add_bias;
00187 float mul_bias;
00188
00189 DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS-1][256]);
00190 DECLARE_ALIGNED_16(short, int_output[AC3_MAX_CHANNELS-1][256]);
00191 DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS-1][256]);
00192 DECLARE_ALIGNED_16(float, tmp_imdct[256]);
00193 DECLARE_ALIGNED_16(float, tmp_output[512]);
00194 DECLARE_ALIGNED_16(float, window[256]);
00195
00196
00197 GetBitContext gb;
00198 AVRandomState dith_state;
00199 AVCodecContext *avctx;
00200 } AC3DecodeContext;
00201
00205 static void ac3_window_init(float *window)
00206 {
00207 int i, j;
00208 double sum = 0.0, bessel, tmp;
00209 double local_window[256];
00210 double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
00211
00212 for (i = 0; i < 256; i++) {
00213 tmp = i * (256 - i) * alpha2;
00214 bessel = 1.0;
00215 for (j = 100; j > 0; j--)
00216 bessel = bessel * tmp / (j * j) + 1;
00217 sum += bessel;
00218 local_window[i] = sum;
00219 }
00220
00221 sum++;
00222 for (i = 0; i < 256; i++)
00223 window[i] = sqrt(local_window[i] / sum);
00224 }
00225
00231 static inline float
00232 symmetric_dequant(int code, int levels)
00233 {
00234 return (code - (levels >> 1)) * (2.0f / levels);
00235 }
00236
00237
00238
00239
00240 static void ac3_tables_init(void)
00241 {
00242 int i;
00243
00244
00245
00246 for(i=0; i<32; i++) {
00247
00248 b1_mantissas[i][0] = symmetric_dequant( i / 9 , 3);
00249 b1_mantissas[i][1] = symmetric_dequant((i % 9) / 3, 3);
00250 b1_mantissas[i][2] = symmetric_dequant((i % 9) % 3, 3);
00251 }
00252 for(i=0; i<128; i++) {
00253
00254 b2_mantissas[i][0] = symmetric_dequant( i / 25 , 5);
00255 b2_mantissas[i][1] = symmetric_dequant((i % 25) / 5, 5);
00256 b2_mantissas[i][2] = symmetric_dequant((i % 25) % 5, 5);
00257
00258
00259 b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
00260 b4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
00261 }
00262
00263
00264 for(i=0; i<7; i++) {
00265
00266 b3_mantissas[i] = symmetric_dequant(i, 7);
00267 }
00268 for(i=0; i<15; i++) {
00269
00270 b5_mantissas[i] = symmetric_dequant(i, 15);
00271 }
00272
00273
00274
00275 for(i=0; i<256; i++) {
00276 int v = (i >> 5) - ((i >> 7) << 3) - 5;
00277 dynrng_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
00278 }
00279
00280
00281
00282
00283 for(i=1; i<32; i++) {
00284 dialnorm_tab[i] = expf((i-31) * M_LN10 / 20.0f);
00285 }
00286 dialnorm_tab[0] = dialnorm_tab[31];
00287
00288
00289
00290 for (i = 0; i < 25; i++)
00291 scale_factors[i] = pow(2.0, -i);
00292
00293
00294
00295 for(i=0; i<128; i++) {
00296 exp_ungroup_tab[i][0] = i / 25;
00297 exp_ungroup_tab[i][1] = (i % 25) / 5;
00298 exp_ungroup_tab[i][2] = (i % 25) % 5;
00299 }
00300 }
00301
00302
00306 static int ac3_decode_init(AVCodecContext *avctx)
00307 {
00308 AC3DecodeContext *ctx = avctx->priv_data;
00309 ctx->avctx = avctx;
00310
00311 ac3_common_init();
00312 ac3_tables_init();
00313 ff_mdct_init(&ctx->imdct_256, 8, 1);
00314 ff_mdct_init(&ctx->imdct_512, 9, 1);
00315 ac3_window_init(ctx->window);
00316 dsputil_init(&ctx->dsp, avctx);
00317 av_init_random(0, &ctx->dith_state);
00318
00319
00320 if(ctx->dsp.float_to_int16 == ff_float_to_int16_c) {
00321 ctx->add_bias = 385.0f;
00322 ctx->mul_bias = 1.0f;
00323 } else {
00324 ctx->add_bias = 0.0f;
00325 ctx->mul_bias = 32767.0f;
00326 }
00327
00328 return 0;
00329 }
00330
00336 static int ac3_parse_header(AC3DecodeContext *ctx)
00337 {
00338 AC3HeaderInfo hdr;
00339 GetBitContext *gb = &ctx->gb;
00340 float cmixlev, surmixlev;
00341 int err, i;
00342
00343 err = ff_ac3_parse_header(gb->buffer, &hdr);
00344 if(err)
00345 return err;
00346
00347
00348 ctx->bit_alloc_params.fscod = hdr.fscod;
00349 ctx->acmod = hdr.acmod;
00350 cmixlev = gain_levels[clevs[hdr.cmixlev]];
00351 surmixlev = gain_levels[slevs[hdr.surmixlev]];
00352 ctx->dsurmod = hdr.dsurmod;
00353 ctx->lfeon = hdr.lfeon;
00354 ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
00355 ctx->sampling_rate = hdr.sample_rate;
00356 ctx->bit_rate = hdr.bit_rate;
00357 ctx->nchans = hdr.channels;
00358 ctx->nfchans = ctx->nchans - ctx->lfeon;
00359 ctx->lfe_ch = ctx->nfchans + 1;
00360 ctx->frame_size = hdr.frame_size;
00361
00362
00363 ctx->out_channels = ctx->nchans;
00364 ctx->output_mode = ctx->acmod;
00365 if(ctx->lfeon)
00366 ctx->output_mode |= AC3_OUTPUT_LFEON;
00367
00368
00369 skip_bits(gb, 16);
00370 skip_bits(gb, 16);
00371 skip_bits(gb, 8);
00372 skip_bits(gb, 11);
00373 if(ctx->acmod == AC3_ACMOD_STEREO) {
00374 skip_bits(gb, 2);
00375 } else {
00376 if((ctx->acmod & 1) && ctx->acmod != AC3_ACMOD_MONO)
00377 skip_bits(gb, 2);
00378 if(ctx->acmod & 4)
00379 skip_bits(gb, 2);
00380 }
00381 skip_bits1(gb);
00382
00383
00384 i = !(ctx->acmod);
00385 do {
00386 ctx->dialnorm[i] = dialnorm_tab[get_bits(gb, 5)];
00387 if (get_bits1(gb))
00388 skip_bits(gb, 8);
00389 if (get_bits1(gb))
00390 skip_bits(gb, 8);
00391 if (get_bits1(gb))
00392 skip_bits(gb, 7);
00393 } while (i--);
00394
00395 skip_bits(gb, 2);
00396
00397
00398
00399 if (get_bits1(gb))
00400 skip_bits(gb, 14);
00401 if (get_bits1(gb))
00402 skip_bits(gb, 14);
00403
00404
00405 if (get_bits1(gb)) {
00406 i = get_bits(gb, 6);
00407 do {
00408 skip_bits(gb, 8);
00409 } while(i--);
00410 }
00411
00412
00413
00414 for(i=0; i<ctx->nfchans; i++) {
00415 ctx->downmix_coeffs[i][0] = gain_levels[ac3_default_coeffs[ctx->acmod][i][0]];
00416 ctx->downmix_coeffs[i][1] = gain_levels[ac3_default_coeffs[ctx->acmod][i][1]];
00417 }
00418 if(ctx->acmod > 1 && ctx->acmod & 1) {
00419 ctx->downmix_coeffs[1][0] = ctx->downmix_coeffs[1][1] = cmixlev;
00420 }
00421 if(ctx->acmod == AC3_ACMOD_2F1R || ctx->acmod == AC3_ACMOD_3F1R) {
00422 int nf = ctx->acmod - 2;
00423 ctx->downmix_coeffs[nf][0] = ctx->downmix_coeffs[nf][1] = surmixlev * LEVEL_MINUS_3DB;
00424 }
00425 if(ctx->acmod == AC3_ACMOD_2F2R || ctx->acmod == AC3_ACMOD_3F2R) {
00426 int nf = ctx->acmod - 4;
00427 ctx->downmix_coeffs[nf][0] = ctx->downmix_coeffs[nf+1][1] = surmixlev;
00428 }
00429
00430 return 0;
00431 }
00432
00437 static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,
00438 uint8_t absexp, int8_t *dexps)
00439 {
00440 int i, j, grp, grpsize;
00441 int dexp[256];
00442 int expacc, prevexp;
00443
00444
00445 grpsize = expstr + (expstr == EXP_D45);
00446 for(grp=0,i=0; grp<ngrps; grp++) {
00447 expacc = get_bits(gb, 7);
00448 dexp[i++] = exp_ungroup_tab[expacc][0];
00449 dexp[i++] = exp_ungroup_tab[expacc][1];
00450 dexp[i++] = exp_ungroup_tab[expacc][2];
00451 }
00452
00453
00454 prevexp = absexp;
00455 for(i=0; i<ngrps*3; i++) {
00456 prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
00457 for(j=0; j<grpsize; j++) {
00458 dexps[(i*grpsize)+j] = prevexp;
00459 }
00460 }
00461 }
00462
00468 static void uncouple_channels(AC3DecodeContext *ctx)
00469 {
00470 int i, j, ch, bnd, subbnd;
00471
00472 subbnd = -1;
00473 i = ctx->startmant[CPL_CH];
00474 for(bnd=0; bnd<ctx->ncplbnd; bnd++) {
00475 do {
00476 subbnd++;
00477 for(j=0; j<12; j++) {
00478 for(ch=1; ch<=ctx->nfchans; ch++) {
00479 if(ctx->chincpl[ch])
00480 ctx->transform_coeffs[ch][i] = ctx->transform_coeffs[CPL_CH][i] * ctx->cplco[ch][bnd] * 8.0f;
00481 }
00482 i++;
00483 }
00484 } while(ctx->cplbndstrc[subbnd]);
00485 }
00486 }
00487
00491 typedef struct {
00492 float b1_mant[3];
00493 float b2_mant[3];
00494 float b4_mant[2];
00495 int b1ptr;
00496 int b2ptr;
00497 int b4ptr;
00498 } mant_groups;
00499
00504 static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
00505 {
00506 GetBitContext *gb = &ctx->gb;
00507 int i, gcode, tbap, start, end;
00508 uint8_t *exps;
00509 uint8_t *bap;
00510 float *coeffs;
00511
00512 exps = ctx->dexps[ch_index];
00513 bap = ctx->bap[ch_index];
00514 coeffs = ctx->transform_coeffs[ch_index];
00515 start = ctx->startmant[ch_index];
00516 end = ctx->endmant[ch_index];
00517
00518 for (i = start; i < end; i++) {
00519 tbap = bap[i];
00520 switch (tbap) {
00521 case 0:
00522 coeffs[i] = ((av_random(&ctx->dith_state) & 0xFFFF) / 65535.0f) - 0.5f;
00523 break;
00524
00525 case 1:
00526 if(m->b1ptr > 2) {
00527 gcode = get_bits(gb, 5);
00528 m->b1_mant[0] = b1_mantissas[gcode][0];
00529 m->b1_mant[1] = b1_mantissas[gcode][1];
00530 m->b1_mant[2] = b1_mantissas[gcode][2];
00531 m->b1ptr = 0;
00532 }
00533 coeffs[i] = m->b1_mant[m->b1ptr++];
00534 break;
00535
00536 case 2:
00537 if(m->b2ptr > 2) {
00538 gcode = get_bits(gb, 7);
00539 m->b2_mant[0] = b2_mantissas[gcode][0];
00540 m->b2_mant[1] = b2_mantissas[gcode][1];
00541 m->b2_mant[2] = b2_mantissas[gcode][2];
00542 m->b2ptr = 0;
00543 }
00544 coeffs[i] = m->b2_mant[m->b2ptr++];
00545 break;
00546
00547 case 3:
00548 coeffs[i] = b3_mantissas[get_bits(gb, 3)];
00549 break;
00550
00551 case 4:
00552 if(m->b4ptr > 1) {
00553 gcode = get_bits(gb, 7);
00554 m->b4_mant[0] = b4_mantissas[gcode][0];
00555 m->b4_mant[1] = b4_mantissas[gcode][1];
00556 m->b4ptr = 0;
00557 }
00558 coeffs[i] = m->b4_mant[m->b4ptr++];
00559 break;
00560
00561 case 5:
00562 coeffs[i] = b5_mantissas[get_bits(gb, 4)];
00563 break;
00564
00565 default:
00566
00567 coeffs[i] = get_sbits(gb, qntztab[tbap]) * scale_factors[qntztab[tbap]-1];
00568 break;
00569 }
00570 coeffs[i] *= scale_factors[exps[i]];
00571 }
00572
00573 return 0;
00574 }
00575
00580 static void remove_dithering(AC3DecodeContext *ctx) {
00581 int ch, i;
00582 int end=0;
00583 float *coeffs;
00584 uint8_t *bap;
00585
00586 for(ch=1; ch<=ctx->nfchans; ch++) {
00587 if(!ctx->dithflag[ch]) {
00588 coeffs = ctx->transform_coeffs[ch];
00589 bap = ctx->bap[ch];
00590 if(ctx->chincpl[ch])
00591 end = ctx->startmant[CPL_CH];
00592 else
00593 end = ctx->endmant[ch];
00594 for(i=0; i<end; i++) {
00595 if(bap[i] == 0)
00596 coeffs[i] = 0.0f;
00597 }
00598 if(ctx->chincpl[ch]) {
00599 bap = ctx->bap[CPL_CH];
00600 for(; i<ctx->endmant[CPL_CH]; i++) {
00601 if(bap[i] == 0)
00602 coeffs[i] = 0.0f;
00603 }
00604 }
00605 }
00606 }
00607 }
00608
00612 static int get_transform_coeffs(AC3DecodeContext * ctx)
00613 {
00614 int ch, end;
00615 int got_cplchan = 0;
00616 mant_groups m;
00617
00618 m.b1ptr = m.b2ptr = m.b4ptr = 3;
00619
00620 for (ch = 1; ch <= ctx->nchans; ch++) {
00621
00622 if (get_transform_coeffs_ch(ctx, ch, &m))
00623 return -1;
00624
00625
00626 if (ctx->chincpl[ch]) {
00627 if (!got_cplchan) {
00628 if (get_transform_coeffs_ch(ctx, CPL_CH, &m)) {
00629 av_log(ctx->avctx, AV_LOG_ERROR, "error in decoupling channels\n");
00630 return -1;
00631 }
00632 uncouple_channels(ctx);
00633 got_cplchan = 1;
00634 }
00635 end = ctx->endmant[CPL_CH];
00636 } else {
00637 end = ctx->endmant[ch];
00638 }
00639 do
00640 ctx->transform_coeffs[ch][end] = 0;
00641 while(++end < 256);
00642 }
00643
00644
00645 if(!ctx->dither_all)
00646 remove_dithering(ctx);
00647
00648 return 0;
00649 }
00650
00655 static void do_rematrixing(AC3DecodeContext *ctx)
00656 {
00657 int bnd, i;
00658 int end, bndend;
00659 float tmp0, tmp1;
00660
00661 end = FFMIN(ctx->endmant[1], ctx->endmant[2]);
00662
00663 for(bnd=0; bnd<ctx->nrematbnd; bnd++) {
00664 if(ctx->rematflg[bnd]) {
00665 bndend = FFMIN(end, rematrix_band_tab[bnd+1]);
00666 for(i=rematrix_band_tab[bnd]; i<bndend; i++) {
00667 tmp0 = ctx->transform_coeffs[1][i];
00668 tmp1 = ctx->transform_coeffs[2][i];
00669 ctx->transform_coeffs[1][i] = tmp0 + tmp1;
00670 ctx->transform_coeffs[2][i] = tmp0 - tmp1;
00671 }
00672 }
00673 }
00674 }
00675
00679 static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
00680 {
00681 int i, k;
00682 DECLARE_ALIGNED_16(float, x[128]);
00683 FFTComplex z[2][64];
00684 float *o_ptr = ctx->tmp_output;
00685
00686 for(i=0; i<2; i++) {
00687
00688 for(k=0; k<128; k++) {
00689 x[k] = ctx->transform_coeffs[chindex][2*k+i];
00690 }
00691
00692
00693 ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
00694
00695
00696 for(k=0; k<32; k++) {
00697 z[i][32+k].re = -o_ptr[128+2*k];
00698 z[i][32+k].im = -o_ptr[2*k];
00699 z[i][31-k].re = o_ptr[2*k+1];
00700 z[i][31-k].im = o_ptr[128+2*k+1];
00701 }
00702 }
00703
00704
00705 for(k=0; k<64; k++) {
00706 o_ptr[ 2*k ] = -z[0][ k].im;
00707 o_ptr[ 2*k+1] = z[0][63-k].re;
00708 o_ptr[128+2*k ] = -z[0][ k].re;
00709 o_ptr[128+2*k+1] = z[0][63-k].im;
00710 o_ptr[256+2*k ] = -z[1][ k].re;
00711 o_ptr[256+2*k+1] = z[1][63-k].im;
00712 o_ptr[384+2*k ] = z[1][ k].im;
00713 o_ptr[384+2*k+1] = -z[1][63-k].re;
00714 }
00715 }
00716
00722 static inline void do_imdct(AC3DecodeContext *ctx)
00723 {
00724 int ch;
00725 int nchans;
00726
00727
00728 nchans = ctx->nfchans;
00729 if(ctx->output_mode & AC3_OUTPUT_LFEON)
00730 nchans++;
00731
00732 for (ch=1; ch<=nchans; ch++) {
00733 if (ctx->blksw[ch]) {
00734 do_imdct_256(ctx, ch);
00735 } else {
00736 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
00737 ctx->transform_coeffs[ch],
00738 ctx->tmp_imdct);
00739 }
00740
00741
00742 ctx->dsp.vector_fmul_add_add(ctx->output[ch-1], ctx->tmp_output,
00743 ctx->window, ctx->delay[ch-1], 0, 256, 1);
00744
00745
00746 ctx->dsp.vector_fmul_reverse(ctx->delay[ch-1], ctx->tmp_output+256,
00747 ctx->window, 256);
00748 }
00749 }
00750
00754 static void ac3_downmix(float samples[AC3_MAX_CHANNELS][256], int nfchans,
00755 int output_mode, float coef[AC3_MAX_CHANNELS][2])
00756 {
00757 int i, j;
00758 float v0, v1, s0, s1;
00759
00760 for(i=0; i<256; i++) {
00761 v0 = v1 = s0 = s1 = 0.0f;
00762 for(j=0; j<nfchans; j++) {
00763 v0 += samples[j][i] * coef[j][0];
00764 v1 += samples[j][i] * coef[j][1];
00765 s0 += coef[j][0];
00766 s1 += coef[j][1];
00767 }
00768
00769
00770
00771
00772
00773
00774 if(output_mode == AC3_ACMOD_MONO) {
00775 samples[0][i] = (v0 + v1) * LEVEL_MINUS_3DB;
00776 } else if(output_mode == AC3_ACMOD_STEREO) {
00777 samples[0][i] = v0;
00778 samples[1][i] = v1;
00779 }
00780 }
00781 }
00782
00786 static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
00787 {
00788 int nfchans = ctx->nfchans;
00789 int acmod = ctx->acmod;
00790 int i, bnd, seg, ch;
00791 GetBitContext *gb = &ctx->gb;
00792 uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];
00793
00794 memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);
00795
00796
00797 for (ch = 1; ch <= nfchans; ch++)
00798 ctx->blksw[ch] = get_bits1(gb);
00799
00800
00801 ctx->dither_all = 1;
00802 for (ch = 1; ch <= nfchans; ch++) {
00803 ctx->dithflag[ch] = get_bits1(gb);
00804 if(!ctx->dithflag[ch])
00805 ctx->dither_all = 0;
00806 }
00807
00808
00809 i = !(ctx->acmod);
00810 do {
00811 if(get_bits1(gb)) {
00812 ctx->dynrng[i] = dynrng_tab[get_bits(gb, 8)];
00813 } else if(blk == 0) {
00814 ctx->dynrng[i] = 1.0f;
00815 }
00816 } while(i--);
00817
00818
00819 if (get_bits1(gb)) {
00820 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
00821 ctx->cplinu = get_bits1(gb);
00822 if (ctx->cplinu) {
00823
00824 int cplbegf, cplendf;
00825
00826
00827 for (ch = 1; ch <= nfchans; ch++)
00828 ctx->chincpl[ch] = get_bits1(gb);
00829
00830
00831 if (acmod == AC3_ACMOD_STEREO)
00832 ctx->phsflginu = get_bits1(gb);
00833
00834
00835 cplbegf = get_bits(gb, 4);
00836 cplendf = get_bits(gb, 4);
00837 if (3 + cplendf - cplbegf < 0) {
00838 av_log(ctx->avctx, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", cplendf, cplbegf);
00839 return -1;
00840 }
00841 ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf;
00842 ctx->startmant[CPL_CH] = cplbegf * 12 + 37;
00843 ctx->endmant[CPL_CH] = cplendf * 12 + 73;
00844 for (bnd = 0; bnd < ctx->ncplsubnd - 1; bnd++) {
00845 if (get_bits1(gb)) {
00846 ctx->cplbndstrc[bnd] = 1;
00847 ctx->ncplbnd--;
00848 }
00849 }
00850 } else {
00851
00852 for (ch = 1; ch <= nfchans; ch++)
00853 ctx->chincpl[ch] = 0;
00854 }
00855 }
00856
00857
00858 if (ctx->cplinu) {
00859 int cplcoe = 0;
00860
00861 for (ch = 1; ch <= nfchans; ch++) {
00862 if (ctx->chincpl[ch]) {
00863 if (get_bits1(gb)) {
00864 int mstrcplco, cplcoexp, cplcomant;
00865 cplcoe = 1;
00866 mstrcplco = 3 * get_bits(gb, 2);
00867 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
00868 cplcoexp = get_bits(gb, 4);
00869 cplcomant = get_bits(gb, 4);
00870 if (cplcoexp == 15)
00871 ctx->cplco[ch][bnd] = cplcomant / 16.0f;
00872 else
00873 ctx->cplco[ch][bnd] = (cplcomant + 16.0f) / 32.0f;
00874 ctx->cplco[ch][bnd] *= scale_factors[cplcoexp + mstrcplco];
00875 }
00876 }
00877 }
00878 }
00879
00880 if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && cplcoe) {
00881 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
00882 if (get_bits1(gb))
00883 ctx->cplco[2][bnd] = -ctx->cplco[2][bnd];
00884 }
00885 }
00886 }
00887
00888
00889 if (acmod == AC3_ACMOD_STEREO) {
00890 ctx->rematstr = get_bits1(gb);
00891 if (ctx->rematstr) {
00892 ctx->nrematbnd = 4;
00893 if(ctx->cplinu && ctx->startmant[CPL_CH] <= 61)
00894 ctx->nrematbnd -= 1 + (ctx->startmant[CPL_CH] == 37);
00895 for(bnd=0; bnd<ctx->nrematbnd; bnd++)
00896 ctx->rematflg[bnd] = get_bits1(gb);
00897 }
00898 }
00899
00900
00901 ctx->expstr[CPL_CH] = EXP_REUSE;
00902 ctx->expstr[ctx->lfe_ch] = EXP_REUSE;
00903 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
00904 if(ch == ctx->lfe_ch)
00905 ctx->expstr[ch] = get_bits(gb, 1);
00906 else
00907 ctx->expstr[ch] = get_bits(gb, 2);
00908 if(ctx->expstr[ch] != EXP_REUSE)
00909 bit_alloc_stages[ch] = 3;
00910 }
00911
00912
00913 for (ch = 1; ch <= nfchans; ch++) {
00914 ctx->startmant[ch] = 0;
00915 if (ctx->expstr[ch] != EXP_REUSE) {
00916 int prev = ctx->endmant[ch];
00917 if (ctx->chincpl[ch])
00918 ctx->endmant[ch] = ctx->startmant[CPL_CH];
00919 else {
00920 int chbwcod = get_bits(gb, 6);
00921 if (chbwcod > 60) {
00922 av_log(ctx->avctx, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
00923 return -1;
00924 }
00925 ctx->endmant[ch] = chbwcod * 3 + 73;
00926 }
00927 if(blk > 0 && ctx->endmant[ch] != prev)
00928 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
00929 }
00930 }
00931 ctx->startmant[ctx->lfe_ch] = 0;
00932 ctx->endmant[ctx->lfe_ch] = 7;
00933
00934
00935 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
00936 if (ctx->expstr[ch] != EXP_REUSE) {
00937 int grpsize, ngrps;
00938 grpsize = 3 << (ctx->expstr[ch] - 1);
00939 if(ch == CPL_CH)
00940 ngrps = (ctx->endmant[ch] - ctx->startmant[ch]) / grpsize;
00941 else if(ch == ctx->lfe_ch)
00942 ngrps = 2;
00943 else
00944 ngrps = (ctx->endmant[ch] + grpsize - 4) / grpsize;
00945 ctx->dexps[ch][0] = get_bits(gb, 4) << !ch;
00946 decode_exponents(gb, ctx->expstr[ch], ngrps, ctx->dexps[ch][0],
00947 &ctx->dexps[ch][ctx->startmant[ch]+!!ch]);
00948 if(ch != CPL_CH && ch != ctx->lfe_ch)
00949 skip_bits(gb, 2);
00950 }
00951 }
00952
00953
00954 if (get_bits1(gb)) {
00955 ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)] >> ctx->bit_alloc_params.halfratecod;
00956 ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)] >> ctx->bit_alloc_params.halfratecod;
00957 ctx->bit_alloc_params.sgain = ff_sgaintab[get_bits(gb, 2)];
00958 ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)];
00959 ctx->bit_alloc_params.floor = ff_floortab[get_bits(gb, 3)];
00960 for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {
00961 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
00962 }
00963 }
00964
00965
00966 if (get_bits1(gb)) {
00967 int csnr;
00968 csnr = (get_bits(gb, 6) - 15) << 4;
00969 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
00970 ctx->snroffst[ch] = (csnr + get_bits(gb, 4)) << 2;
00971 ctx->fgain[ch] = ff_fgaintab[get_bits(gb, 3)];
00972 }
00973 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
00974 }
00975
00976
00977 if (ctx->cplinu && get_bits1(gb)) {
00978 ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);
00979 ctx->bit_alloc_params.cplsleak = get_bits(gb, 3);
00980 bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
00981 }
00982
00983
00984 if (get_bits1(gb)) {
00985
00986 for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
00987 ctx->deltbae[ch] = get_bits(gb, 2);
00988 if (ctx->deltbae[ch] == DBA_RESERVED) {
00989 av_log(ctx->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
00990 return -1;
00991 }
00992 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
00993 }
00994
00995 for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
00996 if (ctx->deltbae[ch] == DBA_NEW) {
00997 ctx->deltnseg[ch] = get_bits(gb, 3);
00998 for (seg = 0; seg <= ctx->deltnseg[ch]; seg++) {
00999 ctx->deltoffst[ch][seg] = get_bits(gb, 5);
01000 ctx->deltlen[ch][seg] = get_bits(gb, 4);
01001 ctx->deltba[ch][seg] = get_bits(gb, 3);
01002 }
01003 }
01004 }
01005 } else if(blk == 0) {
01006 for(ch=0; ch<=ctx->nchans; ch++) {
01007 ctx->deltbae[ch] = DBA_NONE;
01008 }
01009 }
01010
01011
01012 for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {
01013 if(bit_alloc_stages[ch] > 2) {
01014
01015 ff_ac3_bit_alloc_calc_psd(ctx->dexps[ch],
01016 ctx->startmant[ch], ctx->endmant[ch],
01017 ctx->psd[ch], ctx->bndpsd[ch]);
01018 }
01019 if(bit_alloc_stages[ch] > 1) {
01020
01021
01022 ff_ac3_bit_alloc_calc_mask(&ctx->bit_alloc_params, ctx->bndpsd[ch],
01023 ctx->startmant[ch], ctx->endmant[ch],
01024 ctx->fgain[ch], (ch == ctx->lfe_ch),
01025 ctx->deltbae[ch], ctx->deltnseg[ch],
01026 ctx->deltoffst[ch], ctx->deltlen[ch],
01027 ctx->deltba[ch], ctx->mask[ch]);
01028 }
01029 if(bit_alloc_stages[ch] > 0) {
01030
01031 ff_ac3_bit_alloc_calc_bap(ctx->mask[ch], ctx->psd[ch],
01032 ctx->startmant[ch], ctx->endmant[ch],
01033 ctx->snroffst[ch],
01034 ctx->bit_alloc_params.floor,
01035 ctx->bap[ch]);
01036 }
01037 }
01038
01039
01040 if (get_bits1(gb)) {
01041 int skipl = get_bits(gb, 9);
01042 while(skipl--)
01043 skip_bits(gb, 8);
01044 }
01045
01046
01047
01048 if (get_transform_coeffs(ctx)) {
01049 av_log(ctx->avctx, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
01050 return -1;
01051 }
01052
01053
01054 if(ctx->acmod == AC3_ACMOD_STEREO)
01055 do_rematrixing(ctx);
01056
01057
01058 for(ch=1; ch<=ctx->nchans; ch++) {
01059 float gain = 2.0f * ctx->mul_bias;
01060 if(ctx->acmod == AC3_ACMOD_DUALMONO) {
01061 gain *= ctx->dialnorm[ch-1] * ctx->dynrng[ch-1];
01062 } else {
01063 gain *= ctx->dialnorm[0] * ctx->dynrng[0];
01064 }
01065 for(i=0; i<ctx->endmant[ch]; i++) {
01066 ctx->transform_coeffs[ch][i] *= gain;
01067 }
01068 }
01069
01070 do_imdct(ctx);
01071
01072
01073 if(ctx->nchans != ctx->out_channels && !((ctx->output_mode & AC3_OUTPUT_LFEON) &&
01074 ctx->nfchans == ctx->out_channels)) {
01075 ac3_downmix(ctx->output, ctx->nfchans, ctx->output_mode,
01076 ctx->downmix_coeffs);
01077 }
01078
01079
01080 for(ch=0; ch<ctx->out_channels; ch++) {
01081 for(i=0; i<256; i++) {
01082 ctx->output[ch][i] += ctx->add_bias;
01083 }
01084 ctx->dsp.float_to_int16(ctx->int_output[ch], ctx->output[ch], 256);
01085 }
01086
01087 return 0;
01088 }
01089
01093 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
01094 {
01095 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
01096 int16_t *out_samples = (int16_t *)data;
01097 int i, blk, ch, err;
01098
01099
01100 init_get_bits(&ctx->gb, buf, buf_size * 8);
01101
01102
01103 err = ac3_parse_header(ctx);
01104 if(err) {
01105 switch(err) {
01106 case AC3_PARSE_ERROR_SYNC:
01107 av_log(avctx, AV_LOG_ERROR, "frame sync error\n");
01108 break;
01109 case AC3_PARSE_ERROR_BSID:
01110 av_log(avctx, AV_LOG_ERROR, "invalid bitstream id\n");
01111 break;
01112 case AC3_PARSE_ERROR_SAMPLE_RATE:
01113 av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
01114 break;
01115 case AC3_PARSE_ERROR_FRAME_SIZE:
01116 av_log(avctx, AV_LOG_ERROR, "invalid frame size\n");
01117 break;
01118 default:
01119 av_log(avctx, AV_LOG_ERROR, "invalid header\n");
01120 break;
01121 }
01122 return -1;
01123 }
01124
01125
01126 if(avctx->error_resilience >= FF_ER_CAREFUL) {
01127 if(av_crc(av_crc8005, 0, &buf[2], ctx->frame_size-2)) {
01128 av_log(avctx, AV_LOG_ERROR, "frame CRC mismatch\n");
01129 *data_size = 0;
01130 return ctx->frame_size;
01131 }
01132
01133 }
01134
01135 avctx->sample_rate = ctx->sampling_rate;
01136 avctx->bit_rate = ctx->bit_rate;
01137
01138
01139 if(ctx->frame_size > buf_size) {
01140 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
01141 return -1;
01142 }
01143
01144
01145 ctx->out_channels = ctx->nchans;
01146 if (avctx->request_channels > 0)
01147 {
01148 avctx->channels = ctx->out_channels;
01149 if (avctx->channels > avctx->request_channels)
01150 avctx->channels = avctx->request_channels;
01151 }
01152 if (avctx->channels == 0) {
01153 avctx->channels = ctx->out_channels;
01154 } else if(ctx->out_channels < avctx->channels) {
01155 av_log(avctx, AV_LOG_ERROR, "Cannot upmix AC3 from %d to %d channels.\n",
01156 ctx->out_channels, avctx->channels);
01157 return -1;
01158 }
01159 if(avctx->channels == 2) {
01160 ctx->output_mode = AC3_ACMOD_STEREO;
01161 } else if(avctx->channels == 1) {
01162 ctx->output_mode = AC3_ACMOD_MONO;
01163 } else if(avctx->channels != ctx->out_channels) {
01164 av_log(avctx, AV_LOG_ERROR, "Cannot downmix AC3 from %d to %d channels.\n",
01165 ctx->out_channels, avctx->channels);
01166 return -1;
01167 }
01168 ctx->out_channels = avctx->channels;
01169
01170
01171 for (blk = 0; blk < NB_BLOCKS; blk++) {
01172 if (ac3_parse_audio_block(ctx, blk)) {
01173 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
01174 *data_size = 0;
01175 return ctx->frame_size;
01176 }
01177 for (i = 0; i < 256; i++)
01178 for (ch = 0; ch < ctx->out_channels; ch++)
01179 *(out_samples++) = ctx->int_output[ch][i];
01180 }
01181 *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t);
01182 return ctx->frame_size;
01183 }
01184
01188 static int ac3_decode_end(AVCodecContext *avctx)
01189 {
01190 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
01191 ff_mdct_end(&ctx->imdct_512);
01192 ff_mdct_end(&ctx->imdct_256);
01193
01194 return 0;
01195 }
01196
01197 AVCodec ac3_decoder = {
01198 .name = "ac3",
01199 .type = CODEC_TYPE_AUDIO,
01200 .id = CODEC_ID_AC3,
01201 .priv_data_size = sizeof (AC3DecodeContext),
01202 .init = ac3_decode_init,
01203 .close = ac3_decode_end,
01204 .decode = ac3_decode_frame,
01205 };