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