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"
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_AAC);
276 static int ffat_set_extradata(AVCodecContext *avctx)
278 ATDecodeContext *at = avctx->priv_data;
279 if (ffat_usable_extradata(avctx)) {
282 uint8_t *cookie = ffat_get_magic_cookie(avctx, &cookie_size);
284 return AVERROR(ENOMEM);
286 status = AudioConverterSetProperty(at->converter,
287 kAudioConverterDecompressionMagicCookie,
288 cookie_size, cookie);
290 av_log(avctx, AV_LOG_WARNING, "AudioToolbox cookie error: %i\n", (int)status);
292 if (cookie != at->extradata)
298 static av_cold int ffat_create_decoder(AVCodecContext *avctx, AVPacket *pkt)
300 ATDecodeContext *at = avctx->priv_data;
304 enum AVSampleFormat sample_fmt = (avctx->bits_per_raw_sample == 32) ?
305 AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
307 AudioStreamBasicDescription in_format = {
308 .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
309 .mBytesPerPacket = (avctx->codec_id == AV_CODEC_ID_ILBC) ? avctx->block_align : 0,
311 AudioStreamBasicDescription out_format = {
312 .mFormatID = kAudioFormatLinearPCM,
313 .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
314 .mFramesPerPacket = 1,
315 .mBitsPerChannel = av_get_bytes_per_sample(sample_fmt) * 8,
318 avctx->sample_fmt = sample_fmt;
320 if (ffat_usable_extradata(avctx)) {
321 UInt32 format_size = sizeof(in_format);
323 uint8_t *cookie = ffat_get_magic_cookie(avctx, &cookie_size);
325 return AVERROR(ENOMEM);
326 status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo,
327 cookie_size, cookie, &format_size, &in_format);
328 if (cookie != at->extradata)
331 av_log(avctx, AV_LOG_ERROR, "AudioToolbox header-parse error: %i\n", (int)status);
332 return AVERROR_UNKNOWN;
334 #if CONFIG_MP1_AT_DECODER || CONFIG_MP2_AT_DECODER || CONFIG_MP3_AT_DECODER
335 } else if (pkt && pkt->size >= 4 &&
336 (avctx->codec_id == AV_CODEC_ID_MP1 ||
337 avctx->codec_id == AV_CODEC_ID_MP2 ||
338 avctx->codec_id == AV_CODEC_ID_MP3)) {
339 enum AVCodecID codec_id;
341 if (ff_mpa_decode_header(AV_RB32(pkt->data), &avctx->sample_rate,
342 &in_format.mChannelsPerFrame, &avctx->frame_size,
343 &bit_rate, &codec_id) < 0)
344 return AVERROR_INVALIDDATA;
345 avctx->bit_rate = bit_rate;
346 in_format.mSampleRate = avctx->sample_rate;
348 #if CONFIG_AC3_AT_DECODER || CONFIG_EAC3_AT_DECODER
349 } else if (pkt && pkt->size >= 7 &&
350 (avctx->codec_id == AV_CODEC_ID_AC3 ||
351 avctx->codec_id == AV_CODEC_ID_EAC3)) {
352 AC3HeaderInfo hdr, *phdr = &hdr;
354 init_get_bits(&gbc, pkt->data, pkt->size);
355 if (avpriv_ac3_parse_header(&gbc, &phdr) < 0)
356 return AVERROR_INVALIDDATA;
357 in_format.mSampleRate = hdr.sample_rate;
358 in_format.mChannelsPerFrame = hdr.channels;
359 avctx->frame_size = hdr.num_blocks * 256;
360 avctx->bit_rate = hdr.bit_rate;
363 in_format.mSampleRate = avctx->sample_rate ? avctx->sample_rate : 44100;
364 in_format.mChannelsPerFrame = avctx->channels ? avctx->channels : 1;
367 avctx->sample_rate = out_format.mSampleRate = in_format.mSampleRate;
368 avctx->channels = out_format.mChannelsPerFrame = in_format.mChannelsPerFrame;
370 if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_QT)
371 in_format.mFramesPerPacket = 64;
373 status = AudioConverterNew(&in_format, &out_format, &at->converter);
376 av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
377 return AVERROR_UNKNOWN;
380 if ((status = ffat_set_extradata(avctx)) < 0)
383 for (i = 0; i < (sizeof(at->channel_map) / sizeof(at->channel_map[0])); i++)
384 at->channel_map[i] = i;
386 ffat_update_ctx(avctx);
388 if(!(at->decoded_data = av_malloc(av_get_bytes_per_sample(avctx->sample_fmt)
389 * avctx->frame_size * avctx->channels)))
390 return AVERROR(ENOMEM);
392 at->last_pts = AV_NOPTS_VALUE;
397 static av_cold int ffat_init_decoder(AVCodecContext *avctx)
399 ATDecodeContext *at = avctx->priv_data;
400 at->extradata = avctx->extradata;
401 at->extradata_size = avctx->extradata_size;
403 if ((avctx->channels && avctx->sample_rate) || ffat_usable_extradata(avctx))
404 return ffat_create_decoder(avctx, NULL);
409 static OSStatus ffat_decode_callback(AudioConverterRef converter, UInt32 *nb_packets,
410 AudioBufferList *data,
411 AudioStreamPacketDescription **packets,
414 AVCodecContext *avctx = inctx;
415 ATDecodeContext *at = avctx->priv_data;
420 *packets = &at->pkt_desc;
421 at->pkt_desc.mDataByteSize = 0;
426 av_packet_unref(&at->in_pkt);
427 av_packet_move_ref(&at->in_pkt, &at->new_in_pkt);
429 if (!at->in_pkt.data) {
434 data->mNumberBuffers = 1;
435 data->mBuffers[0].mNumberChannels = 0;
436 data->mBuffers[0].mDataByteSize = at->in_pkt.size;
437 data->mBuffers[0].mData = at->in_pkt.data;
441 *packets = &at->pkt_desc;
442 at->pkt_desc.mDataByteSize = at->in_pkt.size;
448 #define COPY_SAMPLES(type) \
449 type *in_ptr = (type*)at->decoded_data; \
450 type *end_ptr = in_ptr + frame->nb_samples * avctx->channels; \
451 type *out_ptr = (type*)frame->data[0]; \
452 for (; in_ptr < end_ptr; in_ptr += avctx->channels, out_ptr += avctx->channels) { \
454 for (c = 0; c < avctx->channels; c++) \
455 out_ptr[c] = in_ptr[at->channel_map[c]]; \
458 static void ffat_copy_samples(AVCodecContext *avctx, AVFrame *frame)
460 ATDecodeContext *at = avctx->priv_data;
461 if (avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
462 COPY_SAMPLES(int32_t);
464 COPY_SAMPLES(int16_t);
468 static int ffat_decode(AVCodecContext *avctx, void *data,
469 int *got_frame_ptr, AVPacket *avpkt)
471 ATDecodeContext *at = avctx->priv_data;
472 AVFrame *frame = data;
473 int pkt_size = avpkt->size;
474 AVPacket filtered_packet = {0};
476 AudioBufferList out_buffers;
478 if (avctx->codec_id == AV_CODEC_ID_AAC && avpkt->size > 2 &&
479 (AV_RB16(avpkt->data) & 0xfff0) == 0xfff0) {
480 AVPacket filter_pkt = {0};
482 const AVBitStreamFilter *bsf = av_bsf_get_by_name("aac_adtstoasc");
484 return AVERROR_BSF_NOT_FOUND;
485 if ((ret = av_bsf_alloc(bsf, &at->bsf)))
487 if (((ret = avcodec_parameters_from_context(at->bsf->par_in, avctx)) < 0) ||
488 ((ret = av_bsf_init(at->bsf)) < 0)) {
489 av_bsf_free(&at->bsf);
494 if ((ret = av_packet_ref(&filter_pkt, avpkt)) < 0)
497 if ((ret = av_bsf_send_packet(at->bsf, &filter_pkt)) < 0) {
498 av_packet_unref(&filter_pkt);
502 if ((ret = av_bsf_receive_packet(at->bsf, &filtered_packet)) < 0)
505 at->extradata = at->bsf->par_out->extradata;
506 at->extradata_size = at->bsf->par_out->extradata_size;
508 avpkt = &filtered_packet;
511 if (!at->converter) {
512 if ((ret = ffat_create_decoder(avctx, avpkt)) < 0) {
513 av_packet_unref(&filtered_packet);
518 out_buffers = (AudioBufferList){
522 .mNumberChannels = avctx->channels,
523 .mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->frame_size
529 av_packet_unref(&at->new_in_pkt);
532 if (filtered_packet.data) {
533 at->new_in_pkt = filtered_packet;
534 } else if ((ret = av_packet_ref(&at->new_in_pkt, avpkt)) < 0) {
541 frame->sample_rate = avctx->sample_rate;
543 frame->nb_samples = avctx->frame_size;
545 out_buffers.mBuffers[0].mData = at->decoded_data;
547 ret = AudioConverterFillComplexBuffer(at->converter, ffat_decode_callback, avctx,
548 &frame->nb_samples, &out_buffers, NULL);
549 if ((!ret || ret == 1) && frame->nb_samples) {
550 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
552 ffat_copy_samples(avctx, frame);
554 if (at->last_pts != AV_NOPTS_VALUE) {
555 frame->pkt_pts = at->last_pts;
556 at->last_pts = avpkt->pts;
558 } else if (ret && ret != 1) {
559 av_log(avctx, AV_LOG_WARNING, "Decode error: %i\n", ret);
561 at->last_pts = avpkt->pts;
567 static av_cold void ffat_decode_flush(AVCodecContext *avctx)
569 ATDecodeContext *at = avctx->priv_data;
570 AudioConverterReset(at->converter);
571 av_packet_unref(&at->new_in_pkt);
572 av_packet_unref(&at->in_pkt);
575 static av_cold int ffat_close_decoder(AVCodecContext *avctx)
577 ATDecodeContext *at = avctx->priv_data;
578 AudioConverterDispose(at->converter);
579 av_bsf_free(&at->bsf);
580 av_packet_unref(&at->new_in_pkt);
581 av_packet_unref(&at->in_pkt);
582 av_free(at->decoded_data);
586 #define FFAT_DEC_CLASS(NAME) \
587 static const AVClass ffat_##NAME##_dec_class = { \
588 .class_name = "at_" #NAME "_dec", \
589 .version = LIBAVUTIL_VERSION_INT, \
592 #define FFAT_DEC(NAME, ID) \
593 FFAT_DEC_CLASS(NAME) \
594 AVCodec ff_##NAME##_at_decoder = { \
595 .name = #NAME "_at", \
596 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
597 .type = AVMEDIA_TYPE_AUDIO, \
599 .priv_data_size = sizeof(ATDecodeContext), \
600 .init = ffat_init_decoder, \
601 .close = ffat_close_decoder, \
602 .decode = ffat_decode, \
603 .flush = ffat_decode_flush, \
604 .priv_class = &ffat_##NAME##_dec_class, \
605 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, \
606 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
609 FFAT_DEC(aac, AV_CODEC_ID_AAC)
610 FFAT_DEC(ac3, AV_CODEC_ID_AC3)
611 FFAT_DEC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT)
612 FFAT_DEC(alac, AV_CODEC_ID_ALAC)
613 FFAT_DEC(amr_nb, AV_CODEC_ID_AMR_NB)
614 FFAT_DEC(eac3, AV_CODEC_ID_EAC3)
615 FFAT_DEC(gsm_ms, AV_CODEC_ID_GSM_MS)
616 FFAT_DEC(ilbc, AV_CODEC_ID_ILBC)
617 FFAT_DEC(mp1, AV_CODEC_ID_MP1)
618 FFAT_DEC(mp2, AV_CODEC_ID_MP2)
619 FFAT_DEC(mp3, AV_CODEC_ID_MP3)
620 FFAT_DEC(pcm_alaw, AV_CODEC_ID_PCM_ALAW)
621 FFAT_DEC(pcm_mulaw, AV_CODEC_ID_PCM_MULAW)
622 FFAT_DEC(qdmc, AV_CODEC_ID_QDMC)
623 FFAT_DEC(qdm2, AV_CODEC_ID_QDM2)