FFmpeg  4.4.9
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 "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;
44  struct FFBufQueue frame_queue;
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 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
70  case FF_PROFILE_AAC_ELD:
71  return kAudioFormatMPEG4AAC_ELD;
72 #endif
73  }
75  return kAudioFormatAppleIMA4;
76  case AV_CODEC_ID_ALAC:
77  return kAudioFormatAppleLossless;
78 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
79  case AV_CODEC_ID_ILBC:
80  return kAudioFormatiLBC;
81 #endif
83  return kAudioFormatALaw;
85  return kAudioFormatULaw;
86  default:
87  av_assert0(!"Invalid codec ID!");
88  return 0;
89  }
90 }
91 
92 static int ffat_update_ctx(AVCodecContext *avctx)
93 {
94  ATDecodeContext *at = avctx->priv_data;
95  UInt32 size = sizeof(unsigned);
96  AudioConverterPrimeInfo prime_info;
97  AudioStreamBasicDescription out_format;
98 
99  AudioConverterGetProperty(at->converter,
100  kAudioConverterPropertyMaximumOutputPacketSize,
101  &size, &at->pkt_size);
102 
103  if (at->pkt_size <= 0)
104  at->pkt_size = 1024 * 50;
105 
106  size = sizeof(prime_info);
107 
108  if (!AudioConverterGetProperty(at->converter,
109  kAudioConverterPrimeInfo,
110  &size, &prime_info)) {
111  avctx->initial_padding = prime_info.leadingFrames;
112  }
113 
114  size = sizeof(out_format);
115  if (!AudioConverterGetProperty(at->converter,
116  kAudioConverterCurrentOutputStreamDescription,
117  &size, &out_format)) {
118  if (out_format.mFramesPerPacket) {
119  avctx->frame_size = out_format.mFramesPerPacket;
120  } else {
121  /* The doc on mFramesPerPacket says:
122  * For formats with a variable number of frames per packet, such as
123  * Ogg Vorbis, set this field to 0.
124  * Looks like it means for decoding. There is no known case that
125  * mFramesPerPacket is zero for encoding. Use a default value for safety.
126  */
127  avctx->frame_size = 1024;
128  av_log(avctx, AV_LOG_WARNING, "Missing mFramesPerPacket\n");
129  }
130  if (out_format.mBytesPerPacket && avctx->codec_id == AV_CODEC_ID_ILBC)
131  avctx->block_align = out_format.mBytesPerPacket;
132  } else {
133  av_log(avctx, AV_LOG_ERROR, "Get OutputStreamDescription failed\n");
134  return AVERROR_EXTERNAL;
135  }
136 
137  at->frame_size = avctx->frame_size;
138  if (avctx->codec_id == AV_CODEC_ID_PCM_MULAW ||
139  avctx->codec_id == AV_CODEC_ID_PCM_ALAW) {
140  at->pkt_size *= 1024;
141  avctx->frame_size *= 1024;
142  }
143 
144  return 0;
145 }
146 
147 static int read_descr(GetByteContext *gb, int *tag)
148 {
149  int len = 0;
150  int count = 4;
151  *tag = bytestream2_get_byte(gb);
152  while (count--) {
153  int c = bytestream2_get_byte(gb);
154  len = (len << 7) | (c & 0x7f);
155  if (!(c & 0x80))
156  break;
157  }
158  return len;
159 }
160 
161 static int get_ilbc_mode(AVCodecContext *avctx)
162 {
163  if (avctx->block_align == 38)
164  return 20;
165  else if (avctx->block_align == 50)
166  return 30;
167  else if (avctx->bit_rate > 0)
168  return avctx->bit_rate <= 14000 ? 30 : 20;
169  else
170  return 30;
171 }
172 
174 {
175  uint64_t map = 1ULL << channel;
176  if (map <= AV_CH_LOW_FREQUENCY)
177  return channel + 1;
178  else if (map <= AV_CH_BACK_RIGHT)
179  return channel + 29;
180  else if (map <= AV_CH_BACK_CENTER)
181  return channel - 1;
182  else if (map <= AV_CH_SIDE_RIGHT)
183  return channel - 4;
184  else if (map <= AV_CH_TOP_BACK_RIGHT)
185  return channel + 1;
186  else if (map <= AV_CH_STEREO_RIGHT)
187  return -1;
188  else if (map <= AV_CH_WIDE_RIGHT)
189  return channel + 4;
190  else if (map <= AV_CH_SURROUND_DIRECT_RIGHT)
191  return channel - 23;
192  else if (map == AV_CH_LOW_FREQUENCY_2)
193  return kAudioChannelLabel_LFE2;
194  else
195  return -1;
196 }
197 
198 static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
199 {
200  int i;
201  int c = 0;
202  layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
203  layout->mNumberChannelDescriptions = count;
204  for (i = 0; i < count; i++) {
205  int label;
206  while (!(in_layout & (1 << c)) && c < 64)
207  c++;
208  if (c == 64)
209  return AVERROR(EINVAL); // This should never happen
210  label = get_channel_label(c);
211  layout->mChannelDescriptions[i].mChannelLabel = label;
212  if (label < 0)
213  return AVERROR(EINVAL);
214  c++;
215  }
216  return 0;
217 }
218 
219 static int get_aac_tag(uint64_t in_layout)
220 {
221  switch (in_layout) {
222  case AV_CH_LAYOUT_MONO:
223  return kAudioChannelLayoutTag_Mono;
224  case AV_CH_LAYOUT_STEREO:
225  return kAudioChannelLayoutTag_Stereo;
226  case AV_CH_LAYOUT_QUAD:
227  return kAudioChannelLayoutTag_AAC_Quadraphonic;
229  return kAudioChannelLayoutTag_AAC_Octagonal;
231  return kAudioChannelLayoutTag_AAC_3_0;
233  return kAudioChannelLayoutTag_AAC_4_0;
235  return kAudioChannelLayoutTag_AAC_5_0;
237  return kAudioChannelLayoutTag_AAC_5_1;
239  return kAudioChannelLayoutTag_AAC_6_0;
241  return kAudioChannelLayoutTag_AAC_6_1;
243  return kAudioChannelLayoutTag_AAC_7_0;
245  return kAudioChannelLayoutTag_AAC_7_1;
247  return kAudioChannelLayoutTag_MPEG_7_1_C;
248  default:
249  return 0;
250  }
251 }
252 
254 {
255  ATDecodeContext *at = avctx->priv_data;
256  OSStatus status;
257  int ret;
258 
259  AudioStreamBasicDescription in_format = {
260  .mSampleRate = avctx->sample_rate,
261  .mFormatID = kAudioFormatLinearPCM,
262  .mFormatFlags = ((avctx->sample_fmt == AV_SAMPLE_FMT_FLT ||
263  avctx->sample_fmt == AV_SAMPLE_FMT_DBL) ? kAudioFormatFlagIsFloat
264  : avctx->sample_fmt == AV_SAMPLE_FMT_U8 ? 0
265  : kAudioFormatFlagIsSignedInteger)
266  | kAudioFormatFlagIsPacked,
267  .mBytesPerPacket = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels,
268  .mFramesPerPacket = 1,
269  .mBytesPerFrame = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels,
270  .mChannelsPerFrame = avctx->channels,
271  .mBitsPerChannel = av_get_bytes_per_sample(avctx->sample_fmt) * 8,
272  };
273  AudioStreamBasicDescription out_format = {
274  .mSampleRate = avctx->sample_rate,
275  .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
276  .mChannelsPerFrame = in_format.mChannelsPerFrame,
277  };
278  UInt32 layout_size = sizeof(AudioChannelLayout) +
279  sizeof(AudioChannelDescription) * avctx->channels;
280  AudioChannelLayout *channel_layout = av_malloc(layout_size);
281 
282  if (!channel_layout)
283  return AVERROR(ENOMEM);
284 
285  if (avctx->codec_id == AV_CODEC_ID_ILBC) {
286  int mode = get_ilbc_mode(avctx);
287  out_format.mFramesPerPacket = 8000 * mode / 1000;
288  out_format.mBytesPerPacket = (mode == 20 ? 38 : 50);
289  }
290 
291  status = AudioConverterNew(&in_format, &out_format, &at->converter);
292 
293  if (status != 0) {
294  av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
295  av_free(channel_layout);
296  return AVERROR_UNKNOWN;
297  }
298 
299  if (!avctx->channel_layout)
301 
302  if ((status = remap_layout(channel_layout, avctx->channel_layout, avctx->channels)) < 0) {
303  av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
304  av_free(channel_layout);
305  return status;
306  }
307 
308  if (AudioConverterSetProperty(at->converter, kAudioConverterInputChannelLayout,
309  layout_size, channel_layout)) {
310  av_log(avctx, AV_LOG_ERROR, "Unsupported input channel layout\n");
311  av_free(channel_layout);
312  return AVERROR(EINVAL);
313  }
314  if (avctx->codec_id == AV_CODEC_ID_AAC) {
315  int tag = get_aac_tag(avctx->channel_layout);
316  if (tag) {
317  channel_layout->mChannelLayoutTag = tag;
318  channel_layout->mNumberChannelDescriptions = 0;
319  }
320  }
321  if (AudioConverterSetProperty(at->converter, kAudioConverterOutputChannelLayout,
322  layout_size, channel_layout)) {
323  av_log(avctx, AV_LOG_ERROR, "Unsupported output channel layout\n");
324  av_free(channel_layout);
325  return AVERROR(EINVAL);
326  }
327  av_free(channel_layout);
328 
329  if (avctx->bits_per_raw_sample)
330  AudioConverterSetProperty(at->converter,
331  kAudioConverterPropertyBitDepthHint,
332  sizeof(avctx->bits_per_raw_sample),
333  &avctx->bits_per_raw_sample);
334 
335 #if !TARGET_OS_IPHONE
336  if (at->mode == -1)
337  at->mode = (avctx->flags & AV_CODEC_FLAG_QSCALE) ?
338  kAudioCodecBitRateControlMode_Variable :
339  kAudioCodecBitRateControlMode_Constant;
340 
341  AudioConverterSetProperty(at->converter, kAudioCodecPropertyBitRateControlMode,
342  sizeof(at->mode), &at->mode);
343 
344  if (at->mode == kAudioCodecBitRateControlMode_Variable) {
345  int q = avctx->global_quality / FF_QP2LAMBDA;
346  if (q < 0 || q > 14) {
347  av_log(avctx, AV_LOG_WARNING,
348  "VBR quality %d out of range, should be 0-14\n", q);
349  q = av_clip(q, 0, 14);
350  }
351  q = 127 - q * 9;
352  AudioConverterSetProperty(at->converter, kAudioCodecPropertySoundQualityForVBR,
353  sizeof(q), &q);
354  } else
355 #endif
356  if (avctx->bit_rate > 0) {
357  UInt32 rate = avctx->bit_rate;
358  UInt32 size;
359  status = AudioConverterGetPropertyInfo(at->converter,
360  kAudioConverterApplicableEncodeBitRates,
361  &size, NULL);
362  if (!status && size) {
363  UInt32 new_rate = rate;
364  int count;
365  int i;
366  AudioValueRange *ranges = av_malloc(size);
367  if (!ranges)
368  return AVERROR(ENOMEM);
369  AudioConverterGetProperty(at->converter,
370  kAudioConverterApplicableEncodeBitRates,
371  &size, ranges);
372  count = size / sizeof(AudioValueRange);
373  for (i = 0; i < count; i++) {
374  AudioValueRange *range = &ranges[i];
375  if (rate >= range->mMinimum && rate <= range->mMaximum) {
376  new_rate = rate;
377  break;
378  } else if (rate > range->mMaximum) {
379  new_rate = range->mMaximum;
380  } else {
381  new_rate = range->mMinimum;
382  break;
383  }
384  }
385  if (new_rate != rate) {
386  av_log(avctx, AV_LOG_WARNING,
387  "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
388  rate = new_rate;
389  }
390  av_free(ranges);
391  }
392  AudioConverterSetProperty(at->converter, kAudioConverterEncodeBitRate,
393  sizeof(rate), &rate);
394  }
395 
396  at->quality = 96 - at->quality * 32;
397  AudioConverterSetProperty(at->converter, kAudioConverterCodecQuality,
398  sizeof(at->quality), &at->quality);
399 
400  if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterCompressionMagicCookie,
401  &avctx->extradata_size, NULL) &&
402  avctx->extradata_size) {
403  int extradata_size = avctx->extradata_size;
404  uint8_t *extradata;
406  return AVERROR(ENOMEM);
407  if (avctx->codec_id == AV_CODEC_ID_ALAC) {
408  avctx->extradata_size = 0x24;
409  AV_WB32(avctx->extradata, 0x24);
410  AV_WB32(avctx->extradata + 4, MKBETAG('a','l','a','c'));
411  extradata = avctx->extradata + 12;
412  avctx->extradata_size = 0x24;
413  } else {
414  extradata = avctx->extradata;
415  }
416  status = AudioConverterGetProperty(at->converter,
417  kAudioConverterCompressionMagicCookie,
418  &extradata_size, extradata);
419  if (status != 0) {
420  av_log(avctx, AV_LOG_ERROR, "AudioToolbox cookie error: %i\n", (int)status);
421  return AVERROR_UNKNOWN;
422  } else if (avctx->codec_id == AV_CODEC_ID_AAC) {
423  GetByteContext gb;
424  int tag, len;
425  bytestream2_init(&gb, extradata, extradata_size);
426  do {
427  len = read_descr(&gb, &tag);
428  if (tag == MP4DecConfigDescrTag) {
429  bytestream2_skip(&gb, 13);
430  len = read_descr(&gb, &tag);
431  if (tag == MP4DecSpecificDescrTag) {
432  len = FFMIN(gb.buffer_end - gb.buffer, len);
433  memmove(extradata, gb.buffer, len);
434  avctx->extradata_size = len;
435  break;
436  }
437  } else if (tag == MP4ESDescrTag) {
438  int flags;
439  bytestream2_skip(&gb, 2);
440  flags = bytestream2_get_byte(&gb);
441  if (flags & 0x80) //streamDependenceFlag
442  bytestream2_skip(&gb, 2);
443  if (flags & 0x40) //URL_Flag
444  bytestream2_skip(&gb, bytestream2_get_byte(&gb));
445  if (flags & 0x20) //OCRstreamFlag
446  bytestream2_skip(&gb, 2);
447  }
448  } while (bytestream2_get_bytes_left(&gb));
449  } else if (avctx->codec_id != AV_CODEC_ID_ALAC) {
450  avctx->extradata_size = extradata_size;
451  }
452  }
453 
454  ret = ffat_update_ctx(avctx);
455  if (ret < 0)
456  return ret;
457 
458 #if !TARGET_OS_IPHONE && defined(__MAC_10_9)
459  if (at->mode == kAudioCodecBitRateControlMode_Variable && avctx->rc_max_rate) {
460  UInt32 max_size = avctx->rc_max_rate * avctx->frame_size / avctx->sample_rate;
461  if (max_size)
462  AudioConverterSetProperty(at->converter, kAudioCodecPropertyPacketSizeLimitForVBR,
463  sizeof(max_size), &max_size);
464  }
465 #endif
466 
467  ff_af_queue_init(avctx, &at->afq);
468 
470  if (!at->encoding_frame)
471  return AVERROR(ENOMEM);
472 
473  return 0;
474 }
475 
476 static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets,
477  AudioBufferList *data,
478  AudioStreamPacketDescription **packets,
479  void *inctx)
480 {
481  AVCodecContext *avctx = inctx;
482  ATDecodeContext *at = avctx->priv_data;
483  AVFrame *frame;
484  int ret;
485 
486  if (!at->frame_queue.available) {
487  if (at->eof) {
488  *nb_packets = 0;
489  return 0;
490  } else {
491  *nb_packets = 0;
492  return 1;
493  }
494  }
495 
497 
498  data->mNumberBuffers = 1;
499  data->mBuffers[0].mNumberChannels = avctx->channels;
500  data->mBuffers[0].mDataByteSize = frame->nb_samples *
502  avctx->channels;
503  data->mBuffers[0].mData = frame->data[0];
504  if (*nb_packets > frame->nb_samples)
505  *nb_packets = frame->nb_samples;
506 
508  ret = av_frame_ref(at->encoding_frame, frame);
509  if (ret < 0) {
510  *nb_packets = 0;
511  return ret;
512  }
513 
514  ff_bufqueue_add(avctx, &at->used_frame_queue, frame);
515 
516  return 0;
517 }
518 
519 static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt,
520  const AVFrame *frame, int *got_packet_ptr)
521 {
522  ATDecodeContext *at = avctx->priv_data;
523  OSStatus ret;
524 
525  AudioBufferList out_buffers = {
526  .mNumberBuffers = 1,
527  .mBuffers = {
528  {
529  .mNumberChannels = avctx->channels,
530  .mDataByteSize = at->pkt_size,
531  }
532  }
533  };
534  AudioStreamPacketDescription out_pkt_desc = {0};
535 
536  if (frame) {
537  AVFrame *in_frame;
538 
539  if (ff_bufqueue_is_full(&at->frame_queue)) {
540  /*
541  * The frame queue is significantly larger than needed in practice,
542  * but no clear way to determine the minimum number of samples to
543  * get output from AudioConverterFillComplexBuffer().
544  */
545  av_log(avctx, AV_LOG_ERROR, "Bug: frame queue is too small.\n");
546  return AVERROR_BUG;
547  }
548 
549  if ((ret = ff_af_queue_add(&at->afq, frame)) < 0)
550  return ret;
551 
552  in_frame = av_frame_clone(frame);
553  if (!in_frame)
554  return AVERROR(ENOMEM);
555 
556  ff_bufqueue_add(avctx, &at->frame_queue, in_frame);
557  } else {
558  at->eof = 1;
559  }
560 
561  if ((ret = ff_alloc_packet2(avctx, avpkt, at->pkt_size, 0)) < 0)
562  return ret;
563 
564 
565  out_buffers.mBuffers[0].mData = avpkt->data;
566 
567  *got_packet_ptr = avctx->frame_size / at->frame_size;
568 
569  ret = AudioConverterFillComplexBuffer(at->converter, ffat_encode_callback, avctx,
570  got_packet_ptr, &out_buffers,
571  (avctx->frame_size > at->frame_size) ? NULL : &out_pkt_desc);
572 
574 
575  if ((!ret || ret == 1) && *got_packet_ptr) {
576  avpkt->size = out_buffers.mBuffers[0].mDataByteSize;
577  ff_af_queue_remove(&at->afq, out_pkt_desc.mVariableFramesInPacket ?
578  out_pkt_desc.mVariableFramesInPacket :
579  avctx->frame_size,
580  &avpkt->pts,
581  &avpkt->duration);
582  } else if (ret && ret != 1) {
583  av_log(avctx, AV_LOG_WARNING, "Encode error: %i\n", ret);
584  }
585 
586  return 0;
587 }
588 
590 {
591  ATDecodeContext *at = avctx->priv_data;
592  AudioConverterReset(at->converter);
595 }
596 
598 {
599  ATDecodeContext *at = avctx->priv_data;
600  AudioConverterDispose(at->converter);
603  ff_af_queue_close(&at->afq);
605  return 0;
606 }
607 
608 static const AVProfile aac_profiles[] = {
609  { FF_PROFILE_AAC_LOW, "LC" },
610  { FF_PROFILE_AAC_HE, "HE-AAC" },
611  { FF_PROFILE_AAC_HE_V2, "HE-AACv2" },
612  { FF_PROFILE_AAC_LD, "LD" },
613  { FF_PROFILE_AAC_ELD, "ELD" },
614  { FF_PROFILE_UNKNOWN },
615 };
616 
617 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
618 static const AVOption options[] = {
619 #if !TARGET_OS_IPHONE
620  {"aac_at_mode", "ratecontrol mode", offsetof(ATDecodeContext, mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable, AE, "mode"},
621  {"auto", "VBR if global quality is given; CBR otherwise", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, AE, "mode"},
622  {"cbr", "constant bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX, AE, "mode"},
623  {"abr", "long-term average bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX, AE, "mode"},
624  {"cvbr", "constrained variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX, AE, "mode"},
625  {"vbr" , "variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX, AE, "mode"},
626 #endif
627  {"aac_at_quality", "quality vs speed control", offsetof(ATDecodeContext, quality), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2, AE},
628  { NULL },
629 };
630 
631 #define FFAT_ENC_CLASS(NAME) \
632  static const AVClass ffat_##NAME##_enc_class = { \
633  .class_name = "at_" #NAME "_enc", \
634  .item_name = av_default_item_name, \
635  .option = options, \
636  .version = LIBAVUTIL_VERSION_INT, \
637  };
638 
639 #define FFAT_ENC(NAME, ID, PROFILES, ...) \
640  FFAT_ENC_CLASS(NAME) \
641  AVCodec ff_##NAME##_at_encoder = { \
642  .name = #NAME "_at", \
643  .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
644  .type = AVMEDIA_TYPE_AUDIO, \
645  .id = ID, \
646  .priv_data_size = sizeof(ATDecodeContext), \
647  .init = ffat_init_encoder, \
648  .close = ffat_close_encoder, \
649  .encode2 = ffat_encode, \
650  .flush = ffat_encode_flush, \
651  .priv_class = &ffat_##NAME##_enc_class, \
652  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | \
653  AV_CODEC_CAP_ENCODER_FLUSH __VA_ARGS__, \
654  .sample_fmts = (const enum AVSampleFormat[]) { \
655  AV_SAMPLE_FMT_S16, \
656  AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
657  }, \
658  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
659  .profiles = PROFILES, \
660  .wrapper_name = "at", \
661  };
662 
663 static const uint64_t aac_at_channel_layouts[] = {
676  0,
677 };
678 
680 //FFAT_ENC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT, NULL)
#define av_cold
Definition: attributes.h:88
AV_SAMPLE_FMT_U8
uint8_t
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
#define FFAT_ENC(NAME, ID, PROFILES,...)
#define AE
static int read_descr(GetByteContext *gb, int *tag)
static av_cold int ffat_close_encoder(AVCodecContext *avctx)
static const AVOption options[]
static const AVProfile aac_profiles[]
static int get_ilbc_mode(AVCodecContext *avctx)
static av_cold void ffat_encode_flush(AVCodecContext *avctx)
static int ffat_update_ctx(AVCodecContext *avctx)
static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static const uint64_t aac_at_channel_layouts[]
static int get_aac_tag(uint64_t in_layout)
static av_cold int ffat_init_encoder(AVCodecContext *avctx)
static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
static av_cold int get_channel_label(int channel)
static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets, AudioBufferList *data, AudioStreamPacketDescription **packets, void *inctx)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
#define FF_PROFILE_AAC_LD
Definition: avcodec.h:1872
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1863
#define FF_PROFILE_AAC_HE
Definition: avcodec.h:1870
#define FF_PROFILE_AAC_HE_V2
Definition: avcodec.h:1871
#define FF_PROFILE_AAC_LOW
Definition: avcodec.h:1867
#define FF_PROFILE_AAC_ELD
Definition: avcodec.h:1873
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
Definition: bufferqueue.h:71
static int ff_bufqueue_is_full(struct FFBufQueue *queue)
Test if a buffer queue is full.
Definition: bufferqueue.h:60
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
Definition: bufferqueue.h:111
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
Definition: bufferqueue.h:98
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
#define flags(name, subs,...)
Definition: cbs_av1.c:572
uint64_t layout
#define FFMIN(a, b)
Definition: common.h:105
#define MKBETAG(a, b, c, d)
Definition: common.h:479
#define av_clip
Definition: common.h:122
#define NULL
Definition: coverity.c:32
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:114
static AVFrame * frame
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
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:34
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
#define AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_6POINT1
#define AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_6POINT0
#define AV_CH_LAYOUT_5POINT0
#define AV_CH_LAYOUT_7POINT1
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_7POINT1_WIDE_BACK
#define AV_CH_LAYOUT_OCTAGONAL
#define AV_CH_LAYOUT_7POINT0
#define AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_5POINT1
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
#define AV_CH_SURROUND_DIRECT_RIGHT
#define AV_CH_WIDE_RIGHT
#define AV_CH_BACK_CENTER
#define AV_CH_LOW_FREQUENCY_2
#define AV_CH_BACK_RIGHT
#define AV_CH_TOP_BACK_RIGHT
#define AV_CH_SIDE_RIGHT
#define AV_CH_LOW_FREQUENCY
#define AV_CH_STEREO_RIGHT
See AV_CH_STEREO_LEFT.
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition: codec.h:129
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:275
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
@ AV_CODEC_ID_PCM_ALAW
Definition: codec_id.h:320
@ AV_CODEC_ID_ALAC
Definition: codec_id.h:440
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition: codec_id.h:353
@ AV_CODEC_ID_AAC
Definition: codec_id.h:426
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:483
@ AV_CODEC_ID_PCM_MULAW
Definition: codec_id.h:319
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:215
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
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:443
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
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
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
const VDPAUPixFmtMap * map
int i
Definition: input.c:407
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
#define MP4ESDescrTag
Definition: isom.h:310
#define MP4DecConfigDescrTag
Definition: isom.h:311
#define MP4DecSpecificDescrTag
Definition: isom.h:312
common internal API header
uint32_t tag
Definition: movenc.c:1611
const char data[16]
Definition: mxf.c:142
AVOptions.
mfxU16 profile
Definition: qsvenc.c:45
struct FFBufQueue frame_queue
AudioConverterRef converter
AVClass * av_class
AudioFrameQueue afq
AVFrame * encoding_frame
struct FFBufQueue used_frame_queue
Describe the class of an AVClass context structure.
Definition: log.h:67
main external API structure.
Definition: avcodec.h:536
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1204
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:602
int64_t bit_rate
the average bitrate
Definition: avcodec.h:586
int profile
profile
Definition: avcodec.h:1862
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1751
int initial_padding
Audio only.
Definition: avcodec.h:2066
int sample_rate
samples per second
Definition: avcodec.h:1196
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1420
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:637
int channels
number of audio channels
Definition: avcodec.h:1197
enum AVCodecID codec_id
Definition: avcodec.h:546
int extradata_size
Definition: avcodec.h:638
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: avcodec.h:1233
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1247
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1216
void * priv_data
Definition: avcodec.h:563
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:384
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
uint8_t * data
Definition: packet.h:369
AVProfile.
Definition: codec.h:183
Structure holding the queue.
Definition: bufferqueue.h:49
unsigned short available
number of available buffers
Definition: bufferqueue.h:52
const uint8_t * buffer_end
Definition: bytestream.h:34
const uint8_t * buffer
Definition: bytestream.h:34
#define av_free(p)
#define av_malloc(s)
#define av_log(a,...)
int size
int len
static double c[64]