]> git.sesse.net Git - ffmpeg/blob - libavcodec/ac3enc.h
avcodec/dcadec: use a constant instead of assuming every compiler can optimize pow...
[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 "libavutil/float_dsp.h"
34
35 #include "ac3.h"
36 #include "ac3dsp.h"
37 #include "avcodec.h"
38 #include "dsputil.h"
39 #include "fft.h"
40 #include "mathops.h"
41 #include "put_bits.h"
42
43 #ifndef CONFIG_AC3ENC_FLOAT
44 #define CONFIG_AC3ENC_FLOAT 0
45 #endif
46
47 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
48 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
49
50 #define AC3ENC_TYPE_AC3_FIXED   0
51 #define AC3ENC_TYPE_AC3         1
52 #define AC3ENC_TYPE_EAC3        2
53
54 #if CONFIG_AC3ENC_FLOAT
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 #define AC3_NAME(x) ff_ac3_fixed_ ## x
65 #define MAC_COEF(d,a,b) MAC64(d,a,b)
66 #define COEF_MIN -16777215
67 #define COEF_MAX  16777215
68 #define NEW_CPL_COORD_THRESHOLD 503317
69 typedef int16_t SampleType;
70 typedef int32_t CoefType;
71 typedef int64_t CoefSumType;
72 #endif
73
74 /* common option values */
75 #define AC3ENC_OPT_NONE            -1
76 #define AC3ENC_OPT_AUTO            -1
77 #define AC3ENC_OPT_OFF              0
78 #define AC3ENC_OPT_ON               1
79 #define AC3ENC_OPT_NOT_INDICATED    0
80 #define AC3ENC_OPT_MODE_ON          2
81 #define AC3ENC_OPT_MODE_OFF         1
82
83 /* specific option values */
84 #define AC3ENC_OPT_LARGE_ROOM       1
85 #define AC3ENC_OPT_SMALL_ROOM       2
86 #define AC3ENC_OPT_DOWNMIX_LTRT     1
87 #define AC3ENC_OPT_DOWNMIX_LORO     2
88 #define AC3ENC_OPT_ADCONV_STANDARD  0
89 #define AC3ENC_OPT_ADCONV_HDCD      1
90
91
92 /**
93  * Encoding Options used by AVOption.
94  */
95 typedef struct AC3EncOptions {
96     /* AC-3 metadata options*/
97     int dialogue_level;
98     int bitstream_mode;
99     float center_mix_level;
100     float surround_mix_level;
101     int dolby_surround_mode;
102     int audio_production_info;
103     int mixing_level;
104     int room_type;
105     int copyright;
106     int original;
107     int extended_bsi_1;
108     int preferred_stereo_downmix;
109     float ltrt_center_mix_level;
110     float ltrt_surround_mix_level;
111     float loro_center_mix_level;
112     float loro_surround_mix_level;
113     int extended_bsi_2;
114     int dolby_surround_ex_mode;
115     int dolby_headphone_mode;
116     int ad_converter_type;
117     int eac3_mixing_metadata;
118     int eac3_info_metadata;
119
120     /* other encoding options */
121     int allow_per_frame_metadata;
122     int stereo_rematrixing;
123     int channel_coupling;
124     int cpl_start;
125 } AC3EncOptions;
126
127 /**
128  * Data for a single audio block.
129  */
130 typedef struct AC3Block {
131     CoefType **mdct_coef;                       ///< MDCT coefficients
132     int32_t  **fixed_coef;                      ///< fixed-point MDCT coefficients
133     uint8_t  **exp;                             ///< original exponents
134     uint8_t  **grouped_exp;                     ///< grouped exponents
135     int16_t  **psd;                             ///< psd per frequency bin
136     int16_t  **band_psd;                        ///< psd per critical band
137     int16_t  **mask;                            ///< masking curve
138     uint16_t **qmant;                           ///< quantized mantissas
139     uint8_t  **cpl_coord_exp;                   ///< coupling coord exponents           (cplcoexp)
140     uint8_t  **cpl_coord_mant;                  ///< coupling coord mantissas           (cplcomant)
141     uint8_t  coeff_shift[AC3_MAX_CHANNELS];     ///< fixed-point coefficient shift values
142     uint8_t  new_rematrixing_strategy;          ///< send new rematrixing flags in this block
143     int      num_rematrixing_bands;             ///< number of rematrixing bands
144     uint8_t  rematrixing_flags[4];              ///< rematrixing flags
145     int      new_cpl_strategy;                  ///< send new coupling strategy
146     int      cpl_in_use;                        ///< coupling in use for this block     (cplinu)
147     uint8_t  channel_in_cpl[AC3_MAX_CHANNELS];  ///< channel in coupling                (chincpl)
148     int      num_cpl_channels;                  ///< number of channels in coupling
149     uint8_t  new_cpl_coords[AC3_MAX_CHANNELS];  ///< send new coupling coordinates      (cplcoe)
150     uint8_t  cpl_master_exp[AC3_MAX_CHANNELS];  ///< coupling coord master exponents    (mstrcplco)
151     int      new_snr_offsets;                   ///< send new SNR offsets
152     int      new_cpl_leak;                      ///< send new coupling leak info
153     int      end_freq[AC3_MAX_CHANNELS];        ///< end frequency bin                  (endmant)
154 } AC3Block;
155
156 /**
157  * AC-3 encoder private context.
158  */
159 typedef struct AC3EncodeContext {
160     AVClass *av_class;                      ///< AVClass used for AVOption
161     AC3EncOptions options;                  ///< encoding options
162     AVCodecContext *avctx;                  ///< parent AVCodecContext
163     PutBitContext pb;                       ///< bitstream writer context
164     DSPContext dsp;
165     AVFloatDSPContext fdsp;
166     AC3DSPContext ac3dsp;                   ///< AC-3 optimized functions
167     FFTContext mdct;                        ///< FFT context for MDCT calculation
168     const SampleType *mdct_window;          ///< MDCT window function array
169
170     AC3Block blocks[AC3_MAX_BLOCKS];        ///< per-block info
171
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)
176
177     int bit_rate;                           ///< target bit rate, in bits-per-second
178     int sample_rate;                        ///< sampling frequency, in Hz
179
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)
188
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
197
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
204
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
209
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
215
216     int rematrixing_enabled;                ///< stereo rematrixing enabled
217
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
231
232     SampleType *windowed_samples;
233     SampleType **planar_samples;
234     uint8_t *bap_buffer;
235     uint8_t *bap1_buffer;
236     CoefType *mdct_coef_buffer;
237     int32_t *fixed_coef_buffer;
238     uint8_t *exp_buffer;
239     uint8_t *grouped_exp_buffer;
240     int16_t *psd_buffer;
241     int16_t *band_psd_buffer;
242     int16_t *mask_buffer;
243     int16_t *qmant_buffer;
244     uint8_t *cpl_coord_exp_buffer;
245     uint8_t *cpl_coord_mant_buffer;
246
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
253
254     /* fixed vs. float function pointers */
255     void (*mdct_end)(struct AC3EncodeContext *s);
256     int  (*mdct_init)(struct AC3EncodeContext *s);
257
258     /* fixed vs. float templated function pointers */
259     int  (*allocate_sample_buffers)(struct AC3EncodeContext *s);
260
261     /* AC-3 vs. E-AC-3 function pointers */
262     void (*output_frame_header)(struct AC3EncodeContext *s);
263 } AC3EncodeContext;
264
265
266 extern const uint64_t ff_ac3_channel_layouts[19];
267
268 int ff_ac3_encode_init(AVCodecContext *avctx);
269
270 int ff_ac3_encode_close(AVCodecContext *avctx);
271
272 int ff_ac3_validate_metadata(AC3EncodeContext *s);
273
274 void ff_ac3_adjust_frame_size(AC3EncodeContext *s);
275
276 void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s);
277
278 void ff_ac3_apply_rematrixing(AC3EncodeContext *s);
279
280 void ff_ac3_process_exponents(AC3EncodeContext *s);
281
282 int ff_ac3_compute_bit_allocation(AC3EncodeContext *s);
283
284 void ff_ac3_group_exponents(AC3EncodeContext *s);
285
286 void ff_ac3_quantize_mantissas(AC3EncodeContext *s);
287
288 void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame);
289
290
291 /* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
292
293 void ff_ac3_fixed_mdct_end(AC3EncodeContext *s);
294 void ff_ac3_float_mdct_end(AC3EncodeContext *s);
295
296 int ff_ac3_fixed_mdct_init(AC3EncodeContext *s);
297 int ff_ac3_float_mdct_init(AC3EncodeContext *s);
298
299
300 /* prototypes for functions in ac3enc_template.c */
301
302 int ff_ac3_fixed_allocate_sample_buffers(AC3EncodeContext *s);
303 int ff_ac3_float_allocate_sample_buffers(AC3EncodeContext *s);
304
305 int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
306                               const AVFrame *frame, int *got_packet_ptr);
307 int ff_ac3_float_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
308                               const AVFrame *frame, int *got_packet_ptr);
309
310 #endif /* AVCODEC_AC3ENC_H */