[FFmpeg-devel] [PATCH] QCELP decoder
Michael Niedermayer
michaelni
Tue Dec 2 00:25:15 CET 2008
On Mon, Dec 01, 2008 at 03:15:40PM -0800, Kenan Gillet wrote:
> Hi,
>
> On Mon, Dec 1, 2008 at 1:39 PM, Michael Niedermayer <michaelni at gmx.at> wrote:
> > On Mon, Dec 01, 2008 at 12:45:09PM -0800, Kenan Gillet wrote:
> >> Hi,
> >> On Mon, Dec 1, 2008 at 8:24 AM, Michael Niedermayer <michaelni at gmx.at> wrote:
>
> [...]
>
>
> >> Index: libavcodec/qcelpdec.c
> >> ===================================================================
> >> --- libavcodec/qcelpdec.c (revision 15972)
> >> +++ libavcodec/qcelpdec.c (working copy)
>
> [...]
>
>
> >> @@ -476,6 +495,64 @@
> >> }
> >>
> >> /**
> >> + * Apply pitch synthesis filter and pitch prefilter to the scaled codebook vector.
> >> + * TIA/EIA/IS-733 2.4.5.2
> >> + *
> >> + * @param q the context
> >> + * @param cdn_vector the scaled codebook vector
> >> + */
> >> +static void apply_pitch_filters(QCELPContext *q,
> >> + float *cdn_vector) {
> >> + int i;
> >> + float gain[4];
> >> + const float *v_synthesis_filtered, *v_pre_filtered;
> >> +
> >> + if (q->bitrate >= RATE_HALF ||
> >> + (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF))) {
> >> +
> >> + if (q->bitrate >= RATE_HALF) {
> >> +
> >> + // Compute gain & lag for the whole frame.
> >> + for (i = 0; i < 4; i++) {
> >> + gain[i] = q->frame.plag[i] ? (q->frame.pgain[i] + 1) * 0.25 : 0.0;
> >> +
> >> + q->frame.plag[i] += 16;
> >> + }
> >> + memcpy(q->prev_pitch_lag, q->frame.plag, sizeof(q->frame.plag));
> >> + } else {
> >> + gain[3] = q->erasure_count < 3 ? 0.9 - 0.3 * (q->erasure_count - 1)
> >> + : 0.0;
> >> + for (i = 0; i < 4; i++)
> >> + gain[i] = FFMIN(q->prev_pitch_gain[i], gain[3]);
> >> +
> >> + memset(q->frame.pfrac, 0, sizeof(q->frame.pfrac));
> >
> >> + memcpy(q->frame.plag, q->prev_pitch_lag, sizeof(q->frame.plag));
> >
> > i think if prev_pitch_lag was used in the surrounding code then this memcpy
> > would be unneeded
>
> done, and var renamed to pitch_lag
>
>
> >> + }
> >> +
> >> + // pitch synthesis filter
> >> + v_synthesis_filtered = do_pitchfilter(q->pitch_synthesis_filter_mem, cdn_vector,
> >> + gain, q->frame.plag, q->frame.pfrac);
> >> +
> >> + // pitch prefilter update
> >> + for (i = 0; i < 4; i++)
> >> + gain[i] = 0.5 * FFMIN(gain[i], 1.0);
> >> +
> >> + v_pre_filtered = do_pitchfilter(q->pitch_pre_filter_mem, v_synthesis_filtered,
> >> + gain, q->frame.plag, q->frame.pfrac);
> >> +
> >> + apply_gain_ctrl(cdn_vector, v_synthesis_filtered, v_pre_filtered);
> >> +
> >
> >> + memcpy(q->prev_pitch_gain, gain, sizeof(q->prev_pitch_gain));
> >
> > cant prev_pitch_gain be directly used instead of gain that then is copied
> > back?
> > (possibly with a better var name than prev_pitch_gain)
>
> done, and var renamed to pitch_gain
>
>
> >
> >
> >> +
> >> + } else {
> >> + memcpy(q->pitch_synthesis_filter_mem, cdn_vector + 17, 143 * sizeof(float));
> >> + memcpy(q->pitch_pre_filter_mem, cdn_vector + 17, 143 * sizeof(float));
> >> + memset(q->prev_pitch_gain, 0, sizeof(q->prev_pitch_gain));
> >> + memset(q->prev_pitch_lag, 0, sizeof(q->prev_pitch_lag));
> >> + }
> >> +}
> >> +
> >> +/**
> >> * Interpolates LSP frequencies and computes LPC coefficients
> >> * for a given bitrate & pitch subframe.
> >> *
> >
>
> [...]
>
>
> >
> > [...]
> >> Index: libavformat/mov.c
> >> ===================================================================
> >> --- libavformat/mov.c (revision 15972)
> >> +++ libavformat/mov.c (working copy)
> >> @@ -988,6 +988,10 @@
> >> #endif
> >> /* no ifdef since parameters are always those */
> >> case CODEC_ID_QCELP:
> >> + st->need_parsing = AVSTREAM_PARSE_FULL;
> >> + st->codec->frame_size= 160;
> >
> >
> >> + st->codec->channels= 1; /* really needed */
> >
> > if this is really needed then its ok
>
> when removed, some samples wav output changes,
> plus QCELP only handles mono.
>
> >
> >
> >> + break;
> >> case CODEC_ID_AMR_NB:
> >> case CODEC_ID_AMR_WB:
> >> st->codec->frame_size= sc->samples_per_frame;
> > [...]
> >
>
> round 15 attached.
>
> note: there is a small update to the glue code in libavformat/mov.c
> so I repost the doc-glue patch for this particular round but it has not
> changed otherwise.
>
> thanks for all your help,
>
> Kenan
> Index: libavcodec/qcelpdec.c
> ===================================================================
> --- libavcodec/qcelpdec.c (revision 15975)
> +++ libavcodec/qcelpdec.c (working copy)
> @@ -65,8 +65,8 @@
> float last_codebook_gain;
> int prev_g1[2];
> int prev_bitrate;
> - float prev_pitch_gain[4];
> - uint8_t prev_pitch_lag[4];
> + float pitch_gain[4];
> + uint8_t pitch_lag[4];
> uint16_t first16bits;
> } QCELPContext;
>
> @@ -75,7 +75,7 @@
> *
> * TIA/EIA/IS-733 2.4.3.3.5
> */
> -void qcelp_lspf2lpc(const float *lspf, float *lpc);
> +void ff_qcelp_lspf2lpc(const float *lspf, float *lpc);
>
> static void weighted_vector_sumf(float *out, const float *in_a,
> const float *in_b, float weight_coeff_a,
> @@ -495,6 +495,58 @@
> }
>
> /**
> + * Apply pitch synthesis filter and pitch prefilter to the scaled codebook vector.
> + * TIA/EIA/IS-733 2.4.5.2
> + *
> + * @param q the context
> + * @param cdn_vector the scaled codebook vector
> + */
> +static void apply_pitch_filters(QCELPContext *q,
> + float *cdn_vector) {
> + int i;
> + const float *v_synthesis_filtered, *v_pre_filtered;
> +
> + if (q->bitrate >= RATE_HALF ||
> + (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF))) {
> +
> + if (q->bitrate >= RATE_HALF) {
> +
> + // Compute gain & lag for the whole frame.
> + for (i = 0; i < 4; i++) {
> + q->pitch_gain[i] = q->frame.plag[i] ? (q->frame.pgain[i] + 1) * 0.25 : 0.0;
> +
> + q->pitch_lag[i] = q->frame.plag[i] + 16;
> + }
> + } else {
> + float max_pitch_gain = q->erasure_count < 3 ? 0.9 - 0.3 * (q->erasure_count - 1)
> + : 0.0;
> + for (i = 0; i < 4; i++)
> + q->pitch_gain[i] = FFMIN(q->pitch_gain[i], max_pitch_gain);
> +
> + memset(q->frame.pfrac, 0, sizeof(q->frame.pfrac));
> + }
> +
> + // pitch synthesis filter
> + v_synthesis_filtered = do_pitchfilter(q->pitch_synthesis_filter_mem, cdn_vector,
> + q->pitch_gain, q->pitch_lag, q->frame.pfrac);
> +
> + // pitch prefilter update
> + for (i = 0; i < 4; i++)
> + q->pitch_gain[i] = 0.5 * FFMIN(q->pitch_gain[i], 1.0);
> +
> + v_pre_filtered = do_pitchfilter(q->pitch_pre_filter_mem, v_synthesis_filtered,
> + q->pitch_gain, q->pitch_lag, q->frame.pfrac);
> +
> + apply_gain_ctrl(cdn_vector, v_synthesis_filtered, v_pre_filtered);
> + } else {
> + memcpy(q->pitch_synthesis_filter_mem, cdn_vector + 17, 143 * sizeof(float));
> + memcpy(q->pitch_pre_filter_mem, cdn_vector + 17, 143 * sizeof(float));
> + memset(q->pitch_gain, 0, sizeof(q->pitch_gain));
> + memset(q->pitch_lag, 0, sizeof(q->pitch_lag));
> + }
> +}
> +
> +/**
> * Interpolates LSP frequencies and computes LPC coefficients
> * for a given bitrate & pitch subframe.
> *
> @@ -522,9 +574,9 @@
> {
> weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
> weight, 1.0 - weight, 10);
> - qcelp_lspf2lpc(interpolated_lspf, lpc);
> + ff_qcelp_lspf2lpc(interpolated_lspf, lpc);
> }else if(q->bitrate >= RATE_QUARTER || (q->bitrate == I_F_Q && !subframe_num))
> - qcelp_lspf2lpc(curr_lspf, lpc);
> + ff_qcelp_lspf2lpc(curr_lspf, lpc);
> }
>
> static int buf_size2bitrate(const int buf_size)
ok
[...]
> Index: libavcodec/qcelp_lsp.c
> ===================================================================
> --- libavcodec/qcelp_lsp.c (revision 0)
> +++ libavcodec/qcelp_lsp.c (revision 0)
> @@ -0,0 +1,98 @@
> +/*
> + * QCELP decoder
> + * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +/**
> + * @file qcelp_lsp.c
> + * QCELP decoder
> + * @author Reynaldo H. Verdejo Pinochet
> + */
> +
> +#include "libavutil/mathematics.h"
> +
> +/**
> + * initial coefficient to perform bandwidth expansion on LPC
> + *
> + * TIA/EIA/IS-733 2.4.3.3.6 6
> + */
> +#define QCELP_BANDWITH_EXPANSION_COEFF 0.9883
> +
> +/**
> + * Computes the Pa / (1 + z(-1)) or Qa / (1 - z(-1)) coefficients
> + * needed for LSP to LPC conversion.
> + * We only need to calculate the 6 first elements of the polynomial.
> + *
> + * @param lspf line spectral pair frequencies
> + * @param f [out] polynomial input/output as a vector
> + *
> + * TIA/EIA/IS-733 2.4.3.3.5-1/2
> + */
> +static void lsp2polyf(const float *lspf, double *f, int lp_half_order)
> +{
> + int i, j;
> +
> + f[0] = 1.0;
> + f[1] = -2 * cos(M_PI * lspf[0]);
> + lspf -= 2;
> + for(i=2; i<=lp_half_order; i++)
> + {
> + double val = -2 * cos(M_PI * lspf[2*i]);
> + f[i] = val * f[i-1] + 2*f[i-2];
> + for(j=i-1; j>1; j--)
> + f[j] += f[j-1] * val + f[j-2];
> + f[1] += val;
> + }
> +}
> +
> +/**
> + * Reconstructs LPC coefficients from the line spectral pair frequencies
> + * and performs bandwidth expansion.
> + *
> + * @param lspf line spectral pair frequencies
> + * @param lpc linear predictive coding coefficients
> + *
> + * @note: bandwith_expansion_coeff could be precalculated into a table
> + * but it seems to be slower on x86
> + *
> + * TIA/EIA/IS-733 2.4.3.3.5
> + */
> +void ff_qcelp_lspf2lpc(const float *lspf, float *lpc)
> +{
> + double pa[6], qa[6];
> + int i;
> + double bandwith_expansion_coeff = -QCELP_BANDWITH_EXPANSION_COEFF * 0.5;
> +
> + lsp2polyf(lspf, pa, 5);
> + lsp2polyf(lspf + 1, qa, 5);
> +
> + for (i=4; i>=0; i--)
> + {
> + double paf = pa[i+1] + pa[i];
> + double qaf = qa[i+1] - qa[i];
> +
> + lpc[i ] = paf + qaf;
> + lpc[9-i] = paf - qaf;
> + }
> + for (i=0; i<10; i++)
> + {
> + lpc[i] *= bandwith_expansion_coeff;
> + bandwith_expansion_coeff *= QCELP_BANDWITH_EXPANSION_COEFF;
> + }
> +}
ok
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
When you are offended at any man's fault, turn to yourself and study your
own failings. Then you will forget your anger. -- Epictetus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20081202/8014103a/attachment.pgp>
More information about the ffmpeg-devel
mailing list