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