23#include <AudioToolbox/AudioToolbox.h>
25#define FF_BUFQUEUE_SIZE 256
66 return kAudioFormatMPEG4AAC;
68 return kAudioFormatMPEG4AAC_HE;
70 return kAudioFormatMPEG4AAC_HE_V2;
72 return kAudioFormatMPEG4AAC_LD;
73#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
75 return kAudioFormatMPEG4AAC_ELD;
79 return kAudioFormatAppleIMA4;
81 return kAudioFormatAppleLossless;
82#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
84 return kAudioFormatiLBC;
87 return kAudioFormatALaw;
89 return kAudioFormatULaw;
99 UInt32
size =
sizeof(unsigned);
100 AudioConverterPrimeInfo prime_info;
101 AudioStreamBasicDescription out_format;
104 kAudioConverterPropertyMaximumOutputPacketSize,
110 size =
sizeof(prime_info);
112 if (!AudioConverterGetProperty(at->
converter,
113 kAudioConverterPrimeInfo,
114 &
size, &prime_info)) {
118 size =
sizeof(out_format);
119 if (!AudioConverterGetProperty(at->
converter,
120 kAudioConverterCurrentOutputStreamDescription,
121 &
size, &out_format)) {
122 if (out_format.mFramesPerPacket) {
123 avctx->
frame_size = out_format.mFramesPerPacket;
155 *
tag = bytestream2_get_byte(gb);
157 int c = bytestream2_get_byte(gb);
172 return avctx->
bit_rate <= 14000 ? 30 : 20;
197 return kAudioChannelLabel_LFE2;
205 layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
214 layout->mChannelDescriptions[
i].mChannelLabel = label;
224 static const struct {
257 AudioStreamBasicDescription in_format = {
259 .mFormatID = kAudioFormatLinearPCM,
263 : kAudioFormatFlagIsSignedInteger)
264 | kAudioFormatFlagIsPacked,
266 .mFramesPerPacket = 1,
271 AudioStreamBasicDescription out_format = {
274 .mChannelsPerFrame = in_format.mChannelsPerFrame,
276 UInt32 layout_size =
sizeof(AudioChannelLayout) +
278 AudioChannelLayout *channel_layout =
av_malloc(layout_size);
285 out_format.mFramesPerPacket = 8000 *
mode / 1000;
286 out_format.mBytesPerPacket = (
mode == 20 ? 38 : 50);
289 status = AudioConverterNew(&in_format, &out_format, &at->
converter);
306 if (AudioConverterSetProperty(at->
converter, kAudioConverterInputChannelLayout,
307 layout_size, channel_layout)) {
315 channel_layout->mChannelLayoutTag =
tag;
316 channel_layout->mNumberChannelDescriptions = 0;
319 if (AudioConverterSetProperty(at->
converter, kAudioConverterOutputChannelLayout,
320 layout_size, channel_layout)) {
329 kAudioConverterPropertyBitDepthHint,
336 kAudioCodecBitRateControlMode_Variable :
337 kAudioCodecBitRateControlMode_Constant;
339 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyBitRateControlMode,
342 if (at->
mode == kAudioCodecBitRateControlMode_Variable) {
344 if (q < 0 || q > 14) {
346 "VBR quality %d out of range, should be 0-14\n", q);
350 AudioConverterSetProperty(at->
converter, kAudioCodecPropertySoundQualityForVBR,
357 status = AudioConverterGetPropertyInfo(at->
converter,
358 kAudioConverterApplicableEncodeBitRates,
360 if (!status &&
size) {
361 UInt32 new_rate = rate;
368 kAudioConverterApplicableEncodeBitRates,
370 count =
size /
sizeof(AudioValueRange);
371 for (
i = 0;
i < count;
i++) {
372 AudioValueRange *
range = &ranges[
i];
373 if (rate >=
range->mMinimum && rate <= range->mMaximum) {
376 }
else if (rate >
range->mMaximum) {
377 new_rate =
range->mMaximum;
379 new_rate =
range->mMinimum;
383 if (new_rate != rate) {
385 "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
390 AudioConverterSetProperty(at->
converter, kAudioConverterEncodeBitRate,
391 sizeof(rate), &rate);
395 AudioConverterSetProperty(at->
converter, kAudioConverterCodecQuality,
398 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterCompressionMagicCookie,
414 status = AudioConverterGetProperty(at->
converter,
415 kAudioConverterCompressionMagicCookie,
416 &extradata_size, extradata);
438 flags = bytestream2_get_byte(&gb);
456#if !TARGET_OS_IPHONE && defined(__MAC_10_9)
457 if (at->
mode == kAudioCodecBitRateControlMode_Variable && avctx->
rc_max_rate) {
460 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyPacketSizeLimitForVBR,
461 sizeof(max_size), &max_size);
475 AudioBufferList *
data,
476 AudioStreamPacketDescription **packets,
496 data->mNumberBuffers = 1;
498 data->mBuffers[0].mDataByteSize =
frame->nb_samples *
501 data->mBuffers[0].mData =
frame->data[0];
502 if (*nb_packets >
frame->nb_samples)
503 *nb_packets =
frame->nb_samples;
522 AudioBufferList out_buffers = {
531 AudioStreamPacketDescription out_pkt_desc = {0};
562 out_buffers.mBuffers[0].mData = avpkt->
data;
567 got_packet_ptr, &out_buffers,
572 if ((!ret || ret == 1) && *got_packet_ptr) {
573 avpkt->
size = out_buffers.mBuffers[0].mDataByteSize;
575 out_pkt_desc.mVariableFramesInPacket :
581 }
else if (ret && ret != 1) {
617#define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
620 {
"aac_at_mode",
"ratecontrol mode", offsetof(
ATDecodeContext,
mode),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable,
AE, .unit =
"mode"},
621 {
"auto",
"VBR if global quality is given; CBR otherwise", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
622 {
"cbr",
"constant bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
623 {
"abr",
"long-term average bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
624 {
"cvbr",
"constrained variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
625 {
"vbr" ,
"variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX,
AE, .unit =
"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, CAPS, CHANNEL_LAYOUTS, CH_LAYOUTS) \
640 FFAT_ENC_CLASS(NAME) \
641 const FFCodec ff_##NAME##_at_encoder = { \
642 .p.name = #NAME "_at", \
643 CODEC_LONG_NAME(#NAME " (AudioToolbox)"), \
644 .p.type = AVMEDIA_TYPE_AUDIO, \
646 .priv_data_size = sizeof(ATDecodeContext), \
647 .init = ffat_init_encoder, \
648 .close = ffat_close_encoder, \
649 FF_CODEC_ENCODE_CB(ffat_encode), \
650 .flush = ffat_encode_flush, \
651 .p.priv_class = &ffat_##NAME##_enc_class, \
652 .p.capabilities = AV_CODEC_CAP_DELAY | \
653 AV_CODEC_CAP_ENCODER_FLUSH CAPS, \
654 .p.profiles = PROFILES, \
655 .p.wrapper_name = "at", \
656 CODEC_CH_LAYOUTS_ARRAY(CH_LAYOUTS), \
657 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.
#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
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.
Structure holding the queue.
unsigned short available
number of available buffers
const uint8_t * buffer_end
static const uint8_t quality[]