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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/crc.h"
23 #include "libavutil/intmath.h"
24 #include "libavutil/md5.h"
25 #include "libavutil/opt.h"
26 #include "avcodec.h"
27 #include "dsputil.h"
28 #include "get_bits.h"
29 #include "golomb.h"
30 #include "internal.h"
31 #include "lpc.h"
32 #include "flac.h"
33 #include "flacdata.h"
34 #include "flacdsp.h"
35 
36 #define FLAC_SUBFRAME_CONSTANT 0
37 #define FLAC_SUBFRAME_VERBATIM 1
38 #define FLAC_SUBFRAME_FIXED 8
39 #define FLAC_SUBFRAME_LPC 32
40 
41 #define MAX_FIXED_ORDER 4
42 #define MAX_PARTITION_ORDER 8
43 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
44 #define MAX_LPC_PRECISION 15
45 #define MAX_LPC_SHIFT 15
46 
47 enum CodingMode {
50 };
51 
52 typedef struct CompressionOptions {
63  int ch_mode;
65 
66 typedef struct RiceContext {
68  int porder;
70 } RiceContext;
71 
72 typedef struct FlacSubframe {
73  int type;
74  int type_code;
75  int obits;
76  int wasted;
77  int order;
79  int shift;
83 } FlacSubframe;
84 
85 typedef struct FlacFrame {
87  int blocksize;
88  int bs_code[2];
90  int ch_mode;
92 } FlacFrame;
93 
94 typedef struct FlacEncodeContext {
95  AVClass *class;
97  int channels;
99  int sr_code[2];
100  int bps_code;
105  uint32_t frame_count;
106  uint64_t sample_count;
112  struct AVMD5 *md5ctx;
114  unsigned int md5_buffer_size;
118 
119 
124 {
125  PutBitContext pb;
126 
127  memset(header, 0, FLAC_STREAMINFO_SIZE);
128  init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
129 
130  /* streaminfo metadata block */
131  put_bits(&pb, 16, s->max_blocksize);
132  put_bits(&pb, 16, s->max_blocksize);
133  put_bits(&pb, 24, s->min_framesize);
134  put_bits(&pb, 24, s->max_framesize);
135  put_bits(&pb, 20, s->samplerate);
136  put_bits(&pb, 3, s->channels-1);
137  put_bits(&pb, 5, s->avctx->bits_per_raw_sample - 1);
138  /* write 36-bit sample count in 2 put_bits() calls */
139  put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
140  put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
141  flush_put_bits(&pb);
142  memcpy(&header[18], s->md5sum, 16);
143 }
144 
145 
150 static int select_blocksize(int samplerate, int block_time_ms)
151 {
152  int i;
153  int target;
154  int blocksize;
155 
156  assert(samplerate > 0);
157  blocksize = ff_flac_blocksize_table[1];
158  target = (samplerate * block_time_ms) / 1000;
159  for (i = 0; i < 16; i++) {
160  if (target >= ff_flac_blocksize_table[i] &&
161  ff_flac_blocksize_table[i] > blocksize) {
162  blocksize = ff_flac_blocksize_table[i];
163  }
164  }
165  return blocksize;
166 }
167 
168 
170 {
171  AVCodecContext *avctx = s->avctx;
172  CompressionOptions *opt = &s->options;
173 
174  av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);
175 
176  switch (opt->lpc_type) {
177  case FF_LPC_TYPE_NONE:
178  av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
179  break;
180  case FF_LPC_TYPE_FIXED:
181  av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
182  break;
184  av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
185  break;
187  av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
188  opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
189  break;
190  }
191 
192  av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
194 
195  switch (opt->prediction_order_method) {
196  case ORDER_METHOD_EST:
197  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
198  break;
199  case ORDER_METHOD_2LEVEL:
200  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
201  break;
202  case ORDER_METHOD_4LEVEL:
203  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
204  break;
205  case ORDER_METHOD_8LEVEL:
206  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
207  break;
208  case ORDER_METHOD_SEARCH:
209  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
210  break;
211  case ORDER_METHOD_LOG:
212  av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
213  break;
214  }
215 
216 
217  av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
219 
220  av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
221 
222  av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
223  opt->lpc_coeff_precision);
224 }
225 
226 
228 {
229  int freq = avctx->sample_rate;
230  int channels = avctx->channels;
231  FlacEncodeContext *s = avctx->priv_data;
232  int i, level, ret;
233  uint8_t *streaminfo;
234 
235  s->avctx = avctx;
236 
237  switch (avctx->sample_fmt) {
238  case AV_SAMPLE_FMT_S16:
239  avctx->bits_per_raw_sample = 16;
240  s->bps_code = 4;
241  break;
242  case AV_SAMPLE_FMT_S32:
243  if (avctx->bits_per_raw_sample != 24)
244  av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
245  avctx->bits_per_raw_sample = 24;
246  s->bps_code = 6;
247  break;
248  }
249 
250  if (channels < 1 || channels > FLAC_MAX_CHANNELS)
251  return -1;
252  s->channels = channels;
253 
254  /* find samplerate in table */
255  if (freq < 1)
256  return -1;
257  for (i = 4; i < 12; i++) {
258  if (freq == ff_flac_sample_rate_table[i]) {
260  s->sr_code[0] = i;
261  s->sr_code[1] = 0;
262  break;
263  }
264  }
265  /* if not in table, samplerate is non-standard */
266  if (i == 12) {
267  if (freq % 1000 == 0 && freq < 255000) {
268  s->sr_code[0] = 12;
269  s->sr_code[1] = freq / 1000;
270  } else if (freq % 10 == 0 && freq < 655350) {
271  s->sr_code[0] = 14;
272  s->sr_code[1] = freq / 10;
273  } else if (freq < 65535) {
274  s->sr_code[0] = 13;
275  s->sr_code[1] = freq;
276  } else {
277  return -1;
278  }
279  s->samplerate = freq;
280  }
281 
282  /* set compression option defaults based on avctx->compression_level */
283  if (avctx->compression_level < 0)
284  s->options.compression_level = 5;
285  else
287 
288  level = s->options.compression_level;
289  if (level > 12) {
290  av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
292  return -1;
293  }
294 
295  s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
296 
302  FF_LPC_TYPE_LEVINSON})[level];
303 
304  s->options.min_prediction_order = ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
305  s->options.max_prediction_order = ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
306 
307  if (s->options.prediction_order_method < 0)
312  ORDER_METHOD_SEARCH})[level];
313 
315  av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
317  return AVERROR(EINVAL);
318  }
319  if (s->options.min_partition_order < 0)
320  s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
321  if (s->options.max_partition_order < 0)
322  s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
323 
324  if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
326  } else if (avctx->min_prediction_order >= 0) {
327  if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
328  if (avctx->min_prediction_order > MAX_FIXED_ORDER) {
329  av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
330  avctx->min_prediction_order);
331  return -1;
332  }
333  } else if (avctx->min_prediction_order < MIN_LPC_ORDER ||
335  av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
336  avctx->min_prediction_order);
337  return -1;
338  }
340  }
341  if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
343  } else if (avctx->max_prediction_order >= 0) {
344  if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
345  if (avctx->max_prediction_order > MAX_FIXED_ORDER) {
346  av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
347  avctx->max_prediction_order);
348  return -1;
349  }
350  } else if (avctx->max_prediction_order < MIN_LPC_ORDER ||
352  av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
353  avctx->max_prediction_order);
354  return -1;
355  }
357  }
359  av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
361  return -1;
362  }
363 
364  if (avctx->frame_size > 0) {
365  if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
366  avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
367  av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
368  avctx->frame_size);
369  return -1;
370  }
371  } else {
373  }
374  s->max_blocksize = s->avctx->frame_size;
375 
376  /* set maximum encoded frame size in verbatim mode */
378  s->channels,
380 
381  /* initialize MD5 context */
382  s->md5ctx = av_md5_alloc();
383  if (!s->md5ctx)
384  return AVERROR(ENOMEM);
385  av_md5_init(s->md5ctx);
386 
387  streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
388  if (!streaminfo)
389  return AVERROR(ENOMEM);
390  write_streaminfo(s, streaminfo);
391  avctx->extradata = streaminfo;
393 
394  s->frame_count = 0;
396 
397 #if FF_API_OLD_ENCODE_AUDIO
398  avctx->coded_frame = avcodec_alloc_frame();
399  if (!avctx->coded_frame)
400  return AVERROR(ENOMEM);
401 #endif
402 
403  ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
405 
406  ff_dsputil_init(&s->dsp, avctx);
407  ff_flacdsp_init(&s->flac_dsp, avctx->sample_fmt,
408  avctx->bits_per_raw_sample);
409 
411 
412  return ret;
413 }
414 
415 
417 {
418  int i, ch;
419  FlacFrame *frame;
420 
421  frame = &s->frame;
422 
423  for (i = 0; i < 16; i++) {
424  if (nb_samples == ff_flac_blocksize_table[i]) {
426  frame->bs_code[0] = i;
427  frame->bs_code[1] = 0;
428  break;
429  }
430  }
431  if (i == 16) {
432  frame->blocksize = nb_samples;
433  if (frame->blocksize <= 256) {
434  frame->bs_code[0] = 6;
435  frame->bs_code[1] = frame->blocksize-1;
436  } else {
437  frame->bs_code[0] = 7;
438  frame->bs_code[1] = frame->blocksize-1;
439  }
440  }
441 
442  for (ch = 0; ch < s->channels; ch++) {
443  FlacSubframe *sub = &frame->subframes[ch];
444 
445  sub->wasted = 0;
446  sub->obits = s->avctx->bits_per_raw_sample;
447 
448  if (sub->obits > 16)
450  else
452  }
453 
454  frame->verbatim_only = 0;
455 }
456 
457 
461 static void copy_samples(FlacEncodeContext *s, const void *samples)
462 {
463  int i, j, ch;
464  FlacFrame *frame;
465  int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
467 
468 #define COPY_SAMPLES(bits) do { \
469  const int ## bits ## _t *samples0 = samples; \
470  frame = &s->frame; \
471  for (i = 0, j = 0; i < frame->blocksize; i++) \
472  for (ch = 0; ch < s->channels; ch++, j++) \
473  frame->subframes[ch].samples[i] = samples0[j] >> shift; \
474 } while (0)
475 
477  COPY_SAMPLES(16);
478  else
479  COPY_SAMPLES(32);
480 }
481 
482 
483 static uint64_t rice_count_exact(int32_t *res, int n, int k)
484 {
485  int i;
486  uint64_t count = 0;
487 
488  for (i = 0; i < n; i++) {
489  int32_t v = -2 * res[i] - 1;
490  v ^= v >> 31;
491  count += (v >> k) + 1 + k;
492  }
493  return count;
494 }
495 
496 
498  int pred_order)
499 {
500  int p, porder, psize;
501  int i, part_end;
502  uint64_t count = 0;
503 
504  /* subframe header */
505  count += 8;
506 
507  /* subframe */
508  if (sub->type == FLAC_SUBFRAME_CONSTANT) {
509  count += sub->obits;
510  } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
511  count += s->frame.blocksize * sub->obits;
512  } else {
513  /* warm-up samples */
514  count += pred_order * sub->obits;
515 
516  /* LPC coefficients */
517  if (sub->type == FLAC_SUBFRAME_LPC)
518  count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
519 
520  /* rice-encoded block */
521  count += 2;
522 
523  /* partition order */
524  porder = sub->rc.porder;
525  psize = s->frame.blocksize >> porder;
526  count += 4;
527 
528  /* residual */
529  i = pred_order;
530  part_end = psize;
531  for (p = 0; p < 1 << porder; p++) {
532  int k = sub->rc.params[p];
533  count += sub->rc.coding_mode;
534  count += rice_count_exact(&sub->residual[i], part_end - i, k);
535  i = part_end;
536  part_end = FFMIN(s->frame.blocksize, part_end + psize);
537  }
538  }
539 
540  return count;
541 }
542 
543 
544 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
545 
549 static int find_optimal_param(uint64_t sum, int n, int max_param)
550 {
551  int k;
552  uint64_t sum2;
553 
554  if (sum <= n >> 1)
555  return 0;
556  sum2 = sum - (n >> 1);
557  k = av_log2(av_clipl_int32(sum2 / n));
558  return FFMIN(k, max_param);
559 }
560 
561 
562 static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder,
563  uint64_t *sums, int n, int pred_order)
564 {
565  int i;
566  int k, cnt, part, max_param;
567  uint64_t all_bits;
568 
569  max_param = (1 << rc->coding_mode) - 2;
570 
571  part = (1 << porder);
572  all_bits = 4 * part;
573 
574  cnt = (n >> porder) - pred_order;
575  for (i = 0; i < part; i++) {
576  k = find_optimal_param(sums[i], cnt, max_param);
577  rc->params[i] = k;
578  all_bits += rice_encode_count(sums[i], cnt, k);
579  cnt = n >> porder;
580  }
581 
582  rc->porder = porder;
583 
584  return all_bits;
585 }
586 
587 
588 static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
589  uint64_t sums[][MAX_PARTITIONS])
590 {
591  int i, j;
592  int parts;
593  uint32_t *res, *res_end;
594 
595  /* sums for highest level */
596  parts = (1 << pmax);
597  res = &data[pred_order];
598  res_end = &data[n >> pmax];
599  for (i = 0; i < parts; i++) {
600  uint64_t sum = 0;
601  while (res < res_end)
602  sum += *(res++);
603  sums[pmax][i] = sum;
604  res_end += n >> pmax;
605  }
606  /* sums for lower levels */
607  for (i = pmax - 1; i >= pmin; i--) {
608  parts = (1 << i);
609  for (j = 0; j < parts; j++)
610  sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
611  }
612 }
613 
614 
615 static uint64_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
616  int32_t *data, int n, int pred_order)
617 {
618  int i;
619  uint64_t bits[MAX_PARTITION_ORDER+1];
620  int opt_porder;
621  RiceContext tmp_rc;
622  uint32_t *udata;
623  uint64_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
624 
625  assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
626  assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
627  assert(pmin <= pmax);
628 
629  tmp_rc.coding_mode = rc->coding_mode;
630 
631  udata = av_malloc(n * sizeof(uint32_t));
632  for (i = 0; i < n; i++)
633  udata[i] = (2*data[i]) ^ (data[i]>>31);
634 
635  calc_sums(pmin, pmax, udata, n, pred_order, sums);
636 
637  opt_porder = pmin;
638  bits[pmin] = UINT32_MAX;
639  for (i = pmin; i <= pmax; i++) {
640  bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
641  if (bits[i] <= bits[opt_porder]) {
642  opt_porder = i;
643  *rc = tmp_rc;
644  }
645  }
646 
647  av_freep(&udata);
648  return bits[opt_porder];
649 }
650 
651 
652 static int get_max_p_order(int max_porder, int n, int order)
653 {
654  int porder = FFMIN(max_porder, av_log2(n^(n-1)));
655  if (order > 0)
656  porder = FFMIN(porder, av_log2(n/order));
657  return porder;
658 }
659 
660 
662  FlacSubframe *sub, int pred_order)
663 {
665  s->frame.blocksize, pred_order);
667  s->frame.blocksize, pred_order);
668 
669  uint64_t bits = 8 + pred_order * sub->obits + 2 + sub->rc.coding_mode;
670  if (sub->type == FLAC_SUBFRAME_LPC)
671  bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
672  bits += calc_rice_params(&sub->rc, pmin, pmax, sub->residual,
673  s->frame.blocksize, pred_order);
674  return bits;
675 }
676 
677 
678 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
679  int order)
680 {
681  int i;
682 
683  for (i = 0; i < order; i++)
684  res[i] = smp[i];
685 
686  if (order == 0) {
687  for (i = order; i < n; i++)
688  res[i] = smp[i];
689  } else if (order == 1) {
690  for (i = order; i < n; i++)
691  res[i] = smp[i] - smp[i-1];
692  } else if (order == 2) {
693  int a = smp[order-1] - smp[order-2];
694  for (i = order; i < n; i += 2) {
695  int b = smp[i ] - smp[i-1];
696  res[i] = b - a;
697  a = smp[i+1] - smp[i ];
698  res[i+1] = a - b;
699  }
700  } else if (order == 3) {
701  int a = smp[order-1] - smp[order-2];
702  int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
703  for (i = order; i < n; i += 2) {
704  int b = smp[i ] - smp[i-1];
705  int d = b - a;
706  res[i] = d - c;
707  a = smp[i+1] - smp[i ];
708  c = a - b;
709  res[i+1] = c - d;
710  }
711  } else {
712  int a = smp[order-1] - smp[order-2];
713  int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
714  int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
715  for (i = order; i < n; i += 2) {
716  int b = smp[i ] - smp[i-1];
717  int d = b - a;
718  int f = d - c;
719  res[i ] = f - e;
720  a = smp[i+1] - smp[i ];
721  c = a - b;
722  e = c - d;
723  res[i+1] = e - f;
724  }
725  }
726 }
727 
728 
729 static int encode_residual_ch(FlacEncodeContext *s, int ch)
730 {
731  int i, n;
732  int min_order, max_order, opt_order, omethod;
733  FlacFrame *frame;
734  FlacSubframe *sub;
736  int shift[MAX_LPC_ORDER];
737  int32_t *res, *smp;
738 
739  frame = &s->frame;
740  sub = &frame->subframes[ch];
741  res = sub->residual;
742  smp = sub->samples;
743  n = frame->blocksize;
744 
745  /* CONSTANT */
746  for (i = 1; i < n; i++)
747  if(smp[i] != smp[0])
748  break;
749  if (i == n) {
750  sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
751  res[0] = smp[0];
752  return subframe_count_exact(s, sub, 0);
753  }
754 
755  /* VERBATIM */
756  if (frame->verbatim_only || n < 5) {
757  sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
758  memcpy(res, smp, n * sizeof(int32_t));
759  return subframe_count_exact(s, sub, 0);
760  }
761 
762  min_order = s->options.min_prediction_order;
763  max_order = s->options.max_prediction_order;
764  omethod = s->options.prediction_order_method;
765 
766  /* FIXED */
767  sub->type = FLAC_SUBFRAME_FIXED;
768  if (s->options.lpc_type == FF_LPC_TYPE_NONE ||
769  s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
770  uint64_t bits[MAX_FIXED_ORDER+1];
771  if (max_order > MAX_FIXED_ORDER)
772  max_order = MAX_FIXED_ORDER;
773  opt_order = 0;
774  bits[0] = UINT32_MAX;
775  for (i = min_order; i <= max_order; i++) {
776  encode_residual_fixed(res, smp, n, i);
777  bits[i] = find_subframe_rice_params(s, sub, i);
778  if (bits[i] < bits[opt_order])
779  opt_order = i;
780  }
781  sub->order = opt_order;
782  sub->type_code = sub->type | sub->order;
783  if (sub->order != max_order) {
784  encode_residual_fixed(res, smp, n, sub->order);
785  find_subframe_rice_params(s, sub, sub->order);
786  }
787  return subframe_count_exact(s, sub, sub->order);
788  }
789 
790  /* LPC */
791  sub->type = FLAC_SUBFRAME_LPC;
792  opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
793  s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
794  s->options.lpc_passes, omethod,
795  MAX_LPC_SHIFT, 0);
796 
797  if (omethod == ORDER_METHOD_2LEVEL ||
798  omethod == ORDER_METHOD_4LEVEL ||
799  omethod == ORDER_METHOD_8LEVEL) {
800  int levels = 1 << omethod;
801  uint64_t bits[1 << ORDER_METHOD_8LEVEL];
802  int order = -1;
803  int opt_index = levels-1;
804  opt_order = max_order-1;
805  bits[opt_index] = UINT32_MAX;
806  for (i = levels-1; i >= 0; i--) {
807  int last_order = order;
808  order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
809  order = av_clip(order, min_order - 1, max_order - 1);
810  if (order == last_order)
811  continue;
812  s->flac_dsp.lpc_encode(res, smp, n, order+1, coefs[order],
813  shift[order]);
814  bits[i] = find_subframe_rice_params(s, sub, order+1);
815  if (bits[i] < bits[opt_index]) {
816  opt_index = i;
817  opt_order = order;
818  }
819  }
820  opt_order++;
821  } else if (omethod == ORDER_METHOD_SEARCH) {
822  // brute-force optimal order search
823  uint64_t bits[MAX_LPC_ORDER];
824  opt_order = 0;
825  bits[0] = UINT32_MAX;
826  for (i = min_order-1; i < max_order; i++) {
827  s->flac_dsp.lpc_encode(res, smp, n, i+1, coefs[i], shift[i]);
828  bits[i] = find_subframe_rice_params(s, sub, i+1);
829  if (bits[i] < bits[opt_order])
830  opt_order = i;
831  }
832  opt_order++;
833  } else if (omethod == ORDER_METHOD_LOG) {
834  uint64_t bits[MAX_LPC_ORDER];
835  int step;
836 
837  opt_order = min_order - 1 + (max_order-min_order)/3;
838  memset(bits, -1, sizeof(bits));
839 
840  for (step = 16; step; step >>= 1) {
841  int last = opt_order;
842  for (i = last-step; i <= last+step; i += step) {
843  if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
844  continue;
845  s->flac_dsp.lpc_encode(res, smp, n, i+1, coefs[i], shift[i]);
846  bits[i] = find_subframe_rice_params(s, sub, i+1);
847  if (bits[i] < bits[opt_order])
848  opt_order = i;
849  }
850  }
851  opt_order++;
852  }
853 
854  sub->order = opt_order;
855  sub->type_code = sub->type | (sub->order-1);
856  sub->shift = shift[sub->order-1];
857  for (i = 0; i < sub->order; i++)
858  sub->coefs[i] = coefs[sub->order-1][i];
859 
860  s->flac_dsp.lpc_encode(res, smp, n, sub->order, sub->coefs, sub->shift);
861 
862  find_subframe_rice_params(s, sub, sub->order);
863 
864  return subframe_count_exact(s, sub, sub->order);
865 }
866 
867 
869 {
870  uint8_t av_unused tmp;
871  int count;
872 
873  /*
874  <14> Sync code
875  <1> Reserved
876  <1> Blocking strategy
877  <4> Block size in inter-channel samples
878  <4> Sample rate
879  <4> Channel assignment
880  <3> Sample size in bits
881  <1> Reserved
882  */
883  count = 32;
884 
885  /* coded frame number */
886  PUT_UTF8(s->frame_count, tmp, count += 8;)
887 
888  /* explicit block size */
889  if (s->frame.bs_code[0] == 6)
890  count += 8;
891  else if (s->frame.bs_code[0] == 7)
892  count += 16;
893 
894  /* explicit sample rate */
895  count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12)) * 8;
896 
897  /* frame header CRC-8 */
898  count += 8;
899 
900  return count;
901 }
902 
903 
905 {
906  int ch;
907  uint64_t count;
908 
909  count = count_frame_header(s);
910 
911  for (ch = 0; ch < s->channels; ch++)
912  count += encode_residual_ch(s, ch);
913 
914  count += (8 - (count & 7)) & 7; // byte alignment
915  count += 16; // CRC-16
916 
917  count >>= 3;
918  if (count > INT_MAX)
919  return AVERROR_BUG;
920  return count;
921 }
922 
923 
925 {
926  int ch, i;
927 
928  for (ch = 0; ch < s->channels; ch++) {
929  FlacSubframe *sub = &s->frame.subframes[ch];
930  int32_t v = 0;
931 
932  for (i = 0; i < s->frame.blocksize; i++) {
933  v |= sub->samples[i];
934  if (v & 1)
935  break;
936  }
937 
938  if (v && !(v & 1)) {
939  v = av_ctz(v);
940 
941  for (i = 0; i < s->frame.blocksize; i++)
942  sub->samples[i] >>= v;
943 
944  sub->wasted = v;
945  sub->obits -= v;
946 
947  /* for 24-bit, check if removing wasted bits makes the range better
948  suited for using RICE instead of RICE2 for entropy coding */
949  if (sub->obits <= 17)
951  }
952  }
953 }
954 
955 
956 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n,
957  int max_rice_param)
958 {
959  int i, best;
960  int32_t lt, rt;
961  uint64_t sum[4];
962  uint64_t score[4];
963  int k;
964 
965  /* calculate sum of 2nd order residual for each channel */
966  sum[0] = sum[1] = sum[2] = sum[3] = 0;
967  for (i = 2; i < n; i++) {
968  lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
969  rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
970  sum[2] += FFABS((lt + rt) >> 1);
971  sum[3] += FFABS(lt - rt);
972  sum[0] += FFABS(lt);
973  sum[1] += FFABS(rt);
974  }
975  /* estimate bit counts */
976  for (i = 0; i < 4; i++) {
977  k = find_optimal_param(2 * sum[i], n, max_rice_param);
978  sum[i] = rice_encode_count( 2 * sum[i], n, k);
979  }
980 
981  /* calculate score for each mode */
982  score[0] = sum[0] + sum[1];
983  score[1] = sum[0] + sum[3];
984  score[2] = sum[1] + sum[3];
985  score[3] = sum[2] + sum[3];
986 
987  /* return mode with lowest score */
988  best = 0;
989  for (i = 1; i < 4; i++)
990  if (score[i] < score[best])
991  best = i;
992 
993  return best;
994 }
995 
996 
1001 {
1002  FlacFrame *frame;
1003  int32_t *left, *right;
1004  int i, n;
1005 
1006  frame = &s->frame;
1007  n = frame->blocksize;
1008  left = frame->subframes[0].samples;
1009  right = frame->subframes[1].samples;
1010 
1011  if (s->channels != 2) {
1013  return;
1014  }
1015 
1016  if (s->options.ch_mode < 0) {
1017  int max_rice_param = (1 << frame->subframes[0].rc.coding_mode) - 2;
1018  frame->ch_mode = estimate_stereo_mode(left, right, n, max_rice_param);
1019  } else
1020  frame->ch_mode = s->options.ch_mode;
1021 
1022  /* perform decorrelation and adjust bits-per-sample */
1023  if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1024  return;
1025  if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1026  int32_t tmp;
1027  for (i = 0; i < n; i++) {
1028  tmp = left[i];
1029  left[i] = (tmp + right[i]) >> 1;
1030  right[i] = tmp - right[i];
1031  }
1032  frame->subframes[1].obits++;
1033  } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1034  for (i = 0; i < n; i++)
1035  right[i] = left[i] - right[i];
1036  frame->subframes[1].obits++;
1037  } else {
1038  for (i = 0; i < n; i++)
1039  left[i] -= right[i];
1040  frame->subframes[0].obits++;
1041  }
1042 }
1043 
1044 
1045 static void write_utf8(PutBitContext *pb, uint32_t val)
1046 {
1047  uint8_t tmp;
1048  PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1049 }
1050 
1051 
1053 {
1054  FlacFrame *frame;
1055  int crc;
1056 
1057  frame = &s->frame;
1058 
1059  put_bits(&s->pb, 16, 0xFFF8);
1060  put_bits(&s->pb, 4, frame->bs_code[0]);
1061  put_bits(&s->pb, 4, s->sr_code[0]);
1062 
1063  if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1064  put_bits(&s->pb, 4, s->channels-1);
1065  else
1066  put_bits(&s->pb, 4, frame->ch_mode + FLAC_MAX_CHANNELS - 1);
1067 
1068  put_bits(&s->pb, 3, s->bps_code);
1069  put_bits(&s->pb, 1, 0);
1070  write_utf8(&s->pb, s->frame_count);
1071 
1072  if (frame->bs_code[0] == 6)
1073  put_bits(&s->pb, 8, frame->bs_code[1]);
1074  else if (frame->bs_code[0] == 7)
1075  put_bits(&s->pb, 16, frame->bs_code[1]);
1076 
1077  if (s->sr_code[0] == 12)
1078  put_bits(&s->pb, 8, s->sr_code[1]);
1079  else if (s->sr_code[0] > 12)
1080  put_bits(&s->pb, 16, s->sr_code[1]);
1081 
1082  flush_put_bits(&s->pb);
1083  crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
1084  put_bits_count(&s->pb) >> 3);
1085  put_bits(&s->pb, 8, crc);
1086 }
1087 
1088 
1090 {
1091  int ch;
1092 
1093  for (ch = 0; ch < s->channels; ch++) {
1094  FlacSubframe *sub = &s->frame.subframes[ch];
1095  int i, p, porder, psize;
1096  int32_t *part_end;
1097  int32_t *res = sub->residual;
1098  int32_t *frame_end = &sub->residual[s->frame.blocksize];
1099 
1100  /* subframe header */
1101  put_bits(&s->pb, 1, 0);
1102  put_bits(&s->pb, 6, sub->type_code);
1103  put_bits(&s->pb, 1, !!sub->wasted);
1104  if (sub->wasted)
1105  put_bits(&s->pb, sub->wasted, 1);
1106 
1107  /* subframe */
1108  if (sub->type == FLAC_SUBFRAME_CONSTANT) {
1109  put_sbits(&s->pb, sub->obits, res[0]);
1110  } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
1111  while (res < frame_end)
1112  put_sbits(&s->pb, sub->obits, *res++);
1113  } else {
1114  /* warm-up samples */
1115  for (i = 0; i < sub->order; i++)
1116  put_sbits(&s->pb, sub->obits, *res++);
1117 
1118  /* LPC coefficients */
1119  if (sub->type == FLAC_SUBFRAME_LPC) {
1120  int cbits = s->options.lpc_coeff_precision;
1121  put_bits( &s->pb, 4, cbits-1);
1122  put_sbits(&s->pb, 5, sub->shift);
1123  for (i = 0; i < sub->order; i++)
1124  put_sbits(&s->pb, cbits, sub->coefs[i]);
1125  }
1126 
1127  /* rice-encoded block */
1128  put_bits(&s->pb, 2, sub->rc.coding_mode - 4);
1129 
1130  /* partition order */
1131  porder = sub->rc.porder;
1132  psize = s->frame.blocksize >> porder;
1133  put_bits(&s->pb, 4, porder);
1134 
1135  /* residual */
1136  part_end = &sub->residual[psize];
1137  for (p = 0; p < 1 << porder; p++) {
1138  int k = sub->rc.params[p];
1139  put_bits(&s->pb, sub->rc.coding_mode, k);
1140  while (res < part_end)
1141  set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
1142  part_end = FFMIN(frame_end, part_end + psize);
1143  }
1144  }
1145  }
1146 }
1147 
1148 
1150 {
1151  int crc;
1152  flush_put_bits(&s->pb);
1154  put_bits_count(&s->pb)>>3));
1155  put_bits(&s->pb, 16, crc);
1156  flush_put_bits(&s->pb);
1157 }
1158 
1159 
1161 {
1162  init_put_bits(&s->pb, avpkt->data, avpkt->size);
1163  write_frame_header(s);
1164  write_subframes(s);
1165  write_frame_footer(s);
1166  return put_bits_count(&s->pb) >> 3;
1167 }
1168 
1169 
1170 static int update_md5_sum(FlacEncodeContext *s, const void *samples)
1171 {
1172  const uint8_t *buf;
1173  int buf_size = s->frame.blocksize * s->channels *
1174  ((s->avctx->bits_per_raw_sample + 7) / 8);
1175 
1176  if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
1177  av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size);
1178  if (!s->md5_buffer)
1179  return AVERROR(ENOMEM);
1180  }
1181 
1182  if (s->avctx->bits_per_raw_sample <= 16) {
1183  buf = (const uint8_t *)samples;
1184 #if HAVE_BIGENDIAN
1185  s->dsp.bswap16_buf((uint16_t *)s->md5_buffer,
1186  (const uint16_t *)samples, buf_size / 2);
1187  buf = s->md5_buffer;
1188 #endif
1189  } else {
1190  int i;
1191  const int32_t *samples0 = samples;
1192  uint8_t *tmp = s->md5_buffer;
1193 
1194  for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1195  int32_t v = samples0[i] >> 8;
1196  *tmp++ = (v ) & 0xFF;
1197  *tmp++ = (v >> 8) & 0xFF;
1198  *tmp++ = (v >> 16) & 0xFF;
1199  }
1200  buf = s->md5_buffer;
1201  }
1202  av_md5_update(s->md5ctx, buf, buf_size);
1203 
1204  return 0;
1205 }
1206 
1207 
1208 static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1209  const AVFrame *frame, int *got_packet_ptr)
1210 {
1211  FlacEncodeContext *s;
1212  int frame_bytes, out_bytes, ret;
1213 
1214  s = avctx->priv_data;
1215 
1216  /* when the last block is reached, update the header in extradata */
1217  if (!frame) {
1219  av_md5_final(s->md5ctx, s->md5sum);
1220  write_streaminfo(s, avctx->extradata);
1221  return 0;
1222  }
1223 
1224  /* change max_framesize for small final frame */
1225  if (frame->nb_samples < s->frame.blocksize) {
1227  s->channels,
1228  avctx->bits_per_raw_sample);
1229  }
1230 
1231  init_frame(s, frame->nb_samples);
1232 
1233  copy_samples(s, frame->data[0]);
1234 
1236 
1237  remove_wasted_bits(s);
1238 
1239  frame_bytes = encode_frame(s);
1240 
1241  /* fallback to verbatim mode if the compressed frame is larger than it
1242  would be if encoded uncompressed. */
1243  if (frame_bytes < 0 || frame_bytes > s->max_framesize) {
1244  s->frame.verbatim_only = 1;
1245  frame_bytes = encode_frame(s);
1246  if (frame_bytes < 0) {
1247  av_log(avctx, AV_LOG_ERROR, "Bad frame count\n");
1248  return frame_bytes;
1249  }
1250  }
1251 
1252  if ((ret = ff_alloc_packet(avpkt, frame_bytes))) {
1253  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
1254  return ret;
1255  }
1256 
1257  out_bytes = write_frame(s, avpkt);
1258 
1259  s->frame_count++;
1260  s->sample_count += frame->nb_samples;
1261  if ((ret = update_md5_sum(s, frame->data[0])) < 0) {
1262  av_log(avctx, AV_LOG_ERROR, "Error updating MD5 checksum\n");
1263  return ret;
1264  }
1265  if (out_bytes > s->max_encoded_framesize)
1266  s->max_encoded_framesize = out_bytes;
1267  if (out_bytes < s->min_framesize)
1268  s->min_framesize = out_bytes;
1269 
1270  avpkt->pts = frame->pts;
1271  avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
1272  avpkt->size = out_bytes;
1273  *got_packet_ptr = 1;
1274  return 0;
1275 }
1276 
1277 
1279 {
1280  if (avctx->priv_data) {
1281  FlacEncodeContext *s = avctx->priv_data;
1282  av_freep(&s->md5ctx);
1283  av_freep(&s->md5_buffer);
1284  ff_lpc_end(&s->lpc_ctx);
1285  }
1286  av_freep(&avctx->extradata);
1287  avctx->extradata_size = 0;
1288 #if FF_API_OLD_ENCODE_AUDIO
1289  av_freep(&avctx->coded_frame);
1290 #endif
1291  return 0;
1292 }
1293 
1294 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1295 static const AVOption options[] = {
1296 { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
1297 { "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" },
1298 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1299 { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1300 { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1301 { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1302 { "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes), AV_OPT_TYPE_INT, {.i64 = 1 }, 1, INT_MAX, FLAGS },
1303 { "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1304 { "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1305 { "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" },
1306 { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
1307 { "2level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1308 { "4level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1309 { "8level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1310 { "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
1311 { "log", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
1312 { "ch_mode", "Stereo decorrelation mode", offsetof(FlacEncodeContext, options.ch_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FLAC_CHMODE_MID_SIDE, FLAGS, "ch_mode" },
1313 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1314 { "indep", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1315 { "left_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1316 { "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1317 { "mid_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1318 { NULL },
1319 };
1320 
1321 static const AVClass flac_encoder_class = {
1322  "FLAC encoder",
1324  options,
1326 };
1327 
1329  .name = "flac",
1330  .type = AVMEDIA_TYPE_AUDIO,
1331  .id = AV_CODEC_ID_FLAC,
1332  .priv_data_size = sizeof(FlacEncodeContext),
1334  .encode2 = flac_encode_frame,
1336  .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
1337  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
1340  .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
1341  .priv_class = &flac_encoder_class,
1342 };