Commit d35d6775 authored by Jean-Marc Valin's avatar Jean-Marc Valin
Browse files

Constrain the pitch gain to prevent potential instabilities.

parent 163b02eb
......@@ -211,7 +211,7 @@ void denormalise_bands(const CELTMode *m, const celt_norm_t * restrict X, celt_s
}
}
int compute_pitch_gain(const CELTMode *m, const celt_sig_t *X, const celt_sig_t *P, int norm_rate, int *gain_id, int _C)
int compute_pitch_gain(const CELTMode *m, const celt_sig_t *X, const celt_sig_t *P, int norm_rate, int *gain_id, int _C, celt_word16_t *gain_prod)
{
int j, c;
celt_word16_t g;
......@@ -281,6 +281,16 @@ int compute_pitch_gain(const CELTMode *m, const celt_sig_t *X, const celt_sig_t
*gain_id = floor(20*(g-.5));
}
#endif
/* This prevents the pitch gain from being above 1.0 for too long by bounding the
maximum error amplification factor to 2.0 */
g = ADD16(QCONST16(.5,14), MULT16_16_16(QCONST16(.05,14),*gain_id));
*gain_prod = MAX16(QCONST32(1., 13), MULT16_16_Q14(*gain_prod,g));
if (*gain_prod>QCONST32(2., 13))
{
*gain_id=9;
*gain_prod = QCONST32(2., 13);
}
if (*gain_id < 0)
{
*gain_id = 0;
......
......@@ -71,7 +71,7 @@ void denormalise_bands(const CELTMode *m, const celt_norm_t * restrict X, celt_s
* @param gains Gain computed for each pitch band (returned)
* @param bank Square root of the energy for each band
*/
int compute_pitch_gain(const CELTMode *m, const celt_sig_t *X, const celt_sig_t *P, int norm_rate, int *gain_id, int _C);
int compute_pitch_gain(const CELTMode *m, const celt_sig_t *X, const celt_sig_t *P, int norm_rate, int *gain_id, int _C, celt_word16_t *gain_prod);
void apply_pitch(const CELTMode *m, celt_sig_t *X, const celt_sig_t *P, int gain_id, int pred, int _C);
......
......@@ -88,6 +88,7 @@ struct CELTEncoder {
int delayedIntra;
celt_word16_t tonal_average;
int fold_decision;
celt_word16_t gain_prod;
int VBR_rate; /* Target number of 16th bits per frame */
celt_word16_t * restrict preemph_memE;
......@@ -650,7 +651,7 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_si
{
compute_mdcts(st->mode, 0, st->out_mem+pitch_index*C, pitch_freq, C);
has_pitch = compute_pitch_gain(st->mode, freq, pitch_freq, norm_rate, &gain_id, C);
has_pitch = compute_pitch_gain(st->mode, freq, pitch_freq, norm_rate, &gain_id, C, &st->gain_prod);
}
if (has_pitch)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment