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
27 #include "celp_filters.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/common.h"
31 void ff_celp_convolve_circ(int16_t* fc_out
, const int16_t* fc_in
,
32 const int16_t* filter
, int len
)
36 memset(fc_out
, 0, len
* sizeof(int16_t));
38 /* Since there are few pulses over an entire subframe (i.e. almost
39 all fc_in[i] are zero) it is faster to loop over fc_in first. */
40 for (i
= 0; i
< len
; i
++) {
42 for (k
= 0; k
< i
; k
++)
43 fc_out
[k
] += (fc_in
[i
] * filter
[len
+ k
- i
]) >> 15;
45 for (k
= i
; k
< len
; k
++)
46 fc_out
[k
] += (fc_in
[i
] * filter
[ k
- i
]) >> 15;
51 void ff_celp_circ_addf(float *out
, const float *in
,
52 const float *lagged
, int lag
, float fac
, int n
)
55 for (k
= 0; k
< lag
; k
++)
56 out
[k
] = in
[k
] + fac
* lagged
[n
+ k
- lag
];
58 out
[k
] = in
[k
] + fac
* lagged
[ k
- lag
];
61 int ff_celp_lp_synthesis_filter(int16_t *out
, const int16_t *filter_coeffs
,
62 const int16_t *in
, int buffer_length
,
63 int filter_length
, int stop_on_overflow
,
64 int shift
, int rounder
)
68 for (n
= 0; n
< buffer_length
; n
++) {
69 int sum
= rounder
, sum1
;
70 for (i
= 1; i
<= filter_length
; i
++)
71 sum
-= (unsigned)(filter_coeffs
[i
-1] * out
[n
-i
]);
73 sum1
= ((sum
>> 12) + in
[n
]) >> shift
;
74 sum
= av_clip_int16(sum1
);
76 if (stop_on_overflow
&& sum
!= sum1
)
85 void ff_celp_lp_synthesis_filterf(float *out
, const float *filter_coeffs
,
86 const float* in
, int buffer_length
,
91 #if 0 // Unoptimized code path for improved readability
92 for (n
= 0; n
< buffer_length
; n
++) {
94 for (i
= 1; i
<= filter_length
; i
++)
95 out
[n
] -= filter_coeffs
[i
-1] * out
[n
-i
];
98 float out0
, out1
, out2
, out3
;
99 float old_out0
, old_out1
, old_out2
, old_out3
;
102 a
= filter_coeffs
[0];
103 b
= filter_coeffs
[1];
104 c
= filter_coeffs
[2];
105 b
-= filter_coeffs
[0] * filter_coeffs
[0];
106 c
-= filter_coeffs
[1] * filter_coeffs
[0];
107 c
-= filter_coeffs
[0] * b
;
109 av_assert2((filter_length
&1)==0 && filter_length
>=4);
115 for (n
= 0; n
<= buffer_length
- 4; n
+=4) {
116 float tmp0
,tmp1
,tmp2
;
124 out0
-= filter_coeffs
[2] * old_out1
;
125 out1
-= filter_coeffs
[2] * old_out2
;
126 out2
-= filter_coeffs
[2] * old_out3
;
128 out0
-= filter_coeffs
[1] * old_out2
;
129 out1
-= filter_coeffs
[1] * old_out3
;
131 out0
-= filter_coeffs
[0] * old_out3
;
133 val
= filter_coeffs
[3];
135 out0
-= val
* old_out0
;
136 out1
-= val
* old_out1
;
137 out2
-= val
* old_out2
;
138 out3
-= val
* old_out3
;
140 for (i
= 5; i
< filter_length
; i
+= 2) {
142 val
= filter_coeffs
[i
-1];
144 out0
-= val
* old_out3
;
145 out1
-= val
* old_out0
;
146 out2
-= val
* old_out1
;
147 out3
-= val
* old_out2
;
149 old_out2
= out
[-i
-1];
151 val
= filter_coeffs
[i
];
153 out0
-= val
* old_out2
;
154 out1
-= val
* old_out3
;
155 out2
-= val
* old_out0
;
156 out3
-= val
* old_out1
;
158 FFSWAP(float, old_out0
, old_out2
);
192 for (; n
< buffer_length
; n
++) {
194 for (i
= 1; i
<= filter_length
; i
++)
195 out
[n
] -= filter_coeffs
[i
-1] * out
[n
-i
];
200 void ff_celp_lp_zero_synthesis_filterf(float *out
, const float *filter_coeffs
,
201 const float *in
, int buffer_length
,
206 for (n
= 0; n
< buffer_length
; n
++) {
208 for (i
= 1; i
<= filter_length
; i
++)
209 out
[n
] += filter_coeffs
[i
-1] * in
[n
-i
];
213 void ff_celp_filter_init(CELPFContext
*c
)
215 c
->celp_lp_synthesis_filterf
= ff_celp_lp_synthesis_filterf
;
216 c
->celp_lp_zero_synthesis_filterf
= ff_celp_lp_zero_synthesis_filterf
;
219 ff_celp_filter_init_mips(c
);