]> git.sesse.net Git - ffmpeg/blob - libavcodec/flacenc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / flacenc.c
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 "libavutil/opt.h"
25 #include "avcodec.h"
26 #include "get_bits.h"
27 #include "golomb.h"
28 #include "internal.h"
29 #include "lpc.h"
30 #include "flac.h"
31 #include "flacdata.h"
32
33 #define FLAC_SUBFRAME_CONSTANT  0
34 #define FLAC_SUBFRAME_VERBATIM  1
35 #define FLAC_SUBFRAME_FIXED     8
36 #define FLAC_SUBFRAME_LPC      32
37
38 #define MAX_FIXED_ORDER     4
39 #define MAX_PARTITION_ORDER 8
40 #define MAX_PARTITIONS     (1 << MAX_PARTITION_ORDER)
41 #define MAX_LPC_PRECISION  15
42 #define MAX_LPC_SHIFT      15
43 #define MAX_RICE_PARAM     14
44
45 typedef struct CompressionOptions {
46     int compression_level;
47     int block_time_ms;
48     enum FFLPCType lpc_type;
49     int lpc_passes;
50     int lpc_coeff_precision;
51     int min_prediction_order;
52     int max_prediction_order;
53     int prediction_order_method;
54     int min_partition_order;
55     int max_partition_order;
56 } CompressionOptions;
57
58 typedef struct RiceContext {
59     int porder;
60     int params[MAX_PARTITIONS];
61 } RiceContext;
62
63 typedef struct FlacSubframe {
64     int type;
65     int type_code;
66     int obits;
67     int order;
68     int32_t coefs[MAX_LPC_ORDER];
69     int shift;
70     RiceContext rc;
71     int32_t samples[FLAC_MAX_BLOCKSIZE];
72     int32_t residual[FLAC_MAX_BLOCKSIZE+1];
73 } FlacSubframe;
74
75 typedef struct FlacFrame {
76     FlacSubframe subframes[FLAC_MAX_CHANNELS];
77     int blocksize;
78     int bs_code[2];
79     uint8_t crc8;
80     int ch_mode;
81     int verbatim_only;
82 } FlacFrame;
83
84 typedef struct FlacEncodeContext {
85     AVClass *class;
86     PutBitContext pb;
87     int channels;
88     int samplerate;
89     int sr_code[2];
90     int max_blocksize;
91     int min_framesize;
92     int max_framesize;
93     int max_encoded_framesize;
94     uint32_t frame_count;
95     uint64_t sample_count;
96     uint8_t md5sum[16];
97     FlacFrame frame;
98     CompressionOptions options;
99     AVCodecContext *avctx;
100     LPCContext lpc_ctx;
101     struct AVMD5 *md5ctx;
102 } FlacEncodeContext;
103
104
105 /**
106  * Write streaminfo metadata block to byte array.
107  */
108 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
109 {
110     PutBitContext pb;
111
112     memset(header, 0, FLAC_STREAMINFO_SIZE);
113     init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
114
115     /* streaminfo metadata block */
116     put_bits(&pb, 16, s->max_blocksize);
117     put_bits(&pb, 16, s->max_blocksize);
118     put_bits(&pb, 24, s->min_framesize);
119     put_bits(&pb, 24, s->max_framesize);
120     put_bits(&pb, 20, s->samplerate);
121     put_bits(&pb, 3, s->channels-1);
122     put_bits(&pb, 5, 15);       /* bits per sample - 1 */
123     /* write 36-bit sample count in 2 put_bits() calls */
124     put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
125     put_bits(&pb, 12,  s->sample_count & 0x000000FFFLL);
126     flush_put_bits(&pb);
127     memcpy(&header[18], s->md5sum, 16);
128 }
129
130
131 /**
132  * Set blocksize based on samplerate.
133  * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
134  */
135 static int select_blocksize(int samplerate, int block_time_ms)
136 {
137     int i;
138     int target;
139     int blocksize;
140
141     assert(samplerate > 0);
142     blocksize = ff_flac_blocksize_table[1];
143     target    = (samplerate * block_time_ms) / 1000;
144     for (i = 0; i < 16; i++) {
145         if (target >= ff_flac_blocksize_table[i] &&
146             ff_flac_blocksize_table[i] > blocksize) {
147             blocksize = ff_flac_blocksize_table[i];
148         }
149     }
150     return blocksize;
151 }
152
153
154 static av_cold void dprint_compression_options(FlacEncodeContext *s)
155 {
156     AVCodecContext     *avctx = s->avctx;
157     CompressionOptions *opt   = &s->options;
158
159     av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);
160
161     switch (opt->lpc_type) {
162     case FF_LPC_TYPE_NONE:
163         av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
164         break;
165     case FF_LPC_TYPE_FIXED:
166         av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
167         break;
168     case FF_LPC_TYPE_LEVINSON:
169         av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
170         break;
171     case FF_LPC_TYPE_CHOLESKY:
172         av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
173                opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
174         break;
175     }
176
177     av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
178            opt->min_prediction_order, opt->max_prediction_order);
179
180     switch (opt->prediction_order_method) {
181     case ORDER_METHOD_EST:
182         av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
183         break;
184     case ORDER_METHOD_2LEVEL:
185         av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
186         break;
187     case ORDER_METHOD_4LEVEL:
188         av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
189         break;
190     case ORDER_METHOD_8LEVEL:
191         av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
192         break;
193     case ORDER_METHOD_SEARCH:
194         av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
195         break;
196     case ORDER_METHOD_LOG:
197         av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
198         break;
199     }
200
201
202     av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
203            opt->min_partition_order, opt->max_partition_order);
204
205     av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
206
207     av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
208            opt->lpc_coeff_precision);
209 }
210
211
212 static av_cold int flac_encode_init(AVCodecContext *avctx)
213 {
214     int freq = avctx->sample_rate;
215     int channels = avctx->channels;
216     FlacEncodeContext *s = avctx->priv_data;
217     int i, level, ret;
218     uint8_t *streaminfo;
219
220     s->avctx = avctx;
221
222     if (avctx->sample_fmt != AV_SAMPLE_FMT_S16)
223         return -1;
224
225     if (channels < 1 || channels > FLAC_MAX_CHANNELS)
226         return -1;
227     s->channels = channels;
228
229     /* find samplerate in table */
230     if (freq < 1)
231         return -1;
232     for (i = 4; i < 12; i++) {
233         if (freq == ff_flac_sample_rate_table[i]) {
234             s->samplerate = ff_flac_sample_rate_table[i];
235             s->sr_code[0] = i;
236             s->sr_code[1] = 0;
237             break;
238         }
239     }
240     /* if not in table, samplerate is non-standard */
241     if (i == 12) {
242         if (freq % 1000 == 0 && freq < 255000) {
243             s->sr_code[0] = 12;
244             s->sr_code[1] = freq / 1000;
245         } else if (freq % 10 == 0 && freq < 655350) {
246             s->sr_code[0] = 14;
247             s->sr_code[1] = freq / 10;
248         } else if (freq < 65535) {
249             s->sr_code[0] = 13;
250             s->sr_code[1] = freq;
251         } else {
252             return -1;
253         }
254         s->samplerate = freq;
255     }
256
257     /* set compression option defaults based on avctx->compression_level */
258     if (avctx->compression_level < 0)
259         s->options.compression_level = 5;
260     else
261         s->options.compression_level = avctx->compression_level;
262
263     level = s->options.compression_level;
264     if (level > 12) {
265         av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
266                s->options.compression_level);
267         return -1;
268     }
269
270     s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
271
272     if (s->options.lpc_type == FF_LPC_TYPE_DEFAULT)
273         s->options.lpc_type  = ((int[]){ FF_LPC_TYPE_FIXED,    FF_LPC_TYPE_FIXED,    FF_LPC_TYPE_FIXED,
274                                          FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
275                                          FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
276                                          FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
277                                          FF_LPC_TYPE_LEVINSON})[level];
278
279     s->options.min_prediction_order = ((int[]){  2,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1})[level];
280     s->options.max_prediction_order = ((int[]){  3,  4,  4,  6,  8,  8,  8,  8, 12, 12, 12, 32, 32})[level];
281
282     if (s->options.prediction_order_method < 0)
283         s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST,    ORDER_METHOD_EST,    ORDER_METHOD_EST,
284                                                        ORDER_METHOD_EST,    ORDER_METHOD_EST,    ORDER_METHOD_EST,
285                                                        ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG,    ORDER_METHOD_4LEVEL,
286                                                        ORDER_METHOD_LOG,    ORDER_METHOD_SEARCH, ORDER_METHOD_LOG,
287                                                        ORDER_METHOD_SEARCH})[level];
288
289     if (s->options.min_partition_order > s->options.max_partition_order) {
290         av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
291                s->options.min_partition_order, s->options.max_partition_order);
292         return AVERROR(EINVAL);
293     }
294     if (s->options.min_partition_order < 0)
295         s->options.min_partition_order = ((int[]){  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0})[level];
296     if (s->options.max_partition_order < 0)
297         s->options.max_partition_order = ((int[]){  2,  2,  3,  3,  3,  8,  8,  8,  8,  8,  8,  8,  8})[level];
298
299     if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
300         s->options.min_prediction_order = 0;
301     } else if (avctx->min_prediction_order >= 0) {
302         if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
303             if (avctx->min_prediction_order > MAX_FIXED_ORDER) {
304                 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
305                        avctx->min_prediction_order);
306                 return -1;
307             }
308         } else if (avctx->min_prediction_order < MIN_LPC_ORDER ||
309                    avctx->min_prediction_order > MAX_LPC_ORDER) {
310             av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
311                    avctx->min_prediction_order);
312             return -1;
313         }
314         s->options.min_prediction_order = avctx->min_prediction_order;
315     }
316     if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
317         s->options.max_prediction_order = 0;
318     } else if (avctx->max_prediction_order >= 0) {
319         if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
320             if (avctx->max_prediction_order > MAX_FIXED_ORDER) {
321                 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
322                        avctx->max_prediction_order);
323                 return -1;
324             }
325         } else if (avctx->max_prediction_order < MIN_LPC_ORDER ||
326                    avctx->max_prediction_order > MAX_LPC_ORDER) {
327             av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
328                    avctx->max_prediction_order);
329             return -1;
330         }
331         s->options.max_prediction_order = avctx->max_prediction_order;
332     }
333     if (s->options.max_prediction_order < s->options.min_prediction_order) {
334         av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
335                s->options.min_prediction_order, s->options.max_prediction_order);
336         return -1;
337     }
338
339     if (avctx->frame_size > 0) {
340         if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
341                 avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
342             av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
343                    avctx->frame_size);
344             return -1;
345         }
346     } else {
347         s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
348     }
349     s->max_blocksize = s->avctx->frame_size;
350
351     /* set maximum encoded frame size in verbatim mode */
352     s->max_framesize = ff_flac_get_max_frame_size(s->avctx->frame_size,
353                                                   s->channels, 16);
354
355     /* initialize MD5 context */
356     s->md5ctx = av_malloc(av_md5_size);
357     if (!s->md5ctx)
358         return AVERROR(ENOMEM);
359     av_md5_init(s->md5ctx);
360
361     streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
362     if (!streaminfo)
363         return AVERROR(ENOMEM);
364     write_streaminfo(s, streaminfo);
365     avctx->extradata = streaminfo;
366     avctx->extradata_size = FLAC_STREAMINFO_SIZE;
367
368     s->frame_count   = 0;
369     s->min_framesize = s->max_framesize;
370
371 #if FF_API_OLD_ENCODE_AUDIO
372     avctx->coded_frame = avcodec_alloc_frame();
373     if (!avctx->coded_frame)
374         return AVERROR(ENOMEM);
375 #endif
376
377     if (channels == 3 &&
378             avctx->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
379         channels == 4 &&
380             avctx->channel_layout != AV_CH_LAYOUT_2_2 &&
381             avctx->channel_layout != AV_CH_LAYOUT_QUAD ||
382         channels == 5 &&
383             avctx->channel_layout != AV_CH_LAYOUT_5POINT0 &&
384             avctx->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
385         channels == 6 &&
386             avctx->channel_layout != AV_CH_LAYOUT_5POINT1 &&
387             avctx->channel_layout != AV_CH_LAYOUT_5POINT1_BACK) {
388         if (avctx->channel_layout) {
389             av_log(avctx, AV_LOG_ERROR, "Channel layout not supported by Flac, "
390                                              "output stream will have incorrect "
391                                              "channel layout.\n");
392         } else {
393             av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
394                                                "will use Flac channel layout for "
395                                                "%d channels.\n", channels);
396         }
397     }
398
399     ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
400                       s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
401
402     dprint_compression_options(s);
403
404     return ret;
405 }
406
407
408 static void init_frame(FlacEncodeContext *s, int nb_samples)
409 {
410     int i, ch;
411     FlacFrame *frame;
412
413     frame = &s->frame;
414
415     for (i = 0; i < 16; i++) {
416         if (nb_samples == ff_flac_blocksize_table[i]) {
417             frame->blocksize  = ff_flac_blocksize_table[i];
418             frame->bs_code[0] = i;
419             frame->bs_code[1] = 0;
420             break;
421         }
422     }
423     if (i == 16) {
424         frame->blocksize = nb_samples;
425         if (frame->blocksize <= 256) {
426             frame->bs_code[0] = 6;
427             frame->bs_code[1] = frame->blocksize-1;
428         } else {
429             frame->bs_code[0] = 7;
430             frame->bs_code[1] = frame->blocksize-1;
431         }
432     }
433
434     for (ch = 0; ch < s->channels; ch++)
435         frame->subframes[ch].obits = 16;
436
437     frame->verbatim_only = 0;
438 }
439
440
441 /**
442  * Copy channel-interleaved input samples into separate subframes.
443  */
444 static void copy_samples(FlacEncodeContext *s, const int16_t *samples)
445 {
446     int i, j, ch;
447     FlacFrame *frame;
448
449     frame = &s->frame;
450     for (i = 0, j = 0; i < frame->blocksize; i++)
451         for (ch = 0; ch < s->channels; ch++, j++)
452             frame->subframes[ch].samples[i] = samples[j];
453 }
454
455
456 static int rice_count_exact(int32_t *res, int n, int k)
457 {
458     int i;
459     int count = 0;
460
461     for (i = 0; i < n; i++) {
462         int32_t v = -2 * res[i] - 1;
463         v ^= v >> 31;
464         count += (v >> k) + 1 + k;
465     }
466     return count;
467 }
468
469
470 static int subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
471                                 int pred_order)
472 {
473     int p, porder, psize;
474     int i, part_end;
475     int count = 0;
476
477     /* subframe header */
478     count += 8;
479
480     /* subframe */
481     if (sub->type == FLAC_SUBFRAME_CONSTANT) {
482         count += sub->obits;
483     } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
484         count += s->frame.blocksize * sub->obits;
485     } else {
486         /* warm-up samples */
487         count += pred_order * sub->obits;
488
489         /* LPC coefficients */
490         if (sub->type == FLAC_SUBFRAME_LPC)
491             count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
492
493         /* rice-encoded block */
494         count += 2;
495
496         /* partition order */
497         porder = sub->rc.porder;
498         psize  = s->frame.blocksize >> porder;
499         count += 4;
500
501         /* residual */
502         i        = pred_order;
503         part_end = psize;
504         for (p = 0; p < 1 << porder; p++) {
505             int k = sub->rc.params[p];
506             count += 4;
507             count += rice_count_exact(&sub->residual[i], part_end - i, k);
508             i = part_end;
509             part_end = FFMIN(s->frame.blocksize, part_end + psize);
510         }
511     }
512
513     return count;
514 }
515
516
517 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
518
519 /**
520  * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
521  */
522 static int find_optimal_param(uint32_t sum, int n)
523 {
524     int k;
525     uint32_t sum2;
526
527     if (sum <= n >> 1)
528         return 0;
529     sum2 = sum - (n >> 1);
530     k    = av_log2(n < 256 ? FASTDIV(sum2, n) : sum2 / n);
531     return FFMIN(k, MAX_RICE_PARAM);
532 }
533
534
535 static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
536                                          uint32_t *sums, int n, int pred_order)
537 {
538     int i;
539     int k, cnt, part;
540     uint32_t all_bits;
541
542     part     = (1 << porder);
543     all_bits = 4 * part;
544
545     cnt = (n >> porder) - pred_order;
546     for (i = 0; i < part; i++) {
547         k = find_optimal_param(sums[i], cnt);
548         rc->params[i] = k;
549         all_bits += rice_encode_count(sums[i], cnt, k);
550         cnt = n >> porder;
551     }
552
553     rc->porder = porder;
554
555     return all_bits;
556 }
557
558
559 static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
560                       uint32_t sums[][MAX_PARTITIONS])
561 {
562     int i, j;
563     int parts;
564     uint32_t *res, *res_end;
565
566     /* sums for highest level */
567     parts   = (1 << pmax);
568     res     = &data[pred_order];
569     res_end = &data[n >> pmax];
570     for (i = 0; i < parts; i++) {
571         uint32_t sum = 0;
572         while (res < res_end)
573             sum += *(res++);
574         sums[pmax][i] = sum;
575         res_end += n >> pmax;
576     }
577     /* sums for lower levels */
578     for (i = pmax - 1; i >= pmin; i--) {
579         parts = (1 << i);
580         for (j = 0; j < parts; j++)
581             sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
582     }
583 }
584
585
586 static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
587                                  int32_t *data, int n, int pred_order)
588 {
589     int i;
590     uint32_t bits[MAX_PARTITION_ORDER+1];
591     int opt_porder;
592     RiceContext tmp_rc;
593     uint32_t *udata;
594     uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
595
596     assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
597     assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
598     assert(pmin <= pmax);
599
600     udata = av_malloc(n * sizeof(uint32_t));
601     for (i = 0; i < n; i++)
602         udata[i] = (2*data[i]) ^ (data[i]>>31);
603
604     calc_sums(pmin, pmax, udata, n, pred_order, sums);
605
606     opt_porder = pmin;
607     bits[pmin] = UINT32_MAX;
608     for (i = pmin; i <= pmax; i++) {
609         bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
610         if (bits[i] <= bits[opt_porder]) {
611             opt_porder = i;
612             *rc = tmp_rc;
613         }
614     }
615
616     av_freep(&udata);
617     return bits[opt_porder];
618 }
619
620
621 static int get_max_p_order(int max_porder, int n, int order)
622 {
623     int porder = FFMIN(max_porder, av_log2(n^(n-1)));
624     if (order > 0)
625         porder = FFMIN(porder, av_log2(n/order));
626     return porder;
627 }
628
629
630 static uint32_t find_subframe_rice_params(FlacEncodeContext *s,
631                                           FlacSubframe *sub, int pred_order)
632 {
633     int pmin = get_max_p_order(s->options.min_partition_order,
634                                s->frame.blocksize, pred_order);
635     int pmax = get_max_p_order(s->options.max_partition_order,
636                                s->frame.blocksize, pred_order);
637
638     uint32_t bits = 8 + pred_order * sub->obits + 2 + 4;
639     if (sub->type == FLAC_SUBFRAME_LPC)
640         bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
641     bits += calc_rice_params(&sub->rc, pmin, pmax, sub->residual,
642                              s->frame.blocksize, pred_order);
643     return bits;
644 }
645
646
647 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
648                                   int order)
649 {
650     int i;
651
652     for (i = 0; i < order; i++)
653         res[i] = smp[i];
654
655     if (order == 0) {
656         for (i = order; i < n; i++)
657             res[i] = smp[i];
658     } else if (order == 1) {
659         for (i = order; i < n; i++)
660             res[i] = smp[i] - smp[i-1];
661     } else if (order == 2) {
662         int a = smp[order-1] - smp[order-2];
663         for (i = order; i < n; i += 2) {
664             int b    = smp[i  ] - smp[i-1];
665             res[i]   = b - a;
666             a        = smp[i+1] - smp[i  ];
667             res[i+1] = a - b;
668         }
669     } else if (order == 3) {
670         int a = smp[order-1] -   smp[order-2];
671         int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
672         for (i = order; i < n; i += 2) {
673             int b    = smp[i  ] - smp[i-1];
674             int d    = b - a;
675             res[i]   = d - c;
676             a        = smp[i+1] - smp[i  ];
677             c        = a - b;
678             res[i+1] = c - d;
679         }
680     } else {
681         int a = smp[order-1] -   smp[order-2];
682         int c = smp[order-1] - 2*smp[order-2] +   smp[order-3];
683         int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
684         for (i = order; i < n; i += 2) {
685             int b    = smp[i  ] - smp[i-1];
686             int d    = b - a;
687             int f    = d - c;
688             res[i  ] = f - e;
689             a        = smp[i+1] - smp[i  ];
690             c        = a - b;
691             e        = c - d;
692             res[i+1] = e - f;
693         }
694     }
695 }
696
697
698 #define LPC1(x) {\
699     int c = coefs[(x)-1];\
700     p0   += c * s;\
701     s     = smp[i-(x)+1];\
702     p1   += c * s;\
703 }
704
705 static av_always_inline void encode_residual_lpc_unrolled(int32_t *res,
706                                     const int32_t *smp, int n, int order,
707                                     const int32_t *coefs, int shift, int big)
708 {
709     int i;
710     for (i = order; i < n; i += 2) {
711         int s  = smp[i-order];
712         int p0 = 0, p1 = 0;
713         if (big) {
714             switch (order) {
715             case 32: LPC1(32)
716             case 31: LPC1(31)
717             case 30: LPC1(30)
718             case 29: LPC1(29)
719             case 28: LPC1(28)
720             case 27: LPC1(27)
721             case 26: LPC1(26)
722             case 25: LPC1(25)
723             case 24: LPC1(24)
724             case 23: LPC1(23)
725             case 22: LPC1(22)
726             case 21: LPC1(21)
727             case 20: LPC1(20)
728             case 19: LPC1(19)
729             case 18: LPC1(18)
730             case 17: LPC1(17)
731             case 16: LPC1(16)
732             case 15: LPC1(15)
733             case 14: LPC1(14)
734             case 13: LPC1(13)
735             case 12: LPC1(12)
736             case 11: LPC1(11)
737             case 10: LPC1(10)
738             case  9: LPC1( 9)
739                      LPC1( 8)
740                      LPC1( 7)
741                      LPC1( 6)
742                      LPC1( 5)
743                      LPC1( 4)
744                      LPC1( 3)
745                      LPC1( 2)
746                      LPC1( 1)
747             }
748         } else {
749             switch (order) {
750             case  8: LPC1( 8)
751             case  7: LPC1( 7)
752             case  6: LPC1( 6)
753             case  5: LPC1( 5)
754             case  4: LPC1( 4)
755             case  3: LPC1( 3)
756             case  2: LPC1( 2)
757             case  1: LPC1( 1)
758             }
759         }
760         res[i  ] = smp[i  ] - (p0 >> shift);
761         res[i+1] = smp[i+1] - (p1 >> shift);
762     }
763 }
764
765
766 static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
767                                 int order, const int32_t *coefs, int shift)
768 {
769     int i;
770     for (i = 0; i < order; i++)
771         res[i] = smp[i];
772 #if CONFIG_SMALL
773     for (i = order; i < n; i += 2) {
774         int j;
775         int s  = smp[i];
776         int p0 = 0, p1 = 0;
777         for (j = 0; j < order; j++) {
778             int c = coefs[j];
779             p1   += c * s;
780             s     = smp[i-j-1];
781             p0   += c * s;
782         }
783         res[i  ] = smp[i  ] - (p0 >> shift);
784         res[i+1] = smp[i+1] - (p1 >> shift);
785     }
786 #else
787     switch (order) {
788     case  1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
789     case  2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
790     case  3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
791     case  4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
792     case  5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
793     case  6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
794     case  7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
795     case  8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
796     default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
797     }
798 #endif
799 }
800
801
802 static int encode_residual_ch(FlacEncodeContext *s, int ch)
803 {
804     int i, n;
805     int min_order, max_order, opt_order, omethod;
806     FlacFrame *frame;
807     FlacSubframe *sub;
808     int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
809     int shift[MAX_LPC_ORDER];
810     int32_t *res, *smp;
811
812     frame = &s->frame;
813     sub   = &frame->subframes[ch];
814     res   = sub->residual;
815     smp   = sub->samples;
816     n     = frame->blocksize;
817
818     /* CONSTANT */
819     for (i = 1; i < n; i++)
820         if(smp[i] != smp[0])
821             break;
822     if (i == n) {
823         sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
824         res[0] = smp[0];
825         return subframe_count_exact(s, sub, 0);
826     }
827
828     /* VERBATIM */
829     if (frame->verbatim_only || n < 5) {
830         sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
831         memcpy(res, smp, n * sizeof(int32_t));
832         return subframe_count_exact(s, sub, 0);
833     }
834
835     min_order  = s->options.min_prediction_order;
836     max_order  = s->options.max_prediction_order;
837     omethod    = s->options.prediction_order_method;
838
839     /* FIXED */
840     sub->type = FLAC_SUBFRAME_FIXED;
841     if (s->options.lpc_type == FF_LPC_TYPE_NONE  ||
842         s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
843         uint32_t bits[MAX_FIXED_ORDER+1];
844         if (max_order > MAX_FIXED_ORDER)
845             max_order = MAX_FIXED_ORDER;
846         opt_order = 0;
847         bits[0]   = UINT32_MAX;
848         for (i = min_order; i <= max_order; i++) {
849             encode_residual_fixed(res, smp, n, i);
850             bits[i] = find_subframe_rice_params(s, sub, i);
851             if (bits[i] < bits[opt_order])
852                 opt_order = i;
853         }
854         sub->order     = opt_order;
855         sub->type_code = sub->type | sub->order;
856         if (sub->order != max_order) {
857             encode_residual_fixed(res, smp, n, sub->order);
858             find_subframe_rice_params(s, sub, sub->order);
859         }
860         return subframe_count_exact(s, sub, sub->order);
861     }
862
863     /* LPC */
864     sub->type = FLAC_SUBFRAME_LPC;
865     opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
866                                   s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
867                                   s->options.lpc_passes, omethod,
868                                   MAX_LPC_SHIFT, 0);
869
870     if (omethod == ORDER_METHOD_2LEVEL ||
871         omethod == ORDER_METHOD_4LEVEL ||
872         omethod == ORDER_METHOD_8LEVEL) {
873         int levels = 1 << omethod;
874         uint32_t bits[1 << ORDER_METHOD_8LEVEL];
875         int order;
876         int opt_index   = levels-1;
877         opt_order       = max_order-1;
878         bits[opt_index] = UINT32_MAX;
879         for (i = levels-1; i >= 0; i--) {
880             order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
881             if (order < 0)
882                 order = 0;
883             encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
884             bits[i] = find_subframe_rice_params(s, sub, order+1);
885             if (bits[i] < bits[opt_index]) {
886                 opt_index = i;
887                 opt_order = order;
888             }
889         }
890         opt_order++;
891     } else if (omethod == ORDER_METHOD_SEARCH) {
892         // brute-force optimal order search
893         uint32_t bits[MAX_LPC_ORDER];
894         opt_order = 0;
895         bits[0]   = UINT32_MAX;
896         for (i = min_order-1; i < max_order; i++) {
897             encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
898             bits[i] = find_subframe_rice_params(s, sub, i+1);
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] = find_subframe_rice_params(s, sub, i+1);
917                 if (bits[i] < bits[opt_order])
918                     opt_order = i;
919             }
920         }
921         opt_order++;
922     }
923
924     sub->order     = opt_order;
925     sub->type_code = sub->type | (sub->order-1);
926     sub->shift     = shift[sub->order-1];
927     for (i = 0; i < sub->order; i++)
928         sub->coefs[i] = coefs[sub->order-1][i];
929
930     encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
931
932     find_subframe_rice_params(s, sub, sub->order);
933
934     return subframe_count_exact(s, sub, sub->order);
935 }
936
937
938 static int count_frame_header(FlacEncodeContext *s)
939 {
940     uint8_t av_unused tmp;
941     int count;
942
943     /*
944     <14> Sync code
945     <1>  Reserved
946     <1>  Blocking strategy
947     <4>  Block size in inter-channel samples
948     <4>  Sample rate
949     <4>  Channel assignment
950     <3>  Sample size in bits
951     <1>  Reserved
952     */
953     count = 32;
954
955     /* coded frame number */
956     PUT_UTF8(s->frame_count, tmp, count += 8;)
957
958     /* explicit block size */
959     if (s->frame.bs_code[0] == 6)
960         count += 8;
961     else if (s->frame.bs_code[0] == 7)
962         count += 16;
963
964     /* explicit sample rate */
965     count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12)) * 8;
966
967     /* frame header CRC-8 */
968     count += 8;
969
970     return count;
971 }
972
973
974 static int encode_frame(FlacEncodeContext *s)
975 {
976     int ch, count;
977
978     count = count_frame_header(s);
979
980     for (ch = 0; ch < s->channels; ch++)
981         count += encode_residual_ch(s, ch);
982
983     count += (8 - (count & 7)) & 7; // byte alignment
984     count += 16;                    // CRC-16
985
986     return count >> 3;
987 }
988
989
990 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
991 {
992     int i, best;
993     int32_t lt, rt;
994     uint64_t sum[4];
995     uint64_t score[4];
996     int k;
997
998     /* calculate sum of 2nd order residual for each channel */
999     sum[0] = sum[1] = sum[2] = sum[3] = 0;
1000     for (i = 2; i < n; i++) {
1001         lt = left_ch[i]  - 2*left_ch[i-1]  + left_ch[i-2];
1002         rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
1003         sum[2] += FFABS((lt + rt) >> 1);
1004         sum[3] += FFABS(lt - rt);
1005         sum[0] += FFABS(lt);
1006         sum[1] += FFABS(rt);
1007     }
1008     /* estimate bit counts */
1009     for (i = 0; i < 4; i++) {
1010         k      = find_optimal_param(2 * sum[i], n);
1011         sum[i] = rice_encode_count( 2 * sum[i], n, k);
1012     }
1013
1014     /* calculate score for each mode */
1015     score[0] = sum[0] + sum[1];
1016     score[1] = sum[0] + sum[3];
1017     score[2] = sum[1] + sum[3];
1018     score[3] = sum[2] + sum[3];
1019
1020     /* return mode with lowest score */
1021     best = 0;
1022     for (i = 1; i < 4; i++)
1023         if (score[i] < score[best])
1024             best = i;
1025     if (best == 0) {
1026         return FLAC_CHMODE_INDEPENDENT;
1027     } else if (best == 1) {
1028         return FLAC_CHMODE_LEFT_SIDE;
1029     } else if (best == 2) {
1030         return FLAC_CHMODE_RIGHT_SIDE;
1031     } else {
1032         return FLAC_CHMODE_MID_SIDE;
1033     }
1034 }
1035
1036
1037 /**
1038  * Perform stereo channel decorrelation.
1039  */
1040 static void channel_decorrelation(FlacEncodeContext *s)
1041 {
1042     FlacFrame *frame;
1043     int32_t *left, *right;
1044     int i, n;
1045
1046     frame = &s->frame;
1047     n     = frame->blocksize;
1048     left  = frame->subframes[0].samples;
1049     right = frame->subframes[1].samples;
1050
1051     if (s->channels != 2) {
1052         frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
1053         return;
1054     }
1055
1056     frame->ch_mode = estimate_stereo_mode(left, right, n);
1057
1058     /* perform decorrelation and adjust bits-per-sample */
1059     if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1060         return;
1061     if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1062         int32_t tmp;
1063         for (i = 0; i < n; i++) {
1064             tmp      = left[i];
1065             left[i]  = (tmp + right[i]) >> 1;
1066             right[i] =  tmp - right[i];
1067         }
1068         frame->subframes[1].obits++;
1069     } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1070         for (i = 0; i < n; i++)
1071             right[i] = left[i] - right[i];
1072         frame->subframes[1].obits++;
1073     } else {
1074         for (i = 0; i < n; i++)
1075             left[i] -= right[i];
1076         frame->subframes[0].obits++;
1077     }
1078 }
1079
1080
1081 static void write_utf8(PutBitContext *pb, uint32_t val)
1082 {
1083     uint8_t tmp;
1084     PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1085 }
1086
1087
1088 static void write_frame_header(FlacEncodeContext *s)
1089 {
1090     FlacFrame *frame;
1091     int crc;
1092
1093     frame = &s->frame;
1094
1095     put_bits(&s->pb, 16, 0xFFF8);
1096     put_bits(&s->pb, 4, frame->bs_code[0]);
1097     put_bits(&s->pb, 4, s->sr_code[0]);
1098
1099     if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1100         put_bits(&s->pb, 4, s->channels-1);
1101     else
1102         put_bits(&s->pb, 4, frame->ch_mode);
1103
1104     put_bits(&s->pb, 3, 4); /* bits-per-sample code */
1105     put_bits(&s->pb, 1, 0);
1106     write_utf8(&s->pb, s->frame_count);
1107
1108     if (frame->bs_code[0] == 6)
1109         put_bits(&s->pb, 8, frame->bs_code[1]);
1110     else if (frame->bs_code[0] == 7)
1111         put_bits(&s->pb, 16, frame->bs_code[1]);
1112
1113     if (s->sr_code[0] == 12)
1114         put_bits(&s->pb, 8, s->sr_code[1]);
1115     else if (s->sr_code[0] > 12)
1116         put_bits(&s->pb, 16, s->sr_code[1]);
1117
1118     flush_put_bits(&s->pb);
1119     crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
1120                  put_bits_count(&s->pb) >> 3);
1121     put_bits(&s->pb, 8, crc);
1122 }
1123
1124
1125 static void write_subframes(FlacEncodeContext *s)
1126 {
1127     int ch;
1128
1129     for (ch = 0; ch < s->channels; ch++) {
1130         FlacSubframe *sub = &s->frame.subframes[ch];
1131         int i, p, porder, psize;
1132         int32_t *part_end;
1133         int32_t *res       =  sub->residual;
1134         int32_t *frame_end = &sub->residual[s->frame.blocksize];
1135
1136         /* subframe header */
1137         put_bits(&s->pb, 1, 0);
1138         put_bits(&s->pb, 6, sub->type_code);
1139         put_bits(&s->pb, 1, 0); /* no wasted bits */
1140
1141         /* subframe */
1142         if (sub->type == FLAC_SUBFRAME_CONSTANT) {
1143             put_sbits(&s->pb, sub->obits, res[0]);
1144         } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
1145             while (res < frame_end)
1146                 put_sbits(&s->pb, sub->obits, *res++);
1147         } else {
1148             /* warm-up samples */
1149             for (i = 0; i < sub->order; i++)
1150                 put_sbits(&s->pb, sub->obits, *res++);
1151
1152             /* LPC coefficients */
1153             if (sub->type == FLAC_SUBFRAME_LPC) {
1154                 int cbits = s->options.lpc_coeff_precision;
1155                 put_bits( &s->pb, 4, cbits-1);
1156                 put_sbits(&s->pb, 5, sub->shift);
1157                 for (i = 0; i < sub->order; i++)
1158                     put_sbits(&s->pb, cbits, sub->coefs[i]);
1159             }
1160
1161             /* rice-encoded block */
1162             put_bits(&s->pb, 2, 0);
1163
1164             /* partition order */
1165             porder  = sub->rc.porder;
1166             psize   = s->frame.blocksize >> porder;
1167             put_bits(&s->pb, 4, porder);
1168
1169             /* residual */
1170             part_end  = &sub->residual[psize];
1171             for (p = 0; p < 1 << porder; p++) {
1172                 int k = sub->rc.params[p];
1173                 put_bits(&s->pb, 4, k);
1174                 while (res < part_end)
1175                     set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
1176                 part_end = FFMIN(frame_end, part_end + psize);
1177             }
1178         }
1179     }
1180 }
1181
1182
1183 static void write_frame_footer(FlacEncodeContext *s)
1184 {
1185     int crc;
1186     flush_put_bits(&s->pb);
1187     crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
1188                             put_bits_count(&s->pb)>>3));
1189     put_bits(&s->pb, 16, crc);
1190     flush_put_bits(&s->pb);
1191 }
1192
1193
1194 static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
1195 {
1196     init_put_bits(&s->pb, avpkt->data, avpkt->size);
1197     write_frame_header(s);
1198     write_subframes(s);
1199     write_frame_footer(s);
1200     return put_bits_count(&s->pb) >> 3;
1201 }
1202
1203
1204 static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
1205 {
1206 #if HAVE_BIGENDIAN
1207     int i;
1208     for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1209         int16_t smp = av_le2ne16(samples[i]);
1210         av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
1211     }
1212 #else
1213     av_md5_update(s->md5ctx, (const uint8_t *)samples, s->frame.blocksize*s->channels*2);
1214 #endif
1215 }
1216
1217
1218 static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1219                              const AVFrame *frame, int *got_packet_ptr)
1220 {
1221     FlacEncodeContext *s;
1222     const int16_t *samples;
1223     int frame_bytes, out_bytes, ret;
1224
1225     s = avctx->priv_data;
1226
1227     /* when the last block is reached, update the header in extradata */
1228     if (!frame) {
1229         s->max_framesize = s->max_encoded_framesize;
1230         av_md5_final(s->md5ctx, s->md5sum);
1231         write_streaminfo(s, avctx->extradata);
1232         return 0;
1233     }
1234     samples = (const int16_t *)frame->data[0];
1235
1236     /* change max_framesize for small final frame */
1237     if (frame->nb_samples < s->frame.blocksize) {
1238         s->max_framesize = ff_flac_get_max_frame_size(frame->nb_samples,
1239                                                       s->channels, 16);
1240     }
1241
1242     init_frame(s, frame->nb_samples);
1243
1244     copy_samples(s, samples);
1245
1246     channel_decorrelation(s);
1247
1248     frame_bytes = encode_frame(s);
1249
1250     /* fallback to verbatim mode if the compressed frame is larger than it
1251        would be if encoded uncompressed. */
1252     if (frame_bytes > s->max_framesize) {
1253         s->frame.verbatim_only = 1;
1254         frame_bytes = encode_frame(s);
1255     }
1256
1257     if ((ret = ff_alloc_packet2(avctx, avpkt, frame_bytes)))
1258         return ret;
1259
1260     out_bytes = write_frame(s, avpkt);
1261
1262     s->frame_count++;
1263     s->sample_count += frame->nb_samples;
1264     update_md5_sum(s, samples);
1265     if (out_bytes > s->max_encoded_framesize)
1266         s->max_encoded_framesize = out_bytes;
1267     if (out_bytes < s->min_framesize)
1268         s->min_framesize = out_bytes;
1269
1270     avpkt->pts      = frame->pts;
1271     avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
1272     avpkt->size     = out_bytes;
1273     *got_packet_ptr = 1;
1274     return 0;
1275 }
1276
1277
1278 static av_cold int flac_encode_close(AVCodecContext *avctx)
1279 {
1280     if (avctx->priv_data) {
1281         FlacEncodeContext *s = avctx->priv_data;
1282         av_freep(&s->md5ctx);
1283         ff_lpc_end(&s->lpc_ctx);
1284     }
1285     av_freep(&avctx->extradata);
1286     avctx->extradata_size = 0;
1287 #if FF_API_OLD_ENCODE_AUDIO
1288     av_freep(&avctx->coded_frame);
1289 #endif
1290     return 0;
1291 }
1292
1293 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1294 static const AVOption options[] = {
1295 { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.dbl = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
1296 { "lpc_type", "LPC algorithm", offsetof(FlacEncodeContext, options.lpc_type), AV_OPT_TYPE_INT, {.dbl = FF_LPC_TYPE_DEFAULT }, FF_LPC_TYPE_DEFAULT, FF_LPC_TYPE_NB-1, FLAGS, "lpc_type" },
1297 { "none",     NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_NONE },     INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1298 { "fixed",    NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_FIXED },    INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1299 { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1300 { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1301 { "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes),  AV_OPT_TYPE_INT, {.dbl = -1 }, INT_MIN, INT_MAX, FLAGS },
1302 { "min_partition_order",  NULL, offsetof(FlacEncodeContext, options.min_partition_order),  AV_OPT_TYPE_INT, {.dbl = -1 },      -1, MAX_PARTITION_ORDER, FLAGS },
1303 { "max_partition_order",  NULL, offsetof(FlacEncodeContext, options.max_partition_order),  AV_OPT_TYPE_INT, {.dbl = -1 },      -1, MAX_PARTITION_ORDER, FLAGS },
1304 { "prediction_order_method", "Search method for selecting prediction order", offsetof(FlacEncodeContext, options.prediction_order_method), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, ORDER_METHOD_LOG, FLAGS, "predm" },
1305 { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_EST },    INT_MIN, INT_MAX, FLAGS, "predm" },
1306 { "2level",     NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1307 { "4level",     NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1308 { "8level",     NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1309 { "search",     NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
1310 { "log",        NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_LOG },    INT_MIN, INT_MAX, FLAGS, "predm" },
1311 { NULL },
1312 };
1313
1314 static const AVClass flac_encoder_class = {
1315     "FLAC encoder",
1316     av_default_item_name,
1317     options,
1318     LIBAVUTIL_VERSION_INT,
1319 };
1320
1321 AVCodec ff_flac_encoder = {
1322     .name           = "flac",
1323     .type           = AVMEDIA_TYPE_AUDIO,
1324     .id             = CODEC_ID_FLAC,
1325     .priv_data_size = sizeof(FlacEncodeContext),
1326     .init           = flac_encode_init,
1327     .encode2        = flac_encode_frame,
1328     .close          = flac_encode_close,
1329     .capabilities   = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_LOSSLESS,
1330     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
1331                                                      AV_SAMPLE_FMT_NONE },
1332     .long_name      = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
1333     .priv_class     = &flac_encoder_class,
1334 };