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"
33 typedef struct ATDecodeContext {
36 AudioConverterRef converter;
37 AudioStreamPacketDescription pkt_desc;
40 AVBitStreamFilterContext *bsf;
47 static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
51 return kAudioFormatMPEG4AAC;
53 return kAudioFormatAC3;
54 case AV_CODEC_ID_ADPCM_IMA_QT:
55 return kAudioFormatAppleIMA4;
56 case AV_CODEC_ID_ALAC:
57 return kAudioFormatAppleLossless;
58 case AV_CODEC_ID_AMR_NB:
59 return kAudioFormatAMR;
60 case AV_CODEC_ID_GSM_MS:
61 return kAudioFormatMicrosoftGSM;
62 case AV_CODEC_ID_ILBC:
63 return kAudioFormatiLBC;
65 return kAudioFormatMPEGLayer1;
67 return kAudioFormatMPEGLayer2;
69 return kAudioFormatMPEGLayer3;
70 case AV_CODEC_ID_PCM_ALAW:
71 return kAudioFormatALaw;
72 case AV_CODEC_ID_PCM_MULAW:
73 return kAudioFormatULaw;
74 case AV_CODEC_ID_QDMC:
75 return kAudioFormatQDesign;
76 case AV_CODEC_ID_QDM2:
77 return kAudioFormatQDesign2;
79 av_assert0(!"Invalid codec ID!");
84 static void ffat_update_ctx(AVCodecContext *avctx)
86 ATDecodeContext *at = avctx->priv_data;
87 AudioStreamBasicDescription in_format;
88 UInt32 size = sizeof(in_format);
89 if (!AudioConverterGetProperty(at->converter,
90 kAudioConverterCurrentInputStreamDescription,
92 avctx->channels = in_format.mChannelsPerFrame;
93 at->pkt_size = in_format.mFramesPerPacket;
100 static void put_descr(PutByteContext *pb, int tag, unsigned int size)
103 bytestream2_put_byte(pb, tag);
105 bytestream2_put_byte(pb, (size >> (7 * i)) | 0x80);
106 bytestream2_put_byte(pb, size & 0x7F);
109 static av_cold int ffat_init_decoder(AVCodecContext *avctx)
111 ATDecodeContext *at = avctx->priv_data;
114 enum AVSampleFormat sample_fmt = (avctx->bits_per_raw_sample == 32) ?
115 AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
117 AudioStreamBasicDescription in_format = {
118 .mSampleRate = avctx->sample_rate ? avctx->sample_rate : 44100,
119 .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
120 .mBytesPerPacket = avctx->block_align,
121 .mChannelsPerFrame = avctx->channels ? avctx->channels : 1,
123 AudioStreamBasicDescription out_format = {
124 .mSampleRate = in_format.mSampleRate,
125 .mFormatID = kAudioFormatLinearPCM,
126 .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
127 .mFramesPerPacket = 1,
128 .mChannelsPerFrame = in_format.mChannelsPerFrame,
129 .mBitsPerChannel = av_get_bytes_per_sample(sample_fmt) * 8,
132 avctx->sample_fmt = sample_fmt;
134 if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_QT)
135 in_format.mFramesPerPacket = 64;
137 status = AudioConverterNew(&in_format, &out_format, &at->converter);
140 av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
141 return AVERROR_UNKNOWN;
144 if (avctx->extradata_size) {
145 char *extradata = avctx->extradata;
146 int extradata_size = avctx->extradata_size;
147 if (avctx->codec_id == AV_CODEC_ID_AAC) {
149 extradata_size = 5 + 3 + 5+13 + 5+avctx->extradata_size;
150 if (!(extradata = av_malloc(extradata_size)))
151 return AVERROR(ENOMEM);
153 bytestream2_init_writer(&pb, extradata, extradata_size);
156 put_descr(&pb, 0x03, 3 + 5+13 + 5+avctx->extradata_size);
157 bytestream2_put_be16(&pb, 0);
158 bytestream2_put_byte(&pb, 0x00); // flags (= no flags)
160 // DecoderConfig descriptor
161 put_descr(&pb, 0x04, 13 + 5+avctx->extradata_size);
163 // Object type indication
164 bytestream2_put_byte(&pb, 0x40);
166 bytestream2_put_byte(&pb, 0x15); // flags (= Audiostream)
168 bytestream2_put_be24(&pb, 0); // Buffersize DB
170 bytestream2_put_be32(&pb, 0); // maxbitrate
171 bytestream2_put_be32(&pb, 0); // avgbitrate
173 // DecoderSpecific info descriptor
174 put_descr(&pb, 0x05, avctx->extradata_size);
175 bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
178 status = AudioConverterSetProperty(at->converter,
179 kAudioConverterDecompressionMagicCookie,
180 extradata_size, extradata);
182 av_log(avctx, AV_LOG_WARNING, "AudioToolbox cookie error: %i\n", (int)status);
185 ffat_update_ctx(avctx);
187 at->last_pts = AV_NOPTS_VALUE;
192 static OSStatus ffat_decode_callback(AudioConverterRef converter, UInt32 *nb_packets,
193 AudioBufferList *data,
194 AudioStreamPacketDescription **packets,
197 AVCodecContext *avctx = inctx;
198 ATDecodeContext *at = avctx->priv_data;
203 *packets = &at->pkt_desc;
204 at->pkt_desc.mDataByteSize = 0;
209 av_packet_move_ref(&at->in_pkt, &at->new_in_pkt);
210 at->new_in_pkt.data = 0;
211 at->new_in_pkt.size = 0;
213 if (!at->in_pkt.data) {
218 data->mNumberBuffers = 1;
219 data->mBuffers[0].mNumberChannels = 0;
220 data->mBuffers[0].mDataByteSize = at->in_pkt.size;
221 data->mBuffers[0].mData = at->in_pkt.data;
225 *packets = &at->pkt_desc;
226 at->pkt_desc.mDataByteSize = at->in_pkt.size;
232 static int ffat_decode(AVCodecContext *avctx, void *data,
233 int *got_frame_ptr, AVPacket *avpkt)
235 ATDecodeContext *at = avctx->priv_data;
236 AVFrame *frame = data;
237 int pkt_size = avpkt->size;
238 AVPacket filtered_packet;
241 AudioBufferList out_buffers = {
245 .mNumberChannels = avctx->channels,
246 .mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * at->pkt_size * avctx->channels,
251 if (avctx->codec_id == AV_CODEC_ID_AAC && avpkt->size > 2 &&
252 (AV_RB16(avpkt->data) & 0xfff0) == 0xfff0) {
254 uint8_t *p_filtered = NULL;
258 if(!(at->bsf = av_bitstream_filter_init("aac_adtstoasc")))
259 return AVERROR(ENOMEM);
262 ret = av_bitstream_filter_filter(at->bsf, avctx, NULL, &p_filtered, &n_filtered,
263 avpkt->data, avpkt->size, 0);
264 if (ret >= 0 && p_filtered != avpkt->data) {
265 filtered_packet = *avpkt;
266 avpkt = &filtered_packet;
267 avpkt->data = p_filtered;
268 avpkt->size = n_filtered;
272 if ((ret = ffat_set_extradata(avctx)) < 0)
274 ffat_update_ctx(avctx);
275 out_buffers.mBuffers[0].mNumberChannels = avctx->channels;
276 out_buffers.mBuffers[0].mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * at->pkt_size * avctx->channels;
280 av_packet_unref(&at->new_in_pkt);
283 if ((ret = av_packet_ref(&at->new_in_pkt, avpkt)) < 0)
285 at->new_in_pkt.data = avpkt->data;
290 frame->sample_rate = avctx->sample_rate;
292 frame->nb_samples = at->pkt_size;
293 ff_get_buffer(avctx, frame, 0);
295 out_buffers.mBuffers[0].mData = frame->data[0];
297 ret = AudioConverterFillComplexBuffer(at->converter, ffat_decode_callback, avctx,
298 &frame->nb_samples, &out_buffers, NULL);
299 if ((!ret || ret == 1) && frame->nb_samples) {
301 if (at->last_pts != AV_NOPTS_VALUE) {
302 frame->pts = at->last_pts;
303 at->last_pts = avpkt->pts;
305 } else if (ret && ret != 1) {
306 av_log(avctx, AV_LOG_WARNING, "Decode error: %i\n", ret);
308 at->last_pts = avpkt->pts;
314 static av_cold void ffat_decode_flush(AVCodecContext *avctx)
316 ATDecodeContext *at = avctx->priv_data;
317 AudioConverterReset(at->converter);
318 av_packet_unref(&at->new_in_pkt);
319 av_packet_unref(&at->in_pkt);
322 static av_cold int ffat_close_decoder(AVCodecContext *avctx)
324 ATDecodeContext *at = avctx->priv_data;
325 AudioConverterDispose(at->converter);
326 av_packet_unref(&at->new_in_pkt);
327 av_packet_unref(&at->in_pkt);
331 #define FFAT_DEC_CLASS(NAME) \
332 static const AVClass ffat_##NAME##_dec_class = { \
333 .class_name = "at_" #NAME "_dec", \
334 .version = LIBAVUTIL_VERSION_INT, \
337 #define FFAT_DEC(NAME, ID) \
338 FFAT_DEC_CLASS(NAME) \
339 AVCodec ff_##NAME##_at_decoder = { \
340 .name = #NAME "_at", \
341 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
342 .type = AVMEDIA_TYPE_AUDIO, \
344 .priv_data_size = sizeof(ATDecodeContext), \
345 .init = ffat_init_decoder, \
346 .close = ffat_close_decoder, \
347 .decode = ffat_decode, \
348 .flush = ffat_decode_flush, \
349 .priv_class = &ffat_##NAME##_dec_class, \
350 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, \
351 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
354 FFAT_DEC(aac, AV_CODEC_ID_AAC)
355 FFAT_DEC(ac3, AV_CODEC_ID_AC3)
356 FFAT_DEC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT)
357 FFAT_DEC(alac, AV_CODEC_ID_ALAC)
358 FFAT_DEC(amr_nb, AV_CODEC_ID_AMR_NB)
359 FFAT_DEC(gsm_ms, AV_CODEC_ID_GSM_MS)
360 FFAT_DEC(ilbc, AV_CODEC_ID_ILBC)
361 FFAT_DEC(mp1, AV_CODEC_ID_MP1)
362 FFAT_DEC(mp2, AV_CODEC_ID_MP2)
363 FFAT_DEC(mp3, AV_CODEC_ID_MP3)
364 FFAT_DEC(pcm_alaw, AV_CODEC_ID_PCM_ALAW)
365 FFAT_DEC(pcm_mulaw, AV_CODEC_ID_PCM_MULAW)
366 FFAT_DEC(qdmc, AV_CODEC_ID_QDMC)
367 FFAT_DEC(qdm2, AV_CODEC_ID_QDM2)