annotate ffmpeg/libavcodec/ac3enc.h @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * AC-3 encoder & E-AC-3 encoder common header
yading@10 3 * Copyright (c) 2000 Fabrice Bellard
yading@10 4 * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
yading@10 5 *
yading@10 6 * This file is part of Libav.
yading@10 7 *
yading@10 8 * Libav is free software; you can redistribute it and/or
yading@10 9 * modify it under the terms of the GNU Lesser General Public
yading@10 10 * License as published by the Free Software Foundation; either
yading@10 11 * version 2.1 of the License, or (at your option) any later version.
yading@10 12 *
yading@10 13 * Libav is distributed in the hope that it will be useful,
yading@10 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 16 * Lesser General Public License for more details.
yading@10 17 *
yading@10 18 * You should have received a copy of the GNU Lesser General Public
yading@10 19 * License along with Libav; if not, write to the Free Software
yading@10 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 21 */
yading@10 22
yading@10 23 /**
yading@10 24 * @file
yading@10 25 * AC-3 encoder & E-AC-3 encoder common header
yading@10 26 */
yading@10 27
yading@10 28 #ifndef AVCODEC_AC3ENC_H
yading@10 29 #define AVCODEC_AC3ENC_H
yading@10 30
yading@10 31 #include <stdint.h>
yading@10 32
yading@10 33 #include "libavutil/float_dsp.h"
yading@10 34 #include "ac3.h"
yading@10 35 #include "ac3dsp.h"
yading@10 36 #include "avcodec.h"
yading@10 37 #include "dsputil.h"
yading@10 38 #include "put_bits.h"
yading@10 39 #include "fft.h"
yading@10 40
yading@10 41 #ifndef CONFIG_AC3ENC_FLOAT
yading@10 42 #define CONFIG_AC3ENC_FLOAT 0
yading@10 43 #endif
yading@10 44
yading@10 45 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
yading@10 46 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
yading@10 47
yading@10 48 #define AC3ENC_TYPE_AC3_FIXED 0
yading@10 49 #define AC3ENC_TYPE_AC3 1
yading@10 50 #define AC3ENC_TYPE_EAC3 2
yading@10 51
yading@10 52 #if CONFIG_AC3ENC_FLOAT
yading@10 53 #define AC3_NAME(x) ff_ac3_float_ ## x
yading@10 54 #define MAC_COEF(d,a,b) ((d)+=(a)*(b))
yading@10 55 #define COEF_MIN (-16777215.0/16777216.0)
yading@10 56 #define COEF_MAX ( 16777215.0/16777216.0)
yading@10 57 #define NEW_CPL_COORD_THRESHOLD 0.03
yading@10 58 typedef float SampleType;
yading@10 59 typedef float CoefType;
yading@10 60 typedef float CoefSumType;
yading@10 61 #else
yading@10 62 #define AC3_NAME(x) ff_ac3_fixed_ ## x
yading@10 63 #define MAC_COEF(d,a,b) MAC64(d,a,b)
yading@10 64 #define COEF_MIN -16777215
yading@10 65 #define COEF_MAX 16777215
yading@10 66 #define NEW_CPL_COORD_THRESHOLD 503317
yading@10 67 typedef int16_t SampleType;
yading@10 68 typedef int32_t CoefType;
yading@10 69 typedef int64_t CoefSumType;
yading@10 70 #endif
yading@10 71
yading@10 72 /* common option values */
yading@10 73 #define AC3ENC_OPT_NONE -1
yading@10 74 #define AC3ENC_OPT_AUTO -1
yading@10 75 #define AC3ENC_OPT_OFF 0
yading@10 76 #define AC3ENC_OPT_ON 1
yading@10 77 #define AC3ENC_OPT_NOT_INDICATED 0
yading@10 78 #define AC3ENC_OPT_MODE_ON 2
yading@10 79 #define AC3ENC_OPT_MODE_OFF 1
yading@10 80
yading@10 81 /* specific option values */
yading@10 82 #define AC3ENC_OPT_LARGE_ROOM 1
yading@10 83 #define AC3ENC_OPT_SMALL_ROOM 2
yading@10 84 #define AC3ENC_OPT_DOWNMIX_LTRT 1
yading@10 85 #define AC3ENC_OPT_DOWNMIX_LORO 2
yading@10 86 #define AC3ENC_OPT_ADCONV_STANDARD 0
yading@10 87 #define AC3ENC_OPT_ADCONV_HDCD 1
yading@10 88
yading@10 89
yading@10 90 /**
yading@10 91 * Encoding Options used by AVOption.
yading@10 92 */
yading@10 93 typedef struct AC3EncOptions {
yading@10 94 /* AC-3 metadata options*/
yading@10 95 int dialogue_level;
yading@10 96 int bitstream_mode;
yading@10 97 float center_mix_level;
yading@10 98 float surround_mix_level;
yading@10 99 int dolby_surround_mode;
yading@10 100 int audio_production_info;
yading@10 101 int mixing_level;
yading@10 102 int room_type;
yading@10 103 int copyright;
yading@10 104 int original;
yading@10 105 int extended_bsi_1;
yading@10 106 int preferred_stereo_downmix;
yading@10 107 float ltrt_center_mix_level;
yading@10 108 float ltrt_surround_mix_level;
yading@10 109 float loro_center_mix_level;
yading@10 110 float loro_surround_mix_level;
yading@10 111 int extended_bsi_2;
yading@10 112 int dolby_surround_ex_mode;
yading@10 113 int dolby_headphone_mode;
yading@10 114 int ad_converter_type;
yading@10 115 int eac3_mixing_metadata;
yading@10 116 int eac3_info_metadata;
yading@10 117
yading@10 118 /* other encoding options */
yading@10 119 int allow_per_frame_metadata;
yading@10 120 int stereo_rematrixing;
yading@10 121 int channel_coupling;
yading@10 122 int cpl_start;
yading@10 123 } AC3EncOptions;
yading@10 124
yading@10 125 /**
yading@10 126 * Data for a single audio block.
yading@10 127 */
yading@10 128 typedef struct AC3Block {
yading@10 129 CoefType **mdct_coef; ///< MDCT coefficients
yading@10 130 int32_t **fixed_coef; ///< fixed-point MDCT coefficients
yading@10 131 uint8_t **exp; ///< original exponents
yading@10 132 uint8_t **grouped_exp; ///< grouped exponents
yading@10 133 int16_t **psd; ///< psd per frequency bin
yading@10 134 int16_t **band_psd; ///< psd per critical band
yading@10 135 int16_t **mask; ///< masking curve
yading@10 136 uint16_t **qmant; ///< quantized mantissas
yading@10 137 uint8_t **cpl_coord_exp; ///< coupling coord exponents (cplcoexp)
yading@10 138 uint8_t **cpl_coord_mant; ///< coupling coord mantissas (cplcomant)
yading@10 139 uint8_t coeff_shift[AC3_MAX_CHANNELS]; ///< fixed-point coefficient shift values
yading@10 140 uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block
yading@10 141 int num_rematrixing_bands; ///< number of rematrixing bands
yading@10 142 uint8_t rematrixing_flags[4]; ///< rematrixing flags
yading@10 143 int new_cpl_strategy; ///< send new coupling strategy
yading@10 144 int cpl_in_use; ///< coupling in use for this block (cplinu)
yading@10 145 uint8_t channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling (chincpl)
yading@10 146 int num_cpl_channels; ///< number of channels in coupling
yading@10 147 uint8_t new_cpl_coords[AC3_MAX_CHANNELS]; ///< send new coupling coordinates (cplcoe)
yading@10 148 uint8_t cpl_master_exp[AC3_MAX_CHANNELS]; ///< coupling coord master exponents (mstrcplco)
yading@10 149 int new_snr_offsets; ///< send new SNR offsets
yading@10 150 int new_cpl_leak; ///< send new coupling leak info
yading@10 151 int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant)
yading@10 152 } AC3Block;
yading@10 153
yading@10 154 /**
yading@10 155 * AC-3 encoder private context.
yading@10 156 */
yading@10 157 typedef struct AC3EncodeContext {
yading@10 158 AVClass *av_class; ///< AVClass used for AVOption
yading@10 159 AC3EncOptions options; ///< encoding options
yading@10 160 AVCodecContext *avctx; ///< parent AVCodecContext
yading@10 161 PutBitContext pb; ///< bitstream writer context
yading@10 162 DSPContext dsp;
yading@10 163 AVFloatDSPContext fdsp;
yading@10 164 AC3DSPContext ac3dsp; ///< AC-3 optimized functions
yading@10 165 FFTContext mdct; ///< FFT context for MDCT calculation
yading@10 166 const SampleType *mdct_window; ///< MDCT window function array
yading@10 167
yading@10 168 AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info
yading@10 169
yading@10 170 int fixed_point; ///< indicates if fixed-point encoder is being used
yading@10 171 int eac3; ///< indicates if this is E-AC-3 vs. AC-3
yading@10 172 int bitstream_id; ///< bitstream id (bsid)
yading@10 173 int bitstream_mode; ///< bitstream mode (bsmod)
yading@10 174
yading@10 175 int bit_rate; ///< target bit rate, in bits-per-second
yading@10 176 int sample_rate; ///< sampling frequency, in Hz
yading@10 177
yading@10 178 int num_blks_code; ///< number of blocks code (numblkscod)
yading@10 179 int num_blocks; ///< number of blocks per frame
yading@10 180 int frame_size_min; ///< minimum frame size in case rounding is necessary
yading@10 181 int frame_size; ///< current frame size in bytes
yading@10 182 int frame_size_code; ///< frame size code (frmsizecod)
yading@10 183 uint16_t crc_inv[2];
yading@10 184 int64_t bits_written; ///< bit count (used to avg. bitrate)
yading@10 185 int64_t samples_written; ///< sample count (used to avg. bitrate)
yading@10 186
yading@10 187 int fbw_channels; ///< number of full-bandwidth channels (nfchans)
yading@10 188 int channels; ///< total number of channels (nchans)
yading@10 189 int lfe_on; ///< indicates if there is an LFE channel (lfeon)
yading@10 190 int lfe_channel; ///< channel index of the LFE channel
yading@10 191 int has_center; ///< indicates if there is a center channel
yading@10 192 int has_surround; ///< indicates if there are one or more surround channels
yading@10 193 int channel_mode; ///< channel mode (acmod)
yading@10 194 const uint8_t *channel_map; ///< channel map used to reorder channels
yading@10 195
yading@10 196 int center_mix_level; ///< center mix level code
yading@10 197 int surround_mix_level; ///< surround mix level code
yading@10 198 int ltrt_center_mix_level; ///< Lt/Rt center mix level code
yading@10 199 int ltrt_surround_mix_level; ///< Lt/Rt surround mix level code
yading@10 200 int loro_center_mix_level; ///< Lo/Ro center mix level code
yading@10 201 int loro_surround_mix_level; ///< Lo/Ro surround mix level code
yading@10 202
yading@10 203 int cutoff; ///< user-specified cutoff frequency, in Hz
yading@10 204 int bandwidth_code; ///< bandwidth code (0 to 60) (chbwcod)
yading@10 205 int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin (strtmant)
yading@10 206 int cpl_end_freq; ///< coupling channel end frequency bin
yading@10 207
yading@10 208 int cpl_on; ///< coupling turned on for this frame
yading@10 209 int cpl_enabled; ///< coupling enabled for all frames
yading@10 210 int num_cpl_subbands; ///< number of coupling subbands (ncplsubnd)
yading@10 211 int num_cpl_bands; ///< number of coupling bands (ncplbnd)
yading@10 212 uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS]; ///< number of coeffs in each coupling band
yading@10 213
yading@10 214 int rematrixing_enabled; ///< stereo rematrixing enabled
yading@10 215
yading@10 216 /* bitrate allocation control */
yading@10 217 int slow_gain_code; ///< slow gain code (sgaincod)
yading@10 218 int slow_decay_code; ///< slow decay code (sdcycod)
yading@10 219 int fast_decay_code; ///< fast decay code (fdcycod)
yading@10 220 int db_per_bit_code; ///< dB/bit code (dbpbcod)
yading@10 221 int floor_code; ///< floor code (floorcod)
yading@10 222 AC3BitAllocParameters bit_alloc; ///< bit allocation parameters
yading@10 223 int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
yading@10 224 int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
yading@10 225 int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
yading@10 226 int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
yading@10 227 int frame_bits; ///< all frame bits except exponents and mantissas
yading@10 228 int exponent_bits; ///< number of bits used for exponents
yading@10 229
yading@10 230 SampleType *windowed_samples;
yading@10 231 SampleType **planar_samples;
yading@10 232 uint8_t *bap_buffer;
yading@10 233 uint8_t *bap1_buffer;
yading@10 234 CoefType *mdct_coef_buffer;
yading@10 235 int32_t *fixed_coef_buffer;
yading@10 236 uint8_t *exp_buffer;
yading@10 237 uint8_t *grouped_exp_buffer;
yading@10 238 int16_t *psd_buffer;
yading@10 239 int16_t *band_psd_buffer;
yading@10 240 int16_t *mask_buffer;
yading@10 241 int16_t *qmant_buffer;
yading@10 242 uint8_t *cpl_coord_exp_buffer;
yading@10 243 uint8_t *cpl_coord_mant_buffer;
yading@10 244
yading@10 245 uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
yading@10 246 uint8_t frame_exp_strategy[AC3_MAX_CHANNELS]; ///< frame exp strategy index
yading@10 247 int use_frame_exp_strategy; ///< indicates use of frame exp strategy
yading@10 248 uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< reference blocks for EXP_REUSE
yading@10 249 uint8_t *ref_bap [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< bit allocation pointers (bap)
yading@10 250 int ref_bap_set; ///< indicates if ref_bap pointers have been set
yading@10 251
yading@10 252 /* fixed vs. float function pointers */
yading@10 253 void (*mdct_end)(struct AC3EncodeContext *s);
yading@10 254 int (*mdct_init)(struct AC3EncodeContext *s);
yading@10 255
yading@10 256 /* fixed vs. float templated function pointers */
yading@10 257 int (*allocate_sample_buffers)(struct AC3EncodeContext *s);
yading@10 258
yading@10 259 /* AC-3 vs. E-AC-3 function pointers */
yading@10 260 void (*output_frame_header)(struct AC3EncodeContext *s);
yading@10 261 } AC3EncodeContext;
yading@10 262
yading@10 263
yading@10 264 extern const uint64_t ff_ac3_channel_layouts[19];
yading@10 265
yading@10 266 int ff_ac3_encode_init(AVCodecContext *avctx);
yading@10 267
yading@10 268 int ff_ac3_encode_close(AVCodecContext *avctx);
yading@10 269
yading@10 270 int ff_ac3_validate_metadata(AC3EncodeContext *s);
yading@10 271
yading@10 272 void ff_ac3_adjust_frame_size(AC3EncodeContext *s);
yading@10 273
yading@10 274 void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s);
yading@10 275
yading@10 276 void ff_ac3_apply_rematrixing(AC3EncodeContext *s);
yading@10 277
yading@10 278 void ff_ac3_process_exponents(AC3EncodeContext *s);
yading@10 279
yading@10 280 int ff_ac3_compute_bit_allocation(AC3EncodeContext *s);
yading@10 281
yading@10 282 void ff_ac3_group_exponents(AC3EncodeContext *s);
yading@10 283
yading@10 284 void ff_ac3_quantize_mantissas(AC3EncodeContext *s);
yading@10 285
yading@10 286 void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame);
yading@10 287
yading@10 288
yading@10 289 /* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
yading@10 290
yading@10 291 void ff_ac3_fixed_mdct_end(AC3EncodeContext *s);
yading@10 292 void ff_ac3_float_mdct_end(AC3EncodeContext *s);
yading@10 293
yading@10 294 int ff_ac3_fixed_mdct_init(AC3EncodeContext *s);
yading@10 295 int ff_ac3_float_mdct_init(AC3EncodeContext *s);
yading@10 296
yading@10 297
yading@10 298 /* prototypes for functions in ac3enc_template.c */
yading@10 299
yading@10 300 int ff_ac3_fixed_allocate_sample_buffers(AC3EncodeContext *s);
yading@10 301 int ff_ac3_float_allocate_sample_buffers(AC3EncodeContext *s);
yading@10 302
yading@10 303 int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
yading@10 304 const AVFrame *frame, int *got_packet_ptr);
yading@10 305 int ff_ac3_float_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
yading@10 306 const AVFrame *frame, int *got_packet_ptr);
yading@10 307
yading@10 308 #endif /* AVCODEC_AC3ENC_H */