Statistics
| Branch: | Revision:

ffmpeg / libavcodec / flacenc.c @ 89912b5f

History | View | Annotate | Download (39.9 KB)

1 9e96ab03 Michael Niedermayer
/**
2
 * FLAC audio encoder
3 38c1a5c4 Justin Ruggles
 * Copyright (c) 2006  Justin Ruggles <justin.ruggles@gmail.com>
4 9e96ab03 Michael Niedermayer
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 9e96ab03 Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 9e96ab03 Michael Niedermayer
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 9e96ab03 Michael Niedermayer
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 9e96ab03 Michael Niedermayer
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22 245976da Diego Biurrun
#include "libavutil/crc.h"
23 a9f8587e Justin Ruggles
#include "libavutil/md5.h"
24 9e96ab03 Michael Niedermayer
#include "avcodec.h"
25 9106a698 Stefano Sabatini
#include "get_bits.h"
26 6810b93a Loren Merritt
#include "dsputil.h"
27 9e96ab03 Michael Niedermayer
#include "golomb.h"
28 c50da3ad Ramiro Polla
#include "lpc.h"
29 07d16e2e Justin Ruggles
#include "flac.h"
30 d4df4e50 Justin Ruggles
#include "flacdata.h"
31 9e96ab03 Michael Niedermayer
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 a403fc03 Justin Ruggles
#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 23940f14 Justin Ruggles
    enum AVLPCType lpc_type;
48
    int lpc_passes;
49 a403fc03 Justin Ruggles
    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 e71bcc37 Justin Ruggles
typedef struct RiceContext {
58
    int porder;
59 a403fc03 Justin Ruggles
    int params[MAX_PARTITIONS];
60 e71bcc37 Justin Ruggles
} RiceContext;
61
62 9e96ab03 Michael Niedermayer
typedef struct FlacSubframe {
63
    int type;
64
    int type_code;
65
    int obits;
66
    int order;
67 a403fc03 Justin Ruggles
    int32_t coefs[MAX_LPC_ORDER];
68
    int shift;
69 e71bcc37 Justin Ruggles
    RiceContext rc;
70 9e96ab03 Michael Niedermayer
    int32_t samples[FLAC_MAX_BLOCKSIZE];
71 0d2caa37 Loren Merritt
    int32_t residual[FLAC_MAX_BLOCKSIZE+1];
72 9e96ab03 Michael Niedermayer
} FlacSubframe;
73
74
typedef struct FlacFrame {
75 07d16e2e Justin Ruggles
    FlacSubframe subframes[FLAC_MAX_CHANNELS];
76 9e96ab03 Michael Niedermayer
    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 f1216221 Justin Ruggles
    int channels;
85
    int samplerate;
86 9e96ab03 Michael Niedermayer
    int sr_code[2];
87 0bc08ed9 Justin Ruggles
    int max_blocksize;
88 6682ae42 Justin Ruggles
    int min_framesize;
89 f1216221 Justin Ruggles
    int max_framesize;
90 6682ae42 Justin Ruggles
    int max_encoded_framesize;
91 9e96ab03 Michael Niedermayer
    uint32_t frame_count;
92 e1a12934 Justin Ruggles
    uint64_t sample_count;
93 a9f8587e Justin Ruggles
    uint8_t md5sum[16];
94 9e96ab03 Michael Niedermayer
    FlacFrame frame;
95 a403fc03 Justin Ruggles
    CompressionOptions options;
96 e71bcc37 Justin Ruggles
    AVCodecContext *avctx;
97 6810b93a Loren Merritt
    DSPContext dsp;
98 a9f8587e Justin Ruggles
    struct AVMD5 *md5ctx;
99 9e96ab03 Michael Niedermayer
} FlacEncodeContext;
100
101 e35b689e Justin Ruggles
102 9e96ab03 Michael Niedermayer
/**
103 e35b689e Justin Ruggles
 * Write streaminfo metadata block to byte array.
104 9e96ab03 Michael Niedermayer
 */
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 0bc08ed9 Justin Ruggles
    put_bits(&pb, 16, s->max_blocksize);
114
    put_bits(&pb, 16, s->max_blocksize);
115 6682ae42 Justin Ruggles
    put_bits(&pb, 24, s->min_framesize);
116 9e96ab03 Michael Niedermayer
    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 e1a12934 Justin Ruggles
    /* 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 9e96ab03 Michael Niedermayer
    flush_put_bits(&pb);
124 a9f8587e Justin Ruggles
    memcpy(&header[18], s->md5sum, 16);
125 9e96ab03 Michael Niedermayer
}
126
127 e35b689e Justin Ruggles
128 9e96ab03 Michael Niedermayer
/**
129 e35b689e Justin Ruggles
 * Set blocksize based on samplerate.
130
 * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
131 9e96ab03 Michael Niedermayer
 */
132 a403fc03 Justin Ruggles
static int select_blocksize(int samplerate, int block_time_ms)
133 9e96ab03 Michael Niedermayer
{
134
    int i;
135
    int target;
136
    int blocksize;
137
138
    assert(samplerate > 0);
139 d4df4e50 Justin Ruggles
    blocksize = ff_flac_blocksize_table[1];
140 e35b689e Justin Ruggles
    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 d4df4e50 Justin Ruggles
            blocksize = ff_flac_blocksize_table[i];
145 9e96ab03 Michael Niedermayer
        }
146
    }
147
    return blocksize;
148
}
149
150 e35b689e Justin Ruggles
151 7fe0c7c8 Justin Ruggles
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 98a6fff9 Zuxy Meng
static av_cold int flac_encode_init(AVCodecContext *avctx)
210 9e96ab03 Michael Niedermayer
{
211
    int freq = avctx->sample_rate;
212
    int channels = avctx->channels;
213
    FlacEncodeContext *s = avctx->priv_data;
214 7c69b830 Michael Niedermayer
    int i, level;
215 9e96ab03 Michael Niedermayer
    uint8_t *streaminfo;
216
217 e71bcc37 Justin Ruggles
    s->avctx = avctx;
218
219 6810b93a Loren Merritt
    dsputil_init(&s->dsp, avctx);
220
221 e35b689e Justin Ruggles
    if (avctx->sample_fmt != SAMPLE_FMT_S16)
222 9e96ab03 Michael Niedermayer
        return -1;
223
224 e35b689e Justin Ruggles
    if (channels < 1 || channels > FLAC_MAX_CHANNELS)
225 9e96ab03 Michael Niedermayer
        return -1;
226
    s->channels = channels;
227
228
    /* find samplerate in table */
229 e35b689e Justin Ruggles
    if (freq < 1)
230 9e96ab03 Michael Niedermayer
        return -1;
231 e35b689e Justin Ruggles
    for (i = 4; i < 12; i++) {
232
        if (freq == ff_flac_sample_rate_table[i]) {
233 d4df4e50 Justin Ruggles
            s->samplerate = ff_flac_sample_rate_table[i];
234 9e96ab03 Michael Niedermayer
            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 e35b689e Justin Ruggles
    if (i == 12) {
241
        if (freq % 1000 == 0 && freq < 255000) {
242 9e96ab03 Michael Niedermayer
            s->sr_code[0] = 12;
243
            s->sr_code[1] = freq / 1000;
244 e35b689e Justin Ruggles
        } else if (freq % 10 == 0 && freq < 655350) {
245 9e96ab03 Michael Niedermayer
            s->sr_code[0] = 14;
246
            s->sr_code[1] = freq / 10;
247 e35b689e Justin Ruggles
        } else if (freq < 65535) {
248 9e96ab03 Michael Niedermayer
            s->sr_code[0] = 13;
249
            s->sr_code[1] = freq;
250
        } else {
251
            return -1;
252
        }
253
        s->samplerate = freq;
254
    }
255
256 a403fc03 Justin Ruggles
    /* set compression option defaults based on avctx->compression_level */
257 e35b689e Justin Ruggles
    if (avctx->compression_level < 0)
258 a403fc03 Justin Ruggles
        s->options.compression_level = 5;
259 e35b689e Justin Ruggles
    else
260 a403fc03 Justin Ruggles
        s->options.compression_level = avctx->compression_level;
261
262 e35b689e Justin Ruggles
    level = s->options.compression_level;
263
    if (level > 12) {
264 a403fc03 Justin Ruggles
        av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
265
               s->options.compression_level);
266
        return -1;
267
    }
268
269 e35b689e Justin Ruggles
    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 26053bdc Justin Ruggles
    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 5f3acd9e Justin Ruggles
                                                   ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG,    ORDER_METHOD_4LEVEL,
283
                                                   ORDER_METHOD_LOG,    ORDER_METHOD_SEARCH, ORDER_METHOD_LOG,
284 26053bdc Justin Ruggles
                                                   ORDER_METHOD_SEARCH})[level];
285 e35b689e Justin Ruggles
286 e1bbc032 Justin Ruggles
    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 7c69b830 Michael Niedermayer
289 a403fc03 Justin Ruggles
    /* set compression option overrides from AVCodecContext */
290 23940f14 Justin Ruggles
#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 a403fc03 Justin Ruggles
                return -1;
315 23940f14 Justin Ruggles
            } else {
316
                s->options.lpc_passes = avctx->lpc_passes;
317 a403fc03 Justin Ruggles
            }
318 23940f14 Justin Ruggles
        }
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 e35b689e Justin Ruggles
            if (avctx->min_prediction_order > MAX_FIXED_ORDER) {
326 a403fc03 Justin Ruggles
                av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
327
                       avctx->min_prediction_order);
328
                return -1;
329
            }
330 e35b689e Justin Ruggles
        } else if (avctx->min_prediction_order < MIN_LPC_ORDER ||
331
                   avctx->min_prediction_order > MAX_LPC_ORDER) {
332 23940f14 Justin Ruggles
            av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
333
                   avctx->min_prediction_order);
334
            return -1;
335 a403fc03 Justin Ruggles
        }
336
        s->options.min_prediction_order = avctx->min_prediction_order;
337
    }
338 23940f14 Justin Ruggles
    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 e35b689e Justin Ruggles
            if (avctx->max_prediction_order > MAX_FIXED_ORDER) {
343 a403fc03 Justin Ruggles
                av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
344
                       avctx->max_prediction_order);
345
                return -1;
346
            }
347 23940f14 Justin Ruggles
        } 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 a403fc03 Justin Ruggles
        }
353
        s->options.max_prediction_order = avctx->max_prediction_order;
354
    }
355 e35b689e Justin Ruggles
    if (s->options.max_prediction_order < s->options.min_prediction_order) {
356 a403fc03 Justin Ruggles
        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 e35b689e Justin Ruggles
    if (avctx->prediction_order_method >= 0) {
362
        if (avctx->prediction_order_method > ORDER_METHOD_LOG) {
363 a403fc03 Justin Ruggles
            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 e35b689e Justin Ruggles
    if (avctx->min_partition_order >= 0) {
371
        if (avctx->min_partition_order > MAX_PARTITION_ORDER) {
372 a403fc03 Justin Ruggles
            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 e35b689e Justin Ruggles
    if (avctx->max_partition_order >= 0) {
379
        if (avctx->max_partition_order > MAX_PARTITION_ORDER) {
380 a403fc03 Justin Ruggles
            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 e35b689e Justin Ruggles
    if (s->options.max_partition_order < s->options.min_partition_order) {
387 a403fc03 Justin Ruggles
        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 e35b689e Justin Ruggles
    if (avctx->frame_size > 0) {
393
        if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
394 c6375bf2 Justin Ruggles
                avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
395 a403fc03 Justin Ruggles
            av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
396
                   avctx->frame_size);
397
            return -1;
398
        }
399
    } else {
400 42ceeb5f Justin Ruggles
        s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
401 a403fc03 Justin Ruggles
    }
402 0bc08ed9 Justin Ruggles
    s->max_blocksize = s->avctx->frame_size;
403 a403fc03 Justin Ruggles
404
    /* set LPC precision */
405 e35b689e Justin Ruggles
    if (avctx->lpc_coeff_precision > 0) {
406
        if (avctx->lpc_coeff_precision > MAX_LPC_PRECISION) {
407 a403fc03 Justin Ruggles
            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 fe790f89 Justin Ruggles
        /* default LPC precision */
414
        s->options.lpc_coeff_precision = 15;
415 a403fc03 Justin Ruggles
    }
416 9e96ab03 Michael Niedermayer
417 f33aa120 Michael Niedermayer
    /* set maximum encoded frame size in verbatim mode */
418 0fb2182d Justin Ruggles
    s->max_framesize = ff_flac_get_max_frame_size(s->avctx->frame_size,
419
                                                  s->channels, 16);
420 9e96ab03 Michael Niedermayer
421 a9f8587e Justin Ruggles
    /* initialize MD5 context */
422
    s->md5ctx = av_malloc(av_md5_size);
423 e35b689e Justin Ruggles
    if (!s->md5ctx)
424 2874c81c Stefano Sabatini
        return AVERROR(ENOMEM);
425 a9f8587e Justin Ruggles
    av_md5_init(s->md5ctx);
426
427 9e96ab03 Michael Niedermayer
    streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
428 e08ec714 Justin Ruggles
    if (!streaminfo)
429
        return AVERROR(ENOMEM);
430 9e96ab03 Michael Niedermayer
    write_streaminfo(s, streaminfo);
431
    avctx->extradata = streaminfo;
432
    avctx->extradata_size = FLAC_STREAMINFO_SIZE;
433
434 e35b689e Justin Ruggles
    s->frame_count   = 0;
435 b9b4fc5e Justin Ruggles
    s->min_framesize = s->max_framesize;
436 9e96ab03 Michael Niedermayer
437
    avctx->coded_frame = avcodec_alloc_frame();
438 e08ec714 Justin Ruggles
    if (!avctx->coded_frame)
439
        return AVERROR(ENOMEM);
440 9e96ab03 Michael Niedermayer
441 7fe0c7c8 Justin Ruggles
    dprint_compression_options(s);
442
443 9e96ab03 Michael Niedermayer
    return 0;
444
}
445
446 e35b689e Justin Ruggles
447 f33aa120 Michael Niedermayer
static void init_frame(FlacEncodeContext *s)
448 9e96ab03 Michael Niedermayer
{
449
    int i, ch;
450
    FlacFrame *frame;
451
452
    frame = &s->frame;
453
454 e35b689e Justin Ruggles
    for (i = 0; i < 16; i++) {
455
        if (s->avctx->frame_size == ff_flac_blocksize_table[i]) {
456
            frame->blocksize  = ff_flac_blocksize_table[i];
457 9e96ab03 Michael Niedermayer
            frame->bs_code[0] = i;
458
            frame->bs_code[1] = 0;
459
            break;
460
        }
461
    }
462 e35b689e Justin Ruggles
    if (i == 16) {
463 42ceeb5f Justin Ruggles
        frame->blocksize = s->avctx->frame_size;
464 e35b689e Justin Ruggles
        if (frame->blocksize <= 256) {
465 9e96ab03 Michael Niedermayer
            frame->bs_code[0] = 6;
466
            frame->bs_code[1] = frame->blocksize-1;
467
        } else {
468
            frame->bs_code[0] = 7;
469
            frame->bs_code[1] = frame->blocksize-1;
470
        }
471
    }
472
473 e35b689e Justin Ruggles
    for (ch = 0; ch < s->channels; ch++)
474 9e96ab03 Michael Niedermayer
        frame->subframes[ch].obits = 16;
475
}
476
477 e35b689e Justin Ruggles
478 9e96ab03 Michael Niedermayer
/**
479 e35b689e Justin Ruggles
 * Copy channel-interleaved input samples into separate subframes.
480 9e96ab03 Michael Niedermayer
 */
481 edac49da Reimar Döffinger
static void copy_samples(FlacEncodeContext *s, const int16_t *samples)
482 9e96ab03 Michael Niedermayer
{
483
    int i, j, ch;
484
    FlacFrame *frame;
485
486
    frame = &s->frame;
487 e35b689e Justin Ruggles
    for (i = 0, j = 0; i < frame->blocksize; i++)
488
        for (ch = 0; ch < s->channels; ch++, j++)
489 9e96ab03 Michael Niedermayer
            frame->subframes[ch].samples[i] = samples[j];
490
}
491
492 e71bcc37 Justin Ruggles
493
#define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
494
495 14bd2a9f Loren Merritt
/**
496 e35b689e Justin Ruggles
 * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
497 14bd2a9f Loren Merritt
 */
498 e71bcc37 Justin Ruggles
static int find_optimal_param(uint32_t sum, int n)
499
{
500 14bd2a9f Loren Merritt
    int k;
501
    uint32_t sum2;
502
503 e35b689e Justin Ruggles
    if (sum <= n >> 1)
504 14bd2a9f Loren Merritt
        return 0;
505 e35b689e Justin Ruggles
    sum2 = sum - (n >> 1);
506
    k    = av_log2(n < 256 ? FASTDIV(sum2, n) : sum2 / n);
507 14bd2a9f Loren Merritt
    return FFMIN(k, MAX_RICE_PARAM);
508 e71bcc37 Justin Ruggles
}
509
510 e35b689e Justin Ruggles
511 e71bcc37 Justin Ruggles
static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
512
                                         uint32_t *sums, int n, int pred_order)
513
{
514
    int i;
515
    int k, cnt, part;
516
    uint32_t all_bits;
517
518 e35b689e Justin Ruggles
    part     = (1 << porder);
519 90f03441 Loren Merritt
    all_bits = 4 * part;
520 e71bcc37 Justin Ruggles
521
    cnt = (n >> porder) - pred_order;
522 e35b689e Justin Ruggles
    for (i = 0; i < part; i++) {
523 e71bcc37 Justin Ruggles
        k = find_optimal_param(sums[i], cnt);
524
        rc->params[i] = k;
525
        all_bits += rice_encode_count(sums[i], cnt, k);
526 90f03441 Loren Merritt
        cnt = n >> porder;
527 e71bcc37 Justin Ruggles
    }
528
529
    rc->porder = porder;
530
531
    return all_bits;
532
}
533
534 e35b689e Justin Ruggles
535 a403fc03 Justin Ruggles
static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
536
                      uint32_t sums[][MAX_PARTITIONS])
537 e71bcc37 Justin Ruggles
{
538
    int i, j;
539 78f67b7a Michael Niedermayer
    int parts;
540
    uint32_t *res, *res_end;
541 e71bcc37 Justin Ruggles
542
    /* sums for highest level */
543 e35b689e Justin Ruggles
    parts   = (1 << pmax);
544
    res     = &data[pred_order];
545 78f67b7a Michael Niedermayer
    res_end = &data[n >> pmax];
546 e35b689e Justin Ruggles
    for (i = 0; i < parts; i++) {
547 f6215b1b Loren Merritt
        uint32_t sum = 0;
548 e35b689e Justin Ruggles
        while (res < res_end)
549 f6215b1b Loren Merritt
            sum += *(res++);
550
        sums[pmax][i] = sum;
551 e35b689e Justin Ruggles
        res_end += n >> pmax;
552 e71bcc37 Justin Ruggles
    }
553
    /* sums for lower levels */
554 e35b689e Justin Ruggles
    for (i = pmax - 1; i >= pmin; i--) {
555 e71bcc37 Justin Ruggles
        parts = (1 << i);
556 e35b689e Justin Ruggles
        for (j = 0; j < parts; j++)
557 e71bcc37 Justin Ruggles
            sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
558
    }
559
}
560
561 e35b689e Justin Ruggles
562 a403fc03 Justin Ruggles
static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
563
                                 int32_t *data, int n, int pred_order)
564 e71bcc37 Justin Ruggles
{
565
    int i;
566 a403fc03 Justin Ruggles
    uint32_t bits[MAX_PARTITION_ORDER+1];
567 e71bcc37 Justin Ruggles
    int opt_porder;
568 a403fc03 Justin Ruggles
    RiceContext tmp_rc;
569 e71bcc37 Justin Ruggles
    uint32_t *udata;
570 a403fc03 Justin Ruggles
    uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
571 e71bcc37 Justin Ruggles
572 a403fc03 Justin Ruggles
    assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
573
    assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
574
    assert(pmin <= pmax);
575 e71bcc37 Justin Ruggles
576
    udata = av_malloc(n * sizeof(uint32_t));
577 e35b689e Justin Ruggles
    for (i = 0; i < n; i++)
578 e71bcc37 Justin Ruggles
        udata[i] = (2*data[i]) ^ (data[i]>>31);
579
580 a403fc03 Justin Ruggles
    calc_sums(pmin, pmax, udata, n, pred_order, sums);
581 e71bcc37 Justin Ruggles
582 a403fc03 Justin Ruggles
    opt_porder = pmin;
583
    bits[pmin] = UINT32_MAX;
584 e35b689e Justin Ruggles
    for (i = pmin; i <= pmax; i++) {
585 a403fc03 Justin Ruggles
        bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
586 e35b689e Justin Ruggles
        if (bits[i] <= bits[opt_porder]) {
587 e71bcc37 Justin Ruggles
            opt_porder = i;
588 e35b689e Justin Ruggles
            *rc = tmp_rc;
589 e71bcc37 Justin Ruggles
        }
590
    }
591
592
    av_freep(&udata);
593 a403fc03 Justin Ruggles
    return bits[opt_porder];
594 e71bcc37 Justin Ruggles
}
595
596 e35b689e Justin Ruggles
597 1e5707b7 Justin Ruggles
static int get_max_p_order(int max_porder, int n, int order)
598
{
599
    int porder = FFMIN(max_porder, av_log2(n^(n-1)));
600 e35b689e Justin Ruggles
    if (order > 0)
601 1e5707b7 Justin Ruggles
        porder = FFMIN(porder, av_log2(n/order));
602
    return porder;
603
}
604
605 e35b689e Justin Ruggles
606 a403fc03 Justin Ruggles
static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmin, int pmax,
607
                                       int32_t *data, int n, int pred_order,
608
                                       int bps)
609 e71bcc37 Justin Ruggles
{
610
    uint32_t bits;
611 e35b689e Justin Ruggles
    pmin  = get_max_p_order(pmin, n, pred_order);
612
    pmax  = get_max_p_order(pmax, n, pred_order);
613
    bits  = pred_order * bps + 6;
614 a403fc03 Justin Ruggles
    bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
615
    return bits;
616
}
617
618 e35b689e Justin Ruggles
619 a403fc03 Justin Ruggles
static uint32_t calc_rice_params_lpc(RiceContext *rc, int pmin, int pmax,
620
                                     int32_t *data, int n, int pred_order,
621
                                     int bps, int precision)
622
{
623
    uint32_t bits;
624 e35b689e Justin Ruggles
    pmin  = get_max_p_order(pmin, n, pred_order);
625
    pmax  = get_max_p_order(pmax, n, pred_order);
626
    bits  = pred_order*bps + 4 + 5 + pred_order*precision + 6;
627 a403fc03 Justin Ruggles
    bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
628 e71bcc37 Justin Ruggles
    return bits;
629
}
630
631 e35b689e Justin Ruggles
632 e71bcc37 Justin Ruggles
static void encode_residual_verbatim(int32_t *res, int32_t *smp, int n)
633
{
634
    assert(n > 0);
635
    memcpy(res, smp, n * sizeof(int32_t));
636
}
637
638 e35b689e Justin Ruggles
639 a403fc03 Justin Ruggles
static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
640
                                  int order)
641 e71bcc37 Justin Ruggles
{
642
    int i;
643
644 e35b689e Justin Ruggles
    for (i = 0; i < order; i++)
645 e71bcc37 Justin Ruggles
        res[i] = smp[i];
646
647 e35b689e Justin Ruggles
    if (order == 0) {
648
        for (i = order; i < n; i++)
649
            res[i] = smp[i];
650
    } else if (order == 1) {
651
        for (i = order; i < n; i++)
652
            res[i] = smp[i] - smp[i-1];
653
    } else if (order == 2) {
654 a309dce7 Loren Merritt
        int a = smp[order-1] - smp[order-2];
655 e35b689e Justin Ruggles
        for (i = order; i < n; i += 2) {
656
            int b    = smp[i  ] - smp[i-1];
657
            res[i]   = b - a;
658
            a        = smp[i+1] - smp[i  ];
659
            res[i+1] = a - b;
660 a309dce7 Loren Merritt
        }
661 e35b689e Justin Ruggles
    } else if (order == 3) {
662
        int a = smp[order-1] -   smp[order-2];
663 a309dce7 Loren Merritt
        int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
664 e35b689e Justin Ruggles
        for (i = order; i < n; i += 2) {
665
            int b    = smp[i  ] - smp[i-1];
666
            int d    = b - a;
667
            res[i]   = d - c;
668
            a        = smp[i+1] - smp[i  ];
669
            c        = a - b;
670
            res[i+1] = c - d;
671 a309dce7 Loren Merritt
        }
672 e35b689e Justin Ruggles
    } else {
673
        int a = smp[order-1] -   smp[order-2];
674
        int c = smp[order-1] - 2*smp[order-2] +   smp[order-3];
675 a309dce7 Loren Merritt
        int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
676 e35b689e Justin Ruggles
        for (i = order; i < n; i += 2) {
677
            int b    = smp[i  ] - smp[i-1];
678
            int d    = b - a;
679
            int f    = d - c;
680
            res[i  ] = f - e;
681
            a        = smp[i+1] - smp[i  ];
682
            c        = a - b;
683
            e        = c - d;
684
            res[i+1] = e - f;
685 a309dce7 Loren Merritt
        }
686 e71bcc37 Justin Ruggles
    }
687
}
688
689 e35b689e Justin Ruggles
690 dc44d4ad Loren Merritt
#define LPC1(x) {\
691 b8de3429 Loren Merritt
    int c = coefs[(x)-1];\
692 e35b689e Justin Ruggles
    p0   += c * s;\
693
    s     = smp[i-(x)+1];\
694
    p1   += c * s;\
695 dc44d4ad Loren Merritt
}
696
697 e35b689e Justin Ruggles
static av_always_inline void encode_residual_lpc_unrolled(int32_t *res,
698
                                    const int32_t *smp, int n, int order,
699
                                    const int32_t *coefs, int shift, int big)
700 dc44d4ad Loren Merritt
{
701
    int i;
702 e35b689e Justin Ruggles
    for (i = order; i < n; i += 2) {
703
        int s  = smp[i-order];
704 b8de3429 Loren Merritt
        int p0 = 0, p1 = 0;
705 e35b689e Justin Ruggles
        if (big) {
706
            switch (order) {
707
            case 32: LPC1(32)
708
            case 31: LPC1(31)
709
            case 30: LPC1(30)
710
            case 29: LPC1(29)
711
            case 28: LPC1(28)
712
            case 27: LPC1(27)
713
            case 26: LPC1(26)
714
            case 25: LPC1(25)
715
            case 24: LPC1(24)
716
            case 23: LPC1(23)
717
            case 22: LPC1(22)
718
            case 21: LPC1(21)
719
            case 20: LPC1(20)
720
            case 19: LPC1(19)
721
            case 18: LPC1(18)
722
            case 17: LPC1(17)
723
            case 16: LPC1(16)
724
            case 15: LPC1(15)
725
            case 14: LPC1(14)
726
            case 13: LPC1(13)
727
            case 12: LPC1(12)
728
            case 11: LPC1(11)
729
            case 10: LPC1(10)
730
            case  9: LPC1( 9)
731
                     LPC1( 8)
732
                     LPC1( 7)
733
                     LPC1( 6)
734
                     LPC1( 5)
735
                     LPC1( 4)
736
                     LPC1( 3)
737
                     LPC1( 2)
738
                     LPC1( 1)
739 dc44d4ad Loren Merritt
            }
740
        } else {
741 e35b689e Justin Ruggles
            switch (order) {
742
            case  8: LPC1( 8)
743
            case  7: LPC1( 7)
744
            case  6: LPC1( 6)
745
            case  5: LPC1( 5)
746
            case  4: LPC1( 4)
747
            case  3: LPC1( 3)
748
            case  2: LPC1( 2)
749
            case  1: LPC1( 1)
750 dc44d4ad Loren Merritt
            }
751
        }
752
        res[i  ] = smp[i  ] - (p0 >> shift);
753
        res[i+1] = smp[i+1] - (p1 >> shift);
754
    }
755
}
756
757 e35b689e Justin Ruggles
758 a403fc03 Justin Ruggles
static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
759
                                int order, const int32_t *coefs, int shift)
760
{
761 dc44d4ad Loren Merritt
    int i;
762 e35b689e Justin Ruggles
    for (i = 0; i < order; i++)
763 a403fc03 Justin Ruggles
        res[i] = smp[i];
764 b250f9c6 Aurelien Jacobs
#if CONFIG_SMALL
765 e35b689e Justin Ruggles
    for (i = order; i < n; i += 2) {
766 dc44d4ad Loren Merritt
        int j;
767 e35b689e Justin Ruggles
        int s  = smp[i];
768 b8de3429 Loren Merritt
        int p0 = 0, p1 = 0;
769 e35b689e Justin Ruggles
        for (j = 0; j < order; j++) {
770 b8de3429 Loren Merritt
            int c = coefs[j];
771 e35b689e Justin Ruggles
            p1   += c * s;
772
            s     = smp[i-j-1];
773
            p0   += c * s;
774 a403fc03 Justin Ruggles
        }
775 b8de3429 Loren Merritt
        res[i  ] = smp[i  ] - (p0 >> shift);
776 f74471e0 Loren Merritt
        res[i+1] = smp[i+1] - (p1 >> shift);
777 a403fc03 Justin Ruggles
    }
778 dc44d4ad Loren Merritt
#else
779 e35b689e Justin Ruggles
    switch (order) {
780
    case  1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
781
    case  2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
782
    case  3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
783
    case  4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
784
    case  5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
785
    case  6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
786
    case  7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
787
    case  8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
788
    default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
789 dc44d4ad Loren Merritt
    }
790
#endif
791 a403fc03 Justin Ruggles
}
792
793 e35b689e Justin Ruggles
794 15f537ed Justin Ruggles
static int encode_residual(FlacEncodeContext *s, int ch)
795 e71bcc37 Justin Ruggles
{
796 a403fc03 Justin Ruggles
    int i, n;
797 26053bdc Justin Ruggles
    int min_order, max_order, opt_order, precision, omethod;
798 1e5707b7 Justin Ruggles
    int min_porder, max_porder;
799 e71bcc37 Justin Ruggles
    FlacFrame *frame;
800
    FlacSubframe *sub;
801 a403fc03 Justin Ruggles
    int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
802
    int shift[MAX_LPC_ORDER];
803 e71bcc37 Justin Ruggles
    int32_t *res, *smp;
804
805 15f537ed Justin Ruggles
    frame = &s->frame;
806 e35b689e Justin Ruggles
    sub   = &frame->subframes[ch];
807
    res   = sub->residual;
808
    smp   = sub->samples;
809
    n     = frame->blocksize;
810 e71bcc37 Justin Ruggles
811
    /* CONSTANT */
812 e35b689e Justin Ruggles
    for (i = 1; i < n; i++)
813
        if(smp[i] != smp[0])
814
            break;
815
    if (i == n) {
816 e71bcc37 Justin Ruggles
        sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
817
        res[0] = smp[0];
818
        return sub->obits;
819
    }
820
821
    /* VERBATIM */
822 e35b689e Justin Ruggles
    if (n < 5) {
823 e71bcc37 Justin Ruggles
        sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
824
        encode_residual_verbatim(res, smp, n);
825
        return sub->obits * n;
826
    }
827
828 15f537ed Justin Ruggles
    min_order  = s->options.min_prediction_order;
829
    max_order  = s->options.max_prediction_order;
830
    min_porder = s->options.min_partition_order;
831
    max_porder = s->options.max_partition_order;
832
    precision  = s->options.lpc_coeff_precision;
833
    omethod    = s->options.prediction_order_method;
834 e71bcc37 Justin Ruggles
835
    /* FIXED */
836 15f537ed Justin Ruggles
    if (s->options.lpc_type == AV_LPC_TYPE_NONE  ||
837
        s->options.lpc_type == AV_LPC_TYPE_FIXED || n <= max_order) {
838 a403fc03 Justin Ruggles
        uint32_t bits[MAX_FIXED_ORDER+1];
839 e35b689e Justin Ruggles
        if (max_order > MAX_FIXED_ORDER)
840
            max_order = MAX_FIXED_ORDER;
841 a403fc03 Justin Ruggles
        opt_order = 0;
842 e35b689e Justin Ruggles
        bits[0]   = UINT32_MAX;
843
        for (i = min_order; i <= max_order; i++) {
844 a403fc03 Justin Ruggles
            encode_residual_fixed(res, smp, n, i);
845 1e5707b7 Justin Ruggles
            bits[i] = calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res,
846 a403fc03 Justin Ruggles
                                             n, i, sub->obits);
847 e35b689e Justin Ruggles
            if (bits[i] < bits[opt_order])
848 a403fc03 Justin Ruggles
                opt_order = i;
849 e71bcc37 Justin Ruggles
        }
850 e35b689e Justin Ruggles
        sub->order     = opt_order;
851
        sub->type      = FLAC_SUBFRAME_FIXED;
852 a403fc03 Justin Ruggles
        sub->type_code = sub->type | sub->order;
853 e35b689e Justin Ruggles
        if (sub->order != max_order) {
854 a403fc03 Justin Ruggles
            encode_residual_fixed(res, smp, n, sub->order);
855 1e5707b7 Justin Ruggles
            return calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res, n,
856 a403fc03 Justin Ruggles
                                          sub->order, sub->obits);
857
        }
858
        return bits[sub->order];
859 e71bcc37 Justin Ruggles
    }
860 a403fc03 Justin Ruggles
861
    /* LPC */
862 15f537ed Justin Ruggles
    opt_order = ff_lpc_calc_coefs(&s->dsp, smp, n, min_order, max_order,
863
                                  precision, coefs, shift, s->options.lpc_type,
864
                                  s->options.lpc_passes, omethod,
865 23940f14 Justin Ruggles
                                  MAX_LPC_SHIFT, 0);
866 26053bdc Justin Ruggles
867 e35b689e Justin Ruggles
    if (omethod == ORDER_METHOD_2LEVEL ||
868
        omethod == ORDER_METHOD_4LEVEL ||
869
        omethod == ORDER_METHOD_8LEVEL) {
870 26053bdc Justin Ruggles
        int levels = 1 << omethod;
871 79aec757 Måns Rullgård
        uint32_t bits[1 << ORDER_METHOD_8LEVEL];
872 26053bdc Justin Ruggles
        int order;
873 e35b689e Justin Ruggles
        int opt_index   = levels-1;
874
        opt_order       = max_order-1;
875 26053bdc Justin Ruggles
        bits[opt_index] = UINT32_MAX;
876 e35b689e Justin Ruggles
        for (i = levels-1; i >= 0; i--) {
877 26053bdc Justin Ruggles
            order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
878 e35b689e Justin Ruggles
            if (order < 0)
879
                order = 0;
880 26053bdc Justin Ruggles
            encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
881
            bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
882
                                           res, n, order+1, sub->obits, precision);
883 e35b689e Justin Ruggles
            if (bits[i] < bits[opt_index]) {
884 26053bdc Justin Ruggles
                opt_index = i;
885
                opt_order = order;
886
            }
887
        }
888
        opt_order++;
889 e35b689e Justin Ruggles
    } else if (omethod == ORDER_METHOD_SEARCH) {
890 26053bdc Justin Ruggles
        // brute-force optimal order search
891
        uint32_t bits[MAX_LPC_ORDER];
892
        opt_order = 0;
893 e35b689e Justin Ruggles
        bits[0]   = UINT32_MAX;
894
        for (i = min_order-1; i < max_order; i++) {
895 26053bdc Justin Ruggles
            encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
896
            bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
897
                                           res, n, i+1, sub->obits, precision);
898 e35b689e Justin Ruggles
            if (bits[i] < bits[opt_order])
899 26053bdc Justin Ruggles
                opt_order = i;
900
        }
901
        opt_order++;
902 e35b689e Justin Ruggles
    } else if (omethod == ORDER_METHOD_LOG) {
903 dbb45a3b Michael Niedermayer
        uint32_t bits[MAX_LPC_ORDER];
904
        int step;
905
906 e35b689e Justin Ruggles
        opt_order = min_order - 1 + (max_order-min_order)/3;
907 dbb45a3b Michael Niedermayer
        memset(bits, -1, sizeof(bits));
908
909 e35b689e Justin Ruggles
        for (step = 16; step; step >>= 1) {
910
            int last = opt_order;
911
            for (i = last-step; i <= last+step; i += step) {
912
                if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
913 dbb45a3b Michael Niedermayer
                    continue;
914
                encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
915
                bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
916 e35b689e Justin Ruggles
                                               res, n, i+1, sub->obits,
917
                                               precision);
918
                if (bits[i] < bits[opt_order])
919
                    opt_order = i;
920 dbb45a3b Michael Niedermayer
            }
921
        }
922
        opt_order++;
923 26053bdc Justin Ruggles
    }
924
925 e35b689e Justin Ruggles
    sub->order     = opt_order;
926
    sub->type      = FLAC_SUBFRAME_LPC;
927 a403fc03 Justin Ruggles
    sub->type_code = sub->type | (sub->order-1);
928 e35b689e Justin Ruggles
    sub->shift     = shift[sub->order-1];
929
    for (i = 0; i < sub->order; i++)
930 a403fc03 Justin Ruggles
        sub->coefs[i] = coefs[sub->order-1][i];
931 e35b689e Justin Ruggles
932 a403fc03 Justin Ruggles
    encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
933 e35b689e Justin Ruggles
934
    return calc_rice_params_lpc(&sub->rc, min_porder, max_porder, res, n,
935
                                sub->order, sub->obits, precision);
936 e71bcc37 Justin Ruggles
}
937
938 e35b689e Justin Ruggles
939 15f537ed Justin Ruggles
static int encode_residual_v(FlacEncodeContext *s, int ch)
940 e71bcc37 Justin Ruggles
{
941
    int i, n;
942
    FlacFrame *frame;
943
    FlacSubframe *sub;
944
    int32_t *res, *smp;
945
946 15f537ed Justin Ruggles
    frame = &s->frame;
947 e35b689e Justin Ruggles
    sub   = &frame->subframes[ch];
948
    res   = sub->residual;
949
    smp   = sub->samples;
950
    n     = frame->blocksize;
951 e71bcc37 Justin Ruggles
952
    /* CONSTANT */
953 e35b689e Justin Ruggles
    for (i = 1; i < n; i++)
954
        if (smp[i] != smp[0])
955
            break;
956
    if (i == n) {
957 e71bcc37 Justin Ruggles
        sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
958 e35b689e Justin Ruggles
        res[0]    = smp[0];
959 e71bcc37 Justin Ruggles
        return sub->obits;
960
    }
961
962
    /* VERBATIM */
963
    sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
964
    encode_residual_verbatim(res, smp, n);
965
    return sub->obits * n;
966
}
967
968 e35b689e Justin Ruggles
969 f33aa120 Michael Niedermayer
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
970
{
971
    int i, best;
972
    int32_t lt, rt;
973 e71bcc37 Justin Ruggles
    uint64_t sum[4];
974 f33aa120 Michael Niedermayer
    uint64_t score[4];
975 e71bcc37 Justin Ruggles
    int k;
976 f33aa120 Michael Niedermayer
977 a403fc03 Justin Ruggles
    /* calculate sum of 2nd order residual for each channel */
978 e71bcc37 Justin Ruggles
    sum[0] = sum[1] = sum[2] = sum[3] = 0;
979 e35b689e Justin Ruggles
    for (i = 2; i < n; i++) {
980
        lt = left_ch[i]  - 2*left_ch[i-1]  + left_ch[i-2];
981 f33aa120 Michael Niedermayer
        rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
982 c26abfa5 Diego Biurrun
        sum[2] += FFABS((lt + rt) >> 1);
983
        sum[3] += FFABS(lt - rt);
984
        sum[0] += FFABS(lt);
985
        sum[1] += FFABS(rt);
986 e71bcc37 Justin Ruggles
    }
987 a403fc03 Justin Ruggles
    /* estimate bit counts */
988 e35b689e Justin Ruggles
    for (i = 0; i < 4; i++) {
989
        k      = find_optimal_param(2 * sum[i], n);
990
        sum[i] = rice_encode_count( 2 * sum[i], n, k);
991 f33aa120 Michael Niedermayer
    }
992
993
    /* calculate score for each mode */
994 e71bcc37 Justin Ruggles
    score[0] = sum[0] + sum[1];
995
    score[1] = sum[0] + sum[3];
996
    score[2] = sum[1] + sum[3];
997
    score[3] = sum[2] + sum[3];
998 f33aa120 Michael Niedermayer
999
    /* return mode with lowest score */
1000
    best = 0;
1001 e35b689e Justin Ruggles
    for (i = 1; i < 4; i++)
1002
        if (score[i] < score[best])
1003 f33aa120 Michael Niedermayer
            best = i;
1004 e35b689e Justin Ruggles
    if (best == 0) {
1005 3159780b Justin Ruggles
        return FLAC_CHMODE_INDEPENDENT;
1006 e35b689e Justin Ruggles
    } else if (best == 1) {
1007 f33aa120 Michael Niedermayer
        return FLAC_CHMODE_LEFT_SIDE;
1008 e35b689e Justin Ruggles
    } else if (best == 2) {
1009 f33aa120 Michael Niedermayer
        return FLAC_CHMODE_RIGHT_SIDE;
1010
    } else {
1011
        return FLAC_CHMODE_MID_SIDE;
1012
    }
1013
}
1014
1015 e35b689e Justin Ruggles
1016 f33aa120 Michael Niedermayer
/**
1017 e35b689e Justin Ruggles
 * Perform stereo channel decorrelation.
1018 f33aa120 Michael Niedermayer
 */
1019 15f537ed Justin Ruggles
static void channel_decorrelation(FlacEncodeContext *s)
1020 f33aa120 Michael Niedermayer
{
1021
    FlacFrame *frame;
1022
    int32_t *left, *right;
1023
    int i, n;
1024
1025 15f537ed Justin Ruggles
    frame = &s->frame;
1026 e35b689e Justin Ruggles
    n     = frame->blocksize;
1027 f33aa120 Michael Niedermayer
    left  = frame->subframes[0].samples;
1028
    right = frame->subframes[1].samples;
1029
1030 15f537ed Justin Ruggles
    if (s->channels != 2) {
1031 3159780b Justin Ruggles
        frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
1032 f33aa120 Michael Niedermayer
        return;
1033
    }
1034
1035
    frame->ch_mode = estimate_stereo_mode(left, right, n);
1036
1037
    /* perform decorrelation and adjust bits-per-sample */
1038 e35b689e Justin Ruggles
    if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1039 f33aa120 Michael Niedermayer
        return;
1040 e35b689e Justin Ruggles
    if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1041 f33aa120 Michael Niedermayer
        int32_t tmp;
1042 e35b689e Justin Ruggles
        for (i = 0; i < n; i++) {
1043
            tmp      = left[i];
1044
            left[i]  = (tmp + right[i]) >> 1;
1045
            right[i] =  tmp - right[i];
1046 f33aa120 Michael Niedermayer
        }
1047
        frame->subframes[1].obits++;
1048 e35b689e Justin Ruggles
    } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1049
        for (i = 0; i < n; i++)
1050 f33aa120 Michael Niedermayer
            right[i] = left[i] - right[i];
1051
        frame->subframes[1].obits++;
1052
    } else {
1053 e35b689e Justin Ruggles
        for (i = 0; i < n; i++)
1054 f33aa120 Michael Niedermayer
            left[i] -= right[i];
1055
        frame->subframes[0].obits++;
1056
    }
1057
}
1058
1059 e35b689e Justin Ruggles
1060 e71bcc37 Justin Ruggles
static void write_utf8(PutBitContext *pb, uint32_t val)
1061 9e96ab03 Michael Niedermayer
{
1062 360932f7 Zuxy Meng
    uint8_t tmp;
1063
    PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1064 9e96ab03 Michael Niedermayer
}
1065
1066 e35b689e Justin Ruggles
1067 e71bcc37 Justin Ruggles
static void output_frame_header(FlacEncodeContext *s)
1068 9e96ab03 Michael Niedermayer
{
1069
    FlacFrame *frame;
1070
    int crc;
1071
1072
    frame = &s->frame;
1073
1074
    put_bits(&s->pb, 16, 0xFFF8);
1075
    put_bits(&s->pb, 4, frame->bs_code[0]);
1076
    put_bits(&s->pb, 4, s->sr_code[0]);
1077 e35b689e Justin Ruggles
1078
    if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1079 ad52941f Justin Ruggles
        put_bits(&s->pb, 4, s->channels-1);
1080 e35b689e Justin Ruggles
    else
1081 9e96ab03 Michael Niedermayer
        put_bits(&s->pb, 4, frame->ch_mode);
1082 e35b689e Justin Ruggles
1083 9e96ab03 Michael Niedermayer
    put_bits(&s->pb, 3, 4); /* bits-per-sample code */
1084
    put_bits(&s->pb, 1, 0);
1085
    write_utf8(&s->pb, s->frame_count);
1086 e35b689e Justin Ruggles
1087
    if (frame->bs_code[0] == 6)
1088 f33aa120 Michael Niedermayer
        put_bits(&s->pb, 8, frame->bs_code[1]);
1089 e35b689e Justin Ruggles
    else if (frame->bs_code[0] == 7)
1090 f33aa120 Michael Niedermayer
        put_bits(&s->pb, 16, frame->bs_code[1]);
1091 e35b689e Justin Ruggles
1092
    if (s->sr_code[0] == 12)
1093 f33aa120 Michael Niedermayer
        put_bits(&s->pb, 8, s->sr_code[1]);
1094 e35b689e Justin Ruggles
    else if (s->sr_code[0] > 12)
1095 f33aa120 Michael Niedermayer
        put_bits(&s->pb, 16, s->sr_code[1]);
1096 e35b689e Justin Ruggles
1097 9e96ab03 Michael Niedermayer
    flush_put_bits(&s->pb);
1098 e35b689e Justin Ruggles
    crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
1099
                 put_bits_count(&s->pb) >> 3);
1100 9e96ab03 Michael Niedermayer
    put_bits(&s->pb, 8, crc);
1101
}
1102
1103 e35b689e Justin Ruggles
1104 1f1c05b0 Justin Ruggles
static void output_subframe_verbatim(FlacEncodeContext *s, FlacSubframe *sub)
1105 9e96ab03 Michael Niedermayer
{
1106 89912b5f Justin Ruggles
    put_sbits(&s->pb, sub->obits, sub->residual[0]);
1107 9e96ab03 Michael Niedermayer
1108 89912b5f Justin Ruggles
    if (sub->type == FLAC_SUBFRAME_VERBATIM) {
1109
        int i;
1110
        for (i = 0; i < s->frame.blocksize; i++)
1111
            put_sbits(&s->pb, sub->obits, sub->residual[i]);
1112 9e96ab03 Michael Niedermayer
    }
1113
}
1114
1115 e35b689e Justin Ruggles
1116 1f1c05b0 Justin Ruggles
static void output_residual(FlacEncodeContext *s, FlacSubframe *sub)
1117 9e96ab03 Michael Niedermayer
{
1118 e71bcc37 Justin Ruggles
    int i, j, p, n, parts;
1119 9e96ab03 Michael Niedermayer
    int k, porder, psize, res_cnt;
1120
    FlacFrame *frame;
1121 e71bcc37 Justin Ruggles
    int32_t *res;
1122 9e96ab03 Michael Niedermayer
1123 15f537ed Justin Ruggles
    frame = &s->frame;
1124 e35b689e Justin Ruggles
    res   = sub->residual;
1125
    n     = frame->blocksize;
1126 9e96ab03 Michael Niedermayer
1127
    /* rice-encoded block */
1128 15f537ed Justin Ruggles
    put_bits(&s->pb, 2, 0);
1129 9e96ab03 Michael Niedermayer
1130
    /* partition order */
1131 e35b689e Justin Ruggles
    porder  = sub->rc.porder;
1132
    psize   = n >> porder;
1133
    parts   = (1 << porder);
1134 15f537ed Justin Ruggles
    put_bits(&s->pb, 4, porder);
1135 9e96ab03 Michael Niedermayer
    res_cnt = psize - sub->order;
1136
1137
    /* residual */
1138
    j = sub->order;
1139 e35b689e Justin Ruggles
    for (p = 0; p < parts; p++) {
1140 e71bcc37 Justin Ruggles
        k = sub->rc.params[p];
1141 15f537ed Justin Ruggles
        put_bits(&s->pb, 4, k);
1142 e35b689e Justin Ruggles
        if (p == 1)
1143
            res_cnt = psize;
1144
        for (i = 0; i < res_cnt && j < n; i++, j++)
1145 15f537ed Justin Ruggles
            set_sr_golomb_flac(&s->pb, res[j], k, INT32_MAX, 0);
1146 9e96ab03 Michael Niedermayer
    }
1147
}
1148
1149 e35b689e Justin Ruggles
1150 1f1c05b0 Justin Ruggles
static void output_subframe_fixed(FlacEncodeContext *s, FlacSubframe *sub)
1151 9e96ab03 Michael Niedermayer
{
1152
    int i;
1153
1154
    /* warm-up samples */
1155 e35b689e Justin Ruggles
    for (i = 0; i < sub->order; i++)
1156 15f537ed Justin Ruggles
        put_sbits(&s->pb, sub->obits, sub->residual[i]);
1157 9e96ab03 Michael Niedermayer
1158
    /* residual */
1159 1f1c05b0 Justin Ruggles
    output_residual(s, sub);
1160 9e96ab03 Michael Niedermayer
}
1161
1162 e35b689e Justin Ruggles
1163 1f1c05b0 Justin Ruggles
static void output_subframe_lpc(FlacEncodeContext *s, FlacSubframe *sub)
1164 a403fc03 Justin Ruggles
{
1165
    int i, cbits;
1166
    FlacFrame *frame;
1167
1168 15f537ed Justin Ruggles
    frame = &s->frame;
1169 a403fc03 Justin Ruggles
1170
    /* warm-up samples */
1171 e35b689e Justin Ruggles
    for (i = 0; i < sub->order; i++)
1172 15f537ed Justin Ruggles
        put_sbits(&s->pb, sub->obits, sub->residual[i]);
1173 a403fc03 Justin Ruggles
1174
    /* LPC coefficients */
1175 15f537ed Justin Ruggles
    cbits = s->options.lpc_coeff_precision;
1176
    put_bits( &s->pb, 4, cbits-1);
1177
    put_sbits(&s->pb, 5, sub->shift);
1178 e35b689e Justin Ruggles
    for (i = 0; i < sub->order; i++)
1179 15f537ed Justin Ruggles
        put_sbits(&s->pb, cbits, sub->coefs[i]);
1180 a403fc03 Justin Ruggles
1181
    /* residual */
1182 1f1c05b0 Justin Ruggles
    output_residual(s, sub);
1183 a403fc03 Justin Ruggles
}
1184
1185 e35b689e Justin Ruggles
1186 9e96ab03 Michael Niedermayer
static void output_subframes(FlacEncodeContext *s)
1187
{
1188
    FlacFrame *frame;
1189
    FlacSubframe *sub;
1190
    int ch;
1191
1192
    frame = &s->frame;
1193
1194 e35b689e Justin Ruggles
    for (ch = 0; ch < s->channels; ch++) {
1195 9e96ab03 Michael Niedermayer
        sub = &frame->subframes[ch];
1196
1197
        /* subframe header */
1198
        put_bits(&s->pb, 1, 0);
1199
        put_bits(&s->pb, 6, sub->type_code);
1200
        put_bits(&s->pb, 1, 0); /* no wasted bits */
1201
1202
        /* subframe */
1203 89912b5f Justin Ruggles
        switch (sub->type) {
1204
        case FLAC_SUBFRAME_CONSTANT:
1205
        case FLAC_SUBFRAME_VERBATIM: output_subframe_verbatim(s, sub); break;
1206
        case FLAC_SUBFRAME_FIXED:    output_subframe_fixed(   s, sub); break;
1207
        case FLAC_SUBFRAME_LPC:      output_subframe_lpc(     s, sub); break;
1208
        }
1209 9e96ab03 Michael Niedermayer
    }
1210
}
1211
1212 e35b689e Justin Ruggles
1213 9e96ab03 Michael Niedermayer
static void output_frame_footer(FlacEncodeContext *s)
1214
{
1215
    int crc;
1216
    flush_put_bits(&s->pb);
1217 e35b689e Justin Ruggles
    crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
1218
                            put_bits_count(&s->pb)>>3));
1219 9e96ab03 Michael Niedermayer
    put_bits(&s->pb, 16, crc);
1220
    flush_put_bits(&s->pb);
1221
}
1222
1223 e35b689e Justin Ruggles
1224 edac49da Reimar Döffinger
static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
1225 a9f8587e Justin Ruggles
{
1226 63613fe6 Måns Rullgård
#if HAVE_BIGENDIAN
1227 a9f8587e Justin Ruggles
    int i;
1228 e35b689e Justin Ruggles
    for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1229 8fc0162a Måns Rullgård
        int16_t smp = av_le2ne16(samples[i]);
1230 a9f8587e Justin Ruggles
        av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
1231
    }
1232
#else
1233 edac49da Reimar Döffinger
    av_md5_update(s->md5ctx, (const uint8_t *)samples, s->frame.blocksize*s->channels*2);
1234 a9f8587e Justin Ruggles
#endif
1235
}
1236
1237 e35b689e Justin Ruggles
1238 9e96ab03 Michael Niedermayer
static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
1239
                             int buf_size, void *data)
1240
{
1241
    int ch;
1242
    FlacEncodeContext *s;
1243 edac49da Reimar Döffinger
    const int16_t *samples = data;
1244 9e96ab03 Michael Niedermayer
    int out_bytes;
1245 f1f373c2 Justin Ruggles
    int reencoded=0;
1246 9e96ab03 Michael Niedermayer
1247
    s = avctx->priv_data;
1248
1249 e35b689e Justin Ruggles
    if (buf_size < s->max_framesize * 2) {
1250 f1f373c2 Justin Ruggles
        av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
1251
        return 0;
1252
    }
1253
1254 e1a12934 Justin Ruggles
    /* when the last block is reached, update the header in extradata */
1255
    if (!data) {
1256 6682ae42 Justin Ruggles
        s->max_framesize = s->max_encoded_framesize;
1257 a9f8587e Justin Ruggles
        av_md5_final(s->md5ctx, s->md5sum);
1258 e1a12934 Justin Ruggles
        write_streaminfo(s, avctx->extradata);
1259
        return 0;
1260
    }
1261
1262 f33aa120 Michael Niedermayer
    init_frame(s);
1263 9e96ab03 Michael Niedermayer
1264
    copy_samples(s, samples);
1265
1266 f33aa120 Michael Niedermayer
    channel_decorrelation(s);
1267
1268 e35b689e Justin Ruggles
    for (ch = 0; ch < s->channels; ch++)
1269 9e96ab03 Michael Niedermayer
        encode_residual(s, ch);
1270 f1f373c2 Justin Ruggles
1271
write_frame:
1272 9e96ab03 Michael Niedermayer
    init_put_bits(&s->pb, frame, buf_size);
1273
    output_frame_header(s);
1274
    output_subframes(s);
1275
    output_frame_footer(s);
1276
    out_bytes = put_bits_count(&s->pb) >> 3;
1277
1278 e35b689e Justin Ruggles
    if (out_bytes > s->max_framesize) {
1279
        if (reencoded) {
1280 9e96ab03 Michael Niedermayer
            /* still too large. must be an error. */
1281
            av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
1282
            return -1;
1283
        }
1284 f1f373c2 Justin Ruggles
1285
        /* frame too large. use verbatim mode */
1286 e35b689e Justin Ruggles
        for (ch = 0; ch < s->channels; ch++)
1287 f1f373c2 Justin Ruggles
            encode_residual_v(s, ch);
1288
        reencoded = 1;
1289
        goto write_frame;
1290 9e96ab03 Michael Niedermayer
    }
1291
1292
    s->frame_count++;
1293 871a24f3 Justin Ruggles
    avctx->coded_frame->pts = s->sample_count;
1294 e1a12934 Justin Ruggles
    s->sample_count += avctx->frame_size;
1295 a9f8587e Justin Ruggles
    update_md5_sum(s, samples);
1296 6682ae42 Justin Ruggles
    if (out_bytes > s->max_encoded_framesize)
1297
        s->max_encoded_framesize = out_bytes;
1298 b9b4fc5e Justin Ruggles
    if (out_bytes < s->min_framesize)
1299
        s->min_framesize = out_bytes;
1300 e1a12934 Justin Ruggles
1301 9e96ab03 Michael Niedermayer
    return out_bytes;
1302
}
1303
1304 e35b689e Justin Ruggles
1305 98a6fff9 Zuxy Meng
static av_cold int flac_encode_close(AVCodecContext *avctx)
1306 9e96ab03 Michael Niedermayer
{
1307 a9f8587e Justin Ruggles
    if (avctx->priv_data) {
1308
        FlacEncodeContext *s = avctx->priv_data;
1309
        av_freep(&s->md5ctx);
1310
    }
1311 f33aa120 Michael Niedermayer
    av_freep(&avctx->extradata);
1312
    avctx->extradata_size = 0;
1313 9e96ab03 Michael Niedermayer
    av_freep(&avctx->coded_frame);
1314
    return 0;
1315
}
1316
1317 e35b689e Justin Ruggles
1318 9e96ab03 Michael Niedermayer
AVCodec flac_encoder = {
1319
    "flac",
1320 72415b2a Stefano Sabatini
    AVMEDIA_TYPE_AUDIO,
1321 9e96ab03 Michael Niedermayer
    CODEC_ID_FLAC,
1322
    sizeof(FlacEncodeContext),
1323
    flac_encode_init,
1324
    flac_encode_frame,
1325
    flac_encode_close,
1326
    NULL,
1327 e1a12934 Justin Ruggles
    .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
1328 b5f09d31 Reimar Döffinger
    .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
1329 fe4bf374 Stefano Sabatini
    .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
1330 9e96ab03 Michael Niedermayer
};