23 #include <AudioToolbox/AudioToolbox.h>
25 #define FF_BUFQUEUE_SIZE 256
62 return kAudioFormatMPEG4AAC;
64 return kAudioFormatMPEG4AAC_HE;
66 return kAudioFormatMPEG4AAC_HE_V2;
68 return kAudioFormatMPEG4AAC_LD;
70 return kAudioFormatMPEG4AAC_ELD;
73 return kAudioFormatAppleIMA4;
75 return kAudioFormatAppleLossless;
77 return kAudioFormatiLBC;
79 return kAudioFormatALaw;
81 return kAudioFormatULaw;
91 UInt32
size =
sizeof(unsigned);
92 AudioConverterPrimeInfo prime_info;
93 AudioStreamBasicDescription out_format;
96 kAudioConverterPropertyMaximumOutputPacketSize,
102 size =
sizeof(prime_info);
104 if (!AudioConverterGetProperty(at->
converter,
105 kAudioConverterPrimeInfo,
106 &size, &prime_info)) {
110 size =
sizeof(out_format);
111 if (!AudioConverterGetProperty(at->
converter,
112 kAudioConverterCurrentOutputStreamDescription,
113 &size, &out_format)) {
114 if (out_format.mFramesPerPacket)
115 avctx->
frame_size = out_format.mFramesPerPacket;
132 *tag = bytestream2_get_byte(gb);
134 int c = bytestream2_get_byte(gb);
135 len = (len << 7) | (c & 0x7f);
149 return avctx->
bit_rate <= 14000 ? 30 : 20;
174 return kAudioChannelLabel_LFE2;
183 layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
184 layout->mNumberChannelDescriptions =
count;
185 for (i = 0; i <
count; i++) {
187 while (!(in_layout & (1 << c)) && c < 64)
192 layout->mChannelDescriptions[i].mChannelLabel = label;
204 return kAudioChannelLayoutTag_Mono;
206 return kAudioChannelLayoutTag_Stereo;
208 return kAudioChannelLayoutTag_AAC_Quadraphonic;
210 return kAudioChannelLayoutTag_AAC_Octagonal;
212 return kAudioChannelLayoutTag_AAC_3_0;
214 return kAudioChannelLayoutTag_AAC_4_0;
216 return kAudioChannelLayoutTag_AAC_5_0;
218 return kAudioChannelLayoutTag_AAC_5_1;
220 return kAudioChannelLayoutTag_AAC_6_0;
222 return kAudioChannelLayoutTag_AAC_6_1;
224 return kAudioChannelLayoutTag_AAC_7_0;
226 return kAudioChannelLayoutTag_AAC_7_1;
228 return kAudioChannelLayoutTag_MPEG_7_1_C;
239 AudioStreamBasicDescription in_format = {
241 .mFormatID = kAudioFormatLinearPCM,
245 : kAudioFormatFlagIsSignedInteger)
246 | kAudioFormatFlagIsPacked,
248 .mFramesPerPacket = 1,
250 .mChannelsPerFrame = avctx->
channels,
253 AudioStreamBasicDescription out_format = {
256 .mChannelsPerFrame = in_format.mChannelsPerFrame,
258 UInt32 layout_size =
sizeof(AudioChannelLayout) +
259 sizeof(AudioChannelDescription) * avctx->
channels;
260 AudioChannelLayout *channel_layout =
av_malloc(layout_size);
267 out_format.mFramesPerPacket = 8000 * mode / 1000;
268 out_format.mBytesPerPacket = (mode == 20 ? 38 : 50);
271 status = AudioConverterNew(&in_format, &out_format, &at->
converter);
288 if (AudioConverterSetProperty(at->
converter, kAudioConverterInputChannelLayout,
289 layout_size, channel_layout)) {
297 channel_layout->mChannelLayoutTag =
tag;
298 channel_layout->mNumberChannelDescriptions = 0;
301 if (AudioConverterSetProperty(at->
converter, kAudioConverterOutputChannelLayout,
302 layout_size, channel_layout)) {
311 kAudioConverterPropertyBitDepthHint,
315 #if !TARGET_OS_IPHONE
318 kAudioCodecBitRateControlMode_Variable :
319 kAudioCodecBitRateControlMode_Constant;
321 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyBitRateControlMode,
324 if (at->
mode == kAudioCodecBitRateControlMode_Variable) {
326 if (q < 0 || q > 14) {
328 "VBR quality %d out of range, should be 0-14\n", q);
329 q = av_clip(q, 0, 14);
332 AudioConverterSetProperty(at->
converter, kAudioCodecPropertySoundQualityForVBR,
339 status = AudioConverterGetPropertyInfo(at->
converter,
340 kAudioConverterApplicableEncodeBitRates,
342 if (!status && size) {
343 UInt32 new_rate = rate;
346 AudioValueRange *ranges =
av_malloc(size);
350 kAudioConverterApplicableEncodeBitRates,
352 count = size /
sizeof(AudioValueRange);
353 for (i = 0; i <
count; i++) {
354 AudioValueRange *range = &ranges[i];
355 if (rate >= range->mMinimum && rate <= range->mMaximum) {
358 }
else if (rate > range->mMaximum) {
359 new_rate = range->mMaximum;
361 new_rate = range->mMinimum;
365 if (new_rate != rate) {
367 "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
372 AudioConverterSetProperty(at->
converter, kAudioConverterEncodeBitRate,
373 sizeof(rate), &rate);
377 AudioConverterSetProperty(at->
converter, kAudioConverterCodecQuality,
380 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterCompressionMagicCookie,
396 status = AudioConverterGetProperty(at->
converter,
397 kAudioConverterCompressionMagicCookie,
398 &extradata_size, extradata);
413 memmove(extradata, gb.
buffer, len);
420 flags = bytestream2_get_byte(&gb);
436 #if !TARGET_OS_IPHONE && defined(__MAC_10_9)
437 if (at->
mode == kAudioCodecBitRateControlMode_Variable && avctx->
rc_max_rate) {
440 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyPacketSizeLimitForVBR,
441 sizeof(max_size), &max_size);
455 AudioBufferList *
data,
456 AudioStreamPacketDescription **packets,
476 data->mNumberBuffers = 1;
477 data->mBuffers[0].mNumberChannels = avctx->
channels;
478 data->mBuffers[0].mDataByteSize = frame->
nb_samples *
481 data->mBuffers[0].mData = frame->
data[0];
503 AudioBufferList out_buffers = {
512 AudioStreamPacketDescription out_pkt_desc = {0};
543 out_buffers.mBuffers[0].mData = avpkt->
data;
548 got_packet_ptr, &out_buffers,
553 if ((!ret || ret == 1) && *got_packet_ptr) {
554 avpkt->
size = out_buffers.mBuffers[0].mDataByteSize;
556 out_pkt_desc.mVariableFramesInPacket :
560 }
else if (ret && ret != 1) {
595 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
597 #if !TARGET_OS_IPHONE
598 {
"aac_at_mode",
"ratecontrol mode", offsetof(
ATDecodeContext,
mode),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable,
AE,
"mode"},
599 {
"auto",
"VBR if global quality is given; CBR otherwise", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX,
AE,
"mode"},
600 {
"cbr",
"constant bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX,
AE,
"mode"},
601 {
"abr",
"long-term average bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX,
AE,
"mode"},
602 {
"cvbr",
"constrained variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX,
AE,
"mode"},
603 {
"vbr" ,
"variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX,
AE,
"mode"},
609 #define FFAT_ENC_CLASS(NAME) \
610 static const AVClass ffat_##NAME##_enc_class = { \
611 .class_name = "at_" #NAME "_enc", \
612 .item_name = av_default_item_name, \
614 .version = LIBAVUTIL_VERSION_INT, \
617 #define FFAT_ENC(NAME, ID, PROFILES, ...) \
618 FFAT_ENC_CLASS(NAME) \
619 AVCodec ff_##NAME##_at_encoder = { \
620 .name = #NAME "_at", \
621 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
622 .type = AVMEDIA_TYPE_AUDIO, \
624 .priv_data_size = sizeof(ATDecodeContext), \
625 .init = ffat_init_encoder, \
626 .close = ffat_close_encoder, \
627 .encode2 = ffat_encode, \
628 .flush = ffat_encode_flush, \
629 .priv_class = &ffat_##NAME##_enc_class, \
630 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY __VA_ARGS__, \
631 .sample_fmts = (const enum AVSampleFormat[]) { \
633 AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
635 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
636 .profiles = PROFILES, \
637 .wrapper_name = "at", \
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.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
enum AVSampleFormat sample_fmt
audio sample format
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
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.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
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)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
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
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
int global_quality
Global quality for codecs which cannot change it per frame.
#define flags(name, subs,...)
#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