int main(void) { int x = 0; int i, j; AVLFG state; av_lfg_init(&state, 0xdeadbeef); for (j = 0; j < 10000; j++) { START_TIMER for (i = 0; i < 624; i++) x += av_lfg_get(&state); STOP_TIMER("624 calls of av_lfg_get"); } av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x); /* BMG usage example */ { double mean = 1000; double stddev = 53; av_lfg_init(&state, 42); for (i = 0; i < 1000; i += 2) { double bmg_out[2]; av_bmg_get(&state, bmg_out); av_log(NULL, AV_LOG_INFO, "%f\n%f\n", bmg_out[0] * stddev + mean, bmg_out[1] * stddev + mean); } } return 0; }
int main(void) { int x = 0; int i, j; AVLFG state; av_lfg_init(&state, 0xdeadbeef); for (j = 0; j < 10000; j++) { START_TIMER for (i = 0; i < 624; i++) { //av_log(NULL, AV_LOG_ERROR, "%X\n", av_lfg_get(&state)); x += av_lfg_get(&state); } STOP_TIMER("624 calls of av_lfg_get"); } av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x); /* BMG usage example */ { double mean = 1000; double stddev = 53; double samp_mean = 0.0, samp_stddev = 0.0; double samp0, samp1; av_lfg_init(&state, 42); for (i = 0; i < 1000; i += 2) { double bmg_out[2]; av_bmg_get(&state, bmg_out); samp0 = bmg_out[0] * stddev + mean; samp1 = bmg_out[1] * stddev + mean; samp_mean += samp0 + samp1; samp_stddev += samp0 * samp0 + samp1 * samp1; av_log(NULL, AV_LOG_INFO, "%f\n%f\n", samp0, samp1); } /* TODO: add proper normality test */ samp_mean /= 1000; samp_stddev /= 999; samp_stddev -= (1000.0/999.0)*samp_mean*samp_mean; samp_stddev = sqrt(samp_stddev); av_log(NULL, AV_LOG_INFO, "sample mean : %f\n" "true mean : %f\n" "sample stddev: %f\n" "true stddev : %f\n", samp_mean, mean, samp_stddev, stddev); } return 0; }
static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce) { FFPsyBand *band; int w, g, w2, i; int wlen = 1024 / sce->ics.num_windows; int bandwidth, cutoff; float *PNS = &s->scoefs[0*128], *PNS34 = &s->scoefs[1*128]; float *NOR34 = &s->scoefs[3*128]; uint8_t nextband[128]; const float lambda = s->lambda; const float freq_mult = avctx->sample_rate*0.5f/wlen; const float thr_mult = NOISE_LAMBDA_REPLACE*(100.0f/lambda); const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f)); const float dist_bias = av_clipf(4.f * 120 / lambda, 0.25f, 4.0f); const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f); int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / ((avctx->flags & CODEC_FLAG_QSCALE) ? 2.0f : avctx->channels) * (lambda / 120.f); /** Keep this in sync with twoloop's cutoff selection */ float rate_bandwidth_multiplier = 1.5f; int prev = -1000, prev_sf = -1; int frame_bit_rate = (avctx->flags & CODEC_FLAG_QSCALE) ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024) : (avctx->bit_rate / avctx->channels); frame_bit_rate *= 1.15f; if (avctx->cutoff > 0) { bandwidth = avctx->cutoff; } else { bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate)); } cutoff = bandwidth * 2 * wlen / avctx->sample_rate; memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type)); ff_init_nextband_map(sce, nextband); for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { int wstart = w*128; for (g = 0; g < sce->ics.num_swb; g++) { int noise_sfi; float dist1 = 0.0f, dist2 = 0.0f, noise_amp; float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh; float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f; float min_energy = -1.0f, max_energy = 0.0f; const int start = wstart+sce->ics.swb_offset[g]; const float freq = (start-wstart)*freq_mult; const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f); if (freq < NOISE_LOW_LIMIT || (start-wstart) >= cutoff) { if (!sce->zeroes[w*16+g]) prev_sf = sce->sf_idx[w*16+g]; continue; } for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; sfb_energy += band->energy; spread = FFMIN(spread, band->spread); threshold += band->threshold; if (!w2) { min_energy = max_energy = band->energy; } else { min_energy = FFMIN(min_energy, band->energy); max_energy = FFMAX(max_energy, band->energy); } } /* Ramps down at ~8000Hz and loosens the dist threshold */ dist_thresh = av_clipf(2.5f*NOISE_LOW_LIMIT/freq, 0.5f, 2.5f) * dist_bias; /* PNS is acceptable when all of these are true: * 1. high spread energy (noise-like band) * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed) * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS) * * At this stage, point 2 is relaxed for zeroed bands near the noise threshold (hole avoidance is more important) */ if ((!sce->zeroes[w*16+g] && !ff_sfdelta_can_remove_band(sce, nextband, prev_sf, w*16+g)) || ((sce->zeroes[w*16+g] || !sce->band_alt[w*16+g]) && sfb_energy < threshold*sqrtf(1.0f/freq_boost)) || spread < spread_threshold || (!sce->zeroes[w*16+g] && sce->band_alt[w*16+g] && sfb_energy > threshold*thr_mult*freq_boost) || min_energy < pns_transient_energy_r * max_energy ) { sce->pns_ener[w*16+g] = sfb_energy; if (!sce->zeroes[w*16+g]) prev_sf = sce->sf_idx[w*16+g]; continue; } pns_tgt_energy = sfb_energy*FFMIN(1.0f, spread*spread); noise_sfi = av_clip(roundf(log2f(pns_tgt_energy)*2), -100, 155); /* Quantize */ noise_amp = -ff_aac_pow2sf_tab[noise_sfi + POW_SF2_ZERO]; /* Dequantize */ if (prev != -1000) { int noise_sfdiff = noise_sfi - prev + SCALE_DIFF_ZERO; if (noise_sfdiff < 0 || noise_sfdiff > 2*SCALE_MAX_DIFF) { if (!sce->zeroes[w*16+g]) prev_sf = sce->sf_idx[w*16+g]; continue; } } for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { float band_energy, scale, pns_senergy; const int start_c = (w+w2)*128+sce->ics.swb_offset[g]; band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; for (i = 0; i < sce->ics.swb_sizes[g]; i+=2) { double rnd[2]; av_bmg_get(&s->lfg, rnd); PNS[i+0] = (float)rnd[0]; PNS[i+1] = (float)rnd[1]; } band_energy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]); scale = noise_amp/sqrtf(band_energy); s->fdsp->vector_fmul_scalar(PNS, PNS, scale, sce->ics.swb_sizes[g]); pns_senergy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]); pns_energy += pns_senergy; abs_pow34_v(NOR34, &sce->coeffs[start_c], sce->ics.swb_sizes[g]); abs_pow34_v(PNS34, PNS, sce->ics.swb_sizes[g]); dist1 += quantize_band_cost(s, &sce->coeffs[start_c], NOR34, sce->ics.swb_sizes[g], sce->sf_idx[(w+w2)*16+g], sce->band_alt[(w+w2)*16+g], lambda/band->threshold, INFINITY, NULL, NULL, 0); /* Estimate rd on average as 5 bits for SF, 4 for the CB, plus spread energy * lambda/thr */ dist2 += band->energy/(band->spread*band->spread)*lambda*dist_thresh/band->threshold; } if (g && sce->band_type[w*16+g-1] == NOISE_BT) { dist2 += 5; } else { dist2 += 9; } energy_ratio = pns_tgt_energy/pns_energy; /* Compensates for quantization error */ sce->pns_ener[w*16+g] = energy_ratio*pns_tgt_energy; if (sce->zeroes[w*16+g] || !sce->band_alt[w*16+g] || (energy_ratio > 0.85f && energy_ratio < 1.25f && dist2 < dist1)) { sce->band_type[w*16+g] = NOISE_BT; sce->zeroes[w*16+g] = 0; prev = noise_sfi; } else { if (!sce->zeroes[w*16+g]) prev_sf = sce->sf_idx[w*16+g]; } } } }