/src/ffmpeg/libavcodec/sipr16k.c
Line | Count | Source |
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 FFmpeg. |
8 | | * |
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. |
13 | | * |
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. |
18 | | * |
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 |
22 | | */ |
23 | | |
24 | | #include <math.h> |
25 | | |
26 | | #include "sipr.h" |
27 | | #include "libavutil/attributes.h" |
28 | | #include "libavutil/common.h" |
29 | | #include "libavutil/float_dsp.h" |
30 | | #include "libavutil/mathematics.h" |
31 | | #include "lsp.h" |
32 | | #include "acelp_vectors.h" |
33 | | #include "acelp_pitch_delay.h" |
34 | | #include "acelp_filters.h" |
35 | | #include "celp_filters.h" |
36 | | |
37 | | #include "sipr16kdata.h" |
38 | | |
39 | | /** |
40 | | * Convert an lsf vector into an lsp vector. |
41 | | * |
42 | | * @param lsf input lsf vector |
43 | | * @param lsp output lsp vector |
44 | | */ |
45 | | static void lsf2lsp(const float *lsf, double *lsp) |
46 | 754k | { |
47 | 754k | int i; |
48 | | |
49 | 12.8M | for (i = 0; i < LP_FILTER_ORDER_16k; i++) |
50 | 12.0M | lsp[i] = cosf(lsf[i]); |
51 | 754k | } |
52 | | |
53 | | static void dequant(float *out, const int *idx, const float * const cbs[]) |
54 | 754k | { |
55 | 754k | int i; |
56 | | |
57 | 3.77M | for (i = 0; i < 4; i++) |
58 | 3.01M | memcpy(out + 3*i, cbs[i] + 3*idx[i], 3*sizeof(float)); |
59 | | |
60 | 754k | memcpy(out + 12, cbs[4] + 4*idx[4], 4*sizeof(float)); |
61 | 754k | } |
62 | | |
63 | | static void lsf_decode_fp_16k(float* lsf_history, float* isp_new, |
64 | | const int* parm, int ma_pred) |
65 | 754k | { |
66 | 754k | int i; |
67 | 754k | float isp_q[LP_FILTER_ORDER_16k]; |
68 | | |
69 | 754k | dequant(isp_q, parm, lsf_codebooks_16k); |
70 | | |
71 | 12.8M | for (i = 0; i < LP_FILTER_ORDER_16k; i++) { |
72 | 12.0M | isp_new[i] = (1 - qu[ma_pred]) * isp_q[i] |
73 | 12.0M | + qu[ma_pred] * lsf_history[i] |
74 | 12.0M | + mean_lsf_16k[i]; |
75 | 12.0M | } |
76 | | |
77 | 754k | memcpy(lsf_history, isp_q, LP_FILTER_ORDER_16k * sizeof(float)); |
78 | 754k | } |
79 | | |
80 | | static int dec_delay3_1st(int index) |
81 | 754k | { |
82 | 754k | if (index < 390) { |
83 | 669k | return index + 88; |
84 | 669k | } else |
85 | 85.4k | return 3 * index - 690; |
86 | 754k | } |
87 | | |
88 | | static int dec_delay3_2nd(int index, int pit_min, int pit_max, |
89 | | int pitch_lag_prev) |
90 | 754k | { |
91 | 754k | if (index < 62) { |
92 | 727k | int pitch_delay_min = av_clip(pitch_lag_prev - 10, |
93 | 727k | pit_min, pit_max - 19); |
94 | 727k | return 3 * pitch_delay_min + index - 2; |
95 | 727k | } else |
96 | 27.1k | return 3 * pitch_lag_prev; |
97 | 754k | } |
98 | | |
99 | | static void postfilter(float *out_data, float* synth, float* iir_mem, |
100 | | float* filt_mem[2], float* mem_preemph) |
101 | 754k | { |
102 | 754k | float buf[30 + LP_FILTER_ORDER_16k]; |
103 | 754k | float *tmpbuf = buf + LP_FILTER_ORDER_16k; |
104 | 754k | float s; |
105 | 754k | int i; |
106 | | |
107 | 12.8M | for (i = 0; i < LP_FILTER_ORDER_16k; i++) |
108 | 12.0M | filt_mem[0][i] = iir_mem[i] * ff_pow_0_5[i]; |
109 | | |
110 | 754k | memcpy(tmpbuf - LP_FILTER_ORDER_16k, mem_preemph, |
111 | 754k | LP_FILTER_ORDER_16k*sizeof(*buf)); |
112 | | |
113 | 754k | ff_celp_lp_synthesis_filterf(tmpbuf, filt_mem[1], synth, 30, |
114 | 754k | LP_FILTER_ORDER_16k); |
115 | | |
116 | 754k | memcpy(synth - LP_FILTER_ORDER_16k, mem_preemph, |
117 | 754k | LP_FILTER_ORDER_16k * sizeof(*synth)); |
118 | | |
119 | 754k | ff_celp_lp_synthesis_filterf(synth, filt_mem[0], synth, 30, |
120 | 754k | LP_FILTER_ORDER_16k); |
121 | | |
122 | 754k | memcpy(out_data + 30 - LP_FILTER_ORDER_16k, |
123 | 754k | synth + 30 - LP_FILTER_ORDER_16k, |
124 | 754k | LP_FILTER_ORDER_16k * sizeof(*synth)); |
125 | | |
126 | 754k | ff_celp_lp_synthesis_filterf(out_data + 30, filt_mem[0], |
127 | 754k | synth + 30, 2 * L_SUBFR_16k - 30, |
128 | 754k | LP_FILTER_ORDER_16k); |
129 | | |
130 | | |
131 | 754k | memcpy(mem_preemph, out_data + 2*L_SUBFR_16k - LP_FILTER_ORDER_16k, |
132 | 754k | LP_FILTER_ORDER_16k * sizeof(*synth)); |
133 | | |
134 | 754k | FFSWAP(float *, filt_mem[0], filt_mem[1]); |
135 | 23.3M | for (i = 0, s = 0; i < 30; i++, s += 1.0/30) |
136 | 22.6M | out_data[i] = tmpbuf[i] + s * (synth[i] - tmpbuf[i]); |
137 | 754k | } |
138 | | |
139 | | /** |
140 | | * Floating point version of ff_acelp_lp_decode(). |
141 | | */ |
142 | | static void acelp_lp_decodef(float *lp_1st, float *lp_2nd, |
143 | | const double *lsp_2nd, const double *lsp_prev) |
144 | 754k | { |
145 | 754k | double lsp_1st[LP_FILTER_ORDER_16k]; |
146 | 754k | int i; |
147 | | |
148 | | /* LSP values for first subframe (3.2.5 of G.729, Equation 24) */ |
149 | 12.8M | for (i = 0; i < LP_FILTER_ORDER_16k; i++) |
150 | 12.0M | lsp_1st[i] = (lsp_2nd[i] + lsp_prev[i]) * 0.5; |
151 | | |
152 | 754k | ff_acelp_lspd2lpc(lsp_1st, lp_1st, LP_FILTER_ORDER_16k >> 1); |
153 | | |
154 | | /* LSP values for second subframe (3.2.5 of G.729) */ |
155 | 754k | ff_acelp_lspd2lpc(lsp_2nd, lp_2nd, LP_FILTER_ORDER_16k >> 1); |
156 | 754k | } |
157 | | |
158 | | /** |
159 | | * Floating point version of ff_acelp_decode_gain_code(). |
160 | | */ |
161 | | static float acelp_decode_gain_codef(float gain_corr_factor, const float *fc_v, |
162 | | float mr_energy, const float *quant_energy, |
163 | | const float *ma_prediction_coeff, |
164 | | int subframe_size, int ma_pred_order) |
165 | 1.50M | { |
166 | 1.50M | mr_energy += ff_scalarproduct_float_c(quant_energy, ma_prediction_coeff, |
167 | 1.50M | ma_pred_order); |
168 | | |
169 | 1.50M | mr_energy = gain_corr_factor * exp(M_LN10 / 20. * mr_energy) / |
170 | 1.50M | sqrt((0.01 + ff_scalarproduct_float_c(fc_v, fc_v, subframe_size))); |
171 | 1.50M | return mr_energy; |
172 | 1.50M | } |
173 | | |
174 | 3.01M | #define DIVIDE_BY_3(x) ((x) * 10923 >> 15) |
175 | | |
176 | | void ff_sipr_decode_frame_16k(SiprContext *ctx, SiprParameters *params, |
177 | | float *out_data) |
178 | 754k | { |
179 | 754k | int frame_size = SUBFRAME_COUNT_16k * L_SUBFR_16k; |
180 | 754k | float *synth = ctx->synth_buf + LP_FILTER_ORDER_16k; |
181 | 754k | float lsf_new[LP_FILTER_ORDER_16k]; |
182 | 754k | double lsp_new[LP_FILTER_ORDER_16k]; |
183 | 754k | float Az[2][LP_FILTER_ORDER_16k]; |
184 | 754k | float fixed_vector[L_SUBFR_16k]; |
185 | 754k | float pitch_fac, gain_code; |
186 | | |
187 | 754k | int i; |
188 | 754k | int pitch_delay_3x; |
189 | | |
190 | 754k | float *excitation = ctx->excitation + 292; |
191 | | |
192 | 754k | lsf_decode_fp_16k(ctx->lsf_history, lsf_new, params->vq_indexes, |
193 | 754k | params->ma_pred_switch); |
194 | | |
195 | 754k | ff_set_min_dist_lsf(lsf_new, LSFQ_DIFF_MIN / 2, LP_FILTER_ORDER_16k); |
196 | | |
197 | 754k | lsf2lsp(lsf_new, lsp_new); |
198 | | |
199 | 754k | acelp_lp_decodef(Az[0], Az[1], lsp_new, ctx->lsp_history_16k); |
200 | | |
201 | 754k | memcpy(ctx->lsp_history_16k, lsp_new, LP_FILTER_ORDER_16k * sizeof(double)); |
202 | | |
203 | 754k | memcpy(synth - LP_FILTER_ORDER_16k, ctx->synth, |
204 | 754k | LP_FILTER_ORDER_16k * sizeof(*synth)); |
205 | | |
206 | 2.26M | for (i = 0; i < SUBFRAME_COUNT_16k; i++) { |
207 | 1.50M | int i_subfr = i * L_SUBFR_16k; |
208 | 1.50M | AMRFixed f; |
209 | 1.50M | float gain_corr_factor; |
210 | 1.50M | int pitch_delay_int; |
211 | 1.50M | int pitch_delay_frac; |
212 | | |
213 | 1.50M | if (!i) { |
214 | 754k | pitch_delay_3x = dec_delay3_1st(params->pitch_delay[i]); |
215 | 754k | } else |
216 | 754k | pitch_delay_3x = dec_delay3_2nd(params->pitch_delay[i], |
217 | 754k | PITCH_MIN, PITCH_MAX, |
218 | 754k | ctx->pitch_lag_prev); |
219 | | |
220 | 1.50M | pitch_fac = gain_pitch_cb_16k[params->gp_index[i]]; |
221 | 1.50M | f.pitch_fac = FFMIN(pitch_fac, 1.0); |
222 | 1.50M | f.pitch_lag = DIVIDE_BY_3(pitch_delay_3x+1); |
223 | 1.50M | ctx->pitch_lag_prev = f.pitch_lag; |
224 | | |
225 | 1.50M | pitch_delay_int = DIVIDE_BY_3(pitch_delay_3x + 2); |
226 | 1.50M | pitch_delay_frac = pitch_delay_3x + 2 - 3*pitch_delay_int; |
227 | | |
228 | 1.50M | ff_acelp_interpolatef(&excitation[i_subfr], |
229 | 1.50M | &excitation[i_subfr] - pitch_delay_int + 1, |
230 | 1.50M | sinc_win, 3, pitch_delay_frac + 1, |
231 | 1.50M | LP_FILTER_ORDER, L_SUBFR_16k); |
232 | | |
233 | | |
234 | 1.50M | memset(fixed_vector, 0, sizeof(fixed_vector)); |
235 | | |
236 | 1.50M | ff_decode_10_pulses_35bits(params->fc_indexes[i], &f, |
237 | 1.50M | ff_fc_4pulses_8bits_tracks_13, 5, 4); |
238 | | |
239 | 1.50M | ff_set_fixed_vector(fixed_vector, &f, 1.0, L_SUBFR_16k); |
240 | | |
241 | 1.50M | gain_corr_factor = gain_cb_16k[params->gc_index[i]]; |
242 | 1.50M | gain_code = gain_corr_factor * |
243 | 1.50M | acelp_decode_gain_codef(sqrt(L_SUBFR_16k), fixed_vector, |
244 | 1.50M | 19.0 - 15.0/(0.05*M_LN10/M_LN2), |
245 | 1.50M | pred_16k, ctx->energy_history, |
246 | 1.50M | L_SUBFR_16k, 2); |
247 | | |
248 | 1.50M | ctx->energy_history[1] = ctx->energy_history[0]; |
249 | 1.50M | ctx->energy_history[0] = 20.0 * log10f(gain_corr_factor); |
250 | | |
251 | 1.50M | ff_weighted_vector_sumf(&excitation[i_subfr], &excitation[i_subfr], |
252 | 1.50M | fixed_vector, pitch_fac, |
253 | 1.50M | gain_code, L_SUBFR_16k); |
254 | | |
255 | 1.50M | ff_celp_lp_synthesis_filterf(synth + i_subfr, Az[i], |
256 | 1.50M | &excitation[i_subfr], L_SUBFR_16k, |
257 | 1.50M | LP_FILTER_ORDER_16k); |
258 | | |
259 | 1.50M | } |
260 | 754k | memcpy(ctx->synth, synth + frame_size - LP_FILTER_ORDER_16k, |
261 | 754k | LP_FILTER_ORDER_16k * sizeof(*synth)); |
262 | | |
263 | 754k | memmove(ctx->excitation, ctx->excitation + 2 * L_SUBFR_16k, |
264 | 754k | (L_INTERPOL+PITCH_MAX) * sizeof(float)); |
265 | | |
266 | 754k | postfilter(out_data, synth, ctx->iir_mem, ctx->filt_mem, ctx->mem_preemph); |
267 | | |
268 | 754k | memcpy(ctx->iir_mem, Az[1], LP_FILTER_ORDER_16k * sizeof(float)); |
269 | 754k | } |
270 | | |
271 | | av_cold void ff_sipr_init_16k(SiprContext *ctx) |
272 | 522 | { |
273 | 522 | int i; |
274 | | |
275 | 8.87k | for (i = 0; i < LP_FILTER_ORDER_16k; i++) |
276 | 8.35k | ctx->lsp_history_16k[i] = cos((i + 1) * M_PI/(LP_FILTER_ORDER_16k + 1)); |
277 | | |
278 | 522 | ctx->filt_mem[0] = ctx->filt_buf[0]; |
279 | 522 | ctx->filt_mem[1] = ctx->filt_buf[1]; |
280 | | |
281 | 522 | ctx->pitch_lag_prev = 180; |
282 | 522 | } |