]> git.sesse.net Git - ffmpeg/blob - libavcodec/ac3enc.h
avformat/mxfdec: set toolkit version metadata
[ffmpeg] / libavcodec / ac3enc.h
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 "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"
41
42 #ifndef AC3ENC_FLOAT
43 #define AC3ENC_FLOAT 0
44 #endif
45
46 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
47 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
48
49 #define AC3ENC_TYPE_AC3_FIXED   0
50 #define AC3ENC_TYPE_AC3         1
51 #define AC3ENC_TYPE_EAC3        2
52
53 #if AC3ENC_FLOAT
54 #include "libavutil/float_dsp.h"
55 #define AC3_NAME(x) ff_ac3_float_ ## x
56 #define MAC_COEF(d,a,b) ((d)+=(a)*(b))
57 #define COEF_MIN (-16777215.0/16777216.0)
58 #define COEF_MAX ( 16777215.0/16777216.0)
59 #define NEW_CPL_COORD_THRESHOLD 0.03
60 typedef float SampleType;
61 typedef float CoefType;
62 typedef float CoefSumType;
63 #else
64 #include "libavutil/fixed_dsp.h"
65 #define AC3_NAME(x) ff_ac3_fixed_ ## x
66 #define MAC_COEF(d,a,b) MAC64(d,a,b)
67 #define COEF_MIN -16777215
68 #define COEF_MAX  16777215
69 #define NEW_CPL_COORD_THRESHOLD 503317
70 typedef int32_t SampleType;
71 typedef int32_t CoefType;
72 typedef int64_t CoefSumType;
73 #endif
74
75 /* common option values */
76 #define AC3ENC_OPT_NONE            -1
77 #define AC3ENC_OPT_AUTO            -1
78 #define AC3ENC_OPT_OFF              0
79 #define AC3ENC_OPT_ON               1
80 #define AC3ENC_OPT_NOT_INDICATED    0
81 #define AC3ENC_OPT_MODE_ON          2
82 #define AC3ENC_OPT_MODE_OFF         1
83 #define AC3ENC_OPT_DSUREX_DPLIIZ    3
84
85 /* specific option values */
86 #define AC3ENC_OPT_LARGE_ROOM       1
87 #define AC3ENC_OPT_SMALL_ROOM       2
88 #define AC3ENC_OPT_DOWNMIX_LTRT     1
89 #define AC3ENC_OPT_DOWNMIX_LORO     2
90 #define AC3ENC_OPT_DOWNMIX_DPLII    3 // reserved value in A/52, but used by encoders to indicate DPL2
91 #define AC3ENC_OPT_ADCONV_STANDARD  0
92 #define AC3ENC_OPT_ADCONV_HDCD      1
93
94
95 /**
96  * Encoding Options used by AVOption.
97  */
98 typedef struct AC3EncOptions {
99     /* AC-3 metadata options*/
100     int dialogue_level;
101     int bitstream_mode;
102     float center_mix_level;
103     float surround_mix_level;
104     int dolby_surround_mode;
105     int audio_production_info;
106     int mixing_level;
107     int room_type;
108     int copyright;
109     int original;
110     int extended_bsi_1;
111     int preferred_stereo_downmix;
112     float ltrt_center_mix_level;
113     float ltrt_surround_mix_level;
114     float loro_center_mix_level;
115     float loro_surround_mix_level;
116     int extended_bsi_2;
117     int dolby_surround_ex_mode;
118     int dolby_headphone_mode;
119     int ad_converter_type;
120     int eac3_mixing_metadata;
121     int eac3_info_metadata;
122
123     /* other encoding options */
124     int allow_per_frame_metadata;
125     int stereo_rematrixing;
126     int channel_coupling;
127     int cpl_start;
128 } AC3EncOptions;
129
130 /**
131  * Data for a single audio block.
132  */
133 typedef struct AC3Block {
134     CoefType **mdct_coef;                       ///< MDCT coefficients
135     int32_t  **fixed_coef;                      ///< fixed-point MDCT coefficients
136     uint8_t  **exp;                             ///< original exponents
137     uint8_t  **grouped_exp;                     ///< grouped exponents
138     int16_t  **psd;                             ///< psd per frequency bin
139     int16_t  **band_psd;                        ///< psd per critical band
140     int16_t  **mask;                            ///< masking curve
141     uint16_t **qmant;                           ///< quantized mantissas
142     uint8_t  **cpl_coord_exp;                   ///< coupling coord exponents           (cplcoexp)
143     uint8_t  **cpl_coord_mant;                  ///< coupling coord mantissas           (cplcomant)
144     uint8_t  new_rematrixing_strategy;          ///< send new rematrixing flags in this block
145     int      num_rematrixing_bands;             ///< number of rematrixing bands
146     uint8_t  rematrixing_flags[4];              ///< rematrixing flags
147     int      new_cpl_strategy;                  ///< send new coupling strategy
148     int      cpl_in_use;                        ///< coupling in use for this block     (cplinu)
149     uint8_t  channel_in_cpl[AC3_MAX_CHANNELS];  ///< channel in coupling                (chincpl)
150     int      num_cpl_channels;                  ///< number of channels in coupling
151     uint8_t  new_cpl_coords[AC3_MAX_CHANNELS];  ///< send new coupling coordinates      (cplcoe)
152     uint8_t  cpl_master_exp[AC3_MAX_CHANNELS];  ///< coupling coord master exponents    (mstrcplco)
153     int      new_snr_offsets;                   ///< send new SNR offsets
154     int      new_cpl_leak;                      ///< send new coupling leak info
155     int      end_freq[AC3_MAX_CHANNELS];        ///< end frequency bin                  (endmant)
156 } AC3Block;
157
158 /**
159  * AC-3 encoder private context.
160  */
161 typedef struct AC3EncodeContext {
162     AVClass *av_class;                      ///< AVClass used for AVOption
163     AC3EncOptions options;                  ///< encoding options
164     AVCodecContext *avctx;                  ///< parent AVCodecContext
165     PutBitContext pb;                       ///< bitstream writer context
166     AudioDSPContext adsp;
167 #if AC3ENC_FLOAT
168     AVFloatDSPContext *fdsp;
169 #else
170     AVFixedDSPContext *fdsp;
171 #endif
172     MECmpContext mecc;
173     AC3DSPContext ac3dsp;                   ///< AC-3 optimized functions
174     FFTContext mdct;                        ///< FFT context for MDCT calculation
175     const SampleType *mdct_window;          ///< MDCT window function array
176
177     AC3Block blocks[AC3_MAX_BLOCKS];        ///< per-block info
178
179     int fixed_point;                        ///< indicates if fixed-point encoder is being used
180     int eac3;                               ///< indicates if this is E-AC-3 vs. AC-3
181     int bitstream_id;                       ///< bitstream id                           (bsid)
182     int bitstream_mode;                     ///< bitstream mode                         (bsmod)
183
184     int bit_rate;                           ///< target bit rate, in bits-per-second
185     int sample_rate;                        ///< sampling frequency, in Hz
186
187     int num_blks_code;                      ///< number of blocks code                  (numblkscod)
188     int num_blocks;                         ///< number of blocks per frame
189     int frame_size_min;                     ///< minimum frame size in case rounding is necessary
190     int frame_size;                         ///< current frame size in bytes
191     int frame_size_code;                    ///< frame size code                        (frmsizecod)
192     uint16_t crc_inv[2];
193     int64_t bits_written;                   ///< bit count    (used to avg. bitrate)
194     int64_t samples_written;                ///< sample count (used to avg. bitrate)
195
196     int fbw_channels;                       ///< number of full-bandwidth channels      (nfchans)
197     int channels;                           ///< total number of channels               (nchans)
198     int lfe_on;                             ///< indicates if there is an LFE channel   (lfeon)
199     int lfe_channel;                        ///< channel index of the LFE channel
200     int has_center;                         ///< indicates if there is a center channel
201     int has_surround;                       ///< indicates if there are one or more surround channels
202     int channel_mode;                       ///< channel mode                           (acmod)
203     const uint8_t *channel_map;             ///< channel map used to reorder channels
204
205     int center_mix_level;                   ///< center mix level code
206     int surround_mix_level;                 ///< surround mix level code
207     int ltrt_center_mix_level;              ///< Lt/Rt center mix level code
208     int ltrt_surround_mix_level;            ///< Lt/Rt surround mix level code
209     int loro_center_mix_level;              ///< Lo/Ro center mix level code
210     int loro_surround_mix_level;            ///< Lo/Ro surround mix level code
211
212     int cutoff;                             ///< user-specified cutoff frequency, in Hz
213     int bandwidth_code;                     ///< bandwidth code (0 to 60)               (chbwcod)
214     int start_freq[AC3_MAX_CHANNELS];       ///< start frequency bin                    (strtmant)
215     int cpl_end_freq;                       ///< coupling channel end frequency bin
216
217     int cpl_on;                             ///< coupling turned on for this frame
218     int cpl_enabled;                        ///< coupling enabled for all frames
219     int num_cpl_subbands;                   ///< number of coupling subbands            (ncplsubnd)
220     int num_cpl_bands;                      ///< number of coupling bands               (ncplbnd)
221     uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS];  ///< number of coeffs in each coupling band
222
223     int rematrixing_enabled;                ///< stereo rematrixing enabled
224
225     /* bitrate allocation control */
226     int slow_gain_code;                     ///< slow gain code                         (sgaincod)
227     int slow_decay_code;                    ///< slow decay code                        (sdcycod)
228     int fast_decay_code;                    ///< fast decay code                        (fdcycod)
229     int db_per_bit_code;                    ///< dB/bit code                            (dbpbcod)
230     int floor_code;                         ///< floor code                             (floorcod)
231     AC3BitAllocParameters bit_alloc;        ///< bit allocation parameters
232     int coarse_snr_offset;                  ///< coarse SNR offsets                     (csnroffst)
233     int fast_gain_code[AC3_MAX_CHANNELS];   ///< fast gain codes (signal-to-mask ratio) (fgaincod)
234     int fine_snr_offset[AC3_MAX_CHANNELS];  ///< fine SNR offsets                       (fsnroffst)
235     int frame_bits_fixed;                   ///< number of non-coefficient bits for fixed parameters
236     int frame_bits;                         ///< all frame bits except exponents and mantissas
237     int exponent_bits;                      ///< number of bits used for exponents
238
239     SampleType *windowed_samples;
240     SampleType **planar_samples;
241     uint8_t *bap_buffer;
242     uint8_t *bap1_buffer;
243     CoefType *mdct_coef_buffer;
244     int32_t *fixed_coef_buffer;
245     uint8_t *exp_buffer;
246     uint8_t *grouped_exp_buffer;
247     int16_t *psd_buffer;
248     int16_t *band_psd_buffer;
249     int16_t *mask_buffer;
250     int16_t *qmant_buffer;
251     uint8_t *cpl_coord_exp_buffer;
252     uint8_t *cpl_coord_mant_buffer;
253
254     uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
255     uint8_t frame_exp_strategy[AC3_MAX_CHANNELS];           ///< frame exp strategy index
256     int use_frame_exp_strategy;                             ///< indicates use of frame exp strategy
257     uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< reference blocks for EXP_REUSE
258     uint8_t *ref_bap     [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< bit allocation pointers (bap)
259     int ref_bap_set;                                         ///< indicates if ref_bap pointers have been set
260
261     int warned_alternate_bitstream;
262
263     /* fixed vs. float function pointers */
264     void (*mdct_end)(struct AC3EncodeContext *s);
265     int  (*mdct_init)(struct AC3EncodeContext *s);
266
267     /* fixed vs. float templated function pointers */
268     int  (*allocate_sample_buffers)(struct AC3EncodeContext *s);
269
270     /* AC-3 vs. E-AC-3 function pointers */
271     void (*output_frame_header)(struct AC3EncodeContext *s);
272 } AC3EncodeContext;
273
274
275 extern const uint64_t ff_ac3_channel_layouts[19];
276
277 int ff_ac3_encode_init(AVCodecContext *avctx);
278 int ff_ac3_float_encode_init(AVCodecContext *avctx);
279
280 int ff_ac3_encode_close(AVCodecContext *avctx);
281
282 int ff_ac3_validate_metadata(AC3EncodeContext *s);
283
284 void ff_ac3_adjust_frame_size(AC3EncodeContext *s);
285
286 void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s);
287
288 int ff_ac3_encode_frame_common_end(AVCodecContext *avctx, AVPacket *avpkt,
289                                    const AVFrame *frame, int *got_packet_ptr);
290
291 /* prototypes for functions in ac3enc_template.c */
292
293 int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
294                               const AVFrame *frame, int *got_packet_ptr);
295 int ff_ac3_float_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
296                               const AVFrame *frame, int *got_packet_ptr);
297
298 #endif /* AVCODEC_AC3ENC_H */