00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "avcodec.h"
00024 #include "internal.h"
00025 #include "aac_ac3_parser.h"
00026 #include "ac3.h"
00027 #include "ac3_parser.h"
00028 #include "ac3dec.h"
00029 #include "ac3dec_data.h"
00030
00032 typedef enum {
00033 EAC3_GAQ_NO =0,
00034 EAC3_GAQ_12,
00035 EAC3_GAQ_14,
00036 EAC3_GAQ_124
00037 } EAC3GaqMode;
00038
00039 #define EAC3_SR_CODE_REDUCED 3
00040
00042 #define COEFF_0 10273905LL
00043
00045 #define COEFF_1 11863283LL
00046
00048 #define COEFF_2 3070444LL
00049
00054 static void idct6(int pre_mant[6])
00055 {
00056 int tmp;
00057 int even0, even1, even2, odd0, odd1, odd2;
00058
00059 odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5];
00060
00061 even2 = ( pre_mant[2] * COEFF_0) >> 23;
00062 tmp = ( pre_mant[4] * COEFF_1) >> 23;
00063 odd0 = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23;
00064
00065 even0 = pre_mant[0] + (tmp >> 1);
00066 even1 = pre_mant[0] - tmp;
00067
00068 tmp = even0;
00069 even0 = tmp + even2;
00070 even2 = tmp - even2;
00071
00072 tmp = odd0;
00073 odd0 = tmp + pre_mant[1] + pre_mant[3];
00074 odd2 = tmp + pre_mant[5] - pre_mant[3];
00075
00076 pre_mant[0] = even0 + odd0;
00077 pre_mant[1] = even1 + odd1;
00078 pre_mant[2] = even2 + odd2;
00079 pre_mant[3] = even2 - odd2;
00080 pre_mant[4] = even1 - odd1;
00081 pre_mant[5] = even0 - odd0;
00082 }
00083
00084 void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
00085 {
00086 int bin, blk, gs;
00087 int end_bap, gaq_mode;
00088 GetBitContext *gbc = &s->gbc;
00089 int gaq_gain[AC3_MAX_COEFS];
00090
00091 gaq_mode = get_bits(gbc, 2);
00092 end_bap = (gaq_mode < 2) ? 12 : 17;
00093
00094
00095
00096 gs = 0;
00097 if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
00098
00099 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
00100 if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap)
00101 gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1);
00102 }
00103 } else if (gaq_mode == EAC3_GAQ_124) {
00104
00105 int gc = 2;
00106 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
00107 if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) {
00108 if (gc++ == 2) {
00109 int group_code = get_bits(gbc, 5);
00110 if (group_code > 26) {
00111 av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n");
00112 group_code = 26;
00113 }
00114 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0];
00115 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1];
00116 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2];
00117 gc = 0;
00118 }
00119 }
00120 }
00121 }
00122
00123 gs=0;
00124 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
00125 int hebap = s->bap[ch][bin];
00126 int bits = ff_eac3_bits_vs_hebap[hebap];
00127 if (!hebap) {
00128
00129 for (blk = 0; blk < 6; blk++) {
00130 s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
00131 }
00132 } else if (hebap < 8) {
00133
00134 int v = get_bits(gbc, bits);
00135 for (blk = 0; blk < 6; blk++) {
00136 s->pre_mantissa[ch][bin][blk] = ff_eac3_mantissa_vq[hebap][v][blk] << 8;
00137 }
00138 } else {
00139
00140 int gbits, log_gain;
00141 if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) {
00142 log_gain = gaq_gain[gs++];
00143 } else {
00144 log_gain = 0;
00145 }
00146 gbits = bits - log_gain;
00147
00148 for (blk = 0; blk < 6; blk++) {
00149 int mant = get_sbits(gbc, gbits);
00150 if (mant == -(1 << (gbits-1))) {
00151
00152 int b;
00153 mant = get_sbits(gbc, bits-2+log_gain) << (26-log_gain-bits);
00154
00155 if (mant >= 0)
00156 b = 32768 >> (log_gain+8);
00157 else
00158 b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1];
00159 mant += (ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (mant>>8) + b) >> 7;
00160 } else {
00161
00162 mant <<= 24 - bits;
00163 if (!log_gain) {
00164
00165 mant += (ff_eac3_gaq_remap_1[hebap-8] * (mant>>8)) >> 7;
00166 }
00167 }
00168 s->pre_mantissa[ch][bin][blk] = mant;
00169 }
00170 }
00171 idct6(s->pre_mantissa[ch][bin]);
00172 }
00173 }
00174
00175 int ff_eac3_parse_header(AC3DecodeContext *s)
00176 {
00177 int i, blk, ch;
00178 int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data;
00179 int parse_transient_proc_info;
00180 int num_cpl_blocks;
00181 GetBitContext *gbc = &s->gbc;
00182
00183
00184
00185
00186 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
00187 ff_log_missing_feature(s->avctx, "Dependent substream decoding", 1);
00188 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
00189 } else if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
00190 av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
00191 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
00192 }
00193
00194
00195
00196
00197 if (s->substreamid) {
00198
00199 ff_log_missing_feature(s->avctx, "Additional substreams", 1);
00200 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
00201 }
00202
00203 if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {
00204
00205
00206
00207
00208 ff_log_missing_feature(s->avctx, "Reduced sampling rates", 1);
00209 return -1;
00210 }
00211 skip_bits(gbc, 5);
00212
00213
00214 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
00215 skip_bits(gbc, 5);
00216 if (get_bits1(gbc)) {
00217 skip_bits(gbc, 8);
00218 }
00219 }
00220
00221
00222 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
00223 if (get_bits1(gbc)) {
00224 skip_bits(gbc, 16);
00225 }
00226 }
00227
00228
00229 if (get_bits1(gbc)) {
00230
00231 if (s->channel_mode > AC3_CHMODE_STEREO) {
00232 skip_bits(gbc, 2);
00233 if (s->channel_mode & 1) {
00234
00235 skip_bits(gbc, 3);
00236 s->center_mix_level = get_bits(gbc, 3);
00237 }
00238 if (s->channel_mode & 4) {
00239
00240 skip_bits(gbc, 3);
00241 s->surround_mix_level = get_bits(gbc, 3);
00242 }
00243 }
00244
00245
00246 if (s->lfe_on && get_bits1(gbc)) {
00247
00248 skip_bits(gbc, 5);
00249 }
00250
00251
00252 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
00253 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
00254
00255 if (get_bits1(gbc)) {
00256 skip_bits(gbc, 6);
00257 }
00258 }
00259 if (get_bits1(gbc)) {
00260 skip_bits(gbc, 6);
00261 }
00262
00263 switch(get_bits(gbc, 2)) {
00264 case 1: skip_bits(gbc, 5); break;
00265 case 2: skip_bits(gbc, 12); break;
00266 case 3: {
00267 int mix_data_size = (get_bits(gbc, 5) + 2) << 3;
00268 skip_bits_long(gbc, mix_data_size);
00269 break;
00270 }
00271 }
00272
00273 if (s->channel_mode < AC3_CHMODE_STEREO) {
00274 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
00275 if (get_bits1(gbc)) {
00276
00277
00278
00279 skip_bits(gbc, 8);
00280 skip_bits(gbc, 6);
00281 }
00282 }
00283 }
00284
00285 if (get_bits1(gbc)) {
00286 for (blk = 0; blk < s->num_blocks; blk++) {
00287 if (s->num_blocks == 1 || get_bits1(gbc)) {
00288 skip_bits(gbc, 5);
00289 }
00290 }
00291 }
00292 }
00293 }
00294
00295
00296 if (get_bits1(gbc)) {
00297 skip_bits(gbc, 3);
00298 skip_bits(gbc, 2);
00299 if (s->channel_mode == AC3_CHMODE_STEREO) {
00300 skip_bits(gbc, 4);
00301 }
00302 if (s->channel_mode >= AC3_CHMODE_2F2R) {
00303 skip_bits(gbc, 2);
00304 }
00305 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
00306 if (get_bits1(gbc)) {
00307 skip_bits(gbc, 8);
00308 }
00309 }
00310 if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
00311 skip_bits1(gbc);
00312 }
00313 }
00314
00315
00316
00317
00318
00319 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
00320 skip_bits1(gbc);
00321 }
00322
00323
00324 if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
00325 (s->num_blocks == 6 || get_bits1(gbc))) {
00326 skip_bits(gbc, 6);
00327 }
00328
00329
00330 if (get_bits1(gbc)) {
00331 int addbsil = get_bits(gbc, 6);
00332 for (i = 0; i < addbsil + 1; i++) {
00333 skip_bits(gbc, 8);
00334 }
00335 }
00336
00337
00338
00339 if (s->num_blocks == 6) {
00340 ac3_exponent_strategy = get_bits1(gbc);
00341 parse_aht_info = get_bits1(gbc);
00342 } else {
00343
00344
00345 ac3_exponent_strategy = 1;
00346 parse_aht_info = 0;
00347 }
00348
00349 s->snr_offset_strategy = get_bits(gbc, 2);
00350 parse_transient_proc_info = get_bits1(gbc);
00351
00352 s->block_switch_syntax = get_bits1(gbc);
00353 if (!s->block_switch_syntax)
00354 memset(s->block_switch, 0, sizeof(s->block_switch));
00355
00356 s->dither_flag_syntax = get_bits1(gbc);
00357 if (!s->dither_flag_syntax) {
00358 for (ch = 1; ch <= s->fbw_channels; ch++)
00359 s->dither_flag[ch] = 1;
00360 }
00361 s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
00362
00363 s->bit_allocation_syntax = get_bits1(gbc);
00364 if (!s->bit_allocation_syntax) {
00365
00366 s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2];
00367 s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1];
00368 s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab [1];
00369 s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2];
00370 s->bit_alloc_params.floor = ff_ac3_floor_tab [7];
00371 }
00372
00373 s->fast_gain_syntax = get_bits1(gbc);
00374 s->dba_syntax = get_bits1(gbc);
00375 s->skip_syntax = get_bits1(gbc);
00376 parse_spx_atten_data = get_bits1(gbc);
00377
00378
00379 num_cpl_blocks = 0;
00380 if (s->channel_mode > 1) {
00381 for (blk = 0; blk < s->num_blocks; blk++) {
00382 s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc));
00383 if (s->cpl_strategy_exists[blk]) {
00384 s->cpl_in_use[blk] = get_bits1(gbc);
00385 } else {
00386 s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
00387 }
00388 num_cpl_blocks += s->cpl_in_use[blk];
00389 }
00390 } else {
00391 memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
00392 }
00393
00394
00395 if (ac3_exponent_strategy) {
00396
00397 for (blk = 0; blk < s->num_blocks; blk++) {
00398 for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) {
00399 s->exp_strategy[blk][ch] = get_bits(gbc, 2);
00400 }
00401 }
00402 } else {
00403
00404 for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
00405 int frmchexpstr = get_bits(gbc, 5);
00406 for (blk = 0; blk < 6; blk++) {
00407 s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
00408 }
00409 }
00410 }
00411
00412 if (s->lfe_on) {
00413 for (blk = 0; blk < s->num_blocks; blk++) {
00414 s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
00415 }
00416 }
00417
00418 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
00419 (s->num_blocks == 6 || get_bits1(gbc))) {
00420 skip_bits(gbc, 5 * s->fbw_channels);
00421 }
00422
00423
00424 if (parse_aht_info) {
00425
00426
00427
00428
00429 s->channel_uses_aht[CPL_CH]=0;
00430 for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
00431 int use_aht = 1;
00432 for (blk = 1; blk < 6; blk++) {
00433 if ((s->exp_strategy[blk][ch] != EXP_REUSE) ||
00434 (!ch && s->cpl_strategy_exists[blk])) {
00435 use_aht = 0;
00436 break;
00437 }
00438 }
00439 s->channel_uses_aht[ch] = use_aht && get_bits1(gbc);
00440 }
00441 } else {
00442 memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
00443 }
00444
00445
00446 if (!s->snr_offset_strategy) {
00447 int csnroffst = (get_bits(gbc, 6) - 15) << 4;
00448 int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
00449 for (ch = 0; ch <= s->channels; ch++)
00450 s->snr_offset[ch] = snroffst;
00451 }
00452
00453
00454 if (parse_transient_proc_info) {
00455 for (ch = 1; ch <= s->fbw_channels; ch++) {
00456 if (get_bits1(gbc)) {
00457 skip_bits(gbc, 10);
00458 skip_bits(gbc, 8);
00459 }
00460 }
00461 }
00462
00463
00464 if (parse_spx_atten_data) {
00465 ff_log_missing_feature(s->avctx, "Spectral extension attenuation", 1);
00466 for (ch = 1; ch <= s->fbw_channels; ch++) {
00467 if (get_bits1(gbc)) {
00468 skip_bits(gbc, 5);
00469 }
00470 }
00471 }
00472
00473
00474 if (s->num_blocks > 1 && get_bits1(gbc)) {
00475
00476
00477
00478
00479 int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
00480 skip_bits_long(gbc, block_start_bits);
00481 ff_log_missing_feature(s->avctx, "Block start info", 1);
00482 }
00483
00484
00485 for (ch = 1; ch <= s->fbw_channels; ch++) {
00486 s->first_cpl_coords[ch] = 1;
00487 }
00488 s->first_cpl_leak = 1;
00489
00490 return 0;
00491 }