]> git.sesse.net Git - ffmpeg/blob - libavcodec/celp_filters.c
celp: optimise ff_celp_lp_synthesis_filter()
[ffmpeg] / libavcodec / celp_filters.c
1 /*
2  * various filters for ACELP-based codecs
3  *
4  * Copyright (c) 2008 Vladimir Voroshilov
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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.
12  *
13  * Libav 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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <inttypes.h>
24
25 #include "avcodec.h"
26 #include "celp_filters.h"
27
28 void ff_celp_convolve_circ(int16_t* fc_out, const int16_t* fc_in,
29                            const int16_t* filter, int len)
30 {
31     int i, k;
32
33     memset(fc_out, 0, len * sizeof(int16_t));
34
35     /* Since there are few pulses over an entire subframe (i.e. almost
36        all fc_in[i] are zero) it is faster to loop over fc_in first. */
37     for (i = 0; i < len; i++) {
38         if (fc_in[i]) {
39             for (k = 0; k < i; k++)
40                 fc_out[k] += (fc_in[i] * filter[len + k - i]) >> 15;
41
42             for (k = i; k < len; k++)
43                 fc_out[k] += (fc_in[i] * filter[      k - i]) >> 15;
44         }
45     }
46 }
47
48 void ff_celp_circ_addf(float *out, const float *in,
49                        const float *lagged, int lag, float fac, int n)
50 {
51     int k;
52     for (k = 0; k < lag; k++)
53         out[k] = in[k] + fac * lagged[n + k - lag];
54     for (; k < n; k++)
55         out[k] = in[k] + fac * lagged[    k - lag];
56 }
57
58 int ff_celp_lp_synthesis_filter(int16_t *out, const int16_t *filter_coeffs,
59                                 const int16_t *in, int buffer_length,
60                                 int filter_length, int stop_on_overflow,
61                                 int shift, int rounder)
62 {
63     int i,n;
64
65     for (n = 0; n < buffer_length; n++) {
66         int sum = -rounder, sum1;
67         for (i = 1; i <= filter_length; i++)
68             sum += filter_coeffs[i-1] * out[n-i];
69
70         sum1 = ((-sum >> 12) + in[n]) >> shift;
71         sum  = av_clip_int16(sum1);
72
73         if (stop_on_overflow && sum != sum1)
74             return 1;
75
76         out[n] = sum;
77     }
78
79     return 0;
80 }
81
82 void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs,
83                                   const float* in, int buffer_length,
84                                   int filter_length)
85 {
86     int i,n;
87
88 #if 0 // Unoptimized code path for improved readability
89     for (n = 0; n < buffer_length; n++) {
90         out[n] = in[n];
91         for (i = 1; i <= filter_length; i++)
92             out[n] -= filter_coeffs[i-1] * out[n-i];
93     }
94 #else
95     float out0, out1, out2, out3;
96     float old_out0, old_out1, old_out2, old_out3;
97     float a,b,c;
98
99     a = filter_coeffs[0];
100     b = filter_coeffs[1];
101     c = filter_coeffs[2];
102     b -= filter_coeffs[0] * filter_coeffs[0];
103     c -= filter_coeffs[1] * filter_coeffs[0];
104     c -= filter_coeffs[0] * b;
105
106     old_out0 = out[-4];
107     old_out1 = out[-3];
108     old_out2 = out[-2];
109     old_out3 = out[-1];
110     for (n = 0; n <= buffer_length - 4; n+=4) {
111         float tmp0,tmp1,tmp2;
112         float val;
113
114         out0 = in[0];
115         out1 = in[1];
116         out2 = in[2];
117         out3 = in[3];
118
119         out0 -= filter_coeffs[2] * old_out1;
120         out1 -= filter_coeffs[2] * old_out2;
121         out2 -= filter_coeffs[2] * old_out3;
122
123         out0 -= filter_coeffs[1] * old_out2;
124         out1 -= filter_coeffs[1] * old_out3;
125
126         out0 -= filter_coeffs[0] * old_out3;
127
128         val = filter_coeffs[3];
129
130         out0 -= val * old_out0;
131         out1 -= val * old_out1;
132         out2 -= val * old_out2;
133         out3 -= val * old_out3;
134
135         for (i = 5; i <= filter_length; i += 2) {
136             old_out3 = out[-i];
137             val = filter_coeffs[i-1];
138
139             out0 -= val * old_out3;
140             out1 -= val * old_out0;
141             out2 -= val * old_out1;
142             out3 -= val * old_out2;
143
144             old_out2 = out[-i-1];
145
146             val = filter_coeffs[i];
147
148             out0 -= val * old_out2;
149             out1 -= val * old_out3;
150             out2 -= val * old_out0;
151             out3 -= val * old_out1;
152
153             FFSWAP(float, old_out0, old_out2);
154             old_out1 = old_out3;
155         }
156
157         tmp0 = out0;
158         tmp1 = out1;
159         tmp2 = out2;
160
161         out3 -= a * tmp2;
162         out2 -= a * tmp1;
163         out1 -= a * tmp0;
164
165         out3 -= b * tmp1;
166         out2 -= b * tmp0;
167
168         out3 -= c * tmp0;
169
170
171         out[0] = out0;
172         out[1] = out1;
173         out[2] = out2;
174         out[3] = out3;
175
176         old_out0 = out0;
177         old_out1 = out1;
178         old_out2 = out2;
179         old_out3 = out3;
180
181         out += 4;
182         in  += 4;
183     }
184
185     out -= n;
186     in -= n;
187     for (; n < buffer_length; n++) {
188         out[n] = in[n];
189         for (i = 1; i <= filter_length; i++)
190             out[n] -= filter_coeffs[i-1] * out[n-i];
191     }
192 #endif
193 }
194
195 void ff_celp_lp_zero_synthesis_filterf(float *out, const float *filter_coeffs,
196                                        const float *in, int buffer_length,
197                                        int filter_length)
198 {
199     int i,n;
200
201     for (n = 0; n < buffer_length; n++) {
202         out[n] = in[n];
203         for (i = 1; i <= filter_length; i++)
204             out[n] += filter_coeffs[i-1] * in[n-i];
205     }
206 }