3 * Copyright (c) 2006 Justin Ruggles <justin.ruggles@gmail.com>
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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/intmath.h"
24 #include "libavutil/md5.h"
25 #include "libavutil/opt.h"
36 #define FLAC_SUBFRAME_CONSTANT 0
37 #define FLAC_SUBFRAME_VERBATIM 1
38 #define FLAC_SUBFRAME_FIXED 8
39 #define FLAC_SUBFRAME_LPC 32
41 #define MAX_FIXED_ORDER 4
42 #define MAX_PARTITION_ORDER 8
43 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
44 #define MAX_LPC_PRECISION 15
45 #define MAX_LPC_SHIFT 15
49 CODING_MODE_RICE2 = 5,
52 typedef struct CompressionOptions {
53 int compression_level;
55 enum FFLPCType lpc_type;
57 int lpc_coeff_precision;
58 int min_prediction_order;
59 int max_prediction_order;
60 int prediction_order_method;
61 int min_partition_order;
62 int max_partition_order;
66 typedef struct RiceContext {
67 enum CodingMode coding_mode;
69 int params[MAX_PARTITIONS];
72 typedef struct FlacSubframe {
78 int32_t coefs[MAX_LPC_ORDER];
81 int32_t samples[FLAC_MAX_BLOCKSIZE];
82 int32_t residual[FLAC_MAX_BLOCKSIZE+1];
85 typedef struct FlacFrame {
86 FlacSubframe subframes[FLAC_MAX_CHANNELS];
94 typedef struct FlacEncodeContext {
104 int max_encoded_framesize;
105 uint32_t frame_count;
106 uint64_t sample_count;
109 CompressionOptions options;
110 AVCodecContext *avctx;
112 struct AVMD5 *md5ctx;
114 unsigned int md5_buffer_size;
116 FLACDSPContext flac_dsp;
121 * Write streaminfo metadata block to byte array.
123 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
127 memset(header, 0, FLAC_STREAMINFO_SIZE);
128 init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
130 /* streaminfo metadata block */
131 put_bits(&pb, 16, s->max_blocksize);
132 put_bits(&pb, 16, s->max_blocksize);
133 put_bits(&pb, 24, s->min_framesize);
134 put_bits(&pb, 24, s->max_framesize);
135 put_bits(&pb, 20, s->samplerate);
136 put_bits(&pb, 3, s->channels-1);
137 put_bits(&pb, 5, s->avctx->bits_per_raw_sample - 1);
138 /* write 36-bit sample count in 2 put_bits() calls */
139 put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
140 put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
142 memcpy(&header[18], s->md5sum, 16);
147 * Set blocksize based on samplerate.
148 * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
150 static int select_blocksize(int samplerate, int block_time_ms)
156 assert(samplerate > 0);
157 blocksize = ff_flac_blocksize_table[1];
158 target = (samplerate * block_time_ms) / 1000;
159 for (i = 0; i < 16; i++) {
160 if (target >= ff_flac_blocksize_table[i] &&
161 ff_flac_blocksize_table[i] > blocksize) {
162 blocksize = ff_flac_blocksize_table[i];
169 static av_cold void dprint_compression_options(FlacEncodeContext *s)
171 AVCodecContext *avctx = s->avctx;
172 CompressionOptions *opt = &s->options;
174 av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);
176 switch (opt->lpc_type) {
177 case FF_LPC_TYPE_NONE:
178 av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
180 case FF_LPC_TYPE_FIXED:
181 av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
183 case FF_LPC_TYPE_LEVINSON:
184 av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
186 case FF_LPC_TYPE_CHOLESKY:
187 av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
188 opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
192 av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
193 opt->min_prediction_order, opt->max_prediction_order);
195 switch (opt->prediction_order_method) {
196 case ORDER_METHOD_EST:
197 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
199 case ORDER_METHOD_2LEVEL:
200 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
202 case ORDER_METHOD_4LEVEL:
203 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
205 case ORDER_METHOD_8LEVEL:
206 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
208 case ORDER_METHOD_SEARCH:
209 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
211 case ORDER_METHOD_LOG:
212 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
217 av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
218 opt->min_partition_order, opt->max_partition_order);
220 av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
222 av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
223 opt->lpc_coeff_precision);
227 static av_cold int flac_encode_init(AVCodecContext *avctx)
229 int freq = avctx->sample_rate;
230 int channels = avctx->channels;
231 FlacEncodeContext *s = avctx->priv_data;
237 switch (avctx->sample_fmt) {
238 case AV_SAMPLE_FMT_S16:
239 avctx->bits_per_raw_sample = 16;
242 case AV_SAMPLE_FMT_S32:
243 if (avctx->bits_per_raw_sample != 24)
244 av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
245 avctx->bits_per_raw_sample = 24;
250 if (channels < 1 || channels > FLAC_MAX_CHANNELS)
252 s->channels = channels;
254 /* find samplerate in table */
257 for (i = 4; i < 12; i++) {
258 if (freq == ff_flac_sample_rate_table[i]) {
259 s->samplerate = ff_flac_sample_rate_table[i];
265 /* if not in table, samplerate is non-standard */
267 if (freq % 1000 == 0 && freq < 255000) {
269 s->sr_code[1] = freq / 1000;
270 } else if (freq % 10 == 0 && freq < 655350) {
272 s->sr_code[1] = freq / 10;
273 } else if (freq < 65535) {
275 s->sr_code[1] = freq;
279 s->samplerate = freq;
282 /* set compression option defaults based on avctx->compression_level */
283 if (avctx->compression_level < 0)
284 s->options.compression_level = 5;
286 s->options.compression_level = avctx->compression_level;
288 level = s->options.compression_level;
290 av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
291 s->options.compression_level);
295 s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
297 if (s->options.lpc_type == FF_LPC_TYPE_DEFAULT)
298 s->options.lpc_type = ((int[]){ FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED,
299 FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
300 FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
301 FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
302 FF_LPC_TYPE_LEVINSON})[level];
304 s->options.min_prediction_order = ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
305 s->options.max_prediction_order = ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
307 if (s->options.prediction_order_method < 0)
308 s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
309 ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
310 ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG, ORDER_METHOD_4LEVEL,
311 ORDER_METHOD_LOG, ORDER_METHOD_SEARCH, ORDER_METHOD_LOG,
312 ORDER_METHOD_SEARCH})[level];
314 if (s->options.min_partition_order > s->options.max_partition_order) {
315 av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
316 s->options.min_partition_order, s->options.max_partition_order);
317 return AVERROR(EINVAL);
319 if (s->options.min_partition_order < 0)
320 s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
321 if (s->options.max_partition_order < 0)
322 s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
324 if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
325 s->options.min_prediction_order = 0;
326 } else if (avctx->min_prediction_order >= 0) {
327 if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
328 if (avctx->min_prediction_order > MAX_FIXED_ORDER) {
329 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
330 avctx->min_prediction_order);
333 } else if (avctx->min_prediction_order < MIN_LPC_ORDER ||
334 avctx->min_prediction_order > MAX_LPC_ORDER) {
335 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
336 avctx->min_prediction_order);
339 s->options.min_prediction_order = avctx->min_prediction_order;
341 if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
342 s->options.max_prediction_order = 0;
343 } else if (avctx->max_prediction_order >= 0) {
344 if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
345 if (avctx->max_prediction_order > MAX_FIXED_ORDER) {
346 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
347 avctx->max_prediction_order);
350 } else if (avctx->max_prediction_order < MIN_LPC_ORDER ||
351 avctx->max_prediction_order > MAX_LPC_ORDER) {
352 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
353 avctx->max_prediction_order);
356 s->options.max_prediction_order = avctx->max_prediction_order;
358 if (s->options.max_prediction_order < s->options.min_prediction_order) {
359 av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
360 s->options.min_prediction_order, s->options.max_prediction_order);
364 if (avctx->frame_size > 0) {
365 if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
366 avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
367 av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
372 s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
374 s->max_blocksize = s->avctx->frame_size;
376 /* set maximum encoded frame size in verbatim mode */
377 s->max_framesize = ff_flac_get_max_frame_size(s->avctx->frame_size,
379 s->avctx->bits_per_raw_sample);
381 /* initialize MD5 context */
382 s->md5ctx = av_md5_alloc();
384 return AVERROR(ENOMEM);
385 av_md5_init(s->md5ctx);
387 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
389 return AVERROR(ENOMEM);
390 write_streaminfo(s, streaminfo);
391 avctx->extradata = streaminfo;
392 avctx->extradata_size = FLAC_STREAMINFO_SIZE;
395 s->min_framesize = s->max_framesize;
397 #if FF_API_OLD_ENCODE_AUDIO
398 avctx->coded_frame = avcodec_alloc_frame();
399 if (!avctx->coded_frame)
400 return AVERROR(ENOMEM);
403 ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
404 s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
406 ff_dsputil_init(&s->dsp, avctx);
407 ff_flacdsp_init(&s->flac_dsp, avctx->sample_fmt,
408 avctx->bits_per_raw_sample);
410 dprint_compression_options(s);
416 static void init_frame(FlacEncodeContext *s, int nb_samples)
423 for (i = 0; i < 16; i++) {
424 if (nb_samples == ff_flac_blocksize_table[i]) {
425 frame->blocksize = ff_flac_blocksize_table[i];
426 frame->bs_code[0] = i;
427 frame->bs_code[1] = 0;
432 frame->blocksize = nb_samples;
433 if (frame->blocksize <= 256) {
434 frame->bs_code[0] = 6;
435 frame->bs_code[1] = frame->blocksize-1;
437 frame->bs_code[0] = 7;
438 frame->bs_code[1] = frame->blocksize-1;
442 for (ch = 0; ch < s->channels; ch++) {
443 FlacSubframe *sub = &frame->subframes[ch];
446 sub->obits = s->avctx->bits_per_raw_sample;
449 sub->rc.coding_mode = CODING_MODE_RICE2;
451 sub->rc.coding_mode = CODING_MODE_RICE;
454 frame->verbatim_only = 0;
459 * Copy channel-interleaved input samples into separate subframes.
461 static void copy_samples(FlacEncodeContext *s, const void *samples)
465 int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
466 s->avctx->bits_per_raw_sample;
468 #define COPY_SAMPLES(bits) do { \
469 const int ## bits ## _t *samples0 = samples; \
471 for (i = 0, j = 0; i < frame->blocksize; i++) \
472 for (ch = 0; ch < s->channels; ch++, j++) \
473 frame->subframes[ch].samples[i] = samples0[j] >> shift; \
476 if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S16)
483 static uint64_t rice_count_exact(int32_t *res, int n, int k)
488 for (i = 0; i < n; i++) {
489 int32_t v = -2 * res[i] - 1;
491 count += (v >> k) + 1 + k;
497 static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
500 int p, porder, psize;
504 /* subframe header */
508 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
510 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
511 count += s->frame.blocksize * sub->obits;
513 /* warm-up samples */
514 count += pred_order * sub->obits;
516 /* LPC coefficients */
517 if (sub->type == FLAC_SUBFRAME_LPC)
518 count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
520 /* rice-encoded block */
523 /* partition order */
524 porder = sub->rc.porder;
525 psize = s->frame.blocksize >> porder;
531 for (p = 0; p < 1 << porder; p++) {
532 int k = sub->rc.params[p];
533 count += sub->rc.coding_mode;
534 count += rice_count_exact(&sub->residual[i], part_end - i, k);
536 part_end = FFMIN(s->frame.blocksize, part_end + psize);
544 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
547 * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
549 static int find_optimal_param(uint64_t sum, int n, int max_param)
556 sum2 = sum - (n >> 1);
557 k = av_log2(av_clipl_int32(sum2 / n));
558 return FFMIN(k, max_param);
562 static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder,
563 uint64_t *sums, int n, int pred_order)
566 int k, cnt, part, max_param;
569 max_param = (1 << rc->coding_mode) - 2;
571 part = (1 << porder);
574 cnt = (n >> porder) - pred_order;
575 for (i = 0; i < part; i++) {
576 k = find_optimal_param(sums[i], cnt, max_param);
578 all_bits += rice_encode_count(sums[i], cnt, k);
588 static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
589 uint64_t sums[][MAX_PARTITIONS])
593 uint32_t *res, *res_end;
595 /* sums for highest level */
597 res = &data[pred_order];
598 res_end = &data[n >> pmax];
599 for (i = 0; i < parts; i++) {
601 while (res < res_end)
604 res_end += n >> pmax;
606 /* sums for lower levels */
607 for (i = pmax - 1; i >= pmin; i--) {
609 for (j = 0; j < parts; j++)
610 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
615 static uint64_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
616 int32_t *data, int n, int pred_order)
619 uint64_t bits[MAX_PARTITION_ORDER+1];
623 uint64_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
625 assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
626 assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
627 assert(pmin <= pmax);
629 tmp_rc.coding_mode = rc->coding_mode;
631 udata = av_malloc(n * sizeof(uint32_t));
632 for (i = 0; i < n; i++)
633 udata[i] = (2*data[i]) ^ (data[i]>>31);
635 calc_sums(pmin, pmax, udata, n, pred_order, sums);
638 bits[pmin] = UINT32_MAX;
639 for (i = pmin; i <= pmax; i++) {
640 bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
641 if (bits[i] <= bits[opt_porder]) {
648 return bits[opt_porder];
652 static int get_max_p_order(int max_porder, int n, int order)
654 int porder = FFMIN(max_porder, av_log2(n^(n-1)));
656 porder = FFMIN(porder, av_log2(n/order));
661 static uint64_t find_subframe_rice_params(FlacEncodeContext *s,
662 FlacSubframe *sub, int pred_order)
664 int pmin = get_max_p_order(s->options.min_partition_order,
665 s->frame.blocksize, pred_order);
666 int pmax = get_max_p_order(s->options.max_partition_order,
667 s->frame.blocksize, pred_order);
669 uint64_t bits = 8 + pred_order * sub->obits + 2 + sub->rc.coding_mode;
670 if (sub->type == FLAC_SUBFRAME_LPC)
671 bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
672 bits += calc_rice_params(&sub->rc, pmin, pmax, sub->residual,
673 s->frame.blocksize, pred_order);
678 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
683 for (i = 0; i < order; i++)
687 for (i = order; i < n; i++)
689 } else if (order == 1) {
690 for (i = order; i < n; i++)
691 res[i] = smp[i] - smp[i-1];
692 } else if (order == 2) {
693 int a = smp[order-1] - smp[order-2];
694 for (i = order; i < n; i += 2) {
695 int b = smp[i ] - smp[i-1];
697 a = smp[i+1] - smp[i ];
700 } else if (order == 3) {
701 int a = smp[order-1] - smp[order-2];
702 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
703 for (i = order; i < n; i += 2) {
704 int b = smp[i ] - smp[i-1];
707 a = smp[i+1] - smp[i ];
712 int a = smp[order-1] - smp[order-2];
713 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
714 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
715 for (i = order; i < n; i += 2) {
716 int b = smp[i ] - smp[i-1];
720 a = smp[i+1] - smp[i ];
729 static int encode_residual_ch(FlacEncodeContext *s, int ch)
732 int min_order, max_order, opt_order, omethod;
735 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
736 int shift[MAX_LPC_ORDER];
740 sub = &frame->subframes[ch];
743 n = frame->blocksize;
746 for (i = 1; i < n; i++)
750 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
752 return subframe_count_exact(s, sub, 0);
756 if (frame->verbatim_only || n < 5) {
757 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
758 memcpy(res, smp, n * sizeof(int32_t));
759 return subframe_count_exact(s, sub, 0);
762 min_order = s->options.min_prediction_order;
763 max_order = s->options.max_prediction_order;
764 omethod = s->options.prediction_order_method;
767 sub->type = FLAC_SUBFRAME_FIXED;
768 if (s->options.lpc_type == FF_LPC_TYPE_NONE ||
769 s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
770 uint64_t bits[MAX_FIXED_ORDER+1];
771 if (max_order > MAX_FIXED_ORDER)
772 max_order = MAX_FIXED_ORDER;
774 bits[0] = UINT32_MAX;
775 for (i = min_order; i <= max_order; i++) {
776 encode_residual_fixed(res, smp, n, i);
777 bits[i] = find_subframe_rice_params(s, sub, i);
778 if (bits[i] < bits[opt_order])
781 sub->order = opt_order;
782 sub->type_code = sub->type | sub->order;
783 if (sub->order != max_order) {
784 encode_residual_fixed(res, smp, n, sub->order);
785 find_subframe_rice_params(s, sub, sub->order);
787 return subframe_count_exact(s, sub, sub->order);
791 sub->type = FLAC_SUBFRAME_LPC;
792 opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
793 s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
794 s->options.lpc_passes, omethod,
797 if (omethod == ORDER_METHOD_2LEVEL ||
798 omethod == ORDER_METHOD_4LEVEL ||
799 omethod == ORDER_METHOD_8LEVEL) {
800 int levels = 1 << omethod;
801 uint64_t bits[1 << ORDER_METHOD_8LEVEL];
803 int opt_index = levels-1;
804 opt_order = max_order-1;
805 bits[opt_index] = UINT32_MAX;
806 for (i = levels-1; i >= 0; i--) {
807 int last_order = order;
808 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
809 order = av_clip(order, min_order - 1, max_order - 1);
810 if (order == last_order)
812 s->flac_dsp.lpc_encode(res, smp, n, order+1, coefs[order],
814 bits[i] = find_subframe_rice_params(s, sub, order+1);
815 if (bits[i] < bits[opt_index]) {
821 } else if (omethod == ORDER_METHOD_SEARCH) {
822 // brute-force optimal order search
823 uint64_t bits[MAX_LPC_ORDER];
825 bits[0] = UINT32_MAX;
826 for (i = min_order-1; i < max_order; i++) {
827 s->flac_dsp.lpc_encode(res, smp, n, i+1, coefs[i], shift[i]);
828 bits[i] = find_subframe_rice_params(s, sub, i+1);
829 if (bits[i] < bits[opt_order])
833 } else if (omethod == ORDER_METHOD_LOG) {
834 uint64_t bits[MAX_LPC_ORDER];
837 opt_order = min_order - 1 + (max_order-min_order)/3;
838 memset(bits, -1, sizeof(bits));
840 for (step = 16; step; step >>= 1) {
841 int last = opt_order;
842 for (i = last-step; i <= last+step; i += step) {
843 if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
845 s->flac_dsp.lpc_encode(res, smp, n, i+1, coefs[i], shift[i]);
846 bits[i] = find_subframe_rice_params(s, sub, i+1);
847 if (bits[i] < bits[opt_order])
854 sub->order = opt_order;
855 sub->type_code = sub->type | (sub->order-1);
856 sub->shift = shift[sub->order-1];
857 for (i = 0; i < sub->order; i++)
858 sub->coefs[i] = coefs[sub->order-1][i];
860 s->flac_dsp.lpc_encode(res, smp, n, sub->order, sub->coefs, sub->shift);
862 find_subframe_rice_params(s, sub, sub->order);
864 return subframe_count_exact(s, sub, sub->order);
868 static int count_frame_header(FlacEncodeContext *s)
870 uint8_t av_unused tmp;
876 <1> Blocking strategy
877 <4> Block size in inter-channel samples
879 <4> Channel assignment
880 <3> Sample size in bits
885 /* coded frame number */
886 PUT_UTF8(s->frame_count, tmp, count += 8;)
888 /* explicit block size */
889 if (s->frame.bs_code[0] == 6)
891 else if (s->frame.bs_code[0] == 7)
894 /* explicit sample rate */
895 count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12)) * 8;
897 /* frame header CRC-8 */
904 static int encode_frame(FlacEncodeContext *s)
909 count = count_frame_header(s);
911 for (ch = 0; ch < s->channels; ch++)
912 count += encode_residual_ch(s, ch);
914 count += (8 - (count & 7)) & 7; // byte alignment
915 count += 16; // CRC-16
924 static void remove_wasted_bits(FlacEncodeContext *s)
928 for (ch = 0; ch < s->channels; ch++) {
929 FlacSubframe *sub = &s->frame.subframes[ch];
932 for (i = 0; i < s->frame.blocksize; i++) {
933 v |= sub->samples[i];
941 for (i = 0; i < s->frame.blocksize; i++)
942 sub->samples[i] >>= v;
947 /* for 24-bit, check if removing wasted bits makes the range better
948 suited for using RICE instead of RICE2 for entropy coding */
949 if (sub->obits <= 17)
950 sub->rc.coding_mode = CODING_MODE_RICE;
956 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n,
965 /* calculate sum of 2nd order residual for each channel */
966 sum[0] = sum[1] = sum[2] = sum[3] = 0;
967 for (i = 2; i < n; i++) {
968 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
969 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
970 sum[2] += FFABS((lt + rt) >> 1);
971 sum[3] += FFABS(lt - rt);
975 /* estimate bit counts */
976 for (i = 0; i < 4; i++) {
977 k = find_optimal_param(2 * sum[i], n, max_rice_param);
978 sum[i] = rice_encode_count( 2 * sum[i], n, k);
981 /* calculate score for each mode */
982 score[0] = sum[0] + sum[1];
983 score[1] = sum[0] + sum[3];
984 score[2] = sum[1] + sum[3];
985 score[3] = sum[2] + sum[3];
987 /* return mode with lowest score */
989 for (i = 1; i < 4; i++)
990 if (score[i] < score[best])
998 * Perform stereo channel decorrelation.
1000 static void channel_decorrelation(FlacEncodeContext *s)
1003 int32_t *left, *right;
1007 n = frame->blocksize;
1008 left = frame->subframes[0].samples;
1009 right = frame->subframes[1].samples;
1011 if (s->channels != 2) {
1012 frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
1016 if (s->options.ch_mode < 0) {
1017 int max_rice_param = (1 << frame->subframes[0].rc.coding_mode) - 2;
1018 frame->ch_mode = estimate_stereo_mode(left, right, n, max_rice_param);
1020 frame->ch_mode = s->options.ch_mode;
1022 /* perform decorrelation and adjust bits-per-sample */
1023 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1025 if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1027 for (i = 0; i < n; i++) {
1029 left[i] = (tmp + right[i]) >> 1;
1030 right[i] = tmp - right[i];
1032 frame->subframes[1].obits++;
1033 } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1034 for (i = 0; i < n; i++)
1035 right[i] = left[i] - right[i];
1036 frame->subframes[1].obits++;
1038 for (i = 0; i < n; i++)
1039 left[i] -= right[i];
1040 frame->subframes[0].obits++;
1045 static void write_utf8(PutBitContext *pb, uint32_t val)
1048 PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1052 static void write_frame_header(FlacEncodeContext *s)
1059 put_bits(&s->pb, 16, 0xFFF8);
1060 put_bits(&s->pb, 4, frame->bs_code[0]);
1061 put_bits(&s->pb, 4, s->sr_code[0]);
1063 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1064 put_bits(&s->pb, 4, s->channels-1);
1066 put_bits(&s->pb, 4, frame->ch_mode + FLAC_MAX_CHANNELS - 1);
1068 put_bits(&s->pb, 3, s->bps_code);
1069 put_bits(&s->pb, 1, 0);
1070 write_utf8(&s->pb, s->frame_count);
1072 if (frame->bs_code[0] == 6)
1073 put_bits(&s->pb, 8, frame->bs_code[1]);
1074 else if (frame->bs_code[0] == 7)
1075 put_bits(&s->pb, 16, frame->bs_code[1]);
1077 if (s->sr_code[0] == 12)
1078 put_bits(&s->pb, 8, s->sr_code[1]);
1079 else if (s->sr_code[0] > 12)
1080 put_bits(&s->pb, 16, s->sr_code[1]);
1082 flush_put_bits(&s->pb);
1083 crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
1084 put_bits_count(&s->pb) >> 3);
1085 put_bits(&s->pb, 8, crc);
1089 static void write_subframes(FlacEncodeContext *s)
1093 for (ch = 0; ch < s->channels; ch++) {
1094 FlacSubframe *sub = &s->frame.subframes[ch];
1095 int i, p, porder, psize;
1097 int32_t *res = sub->residual;
1098 int32_t *frame_end = &sub->residual[s->frame.blocksize];
1100 /* subframe header */
1101 put_bits(&s->pb, 1, 0);
1102 put_bits(&s->pb, 6, sub->type_code);
1103 put_bits(&s->pb, 1, !!sub->wasted);
1105 put_bits(&s->pb, sub->wasted, 1);
1108 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
1109 put_sbits(&s->pb, sub->obits, res[0]);
1110 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
1111 while (res < frame_end)
1112 put_sbits(&s->pb, sub->obits, *res++);
1114 /* warm-up samples */
1115 for (i = 0; i < sub->order; i++)
1116 put_sbits(&s->pb, sub->obits, *res++);
1118 /* LPC coefficients */
1119 if (sub->type == FLAC_SUBFRAME_LPC) {
1120 int cbits = s->options.lpc_coeff_precision;
1121 put_bits( &s->pb, 4, cbits-1);
1122 put_sbits(&s->pb, 5, sub->shift);
1123 for (i = 0; i < sub->order; i++)
1124 put_sbits(&s->pb, cbits, sub->coefs[i]);
1127 /* rice-encoded block */
1128 put_bits(&s->pb, 2, sub->rc.coding_mode - 4);
1130 /* partition order */
1131 porder = sub->rc.porder;
1132 psize = s->frame.blocksize >> porder;
1133 put_bits(&s->pb, 4, porder);
1136 part_end = &sub->residual[psize];
1137 for (p = 0; p < 1 << porder; p++) {
1138 int k = sub->rc.params[p];
1139 put_bits(&s->pb, sub->rc.coding_mode, k);
1140 while (res < part_end)
1141 set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
1142 part_end = FFMIN(frame_end, part_end + psize);
1149 static void write_frame_footer(FlacEncodeContext *s)
1152 flush_put_bits(&s->pb);
1153 crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
1154 put_bits_count(&s->pb)>>3));
1155 put_bits(&s->pb, 16, crc);
1156 flush_put_bits(&s->pb);
1160 static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
1162 init_put_bits(&s->pb, avpkt->data, avpkt->size);
1163 write_frame_header(s);
1165 write_frame_footer(s);
1166 return put_bits_count(&s->pb) >> 3;
1170 static int update_md5_sum(FlacEncodeContext *s, const void *samples)
1173 int buf_size = s->frame.blocksize * s->channels *
1174 ((s->avctx->bits_per_raw_sample + 7) / 8);
1176 if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
1177 av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size);
1179 return AVERROR(ENOMEM);
1182 if (s->avctx->bits_per_raw_sample <= 16) {
1183 buf = (const uint8_t *)samples;
1185 s->dsp.bswap16_buf((uint16_t *)s->md5_buffer,
1186 (const uint16_t *)samples, buf_size / 2);
1187 buf = s->md5_buffer;
1191 const int32_t *samples0 = samples;
1192 uint8_t *tmp = s->md5_buffer;
1194 for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1195 int32_t v = samples0[i] >> 8;
1196 *tmp++ = (v ) & 0xFF;
1197 *tmp++ = (v >> 8) & 0xFF;
1198 *tmp++ = (v >> 16) & 0xFF;
1200 buf = s->md5_buffer;
1202 av_md5_update(s->md5ctx, buf, buf_size);
1208 static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1209 const AVFrame *frame, int *got_packet_ptr)
1211 FlacEncodeContext *s;
1212 int frame_bytes, out_bytes, ret;
1214 s = avctx->priv_data;
1216 /* when the last block is reached, update the header in extradata */
1218 s->max_framesize = s->max_encoded_framesize;
1219 av_md5_final(s->md5ctx, s->md5sum);
1220 write_streaminfo(s, avctx->extradata);
1224 /* change max_framesize for small final frame */
1225 if (frame->nb_samples < s->frame.blocksize) {
1226 s->max_framesize = ff_flac_get_max_frame_size(frame->nb_samples,
1228 avctx->bits_per_raw_sample);
1231 init_frame(s, frame->nb_samples);
1233 copy_samples(s, frame->data[0]);
1235 channel_decorrelation(s);
1237 remove_wasted_bits(s);
1239 frame_bytes = encode_frame(s);
1241 /* fallback to verbatim mode if the compressed frame is larger than it
1242 would be if encoded uncompressed. */
1243 if (frame_bytes < 0 || frame_bytes > s->max_framesize) {
1244 s->frame.verbatim_only = 1;
1245 frame_bytes = encode_frame(s);
1246 if (frame_bytes < 0) {
1247 av_log(avctx, AV_LOG_ERROR, "Bad frame count\n");
1252 if ((ret = ff_alloc_packet(avpkt, frame_bytes))) {
1253 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
1257 out_bytes = write_frame(s, avpkt);
1260 s->sample_count += frame->nb_samples;
1261 if ((ret = update_md5_sum(s, frame->data[0])) < 0) {
1262 av_log(avctx, AV_LOG_ERROR, "Error updating MD5 checksum\n");
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;
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;
1278 static av_cold int flac_encode_close(AVCodecContext *avctx)
1280 if (avctx->priv_data) {
1281 FlacEncodeContext *s = avctx->priv_data;
1282 av_freep(&s->md5ctx);
1283 av_freep(&s->md5_buffer);
1284 ff_lpc_end(&s->lpc_ctx);
1286 av_freep(&avctx->extradata);
1287 avctx->extradata_size = 0;
1288 #if FF_API_OLD_ENCODE_AUDIO
1289 av_freep(&avctx->coded_frame);
1294 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1295 static const AVOption options[] = {
1296 { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
1297 { "lpc_type", "LPC algorithm", offsetof(FlacEncodeContext, options.lpc_type), AV_OPT_TYPE_INT, {.i64 = FF_LPC_TYPE_DEFAULT }, FF_LPC_TYPE_DEFAULT, FF_LPC_TYPE_NB-1, FLAGS, "lpc_type" },
1298 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1299 { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1300 { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1301 { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1302 { "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes), AV_OPT_TYPE_INT, {.i64 = 1 }, 1, INT_MAX, FLAGS },
1303 { "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1304 { "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1305 { "prediction_order_method", "Search method for selecting prediction order", offsetof(FlacEncodeContext, options.prediction_order_method), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, ORDER_METHOD_LOG, FLAGS, "predm" },
1306 { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
1307 { "2level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1308 { "4level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1309 { "8level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1310 { "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
1311 { "log", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
1312 { "ch_mode", "Stereo decorrelation mode", offsetof(FlacEncodeContext, options.ch_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FLAC_CHMODE_MID_SIDE, FLAGS, "ch_mode" },
1313 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1314 { "indep", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1315 { "left_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1316 { "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1317 { "mid_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1321 static const AVClass flac_encoder_class = {
1323 av_default_item_name,
1325 LIBAVUTIL_VERSION_INT,
1328 AVCodec ff_flac_encoder = {
1330 .type = AVMEDIA_TYPE_AUDIO,
1331 .id = AV_CODEC_ID_FLAC,
1332 .priv_data_size = sizeof(FlacEncodeContext),
1333 .init = flac_encode_init,
1334 .encode2 = flac_encode_frame,
1335 .close = flac_encode_close,
1336 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
1337 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
1339 AV_SAMPLE_FMT_NONE },
1340 .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
1341 .priv_class = &flac_encoder_class,