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 "codec_internal.h"
33 #include "encode.h"
34 #include "internal.h"
35 #include "libavformat/isom.h"
36 #include "libavutil/avassert.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/log.h"
40 
41 typedef struct ATDecodeContext {
43  int mode;
44  int quality;
45 
46  AudioConverterRef converter;
49 
50  unsigned pkt_size;
52  int eof;
54 
57 
58 static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
59 {
60  switch (codec) {
61  case AV_CODEC_ID_AAC:
62  switch (profile) {
63  case AV_PROFILE_AAC_LOW:
64  default:
65  return kAudioFormatMPEG4AAC;
66  case AV_PROFILE_AAC_HE:
67  return kAudioFormatMPEG4AAC_HE;
69  return kAudioFormatMPEG4AAC_HE_V2;
70  case AV_PROFILE_AAC_LD:
71  return kAudioFormatMPEG4AAC_LD;
72 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
73  case AV_PROFILE_AAC_ELD:
74  return kAudioFormatMPEG4AAC_ELD;
75 #endif
76  }
78  return kAudioFormatAppleIMA4;
79  case AV_CODEC_ID_ALAC:
80  return kAudioFormatAppleLossless;
81 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
82  case AV_CODEC_ID_ILBC:
83  return kAudioFormatiLBC;
84 #endif
86  return kAudioFormatALaw;
88  return kAudioFormatULaw;
89  default:
90  av_assert0(!"Invalid codec ID!");
91  return 0;
92  }
93 }
94 
95 static int ffat_update_ctx(AVCodecContext *avctx)
96 {
97  ATDecodeContext *at = avctx->priv_data;
98  UInt32 size = sizeof(unsigned);
99  AudioConverterPrimeInfo prime_info;
100  AudioStreamBasicDescription out_format;
101 
102  AudioConverterGetProperty(at->converter,
103  kAudioConverterPropertyMaximumOutputPacketSize,
104  &size, &at->pkt_size);
105 
106  if (at->pkt_size <= 0)
107  at->pkt_size = 1024 * 50;
108 
109  size = sizeof(prime_info);
110 
111  if (!AudioConverterGetProperty(at->converter,
112  kAudioConverterPrimeInfo,
113  &size, &prime_info)) {
114  avctx->initial_padding = prime_info.leadingFrames;
115  }
116 
117  size = sizeof(out_format);
118  if (!AudioConverterGetProperty(at->converter,
119  kAudioConverterCurrentOutputStreamDescription,
120  &size, &out_format)) {
121  if (out_format.mFramesPerPacket) {
122  avctx->frame_size = out_format.mFramesPerPacket;
123  } else {
124  /* The doc on mFramesPerPacket says:
125  * For formats with a variable number of frames per packet, such as
126  * Ogg Vorbis, set this field to 0.
127  * Looks like it means for decoding. There is no known case that
128  * mFramesPerPacket is zero for encoding. Use a default value for safety.
129  */
130  avctx->frame_size = 1024;
131  av_log(avctx, AV_LOG_WARNING, "Missing mFramesPerPacket\n");
132  }
133  if (out_format.mBytesPerPacket && avctx->codec_id == AV_CODEC_ID_ILBC)
134  avctx->block_align = out_format.mBytesPerPacket;
135  } else {
136  av_log(avctx, AV_LOG_ERROR, "Get OutputStreamDescription failed\n");
137  return AVERROR_EXTERNAL;
138  }
139 
140  at->frame_size = avctx->frame_size;
141  if (avctx->codec_id == AV_CODEC_ID_PCM_MULAW ||
142  avctx->codec_id == AV_CODEC_ID_PCM_ALAW) {
143  at->pkt_size *= 1024;
144  avctx->frame_size *= 1024;
145  }
146 
147  return 0;
148 }
149 
150 static int read_descr(GetByteContext *gb, int *tag)
151 {
152  int len = 0;
153  int count = 4;
154  *tag = bytestream2_get_byte(gb);
155  while (count--) {
156  int c = bytestream2_get_byte(gb);
157  len = (len << 7) | (c & 0x7f);
158  if (!(c & 0x80))
159  break;
160  }
161  return len;
162 }
163 
164 static int get_ilbc_mode(AVCodecContext *avctx)
165 {
166  if (avctx->block_align == 38)
167  return 20;
168  else if (avctx->block_align == 50)
169  return 30;
170  else if (avctx->bit_rate > 0)
171  return avctx->bit_rate <= 14000 ? 30 : 20;
172  else
173  return 30;
174 }
175 
177 {
178  uint64_t map = 1 << channel;
179  if (map <= AV_CH_LOW_FREQUENCY)
180  return channel + 1;
181  else if (map <= AV_CH_BACK_RIGHT)
182  return channel + 29;
183  else if (map <= AV_CH_BACK_CENTER)
184  return channel - 1;
185  else if (map <= AV_CH_SIDE_RIGHT)
186  return channel - 4;
187  else if (map <= AV_CH_TOP_BACK_RIGHT)
188  return channel + 1;
189  else if (map <= AV_CH_STEREO_RIGHT)
190  return -1;
191  else if (map <= AV_CH_WIDE_RIGHT)
192  return channel + 4;
193  else if (map <= AV_CH_SURROUND_DIRECT_RIGHT)
194  return channel - 23;
195  else if (map == AV_CH_LOW_FREQUENCY_2)
196  return kAudioChannelLabel_LFE2;
197  else
198  return -1;
199 }
200 
201 static int remap_layout(AudioChannelLayout *layout, const AVChannelLayout *in_layout)
202 {
203  int i;
204  layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
205  layout->mNumberChannelDescriptions = in_layout->nb_channels;
206  for (i = 0; i < in_layout->nb_channels; i++) {
207  int c, label;
208 
210  if (c < 0 || c >= 64)
211  return AVERROR(EINVAL);
212  label = get_channel_label(c);
213  layout->mChannelDescriptions[i].mChannelLabel = label;
214  if (label < 0)
215  return AVERROR(EINVAL);
216  c++;
217  }
218  return 0;
219 }
220 
221 static int get_aac_tag(const AVChannelLayout *in_layout)
222 {
223  static const struct {
224  AVChannelLayout chl;
225  int tag;
226  } map[] = {
227  { AV_CHANNEL_LAYOUT_MONO, kAudioChannelLayoutTag_Mono },
228  { AV_CHANNEL_LAYOUT_STEREO, kAudioChannelLayoutTag_Stereo },
229  { AV_CHANNEL_LAYOUT_QUAD, kAudioChannelLayoutTag_AAC_Quadraphonic },
230  { AV_CHANNEL_LAYOUT_OCTAGONAL, kAudioChannelLayoutTag_AAC_Octagonal },
231  { AV_CHANNEL_LAYOUT_SURROUND, kAudioChannelLayoutTag_AAC_3_0 },
232  { AV_CHANNEL_LAYOUT_4POINT0, kAudioChannelLayoutTag_AAC_4_0 },
233  { AV_CHANNEL_LAYOUT_5POINT0, kAudioChannelLayoutTag_AAC_5_0 },
234  { AV_CHANNEL_LAYOUT_5POINT1, kAudioChannelLayoutTag_AAC_5_1 },
235  { AV_CHANNEL_LAYOUT_6POINT0, kAudioChannelLayoutTag_AAC_6_0 },
236  { AV_CHANNEL_LAYOUT_6POINT1, kAudioChannelLayoutTag_AAC_6_1 },
237  { AV_CHANNEL_LAYOUT_7POINT0, kAudioChannelLayoutTag_AAC_7_0 },
238  { AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK, kAudioChannelLayoutTag_AAC_7_1 },
239  { AV_CHANNEL_LAYOUT_7POINT1, kAudioChannelLayoutTag_MPEG_7_1_C },
240  };
241  int i;
242 
243  for (i = 0; i < FF_ARRAY_ELEMS(map); i++)
244  if (!av_channel_layout_compare(in_layout, &map[i].chl))
245  return map[i].tag;
246 
247  return 0;
248 }
249 
251 {
252  ATDecodeContext *at = avctx->priv_data;
253  OSStatus status;
254  int ret;
255 
256  AudioStreamBasicDescription in_format = {
257  .mSampleRate = avctx->sample_rate,
258  .mFormatID = kAudioFormatLinearPCM,
259  .mFormatFlags = ((avctx->sample_fmt == AV_SAMPLE_FMT_FLT ||
260  avctx->sample_fmt == AV_SAMPLE_FMT_DBL) ? kAudioFormatFlagIsFloat
261  : avctx->sample_fmt == AV_SAMPLE_FMT_U8 ? 0
262  : kAudioFormatFlagIsSignedInteger)
263  | kAudioFormatFlagIsPacked,
264  .mBytesPerPacket = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->ch_layout.nb_channels,
265  .mFramesPerPacket = 1,
266  .mBytesPerFrame = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->ch_layout.nb_channels,
267  .mChannelsPerFrame = avctx->ch_layout.nb_channels,
268  .mBitsPerChannel = av_get_bytes_per_sample(avctx->sample_fmt) * 8,
269  };
270  AudioStreamBasicDescription out_format = {
271  .mSampleRate = avctx->sample_rate,
272  .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
273  .mChannelsPerFrame = in_format.mChannelsPerFrame,
274  };
275  UInt32 layout_size = sizeof(AudioChannelLayout) +
276  sizeof(AudioChannelDescription) * avctx->ch_layout.nb_channels;
277  AudioChannelLayout *channel_layout = av_malloc(layout_size);
278 
279  if (!channel_layout)
280  return AVERROR(ENOMEM);
281 
282  if (avctx->codec_id == AV_CODEC_ID_ILBC) {
283  int mode = get_ilbc_mode(avctx);
284  out_format.mFramesPerPacket = 8000 * mode / 1000;
285  out_format.mBytesPerPacket = (mode == 20 ? 38 : 50);
286  }
287 
288  status = AudioConverterNew(&in_format, &out_format, &at->converter);
289 
290  if (status != 0) {
291  av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
292  av_free(channel_layout);
293  return AVERROR_UNKNOWN;
294  }
295 
298 
299  if ((status = remap_layout(channel_layout, &avctx->ch_layout)) < 0) {
300  av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
301  av_free(channel_layout);
302  return status;
303  }
304 
305  if (AudioConverterSetProperty(at->converter, kAudioConverterInputChannelLayout,
306  layout_size, channel_layout)) {
307  av_log(avctx, AV_LOG_ERROR, "Unsupported input channel layout\n");
308  av_free(channel_layout);
309  return AVERROR(EINVAL);
310  }
311  if (avctx->codec_id == AV_CODEC_ID_AAC) {
312  int tag = get_aac_tag(&avctx->ch_layout);
313  if (tag) {
314  channel_layout->mChannelLayoutTag = tag;
315  channel_layout->mNumberChannelDescriptions = 0;
316  }
317  }
318  if (AudioConverterSetProperty(at->converter, kAudioConverterOutputChannelLayout,
319  layout_size, channel_layout)) {
320  av_log(avctx, AV_LOG_ERROR, "Unsupported output channel layout\n");
321  av_free(channel_layout);
322  return AVERROR(EINVAL);
323  }
324  av_free(channel_layout);
325 
326  if (avctx->bits_per_raw_sample)
327  AudioConverterSetProperty(at->converter,
328  kAudioConverterPropertyBitDepthHint,
329  sizeof(avctx->bits_per_raw_sample),
330  &avctx->bits_per_raw_sample);
331 
332 #if !TARGET_OS_IPHONE
333  if (at->mode == -1)
334  at->mode = (avctx->flags & AV_CODEC_FLAG_QSCALE) ?
335  kAudioCodecBitRateControlMode_Variable :
336  kAudioCodecBitRateControlMode_Constant;
337 
338  AudioConverterSetProperty(at->converter, kAudioCodecPropertyBitRateControlMode,
339  sizeof(at->mode), &at->mode);
340 
341  if (at->mode == kAudioCodecBitRateControlMode_Variable) {
342  int q = avctx->global_quality / FF_QP2LAMBDA;
343  if (q < 0 || q > 14) {
344  av_log(avctx, AV_LOG_WARNING,
345  "VBR quality %d out of range, should be 0-14\n", q);
346  q = av_clip(q, 0, 14);
347  }
348  q = 127 - q * 9;
349  AudioConverterSetProperty(at->converter, kAudioCodecPropertySoundQualityForVBR,
350  sizeof(q), &q);
351  } else
352 #endif
353  if (avctx->bit_rate > 0) {
354  UInt32 rate = avctx->bit_rate;
355  UInt32 size;
356  status = AudioConverterGetPropertyInfo(at->converter,
357  kAudioConverterApplicableEncodeBitRates,
358  &size, NULL);
359  if (!status && size) {
360  UInt32 new_rate = rate;
361  int count;
362  int i;
363  AudioValueRange *ranges = av_malloc(size);
364  if (!ranges)
365  return AVERROR(ENOMEM);
366  AudioConverterGetProperty(at->converter,
367  kAudioConverterApplicableEncodeBitRates,
368  &size, ranges);
369  count = size / sizeof(AudioValueRange);
370  for (i = 0; i < count; i++) {
371  AudioValueRange *range = &ranges[i];
372  if (rate >= range->mMinimum && rate <= range->mMaximum) {
373  new_rate = rate;
374  break;
375  } else if (rate > range->mMaximum) {
376  new_rate = range->mMaximum;
377  } else {
378  new_rate = range->mMinimum;
379  break;
380  }
381  }
382  if (new_rate != rate) {
383  av_log(avctx, AV_LOG_WARNING,
384  "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
385  rate = new_rate;
386  }
387  av_free(ranges);
388  }
389  AudioConverterSetProperty(at->converter, kAudioConverterEncodeBitRate,
390  sizeof(rate), &rate);
391  }
392 
393  at->quality = 96 - at->quality * 32;
394  AudioConverterSetProperty(at->converter, kAudioConverterCodecQuality,
395  sizeof(at->quality), &at->quality);
396 
397  if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterCompressionMagicCookie,
398  &avctx->extradata_size, NULL) &&
399  avctx->extradata_size) {
400  int extradata_size = avctx->extradata_size;
401  uint8_t *extradata;
403  return AVERROR(ENOMEM);
404  if (avctx->codec_id == AV_CODEC_ID_ALAC) {
405  avctx->extradata_size = 0x24;
406  AV_WB32(avctx->extradata, 0x24);
407  AV_WB32(avctx->extradata + 4, MKBETAG('a','l','a','c'));
408  extradata = avctx->extradata + 12;
409  avctx->extradata_size = 0x24;
410  } else {
411  extradata = avctx->extradata;
412  }
413  status = AudioConverterGetProperty(at->converter,
414  kAudioConverterCompressionMagicCookie,
415  &extradata_size, extradata);
416  if (status != 0) {
417  av_log(avctx, AV_LOG_ERROR, "AudioToolbox cookie error: %i\n", (int)status);
418  return AVERROR_UNKNOWN;
419  } else if (avctx->codec_id == AV_CODEC_ID_AAC) {
420  GetByteContext gb;
421  int tag, len;
422  bytestream2_init(&gb, extradata, extradata_size);
423  do {
424  len = read_descr(&gb, &tag);
425  if (tag == MP4DecConfigDescrTag) {
426  bytestream2_skip(&gb, 13);
427  len = read_descr(&gb, &tag);
428  if (tag == MP4DecSpecificDescrTag) {
429  len = FFMIN(gb.buffer_end - gb.buffer, len);
430  memmove(extradata, gb.buffer, len);
431  avctx->extradata_size = len;
432  break;
433  }
434  } else if (tag == MP4ESDescrTag) {
435  int flags;
436  bytestream2_skip(&gb, 2);
437  flags = bytestream2_get_byte(&gb);
438  if (flags & 0x80) //streamDependenceFlag
439  bytestream2_skip(&gb, 2);
440  if (flags & 0x40) //URL_Flag
441  bytestream2_skip(&gb, bytestream2_get_byte(&gb));
442  if (flags & 0x20) //OCRstreamFlag
443  bytestream2_skip(&gb, 2);
444  }
445  } while (bytestream2_get_bytes_left(&gb));
446  } else if (avctx->codec_id != AV_CODEC_ID_ALAC) {
447  avctx->extradata_size = extradata_size;
448  }
449  }
450 
451  ret = ffat_update_ctx(avctx);
452  if (ret < 0)
453  return ret;
454 
455 #if !TARGET_OS_IPHONE && defined(__MAC_10_9)
456  if (at->mode == kAudioCodecBitRateControlMode_Variable && avctx->rc_max_rate) {
457  UInt32 max_size = avctx->rc_max_rate * avctx->frame_size / avctx->sample_rate;
458  if (max_size)
459  AudioConverterSetProperty(at->converter, kAudioCodecPropertyPacketSizeLimitForVBR,
460  sizeof(max_size), &max_size);
461  }
462 #endif
463 
464  ff_af_queue_init(avctx, &at->afq);
465 
467  if (!at->encoding_frame)
468  return AVERROR(ENOMEM);
469 
470  return 0;
471 }
472 
473 static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets,
474  AudioBufferList *data,
475  AudioStreamPacketDescription **packets,
476  void *inctx)
477 {
478  AVCodecContext *avctx = inctx;
479  ATDecodeContext *at = avctx->priv_data;
480  AVFrame *frame;
481  int ret;
482 
483  if (!at->frame_queue.available) {
484  if (at->eof) {
485  *nb_packets = 0;
486  return 0;
487  } else {
488  *nb_packets = 0;
489  return 1;
490  }
491  }
492 
494 
495  data->mNumberBuffers = 1;
496  data->mBuffers[0].mNumberChannels = avctx->ch_layout.nb_channels;
497  data->mBuffers[0].mDataByteSize = frame->nb_samples *
499  avctx->ch_layout.nb_channels;
500  data->mBuffers[0].mData = frame->data[0];
501  if (*nb_packets > frame->nb_samples)
502  *nb_packets = frame->nb_samples;
503 
505  if (ret < 0) {
506  *nb_packets = 0;
507  return ret;
508  }
509 
510  ff_bufqueue_add(avctx, &at->used_frame_queue, frame);
511 
512  return 0;
513 }
514 
515 static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt,
516  const AVFrame *frame, int *got_packet_ptr)
517 {
518  ATDecodeContext *at = avctx->priv_data;
519  OSStatus ret;
520 
521  AudioBufferList out_buffers = {
522  .mNumberBuffers = 1,
523  .mBuffers = {
524  {
525  .mNumberChannels = avctx->ch_layout.nb_channels,
526  .mDataByteSize = at->pkt_size,
527  }
528  }
529  };
530  AudioStreamPacketDescription out_pkt_desc = {0};
531 
532  if (frame) {
533  AVFrame *in_frame;
534 
535  if (ff_bufqueue_is_full(&at->frame_queue)) {
536  /*
537  * The frame queue is significantly larger than needed in practice,
538  * but no clear way to determine the minimum number of samples to
539  * get output from AudioConverterFillComplexBuffer().
540  */
541  av_log(avctx, AV_LOG_ERROR, "Bug: frame queue is too small.\n");
542  return AVERROR_BUG;
543  }
544 
545  if ((ret = ff_af_queue_add(&at->afq, frame)) < 0)
546  return ret;
547 
548  in_frame = av_frame_clone(frame);
549  if (!in_frame)
550  return AVERROR(ENOMEM);
551 
552  ff_bufqueue_add(avctx, &at->frame_queue, in_frame);
553  } else {
554  at->eof = 1;
555  }
556 
557  if ((ret = ff_alloc_packet(avctx, avpkt, at->pkt_size)) < 0)
558  return ret;
559 
560 
561  out_buffers.mBuffers[0].mData = avpkt->data;
562 
563  *got_packet_ptr = avctx->frame_size / at->frame_size;
564 
565  ret = AudioConverterFillComplexBuffer(at->converter, ffat_encode_callback, avctx,
566  got_packet_ptr, &out_buffers,
567  (avctx->frame_size > at->frame_size) ? NULL : &out_pkt_desc);
568 
570 
571  if ((!ret || ret == 1) && *got_packet_ptr) {
572  avpkt->size = out_buffers.mBuffers[0].mDataByteSize;
573  ff_af_queue_remove(&at->afq, out_pkt_desc.mVariableFramesInPacket ?
574  out_pkt_desc.mVariableFramesInPacket :
575  avctx->frame_size,
576  &avpkt->pts,
577  &avpkt->duration);
578  } else if (ret && ret != 1) {
579  av_log(avctx, AV_LOG_ERROR, "Encode error: %i\n", ret);
580  return AVERROR_EXTERNAL;
581  }
582 
583  return 0;
584 }
585 
587 {
588  ATDecodeContext *at = avctx->priv_data;
589  AudioConverterReset(at->converter);
592 }
593 
595 {
596  ATDecodeContext *at = avctx->priv_data;
597  AudioConverterDispose(at->converter);
600  ff_af_queue_close(&at->afq);
602  return 0;
603 }
604 
605 static const AVProfile aac_profiles[] = {
606  { AV_PROFILE_AAC_LOW, "LC" },
607  { AV_PROFILE_AAC_HE, "HE-AAC" },
608  { AV_PROFILE_AAC_HE_V2, "HE-AACv2" },
609  { AV_PROFILE_AAC_LD, "LD" },
610  { AV_PROFILE_AAC_ELD, "ELD" },
611  { AV_PROFILE_UNKNOWN },
612 };
613 
614 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
615 static const AVOption options[] = {
616 #if !TARGET_OS_IPHONE
617  {"aac_at_mode", "ratecontrol mode", offsetof(ATDecodeContext, mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable, AE, .unit = "mode"},
618  {"auto", "VBR if global quality is given; CBR otherwise", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, AE, .unit = "mode"},
619  {"cbr", "constant bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX, AE, .unit = "mode"},
620  {"abr", "long-term average bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX, AE, .unit = "mode"},
621  {"cvbr", "constrained variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX, AE, .unit = "mode"},
622  {"vbr" , "variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX, AE, .unit = "mode"},
623 #endif
624  {"aac_at_quality", "quality vs speed control", offsetof(ATDecodeContext, quality), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2, AE},
625  { NULL },
626 };
627 
628 #define FFAT_ENC_CLASS(NAME) \
629  static const AVClass ffat_##NAME##_enc_class = { \
630  .class_name = "at_" #NAME "_enc", \
631  .item_name = av_default_item_name, \
632  .option = options, \
633  .version = LIBAVUTIL_VERSION_INT, \
634  };
635 
636 #define FFAT_ENC(NAME, ID, PROFILES, CAPS, CHANNEL_LAYOUTS, CH_LAYOUTS) \
637  FFAT_ENC_CLASS(NAME) \
638  const FFCodec ff_##NAME##_at_encoder = { \
639  .p.name = #NAME "_at", \
640  CODEC_LONG_NAME(#NAME " (AudioToolbox)"), \
641  .p.type = AVMEDIA_TYPE_AUDIO, \
642  .p.id = ID, \
643  .priv_data_size = sizeof(ATDecodeContext), \
644  .init = ffat_init_encoder, \
645  .close = ffat_close_encoder, \
646  FF_CODEC_ENCODE_CB(ffat_encode), \
647  .flush = ffat_encode_flush, \
648  .p.priv_class = &ffat_##NAME##_enc_class, \
649  .p.capabilities = AV_CODEC_CAP_DELAY | \
650  AV_CODEC_CAP_ENCODER_FLUSH CAPS, \
651  .p.ch_layouts = CH_LAYOUTS, \
652  .p.sample_fmts = (const enum AVSampleFormat[]) { \
653  AV_SAMPLE_FMT_S16, \
654  AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
655  }, \
656  .p.profiles = PROFILES, \
657  .p.wrapper_name = "at", \
658  };
659 
673  { 0 },
674 };
675 
676 FFAT_ENC(aac, AV_CODEC_ID_AAC, aac_profiles, , aac_at_channel_layouts, aac_at_ch_layouts)
677 //FFAT_ENC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT, NULL)
read_descr
static int read_descr(GetByteContext *gb, int *tag)
Definition: audiotoolboxenc.c:150
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1077
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AV_CODEC_ID_ADPCM_IMA_QT
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition: codec_id.h:367
MP4DecConfigDescrTag
#define MP4DecConfigDescrTag
Definition: isom.h:369
FFAT_ENC
#define FFAT_ENC(NAME, ID, PROFILES, CAPS, CHANNEL_LAYOUTS, CH_LAYOUTS)
Definition: audiotoolboxenc.c:636
av_clip
#define av_clip
Definition: common.h:98
AV_CHANNEL_LAYOUT_OCTAGONAL
#define AV_CHANNEL_LAYOUT_OCTAGONAL
Definition: channel_layout.h:405
get_channel_label
static av_cold int get_channel_label(int channel)
Definition: audiotoolboxenc.c:176
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
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
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:379
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
GetByteContext
Definition: bytestream.h:33
AV_CH_LOW_FREQUENCY_2
#define AV_CH_LOW_FREQUENCY_2
Definition: channel_layout.h:192
ff_af_queue_close
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
Definition: audio_frame_queue.c:36
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:224
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:130
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
av_channel_layout_channel_from_index
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
Definition: channel_layout.c:664
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVOption
AVOption.
Definition: opt.h:346
encode.h
data
const char data[16]
Definition: mxf.c:148
AV_CODEC_ID_ALAC
@ AV_CODEC_ID_ALAC
Definition: codec_id.h:456
ffat_init_encoder
static av_cold int ffat_init_encoder(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:250
get_ilbc_mode
static int get_ilbc_mode(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:164
aac_profiles
static const AVProfile aac_profiles[]
Definition: audiotoolboxenc.c:605
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:308
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:58
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
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
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
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:191
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AV_CH_WIDE_RIGHT
#define AV_CH_WIDE_RIGHT
Definition: channel_layout.h:189
AVProfile
AVProfile.
Definition: codec.h:179
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:48
ATDecodeContext::frame_size
int frame_size
Definition: audiotoolboxenc.c:53
ffat_close_encoder
static av_cold int ffat_close_encoder(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:594
ATDecodeContext::eof
int eof
Definition: audiotoolboxdec.c:56
MP4DecSpecificDescrTag
#define MP4DecSpecificDescrTag
Definition: isom.h:370
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
AE
#define AE
Definition: audiotoolboxenc.c:614
remap_layout
static int remap_layout(AudioChannelLayout *layout, const AVChannelLayout *in_layout)
Definition: audiotoolboxenc.c:201
audio_frame_queue.h
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:1122
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:502
ffat_encode_callback
static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets, AudioBufferList *data, AudioStreamPacketDescription **packets, void *inctx)
Definition: audiotoolboxenc.c:473
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:43
ffat_update_ctx
static int ffat_update_ctx(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:95
AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_SURROUND
Definition: channel_layout.h:382
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:118
options
static const AVOption options[]
Definition: audiotoolboxenc.c:615
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
AV_PROFILE_UNKNOWN
#define AV_PROFILE_UNKNOWN
Definition: defs.h:65
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:171
AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_4POINT0
Definition: channel_layout.h:384
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
AV_CHANNEL_LAYOUT_7POINT1
#define AV_CHANNEL_LAYOUT_7POINT1
Definition: channel_layout.h:401
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1239
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:112
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
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:1574
ATDecodeContext::encoding_frame
AVFrame * encoding_frame
Definition: audiotoolboxenc.c:55
AV_PROFILE_AAC_ELD
#define AV_PROFILE_AAC_ELD
Definition: defs.h:75
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:563
AV_CODEC_ID_PCM_MULAW
@ AV_CODEC_ID_PCM_MULAW
Definition: codec_id.h:334
AVCodecContext::rc_max_rate
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1292
frame
static AVFrame * frame
Definition: demux_decode.c:54
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:455
AV_CH_STEREO_RIGHT
#define AV_CH_STEREO_RIGHT
Definition: channel_layout.h:187
AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK
#define AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK
Definition: channel_layout.h:403
MP4ESDescrTag
#define MP4ESDescrTag
Definition: isom.h:368
AV_CODEC_ID_PCM_ALAW
@ AV_CODEC_ID_PCM_ALAW
Definition: codec_id.h:335
ATDecodeContext::quality
int quality
Definition: audiotoolboxenc.c:44
get_aac_tag
static int get_aac_tag(const AVChannelLayout *in_layout)
Definition: audiotoolboxenc.c:221
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
isom.h
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:495
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
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:49
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:417
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:442
bufferqueue.h
AVPacket::size
int size
Definition: packet.h:523
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
codec_internal.h
AV_PROFILE_AAC_LD
#define AV_PROFILE_AAC_LD
Definition: defs.h:74
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1057
size
int size
Definition: twinvq_data.h:10344
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2557
AV_CHANNEL_LAYOUT_6POINT0
#define AV_CHANNEL_LAYOUT_6POINT0
Definition: channel_layout.h:392
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
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:185
av_channel_layout_compare
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
Definition: channel_layout.c:800
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:830
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
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:424
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:108
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
FFBufQueue
Structure holding the queue.
Definition: bufferqueue.h:49
AV_CHANNEL_LAYOUT_QUAD
#define AV_CHANNEL_LAYOUT_QUAD
Definition: channel_layout.h:387
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:57
FFBufQueue::available
unsigned short available
number of available buffers
Definition: bufferqueue.h:52
AV_CH_BACK_CENTER
#define AV_CH_BACK_CENTER
Definition: channel_layout.h:176
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:254
AV_CH_SIDE_RIGHT
#define AV_CH_SIDE_RIGHT
Definition: channel_layout.h:178
len
int len
Definition: vorbis_enc_data.h:426
profile
int profile
Definition: mxfenc.c:2226
GetByteContext::buffer_end
const uint8_t * buffer_end
Definition: bytestream.h:34
avcodec.h
tag
uint32_t tag
Definition: movenc.c:1791
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:1083
ffat_encode_flush
static av_cold void ffat_encode_flush(AVCodecContext *avctx)
Definition: audiotoolboxenc.c:586
AV_CHANNEL_LAYOUT_7POINT0
#define AV_CHANNEL_LAYOUT_7POINT0
Definition: channel_layout.h:399
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AV_PROFILE_AAC_LOW
#define AV_PROFILE_AAC_LOW
Definition: defs.h:69
av_frame_replace
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition: frame.c:453
ATDecodeContext::converter
AudioConverterRef converter
Definition: audiotoolboxdec.c:45
AVCodecContext
main external API structure.
Definition: avcodec.h:445
status
ov_status_e status
Definition: dnn_backend_openvino.c:120
channel_layout.h
AV_PROFILE_AAC_HE_V2
#define AV_PROFILE_AAC_HE_V2
Definition: defs.h:73
ffat_encode
static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: audiotoolboxenc.c:515
mode
mode
Definition: ebur128.h:83
ATDecodeContext
Definition: audiotoolboxdec.c:42
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1639
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:378
AV_PROFILE_AAC_HE
#define AV_PROFILE_AAC_HE
Definition: defs.h:72
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
aac_at_ch_layouts
static const AVChannelLayout aac_at_ch_layouts[]
Definition: audiotoolboxenc.c:660
ATDecodeContext::pkt_size
unsigned pkt_size
Definition: audiotoolboxenc.c:50
AV_CODEC_ID_ILBC
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:499
bytestream.h
ATDecodeContext::av_class
AVClass * av_class
Definition: audiotoolboxdec.c:43
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:482
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
ATDecodeContext::afq
AudioFrameQueue afq
Definition: audiotoolboxenc.c:51
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_CHANNEL_LAYOUT_6POINT1
#define AV_CHANNEL_LAYOUT_6POINT1
Definition: channel_layout.h:396
AV_CH_BACK_RIGHT
#define AV_CH_BACK_RIGHT
Definition: channel_layout.h:173
AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_5POINT0
Definition: channel_layout.h:388
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:61
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_5POINT1
Definition: channel_layout.h:389
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:244
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:61
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60
channel
channel
Definition: ebur128.h:39
ATDecodeContext::frame_queue
struct FFBufQueue frame_queue
Definition: audiotoolboxenc.c:47