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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "avcodec.h"
00048 #include "internal.h"
00049 #include "aac_ac3_parser.h"
00050 #include "ac3.h"
00051 #include "ac3_parser.h"
00052 #include "ac3dec.h"
00053 #include "ac3dec_data.h"
00054 #include "eac3dec_data.h"
00055
00057 typedef enum {
00058 EAC3_GAQ_NO =0,
00059 EAC3_GAQ_12,
00060 EAC3_GAQ_14,
00061 EAC3_GAQ_124
00062 } EAC3GaqMode;
00063
00064 #define EAC3_SR_CODE_REDUCED 3
00065
00066 void ff_eac3_apply_spectral_extension(AC3DecodeContext *s)
00067 {
00068 int bin, bnd, ch, i;
00069 uint8_t wrapflag[SPX_MAX_BANDS]={1,0,}, num_copy_sections, copy_sizes[SPX_MAX_BANDS];
00070 float rms_energy[SPX_MAX_BANDS];
00071
00072
00073
00074 bin = s->spx_dst_start_freq;
00075 num_copy_sections = 0;
00076 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
00077 int copysize;
00078 int bandsize = s->spx_band_sizes[bnd];
00079 if (bin + bandsize > s->spx_src_start_freq) {
00080 copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
00081 bin = s->spx_dst_start_freq;
00082 wrapflag[bnd] = 1;
00083 }
00084 for (i = 0; i < bandsize; i += copysize) {
00085 if (bin == s->spx_src_start_freq) {
00086 copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
00087 bin = s->spx_dst_start_freq;
00088 }
00089 copysize = FFMIN(bandsize - i, s->spx_src_start_freq - bin);
00090 bin += copysize;
00091 }
00092 }
00093 copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
00094
00095 for (ch = 1; ch <= s->fbw_channels; ch++) {
00096 if (!s->channel_uses_spx[ch])
00097 continue;
00098
00099
00100 bin = s->spx_src_start_freq;
00101 for (i = 0; i < num_copy_sections; i++) {
00102 memcpy(&s->transform_coeffs[ch][bin],
00103 &s->transform_coeffs[ch][s->spx_dst_start_freq],
00104 copy_sizes[i]*sizeof(float));
00105 bin += copy_sizes[i];
00106 }
00107
00108
00109 bin = s->spx_src_start_freq;
00110 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
00111 int bandsize = s->spx_band_sizes[bnd];
00112 float accum = 0.0f;
00113 for (i = 0; i < bandsize; i++) {
00114 float coeff = s->transform_coeffs[ch][bin++];
00115 accum += coeff * coeff;
00116 }
00117 rms_energy[bnd] = sqrtf(accum / bandsize);
00118 }
00119
00120
00121
00122 if (s->spx_atten_code[ch] >= 0) {
00123 const float *atten_tab = ff_eac3_spx_atten_tab[s->spx_atten_code[ch]];
00124 bin = s->spx_src_start_freq - 2;
00125 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
00126 if (wrapflag[bnd]) {
00127 float *coeffs = &s->transform_coeffs[ch][bin];
00128 coeffs[0] *= atten_tab[0];
00129 coeffs[1] *= atten_tab[1];
00130 coeffs[2] *= atten_tab[2];
00131 coeffs[3] *= atten_tab[1];
00132 coeffs[4] *= atten_tab[0];
00133 }
00134 bin += s->spx_band_sizes[bnd];
00135 }
00136 }
00137
00138
00139
00140
00141 bin = s->spx_src_start_freq;
00142 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
00143 float nscale = s->spx_noise_blend[ch][bnd] * rms_energy[bnd] * (1.0f/(1<<31));
00144 float sscale = s->spx_signal_blend[ch][bnd];
00145 for (i = 0; i < s->spx_band_sizes[bnd]; i++) {
00146 float noise = nscale * (int32_t)av_lfg_get(&s->dith_state);
00147 s->transform_coeffs[ch][bin] *= sscale;
00148 s->transform_coeffs[ch][bin++] += noise;
00149 }
00150 }
00151 }
00152 }
00153
00154
00156 #define COEFF_0 10273905LL
00157
00159 #define COEFF_1 11863283LL
00160
00162 #define COEFF_2 3070444LL
00163
00168 static void idct6(int pre_mant[6])
00169 {
00170 int tmp;
00171 int even0, even1, even2, odd0, odd1, odd2;
00172
00173 odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5];
00174
00175 even2 = ( pre_mant[2] * COEFF_0) >> 23;
00176 tmp = ( pre_mant[4] * COEFF_1) >> 23;
00177 odd0 = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23;
00178
00179 even0 = pre_mant[0] + (tmp >> 1);
00180 even1 = pre_mant[0] - tmp;
00181
00182 tmp = even0;
00183 even0 = tmp + even2;
00184 even2 = tmp - even2;
00185
00186 tmp = odd0;
00187 odd0 = tmp + pre_mant[1] + pre_mant[3];
00188 odd2 = tmp + pre_mant[5] - pre_mant[3];
00189
00190 pre_mant[0] = even0 + odd0;
00191 pre_mant[1] = even1 + odd1;
00192 pre_mant[2] = even2 + odd2;
00193 pre_mant[3] = even2 - odd2;
00194 pre_mant[4] = even1 - odd1;
00195 pre_mant[5] = even0 - odd0;
00196 }
00197
00198 void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
00199 {
00200 int bin, blk, gs;
00201 int end_bap, gaq_mode;
00202 GetBitContext *gbc = &s->gbc;
00203 int gaq_gain[AC3_MAX_COEFS];
00204
00205 gaq_mode = get_bits(gbc, 2);
00206 end_bap = (gaq_mode < 2) ? 12 : 17;
00207
00208
00209
00210 gs = 0;
00211 if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
00212
00213 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
00214 if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap)
00215 gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1);
00216 }
00217 } else if (gaq_mode == EAC3_GAQ_124) {
00218
00219 int gc = 2;
00220 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
00221 if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) {
00222 if (gc++ == 2) {
00223 int group_code = get_bits(gbc, 5);
00224 if (group_code > 26) {
00225 av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n");
00226 group_code = 26;
00227 }
00228 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0];
00229 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1];
00230 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2];
00231 gc = 0;
00232 }
00233 }
00234 }
00235 }
00236
00237 gs=0;
00238 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
00239 int hebap = s->bap[ch][bin];
00240 int bits = ff_eac3_bits_vs_hebap[hebap];
00241 if (!hebap) {
00242
00243 for (blk = 0; blk < 6; blk++) {
00244 s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
00245 }
00246 } else if (hebap < 8) {
00247
00248 int v = get_bits(gbc, bits);
00249 for (blk = 0; blk < 6; blk++) {
00250 s->pre_mantissa[ch][bin][blk] = ff_eac3_mantissa_vq[hebap][v][blk] << 8;
00251 }
00252 } else {
00253
00254 int gbits, log_gain;
00255 if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) {
00256 log_gain = gaq_gain[gs++];
00257 } else {
00258 log_gain = 0;
00259 }
00260 gbits = bits - log_gain;
00261
00262 for (blk = 0; blk < 6; blk++) {
00263 int mant = get_sbits(gbc, gbits);
00264 if (log_gain && mant == -(1 << (gbits-1))) {
00265
00266 int b;
00267 int mbits = bits - (2 - log_gain);
00268 mant = get_sbits(gbc, mbits);
00269 mant <<= (23 - (mbits - 1));
00270
00271 if (mant >= 0)
00272 b = 1 << (23 - log_gain);
00273 else
00274 b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1] << 8;
00275 mant += ((ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (int64_t)mant) >> 15) + b;
00276 } else {
00277
00278 mant <<= 24 - bits;
00279 if (!log_gain) {
00280
00281 mant += (ff_eac3_gaq_remap_1[hebap-8] * (int64_t)mant) >> 15;
00282 }
00283 }
00284 s->pre_mantissa[ch][bin][blk] = mant;
00285 }
00286 }
00287 idct6(s->pre_mantissa[ch][bin]);
00288 }
00289 }
00290
00291 int ff_eac3_parse_header(AC3DecodeContext *s)
00292 {
00293 int i, blk, ch;
00294 int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data;
00295 int parse_transient_proc_info;
00296 int num_cpl_blocks;
00297 GetBitContext *gbc = &s->gbc;
00298
00299
00300
00301
00302 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
00303 av_log_missing_feature(s->avctx, "Dependent substream decoding", 1);
00304 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
00305 } else if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
00306 av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
00307 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
00308 }
00309
00310
00311
00312
00313 if (s->substreamid) {
00314
00315 av_log_missing_feature(s->avctx, "Additional substreams", 1);
00316 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
00317 }
00318
00319 if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {
00320
00321
00322
00323
00324 av_log_missing_feature(s->avctx, "Reduced sampling rates", 1);
00325 return -1;
00326 }
00327 skip_bits(gbc, 5);
00328
00329
00330 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
00331 skip_bits(gbc, 5);
00332 if (get_bits1(gbc)) {
00333 skip_bits(gbc, 8);
00334 }
00335 }
00336
00337
00338 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
00339 if (get_bits1(gbc)) {
00340 skip_bits(gbc, 16);
00341 }
00342 }
00343
00344
00345 if (get_bits1(gbc)) {
00346
00347 if (s->channel_mode > AC3_CHMODE_STEREO) {
00348 skip_bits(gbc, 2);
00349 if (s->channel_mode & 1) {
00350
00351 skip_bits(gbc, 3);
00352 s->center_mix_level = get_bits(gbc, 3);
00353 }
00354 if (s->channel_mode & 4) {
00355
00356 skip_bits(gbc, 3);
00357 s->surround_mix_level = get_bits(gbc, 3);
00358 }
00359 }
00360
00361
00362 if (s->lfe_on && get_bits1(gbc)) {
00363
00364 skip_bits(gbc, 5);
00365 }
00366
00367
00368 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
00369 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
00370
00371 if (get_bits1(gbc)) {
00372 skip_bits(gbc, 6);
00373 }
00374 }
00375 if (get_bits1(gbc)) {
00376 skip_bits(gbc, 6);
00377 }
00378
00379 switch(get_bits(gbc, 2)) {
00380 case 1: skip_bits(gbc, 5); break;
00381 case 2: skip_bits(gbc, 12); break;
00382 case 3: {
00383 int mix_data_size = (get_bits(gbc, 5) + 2) << 3;
00384 skip_bits_long(gbc, mix_data_size);
00385 break;
00386 }
00387 }
00388
00389 if (s->channel_mode < AC3_CHMODE_STEREO) {
00390 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
00391 if (get_bits1(gbc)) {
00392
00393
00394
00395 skip_bits(gbc, 8);
00396 skip_bits(gbc, 6);
00397 }
00398 }
00399 }
00400
00401 if (get_bits1(gbc)) {
00402 for (blk = 0; blk < s->num_blocks; blk++) {
00403 if (s->num_blocks == 1 || get_bits1(gbc)) {
00404 skip_bits(gbc, 5);
00405 }
00406 }
00407 }
00408 }
00409 }
00410
00411
00412 if (get_bits1(gbc)) {
00413 skip_bits(gbc, 3);
00414 skip_bits(gbc, 2);
00415 if (s->channel_mode == AC3_CHMODE_STEREO) {
00416 skip_bits(gbc, 4);
00417 }
00418 if (s->channel_mode >= AC3_CHMODE_2F2R) {
00419 skip_bits(gbc, 2);
00420 }
00421 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
00422 if (get_bits1(gbc)) {
00423 skip_bits(gbc, 8);
00424 }
00425 }
00426 if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
00427 skip_bits1(gbc);
00428 }
00429 }
00430
00431
00432
00433
00434
00435 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
00436 skip_bits1(gbc);
00437 }
00438
00439
00440 if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
00441 (s->num_blocks == 6 || get_bits1(gbc))) {
00442 skip_bits(gbc, 6);
00443 }
00444
00445
00446 if (get_bits1(gbc)) {
00447 int addbsil = get_bits(gbc, 6);
00448 for (i = 0; i < addbsil + 1; i++) {
00449 skip_bits(gbc, 8);
00450 }
00451 }
00452
00453
00454
00455 if (s->num_blocks == 6) {
00456 ac3_exponent_strategy = get_bits1(gbc);
00457 parse_aht_info = get_bits1(gbc);
00458 } else {
00459
00460
00461 ac3_exponent_strategy = 1;
00462 parse_aht_info = 0;
00463 }
00464
00465 s->snr_offset_strategy = get_bits(gbc, 2);
00466 parse_transient_proc_info = get_bits1(gbc);
00467
00468 s->block_switch_syntax = get_bits1(gbc);
00469 if (!s->block_switch_syntax)
00470 memset(s->block_switch, 0, sizeof(s->block_switch));
00471
00472 s->dither_flag_syntax = get_bits1(gbc);
00473 if (!s->dither_flag_syntax) {
00474 for (ch = 1; ch <= s->fbw_channels; ch++)
00475 s->dither_flag[ch] = 1;
00476 }
00477 s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
00478
00479 s->bit_allocation_syntax = get_bits1(gbc);
00480 if (!s->bit_allocation_syntax) {
00481
00482 s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2];
00483 s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1];
00484 s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab [1];
00485 s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2];
00486 s->bit_alloc_params.floor = ff_ac3_floor_tab [7];
00487 }
00488
00489 s->fast_gain_syntax = get_bits1(gbc);
00490 s->dba_syntax = get_bits1(gbc);
00491 s->skip_syntax = get_bits1(gbc);
00492 parse_spx_atten_data = get_bits1(gbc);
00493
00494
00495 num_cpl_blocks = 0;
00496 if (s->channel_mode > 1) {
00497 for (blk = 0; blk < s->num_blocks; blk++) {
00498 s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc));
00499 if (s->cpl_strategy_exists[blk]) {
00500 s->cpl_in_use[blk] = get_bits1(gbc);
00501 } else {
00502 s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
00503 }
00504 num_cpl_blocks += s->cpl_in_use[blk];
00505 }
00506 } else {
00507 memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
00508 }
00509
00510
00511 if (ac3_exponent_strategy) {
00512
00513 for (blk = 0; blk < s->num_blocks; blk++) {
00514 for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) {
00515 s->exp_strategy[blk][ch] = get_bits(gbc, 2);
00516 }
00517 }
00518 } else {
00519
00520 for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
00521 int frmchexpstr = get_bits(gbc, 5);
00522 for (blk = 0; blk < 6; blk++) {
00523 s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
00524 }
00525 }
00526 }
00527
00528 if (s->lfe_on) {
00529 for (blk = 0; blk < s->num_blocks; blk++) {
00530 s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
00531 }
00532 }
00533
00534 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
00535 (s->num_blocks == 6 || get_bits1(gbc))) {
00536 skip_bits(gbc, 5 * s->fbw_channels);
00537 }
00538
00539
00540 if (parse_aht_info) {
00541
00542
00543
00544
00545 s->channel_uses_aht[CPL_CH]=0;
00546 for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
00547 int use_aht = 1;
00548 for (blk = 1; blk < 6; blk++) {
00549 if ((s->exp_strategy[blk][ch] != EXP_REUSE) ||
00550 (!ch && s->cpl_strategy_exists[blk])) {
00551 use_aht = 0;
00552 break;
00553 }
00554 }
00555 s->channel_uses_aht[ch] = use_aht && get_bits1(gbc);
00556 }
00557 } else {
00558 memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
00559 }
00560
00561
00562 if (!s->snr_offset_strategy) {
00563 int csnroffst = (get_bits(gbc, 6) - 15) << 4;
00564 int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
00565 for (ch = 0; ch <= s->channels; ch++)
00566 s->snr_offset[ch] = snroffst;
00567 }
00568
00569
00570 if (parse_transient_proc_info) {
00571 for (ch = 1; ch <= s->fbw_channels; ch++) {
00572 if (get_bits1(gbc)) {
00573 skip_bits(gbc, 10);
00574 skip_bits(gbc, 8);
00575 }
00576 }
00577 }
00578
00579
00580 for (ch = 1; ch <= s->fbw_channels; ch++) {
00581 if (parse_spx_atten_data && get_bits1(gbc)) {
00582 s->spx_atten_code[ch] = get_bits(gbc, 5);
00583 } else {
00584 s->spx_atten_code[ch] = -1;
00585 }
00586 }
00587
00588
00589 if (s->num_blocks > 1 && get_bits1(gbc)) {
00590
00591
00592
00593
00594 int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
00595 skip_bits_long(gbc, block_start_bits);
00596 av_log_missing_feature(s->avctx, "Block start info", 1);
00597 }
00598
00599
00600 for (ch = 1; ch <= s->fbw_channels; ch++) {
00601 s->first_spx_coords[ch] = 1;
00602 s->first_cpl_coords[ch] = 1;
00603 }
00604 s->first_cpl_leak = 1;
00605
00606 return 0;
00607 }