• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/eac3dec.c

Go to the documentation of this file.
00001 /*
00002  * E-AC-3 decoder
00003  * Copyright (c) 2007 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
00004  * Copyright (c) 2008 Justin Ruggles
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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     /* if GAQ gain is used, decode gain codes for bins with hebap between
00095        8 and end_bap */
00096     gs = 0;
00097     if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
00098         /* read 1-bit GAQ gain codes */
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         /* read 1.67-bit GAQ gain codes (3 codes in 5 bits) */
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             /* zero-mantissa dithering */
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             /* Vector Quantization */
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             /* Gain Adaptive Quantization */
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                     /* large mantissa */
00152                     int b;
00153                     mant = get_sbits(gbc, bits-2+log_gain) << (26-log_gain-bits);
00154                     /* remap mantissa value to correct for asymmetric quantization */
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                     /* small mantissa, no GAQ, or Gk=1 */
00162                     mant <<= 24 - bits;
00163                     if (!log_gain) {
00164                         /* remap mantissa value for no GAQ or Gk=1 */
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     /* An E-AC-3 stream can have multiple independent streams which the
00184        application can select from. each independent stream can also contain
00185        dependent streams which are used to add or replace channels. */
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     /* The substream id indicates which substream this frame belongs to. each
00195        independent stream has its own substream id, and the dependent streams
00196        associated to an independent stream have matching substream id's. */
00197     if (s->substreamid) {
00198         /* only decode substream with id=0. skip any additional substreams. */
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         /* The E-AC-3 specification does not tell how to handle reduced sample
00205            rates in bit allocation.  The best assumption would be that it is
00206            handled like AC-3 DolbyNet, but we cannot be sure until we have a
00207            sample which utilizes this feature. */
00208         ff_log_missing_feature(s->avctx, "Reduced sampling rates", 1);
00209         return -1;
00210     }
00211     skip_bits(gbc, 5); // skip bitstream id
00212 
00213     /* volume control params */
00214     for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
00215         skip_bits(gbc, 5); // skip dialog normalization
00216         if (get_bits1(gbc)) {
00217             skip_bits(gbc, 8); // skip compression gain word
00218         }
00219     }
00220 
00221     /* dependent stream channel map */
00222     if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
00223         if (get_bits1(gbc)) {
00224             skip_bits(gbc, 16); // skip custom channel map
00225         }
00226     }
00227 
00228     /* mixing metadata */
00229     if (get_bits1(gbc)) {
00230         /* center and surround mix levels */
00231         if (s->channel_mode > AC3_CHMODE_STEREO) {
00232             skip_bits(gbc, 2);  // skip preferred stereo downmix mode
00233             if (s->channel_mode & 1) {
00234                 /* if three front channels exist */
00235                 skip_bits(gbc, 3); //skip Lt/Rt center mix level
00236                 s->center_mix_level = get_bits(gbc, 3);
00237             }
00238             if (s->channel_mode & 4) {
00239                 /* if a surround channel exists */
00240                 skip_bits(gbc, 3); //skip Lt/Rt surround mix level
00241                 s->surround_mix_level = get_bits(gbc, 3);
00242             }
00243         }
00244 
00245         /* lfe mix level */
00246         if (s->lfe_on && get_bits1(gbc)) {
00247             // TODO: use LFE mix level
00248             skip_bits(gbc, 5); // skip LFE mix level code
00249         }
00250 
00251         /* info for mixing with other streams and substreams */
00252         if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
00253             for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
00254                 // TODO: apply program scale factor
00255                 if (get_bits1(gbc)) {
00256                     skip_bits(gbc, 6);  // skip program scale factor
00257                 }
00258             }
00259             if (get_bits1(gbc)) {
00260                 skip_bits(gbc, 6);  // skip external program scale factor
00261             }
00262             /* skip mixing parameter data */
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             /* skip pan information for mono or dual mono source */
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                         /* note: this is not in the ATSC A/52B specification
00277                            reference: ETSI TS 102 366 V1.1.1
00278                                       section: E.1.3.1.25 */
00279                         skip_bits(gbc, 8);  // skip pan mean direction index
00280                         skip_bits(gbc, 6);  // skip reserved paninfo bits
00281                     }
00282                 }
00283             }
00284             /* skip mixing configuration information */
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     /* informational metadata */
00296     if (get_bits1(gbc)) {
00297         skip_bits(gbc, 3); // skip bit stream mode
00298         skip_bits(gbc, 2); // skip copyright bit and original bitstream bit
00299         if (s->channel_mode == AC3_CHMODE_STEREO) {
00300             skip_bits(gbc, 4); // skip Dolby surround and headphone mode
00301         }
00302         if (s->channel_mode >= AC3_CHMODE_2F2R) {
00303             skip_bits(gbc, 2); // skip Dolby surround EX mode
00304         }
00305         for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
00306             if (get_bits1(gbc)) {
00307                 skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type
00308             }
00309         }
00310         if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
00311             skip_bits1(gbc); // skip source sample rate code
00312         }
00313     }
00314 
00315     /* converter synchronization flag
00316        If frames are less than six blocks, this bit should be turned on
00317        once every 6 blocks to indicate the start of a frame set.
00318        reference: RFC 4598, Section 2.1.3  Frame Sets */
00319     if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
00320         skip_bits1(gbc); // skip converter synchronization flag
00321     }
00322 
00323     /* original frame size code if this stream was converted from AC-3 */
00324     if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
00325             (s->num_blocks == 6 || get_bits1(gbc))) {
00326         skip_bits(gbc, 6); // skip frame size code
00327     }
00328 
00329     /* additional bitstream info */
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); // skip additional bit stream info
00334         }
00335     }
00336 
00337     /* audio frame syntax flags, strategy data, and per-frame data */
00338 
00339     if (s->num_blocks == 6) {
00340         ac3_exponent_strategy = get_bits1(gbc);
00341         parse_aht_info        = get_bits1(gbc);
00342     } else {
00343         /* less than 6 blocks, so use AC-3-style exponent strategy syntax, and
00344            do not use AHT */
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         /* set default bit allocation parameters */
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     /* coupling strategy occurance and coupling use per block */
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     /* exponent strategy data */
00395     if (ac3_exponent_strategy) {
00396         /* AC-3-style exponent strategy syntax */
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         /* LUT-based exponent strategy syntax */
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     /* LFE exponent strategy */
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     /* original exponent strategies if this stream was converted from AC-3 */
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); // skip converter channel exponent strategy
00421     }
00422 
00423     /* determine which channels use AHT */
00424     if (parse_aht_info) {
00425         /* For AHT to be used, all non-zero blocks must reuse exponents from
00426            the first block.  Furthermore, for AHT to be used in the coupling
00427            channel, all blocks must use coupling and use the same coupling
00428            strategy. */
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     /* per-frame SNR offset */
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     /* transient pre-noise processing data */
00454     if (parse_transient_proc_info) {
00455         for (ch = 1; ch <= s->fbw_channels; ch++) {
00456             if (get_bits1(gbc)) { // channel in transient processing
00457                 skip_bits(gbc, 10); // skip transient processing location
00458                 skip_bits(gbc, 8);  // skip transient processing length
00459             }
00460         }
00461     }
00462 
00463     /* spectral extension attenuation data */
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)) { // channel has spx attenuation
00468                 skip_bits(gbc, 5); // skip spx attenuation code
00469             }
00470         }
00471     }
00472 
00473     /* block start information */
00474     if (s->num_blocks > 1 && get_bits1(gbc)) {
00475         /* reference: Section E2.3.2.27
00476            nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame)))
00477            The spec does not say what this data is or what it's used for.
00478            It is likely the offset of each block within the frame. */
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     /* syntax state initialization */
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 }

Generated on Tue Nov 4 2014 12:59:21 for ffmpeg by  doxygen 1.7.1