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 & (1ULL <<
id))
206 layout_mask |= 1ULL <<
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);
294 status = AudioConverterSetProperty(at->
converter,
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
344 }
else if (
pkt &&
pkt->size >= 4 &&
351 &in_format.mChannelsPerFrame, &avctx->
frame_size,
357#if CONFIG_AC3_AT_DECODER || CONFIG_EAC3_AT_DECODER
358 }
else if (
pkt &&
pkt->size >= 7 &&
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;
389 status = AudioConverterNew(&in_format, &out_format, &at->
converter);
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){
551 if ((!ret || ret == 1) &&
frame->nb_samples) {
560 }
else if (ret && ret != 1) {
589#define FFAT_DEC_CLASS(NAME) \
590 static const AVClass ffat_##NAME##_dec_class = { \
591 .class_name = "at_" #NAME "_dec", \
592 .version = LIBAVUTIL_VERSION_INT, \
595#define FFAT_DEC(NAME, ID, bsf_name) \
596 FFAT_DEC_CLASS(NAME) \
597 const FFCodec ff_##NAME##_at_decoder = { \
598 .p.name = #NAME "_at", \
599 CODEC_LONG_NAME(#NAME " (AudioToolbox)"), \
600 .p.type = AVMEDIA_TYPE_AUDIO, \
602 .priv_data_size = sizeof(ATDecodeContext), \
603 .init = ffat_init_decoder, \
604 .close = ffat_close_decoder, \
605 FF_CODEC_DECODE_CB(ffat_decode), \
606 .flush = ffat_decode_flush, \
607 .p.priv_class = &ffat_##NAME##_dec_class, \
609 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_CHANNEL_CONF, \
610 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
611 .p.wrapper_name = "at", \
int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
Parse AC-3 frame header.
static const char *const format[]
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
#define i(width, name, range_min, range_max)
Public libavutil channel layout APIs header.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
#define AV_CH_LOW_FREQUENCY_2
#define AV_CH_FRONT_CENTER
AVCodecID
Identify the syntax and semantics of the bitstream.
@ AV_CODEC_ID_ADPCM_IMA_QT
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_S32
signed 32 bits
@ AV_SAMPLE_FMT_S16
signed 16 bits
#define AV_NOPTS_VALUE
Undefined timestamp value.
Memory handling functions.
AudioConverterRef converter
AudioStreamPacketDescription pkt_desc
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
int64_t bit_rate
the average bitrate
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
int sample_rate
samples per second
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
int frame_size
Number of samples per channel in an audio frame.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...