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
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  */
23 /**
24  * @file
25  * AC-3 encoder & E-AC-3 encoder common header
26  */
28 #ifndef AVCODEC_AC3ENC_H
29 #define AVCODEC_AC3ENC_H
31 #include <stdint.h>
33 #include "ac3.h"
34 #include "ac3dsp.h"
35 #include "avcodec.h"
36 #include "fft.h"
37 #include "mathops.h"
38 #include "me_cmp.h"
39 #include "put_bits.h"
40 #include "audiodsp.h"
42 #ifndef AC3ENC_FLOAT
43 #define AC3ENC_FLOAT 0
44 #endif
47 #include "libavutil/float_dsp.h"
48 #define AC3_NAME(x) ff_ac3_float_ ## x
49 #define MAC_COEF(d,a,b) ((d)+=(a)*(b))
50 #define COEF_MIN (-16777215.0/16777216.0)
51 #define COEF_MAX ( 16777215.0/16777216.0)
53 typedef float SampleType;
54 typedef float CoefType;
55 typedef float CoefSumType;
56 #else
57 #include "libavutil/fixed_dsp.h"
58 #define AC3_NAME(x) ff_ac3_fixed_ ## x
59 #define MAC_COEF(d,a,b) MAC64(d,a,b)
60 #define COEF_MIN -16777215
61 #define COEF_MAX 16777215
62 #define NEW_CPL_COORD_THRESHOLD 503317
64 typedef int32_t CoefType;
65 typedef int64_t CoefSumType;
66 #endif
68 /* common option values */
69 #define AC3ENC_OPT_NONE -1
70 #define AC3ENC_OPT_AUTO -1
71 #define AC3ENC_OPT_OFF 0
72 #define AC3ENC_OPT_ON 1
74 #define AC3ENC_OPT_MODE_ON 2
75 #define AC3ENC_OPT_MODE_OFF 1
78 /* specific option values */
79 #define AC3ENC_OPT_LARGE_ROOM 1
80 #define AC3ENC_OPT_SMALL_ROOM 2
83 #define AC3ENC_OPT_DOWNMIX_DPLII 3 // reserved value in A/52, but used by encoders to indicate DPL2
88 /**
89  * Encoding Options used by AVOption.
90  */
91 typedef struct AC3EncOptions {
92  /* AC-3 metadata options*/
102  int original;
116  /* other encoding options */
121 } AC3EncOptions;
123 /**
124  * Data for a single audio block.
125  */
126 typedef struct AC3Block {
127  CoefType **mdct_coef; ///< MDCT coefficients
128  int32_t **fixed_coef; ///< fixed-point MDCT coefficients
129  uint8_t **exp; ///< original exponents
130  uint8_t **grouped_exp; ///< grouped exponents
131  int16_t **psd; ///< psd per frequency bin
132  int16_t **band_psd; ///< psd per critical band
133  int16_t **mask; ///< masking curve
134  uint16_t **qmant; ///< quantized mantissas
135  uint8_t **cpl_coord_exp; ///< coupling coord exponents (cplcoexp)
136  uint8_t **cpl_coord_mant; ///< coupling coord mantissas (cplcomant)
137  uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block
138  int num_rematrixing_bands; ///< number of rematrixing bands
139  uint8_t rematrixing_flags[4]; ///< rematrixing flags
140  int new_cpl_strategy; ///< send new coupling strategy
141  int cpl_in_use; ///< coupling in use for this block (cplinu)
142  uint8_t channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling (chincpl)
143  int num_cpl_channels; ///< number of channels in coupling
144  uint8_t new_cpl_coords[AC3_MAX_CHANNELS]; ///< send new coupling coordinates (cplcoe)
145  uint8_t cpl_master_exp[AC3_MAX_CHANNELS]; ///< coupling coord master exponents (mstrcplco)
146  int new_snr_offsets; ///< send new SNR offsets
147  int new_cpl_leak; ///< send new coupling leak info
148  int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant)
149 } AC3Block;
151 /**
152  * AC-3 encoder private context.
153  */
154 typedef struct AC3EncodeContext {
155  AVClass *av_class; ///< AVClass used for AVOption
156  AC3EncOptions options; ///< encoding options
157  AVCodecContext *avctx; ///< parent AVCodecContext
158  PutBitContext pb; ///< bitstream writer context
160 #if AC3ENC_FLOAT
161  AVFloatDSPContext *fdsp;
162 #else
164 #endif
166  AC3DSPContext ac3dsp; ///< AC-3 optimized functions
167  FFTContext mdct; ///< FFT context for MDCT calculation
168  const SampleType *mdct_window; ///< MDCT window function array
170  AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info
172  int fixed_point; ///< indicates if fixed-point encoder is being used
173  int eac3; ///< indicates if this is E-AC-3 vs. AC-3
174  int bitstream_id; ///< bitstream id (bsid)
175  int bitstream_mode; ///< bitstream mode (bsmod)
177  int bit_rate; ///< target bit rate, in bits-per-second
178  int sample_rate; ///< sampling frequency, in Hz
180  int num_blks_code; ///< number of blocks code (numblkscod)
181  int num_blocks; ///< number of blocks per frame
182  int frame_size_min; ///< minimum frame size in case rounding is necessary
183  int frame_size; ///< current frame size in bytes
184  int frame_size_code; ///< frame size code (frmsizecod)
185  uint16_t crc_inv[2];
186  int64_t bits_written; ///< bit count (used to avg. bitrate)
187  int64_t samples_written; ///< sample count (used to avg. bitrate)
189  int fbw_channels; ///< number of full-bandwidth channels (nfchans)
190  int channels; ///< total number of channels (nchans)
191  int lfe_on; ///< indicates if there is an LFE channel (lfeon)
192  int lfe_channel; ///< channel index of the LFE channel
193  int has_center; ///< indicates if there is a center channel
194  int has_surround; ///< indicates if there are one or more surround channels
195  int channel_mode; ///< channel mode (acmod)
196  const uint8_t *channel_map; ///< channel map used to reorder channels
198  int center_mix_level; ///< center mix level code
199  int surround_mix_level; ///< surround mix level code
200  int ltrt_center_mix_level; ///< Lt/Rt center mix level code
201  int ltrt_surround_mix_level; ///< Lt/Rt surround mix level code
202  int loro_center_mix_level; ///< Lo/Ro center mix level code
203  int loro_surround_mix_level; ///< Lo/Ro surround mix level code
205  int cutoff; ///< user-specified cutoff frequency, in Hz
206  int bandwidth_code; ///< bandwidth code (0 to 60) (chbwcod)
207  int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin (strtmant)
208  int cpl_end_freq; ///< coupling channel end frequency bin
210  int cpl_on; ///< coupling turned on for this frame
211  int cpl_enabled; ///< coupling enabled for all frames
212  int num_cpl_subbands; ///< number of coupling subbands (ncplsubnd)
213  int num_cpl_bands; ///< number of coupling bands (ncplbnd)
214  uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS]; ///< number of coeffs in each coupling band
216  int rematrixing_enabled; ///< stereo rematrixing enabled
218  /* bitrate allocation control */
219  int slow_gain_code; ///< slow gain code (sgaincod)
220  int slow_decay_code; ///< slow decay code (sdcycod)
221  int fast_decay_code; ///< fast decay code (fdcycod)
222  int db_per_bit_code; ///< dB/bit code (dbpbcod)
223  int floor_code; ///< floor code (floorcod)
224  AC3BitAllocParameters bit_alloc; ///< bit allocation parameters
225  int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
226  int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
227  int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
228  int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
229  int frame_bits; ///< all frame bits except exponents and mantissas
230  int exponent_bits; ///< number of bits used for exponents
240  int16_t *psd_buffer;
241  int16_t *band_psd_buffer;
242  int16_t *mask_buffer;
243  int16_t *qmant_buffer;
247  uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
248  uint8_t frame_exp_strategy[AC3_MAX_CHANNELS]; ///< frame exp strategy index
249  int use_frame_exp_strategy; ///< indicates use of frame exp strategy
250  uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< reference blocks for EXP_REUSE
251  uint8_t *ref_bap [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< bit allocation pointers (bap)
252  int ref_bap_set; ///< indicates if ref_bap pointers have been set
256  /* fixed vs. float function pointers */
260  /* fixed vs. float templated function pointers */
263  /* AC-3 vs. E-AC-3 function pointers */
268 extern const uint64_t ff_ac3_channel_layouts[19];
269 extern const AVOption ff_ac3_enc_options[];
270 extern const AVCodecDefault ff_ac3_enc_defaults[];
284  const AVFrame *frame, int *got_packet_ptr);
286 /* prototypes for functions in ac3enc_template.c */
289  const AVFrame *frame, int *got_packet_ptr);
291  const AVFrame *frame, int *got_packet_ptr);
293 #endif /* AVCODEC_AC3ENC_H */
uint8_t new_rematrixing_strategy
send new rematrixing flags in this block
Definition: ac3enc.h:137
int eac3_mixing_metadata
Definition: ac3enc.h:113
int ff_ac3_encode_init(AVCodecContext *avctx)
Definition: ac3enc.c:2506
int dialogue_level
Definition: ac3enc.h:93
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
int db_per_bit_code
dB/bit code (dbpbcod)
Definition: ac3enc.h:222
Definition: opt.h:248
int slow_decay_code
slow decay code (sdcycod)
Definition: ac3enc.h:220
Encoding Options used by AVOption.
Definition: ac3enc.h:91
float loro_surround_mix_level
Definition: ac3enc.h:108
int channel_coupling
Definition: ac3enc.h:119
int dolby_surround_ex_mode
Definition: ac3enc.h:110
static int allocate_sample_buffers(AC3EncodeContext *s)
uint8_t ** cpl_coord_exp
coupling coord exponents (cplcoexp)
Definition: ac3enc.h:135
int bandwidth_code
bandwidth code (0 to 60) (chbwcod)
Definition: ac3enc.h:206
uint8_t * grouped_exp_buffer
Definition: ac3enc.h:239
int16_t ** psd
psd per frequency bin
Definition: ac3enc.h:131
int frame_size_code
frame size code (frmsizecod)
Definition: ac3enc.h:184
int frame_bits
all frame bits except exponents and mantissas
Definition: ac3enc.h:229
const uint64_t ff_ac3_channel_layouts[19]
List of supported channel layouts.
Definition: ac3enc.c:142
uint8_t ** cpl_coord_mant
coupling coord mantissas (cplcomant)
Definition: ac3enc.h:136
uint16_t ** qmant
quantized mantissas
Definition: ac3enc.h:134
int ff_ac3_validate_metadata(AC3EncodeContext *s)
Validate metadata options as set by AVOption system.
Definition: ac3enc.c:1930
PutBitContext pb
bitstream writer context
Definition: ac3enc.h:158
int num_cpl_channels
number of channels in coupling
Definition: ac3enc.h:143
AC3BitAllocParameters bit_alloc
bit allocation parameters
Definition: ac3enc.h:224
float ltrt_surround_mix_level
Definition: ac3enc.h:106
int ff_ac3_float_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
int new_cpl_leak
send new coupling leak info
Definition: ac3enc.h:147
int rematrixing_enabled
stereo rematrixing enabled
Definition: ac3enc.h:216
int bitstream_mode
Definition: ac3enc.h:94
int ff_ac3_float_encode_init(AVCodecContext *avctx)
Definition: ac3enc_float.c:122
int channel_mode
channel mode (acmod)
Definition: ac3enc.h:195
int num_cpl_subbands
number of coupling subbands (ncplsubnd)
Definition: ac3enc.h:212
float surround_mix_level
Definition: ac3enc.h:96
int fbw_channels
number of full-bandwidth channels (nfchans)
Definition: ac3enc.h:189
uint8_t * bap1_buffer
Definition: ac3enc.h:235
int slow_gain_code
slow gain code (sgaincod)
Definition: ac3enc.h:219
AVFixedDSPContext * fdsp
Definition: ac3enc.h:163
uint8_t ** exp
original exponents
Definition: ac3enc.h:129
int num_rematrixing_bands
number of rematrixing bands
Definition: ac3enc.h:138
AC3DSPContext ac3dsp
AC-3 optimized functions.
Definition: ac3enc.h:166
int loro_center_mix_level
Lo/Ro center mix level code.
Definition: ac3enc.h:202
int num_cpl_bands
number of coupling bands (ncplbnd)
Definition: ac3enc.h:213
int lfe_channel
channel index of the LFE channel
Definition: ac3enc.h:192
int ref_bap_set
indicates if ref_bap pointers have been set
Definition: ac3enc.h:252
int new_snr_offsets
send new SNR offsets
Definition: ac3enc.h:146
void(* output_frame_header)(struct AC3EncodeContext *s)
Definition: ac3enc.h:264
int64_t CoefSumType
Definition: ac3enc.h:65
int loro_surround_mix_level
Lo/Ro surround mix level code.
Definition: ac3enc.h:203
CoefType ** mdct_coef
MDCT coefficients.
Definition: ac3enc.h:127
int eac3_info_metadata
Definition: ac3enc.h:114
int mixing_level
Definition: ac3enc.h:99
int num_blks_code
number of blocks code (numblkscod)
Definition: ac3enc.h:180
AC3EncOptions options
encoding options
Definition: ac3enc.h:156
int16_t ** band_psd
psd per critical band
Definition: ac3enc.h:132
AVClass * av_class
AVClass used for AVOption.
Definition: ac3enc.h:155
float ltrt_center_mix_level
Definition: ac3enc.h:105
int channels
total number of channels (nchans)
Definition: ac3enc.h:190
maximum number of channels, including coupling channel
Definition: ac3.h:32
int cpl_on
coupling turned on for this frame
Definition: ac3enc.h:210
int16_t * mask_buffer
Definition: ac3enc.h:242
int16_t * psd_buffer
Definition: ac3enc.h:240
int fixed_point
indicates if fixed-point encoder is being used
Definition: ac3enc.h:172
int ltrt_surround_mix_level
Lt/Rt surround mix level code.
Definition: ac3enc.h:201
int new_cpl_strategy
send new coupling strategy
Definition: ac3enc.h:140
int surround_mix_level
surround mix level code
Definition: ac3enc.h:199
int cpl_in_use
coupling in use for this block (cplinu)
Definition: ac3enc.h:141
int cpl_enabled
coupling enabled for all frames
Definition: ac3enc.h:211
int32_t SampleType
Definition: ac3enc.h:63
Data for a single audio block.
Definition: ac3enc.h:126
int floor_code
floor code (floorcod)
Definition: ac3enc.h:223
int bitstream_mode
bitstream mode (bsmod)
Definition: ac3enc.h:175
int has_surround
indicates if there are one or more surround channels
Definition: ac3enc.h:194
Definition: fft.h:83
AudioDSPContext adsp
Definition: ac3enc.h:159
int eac3
indicates if this is E-AC-3 vs. AC-3
Definition: ac3enc.h:173
float loro_center_mix_level
Definition: ac3enc.h:107
int ad_converter_type
Definition: ac3enc.h:112
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
const AVCodecDefault ff_ac3_enc_defaults[]
Definition: ac3enc.c:127
void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s)
Set the initial coupling strategy parameters prior to coupling analysis.
Definition: ac3enc.c:271
#define s(width, name)
Definition: cbs_vp9.c:257
int exponent_bits
number of bits used for exponents
Definition: ac3enc.h:230
int stereo_rematrixing
Definition: ac3enc.h:118
int coarse_snr_offset
coarse SNR offsets (csnroffst)
Definition: ac3enc.h:225
int16_t ** mask
masking curve
Definition: ac3enc.h:133
FFTContext mdct
FFT context for MDCT calculation.
Definition: ac3enc.h:167
const SampleType * mdct_window
MDCT window function array.
Definition: ac3enc.h:168
SampleType ** planar_samples
Definition: ac3enc.h:233
int fast_decay_code
fast decay code (fdcycod)
Definition: ac3enc.h:221
int16_t * qmant_buffer
Definition: ac3enc.h:243
Libavcodec external API header.
int audio_production_info
Definition: ac3enc.h:98
int dolby_surround_mode
Definition: ac3enc.h:97
main external API structure.
Definition: avcodec.h:531
int sample_rate
sampling frequency, in Hz
Definition: ac3enc.h:178
CoefType * mdct_coef_buffer
Definition: ac3enc.h:236
int ff_ac3_encode_frame_common_end(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: ac3enc.c:1742
int has_center
indicates if there is a center channel
Definition: ac3enc.h:193
int bit_rate
target bit rate, in bits-per-second
Definition: ac3enc.h:177
const uint8_t * channel_map
channel map used to reorder channels
Definition: ac3enc.h:196
uint8_t * exp_buffer
Definition: ac3enc.h:238
int frame_bits_fixed
number of non-coefficient bits for fixed parameters
Definition: ac3enc.h:228
int warned_alternate_bitstream
Definition: ac3enc.h:254
Describe the class of an AVClass context structure.
Definition: log.h:67
int cpl_start
Definition: ac3enc.h:120
uint8_t * cpl_coord_exp_buffer
Definition: ac3enc.h:244
int ltrt_center_mix_level
Lt/Rt center mix level code.
Definition: ac3enc.h:200
int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
int center_mix_level
center mix level code
Definition: ac3enc.h:198
#define AC3_MAX_BLOCKS
Definition: ac3.h:37
AC-3 encoder private context.
Definition: ac3enc.h:154
SampleType * windowed_samples
Definition: ac3enc.h:232
int preferred_stereo_downmix
Definition: ac3enc.h:104
int num_blocks
number of blocks per frame
Definition: ac3enc.h:181
int ff_ac3_encode_close(AVCodecContext *avctx)
Finalize encoding and free any memory allocated by the encoder.
Definition: ac3enc.c:2113
float center_mix_level
Definition: ac3enc.h:95
int extended_bsi_2
Definition: ac3enc.h:109
int frame_size
current frame size in bytes
Definition: ac3enc.h:183
int room_type
Definition: ac3enc.h:100
uint8_t ** grouped_exp
grouped exponents
Definition: ac3enc.h:130
int cpl_end_freq
coupling channel end frequency bin
Definition: ac3enc.h:208
MECmpContext mecc
Definition: ac3enc.h:165
Definition: ac3.h:41
static void mdct_end(FFTContext *s)
Definition: fft.c:277
static void mdct_init(FFTContext **s, int nbits, int inverse, double scale)
Definition: fft.c:232
int64_t bits_written
bit count (used to avg. bitrate)
Definition: ac3enc.h:186
int bitstream_id
bitstream id (bsid)
Definition: ac3enc.h:174
int16_t * band_psd_buffer
Definition: ac3enc.h:241
int dolby_headphone_mode
Definition: ac3enc.h:111
AVCodecContext * avctx
parent AVCodecContext
Definition: ac3enc.h:157
const AVOption ff_ac3_enc_options[]
Definition: ac3enc.c:77
int allow_per_frame_metadata
Definition: ac3enc.h:117
int original
Definition: ac3enc.h:102
uint8_t * bap_buffer
Definition: ac3enc.h:234
int frame_size_min
minimum frame size in case rounding is necessary
Definition: ac3enc.h:182
int64_t samples_written
sample count (used to avg. bitrate)
Definition: ac3enc.h:187
uint8_t * cpl_coord_mant_buffer
Definition: ac3enc.h:245
int use_frame_exp_strategy
indicates use of frame exp strategy
Definition: ac3enc.h:249
int32_t CoefType
Definition: ac3enc.h:64
int cutoff
user-specified cutoff frequency, in Hz
Definition: ac3enc.h:205
void ff_ac3_adjust_frame_size(AC3EncodeContext *s)
Adjust the frame size to make the average bit rate match the target bit rate.
Definition: ac3enc.c:253
int lfe_on
indicates if there is an LFE channel (lfeon)
Definition: ac3enc.h:191
This structure stores compressed data.
Definition: packet.h:340
Common code between the AC-3 encoder and decoder.
int32_t * fixed_coef_buffer
Definition: ac3enc.h:237
int extended_bsi_1
Definition: ac3enc.h:103
int copyright
Definition: ac3enc.h:101
int32_t ** fixed_coef
fixed-point MDCT coefficients
Definition: ac3enc.h:128
bitstream writer API