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