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;
154 *
tag = bytestream2_get_byte(gb);
156 int c = bytestream2_get_byte(gb);
171 return avctx->
bit_rate <= 14000 ? 30 : 20;
196 return kAudioChannelLabel_LFE2;
204 layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
210 if (c < 0 || c >= 64)
213 layout->mChannelDescriptions[
i].mChannelLabel = label;
223 static const struct {
256 AudioStreamBasicDescription in_format = {
258 .mFormatID = kAudioFormatLinearPCM,
262 : kAudioFormatFlagIsSignedInteger)
263 | kAudioFormatFlagIsPacked,
265 .mFramesPerPacket = 1,
270 AudioStreamBasicDescription out_format = {
273 .mChannelsPerFrame = in_format.mChannelsPerFrame,
275 UInt32 layout_size =
sizeof(AudioChannelLayout) +
277 AudioChannelLayout *channel_layout =
av_malloc(layout_size);
284 out_format.mFramesPerPacket = 8000 *
mode / 1000;
285 out_format.mBytesPerPacket = (
mode == 20 ? 38 : 50);
305 if (AudioConverterSetProperty(at->
converter, kAudioConverterInputChannelLayout,
306 layout_size, channel_layout)) {
314 channel_layout->mChannelLayoutTag =
tag;
315 channel_layout->mNumberChannelDescriptions = 0;
318 if (AudioConverterSetProperty(at->
converter, kAudioConverterOutputChannelLayout,
319 layout_size, channel_layout)) {
328 kAudioConverterPropertyBitDepthHint,
332 #if !TARGET_OS_IPHONE
335 kAudioCodecBitRateControlMode_Variable :
336 kAudioCodecBitRateControlMode_Constant;
338 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyBitRateControlMode,
341 if (at->
mode == kAudioCodecBitRateControlMode_Variable) {
343 if (q < 0 || q > 14) {
345 "VBR quality %d out of range, should be 0-14\n", q);
349 AudioConverterSetProperty(at->
converter, kAudioCodecPropertySoundQualityForVBR,
357 kAudioConverterApplicableEncodeBitRates,
360 UInt32 new_rate = rate;
367 kAudioConverterApplicableEncodeBitRates,
369 count =
size /
sizeof(AudioValueRange);
370 for (
i = 0;
i < count;
i++) {
371 AudioValueRange *
range = &ranges[
i];
372 if (rate >=
range->mMinimum && rate <= range->mMaximum) {
375 }
else if (rate >
range->mMaximum) {
376 new_rate =
range->mMaximum;
378 new_rate =
range->mMinimum;
382 if (new_rate != rate) {
384 "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
389 AudioConverterSetProperty(at->
converter, kAudioConverterEncodeBitRate,
390 sizeof(rate), &rate);
394 AudioConverterSetProperty(at->
converter, kAudioConverterCodecQuality,
397 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterCompressionMagicCookie,
414 kAudioConverterCompressionMagicCookie,
415 &extradata_size, extradata);
437 flags = bytestream2_get_byte(&gb);
455 #if !TARGET_OS_IPHONE && defined(__MAC_10_9)
456 if (at->
mode == kAudioCodecBitRateControlMode_Variable && avctx->
rc_max_rate) {
459 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyPacketSizeLimitForVBR,
460 sizeof(max_size), &max_size);
474 AudioBufferList *
data,
475 AudioStreamPacketDescription **packets,
495 data->mNumberBuffers = 1;
521 AudioBufferList out_buffers = {
530 AudioStreamPacketDescription out_pkt_desc = {0};
561 out_buffers.mBuffers[0].mData = avpkt->
data;
566 got_packet_ptr, &out_buffers,
571 if ((!
ret ||
ret == 1) && *got_packet_ptr) {
572 avpkt->
size = out_buffers.mBuffers[0].mDataByteSize;
574 out_pkt_desc.mVariableFramesInPacket :
578 }
else if (
ret &&
ret != 1) {
614 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
616 #if !TARGET_OS_IPHONE
617 {
"aac_at_mode",
"ratecontrol mode", offsetof(
ATDecodeContext,
mode),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable,
AE, .unit =
"mode"},
618 {
"auto",
"VBR if global quality is given; CBR otherwise", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
619 {
"cbr",
"constant bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
620 {
"abr",
"long-term average bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
621 {
"cvbr",
"constrained variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
622 {
"vbr" ,
"variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX,
AE, .unit =
"mode"},
628 #define FFAT_ENC_CLASS(NAME) \
629 static const AVClass ffat_##NAME##_enc_class = { \
630 .class_name = "at_" #NAME "_enc", \
631 .item_name = av_default_item_name, \
633 .version = LIBAVUTIL_VERSION_INT, \
636 #define FFAT_ENC(NAME, ID, PROFILES, CAPS, CHANNEL_LAYOUTS, CH_LAYOUTS) \
637 FFAT_ENC_CLASS(NAME) \
638 const FFCodec ff_##NAME##_at_encoder = { \
639 .p.name = #NAME "_at", \
640 CODEC_LONG_NAME(#NAME " (AudioToolbox)"), \
641 .p.type = AVMEDIA_TYPE_AUDIO, \
643 .priv_data_size = sizeof(ATDecodeContext), \
644 .init = ffat_init_encoder, \
645 .close = ffat_close_encoder, \
646 FF_CODEC_ENCODE_CB(ffat_encode), \
647 .flush = ffat_encode_flush, \
648 .p.priv_class = &ffat_##NAME##_enc_class, \
649 .p.capabilities = AV_CODEC_CAP_DELAY | \
650 AV_CODEC_CAP_ENCODER_FLUSH CAPS, \
651 .p.ch_layouts = CH_LAYOUTS, \
652 .p.sample_fmts = (const enum AVSampleFormat[]) { \
654 AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
656 .p.profiles = PROFILES, \
657 .p.wrapper_name = "at", \
int frame_size
Number of samples per channel in an audio frame.
#define AV_LOG_WARNING
Something somehow does not look correct.
@ 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_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
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_CHANNEL_LAYOUT_SURROUND
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.
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
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_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.
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