]> git.sesse.net Git - ffmpeg/blob - libavcodec/sipr16k.c
dwt: Remove unused code.
[ffmpeg] / libavcodec / sipr16k.c
1 /*
2  * SIPR decoder for the 16k mode
3  *
4  * Copyright (c) 2008 Vladimir Voroshilov
5  * Copyright (c) 2009 Vitor Sessak
6  *
7  * This file is part of Libav.
8  *
9  * Libav is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * Libav is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <math.h>
25
26 #include "sipr.h"
27 #include "libavutil/common.h"
28 #include "libavutil/mathematics.h"
29 #include "lsp.h"
30 #include "celp_math.h"
31 #include "acelp_vectors.h"
32 #include "acelp_pitch_delay.h"
33 #include "acelp_filters.h"
34 #include "celp_filters.h"
35
36 #include "sipr16kdata.h"
37
38 /**
39  * Convert an lsf vector into an lsp vector.
40  *
41  * @param lsf               input lsf vector
42  * @param lsp               output lsp vector
43  */
44 static void lsf2lsp(const float *lsf, double *lsp)
45 {
46     int i;
47
48     for (i = 0; i < LP_FILTER_ORDER_16k; i++)
49         lsp[i] = cosf(lsf[i]);
50 }
51
52 static void dequant(float *out, const int *idx, const float *cbs[])
53 {
54     int i;
55
56     for (i = 0; i < 4; i++)
57         memcpy(out + 3*i, cbs[i] + 3*idx[i], 3*sizeof(float));
58
59     memcpy(out + 12, cbs[4] + 4*idx[4], 4*sizeof(float));
60 }
61
62 static void lsf_decode_fp_16k(float* lsf_history, float* isp_new,
63                               const int* parm, int ma_pred)
64 {
65     int i;
66     float isp_q[LP_FILTER_ORDER_16k];
67
68     dequant(isp_q, parm, lsf_codebooks_16k);
69
70     for (i = 0; i < LP_FILTER_ORDER_16k; i++) {
71         isp_new[i] = (1 - qu[ma_pred]) * isp_q[i]
72                     +     qu[ma_pred]  * lsf_history[i]
73                     + mean_lsf_16k[i];
74     }
75
76     memcpy(lsf_history, isp_q, LP_FILTER_ORDER_16k * sizeof(float));
77 }
78
79 static int dec_delay3_1st(int index)
80 {
81     if (index < 390) {
82         return index + 88;
83     } else
84         return 3 * index - 690;
85 }
86
87 static int dec_delay3_2nd(int index, int pit_min, int pit_max,
88                           int pitch_lag_prev)
89 {
90     if (index < 62) {
91         int pitch_delay_min = av_clip(pitch_lag_prev - 10,
92                                       pit_min, pit_max - 19);
93         return 3 * pitch_delay_min + index - 2;
94     } else
95         return 3 * pitch_lag_prev;
96 }
97
98 static void postfilter(float *out_data, float* synth, float* iir_mem,
99                        float* filt_mem[2], float* mem_preemph)
100 {
101     float buf[30 + LP_FILTER_ORDER_16k];
102     float *tmpbuf = buf + LP_FILTER_ORDER_16k;
103     float s;
104     int i;
105
106     for (i = 0; i < LP_FILTER_ORDER_16k; i++)
107         filt_mem[0][i] = iir_mem[i] * ff_pow_0_5[i];
108
109     memcpy(tmpbuf - LP_FILTER_ORDER_16k, mem_preemph,
110            LP_FILTER_ORDER_16k*sizeof(*buf));
111
112     ff_celp_lp_synthesis_filterf(tmpbuf, filt_mem[1], synth, 30,
113                                  LP_FILTER_ORDER_16k);
114
115     memcpy(synth - LP_FILTER_ORDER_16k, mem_preemph,
116            LP_FILTER_ORDER_16k * sizeof(*synth));
117
118     ff_celp_lp_synthesis_filterf(synth, filt_mem[0], synth, 30,
119                                  LP_FILTER_ORDER_16k);
120
121     memcpy(out_data + 30 - LP_FILTER_ORDER_16k,
122            synth    + 30 - LP_FILTER_ORDER_16k,
123            LP_FILTER_ORDER_16k * sizeof(*synth));
124
125     ff_celp_lp_synthesis_filterf(out_data + 30, filt_mem[0],
126                                  synth + 30, 2 * L_SUBFR_16k - 30,
127                                  LP_FILTER_ORDER_16k);
128
129
130     memcpy(mem_preemph, out_data + 2*L_SUBFR_16k - LP_FILTER_ORDER_16k,
131            LP_FILTER_ORDER_16k * sizeof(*synth));
132
133     FFSWAP(float *, filt_mem[0], filt_mem[1]);
134     for (i = 0, s = 0; i < 30; i++, s += 1.0/30)
135         out_data[i] = tmpbuf[i] + s * (synth[i] - tmpbuf[i]);
136 }
137
138 /**
139  * Floating point version of ff_acelp_lp_decode().
140  */
141 static void acelp_lp_decodef(float *lp_1st, float *lp_2nd,
142                              const double *lsp_2nd, const double *lsp_prev)
143 {
144     double lsp_1st[LP_FILTER_ORDER_16k];
145     int i;
146
147     /* LSP values for first subframe (3.2.5 of G.729, Equation 24) */
148     for (i = 0; i < LP_FILTER_ORDER_16k; i++)
149         lsp_1st[i] = (lsp_2nd[i] + lsp_prev[i]) * 0.5;
150
151     ff_acelp_lspd2lpc(lsp_1st, lp_1st, LP_FILTER_ORDER_16k >> 1);
152
153     /* LSP values for second subframe (3.2.5 of G.729) */
154     ff_acelp_lspd2lpc(lsp_2nd, lp_2nd, LP_FILTER_ORDER_16k >> 1);
155 }
156
157 /**
158  * Floating point version of ff_acelp_decode_gain_code().
159  */
160 static float acelp_decode_gain_codef(float gain_corr_factor, const float *fc_v,
161                                      float mr_energy, const float *quant_energy,
162                                      const float *ma_prediction_coeff,
163                                      int subframe_size, int ma_pred_order)
164 {
165     mr_energy +=
166         ff_dot_productf(quant_energy, ma_prediction_coeff, ma_pred_order);
167
168     mr_energy = gain_corr_factor * exp(M_LN10 / 20. * mr_energy) /
169         sqrt((0.01 + ff_dot_productf(fc_v, fc_v, subframe_size)));
170     return mr_energy;
171 }
172
173 #define DIVIDE_BY_3(x) ((x) * 10923 >> 15)
174
175 void ff_sipr_decode_frame_16k(SiprContext *ctx, SiprParameters *params,
176                               float *out_data)
177 {
178     int frame_size = SUBFRAME_COUNT_16k * L_SUBFR_16k;
179     float *synth = ctx->synth_buf + LP_FILTER_ORDER_16k;
180     float lsf_new[LP_FILTER_ORDER_16k];
181     double lsp_new[LP_FILTER_ORDER_16k];
182     float Az[2][LP_FILTER_ORDER_16k];
183     float fixed_vector[L_SUBFR_16k];
184     float pitch_fac, gain_code;
185
186     int i;
187     int pitch_delay_3x;
188
189     float *excitation = ctx->excitation + 292;
190
191     lsf_decode_fp_16k(ctx->lsf_history, lsf_new, params->vq_indexes,
192                       params->ma_pred_switch);
193
194     ff_set_min_dist_lsf(lsf_new, LSFQ_DIFF_MIN / 2, LP_FILTER_ORDER_16k);
195
196     lsf2lsp(lsf_new, lsp_new);
197
198     acelp_lp_decodef(Az[0], Az[1], lsp_new, ctx->lsp_history_16k);
199
200     memcpy(ctx->lsp_history_16k, lsp_new, LP_FILTER_ORDER_16k * sizeof(double));
201
202     memcpy(synth - LP_FILTER_ORDER_16k, ctx->synth,
203            LP_FILTER_ORDER_16k * sizeof(*synth));
204
205     for (i = 0; i < SUBFRAME_COUNT_16k; i++) {
206         int i_subfr = i * L_SUBFR_16k;
207         AMRFixed f;
208         float gain_corr_factor;
209         int pitch_delay_int;
210         int pitch_delay_frac;
211
212         if (!i) {
213             pitch_delay_3x = dec_delay3_1st(params->pitch_delay[i]);
214         } else
215             pitch_delay_3x = dec_delay3_2nd(params->pitch_delay[i],
216                                             PITCH_MIN, PITCH_MAX,
217                                             ctx->pitch_lag_prev);
218
219         pitch_fac = gain_pitch_cb_16k[params->gp_index[i]];
220         f.pitch_fac = FFMIN(pitch_fac, 1.0);
221         f.pitch_lag = DIVIDE_BY_3(pitch_delay_3x+1);
222         ctx->pitch_lag_prev = f.pitch_lag;
223
224         pitch_delay_int  = DIVIDE_BY_3(pitch_delay_3x + 2);
225         pitch_delay_frac = pitch_delay_3x + 2 - 3*pitch_delay_int;
226
227         ff_acelp_interpolatef(&excitation[i_subfr],
228                               &excitation[i_subfr] - pitch_delay_int + 1,
229                               sinc_win, 3, pitch_delay_frac + 1,
230                               LP_FILTER_ORDER, L_SUBFR_16k);
231
232
233         memset(fixed_vector, 0, sizeof(fixed_vector));
234
235         ff_decode_10_pulses_35bits(params->fc_indexes[i], &f,
236                                    ff_fc_4pulses_8bits_tracks_13, 5, 4);
237
238         ff_set_fixed_vector(fixed_vector, &f, 1.0, L_SUBFR_16k);
239
240         gain_corr_factor = gain_cb_16k[params->gc_index[i]];
241         gain_code = gain_corr_factor *
242             acelp_decode_gain_codef(sqrt(L_SUBFR_16k), fixed_vector,
243                                     19.0 - 15.0/(0.05*M_LN10/M_LN2),
244                                     pred_16k, ctx->energy_history,
245                                     L_SUBFR_16k, 2);
246
247         ctx->energy_history[1] = ctx->energy_history[0];
248         ctx->energy_history[0] = 20.0 * log10f(gain_corr_factor);
249
250         ff_weighted_vector_sumf(&excitation[i_subfr], &excitation[i_subfr],
251                                 fixed_vector, pitch_fac,
252                                 gain_code, L_SUBFR_16k);
253
254         ff_celp_lp_synthesis_filterf(synth + i_subfr, Az[i],
255                                      &excitation[i_subfr], L_SUBFR_16k,
256                                      LP_FILTER_ORDER_16k);
257
258     }
259     memcpy(ctx->synth, synth + frame_size - LP_FILTER_ORDER_16k,
260            LP_FILTER_ORDER_16k * sizeof(*synth));
261
262     memmove(ctx->excitation, ctx->excitation + 2 * L_SUBFR_16k,
263             (L_INTERPOL+PITCH_MAX) * sizeof(float));
264
265     postfilter(out_data, synth, ctx->iir_mem, ctx->filt_mem, ctx->mem_preemph);
266
267     memcpy(ctx->iir_mem, Az[1], LP_FILTER_ORDER_16k * sizeof(float));
268 }
269
270 void ff_sipr_init_16k(SiprContext *ctx)
271 {
272     int i;
273
274     for (i = 0; i < LP_FILTER_ORDER_16k; i++)
275         ctx->lsp_history_16k[i] = cos((i + 1) * M_PI/(LP_FILTER_ORDER_16k + 1));
276
277     ctx->filt_mem[0] = ctx->filt_buf[0];
278     ctx->filt_mem[1] = ctx->filt_buf[1];
279
280     ctx->pitch_lag_prev = 180;
281 }