Libav
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 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/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_MAX_LPC_SHIFT 9
42 
43 #define ALAC_CHMODE_LEFT_RIGHT 0
44 #define ALAC_CHMODE_LEFT_SIDE 1
45 #define ALAC_CHMODE_RIGHT_SIDE 2
46 #define ALAC_CHMODE_MID_SIDE 3
47 
48 typedef struct RiceContext {
53 } RiceContext;
54 
55 typedef struct AlacLPCContext {
56  int lpc_order;
57  int lpc_coeff[ALAC_MAX_LPC_ORDER+1];
58  int lpc_quant;
60 
61 typedef struct AlacEncodeContext {
62  const AVClass *class;
64  int frame_size;
65  int verbatim;
72  int32_t sample_buf[2][DEFAULT_FRAME_SIZE];
73  int32_t predictor_buf[DEFAULT_FRAME_SIZE];
81 
82 
83 static void init_sample_buffers(AlacEncodeContext *s, int channels,
84  const uint8_t *samples[2])
85 {
86  int ch, i;
87  int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
89 
90 #define COPY_SAMPLES(type) do { \
91  for (ch = 0; ch < channels; ch++) { \
92  int32_t *bptr = s->sample_buf[ch]; \
93  const type *sptr = (const type *)samples[ch]; \
94  for (i = 0; i < s->frame_size; i++) \
95  bptr[i] = sptr[i] >> shift; \
96  } \
97  } while (0)
98 
101  else
102  COPY_SAMPLES(int16_t);
103 }
104 
105 static void encode_scalar(AlacEncodeContext *s, int x,
106  int k, int write_sample_size)
107 {
108  int divisor, q, r;
109 
110  k = FFMIN(k, s->rc.k_modifier);
111  divisor = (1<<k) - 1;
112  q = x / divisor;
113  r = x % divisor;
114 
115  if (q > 8) {
116  // write escape code and sample value directly
118  put_bits(&s->pbctx, write_sample_size, x);
119  } else {
120  if (q)
121  put_bits(&s->pbctx, q, (1<<q) - 1);
122  put_bits(&s->pbctx, 1, 0);
123 
124  if (k != 1) {
125  if (r > 0)
126  put_bits(&s->pbctx, k, r+1);
127  else
128  put_bits(&s->pbctx, k-1, 0);
129  }
130  }
131 }
132 
134  enum AlacRawDataBlockType element,
135  int instance)
136 {
137  int encode_fs = 0;
138 
140  encode_fs = 1;
141 
142  put_bits(&s->pbctx, 3, element); // element type
143  put_bits(&s->pbctx, 4, instance); // element instance
144  put_bits(&s->pbctx, 12, 0); // unused header bits
145  put_bits(&s->pbctx, 1, encode_fs); // Sample count is in the header
146  put_bits(&s->pbctx, 2, s->extra_bits >> 3); // Extra bytes (for 24-bit)
147  put_bits(&s->pbctx, 1, s->verbatim); // Audio block is verbatim
148  if (encode_fs)
149  put_bits32(&s->pbctx, s->frame_size); // No. of samples in the frame
150 }
151 
153 {
155  int shift[MAX_LPC_ORDER];
156  int opt_order;
157 
158  if (s->compression_level == 1) {
159  s->lpc[ch].lpc_order = 6;
160  s->lpc[ch].lpc_quant = 6;
161  s->lpc[ch].lpc_coeff[0] = 160;
162  s->lpc[ch].lpc_coeff[1] = -190;
163  s->lpc[ch].lpc_coeff[2] = 170;
164  s->lpc[ch].lpc_coeff[3] = -130;
165  s->lpc[ch].lpc_coeff[4] = 80;
166  s->lpc[ch].lpc_coeff[5] = -25;
167  } else {
168  opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, s->sample_buf[ch],
169  s->frame_size,
172  ALAC_MAX_LPC_PRECISION, coefs, shift,
175 
176  s->lpc[ch].lpc_order = opt_order;
177  s->lpc[ch].lpc_quant = shift[opt_order-1];
178  memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
179  }
180 }
181 
182 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
183 {
184  int i, best;
185  int32_t lt, rt;
186  uint64_t sum[4];
187  uint64_t score[4];
188 
189  /* calculate sum of 2nd order residual for each channel */
190  sum[0] = sum[1] = sum[2] = sum[3] = 0;
191  for (i = 2; i < n; i++) {
192  lt = left_ch[i] - 2 * left_ch[i - 1] + left_ch[i - 2];
193  rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
194  sum[2] += FFABS((lt + rt) >> 1);
195  sum[3] += FFABS(lt - rt);
196  sum[0] += FFABS(lt);
197  sum[1] += FFABS(rt);
198  }
199 
200  /* calculate score for each mode */
201  score[0] = sum[0] + sum[1];
202  score[1] = sum[0] + sum[3];
203  score[2] = sum[1] + sum[3];
204  score[3] = sum[2] + sum[3];
205 
206  /* return mode with lowest score */
207  best = 0;
208  for (i = 1; i < 4; i++) {
209  if (score[i] < score[best])
210  best = i;
211  }
212  return best;
213 }
214 
216 {
217  int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
218  int i, mode, n = s->frame_size;
219  int32_t tmp;
220 
221  mode = estimate_stereo_mode(left, right, n);
222 
223  switch (mode) {
225  s->interlacing_leftweight = 0;
226  s->interlacing_shift = 0;
227  break;
229  for (i = 0; i < n; i++)
230  right[i] = left[i] - right[i];
231  s->interlacing_leftweight = 1;
232  s->interlacing_shift = 0;
233  break;
235  for (i = 0; i < n; i++) {
236  tmp = right[i];
237  right[i] = left[i] - right[i];
238  left[i] = tmp + (right[i] >> 31);
239  }
240  s->interlacing_leftweight = 1;
241  s->interlacing_shift = 31;
242  break;
243  default:
244  for (i = 0; i < n; i++) {
245  tmp = left[i];
246  left[i] = (tmp + right[i]) >> 1;
247  right[i] = tmp - right[i];
248  }
249  s->interlacing_leftweight = 1;
250  s->interlacing_shift = 1;
251  break;
252  }
253 }
254 
256 {
257  int i;
258  AlacLPCContext lpc = s->lpc[ch];
259 
260  if (lpc.lpc_order == 31) {
261  s->predictor_buf[0] = s->sample_buf[ch][0];
262 
263  for (i = 1; i < s->frame_size; i++) {
264  s->predictor_buf[i] = s->sample_buf[ch][i ] -
265  s->sample_buf[ch][i - 1];
266  }
267 
268  return;
269  }
270 
271  // generalised linear predictor
272 
273  if (lpc.lpc_order > 0) {
274  int32_t *samples = s->sample_buf[ch];
275  int32_t *residual = s->predictor_buf;
276 
277  // generate warm-up samples
278  residual[0] = samples[0];
279  for (i = 1; i <= lpc.lpc_order; i++)
280  residual[i] = samples[i] - samples[i-1];
281 
282  // perform lpc on remaining samples
283  for (i = lpc.lpc_order + 1; i < s->frame_size; i++) {
284  int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
285 
286  for (j = 0; j < lpc.lpc_order; j++) {
287  sum += (samples[lpc.lpc_order-j] - samples[0]) *
288  lpc.lpc_coeff[j];
289  }
290 
291  sum >>= lpc.lpc_quant;
292  sum += samples[0];
293  residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
294  s->write_sample_size);
295  res_val = residual[i];
296 
297  if (res_val) {
298  int index = lpc.lpc_order - 1;
299  int neg = (res_val < 0);
300 
301  while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
302  int val = samples[0] - samples[lpc.lpc_order - index];
303  int sign = (val ? FFSIGN(val) : 0);
304 
305  if (neg)
306  sign *= -1;
307 
308  lpc.lpc_coeff[index] -= sign;
309  val *= sign;
310  res_val -= (val >> lpc.lpc_quant) * (lpc.lpc_order - index);
311  index--;
312  }
313  }
314  samples++;
315  }
316  }
317 }
318 
320 {
321  unsigned int history = s->rc.initial_history;
322  int sign_modifier = 0, i, k;
323  int32_t *samples = s->predictor_buf;
324 
325  for (i = 0; i < s->frame_size;) {
326  int x;
327 
328  k = av_log2((history >> 9) + 3);
329 
330  x = -2 * (*samples) -1;
331  x ^= x >> 31;
332 
333  samples++;
334  i++;
335 
336  encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
337 
338  history += x * s->rc.history_mult -
339  ((history * s->rc.history_mult) >> 9);
340 
341  sign_modifier = 0;
342  if (x > 0xFFFF)
343  history = 0xFFFF;
344 
345  if (history < 128 && i < s->frame_size) {
346  unsigned int block_size = 0;
347 
348  k = 7 - av_log2(history) + ((history + 16) >> 6);
349 
350  while (*samples == 0 && i < s->frame_size) {
351  samples++;
352  i++;
353  block_size++;
354  }
355  encode_scalar(s, block_size, k, 16);
356  sign_modifier = (block_size <= 0xFFFF);
357  history = 0;
358  }
359 
360  }
361 }
362 
364  enum AlacRawDataBlockType element, int instance,
365  const uint8_t *samples0, const uint8_t *samples1)
366 {
367  const uint8_t *samples[2] = { samples0, samples1 };
368  int i, j, channels;
369  int prediction_type = 0;
370  PutBitContext *pb = &s->pbctx;
371 
372  channels = element == TYPE_CPE ? 2 : 1;
373 
374  if (s->verbatim) {
375  write_element_header(s, element, instance);
376  /* samples are channel-interleaved in verbatim mode */
377  if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
378  int shift = 32 - s->avctx->bits_per_raw_sample;
379  const int32_t *samples_s32[2] = { (const int32_t *)samples0,
380  (const int32_t *)samples1 };
381  for (i = 0; i < s->frame_size; i++)
382  for (j = 0; j < channels; j++)
384  samples_s32[j][i] >> shift);
385  } else {
386  const int16_t *samples_s16[2] = { (const int16_t *)samples0,
387  (const int16_t *)samples1 };
388  for (i = 0; i < s->frame_size; i++)
389  for (j = 0; j < channels; j++)
391  samples_s16[j][i]);
392  }
393  } else {
395  channels - 1;
396 
397  init_sample_buffers(s, channels, samples);
398  write_element_header(s, element, instance);
399 
400  if (channels == 2)
402  else
404  put_bits(pb, 8, s->interlacing_shift);
405  put_bits(pb, 8, s->interlacing_leftweight);
406 
407  for (i = 0; i < channels; i++) {
408  calc_predictor_params(s, i);
409 
410  put_bits(pb, 4, prediction_type);
411  put_bits(pb, 4, s->lpc[i].lpc_quant);
412 
413  put_bits(pb, 3, s->rc.rice_modifier);
414  put_bits(pb, 5, s->lpc[i].lpc_order);
415  // predictor coeff. table
416  for (j = 0; j < s->lpc[i].lpc_order; j++)
417  put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]);
418  }
419 
420  // write extra bits if needed
421  if (s->extra_bits) {
422  uint32_t mask = (1 << s->extra_bits) - 1;
423  for (i = 0; i < s->frame_size; i++) {
424  for (j = 0; j < channels; j++) {
425  put_bits(pb, s->extra_bits, s->sample_buf[j][i] & mask);
426  s->sample_buf[j][i] >>= s->extra_bits;
427  }
428  }
429  }
430 
431  // apply lpc and entropy coding to audio samples
432  for (i = 0; i < channels; i++) {
433  alac_linear_predictor(s, i);
434 
435  // TODO: determine when this will actually help. for now it's not used.
436  if (prediction_type == 15) {
437  // 2nd pass 1st order filter
438  for (j = s->frame_size - 1; j > 0; j--)
439  s->predictor_buf[j] -= s->predictor_buf[j - 1];
440  }
442  }
443  }
444 }
445 
446 static int write_frame(AlacEncodeContext *s, AVPacket *avpkt,
447  uint8_t * const *samples)
448 {
449  PutBitContext *pb = &s->pbctx;
450  const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[s->avctx->channels - 1];
451  const uint8_t *ch_map = ff_alac_channel_layout_offsets[s->avctx->channels - 1];
452  int ch, element, sce, cpe;
453 
454  init_put_bits(pb, avpkt->data, avpkt->size);
455 
456  ch = element = sce = cpe = 0;
457  while (ch < s->avctx->channels) {
458  if (ch_elements[element] == TYPE_CPE) {
459  write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
460  samples[ch_map[ch + 1]]);
461  cpe++;
462  ch += 2;
463  } else {
464  write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL);
465  sce++;
466  ch++;
467  }
468  element++;
469  }
470 
471  put_bits(pb, 3, TYPE_END);
472  flush_put_bits(pb);
473 
474  return put_bits_count(pb) >> 3;
475 }
476 
477 static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
478 {
479  int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
480  return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
481 }
482 
484 {
485  AlacEncodeContext *s = avctx->priv_data;
486  ff_lpc_end(&s->lpc_ctx);
487  av_freep(&avctx->extradata);
488  avctx->extradata_size = 0;
489  return 0;
490 }
491 
493 {
494  AlacEncodeContext *s = avctx->priv_data;
495  int ret;
496  uint8_t *alac_extradata;
497 
499 
500  if (avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
501  if (avctx->bits_per_raw_sample != 24)
502  av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
503  avctx->bits_per_raw_sample = 24;
504  } else {
505  avctx->bits_per_raw_sample = 16;
506  s->extra_bits = 0;
507  }
508 
509  // Set default compression level
511  s->compression_level = 2;
512  else
513  s->compression_level = av_clip(avctx->compression_level, 0, 2);
514 
515  // Initialize default Rice parameters
516  s->rc.history_mult = 40;
517  s->rc.initial_history = 10;
518  s->rc.k_modifier = 14;
519  s->rc.rice_modifier = 4;
520 
522  avctx->channels,
523  avctx->bits_per_raw_sample);
524 
526  if (!avctx->extradata) {
527  ret = AVERROR(ENOMEM);
528  goto error;
529  }
531 
532  alac_extradata = avctx->extradata;
533  AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
534  AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
535  AV_WB32(alac_extradata+12, avctx->frame_size);
536  AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
537  AV_WB8 (alac_extradata+21, avctx->channels);
538  AV_WB32(alac_extradata+24, s->max_coded_frame_size);
539  AV_WB32(alac_extradata+28,
540  avctx->sample_rate * avctx->channels * avctx->bits_per_raw_sample); // average bitrate
541  AV_WB32(alac_extradata+32, avctx->sample_rate);
542 
543  // Set relevant extradata fields
544  if (s->compression_level > 0) {
545  AV_WB8(alac_extradata+18, s->rc.history_mult);
546  AV_WB8(alac_extradata+19, s->rc.initial_history);
547  AV_WB8(alac_extradata+20, s->rc.k_modifier);
548  }
549 
550 #if FF_API_PRIVATE_OPT
552  if (avctx->min_prediction_order >= 0) {
553  if (avctx->min_prediction_order < MIN_LPC_ORDER ||
555  av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
556  avctx->min_prediction_order);
557  ret = AVERROR(EINVAL);
558  goto error;
559  }
560 
562  }
563 
564  if (avctx->max_prediction_order >= 0) {
565  if (avctx->max_prediction_order < MIN_LPC_ORDER ||
567  av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
568  avctx->max_prediction_order);
569  ret = AVERROR(EINVAL);
570  goto error;
571  }
572 
574  }
576 #endif
577 
579  av_log(avctx, AV_LOG_ERROR,
580  "invalid prediction orders: min=%d max=%d\n",
582  ret = AVERROR(EINVAL);
583  goto error;
584  }
585 
586  s->avctx = avctx;
587 
588  if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
590  FF_LPC_TYPE_LEVINSON)) < 0) {
591  goto error;
592  }
593 
594  return 0;
595 error:
596  alac_encode_close(avctx);
597  return ret;
598 }
599 
600 static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
601  const AVFrame *frame, int *got_packet_ptr)
602 {
603  AlacEncodeContext *s = avctx->priv_data;
604  int out_bytes, max_frame_size, ret;
605 
606  s->frame_size = frame->nb_samples;
607 
608  if (frame->nb_samples < DEFAULT_FRAME_SIZE)
609  max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
610  avctx->bits_per_raw_sample);
611  else
612  max_frame_size = s->max_coded_frame_size;
613 
614  if ((ret = ff_alloc_packet(avpkt, 2 * max_frame_size))) {
615  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
616  return ret;
617  }
618 
619  /* use verbatim mode for compression_level 0 */
620  if (s->compression_level) {
621  s->verbatim = 0;
622  s->extra_bits = avctx->bits_per_raw_sample - 16;
623  } else {
624  s->verbatim = 1;
625  s->extra_bits = 0;
626  }
627 
628  out_bytes = write_frame(s, avpkt, frame->extended_data);
629 
630  if (out_bytes > max_frame_size) {
631  /* frame too large. use verbatim mode */
632  s->verbatim = 1;
633  s->extra_bits = 0;
634  out_bytes = write_frame(s, avpkt, frame->extended_data);
635  }
636 
637  avpkt->size = out_bytes;
638  *got_packet_ptr = 1;
639  return 0;
640 }
641 
642 #define OFFSET(x) offsetof(AlacEncodeContext, x)
643 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
644 static const AVOption options[] = {
645  { "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 },
646  { "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 },
647 
648  { NULL },
649 };
650 
651 static const AVClass alacenc_class = {
652  .class_name = "alacenc",
653  .item_name = av_default_item_name,
654  .option = options,
655  .version = LIBAVUTIL_VERSION_INT,
656 };
657 
659  .name = "alac",
660  .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
661  .type = AVMEDIA_TYPE_AUDIO,
662  .id = AV_CODEC_ID_ALAC,
663  .priv_data_size = sizeof(AlacEncodeContext),
664  .priv_class = &alacenc_class,
666  .encode2 = alac_encode_frame,
667  .close = alac_encode_close,
668  .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
669  .channel_layouts = ff_alac_channel_layouts,
670  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
673 };
#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:182
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:1496
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
AVOption.
Definition: opt.h:234
Definition: lpc.h:49
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:172
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
LPCContext lpc_ctx
Definition: alacenc.c:79
#define ALAC_ESCAPE_CODE
Definition: alacenc.c:36
#define MAX_LPC_ORDER
Definition: lpc.h:35
Definition: aac.h:56
int ff_lpc_calc_coefs(LPCContext *s, const int32_t *samples, int blocksize, int min_order, int max_order, int precision, int32_t coefs[][MAX_LPC_ORDER], int *shift, enum FFLPCType lpc_type, int lpc_passes, int omethod, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
Definition: lpc.c:169
static const uint8_t frame_size[4]
Definition: g723_1.h:219
Definition: aac.h:49
Definition: aac.h:50
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define AV_WB8(p, d)
Definition: intreadwrite.h:369
#define COPY_SAMPLES(type)
PutBitContext pbctx
Definition: alacenc.c:76
static void write_element_header(AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance)
Definition: alacenc.c:133
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2776
int history_mult
Definition: alacenc.c:49
static av_cold int alac_encode_init(AVCodecContext *avctx)
Definition: alacenc.c:492
#define DEFAULT_MAX_PRED_ORDER
Definition: alacenc.c:38
AVCodec.
Definition: avcodec.h:3120
#define AV_WB32(p, val)
Definition: intreadwrite.h:246
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
int initial_history
Definition: alacenc.c:50
static void write_element(AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance, const uint8_t *samples0, const uint8_t *samples1)
Definition: alacenc.c:363
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:39
int lpc_quant
Definition: alacenc.c:58
static const AVClass alacenc_class
Definition: alacenc.c:651
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2160
uint8_t
#define av_cold
Definition: attributes.h:66
static void alac_linear_predictor(AlacEncodeContext *s, int ch)
Definition: alacenc.c:255
AVOptions.
static void alac_entropy_coder(AlacEncodeContext *s)
Definition: alacenc.c:319
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
int max_coded_frame_size
Definition: alacenc.c:69
int interlacing_leftweight
Definition: alacenc.c:75
int interlacing_shift
Definition: alacenc.c:74
AVCodecContext * avctx
Definition: alacenc.c:63
uint8_t * data
Definition: avcodec.h:1346
AVCodec ff_alac_encoder
Definition: alacenc.c:658
#define FFALIGN(x, a)
Definition: macros.h:48
#define r
Definition: input.c:51
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
static void calc_predictor_params(AlacEncodeContext *s, int ch)
Definition: alacenc.c:152
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:600
#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:148
AlacRawDataBlockType
Definition: alac_data.h:26
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
#define AE
Definition: alacenc.c:643
int frame_size
current frame size
Definition: alacenc.c:64
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:67
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:284
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: avcodec.h:868
#define FFMIN(a, b)
Definition: common.h:66
signed 32 bits, planar
Definition: samplefmt.h:70
int verbatim
current frame verbatim mode flag
Definition: alacenc.c:65
#define FFSIGN(a)
Definition: common.h:62
int32_t
int k_modifier
Definition: alacenc.c:51
#define FFABS(a)
Definition: common.h:61
static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
Definition: alacenc.c:477
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1211
const uint64_t ff_alac_channel_layouts[ALAC_MAX_CHANNELS+1]
Definition: alac_data.c:35
int max_prediction_order
Definition: alacenc.c:68
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static const AVOption options[]
Definition: alacenc.c:644
#define ALAC_CHMODE_LEFT_SIDE
Definition: alacenc.c:44
#define ALAC_MAX_LPC_ORDER
Definition: alacenc.c:37
AlacLPCContext lpc[2]
Definition: alacenc.c:78
attribute_deprecated int max_prediction_order
Definition: avcodec.h:2495
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2172
NULL
Definition: eval.c:55
int32_t sample_buf[2][DEFAULT_FRAME_SIZE]
Definition: alacenc.c:72
Libavcodec external API header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:60
int compression_level
Definition: avcodec.h:1495
int sample_rate
samples per second
Definition: avcodec.h:2152
av_default_item_name
Definition: dnxhdenc.c:55
#define MIN_LPC_ORDER
Definition: lpc.h:34
#define ALAC_MAX_LPC_SHIFT
Definition: alacenc.c:41
main external API structure.
Definition: avcodec.h:1409
int compression_level
Definition: alacenc.c:66
Levinson-Durbin recursion.
Definition: lpc.h:44
#define ORDER_METHOD_EST
Definition: lpc.h:27
int extradata_size
Definition: avcodec.h:1524
Describe the class of an AVClass context structure.
Definition: log.h:34
int index
Definition: gxfenc.c:72
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:262
static int write_frame(AlacEncodeContext *s, AVPacket *avpkt, uint8_t *const *samples)
Definition: alacenc.c:446
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:118
static void init_sample_buffers(AlacEncodeContext *s, int channels, const uint8_t *samples[2])
Definition: alacenc.c:83
enum AlacRawDataBlockType ff_alac_channel_elements[ALAC_MAX_CHANNELS][5]
Definition: alac_data.c:47
int lpc_order
Definition: alacenc.c:56
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:95
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
Definition: alacenc.c:182
int rice_modifier
Definition: alacenc.c:52
int min_prediction_order
Definition: alacenc.c:67
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
#define ALAC_CHMODE_LEFT_RIGHT
Definition: alacenc.c:43
#define ALAC_CHMODE_RIGHT_SIDE
Definition: alacenc.c:45
RiceContext rc
Definition: alacenc.c:77
static av_cold int alac_encode_close(AVCodecContext *avctx)
Definition: alacenc.c:483
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
unsigned bps
Definition: movenc.c:855
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
#define MKBETAG(a, b, c, d)
Definition: common.h:257
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
void * priv_data
Definition: avcodec.h:1451
int lpc_coeff[ALAC_MAX_LPC_ORDER+1]
Definition: alacenc.c:57
int32_t predictor_buf[DEFAULT_FRAME_SIZE]
Definition: alacenc.c:73
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
attribute_deprecated int min_prediction_order
Definition: avcodec.h:2491
int channels
number of audio channels
Definition: avcodec.h:2153
#define av_log2
Definition: intmath.h:85
static uint8_t tmp[8]
Definition: des.c:38
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
signed 16 bits, planar
Definition: samplefmt.h:69
#define av_always_inline
Definition: attributes.h:40
#define ALAC_EXTRADATA_SIZE
Definition: alacenc.c:32
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:174
#define DEFAULT_FRAME_SIZE
Definition: alacenc.c:31
This structure stores compressed data.
Definition: avcodec.h:1323
static void alac_stereo_decorrelation(AlacEncodeContext *s)
Definition: alacenc.c:215
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
Definition: alacenc.c:105
#define OFFSET(x)
Definition: alacenc.c:642
int write_sample_size
Definition: alacenc.c:70
bitstream writer API