2 * AC-3 encoder & E-AC-3 encoder common header
3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * AC-3 encoder & E-AC-3 encoder common header
28 #ifndef AVCODEC_AC3ENC_H
29 #define AVCODEC_AC3ENC_H
39 #ifndef CONFIG_AC3ENC_FLOAT
40 #define CONFIG_AC3ENC_FLOAT 0
43 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
44 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
46 #define AC3ENC_TYPE_AC3_FIXED 0
47 #define AC3ENC_TYPE_AC3 1
48 #define AC3ENC_TYPE_EAC3 2
50 #if CONFIG_AC3ENC_FLOAT
51 #define AC3_NAME(x) ff_ac3_float_ ## x
52 #define MAC_COEF(d,a,b) ((d)+=(a)*(b))
53 #define COEF_MIN (-16777215.0/16777216.0)
54 #define COEF_MAX ( 16777215.0/16777216.0)
55 typedef float SampleType;
56 typedef float CoefType;
57 typedef float CoefSumType;
59 #define AC3_NAME(x) ff_ac3_fixed_ ## x
60 #define MAC_COEF(d,a,b) MAC64(d,a,b)
61 #define COEF_MIN -16777215
62 #define COEF_MAX 16777215
63 typedef int16_t SampleType;
64 typedef int32_t CoefType;
65 typedef int64_t CoefSumType;
69 typedef struct AC3MDCTContext {
70 const SampleType *window; ///< MDCT window function
71 FFTContext fft; ///< FFT context for MDCT calculation
75 * Encoding Options used by AVOption.
77 typedef struct AC3EncOptions {
78 /* AC-3 metadata options*/
81 float center_mix_level;
82 float surround_mix_level;
83 int dolby_surround_mode;
84 int audio_production_info;
90 int preferred_stereo_downmix;
91 float ltrt_center_mix_level;
92 float ltrt_surround_mix_level;
93 float loro_center_mix_level;
94 float loro_surround_mix_level;
96 int dolby_surround_ex_mode;
97 int dolby_headphone_mode;
98 int ad_converter_type;
100 /* other encoding options */
101 int allow_per_frame_metadata;
102 int stereo_rematrixing;
103 int channel_coupling;
108 * Data for a single audio block.
110 typedef struct AC3Block {
111 CoefType **mdct_coef; ///< MDCT coefficients
112 int32_t **fixed_coef; ///< fixed-point MDCT coefficients
113 uint8_t **exp; ///< original exponents
114 uint8_t **grouped_exp; ///< grouped exponents
115 int16_t **psd; ///< psd per frequency bin
116 int16_t **band_psd; ///< psd per critical band
117 int16_t **mask; ///< masking curve
118 uint16_t **qmant; ///< quantized mantissas
119 uint8_t **cpl_coord_exp; ///< coupling coord exponents (cplcoexp)
120 uint8_t **cpl_coord_mant; ///< coupling coord mantissas (cplcomant)
121 uint8_t coeff_shift[AC3_MAX_CHANNELS]; ///< fixed-point coefficient shift values
122 uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block
123 int num_rematrixing_bands; ///< number of rematrixing bands
124 uint8_t rematrixing_flags[4]; ///< rematrixing flags
125 int new_cpl_strategy; ///< send new coupling strategy
126 int cpl_in_use; ///< coupling in use for this block (cplinu)
127 uint8_t channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling (chincpl)
128 int num_cpl_channels; ///< number of channels in coupling
129 uint8_t new_cpl_coords; ///< send new coupling coordinates (cplcoe)
130 uint8_t cpl_master_exp[AC3_MAX_CHANNELS]; ///< coupling coord master exponents (mstrcplco)
131 int new_snr_offsets; ///< send new SNR offsets
132 int new_cpl_leak; ///< send new coupling leak info
133 int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant)
137 * AC-3 encoder private context.
139 typedef struct AC3EncodeContext {
140 AVClass *av_class; ///< AVClass used for AVOption
141 AC3EncOptions options; ///< encoding options
142 AVCodecContext *avctx; ///< parent AVCodecContext
143 PutBitContext pb; ///< bitstream writer context
145 AC3DSPContext ac3dsp; ///< AC-3 optimized functions
146 AC3MDCTContext *mdct; ///< MDCT context
148 AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info
150 int fixed_point; ///< indicates if fixed-point encoder is being used
151 int eac3; ///< indicates if this is E-AC-3 vs. AC-3
152 int bitstream_id; ///< bitstream id (bsid)
153 int bitstream_mode; ///< bitstream mode (bsmod)
155 int bit_rate; ///< target bit rate, in bits-per-second
156 int sample_rate; ///< sampling frequency, in Hz
158 int frame_size_min; ///< minimum frame size in case rounding is necessary
159 int frame_size; ///< current frame size in bytes
160 int frame_size_code; ///< frame size code (frmsizecod)
162 int64_t bits_written; ///< bit count (used to avg. bitrate)
163 int64_t samples_written; ///< sample count (used to avg. bitrate)
165 int fbw_channels; ///< number of full-bandwidth channels (nfchans)
166 int channels; ///< total number of channels (nchans)
167 int lfe_on; ///< indicates if there is an LFE channel (lfeon)
168 int lfe_channel; ///< channel index of the LFE channel
169 int has_center; ///< indicates if there is a center channel
170 int has_surround; ///< indicates if there are one or more surround channels
171 int channel_mode; ///< channel mode (acmod)
172 const uint8_t *channel_map; ///< channel map used to reorder channels
174 int center_mix_level; ///< center mix level code
175 int surround_mix_level; ///< surround mix level code
176 int ltrt_center_mix_level; ///< Lt/Rt center mix level code
177 int ltrt_surround_mix_level; ///< Lt/Rt surround mix level code
178 int loro_center_mix_level; ///< Lo/Ro center mix level code
179 int loro_surround_mix_level; ///< Lo/Ro surround mix level code
181 int cutoff; ///< user-specified cutoff frequency, in Hz
182 int bandwidth_code; ///< bandwidth code (0 to 60) (chbwcod)
183 int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin (strtmant)
184 int cpl_end_freq; ///< coupling channel end frequency bin
186 int cpl_on; ///< coupling turned on for this frame
187 int cpl_enabled; ///< coupling enabled for all frames
188 int num_cpl_subbands; ///< number of coupling subbands (ncplsubnd)
189 int num_cpl_bands; ///< number of coupling bands (ncplbnd)
190 uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS]; ///< number of coeffs in each coupling band
192 int rematrixing_enabled; ///< stereo rematrixing enabled
194 /* bitrate allocation control */
195 int slow_gain_code; ///< slow gain code (sgaincod)
196 int slow_decay_code; ///< slow decay code (sdcycod)
197 int fast_decay_code; ///< fast decay code (fdcycod)
198 int db_per_bit_code; ///< dB/bit code (dbpbcod)
199 int floor_code; ///< floor code (floorcod)
200 AC3BitAllocParameters bit_alloc; ///< bit allocation parameters
201 int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
202 int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
203 int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
204 int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
205 int frame_bits; ///< all frame bits except exponents and mantissas
206 int exponent_bits; ///< number of bits used for exponents
208 SampleType *windowed_samples;
209 SampleType **planar_samples;
211 uint8_t *bap1_buffer;
212 CoefType *mdct_coef_buffer;
213 int32_t *fixed_coef_buffer;
215 uint8_t *grouped_exp_buffer;
217 int16_t *band_psd_buffer;
218 int16_t *mask_buffer;
219 int16_t *qmant_buffer;
220 uint8_t *cpl_coord_exp_buffer;
221 uint8_t *cpl_coord_mant_buffer;
223 uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
224 uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< reference blocks for EXP_REUSE
225 uint8_t *ref_bap [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< bit allocation pointers (bap)
226 int ref_bap_set; ///< indicates if ref_bap pointers have been set
228 /* fixed vs. float function pointers */
229 void (*mdct_end)(AC3MDCTContext *mdct);
230 int (*mdct_init)(AVCodecContext *avctx, AC3MDCTContext *mdct, int nbits);
232 /* fixed vs. float templated function pointers */
233 int (*allocate_sample_buffers)(struct AC3EncodeContext *s);
235 /* AC-3 vs. E-AC-3 function pointers */
236 void (*output_frame_header)(struct AC3EncodeContext *s);
240 int ff_ac3_encode_init(AVCodecContext *avctx);
242 int ff_ac3_encode_close(AVCodecContext *avctx);
244 int ff_ac3_validate_metadata(AVCodecContext *avctx);
246 void ff_ac3_adjust_frame_size(AC3EncodeContext *s);
248 void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s);
250 void ff_ac3_apply_rematrixing(AC3EncodeContext *s);
252 void ff_ac3_process_exponents(AC3EncodeContext *s);
254 int ff_ac3_compute_bit_allocation(AC3EncodeContext *s);
256 void ff_ac3_quantize_mantissas(AC3EncodeContext *s);
258 void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame);
261 /* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
263 void ff_ac3_fixed_mdct_end(AC3MDCTContext *mdct);
264 void ff_ac3_float_mdct_end(AC3MDCTContext *mdct);
266 int ff_ac3_fixed_mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
268 int ff_ac3_float_mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
272 /* prototypes for functions in ac3enc_template.c */
274 int ff_ac3_fixed_allocate_sample_buffers(AC3EncodeContext *s);
275 int ff_ac3_float_allocate_sample_buffers(AC3EncodeContext *s);
277 int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, unsigned char *frame,
278 int buf_size, void *data);
279 int ff_ac3_float_encode_frame(AVCodecContext *avctx, unsigned char *frame,
280 int buf_size, void *data);
282 #endif /* AVCODEC_AC3ENC_H */