23#include <AudioToolbox/AudioToolbox.h>
25#define FF_BUFQUEUE_SIZE 256
67 return kAudioFormatMPEG4AAC;
69 return kAudioFormatMPEG4AAC_HE;
71 return kAudioFormatMPEG4AAC_HE_V2;
73 return kAudioFormatMPEG4AAC_LD;
74#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
76 return kAudioFormatMPEG4AAC_ELD;
80 return kAudioFormatAppleIMA4;
82 return kAudioFormatAppleLossless;
83#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
85 return kAudioFormatiLBC;
88 return kAudioFormatALaw;
90 return kAudioFormatULaw;
100 UInt32
size =
sizeof(unsigned);
101 AudioConverterPrimeInfo prime_info;
102 AudioStreamBasicDescription out_format;
105 kAudioConverterPropertyMaximumOutputPacketSize,
111 size =
sizeof(prime_info);
113 if (!AudioConverterGetProperty(at->
converter,
114 kAudioConverterPrimeInfo,
115 &
size, &prime_info)) {
123 size =
sizeof(out_format);
124 if (!AudioConverterGetProperty(at->
converter,
125 kAudioConverterCurrentOutputStreamDescription,
126 &
size, &out_format)) {
127 if (out_format.mFramesPerPacket) {
128 avctx->
frame_size = out_format.mFramesPerPacket;
160 *
tag = bytestream2_get_byte(gb);
162 int c = bytestream2_get_byte(gb);
177 return avctx->
bit_rate <= 14000 ? 30 : 20;
202 return kAudioChannelLabel_LFE2;
210 layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
229 static const struct {
262 AudioStreamBasicDescription in_format = {
264 .mFormatID = kAudioFormatLinearPCM,
268 : kAudioFormatFlagIsSignedInteger)
269 | kAudioFormatFlagIsPacked,
271 .mFramesPerPacket = 1,
276 AudioStreamBasicDescription out_format = {
279 .mChannelsPerFrame = in_format.mChannelsPerFrame,
281 UInt32 layout_size =
sizeof(AudioChannelLayout) +
283 AudioChannelLayout *channel_layout =
av_malloc(layout_size);
290 out_format.mFramesPerPacket = 8000 *
mode / 1000;
291 out_format.mBytesPerPacket = (
mode == 20 ? 38 : 50);
294 status = AudioConverterNew(&in_format, &out_format, &at->
converter);
311 if (AudioConverterSetProperty(at->
converter, kAudioConverterInputChannelLayout,
312 layout_size, channel_layout)) {
320 channel_layout->mChannelLayoutTag =
tag;
321 channel_layout->mNumberChannelDescriptions = 0;
324 if (AudioConverterSetProperty(at->
converter, kAudioConverterOutputChannelLayout,
325 layout_size, channel_layout)) {
334 kAudioConverterPropertyBitDepthHint,
341 kAudioCodecBitRateControlMode_Variable :
342 kAudioCodecBitRateControlMode_Constant;
344 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyBitRateControlMode,
347 if (at->
mode == kAudioCodecBitRateControlMode_Variable) {
349 if (q < 0 || q > 14) {
351 "VBR quality %d out of range, should be 0-14\n", q);
355 AudioConverterSetProperty(at->
converter, kAudioCodecPropertySoundQualityForVBR,
362 status = AudioConverterGetPropertyInfo(at->
converter,
363 kAudioConverterApplicableEncodeBitRates,
365 if (!status &&
size) {
366 UInt32 new_rate = rate;
373 kAudioConverterApplicableEncodeBitRates,
375 count =
size /
sizeof(AudioValueRange);
376 for (
i = 0;
i < count;
i++) {
377 AudioValueRange *
range = &ranges[
i];
378 if (rate >=
range->mMinimum && rate <= range->mMaximum) {
381 }
else if (rate >
range->mMaximum) {
382 new_rate =
range->mMaximum;
384 new_rate =
range->mMinimum;
388 if (new_rate != rate) {
390 "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
395 AudioConverterSetProperty(at->
converter, kAudioConverterEncodeBitRate,
396 sizeof(rate), &rate);
400 AudioConverterSetProperty(at->
converter, kAudioConverterCodecQuality,
403 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterCompressionMagicCookie,
419 status = AudioConverterGetProperty(at->
converter,
420 kAudioConverterCompressionMagicCookie,
421 &extradata_size, extradata);
443 flags = bytestream2_get_byte(&gb);
461#if !TARGET_OS_IPHONE && defined(__MAC_10_9)
462 if (at->
mode == kAudioCodecBitRateControlMode_Variable && avctx->
rc_max_rate) {
465 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyPacketSizeLimitForVBR,
466 sizeof(max_size), &max_size);
480 AudioBufferList *
data,
481 AudioStreamPacketDescription **packets,
501 data->mNumberBuffers = 1;
503 data->mBuffers[0].mDataByteSize =
frame->nb_samples *
506 data->mBuffers[0].mData =
frame->data[0];
507 if (*nb_packets >
frame->nb_samples)
508 *nb_packets =
frame->nb_samples;
527 AudioBufferList out_buffers = {
536 AudioStreamPacketDescription out_pkt_desc = {0};
567 out_buffers.mBuffers[0].mData = avpkt->
data;
572 got_packet_ptr, &out_buffers,
577 if ((!
ret ||
ret == 1) && *got_packet_ptr) {
578 avpkt->
size = out_buffers.mBuffers[0].mDataByteSize;
590 out_pkt_desc.mVariableFramesInPacket :
597 }
else if (
ret &&
ret != 1) {
633#define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
636 {
"aac_at_mode",
"ratecontrol mode", offsetof(
ATDecodeContext,
mode),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable,
AE, .unit =
"mode"},
637 {
"auto",
"VBR if global quality is given; CBR otherwise", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
638 {
"cbr",
"constant bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
639 {
"abr",
"long-term average bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
640 {
"cvbr",
"constrained variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
641 {
"vbr" ,
"variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
647#define FFAT_ENC_CLASS(NAME) \
648 static const AVClass ffat_##NAME##_enc_class = { \
649 .class_name = "at_" #NAME "_enc", \
650 .item_name = av_default_item_name, \
652 .version = LIBAVUTIL_VERSION_INT, \
655#define FFAT_ENC(NAME, ID, PROFILES, CAPS, CHANNEL_LAYOUTS, CH_LAYOUTS) \
656 FFAT_ENC_CLASS(NAME) \
657 const FFCodec ff_##NAME##_at_encoder = { \
658 .p.name = #NAME "_at", \
659 CODEC_LONG_NAME(#NAME " (AudioToolbox)"), \
660 .p.type = AVMEDIA_TYPE_AUDIO, \
662 .priv_data_size = sizeof(ATDecodeContext), \
663 .init = ffat_init_encoder, \
664 .close = ffat_close_encoder, \
665 FF_CODEC_ENCODE_CB(ffat_encode), \
666 .flush = ffat_encode_flush, \
667 .p.priv_class = &ffat_##NAME##_enc_class, \
668 .p.capabilities = AV_CODEC_CAP_DELAY | \
669 AV_CODEC_CAP_ENCODER_FLUSH CAPS, \
670 .p.profiles = PROFILES, \
671 .p.wrapper_name = "at", \
672 CODEC_CH_LAYOUTS_ARRAY(CH_LAYOUTS), \
673 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), \
av_cold void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
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.
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(const 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 i(width, name, range_min, range_max)
Public libavutil channel layout APIs header.
#define AV_PROFILE_AAC_HE
#define AV_PROFILE_UNKNOWN
#define AV_PROFILE_AAC_LOW
#define AV_PROFILE_AAC_HE_V2
#define AV_PROFILE_AAC_LD
#define AV_PROFILE_AAC_ELD
channel
Use these values when setting the channel map with ebur128_set_channel().
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
#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
#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.
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_CHANNEL_LAYOUT_4POINT0
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
#define AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_STEREO
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
#define AV_CHANNEL_LAYOUT_6POINT0
#define AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_MONO
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
#define AV_CHANNEL_LAYOUT_7POINT0
#define AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_OCTAGONAL
#define AV_CHANNEL_LAYOUT_7POINT1
#define AV_CHANNEL_LAYOUT_QUAD
#define AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK
#define AV_CHANNEL_LAYOUT_6POINT1
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#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.
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same 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.
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
#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.
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
@ AV_SAMPLE_FMT_DBL
double
#define AV_NOPTS_VALUE
Undefined timestamp value.
const VDPAUPixFmtMap * map
#define MP4DecConfigDescrTag
#define MP4DecSpecificDescrTag
common internal api header.
static const AVClass av_class
#define MKBETAG(a, b, c, d)
Memory handling functions.
#define FF_ARRAY_ELEMS(a)
struct FFBufQueue frame_queue
AudioConverterRef converter
struct FFBufQueue used_frame_queue
An AVChannelLayout holds information about the channel layout of audio data.
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
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
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.
int flags
A combination of AV_PKT_FLAG values.
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
static const uint8_t quality[]