]> git.sesse.net Git - ffmpeg/blob - libavcodec/audiotoolboxdec.c
lavc/audiotoolboxdec: add eac3 decoder
[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 #ifndef __MAC_10_11
34 #define kAudioFormatEnhancedAC3 'ec-3'
35 #endif
36
37 typedef struct ATDecodeContext {
38     AVClass *av_class;
39
40     AudioConverterRef converter;
41     AudioStreamPacketDescription pkt_desc;
42     AVPacket in_pkt;
43     AVPacket new_in_pkt;
44     AVBitStreamFilterContext *bsf;
45     char *decoded_data;
46     int channel_map[64];
47
48     int64_t last_pts;
49     int eof;
50 } ATDecodeContext;
51
52 static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
53 {
54     switch (codec) {
55     case AV_CODEC_ID_AAC:
56         return kAudioFormatMPEG4AAC;
57     case AV_CODEC_ID_AC3:
58         return kAudioFormatAC3;
59     case AV_CODEC_ID_ADPCM_IMA_QT:
60         return kAudioFormatAppleIMA4;
61     case AV_CODEC_ID_ALAC:
62         return kAudioFormatAppleLossless;
63     case AV_CODEC_ID_AMR_NB:
64         return kAudioFormatAMR;
65     case AV_CODEC_ID_EAC3:
66         return kAudioFormatEnhancedAC3;
67     case AV_CODEC_ID_GSM_MS:
68         return kAudioFormatMicrosoftGSM;
69     case AV_CODEC_ID_ILBC:
70         return kAudioFormatiLBC;
71     case AV_CODEC_ID_MP1:
72         return kAudioFormatMPEGLayer1;
73     case AV_CODEC_ID_MP2:
74         return kAudioFormatMPEGLayer2;
75     case AV_CODEC_ID_MP3:
76         return kAudioFormatMPEGLayer3;
77     case AV_CODEC_ID_PCM_ALAW:
78         return kAudioFormatALaw;
79     case AV_CODEC_ID_PCM_MULAW:
80         return kAudioFormatULaw;
81     case AV_CODEC_ID_QDMC:
82         return kAudioFormatQDesign;
83     case AV_CODEC_ID_QDM2:
84         return kAudioFormatQDesign2;
85     default:
86         av_assert0(!"Invalid codec ID!");
87         return 0;
88     }
89 }
90
91 static int ffat_get_channel_id(AudioChannelLabel label)
92 {
93     if (label == 0)
94         return -1;
95     else if (label <= kAudioChannelLabel_LFEScreen)
96         return label - 1;
97     else if (label <= kAudioChannelLabel_RightSurround)
98         return label + 4;
99     else if (label <= kAudioChannelLabel_CenterSurround)
100         return label + 1;
101     else if (label <= kAudioChannelLabel_RightSurroundDirect)
102         return label + 23;
103     else if (label <= kAudioChannelLabel_TopBackRight)
104         return label - 1;
105     else if (label < kAudioChannelLabel_RearSurroundLeft)
106         return -1;
107     else if (label <= kAudioChannelLabel_RearSurroundRight)
108         return label - 29;
109     else if (label <= kAudioChannelLabel_RightWide)
110         return label - 4;
111     else if (label == kAudioChannelLabel_LFE2)
112         return ff_ctzll(AV_CH_LOW_FREQUENCY_2);
113     else if (label == kAudioChannelLabel_Mono)
114         return ff_ctzll(AV_CH_FRONT_CENTER);
115     else
116         return -1;
117 }
118
119 static int ffat_compare_channel_descriptions(const void* a, const void* b)
120 {
121     const AudioChannelDescription* da = a;
122     const AudioChannelDescription* db = b;
123     return ffat_get_channel_id(da->mChannelLabel) - ffat_get_channel_id(db->mChannelLabel);
124 }
125
126 static AudioChannelLayout *ffat_convert_layout(AudioChannelLayout *layout, UInt32* size)
127 {
128     AudioChannelLayoutTag tag = layout->mChannelLayoutTag;
129     AudioChannelLayout *new_layout;
130     if (tag == kAudioChannelLayoutTag_UseChannelDescriptions)
131         return layout;
132     else if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
133         AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForBitmap,
134                                    sizeof(UInt32), &layout->mChannelBitmap, size);
135     else
136         AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForTag,
137                                    sizeof(AudioChannelLayoutTag), &tag, size);
138     new_layout = av_malloc(*size);
139     if (!new_layout) {
140         av_free(layout);
141         return NULL;
142     }
143     if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
144         AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForBitmap,
145                                sizeof(UInt32), &layout->mChannelBitmap, size, new_layout);
146     else
147         AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForTag,
148                                sizeof(AudioChannelLayoutTag), &tag, size, new_layout);
149     new_layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
150     av_free(layout);
151     return new_layout;
152 }
153
154 static int ffat_update_ctx(AVCodecContext *avctx)
155 {
156     ATDecodeContext *at = avctx->priv_data;
157     AudioStreamBasicDescription format;
158     UInt32 size = sizeof(format);
159     if (!AudioConverterGetProperty(at->converter,
160                                    kAudioConverterCurrentInputStreamDescription,
161                                    &size, &format)) {
162         if (format.mSampleRate)
163             avctx->sample_rate = format.mSampleRate;
164         avctx->channels = format.mChannelsPerFrame;
165         avctx->channel_layout = av_get_default_channel_layout(avctx->channels);
166         avctx->frame_size = format.mFramesPerPacket;
167     }
168
169     if (!AudioConverterGetProperty(at->converter,
170                                    kAudioConverterCurrentOutputStreamDescription,
171                                    &size, &format)) {
172         format.mSampleRate = avctx->sample_rate;
173         format.mChannelsPerFrame = avctx->channels;
174         AudioConverterSetProperty(at->converter,
175                                   kAudioConverterCurrentOutputStreamDescription,
176                                   size, &format);
177     }
178
179     if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterOutputChannelLayout,
180                                        &size, NULL) && size) {
181         AudioChannelLayout *layout = av_malloc(size);
182         uint64_t layout_mask = 0;
183         int i;
184         if (!layout)
185             return AVERROR(ENOMEM);
186         AudioConverterGetProperty(at->converter, kAudioConverterOutputChannelLayout,
187                                   &size, layout);
188         if (!(layout = ffat_convert_layout(layout, &size)))
189             return AVERROR(ENOMEM);
190         for (i = 0; i < layout->mNumberChannelDescriptions; i++) {
191             int id = ffat_get_channel_id(layout->mChannelDescriptions[i].mChannelLabel);
192             if (id < 0)
193                 goto done;
194             if (layout_mask & (1 << id))
195                 goto done;
196             layout_mask |= 1 << id;
197             layout->mChannelDescriptions[i].mChannelFlags = i; // Abusing flags as index
198         }
199         avctx->channel_layout = layout_mask;
200         qsort(layout->mChannelDescriptions, layout->mNumberChannelDescriptions,
201               sizeof(AudioChannelDescription), &ffat_compare_channel_descriptions);
202         for (i = 0; i < layout->mNumberChannelDescriptions; i++)
203             at->channel_map[i] = layout->mChannelDescriptions[i].mChannelFlags;
204 done:
205         av_free(layout);
206     }
207
208     if (!avctx->frame_size)
209         avctx->frame_size = 2048;
210
211     return 0;
212 }
213
214 static void put_descr(PutByteContext *pb, int tag, unsigned int size)
215 {
216     int i = 3;
217     bytestream2_put_byte(pb, tag);
218     for (; i > 0; i--)
219         bytestream2_put_byte(pb, (size >> (7 * i)) | 0x80);
220     bytestream2_put_byte(pb, size & 0x7F);
221 }
222
223 static int ffat_set_extradata(AVCodecContext *avctx)
224 {
225     ATDecodeContext *at = avctx->priv_data;
226     if (avctx->extradata_size) {
227         OSStatus status;
228         char *extradata = avctx->extradata;
229         int extradata_size = avctx->extradata_size;
230         if (avctx->codec_id == AV_CODEC_ID_AAC) {
231             PutByteContext pb;
232             extradata_size = 5 + 3 + 5+13 + 5+avctx->extradata_size;
233             if (!(extradata = av_malloc(extradata_size)))
234                 return AVERROR(ENOMEM);
235
236             bytestream2_init_writer(&pb, extradata, extradata_size);
237
238             // ES descriptor
239             put_descr(&pb, 0x03, 3 + 5+13 + 5+avctx->extradata_size);
240             bytestream2_put_be16(&pb, 0);
241             bytestream2_put_byte(&pb, 0x00); // flags (= no flags)
242
243             // DecoderConfig descriptor
244             put_descr(&pb, 0x04, 13 + 5+avctx->extradata_size);
245
246             // Object type indication
247             bytestream2_put_byte(&pb, 0x40);
248
249             bytestream2_put_byte(&pb, 0x15); // flags (= Audiostream)
250
251             bytestream2_put_be24(&pb, 0); // Buffersize DB
252
253             bytestream2_put_be32(&pb, 0); // maxbitrate
254             bytestream2_put_be32(&pb, 0); // avgbitrate
255
256             // DecoderSpecific info descriptor
257             put_descr(&pb, 0x05, avctx->extradata_size);
258             bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
259         }
260
261         status = AudioConverterSetProperty(at->converter,
262                                            kAudioConverterDecompressionMagicCookie,
263                                            extradata_size, extradata);
264         if (status != 0)
265             av_log(avctx, AV_LOG_WARNING, "AudioToolbox cookie error: %i\n", (int)status);
266
267         if (avctx->codec_id == AV_CODEC_ID_AAC)
268             av_free(extradata);
269     }
270     return 0;
271 }
272
273 static av_cold int ffat_create_decoder(AVCodecContext *avctx)
274 {
275     ATDecodeContext *at = avctx->priv_data;
276     OSStatus status;
277     int i;
278
279     enum AVSampleFormat sample_fmt = (avctx->bits_per_raw_sample == 32) ?
280                                      AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
281
282     AudioStreamBasicDescription in_format = {
283         .mSampleRate = avctx->sample_rate ? avctx->sample_rate : 44100,
284         .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
285         .mBytesPerPacket = avctx->block_align,
286         .mChannelsPerFrame = avctx->channels ? avctx->channels : 1,
287     };
288     AudioStreamBasicDescription out_format = {
289         .mSampleRate = in_format.mSampleRate,
290         .mFormatID = kAudioFormatLinearPCM,
291         .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
292         .mFramesPerPacket = 1,
293         .mChannelsPerFrame = in_format.mChannelsPerFrame,
294         .mBitsPerChannel = av_get_bytes_per_sample(sample_fmt) * 8,
295     };
296
297     avctx->sample_fmt = sample_fmt;
298
299     if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_QT)
300         in_format.mFramesPerPacket = 64;
301
302     status = AudioConverterNew(&in_format, &out_format, &at->converter);
303
304     if (status != 0) {
305         av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
306         return AVERROR_UNKNOWN;
307     }
308
309     if ((status = ffat_set_extradata(avctx)) < 0)
310         return status;
311
312     for (i = 0; i < (sizeof(at->channel_map) / sizeof(at->channel_map[0])); i++)
313         at->channel_map[i] = i;
314
315     ffat_update_ctx(avctx);
316
317     if(!(at->decoded_data = av_malloc(av_get_bytes_per_sample(avctx->sample_fmt)
318                                       * avctx->frame_size * avctx->channels)))
319         return AVERROR(ENOMEM);
320
321     at->last_pts = AV_NOPTS_VALUE;
322
323     return 0;
324 }
325
326 static av_cold int ffat_init_decoder(AVCodecContext *avctx)
327 {
328     if (avctx->channels || avctx->extradata_size)
329         return ffat_create_decoder(avctx);
330     else
331         return 0;
332 }
333
334 static OSStatus ffat_decode_callback(AudioConverterRef converter, UInt32 *nb_packets,
335                                      AudioBufferList *data,
336                                      AudioStreamPacketDescription **packets,
337                                      void *inctx)
338 {
339     AVCodecContext *avctx = inctx;
340     ATDecodeContext *at = avctx->priv_data;
341
342     if (at->eof) {
343         *nb_packets = 0;
344         if (packets) {
345             *packets = &at->pkt_desc;
346             at->pkt_desc.mDataByteSize = 0;
347         }
348         return 0;
349     }
350
351     av_packet_move_ref(&at->in_pkt, &at->new_in_pkt);
352     at->new_in_pkt.data = 0;
353     at->new_in_pkt.size = 0;
354
355     if (!at->in_pkt.data) {
356         *nb_packets = 0;
357         return 1;
358     }
359
360     data->mNumberBuffers              = 1;
361     data->mBuffers[0].mNumberChannels = 0;
362     data->mBuffers[0].mDataByteSize   = at->in_pkt.size;
363     data->mBuffers[0].mData           = at->in_pkt.data;
364     *nb_packets = 1;
365
366     if (packets) {
367         *packets = &at->pkt_desc;
368         at->pkt_desc.mDataByteSize = at->in_pkt.size;
369     }
370
371     return 0;
372 }
373
374 #define COPY_SAMPLES(type) \
375     type *in_ptr = (type*)at->decoded_data; \
376     type *end_ptr = in_ptr + frame->nb_samples * avctx->channels; \
377     type *out_ptr = (type*)frame->data[0]; \
378     for (; in_ptr < end_ptr; in_ptr += avctx->channels, out_ptr += avctx->channels) { \
379         int c; \
380         for (c = 0; c < avctx->channels; c++) \
381             out_ptr[c] = in_ptr[at->channel_map[c]]; \
382     }
383
384 static void ffat_copy_samples(AVCodecContext *avctx, AVFrame *frame)
385 {
386     ATDecodeContext *at = avctx->priv_data;
387     if (avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
388         COPY_SAMPLES(int32_t);
389     } else {
390         COPY_SAMPLES(int16_t);
391     }
392 }
393
394 static int ffat_decode(AVCodecContext *avctx, void *data,
395                        int *got_frame_ptr, AVPacket *avpkt)
396 {
397     ATDecodeContext *at = avctx->priv_data;
398     AVFrame *frame = data;
399     int pkt_size = avpkt->size;
400     AVPacket filtered_packet;
401     OSStatus ret;
402     AudioBufferList out_buffers;
403
404     if (avctx->codec_id == AV_CODEC_ID_AAC && avpkt->size > 2 &&
405         (AV_RB16(avpkt->data) & 0xfff0) == 0xfff0) {
406         uint8_t *p_filtered = NULL;
407         int      n_filtered = 0;
408         if (!at->bsf) {
409             if(!(at->bsf = av_bitstream_filter_init("aac_adtstoasc")))
410                 return AVERROR(ENOMEM);
411         }
412
413         ret = av_bitstream_filter_filter(at->bsf, avctx, NULL, &p_filtered, &n_filtered,
414                                          avpkt->data, avpkt->size, 0);
415         if (ret >= 0 && p_filtered != avpkt->data) {
416             filtered_packet = *avpkt;
417             avpkt = &filtered_packet;
418             avpkt->data = p_filtered;
419             avpkt->size = n_filtered;
420         }
421     }
422
423     if (!at->converter) {
424         if ((ret = ffat_create_decoder(avctx)) < 0)
425             return ret;
426     }
427
428     out_buffers = (AudioBufferList){
429         .mNumberBuffers = 1,
430         .mBuffers = {
431             {
432                 .mNumberChannels = avctx->channels,
433                 .mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->frame_size
434                                  * avctx->channels,
435             }
436         }
437     };
438
439     av_packet_unref(&at->new_in_pkt);
440
441     if (avpkt->size) {
442         if ((ret = av_packet_ref(&at->new_in_pkt, avpkt)) < 0)
443             return ret;
444         at->new_in_pkt.data = avpkt->data;
445     } else {
446         at->eof = 1;
447     }
448
449     frame->sample_rate = avctx->sample_rate;
450
451     frame->nb_samples = avctx->frame_size;
452
453     out_buffers.mBuffers[0].mData = at->decoded_data;
454
455     ret = AudioConverterFillComplexBuffer(at->converter, ffat_decode_callback, avctx,
456                                           &frame->nb_samples, &out_buffers, NULL);
457     if ((!ret || ret == 1) && frame->nb_samples) {
458         if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
459             return ret;
460         ffat_copy_samples(avctx, frame);
461         *got_frame_ptr = 1;
462         if (at->last_pts != AV_NOPTS_VALUE) {
463             frame->pkt_pts = at->last_pts;
464             at->last_pts = avpkt->pts;
465         }
466     } else if (ret && ret != 1) {
467         av_log(avctx, AV_LOG_WARNING, "Decode error: %i\n", ret);
468     } else {
469         at->last_pts = avpkt->pts;
470     }
471
472     return pkt_size;
473 }
474
475 static av_cold void ffat_decode_flush(AVCodecContext *avctx)
476 {
477     ATDecodeContext *at = avctx->priv_data;
478     AudioConverterReset(at->converter);
479     av_packet_unref(&at->new_in_pkt);
480     av_packet_unref(&at->in_pkt);
481 }
482
483 static av_cold int ffat_close_decoder(AVCodecContext *avctx)
484 {
485     ATDecodeContext *at = avctx->priv_data;
486     AudioConverterDispose(at->converter);
487     av_packet_unref(&at->new_in_pkt);
488     av_packet_unref(&at->in_pkt);
489     av_free(at->decoded_data);
490     return 0;
491 }
492
493 #define FFAT_DEC_CLASS(NAME) \
494     static const AVClass ffat_##NAME##_dec_class = { \
495         .class_name = "at_" #NAME "_dec", \
496         .version    = LIBAVUTIL_VERSION_INT, \
497     };
498
499 #define FFAT_DEC(NAME, ID) \
500     FFAT_DEC_CLASS(NAME) \
501     AVCodec ff_##NAME##_at_decoder = { \
502         .name           = #NAME "_at", \
503         .long_name      = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
504         .type           = AVMEDIA_TYPE_AUDIO, \
505         .id             = ID, \
506         .priv_data_size = sizeof(ATDecodeContext), \
507         .init           = ffat_init_decoder, \
508         .close          = ffat_close_decoder, \
509         .decode         = ffat_decode, \
510         .flush          = ffat_decode_flush, \
511         .priv_class     = &ffat_##NAME##_dec_class, \
512         .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, \
513         .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE, \
514     };
515
516 FFAT_DEC(aac,          AV_CODEC_ID_AAC)
517 FFAT_DEC(ac3,          AV_CODEC_ID_AC3)
518 FFAT_DEC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT)
519 FFAT_DEC(alac,         AV_CODEC_ID_ALAC)
520 FFAT_DEC(amr_nb,       AV_CODEC_ID_AMR_NB)
521 FFAT_DEC(eac3,         AV_CODEC_ID_EAC3)
522 FFAT_DEC(gsm_ms,       AV_CODEC_ID_GSM_MS)
523 FFAT_DEC(ilbc,         AV_CODEC_ID_ILBC)
524 FFAT_DEC(mp1,          AV_CODEC_ID_MP1)
525 FFAT_DEC(mp2,          AV_CODEC_ID_MP2)
526 FFAT_DEC(mp3,          AV_CODEC_ID_MP3)
527 FFAT_DEC(pcm_alaw,     AV_CODEC_ID_PCM_ALAW)
528 FFAT_DEC(pcm_mulaw,    AV_CODEC_ID_PCM_MULAW)
529 FFAT_DEC(qdmc,         AV_CODEC_ID_QDMC)
530 FFAT_DEC(qdm2,         AV_CODEC_ID_QDM2)