FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ac3enc.h
Go to the documentation of this file.
1 /*
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>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg 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.
12  *
13  * FFmpeg 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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * AC-3 encoder & E-AC-3 encoder common header
26  */
27 
28 #ifndef AVCODEC_AC3ENC_H
29 #define AVCODEC_AC3ENC_H
30 
31 #include <stdint.h>
32 
33 #include "libavutil/float_dsp.h"
34 #include "ac3.h"
35 #include "ac3dsp.h"
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "put_bits.h"
39 #include "fft.h"
40 
41 #ifndef CONFIG_AC3ENC_FLOAT
42 #define CONFIG_AC3ENC_FLOAT 0
43 #endif
44 
45 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
46 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
47 
48 #define AC3ENC_TYPE_AC3_FIXED 0
49 #define AC3ENC_TYPE_AC3 1
50 #define AC3ENC_TYPE_EAC3 2
51 
52 #if CONFIG_AC3ENC_FLOAT
53 #define AC3_NAME(x) ff_ac3_float_ ## x
54 #define MAC_COEF(d,a,b) ((d)+=(a)*(b))
55 #define COEF_MIN (-16777215.0/16777216.0)
56 #define COEF_MAX ( 16777215.0/16777216.0)
57 #define NEW_CPL_COORD_THRESHOLD 0.03
58 typedef float SampleType;
59 typedef float CoefType;
60 typedef float CoefSumType;
61 #else
62 #define AC3_NAME(x) ff_ac3_fixed_ ## x
63 #define MAC_COEF(d,a,b) MAC64(d,a,b)
64 #define COEF_MIN -16777215
65 #define COEF_MAX 16777215
66 #define NEW_CPL_COORD_THRESHOLD 503317
67 typedef int16_t SampleType;
68 typedef int32_t CoefType;
69 typedef int64_t CoefSumType;
70 #endif
71 
72 /* common option values */
73 #define AC3ENC_OPT_NONE -1
74 #define AC3ENC_OPT_AUTO -1
75 #define AC3ENC_OPT_OFF 0
76 #define AC3ENC_OPT_ON 1
77 #define AC3ENC_OPT_NOT_INDICATED 0
78 #define AC3ENC_OPT_MODE_ON 2
79 #define AC3ENC_OPT_MODE_OFF 1
80 
81 /* specific option values */
82 #define AC3ENC_OPT_LARGE_ROOM 1
83 #define AC3ENC_OPT_SMALL_ROOM 2
84 #define AC3ENC_OPT_DOWNMIX_LTRT 1
85 #define AC3ENC_OPT_DOWNMIX_LORO 2
86 #define AC3ENC_OPT_ADCONV_STANDARD 0
87 #define AC3ENC_OPT_ADCONV_HDCD 1
88 
89 
90 /**
91  * Encoding Options used by AVOption.
92  */
93 typedef struct AC3EncOptions {
94  /* AC-3 metadata options*/
104  int original;
117 
118  /* other encoding options */
123 } AC3EncOptions;
124 
125 /**
126  * Data for a single audio block.
127  */
128 typedef struct AC3Block {
129  CoefType **mdct_coef; ///< MDCT coefficients
130  int32_t **fixed_coef; ///< fixed-point MDCT coefficients
131  uint8_t **exp; ///< original exponents
132  uint8_t **grouped_exp; ///< grouped exponents
133  int16_t **psd; ///< psd per frequency bin
134  int16_t **band_psd; ///< psd per critical band
135  int16_t **mask; ///< masking curve
136  uint16_t **qmant; ///< quantized mantissas
137  uint8_t **cpl_coord_exp; ///< coupling coord exponents (cplcoexp)
138  uint8_t **cpl_coord_mant; ///< coupling coord mantissas (cplcomant)
139  uint8_t coeff_shift[AC3_MAX_CHANNELS]; ///< fixed-point coefficient shift values
140  uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block
141  int num_rematrixing_bands; ///< number of rematrixing bands
142  uint8_t rematrixing_flags[4]; ///< rematrixing flags
143  int new_cpl_strategy; ///< send new coupling strategy
144  int cpl_in_use; ///< coupling in use for this block (cplinu)
145  uint8_t channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling (chincpl)
146  int num_cpl_channels; ///< number of channels in coupling
147  uint8_t new_cpl_coords[AC3_MAX_CHANNELS]; ///< send new coupling coordinates (cplcoe)
148  uint8_t cpl_master_exp[AC3_MAX_CHANNELS]; ///< coupling coord master exponents (mstrcplco)
149  int new_snr_offsets; ///< send new SNR offsets
150  int new_cpl_leak; ///< send new coupling leak info
151  int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant)
152 } AC3Block;
153 
154 /**
155  * AC-3 encoder private context.
156  */
157 typedef struct AC3EncodeContext {
158  AVClass *av_class; ///< AVClass used for AVOption
159  AC3EncOptions options; ///< encoding options
160  AVCodecContext *avctx; ///< parent AVCodecContext
161  PutBitContext pb; ///< bitstream writer context
164  AC3DSPContext ac3dsp; ///< AC-3 optimized functions
165  FFTContext mdct; ///< FFT context for MDCT calculation
166  const SampleType *mdct_window; ///< MDCT window function array
167 
168  AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info
169 
170  int fixed_point; ///< indicates if fixed-point encoder is being used
171  int eac3; ///< indicates if this is E-AC-3 vs. AC-3
172  int bitstream_id; ///< bitstream id (bsid)
173  int bitstream_mode; ///< bitstream mode (bsmod)
174 
175  int bit_rate; ///< target bit rate, in bits-per-second
176  int sample_rate; ///< sampling frequency, in Hz
177 
178  int num_blks_code; ///< number of blocks code (numblkscod)
179  int num_blocks; ///< number of blocks per frame
180  int frame_size_min; ///< minimum frame size in case rounding is necessary
181  int frame_size; ///< current frame size in bytes
182  int frame_size_code; ///< frame size code (frmsizecod)
183  uint16_t crc_inv[2];
184  int64_t bits_written; ///< bit count (used to avg. bitrate)
185  int64_t samples_written; ///< sample count (used to avg. bitrate)
186 
187  int fbw_channels; ///< number of full-bandwidth channels (nfchans)
188  int channels; ///< total number of channels (nchans)
189  int lfe_on; ///< indicates if there is an LFE channel (lfeon)
190  int lfe_channel; ///< channel index of the LFE channel
191  int has_center; ///< indicates if there is a center channel
192  int has_surround; ///< indicates if there are one or more surround channels
193  int channel_mode; ///< channel mode (acmod)
194  const uint8_t *channel_map; ///< channel map used to reorder channels
195 
196  int center_mix_level; ///< center mix level code
197  int surround_mix_level; ///< surround mix level code
198  int ltrt_center_mix_level; ///< Lt/Rt center mix level code
199  int ltrt_surround_mix_level; ///< Lt/Rt surround mix level code
200  int loro_center_mix_level; ///< Lo/Ro center mix level code
201  int loro_surround_mix_level; ///< Lo/Ro surround mix level code
202 
203  int cutoff; ///< user-specified cutoff frequency, in Hz
204  int bandwidth_code; ///< bandwidth code (0 to 60) (chbwcod)
205  int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin (strtmant)
206  int cpl_end_freq; ///< coupling channel end frequency bin
207 
208  int cpl_on; ///< coupling turned on for this frame
209  int cpl_enabled; ///< coupling enabled for all frames
210  int num_cpl_subbands; ///< number of coupling subbands (ncplsubnd)
211  int num_cpl_bands; ///< number of coupling bands (ncplbnd)
212  uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS]; ///< number of coeffs in each coupling band
213 
214  int rematrixing_enabled; ///< stereo rematrixing enabled
215 
216  /* bitrate allocation control */
217  int slow_gain_code; ///< slow gain code (sgaincod)
218  int slow_decay_code; ///< slow decay code (sdcycod)
219  int fast_decay_code; ///< fast decay code (fdcycod)
220  int db_per_bit_code; ///< dB/bit code (dbpbcod)
221  int floor_code; ///< floor code (floorcod)
222  AC3BitAllocParameters bit_alloc; ///< bit allocation parameters
223  int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
224  int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
225  int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
226  int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
227  int frame_bits; ///< all frame bits except exponents and mantissas
228  int exponent_bits; ///< number of bits used for exponents
229 
238  int16_t *psd_buffer;
239  int16_t *band_psd_buffer;
240  int16_t *mask_buffer;
241  int16_t *qmant_buffer;
244 
246  uint8_t frame_exp_strategy[AC3_MAX_CHANNELS]; ///< frame exp strategy index
247  int use_frame_exp_strategy; ///< indicates use of frame exp strategy
248  uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< reference blocks for EXP_REUSE
249  uint8_t *ref_bap [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< bit allocation pointers (bap)
250  int ref_bap_set; ///< indicates if ref_bap pointers have been set
251 
252  /* fixed vs. float function pointers */
254  int (*mdct_init)(struct AC3EncodeContext *s);
255 
256  /* fixed vs. float templated function pointers */
258 
259  /* AC-3 vs. E-AC-3 function pointers */
262 
263 
264 extern const uint64_t ff_ac3_channel_layouts[19];
265 
267 
269 
271 
273 
275 
277 
279 
281 
283 
285 
286 void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame);
287 
288 
289 /* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
290 
293 
296 
297 
298 /* prototypes for functions in ac3enc_template.c */
299 
302 
304  const AVFrame *frame, int *got_packet_ptr);
306  const AVFrame *frame, int *got_packet_ptr);
307 
308 #endif /* AVCODEC_AC3ENC_H */