00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #ifndef AVCODEC_AC3ENC_H
00029 #define AVCODEC_AC3ENC_H
00030
00031 #include <stdint.h>
00032
00033 #include "libavutil/float_dsp.h"
00034 #include "ac3.h"
00035 #include "ac3dsp.h"
00036 #include "avcodec.h"
00037 #include "dsputil.h"
00038 #include "put_bits.h"
00039 #include "fft.h"
00040
00041 #ifndef CONFIG_AC3ENC_FLOAT
00042 #define CONFIG_AC3ENC_FLOAT 0
00043 #endif
00044
00045 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
00046 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
00047
00048 #define AC3ENC_TYPE_AC3_FIXED 0
00049 #define AC3ENC_TYPE_AC3 1
00050 #define AC3ENC_TYPE_EAC3 2
00051
00052 #if CONFIG_AC3ENC_FLOAT
00053 #define AC3_NAME(x) ff_ac3_float_ ## x
00054 #define MAC_COEF(d,a,b) ((d)+=(a)*(b))
00055 #define COEF_MIN (-16777215.0/16777216.0)
00056 #define COEF_MAX ( 16777215.0/16777216.0)
00057 #define NEW_CPL_COORD_THRESHOLD 0.03
00058 typedef float SampleType;
00059 typedef float CoefType;
00060 typedef float CoefSumType;
00061 #else
00062 #define AC3_NAME(x) ff_ac3_fixed_ ## x
00063 #define MAC_COEF(d,a,b) MAC64(d,a,b)
00064 #define COEF_MIN -16777215
00065 #define COEF_MAX 16777215
00066 #define NEW_CPL_COORD_THRESHOLD 503317
00067 typedef int16_t SampleType;
00068 typedef int32_t CoefType;
00069 typedef int64_t CoefSumType;
00070 #endif
00071
00072
00073 #define AC3ENC_OPT_NONE -1
00074 #define AC3ENC_OPT_AUTO -1
00075 #define AC3ENC_OPT_OFF 0
00076 #define AC3ENC_OPT_ON 1
00077 #define AC3ENC_OPT_NOT_INDICATED 0
00078 #define AC3ENC_OPT_MODE_ON 2
00079 #define AC3ENC_OPT_MODE_OFF 1
00080
00081
00082 #define AC3ENC_OPT_LARGE_ROOM 1
00083 #define AC3ENC_OPT_SMALL_ROOM 2
00084 #define AC3ENC_OPT_DOWNMIX_LTRT 1
00085 #define AC3ENC_OPT_DOWNMIX_LORO 2
00086 #define AC3ENC_OPT_ADCONV_STANDARD 0
00087 #define AC3ENC_OPT_ADCONV_HDCD 1
00088
00089
00093 typedef struct AC3EncOptions {
00094
00095 int dialogue_level;
00096 int bitstream_mode;
00097 float center_mix_level;
00098 float surround_mix_level;
00099 int dolby_surround_mode;
00100 int audio_production_info;
00101 int mixing_level;
00102 int room_type;
00103 int copyright;
00104 int original;
00105 int extended_bsi_1;
00106 int preferred_stereo_downmix;
00107 float ltrt_center_mix_level;
00108 float ltrt_surround_mix_level;
00109 float loro_center_mix_level;
00110 float loro_surround_mix_level;
00111 int extended_bsi_2;
00112 int dolby_surround_ex_mode;
00113 int dolby_headphone_mode;
00114 int ad_converter_type;
00115 int eac3_mixing_metadata;
00116 int eac3_info_metadata;
00117
00118
00119 int allow_per_frame_metadata;
00120 int stereo_rematrixing;
00121 int channel_coupling;
00122 int cpl_start;
00123 } AC3EncOptions;
00124
00128 typedef struct AC3Block {
00129 CoefType **mdct_coef;
00130 int32_t **fixed_coef;
00131 uint8_t **exp;
00132 uint8_t **grouped_exp;
00133 int16_t **psd;
00134 int16_t **band_psd;
00135 int16_t **mask;
00136 uint16_t **qmant;
00137 uint8_t **cpl_coord_exp;
00138 uint8_t **cpl_coord_mant;
00139 uint8_t coeff_shift[AC3_MAX_CHANNELS];
00140 uint8_t new_rematrixing_strategy;
00141 int num_rematrixing_bands;
00142 uint8_t rematrixing_flags[4];
00143 int new_cpl_strategy;
00144 int cpl_in_use;
00145 uint8_t channel_in_cpl[AC3_MAX_CHANNELS];
00146 int num_cpl_channels;
00147 uint8_t new_cpl_coords[AC3_MAX_CHANNELS];
00148 uint8_t cpl_master_exp[AC3_MAX_CHANNELS];
00149 int new_snr_offsets;
00150 int new_cpl_leak;
00151 int end_freq[AC3_MAX_CHANNELS];
00152 } AC3Block;
00153
00157 typedef struct AC3EncodeContext {
00158 AVClass *av_class;
00159 AC3EncOptions options;
00160 AVCodecContext *avctx;
00161 PutBitContext pb;
00162 DSPContext dsp;
00163 AVFloatDSPContext fdsp;
00164 AC3DSPContext ac3dsp;
00165 FFTContext mdct;
00166 const SampleType *mdct_window;
00167
00168 AC3Block blocks[AC3_MAX_BLOCKS];
00169
00170 int fixed_point;
00171 int eac3;
00172 int bitstream_id;
00173 int bitstream_mode;
00174
00175 int bit_rate;
00176 int sample_rate;
00177
00178 int num_blks_code;
00179 int num_blocks;
00180 int frame_size_min;
00181 int frame_size;
00182 int frame_size_code;
00183 uint16_t crc_inv[2];
00184 int64_t bits_written;
00185 int64_t samples_written;
00186
00187 int fbw_channels;
00188 int channels;
00189 int lfe_on;
00190 int lfe_channel;
00191 int has_center;
00192 int has_surround;
00193 int channel_mode;
00194 const uint8_t *channel_map;
00195
00196 int center_mix_level;
00197 int surround_mix_level;
00198 int ltrt_center_mix_level;
00199 int ltrt_surround_mix_level;
00200 int loro_center_mix_level;
00201 int loro_surround_mix_level;
00202
00203 int cutoff;
00204 int bandwidth_code;
00205 int start_freq[AC3_MAX_CHANNELS];
00206 int cpl_end_freq;
00207
00208 int cpl_on;
00209 int cpl_enabled;
00210 int num_cpl_subbands;
00211 int num_cpl_bands;
00212 uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS];
00213
00214 int rematrixing_enabled;
00215
00216
00217 int slow_gain_code;
00218 int slow_decay_code;
00219 int fast_decay_code;
00220 int db_per_bit_code;
00221 int floor_code;
00222 AC3BitAllocParameters bit_alloc;
00223 int coarse_snr_offset;
00224 int fast_gain_code[AC3_MAX_CHANNELS];
00225 int fine_snr_offset[AC3_MAX_CHANNELS];
00226 int frame_bits_fixed;
00227 int frame_bits;
00228 int exponent_bits;
00229
00230 SampleType *windowed_samples;
00231 SampleType **planar_samples;
00232 uint8_t *bap_buffer;
00233 uint8_t *bap1_buffer;
00234 CoefType *mdct_coef_buffer;
00235 int32_t *fixed_coef_buffer;
00236 uint8_t *exp_buffer;
00237 uint8_t *grouped_exp_buffer;
00238 int16_t *psd_buffer;
00239 int16_t *band_psd_buffer;
00240 int16_t *mask_buffer;
00241 int16_t *qmant_buffer;
00242 uint8_t *cpl_coord_exp_buffer;
00243 uint8_t *cpl_coord_mant_buffer;
00244
00245 uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
00246 uint8_t frame_exp_strategy[AC3_MAX_CHANNELS];
00247 int use_frame_exp_strategy;
00248 uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
00249 uint8_t *ref_bap [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
00250 int ref_bap_set;
00251
00252
00253 void (*mdct_end)(struct AC3EncodeContext *s);
00254 int (*mdct_init)(struct AC3EncodeContext *s);
00255
00256
00257 int (*allocate_sample_buffers)(struct AC3EncodeContext *s);
00258
00259
00260 void (*output_frame_header)(struct AC3EncodeContext *s);
00261 } AC3EncodeContext;
00262
00263
00264 extern const uint64_t ff_ac3_channel_layouts[19];
00265
00266 int ff_ac3_encode_init(AVCodecContext *avctx);
00267
00268 int ff_ac3_encode_close(AVCodecContext *avctx);
00269
00270 int ff_ac3_validate_metadata(AC3EncodeContext *s);
00271
00272 void ff_ac3_adjust_frame_size(AC3EncodeContext *s);
00273
00274 void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s);
00275
00276 void ff_ac3_apply_rematrixing(AC3EncodeContext *s);
00277
00278 void ff_ac3_process_exponents(AC3EncodeContext *s);
00279
00280 int ff_ac3_compute_bit_allocation(AC3EncodeContext *s);
00281
00282 void ff_ac3_group_exponents(AC3EncodeContext *s);
00283
00284 void ff_ac3_quantize_mantissas(AC3EncodeContext *s);
00285
00286 void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame);
00287
00288
00289
00290
00291 void ff_ac3_fixed_mdct_end(AC3EncodeContext *s);
00292 void ff_ac3_float_mdct_end(AC3EncodeContext *s);
00293
00294 int ff_ac3_fixed_mdct_init(AC3EncodeContext *s);
00295 int ff_ac3_float_mdct_init(AC3EncodeContext *s);
00296
00297
00298
00299
00300 int ff_ac3_fixed_allocate_sample_buffers(AC3EncodeContext *s);
00301 int ff_ac3_float_allocate_sample_buffers(AC3EncodeContext *s);
00302
00303 int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00304 const AVFrame *frame, int *got_packet_ptr);
00305 int ff_ac3_float_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00306 const AVFrame *frame, int *got_packet_ptr);
00307
00308 #endif