]> git.sesse.net Git - ffmpeg/blob - libavcodec/audiotoolboxdec.c
lavc/audiotoolboxdec: support ADTS AAC input
[ffmpeg] / libavcodec / audiotoolboxdec.c
1 /*
2  * Audio Toolbox system codecs
3  *
4  * copyright (c) 2016 Rodger Combs
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <AudioToolbox/AudioToolbox.h>
24
25 #include "config.h"
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "internal.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/log.h"
32
33 typedef struct ATDecodeContext {
34     AVClass *av_class;
35
36     AudioConverterRef converter;
37     AudioStreamPacketDescription pkt_desc;
38     AVPacket in_pkt;
39     AVPacket new_in_pkt;
40     AVBitStreamFilterContext *bsf;
41
42     unsigned pkt_size;
43     int64_t last_pts;
44     int eof;
45 } ATDecodeContext;
46
47 static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
48 {
49     switch (codec) {
50     case AV_CODEC_ID_AAC:
51         return kAudioFormatMPEG4AAC;
52     case AV_CODEC_ID_AC3:
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;
64     case AV_CODEC_ID_MP1:
65         return kAudioFormatMPEGLayer1;
66     case AV_CODEC_ID_MP2:
67         return kAudioFormatMPEGLayer2;
68     case AV_CODEC_ID_MP3:
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;
78     default:
79         av_assert0(!"Invalid codec ID!");
80         return 0;
81     }
82 }
83
84 static void ffat_update_ctx(AVCodecContext *avctx)
85 {
86     ATDecodeContext *at = avctx->priv_data;
87     AudioStreamBasicDescription in_format;
88     UInt32 size = sizeof(in_format);
89     if (!AudioConverterGetProperty(at->converter,
90                                    kAudioConverterCurrentInputStreamDescription,
91                                    &size, &in_format)) {
92         avctx->channels = in_format.mChannelsPerFrame;
93         at->pkt_size = in_format.mFramesPerPacket;
94     }
95
96     if (!at->pkt_size)
97         at->pkt_size = 2048;
98 }
99
100 static void put_descr(PutByteContext *pb, int tag, unsigned int size)
101 {
102     int i = 3;
103     bytestream2_put_byte(pb, tag);
104     for (; i > 0; i--)
105         bytestream2_put_byte(pb, (size >> (7 * i)) | 0x80);
106     bytestream2_put_byte(pb, size & 0x7F);
107 }
108
109 static av_cold int ffat_init_decoder(AVCodecContext *avctx)
110 {
111     ATDecodeContext *at = avctx->priv_data;
112     OSStatus status;
113
114     enum AVSampleFormat sample_fmt = (avctx->bits_per_raw_sample == 32) ?
115                                      AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
116
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,
122     };
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,
130     };
131
132     avctx->sample_fmt = sample_fmt;
133
134     if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_QT)
135         in_format.mFramesPerPacket = 64;
136
137     status = AudioConverterNew(&in_format, &out_format, &at->converter);
138
139     if (status != 0) {
140         av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
141         return AVERROR_UNKNOWN;
142     }
143
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) {
148             PutByteContext pb;
149             extradata_size = 5 + 3 + 5+13 + 5+avctx->extradata_size;
150             if (!(extradata = av_malloc(extradata_size)))
151                 return AVERROR(ENOMEM);
152
153             bytestream2_init_writer(&pb, extradata, extradata_size);
154
155             // ES descriptor
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)
159
160             // DecoderConfig descriptor
161             put_descr(&pb, 0x04, 13 + 5+avctx->extradata_size);
162
163             // Object type indication
164             bytestream2_put_byte(&pb, 0x40);
165
166             bytestream2_put_byte(&pb, 0x15); // flags (= Audiostream)
167
168             bytestream2_put_be24(&pb, 0); // Buffersize DB
169
170             bytestream2_put_be32(&pb, 0); // maxbitrate
171             bytestream2_put_be32(&pb, 0); // avgbitrate
172
173             // DecoderSpecific info descriptor
174             put_descr(&pb, 0x05, avctx->extradata_size);
175             bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
176         }
177
178         status = AudioConverterSetProperty(at->converter,
179                                            kAudioConverterDecompressionMagicCookie,
180                                            extradata_size, extradata);
181         if (status != 0)
182             av_log(avctx, AV_LOG_WARNING, "AudioToolbox cookie error: %i\n", (int)status);
183     }
184
185     ffat_update_ctx(avctx);
186
187     at->last_pts = AV_NOPTS_VALUE;
188
189     return 0;
190 }
191
192 static OSStatus ffat_decode_callback(AudioConverterRef converter, UInt32 *nb_packets,
193                                      AudioBufferList *data,
194                                      AudioStreamPacketDescription **packets,
195                                      void *inctx)
196 {
197     AVCodecContext *avctx = inctx;
198     ATDecodeContext *at = avctx->priv_data;
199
200     if (at->eof) {
201         *nb_packets = 0;
202         if (packets) {
203             *packets = &at->pkt_desc;
204             at->pkt_desc.mDataByteSize = 0;
205         }
206         return 0;
207     }
208
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;
212
213     if (!at->in_pkt.data) {
214         *nb_packets = 0;
215         return 1;
216     }
217
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;
222     *nb_packets = 1;
223
224     if (packets) {
225         *packets = &at->pkt_desc;
226         at->pkt_desc.mDataByteSize = at->in_pkt.size;
227     }
228
229     return 0;
230 }
231
232 static int ffat_decode(AVCodecContext *avctx, void *data,
233                        int *got_frame_ptr, AVPacket *avpkt)
234 {
235     ATDecodeContext *at = avctx->priv_data;
236     AVFrame *frame = data;
237     int pkt_size = avpkt->size;
238     AVPacket filtered_packet;
239     OSStatus ret;
240
241     AudioBufferList out_buffers = {
242         .mNumberBuffers = 1,
243         .mBuffers = {
244             {
245                 .mNumberChannels = avctx->channels,
246                 .mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * at->pkt_size * avctx->channels,
247             }
248         }
249     };
250
251     if (avctx->codec_id == AV_CODEC_ID_AAC && avpkt->size > 2 &&
252         (AV_RB16(avpkt->data) & 0xfff0) == 0xfff0) {
253         int first = 0;
254         uint8_t *p_filtered = NULL;
255         int      n_filtered = 0;
256         if (!at->bsf) {
257             first = 1;
258             if(!(at->bsf = av_bitstream_filter_init("aac_adtstoasc")))
259                 return AVERROR(ENOMEM);
260         }
261
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;
269         }
270
271         if (first) {
272             if ((ret = ffat_set_extradata(avctx)) < 0)
273                 return ret;
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;
277         }
278     }
279
280     av_packet_unref(&at->new_in_pkt);
281
282     if (avpkt->size) {
283         if ((ret = av_packet_ref(&at->new_in_pkt, avpkt)) < 0)
284             return ret;
285         at->new_in_pkt.data = avpkt->data;
286     } else {
287         at->eof = 1;
288     }
289
290     frame->sample_rate = avctx->sample_rate;
291
292     frame->nb_samples = at->pkt_size;
293     ff_get_buffer(avctx, frame, 0);
294
295     out_buffers.mBuffers[0].mData = frame->data[0];
296
297     ret = AudioConverterFillComplexBuffer(at->converter, ffat_decode_callback, avctx,
298                                           &frame->nb_samples, &out_buffers, NULL);
299     if ((!ret || ret == 1) && frame->nb_samples) {
300         *got_frame_ptr = 1;
301         if (at->last_pts != AV_NOPTS_VALUE) {
302             frame->pts = at->last_pts;
303             at->last_pts = avpkt->pts;
304         }
305     } else if (ret && ret != 1) {
306         av_log(avctx, AV_LOG_WARNING, "Decode error: %i\n", ret);
307     } else {
308         at->last_pts = avpkt->pts;
309     }
310
311     return pkt_size;
312 }
313
314 static av_cold void ffat_decode_flush(AVCodecContext *avctx)
315 {
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);
320 }
321
322 static av_cold int ffat_close_decoder(AVCodecContext *avctx)
323 {
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);
328     return 0;
329 }
330
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, \
335     };
336
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, \
343         .id             = ID, \
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, \
352     };
353
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)