• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/celp_filters.c

Go to the documentation of this file.
00001 /*
00002  * various filters for ACELP-based codecs
00003  *
00004  * Copyright (c) 2008 Vladimir Voroshilov
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include <inttypes.h>
00024 
00025 #include "avcodec.h"
00026 #include "celp_filters.h"
00027 
00028 void ff_celp_convolve_circ(int16_t* fc_out, const int16_t* fc_in,
00029                            const int16_t* filter, int len)
00030 {
00031     int i, k;
00032 
00033     memset(fc_out, 0, len * sizeof(int16_t));
00034 
00035     /* Since there are few pulses over an entire subframe (i.e. almost
00036        all fc_in[i] are zero) it is faster to loop over fc_in first. */
00037     for (i = 0; i < len; i++) {
00038         if (fc_in[i]) {
00039             for (k = 0; k < i; k++)
00040                 fc_out[k] += (fc_in[i] * filter[len + k - i]) >> 15;
00041 
00042             for (k = i; k < len; k++)
00043                 fc_out[k] += (fc_in[i] * filter[      k - i]) >> 15;
00044         }
00045     }
00046 }
00047 
00048 void ff_celp_circ_addf(float *out, const float *in,
00049                        const float *lagged, int lag, float fac, int n)
00050 {
00051     int k;
00052     for (k = 0; k < lag; k++)
00053         out[k] = in[k] + fac * lagged[n + k - lag];
00054     for (; k < n; k++)
00055         out[k] = in[k] + fac * lagged[    k - lag];
00056 }
00057 
00058 int ff_celp_lp_synthesis_filter(int16_t *out, const int16_t *filter_coeffs,
00059                                 const int16_t *in, int buffer_length,
00060                                 int filter_length, int stop_on_overflow,
00061                                 int rounder)
00062 {
00063     int i,n;
00064 
00065     for (n = 0; n < buffer_length; n++) {
00066         int sum = rounder;
00067         for (i = 1; i <= filter_length; i++)
00068             sum -= filter_coeffs[i-1] * out[n-i];
00069 
00070         sum = (sum >> 12) + in[n];
00071 
00072         if (sum + 0x8000 > 0xFFFFU) {
00073             if (stop_on_overflow)
00074                 return 1;
00075             sum = (sum >> 31) ^ 32767;
00076         }
00077         out[n] = sum;
00078     }
00079 
00080     return 0;
00081 }
00082 
00083 void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs,
00084                                   const float* in, int buffer_length,
00085                                   int filter_length)
00086 {
00087     int i,n;
00088 
00089 #if 0 // Unoptimized code path for improved readability
00090     for (n = 0; n < buffer_length; n++) {
00091         out[n] = in[n];
00092         for (i = 1; i <= filter_length; i++)
00093             out[n] -= filter_coeffs[i-1] * out[n-i];
00094     }
00095 #else
00096     float out0, out1, out2, out3;
00097     float old_out0, old_out1, old_out2, old_out3;
00098     float a,b,c;
00099 
00100     a = filter_coeffs[0];
00101     b = filter_coeffs[1];
00102     c = filter_coeffs[2];
00103     b -= filter_coeffs[0] * filter_coeffs[0];
00104     c -= filter_coeffs[1] * filter_coeffs[0];
00105     c -= filter_coeffs[0] * b;
00106 
00107     old_out0 = out[-4];
00108     old_out1 = out[-3];
00109     old_out2 = out[-2];
00110     old_out3 = out[-1];
00111     for (n = 0; n <= buffer_length - 4; n+=4) {
00112         float tmp0,tmp1,tmp2,tmp3;
00113         float val;
00114 
00115         out0 = in[0];
00116         out1 = in[1];
00117         out2 = in[2];
00118         out3 = in[3];
00119 
00120         out0 -= filter_coeffs[2] * old_out1;
00121         out1 -= filter_coeffs[2] * old_out2;
00122         out2 -= filter_coeffs[2] * old_out3;
00123 
00124         out0 -= filter_coeffs[1] * old_out2;
00125         out1 -= filter_coeffs[1] * old_out3;
00126 
00127         out0 -= filter_coeffs[0] * old_out3;
00128 
00129         val = filter_coeffs[3];
00130 
00131         out0 -= val * old_out0;
00132         out1 -= val * old_out1;
00133         out2 -= val * old_out2;
00134         out3 -= val * old_out3;
00135 
00136         old_out3 = out[-5];
00137 
00138         for (i = 5; i <= filter_length; i += 2) {
00139             val = filter_coeffs[i-1];
00140 
00141             out0 -= val * old_out3;
00142             out1 -= val * old_out0;
00143             out2 -= val * old_out1;
00144             out3 -= val * old_out2;
00145 
00146             old_out2 = out[-i-1];
00147 
00148             val = filter_coeffs[i];
00149 
00150             out0 -= val * old_out2;
00151             out1 -= val * old_out3;
00152             out2 -= val * old_out0;
00153             out3 -= val * old_out1;
00154 
00155             FFSWAP(float, old_out0, old_out2);
00156             old_out1 = old_out3;
00157             old_out3 = out[-i-2];
00158         }
00159 
00160         tmp0 = out0;
00161         tmp1 = out1;
00162         tmp2 = out2;
00163         tmp3 = out3;
00164 
00165         out3 -= a * tmp2;
00166         out2 -= a * tmp1;
00167         out1 -= a * tmp0;
00168 
00169         out3 -= b * tmp1;
00170         out2 -= b * tmp0;
00171 
00172         out3 -= c * tmp0;
00173 
00174 
00175         out[0] = out0;
00176         out[1] = out1;
00177         out[2] = out2;
00178         out[3] = out3;
00179 
00180         old_out0 = out0;
00181         old_out1 = out1;
00182         old_out2 = out2;
00183         old_out3 = out3;
00184 
00185         out += 4;
00186         in  += 4;
00187     }
00188 
00189     out -= n;
00190     in -= n;
00191     for (; n < buffer_length; n++) {
00192         out[n] = in[n];
00193         for (i = 1; i <= filter_length; i++)
00194             out[n] -= filter_coeffs[i-1] * out[n-i];
00195     }
00196 #endif
00197 }
00198 
00199 void ff_celp_lp_zero_synthesis_filterf(float *out, const float *filter_coeffs,
00200                                        const float *in, int buffer_length,
00201                                        int filter_length)
00202 {
00203     int i,n;
00204 
00205     for (n = 0; n < buffer_length; n++) {
00206         out[n] = in[n];
00207         for (i = 1; i <= filter_length; i++)
00208             out[n] += filter_coeffs[i-1] * in[n-i];
00209     }
00210 }

Generated on Fri Sep 16 2011 17:17:34 for FFmpeg by  doxygen 1.7.1