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