55#define NOISE_SPREAD_THRESHOLD 0.9f
59#define NOISE_LAMBDA_REPLACE 1.948f
74 const float *in,
float *
quant,
const float *scaled,
75 int size,
int scale_idx,
int cb,
76 const float lambda,
const float uplim,
77 int *
bits,
float *energy);
87 const float *scaled,
int size,
int scale_idx,
88 int cb,
const float lambda,
const float uplim,
89 int *
bits,
float *energy,
int BT_ZERO,
int BT_UNSIGNED,
90 int BT_PAIR,
int BT_ESC,
int BT_NOISE,
int BT_STEREO,
97 const float CLIPPED_ESCAPE = 165140.0f*IQ;
100 const int dim = BT_PAIR ? 2 : 4;
104 if (BT_ZERO || BT_NOISE || BT_STEREO) {
113 for (
int j = 0; j <
dim; j++)
119 s->aacdsp.abs_pow34(
s->scoefs, in,
size);
130 int *quants =
s->qcoefs +
i;
133 float quantized, rd = 0.0f;
134 for (
int j = 0; j <
dim; j++) {
136 curidx += quants[j] + off;
141 for (
int j = 0; j <
dim; j++) {
144 if (BT_ESC && vec[j] == 64.0f) {
145 if (t >= CLIPPED_ESCAPE) {
146 quantized = CLIPPED_ESCAPE;
154 quantized = vec[j]*IQ;
158 out[
i+j] = in[
i+j] >= 0 ? quantized : -quantized;
161 qenergy += quantized*quantized;
165 for (
int j = 0; j <
dim; j++) {
166 quantized = vec[j]*IQ;
167 qenergy += quantized*quantized;
169 out[
i+j] = quantized;
170 rd += (in[
i+j] - quantized)*(in[
i+j] - quantized);
173 cost += rd *
lambda + curbits;
180 for (
int j = 0; j <
dim; j++)
184 for (
int j = 0; j < 2; j++) {
205 const float *in,
float *
quant,
const float *scaled,
206 int size,
int scale_idx,
int cb,
207 const float lambda,
const float uplim,
208 int *
bits,
float *energy) {
213#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
214static float quantize_and_encode_band_cost_ ## NAME( \
215 struct AACEncContext *s, \
216 PutBitContext *pb, const float *in, float *quant, \
217 const float *scaled, int size, int scale_idx, \
218 int cb, const float lambda, const float uplim, \
219 int *bits, float *energy) { \
220 return quantize_and_encode_band_cost_template( \
221 s, pb, in, quant, scaled, size, scale_idx, \
222 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \
223 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
239 quantize_and_encode_band_cost_ZERO,
240 quantize_and_encode_band_cost_SQUAD,
241 quantize_and_encode_band_cost_SQUAD,
242 quantize_and_encode_band_cost_UQUAD,
243 quantize_and_encode_band_cost_UQUAD,
244 quantize_and_encode_band_cost_SPAIR,
245 quantize_and_encode_band_cost_SPAIR,
246 quantize_and_encode_band_cost_UPAIR,
247 quantize_and_encode_band_cost_UPAIR,
248 quantize_and_encode_band_cost_UPAIR,
249 quantize_and_encode_band_cost_UPAIR,
250 quantize_and_encode_band_cost_ESC,
252 quantize_and_encode_band_cost_NOISE,
253 quantize_and_encode_band_cost_STEREO,
254 quantize_and_encode_band_cost_STEREO,
259 quantize_and_encode_band_cost_ZERO,
260 quantize_and_encode_band_cost_SQUAD,
261 quantize_and_encode_band_cost_SQUAD,
262 quantize_and_encode_band_cost_UQUAD,
263 quantize_and_encode_band_cost_UQUAD,
264 quantize_and_encode_band_cost_SPAIR,
265 quantize_and_encode_band_cost_SPAIR,
266 quantize_and_encode_band_cost_UPAIR,
267 quantize_and_encode_band_cost_UPAIR,
268 quantize_and_encode_band_cost_UPAIR,
269 quantize_and_encode_band_cost_UPAIR,
270 quantize_and_encode_band_cost_ESC_RTZ,
272 quantize_and_encode_band_cost_NOISE,
273 quantize_and_encode_band_cost_STEREO,
274 quantize_and_encode_band_cost_STEREO,
278 const float *in,
float *
quant,
const float *scaled,
279 int size,
int scale_idx,
int cb,
280 const float lambda,
const float uplim,
281 int *
bits,
float *energy)
289 const float *in,
float *
out,
int size,
int scale_idx,
290 int cb,
const float lambda,
int rtz)
310#define TRELLIS_STAGES 121
311#define TRELLIS_STATES (SCALE_MAX_DIFF+1)
316 int prevscaler_n = -255, prevscaler_i = 0;
328 if (prevscaler_n == -255)
356 int start = 0,
i,
w, w2,
g;
358 float dists[128] = { 0 }, uplims[128] = { 0 };
360 int fflag, minscaler;
367 destbits =
FFMIN(destbits, 5800);
376 FFPsyBand *band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
384 uplims[
w*16+
g] = uplim *512;
388 minthr =
FFMIN(minthr, uplim);
405 s->aacdsp.abs_pow34(
s->scoefs, sce->
coeffs, 1024);
411 const float *scaled =
s->scoefs + start;
421 minscaler = sce->
sf_idx[0];
423 qstep = its ? 1 : 32;
430 const float *coefs = sce->
coeffs + start;
431 const float *scaled =
s->scoefs + start;
453 dists[
w*16+
g] = dist -
bits;
462 if (tbits > destbits) {
463 for (
i = 0;
i < 128;
i++)
464 if (sce->
sf_idx[
i] < 218 - qstep)
467 for (
i = 0;
i < 128;
i++)
468 if (sce->
sf_idx[
i] > 60 - qstep)
472 if (!qstep && tbits > destbits*1.02 && sce->
sf_idx[0] < 217)
482 if (dists[
w*16+
g] > uplims[
w*16+
g] && sce->
sf_idx[
w*16+
g] > 60) {
496 }
while (fflag && its < 10);
504 int bandwidth, cutoff;
505 float *PNS = &
s->scoefs[0*128], *PNS34 = &
s->scoefs[1*128];
506 float *NOR34 = &
s->scoefs[3*128];
507 uint8_t nextband[128];
508 const float lambda =
s->lambda;
509 const float freq_mult = avctx->
sample_rate*0.5f/wlen;
512 const float dist_bias =
av_clipf(4.f * 120 / lambda, 0.25f, 4.0f);
513 const float pns_transient_energy_r =
FFMIN(0.7f, lambda / 140.f);
515 int prev = -1000, prev_sf = -1;
520 bandwidth =
s->bandwidth;
521 cutoff = bandwidth * 2 * wlen / avctx->
sample_rate;
529 float dist1 = 0.0f, dist2 = 0.0f, noise_amp;
530 float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh;
531 float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
532 float min_energy = -1.0f, max_energy = 0.0f;
534 const float freq = (start-wstart)*freq_mult;
542 band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
543 sfb_energy += band->
energy;
547 min_energy = max_energy = band->
energy;
565 ((sce->
zeroes[
w*16+
g] || !sce->
band_alt[
w*16+
g]) && sfb_energy < threshold*
sqrtf(1.0f/freq_boost)) || spread < spread_threshold ||
566 (!sce->
zeroes[
w*16+
g] && sce->
band_alt[
w*16+
g] && sfb_energy > threshold*thr_mult*freq_boost) ||
567 min_energy < pns_transient_energy_r * max_energy ) {
574 pns_tgt_energy = sfb_energy*
FFMIN(1.0f, spread*spread);
575 noise_sfi =
av_clip(roundf(
log2f(pns_tgt_energy)*2), -100, 155);
586 float band_energy,
scale, pns_senergy;
588 band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
591 PNS[
i] =
s->random_state;
593 band_energy =
s->fdsp->scalarproduct_float(PNS, PNS, sce->
ics.
swb_sizes[
g]);
596 pns_senergy =
s->fdsp->scalarproduct_float(PNS, PNS, sce->
ics.
swb_sizes[
g]);
597 pns_energy += pns_senergy;
614 energy_ratio = pns_tgt_energy/pns_energy;
615 sce->
pns_ener[
w*16+
g] = energy_ratio*pns_tgt_energy;
616 if (sce->
zeroes[
w*16+
g] || !sce->
band_alt[
w*16+
g] || (energy_ratio > 0.85f && energy_ratio < 1.25f && dist2 < dist1)) {
633 int bandwidth, cutoff;
634 const float lambda =
s->lambda;
635 const float freq_mult = avctx->
sample_rate*0.5f/wlen;
637 const float pns_transient_energy_r =
FFMIN(0.7f, lambda / 140.f);
642 bandwidth =
s->bandwidth;
643 cutoff = bandwidth * 2 * wlen / avctx->
sample_rate;
648 float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
649 float min_energy = -1.0f, max_energy = 0.0f;
651 const float freq = start*freq_mult;
653 if (freq < NOISE_LOW_LIMIT || start >= cutoff) {
658 band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
659 sfb_energy += band->
energy;
663 min_energy = max_energy = band->
energy;
679 int near = sfb_energy < 2.0f * threshold &&
680 sfb_energy > threshold * 0.25f;
683 sce->
can_pns[
w*16+
g] = spread >= spread_threshold &&
684 min_energy >= 0.2f * max_energy;
685 }
else if (sfb_energy < threshold*
sqrtf(1.5f/freq_boost) || spread < spread_threshold || min_energy < pns_transient_energy_r * max_energy) {
697 int start = 0,
i,
w, w2,
g, sid_sf_boost, prev_mid, prev_side;
698 uint8_t nextband0[128], nextband1[128];
699 float *
M =
s->scoefs + 128*0, *
S =
s->scoefs + 128*1;
700 float *L34 =
s->scoefs + 128*2, *R34 =
s->scoefs + 128*3;
701 float *M34 =
s->scoefs + 128*4, *S34 =
s->scoefs + 128*5;
702 const float lambda =
s->lambda;
703 const float mslambda =
FFMIN(1.0f, lambda / 120.f);
713 prev_mid = sce0->
sf_idx[0];
714 prev_side = sce1->
sf_idx[0];
722 float Mmax = 0.0f, Smax = 0.0f;
728 + sce1->
coeffs[start+(
w+w2)*128+
i]) * 0.5;
735 Mmax =
FFMAX(Mmax, M34[
i]);
736 Smax =
FFMAX(Smax, S34[
i]);
740 for (sid_sf_boost = 0; sid_sf_boost < 4; sid_sf_boost++) {
741 float dist1 = 0.0f, dist2 = 0.0f;
761 midcb =
FFMAX(1,midcb);
762 sidcb =
FFMAX(1,sidcb);
765 FFPsyBand *band0 = &
s->psy.ch[
s->cur_channel+0].psy_bands[(
w+w2)*16+
g];
766 FFPsyBand *band1 = &
s->psy.ch[
s->cur_channel+1].psy_bands[(
w+w2)*16+
g];
771 + sce1->
coeffs[start+(
w+w2)*128+
i]) * 0.5;
803 mslambda / (minthr * bmax + FLT_MIN),
INFINITY, &b4,
NULL);
821 }
else if (
B1 >
B0) {
AAC definitions and structures.
#define SCALE_MAX_DIFF
maximum scalefactor difference allowed by standard
@ INTENSITY_BT
Scalefactor data are intensity stereo positions (in phase).
@ INTENSITY_BT2
Scalefactor data are intensity stereo positions (out of phase).
@ RESERVED_BT
Band types following are encoded differently from others.
@ NOISE_BT
Spectral data are scaled white noise not coded in the bitstream.
#define SCALE_MAX_POS
scalefactor index maximum value
#define SCALE_DIV_512
scalefactor difference that corresponds to scale difference in 512 times
#define SCALE_ONE_POS
scalefactor index that corresponds to scale=1.0
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
#define POW_SF2_ZERO
ff_aac_pow2sf_tab index corresponding to pow(2, 0);
float(* quantize_and_encode_band_func)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *quant, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy)
float ff_quantize_and_encode_band_cost(struct AACEncContext *s, PutBitContext *pb, const float *in, float *quant, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy)
static const quantize_and_encode_band_func quantize_and_encode_band_cost_arr[]
static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
static av_always_inline float quantize_and_encode_band_cost_template(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy, int BT_ZERO, int BT_UNSIGNED, int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO, const float ROUNDING)
Calculate rate distortion cost for quantizing with given codebook.
const AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB]
static void mark_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
static void search_for_ms(AACEncContext *s, ChannelElement *cpe)
static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size, int scale_idx, int cb, const float lambda, int rtz)
static const quantize_and_encode_band_func quantize_and_encode_band_cost_rtz_arr[]
#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING)
static float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb, const float *in, float *quant, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy)
av_cold void ff_aac_coder_init_tables(void)
#define NOISE_LAMBDA_REPLACE
#define NOISE_SPREAD_THRESHOLD
static float esc_pow43[1<< 13]
static void search_for_quantizers_nmr(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
AAC encoder trellis codebook selector.
static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce, int win, int group_len, const float lambda)
AAC encoder twoloop coder.
static void search_for_quantizers_twoloop(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
two-loop quantizers search taken from ISO 13818-7 Appendix C
#define NOISE_LOW_LIMIT
This file contains a template for the twoloop coder function.
static av_always_inline int lcg_random(unsigned previous_val)
linear congruential pseudorandom number generator
void ff_quantize_band_cost_cache_init(struct AACEncContext *s)
void ff_aac_search_for_is(AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe)
AAC encoder Intensity Stereo.
static float quantize_band_cost(struct AACEncContext *s, const float *in, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy)
static float quantize_band_cost_cached(struct AACEncContext *s, int w, int g, const float *in, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, float *energy, int rtz)
void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
Encode TNS data.
void ff_aac_apply_tns(AACEncContext *s, SingleChannelElement *sce)
AAC encoder temporal noise shaping.
static void ff_init_nextband_map(const SingleChannelElement *sce, uint8_t *nextband)
static av_always_inline float bval2bmax(float b)
approximates exp10f(-3.0f*(0.5f + 0.5f * cosf(FFMIN(b,15.5f) / 15.5f)))
static int find_min_book(float maxval, int sf)
static float find_max_val(int group_len, int swb_size, const float *scaled)
static int ff_sfdelta_can_replace(const SingleChannelElement *sce, const uint8_t *nextband, int prev_sf, int new_sf, int band)
static int ff_sfdelta_can_remove_band(const SingleChannelElement *sce, const uint8_t *nextband, int prev_sf, int band)
static const uint8_t aac_cb_maxval[12]
static const uint8_t aac_cb_range[12]
const float *const ff_aac_codebook_vectors[]
const uint8_t ff_aac_scalefactor_bits[121]
const uint8_t *const ff_aac_spectral_bits[11]
const uint16_t *const ff_aac_spectral_codes[11]
float ff_aac_pow2sf_tab[428]
float ff_aac_pow34sf_tab[428]
static const float bands[]
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define i(width, name, range_min, range_max)
static __device__ float sqrtf(float a)
static __device__ float fabsf(float a)
static __device__ float ceilf(float a)
static const uint8_t bits[8]
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static void put_sbits(PutBitContext *pb, int n, int32_t value)
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
int64_t bit_rate
the average bitrate
int sample_rate
samples per second
structure used in optimal codebook search
int prev_idx
pointer to the previous path point
channel element - generic struct for SCE/CPE/CCE/LFE
uint8_t ms_mask[128]
Set if mid/side stereo is used for each scalefactor window band.
SingleChannelElement ch[2]
uint8_t is_mask[128]
Set if intensity stereo is used.
int common_window
Set if channels share a common 'IndividualChannelStream' in bitstream.
single band psychoacoustic information
int num_swb
number of scalefactor window bands
const uint8_t * swb_sizes
table of scalefactor band sizes for a particular window
const uint16_t * swb_offset
table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular wind...
Single Channel Element - used for both SCE and LFE elements.
uint8_t zeroes[128]
band is not coded
float coeffs[1024]
coefficients for IMDCT, maybe processed
float is_ener[128]
Intensity stereo pos.
uint8_t can_pns[128]
band is allowed to PNS (informative)
float pns_ener[128]
Noise energy values.
enum BandType band_alt[128]
alternative band type
enum BandType band_type[128]
band types
IndividualChannelStream ics
int sf_idx[128]
scalefactor indices
static double cb(void *priv, double x, double y)
static double b1(void *priv, double x, double y)
static double b2(void *priv, double x, double y)
static double b3(void *priv, double x, double y)
static const uint8_t quant[64]