FFmpeg
Macros | Functions | Variables
aacenc.c File Reference
#include <float.h>
#include "libavutil/channel_layout.h"
#include "libavutil/libm.h"
#include "libavutil/float_dsp.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "codec_internal.h"
#include "encode.h"
#include "put_bits.h"
#include "mpeg4audio.h"
#include "sinewin.h"
#include "profiles.h"
#include "version.h"
#include "aac.h"
#include "aactab.h"
#include "aacenc.h"
#include "aacenctab.h"
#include "aacenc_utils.h"
#include "psymodel.h"

Go to the source code of this file.

Macros

#define WINDOW_FUNC(type)
 
#define AACENC_FLAGS   AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
 

Functions

static void put_pce (PutBitContext *pb, AVCodecContext *avctx)
 
static int put_audio_specific_config (AVCodecContext *avctx)
 Make AAC audio config object. More...
 
void ff_quantize_band_cost_cache_init (struct AACEncContext *s)
 
 WINDOW_FUNC (only_long)
 
 WINDOW_FUNC (long_start)
 
 WINDOW_FUNC (long_stop)
 
 WINDOW_FUNC (eight_short)
 
static void apply_window_and_mdct (AACEncContext *s, SingleChannelElement *sce, float *audio)
 
static void put_ics_info (AACEncContext *s, IndividualChannelStream *info)
 Encode ics_info element. More...
 
static void encode_ms_info (PutBitContext *pb, ChannelElement *cpe)
 Encode MS data. More...
 
static void adjust_frame_information (ChannelElement *cpe, int chans)
 Produce integer coefficients from scalefactors provided by the model. More...
 
static void apply_intensity_stereo (ChannelElement *cpe)
 
static void apply_mid_side_stereo (ChannelElement *cpe)
 
static void encode_band_info (AACEncContext *s, SingleChannelElement *sce)
 Encode scalefactor band coding type. More...
 
static void encode_scale_factors (AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce)
 Encode scalefactors. More...
 
static void encode_pulses (AACEncContext *s, Pulse *pulse)
 Encode pulse data. More...
 
static void encode_spectral_coeffs (AACEncContext *s, SingleChannelElement *sce)
 Encode spectral coefficients processed by psychoacoustic model. More...
 
static void avoid_clipping (AACEncContext *s, SingleChannelElement *sce)
 Downscale spectral coefficients for near-clipping windows to avoid artifacts. More...
 
static int encode_individual_channel (AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, int common_window)
 Encode one channel of audio data. More...
 
static void put_bitstream_info (AACEncContext *s, const char *name)
 Write some auxiliary information about the created AAC file. More...
 
static void copy_input_samples (AACEncContext *s, const AVFrame *frame)
 
static int aac_encode_frame (AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
 
static av_cold int aac_encode_end (AVCodecContext *avctx)
 
static av_cold int dsp_init (AVCodecContext *avctx, AACEncContext *s)
 
static av_cold int alloc_buffers (AVCodecContext *avctx, AACEncContext *s)
 
static av_cold int aac_encode_init (AVCodecContext *avctx)
 

Variables

static const AACPCEInfo aac_pce_configs []
 List of PCE (Program Configuration Element) for the channel layouts listed in channel_layout.h. More...
 
static void(*const apply_window [4])(AVFloatDSPContext *fdsp, SingleChannelElement *sce, const float *audio)
 
static const AVOption aacenc_options []
 
static const AVClass aacenc_class
 
static const FFCodecDefault aac_encode_defaults []
 
const FFCodec ff_aac_encoder
 

Detailed Description

AAC encoder

Definition in file aacenc.c.

Macro Definition Documentation

◆ WINDOW_FUNC

#define WINDOW_FUNC (   type)
Value:
static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \
const float *audio)

Definition at line 409 of file aacenc.c.

◆ AACENC_FLAGS

Definition at line 1386 of file aacenc.c.

Function Documentation

◆ put_pce()

static void put_pce ( PutBitContext pb,
AVCodecContext avctx 
)
static

Definition at line 327 of file aacenc.c.

Referenced by put_audio_specific_config().

◆ put_audio_specific_config()

static int put_audio_specific_config ( AVCodecContext avctx)
static

Make AAC audio config object.

See also
1.6.2.1 "Syntax - AudioSpecificConfig"

Definition at line 368 of file aacenc.c.

Referenced by aac_encode_init().

◆ ff_quantize_band_cost_cache_init()

void ff_quantize_band_cost_cache_init ( struct AACEncContext s)

Definition at line 400 of file aacenc.c.

Referenced by search_for_quantizers_fast(), and search_for_quantizers_twoloop().

◆ WINDOW_FUNC() [1/4]

WINDOW_FUNC ( only_long  )

Definition at line 414 of file aacenc.c.

◆ WINDOW_FUNC() [2/4]

WINDOW_FUNC ( long_start  )

Definition at line 424 of file aacenc.c.

◆ WINDOW_FUNC() [3/4]

WINDOW_FUNC ( long_stop  )

Definition at line 436 of file aacenc.c.

◆ WINDOW_FUNC() [4/4]

WINDOW_FUNC ( eight_short  )

Definition at line 448 of file aacenc.c.

◆ apply_window_and_mdct()

static void apply_window_and_mdct ( AACEncContext s,
SingleChannelElement sce,
float audio 
)
static

Definition at line 474 of file aacenc.c.

Referenced by aac_encode_frame().

◆ put_ics_info()

static void put_ics_info ( AACEncContext s,
IndividualChannelStream info 
)
static

Encode ics_info element.

See also
Table 4.6 (syntax of ics_info)

Definition at line 495 of file aacenc.c.

Referenced by aac_encode_frame(), and encode_individual_channel().

◆ encode_ms_info()

static void encode_ms_info ( PutBitContext pb,
ChannelElement cpe 
)
static

Encode MS data.

See also
4.6.8.1 "Joint Coding - M/S Stereo"

Definition at line 516 of file aacenc.c.

Referenced by aac_encode_frame().

◆ adjust_frame_information()

static void adjust_frame_information ( ChannelElement cpe,
int  chans 
)
static

Produce integer coefficients from scalefactors provided by the model.

Definition at line 530 of file aacenc.c.

Referenced by aac_encode_frame().

◆ apply_intensity_stereo()

static void apply_intensity_stereo ( ChannelElement cpe)
static

Definition at line 580 of file aacenc.c.

Referenced by aac_encode_frame().

◆ apply_mid_side_stereo()

static void apply_mid_side_stereo ( ChannelElement cpe)
static

Definition at line 609 of file aacenc.c.

Referenced by aac_encode_frame().

◆ encode_band_info()

static void encode_band_info ( AACEncContext s,
SingleChannelElement sce 
)
static

Encode scalefactor band coding type.

Definition at line 644 of file aacenc.c.

Referenced by encode_individual_channel().

◆ encode_scale_factors()

static void encode_scale_factors ( AVCodecContext avctx,
AACEncContext s,
SingleChannelElement sce 
)
static

Encode scalefactors.

Definition at line 658 of file aacenc.c.

Referenced by encode_individual_channel().

◆ encode_pulses()

static void encode_pulses ( AACEncContext s,
Pulse pulse 
)
static

Encode pulse data.

Definition at line 694 of file aacenc.c.

Referenced by encode_individual_channel().

◆ encode_spectral_coeffs()

static void encode_spectral_coeffs ( AACEncContext s,
SingleChannelElement sce 
)
static

Encode spectral coefficients processed by psychoacoustic model.

Definition at line 713 of file aacenc.c.

Referenced by encode_individual_channel().

◆ avoid_clipping()

static void avoid_clipping ( AACEncContext s,
SingleChannelElement sce 
)
static

Downscale spectral coefficients for near-clipping windows to avoid artifacts.

Definition at line 741 of file aacenc.c.

Referenced by aac_encode_frame().

◆ encode_individual_channel()

static int encode_individual_channel ( AVCodecContext avctx,
AACEncContext s,
SingleChannelElement sce,
int  common_window 
)
static

Encode one channel of audio data.

Definition at line 761 of file aacenc.c.

Referenced by aac_encode_frame().

◆ put_bitstream_info()

static void put_bitstream_info ( AACEncContext s,
const char *  name 
)
static

Write some auxiliary information about the created AAC file.

Definition at line 787 of file aacenc.c.

Referenced by aac_encode_frame().

◆ copy_input_samples()

static void copy_input_samples ( AACEncContext s,
const AVFrame frame 
)
static

Definition at line 808 of file aacenc.c.

Referenced by aac_encode_frame().

◆ aac_encode_frame()

static int aac_encode_frame ( AVCodecContext avctx,
AVPacket avpkt,
const AVFrame frame,
int got_packet_ptr 
)
static

Definition at line 830 of file aacenc.c.

◆ aac_encode_end()

static av_cold int aac_encode_end ( AVCodecContext avctx)
static

Definition at line 1173 of file aacenc.c.

◆ dsp_init()

static av_cold int dsp_init ( AVCodecContext avctx,
AACEncContext s 
)
static

Definition at line 1192 of file aacenc.c.

Referenced by aac_encode_init().

◆ alloc_buffers()

static av_cold int alloc_buffers ( AVCodecContext avctx,
AACEncContext s 
)
static

Definition at line 1211 of file aacenc.c.

Referenced by aac_encode_init().

◆ aac_encode_init()

static av_cold int aac_encode_init ( AVCodecContext avctx)
static

Definition at line 1224 of file aacenc.c.

Variable Documentation

◆ aac_pce_configs

const AACPCEInfo aac_pce_configs[]
static

List of PCE (Program Configuration Element) for the channel layouts listed in channel_layout.h.

For those wishing in the future to add other layouts:

  • num_ele: number of elements in each group of front, side, back, lfe channels (an element is of type SCE (single channel), CPE (channel pair) for the first 3 groups; and is LFE for LFE group).
  • pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group
  • index: there are three independent indices for SCE, CPE and LFE; they are incremented irrespective of the group to which the element belongs; they are not reset when going from one group to another

    Example: for 7.0 channel layout, .pairing = { { 1, 0 }, { 1 }, { 1 }, }, (3 CPE and 1 SCE in front group) .index = { { 0, 0 }, { 1 }, { 2 }, }, (index is 0 for the single SCE but goes from 0 to 2 for the CPEs)

    The index order impacts the channel ordering. But is otherwise arbitrary (the sequence could have been 2, 0, 1 instead of 0, 1, 2).

    Spec allows for discontinuous indices, e.g. if one has a total of two SCE, SCE.0 SCE.15 is OK per spec; BUT it won't be decoded by our AAC decoder which at this time requires that indices fully cover some range starting from 0 (SCE.1 SCE.0 is OK but not SCE.0 SCE.15).

  • config_map: total number of elements and their types. Beware, the way the types are ordered impacts the final channel ordering.
  • reorder_map: reorders the channels.

Definition at line 89 of file aacenc.c.

Referenced by aac_encode_init().

◆ apply_window

void(*const apply_window[4])(AVFloatDSPContext *fdsp, SingleChannelElement *sce, const float *audio)
static
Initial value:
= {
[ONLY_LONG_SEQUENCE] = apply_only_long_window,
[LONG_START_SEQUENCE] = apply_long_start_window,
[EIGHT_SHORT_SEQUENCE] = apply_eight_short_window,
[LONG_STOP_SEQUENCE] = apply_long_stop_window
}

Definition at line 465 of file aacenc.c.

Referenced by aac_encode_frame(), apply_window_and_mdct(), and ff_mpa_synth_filter_TMPL().

◆ aacenc_options

const AVOption aacenc_options[]
static
Initial value:
= {
{"aac_coder", "Coding algorithm", 0x42, AV_OPT_TYPE_INT, {.i64 = AAC_CODER_TWOLOOP}, 0, AAC_CODER_NB-1, AACENC_FLAGS, "coder"},
{"anmr", "ANMR method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_ANMR}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
{"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
{"fast", "Default fast search", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
{"aac_ms", "Force M/S stereo coding", 0x42, AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AACENC_FLAGS},
{"aac_is", "Intensity stereo coding", 0x42, AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
{"aac_pns", "Perceptual noise substitution", 0x42, AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
{"aac_tns", "Temporal noise shaping", 0x42, AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
{"aac_ltp", "Long term prediction", 0x42, AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
{"aac_pred", "AAC-Main prediction", 0x42, AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
{"aac_pce", "Forces the use of PCEs", 0x42, AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
{NULL}
}

Definition at line 1387 of file aacenc.c.

◆ aacenc_class

const AVClass aacenc_class
static
Initial value:
= {
.class_name = "AAC encoder",
.item_name = av_default_item_name,
.option = aacenc_options,
}

Definition at line 1403 of file aacenc.c.

◆ aac_encode_defaults

const FFCodecDefault aac_encode_defaults[]
static
Initial value:
= {
{ "b", "0" },
{ NULL }
}

Definition at line 1410 of file aacenc.c.

◆ ff_aac_encoder

const FFCodec ff_aac_encoder
Initial value:
= {
.p.name = "aac",
CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
.p.type = AVMEDIA_TYPE_AUDIO,
.p.id = AV_CODEC_ID_AAC,
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
.priv_data_size = sizeof(AACEncContext),
.close = aac_encode_end,
.defaults = aac_encode_defaults,
.p.supported_samplerates = ff_mpeg4audio_sample_rates,
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
.p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
.p.priv_class = &aacenc_class,
}

Definition at line 1415 of file aacenc.c.

AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
aacenc_class
static const AVClass aacenc_class
Definition: aacenc.c:1403
aac_encode_init
static av_cold int aac_encode_init(AVCodecContext *avctx)
Definition: aacenc.c:1224
AAC_CODER_NB
@ AAC_CODER_NB
Definition: aacenc.h:43
AAC_CODER_FAST
@ AAC_CODER_FAST
Definition: aacenc.h:41
init
static int init
Definition: av_tx.c:47
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:307
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
EIGHT_SHORT_SEQUENCE
@ EIGHT_SHORT_SEQUENCE
Definition: aac.h:80
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:436
aac_encode_frame
static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: aacenc.c:830
AAC_CODER_ANMR
@ AAC_CODER_ANMR
Definition: aacenc.h:39
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
ONLY_LONG_SEQUENCE
@ ONLY_LONG_SEQUENCE
Definition: aac.h:78
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AVFloatDSPContext
Definition: float_dsp.h:24
AAC_CODER_TWOLOOP
@ AAC_CODER_TWOLOOP
Definition: aacenc.h:40
SingleChannelElement
Single Channel Element - used for both SCE and LFE elements.
Definition: aac.h:249
aacenc_options
static const AVOption aacenc_options[]
Definition: aacenc.c:1387
LONG_STOP_SEQUENCE
@ LONG_STOP_SEQUENCE
Definition: aac.h:81
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AACENC_FLAGS
#define AACENC_FLAGS
Definition: aacenc.c:1386
aac_encode_defaults
static const FFCodecDefault aac_encode_defaults[]
Definition: aacenc.c:1410
LONG_START_SEQUENCE
@ LONG_START_SEQUENCE
Definition: aac.h:79
AACEncContext
AAC encoder context.
Definition: aacenc.h:108
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:82
ff_mpeg4audio_sample_rates
const int ff_mpeg4audio_sample_rates[16]
Definition: mpeg4audio_sample_rates.h:26
aac_encode_end
static av_cold int aac_encode_end(AVCodecContext *avctx)
Definition: aacenc.c:1173
FF_AAC_PROFILE_OPTS
#define FF_AAC_PROFILE_OPTS
Definition: profiles.h:28
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:87
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234