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