00001 /* 00002 * Common code between AC3 encoder and decoder 00003 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard. 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 /** 00023 * @file ac3.h 00024 * Common code between AC3 encoder and decoder. 00025 */ 00026 00027 #ifndef FFMPEG_AC3_H 00028 #define FFMPEG_AC3_H 00029 00030 #include "ac3tab.h" 00031 00032 #define AC3_MAX_CODED_FRAME_SIZE 3840 /* in bytes */ 00033 #define AC3_MAX_CHANNELS 6 /* including LFE channel */ 00034 00035 #define NB_BLOCKS 6 /* number of PCM blocks inside an AC3 frame */ 00036 #define AC3_FRAME_SIZE (NB_BLOCKS * 256) 00037 00038 /* exponent encoding strategy */ 00039 #define EXP_REUSE 0 00040 #define EXP_NEW 1 00041 00042 #define EXP_D15 1 00043 #define EXP_D25 2 00044 #define EXP_D45 3 00045 00046 /** Delta bit allocation strategy */ 00047 typedef enum { 00048 DBA_REUSE = 0, 00049 DBA_NEW, 00050 DBA_NONE, 00051 DBA_RESERVED 00052 } AC3DeltaStrategy; 00053 00054 /** Channel mode (audio coding mode) */ 00055 typedef enum { 00056 AC3_CHMODE_DUALMONO = 0, 00057 AC3_CHMODE_MONO, 00058 AC3_CHMODE_STEREO, 00059 AC3_CHMODE_3F, 00060 AC3_CHMODE_2F1R, 00061 AC3_CHMODE_3F1R, 00062 AC3_CHMODE_2F2R, 00063 AC3_CHMODE_3F2R 00064 } AC3ChannelMode; 00065 00066 typedef struct AC3BitAllocParameters { 00067 int sr_code; 00068 int sr_shift; 00069 int slow_gain, slow_decay, fast_decay, db_per_bit, floor; 00070 int cpl_fast_leak, cpl_slow_leak; 00071 } AC3BitAllocParameters; 00072 00073 /** 00074 * @struct AC3HeaderInfo 00075 * Coded AC-3 header values up to the lfeon element, plus derived values. 00076 */ 00077 typedef struct { 00078 /** @defgroup coded Coded elements 00079 * @{ 00080 */ 00081 uint16_t sync_word; 00082 uint16_t crc1; 00083 uint8_t sr_code; 00084 uint8_t bitstream_id; 00085 uint8_t channel_mode; 00086 uint8_t lfe_on; 00087 uint8_t frame_type; 00088 int substreamid; ///< substream identification 00089 int center_mix_level; ///< Center mix level index 00090 int surround_mix_level; ///< Surround mix level index 00091 uint16_t channel_map; 00092 int num_blocks; ///< number of audio blocks 00093 /** @} */ 00094 00095 /** @defgroup derived Derived values 00096 * @{ 00097 */ 00098 uint8_t sr_shift; 00099 uint16_t sample_rate; 00100 uint32_t bit_rate; 00101 uint8_t channels; 00102 uint16_t frame_size; 00103 /** @} */ 00104 } AC3HeaderInfo; 00105 00106 typedef enum { 00107 EAC3_FRAME_TYPE_INDEPENDENT = 0, 00108 EAC3_FRAME_TYPE_DEPENDENT, 00109 EAC3_FRAME_TYPE_AC3_CONVERT, 00110 EAC3_FRAME_TYPE_RESERVED 00111 } EAC3FrameType; 00112 00113 void ac3_common_init(void); 00114 00115 /** 00116 * Calculates the log power-spectral density of the input signal. 00117 * This gives a rough estimate of signal power in the frequency domain by using 00118 * the spectral envelope (exponents). The psd is also separately grouped 00119 * into critical bands for use in the calculating the masking curve. 00120 * 128 units in psd = -6 dB. The dbknee parameter in AC3BitAllocParameters 00121 * determines the reference level. 00122 * 00123 * @param[in] exp frequency coefficient exponents 00124 * @param[in] start starting bin location 00125 * @param[in] end ending bin location 00126 * @param[out] psd signal power for each frequency bin 00127 * @param[out] band_psd signal power for each critical band 00128 */ 00129 void ff_ac3_bit_alloc_calc_psd(int8_t *exp, int start, int end, int16_t *psd, 00130 int16_t *band_psd); 00131 00132 /** 00133 * Calculates the masking curve. 00134 * First, the excitation is calculated using parameters in \p s and the signal 00135 * power in each critical band. The excitation is compared with a predefined 00136 * hearing threshold table to produce the masking curve. If delta bit 00137 * allocation information is provided, it is used for adjusting the masking 00138 * curve, usually to give a closer match to a better psychoacoustic model. 00139 * 00140 * @param[in] s adjustable bit allocation parameters 00141 * @param[in] band_psd signal power for each critical band 00142 * @param[in] start starting bin location 00143 * @param[in] end ending bin location 00144 * @param[in] fast_gain fast gain (estimated signal-to-mask ratio) 00145 * @param[in] is_lfe whether or not the channel being processed is the LFE 00146 * @param[in] dba_mode delta bit allocation mode (none, reuse, or new) 00147 * @param[in] dba_nsegs number of delta segments 00148 * @param[in] dba_offsets location offsets for each segment 00149 * @param[in] dba_lengths length of each segment 00150 * @param[in] dba_values delta bit allocation for each segment 00151 * @param[out] mask calculated masking curve 00152 */ 00153 void ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *band_psd, 00154 int start, int end, int fast_gain, int is_lfe, 00155 int dba_mode, int dba_nsegs, uint8_t *dba_offsets, 00156 uint8_t *dba_lengths, uint8_t *dba_values, 00157 int16_t *mask); 00158 00159 /** 00160 * Calculates bit allocation pointers. 00161 * The SNR is the difference between the masking curve and the signal. AC-3 00162 * uses this value for each frequency bin to allocate bits. The \p snroffset 00163 * parameter is a global adjustment to the SNR for all bins. 00164 * 00165 * @param[in] mask masking curve 00166 * @param[in] psd signal power for each frequency bin 00167 * @param[in] start starting bin location 00168 * @param[in] end ending bin location 00169 * @param[in] snr_offset SNR adjustment 00170 * @param[in] floor noise floor 00171 * @param[in] bap_tab look-up table for bit allocation pointers 00172 * @param[out] bap bit allocation pointers 00173 */ 00174 void ff_ac3_bit_alloc_calc_bap(int16_t *mask, int16_t *psd, int start, int end, 00175 int snr_offset, int floor, 00176 const uint8_t *bap_tab, uint8_t *bap); 00177 00178 void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap, 00179 int8_t *exp, int start, int end, 00180 int snr_offset, int fast_gain, int is_lfe, 00181 int dba_mode, int dba_nsegs, 00182 uint8_t *dba_offsets, uint8_t *dba_lengths, 00183 uint8_t *dba_values); 00184 00185 #endif /* FFMPEG_AC3_H */
1.5.1