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
28void ff_celp_convolve_circ(
29        int16_t* fc_out,
30        const int16_t* fc_in,
31        const int16_t* filter,
32        int len)
33{
34    int i, k;
35
36    memset(fc_out, 0, len * sizeof(int16_t));
37
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++)
41    {
42        if(fc_in[i])
43        {
44            for(k=0; k<i; k++)
45                fc_out[k] += (fc_in[i] * filter[len + k - i]) >> 15;
46
47            for(k=i; k<len; k++)
48                fc_out[k] += (fc_in[i] * filter[      k - i]) >> 15;
49        }
50    }
51}
52
53int ff_celp_lp_synthesis_filter(
54        int16_t *out,
55        const int16_t* filter_coeffs,
56        const int16_t* in,
57        int buffer_length,
58        int filter_length,
59        int stop_on_overflow,
60        int rounder)
61{
62    int i,n;
63
64    // These two lines are to avoid a -1 subtraction in the main loop
65    filter_length++;
66    filter_coeffs--;
67
68    for(n=0; n<buffer_length; n++)
69    {
70        int sum = rounder;
71        for(i=1; i<filter_length; i++)
72            sum -= filter_coeffs[i] * out[n-i];
73
74        sum = (sum >> 12) + in[n];
75
76        if(sum + 0x8000 > 0xFFFFU)
77        {
78            if(stop_on_overflow)
79                return 1;
80            sum = (sum >> 31) ^ 32767;
81        }
82        out[n] = sum;
83    }
84
85    return 0;
86}
87
88void ff_celp_lp_synthesis_filterf(
89        float *out,
90        const float* filter_coeffs,
91        const float* in,
92        int buffer_length,
93        int filter_length)
94{
95    int i,n;
96
97    // These two lines are to avoid a -1 subtraction in the main loop
98    filter_length++;
99    filter_coeffs--;
100
101    for(n=0; n<buffer_length; n++)
102    {
103        out[n] = in[n];
104        for(i=1; i<filter_length; i++)
105            out[n] -= filter_coeffs[i] * out[n-i];
106    }
107}
108