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