FFmpeg  4.0
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"
23 #include "libavutil/crc.h"
24 #include "libavutil/intmath.h"
25 #include "libavutil/md5.h"
26 #include "libavutil/opt.h"
27 
28 #include "avcodec.h"
29 #include "bswapdsp.h"
30 #include "put_bits.h"
31 #include "golomb.h"
32 #include "internal.h"
33 #include "lpc.h"
34 #include "flac.h"
35 #include "flacdata.h"
36 #include "flacdsp.h"
37 
38 #define FLAC_SUBFRAME_CONSTANT 0
39 #define FLAC_SUBFRAME_VERBATIM 1
40 #define FLAC_SUBFRAME_FIXED 8
41 #define FLAC_SUBFRAME_LPC 32
42 
43 #define MAX_FIXED_ORDER 4
44 #define MAX_PARTITION_ORDER 8
45 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
46 #define MAX_LPC_PRECISION 15
47 #define MIN_LPC_SHIFT 0
48 #define MAX_LPC_SHIFT 15
49 
50 enum CodingMode {
53 };
54 
55 typedef struct CompressionOptions {
66  int ch_mode;
70 
71 typedef struct RiceContext {
72  enum CodingMode coding_mode;
73  int porder;
75 } RiceContext;
76 
77 typedef struct FlacSubframe {
78  int type;
79  int type_code;
80  int obits;
81  int wasted;
82  int order;
84  int shift;
85 
87  uint32_t rc_udata[FLAC_MAX_BLOCKSIZE];
88  uint64_t rc_sums[32][MAX_PARTITIONS];
89 
92 } FlacSubframe;
93 
94 typedef struct FlacFrame {
96  int blocksize;
97  int bs_code[2];
99  int ch_mode;
101 } FlacFrame;
102 
103 typedef struct FlacEncodeContext {
104  AVClass *class;
106  int channels;
108  int sr_code[2];
109  int bps_code;
114  uint32_t frame_count;
115  uint64_t sample_count;
116  uint8_t md5sum[16];
121  struct AVMD5 *md5ctx;
123  unsigned int md5_buffer_size;
126 
127  int flushed;
128  int64_t next_pts;
130 
131 
132 /**
133  * Write streaminfo metadata block to byte array.
134  */
136 {
137  PutBitContext pb;
138 
139  memset(header, 0, FLAC_STREAMINFO_SIZE);
140  init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
141 
142  /* streaminfo metadata block */
143  put_bits(&pb, 16, s->max_blocksize);
144  put_bits(&pb, 16, s->max_blocksize);
145  put_bits(&pb, 24, s->min_framesize);
146  put_bits(&pb, 24, s->max_framesize);
147  put_bits(&pb, 20, s->samplerate);
148  put_bits(&pb, 3, s->channels-1);
149  put_bits(&pb, 5, s->avctx->bits_per_raw_sample - 1);
150  /* write 36-bit sample count in 2 put_bits() calls */
151  put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
152  put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
153  flush_put_bits(&pb);
154  memcpy(&header[18], s->md5sum, 16);
155 }
156 
157 
158 /**
159  * Set blocksize based on samplerate.
160  * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
161  */
162 static int select_blocksize(int samplerate, int block_time_ms)
163 {
164  int i;
165  int target;
166  int blocksize;
167 
168  av_assert0(samplerate > 0);
169  blocksize = ff_flac_blocksize_table[1];
170  target = (samplerate * block_time_ms) / 1000;
171  for (i = 0; i < 16; i++) {
172  if (target >= ff_flac_blocksize_table[i] &&
173  ff_flac_blocksize_table[i] > blocksize) {
174  blocksize = ff_flac_blocksize_table[i];
175  }
176  }
177  return blocksize;
178 }
179 
180 
182 {
183  AVCodecContext *avctx = s->avctx;
184  CompressionOptions *opt = &s->options;
185 
186  av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);
187 
188  switch (opt->lpc_type) {
189  case FF_LPC_TYPE_NONE:
190  av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
191  break;
192  case FF_LPC_TYPE_FIXED:
193  av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
194  break;
196  av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
197  break;
199  av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
200  opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
201  break;
202  }
203 
204  av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
206 
207  switch (opt->prediction_order_method) {
208  case ORDER_METHOD_EST:
209  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
210  break;
211  case ORDER_METHOD_2LEVEL:
212  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
213  break;
214  case ORDER_METHOD_4LEVEL:
215  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
216  break;
217  case ORDER_METHOD_8LEVEL:
218  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
219  break;
220  case ORDER_METHOD_SEARCH:
221  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
222  break;
223  case ORDER_METHOD_LOG:
224  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
225  break;
226  }
227 
228 
229  av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
231 
232  av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
233 
234  av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
235  opt->lpc_coeff_precision);
236 }
237 
238 
240 {
241  int freq = avctx->sample_rate;
242  int channels = avctx->channels;
243  FlacEncodeContext *s = avctx->priv_data;
244  int i, level, ret;
245  uint8_t *streaminfo;
246 
247  s->avctx = avctx;
248 
249  switch (avctx->sample_fmt) {
250  case AV_SAMPLE_FMT_S16:
251  avctx->bits_per_raw_sample = 16;
252  s->bps_code = 4;
253  break;
254  case AV_SAMPLE_FMT_S32:
255  if (avctx->bits_per_raw_sample != 24)
256  av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
257  avctx->bits_per_raw_sample = 24;
258  s->bps_code = 6;
259  break;
260  }
261 
263  av_log(avctx, AV_LOG_ERROR, "%d channels not supported (max %d)\n",
264  channels, FLAC_MAX_CHANNELS);
265  return AVERROR(EINVAL);
266  }
267  s->channels = channels;
268 
269  /* find samplerate in table */
270  if (freq < 1)
271  return AVERROR(EINVAL);
272  for (i = 4; i < 12; i++) {
273  if (freq == ff_flac_sample_rate_table[i]) {
275  s->sr_code[0] = i;
276  s->sr_code[1] = 0;
277  break;
278  }
279  }
280  /* if not in table, samplerate is non-standard */
281  if (i == 12) {
282  if (freq % 1000 == 0 && freq < 255000) {
283  s->sr_code[0] = 12;
284  s->sr_code[1] = freq / 1000;
285  } else if (freq % 10 == 0 && freq < 655350) {
286  s->sr_code[0] = 14;
287  s->sr_code[1] = freq / 10;
288  } else if (freq < 65535) {
289  s->sr_code[0] = 13;
290  s->sr_code[1] = freq;
291  } else {
292  av_log(avctx, AV_LOG_ERROR, "%d Hz not supported\n", freq);
293  return AVERROR(EINVAL);
294  }
295  s->samplerate = freq;
296  }
297 
298  /* set compression option defaults based on avctx->compression_level */
299  if (avctx->compression_level < 0)
300  s->options.compression_level = 5;
301  else
303 
304  level = s->options.compression_level;
305  if (level > 12) {
306  av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
308  return AVERROR(EINVAL);
309  }
310 
311  s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
312 
318  FF_LPC_TYPE_LEVINSON})[level];
319 
320  if (s->options.min_prediction_order < 0)
321  s->options.min_prediction_order = ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
322  if (s->options.max_prediction_order < 0)
323  s->options.max_prediction_order = ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
324 
325  if (s->options.prediction_order_method < 0)
330  ORDER_METHOD_SEARCH})[level];
331 
333  av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
335  return AVERROR(EINVAL);
336  }
337  if (s->options.min_partition_order < 0)
338  s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
339  if (s->options.max_partition_order < 0)
340  s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
341 
342 #if FF_API_PRIVATE_OPT
344  if (avctx->min_prediction_order >= 0) {
345  if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
346  if (avctx->min_prediction_order > MAX_FIXED_ORDER) {
347  av_log(avctx, AV_LOG_WARNING,
348  "invalid min prediction order %d, clamped to %d\n",
351  }
352  } else if (avctx->min_prediction_order < MIN_LPC_ORDER ||
354  av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
355  avctx->min_prediction_order);
356  return AVERROR(EINVAL);
357  }
359  }
360  if (avctx->max_prediction_order >= 0) {
361  if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
362  if (avctx->max_prediction_order > MAX_FIXED_ORDER) {
363  av_log(avctx, AV_LOG_WARNING,
364  "invalid max prediction order %d, clamped to %d\n",
367  }
368  } else if (avctx->max_prediction_order < MIN_LPC_ORDER ||
370  av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
371  avctx->max_prediction_order);
372  return AVERROR(EINVAL);
373  }
375  }
377 #endif
378  if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
381  } else if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
383  av_log(avctx, AV_LOG_WARNING,
384  "invalid min prediction order %d, clamped to %d\n",
387  }
389  av_log(avctx, AV_LOG_WARNING,
390  "invalid max prediction order %d, clamped to %d\n",
393  }
394  }
395 
397  av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
399  return AVERROR(EINVAL);
400  }
401 
402  if (avctx->frame_size > 0) {
403  if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
404  avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
405  av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
406  avctx->frame_size);
407  return AVERROR(EINVAL);
408  }
409  } else {
411  }
412  s->max_blocksize = s->avctx->frame_size;
413 
414  /* set maximum encoded frame size in verbatim mode */
416  s->channels,
418 
419  /* initialize MD5 context */
420  s->md5ctx = av_md5_alloc();
421  if (!s->md5ctx)
422  return AVERROR(ENOMEM);
423  av_md5_init(s->md5ctx);
424 
425  streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
426  if (!streaminfo)
427  return AVERROR(ENOMEM);
428  write_streaminfo(s, streaminfo);
429  avctx->extradata = streaminfo;
431 
432  s->frame_count = 0;
434 
435  if (channels == 3 &&
437  channels == 4 &&
438  avctx->channel_layout != AV_CH_LAYOUT_2_2 &&
439  avctx->channel_layout != AV_CH_LAYOUT_QUAD ||
440  channels == 5 &&
443  channels == 6 &&
446  if (avctx->channel_layout) {
447  av_log(avctx, AV_LOG_ERROR, "Channel layout not supported by Flac, "
448  "output stream will have incorrect "
449  "channel layout.\n");
450  } else {
451  av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
452  "will use Flac channel layout for "
453  "%d channels.\n", channels);
454  }
455  }
456 
457  ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
459 
460  ff_bswapdsp_init(&s->bdsp);
461  ff_flacdsp_init(&s->flac_dsp, avctx->sample_fmt, channels,
462  avctx->bits_per_raw_sample);
463 
465 
466  return ret;
467 }
468 
469 
470 static void init_frame(FlacEncodeContext *s, int nb_samples)
471 {
472  int i, ch;
473  FlacFrame *frame;
474 
475  frame = &s->frame;
476 
477  for (i = 0; i < 16; i++) {
478  if (nb_samples == ff_flac_blocksize_table[i]) {
480  frame->bs_code[0] = i;
481  frame->bs_code[1] = 0;
482  break;
483  }
484  }
485  if (i == 16) {
486  frame->blocksize = nb_samples;
487  if (frame->blocksize <= 256) {
488  frame->bs_code[0] = 6;
489  frame->bs_code[1] = frame->blocksize-1;
490  } else {
491  frame->bs_code[0] = 7;
492  frame->bs_code[1] = frame->blocksize-1;
493  }
494  }
495 
496  for (ch = 0; ch < s->channels; ch++) {
497  FlacSubframe *sub = &frame->subframes[ch];
498 
499  sub->wasted = 0;
500  sub->obits = s->avctx->bits_per_raw_sample;
501 
502  if (sub->obits > 16)
504  else
506  }
507 
508  frame->verbatim_only = 0;
509 }
510 
511 
512 /**
513  * Copy channel-interleaved input samples into separate subframes.
514  */
515 static void copy_samples(FlacEncodeContext *s, const void *samples)
516 {
517  int i, j, ch;
518  FlacFrame *frame;
521 
522 #define COPY_SAMPLES(bits) do { \
523  const int ## bits ## _t *samples0 = samples; \
524  frame = &s->frame; \
525  for (i = 0, j = 0; i < frame->blocksize; i++) \
526  for (ch = 0; ch < s->channels; ch++, j++) \
527  frame->subframes[ch].samples[i] = samples0[j] >> shift; \
528 } while (0)
529 
531  COPY_SAMPLES(16);
532  else
533  COPY_SAMPLES(32);
534 }
535 
536 
537 static uint64_t rice_count_exact(const int32_t *res, int n, int k)
538 {
539  int i;
540  uint64_t count = 0;
541 
542  for (i = 0; i < n; i++) {
543  int32_t v = -2 * res[i] - 1;
544  v ^= v >> 31;
545  count += (v >> k) + 1 + k;
546  }
547  return count;
548 }
549 
550 
552  int pred_order)
553 {
554  int p, porder, psize;
555  int i, part_end;
556  uint64_t count = 0;
557 
558  /* subframe header */
559  count += 8;
560 
561  if (sub->wasted)
562  count += sub->wasted;
563 
564  /* subframe */
565  if (sub->type == FLAC_SUBFRAME_CONSTANT) {
566  count += sub->obits;
567  } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
568  count += s->frame.blocksize * sub->obits;
569  } else {
570  /* warm-up samples */
571  count += pred_order * sub->obits;
572 
573  /* LPC coefficients */
574  if (sub->type == FLAC_SUBFRAME_LPC)
575  count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
576 
577  /* rice-encoded block */
578  count += 2;
579 
580  /* partition order */
581  porder = sub->rc.porder;
582  psize = s->frame.blocksize >> porder;
583  count += 4;
584 
585  /* residual */
586  i = pred_order;
587  part_end = psize;
588  for (p = 0; p < 1 << porder; p++) {
589  int k = sub->rc.params[p];
590  count += sub->rc.coding_mode;
591  count += rice_count_exact(&sub->residual[i], part_end - i, k);
592  i = part_end;
593  part_end = FFMIN(s->frame.blocksize, part_end + psize);
594  }
595  }
596 
597  return count;
598 }
599 
600 
601 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
602 
603 /**
604  * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
605  */
606 static int find_optimal_param(uint64_t sum, int n, int max_param)
607 {
608  int k;
609  uint64_t sum2;
610 
611  if (sum <= n >> 1)
612  return 0;
613  sum2 = sum - (n >> 1);
614  k = av_log2(av_clipl_int32(sum2 / n));
615  return FFMIN(k, max_param);
616 }
617 
618 static int find_optimal_param_exact(uint64_t sums[32][MAX_PARTITIONS], int i, int max_param)
619 {
620  int bestk = 0;
621  int64_t bestbits = INT64_MAX;
622  int k;
623 
624  for (k = 0; k <= max_param; k++) {
625  int64_t bits = sums[k][i];
626  if (bits < bestbits) {
627  bestbits = bits;
628  bestk = k;
629  }
630  }
631 
632  return bestk;
633 }
634 
635 static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder,
636  uint64_t sums[32][MAX_PARTITIONS],
637  int n, int pred_order, int max_param, int exact)
638 {
639  int i;
640  int k, cnt, part;
641  uint64_t all_bits;
642 
643  part = (1 << porder);
644  all_bits = 4 * part;
645 
646  cnt = (n >> porder) - pred_order;
647  for (i = 0; i < part; i++) {
648  if (exact) {
649  k = find_optimal_param_exact(sums, i, max_param);
650  all_bits += sums[k][i];
651  } else {
652  k = find_optimal_param(sums[0][i], cnt, max_param);
653  all_bits += rice_encode_count(sums[0][i], cnt, k);
654  }
655  rc->params[i] = k;
656  cnt = n >> porder;
657  }
658 
659  rc->porder = porder;
660 
661  return all_bits;
662 }
663 
664 
665 static void calc_sum_top(int pmax, int kmax, const uint32_t *data, int n, int pred_order,
666  uint64_t sums[32][MAX_PARTITIONS])
667 {
668  int i, k;
669  int parts;
670  const uint32_t *res, *res_end;
671 
672  /* sums for highest level */
673  parts = (1 << pmax);
674 
675  for (k = 0; k <= kmax; k++) {
676  res = &data[pred_order];
677  res_end = &data[n >> pmax];
678  for (i = 0; i < parts; i++) {
679  if (kmax) {
680  uint64_t sum = (1LL + k) * (res_end - res);
681  while (res < res_end)
682  sum += *(res++) >> k;
683  sums[k][i] = sum;
684  } else {
685  uint64_t sum = 0;
686  while (res < res_end)
687  sum += *(res++);
688  sums[k][i] = sum;
689  }
690  res_end += n >> pmax;
691  }
692  }
693 }
694 
695 static void calc_sum_next(int level, uint64_t sums[32][MAX_PARTITIONS], int kmax)
696 {
697  int i, k;
698  int parts = (1 << level);
699  for (i = 0; i < parts; i++) {
700  for (k=0; k<=kmax; k++)
701  sums[k][i] = sums[k][2*i] + sums[k][2*i+1];
702  }
703 }
704 
705 static uint64_t calc_rice_params(RiceContext *rc,
706  uint32_t udata[FLAC_MAX_BLOCKSIZE],
707  uint64_t sums[32][MAX_PARTITIONS],
708  int pmin, int pmax,
709  const int32_t *data, int n, int pred_order, int exact)
710 {
711  int i;
712  uint64_t bits[MAX_PARTITION_ORDER+1];
713  int opt_porder;
714  RiceContext tmp_rc;
715  int kmax = (1 << rc->coding_mode) - 2;
716 
717  av_assert1(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
718  av_assert1(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
719  av_assert1(pmin <= pmax);
720 
721  tmp_rc.coding_mode = rc->coding_mode;
722 
723  for (i = 0; i < n; i++)
724  udata[i] = (2 * data[i]) ^ (data[i] >> 31);
725 
726  calc_sum_top(pmax, exact ? kmax : 0, udata, n, pred_order, sums);
727 
728  opt_porder = pmin;
729  bits[pmin] = UINT32_MAX;
730  for (i = pmax; ; ) {
731  bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums, n, pred_order, kmax, exact);
732  if (bits[i] < bits[opt_porder] || pmax == pmin) {
733  opt_porder = i;
734  *rc = tmp_rc;
735  }
736  if (i == pmin)
737  break;
738  calc_sum_next(--i, sums, exact ? kmax : 0);
739  }
740 
741  return bits[opt_porder];
742 }
743 
744 
745 static int get_max_p_order(int max_porder, int n, int order)
746 {
747  int porder = FFMIN(max_porder, av_log2(n^(n-1)));
748  if (order > 0)
749  porder = FFMIN(porder, av_log2(n/order));
750  return porder;
751 }
752 
753 
755  FlacSubframe *sub, int pred_order)
756 {
758  s->frame.blocksize, pred_order);
760  s->frame.blocksize, pred_order);
761 
762  uint64_t bits = 8 + pred_order * sub->obits + 2 + sub->rc.coding_mode;
763  if (sub->type == FLAC_SUBFRAME_LPC)
764  bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
765  bits += calc_rice_params(&sub->rc, sub->rc_udata, sub->rc_sums, pmin, pmax, sub->residual,
766  s->frame.blocksize, pred_order, s->options.exact_rice_parameters);
767  return bits;
768 }
769 
770 
771 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
772  int order)
773 {
774  int i;
775 
776  for (i = 0; i < order; i++)
777  res[i] = smp[i];
778 
779  if (order == 0) {
780  for (i = order; i < n; i++)
781  res[i] = smp[i];
782  } else if (order == 1) {
783  for (i = order; i < n; i++)
784  res[i] = smp[i] - smp[i-1];
785  } else if (order == 2) {
786  int a = smp[order-1] - smp[order-2];
787  for (i = order; i < n; i += 2) {
788  int b = smp[i ] - smp[i-1];
789  res[i] = b - a;
790  a = smp[i+1] - smp[i ];
791  res[i+1] = a - b;
792  }
793  } else if (order == 3) {
794  int a = smp[order-1] - smp[order-2];
795  int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
796  for (i = order; i < n; i += 2) {
797  int b = smp[i ] - smp[i-1];
798  int d = b - a;
799  res[i] = d - c;
800  a = smp[i+1] - smp[i ];
801  c = a - b;
802  res[i+1] = c - d;
803  }
804  } else {
805  int a = smp[order-1] - smp[order-2];
806  int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
807  int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
808  for (i = order; i < n; i += 2) {
809  int b = smp[i ] - smp[i-1];
810  int d = b - a;
811  int f = d - c;
812  res[i ] = f - e;
813  a = smp[i+1] - smp[i ];
814  c = a - b;
815  e = c - d;
816  res[i+1] = e - f;
817  }
818  }
819 }
820 
821 
823 {
824  int i, n;
825  int min_order, max_order, opt_order, omethod;
826  FlacFrame *frame;
827  FlacSubframe *sub;
829  int shift[MAX_LPC_ORDER];
830  int32_t *res, *smp;
831 
832  frame = &s->frame;
833  sub = &frame->subframes[ch];
834  res = sub->residual;
835  smp = sub->samples;
836  n = frame->blocksize;
837 
838  /* CONSTANT */
839  for (i = 1; i < n; i++)
840  if(smp[i] != smp[0])
841  break;
842  if (i == n) {
843  sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
844  res[0] = smp[0];
845  return subframe_count_exact(s, sub, 0);
846  }
847 
848  /* VERBATIM */
849  if (frame->verbatim_only || n < 5) {
850  sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
851  memcpy(res, smp, n * sizeof(int32_t));
852  return subframe_count_exact(s, sub, 0);
853  }
854 
855  min_order = s->options.min_prediction_order;
856  max_order = s->options.max_prediction_order;
857  omethod = s->options.prediction_order_method;
858 
859  /* FIXED */
860  sub->type = FLAC_SUBFRAME_FIXED;
861  if (s->options.lpc_type == FF_LPC_TYPE_NONE ||
862  s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
863  uint64_t bits[MAX_FIXED_ORDER+1];
864  if (max_order > MAX_FIXED_ORDER)
865  max_order = MAX_FIXED_ORDER;
866  opt_order = 0;
867  bits[0] = UINT32_MAX;
868  for (i = min_order; i <= max_order; i++) {
869  encode_residual_fixed(res, smp, n, i);
870  bits[i] = find_subframe_rice_params(s, sub, i);
871  if (bits[i] < bits[opt_order])
872  opt_order = i;
873  }
874  sub->order = opt_order;
875  sub->type_code = sub->type | sub->order;
876  if (sub->order != max_order) {
877  encode_residual_fixed(res, smp, n, sub->order);
878  find_subframe_rice_params(s, sub, sub->order);
879  }
880  return subframe_count_exact(s, sub, sub->order);
881  }
882 
883  /* LPC */
884  sub->type = FLAC_SUBFRAME_LPC;
885  opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
886  s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
887  s->options.lpc_passes, omethod,
889 
890  if (omethod == ORDER_METHOD_2LEVEL ||
891  omethod == ORDER_METHOD_4LEVEL ||
892  omethod == ORDER_METHOD_8LEVEL) {
893  int levels = 1 << omethod;
894  uint64_t bits[1 << ORDER_METHOD_8LEVEL];
895  int order = -1;
896  int opt_index = levels-1;
897  opt_order = max_order-1;
898  bits[opt_index] = UINT32_MAX;
899  for (i = levels-1; i >= 0; i--) {
900  int last_order = order;
901  order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
902  order = av_clip(order, min_order - 1, max_order - 1);
903  if (order == last_order)
904  continue;
905  if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(order) <= 32) {
906  s->flac_dsp.lpc16_encode(res, smp, n, order+1, coefs[order],
907  shift[order]);
908  } else {
909  s->flac_dsp.lpc32_encode(res, smp, n, order+1, coefs[order],
910  shift[order]);
911  }
912  bits[i] = find_subframe_rice_params(s, sub, order+1);
913  if (bits[i] < bits[opt_index]) {
914  opt_index = i;
915  opt_order = order;
916  }
917  }
918  opt_order++;
919  } else if (omethod == ORDER_METHOD_SEARCH) {
920  // brute-force optimal order search
921  uint64_t bits[MAX_LPC_ORDER];
922  opt_order = 0;
923  bits[0] = UINT32_MAX;
924  for (i = min_order-1; i < max_order; i++) {
925  if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(i) <= 32) {
926  s->flac_dsp.lpc16_encode(res, smp, n, i+1, coefs[i], shift[i]);
927  } else {
928  s->flac_dsp.lpc32_encode(res, smp, n, i+1, coefs[i], shift[i]);
929  }
930  bits[i] = find_subframe_rice_params(s, sub, i+1);
931  if (bits[i] < bits[opt_order])
932  opt_order = i;
933  }
934  opt_order++;
935  } else if (omethod == ORDER_METHOD_LOG) {
936  uint64_t bits[MAX_LPC_ORDER];
937  int step;
938 
939  opt_order = min_order - 1 + (max_order-min_order)/3;
940  memset(bits, -1, sizeof(bits));
941 
942  for (step = 16; step; step >>= 1) {
943  int last = opt_order;
944  for (i = last-step; i <= last+step; i += step) {
945  if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
946  continue;
947  if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(i) <= 32) {
948  s->flac_dsp.lpc32_encode(res, smp, n, i+1, coefs[i], shift[i]);
949  } else {
950  s->flac_dsp.lpc16_encode(res, smp, n, i+1, coefs[i], shift[i]);
951  }
952  bits[i] = find_subframe_rice_params(s, sub, i+1);
953  if (bits[i] < bits[opt_order])
954  opt_order = i;
955  }
956  }
957  opt_order++;
958  }
959 
960  if (s->options.multi_dim_quant) {
961  int allsteps = 1;
962  int i, step, improved;
963  int64_t best_score = INT64_MAX;
964  int32_t qmax;
965 
966  qmax = (1 << (s->options.lpc_coeff_precision - 1)) - 1;
967 
968  for (i=0; i<opt_order; i++)
969  allsteps *= 3;
970 
971  do {
972  improved = 0;
973  for (step = 0; step < allsteps; step++) {
974  int tmp = step;
975  int32_t lpc_try[MAX_LPC_ORDER];
976  int64_t score = 0;
977  int diffsum = 0;
978 
979  for (i=0; i<opt_order; i++) {
980  int diff = ((tmp + 1) % 3) - 1;
981  lpc_try[i] = av_clip(coefs[opt_order - 1][i] + diff, -qmax, qmax);
982  tmp /= 3;
983  diffsum += !!diff;
984  }
985  if (diffsum >8)
986  continue;
987 
988  if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(opt_order - 1) <= 32) {
989  s->flac_dsp.lpc16_encode(res, smp, n, opt_order, lpc_try, shift[opt_order-1]);
990  } else {
991  s->flac_dsp.lpc32_encode(res, smp, n, opt_order, lpc_try, shift[opt_order-1]);
992  }
993  score = find_subframe_rice_params(s, sub, opt_order);
994  if (score < best_score) {
995  best_score = score;
996  memcpy(coefs[opt_order-1], lpc_try, sizeof(*coefs));
997  improved=1;
998  }
999  }
1000  } while(improved);
1001  }
1002 
1003  sub->order = opt_order;
1004  sub->type_code = sub->type | (sub->order-1);
1005  sub->shift = shift[sub->order-1];
1006  for (i = 0; i < sub->order; i++)
1007  sub->coefs[i] = coefs[sub->order-1][i];
1008 
1009  if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(opt_order) <= 32) {
1010  s->flac_dsp.lpc16_encode(res, smp, n, sub->order, sub->coefs, sub->shift);
1011  } else {
1012  s->flac_dsp.lpc32_encode(res, smp, n, sub->order, sub->coefs, sub->shift);
1013  }
1014 
1015  find_subframe_rice_params(s, sub, sub->order);
1016 
1017  return subframe_count_exact(s, sub, sub->order);
1018 }
1019 
1020 
1022 {
1024  int count;
1025 
1026  /*
1027  <14> Sync code
1028  <1> Reserved
1029  <1> Blocking strategy
1030  <4> Block size in inter-channel samples
1031  <4> Sample rate
1032  <4> Channel assignment
1033  <3> Sample size in bits
1034  <1> Reserved
1035  */
1036  count = 32;
1037 
1038  /* coded frame number */
1039  PUT_UTF8(s->frame_count, tmp, count += 8;)
1040 
1041  /* explicit block size */
1042  if (s->frame.bs_code[0] == 6)
1043  count += 8;
1044  else if (s->frame.bs_code[0] == 7)
1045  count += 16;
1046 
1047  /* explicit sample rate */
1048  count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12) * 2) * 8;
1049 
1050  /* frame header CRC-8 */
1051  count += 8;
1052 
1053  return count;
1054 }
1055 
1056 
1058 {
1059  int ch;
1060  uint64_t count;
1061 
1062  count = count_frame_header(s);
1063 
1064  for (ch = 0; ch < s->channels; ch++)
1065  count += encode_residual_ch(s, ch);
1066 
1067  count += (8 - (count & 7)) & 7; // byte alignment
1068  count += 16; // CRC-16
1069 
1070  count >>= 3;
1071  if (count > INT_MAX)
1072  return AVERROR_BUG;
1073  return count;
1074 }
1075 
1076 
1078 {
1079  int ch, i;
1080 
1081  for (ch = 0; ch < s->channels; ch++) {
1082  FlacSubframe *sub = &s->frame.subframes[ch];
1083  int32_t v = 0;
1084 
1085  for (i = 0; i < s->frame.blocksize; i++) {
1086  v |= sub->samples[i];
1087  if (v & 1)
1088  break;
1089  }
1090 
1091  if (v && !(v & 1)) {
1092  v = ff_ctz(v);
1093 
1094  for (i = 0; i < s->frame.blocksize; i++)
1095  sub->samples[i] >>= v;
1096 
1097  sub->wasted = v;
1098  sub->obits -= v;
1099 
1100  /* for 24-bit, check if removing wasted bits makes the range better
1101  suited for using RICE instead of RICE2 for entropy coding */
1102  if (sub->obits <= 17)
1104  }
1105  }
1106 }
1107 
1108 
1109 static int estimate_stereo_mode(const int32_t *left_ch, const int32_t *right_ch, int n,
1110  int max_rice_param)
1111 {
1112  int i, best;
1113  int32_t lt, rt;
1114  uint64_t sum[4];
1115  uint64_t score[4];
1116  int k;
1117 
1118  /* calculate sum of 2nd order residual for each channel */
1119  sum[0] = sum[1] = sum[2] = sum[3] = 0;
1120  for (i = 2; i < n; i++) {
1121  lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
1122  rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
1123  sum[2] += FFABS((lt + rt) >> 1);
1124  sum[3] += FFABS(lt - rt);
1125  sum[0] += FFABS(lt);
1126  sum[1] += FFABS(rt);
1127  }
1128  /* estimate bit counts */
1129  for (i = 0; i < 4; i++) {
1130  k = find_optimal_param(2 * sum[i], n, max_rice_param);
1131  sum[i] = rice_encode_count( 2 * sum[i], n, k);
1132  }
1133 
1134  /* calculate score for each mode */
1135  score[0] = sum[0] + sum[1];
1136  score[1] = sum[0] + sum[3];
1137  score[2] = sum[1] + sum[3];
1138  score[3] = sum[2] + sum[3];
1139 
1140  /* return mode with lowest score */
1141  best = 0;
1142  for (i = 1; i < 4; i++)
1143  if (score[i] < score[best])
1144  best = i;
1145 
1146  return best;
1147 }
1148 
1149 
1150 /**
1151  * Perform stereo channel decorrelation.
1152  */
1154 {
1155  FlacFrame *frame;
1156  int32_t *left, *right;
1157  int i, n;
1158 
1159  frame = &s->frame;
1160  n = frame->blocksize;
1161  left = frame->subframes[0].samples;
1162  right = frame->subframes[1].samples;
1163 
1164  if (s->channels != 2) {
1166  return;
1167  }
1168 
1169  if (s->options.ch_mode < 0) {
1170  int max_rice_param = (1 << frame->subframes[0].rc.coding_mode) - 2;
1171  frame->ch_mode = estimate_stereo_mode(left, right, n, max_rice_param);
1172  } else
1173  frame->ch_mode = s->options.ch_mode;
1174 
1175  /* perform decorrelation and adjust bits-per-sample */
1176  if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1177  return;
1178  if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1179  int32_t tmp;
1180  for (i = 0; i < n; i++) {
1181  tmp = left[i];
1182  left[i] = (tmp + right[i]) >> 1;
1183  right[i] = tmp - right[i];
1184  }
1185  frame->subframes[1].obits++;
1186  } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1187  for (i = 0; i < n; i++)
1188  right[i] = left[i] - right[i];
1189  frame->subframes[1].obits++;
1190  } else {
1191  for (i = 0; i < n; i++)
1192  left[i] -= right[i];
1193  frame->subframes[0].obits++;
1194  }
1195 }
1196 
1197 
1198 static void write_utf8(PutBitContext *pb, uint32_t val)
1199 {
1200  uint8_t tmp;
1201  PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1202 }
1203 
1204 
1206 {
1207  FlacFrame *frame;
1208  int crc;
1209 
1210  frame = &s->frame;
1211 
1212  put_bits(&s->pb, 16, 0xFFF8);
1213  put_bits(&s->pb, 4, frame->bs_code[0]);
1214  put_bits(&s->pb, 4, s->sr_code[0]);
1215 
1216  if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1217  put_bits(&s->pb, 4, s->channels-1);
1218  else
1219  put_bits(&s->pb, 4, frame->ch_mode + FLAC_MAX_CHANNELS - 1);
1220 
1221  put_bits(&s->pb, 3, s->bps_code);
1222  put_bits(&s->pb, 1, 0);
1223  write_utf8(&s->pb, s->frame_count);
1224 
1225  if (frame->bs_code[0] == 6)
1226  put_bits(&s->pb, 8, frame->bs_code[1]);
1227  else if (frame->bs_code[0] == 7)
1228  put_bits(&s->pb, 16, frame->bs_code[1]);
1229 
1230  if (s->sr_code[0] == 12)
1231  put_bits(&s->pb, 8, s->sr_code[1]);
1232  else if (s->sr_code[0] > 12)
1233  put_bits(&s->pb, 16, s->sr_code[1]);
1234 
1235  flush_put_bits(&s->pb);
1236  crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
1237  put_bits_count(&s->pb) >> 3);
1238  put_bits(&s->pb, 8, crc);
1239 }
1240 
1241 
1243 {
1244  int ch;
1245 
1246  for (ch = 0; ch < s->channels; ch++) {
1247  FlacSubframe *sub = &s->frame.subframes[ch];
1248  int i, p, porder, psize;
1249  int32_t *part_end;
1250  int32_t *res = sub->residual;
1251  int32_t *frame_end = &sub->residual[s->frame.blocksize];
1252 
1253  /* subframe header */
1254  put_bits(&s->pb, 1, 0);
1255  put_bits(&s->pb, 6, sub->type_code);
1256  put_bits(&s->pb, 1, !!sub->wasted);
1257  if (sub->wasted)
1258  put_bits(&s->pb, sub->wasted, 1);
1259 
1260  /* subframe */
1261  if (sub->type == FLAC_SUBFRAME_CONSTANT) {
1262  put_sbits(&s->pb, sub->obits, res[0]);
1263  } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
1264  while (res < frame_end)
1265  put_sbits(&s->pb, sub->obits, *res++);
1266  } else {
1267  /* warm-up samples */
1268  for (i = 0; i < sub->order; i++)
1269  put_sbits(&s->pb, sub->obits, *res++);
1270 
1271  /* LPC coefficients */
1272  if (sub->type == FLAC_SUBFRAME_LPC) {
1273  int cbits = s->options.lpc_coeff_precision;
1274  put_bits( &s->pb, 4, cbits-1);
1275  put_sbits(&s->pb, 5, sub->shift);
1276  for (i = 0; i < sub->order; i++)
1277  put_sbits(&s->pb, cbits, sub->coefs[i]);
1278  }
1279 
1280  /* rice-encoded block */
1281  put_bits(&s->pb, 2, sub->rc.coding_mode - 4);
1282 
1283  /* partition order */
1284  porder = sub->rc.porder;
1285  psize = s->frame.blocksize >> porder;
1286  put_bits(&s->pb, 4, porder);
1287 
1288  /* residual */
1289  part_end = &sub->residual[psize];
1290  for (p = 0; p < 1 << porder; p++) {
1291  int k = sub->rc.params[p];
1292  put_bits(&s->pb, sub->rc.coding_mode, k);
1293  while (res < part_end)
1294  set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
1295  part_end = FFMIN(frame_end, part_end + psize);
1296  }
1297  }
1298  }
1299 }
1300 
1301 
1303 {
1304  int crc;
1305  flush_put_bits(&s->pb);
1307  put_bits_count(&s->pb)>>3));
1308  put_bits(&s->pb, 16, crc);
1309  flush_put_bits(&s->pb);
1310 }
1311 
1312 
1314 {
1315  init_put_bits(&s->pb, avpkt->data, avpkt->size);
1316  write_frame_header(s);
1317  write_subframes(s);
1318  write_frame_footer(s);
1319  return put_bits_count(&s->pb) >> 3;
1320 }
1321 
1322 
1323 static int update_md5_sum(FlacEncodeContext *s, const void *samples)
1324 {
1325  const uint8_t *buf;
1326  int buf_size = s->frame.blocksize * s->channels *
1327  ((s->avctx->bits_per_raw_sample + 7) / 8);
1328 
1329  if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
1330  av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size);
1331  if (!s->md5_buffer)
1332  return AVERROR(ENOMEM);
1333  }
1334 
1335  if (s->avctx->bits_per_raw_sample <= 16) {
1336  buf = (const uint8_t *)samples;
1337 #if HAVE_BIGENDIAN
1338  s->bdsp.bswap16_buf((uint16_t *) s->md5_buffer,
1339  (const uint16_t *) samples, buf_size / 2);
1340  buf = s->md5_buffer;
1341 #endif
1342  } else {
1343  int i;
1344  const int32_t *samples0 = samples;
1345  uint8_t *tmp = s->md5_buffer;
1346 
1347  for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1348  int32_t v = samples0[i] >> 8;
1349  AV_WL24(tmp + 3*i, v);
1350  }
1351  buf = s->md5_buffer;
1352  }
1353  av_md5_update(s->md5ctx, buf, buf_size);
1354 
1355  return 0;
1356 }
1357 
1358 
1359 static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1360  const AVFrame *frame, int *got_packet_ptr)
1361 {
1363  int frame_bytes, out_bytes, ret;
1364 
1365  s = avctx->priv_data;
1366 
1367  /* when the last block is reached, update the header in extradata */
1368  if (!frame) {
1370  av_md5_final(s->md5ctx, s->md5sum);
1371  write_streaminfo(s, avctx->extradata);
1372 
1373 #if FF_API_SIDEDATA_ONLY_PKT
1375  if (avctx->side_data_only_packets && !s->flushed) {
1377 #else
1378  if (!s->flushed) {
1379 #endif
1381  avctx->extradata_size);
1382  if (!side_data)
1383  return AVERROR(ENOMEM);
1384  memcpy(side_data, avctx->extradata, avctx->extradata_size);
1385 
1386  avpkt->pts = s->next_pts;
1387 
1388  *got_packet_ptr = 1;
1389  s->flushed = 1;
1390  }
1391 
1392  return 0;
1393  }
1394 
1395  /* change max_framesize for small final frame */
1396  if (frame->nb_samples < s->frame.blocksize) {
1398  s->channels,
1399  avctx->bits_per_raw_sample);
1400  }
1401 
1402  init_frame(s, frame->nb_samples);
1403 
1404  copy_samples(s, frame->data[0]);
1405 
1407 
1408  remove_wasted_bits(s);
1409 
1410  frame_bytes = encode_frame(s);
1411 
1412  /* Fall back on verbatim mode if the compressed frame is larger than it
1413  would be if encoded uncompressed. */
1414  if (frame_bytes < 0 || frame_bytes > s->max_framesize) {
1415  s->frame.verbatim_only = 1;
1416  frame_bytes = encode_frame(s);
1417  if (frame_bytes < 0) {
1418  av_log(avctx, AV_LOG_ERROR, "Bad frame count\n");
1419  return frame_bytes;
1420  }
1421  }
1422 
1423  if ((ret = ff_alloc_packet2(avctx, avpkt, frame_bytes, 0)) < 0)
1424  return ret;
1425 
1426  out_bytes = write_frame(s, avpkt);
1427 
1428  s->frame_count++;
1429  s->sample_count += frame->nb_samples;
1430  if ((ret = update_md5_sum(s, frame->data[0])) < 0) {
1431  av_log(avctx, AV_LOG_ERROR, "Error updating MD5 checksum\n");
1432  return ret;
1433  }
1434  if (out_bytes > s->max_encoded_framesize)
1435  s->max_encoded_framesize = out_bytes;
1436  if (out_bytes < s->min_framesize)
1437  s->min_framesize = out_bytes;
1438 
1439  avpkt->pts = frame->pts;
1440  avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
1441  avpkt->size = out_bytes;
1442 
1443  s->next_pts = avpkt->pts + avpkt->duration;
1444 
1445  *got_packet_ptr = 1;
1446  return 0;
1447 }
1448 
1449 
1451 {
1452  if (avctx->priv_data) {
1453  FlacEncodeContext *s = avctx->priv_data;
1454  av_freep(&s->md5ctx);
1455  av_freep(&s->md5_buffer);
1456  ff_lpc_end(&s->lpc_ctx);
1457  }
1458  av_freep(&avctx->extradata);
1459  avctx->extradata_size = 0;
1460  return 0;
1461 }
1462 
1463 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1464 static const AVOption options[] = {
1465 { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
1466 { "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" },
1467 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1468 { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1469 { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1470 { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1471 { "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 },
1472 { "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1473 { "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1474 { "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" },
1475 { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
1476 { "2level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1477 { "4level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1478 { "8level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1479 { "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
1480 { "log", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
1481 { "ch_mode", "Stereo decorrelation mode", offsetof(FlacEncodeContext, options.ch_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FLAC_CHMODE_MID_SIDE, FLAGS, "ch_mode" },
1482 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1483 { "indep", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1484 { "left_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1485 { "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1486 { "mid_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1487 { "exact_rice_parameters", "Calculate rice parameters exactly", offsetof(FlacEncodeContext, options.exact_rice_parameters), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1488 { "multi_dim_quant", "Multi-dimensional quantization", offsetof(FlacEncodeContext, options.multi_dim_quant), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1489 { "min_prediction_order", NULL, offsetof(FlacEncodeContext, options.min_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS },
1490 { "max_prediction_order", NULL, offsetof(FlacEncodeContext, options.max_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS },
1491 
1492 { NULL },
1493 };
1494 
1495 static const AVClass flac_encoder_class = {
1496  .class_name = "FLAC encoder",
1497  .item_name = av_default_item_name,
1498  .option = options,
1499  .version = LIBAVUTIL_VERSION_INT,
1500 };
1501 
1503  .name = "flac",
1504  .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
1505  .type = AVMEDIA_TYPE_AUDIO,
1506  .id = AV_CODEC_ID_FLAC,
1507  .priv_data_size = sizeof(FlacEncodeContext),
1509  .encode2 = flac_encode_frame,
1510  .close = flac_encode_close,
1512  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
1515  .priv_class = &flac_encoder_class,
1516 };
#define MAX_FIXED_ORDER
Definition: flacenc.c:43
uint32_t rc_udata[FLAC_MAX_BLOCKSIZE]
Definition: flacenc.c:87
#define NULL
Definition: coverity.c:32
#define rice_encode_count(sum, n, k)
Definition: flacenc.c:601
const char const char void * val
Definition: avisynth_c.h:771
#define ff_ctz
Definition: intmath.h:106
#define ORDER_METHOD_SEARCH
Definition: lpc.h:34
const char * s
Definition: avisynth_c.h:768
static int shift(int a, int b)
Definition: sonic.c:82
int type
Definition: flacenc.c:78
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
Definition: bswapdsp.h:26
#define ORDER_METHOD_8LEVEL
Definition: lpc.h:33
AVCodec ff_flac_encoder
Definition: flacenc.c:1502
AVOption.
Definition: opt.h:246
int min_prediction_order
Definition: flacenc.c:61
Definition: lpc.h:52
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:240
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:207
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
struct AVMD5 * md5ctx
Definition: flacenc.c:121
#define MAX_LPC_ORDER
Definition: lpc.h:38
av_cold void ff_flacdsp_init(FLACDSPContext *c, enum AVSampleFormat fmt, int channels, int bps)
Definition: flacdsp.c:88
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
Calculate an estimate for the maximum frame size based on verbatim mode.
Definition: flac.c:148
channels
Definition: aptx.c:30
int size
Definition: avcodec.h:1431
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&HAVE_MMX) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
const char * b
Definition: vf_curves.c:113
int min_partition_order
Definition: flacenc.c:64
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
#define MAX_PARTITION_ORDER
Definition: flacenc.c:44
#define av_bswap16
Definition: bswap.h:31
#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:438
int64_t next_pts
Definition: flacenc.c:128
#define FLAC_MAX_BLOCKSIZE
Definition: flac.h:37
#define MAX_LPC_SHIFT
Definition: flacenc.c:48
#define AV_CH_LAYOUT_STEREO
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2741
static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: flacenc.c:1359
int max_partition_order
Definition: flacenc.c:65
AVCodec.
Definition: avcodec.h:3408
#define AV_CH_LAYOUT_5POINT0
void av_md5_update(AVMD5 *ctx, const uint8_t *src, int len)
Update hash value.
Definition: md5.c:154
static int select_blocksize(int samplerate, int block_time_ms)
Set blocksize based on samplerate.
Definition: flacenc.c:162
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#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: avcodec.h:984
FlacFrame frame
Definition: flacenc.c:117
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
#define AV_WL24(p, d)
Definition: intreadwrite.h:464
attribute_deprecated int side_data_only_packets
Encoding only and set by default.
Definition: avcodec.h:3013
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition: md5.c:48
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2181
uint8_t
#define ORDER_METHOD_LOG
Definition: lpc.h:35
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVOptions.
int order
Definition: flacenc.c:82
do not use LPC prediction or use all zero coefficients
Definition: lpc.h:45
int32_t coefs[MAX_LPC_ORDER]
Definition: flacenc.c:83
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1448
int wasted
Definition: flacenc.c:81
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:311
FLACDSPContext flac_dsp
Definition: flacenc.c:125
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1618
uint8_t * md5_buffer
Definition: flacenc.c:122
static AVFrame * frame
Public header for CRC hash function implementation.
const char data[16]
Definition: mxf.c:90
uint8_t * data
Definition: avcodec.h:1430
static uint64_t find_subframe_rice_params(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
Definition: flacenc.c:754
int params[MAX_PARTITIONS]
Definition: flacenc.c:74
static const uint8_t header[24]
Definition: sdr2.c:67
Definition: md5.c:40
uint64_t sample_count
Definition: flacenc.c:115
uint8_t crc8
Definition: flacenc.c:98
signed 32 bits
Definition: samplefmt.h:62
#define FLAC_MIN_BLOCKSIZE
Definition: flac.h:36
#define av_log(a,...)
static void write_subframes(FlacEncodeContext *s)
Definition: flacenc.c:1242
#define AV_CH_LAYOUT_5POINT1
int shift
Definition: flacenc.c:84
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define ORDER_METHOD_4LEVEL
Definition: lpc.h:32
unsigned int md5_buffer_size
Definition: flacenc.c:123
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
int exact_rice_parameters
Definition: flacenc.c:67
#define AVERROR(e)
Definition: error.h:43
uint64_t rc_sums[32][MAX_PARTITIONS]
Definition: flacenc.c:88
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int sr_code[2]
Definition: flacenc.c:108
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define FLAC_SUBFRAME_LPC
Definition: flacenc.c:41
uint8_t * buf
Definition: put_bits.h:38
enum CodingMode coding_mode
Definition: flacenc.c:72
simple assert() macros that are a bit more flexible than ISO C assert().
#define AV_CH_LAYOUT_QUAD
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
#define MIN_LPC_SHIFT
Definition: flacenc.c:47
#define COPY_SAMPLES(bits)
int porder
Definition: flacenc.c:73
#define FLAC_SUBFRAME_VERBATIM
Definition: flacenc.c:39
int32_t samples[FLAC_MAX_BLOCKSIZE]
Definition: flacenc.c:90
static void remove_wasted_bits(FlacEncodeContext *s)
Definition: flacenc.c:1077
#define FLAC_SUBFRAME_CONSTANT
Definition: flacenc.c:38
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2224
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
#define ORDER_METHOD_2LEVEL
Definition: lpc.h:31
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:635
static void frame_end(MpegEncContext *s)
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:488
uint64_t residual
Definition: dirac_vlc.h:29
#define AV_CH_LAYOUT_2_2
int type_code
Definition: flacenc.c:79
#define FLAC_SUBFRAME_FIXED
Definition: flacenc.c:40
static int encode_residual_ch(FlacEncodeContext *s, int ch)
Definition: flacenc.c:822
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:322
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
static uint64_t rice_count_exact(const int32_t *res, int n, int k)
Definition: flacenc.c:537
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: avcodec.h:989
#define FFMIN(a, b)
Definition: common.h:96
int obits
Definition: flacenc.c:80
static int encode_frame(FlacEncodeContext *s)
Definition: flacenc.c:1057
#define FLAGS
Definition: flacenc.c:1463
int32_t
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:392
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:34
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int n
Definition: avisynth_c.h:684
#define AV_CH_FRONT_CENTER
int prediction_order_method
Definition: flacenc.c:63
#define AV_CH_LAYOUT_5POINT1_BACK
static int get_max_p_order(int max_porder, int n, int order)
Definition: flacenc.c:745
static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
Definition: flacenc.c:1313
if(ret< 0)
Definition: vf_mcdeint.c:279
static int find_optimal_param_exact(uint64_t sums[32][MAX_PARTITIONS], int i, int max_param)
Definition: flacenc.c:618
Not part of ABI.
Definition: lpc.h:49
PutBitContext pb
Definition: flacenc.c:105
int lpc_coeff_precision
Definition: flacenc.c:60
#define av_log2
Definition: intmath.h:83
attribute_deprecated int max_prediction_order
Definition: avcodec.h:2471
void(* lpc16_encode)(int32_t *res, const int32_t *smp, int len, int order, const int32_t coefs[32], int shift)
Definition: flacdsp.h:33
static void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len)
write signed golomb rice code (flac).
Definition: golomb.h:579
static const AVOption options[]
Definition: flacenc.c:1464
static void channel_decorrelation(FlacEncodeContext *s)
Perform stereo channel decorrelation.
Definition: flacenc.c:1153
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2193
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: avcodec.h:1158
int bs_code[2]
Definition: flacenc.c:97
const int ff_flac_sample_rate_table[16]
Definition: flacdata.c:24
Libavcodec external API header.
static void calc_sum_next(int level, uint64_t sums[32][MAX_PARTITIONS], int kmax)
Definition: flacenc.c:695
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int compression_level
Definition: avcodec.h:1590
int sample_rate
samples per second
Definition: avcodec.h:2173
static void write_frame_header(FlacEncodeContext *s)
Definition: flacenc.c:1205
#define MIN_LPC_ORDER
Definition: lpc.h:37
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:665
main external API structure.
Definition: avcodec.h:1518
int ch_mode
Definition: flacenc.c:99
static int count_frame_header(FlacEncodeContext *s)
Definition: flacenc.c:1021
Levinson-Durbin recursion.
Definition: lpc.h:47
#define ORDER_METHOD_EST
Definition: lpc.h:30
void av_md5_init(AVMD5 *ctx)
Initialize MD5 hashing.
Definition: md5.c:143
void * buf
Definition: avisynth_c.h:690
int extradata_size
Definition: avcodec.h:1619
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
Describe the class of an AVClass context structure.
Definition: log.h:67
use the codec default LPC type
Definition: lpc.h:44
enum FFLPCType lpc_type
Definition: flacenc.c:58
int blocksize
Definition: flacenc.c:96
#define MAX_PARTITIONS
Definition: flacenc.c:45
#define AV_CH_LAYOUT_5POINT0_BACK
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:705
uint8_t md5sum[16]
Definition: flacenc.c:116
static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n, int order)
Definition: flacenc.c:771
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
Definition: md5.c:192
int max_encoded_framesize
Definition: flacenc.c:113
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:200
static void write_utf8(PutBitContext *pb, uint32_t val)
Definition: flacenc.c:1198
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition: lpc.c:300
#define MAX_LPC_PRECISION
Definition: flacenc.c:46
int max_prediction_order
Definition: flacenc.c:62
static void copy_samples(FlacEncodeContext *s, const void *samples)
Copy channel-interleaved input samples into separate subframes.
Definition: flacenc.c:515
int compression_level
Definition: flacenc.c:56
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
uint8_t level
Definition: svq3.c:207
RiceContext rc
Definition: flacenc.c:86
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
AVCodecContext * avctx
Definition: flacenc.c:119
static void write_frame_footer(FlacEncodeContext *s)
Definition: flacenc.c:1302
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
FlacSubframe subframes[FLAC_MAX_CHANNELS]
Definition: flacenc.c:95
const char const char * params
Definition: avisynth_c.h:775
CompressionOptions options
Definition: flacenc.c:118
FFLPCType
LPC analysis type.
Definition: lpc.h:43
Cholesky factorization.
Definition: lpc.h:48
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
static int estimate_stereo_mode(const int32_t *left_ch, const int32_t *right_ch, int n, int max_rice_param)
Definition: flacenc.c:1109
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
int32_t residual[FLAC_MAX_BLOCKSIZE+11]
Definition: flacenc.c:91
const int32_t ff_flac_blocksize_table[16]
Definition: flacdata.c:30
signed 16 bits
Definition: samplefmt.h:61
static double c[64]
void(* lpc32_encode)(int32_t *res, const int32_t *smp, int len, int order, const int32_t coefs[32], int shift)
Definition: flacdsp.h:35
#define AV_CODEC_CAP_LOSSLESS
Codec is lossless.
Definition: avcodec.h:1049
LPCContext lpc_ctx
Definition: flacenc.c:120
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
static av_cold int flac_encode_close(AVCodecContext *avctx)
Definition: flacenc.c:1450
static av_cold void dprint_compression_options(FlacEncodeContext *s)
Definition: flacenc.c:181
fixed LPC coefficients
Definition: lpc.h:46
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
void * priv_data
Definition: avcodec.h:1545
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:606
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
attribute_deprecated int min_prediction_order
Definition: avcodec.h:2467
int channels
number of audio channels
Definition: avcodec.h:2174
static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
Definition: flacenc.c:551
static const AVClass flac_encoder_class
Definition: flacenc.c:1495
BswapDSPContext bdsp
Definition: flacenc.c:124
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
CodingMode
Definition: flacenc.c:50
Public header for MD5 hash function implementation.
#define av_freep(p)
static void init_frame(FlacEncodeContext *s, int nb_samples)
Definition: flacenc.c:470
void INT64 INT64 count
Definition: avisynth_c.h:690
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition: internal.h:280
#define HAVE_BIGENDIAN
Definition: config.h:196
static int update_md5_sum(FlacEncodeContext *s, const void *samples)
Definition: flacenc.c:1323
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:329
exp golomb vlc stuff
This structure stores compressed data.
Definition: avcodec.h:1407
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:284
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1423
static av_cold int flac_encode_init(AVCodecContext *avctx)
Definition: flacenc.c:239
static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
Write streaminfo metadata block to byte array.
Definition: flacenc.c:135
#define FLAC_MAX_CHANNELS
Definition: flac.h:35
#define av_unused
Definition: attributes.h:125
int verbatim_only
Definition: flacenc.c:100
uint32_t frame_count
Definition: flacenc.c:114
static uint8_t tmp[11]
Definition: aes_ctr.c:26
bitstream writer API