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 "ac3_parser.h"
28 #include "bytestream.h"
30 #include "mpegaudiodecheader.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/log.h"
35 #if __MAC_OS_X_VERSION_MIN_REQUIRED < 101100
36 #define kAudioFormatEnhancedAC3 'ec-3'
39 typedef struct ATDecodeContext {
42 AudioConverterRef converter;
43 AudioStreamPacketDescription pkt_desc;
57 static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
61 return kAudioFormatMPEG4AAC;
63 return kAudioFormatAC3;
64 case AV_CODEC_ID_ADPCM_IMA_QT:
65 return kAudioFormatAppleIMA4;
66 case AV_CODEC_ID_ALAC:
67 return kAudioFormatAppleLossless;
68 case AV_CODEC_ID_AMR_NB:
69 return kAudioFormatAMR;
70 case AV_CODEC_ID_EAC3:
71 return kAudioFormatEnhancedAC3;
72 case AV_CODEC_ID_GSM_MS:
73 return kAudioFormatMicrosoftGSM;
74 case AV_CODEC_ID_ILBC:
75 return kAudioFormatiLBC;
77 return kAudioFormatMPEGLayer1;
79 return kAudioFormatMPEGLayer2;
81 return kAudioFormatMPEGLayer3;
82 case AV_CODEC_ID_PCM_ALAW:
83 return kAudioFormatALaw;
84 case AV_CODEC_ID_PCM_MULAW:
85 return kAudioFormatULaw;
86 case AV_CODEC_ID_QDMC:
87 return kAudioFormatQDesign;
88 case AV_CODEC_ID_QDM2:
89 return kAudioFormatQDesign2;
91 av_assert0(!"Invalid codec ID!");
96 static int ffat_get_channel_id(AudioChannelLabel label)
100 else if (label <= kAudioChannelLabel_LFEScreen)
102 else if (label <= kAudioChannelLabel_RightSurround)
104 else if (label <= kAudioChannelLabel_CenterSurround)
106 else if (label <= kAudioChannelLabel_RightSurroundDirect)
108 else if (label <= kAudioChannelLabel_TopBackRight)
110 else if (label < kAudioChannelLabel_RearSurroundLeft)
112 else if (label <= kAudioChannelLabel_RearSurroundRight)
114 else if (label <= kAudioChannelLabel_RightWide)
116 else if (label == kAudioChannelLabel_LFE2)
117 return ff_ctzll(AV_CH_LOW_FREQUENCY_2);
118 else if (label == kAudioChannelLabel_Mono)
119 return ff_ctzll(AV_CH_FRONT_CENTER);
124 static int ffat_compare_channel_descriptions(const void* a, const void* b)
126 const AudioChannelDescription* da = a;
127 const AudioChannelDescription* db = b;
128 return ffat_get_channel_id(da->mChannelLabel) - ffat_get_channel_id(db->mChannelLabel);
131 static AudioChannelLayout *ffat_convert_layout(AudioChannelLayout *layout, UInt32* size)
133 AudioChannelLayoutTag tag = layout->mChannelLayoutTag;
134 AudioChannelLayout *new_layout;
135 if (tag == kAudioChannelLayoutTag_UseChannelDescriptions)
137 else if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
138 AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForBitmap,
139 sizeof(UInt32), &layout->mChannelBitmap, size);
141 AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForTag,
142 sizeof(AudioChannelLayoutTag), &tag, size);
143 new_layout = av_malloc(*size);
148 if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
149 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForBitmap,
150 sizeof(UInt32), &layout->mChannelBitmap, size, new_layout);
152 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForTag,
153 sizeof(AudioChannelLayoutTag), &tag, size, new_layout);
154 new_layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
159 static int ffat_update_ctx(AVCodecContext *avctx)
161 ATDecodeContext *at = avctx->priv_data;
162 AudioStreamBasicDescription format;
163 UInt32 size = sizeof(format);
164 if (!AudioConverterGetProperty(at->converter,
165 kAudioConverterCurrentInputStreamDescription,
167 if (format.mSampleRate)
168 avctx->sample_rate = format.mSampleRate;
169 avctx->channels = format.mChannelsPerFrame;
170 avctx->channel_layout = av_get_default_channel_layout(avctx->channels);
171 avctx->frame_size = format.mFramesPerPacket;
174 if (!AudioConverterGetProperty(at->converter,
175 kAudioConverterCurrentOutputStreamDescription,
177 format.mSampleRate = avctx->sample_rate;
178 format.mChannelsPerFrame = avctx->channels;
179 AudioConverterSetProperty(at->converter,
180 kAudioConverterCurrentOutputStreamDescription,
184 if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterOutputChannelLayout,
185 &size, NULL) && size) {
186 AudioChannelLayout *layout = av_malloc(size);
187 uint64_t layout_mask = 0;
190 return AVERROR(ENOMEM);
191 AudioConverterGetProperty(at->converter, kAudioConverterOutputChannelLayout,
193 if (!(layout = ffat_convert_layout(layout, &size)))
194 return AVERROR(ENOMEM);
195 for (i = 0; i < layout->mNumberChannelDescriptions; i++) {
196 int id = ffat_get_channel_id(layout->mChannelDescriptions[i].mChannelLabel);
199 if (layout_mask & (1 << id))
201 layout_mask |= 1 << id;
202 layout->mChannelDescriptions[i].mChannelFlags = i; // Abusing flags as index
204 avctx->channel_layout = layout_mask;
205 qsort(layout->mChannelDescriptions, layout->mNumberChannelDescriptions,
206 sizeof(AudioChannelDescription), &ffat_compare_channel_descriptions);
207 for (i = 0; i < layout->mNumberChannelDescriptions; i++)
208 at->channel_map[i] = layout->mChannelDescriptions[i].mChannelFlags;
213 if (!avctx->frame_size)
214 avctx->frame_size = 2048;
219 static void put_descr(PutByteContext *pb, int tag, unsigned int size)
222 bytestream2_put_byte(pb, tag);
224 bytestream2_put_byte(pb, (size >> (7 * i)) | 0x80);
225 bytestream2_put_byte(pb, size & 0x7F);
228 static uint8_t* ffat_get_magic_cookie(AVCodecContext *avctx, UInt32 *cookie_size)
230 ATDecodeContext *at = avctx->priv_data;
231 if (avctx->codec_id == AV_CODEC_ID_AAC) {
234 *cookie_size = 5 + 3 + 5+13 + 5+at->extradata_size;
235 if (!(extradata = av_malloc(*cookie_size)))
238 bytestream2_init_writer(&pb, extradata, *cookie_size);
241 put_descr(&pb, 0x03, 3 + 5+13 + 5+at->extradata_size);
242 bytestream2_put_be16(&pb, 0);
243 bytestream2_put_byte(&pb, 0x00); // flags (= no flags)
245 // DecoderConfig descriptor
246 put_descr(&pb, 0x04, 13 + 5+at->extradata_size);
248 // Object type indication
249 bytestream2_put_byte(&pb, 0x40);
251 bytestream2_put_byte(&pb, 0x15); // flags (= Audiostream)
253 bytestream2_put_be24(&pb, 0); // Buffersize DB
255 bytestream2_put_be32(&pb, 0); // maxbitrate
256 bytestream2_put_be32(&pb, 0); // avgbitrate
258 // DecoderSpecific info descriptor
259 put_descr(&pb, 0x05, at->extradata_size);
260 bytestream2_put_buffer(&pb, at->extradata, at->extradata_size);
263 *cookie_size = at->extradata_size;
264 return at->extradata;
268 static av_cold int ffat_usable_extradata(AVCodecContext *avctx)
270 ATDecodeContext *at = avctx->priv_data;
271 return at->extradata_size &&
272 (avctx->codec_id == AV_CODEC_ID_ALAC ||
273 avctx->codec_id == AV_CODEC_ID_QDM2 ||
274 avctx->codec_id == AV_CODEC_ID_QDMC ||
275 avctx->codec_id == AV_CODEC_ID_AAC);
278 static int ffat_set_extradata(AVCodecContext *avctx)
280 ATDecodeContext *at = avctx->priv_data;
281 if (ffat_usable_extradata(avctx)) {
284 uint8_t *cookie = ffat_get_magic_cookie(avctx, &cookie_size);
286 return AVERROR(ENOMEM);
288 status = AudioConverterSetProperty(at->converter,
289 kAudioConverterDecompressionMagicCookie,
290 cookie_size, cookie);
292 av_log(avctx, AV_LOG_WARNING, "AudioToolbox cookie error: %i\n", (int)status);
294 if (cookie != at->extradata)
300 static av_cold int ffat_create_decoder(AVCodecContext *avctx, AVPacket *pkt)
302 ATDecodeContext *at = avctx->priv_data;
306 enum AVSampleFormat sample_fmt = (avctx->bits_per_raw_sample == 32) ?
307 AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
309 AudioStreamBasicDescription in_format = {
310 .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
311 .mBytesPerPacket = (avctx->codec_id == AV_CODEC_ID_ILBC) ? avctx->block_align : 0,
313 AudioStreamBasicDescription out_format = {
314 .mFormatID = kAudioFormatLinearPCM,
315 .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
316 .mFramesPerPacket = 1,
317 .mBitsPerChannel = av_get_bytes_per_sample(sample_fmt) * 8,
320 avctx->sample_fmt = sample_fmt;
322 if (ffat_usable_extradata(avctx)) {
323 UInt32 format_size = sizeof(in_format);
325 uint8_t *cookie = ffat_get_magic_cookie(avctx, &cookie_size);
327 return AVERROR(ENOMEM);
328 status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo,
329 cookie_size, cookie, &format_size, &in_format);
330 if (cookie != at->extradata)
333 av_log(avctx, AV_LOG_ERROR, "AudioToolbox header-parse error: %i\n", (int)status);
334 return AVERROR_UNKNOWN;
336 #if CONFIG_MP1_AT_DECODER || CONFIG_MP2_AT_DECODER || CONFIG_MP3_AT_DECODER
337 } else if (pkt && pkt->size >= 4 &&
338 (avctx->codec_id == AV_CODEC_ID_MP1 ||
339 avctx->codec_id == AV_CODEC_ID_MP2 ||
340 avctx->codec_id == AV_CODEC_ID_MP3)) {
341 enum AVCodecID codec_id;
343 if (ff_mpa_decode_header(AV_RB32(pkt->data), &avctx->sample_rate,
344 &in_format.mChannelsPerFrame, &avctx->frame_size,
345 &bit_rate, &codec_id) < 0)
346 return AVERROR_INVALIDDATA;
347 avctx->bit_rate = bit_rate;
348 in_format.mSampleRate = avctx->sample_rate;
350 #if CONFIG_AC3_AT_DECODER || CONFIG_EAC3_AT_DECODER
351 } else if (pkt && pkt->size >= 7 &&
352 (avctx->codec_id == AV_CODEC_ID_AC3 ||
353 avctx->codec_id == AV_CODEC_ID_EAC3)) {
354 AC3HeaderInfo hdr, *phdr = &hdr;
356 init_get_bits(&gbc, pkt->data, pkt->size);
357 if (avpriv_ac3_parse_header(&gbc, &phdr) < 0)
358 return AVERROR_INVALIDDATA;
359 in_format.mSampleRate = hdr.sample_rate;
360 in_format.mChannelsPerFrame = hdr.channels;
361 avctx->frame_size = hdr.num_blocks * 256;
362 avctx->bit_rate = hdr.bit_rate;
365 in_format.mSampleRate = avctx->sample_rate ? avctx->sample_rate : 44100;
366 in_format.mChannelsPerFrame = avctx->channels ? avctx->channels : 1;
369 avctx->sample_rate = out_format.mSampleRate = in_format.mSampleRate;
370 avctx->channels = out_format.mChannelsPerFrame = in_format.mChannelsPerFrame;
372 if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_QT)
373 in_format.mFramesPerPacket = 64;
375 status = AudioConverterNew(&in_format, &out_format, &at->converter);
378 av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
379 return AVERROR_UNKNOWN;
382 if ((status = ffat_set_extradata(avctx)) < 0)
385 for (i = 0; i < (sizeof(at->channel_map) / sizeof(at->channel_map[0])); i++)
386 at->channel_map[i] = i;
388 ffat_update_ctx(avctx);
390 if(!(at->decoded_data = av_malloc(av_get_bytes_per_sample(avctx->sample_fmt)
391 * avctx->frame_size * avctx->channels)))
392 return AVERROR(ENOMEM);
394 at->last_pts = AV_NOPTS_VALUE;
399 static av_cold int ffat_init_decoder(AVCodecContext *avctx)
401 ATDecodeContext *at = avctx->priv_data;
402 at->extradata = avctx->extradata;
403 at->extradata_size = avctx->extradata_size;
405 if ((avctx->channels && avctx->sample_rate) || ffat_usable_extradata(avctx))
406 return ffat_create_decoder(avctx, NULL);
411 static OSStatus ffat_decode_callback(AudioConverterRef converter, UInt32 *nb_packets,
412 AudioBufferList *data,
413 AudioStreamPacketDescription **packets,
416 AVCodecContext *avctx = inctx;
417 ATDecodeContext *at = avctx->priv_data;
422 *packets = &at->pkt_desc;
423 at->pkt_desc.mDataByteSize = 0;
428 av_packet_unref(&at->in_pkt);
429 av_packet_move_ref(&at->in_pkt, &at->new_in_pkt);
431 if (!at->in_pkt.data) {
436 data->mNumberBuffers = 1;
437 data->mBuffers[0].mNumberChannels = 0;
438 data->mBuffers[0].mDataByteSize = at->in_pkt.size;
439 data->mBuffers[0].mData = at->in_pkt.data;
443 *packets = &at->pkt_desc;
444 at->pkt_desc.mDataByteSize = at->in_pkt.size;
450 #define COPY_SAMPLES(type) \
451 type *in_ptr = (type*)at->decoded_data; \
452 type *end_ptr = in_ptr + frame->nb_samples * avctx->channels; \
453 type *out_ptr = (type*)frame->data[0]; \
454 for (; in_ptr < end_ptr; in_ptr += avctx->channels, out_ptr += avctx->channels) { \
456 for (c = 0; c < avctx->channels; c++) \
457 out_ptr[c] = in_ptr[at->channel_map[c]]; \
460 static void ffat_copy_samples(AVCodecContext *avctx, AVFrame *frame)
462 ATDecodeContext *at = avctx->priv_data;
463 if (avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
464 COPY_SAMPLES(int32_t);
466 COPY_SAMPLES(int16_t);
470 static int ffat_decode(AVCodecContext *avctx, void *data,
471 int *got_frame_ptr, AVPacket *avpkt)
473 ATDecodeContext *at = avctx->priv_data;
474 AVFrame *frame = data;
475 int pkt_size = avpkt->size;
476 AVPacket filtered_packet = {0};
478 AudioBufferList out_buffers;
480 if (avctx->codec_id == AV_CODEC_ID_AAC && avpkt->size > 2 &&
481 (AV_RB16(avpkt->data) & 0xfff0) == 0xfff0) {
482 AVPacket filter_pkt = {0};
484 const AVBitStreamFilter *bsf = av_bsf_get_by_name("aac_adtstoasc");
486 return AVERROR_BSF_NOT_FOUND;
487 if ((ret = av_bsf_alloc(bsf, &at->bsf)))
489 if (((ret = avcodec_parameters_from_context(at->bsf->par_in, avctx)) < 0) ||
490 ((ret = av_bsf_init(at->bsf)) < 0)) {
491 av_bsf_free(&at->bsf);
496 if ((ret = av_packet_ref(&filter_pkt, avpkt)) < 0)
499 if ((ret = av_bsf_send_packet(at->bsf, &filter_pkt)) < 0) {
500 av_packet_unref(&filter_pkt);
504 if ((ret = av_bsf_receive_packet(at->bsf, &filtered_packet)) < 0)
507 at->extradata = at->bsf->par_out->extradata;
508 at->extradata_size = at->bsf->par_out->extradata_size;
510 avpkt = &filtered_packet;
513 if (!at->converter) {
514 if ((ret = ffat_create_decoder(avctx, avpkt)) < 0) {
515 av_packet_unref(&filtered_packet);
520 out_buffers = (AudioBufferList){
524 .mNumberChannels = avctx->channels,
525 .mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->frame_size
531 av_packet_unref(&at->new_in_pkt);
534 if (filtered_packet.data) {
535 at->new_in_pkt = filtered_packet;
536 } else if ((ret = av_packet_ref(&at->new_in_pkt, avpkt)) < 0) {
543 frame->sample_rate = avctx->sample_rate;
545 frame->nb_samples = avctx->frame_size;
547 out_buffers.mBuffers[0].mData = at->decoded_data;
549 ret = AudioConverterFillComplexBuffer(at->converter, ffat_decode_callback, avctx,
550 &frame->nb_samples, &out_buffers, NULL);
551 if ((!ret || ret == 1) && frame->nb_samples) {
552 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
554 ffat_copy_samples(avctx, frame);
556 if (at->last_pts != AV_NOPTS_VALUE) {
557 frame->pts = at->last_pts;
559 FF_DISABLE_DEPRECATION_WARNINGS
560 frame->pkt_pts = at->last_pts;
561 FF_ENABLE_DEPRECATION_WARNINGS
563 at->last_pts = avpkt->pts;
565 } else if (ret && ret != 1) {
566 av_log(avctx, AV_LOG_WARNING, "Decode error: %i\n", ret);
568 at->last_pts = avpkt->pts;
574 static av_cold void ffat_decode_flush(AVCodecContext *avctx)
576 ATDecodeContext *at = avctx->priv_data;
577 AudioConverterReset(at->converter);
578 av_packet_unref(&at->new_in_pkt);
579 av_packet_unref(&at->in_pkt);
582 static av_cold int ffat_close_decoder(AVCodecContext *avctx)
584 ATDecodeContext *at = avctx->priv_data;
585 AudioConverterDispose(at->converter);
586 av_bsf_free(&at->bsf);
587 av_packet_unref(&at->new_in_pkt);
588 av_packet_unref(&at->in_pkt);
589 av_free(at->decoded_data);
593 #define FFAT_DEC_CLASS(NAME) \
594 static const AVClass ffat_##NAME##_dec_class = { \
595 .class_name = "at_" #NAME "_dec", \
596 .version = LIBAVUTIL_VERSION_INT, \
599 #define FFAT_DEC(NAME, ID) \
600 FFAT_DEC_CLASS(NAME) \
601 AVCodec ff_##NAME##_at_decoder = { \
602 .name = #NAME "_at", \
603 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
604 .type = AVMEDIA_TYPE_AUDIO, \
606 .priv_data_size = sizeof(ATDecodeContext), \
607 .init = ffat_init_decoder, \
608 .close = ffat_close_decoder, \
609 .decode = ffat_decode, \
610 .flush = ffat_decode_flush, \
611 .priv_class = &ffat_##NAME##_dec_class, \
612 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, \
613 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
616 FFAT_DEC(aac, AV_CODEC_ID_AAC)
617 FFAT_DEC(ac3, AV_CODEC_ID_AC3)
618 FFAT_DEC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT)
619 FFAT_DEC(alac, AV_CODEC_ID_ALAC)
620 FFAT_DEC(amr_nb, AV_CODEC_ID_AMR_NB)
621 FFAT_DEC(eac3, AV_CODEC_ID_EAC3)
622 FFAT_DEC(gsm_ms, AV_CODEC_ID_GSM_MS)
623 FFAT_DEC(ilbc, AV_CODEC_ID_ILBC)
624 FFAT_DEC(mp1, AV_CODEC_ID_MP1)
625 FFAT_DEC(mp2, AV_CODEC_ID_MP2)
626 FFAT_DEC(mp3, AV_CODEC_ID_MP3)
627 FFAT_DEC(pcm_alaw, AV_CODEC_ID_PCM_ALAW)
628 FFAT_DEC(pcm_mulaw, AV_CODEC_ID_PCM_MULAW)
629 FFAT_DEC(qdmc, AV_CODEC_ID_QDMC)
630 FFAT_DEC(qdm2, AV_CODEC_ID_QDM2)