static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, const float *in, int size, int scale_idx, int cb, const float lambda) { quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda, INFINITY, NULL); }
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) { return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx, cb, lambda, uplim, bits); }
/** * Calculate rate distortion cost for quantizing with given codebook * * @return quantization distortion */ static float quantize_and_encode_band_cost(struct AACEncContext *s, PutBitContext *pb, const float *in, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits) { const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512]; const float CLIPPED_ESCAPE = 165140.0f*IQ; int i, j, k; float cost = 0; const int dim = cb < FIRST_PAIR_BT ? 4 : 2; int resbits = 0; #ifndef USE_REALLY_FULL_SEARCH const float Q34 = sqrtf(Q * sqrtf(Q)); const int range = aac_cb_range[cb]; const int maxval = aac_cb_maxval[cb]; int offs[4]; #endif /* USE_REALLY_FULL_SEARCH */ if (!cb) { for (i = 0; i < size; i++) cost += in[i]*in[i]; if (bits) *bits = 0; return cost * lambda; } #ifndef USE_REALLY_FULL_SEARCH offs[0] = 1; for (i = 1; i < dim; i++) offs[i] = offs[i-1]*range; if (!scaled) { abs_pow34_v(s->scoefs, in, size); scaled = s->scoefs; } quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval); #endif /* USE_REALLY_FULL_SEARCH */ for (i = 0; i < size; i += dim) { float mincost; int minidx = 0; int minbits = 0; const float *vec; #ifndef USE_REALLY_FULL_SEARCH int (*quants)[2] = &s->qcoefs[i]; mincost = 0.0f; for (j = 0; j < dim; j++) mincost += in[i+j]*in[i+j]; minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40; minbits = ff_aac_spectral_bits[cb-1][minidx]; mincost = mincost * lambda + minbits; for (j = 0; j < (1<<dim); j++) { float rd = 0.0f; int curbits; int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40; int same = 0; for (k = 0; k < dim; k++) { if ((j & (1 << k)) && quants[k][0] == quants[k][1]) { same = 1; break; } } if (same) continue; for (k = 0; k < dim; k++) curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k]; curbits = ff_aac_spectral_bits[cb-1][curidx]; vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; #else mincost = INFINITY; vec = ff_aac_codebook_vectors[cb-1]; for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) { float rd = 0.0f; int curbits = ff_aac_spectral_bits[cb-1][j]; int curidx = j; #endif /* USE_REALLY_FULL_SEARCH */ if (IS_CODEBOOK_UNSIGNED(cb)) { for (k = 0; k < dim; k++) { float t = fabsf(in[i+k]); float di; if (vec[k] == 64.0f) { //FIXME: slow //do not code with escape sequence small values if (t < 39.0f*IQ) { rd = INFINITY; break; } if (t >= CLIPPED_ESCAPE) { di = t - CLIPPED_ESCAPE; curbits += 21; } else { int c = av_clip(quant(t, Q), 0, 8191); di = t - c*cbrtf(c)*IQ; curbits += av_log2(c)*2 - 4 + 1; } } else { di = t - vec[k]*IQ; } if (vec[k] != 0.0f) curbits++; rd += di*di; } } else { for (k = 0; k < dim; k++) { float di = in[i+k] - vec[k]*IQ; rd += di*di; } } rd = rd * lambda + curbits; if (rd < mincost) { mincost = rd; minidx = curidx; minbits = curbits; } } cost += mincost; resbits += minbits; if (cost >= uplim) return uplim; if (pb) { put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]); if (IS_CODEBOOK_UNSIGNED(cb)) for (j = 0; j < dim; j++) if (ff_aac_codebook_vectors[cb-1][minidx*dim+j] != 0.0f) put_bits(pb, 1, in[i+j] < 0.0f); if (cb == ESC_BT) { for (j = 0; j < 2; j++) { if (ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f) { int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191); int len = av_log2(coef); put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2); put_bits(pb, len, coef & ((1 << len) - 1)); } } } } } if (bits) *bits = resbits; return cost; } 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) { return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx, cb, lambda, uplim, bits); } static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, const float *in, int size, int scale_idx, int cb, const float lambda) { quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda, INFINITY, NULL); }
void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce) { int sfb, i, count = 0, cost_coeffs = 0, cost_pred = 0; const int pmax = FFMIN(sce->ics.max_sfb, ff_aac_pred_sfb_max[s->samplerate_index]); float *O34 = &s->scoefs[128*0], *P34 = &s->scoefs[128*1]; float *SENT = &s->scoefs[128*2], *S34 = &s->scoefs[128*3]; float *QERR = &s->scoefs[128*4]; if (sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE) { sce->ics.predictor_present = 0; return; } if (!sce->ics.predictor_initialized) { reset_all_predictors(sce->predictor_state); sce->ics.predictor_initialized = 1; memcpy(sce->prcoeffs, sce->coeffs, 1024*sizeof(float)); for (i = 1; i < 31; i++) sce->ics.predictor_reset_count[i] = i; } update_pred_resets(sce); memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type)); for (sfb = PRED_SFB_START; sfb < pmax; sfb++) { int cost1, cost2, cb_p; float dist1, dist2, dist_spec_err = 0.0f; const int cb_n = sce->band_type[sfb]; const int start_coef = sce->ics.swb_offset[sfb]; const int num_coeffs = sce->ics.swb_offset[sfb + 1] - start_coef; const FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[sfb]; if (start_coef + num_coeffs > MAX_PREDICTORS || (s->cur_channel && sce->band_type[sfb] >= INTENSITY_BT2) || sce->band_type[sfb] == NOISE_BT) continue; /* Normal coefficients */ abs_pow34_v(O34, &sce->coeffs[start_coef], num_coeffs); dist1 = quantize_and_encode_band_cost(s, NULL, &sce->coeffs[start_coef], NULL, O34, num_coeffs, sce->sf_idx[sfb], cb_n, s->lambda / band->threshold, INFINITY, &cost1, NULL, 0); cost_coeffs += cost1; /* Encoded coefficients - needed for #bits, band type and quant. error */ for (i = 0; i < num_coeffs; i++) SENT[i] = sce->coeffs[start_coef + i] - sce->prcoeffs[start_coef + i]; abs_pow34_v(S34, SENT, num_coeffs); if (cb_n < RESERVED_BT) cb_p = find_min_book(find_max_val(1, num_coeffs, S34), sce->sf_idx[sfb]); else cb_p = cb_n; quantize_and_encode_band_cost(s, NULL, SENT, QERR, S34, num_coeffs, sce->sf_idx[sfb], cb_p, s->lambda / band->threshold, INFINITY, &cost2, NULL, 0); /* Reconstructed coefficients - needed for distortion measurements */ for (i = 0; i < num_coeffs; i++) sce->prcoeffs[start_coef + i] += QERR[i] != 0.0f ? (sce->prcoeffs[start_coef + i] - QERR[i]) : 0.0f; abs_pow34_v(P34, &sce->prcoeffs[start_coef], num_coeffs); if (cb_n < RESERVED_BT) cb_p = find_min_book(find_max_val(1, num_coeffs, P34), sce->sf_idx[sfb]); else cb_p = cb_n; dist2 = quantize_and_encode_band_cost(s, NULL, &sce->prcoeffs[start_coef], NULL, P34, num_coeffs, sce->sf_idx[sfb], cb_p, s->lambda / band->threshold, INFINITY, NULL, NULL, 0); for (i = 0; i < num_coeffs; i++) dist_spec_err += (O34[i] - P34[i])*(O34[i] - P34[i]); dist_spec_err *= s->lambda / band->threshold; dist2 += dist_spec_err; if (dist2 <= dist1 && cb_p <= cb_n) { cost_pred += cost2; sce->ics.prediction_used[sfb] = 1; sce->band_alt[sfb] = cb_n; sce->band_type[sfb] = cb_p; count++; } else { cost_pred += cost1; sce->band_alt[sfb] = cb_p; } } if (count && cost_coeffs < cost_pred) { count = 0; for (sfb = PRED_SFB_START; sfb < pmax; sfb++) RESTORE_PRED(sce, sfb); memset(&sce->ics.prediction_used, 0, sizeof(sce->ics.prediction_used)); } sce->ics.predictor_present = !!count; }