ac3enc.h 13.5 KB
Newer Older
1 2 3 4 5
/*
 * AC-3 encoder & E-AC-3 encoder common header
 * Copyright (c) 2000 Fabrice Bellard
 * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9 10 11 12
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14 15 16 17 18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 21 22 23 24 25 26 27 28 29 30 31
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * AC-3 encoder & E-AC-3 encoder common header
 */

#ifndef AVCODEC_AC3ENC_H
#define AVCODEC_AC3ENC_H

#include <stdint.h>
32 33

#include "libavutil/float_dsp.h"
34

35 36 37 38
#include "ac3.h"
#include "ac3dsp.h"
#include "avcodec.h"
#include "fft.h"
39
#include "mathops.h"
40
#include "me_cmp.h"
41
#include "put_bits.h"
42
#include "audiodsp.h"
43 44 45 46 47

#ifndef CONFIG_AC3ENC_FLOAT
#define CONFIG_AC3ENC_FLOAT 0
#endif

48 49 50 51 52 53 54
#define OFFSET(param) offsetof(AC3EncodeContext, options.param)
#define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)

#define AC3ENC_TYPE_AC3_FIXED   0
#define AC3ENC_TYPE_AC3         1
#define AC3ENC_TYPE_EAC3        2

55
#if CONFIG_AC3ENC_FLOAT
56
#define AC3_NAME(x) ff_ac3_float_ ## x
57
#define MAC_COEF(d,a,b) ((d)+=(a)*(b))
58 59
#define COEF_MIN (-16777215.0/16777216.0)
#define COEF_MAX ( 16777215.0/16777216.0)
60
#define NEW_CPL_COORD_THRESHOLD 0.03
61 62 63 64
typedef float SampleType;
typedef float CoefType;
typedef float CoefSumType;
#else
65
#define AC3_NAME(x) ff_ac3_fixed_ ## x
66
#define MAC_COEF(d,a,b) MAC64(d,a,b)
67 68
#define COEF_MIN -16777215
#define COEF_MAX  16777215
69
#define NEW_CPL_COORD_THRESHOLD 503317
70 71 72 73 74
typedef int16_t SampleType;
typedef int32_t CoefType;
typedef int64_t CoefSumType;
#endif

75 76 77 78 79 80
/* common option values */
#define AC3ENC_OPT_NONE            -1
#define AC3ENC_OPT_AUTO            -1
#define AC3ENC_OPT_OFF              0
#define AC3ENC_OPT_ON               1
#define AC3ENC_OPT_NOT_INDICATED    0
81 82
#define AC3ENC_OPT_MODE_ON          2
#define AC3ENC_OPT_MODE_OFF         1
83
#define AC3ENC_OPT_DSUREX_DPLIIZ    3
84 85 86 87 88 89

/* specific option values */
#define AC3ENC_OPT_LARGE_ROOM       1
#define AC3ENC_OPT_SMALL_ROOM       2
#define AC3ENC_OPT_DOWNMIX_LTRT     1
#define AC3ENC_OPT_DOWNMIX_LORO     2
90
#define AC3ENC_OPT_DOWNMIX_DPLII    3 // reserved value in A/52, but used by encoders to indicate DPL2
91 92 93
#define AC3ENC_OPT_ADCONV_STANDARD  0
#define AC3ENC_OPT_ADCONV_HDCD      1

94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/**
 * Encoding Options used by AVOption.
 */
typedef struct AC3EncOptions {
    /* AC-3 metadata options*/
    int dialogue_level;
    int bitstream_mode;
    float center_mix_level;
    float surround_mix_level;
    int dolby_surround_mode;
    int audio_production_info;
    int mixing_level;
    int room_type;
    int copyright;
    int original;
    int extended_bsi_1;
    int preferred_stereo_downmix;
    float ltrt_center_mix_level;
    float ltrt_surround_mix_level;
    float loro_center_mix_level;
    float loro_surround_mix_level;
    int extended_bsi_2;
    int dolby_surround_ex_mode;
    int dolby_headphone_mode;
    int ad_converter_type;
120 121
    int eac3_mixing_metadata;
    int eac3_info_metadata;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

    /* other encoding options */
    int allow_per_frame_metadata;
    int stereo_rematrixing;
    int channel_coupling;
    int cpl_start;
} AC3EncOptions;

/**
 * Data for a single audio block.
 */
typedef struct AC3Block {
    CoefType **mdct_coef;                       ///< MDCT coefficients
    int32_t  **fixed_coef;                      ///< fixed-point MDCT coefficients
    uint8_t  **exp;                             ///< original exponents
    uint8_t  **grouped_exp;                     ///< grouped exponents
    int16_t  **psd;                             ///< psd per frequency bin
    int16_t  **band_psd;                        ///< psd per critical band
    int16_t  **mask;                            ///< masking curve
    uint16_t **qmant;                           ///< quantized mantissas
    uint8_t  **cpl_coord_exp;                   ///< coupling coord exponents           (cplcoexp)
    uint8_t  **cpl_coord_mant;                  ///< coupling coord mantissas           (cplcomant)
    uint8_t  coeff_shift[AC3_MAX_CHANNELS];     ///< fixed-point coefficient shift values
    uint8_t  new_rematrixing_strategy;          ///< send new rematrixing flags in this block
    int      num_rematrixing_bands;             ///< number of rematrixing bands
    uint8_t  rematrixing_flags[4];              ///< rematrixing flags
    int      new_cpl_strategy;                  ///< send new coupling strategy
    int      cpl_in_use;                        ///< coupling in use for this block     (cplinu)
    uint8_t  channel_in_cpl[AC3_MAX_CHANNELS];  ///< channel in coupling                (chincpl)
    int      num_cpl_channels;                  ///< number of channels in coupling
152
    uint8_t  new_cpl_coords[AC3_MAX_CHANNELS];  ///< send new coupling coordinates      (cplcoe)
153 154 155 156 157 158 159 160 161 162 163 164
    uint8_t  cpl_master_exp[AC3_MAX_CHANNELS];  ///< coupling coord master exponents    (mstrcplco)
    int      new_snr_offsets;                   ///< send new SNR offsets
    int      new_cpl_leak;                      ///< send new coupling leak info
    int      end_freq[AC3_MAX_CHANNELS];        ///< end frequency bin                  (endmant)
} AC3Block;

/**
 * AC-3 encoder private context.
 */
typedef struct AC3EncodeContext {
    AVClass *av_class;                      ///< AVClass used for AVOption
    AC3EncOptions options;                  ///< encoding options
165
    AVCodecContext *avctx;                  ///< parent AVCodecContext
166
    PutBitContext pb;                       ///< bitstream writer context
167
    AudioDSPContext adsp;
168
    AVFloatDSPContext *fdsp;
169
    MECmpContext mecc;
170
    AC3DSPContext ac3dsp;                   ///< AC-3 optimized functions
171 172
    FFTContext mdct;                        ///< FFT context for MDCT calculation
    const SampleType *mdct_window;          ///< MDCT window function array
173 174 175

    AC3Block blocks[AC3_MAX_BLOCKS];        ///< per-block info

176
    int fixed_point;                        ///< indicates if fixed-point encoder is being used
177 178 179 180 181 182 183
    int eac3;                               ///< indicates if this is E-AC-3 vs. AC-3
    int bitstream_id;                       ///< bitstream id                           (bsid)
    int bitstream_mode;                     ///< bitstream mode                         (bsmod)

    int bit_rate;                           ///< target bit rate, in bits-per-second
    int sample_rate;                        ///< sampling frequency, in Hz

184 185
    int num_blks_code;                      ///< number of blocks code                  (numblkscod)
    int num_blocks;                         ///< number of blocks per frame
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    int frame_size_min;                     ///< minimum frame size in case rounding is necessary
    int frame_size;                         ///< current frame size in bytes
    int frame_size_code;                    ///< frame size code                        (frmsizecod)
    uint16_t crc_inv[2];
    int64_t bits_written;                   ///< bit count    (used to avg. bitrate)
    int64_t samples_written;                ///< sample count (used to avg. bitrate)

    int fbw_channels;                       ///< number of full-bandwidth channels      (nfchans)
    int channels;                           ///< total number of channels               (nchans)
    int lfe_on;                             ///< indicates if there is an LFE channel   (lfeon)
    int lfe_channel;                        ///< channel index of the LFE channel
    int has_center;                         ///< indicates if there is a center channel
    int has_surround;                       ///< indicates if there are one or more surround channels
    int channel_mode;                       ///< channel mode                           (acmod)
    const uint8_t *channel_map;             ///< channel map used to reorder channels

    int center_mix_level;                   ///< center mix level code
    int surround_mix_level;                 ///< surround mix level code
    int ltrt_center_mix_level;              ///< Lt/Rt center mix level code
    int ltrt_surround_mix_level;            ///< Lt/Rt surround mix level code
    int loro_center_mix_level;              ///< Lo/Ro center mix level code
    int loro_surround_mix_level;            ///< Lo/Ro surround mix level code

    int cutoff;                             ///< user-specified cutoff frequency, in Hz
    int bandwidth_code;                     ///< bandwidth code (0 to 60)               (chbwcod)
    int start_freq[AC3_MAX_CHANNELS];       ///< start frequency bin                    (strtmant)
    int cpl_end_freq;                       ///< coupling channel end frequency bin

    int cpl_on;                             ///< coupling turned on for this frame
    int cpl_enabled;                        ///< coupling enabled for all frames
    int num_cpl_subbands;                   ///< number of coupling subbands            (ncplsubnd)
    int num_cpl_bands;                      ///< number of coupling bands               (ncplbnd)
    uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS];  ///< number of coeffs in each coupling band

    int rematrixing_enabled;                ///< stereo rematrixing enabled

    /* bitrate allocation control */
    int slow_gain_code;                     ///< slow gain code                         (sgaincod)
    int slow_decay_code;                    ///< slow decay code                        (sdcycod)
    int fast_decay_code;                    ///< fast decay code                        (fdcycod)
    int db_per_bit_code;                    ///< dB/bit code                            (dbpbcod)
    int floor_code;                         ///< floor code                             (floorcod)
    AC3BitAllocParameters bit_alloc;        ///< bit allocation parameters
    int coarse_snr_offset;                  ///< coarse SNR offsets                     (csnroffst)
    int fast_gain_code[AC3_MAX_CHANNELS];   ///< fast gain codes (signal-to-mask ratio) (fgaincod)
    int fine_snr_offset[AC3_MAX_CHANNELS];  ///< fine SNR offsets                       (fsnroffst)
    int frame_bits_fixed;                   ///< number of non-coefficient bits for fixed parameters
    int frame_bits;                         ///< all frame bits except exponents and mantissas
    int exponent_bits;                      ///< number of bits used for exponents

236
    SampleType *windowed_samples;
237 238 239 240 241 242 243 244 245 246
    SampleType **planar_samples;
    uint8_t *bap_buffer;
    uint8_t *bap1_buffer;
    CoefType *mdct_coef_buffer;
    int32_t *fixed_coef_buffer;
    uint8_t *exp_buffer;
    uint8_t *grouped_exp_buffer;
    int16_t *psd_buffer;
    int16_t *band_psd_buffer;
    int16_t *mask_buffer;
247
    int16_t *qmant_buffer;
248 249 250 251
    uint8_t *cpl_coord_exp_buffer;
    uint8_t *cpl_coord_mant_buffer;

    uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
252 253
    uint8_t frame_exp_strategy[AC3_MAX_CHANNELS];           ///< frame exp strategy index
    int use_frame_exp_strategy;                             ///< indicates use of frame exp strategy
254 255 256 257
    uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< reference blocks for EXP_REUSE
    uint8_t *ref_bap     [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< bit allocation pointers (bap)
    int ref_bap_set;                                         ///< indicates if ref_bap pointers have been set

258
    /* fixed vs. float function pointers */
259 260
    void (*mdct_end)(struct AC3EncodeContext *s);
    int  (*mdct_init)(struct AC3EncodeContext *s);
261 262

    /* fixed vs. float templated function pointers */
263
    int  (*allocate_sample_buffers)(struct AC3EncodeContext *s);
264 265

    /* AC-3 vs. E-AC-3 function pointers */
266
    void (*output_frame_header)(struct AC3EncodeContext *s);
267 268
} AC3EncodeContext;

269

270
extern const uint64_t ff_ac3_channel_layouts[19];
271 272

int ff_ac3_encode_init(AVCodecContext *avctx);
273
int ff_ac3_float_encode_init(AVCodecContext *avctx);
274 275 276

int ff_ac3_encode_close(AVCodecContext *avctx);

277
int ff_ac3_validate_metadata(AC3EncodeContext *s);
278 279 280 281 282 283 284 285 286 287 288

void ff_ac3_adjust_frame_size(AC3EncodeContext *s);

void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s);

void ff_ac3_apply_rematrixing(AC3EncodeContext *s);

void ff_ac3_process_exponents(AC3EncodeContext *s);

int ff_ac3_compute_bit_allocation(AC3EncodeContext *s);

289 290
void ff_ac3_group_exponents(AC3EncodeContext *s);

291 292 293 294
void ff_ac3_quantize_mantissas(AC3EncodeContext *s);

void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame);

295 296 297

/* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */

298 299
void ff_ac3_fixed_mdct_end(AC3EncodeContext *s);
void ff_ac3_float_mdct_end(AC3EncodeContext *s);
300

301 302
int ff_ac3_fixed_mdct_init(AC3EncodeContext *s);
int ff_ac3_float_mdct_init(AC3EncodeContext *s);
303 304 305 306


/* prototypes for functions in ac3enc_template.c */

307 308 309
int ff_ac3_fixed_allocate_sample_buffers(AC3EncodeContext *s);
int ff_ac3_float_allocate_sample_buffers(AC3EncodeContext *s);

310 311 312 313
int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                              const AVFrame *frame, int *got_packet_ptr);
int ff_ac3_float_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                              const AVFrame *frame, int *got_packet_ptr);
314

315
#endif /* AVCODEC_AC3ENC_H */