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