From 05686a5d6e366d3a067c39f1b8567def7baa450d Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Mon, 11 Feb 2008 17:54:28 +1100 Subject: [PATCH] As extra safety, make sure not to use pitch prediction when it would make the "effective gain" too high (>10 dB). --- libcelt/celt.c | 61 +++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/libcelt/celt.c b/libcelt/celt.c index e3993a8f..83f3b7af 100644 --- a/libcelt/celt.c +++ b/libcelt/celt.c @@ -151,9 +151,10 @@ void celt_encoder_destroy(CELTEncoder *st) } -static void compute_mdcts(mdct_lookup *mdct_lookup, float *window, float *in, float *out, int N, int B, int C) +static float compute_mdcts(mdct_lookup *mdct_lookup, float *window, float *in, float *out, int N, int B, int C) { int i, c; + float E = 1e-15; for (c=0;cmode->nbEBands*C]; float gains[st->mode->nbPBands]; int pitch_index; - + float curr_power, pitch_power; + N4 = (N-st->overlap)/2; for (c=0;cmdct_lookup, st->window, in, X, N, B, C); + curr_power = compute_mdcts(&st->mdct_lookup, st->window, in, X, N, B, C); #if 0 /* Mask disabled until it can be made to do something useful */ compute_mdct_masking(X, mask, B*C*N, st->Fs); @@ -261,8 +267,9 @@ int celt_encode(CELTEncoder *st, celt_int16_t *pcm, unsigned char *compressed, i find_spectral_pitch(st->fft, &st->psy, in, st->out_mem, MAX_PERIOD, (B+1)*N, C, &pitch_index); /* Compute MDCTs of the pitch part */ - compute_mdcts(&st->mdct_lookup, st->window, st->out_mem+pitch_index*C, P, N, B, C); + pitch_power = compute_mdcts(&st->mdct_lookup, st->window, st->out_mem+pitch_index*C, P, N, B, C); + //printf ("%f %f\n", curr_power, pitch_power); /*int j; for (j=0;jmode->nbEBands;i++)printf("%f ", bandE[i]);printf("\n"); //for (i=0;imode, bandE, st->oldBandE, &st->enc); + + /* Check if we can safely use the pitch (i.e. effective gain isn't too high) */ + if (curr_power + 1e5f < 10.f*pitch_power) { + /* Normalise the pitch vector as well (discard the energies) */ float bandEp[st->mode->nbEBands*st->mode->nbChannels]; compute_band_energies(st->mode, P, bandEp); normalise_bands(st->mode, P, bandEp); + + if (C==2) + { + stereo_mix(st->mode, X, bandE, 1); + stereo_mix(st->mode, P, bandE, 1); + } + /* Simulates intensity stereo */ + //for (i=30;imode, X, P, gains, bandE); + has_pitch = quant_pitch(gains, st->mode->nbPBands, &st->enc); + if (has_pitch) + ec_enc_uint(&st->enc, pitch_index, MAX_PERIOD-(B+1)*N); + } else { + /* No pitch, so we just pretend we found a gain of zero */ + for (i=0;imode->nbPBands;i++) + gains[i] = 0; + ec_enc_uint(&st->enc, 0, 128); + for (i=0;imode, bandE, st->oldBandE, &st->enc); - - if (C==2) - { - stereo_mix(st->mode, X, bandE, 1); - stereo_mix(st->mode, P, bandE, 1); - } - /* Simulates intensity stereo */ - //for (i=30;imode, X, P, gains, bandE); - has_pitch = quant_pitch(gains, st->mode->nbPBands, &st->enc); - if (has_pitch) - ec_enc_uint(&st->enc, pitch_index, MAX_PERIOD-(B+1)*N); pitch_quant_bands(st->mode, X, P, gains);