3 * Copyright (c) 2006 Justin Ruggles <justin.ruggles@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/crc.h"
23 #include "libavutil/md5.h"
32 #define FLAC_SUBFRAME_CONSTANT 0
33 #define FLAC_SUBFRAME_VERBATIM 1
34 #define FLAC_SUBFRAME_FIXED 8
35 #define FLAC_SUBFRAME_LPC 32
37 #define MAX_FIXED_ORDER 4
38 #define MAX_PARTITION_ORDER 8
39 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
40 #define MAX_LPC_PRECISION 15
41 #define MAX_LPC_SHIFT 15
42 #define MAX_RICE_PARAM 14
44 typedef struct CompressionOptions {
45 int compression_level;
47 enum AVLPCType lpc_type;
49 int lpc_coeff_precision;
50 int min_prediction_order;
51 int max_prediction_order;
52 int prediction_order_method;
53 int min_partition_order;
54 int max_partition_order;
57 typedef struct RiceContext {
59 int params[MAX_PARTITIONS];
62 typedef struct FlacSubframe {
67 int32_t coefs[MAX_LPC_ORDER];
70 int32_t samples[FLAC_MAX_BLOCKSIZE];
71 int32_t residual[FLAC_MAX_BLOCKSIZE+1];
74 typedef struct FlacFrame {
75 FlacSubframe subframes[FLAC_MAX_CHANNELS];
82 typedef struct FlacEncodeContext {
90 int max_encoded_framesize;
92 uint64_t sample_count;
95 CompressionOptions options;
96 AVCodecContext *avctx;
103 * Write streaminfo metadata block to byte array.
105 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
109 memset(header, 0, FLAC_STREAMINFO_SIZE);
110 init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
112 /* streaminfo metadata block */
113 put_bits(&pb, 16, s->max_blocksize);
114 put_bits(&pb, 16, s->max_blocksize);
115 put_bits(&pb, 24, s->min_framesize);
116 put_bits(&pb, 24, s->max_framesize);
117 put_bits(&pb, 20, s->samplerate);
118 put_bits(&pb, 3, s->channels-1);
119 put_bits(&pb, 5, 15); /* bits per sample - 1 */
120 /* write 36-bit sample count in 2 put_bits() calls */
121 put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
122 put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
124 memcpy(&header[18], s->md5sum, 16);
129 * Set blocksize based on samplerate.
130 * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
132 static int select_blocksize(int samplerate, int block_time_ms)
138 assert(samplerate > 0);
139 blocksize = ff_flac_blocksize_table[1];
140 target = (samplerate * block_time_ms) / 1000;
141 for (i = 0; i < 16; i++) {
142 if (target >= ff_flac_blocksize_table[i] &&
143 ff_flac_blocksize_table[i] > blocksize) {
144 blocksize = ff_flac_blocksize_table[i];
151 static av_cold void dprint_compression_options(FlacEncodeContext *s)
153 AVCodecContext *avctx = s->avctx;
154 CompressionOptions *opt = &s->options;
156 av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);
158 switch (opt->lpc_type) {
159 case AV_LPC_TYPE_NONE:
160 av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
162 case AV_LPC_TYPE_FIXED:
163 av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
165 case AV_LPC_TYPE_LEVINSON:
166 av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
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");
174 av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
175 opt->min_prediction_order, opt->max_prediction_order);
177 switch (opt->prediction_order_method) {
178 case ORDER_METHOD_EST:
179 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
181 case ORDER_METHOD_2LEVEL:
182 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
184 case ORDER_METHOD_4LEVEL:
185 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
187 case ORDER_METHOD_8LEVEL:
188 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
190 case ORDER_METHOD_SEARCH:
191 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
193 case ORDER_METHOD_LOG:
194 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
199 av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
200 opt->min_partition_order, opt->max_partition_order);
202 av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
204 av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
205 opt->lpc_coeff_precision);
209 static av_cold int flac_encode_init(AVCodecContext *avctx)
211 int freq = avctx->sample_rate;
212 int channels = avctx->channels;
213 FlacEncodeContext *s = avctx->priv_data;
219 dsputil_init(&s->dsp, avctx);
221 if (avctx->sample_fmt != SAMPLE_FMT_S16)
224 if (channels < 1 || channels > FLAC_MAX_CHANNELS)
226 s->channels = channels;
228 /* find samplerate in table */
231 for (i = 4; i < 12; i++) {
232 if (freq == ff_flac_sample_rate_table[i]) {
233 s->samplerate = ff_flac_sample_rate_table[i];
239 /* if not in table, samplerate is non-standard */
241 if (freq % 1000 == 0 && freq < 255000) {
243 s->sr_code[1] = freq / 1000;
244 } else if (freq % 10 == 0 && freq < 655350) {
246 s->sr_code[1] = freq / 10;
247 } else if (freq < 65535) {
249 s->sr_code[1] = freq;
253 s->samplerate = freq;
256 /* set compression option defaults based on avctx->compression_level */
257 if (avctx->compression_level < 0)
258 s->options.compression_level = 5;
260 s->options.compression_level = avctx->compression_level;
262 level = s->options.compression_level;
264 av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
265 s->options.compression_level);
269 s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
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];
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];
280 s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
281 ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
282 ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG, ORDER_METHOD_4LEVEL,
283 ORDER_METHOD_LOG, ORDER_METHOD_SEARCH, ORDER_METHOD_LOG,
284 ORDER_METHOD_SEARCH})[level];
286 s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
287 s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
289 /* set compression option overrides from AVCodecContext */
290 #if LIBAVCODEC_VERSION_MAJOR < 53
291 /* for compatibility with deprecated AVCodecContext.use_lpc */
292 if (avctx->use_lpc == 0) {
293 s->options.lpc_type = AV_LPC_TYPE_FIXED;
294 } else if (avctx->use_lpc == 1) {
295 s->options.lpc_type = AV_LPC_TYPE_LEVINSON;
296 } else if (avctx->use_lpc > 1) {
297 s->options.lpc_type = AV_LPC_TYPE_CHOLESKY;
298 s->options.lpc_passes = avctx->use_lpc - 1;
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);
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",
316 s->options.lpc_passes = avctx->lpc_passes;
321 if (s->options.lpc_type == AV_LPC_TYPE_NONE) {
322 s->options.min_prediction_order = 0;
323 } else if (avctx->min_prediction_order >= 0) {
324 if (s->options.lpc_type == AV_LPC_TYPE_FIXED) {
325 if (avctx->min_prediction_order > MAX_FIXED_ORDER) {
326 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
327 avctx->min_prediction_order);
330 } else if (avctx->min_prediction_order < MIN_LPC_ORDER ||
331 avctx->min_prediction_order > MAX_LPC_ORDER) {
332 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
333 avctx->min_prediction_order);
336 s->options.min_prediction_order = avctx->min_prediction_order;
338 if (s->options.lpc_type == AV_LPC_TYPE_NONE) {
339 s->options.max_prediction_order = 0;
340 } else if (avctx->max_prediction_order >= 0) {
341 if (s->options.lpc_type == AV_LPC_TYPE_FIXED) {
342 if (avctx->max_prediction_order > MAX_FIXED_ORDER) {
343 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
344 avctx->max_prediction_order);
347 } else if (avctx->max_prediction_order < MIN_LPC_ORDER ||
348 avctx->max_prediction_order > MAX_LPC_ORDER) {
349 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
350 avctx->max_prediction_order);
353 s->options.max_prediction_order = avctx->max_prediction_order;
355 if (s->options.max_prediction_order < s->options.min_prediction_order) {
356 av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
357 s->options.min_prediction_order, s->options.max_prediction_order);
361 if (avctx->prediction_order_method >= 0) {
362 if (avctx->prediction_order_method > ORDER_METHOD_LOG) {
363 av_log(avctx, AV_LOG_ERROR, "invalid prediction order method: %d\n",
364 avctx->prediction_order_method);
367 s->options.prediction_order_method = avctx->prediction_order_method;
370 if (avctx->min_partition_order >= 0) {
371 if (avctx->min_partition_order > MAX_PARTITION_ORDER) {
372 av_log(avctx, AV_LOG_ERROR, "invalid min partition order: %d\n",
373 avctx->min_partition_order);
376 s->options.min_partition_order = avctx->min_partition_order;
378 if (avctx->max_partition_order >= 0) {
379 if (avctx->max_partition_order > MAX_PARTITION_ORDER) {
380 av_log(avctx, AV_LOG_ERROR, "invalid max partition order: %d\n",
381 avctx->max_partition_order);
384 s->options.max_partition_order = avctx->max_partition_order;
386 if (s->options.max_partition_order < s->options.min_partition_order) {
387 av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
388 s->options.min_partition_order, s->options.max_partition_order);
392 if (avctx->frame_size > 0) {
393 if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
394 avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
395 av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
400 s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
402 s->max_blocksize = s->avctx->frame_size;
404 /* set LPC precision */
405 if (avctx->lpc_coeff_precision > 0) {
406 if (avctx->lpc_coeff_precision > MAX_LPC_PRECISION) {
407 av_log(avctx, AV_LOG_ERROR, "invalid lpc coeff precision: %d\n",
408 avctx->lpc_coeff_precision);
411 s->options.lpc_coeff_precision = avctx->lpc_coeff_precision;
413 /* default LPC precision */
414 s->options.lpc_coeff_precision = 15;
417 /* set maximum encoded frame size in verbatim mode */
418 s->max_framesize = ff_flac_get_max_frame_size(s->avctx->frame_size,
421 /* initialize MD5 context */
422 s->md5ctx = av_malloc(av_md5_size);
424 return AVERROR(ENOMEM);
425 av_md5_init(s->md5ctx);
427 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
429 return AVERROR(ENOMEM);
430 write_streaminfo(s, streaminfo);
431 avctx->extradata = streaminfo;
432 avctx->extradata_size = FLAC_STREAMINFO_SIZE;
435 s->min_framesize = s->max_framesize;
437 avctx->coded_frame = avcodec_alloc_frame();
438 if (!avctx->coded_frame)
439 return AVERROR(ENOMEM);
441 dprint_compression_options(s);
447 static void init_frame(FlacEncodeContext *s)
454 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 frame->bs_code[0] = i;
458 frame->bs_code[1] = 0;
463 frame->blocksize = s->avctx->frame_size;
464 if (frame->blocksize <= 256) {
465 frame->bs_code[0] = 6;
466 frame->bs_code[1] = frame->blocksize-1;
468 frame->bs_code[0] = 7;
469 frame->bs_code[1] = frame->blocksize-1;
473 for (ch = 0; ch < s->channels; ch++)
474 frame->subframes[ch].obits = 16;
479 * Copy channel-interleaved input samples into separate subframes.
481 static void copy_samples(FlacEncodeContext *s, const int16_t *samples)
487 for (i = 0, j = 0; i < frame->blocksize; i++)
488 for (ch = 0; ch < s->channels; ch++, j++)
489 frame->subframes[ch].samples[i] = samples[j];
493 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
496 * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
498 static int find_optimal_param(uint32_t sum, int n)
505 sum2 = sum - (n >> 1);
506 k = av_log2(n < 256 ? FASTDIV(sum2, n) : sum2 / n);
507 return FFMIN(k, MAX_RICE_PARAM);
511 static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
512 uint32_t *sums, int n, int pred_order)
518 part = (1 << porder);
521 cnt = (n >> porder) - pred_order;
522 for (i = 0; i < part; i++) {
523 k = find_optimal_param(sums[i], cnt);
525 all_bits += rice_encode_count(sums[i], cnt, k);
535 static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
536 uint32_t sums[][MAX_PARTITIONS])
540 uint32_t *res, *res_end;
542 /* sums for highest level */
544 res = &data[pred_order];
545 res_end = &data[n >> pmax];
546 for (i = 0; i < parts; i++) {
548 while (res < res_end)
551 res_end += n >> pmax;
553 /* sums for lower levels */
554 for (i = pmax - 1; i >= pmin; i--) {
556 for (j = 0; j < parts; j++)
557 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
562 static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
563 int32_t *data, int n, int pred_order)
566 uint32_t bits[MAX_PARTITION_ORDER+1];
570 uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
572 assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
573 assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
574 assert(pmin <= pmax);
576 udata = av_malloc(n * sizeof(uint32_t));
577 for (i = 0; i < n; i++)
578 udata[i] = (2*data[i]) ^ (data[i]>>31);
580 calc_sums(pmin, pmax, udata, n, pred_order, sums);
583 bits[pmin] = UINT32_MAX;
584 for (i = pmin; i <= pmax; i++) {
585 bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
586 if (bits[i] <= bits[opt_porder]) {
593 return bits[opt_porder];
597 static int get_max_p_order(int max_porder, int n, int order)
599 int porder = FFMIN(max_porder, av_log2(n^(n-1)));
601 porder = FFMIN(porder, av_log2(n/order));
606 static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmin, int pmax,
607 int32_t *data, int n, int pred_order,
611 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 bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
619 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)
624 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 bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
632 static void encode_residual_verbatim(int32_t *res, int32_t *smp, int n)
635 memcpy(res, smp, n * sizeof(int32_t));
639 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
644 for (i = 0; i < order; i++)
648 for (i = order; i < n; 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 int a = smp[order-1] - smp[order-2];
655 for (i = order; i < n; i += 2) {
656 int b = smp[i ] - smp[i-1];
658 a = smp[i+1] - smp[i ];
661 } else if (order == 3) {
662 int a = smp[order-1] - smp[order-2];
663 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
664 for (i = order; i < n; i += 2) {
665 int b = smp[i ] - smp[i-1];
668 a = smp[i+1] - smp[i ];
673 int a = smp[order-1] - smp[order-2];
674 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
675 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
676 for (i = order; i < n; i += 2) {
677 int b = smp[i ] - smp[i-1];
681 a = smp[i+1] - smp[i ];
691 int c = coefs[(x)-1];\
697 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)
702 for (i = order; i < n; i += 2) {
703 int s = smp[i-order];
752 res[i ] = smp[i ] - (p0 >> shift);
753 res[i+1] = smp[i+1] - (p1 >> shift);
758 static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
759 int order, const int32_t *coefs, int shift)
762 for (i = 0; i < order; i++)
765 for (i = order; i < n; i += 2) {
769 for (j = 0; j < order; j++) {
775 res[i ] = smp[i ] - (p0 >> shift);
776 res[i+1] = smp[i+1] - (p1 >> shift);
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;
794 static int encode_residual(FlacEncodeContext *ctx, int ch)
797 int min_order, max_order, opt_order, precision, omethod;
798 int min_porder, max_porder;
801 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
802 int shift[MAX_LPC_ORDER];
806 sub = &frame->subframes[ch];
809 n = frame->blocksize;
812 for (i = 1; i < n; i++)
816 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
823 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
824 encode_residual_verbatim(res, smp, n);
825 return sub->obits * n;
828 min_order = ctx->options.min_prediction_order;
829 max_order = ctx->options.max_prediction_order;
830 min_porder = ctx->options.min_partition_order;
831 max_porder = ctx->options.max_partition_order;
832 precision = ctx->options.lpc_coeff_precision;
833 omethod = ctx->options.prediction_order_method;
836 if (ctx->options.lpc_type == AV_LPC_TYPE_NONE ||
837 ctx->options.lpc_type == AV_LPC_TYPE_FIXED || n <= max_order) {
838 uint32_t bits[MAX_FIXED_ORDER+1];
839 if (max_order > MAX_FIXED_ORDER)
840 max_order = MAX_FIXED_ORDER;
842 bits[0] = UINT32_MAX;
843 for (i = min_order; i <= max_order; i++) {
844 encode_residual_fixed(res, smp, n, i);
845 bits[i] = calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res,
847 if (bits[i] < bits[opt_order])
850 sub->order = opt_order;
851 sub->type = FLAC_SUBFRAME_FIXED;
852 sub->type_code = sub->type | sub->order;
853 if (sub->order != max_order) {
854 encode_residual_fixed(res, smp, n, sub->order);
855 return calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res, n,
856 sub->order, sub->obits);
858 return bits[sub->order];
862 opt_order = ff_lpc_calc_coefs(&ctx->dsp, smp, n, min_order, max_order,
863 precision, coefs, shift, ctx->options.lpc_type,
864 ctx->options.lpc_passes, omethod,
867 if (omethod == ORDER_METHOD_2LEVEL ||
868 omethod == ORDER_METHOD_4LEVEL ||
869 omethod == ORDER_METHOD_8LEVEL) {
870 int levels = 1 << omethod;
871 uint32_t bits[1 << ORDER_METHOD_8LEVEL];
873 int opt_index = levels-1;
874 opt_order = max_order-1;
875 bits[opt_index] = UINT32_MAX;
876 for (i = levels-1; i >= 0; i--) {
877 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
880 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 if (bits[i] < bits[opt_index]) {
889 } else if (omethod == ORDER_METHOD_SEARCH) {
890 // brute-force optimal order search
891 uint32_t bits[MAX_LPC_ORDER];
893 bits[0] = UINT32_MAX;
894 for (i = min_order-1; i < max_order; i++) {
895 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 if (bits[i] < bits[opt_order])
902 } else if (omethod == ORDER_METHOD_LOG) {
903 uint32_t bits[MAX_LPC_ORDER];
906 opt_order = min_order - 1 + (max_order-min_order)/3;
907 memset(bits, -1, sizeof(bits));
909 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)
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 res, n, i+1, sub->obits,
918 if (bits[i] < bits[opt_order])
925 sub->order = opt_order;
926 sub->type = FLAC_SUBFRAME_LPC;
927 sub->type_code = sub->type | (sub->order-1);
928 sub->shift = shift[sub->order-1];
929 for (i = 0; i < sub->order; i++)
930 sub->coefs[i] = coefs[sub->order-1][i];
932 encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
934 return calc_rice_params_lpc(&sub->rc, min_porder, max_porder, res, n,
935 sub->order, sub->obits, precision);
939 static int encode_residual_v(FlacEncodeContext *ctx, int ch)
947 sub = &frame->subframes[ch];
950 n = frame->blocksize;
953 for (i = 1; i < n; i++)
954 if (smp[i] != smp[0])
957 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
963 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
964 encode_residual_verbatim(res, smp, n);
965 return sub->obits * n;
969 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
977 /* calculate sum of 2nd order residual for each channel */
978 sum[0] = sum[1] = sum[2] = sum[3] = 0;
979 for (i = 2; i < n; i++) {
980 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
981 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
982 sum[2] += FFABS((lt + rt) >> 1);
983 sum[3] += FFABS(lt - rt);
987 /* estimate bit counts */
988 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);
993 /* calculate score for each mode */
994 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];
999 /* return mode with lowest score */
1001 for (i = 1; i < 4; i++)
1002 if (score[i] < score[best])
1005 return FLAC_CHMODE_INDEPENDENT;
1006 } else if (best == 1) {
1007 return FLAC_CHMODE_LEFT_SIDE;
1008 } else if (best == 2) {
1009 return FLAC_CHMODE_RIGHT_SIDE;
1011 return FLAC_CHMODE_MID_SIDE;
1017 * Perform stereo channel decorrelation.
1019 static void channel_decorrelation(FlacEncodeContext *ctx)
1022 int32_t *left, *right;
1025 frame = &ctx->frame;
1026 n = frame->blocksize;
1027 left = frame->subframes[0].samples;
1028 right = frame->subframes[1].samples;
1030 if (ctx->channels != 2) {
1031 frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
1035 frame->ch_mode = estimate_stereo_mode(left, right, n);
1037 /* perform decorrelation and adjust bits-per-sample */
1038 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1040 if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1042 for (i = 0; i < n; i++) {
1044 left[i] = (tmp + right[i]) >> 1;
1045 right[i] = tmp - right[i];
1047 frame->subframes[1].obits++;
1048 } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1049 for (i = 0; i < n; i++)
1050 right[i] = left[i] - right[i];
1051 frame->subframes[1].obits++;
1053 for (i = 0; i < n; i++)
1054 left[i] -= right[i];
1055 frame->subframes[0].obits++;
1060 static void write_utf8(PutBitContext *pb, uint32_t val)
1063 PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1067 static void output_frame_header(FlacEncodeContext *s)
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]);
1078 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1079 put_bits(&s->pb, 4, s->channels-1);
1081 put_bits(&s->pb, 4, frame->ch_mode);
1083 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);
1087 if (frame->bs_code[0] == 6)
1088 put_bits(&s->pb, 8, frame->bs_code[1]);
1089 else if (frame->bs_code[0] == 7)
1090 put_bits(&s->pb, 16, frame->bs_code[1]);
1092 if (s->sr_code[0] == 12)
1093 put_bits(&s->pb, 8, s->sr_code[1]);
1094 else if (s->sr_code[0] > 12)
1095 put_bits(&s->pb, 16, s->sr_code[1]);
1097 flush_put_bits(&s->pb);
1098 crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
1099 put_bits_count(&s->pb) >> 3);
1100 put_bits(&s->pb, 8, crc);
1104 static void output_subframe_constant(FlacEncodeContext *s, int ch)
1109 sub = &s->frame.subframes[ch];
1110 res = sub->residual[0];
1111 put_sbits(&s->pb, sub->obits, res);
1115 static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
1123 sub = &frame->subframes[ch];
1125 for (i = 0; i < frame->blocksize; i++) {
1126 res = sub->residual[i];
1127 put_sbits(&s->pb, sub->obits, res);
1132 static void output_residual(FlacEncodeContext *ctx, int ch)
1134 int i, j, p, n, parts;
1135 int k, porder, psize, res_cnt;
1140 frame = &ctx->frame;
1141 sub = &frame->subframes[ch];
1142 res = sub->residual;
1143 n = frame->blocksize;
1145 /* rice-encoded block */
1146 put_bits(&ctx->pb, 2, 0);
1148 /* partition order */
1149 porder = sub->rc.porder;
1150 psize = n >> porder;
1151 parts = (1 << porder);
1152 put_bits(&ctx->pb, 4, porder);
1153 res_cnt = psize - sub->order;
1157 for (p = 0; p < parts; p++) {
1158 k = sub->rc.params[p];
1159 put_bits(&ctx->pb, 4, k);
1162 for (i = 0; i < res_cnt && j < n; i++, j++)
1163 set_sr_golomb_flac(&ctx->pb, res[j], k, INT32_MAX, 0);
1168 static void output_subframe_fixed(FlacEncodeContext *ctx, int ch)
1174 frame = &ctx->frame;
1175 sub = &frame->subframes[ch];
1177 /* warm-up samples */
1178 for (i = 0; i < sub->order; i++)
1179 put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
1182 output_residual(ctx, ch);
1186 static void output_subframe_lpc(FlacEncodeContext *ctx, int ch)
1192 frame = &ctx->frame;
1193 sub = &frame->subframes[ch];
1195 /* warm-up samples */
1196 for (i = 0; i < sub->order; i++)
1197 put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
1199 /* LPC coefficients */
1200 cbits = ctx->options.lpc_coeff_precision;
1201 put_bits( &ctx->pb, 4, cbits-1);
1202 put_sbits(&ctx->pb, 5, sub->shift);
1203 for (i = 0; i < sub->order; i++)
1204 put_sbits(&ctx->pb, cbits, sub->coefs[i]);
1207 output_residual(ctx, ch);
1211 static void output_subframes(FlacEncodeContext *s)
1219 for (ch = 0; ch < s->channels; ch++) {
1220 sub = &frame->subframes[ch];
1222 /* subframe header */
1223 put_bits(&s->pb, 1, 0);
1224 put_bits(&s->pb, 6, sub->type_code);
1225 put_bits(&s->pb, 1, 0); /* no wasted bits */
1228 if(sub->type == FLAC_SUBFRAME_CONSTANT)
1229 output_subframe_constant(s, ch);
1230 else if(sub->type == FLAC_SUBFRAME_VERBATIM)
1231 output_subframe_verbatim(s, ch);
1232 else if(sub->type == FLAC_SUBFRAME_FIXED)
1233 output_subframe_fixed(s, ch);
1234 else if(sub->type == FLAC_SUBFRAME_LPC)
1235 output_subframe_lpc(s, ch);
1240 static void output_frame_footer(FlacEncodeContext *s)
1243 flush_put_bits(&s->pb);
1244 crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
1245 put_bits_count(&s->pb)>>3));
1246 put_bits(&s->pb, 16, crc);
1247 flush_put_bits(&s->pb);
1251 static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
1255 for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1256 int16_t smp = av_le2ne16(samples[i]);
1257 av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
1260 av_md5_update(s->md5ctx, (const uint8_t *)samples, s->frame.blocksize*s->channels*2);
1265 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
1266 int buf_size, void *data)
1269 FlacEncodeContext *s;
1270 const int16_t *samples = data;
1274 s = avctx->priv_data;
1276 if (buf_size < s->max_framesize * 2) {
1277 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
1281 /* when the last block is reached, update the header in extradata */
1283 s->max_framesize = s->max_encoded_framesize;
1284 av_md5_final(s->md5ctx, s->md5sum);
1285 write_streaminfo(s, avctx->extradata);
1291 copy_samples(s, samples);
1293 channel_decorrelation(s);
1295 for (ch = 0; ch < s->channels; ch++)
1296 encode_residual(s, ch);
1299 init_put_bits(&s->pb, frame, buf_size);
1300 output_frame_header(s);
1301 output_subframes(s);
1302 output_frame_footer(s);
1303 out_bytes = put_bits_count(&s->pb) >> 3;
1305 if (out_bytes > s->max_framesize) {
1307 /* still too large. must be an error. */
1308 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
1312 /* frame too large. use verbatim mode */
1313 for (ch = 0; ch < s->channels; ch++)
1314 encode_residual_v(s, ch);
1320 s->sample_count += avctx->frame_size;
1321 update_md5_sum(s, samples);
1322 if (out_bytes > s->max_encoded_framesize)
1323 s->max_encoded_framesize = out_bytes;
1324 if (out_bytes < s->min_framesize)
1325 s->min_framesize = out_bytes;
1331 static av_cold int flac_encode_close(AVCodecContext *avctx)
1333 if (avctx->priv_data) {
1334 FlacEncodeContext *s = avctx->priv_data;
1335 av_freep(&s->md5ctx);
1337 av_freep(&avctx->extradata);
1338 avctx->extradata_size = 0;
1339 av_freep(&avctx->coded_frame);
1344 AVCodec flac_encoder = {
1348 sizeof(FlacEncodeContext),
1353 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
1354 .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
1355 .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),