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