ac3enc.c 77.6 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1
/*
2
 * The simplest AC-3 encoder
3
 * Copyright (c) 2000 Fabrice Bellard
4 5
 * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
 * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de>
Fabrice Bellard's avatar
Fabrice Bellard committed
6
 *
7
 * This file is part of Libav.
8
 *
9
 * Libav is free software; you can redistribute it and/or
Fabrice Bellard's avatar
Fabrice Bellard committed
10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 *
14
 * Libav is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Fabrice Bellard's avatar
Fabrice Bellard committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
18
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with Libav; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
22
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
23 24

/**
25
 * @file
26
 * The simplest AC-3 encoder.
Michael Niedermayer's avatar
Michael Niedermayer committed
27
 */
28

Fabrice Bellard's avatar
Fabrice Bellard committed
29
//#define DEBUG
30
//#define ASSERT_LEVEL 2
31

32 33
#include <stdint.h>

34
#include "libavutil/audioconvert.h"
35
#include "libavutil/avassert.h"
36
#include "libavutil/crc.h"
37
#include "libavutil/opt.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
38
#include "avcodec.h"
39
#include "put_bits.h"
40
#include "dsputil.h"
41
#include "ac3dsp.h"
42
#include "ac3.h"
43
#include "audioconvert.h"
44
#include "fft.h"
45

46

47 48 49 50 51
#ifndef CONFIG_AC3ENC_FLOAT
#define CONFIG_AC3ENC_FLOAT 0
#endif


52 53 54
/** Maximum number of exponent groups. +1 for separate DC exponent. */
#define AC3_MAX_EXP_GROUPS 85

55
#if CONFIG_AC3ENC_FLOAT
56 57 58 59
#define MAC_COEF(d,a,b) ((d)+=(a)*(b))
typedef float SampleType;
typedef float CoefType;
typedef float CoefSumType;
60
#else
61 62 63 64
#define MAC_COEF(d,a,b) MAC64(d,a,b)
typedef int16_t SampleType;
typedef int32_t CoefType;
typedef int64_t CoefSumType;
65
#endif
66

67 68 69 70
typedef struct AC3MDCTContext {
    const SampleType *window;           ///< MDCT window function
    FFTContext fft;                     ///< FFT context for MDCT calculation
} AC3MDCTContext;
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/**
 * 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;

    /* other encoding options */
    int allow_per_frame_metadata;
100
    int stereo_rematrixing;
101 102
} AC3EncOptions;

103 104 105 106
/**
 * Data for a single audio block.
 */
typedef struct AC3Block {
107
    uint8_t  **bap;                             ///< bit allocation pointers (bap)
108
    CoefType **mdct_coef;                       ///< MDCT coefficients
109
    int32_t  **fixed_coef;                      ///< fixed-point MDCT coefficients
110 111 112 113 114 115
    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
116
    uint8_t  coeff_shift[AC3_MAX_CHANNELS];     ///< fixed-point coefficient shift values
117 118
    uint8_t  new_rematrixing_strategy;          ///< send new rematrixing flags in this block
    uint8_t  rematrixing_flags[4];              ///< rematrixing flags
119
    struct AC3Block *exp_ref_block[AC3_MAX_CHANNELS]; ///< reference blocks for EXP_REUSE
120 121
} AC3Block;

122 123 124
/**
 * AC-3 encoder private context.
 */
125
typedef struct AC3EncodeContext {
126 127
    AVClass *av_class;                      ///< AVClass used for AVOption
    AC3EncOptions options;                  ///< encoding options
128
    PutBitContext pb;                       ///< bitstream writer context
129
    DSPContext dsp;
130
    AC3DSPContext ac3dsp;                   ///< AC-3 optimized functions
131
    AC3MDCTContext mdct;                    ///< MDCT context
132

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

135 136
    int bitstream_id;                       ///< bitstream id                           (bsid)
    int bitstream_mode;                     ///< bitstream mode                         (bsmod)
137

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

141
    int frame_size_min;                     ///< minimum frame size in case rounding is necessary
142
    int frame_size;                         ///< current frame size in bytes
143
    int frame_size_code;                    ///< frame size code                        (frmsizecod)
144
    uint16_t crc_inv[2];
145 146
    int bits_written;                       ///< bit count    (used to avg. bitrate)
    int samples_written;                    ///< sample count (used to avg. bitrate)
147

148 149 150 151
    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
152 153
    int has_center;                         ///< indicates if there is a center channel
    int has_surround;                       ///< indicates if there are one or more surround channels
154 155
    int channel_mode;                       ///< channel mode                           (acmod)
    const uint8_t *channel_map;             ///< channel map used to reorder channels
156

157 158 159 160 161 162 163
    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

164
    int cutoff;                             ///< user-specified cutoff frequency, in Hz
165
    int bandwidth_code[AC3_MAX_CHANNELS];   ///< bandwidth code (0 to 60)               (chbwcod)
166
    int nb_coefs[AC3_MAX_CHANNELS];
167

168
    int rematrixing_enabled;                ///< stereo rematrixing enabled
169
    int num_rematrixing_bands;              ///< number of rematrixing bands
170

171
    /* bitrate allocation control */
172 173 174 175 176 177 178 179 180
    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)
181
    int frame_bits_fixed;                   ///< number of non-coefficient bits for fixed parameters
182 183
    int frame_bits;                         ///< all frame bits except exponents and mantissas
    int exponent_bits;                      ///< number of bits used for exponents
184

185
    SampleType **planar_samples;
186 187
    uint8_t *bap_buffer;
    uint8_t *bap1_buffer;
188
    CoefType *mdct_coef_buffer;
189
    int32_t *fixed_coef_buffer;
190 191 192 193 194 195
    uint8_t *exp_buffer;
    uint8_t *grouped_exp_buffer;
    int16_t *psd_buffer;
    int16_t *band_psd_buffer;
    int16_t *mask_buffer;
    uint16_t *qmant_buffer;
196

197 198
    uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies

Vitor Sessak's avatar
Vitor Sessak committed
199
    DECLARE_ALIGNED(32, SampleType, windowed_samples)[AC3_WINDOW_SIZE];
200
} AC3EncodeContext;
Fabrice Bellard's avatar
Fabrice Bellard committed
201

202 203 204 205
typedef struct AC3Mant {
    uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; ///< mantissa pointers for bap=1,2,4
    int mant1_cnt, mant2_cnt, mant4_cnt;    ///< mantissa counts for bap=1,2,4
} AC3Mant;
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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
#define CMIXLEV_NUM_OPTIONS 3
static const float cmixlev_options[CMIXLEV_NUM_OPTIONS] = {
    LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB
};

#define SURMIXLEV_NUM_OPTIONS 3
static const float surmixlev_options[SURMIXLEV_NUM_OPTIONS] = {
    LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO
};

#define EXTMIXLEV_NUM_OPTIONS 8
static const float extmixlev_options[EXTMIXLEV_NUM_OPTIONS] = {
    LEVEL_PLUS_3DB,  LEVEL_PLUS_1POINT5DB,  LEVEL_ONE,       LEVEL_MINUS_4POINT5DB,
    LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB, LEVEL_ZERO
};


#define OFFSET(param) offsetof(AC3EncodeContext, options.param)
#define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)

static const AVOption options[] = {
/* Metadata Options */
{"per_frame_metadata", "Allow Changing Metadata Per-Frame", OFFSET(allow_per_frame_metadata), FF_OPT_TYPE_INT, 0, 0, 1, AC3ENC_PARAM},
/* downmix levels */
{"center_mixlev", "Center Mix Level", OFFSET(center_mix_level), FF_OPT_TYPE_FLOAT, LEVEL_MINUS_4POINT5DB, 0.0, 1.0, AC3ENC_PARAM},
{"surround_mixlev", "Surround Mix Level", OFFSET(surround_mix_level), FF_OPT_TYPE_FLOAT, LEVEL_MINUS_6DB, 0.0, 1.0, AC3ENC_PARAM},
/* audio production information */
{"mixing_level", "Mixing Level", OFFSET(mixing_level), FF_OPT_TYPE_INT, -1, -1, 111, AC3ENC_PARAM},
{"room_type", "Room Type", OFFSET(room_type), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "room_type"},
    {"notindicated", "Not Indicated (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
    {"large",        "Large Room",              0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
    {"small",        "Small Room",              0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
/* other metadata options */
{"copyright", "Copyright Bit", OFFSET(copyright), FF_OPT_TYPE_INT, 0, 0, 1, AC3ENC_PARAM},
{"dialnorm", "Dialogue Level (dB)", OFFSET(dialogue_level), FF_OPT_TYPE_INT, -31, -31, -1, AC3ENC_PARAM},
{"dsur_mode", "Dolby Surround Mode", OFFSET(dolby_surround_mode), FF_OPT_TYPE_INT, 0, 0, 2, AC3ENC_PARAM, "dsur_mode"},
    {"notindicated", "Not Indicated (default)",    0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
    {"on",           "Dolby Surround Encoded",     0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
    {"off",          "Not Dolby Surround Encoded", 0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
{"original", "Original Bit Stream", OFFSET(original), FF_OPT_TYPE_INT, 1, 0, 1, AC3ENC_PARAM},
/* extended bitstream information */
{"dmix_mode", "Preferred Stereo Downmix Mode", OFFSET(preferred_stereo_downmix), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "dmix_mode"},
    {"notindicated", "Not Indicated (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
    {"ltrt", "Lt/Rt Downmix Preferred",         0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
    {"loro", "Lo/Ro Downmix Preferred",         0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
{"ltrt_cmixlev", "Lt/Rt Center Mix Level", OFFSET(ltrt_center_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
{"ltrt_surmixlev", "Lt/Rt Surround Mix Level", OFFSET(ltrt_surround_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
{"loro_cmixlev", "Lo/Ro Center Mix Level", OFFSET(loro_center_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
{"loro_surmixlev", "Lo/Ro Surround Mix Level", OFFSET(loro_surround_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
{"dsurex_mode", "Dolby Surround EX Mode", OFFSET(dolby_surround_ex_mode), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "dsurex_mode"},
    {"notindicated", "Not Indicated (default)",       0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
    {"on",           "Dolby Surround EX Encoded",     0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
    {"off",          "Not Dolby Surround EX Encoded", 0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
{"dheadphone_mode", "Dolby Headphone Mode", OFFSET(dolby_headphone_mode), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "dheadphone_mode"},
    {"notindicated", "Not Indicated (default)",     0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
    {"on",           "Dolby Headphone Encoded",     0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
    {"off",          "Not Dolby Headphone Encoded", 0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
{"ad_conv_type", "A/D Converter Type", OFFSET(ad_converter_type), FF_OPT_TYPE_INT, -1, -1, 1, AC3ENC_PARAM, "ad_conv_type"},
    {"standard", "Standard (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
    {"hdcd",     "HDCD",               0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
267 268
/* Other Encoding Options */
{"stereo_rematrixing", "Stereo Rematrixing", OFFSET(stereo_rematrixing), FF_OPT_TYPE_INT, 1, 0, 1, AC3ENC_PARAM},
269 270 271 272 273 274 275 276 277 278 279 280
{NULL}
};

#if CONFIG_AC3ENC_FLOAT
static AVClass ac3enc_class = { "AC-3 Encoder", av_default_item_name,
                                options, LIBAVUTIL_VERSION_INT };
#else
static AVClass ac3enc_class = { "Fixed-Point AC-3 Encoder", av_default_item_name,
                                options, LIBAVUTIL_VERSION_INT };
#endif


281
/* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
282 283 284 285 286 287

static av_cold void mdct_end(AC3MDCTContext *mdct);

static av_cold int mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
                             int nbits);

288
static void apply_window(DSPContext *dsp, SampleType *output, const SampleType *input,
289
                         const SampleType *window, unsigned int len);
290 291 292

static int normalize_samples(AC3EncodeContext *s);

293 294
static void scale_coefficients(AC3EncodeContext *s);

295

296 297 298 299
/**
 * LUT for number of exponent groups.
 * exponent_group_tab[exponent strategy-1][number of coefficients]
 */
300
static uint8_t exponent_group_tab[3][256];
301

302

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
/**
 * List of supported channel layouts.
 */
static const int64_t ac3_channel_layouts[] = {
     AV_CH_LAYOUT_MONO,
     AV_CH_LAYOUT_STEREO,
     AV_CH_LAYOUT_2_1,
     AV_CH_LAYOUT_SURROUND,
     AV_CH_LAYOUT_2_2,
     AV_CH_LAYOUT_QUAD,
     AV_CH_LAYOUT_4POINT0,
     AV_CH_LAYOUT_5POINT0,
     AV_CH_LAYOUT_5POINT0_BACK,
    (AV_CH_LAYOUT_MONO     | AV_CH_LOW_FREQUENCY),
    (AV_CH_LAYOUT_STEREO   | AV_CH_LOW_FREQUENCY),
    (AV_CH_LAYOUT_2_1      | AV_CH_LOW_FREQUENCY),
    (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY),
    (AV_CH_LAYOUT_2_2      | AV_CH_LOW_FREQUENCY),
    (AV_CH_LAYOUT_QUAD     | AV_CH_LOW_FREQUENCY),
    (AV_CH_LAYOUT_4POINT0  | AV_CH_LOW_FREQUENCY),
     AV_CH_LAYOUT_5POINT1,
     AV_CH_LAYOUT_5POINT1_BACK,
     0
};


329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
/**
 * LUT to select the bandwidth code based on the bit rate, sample rate, and
 * number of full-bandwidth channels.
 * bandwidth_tab[fbw_channels-1][sample rate code][bit rate code]
 */
static const uint8_t ac3_bandwidth_tab[5][3][19] = {
//      32  40  48  56  64  80  96 112 128 160 192 224 256 320 384 448 512 576 640

    { {  0,  0,  0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
      {  0,  0,  0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
      {  0,  0,  0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },

    { {  0,  0,  0,  0,  0,  0,  0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
      {  0,  0,  0,  0,  0,  0,  4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
      {  0,  0,  0,  0,  0,  0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },

    { {  0,  0,  0,  0,  0,  0,  0,  0,  0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
      {  0,  0,  0,  0,  0,  0,  0,  0,  4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
      {  0,  0,  0,  0,  0,  0,  0,  0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },

    { {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
      {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
      {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },

    { {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  8, 20, 32, 40, 48, 48, 48, 48 },
      {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 12, 24, 36, 44, 56, 56, 56, 56 },
      {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 28, 44, 60, 60, 60, 60, 60, 60 } }
};


359 360 361 362 363 364 365 366 367 368
/**
 * Adjust the frame size to make the average bit rate match the target bit rate.
 * This is only needed for 11025, 22050, and 44100 sample rates.
 */
static void adjust_frame_size(AC3EncodeContext *s)
{
    while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
        s->bits_written    -= s->bit_rate;
        s->samples_written -= s->sample_rate;
    }
369 370
    s->frame_size = s->frame_size_min +
                    2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
371 372 373 374 375
    s->bits_written    += s->frame_size * 8;
    s->samples_written += AC3_FRAME_SIZE;
}


376 377
/**
 * Deinterleave input samples.
378
 * Channels are reordered from Libav's default order to AC-3 order.
379 380
 */
static void deinterleave_input_samples(AC3EncodeContext *s,
381
                                       const SampleType *samples)
382 383 384 385 386
{
    int ch, i;

    /* deinterleave and remap input samples */
    for (ch = 0; ch < s->channels; ch++) {
387
        const SampleType *sptr;
388 389 390
        int sinc;

        /* copy last 256 samples of previous frame to the start of the current frame */
391
        memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_FRAME_SIZE],
392
               AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
393 394 395 396 397

        /* deinterleave */
        sinc = s->channels;
        sptr = samples + s->channel_map[ch];
        for (i = AC3_BLOCK_SIZE; i < AC3_FRAME_SIZE+AC3_BLOCK_SIZE; i++) {
398
            s->planar_samples[ch][i] = *sptr;
399 400 401 402 403 404
            sptr += sinc;
        }
    }
}


405 406 407 408 409
/**
 * Apply the MDCT to input samples to generate frequency coefficients.
 * This applies the KBD window and normalizes the input to reduce precision
 * loss due to fixed-point calculations.
 */
410
static void apply_mdct(AC3EncodeContext *s)
411 412 413 414 415
{
    int blk, ch;

    for (ch = 0; ch < s->channels; ch++) {
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
416
            AC3Block *block = &s->blocks[blk];
417
            const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
418

419
            apply_window(&s->dsp, s->windowed_samples, input_samples, s->mdct.window, AC3_WINDOW_SIZE);
420

421
            block->coeff_shift[ch] = normalize_samples(s);
422

423 424
            s->mdct.fft.mdct_calcw(&s->mdct.fft, block->mdct_coef[ch],
                                   s->windowed_samples);
425 426 427 428 429
        }
    }
}


430 431 432 433 434 435 436 437 438
/**
 * Determine rematrixing flags for each block and band.
 */
static void compute_rematrixing_strategy(AC3EncodeContext *s)
{
    int nb_coefs;
    int blk, bnd, i;
    AC3Block *block, *block0;

439
    if (s->channel_mode != AC3_CHMODE_STEREO)
440 441
        return;

442 443
    s->num_rematrixing_bands = 4;

444 445 446 447
    nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);

    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
        block = &s->blocks[blk];
448
        block->new_rematrixing_strategy = !blk;
449 450
        if (!s->rematrixing_enabled)
            continue;
451
        for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
452 453 454 455 456 457 458 459 460
            /* calculate calculate sum of squared coeffs for one band in one block */
            int start = ff_ac3_rematrix_band_tab[bnd];
            int end   = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
            CoefSumType sum[4] = {0,};
            for (i = start; i < end; i++) {
                CoefType lt = block->mdct_coef[0][i];
                CoefType rt = block->mdct_coef[1][i];
                CoefType md = lt + rt;
                CoefType sd = lt - rt;
461 462 463 464
                MAC_COEF(sum[0], lt, lt);
                MAC_COEF(sum[1], rt, rt);
                MAC_COEF(sum[2], md, md);
                MAC_COEF(sum[3], sd, sd);
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
            }

            /* compare sums to determine if rematrixing will be used for this band */
            if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
                block->rematrixing_flags[bnd] = 1;
            else
                block->rematrixing_flags[bnd] = 0;

            /* determine if new rematrixing flags will be sent */
            if (blk &&
                block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
                block->new_rematrixing_strategy = 1;
            }
        }
        block0 = block;
    }
}


/**
 * Apply stereo rematrixing to coefficients based on rematrixing flags.
 */
static void apply_rematrixing(AC3EncodeContext *s)
{
    int nb_coefs;
    int blk, bnd, i;
    int start, end;
    uint8_t *flags;

494
    if (!s->rematrixing_enabled)
495 496 497 498 499 500 501 502
        return;

    nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);

    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
        AC3Block *block = &s->blocks[blk];
        if (block->new_rematrixing_strategy)
            flags = block->rematrixing_flags;
503
        for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
            if (flags[bnd]) {
                start = ff_ac3_rematrix_band_tab[bnd];
                end   = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
                for (i = start; i < end; i++) {
                    int32_t lt = block->fixed_coef[0][i];
                    int32_t rt = block->fixed_coef[1][i];
                    block->fixed_coef[0][i] = (lt + rt) >> 1;
                    block->fixed_coef[1][i] = (lt - rt) >> 1;
                }
            }
        }
    }
}


519 520 521 522 523
/**
 * Initialize exponent tables.
 */
static av_cold void exponent_init(AC3EncodeContext *s)
{
524 525 526 527 528 529 530
    int expstr, i, grpsize;

    for (expstr = EXP_D15-1; expstr <= EXP_D45-1; expstr++) {
        grpsize = 3 << expstr;
        for (i = 73; i < 256; i++) {
            exponent_group_tab[expstr][i] = (i + grpsize - 4) / grpsize;
        }
531
    }
532 533
    /* LFE */
    exponent_group_tab[0][7] = 2;
534 535 536
}


537 538 539 540 541
/**
 * Extract exponents from the MDCT coefficients.
 * This takes into account the normalization that was done to the input samples
 * by adjusting the exponents by the exponent shift values.
 */
542
static void extract_exponents(AC3EncodeContext *s)
543
{
544
    int blk, ch;
545 546 547

    for (ch = 0; ch < s->channels; ch++) {
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
548
            AC3Block *block = &s->blocks[blk];
549 550
            s->ac3dsp.extract_exponents(block->exp[ch], block->fixed_coef[ch],
                                        AC3_MAX_COEFS);
551 552 553 554 555
        }
    }
}


556 557 558 559
/**
 * Exponent Difference Threshold.
 * New exponents are sent if their SAD exceed this number.
 */
560
#define EXP_DIFF_THRESHOLD 500
561

562

563 564 565
/**
 * Calculate exponent strategies for all blocks in a single channel.
 */
566
static void compute_exp_strategy_ch(AC3EncodeContext *s, uint8_t *exp_strategy,
567
                                    uint8_t *exp)
Fabrice Bellard's avatar
Fabrice Bellard committed
568
{
569
    int blk, blk1;
Fabrice Bellard's avatar
Fabrice Bellard committed
570
    int exp_diff;
571

Fabrice Bellard's avatar
Fabrice Bellard committed
572 573
    /* estimate if the exponent variation & decide if they should be
       reused in the next frame */
574
    exp_strategy[0] = EXP_NEW;
575
    exp += AC3_MAX_COEFS;
576
    for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
577
        exp_diff = s->dsp.sad[0](NULL, exp, exp - AC3_MAX_COEFS, 16, 16);
Fabrice Bellard's avatar
Fabrice Bellard committed
578
        if (exp_diff > EXP_DIFF_THRESHOLD)
579
            exp_strategy[blk] = EXP_NEW;
Fabrice Bellard's avatar
Fabrice Bellard committed
580
        else
581
            exp_strategy[blk] = EXP_REUSE;
582
        exp += AC3_MAX_COEFS;
Fabrice Bellard's avatar
Fabrice Bellard committed
583
    }
584

Fabrice Bellard's avatar
Fabrice Bellard committed
585 586
    /* now select the encoding strategy type : if exponents are often
       recoded, we use a coarse encoding */
587 588 589
    blk = 0;
    while (blk < AC3_MAX_BLOCKS) {
        blk1 = blk + 1;
590
        while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE)
591 592
            blk1++;
        switch (blk1 - blk) {
593
        case 1:  exp_strategy[blk] = EXP_D45; break;
Fabrice Bellard's avatar
Fabrice Bellard committed
594
        case 2:
595 596
        case 3:  exp_strategy[blk] = EXP_D25; break;
        default: exp_strategy[blk] = EXP_D15; break;
Fabrice Bellard's avatar
Fabrice Bellard committed
597
        }
598
        blk = blk1;
Fabrice Bellard's avatar
Fabrice Bellard committed
599 600 601
    }
}

602

603 604
/**
 * Calculate exponent strategies for all channels.
605
 * Array arrangement is reversed to simplify the per-channel calculation.
606
 */
607
static void compute_exp_strategy(AC3EncodeContext *s)
608
{
609
    int ch, blk;
610

611
    for (ch = 0; ch < s->fbw_channels; ch++) {
612
        compute_exp_strategy_ch(s, s->exp_strategy[ch], s->blocks[0].exp[ch]);
613 614 615
    }
    if (s->lfe_on) {
        ch = s->lfe_channel;
616
        s->exp_strategy[ch][0] = EXP_D15;
617
        for (blk = 1; blk < AC3_MAX_BLOCKS; blk++)
618
            s->exp_strategy[ch][blk] = EXP_REUSE;
619 620 621 622
    }
}


623 624 625
/**
 * Update the exponents so that they are the ones the decoder will decode.
 */
626
static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
Fabrice Bellard's avatar
Fabrice Bellard committed
627
{
628
    int nb_groups, i, k;
Fabrice Bellard's avatar
Fabrice Bellard committed
629

630
    nb_groups = exponent_group_tab[exp_strategy-1][nb_exps] * 3;
Fabrice Bellard's avatar
Fabrice Bellard committed
631 632

    /* for each group, compute the minimum exponent */
633 634 635 636 637 638 639 640
    switch(exp_strategy) {
    case EXP_D25:
        for (i = 1, k = 1; i <= nb_groups; i++) {
            uint8_t exp_min = exp[k];
            if (exp[k+1] < exp_min)
                exp_min = exp[k+1];
            exp[i] = exp_min;
            k += 2;
Fabrice Bellard's avatar
Fabrice Bellard committed
641
        }
642 643 644 645 646 647 648 649 650 651 652 653 654 655
        break;
    case EXP_D45:
        for (i = 1, k = 1; i <= nb_groups; i++) {
            uint8_t exp_min = exp[k];
            if (exp[k+1] < exp_min)
                exp_min = exp[k+1];
            if (exp[k+2] < exp_min)
                exp_min = exp[k+2];
            if (exp[k+3] < exp_min)
                exp_min = exp[k+3];
            exp[i] = exp_min;
            k += 4;
        }
        break;
656
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
657 658

    /* constraint for DC exponent */
659 660
    if (exp[0] > 15)
        exp[0] = 15;
Fabrice Bellard's avatar
Fabrice Bellard committed
661

662 663 664
    /* decrease the delta between each groups to within 2 so that they can be
       differentially encoded */
    for (i = 1; i <= nb_groups; i++)
665
        exp[i] = FFMIN(exp[i], exp[i-1] + 2);
666 667
    i--;
    while (--i >= 0)
668
        exp[i] = FFMIN(exp[i], exp[i+1] + 2);
669

Fabrice Bellard's avatar
Fabrice Bellard committed
670
    /* now we have the exponent values the decoder will see */
671 672 673 674 675 676 677 678 679 680 681 682 683 684
    switch (exp_strategy) {
    case EXP_D25:
        for (i = nb_groups, k = nb_groups * 2; i > 0; i--) {
            uint8_t exp1 = exp[i];
            exp[k--] = exp1;
            exp[k--] = exp1;
        }
        break;
    case EXP_D45:
        for (i = nb_groups, k = nb_groups * 4; i > 0; i--) {
            exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i];
            k -= 4;
        }
        break;
Fabrice Bellard's avatar
Fabrice Bellard committed
685 686 687
    }
}

688

689 690 691 692 693 694
/**
 * Encode exponents from original extracted form to what the decoder will see.
 * This copies and groups exponents based on exponent strategy and reduces
 * deltas between adjacent exponent groups so that they can be differentially
 * encoded.
 */
695
static void encode_exponents(AC3EncodeContext *s)
696
{
697
    int blk, blk1, ch;
698
    uint8_t *exp, *exp_strategy;
699
    int nb_coefs, num_reuse_blocks;
700 701

    for (ch = 0; ch < s->channels; ch++) {
702 703 704 705
        exp          = s->blocks[0].exp[ch];
        exp_strategy = s->exp_strategy[ch];
        nb_coefs     = s->nb_coefs[ch];

706 707 708
        blk = 0;
        while (blk < AC3_MAX_BLOCKS) {
            blk1 = blk + 1;
709

710 711 712 713 714
            /* count the number of EXP_REUSE blocks after the current block
               and set exponent reference block pointers */
            s->blocks[blk].exp_ref_block[ch] = &s->blocks[blk];
            while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE) {
                s->blocks[blk1].exp_ref_block[ch] = &s->blocks[blk];
715
                blk1++;
716
            }
717 718 719
            num_reuse_blocks = blk1 - blk - 1;

            /* for the EXP_REUSE case we select the min of the exponents */
720
            s->ac3dsp.ac3_exponent_min(exp, num_reuse_blocks, nb_coefs);
721

722 723
            encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk]);

724
            exp += AC3_MAX_COEFS * (num_reuse_blocks + 1);
725 726 727
            blk = blk1;
        }
    }
728 729 730 731 732 733 734 735
}


/**
 * Group exponents.
 * 3 delta-encoded exponents are in each 7-bit group. The number of groups
 * varies depending on exponent strategy and bandwidth.
 */
736
static void group_exponents(AC3EncodeContext *s)
737 738
{
    int blk, ch, i;
739
    int group_size, nb_groups, bit_count;
740 741 742 743 744 745
    uint8_t *p;
    int delta0, delta1, delta2;
    int exp0, exp1;

    bit_count = 0;
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
746
        AC3Block *block = &s->blocks[blk];
747
        for (ch = 0; ch < s->channels; ch++) {
748 749
            int exp_strategy = s->exp_strategy[ch][blk];
            if (exp_strategy == EXP_REUSE)
750
                continue;
751 752
            group_size = exp_strategy + (exp_strategy == EXP_D45);
            nb_groups = exponent_group_tab[exp_strategy-1][s->nb_coefs[ch]];
753
            bit_count += 4 + (nb_groups * 7);
754
            p = block->exp[ch];
755 756 757

            /* DC exponent */
            exp1 = *p++;
758
            block->grouped_exp[ch][0] = exp1;
759 760

            /* remaining exponents are delta encoded */
761
            for (i = 1; i <= nb_groups; i++) {
762 763 764 765 766
                /* merge three delta in one code */
                exp0   = exp1;
                exp1   = p[0];
                p     += group_size;
                delta0 = exp1 - exp0 + 2;
Justin Ruggles's avatar
Justin Ruggles committed
767
                av_assert2(delta0 >= 0 && delta0 <= 4);
768 769 770 771 772

                exp0   = exp1;
                exp1   = p[0];
                p     += group_size;
                delta1 = exp1 - exp0 + 2;
Justin Ruggles's avatar
Justin Ruggles committed
773
                av_assert2(delta1 >= 0 && delta1 <= 4);
774 775 776 777 778

                exp0   = exp1;
                exp1   = p[0];
                p     += group_size;
                delta2 = exp1 - exp0 + 2;
Justin Ruggles's avatar
Justin Ruggles committed
779
                av_assert2(delta2 >= 0 && delta2 <= 4);
780

781
                block->grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
782 783 784
            }
        }
    }
785

786
    s->exponent_bits = bit_count;
787 788 789 790 791 792 793 794
}


/**
 * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
 * Extract exponents from MDCT coefficients, calculate exponent strategies,
 * and encode final exponents.
 */
795
static void process_exponents(AC3EncodeContext *s)
796
{
797
    extract_exponents(s);
798

799
    compute_exp_strategy(s);
800

801
    encode_exponents(s);
802

803
    group_exponents(s);
804 805

    emms_c();
806 807 808
}


809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
/**
 * Count frame bits that are based solely on fixed parameters.
 * This only has to be run once when the encoder is initialized.
 */
static void count_frame_bits_fixed(AC3EncodeContext *s)
{
    static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
    int blk;
    int frame_bits;

    /* assumptions:
     *   no dynamic range codes
     *   no channel coupling
     *   bit allocation parameters do not change between blocks
     *   SNR offsets do not change between blocks
     *   no delta bit allocation
     *   no skipped data
     *   no auxilliary data
     */

    /* header size */
    frame_bits = 65;
    frame_bits += frame_bits_inc[s->channel_mode];

    /* audio blocks */
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
        frame_bits += s->fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
        if (s->channel_mode == AC3_CHMODE_STEREO) {
            frame_bits++; /* rematstr */
        }
        frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
        if (s->lfe_on)
            frame_bits++; /* lfeexpstr */
        frame_bits++; /* baie */
        frame_bits++; /* snr */
        frame_bits += 2; /* delta / skip */
    }
    frame_bits++; /* cplinu for block 0 */
    /* bit alloc info */
    /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
    /* csnroffset[6] */
    /* (fsnoffset[4] + fgaincod[4]) * c */
    frame_bits += 2*4 + 3 + 6 + s->channels * (4 + 3);

    /* auxdatae, crcrsv */
    frame_bits += 2;

    /* CRC */
    frame_bits += 16;

    s->frame_bits_fixed = frame_bits;
}


863 864 865 866 867 868 869 870 871 872 873 874
/**
 * Initialize bit allocation.
 * Set default parameter codes and calculate parameter values.
 */
static void bit_alloc_init(AC3EncodeContext *s)
{
    int ch;

    /* init default parameters */
    s->slow_decay_code = 2;
    s->fast_decay_code = 1;
    s->slow_gain_code  = 1;
875
    s->db_per_bit_code = 3;
876
    s->floor_code      = 7;
877 878 879 880 881 882 883 884 885 886 887 888 889 890
    for (ch = 0; ch < s->channels; ch++)
        s->fast_gain_code[ch] = 4;

    /* initial snr offset */
    s->coarse_snr_offset = 40;

    /* compute real values */
    /* currently none of these values change during encoding, so we can just
       set them once at initialization */
    s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
    s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;
    s->bit_alloc.slow_gain  = ff_ac3_slow_gain_tab[s->slow_gain_code];
    s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
    s->bit_alloc.floor      = ff_ac3_floor_tab[s->floor_code];
891 892

    count_frame_bits_fixed(s);
893 894 895
}


896 897
/**
 * Count the bits used to encode the frame, minus exponents and mantissas.
898 899
 * Bits based on fixed parameters have already been counted, so now we just
 * have to add the bits based on parameters that change during encoding.
900
 */
901
static void count_frame_bits(AC3EncodeContext *s)
902
{
903
    AC3EncOptions *opt = &s->options;
904
    int blk, ch;
905
    int frame_bits = 0;
906

907 908 909 910 911 912 913 914 915
    if (opt->audio_production_info)
        frame_bits += 7;
    if (s->bitstream_id == 6) {
        if (opt->extended_bsi_1)
            frame_bits += 14;
        if (opt->extended_bsi_2)
            frame_bits += 14;
    }

916
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
917 918 919
        /* stereo rematrixing */
        if (s->channel_mode == AC3_CHMODE_STEREO &&
            s->blocks[blk].new_rematrixing_strategy) {
920
            frame_bits += s->num_rematrixing_bands;