dcaenc.c 31.1 KB
Newer Older
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
1 2
/*
 * DCA encoder
Paul B Mahol's avatar
Paul B Mahol committed
3
 * Copyright (C) 2008-2012 Alexander E. Patrakov
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *               2010 Benjamin Larsson
 *               2011 Xiang Wang
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

Paul B Mahol's avatar
Paul B Mahol committed
24
#include "libavutil/avassert.h"
25
#include "libavutil/channel_layout.h"
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
26
#include "libavutil/common.h"
27
#include "libavutil/ffmath.h"
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
28
#include "avcodec.h"
Paul B Mahol's avatar
Paul B Mahol committed
29 30 31
#include "dca.h"
#include "dcadata.h"
#include "dcaenc.h"
32
#include "internal.h"
33
#include "mathops.h"
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
34 35 36
#include "put_bits.h"

#define MAX_CHANNELS 6
Paul B Mahol's avatar
Paul B Mahol committed
37
#define DCA_MAX_FRAME_SIZE 16384
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
38
#define DCA_HEADER_SIZE 13
Paul B Mahol's avatar
Paul B Mahol committed
39
#define DCA_LFE_SAMPLES 8
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
40

41
#define DCAENC_SUBBANDS 32
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
42
#define SUBFRAMES 1
Paul B Mahol's avatar
Paul B Mahol committed
43 44 45
#define SUBSUBFRAMES 2
#define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
#define AUBANDS 25
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
46

47
typedef struct DCAEncContext {
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
48 49
    PutBitContext pb;
    int frame_size;
Paul B Mahol's avatar
Paul B Mahol committed
50 51 52
    int frame_bits;
    int fullband_channels;
    int channels;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
53
    int lfe_channel;
Paul B Mahol's avatar
Paul B Mahol committed
54 55 56 57 58
    int samplerate_index;
    int bitrate_index;
    int channel_config;
    const int32_t *band_interpolation;
    const int32_t *band_spectrum;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
59
    int lfe_scale_factor;
Paul B Mahol's avatar
Paul B Mahol committed
60 61
    softfloat lfe_quant;
    int32_t lfe_peak_cb;
62
    const int8_t *channel_order_tab;  ///< channel reordering table, lfe and non lfe
Paul B Mahol's avatar
Paul B Mahol committed
63 64

    int32_t history[512][MAX_CHANNELS]; /* This is a circular buffer */
65 66 67
    int32_t subband[SUBBAND_SAMPLES][DCAENC_SUBBANDS][MAX_CHANNELS];
    int32_t quantized[SUBBAND_SAMPLES][DCAENC_SUBBANDS][MAX_CHANNELS];
    int32_t peak_cb[DCAENC_SUBBANDS][MAX_CHANNELS];
Paul B Mahol's avatar
Paul B Mahol committed
68 69
    int32_t downsampled_lfe[DCA_LFE_SAMPLES];
    int32_t masking_curve_cb[SUBSUBFRAMES][256];
70 71 72
    int abits[DCAENC_SUBBANDS][MAX_CHANNELS];
    int scale_factor[DCAENC_SUBBANDS][MAX_CHANNELS];
    softfloat quant[DCAENC_SUBBANDS][MAX_CHANNELS];
Paul B Mahol's avatar
Paul B Mahol committed
73 74 75 76 77
    int32_t eff_masking_curve_cb[256];
    int32_t band_masking_cb[32];
    int32_t worst_quantization_noise;
    int32_t worst_noise_ever;
    int consumed_bits;
78
} DCAEncContext;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
79

Paul B Mahol's avatar
Paul B Mahol committed
80 81 82 83 84 85 86
static int32_t cos_table[2048];
static int32_t band_interpolation[2][512];
static int32_t band_spectrum[2][8];
static int32_t auf[9][AUBANDS][256];
static int32_t cb_to_add[256];
static int32_t cb_to_level[2048];
static int32_t lfe_fir_64i[512];
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
87

Paul B Mahol's avatar
Paul B Mahol committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/* Transfer function of outer and middle ear, Hz -> dB */
static double hom(double f)
{
    double f1 = f / 1000;

    return -3.64 * pow(f1, -0.8)
           + 6.8 * exp(-0.6 * (f1 - 3.4) * (f1 - 3.4))
           - 6.0 * exp(-0.15 * (f1 - 8.7) * (f1 - 8.7))
           - 0.0006 * (f1 * f1) * (f1 * f1);
}

static double gammafilter(int i, double f)
{
    double h = (f - fc[i]) / erb[i];

    h = 1 + h * h;
    h = 1 / (h * h);
    return 20 * log10(h);
}

static int encode_init(AVCodecContext *avctx)
{
110
    DCAEncContext *c = avctx->priv_data;
Paul B Mahol's avatar
Paul B Mahol committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    uint64_t layout = avctx->channel_layout;
    int i, min_frame_bits;

    c->fullband_channels = c->channels = avctx->channels;
    c->lfe_channel = (avctx->channels == 3 || avctx->channels == 6);
    c->band_interpolation = band_interpolation[1];
    c->band_spectrum = band_spectrum[1];
    c->worst_quantization_noise = -2047;
    c->worst_noise_ever = -2047;

    if (!layout) {
        av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
                                      "encoder will guess the layout, but it "
                                      "might be incorrect.\n");
        layout = av_get_default_channel_layout(avctx->channels);
    }
    switch (layout) {
    case AV_CH_LAYOUT_MONO:         c->channel_config = 0; break;
    case AV_CH_LAYOUT_STEREO:       c->channel_config = 2; break;
    case AV_CH_LAYOUT_2_2:          c->channel_config = 8; break;
    case AV_CH_LAYOUT_5POINT0:      c->channel_config = 9; break;
    case AV_CH_LAYOUT_5POINT1:      c->channel_config = 9; break;
    default:
        av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout!\n");
        return AVERROR_PATCHWELCOME;
    }

138
    if (c->lfe_channel) {
Paul B Mahol's avatar
Paul B Mahol committed
139
        c->fullband_channels--;
140
        c->channel_order_tab = channel_reorder_lfe[c->channel_config];
141
    } else {
142
        c->channel_order_tab = channel_reorder_nolfe[c->channel_config];
143
    }
Paul B Mahol's avatar
Paul B Mahol committed
144 145 146 147 148 149 150 151 152 153

    for (i = 0; i < 9; i++) {
        if (sample_rates[i] == avctx->sample_rate)
            break;
    }
    if (i == 9)
        return AVERROR(EINVAL);
    c->samplerate_index = i;

    if (avctx->bit_rate < 32000 || avctx->bit_rate > 3840000) {
154
        av_log(avctx, AV_LOG_ERROR, "Bit rate %"PRId64" not supported.", (int64_t)avctx->bit_rate);
Paul B Mahol's avatar
Paul B Mahol committed
155 156
        return AVERROR(EINVAL);
    }
157
    for (i = 0; ff_dca_bit_rates[i] < avctx->bit_rate; i++)
Paul B Mahol's avatar
Paul B Mahol committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171
        ;
    c->bitrate_index = i;
    c->frame_bits = FFALIGN((avctx->bit_rate * 512 + avctx->sample_rate - 1) / avctx->sample_rate, 32);
    min_frame_bits = 132 + (493 + 28 * 32) * c->fullband_channels + c->lfe_channel * 72;
    if (c->frame_bits < min_frame_bits || c->frame_bits > (DCA_MAX_FRAME_SIZE << 3))
        return AVERROR(EINVAL);

    c->frame_size = (c->frame_bits + 7) / 8;

    avctx->frame_size = 32 * SUBBAND_SAMPLES;

    if (!cos_table[0]) {
        int j, k;

172 173 174 175
        cos_table[0] = 0x7fffffff;
        cos_table[512] = 0;
        cos_table[1024] = -cos_table[0];
        for (i = 1; i < 512; i++) {
Paul B Mahol's avatar
Paul B Mahol committed
176
            cos_table[i]   = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
177 178 179 180 181
            cos_table[1024-i] = -cos_table[i];
            cos_table[1024+i] = -cos_table[i];
            cos_table[2048-i] = cos_table[i];
        }
        for (i = 0; i < 2048; i++) {
182
            cb_to_level[i] = (int32_t)(0x7fffffff * ff_exp10(-0.005 * i));
Paul B Mahol's avatar
Paul B Mahol committed
183 184
        }

185 186 187 188 189
        for (k = 0; k < 32; k++) {
            for (j = 0; j < 8; j++) {
                lfe_fir_64i[64 * j + k] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
                lfe_fir_64i[64 * (7-j) + (63 - k)] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
            }
190 191
        }

Paul B Mahol's avatar
Paul B Mahol committed
192
        for (i = 0; i < 512; i++) {
193 194
            band_interpolation[0][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_perfect[i]);
            band_interpolation[1][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_nonperfect[i]);
Paul B Mahol's avatar
Paul B Mahol committed
195 196 197 198 199 200 201 202 203 204 205 206 207
        }

        for (i = 0; i < 9; i++) {
            for (j = 0; j < AUBANDS; j++) {
                for (k = 0; k < 256; k++) {
                    double freq = sample_rates[i] * (k + 0.5) / 512;

                    auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
                }
            }
        }

        for (i = 0; i < 256; i++) {
208
            double add = 1 + ff_exp10(-0.01 * i);
Paul B Mahol's avatar
Paul B Mahol committed
209 210 211 212 213
            cb_to_add[i] = (int32_t)(100 * log10(add));
        }
        for (j = 0; j < 8; j++) {
            double accum = 0;
            for (i = 0; i < 512; i++) {
214
                double reconst = ff_dca_fir_32bands_perfect[i] * ((i & 64) ? (-1) : 1);
Paul B Mahol's avatar
Paul B Mahol committed
215 216 217 218 219 220 221
                accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
            }
            band_spectrum[0][j] = (int32_t)(200 * log10(accum));
        }
        for (j = 0; j < 8; j++) {
            double accum = 0;
            for (i = 0; i < 512; i++) {
222
                double reconst = ff_dca_fir_32bands_nonperfect[i] * ((i & 64) ? (-1) : 1);
Paul B Mahol's avatar
Paul B Mahol committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
                accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
            }
            band_spectrum[1][j] = (int32_t)(200 * log10(accum));
        }
    }
    return 0;
}

static inline int32_t cos_t(int x)
{
    return cos_table[x & 2047];
}

static inline int32_t sin_t(int x)
{
    return cos_t(x - 512);
}
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
240

Paul B Mahol's avatar
Paul B Mahol committed
241 242 243 244
static inline int32_t half32(int32_t a)
{
    return (a + 1) >> 1;
}
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
245 246 247

static inline int32_t mul32(int32_t a, int32_t b)
{
Paul B Mahol's avatar
Paul B Mahol committed
248 249 250 251
    int64_t r = (int64_t)a * b + 0x80000000ULL;
    return r >> 32;
}

252
static void subband_transform(DCAEncContext *c, const int32_t *input)
Paul B Mahol's avatar
Paul B Mahol committed
253 254 255 256 257 258 259
{
    int ch, subs, i, k, j;

    for (ch = 0; ch < c->fullband_channels; ch++) {
        /* History is copied because it is also needed for PSY */
        int32_t hist[512];
        int hist_start = 0;
260
        const int chi = c->channel_order_tab[ch];
Paul B Mahol's avatar
Paul B Mahol committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

        for (i = 0; i < 512; i++)
            hist[i] = c->history[i][ch];

        for (subs = 0; subs < SUBBAND_SAMPLES; subs++) {
            int32_t accum[64];
            int32_t resp;
            int band;

            /* Calculate the convolutions at once */
            for (i = 0; i < 64; i++)
                accum[i] = 0;

            for (k = 0, i = hist_start, j = 0;
                    i < 512; k = (k + 1) & 63, i++, j++)
                accum[k] += mul32(hist[i], c->band_interpolation[j]);
            for (i = 0; i < hist_start; k = (k + 1) & 63, i++, j++)
                accum[k] += mul32(hist[i], c->band_interpolation[j]);

            for (k = 16; k < 32; k++)
                accum[k] = accum[k] - accum[31 - k];
            for (k = 32; k < 48; k++)
                accum[k] = accum[k] + accum[95 - k];

            for (band = 0; band < 32; band++) {
                resp = 0;
                for (i = 16; i < 48; i++) {
                    int s = (2 * band + 1) * (2 * (i + 16) + 1);
                    resp += mul32(accum[i], cos_t(s << 3)) >> 3;
                }

                c->subband[subs][band][ch] = ((band + 1) & 2) ? -resp : resp;
            }

            /* Copy in 32 new samples from input */
            for (i = 0; i < 32; i++)
297
                hist[i + hist_start] = input[(subs * 32 + i) * c->channels + chi];
Paul B Mahol's avatar
Paul B Mahol committed
298 299 300 301 302
            hist_start = (hist_start + 32) & 511;
        }
    }
}

303
static void lfe_downsample(DCAEncContext *c, const int32_t *input)
Paul B Mahol's avatar
Paul B Mahol committed
304 305
{
    /* FIXME: make 128x LFE downsampling possible */
306
    const int lfech = lfe_index[c->channel_config];
Paul B Mahol's avatar
Paul B Mahol committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    int i, j, lfes;
    int32_t hist[512];
    int32_t accum;
    int hist_start = 0;

    for (i = 0; i < 512; i++)
        hist[i] = c->history[i][c->channels - 1];

    for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) {
        /* Calculate the convolution */
        accum = 0;

        for (i = hist_start, j = 0; i < 512; i++, j++)
            accum += mul32(hist[i], lfe_fir_64i[j]);
        for (i = 0; i < hist_start; i++, j++)
            accum += mul32(hist[i], lfe_fir_64i[j]);

        c->downsampled_lfe[lfes] = accum;

        /* Copy in 64 new samples from input */
        for (i = 0; i < 64; i++)
328
            hist[i + hist_start] = input[(lfes * 64 + i) * c->channels + lfech];
Paul B Mahol's avatar
Paul B Mahol committed
329 330 331

        hist_start = (hist_start + 64) & 511;
    }
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
332 333
}

Paul B Mahol's avatar
Paul B Mahol committed
334 335 336 337
typedef struct {
    int32_t re;
    int32_t im;
} cplx32;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
338

Paul B Mahol's avatar
Paul B Mahol committed
339
static void fft(const int32_t in[2 * 256], cplx32 out[256])
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
340
{
Paul B Mahol's avatar
Paul B Mahol committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    cplx32 buf[256], rin[256], rout[256];
    int i, j, k, l;

    /* do two transforms in parallel */
    for (i = 0; i < 256; i++) {
        /* Apply the Hann window */
        rin[i].re = mul32(in[2 * i], 0x3fffffff - (cos_t(8 * i + 2) >> 1));
        rin[i].im = mul32(in[2 * i + 1], 0x3fffffff - (cos_t(8 * i + 6) >> 1));
    }
    /* pre-rotation */
    for (i = 0; i < 256; i++) {
        buf[i].re = mul32(cos_t(4 * i + 2), rin[i].re)
                  - mul32(sin_t(4 * i + 2), rin[i].im);
        buf[i].im = mul32(cos_t(4 * i + 2), rin[i].im)
                  + mul32(sin_t(4 * i + 2), rin[i].re);
    }

    for (j = 256, l = 1; j != 1; j >>= 1, l <<= 1) {
        for (k = 0; k < 256; k += j) {
            for (i = k; i < k + j / 2; i++) {
                cplx32 sum, diff;
                int t = 8 * l * i;

                sum.re = buf[i].re + buf[i + j / 2].re;
                sum.im = buf[i].im + buf[i + j / 2].im;

                diff.re = buf[i].re - buf[i + j / 2].re;
                diff.im = buf[i].im - buf[i + j / 2].im;

                buf[i].re = half32(sum.re);
                buf[i].im = half32(sum.im);

                buf[i + j / 2].re = mul32(diff.re, cos_t(t))
                                  - mul32(diff.im, sin_t(t));
                buf[i + j / 2].im = mul32(diff.im, cos_t(t))
                                  + mul32(diff.re, sin_t(t));
            }
        }
    }
    /* post-rotation */
    for (i = 0; i < 256; i++) {
        int b = ff_reverse[i];
        rout[i].re = mul32(buf[b].re, cos_t(4 * i))
                   - mul32(buf[b].im, sin_t(4 * i));
        rout[i].im = mul32(buf[b].im, cos_t(4 * i))
                   + mul32(buf[b].re, sin_t(4 * i));
    }
    for (i = 0; i < 256; i++) {
        /* separate the results of the two transforms */
        cplx32 o1, o2;

        o1.re =  rout[i].re - rout[255 - i].re;
        o1.im =  rout[i].im + rout[255 - i].im;

        o2.re =  rout[i].im - rout[255 - i].im;
        o2.im = -rout[i].re - rout[255 - i].re;

        /* combine them into one long transform */
        out[i].re = mul32( o1.re + o2.re, cos_t(2 * i + 1))
                  + mul32( o1.im - o2.im, sin_t(2 * i + 1));
        out[i].im = mul32( o1.im + o2.im, cos_t(2 * i + 1))
                  + mul32(-o1.re + o2.re, sin_t(2 * i + 1));
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
403
    }
Paul B Mahol's avatar
Paul B Mahol committed
404
}
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
405

Paul B Mahol's avatar
Paul B Mahol committed
406 407 408 409 410 411 412 413 414 415
static int32_t get_cb(int32_t in)
{
    int i, res;

    res = 0;
    if (in < 0)
        in = -in;
    for (i = 1024; i > 0; i >>= 1) {
        if (cb_to_level[i + res] >= in)
            res += i;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
416
    }
Paul B Mahol's avatar
Paul B Mahol committed
417
    return -res;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
418 419
}

Paul B Mahol's avatar
Paul B Mahol committed
420
static int32_t add_cb(int32_t a, int32_t b)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
421
{
Paul B Mahol's avatar
Paul B Mahol committed
422 423 424 425 426 427
    if (a < b)
        FFSWAP(int32_t, a, b);

    if (a - b >= 256)
        return a;
    return a + cb_to_add[a - b];
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
428 429
}

Paul B Mahol's avatar
Paul B Mahol committed
430 431
static void adjust_jnd(int samplerate_index,
                       const int32_t in[512], int32_t out_cb[256])
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
432
{
Paul B Mahol's avatar
Paul B Mahol committed
433 434 435 436 437 438 439 440 441
    int32_t power[256];
    cplx32 out[256];
    int32_t out_cb_unnorm[256];
    int32_t denom;
    const int32_t ca_cb = -1114;
    const int32_t cs_cb = 928;
    int i, j;

    fft(in, out);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
442

Paul B Mahol's avatar
Paul B Mahol committed
443 444 445
    for (j = 0; j < 256; j++) {
        power[j] = add_cb(get_cb(out[j].re), get_cb(out[j].im));
        out_cb_unnorm[j] = -2047; /* and can only grow */
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
446
    }
Paul B Mahol's avatar
Paul B Mahol committed
447 448 449 450 451 452 453 454 455 456 457 458

    for (i = 0; i < AUBANDS; i++) {
        denom = ca_cb; /* and can only grow */
        for (j = 0; j < 256; j++)
            denom = add_cb(denom, power[j] + auf[samplerate_index][i][j]);
        for (j = 0; j < 256; j++)
            out_cb_unnorm[j] = add_cb(out_cb_unnorm[j],
                    -denom + auf[samplerate_index][i][j]);
    }

    for (j = 0; j < 256; j++)
        out_cb[j] = add_cb(out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
459 460
}

461
typedef void (*walk_band_t)(DCAEncContext *c, int band1, int band2, int f,
Paul B Mahol's avatar
Paul B Mahol committed
462 463 464
                            int32_t spectrum1, int32_t spectrum2, int channel,
                            int32_t * arg);

465
static void walk_band_low(DCAEncContext *c, int band, int channel,
Paul B Mahol's avatar
Paul B Mahol committed
466
                          walk_band_t walk, int32_t *arg)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
467
{
Paul B Mahol's avatar
Paul B Mahol committed
468
    int f;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
469

Paul B Mahol's avatar
Paul B Mahol committed
470 471 472 473 474 475 476 477 478
    if (band == 0) {
        for (f = 0; f < 4; f++)
            walk(c, 0, 0, f, 0, -2047, channel, arg);
    } else {
        for (f = 0; f < 8; f++)
            walk(c, band, band - 1, 8 * band - 4 + f,
                    c->band_spectrum[7 - f], c->band_spectrum[f], channel, arg);
    }
}
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
479

480
static void walk_band_high(DCAEncContext *c, int band, int channel,
Paul B Mahol's avatar
Paul B Mahol committed
481 482 483
                           walk_band_t walk, int32_t *arg)
{
    int f;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
484

Paul B Mahol's avatar
Paul B Mahol committed
485 486 487 488 489 490 491 492 493
    if (band == 31) {
        for (f = 0; f < 4; f++)
            walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
    } else {
        for (f = 0; f < 8; f++)
            walk(c, band, band + 1, 8 * band + 4 + f,
                    c->band_spectrum[f], c->band_spectrum[7 - f], channel, arg);
    }
}
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
494

495
static void update_band_masking(DCAEncContext *c, int band1, int band2,
Paul B Mahol's avatar
Paul B Mahol committed
496 497 498 499
                                int f, int32_t spectrum1, int32_t spectrum2,
                                int channel, int32_t * arg)
{
    int32_t value = c->eff_masking_curve_cb[f] - spectrum1;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
500

Paul B Mahol's avatar
Paul B Mahol committed
501 502 503 504
    if (value < c->band_masking_cb[band1])
        c->band_masking_cb[band1] = value;
}

505
static void calc_masking(DCAEncContext *c, const int32_t *input)
Paul B Mahol's avatar
Paul B Mahol committed
506 507 508 509 510 511 512 513 514 515
{
    int i, k, band, ch, ssf;
    int32_t data[512];

    for (i = 0; i < 256; i++)
        for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
            c->masking_curve_cb[ssf][i] = -2047;

    for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
        for (ch = 0; ch < c->fullband_channels; ch++) {
516 517
            const int chi = c->channel_order_tab[ch];

Paul B Mahol's avatar
Paul B Mahol committed
518 519 520
            for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
                data[i] = c->history[k][ch];
            for (k -= 512; i < 512; i++, k++)
521
                data[i] = input[k * c->channels + chi];
Paul B Mahol's avatar
Paul B Mahol committed
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
            adjust_jnd(c->samplerate_index, data, c->masking_curve_cb[ssf]);
        }
    for (i = 0; i < 256; i++) {
        int32_t m = 2048;

        for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
            if (c->masking_curve_cb[ssf][i] < m)
                m = c->masking_curve_cb[ssf][i];
        c->eff_masking_curve_cb[i] = m;
    }

    for (band = 0; band < 32; band++) {
        c->band_masking_cb[band] = 2048;
        walk_band_low(c, band, 0, update_band_masking, NULL);
        walk_band_high(c, band, 0, update_band_masking, NULL);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
537 538 539
    }
}

540
static void find_peaks(DCAEncContext *c)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
541
{
Paul B Mahol's avatar
Paul B Mahol committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    int band, ch;

    for (band = 0; band < 32; band++)
        for (ch = 0; ch < c->fullband_channels; ch++) {
            int sample;
            int32_t m = 0;

            for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
                int32_t s = abs(c->subband[sample][band][ch]);
                if (m < s)
                    m = s;
            }
            c->peak_cb[band][ch] = get_cb(m);
        }

    if (c->lfe_channel) {
        int sample;
        int32_t m = 0;

        for (sample = 0; sample < DCA_LFE_SAMPLES; sample++)
            if (m < abs(c->downsampled_lfe[sample]))
                m = abs(c->downsampled_lfe[sample]);
        c->lfe_peak_cb = get_cb(m);
    }
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
566 567
}

Paul B Mahol's avatar
Paul B Mahol committed
568 569 570 571 572
static const int snr_fudge = 128;
#define USED_1ABITS 1
#define USED_NABITS 2
#define USED_26ABITS 4

573
static int init_quantization_noise(DCAEncContext *c, int noise)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
574
{
Paul B Mahol's avatar
Paul B Mahol committed
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
    int ch, band, ret = 0;

    c->consumed_bits = 132 + 493 * c->fullband_channels;
    if (c->lfe_channel)
        c->consumed_bits += 72;

    /* attempt to guess the bit distribution based on the prevoius frame */
    for (ch = 0; ch < c->fullband_channels; ch++) {
        for (band = 0; band < 32; band++) {
            int snr_cb = c->peak_cb[band][ch] - c->band_masking_cb[band] - noise;

            if (snr_cb >= 1312) {
                c->abits[band][ch] = 26;
                ret |= USED_26ABITS;
            } else if (snr_cb >= 222) {
                c->abits[band][ch] = 8 + mul32(snr_cb - 222, 69000000);
                ret |= USED_NABITS;
            } else if (snr_cb >= 0) {
                c->abits[band][ch] = 2 + mul32(snr_cb, 106000000);
                ret |= USED_NABITS;
            } else {
                c->abits[band][ch] = 1;
                ret |= USED_1ABITS;
            }
        }
    }
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
601

Paul B Mahol's avatar
Paul B Mahol committed
602 603 604 605 606 607 608 609
    for (band = 0; band < 32; band++)
        for (ch = 0; ch < c->fullband_channels; ch++) {
            c->consumed_bits += bit_consumption[c->abits[band][ch]];
        }

    return ret;
}

610
static void assign_bits(DCAEncContext *c)
Paul B Mahol's avatar
Paul B Mahol committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
{
    /* Find the bounds where the binary search should work */
    int low, high, down;
    int used_abits = 0;

    init_quantization_noise(c, c->worst_quantization_noise);
    low = high = c->worst_quantization_noise;
    if (c->consumed_bits > c->frame_bits) {
        while (c->consumed_bits > c->frame_bits) {
            av_assert0(used_abits != USED_1ABITS);
            low = high;
            high += snr_fudge;
            used_abits = init_quantization_noise(c, high);
        }
    } else {
        while (c->consumed_bits <= c->frame_bits) {
            high = low;
            if (used_abits == USED_26ABITS)
                goto out; /* The requested bitrate is too high, pad with zeros */
            low -= snr_fudge;
            used_abits = init_quantization_noise(c, low);
        }
    }

    /* Now do a binary search between low and high to see what fits */
    for (down = snr_fudge >> 1; down; down >>= 1) {
        init_quantization_noise(c, high - down);
        if (c->consumed_bits <= c->frame_bits)
            high -= down;
    }
    init_quantization_noise(c, high);
out:
    c->worst_quantization_noise = high;
    if (high > c->worst_noise_ever)
        c->worst_noise_ever = high;
}

648
static void shift_history(DCAEncContext *c, const int32_t *input)
Paul B Mahol's avatar
Paul B Mahol committed
649 650 651 652
{
    int k, ch;

    for (k = 0; k < 512; k++)
653 654 655 656 657
        for (ch = 0; ch < c->channels; ch++) {
            const int chi = c->channel_order_tab[ch];

            c->history[k][ch] = input[k * c->channels + chi];
        }
Paul B Mahol's avatar
Paul B Mahol committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
}

static int32_t quantize_value(int32_t value, softfloat quant)
{
    int32_t offset = 1 << (quant.e - 1);

    value = mul32(value, quant.m) + offset;
    value = value >> quant.e;
    return value;
}

static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
{
    int32_t peak;
    int our_nscale, try_remove;
    softfloat our_quant;

    av_assert0(peak_cb <= 0);
    av_assert0(peak_cb >= -2047);

    our_nscale = 127;
    peak = cb_to_level[-peak_cb];

    for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
        if (scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e <= 17)
            continue;
        our_quant.m = mul32(scalefactor_inv[our_nscale - try_remove].m, stepsize_inv[abits].m);
        our_quant.e = scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e - 17;
686
        if ((ff_dca_quant_levels[abits] - 1) / 2 < quantize_value(peak, our_quant))
Paul B Mahol's avatar
Paul B Mahol committed
687 688 689 690 691 692 693 694 695
            continue;
        our_nscale -= try_remove;
    }

    if (our_nscale >= 125)
        our_nscale = 124;

    quant->m = mul32(scalefactor_inv[our_nscale].m, stepsize_inv[abits].m);
    quant->e = scalefactor_inv[our_nscale].e + stepsize_inv[abits].e - 17;
696
    av_assert0((ff_dca_quant_levels[abits] - 1) / 2 >= quantize_value(peak, *quant));
Paul B Mahol's avatar
Paul B Mahol committed
697 698 699 700

    return our_nscale;
}

701
static void calc_scales(DCAEncContext *c)
Paul B Mahol's avatar
Paul B Mahol committed
702 703 704 705 706 707 708 709 710 711 712 713 714
{
    int band, ch;

    for (band = 0; band < 32; band++)
        for (ch = 0; ch < c->fullband_channels; ch++)
            c->scale_factor[band][ch] = calc_one_scale(c->peak_cb[band][ch],
                                                       c->abits[band][ch],
                                                       &c->quant[band][ch]);

    if (c->lfe_channel)
        c->lfe_scale_factor = calc_one_scale(c->lfe_peak_cb, 11, &c->lfe_quant);
}

715
static void quantize_all(DCAEncContext *c)
Paul B Mahol's avatar
Paul B Mahol committed
716 717 718 719 720 721 722
{
    int sample, band, ch;

    for (sample = 0; sample < SUBBAND_SAMPLES; sample++)
        for (band = 0; band < 32; band++)
            for (ch = 0; ch < c->fullband_channels; ch++)
                c->quantized[sample][band][ch] = quantize_value(c->subband[sample][band][ch], c->quant[band][ch]);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
723 724
}

725
static void put_frame_header(DCAEncContext *c)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
{
    /* SYNC */
    put_bits(&c->pb, 16, 0x7ffe);
    put_bits(&c->pb, 16, 0x8001);

    /* Frame type: normal */
    put_bits(&c->pb, 1, 1);

    /* Deficit sample count: none */
    put_bits(&c->pb, 5, 31);

    /* CRC is not present */
    put_bits(&c->pb, 1, 0);

    /* Number of PCM sample blocks */
Paul B Mahol's avatar
Paul B Mahol committed
741
    put_bits(&c->pb, 7, SUBBAND_SAMPLES - 1);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
742 743

    /* Primary frame byte size */
Paul B Mahol's avatar
Paul B Mahol committed
744
    put_bits(&c->pb, 14, c->frame_size - 1);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
745

Paul B Mahol's avatar
Paul B Mahol committed
746 747
    /* Audio channel arrangement */
    put_bits(&c->pb, 6, c->channel_config);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
748 749

    /* Core audio sampling frequency */
Paul B Mahol's avatar
Paul B Mahol committed
750
    put_bits(&c->pb, 4, bitstream_sfreq[c->samplerate_index]);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
751

Paul B Mahol's avatar
Paul B Mahol committed
752 753
    /* Transmission bit rate */
    put_bits(&c->pb, 5, c->bitrate_index);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

    /* Embedded down mix: disabled */
    put_bits(&c->pb, 1, 0);

    /* Embedded dynamic range flag: not present */
    put_bits(&c->pb, 1, 0);

    /* Embedded time stamp flag: not present */
    put_bits(&c->pb, 1, 0);

    /* Auxiliary data flag: not present */
    put_bits(&c->pb, 1, 0);

    /* HDCD source: no */
    put_bits(&c->pb, 1, 0);

    /* Extension audio ID: N/A */
    put_bits(&c->pb, 3, 0);

    /* Extended audio data: not present */
    put_bits(&c->pb, 1, 0);

    /* Audio sync word insertion flag: after each sub-frame */
    put_bits(&c->pb, 1, 0);

Paul B Mahol's avatar
Paul B Mahol committed
779 780
    /* Low frequency effects flag: not present or 64x subsampling */
    put_bits(&c->pb, 2, c->lfe_channel ? 2 : 0);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

    /* Predictor history switch flag: on */
    put_bits(&c->pb, 1, 1);

    /* No CRC */
    /* Multirate interpolator switch: non-perfect reconstruction */
    put_bits(&c->pb, 1, 0);

    /* Encoder software revision: 7 */
    put_bits(&c->pb, 4, 7);

    /* Copy history: 0 */
    put_bits(&c->pb, 2, 0);

    /* Source PCM resolution: 16 bits, not DTS ES */
    put_bits(&c->pb, 3, 0);

    /* Front sum/difference coding: no */
    put_bits(&c->pb, 1, 0);

    /* Surrounds sum/difference coding: no */
    put_bits(&c->pb, 1, 0);

    /* Dialog normalization: 0 dB */
    put_bits(&c->pb, 4, 0);
}

808
static void put_primary_audio_header(DCAEncContext *c)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
809 810 811 812 813 814 815 816 817
{
    static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
    static const int thr[11]    = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };

    int ch, i;
    /* Number of subframes */
    put_bits(&c->pb, 4, SUBFRAMES - 1);

    /* Number of primary audio channels */
Paul B Mahol's avatar
Paul B Mahol committed
818
    put_bits(&c->pb, 3, c->fullband_channels - 1);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
819 820

    /* Subband activity count */
Paul B Mahol's avatar
Paul B Mahol committed
821
    for (ch = 0; ch < c->fullband_channels; ch++)
822
        put_bits(&c->pb, 5, DCAENC_SUBBANDS - 2);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
823 824

    /* High frequency VQ start subband */
Paul B Mahol's avatar
Paul B Mahol committed
825
    for (ch = 0; ch < c->fullband_channels; ch++)
826
        put_bits(&c->pb, 5, DCAENC_SUBBANDS - 1);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
827 828

    /* Joint intensity coding index: 0, 0 */
Paul B Mahol's avatar
Paul B Mahol committed
829
    for (ch = 0; ch < c->fullband_channels; ch++)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
830 831 832
        put_bits(&c->pb, 3, 0);

    /* Transient mode codebook: A4, A4 (arbitrary) */
Paul B Mahol's avatar
Paul B Mahol committed
833
    for (ch = 0; ch < c->fullband_channels; ch++)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
834 835 836
        put_bits(&c->pb, 2, 0);

    /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
Paul B Mahol's avatar
Paul B Mahol committed
837
    for (ch = 0; ch < c->fullband_channels; ch++)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
838 839 840
        put_bits(&c->pb, 3, 6);

    /* Bit allocation quantizer select: linear 5-bit */
Paul B Mahol's avatar
Paul B Mahol committed
841
    for (ch = 0; ch < c->fullband_channels; ch++)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
842 843 844 845 846
        put_bits(&c->pb, 3, 6);

    /* Quantization index codebook select: dummy data
       to avoid transmission of scale factor adjustment */
    for (i = 1; i < 11; i++)
Paul B Mahol's avatar
Paul B Mahol committed
847
        for (ch = 0; ch < c->fullband_channels; ch++)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
848 849 850
            put_bits(&c->pb, bitlen[i], thr[i]);

    /* Scale factor adjustment index: not transmitted */
Paul B Mahol's avatar
Paul B Mahol committed
851
    /* Audio header CRC check word: not transmitted */
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
852 853
}

854
static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
855
{
Paul B Mahol's avatar
Paul B Mahol committed
856 857 858 859 860
    if (c->abits[band][ch] <= 7) {
        int sum, i, j;
        for (i = 0; i < 8; i += 4) {
            sum = 0;
            for (j = 3; j >= 0; j--) {
861
                sum *= ff_dca_quant_levels[c->abits[band][ch]];
Paul B Mahol's avatar
Paul B Mahol committed
862
                sum += c->quantized[ss * 8 + i + j][band][ch];
863
                sum += (ff_dca_quant_levels[c->abits[band][ch]] - 1) / 2;
Paul B Mahol's avatar
Paul B Mahol committed
864 865 866 867 868 869 870
            }
            put_bits(&c->pb, bit_consumption[c->abits[band][ch]] / 4, sum);
        }
    } else {
        int i;
        for (i = 0; i < 8; i++) {
            int bits = bit_consumption[c->abits[band][ch]] / 16;
James Almer's avatar
James Almer committed
871
            put_sbits(&c->pb, bits, c->quantized[ss * 8 + i][band][ch]);
Paul B Mahol's avatar
Paul B Mahol committed
872
        }
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
873 874 875
    }
}

876
static void put_subframe(DCAEncContext *c, int subframe)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
877
{
Paul B Mahol's avatar
Paul B Mahol committed
878
    int i, band, ss, ch;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
879 880 881 882 883 884 885 886

    /* Subsubframes count */
    put_bits(&c->pb, 2, SUBSUBFRAMES -1);

    /* Partial subsubframe sample count: dummy */
    put_bits(&c->pb, 3, 0);

    /* Prediction mode: no ADPCM, in each channel and subband */
Paul B Mahol's avatar
Paul B Mahol committed
887
    for (ch = 0; ch < c->fullband_channels; ch++)
888
        for (band = 0; band < DCAENC_SUBBANDS; band++)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
889 890
            put_bits(&c->pb, 1, 0);

Lou Logan's avatar
Lou Logan committed
891
    /* Prediction VQ address: not transmitted */
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
892
    /* Bit allocation index */
Paul B Mahol's avatar
Paul B Mahol committed
893
    for (ch = 0; ch < c->fullband_channels; ch++)
894
        for (band = 0; band < DCAENC_SUBBANDS; band++)
Paul B Mahol's avatar
Paul B Mahol committed
895
            put_bits(&c->pb, 5, c->abits[band][ch]);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
896 897 898

    if (SUBSUBFRAMES > 1) {
        /* Transition mode: none for each channel and subband */
Paul B Mahol's avatar
Paul B Mahol committed
899
        for (ch = 0; ch < c->fullband_channels; ch++)
900
            for (band = 0; band < DCAENC_SUBBANDS; band++)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
901 902 903
                put_bits(&c->pb, 1, 0); /* codebook A4 */
    }

Paul B Mahol's avatar
Paul B Mahol committed
904 905
    /* Scale factors */
    for (ch = 0; ch < c->fullband_channels; ch++)
906
        for (band = 0; band < DCAENC_SUBBANDS; band++)
Paul B Mahol's avatar
Paul B Mahol committed
907
            put_bits(&c->pb, 7, c->scale_factor[band][ch]);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
908 909 910 911 912 913 914 915

    /* Joint subband scale factor codebook select: not transmitted */
    /* Scale factors for joint subband coding: not transmitted */
    /* Stereo down-mix coefficients: not transmitted */
    /* Dynamic range coefficient: not transmitted */
    /* Stde information CRC check word: not transmitted */
    /* VQ encoded high frequency subbands: not transmitted */

Paul B Mahol's avatar
Paul B Mahol committed
916
    /* LFE data: 8 samples and scalefactor */
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
917
    if (c->lfe_channel) {
Paul B Mahol's avatar
Paul B Mahol committed
918 919
        for (i = 0; i < DCA_LFE_SAMPLES; i++)
            put_bits(&c->pb, 8, quantize_value(c->downsampled_lfe[i], c->lfe_quant) & 0xff);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
920 921 922 923 924
        put_bits(&c->pb, 8, c->lfe_scale_factor);
    }

    /* Audio data (subsubframes) */
    for (ss = 0; ss < SUBSUBFRAMES ; ss++)
Paul B Mahol's avatar
Paul B Mahol committed
925
        for (ch = 0; ch < c->fullband_channels; ch++)
926
            for (band = 0; band < DCAENC_SUBBANDS; band++)
Paul B Mahol's avatar
Paul B Mahol committed
927
                    put_subframe_samples(c, ss, band, ch);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
928 929 930 931 932

    /* DSYNC */
    put_bits(&c->pb, 16, 0xffff);
}

933 934
static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                        const AVFrame *frame, int *got_packet_ptr)
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
935
{
936
    DCAEncContext *c = avctx->priv_data;
Paul B Mahol's avatar
Paul B Mahol committed
937 938
    const int32_t *samples;
    int ret, i;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
939

940
    if ((ret = ff_alloc_packet2(avctx, avpkt, c->frame_size, 0)) < 0)
941 942
        return ret;

Paul B Mahol's avatar
Paul B Mahol committed
943
    samples = (const int32_t *)frame->data[0];
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
944

Paul B Mahol's avatar
Paul B Mahol committed
945 946 947
    subband_transform(c, samples);
    if (c->lfe_channel)
        lfe_downsample(c, samples);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
948

Paul B Mahol's avatar
Paul B Mahol committed
949 950 951 952 953 954
    calc_masking(c, samples);
    find_peaks(c);
    assign_bits(c);
    calc_scales(c);
    quantize_all(c);
    shift_history(c, samples);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
955

Paul B Mahol's avatar
Paul B Mahol committed
956 957 958 959 960
    init_put_bits(&c->pb, avpkt->data, avpkt->size);
    put_frame_header(c);
    put_primary_audio_header(c);
    for (i = 0; i < SUBFRAMES; i++)
        put_subframe(c, i);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
961

962 963 964 965

    for (i = put_bits_count(&c->pb); i < 8*c->frame_size; i++)
        put_bits(&c->pb, 1, 0);

Paul B Mahol's avatar
Paul B Mahol committed
966
    flush_put_bits(&c->pb);
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
967

Paul B Mahol's avatar
Paul B Mahol committed
968 969
    avpkt->pts      = frame->pts;
    avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
970
    avpkt->size     = put_bits_count(&c->pb) >> 3;
Paul B Mahol's avatar
Paul B Mahol committed
971
    *got_packet_ptr = 1;
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
972 973 974
    return 0;
}

Paul B Mahol's avatar
Paul B Mahol committed
975 976 977 978 979
static const AVCodecDefault defaults[] = {
    { "b",          "1411200" },
    { NULL },
};

Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
980
AVCodec ff_dca_encoder = {
Paul B Mahol's avatar
Paul B Mahol committed
981
    .name                  = "dca",
982
    .long_name             = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
Paul B Mahol's avatar
Paul B Mahol committed
983 984
    .type                  = AVMEDIA_TYPE_AUDIO,
    .id                    = AV_CODEC_ID_DTS,
985
    .priv_data_size        = sizeof(DCAEncContext),
Paul B Mahol's avatar
Paul B Mahol committed
986 987
    .init                  = encode_init,
    .encode2               = encode_frame,
988
    .capabilities          = AV_CODEC_CAP_EXPERIMENTAL,
Paul B Mahol's avatar
Paul B Mahol committed
989 990 991 992 993 994 995 996 997 998
    .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
                                                            AV_SAMPLE_FMT_NONE },
    .supported_samplerates = sample_rates,
    .channel_layouts       = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
                                                  AV_CH_LAYOUT_STEREO,
                                                  AV_CH_LAYOUT_2_2,
                                                  AV_CH_LAYOUT_5POINT0,
                                                  AV_CH_LAYOUT_5POINT1,
                                                  0 },
    .defaults              = defaults,
Alexander E. Patrakov's avatar
Alexander E. Patrakov committed
999
};