FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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;
52 
53 static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
54 {
55  switch (codec) {
56  case AV_CODEC_ID_AAC:
57  switch (profile) {
58  case FF_PROFILE_AAC_LOW:
59  default:
60  return kAudioFormatMPEG4AAC;
61  case FF_PROFILE_AAC_HE:
62  return kAudioFormatMPEG4AAC_HE;
64  return kAudioFormatMPEG4AAC_HE_V2;
65  case FF_PROFILE_AAC_LD:
66  return kAudioFormatMPEG4AAC_LD;
67  case FF_PROFILE_AAC_ELD:
68  return kAudioFormatMPEG4AAC_ELD;
69  }
71  return kAudioFormatAppleIMA4;
72  case AV_CODEC_ID_ALAC:
73  return kAudioFormatAppleLossless;
74  case AV_CODEC_ID_ILBC:
75  return kAudioFormatiLBC;
77  return kAudioFormatALaw;
79  return kAudioFormatULaw;
80  default:
81  av_assert0(!"Invalid codec ID!");
82  return 0;
83  }
84 }
85 
86 static void ffat_update_ctx(AVCodecContext *avctx)
87 {
88  ATDecodeContext *at = avctx->priv_data;
89  UInt32 size = sizeof(unsigned);
90  AudioConverterPrimeInfo prime_info;
91  AudioStreamBasicDescription out_format;
92 
93  AudioConverterGetProperty(at->converter,
94  kAudioConverterPropertyMaximumOutputPacketSize,
95  &size, &at->pkt_size);
96 
97  if (at->pkt_size <= 0)
98  at->pkt_size = 1024 * 50;
99 
100  size = sizeof(prime_info);
101 
102  if (!AudioConverterGetProperty(at->converter,
103  kAudioConverterPrimeInfo,
104  &size, &prime_info)) {
105  avctx->initial_padding = prime_info.leadingFrames;
106  }
107 
108  size = sizeof(out_format);
109  if (!AudioConverterGetProperty(at->converter,
110  kAudioConverterCurrentOutputStreamDescription,
111  &size, &out_format)) {
112  if (out_format.mFramesPerPacket)
113  avctx->frame_size = out_format.mFramesPerPacket;
114  if (out_format.mBytesPerPacket && avctx->codec_id == AV_CODEC_ID_ILBC)
115  avctx->block_align = out_format.mBytesPerPacket;
116  }
117 
118  at->frame_size = avctx->frame_size;
119  if (avctx->codec_id == AV_CODEC_ID_PCM_MULAW ||
120  avctx->codec_id == AV_CODEC_ID_PCM_ALAW) {
121  at->pkt_size *= 1024;
122  avctx->frame_size *= 1024;
123  }
124 }
125 
126 static int read_descr(GetByteContext *gb, int *tag)
127 {
128  int len = 0;
129  int count = 4;
130  *tag = bytestream2_get_byte(gb);
131  while (count--) {
132  int c = bytestream2_get_byte(gb);
133  len = (len << 7) | (c & 0x7f);
134  if (!(c & 0x80))
135  break;
136  }
137  return len;
138 }
139 
140 static int get_ilbc_mode(AVCodecContext *avctx)
141 {
142  if (avctx->block_align == 38)
143  return 20;
144  else if (avctx->block_align == 50)
145  return 30;
146  else if (avctx->bit_rate > 0)
147  return avctx->bit_rate <= 14000 ? 30 : 20;
148  else
149  return 30;
150 }
151 
152 static av_cold int get_channel_label(int channel)
153 {
154  uint64_t map = 1 << channel;
155  if (map <= AV_CH_LOW_FREQUENCY)
156  return channel + 1;
157  else if (map <= AV_CH_BACK_RIGHT)
158  return channel + 29;
159  else if (map <= AV_CH_BACK_CENTER)
160  return channel - 1;
161  else if (map <= AV_CH_SIDE_RIGHT)
162  return channel - 4;
163  else if (map <= AV_CH_TOP_BACK_RIGHT)
164  return channel + 1;
165  else if (map <= AV_CH_STEREO_RIGHT)
166  return -1;
167  else if (map <= AV_CH_WIDE_RIGHT)
168  return channel + 4;
169  else if (map <= AV_CH_SURROUND_DIRECT_RIGHT)
170  return channel - 23;
171  else if (map == AV_CH_LOW_FREQUENCY_2)
172  return kAudioChannelLabel_LFE2;
173  else
174  return -1;
175 }
176 
177 static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
178 {
179  int i;
180  int c = 0;
181  layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
182  layout->mNumberChannelDescriptions = count;
183  for (i = 0; i < count; i++) {
184  int label;
185  while (!(in_layout & (1 << c)) && c < 64)
186  c++;
187  if (c == 64)
188  return AVERROR(EINVAL); // This should never happen
189  label = get_channel_label(c);
190  layout->mChannelDescriptions[i].mChannelLabel = label;
191  if (label < 0)
192  return AVERROR(EINVAL);
193  c++;
194  }
195  return 0;
196 }
197 
198 static int get_aac_tag(uint64_t in_layout)
199 {
200  switch (in_layout) {
201  case AV_CH_LAYOUT_MONO:
202  return kAudioChannelLayoutTag_Mono;
203  case AV_CH_LAYOUT_STEREO:
204  return kAudioChannelLayoutTag_Stereo;
205  case AV_CH_LAYOUT_QUAD:
206  return kAudioChannelLayoutTag_AAC_Quadraphonic;
208  return kAudioChannelLayoutTag_AAC_Octagonal;
210  return kAudioChannelLayoutTag_AAC_3_0;
212  return kAudioChannelLayoutTag_AAC_4_0;
214  return kAudioChannelLayoutTag_AAC_5_0;
216  return kAudioChannelLayoutTag_AAC_5_1;
218  return kAudioChannelLayoutTag_AAC_6_0;
220  return kAudioChannelLayoutTag_AAC_6_1;
222  return kAudioChannelLayoutTag_AAC_7_0;
224  return kAudioChannelLayoutTag_AAC_7_1;
226  return kAudioChannelLayoutTag_MPEG_7_1_C;
227  default:
228  return 0;
229  }
230 }
231 
233 {
234  ATDecodeContext *at = avctx->priv_data;
235  OSStatus status;
236 
237  AudioStreamBasicDescription in_format = {
238  .mSampleRate = avctx->sample_rate,
239  .mFormatID = kAudioFormatLinearPCM,
240  .mFormatFlags = ((avctx->sample_fmt == AV_SAMPLE_FMT_FLT ||
241  avctx->sample_fmt == AV_SAMPLE_FMT_DBL) ? kAudioFormatFlagIsFloat
242  : avctx->sample_fmt == AV_SAMPLE_FMT_U8 ? 0
243  : kAudioFormatFlagIsSignedInteger)
244  | kAudioFormatFlagIsPacked,
245  .mBytesPerPacket = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels,
246  .mFramesPerPacket = 1,
247  .mBytesPerFrame = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels,
248  .mChannelsPerFrame = avctx->channels,
249  .mBitsPerChannel = av_get_bytes_per_sample(avctx->sample_fmt) * 8,
250  };
251  AudioStreamBasicDescription out_format = {
252  .mSampleRate = avctx->sample_rate,
253  .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
254  .mChannelsPerFrame = in_format.mChannelsPerFrame,
255  };
256  UInt32 layout_size = sizeof(AudioChannelLayout) +
257  sizeof(AudioChannelDescription) * avctx->channels;
258  AudioChannelLayout *channel_layout = av_malloc(layout_size);
259 
260  if (!channel_layout)
261  return AVERROR(ENOMEM);
262 
263  if (avctx->codec_id == AV_CODEC_ID_ILBC) {
264  int mode = get_ilbc_mode(avctx);
265  out_format.mFramesPerPacket = 8000 * mode / 1000;
266  out_format.mBytesPerPacket = (mode == 20 ? 38 : 50);
267  }
268 
269  status = AudioConverterNew(&in_format, &out_format, &at->converter);
270 
271  if (status != 0) {
272  av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
273  av_free(channel_layout);
274  return AVERROR_UNKNOWN;
275  }
276 
277  if (!avctx->channel_layout)
279 
280  if ((status = remap_layout(channel_layout, avctx->channel_layout, avctx->channels)) < 0) {
281  av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
282  av_free(channel_layout);
283  return status;
284  }
285 
286  if (AudioConverterSetProperty(at->converter, kAudioConverterInputChannelLayout,
287  layout_size, channel_layout)) {
288  av_log(avctx, AV_LOG_ERROR, "Unsupported input channel layout\n");
289  av_free(channel_layout);
290  return AVERROR(EINVAL);
291  }
292  if (avctx->codec_id == AV_CODEC_ID_AAC) {
293  int tag = get_aac_tag(avctx->channel_layout);
294  if (tag) {
295  channel_layout->mChannelLayoutTag = tag;
296  channel_layout->mNumberChannelDescriptions = 0;
297  }
298  }
299  if (AudioConverterSetProperty(at->converter, kAudioConverterOutputChannelLayout,
300  layout_size, channel_layout)) {
301  av_log(avctx, AV_LOG_ERROR, "Unsupported output channel layout\n");
302  av_free(channel_layout);
303  return AVERROR(EINVAL);
304  }
305  av_free(channel_layout);
306 
307  if (avctx->bits_per_raw_sample)
308  AudioConverterSetProperty(at->converter,
309  kAudioConverterPropertyBitDepthHint,
310  sizeof(avctx->bits_per_raw_sample),
311  &avctx->bits_per_raw_sample);
312 
313 #if !TARGET_OS_IPHONE
314  if (at->mode == -1)
315  at->mode = (avctx->flags & AV_CODEC_FLAG_QSCALE) ?
316  kAudioCodecBitRateControlMode_Variable :
317  kAudioCodecBitRateControlMode_Constant;
318 
319  AudioConverterSetProperty(at->converter, kAudioCodecPropertyBitRateControlMode,
320  sizeof(at->mode), &at->mode);
321 
322  if (at->mode == kAudioCodecBitRateControlMode_Variable) {
323  int q = avctx->global_quality / FF_QP2LAMBDA;
324  if (q < 0 || q > 14) {
325  av_log(avctx, AV_LOG_WARNING,
326  "VBR quality %d out of range, should be 0-14\n", q);
327  q = av_clip(q, 0, 14);
328  }
329  q = 127 - q * 9;
330  AudioConverterSetProperty(at->converter, kAudioCodecPropertySoundQualityForVBR,
331  sizeof(q), &q);
332  } else
333 #endif
334  if (avctx->bit_rate > 0) {
335  UInt32 rate = avctx->bit_rate;
336  UInt32 size;
337  status = AudioConverterGetPropertyInfo(at->converter,
338  kAudioConverterApplicableEncodeBitRates,
339  &size, NULL);
340  if (!status && size) {
341  UInt32 new_rate = rate;
342  int count;
343  int i;
344  AudioValueRange *ranges = av_malloc(size);
345  if (!ranges)
346  return AVERROR(ENOMEM);
347  AudioConverterGetProperty(at->converter,
348  kAudioConverterApplicableEncodeBitRates,
349  &size, ranges);
350  count = size / sizeof(AudioValueRange);
351  for (i = 0; i < count; i++) {
352  AudioValueRange *range = &ranges[i];
353  if (rate >= range->mMinimum && rate <= range->mMaximum) {
354  new_rate = rate;
355  break;
356  } else if (rate > range->mMaximum) {
357  new_rate = range->mMaximum;
358  } else {
359  new_rate = range->mMinimum;
360  break;
361  }
362  }
363  if (new_rate != rate) {
364  av_log(avctx, AV_LOG_WARNING,
365  "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
366  rate = new_rate;
367  }
368  av_free(ranges);
369  }
370  AudioConverterSetProperty(at->converter, kAudioConverterEncodeBitRate,
371  sizeof(rate), &rate);
372  }
373 
374  at->quality = 96 - at->quality * 32;
375  AudioConverterSetProperty(at->converter, kAudioConverterCodecQuality,
376  sizeof(at->quality), &at->quality);
377 
378  if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterCompressionMagicCookie,
379  &avctx->extradata_size, NULL) &&
380  avctx->extradata_size) {
381  int extradata_size = avctx->extradata_size;
382  uint8_t *extradata;
384  return AVERROR(ENOMEM);
385  if (avctx->codec_id == AV_CODEC_ID_ALAC) {
386  avctx->extradata_size = 0x24;
387  AV_WB32(avctx->extradata, 0x24);
388  AV_WB32(avctx->extradata + 4, MKBETAG('a','l','a','c'));
389  extradata = avctx->extradata + 12;
390  avctx->extradata_size = 0x24;
391  } else {
392  extradata = avctx->extradata;
393  }
394  status = AudioConverterGetProperty(at->converter,
395  kAudioConverterCompressionMagicCookie,
396  &extradata_size, extradata);
397  if (status != 0) {
398  av_log(avctx, AV_LOG_ERROR, "AudioToolbox cookie error: %i\n", (int)status);
399  return AVERROR_UNKNOWN;
400  } else if (avctx->codec_id == AV_CODEC_ID_AAC) {
401  GetByteContext gb;
402  int tag, len;
403  bytestream2_init(&gb, extradata, extradata_size);
404  do {
405  len = read_descr(&gb, &tag);
406  if (tag == MP4DecConfigDescrTag) {
407  bytestream2_skip(&gb, 13);
408  len = read_descr(&gb, &tag);
409  if (tag == MP4DecSpecificDescrTag) {
410  len = FFMIN(gb.buffer_end - gb.buffer, len);
411  memmove(extradata, gb.buffer, len);
412  avctx->extradata_size = len;
413  break;
414  }
415  } else if (tag == MP4ESDescrTag) {
416  int flags;
417  bytestream2_skip(&gb, 2);
418  flags = bytestream2_get_byte(&gb);
419  if (flags & 0x80) //streamDependenceFlag
420  bytestream2_skip(&gb, 2);
421  if (flags & 0x40) //URL_Flag
422  bytestream2_skip(&gb, bytestream2_get_byte(&gb));
423  if (flags & 0x20) //OCRstreamFlag
424  bytestream2_skip(&gb, 2);
425  }
426  } while (bytestream2_get_bytes_left(&gb));
427  } else if (avctx->codec_id != AV_CODEC_ID_ALAC) {
428  avctx->extradata_size = extradata_size;
429  }
430  }
431 
432  ffat_update_ctx(avctx);
433 
434 #if !TARGET_OS_IPHONE && defined(__MAC_10_9)
435  if (at->mode == kAudioCodecBitRateControlMode_Variable && avctx->rc_max_rate) {
436  UInt32 max_size = avctx->rc_max_rate * avctx->frame_size / avctx->sample_rate;
437  if (max_size)
438  AudioConverterSetProperty(at->converter, kAudioCodecPropertyPacketSizeLimitForVBR,
439  sizeof(max_size), &max_size);
440  }
441 #endif
442 
443  ff_af_queue_init(avctx, &at->afq);
444 
445  return 0;
446 }
447 
448 static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets,
449  AudioBufferList *data,
450  AudioStreamPacketDescription **packets,
451  void *inctx)
452 {
453  AVCodecContext *avctx = inctx;
454  ATDecodeContext *at = avctx->priv_data;
455  AVFrame *frame;
456 
457  if (!at->frame_queue.available) {
458  if (at->eof) {
459  *nb_packets = 0;
460  return 0;
461  } else {
462  *nb_packets = 0;
463  return 1;
464  }
465  }
466 
467  frame = ff_bufqueue_get(&at->frame_queue);
468 
469  data->mNumberBuffers = 1;
470  data->mBuffers[0].mNumberChannels = avctx->channels;
471  data->mBuffers[0].mDataByteSize = frame->nb_samples *
473  avctx->channels;
474  data->mBuffers[0].mData = frame->data[0];
475  if (*nb_packets > frame->nb_samples)
476  *nb_packets = frame->nb_samples;
477 
478  ff_bufqueue_add(avctx, &at->used_frame_queue, frame);
479 
480  return 0;
481 }
482 
483 static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt,
484  const AVFrame *frame, int *got_packet_ptr)
485 {
486  ATDecodeContext *at = avctx->priv_data;
487  OSStatus ret;
488 
489  AudioBufferList out_buffers = {
490  .mNumberBuffers = 1,
491  .mBuffers = {
492  {
493  .mNumberChannels = avctx->channels,
494  .mDataByteSize = at->pkt_size,
495  }
496  }
497  };
498  AudioStreamPacketDescription out_pkt_desc = {0};
499 
500  if (frame) {
501  AVFrame *in_frame;
502 
503  if (ff_bufqueue_is_full(&at->frame_queue)) {
504  /*
505  * The frame queue is significantly larger than needed in practice,
506  * but no clear way to determine the minimum number of samples to
507  * get output from AudioConverterFillComplexBuffer().
508  */
509  av_log(avctx, AV_LOG_ERROR, "Bug: frame queue is too small.\n");
510  return AVERROR_BUG;
511  }
512 
513  if ((ret = ff_af_queue_add(&at->afq, frame)) < 0)
514  return ret;
515 
516  in_frame = av_frame_clone(frame);
517  if (!in_frame)
518  return AVERROR(ENOMEM);
519 
520  ff_bufqueue_add(avctx, &at->frame_queue, in_frame);
521  } else {
522  at->eof = 1;
523  }
524 
525  if ((ret = ff_alloc_packet2(avctx, avpkt, at->pkt_size, 0)) < 0)
526  return ret;
527 
528 
529  out_buffers.mBuffers[0].mData = avpkt->data;
530 
531  *got_packet_ptr = avctx->frame_size / at->frame_size;
532 
533  ret = AudioConverterFillComplexBuffer(at->converter, ffat_encode_callback, avctx,
534  got_packet_ptr, &out_buffers,
535  (avctx->frame_size > at->frame_size) ? NULL : &out_pkt_desc);
536 
538 
539  if ((!ret || ret == 1) && *got_packet_ptr) {
540  avpkt->size = out_buffers.mBuffers[0].mDataByteSize;
541  ff_af_queue_remove(&at->afq, out_pkt_desc.mVariableFramesInPacket ?
542  out_pkt_desc.mVariableFramesInPacket :
543  avctx->frame_size,
544  &avpkt->pts,
545  &avpkt->duration);
546  } else if (ret && ret != 1) {
547  av_log(avctx, AV_LOG_WARNING, "Encode error: %i\n", ret);
548  }
549 
550  return 0;
551 }
552 
554 {
555  ATDecodeContext *at = avctx->priv_data;
556  AudioConverterReset(at->converter);
559 }
560 
562 {
563  ATDecodeContext *at = avctx->priv_data;
564  AudioConverterDispose(at->converter);
567  ff_af_queue_close(&at->afq);
568  return 0;
569 }
570 
571 static const AVProfile aac_profiles[] = {
572  { FF_PROFILE_AAC_LOW, "LC" },
573  { FF_PROFILE_AAC_HE, "HE-AAC" },
574  { FF_PROFILE_AAC_HE_V2, "HE-AACv2" },
575  { FF_PROFILE_AAC_LD, "LD" },
576  { FF_PROFILE_AAC_ELD, "ELD" },
577  { FF_PROFILE_UNKNOWN },
578 };
579 
580 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
581 static const AVOption options[] = {
582 #if !TARGET_OS_IPHONE
583  {"aac_at_mode", "ratecontrol mode", offsetof(ATDecodeContext, mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable, AE, "mode"},
584  {"auto", "VBR if global quality is given; CBR otherwise", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, AE, "mode"},
585  {"cbr", "constant bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX, AE, "mode"},
586  {"abr", "long-term average bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX, AE, "mode"},
587  {"cvbr", "constrained variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX, AE, "mode"},
588  {"vbr" , "variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX, AE, "mode"},
589 #endif
590  {"aac_at_quality", "quality vs speed control", offsetof(ATDecodeContext, quality), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2, AE},
591  { NULL },
592 };
593 
594 #define FFAT_ENC_CLASS(NAME) \
595  static const AVClass ffat_##NAME##_enc_class = { \
596  .class_name = "at_" #NAME "_enc", \
597  .item_name = av_default_item_name, \
598  .option = options, \
599  .version = LIBAVUTIL_VERSION_INT, \
600  };
601 
602 #define FFAT_ENC(NAME, ID, PROFILES, ...) \
603  FFAT_ENC_CLASS(NAME) \
604  AVCodec ff_##NAME##_at_encoder = { \
605  .name = #NAME "_at", \
606  .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
607  .type = AVMEDIA_TYPE_AUDIO, \
608  .id = ID, \
609  .priv_data_size = sizeof(ATDecodeContext), \
610  .init = ffat_init_encoder, \
611  .close = ffat_close_encoder, \
612  .encode2 = ffat_encode, \
613  .flush = ffat_encode_flush, \
614  .priv_class = &ffat_##NAME##_enc_class, \
615  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY __VA_ARGS__, \
616  .sample_fmts = (const enum AVSampleFormat[]) { \
617  AV_SAMPLE_FMT_S16, \
618  AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
619  }, \
620  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
621  .profiles = PROFILES, \
622  };
623 
624 static const uint64_t aac_at_channel_layouts[] = {
637  0,
638 };
639 
641 //FFAT_ENC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT, NULL)
643 FFAT_ENC(ilbc, AV_CODEC_ID_ILBC, NULL)
644 FFAT_ENC(pcm_alaw, AV_CODEC_ID_PCM_ALAW, NULL)
645 FFAT_ENC(pcm_mulaw, AV_CODEC_ID_PCM_MULAW, 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
#define AV_CH_LAYOUT_7POINT1
static int get_aac_tag(uint64_t in_layout)
#define FFAT_ENC(NAME, ID, PROFILES,...)
#define MP4ESDescrTag
Definition: isom.h:251
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#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:1741
#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:1602
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
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
#define AV_CH_SURROUND_DIRECT_RIGHT
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3077
#define AV_CH_LAYOUT_STEREO
static av_cold int get_channel_label(int channel)
#define FF_PROFILE_AAC_HE_V2
Definition: avcodec.h:3190
int profile
profile
Definition: avcodec.h:3181
#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:2475
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
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2446
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AV_SAMPLE_FMT_U8
mode
Definition: f_perms.c:27
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:1619
#define AV_CH_WIDE_RIGHT
AVClass * av_class
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1791
#define AV_CH_LOW_FREQUENCY
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1601
const uint8_t * buffer
Definition: bytestream.h:34
uint32_t tag
Definition: movenc.c:1382
ptrdiff_t size
Definition: opengl_enc.c:101
#define FF_PROFILE_AAC_LD
Definition: avcodec.h:3191
#define av_log(a,...)
#define AV_CH_LAYOUT_5POINT1
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:191
#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:253
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets, AudioBufferList *data, AudioStreamPacketDescription **packets, void *inctx)
int initial_padding
Audio only.
Definition: avcodec.h:3366
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1771
simple assert() macros that are a bit more flexible than ISO C assert().
#define AV_CH_LAYOUT_QUAD
GLsizei count
Definition: opengl_enc.c:109
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define FF_PROFILE_AAC_ELD
Definition: avcodec.h:3192
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition: avcodec.h:1038
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2489
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:3186
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:3182
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:480
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2458
Libavcodec external API header.
#define MP4DecConfigDescrTag
Definition: isom.h:252
enum AVCodecID codec_id
Definition: avcodec.h:1693
unsigned short available
number of available buffers
Definition: bufferqueue.h:52
int sample_rate
samples per second
Definition: avcodec.h:2438
main external API structure.
Definition: avcodec.h:1676
int extradata_size
Definition: avcodec.h:1792
#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:118
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
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1722
mfxU16 profile
Definition: qsvenc.c:42
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1757
static int flags
Definition: cpu.c:47
#define AV_CH_BACK_CENTER
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
#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]
#define AV_CODEC_CAP_LOSSLESS
Codec is lossless.
Definition: avcodec.h:1056
AVProfile.
Definition: avcodec.h:3588
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define MKBETAG(a, b, c, d)
Definition: common.h:343
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:734
void * priv_data
Definition: avcodec.h:1718
#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:2439
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:221
uint64_t layout
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
#define FF_PROFILE_AAC_HE
Definition: avcodec.h:3189
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)
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:1578
static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1594
#define AV_CH_BACK_RIGHT
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:2676