flacenc.c 40.3 KB
Newer Older
1 2
/**
 * FLAC audio encoder
3
 * Copyright (c) 2006  Justin Ruggles <justin.ruggles@gmail.com>
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * 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
18
 * License along with FFmpeg; if not, write to the Free Software
19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include "libavutil/crc.h"
23
#include "libavutil/md5.h"
24
#include "avcodec.h"
25
#include "get_bits.h"
26
#include "dsputil.h"
27
#include "golomb.h"
28
#include "lpc.h"
29
#include "flac.h"
30
#include "flacdata.h"
31 32 33 34 35 36

#define FLAC_SUBFRAME_CONSTANT  0
#define FLAC_SUBFRAME_VERBATIM  1
#define FLAC_SUBFRAME_FIXED     8
#define FLAC_SUBFRAME_LPC      32

37 38 39 40 41 42 43 44 45 46
#define MAX_FIXED_ORDER     4
#define MAX_PARTITION_ORDER 8
#define MAX_PARTITIONS     (1 << MAX_PARTITION_ORDER)
#define MAX_LPC_PRECISION  15
#define MAX_LPC_SHIFT      15
#define MAX_RICE_PARAM     14

typedef struct CompressionOptions {
    int compression_level;
    int block_time_ms;
47 48
    enum AVLPCType lpc_type;
    int lpc_passes;
49 50 51 52 53 54 55 56
    int lpc_coeff_precision;
    int min_prediction_order;
    int max_prediction_order;
    int prediction_order_method;
    int min_partition_order;
    int max_partition_order;
} CompressionOptions;

57 58
typedef struct RiceContext {
    int porder;
59
    int params[MAX_PARTITIONS];
60 61
} RiceContext;

62 63 64 65 66
typedef struct FlacSubframe {
    int type;
    int type_code;
    int obits;
    int order;
67 68
    int32_t coefs[MAX_LPC_ORDER];
    int shift;
69
    RiceContext rc;
70
    int32_t samples[FLAC_MAX_BLOCKSIZE];
71
    int32_t residual[FLAC_MAX_BLOCKSIZE+1];
72 73 74
} FlacSubframe;

typedef struct FlacFrame {
75
    FlacSubframe subframes[FLAC_MAX_CHANNELS];
76 77 78 79
    int blocksize;
    int bs_code[2];
    uint8_t crc8;
    int ch_mode;
80
    int verbatim_only;
81 82 83 84
} FlacFrame;

typedef struct FlacEncodeContext {
    PutBitContext pb;
85 86
    int channels;
    int samplerate;
87
    int sr_code[2];
88
    int max_blocksize;
89
    int min_framesize;
90
    int max_framesize;
91
    int max_encoded_framesize;
92
    uint32_t frame_count;
93
    uint64_t sample_count;
94
    uint8_t md5sum[16];
95
    FlacFrame frame;
96
    CompressionOptions options;
97
    AVCodecContext *avctx;
98
    DSPContext dsp;
99
    struct AVMD5 *md5ctx;
100 101
} FlacEncodeContext;

102

103
/**
104
 * Write streaminfo metadata block to byte array.
105 106 107 108 109 110 111 112 113
 */
static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
{
    PutBitContext pb;

    memset(header, 0, FLAC_STREAMINFO_SIZE);
    init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);

    /* streaminfo metadata block */
114 115
    put_bits(&pb, 16, s->max_blocksize);
    put_bits(&pb, 16, s->max_blocksize);
116
    put_bits(&pb, 24, s->min_framesize);
117 118 119 120
    put_bits(&pb, 24, s->max_framesize);
    put_bits(&pb, 20, s->samplerate);
    put_bits(&pb, 3, s->channels-1);
    put_bits(&pb, 5, 15);       /* bits per sample - 1 */
121 122 123
    /* write 36-bit sample count in 2 put_bits() calls */
    put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
    put_bits(&pb, 12,  s->sample_count & 0x000000FFFLL);
124
    flush_put_bits(&pb);
125
    memcpy(&header[18], s->md5sum, 16);
126 127
}

128

129
/**
130 131
 * Set blocksize based on samplerate.
 * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
132
 */
133
static int select_blocksize(int samplerate, int block_time_ms)
134 135 136 137 138 139
{
    int i;
    int target;
    int blocksize;

    assert(samplerate > 0);
140
    blocksize = ff_flac_blocksize_table[1];
141 142 143 144
    target    = (samplerate * block_time_ms) / 1000;
    for (i = 0; i < 16; i++) {
        if (target >= ff_flac_blocksize_table[i] &&
            ff_flac_blocksize_table[i] > blocksize) {
145
            blocksize = ff_flac_blocksize_table[i];
146 147 148 149 150
        }
    }
    return blocksize;
}

151

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
static av_cold void dprint_compression_options(FlacEncodeContext *s)
{
    AVCodecContext     *avctx = s->avctx;
    CompressionOptions *opt   = &s->options;

    av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);

    switch (opt->lpc_type) {
    case AV_LPC_TYPE_NONE:
        av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
        break;
    case AV_LPC_TYPE_FIXED:
        av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
        break;
    case AV_LPC_TYPE_LEVINSON:
        av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
        break;
    case AV_LPC_TYPE_CHOLESKY:
        av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
               opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
        break;
    }

    av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
           opt->min_prediction_order, opt->max_prediction_order);

    switch (opt->prediction_order_method) {
    case ORDER_METHOD_EST:
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
        break;
    case ORDER_METHOD_2LEVEL:
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
        break;
    case ORDER_METHOD_4LEVEL:
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
        break;
    case ORDER_METHOD_8LEVEL:
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
        break;
    case ORDER_METHOD_SEARCH:
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
        break;
    case ORDER_METHOD_LOG:
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
        break;
    }


    av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
           opt->min_partition_order, opt->max_partition_order);

    av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);

    av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
           opt->lpc_coeff_precision);
}


210
static av_cold int flac_encode_init(AVCodecContext *avctx)
211 212 213 214
{
    int freq = avctx->sample_rate;
    int channels = avctx->channels;
    FlacEncodeContext *s = avctx->priv_data;
Michael Niedermayer's avatar
Michael Niedermayer committed
215
    int i, level;
216 217
    uint8_t *streaminfo;

218 219
    s->avctx = avctx;

220 221
    dsputil_init(&s->dsp, avctx);

222
    if (avctx->sample_fmt != SAMPLE_FMT_S16)
223 224
        return -1;

225
    if (channels < 1 || channels > FLAC_MAX_CHANNELS)
226 227 228 229
        return -1;
    s->channels = channels;

    /* find samplerate in table */
230
    if (freq < 1)
231
        return -1;
232 233
    for (i = 4; i < 12; i++) {
        if (freq == ff_flac_sample_rate_table[i]) {
234
            s->samplerate = ff_flac_sample_rate_table[i];
235 236 237 238 239 240
            s->sr_code[0] = i;
            s->sr_code[1] = 0;
            break;
        }
    }
    /* if not in table, samplerate is non-standard */
241 242
    if (i == 12) {
        if (freq % 1000 == 0 && freq < 255000) {
243 244
            s->sr_code[0] = 12;
            s->sr_code[1] = freq / 1000;
245
        } else if (freq % 10 == 0 && freq < 655350) {
246 247
            s->sr_code[0] = 14;
            s->sr_code[1] = freq / 10;
248
        } else if (freq < 65535) {
249 250 251 252 253 254 255 256
            s->sr_code[0] = 13;
            s->sr_code[1] = freq;
        } else {
            return -1;
        }
        s->samplerate = freq;
    }

257
    /* set compression option defaults based on avctx->compression_level */
258
    if (avctx->compression_level < 0)
259
        s->options.compression_level = 5;
260
    else
261 262
        s->options.compression_level = avctx->compression_level;

263 264
    level = s->options.compression_level;
    if (level > 12) {
265 266 267 268 269
        av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
               s->options.compression_level);
        return -1;
    }

270 271 272 273 274 275 276 277 278 279 280
    s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];

    s->options.lpc_type      = ((int[]){ AV_LPC_TYPE_FIXED,    AV_LPC_TYPE_FIXED,    AV_LPC_TYPE_FIXED,
                                         AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON,
                                         AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON,
                                         AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON,
                                         AV_LPC_TYPE_LEVINSON})[level];

    s->options.min_prediction_order = ((int[]){  2,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1})[level];
    s->options.max_prediction_order = ((int[]){  3,  4,  4,  6,  8,  8,  8,  8, 12, 12, 12, 32, 32})[level];

281 282
    s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST,    ORDER_METHOD_EST,    ORDER_METHOD_EST,
                                                   ORDER_METHOD_EST,    ORDER_METHOD_EST,    ORDER_METHOD_EST,
283 284
                                                   ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG,    ORDER_METHOD_4LEVEL,
                                                   ORDER_METHOD_LOG,    ORDER_METHOD_SEARCH, ORDER_METHOD_LOG,
285
                                                   ORDER_METHOD_SEARCH})[level];
286

287 288
    s->options.min_partition_order = ((int[]){  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0})[level];
    s->options.max_partition_order = ((int[]){  2,  2,  3,  3,  3,  8,  8,  8,  8,  8,  8,  8,  8})[level];
Michael Niedermayer's avatar
Michael Niedermayer committed
289

290
    /* set compression option overrides from AVCodecContext */
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
#if LIBAVCODEC_VERSION_MAJOR < 53
    /* for compatibility with deprecated AVCodecContext.use_lpc */
    if (avctx->use_lpc == 0) {
        s->options.lpc_type = AV_LPC_TYPE_FIXED;
    } else if (avctx->use_lpc == 1) {
        s->options.lpc_type = AV_LPC_TYPE_LEVINSON;
    } else if (avctx->use_lpc > 1) {
        s->options.lpc_type   = AV_LPC_TYPE_CHOLESKY;
        s->options.lpc_passes = avctx->use_lpc - 1;
    }
#endif
    if (avctx->lpc_type > AV_LPC_TYPE_DEFAULT) {
        if (avctx->lpc_type > AV_LPC_TYPE_CHOLESKY) {
            av_log(avctx, AV_LOG_ERROR, "unknown lpc type: %d\n", avctx->lpc_type);
            return -1;
        }
        s->options.lpc_type = avctx->lpc_type;
        if (s->options.lpc_type == AV_LPC_TYPE_CHOLESKY) {
            if (avctx->lpc_passes < 0) {
                // default number of passes for Cholesky
                s->options.lpc_passes = 2;
            } else if (avctx->lpc_passes == 0) {
                av_log(avctx, AV_LOG_ERROR, "invalid number of lpc passes: %d\n",
                       avctx->lpc_passes);
315
                return -1;
316 317
            } else {
                s->options.lpc_passes = avctx->lpc_passes;
318
            }
319 320 321 322 323 324 325
        }
    }

    if (s->options.lpc_type == AV_LPC_TYPE_NONE) {
        s->options.min_prediction_order = 0;
    } else if (avctx->min_prediction_order >= 0) {
        if (s->options.lpc_type == AV_LPC_TYPE_FIXED) {
326
            if (avctx->min_prediction_order > MAX_FIXED_ORDER) {
327 328 329 330
                av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
                       avctx->min_prediction_order);
                return -1;
            }
331 332
        } else if (avctx->min_prediction_order < MIN_LPC_ORDER ||
                   avctx->min_prediction_order > MAX_LPC_ORDER) {
333 334 335
            av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
                   avctx->min_prediction_order);
            return -1;
336 337 338
        }
        s->options.min_prediction_order = avctx->min_prediction_order;
    }
339 340 341 342
    if (s->options.lpc_type == AV_LPC_TYPE_NONE) {
        s->options.max_prediction_order = 0;
    } else if (avctx->max_prediction_order >= 0) {
        if (s->options.lpc_type == AV_LPC_TYPE_FIXED) {
343
            if (avctx->max_prediction_order > MAX_FIXED_ORDER) {
344 345 346 347
                av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
                       avctx->max_prediction_order);
                return -1;
            }
348 349 350 351 352
        } else if (avctx->max_prediction_order < MIN_LPC_ORDER ||
                   avctx->max_prediction_order > MAX_LPC_ORDER) {
            av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
                   avctx->max_prediction_order);
            return -1;
353 354 355
        }
        s->options.max_prediction_order = avctx->max_prediction_order;
    }
356
    if (s->options.max_prediction_order < s->options.min_prediction_order) {
357 358 359 360 361
        av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
               s->options.min_prediction_order, s->options.max_prediction_order);
        return -1;
    }

362 363
    if (avctx->prediction_order_method >= 0) {
        if (avctx->prediction_order_method > ORDER_METHOD_LOG) {
364 365 366 367 368 369 370
            av_log(avctx, AV_LOG_ERROR, "invalid prediction order method: %d\n",
                   avctx->prediction_order_method);
            return -1;
        }
        s->options.prediction_order_method = avctx->prediction_order_method;
    }

371 372
    if (avctx->min_partition_order >= 0) {
        if (avctx->min_partition_order > MAX_PARTITION_ORDER) {
373 374 375 376 377 378
            av_log(avctx, AV_LOG_ERROR, "invalid min partition order: %d\n",
                   avctx->min_partition_order);
            return -1;
        }
        s->options.min_partition_order = avctx->min_partition_order;
    }
379 380
    if (avctx->max_partition_order >= 0) {
        if (avctx->max_partition_order > MAX_PARTITION_ORDER) {
381 382 383 384 385 386
            av_log(avctx, AV_LOG_ERROR, "invalid max partition order: %d\n",
                   avctx->max_partition_order);
            return -1;
        }
        s->options.max_partition_order = avctx->max_partition_order;
    }
387
    if (s->options.max_partition_order < s->options.min_partition_order) {
388 389 390 391 392
        av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
               s->options.min_partition_order, s->options.max_partition_order);
        return -1;
    }

393 394
    if (avctx->frame_size > 0) {
        if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
395
                avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
396 397 398 399 400
            av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
                   avctx->frame_size);
            return -1;
        }
    } else {
401
        s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
402
    }
403
    s->max_blocksize = s->avctx->frame_size;
404 405

    /* set LPC precision */
406 407
    if (avctx->lpc_coeff_precision > 0) {
        if (avctx->lpc_coeff_precision > MAX_LPC_PRECISION) {
408 409 410 411 412 413
            av_log(avctx, AV_LOG_ERROR, "invalid lpc coeff precision: %d\n",
                   avctx->lpc_coeff_precision);
            return -1;
        }
        s->options.lpc_coeff_precision = avctx->lpc_coeff_precision;
    } else {
414 415
        /* default LPC precision */
        s->options.lpc_coeff_precision = 15;
416
    }
417

418
    /* set maximum encoded frame size in verbatim mode */
419 420
    s->max_framesize = ff_flac_get_max_frame_size(s->avctx->frame_size,
                                                  s->channels, 16);
421

422 423
    /* initialize MD5 context */
    s->md5ctx = av_malloc(av_md5_size);
424
    if (!s->md5ctx)
425
        return AVERROR(ENOMEM);
426 427
    av_md5_init(s->md5ctx);

428
    streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
429 430
    if (!streaminfo)
        return AVERROR(ENOMEM);
431 432 433 434
    write_streaminfo(s, streaminfo);
    avctx->extradata = streaminfo;
    avctx->extradata_size = FLAC_STREAMINFO_SIZE;

435
    s->frame_count   = 0;
436
    s->min_framesize = s->max_framesize;
437 438

    avctx->coded_frame = avcodec_alloc_frame();
439 440
    if (!avctx->coded_frame)
        return AVERROR(ENOMEM);
441

442 443
    dprint_compression_options(s);

444 445 446
    return 0;
}

447

448
static void init_frame(FlacEncodeContext *s)
449 450 451 452 453 454
{
    int i, ch;
    FlacFrame *frame;

    frame = &s->frame;

455 456 457
    for (i = 0; i < 16; i++) {
        if (s->avctx->frame_size == ff_flac_blocksize_table[i]) {
            frame->blocksize  = ff_flac_blocksize_table[i];
458 459 460 461 462
            frame->bs_code[0] = i;
            frame->bs_code[1] = 0;
            break;
        }
    }
463
    if (i == 16) {
464
        frame->blocksize = s->avctx->frame_size;
465
        if (frame->blocksize <= 256) {
466 467 468 469 470 471 472 473
            frame->bs_code[0] = 6;
            frame->bs_code[1] = frame->blocksize-1;
        } else {
            frame->bs_code[0] = 7;
            frame->bs_code[1] = frame->blocksize-1;
        }
    }

474
    for (ch = 0; ch < s->channels; ch++)
475
        frame->subframes[ch].obits = 16;
476 477

    frame->verbatim_only = 0;
478 479
}

480

481
/**
482
 * Copy channel-interleaved input samples into separate subframes.
483
 */
484
static void copy_samples(FlacEncodeContext *s, const int16_t *samples)
485 486 487 488 489
{
    int i, j, ch;
    FlacFrame *frame;

    frame = &s->frame;
490 491
    for (i = 0, j = 0; i < frame->blocksize; i++)
        for (ch = 0; ch < s->channels; ch++, j++)
492 493 494
            frame->subframes[ch].samples[i] = samples[j];
}

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 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
static int rice_count_exact(int32_t *res, int n, int k)
{
    int i;
    int count = 0;

    for (i = 0; i < n; i++) {
        int32_t v = -2 * res[i] - 1;
        v ^= v >> 31;
        count += (v >> k) + 1 + k;
    }
    return count;
}


static int subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
                                int pred_order)
{
    int p, porder, psize;
    int i, part_end;
    int count = 0;

    /* subframe header */
    count += 8;

    /* subframe */
    if (sub->type == FLAC_SUBFRAME_CONSTANT) {
        count += sub->obits;
    } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
        count += s->frame.blocksize * sub->obits;
    } else {
        /* warm-up samples */
        count += pred_order * sub->obits;

        /* LPC coefficients */
        if (sub->type == FLAC_SUBFRAME_LPC)
            count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;

        /* rice-encoded block */
        count += 2;

        /* partition order */
        porder = sub->rc.porder;
        psize  = s->frame.blocksize >> porder;
        count += 4;

        /* residual */
        i        = pred_order;
        part_end = psize;
        for (p = 0; p < 1 << porder; p++) {
            int k = sub->rc.params[p];
            count += 4;
            count += rice_count_exact(&sub->residual[i], part_end - i, k);
            i = part_end;
            part_end = FFMIN(s->frame.blocksize, part_end + psize);
        }
    }

    return count;
}


557 558
#define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))

559
/**
560
 * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
561
 */
562 563
static int find_optimal_param(uint32_t sum, int n)
{
564 565 566
    int k;
    uint32_t sum2;

567
    if (sum <= n >> 1)
568
        return 0;
569 570
    sum2 = sum - (n >> 1);
    k    = av_log2(n < 256 ? FASTDIV(sum2, n) : sum2 / n);
571
    return FFMIN(k, MAX_RICE_PARAM);
572 573
}

574

575 576 577 578 579 580 581
static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
                                         uint32_t *sums, int n, int pred_order)
{
    int i;
    int k, cnt, part;
    uint32_t all_bits;

582
    part     = (1 << porder);
583
    all_bits = 4 * part;
584 585

    cnt = (n >> porder) - pred_order;
586
    for (i = 0; i < part; i++) {
587 588 589
        k = find_optimal_param(sums[i], cnt);
        rc->params[i] = k;
        all_bits += rice_encode_count(sums[i], cnt, k);
590
        cnt = n >> porder;
591 592 593 594 595 596 597
    }

    rc->porder = porder;

    return all_bits;
}

598

599 600
static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
                      uint32_t sums[][MAX_PARTITIONS])
601 602
{
    int i, j;
Michael Niedermayer's avatar
Michael Niedermayer committed
603 604
    int parts;
    uint32_t *res, *res_end;
605 606

    /* sums for highest level */
607 608
    parts   = (1 << pmax);
    res     = &data[pred_order];
Michael Niedermayer's avatar
Michael Niedermayer committed
609
    res_end = &data[n >> pmax];
610
    for (i = 0; i < parts; i++) {
611
        uint32_t sum = 0;
612
        while (res < res_end)
613 614
            sum += *(res++);
        sums[pmax][i] = sum;
615
        res_end += n >> pmax;
616 617
    }
    /* sums for lower levels */
618
    for (i = pmax - 1; i >= pmin; i--) {
619
        parts = (1 << i);
620
        for (j = 0; j < parts; j++)
621 622 623 624
            sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
    }
}

625

626 627
static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
                                 int32_t *data, int n, int pred_order)
628 629
{
    int i;
630
    uint32_t bits[MAX_PARTITION_ORDER+1];
631
    int opt_porder;
632
    RiceContext tmp_rc;
633
    uint32_t *udata;
634
    uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
635

636 637 638
    assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
    assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
    assert(pmin <= pmax);
639 640

    udata = av_malloc(n * sizeof(uint32_t));
641
    for (i = 0; i < n; i++)
642 643
        udata[i] = (2*data[i]) ^ (data[i]>>31);

644
    calc_sums(pmin, pmax, udata, n, pred_order, sums);
645

646 647
    opt_porder = pmin;
    bits[pmin] = UINT32_MAX;
648
    for (i = pmin; i <= pmax; i++) {
649
        bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
650
        if (bits[i] <= bits[opt_porder]) {
651
            opt_porder = i;
652
            *rc = tmp_rc;
653 654 655 656
        }
    }

    av_freep(&udata);
657
    return bits[opt_porder];
658 659
}

660

661 662 663
static int get_max_p_order(int max_porder, int n, int order)
{
    int porder = FFMIN(max_porder, av_log2(n^(n-1)));
664
    if (order > 0)
665 666 667 668
        porder = FFMIN(porder, av_log2(n/order));
    return porder;
}

669

670
static uint32_t find_subframe_rice_params(FlacEncodeContext *s,
671
                                          FlacSubframe *sub, int pred_order)
672
{
673 674 675 676 677 678 679 680 681 682
    int pmin = get_max_p_order(s->options.min_partition_order,
                               s->frame.blocksize, pred_order);
    int pmax = get_max_p_order(s->options.max_partition_order,
                               s->frame.blocksize, pred_order);

    uint32_t bits = 8 + pred_order * sub->obits + 2 + 4;
    if (sub->type == FLAC_SUBFRAME_LPC)
        bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
    bits += calc_rice_params(&sub->rc, pmin, pmax, sub->residual,
                             s->frame.blocksize, pred_order);
683 684 685
    return bits;
}

686

687 688
static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
                                  int order)
689 690 691
{
    int i;

692
    for (i = 0; i < order; i++)
693 694
        res[i] = smp[i];

695 696 697 698 699 700 701
    if (order == 0) {
        for (i = order; i < n; i++)
            res[i] = smp[i];
    } else if (order == 1) {
        for (i = order; i < n; i++)
            res[i] = smp[i] - smp[i-1];
    } else if (order == 2) {
702
        int a = smp[order-1] - smp[order-2];
703 704 705 706 707
        for (i = order; i < n; i += 2) {
            int b    = smp[i  ] - smp[i-1];
            res[i]   = b - a;
            a        = smp[i+1] - smp[i  ];
            res[i+1] = a - b;
708
        }
709 710
    } else if (order == 3) {
        int a = smp[order-1] -   smp[order-2];
711
        int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
712 713 714 715 716 717 718
        for (i = order; i < n; i += 2) {
            int b    = smp[i  ] - smp[i-1];
            int d    = b - a;
            res[i]   = d - c;
            a        = smp[i+1] - smp[i  ];
            c        = a - b;
            res[i+1] = c - d;
719
        }
720 721 722
    } else {
        int a = smp[order-1] -   smp[order-2];
        int c = smp[order-1] - 2*smp[order-2] +   smp[order-3];
723
        int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
724 725 726 727 728 729 730 731 732
        for (i = order; i < n; i += 2) {
            int b    = smp[i  ] - smp[i-1];
            int d    = b - a;
            int f    = d - c;
            res[i  ] = f - e;
            a        = smp[i+1] - smp[i  ];
            c        = a - b;
            e        = c - d;
            res[i+1] = e - f;
733
        }
734 735 736
    }
}

737

738
#define LPC1(x) {\
Loren Merritt's avatar
Loren Merritt committed
739
    int c = coefs[(x)-1];\
740 741 742
    p0   += c * s;\
    s     = smp[i-(x)+1];\
    p1   += c * s;\
743 744
}

745 746 747
static av_always_inline void encode_residual_lpc_unrolled(int32_t *res,
                                    const int32_t *smp, int n, int order,
                                    const int32_t *coefs, int shift, int big)
748 749
{
    int i;
750 751
    for (i = order; i < n; i += 2) {
        int s  = smp[i-order];
Loren Merritt's avatar
Loren Merritt committed
752
        int p0 = 0, p1 = 0;
753 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 779 780 781 782 783 784 785 786
        if (big) {
            switch (order) {
            case 32: LPC1(32)
            case 31: LPC1(31)
            case 30: LPC1(30)
            case 29: LPC1(29)
            case 28: LPC1(28)
            case 27: LPC1(27)
            case 26: LPC1(26)
            case 25: LPC1(25)
            case 24: LPC1(24)
            case 23: LPC1(23)
            case 22: LPC1(22)
            case 21: LPC1(21)
            case 20: LPC1(20)
            case 19: LPC1(19)
            case 18: LPC1(18)
            case 17: LPC1(17)
            case 16: LPC1(16)
            case 15: LPC1(15)
            case 14: LPC1(14)
            case 13: LPC1(13)
            case 12: LPC1(12)
            case 11: LPC1(11)
            case 10: LPC1(10)
            case  9: LPC1( 9)
                     LPC1( 8)
                     LPC1( 7)
                     LPC1( 6)
                     LPC1( 5)
                     LPC1( 4)
                     LPC1( 3)
                     LPC1( 2)
                     LPC1( 1)
787 788
            }
        } else {
789 790 791 792 793 794 795 796 797
            switch (order) {
            case  8: LPC1( 8)
            case  7: LPC1( 7)
            case  6: LPC1( 6)
            case  5: LPC1( 5)
            case  4: LPC1( 4)
            case  3: LPC1( 3)
            case  2: LPC1( 2)
            case  1: LPC1( 1)
798 799 800 801 802 803 804
            }
        }
        res[i  ] = smp[i  ] - (p0 >> shift);
        res[i+1] = smp[i+1] - (p1 >> shift);
    }
}

805

806 807 808
static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
                                int order, const int32_t *coefs, int shift)
{
809
    int i;
810
    for (i = 0; i < order; i++)
811
        res[i] = smp[i];
812
#if CONFIG_SMALL
813
    for (i = order; i < n; i += 2) {
814
        int j;
815
        int s  = smp[i];
Loren Merritt's avatar
Loren Merritt committed
816
        int p0 = 0, p1 = 0;
817
        for (j = 0; j < order; j++) {
Loren Merritt's avatar
Loren Merritt committed
818
            int c = coefs[j];
819 820 821
            p1   += c * s;
            s     = smp[i-j-1];
            p0   += c * s;
822
        }
Loren Merritt's avatar
Loren Merritt committed
823
        res[i  ] = smp[i  ] - (p0 >> shift);
824
        res[i+1] = smp[i+1] - (p1 >> shift);
825
    }
826
#else
827 828 829 830 831 832 833 834 835 836
    switch (order) {
    case  1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
    case  2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
    case  3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
    case  4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
    case  5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
    case  6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
    case  7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
    case  8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
    default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
837 838
    }
#endif
839 840
}

841

842
static int encode_residual_ch(FlacEncodeContext *s, int ch)
843
{
844
    int i, n;
845
    int min_order, max_order, opt_order, omethod;
846 847
    FlacFrame *frame;
    FlacSubframe *sub;
848 849
    int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
    int shift[MAX_LPC_ORDER];
850 851
    int32_t *res, *smp;

852
    frame = &s->frame;
853 854 855 856
    sub   = &frame->subframes[ch];
    res   = sub->residual;
    smp   = sub->samples;
    n     = frame->blocksize;
857 858

    /* CONSTANT */
859 860 861 862
    for (i = 1; i < n; i++)
        if(smp[i] != smp[0])
            break;
    if (i == n) {
863 864
        sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
        res[0] = smp[0];
865
        return subframe_count_exact(s, sub, 0);
866 867 868
    }

    /* VERBATIM */
869
    if (frame->verbatim_only || n < 5) {
870
        sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
871
        memcpy(res, smp, n * sizeof(int32_t));
872
        return subframe_count_exact(s, sub, 0);
873 874
    }

875 876 877
    min_order  = s->options.min_prediction_order;
    max_order  = s->options.max_prediction_order;
    omethod    = s->options.prediction_order_method;
878 879

    /* FIXED */
880
    sub->type = FLAC_SUBFRAME_FIXED;
881 882
    if (s->options.lpc_type == AV_LPC_TYPE_NONE  ||
        s->options.lpc_type == AV_LPC_TYPE_FIXED || n <= max_order) {
883
        uint32_t bits[MAX_FIXED_ORDER+1];
884 885
        if (max_order > MAX_FIXED_ORDER)
            max_order = MAX_FIXED_ORDER;
886
        opt_order = 0;
887 888
        bits[0]   = UINT32_MAX;
        for (i = min_order; i <= max_order; i++) {
889
            encode_residual_fixed(res, smp, n, i);
890
            bits[i] = find_subframe_rice_params(s, sub, i);
891
            if (bits[i] < bits[opt_order])
892
                opt_order = i;
893
        }
894
        sub->order     = opt_order;
895
        sub->type_code = sub->type | sub->order;
896
        if (sub->order != max_order) {
897
            encode_residual_fixed(res, smp, n, sub->order);
898
            find_subframe_rice_params(s, sub, sub->order);
899
        }
900
        return subframe_count_exact(s, sub, sub->order);
901
    }
902 903

    /* LPC */
904
    sub->type = FLAC_SUBFRAME_LPC;
905
    opt_order = ff_lpc_calc_coefs(&s->dsp, smp, n, min_order, max_order,
906
                                  s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
907
                                  s->options.lpc_passes, omethod,
908
                                  MAX_LPC_SHIFT, 0);
909

910 911 912
    if (omethod == ORDER_METHOD_2LEVEL ||
        omethod == ORDER_METHOD_4LEVEL ||
        omethod == ORDER_METHOD_8LEVEL) {
913
        int levels = 1 << omethod;
914
        uint32_t bits[1 << ORDER_METHOD_8LEVEL];
915
        int order;
916 917
        int opt_index   = levels-1;
        opt_order       = max_order-1;
918
        bits[opt_index] = UINT32_MAX;
919
        for (i = levels-1; i >= 0; i--) {
920
            order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
921 922
            if (order < 0)
                order = 0;
923
            encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
924
            bits[i] = find_subframe_rice_params(s, sub, order+1);
925
            if (bits[i] < bits[opt_index]) {
926 927 928 929 930
                opt_index = i;
                opt_order = order;
            }
        }
        opt_order++;
931
    } else if (omethod == ORDER_METHOD_SEARCH) {
932 933 934
        // brute-force optimal order search
        uint32_t bits[MAX_LPC_ORDER];
        opt_order = 0;
935 936
        bits[0]   = UINT32_MAX;
        for (i = min_order-1; i < max_order; i++) {
937
            encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
938
            bits[i] = find_subframe_rice_params(s, sub, i+1);
939
            if (bits[i] < bits[opt_order])
940 941 942
                opt_order = i;
        }
        opt_order++;
943
    } else if (omethod == ORDER_METHOD_LOG) {
944 945 946
        uint32_t bits[MAX_LPC_ORDER];
        int step;

947
        opt_order = min_order - 1 + (max_order-min_order)/3;
948 949
        memset(bits, -1, sizeof(bits));

950 951 952 953
        for (step = 16; step; step >>= 1) {
            int last = opt_order;
            for (i = last-step; i <= last+step; i += step) {
                if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
954 955
                    continue;
                encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
956
                bits[i] = find_subframe_rice_params(s, sub, i+1);
957 958
                if (bits[i] < bits[opt_order])
                    opt_order = i;
959 960 961
            }
        }
        opt_order++;
962 963
    }

964
    sub->order     = opt_order;
965
    sub->type_code = sub->type | (sub->order-1);
966 967
    sub->shift     = shift[sub->order-1];
    for (i = 0; i < sub->order; i++)
968
        sub->coefs[i] = coefs[sub->order-1][i];
969

970
    encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
971

972 973 974
    find_subframe_rice_params(s, sub, sub->order);

    return subframe_count_exact(s, sub, sub->order);
975 976
}

977

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
static int count_frame_header(FlacEncodeContext *s)
{
    uint8_t tmp;
    int count;

    /*
    <14> Sync code
    <1>  Reserved
    <1>  Blocking strategy
    <4>  Block size in inter-channel samples
    <4>  Sample rate
    <4>  Channel assignment
    <3>  Sample size in bits
    <1>  Reserved
    */
    count = 32;

    /* coded frame number */
    PUT_UTF8(s->frame_count, tmp, count += 8;)

    /* explicit block size */
999 1000 1001 1002
    if (s->frame.bs_code[0] == 6)
        count += 8;
    else if (s->frame.bs_code[0] == 7)
        count += 16;
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

    /* explicit sample rate */
    count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12)) * 8;

    /* frame header CRC-8 */
    count += 8;

    return count;
}


static int encode_frame(FlacEncodeContext *s)
{
    int ch, count;

    count = count_frame_header(s);

    for (ch = 0; ch < s->channels; ch++)
        count += encode_residual_ch(s, ch);

    count += (8 - (count & 7)) & 7; // byte alignment
    count += 16;                    // CRC-16

    return count >> 3;
}


1030 1031 1032 1033
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
{
    int i, best;
    int32_t lt, rt;
1034
    uint64_t sum[4];
1035
    uint64_t score[4];
1036
    int k;
1037

1038
    /* calculate sum of 2nd order residual for each channel */
1039
    sum[0] = sum[1] = sum[2] = sum[3] = 0;
1040 1041
    for (i = 2; i < n; i++) {
        lt = left_ch[i]  - 2*left_ch[i-1]  + left_ch[i-2];
1042
        rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
1043 1044 1045 1046
        sum[2] += FFABS((lt + rt) >> 1);
        sum[3] += FFABS(lt - rt);
        sum[0] += FFABS(lt);
        sum[1] += FFABS(rt);
1047
    }
1048
    /* estimate bit counts */
1049 1050 1051
    for (i = 0; i < 4; i++) {
        k      = find_optimal_param(2 * sum[i], n);
        sum[i] = rice_encode_count( 2 * sum[i], n, k);
1052 1053 1054
    }

    /* calculate score for each mode */
1055 1056 1057 1058
    score[0] = sum[0] + sum[1];
    score[1] = sum[0] + sum[3];
    score[2] = sum[1] + sum[3];
    score[3] = sum[2] + sum[3];
1059 1060 1061

    /* return mode with lowest score */
    best = 0;
1062 1063
    for (i = 1; i < 4; i++)
        if (score[i] < score[best])
1064
            best = i;
1065
    if (best == 0) {
1066
        return FLAC_CHMODE_INDEPENDENT;
1067
    } else if (best == 1) {
1068
        return FLAC_CHMODE_LEFT_SIDE;
1069
    } else if (best == 2) {
1070 1071 1072 1073 1074 1075
        return FLAC_CHMODE_RIGHT_SIDE;
    } else {
        return FLAC_CHMODE_MID_SIDE;
    }
}

1076

1077
/**
1078
 * Perform stereo channel decorrelation.
1079
 */
1080
static void channel_decorrelation(FlacEncodeContext *s)
1081 1082 1083 1084 1085
{
    FlacFrame *frame;
    int32_t *left, *right;
    int i, n;

1086
    frame = &s->frame;
1087
    n     = frame->blocksize;
1088 1089 1090
    left  = frame->subframes[0].samples;
    right = frame->subframes[1].samples;

1091
    if (s->channels != 2) {
1092
        frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
1093 1094 1095 1096 1097 1098
        return;
    }

    frame->ch_mode = estimate_stereo_mode(left, right, n);

    /* perform decorrelation and adjust bits-per-sample */
1099
    if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1100
        return;
1101
    if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1102
        int32_t tmp;
1103 1104 1105 1106
        for (i = 0; i < n; i++) {
            tmp      = left[i];
            left[i]  = (tmp + right[i]) >> 1;
            right[i] =  tmp - right[i];
1107 1108
        }
        frame->subframes[1].obits++;
1109 1110
    } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
        for (i = 0; i < n; i++)
1111 1112 1113
            right[i] = left[i] - right[i];
        frame->subframes[1].obits++;
    } else {
1114
        for (i = 0; i < n; i++)
1115 1116 1117 1118 1119
            left[i] -= right[i];
        frame->subframes[0].obits++;
    }
}

1120

1121
static void write_utf8(PutBitContext *pb, uint32_t val)
1122
{
1123 1124
    uint8_t tmp;
    PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1125 1126
}

1127

1128
static void write_frame_header(FlacEncodeContext *s)
1129 1130 1131 1132 1133 1134 1135 1136 1137
{
    FlacFrame *frame;
    int crc;

    frame = &s->frame;

    put_bits(&s->pb, 16, 0xFFF8);
    put_bits(&s->pb, 4, frame->bs_code[0]);
    put_bits(&s->pb, 4, s->sr_code[0]);
1138 1139

    if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1140
        put_bits(&s->pb, 4, s->channels-1);
1141
    else
1142
        put_bits(&s->pb, 4, frame->ch_mode);
1143

1144 1145 1146
    put_bits(&s->pb, 3, 4); /* bits-per-sample code */
    put_bits(&s->pb, 1, 0);
    write_utf8(&s->pb, s->frame_count);
1147 1148

    if (frame->bs_code[0] == 6)
1149
        put_bits(&s->pb, 8, frame->bs_code[1]);
1150
    else if (frame->bs_code[0] == 7)
1151
        put_bits(&s->pb, 16, frame->bs_code[1]);
1152 1153

    if (s->sr_code[0] == 12)
1154
        put_bits(&s->pb, 8, s->sr_code[1]);
1155
    else if (s->sr_code[0] > 12)
1156
        put_bits(&s->pb, 16, s->sr_code[1]);
1157

1158
    flush_put_bits(&s->pb);
1159 1160
    crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
                 put_bits_count(&s->pb) >> 3);
1161 1162 1163
    put_bits(&s->pb, 8, crc);
}

1164

1165
static void write_subframes(FlacEncodeContext *s)
1166
{
1167 1168 1169 1170
    int ch;

    for (ch = 0; ch < s->channels; ch++) {
        FlacSubframe *sub = &s->frame.subframes[ch];
Justin Ruggles's avatar
Justin Ruggles committed
1171 1172 1173 1174
        int i, p, porder, psize;
        int32_t *part_end;
        int32_t *res       =  sub->residual;
        int32_t *frame_end = &sub->residual[s->frame.blocksize];
1175

1176 1177 1178 1179 1180 1181
        /* subframe header */
        put_bits(&s->pb, 1, 0);
        put_bits(&s->pb, 6, sub->type_code);
        put_bits(&s->pb, 1, 0); /* no wasted bits */

        /* subframe */
Justin Ruggles's avatar
Justin Ruggles committed
1182 1183 1184 1185 1186 1187 1188
        if (sub->type == FLAC_SUBFRAME_CONSTANT) {
            put_sbits(&s->pb, sub->obits, res[0]);
        } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
            while (res < frame_end)
                put_sbits(&s->pb, sub->obits, *res++);
        } else {
            /* warm-up samples */
Justin Ruggles's avatar
Justin Ruggles committed
1189
            for (i = 0; i < sub->order; i++)
Justin Ruggles's avatar
Justin Ruggles committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
                put_sbits(&s->pb, sub->obits, *res++);

            /* LPC coefficients */
            if (sub->type == FLAC_SUBFRAME_LPC) {
                int cbits = s->options.lpc_coeff_precision;
                put_bits( &s->pb, 4, cbits-1);
                put_sbits(&s->pb, 5, sub->shift);
                for (i = 0; i < sub->order; i++)
                    put_sbits(&s->pb, cbits, sub->coefs[i]);
            }
Justin Ruggles's avatar
Justin Ruggles committed
1200

Justin Ruggles's avatar
Justin Ruggles committed
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
            /* rice-encoded block */
            put_bits(&s->pb, 2, 0);

            /* partition order */
            porder  = sub->rc.porder;
            psize   = s->frame.blocksize >> porder;
            put_bits(&s->pb, 4, porder);

            /* residual */
            part_end  = &sub->residual[psize];
            for (p = 0; p < 1 << porder; p++) {
                int k = sub->rc.params[p];
                put_bits(&s->pb, 4, k);
                while (res < part_end)
                    set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
                part_end = FFMIN(frame_end, part_end + psize);
            }
Justin Ruggles's avatar
Justin Ruggles committed
1218
        }
1219
    }
1220 1221
}

1222

1223
static void write_frame_footer(FlacEncodeContext *s)
1224 1225 1226
{
    int crc;
    flush_put_bits(&s->pb);
1227 1228
    crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
                            put_bits_count(&s->pb)>>3));
1229 1230 1231 1232
    put_bits(&s->pb, 16, crc);
    flush_put_bits(&s->pb);
}

1233

1234 1235 1236
static int write_frame(FlacEncodeContext *s, uint8_t *frame, int buf_size)
{
    init_put_bits(&s->pb, frame, buf_size);
1237 1238 1239
    write_frame_header(s);
    write_subframes(s);
    write_frame_footer(s);
1240 1241 1242 1243
    return put_bits_count(&s->pb) >> 3;
}


1244
static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
1245
{
1246
#if HAVE_BIGENDIAN
1247
    int i;
1248
    for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1249
        int16_t smp = av_le2ne16(samples[i]);
1250 1251 1252
        av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
    }
#else
1253
    av_md5_update(s->md5ctx, (const uint8_t *)samples, s->frame.blocksize*s->channels*2);
1254 1255 1256
#endif
}

1257

1258 1259 1260 1261
static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
                             int buf_size, void *data)
{
    FlacEncodeContext *s;
1262
    const int16_t *samples = data;
1263
    int frame_bytes, out_bytes;
1264 1265 1266

    s = avctx->priv_data;

1267 1268
    /* when the last block is reached, update the header in extradata */
    if (!data) {
1269
        s->max_framesize = s->max_encoded_framesize;
1270
        av_md5_final(s->md5ctx, s->md5sum);
1271 1272 1273 1274
        write_streaminfo(s, avctx->extradata);
        return 0;
    }

1275 1276 1277 1278 1279 1280
    /* change max_framesize for small final frame */
    if (avctx->frame_size < s->frame.blocksize) {
        s->max_framesize = ff_flac_get_max_frame_size(avctx->frame_size,
                                                      s->channels, 16);
    }

1281
    init_frame(s);
1282 1283 1284

    copy_samples(s, samples);

1285 1286
    channel_decorrelation(s);

1287
    frame_bytes = encode_frame(s);
1288

1289 1290
    /* fallback to verbatim mode if the compressed frame is larger than it
       would be if encoded uncompressed. */
1291
    if (frame_bytes > s->max_framesize) {
1292
        s->frame.verbatim_only = 1;
1293
        frame_bytes = encode_frame(s);
1294 1295
    }

1296 1297 1298 1299 1300 1301
    if (buf_size < frame_bytes) {
        av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
        return 0;
    }
    out_bytes = write_frame(s, frame, buf_size);

1302
    s->frame_count++;
1303
    avctx->coded_frame->pts = s->sample_count;
1304
    s->sample_count += avctx->frame_size;
1305
    update_md5_sum(s, samples);
1306 1307
    if (out_bytes > s->max_encoded_framesize)
        s->max_encoded_framesize = out_bytes;
1308 1309
    if (out_bytes < s->min_framesize)
        s->min_framesize = out_bytes;
1310

1311 1312 1313
    return out_bytes;
}

1314

1315
static av_cold int flac_encode_close(AVCodecContext *avctx)
1316
{
1317 1318 1319 1320
    if (avctx->priv_data) {
        FlacEncodeContext *s = avctx->priv_data;
        av_freep(&s->md5ctx);
    }
1321 1322
    av_freep(&avctx->extradata);
    avctx->extradata_size = 0;
1323 1324 1325 1326
    av_freep(&avctx->coded_frame);
    return 0;
}

1327

1328 1329
AVCodec flac_encoder = {
    "flac",
1330
    AVMEDIA_TYPE_AUDIO,
1331 1332 1333 1334 1335 1336
    CODEC_ID_FLAC,
    sizeof(FlacEncodeContext),
    flac_encode_init,
    flac_encode_frame,
    flac_encode_close,
    NULL,
1337
    .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
1338
    .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
1339
    .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
1340
};