FFmpeg
audiotoolboxenc.c
Go to the documentation of this file.
1 /*
2  * Audio Toolbox system codecs
3  *
4  * copyright (c) 2016 rcombs
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <AudioToolbox/AudioToolbox.h>
24 
25 #define FF_BUFQUEUE_SIZE 256
27 
28 #include "config.h"
29 #include "audio_frame_queue.h"
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "encode.h"
33 #include "internal.h"
34 #include "libavformat/isom.h"
35 #include "libavutil/avassert.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/log.h"
39 
40 typedef struct ATDecodeContext {
42  int mode;
43  int quality;
44 
45  AudioConverterRef converter;
48 
49  unsigned pkt_size;
51  int eof;
53 
56 
57 static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
58 {
59  switch (codec) {
60  case AV_CODEC_ID_AAC:
61  switch (profile) {
62  case FF_PROFILE_AAC_LOW:
63  default:
64  return kAudioFormatMPEG4AAC;
65  case FF_PROFILE_AAC_HE:
66  return kAudioFormatMPEG4AAC_HE;
68  return kAudioFormatMPEG4AAC_HE_V2;
69  case FF_PROFILE_AAC_LD:
70  return kAudioFormatMPEG4AAC_LD;
71  case FF_PROFILE_AAC_ELD:
72  return kAudioFormatMPEG4AAC_ELD;
73  }
75  return kAudioFormatAppleIMA4;
76  case AV_CODEC_ID_ALAC:
77  return kAudioFormatAppleLossless;
78  case AV_CODEC_ID_ILBC:
79  return kAudioFormatiLBC;
81  return kAudioFormatALaw;
83  return kAudioFormatULaw;
84  default:
85  av_assert0(!"Invalid codec ID!");
86  return 0;
87  }
88 }
89 
90 static void ffat_update_ctx(AVCodecContext *avctx)
91 {
92  ATDecodeContext *at = avctx->priv_data;
93  UInt32 size = sizeof(unsigned);
94  AudioConverterPrimeInfo prime_info;
95  AudioStreamBasicDescription out_format;
96 
97  AudioConverterGetProperty(at->converter,
98  kAudioConverterPropertyMaximumOutputPacketSize,
99  &size, &at->pkt_size);
100 
101  if (at->pkt_size <= 0)
102  at->pkt_size = 1024 * 50;
103 
104  size = sizeof(prime_info);
105 
106  if (!AudioConverterGetProperty(at->converter,
107  kAudioConverterPrimeInfo,
108  &size, &prime_info)) {
109  avctx->initial_padding = prime_info.leadingFrames;
110  }
111 
112  size = sizeof(out_format);
113  if (!AudioConverterGetProperty(at->converter,
114  kAudioConverterCurrentOutputStreamDescription,
115  &size, &out_format)) {
116  if (out_format.mFramesPerPacket)
117  avctx->frame_size = out_format.mFramesPerPacket;
118  if (out_format.mBytesPerPacket && avctx->codec_id == AV_CODEC_ID_ILBC)
119  avctx->block_align = out_format.mBytesPerPacket;
120  }
121 
122  at->frame_size = avctx->frame_size;
123  if (avctx->codec_id == AV_CODEC_ID_PCM_MULAW ||
124  avctx->codec_id == AV_CODEC_ID_PCM_ALAW) {
125  at->pkt_size *= 1024;
126  avctx->frame_size *= 1024;
127  }
128 }
129 
130 static int read_descr(GetByteContext *gb, int *tag)
131 {
132  int len = 0;
133  int count = 4;
134  *tag = bytestream2_get_byte(gb);
135  while (count--) {
136  int c = bytestream2_get_byte(gb);
137  len = (len << 7) | (c & 0x7f);
138  if (!(c & 0x80))
139  break;
140  }
141  return len;
142 }
143 
144 static int get_ilbc_mode(AVCodecContext *avctx)
145 {
146  if (avctx->block_align == 38)
147  return 20;
148  else if (avctx->block_align == 50)
149  return 30;
150  else if (avctx->bit_rate > 0)
151  return avctx->bit_rate <= 14000 ? 30 : 20;
152  else
153  return 30;
154 }
155 
157 {
158  uint64_t map = 1 << channel;
159  if (map <= AV_CH_LOW_FREQUENCY)
160  return channel + 1;
161  else if (map <= AV_CH_BACK_RIGHT)
162  return channel + 29;
163  else if (map <= AV_CH_BACK_CENTER)
164  return channel - 1;
165  else if (map <= AV_CH_SIDE_RIGHT)
166  return channel - 4;
167  else if (map <= AV_CH_TOP_BACK_RIGHT)
168  return channel + 1;
169  else if (map <= AV_CH_STEREO_RIGHT)
170  return -1;
171  else if (map <= AV_CH_WIDE_RIGHT)
172  return channel + 4;
173  else if (map <= AV_CH_SURROUND_DIRECT_RIGHT)
174  return channel - 23;
175  else if (map == AV_CH_LOW_FREQUENCY_2)
176  return kAudioChannelLabel_LFE2;
177  else
178  return -1;
179 }
180 
181 static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
182 {
183  int i;
184  int c = 0;
185  layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
186  layout->mNumberChannelDescriptions = count;
187  for (i = 0; i < count; i++) {
188  int label;
189  while (!(in_layout & (1 << c)) && c < 64)
190  c++;
191  if (c == 64)
192  return AVERROR(EINVAL); // This should never happen
193  label = get_channel_label(c);
194  layout->mChannelDescriptions[i].mChannelLabel = label;
195  if (label < 0)
196  return AVERROR(EINVAL);
197  c++;
198  }
199  return 0;
200 }
201 
202 static int get_aac_tag(uint64_t in_layout)
203 {
204  switch (in_layout) {
205  case AV_CH_LAYOUT_MONO:
206  return kAudioChannelLayoutTag_Mono;
207  case AV_CH_LAYOUT_STEREO:
208  return kAudioChannelLayoutTag_Stereo;
209  case AV_CH_LAYOUT_QUAD:
210  return kAudioChannelLayoutTag_AAC_Quadraphonic;
212  return kAudioChannelLayoutTag_AAC_Octagonal;
214  return kAudioChannelLayoutTag_AAC_3_0;
216  return kAudioChannelLayoutTag_AAC_4_0;
218  return kAudioChannelLayoutTag_AAC_5_0;
220  return kAudioChannelLayoutTag_AAC_5_1;
222  return kAudioChannelLayoutTag_AAC_6_0;
224  return kAudioChannelLayoutTag_AAC_6_1;
226  return kAudioChannelLayoutTag_AAC_7_0;
228  return kAudioChannelLayoutTag_AAC_7_1;
230  return kAudioChannelLayoutTag_MPEG_7_1_C;
231  default:
232  return 0;
233  }
234 }
235 
237 {
238  ATDecodeContext *at = avctx->priv_data;
239  OSStatus status;
240 
241  AudioStreamBasicDescription in_format = {
242  .mSampleRate = avctx->sample_rate,
243  .mFormatID = kAudioFormatLinearPCM,
244  .mFormatFlags = ((avctx->sample_fmt == AV_SAMPLE_FMT_FLT ||
245  avctx->sample_fmt == AV_SAMPLE_FMT_DBL) ? kAudioFormatFlagIsFloat
246  : avctx->sample_fmt == AV_SAMPLE_FMT_U8 ? 0
247  : kAudioFormatFlagIsSignedInteger)
248  | kAudioFormatFlagIsPacked,
249  .mBytesPerPacket = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels,
250  .mFramesPerPacket = 1,
251  .mBytesPerFrame = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels,
252  .mChannelsPerFrame = avctx->channels,
253  .mBitsPerChannel = av_get_bytes_per_sample(avctx->sample_fmt) * 8,
254  };
255  AudioStreamBasicDescription out_format = {
256  .mSampleRate = avctx->sample_rate,
257  .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
258  .mChannelsPerFrame = in_format.mChannelsPerFrame,
259  };
260  UInt32 layout_size = sizeof(AudioChannelLayout) +
261  sizeof(AudioChannelDescription) * avctx->channels;
262  AudioChannelLayout *channel_layout = av_malloc(layout_size);
263 
264  if (!channel_layout)
265  return AVERROR(ENOMEM);
266 
267  if (avctx->codec_id == AV_CODEC_ID_ILBC) {
268  int mode = get_ilbc_mode(avctx);
269  out_format.mFramesPerPacket = 8000 * mode / 1000;
270  out_format.mBytesPerPacket = (mode == 20 ? 38 : 50);
271  }
272 
273  status = AudioConverterNew(&in_format, &out_format, &at->converter);
274 
275  if (status != 0) {
276  av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
277  av_free(channel_layout);
278  return AVERROR_UNKNOWN;
279  }
280 
281  if (!avctx->channel_layout)
283 
284  if ((status = remap_layout(channel_layout, avctx->channel_layout, avctx->channels)) < 0) {
285  av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
286  av_free(channel_layout);
287  return status;
288  }
289 
290  if (AudioConverterSetProperty(at->converter, kAudioConverterInputChannelLayout,
291  layout_size, channel_layout)) {
292  av_log(avctx, AV_LOG_ERROR, "Unsupported input channel layout\n");
293  av_free(channel_layout);
294  return AVERROR(EINVAL);
295  }
296  if (avctx->codec_id == AV_CODEC_ID_AAC) {
297  int tag = get_aac_tag(avctx->channel_layout);
298  if (tag) {
299  channel_layout->mChannelLayoutTag = tag;
300  channel_layout->mNumberChannelDescriptions = 0;
301  }
302  }
303  if (AudioConverterSetProperty(at->converter, kAudioConverterOutputChannelLayout,
304  layout_size, channel_layout)) {
305  av_log(avctx, AV_LOG_ERROR, "Unsupported output channel layout\n");
306  av_free(channel_layout);
307  return AVERROR(EINVAL);
308  }
309  av_free(channel_layout);
310 
311  if (avctx->bits_per_raw_sample)
312  AudioConverterSetProperty(at->converter,
313  kAudioConverterPropertyBitDepthHint,
314  sizeof(avctx->bits_per_raw_sample),
315  &avctx->bits_per_raw_sample);
316 
317 #if !TARGET_OS_IPHONE
318  if (at->mode == -1)
319  at->mode = (avctx->flags & AV_CODEC_FLAG_QSCALE) ?
320  kAudioCodecBitRateControlMode_Variable :
321  kAudioCodecBitRateControlMode_Constant;
322 
323  AudioConverterSetProperty(at->converter, kAudioCodecPropertyBitRateControlMode,
324  sizeof(at->mode), &at->mode);
325 
326  if (at->mode == kAudioCodecBitRateControlMode_Variable) {
327  int q = avctx->global_quality / FF_QP2LAMBDA;
328  if (q < 0 || q > 14) {
329  av_log(avctx, AV_LOG_WARNING,
330  "VBR quality %d out of range, should be 0-14\n", q);
331  q = av_clip(q, 0, 14);
332  }
333  q = 127 - q * 9;
334  AudioConverterSetProperty(at->converter, kAudioCodecPropertySoundQualityForVBR,
335  sizeof(q), &q);
336  } else
337 #endif
338  if (avctx->bit_rate > 0) {
339  UInt32 rate = avctx->bit_rate;
340  UInt32 size;
341  status = AudioConverterGetPropertyInfo(at->converter,
342  kAudioConverterApplicableEncodeBitRates,
343  &size, NULL);
344  if (!status && size) {
345  UInt32 new_rate = rate;
346  int count;
347  int i;
348  AudioValueRange *ranges = av_malloc(size);
349  if (!ranges)
350  return AVERROR(ENOMEM);
351  AudioConverterGetProperty(at->converter,
352  kAudioConverterApplicableEncodeBitRates,
353  &size, ranges);
354  count = size / sizeof(AudioValueRange);
355  for (i = 0; i < count; i++) {
356  AudioValueRange *range = &ranges[i];
357  if (rate >= range->mMinimum && rate <= range->mMaximum) {
358  new_rate = rate;
359  break;
360  } else if (rate > range->mMaximum) {
361  new_rate = range->mMaximum;
362  } else {
363  new_rate = range->mMinimum;
364  break;
365  }
366  }
367  if (new_rate != rate) {
368  av_log(avctx, AV_LOG_WARNING,
369  "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
370  rate = new_rate;
371  }
372  av_free(ranges);
373  }
374  AudioConverterSetProperty(at->converter, kAudioConverterEncodeBitRate,
375  sizeof(rate), &rate);
376  }
377 
378  at->quality = 96 - at->quality * 32;
379  AudioConverterSetProperty(at->converter, kAudioConverterCodecQuality,
380  sizeof(at->quality), &at->quality);
381 
382  if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterCompressionMagicCookie,
383  &avctx->extradata_size, NULL) &&
384  avctx->extradata_size) {
385  int extradata_size = avctx->extradata_size;
386  uint8_t *extradata;
388  return AVERROR(ENOMEM);
389  if (avctx->codec_id == AV_CODEC_ID_ALAC) {
390  avctx->extradata_size = 0x24;
391  AV_WB32(avctx->extradata, 0x24);
392  AV_WB32(avctx->extradata + 4, MKBETAG('a','l','a','c'));
393  extradata = avctx->extradata + 12;
394  avctx->extradata_size = 0x24;
395  } else {
396  extradata = avctx->extradata;
397  }
398  status = AudioConverterGetProperty(at->converter,
399  kAudioConverterCompressionMagicCookie,
400  &extradata_size, extradata);
401  if (status != 0) {
402  av_log(avctx, AV_LOG_ERROR, "AudioToolbox cookie error: %i\n", (int)status);
403  return AVERROR_UNKNOWN;
404  } else if (avctx->codec_id == AV_CODEC_ID_AAC) {
405  GetByteContext gb;
406  int tag, len;
407  bytestream2_init(&gb, extradata, extradata_size);
408  do {
409  len = read_descr(&gb, &tag);
410  if (tag == MP4DecConfigDescrTag) {
411  bytestream2_skip(&gb, 13);
412  len = read_descr(&gb, &tag);
413  if (tag == MP4DecSpecificDescrTag) {
414  len = FFMIN(gb.buffer_end - gb.buffer, len);
415  memmove(extradata, gb.buffer, len);
416  avctx->extradata_size = len;
417  break;
418  }
419  } else if (tag == MP4ESDescrTag) {
420  int flags;
421  bytestream2_skip(&gb, 2);
422  flags = bytestream2_get_byte(&gb);
423  if (flags & 0x80) //streamDependenceFlag
424  bytestream2_skip(&gb, 2);
425  if (flags & 0x40) //URL_Flag
426  bytestream2_skip(&gb, bytestream2_get_byte(&gb));
427  if (flags & 0x20) //OCRstreamFlag
428  bytestream2_skip(&gb, 2);
429  }
430  } while (bytestream2_get_bytes_left(&gb));
431  } else if (avctx->codec_id != AV_CODEC_ID_ALAC) {
432  avctx->extradata_size = extradata_size;
433  }
434  }
435 
436  ffat_update_ctx(avctx);
437 
438 #if !TARGET_OS_IPHONE && defined(__MAC_10_9)
439  if (at->mode == kAudioCodecBitRateControlMode_Variable && avctx->rc_max_rate) {
440  UInt32 max_size = avctx->rc_max_rate * avctx->frame_size / avctx->sample_rate;
441  if (max_size)
442  AudioConverterSetProperty(at->converter, kAudioCodecPropertyPacketSizeLimitForVBR,
443  sizeof(max_size), &max_size);
444  }
445 #endif
446 
447  ff_af_queue_init(avctx, &at->afq);
448 
450  if (!at->encoding_frame)
451  return AVERROR(ENOMEM);
452 
453  return 0;
454 }
455 
456 static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets,
457  AudioBufferList *data,
458  AudioStreamPacketDescription **packets,
459  void *inctx)
460 {
461  AVCodecContext *avctx = inctx;
462  ATDecodeContext *at = avctx->priv_data;
463  AVFrame *frame;
464  int ret;
465 
466  if (!at->frame_queue.available) {
467  if (at->eof) {
468  *nb_packets = 0;
469  return 0;
470  } else {
471  *nb_packets = 0;
472  return 1;
473  }
474  }
475 
477 
478  data->mNumberBuffers = 1;
479  data->mBuffers[0].mNumberChannels = avctx->channels;
480  data->mBuffers[0].mDataByteSize = frame->nb_samples *
482  avctx->channels;
483  data->mBuffers[0].mData = frame->data[0];
484  if (*nb_packets > frame->nb_samples)
485  *nb_packets = frame->nb_samples;
486 
489  if (ret < 0) {
490  *nb_packets = 0;
491  return ret;
492  }
493 
494  ff_bufqueue_add(avctx, &at->used_frame_queue, frame);
495 
496  return 0;
497 }
498 
499 static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt,
500  const AVFrame *frame, int *got_packet_ptr)
501 {
502  ATDecodeContext *at = avctx->priv_data;
503  OSStatus ret;
504 
505  AudioBufferList out_buffers = {
506  .mNumberBuffers = 1,
507  .mBuffers = {
508  {
509  .mNumberChannels = avctx->channels,
510  .mDataByteSize = at->pkt_size,
511  }
512  }
513  };
514  AudioStreamPacketDescription out_pkt_desc = {0};
515 
516  if (frame) {
517  AVFrame *in_frame;
518 
519  if (ff_bufqueue_is_full(&at->frame_queue)) {
520  /*
521  * The frame queue is significantly larger than needed in practice,
522  * but no clear way to determine the minimum number of samples to
523  * get output from AudioConverterFillComplexBuffer().
524  */
525  av_log(avctx, AV_LOG_ERROR, "Bug: frame queue is too small.\n");
526  return AVERROR_BUG;
527  }
528 
529  if ((ret = ff_af_queue_add(&at->afq, frame)) < 0)
530  return ret;
531 
532  in_frame = av_frame_clone(frame);
533  if (!in_frame)
534  return AVERROR(ENOMEM);
535 
536  ff_bufqueue_add(avctx, &at->frame_queue, in_frame);
537  } else {
538  at->eof = 1;
539  }
540 
541  if ((ret = ff_alloc_packet(avctx, avpkt, at->pkt_size)) < 0)
542  return ret;
543 
544 
545  out_buffers.mBuffers[0].mData = avpkt->data;
546 
547  *got_packet_ptr = avctx->frame_size / at->frame_size;
548 
549  ret = AudioConverterFillComplexBuffer(at->converter, ffat_encode_callback, avctx,
550  got_packet_ptr, &out_buffers,
551  (avctx->frame_size > at->frame_size) ? NULL : &out_pkt_desc);
552 
554 
555  if ((!ret || ret == 1) && *got_packet_ptr) {
556  avpkt->size = out_buffers.mBuffers[0].mDataByteSize;
557  ff_af_queue_remove(&at->afq, out_pkt_desc.mVariableFramesInPacket ?
558  out_pkt_desc.mVariableFramesInPacket :
559  avctx->frame_size,
560  &avpkt->pts,
561  &avpkt->duration);
562  } else if (ret && ret != 1) {
563  av_log(avctx, AV_LOG_WARNING, "Encode error: %i\n", ret);
564  }
565 
566  return 0;
567 }
568 
570 {
571  ATDecodeContext *at = avctx->priv_data;
572  AudioConverterReset(at->converter);
575 }
576 
578 {
579  ATDecodeContext *at = avctx->priv_data;
580  AudioConverterDispose(at->converter);
583  ff_af_queue_close(&at->afq);
585  return 0;
586 }
587 
588 static const AVProfile aac_profiles[] = {
589  { FF_PROFILE_AAC_LOW, "LC" },
590  { FF_PROFILE_AAC_HE, "HE-AAC" },
591  { FF_PROFILE_AAC_HE_V2, "HE-AACv2" },
592  { FF_PROFILE_AAC_LD, "LD" },
593  { FF_PROFILE_AAC_ELD, "ELD" },
594  { FF_PROFILE_UNKNOWN },
595 };
596 
597 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
598 static const AVOption options[] = {
599 #if !TARGET_OS_IPHONE
600  {"aac_at_mode", "ratecontrol mode", offsetof(ATDecodeContext, mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable, AE, "mode"},
601  {"auto", "VBR if global quality is given; CBR otherwise", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, AE, "mode"},
602  {"cbr", "constant bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX, AE, "mode"},
603  {"abr", "long-term average bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX, AE, "mode"},
604  {"cvbr", "constrained variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX, AE, "mode"},
605  {"vbr" , "variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX, AE, "mode"},
606 #endif
607  {"aac_at_quality", "quality vs speed control", offsetof(ATDecodeContext, quality), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2, AE},
608  { NULL },
609 };
610 
611 #define FFAT_ENC_CLASS(NAME) \
612  static const AVClass ffat_##NAME##_enc_class = { \
613  .class_name = "at_" #NAME "_enc", \
614  .item_name = av_default_item_name, \
615  .option = options, \
616  .version = LIBAVUTIL_VERSION_INT, \
617  };
618 
619 #define FFAT_ENC(NAME, ID, PROFILES, ...) \
620  FFAT_ENC_CLASS(NAME) \
621  const AVCodec ff_##NAME##_at_encoder = { \
622  .name = #NAME "_at", \
623  .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
624  .type = AVMEDIA_TYPE_AUDIO, \
625  .id = ID, \
626  .priv_data_size = sizeof(ATDecodeContext), \
627  .init = ffat_init_encoder, \
628  .close = ffat_close_encoder, \
629  .encode2 = ffat_encode, \
630  .flush = ffat_encode_flush, \
631  .priv_class = &ffat_##NAME##_enc_class, \
632  .capabilities = AV_CODEC_CAP_DELAY | \
633  AV_CODEC_CAP_ENCODER_FLUSH __VA_ARGS__, \
634  .sample_fmts = (const enum AVSampleFormat[]) { \
635  AV_SAMPLE_FMT_S16, \
636  AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
637  }, \
638  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
639  .profiles = PROFILES, \
640  .wrapper_name = "at", \
641  };
642 
643 static const uint64_t aac_at_channel_layouts[] = {
656  0,
657 };
658 
660 //FFAT_ENC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT, NULL)
AV_CH_LAYOUT_7POINT0
#define AV_CH_LAYOUT_7POINT0
Definition: channel_layout.h:110
read_descr
static int read_descr(GetByteContext *gb, int *tag)
Definition: audiotoolboxenc.c:130
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1012
AV_CH_LAYOUT_6POINT1
#define AV_CH_LAYOUT_6POINT1
Definition: channel_layout.h:107
get_aac_tag
static int get_aac_tag(uint64_t in_layout)
Definition: audiotoolboxenc.c:202
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AV_CH_LAYOUT_7POINT1_WIDE_BACK
#define AV_CH_LAYOUT_7POINT1_WIDE_BACK
Definition: channel_layout.h:114
AV_CODEC_ID_ADPCM_IMA_QT
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition: codec_id.h:353
status
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
MP4DecConfigDescrTag
#define MP4DecConfigDescrTag
Definition: isom.h:319
av_clip
#define av_clip
Definition: common.h:96
get_channel_label
static av_cold int get_channel_label(int channel)
Definition: audiotoolboxenc.c:156
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
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1043
ff_af_queue_remove
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
Definition: audio_frame_queue.c:75
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:992
GetByteContext
Definition: bytestream.h:33
AV_CH_LOW_FREQUENCY_2
#define AV_CH_LOW_FREQUENCY_2
Definition: channel_layout.h:73
ff_af_queue_close
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
Definition: audio_frame_queue.c:36
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:210
ff_af_queue_init
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
Definition: audio_frame_queue.c:28
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
encode.h
data
const char data[16]
Definition: mxf.c:143
AV_CODEC_ID_ALAC
@ AV_CODEC_ID_ALAC
Definition: codec_id.h:439
ffat_init_encoder
static av_cold int ffat_init_encoder(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:236
get_ilbc_mode
static int get_ilbc_mode(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:144
aac_profiles
static const AVProfile aac_profiles[]
Definition: audiotoolboxenc.c:588
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:391
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
ffat_get_format_id
static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
Definition: audiotoolboxenc.c:57
quality
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
Definition: rate_distortion.txt:12
ff_bufqueue_get
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
Definition: bufferqueue.h:98
AV_CH_SURROUND_DIRECT_RIGHT
#define AV_CH_SURROUND_DIRECT_RIGHT
Definition: channel_layout.h:72
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
AV_CH_WIDE_RIGHT
#define AV_CH_WIDE_RIGHT
Definition: channel_layout.h:70
AVProfile
AVProfile.
Definition: codec.h:188
AV_CH_LAYOUT_6POINT0
#define AV_CH_LAYOUT_6POINT0
Definition: channel_layout.h:104
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
ATDecodeContext::used_frame_queue
struct FFBufQueue used_frame_queue
Definition: audiotoolboxenc.c:47
ATDecodeContext::frame_size
int frame_size
Definition: audiotoolboxenc.c:52
ffat_close_encoder
static av_cold int ffat_close_encoder(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:577
ATDecodeContext::eof
int eof
Definition: audiotoolboxdec.c:54
MP4DecSpecificDescrTag
#define MP4DecSpecificDescrTag
Definition: isom.h:320
AE
#define AE
Definition: audiotoolboxenc.c:597
audio_frame_queue.h
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:1701
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:463
ffat_encode_callback
static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets, AudioBufferList *data, AudioStreamPacketDescription **packets, void *inctx)
Definition: audiotoolboxenc.c:456
ff_af_queue_add
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
Definition: audio_frame_queue.c:44
ATDecodeContext::mode
int mode
Definition: audiotoolboxenc.c:42
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_QUAD
Definition: channel_layout.h:99
ff_bufqueue_is_full
static int ff_bufqueue_is_full(struct FFBufQueue *queue)
Test if a buffer queue is full.
Definition: bufferqueue.h:60
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:97
options
static const AVOption options[]
Definition: audiotoolboxenc.c:598
avassert.h
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
FF_PROFILE_AAC_HE_V2
#define FF_PROFILE_AAC_HE_V2
Definition: avcodec.h:1534
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:52
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:485
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:449
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
FF_PROFILE_UNKNOWN
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1526
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AudioFrameQueue
Definition: audio_frame_queue.h:32
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1425
ATDecodeContext::encoding_frame
AVFrame * encoding_frame
Definition: audiotoolboxenc.c:54
FFAT_ENC
#define FFAT_ENC(NAME, ID, PROFILES,...)
Definition: audiotoolboxenc.c:619
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:422
AV_CODEC_ID_PCM_MULAW
@ AV_CODEC_ID_PCM_MULAW
Definition: codec_id.h:320
AVCodecContext::rc_max_rate
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1194
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:393
AV_CH_STEREO_RIGHT
#define AV_CH_STEREO_RIGHT
See AV_CH_STEREO_LEFT.
Definition: channel_layout.h:68
MP4ESDescrTag
#define MP4ESDescrTag
Definition: isom.h:318
AV_CODEC_ID_PCM_ALAW
@ AV_CODEC_ID_PCM_ALAW
Definition: codec_id.h:321
ATDecodeContext::quality
int quality
Definition: audiotoolboxenc.c:43
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
isom.h
ffat_update_ctx
static void ffat_update_ctx(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:90
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:433
FF_PROFILE_AAC_LD
#define FF_PROFILE_AAC_LD
Definition: avcodec.h:1535
ff_bufqueue_discard_all
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
Definition: bufferqueue.h:111
AV_CH_LAYOUT_5POINT1
#define AV_CH_LAYOUT_5POINT1
Definition: channel_layout.h:101
AV_CODEC_CAP_VARIABLE_FRAME_SIZE
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition: codec.h:134
FF_PROFILE_AAC_ELD
#define FF_PROFILE_AAC_ELD
Definition: avcodec.h:1536
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:425
bufferqueue.h
AVPacket::size
int size
Definition: packet.h:374
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:325
FF_PROFILE_AAC_LOW
#define FF_PROFILE_AAC_LOW
Definition: avcodec.h:1530
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1000
size
int size
Definition: twinvq_data.h:10344
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
ff_bufqueue_add
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
Definition: bufferqueue.h:71
AV_CH_TOP_BACK_RIGHT
#define AV_CH_TOP_BACK_RIGHT
Definition: channel_layout.h:66
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:993
layout
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 layout
Definition: filter_design.txt:18
AV_CH_LAYOUT_OCTAGONAL
#define AV_CH_LAYOUT_OCTAGONAL
Definition: channel_layout.h:115
AV_CH_LAYOUT_5POINT0
#define AV_CH_LAYOUT_5POINT0
Definition: channel_layout.h:100
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
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
FFBufQueue
Structure holding the queue.
Definition: bufferqueue.h:49
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:60
FFBufQueue::available
unsigned short available
number of available buffers
Definition: bufferqueue.h:52
AV_CH_LAYOUT_7POINT1
#define AV_CH_LAYOUT_7POINT1
Definition: channel_layout.h:112
AV_CH_BACK_CENTER
#define AV_CH_BACK_CENTER
Definition: channel_layout.h:57
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:435
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
AV_CH_SIDE_RIGHT
#define AV_CH_SIDE_RIGHT
Definition: channel_layout.h:59
len
int len
Definition: vorbis_enc_data.h:426
profile
int profile
Definition: mxfenc.c:2003
GetByteContext::buffer_end
const uint8_t * buffer_end
Definition: bytestream.h:34
avcodec.h
FF_PROFILE_AAC_HE
#define FF_PROFILE_AAC_HE
Definition: avcodec.h:1533
tag
uint32_t tag
Definition: movenc.c:1596
ret
ret
Definition: filter_design.txt:187
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: avcodec.h:1029
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
AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_SURROUND
Definition: channel_layout.h:94
ffat_encode_flush
static av_cold void ffat_encode_flush(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:569
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
ATDecodeContext::converter
AudioConverterRef converter
Definition: audiotoolboxdec.c:43
AVCodecContext
main external API structure.
Definition: avcodec.h:383
channel_layout.h
ffat_encode
static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: audiotoolboxenc.c:499
mode
mode
Definition: ebur128.h:83
ATDecodeContext
Definition: audiotoolboxdec.c:40
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1525
av_get_default_channel_layout
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
Definition: channel_layout.c:231
remap_layout
static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
Definition: audiotoolboxenc.c:181
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
channel_layouts
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:114
ATDecodeContext::pkt_size
unsigned pkt_size
Definition: audiotoolboxenc.c:49
AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_4POINT0
Definition: channel_layout.h:96
AV_CODEC_ID_ILBC
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:482
bytestream.h
ATDecodeContext::av_class
AVClass * av_class
Definition: audiotoolboxdec.c:41
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
ATDecodeContext::afq
AudioFrameQueue afq
Definition: audiotoolboxenc.c:50
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_CH_BACK_RIGHT
#define AV_CH_BACK_RIGHT
Definition: channel_layout.h:54
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
aac_at_channel_layouts
static const uint64_t aac_at_channel_layouts[]
Definition: audiotoolboxenc.c:643
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:34
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
channel
channel
Definition: ebur128.h:39
ATDecodeContext::frame_queue
struct FFBufQueue frame_queue
Definition: audiotoolboxenc.c:46