Libav
celp_filters.c
Go to the documentation of this file.
1 /*
2  * various filters for ACELP-based codecs
3  *
4  * Copyright (c) 2008 Vladimir Voroshilov
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 #include "libavutil/common.h"
28 
29 void ff_celp_convolve_circ(int16_t* fc_out, const int16_t* fc_in,
30  const int16_t* filter, int len)
31 {
32  int i, k;
33 
34  memset(fc_out, 0, len * sizeof(int16_t));
35 
36  /* Since there are few pulses over an entire subframe (i.e. almost
37  all fc_in[i] are zero) it is faster to loop over fc_in first. */
38  for (i = 0; i < len; i++) {
39  if (fc_in[i]) {
40  for (k = 0; k < i; k++)
41  fc_out[k] += (fc_in[i] * filter[len + k - i]) >> 15;
42 
43  for (k = i; k < len; k++)
44  fc_out[k] += (fc_in[i] * filter[ k - i]) >> 15;
45  }
46  }
47 }
48 
49 void ff_celp_circ_addf(float *out, const float *in,
50  const float *lagged, int lag, float fac, int n)
51 {
52  int k;
53  for (k = 0; k < lag; k++)
54  out[k] = in[k] + fac * lagged[n + k - lag];
55  for (; k < n; k++)
56  out[k] = in[k] + fac * lagged[ k - lag];
57 }
58 
59 int ff_celp_lp_synthesis_filter(int16_t *out, const int16_t *filter_coeffs,
60  const int16_t *in, int buffer_length,
61  int filter_length, int stop_on_overflow,
62  int shift, int rounder)
63 {
64  int i,n;
65 
66  for (n = 0; n < buffer_length; n++) {
67  int sum = -rounder, sum1;
68  for (i = 1; i <= filter_length; i++)
69  sum += filter_coeffs[i-1] * out[n-i];
70 
71  sum1 = ((-sum >> 12) + in[n]) >> shift;
72  sum = av_clip_int16(sum1);
73 
74  if (stop_on_overflow && sum != sum1)
75  return 1;
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 #if 0 // Unoptimized code path for improved readability
90  for (n = 0; n < buffer_length; n++) {
91  out[n] = in[n];
92  for (i = 1; i <= filter_length; i++)
93  out[n] -= filter_coeffs[i-1] * out[n-i];
94  }
95 #else
96  float out0, out1, out2, out3;
97  float old_out0, old_out1, old_out2, old_out3;
98  float a,b,c;
99 
100  a = filter_coeffs[0];
101  b = filter_coeffs[1];
102  c = filter_coeffs[2];
103  b -= filter_coeffs[0] * filter_coeffs[0];
104  c -= filter_coeffs[1] * filter_coeffs[0];
105  c -= filter_coeffs[0] * b;
106 
107  old_out0 = out[-4];
108  old_out1 = out[-3];
109  old_out2 = out[-2];
110  old_out3 = out[-1];
111  for (n = 0; n <= buffer_length - 4; n+=4) {
112  float tmp0,tmp1,tmp2;
113  float val;
114 
115  out0 = in[0];
116  out1 = in[1];
117  out2 = in[2];
118  out3 = in[3];
119 
120  out0 -= filter_coeffs[2] * old_out1;
121  out1 -= filter_coeffs[2] * old_out2;
122  out2 -= filter_coeffs[2] * old_out3;
123 
124  out0 -= filter_coeffs[1] * old_out2;
125  out1 -= filter_coeffs[1] * old_out3;
126 
127  out0 -= filter_coeffs[0] * old_out3;
128 
129  val = filter_coeffs[3];
130 
131  out0 -= val * old_out0;
132  out1 -= val * old_out1;
133  out2 -= val * old_out2;
134  out3 -= val * old_out3;
135 
136  for (i = 5; i <= filter_length; i += 2) {
137  old_out3 = out[-i];
138  val = filter_coeffs[i-1];
139 
140  out0 -= val * old_out3;
141  out1 -= val * old_out0;
142  out2 -= val * old_out1;
143  out3 -= val * old_out2;
144 
145  old_out2 = out[-i-1];
146 
147  val = filter_coeffs[i];
148 
149  out0 -= val * old_out2;
150  out1 -= val * old_out3;
151  out2 -= val * old_out0;
152  out3 -= val * old_out1;
153 
154  FFSWAP(float, old_out0, old_out2);
155  old_out1 = old_out3;
156  }
157 
158  tmp0 = out0;
159  tmp1 = out1;
160  tmp2 = out2;
161 
162  out3 -= a * tmp2;
163  out2 -= a * tmp1;
164  out1 -= a * tmp0;
165 
166  out3 -= b * tmp1;
167  out2 -= b * tmp0;
168 
169  out3 -= c * tmp0;
170 
171 
172  out[0] = out0;
173  out[1] = out1;
174  out[2] = out2;
175  out[3] = out3;
176 
177  old_out0 = out0;
178  old_out1 = out1;
179  old_out2 = out2;
180  old_out3 = out3;
181 
182  out += 4;
183  in += 4;
184  }
185 
186  out -= n;
187  in -= n;
188  for (; n < buffer_length; n++) {
189  out[n] = in[n];
190  for (i = 1; i <= filter_length; i++)
191  out[n] -= filter_coeffs[i-1] * out[n-i];
192  }
193 #endif
194 }
195 
196 void ff_celp_lp_zero_synthesis_filterf(float *out, const float *filter_coeffs,
197  const float *in, int buffer_length,
198  int filter_length)
199 {
200  int i,n;
201 
202  for (n = 0; n < buffer_length; n++) {
203  out[n] = in[n];
204  for (i = 1; i <= filter_length; i++)
205  out[n] += filter_coeffs[i-1] * in[n-i];
206  }
207 }
void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs, const float *in, int buffer_length, int filter_length)
LP synthesis filter.
Definition: celp_filters.c:83
int ff_celp_lp_synthesis_filter(int16_t *out, const int16_t *filter_coeffs, const int16_t *in, int buffer_length, int filter_length, int stop_on_overflow, int shift, int rounder)
LP synthesis filter.
Definition: celp_filters.c:59
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
#define b
Definition: input.c:52
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
void ff_celp_convolve_circ(int16_t *fc_out, const int16_t *fc_in, const int16_t *filter, int len)
Circularly convolve fixed vector with a phase dispersion impulse response filter (D.6.2 of G.729 and 6.1.5 of AMR).
Definition: celp_filters.c:29
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:307
Libavcodec external API header.
void ff_celp_circ_addf(float *out, const float *in, const float *lagged, int lag, float fac, int n)
Add an array to a rotated array.
Definition: celp_filters.c:49
void ff_celp_lp_zero_synthesis_filterf(float *out, const float *filter_coeffs, const float *in, int buffer_length, int filter_length)
LP zero synthesis filter.
Definition: celp_filters.c:196
common internal and external API header
int len
#define FFSWAP(type, a, b)
Definition: common.h:60