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 typedef float SampleType;
54 typedef float CoefType;
55 typedef float CoefSumType;
57 #define AC3_NAME(x) ff_ac3_fixed_ ## x
58 #define MAC_COEF(d,a,b) MAC64(d,a,b)
59 typedef int16_t SampleType;
60 typedef int32_t CoefType;
61 typedef int64_t CoefSumType;
65 typedef struct AC3MDCTContext {
66 const SampleType *window; ///< MDCT window function
67 FFTContext fft; ///< FFT context for MDCT calculation
71 * Encoding Options used by AVOption.
73 typedef struct AC3EncOptions {
74 /* AC-3 metadata options*/
77 float center_mix_level;
78 float surround_mix_level;
79 int dolby_surround_mode;
80 int audio_production_info;
86 int preferred_stereo_downmix;
87 float ltrt_center_mix_level;
88 float ltrt_surround_mix_level;
89 float loro_center_mix_level;
90 float loro_surround_mix_level;
92 int dolby_surround_ex_mode;
93 int dolby_headphone_mode;
94 int ad_converter_type;
96 /* other encoding options */
97 int allow_per_frame_metadata;
98 int stereo_rematrixing;
104 * Data for a single audio block.
106 typedef struct AC3Block {
107 CoefType **mdct_coef; ///< MDCT coefficients
108 int32_t **fixed_coef; ///< fixed-point MDCT coefficients
109 uint8_t **exp; ///< original exponents
110 uint8_t **grouped_exp; ///< grouped exponents
111 int16_t **psd; ///< psd per frequency bin
112 int16_t **band_psd; ///< psd per critical band
113 int16_t **mask; ///< masking curve
114 uint16_t **qmant; ///< quantized mantissas
115 uint8_t **cpl_coord_exp; ///< coupling coord exponents (cplcoexp)
116 uint8_t **cpl_coord_mant; ///< coupling coord mantissas (cplcomant)
117 uint8_t coeff_shift[AC3_MAX_CHANNELS]; ///< fixed-point coefficient shift values
118 uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block
119 int num_rematrixing_bands; ///< number of rematrixing bands
120 uint8_t rematrixing_flags[4]; ///< rematrixing flags
121 int new_cpl_strategy; ///< send new coupling strategy
122 int cpl_in_use; ///< coupling in use for this block (cplinu)
123 uint8_t channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling (chincpl)
124 int num_cpl_channels; ///< number of channels in coupling
125 uint8_t new_cpl_coords; ///< send new coupling coordinates (cplcoe)
126 uint8_t cpl_master_exp[AC3_MAX_CHANNELS]; ///< coupling coord master exponents (mstrcplco)
127 int new_snr_offsets; ///< send new SNR offsets
128 int new_cpl_leak; ///< send new coupling leak info
129 int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant)
133 * AC-3 encoder private context.
135 typedef struct AC3EncodeContext {
136 AVClass *av_class; ///< AVClass used for AVOption
137 AC3EncOptions options; ///< encoding options
138 AVCodecContext *avctx; ///< parent AVCodecContext
139 PutBitContext pb; ///< bitstream writer context
141 AC3DSPContext ac3dsp; ///< AC-3 optimized functions
142 AC3MDCTContext *mdct; ///< MDCT context
144 AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info
146 int fixed_point; ///< indicates if fixed-point encoder is being used
147 int eac3; ///< indicates if this is E-AC-3 vs. AC-3
148 int bitstream_id; ///< bitstream id (bsid)
149 int bitstream_mode; ///< bitstream mode (bsmod)
151 int bit_rate; ///< target bit rate, in bits-per-second
152 int sample_rate; ///< sampling frequency, in Hz
154 int frame_size_min; ///< minimum frame size in case rounding is necessary
155 int frame_size; ///< current frame size in bytes
156 int frame_size_code; ///< frame size code (frmsizecod)
158 int64_t bits_written; ///< bit count (used to avg. bitrate)
159 int64_t samples_written; ///< sample count (used to avg. bitrate)
161 int fbw_channels; ///< number of full-bandwidth channels (nfchans)
162 int channels; ///< total number of channels (nchans)
163 int lfe_on; ///< indicates if there is an LFE channel (lfeon)
164 int lfe_channel; ///< channel index of the LFE channel
165 int has_center; ///< indicates if there is a center channel
166 int has_surround; ///< indicates if there are one or more surround channels
167 int channel_mode; ///< channel mode (acmod)
168 const uint8_t *channel_map; ///< channel map used to reorder channels
170 int center_mix_level; ///< center mix level code
171 int surround_mix_level; ///< surround mix level code
172 int ltrt_center_mix_level; ///< Lt/Rt center mix level code
173 int ltrt_surround_mix_level; ///< Lt/Rt surround mix level code
174 int loro_center_mix_level; ///< Lo/Ro center mix level code
175 int loro_surround_mix_level; ///< Lo/Ro surround mix level code
177 int cutoff; ///< user-specified cutoff frequency, in Hz
178 int bandwidth_code; ///< bandwidth code (0 to 60) (chbwcod)
179 int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin (strtmant)
180 int cpl_end_freq; ///< coupling channel end frequency bin
182 int cpl_on; ///< coupling turned on for this frame
183 int cpl_enabled; ///< coupling enabled for all frames
184 int num_cpl_subbands; ///< number of coupling subbands (ncplsubnd)
185 int num_cpl_bands; ///< number of coupling bands (ncplbnd)
186 uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS]; ///< number of coeffs in each coupling band
188 int rematrixing_enabled; ///< stereo rematrixing enabled
190 /* bitrate allocation control */
191 int slow_gain_code; ///< slow gain code (sgaincod)
192 int slow_decay_code; ///< slow decay code (sdcycod)
193 int fast_decay_code; ///< fast decay code (fdcycod)
194 int db_per_bit_code; ///< dB/bit code (dbpbcod)
195 int floor_code; ///< floor code (floorcod)
196 AC3BitAllocParameters bit_alloc; ///< bit allocation parameters
197 int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
198 int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
199 int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
200 int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
201 int frame_bits; ///< all frame bits except exponents and mantissas
202 int exponent_bits; ///< number of bits used for exponents
204 SampleType *windowed_samples;
205 SampleType **planar_samples;
207 uint8_t *bap1_buffer;
208 CoefType *mdct_coef_buffer;
209 int32_t *fixed_coef_buffer;
211 uint8_t *grouped_exp_buffer;
213 int16_t *band_psd_buffer;
214 int16_t *mask_buffer;
215 int16_t *qmant_buffer;
216 uint8_t *cpl_coord_exp_buffer;
217 uint8_t *cpl_coord_mant_buffer;
219 uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
220 uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< reference blocks for EXP_REUSE
221 uint8_t *ref_bap [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< bit allocation pointers (bap)
222 int ref_bap_set; ///< indicates if ref_bap pointers have been set
224 /* fixed vs. float function pointers */
225 void (*mdct_end)(AC3MDCTContext *mdct);
226 int (*mdct_init)(AVCodecContext *avctx, AC3MDCTContext *mdct, int nbits);
227 void (*apply_window)(DSPContext *dsp, SampleType *output,
228 const SampleType *input, const SampleType *window,
230 int (*normalize_samples)(struct AC3EncodeContext *s);
231 void (*scale_coefficients)(struct AC3EncodeContext *s);
233 /* fixed vs. float templated function pointers */
234 int (*allocate_sample_buffers)(struct AC3EncodeContext *s);
235 void (*deinterleave_input_samples)(struct AC3EncodeContext *s,
236 const SampleType *samples);
237 void (*apply_mdct)(struct AC3EncodeContext *s);
238 void (*apply_channel_coupling)(struct AC3EncodeContext *s);
239 void (*compute_rematrixing_strategy)(struct AC3EncodeContext *s);
241 /* AC-3 vs. E-AC-3 function pointers */
242 void (*output_frame_header)(struct AC3EncodeContext *s);
246 int ff_ac3_encode_init(AVCodecContext *avctx);
248 int ff_ac3_encode_frame(AVCodecContext *avctx, unsigned char *frame,
249 int buf_size, void *data);
251 int ff_ac3_encode_close(AVCodecContext *avctx);
254 /* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
256 void ff_ac3_fixed_mdct_end(AC3MDCTContext *mdct);
257 void ff_ac3_float_mdct_end(AC3MDCTContext *mdct);
259 int ff_ac3_fixed_mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
261 int ff_ac3_float_mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
264 void ff_ac3_fixed_apply_window(DSPContext *dsp, SampleType *output,
265 const SampleType *input,
266 const SampleType *window, unsigned int len);
267 void ff_ac3_float_apply_window(DSPContext *dsp, SampleType *output,
268 const SampleType *input,
269 const SampleType *window, unsigned int len);
271 int ff_ac3_fixed_normalize_samples(AC3EncodeContext *s);
273 void ff_ac3_fixed_scale_coefficients(AC3EncodeContext *s);
274 void ff_ac3_float_scale_coefficients(AC3EncodeContext *s);
277 /* prototypes for functions in ac3enc_template.c */
279 int ff_ac3_fixed_allocate_sample_buffers(AC3EncodeContext *s);
280 int ff_ac3_float_allocate_sample_buffers(AC3EncodeContext *s);
282 void ff_ac3_fixed_deinterleave_input_samples(AC3EncodeContext *s,
283 const SampleType *samples);
284 void ff_ac3_float_deinterleave_input_samples(AC3EncodeContext *s,
285 const SampleType *samples);
287 void ff_ac3_fixed_apply_mdct(AC3EncodeContext *s);
288 void ff_ac3_float_apply_mdct(AC3EncodeContext *s);
290 void ff_ac3_fixed_apply_channel_coupling(AC3EncodeContext *s);
291 void ff_ac3_float_apply_channel_coupling(AC3EncodeContext *s);
293 void ff_ac3_fixed_compute_rematrixing_strategy(AC3EncodeContext *s);
294 void ff_ac3_float_compute_rematrixing_strategy(AC3EncodeContext *s);
296 #endif /* AVCODEC_AC3ENC_H */