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

AAC encoder. More...

#include <float.h>
#include "libavutil/channel_layout.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 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 382 of file aacenc.c.

◆ NMR_IS_IMG_GATE

#define NMR_IS_IMG_GATE   8000.0f

Definition at line 581 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_IS_LOW_LIMIT

#define NMR_IS_LOW_LIMIT   6100

Definition at line 584 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_MS_EQUIV

#define NMR_MS_EQUIV   0.5f

Definition at line 587 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_MS_MASK

#define NMR_MS_MASK   0.0f

Definition at line 588 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_DECORR_LO

#define NMR_DECORR_LO   0.20f

Definition at line 593 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_STICKY

#define NMR_STICKY   2.0f

Definition at line 596 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_SDEC_EMA

#define NMR_SDEC_EMA   0.75f

Definition at line 599 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ NMR_PNS_STEREO_DECORR

#define NMR_PNS_STEREO_DECORR   0.6f

Definition at line 602 of file aacenc.c.

Referenced by nmr_decide_stereo().

◆ AACENC_FLAGS

Definition at line 1722 of file aacenc.c.

Function Documentation

◆ put_pce()

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

Definition at line 301 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 342 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 387 of file aacenc.c.

◆ WINDOW_FUNC() [2/4]

WINDOW_FUNC ( long_start )

Definition at line 397 of file aacenc.c.

◆ WINDOW_FUNC() [3/4]

WINDOW_FUNC ( long_stop )

Definition at line 409 of file aacenc.c.

◆ WINDOW_FUNC() [4/4]

WINDOW_FUNC ( eight_short )

Definition at line 421 of file aacenc.c.

◆ apply_window_and_mdct()

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

Definition at line 447 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 468 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 489 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 503 of file aacenc.c.

Referenced by aac_encode_frame().

◆ apply_intensity_stereo()

static void apply_intensity_stereo ( ChannelElement * cpe)
static

Definition at line 551 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 605 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 629 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 661 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 691 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 850 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 885 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 899 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 935 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 954 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 982 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 1002 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 1023 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 1044 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 1066 of file aacenc.c.

◆ aac_encode_end()

static av_cold int aac_encode_end ( AVCodecContext * avctx)
static

Definition at line 1499 of file aacenc.c.

◆ dsp_init()

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

Definition at line 1525 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 1544 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 1563 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 90 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 438 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},
{NULL}
}
#define AACENC_FLAGS
Definition aacenc.c:1722
@ 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 1723 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:1723
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 1738 of file aacenc.c.

◆ aac_encode_defaults

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

Definition at line 1745 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:1738
static av_cold int aac_encode_end(AVCodecContext *avctx)
Definition aacenc.c:1499
static av_cold int aac_encode_init(AVCodecContext *avctx)
Definition aacenc.c:1563
static const FFCodecDefault aac_encode_defaults[]
Definition aacenc.c:1745
static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition aacenc.c:1066
#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:258

Definition at line 1750 of file aacenc.c.