]> git.sesse.net Git - ffmpeg/blob - libavcodec/celp_filters.c
d54e2fa7099aef50dd167078b701d8e9fffeeb99
[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 FFmpeg.
7  *
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.
12  *
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.
17  *
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
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,
29                            const int16_t* fc_in,
30                            const int16_t* filter,
31                            int len)
32 {
33     int i, k;
34
35     memset(fc_out, 0, len * sizeof(int16_t));
36
37     /* Since there are few pulses over an entire subframe (i.e. almost
38        all fc_in[i] are zero) it is faster to loop over fc_in first. */
39     for (i = 0; i < len; i++) {
40         if (fc_in[i]) {
41             for (k = 0; k < i; k++)
42                 fc_out[k] += (fc_in[i] * filter[len + k - i]) >> 15;
43
44             for (k = i; k < len; k++)
45                 fc_out[k] += (fc_in[i] * filter[      k - i]) >> 15;
46         }
47     }
48 }
49
50 void ff_celp_circ_addf(float *out, const float *in,
51                        const float *lagged, int lag, float fac, int n)
52 {
53     int k;
54     for (k = 0; k < lag; k++)
55         out[k] = in[k] + fac * lagged[n + k - lag];
56     for (; k < n; k++)
57         out[k] = in[k] + fac * lagged[    k - lag];
58 }
59
60 int ff_celp_lp_synthesis_filter(int16_t *out,
61                                 const int16_t* filter_coeffs,
62                                 const int16_t* in,
63                                 int buffer_length,
64                                 int filter_length,
65                                 int stop_on_overflow,
66                                 int rounder)
67 {
68     int i,n;
69
70     // Avoids a +1 in the inner loop.
71     filter_length++;
72
73     for (n = 0; n < buffer_length; n++) {
74         int sum = rounder;
75         for (i = 1; i < filter_length; i++)
76             sum -= filter_coeffs[i-1] * out[n-i];
77
78         sum = (sum >> 12) + in[n];
79
80         if (sum + 0x8000 > 0xFFFFU) {
81             if (stop_on_overflow)
82                 return 1;
83             sum = (sum >> 31) ^ 32767;
84         }
85         out[n] = sum;
86     }
87
88     return 0;
89 }
90
91 void ff_celp_lp_synthesis_filterf(float *out,
92                                   const float* filter_coeffs,
93                                   const float* in,
94                                   int buffer_length,
95                                   int filter_length)
96 {
97     int i,n;
98
99     // Avoids a +1 in the inner loop.
100     filter_length++;
101
102     for (n = 0; n < buffer_length; n++) {
103         out[n] = in[n];
104         for (i = 1; i < filter_length; i++)
105             out[n] -= filter_coeffs[i-1] * out[n-i];
106     }
107 }
108
109 void ff_celp_lp_zero_synthesis_filterf(float *out,
110                                        const float* filter_coeffs,
111                                        const float* in,
112                                        int buffer_length,
113                                        int filter_length)
114 {
115     int i,n;
116
117     // Avoids a +1 in the inner loop.
118     filter_length++;
119
120     for (n = 0; n < buffer_length; n++) {
121         out[n] = in[n];
122         for (i = 1; i < filter_length; i++)
123             out[n] += filter_coeffs[i-1] * in[n-i];
124     }
125 }