FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
alacenc.c
Go to the documentation of this file.
1 /*
2  * ALAC audio encoder
3  * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
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/opt.h"
23 
24 #include "avcodec.h"
25 #include "put_bits.h"
26 #include "internal.h"
27 #include "lpc.h"
28 #include "mathops.h"
29 #include "alac_data.h"
30 
31 #define DEFAULT_FRAME_SIZE 4096
32 #define ALAC_EXTRADATA_SIZE 36
33 #define ALAC_FRAME_HEADER_SIZE 55
34 #define ALAC_FRAME_FOOTER_SIZE 3
35 
36 #define ALAC_ESCAPE_CODE 0x1FF
37 #define ALAC_MAX_LPC_ORDER 30
38 #define DEFAULT_MAX_PRED_ORDER 6
39 #define DEFAULT_MIN_PRED_ORDER 4
40 #define ALAC_MAX_LPC_PRECISION 9
41 #define ALAC_MIN_LPC_SHIFT 0
42 #define ALAC_MAX_LPC_SHIFT 9
43 
44 #define ALAC_CHMODE_LEFT_RIGHT 0
45 #define ALAC_CHMODE_LEFT_SIDE 1
46 #define ALAC_CHMODE_RIGHT_SIDE 2
47 #define ALAC_CHMODE_MID_SIDE 3
48 
49 typedef struct RiceContext {
54 } RiceContext;
55 
56 typedef struct AlacLPCContext {
57  int lpc_order;
59  int lpc_quant;
61 
62 typedef struct AlacEncodeContext {
63  const AVClass *class;
65  int frame_size; /**< current frame size */
66  int verbatim; /**< current frame verbatim mode flag */
82 
83 
84 static void init_sample_buffers(AlacEncodeContext *s, int channels,
85  const uint8_t *samples[2])
86 {
87  int ch, i;
90 
91 #define COPY_SAMPLES(type) do { \
92  for (ch = 0; ch < channels; ch++) { \
93  int32_t *bptr = s->sample_buf[ch]; \
94  const type *sptr = (const type *)samples[ch]; \
95  for (i = 0; i < s->frame_size; i++) \
96  bptr[i] = sptr[i] >> shift; \
97  } \
98  } while (0)
99 
102  else
103  COPY_SAMPLES(int16_t);
104 }
105 
106 static void encode_scalar(AlacEncodeContext *s, int x,
107  int k, int write_sample_size)
108 {
109  int divisor, q, r;
110 
111  k = FFMIN(k, s->rc.k_modifier);
112  divisor = (1<<k) - 1;
113  q = x / divisor;
114  r = x % divisor;
115 
116  if (q > 8) {
117  // write escape code and sample value directly
119  put_bits(&s->pbctx, write_sample_size, x);
120  } else {
121  if (q)
122  put_bits(&s->pbctx, q, (1<<q) - 1);
123  put_bits(&s->pbctx, 1, 0);
124 
125  if (k != 1) {
126  if (r > 0)
127  put_bits(&s->pbctx, k, r+1);
128  else
129  put_bits(&s->pbctx, k-1, 0);
130  }
131  }
132 }
133 
135  enum AlacRawDataBlockType element,
136  int instance)
137 {
138  int encode_fs = 0;
139 
141  encode_fs = 1;
142 
143  put_bits(&s->pbctx, 3, element); // element type
144  put_bits(&s->pbctx, 4, instance); // element instance
145  put_bits(&s->pbctx, 12, 0); // unused header bits
146  put_bits(&s->pbctx, 1, encode_fs); // Sample count is in the header
147  put_bits(&s->pbctx, 2, s->extra_bits >> 3); // Extra bytes (for 24-bit)
148  put_bits(&s->pbctx, 1, s->verbatim); // Audio block is verbatim
149  if (encode_fs)
150  put_bits32(&s->pbctx, s->frame_size); // No. of samples in the frame
151 }
152 
154 {
156  int shift[MAX_LPC_ORDER];
157  int opt_order;
158 
159  if (s->compression_level == 1) {
160  s->lpc[ch].lpc_order = 6;
161  s->lpc[ch].lpc_quant = 6;
162  s->lpc[ch].lpc_coeff[0] = 160;
163  s->lpc[ch].lpc_coeff[1] = -190;
164  s->lpc[ch].lpc_coeff[2] = 170;
165  s->lpc[ch].lpc_coeff[3] = -130;
166  s->lpc[ch].lpc_coeff[4] = 80;
167  s->lpc[ch].lpc_coeff[5] = -25;
168  } else {
169  opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, s->sample_buf[ch],
170  s->frame_size,
173  ALAC_MAX_LPC_PRECISION, coefs, shift,
176  ALAC_MAX_LPC_SHIFT, 1);
177 
178  s->lpc[ch].lpc_order = opt_order;
179  s->lpc[ch].lpc_quant = shift[opt_order-1];
180  memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
181  }
182 }
183 
184 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
185 {
186  int i, best;
187  int32_t lt, rt;
188  uint64_t sum[4];
189  uint64_t score[4];
190 
191  /* calculate sum of 2nd order residual for each channel */
192  sum[0] = sum[1] = sum[2] = sum[3] = 0;
193  for (i = 2; i < n; i++) {
194  lt = left_ch[i] - 2 * left_ch[i - 1] + left_ch[i - 2];
195  rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
196  sum[2] += FFABS((lt + rt) >> 1);
197  sum[3] += FFABS(lt - rt);
198  sum[0] += FFABS(lt);
199  sum[1] += FFABS(rt);
200  }
201 
202  /* calculate score for each mode */
203  score[0] = sum[0] + sum[1];
204  score[1] = sum[0] + sum[3];
205  score[2] = sum[1] + sum[3];
206  score[3] = sum[2] + sum[3];
207 
208  /* return mode with lowest score */
209  best = 0;
210  for (i = 1; i < 4; i++) {
211  if (score[i] < score[best])
212  best = i;
213  }
214  return best;
215 }
216 
218 {
219  int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
220  int i, mode, n = s->frame_size;
221  int32_t tmp;
222 
223  mode = estimate_stereo_mode(left, right, n);
224 
225  switch (mode) {
227  s->interlacing_leftweight = 0;
228  s->interlacing_shift = 0;
229  break;
231  for (i = 0; i < n; i++)
232  right[i] = left[i] - right[i];
233  s->interlacing_leftweight = 1;
234  s->interlacing_shift = 0;
235  break;
237  for (i = 0; i < n; i++) {
238  tmp = right[i];
239  right[i] = left[i] - right[i];
240  left[i] = tmp + (right[i] >> 31);
241  }
242  s->interlacing_leftweight = 1;
243  s->interlacing_shift = 31;
244  break;
245  default:
246  for (i = 0; i < n; i++) {
247  tmp = left[i];
248  left[i] = (tmp + right[i]) >> 1;
249  right[i] = tmp - right[i];
250  }
251  s->interlacing_leftweight = 1;
252  s->interlacing_shift = 1;
253  break;
254  }
255 }
256 
258 {
259  int i;
260  AlacLPCContext lpc = s->lpc[ch];
262 
263  if (lpc.lpc_order == 31) {
264  residual[0] = s->sample_buf[ch][0];
265 
266  for (i = 1; i < s->frame_size; i++) {
267  residual[i] = s->sample_buf[ch][i ] -
268  s->sample_buf[ch][i - 1];
269  }
270 
271  return;
272  }
273 
274  // generalised linear predictor
275 
276  if (lpc.lpc_order > 0) {
277  int32_t *samples = s->sample_buf[ch];
278 
279  // generate warm-up samples
280  residual[0] = samples[0];
281  for (i = 1; i <= lpc.lpc_order; i++)
282  residual[i] = sign_extend(samples[i] - samples[i-1], s->write_sample_size);
283 
284  // perform lpc on remaining samples
285  for (i = lpc.lpc_order + 1; i < s->frame_size; i++) {
286  int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
287 
288  for (j = 0; j < lpc.lpc_order; j++) {
289  sum += (samples[lpc.lpc_order-j] - samples[0]) *
290  lpc.lpc_coeff[j];
291  }
292 
293  sum >>= lpc.lpc_quant;
294  sum += samples[0];
295  residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
296  s->write_sample_size);
297  res_val = residual[i];
298 
299  if (res_val) {
300  int index = lpc.lpc_order - 1;
301  int neg = (res_val < 0);
302 
303  while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
304  int val = samples[0] - samples[lpc.lpc_order - index];
305  int sign = (val ? FFSIGN(val) : 0);
306 
307  if (neg)
308  sign *= -1;
309 
310  lpc.lpc_coeff[index] -= sign;
311  val *= sign;
312  res_val -= (val >> lpc.lpc_quant) * (lpc.lpc_order - index);
313  index--;
314  }
315  }
316  samples++;
317  }
318  }
319 }
320 
322 {
323  unsigned int history = s->rc.initial_history;
324  int sign_modifier = 0, i, k;
325  int32_t *samples = s->predictor_buf[ch];
326 
327  for (i = 0; i < s->frame_size;) {
328  int x;
329 
330  k = av_log2((history >> 9) + 3);
331 
332  x = -2 * (*samples) -1;
333  x ^= x >> 31;
334 
335  samples++;
336  i++;
337 
338  encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
339 
340  history += x * s->rc.history_mult -
341  ((history * s->rc.history_mult) >> 9);
342 
343  sign_modifier = 0;
344  if (x > 0xFFFF)
345  history = 0xFFFF;
346 
347  if (history < 128 && i < s->frame_size) {
348  unsigned int block_size = 0;
349 
350  k = 7 - av_log2(history) + ((history + 16) >> 6);
351 
352  while (*samples == 0 && i < s->frame_size) {
353  samples++;
354  i++;
355  block_size++;
356  }
357  encode_scalar(s, block_size, k, 16);
358  sign_modifier = (block_size <= 0xFFFF);
359  history = 0;
360  }
361 
362  }
363 }
364 
366  enum AlacRawDataBlockType element, int instance,
367  const uint8_t *samples0, const uint8_t *samples1)
368 {
369  const uint8_t *samples[2] = { samples0, samples1 };
370  int i, j, channels;
371  int prediction_type = 0;
372  PutBitContext *pb = &s->pbctx;
373 
374  channels = element == TYPE_CPE ? 2 : 1;
375 
376  if (s->verbatim) {
377  write_element_header(s, element, instance);
378  /* samples are channel-interleaved in verbatim mode */
379  if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
380  int shift = 32 - s->avctx->bits_per_raw_sample;
381  const int32_t *samples_s32[2] = { (const int32_t *)samples0,
382  (const int32_t *)samples1 };
383  for (i = 0; i < s->frame_size; i++)
384  for (j = 0; j < channels; j++)
386  samples_s32[j][i] >> shift);
387  } else {
388  const int16_t *samples_s16[2] = { (const int16_t *)samples0,
389  (const int16_t *)samples1 };
390  for (i = 0; i < s->frame_size; i++)
391  for (j = 0; j < channels; j++)
393  samples_s16[j][i]);
394  }
395  } else {
397  channels - 1;
398 
399  init_sample_buffers(s, channels, samples);
400  write_element_header(s, element, instance);
401 
402  // extract extra bits if needed
403  if (s->extra_bits) {
404  uint32_t mask = (1 << s->extra_bits) - 1;
405  for (j = 0; j < channels; j++) {
406  int32_t *extra = s->predictor_buf[j];
407  int32_t *smp = s->sample_buf[j];
408  for (i = 0; i < s->frame_size; i++) {
409  extra[i] = smp[i] & mask;
410  smp[i] >>= s->extra_bits;
411  }
412  }
413  }
414 
415  if (channels == 2)
417  else
419  put_bits(pb, 8, s->interlacing_shift);
420  put_bits(pb, 8, s->interlacing_leftweight);
421 
422  for (i = 0; i < channels; i++) {
423  calc_predictor_params(s, i);
424 
425  put_bits(pb, 4, prediction_type);
426  put_bits(pb, 4, s->lpc[i].lpc_quant);
427 
428  put_bits(pb, 3, s->rc.rice_modifier);
429  put_bits(pb, 5, s->lpc[i].lpc_order);
430  // predictor coeff. table
431  for (j = 0; j < s->lpc[i].lpc_order; j++)
432  put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]);
433  }
434 
435  // write extra bits if needed
436  if (s->extra_bits) {
437  for (i = 0; i < s->frame_size; i++) {
438  for (j = 0; j < channels; j++) {
439  put_bits(pb, s->extra_bits, s->predictor_buf[j][i]);
440  }
441  }
442  }
443 
444  // apply lpc and entropy coding to audio samples
445  for (i = 0; i < channels; i++) {
446  alac_linear_predictor(s, i);
447 
448  // TODO: determine when this will actually help. for now it's not used.
449  if (prediction_type == 15) {
450  // 2nd pass 1st order filter
451  int32_t *residual = s->predictor_buf[i];
452  for (j = s->frame_size - 1; j > 0; j--)
453  residual[j] -= residual[j - 1];
454  }
455  alac_entropy_coder(s, i);
456  }
457  }
458 }
459 
461  uint8_t * const *samples)
462 {
463  PutBitContext *pb = &s->pbctx;
464  const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[s->avctx->channels - 1];
465  const uint8_t *ch_map = ff_alac_channel_layout_offsets[s->avctx->channels - 1];
466  int ch, element, sce, cpe;
467 
468  init_put_bits(pb, avpkt->data, avpkt->size);
469 
470  ch = element = sce = cpe = 0;
471  while (ch < s->avctx->channels) {
472  if (ch_elements[element] == TYPE_CPE) {
473  write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
474  samples[ch_map[ch + 1]]);
475  cpe++;
476  ch += 2;
477  } else {
478  write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL);
479  sce++;
480  ch++;
481  }
482  element++;
483  }
484 
485  put_bits(pb, 3, TYPE_END);
486  flush_put_bits(pb);
487 
488  return put_bits_count(pb) >> 3;
489 }
490 
492 {
493  int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
494  return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
495 }
496 
498 {
499  AlacEncodeContext *s = avctx->priv_data;
500  ff_lpc_end(&s->lpc_ctx);
501  av_freep(&avctx->extradata);
502  avctx->extradata_size = 0;
503  return 0;
504 }
505 
507 {
508  AlacEncodeContext *s = avctx->priv_data;
509  int ret;
510  uint8_t *alac_extradata;
511 
513 
514  if (avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
515  if (avctx->bits_per_raw_sample != 24)
516  av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
517  avctx->bits_per_raw_sample = 24;
518  } else {
519  avctx->bits_per_raw_sample = 16;
520  s->extra_bits = 0;
521  }
522 
523  // Set default compression level
525  s->compression_level = 2;
526  else
527  s->compression_level = av_clip(avctx->compression_level, 0, 2);
528 
529  // Initialize default Rice parameters
530  s->rc.history_mult = 40;
531  s->rc.initial_history = 10;
532  s->rc.k_modifier = 14;
533  s->rc.rice_modifier = 4;
534 
536  avctx->channels,
537  avctx->bits_per_raw_sample);
538 
540  if (!avctx->extradata) {
541  ret = AVERROR(ENOMEM);
542  goto error;
543  }
545 
546  alac_extradata = avctx->extradata;
547  AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
548  AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
549  AV_WB32(alac_extradata+12, avctx->frame_size);
550  AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
551  AV_WB8 (alac_extradata+21, avctx->channels);
552  AV_WB32(alac_extradata+24, s->max_coded_frame_size);
553  AV_WB32(alac_extradata+28,
554  avctx->sample_rate * avctx->channels * avctx->bits_per_raw_sample); // average bitrate
555  AV_WB32(alac_extradata+32, avctx->sample_rate);
556 
557  // Set relevant extradata fields
558  if (s->compression_level > 0) {
559  AV_WB8(alac_extradata+18, s->rc.history_mult);
560  AV_WB8(alac_extradata+19, s->rc.initial_history);
561  AV_WB8(alac_extradata+20, s->rc.k_modifier);
562  }
563 
564 #if FF_API_PRIVATE_OPT
566  if (avctx->min_prediction_order >= 0) {
567  if (avctx->min_prediction_order < MIN_LPC_ORDER ||
569  av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
570  avctx->min_prediction_order);
571  ret = AVERROR(EINVAL);
572  goto error;
573  }
574 
576  }
577 
578  if (avctx->max_prediction_order >= 0) {
579  if (avctx->max_prediction_order < MIN_LPC_ORDER ||
581  av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
582  avctx->max_prediction_order);
583  ret = AVERROR(EINVAL);
584  goto error;
585  }
586 
588  }
590 #endif
591 
593  av_log(avctx, AV_LOG_ERROR,
594  "invalid prediction orders: min=%d max=%d\n",
596  ret = AVERROR(EINVAL);
597  goto error;
598  }
599 
600  s->avctx = avctx;
601 
602  if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
604  FF_LPC_TYPE_LEVINSON)) < 0) {
605  goto error;
606  }
607 
608  return 0;
609 error:
610  alac_encode_close(avctx);
611  return ret;
612 }
613 
614 static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
615  const AVFrame *frame, int *got_packet_ptr)
616 {
617  AlacEncodeContext *s = avctx->priv_data;
618  int out_bytes, max_frame_size, ret;
619 
620  s->frame_size = frame->nb_samples;
621 
622  if (frame->nb_samples < DEFAULT_FRAME_SIZE)
623  max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
624  avctx->bits_per_raw_sample);
625  else
626  max_frame_size = s->max_coded_frame_size;
627 
628  if ((ret = ff_alloc_packet2(avctx, avpkt, 4 * max_frame_size, 0)) < 0)
629  return ret;
630 
631  /* use verbatim mode for compression_level 0 */
632  if (s->compression_level) {
633  s->verbatim = 0;
634  s->extra_bits = avctx->bits_per_raw_sample - 16;
635  } else {
636  s->verbatim = 1;
637  s->extra_bits = 0;
638  }
639 
640  out_bytes = write_frame(s, avpkt, frame->extended_data);
641 
642  if (out_bytes > max_frame_size) {
643  /* frame too large. use verbatim mode */
644  s->verbatim = 1;
645  s->extra_bits = 0;
646  out_bytes = write_frame(s, avpkt, frame->extended_data);
647  }
648 
649  avpkt->size = out_bytes;
650  *got_packet_ptr = 1;
651  return 0;
652 }
653 
654 #define OFFSET(x) offsetof(AlacEncodeContext, x)
655 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
656 static const AVOption options[] = {
657  { "min_prediction_order", NULL, OFFSET(min_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MIN_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE },
658  { "max_prediction_order", NULL, OFFSET(max_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MAX_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE },
659 
660  { NULL },
661 };
662 
663 static const AVClass alacenc_class = {
664  .class_name = "alacenc",
665  .item_name = av_default_item_name,
666  .option = options,
667  .version = LIBAVUTIL_VERSION_INT,
668 };
669 
671  .name = "alac",
672  .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
673  .type = AVMEDIA_TYPE_AUDIO,
674  .id = AV_CODEC_ID_ALAC,
675  .priv_data_size = sizeof(AlacEncodeContext),
676  .priv_class = &alacenc_class,
678  .encode2 = alac_encode_frame,
679  .close = alac_encode_close,
680  .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
682  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
685 };
#define DEFAULT_MIN_PRED_ORDER
Definition: alacenc.c:39
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:210
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
const char * s
Definition: avisynth_c.h:768
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:1820
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
AVOption.
Definition: opt.h:246
Definition: lpc.h:52
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:200
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
LPCContext lpc_ctx
Definition: alacenc.c:80
#define ALAC_ESCAPE_CODE
Definition: alacenc.c:36
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
#define MAX_LPC_ORDER
Definition: lpc.h:38
Definition: aac.h:63
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
Definition: aac.h:56
Definition: aac.h:57
int size
Definition: avcodec.h:1658
#define AV_WB8(p, d)
Definition: intreadwrite.h:401
int av_log2(unsigned v)
Definition: intmath.c:26
#define COPY_SAMPLES(type)
PutBitContext pbctx
Definition: alacenc.c:77
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static void write_element_header(AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance)
Definition: alacenc.c:134
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3133
int history_mult
Definition: alacenc.c:50
static av_cold int alac_encode_init(AVCodecContext *avctx)
Definition: alacenc.c:506
#define DEFAULT_MAX_PRED_ORDER
Definition: alacenc.c:38
AVCodec.
Definition: avcodec.h:3681
int initial_history
Definition: alacenc.c:51
static void write_element(AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance, const uint8_t *samples0, const uint8_t *samples1)
Definition: alacenc.c:365
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int lpc_quant
Definition: alacenc.c:59
static const AVClass alacenc_class
Definition: alacenc.c:663
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2502
uint8_t
#define av_cold
Definition: attributes.h:82
static void alac_linear_predictor(AlacEncodeContext *s, int ch)
Definition: alacenc.c:257
AVOptions.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1847
int max_coded_frame_size
Definition: alacenc.c:70
static AVFrame * frame
int interlacing_leftweight
Definition: alacenc.c:76
int interlacing_shift
Definition: alacenc.c:75
AVCodecContext * avctx
Definition: alacenc.c:64
uint8_t * data
Definition: avcodec.h:1657
AVCodec ff_alac_encoder
Definition: alacenc.c:670
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void calc_predictor_params(AlacEncodeContext *s, int ch)
Definition: alacenc.c:153
static const uint16_t mask[17]
Definition: lzw.c:38
static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: alacenc.c:614
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define ALAC_MAX_LPC_PRECISION
Definition: alacenc.c:40
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
const char * r
Definition: vf_curves.c:111
AlacRawDataBlockType
Definition: alac_data.h:26
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
#define AE
Definition: alacenc.c:655
static void alac_entropy_coder(AlacEncodeContext *s, int ch)
Definition: alacenc.c:321
int frame_size
current frame size
Definition: alacenc.c:65
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
uint64_t residual
Definition: dirac_vlc.h:29
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:322
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: avcodec.h:1024
#define FFMIN(a, b)
Definition: common.h:96
signed 32 bits, planar
Definition: samplefmt.h:68
int verbatim
current frame verbatim mode flag
Definition: alacenc.c:66
#define FFSIGN(a)
Definition: common.h:73
int32_t
int k_modifier
Definition: alacenc.c:52
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
Definition: alacenc.c:491
int n
Definition: avisynth_c.h:684
const uint64_t ff_alac_channel_layouts[ALAC_MAX_CHANNELS+1]
Definition: alac_data.c:35
int max_prediction_order
Definition: alacenc.c:69
static void error(const char *err)
static const AVOption options[]
Definition: alacenc.c:656
#define ALAC_CHMODE_LEFT_SIDE
Definition: alacenc.c:45
#define ALAC_MAX_LPC_ORDER
Definition: alacenc.c:37
AlacLPCContext lpc[2]
Definition: alacenc.c:79
attribute_deprecated int max_prediction_order
Definition: avcodec.h:2840
#define ALAC_MIN_LPC_SHIFT
Definition: alacenc.c:41
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2514
int frame_size
Definition: mxfenc.c:1820
int32_t sample_buf[2][DEFAULT_FRAME_SIZE]
Definition: alacenc.c:73
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int compression_level
Definition: avcodec.h:1819
int sample_rate
samples per second
Definition: avcodec.h:2494
#define MIN_LPC_ORDER
Definition: lpc.h:37
#define ALAC_MAX_LPC_SHIFT
Definition: alacenc.c:42
main external API structure.
Definition: avcodec.h:1732
int compression_level
Definition: alacenc.c:67
Levinson-Durbin recursion.
Definition: lpc.h:47
#define ORDER_METHOD_EST
Definition: lpc.h:30
int extradata_size
Definition: avcodec.h:1848
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:118
int32_t predictor_buf[2][DEFAULT_FRAME_SIZE]
Definition: alacenc.c:74
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_WB32(p, v)
Definition: intreadwrite.h:424
int index
Definition: gxfenc.c:89
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1736
int ff_lpc_calc_coefs(LPCContext *s, const int32_t *samples, int blocksize, int min_order, int max_order, int precision, int32_t coefs[][MAX_LPC_ORDER], int *shift, enum FFLPCType lpc_type, int lpc_passes, int omethod, int min_shift, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
Definition: lpc.c:200
const uint8_t ff_alac_channel_layout_offsets[ALAC_MAX_CHANNELS][ALAC_MAX_CHANNELS]
Definition: alac_data.c:24
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition: lpc.c:300
static int write_frame(AlacEncodeContext *s, AVPacket *avpkt, uint8_t *const *samples)
Definition: alacenc.c:460
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:130
static void init_sample_buffers(AlacEncodeContext *s, int channels, const uint8_t *samples[2])
Definition: alacenc.c:84
enum AlacRawDataBlockType ff_alac_channel_elements[ALAC_MAX_CHANNELS][5]
Definition: alac_data.c:47
int lpc_order
Definition: alacenc.c:57
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
Definition: alacenc.c:184
int rice_modifier
Definition: alacenc.c:53
int min_prediction_order
Definition: alacenc.c:68
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
#define ALAC_CHMODE_LEFT_RIGHT
Definition: alacenc.c:44
#define ALAC_CHMODE_RIGHT_SIDE
Definition: alacenc.c:46
RiceContext rc
Definition: alacenc.c:78
static av_cold int alac_encode_close(AVCodecContext *avctx)
Definition: alacenc.c:497
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_YASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
unsigned bps
Definition: movenc.c:1414
#define MKBETAG(a, b, c, d)
Definition: common.h:343
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:769
void * priv_data
Definition: avcodec.h:1774
int lpc_coeff[ALAC_MAX_LPC_ORDER+1]
Definition: alacenc.c:58
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
attribute_deprecated int min_prediction_order
Definition: avcodec.h:2836
int channels
number of audio channels
Definition: avcodec.h:2495
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
signed 16 bits, planar
Definition: samplefmt.h:67
#define av_always_inline
Definition: attributes.h:39
#define ALAC_EXTRADATA_SIZE
Definition: alacenc.c:32
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:234
#define DEFAULT_FRAME_SIZE
Definition: alacenc.c:31
This structure stores compressed data.
Definition: avcodec.h:1634
static void alac_stereo_decorrelation(AlacEncodeContext *s)
Definition: alacenc.c:217
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:244
static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
Definition: alacenc.c:106
#define OFFSET(x)
Definition: alacenc.c:654
int write_sample_size
Definition: alacenc.c:71
static uint8_t tmp[11]
Definition: aes_ctr.c:26
bitstream writer API