Go to the documentation of this file.
23 #include <AudioToolbox/AudioToolbox.h>
25 #define FF_BUFQUEUE_SIZE 256
65 return kAudioFormatMPEG4AAC;
67 return kAudioFormatMPEG4AAC_HE;
69 return kAudioFormatMPEG4AAC_HE_V2;
71 return kAudioFormatMPEG4AAC_LD;
72 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
74 return kAudioFormatMPEG4AAC_ELD;
78 return kAudioFormatAppleIMA4;
80 return kAudioFormatAppleLossless;
81 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
83 return kAudioFormatiLBC;
86 return kAudioFormatALaw;
88 return kAudioFormatULaw;
98 UInt32
size =
sizeof(unsigned);
99 AudioConverterPrimeInfo prime_info;
100 AudioStreamBasicDescription out_format;
103 kAudioConverterPropertyMaximumOutputPacketSize,
109 size =
sizeof(prime_info);
111 if (!AudioConverterGetProperty(at->
converter,
112 kAudioConverterPrimeInfo,
113 &
size, &prime_info)) {
117 size =
sizeof(out_format);
118 if (!AudioConverterGetProperty(at->
converter,
119 kAudioConverterCurrentOutputStreamDescription,
120 &
size, &out_format)) {
121 if (out_format.mFramesPerPacket)
122 avctx->
frame_size = out_format.mFramesPerPacket;
139 *
tag = bytestream2_get_byte(gb);
141 int c = bytestream2_get_byte(gb);
156 return avctx->
bit_rate <= 14000 ? 30 : 20;
181 return kAudioChannelLabel_LFE2;
189 layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
195 if (c < 0 || c >= 64)
198 layout->mChannelDescriptions[
i].mChannelLabel = label;
208 static const struct {
240 AudioStreamBasicDescription in_format = {
242 .mFormatID = kAudioFormatLinearPCM,
246 : kAudioFormatFlagIsSignedInteger)
247 | kAudioFormatFlagIsPacked,
249 .mFramesPerPacket = 1,
254 AudioStreamBasicDescription out_format = {
257 .mChannelsPerFrame = in_format.mChannelsPerFrame,
259 UInt32 layout_size =
sizeof(AudioChannelLayout) +
261 AudioChannelLayout *channel_layout =
av_malloc(layout_size);
268 out_format.mFramesPerPacket = 8000 *
mode / 1000;
269 out_format.mBytesPerPacket = (
mode == 20 ? 38 : 50);
289 if (AudioConverterSetProperty(at->
converter, kAudioConverterInputChannelLayout,
290 layout_size, channel_layout)) {
298 channel_layout->mChannelLayoutTag =
tag;
299 channel_layout->mNumberChannelDescriptions = 0;
302 if (AudioConverterSetProperty(at->
converter, kAudioConverterOutputChannelLayout,
303 layout_size, channel_layout)) {
312 kAudioConverterPropertyBitDepthHint,
316 #if !TARGET_OS_IPHONE
319 kAudioCodecBitRateControlMode_Variable :
320 kAudioCodecBitRateControlMode_Constant;
322 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyBitRateControlMode,
325 if (at->
mode == kAudioCodecBitRateControlMode_Variable) {
327 if (q < 0 || q > 14) {
329 "VBR quality %d out of range, should be 0-14\n", q);
333 AudioConverterSetProperty(at->
converter, kAudioCodecPropertySoundQualityForVBR,
341 kAudioConverterApplicableEncodeBitRates,
344 UInt32 new_rate = rate;
351 kAudioConverterApplicableEncodeBitRates,
353 count =
size /
sizeof(AudioValueRange);
354 for (
i = 0;
i < count;
i++) {
355 AudioValueRange *
range = &ranges[
i];
356 if (rate >=
range->mMinimum && rate <= range->mMaximum) {
359 }
else if (rate >
range->mMaximum) {
360 new_rate =
range->mMaximum;
362 new_rate =
range->mMinimum;
366 if (new_rate != rate) {
368 "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
373 AudioConverterSetProperty(at->
converter, kAudioConverterEncodeBitRate,
374 sizeof(rate), &rate);
378 AudioConverterSetProperty(at->
converter, kAudioConverterCodecQuality,
381 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterCompressionMagicCookie,
398 kAudioConverterCompressionMagicCookie,
399 &extradata_size, extradata);
421 flags = bytestream2_get_byte(&gb);
437 #if !TARGET_OS_IPHONE && defined(__MAC_10_9)
438 if (at->
mode == kAudioCodecBitRateControlMode_Variable && avctx->
rc_max_rate) {
441 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyPacketSizeLimitForVBR,
442 sizeof(max_size), &max_size);
456 AudioBufferList *
data,
457 AudioStreamPacketDescription **packets,
477 data->mNumberBuffers = 1;
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) {
596 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
598 #if !TARGET_OS_IPHONE
599 {
"aac_at_mode",
"ratecontrol mode", offsetof(
ATDecodeContext,
mode),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable,
AE,
"mode"},
600 {
"auto",
"VBR if global quality is given; CBR otherwise", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX,
AE,
"mode"},
601 {
"cbr",
"constant bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX,
AE,
"mode"},
602 {
"abr",
"long-term average bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX,
AE,
"mode"},
603 {
"cvbr",
"constrained variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX,
AE,
"mode"},
604 {
"vbr" ,
"variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX,
AE,
"mode"},
610 #define FFAT_ENC_CLASS(NAME) \
611 static const AVClass ffat_##NAME##_enc_class = { \
612 .class_name = "at_" #NAME "_enc", \
613 .item_name = av_default_item_name, \
615 .version = LIBAVUTIL_VERSION_INT, \
618 #define FFAT_ENC(NAME, ID, PROFILES, CAPS, CHANNEL_LAYOUTS, CH_LAYOUTS) \
619 FFAT_ENC_CLASS(NAME) \
620 const FFCodec ff_##NAME##_at_encoder = { \
621 .p.name = #NAME "_at", \
622 CODEC_LONG_NAME(#NAME " (AudioToolbox)"), \
623 .p.type = AVMEDIA_TYPE_AUDIO, \
625 .priv_data_size = sizeof(ATDecodeContext), \
626 .init = ffat_init_encoder, \
627 .close = ffat_close_encoder, \
628 FF_CODEC_ENCODE_CB(ffat_encode), \
629 .flush = ffat_encode_flush, \
630 .p.priv_class = &ffat_##NAME##_enc_class, \
631 .p.capabilities = AV_CODEC_CAP_DELAY | \
632 AV_CODEC_CAP_ENCODER_FLUSH CAPS, \
633 CODEC_OLD_CHANNEL_LAYOUTS_ARRAY(CHANNEL_LAYOUTS) \
634 .p.ch_layouts = CH_LAYOUTS, \
635 .p.sample_fmts = (const enum AVSampleFormat[]) { \
637 AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
639 .p.profiles = PROFILES, \
640 .p.wrapper_name = "at", \
659 #if FF_API_OLD_CHANNEL_LAYOUT
660 static const uint64_t aac_at_channel_layouts[] = {
#define AV_CH_LAYOUT_7POINT0
int frame_size
Number of samples per channel in an audio frame.
#define AV_CH_LAYOUT_6POINT1
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_CH_LAYOUT_7POINT1_WIDE_BACK
@ AV_CODEC_ID_ADPCM_IMA_QT
#define MP4DecConfigDescrTag
#define AV_CHANNEL_LAYOUT_OCTAGONAL
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
#define AV_CHANNEL_LAYOUT_STEREO
int sample_rate
samples per second
#define AV_CH_LOW_FREQUENCY_2
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
#define AV_CH_LAYOUT_MONO
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
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.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
enum AVChannelOrder order
Channel order used in this layout.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
int nb_channels
Number of channels in this layout.
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
#define AV_CH_SURROUND_DIRECT_RIGHT
#define AV_CH_LAYOUT_6POINT0
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
struct FFBufQueue used_frame_queue
#define MP4DecSpecificDescrTag
AVChannelLayout ch_layout
Audio channel layout.
int initial_padding
Audio only.
int flags
AV_CODEC_FLAG_*.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define AV_CH_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_SURROUND
#define AV_CH_LAYOUT_QUAD
static int ff_bufqueue_is_full(struct FFBufQueue *queue)
Test if a buffer queue is full.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
#define AV_PROFILE_UNKNOWN
#define AV_CH_LOW_FREQUENCY
#define AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_7POINT1
int global_quality
Global quality for codecs which cannot change it per frame.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_PROFILE_AAC_ELD
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
int64_t rc_max_rate
maximum bitrate
#define AV_CH_STEREO_RIGHT
#define AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK
Describe the class of an AVClass context structure.
int64_t bit_rate
the average bitrate
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
#define AV_CH_LAYOUT_5POINT1
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
AVCodecID
Identify the syntax and semantics of the bitstream.
An AVChannelLayout holds information about the channel layout of audio data.
#define AV_PROFILE_AAC_LD
enum AVSampleFormat sample_fmt
audio sample format
#define MKBETAG(a, b, c, d)
#define AV_CHANNEL_LAYOUT_6POINT0
#define AVERROR_EXTERNAL
Generic error in an external library.
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
#define AV_CH_TOP_BACK_RIGHT
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
#define AV_CH_LAYOUT_OCTAGONAL
#define AV_CH_LAYOUT_5POINT0
int nb_samples
number of audio samples (per channel) described by this frame
#define i(width, name, range_min, range_max)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Structure holding the queue.
#define AV_CHANNEL_LAYOUT_QUAD
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
unsigned short available
number of available buffers
#define AV_CH_BACK_CENTER
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const uint8_t * buffer_end
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
#define AV_CH_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_7POINT0
#define AV_INPUT_BUFFER_PADDING_SIZE
#define AV_PROFILE_AAC_LOW
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
AudioConverterRef converter
main external API structure.
#define AV_PROFILE_AAC_HE_V2
const VDPAUPixFmtMap * map
#define AV_CHANNEL_LAYOUT_MONO
#define AV_PROFILE_AAC_HE
This structure stores compressed data.
#define AV_CH_LAYOUT_4POINT0
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
#define flags(name, subs,...)
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AV_CHANNEL_LAYOUT_6POINT1
#define AV_CHANNEL_LAYOUT_5POINT0
@ AV_SAMPLE_FMT_DBL
double
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define AV_CHANNEL_LAYOUT_5POINT1
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
struct FFBufQueue frame_queue