ac3enc.c 78.3 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 56 57 58 59 60
/* stereo rematrixing algorithms */
#define AC3_REMATRIXING_IS_STATIC 0x1
#define AC3_REMATRIXING_SUMS    0
#define AC3_REMATRIXING_NONE    1
#define AC3_REMATRIXING_ALWAYS  3

61
#if CONFIG_AC3ENC_FLOAT
62 63 64 65
#define MAC_COEF(d,a,b) ((d)+=(a)*(b))
typedef float SampleType;
typedef float CoefType;
typedef float CoefSumType;
66
#else
67 68 69 70
#define MAC_COEF(d,a,b) MAC64(d,a,b)
typedef int16_t SampleType;
typedef int32_t CoefType;
typedef int64_t CoefSumType;
71
#endif
72

73 74 75 76
typedef struct AC3MDCTContext {
    const SampleType *window;           ///< MDCT window function
    FFTContext fft;                     ///< FFT context for MDCT calculation
} AC3MDCTContext;
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/**
 * 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;

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

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

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

140 141
    int bitstream_id;                       ///< bitstream id                           (bsid)
    int bitstream_mode;                     ///< bitstream mode                         (bsmod)
142

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

146
    int frame_size_min;                     ///< minimum frame size in case rounding is necessary
147
    int frame_size;                         ///< current frame size in bytes
148
    int frame_size_code;                    ///< frame size code                        (frmsizecod)
149
    uint16_t crc_inv[2];
150 151
    int bits_written;                       ///< bit count    (used to avg. bitrate)
    int samples_written;                    ///< sample count (used to avg. bitrate)
152

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

162 163 164 165 166 167 168
    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

169
    int cutoff;                             ///< user-specified cutoff frequency, in Hz
170
    int bandwidth_code[AC3_MAX_CHANNELS];   ///< bandwidth code (0 to 60)               (chbwcod)
171
    int nb_coefs[AC3_MAX_CHANNELS];
172

173
    int rematrixing;                        ///< determines how rematrixing strategy is calculated
174
    int num_rematrixing_bands;              ///< number of rematrixing bands
175

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

190
    SampleType **planar_samples;
191 192
    uint8_t *bap_buffer;
    uint8_t *bap1_buffer;
193
    CoefType *mdct_coef_buffer;
194
    int32_t *fixed_coef_buffer;
195 196 197 198 199 200
    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;
201

202 203
    uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies

Vitor Sessak's avatar
Vitor Sessak committed
204
    DECLARE_ALIGNED(32, SampleType, windowed_samples)[AC3_WINDOW_SIZE];
205
} AC3EncodeContext;
Fabrice Bellard's avatar
Fabrice Bellard committed
206

207 208 209 210
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;
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 275 276 277 278 279 280 281 282 283
#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


284
/* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
285 286 287 288 289 290

static av_cold void mdct_end(AC3MDCTContext *mdct);

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

291
static void apply_window(DSPContext *dsp, SampleType *output, const SampleType *input,
292
                         const SampleType *window, unsigned int len);
293 294 295

static int normalize_samples(AC3EncodeContext *s);

296 297
static void scale_coefficients(AC3EncodeContext *s);

298

299 300 301 302
/**
 * LUT for number of exponent groups.
 * exponent_group_tab[exponent strategy-1][number of coefficients]
 */
303
static uint8_t exponent_group_tab[3][256];
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 329 330 331
/**
 * 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
};


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 359 360 361
/**
 * 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 } }
};


362 363 364 365 366 367 368 369 370 371
/**
 * 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;
    }
372 373
    s->frame_size = s->frame_size_min +
                    2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
374 375 376 377 378
    s->bits_written    += s->frame_size * 8;
    s->samples_written += AC3_FRAME_SIZE;
}


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

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

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

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


408 409 410 411 412
/**
 * 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.
 */
413
static void apply_mdct(AC3EncodeContext *s)
414 415 416 417 418
{
    int blk, ch;

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

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

424
            block->coeff_shift[ch] = normalize_samples(s);
425

426 427
            s->mdct.fft.mdct_calcw(&s->mdct.fft, block->mdct_coef[ch],
                                   s->windowed_samples);
428 429 430 431 432
        }
    }
}


433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
/**
 * 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;

464 465
    s->num_rematrixing_bands = 4;

466 467 468 469 470 471 472
    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];
473
        block->new_rematrixing_strategy = !blk;
474
        for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
475 476 477 478 479 480 481 482 483
            /* 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;
484 485 486 487
                MAC_COEF(sum[0], lt, lt);
                MAC_COEF(sum[1], rt, rt);
                MAC_COEF(sum[2], md, md);
                MAC_COEF(sum[3], sd, sd);
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
            }

            /* 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;
526
        for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
            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;
                }
            }
        }
    }
}


542 543 544 545 546 547 548 549 550 551 552
/**
 * 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;
    }
553 554
    /* LFE */
    exponent_group_tab[0][7] = 2;
555 556 557
}


558 559 560 561 562
/**
 * 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.
 */
563
static void extract_exponents(AC3EncodeContext *s)
564
{
565
    int blk, ch;
566 567 568

    for (ch = 0; ch < s->channels; ch++) {
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
569
            AC3Block *block = &s->blocks[blk];
570 571
            s->ac3dsp.extract_exponents(block->exp[ch], block->fixed_coef[ch],
                                        AC3_MAX_COEFS);
572 573 574 575 576
        }
    }
}


577 578 579 580
/**
 * Exponent Difference Threshold.
 * New exponents are sent if their SAD exceed this number.
 */
581
#define EXP_DIFF_THRESHOLD 500
582

583

584 585 586
/**
 * Calculate exponent strategies for all blocks in a single channel.
 */
587
static void compute_exp_strategy_ch(AC3EncodeContext *s, uint8_t *exp_strategy,
588
                                    uint8_t *exp)
Fabrice Bellard's avatar
Fabrice Bellard committed
589
{
590
    int blk, blk1;
Fabrice Bellard's avatar
Fabrice Bellard committed
591
    int exp_diff;
592

Fabrice Bellard's avatar
Fabrice Bellard committed
593 594
    /* estimate if the exponent variation & decide if they should be
       reused in the next frame */
595
    exp_strategy[0] = EXP_NEW;
596
    exp += AC3_MAX_COEFS;
597
    for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
598
        exp_diff = s->dsp.sad[0](NULL, exp, exp - AC3_MAX_COEFS, 16, 16);
Fabrice Bellard's avatar
Fabrice Bellard committed
599
        if (exp_diff > EXP_DIFF_THRESHOLD)
600
            exp_strategy[blk] = EXP_NEW;
Fabrice Bellard's avatar
Fabrice Bellard committed
601
        else
602
            exp_strategy[blk] = EXP_REUSE;
603
        exp += AC3_MAX_COEFS;
Fabrice Bellard's avatar
Fabrice Bellard committed
604
    }
605

Fabrice Bellard's avatar
Fabrice Bellard committed
606 607
    /* now select the encoding strategy type : if exponents are often
       recoded, we use a coarse encoding */
608 609 610
    blk = 0;
    while (blk < AC3_MAX_BLOCKS) {
        blk1 = blk + 1;
611
        while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE)
612 613
            blk1++;
        switch (blk1 - blk) {
614
        case 1:  exp_strategy[blk] = EXP_D45; break;
Fabrice Bellard's avatar
Fabrice Bellard committed
615
        case 2:
616 617
        case 3:  exp_strategy[blk] = EXP_D25; break;
        default: exp_strategy[blk] = EXP_D15; break;
Fabrice Bellard's avatar
Fabrice Bellard committed
618
        }
619
        blk = blk1;
Fabrice Bellard's avatar
Fabrice Bellard committed
620 621 622
    }
}

623

624 625
/**
 * Calculate exponent strategies for all channels.
626
 * Array arrangement is reversed to simplify the per-channel calculation.
627
 */
628
static void compute_exp_strategy(AC3EncodeContext *s)
629
{
630
    int ch, blk;
631

632
    for (ch = 0; ch < s->fbw_channels; ch++) {
633
        compute_exp_strategy_ch(s, s->exp_strategy[ch], s->blocks[0].exp[ch]);
634 635 636
    }
    if (s->lfe_on) {
        ch = s->lfe_channel;
637
        s->exp_strategy[ch][0] = EXP_D15;
638
        for (blk = 1; blk < AC3_MAX_BLOCKS; blk++)
639
            s->exp_strategy[ch][blk] = EXP_REUSE;
640 641 642 643
    }
}


644 645 646
/**
 * Update the exponents so that they are the ones the decoder will decode.
 */
647
static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
Fabrice Bellard's avatar
Fabrice Bellard committed
648
{
649
    int nb_groups, i, k;
Fabrice Bellard's avatar
Fabrice Bellard committed
650

651
    nb_groups = exponent_group_tab[exp_strategy-1][nb_exps] * 3;
Fabrice Bellard's avatar
Fabrice Bellard committed
652 653

    /* for each group, compute the minimum exponent */
654 655 656 657 658 659 660 661
    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
662
        }
663 664 665 666 667 668 669 670 671 672 673 674 675 676
        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;
677
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
678 679

    /* constraint for DC exponent */
680 681
    if (exp[0] > 15)
        exp[0] = 15;
Fabrice Bellard's avatar
Fabrice Bellard committed
682

683 684 685
    /* decrease the delta between each groups to within 2 so that they can be
       differentially encoded */
    for (i = 1; i <= nb_groups; i++)
686
        exp[i] = FFMIN(exp[i], exp[i-1] + 2);
687 688
    i--;
    while (--i >= 0)
689
        exp[i] = FFMIN(exp[i], exp[i+1] + 2);
690

Fabrice Bellard's avatar
Fabrice Bellard committed
691
    /* now we have the exponent values the decoder will see */
692 693 694 695 696 697 698 699 700 701 702 703 704 705
    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
706 707 708
    }
}

709

710 711 712 713 714 715
/**
 * 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.
 */
716
static void encode_exponents(AC3EncodeContext *s)
717
{
718
    int blk, blk1, ch;
719
    uint8_t *exp, *exp_strategy;
720
    int nb_coefs, num_reuse_blocks;
721 722

    for (ch = 0; ch < s->channels; ch++) {
723 724 725 726
        exp          = s->blocks[0].exp[ch];
        exp_strategy = s->exp_strategy[ch];
        nb_coefs     = s->nb_coefs[ch];

727 728 729
        blk = 0;
        while (blk < AC3_MAX_BLOCKS) {
            blk1 = blk + 1;
730

731 732 733 734 735
            /* 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];
736
                blk1++;
737
            }
738 739 740
            num_reuse_blocks = blk1 - blk - 1;

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

743 744
            encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk]);

745
            exp += AC3_MAX_COEFS * (num_reuse_blocks + 1);
746 747 748
            blk = blk1;
        }
    }
749 750 751 752 753 754 755 756
}


/**
 * Group exponents.
 * 3 delta-encoded exponents are in each 7-bit group. The number of groups
 * varies depending on exponent strategy and bandwidth.
 */
757
static void group_exponents(AC3EncodeContext *s)
758 759
{
    int blk, ch, i;
760
    int group_size, nb_groups, bit_count;
761 762 763 764 765 766
    uint8_t *p;
    int delta0, delta1, delta2;
    int exp0, exp1;

    bit_count = 0;
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
767
        AC3Block *block = &s->blocks[blk];
768
        for (ch = 0; ch < s->channels; ch++) {
769 770
            int exp_strategy = s->exp_strategy[ch][blk];
            if (exp_strategy == EXP_REUSE)
771
                continue;
772 773
            group_size = exp_strategy + (exp_strategy == EXP_D45);
            nb_groups = exponent_group_tab[exp_strategy-1][s->nb_coefs[ch]];
774
            bit_count += 4 + (nb_groups * 7);
775
            p = block->exp[ch];
776 777 778

            /* DC exponent */
            exp1 = *p++;
779
            block->grouped_exp[ch][0] = exp1;
780 781

            /* remaining exponents are delta encoded */
782
            for (i = 1; i <= nb_groups; i++) {
783 784 785 786 787
                /* 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
788
                av_assert2(delta0 >= 0 && delta0 <= 4);
789 790 791 792 793

                exp0   = exp1;
                exp1   = p[0];
                p     += group_size;
                delta1 = exp1 - exp0 + 2;
Justin Ruggles's avatar
Justin Ruggles committed
794
                av_assert2(delta1 >= 0 && delta1 <= 4);
795 796 797 798 799

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

802
                block->grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
803 804 805
            }
        }
    }
806

807
    s->exponent_bits = bit_count;
808 809 810 811 812 813 814 815
}


/**
 * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
 * Extract exponents from MDCT coefficients, calculate exponent strategies,
 * and encode final exponents.
 */
816
static void process_exponents(AC3EncodeContext *s)
817
{
818
    extract_exponents(s);
819

820
    compute_exp_strategy(s);
821

822
    encode_exponents(s);
823

824
    group_exponents(s);
825 826

    emms_c();
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 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
/**
 * 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;
}


884 885 886 887 888 889 890 891 892 893 894 895
/**
 * 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;
896
    s->db_per_bit_code = 3;
897
    s->floor_code      = 7;
898 899 900 901 902 903 904 905 906 907 908 909 910 911
    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];
912 913

    count_frame_bits_fixed(s);
914 915 916
}


917 918
/**
 * Count the bits used to encode the frame, minus exponents and mantissas.
919 920
 * 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.
921
 */
922
static void count_frame_bits(AC3EncodeContext *s)
923
{
924
    AC3EncOptions *opt = &s->options;
925
    int blk, ch;
926
    int frame_bits = 0;
927

928 929 930 931 932 933 934 935 936
    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;
    }

937
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
938 939 940
        /* stereo rematrixing */
        if (s->channel_mode == AC3_CHMODE_STEREO &&
            s->blocks[blk].new_rematrixing_strategy) {
941
            frame_bits += s->num_rematrixing_bands;
942 943
        }

944
        for (ch = 0; ch < s->fbw_channels; ch++) {
945
            if (s->exp_strategy