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;
70 return kAudioFormatMPEG4AAC_ELD;
73 return kAudioFormatAppleIMA4;
75 return kAudioFormatAppleLossless;
77 return kAudioFormatiLBC;
79 return kAudioFormatALaw;
81 return kAudioFormatULaw;
91 UInt32
size =
sizeof(unsigned);
92 AudioConverterPrimeInfo prime_info;
93 AudioStreamBasicDescription out_format;
96 kAudioConverterPropertyMaximumOutputPacketSize,
102 size =
sizeof(prime_info);
104 if (!AudioConverterGetProperty(at->
converter,
105 kAudioConverterPrimeInfo,
106 &size, &prime_info)) {
110 size =
sizeof(out_format);
111 if (!AudioConverterGetProperty(at->
converter,
112 kAudioConverterCurrentOutputStreamDescription,
113 &size, &out_format)) {
114 if (out_format.mFramesPerPacket)
115 avctx->
frame_size = out_format.mFramesPerPacket;
132 *tag = bytestream2_get_byte(gb);
134 int c = bytestream2_get_byte(gb);
135 len = (len << 7) | (c & 0x7f);
149 return avctx->
bit_rate <= 14000 ? 30 : 20;
174 return kAudioChannelLabel_LFE2;
183 layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
184 layout->mNumberChannelDescriptions =
count;
185 for (i = 0; i <
count; i++) {
187 while (!(in_layout & (1 << c)) && c < 64)
192 layout->mChannelDescriptions[
i].mChannelLabel = label;
204 return kAudioChannelLayoutTag_Mono;
206 return kAudioChannelLayoutTag_Stereo;
208 return kAudioChannelLayoutTag_AAC_Quadraphonic;
210 return kAudioChannelLayoutTag_AAC_Octagonal;
212 return kAudioChannelLayoutTag_AAC_3_0;
214 return kAudioChannelLayoutTag_AAC_4_0;
216 return kAudioChannelLayoutTag_AAC_5_0;
218 return kAudioChannelLayoutTag_AAC_5_1;
220 return kAudioChannelLayoutTag_AAC_6_0;
222 return kAudioChannelLayoutTag_AAC_6_1;
224 return kAudioChannelLayoutTag_AAC_7_0;
226 return kAudioChannelLayoutTag_AAC_7_1;
228 return kAudioChannelLayoutTag_MPEG_7_1_C;
239 AudioStreamBasicDescription in_format = {
241 .mFormatID = kAudioFormatLinearPCM,
245 : kAudioFormatFlagIsSignedInteger)
246 | kAudioFormatFlagIsPacked,
248 .mFramesPerPacket = 1,
250 .mChannelsPerFrame = avctx->
channels,
253 AudioStreamBasicDescription out_format = {
256 .mChannelsPerFrame = in_format.mChannelsPerFrame,
258 UInt32 layout_size =
sizeof(AudioChannelLayout) +
259 sizeof(AudioChannelDescription) * avctx->
channels;
260 AudioChannelLayout *channel_layout =
av_malloc(layout_size);
267 out_format.mFramesPerPacket = 8000 * mode / 1000;
268 out_format.mBytesPerPacket = (mode == 20 ? 38 : 50);
271 status = AudioConverterNew(&in_format, &out_format, &at->
converter);
288 if (AudioConverterSetProperty(at->
converter, kAudioConverterInputChannelLayout,
289 layout_size, channel_layout)) {
297 channel_layout->mChannelLayoutTag =
tag;
298 channel_layout->mNumberChannelDescriptions = 0;
301 if (AudioConverterSetProperty(at->
converter, kAudioConverterOutputChannelLayout,
302 layout_size, channel_layout)) {
311 kAudioConverterPropertyBitDepthHint,
315 #if !TARGET_OS_IPHONE 318 kAudioCodecBitRateControlMode_Variable :
319 kAudioCodecBitRateControlMode_Constant;
321 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyBitRateControlMode,
324 if (at->
mode == kAudioCodecBitRateControlMode_Variable) {
326 if (q < 0 || q > 14) {
328 "VBR quality %d out of range, should be 0-14\n", q);
329 q = av_clip(q, 0, 14);
332 AudioConverterSetProperty(at->
converter, kAudioCodecPropertySoundQualityForVBR,
339 status = AudioConverterGetPropertyInfo(at->
converter,
340 kAudioConverterApplicableEncodeBitRates,
342 if (!status && size) {
343 UInt32 new_rate = rate;
346 AudioValueRange *ranges =
av_malloc(size);
350 kAudioConverterApplicableEncodeBitRates,
352 count = size /
sizeof(AudioValueRange);
353 for (i = 0; i <
count; i++) {
354 AudioValueRange *range = &ranges[
i];
355 if (rate >= range->mMinimum && rate <= range->mMaximum) {
358 }
else if (rate > range->mMaximum) {
359 new_rate = range->mMaximum;
361 new_rate = range->mMinimum;
365 if (new_rate != rate) {
367 "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
372 AudioConverterSetProperty(at->
converter, kAudioConverterEncodeBitRate,
373 sizeof(rate), &rate);
377 AudioConverterSetProperty(at->
converter, kAudioConverterCodecQuality,
380 if (!AudioConverterGetPropertyInfo(at->
converter, kAudioConverterCompressionMagicCookie,
396 status = AudioConverterGetProperty(at->
converter,
397 kAudioConverterCompressionMagicCookie,
398 &extradata_size, extradata);
413 memmove(extradata, gb.
buffer, len);
420 flags = bytestream2_get_byte(&gb);
436 #if !TARGET_OS_IPHONE && defined(__MAC_10_9) 437 if (at->
mode == kAudioCodecBitRateControlMode_Variable && avctx->
rc_max_rate) {
440 AudioConverterSetProperty(at->
converter, kAudioCodecPropertyPacketSizeLimitForVBR,
441 sizeof(max_size), &max_size);
455 AudioBufferList *
data,
456 AudioStreamPacketDescription **packets,
476 data->mNumberBuffers = 1;
477 data->mBuffers[0].mNumberChannels = avctx->
channels;
478 data->mBuffers[0].mDataByteSize = frame->
nb_samples *
481 data->mBuffers[0].mData = frame->
data[0];
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) {
595 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM 597 #if !TARGET_OS_IPHONE 598 {
"aac_at_mode",
"ratecontrol mode", offsetof(
ATDecodeContext,
mode),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable,
AE,
"mode"},
599 {
"auto",
"VBR if global quality is given; CBR otherwise", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX,
AE,
"mode"},
600 {
"cbr",
"constant bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX,
AE,
"mode"},
601 {
"abr",
"long-term average bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX,
AE,
"mode"},
602 {
"cvbr",
"constrained variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX,
AE,
"mode"},
603 {
"vbr" ,
"variable bitrate", 0,
AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX,
AE,
"mode"},
609 #define FFAT_ENC_CLASS(NAME) \ 610 static const AVClass ffat_##NAME##_enc_class = { \ 611 .class_name = "at_" #NAME "_enc", \ 612 .item_name = av_default_item_name, \ 614 .version = LIBAVUTIL_VERSION_INT, \ 617 #define FFAT_ENC(NAME, ID, PROFILES, ...) \ 618 FFAT_ENC_CLASS(NAME) \ 619 AVCodec ff_##NAME##_at_encoder = { \ 620 .name = #NAME "_at", \ 621 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \ 622 .type = AVMEDIA_TYPE_AUDIO, \ 624 .priv_data_size = sizeof(ATDecodeContext), \ 625 .init = ffat_init_encoder, \ 626 .close = ffat_close_encoder, \ 627 .encode2 = ffat_encode, \ 628 .flush = ffat_encode_flush, \ 629 .priv_class = &ffat_##NAME##_enc_class, \ 630 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | \ 631 AV_CODEC_CAP_ENCODER_FLUSH __VA_ARGS__, \ 632 .sample_fmts = (const enum AVSampleFormat[]) { \ 634 AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \ 636 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \ 637 .profiles = PROFILES, \ 638 .wrapper_name = "at", \ static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
#define AV_CH_LAYOUT_7POINT1
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
#define AV_CH_LAYOUT_6POINT1
#define AV_CH_LAYOUT_6POINT0
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
#define AV_CH_LAYOUT_SURROUND
struct FFBufQueue used_frame_queue
struct FFBufQueue frame_queue
#define AV_CH_LOW_FREQUENCY_2
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
#define AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_7POINT0
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
#define AV_CH_SURROUND_DIRECT_RIGHT
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_CH_LAYOUT_STEREO
#define FF_PROFILE_AAC_HE_V2
#define AV_CH_LAYOUT_5POINT0
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Structure holding the queue.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
enum AVSampleFormat sample_fmt
audio sample format
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
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
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define AV_CH_LOW_FREQUENCY
#define FF_PROFILE_AAC_LD
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
#define AV_CH_LAYOUT_5POINT1
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define MP4DecSpecificDescrTag
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int initial_padding
Audio only.
int flags
AV_CODEC_FLAG_*.
simple assert() macros that are a bit more flexible than ISO C assert().
#define AV_CH_LAYOUT_QUAD
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define FF_PROFILE_AAC_ELD
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
uint64_t channel_layout
Audio channel layout.
const uint8_t * buffer_end
#define AV_CH_STEREO_RIGHT
See AV_CH_STEREO_LEFT.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
static int ff_bufqueue_is_full(struct FFBufQueue *queue)
Test if a buffer queue is full.
#define FF_PROFILE_AAC_LOW
#define FF_PROFILE_UNKNOWN
AudioConverterRef converter
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
int frame_size
Number of samples per channel in an audio frame.
Libavcodec external API header.
#define MP4DecConfigDescrTag
unsigned short available
number of available buffers
int sample_rate
samples per second
main external API structure.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AV_CH_TOP_BACK_RIGHT
static const uint16_t channel_layouts[7]
Describe the class of an AVClass context structure.
#define AV_CH_LAYOUT_7POINT1_WIDE_BACK
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
#define AV_CH_LAYOUT_OCTAGONAL
const VDPAUPixFmtMap * map
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
int global_quality
Global quality for codecs which cannot change it per frame.
#define flags(name, subs,...)
#define AV_CH_BACK_CENTER
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
common internal api header.
channel
Use these values when setting the channel map with ebur128_set_channel().
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define MKBETAG(a, b, c, d)
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int channels
number of audio channels
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
#define FF_PROFILE_AAC_HE
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
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
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
mode
Use these values in ebur128_init (or'ed).
int nb_samples
number of audio samples (per channel) described by this frame
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
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
int64_t rc_max_rate
maximum bitrate