FFmpeg
audiotoolboxenc.c
Go to the documentation of this file.
1 /*
2  * Audio Toolbox system codecs
3  *
4  * copyright (c) 2016 Rodger Combs
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 "internal.h"
33 #include "libavformat/isom.h"
34 #include "libavutil/avassert.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/log.h"
37 
38 typedef struct ATDecodeContext {
40  int mode;
41  int quality;
42 
43  AudioConverterRef converter;
46 
47  unsigned pkt_size;
49  int eof;
51 
54 
55 static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
56 {
57  switch (codec) {
58  case AV_CODEC_ID_AAC:
59  switch (profile) {
60  case FF_PROFILE_AAC_LOW:
61  default:
62  return kAudioFormatMPEG4AAC;
63  case FF_PROFILE_AAC_HE:
64  return kAudioFormatMPEG4AAC_HE;
66  return kAudioFormatMPEG4AAC_HE_V2;
67  case FF_PROFILE_AAC_LD:
68  return kAudioFormatMPEG4AAC_LD;
69  case FF_PROFILE_AAC_ELD:
70  return kAudioFormatMPEG4AAC_ELD;
71  }
73  return kAudioFormatAppleIMA4;
74  case AV_CODEC_ID_ALAC:
75  return kAudioFormatAppleLossless;
76  case AV_CODEC_ID_ILBC:
77  return kAudioFormatiLBC;
79  return kAudioFormatALaw;
81  return kAudioFormatULaw;
82  default:
83  av_assert0(!"Invalid codec ID!");
84  return 0;
85  }
86 }
87 
88 static void ffat_update_ctx(AVCodecContext *avctx)
89 {
90  ATDecodeContext *at = avctx->priv_data;
91  UInt32 size = sizeof(unsigned);
92  AudioConverterPrimeInfo prime_info;
93  AudioStreamBasicDescription out_format;
94 
95  AudioConverterGetProperty(at->converter,
96  kAudioConverterPropertyMaximumOutputPacketSize,
97  &size, &at->pkt_size);
98 
99  if (at->pkt_size <= 0)
100  at->pkt_size = 1024 * 50;
101 
102  size = sizeof(prime_info);
103 
104  if (!AudioConverterGetProperty(at->converter,
105  kAudioConverterPrimeInfo,
106  &size, &prime_info)) {
107  avctx->initial_padding = prime_info.leadingFrames;
108  }
109 
110  size = sizeof(out_format);
111  if (!AudioConverterGetProperty(at->converter,
112  kAudioConverterCurrentOutputStreamDescription,
113  &size, &out_format)) {
114  if (out_format.mFramesPerPacket)
115  avctx->frame_size = out_format.mFramesPerPacket;
116  if (out_format.mBytesPerPacket && avctx->codec_id == AV_CODEC_ID_ILBC)
117  avctx->block_align = out_format.mBytesPerPacket;
118  }
119 
120  at->frame_size = avctx->frame_size;
121  if (avctx->codec_id == AV_CODEC_ID_PCM_MULAW ||
122  avctx->codec_id == AV_CODEC_ID_PCM_ALAW) {
123  at->pkt_size *= 1024;
124  avctx->frame_size *= 1024;
125  }
126 }
127 
128 static int read_descr(GetByteContext *gb, int *tag)
129 {
130  int len = 0;
131  int count = 4;
132  *tag = bytestream2_get_byte(gb);
133  while (count--) {
134  int c = bytestream2_get_byte(gb);
135  len = (len << 7) | (c & 0x7f);
136  if (!(c & 0x80))
137  break;
138  }
139  return len;
140 }
141 
142 static int get_ilbc_mode(AVCodecContext *avctx)
143 {
144  if (avctx->block_align == 38)
145  return 20;
146  else if (avctx->block_align == 50)
147  return 30;
148  else if (avctx->bit_rate > 0)
149  return avctx->bit_rate <= 14000 ? 30 : 20;
150  else
151  return 30;
152 }
153 
155 {
156  uint64_t map = 1 << channel;
157  if (map <= AV_CH_LOW_FREQUENCY)
158  return channel + 1;
159  else if (map <= AV_CH_BACK_RIGHT)
160  return channel + 29;
161  else if (map <= AV_CH_BACK_CENTER)
162  return channel - 1;
163  else if (map <= AV_CH_SIDE_RIGHT)
164  return channel - 4;
165  else if (map <= AV_CH_TOP_BACK_RIGHT)
166  return channel + 1;
167  else if (map <= AV_CH_STEREO_RIGHT)
168  return -1;
169  else if (map <= AV_CH_WIDE_RIGHT)
170  return channel + 4;
171  else if (map <= AV_CH_SURROUND_DIRECT_RIGHT)
172  return channel - 23;
173  else if (map == AV_CH_LOW_FREQUENCY_2)
174  return kAudioChannelLabel_LFE2;
175  else
176  return -1;
177 }
178 
179 static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
180 {
181  int i;
182  int c = 0;
183  layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
184  layout->mNumberChannelDescriptions = count;
185  for (i = 0; i < count; i++) {
186  int label;
187  while (!(in_layout & (1 << c)) && c < 64)
188  c++;
189  if (c == 64)
190  return AVERROR(EINVAL); // This should never happen
191  label = get_channel_label(c);
192  layout->mChannelDescriptions[i].mChannelLabel = label;
193  if (label < 0)
194  return AVERROR(EINVAL);
195  c++;
196  }
197  return 0;
198 }
199 
200 static int get_aac_tag(uint64_t in_layout)
201 {
202  switch (in_layout) {
203  case AV_CH_LAYOUT_MONO:
204  return kAudioChannelLayoutTag_Mono;
205  case AV_CH_LAYOUT_STEREO:
206  return kAudioChannelLayoutTag_Stereo;
207  case AV_CH_LAYOUT_QUAD:
208  return kAudioChannelLayoutTag_AAC_Quadraphonic;
210  return kAudioChannelLayoutTag_AAC_Octagonal;
212  return kAudioChannelLayoutTag_AAC_3_0;
214  return kAudioChannelLayoutTag_AAC_4_0;
216  return kAudioChannelLayoutTag_AAC_5_0;
218  return kAudioChannelLayoutTag_AAC_5_1;
220  return kAudioChannelLayoutTag_AAC_6_0;
222  return kAudioChannelLayoutTag_AAC_6_1;
224  return kAudioChannelLayoutTag_AAC_7_0;
226  return kAudioChannelLayoutTag_AAC_7_1;
228  return kAudioChannelLayoutTag_MPEG_7_1_C;
229  default:
230  return 0;
231  }
232 }
233 
235 {
236  ATDecodeContext *at = avctx->priv_data;
237  OSStatus status;
238 
239  AudioStreamBasicDescription in_format = {
240  .mSampleRate = avctx->sample_rate,
241  .mFormatID = kAudioFormatLinearPCM,
242  .mFormatFlags = ((avctx->sample_fmt == AV_SAMPLE_FMT_FLT ||
243  avctx->sample_fmt == AV_SAMPLE_FMT_DBL) ? kAudioFormatFlagIsFloat
244  : avctx->sample_fmt == AV_SAMPLE_FMT_U8 ? 0
245  : kAudioFormatFlagIsSignedInteger)
246  | kAudioFormatFlagIsPacked,
247  .mBytesPerPacket = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels,
248  .mFramesPerPacket = 1,
249  .mBytesPerFrame = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels,
250  .mChannelsPerFrame = avctx->channels,
251  .mBitsPerChannel = av_get_bytes_per_sample(avctx->sample_fmt) * 8,
252  };
253  AudioStreamBasicDescription out_format = {
254  .mSampleRate = avctx->sample_rate,
255  .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
256  .mChannelsPerFrame = in_format.mChannelsPerFrame,
257  };
258  UInt32 layout_size = sizeof(AudioChannelLayout) +
259  sizeof(AudioChannelDescription) * avctx->channels;
260  AudioChannelLayout *channel_layout = av_malloc(layout_size);
261 
262  if (!channel_layout)
263  return AVERROR(ENOMEM);
264 
265  if (avctx->codec_id == AV_CODEC_ID_ILBC) {
266  int mode = get_ilbc_mode(avctx);
267  out_format.mFramesPerPacket = 8000 * mode / 1000;
268  out_format.mBytesPerPacket = (mode == 20 ? 38 : 50);
269  }
270 
271  status = AudioConverterNew(&in_format, &out_format, &at->converter);
272 
273  if (status != 0) {
274  av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
275  av_free(channel_layout);
276  return AVERROR_UNKNOWN;
277  }
278 
279  if (!avctx->channel_layout)
281 
282  if ((status = remap_layout(channel_layout, avctx->channel_layout, avctx->channels)) < 0) {
283  av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
284  av_free(channel_layout);
285  return status;
286  }
287 
288  if (AudioConverterSetProperty(at->converter, kAudioConverterInputChannelLayout,
289  layout_size, channel_layout)) {
290  av_log(avctx, AV_LOG_ERROR, "Unsupported input channel layout\n");
291  av_free(channel_layout);
292  return AVERROR(EINVAL);
293  }
294  if (avctx->codec_id == AV_CODEC_ID_AAC) {
295  int tag = get_aac_tag(avctx->channel_layout);
296  if (tag) {
297  channel_layout->mChannelLayoutTag = tag;
298  channel_layout->mNumberChannelDescriptions = 0;
299  }
300  }
301  if (AudioConverterSetProperty(at->converter, kAudioConverterOutputChannelLayout,
302  layout_size, channel_layout)) {
303  av_log(avctx, AV_LOG_ERROR, "Unsupported output channel layout\n");
304  av_free(channel_layout);
305  return AVERROR(EINVAL);
306  }
307  av_free(channel_layout);
308 
309  if (avctx->bits_per_raw_sample)
310  AudioConverterSetProperty(at->converter,
311  kAudioConverterPropertyBitDepthHint,
312  sizeof(avctx->bits_per_raw_sample),
313  &avctx->bits_per_raw_sample);
314 
315 #if !TARGET_OS_IPHONE
316  if (at->mode == -1)
317  at->mode = (avctx->flags & AV_CODEC_FLAG_QSCALE) ?
318  kAudioCodecBitRateControlMode_Variable :
319  kAudioCodecBitRateControlMode_Constant;
320 
321  AudioConverterSetProperty(at->converter, kAudioCodecPropertyBitRateControlMode,
322  sizeof(at->mode), &at->mode);
323 
324  if (at->mode == kAudioCodecBitRateControlMode_Variable) {
325  int q = avctx->global_quality / FF_QP2LAMBDA;
326  if (q < 0 || q > 14) {
327  av_log(avctx, AV_LOG_WARNING,
328  "VBR quality %d out of range, should be 0-14\n", q);
329  q = av_clip(q, 0, 14);
330  }
331  q = 127 - q * 9;
332  AudioConverterSetProperty(at->converter, kAudioCodecPropertySoundQualityForVBR,
333  sizeof(q), &q);
334  } else
335 #endif
336  if (avctx->bit_rate > 0) {
337  UInt32 rate = avctx->bit_rate;
338  UInt32 size;
339  status = AudioConverterGetPropertyInfo(at->converter,
340  kAudioConverterApplicableEncodeBitRates,
341  &size, NULL);
342  if (!status && size) {
343  UInt32 new_rate = rate;
344  int count;
345  int i;
346  AudioValueRange *ranges = av_malloc(size);
347  if (!ranges)
348  return AVERROR(ENOMEM);
349  AudioConverterGetProperty(at->converter,
350  kAudioConverterApplicableEncodeBitRates,
351  &size, ranges);
352  count = size / sizeof(AudioValueRange);
353  for (i = 0; i < count; i++) {
354  AudioValueRange *range = &ranges[i];
355  if (rate >= range->mMinimum && rate <= range->mMaximum) {
356  new_rate = rate;
357  break;
358  } else if (rate > range->mMaximum) {
359  new_rate = range->mMaximum;
360  } else {
361  new_rate = range->mMinimum;
362  break;
363  }
364  }
365  if (new_rate != rate) {
366  av_log(avctx, AV_LOG_WARNING,
367  "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
368  rate = new_rate;
369  }
370  av_free(ranges);
371  }
372  AudioConverterSetProperty(at->converter, kAudioConverterEncodeBitRate,
373  sizeof(rate), &rate);
374  }
375 
376  at->quality = 96 - at->quality * 32;
377  AudioConverterSetProperty(at->converter, kAudioConverterCodecQuality,
378  sizeof(at->quality), &at->quality);
379 
380  if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterCompressionMagicCookie,
381  &avctx->extradata_size, NULL) &&
382  avctx->extradata_size) {
383  int extradata_size = avctx->extradata_size;
384  uint8_t *extradata;
386  return AVERROR(ENOMEM);
387  if (avctx->codec_id == AV_CODEC_ID_ALAC) {
388  avctx->extradata_size = 0x24;
389  AV_WB32(avctx->extradata, 0x24);
390  AV_WB32(avctx->extradata + 4, MKBETAG('a','l','a','c'));
391  extradata = avctx->extradata + 12;
392  avctx->extradata_size = 0x24;
393  } else {
394  extradata = avctx->extradata;
395  }
396  status = AudioConverterGetProperty(at->converter,
397  kAudioConverterCompressionMagicCookie,
398  &extradata_size, extradata);
399  if (status != 0) {
400  av_log(avctx, AV_LOG_ERROR, "AudioToolbox cookie error: %i\n", (int)status);
401  return AVERROR_UNKNOWN;
402  } else if (avctx->codec_id == AV_CODEC_ID_AAC) {
403  GetByteContext gb;
404  int tag, len;
405  bytestream2_init(&gb, extradata, extradata_size);
406  do {
407  len = read_descr(&gb, &tag);
408  if (tag == MP4DecConfigDescrTag) {
409  bytestream2_skip(&gb, 13);
410  len = read_descr(&gb, &tag);
411  if (tag == MP4DecSpecificDescrTag) {
412  len = FFMIN(gb.buffer_end - gb.buffer, len);
413  memmove(extradata, gb.buffer, len);
414  avctx->extradata_size = len;
415  break;
416  }
417  } else if (tag == MP4ESDescrTag) {
418  int flags;
419  bytestream2_skip(&gb, 2);
420  flags = bytestream2_get_byte(&gb);
421  if (flags & 0x80) //streamDependenceFlag
422  bytestream2_skip(&gb, 2);
423  if (flags & 0x40) //URL_Flag
424  bytestream2_skip(&gb, bytestream2_get_byte(&gb));
425  if (flags & 0x20) //OCRstreamFlag
426  bytestream2_skip(&gb, 2);
427  }
428  } while (bytestream2_get_bytes_left(&gb));
429  } else if (avctx->codec_id != AV_CODEC_ID_ALAC) {
430  avctx->extradata_size = extradata_size;
431  }
432  }
433 
434  ffat_update_ctx(avctx);
435 
436 #if !TARGET_OS_IPHONE && defined(__MAC_10_9)
437  if (at->mode == kAudioCodecBitRateControlMode_Variable && avctx->rc_max_rate) {
438  UInt32 max_size = avctx->rc_max_rate * avctx->frame_size / avctx->sample_rate;
439  if (max_size)
440  AudioConverterSetProperty(at->converter, kAudioCodecPropertyPacketSizeLimitForVBR,
441  sizeof(max_size), &max_size);
442  }
443 #endif
444 
445  ff_af_queue_init(avctx, &at->afq);
446 
448  if (!at->encoding_frame)
449  return AVERROR(ENOMEM);
450 
451  return 0;
452 }
453 
454 static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets,
455  AudioBufferList *data,
456  AudioStreamPacketDescription **packets,
457  void *inctx)
458 {
459  AVCodecContext *avctx = inctx;
460  ATDecodeContext *at = avctx->priv_data;
461  AVFrame *frame;
462  int ret;
463 
464  if (!at->frame_queue.available) {
465  if (at->eof) {
466  *nb_packets = 0;
467  return 0;
468  } else {
469  *nb_packets = 0;
470  return 1;
471  }
472  }
473 
475 
476  data->mNumberBuffers = 1;
477  data->mBuffers[0].mNumberChannels = avctx->channels;
478  data->mBuffers[0].mDataByteSize = frame->nb_samples *
480  avctx->channels;
481  data->mBuffers[0].mData = frame->data[0];
482  if (*nb_packets > frame->nb_samples)
483  *nb_packets = frame->nb_samples;
484 
487  if (ret < 0) {
488  *nb_packets = 0;
489  return ret;
490  }
491 
492  ff_bufqueue_add(avctx, &at->used_frame_queue, frame);
493 
494  return 0;
495 }
496 
497 static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt,
498  const AVFrame *frame, int *got_packet_ptr)
499 {
500  ATDecodeContext *at = avctx->priv_data;
501  OSStatus ret;
502 
503  AudioBufferList out_buffers = {
504  .mNumberBuffers = 1,
505  .mBuffers = {
506  {
507  .mNumberChannels = avctx->channels,
508  .mDataByteSize = at->pkt_size,
509  }
510  }
511  };
512  AudioStreamPacketDescription out_pkt_desc = {0};
513 
514  if (frame) {
515  AVFrame *in_frame;
516 
517  if (ff_bufqueue_is_full(&at->frame_queue)) {
518  /*
519  * The frame queue is significantly larger than needed in practice,
520  * but no clear way to determine the minimum number of samples to
521  * get output from AudioConverterFillComplexBuffer().
522  */
523  av_log(avctx, AV_LOG_ERROR, "Bug: frame queue is too small.\n");
524  return AVERROR_BUG;
525  }
526 
527  if ((ret = ff_af_queue_add(&at->afq, frame)) < 0)
528  return ret;
529 
530  in_frame = av_frame_clone(frame);
531  if (!in_frame)
532  return AVERROR(ENOMEM);
533 
534  ff_bufqueue_add(avctx, &at->frame_queue, in_frame);
535  } else {
536  at->eof = 1;
537  }
538 
539  if ((ret = ff_alloc_packet2(avctx, avpkt, at->pkt_size, 0)) < 0)
540  return ret;
541 
542 
543  out_buffers.mBuffers[0].mData = avpkt->data;
544 
545  *got_packet_ptr = avctx->frame_size / at->frame_size;
546 
547  ret = AudioConverterFillComplexBuffer(at->converter, ffat_encode_callback, avctx,
548  got_packet_ptr, &out_buffers,
549  (avctx->frame_size > at->frame_size) ? NULL : &out_pkt_desc);
550 
552 
553  if ((!ret || ret == 1) && *got_packet_ptr) {
554  avpkt->size = out_buffers.mBuffers[0].mDataByteSize;
555  ff_af_queue_remove(&at->afq, out_pkt_desc.mVariableFramesInPacket ?
556  out_pkt_desc.mVariableFramesInPacket :
557  avctx->frame_size,
558  &avpkt->pts,
559  &avpkt->duration);
560  } else if (ret && ret != 1) {
561  av_log(avctx, AV_LOG_WARNING, "Encode error: %i\n", ret);
562  }
563 
564  return 0;
565 }
566 
568 {
569  ATDecodeContext *at = avctx->priv_data;
570  AudioConverterReset(at->converter);
573 }
574 
576 {
577  ATDecodeContext *at = avctx->priv_data;
578  AudioConverterDispose(at->converter);
581  ff_af_queue_close(&at->afq);
583  return 0;
584 }
585 
586 static const AVProfile aac_profiles[] = {
587  { FF_PROFILE_AAC_LOW, "LC" },
588  { FF_PROFILE_AAC_HE, "HE-AAC" },
589  { FF_PROFILE_AAC_HE_V2, "HE-AACv2" },
590  { FF_PROFILE_AAC_LD, "LD" },
591  { FF_PROFILE_AAC_ELD, "ELD" },
592  { FF_PROFILE_UNKNOWN },
593 };
594 
595 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
596 static const AVOption options[] = {
597 #if !TARGET_OS_IPHONE
598  {"aac_at_mode", "ratecontrol mode", offsetof(ATDecodeContext, mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable, AE, "mode"},
599  {"auto", "VBR if global quality is given; CBR otherwise", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, AE, "mode"},
600  {"cbr", "constant bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX, AE, "mode"},
601  {"abr", "long-term average bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX, AE, "mode"},
602  {"cvbr", "constrained variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX, AE, "mode"},
603  {"vbr" , "variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX, AE, "mode"},
604 #endif
605  {"aac_at_quality", "quality vs speed control", offsetof(ATDecodeContext, quality), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2, AE},
606  { NULL },
607 };
608 
609 #define FFAT_ENC_CLASS(NAME) \
610  static const AVClass ffat_##NAME##_enc_class = { \
611  .class_name = "at_" #NAME "_enc", \
612  .item_name = av_default_item_name, \
613  .option = options, \
614  .version = LIBAVUTIL_VERSION_INT, \
615  };
616 
617 #define FFAT_ENC(NAME, ID, PROFILES, ...) \
618  FFAT_ENC_CLASS(NAME) \
619  AVCodec ff_##NAME##_at_encoder = { \
620  .name = #NAME "_at", \
621  .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
622  .type = AVMEDIA_TYPE_AUDIO, \
623  .id = ID, \
624  .priv_data_size = sizeof(ATDecodeContext), \
625  .init = ffat_init_encoder, \
626  .close = ffat_close_encoder, \
627  .encode2 = ffat_encode, \
628  .flush = ffat_encode_flush, \
629  .priv_class = &ffat_##NAME##_enc_class, \
630  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | \
631  AV_CODEC_CAP_ENCODER_FLUSH __VA_ARGS__, \
632  .sample_fmts = (const enum AVSampleFormat[]) { \
633  AV_SAMPLE_FMT_S16, \
634  AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
635  }, \
636  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
637  .profiles = PROFILES, \
638  .wrapper_name = "at", \
639  };
640 
641 static const uint64_t aac_at_channel_layouts[] = {
654  0,
655 };
656 
658 //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:105
read_descr
static int read_descr(GetByteContext *gb, int *tag)
Definition: audiotoolboxenc.c:128
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1206
AV_CH_LAYOUT_6POINT1
#define AV_CH_LAYOUT_6POINT1
Definition: channel_layout.h:102
get_aac_tag
static int get_aac_tag(uint64_t in_layout)
Definition: audiotoolboxenc.c:200
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AV_CH_LAYOUT_7POINT1_WIDE_BACK
#define AV_CH_LAYOUT_7POINT1_WIDE_BACK
Definition: channel_layout.h:109
AV_CODEC_ID_ADPCM_IMA_QT
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition: codec_id.h:340
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:304
get_channel_label
static av_cold int get_channel_label(int channel)
Definition: audiotoolboxenc.c:154
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:1237
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:1186
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:85
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:275
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:203
profile
mfxU16 profile
Definition: qsvenc.c:45
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
AVOption
AVOption.
Definition: opt.h:246
data
const char data[16]
Definition: mxf.c:91
AV_CODEC_ID_ALAC
@ AV_CODEC_ID_ALAC
Definition: codec_id.h:426
ffat_init_encoder
static av_cold int ffat_init_encoder(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:234
get_ilbc_mode
static int get_ilbc_mode(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:142
aac_profiles
static const AVProfile aac_profiles[]
Definition: audiotoolboxenc.c:586
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:373
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
ffat_get_format_id
static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
Definition: audiotoolboxenc.c:55
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:176
AV_CH_LAYOUT_6POINT0
#define AV_CH_LAYOUT_6POINT0
Definition: channel_layout.h:99
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
ATDecodeContext::used_frame_queue
struct FFBufQueue used_frame_queue
Definition: audiotoolboxenc.c:45
ATDecodeContext::frame_size
int frame_size
Definition: audiotoolboxenc.c:50
ffat_close_encoder
static av_cold int ffat_close_encoder(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:575
ATDecodeContext::eof
int eof
Definition: audiotoolboxdec.c:53
MP4DecSpecificDescrTag
#define MP4DecSpecificDescrTag
Definition: isom.h:305
AE
#define AE
Definition: audiotoolboxenc.c:595
audio_frame_queue.h
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:2060
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:606
ffat_encode_callback
static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets, AudioBufferList *data, AudioStreamPacketDescription **packets, void *inctx)
Definition: audiotoolboxenc.c:454
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:40
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:86
AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_QUAD
Definition: channel_layout.h:94
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:190
options
static const AVOption options[]
Definition: audiotoolboxenc.c:596
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:90
FF_PROFILE_AAC_HE_V2
#define FF_PROFILE_AAC_HE_V2
Definition: avcodec.h:1868
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:52
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:628
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:592
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
FF_PROFILE_UNKNOWN
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1860
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:1757
ATDecodeContext::encoding_frame
AVFrame * encoding_frame
Definition: audiotoolboxenc.c:52
FFAT_ENC
#define FFAT_ENC(NAME, ID, PROFILES,...)
Definition: audiotoolboxenc.c:617
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:541
AV_CODEC_ID_PCM_MULAW
@ AV_CODEC_ID_PCM_MULAW
Definition: codec_id.h:307
AVCodecContext::rc_max_rate
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1404
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:536
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:303
AV_CODEC_ID_PCM_ALAW
@ AV_CODEC_ID_PCM_ALAW
Definition: codec_id.h:308
ATDecodeContext::quality
int quality
Definition: audiotoolboxenc.c:41
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
isom.h
ffat_update_ctx
static void ffat_update_ctx(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:88
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:576
FF_PROFILE_AAC_LD
#define FF_PROFILE_AAC_LD
Definition: avcodec.h:1869
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:96
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:122
FF_PROFILE_AAC_ELD
#define FF_PROFILE_AAC_ELD
Definition: avcodec.h:1870
AV_SAMPLE_FMT_U8
AV_SAMPLE_FMT_U8
Definition: audio_convert.c:194
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:154
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:412
bufferqueue.h
AVPacket::size
int size
Definition: packet.h:356
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:444
FF_PROFILE_AAC_LOW
#define FF_PROFILE_AAC_LOW
Definition: avcodec.h:1864
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1194
size
int size
Definition: twinvq_data.h:11134
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: common.h:407
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
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:1187
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:110
AV_CH_LAYOUT_5POINT0
#define AV_CH_LAYOUT_5POINT0
Definition: channel_layout.h:95
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:348
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:627
FFBufQueue
Structure holding the queue.
Definition: bufferqueue.h:49
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:107
AV_CH_BACK_CENTER
#define AV_CH_BACK_CENTER
Definition: channel_layout.h:57
uint8_t
uint8_t
Definition: audio_convert.c:194
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:554
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:237
AV_CH_SIDE_RIGHT
#define AV_CH_SIDE_RIGHT
Definition: channel_layout.h:59
len
int len
Definition: vorbis_enc_data.h:452
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:1867
tag
uint32_t tag
Definition: movenc.c:1532
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:1223
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:89
ffat_encode_flush
static av_cold void ffat_encode_flush(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:567
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: avcodec.h:215
ATDecodeContext::converter
AudioConverterRef converter
Definition: audiotoolboxdec.c:42
AVCodecContext
main external API structure.
Definition: avcodec.h:526
ffat_encode
static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: audiotoolboxenc.c:497
mode
mode
Definition: ebur128.h:83
ATDecodeContext
Definition: audiotoolboxdec.c:39
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1859
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:225
remap_layout
static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
Definition: audiotoolboxenc.c:179
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:85
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVPacket
This structure stores compressed data.
Definition: packet.h:332
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:553
channel_layouts
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:113
ATDecodeContext::pkt_size
unsigned pkt_size
Definition: audiotoolboxenc.c:47
AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_4POINT0
Definition: channel_layout.h:91
AV_CODEC_ID_ILBC
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:469
bytestream.h
ATDecodeContext::av_class
AVClass * av_class
Definition: audiotoolboxdec.c:40
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
ATDecodeContext::afq
AudioFrameQueue afq
Definition: audiotoolboxenc.c:48
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
ff_alloc_packet2
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
aac_at_channel_layouts
static const uint64_t aac_at_channel_layouts[]
Definition: audiotoolboxenc.c:641
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:232
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:44