FFmpeg
Loading...
Searching...
No Matches
flacenc.c
Go to the documentation of this file.
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/avassert.h"
24#include "libavutil/crc.h"
25#include "libavutil/intmath.h"
26#include "libavutil/md5.h"
27#include "libavutil/mem.h"
28#include "libavutil/opt.h"
29
30#include "avcodec.h"
31#include "bswapdsp.h"
32#include "codec_internal.h"
33#include "encode.h"
34#include "put_bits.h"
35#include "lpc.h"
36#include "flac.h"
37#include "flacdata.h"
38#include "flacencdsp.h"
39
40#define FLAC_SUBFRAME_CONSTANT 0
41#define FLAC_SUBFRAME_VERBATIM 1
42#define FLAC_SUBFRAME_FIXED 8
43#define FLAC_SUBFRAME_LPC 32
44
45#define MAX_FIXED_ORDER 4
46#define MAX_PARTITION_ORDER 8
47#define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
48#define MAX_LPC_PRECISION 15
49#define MIN_LPC_SHIFT 0
50#define MAX_LPC_SHIFT 15
51
56
57typedef struct CompressionOptions {
60 int /* enum FFLPCType */ lpc_type;
72
73typedef struct RiceContext {
75 int porder;
78
95
105
133
134
135/**
136 * Write streaminfo metadata block to byte array.
137 */
139{
140 PutBitContext pb;
141
142 memset(header, 0, FLAC_STREAMINFO_SIZE);
144
145 /* streaminfo metadata block */
146 put_bits(&pb, 16, s->max_blocksize);
147 put_bits(&pb, 16, s->max_blocksize);
148 put_bits(&pb, 24, s->min_framesize);
149 put_bits(&pb, 24, s->max_framesize);
150 put_bits(&pb, 20, s->samplerate);
151 put_bits(&pb, 3, s->channels-1);
152 put_bits(&pb, 5, s->avctx->bits_per_raw_sample - 1);
153 /* write 36-bit sample count in 2 put_bits() calls */
154 put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
155 put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
156 flush_put_bits(&pb);
157 memcpy(&header[18], s->md5sum, 16);
158}
159
160
161/**
162 * Calculate an estimate for the maximum frame size based on verbatim mode.
163 * @param blocksize block size, in samples
164 * @param ch number of channels
165 * @param bps bits-per-sample
166 */
167static int flac_get_max_frame_size(int blocksize, int ch, int bps)
168{
169 /* Technically, there is no limit to FLAC frame size, but an encoder
170 should not write a frame that is larger than if verbatim encoding mode
171 were to be used. */
172
173 int count;
174
175 count = 16; /* frame header */
176 count += ch * ((7+bps+7)/8); /* subframe headers */
177 if (ch == 2) {
178 /* for stereo, need to account for using decorrelation */
179 count += (( 2*bps+1) * blocksize + 7) / 8;
180 } else {
181 count += ( ch*bps * blocksize + 7) / 8;
182 }
183 count += 2; /* frame footer */
184
185 return count;
186}
187
188
189/**
190 * Set blocksize based on samplerate.
191 * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
192 */
193static int select_blocksize(int samplerate, int block_time_ms)
194{
195 int i;
196 int target;
197 int blocksize;
198
199 av_assert0(samplerate > 0);
200 blocksize = ff_flac_blocksize_table[1];
201 target = (samplerate * block_time_ms) / 1000;
202 for (i = 0; i < 16; i++) {
203 if (target >= ff_flac_blocksize_table[i] &&
204 ff_flac_blocksize_table[i] > blocksize) {
205 blocksize = ff_flac_blocksize_table[i];
206 }
207 }
208 return blocksize;
209}
210
211
213{
214 AVCodecContext *avctx = s->avctx;
215 CompressionOptions *opt = &s->options;
216
217 av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);
218
219 switch (opt->lpc_type) {
220 case FF_LPC_TYPE_NONE:
221 av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
222 break;
224 av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
225 break;
227 av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
228 break;
230 av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
231 opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
232 break;
233 }
234
235 av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
237
238 switch (opt->prediction_order_method) {
239 case ORDER_METHOD_EST:
240 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
241 break;
243 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
244 break;
246 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
247 break;
249 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
250 break;
252 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
253 break;
254 case ORDER_METHOD_LOG:
255 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
256 break;
257 }
258
259
260 av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
262
263 av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
264
265 av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
267}
268
269
271{
272 int freq = avctx->sample_rate;
273 int channels = avctx->ch_layout.nb_channels;
274 FlacEncodeContext *s = avctx->priv_data;
275 int i, level, ret;
276 uint8_t *streaminfo;
277
278 s->avctx = avctx;
279
280 switch (avctx->sample_fmt) {
282 avctx->bits_per_raw_sample = 16;
283 s->bps_code = 4;
284 break;
286 if (avctx->bits_per_raw_sample <= 24) {
287 if (avctx->bits_per_raw_sample < 24)
288 av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
289 avctx->bits_per_raw_sample = 24;
290 s->bps_code = 6;
292 av_log(avctx, AV_LOG_WARNING,
293 "encoding as 24 bits-per-sample, more is considered "
294 "experimental. Add -strict experimental if you want "
295 "to encode more than 24 bits-per-sample\n");
296 avctx->bits_per_raw_sample = 24;
297 s->bps_code = 6;
298 } else {
299 avctx->bits_per_raw_sample = 32;
300 s->bps_code = 7;
301 }
302 break;
303 }
304
306 av_log(avctx, AV_LOG_ERROR, "%d channels not supported (max %d)\n",
308 return AVERROR(EINVAL);
309 }
310 s->channels = channels;
311
312 /* find samplerate in table */
313 if (freq < 1)
314 return AVERROR(EINVAL);
315 for (i = 1; i < 12; i++) {
316 if (freq == ff_flac_sample_rate_table[i]) {
317 s->samplerate = ff_flac_sample_rate_table[i];
318 s->sr_code[0] = i;
319 s->sr_code[1] = 0;
320 break;
321 }
322 }
323 /* if not in table, samplerate is non-standard */
324 if (i == 12) {
325 if (freq % 1000 == 0 && freq < 255000) {
326 s->sr_code[0] = 12;
327 s->sr_code[1] = freq / 1000;
328 } else if (freq % 10 == 0 && freq < 655350) {
329 s->sr_code[0] = 14;
330 s->sr_code[1] = freq / 10;
331 } else if (freq < 65535) {
332 s->sr_code[0] = 13;
333 s->sr_code[1] = freq;
334 } else if (freq < 1048576) {
335 s->sr_code[0] = 0;
336 s->sr_code[1] = 0;
337 } else {
338 av_log(avctx, AV_LOG_ERROR, "%d Hz not supported\n", freq);
339 return AVERROR(EINVAL);
340 }
341 s->samplerate = freq;
342 }
343
344 /* set compression option defaults based on avctx->compression_level */
345 if (avctx->compression_level < 0)
346 s->options.compression_level = 5;
347 else
348 s->options.compression_level = avctx->compression_level;
349
350 level = s->options.compression_level;
351 if (level > 12) {
352 av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
353 s->options.compression_level);
354 return AVERROR(EINVAL);
355 }
356
357 s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
358
359 if (s->options.lpc_type == FF_LPC_TYPE_DEFAULT)
360 s->options.lpc_type = ((int[]){ FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED,
365
366 if (s->options.min_prediction_order < 0)
367 s->options.min_prediction_order = ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
368 if (s->options.max_prediction_order < 0)
369 s->options.max_prediction_order = ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
370
371 if (s->options.prediction_order_method < 0)
372 s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
377
378 if (s->options.min_partition_order > s->options.max_partition_order) {
379 av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
380 s->options.min_partition_order, s->options.max_partition_order);
381 return AVERROR(EINVAL);
382 }
383 if (s->options.min_partition_order < 0)
384 s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
385 if (s->options.max_partition_order < 0)
386 s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
387
388 if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
389 s->options.min_prediction_order = 0;
390 s->options.max_prediction_order = 0;
391 } else if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
392 if (s->options.min_prediction_order > MAX_FIXED_ORDER) {
393 av_log(avctx, AV_LOG_WARNING,
394 "invalid min prediction order %d, clamped to %d\n",
395 s->options.min_prediction_order, MAX_FIXED_ORDER);
396 s->options.min_prediction_order = MAX_FIXED_ORDER;
397 }
398 if (s->options.max_prediction_order > MAX_FIXED_ORDER) {
399 av_log(avctx, AV_LOG_WARNING,
400 "invalid max prediction order %d, clamped to %d\n",
401 s->options.max_prediction_order, MAX_FIXED_ORDER);
402 s->options.max_prediction_order = MAX_FIXED_ORDER;
403 }
404 } else if (s->options.min_prediction_order < MIN_LPC_ORDER) {
405 av_log(avctx, AV_LOG_WARNING,
406 "invalid min prediction order %d, clamped to %d\n",
407 s->options.min_prediction_order, MIN_LPC_ORDER);
408 s->options.min_prediction_order = MIN_LPC_ORDER;
409 }
410
411 if (s->options.max_prediction_order < s->options.min_prediction_order) {
412 av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
413 s->options.min_prediction_order, s->options.max_prediction_order);
414 return AVERROR(EINVAL);
415 }
416
417 if (avctx->frame_size > 0) {
418 if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
420 av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
421 avctx->frame_size);
422 return AVERROR(EINVAL);
423 }
424 } else {
425 s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
426 }
427 s->max_blocksize = s->avctx->frame_size;
428
429 /* set maximum encoded frame size in verbatim mode */
430 s->max_framesize = flac_get_max_frame_size(s->avctx->frame_size,
431 s->channels,
432 s->avctx->bits_per_raw_sample);
433
434 /* initialize MD5 context */
435 s->md5ctx = av_md5_alloc();
436 if (!s->md5ctx)
437 return AVERROR(ENOMEM);
438 av_md5_init(s->md5ctx);
439
440 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
441 if (!streaminfo)
442 return AVERROR(ENOMEM);
443 write_streaminfo(s, streaminfo);
444 avctx->extradata = streaminfo;
446
447 s->frame_count = 0;
448 s->min_framesize = s->max_framesize;
449
450 if ((channels == 3 &&
452 (channels == 4 &&
455 (channels == 5 &&
458 (channels == 6 &&
462 av_log(avctx, AV_LOG_ERROR, "Channel layout not supported by Flac, "
463 "output stream will have incorrect "
464 "channel layout.\n");
465 } else {
466 av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
467 "will use Flac channel layout for "
468 "%d channels.\n", channels);
469 }
470 }
471
472 ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
473 s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
474
475 ff_bswapdsp_init(&s->bdsp);
476 ff_flacencdsp_init(&s->flac_dsp);
477
479
480 return ret;
481}
482
483
484static void init_frame(FlacEncodeContext *s, int nb_samples)
485{
486 int i, ch;
488
489 frame = &s->frame;
490
491 for (i = 0; i < 16; i++) {
492 if (nb_samples == ff_flac_blocksize_table[i]) {
493 frame->blocksize = ff_flac_blocksize_table[i];
494 frame->bs_code[0] = i;
495 frame->bs_code[1] = 0;
496 break;
497 }
498 }
499 if (i == 16) {
500 frame->blocksize = nb_samples;
501 if (frame->blocksize <= 256) {
502 frame->bs_code[0] = 6;
503 frame->bs_code[1] = frame->blocksize-1;
504 } else {
505 frame->bs_code[0] = 7;
506 frame->bs_code[1] = frame->blocksize-1;
507 }
508 }
509
510 for (ch = 0; ch < s->channels; ch++) {
511 FlacSubframe *sub = &frame->subframes[ch];
512
513 sub->wasted = 0;
514 sub->obits = s->avctx->bits_per_raw_sample;
515
516 if (sub->obits > 16)
518 else
520 }
521
522 frame->verbatim_only = 0;
523}
524
525
526/**
527 * Copy channel-interleaved input samples into separate subframes.
528 */
529static void copy_samples(FlacEncodeContext *s, const void *samples)
530{
531 int i, j, ch;
533
534#define COPY_SAMPLES(bits, shift0) do { \
535 const int ## bits ## _t *samples0 = samples; \
536 const int shift = shift0; \
537 frame = &s->frame; \
538 for (i = 0, j = 0; i < frame->blocksize; i++) \
539 for (ch = 0; ch < s->channels; ch++, j++) \
540 frame->subframes[ch].samples[i] = samples0[j] >> shift; \
541} while (0)
542
543 if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S16)
544 COPY_SAMPLES(16, 0);
545 else
546 COPY_SAMPLES(32, 32 - s->avctx->bits_per_raw_sample);
547}
548
549
550static uint64_t rice_count_exact(const int32_t *res, int n, int k)
551{
552 int i;
553 uint64_t count = 0;
554
555 for (i = 0; i < n; i++) {
556 unsigned v = ((unsigned)(res[i]) << 1) ^ (res[i] >> 31);
557 count += (v >> k) + 1 + k;
558 }
559 return count;
560}
561
562
564 int pred_order)
565{
566 int p, porder, psize;
567 int i, part_end;
568 uint64_t count = 0;
569
570 /* subframe header */
571 count += 8;
572
573 if (sub->wasted)
574 count += sub->wasted;
575
576 /* subframe */
577 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
578 count += sub->obits;
579 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
580 count += s->frame.blocksize * sub->obits;
581 } else {
582 /* warm-up samples */
583 count += pred_order * sub->obits;
584
585 /* LPC coefficients */
586 if (sub->type == FLAC_SUBFRAME_LPC)
587 count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
588
589 /* rice-encoded block */
590 count += 2;
591
592 /* partition order */
593 porder = sub->rc.porder;
594 psize = s->frame.blocksize >> porder;
595 count += 4;
596
597 /* residual */
598 i = pred_order;
599 part_end = psize;
600 for (p = 0; p < 1 << porder; p++) {
601 int k = sub->rc.params[p];
602 count += sub->rc.coding_mode;
603 count += rice_count_exact(&sub->residual[i], part_end - i, k);
604 i = part_end;
605 part_end = FFMIN(s->frame.blocksize, part_end + psize);
606 }
607 }
608
609 return count;
610}
611
612
613#define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
614
615/**
616 * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
617 */
618static int find_optimal_param(uint64_t sum, int n, int max_param)
619{
620 int k;
621 uint64_t sum2;
622
623 if (sum <= n >> 1)
624 return 0;
625 sum2 = sum - (n >> 1);
626 k = av_log2(av_clipl_int32(sum2 / n));
627 return FFMIN(k, max_param);
628}
629
630static int find_optimal_param_exact(uint64_t sums[32][MAX_PARTITIONS], int i, int max_param)
631{
632 int bestk = 0;
633 int64_t bestbits = INT64_MAX;
634 int k;
635
636 for (k = 0; k <= max_param; k++) {
637 int64_t bits = sums[k][i];
638 if (bits < bestbits) {
639 bestbits = bits;
640 bestk = k;
641 }
642 }
643
644 return bestk;
645}
646
647static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder,
648 uint64_t sums[32][MAX_PARTITIONS],
649 int n, int pred_order, int max_param, int exact)
650{
651 int i;
652 int k, cnt, part;
653 uint64_t all_bits;
654
655 part = (1 << porder);
656 all_bits = 4 * part;
657
658 cnt = (n >> porder) - pred_order;
659 for (i = 0; i < part; i++) {
660 if (exact) {
661 k = find_optimal_param_exact(sums, i, max_param);
662 all_bits += sums[k][i];
663 } else {
664 k = find_optimal_param(sums[0][i], cnt, max_param);
665 all_bits += rice_encode_count(sums[0][i], cnt, k);
666 }
667 rc->params[i] = k;
668 cnt = n >> porder;
669 }
670
671 rc->porder = porder;
672
673 return all_bits;
674}
675
676
677static void calc_sum_top(int pmax, int kmax, const uint32_t *data, int n, int pred_order,
678 uint64_t sums[32][MAX_PARTITIONS])
679{
680 int i, k;
681 int parts;
682 const uint32_t *res, *res_end;
683
684 /* sums for highest level */
685 parts = (1 << pmax);
686
687 for (k = 0; k <= kmax; k++) {
688 res = &data[pred_order];
689 res_end = &data[n >> pmax];
690 for (i = 0; i < parts; i++) {
691 if (kmax) {
692 uint64_t sum = (1LL + k) * (res_end - res);
693 while (res < res_end)
694 sum += *(res++) >> k;
695 sums[k][i] = sum;
696 } else {
697 uint64_t sum = 0;
698 while (res < res_end)
699 sum += *(res++);
700 sums[k][i] = sum;
701 }
702 res_end += n >> pmax;
703 }
704 }
705}
706
707static void calc_sum_next(int level, uint64_t sums[32][MAX_PARTITIONS], int kmax)
708{
709 int i, k;
710 int parts = (1 << level);
711 for (i = 0; i < parts; i++) {
712 for (k=0; k<=kmax; k++)
713 sums[k][i] = sums[k][2*i] + sums[k][2*i+1];
714 }
715}
716
717static uint64_t calc_rice_params(RiceContext *rc,
718 uint32_t udata[FLAC_MAX_BLOCKSIZE],
719 uint64_t sums[32][MAX_PARTITIONS],
720 int pmin, int pmax,
721 const int32_t *data, int n, int pred_order, int exact)
722{
723 int i;
724 uint64_t bits[MAX_PARTITION_ORDER+1];
725 int opt_porder;
726 RiceContext tmp_rc;
727 int kmax = (1 << rc->coding_mode) - 2;
728
729 av_assert1(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
730 av_assert1(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
731 av_assert1(pmin <= pmax);
732
733 tmp_rc.coding_mode = rc->coding_mode;
734
735 for (i = pred_order; i < n; i++)
736 udata[i] = ((unsigned)(data[i]) << 1) ^ (data[i] >> 31);
737
738 calc_sum_top(pmax, exact ? kmax : 0, udata, n, pred_order, sums);
739
740 opt_porder = pmin;
741 bits[pmin] = UINT32_MAX;
742 for (i = pmax; ; ) {
743 bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums, n, pred_order, kmax, exact);
744 if (bits[i] < bits[opt_porder] || pmax == pmin) {
745 opt_porder = i;
746 *rc = tmp_rc;
747 }
748 if (i == pmin)
749 break;
750 calc_sum_next(--i, sums, exact ? kmax : 0);
751 }
752
753 return bits[opt_porder];
754}
755
756
757static int get_max_p_order(int max_porder, int n, int order)
758{
759 int porder = FFMIN(max_porder, av_log2(n^(n-1)));
760 if (order > 0)
761 porder = FFMIN(porder, av_log2(n/order));
762 return porder;
763}
764
765
767 FlacSubframe *sub, int pred_order)
768{
769 int pmin = get_max_p_order(s->options.min_partition_order,
770 s->frame.blocksize, pred_order);
771 int pmax = get_max_p_order(s->options.max_partition_order,
772 s->frame.blocksize, pred_order);
773
774 uint64_t bits = 8 + pred_order * sub->obits + 2 + sub->rc.coding_mode;
775 if (sub->type == FLAC_SUBFRAME_LPC)
776 bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
777 bits += calc_rice_params(&sub->rc, sub->rc_udata, sub->rc_sums, pmin, pmax, sub->residual,
778 s->frame.blocksize, pred_order, s->options.exact_rice_parameters);
779 return bits;
780}
781
782
783static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
784 int order)
785{
786 int i;
787
788 for (i = 0; i < order; i++)
789 res[i] = smp[i];
790
791 if (order == 0) {
792 for (i = order; i < n; i++)
793 res[i] = smp[i];
794 } else if (order == 1) {
795 for (i = order; i < n; i++)
796 res[i] = smp[i] - smp[i-1];
797 } else if (order == 2) {
798 int a = smp[order-1] - smp[order-2];
799 for (i = order; i < n; i += 2) {
800 int b = smp[i ] - smp[i-1];
801 res[i] = b - a;
802 a = smp[i+1] - smp[i ];
803 res[i+1] = a - b;
804 }
805 } else if (order == 3) {
806 int a = smp[order-1] - smp[order-2];
807 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
808 for (i = order; i < n; i += 2) {
809 int b = smp[i ] - smp[i-1];
810 int d = b - a;
811 res[i] = d - c;
812 a = smp[i+1] - smp[i ];
813 c = a - b;
814 res[i+1] = c - d;
815 }
816 } else {
817 int a = smp[order-1] - smp[order-2];
818 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
819 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
820 for (i = order; i < n; i += 2) {
821 int b = smp[i ] - smp[i-1];
822 int d = b - a;
823 int f = d - c;
824 res[i ] = f - e;
825 a = smp[i+1] - smp[i ];
826 c = a - b;
827 e = c - d;
828 res[i+1] = e - f;
829 }
830 }
831}
832
833
834/* These four functions check for every residual whether it can be
835 * contained in <INT32_MIN,INT32_MAX]. In case it doesn't, the
836 * function that called this function has to try something else.
837 * Each function is duplicated, once for int32_t input, once for
838 * int64_t input */
839#define ENCODE_RESIDUAL_FIXED_WITH_RESIDUAL_LIMIT() \
840{ \
841 for (int i = 0; i < order; i++) \
842 res[i] = smp[i]; \
843 if (order == 0) { \
844 for (int i = order; i < n; i++) { \
845 if (smp[i] == INT32_MIN) \
846 return 1; \
847 res[i] = smp[i]; \
848 } \
849 } else if (order == 1) { \
850 for (int i = order; i < n; i++) { \
851 int64_t res64 = (int64_t)smp[i] - smp[i-1]; \
852 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
853 return 1; \
854 res[i] = res64; \
855 } \
856 } else if (order == 2) { \
857 for (int i = order; i < n; i++) { \
858 int64_t res64 = (int64_t)smp[i] - 2*(int64_t)smp[i-1] + smp[i-2]; \
859 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
860 return 1; \
861 res[i] = res64; \
862 } \
863 } else if (order == 3) { \
864 for (int i = order; i < n; i++) { \
865 int64_t res64 = (int64_t)smp[i] - 3*(int64_t)smp[i-1] + 3*(int64_t)smp[i-2] - smp[i-3]; \
866 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
867 return 1; \
868 res[i] = res64; \
869 } \
870 } else { \
871 for (int i = order; i < n; i++) { \
872 int64_t res64 = (int64_t)smp[i] - 4*(int64_t)smp[i-1] + 6*(int64_t)smp[i-2] - 4*(int64_t)smp[i-3] + smp[i-4]; \
873 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
874 return 1; \
875 res[i] = res64; \
876 } \
877 } \
878 return 0; \
879}
880
882 int n, int order)
883{
885}
886
887
889 int n, int order)
890{
892}
893
894#define LPC_ENCODE_WITH_RESIDUAL_LIMIT() \
895{ \
896 for (int i = 0; i < order; i++) \
897 res[i] = smp[i]; \
898 for (int i = order; i < len; i++) { \
899 int64_t p = 0, tmp; \
900 for (int j = 0; j < order; j++) \
901 p += (int64_t)coefs[j]*smp[(i-1)-j]; \
902 p >>= shift; \
903 tmp = smp[i] - p; \
904 if (tmp <= INT32_MIN || tmp > INT32_MAX) \
905 return 1; \
906 res[i] = tmp; \
907 } \
908 return 0; \
909}
910
911static int lpc_encode_with_residual_limit(int32_t *res, const int32_t *smp, int len,
912 int order, int32_t *coefs, int shift)
913{
915}
916
918 int order, int32_t *coefs, int shift)
919{
921}
922
924 int32_t *res, const int32_t *smp,
925 const int64_t *smp_33bps, int len,
926 int order, int32_t *coefs, int shift)
927{
928 uint64_t max_residual_value = 0;
929 int64_t max_sample_value = ((int64_t)(1) << (bps-1));
930 /* This calculates the max size of any residual with the current
931 * predictor, so we know whether we need to check the residual */
932 for (int i = 0; i < order; i++)
933 max_residual_value += FFABS(max_sample_value * coefs[i]);
934 max_residual_value >>= shift;
935 max_residual_value += max_sample_value;
936 if (bps > 32) {
937 if (lpc_encode_with_residual_limit_33bps(res, smp_33bps, len, order, coefs, shift))
938 return 1;
939 } else if (max_residual_value > INT32_MAX) {
940 if (lpc_encode_with_residual_limit(res, smp, len, order, coefs, shift))
941 return 1;
942 } else if (bps + s->options.lpc_coeff_precision + av_log2(order) <= 32) {
943 s->flac_dsp.lpc16_encode(res, smp, len, order, coefs, shift);
944 } else {
945 s->flac_dsp.lpc32_encode(res, smp, len, order, coefs, shift);
946 }
947 return 0;
948}
949
950#define DEFAULT_TO_VERBATIM() \
951{ \
952 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM; \
953 if (sub->obits <= 32) \
954 memcpy(res, smp, n * sizeof(int32_t)); \
955 return subframe_count_exact(s, sub, 0); \
956}
957
959{
960 int i, n;
961 int min_order, max_order, opt_order, omethod;
963 FlacSubframe *sub;
965 int shift[MAX_LPC_ORDER];
966 int32_t *res, *smp;
967 int64_t *smp_33bps;
968
969 frame = &s->frame;
970 sub = &frame->subframes[ch];
971 res = sub->residual;
972 smp = sub->samples;
973 smp_33bps = frame->samples_33bps;
974 n = frame->blocksize;
975
976 /* CONSTANT */
977 if (sub->obits > 32) {
978 for (i = 1; i < n; i++)
979 if(smp_33bps[i] != smp_33bps[0])
980 break;
981 if (i == n) {
983 return subframe_count_exact(s, sub, 0);
984 }
985 } else {
986 for (i = 1; i < n; i++)
987 if(smp[i] != smp[0])
988 break;
989 if (i == n) {
991 res[0] = smp[0];
992 return subframe_count_exact(s, sub, 0);
993 }
994 }
995
996 /* VERBATIM */
997 if (frame->verbatim_only || n < 5) {
999 }
1000
1001 min_order = s->options.min_prediction_order;
1002 max_order = s->options.max_prediction_order;
1003 omethod = s->options.prediction_order_method;
1004
1005 /* FIXED */
1007 if (s->options.lpc_type == FF_LPC_TYPE_NONE ||
1008 s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
1009 uint64_t bits[MAX_FIXED_ORDER+1];
1010 if (max_order > MAX_FIXED_ORDER)
1011 max_order = MAX_FIXED_ORDER;
1012 opt_order = 0;
1013 bits[0] = UINT32_MAX;
1014 for (i = min_order; i <= max_order; i++) {
1015 if (sub->obits == 33) {
1016 if (encode_residual_fixed_with_residual_limit_33bps(res, smp_33bps, n, i))
1017 continue;
1018 } else if (sub->obits + i >= 32) {
1020 continue;
1021 } else
1022 encode_residual_fixed(res, smp, n, i);
1024 if (bits[i] < bits[opt_order])
1025 opt_order = i;
1026 }
1027 if (opt_order == 0 && bits[0] == UINT32_MAX) {
1028 /* No predictor found with residuals within <INT32_MIN,INT32_MAX],
1029 * so encode a verbatim subframe instead */
1031 }
1032 sub->order = opt_order;
1033 sub->type_code = sub->type | sub->order;
1034 if (sub->order != max_order) {
1035 if (sub->obits == 33)
1037 else if (sub->obits + i >= 32)
1039 else
1040 encode_residual_fixed(res, smp, n, sub->order);
1041 find_subframe_rice_params(s, sub, sub->order);
1042 }
1043 return subframe_count_exact(s, sub, sub->order);
1044 }
1045
1046 /* LPC */
1047 sub->type = FLAC_SUBFRAME_LPC;
1048 if (sub->obits == 33)
1049 /* As ff_lpc_calc_coefs is shared with other codecs and the LSB
1050 * probably isn't predictable anyway, throw away LSB for analysis
1051 * so it fits 32 bit int and existing function can be used
1052 * unmodified */
1053 for (i = 0; i < n; i++)
1054 smp[i] = smp_33bps[i] >> 1;
1055
1056 opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
1057 s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
1058 s->options.lpc_passes, omethod,
1060
1061 if (omethod == ORDER_METHOD_2LEVEL ||
1062 omethod == ORDER_METHOD_4LEVEL ||
1063 omethod == ORDER_METHOD_8LEVEL) {
1064 int levels = 1 << omethod;
1065 uint64_t bits[1 << ORDER_METHOD_8LEVEL];
1066 int order = -1;
1067 int opt_index = levels-1;
1068 opt_order = max_order-1;
1069 bits[opt_index] = UINT32_MAX;
1070 for (i = levels-1; i >= 0; i--) {
1071 int last_order = order;
1072 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
1073 order = av_clip(order, min_order - 1, max_order - 1);
1074 if (order == last_order)
1075 continue;
1076 if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, order+1, coefs[order], shift[order]))
1077 continue;
1078 bits[i] = find_subframe_rice_params(s, sub, order+1);
1079 if (bits[i] < bits[opt_index]) {
1080 opt_index = i;
1081 opt_order = order;
1082 }
1083 }
1084 opt_order++;
1085 } else if (omethod == ORDER_METHOD_SEARCH) {
1086 // brute-force optimal order search
1087 uint64_t bits[MAX_LPC_ORDER];
1088 opt_order = 0;
1089 bits[0] = UINT32_MAX;
1090 for (i = min_order-1; i < max_order; i++) {
1091 if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, i+1, coefs[i], shift[i]))
1092 continue;
1093 bits[i] = find_subframe_rice_params(s, sub, i+1);
1094 if (bits[i] < bits[opt_order])
1095 opt_order = i;
1096 }
1097 opt_order++;
1098 } else if (omethod == ORDER_METHOD_LOG) {
1099 uint64_t bits[MAX_LPC_ORDER];
1100 int step;
1101
1102 opt_order = min_order - 1 + (max_order-min_order)/3;
1103 memset(bits, -1, sizeof(bits));
1104
1105 for (step = 16; step; step >>= 1) {
1106 int last = opt_order;
1107 for (i = last-step; i <= last+step; i += step) {
1108 if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
1109 continue;
1110 if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, i+1, coefs[i], shift[i]))
1111 continue;
1112 bits[i] = find_subframe_rice_params(s, sub, i+1);
1113 if (bits[i] < bits[opt_order])
1114 opt_order = i;
1115 }
1116 }
1117 opt_order++;
1118 }
1119
1120 if (s->options.multi_dim_quant) {
1121 int allsteps = 1;
1122 int i, step, improved;
1123 int64_t best_score = INT64_MAX;
1124 int32_t qmax;
1125
1126 qmax = (1 << (s->options.lpc_coeff_precision - 1)) - 1;
1127
1128 for (i=0; i<opt_order; i++)
1129 allsteps *= 3;
1130
1131 do {
1132 improved = 0;
1133 for (step = 0; step < allsteps; step++) {
1134 int tmp = step;
1135 int32_t lpc_try[MAX_LPC_ORDER];
1136 int64_t score = 0;
1137 int diffsum = 0;
1138
1139 for (i=0; i<opt_order; i++) {
1140 int diff = ((tmp + 1) % 3) - 1;
1141 lpc_try[i] = av_clip(coefs[opt_order - 1][i] + diff, -qmax, qmax);
1142 tmp /= 3;
1143 diffsum += !!diff;
1144 }
1145 if (diffsum >8)
1146 continue;
1147
1148 if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, opt_order, lpc_try, shift[opt_order-1]))
1149 continue;
1150 score = find_subframe_rice_params(s, sub, opt_order);
1151 if (score < best_score) {
1152 best_score = score;
1153 memcpy(coefs[opt_order-1], lpc_try, sizeof(*coefs));
1154 improved=1;
1155 }
1156 }
1157 } while(improved);
1158 }
1159
1160 sub->order = opt_order;
1161 sub->type_code = sub->type | (sub->order-1);
1162 sub->shift = shift[sub->order-1];
1163 for (i = 0; i < sub->order; i++)
1164 sub->coefs[i] = coefs[sub->order-1][i];
1165
1166 if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, sub->order, sub->coefs, sub->shift)) {
1167 /* No predictor found with residuals within <INT32_MIN,INT32_MAX],
1168 * so encode a verbatim subframe instead */
1170 }
1171
1172 find_subframe_rice_params(s, sub, sub->order);
1173
1174 return subframe_count_exact(s, sub, sub->order);
1175}
1176
1177
1179{
1180 av_unused uint8_t tmp;
1181 int count;
1182
1183 /*
1184 <14> Sync code
1185 <1> Reserved
1186 <1> Blocking strategy
1187 <4> Block size in inter-channel samples
1188 <4> Sample rate
1189 <4> Channel assignment
1190 <3> Sample size in bits
1191 <1> Reserved
1192 */
1193 count = 32;
1194
1195 /* coded frame number */
1196 PUT_UTF8(s->frame_count, tmp, count += 8;)
1197
1198 /* explicit block size */
1199 if (s->frame.bs_code[0] == 6)
1200 count += 8;
1201 else if (s->frame.bs_code[0] == 7)
1202 count += 16;
1203
1204 /* explicit sample rate */
1205 count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12) * 2) * 8;
1206
1207 /* frame header CRC-8 */
1208 count += 8;
1209
1210 return count;
1211}
1212
1213
1215{
1216 int ch;
1217 uint64_t count;
1218
1219 count = count_frame_header(s);
1220
1221 for (ch = 0; ch < s->channels; ch++)
1222 count += encode_residual_ch(s, ch);
1223
1224 count += (8 - (count & 7)) & 7; // byte alignment
1225 count += 16; // CRC-16
1226
1227 count >>= 3;
1228 if (count > INT_MAX)
1229 return AVERROR_BUG;
1230 return count;
1231}
1232
1233
1235{
1236 int ch, i, wasted_bits;
1237
1238 for (ch = 0; ch < s->channels; ch++) {
1239 FlacSubframe *sub = &s->frame.subframes[ch];
1240
1241 if (sub->obits > 32) {
1242 int64_t v = 0;
1243 for (i = 0; i < s->frame.blocksize; i++) {
1244 v |= s->frame.samples_33bps[i];
1245 if (v & 1)
1246 break;
1247 }
1248
1249 if (!v || (v & 1))
1250 return;
1251
1252 v = ff_ctzll(v);
1253
1254 /* If any wasted bits are found, samples are moved
1255 * from frame.samples_33bps to frame.subframes[ch] */
1256 for (i = 0; i < s->frame.blocksize; i++)
1257 sub->samples[i] = s->frame.samples_33bps[i] >> v;
1258 wasted_bits = v;
1259 } else {
1260 int32_t v = 0;
1261 for (i = 0; i < s->frame.blocksize; i++) {
1262 v |= sub->samples[i];
1263 if (v & 1)
1264 break;
1265 }
1266
1267 if (!v || (v & 1))
1268 return;
1269
1270 v = ff_ctz(v);
1271
1272 for (i = 0; i < s->frame.blocksize; i++)
1273 sub->samples[i] >>= v;
1274 wasted_bits = v;
1275 }
1276
1277 sub->wasted = wasted_bits;
1278 sub->obits -= wasted_bits;
1279
1280 /* for 24-bit, check if removing wasted bits makes the range better
1281 * suited for using RICE instead of RICE2 for entropy coding */
1282 if (sub->obits <= 17)
1284 }
1285}
1286
1287
1288static int estimate_stereo_mode(const int32_t *left_ch, const int32_t *right_ch, int n,
1289 int max_rice_param, int bps)
1290{
1291 int best;
1292 uint64_t sum[4];
1293 uint64_t score[4];
1294 int k;
1295
1296 /* calculate sum of 2nd order residual for each channel */
1297 sum[0] = sum[1] = sum[2] = sum[3] = 0;
1298 if(bps < 30) {
1299 int32_t lt, rt;
1300 for (int i = 2; i < n; i++) {
1301 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
1302 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
1303 sum[2] += FFABS((lt + rt) >> 1);
1304 sum[3] += FFABS(lt - rt);
1305 sum[0] += FFABS(lt);
1306 sum[1] += FFABS(rt);
1307 }
1308 } else {
1309 int64_t lt, rt;
1310 for (int i = 2; i < n; i++) {
1311 lt = (int64_t)left_ch[i] - 2*(int64_t)left_ch[i-1] + left_ch[i-2];
1312 rt = (int64_t)right_ch[i] - 2*(int64_t)right_ch[i-1] + right_ch[i-2];
1313 sum[2] += FFABS((lt + rt) >> 1);
1314 sum[3] += FFABS(lt - rt);
1315 sum[0] += FFABS(lt);
1316 sum[1] += FFABS(rt);
1317 }
1318 }
1319 /* estimate bit counts */
1320 for (int i = 0; i < 4; i++) {
1321 k = find_optimal_param(2 * sum[i], n, max_rice_param);
1322 sum[i] = rice_encode_count( 2 * sum[i], n, k);
1323 }
1324
1325 /* calculate score for each mode */
1326 score[0] = sum[0] + sum[1];
1327 score[1] = sum[0] + sum[3];
1328 score[2] = sum[1] + sum[3];
1329 score[3] = sum[2] + sum[3];
1330
1331 /* return mode with lowest score */
1332 best = 0;
1333 for (int i = 1; i < 4; i++)
1334 if (score[i] < score[best])
1335 best = i;
1336
1337 return best;
1338}
1339
1340
1341/**
1342 * Perform stereo channel decorrelation.
1343 */
1345{
1347 int32_t *left, *right;
1348 int64_t *side_33bps;
1349 int n;
1350
1351 frame = &s->frame;
1352 n = frame->blocksize;
1353 left = frame->subframes[0].samples;
1354 right = frame->subframes[1].samples;
1355 side_33bps = frame->samples_33bps;
1356
1357 if (s->channels != 2) {
1358 frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
1359 return;
1360 }
1361
1362 if (s->options.ch_mode < 0) {
1363 int max_rice_param = (1 << frame->subframes[0].rc.coding_mode) - 2;
1364 frame->ch_mode = estimate_stereo_mode(left, right, n, max_rice_param, s->avctx->bits_per_raw_sample);
1365 } else
1366 frame->ch_mode = s->options.ch_mode;
1367
1368 /* perform decorrelation and adjust bits-per-sample */
1369 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1370 return;
1371 if(s->avctx->bits_per_raw_sample == 32) {
1372 if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1373 int64_t tmp;
1374 for (int i = 0; i < n; i++) {
1375 tmp = left[i];
1376 left[i] = (tmp + right[i]) >> 1;
1377 side_33bps[i] = tmp - right[i];
1378 }
1379 frame->subframes[1].obits++;
1380 } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1381 for (int i = 0; i < n; i++)
1382 side_33bps[i] = (int64_t)left[i] - right[i];
1383 frame->subframes[1].obits++;
1384 } else {
1385 for (int i = 0; i < n; i++)
1386 side_33bps[i] = (int64_t)left[i] - right[i];
1387 frame->subframes[0].obits++;
1388 }
1389 } else {
1390 if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1391 int32_t tmp;
1392 for (int i = 0; i < n; i++) {
1393 tmp = left[i];
1394 left[i] = (tmp + right[i]) >> 1;
1395 right[i] = tmp - right[i];
1396 }
1397 frame->subframes[1].obits++;
1398 } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1399 for (int i = 0; i < n; i++)
1400 right[i] = left[i] - right[i];
1401 frame->subframes[1].obits++;
1402 } else {
1403 for (int i = 0; i < n; i++)
1404 left[i] -= right[i];
1405 frame->subframes[0].obits++;
1406 }
1407 }
1408}
1409
1410
1411static void write_utf8(PutBitContext *pb, uint32_t val)
1412{
1413 uint8_t tmp;
1414 PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1415}
1416
1417
1419{
1421 int crc;
1422
1423 frame = &s->frame;
1424
1425 put_bits(&s->pb, 16, 0xFFF8);
1426 put_bits(&s->pb, 4, frame->bs_code[0]);
1427 put_bits(&s->pb, 4, s->sr_code[0]);
1428
1429 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1430 put_bits(&s->pb, 4, s->channels-1);
1431 else
1432 put_bits(&s->pb, 4, frame->ch_mode + FLAC_MAX_CHANNELS - 1);
1433
1434 put_bits(&s->pb, 3, s->bps_code);
1435 put_bits(&s->pb, 1, 0);
1436 write_utf8(&s->pb, s->frame_count);
1437
1438 if (frame->bs_code[0] == 6)
1439 put_bits(&s->pb, 8, frame->bs_code[1]);
1440 else if (frame->bs_code[0] == 7)
1441 put_bits(&s->pb, 16, frame->bs_code[1]);
1442
1443 if (s->sr_code[0] == 12)
1444 put_bits(&s->pb, 8, s->sr_code[1]);
1445 else if (s->sr_code[0] > 12)
1446 put_bits(&s->pb, 16, s->sr_code[1]);
1447
1448 flush_put_bits(&s->pb);
1449 crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
1450 put_bytes_output(&s->pb));
1451 put_bits(&s->pb, 8, crc);
1452}
1453
1454
1455static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k)
1456{
1457 unsigned v, e;
1458
1459 v = ((unsigned)(i) << 1) ^ (i >> 31);
1460
1461 e = (v >> k) + 1;
1462 while (e > 31) {
1463 put_bits(pb, 31, 0);
1464 e -= 31;
1465 }
1466 put_bits(pb, e, 1);
1467 if (k) {
1468 unsigned mask = UINT32_MAX >> (32-k);
1469 put_bits(pb, k, v & mask);
1470 }
1471}
1472
1473
1475{
1476 int ch;
1477
1478 for (ch = 0; ch < s->channels; ch++) {
1479 FlacSubframe *sub = &s->frame.subframes[ch];
1480 int p, porder, psize;
1481 int32_t *part_end;
1482 int32_t *res = sub->residual;
1483 int32_t *frame_end = &sub->residual[s->frame.blocksize];
1484
1485 /* subframe header */
1486 put_bits(&s->pb, 1, 0);
1487 put_bits(&s->pb, 6, sub->type_code);
1488 put_bits(&s->pb, 1, !!sub->wasted);
1489 if (sub->wasted)
1490 put_bits(&s->pb, sub->wasted, 1);
1491
1492 /* subframe */
1493 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
1494 if(sub->obits == 33)
1495 put_sbits63(&s->pb, 33, s->frame.samples_33bps[0]);
1496 else if(sub->obits == 32)
1497 put_bits32(&s->pb, res[0]);
1498 else
1499 put_sbits(&s->pb, sub->obits, res[0]);
1500 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
1501 if (sub->obits == 33) {
1502 int64_t *res64 = s->frame.samples_33bps;
1503 int64_t *frame_end64 = &s->frame.samples_33bps[s->frame.blocksize];
1504 while (res64 < frame_end64)
1505 put_sbits63(&s->pb, 33, (*res64++));
1506 } else if (sub->obits == 32) {
1507 while (res < frame_end)
1508 put_bits32(&s->pb, *res++);
1509 } else {
1510 while (res < frame_end)
1511 put_sbits(&s->pb, sub->obits, *res++);
1512 }
1513 } else {
1514 /* warm-up samples */
1515 if (sub->obits == 33) {
1516 for (int i = 0; i < sub->order; i++)
1517 put_sbits63(&s->pb, 33, s->frame.samples_33bps[i]);
1518 res += sub->order;
1519 } else if (sub->obits == 32) {
1520 for (int i = 0; i < sub->order; i++)
1521 put_bits32(&s->pb, *res++);
1522 } else {
1523 for (int i = 0; i < sub->order; i++)
1524 put_sbits(&s->pb, sub->obits, *res++);
1525 }
1526
1527 /* LPC coefficients */
1528 if (sub->type == FLAC_SUBFRAME_LPC) {
1529 int cbits = s->options.lpc_coeff_precision;
1530 put_bits( &s->pb, 4, cbits-1);
1531 put_sbits(&s->pb, 5, sub->shift);
1532 for (int i = 0; i < sub->order; i++)
1533 put_sbits(&s->pb, cbits, sub->coefs[i]);
1534 }
1535
1536 /* rice-encoded block */
1537 put_bits(&s->pb, 2, sub->rc.coding_mode - 4);
1538
1539 /* partition order */
1540 porder = sub->rc.porder;
1541 psize = s->frame.blocksize >> porder;
1542 put_bits(&s->pb, 4, porder);
1543
1544 /* residual */
1545 part_end = &sub->residual[psize];
1546 for (p = 0; p < 1 << porder; p++) {
1547 int k = sub->rc.params[p];
1548 put_bits(&s->pb, sub->rc.coding_mode, k);
1549 while (res < part_end)
1550 set_sr_golomb_flac(&s->pb, *res++, k);
1551 part_end = FFMIN(frame_end, part_end + psize);
1552 }
1553 }
1554 }
1555}
1556
1557
1559{
1560 int crc;
1561 flush_put_bits(&s->pb);
1563 put_bytes_output(&s->pb)));
1564 put_bits(&s->pb, 16, crc);
1565 flush_put_bits(&s->pb);
1566}
1567
1568
1570{
1571 init_put_bits(&s->pb, avpkt->data, avpkt->size);
1575 return put_bytes_output(&s->pb);
1576}
1577
1578
1579static int update_md5_sum(FlacEncodeContext *s, const void *samples)
1580{
1581 const uint8_t *buf;
1582 int buf_size = s->frame.blocksize * s->channels *
1583 ((s->avctx->bits_per_raw_sample + 7) / 8);
1584
1585 if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
1586 av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size);
1587 if (!s->md5_buffer)
1588 return AVERROR(ENOMEM);
1589 }
1590
1591 if (s->avctx->bits_per_raw_sample <= 16) {
1592 buf = (const uint8_t *)samples;
1593#if HAVE_BIGENDIAN
1594 s->bdsp.bswap16_buf((uint16_t *) s->md5_buffer,
1595 (const uint16_t *) samples, buf_size / 2);
1596 buf = s->md5_buffer;
1597#endif
1598 } else if (s->avctx->bits_per_raw_sample <= 24) {
1599 int i;
1600 const int32_t *samples0 = samples;
1601 uint8_t *tmp = s->md5_buffer;
1602
1603 for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1604 int32_t v = samples0[i] >> 8;
1605 AV_WL24(tmp + 3*i, v);
1606 }
1607 buf = s->md5_buffer;
1608 } else {
1609 /* s->avctx->bits_per_raw_sample <= 32 */
1610 int i;
1611 const int32_t *samples0 = samples;
1612 uint8_t *tmp = s->md5_buffer;
1613
1614 for (i = 0; i < s->frame.blocksize * s->channels; i++)
1615 AV_WL32(tmp + 4*i, samples0[i]);
1616 buf = s->md5_buffer;
1617 }
1618 av_md5_update(s->md5ctx, buf, buf_size);
1619
1620 return 0;
1621}
1622
1623
1625 const AVFrame *frame, int *got_packet_ptr)
1626{
1628 int frame_bytes, out_bytes, ret;
1629
1630 s = avctx->priv_data;
1631
1632 /* when the last block is reached, update the header in extradata */
1633 if (!frame) {
1634 s->max_framesize = s->max_encoded_framesize;
1635 av_md5_final(s->md5ctx, s->md5sum);
1636 write_streaminfo(s, avctx->extradata);
1637
1638 if (!s->flushed) {
1639 uint8_t *side_data = av_packet_new_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
1640 avctx->extradata_size);
1641 if (!side_data)
1642 return AVERROR(ENOMEM);
1643 memcpy(side_data, avctx->extradata, avctx->extradata_size);
1644
1645 avpkt->pts = s->next_pts;
1646
1647 *got_packet_ptr = 1;
1648 s->flushed = 1;
1649 }
1650
1651 return 0;
1652 }
1653
1654 /* change max_framesize for small final frame */
1655 if (frame->nb_samples < s->frame.blocksize) {
1656 s->max_framesize = flac_get_max_frame_size(frame->nb_samples,
1657 s->channels,
1658 avctx->bits_per_raw_sample);
1659 }
1660
1661 init_frame(s, frame->nb_samples);
1662
1663 copy_samples(s, frame->data[0]);
1664
1666
1668
1669 frame_bytes = encode_frame(s);
1670
1671 /* Fall back on verbatim mode if the compressed frame is larger than it
1672 would be if encoded uncompressed. */
1673 if (frame_bytes < 0 || frame_bytes > s->max_framesize) {
1674 s->frame.verbatim_only = 1;
1675 frame_bytes = encode_frame(s);
1676 if (frame_bytes < 0) {
1677 av_log(avctx, AV_LOG_ERROR, "Bad frame count\n");
1678 return frame_bytes;
1679 }
1680 }
1681
1682 if ((ret = ff_get_encode_buffer(avctx, avpkt, frame_bytes, 0)) < 0)
1683 return ret;
1684
1685 out_bytes = write_frame(s, avpkt);
1686
1687 s->frame_count++;
1688 s->sample_count += frame->nb_samples;
1689 if ((ret = update_md5_sum(s, frame->data[0])) < 0) {
1690 av_log(avctx, AV_LOG_ERROR, "Error updating MD5 checksum\n");
1691 return ret;
1692 }
1693 if (out_bytes > s->max_encoded_framesize)
1694 s->max_encoded_framesize = out_bytes;
1695 if (out_bytes < s->min_framesize)
1696 s->min_framesize = out_bytes;
1697
1698 s->next_pts = frame->pts + ff_samples_to_time_base(avctx, frame->nb_samples);
1699
1700 av_shrink_packet(avpkt, out_bytes);
1701
1702 *got_packet_ptr = 1;
1703 return 0;
1704}
1705
1706
1708{
1709 FlacEncodeContext *s = avctx->priv_data;
1710
1711 av_freep(&s->md5ctx);
1712 av_freep(&s->md5_buffer);
1713 ff_lpc_end(&s->lpc_ctx);
1714 return 0;
1715}
1716
1717#define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1718static const AVOption options[] = {
1719{ "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
1720{ "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, .unit = "lpc_type" },
1721{ "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" },
1722{ "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" },
1723{ "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" },
1724{ "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" },
1725{ "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes), AV_OPT_TYPE_INT, {.i64 = 2 }, 1, INT_MAX, FLAGS },
1726{ "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1727{ "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1728{ "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, .unit = "predm" },
1729{ "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1730{ "2level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1731{ "4level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1732{ "8level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1733{ "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1734{ "log", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1735{ "ch_mode", "Stereo decorrelation mode", offsetof(FlacEncodeContext, options.ch_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FLAC_CHMODE_MID_SIDE, FLAGS, .unit = "ch_mode" },
1736{ "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" },
1737{ "indep", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" },
1738{ "left_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" },
1739{ "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" },
1740{ "mid_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" },
1741{ "exact_rice_parameters", "Calculate rice parameters exactly", offsetof(FlacEncodeContext, options.exact_rice_parameters), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1742{ "multi_dim_quant", "Multi-dimensional quantization", offsetof(FlacEncodeContext, options.multi_dim_quant), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1743{ "min_prediction_order", NULL, offsetof(FlacEncodeContext, options.min_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS },
1744{ "max_prediction_order", NULL, offsetof(FlacEncodeContext, options.max_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS },
1745
1746{ NULL },
1747};
1748
1750 .class_name = "FLAC encoder",
1751 .item_name = av_default_item_name,
1752 .option = options,
1753 .version = LIBAVUTIL_VERSION_INT,
1754};
1755
1757 .p.name = "flac",
1758 CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)"),
1759 .p.type = AVMEDIA_TYPE_AUDIO,
1760 .p.id = AV_CODEC_ID_FLAC,
1761 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1764 .priv_data_size = sizeof(FlacEncodeContext),
1767 .close = flac_encode_close,
1769 .p.priv_class = &flac_encoder_class,
1771};
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_flac_encoder
Definition flacenc.c:1756
channels
Definition aptx.h:31
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:596
#define FF_CODEC_CAP_EOF_FLUSH
The encoder has AV_CODEC_CAP_DELAY set, but does not actually have delay - it only wants to be flushe...
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
#define av_clip
Definition common.h:100
#define av_clipl_int32
Definition common.h:118
#define PUT_UTF8(val, tmp, PUT_BYTE)
Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
Definition common.h:530
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition defs.h:62
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
static av_always_inline int64_t ff_samples_to_time_base(const AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition encode.h:96
static const uint8_t bits[8]
Definition fastaudio.c:100
int ret
Definition ffplay.c:391
FLAC (Free Lossless Audio Codec) common stuff.
#define FLAC_MAX_BLOCKSIZE
Definition flac.h:35
@ FLAC_CHMODE_RIGHT_SIDE
Definition flac.h:41
@ FLAC_CHMODE_MID_SIDE
Definition flac.h:42
@ FLAC_CHMODE_LEFT_SIDE
Definition flac.h:40
@ FLAC_CHMODE_INDEPENDENT
Definition flac.h:39
#define FLAC_MIN_BLOCKSIZE
Definition flac.h:34
#define FLAC_MAX_CHANNELS
Definition flac.h:33
#define FLAC_STREAMINFO_SIZE
Definition flac.h:32
const int ff_flac_sample_rate_table[16]
Definition flacdata.c:24
const int32_t ff_flac_blocksize_table[16]
Definition flacdata.c:30
av_cold void ff_flacencdsp_init(FLACEncDSPContext *c)
Definition flacencdsp.c:32
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition codec.h:84
@ AV_CODEC_ID_FLAC
Definition codec_id.h:470
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition packet.c:113
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_5POINT0
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
#define AV_CHANNEL_LAYOUT_2_2
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_QUAD
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_8_ATM
Definition crc.h:49
@ AV_CRC_16_ANSI
Definition crc.h:50
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
#define ff_ctzll
Definition intmath.h:125
#define ff_ctz
Definition intmath.h:105
void av_md5_init(AVMD5 *ctx)
Initialize MD5 hashing.
Definition md5.c:143
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
Definition md5.c:188
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition md5.c:50
void av_md5_update(AVMD5 *ctx, const uint8_t *src, size_t len)
Update hash value.
Definition md5.c:153
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:661
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition samplefmt.h:59
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int a
#define b
Definition input.c:43
#define av_log2
Definition intmath.h:84
#define AV_WL32(p, v)
#define AV_WL24(p, d)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
static int shift(int a, int b)
Definition bonk.c:261
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition bswapdsp.c:37
static void write_subframes(FlacEncodeContext *s)
Definition flacenc.c:1474
CodingMode
Definition flacenc.c:52
@ CODING_MODE_RICE2
Definition flacenc.c:54
@ CODING_MODE_RICE
Definition flacenc.c:53
#define MAX_PARTITION_ORDER
Definition flacenc.c:46
#define FLAC_SUBFRAME_CONSTANT
Definition flacenc.c:40
static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
Write streaminfo metadata block to byte array.
Definition flacenc.c:138
static void init_frame(FlacEncodeContext *s, int nb_samples)
Definition flacenc.c:484
static int encode_residual_ch(FlacEncodeContext *s, int ch)
Definition flacenc.c:958
static void write_frame_footer(FlacEncodeContext *s)
Definition flacenc.c:1558
static av_cold int flac_encode_close(AVCodecContext *avctx)
Definition flacenc.c:1707
#define FLAC_SUBFRAME_FIXED
Definition flacenc.c:42
static av_cold void dprint_compression_options(FlacEncodeContext *s)
Definition flacenc.c:212
static void copy_samples(FlacEncodeContext *s, const void *samples)
Copy channel-interleaved input samples into separate subframes.
Definition flacenc.c:529
#define MAX_LPC_PRECISION
Definition flacenc.c:48
static int lpc_encode_with_residual_limit(int32_t *res, const int32_t *smp, int len, int order, int32_t *coefs, int shift)
Definition flacenc.c:911
static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder, uint64_t sums[32][MAX_PARTITIONS], int n, int pred_order, int max_param, int exact)
Definition flacenc.c:647
#define rice_encode_count(sum, n, k)
Definition flacenc.c:613
static void write_frame_header(FlacEncodeContext *s)
Definition flacenc.c:1418
static uint64_t calc_rice_params(RiceContext *rc, uint32_t udata[FLAC_MAX_BLOCKSIZE], uint64_t sums[32][MAX_PARTITIONS], int pmin, int pmax, const int32_t *data, int n, int pred_order, int exact)
Definition flacenc.c:717
static const AVClass flac_encoder_class
Definition flacenc.c:1749
#define MAX_FIXED_ORDER
Definition flacenc.c:45
static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n, int order)
Definition flacenc.c:783
#define MAX_LPC_SHIFT
Definition flacenc.c:50
#define FLAC_SUBFRAME_VERBATIM
Definition flacenc.c:41
static int estimate_stereo_mode(const int32_t *left_ch, const int32_t *right_ch, int n, int max_rice_param, int bps)
Definition flacenc.c:1288
static void calc_sum_top(int pmax, int kmax, const uint32_t *data, int n, int pred_order, uint64_t sums[32][MAX_PARTITIONS])
Definition flacenc.c:677
static void write_utf8(PutBitContext *pb, uint32_t val)
Definition flacenc.c:1411
static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
Definition flacenc.c:1569
static int find_optimal_param(uint64_t sum, int n, int max_param)
Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
Definition flacenc.c:618
static int get_max_p_order(int max_porder, int n, int order)
Definition flacenc.c:757
static void channel_decorrelation(FlacEncodeContext *s)
Perform stereo channel decorrelation.
Definition flacenc.c:1344
static void set_sr_golomb_flac(PutBitContext *pb, int i, int k)
Definition flacenc.c:1455
#define MIN_LPC_SHIFT
Definition flacenc.c:49
static av_cold int flac_encode_init(AVCodecContext *avctx)
Definition flacenc.c:270
static int encode_frame(FlacEncodeContext *s)
Definition flacenc.c:1214
static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
Definition flacenc.c:563
static int select_blocksize(int samplerate, int block_time_ms)
Set blocksize based on samplerate.
Definition flacenc.c:193
static int encode_residual_fixed_with_residual_limit(int32_t *res, const int32_t *smp, int n, int order)
Definition flacenc.c:881
static int lpc_encode_choose_datapath(FlacEncodeContext *s, int32_t bps, int32_t *res, const int32_t *smp, const int64_t *smp_33bps, int len, int order, int32_t *coefs, int shift)
Definition flacenc.c:923
#define COPY_SAMPLES(bits, shift0)
static int encode_residual_fixed_with_residual_limit_33bps(int32_t *res, const int64_t *smp, int n, int order)
Definition flacenc.c:888
static int lpc_encode_with_residual_limit_33bps(int32_t *res, const int64_t *smp, int len, int order, int32_t *coefs, int shift)
Definition flacenc.c:917
#define ENCODE_RESIDUAL_FIXED_WITH_RESIDUAL_LIMIT()
Definition flacenc.c:839
static uint64_t rice_count_exact(const int32_t *res, int n, int k)
Definition flacenc.c:550
#define MAX_PARTITIONS
Definition flacenc.c:47
#define FLAC_SUBFRAME_LPC
Definition flacenc.c:43
#define LPC_ENCODE_WITH_RESIDUAL_LIMIT()
Definition flacenc.c:894
static int update_md5_sum(FlacEncodeContext *s, const void *samples)
Definition flacenc.c:1579
static int find_optimal_param_exact(uint64_t sums[32][MAX_PARTITIONS], int i, int max_param)
Definition flacenc.c:630
static int count_frame_header(FlacEncodeContext *s)
Definition flacenc.c:1178
static int flac_get_max_frame_size(int blocksize, int ch, int bps)
Calculate an estimate for the maximum frame size based on verbatim mode.
Definition flacenc.c:167
#define DEFAULT_TO_VERBATIM()
Definition flacenc.c:950
static void remove_wasted_bits(FlacEncodeContext *s)
Definition flacenc.c:1234
static void calc_sum_next(int level, uint64_t sums[32][MAX_PARTITIONS], int kmax)
Definition flacenc.c:707
static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition flacenc.c:1624
static uint64_t find_subframe_rice_params(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
Definition flacenc.c:766
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition lpc.c:367
int ff_lpc_calc_coefs(LPCContext *s, const int32_t *samples, int blocksize, int min_order, int max_order, int precision, int32_t coefs[][MAX_LPC_ORDER], int *shift, enum FFLPCType lpc_type, int lpc_passes, int omethod, int min_shift, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
Definition lpc.c:240
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition lpc.c:342
#define av_unused
Definition attributes.h:164
#define av_cold
Definition attributes.h:117
#define ORDER_METHOD_4LEVEL
Definition lpc.h:31
#define ORDER_METHOD_EST
Definition lpc.h:29
#define ORDER_METHOD_SEARCH
Definition lpc.h:33
#define MAX_LPC_ORDER
Definition lpc.h:37
@ FF_LPC_TYPE_NONE
do not use LPC prediction or use all zero coefficients
Definition lpc.h:44
@ FF_LPC_TYPE_CHOLESKY
Cholesky factorization.
Definition lpc.h:47
@ FF_LPC_TYPE_FIXED
fixed LPC coefficients
Definition lpc.h:45
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
Definition lpc.h:46
@ FF_LPC_TYPE_DEFAULT
use the codec default LPC type
Definition lpc.h:43
@ FF_LPC_TYPE_NB
Not part of ABI.
Definition lpc.h:48
#define ORDER_METHOD_LOG
Definition lpc.h:34
#define ORDER_METHOD_8LEVEL
Definition lpc.h:32
#define MIN_LPC_ORDER
Definition lpc.h:36
#define ORDER_METHOD_2LEVEL
Definition lpc.h:30
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
Public header for MD5 hash function implementation.
Memory handling functions.
unsigned bps
Definition movenc.c:2129
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
bitstream writer API
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition put_bits.h:291
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void put_sbits63(PutBitContext *pb, int n, int64_t value)
Definition put_bits.h:391
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition put_bits.h:301
#define av_bswap16
Definition bswap.h:28
static const uint8_t header[24]
Definition sdr2.c:68
An AVChannelLayout holds information about the channel layout of audio data.
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1056
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1048
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition avcodec.h:1378
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1578
int sample_rate
samples per second
Definition avcodec.h:1041
int compression_level
Definition avcodec.h:1243
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1069
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
Definition md5.c:42
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:603
uint8_t * data
Definition packet.h:610
int min_prediction_order
Definition flacenc.c:63
int min_partition_order
Definition flacenc.c:66
int prediction_order_method
Definition flacenc.c:65
int exact_rice_parameters
Definition flacenc.c:69
int max_partition_order
Definition flacenc.c:67
int lpc_coeff_precision
Definition flacenc.c:62
int max_prediction_order
Definition flacenc.c:64
PutBitContext pb
Definition flacenc.c:108
FlacFrame frame
Definition flacenc.c:120
unsigned int md5_buffer_size
Definition flacenc.c:126
CompressionOptions options
Definition flacenc.c:121
uint64_t sample_count
Definition flacenc.c:118
struct AVMD5 * md5ctx
Definition flacenc.c:124
LPCContext lpc_ctx
Definition flacenc.c:123
uint32_t frame_count
Definition flacenc.c:117
uint8_t md5sum[16]
Definition flacenc.c:119
uint8_t * md5_buffer
Definition flacenc.c:125
FLACEncDSPContext flac_dsp
Definition flacenc.c:128
int64_t next_pts
Definition flacenc.c:131
BswapDSPContext bdsp
Definition flacenc.c:127
int max_encoded_framesize
Definition flacenc.c:116
AVCodecContext * avctx
Definition flacenc.c:122
int ch_mode
Definition flacenc.c:102
int64_t samples_33bps[FLAC_MAX_BLOCKSIZE]
Definition flacenc.c:98
FlacSubframe subframes[FLAC_MAX_CHANNELS]
Definition flacenc.c:97
uint8_t crc8
Definition flacenc.c:101
int bs_code[2]
Definition flacenc.c:100
int verbatim_only
Definition flacenc.c:103
int blocksize
Definition flacenc.c:99
uint32_t rc_udata[FLAC_MAX_BLOCKSIZE]
Definition flacenc.c:89
int32_t coefs[MAX_LPC_ORDER]
Definition flacenc.c:85
int32_t samples[FLAC_MAX_BLOCKSIZE]
Definition flacenc.c:92
uint64_t rc_sums[32][MAX_PARTITIONS]
Definition flacenc.c:90
int type_code
Definition flacenc.c:81
int32_t residual[FLAC_MAX_BLOCKSIZE+11]
Definition flacenc.c:93
RiceContext rc
Definition flacenc.c:88
int params[MAX_PARTITIONS]
Definition flacenc.c:76
int porder
Definition flacenc.c:75
enum CodingMode coding_mode
Definition flacenc.c:74
uint8_t level
Definition svq3.c:208
static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples)
Compute the end timestamp of a frame.
Definition sync_queue.c:118
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
int len
static double c[64]