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_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;
58  int lpc_quant;
60 
61 typedef struct AlacEncodeContext {
62  const AVClass *class;
64  int frame_size; /**< current frame size */
65  int verbatim; /**< current frame verbatim mode flag */
81 
82 
83 static void init_sample_buffers(AlacEncodeContext *s, int channels,
84  uint8_t const *samples[2])
85 {
86  int ch, i;
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  int32_t *residual = s->predictor_buf[ch];
260 
261  if (lpc.lpc_order == 31) {
262  residual[0] = s->sample_buf[ch][0];
263 
264  for (i = 1; i < s->frame_size; i++) {
265  residual[i] = s->sample_buf[ch][i ] -
266  s->sample_buf[ch][i - 1];
267  }
268 
269  return;
270  }
271 
272  // generalised linear predictor
273 
274  if (lpc.lpc_order > 0) {
275  int32_t *samples = s->sample_buf[ch];
276 
277  // generate warm-up samples
278  residual[0] = samples[0];
279  for (i = 1; i <= lpc.lpc_order; i++)
280  residual[i] = sign_extend(samples[i] - samples[i-1], s->write_sample_size);
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[ch];
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  uint8_t const *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  int32_t const *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  int16_t const *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  // extract extra bits if needed
401  if (s->extra_bits) {
402  uint32_t mask = (1 << s->extra_bits) - 1;
403  for (j = 0; j < channels; j++) {
404  int32_t *extra = s->predictor_buf[j];
405  int32_t *smp = s->sample_buf[j];
406  for (i = 0; i < s->frame_size; i++) {
407  extra[i] = smp[i] & mask;
408  smp[i] >>= s->extra_bits;
409  }
410  }
411  }
412 
413  if (channels == 2)
415  else
417  put_bits(pb, 8, s->interlacing_shift);
418  put_bits(pb, 8, s->interlacing_leftweight);
419 
420  for (i = 0; i < channels; i++) {
421  calc_predictor_params(s, i);
422 
423  put_bits(pb, 4, prediction_type);
424  put_bits(pb, 4, s->lpc[i].lpc_quant);
425 
426  put_bits(pb, 3, s->rc.rice_modifier);
427  put_bits(pb, 5, s->lpc[i].lpc_order);
428  // predictor coeff. table
429  for (j = 0; j < s->lpc[i].lpc_order; j++)
430  put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]);
431  }
432 
433  // write extra bits if needed
434  if (s->extra_bits) {
435  for (i = 0; i < s->frame_size; i++) {
436  for (j = 0; j < channels; j++) {
437  put_bits(pb, s->extra_bits, s->predictor_buf[j][i]);
438  }
439  }
440  }
441 
442  // apply lpc and entropy coding to audio samples
443  for (i = 0; i < channels; i++) {
444  alac_linear_predictor(s, i);
445 
446  // TODO: determine when this will actually help. for now it's not used.
447  if (prediction_type == 15) {
448  // 2nd pass 1st order filter
449  int32_t *residual = s->predictor_buf[i];
450  for (j = s->frame_size - 1; j > 0; j--)
451  residual[j] -= residual[j - 1];
452  }
453  alac_entropy_coder(s, i);
454  }
455  }
456 }
457 
459  uint8_t * const *samples)
460 {
461  PutBitContext *pb = &s->pbctx;
462  const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[s->avctx->channels - 1];
463  const uint8_t *ch_map = ff_alac_channel_layout_offsets[s->avctx->channels - 1];
464  int ch, element, sce, cpe;
465 
466  init_put_bits(pb, avpkt->data, avpkt->size);
467 
468  ch = element = sce = cpe = 0;
469  while (ch < s->avctx->channels) {
470  if (ch_elements[element] == TYPE_CPE) {
471  write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
472  samples[ch_map[ch + 1]]);
473  cpe++;
474  ch += 2;
475  } else {
476  write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL);
477  sce++;
478  ch++;
479  }
480  element++;
481  }
482 
483  put_bits(pb, 3, TYPE_END);
484  flush_put_bits(pb);
485 
486  return put_bits_count(pb) >> 3;
487 }
488 
489 static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
490 {
491  int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
492  return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
493 }
494 
496 {
497  AlacEncodeContext *s = avctx->priv_data;
498  ff_lpc_end(&s->lpc_ctx);
499  av_freep(&avctx->extradata);
500  avctx->extradata_size = 0;
501  return 0;
502 }
503 
505 {
506  AlacEncodeContext *s = avctx->priv_data;
507  int ret;
508  uint8_t *alac_extradata;
509 
511 
512  if (avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
513  if (avctx->bits_per_raw_sample != 24)
514  av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
515  avctx->bits_per_raw_sample = 24;
516  } else {
517  avctx->bits_per_raw_sample = 16;
518  s->extra_bits = 0;
519  }
520 
521  // Set default compression level
523  s->compression_level = 2;
524  else
525  s->compression_level = av_clip(avctx->compression_level, 0, 2);
526 
527  // Initialize default Rice parameters
528  s->rc.history_mult = 40;
529  s->rc.initial_history = 10;
530  s->rc.k_modifier = 14;
531  s->rc.rice_modifier = 4;
532 
534  avctx->channels,
535  avctx->bits_per_raw_sample);
536 
538  if (!avctx->extradata) {
539  ret = AVERROR(ENOMEM);
540  goto error;
541  }
543 
544  alac_extradata = avctx->extradata;
545  AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
546  AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
547  AV_WB32(alac_extradata+12, avctx->frame_size);
548  AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
549  AV_WB8 (alac_extradata+21, avctx->channels);
550  AV_WB32(alac_extradata+24, s->max_coded_frame_size);
551  AV_WB32(alac_extradata+28,
552  avctx->sample_rate * avctx->channels * avctx->bits_per_raw_sample); // average bitrate
553  AV_WB32(alac_extradata+32, avctx->sample_rate);
554 
555  // Set relevant extradata fields
556  if (s->compression_level > 0) {
557  AV_WB8(alac_extradata+18, s->rc.history_mult);
558  AV_WB8(alac_extradata+19, s->rc.initial_history);
559  AV_WB8(alac_extradata+20, s->rc.k_modifier);
560  }
561 
562 #if FF_API_PRIVATE_OPT
564  if (avctx->min_prediction_order >= 0) {
565  if (avctx->min_prediction_order < MIN_LPC_ORDER ||
567  av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
568  avctx->min_prediction_order);
569  ret = AVERROR(EINVAL);
570  goto error;
571  }
572 
574  }
575 
576  if (avctx->max_prediction_order >= 0) {
577  if (avctx->max_prediction_order < MIN_LPC_ORDER ||
579  av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
580  avctx->max_prediction_order);
581  ret = AVERROR(EINVAL);
582  goto error;
583  }
584 
586  }
588 #endif
589 
591  av_log(avctx, AV_LOG_ERROR,
592  "invalid prediction orders: min=%d max=%d\n",
594  ret = AVERROR(EINVAL);
595  goto error;
596  }
597 
598  s->avctx = avctx;
599 
600  if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
602  FF_LPC_TYPE_LEVINSON)) < 0) {
603  goto error;
604  }
605 
606  return 0;
607 error:
608  alac_encode_close(avctx);
609  return ret;
610 }
611 
612 static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
613  const AVFrame *frame, int *got_packet_ptr)
614 {
615  AlacEncodeContext *s = avctx->priv_data;
616  int out_bytes, max_frame_size, ret;
617 
618  s->frame_size = frame->nb_samples;
619 
620  if (frame->nb_samples < DEFAULT_FRAME_SIZE)
621  max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
622  avctx->bits_per_raw_sample);
623  else
624  max_frame_size = s->max_coded_frame_size;
625 
626  if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * max_frame_size, 0)) < 0)
627  return ret;
628 
629  /* use verbatim mode for compression_level 0 */
630  if (s->compression_level) {
631  s->verbatim = 0;
632  s->extra_bits = avctx->bits_per_raw_sample - 16;
633  } else {
634  s->verbatim = 1;
635  s->extra_bits = 0;
636  }
637 
638  out_bytes = write_frame(s, avpkt, frame->extended_data);
639 
640  if (out_bytes > max_frame_size) {
641  /* frame too large. use verbatim mode */
642  s->verbatim = 1;
643  s->extra_bits = 0;
644  out_bytes = write_frame(s, avpkt, frame->extended_data);
645  }
646 
647  avpkt->size = out_bytes;
648  *got_packet_ptr = 1;
649  return 0;
650 }
651 
652 #define OFFSET(x) offsetof(AlacEncodeContext, x)
653 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
654 static const AVOption options[] = {
655  { "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 },
656  { "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 },
657 
658  { NULL },
659 };
660 
661 static const AVClass alacenc_class = {
662  .class_name = "alacenc",
663  .item_name = av_default_item_name,
664  .option = options,
665  .version = LIBAVUTIL_VERSION_INT,
666 };
667 
669  .name = "alac",
670  .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
671  .type = AVMEDIA_TYPE_AUDIO,
672  .id = AV_CODEC_ID_ALAC,
673  .priv_data_size = sizeof(AlacEncodeContext),
674  .priv_class = &alacenc_class,
676  .encode2 = alac_encode_frame,
677  .close = alac_encode_close,
678  .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
679  .channel_layouts = ff_alac_channel_layouts,
680  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
683 };
#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:634
const char * s
Definition: avisynth_c.h:631
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:1620
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
AVOption.
Definition: opt.h:245
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:168
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
LPCContext lpc_ctx
Definition: alacenc.c:79
#define ALAC_ESCAPE_CODE
Definition: alacenc.c:36
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
#define MAX_LPC_ORDER
Definition: lpc.h:38
Definition: aac.h:63
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:198
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
Definition: aac.h:56
Definition: aac.h:57
int size
Definition: avcodec.h:1468
#define AV_WB8(p, d)
Definition: intreadwrite.h:396
int av_log2(unsigned v)
Definition: intmath.c:26
#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:2924
int history_mult
Definition: alacenc.c:49
static av_cold int alac_encode_init(AVCodecContext *avctx)
Definition: alacenc.c:504
#define DEFAULT_MAX_PRED_ORDER
Definition: alacenc.c:38
AVCodec.
Definition: avcodec.h:3392
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:72
int lpc_quant
Definition: alacenc.c:58
static const AVClass alacenc_class
Definition: alacenc.c:661
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2295
uint8_t
#define av_cold
Definition: attributes.h:82
static void alac_linear_predictor(AlacEncodeContext *s, int ch)
Definition: alacenc.c:255
mode
Definition: f_perms.c:27
AVOptions.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
int max_coded_frame_size
Definition: alacenc.c:69
static AVFrame * frame
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:1467
AVCodec ff_alac_encoder
Definition: alacenc.c:668
#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: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:612
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:176
const char * r
Definition: vf_curves.c:107
AlacRawDataBlockType
Definition: alac_data.h:26
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
#define AE
Definition: alacenc.c:653
static void alac_entropy_coder(AlacEncodeContext *s, int ch)
Definition: alacenc.c:319
int frame_size
current frame size
Definition: alacenc.c:64
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:318
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: avcodec.h:886
#define FFMIN(a, b)
Definition: common.h:96
signed 32 bits, planar
Definition: samplefmt.h:69
int verbatim
current frame verbatim mode flag
Definition: alacenc.c:65
#define FFSIGN(a)
Definition: common.h:73
int32_t
int k_modifier
Definition: alacenc.c:51
#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:489
int n
Definition: avisynth_c.h:547
const uint64_t ff_alac_channel_layouts[ALAC_MAX_CHANNELS+1]
Definition: alac_data.c:35
int max_prediction_order
Definition: alacenc.c:68
static const AVOption options[]
Definition: alacenc.c:654
#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:2631
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2307
int frame_size
Definition: mxfenc.c:1821
int32_t sample_buf[2][DEFAULT_FRAME_SIZE]
Definition: alacenc.c:72
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
int compression_level
Definition: avcodec.h:1619
int sample_rate
samples per second
Definition: avcodec.h:2287
#define MIN_LPC_ORDER
Definition: lpc.h:37
#define ALAC_MAX_LPC_SHIFT
Definition: alacenc.c:41
main external API structure.
Definition: avcodec.h:1532
int compression_level
Definition: alacenc.c:66
Levinson-Durbin recursion.
Definition: lpc.h:47
#define ORDER_METHOD_EST
Definition: lpc.h:30
int extradata_size
Definition: avcodec.h:1648
int32_t predictor_buf[2][DEFAULT_FRAME_SIZE]
Definition: alacenc.c:73
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
int index
Definition: gxfenc.c:89
static void init_sample_buffers(AlacEncodeContext *s, int channels, uint8_t const *samples[2])
Definition: alacenc.c:83
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:1621
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:296
static int write_frame(AlacEncodeContext *s, AVPacket *avpkt, uint8_t *const *samples)
Definition: alacenc.c:458
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:138
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:104
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:80
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: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:495
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:1349
#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:635
void * priv_data
Definition: avcodec.h:1574
int lpc_coeff[ALAC_MAX_LPC_ORDER+1]
Definition: alacenc.c:57
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
attribute_deprecated int min_prediction_order
Definition: avcodec.h:2627
int channels
number of audio channels
Definition: avcodec.h:2288
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
signed 16 bits, planar
Definition: samplefmt.h:68
#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:225
#define DEFAULT_FRAME_SIZE
Definition: alacenc.c:31
This structure stores compressed data.
Definition: avcodec.h:1444
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:235
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:252
static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
Definition: alacenc.c:105
#define OFFSET(x)
Definition: alacenc.c:652
int write_sample_size
Definition: alacenc.c:70
bitstream writer API