23 #include <AudioToolbox/AudioToolbox.h>
25 #define FF_BUFQUEUE_SIZE 256
60 return kAudioFormatMPEG4AAC;
62 return kAudioFormatMPEG4AAC_HE;
64 return kAudioFormatMPEG4AAC_HE_V2;
66 return kAudioFormatMPEG4AAC_LD;
68 return kAudioFormatMPEG4AAC_ELD;
71 return kAudioFormatAppleIMA4;
73 return kAudioFormatAppleLossless;
75 return kAudioFormatiLBC;
77 return kAudioFormatALaw;
79 return kAudioFormatULaw;
89 UInt32
size =
sizeof(unsigned);
90 AudioConverterPrimeInfo prime_info;
91 AudioStreamBasicDescription out_format;
94 kAudioConverterPropertyMaximumOutputPacketSize,
100 size =
sizeof(prime_info);
102 if (!AudioConverterGetProperty(at->
converter,
103 kAudioConverterPrimeInfo,
104 &size, &prime_info)) {
108 size =
sizeof(out_format);
109 if (!AudioConverterGetProperty(at->
converter,
110 kAudioConverterCurrentOutputStreamDescription,
111 &size, &out_format)) {
112 if (out_format.mFramesPerPacket)
113 avctx->
frame_size = out_format.mFramesPerPacket;
130 *tag = bytestream2_get_byte(gb);
132 int c = bytestream2_get_byte(gb);
133 len = (len << 7) | (c & 0x7f);
147 return avctx->
bit_rate <= 14000 ? 30 : 20;
172 return kAudioChannelLabel_LFE2;
181 layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
182 layout->mNumberChannelDescriptions =
count;
183 for (i = 0; i <
count; i++) {
185 while (!(in_layout & (1 << c)) && c < 64)
190 layout->mChannelDescriptions[i].mChannelLabel = label;
202 return kAudioChannelLayoutTag_Mono;
204 return kAudioChannelLayoutTag_Stereo;
206 return kAudioChannelLayoutTag_AAC_Quadraphonic;
208 return kAudioChannelLayoutTag_AAC_Octagonal;
210 return kAudioChannelLayoutTag_AAC_3_0;
212 return kAudioChannelLayoutTag_AAC_4_0;
214 return kAudioChannelLayoutTag_AAC_5_0;
216 return kAudioChannelLayoutTag_AAC_5_1;
218 return kAudioChannelLayoutTag_AAC_6_0;
220 return kAudioChannelLayoutTag_AAC_6_1;
222 return kAudioChannelLayoutTag_AAC_7_0;
224 return kAudioChannelLayoutTag_AAC_7_1;
226 return kAudioChannelLayoutTag_MPEG_7_1_C;
237 AudioStreamBasicDescription in_format = {
239 .mFormatID = kAudioFormatLinearPCM,
243 : kAudioFormatFlagIsSignedInteger)
244 | kAudioFormatFlagIsPacked,
246 .mFramesPerPacket = 1,
248 .mChannelsPerFrame = avctx->
channels,
251 AudioStreamBasicDescription out_format = {
254 .mChannelsPerFrame = in_format.mChannelsPerFrame,
256 UInt32 layout_size =
sizeof(AudioChannelLayout) +
257 sizeof(AudioChannelDescription) * avctx->
channels;
258 AudioChannelLayout *channel_layout =
av_malloc(layout_size);
265 out_format.mFramesPerPacket = 8000 * mode / 1000;
266 out_format.mBytesPerPacket = (mode == 20 ? 38 : 50);
269 status = AudioConverterNew(&in_format, &out_format, &at->
converter);
286 if (AudioConverterSetProperty(at->
converter, kAudioConverterInputChannelLayout,
287 layout_size, channel_layout)) {
295 channel_layout->mChannelLayoutTag =
tag;
296 channel_layout->mNumberChannelDescriptions = 0;
299 if (AudioConverterSetProperty(at->
converter, kAudioConverterOutputChannelLayout,
300 layout_size, channel_layout)) {
309 kAudioConverterPropertyBitDepthHint,
313 #if !TARGET_OS_IPHONE
316 kAudioCodecBitRateControlMode_Variable :
317 kAudioCodecBitRateControlMode_Constant;
319 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyBitRateControlMode,
322 if (at->
mode == kAudioCodecBitRateControlMode_Variable) {
324 if (q < 0 || q > 14) {
326 "VBR quality %d out of range, should be 0-14\n", q);
327 q = av_clip(q, 0, 14);
330 AudioConverterSetProperty(at->
converter, kAudioCodecPropertySoundQualityForVBR,
337 status = AudioConverterGetPropertyInfo(at->
converter,
338 kAudioConverterApplicableEncodeBitRates,
340 if (!status && size) {
341 UInt32 new_rate = rate;
344 AudioValueRange *ranges =
av_malloc(size);
348 kAudioConverterApplicableEncodeBitRates,
350 count = size /
sizeof(AudioValueRange);
351 for (i = 0; i <
count; i++) {
352 AudioValueRange *range = &ranges[i];
353 if (rate >= range->mMinimum && rate <= range->mMaximum) {
356 }
else if (rate > range->mMaximum) {
357 new_rate = range->mMaximum;
359 new_rate = range->mMinimum;
363 if (new_rate != rate) {
365 "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
370 AudioConverterSetProperty(at->
converter, kAudioConverterEncodeBitRate,
371 sizeof(rate), &rate);
375 AudioConverterSetProperty(at->
converter, kAudioConverterCodecQuality,
378 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterCompressionMagicCookie,
394 status = AudioConverterGetProperty(at->
converter,
395 kAudioConverterCompressionMagicCookie,
396 &extradata_size, extradata);
411 memmove(extradata, gb.
buffer, len);
418 flags = bytestream2_get_byte(&gb);
434 #if !TARGET_OS_IPHONE && defined(__MAC_10_9)
435 if (at->
mode == kAudioCodecBitRateControlMode_Variable && avctx->
rc_max_rate) {
438 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyPacketSizeLimitForVBR,
439 sizeof(max_size), &max_size);
449 AudioBufferList *
data,
450 AudioStreamPacketDescription **packets,
469 data->mNumberBuffers = 1;
470 data->mBuffers[0].mNumberChannels = avctx->
channels;
471 data->mBuffers[0].mDataByteSize = frame->
nb_samples *
474 data->mBuffers[0].mData = frame->
data[0];
489 AudioBufferList out_buffers = {
498 AudioStreamPacketDescription out_pkt_desc = {0};
529 out_buffers.mBuffers[0].mData = avpkt->
data;
534 got_packet_ptr, &out_buffers,
539 if ((!ret || ret == 1) && *got_packet_ptr) {
540 avpkt->
size = out_buffers.mBuffers[0].mDataByteSize;
542 out_pkt_desc.mVariableFramesInPacket :
546 }
else if (ret && ret != 1) {
580 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
582 #if !TARGET_OS_IPHONE
583 {
"aac_at_mode",
"ratecontrol mode", offsetof(
ATDecodeContext,
mode),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable,
AE,
"mode"},
584 {
"auto",
"VBR if global quality is given; CBR otherwise", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX,
AE,
"mode"},
585 {
"cbr",
"constant bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX,
AE,
"mode"},
586 {
"abr",
"long-term average bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX,
AE,
"mode"},
587 {
"cvbr",
"constrained variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX,
AE,
"mode"},
588 {
"vbr" ,
"variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX,
AE,
"mode"},
594 #define FFAT_ENC_CLASS(NAME) \
595 static const AVClass ffat_##NAME##_enc_class = { \
596 .class_name = "at_" #NAME "_enc", \
597 .item_name = av_default_item_name, \
599 .version = LIBAVUTIL_VERSION_INT, \
602 #define FFAT_ENC(NAME, ID, PROFILES, ...) \
603 FFAT_ENC_CLASS(NAME) \
604 AVCodec ff_##NAME##_at_encoder = { \
605 .name = #NAME "_at", \
606 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
607 .type = AVMEDIA_TYPE_AUDIO, \
609 .priv_data_size = sizeof(ATDecodeContext), \
610 .init = ffat_init_encoder, \
611 .close = ffat_close_encoder, \
612 .encode2 = ffat_encode, \
613 .flush = ffat_encode_flush, \
614 .priv_class = &ffat_##NAME##_enc_class, \
615 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY __VA_ARGS__, \
616 .sample_fmts = (const enum AVSampleFormat[]) { \
618 AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
620 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
621 .profiles = PROFILES, \
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
#define AV_CH_LAYOUT_7POINT1
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
#define AV_CH_LAYOUT_6POINT1
#define AV_CH_LAYOUT_6POINT0
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
#define AV_CH_LAYOUT_SURROUND
struct FFBufQueue used_frame_queue
struct FFBufQueue frame_queue
#define AV_CH_LOW_FREQUENCY_2
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
#define AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_7POINT0
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
#define AV_CH_SURROUND_DIRECT_RIGHT
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_CH_LAYOUT_STEREO
#define FF_PROFILE_AAC_HE_V2
#define AV_CH_LAYOUT_5POINT0
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Structure holding the queue.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
enum AVSampleFormat sample_fmt
audio sample format
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define AV_CH_LOW_FREQUENCY
#define FF_PROFILE_AAC_LD
#define AV_CH_LAYOUT_5POINT1
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define MP4DecSpecificDescrTag
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
int initial_padding
Audio only.
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
int flags
AV_CODEC_FLAG_*.
simple assert() macros that are a bit more flexible than ISO C assert().
#define AV_CH_LAYOUT_QUAD
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define FF_PROFILE_AAC_ELD
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
uint64_t channel_layout
Audio channel layout.
const uint8_t * buffer_end
#define AV_CH_STEREO_RIGHT
See AV_CH_STEREO_LEFT.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
static int ff_bufqueue_is_full(struct FFBufQueue *queue)
Test if a buffer queue is full.
#define FF_PROFILE_AAC_LOW
#define FF_PROFILE_UNKNOWN
AudioConverterRef converter
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
int frame_size
Number of samples per channel in an audio frame.
Libavcodec external API header.
#define MP4DecConfigDescrTag
unsigned short available
number of available buffers
int sample_rate
samples per second
main external API structure.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AV_CH_TOP_BACK_RIGHT
static const uint16_t channel_layouts[7]
Describe the class of an AVClass context structure.
#define AV_CH_LAYOUT_7POINT1_WIDE_BACK
#define AV_CH_LAYOUT_OCTAGONAL
const VDPAUPixFmtMap * map
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
int global_quality
Global quality for codecs which cannot change it per frame.
#define AV_CH_BACK_CENTER
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
common internal api header.
channel
Use these values when setting the channel map with ebur128_set_channel().
#define AV_CODEC_CAP_LOSSLESS
Codec is lossless.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define MKBETAG(a, b, c, d)
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int channels
number of audio channels
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
#define FF_PROFILE_AAC_HE
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
mode
Use these values in ebur128_init (or'ed).
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...
int64_t rc_max_rate
maximum bitrate