FFmpeg
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 "encode.h"
26 #include "put_bits.h"
27 #include "internal.h"
28 #include "lpc.h"
29 #include "mathops.h"
30 #include "alac_data.h"
31 
32 #define DEFAULT_FRAME_SIZE 4096
33 #define ALAC_EXTRADATA_SIZE 36
34 #define ALAC_FRAME_HEADER_SIZE 55
35 #define ALAC_FRAME_FOOTER_SIZE 3
36 
37 #define ALAC_ESCAPE_CODE 0x1FF
38 #define ALAC_MAX_LPC_ORDER 30
39 #define DEFAULT_MAX_PRED_ORDER 6
40 #define DEFAULT_MIN_PRED_ORDER 4
41 #define ALAC_MAX_LPC_PRECISION 9
42 #define ALAC_MIN_LPC_SHIFT 0
43 #define ALAC_MAX_LPC_SHIFT 9
44 
45 #define ALAC_CHMODE_LEFT_RIGHT 0
46 #define ALAC_CHMODE_LEFT_SIDE 1
47 #define ALAC_CHMODE_RIGHT_SIDE 2
48 #define ALAC_CHMODE_MID_SIDE 3
49 
50 typedef struct RiceContext {
55 } RiceContext;
56 
57 typedef struct AlacLPCContext {
58  int lpc_order;
60  int lpc_quant;
62 
63 typedef struct AlacEncodeContext {
64  const AVClass *class;
66  int frame_size; /**< current frame size */
67  int verbatim; /**< current frame verbatim mode flag */
83 
84 
86  const uint8_t *samples[2])
87 {
88  int ch, i;
89  int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
90  s->avctx->bits_per_raw_sample;
91 
92 #define COPY_SAMPLES(type) do { \
93  for (ch = 0; ch < channels; ch++) { \
94  int32_t *bptr = s->sample_buf[ch]; \
95  const type *sptr = (const type *)samples[ch]; \
96  for (i = 0; i < s->frame_size; i++) \
97  bptr[i] = sptr[i] >> shift; \
98  } \
99  } while (0)
100 
101  if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P)
103  else
104  COPY_SAMPLES(int16_t);
105 }
106 
107 static void encode_scalar(AlacEncodeContext *s, int x,
108  int k, int write_sample_size)
109 {
110  int divisor, q, r;
111 
112  k = FFMIN(k, s->rc.k_modifier);
113  divisor = (1<<k) - 1;
114  q = x / divisor;
115  r = x % divisor;
116 
117  if (q > 8) {
118  // write escape code and sample value directly
119  put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
120  put_bits(&s->pbctx, write_sample_size, x);
121  } else {
122  if (q)
123  put_bits(&s->pbctx, q, (1<<q) - 1);
124  put_bits(&s->pbctx, 1, 0);
125 
126  if (k != 1) {
127  if (r > 0)
128  put_bits(&s->pbctx, k, r+1);
129  else
130  put_bits(&s->pbctx, k-1, 0);
131  }
132  }
133 }
134 
136  enum AlacRawDataBlockType element,
137  int instance)
138 {
139  int encode_fs = 0;
140 
141  if (s->frame_size < DEFAULT_FRAME_SIZE)
142  encode_fs = 1;
143 
144  put_bits(&s->pbctx, 3, element); // element type
145  put_bits(&s->pbctx, 4, instance); // element instance
146  put_bits(&s->pbctx, 12, 0); // unused header bits
147  put_bits(&s->pbctx, 1, encode_fs); // Sample count is in the header
148  put_bits(&s->pbctx, 2, s->extra_bits >> 3); // Extra bytes (for 24-bit)
149  put_bits(&s->pbctx, 1, s->verbatim); // Audio block is verbatim
150  if (encode_fs)
151  put_bits32(&s->pbctx, s->frame_size); // No. of samples in the frame
152 }
153 
155 {
157  int shift[MAX_LPC_ORDER];
158  int opt_order;
159 
160  if (s->compression_level == 1) {
161  s->lpc[ch].lpc_order = 6;
162  s->lpc[ch].lpc_quant = 6;
163  s->lpc[ch].lpc_coeff[0] = 160;
164  s->lpc[ch].lpc_coeff[1] = -190;
165  s->lpc[ch].lpc_coeff[2] = 170;
166  s->lpc[ch].lpc_coeff[3] = -130;
167  s->lpc[ch].lpc_coeff[4] = 80;
168  s->lpc[ch].lpc_coeff[5] = -25;
169  } else {
170  opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, s->sample_buf[ch],
171  s->frame_size,
172  s->min_prediction_order,
173  s->max_prediction_order,
177  ALAC_MAX_LPC_SHIFT, 1);
178 
179  s->lpc[ch].lpc_order = opt_order;
180  s->lpc[ch].lpc_quant = shift[opt_order-1];
181  memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
182  }
183 }
184 
185 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
186 {
187  int i, best;
188  int32_t lt, rt;
189  uint64_t sum[4];
190  uint64_t score[4];
191 
192  /* calculate sum of 2nd order residual for each channel */
193  sum[0] = sum[1] = sum[2] = sum[3] = 0;
194  for (i = 2; i < n; i++) {
195  lt = left_ch[i] - 2 * left_ch[i - 1] + left_ch[i - 2];
196  rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
197  sum[2] += FFABS((lt + rt) >> 1);
198  sum[3] += FFABS(lt - rt);
199  sum[0] += FFABS(lt);
200  sum[1] += FFABS(rt);
201  }
202 
203  /* calculate score for each mode */
204  score[0] = sum[0] + sum[1];
205  score[1] = sum[0] + sum[3];
206  score[2] = sum[1] + sum[3];
207  score[3] = sum[2] + sum[3];
208 
209  /* return mode with lowest score */
210  best = 0;
211  for (i = 1; i < 4; i++) {
212  if (score[i] < score[best])
213  best = i;
214  }
215  return best;
216 }
217 
219 {
220  int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
221  int i, mode, n = s->frame_size;
222  int32_t tmp;
223 
224  mode = estimate_stereo_mode(left, right, n);
225 
226  switch (mode) {
228  s->interlacing_leftweight = 0;
229  s->interlacing_shift = 0;
230  break;
232  for (i = 0; i < n; i++)
233  right[i] = left[i] - right[i];
234  s->interlacing_leftweight = 1;
235  s->interlacing_shift = 0;
236  break;
238  for (i = 0; i < n; i++) {
239  tmp = right[i];
240  right[i] = left[i] - right[i];
241  left[i] = tmp + (right[i] >> 31);
242  }
243  s->interlacing_leftweight = 1;
244  s->interlacing_shift = 31;
245  break;
246  default:
247  for (i = 0; i < n; i++) {
248  tmp = left[i];
249  left[i] = (tmp + right[i]) >> 1;
250  right[i] = tmp - right[i];
251  }
252  s->interlacing_leftweight = 1;
253  s->interlacing_shift = 1;
254  break;
255  }
256 }
257 
259 {
260  int i;
261  AlacLPCContext lpc = s->lpc[ch];
262  int32_t *residual = s->predictor_buf[ch];
263 
264  if (lpc.lpc_order == 31) {
265  residual[0] = s->sample_buf[ch][0];
266 
267  for (i = 1; i < s->frame_size; i++) {
268  residual[i] = s->sample_buf[ch][i ] -
269  s->sample_buf[ch][i - 1];
270  }
271 
272  return;
273  }
274 
275  // generalised linear predictor
276 
277  if (lpc.lpc_order > 0) {
278  int32_t *samples = s->sample_buf[ch];
279 
280  // generate warm-up samples
281  residual[0] = samples[0];
282  for (i = 1; i <= lpc.lpc_order; i++)
283  residual[i] = sign_extend(samples[i] - samples[i-1], s->write_sample_size);
284 
285  // perform lpc on remaining samples
286  for (i = lpc.lpc_order + 1; i < s->frame_size; i++) {
287  int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
288 
289  for (j = 0; j < lpc.lpc_order; j++) {
290  sum += (samples[lpc.lpc_order-j] - samples[0]) *
291  lpc.lpc_coeff[j];
292  }
293 
294  sum >>= lpc.lpc_quant;
295  sum += samples[0];
296  residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
297  s->write_sample_size);
298  res_val = residual[i];
299 
300  if (res_val) {
301  int index = lpc.lpc_order - 1;
302  int neg = (res_val < 0);
303 
304  while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
305  int val = samples[0] - samples[lpc.lpc_order - index];
306  int sign = (val ? FFSIGN(val) : 0);
307 
308  if (neg)
309  sign *= -1;
310 
311  lpc.lpc_coeff[index] -= sign;
312  val *= sign;
313  res_val -= (val >> lpc.lpc_quant) * (lpc.lpc_order - index);
314  index--;
315  }
316  }
317  samples++;
318  }
319  }
320 }
321 
323 {
324  unsigned int history = s->rc.initial_history;
325  int sign_modifier = 0, i, k;
326  int32_t *samples = s->predictor_buf[ch];
327 
328  for (i = 0; i < s->frame_size;) {
329  int x;
330 
331  k = av_log2((history >> 9) + 3);
332 
333  x = -2 * (*samples) -1;
334  x ^= x >> 31;
335 
336  samples++;
337  i++;
338 
339  encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
340 
341  history += x * s->rc.history_mult -
342  ((history * s->rc.history_mult) >> 9);
343 
344  sign_modifier = 0;
345  if (x > 0xFFFF)
346  history = 0xFFFF;
347 
348  if (history < 128 && i < s->frame_size) {
349  unsigned int block_size = 0;
350 
351  k = 7 - av_log2(history) + ((history + 16) >> 6);
352 
353  while (*samples == 0 && i < s->frame_size) {
354  samples++;
355  i++;
356  block_size++;
357  }
358  encode_scalar(s, block_size, k, 16);
359  sign_modifier = (block_size <= 0xFFFF);
360  history = 0;
361  }
362 
363  }
364 }
365 
367  enum AlacRawDataBlockType element, int instance,
368  const uint8_t *samples0, const uint8_t *samples1)
369 {
370  const uint8_t *samples[2] = { samples0, samples1 };
371  int i, j, channels;
372  int prediction_type = 0;
373  PutBitContext *pb = &s->pbctx;
374 
375  channels = element == TYPE_CPE ? 2 : 1;
376 
377  if (s->verbatim) {
378  write_element_header(s, element, instance);
379  /* samples are channel-interleaved in verbatim mode */
380  if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
381  int shift = 32 - s->avctx->bits_per_raw_sample;
382  const int32_t *samples_s32[2] = { (const int32_t *)samples0,
383  (const int32_t *)samples1 };
384  for (i = 0; i < s->frame_size; i++)
385  for (j = 0; j < channels; j++)
386  put_sbits(pb, s->avctx->bits_per_raw_sample,
387  samples_s32[j][i] >> shift);
388  } else {
389  const int16_t *samples_s16[2] = { (const int16_t *)samples0,
390  (const int16_t *)samples1 };
391  for (i = 0; i < s->frame_size; i++)
392  for (j = 0; j < channels; j++)
393  put_sbits(pb, s->avctx->bits_per_raw_sample,
394  samples_s16[j][i]);
395  }
396  } else {
397  s->write_sample_size = s->avctx->bits_per_raw_sample - s->extra_bits +
398  channels - 1;
399 
401  write_element_header(s, element, instance);
402 
403  // extract extra bits if needed
404  if (s->extra_bits) {
405  uint32_t mask = (1 << s->extra_bits) - 1;
406  for (j = 0; j < channels; j++) {
407  int32_t *extra = s->predictor_buf[j];
408  int32_t *smp = s->sample_buf[j];
409  for (i = 0; i < s->frame_size; i++) {
410  extra[i] = smp[i] & mask;
411  smp[i] >>= s->extra_bits;
412  }
413  }
414  }
415 
416  if (channels == 2)
418  else
419  s->interlacing_shift = s->interlacing_leftweight = 0;
420  put_bits(pb, 8, s->interlacing_shift);
421  put_bits(pb, 8, s->interlacing_leftweight);
422 
423  for (i = 0; i < channels; i++) {
425 
426  put_bits(pb, 4, prediction_type);
427  put_bits(pb, 4, s->lpc[i].lpc_quant);
428 
429  put_bits(pb, 3, s->rc.rice_modifier);
430  put_bits(pb, 5, s->lpc[i].lpc_order);
431  // predictor coeff. table
432  for (j = 0; j < s->lpc[i].lpc_order; j++)
433  put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]);
434  }
435 
436  // write extra bits if needed
437  if (s->extra_bits) {
438  for (i = 0; i < s->frame_size; i++) {
439  for (j = 0; j < channels; j++) {
440  put_bits(pb, s->extra_bits, s->predictor_buf[j][i]);
441  }
442  }
443  }
444 
445  // apply lpc and entropy coding to audio samples
446  for (i = 0; i < channels; i++) {
448 
449  // TODO: determine when this will actually help. for now it's not used.
450  if (prediction_type == 15) {
451  // 2nd pass 1st order filter
452  int32_t *residual = s->predictor_buf[i];
453  for (j = s->frame_size - 1; j > 0; j--)
454  residual[j] -= residual[j - 1];
455  }
457  }
458  }
459 }
460 
462  uint8_t * const *samples)
463 {
464  PutBitContext *pb = &s->pbctx;
465  const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[s->avctx->channels - 1];
466  const uint8_t *ch_map = ff_alac_channel_layout_offsets[s->avctx->channels - 1];
467  int ch, element, sce, cpe;
468 
469  init_put_bits(pb, avpkt->data, avpkt->size);
470 
471  ch = element = sce = cpe = 0;
472  while (ch < s->avctx->channels) {
473  if (ch_elements[element] == TYPE_CPE) {
474  write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
475  samples[ch_map[ch + 1]]);
476  cpe++;
477  ch += 2;
478  } else {
479  write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL);
480  sce++;
481  ch++;
482  }
483  element++;
484  }
485 
486  put_bits(pb, 3, TYPE_END);
487  flush_put_bits(pb);
488 
489  return put_bytes_output(pb);
490 }
491 
492 static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
493 {
494  int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
495  return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
496 }
497 
499 {
500  AlacEncodeContext *s = avctx->priv_data;
501  ff_lpc_end(&s->lpc_ctx);
502  return 0;
503 }
504 
506 {
507  AlacEncodeContext *s = avctx->priv_data;
508  int ret;
509  uint8_t *alac_extradata;
510 
511  avctx->frame_size = s->frame_size = DEFAULT_FRAME_SIZE;
512 
513  if (avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
514  if (avctx->bits_per_raw_sample != 24)
515  av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
516  avctx->bits_per_raw_sample = 24;
517  } else {
518  avctx->bits_per_raw_sample = 16;
519  s->extra_bits = 0;
520  }
521 
522  // Set default compression level
524  s->compression_level = 2;
525  else
526  s->compression_level = av_clip(avctx->compression_level, 0, 2);
527 
528  // Initialize default Rice parameters
529  s->rc.history_mult = 40;
530  s->rc.initial_history = 10;
531  s->rc.k_modifier = 14;
532  s->rc.rice_modifier = 4;
533 
534  s->max_coded_frame_size = get_max_frame_size(avctx->frame_size,
535  avctx->channels,
536  avctx->bits_per_raw_sample);
537 
539  if (!avctx->extradata)
540  return AVERROR(ENOMEM);
542 
543  alac_extradata = avctx->extradata;
544  AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
545  AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
546  AV_WB32(alac_extradata+12, avctx->frame_size);
547  AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
548  AV_WB8 (alac_extradata+21, avctx->channels);
549  AV_WB32(alac_extradata+24, s->max_coded_frame_size);
550  AV_WB32(alac_extradata+28,
551  avctx->sample_rate * avctx->channels * avctx->bits_per_raw_sample); // average bitrate
552  AV_WB32(alac_extradata+32, avctx->sample_rate);
553 
554  // Set relevant extradata fields
555  if (s->compression_level > 0) {
556  AV_WB8(alac_extradata+18, s->rc.history_mult);
557  AV_WB8(alac_extradata+19, s->rc.initial_history);
558  AV_WB8(alac_extradata+20, s->rc.k_modifier);
559  }
560 
561  if (s->max_prediction_order < s->min_prediction_order) {
562  av_log(avctx, AV_LOG_ERROR,
563  "invalid prediction orders: min=%d max=%d\n",
564  s->min_prediction_order, s->max_prediction_order);
565  return AVERROR(EINVAL);
566  }
567 
568  s->avctx = avctx;
569 
570  if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
571  s->max_prediction_order,
572  FF_LPC_TYPE_LEVINSON)) < 0) {
573  return ret;
574  }
575 
576  return 0;
577 }
578 
579 static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
580  const AVFrame *frame, int *got_packet_ptr)
581 {
582  AlacEncodeContext *s = avctx->priv_data;
583  int out_bytes, max_frame_size, ret;
584 
585  s->frame_size = frame->nb_samples;
586 
587  if (frame->nb_samples < DEFAULT_FRAME_SIZE)
588  max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
589  avctx->bits_per_raw_sample);
590  else
591  max_frame_size = s->max_coded_frame_size;
592 
593  if ((ret = ff_alloc_packet(avctx, avpkt, 4 * max_frame_size)) < 0)
594  return ret;
595 
596  /* use verbatim mode for compression_level 0 */
597  if (s->compression_level) {
598  s->verbatim = 0;
599  s->extra_bits = avctx->bits_per_raw_sample - 16;
600  } else {
601  s->verbatim = 1;
602  s->extra_bits = 0;
603  }
604 
605  out_bytes = write_frame(s, avpkt, frame->extended_data);
606 
607  if (out_bytes > max_frame_size) {
608  /* frame too large. use verbatim mode */
609  s->verbatim = 1;
610  s->extra_bits = 0;
611  out_bytes = write_frame(s, avpkt, frame->extended_data);
612  }
613 
614  avpkt->size = out_bytes;
615  *got_packet_ptr = 1;
616  return 0;
617 }
618 
619 #define OFFSET(x) offsetof(AlacEncodeContext, x)
620 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
621 static const AVOption options[] = {
622  { "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 },
623  { "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 },
624 
625  { NULL },
626 };
627 
628 static const AVClass alacenc_class = {
629  .class_name = "alacenc",
630  .item_name = av_default_item_name,
631  .option = options,
632  .version = LIBAVUTIL_VERSION_INT,
633 };
634 
636  .name = "alac",
637  .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
638  .type = AVMEDIA_TYPE_AUDIO,
639  .id = AV_CODEC_ID_ALAC,
640  .priv_data_size = sizeof(AlacEncodeContext),
641  .priv_class = &alacenc_class,
643  .encode2 = alac_encode_frame,
644  .close = alac_encode_close,
645  .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
647  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
650  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
651 };
DEFAULT_FRAME_SIZE
#define DEFAULT_FRAME_SIZE
Definition: alacenc.c:32
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1012
AVCodec
AVCodec.
Definition: codec.h:202
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
alac_stereo_decorrelation
static void alac_stereo_decorrelation(AlacEncodeContext *s)
Definition: alacenc.c:218
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
OFFSET
#define OFFSET(x)
Definition: alacenc.c:619
ALAC_ESCAPE_CODE
#define ALAC_ESCAPE_CODE
Definition: alacenc.c:37
av_clip
#define av_clip
Definition: common.h:96
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
r
const char * r
Definition: vf_curves.c:116
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AlacEncodeContext::compression_level
int compression_level
Definition: alacenc.c:68
put_bits32
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:290
AlacEncodeContext::verbatim
int verbatim
current frame verbatim mode flag
Definition: alacenc.c:67
alac_data.h
put_bytes_output
static int put_bytes_output(const PutBitContext *s)
Definition: put_bits.h:88
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:992
DEFAULT_MIN_PRED_ORDER
#define DEFAULT_MIN_PRED_ORDER
Definition: alacenc.c:40
AlacEncodeContext::predictor_buf
int32_t predictor_buf[2][DEFAULT_FRAME_SIZE]
Definition: alacenc.c:75
AlacLPCContext::lpc_quant
int lpc_quant
Definition: alacenc.c:60
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
put_sbits
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:280
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:61
RiceContext
Definition: alacenc.c:50
write_element_header
static void write_element_header(AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance)
Definition: alacenc.c:135
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:220
index
fg index
Definition: ffmpeg_filter.c:168
alac_encode_init
static av_cold int alac_encode_init(AVCodecContext *avctx)
Definition: alacenc.c:505
internal.h
AlacEncodeContext::avctx
AVCodecContext * avctx
Definition: alacenc.c:65
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
encode.h
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:68
AV_CODEC_ID_ALAC
@ AV_CODEC_ID_ALAC
Definition: codec_id.h:439
AlacLPCContext::lpc_order
int lpc_order
Definition: alacenc.c:58
lpc.h
FF_COMPRESSION_DEFAULT
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:456
alac_linear_predictor
static void alac_linear_predictor(AlacEncodeContext *s, int ch)
Definition: alacenc.c:258
COPY_SAMPLES
#define COPY_SAMPLES(type)
LPCContext
Definition: lpc.h:52
AlacEncodeContext::lpc
AlacLPCContext lpc[2]
Definition: alacenc.c:80
DEFAULT_MAX_PRED_ORDER
#define DEFAULT_MAX_PRED_ORDER
Definition: alacenc.c:39
FFSIGN
#define FFSIGN(a)
Definition: common.h:66
TYPE_CPE
@ TYPE_CPE
Definition: aac.h:58
AlacEncodeContext::write_sample_size
int write_sample_size
Definition: alacenc.c:72
write_element
static void write_element(AlacEncodeContext *s, enum AlacRawDataBlockType element, int instance, const uint8_t *samples0, const uint8_t *samples1)
Definition: alacenc.c:366
val
static double val(void *priv, double ch)
Definition: aeval.c:76
alacenc_class
static const AVClass alacenc_class
Definition: alacenc.c:628
AlacEncodeContext::extra_bits
int extra_bits
Definition: alacenc.c:73
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
calc_predictor_params
static void calc_predictor_params(AlacEncodeContext *s, int ch)
Definition: alacenc.c:154
mask
static const uint16_t mask[17]
Definition: lzw.c:38
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:485
alac_encode_frame
static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: alacenc.c:579
s
#define s(width, name)
Definition: cbs_vp9.c:257
frame_size
int frame_size
Definition: mxfenc.c:2205
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1425
RiceContext::rice_modifier
int rice_modifier
Definition: alacenc.c:54
RiceContext::k_modifier
int k_modifier
Definition: alacenc.c:53
channels
channels
Definition: aptx.h:33
alac_entropy_coder
static void alac_entropy_coder(AlacEncodeContext *s, int ch)
Definition: alacenc.c:322
PutBitContext
Definition: put_bits.h:49
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:65
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
ALAC_MAX_LPC_PRECISION
#define ALAC_MAX_LPC_PRECISION
Definition: alacenc.c:41
AlacEncodeContext::pbctx
PutBitContext pbctx
Definition: alacenc.c:78
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
mathops.h
AE
#define AE
Definition: alacenc.c:620
ff_lpc_calc_coefs
int ff_lpc_calc_coefs(LPCContext *s, const int32_t *samples, int blocksize, int min_order, int max_order, int precision, int32_t coefs[][MAX_LPC_ORDER], int *shift, enum FFLPCType lpc_type, int lpc_passes, int omethod, int min_shift, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
Definition: lpc.c:201
AlacEncodeContext::interlacing_shift
int interlacing_shift
Definition: alacenc.c:76
ff_alac_channel_elements
enum AlacRawDataBlockType ff_alac_channel_elements[ALAC_MAX_CHANNELS][5]
Definition: alac_data.c:47
AlacLPCContext
Definition: alacenc.c:57
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
options
static const AVOption options[]
Definition: alacenc.c:621
AlacEncodeContext::max_coded_frame_size
int max_coded_frame_size
Definition: alacenc.c:71
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
TYPE_END
@ TYPE_END
Definition: aac.h:64
MAX_LPC_ORDER
#define MAX_LPC_ORDER
Definition: lpc.h:38
AlacEncodeContext::rc
RiceContext rc
Definition: alacenc.c:79
bps
unsigned bps
Definition: movenc.c:1596
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1000
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
ALAC_MAX_LPC_SHIFT
#define ALAC_MAX_LPC_SHIFT
Definition: alacenc.c:43
MIN_LPC_ORDER
#define MIN_LPC_ORDER
Definition: lpc.h:37
get_max_frame_size
static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
Definition: alacenc.c:492
AlacEncodeContext::max_prediction_order
int max_prediction_order
Definition: alacenc.c:70
RiceContext::history_mult
int history_mult
Definition: alacenc.c:51
ORDER_METHOD_EST
#define ORDER_METHOD_EST
Definition: lpc.h:30
ALAC_CHMODE_LEFT_SIDE
#define ALAC_CHMODE_LEFT_SIDE
Definition: alacenc.c:46
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
ALAC_MAX_LPC_ORDER
#define ALAC_MAX_LPC_ORDER
Definition: alacenc.c:38
ff_lpc_end
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:323
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:993
ALAC_MIN_LPC_SHIFT
#define ALAC_MIN_LPC_SHIFT
Definition: alacenc.c:42
AlacEncodeContext::frame_size
int frame_size
current frame size
Definition: alacenc.c:66
AlacEncodeContext::min_prediction_order
int min_prediction_order
Definition: alacenc.c:69
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
ff_alac_channel_layout_offsets
const uint8_t ff_alac_channel_layout_offsets[ALAC_MAX_CHANNELS][ALAC_MAX_CHANNELS]
Definition: alac_data.c:24
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:484
ff_alac_encoder
const AVCodec ff_alac_encoder
Definition: alacenc.c:635
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
init_sample_buffers
static void init_sample_buffers(AlacEncodeContext *s, int channels, const uint8_t *samples[2])
Definition: alacenc.c:85
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:263
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
TYPE_SCE
@ TYPE_SCE
Definition: aac.h:57
ff_alac_channel_layouts
const uint64_t ff_alac_channel_layouts[ALAC_MAX_CHANNELS+1]
Definition: alac_data.c:35
avcodec.h
AV_WB8
#define AV_WB8(p, d)
Definition: intreadwrite.h:396
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
ALAC_CHMODE_LEFT_RIGHT
#define ALAC_CHMODE_LEFT_RIGHT
Definition: alacenc.c:45
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
write_frame
static int write_frame(AlacEncodeContext *s, AVPacket *avpkt, uint8_t *const *samples)
Definition: alacenc.c:461
AVCodecContext
main external API structure.
Definition: avcodec.h:383
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
estimate_stereo_mode
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
Definition: alacenc.c:185
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:130
AlacLPCContext::lpc_coeff
int lpc_coeff[ALAC_MAX_LPC_ORDER+1]
Definition: alacenc.c:59
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
AlacEncodeContext::sample_buf
int32_t sample_buf[2][DEFAULT_FRAME_SIZE]
Definition: alacenc.c:74
AlacRawDataBlockType
AlacRawDataBlockType
Definition: alac_data.h:26
shift
static int shift(int a, int b)
Definition: sonic.c:83
AlacEncodeContext::lpc_ctx
LPCContext lpc_ctx
Definition: alacenc.c:81
ALAC_CHMODE_RIGHT_SIDE
#define ALAC_CHMODE_RIGHT_SIDE
Definition: alacenc.c:47
alac_encode_close
static av_cold int alac_encode_close(AVCodecContext *avctx)
Definition: alacenc.c:498
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:142
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
ALAC_EXTRADATA_SIZE
#define ALAC_EXTRADATA_SIZE
Definition: alacenc.c:33
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AVPacket
This structure stores compressed data.
Definition: packet.h:350
channel_layouts
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:114
AlacEncodeContext
Definition: alacenc.c:63
int32_t
int32_t
Definition: audioconvert.c:56
AlacEncodeContext::interlacing_leftweight
int interlacing_leftweight
Definition: alacenc.c:77
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
encode_scalar
static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
Definition: alacenc.c:107
RiceContext::initial_history
int initial_history
Definition: alacenc.c:52
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:87
put_bits.h
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:34
FF_LPC_TYPE_LEVINSON
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
Definition: lpc.h:47
ff_lpc_init
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition: lpc.c:301
AVCodecContext::compression_level
int compression_level
Definition: avcodec.h:455