FFmpeg
Loading...
Searching...
No Matches
aaccoder_nmr.h File Reference
#include <float.h>
#include <string.h>
#include "libavutil/mathematics.h"
#include "mathops.h"
#include "avcodec.h"
#include "put_bits.h"
#include "aac.h"
#include "aacenc.h"
#include "aactab.h"
#include "aacenctab.h"

Go to the source code of this file.

Macros

#define NMR_SFBITS(d)
 AAC encoder NMR scalefactor coder.
 
#define NMR_ITERS   14 /* lambda binary-search iters */
 
#define NMR_IFINE   9 /* fine-pass lambda iters */
 
#define NMR_CITERS   7 /* coarse-pass lambda iters */
 
#define NMR_CWARM
 
#define NMR_COARSE
 
#define NMR_STEP   1 /* fine-pass scalefactor candidate granularity */
 
#define NMR_PNS_BITS   9 /* approx cost in bits of signalling PNS */
 
#define NMR_PNS_HOLE_FRAC   0.5f
 
#define NMR_PNS_HOLE_SPREAD   0.5f
 
#define NMR_PNS_HOLE_LAM   20.0f
 
#define NMR_RC_K_CBR   0.5f
 
#define NMR_RC_ITERS   8 /* lambda bisection iters when clamping an over-cap frame */
 
#define NMR_RC_CORR   1.5f
 
#define NMR_RC_CAPK   3.0f
 
#define NMR_VBR_ANCHOR   (-0.75f)
 
#define NMR_VBR_TMIN   (-3.8f)
 
#define NMR_ABR_TMIN   (-3.0f)
 
#define NMR_ABR_TMAX   2.5f
 
#define NMR_ABR_K   0.003f
 
#define NMR_ABR_EMA   0.0023f
 
#define NMR_ABR_STEP   0.15f
 
#define NMR_ABR_HOLD   120
 
#define NMR_ABR_BOOT_GAIN   1.2f
 
#define NMR_ABR_TOL   0.02f
 
#define NMR_ABR_BOOTS   6
 
#define NMR_ABR_SETTLE   100
 
#define NMR_INFL_HF   0.10f
 
#define NMR_CBR_BUF   1536
 
#define NMR_SLEW   1.6f
 
#define NMR_SLEW_RUN   1.15f /* within short runs */
 
#define NMR_RC_CITERS   3 /* corridor coarse-pass iters */
 
#define NMR_TRANS_PM   2.0f
 
#define NMR_ZERO_STICKY   0.5f
 
#define NMR_GROUP_KEEP   0.25f
 
#define NMR_HF_TAPER   0.08f
 
#define NMR_HF_TAPER_KNEE   8000.0f
 
#define NMR_BURST_GAP   10
 
#define NMR_BURST_GAIN   8.0f
 
#define NMR_SHORT_BOOST   2.0f
 
#define NMR_RC_FITERS   4 /* corridor fine-pass iters */
 
#define NMR_RC_TRACK   0.1f /* per-frame pull of the corridor centre toward the realized lambda */
 
#define NMR_PNS_NDGATE   4.0f
 
#define NMR_PNS_MAX_ET   8.0f
 
#define NMR_PNS_LAM   100.0f
 
#define NMR_PNS_ENTER   0.7f
 
#define NMR_PNS_STAY   1.4f
 
#define NMR_PNS_ON   8
 
#define NMR_PNS_OFF   4
 

Functions

static float nmr_solve (AACEncContext *s, const float(*nd)[NMR_NCAND], const int(*nb)[NMR_NCAND], const int *blo, const int *bnc, int step, const int *act, int nact, int destbits, int *chosen, float lo_l, float hi_l, int iters)
 Viterbi over the coding sequence act[0..nact-1] (indices into the per-band curves nd/nb), with lambda binary-searched so the coded size ~ destbits.
 
static int nmr_band_curve (AACEncContext *s, SingleChannelElement *sce, int w, int g, int start, int lo, int step, int maxn, float invthr, float maxval, float *nd_row, int *nb_row)
 
static void nmr_bail_channel (SingleChannelElement *sce)
 
static int nmr_setup_channel (AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, NMRSlot *t)
 
static int nmr_slot_bits (const NMRSlot *t, const int(*nb)[NMR_NCAND], int step)
 
static int nmr_eval_slots (AACEncContext *s, NMRSlot *const *sl, int nsl, int step, float lam)
 
static float nmr_solve_slots (AACEncContext *s, NMRSlot *const *sl, int nsl, int step, int destbits, float lo_l, float hi_l, int iters)
 
static float nmr_nd_stat (AACEncContext *s, NMRSlot *const *sl, int nsl)
 
static float nmr_solve_slots_nd (AACEncContext *s, NMRSlot *const *sl, int nsl, int step, float target, float lo_l, float hi_l, int iters)
 
static void nmr_commit_channel (AACEncContext *s, NMRSlot *t)
 
static void nmr_solve_group (AVCodecContext *avctx, AACEncContext *s, const float lambda, NMRSlot *const *sl, int nsl, int chans, int rc_eligible, int rc_global, int rc_rate_frame, int rc_bmax)
 
static void search_for_quantizers_nmr (AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
 

Macro Definition Documentation

◆ NMR_SFBITS

#define NMR_SFBITS ( d)
Value:
#define SCALE_MAX_DIFF
maximum scalefactor difference allowed by standard
Definition aac.h:94
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
Definition aac.h:95
const uint8_t ff_aac_scalefactor_bits[121]
Definition aactab.c:200
#define av_clip
Definition common.h:100

AAC encoder NMR scalefactor coder.

Optimizes the same noise-to-mask objective as the two-loop coder, but with an optimal Viterbi search over scalefactors instead of a heuristic loop. For each coded band the per-scalefactor distortion/bits curve is precomputed, then a trellis over the (window-group, band) coding sequence minimizes sum_g = dist_g(sf_g)/threshold_g + lambda * (spectral_bits_g(sf_g) + scalefactor_differential_bits) with |sf_g - sf_{g-1}| <= SCALE_MAX_DIFF as a constraint, and lambda binary-searched so the coded size meets the per-frame bit budget

Perceptual noise substitution (PNS) is integrated into the same objective: once the trellis settles on its operating lambda, each noise-like band (flagged by mark_pns) is offered a terminal "code as noise" candidate whose cost is nmr_pns + lambda*NMR_PNS_BITS. Because NMR_PNS_BITS is far below a band's spectral bit count, this candidate only wins when lambda is large, i.e. when the encoder is struggling to hold the bitrate. The bits freed by the chosen PNS bands are then re-spent by a second trellis pass over the remaining bands.

Definition at line 58 of file aaccoder_nmr.h.

Referenced by nmr_solve().

◆ NMR_ITERS

#define NMR_ITERS   14 /* lambda binary-search iters */

Definition at line 60 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_IFINE

#define NMR_IFINE   9 /* fine-pass lambda iters */

Definition at line 61 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_CITERS

#define NMR_CITERS   7 /* coarse-pass lambda iters */

Definition at line 62 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_CWARM

#define NMR_CWARM
Value:
5 /* coarse-pass iters when warm-started off the previous frame's
* lambda: the bracket spans 10 octaves instead of ~43, so fewer
* bisection steps reach the same resolution */

Definition at line 63 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_COARSE

#define NMR_COARSE
Value:
8 /* two-pass coarse->fine grid step, cuts the Viterbi ncand^2 with no
* quality loss, 0 disables it (single full-resolution pass) */

Definition at line 64 of file aaccoder_nmr.h.

Referenced by nmr_setup_channel(), and nmr_solve_group().

◆ NMR_STEP

#define NMR_STEP   1 /* fine-pass scalefactor candidate granularity */

Definition at line 65 of file aaccoder_nmr.h.

Referenced by nmr_commit_channel(), nmr_setup_channel(), and nmr_solve_group().

◆ NMR_PNS_BITS

#define NMR_PNS_BITS   9 /* approx cost in bits of signalling PNS */

Definition at line 67 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_PNS_HOLE_FRAC

#define NMR_PNS_HOLE_FRAC   0.5f

Definition at line 71 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_PNS_HOLE_SPREAD

#define NMR_PNS_HOLE_SPREAD   0.5f

Definition at line 72 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_PNS_HOLE_LAM

#define NMR_PNS_HOLE_LAM   20.0f

Definition at line 75 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_RC_K_CBR

#define NMR_RC_K_CBR   0.5f

Definition at line 79 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_RC_ITERS

#define NMR_RC_ITERS   8 /* lambda bisection iters when clamping an over-cap frame */

Definition at line 81 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_RC_CORR

#define NMR_RC_CORR   1.5f

Definition at line 84 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_RC_CAPK

#define NMR_RC_CAPK   3.0f

Definition at line 87 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_VBR_ANCHOR

#define NMR_VBR_ANCHOR   (-0.75f)

Definition at line 103 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_VBR_TMIN

#define NMR_VBR_TMIN   (-3.8f)

Definition at line 104 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_ABR_TMIN

#define NMR_ABR_TMIN   (-3.0f)

Definition at line 110 of file aaccoder_nmr.h.

◆ NMR_ABR_TMAX

#define NMR_ABR_TMAX   2.5f

Definition at line 111 of file aaccoder_nmr.h.

◆ NMR_ABR_K

#define NMR_ABR_K   0.003f

Definition at line 115 of file aaccoder_nmr.h.

◆ NMR_ABR_EMA

#define NMR_ABR_EMA   0.0023f

Definition at line 116 of file aaccoder_nmr.h.

◆ NMR_ABR_STEP

#define NMR_ABR_STEP   0.15f

Definition at line 117 of file aaccoder_nmr.h.

◆ NMR_ABR_HOLD

#define NMR_ABR_HOLD   120

Definition at line 118 of file aaccoder_nmr.h.

◆ NMR_ABR_BOOT_GAIN

#define NMR_ABR_BOOT_GAIN   1.2f

Definition at line 121 of file aaccoder_nmr.h.

◆ NMR_ABR_TOL

#define NMR_ABR_TOL   0.02f

Definition at line 122 of file aaccoder_nmr.h.

◆ NMR_ABR_BOOTS

#define NMR_ABR_BOOTS   6

Definition at line 123 of file aaccoder_nmr.h.

◆ NMR_ABR_SETTLE

#define NMR_ABR_SETTLE   100

Definition at line 124 of file aaccoder_nmr.h.

◆ NMR_INFL_HF

#define NMR_INFL_HF   0.10f

Definition at line 128 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_CBR_BUF

#define NMR_CBR_BUF   1536

Definition at line 131 of file aaccoder_nmr.h.

◆ NMR_SLEW

#define NMR_SLEW   1.6f

Definition at line 134 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_SLEW_RUN

#define NMR_SLEW_RUN   1.15f /* within short runs */

Definition at line 135 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_RC_CITERS

#define NMR_RC_CITERS   3 /* corridor coarse-pass iters */

Definition at line 136 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_TRANS_PM

#define NMR_TRANS_PM   2.0f

Definition at line 140 of file aaccoder_nmr.h.

Referenced by nmr_setup_channel().

◆ NMR_ZERO_STICKY

#define NMR_ZERO_STICKY   0.5f

Definition at line 144 of file aaccoder_nmr.h.

Referenced by nmr_setup_channel().

◆ NMR_GROUP_KEEP

#define NMR_GROUP_KEEP   0.25f

Definition at line 148 of file aaccoder_nmr.h.

Referenced by nmr_setup_channel().

◆ NMR_HF_TAPER

#define NMR_HF_TAPER   0.08f

Definition at line 152 of file aaccoder_nmr.h.

Referenced by nmr_setup_channel().

◆ NMR_HF_TAPER_KNEE

#define NMR_HF_TAPER_KNEE   8000.0f

Definition at line 153 of file aaccoder_nmr.h.

Referenced by nmr_setup_channel().

◆ NMR_BURST_GAP

#define NMR_BURST_GAP   10

Definition at line 157 of file aaccoder_nmr.h.

◆ NMR_BURST_GAIN

#define NMR_BURST_GAIN   8.0f

Definition at line 158 of file aaccoder_nmr.h.

◆ NMR_SHORT_BOOST

#define NMR_SHORT_BOOST   2.0f

Definition at line 161 of file aaccoder_nmr.h.

◆ NMR_RC_FITERS

#define NMR_RC_FITERS   4 /* corridor fine-pass iters */

Definition at line 162 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_RC_TRACK

#define NMR_RC_TRACK   0.1f /* per-frame pull of the corridor centre toward the realized lambda */

Definition at line 163 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_PNS_NDGATE

#define NMR_PNS_NDGATE   4.0f

Definition at line 166 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_PNS_MAX_ET

#define NMR_PNS_MAX_ET   8.0f

Definition at line 170 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_PNS_LAM

#define NMR_PNS_LAM   100.0f

Definition at line 174 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_PNS_ENTER

#define NMR_PNS_ENTER   0.7f

Definition at line 177 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_PNS_STAY

#define NMR_PNS_STAY   1.4f

Definition at line 178 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_PNS_ON

#define NMR_PNS_ON   8

Definition at line 181 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ NMR_PNS_OFF

#define NMR_PNS_OFF   4

Definition at line 182 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

Function Documentation

◆ nmr_solve()

static float nmr_solve ( AACEncContext * s,
const float(*) nd[NMR_NCAND],
const int(*) nb[NMR_NCAND],
const int * blo,
const int * bnc,
int step,
const int * act,
int nact,
int destbits,
int * chosen,
float lo_l,
float hi_l,
int iters )
static

Viterbi over the coding sequence act[0..nact-1] (indices into the per-band curves nd/nb), with lambda binary-searched so the coded size ~ destbits.

Fills chosen[band] for every band referenced by act. Returns the operating lambda. node cost = dist/threshold + lambda*spectral_bits; edge cost = lambda*sf_differential_bits; |delta sf| <= SCALE_MAX_DIFF hard.

Definition at line 191 of file aaccoder_nmr.h.

◆ nmr_band_curve()

static int nmr_band_curve ( AACEncContext * s,
SingleChannelElement * sce,
int w,
int g,
int start,
int lo,
int step,
int maxn,
float invthr,
float maxval,
float * nd_row,
int * nb_row )
static

Definition at line 255 of file aaccoder_nmr.h.

Referenced by nmr_setup_channel(), and nmr_solve_group().

◆ nmr_bail_channel()

static void nmr_bail_channel ( SingleChannelElement * sce)
static

Definition at line 281 of file aaccoder_nmr.h.

◆ nmr_setup_channel()

static int nmr_setup_channel ( AVCodecContext * avctx,
AACEncContext * s,
SingleChannelElement * sce,
NMRSlot * t )
static

Definition at line 294 of file aaccoder_nmr.h.

◆ nmr_slot_bits()

static int nmr_slot_bits ( const NMRSlot * t,
const int(*) nb[NMR_NCAND],
int step )
static

Definition at line 550 of file aaccoder_nmr.h.

Referenced by nmr_commit_channel(), and nmr_solve_group().

◆ nmr_eval_slots()

static int nmr_eval_slots ( AACEncContext * s,
NMRSlot *const * sl,
int nsl,
int step,
float lam )
static

Definition at line 562 of file aaccoder_nmr.h.

Referenced by nmr_solve_group(), nmr_solve_slots(), and nmr_solve_slots_nd().

◆ nmr_solve_slots()

static float nmr_solve_slots ( AACEncContext * s,
NMRSlot *const * sl,
int nsl,
int step,
int destbits,
float lo_l,
float hi_l,
int iters )
static

Definition at line 579 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ nmr_nd_stat()

static float nmr_nd_stat ( AACEncContext * s,
NMRSlot *const * sl,
int nsl )
static

Definition at line 601 of file aaccoder_nmr.h.

Referenced by nmr_solve_group(), and nmr_solve_slots_nd().

◆ nmr_solve_slots_nd()

static float nmr_solve_slots_nd ( AACEncContext * s,
NMRSlot *const * sl,
int nsl,
int step,
float target,
float lo_l,
float hi_l,
int iters )
static

Definition at line 626 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ nmr_commit_channel()

static void nmr_commit_channel ( AACEncContext * s,
NMRSlot * t )
static

Definition at line 648 of file aaccoder_nmr.h.

Referenced by nmr_solve_group().

◆ nmr_solve_group()

static void nmr_solve_group ( AVCodecContext * avctx,
AACEncContext * s,
const float lambda,
NMRSlot *const * sl,
int nsl,
int chans,
int rc_eligible,
int rc_global,
int rc_rate_frame,
int rc_bmax )
static

Definition at line 727 of file aaccoder_nmr.h.

◆ search_for_quantizers_nmr()

static void search_for_quantizers_nmr ( AVCodecContext * avctx,
AACEncContext * s,
SingleChannelElement * sce,
const float lambda )
static

Definition at line 1345 of file aaccoder_nmr.h.