ac3enc.c 77.5 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
#include "libavutil/audioconvert.h"
33
#include "libavutil/avassert.h"
34
#include "libavutil/crc.h"
35
#include "libavutil/opt.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
36
#include "avcodec.h"
37
#include "put_bits.h"
38
#include "dsputil.h"
39
#include "ac3dsp.h"
40
#include "ac3.h"
41
#include "audioconvert.h"
42

43

44 45 46 47 48
#ifndef CONFIG_AC3ENC_FLOAT
#define CONFIG_AC3ENC_FLOAT 0
#endif


49 50 51
/** Maximum number of exponent groups. +1 for separate DC exponent. */
#define AC3_MAX_EXP_GROUPS 85

52 53 54 55 56 57
/* stereo rematrixing algorithms */
#define AC3_REMATRIXING_IS_STATIC 0x1
#define AC3_REMATRIXING_SUMS    0
#define AC3_REMATRIXING_NONE    1
#define AC3_REMATRIXING_ALWAYS  3

58
/** Scale a float value by 2^bits and convert to an integer. */
Justin Ruggles's avatar
Justin Ruggles committed
59 60
#define SCALE_FLOAT(a, bits) lrintf((a) * (float)(1 << (bits)))

61

62 63 64
#if CONFIG_AC3ENC_FLOAT
#include "ac3enc_float.h"
#else
65
#include "ac3enc_fixed.h"
66
#endif
67

68

69 70 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
/**
 * 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;
} AC3EncOptions;

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

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

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

131 132
    int bitstream_id;                       ///< bitstream id                           (bsid)
    int bitstream_mode;                     ///< bitstream mode                         (bsmod)
133

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

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

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

153 154 155 156 157 158 159
    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

160
    int cutoff;                             ///< user-specified cutoff frequency, in Hz
161
    int bandwidth_code[AC3_MAX_CHANNELS];   ///< bandwidth code (0 to 60)               (chbwcod)
162
    int nb_coefs[AC3_MAX_CHANNELS];
163

164
    int rematrixing;                        ///< determines how rematrixing strategy is calculated
165
    int num_rematrixing_bands;              ///< number of rematrixing bands
166

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

181
    SampleType **planar_samples;
182 183
    uint8_t *bap_buffer;
    uint8_t *bap1_buffer;
184
    CoefType *mdct_coef_buffer;
185
    int32_t *fixed_coef_buffer;
186 187 188 189 190 191
    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;
192

193 194
    uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies

195
    DECLARE_ALIGNED(16, SampleType, windowed_samples)[AC3_WINDOW_SIZE];
196
} AC3EncodeContext;
Fabrice Bellard's avatar
Fabrice Bellard committed
197

198 199 200 201
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;
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 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 267 268 269 270 271 272 273 274
#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"},
{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


275
/* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
276 277 278 279 280 281 282 283

static av_cold void mdct_end(AC3MDCTContext *mdct);

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

static void mdct512(AC3MDCTContext *mdct, CoefType *out, SampleType *in);

284
static void apply_window(DSPContext *dsp, SampleType *output, const SampleType *input,
285
                         const SampleType *window, unsigned int len);
286 287 288

static int normalize_samples(AC3EncodeContext *s);

289 290
static void scale_coefficients(AC3EncodeContext *s);

291

292 293 294 295
/**
 * LUT for number of exponent groups.
 * exponent_group_tab[exponent strategy-1][number of coefficients]
 */
296
static uint8_t exponent_group_tab[3][256];
297

298

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
/**
 * 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
};


325 326 327 328 329 330 331 332 333 334
/**
 * 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;
    }
335 336
    s->frame_size = s->frame_size_min +
                    2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
337 338 339 340 341
    s->bits_written    += s->frame_size * 8;
    s->samples_written += AC3_FRAME_SIZE;
}


342 343
/**
 * Deinterleave input samples.
344
 * Channels are reordered from Libav's default order to AC-3 order.
345 346
 */
static void deinterleave_input_samples(AC3EncodeContext *s,
347
                                       const SampleType *samples)
348 349 350 351 352
{
    int ch, i;

    /* deinterleave and remap input samples */
    for (ch = 0; ch < s->channels; ch++) {
353
        const SampleType *sptr;
354 355 356
        int sinc;

        /* copy last 256 samples of previous frame to the start of the current frame */
357
        memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_FRAME_SIZE],
358
               AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
359 360 361 362 363

        /* deinterleave */
        sinc = s->channels;
        sptr = samples + s->channel_map[ch];
        for (i = AC3_BLOCK_SIZE; i < AC3_FRAME_SIZE+AC3_BLOCK_SIZE; i++) {
364
            s->planar_samples[ch][i] = *sptr;
365 366 367 368 369 370
            sptr += sinc;
        }
    }
}


371 372 373 374 375
/**
 * 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.
 */
376
static void apply_mdct(AC3EncodeContext *s)
377 378 379 380 381
{
    int blk, ch;

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

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

387
            block->coeff_shift[ch] = normalize_samples(s);
388

389
            mdct512(&s->mdct, block->mdct_coef[ch], s->windowed_samples);
390 391 392 393 394
        }
    }
}


395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
/**
 * Initialize stereo rematrixing.
 * If the strategy does not change for each frame, set the rematrixing flags.
 */
static void rematrixing_init(AC3EncodeContext *s)
{
    if (s->channel_mode == AC3_CHMODE_STEREO)
        s->rematrixing = AC3_REMATRIXING_SUMS;
    else
        s->rematrixing = AC3_REMATRIXING_NONE;
    /* NOTE: AC3_REMATRIXING_ALWAYS might be used in
             the future in conjunction with channel coupling. */

    if (s->rematrixing & AC3_REMATRIXING_IS_STATIC) {
        int flag = (s->rematrixing == AC3_REMATRIXING_ALWAYS);
        s->blocks[0].new_rematrixing_strategy = 1;
        memset(s->blocks[0].rematrixing_flags, flag,
               sizeof(s->blocks[0].rematrixing_flags));
    }
}


/**
 * 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;

426 427
    s->num_rematrixing_bands = 4;

428 429 430 431 432 433 434
    if (s->rematrixing & AC3_REMATRIXING_IS_STATIC)
        return;

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

    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
        block = &s->blocks[blk];
435
        block->new_rematrixing_strategy = !blk;
436
        for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
437 438 439 440 441 442 443 444 445
            /* 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;
446 447 448 449
                MAC_COEF(sum[0], lt, lt);
                MAC_COEF(sum[1], rt, rt);
                MAC_COEF(sum[2], md, md);
                MAC_COEF(sum[3], sd, sd);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
            }

            /* 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;

    if (s->rematrixing == AC3_REMATRIXING_NONE)
        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;
488
        for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
            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;
                }
            }
        }
    }
}


504 505 506 507 508 509 510 511 512 513 514
/**
 * Initialize exponent tables.
 */
static av_cold void exponent_init(AC3EncodeContext *s)
{
    int i;
    for (i = 73; i < 256; i++) {
        exponent_group_tab[0][i] = (i - 1) /  3;
        exponent_group_tab[1][i] = (i + 2) /  6;
        exponent_group_tab[2][i] = (i + 8) / 12;
    }
515 516
    /* LFE */
    exponent_group_tab[0][7] = 2;
517 518 519
}


520 521 522 523 524
/**
 * 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.
 */
525
static void extract_exponents(AC3EncodeContext *s)
526 527 528 529 530
{
    int blk, ch, i;

    for (ch = 0; ch < s->channels; ch++) {
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
531
            AC3Block *block = &s->blocks[blk];
532
            uint8_t *exp   = block->exp[ch];
533
            int32_t *coef = block->fixed_coef[ch];
534 535
            for (i = 0; i < AC3_MAX_COEFS; i++) {
                int e;
536
                int v = abs(coef[i]);
537 538 539
                if (v == 0)
                    e = 24;
                else {
540
                    e = 23 - av_log2(v);
541 542
                    if (e >= 24) {
                        e = 24;
543
                        coef[i] = 0;
544
                    }
Justin Ruggles's avatar
Justin Ruggles committed
545
                    av_assert2(e >= 0);
546
                }
547
                exp[i] = e;
548 549 550 551 552 553
            }
        }
    }
}


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

560

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

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

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

600

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

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


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

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

    /* for each group, compute the minimum exponent */
631 632 633 634 635 636 637 638
    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
639
        }
640 641 642 643 644 645 646 647 648 649 650 651 652 653
        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;
654
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
655 656

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

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

Fabrice Bellard's avatar
Fabrice Bellard committed
668
    /* now we have the exponent values the decoder will see */
669 670 671 672 673 674 675 676 677 678 679 680 681 682
    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
683 684 685
    }
}

686

687 688 689 690 691 692
/**
 * 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.
 */
693
static void encode_exponents(AC3EncodeContext *s)
694
{
695
    int blk, blk1, ch;
696
    uint8_t *exp, *exp_strategy;
697
    int nb_coefs, num_reuse_blocks;
698 699

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

704 705 706
        blk = 0;
        while (blk < AC3_MAX_BLOCKS) {
            blk1 = blk + 1;
707

708 709 710 711 712
            /* 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];
713
                blk1++;
714
            }
715 716 717
            num_reuse_blocks = blk1 - blk - 1;

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

720 721
            encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk]);

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


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

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

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

            /* remaining exponents are delta encoded */
759
            for (i = 1; i <= nb_groups; i++) {
760 761 762 763 764
                /* 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
765
                av_assert2(delta0 >= 0 && delta0 <= 4);
766 767 768 769 770

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

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

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

784
    s->exponent_bits = bit_count;
785 786 787 788 789 790 791 792
}


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

797
    compute_exp_strategy(s);
798

799
    encode_exponents(s);
800

801
    group_exponents(s);
802 803

    emms_c();
804 805 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
/**
 * 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;
}


861 862 863 864 865 866 867 868 869 870 871 872
/**
 * 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;
873
    s->db_per_bit_code = 3;
874
    s->floor_code      = 7;
875 876 877 878 879 880 881 882 883 884 885 886 887 888
    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];
889 890

    count_frame_bits_fixed(s);
891 892 893
}


894 895
/**
 * Count the bits used to encode the frame, minus exponents and mantissas.
896 897
 * 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.
898
 */
899
static void count_frame_bits(AC3EncodeContext *s)
900
{
901
    AC3EncOptions *opt = &s->options;
902
    int blk, ch;
903
    int frame_bits = 0;
904

905 906 907 908 909 910 911 912 913
    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;
    }

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

921
        for (ch = 0; ch < s->fbw_channels; ch++) {
922
            if (s->exp_strategy[ch][blk] != EXP_REUSE)
923 924 925
                frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
        }
    }
926
    s->frame_bits = s->frame_bits_fixed + frame_bits;
927 928 929
}


930 931 932
/**
 * Calculate the number of bits needed to encode a set of mantissas.
 */
933