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/avassert.h"
28 #include "libavutil/common.h"
30 void ff_celp_convolve_circ(int16_t* fc_out
, const int16_t* fc_in
,
31 const int16_t* filter
, int len
)
35 memset(fc_out
, 0, len
* sizeof(int16_t));
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
++) {
41 for (k
= 0; k
< i
; k
++)
42 fc_out
[k
] += (fc_in
[i
] * filter
[len
+ k
- i
]) >> 15;
44 for (k
= i
; k
< len
; k
++)
45 fc_out
[k
] += (fc_in
[i
] * filter
[ k
- i
]) >> 15;
50 void ff_celp_circ_addf(float *out
, const float *in
,
51 const float *lagged
, int lag
, float fac
, int n
)
54 for (k
= 0; k
< lag
; k
++)
55 out
[k
] = in
[k
] + fac
* lagged
[n
+ k
- lag
];
57 out
[k
] = in
[k
] + fac
* lagged
[ k
- lag
];
60 int ff_celp_lp_synthesis_filter(int16_t *out
, const int16_t *filter_coeffs
,
61 const int16_t *in
, int buffer_length
,
62 int filter_length
, int stop_on_overflow
,
63 int shift
, int rounder
)
67 for (n
= 0; n
< buffer_length
; n
++) {
68 int sum
= -rounder
, sum1
;
69 for (i
= 1; i
<= filter_length
; i
++)
70 sum
+= filter_coeffs
[i
-1] * out
[n
-i
];
72 sum1
= ((-sum
>> 12) + in
[n
]) >> shift
;
73 sum
= av_clip_int16(sum1
);
75 if (stop_on_overflow
&& sum
!= sum1
)
84 void ff_celp_lp_synthesis_filterf(float *out
, const float *filter_coeffs
,
85 const float* in
, int buffer_length
,
90 #if 0 // Unoptimized code path for improved readability
91 for (n
= 0; n
< buffer_length
; n
++) {
93 for (i
= 1; i
<= filter_length
; i
++)
94 out
[n
] -= filter_coeffs
[i
-1] * out
[n
-i
];
97 float out0
, out1
, out2
, out3
;
98 float old_out0
, old_out1
, old_out2
, old_out3
;
101 a
= filter_coeffs
[0];
102 b
= filter_coeffs
[1];
103 c
= filter_coeffs
[2];
104 b
-= filter_coeffs
[0] * filter_coeffs
[0];
105 c
-= filter_coeffs
[1] * filter_coeffs
[0];
106 c
-= filter_coeffs
[0] * b
;
108 av_assert2((filter_length
&1)==0 && filter_length
>=4);
114 for (n
= 0; n
<= buffer_length
- 4; n
+=4) {
115 float tmp0
,tmp1
,tmp2
;
123 out0
-= filter_coeffs
[2] * old_out1
;
124 out1
-= filter_coeffs
[2] * old_out2
;
125 out2
-= filter_coeffs
[2] * old_out3
;
127 out0
-= filter_coeffs
[1] * old_out2
;
128 out1
-= filter_coeffs
[1] * old_out3
;
130 out0
-= filter_coeffs
[0] * old_out3
;
132 val
= filter_coeffs
[3];
134 out0
-= val
* old_out0
;
135 out1
-= val
* old_out1
;
136 out2
-= val
* old_out2
;
137 out3
-= val
* old_out3
;
139 for (i
= 5; i
< filter_length
; i
+= 2) {
141 val
= filter_coeffs
[i
-1];
143 out0
-= val
* old_out3
;
144 out1
-= val
* old_out0
;
145 out2
-= val
* old_out1
;
146 out3
-= val
* old_out2
;
148 old_out2
= out
[-i
-1];
150 val
= filter_coeffs
[i
];
152 out0
-= val
* old_out2
;
153 out1
-= val
* old_out3
;
154 out2
-= val
* old_out0
;
155 out3
-= val
* old_out1
;
157 FFSWAP(float, old_out0
, old_out2
);
191 for (; n
< buffer_length
; n
++) {
193 for (i
= 1; i
<= filter_length
; i
++)
194 out
[n
] -= filter_coeffs
[i
-1] * out
[n
-i
];
199 void ff_celp_lp_zero_synthesis_filterf(float *out
, const float *filter_coeffs
,
200 const float *in
, int buffer_length
,
205 for (n
= 0; n
< buffer_length
; n
++) {
207 for (i
= 1; i
<= filter_length
; i
++)
208 out
[n
] += filter_coeffs
[i
-1] * in
[n
-i
];
212 void ff_celp_filter_init(CELPFContext
*c
)
214 c
->celp_lp_synthesis_filterf
= ff_celp_lp_synthesis_filterf
;
215 c
->celp_lp_zero_synthesis_filterf
= ff_celp_lp_zero_synthesis_filterf
;
218 ff_celp_filter_init_mips(c
);