23 #include <AudioToolbox/AudioToolbox.h>
26 #include "config_components.h"
38 #if __MAC_OS_X_VERSION_MIN_REQUIRED < 101100
39 #define kAudioFormatEnhancedAC3 'ec-3'
63 return kAudioFormatMPEG4AAC;
65 return kAudioFormatAC3;
67 return kAudioFormatAppleIMA4;
69 return kAudioFormatAppleLossless;
71 return kAudioFormatAMR;
74 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
76 return kAudioFormatMicrosoftGSM;
78 return kAudioFormatiLBC;
81 return kAudioFormatMPEGLayer1;
83 return kAudioFormatMPEGLayer2;
85 return kAudioFormatMPEGLayer3;
87 return kAudioFormatALaw;
89 return kAudioFormatULaw;
91 return kAudioFormatQDesign;
93 return kAudioFormatQDesign2;
104 else if (label <= kAudioChannelLabel_LFEScreen)
106 else if (label <= kAudioChannelLabel_RightSurround)
108 else if (label <= kAudioChannelLabel_CenterSurround)
110 else if (label <= kAudioChannelLabel_RightSurroundDirect)
112 else if (label <= kAudioChannelLabel_TopBackRight)
114 else if (label < kAudioChannelLabel_RearSurroundLeft)
116 else if (label <= kAudioChannelLabel_RearSurroundRight)
118 else if (label <= kAudioChannelLabel_RightWide)
120 else if (label == kAudioChannelLabel_LFE2)
122 else if (label == kAudioChannelLabel_Mono)
130 const AudioChannelDescription* da =
a;
131 const AudioChannelDescription* db =
b;
137 AudioChannelLayoutTag
tag =
layout->mChannelLayoutTag;
138 AudioChannelLayout *new_layout;
139 if (
tag == kAudioChannelLayoutTag_UseChannelDescriptions)
141 else if (
tag == kAudioChannelLayoutTag_UseChannelBitmap)
142 AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForBitmap,
143 sizeof(UInt32), &
layout->mChannelBitmap,
size);
145 AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForTag,
146 sizeof(AudioChannelLayoutTag), &
tag,
size);
152 if (
tag == kAudioChannelLayoutTag_UseChannelBitmap)
153 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForBitmap,
154 sizeof(UInt32), &
layout->mChannelBitmap,
size, new_layout);
156 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForTag,
157 sizeof(AudioChannelLayoutTag), &
tag,
size, new_layout);
158 new_layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
166 AudioStreamBasicDescription
format;
168 if (!AudioConverterGetProperty(at->
converter,
169 kAudioConverterCurrentInputStreamDescription,
178 if (!AudioConverterGetProperty(at->
converter,
179 kAudioConverterCurrentOutputStreamDescription,
184 kAudioConverterCurrentOutputStreamDescription,
188 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterOutputChannelLayout,
191 uint64_t layout_mask = 0;
195 AudioConverterGetProperty(at->
converter, kAudioConverterOutputChannelLayout,
199 for (
i = 0;
i <
layout->mNumberChannelDescriptions;
i++) {
203 if (layout_mask & (1 <<
id))
205 layout_mask |= 1 <<
id;
206 layout->mChannelDescriptions[
i].mChannelFlags =
i;
210 qsort(
layout->mChannelDescriptions,
layout->mNumberChannelDescriptions,
212 for (
i = 0;
i <
layout->mNumberChannelDescriptions;
i++)
227 bytestream2_put_byte(pb,
tag);
229 bytestream2_put_byte(pb, (
size >> (7 *
i)) | 0x80);
230 bytestream2_put_byte(pb,
size & 0x7F);
240 if (!(extradata =
av_malloc(*cookie_size)))
247 bytestream2_put_be16(&pb, 0);
248 bytestream2_put_byte(&pb, 0x00);
254 bytestream2_put_byte(&pb, 0x40);
256 bytestream2_put_byte(&pb, 0x15);
258 bytestream2_put_be24(&pb, 0);
260 bytestream2_put_be32(&pb, 0);
261 bytestream2_put_be32(&pb, 0);
294 kAudioConverterDecompressionMagicCookie,
295 cookie_size, cookie);
315 AudioStreamBasicDescription in_format = {
319 AudioStreamBasicDescription out_format = {
320 .mFormatID = kAudioFormatLinearPCM,
321 .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
322 .mFramesPerPacket = 1,
329 UInt32 format_size =
sizeof(in_format);
334 status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo,
335 cookie_size, cookie, &format_size, &in_format);
342 #if CONFIG_MP1_AT_DECODER || CONFIG_MP2_AT_DECODER || CONFIG_MP3_AT_DECODER
350 &in_format.mChannelsPerFrame, &avctx->
frame_size,
356 #if CONFIG_AC3_AT_DECODER || CONFIG_EAC3_AT_DECODER
366 in_format.mChannelsPerFrame = hdr.
channels;
375 avctx->
sample_rate = out_format.mSampleRate = in_format.mSampleRate;
380 out_format.mBytesPerFrame =
381 out_format.mChannelsPerFrame * (out_format.mBitsPerChannel / 8);
382 out_format.mBytesPerPacket =
383 out_format.mBytesPerFrame * out_format.mFramesPerPacket;
386 in_format.mFramesPerPacket = 64;
430 AudioBufferList *
data,
431 AudioStreamPacketDescription **packets,
454 data->mNumberBuffers = 1;
455 data->mBuffers[0].mNumberChannels = 0;
468 #define COPY_SAMPLES(type) \
469 type *in_ptr = (type*)at->decoded_data; \
470 type *end_ptr = in_ptr + frame->nb_samples * avctx->ch_layout.nb_channels; \
471 type *out_ptr = (type*)frame->data[0]; \
472 for (; in_ptr < end_ptr; in_ptr += avctx->ch_layout.nb_channels, out_ptr += avctx->ch_layout.nb_channels) { \
474 for (c = 0; c < avctx->ch_layout.nb_channels; c++) \
475 out_ptr[c] = in_ptr[at->channel_map[c]]; \
489 int *got_frame_ptr,
AVPacket *avpkt)
492 int pkt_size = avpkt->
size;
494 AudioBufferList out_buffers;
499 size_t side_data_size;
503 if (side_data_size) {
508 memcpy(at->
extradata, side_data, side_data_size);
519 out_buffers = (AudioBufferList){
557 }
else if (
ret &&
ret != 1) {
586 #define FFAT_DEC_CLASS(NAME) \
587 static const AVClass ffat_##NAME##_dec_class = { \
588 .class_name = "at_" #NAME "_dec", \
589 .version = LIBAVUTIL_VERSION_INT, \
592 #define FFAT_DEC(NAME, ID, bsf_name) \
593 FFAT_DEC_CLASS(NAME) \
594 const FFCodec ff_##NAME##_at_decoder = { \
595 .p.name = #NAME "_at", \
596 CODEC_LONG_NAME(#NAME " (AudioToolbox)"), \
597 .p.type = AVMEDIA_TYPE_AUDIO, \
599 .priv_data_size = sizeof(ATDecodeContext), \
600 .init = ffat_init_decoder, \
601 .close = ffat_close_decoder, \
602 FF_CODEC_DECODE_CB(ffat_decode), \
603 .flush = ffat_decode_flush, \
604 .p.priv_class = &ffat_##NAME##_dec_class, \
606 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_CHANNEL_CONF, \
607 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
608 .p.wrapper_name = "at", \