2 * various filters for ACELP-based codecs
4 * Copyright (c) 2008 Vladimir Voroshilov
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "celp_filters.h"
27 #include "libavutil/common.h"
29 void ff_celp_convolve_circ(int16_t* fc_out, const int16_t* fc_in,
30 const int16_t* filter, int len)
34 memset(fc_out, 0, len * sizeof(int16_t));
36 /* Since there are few pulses over an entire subframe (i.e. almost
37 all fc_in[i] are zero) it is faster to loop over fc_in first. */
38 for (i = 0; i < len; i++) {
40 for (k = 0; k < i; k++)
41 fc_out[k] += (fc_in[i] * filter[len + k - i]) >> 15;
43 for (k = i; k < len; k++)
44 fc_out[k] += (fc_in[i] * filter[ k - i]) >> 15;
49 void ff_celp_circ_addf(float *out, const float *in,
50 const float *lagged, int lag, float fac, int n)
53 for (k = 0; k < lag; k++)
54 out[k] = in[k] + fac * lagged[n + k - lag];
56 out[k] = in[k] + fac * lagged[ k - lag];
59 int ff_celp_lp_synthesis_filter(int16_t *out, const int16_t *filter_coeffs,
60 const int16_t *in, int buffer_length,
61 int filter_length, int stop_on_overflow,
62 int shift, int rounder)
66 for (n = 0; n < buffer_length; n++) {
67 int sum = -rounder, sum1;
68 for (i = 1; i <= filter_length; i++)
69 sum += filter_coeffs[i-1] * out[n-i];
71 sum1 = ((-sum >> 12) + in[n]) >> shift;
72 sum = av_clip_int16(sum1);
74 if (stop_on_overflow && sum != sum1)
83 void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs,
84 const float* in, int buffer_length,
89 #if 0 // Unoptimized code path for improved readability
90 for (n = 0; n < buffer_length; n++) {
92 for (i = 1; i <= filter_length; i++)
93 out[n] -= filter_coeffs[i-1] * out[n-i];
96 float out0, out1, out2, out3;
97 float old_out0, old_out1, old_out2, old_out3;
100 a = filter_coeffs[0];
101 b = filter_coeffs[1];
102 c = filter_coeffs[2];
103 b -= filter_coeffs[0] * filter_coeffs[0];
104 c -= filter_coeffs[1] * filter_coeffs[0];
105 c -= filter_coeffs[0] * b;
111 for (n = 0; n <= buffer_length - 4; n+=4) {
112 float tmp0,tmp1,tmp2;
120 out0 -= filter_coeffs[2] * old_out1;
121 out1 -= filter_coeffs[2] * old_out2;
122 out2 -= filter_coeffs[2] * old_out3;
124 out0 -= filter_coeffs[1] * old_out2;
125 out1 -= filter_coeffs[1] * old_out3;
127 out0 -= filter_coeffs[0] * old_out3;
129 val = filter_coeffs[3];
131 out0 -= val * old_out0;
132 out1 -= val * old_out1;
133 out2 -= val * old_out2;
134 out3 -= val * old_out3;
136 for (i = 5; i <= filter_length; i += 2) {
138 val = filter_coeffs[i-1];
140 out0 -= val * old_out3;
141 out1 -= val * old_out0;
142 out2 -= val * old_out1;
143 out3 -= val * old_out2;
145 old_out2 = out[-i-1];
147 val = filter_coeffs[i];
149 out0 -= val * old_out2;
150 out1 -= val * old_out3;
151 out2 -= val * old_out0;
152 out3 -= val * old_out1;
154 FFSWAP(float, old_out0, old_out2);
188 for (; n < buffer_length; n++) {
190 for (i = 1; i <= filter_length; i++)
191 out[n] -= filter_coeffs[i-1] * out[n-i];
196 void ff_celp_lp_zero_synthesis_filterf(float *out, const float *filter_coeffs,
197 const float *in, int buffer_length,
202 for (n = 0; n < buffer_length; n++) {
204 for (i = 1; i <= filter_length; i++)
205 out[n] += filter_coeffs[i-1] * in[n-i];
209 void ff_celp_filter_init(CELPFContext *c)
211 c->celp_lp_synthesis_filterf = ff_celp_lp_synthesis_filterf;
212 c->celp_lp_zero_synthesis_filterf = ff_celp_lp_zero_synthesis_filterf;
215 ff_celp_filter_init_mips(c);