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"
35
36 #if __MAC_OS_X_VERSION_MIN_REQUIRED < 101100
37 #define kAudioFormatEnhancedAC3 'ec-3'
38 #endif
39
42
49
52
56
58 {
59 switch (codec) {
61 return kAudioFormatMPEG4AAC;
63 return kAudioFormatAC3;
65 return kAudioFormatAppleIMA4;
67 return kAudioFormatAppleLossless;
69 return kAudioFormatAMR;
73 return kAudioFormatMicrosoftGSM;
75 return kAudioFormatiLBC;
77 return kAudioFormatMPEGLayer1;
79 return kAudioFormatMPEGLayer2;
81 return kAudioFormatMPEGLayer3;
83 return kAudioFormatALaw;
85 return kAudioFormatULaw;
87 return kAudioFormatQDesign;
89 return kAudioFormatQDesign2;
90 default:
92 return 0;
93 }
94 }
95
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)
118 else if (label == kAudioChannelLabel_Mono)
120 else
121 return -1;
122 }
123
125 {
126 const AudioChannelDescription* da =
a;
127 const AudioChannelDescription* db =
b;
129 }
130
132 {
133 AudioChannelLayoutTag
tag =
layout->mChannelLayoutTag;
134 AudioChannelLayout *new_layout;
135 if (
tag == kAudioChannelLayoutTag_UseChannelDescriptions)
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);
144 if (!new_layout) {
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;
156 return new_layout;
157 }
158
160 {
162 AudioStreamBasicDescription
format;
164 if (!AudioConverterGetProperty(at->
converter,
165 kAudioConverterCurrentInputStreamDescription,
172 }
173
174 if (!AudioConverterGetProperty(at->
converter,
175 kAudioConverterCurrentOutputStreamDescription,
180 kAudioConverterCurrentOutputStreamDescription,
182 }
183
184 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterOutputChannelLayout,
187 uint64_t layout_mask = 0;
191 AudioConverterGetProperty(at->
converter, kAudioConverterOutputChannelLayout,
195 for (
i = 0;
i <
layout->mNumberChannelDescriptions;
i++) {
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 }
205 qsort(
layout->mChannelDescriptions,
layout->mNumberChannelDescriptions,
207 for (
i = 0;
i <
layout->mNumberChannelDescriptions;
i++)
209 done:
211 }
212
215
216 return 0;
217 }
218
220 {
222 bytestream2_put_byte(pb,
tag);
224 bytestream2_put_byte(pb, (
size >> (7 *
i)) | 0x80);
225 bytestream2_put_byte(pb,
size & 0x7F);
226 }
227
229 {
232 char *extradata;
235 if (!(extradata =
av_malloc(*cookie_size)))
237
239
240 // ES descriptor
242 bytestream2_put_be16(&pb, 0);
243 bytestream2_put_byte(&pb, 0x00); // flags (= no flags)
244
245 // DecoderConfig descriptor
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
261 return extradata;
262 } else {
265 }
266 }
267
269 {
276 }
277
279 {
283 UInt32 cookie_size;
285 if (!cookie)
287
289 kAudioConverterDecompressionMagicCookie,
290 cookie_size, cookie);
293
296 }
297 return 0;
298 }
299
302 {
306
309
310 AudioStreamBasicDescription in_format = {
313 };
314 AudioStreamBasicDescription out_format = {
315 .mFormatID = kAudioFormatLinearPCM,
316 .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
317 .mFramesPerPacket = 1,
319 };
320
322
324 UInt32 format_size = sizeof(in_format);
325 UInt32 cookie_size;
327 if (!cookie)
329 status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo,
330 cookie_size, cookie, &format_size, &in_format);
336 }
337 #if CONFIG_MP1_AT_DECODER || CONFIG_MP2_AT_DECODER || CONFIG_MP3_AT_DECODER
343 int bit_rate;
345 &in_format.mChannelsPerFrame, &avctx->
frame_size,
350 #endif
351 #if CONFIG_AC3_AT_DECODER || CONFIG_EAC3_AT_DECODER
361 in_format.mChannelsPerFrame = hdr.
channels;
364 #endif
365 } else {
368 }
369
370 avctx->
sample_rate = out_format.mSampleRate = in_format.mSampleRate;
371 avctx->
channels = out_format.mChannelsPerFrame = in_format.mChannelsPerFrame;
372
374 in_format.mFramesPerPacket = 64;
375
377
381 }
382
385
388
390
394
396
397 return 0;
398 }
399
401 {
409 }
410
413 else
414 return 0;
415 }
416
418 AudioBufferList *
data,
419 AudioStreamPacketDescription **packets,
420 void *inctx)
421 {
424
426 *nb_packets = 0;
427 if (packets) {
430 }
431 return 0;
432 }
433
436
438 *nb_packets = 0;
439 return 1;
440 }
441
442 data->mNumberBuffers = 1;
443 data->mBuffers[0].mNumberChannels = 0;
446 *nb_packets = 1;
447
448 if (packets) {
451 }
452
453 return 0;
454 }
455
456 #define COPY_SAMPLES(type) \
457 type *in_ptr = (type*)at->decoded_data; \
458 type *end_ptr = in_ptr + frame->nb_samples * avctx->channels; \
459 type *out_ptr = (type*)frame->data[0]; \
460 for (; in_ptr < end_ptr; in_ptr += avctx->channels, out_ptr += avctx->channels) { \
461 int c; \
462 for (c = 0; c < avctx->channels; c++) \
463 out_ptr[c] = in_ptr[at->channel_map[c]]; \
464 }
465
467 {
471 } else {
473 }
474 }
475
477 int *got_frame_ptr,
AVPacket *avpkt)
478 {
481 int pkt_size = avpkt->
size;
483 AudioBufferList out_buffers;
484
487 uint8_t *side_data;
488 size_t side_data_size;
489
491 &side_data_size);
492 if (side_data_size) {
497 memcpy(at->
extradata, side_data, side_data_size);
498 }
499 }
500 }
501
505 }
506 }
507
508 out_buffers = (AudioBufferList){
509 .mNumberBuffers = 1,
510 .mBuffers = {
511 {
515 }
516 }
517 };
518
520
524 }
525 } else {
527 }
528
530
532
534
541 *got_frame_ptr = 1;
545 }
546 }
else if (
ret &&
ret != 1) {
548 } else {
550 }
551
552 return pkt_size;
553 }
554
556 {
561 }
562
564 {
572 return 0;
573 }
574
575 #define FFAT_DEC_CLASS(NAME) \
576 static const AVClass ffat_##NAME##_dec_class = { \
577 .class_name = "at_" #NAME "_dec", \
578 .version = LIBAVUTIL_VERSION_INT, \
579 };
580
581 #define FFAT_DEC(NAME, ID, bsf_name) \
582 FFAT_DEC_CLASS(NAME) \
583 const AVCodec ff_##NAME##_at_decoder = { \
584 .name = #NAME "_at", \
585 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
586 .type = AVMEDIA_TYPE_AUDIO, \
587 .id = ID, \
588 .priv_data_size = sizeof(ATDecodeContext), \
589 .init = ffat_init_decoder, \
590 .close = ffat_close_decoder, \
591 .decode = ffat_decode, \
592 .flush = ffat_decode_flush, \
593 .priv_class = &ffat_##NAME##_dec_class, \
594 .bsfs = bsf_name, \
595 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_CHANNEL_CONF, \
596 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP, \
597 .wrapper_name = "at", \
598 };
599