FFmpeg  4.0
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;
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) {
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 
474  frame = ff_bufqueue_get(&at->frame_queue);
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 
486  ret = av_frame_ref(at->encoding_frame, frame);
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 __VA_ARGS__, \
631  .sample_fmts = (const enum AVSampleFormat[]) { \
632  AV_SAMPLE_FMT_S16, \
633  AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
634  }, \
635  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
636  .profiles = PROFILES, \
637  .wrapper_name = "at", \
638  };
639 
640 static const uint64_t aac_at_channel_layouts[] = {
653  0,
654 };
655 
657 //FFAT_ENC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT, NULL)
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
Definition: bufferqueue.h:98
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
#define NULL
Definition: coverity.c:32
uint8_t * extradata
#define AV_CH_LAYOUT_7POINT1
static int get_aac_tag(uint64_t in_layout)
#define FFAT_ENC(NAME, ID, PROFILES,...)
int size
#define MP4ESDescrTag
Definition: isom.h:287
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
AVOption.
Definition: opt.h:246
#define AV_CH_LAYOUT_6POINT1
#define AV_CH_LAYOUT_6POINT0
static av_cold int ffat_init_encoder(AVCodecContext *avctx)
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1568
#define AV_CH_LAYOUT_SURROUND
static av_cold int ffat_close_encoder(AVCodecContext *avctx)
#define AE
struct FFBufQueue used_frame_queue
int size
Definition: avcodec.h:1431
struct FFBufQueue frame_queue
#define AV_CH_LOW_FREQUENCY_2
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
#define AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_7POINT0
#define AV_CH_SURROUND_DIRECT_RIGHT
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2741
#define AV_CH_LAYOUT_STEREO
static av_cold int get_channel_label(int channel)
#define FF_PROFILE_AAC_HE_V2
Definition: avcodec.h:2852
int profile
profile
Definition: avcodec.h:2843
#define AV_CH_LAYOUT_5POINT0
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2210
static av_cold void ffat_encode_flush(AVCodecContext *avctx)
Structure holding the queue.
Definition: bufferqueue.h:49
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
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
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2181
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AV_SAMPLE_FMT_U8
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
static const AVProfile aac_profiles[]
AVOptions.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1448
#define AV_CH_WIDE_RIGHT
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:441
AVClass * av_class
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1618
#define AV_CH_LOW_FREQUENCY
static AVFrame * frame
const char data[16]
Definition: mxf.c:90
uint8_t * data
Definition: avcodec.h:1430
const uint8_t * buffer
Definition: bytestream.h:34
static int flags
Definition: log.c:55
uint32_t tag
Definition: movenc.c:1455
#define FF_PROFILE_AAC_LD
Definition: avcodec.h:2853
#define av_log(a,...)
#define AV_CH_LAYOUT_5POINT1
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void ffat_update_ctx(AVCodecContext *avctx)
#define AVERROR(e)
Definition: error.h:43
#define MP4DecSpecificDescrTag
Definition: isom.h:289
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets, AudioBufferList *data, AudioStreamPacketDescription **packets, void *inctx)
int initial_padding
Audio only.
Definition: avcodec.h:3031
AVFrame * encoding_frame
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1598
simple assert() macros that are a bit more flexible than ISO C assert().
#define AV_CH_LAYOUT_QUAD
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define FF_PROFILE_AAC_ELD
Definition: avcodec.h:2854
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition: avcodec.h:1031
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2224
static const AVOption options[]
static const uint64_t aac_at_channel_layouts[]
const uint8_t * buffer_end
Definition: bytestream.h:34
#define AV_CH_STEREO_RIGHT
See AV_CH_STEREO_LEFT.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:833
#define FFMIN(a, b)
Definition: common.h:96
static int ff_bufqueue_is_full(struct FFBufQueue *queue)
Test if a buffer queue is full.
Definition: bufferqueue.h:60
#define FF_PROFILE_AAC_LOW
Definition: avcodec.h:2848
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2844
AudioConverterRef converter
AudioFrameQueue afq
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
Definition: bufferqueue.h:111
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:538
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2193
Libavcodec external API header.
#define MP4DecConfigDescrTag
Definition: isom.h:288
enum AVCodecID codec_id
Definition: avcodec.h:1528
unsigned short available
number of available buffers
Definition: bufferqueue.h:52
int sample_rate
samples per second
Definition: avcodec.h:2173
main external API structure.
Definition: avcodec.h:1518
int extradata_size
Definition: avcodec.h:1619
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define AV_CH_TOP_BACK_RIGHT
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:113
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_CH_LAYOUT_7POINT1_WIDE_BACK
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
#define AV_CH_LAYOUT_OCTAGONAL
static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
const VDPAUPixFmtMap * map
mfxU16 profile
Definition: qsvenc.c:44
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:551
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1584
#define AV_CH_BACK_CENTER
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
#define AV_CH_SIDE_RIGHT
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
common internal api header.
static double c[64]
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
#define AV_CODEC_CAP_LOSSLESS
Codec is lossless.
Definition: avcodec.h:1049
AVProfile.
Definition: avcodec.h:3341
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define MKBETAG(a, b, c, d)
Definition: common.h:367
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:773
void * priv_data
Definition: avcodec.h:1545
#define av_free(p)
static int get_ilbc_mode(AVCodecContext *avctx)
static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
int len
int channels
number of audio channels
Definition: avcodec.h:2174
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
uint64_t layout
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
#define FF_PROFILE_AAC_HE
Definition: avcodec.h:2851
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
static int read_descr(GetByteContext *gb, int *tag)
void INT64 INT64 count
Definition: avisynth_c.h:690
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
Definition: bufferqueue.h:71
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1407
static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:284
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1423
#define AV_CH_BACK_RIGHT
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:2391