Statistics
| Branch: | Revision:

ffmpeg / libavcodec / flacenc.c @ 7fe0c7c8

History | View | Annotate | Download (40.5 KB)

1
/**
2
 * FLAC audio encoder
3
 * Copyright (c) 2006  Justin Ruggles <justin.ruggles@gmail.com>
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * 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
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
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
#define FLAC_SUBFRAME_CONSTANT  0
33
#define FLAC_SUBFRAME_VERBATIM  1
34
#define FLAC_SUBFRAME_FIXED     8
35
#define FLAC_SUBFRAME_LPC      32
36

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

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

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

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

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

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

    
101

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

    
109
    memset(header, 0, FLAC_STREAMINFO_SIZE);
110
    init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
111

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

    
127

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

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

    
150

    
151
static av_cold void dprint_compression_options(FlacEncodeContext *s)
152
{
153
    AVCodecContext     *avctx = s->avctx;
154
    CompressionOptions *opt   = &s->options;
155

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

    
158
    switch (opt->lpc_type) {
159
    case AV_LPC_TYPE_NONE:
160
        av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
161
        break;
162
    case AV_LPC_TYPE_FIXED:
163
        av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
164
        break;
165
    case AV_LPC_TYPE_LEVINSON:
166
        av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
167
        break;
168
    case AV_LPC_TYPE_CHOLESKY:
169
        av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
170
               opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
171
        break;
172
    }
173

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

    
177
    switch (opt->prediction_order_method) {
178
    case ORDER_METHOD_EST:
179
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
180
        break;
181
    case ORDER_METHOD_2LEVEL:
182
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
183
        break;
184
    case ORDER_METHOD_4LEVEL:
185
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
186
        break;
187
    case ORDER_METHOD_8LEVEL:
188
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
189
        break;
190
    case ORDER_METHOD_SEARCH:
191
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
192
        break;
193
    case ORDER_METHOD_LOG:
194
        av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
195
        break;
196
    }
197

    
198

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

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

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

    
208

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

    
217
    s->avctx = avctx;
218

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

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

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

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

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

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

    
269
    s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
270

    
271
    s->options.lpc_type      = ((int[]){ AV_LPC_TYPE_FIXED,    AV_LPC_TYPE_FIXED,    AV_LPC_TYPE_FIXED,
272
                                         AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON,
273
                                         AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON,
274
                                         AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON,
275
                                         AV_LPC_TYPE_LEVINSON})[level];
276

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

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

    
286
    s->options.min_partition_order = ((int[]){  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0})[level];
287
    s->options.max_partition_order = ((int[]){  2,  2,  3,  3,  3,  8,  8,  8,  8,  8,  8,  8,  8})[level];
288

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

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

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

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

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

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

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

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

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

    
434
    s->frame_count   = 0;
435
    s->min_framesize = s->max_framesize;
436

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

    
442
    dprint_compression_options(s);
443

    
444
    return 0;
445
}
446

    
447

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

    
453
    frame = &s->frame;
454

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

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

    
478

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

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

    
493

    
494
#define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
495

    
496
/**
497
 * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
498
 */
499
static int find_optimal_param(uint32_t sum, int n)
500
{
501
    int k;
502
    uint32_t sum2;
503

    
504
    if (sum <= n >> 1)
505
        return 0;
506
    sum2 = sum - (n >> 1);
507
    k    = av_log2(n < 256 ? FASTDIV(sum2, n) : sum2 / n);
508
    return FFMIN(k, MAX_RICE_PARAM);
509
}
510

    
511

    
512
static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
513
                                         uint32_t *sums, int n, int pred_order)
514
{
515
    int i;
516
    int k, cnt, part;
517
    uint32_t all_bits;
518

    
519
    part     = (1 << porder);
520
    all_bits = 4 * part;
521

    
522
    cnt = (n >> porder) - pred_order;
523
    for (i = 0; i < part; i++) {
524
        k = find_optimal_param(sums[i], cnt);
525
        rc->params[i] = k;
526
        all_bits += rice_encode_count(sums[i], cnt, k);
527
        cnt = n >> porder;
528
    }
529

    
530
    rc->porder = porder;
531

    
532
    return all_bits;
533
}
534

    
535

    
536
static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
537
                      uint32_t sums[][MAX_PARTITIONS])
538
{
539
    int i, j;
540
    int parts;
541
    uint32_t *res, *res_end;
542

    
543
    /* sums for highest level */
544
    parts   = (1 << pmax);
545
    res     = &data[pred_order];
546
    res_end = &data[n >> pmax];
547
    for (i = 0; i < parts; i++) {
548
        uint32_t sum = 0;
549
        while (res < res_end)
550
            sum += *(res++);
551
        sums[pmax][i] = sum;
552
        res_end += n >> pmax;
553
    }
554
    /* sums for lower levels */
555
    for (i = pmax - 1; i >= pmin; i--) {
556
        parts = (1 << i);
557
        for (j = 0; j < parts; j++)
558
            sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
559
    }
560
}
561

    
562

    
563
static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
564
                                 int32_t *data, int n, int pred_order)
565
{
566
    int i;
567
    uint32_t bits[MAX_PARTITION_ORDER+1];
568
    int opt_porder;
569
    RiceContext tmp_rc;
570
    uint32_t *udata;
571
    uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
572

    
573
    assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
574
    assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
575
    assert(pmin <= pmax);
576

    
577
    udata = av_malloc(n * sizeof(uint32_t));
578
    for (i = 0; i < n; i++)
579
        udata[i] = (2*data[i]) ^ (data[i]>>31);
580

    
581
    calc_sums(pmin, pmax, udata, n, pred_order, sums);
582

    
583
    opt_porder = pmin;
584
    bits[pmin] = UINT32_MAX;
585
    for (i = pmin; i <= pmax; i++) {
586
        bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
587
        if (bits[i] <= bits[opt_porder]) {
588
            opt_porder = i;
589
            *rc = tmp_rc;
590
        }
591
    }
592

    
593
    av_freep(&udata);
594
    return bits[opt_porder];
595
}
596

    
597

    
598
static int get_max_p_order(int max_porder, int n, int order)
599
{
600
    int porder = FFMIN(max_porder, av_log2(n^(n-1)));
601
    if (order > 0)
602
        porder = FFMIN(porder, av_log2(n/order));
603
    return porder;
604
}
605

    
606

    
607
static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmin, int pmax,
608
                                       int32_t *data, int n, int pred_order,
609
                                       int bps)
610
{
611
    uint32_t bits;
612
    pmin  = get_max_p_order(pmin, n, pred_order);
613
    pmax  = get_max_p_order(pmax, n, pred_order);
614
    bits  = pred_order * bps + 6;
615
    bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
616
    return bits;
617
}
618

    
619

    
620
static uint32_t calc_rice_params_lpc(RiceContext *rc, int pmin, int pmax,
621
                                     int32_t *data, int n, int pred_order,
622
                                     int bps, int precision)
623
{
624
    uint32_t bits;
625
    pmin  = get_max_p_order(pmin, n, pred_order);
626
    pmax  = get_max_p_order(pmax, n, pred_order);
627
    bits  = pred_order*bps + 4 + 5 + pred_order*precision + 6;
628
    bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
629
    return bits;
630
}
631

    
632

    
633
static void encode_residual_verbatim(int32_t *res, int32_t *smp, int n)
634
{
635
    assert(n > 0);
636
    memcpy(res, smp, n * sizeof(int32_t));
637
}
638

    
639

    
640
static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
641
                                  int order)
642
{
643
    int i;
644

    
645
    for (i = 0; i < order; i++)
646
        res[i] = smp[i];
647

    
648
    if (order == 0) {
649
        for (i = order; i < n; i++)
650
            res[i] = smp[i];
651
    } else if (order == 1) {
652
        for (i = order; i < n; i++)
653
            res[i] = smp[i] - smp[i-1];
654
    } else if (order == 2) {
655
        int a = smp[order-1] - smp[order-2];
656
        for (i = order; i < n; i += 2) {
657
            int b    = smp[i  ] - smp[i-1];
658
            res[i]   = b - a;
659
            a        = smp[i+1] - smp[i  ];
660
            res[i+1] = a - b;
661
        }
662
    } else if (order == 3) {
663
        int a = smp[order-1] -   smp[order-2];
664
        int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
665
        for (i = order; i < n; i += 2) {
666
            int b    = smp[i  ] - smp[i-1];
667
            int d    = b - a;
668
            res[i]   = d - c;
669
            a        = smp[i+1] - smp[i  ];
670
            c        = a - b;
671
            res[i+1] = c - d;
672
        }
673
    } else {
674
        int a = smp[order-1] -   smp[order-2];
675
        int c = smp[order-1] - 2*smp[order-2] +   smp[order-3];
676
        int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
677
        for (i = order; i < n; i += 2) {
678
            int b    = smp[i  ] - smp[i-1];
679
            int d    = b - a;
680
            int f    = d - c;
681
            res[i  ] = f - e;
682
            a        = smp[i+1] - smp[i  ];
683
            c        = a - b;
684
            e        = c - d;
685
            res[i+1] = e - f;
686
        }
687
    }
688
}
689

    
690

    
691
#define LPC1(x) {\
692
    int c = coefs[(x)-1];\
693
    p0   += c * s;\
694
    s     = smp[i-(x)+1];\
695
    p1   += c * s;\
696
}
697

    
698
static av_always_inline void encode_residual_lpc_unrolled(int32_t *res,
699
                                    const int32_t *smp, int n, int order,
700
                                    const int32_t *coefs, int shift, int big)
701
{
702
    int i;
703
    for (i = order; i < n; i += 2) {
704
        int s  = smp[i-order];
705
        int p0 = 0, p1 = 0;
706
        if (big) {
707
            switch (order) {
708
            case 32: LPC1(32)
709
            case 31: LPC1(31)
710
            case 30: LPC1(30)
711
            case 29: LPC1(29)
712
            case 28: LPC1(28)
713
            case 27: LPC1(27)
714
            case 26: LPC1(26)
715
            case 25: LPC1(25)
716
            case 24: LPC1(24)
717
            case 23: LPC1(23)
718
            case 22: LPC1(22)
719
            case 21: LPC1(21)
720
            case 20: LPC1(20)
721
            case 19: LPC1(19)
722
            case 18: LPC1(18)
723
            case 17: LPC1(17)
724
            case 16: LPC1(16)
725
            case 15: LPC1(15)
726
            case 14: LPC1(14)
727
            case 13: LPC1(13)
728
            case 12: LPC1(12)
729
            case 11: LPC1(11)
730
            case 10: LPC1(10)
731
            case  9: LPC1( 9)
732
                     LPC1( 8)
733
                     LPC1( 7)
734
                     LPC1( 6)
735
                     LPC1( 5)
736
                     LPC1( 4)
737
                     LPC1( 3)
738
                     LPC1( 2)
739
                     LPC1( 1)
740
            }
741
        } else {
742
            switch (order) {
743
            case  8: LPC1( 8)
744
            case  7: LPC1( 7)
745
            case  6: LPC1( 6)
746
            case  5: LPC1( 5)
747
            case  4: LPC1( 4)
748
            case  3: LPC1( 3)
749
            case  2: LPC1( 2)
750
            case  1: LPC1( 1)
751
            }
752
        }
753
        res[i  ] = smp[i  ] - (p0 >> shift);
754
        res[i+1] = smp[i+1] - (p1 >> shift);
755
    }
756
}
757

    
758

    
759
static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
760
                                int order, const int32_t *coefs, int shift)
761
{
762
    int i;
763
    for (i = 0; i < order; i++)
764
        res[i] = smp[i];
765
#if CONFIG_SMALL
766
    for (i = order; i < n; i += 2) {
767
        int j;
768
        int s  = smp[i];
769
        int p0 = 0, p1 = 0;
770
        for (j = 0; j < order; j++) {
771
            int c = coefs[j];
772
            p1   += c * s;
773
            s     = smp[i-j-1];
774
            p0   += c * s;
775
        }
776
        res[i  ] = smp[i  ] - (p0 >> shift);
777
        res[i+1] = smp[i+1] - (p1 >> shift);
778
    }
779
#else
780
    switch (order) {
781
    case  1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
782
    case  2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
783
    case  3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
784
    case  4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
785
    case  5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
786
    case  6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
787
    case  7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
788
    case  8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
789
    default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
790
    }
791
#endif
792
}
793

    
794

    
795
static int encode_residual(FlacEncodeContext *ctx, int ch)
796
{
797
    int i, n;
798
    int min_order, max_order, opt_order, precision, omethod;
799
    int min_porder, max_porder;
800
    FlacFrame *frame;
801
    FlacSubframe *sub;
802
    int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
803
    int shift[MAX_LPC_ORDER];
804
    int32_t *res, *smp;
805

    
806
    frame = &ctx->frame;
807
    sub   = &frame->subframes[ch];
808
    res   = sub->residual;
809
    smp   = sub->samples;
810
    n     = frame->blocksize;
811

    
812
    /* CONSTANT */
813
    for (i = 1; i < n; i++)
814
        if(smp[i] != smp[0])
815
            break;
816
    if (i == n) {
817
        sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
818
        res[0] = smp[0];
819
        return sub->obits;
820
    }
821

    
822
    /* VERBATIM */
823
    if (n < 5) {
824
        sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
825
        encode_residual_verbatim(res, smp, n);
826
        return sub->obits * n;
827
    }
828

    
829
    min_order  = ctx->options.min_prediction_order;
830
    max_order  = ctx->options.max_prediction_order;
831
    min_porder = ctx->options.min_partition_order;
832
    max_porder = ctx->options.max_partition_order;
833
    precision  = ctx->options.lpc_coeff_precision;
834
    omethod    = ctx->options.prediction_order_method;
835

    
836
    /* FIXED */
837
    if (ctx->options.lpc_type == AV_LPC_TYPE_NONE  ||
838
        ctx->options.lpc_type == AV_LPC_TYPE_FIXED || n <= max_order) {
839
        uint32_t bits[MAX_FIXED_ORDER+1];
840
        if (max_order > MAX_FIXED_ORDER)
841
            max_order = MAX_FIXED_ORDER;
842
        opt_order = 0;
843
        bits[0]   = UINT32_MAX;
844
        for (i = min_order; i <= max_order; i++) {
845
            encode_residual_fixed(res, smp, n, i);
846
            bits[i] = calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res,
847
                                             n, i, sub->obits);
848
            if (bits[i] < bits[opt_order])
849
                opt_order = i;
850
        }
851
        sub->order     = opt_order;
852
        sub->type      = FLAC_SUBFRAME_FIXED;
853
        sub->type_code = sub->type | sub->order;
854
        if (sub->order != max_order) {
855
            encode_residual_fixed(res, smp, n, sub->order);
856
            return calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res, n,
857
                                          sub->order, sub->obits);
858
        }
859
        return bits[sub->order];
860
    }
861

    
862
    /* LPC */
863
    opt_order = ff_lpc_calc_coefs(&ctx->dsp, smp, n, min_order, max_order,
864
                                  precision, coefs, shift, ctx->options.lpc_type,
865
                                  ctx->options.lpc_passes, omethod,
866
                                  MAX_LPC_SHIFT, 0);
867

    
868
    if (omethod == ORDER_METHOD_2LEVEL ||
869
        omethod == ORDER_METHOD_4LEVEL ||
870
        omethod == ORDER_METHOD_8LEVEL) {
871
        int levels = 1 << omethod;
872
        uint32_t bits[1 << ORDER_METHOD_8LEVEL];
873
        int order;
874
        int opt_index   = levels-1;
875
        opt_order       = max_order-1;
876
        bits[opt_index] = UINT32_MAX;
877
        for (i = levels-1; i >= 0; i--) {
878
            order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
879
            if (order < 0)
880
                order = 0;
881
            encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
882
            bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
883
                                           res, n, order+1, sub->obits, precision);
884
            if (bits[i] < bits[opt_index]) {
885
                opt_index = i;
886
                opt_order = order;
887
            }
888
        }
889
        opt_order++;
890
    } else if (omethod == ORDER_METHOD_SEARCH) {
891
        // brute-force optimal order search
892
        uint32_t bits[MAX_LPC_ORDER];
893
        opt_order = 0;
894
        bits[0]   = UINT32_MAX;
895
        for (i = min_order-1; i < max_order; i++) {
896
            encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
897
            bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
898
                                           res, n, i+1, sub->obits, precision);
899
            if (bits[i] < bits[opt_order])
900
                opt_order = i;
901
        }
902
        opt_order++;
903
    } else if (omethod == ORDER_METHOD_LOG) {
904
        uint32_t bits[MAX_LPC_ORDER];
905
        int step;
906

    
907
        opt_order = min_order - 1 + (max_order-min_order)/3;
908
        memset(bits, -1, sizeof(bits));
909

    
910
        for (step = 16; step; step >>= 1) {
911
            int last = opt_order;
912
            for (i = last-step; i <= last+step; i += step) {
913
                if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
914
                    continue;
915
                encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
916
                bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
917
                                               res, n, i+1, sub->obits,
918
                                               precision);
919
                if (bits[i] < bits[opt_order])
920
                    opt_order = i;
921
            }
922
        }
923
        opt_order++;
924
    }
925

    
926
    sub->order     = opt_order;
927
    sub->type      = FLAC_SUBFRAME_LPC;
928
    sub->type_code = sub->type | (sub->order-1);
929
    sub->shift     = shift[sub->order-1];
930
    for (i = 0; i < sub->order; i++)
931
        sub->coefs[i] = coefs[sub->order-1][i];
932

    
933
    encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
934

    
935
    return calc_rice_params_lpc(&sub->rc, min_porder, max_porder, res, n,
936
                                sub->order, sub->obits, precision);
937
}
938

    
939

    
940
static int encode_residual_v(FlacEncodeContext *ctx, int ch)
941
{
942
    int i, n;
943
    FlacFrame *frame;
944
    FlacSubframe *sub;
945
    int32_t *res, *smp;
946

    
947
    frame = &ctx->frame;
948
    sub   = &frame->subframes[ch];
949
    res   = sub->residual;
950
    smp   = sub->samples;
951
    n     = frame->blocksize;
952

    
953
    /* CONSTANT */
954
    for (i = 1; i < n; i++)
955
        if (smp[i] != smp[0])
956
            break;
957
    if (i == n) {
958
        sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
959
        res[0]    = smp[0];
960
        return sub->obits;
961
    }
962

    
963
    /* VERBATIM */
964
    sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
965
    encode_residual_verbatim(res, smp, n);
966
    return sub->obits * n;
967
}
968

    
969

    
970
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
971
{
972
    int i, best;
973
    int32_t lt, rt;
974
    uint64_t sum[4];
975
    uint64_t score[4];
976
    int k;
977

    
978
    /* calculate sum of 2nd order residual for each channel */
979
    sum[0] = sum[1] = sum[2] = sum[3] = 0;
980
    for (i = 2; i < n; i++) {
981
        lt = left_ch[i]  - 2*left_ch[i-1]  + left_ch[i-2];
982
        rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
983
        sum[2] += FFABS((lt + rt) >> 1);
984
        sum[3] += FFABS(lt - rt);
985
        sum[0] += FFABS(lt);
986
        sum[1] += FFABS(rt);
987
    }
988
    /* estimate bit counts */
989
    for (i = 0; i < 4; i++) {
990
        k      = find_optimal_param(2 * sum[i], n);
991
        sum[i] = rice_encode_count( 2 * sum[i], n, k);
992
    }
993

    
994
    /* calculate score for each mode */
995
    score[0] = sum[0] + sum[1];
996
    score[1] = sum[0] + sum[3];
997
    score[2] = sum[1] + sum[3];
998
    score[3] = sum[2] + sum[3];
999

    
1000
    /* return mode with lowest score */
1001
    best = 0;
1002
    for (i = 1; i < 4; i++)
1003
        if (score[i] < score[best])
1004
            best = i;
1005
    if (best == 0) {
1006
        return FLAC_CHMODE_INDEPENDENT;
1007
    } else if (best == 1) {
1008
        return FLAC_CHMODE_LEFT_SIDE;
1009
    } else if (best == 2) {
1010
        return FLAC_CHMODE_RIGHT_SIDE;
1011
    } else {
1012
        return FLAC_CHMODE_MID_SIDE;
1013
    }
1014
}
1015

    
1016

    
1017
/**
1018
 * Perform stereo channel decorrelation.
1019
 */
1020
static void channel_decorrelation(FlacEncodeContext *ctx)
1021
{
1022
    FlacFrame *frame;
1023
    int32_t *left, *right;
1024
    int i, n;
1025

    
1026
    frame = &ctx->frame;
1027
    n     = frame->blocksize;
1028
    left  = frame->subframes[0].samples;
1029
    right = frame->subframes[1].samples;
1030

    
1031
    if (ctx->channels != 2) {
1032
        frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
1033
        return;
1034
    }
1035

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

    
1038
    /* perform decorrelation and adjust bits-per-sample */
1039
    if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1040
        return;
1041
    if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1042
        int32_t tmp;
1043
        for (i = 0; i < n; i++) {
1044
            tmp      = left[i];
1045
            left[i]  = (tmp + right[i]) >> 1;
1046
            right[i] =  tmp - right[i];
1047
        }
1048
        frame->subframes[1].obits++;
1049
    } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1050
        for (i = 0; i < n; i++)
1051
            right[i] = left[i] - right[i];
1052
        frame->subframes[1].obits++;
1053
    } else {
1054
        for (i = 0; i < n; i++)
1055
            left[i] -= right[i];
1056
        frame->subframes[0].obits++;
1057
    }
1058
}
1059

    
1060

    
1061
static void write_utf8(PutBitContext *pb, uint32_t val)
1062
{
1063
    uint8_t tmp;
1064
    PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1065
}
1066

    
1067

    
1068
static void output_frame_header(FlacEncodeContext *s)
1069
{
1070
    FlacFrame *frame;
1071
    int crc;
1072

    
1073
    frame = &s->frame;
1074

    
1075
    put_bits(&s->pb, 16, 0xFFF8);
1076
    put_bits(&s->pb, 4, frame->bs_code[0]);
1077
    put_bits(&s->pb, 4, s->sr_code[0]);
1078

    
1079
    if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1080
        put_bits(&s->pb, 4, s->channels-1);
1081
    else
1082
        put_bits(&s->pb, 4, frame->ch_mode);
1083

    
1084
    put_bits(&s->pb, 3, 4); /* bits-per-sample code */
1085
    put_bits(&s->pb, 1, 0);
1086
    write_utf8(&s->pb, s->frame_count);
1087

    
1088
    if (frame->bs_code[0] == 6)
1089
        put_bits(&s->pb, 8, frame->bs_code[1]);
1090
    else if (frame->bs_code[0] == 7)
1091
        put_bits(&s->pb, 16, frame->bs_code[1]);
1092

    
1093
    if (s->sr_code[0] == 12)
1094
        put_bits(&s->pb, 8, s->sr_code[1]);
1095
    else if (s->sr_code[0] > 12)
1096
        put_bits(&s->pb, 16, s->sr_code[1]);
1097

    
1098
    flush_put_bits(&s->pb);
1099
    crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
1100
                 put_bits_count(&s->pb) >> 3);
1101
    put_bits(&s->pb, 8, crc);
1102
}
1103

    
1104

    
1105
static void output_subframe_constant(FlacEncodeContext *s, int ch)
1106
{
1107
    FlacSubframe *sub;
1108
    int32_t res;
1109

    
1110
    sub = &s->frame.subframes[ch];
1111
    res = sub->residual[0];
1112
    put_sbits(&s->pb, sub->obits, res);
1113
}
1114

    
1115

    
1116
static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
1117
{
1118
    int i;
1119
    FlacFrame *frame;
1120
    FlacSubframe *sub;
1121
    int32_t res;
1122

    
1123
    frame = &s->frame;
1124
    sub   = &frame->subframes[ch];
1125

    
1126
    for (i = 0; i < frame->blocksize; i++) {
1127
        res = sub->residual[i];
1128
        put_sbits(&s->pb, sub->obits, res);
1129
    }
1130
}
1131

    
1132

    
1133
static void output_residual(FlacEncodeContext *ctx, int ch)
1134
{
1135
    int i, j, p, n, parts;
1136
    int k, porder, psize, res_cnt;
1137
    FlacFrame *frame;
1138
    FlacSubframe *sub;
1139
    int32_t *res;
1140

    
1141
    frame = &ctx->frame;
1142
    sub   = &frame->subframes[ch];
1143
    res   = sub->residual;
1144
    n     = frame->blocksize;
1145

    
1146
    /* rice-encoded block */
1147
    put_bits(&ctx->pb, 2, 0);
1148

    
1149
    /* partition order */
1150
    porder  = sub->rc.porder;
1151
    psize   = n >> porder;
1152
    parts   = (1 << porder);
1153
    put_bits(&ctx->pb, 4, porder);
1154
    res_cnt = psize - sub->order;
1155

    
1156
    /* residual */
1157
    j = sub->order;
1158
    for (p = 0; p < parts; p++) {
1159
        k = sub->rc.params[p];
1160
        put_bits(&ctx->pb, 4, k);
1161
        if (p == 1)
1162
            res_cnt = psize;
1163
        for (i = 0; i < res_cnt && j < n; i++, j++)
1164
            set_sr_golomb_flac(&ctx->pb, res[j], k, INT32_MAX, 0);
1165
    }
1166
}
1167

    
1168

    
1169
static void output_subframe_fixed(FlacEncodeContext *ctx, int ch)
1170
{
1171
    int i;
1172
    FlacFrame *frame;
1173
    FlacSubframe *sub;
1174

    
1175
    frame = &ctx->frame;
1176
    sub   = &frame->subframes[ch];
1177

    
1178
    /* warm-up samples */
1179
    for (i = 0; i < sub->order; i++)
1180
        put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
1181

    
1182
    /* residual */
1183
    output_residual(ctx, ch);
1184
}
1185

    
1186

    
1187
static void output_subframe_lpc(FlacEncodeContext *ctx, int ch)
1188
{
1189
    int i, cbits;
1190
    FlacFrame *frame;
1191
    FlacSubframe *sub;
1192

    
1193
    frame = &ctx->frame;
1194
    sub = &frame->subframes[ch];
1195

    
1196
    /* warm-up samples */
1197
    for (i = 0; i < sub->order; i++)
1198
        put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
1199

    
1200
    /* LPC coefficients */
1201
    cbits = ctx->options.lpc_coeff_precision;
1202
    put_bits( &ctx->pb, 4, cbits-1);
1203
    put_sbits(&ctx->pb, 5, sub->shift);
1204
    for (i = 0; i < sub->order; i++)
1205
        put_sbits(&ctx->pb, cbits, sub->coefs[i]);
1206

    
1207
    /* residual */
1208
    output_residual(ctx, ch);
1209
}
1210

    
1211

    
1212
static void output_subframes(FlacEncodeContext *s)
1213
{
1214
    FlacFrame *frame;
1215
    FlacSubframe *sub;
1216
    int ch;
1217

    
1218
    frame = &s->frame;
1219

    
1220
    for (ch = 0; ch < s->channels; ch++) {
1221
        sub = &frame->subframes[ch];
1222

    
1223
        /* subframe header */
1224
        put_bits(&s->pb, 1, 0);
1225
        put_bits(&s->pb, 6, sub->type_code);
1226
        put_bits(&s->pb, 1, 0); /* no wasted bits */
1227

    
1228
        /* subframe */
1229
        if(sub->type == FLAC_SUBFRAME_CONSTANT)
1230
            output_subframe_constant(s, ch);
1231
        else if(sub->type == FLAC_SUBFRAME_VERBATIM)
1232
            output_subframe_verbatim(s, ch);
1233
        else if(sub->type == FLAC_SUBFRAME_FIXED)
1234
            output_subframe_fixed(s, ch);
1235
        else if(sub->type == FLAC_SUBFRAME_LPC)
1236
            output_subframe_lpc(s, ch);
1237
    }
1238
}
1239

    
1240

    
1241
static void output_frame_footer(FlacEncodeContext *s)
1242
{
1243
    int crc;
1244
    flush_put_bits(&s->pb);
1245
    crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
1246
                            put_bits_count(&s->pb)>>3));
1247
    put_bits(&s->pb, 16, crc);
1248
    flush_put_bits(&s->pb);
1249
}
1250

    
1251

    
1252
static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
1253
{
1254
#if HAVE_BIGENDIAN
1255
    int i;
1256
    for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1257
        int16_t smp = av_le2ne16(samples[i]);
1258
        av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
1259
    }
1260
#else
1261
    av_md5_update(s->md5ctx, (const uint8_t *)samples, s->frame.blocksize*s->channels*2);
1262
#endif
1263
}
1264

    
1265

    
1266
static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
1267
                             int buf_size, void *data)
1268
{
1269
    int ch;
1270
    FlacEncodeContext *s;
1271
    const int16_t *samples = data;
1272
    int out_bytes;
1273
    int reencoded=0;
1274

    
1275
    s = avctx->priv_data;
1276

    
1277
    if (buf_size < s->max_framesize * 2) {
1278
        av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
1279
        return 0;
1280
    }
1281

    
1282
    /* when the last block is reached, update the header in extradata */
1283
    if (!data) {
1284
        s->max_framesize = s->max_encoded_framesize;
1285
        av_md5_final(s->md5ctx, s->md5sum);
1286
        write_streaminfo(s, avctx->extradata);
1287
        return 0;
1288
    }
1289

    
1290
    init_frame(s);
1291

    
1292
    copy_samples(s, samples);
1293

    
1294
    channel_decorrelation(s);
1295

    
1296
    for (ch = 0; ch < s->channels; ch++)
1297
        encode_residual(s, ch);
1298

    
1299
write_frame:
1300
    init_put_bits(&s->pb, frame, buf_size);
1301
    output_frame_header(s);
1302
    output_subframes(s);
1303
    output_frame_footer(s);
1304
    out_bytes = put_bits_count(&s->pb) >> 3;
1305

    
1306
    if (out_bytes > s->max_framesize) {
1307
        if (reencoded) {
1308
            /* still too large. must be an error. */
1309
            av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
1310
            return -1;
1311
        }
1312

    
1313
        /* frame too large. use verbatim mode */
1314
        for (ch = 0; ch < s->channels; ch++)
1315
            encode_residual_v(s, ch);
1316
        reencoded = 1;
1317
        goto write_frame;
1318
    }
1319

    
1320
    s->frame_count++;
1321
    s->sample_count += avctx->frame_size;
1322
    update_md5_sum(s, samples);
1323
    if (out_bytes > s->max_encoded_framesize)
1324
        s->max_encoded_framesize = out_bytes;
1325
    if (out_bytes < s->min_framesize)
1326
        s->min_framesize = out_bytes;
1327

    
1328
    return out_bytes;
1329
}
1330

    
1331

    
1332
static av_cold int flac_encode_close(AVCodecContext *avctx)
1333
{
1334
    if (avctx->priv_data) {
1335
        FlacEncodeContext *s = avctx->priv_data;
1336
        av_freep(&s->md5ctx);
1337
    }
1338
    av_freep(&avctx->extradata);
1339
    avctx->extradata_size = 0;
1340
    av_freep(&avctx->coded_frame);
1341
    return 0;
1342
}
1343

    
1344

    
1345
AVCodec flac_encoder = {
1346
    "flac",
1347
    AVMEDIA_TYPE_AUDIO,
1348
    CODEC_ID_FLAC,
1349
    sizeof(FlacEncodeContext),
1350
    flac_encode_init,
1351
    flac_encode_frame,
1352
    flac_encode_close,
1353
    NULL,
1354
    .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
1355
    .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
1356
    .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
1357
};