23 #include <AudioToolbox/AudioToolbox.h>
26 #include "config_components.h"
39 #if __MAC_OS_X_VERSION_MIN_REQUIRED < 101100
40 #define kAudioFormatEnhancedAC3 'ec-3'
64 return kAudioFormatMPEG4AAC;
66 return kAudioFormatAC3;
68 return kAudioFormatAppleIMA4;
70 return kAudioFormatAppleLossless;
72 return kAudioFormatAMR;
75 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
77 return kAudioFormatMicrosoftGSM;
79 return kAudioFormatiLBC;
82 return kAudioFormatMPEGLayer1;
84 return kAudioFormatMPEGLayer2;
86 return kAudioFormatMPEGLayer3;
88 return kAudioFormatALaw;
90 return kAudioFormatULaw;
92 return kAudioFormatQDesign;
94 return kAudioFormatQDesign2;
105 else if (label <= kAudioChannelLabel_LFEScreen)
107 else if (label <= kAudioChannelLabel_RightSurround)
109 else if (label <= kAudioChannelLabel_CenterSurround)
111 else if (label <= kAudioChannelLabel_RightSurroundDirect)
113 else if (label <= kAudioChannelLabel_TopBackRight)
115 else if (label < kAudioChannelLabel_RearSurroundLeft)
117 else if (label <= kAudioChannelLabel_RearSurroundRight)
119 else if (label <= kAudioChannelLabel_RightWide)
121 else if (label == kAudioChannelLabel_LFE2)
123 else if (label == kAudioChannelLabel_Mono)
131 const AudioChannelDescription* da =
a;
132 const AudioChannelDescription* db =
b;
138 AudioChannelLayoutTag
tag =
layout->mChannelLayoutTag;
139 AudioChannelLayout *new_layout;
140 if (
tag == kAudioChannelLayoutTag_UseChannelDescriptions)
142 else if (
tag == kAudioChannelLayoutTag_UseChannelBitmap)
143 AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForBitmap,
144 sizeof(UInt32), &
layout->mChannelBitmap,
size);
146 AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForTag,
147 sizeof(AudioChannelLayoutTag), &
tag,
size);
153 if (
tag == kAudioChannelLayoutTag_UseChannelBitmap)
154 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForBitmap,
155 sizeof(UInt32), &
layout->mChannelBitmap,
size, new_layout);
157 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForTag,
158 sizeof(AudioChannelLayoutTag), &
tag,
size, new_layout);
159 new_layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
167 AudioStreamBasicDescription
format;
169 if (!AudioConverterGetProperty(at->
converter,
170 kAudioConverterCurrentInputStreamDescription,
179 if (!AudioConverterGetProperty(at->
converter,
180 kAudioConverterCurrentOutputStreamDescription,
185 kAudioConverterCurrentOutputStreamDescription,
189 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterOutputChannelLayout,
192 uint64_t layout_mask = 0;
196 AudioConverterGetProperty(at->
converter, kAudioConverterOutputChannelLayout,
200 for (
i = 0;
i <
layout->mNumberChannelDescriptions;
i++) {
204 if (layout_mask & (1 <<
id))
206 layout_mask |= 1 <<
id;
207 layout->mChannelDescriptions[
i].mChannelFlags =
i;
211 qsort(
layout->mChannelDescriptions,
layout->mNumberChannelDescriptions,
213 for (
i = 0;
i <
layout->mNumberChannelDescriptions;
i++)
228 bytestream2_put_byte(pb,
tag);
230 bytestream2_put_byte(pb, (
size >> (7 *
i)) | 0x80);
231 bytestream2_put_byte(pb,
size & 0x7F);
241 if (!(extradata =
av_malloc(*cookie_size)))
248 bytestream2_put_be16(&pb, 0);
249 bytestream2_put_byte(&pb, 0x00);
255 bytestream2_put_byte(&pb, 0x40);
257 bytestream2_put_byte(&pb, 0x15);
259 bytestream2_put_be24(&pb, 0);
261 bytestream2_put_be32(&pb, 0);
262 bytestream2_put_be32(&pb, 0);
295 kAudioConverterDecompressionMagicCookie,
296 cookie_size, cookie);
316 AudioStreamBasicDescription in_format = {
320 AudioStreamBasicDescription out_format = {
321 .mFormatID = kAudioFormatLinearPCM,
322 .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
323 .mFramesPerPacket = 1,
330 UInt32 format_size =
sizeof(in_format);
335 status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo,
336 cookie_size, cookie, &format_size, &in_format);
343 #if CONFIG_MP1_AT_DECODER || CONFIG_MP2_AT_DECODER || CONFIG_MP3_AT_DECODER
351 &in_format.mChannelsPerFrame, &avctx->
frame_size,
357 #if CONFIG_AC3_AT_DECODER || CONFIG_EAC3_AT_DECODER
367 in_format.mChannelsPerFrame = hdr.
channels;
376 avctx->
sample_rate = out_format.mSampleRate = in_format.mSampleRate;
381 out_format.mBytesPerFrame =
382 out_format.mChannelsPerFrame * (out_format.mBitsPerChannel / 8);
383 out_format.mBytesPerPacket =
384 out_format.mBytesPerFrame * out_format.mFramesPerPacket;
387 in_format.mFramesPerPacket = 64;
431 AudioBufferList *
data,
432 AudioStreamPacketDescription **packets,
455 data->mNumberBuffers = 1;
456 data->mBuffers[0].mNumberChannels = 0;
469 #define COPY_SAMPLES(type) \
470 type *in_ptr = (type*)at->decoded_data; \
471 type *end_ptr = in_ptr + frame->nb_samples * avctx->ch_layout.nb_channels; \
472 type *out_ptr = (type*)frame->data[0]; \
473 for (; in_ptr < end_ptr; in_ptr += avctx->ch_layout.nb_channels, out_ptr += avctx->ch_layout.nb_channels) { \
475 for (c = 0; c < avctx->ch_layout.nb_channels; c++) \
476 out_ptr[c] = in_ptr[at->channel_map[c]]; \
490 int *got_frame_ptr,
AVPacket *avpkt)
493 int pkt_size = avpkt->
size;
495 AudioBufferList out_buffers;
500 size_t side_data_size;
504 if (side_data_size) {
509 memcpy(at->
extradata, side_data, side_data_size);
520 out_buffers = (AudioBufferList){
558 }
else if (
ret &&
ret != 1) {
587 #define FFAT_DEC_CLASS(NAME) \
588 static const AVClass ffat_##NAME##_dec_class = { \
589 .class_name = "at_" #NAME "_dec", \
590 .version = LIBAVUTIL_VERSION_INT, \
593 #define FFAT_DEC(NAME, ID, bsf_name) \
594 FFAT_DEC_CLASS(NAME) \
595 const FFCodec ff_##NAME##_at_decoder = { \
596 .p.name = #NAME "_at", \
597 CODEC_LONG_NAME(#NAME " (AudioToolbox)"), \
598 .p.type = AVMEDIA_TYPE_AUDIO, \
600 .priv_data_size = sizeof(ATDecodeContext), \
601 .init = ffat_init_decoder, \
602 .close = ffat_close_decoder, \
603 FF_CODEC_DECODE_CB(ffat_decode), \
604 .flush = ffat_decode_flush, \
605 .p.priv_class = &ffat_##NAME##_dec_class, \
607 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_CHANNEL_CONF, \
608 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
609 .p.wrapper_name = "at", \