23 #include <AudioToolbox/AudioToolbox.h>
36 #define kAudioFormatEnhancedAC3 'ec-3'
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;
100 else if (label <= kAudioChannelLabel_LFEScreen)
102 else if (label <= kAudioChannelLabel_RightSurround)
104 else if (label <= kAudioChannelLabel_CenterSurround)
106 else if (label <= kAudioChannelLabel_RightSurroundDirect)
108 else if (label <= kAudioChannelLabel_TopBackRight)
110 else if (label < kAudioChannelLabel_RearSurroundLeft)
112 else if (label <= kAudioChannelLabel_RearSurroundRight)
114 else if (label <= kAudioChannelLabel_RightWide)
116 else if (label == kAudioChannelLabel_LFE2)
118 else if (label == kAudioChannelLabel_Mono)
126 const AudioChannelDescription* da =
a;
127 const AudioChannelDescription* db =
b;
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);
141 AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForTag,
142 sizeof(AudioChannelLayoutTag), &tag, size);
148 if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
149 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForBitmap,
150 sizeof(UInt32), &layout->mChannelBitmap, size, new_layout);
152 AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForTag,
153 sizeof(AudioChannelLayoutTag), &tag, size, new_layout);
154 new_layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
162 AudioStreamBasicDescription
format;
164 if (!AudioConverterGetProperty(at->
converter,
165 kAudioConverterCurrentInputStreamDescription,
167 if (format.mSampleRate)
169 avctx->
channels = format.mChannelsPerFrame;
174 if (!AudioConverterGetProperty(at->
converter,
175 kAudioConverterCurrentOutputStreamDescription,
178 format.mChannelsPerFrame = avctx->
channels;
180 kAudioConverterCurrentOutputStreamDescription,
184 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterOutputChannelLayout,
185 &size,
NULL) && size) {
187 uint64_t layout_mask = 0;
191 AudioConverterGetProperty(at->
converter, kAudioConverterOutputChannelLayout,
195 for (i = 0; i < layout->mNumberChannelDescriptions; i++) {
199 if (layout_mask & (1 <<
id))
201 layout_mask |= 1 <<
id;
202 layout->mChannelDescriptions[i].mChannelFlags = i;
205 qsort(layout->mChannelDescriptions, layout->mNumberChannelDescriptions,
207 for (i = 0; i < layout->mNumberChannelDescriptions; i++)
208 at->
channel_map[i] = layout->mChannelDescriptions[i].mChannelFlags;
222 bytestream2_put_byte(pb, tag);
224 bytestream2_put_byte(pb, (size >> (7 * i)) | 0x80);
225 bytestream2_put_byte(pb, size & 0x7F);
235 if (!(extradata =
av_malloc(*cookie_size)))
242 bytestream2_put_be16(&pb, 0);
243 bytestream2_put_byte(&pb, 0x00);
249 bytestream2_put_byte(&pb, 0x40);
251 bytestream2_put_byte(&pb, 0x15);
253 bytestream2_put_be24(&pb, 0);
255 bytestream2_put_be32(&pb, 0);
256 bytestream2_put_be32(&pb, 0);
288 status = AudioConverterSetProperty(at->
converter,
289 kAudioConverterDecompressionMagicCookie,
290 cookie_size, cookie);
309 AudioStreamBasicDescription in_format = {
313 AudioStreamBasicDescription out_format = {
314 .mFormatID = kAudioFormatLinearPCM,
315 .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
316 .mFramesPerPacket = 1,
323 UInt32 format_size =
sizeof(in_format);
328 status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo,
329 cookie_size, cookie, &format_size, &in_format);
336 #if CONFIG_MP1_AT_DECODER || CONFIG_MP2_AT_DECODER || CONFIG_MP3_AT_DECODER
337 }
else if (pkt && pkt->
size >= 4 &&
344 &in_format.mChannelsPerFrame, &avctx->
frame_size,
345 &bit_rate, &codec_id) < 0)
350 #if CONFIG_AC3_AT_DECODER || CONFIG_EAC3_AT_DECODER
351 }
else if (pkt && pkt->
size >= 7 &&
360 in_format.mChannelsPerFrame = hdr.
channels;
369 avctx->
sample_rate = out_format.mSampleRate = in_format.mSampleRate;
370 avctx->
channels = out_format.mChannelsPerFrame = in_format.mChannelsPerFrame;
373 in_format.mFramesPerPacket = 64;
375 status = AudioConverterNew(&in_format, &out_format, &at->
converter);
412 AudioBufferList *
data,
413 AudioStreamPacketDescription **packets,
436 data->mNumberBuffers = 1;
437 data->mBuffers[0].mNumberChannels = 0;
438 data->mBuffers[0].mDataByteSize = at->
in_pkt.
size;
450 #define COPY_SAMPLES(type) \
451 type *in_ptr = (type*)at->decoded_data; \
452 type *end_ptr = in_ptr + frame->nb_samples * avctx->channels; \
453 type *out_ptr = (type*)frame->data[0]; \
454 for (; in_ptr < end_ptr; in_ptr += avctx->channels, out_ptr += avctx->channels) { \
456 for (c = 0; c < avctx->channels; c++) \
457 out_ptr[c] = in_ptr[at->channel_map[c]]; \
471 int *got_frame_ptr,
AVPacket *avpkt)
475 int pkt_size = avpkt->
size;
478 AudioBufferList out_buffers;
510 avpkt = &filtered_packet;
520 out_buffers = (AudioBufferList){
534 if (filtered_packet.
data) {
551 if ((!ret || ret == 1) && frame->
nb_samples) {
560 }
else if (ret && ret != 1) {
588 #define FFAT_DEC_CLASS(NAME) \
589 static const AVClass ffat_##NAME##_dec_class = { \
590 .class_name = "at_" #NAME "_dec", \
591 .version = LIBAVUTIL_VERSION_INT, \
594 #define FFAT_DEC(NAME, ID) \
595 FFAT_DEC_CLASS(NAME) \
596 AVCodec ff_##NAME##_at_decoder = { \
597 .name = #NAME "_at", \
598 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
599 .type = AVMEDIA_TYPE_AUDIO, \
601 .priv_data_size = sizeof(ATDecodeContext), \
602 .init = ffat_init_decoder, \
603 .close = ffat_close_decoder, \
604 .decode = ffat_decode, \
605 .flush = ffat_decode_flush, \
606 .priv_class = &ffat_##NAME##_dec_class, \
607 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, \
608 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
void av_bsf_free(AVBSFContext **ctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
AVCodecParameters * par_out
Parameters of the output stream.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
The bitstream filter state.
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
#define AV_CH_LOW_FREQUENCY_2
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
enum AVSampleFormat sample_fmt
audio sample format
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
preferred ID for decoding MPEG audio layer 1, 2 or 3
simple assert() macros that are a bit more flexible than ISO C assert().
uint64_t channel_layout
Audio channel layout.
int extradata_size
Size of the extradata content in bytes.
AudioStreamPacketDescription pkt_desc
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
AudioConverterRef converter
#define AVERROR_BSF_NOT_FOUND
Bitstream filter not found.
#define AV_CH_FRONT_CENTER
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
int frame_size
Number of samples per channel in an audio frame.
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
int sample_rate
samples per second
main external API structure.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static const char * format
Describe the class of an AVClass context structure.
int sample_rate
Sample rate of the audio data.
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
common internal api header.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
int avpriv_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo **phdr)
Parse AC-3 frame header.
int channels
number of audio channels
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
This structure stores compressed data.
AVCodecParameters * par_in
Parameters of the input stream.
int nb_samples
number of audio samples (per channel) described by this frame
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.