FFmpeg
flacenc.c
Go to the documentation of this file.
1 /*
2  * FLAC audio encoder
3  * Copyright (c) 2006 Justin Ruggles <justin.ruggles@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/avassert.h"
24 #include "libavutil/crc.h"
25 #include "libavutil/intmath.h"
26 #include "libavutil/md5.h"
27 #include "libavutil/opt.h"
28 
29 #include "avcodec.h"
30 #include "bswapdsp.h"
31 #include "codec_internal.h"
32 #include "encode.h"
33 #include "put_bits.h"
34 #include "put_golomb.h"
35 #include "lpc.h"
36 #include "flac.h"
37 #include "flacdata.h"
38 #include "flacencdsp.h"
39 
40 #define FLAC_SUBFRAME_CONSTANT 0
41 #define FLAC_SUBFRAME_VERBATIM 1
42 #define FLAC_SUBFRAME_FIXED 8
43 #define FLAC_SUBFRAME_LPC 32
44 
45 #define MAX_FIXED_ORDER 4
46 #define MAX_PARTITION_ORDER 8
47 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
48 #define MAX_LPC_PRECISION 15
49 #define MIN_LPC_SHIFT 0
50 #define MAX_LPC_SHIFT 15
51 
52 enum CodingMode {
55 };
56 
57 typedef struct CompressionOptions {
68  int ch_mode;
72 
73 typedef struct RiceContext {
75  int porder;
77 } RiceContext;
78 
79 typedef struct FlacSubframe {
80  int type;
81  int type_code;
82  int obits;
83  int wasted;
84  int order;
86  int shift;
87 
90  uint64_t rc_sums[32][MAX_PARTITIONS];
91 
94 } FlacSubframe;
95 
96 typedef struct FlacFrame {
98  int blocksize;
99  int bs_code[2];
100  uint8_t crc8;
101  int ch_mode;
103 } FlacFrame;
104 
105 typedef struct FlacEncodeContext {
106  AVClass *class;
108  int channels;
110  int sr_code[2];
111  int bps_code;
116  uint32_t frame_count;
117  uint64_t sample_count;
118  uint8_t md5sum[16];
123  struct AVMD5 *md5ctx;
124  uint8_t *md5_buffer;
125  unsigned int md5_buffer_size;
128 
129  int flushed;
130  int64_t next_pts;
132 
133 
134 /**
135  * Write streaminfo metadata block to byte array.
136  */
138 {
139  PutBitContext pb;
140 
141  memset(header, 0, FLAC_STREAMINFO_SIZE);
143 
144  /* streaminfo metadata block */
145  put_bits(&pb, 16, s->max_blocksize);
146  put_bits(&pb, 16, s->max_blocksize);
147  put_bits(&pb, 24, s->min_framesize);
148  put_bits(&pb, 24, s->max_framesize);
149  put_bits(&pb, 20, s->samplerate);
150  put_bits(&pb, 3, s->channels-1);
151  put_bits(&pb, 5, s->avctx->bits_per_raw_sample - 1);
152  /* write 36-bit sample count in 2 put_bits() calls */
153  put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
154  put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
155  flush_put_bits(&pb);
156  memcpy(&header[18], s->md5sum, 16);
157 }
158 
159 
160 /**
161  * Calculate an estimate for the maximum frame size based on verbatim mode.
162  * @param blocksize block size, in samples
163  * @param ch number of channels
164  * @param bps bits-per-sample
165  */
166 static int flac_get_max_frame_size(int blocksize, int ch, int bps)
167 {
168  /* Technically, there is no limit to FLAC frame size, but an encoder
169  should not write a frame that is larger than if verbatim encoding mode
170  were to be used. */
171 
172  int count;
173 
174  count = 16; /* frame header */
175  count += ch * ((7+bps+7)/8); /* subframe headers */
176  if (ch == 2) {
177  /* for stereo, need to account for using decorrelation */
178  count += (( 2*bps+1) * blocksize + 7) / 8;
179  } else {
180  count += ( ch*bps * blocksize + 7) / 8;
181  }
182  count += 2; /* frame footer */
183 
184  return count;
185 }
186 
187 
188 /**
189  * Set blocksize based on samplerate.
190  * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
191  */
192 static int select_blocksize(int samplerate, int block_time_ms)
193 {
194  int i;
195  int target;
196  int blocksize;
197 
198  av_assert0(samplerate > 0);
199  blocksize = ff_flac_blocksize_table[1];
200  target = (samplerate * block_time_ms) / 1000;
201  for (i = 0; i < 16; i++) {
202  if (target >= ff_flac_blocksize_table[i] &&
203  ff_flac_blocksize_table[i] > blocksize) {
204  blocksize = ff_flac_blocksize_table[i];
205  }
206  }
207  return blocksize;
208 }
209 
210 
212 {
213  AVCodecContext *avctx = s->avctx;
214  CompressionOptions *opt = &s->options;
215 
216  av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);
217 
218  switch (opt->lpc_type) {
219  case FF_LPC_TYPE_NONE:
220  av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
221  break;
222  case FF_LPC_TYPE_FIXED:
223  av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
224  break;
226  av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
227  break;
229  av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
230  opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
231  break;
232  }
233 
234  av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
236 
237  switch (opt->prediction_order_method) {
238  case ORDER_METHOD_EST:
239  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
240  break;
241  case ORDER_METHOD_2LEVEL:
242  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
243  break;
244  case ORDER_METHOD_4LEVEL:
245  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
246  break;
247  case ORDER_METHOD_8LEVEL:
248  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
249  break;
250  case ORDER_METHOD_SEARCH:
251  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
252  break;
253  case ORDER_METHOD_LOG:
254  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
255  break;
256  }
257 
258 
259  av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
261 
262  av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
263 
264  av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
265  opt->lpc_coeff_precision);
266 }
267 
268 
270 {
271  int freq = avctx->sample_rate;
272  int channels = avctx->ch_layout.nb_channels;
273  FlacEncodeContext *s = avctx->priv_data;
274  int i, level, ret;
275  uint8_t *streaminfo;
276 
277  s->avctx = avctx;
278 
279  switch (avctx->sample_fmt) {
280  case AV_SAMPLE_FMT_S16:
281  avctx->bits_per_raw_sample = 16;
282  s->bps_code = 4;
283  break;
284  case AV_SAMPLE_FMT_S32:
285  if (avctx->bits_per_raw_sample != 24)
286  av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
287  avctx->bits_per_raw_sample = 24;
288  s->bps_code = 6;
289  break;
290  }
291 
293  av_log(avctx, AV_LOG_ERROR, "%d channels not supported (max %d)\n",
295  return AVERROR(EINVAL);
296  }
297  s->channels = channels;
298 
299  /* find samplerate in table */
300  if (freq < 1)
301  return AVERROR(EINVAL);
302  for (i = 1; i < 12; i++) {
303  if (freq == ff_flac_sample_rate_table[i]) {
304  s->samplerate = ff_flac_sample_rate_table[i];
305  s->sr_code[0] = i;
306  s->sr_code[1] = 0;
307  break;
308  }
309  }
310  /* if not in table, samplerate is non-standard */
311  if (i == 12) {
312  if (freq % 1000 == 0 && freq < 255000) {
313  s->sr_code[0] = 12;
314  s->sr_code[1] = freq / 1000;
315  } else if (freq % 10 == 0 && freq < 655350) {
316  s->sr_code[0] = 14;
317  s->sr_code[1] = freq / 10;
318  } else if (freq < 65535) {
319  s->sr_code[0] = 13;
320  s->sr_code[1] = freq;
321  } else if (freq < 1048576) {
322  s->sr_code[0] = 0;
323  s->sr_code[1] = 0;
324  } else {
325  av_log(avctx, AV_LOG_ERROR, "%d Hz not supported\n", freq);
326  return AVERROR(EINVAL);
327  }
328  s->samplerate = freq;
329  }
330 
331  /* set compression option defaults based on avctx->compression_level */
332  if (avctx->compression_level < 0)
333  s->options.compression_level = 5;
334  else
335  s->options.compression_level = avctx->compression_level;
336 
337  level = s->options.compression_level;
338  if (level > 12) {
339  av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
340  s->options.compression_level);
341  return AVERROR(EINVAL);
342  }
343 
344  s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
345 
346  if (s->options.lpc_type == FF_LPC_TYPE_DEFAULT)
347  s->options.lpc_type = ((int[]){ FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED,
352 
353  if (s->options.min_prediction_order < 0)
354  s->options.min_prediction_order = ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
355  if (s->options.max_prediction_order < 0)
356  s->options.max_prediction_order = ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
357 
358  if (s->options.prediction_order_method < 0)
359  s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
364 
365  if (s->options.min_partition_order > s->options.max_partition_order) {
366  av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
367  s->options.min_partition_order, s->options.max_partition_order);
368  return AVERROR(EINVAL);
369  }
370  if (s->options.min_partition_order < 0)
371  s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
372  if (s->options.max_partition_order < 0)
373  s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
374 
375  if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
376  s->options.min_prediction_order = 0;
377  s->options.max_prediction_order = 0;
378  } else if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
379  if (s->options.min_prediction_order > MAX_FIXED_ORDER) {
380  av_log(avctx, AV_LOG_WARNING,
381  "invalid min prediction order %d, clamped to %d\n",
382  s->options.min_prediction_order, MAX_FIXED_ORDER);
383  s->options.min_prediction_order = MAX_FIXED_ORDER;
384  }
385  if (s->options.max_prediction_order > MAX_FIXED_ORDER) {
386  av_log(avctx, AV_LOG_WARNING,
387  "invalid max prediction order %d, clamped to %d\n",
388  s->options.max_prediction_order, MAX_FIXED_ORDER);
389  s->options.max_prediction_order = MAX_FIXED_ORDER;
390  }
391  }
392 
393  if (s->options.max_prediction_order < s->options.min_prediction_order) {
394  av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
395  s->options.min_prediction_order, s->options.max_prediction_order);
396  return AVERROR(EINVAL);
397  }
398 
399  if (avctx->frame_size > 0) {
400  if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
401  avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
402  av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
403  avctx->frame_size);
404  return AVERROR(EINVAL);
405  }
406  } else {
407  s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
408  }
409  s->max_blocksize = s->avctx->frame_size;
410 
411  /* set maximum encoded frame size in verbatim mode */
412  s->max_framesize = flac_get_max_frame_size(s->avctx->frame_size,
413  s->channels,
414  s->avctx->bits_per_raw_sample);
415 
416  /* initialize MD5 context */
417  s->md5ctx = av_md5_alloc();
418  if (!s->md5ctx)
419  return AVERROR(ENOMEM);
420  av_md5_init(s->md5ctx);
421 
422  streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
423  if (!streaminfo)
424  return AVERROR(ENOMEM);
425  write_streaminfo(s, streaminfo);
426  avctx->extradata = streaminfo;
428 
429  s->frame_count = 0;
430  s->min_framesize = s->max_framesize;
431 
432  if ((channels == 3 &&
434  (channels == 4 &&
437  (channels == 5 &&
440  (channels == 6 &&
443  if (avctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
444  av_log(avctx, AV_LOG_ERROR, "Channel layout not supported by Flac, "
445  "output stream will have incorrect "
446  "channel layout.\n");
447  } else {
448  av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
449  "will use Flac channel layout for "
450  "%d channels.\n", channels);
451  }
452  }
453 
454  ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
455  s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
456 
457  ff_bswapdsp_init(&s->bdsp);
458  ff_flacencdsp_init(&s->flac_dsp);
459 
461 
462  return ret;
463 }
464 
465 
466 static void init_frame(FlacEncodeContext *s, int nb_samples)
467 {
468  int i, ch;
469  FlacFrame *frame;
470 
471  frame = &s->frame;
472 
473  for (i = 0; i < 16; i++) {
474  if (nb_samples == ff_flac_blocksize_table[i]) {
475  frame->blocksize = ff_flac_blocksize_table[i];
476  frame->bs_code[0] = i;
477  frame->bs_code[1] = 0;
478  break;
479  }
480  }
481  if (i == 16) {
482  frame->blocksize = nb_samples;
483  if (frame->blocksize <= 256) {
484  frame->bs_code[0] = 6;
485  frame->bs_code[1] = frame->blocksize-1;
486  } else {
487  frame->bs_code[0] = 7;
488  frame->bs_code[1] = frame->blocksize-1;
489  }
490  }
491 
492  for (ch = 0; ch < s->channels; ch++) {
493  FlacSubframe *sub = &frame->subframes[ch];
494 
495  sub->wasted = 0;
496  sub->obits = s->avctx->bits_per_raw_sample;
497 
498  if (sub->obits > 16)
499  sub->rc.coding_mode = CODING_MODE_RICE2;
500  else
501  sub->rc.coding_mode = CODING_MODE_RICE;
502  }
503 
504  frame->verbatim_only = 0;
505 }
506 
507 
508 /**
509  * Copy channel-interleaved input samples into separate subframes.
510  */
511 static void copy_samples(FlacEncodeContext *s, const void *samples)
512 {
513  int i, j, ch;
514  FlacFrame *frame;
515  int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
516  s->avctx->bits_per_raw_sample;
517 
518 #define COPY_SAMPLES(bits) do { \
519  const int ## bits ## _t *samples0 = samples; \
520  frame = &s->frame; \
521  for (i = 0, j = 0; i < frame->blocksize; i++) \
522  for (ch = 0; ch < s->channels; ch++, j++) \
523  frame->subframes[ch].samples[i] = samples0[j] >> shift; \
524 } while (0)
525 
526  if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S16)
527  COPY_SAMPLES(16);
528  else
529  COPY_SAMPLES(32);
530 }
531 
532 
533 static uint64_t rice_count_exact(const int32_t *res, int n, int k)
534 {
535  int i;
536  uint64_t count = 0;
537 
538  for (i = 0; i < n; i++) {
539  int32_t v = -2 * res[i] - 1;
540  v ^= v >> 31;
541  count += (v >> k) + 1 + k;
542  }
543  return count;
544 }
545 
546 
548  int pred_order)
549 {
550  int p, porder, psize;
551  int i, part_end;
552  uint64_t count = 0;
553 
554  /* subframe header */
555  count += 8;
556 
557  if (sub->wasted)
558  count += sub->wasted;
559 
560  /* subframe */
561  if (sub->type == FLAC_SUBFRAME_CONSTANT) {
562  count += sub->obits;
563  } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
564  count += s->frame.blocksize * sub->obits;
565  } else {
566  /* warm-up samples */
567  count += pred_order * sub->obits;
568 
569  /* LPC coefficients */
570  if (sub->type == FLAC_SUBFRAME_LPC)
571  count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
572 
573  /* rice-encoded block */
574  count += 2;
575 
576  /* partition order */
577  porder = sub->rc.porder;
578  psize = s->frame.blocksize >> porder;
579  count += 4;
580 
581  /* residual */
582  i = pred_order;
583  part_end = psize;
584  for (p = 0; p < 1 << porder; p++) {
585  int k = sub->rc.params[p];
586  count += sub->rc.coding_mode;
587  count += rice_count_exact(&sub->residual[i], part_end - i, k);
588  i = part_end;
589  part_end = FFMIN(s->frame.blocksize, part_end + psize);
590  }
591  }
592 
593  return count;
594 }
595 
596 
597 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
598 
599 /**
600  * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
601  */
602 static int find_optimal_param(uint64_t sum, int n, int max_param)
603 {
604  int k;
605  uint64_t sum2;
606 
607  if (sum <= n >> 1)
608  return 0;
609  sum2 = sum - (n >> 1);
610  k = av_log2(av_clipl_int32(sum2 / n));
611  return FFMIN(k, max_param);
612 }
613 
614 static int find_optimal_param_exact(uint64_t sums[32][MAX_PARTITIONS], int i, int max_param)
615 {
616  int bestk = 0;
617  int64_t bestbits = INT64_MAX;
618  int k;
619 
620  for (k = 0; k <= max_param; k++) {
621  int64_t bits = sums[k][i];
622  if (bits < bestbits) {
623  bestbits = bits;
624  bestk = k;
625  }
626  }
627 
628  return bestk;
629 }
630 
631 static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder,
632  uint64_t sums[32][MAX_PARTITIONS],
633  int n, int pred_order, int max_param, int exact)
634 {
635  int i;
636  int k, cnt, part;
637  uint64_t all_bits;
638 
639  part = (1 << porder);
640  all_bits = 4 * part;
641 
642  cnt = (n >> porder) - pred_order;
643  for (i = 0; i < part; i++) {
644  if (exact) {
645  k = find_optimal_param_exact(sums, i, max_param);
646  all_bits += sums[k][i];
647  } else {
648  k = find_optimal_param(sums[0][i], cnt, max_param);
649  all_bits += rice_encode_count(sums[0][i], cnt, k);
650  }
651  rc->params[i] = k;
652  cnt = n >> porder;
653  }
654 
655  rc->porder = porder;
656 
657  return all_bits;
658 }
659 
660 
661 static void calc_sum_top(int pmax, int kmax, const uint32_t *data, int n, int pred_order,
662  uint64_t sums[32][MAX_PARTITIONS])
663 {
664  int i, k;
665  int parts;
666  const uint32_t *res, *res_end;
667 
668  /* sums for highest level */
669  parts = (1 << pmax);
670 
671  for (k = 0; k <= kmax; k++) {
672  res = &data[pred_order];
673  res_end = &data[n >> pmax];
674  for (i = 0; i < parts; i++) {
675  if (kmax) {
676  uint64_t sum = (1LL + k) * (res_end - res);
677  while (res < res_end)
678  sum += *(res++) >> k;
679  sums[k][i] = sum;
680  } else {
681  uint64_t sum = 0;
682  while (res < res_end)
683  sum += *(res++);
684  sums[k][i] = sum;
685  }
686  res_end += n >> pmax;
687  }
688  }
689 }
690 
691 static void calc_sum_next(int level, uint64_t sums[32][MAX_PARTITIONS], int kmax)
692 {
693  int i, k;
694  int parts = (1 << level);
695  for (i = 0; i < parts; i++) {
696  for (k=0; k<=kmax; k++)
697  sums[k][i] = sums[k][2*i] + sums[k][2*i+1];
698  }
699 }
700 
701 static uint64_t calc_rice_params(RiceContext *rc,
702  uint32_t udata[FLAC_MAX_BLOCKSIZE],
703  uint64_t sums[32][MAX_PARTITIONS],
704  int pmin, int pmax,
705  const int32_t *data, int n, int pred_order, int exact)
706 {
707  int i;
708  uint64_t bits[MAX_PARTITION_ORDER+1];
709  int opt_porder;
710  RiceContext tmp_rc;
711  int kmax = (1 << rc->coding_mode) - 2;
712 
713  av_assert1(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
714  av_assert1(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
715  av_assert1(pmin <= pmax);
716 
717  tmp_rc.coding_mode = rc->coding_mode;
718 
719  for (i = 0; i < n; i++)
720  udata[i] = (2 * data[i]) ^ (data[i] >> 31);
721 
722  calc_sum_top(pmax, exact ? kmax : 0, udata, n, pred_order, sums);
723 
724  opt_porder = pmin;
725  bits[pmin] = UINT32_MAX;
726  for (i = pmax; ; ) {
727  bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums, n, pred_order, kmax, exact);
728  if (bits[i] < bits[opt_porder] || pmax == pmin) {
729  opt_porder = i;
730  *rc = tmp_rc;
731  }
732  if (i == pmin)
733  break;
734  calc_sum_next(--i, sums, exact ? kmax : 0);
735  }
736 
737  return bits[opt_porder];
738 }
739 
740 
741 static int get_max_p_order(int max_porder, int n, int order)
742 {
743  int porder = FFMIN(max_porder, av_log2(n^(n-1)));
744  if (order > 0)
745  porder = FFMIN(porder, av_log2(n/order));
746  return porder;
747 }
748 
749 
751  FlacSubframe *sub, int pred_order)
752 {
753  int pmin = get_max_p_order(s->options.min_partition_order,
754  s->frame.blocksize, pred_order);
755  int pmax = get_max_p_order(s->options.max_partition_order,
756  s->frame.blocksize, pred_order);
757 
758  uint64_t bits = 8 + pred_order * sub->obits + 2 + sub->rc.coding_mode;
759  if (sub->type == FLAC_SUBFRAME_LPC)
760  bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
761  bits += calc_rice_params(&sub->rc, sub->rc_udata, sub->rc_sums, pmin, pmax, sub->residual,
762  s->frame.blocksize, pred_order, s->options.exact_rice_parameters);
763  return bits;
764 }
765 
766 
767 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
768  int order)
769 {
770  int i;
771 
772  for (i = 0; i < order; i++)
773  res[i] = smp[i];
774 
775  if (order == 0) {
776  for (i = order; i < n; i++)
777  res[i] = smp[i];
778  } else if (order == 1) {
779  for (i = order; i < n; i++)
780  res[i] = smp[i] - smp[i-1];
781  } else if (order == 2) {
782  int a = smp[order-1] - smp[order-2];
783  for (i = order; i < n; i += 2) {
784  int b = smp[i ] - smp[i-1];
785  res[i] = b - a;
786  a = smp[i+1] - smp[i ];
787  res[i+1] = a - b;
788  }
789  } else if (order == 3) {
790  int a = smp[order-1] - smp[order-2];
791  int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
792  for (i = order; i < n; i += 2) {
793  int b = smp[i ] - smp[i-1];
794  int d = b - a;
795  res[i] = d - c;
796  a = smp[i+1] - smp[i ];
797  c = a - b;
798  res[i+1] = c - d;
799  }
800  } else {
801  int a = smp[order-1] - smp[order-2];
802  int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
803  int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
804  for (i = order; i < n; i += 2) {
805  int b = smp[i ] - smp[i-1];
806  int d = b - a;
807  int f = d - c;
808  res[i ] = f - e;
809  a = smp[i+1] - smp[i ];
810  c = a - b;
811  e = c - d;
812  res[i+1] = e - f;
813  }
814  }
815 }
816 
817 
819 {
820  int i, n;
821  int min_order, max_order, opt_order, omethod;
822  FlacFrame *frame;
823  FlacSubframe *sub;
825  int shift[MAX_LPC_ORDER];
826  int32_t *res, *smp;
827 
828  frame = &s->frame;
829  sub = &frame->subframes[ch];
830  res = sub->residual;
831  smp = sub->samples;
832  n = frame->blocksize;
833 
834  /* CONSTANT */
835  for (i = 1; i < n; i++)
836  if(smp[i] != smp[0])
837  break;
838  if (i == n) {
839  sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
840  res[0] = smp[0];
841  return subframe_count_exact(s, sub, 0);
842  }
843 
844  /* VERBATIM */
845  if (frame->verbatim_only || n < 5) {
846  sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
847  memcpy(res, smp, n * sizeof(int32_t));
848  return subframe_count_exact(s, sub, 0);
849  }
850 
851  min_order = s->options.min_prediction_order;
852  max_order = s->options.max_prediction_order;
853  omethod = s->options.prediction_order_method;
854 
855  /* FIXED */
856  sub->type = FLAC_SUBFRAME_FIXED;
857  if (s->options.lpc_type == FF_LPC_TYPE_NONE ||
858  s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
859  uint64_t bits[MAX_FIXED_ORDER+1];
860  if (max_order > MAX_FIXED_ORDER)
861  max_order = MAX_FIXED_ORDER;
862  opt_order = 0;
863  bits[0] = UINT32_MAX;
864  for (i = min_order; i <= max_order; i++) {
865  encode_residual_fixed(res, smp, n, i);
867  if (bits[i] < bits[opt_order])
868  opt_order = i;
869  }
870  sub->order = opt_order;
871  sub->type_code = sub->type | sub->order;
872  if (sub->order != max_order) {
873  encode_residual_fixed(res, smp, n, sub->order);
875  }
876  return subframe_count_exact(s, sub, sub->order);
877  }
878 
879  /* LPC */
880  sub->type = FLAC_SUBFRAME_LPC;
881  opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
882  s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
883  s->options.lpc_passes, omethod,
885 
886  if (omethod == ORDER_METHOD_2LEVEL ||
887  omethod == ORDER_METHOD_4LEVEL ||
888  omethod == ORDER_METHOD_8LEVEL) {
889  int levels = 1 << omethod;
890  uint64_t bits[1 << ORDER_METHOD_8LEVEL];
891  int order = -1;
892  int opt_index = levels-1;
893  opt_order = max_order-1;
894  bits[opt_index] = UINT32_MAX;
895  for (i = levels-1; i >= 0; i--) {
896  int last_order = order;
897  order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
898  order = av_clip(order, min_order - 1, max_order - 1);
899  if (order == last_order)
900  continue;
901  if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(order) <= 32) {
902  s->flac_dsp.lpc16_encode(res, smp, n, order+1, coefs[order],
903  shift[order]);
904  } else {
905  s->flac_dsp.lpc32_encode(res, smp, n, order+1, coefs[order],
906  shift[order]);
907  }
908  bits[i] = find_subframe_rice_params(s, sub, order+1);
909  if (bits[i] < bits[opt_index]) {
910  opt_index = i;
911  opt_order = order;
912  }
913  }
914  opt_order++;
915  } else if (omethod == ORDER_METHOD_SEARCH) {
916  // brute-force optimal order search
917  uint64_t bits[MAX_LPC_ORDER];
918  opt_order = 0;
919  bits[0] = UINT32_MAX;
920  for (i = min_order-1; i < max_order; i++) {
921  if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(i) <= 32) {
922  s->flac_dsp.lpc16_encode(res, smp, n, i+1, coefs[i], shift[i]);
923  } else {
924  s->flac_dsp.lpc32_encode(res, smp, n, i+1, coefs[i], shift[i]);
925  }
927  if (bits[i] < bits[opt_order])
928  opt_order = i;
929  }
930  opt_order++;
931  } else if (omethod == ORDER_METHOD_LOG) {
932  uint64_t bits[MAX_LPC_ORDER];
933  int step;
934 
935  opt_order = min_order - 1 + (max_order-min_order)/3;
936  memset(bits, -1, sizeof(bits));
937 
938  for (step = 16; step; step >>= 1) {
939  int last = opt_order;
940  for (i = last-step; i <= last+step; i += step) {
941  if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
942  continue;
943  if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(i) <= 32) {
944  s->flac_dsp.lpc32_encode(res, smp, n, i+1, coefs[i], shift[i]);
945  } else {
946  s->flac_dsp.lpc16_encode(res, smp, n, i+1, coefs[i], shift[i]);
947  }
949  if (bits[i] < bits[opt_order])
950  opt_order = i;
951  }
952  }
953  opt_order++;
954  }
955 
956  if (s->options.multi_dim_quant) {
957  int allsteps = 1;
958  int i, step, improved;
959  int64_t best_score = INT64_MAX;
960  int32_t qmax;
961 
962  qmax = (1 << (s->options.lpc_coeff_precision - 1)) - 1;
963 
964  for (i=0; i<opt_order; i++)
965  allsteps *= 3;
966 
967  do {
968  improved = 0;
969  for (step = 0; step < allsteps; step++) {
970  int tmp = step;
971  int32_t lpc_try[MAX_LPC_ORDER];
972  int64_t score = 0;
973  int diffsum = 0;
974 
975  for (i=0; i<opt_order; i++) {
976  int diff = ((tmp + 1) % 3) - 1;
977  lpc_try[i] = av_clip(coefs[opt_order - 1][i] + diff, -qmax, qmax);
978  tmp /= 3;
979  diffsum += !!diff;
980  }
981  if (diffsum >8)
982  continue;
983 
984  if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(opt_order - 1) <= 32) {
985  s->flac_dsp.lpc16_encode(res, smp, n, opt_order, lpc_try, shift[opt_order-1]);
986  } else {
987  s->flac_dsp.lpc32_encode(res, smp, n, opt_order, lpc_try, shift[opt_order-1]);
988  }
989  score = find_subframe_rice_params(s, sub, opt_order);
990  if (score < best_score) {
991  best_score = score;
992  memcpy(coefs[opt_order-1], lpc_try, sizeof(*coefs));
993  improved=1;
994  }
995  }
996  } while(improved);
997  }
998 
999  sub->order = opt_order;
1000  sub->type_code = sub->type | (sub->order-1);
1001  sub->shift = shift[sub->order-1];
1002  for (i = 0; i < sub->order; i++)
1003  sub->coefs[i] = coefs[sub->order-1][i];
1004 
1005  if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(opt_order) <= 32) {
1006  s->flac_dsp.lpc16_encode(res, smp, n, sub->order, sub->coefs, sub->shift);
1007  } else {
1008  s->flac_dsp.lpc32_encode(res, smp, n, sub->order, sub->coefs, sub->shift);
1009  }
1010 
1011  find_subframe_rice_params(s, sub, sub->order);
1012 
1013  return subframe_count_exact(s, sub, sub->order);
1014 }
1015 
1016 
1018 {
1019  uint8_t av_unused tmp;
1020  int count;
1021 
1022  /*
1023  <14> Sync code
1024  <1> Reserved
1025  <1> Blocking strategy
1026  <4> Block size in inter-channel samples
1027  <4> Sample rate
1028  <4> Channel assignment
1029  <3> Sample size in bits
1030  <1> Reserved
1031  */
1032  count = 32;
1033 
1034  /* coded frame number */
1035  PUT_UTF8(s->frame_count, tmp, count += 8;)
1036 
1037  /* explicit block size */
1038  if (s->frame.bs_code[0] == 6)
1039  count += 8;
1040  else if (s->frame.bs_code[0] == 7)
1041  count += 16;
1042 
1043  /* explicit sample rate */
1044  count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12) * 2) * 8;
1045 
1046  /* frame header CRC-8 */
1047  count += 8;
1048 
1049  return count;
1050 }
1051 
1052 
1054 {
1055  int ch;
1056  uint64_t count;
1057 
1058  count = count_frame_header(s);
1059 
1060  for (ch = 0; ch < s->channels; ch++)
1061  count += encode_residual_ch(s, ch);
1062 
1063  count += (8 - (count & 7)) & 7; // byte alignment
1064  count += 16; // CRC-16
1065 
1066  count >>= 3;
1067  if (count > INT_MAX)
1068  return AVERROR_BUG;
1069  return count;
1070 }
1071 
1072 
1074 {
1075  int ch, i;
1076 
1077  for (ch = 0; ch < s->channels; ch++) {
1078  FlacSubframe *sub = &s->frame.subframes[ch];
1079  int32_t v = 0;
1080 
1081  for (i = 0; i < s->frame.blocksize; i++) {
1082  v |= sub->samples[i];
1083  if (v & 1)
1084  break;
1085  }
1086 
1087  if (v && !(v & 1)) {
1088  v = ff_ctz(v);
1089 
1090  for (i = 0; i < s->frame.blocksize; i++)
1091  sub->samples[i] >>= v;
1092 
1093  sub->wasted = v;
1094  sub->obits -= v;
1095 
1096  /* for 24-bit, check if removing wasted bits makes the range better
1097  suited for using RICE instead of RICE2 for entropy coding */
1098  if (sub->obits <= 17)
1099  sub->rc.coding_mode = CODING_MODE_RICE;
1100  }
1101  }
1102 }
1103 
1104 
1105 static int estimate_stereo_mode(const int32_t *left_ch, const int32_t *right_ch, int n,
1106  int max_rice_param)
1107 {
1108  int i, best;
1109  int32_t lt, rt;
1110  uint64_t sum[4];
1111  uint64_t score[4];
1112  int k;
1113 
1114  /* calculate sum of 2nd order residual for each channel */
1115  sum[0] = sum[1] = sum[2] = sum[3] = 0;
1116  for (i = 2; i < n; i++) {
1117  lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
1118  rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
1119  sum[2] += FFABS((lt + rt) >> 1);
1120  sum[3] += FFABS(lt - rt);
1121  sum[0] += FFABS(lt);
1122  sum[1] += FFABS(rt);
1123  }
1124  /* estimate bit counts */
1125  for (i = 0; i < 4; i++) {
1126  k = find_optimal_param(2 * sum[i], n, max_rice_param);
1127  sum[i] = rice_encode_count( 2 * sum[i], n, k);
1128  }
1129 
1130  /* calculate score for each mode */
1131  score[0] = sum[0] + sum[1];
1132  score[1] = sum[0] + sum[3];
1133  score[2] = sum[1] + sum[3];
1134  score[3] = sum[2] + sum[3];
1135 
1136  /* return mode with lowest score */
1137  best = 0;
1138  for (i = 1; i < 4; i++)
1139  if (score[i] < score[best])
1140  best = i;
1141 
1142  return best;
1143 }
1144 
1145 
1146 /**
1147  * Perform stereo channel decorrelation.
1148  */
1150 {
1151  FlacFrame *frame;
1152  int32_t *left, *right;
1153  int i, n;
1154 
1155  frame = &s->frame;
1156  n = frame->blocksize;
1157  left = frame->subframes[0].samples;
1158  right = frame->subframes[1].samples;
1159 
1160  if (s->channels != 2) {
1161  frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
1162  return;
1163  }
1164 
1165  if (s->options.ch_mode < 0) {
1166  int max_rice_param = (1 << frame->subframes[0].rc.coding_mode) - 2;
1167  frame->ch_mode = estimate_stereo_mode(left, right, n, max_rice_param);
1168  } else
1169  frame->ch_mode = s->options.ch_mode;
1170 
1171  /* perform decorrelation and adjust bits-per-sample */
1172  if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1173  return;
1174  if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1175  int32_t tmp;
1176  for (i = 0; i < n; i++) {
1177  tmp = left[i];
1178  left[i] = (tmp + right[i]) >> 1;
1179  right[i] = tmp - right[i];
1180  }
1181  frame->subframes[1].obits++;
1182  } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1183  for (i = 0; i < n; i++)
1184  right[i] = left[i] - right[i];
1185  frame->subframes[1].obits++;
1186  } else {
1187  for (i = 0; i < n; i++)
1188  left[i] -= right[i];
1189  frame->subframes[0].obits++;
1190  }
1191 }
1192 
1193 
1194 static void write_utf8(PutBitContext *pb, uint32_t val)
1195 {
1196  uint8_t tmp;
1197  PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1198 }
1199 
1200 
1202 {
1203  FlacFrame *frame;
1204  int crc;
1205 
1206  frame = &s->frame;
1207 
1208  put_bits(&s->pb, 16, 0xFFF8);
1209  put_bits(&s->pb, 4, frame->bs_code[0]);
1210  put_bits(&s->pb, 4, s->sr_code[0]);
1211 
1212  if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1213  put_bits(&s->pb, 4, s->channels-1);
1214  else
1215  put_bits(&s->pb, 4, frame->ch_mode + FLAC_MAX_CHANNELS - 1);
1216 
1217  put_bits(&s->pb, 3, s->bps_code);
1218  put_bits(&s->pb, 1, 0);
1219  write_utf8(&s->pb, s->frame_count);
1220 
1221  if (frame->bs_code[0] == 6)
1222  put_bits(&s->pb, 8, frame->bs_code[1]);
1223  else if (frame->bs_code[0] == 7)
1224  put_bits(&s->pb, 16, frame->bs_code[1]);
1225 
1226  if (s->sr_code[0] == 12)
1227  put_bits(&s->pb, 8, s->sr_code[1]);
1228  else if (s->sr_code[0] > 12)
1229  put_bits(&s->pb, 16, s->sr_code[1]);
1230 
1231  flush_put_bits(&s->pb);
1232  crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
1233  put_bytes_output(&s->pb));
1234  put_bits(&s->pb, 8, crc);
1235 }
1236 
1237 
1239 {
1240  int ch;
1241 
1242  for (ch = 0; ch < s->channels; ch++) {
1243  FlacSubframe *sub = &s->frame.subframes[ch];
1244  int i, p, porder, psize;
1245  int32_t *part_end;
1246  int32_t *res = sub->residual;
1247  int32_t *frame_end = &sub->residual[s->frame.blocksize];
1248 
1249  /* subframe header */
1250  put_bits(&s->pb, 1, 0);
1251  put_bits(&s->pb, 6, sub->type_code);
1252  put_bits(&s->pb, 1, !!sub->wasted);
1253  if (sub->wasted)
1254  put_bits(&s->pb, sub->wasted, 1);
1255 
1256  /* subframe */
1257  if (sub->type == FLAC_SUBFRAME_CONSTANT) {
1258  put_sbits(&s->pb, sub->obits, res[0]);
1259  } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
1260  while (res < frame_end)
1261  put_sbits(&s->pb, sub->obits, *res++);
1262  } else {
1263  /* warm-up samples */
1264  for (i = 0; i < sub->order; i++)
1265  put_sbits(&s->pb, sub->obits, *res++);
1266 
1267  /* LPC coefficients */
1268  if (sub->type == FLAC_SUBFRAME_LPC) {
1269  int cbits = s->options.lpc_coeff_precision;
1270  put_bits( &s->pb, 4, cbits-1);
1271  put_sbits(&s->pb, 5, sub->shift);
1272  for (i = 0; i < sub->order; i++)
1273  put_sbits(&s->pb, cbits, sub->coefs[i]);
1274  }
1275 
1276  /* rice-encoded block */
1277  put_bits(&s->pb, 2, sub->rc.coding_mode - 4);
1278 
1279  /* partition order */
1280  porder = sub->rc.porder;
1281  psize = s->frame.blocksize >> porder;
1282  put_bits(&s->pb, 4, porder);
1283 
1284  /* residual */
1285  part_end = &sub->residual[psize];
1286  for (p = 0; p < 1 << porder; p++) {
1287  int k = sub->rc.params[p];
1288  put_bits(&s->pb, sub->rc.coding_mode, k);
1289  while (res < part_end)
1290  set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
1291  part_end = FFMIN(frame_end, part_end + psize);
1292  }
1293  }
1294  }
1295 }
1296 
1297 
1299 {
1300  int crc;
1301  flush_put_bits(&s->pb);
1302  crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
1303  put_bytes_output(&s->pb)));
1304  put_bits(&s->pb, 16, crc);
1305  flush_put_bits(&s->pb);
1306 }
1307 
1308 
1310 {
1311  init_put_bits(&s->pb, avpkt->data, avpkt->size);
1313  write_subframes(s);
1315  return put_bytes_output(&s->pb);
1316 }
1317 
1318 
1319 static int update_md5_sum(FlacEncodeContext *s, const void *samples)
1320 {
1321  const uint8_t *buf;
1322  int buf_size = s->frame.blocksize * s->channels *
1323  ((s->avctx->bits_per_raw_sample + 7) / 8);
1324 
1325  if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
1326  av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size);
1327  if (!s->md5_buffer)
1328  return AVERROR(ENOMEM);
1329  }
1330 
1331  if (s->avctx->bits_per_raw_sample <= 16) {
1332  buf = (const uint8_t *)samples;
1333 #if HAVE_BIGENDIAN
1334  s->bdsp.bswap16_buf((uint16_t *) s->md5_buffer,
1335  (const uint16_t *) samples, buf_size / 2);
1336  buf = s->md5_buffer;
1337 #endif
1338  } else {
1339  int i;
1340  const int32_t *samples0 = samples;
1341  uint8_t *tmp = s->md5_buffer;
1342 
1343  for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1344  int32_t v = samples0[i] >> 8;
1345  AV_WL24(tmp + 3*i, v);
1346  }
1347  buf = s->md5_buffer;
1348  }
1349  av_md5_update(s->md5ctx, buf, buf_size);
1350 
1351  return 0;
1352 }
1353 
1354 
1355 static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1356  const AVFrame *frame, int *got_packet_ptr)
1357 {
1359  int frame_bytes, out_bytes, ret;
1360 
1361  s = avctx->priv_data;
1362 
1363  /* when the last block is reached, update the header in extradata */
1364  if (!frame) {
1365  s->max_framesize = s->max_encoded_framesize;
1366  av_md5_final(s->md5ctx, s->md5sum);
1367  write_streaminfo(s, avctx->extradata);
1368 
1369  if (!s->flushed) {
1370  uint8_t *side_data = av_packet_new_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
1371  avctx->extradata_size);
1372  if (!side_data)
1373  return AVERROR(ENOMEM);
1374  memcpy(side_data, avctx->extradata, avctx->extradata_size);
1375 
1376  avpkt->pts = s->next_pts;
1377 
1378  *got_packet_ptr = 1;
1379  s->flushed = 1;
1380  }
1381 
1382  return 0;
1383  }
1384 
1385  /* change max_framesize for small final frame */
1386  if (frame->nb_samples < s->frame.blocksize) {
1387  s->max_framesize = flac_get_max_frame_size(frame->nb_samples,
1388  s->channels,
1389  avctx->bits_per_raw_sample);
1390  }
1391 
1392  init_frame(s, frame->nb_samples);
1393 
1394  copy_samples(s, frame->data[0]);
1395 
1397 
1399 
1400  frame_bytes = encode_frame(s);
1401 
1402  /* Fall back on verbatim mode if the compressed frame is larger than it
1403  would be if encoded uncompressed. */
1404  if (frame_bytes < 0 || frame_bytes > s->max_framesize) {
1405  s->frame.verbatim_only = 1;
1406  frame_bytes = encode_frame(s);
1407  if (frame_bytes < 0) {
1408  av_log(avctx, AV_LOG_ERROR, "Bad frame count\n");
1409  return frame_bytes;
1410  }
1411  }
1412 
1413  if ((ret = ff_get_encode_buffer(avctx, avpkt, frame_bytes, 0)) < 0)
1414  return ret;
1415 
1416  out_bytes = write_frame(s, avpkt);
1417 
1418  s->frame_count++;
1419  s->sample_count += frame->nb_samples;
1420  if ((ret = update_md5_sum(s, frame->data[0])) < 0) {
1421  av_log(avctx, AV_LOG_ERROR, "Error updating MD5 checksum\n");
1422  return ret;
1423  }
1424  if (out_bytes > s->max_encoded_framesize)
1425  s->max_encoded_framesize = out_bytes;
1426  if (out_bytes < s->min_framesize)
1427  s->min_framesize = out_bytes;
1428 
1429  avpkt->pts = frame->pts;
1430  avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
1431 
1432  s->next_pts = avpkt->pts + avpkt->duration;
1433 
1434  av_shrink_packet(avpkt, out_bytes);
1435 
1436  *got_packet_ptr = 1;
1437  return 0;
1438 }
1439 
1440 
1442 {
1443  FlacEncodeContext *s = avctx->priv_data;
1444 
1445  av_freep(&s->md5ctx);
1446  av_freep(&s->md5_buffer);
1447  ff_lpc_end(&s->lpc_ctx);
1448  return 0;
1449 }
1450 
1451 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1452 static const AVOption options[] = {
1453 { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
1454 { "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" },
1455 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1456 { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1457 { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1458 { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1459 { "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 },
1460 { "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1461 { "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1462 { "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" },
1463 { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
1464 { "2level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1465 { "4level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1466 { "8level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1467 { "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
1468 { "log", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
1469 { "ch_mode", "Stereo decorrelation mode", offsetof(FlacEncodeContext, options.ch_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FLAC_CHMODE_MID_SIDE, FLAGS, "ch_mode" },
1470 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1471 { "indep", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1472 { "left_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1473 { "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1474 { "mid_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1475 { "exact_rice_parameters", "Calculate rice parameters exactly", offsetof(FlacEncodeContext, options.exact_rice_parameters), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1476 { "multi_dim_quant", "Multi-dimensional quantization", offsetof(FlacEncodeContext, options.multi_dim_quant), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1477 { "min_prediction_order", NULL, offsetof(FlacEncodeContext, options.min_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS },
1478 { "max_prediction_order", NULL, offsetof(FlacEncodeContext, options.max_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS },
1479 
1480 { NULL },
1481 };
1482 
1483 static const AVClass flac_encoder_class = {
1484  .class_name = "FLAC encoder",
1485  .item_name = av_default_item_name,
1486  .option = options,
1487  .version = LIBAVUTIL_VERSION_INT,
1488 };
1489 
1491  .p.name = "flac",
1492  CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)"),
1493  .p.type = AVMEDIA_TYPE_AUDIO,
1494  .p.id = AV_CODEC_ID_FLAC,
1495  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1497  .priv_data_size = sizeof(FlacEncodeContext),
1500  .close = flac_encode_close,
1501  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
1504  .p.priv_class = &flac_encoder_class,
1505  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1506 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1035
bswapdsp.h
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
PUT_UTF8
#define PUT_UTF8(val, tmp, PUT_BYTE)
Definition: common.h:522
FFLPCType
FFLPCType
LPC analysis type.
Definition: lpc.h:44
level
uint8_t level
Definition: svq3.c:204
FlacSubframe::samples
int32_t samples[FLAC_MAX_BLOCKSIZE]
Definition: flacenc.c:92
av_clip
#define av_clip
Definition: common.h:95
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
flac_encode_init
static av_cold int flac_encode_init(AVCodecContext *avctx)
Definition: flacenc.c:269
FLAC_CHMODE_MID_SIDE
@ FLAC_CHMODE_MID_SIDE
Definition: flac.h:42
ff_ctz
#define ff_ctz
Definition: intmath.h:107
put_bytes_output
static int put_bytes_output(const PutBitContext *s)
Definition: put_bits.h:89
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1007
CompressionOptions::exact_rice_parameters
int exact_rice_parameters
Definition: flacenc.c:69
MAX_PARTITION_ORDER
#define MAX_PARTITION_ORDER
Definition: flacenc.c:46
FlacEncodeContext::bdsp
BswapDSPContext bdsp
Definition: flacenc.c:126
sub
static float sub(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:31
CompressionOptions::max_prediction_order
int max_prediction_order
Definition: flacenc.c:64
CompressionOptions::ch_mode
int ch_mode
Definition: flacenc.c:68
MAX_LPC_SHIFT
#define MAX_LPC_SHIFT
Definition: flacenc.c:50
ff_lpc_calc_coefs
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:207
put_sbits
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:281
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
flac_encode_frame
static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: flacenc.c:1355
FLACEncDSPContext
Definition: flacencdsp.h:24
FlacEncodeContext::md5sum
uint8_t md5sum[16]
Definition: flacenc.c:118
ff_lpc_init
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition: lpc.c:307
RiceContext
Definition: alacenc.c:50
AV_CHANNEL_LAYOUT_2_2
#define AV_CHANNEL_LAYOUT_2_2
Definition: channel_layout.h:376
av_unused
#define av_unused
Definition: attributes.h:131
FlacEncodeContext::avctx
AVCodecContext * avctx
Definition: flacenc.c:121
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:221
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
CompressionOptions::max_partition_order
int max_partition_order
Definition: flacenc.c:67
FF_LPC_TYPE_CHOLESKY
@ FF_LPC_TYPE_CHOLESKY
Cholesky factorization.
Definition: lpc.h:49
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
encode.h
FlacEncodeContext
Definition: flacenc.c:105
b
#define b
Definition: input.c:41
data
const char data[16]
Definition: mxf.c:146
FFCodec
Definition: codec_internal.h:119
CompressionOptions::prediction_order_method
int prediction_order_method
Definition: flacenc.c:65
FlacFrame
Definition: flacenc.c:96
select_blocksize
static int select_blocksize(int samplerate, int block_time_ms)
Set blocksize based on samplerate.
Definition: flacenc.c:192
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
CODING_MODE_RICE
@ CODING_MODE_RICE
Definition: flacenc.c:53
AV_CODEC_ID_FLAC
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:447
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:306
lpc.h
FLAC_CHMODE_RIGHT_SIDE
@ FLAC_CHMODE_RIGHT_SIDE
Definition: flac.h:41
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
CompressionOptions::compression_level
int compression_level
Definition: flacenc.c:58
FLAC_CHMODE_LEFT_SIDE
@ FLAC_CHMODE_LEFT_SIDE
Definition: flac.h:40
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
FlacEncodeContext::pb
PutBitContext pb
Definition: flacenc.c:107
crc.h
LPCContext
Definition: lpc.h:53
find_subframe_rice_params
static uint64_t find_subframe_rice_params(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
Definition: flacenc.c:750
ORDER_METHOD_4LEVEL
#define ORDER_METHOD_4LEVEL
Definition: lpc.h:33
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:123
FlacSubframe::wasted
int wasted
Definition: flacenc.c:83
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2059
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:112
FlacEncodeContext::frame_count
uint32_t frame_count
Definition: flacenc.c:116
CompressionOptions::lpc_coeff_precision
int lpc_coeff_precision
Definition: flacenc.c:62
val
static double val(void *priv, double ch)
Definition: aeval.c:77
FlacEncodeContext::flac_dsp
FLACEncDSPContext flac_dsp
Definition: flacenc.c:127
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:307
AVMD5
Definition: md5.c:40
AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_SURROUND
Definition: channel_layout.h:372
FF_LPC_TYPE_DEFAULT
@ FF_LPC_TYPE_DEFAULT
use the codec default LPC type
Definition: lpc.h:45
ff_flac_blocksize_table
const int32_t ff_flac_blocksize_table[16]
Definition: flacdata.c:30
avassert.h
put_golomb.h
exp golomb vlc writing stuff
FlacFrame::ch_mode
int ch_mode
Definition: flacenc.c:101
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
write_subframes
static void write_subframes(FlacEncodeContext *s)
Definition: flacenc.c:1238
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:500
s
#define s(width, name)
Definition: cbs_vp9.c:256
FlacSubframe::type_code
int type_code
Definition: flacenc.c:81
FlacSubframe::obits
int obits
Definition: flacenc.c:82
FlacEncodeContext::max_framesize
int max_framesize
Definition: flacenc.c:114
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:112
AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
Definition: channel_layout.h:380
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:363
flac_get_max_frame_size
static int flac_get_max_frame_size(int blocksize, int ch, int bps)
Calculate an estimate for the maximum frame size based on verbatim mode.
Definition: flacenc.c:166
CompressionOptions::multi_dim_quant
int multi_dim_quant
Definition: flacenc.c:70
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
FlacSubframe::order
int order
Definition: flacenc.c:84
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1451
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
remove_wasted_bits
static void remove_wasted_bits(FlacEncodeContext *s)
Definition: flacenc.c:1073
channels
channels
Definition: aptx.h:31
FlacSubframe::rc
RiceContext rc
Definition: flacenc.c:88
FLAC_SUBFRAME_LPC
#define FLAC_SUBFRAME_LPC
Definition: flacenc.c:43
set_sr_golomb_flac
static void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len)
write signed golomb rice code (flac).
Definition: put_golomb.h:157
COPY_SAMPLES
#define COPY_SAMPLES(bits)
calc_optimal_rice_params
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:631
CompressionOptions
Definition: dcaenc.c:56
FLAC_SUBFRAME_VERBATIM
#define FLAC_SUBFRAME_VERBATIM
Definition: flacenc.c:41
PutBitContext
Definition: put_bits.h:50
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
RiceContext::porder
int porder
Definition: flacenc.c:75
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:64
FLAC_SUBFRAME_CONSTANT
#define FLAC_SUBFRAME_CONSTANT
Definition: flacenc.c:40
if
if(ret)
Definition: filter_design.txt:179
ff_flac_sample_rate_table
const int ff_flac_sample_rate_table[16]
Definition: flacdata.c:24
AV_CRC_16_ANSI
@ AV_CRC_16_ANSI
Definition: crc.h:50
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
FlacFrame::subframes
FlacSubframe subframes[FLAC_MAX_CHANNELS]
Definition: flacenc.c:97
ff_bswapdsp_init
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
NULL
#define NULL
Definition: coverity.c:32
FLAC_SUBFRAME_FIXED
#define FLAC_SUBFRAME_FIXED
Definition: flacenc.c:42
FlacEncodeContext::lpc_ctx
LPCContext lpc_ctx
Definition: flacenc.c:122
MIN_LPC_SHIFT
#define MIN_LPC_SHIFT
Definition: flacenc.c:49
ff_samples_to_time_base
static av_always_inline int64_t ff_samples_to_time_base(const AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition: encode.h:84
FlacEncodeContext::max_blocksize
int max_blocksize
Definition: flacenc.c:112
AV_WL24
#define AV_WL24(p, d)
Definition: intreadwrite.h:464
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
FLAGS
#define FLAGS
Definition: flacenc.c:1451
FlacEncodeContext::next_pts
int64_t next_pts
Definition: flacenc.c:130
ff_lpc_end
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:330
FLAC_STREAMINFO_SIZE
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:32
FlacEncodeContext::md5_buffer
uint8_t * md5_buffer
Definition: flacenc.c:124
ORDER_METHOD_SEARCH
#define ORDER_METHOD_SEARCH
Definition: lpc.h:35
CompressionOptions::lpc_passes
int lpc_passes
Definition: flacenc.c:61
FlacEncodeContext::channels
int channels
Definition: flacenc.c:108
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
rice_count_exact
static uint64_t rice_count_exact(const int32_t *res, int n, int k)
Definition: flacenc.c:533
FlacEncodeContext::max_encoded_framesize
int max_encoded_framesize
Definition: flacenc.c:115
encode_residual_ch
static int encode_residual_ch(FlacEncodeContext *s, int ch)
Definition: flacenc.c:818
get_max_p_order
static int get_max_p_order(int max_porder, int n, int order)
Definition: flacenc.c:741
ORDER_METHOD_8LEVEL
#define ORDER_METHOD_8LEVEL
Definition: lpc.h:34
find_optimal_param_exact
static int find_optimal_param_exact(uint64_t sums[32][MAX_PARTITIONS], int i, int max_param)
Definition: flacenc.c:614
FlacEncodeContext::flushed
int flushed
Definition: flacenc.c:129
f
f
Definition: af_crystalizer.c:122
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
FlacEncodeContext::md5_buffer_size
unsigned int md5_buffer_size
Definition: flacenc.c:125
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:301
codec_internal.h
av_clipl_int32
#define av_clipl_int32
Definition: common.h:113
shift
static int shift(int a, int b)
Definition: bonk.c:253
channel_decorrelation
static void channel_decorrelation(FlacEncodeContext *s)
Perform stereo channel decorrelation.
Definition: flacenc.c:1149
FF_LPC_TYPE_NB
@ FF_LPC_TYPE_NB
Not part of ABI.
Definition: lpc.h:50
MAX_LPC_ORDER
#define MAX_LPC_ORDER
Definition: lpc.h:39
bps
unsigned bps
Definition: movenc.c:1648
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1023
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
flacdata.h
encode_frame
static int encode_frame(FlacEncodeContext *s)
Definition: flacenc.c:1053
calc_sum_top
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:661
FlacFrame::crc8
uint8_t crc8
Definition: flacenc.c:100
FlacEncodeContext::frame
FlacFrame frame
Definition: flacenc.c:119
FlacFrame::bs_code
int bs_code[2]
Definition: flacenc.c:99
flacencdsp.h
FlacSubframe::residual
int32_t residual[FLAC_MAX_BLOCKSIZE+11]
Definition: flacenc.c:93
FlacEncodeContext::options
CompressionOptions options
Definition: flacenc.c:120
header
static const uint8_t header[24]
Definition: sdr2.c:67
FlacEncodeContext::samplerate
int samplerate
Definition: flacenc.c:109
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
FlacEncodeContext::min_framesize
int min_framesize
Definition: flacenc.c:113
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
write_frame
static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
Definition: flacenc.c:1309
av_bswap16
#define av_bswap16
Definition: bswap.h:31
FlacSubframe::type
int type
Definition: flacenc.c:80
ORDER_METHOD_EST
#define ORDER_METHOD_EST
Definition: lpc.h:31
options
static const AVOption options[]
Definition: flacenc.c:1452
FlacSubframe
Definition: flacenc.c:79
av_channel_layout_compare
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
Definition: channel_layout.c:932
FlacSubframe::coefs
int32_t coefs[MAX_LPC_ORDER]
Definition: flacenc.c:85
calc_sum_next
static void calc_sum_next(int level, uint64_t sums[32][MAX_PARTITIONS], int kmax)
Definition: flacenc.c:691
av_md5_init
void av_md5_init(AVMD5 *ctx)
Initialize MD5 hashing.
Definition: md5.c:141
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:108
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:499
write_utf8
static void write_utf8(PutBitContext *pb, uint32_t val)
Definition: flacenc.c:1194
AV_CHANNEL_LAYOUT_QUAD
#define AV_CHANNEL_LAYOUT_QUAD
Definition: channel_layout.h:377
CompressionOptions::min_partition_order
int min_partition_order
Definition: flacenc.c:66
count_frame_header
static int count_frame_header(FlacEncodeContext *s)
Definition: flacenc.c:1017
ff_flac_encoder
const FFCodec ff_flac_encoder
Definition: flacenc.c:1490
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
write_frame_header
static void write_frame_header(FlacEncodeContext *s)
Definition: flacenc.c:1201
md5.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_flacencdsp_init
av_cold void ff_flacencdsp_init(FLACEncDSPContext *c)
Definition: flacencdsp.c:32
write_frame_footer
static void write_frame_footer(FlacEncodeContext *s)
Definition: flacenc.c:1298
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
copy_samples
static void copy_samples(FlacEncodeContext *s, const void *samples)
Copy channel-interleaved input samples into separate subframes.
Definition: flacenc.c:511
av_md5_final
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
Definition: md5.c:186
MAX_PARTITIONS
#define MAX_PARTITIONS
Definition: flacenc.c:47
avcodec.h
CompressionOptions::lpc_type
enum FFLPCType lpc_type
Definition: flacenc.c:60
estimate_stereo_mode
static int estimate_stereo_mode(const int32_t *left_ch, const int32_t *right_ch, int n, int max_rice_param)
Definition: flacenc.c:1105
FlacEncodeContext::sample_count
uint64_t sample_count
Definition: flacenc.c:117
calc_rice_params
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:701
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
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:71
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
encode_residual_fixed
static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n, int order)
Definition: flacenc.c:767
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
FLAC_MAX_CHANNELS
#define FLAC_MAX_CHANNELS
Definition: flac.h:33
MAX_LPC_PRECISION
#define MAX_LPC_PRECISION
Definition: flacenc.c:48
AVCodecContext
main external API structure.
Definition: avcodec.h:398
FlacEncodeContext::bps_code
int bps_code
Definition: flacenc.c:111
FlacSubframe::rc_sums
uint64_t rc_sums[32][MAX_PARTITIONS]
Definition: flacenc.c:90
channel_layout.h
FlacSubframe::rc_udata
uint32_t rc_udata[FLAC_MAX_BLOCKSIZE]
Definition: flacenc.c:89
ORDER_METHOD_LOG
#define ORDER_METHOD_LOG
Definition: lpc.h:36
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: avpacket.c:230
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:79
av_crc
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:392
av_md5_alloc
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition: md5.c:48
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:56
subframe_count_exact
static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
Definition: flacenc.c:547
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
frame_end
static void frame_end(MpegEncContext *s)
Definition: mpegvideo_enc.c:1626
FlacEncodeContext::sr_code
int sr_code[2]
Definition: flacenc.c:110
CompressionOptions::min_prediction_order
int min_prediction_order
Definition: flacenc.c:63
AV_CODEC_CAP_DELAY
#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:82
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
FlacSubframe::shift
int shift
Definition: flacenc.c:86
av_md5_update
void av_md5_update(AVMD5 *ctx, const uint8_t *src, size_t len)
Update hash value.
Definition: md5.c:151
init_frame
static void init_frame(FlacEncodeContext *s, int nb_samples)
Definition: flacenc.c:466
RiceContext::params
int params[MAX_PARTITIONS]
Definition: flacenc.c:76
FLAC_MAX_BLOCKSIZE
#define FLAC_MAX_BLOCKSIZE
Definition: flac.h:35
AV_CRC_8_ATM
@ AV_CRC_8_ATM
Definition: crc.h:49
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:139
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:425
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
RiceContext::coding_mode
enum CodingMode coding_mode
Definition: flacenc.c:74
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_fast_malloc
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:565
find_optimal_param
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:602
dprint_compression_options
static av_cold void dprint_compression_options(FlacEncodeContext *s)
Definition: flacenc.c:211
update_md5_sum
static int update_md5_sum(FlacEncodeContext *s, const void *samples)
Definition: flacenc.c:1319
flac_encode_close
static av_cold int flac_encode_close(AVCodecContext *avctx)
Definition: flacenc.c:1441
d
d
Definition: ffmpeg_filter.c:156
CODING_MODE_RICE2
@ CODING_MODE_RICE2
Definition: flacenc.c:54
int32_t
int32_t
Definition: audioconvert.c:56
CompressionOptions::block_time_ms
int block_time_ms
Definition: flacenc.c:59
AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:381
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
flac_encoder_class
static const AVClass flac_encoder_class
Definition: flacenc.c:1483
BswapDSPContext
Definition: bswapdsp.h:24
write_streaminfo
static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
Write streaminfo metadata block to byte array.
Definition: flacenc.c:137
AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_5POINT0
Definition: channel_layout.h:378
MAX_FIXED_ORDER
#define MAX_FIXED_ORDER
Definition: flacenc.c:45
ORDER_METHOD_2LEVEL
#define ORDER_METHOD_2LEVEL
Definition: lpc.h:32
FLAC_MIN_BLOCKSIZE
#define FLAC_MIN_BLOCKSIZE
Definition: flac.h:34
FLAC_CHMODE_INDEPENDENT
@ FLAC_CHMODE_INDEPENDENT
Definition: flac.h:39
flac.h
FF_LPC_TYPE_NONE
@ FF_LPC_TYPE_NONE
do not use LPC prediction or use all zero coefficients
Definition: lpc.h:46
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:87
FlacFrame::verbatim_only
int verbatim_only
Definition: flacenc.c:102
AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_5POINT1
Definition: channel_layout.h:379
put_bits.h
FlacEncodeContext::md5ctx
struct AVMD5 * md5ctx
Definition: flacenc.c:123
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
FF_LPC_TYPE_LEVINSON
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
Definition: lpc.h:48
FlacFrame::blocksize
int blocksize
Definition: flacenc.c:98
CodingMode
CodingMode
Definition: flacenc.c:52
rice_encode_count
#define rice_encode_count(sum, n, k)
Definition: flacenc.c:597
AVCodecContext::compression_level
int compression_level
Definition: avcodec.h:470
FF_LPC_TYPE_FIXED
@ FF_LPC_TYPE_FIXED
fixed LPC coefficients
Definition: lpc.h:47
intmath.h