55#define NOISE_SPREAD_THRESHOLD 0.9f
59#define NOISE_LAMBDA_REPLACE 1.948f
65 const float *in,
float *
quant,
const float *scaled,
66 int size,
int scale_idx,
int cb,
67 const float lambda,
const float uplim,
68 int *
bits,
float *energy);
78 const float *scaled,
int size,
int scale_idx,
79 int cb,
const float lambda,
const float uplim,
80 int *
bits,
float *energy,
int BT_ZERO,
int BT_UNSIGNED,
81 int BT_PAIR,
int BT_ESC,
int BT_NOISE,
int BT_STEREO,
88 const float CLIPPED_ESCAPE = 165140.0f*IQ;
91 const int dim = BT_PAIR ? 2 : 4;
95 if (BT_ZERO || BT_NOISE || BT_STEREO) {
104 for (
int j = 0; j <
dim; j++)
110 s->aacdsp.abs_pow34(
s->scoefs, in,
size);
121 int *quants =
s->qcoefs +
i;
124 float quantized, rd = 0.0f;
125 for (
int j = 0; j <
dim; j++) {
127 curidx += quants[j] + off;
132 for (
int j = 0; j <
dim; j++) {
135 if (BT_ESC && vec[j] == 64.0f) {
136 if (t >= CLIPPED_ESCAPE) {
137 quantized = CLIPPED_ESCAPE;
145 quantized = vec[j]*IQ;
149 out[
i+j] = in[
i+j] >= 0 ? quantized : -quantized;
152 qenergy += quantized*quantized;
156 for (
int j = 0; j <
dim; j++) {
157 quantized = vec[j]*IQ;
158 qenergy += quantized*quantized;
160 out[
i+j] = quantized;
161 rd += (in[
i+j] - quantized)*(in[
i+j] - quantized);
164 cost += rd *
lambda + curbits;
171 for (
int j = 0; j <
dim; j++)
175 for (
int j = 0; j < 2; j++) {
196 const float *in,
float *
quant,
const float *scaled,
197 int size,
int scale_idx,
int cb,
198 const float lambda,
const float uplim,
199 int *
bits,
float *energy) {
204#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
205static float quantize_and_encode_band_cost_ ## NAME( \
206 struct AACEncContext *s, \
207 PutBitContext *pb, const float *in, float *quant, \
208 const float *scaled, int size, int scale_idx, \
209 int cb, const float lambda, const float uplim, \
210 int *bits, float *energy) { \
211 return quantize_and_encode_band_cost_template( \
212 s, pb, in, quant, scaled, size, scale_idx, \
213 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \
214 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
230 quantize_and_encode_band_cost_ZERO,
231 quantize_and_encode_band_cost_SQUAD,
232 quantize_and_encode_band_cost_SQUAD,
233 quantize_and_encode_band_cost_UQUAD,
234 quantize_and_encode_band_cost_UQUAD,
235 quantize_and_encode_band_cost_SPAIR,
236 quantize_and_encode_band_cost_SPAIR,
237 quantize_and_encode_band_cost_UPAIR,
238 quantize_and_encode_band_cost_UPAIR,
239 quantize_and_encode_band_cost_UPAIR,
240 quantize_and_encode_band_cost_UPAIR,
241 quantize_and_encode_band_cost_ESC,
243 quantize_and_encode_band_cost_NOISE,
244 quantize_and_encode_band_cost_STEREO,
245 quantize_and_encode_band_cost_STEREO,
250 quantize_and_encode_band_cost_ZERO,
251 quantize_and_encode_band_cost_SQUAD,
252 quantize_and_encode_band_cost_SQUAD,
253 quantize_and_encode_band_cost_UQUAD,
254 quantize_and_encode_band_cost_UQUAD,
255 quantize_and_encode_band_cost_SPAIR,
256 quantize_and_encode_band_cost_SPAIR,
257 quantize_and_encode_band_cost_UPAIR,
258 quantize_and_encode_band_cost_UPAIR,
259 quantize_and_encode_band_cost_UPAIR,
260 quantize_and_encode_band_cost_UPAIR,
261 quantize_and_encode_band_cost_ESC_RTZ,
263 quantize_and_encode_band_cost_NOISE,
264 quantize_and_encode_band_cost_STEREO,
265 quantize_and_encode_band_cost_STEREO,
269 const float *in,
float *
quant,
const float *scaled,
270 int size,
int scale_idx,
int cb,
271 const float lambda,
const float uplim,
272 int *
bits,
float *energy)
280 const float *in,
float *
out,
int size,
int scale_idx,
281 int cb,
const float lambda,
int rtz)
301#define TRELLIS_STAGES 121
302#define TRELLIS_STATES (SCALE_MAX_DIFF+1)
307 int prevscaler_n = -255, prevscaler_i = 0;
319 if (prevscaler_n == -255)
347 int start = 0,
i,
w, w2,
g;
349 float dists[128] = { 0 }, uplims[128] = { 0 };
351 int fflag, minscaler;
358 destbits =
FFMIN(destbits, 5800);
367 FFPsyBand *band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
375 uplims[
w*16+
g] = uplim *512;
379 minthr =
FFMIN(minthr, uplim);
396 s->aacdsp.abs_pow34(
s->scoefs, sce->
coeffs, 1024);
402 const float *scaled =
s->scoefs + start;
412 minscaler = sce->
sf_idx[0];
414 qstep = its ? 1 : 32;
421 const float *coefs = sce->
coeffs + start;
422 const float *scaled =
s->scoefs + start;
444 dists[
w*16+
g] = dist -
bits;
453 if (tbits > destbits) {
454 for (
i = 0;
i < 128;
i++)
455 if (sce->
sf_idx[
i] < 218 - qstep)
458 for (
i = 0;
i < 128;
i++)
459 if (sce->
sf_idx[
i] > 60 - qstep)
463 if (!qstep && tbits > destbits*1.02 && sce->
sf_idx[0] < 217)
473 if (dists[
w*16+
g] > uplims[
w*16+
g] && sce->
sf_idx[
w*16+
g] > 60) {
487 }
while (fflag && its < 10);
495 int bandwidth, cutoff;
496 float *PNS = &
s->scoefs[0*128], *PNS34 = &
s->scoefs[1*128];
497 float *NOR34 = &
s->scoefs[3*128];
498 uint8_t nextband[128];
499 const float lambda =
s->lambda;
500 const float freq_mult = avctx->
sample_rate*0.5f/wlen;
503 const float dist_bias =
av_clipf(4.f * 120 / lambda, 0.25f, 4.0f);
504 const float pns_transient_energy_r =
FFMIN(0.7f, lambda / 140.f);
506 int prev = -1000, prev_sf = -1;
511 bandwidth =
s->bandwidth;
512 cutoff = bandwidth * 2 * wlen / avctx->
sample_rate;
520 float dist1 = 0.0f, dist2 = 0.0f, noise_amp;
521 float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh;
522 float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
523 float min_energy = -1.0f, max_energy = 0.0f;
525 const float freq = (start-wstart)*freq_mult;
533 band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
534 sfb_energy += band->
energy;
538 min_energy = max_energy = band->
energy;
556 ((sce->
zeroes[
w*16+
g] || !sce->
band_alt[
w*16+
g]) && sfb_energy < threshold*
sqrtf(1.0f/freq_boost)) || spread < spread_threshold ||
557 (!sce->
zeroes[
w*16+
g] && sce->
band_alt[
w*16+
g] && sfb_energy > threshold*thr_mult*freq_boost) ||
558 min_energy < pns_transient_energy_r * max_energy ) {
565 pns_tgt_energy = sfb_energy*
FFMIN(1.0f, spread*spread);
577 float band_energy,
scale, pns_senergy;
579 band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
582 PNS[
i] =
s->random_state;
584 band_energy =
s->fdsp->scalarproduct_float(PNS, PNS, sce->
ics.
swb_sizes[
g]);
587 pns_senergy =
s->fdsp->scalarproduct_float(PNS, PNS, sce->
ics.
swb_sizes[
g]);
588 pns_energy += pns_senergy;
605 energy_ratio = pns_tgt_energy/pns_energy;
606 sce->
pns_ener[
w*16+
g] = energy_ratio*pns_tgt_energy;
607 if (sce->
zeroes[
w*16+
g] || !sce->
band_alt[
w*16+
g] || (energy_ratio > 0.85f && energy_ratio < 1.25f && dist2 < dist1)) {
624 int bandwidth, cutoff;
625 const float lambda =
s->lambda;
626 const float freq_mult = avctx->
sample_rate*0.5f/wlen;
628 const float pns_transient_energy_r =
FFMIN(0.7f, lambda / 140.f);
633 bandwidth =
s->bandwidth;
634 cutoff = bandwidth * 2 * wlen / avctx->
sample_rate;
639 float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
640 float min_energy = -1.0f, max_energy = 0.0f;
642 const float freq = start*freq_mult;
644 if (freq < NOISE_LOW_LIMIT || start >= cutoff) {
649 band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
650 sfb_energy += band->
energy;
654 min_energy = max_energy = band->
energy;
670 int near = sfb_energy < 2.0f * threshold &&
671 sfb_energy > threshold * 0.25f;
674 sce->
can_pns[
w*16+
g] = spread >= spread_threshold &&
675 min_energy >= 0.2f * max_energy;
676 }
else if (sfb_energy < threshold*
sqrtf(1.5f/freq_boost) || spread < spread_threshold || min_energy < pns_transient_energy_r * max_energy) {
688 int start = 0,
i,
w, w2,
g, sid_sf_boost, prev_mid, prev_side;
689 uint8_t nextband0[128], nextband1[128];
690 float *
M =
s->scoefs + 128*0, *
S =
s->scoefs + 128*1;
691 float *L34 =
s->scoefs + 128*2, *R34 =
s->scoefs + 128*3;
692 float *M34 =
s->scoefs + 128*4, *S34 =
s->scoefs + 128*5;
693 const float lambda =
s->lambda;
694 const float mslambda =
FFMIN(1.0f, lambda / 120.f);
704 prev_mid = sce0->
sf_idx[0];
705 prev_side = sce1->
sf_idx[0];
713 float Mmax = 0.0f, Smax = 0.0f;
719 + sce1->
coeffs[start+(
w+w2)*128+
i]) * 0.5;
726 Mmax =
FFMAX(Mmax, M34[
i]);
727 Smax =
FFMAX(Smax, S34[
i]);
731 for (sid_sf_boost = 0; sid_sf_boost < 4; sid_sf_boost++) {
732 float dist1 = 0.0f, dist2 = 0.0f;
752 midcb =
FFMAX(1,midcb);
753 sidcb =
FFMAX(1,sidcb);
756 FFPsyBand *band0 = &
s->psy.ch[
s->cur_channel+0].psy_bands[(
w+w2)*16+
g];
757 FFPsyBand *band1 = &
s->psy.ch[
s->cur_channel+1].psy_bands[(
w+w2)*16+
g];
762 + sce1->
coeffs[start+(
w+w2)*128+
i]) * 0.5;
794 mslambda / (minthr * bmax + FLT_MIN),
INFINITY, &b4,
NULL);
812 }
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)
#define NOISE_LAMBDA_REPLACE
#define NOISE_SPREAD_THRESHOLD
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 void scale(int *out, const int *in, const int w, const int h, const int shift)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Replacements for frequently missing libm functions.
static av_always_inline av_const float roundf(float x)
static av_always_inline float cbrtf(float x)
static void put_sbits(PutBitContext *pb, int n, int32_t value)
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]