2 * Audio Toolbox system codecs
4 * copyright (c) 2016 Rodger Combs
6 * This file is part of FFmpeg.
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.
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.
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
23 #include <AudioToolbox/AudioToolbox.h>
27 #include "bytestream.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/log.h"
34 #define kAudioFormatEnhancedAC3 'ec-3'
37 typedef struct ATDecodeContext {
40 AudioConverterRef converter;
41 AudioStreamPacketDescription pkt_desc;
44 AVBitStreamFilterContext *bsf;
52 static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
56 return kAudioFormatMPEG4AAC;
58 return kAudioFormatAC3;
59 case AV_CODEC_ID_ADPCM_IMA_QT:
60 return kAudioFormatAppleIMA4;
61 case AV_CODEC_ID_ALAC:
62 return kAudioFormatAppleLossless;
63 case AV_CODEC_ID_AMR_NB:
64 return kAudioFormatAMR;
65 case AV_CODEC_ID_EAC3:
66 return kAudioFormatEnhancedAC3;
67 case AV_CODEC_ID_GSM_MS:
68 return kAudioFormatMicrosoftGSM;
69 case AV_CODEC_ID_ILBC:
70 return kAudioFormatiLBC;
72 return kAudioFormatMPEGLayer1;
74 return kAudioFormatMPEGLayer2;
76 return kAudioFormatMPEGLayer3;
77 case AV_CODEC_ID_PCM_ALAW:
78 return kAudioFormatALaw;
79 case AV_CODEC_ID_PCM_MULAW:
80 return kAudioFormatULaw;
81 case AV_CODEC_ID_QDMC:
82 return kAudioFormatQDesign;
83 case AV_CODEC_ID_QDM2:
84 return kAudioFormatQDesign2;
86 av_assert0(!"Invalid codec ID!");
91 static int ffat_get_channel_id(AudioChannelLabel label)
95 else if (label <= kAudioChannelLabel_LFEScreen)
97 else if (label <= kAudioChannelLabel_RightSurround)
99 else if (label <= kAudioChannelLabel_CenterSurround)
101 else if (label <= kAudioChannelLabel_RightSurroundDirect)
103 else if (label <= kAudioChannelLabel_TopBackRight)
105 else if (label < kAudioChannelLabel_RearSurroundLeft)
107 else if (label <= kAudioChannelLabel_RearSurroundRight)
109 else if (label <= kAudioChannelLabel_RightWide)
111 else if (label == kAudioChannelLabel_LFE2)
112 return ff_ctzll(AV_CH_LOW_FREQUENCY_2);
113 else if (label == kAudioChannelLabel_Mono)
114 return ff_ctzll(AV_CH_FRONT_CENTER);
119 static int ffat_compare_channel_descriptions(const void* a, const void* b)
121 const AudioChannelDescription* da = a;
122 const AudioChannelDescription* db = b;
123 return ffat_get_channel_id(da->mChannelLabel) - ffat_get_channel_id(db->mChannelLabel);
126 static AudioChannelLayout *ffat_convert_layout(AudioChannelLayout *layout, UInt32* size)
128 AudioChannelLayoutTag tag = layout->mChannelLayoutTag;
129 AudioChannelLayout *new_layout;
130 if (tag == kAudioChannelLayoutTag_UseChannelDescriptions)
132 else if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
133 AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForBitmap,
134 sizeof(UInt32), &layout->mChannelBitmap, size);
136 AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForTag,
137 sizeof(AudioChannelLayoutTag), &tag, size);
138 new_layout = av_malloc(*size);
143 if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
144 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForBitmap,
145 sizeof(UInt32), &layout->mChannelBitmap, size, new_layout);
147 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForTag,
148 sizeof(AudioChannelLayoutTag), &tag, size, new_layout);
149 new_layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
154 static int ffat_update_ctx(AVCodecContext *avctx)
156 ATDecodeContext *at = avctx->priv_data;
157 AudioStreamBasicDescription format;
158 UInt32 size = sizeof(format);
159 if (!AudioConverterGetProperty(at->converter,
160 kAudioConverterCurrentInputStreamDescription,
162 if (format.mSampleRate)
163 avctx->sample_rate = format.mSampleRate;
164 avctx->channels = format.mChannelsPerFrame;
165 avctx->channel_layout = av_get_default_channel_layout(avctx->channels);
166 avctx->frame_size = format.mFramesPerPacket;
169 if (!AudioConverterGetProperty(at->converter,
170 kAudioConverterCurrentOutputStreamDescription,
172 format.mSampleRate = avctx->sample_rate;
173 format.mChannelsPerFrame = avctx->channels;
174 AudioConverterSetProperty(at->converter,
175 kAudioConverterCurrentOutputStreamDescription,
179 if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterOutputChannelLayout,
180 &size, NULL) && size) {
181 AudioChannelLayout *layout = av_malloc(size);
182 uint64_t layout_mask = 0;
185 return AVERROR(ENOMEM);
186 AudioConverterGetProperty(at->converter, kAudioConverterOutputChannelLayout,
188 if (!(layout = ffat_convert_layout(layout, &size)))
189 return AVERROR(ENOMEM);
190 for (i = 0; i < layout->mNumberChannelDescriptions; i++) {
191 int id = ffat_get_channel_id(layout->mChannelDescriptions[i].mChannelLabel);
194 if (layout_mask & (1 << id))
196 layout_mask |= 1 << id;
197 layout->mChannelDescriptions[i].mChannelFlags = i; // Abusing flags as index
199 avctx->channel_layout = layout_mask;
200 qsort(layout->mChannelDescriptions, layout->mNumberChannelDescriptions,
201 sizeof(AudioChannelDescription), &ffat_compare_channel_descriptions);
202 for (i = 0; i < layout->mNumberChannelDescriptions; i++)
203 at->channel_map[i] = layout->mChannelDescriptions[i].mChannelFlags;
208 if (!avctx->frame_size)
209 avctx->frame_size = 2048;
214 static void put_descr(PutByteContext *pb, int tag, unsigned int size)
217 bytestream2_put_byte(pb, tag);
219 bytestream2_put_byte(pb, (size >> (7 * i)) | 0x80);
220 bytestream2_put_byte(pb, size & 0x7F);
223 static int ffat_set_extradata(AVCodecContext *avctx)
225 ATDecodeContext *at = avctx->priv_data;
226 if (avctx->extradata_size) {
228 char *extradata = avctx->extradata;
229 int extradata_size = avctx->extradata_size;
230 if (avctx->codec_id == AV_CODEC_ID_AAC) {
232 extradata_size = 5 + 3 + 5+13 + 5+avctx->extradata_size;
233 if (!(extradata = av_malloc(extradata_size)))
234 return AVERROR(ENOMEM);
236 bytestream2_init_writer(&pb, extradata, extradata_size);
239 put_descr(&pb, 0x03, 3 + 5+13 + 5+avctx->extradata_size);
240 bytestream2_put_be16(&pb, 0);
241 bytestream2_put_byte(&pb, 0x00); // flags (= no flags)
243 // DecoderConfig descriptor
244 put_descr(&pb, 0x04, 13 + 5+avctx->extradata_size);
246 // Object type indication
247 bytestream2_put_byte(&pb, 0x40);
249 bytestream2_put_byte(&pb, 0x15); // flags (= Audiostream)
251 bytestream2_put_be24(&pb, 0); // Buffersize DB
253 bytestream2_put_be32(&pb, 0); // maxbitrate
254 bytestream2_put_be32(&pb, 0); // avgbitrate
256 // DecoderSpecific info descriptor
257 put_descr(&pb, 0x05, avctx->extradata_size);
258 bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
261 status = AudioConverterSetProperty(at->converter,
262 kAudioConverterDecompressionMagicCookie,
263 extradata_size, extradata);
265 av_log(avctx, AV_LOG_WARNING, "AudioToolbox cookie error: %i\n", (int)status);
267 if (avctx->codec_id == AV_CODEC_ID_AAC)
273 static av_cold int ffat_create_decoder(AVCodecContext *avctx)
275 ATDecodeContext *at = avctx->priv_data;
279 enum AVSampleFormat sample_fmt = (avctx->bits_per_raw_sample == 32) ?
280 AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
282 AudioStreamBasicDescription in_format = {
283 .mSampleRate = avctx->sample_rate ? avctx->sample_rate : 44100,
284 .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
285 .mBytesPerPacket = avctx->block_align,
286 .mChannelsPerFrame = avctx->channels ? avctx->channels : 1,
288 AudioStreamBasicDescription out_format = {
289 .mSampleRate = in_format.mSampleRate,
290 .mFormatID = kAudioFormatLinearPCM,
291 .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
292 .mFramesPerPacket = 1,
293 .mChannelsPerFrame = in_format.mChannelsPerFrame,
294 .mBitsPerChannel = av_get_bytes_per_sample(sample_fmt) * 8,
297 avctx->sample_fmt = sample_fmt;
299 if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_QT)
300 in_format.mFramesPerPacket = 64;
302 status = AudioConverterNew(&in_format, &out_format, &at->converter);
305 av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
306 return AVERROR_UNKNOWN;
309 if ((status = ffat_set_extradata(avctx)) < 0)
312 for (i = 0; i < (sizeof(at->channel_map) / sizeof(at->channel_map[0])); i++)
313 at->channel_map[i] = i;
315 ffat_update_ctx(avctx);
317 if(!(at->decoded_data = av_malloc(av_get_bytes_per_sample(avctx->sample_fmt)
318 * avctx->frame_size * avctx->channels)))
319 return AVERROR(ENOMEM);
321 at->last_pts = AV_NOPTS_VALUE;
326 static av_cold int ffat_init_decoder(AVCodecContext *avctx)
328 if (avctx->channels || avctx->extradata_size)
329 return ffat_create_decoder(avctx);
334 static OSStatus ffat_decode_callback(AudioConverterRef converter, UInt32 *nb_packets,
335 AudioBufferList *data,
336 AudioStreamPacketDescription **packets,
339 AVCodecContext *avctx = inctx;
340 ATDecodeContext *at = avctx->priv_data;
345 *packets = &at->pkt_desc;
346 at->pkt_desc.mDataByteSize = 0;
351 av_packet_move_ref(&at->in_pkt, &at->new_in_pkt);
352 at->new_in_pkt.data = 0;
353 at->new_in_pkt.size = 0;
355 if (!at->in_pkt.data) {
360 data->mNumberBuffers = 1;
361 data->mBuffers[0].mNumberChannels = 0;
362 data->mBuffers[0].mDataByteSize = at->in_pkt.size;
363 data->mBuffers[0].mData = at->in_pkt.data;
367 *packets = &at->pkt_desc;
368 at->pkt_desc.mDataByteSize = at->in_pkt.size;
374 #define COPY_SAMPLES(type) \
375 type *in_ptr = (type*)at->decoded_data; \
376 type *end_ptr = in_ptr + frame->nb_samples * avctx->channels; \
377 type *out_ptr = (type*)frame->data[0]; \
378 for (; in_ptr < end_ptr; in_ptr += avctx->channels, out_ptr += avctx->channels) { \
380 for (c = 0; c < avctx->channels; c++) \
381 out_ptr[c] = in_ptr[at->channel_map[c]]; \
384 static void ffat_copy_samples(AVCodecContext *avctx, AVFrame *frame)
386 ATDecodeContext *at = avctx->priv_data;
387 if (avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
388 COPY_SAMPLES(int32_t);
390 COPY_SAMPLES(int16_t);
394 static int ffat_decode(AVCodecContext *avctx, void *data,
395 int *got_frame_ptr, AVPacket *avpkt)
397 ATDecodeContext *at = avctx->priv_data;
398 AVFrame *frame = data;
399 int pkt_size = avpkt->size;
400 AVPacket filtered_packet;
402 AudioBufferList out_buffers;
404 if (avctx->codec_id == AV_CODEC_ID_AAC && avpkt->size > 2 &&
405 (AV_RB16(avpkt->data) & 0xfff0) == 0xfff0) {
406 uint8_t *p_filtered = NULL;
409 if(!(at->bsf = av_bitstream_filter_init("aac_adtstoasc")))
410 return AVERROR(ENOMEM);
413 ret = av_bitstream_filter_filter(at->bsf, avctx, NULL, &p_filtered, &n_filtered,
414 avpkt->data, avpkt->size, 0);
415 if (ret >= 0 && p_filtered != avpkt->data) {
416 filtered_packet = *avpkt;
417 avpkt = &filtered_packet;
418 avpkt->data = p_filtered;
419 avpkt->size = n_filtered;
423 if (!at->converter) {
424 if ((ret = ffat_create_decoder(avctx)) < 0)
428 out_buffers = (AudioBufferList){
432 .mNumberChannels = avctx->channels,
433 .mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->frame_size
439 av_packet_unref(&at->new_in_pkt);
442 if ((ret = av_packet_ref(&at->new_in_pkt, avpkt)) < 0)
444 at->new_in_pkt.data = avpkt->data;
449 frame->sample_rate = avctx->sample_rate;
451 frame->nb_samples = avctx->frame_size;
453 out_buffers.mBuffers[0].mData = at->decoded_data;
455 ret = AudioConverterFillComplexBuffer(at->converter, ffat_decode_callback, avctx,
456 &frame->nb_samples, &out_buffers, NULL);
457 if ((!ret || ret == 1) && frame->nb_samples) {
458 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
460 ffat_copy_samples(avctx, frame);
462 if (at->last_pts != AV_NOPTS_VALUE) {
463 frame->pkt_pts = at->last_pts;
464 at->last_pts = avpkt->pts;
466 } else if (ret && ret != 1) {
467 av_log(avctx, AV_LOG_WARNING, "Decode error: %i\n", ret);
469 at->last_pts = avpkt->pts;
475 static av_cold void ffat_decode_flush(AVCodecContext *avctx)
477 ATDecodeContext *at = avctx->priv_data;
478 AudioConverterReset(at->converter);
479 av_packet_unref(&at->new_in_pkt);
480 av_packet_unref(&at->in_pkt);
483 static av_cold int ffat_close_decoder(AVCodecContext *avctx)
485 ATDecodeContext *at = avctx->priv_data;
486 AudioConverterDispose(at->converter);
487 av_packet_unref(&at->new_in_pkt);
488 av_packet_unref(&at->in_pkt);
489 av_free(at->decoded_data);
493 #define FFAT_DEC_CLASS(NAME) \
494 static const AVClass ffat_##NAME##_dec_class = { \
495 .class_name = "at_" #NAME "_dec", \
496 .version = LIBAVUTIL_VERSION_INT, \
499 #define FFAT_DEC(NAME, ID) \
500 FFAT_DEC_CLASS(NAME) \
501 AVCodec ff_##NAME##_at_decoder = { \
502 .name = #NAME "_at", \
503 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
504 .type = AVMEDIA_TYPE_AUDIO, \
506 .priv_data_size = sizeof(ATDecodeContext), \
507 .init = ffat_init_decoder, \
508 .close = ffat_close_decoder, \
509 .decode = ffat_decode, \
510 .flush = ffat_decode_flush, \
511 .priv_class = &ffat_##NAME##_dec_class, \
512 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, \
513 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
516 FFAT_DEC(aac, AV_CODEC_ID_AAC)
517 FFAT_DEC(ac3, AV_CODEC_ID_AC3)
518 FFAT_DEC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT)
519 FFAT_DEC(alac, AV_CODEC_ID_ALAC)
520 FFAT_DEC(amr_nb, AV_CODEC_ID_AMR_NB)
521 FFAT_DEC(eac3, AV_CODEC_ID_EAC3)
522 FFAT_DEC(gsm_ms, AV_CODEC_ID_GSM_MS)
523 FFAT_DEC(ilbc, AV_CODEC_ID_ILBC)
524 FFAT_DEC(mp1, AV_CODEC_ID_MP1)
525 FFAT_DEC(mp2, AV_CODEC_ID_MP2)
526 FFAT_DEC(mp3, AV_CODEC_ID_MP3)
527 FFAT_DEC(pcm_alaw, AV_CODEC_ID_PCM_ALAW)
528 FFAT_DEC(pcm_mulaw, AV_CODEC_ID_PCM_MULAW)
529 FFAT_DEC(qdmc, AV_CODEC_ID_QDMC)
530 FFAT_DEC(qdm2, AV_CODEC_ID_QDM2)