52 #define NOISE_LOW_LIMIT 4500
56 #define NOISE_SPREAD_THRESHOLD 0.5f
61 #define NOISE_LAMBDA_NUMERATOR 252.1f
76 int win,
int group_len,
const float lambda)
83 const int run_esc = (1 <<
run_bits) - 1;
85 int stackrun[120], stackcb[120], stack_len;
96 for (swb = 0; swb < max_sfb; swb++) {
98 if (sce->
zeroes[win*16 + swb]) {
105 float minrd = next_minrd;
106 int mincb = next_mincb;
110 float cost_stay_here, cost_get_here;
119 for (w = 0; w < group_len; w++) {
122 &s->
scoefs[start + w*128], size,
126 cost_stay_here = path[swb][
cb].
cost + rd;
127 cost_get_here = minrd + rd + run_bits + 4;
131 if (cost_get_here < cost_stay_here) {
133 path[swb+1][
cb].
cost = cost_get_here;
134 path[swb+1][
cb].
run = 1;
137 path[swb+1][
cb].
cost = cost_stay_here;
140 if (path[swb+1][cb].cost < next_minrd) {
141 next_minrd = path[swb+1][
cb].
cost;
153 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
159 stackrun[stack_len] = path[ppos][
cb].
run;
160 stackcb [stack_len] =
cb;
162 ppos -= path[ppos][
cb].
run;
167 for (i = stack_len - 1; i >= 0; i--) {
171 memset(sce->
zeroes + win*16 + start, !cb, count);
173 for (j = 0; j <
count; j++) {
177 while (count >= run_esc) {
186 int win,
int group_len,
const float lambda)
193 const int run_esc = (1 <<
run_bits) - 1;
194 int idx, ppos,
count;
195 int stackrun[120], stackcb[120], stack_len;
202 path[0][
cb].
cost = run_bits+4;
206 for (swb = 0; swb < max_sfb; swb++) {
208 if (sce->
zeroes[win*16 + swb]) {
209 float cost_stay_here = path[swb][0].
cost;
210 float cost_get_here = next_minbits + run_bits + 4;
214 if (cost_get_here < cost_stay_here) {
215 path[swb+1][0].
prev_idx = next_mincb;
216 path[swb+1][0].
cost = cost_get_here;
217 path[swb+1][0].
run = 1;
220 path[swb+1][0].
cost = cost_stay_here;
221 path[swb+1][0].
run = path[swb][0].
run + 1;
223 next_minbits = path[swb+1][0].
cost;
226 path[swb+1][
cb].
cost = 61450;
228 path[swb+1][
cb].
run = 0;
231 float minbits = next_minbits;
232 int mincb = next_mincb;
233 int startcb = sce->
band_type[win*16+swb];
237 for (cb = 0; cb < startcb; cb++) {
238 path[swb+1][
cb].
cost = 61450;
240 path[swb+1][
cb].
run = 0;
243 float cost_stay_here, cost_get_here;
246 path[swb+1][
cb].
cost = 61450;
248 path[swb+1][
cb].
run = 0;
251 for (w = 0; w < group_len; w++) {
253 &s->
scoefs[start + w*128], size,
259 cost_get_here = minbits + bits + run_bits + 4;
263 if (cost_get_here < cost_stay_here) {
265 path[swb+1][
cb].
cost = cost_get_here;
266 path[swb+1][
cb].
run = 1;
269 path[swb+1][
cb].
cost = cost_stay_here;
272 if (path[swb+1][cb].cost < next_minbits) {
273 next_minbits = path[swb+1][
cb].
cost;
285 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
291 stackrun[stack_len] = path[ppos][
cb].
run;
292 stackcb [stack_len] =
cb;
294 ppos -= path[ppos][
cb].
run;
299 for (i = stack_len - 1; i >= 0; i--) {
303 memset(sce->
zeroes + win*16 + start, !cb, count);
305 for (j = 0; j <
count; j++) {
309 while (count >= run_esc) {
322 #define TRELLIS_STAGES 121
323 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
328 int minscaler_n = sce->
sf_idx[0], minscaler_i = sce->
sf_idx[0];
336 minscaler_i =
FFMIN(minscaler_i, sce->
sf_idx[w*16+g]);
340 minscaler_n =
FFMIN(minscaler_n, sce->
sf_idx[w*16+g]);
366 int q, w, w2,
g,
start = 0;
373 float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
374 int q0,
q1, qcnt = 0;
376 for (i = 0; i < 1024; i++) {
377 float t = fabsf(sce->
coeffs[i]);
406 }
else if (q1 > q1high) {
413 paths[0][i].
cost = 0.0f;
414 paths[0][i].
prev = -1;
419 paths[j][i].
prev = -2;
431 bandaddr[idx] = w * 16 +
g;
437 sce->
zeroes[(w+w2)*16+g] = 1;
440 sce->
zeroes[(w+w2)*16+g] = 0;
443 float t = fabsf(coefs[w2*128+i]);
445 qmin =
FFMIN(qmin, t);
446 qmax =
FFMAX(qmax, t);
450 int minscale, maxscale;
457 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
458 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
460 for (q = minscale; q < maxscale; q++) {
468 minrd =
FFMIN(minrd, dist);
470 for (i = 0; i < q1 -
q0; i++) {
472 cost = paths[idx - 1][i].
cost + dist
474 if (cost < paths[idx][q].cost) {
475 paths[idx][q].
cost = cost;
476 paths[idx][q].
prev = i;
481 for (q = 0; q < q1 -
q0; q++) {
482 paths[idx][q].
cost = paths[idx - 1][q].
cost + 1;
483 paths[idx][q].
prev = q;
492 mincost = paths[idx][0].
cost;
495 if (paths[idx][i].cost < mincost) {
496 mincost = paths[idx][i].
cost;
501 sce->
sf_idx[bandaddr[idx]] = minq +
q0;
502 minq = paths[idx][minq].
prev;
520 int start = 0, i, w, w2,
g;
522 float dists[128] = { 0 }, uplims[128] = { 0 };
524 int fflag, minscaler;
531 destbits =
FFMIN(destbits, 5800);
537 float uplim = 0.0f, energy = 0.0f;
543 sce->
zeroes[(w+w2)*16+g] = 1;
548 uplims[w*16+
g] = uplim *512;
551 minthr =
FFMIN(minthr, uplim);
557 if (sce->
zeroes[w*16+g]) {
582 minscaler = sce->
sf_idx[0];
584 qstep = its ? 1 : 32;
597 if (sce->
zeroes[w*16+g] || sce->
sf_idx[w*16+g] >= 218) {
616 dists[w*16+
g] = dist -
bits;
625 if (tbits > destbits) {
626 for (i = 0; i < 128; i++)
627 if (sce->
sf_idx[i] < 218 - qstep)
630 for (i = 0; i < 128; i++)
631 if (sce->
sf_idx[i] > 60 - qstep)
635 if (!qstep && tbits > destbits*1.02 && sce->
sf_idx[0] < 217)
644 int prevsc = sce->
sf_idx[w*16+
g];
645 if (dists[w*16+g] > uplims[w*16+g] && sce->
sf_idx[w*16+g] > 60) {
653 if (sce->
sf_idx[w*16+g] != prevsc)
659 }
while (fflag && its < 10);
666 int start = 0, i, w, w2,
g;
667 float uplim[128], maxq[128];
669 float distfact = ((sce->
ics.
num_windows > 1) ? 85.80 : 147.84) / lambda;
670 int last = 0, lastband = 0, curband = 0;
671 float avg_energy = 0.0;
674 for (i = 0; i < 1024; i++) {
686 for (w = 0; w < 8; w++) {
689 for (i = 0; i < 128; i++) {
695 avg_energy += coeffs[i] * coeffs[i];
696 last =
FFMAX(last, i);
697 lastband =
FFMAX(lastband, curband);
704 if (avg_energy == 0.0f) {
715 float maxval = -1, thr = 0.0f, t;
721 memset(coefs + w2*128, 0,
sizeof(coefs[0])*
size);
725 for (i = 0; i <
size; i++) {
726 float t = coefs[w2*128+i]*coefs[w2*128+i];
727 maxq[w*16+
g] =
FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
736 start2 =
FFMAX(peakpos - 2, start2);
737 end2 =
FFMIN(peakpos + 3, end2);
743 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
744 t = 1.0 - (1.0 * start2 / last);
745 uplim[w*16+
g] = distfact / (1.4 * thr + t*t*t + 0.075);
756 int scf, prev_scf, step;
757 int min_scf = -1, max_scf = 256;
759 if (maxq[w*16+g] < 21.544) {
783 dist *= 1.0f / 512.0f / lambda;
785 if (quant_max >= 8191) {
786 sce->
sf_idx[w*16+
g] = prev_scf;
790 curdiff = fabsf(dist - uplim[w*16+g]);
794 step =
log2f(curdiff);
795 if (dist > uplim[w*16+g])
798 scf = av_clip_uint8(scf);
799 step = scf - prev_scf;
800 if (
FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
801 sce->
sf_idx[w*16+
g] = av_clip(scf, min_scf, max_scf);
813 for (i = 1; i < 128; i++) {
823 for (i = 126; i >= 0; i--) {
843 sce->
sf_idx[(w+w2)*16+g] = 218;
844 sce->
zeroes[(w+w2)*16+g] = 1;
847 sce->
zeroes[(w+w2)*16+g] = 0;
853 for (i = 0; i < 128; i++) {
867 const float lambda = s->
lambda;
876 float energy = 0.0f, threshold = 0.0f, spread = 0.0f;
884 ((sce->
zeroes[w*16+g] && energy >= threshold) ||
898 int start = 0, i, w, w2,
g;
899 float M[128],
S[128];
901 const float lambda = s->
lambda;
910 float dist1 = 0.0f, dist2 = 0.0f;
917 M[i] = (sce0->
coeffs[start+(w+w2)*128+i]
918 + sce1->
coeffs[start+(w+w2)*128+i]) * 0.5;
920 - sce1->
coeffs[start+(w+w2)*128+i];
929 sce0->
sf_idx[(w+w2)*16+g],
935 sce1->
sf_idx[(w+w2)*16+g],
941 sce0->
sf_idx[(w+w2)*16+g],
947 sce1->
sf_idx[(w+w2)*16+g],
951 cpe->
ms_mask[w*16+
g] = dist2 < dist1;
static const uint8_t *const run_value_bits[2]
float pns_ener[128]
Noise energy values (used by encoder)
AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB]
static const uint8_t aac_cb_out_map[CB_TOT_ALL]
Map to convert values from BandCodingPath index to a codebook index.
static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce, int win, int group_len, const float lambda)
Encode band info for single window group bands.
static void abs_pow34_v(float *out, const float *in, const int size)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
FFPsyBand psy_bands[PSY_MAX_BANDS]
channel bands information
void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
Encode TNS data.
#define SCALE_MAX_POS
scalefactor index maximum value
#define SCALE_MAX_DIFF
maximum scalefactor difference allowed by standard
int common_window
Set if channels share a common 'IndividualChannelStream' in bitstream.
int prev_idx
pointer to the previous path point
uint8_t ms_mask[128]
Set if mid/side stereo is used for each scalefactor window band.
static const uint8_t q1[256]
static uint8_t coef2maxsf(float coef)
Return the maximum scalefactor where the quantized coef is not zero.
static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
Spectral data are scaled white noise not coded in the bitstream.
static double cb(void *priv, double x, double y)
SingleChannelElement ch[2]
const uint8_t ff_aac_scalefactor_bits[121]
AAC encoder main prediction.
static const uint8_t run_bits[7][16]
void ff_aac_encode_main_pred(AACEncContext *s, SingleChannelElement *sce)
Encoder predictors data.
Scalefactor data are intensity stereo positions (in phase).
single band psychoacoustic information
static const uint8_t aac_cb_in_map[CB_TOT_ALL+1]
Inverse map to convert from codebooks to BandCodingPath indices.
float coeffs[1024]
coefficients for IMDCT, maybe processed
void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
float is_ener[128]
Intensity stereo pos (used by encoder)
void ff_aac_apply_tns(AACEncContext *s, SingleChannelElement *sce)
uint8_t max_sfb
number of scalefactor bands per group
int num_swb
number of scalefactor window bands
Libavcodec external API header.
static const uint8_t q0[256]
void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce)
#define NOISE_LOW_LIMIT
Frequency in Hz for lower limit of noise substitution.
#define POW_SF2_ZERO
ff_aac_pow2sf_tab index corresponding to pow(2, 0);
#define NOISE_LAMBDA_NUMERATOR
#define SCALE_DIV_512
scalefactor difference that corresponds to scale difference in 512 times
static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
void ff_aac_adjust_common_prediction(AACEncContext *s, ChannelElement *cpe)
int bit_rate
the average bitrate
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
static uint8_t coef2minsf(float coef)
Return the minimum scalefactor where the quantized coef does not clip.
void ff_aac_apply_main_pred(AACEncContext *s, SingleChannelElement *sce)
static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
AAC encoder Intensity Stereo.
AAC definitions and structures.
#define FF_ARRAY_ELEMS(a)
void ff_aac_search_for_is(AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe)
static int find_min_book(float maxval, int sf)
int sample_rate
samples per second
static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce, int win, int group_len, const float lambda)
main external API structure.
IndividualChannelStream ics
structure used in optimal codebook search
Replacements for frequently missing libm functions.
Spectral data are coded with an escape sequence.
const uint8_t * swb_sizes
table of scalefactor band sizes for a particular window
#define NOISE_SPREAD_THRESHOLD
#define CB_TOT_ALL
Total number of codebooks, including special ones.
uint8_t zeroes[128]
band is not coded (used by encoder)
int sf_idx[128]
scalefactor indices (used by encoder)
Scalefactor data are intensity stereo positions (out of phase).
#define SCALE_ONE_POS
scalefactor index that corresponds to scale=1.0
static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, const float lambda)
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
Single Channel Element - used for both SCE and LFE elements.
float ff_aac_pow2sf_tab[428]
channel element - generic struct for SCE/CPE/CCE/LFE
static const int16_t coeffs[]
int channels
number of audio channels
FFPsyChannel * ch
single channel information
enum BandType band_type[128]
band types
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)
AAC encoder temporal noise shaping.
static float find_max_val(int group_len, int swb_size, const float *scaled)
float scoefs[1024]
scaled coefficients
static void search_for_ms(AACEncContext *s, ChannelElement *cpe)
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, int rtz)