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 quantization_tab[16] = {
00070 0, 3, 5, 7, 11, 15,
00071 5, 6, 7, 8, 9, 10, 11, 12, 14, 16
00072 };
00073
00075 static float dynamic_range_tab[256];
00076
00078 #define LEVEL_MINUS_3DB 0.7071067811865476
00079 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
00080 #define LEVEL_MINUS_6DB 0.5000000000000000
00081 #define LEVEL_MINUS_9DB 0.3535533905932738
00082 #define LEVEL_ZERO 0.0000000000000000
00083 #define LEVEL_ONE 1.0000000000000000
00084
00085 static const float gain_levels[6] = {
00086 LEVEL_ZERO,
00087 LEVEL_ONE,
00088 LEVEL_MINUS_3DB,
00089 LEVEL_MINUS_4POINT5DB,
00090 LEVEL_MINUS_6DB,
00091 LEVEL_MINUS_9DB
00092 };
00093
00098 static const uint8_t center_levels[4] = { 2, 3, 4, 3 };
00099
00104 static const uint8_t surround_levels[4] = { 2, 4, 0, 4 };
00105
00110 static const uint8_t ac3_default_coeffs[8][5][2] = {
00111 { { 1, 0 }, { 0, 1 }, },
00112 { { 2, 2 }, },
00113 { { 1, 0 }, { 0, 1 }, },
00114 { { 1, 0 }, { 3, 3 }, { 0, 1 }, },
00115 { { 1, 0 }, { 0, 1 }, { 4, 4 }, },
00116 { { 1, 0 }, { 3, 3 }, { 0, 1 }, { 5, 5 }, },
00117 { { 1, 0 }, { 0, 1 }, { 4, 0 }, { 0, 4 }, },
00118 { { 1, 0 }, { 3, 3 }, { 0, 1 }, { 4, 0 }, { 0, 4 }, },
00119 };
00120
00121
00122 #undef AC3_MAX_CHANNELS
00123 #define AC3_MAX_CHANNELS 7
00124 #define CPL_CH 0
00125
00126 #define AC3_OUTPUT_LFEON 8
00127
00128 typedef struct {
00129 int channel_mode;
00130 int block_switch[AC3_MAX_CHANNELS];
00131 int dither_flag[AC3_MAX_CHANNELS];
00132 int dither_all;
00133 int cpl_in_use;
00134 int channel_in_cpl[AC3_MAX_CHANNELS];
00135 int phase_flags_in_use;
00136 int phase_flags[18];
00137 int cpl_band_struct[18];
00138 int num_rematrixing_bands;
00139 int rematrixing_flags[4];
00140 int exp_strategy[AC3_MAX_CHANNELS];
00141 int snr_offset[AC3_MAX_CHANNELS];
00142 int fast_gain[AC3_MAX_CHANNELS];
00143 int dba_mode[AC3_MAX_CHANNELS];
00144 int dba_nsegs[AC3_MAX_CHANNELS];
00145 uint8_t dba_offsets[AC3_MAX_CHANNELS][8];
00146 uint8_t dba_lengths[AC3_MAX_CHANNELS][8];
00147 uint8_t dba_values[AC3_MAX_CHANNELS][8];
00148
00149 int sample_rate;
00150 int bit_rate;
00151 int frame_size;
00152
00153 int channels;
00154 int fbw_channels;
00155 int lfe_on;
00156 int lfe_ch;
00157 int output_mode;
00158 int out_channels;
00159
00160 int center_mix_level;
00161 int surround_mix_level;
00162 float downmix_coeffs[AC3_MAX_CHANNELS][2];
00163 float dynamic_range[2];
00164 float cpl_coords[AC3_MAX_CHANNELS][18];
00165 int num_cpl_bands;
00166 int num_cpl_subbands;
00167 int start_freq[AC3_MAX_CHANNELS];
00168 int end_freq[AC3_MAX_CHANNELS];
00169 AC3BitAllocParameters bit_alloc_params;
00170
00171 int8_t dexps[AC3_MAX_CHANNELS][256];
00172 uint8_t bap[AC3_MAX_CHANNELS][256];
00173 int16_t psd[AC3_MAX_CHANNELS][256];
00174 int16_t band_psd[AC3_MAX_CHANNELS][50];
00175 int16_t mask[AC3_MAX_CHANNELS][50];
00176
00177 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]);
00178
00179
00180 MDCTContext imdct_512;
00181 MDCTContext imdct_256;
00182 DSPContext dsp;
00183 float add_bias;
00184 float mul_bias;
00185
00186 DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS-1][256]);
00187 DECLARE_ALIGNED_16(short, int_output[AC3_MAX_CHANNELS-1][256]);
00188 DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS-1][256]);
00189 DECLARE_ALIGNED_16(float, tmp_imdct[256]);
00190 DECLARE_ALIGNED_16(float, tmp_output[512]);
00191 DECLARE_ALIGNED_16(float, window[256]);
00192
00193
00194 GetBitContext gbc;
00195 AVRandomState dith_state;
00196 AVCodecContext *avctx;
00197 } AC3DecodeContext;
00198
00204 static inline float
00205 symmetric_dequant(int code, int levels)
00206 {
00207 return (code - (levels >> 1)) * (2.0f / levels);
00208 }
00209
00210
00211
00212
00213 static void ac3_tables_init(void)
00214 {
00215 int i;
00216
00217
00218
00219 for(i=0; i<32; i++) {
00220
00221 b1_mantissas[i][0] = symmetric_dequant( i / 9 , 3);
00222 b1_mantissas[i][1] = symmetric_dequant((i % 9) / 3, 3);
00223 b1_mantissas[i][2] = symmetric_dequant((i % 9) % 3, 3);
00224 }
00225 for(i=0; i<128; i++) {
00226
00227 b2_mantissas[i][0] = symmetric_dequant( i / 25 , 5);
00228 b2_mantissas[i][1] = symmetric_dequant((i % 25) / 5, 5);
00229 b2_mantissas[i][2] = symmetric_dequant((i % 25) % 5, 5);
00230
00231
00232 b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
00233 b4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
00234 }
00235
00236
00237 for(i=0; i<7; i++) {
00238
00239 b3_mantissas[i] = symmetric_dequant(i, 7);
00240 }
00241 for(i=0; i<15; i++) {
00242
00243 b5_mantissas[i] = symmetric_dequant(i, 15);
00244 }
00245
00246
00247
00248 for(i=0; i<256; i++) {
00249 int v = (i >> 5) - ((i >> 7) << 3) - 5;
00250 dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
00251 }
00252
00253
00254
00255 for (i = 0; i < 25; i++)
00256 scale_factors[i] = pow(2.0, -i);
00257
00258
00259
00260 for(i=0; i<128; i++) {
00261 exp_ungroup_tab[i][0] = i / 25;
00262 exp_ungroup_tab[i][1] = (i % 25) / 5;
00263 exp_ungroup_tab[i][2] = (i % 25) % 5;
00264 }
00265 }
00266
00267
00271 static int ac3_decode_init(AVCodecContext *avctx)
00272 {
00273 AC3DecodeContext *s = avctx->priv_data;
00274 s->avctx = avctx;
00275
00276 ac3_common_init();
00277 ac3_tables_init();
00278 ff_mdct_init(&s->imdct_256, 8, 1);
00279 ff_mdct_init(&s->imdct_512, 9, 1);
00280 ff_kbd_window_init(s->window, 5.0, 256);
00281 dsputil_init(&s->dsp, avctx);
00282 av_init_random(0, &s->dith_state);
00283
00284
00285 if(s->dsp.float_to_int16 == ff_float_to_int16_c) {
00286 s->add_bias = 385.0f;
00287 s->mul_bias = 1.0f;
00288 } else {
00289 s->add_bias = 0.0f;
00290 s->mul_bias = 32767.0f;
00291 }
00292
00293
00294 if (avctx->channels > 0 && avctx->request_channels > 0 &&
00295 avctx->request_channels < avctx->channels &&
00296 avctx->request_channels <= 2) {
00297 avctx->channels = avctx->request_channels;
00298 }
00299
00300 return 0;
00301 }
00302
00308 static int ac3_parse_header(AC3DecodeContext *s)
00309 {
00310 AC3HeaderInfo hdr;
00311 GetBitContext *gbc = &s->gbc;
00312 int err, i;
00313
00314 err = ff_ac3_parse_header(gbc->buffer, &hdr);
00315 if(err)
00316 return err;
00317
00318 if(hdr.bitstream_id > 10)
00319 return AC3_PARSE_ERROR_BSID;
00320
00321
00322 s->bit_alloc_params.sr_code = hdr.sr_code;
00323 s->channel_mode = hdr.channel_mode;
00324 s->lfe_on = hdr.lfe_on;
00325 s->bit_alloc_params.sr_shift = hdr.sr_shift;
00326 s->sample_rate = hdr.sample_rate;
00327 s->bit_rate = hdr.bit_rate;
00328 s->channels = hdr.channels;
00329 s->fbw_channels = s->channels - s->lfe_on;
00330 s->lfe_ch = s->fbw_channels + 1;
00331 s->frame_size = hdr.frame_size;
00332
00333
00334 s->out_channels = s->channels;
00335 s->output_mode = s->channel_mode;
00336 if(s->lfe_on)
00337 s->output_mode |= AC3_OUTPUT_LFEON;
00338
00339
00340 s->center_mix_level = 3;
00341 s->surround_mix_level = 4;
00342
00343
00344 skip_bits(gbc, 16);
00345 skip_bits(gbc, 16);
00346 skip_bits(gbc, 8);
00347 skip_bits(gbc, 11);
00348 if(s->channel_mode == AC3_CHMODE_STEREO) {
00349 skip_bits(gbc, 2);
00350 } else {
00351 if((s->channel_mode & 1) && s->channel_mode != AC3_CHMODE_MONO)
00352 s->center_mix_level = center_levels[get_bits(gbc, 2)];
00353 if(s->channel_mode & 4)
00354 s->surround_mix_level = surround_levels[get_bits(gbc, 2)];
00355 }
00356 skip_bits1(gbc);
00357
00358
00359 i = !(s->channel_mode);
00360 do {
00361 skip_bits(gbc, 5);
00362 if (get_bits1(gbc))
00363 skip_bits(gbc, 8);
00364 if (get_bits1(gbc))
00365 skip_bits(gbc, 8);
00366 if (get_bits1(gbc))
00367 skip_bits(gbc, 7);
00368 } while (i--);
00369
00370 skip_bits(gbc, 2);
00371
00372
00373
00374 if (get_bits1(gbc))
00375 skip_bits(gbc, 14);
00376 if (get_bits1(gbc))
00377 skip_bits(gbc, 14);
00378
00379
00380 if (get_bits1(gbc)) {
00381 i = get_bits(gbc, 6);
00382 do {
00383 skip_bits(gbc, 8);
00384 } while(i--);
00385 }
00386
00387 return 0;
00388 }
00389
00394 static void set_downmix_coeffs(AC3DecodeContext *s)
00395 {
00396 int i;
00397 float cmix = gain_levels[s->center_mix_level];
00398 float smix = gain_levels[s->surround_mix_level];
00399
00400 for(i=0; i<s->fbw_channels; i++) {
00401 s->downmix_coeffs[i][0] = gain_levels[ac3_default_coeffs[s->channel_mode][i][0]];
00402 s->downmix_coeffs[i][1] = gain_levels[ac3_default_coeffs[s->channel_mode][i][1]];
00403 }
00404 if(s->channel_mode > 1 && s->channel_mode & 1) {
00405 s->downmix_coeffs[1][0] = s->downmix_coeffs[1][1] = cmix;
00406 }
00407 if(s->channel_mode == AC3_CHMODE_2F1R || s->channel_mode == AC3_CHMODE_3F1R) {
00408 int nf = s->channel_mode - 2;
00409 s->downmix_coeffs[nf][0] = s->downmix_coeffs[nf][1] = smix * LEVEL_MINUS_3DB;
00410 }
00411 if(s->channel_mode == AC3_CHMODE_2F2R || s->channel_mode == AC3_CHMODE_3F2R) {
00412 int nf = s->channel_mode - 4;
00413 s->downmix_coeffs[nf][0] = s->downmix_coeffs[nf+1][1] = smix;
00414 }
00415 }
00416
00421 static void decode_exponents(GetBitContext *gbc, int exp_strategy, int ngrps,
00422 uint8_t absexp, int8_t *dexps)
00423 {
00424 int i, j, grp, group_size;
00425 int dexp[256];
00426 int expacc, prevexp;
00427
00428
00429 group_size = exp_strategy + (exp_strategy == EXP_D45);
00430 for(grp=0,i=0; grp<ngrps; grp++) {
00431 expacc = get_bits(gbc, 7);
00432 dexp[i++] = exp_ungroup_tab[expacc][0];
00433 dexp[i++] = exp_ungroup_tab[expacc][1];
00434 dexp[i++] = exp_ungroup_tab[expacc][2];
00435 }
00436
00437
00438 prevexp = absexp;
00439 for(i=0; i<ngrps*3; i++) {
00440 prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
00441 for(j=0; j<group_size; j++) {
00442 dexps[(i*group_size)+j] = prevexp;
00443 }
00444 }
00445 }
00446
00452 static void uncouple_channels(AC3DecodeContext *s)
00453 {
00454 int i, j, ch, bnd, subbnd;
00455
00456 subbnd = -1;
00457 i = s->start_freq[CPL_CH];
00458 for(bnd=0; bnd<s->num_cpl_bands; bnd++) {
00459 do {
00460 subbnd++;
00461 for(j=0; j<12; j++) {
00462 for(ch=1; ch<=s->fbw_channels; ch++) {
00463 if(s->channel_in_cpl[ch]) {
00464 s->transform_coeffs[ch][i] = s->transform_coeffs[CPL_CH][i] * s->cpl_coords[ch][bnd] * 8.0f;
00465 if (ch == 2 && s->phase_flags[bnd])
00466 s->transform_coeffs[ch][i] = -s->transform_coeffs[ch][i];
00467 }
00468 }
00469 i++;
00470 }
00471 } while(s->cpl_band_struct[subbnd]);
00472 }
00473 }
00474
00478 typedef struct {
00479 float b1_mant[3];
00480 float b2_mant[3];
00481 float b4_mant[2];
00482 int b1ptr;
00483 int b2ptr;
00484 int b4ptr;
00485 } mant_groups;
00486
00491 static int get_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, mant_groups *m)
00492 {
00493 GetBitContext *gbc = &s->gbc;
00494 int i, gcode, tbap, start, end;
00495 uint8_t *exps;
00496 uint8_t *bap;
00497 float *coeffs;
00498
00499 exps = s->dexps[ch_index];
00500 bap = s->bap[ch_index];
00501 coeffs = s->transform_coeffs[ch_index];
00502 start = s->start_freq[ch_index];
00503 end = s->end_freq[ch_index];
00504
00505 for (i = start; i < end; i++) {
00506 tbap = bap[i];
00507 switch (tbap) {
00508 case 0:
00509 coeffs[i] = ((av_random(&s->dith_state) & 0xFFFF) / 65535.0f) - 0.5f;
00510 break;
00511
00512 case 1:
00513 if(m->b1ptr > 2) {
00514 gcode = get_bits(gbc, 5);
00515 m->b1_mant[0] = b1_mantissas[gcode][0];
00516 m->b1_mant[1] = b1_mantissas[gcode][1];
00517 m->b1_mant[2] = b1_mantissas[gcode][2];
00518 m->b1ptr = 0;
00519 }
00520 coeffs[i] = m->b1_mant[m->b1ptr++];
00521 break;
00522
00523 case 2:
00524 if(m->b2ptr > 2) {
00525 gcode = get_bits(gbc, 7);
00526 m->b2_mant[0] = b2_mantissas[gcode][0];
00527 m->b2_mant[1] = b2_mantissas[gcode][1];
00528 m->b2_mant[2] = b2_mantissas[gcode][2];
00529 m->b2ptr = 0;
00530 }
00531 coeffs[i] = m->b2_mant[m->b2ptr++];
00532 break;
00533
00534 case 3:
00535 coeffs[i] = b3_mantissas[get_bits(gbc, 3)];
00536 break;
00537
00538 case 4:
00539 if(m->b4ptr > 1) {
00540 gcode = get_bits(gbc, 7);
00541 m->b4_mant[0] = b4_mantissas[gcode][0];
00542 m->b4_mant[1] = b4_mantissas[gcode][1];
00543 m->b4ptr = 0;
00544 }
00545 coeffs[i] = m->b4_mant[m->b4ptr++];
00546 break;
00547
00548 case 5:
00549 coeffs[i] = b5_mantissas[get_bits(gbc, 4)];
00550 break;
00551
00552 default:
00553
00554 coeffs[i] = get_sbits(gbc, quantization_tab[tbap]) * scale_factors[quantization_tab[tbap]-1];
00555 break;
00556 }
00557 coeffs[i] *= scale_factors[exps[i]];
00558 }
00559
00560 return 0;
00561 }
00562
00567 static void remove_dithering(AC3DecodeContext *s) {
00568 int ch, i;
00569 int end=0;
00570 float *coeffs;
00571 uint8_t *bap;
00572
00573 for(ch=1; ch<=s->fbw_channels; ch++) {
00574 if(!s->dither_flag[ch]) {
00575 coeffs = s->transform_coeffs[ch];
00576 bap = s->bap[ch];
00577 if(s->channel_in_cpl[ch])
00578 end = s->start_freq[CPL_CH];
00579 else
00580 end = s->end_freq[ch];
00581 for(i=0; i<end; i++) {
00582 if(!bap[i])
00583 coeffs[i] = 0.0f;
00584 }
00585 if(s->channel_in_cpl[ch]) {
00586 bap = s->bap[CPL_CH];
00587 for(; i<s->end_freq[CPL_CH]; i++) {
00588 if(!bap[i])
00589 coeffs[i] = 0.0f;
00590 }
00591 }
00592 }
00593 }
00594 }
00595
00599 static int get_transform_coeffs(AC3DecodeContext *s)
00600 {
00601 int ch, end;
00602 int got_cplchan = 0;
00603 mant_groups m;
00604
00605 m.b1ptr = m.b2ptr = m.b4ptr = 3;
00606
00607 for (ch = 1; ch <= s->channels; ch++) {
00608
00609 if (get_transform_coeffs_ch(s, ch, &m))
00610 return -1;
00611
00612
00613 if (s->channel_in_cpl[ch]) {
00614 if (!got_cplchan) {
00615 if (get_transform_coeffs_ch(s, CPL_CH, &m)) {
00616 av_log(s->avctx, AV_LOG_ERROR, "error in decoupling channels\n");
00617 return -1;
00618 }
00619 uncouple_channels(s);
00620 got_cplchan = 1;
00621 }
00622 end = s->end_freq[CPL_CH];
00623 } else {
00624 end = s->end_freq[ch];
00625 }
00626 do
00627 s->transform_coeffs[ch][end] = 0;
00628 while(++end < 256);
00629 }
00630
00631
00632 if(!s->dither_all)
00633 remove_dithering(s);
00634
00635 return 0;
00636 }
00637
00642 static void do_rematrixing(AC3DecodeContext *s)
00643 {
00644 int bnd, i;
00645 int end, bndend;
00646 float tmp0, tmp1;
00647
00648 end = FFMIN(s->end_freq[1], s->end_freq[2]);
00649
00650 for(bnd=0; bnd<s->num_rematrixing_bands; bnd++) {
00651 if(s->rematrixing_flags[bnd]) {
00652 bndend = FFMIN(end, rematrix_band_tab[bnd+1]);
00653 for(i=rematrix_band_tab[bnd]; i<bndend; i++) {
00654 tmp0 = s->transform_coeffs[1][i];
00655 tmp1 = s->transform_coeffs[2][i];
00656 s->transform_coeffs[1][i] = tmp0 + tmp1;
00657 s->transform_coeffs[2][i] = tmp0 - tmp1;
00658 }
00659 }
00660 }
00661 }
00662
00666 static void do_imdct_256(AC3DecodeContext *s, int chindex)
00667 {
00668 int i, k;
00669 DECLARE_ALIGNED_16(float, x[128]);
00670 FFTComplex z[2][64];
00671 float *o_ptr = s->tmp_output;
00672
00673 for(i=0; i<2; i++) {
00674
00675 for(k=0; k<128; k++) {
00676 x[k] = s->transform_coeffs[chindex][2*k+i];
00677 }
00678
00679
00680 s->imdct_256.fft.imdct_calc(&s->imdct_256, o_ptr, x, s->tmp_imdct);
00681
00682
00683 for(k=0; k<32; k++) {
00684 z[i][32+k].re = -o_ptr[128+2*k];
00685 z[i][32+k].im = -o_ptr[2*k];
00686 z[i][31-k].re = o_ptr[2*k+1];
00687 z[i][31-k].im = o_ptr[128+2*k+1];
00688 }
00689 }
00690
00691
00692 for(k=0; k<64; k++) {
00693 o_ptr[ 2*k ] = -z[0][ k].im;
00694 o_ptr[ 2*k+1] = z[0][63-k].re;
00695 o_ptr[128+2*k ] = -z[0][ k].re;
00696 o_ptr[128+2*k+1] = z[0][63-k].im;
00697 o_ptr[256+2*k ] = -z[1][ k].re;
00698 o_ptr[256+2*k+1] = z[1][63-k].im;
00699 o_ptr[384+2*k ] = z[1][ k].im;
00700 o_ptr[384+2*k+1] = -z[1][63-k].re;
00701 }
00702 }
00703
00709 static inline void do_imdct(AC3DecodeContext *s)
00710 {
00711 int ch;
00712 int channels;
00713
00714
00715 channels = s->fbw_channels;
00716 if(s->output_mode & AC3_OUTPUT_LFEON)
00717 channels++;
00718
00719 for (ch=1; ch<=channels; ch++) {
00720 if (s->block_switch[ch]) {
00721 do_imdct_256(s, ch);
00722 } else {
00723 s->imdct_512.fft.imdct_calc(&s->imdct_512, s->tmp_output,
00724 s->transform_coeffs[ch], s->tmp_imdct);
00725 }
00726
00727
00728 s->dsp.vector_fmul_add_add(s->output[ch-1], s->tmp_output,
00729 s->window, s->delay[ch-1], 0, 256, 1);
00730
00731
00732 s->dsp.vector_fmul_reverse(s->delay[ch-1], s->tmp_output+256,
00733 s->window, 256);
00734 }
00735 }
00736
00740 static void ac3_downmix(AC3DecodeContext *s)
00741 {
00742 int i, j;
00743 float v0, v1, s0, s1;
00744
00745 for(i=0; i<256; i++) {
00746 v0 = v1 = s0 = s1 = 0.0f;
00747 for(j=0; j<s->fbw_channels; j++) {
00748 v0 += s->output[j][i] * s->downmix_coeffs[j][0];
00749 v1 += s->output[j][i] * s->downmix_coeffs[j][1];
00750 s0 += s->downmix_coeffs[j][0];
00751 s1 += s->downmix_coeffs[j][1];
00752 }
00753 v0 /= s0;
00754 v1 /= s1;
00755 if(s->output_mode == AC3_CHMODE_MONO) {
00756 s->output[0][i] = (v0 + v1) * LEVEL_MINUS_3DB;
00757 } else if(s->output_mode == AC3_CHMODE_STEREO) {
00758 s->output[0][i] = v0;
00759 s->output[1][i] = v1;
00760 }
00761 }
00762 }
00763
00767 static int ac3_parse_audio_block(AC3DecodeContext *s, int blk)
00768 {
00769 int fbw_channels = s->fbw_channels;
00770 int channel_mode = s->channel_mode;
00771 int i, bnd, seg, ch;
00772 GetBitContext *gbc = &s->gbc;
00773 uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];
00774
00775 memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);
00776
00777
00778 for (ch = 1; ch <= fbw_channels; ch++)
00779 s->block_switch[ch] = get_bits1(gbc);
00780
00781
00782 s->dither_all = 1;
00783 for (ch = 1; ch <= fbw_channels; ch++) {
00784 s->dither_flag[ch] = get_bits1(gbc);
00785 if(!s->dither_flag[ch])
00786 s->dither_all = 0;
00787 }
00788
00789
00790 i = !(s->channel_mode);
00791 do {
00792 if(get_bits1(gbc)) {
00793 s->dynamic_range[i] = ((dynamic_range_tab[get_bits(gbc, 8)]-1.0) *
00794 s->avctx->drc_scale)+1.0;
00795 } else if(blk == 0) {
00796 s->dynamic_range[i] = 1.0f;
00797 }
00798 } while(i--);
00799
00800
00801 if (get_bits1(gbc)) {
00802 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
00803 s->cpl_in_use = get_bits1(gbc);
00804 if (s->cpl_in_use) {
00805
00806 int cpl_begin_freq, cpl_end_freq;
00807
00808
00809 for (ch = 1; ch <= fbw_channels; ch++)
00810 s->channel_in_cpl[ch] = get_bits1(gbc);
00811
00812
00813 if (channel_mode == AC3_CHMODE_STEREO)
00814 s->phase_flags_in_use = get_bits1(gbc);
00815
00816
00817 cpl_begin_freq = get_bits(gbc, 4);
00818 cpl_end_freq = get_bits(gbc, 4);
00819 if (3 + cpl_end_freq - cpl_begin_freq < 0) {
00820 av_log(s->avctx, AV_LOG_ERROR, "3+cplendf = %d < cplbegf = %d\n", 3+cpl_end_freq, cpl_begin_freq);
00821 return -1;
00822 }
00823 s->num_cpl_bands = s->num_cpl_subbands = 3 + cpl_end_freq - cpl_begin_freq;
00824 s->start_freq[CPL_CH] = cpl_begin_freq * 12 + 37;
00825 s->end_freq[CPL_CH] = cpl_end_freq * 12 + 73;
00826 for (bnd = 0; bnd < s->num_cpl_subbands - 1; bnd++) {
00827 if (get_bits1(gbc)) {
00828 s->cpl_band_struct[bnd] = 1;
00829 s->num_cpl_bands--;
00830 }
00831 }
00832 s->cpl_band_struct[s->num_cpl_subbands-1] = 0;
00833 } else {
00834
00835 for (ch = 1; ch <= fbw_channels; ch++)
00836 s->channel_in_cpl[ch] = 0;
00837 }
00838 }
00839
00840
00841 if (s->cpl_in_use) {
00842 int cpl_coords_exist = 0;
00843
00844 for (ch = 1; ch <= fbw_channels; ch++) {
00845 if (s->channel_in_cpl[ch]) {
00846 if (get_bits1(gbc)) {
00847 int master_cpl_coord, cpl_coord_exp, cpl_coord_mant;
00848 cpl_coords_exist = 1;
00849 master_cpl_coord = 3 * get_bits(gbc, 2);
00850 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
00851 cpl_coord_exp = get_bits(gbc, 4);
00852 cpl_coord_mant = get_bits(gbc, 4);
00853 if (cpl_coord_exp == 15)
00854 s->cpl_coords[ch][bnd] = cpl_coord_mant / 16.0f;
00855 else
00856 s->cpl_coords[ch][bnd] = (cpl_coord_mant + 16.0f) / 32.0f;
00857 s->cpl_coords[ch][bnd] *= scale_factors[cpl_coord_exp + master_cpl_coord];
00858 }
00859 }
00860 }
00861 }
00862
00863 if (channel_mode == AC3_CHMODE_STEREO && cpl_coords_exist) {
00864 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
00865 s->phase_flags[bnd] = s->phase_flags_in_use? get_bits1(gbc) : 0;
00866 }
00867 }
00868 }
00869
00870
00871 if (channel_mode == AC3_CHMODE_STEREO) {
00872 if (get_bits1(gbc)) {
00873 s->num_rematrixing_bands = 4;
00874 if(s->cpl_in_use && s->start_freq[CPL_CH] <= 61)
00875 s->num_rematrixing_bands -= 1 + (s->start_freq[CPL_CH] == 37);
00876 for(bnd=0; bnd<s->num_rematrixing_bands; bnd++)
00877 s->rematrixing_flags[bnd] = get_bits1(gbc);
00878 }
00879 }
00880
00881
00882 s->exp_strategy[CPL_CH] = EXP_REUSE;
00883 s->exp_strategy[s->lfe_ch] = EXP_REUSE;
00884 for (ch = !s->cpl_in_use; ch <= s->channels; ch++) {
00885 if(ch == s->lfe_ch)
00886 s->exp_strategy[ch] = get_bits(gbc, 1);
00887 else
00888 s->exp_strategy[ch] = get_bits(gbc, 2);
00889 if(s->exp_strategy[ch] != EXP_REUSE)
00890 bit_alloc_stages[ch] = 3;
00891 }
00892
00893
00894 for (ch = 1; ch <= fbw_channels; ch++) {
00895 s->start_freq[ch] = 0;
00896 if (s->exp_strategy[ch] != EXP_REUSE) {
00897 int prev = s->end_freq[ch];
00898 if (s->channel_in_cpl[ch])
00899 s->end_freq[ch] = s->start_freq[CPL_CH];
00900 else {
00901 int bandwidth_code = get_bits(gbc, 6);
00902 if (bandwidth_code > 60) {
00903 av_log(s->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60", bandwidth_code);
00904 return -1;
00905 }
00906 s->end_freq[ch] = bandwidth_code * 3 + 73;
00907 }
00908 if(blk > 0 && s->end_freq[ch] != prev)
00909 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
00910 }
00911 }
00912 s->start_freq[s->lfe_ch] = 0;
00913 s->end_freq[s->lfe_ch] = 7;
00914
00915
00916 for (ch = !s->cpl_in_use; ch <= s->channels; ch++) {
00917 if (s->exp_strategy[ch] != EXP_REUSE) {
00918 int group_size, num_groups;
00919 group_size = 3 << (s->exp_strategy[ch] - 1);
00920 if(ch == CPL_CH)
00921 num_groups = (s->end_freq[ch] - s->start_freq[ch]) / group_size;
00922 else if(ch == s->lfe_ch)
00923 num_groups = 2;
00924 else
00925 num_groups = (s->end_freq[ch] + group_size - 4) / group_size;
00926 s->dexps[ch][0] = get_bits(gbc, 4) << !ch;
00927 decode_exponents(gbc, s->exp_strategy[ch], num_groups, s->dexps[ch][0],
00928 &s->dexps[ch][s->start_freq[ch]+!!ch]);
00929 if(ch != CPL_CH && ch != s->lfe_ch)
00930 skip_bits(gbc, 2);
00931 }
00932 }
00933
00934
00935 if (get_bits1(gbc)) {
00936 s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
00937 s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
00938 s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab[get_bits(gbc, 2)];
00939 s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[get_bits(gbc, 2)];
00940 s->bit_alloc_params.floor = ff_ac3_floor_tab[get_bits(gbc, 3)];
00941 for(ch=!s->cpl_in_use; ch<=s->channels; ch++) {
00942 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
00943 }
00944 }
00945
00946
00947 if (get_bits1(gbc)) {
00948 int csnr;
00949 csnr = (get_bits(gbc, 6) - 15) << 4;
00950 for (ch = !s->cpl_in_use; ch <= s->channels; ch++) {
00951 s->snr_offset[ch] = (csnr + get_bits(gbc, 4)) << 2;
00952 s->fast_gain[ch] = ff_ac3_fast_gain_tab[get_bits(gbc, 3)];
00953 }
00954 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
00955 }
00956
00957
00958 if (s->cpl_in_use && get_bits1(gbc)) {
00959 s->bit_alloc_params.cpl_fast_leak = get_bits(gbc, 3);
00960 s->bit_alloc_params.cpl_slow_leak = get_bits(gbc, 3);
00961 bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
00962 }
00963
00964
00965 if (get_bits1(gbc)) {
00966
00967 for (ch = !s->cpl_in_use; ch <= fbw_channels; ch++) {
00968 s->dba_mode[ch] = get_bits(gbc, 2);
00969 if (s->dba_mode[ch] == DBA_RESERVED) {
00970 av_log(s->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
00971 return -1;
00972 }
00973 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
00974 }
00975
00976 for (ch = !s->cpl_in_use; ch <= fbw_channels; ch++) {
00977 if (s->dba_mode[ch] == DBA_NEW) {
00978 s->dba_nsegs[ch] = get_bits(gbc, 3);
00979 for (seg = 0; seg <= s->dba_nsegs[ch]; seg++) {
00980 s->dba_offsets[ch][seg] = get_bits(gbc, 5);
00981 s->dba_lengths[ch][seg] = get_bits(gbc, 4);
00982 s->dba_values[ch][seg] = get_bits(gbc, 3);
00983 }
00984 }
00985 }
00986 } else if(blk == 0) {
00987 for(ch=0; ch<=s->channels; ch++) {
00988 s->dba_mode[ch] = DBA_NONE;
00989 }
00990 }
00991
00992
00993 for(ch=!s->cpl_in_use; ch<=s->channels; ch++) {
00994 if(bit_alloc_stages[ch] > 2) {
00995
00996 ff_ac3_bit_alloc_calc_psd(s->dexps[ch],
00997 s->start_freq[ch], s->end_freq[ch],
00998 s->psd[ch], s->band_psd[ch]);
00999 }
01000 if(bit_alloc_stages[ch] > 1) {
01001
01002
01003 ff_ac3_bit_alloc_calc_mask(&s->bit_alloc_params, s->band_psd[ch],
01004 s->start_freq[ch], s->end_freq[ch],
01005 s->fast_gain[ch], (ch == s->lfe_ch),
01006 s->dba_mode[ch], s->dba_nsegs[ch],
01007 s->dba_offsets[ch], s->dba_lengths[ch],
01008 s->dba_values[ch], s->mask[ch]);
01009 }
01010 if(bit_alloc_stages[ch] > 0) {
01011
01012 ff_ac3_bit_alloc_calc_bap(s->mask[ch], s->psd[ch],
01013 s->start_freq[ch], s->end_freq[ch],
01014 s->snr_offset[ch],
01015 s->bit_alloc_params.floor,
01016 s->bap[ch]);
01017 }
01018 }
01019
01020
01021 if (get_bits1(gbc)) {
01022 int skipl = get_bits(gbc, 9);
01023 while(skipl--)
01024 skip_bits(gbc, 8);
01025 }
01026
01027
01028
01029 if (get_transform_coeffs(s)) {
01030 av_log(s->avctx, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
01031 return -1;
01032 }
01033
01034
01035 if(s->channel_mode == AC3_CHMODE_STEREO)
01036 do_rematrixing(s);
01037
01038
01039 for(ch=1; ch<=s->channels; ch++) {
01040 float gain = 2.0f * s->mul_bias;
01041 if(s->channel_mode == AC3_CHMODE_DUALMONO) {
01042 gain *= s->dynamic_range[ch-1];
01043 } else {
01044 gain *= s->dynamic_range[0];
01045 }
01046 for(i=0; i<s->end_freq[ch]; i++) {
01047 s->transform_coeffs[ch][i] *= gain;
01048 }
01049 }
01050
01051 do_imdct(s);
01052
01053
01054 if(s->channels != s->out_channels && !((s->output_mode & AC3_OUTPUT_LFEON) &&
01055 s->fbw_channels == s->out_channels)) {
01056 ac3_downmix(s);
01057 }
01058
01059
01060 for(ch=0; ch<s->out_channels; ch++) {
01061 for(i=0; i<256; i++) {
01062 s->output[ch][i] += s->add_bias;
01063 }
01064 s->dsp.float_to_int16(s->int_output[ch], s->output[ch], 256);
01065 }
01066
01067 return 0;
01068 }
01069
01073 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
01074 {
01075 AC3DecodeContext *s = avctx->priv_data;
01076 int16_t *out_samples = (int16_t *)data;
01077 int i, blk, ch, err;
01078
01079
01080 init_get_bits(&s->gbc, buf, buf_size * 8);
01081
01082
01083 err = ac3_parse_header(s);
01084 if(err) {
01085 switch(err) {
01086 case AC3_PARSE_ERROR_SYNC:
01087 av_log(avctx, AV_LOG_ERROR, "frame sync error\n");
01088 break;
01089 case AC3_PARSE_ERROR_BSID:
01090 av_log(avctx, AV_LOG_ERROR, "invalid bitstream id\n");
01091 break;
01092 case AC3_PARSE_ERROR_SAMPLE_RATE:
01093 av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
01094 break;
01095 case AC3_PARSE_ERROR_FRAME_SIZE:
01096 av_log(avctx, AV_LOG_ERROR, "invalid frame size\n");
01097 break;
01098 default:
01099 av_log(avctx, AV_LOG_ERROR, "invalid header\n");
01100 break;
01101 }
01102 return -1;
01103 }
01104
01105
01106 if(s->frame_size > buf_size) {
01107 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
01108 return -1;
01109 }
01110
01111
01112 if(avctx->error_resilience >= FF_ER_CAREFUL) {
01113 if(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, &buf[2], s->frame_size-2)) {
01114 av_log(avctx, AV_LOG_ERROR, "frame CRC mismatch\n");
01115 return -1;
01116 }
01117
01118 }
01119
01120 avctx->sample_rate = s->sample_rate;
01121 avctx->bit_rate = s->bit_rate;
01122
01123
01124 s->out_channels = s->channels;
01125 if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
01126 avctx->request_channels < s->channels) {
01127 s->out_channels = avctx->request_channels;
01128 s->output_mode = avctx->request_channels == 1 ? AC3_CHMODE_MONO : AC3_CHMODE_STEREO;
01129 }
01130 avctx->channels = s->out_channels;
01131
01132
01133 if(s->channels != s->out_channels && !((s->output_mode & AC3_OUTPUT_LFEON) &&
01134 s->fbw_channels == s->out_channels)) {
01135 set_downmix_coeffs(s);
01136 }
01137
01138
01139 for (blk = 0; blk < NB_BLOCKS; blk++) {
01140 if (ac3_parse_audio_block(s, blk)) {
01141 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
01142 *data_size = 0;
01143 return s->frame_size;
01144 }
01145 for (i = 0; i < 256; i++)
01146 for (ch = 0; ch < s->out_channels; ch++)
01147 *(out_samples++) = s->int_output[ch][i];
01148 }
01149 *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t);
01150 return s->frame_size;
01151 }
01152
01156 static int ac3_decode_end(AVCodecContext *avctx)
01157 {
01158 AC3DecodeContext *s = avctx->priv_data;
01159 ff_mdct_end(&s->imdct_512);
01160 ff_mdct_end(&s->imdct_256);
01161
01162 return 0;
01163 }
01164
01165 AVCodec ac3_decoder = {
01166 .name = "ac3",
01167 .type = CODEC_TYPE_AUDIO,
01168 .id = CODEC_ID_AC3,
01169 .priv_data_size = sizeof (AC3DecodeContext),
01170 .init = ac3_decode_init,
01171 .close = ac3_decode_end,
01172 .decode = ac3_decode_frame,
01173 };