~ubuntu-branches/ubuntu/trusty/gst-libav1.0/trusty-proposed

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/libavcodec/flacenc.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2013-09-24 17:07:00 UTC
  • mfrom: (1.1.17) (7.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20130924170700-4dg62s3pwl0pdakz
Tags: 1.2.0-1
* New upstream stable release:
  + debian/control:
    - Build depend on GStreamer and gst-plugins-base >= 1.2.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
 
1
/*
2
2
 * FLAC audio encoder
3
3
 * Copyright (c) 2006  Justin Ruggles <justin.ruggles@gmail.com>
4
4
 *
20
20
 */
21
21
 
22
22
#include "libavutil/crc.h"
 
23
#include "libavutil/intmath.h"
23
24
#include "libavutil/md5.h"
24
25
#include "libavutil/opt.h"
25
26
#include "avcodec.h"
 
27
#include "dsputil.h"
26
28
#include "get_bits.h"
27
29
#include "golomb.h"
 
30
#include "internal.h"
28
31
#include "lpc.h"
29
32
#include "flac.h"
30
33
#include "flacdata.h"
 
34
#include "flacdsp.h"
31
35
 
32
36
#define FLAC_SUBFRAME_CONSTANT  0
33
37
#define FLAC_SUBFRAME_VERBATIM  1
39
43
#define MAX_PARTITIONS     (1 << MAX_PARTITION_ORDER)
40
44
#define MAX_LPC_PRECISION  15
41
45
#define MAX_LPC_SHIFT      15
42
 
#define MAX_RICE_PARAM     14
 
46
 
 
47
enum CodingMode {
 
48
    CODING_MODE_RICE  = 4,
 
49
    CODING_MODE_RICE2 = 5,
 
50
};
43
51
 
44
52
typedef struct CompressionOptions {
45
53
    int compression_level;
52
60
    int prediction_order_method;
53
61
    int min_partition_order;
54
62
    int max_partition_order;
 
63
    int ch_mode;
55
64
} CompressionOptions;
56
65
 
57
66
typedef struct RiceContext {
 
67
    enum CodingMode coding_mode;
58
68
    int porder;
59
69
    int params[MAX_PARTITIONS];
60
70
} RiceContext;
63
73
    int type;
64
74
    int type_code;
65
75
    int obits;
 
76
    int wasted;
66
77
    int order;
67
78
    int32_t coefs[MAX_LPC_ORDER];
68
79
    int shift;
86
97
    int channels;
87
98
    int samplerate;
88
99
    int sr_code[2];
 
100
    int bps_code;
89
101
    int max_blocksize;
90
102
    int min_framesize;
91
103
    int max_framesize;
98
110
    AVCodecContext *avctx;
99
111
    LPCContext lpc_ctx;
100
112
    struct AVMD5 *md5ctx;
 
113
    uint8_t *md5_buffer;
 
114
    unsigned int md5_buffer_size;
 
115
    DSPContext dsp;
 
116
    FLACDSPContext flac_dsp;
101
117
} FlacEncodeContext;
102
118
 
103
119
 
118
134
    put_bits(&pb, 24, s->max_framesize);
119
135
    put_bits(&pb, 20, s->samplerate);
120
136
    put_bits(&pb, 3, s->channels-1);
121
 
    put_bits(&pb, 5, 15);       /* bits per sample - 1 */
 
137
    put_bits(&pb,  5, s->avctx->bits_per_raw_sample - 1);
122
138
    /* write 36-bit sample count in 2 put_bits() calls */
123
139
    put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
124
140
    put_bits(&pb, 12,  s->sample_count & 0x000000FFFLL);
218
234
 
219
235
    s->avctx = avctx;
220
236
 
221
 
    if (avctx->sample_fmt != AV_SAMPLE_FMT_S16)
222
 
        return -1;
 
237
    switch (avctx->sample_fmt) {
 
238
    case AV_SAMPLE_FMT_S16:
 
239
        avctx->bits_per_raw_sample = 16;
 
240
        s->bps_code                = 4;
 
241
        break;
 
242
    case AV_SAMPLE_FMT_S32:
 
243
        if (avctx->bits_per_raw_sample != 24)
 
244
            av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
 
245
        avctx->bits_per_raw_sample = 24;
 
246
        s->bps_code                = 6;
 
247
        break;
 
248
    }
223
249
 
224
250
    if (channels < 1 || channels > FLAC_MAX_CHANNELS)
225
251
        return -1;
295
321
    if (s->options.max_partition_order < 0)
296
322
        s->options.max_partition_order = ((int[]){  2,  2,  3,  3,  3,  8,  8,  8,  8,  8,  8,  8,  8})[level];
297
323
 
298
 
    /* set compression option overrides from AVCodecContext */
299
 
#if FF_API_FLAC_GLOBAL_OPTS
300
 
    if (avctx->lpc_type > FF_LPC_TYPE_DEFAULT) {
301
 
        if (avctx->lpc_type > FF_LPC_TYPE_CHOLESKY) {
302
 
            av_log(avctx, AV_LOG_ERROR, "unknown lpc type: %d\n", avctx->lpc_type);
303
 
            return -1;
304
 
        }
305
 
        s->options.lpc_type = avctx->lpc_type;
306
 
        if (s->options.lpc_type == FF_LPC_TYPE_CHOLESKY) {
307
 
            if (avctx->lpc_passes < 0) {
308
 
                // default number of passes for Cholesky
309
 
                s->options.lpc_passes = 2;
310
 
            } else if (avctx->lpc_passes == 0) {
311
 
                av_log(avctx, AV_LOG_ERROR, "invalid number of lpc passes: %d\n",
312
 
                       avctx->lpc_passes);
313
 
                return -1;
314
 
            } else {
315
 
                s->options.lpc_passes = avctx->lpc_passes;
316
 
            }
317
 
        }
318
 
    }
319
 
#endif
320
 
 
321
324
    if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
322
325
        s->options.min_prediction_order = 0;
323
326
    } else if (avctx->min_prediction_order >= 0) {
358
361
        return -1;
359
362
    }
360
363
 
361
 
#if FF_API_FLAC_GLOBAL_OPTS
362
 
    if (avctx->prediction_order_method >= 0) {
363
 
        if (avctx->prediction_order_method > ORDER_METHOD_LOG) {
364
 
            av_log(avctx, AV_LOG_ERROR, "invalid prediction order method: %d\n",
365
 
                   avctx->prediction_order_method);
366
 
            return -1;
367
 
        }
368
 
        s->options.prediction_order_method = avctx->prediction_order_method;
369
 
    }
370
 
 
371
 
    if (avctx->min_partition_order >= 0) {
372
 
        if (avctx->min_partition_order > MAX_PARTITION_ORDER) {
373
 
            av_log(avctx, AV_LOG_ERROR, "invalid min partition order: %d\n",
374
 
                   avctx->min_partition_order);
375
 
            return -1;
376
 
        }
377
 
        s->options.min_partition_order = avctx->min_partition_order;
378
 
    }
379
 
    if (avctx->max_partition_order >= 0) {
380
 
        if (avctx->max_partition_order > MAX_PARTITION_ORDER) {
381
 
            av_log(avctx, AV_LOG_ERROR, "invalid max partition order: %d\n",
382
 
                   avctx->max_partition_order);
383
 
            return -1;
384
 
        }
385
 
        s->options.max_partition_order = avctx->max_partition_order;
386
 
    }
387
 
    if (s->options.max_partition_order < s->options.min_partition_order) {
388
 
        av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
389
 
               s->options.min_partition_order, s->options.max_partition_order);
390
 
        return -1;
391
 
    }
392
 
#endif
393
 
 
394
364
    if (avctx->frame_size > 0) {
395
365
        if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
396
366
                avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
403
373
    }
404
374
    s->max_blocksize = s->avctx->frame_size;
405
375
 
406
 
#if FF_API_FLAC_GLOBAL_OPTS
407
 
    /* set LPC precision */
408
 
    if (avctx->lpc_coeff_precision > 0) {
409
 
        if (avctx->lpc_coeff_precision > MAX_LPC_PRECISION) {
410
 
            av_log(avctx, AV_LOG_ERROR, "invalid lpc coeff precision: %d\n",
411
 
                   avctx->lpc_coeff_precision);
412
 
            return -1;
413
 
        }
414
 
        s->options.lpc_coeff_precision = avctx->lpc_coeff_precision;
415
 
    }
416
 
#endif
417
 
 
418
376
    /* set maximum encoded frame size in verbatim mode */
419
377
    s->max_framesize = ff_flac_get_max_frame_size(s->avctx->frame_size,
420
 
                                                  s->channels, 16);
 
378
                                                  s->channels,
 
379
                                                  s->avctx->bits_per_raw_sample);
421
380
 
422
381
    /* initialize MD5 context */
423
 
    s->md5ctx = av_malloc(av_md5_size);
 
382
    s->md5ctx = av_md5_alloc();
424
383
    if (!s->md5ctx)
425
384
        return AVERROR(ENOMEM);
426
385
    av_md5_init(s->md5ctx);
435
394
    s->frame_count   = 0;
436
395
    s->min_framesize = s->max_framesize;
437
396
 
 
397
#if FF_API_OLD_ENCODE_AUDIO
438
398
    avctx->coded_frame = avcodec_alloc_frame();
439
399
    if (!avctx->coded_frame)
440
400
        return AVERROR(ENOMEM);
 
401
#endif
441
402
 
442
403
    ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
443
404
                      s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
444
405
 
 
406
    ff_dsputil_init(&s->dsp, avctx);
 
407
    ff_flacdsp_init(&s->flac_dsp, avctx->sample_fmt,
 
408
                    avctx->bits_per_raw_sample);
 
409
 
445
410
    dprint_compression_options(s);
446
411
 
447
412
    return ret;
448
413
}
449
414
 
450
415
 
451
 
static void init_frame(FlacEncodeContext *s)
 
416
static void init_frame(FlacEncodeContext *s, int nb_samples)
452
417
{
453
418
    int i, ch;
454
419
    FlacFrame *frame;
456
421
    frame = &s->frame;
457
422
 
458
423
    for (i = 0; i < 16; i++) {
459
 
        if (s->avctx->frame_size == ff_flac_blocksize_table[i]) {
 
424
        if (nb_samples == ff_flac_blocksize_table[i]) {
460
425
            frame->blocksize  = ff_flac_blocksize_table[i];
461
426
            frame->bs_code[0] = i;
462
427
            frame->bs_code[1] = 0;
464
429
        }
465
430
    }
466
431
    if (i == 16) {
467
 
        frame->blocksize = s->avctx->frame_size;
 
432
        frame->blocksize = nb_samples;
468
433
        if (frame->blocksize <= 256) {
469
434
            frame->bs_code[0] = 6;
470
435
            frame->bs_code[1] = frame->blocksize-1;
474
439
        }
475
440
    }
476
441
 
477
 
    for (ch = 0; ch < s->channels; ch++)
478
 
        frame->subframes[ch].obits = 16;
 
442
    for (ch = 0; ch < s->channels; ch++) {
 
443
        FlacSubframe *sub = &frame->subframes[ch];
 
444
 
 
445
        sub->wasted = 0;
 
446
        sub->obits  = s->avctx->bits_per_raw_sample;
 
447
 
 
448
        if (sub->obits > 16)
 
449
            sub->rc.coding_mode = CODING_MODE_RICE2;
 
450
        else
 
451
            sub->rc.coding_mode = CODING_MODE_RICE;
 
452
    }
479
453
 
480
454
    frame->verbatim_only = 0;
481
455
}
484
458
/**
485
459
 * Copy channel-interleaved input samples into separate subframes.
486
460
 */
487
 
static void copy_samples(FlacEncodeContext *s, const int16_t *samples)
 
461
static void copy_samples(FlacEncodeContext *s, const void *samples)
488
462
{
489
463
    int i, j, ch;
490
464
    FlacFrame *frame;
491
 
 
492
 
    frame = &s->frame;
493
 
    for (i = 0, j = 0; i < frame->blocksize; i++)
494
 
        for (ch = 0; ch < s->channels; ch++, j++)
495
 
            frame->subframes[ch].samples[i] = samples[j];
 
465
    int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
 
466
                s->avctx->bits_per_raw_sample;
 
467
 
 
468
#define COPY_SAMPLES(bits) do {                                     \
 
469
    const int ## bits ## _t *samples0 = samples;                    \
 
470
    frame = &s->frame;                                              \
 
471
    for (i = 0, j = 0; i < frame->blocksize; i++)                   \
 
472
        for (ch = 0; ch < s->channels; ch++, j++)                   \
 
473
            frame->subframes[ch].samples[i] = samples0[j] >> shift; \
 
474
} while (0)
 
475
 
 
476
    if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S16)
 
477
        COPY_SAMPLES(16);
 
478
    else
 
479
        COPY_SAMPLES(32);
496
480
}
497
481
 
498
482
 
499
 
static int rice_count_exact(int32_t *res, int n, int k)
 
483
static uint64_t rice_count_exact(int32_t *res, int n, int k)
500
484
{
501
485
    int i;
502
 
    int count = 0;
 
486
    uint64_t count = 0;
503
487
 
504
488
    for (i = 0; i < n; i++) {
505
489
        int32_t v = -2 * res[i] - 1;
510
494
}
511
495
 
512
496
 
513
 
static int subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
514
 
                                int pred_order)
 
497
static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
 
498
                                     int pred_order)
515
499
{
516
500
    int p, porder, psize;
517
501
    int i, part_end;
518
 
    int count = 0;
 
502
    uint64_t count = 0;
519
503
 
520
504
    /* subframe header */
521
505
    count += 8;
546
530
        part_end = psize;
547
531
        for (p = 0; p < 1 << porder; p++) {
548
532
            int k = sub->rc.params[p];
549
 
            count += 4;
 
533
            count += sub->rc.coding_mode;
550
534
            count += rice_count_exact(&sub->residual[i], part_end - i, k);
551
535
            i = part_end;
552
536
            part_end = FFMIN(s->frame.blocksize, part_end + psize);
562
546
/**
563
547
 * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
564
548
 */
565
 
static int find_optimal_param(uint32_t sum, int n)
 
549
static int find_optimal_param(uint64_t sum, int n, int max_param)
566
550
{
567
551
    int k;
568
 
    uint32_t sum2;
 
552
    uint64_t sum2;
569
553
 
570
554
    if (sum <= n >> 1)
571
555
        return 0;
572
556
    sum2 = sum - (n >> 1);
573
 
    k    = av_log2(n < 256 ? FASTDIV(sum2, n) : sum2 / n);
574
 
    return FFMIN(k, MAX_RICE_PARAM);
 
557
    k    = av_log2(av_clipl_int32(sum2 / n));
 
558
    return FFMIN(k, max_param);
575
559
}
576
560
 
577
561
 
578
 
static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
579
 
                                         uint32_t *sums, int n, int pred_order)
 
562
static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder,
 
563
                                         uint64_t *sums, int n, int pred_order)
580
564
{
581
565
    int i;
582
 
    int k, cnt, part;
583
 
    uint32_t all_bits;
 
566
    int k, cnt, part, max_param;
 
567
    uint64_t all_bits;
 
568
 
 
569
    max_param = (1 << rc->coding_mode) - 2;
584
570
 
585
571
    part     = (1 << porder);
586
572
    all_bits = 4 * part;
587
573
 
588
574
    cnt = (n >> porder) - pred_order;
589
575
    for (i = 0; i < part; i++) {
590
 
        k = find_optimal_param(sums[i], cnt);
 
576
        k = find_optimal_param(sums[i], cnt, max_param);
591
577
        rc->params[i] = k;
592
578
        all_bits += rice_encode_count(sums[i], cnt, k);
593
579
        cnt = n >> porder;
600
586
 
601
587
 
602
588
static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
603
 
                      uint32_t sums[][MAX_PARTITIONS])
 
589
                      uint64_t sums[][MAX_PARTITIONS])
604
590
{
605
591
    int i, j;
606
592
    int parts;
611
597
    res     = &data[pred_order];
612
598
    res_end = &data[n >> pmax];
613
599
    for (i = 0; i < parts; i++) {
614
 
        uint32_t sum = 0;
 
600
        uint64_t sum = 0;
615
601
        while (res < res_end)
616
602
            sum += *(res++);
617
603
        sums[pmax][i] = sum;
626
612
}
627
613
 
628
614
 
629
 
static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
 
615
static uint64_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
630
616
                                 int32_t *data, int n, int pred_order)
631
617
{
632
618
    int i;
633
 
    uint32_t bits[MAX_PARTITION_ORDER+1];
 
619
    uint64_t bits[MAX_PARTITION_ORDER+1];
634
620
    int opt_porder;
635
621
    RiceContext tmp_rc;
636
622
    uint32_t *udata;
637
 
    uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
 
623
    uint64_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
638
624
 
639
625
    assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
640
626
    assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
641
627
    assert(pmin <= pmax);
642
628
 
 
629
    tmp_rc.coding_mode = rc->coding_mode;
 
630
 
643
631
    udata = av_malloc(n * sizeof(uint32_t));
644
632
    for (i = 0; i < n; i++)
645
633
        udata[i] = (2*data[i]) ^ (data[i]>>31);
670
658
}
671
659
 
672
660
 
673
 
static uint32_t find_subframe_rice_params(FlacEncodeContext *s,
 
661
static uint64_t find_subframe_rice_params(FlacEncodeContext *s,
674
662
                                          FlacSubframe *sub, int pred_order)
675
663
{
676
664
    int pmin = get_max_p_order(s->options.min_partition_order,
678
666
    int pmax = get_max_p_order(s->options.max_partition_order,
679
667
                               s->frame.blocksize, pred_order);
680
668
 
681
 
    uint32_t bits = 8 + pred_order * sub->obits + 2 + 4;
 
669
    uint64_t bits = 8 + pred_order * sub->obits + 2 + sub->rc.coding_mode;
682
670
    if (sub->type == FLAC_SUBFRAME_LPC)
683
671
        bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
684
672
    bits += calc_rice_params(&sub->rc, pmin, pmax, sub->residual,
738
726
}
739
727
 
740
728
 
741
 
#define LPC1(x) {\
742
 
    int c = coefs[(x)-1];\
743
 
    p0   += c * s;\
744
 
    s     = smp[i-(x)+1];\
745
 
    p1   += c * s;\
746
 
}
747
 
 
748
 
static av_always_inline void encode_residual_lpc_unrolled(int32_t *res,
749
 
                                    const int32_t *smp, int n, int order,
750
 
                                    const int32_t *coefs, int shift, int big)
751
 
{
752
 
    int i;
753
 
    for (i = order; i < n; i += 2) {
754
 
        int s  = smp[i-order];
755
 
        int p0 = 0, p1 = 0;
756
 
        if (big) {
757
 
            switch (order) {
758
 
            case 32: LPC1(32)
759
 
            case 31: LPC1(31)
760
 
            case 30: LPC1(30)
761
 
            case 29: LPC1(29)
762
 
            case 28: LPC1(28)
763
 
            case 27: LPC1(27)
764
 
            case 26: LPC1(26)
765
 
            case 25: LPC1(25)
766
 
            case 24: LPC1(24)
767
 
            case 23: LPC1(23)
768
 
            case 22: LPC1(22)
769
 
            case 21: LPC1(21)
770
 
            case 20: LPC1(20)
771
 
            case 19: LPC1(19)
772
 
            case 18: LPC1(18)
773
 
            case 17: LPC1(17)
774
 
            case 16: LPC1(16)
775
 
            case 15: LPC1(15)
776
 
            case 14: LPC1(14)
777
 
            case 13: LPC1(13)
778
 
            case 12: LPC1(12)
779
 
            case 11: LPC1(11)
780
 
            case 10: LPC1(10)
781
 
            case  9: LPC1( 9)
782
 
                     LPC1( 8)
783
 
                     LPC1( 7)
784
 
                     LPC1( 6)
785
 
                     LPC1( 5)
786
 
                     LPC1( 4)
787
 
                     LPC1( 3)
788
 
                     LPC1( 2)
789
 
                     LPC1( 1)
790
 
            }
791
 
        } else {
792
 
            switch (order) {
793
 
            case  8: LPC1( 8)
794
 
            case  7: LPC1( 7)
795
 
            case  6: LPC1( 6)
796
 
            case  5: LPC1( 5)
797
 
            case  4: LPC1( 4)
798
 
            case  3: LPC1( 3)
799
 
            case  2: LPC1( 2)
800
 
            case  1: LPC1( 1)
801
 
            }
802
 
        }
803
 
        res[i  ] = smp[i  ] - (p0 >> shift);
804
 
        res[i+1] = smp[i+1] - (p1 >> shift);
805
 
    }
806
 
}
807
 
 
808
 
 
809
 
static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
810
 
                                int order, const int32_t *coefs, int shift)
811
 
{
812
 
    int i;
813
 
    for (i = 0; i < order; i++)
814
 
        res[i] = smp[i];
815
 
#if CONFIG_SMALL
816
 
    for (i = order; i < n; i += 2) {
817
 
        int j;
818
 
        int s  = smp[i];
819
 
        int p0 = 0, p1 = 0;
820
 
        for (j = 0; j < order; j++) {
821
 
            int c = coefs[j];
822
 
            p1   += c * s;
823
 
            s     = smp[i-j-1];
824
 
            p0   += c * s;
825
 
        }
826
 
        res[i  ] = smp[i  ] - (p0 >> shift);
827
 
        res[i+1] = smp[i+1] - (p1 >> shift);
828
 
    }
829
 
#else
830
 
    switch (order) {
831
 
    case  1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
832
 
    case  2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
833
 
    case  3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
834
 
    case  4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
835
 
    case  5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
836
 
    case  6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
837
 
    case  7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
838
 
    case  8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
839
 
    default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
840
 
    }
841
 
#endif
842
 
}
843
 
 
844
 
 
845
729
static int encode_residual_ch(FlacEncodeContext *s, int ch)
846
730
{
847
731
    int i, n;
883
767
    sub->type = FLAC_SUBFRAME_FIXED;
884
768
    if (s->options.lpc_type == FF_LPC_TYPE_NONE  ||
885
769
        s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
886
 
        uint32_t bits[MAX_FIXED_ORDER+1];
 
770
        uint64_t bits[MAX_FIXED_ORDER+1];
887
771
        if (max_order > MAX_FIXED_ORDER)
888
772
            max_order = MAX_FIXED_ORDER;
889
773
        opt_order = 0;
914
798
        omethod == ORDER_METHOD_4LEVEL ||
915
799
        omethod == ORDER_METHOD_8LEVEL) {
916
800
        int levels = 1 << omethod;
917
 
        uint32_t bits[1 << ORDER_METHOD_8LEVEL];
 
801
        uint64_t bits[1 << ORDER_METHOD_8LEVEL];
918
802
        int order       = -1;
919
803
        int opt_index   = levels-1;
920
804
        opt_order       = max_order-1;
925
809
            order = av_clip(order, min_order - 1, max_order - 1);
926
810
            if (order == last_order)
927
811
                continue;
928
 
            encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
 
812
            s->flac_dsp.lpc_encode(res, smp, n, order+1, coefs[order],
 
813
                                   shift[order]);
929
814
            bits[i] = find_subframe_rice_params(s, sub, order+1);
930
815
            if (bits[i] < bits[opt_index]) {
931
816
                opt_index = i;
935
820
        opt_order++;
936
821
    } else if (omethod == ORDER_METHOD_SEARCH) {
937
822
        // brute-force optimal order search
938
 
        uint32_t bits[MAX_LPC_ORDER];
 
823
        uint64_t bits[MAX_LPC_ORDER];
939
824
        opt_order = 0;
940
825
        bits[0]   = UINT32_MAX;
941
826
        for (i = min_order-1; i < max_order; i++) {
942
 
            encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
 
827
            s->flac_dsp.lpc_encode(res, smp, n, i+1, coefs[i], shift[i]);
943
828
            bits[i] = find_subframe_rice_params(s, sub, i+1);
944
829
            if (bits[i] < bits[opt_order])
945
830
                opt_order = i;
946
831
        }
947
832
        opt_order++;
948
833
    } else if (omethod == ORDER_METHOD_LOG) {
949
 
        uint32_t bits[MAX_LPC_ORDER];
 
834
        uint64_t bits[MAX_LPC_ORDER];
950
835
        int step;
951
836
 
952
837
        opt_order = min_order - 1 + (max_order-min_order)/3;
957
842
            for (i = last-step; i <= last+step; i += step) {
958
843
                if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
959
844
                    continue;
960
 
                encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
 
845
                s->flac_dsp.lpc_encode(res, smp, n, i+1, coefs[i], shift[i]);
961
846
                bits[i] = find_subframe_rice_params(s, sub, i+1);
962
847
                if (bits[i] < bits[opt_order])
963
848
                    opt_order = i;
972
857
    for (i = 0; i < sub->order; i++)
973
858
        sub->coefs[i] = coefs[sub->order-1][i];
974
859
 
975
 
    encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
 
860
    s->flac_dsp.lpc_encode(res, smp, n, sub->order, sub->coefs, sub->shift);
976
861
 
977
862
    find_subframe_rice_params(s, sub, sub->order);
978
863
 
1018
903
 
1019
904
static int encode_frame(FlacEncodeContext *s)
1020
905
{
1021
 
    int ch, count;
 
906
    int ch;
 
907
    uint64_t count;
1022
908
 
1023
909
    count = count_frame_header(s);
1024
910
 
1028
914
    count += (8 - (count & 7)) & 7; // byte alignment
1029
915
    count += 16;                    // CRC-16
1030
916
 
1031
 
    return count >> 3;
1032
 
}
1033
 
 
1034
 
 
1035
 
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
 
917
    count >>= 3;
 
918
    if (count > INT_MAX)
 
919
        return AVERROR_BUG;
 
920
    return count;
 
921
}
 
922
 
 
923
 
 
924
static void remove_wasted_bits(FlacEncodeContext *s)
 
925
{
 
926
    int ch, i;
 
927
 
 
928
    for (ch = 0; ch < s->channels; ch++) {
 
929
        FlacSubframe *sub = &s->frame.subframes[ch];
 
930
        int32_t v         = 0;
 
931
 
 
932
        for (i = 0; i < s->frame.blocksize; i++) {
 
933
            v |= sub->samples[i];
 
934
            if (v & 1)
 
935
                break;
 
936
        }
 
937
 
 
938
        if (v && !(v & 1)) {
 
939
            v = av_ctz(v);
 
940
 
 
941
            for (i = 0; i < s->frame.blocksize; i++)
 
942
                sub->samples[i] >>= v;
 
943
 
 
944
            sub->wasted = v;
 
945
            sub->obits -= v;
 
946
 
 
947
            /* for 24-bit, check if removing wasted bits makes the range better
 
948
               suited for using RICE instead of RICE2 for entropy coding */
 
949
            if (sub->obits <= 17)
 
950
                sub->rc.coding_mode = CODING_MODE_RICE;
 
951
        }
 
952
    }
 
953
}
 
954
 
 
955
 
 
956
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n,
 
957
                                int max_rice_param)
1036
958
{
1037
959
    int i, best;
1038
960
    int32_t lt, rt;
1052
974
    }
1053
975
    /* estimate bit counts */
1054
976
    for (i = 0; i < 4; i++) {
1055
 
        k      = find_optimal_param(2 * sum[i], n);
 
977
        k      = find_optimal_param(2 * sum[i], n, max_rice_param);
1056
978
        sum[i] = rice_encode_count( 2 * sum[i], n, k);
1057
979
    }
1058
980
 
1067
989
    for (i = 1; i < 4; i++)
1068
990
        if (score[i] < score[best])
1069
991
            best = i;
1070
 
    if (best == 0) {
1071
 
        return FLAC_CHMODE_INDEPENDENT;
1072
 
    } else if (best == 1) {
1073
 
        return FLAC_CHMODE_LEFT_SIDE;
1074
 
    } else if (best == 2) {
1075
 
        return FLAC_CHMODE_RIGHT_SIDE;
1076
 
    } else {
1077
 
        return FLAC_CHMODE_MID_SIDE;
1078
 
    }
 
992
 
 
993
    return best;
1079
994
}
1080
995
 
1081
996
 
1098
1013
        return;
1099
1014
    }
1100
1015
 
1101
 
    frame->ch_mode = estimate_stereo_mode(left, right, n);
 
1016
    if (s->options.ch_mode < 0) {
 
1017
        int max_rice_param = (1 << frame->subframes[0].rc.coding_mode) - 2;
 
1018
        frame->ch_mode = estimate_stereo_mode(left, right, n, max_rice_param);
 
1019
    } else
 
1020
        frame->ch_mode = s->options.ch_mode;
1102
1021
 
1103
1022
    /* perform decorrelation and adjust bits-per-sample */
1104
1023
    if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1144
1063
    if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1145
1064
        put_bits(&s->pb, 4, s->channels-1);
1146
1065
    else
1147
 
        put_bits(&s->pb, 4, frame->ch_mode);
 
1066
        put_bits(&s->pb, 4, frame->ch_mode + FLAC_MAX_CHANNELS - 1);
1148
1067
 
1149
 
    put_bits(&s->pb, 3, 4); /* bits-per-sample code */
 
1068
    put_bits(&s->pb, 3, s->bps_code);
1150
1069
    put_bits(&s->pb, 1, 0);
1151
1070
    write_utf8(&s->pb, s->frame_count);
1152
1071
 
1181
1100
        /* subframe header */
1182
1101
        put_bits(&s->pb, 1, 0);
1183
1102
        put_bits(&s->pb, 6, sub->type_code);
1184
 
        put_bits(&s->pb, 1, 0); /* no wasted bits */
 
1103
        put_bits(&s->pb, 1, !!sub->wasted);
 
1104
        if (sub->wasted)
 
1105
            put_bits(&s->pb, sub->wasted, 1);
1185
1106
 
1186
1107
        /* subframe */
1187
1108
        if (sub->type == FLAC_SUBFRAME_CONSTANT) {
1204
1125
            }
1205
1126
 
1206
1127
            /* rice-encoded block */
1207
 
            put_bits(&s->pb, 2, 0);
 
1128
            put_bits(&s->pb, 2, sub->rc.coding_mode - 4);
1208
1129
 
1209
1130
            /* partition order */
1210
1131
            porder  = sub->rc.porder;
1215
1136
            part_end  = &sub->residual[psize];
1216
1137
            for (p = 0; p < 1 << porder; p++) {
1217
1138
                int k = sub->rc.params[p];
1218
 
                put_bits(&s->pb, 4, k);
 
1139
                put_bits(&s->pb, sub->rc.coding_mode, k);
1219
1140
                while (res < part_end)
1220
1141
                    set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
1221
1142
                part_end = FFMIN(frame_end, part_end + psize);
1236
1157
}
1237
1158
 
1238
1159
 
1239
 
static int write_frame(FlacEncodeContext *s, uint8_t *frame, int buf_size)
 
1160
static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
1240
1161
{
1241
 
    init_put_bits(&s->pb, frame, buf_size);
 
1162
    init_put_bits(&s->pb, avpkt->data, avpkt->size);
1242
1163
    write_frame_header(s);
1243
1164
    write_subframes(s);
1244
1165
    write_frame_footer(s);
1246
1167
}
1247
1168
 
1248
1169
 
1249
 
static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
 
1170
static int update_md5_sum(FlacEncodeContext *s, const void *samples)
1250
1171
{
 
1172
    const uint8_t *buf;
 
1173
    int buf_size = s->frame.blocksize * s->channels *
 
1174
                   ((s->avctx->bits_per_raw_sample + 7) / 8);
 
1175
 
 
1176
    if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
 
1177
        av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size);
 
1178
        if (!s->md5_buffer)
 
1179
            return AVERROR(ENOMEM);
 
1180
    }
 
1181
 
 
1182
    if (s->avctx->bits_per_raw_sample <= 16) {
 
1183
        buf = (const uint8_t *)samples;
1251
1184
#if HAVE_BIGENDIAN
1252
 
    int i;
1253
 
    for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1254
 
        int16_t smp = av_le2ne16(samples[i]);
1255
 
        av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
 
1185
        s->dsp.bswap16_buf((uint16_t *)s->md5_buffer,
 
1186
                           (const uint16_t *)samples, buf_size / 2);
 
1187
        buf = s->md5_buffer;
 
1188
#endif
 
1189
    } else {
 
1190
        int i;
 
1191
        const int32_t *samples0 = samples;
 
1192
        uint8_t *tmp            = s->md5_buffer;
 
1193
 
 
1194
        for (i = 0; i < s->frame.blocksize * s->channels; i++) {
 
1195
            int32_t v = samples0[i] >> 8;
 
1196
            *tmp++    = (v      ) & 0xFF;
 
1197
            *tmp++    = (v >>  8) & 0xFF;
 
1198
            *tmp++    = (v >> 16) & 0xFF;
 
1199
        }
 
1200
        buf = s->md5_buffer;
1256
1201
    }
1257
 
#else
1258
 
    av_md5_update(s->md5ctx, (const uint8_t *)samples, s->frame.blocksize*s->channels*2);
1259
 
#endif
 
1202
    av_md5_update(s->md5ctx, buf, buf_size);
 
1203
 
 
1204
    return 0;
1260
1205
}
1261
1206
 
1262
1207
 
1263
 
static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
1264
 
                             int buf_size, void *data)
 
1208
static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
1209
                             const AVFrame *frame, int *got_packet_ptr)
1265
1210
{
1266
1211
    FlacEncodeContext *s;
1267
 
    const int16_t *samples = data;
1268
 
    int frame_bytes, out_bytes;
 
1212
    int frame_bytes, out_bytes, ret;
1269
1213
 
1270
1214
    s = avctx->priv_data;
1271
1215
 
1272
1216
    /* when the last block is reached, update the header in extradata */
1273
 
    if (!data) {
 
1217
    if (!frame) {
1274
1218
        s->max_framesize = s->max_encoded_framesize;
1275
1219
        av_md5_final(s->md5ctx, s->md5sum);
1276
1220
        write_streaminfo(s, avctx->extradata);
1278
1222
    }
1279
1223
 
1280
1224
    /* change max_framesize for small final frame */
1281
 
    if (avctx->frame_size < s->frame.blocksize) {
1282
 
        s->max_framesize = ff_flac_get_max_frame_size(avctx->frame_size,
1283
 
                                                      s->channels, 16);
 
1225
    if (frame->nb_samples < s->frame.blocksize) {
 
1226
        s->max_framesize = ff_flac_get_max_frame_size(frame->nb_samples,
 
1227
                                                      s->channels,
 
1228
                                                      avctx->bits_per_raw_sample);
1284
1229
    }
1285
1230
 
1286
 
    init_frame(s);
 
1231
    init_frame(s, frame->nb_samples);
1287
1232
 
1288
 
    copy_samples(s, samples);
 
1233
    copy_samples(s, frame->data[0]);
1289
1234
 
1290
1235
    channel_decorrelation(s);
1291
1236
 
 
1237
    remove_wasted_bits(s);
 
1238
 
1292
1239
    frame_bytes = encode_frame(s);
1293
1240
 
1294
1241
    /* fallback to verbatim mode if the compressed frame is larger than it
1295
1242
       would be if encoded uncompressed. */
1296
 
    if (frame_bytes > s->max_framesize) {
 
1243
    if (frame_bytes < 0 || frame_bytes > s->max_framesize) {
1297
1244
        s->frame.verbatim_only = 1;
1298
1245
        frame_bytes = encode_frame(s);
1299
 
    }
1300
 
 
1301
 
    if (buf_size < frame_bytes) {
1302
 
        av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
1303
 
        return 0;
1304
 
    }
1305
 
    out_bytes = write_frame(s, frame, buf_size);
 
1246
        if (frame_bytes < 0) {
 
1247
            av_log(avctx, AV_LOG_ERROR, "Bad frame count\n");
 
1248
            return frame_bytes;
 
1249
        }
 
1250
    }
 
1251
 
 
1252
    if ((ret = ff_alloc_packet(avpkt, frame_bytes))) {
 
1253
        av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
 
1254
        return ret;
 
1255
    }
 
1256
 
 
1257
    out_bytes = write_frame(s, avpkt);
1306
1258
 
1307
1259
    s->frame_count++;
1308
 
    avctx->coded_frame->pts = s->sample_count;
1309
 
    s->sample_count += avctx->frame_size;
1310
 
    update_md5_sum(s, samples);
 
1260
    s->sample_count += frame->nb_samples;
 
1261
    if ((ret = update_md5_sum(s, frame->data[0])) < 0) {
 
1262
        av_log(avctx, AV_LOG_ERROR, "Error updating MD5 checksum\n");
 
1263
        return ret;
 
1264
    }
1311
1265
    if (out_bytes > s->max_encoded_framesize)
1312
1266
        s->max_encoded_framesize = out_bytes;
1313
1267
    if (out_bytes < s->min_framesize)
1314
1268
        s->min_framesize = out_bytes;
1315
1269
 
1316
 
    return out_bytes;
 
1270
    avpkt->pts      = frame->pts;
 
1271
    avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
 
1272
    avpkt->size     = out_bytes;
 
1273
    *got_packet_ptr = 1;
 
1274
    return 0;
1317
1275
}
1318
1276
 
1319
1277
 
1322
1280
    if (avctx->priv_data) {
1323
1281
        FlacEncodeContext *s = avctx->priv_data;
1324
1282
        av_freep(&s->md5ctx);
 
1283
        av_freep(&s->md5_buffer);
1325
1284
        ff_lpc_end(&s->lpc_ctx);
1326
1285
    }
1327
1286
    av_freep(&avctx->extradata);
1328
1287
    avctx->extradata_size = 0;
 
1288
#if FF_API_OLD_ENCODE_AUDIO
1329
1289
    av_freep(&avctx->coded_frame);
 
1290
#endif
1330
1291
    return 0;
1331
1292
}
1332
1293
 
1333
1294
#define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1334
1295
static const AVOption options[] = {
1335
 
{ "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.dbl = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
1336
 
{ "lpc_type", "LPC algorithm", offsetof(FlacEncodeContext, options.lpc_type), AV_OPT_TYPE_INT, {.dbl = FF_LPC_TYPE_DEFAULT }, FF_LPC_TYPE_DEFAULT, FF_LPC_TYPE_NB-1, FLAGS, "lpc_type" },
1337
 
{ "none",     NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_NONE },     INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1338
 
{ "fixed",    NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_FIXED },    INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1339
 
{ "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1340
 
{ "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1341
 
{ "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes),  AV_OPT_TYPE_INT, {.dbl = -1 }, INT_MIN, INT_MAX, FLAGS },
1342
 
{ "min_partition_order",  NULL, offsetof(FlacEncodeContext, options.min_partition_order),  AV_OPT_TYPE_INT, {.dbl = -1 },      -1, MAX_PARTITION_ORDER, FLAGS },
1343
 
{ "max_partition_order",  NULL, offsetof(FlacEncodeContext, options.max_partition_order),  AV_OPT_TYPE_INT, {.dbl = -1 },      -1, MAX_PARTITION_ORDER, FLAGS },
1344
 
{ "prediction_order_method", "Search method for selecting prediction order", offsetof(FlacEncodeContext, options.prediction_order_method), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, ORDER_METHOD_LOG, FLAGS, "predm" },
1345
 
{ "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_EST },    INT_MIN, INT_MAX, FLAGS, "predm" },
1346
 
{ "2level",     NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1347
 
{ "4level",     NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1348
 
{ "8level",     NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1349
 
{ "search",     NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
1350
 
{ "log",        NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_LOG },    INT_MIN, INT_MAX, FLAGS, "predm" },
 
1296
{ "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
 
1297
{ "lpc_type", "LPC algorithm", offsetof(FlacEncodeContext, options.lpc_type), AV_OPT_TYPE_INT, {.i64 = FF_LPC_TYPE_DEFAULT }, FF_LPC_TYPE_DEFAULT, FF_LPC_TYPE_NB-1, FLAGS, "lpc_type" },
 
1298
{ "none",     NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE },     INT_MIN, INT_MAX, FLAGS, "lpc_type" },
 
1299
{ "fixed",    NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED },    INT_MIN, INT_MAX, FLAGS, "lpc_type" },
 
1300
{ "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
 
1301
{ "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
 
1302
{ "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes),  AV_OPT_TYPE_INT, {.i64 = 1 }, 1, INT_MAX, FLAGS },
 
1303
{ "min_partition_order",  NULL, offsetof(FlacEncodeContext, options.min_partition_order),  AV_OPT_TYPE_INT, {.i64 = -1 },      -1, MAX_PARTITION_ORDER, FLAGS },
 
1304
{ "max_partition_order",  NULL, offsetof(FlacEncodeContext, options.max_partition_order),  AV_OPT_TYPE_INT, {.i64 = -1 },      -1, MAX_PARTITION_ORDER, FLAGS },
 
1305
{ "prediction_order_method", "Search method for selecting prediction order", offsetof(FlacEncodeContext, options.prediction_order_method), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, ORDER_METHOD_LOG, FLAGS, "predm" },
 
1306
{ "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST },    INT_MIN, INT_MAX, FLAGS, "predm" },
 
1307
{ "2level",     NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
 
1308
{ "4level",     NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
 
1309
{ "8level",     NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
 
1310
{ "search",     NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
 
1311
{ "log",        NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG },    INT_MIN, INT_MAX, FLAGS, "predm" },
 
1312
{ "ch_mode", "Stereo decorrelation mode", offsetof(FlacEncodeContext, options.ch_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FLAC_CHMODE_MID_SIDE, FLAGS, "ch_mode" },
 
1313
{ "auto",       NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1                      }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
 
1314
{ "indep",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
 
1315
{ "left_side",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE   }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
 
1316
{ "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE  }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
 
1317
{ "mid_side",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE    }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1351
1318
{ NULL },
1352
1319
};
1353
1320
 
1361
1328
AVCodec ff_flac_encoder = {
1362
1329
    .name           = "flac",
1363
1330
    .type           = AVMEDIA_TYPE_AUDIO,
1364
 
    .id             = CODEC_ID_FLAC,
 
1331
    .id             = AV_CODEC_ID_FLAC,
1365
1332
    .priv_data_size = sizeof(FlacEncodeContext),
1366
1333
    .init           = flac_encode_init,
1367
 
    .encode         = flac_encode_frame,
 
1334
    .encode2        = flac_encode_frame,
1368
1335
    .close          = flac_encode_close,
1369
 
    .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
1370
 
    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
1371
 
    .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
1372
 
    .priv_class = &flac_encoder_class,
 
1336
    .capabilities   = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
 
1337
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
 
1338
                                                     AV_SAMPLE_FMT_S32,
 
1339
                                                     AV_SAMPLE_FMT_NONE },
 
1340
    .long_name      = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
 
1341
    .priv_class     = &flac_encoder_class,
1373
1342
};