FFmpeg
Loading...
Searching...
No Matches
aacenc.c File Reference

AAC encoder. More...

#include <float.h>
#include "libavutil/channel_layout.h"
#include "libavutil/crc.h"
#include "libavutil/libm.h"
#include "libavutil/float_dsp.h"
#include "libavutil/mem.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 NMR_IS_IMG_GATE   8000.0f
 
#define NMR_IS_LOW_LIMIT   6100
 
#define NMR_MS_EQUIV   0.5f
 
#define NMR_MS_MASK   0.0f
 
#define NMR_DECORR_LO   0.20f
 
#define NMR_STICKY   2.0f
 
#define NMR_SDEC_EMA   0.75f
 
#define NMR_PNS_STEREO_DECORR   0.6f
 
#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, int chcfg)
 Make AAC audio config object.
 
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.
 
static void encode_ms_info (PutBitContext *pb, ChannelElement *cpe)
 Encode MS data.
 
static void adjust_frame_information (ChannelElement *cpe, int chans)
 Produce integer coefficients from scalefactors provided by the model.
 
static void apply_intensity_stereo (ChannelElement *cpe)
 
static void nmr_apply_ms_band (AACEncContext *s, ChannelElement *cpe, int w, int g, int start, int len, int gl)
 
static int nmr_is_image_masked (AACEncContext *s, ChannelElement *cpe, int w, int g, int start, int len, int gl, float ener0, float ener1, float dot, float minthr0, float minthr1, float *ratio_out, float *scale_out, float *sr_out, int *p_out)
 
static void nmr_apply_is_band (AACEncContext *s, ChannelElement *cpe, int w, int g, int start, int len, int gl, float scale, float sr_, int p, float ener0, float ener1)
 
static void nmr_decide_stereo (AACEncContext *s, ChannelElement *cpe)
 
static void apply_mid_side_stereo (ChannelElement *cpe)
 
static void encode_band_info (AACEncContext *s, SingleChannelElement *sce)
 Encode scalefactor band coding type.
 
static void encode_scale_factors (AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce)
 Encode scalefactors.
 
static void encode_pulses (AACEncContext *s, Pulse *pulse)
 Encode pulse data.
 
static void encode_spectral_coeffs (AACEncContext *s, SingleChannelElement *sce)
 Encode spectral coefficients processed by psychoacoustic model.
 
static void avoid_clipping (AACEncContext *s, SingleChannelElement *sce)
 Downscale spectral coefficients for near-clipping windows to avoid artifacts.
 
static int encode_individual_channel (AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, int common_window)
 Encode one channel of audio data.
 
static void put_bitstream_info (AACEncContext *s, const char *name)
 Write some auxiliary information about the created AAC file.
 
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 check_height_ext (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.
 
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)
cl_device_type type
Single Channel Element - used for both SCE and LFE elements.
Definition aacdec.h:217

Definition at line 534 of file aacenc.c.

◆ NMR_IS_IMG_GATE

#define NMR_IS_IMG_GATE   8000.0f

Definition at line 733 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_IS_LOW_LIMIT

#define NMR_IS_LOW_LIMIT   6100

Definition at line 736 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_MS_EQUIV

#define NMR_MS_EQUIV   0.5f

Definition at line 739 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_MS_MASK

#define NMR_MS_MASK   0.0f

Definition at line 740 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_DECORR_LO

#define NMR_DECORR_LO   0.20f

Definition at line 745 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_STICKY

#define NMR_STICKY   2.0f

Definition at line 748 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_SDEC_EMA

#define NMR_SDEC_EMA   0.75f

Definition at line 751 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_PNS_STEREO_DECORR

#define NMR_PNS_STEREO_DECORR   0.6f

Definition at line 754 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ AACENC_FLAGS

Definition at line 1901 of file aacenc.c.

Function Documentation

◆ put_pce()

static void put_pce ( PutBitContext * pb,
AVCodecContext * avctx )
static

Definition at line 433 of file aacenc.c.

Referenced by put_audio_specific_config().

◆ put_audio_specific_config()

static int put_audio_specific_config ( AVCodecContext * avctx,
int chcfg )
static

Make AAC audio config object.

See also
1.6.2.1 "Syntax - AudioSpecificConfig"

Definition at line 494 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)

◆ WINDOW_FUNC() [1/4]

WINDOW_FUNC ( only_long )

Definition at line 539 of file aacenc.c.

◆ WINDOW_FUNC() [2/4]

WINDOW_FUNC ( long_start )

Definition at line 549 of file aacenc.c.

◆ WINDOW_FUNC() [3/4]

WINDOW_FUNC ( long_stop )

Definition at line 561 of file aacenc.c.

◆ WINDOW_FUNC() [4/4]

WINDOW_FUNC ( eight_short )

Definition at line 573 of file aacenc.c.

◆ apply_window_and_mdct()

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

Definition at line 599 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 620 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 641 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 655 of file aacenc.c.

Referenced by aac_encode_frame().

◆ apply_intensity_stereo()

static void apply_intensity_stereo ( ChannelElement * cpe)
static

Definition at line 703 of file aacenc.c.

Referenced by aac_encode_frame().

◆ nmr_apply_ms_band()

static void nmr_apply_ms_band ( AACEncContext * s,
ChannelElement * cpe,
int w,
int g,
int start,
int len,
int gl )
static

Definition at line 757 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ nmr_is_image_masked()

static int nmr_is_image_masked ( AACEncContext * s,
ChannelElement * cpe,
int w,
int g,
int start,
int len,
int gl,
float ener0,
float ener1,
float dot,
float minthr0,
float minthr1,
float * ratio_out,
float * scale_out,
float * sr_out,
int * p_out )
static

Definition at line 781 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ nmr_apply_is_band()

static void nmr_apply_is_band ( AACEncContext * s,
ChannelElement * cpe,
int w,
int g,
int start,
int len,
int gl,
float scale,
float sr_,
int p,
float ener0,
float ener1 )
static

Definition at line 813 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ nmr_decide_stereo()

static void nmr_decide_stereo ( AACEncContext * s,
ChannelElement * cpe )
static

Definition at line 843 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 1002 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 1037 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 1051 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 1087 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 1106 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 1134 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 1154 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 1175 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 1196 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 1218 of file aacenc.c.

◆ aac_encode_end()

static av_cold int aac_encode_end ( AVCodecContext * avctx)
static

Definition at line 1651 of file aacenc.c.

◆ dsp_init()

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

Definition at line 1677 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 1696 of file aacenc.c.

Referenced by aac_encode_init().

◆ check_height_ext()

static av_cold int check_height_ext ( AVCodecContext * avctx,
AACEncContext * s )
static

Definition at line 1715 of file aacenc.c.

Referenced by aac_encode_init().

◆ aac_encode_init()

static av_cold int aac_encode_init ( AVCodecContext * avctx)
static

FFmpeg used to produce out-of-spec AAC files that mistagged 7.1 as 7.1(wide), and this wark-around is still enabled by default in aacdec.c, so avoid producing such files in the rare case that the user correctly passed 7.1(wide) channel layout content.

Definition at line 1727 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).

  • height: 0 for a base layer element, 1 for a top layer element, 2 for a bottom layer element.
  • 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 94 of file aacenc.c.

Referenced by aac_encode_init().

◆ apply_window

void(*const apply_window[4])(AVFloatDSPContext *fdsp, SingleChannelElement *sce, const float *audio) ( 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
}
@ EIGHT_SHORT_SEQUENCE
Definition aac.h:66
@ LONG_STOP_SEQUENCE
Definition aac.h:67
@ ONLY_LONG_SEQUENCE
Definition aac.h:64
@ LONG_START_SEQUENCE
Definition aac.h:65

Definition at line 590 of file aacenc.c.

Referenced by apply_window(), apply_window_and_mdct(), de_stereo(), feed(), feed(), ff_lpc_calc_ref_coefs_f(), 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_NMR}, 0, AAC_CODER_NB-1, AACENC_FLAGS, .unit = "coder"},
{"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "coder"},
{"fast", "Fast search", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "coder"},
{"nmr", "Noise-to-mask ratio scalefactor trellis", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_NMR}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "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_pce", "Forces the use of PCEs", 0x42 , AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
{"aac_nmr_speed", "NMR coder speed level: 0 = slowest/best, higher trades quality for speed", 0x42 , AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, AACENC_FLAGS},
{"aac_allow_71wide", "Allow non-PCE use of 7.1(wide) channel layout", 0x42 , AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AACENC_FLAGS},
{NULL}
}
#define AACENC_FLAGS
Definition aacenc.c:1901
@ AAC_CODER_FAST
Definition aacenc.h:46
@ AAC_CODER_NMR
Definition aacenc.h:47
@ AAC_CODER_NB
Definition aacenc.h:49
@ AAC_CODER_TWOLOOP
Definition aacenc.h:45
#define NULL
Definition coverity.c:32
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define FF_AAC_PROFILE_OPTS
Definition profiles.h:29

Definition at line 1902 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,
}
static const AVOption aacenc_options[]
Definition aacenc.c:1902
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
#define LIBAVUTIL_VERSION_INT
Definition version.h:85

Definition at line 1918 of file aacenc.c.

◆ aac_encode_defaults

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

Definition at line 1925 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,
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
.p.priv_class = &aacenc_class,
}
static const AVClass aacenc_class
Definition aacenc.c:1918
static av_cold int aac_encode_end(AVCodecContext *avctx)
Definition aacenc.c:1651
static av_cold int aac_encode_init(AVCodecContext *avctx)
Definition aacenc.c:1727
static const FFCodecDefault aac_encode_defaults[]
Definition aacenc.c:1925
static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition aacenc.c:1218
#define CODEC_SAMPLERATES_ARRAY(array)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#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:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition codec.h:84
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
const int ff_mpeg4audio_sample_rates[16]
AAC encoder context.
Definition aacenc.h:260

Definition at line 1930 of file aacenc.c.