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;
56
59
60static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
61{
62 switch (codec) {
63 case AV_CODEC_ID_AAC:
64 switch (profile) {
66 default:
67 return kAudioFormatMPEG4AAC;
69 return kAudioFormatMPEG4AAC_HE;
71 return kAudioFormatMPEG4AAC_HE_V2;
73 return kAudioFormatMPEG4AAC_LD;
74#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
76 return kAudioFormatMPEG4AAC_ELD;
77#endif
78 }
80 return kAudioFormatAppleIMA4;
82 return kAudioFormatAppleLossless;
83#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
85 return kAudioFormatiLBC;
86#endif
88 return kAudioFormatALaw;
90 return kAudioFormatULaw;
91 default:
92 av_assert0(!"Invalid codec ID!");
93 return 0;
94 }
95}
96
98{
99 ATDecodeContext *at = avctx->priv_data;
100 UInt32 size = sizeof(unsigned);
101 AudioConverterPrimeInfo prime_info;
102 AudioStreamBasicDescription out_format;
103
104 AudioConverterGetProperty(at->converter,
105 kAudioConverterPropertyMaximumOutputPacketSize,
106 &size, &at->pkt_size);
107
108 if (at->pkt_size <= 0)
109 at->pkt_size = 1024 * 50;
110
111 size = sizeof(prime_info);
112
113 if (!AudioConverterGetProperty(at->converter,
114 kAudioConverterPrimeInfo,
115 &size, &prime_info)) {
116 avctx->initial_padding = prime_info.leadingFrames;
117 if (avctx->codec_id == AV_CODEC_ID_AAC &&
118 (avctx->profile == AV_PROFILE_AAC_HE ||
119 avctx->profile == AV_PROFILE_AAC_HE_V2))
120 avctx->initial_padding *= 2;
121 }
122
123 size = sizeof(out_format);
124 if (!AudioConverterGetProperty(at->converter,
125 kAudioConverterCurrentOutputStreamDescription,
126 &size, &out_format)) {
127 if (out_format.mFramesPerPacket) {
128 avctx->frame_size = out_format.mFramesPerPacket;
129 } else {
130 /* The doc on mFramesPerPacket says:
131 * For formats with a variable number of frames per packet, such as
132 * Ogg Vorbis, set this field to 0.
133 * Looks like it means for decoding. There is no known case that
134 * mFramesPerPacket is zero for encoding. Use a default value for safety.
135 */
136 avctx->frame_size = 1024;
137 av_log(avctx, AV_LOG_WARNING, "Missing mFramesPerPacket\n");
138 }
139 if (out_format.mBytesPerPacket && avctx->codec_id == AV_CODEC_ID_ILBC)
140 avctx->block_align = out_format.mBytesPerPacket;
141 } else {
142 av_log(avctx, AV_LOG_ERROR, "Get OutputStreamDescription failed\n");
143 return AVERROR_EXTERNAL;
144 }
145
146 at->frame_size = avctx->frame_size;
147 if (avctx->codec_id == AV_CODEC_ID_PCM_MULAW ||
148 avctx->codec_id == AV_CODEC_ID_PCM_ALAW) {
149 at->pkt_size *= 1024;
150 avctx->frame_size *= 1024;
151 }
152
153 return 0;
154}
155
156static int read_descr(GetByteContext *gb, int *tag)
157{
158 int len = 0;
159 int count = 4;
160 *tag = bytestream2_get_byte(gb);
161 while (count--) {
162 int c = bytestream2_get_byte(gb);
163 len = (len << 7) | (c & 0x7f);
164 if (!(c & 0x80))
165 break;
166 }
167 return len;
168}
169
171{
172 if (avctx->block_align == 38)
173 return 20;
174 else if (avctx->block_align == 50)
175 return 30;
176 else if (avctx->bit_rate > 0)
177 return avctx->bit_rate <= 14000 ? 30 : 20;
178 else
179 return 30;
180}
181
183{
184 uint64_t map = 1ULL << channel;
186 return channel + 1;
187 else if (map <= AV_CH_BACK_RIGHT)
188 return channel + 29;
189 else if (map <= AV_CH_BACK_CENTER)
190 return channel - 1;
191 else if (map <= AV_CH_SIDE_RIGHT)
192 return channel - 4;
193 else if (map <= AV_CH_TOP_BACK_RIGHT)
194 return channel + 1;
195 else if (map <= AV_CH_STEREO_RIGHT)
196 return -1;
197 else if (map <= AV_CH_WIDE_RIGHT)
198 return channel + 4;
200 return channel - 23;
201 else if (map == AV_CH_LOW_FREQUENCY_2)
202 return kAudioChannelLabel_LFE2;
203 else
204 return -1;
205}
206
207static int remap_layout(AudioChannelLayout *layout, const AVChannelLayout *in_layout)
208{
209 int i;
210 layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
211 layout->mNumberChannelDescriptions = in_layout->nb_channels;
212 for (i = 0; i < in_layout->nb_channels; i++) {
213 int c, label;
214
216 if (c < 0 || c >= 64)
217 return AVERROR(EINVAL);
219 layout->mChannelDescriptions[i].mChannelLabel = label;
220 if (label < 0)
221 return AVERROR(EINVAL);
222 c++;
223 }
224 return 0;
225}
226
227static int get_aac_tag(const AVChannelLayout *in_layout)
228{
229 static const struct {
230 AVChannelLayout chl;
231 int tag;
232 } map[] = {
233 { AV_CHANNEL_LAYOUT_MONO, kAudioChannelLayoutTag_Mono },
234 { AV_CHANNEL_LAYOUT_STEREO, kAudioChannelLayoutTag_Stereo },
235 { AV_CHANNEL_LAYOUT_QUAD, kAudioChannelLayoutTag_AAC_Quadraphonic },
236 { AV_CHANNEL_LAYOUT_OCTAGONAL, kAudioChannelLayoutTag_AAC_Octagonal },
237 { AV_CHANNEL_LAYOUT_SURROUND, kAudioChannelLayoutTag_AAC_3_0 },
238 { AV_CHANNEL_LAYOUT_4POINT0, kAudioChannelLayoutTag_AAC_4_0 },
239 { AV_CHANNEL_LAYOUT_5POINT0, kAudioChannelLayoutTag_AAC_5_0 },
240 { AV_CHANNEL_LAYOUT_5POINT1, kAudioChannelLayoutTag_AAC_5_1 },
241 { AV_CHANNEL_LAYOUT_6POINT0, kAudioChannelLayoutTag_AAC_6_0 },
242 { AV_CHANNEL_LAYOUT_6POINT1, kAudioChannelLayoutTag_AAC_6_1 },
243 { AV_CHANNEL_LAYOUT_7POINT0, kAudioChannelLayoutTag_AAC_7_0 },
244 { AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK, kAudioChannelLayoutTag_AAC_7_1 },
245 { AV_CHANNEL_LAYOUT_7POINT1, kAudioChannelLayoutTag_MPEG_7_1_C },
246 };
247 int i;
248
249 for (i = 0; i < FF_ARRAY_ELEMS(map); i++)
250 if (!av_channel_layout_compare(in_layout, &map[i].chl))
251 return map[i].tag;
252
253 return 0;
254}
255
257{
258 ATDecodeContext *at = avctx->priv_data;
259 OSStatus status;
260 int ret;
261
262 AudioStreamBasicDescription in_format = {
263 .mSampleRate = avctx->sample_rate,
264 .mFormatID = kAudioFormatLinearPCM,
265 .mFormatFlags = ((avctx->sample_fmt == AV_SAMPLE_FMT_FLT ||
266 avctx->sample_fmt == AV_SAMPLE_FMT_DBL) ? kAudioFormatFlagIsFloat
267 : avctx->sample_fmt == AV_SAMPLE_FMT_U8 ? 0
268 : kAudioFormatFlagIsSignedInteger)
269 | kAudioFormatFlagIsPacked,
270 .mBytesPerPacket = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->ch_layout.nb_channels,
271 .mFramesPerPacket = 1,
272 .mBytesPerFrame = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->ch_layout.nb_channels,
273 .mChannelsPerFrame = avctx->ch_layout.nb_channels,
274 .mBitsPerChannel = av_get_bytes_per_sample(avctx->sample_fmt) * 8,
275 };
276 AudioStreamBasicDescription out_format = {
277 .mSampleRate = avctx->sample_rate,
278 .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
279 .mChannelsPerFrame = in_format.mChannelsPerFrame,
280 };
281 UInt32 layout_size = sizeof(AudioChannelLayout) +
282 sizeof(AudioChannelDescription) * avctx->ch_layout.nb_channels;
283 AudioChannelLayout *channel_layout = av_malloc(layout_size);
284
285 if (!channel_layout)
286 return AVERROR(ENOMEM);
287
288 if (avctx->codec_id == AV_CODEC_ID_ILBC) {
289 int mode = get_ilbc_mode(avctx);
290 out_format.mFramesPerPacket = 8000 * mode / 1000;
291 out_format.mBytesPerPacket = (mode == 20 ? 38 : 50);
292 }
293
294 status = AudioConverterNew(&in_format, &out_format, &at->converter);
295
296 if (status != 0) {
297 av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
298 av_free(channel_layout);
299 return AVERROR_UNKNOWN;
300 }
301
304
305 if ((status = remap_layout(channel_layout, &avctx->ch_layout)) < 0) {
306 av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
307 av_free(channel_layout);
308 return status;
309 }
310
311 if (AudioConverterSetProperty(at->converter, kAudioConverterInputChannelLayout,
312 layout_size, channel_layout)) {
313 av_log(avctx, AV_LOG_ERROR, "Unsupported input channel layout\n");
314 av_free(channel_layout);
315 return AVERROR(EINVAL);
316 }
317 if (avctx->codec_id == AV_CODEC_ID_AAC) {
318 int tag = get_aac_tag(&avctx->ch_layout);
319 if (tag) {
320 channel_layout->mChannelLayoutTag = tag;
321 channel_layout->mNumberChannelDescriptions = 0;
322 }
323 }
324 if (AudioConverterSetProperty(at->converter, kAudioConverterOutputChannelLayout,
325 layout_size, channel_layout)) {
326 av_log(avctx, AV_LOG_ERROR, "Unsupported output channel layout\n");
327 av_free(channel_layout);
328 return AVERROR(EINVAL);
329 }
330 av_free(channel_layout);
331
332 if (avctx->bits_per_raw_sample)
333 AudioConverterSetProperty(at->converter,
334 kAudioConverterPropertyBitDepthHint,
335 sizeof(avctx->bits_per_raw_sample),
336 &avctx->bits_per_raw_sample);
337
338#if !TARGET_OS_IPHONE
339 if (at->mode == -1)
340 at->mode = (avctx->flags & AV_CODEC_FLAG_QSCALE) ?
341 kAudioCodecBitRateControlMode_Variable :
342 kAudioCodecBitRateControlMode_Constant;
343
344 AudioConverterSetProperty(at->converter, kAudioCodecPropertyBitRateControlMode,
345 sizeof(at->mode), &at->mode);
346
347 if (at->mode == kAudioCodecBitRateControlMode_Variable) {
348 int q = avctx->global_quality / FF_QP2LAMBDA;
349 if (q < 0 || q > 14) {
350 av_log(avctx, AV_LOG_WARNING,
351 "VBR quality %d out of range, should be 0-14\n", q);
352 q = av_clip(q, 0, 14);
353 }
354 q = 127 - q * 9;
355 AudioConverterSetProperty(at->converter, kAudioCodecPropertySoundQualityForVBR,
356 sizeof(q), &q);
357 } else
358#endif
359 if (avctx->bit_rate > 0) {
360 UInt32 rate = avctx->bit_rate;
361 UInt32 size;
362 status = AudioConverterGetPropertyInfo(at->converter,
363 kAudioConverterApplicableEncodeBitRates,
364 &size, NULL);
365 if (!status && size) {
366 UInt32 new_rate = rate;
367 int count;
368 int i;
369 AudioValueRange *ranges = av_malloc(size);
370 if (!ranges)
371 return AVERROR(ENOMEM);
372 AudioConverterGetProperty(at->converter,
373 kAudioConverterApplicableEncodeBitRates,
374 &size, ranges);
375 count = size / sizeof(AudioValueRange);
376 for (i = 0; i < count; i++) {
377 AudioValueRange *range = &ranges[i];
378 if (rate >= range->mMinimum && rate <= range->mMaximum) {
379 new_rate = rate;
380 break;
381 } else if (rate > range->mMaximum) {
382 new_rate = range->mMaximum;
383 } else {
384 new_rate = range->mMinimum;
385 break;
386 }
387 }
388 if (new_rate != rate) {
389 av_log(avctx, AV_LOG_WARNING,
390 "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
391 rate = new_rate;
392 }
393 av_free(ranges);
394 }
395 AudioConverterSetProperty(at->converter, kAudioConverterEncodeBitRate,
396 sizeof(rate), &rate);
397 }
398
399 at->quality = 96 - at->quality * 32;
400 AudioConverterSetProperty(at->converter, kAudioConverterCodecQuality,
401 sizeof(at->quality), &at->quality);
402
403 if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterCompressionMagicCookie,
404 &avctx->extradata_size, NULL) &&
405 avctx->extradata_size) {
406 int extradata_size = avctx->extradata_size;
407 uint8_t *extradata;
409 return AVERROR(ENOMEM);
410 if (avctx->codec_id == AV_CODEC_ID_ALAC) {
411 avctx->extradata_size = 0x24;
412 AV_WB32(avctx->extradata, 0x24);
413 AV_WB32(avctx->extradata + 4, MKBETAG('a','l','a','c'));
414 extradata = avctx->extradata + 12;
415 avctx->extradata_size = 0x24;
416 } else {
417 extradata = avctx->extradata;
418 }
419 status = AudioConverterGetProperty(at->converter,
420 kAudioConverterCompressionMagicCookie,
421 &extradata_size, extradata);
422 if (status != 0) {
423 av_log(avctx, AV_LOG_ERROR, "AudioToolbox cookie error: %i\n", (int)status);
424 return AVERROR_UNKNOWN;
425 } else if (avctx->codec_id == AV_CODEC_ID_AAC) {
427 int tag, len;
428 bytestream2_init(&gb, extradata, extradata_size);
429 do {
430 len = read_descr(&gb, &tag);
431 if (tag == MP4DecConfigDescrTag) {
432 bytestream2_skip(&gb, 13);
433 len = read_descr(&gb, &tag);
435 len = FFMIN(gb.buffer_end - gb.buffer, len);
436 memmove(extradata, gb.buffer, len);
437 avctx->extradata_size = len;
438 break;
439 }
440 } else if (tag == MP4ESDescrTag) {
441 int flags;
442 bytestream2_skip(&gb, 2);
443 flags = bytestream2_get_byte(&gb);
444 if (flags & 0x80) //streamDependenceFlag
445 bytestream2_skip(&gb, 2);
446 if (flags & 0x40) //URL_Flag
447 bytestream2_skip(&gb, bytestream2_get_byte(&gb));
448 if (flags & 0x20) //OCRstreamFlag
449 bytestream2_skip(&gb, 2);
450 }
451 } while (bytestream2_get_bytes_left(&gb));
452 } else if (avctx->codec_id != AV_CODEC_ID_ALAC) {
453 avctx->extradata_size = extradata_size;
454 }
455 }
456
457 ret = ffat_update_ctx(avctx);
458 if (ret < 0)
459 return ret;
460
461#if !TARGET_OS_IPHONE && defined(__MAC_10_9)
462 if (at->mode == kAudioCodecBitRateControlMode_Variable && avctx->rc_max_rate) {
463 UInt32 max_size = avctx->rc_max_rate * avctx->frame_size / avctx->sample_rate;
464 if (max_size)
465 AudioConverterSetProperty(at->converter, kAudioCodecPropertyPacketSizeLimitForVBR,
466 sizeof(max_size), &max_size);
467 }
468#endif
469
470 ff_af_queue_init(avctx, &at->afq);
471
473 if (!at->encoding_frame)
474 return AVERROR(ENOMEM);
475
476 return 0;
477}
478
479static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets,
480 AudioBufferList *data,
481 AudioStreamPacketDescription **packets,
482 void *inctx)
483{
484 AVCodecContext *avctx = inctx;
485 ATDecodeContext *at = avctx->priv_data;
486 AVFrame *frame;
487 int ret;
488
489 if (!at->frame_queue.available) {
490 if (at->eof) {
491 *nb_packets = 0;
492 return 0;
493 } else {
494 *nb_packets = 0;
495 return 1;
496 }
497 }
498
500
501 data->mNumberBuffers = 1;
502 data->mBuffers[0].mNumberChannels = avctx->ch_layout.nb_channels;
503 data->mBuffers[0].mDataByteSize = frame->nb_samples *
505 avctx->ch_layout.nb_channels;
506 data->mBuffers[0].mData = frame->data[0];
507 if (*nb_packets > frame->nb_samples)
508 *nb_packets = frame->nb_samples;
509
511 if (ret < 0) {
512 *nb_packets = 0;
513 return ret;
514 }
515
517
518 return 0;
519}
520
521static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt,
522 const AVFrame *frame, int *got_packet_ptr)
523{
524 ATDecodeContext *at = avctx->priv_data;
525 OSStatus ret;
526
527 AudioBufferList out_buffers = {
528 .mNumberBuffers = 1,
529 .mBuffers = {
530 {
531 .mNumberChannels = avctx->ch_layout.nb_channels,
532 .mDataByteSize = at->pkt_size,
533 }
534 }
535 };
536 AudioStreamPacketDescription out_pkt_desc = {0};
537
538 if (frame) {
539 AVFrame *in_frame;
540
542 /*
543 * The frame queue is significantly larger than needed in practice,
544 * but no clear way to determine the minimum number of samples to
545 * get output from AudioConverterFillComplexBuffer().
546 */
547 av_log(avctx, AV_LOG_ERROR, "Bug: frame queue is too small.\n");
548 return AVERROR_BUG;
549 }
550
551 if ((ret = ff_af_queue_add(&at->afq, frame)) < 0)
552 return ret;
553
554 in_frame = av_frame_clone(frame);
555 if (!in_frame)
556 return AVERROR(ENOMEM);
557
558 ff_bufqueue_add(avctx, &at->frame_queue, in_frame);
559 } else {
560 at->eof = 1;
561 }
562
563 if ((ret = ff_alloc_packet(avctx, avpkt, at->pkt_size)) < 0)
564 return ret;
565
566
567 out_buffers.mBuffers[0].mData = avpkt->data;
568
569 *got_packet_ptr = avctx->frame_size / at->frame_size;
570
571 ret = AudioConverterFillComplexBuffer(at->converter, ffat_encode_callback, avctx,
572 got_packet_ptr, &out_buffers,
573 (avctx->frame_size > at->frame_size) ? NULL : &out_pkt_desc);
574
576
577 if ((!ret || ret == 1) && *got_packet_ptr) {
578 avpkt->size = out_buffers.mBuffers[0].mDataByteSize;
579 avpkt->flags |= AV_PKT_FLAG_KEY;
580 if (at->eof && !at->afq.remaining_samples) {
581 uint8_t *sd = av_packet_new_side_data(avpkt,
583 if (!sd)
584 return AVERROR(ENOMEM);
585 AV_WL32(sd + 4, avctx->frame_size);
586 avpkt->pts = at->end_pts;
587 return 0;
588 }
589 ret = ff_af_queue_remove(&at->afq, out_pkt_desc.mVariableFramesInPacket ?
590 out_pkt_desc.mVariableFramesInPacket :
591 avctx->frame_size,
592 avpkt);
593 if (ret < 0)
594 return ret;
595 at->end_pts = avpkt->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
596 avpkt->pts + avpkt->duration;
597 } else if (ret && ret != 1) {
598 av_log(avctx, AV_LOG_ERROR, "Encode error: %i\n", ret);
599 return AVERROR_EXTERNAL;
600 }
601
602 return 0;
603}
604
606{
607 ATDecodeContext *at = avctx->priv_data;
608 AudioConverterReset(at->converter);
611}
612
614{
615 ATDecodeContext *at = avctx->priv_data;
616 AudioConverterDispose(at->converter);
621 return 0;
622}
623
624static const AVProfile aac_profiles[] = {
625 { AV_PROFILE_AAC_LOW, "LC" },
626 { AV_PROFILE_AAC_HE, "HE-AAC" },
627 { AV_PROFILE_AAC_HE_V2, "HE-AACv2" },
628 { AV_PROFILE_AAC_LD, "LD" },
629 { AV_PROFILE_AAC_ELD, "ELD" },
631};
632
633#define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
634static const AVOption options[] = {
635#if !TARGET_OS_IPHONE
636 {"aac_at_mode", "ratecontrol mode", offsetof(ATDecodeContext, mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable, AE, .unit = "mode"},
637 {"auto", "VBR if global quality is given; CBR otherwise", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, AE, .unit = "mode"},
638 {"cbr", "constant bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX, AE, .unit = "mode"},
639 {"abr", "long-term average bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX, AE, .unit = "mode"},
640 {"cvbr", "constrained variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX, AE, .unit = "mode"},
641 {"vbr" , "variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX, AE, .unit = "mode"},
642#endif
643 {"aac_at_quality", "quality vs speed control", offsetof(ATDecodeContext, quality), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2, AE},
644 { NULL },
645};
646
647#define FFAT_ENC_CLASS(NAME) \
648 static const AVClass ffat_##NAME##_enc_class = { \
649 .class_name = "at_" #NAME "_enc", \
650 .item_name = av_default_item_name, \
651 .option = options, \
652 .version = LIBAVUTIL_VERSION_INT, \
653 };
654
655#define FFAT_ENC(NAME, ID, PROFILES, CAPS, CHANNEL_LAYOUTS, CH_LAYOUTS) \
656 FFAT_ENC_CLASS(NAME) \
657 const FFCodec ff_##NAME##_at_encoder = { \
658 .p.name = #NAME "_at", \
659 CODEC_LONG_NAME(#NAME " (AudioToolbox)"), \
660 .p.type = AVMEDIA_TYPE_AUDIO, \
661 .p.id = ID, \
662 .priv_data_size = sizeof(ATDecodeContext), \
663 .init = ffat_init_encoder, \
664 .close = ffat_close_encoder, \
665 FF_CODEC_ENCODE_CB(ffat_encode), \
666 .flush = ffat_encode_flush, \
667 .p.priv_class = &ffat_##NAME##_enc_class, \
668 .p.capabilities = AV_CODEC_CAP_DELAY | \
669 AV_CODEC_CAP_ENCODER_FLUSH CAPS, \
670 .p.profiles = PROFILES, \
671 .p.wrapper_name = "at", \
672 CODEC_CH_LAYOUTS_ARRAY(CH_LAYOUTS), \
673 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), \
674 };
675
691
692FFAT_ENC(aac, AV_CODEC_ID_AAC, aac_profiles, , aac_at_channel_layouts, aac_at_ch_layouts)
693//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
long long int64_t
Definition coverity.c:34
#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:63
int ret
Definition ffplay.c:391
const char * label
@ 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:340
@ AV_CODEC_ID_ALAC
Definition codec_id.h:475
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition codec_id.h:374
@ AV_CODEC_ID_AAC
Definition codec_id.h:461
@ AV_CODEC_ID_ILBC
Definition codec_id.h:518
@ AV_CODEC_ID_PCM_MULAW
Definition codec_id.h:339
#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
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
#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:379
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:486
#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:109
@ 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
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
const VDPAUPixFmtMap * map
#define AV_WB32(p, v)
#define AV_WL32(p, v)
#define MP4ESDescrTag
Definition isom.h:441
#define MP4DecConfigDescrTag
Definition isom.h:442
#define MP4DecSpecificDescrTag
Definition isom.h:443
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:2190
const char data[16]
Definition mxf.c:155
int profile
Definition mxfenc.c:2405
#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:1059
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1051
int global_quality
Global quality for codecs which cannot change it per frame.
Definition avcodec.h:1240
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int profile
profile
Definition avcodec.h:1646
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1581
int initial_padding
Audio only.
Definition avcodec.h:1118
int sample_rate
samples per second
Definition avcodec.h:1044
int64_t rc_max_rate
maximum bitrate
Definition avcodec.h:1293
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:1079
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1072
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int flags
A combination of AV_PKT_FLAG values.
Definition packet.h:616
int size
Definition packet.h:611
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition packet.h:628
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:603
uint8_t * data
Definition packet.h:610
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:85
#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]