2 * SIPR decoder for the 16k mode
4 * Copyright (c) 2008 Vladimir Voroshilov
5 * Copyright (c) 2009 Vitor Sessak
7 * This file is part of FFmpeg.
9 * FFmpeg 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.
14 * FFmpeg 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.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/mathematics.h"
29 #include "celp_math.h"
30 #include "acelp_vectors.h"
31 #include "acelp_pitch_delay.h"
32 #include "acelp_filters.h"
33 #include "celp_filters.h"
35 #include "sipr16kdata.h"
38 * Convert an lsf vector into an lsp vector.
40 * @param lsf input lsf vector
41 * @param lsp output lsp vector
43 static void lsf2lsp(const float *lsf, double *lsp)
47 for (i = 0; i < LP_FILTER_ORDER_16k; i++)
48 lsp[i] = cosf(lsf[i]);
51 static void dequant(float *out, const int *idx, const float *cbs[])
55 for (i = 0; i < 4; i++)
56 memcpy(out + 3*i, cbs[i] + 3*idx[i], 3*sizeof(float));
58 memcpy(out + 12, cbs[4] + 4*idx[4], 4*sizeof(float));
61 static void lsf_decode_fp_16k(float* lsf_history, float* isp_new,
62 const int* parm, int ma_pred)
65 float isp_q[LP_FILTER_ORDER_16k];
67 dequant(isp_q, parm, lsf_codebooks_16k);
69 for (i = 0; i < LP_FILTER_ORDER_16k; i++) {
70 isp_new[i] = (1 - qu[ma_pred]) * isp_q[i]
71 + qu[ma_pred] * lsf_history[i]
75 memcpy(lsf_history, isp_q, LP_FILTER_ORDER_16k * sizeof(float));
78 static int dec_delay3_1st(int index)
83 return 3 * index - 690;
86 static int dec_delay3_2nd(int index, int pit_min, int pit_max,
90 int pitch_delay_min = av_clip(pitch_lag_prev - 10,
91 pit_min, pit_max - 19);
92 return 3 * pitch_delay_min + index - 2;
94 return 3 * pitch_lag_prev;
97 static void postfilter(float* synth, float* iir_mem, float* filt_mem[2],
100 float buf[30 + LP_FILTER_ORDER_16k];
101 float *tmpbuf = buf + LP_FILTER_ORDER_16k;
105 for (i = 0; i < LP_FILTER_ORDER_16k; i++)
106 filt_mem[0][i] = iir_mem[i] * ff_pow_0_5[i];
108 memcpy(tmpbuf - LP_FILTER_ORDER_16k, mem_preemph,
109 LP_FILTER_ORDER_16k*sizeof(*buf));
111 ff_celp_lp_synthesis_filterf(tmpbuf, filt_mem[1], synth, 30,
112 LP_FILTER_ORDER_16k);
114 memcpy(synth - LP_FILTER_ORDER_16k, mem_preemph,
115 LP_FILTER_ORDER_16k * sizeof(*synth));
117 ff_celp_lp_synthesis_filterf(synth, filt_mem[0], synth, 2*L_SUBFR_16k,
118 LP_FILTER_ORDER_16k);
120 memcpy(mem_preemph, synth + 2*L_SUBFR_16k - LP_FILTER_ORDER_16k,
121 LP_FILTER_ORDER_16k * sizeof(*synth));
123 FFSWAP(float *, filt_mem[0], filt_mem[1]);
124 for (i = 0, s = 0; i < 30; i++, s += 1.0/30)
125 synth[i] = tmpbuf[i] + s * (synth[i] - tmpbuf[i]);
129 * Floating point version of ff_acelp_lp_decode().
131 static void acelp_lp_decodef(float *lp_1st, float *lp_2nd,
132 const double *lsp_2nd, const double *lsp_prev)
134 double lsp_1st[LP_FILTER_ORDER_16k];
137 /* LSP values for first subframe (3.2.5 of G.729, Equation 24) */
138 for (i = 0; i < LP_FILTER_ORDER_16k; i++)
139 lsp_1st[i] = (lsp_2nd[i] + lsp_prev[i]) * 0.5;
141 ff_acelp_lspd2lpc(lsp_1st, lp_1st, LP_FILTER_ORDER_16k >> 1);
143 /* LSP values for second subframe (3.2.5 of G.729) */
144 ff_acelp_lspd2lpc(lsp_2nd, lp_2nd, LP_FILTER_ORDER_16k >> 1);
148 * Floating point version of ff_acelp_decode_gain_code().
150 static float acelp_decode_gain_codef(float gain_corr_factor, const float *fc_v,
151 float mr_energy, const float *quant_energy,
152 const float *ma_prediction_coeff,
153 int subframe_size, int ma_pred_order)
156 ff_dot_productf(quant_energy, ma_prediction_coeff, ma_pred_order);
158 mr_energy = gain_corr_factor * exp(M_LN10 / 20. * mr_energy) /
159 sqrt((0.01 + ff_dot_productf(fc_v, fc_v, subframe_size)));
163 #define DIVIDE_BY_3(x) ((x) * 10923 >> 15)
165 void ff_sipr_decode_frame_16k(SiprContext *ctx, SiprParameters *params,
168 int frame_size = SUBFRAME_COUNT_16k * L_SUBFR_16k;
169 float *synth = ctx->synth_buf + LP_FILTER_ORDER_16k;
170 float lsf_new[LP_FILTER_ORDER_16k];
171 double lsp_new[LP_FILTER_ORDER_16k];
172 float Az[2][LP_FILTER_ORDER_16k];
173 float fixed_vector[L_SUBFR_16k];
174 float pitch_fac, gain_code;
179 float *excitation = ctx->excitation + 292;
181 lsf_decode_fp_16k(ctx->lsf_history, lsf_new, params->vq_indexes,
182 params->ma_pred_switch);
184 ff_set_min_dist_lsf(lsf_new, LSFQ_DIFF_MIN / 2, LP_FILTER_ORDER_16k);
186 lsf2lsp(lsf_new, lsp_new);
188 acelp_lp_decodef(Az[0], Az[1], lsp_new, ctx->lsp_history_16k);
190 memcpy(ctx->lsp_history_16k, lsp_new, LP_FILTER_ORDER_16k * sizeof(double));
192 memcpy(synth - LP_FILTER_ORDER_16k, ctx->synth,
193 LP_FILTER_ORDER_16k * sizeof(*synth));
195 for (i = 0; i < SUBFRAME_COUNT_16k; i++) {
196 int i_subfr = i * L_SUBFR_16k;
198 float gain_corr_factor;
200 int pitch_delay_frac;
203 pitch_delay_3x = dec_delay3_1st(params->pitch_delay[i]);
205 pitch_delay_3x = dec_delay3_2nd(params->pitch_delay[i],
206 PITCH_MIN, PITCH_MAX,
207 ctx->pitch_lag_prev);
209 pitch_fac = gain_pitch_cb_16k[params->gp_index[i]];
210 f.pitch_fac = FFMIN(pitch_fac, 1.0);
211 f.pitch_lag = DIVIDE_BY_3(pitch_delay_3x+1);
212 ctx->pitch_lag_prev = f.pitch_lag;
214 pitch_delay_int = DIVIDE_BY_3(pitch_delay_3x + 2);
215 pitch_delay_frac = pitch_delay_3x + 2 - 3*pitch_delay_int;
217 ff_acelp_interpolatef(&excitation[i_subfr],
218 &excitation[i_subfr] - pitch_delay_int + 1,
219 sinc_win, 3, pitch_delay_frac + 1,
220 LP_FILTER_ORDER, L_SUBFR_16k);
223 memset(fixed_vector, 0, sizeof(fixed_vector));
225 ff_decode_10_pulses_35bits(params->fc_indexes[i], &f,
226 ff_fc_4pulses_8bits_tracks_13, 5, 4);
228 ff_set_fixed_vector(fixed_vector, &f, 1.0, L_SUBFR_16k);
230 gain_corr_factor = gain_cb_16k[params->gc_index[i]];
231 gain_code = gain_corr_factor *
232 acelp_decode_gain_codef(sqrt(L_SUBFR_16k), fixed_vector,
233 19.0 - 15.0/(0.05*M_LN10/M_LN2),
234 pred_16k, ctx->energy_history,
237 ctx->energy_history[1] = ctx->energy_history[0];
238 ctx->energy_history[0] = 20.0 * log10f(gain_corr_factor);
240 ff_weighted_vector_sumf(&excitation[i_subfr], &excitation[i_subfr],
241 fixed_vector, pitch_fac,
242 gain_code, L_SUBFR_16k);
244 ff_celp_lp_synthesis_filterf(synth + i_subfr, Az[i],
245 &excitation[i_subfr], L_SUBFR_16k,
246 LP_FILTER_ORDER_16k);
249 memcpy(ctx->synth, synth + frame_size - LP_FILTER_ORDER_16k,
250 LP_FILTER_ORDER_16k * sizeof(*synth));
252 memmove(ctx->excitation, ctx->excitation + 2 * L_SUBFR_16k,
253 (L_INTERPOL+PITCH_MAX) * sizeof(float));
255 postfilter(synth, ctx->iir_mem, ctx->filt_mem, ctx->mem_preemph);
257 memcpy(ctx->iir_mem, Az[1], LP_FILTER_ORDER_16k * sizeof(float));
259 ctx->dsp.vector_clipf(out_data, synth, -1, 32767./(1<<15), frame_size);
263 void ff_sipr_init_16k(SiprContext *ctx)
267 for (i = 0; i < LP_FILTER_ORDER_16k; i++)
268 ctx->lsp_history_16k[i] = cos((i + 1) * M_PI/(LP_FILTER_ORDER_16k + 1));
270 ctx->filt_mem[0] = ctx->filt_buf[0];
271 ctx->filt_mem[1] = ctx->filt_buf[1];
273 ctx->pitch_lag_prev = 180;