40 #ifndef AVCODEC_AACCODER_TWOLOOP_H 41 #define AVCODEC_AACCODER_TWOLOOP_H 54 #define NOISE_LOW_LIMIT 4000 56 #define sclip(x) av_clip(x,60,218) 61 return (!g || !sce->
zeroes[w*16+g-1] || !sce->
can_pns[w*16+g-1]) ? 9 : 5;
72 int start = 0, i,
w, w2,
g, recomprd;
76 int refbits = destbits;
77 int toomanybits, toofewbits;
80 int maxsf[128], minsf[128];
81 float dists[128] = { 0 }, qenergies[128] = { 0 }, uplims[128], euplims[128], energies[128];
82 float maxvals[128], spread_thr_r[128];
83 float min_spread_thr_r, max_spread_thr_r;
94 float rdlambda = av_clipf(2.0f * 120.f / lambda, 0.0625f, 16.0f);
95 const float nzslope = 1.5f;
96 float rdmin = 0.03125f;
104 float sfoffs = av_clipf(
log2f(120.0f / lambda) * 4.0f, -5, 10);
106 int fflag, minscaler, maxscaler, nminscaler;
124 if (lambda > 120.f) {
125 zeroscale = av_clipf(
powf(120.f / lambda, 0.25f), 0.0625f, 1.0f);
153 toofewbits = destbits / 16;
157 rdlambda = sqrtf(rdlambda);
167 toomanybits = destbits + destbits/8;
168 toofewbits = destbits - destbits/8;
171 rdlambda = sqrtf(rdlambda);
186 float rate_bandwidth_multiplier = 1.5f;
188 ? (refbits * rate_bandwidth_multiplier * avctx->
sample_rate / 1024)
193 frame_bit_rate *= 1.15f;
196 bandwidth = avctx->
cutoff;
202 cutoff = bandwidth * 2 * wlen / avctx->
sample_rate;
210 destbits =
FFMIN(destbits, 5800);
211 toomanybits =
FFMIN(toomanybits, 5800);
212 toofewbits =
FFMIN(toofewbits, 5800);
217 min_spread_thr_r = -1;
218 max_spread_thr_r = -1;
222 float uplim = 0.0f, energy = 0.0f, spread = 0.0f;
226 sce->
zeroes[(w+w2)*16+g] = 1;
245 uplims[w*16+
g] = uplim;
246 energies[w*16+
g] = energy;
250 if (nz && sce->
can_pns[w*16+g]) {
251 spread_thr_r[w*16+
g] = energy * nz / (uplim * spread);
252 if (min_spread_thr_r < 0) {
253 min_spread_thr_r = max_spread_thr_r = spread_thr_r[w*16+
g];
255 min_spread_thr_r =
FFMIN(min_spread_thr_r, spread_thr_r[w*16+g]);
256 max_spread_thr_r =
FFMAX(max_spread_thr_r, spread_thr_r[w*16+g]);
266 if (sce->
zeroes[w*16+g]) {
297 for (i = 0; i <
sizeof(minsf) /
sizeof(minsf[0]); ++i)
305 if (maxvals[w*16+g] > 0) {
308 minsf[(w+w2)*16+
g] = minsfidx;
321 memcpy(euplims, uplims,
sizeof(euplims));
328 float cleanup_factor =
ff_sqrf(av_clipf(start / (cutoff * 0.75f), 1.0f, 2.0f));
333 nzslope * cleanup_factor);
334 energy2uplim *= de_psy_factor;
337 energy2uplim = sqrtf(energy2uplim);
339 energy2uplim =
FFMAX(0.015625f,
FFMIN(1.0f, energy2uplim));
340 uplims[w*16+
g] *= av_clipf(rdlambda * energy2uplim, rdmin, rdmax)
348 energy2uplim *= de_psy_factor;
351 energy2uplim = sqrtf(energy2uplim);
353 energy2uplim =
FFMAX(0.015625f,
FFMIN(1.0f, energy2uplim));
354 euplims[w*16+
g] *= av_clipf(rdlambda * energy2uplim * sce->
ics.
group_len[w],
361 for (i = 0; i <
sizeof(maxsf) /
sizeof(maxsf[0]); ++i)
369 int qstep = its ? 1 : 32;
383 float qenergy = 0.0f;
385 if (sce->
zeroes[w*16+g] || sce->
sf_idx[w*16+g] >= 218) {
409 dists[w*16+
g] = dist - bits;
410 qenergies[w*16+
g] = qenergy;
420 if (tbits > toomanybits) {
422 for (i = 0; i < 128; i++) {
425 int new_sf =
FFMIN(maxsf_i, sce->
sf_idx[i] + qstep);
426 if (new_sf != sce->
sf_idx[i]) {
432 }
else if (tbits < toofewbits) {
434 for (i = 0; i < 128; i++) {
437 if (new_sf != sce->
sf_idx[i]) {
445 if (!qstep && tbits > toomanybits && sce->
sf_idx[0] < 217 && changed)
450 fflag = tbits < toofewbits;
451 for (i = 0; i < 2 && (overdist || recomprd); ++i) {
464 float qenergy = 0.0f;
466 if (sce->
zeroes[w*16+g] || sce->
sf_idx[w*16+g] >= 218) {
490 dists[w*16+
g] = dist - bits;
491 qenergies[w*16+
g] = qenergy;
502 if (!i && s->
options.
pns && its > maxits/2 && tbits > toofewbits) {
503 float maxoverdist = 0.0f;
504 float ovrfactor = 1.f+(maxits-its)*16.f/maxits;
505 overdist = recomprd = 0;
509 float ovrdist = dists[w*16+
g] /
FFMAX(uplims[w*16+g],euplims[w*16+g]);
510 maxoverdist =
FFMAX(maxoverdist, ovrdist);
519 float minspread = max_spread_thr_r;
520 float maxspread = min_spread_thr_r;
524 int maxzeroed, zloop;
527 if (start >= pns_start_pos && !sce->
zeroes[w*16+g] && sce->
can_pns[w*16+g]) {
528 minspread =
FFMIN(minspread, spread_thr_r[w*16+g]);
529 maxspread =
FFMAX(maxspread, spread_thr_r[w*16+g]);
534 zspread = (maxspread-minspread) * 0.0125f + minspread;
540 zspread =
FFMIN3(min_spread_thr_r * 8.f, zspread,
541 ((toomanybits - tbits) * min_spread_thr_r + (tbits - toofewbits) * max_spread_thr_r) / (toomanybits - toofewbits + 1));
542 maxzeroed =
FFMIN(zeroable,
FFMAX(1, (zeroable * its + maxits - 1) / (2 * maxits)));
543 for (zloop = 0; zloop < 2; zloop++) {
550 float loopovrfactor = (zloop) ? 1.0f : ovrfactor;
553 for (g = sce->
ics.
num_swb-1; g > 0 && zeroed < maxzeroed; g--) {
557 if (!sce->
zeroes[w*16+g] && sce->
can_pns[w*16+g] && spread_thr_r[w*16+g] <= zspread
558 && sce->
sf_idx[w*16+g] > loopminsf
559 && (dists[w*16+g] > loopovrfactor*uplims[w*16+g] || !(mcb =
find_min_book(maxvals[w*16+g], sce->
sf_idx[w*16+g]))
560 || (mcb <= 1 && dists[w*16+g] >
FFMIN(uplims[w*16+g], euplims[w*16+g]))) ) {
569 recomprd = fflag = 1;
580 if (!sce->
zeroes[w*16+g]) {
591 int depth = (its > maxits/2) ? ((its > maxits*2/3) ? 1 : 3) : 10;
592 int edepth = depth+2;
593 float uplmax = its / (maxits*0.25f) + 1.0f;
594 uplmax *= (tbits > destbits) ?
FFMIN(2.0f, tbits / (
float)
FFMAX(1,destbits)) : 1.0f;
597 int prevsc = sce->
sf_idx[w*16+
g];
598 if (prev < 0 && !sce->zeroes[w*16+g])
600 if (!sce->
zeroes[w*16+g]) {
606 if ((!cmb || dists[w*16+g] > uplims[w*16+g]) && sce->
sf_idx[w*16+g] >
FFMAX(mindeltasf, minsf[w*16+g])) {
613 for (i = 0; i < edepth && sce->
sf_idx[w*16+
g] > mindeltasf; ++i) {
618 dist = qenergy = 0.f;
621 maxsf[w*16+
g] =
FFMIN(sce->
sf_idx[w*16+g]-1, maxsf[w*16+g]);
622 }
else if (i >= depth && dists[w*16+g] < euplims[w*16+g]) {
629 if (!g && sce->
ics.
num_windows > 1 && dists[w*16+g] >= euplims[w*16+g])
630 maxsf[w*16+
g] =
FFMIN(sce->
sf_idx[w*16+g], maxsf[w*16+g]);
647 dists[w*16+
g] = dist - bits;
648 qenergies[w*16+
g] = qenergy;
649 if (mb && (sce->
sf_idx[w*16+g] < mindeltasf || (
650 (dists[w*16+g] <
FFMIN(uplmax*uplims[w*16+g], euplims[w*16+g]))
651 && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g])
656 }
else if (tbits > toofewbits && sce->
sf_idx[w*16+g] <
FFMIN(maxdeltasf, maxsf[w*16+g])
657 && (dists[w*16+g] <
FFMIN(euplims[w*16+g], uplims[w*16+g]))
658 && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g])
661 for (i = 0; i < depth && sce->
sf_idx[w*16+
g] < maxdeltasf; ++i) {
666 dist = qenergy = 0.f;
684 if (dist <
FFMIN(euplims[w*16+g], uplims[w*16+g])) {
686 dists[w*16+
g] = dist;
687 qenergies[w*16+
g] = qenergy;
692 maxsf[w*16+
g] =
FFMIN(sce->
sf_idx[w*16+g], maxsf[w*16+g]);
697 prev = sce->
sf_idx[w*16+
g] = av_clip(sce->
sf_idx[w*16+g], mindeltasf, maxdeltasf);
698 if (sce->
sf_idx[w*16+g] != prevsc)
700 nminscaler =
FFMIN(nminscaler, sce->
sf_idx[w*16+g]);
711 if (!sce->
zeroes[w*16+g]) {
712 int prevsf = sce->
sf_idx[w*16+
g];
718 if (!fflag && prevsf != sce->
sf_idx[w*16+g])
725 }
while (fflag && its < maxits);
734 if (!sce->
zeroes[w*16+g]) {
749 if (!sce->
zeroes[w*16+g]) {
753 }
else if (sce->
zeroes[0]) {
void ff_quantize_band_cost_cache_init(struct AACEncContext *s)
enum RawDataBlockType cur_type
channel group type cur_channel belongs to
int64_t bit_rate
the average bitrate
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
#define AAC_CUTOFF_FROM_BITRATE(bit_rate, channels, sample_rate)
FFPsyBand psy_bands[PSY_MAX_BANDS]
channel bands information
#define SCALE_MAX_POS
scalefactor index maximum value
#define SCALE_MAX_DIFF
maximum scalefactor difference allowed by standard
static int ff_sfdelta_can_remove_band(const SingleChannelElement *sce, const uint8_t *nextband, int prev_sf, int band)
int alloc
number of bits allocated by the psy, or -1 if no allocation was done
static int ff_pns_bits(SingleChannelElement *sce, int w, int g)
const uint16_t * swb_offset
table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular wind...
static double cb(void *priv, double x, double y)
AACEncOptions options
encoding options
static float find_form_factor(int group_len, int swb_size, float thresh, const float *scaled, float nzslope)
const uint8_t ff_aac_scalefactor_bits[121]
single band psychoacoustic information
int flags
AV_CODEC_FLAG_*.
int num_swb
number of scalefactor window bands
#define SCALE_DIV_512
scalefactor difference that corresponds to scale difference in 512 times
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
static uint8_t coef2minsf(float coef)
Return the minimum scalefactor where the quantized coef does not clip.
int cur_channel
current channel for coder context
uint8_t can_pns[128]
band is allowed to PNS (informative)
static void ff_init_nextband_map(const SingleChannelElement *sce, uint8_t *nextband)
AAC definitions and structures.
static av_const float ff_sqrf(float a)
Libavcodec external API header.
struct FFPsyContext::@106 bitres
static int find_min_book(float maxval, int sf)
int sample_rate
samples per second
main external API structure.
IndividualChannelStream ics
const uint8_t * swb_sizes
table of scalefactor band sizes for a particular window
int cutoff
lowpass frequency cutoff for analysis
int global_quality
Global quality for codecs which cannot change it per frame.
uint8_t zeroes[128]
band is not coded (used by encoder)
int sf_idx[128]
scalefactor indices (used by encoder)
INTFLOAT coeffs[1024]
coefficients for IMDCT, maybe processed
#define SCALE_ONE_POS
scalefactor index that corresponds to scale=1.0
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.
int cutoff
Audio cutoff bandwidth (0 means "automatic")
int channels
number of audio channels
FFPsyChannel * ch
single channel information
enum BandType band_type[128]
band types
static float find_max_val(int group_len, int swb_size, const float *scaled)
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(* abs_pow34)(float *out, const float *in, const int size)
float scoefs[1024]
scaled coefficients
#define NOISE_LOW_LIMIT
This file contains a template for the twoloop coder function.