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 Libav.
7  *
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.
12  *
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.
17  *
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
21  */
22 
28 #ifndef AVCODEC_AC3ENC_H
29 #define AVCODEC_AC3ENC_H
30 
31 #include <stdint.h>
32 #include "ac3.h"
33 #include "ac3dsp.h"
34 #include "avcodec.h"
35 #include "dsputil.h"
36 #include "put_bits.h"
37 #include "fft.h"
38 
39 #ifndef CONFIG_AC3ENC_FLOAT
40 #define CONFIG_AC3ENC_FLOAT 0
41 #endif
42 
43 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
44 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
45 
46 #define AC3ENC_TYPE_AC3_FIXED 0
47 #define AC3ENC_TYPE_AC3 1
48 #define AC3ENC_TYPE_EAC3 2
49 
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 #define NEW_CPL_COORD_THRESHOLD 0.03
56 typedef float SampleType;
57 typedef float CoefType;
58 typedef float CoefSumType;
59 #else
60 #define AC3_NAME(x) ff_ac3_fixed_ ## x
61 #define MAC_COEF(d,a,b) MAC64(d,a,b)
62 #define COEF_MIN -16777215
63 #define COEF_MAX 16777215
64 #define NEW_CPL_COORD_THRESHOLD 503317
65 typedef int16_t SampleType;
66 typedef int32_t CoefType;
67 typedef int64_t CoefSumType;
68 #endif
69 
70 /* common option values */
71 #define AC3ENC_OPT_NONE -1
72 #define AC3ENC_OPT_AUTO -1
73 #define AC3ENC_OPT_OFF 0
74 #define AC3ENC_OPT_ON 1
75 #define AC3ENC_OPT_NOT_INDICATED 0
76 #define AC3ENC_OPT_MODE_ON 2
77 #define AC3ENC_OPT_MODE_OFF 1
78 
79 /* specific option values */
80 #define AC3ENC_OPT_LARGE_ROOM 1
81 #define AC3ENC_OPT_SMALL_ROOM 2
82 #define AC3ENC_OPT_DOWNMIX_LTRT 1
83 #define AC3ENC_OPT_DOWNMIX_LORO 2
84 #define AC3ENC_OPT_ADCONV_STANDARD 0
85 #define AC3ENC_OPT_ADCONV_HDCD 1
86 
87 
91 typedef struct AC3EncOptions {
92  /* AC-3 metadata options*/
102  int original;
115 
116  /* other encoding options */
121 } AC3EncOptions;
122 
126 typedef struct AC3Block {
128  int32_t **fixed_coef;
129  uint8_t **exp;
130  uint8_t **grouped_exp;
131  int16_t **psd;
132  int16_t **band_psd;
133  int16_t **mask;
134  uint16_t **qmant;
135  uint8_t **cpl_coord_exp;
136  uint8_t **cpl_coord_mant;
140  uint8_t rematrixing_flags[4];
150 } AC3Block;
151 
155 typedef struct AC3EncodeContext {
164 
166 
168  int eac3;
171 
172  int bit_rate;
174 
180  uint16_t crc_inv[2];
181  int64_t bits_written;
182  int64_t samples_written;
183 
185  int channels;
186  int lfe_on;
191  const uint8_t *channel_map;
192 
199 
200  int cutoff;
204 
205  int cpl_on;
210 
212 
213  /* bitrate allocation control */
226 
229  uint8_t *bap_buffer;
230  uint8_t *bap1_buffer;
233  uint8_t *exp_buffer;
235  int16_t *psd_buffer;
236  int16_t *band_psd_buffer;
237  int16_t *mask_buffer;
238  int16_t *qmant_buffer;
241 
248 
249  /* fixed vs. float function pointers */
251  int (*mdct_init)(struct AC3EncodeContext *s);
252 
253  /* fixed vs. float templated function pointers */
255 
256  /* AC-3 vs. E-AC-3 function pointers */
259 
260 
261 extern const uint64_t ff_ac3_channel_layouts[19];
262 
264 
266 
268 
270 
272 
274 
276 
278 
280 
282 
283 void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame);
284 
285 
286 /* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
287 
290 
293 
294 
295 /* prototypes for functions in ac3enc_template.c */
296 
299 
300 int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, unsigned char *frame,
301  int buf_size, void *data);
302 int ff_ac3_float_encode_frame(AVCodecContext *avctx, unsigned char *frame,
303  int buf_size, void *data);
304 
305 #endif /* AVCODEC_AC3ENC_H */
uint8_t new_rematrixing_strategy
send new rematrixing flags in this block
Definition: ac3enc.h:138
uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]
exponent strategies
Definition: ac3enc.h:242
int eac3_mixing_metadata
Definition: ac3enc.h:113
int ff_ac3_encode_init(AVCodecContext *avctx)
Definition: ac3enc.c:2410
int dialogue_level
Definition: ac3enc.h:93
int db_per_bit_code
dB/bit code (dbpbcod)
Definition: ac3enc.h:217
int slow_decay_code
slow decay code (sdcycod)
Definition: ac3enc.h:215
struct AC3EncodeContext AC3EncodeContext
AC-3 encoder private context.
Encoding Options used by AVOption.
Definition: ac3enc.h:91
int ff_ac3_fixed_allocate_sample_buffers(AC3EncodeContext *s)
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
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:201
uint8_t * grouped_exp_buffer
Definition: ac3enc.h:234
int16_t ** psd
psd per frequency bin
Definition: ac3enc.h:131
int frame_size_code
frame size code (frmsizecod)
Definition: ac3enc.h:179
void(* mdct_end)(struct AC3EncodeContext *s)
Definition: ac3enc.h:250
int frame_bits
all frame bits except exponents and mantissas
Definition: ac3enc.h:224
const uint64_t ff_ac3_channel_layouts[19]
List of supported channel layouts.
Definition: ac3enc.c:80
uint8_t ** cpl_coord_mant
coupling coord mantissas (cplcomant)
Definition: ac3enc.h:136
uint16_t ** qmant
quantized mantissas
Definition: ac3enc.h:134
int start_freq[AC3_MAX_CHANNELS]
start frequency bin (strtmant)
Definition: ac3enc.h:202
int ff_ac3_validate_metadata(AC3EncodeContext *s)
Validate metadata options as set by AVOption system.
Definition: ac3enc.c:1836
PutBitContext pb
bitstream writer context
Definition: ac3enc.h:159
int num_cpl_channels
number of channels in coupling
Definition: ac3enc.h:144
AC3BitAllocParameters bit_alloc
bit allocation parameters
Definition: ac3enc.h:219
int ff_ac3_float_allocate_sample_buffers(AC3EncodeContext *s)
DSPContext dsp
Definition: ac3enc.h:160
float ltrt_surround_mix_level
Definition: ac3enc.h:106
int new_cpl_leak
send new coupling leak info
Definition: ac3enc.h:148
int rematrixing_enabled
stereo rematrixing enabled
Definition: ac3enc.h:211
int bitstream_mode
Definition: ac3enc.h:94
int channel_mode
channel mode (acmod)
Definition: ac3enc.h:190
int num_cpl_subbands
number of coupling subbands (ncplsubnd)
Definition: ac3enc.h:207
float surround_mix_level
Definition: ac3enc.h:96
int(* allocate_sample_buffers)(struct AC3EncodeContext *s)
Definition: ac3enc.h:254
uint8_t rematrixing_flags[4]
rematrixing flags
Definition: ac3enc.h:140
int fbw_channels
number of full-bandwidth channels (nfchans)
Definition: ac3enc.h:184
uint8_t new_cpl_coords[AC3_MAX_CHANNELS]
send new coupling coordinates (cplcoe)
Definition: ac3enc.h:145
int ff_ac3_compute_bit_allocation(AC3EncodeContext *s)
Definition: ac3enc.c:1145
uint8_t * bap1_buffer
Definition: ac3enc.h:230
int slow_gain_code
slow gain code (sgaincod)
Definition: ac3enc.h:214
const char data[16]
Definition: mxf.c:60
uint8_t cpl_master_exp[AC3_MAX_CHANNELS]
coupling coord master exponents (mstrcplco)
Definition: ac3enc.h:146
uint8_t ** exp
original exponents
Definition: ac3enc.h:129
int num_rematrixing_bands
number of rematrixing bands
Definition: ac3enc.h:139
AC3DSPContext ac3dsp
AC-3 optimized functions.
Definition: ac3enc.h:161
int loro_center_mix_level
Lo/Ro center mix level code.
Definition: ac3enc.h:197
int num_cpl_bands
number of coupling bands (ncplbnd)
Definition: ac3enc.h:208
void ff_ac3_fixed_mdct_end(AC3EncodeContext *s)
int lfe_channel
channel index of the LFE channel
Definition: ac3enc.h:187
int ref_bap_set
indicates if ref_bap pointers have been set
Definition: ac3enc.h:247
void ff_ac3_quantize_mantissas(AC3EncodeContext *s)
Quantize mantissas using coefficients, exponents, and bit allocation pointers.
Definition: ac3enc.c:1302
int new_snr_offsets
send new SNR offsets
Definition: ac3enc.h:147
int64_t CoefSumType
Definition: ac3enc.h:67
int loro_surround_mix_level
Lo/Ro surround mix level code.
Definition: ac3enc.h:198
CoefType ** mdct_coef
MDCT coefficients.
Definition: ac3enc.h:127
uint8_t channel_in_cpl[AC3_MAX_CHANNELS]
channel in coupling (chincpl)
Definition: ac3enc.h:143
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:175
AC3EncOptions options
encoding options
Definition: ac3enc.h:157
int16_t ** band_psd
psd per critical band
Definition: ac3enc.h:132
AVClass * av_class
AVClass used for AVOption.
Definition: ac3enc.h:156
float ltrt_center_mix_level
Definition: ac3enc.h:105
int channels
total number of channels (nchans)
Definition: ac3enc.h:185
#define AC3_MAX_CHANNELS
maximum number of channels, including coupling channel
Definition: ac3.h:31
int cpl_on
coupling turned on for this frame
Definition: ac3enc.h:205
uint16_t crc_inv[2]
Definition: ac3enc.h:180
int16_t * mask_buffer
Definition: ac3enc.h:237
int16_t * psd_buffer
Definition: ac3enc.h:235
int fixed_point
indicates if fixed-point encoder is being used
Definition: ac3enc.h:167
int ltrt_surround_mix_level
Lt/Rt surround mix level code.
Definition: ac3enc.h:196
int new_cpl_strategy
send new coupling strategy
Definition: ac3enc.h:141
int surround_mix_level
surround mix level code
Definition: ac3enc.h:194
int cpl_in_use
coupling in use for this block (cplinu)
Definition: ac3enc.h:142
int cpl_enabled
coupling enabled for all frames
Definition: ac3enc.h:206
int16_t SampleType
Definition: ac3enc.h:65
int ff_ac3_float_encode_frame(AVCodecContext *avctx, unsigned char *frame, int buf_size, void *data)
Data for a single audio block.
Definition: ac3enc.h:126
int(* mdct_init)(struct AC3EncodeContext *s)
Definition: ac3enc.h:251
int floor_code
floor code (floorcod)
Definition: ac3enc.h:218
int bitstream_mode
bitstream mode (bsmod)
Definition: ac3enc.h:170
int has_surround
indicates if there are one or more surround channels
Definition: ac3enc.h:189
Definition: fft.h:62
uint8_t frame_exp_strategy[AC3_MAX_CHANNELS]
frame exp strategy index
Definition: ac3enc.h:243
int eac3
indicates if this is E-AC-3 vs. AC-3
Definition: ac3enc.h:168
void ff_ac3_apply_rematrixing(AC3EncodeContext *s)
Apply stereo rematrixing to coefficients based on rematrixing flags.
Definition: ac3enc.c:271
float loro_center_mix_level
Definition: ac3enc.h:107
int ff_ac3_float_mdct_init(AC3EncodeContext *s)
Initialize MDCT tables.
Definition: ac3enc_float.c:63
struct AC3Block AC3Block
Data for a single audio block.
int ad_converter_type
Definition: ac3enc.h:112
void ff_ac3_process_exponents(AC3EncodeContext *s)
Calculate final exponents from the supplied MDCT coefficients and exponent shift. ...
Definition: ac3enc.c:636
void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s)
Set the initial coupling strategy parameters prior to coupling analysis.
Definition: ac3enc.c:200
int exponent_bits
number of bits used for exponents
Definition: ac3enc.h:225
int stereo_rematrixing
Definition: ac3enc.h:118
int coarse_snr_offset
coarse SNR offsets (csnroffst)
Definition: ac3enc.h:220
int16_t ** mask
masking curve
Definition: ac3enc.h:133
FFTContext mdct
FFT context for MDCT calculation.
Definition: ac3enc.h:162
const SampleType * mdct_window
MDCT window function array.
Definition: ac3enc.h:163
SampleType ** planar_samples
Definition: ac3enc.h:228
int fast_decay_code
fast decay code (fdcycod)
Definition: ac3enc.h:216
int16_t * qmant_buffer
Definition: ac3enc.h:238
void(* output_frame_header)(struct AC3EncodeContext *s)
Definition: ac3enc.h:257
uint8_t * ref_bap[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]
bit allocation pointers (bap)
Definition: ac3enc.h:246
external API header
int audio_production_info
Definition: ac3enc.h:98
struct AC3EncOptions AC3EncOptions
Encoding Options used by AVOption.
int dolby_surround_mode
Definition: ac3enc.h:97
main external API structure.
Definition: avcodec.h:1329
int fast_gain_code[AC3_MAX_CHANNELS]
fast gain codes (signal-to-mask ratio) (fgaincod)
Definition: ac3enc.h:221
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
int sample_rate
sampling frequency, in Hz
Definition: ac3enc.h:173
CoefType * mdct_coef_buffer
Definition: ac3enc.h:231
int has_center
indicates if there is a center channel
Definition: ac3enc.h:188
int bit_rate
target bit rate, in bits-per-second
Definition: ac3enc.h:172
const uint8_t * channel_map
channel map used to reorder channels
Definition: ac3enc.h:191
uint8_t * exp_buffer
Definition: ac3enc.h:233
int frame_bits_fixed
number of non-coefficient bits for fixed parameters
Definition: ac3enc.h:223
int end_freq[AC3_MAX_CHANNELS]
end frequency bin (endmant)
Definition: ac3enc.h:149
Describe the class of an AVClass context structure.
Definition: log.h:33
int cpl_start
Definition: ac3enc.h:120
uint8_t * cpl_coord_exp_buffer
Definition: ac3enc.h:239
int ltrt_center_mix_level
Lt/Rt center mix level code.
Definition: ac3enc.h:195
int center_mix_level
center mix level code
Definition: ac3enc.h:193
#define AC3_MAX_BLOCKS
Definition: ac3.h:36
AC-3 encoder private context.
Definition: ac3enc.h:155
AC3Block blocks[AC3_MAX_BLOCKS]
per-block info
Definition: ac3enc.h:165
SampleType * windowed_samples
Definition: ac3enc.h:227
int preferred_stereo_downmix
Definition: ac3enc.h:104
int num_blocks
number of blocks per frame
Definition: ac3enc.h:176
void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame)
Write the frame to the output bitstream.
Definition: ac3enc.c:1664
int ff_ac3_encode_close(AVCodecContext *avctx)
Finalize encoding and free any memory allocated by the encoder.
Definition: ac3enc.c:2020
float center_mix_level
Definition: ac3enc.h:95
uint8_t coeff_shift[AC3_MAX_CHANNELS]
fixed-point coefficient shift values
Definition: ac3enc.h:137
int extended_bsi_2
Definition: ac3enc.h:109
int frame_size
current frame size in bytes
Definition: ac3enc.h:178
int room_type
Definition: ac3enc.h:100
uint8_t ** grouped_exp
grouped exponents
Definition: ac3enc.h:130
int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, unsigned char *frame, int buf_size, void *data)
int cpl_end_freq
coupling channel end frequency bin
Definition: ac3enc.h:203
uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS]
number of coeffs in each coupling band
Definition: ac3enc.h:209
#define AC3_MAX_CPL_BANDS
Definition: ac3.h:40
int64_t bits_written
bit count (used to avg. bitrate)
Definition: ac3enc.h:181
int bitstream_id
bitstream id (bsid)
Definition: ac3enc.h:169
DSP utils.
int16_t * band_psd_buffer
Definition: ac3enc.h:236
int dolby_headphone_mode
Definition: ac3enc.h:111
AVCodecContext * avctx
parent AVCodecContext
Definition: ac3enc.h:158
uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]
reference blocks for EXP_REUSE
Definition: ac3enc.h:245
int allow_per_frame_metadata
Definition: ac3enc.h:117
int original
Definition: ac3enc.h:102
uint8_t * bap_buffer
Definition: ac3enc.h:229
int frame_size_min
minimum frame size in case rounding is necessary
Definition: ac3enc.h:177
void ff_ac3_float_mdct_end(AC3EncodeContext *s)
Finalize MDCT and free allocated memory.
Definition: ac3enc_float.c:50
int64_t samples_written
sample count (used to avg. bitrate)
Definition: ac3enc.h:182
uint8_t * cpl_coord_mant_buffer
Definition: ac3enc.h:240
int use_frame_exp_strategy
indicates use of frame exp strategy
Definition: ac3enc.h:244
int ff_ac3_fixed_mdct_init(AC3EncodeContext *s)
int32_t CoefType
Definition: ac3enc.h:66
int cutoff
user-specified cutoff frequency, in Hz
Definition: ac3enc.h:200
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:182
int lfe_on
indicates if there is an LFE channel (lfeon)
Definition: ac3enc.h:186
int fine_snr_offset[AC3_MAX_CHANNELS]
fine SNR offsets (fsnroffst)
Definition: ac3enc.h:222
Common code between the AC-3 encoder and decoder.
int32_t * fixed_coef_buffer
Definition: ac3enc.h:232
int extended_bsi_1
Definition: ac3enc.h:103
void ff_ac3_group_exponents(AC3EncodeContext *s)
Group exponents.
Definition: ac3enc.c:578
DSPContext.
Definition: dsputil.h:226
int copyright
Definition: ac3enc.h:101
int32_t ** fixed_coef
fixed-point MDCT coefficients
Definition: ac3enc.h:128
bitstream writer API