]> git.sesse.net Git - ffmpeg/blob - libavcodec/qcelp_lsp.c
Part 1 of 2 of Kenan Gillet's 'make ff_qcelp_lspf2lpc
[ffmpeg] / libavcodec / qcelp_lsp.c
1 /*
2  * QCELP decoder
3  * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file libavcodec/qcelp_lsp.c
24  * QCELP decoder
25  * @author Reynaldo H. Verdejo Pinochet
26  * @remark FFmpeg merging spearheaded by Kenan Gillet
27  * @remark Development mentored by Benjamin Larson
28  */
29
30 #include "libavutil/mathematics.h"
31
32 /**
33  * initial coefficient to perform bandwidth expansion on LPC
34  *
35  * @note: 0.9883 looks like an approximation of 253/256.
36  *
37  * TIA/EIA/IS-733 2.4.3.3.6 6
38  */
39 #define QCELP_BANDWITH_EXPANSION_COEFF 0.9883
40
41 /**
42  * Computes the Pa / (1 + z(-1)) or Qa / (1 - z(-1)) coefficients
43  * needed for LSP to LPC conversion.
44  * We only need to calculate the 6 first elements of the polynomial.
45  *
46  * @param lspf line spectral pair frequencies
47  * @param f [out] polynomial input/output as a vector
48  *
49  * TIA/EIA/IS-733 2.4.3.3.5-1/2
50  */
51 static void lsp2polyf(const double *lspf, double *f, int lp_half_order)
52 {
53     int i, j;
54
55     f[0] = 1.0;
56     f[1] = -2 * lspf[0];
57     lspf -= 2;
58     for(i=2; i<=lp_half_order; i++)
59     {
60         double val = -2 * lspf[2*i];
61         f[i] = val * f[i-1] + 2*f[i-2];
62         for(j=i-1; j>1; j--)
63             f[j] += f[j-1] * val + f[j-2];
64         f[1] += val;
65     }
66 }
67
68 /**
69  * Reconstructs LPC coefficients from the line spectral pair frequencies.
70  *
71  * @param lspf line spectral pair frequencies
72  * @param lpc linear predictive coding coefficients
73  */
74 void ff_celp_lspf2lpc(const double *lspf, float *lpc)
75 {
76     double pa[6], qa[6];
77     int   i;
78
79     lsp2polyf(lspf,     pa, 5);
80     lsp2polyf(lspf + 1, qa, 5);
81
82     for (i=4; i>=0; i--)
83     {
84         double paf = pa[i+1] + pa[i];
85         double qaf = qa[i+1] - qa[i];
86
87         lpc[i  ] = 0.5 * (paf+qaf);
88         lpc[9-i] = 0.5 * (paf-qaf);
89     }
90 }
91
92 /**
93  * Reconstructs LPC coefficients from the line spectral pair frequencies
94  * and performs bandwidth expansion.
95  *
96  * @param lspf line spectral pair frequencies
97  * @param lpc linear predictive coding coefficients
98  *
99  * @note: bandwith_expansion_coeff could be precalculated into a table
100  *        but it seems to be slower on x86
101  *
102  * TIA/EIA/IS-733 2.4.3.3.5
103  */
104 void ff_qcelp_lspf2lpc(const float *lspf, float *lpc)
105 {
106     double lsf[10];
107     double bandwith_expansion_coeff = QCELP_BANDWITH_EXPANSION_COEFF;
108     int   i;
109
110     for (i=0; i<10; i++)
111         lsf[i] = cos(M_PI * lspf[i]);
112
113     ff_celp_lspf2lpc(lsf, lpc);
114
115     for (i=0; i<10; i++)
116     {
117         lpc[i] *= bandwith_expansion_coeff;
118         bandwith_expansion_coeff *= QCELP_BANDWITH_EXPANSION_COEFF;
119     }
120 }