Commit 979c397ce7 for ffmpeg
commit 979c397ce79b02448f1b6976cf981f6e70fa96d0
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Wed Dec 2 11:03:34 2026 -0800
avcodec/qcelpdec: fix adaptive postfilter
Apply the recursive anti-tilt filter and update gain once per
40-sample subframe using its interpolated LPCs. Initialize gain
to one and decay it on a zero-input subframe with a nonzero filter
tail, as specified in IS-733 sections 2.4.8.6 and 2.4.9.
Assisted-by: Fairy
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
diff --git a/libavcodec/qcelpdec.c b/libavcodec/qcelpdec.c
index d7e4910973..87cb9689a6 100644
--- a/libavcodec/qcelpdec.c
+++ b/libavcodec/qcelpdec.c
@@ -96,6 +96,8 @@ static av_cold int qcelp_decode_init(AVCodecContext *avctx)
for (i = 0; i < 10; i++)
q->prev_lspf[i] = (i + 1) / 11.0;
+ q->postfilter_agc_mem = 1.0f;
+
return 0;
}
@@ -653,7 +655,8 @@ static void warn_insufficient_frame_quality(AVCodecContext *avctx,
avctx->frame_num, message);
}
-static void postfilter(QCELPContext *q, float *samples, float *lpc)
+static void postfilter(QCELPContext *q, float *samples, const float *lpc,
+ const float *dry)
{
static const float pow_0_775[10] = {
0.775000, 0.600625, 0.465484, 0.360750, 0.279582,
@@ -662,7 +665,8 @@ static void postfilter(QCELPContext *q, float *samples, float *lpc)
0.625000, 0.390625, 0.244141, 0.152588, 0.095367,
0.059605, 0.037253, 0.023283, 0.014552, 0.009095
};
- float lpc_s[10], lpc_p[10], pole_out[170], zero_out[160];
+ float lpc_s[10], lpc_p[10], pole_out[50], zero_out[40];
+ float input_energy, output_energy;
int n;
for (n = 0; n < 10; n++) {
@@ -671,18 +675,25 @@ static void postfilter(QCELPContext *q, float *samples, float *lpc)
}
ff_celp_lp_zero_synthesis_filterf(zero_out, lpc_s,
- q->formant_mem + 10, 160, 10);
+ dry, 40, 10);
memcpy(pole_out, q->postfilter_synth_mem, sizeof(float) * 10);
- ff_celp_lp_synthesis_filterf(pole_out + 10, lpc_p, zero_out, 160, 10);
- memcpy(q->postfilter_synth_mem, pole_out + 160, sizeof(float) * 10);
+ ff_celp_lp_synthesis_filterf(pole_out + 10, lpc_p, zero_out, 40, 10);
+ memcpy(q->postfilter_synth_mem, pole_out + 40, sizeof(float) * 10);
+
+ /* B(z) = 1 / (1 + 0.3 z^-1), TIA/EIA/IS-733 2.4.8.6-2. */
+ for (n = 0; n < 40; n++) {
+ pole_out[n + 10] -= 0.3f * q->postfilter_tilt_mem;
+ q->postfilter_tilt_mem = pole_out[n + 10];
+ }
- ff_tilt_compensation(&q->postfilter_tilt_mem, 0.3, pole_out + 10, 160);
+ input_energy = ff_scalarproduct_float_c(dry, dry, 40);
+ output_energy = ff_scalarproduct_float_c(pole_out + 10, pole_out + 10, 40);
+ if (output_energy > 0.0f)
+ q->postfilter_agc_mem = 0.9375f * q->postfilter_agc_mem +
+ 0.0625f * sqrtf(input_energy / output_energy);
- ff_adaptive_gain_control(samples, pole_out + 10,
- ff_scalarproduct_float_c(q->formant_mem + 10,
- q->formant_mem + 10,
- 160),
- 160, 0.9375, &q->postfilter_agc_mem);
+ for (n = 0; n < 40; n++)
+ samples[n] = pole_out[n + 10] * q->postfilter_agc_mem;
}
static int qcelp_decode_frame(AVCodecContext *avctx, AVFrame *frame,
@@ -775,12 +786,10 @@ erasure:
interpolate_lpc(q, quantized_lspf, lpc, i);
ff_celp_lp_synthesis_filterf(formant_mem, lpc,
outbuffer + i * 40, 40, 10);
+ postfilter(q, outbuffer + i * 40, lpc, formant_mem);
formant_mem += 40;
}
- // postfilter, as per TIA/EIA/IS-733 2.4.8.6
- postfilter(q, outbuffer, lpc);
-
memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float));
memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf));
diff --git a/tests/fate/voice.mak b/tests/fate/voice.mak
index fa35584ddb..2cfaaced45 100644
--- a/tests/fate/voice.mak
+++ b/tests/fate/voice.mak
@@ -105,7 +105,8 @@ fate-gsm: $(FATE_GSM-yes)
FATE_VOICE-$(call PCM, QCP, QCELP, ARESAMPLE_FILTER) += fate-qcelp
fate-qcelp: CMD = pcm -i $(TARGET_SAMPLES)/qcp/0036580847.QCP
fate-qcelp: CMP = oneoff
-fate-qcelp: REF = $(SAMPLES)/qcp/0036580847.pcm
+fate-qcelp: REF = $(SAMPLES)/qcp/0036580847-is733.pcm
+fate-qcelp: FUZZ = 2
FATE_VOICE-$(call PCM, WAV, TRUESPEECH) += fate-truespeech
fate-truespeech: CMD = pcm -i $(TARGET_SAMPLES)/truespeech/a6.wav