]> git.sesse.net Git - ffmpeg/blob - libavcodec/celp_filters.c
Export H264 profile and level in AVCodecContext.
[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, 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 rounder)
62 {
63     int i,n;
64
65     for (n = 0; n < buffer_length; n++) {
66         int sum = rounder;
67         for (i = 1; i <= filter_length; i++)
68             sum -= filter_coeffs[i-1] * out[n-i];
69
70         sum = (sum >> 12) + in[n];
71
72         if (sum + 0x8000 > 0xFFFFU) {
73             if (stop_on_overflow)
74                 return 1;
75             sum = (sum >> 31) ^ 32767;
76         }
77         out[n] = sum;
78     }
79
80     return 0;
81 }
82
83 void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs,
84                                   const float* in, int buffer_length,
85                                   int filter_length)
86 {
87     int i,n;
88
89     float out0, out1, out2, out3;
90     float old_out0, old_out1, old_out2, old_out3;
91     float a,b,c;
92
93     a = filter_coeffs[0];
94     b = filter_coeffs[1];
95     c = filter_coeffs[2];
96     b -= filter_coeffs[0] * filter_coeffs[0];
97     c -= filter_coeffs[1] * filter_coeffs[0];
98     c -= filter_coeffs[0] * b;
99
100     old_out0 = out[-4];
101     old_out1 = out[-3];
102     old_out2 = out[-2];
103     old_out3 = out[-1];
104     for (n = 0; n <= buffer_length - 4; n+=4) {
105         float tmp0,tmp1,tmp2,tmp3;
106         float val;
107
108         out0 = in[0];
109         out1 = in[1];
110         out2 = in[2];
111         out3 = in[3];
112
113         out0 -= filter_coeffs[2] * old_out1;
114         out1 -= filter_coeffs[2] * old_out2;
115         out2 -= filter_coeffs[2] * old_out3;
116
117         out0 -= filter_coeffs[1] * old_out2;
118         out1 -= filter_coeffs[1] * old_out3;
119
120         out0 -= filter_coeffs[0] * old_out3;
121
122         val = filter_coeffs[3];
123
124         out0 -= val * old_out0;
125         out1 -= val * old_out1;
126         out2 -= val * old_out2;
127         out3 -= val * old_out3;
128
129         old_out3 = out[-5];
130
131         for (i = 5; i <= filter_length; i += 2) {
132             val = filter_coeffs[i-1];
133
134             out0 -= val * old_out3;
135             out1 -= val * old_out0;
136             out2 -= val * old_out1;
137             out3 -= val * old_out2;
138
139             old_out2 = out[-i-1];
140
141             val = filter_coeffs[i];
142
143             out0 -= val * old_out2;
144             out1 -= val * old_out3;
145             out2 -= val * old_out0;
146             out3 -= val * old_out1;
147
148             FFSWAP(float, old_out0, old_out2);
149             old_out1 = old_out3;
150             old_out3 = out[-i-2];
151         }
152
153         tmp0 = out0;
154         tmp1 = out1;
155         tmp2 = out2;
156         tmp3 = out3;
157
158         out3 -= a * tmp2;
159         out2 -= a * tmp1;
160         out1 -= a * tmp0;
161
162         out3 -= b * tmp1;
163         out2 -= b * tmp0;
164
165         out3 -= c * tmp0;
166
167
168         out[0] = out0;
169         out[1] = out1;
170         out[2] = out2;
171         out[3] = out3;
172
173         old_out0 = out0;
174         old_out1 = out1;
175         old_out2 = out2;
176         old_out3 = out3;
177
178         out += 4;
179         in  += 4;
180     }
181
182     out -= n;
183     in -= n;
184     for (; n < buffer_length; n++) {
185         out[n] = in[n];
186         for (i = 1; i <= filter_length; i++)
187             out[n] -= filter_coeffs[i-1] * out[n-i];
188     }
189 }
190
191 void ff_celp_lp_zero_synthesis_filterf(float *out, const float *filter_coeffs,
192                                        const float *in, int buffer_length,
193                                        int filter_length)
194 {
195     int i,n;
196
197     for (n = 0; n < buffer_length; n++) {
198         out[n] = in[n];
199         for (i = 1; i <= filter_length; i++)
200             out[n] += filter_coeffs[i-1] * in[n-i];
201     }
202 }