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 33
#include <stdint.h>

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

47

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


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

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

68 69 70 71
typedef struct AC3MDCTContext {
    const SampleType *window;           ///< MDCT window function
    FFTContext fft;                     ///< FFT context for MDCT calculation
} AC3MDCTContext;
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 100
/**
 * 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;
101
    int stereo_rematrixing;
102 103
} AC3EncOptions;

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

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

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

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

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

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

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

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

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

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

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

186
    SampleType **planar_samples;
187 188
    uint8_t *bap_buffer;
    uint8_t *bap1_buffer;
189
    CoefType *mdct_coef_buffer;
190
    int32_t *fixed_coef_buffer;
191 192 193 194 195 196
    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;
197

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

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

203 204 205 206
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;
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
#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 */
230
{"per_frame_metadata", "Allow Changing Metadata Per-Frame", OFFSET(allow_per_frame_metadata), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, 1, AC3ENC_PARAM},
231
/* downmix levels */
232 233
{"center_mixlev", "Center Mix Level", OFFSET(center_mix_level), FF_OPT_TYPE_FLOAT, {.dbl = LEVEL_MINUS_4POINT5DB }, 0.0, 1.0, AC3ENC_PARAM},
{"surround_mixlev", "Surround Mix Level", OFFSET(surround_mix_level), FF_OPT_TYPE_FLOAT, {.dbl = LEVEL_MINUS_6DB }, 0.0, 1.0, AC3ENC_PARAM},
234
/* audio production information */
235 236 237 238 239
{"mixing_level", "Mixing Level", OFFSET(mixing_level), FF_OPT_TYPE_INT, {.dbl = -1 }, -1, 111, AC3ENC_PARAM},
{"room_type", "Room Type", OFFSET(room_type), FF_OPT_TYPE_INT, {.dbl = -1 }, -1, 2, AC3ENC_PARAM, "room_type"},
    {"notindicated", "Not Indicated (default)", 0, FF_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
    {"large",        "Large Room",              0, FF_OPT_TYPE_CONST, {.dbl = 1 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
    {"small",        "Small Room",              0, FF_OPT_TYPE_CONST, {.dbl = 2 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
240
/* other metadata options */
241 242 243 244 245 246 247
{"copyright", "Copyright Bit", OFFSET(copyright), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, 1, AC3ENC_PARAM},
{"dialnorm", "Dialogue Level (dB)", OFFSET(dialogue_level), FF_OPT_TYPE_INT, {.dbl = -31 }, -31, -1, AC3ENC_PARAM},
{"dsur_mode", "Dolby Surround Mode", OFFSET(dolby_surround_mode), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, 2, AC3ENC_PARAM, "dsur_mode"},
    {"notindicated", "Not Indicated (default)",    0, FF_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
    {"on",           "Dolby Surround Encoded",     0, FF_OPT_TYPE_CONST, {.dbl = 1 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
    {"off",          "Not Dolby Surround Encoded", 0, FF_OPT_TYPE_CONST, {.dbl = 2 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
{"original", "Original Bit Stream", OFFSET(original), FF_OPT_TYPE_INT,   {.dbl = 1 }, 0, 1, AC3ENC_PARAM},
248
/* extended bitstream information */
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
{"dmix_mode", "Preferred Stereo Downmix Mode", OFFSET(preferred_stereo_downmix), FF_OPT_TYPE_INT, {.dbl = -1 }, -1, 2, AC3ENC_PARAM, "dmix_mode"},
    {"notindicated", "Not Indicated (default)", 0, FF_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
    {"ltrt", "Lt/Rt Downmix Preferred",         0, FF_OPT_TYPE_CONST, {.dbl = 1 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
    {"loro", "Lo/Ro Downmix Preferred",         0, FF_OPT_TYPE_CONST, {.dbl = 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, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
{"ltrt_surmixlev", "Lt/Rt Surround Mix Level", OFFSET(ltrt_surround_mix_level), FF_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
{"loro_cmixlev", "Lo/Ro Center Mix Level", OFFSET(loro_center_mix_level), FF_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
{"loro_surmixlev", "Lo/Ro Surround Mix Level", OFFSET(loro_surround_mix_level), FF_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
{"dsurex_mode", "Dolby Surround EX Mode", OFFSET(dolby_surround_ex_mode), FF_OPT_TYPE_INT, {.dbl = -1 }, -1, 2, AC3ENC_PARAM, "dsurex_mode"},
    {"notindicated", "Not Indicated (default)",       0, FF_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
    {"on",           "Dolby Surround EX Encoded",     0, FF_OPT_TYPE_CONST, {.dbl = 1 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
    {"off",          "Not Dolby Surround EX Encoded", 0, FF_OPT_TYPE_CONST, {.dbl = 2 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
{"dheadphone_mode", "Dolby Headphone Mode", OFFSET(dolby_headphone_mode), FF_OPT_TYPE_INT, {.dbl = -1 }, -1, 2, AC3ENC_PARAM, "dheadphone_mode"},
    {"notindicated", "Not Indicated (default)",     0, FF_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
    {"on",           "Dolby Headphone Encoded",     0, FF_OPT_TYPE_CONST, {.dbl = 1 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
    {"off",          "Not Dolby Headphone Encoded", 0, FF_OPT_TYPE_CONST, {.dbl = 2 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
{"ad_conv_type", "A/D Converter Type", OFFSET(ad_converter_type), FF_OPT_TYPE_INT, {.dbl = -1 }, -1, 1, AC3ENC_PARAM, "ad_conv_type"},
    {"standard", "Standard (default)", 0, FF_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
    {"hdcd",     "HDCD",               0, FF_OPT_TYPE_CONST, {.dbl = 1 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
268
/* Other Encoding Options */
269
{"stereo_rematrixing", "Stereo Rematrixing", OFFSET(stereo_rematrixing), FF_OPT_TYPE_INT, {.dbl = 1 }, 0, 1, AC3ENC_PARAM},
270 271 272 273 274 275 276 277 278 279 280 281
{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


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

static av_cold void mdct_end(AC3MDCTContext *mdct);

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

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

static int normalize_samples(AC3EncodeContext *s);

294 295
static void scale_coefficients(AC3EncodeContext *s);

296

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


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


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


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

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

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

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


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

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

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

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

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


431 432 433 434 435 436 437 438 439
/**
 * 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;

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

443 444
    s->num_rematrixing_bands = 4;

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

    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
        block = &s->blocks[blk];
449
        block->new_rematrixing_strategy = !blk;
450 451
        if (!s->rematrixing_enabled)
            continue;
452
        for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
453 454 455 456 457 458 459 460 461
            /* 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;
462 463 464 465
                MAC_COEF(sum[0], lt, lt);
                MAC_COEF(sum[1], rt, rt);
                MAC_COEF(sum[2], md, md);
                MAC_COEF(sum[3], sd, sd);
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 494
            }

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

495
    if (!s->rematrixing_enabled)
496 497 498 499 500 501 502 503
        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;
504
        for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
            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;
                }
            }
        }
    }
}


520 521 522 523 524
/**
 * Initialize exponent tables.
 */
static av_cold void exponent_init(AC3EncodeContext *s)
{
525 526 527 528 529 530 531
    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;
        }
532
    }
533 534
    /* LFE */
    exponent_group_tab[0][7] = 2;
535 536 537
}


538 539 540 541 542
/**
 * 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.
 */
543
static void extract_exponents(AC3EncodeContext *s)
544
{
545
    int blk, ch;
546 547 548

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


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

563

564 565
/**
 * Calculate exponent strategies for all channels.
566
 * Array arrangement is reversed to simplify the per-channel calculation.
567
 */
568
static void compute_exp_strategy(AC3EncodeContext *s)
569
{
570
    int ch, blk, blk1;
571

572
    for (ch = 0; ch < s->fbw_channels; ch++) {
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
        uint8_t *exp_strategy = s->exp_strategy[ch];
        uint8_t *exp          = s->blocks[0].exp[ch];
        int exp_diff;

        /* estimate if the exponent variation & decide if they should be
           reused in the next frame */
        exp_strategy[0] = EXP_NEW;
        exp += AC3_MAX_COEFS;
        for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
            exp_diff = s->dsp.sad[0](NULL, exp, exp - AC3_MAX_COEFS, 16, 16);
            if (exp_diff > EXP_DIFF_THRESHOLD)
                exp_strategy[blk] = EXP_NEW;
            else
                exp_strategy[blk] = EXP_REUSE;
            exp += AC3_MAX_COEFS;
        }

        /* now select the encoding strategy type : if exponents are often
           recoded, we use a coarse encoding */
        blk = 0;
        while (blk < AC3_MAX_BLOCKS) {
            blk1 = blk + 1;
            while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE)
                blk1++;
            switch (blk1 - blk) {
            case 1:  exp_strategy[blk] = EXP_D45; break;
            case 2:
            case 3:  exp_strategy[blk] = EXP_D25; break;
            default: exp_strategy[blk] = EXP_D15; break;
            }
            blk = blk1;
        }
605 606 607
    }
    if (s->lfe_on) {
        ch = s->lfe_channel;
608
        s->exp_strategy[ch][0] = EXP_D15;
609
        for (blk = 1; blk < AC3_MAX_BLOCKS; blk++)
610
            s->exp_strategy[ch][blk] = EXP_REUSE;
611 612 613 614
    }
}


615 616 617
/**
 * Update the exponents so that they are the ones the decoder will decode.
 */
618
static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
Fabrice Bellard's avatar
Fabrice Bellard committed
619
{
620
    int nb_groups, i, k;
Fabrice Bellard's avatar
Fabrice Bellard committed
621

622
    nb_groups = exponent_group_tab[exp_strategy-1][nb_exps] * 3;
Fabrice Bellard's avatar
Fabrice Bellard committed
623 624

    /* for each group, compute the minimum exponent */
625 626 627 628 629 630 631 632
    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
633
        }
634 635 636 637 638 639 640 641 642 643 644 645 646 647
        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;
648
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
649 650

    /* constraint for DC exponent */
651 652
    if (exp[0] > 15)
        exp[0] = 15;
Fabrice Bellard's avatar
Fabrice Bellard committed
653

654 655 656
    /* decrease the delta between each groups to within 2 so that they can be
       differentially encoded */
    for (i = 1; i <= nb_groups; i++)
657
        exp[i] = FFMIN(exp[i], exp[i-1] + 2);
658 659
    i--;
    while (--i >= 0)
660
        exp[i] = FFMIN(exp[i], exp[i+1] + 2);
661

Fabrice Bellard's avatar
Fabrice Bellard committed
662
    /* now we have the exponent values the decoder will see */
663 664 665 666 667 668 669 670 671 672 673 674 675 676
    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
677 678 679
    }
}

680

681 682 683 684 685 686
/**
 * 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.
 */
687
static void encode_exponents(AC3EncodeContext *s)
688
{
689
    int blk, blk1, ch;
690
    uint8_t *exp, *exp_strategy;
691
    int nb_coefs, num_reuse_blocks;
692 693

    for (ch = 0; ch < s->channels; ch++) {
694 695 696 697
        exp          = s->blocks[0].exp[ch];
        exp_strategy = s->exp_strategy[ch];
        nb_coefs     = s->nb_coefs[ch];

698 699 700
        blk = 0;
        while (blk < AC3_MAX_BLOCKS) {
            blk1 = blk + 1;
701

702 703 704 705 706
            /* 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];
707
                blk1++;
708
            }
709 710 711
            num_reuse_blocks = blk1 - blk - 1;

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

714 715
            encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk]);

716
            exp += AC3_MAX_COEFS * (num_reuse_blocks + 1);
717 718 719
            blk = blk1;
        }
    }
720 721 722 723 724 725 726 727
}


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

    bit_count = 0;
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
738
        AC3Block *block = &s->blocks[blk];
739
        for (ch = 0; ch < s->channels; ch++) {
740 741
            int exp_strategy = s->exp_strategy[ch][blk];
            if (exp_strategy == EXP_REUSE)
742
                continue;
743 744
            group_size = exp_strategy + (exp_strategy == EXP_D45);
            nb_groups = exponent_group_tab[exp_strategy-1][s->nb_coefs[ch]];
745
            bit_count += 4 + (nb_groups * 7);
746
            p = block->exp[ch];
747 748 749

            /* DC exponent */
            exp1 = *p++;
750
            block->grouped_exp[ch][0] = exp1;
751 752

            /* remaining exponents are delta encoded */
753
            for (i = 1; i <= nb_groups; i++) {
754 755 756 757 758
                /* 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
759
                av_assert2(delta0 >= 0 && delta0 <= 4);
760 761 762 763 764

                exp0   = exp1;
                exp1   = p[0];
                p     += group_size;
                delta1 = exp1 - exp0 + 2;
Justin Ruggles's avatar
Justin Ruggles committed
765
                av_assert2(delta1 >= 0 && delta1 <= 4);
766 767 768 769 770

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

773
                block->grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
774 775 776
            }
        }
    }
777

778
    s->exponent_bits = bit_count;
779 780 781 782 783 784 785 786
}


/**
 * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
 * Extract exponents from MDCT coefficients, calculate exponent strategies,
 * and encode final exponents.
 */
787
static void process_exponents(AC3EncodeContext *s)
788
{
789
    extract_exponents(s);
790

791
    compute_exp_strategy(s);
792

793
    encode_exponents(s);
794

795
    group_exponents(s);
796 797

    emms_c();
798 799 800
}


801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
/**
 * 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
     */

821
    /* header */
822 823 824 825 826
    frame_bits = 65;
    frame_bits += frame_bits_inc[s->channel_mode];

    /* audio blocks */
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
        /* block switch flags */
        frame_bits += s->fbw_channels;

        /* dither flags */
        frame_bits += s->fbw_channels;

        /* dynamic range */
        frame_bits++;

        /* coupling strategy */
        frame_bits++;
        if (!blk)
            frame_bits++;

        /* exponent strategy */
        frame_bits += 2 * s->fbw_channels;
843
        if (s->lfe_on)
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
            frame_bits++;

        /* bit allocation params */
        frame_bits++;
        if (!blk)
            frame_bits += 2 + 2 + 2 + 2 + 3;

        /* snr offsets and fast gain codes */
        frame_bits++;
        if (!blk)
            frame_bits += 6 + s->channels * (4 + 3);

        /* delta bit allocation */
        frame_bits++;

        /* skipped data */
        frame_bits++;
861 862
    }

863 864
    /* auxiliary data */
    frame_bits++;
865 866

    /* CRC */
867
    frame_bits += 1 + 16;
868 869 870 871 872

    s->frame_bits_fixed = frame_bits;
}


873 874 875 876 877 878 879 880 881 882 883 884
/**
 * 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;
885
    s->db_per_bit_code = 3;
886
    s->floor_code      = 7;
887 888 889 890 891 892 893 894 895 896 897 898 899 900
    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];
901 902

    count_frame_bits_fixed(s);
903 904 905
}


906 907
/**
 * Count the bits used to encode the frame, minus exponents and mantissas.
908 909
 * 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.
910
 */
911
static void count_frame_bits(AC3EncodeContext *s)
912
{
913
    AC3EncOptions *opt = &s->options;
914
    int blk, ch;
915
    int frame_bits = 0;
916

917
    /* header */
918 919 920 921 922 923 924 925 926
    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;
    }

927
    /* audio blocks */
928
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
929
        /* stereo rematrixing */
930 931 932 933
        if (s->channel_mode == AC3_CHMODE_STEREO) {
            frame_bits++;
            if (s->blocks[blk].new_rematrixing_strategy)
                frame_bits += s->num_rematrixing_bands;
934 935
        }