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"
37
38 #if __MAC_OS_X_VERSION_MIN_REQUIRED < 101100
39 #define kAudioFormatEnhancedAC3 'ec-3'
40 #endif
41
44
51
54
58
60 {
61 switch (codec) {
63 return kAudioFormatMPEG4AAC;
65 return kAudioFormatAC3;
67 return kAudioFormatAppleIMA4;
69 return kAudioFormatAppleLossless;
71 return kAudioFormatAMR;
75 return kAudioFormatMicrosoftGSM;
77 return kAudioFormatiLBC;
79 return kAudioFormatMPEGLayer1;
81 return kAudioFormatMPEGLayer2;
83 return kAudioFormatMPEGLayer3;
85 return kAudioFormatALaw;
87 return kAudioFormatULaw;
89 return kAudioFormatQDesign;
91 return kAudioFormatQDesign2;
92 default:
94 return 0;
95 }
96 }
97
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)
120 else if (label == kAudioChannelLabel_Mono)
122 else
123 return -1;
124 }
125
127 {
128 const AudioChannelDescription* da =
a;
129 const AudioChannelDescription* db =
b;
131 }
132
134 {
135 AudioChannelLayoutTag
tag =
layout->mChannelLayoutTag;
136 AudioChannelLayout *new_layout;
137 if (
tag == kAudioChannelLayoutTag_UseChannelDescriptions)
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);
146 if (!new_layout) {
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;
158 return new_layout;
159 }
160
162 {
164 AudioStreamBasicDescription
format;
166 if (!AudioConverterGetProperty(at->
converter,
167 kAudioConverterCurrentInputStreamDescription,
174 }
175
176 if (!AudioConverterGetProperty(at->
converter,
177 kAudioConverterCurrentOutputStreamDescription,
182 kAudioConverterCurrentOutputStreamDescription,
184 }
185
186 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterOutputChannelLayout,
189 uint64_t layout_mask = 0;
193 AudioConverterGetProperty(at->
converter, kAudioConverterOutputChannelLayout,
197 for (
i = 0;
i <
layout->mNumberChannelDescriptions;
i++) {
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 }
208 qsort(
layout->mChannelDescriptions,
layout->mNumberChannelDescriptions,
210 for (
i = 0;
i <
layout->mNumberChannelDescriptions;
i++)
212 done:
214 }
215
218
219 return 0;
220 }
221
223 {
225 bytestream2_put_byte(pb,
tag);
227 bytestream2_put_byte(pb, (
size >> (7 *
i)) | 0x80);
228 bytestream2_put_byte(pb,
size & 0x7F);
229 }
230
232 {
235 char *extradata;
238 if (!(extradata =
av_malloc(*cookie_size)))
240
242
243 // ES descriptor
245 bytestream2_put_be16(&pb, 0);
246 bytestream2_put_byte(&pb, 0x00); // flags (= no flags)
247
248 // DecoderConfig descriptor
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
264 return extradata;
265 } else {
268 }
269 }
270
272 {
279 }
280
282 {
286 UInt32 cookie_size;
288 if (!cookie)
290
292 kAudioConverterDecompressionMagicCookie,
293 cookie_size, cookie);
296
299 }
300 return 0;
301 }
302
305 {
309
312
313 AudioStreamBasicDescription in_format = {
316 };
317 AudioStreamBasicDescription out_format = {
318 .mFormatID = kAudioFormatLinearPCM,
319 .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
320 .mFramesPerPacket = 1,
322 };
323
325
327 UInt32 format_size = sizeof(in_format);
328 UInt32 cookie_size;
330 if (!cookie)
332 status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo,
333 cookie_size, cookie, &format_size, &in_format);
339 }
340 #if CONFIG_MP1_AT_DECODER || CONFIG_MP2_AT_DECODER || CONFIG_MP3_AT_DECODER
346 int bit_rate;
348 &in_format.mChannelsPerFrame, &avctx->
frame_size,
353 #endif
354 #if CONFIG_AC3_AT_DECODER || CONFIG_EAC3_AT_DECODER
364 in_format.mChannelsPerFrame = hdr.
channels;
367 #endif
368 } else {
371 }
372
373 avctx->
sample_rate = out_format.mSampleRate = in_format.mSampleRate;
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
384 in_format.mFramesPerPacket = 64;
385
387
391 }
392
395
398
400
404
406
407 return 0;
408 }
409
411 {
419 }
420
423 else
424 return 0;
425 }
426
428 AudioBufferList *
data,
429 AudioStreamPacketDescription **packets,
430 void *inctx)
431 {
434
436 *nb_packets = 0;
437 if (packets) {
440 }
441 return 0;
442 }
443
446
448 *nb_packets = 0;
449 return 1;
450 }
451
452 data->mNumberBuffers = 1;
453 data->mBuffers[0].mNumberChannels = 0;
456 *nb_packets = 1;
457
458 if (packets) {
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
477 {
481 } else {
483 }
484 }
485
487 int *got_frame_ptr,
AVPacket *avpkt)
488 {
490 int pkt_size = avpkt->
size;
492 AudioBufferList out_buffers;
493
496 uint8_t *side_data;
497 size_t side_data_size;
498
500 &side_data_size);
501 if (side_data_size) {
506 memcpy(at->
extradata, side_data, side_data_size);
507 }
508 }
509 }
510
514 }
515 }
516
517 out_buffers = (AudioBufferList){
518 .mNumberBuffers = 1,
519 .mBuffers = {
520 {
524 }
525 }
526 };
527
529
533 }
534 } else {
536 }
537
539
541
543
550 *got_frame_ptr = 1;
554 }
555 }
else if (
ret &&
ret != 1) {
557 } else {
559 }
560
561 return pkt_size;
562 }
563
565 {
570 }
571
573 {
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 CODEC_LONG_NAME(#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_CLEANUP, \
606 .p.wrapper_name = "at", \
607 };
608