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;
56 static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
60 return kAudioFormatMPEG4AAC;
62 return kAudioFormatAC3;
63 case AV_CODEC_ID_ADPCM_IMA_QT:
64 return kAudioFormatAppleIMA4;
65 case AV_CODEC_ID_ALAC:
66 return kAudioFormatAppleLossless;
67 case AV_CODEC_ID_AMR_NB:
68 return kAudioFormatAMR;
69 case AV_CODEC_ID_EAC3:
70 return kAudioFormatEnhancedAC3;
71 case AV_CODEC_ID_GSM_MS:
72 return kAudioFormatMicrosoftGSM;
73 case AV_CODEC_ID_ILBC:
74 return kAudioFormatiLBC;
76 return kAudioFormatMPEGLayer1;
78 return kAudioFormatMPEGLayer2;
80 return kAudioFormatMPEGLayer3;
81 case AV_CODEC_ID_PCM_ALAW:
82 return kAudioFormatALaw;
83 case AV_CODEC_ID_PCM_MULAW:
84 return kAudioFormatULaw;
85 case AV_CODEC_ID_QDMC:
86 return kAudioFormatQDesign;
87 case AV_CODEC_ID_QDM2:
88 return kAudioFormatQDesign2;
90 av_assert0(!"Invalid codec ID!");
95 static int ffat_get_channel_id(AudioChannelLabel label)
99 else if (label <= kAudioChannelLabel_LFEScreen)
101 else if (label <= kAudioChannelLabel_RightSurround)
103 else if (label <= kAudioChannelLabel_CenterSurround)
105 else if (label <= kAudioChannelLabel_RightSurroundDirect)
107 else if (label <= kAudioChannelLabel_TopBackRight)
109 else if (label < kAudioChannelLabel_RearSurroundLeft)
111 else if (label <= kAudioChannelLabel_RearSurroundRight)
113 else if (label <= kAudioChannelLabel_RightWide)
115 else if (label == kAudioChannelLabel_LFE2)
116 return ff_ctzll(AV_CH_LOW_FREQUENCY_2);
117 else if (label == kAudioChannelLabel_Mono)
118 return ff_ctzll(AV_CH_FRONT_CENTER);
123 static int ffat_compare_channel_descriptions(const void* a, const void* b)
125 const AudioChannelDescription* da = a;
126 const AudioChannelDescription* db = b;
127 return ffat_get_channel_id(da->mChannelLabel) - ffat_get_channel_id(db->mChannelLabel);
130 static AudioChannelLayout *ffat_convert_layout(AudioChannelLayout *layout, UInt32* size)
132 AudioChannelLayoutTag tag = layout->mChannelLayoutTag;
133 AudioChannelLayout *new_layout;
134 if (tag == kAudioChannelLayoutTag_UseChannelDescriptions)
136 else if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
137 AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForBitmap,
138 sizeof(UInt32), &layout->mChannelBitmap, size);
140 AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForTag,
141 sizeof(AudioChannelLayoutTag), &tag, size);
142 new_layout = av_malloc(*size);
147 if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
148 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForBitmap,
149 sizeof(UInt32), &layout->mChannelBitmap, size, new_layout);
151 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForTag,
152 sizeof(AudioChannelLayoutTag), &tag, size, new_layout);
153 new_layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
158 static int ffat_update_ctx(AVCodecContext *avctx)
160 ATDecodeContext *at = avctx->priv_data;
161 AudioStreamBasicDescription format;
162 UInt32 size = sizeof(format);
163 if (!AudioConverterGetProperty(at->converter,
164 kAudioConverterCurrentInputStreamDescription,
166 if (format.mSampleRate)
167 avctx->sample_rate = format.mSampleRate;
168 avctx->channels = format.mChannelsPerFrame;
169 avctx->channel_layout = av_get_default_channel_layout(avctx->channels);
170 avctx->frame_size = format.mFramesPerPacket;
173 if (!AudioConverterGetProperty(at->converter,
174 kAudioConverterCurrentOutputStreamDescription,
176 format.mSampleRate = avctx->sample_rate;
177 format.mChannelsPerFrame = avctx->channels;
178 AudioConverterSetProperty(at->converter,
179 kAudioConverterCurrentOutputStreamDescription,
183 if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterOutputChannelLayout,
184 &size, NULL) && size) {
185 AudioChannelLayout *layout = av_malloc(size);
186 uint64_t layout_mask = 0;
189 return AVERROR(ENOMEM);
190 AudioConverterGetProperty(at->converter, kAudioConverterOutputChannelLayout,
192 if (!(layout = ffat_convert_layout(layout, &size)))
193 return AVERROR(ENOMEM);
194 for (i = 0; i < layout->mNumberChannelDescriptions; i++) {
195 int id = ffat_get_channel_id(layout->mChannelDescriptions[i].mChannelLabel);
198 if (layout_mask & (1 << id))
200 layout_mask |= 1 << id;
201 layout->mChannelDescriptions[i].mChannelFlags = i; // Abusing flags as index
203 avctx->channel_layout = layout_mask;
204 qsort(layout->mChannelDescriptions, layout->mNumberChannelDescriptions,
205 sizeof(AudioChannelDescription), &ffat_compare_channel_descriptions);
206 for (i = 0; i < layout->mNumberChannelDescriptions; i++)
207 at->channel_map[i] = layout->mChannelDescriptions[i].mChannelFlags;
212 if (!avctx->frame_size)
213 avctx->frame_size = 2048;
218 static void put_descr(PutByteContext *pb, int tag, unsigned int size)
221 bytestream2_put_byte(pb, tag);
223 bytestream2_put_byte(pb, (size >> (7 * i)) | 0x80);
224 bytestream2_put_byte(pb, size & 0x7F);
227 static uint8_t* ffat_get_magic_cookie(AVCodecContext *avctx, UInt32 *cookie_size)
229 ATDecodeContext *at = avctx->priv_data;
230 if (avctx->codec_id == AV_CODEC_ID_AAC) {
233 *cookie_size = 5 + 3 + 5+13 + 5+at->extradata_size;
234 if (!(extradata = av_malloc(*cookie_size)))
237 bytestream2_init_writer(&pb, extradata, *cookie_size);
240 put_descr(&pb, 0x03, 3 + 5+13 + 5+at->extradata_size);
241 bytestream2_put_be16(&pb, 0);
242 bytestream2_put_byte(&pb, 0x00); // flags (= no flags)
244 // DecoderConfig descriptor
245 put_descr(&pb, 0x04, 13 + 5+at->extradata_size);
247 // Object type indication
248 bytestream2_put_byte(&pb, 0x40);
250 bytestream2_put_byte(&pb, 0x15); // flags (= Audiostream)
252 bytestream2_put_be24(&pb, 0); // Buffersize DB
254 bytestream2_put_be32(&pb, 0); // maxbitrate
255 bytestream2_put_be32(&pb, 0); // avgbitrate
257 // DecoderSpecific info descriptor
258 put_descr(&pb, 0x05, at->extradata_size);
259 bytestream2_put_buffer(&pb, at->extradata, at->extradata_size);
262 *cookie_size = at->extradata_size;
263 return at->extradata;
267 static av_cold int ffat_usable_extradata(AVCodecContext *avctx)
269 ATDecodeContext *at = avctx->priv_data;
270 return at->extradata_size &&
271 (avctx->codec_id == AV_CODEC_ID_ALAC ||
272 avctx->codec_id == AV_CODEC_ID_QDM2 ||
273 avctx->codec_id == AV_CODEC_ID_QDMC ||
274 avctx->codec_id == AV_CODEC_ID_AAC);
277 static int ffat_set_extradata(AVCodecContext *avctx)
279 ATDecodeContext *at = avctx->priv_data;
280 if (ffat_usable_extradata(avctx)) {
283 uint8_t *cookie = ffat_get_magic_cookie(avctx, &cookie_size);
285 return AVERROR(ENOMEM);
287 status = AudioConverterSetProperty(at->converter,
288 kAudioConverterDecompressionMagicCookie,
289 cookie_size, cookie);
291 av_log(avctx, AV_LOG_WARNING, "AudioToolbox cookie error: %i\n", (int)status);
293 if (cookie != at->extradata)
299 static av_cold int ffat_create_decoder(AVCodecContext *avctx, AVPacket *pkt)
301 ATDecodeContext *at = avctx->priv_data;
305 enum AVSampleFormat sample_fmt = (avctx->bits_per_raw_sample == 32) ?
306 AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
308 AudioStreamBasicDescription in_format = {
309 .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
310 .mBytesPerPacket = (avctx->codec_id == AV_CODEC_ID_ILBC) ? avctx->block_align : 0,
312 AudioStreamBasicDescription out_format = {
313 .mFormatID = kAudioFormatLinearPCM,
314 .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
315 .mFramesPerPacket = 1,
316 .mBitsPerChannel = av_get_bytes_per_sample(sample_fmt) * 8,
319 avctx->sample_fmt = sample_fmt;
321 if (ffat_usable_extradata(avctx)) {
322 UInt32 format_size = sizeof(in_format);
324 uint8_t *cookie = ffat_get_magic_cookie(avctx, &cookie_size);
326 return AVERROR(ENOMEM);
327 status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo,
328 cookie_size, cookie, &format_size, &in_format);
329 if (cookie != at->extradata)
332 av_log(avctx, AV_LOG_ERROR, "AudioToolbox header-parse error: %i\n", (int)status);
333 return AVERROR_UNKNOWN;
335 #if CONFIG_MP1_AT_DECODER || CONFIG_MP2_AT_DECODER || CONFIG_MP3_AT_DECODER
336 } else if (pkt && pkt->size >= 4 &&
337 (avctx->codec_id == AV_CODEC_ID_MP1 ||
338 avctx->codec_id == AV_CODEC_ID_MP2 ||
339 avctx->codec_id == AV_CODEC_ID_MP3)) {
340 enum AVCodecID codec_id;
342 if (ff_mpa_decode_header(AV_RB32(pkt->data), &avctx->sample_rate,
343 &in_format.mChannelsPerFrame, &avctx->frame_size,
344 &bit_rate, &codec_id) < 0)
345 return AVERROR_INVALIDDATA;
346 avctx->bit_rate = bit_rate;
347 in_format.mSampleRate = avctx->sample_rate;
349 #if CONFIG_AC3_AT_DECODER || CONFIG_EAC3_AT_DECODER
350 } else if (pkt && pkt->size >= 7 &&
351 (avctx->codec_id == AV_CODEC_ID_AC3 ||
352 avctx->codec_id == AV_CODEC_ID_EAC3)) {
353 AC3HeaderInfo hdr, *phdr = &hdr;
355 init_get_bits(&gbc, pkt->data, pkt->size);
356 if (avpriv_ac3_parse_header(&gbc, &phdr) < 0)
357 return AVERROR_INVALIDDATA;
358 in_format.mSampleRate = hdr.sample_rate;
359 in_format.mChannelsPerFrame = hdr.channels;
360 avctx->frame_size = hdr.num_blocks * 256;
361 avctx->bit_rate = hdr.bit_rate;
364 in_format.mSampleRate = avctx->sample_rate ? avctx->sample_rate : 44100;
365 in_format.mChannelsPerFrame = avctx->channels ? avctx->channels : 1;
368 avctx->sample_rate = out_format.mSampleRate = in_format.mSampleRate;
369 avctx->channels = out_format.mChannelsPerFrame = in_format.mChannelsPerFrame;
371 if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_QT)
372 in_format.mFramesPerPacket = 64;
374 status = AudioConverterNew(&in_format, &out_format, &at->converter);
377 av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
378 return AVERROR_UNKNOWN;
381 if ((status = ffat_set_extradata(avctx)) < 0)
384 for (i = 0; i < (sizeof(at->channel_map) / sizeof(at->channel_map[0])); i++)
385 at->channel_map[i] = i;
387 ffat_update_ctx(avctx);
389 if(!(at->decoded_data = av_malloc(av_get_bytes_per_sample(avctx->sample_fmt)
390 * avctx->frame_size * avctx->channels)))
391 return AVERROR(ENOMEM);
393 at->last_pts = AV_NOPTS_VALUE;
398 static av_cold int ffat_init_decoder(AVCodecContext *avctx)
400 ATDecodeContext *at = avctx->priv_data;
401 if (avctx->extradata_size) {
402 at->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
404 return AVERROR(ENOMEM);
405 at->extradata_size = avctx->extradata_size;
406 memcpy(at->extradata, avctx->extradata, avctx->extradata_size);
409 if ((avctx->channels && avctx->sample_rate) || ffat_usable_extradata(avctx))
410 return ffat_create_decoder(avctx, NULL);
415 static OSStatus ffat_decode_callback(AudioConverterRef converter, UInt32 *nb_packets,
416 AudioBufferList *data,
417 AudioStreamPacketDescription **packets,
420 AVCodecContext *avctx = inctx;
421 ATDecodeContext *at = avctx->priv_data;
426 *packets = &at->pkt_desc;
427 at->pkt_desc.mDataByteSize = 0;
432 av_packet_unref(&at->in_pkt);
433 av_packet_move_ref(&at->in_pkt, &at->new_in_pkt);
435 if (!at->in_pkt.data) {
440 data->mNumberBuffers = 1;
441 data->mBuffers[0].mNumberChannels = 0;
442 data->mBuffers[0].mDataByteSize = at->in_pkt.size;
443 data->mBuffers[0].mData = at->in_pkt.data;
447 *packets = &at->pkt_desc;
448 at->pkt_desc.mDataByteSize = at->in_pkt.size;
454 #define COPY_SAMPLES(type) \
455 type *in_ptr = (type*)at->decoded_data; \
456 type *end_ptr = in_ptr + frame->nb_samples * avctx->channels; \
457 type *out_ptr = (type*)frame->data[0]; \
458 for (; in_ptr < end_ptr; in_ptr += avctx->channels, out_ptr += avctx->channels) { \
460 for (c = 0; c < avctx->channels; c++) \
461 out_ptr[c] = in_ptr[at->channel_map[c]]; \
464 static void ffat_copy_samples(AVCodecContext *avctx, AVFrame *frame)
466 ATDecodeContext *at = avctx->priv_data;
467 if (avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
468 COPY_SAMPLES(int32_t);
470 COPY_SAMPLES(int16_t);
474 static int ffat_decode(AVCodecContext *avctx, void *data,
475 int *got_frame_ptr, AVPacket *avpkt)
477 ATDecodeContext *at = avctx->priv_data;
478 AVFrame *frame = data;
479 int pkt_size = avpkt->size;
481 AudioBufferList out_buffers;
483 if (avctx->codec_id == AV_CODEC_ID_AAC) {
484 if (!at->extradata_size) {
486 int side_data_size = 0;
488 side_data = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
490 if (side_data_size) {
491 at->extradata = av_mallocz(side_data_size + AV_INPUT_BUFFER_PADDING_SIZE);
493 return AVERROR(ENOMEM);
494 at->extradata_size = side_data_size;
495 memcpy(at->extradata, side_data, side_data_size);
500 if (!at->converter) {
501 if ((ret = ffat_create_decoder(avctx, avpkt)) < 0) {
506 out_buffers = (AudioBufferList){
510 .mNumberChannels = avctx->channels,
511 .mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->frame_size
517 av_packet_unref(&at->new_in_pkt);
520 if ((ret = av_packet_ref(&at->new_in_pkt, avpkt)) < 0) {
527 frame->sample_rate = avctx->sample_rate;
529 frame->nb_samples = avctx->frame_size;
531 out_buffers.mBuffers[0].mData = at->decoded_data;
533 ret = AudioConverterFillComplexBuffer(at->converter, ffat_decode_callback, avctx,
534 &frame->nb_samples, &out_buffers, NULL);
535 if ((!ret || ret == 1) && frame->nb_samples) {
536 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
538 ffat_copy_samples(avctx, frame);
540 if (at->last_pts != AV_NOPTS_VALUE) {
541 frame->pts = at->last_pts;
543 FF_DISABLE_DEPRECATION_WARNINGS
544 frame->pkt_pts = at->last_pts;
545 FF_ENABLE_DEPRECATION_WARNINGS
547 at->last_pts = avpkt->pts;
549 } else if (ret && ret != 1) {
550 av_log(avctx, AV_LOG_WARNING, "Decode error: %i\n", ret);
552 at->last_pts = avpkt->pts;
558 static av_cold void ffat_decode_flush(AVCodecContext *avctx)
560 ATDecodeContext *at = avctx->priv_data;
561 AudioConverterReset(at->converter);
562 av_packet_unref(&at->new_in_pkt);
563 av_packet_unref(&at->in_pkt);
566 static av_cold int ffat_close_decoder(AVCodecContext *avctx)
568 ATDecodeContext *at = avctx->priv_data;
570 AudioConverterDispose(at->converter);
571 av_packet_unref(&at->new_in_pkt);
572 av_packet_unref(&at->in_pkt);
573 av_freep(&at->decoded_data);
574 av_freep(&at->extradata);
578 #define FFAT_DEC_CLASS(NAME) \
579 static const AVClass ffat_##NAME##_dec_class = { \
580 .class_name = "at_" #NAME "_dec", \
581 .version = LIBAVUTIL_VERSION_INT, \
584 #define FFAT_DEC(NAME, ID, bsf_name) \
585 FFAT_DEC_CLASS(NAME) \
586 AVCodec ff_##NAME##_at_decoder = { \
587 .name = #NAME "_at", \
588 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
589 .type = AVMEDIA_TYPE_AUDIO, \
591 .priv_data_size = sizeof(ATDecodeContext), \
592 .init = ffat_init_decoder, \
593 .close = ffat_close_decoder, \
594 .decode = ffat_decode, \
595 .flush = ffat_decode_flush, \
596 .priv_class = &ffat_##NAME##_dec_class, \
598 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, \
599 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP, \
602 FFAT_DEC(aac, AV_CODEC_ID_AAC, "aac_adtstoasc")
603 FFAT_DEC(ac3, AV_CODEC_ID_AC3, NULL)
604 FFAT_DEC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT, NULL)
605 FFAT_DEC(alac, AV_CODEC_ID_ALAC, NULL)
606 FFAT_DEC(amr_nb, AV_CODEC_ID_AMR_NB, NULL)
607 FFAT_DEC(eac3, AV_CODEC_ID_EAC3, NULL)
608 FFAT_DEC(gsm_ms, AV_CODEC_ID_GSM_MS, NULL)
609 FFAT_DEC(ilbc, AV_CODEC_ID_ILBC, NULL)
610 FFAT_DEC(mp1, AV_CODEC_ID_MP1, NULL)
611 FFAT_DEC(mp2, AV_CODEC_ID_MP2, NULL)
612 FFAT_DEC(mp3, AV_CODEC_ID_MP3, NULL)
613 FFAT_DEC(pcm_alaw, AV_CODEC_ID_PCM_ALAW, NULL)
614 FFAT_DEC(pcm_mulaw, AV_CODEC_ID_PCM_MULAW, NULL)
615 FFAT_DEC(qdmc, AV_CODEC_ID_QDMC, NULL)
616 FFAT_DEC(qdm2, AV_CODEC_ID_QDM2, NULL)