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;
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]) {
313 memcpy(euplims, uplims,
sizeof(euplims));
320 float cleanup_factor =
ff_sqrf(av_clipf(start / (cutoff * 0.75f), 1.0f, 2.0f));
325 nzslope * cleanup_factor);
326 energy2uplim *= de_psy_factor;
329 energy2uplim = sqrtf(energy2uplim);
331 energy2uplim =
FFMAX(0.015625f,
FFMIN(1.0f, energy2uplim));
332 uplims[w*16+
g] *= av_clipf(rdlambda * energy2uplim, rdmin, rdmax)
340 energy2uplim *= de_psy_factor;
343 energy2uplim = sqrtf(energy2uplim);
345 energy2uplim =
FFMAX(0.015625f,
FFMIN(1.0f, energy2uplim));
346 euplims[w*16+
g] *= av_clipf(rdlambda * energy2uplim * sce->
ics.
group_len[w],
353 for (i = 0; i <
sizeof(maxsf) /
sizeof(maxsf[0]); ++i)
361 int qstep = its ? 1 : 32;
375 float qenergy = 0.0f;
377 if (sce->
zeroes[w*16+g] || sce->
sf_idx[w*16+g] >= 218) {
401 dists[w*16+
g] = dist -
bits;
402 qenergies[w*16+
g] = qenergy;
412 if (tbits > toomanybits) {
414 for (i = 0; i < 128; i++) {
417 int new_sf =
FFMIN(maxsf_i, sce->
sf_idx[i] + qstep);
418 if (new_sf != sce->
sf_idx[i]) {
424 }
else if (tbits < toofewbits) {
426 for (i = 0; i < 128; i++) {
429 if (new_sf != sce->
sf_idx[i]) {
437 if (!qstep && tbits > toomanybits && sce->
sf_idx[0] < 217 && changed)
442 fflag = tbits < toofewbits;
443 for (i = 0; i < 2 && (overdist || recomprd); ++i) {
456 float qenergy = 0.0f;
458 if (sce->
zeroes[w*16+g] || sce->
sf_idx[w*16+g] >= 218) {
482 dists[w*16+
g] = dist -
bits;
483 qenergies[w*16+
g] = qenergy;
494 if (!i && s->
options.
pns && its > maxits/2 && tbits > toofewbits) {
495 float maxoverdist = 0.0f;
496 float ovrfactor = 1.f+(maxits-its)*16.f/maxits;
497 overdist = recomprd = 0;
501 float ovrdist = dists[w*16+
g] /
FFMAX(uplims[w*16+g],euplims[w*16+g]);
502 maxoverdist =
FFMAX(maxoverdist, ovrdist);
511 float minspread = max_spread_thr_r;
512 float maxspread = min_spread_thr_r;
516 int maxzeroed, zloop;
519 if (start >= pns_start_pos && !sce->
zeroes[w*16+g] && sce->
can_pns[w*16+g]) {
520 minspread =
FFMIN(minspread, spread_thr_r[w*16+g]);
521 maxspread =
FFMAX(maxspread, spread_thr_r[w*16+g]);
526 zspread = (maxspread-minspread) * 0.0125f + minspread;
532 zspread =
FFMIN3(min_spread_thr_r * 8.f, zspread,
533 ((toomanybits - tbits) * min_spread_thr_r + (tbits - toofewbits) * max_spread_thr_r) / (toomanybits - toofewbits + 1));
534 maxzeroed =
FFMIN(zeroable,
FFMAX(1, (zeroable * its + maxits - 1) / (2 * maxits)));
535 for (zloop = 0; zloop < 2; zloop++) {
542 float loopovrfactor = (zloop) ? 1.0f : ovrfactor;
545 for (g = sce->
ics.
num_swb-1; g > 0 && zeroed < maxzeroed; g--) {
549 if (!sce->
zeroes[w*16+g] && sce->
can_pns[w*16+g] && spread_thr_r[w*16+g] <= zspread
550 && sce->
sf_idx[w*16+g] > loopminsf
551 && (dists[w*16+g] > loopovrfactor*uplims[w*16+g] || !(mcb =
find_min_book(maxvals[w*16+g], sce->
sf_idx[w*16+g]))
552 || (mcb <= 1 && dists[w*16+g] >
FFMIN(uplims[w*16+g], euplims[w*16+g]))) ) {
561 recomprd = fflag = 1;
572 if (!sce->
zeroes[w*16+g]) {
583 int depth = (its > maxits/2) ? ((its > maxits*2/3) ? 1 : 3) : 10;
584 int edepth = depth+2;
585 float uplmax = its / (maxits*0.25f) + 1.0f;
586 uplmax *= (tbits > destbits) ?
FFMIN(2.0f, tbits / (
float)
FFMAX(1,destbits)) : 1.0f;
589 int prevsc = sce->
sf_idx[w*16+
g];
590 if (prev < 0 && !sce->zeroes[w*16+g])
592 if (!sce->
zeroes[w*16+g]) {
598 if ((!cmb || dists[w*16+g] > uplims[w*16+g]) && sce->
sf_idx[w*16+g] > mindeltasf) {
605 for (i = 0; i < edepth && sce->
sf_idx[w*16+
g] > mindeltasf; ++i) {
610 dist = qenergy = 0.f;
613 maxsf[w*16+
g] =
FFMIN(sce->
sf_idx[w*16+g]-1, maxsf[w*16+g]);
614 }
else if (i >= depth && dists[w*16+g] < euplims[w*16+g]) {
621 if (!g && sce->
ics.
num_windows > 1 && dists[w*16+g] >= euplims[w*16+g])
622 maxsf[w*16+
g] =
FFMIN(sce->
sf_idx[w*16+g], maxsf[w*16+g]);
639 dists[w*16+
g] = dist -
bits;
640 qenergies[w*16+
g] = qenergy;
641 if (mb && (sce->
sf_idx[w*16+g] < mindeltasf || (
642 (dists[w*16+g] <
FFMIN(uplmax*uplims[w*16+g], euplims[w*16+g]))
643 && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g])
648 }
else if (tbits > toofewbits && sce->
sf_idx[w*16+g] <
FFMIN(maxdeltasf, maxsf[w*16+g])
649 && (dists[w*16+g] <
FFMIN(euplims[w*16+g], uplims[w*16+g]))
650 && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g])
653 for (i = 0; i < depth && sce->
sf_idx[w*16+
g] < maxdeltasf; ++i) {
658 dist = qenergy = 0.f;
676 if (dist <
FFMIN(euplims[w*16+g], uplims[w*16+g])) {
678 dists[w*16+
g] = dist;
679 qenergies[w*16+
g] = qenergy;
684 maxsf[w*16+
g] =
FFMIN(sce->
sf_idx[w*16+g], maxsf[w*16+g]);
689 prev = sce->
sf_idx[w*16+
g] = av_clip(sce->
sf_idx[w*16+g], mindeltasf, maxdeltasf);
690 if (sce->
sf_idx[w*16+g] != prevsc)
692 nminscaler =
FFMIN(nminscaler, sce->
sf_idx[w*16+g]);
703 if (!sce->
zeroes[w*16+g]) {
704 int prevsf = sce->
sf_idx[w*16+
g];
710 if (!fflag && prevsf != sce->
sf_idx[w*16+g])
717 }
while (fflag && its < maxits);
726 if (!sce->
zeroes[w*16+g]) {
741 if (!sce->
zeroes[w*16+g]) {
745 }
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
static void abs_pow34_v(float *out, const float *in, const int size)
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
struct FFPsyContext::@81 bitres
int flags
AV_CODEC_FLAG_*.
#define CODEC_FLAG_QSCALE
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.
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.
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)
float scoefs[1024]
scaled coefficients
#define NOISE_LOW_LIMIT
This file contains a template for the twoloop coder function.