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;
69 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
71 return kAudioFormatMPEG4AAC_ELD;
75 return kAudioFormatAppleIMA4;
77 return kAudioFormatAppleLossless;
78 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
80 return kAudioFormatiLBC;
83 return kAudioFormatALaw;
85 return kAudioFormatULaw;
95 UInt32
size =
sizeof(unsigned);
96 AudioConverterPrimeInfo prime_info;
97 AudioStreamBasicDescription out_format;
100 kAudioConverterPropertyMaximumOutputPacketSize,
106 size =
sizeof(prime_info);
108 if (!AudioConverterGetProperty(at->
converter,
109 kAudioConverterPrimeInfo,
110 &
size, &prime_info)) {
114 size =
sizeof(out_format);
115 if (!AudioConverterGetProperty(at->
converter,
116 kAudioConverterCurrentOutputStreamDescription,
117 &
size, &out_format)) {
118 if (out_format.mFramesPerPacket) {
119 avctx->
frame_size = out_format.mFramesPerPacket;
151 *
tag = bytestream2_get_byte(gb);
153 int c = bytestream2_get_byte(gb);
168 return avctx->
bit_rate <= 14000 ? 30 : 20;
193 return kAudioChannelLabel_LFE2;
202 layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
203 layout->mNumberChannelDescriptions = count;
204 for (
i = 0;
i < count;
i++) {
206 while (!(in_layout & (1 <<
c)) &&
c < 64)
211 layout->mChannelDescriptions[
i].mChannelLabel = label;
223 return kAudioChannelLayoutTag_Mono;
225 return kAudioChannelLayoutTag_Stereo;
227 return kAudioChannelLayoutTag_AAC_Quadraphonic;
229 return kAudioChannelLayoutTag_AAC_Octagonal;
231 return kAudioChannelLayoutTag_AAC_3_0;
233 return kAudioChannelLayoutTag_AAC_4_0;
235 return kAudioChannelLayoutTag_AAC_5_0;
237 return kAudioChannelLayoutTag_AAC_5_1;
239 return kAudioChannelLayoutTag_AAC_6_0;
241 return kAudioChannelLayoutTag_AAC_6_1;
243 return kAudioChannelLayoutTag_AAC_7_0;
245 return kAudioChannelLayoutTag_AAC_7_1;
247 return kAudioChannelLayoutTag_MPEG_7_1_C;
259 AudioStreamBasicDescription in_format = {
261 .mFormatID = kAudioFormatLinearPCM,
265 : kAudioFormatFlagIsSignedInteger)
266 | kAudioFormatFlagIsPacked,
268 .mFramesPerPacket = 1,
270 .mChannelsPerFrame = avctx->
channels,
273 AudioStreamBasicDescription out_format = {
276 .mChannelsPerFrame = in_format.mChannelsPerFrame,
278 UInt32 layout_size =
sizeof(AudioChannelLayout) +
279 sizeof(AudioChannelDescription) * avctx->
channels;
280 AudioChannelLayout *channel_layout =
av_malloc(layout_size);
287 out_format.mFramesPerPacket = 8000 *
mode / 1000;
288 out_format.mBytesPerPacket = (
mode == 20 ? 38 : 50);
291 status = AudioConverterNew(&in_format, &out_format, &at->
converter);
308 if (AudioConverterSetProperty(at->
converter, kAudioConverterInputChannelLayout,
309 layout_size, channel_layout)) {
317 channel_layout->mChannelLayoutTag =
tag;
318 channel_layout->mNumberChannelDescriptions = 0;
321 if (AudioConverterSetProperty(at->
converter, kAudioConverterOutputChannelLayout,
322 layout_size, channel_layout)) {
331 kAudioConverterPropertyBitDepthHint,
335 #if !TARGET_OS_IPHONE
338 kAudioCodecBitRateControlMode_Variable :
339 kAudioCodecBitRateControlMode_Constant;
341 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyBitRateControlMode,
344 if (at->
mode == kAudioCodecBitRateControlMode_Variable) {
346 if (q < 0 || q > 14) {
348 "VBR quality %d out of range, should be 0-14\n", q);
352 AudioConverterSetProperty(at->
converter, kAudioCodecPropertySoundQualityForVBR,
359 status = AudioConverterGetPropertyInfo(at->
converter,
360 kAudioConverterApplicableEncodeBitRates,
362 if (!status &&
size) {
363 UInt32 new_rate = rate;
370 kAudioConverterApplicableEncodeBitRates,
372 count =
size /
sizeof(AudioValueRange);
373 for (
i = 0;
i < count;
i++) {
374 AudioValueRange *range = &ranges[
i];
375 if (rate >= range->mMinimum && rate <= range->mMaximum) {
378 }
else if (rate > range->mMaximum) {
379 new_rate = range->mMaximum;
381 new_rate = range->mMinimum;
385 if (new_rate != rate) {
387 "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
392 AudioConverterSetProperty(at->
converter, kAudioConverterEncodeBitRate,
393 sizeof(rate), &rate);
397 AudioConverterSetProperty(at->
converter, kAudioConverterCodecQuality,
400 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterCompressionMagicCookie,
416 status = AudioConverterGetProperty(at->
converter,
417 kAudioConverterCompressionMagicCookie,
418 &extradata_size, extradata);
440 flags = bytestream2_get_byte(&gb);
458 #if !TARGET_OS_IPHONE && defined(__MAC_10_9)
459 if (at->
mode == kAudioCodecBitRateControlMode_Variable && avctx->
rc_max_rate) {
462 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyPacketSizeLimitForVBR,
463 sizeof(max_size), &max_size);
477 AudioBufferList *
data,
478 AudioStreamPacketDescription **packets,
498 data->mNumberBuffers = 1;
525 AudioBufferList out_buffers = {
534 AudioStreamPacketDescription out_pkt_desc = {0};
565 out_buffers.mBuffers[0].mData = avpkt->
data;
570 got_packet_ptr, &out_buffers,
575 if ((!ret || ret == 1) && *got_packet_ptr) {
576 avpkt->
size = out_buffers.mBuffers[0].mDataByteSize;
578 out_pkt_desc.mVariableFramesInPacket :
582 }
else if (ret && ret != 1) {
617 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
619 #if !TARGET_OS_IPHONE
620 {
"aac_at_mode",
"ratecontrol mode", offsetof(
ATDecodeContext,
mode),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable,
AE,
"mode"},
621 {
"auto",
"VBR if global quality is given; CBR otherwise", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX,
AE,
"mode"},
622 {
"cbr",
"constant bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX,
AE,
"mode"},
623 {
"abr",
"long-term average bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX,
AE,
"mode"},
624 {
"cvbr",
"constrained variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX,
AE,
"mode"},
625 {
"vbr" ,
"variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX,
AE,
"mode"},
631 #define FFAT_ENC_CLASS(NAME) \
632 static const AVClass ffat_##NAME##_enc_class = { \
633 .class_name = "at_" #NAME "_enc", \
634 .item_name = av_default_item_name, \
636 .version = LIBAVUTIL_VERSION_INT, \
639 #define FFAT_ENC(NAME, ID, PROFILES, ...) \
640 FFAT_ENC_CLASS(NAME) \
641 AVCodec ff_##NAME##_at_encoder = { \
642 .name = #NAME "_at", \
643 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
644 .type = AVMEDIA_TYPE_AUDIO, \
646 .priv_data_size = sizeof(ATDecodeContext), \
647 .init = ffat_init_encoder, \
648 .close = ffat_close_encoder, \
649 .encode2 = ffat_encode, \
650 .flush = ffat_encode_flush, \
651 .priv_class = &ffat_##NAME##_enc_class, \
652 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | \
653 AV_CODEC_CAP_ENCODER_FLUSH __VA_ARGS__, \
654 .sample_fmts = (const enum AVSampleFormat[]) { \
656 AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
658 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
659 .profiles = PROFILES, \
660 .wrapper_name = "at", \
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
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.
#define FF_PROFILE_AAC_LD
#define FF_PROFILE_UNKNOWN
#define FF_PROFILE_AAC_HE
#define FF_PROFILE_AAC_HE_V2
#define FF_PROFILE_AAC_LOW
#define FF_PROFILE_AAC_ELD
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
static int ff_bufqueue_is_full(struct FFBufQueue *queue)
Test if a buffer queue is full.
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
#define flags(name, subs,...)
#define MKBETAG(a, b, c, d)
static const uint16_t channel_layouts[7]
mode
Use these values in ebur128_init (or'ed).
channel
Use these values when setting the channel map with ebur128_set_channel().
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
#define AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_6POINT1
#define AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_6POINT0
#define AV_CH_LAYOUT_5POINT0
#define AV_CH_LAYOUT_7POINT1
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_7POINT1_WIDE_BACK
#define AV_CH_LAYOUT_OCTAGONAL
#define AV_CH_LAYOUT_7POINT0
#define AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_5POINT1
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
#define AV_CH_SURROUND_DIRECT_RIGHT
#define AV_CH_BACK_CENTER
#define AV_CH_LOW_FREQUENCY_2
#define AV_CH_TOP_BACK_RIGHT
#define AV_CH_LOW_FREQUENCY
#define AV_CH_STEREO_RIGHT
See AV_CH_STEREO_LEFT.
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
AVCodecID
Identify the syntax and semantics of the bitstream.
@ AV_CODEC_ID_ADPCM_IMA_QT
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
@ AV_SAMPLE_FMT_DBL
double
const VDPAUPixFmtMap * map
#define MP4DecConfigDescrTag
#define MP4DecSpecificDescrTag
common internal API header
struct FFBufQueue frame_queue
AudioConverterRef converter
struct FFBufQueue used_frame_queue
Describe the class of an AVClass context structure.
main external API structure.
enum AVSampleFormat sample_fmt
audio sample format
int global_quality
Global quality for codecs which cannot change it per frame.
int64_t bit_rate
the average bitrate
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
int initial_padding
Audio only.
int sample_rate
samples per second
int64_t rc_max_rate
maximum bitrate
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int channels
number of audio channels
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
uint64_t channel_layout
Audio channel layout.
int frame_size
Number of samples per channel in an audio frame.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
This structure stores compressed data.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Structure holding the queue.
unsigned short available
number of available buffers
const uint8_t * buffer_end