3 * Copyright (C) 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
28 #include <gst/base/gstbasetransform.h>
29 #include <gst/audio/audio.h>
30 #include <gst/audio/gstaudiofilter.h>
34 #include "audiofxbaseiirfilter.h"
36 #define GST_CAT_DEFAULT gst_audio_fx_base_iir_filter_debug
37 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
39 #define ALLOWED_CAPS \
41 " format=(string){"GST_AUDIO_NE(F32)","GST_AUDIO_NE(F64)"}," \
42 " rate = (int) [ 1, MAX ]," \
43 " channels = (int) [ 1, MAX ]," \
44 " layout=(string) interleaved"
46 #define gst_audio_fx_base_iir_filter_parent_class parent_class
47 G_DEFINE_TYPE (GstAudioFXBaseIIRFilter,
48 gst_audio_fx_base_iir_filter, GST_TYPE_AUDIO_FILTER);
50 static gboolean gst_audio_fx_base_iir_filter_setup (GstAudioFilter * filter,
51 const GstAudioInfo * info);
53 gst_audio_fx_base_iir_filter_transform_ip (GstBaseTransform * base,
55 static gboolean gst_audio_fx_base_iir_filter_stop (GstBaseTransform * base);
57 static void process_64 (GstAudioFXBaseIIRFilter * filter,
58 gdouble * data, guint num_samples);
59 static void process_32 (GstAudioFXBaseIIRFilter * filter,
60 gfloat * data, guint num_samples);
62 /* GObject vmethod implementations */
65 gst_audio_fx_base_iir_filter_finalize (GObject * object)
67 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (object);
79 if (filter->channels) {
80 GstAudioFXBaseIIRFilterChannelCtx *ctx;
83 for (i = 0; i < filter->nchannels; i++) {
84 ctx = &filter->channels[i];
89 g_free (filter->channels);
90 filter->channels = NULL;
92 g_mutex_clear (&filter->lock);
94 G_OBJECT_CLASS (parent_class)->finalize (object);
98 gst_audio_fx_base_iir_filter_class_init (GstAudioFXBaseIIRFilterClass * klass)
100 GObjectClass *gobject_class = (GObjectClass *) klass;
101 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
102 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
105 GST_DEBUG_CATEGORY_INIT (gst_audio_fx_base_iir_filter_debug,
106 "audiofxbaseiirfilter", 0, "Audio IIR Filter Base Class");
108 gobject_class->finalize = gst_audio_fx_base_iir_filter_finalize;
110 caps = gst_caps_from_string (ALLOWED_CAPS);
111 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
113 gst_caps_unref (caps);
115 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_fx_base_iir_filter_setup);
117 trans_class->transform_ip =
118 GST_DEBUG_FUNCPTR (gst_audio_fx_base_iir_filter_transform_ip);
119 trans_class->transform_ip_on_passthrough = FALSE;
120 trans_class->stop = GST_DEBUG_FUNCPTR (gst_audio_fx_base_iir_filter_stop);
124 gst_audio_fx_base_iir_filter_init (GstAudioFXBaseIIRFilter * filter)
126 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
132 filter->channels = NULL;
133 filter->nchannels = 0;
135 g_mutex_init (&filter->lock);
138 /* Evaluate the transfer function that corresponds to the IIR
139 * coefficients at (zr + zi*I)^-1 and return the magnitude */
141 gst_audio_fx_base_iir_filter_calculate_gain (gdouble * a, guint na, gdouble * b,
142 guint nb, gdouble zr, gdouble zi)
144 gdouble sum_ar, sum_ai;
145 gdouble sum_br, sum_bi;
146 gdouble gain_r, gain_i;
155 for (i = na - 2; i >= 0; i--) {
159 sum_ar = (sum_r_old * zr - sum_i_old * zi) + a[i];
160 sum_ai = (sum_r_old * zi + sum_i_old * zr) + 0.0;
165 for (i = nb - 2; i >= 0; i--) {
169 sum_br = (sum_r_old * zr - sum_i_old * zi) + b[i];
170 sum_bi = (sum_r_old * zi + sum_i_old * zr) + 0.0;
174 (sum_br * sum_ar + sum_bi * sum_ai) / (sum_ar * sum_ar + sum_ai * sum_ai);
176 (sum_bi * sum_ar - sum_br * sum_ai) / (sum_ar * sum_ar + sum_ai * sum_ai);
178 return (sqrt (gain_r * gain_r + gain_i * gain_i));
182 gst_audio_fx_base_iir_filter_set_coefficients (GstAudioFXBaseIIRFilter * filter,
183 gdouble * a, guint na, gdouble * b, guint nb)
187 g_return_if_fail (GST_IS_AUDIO_FX_BASE_IIR_FILTER (filter));
189 g_mutex_lock (&filter->lock);
194 filter->a = filter->b = NULL;
196 if (filter->channels) {
197 GstAudioFXBaseIIRFilterChannelCtx *ctx;
198 gboolean free = (na != filter->na || nb != filter->nb);
200 for (i = 0; i < filter->nchannels; i++) {
201 ctx = &filter->channels[i];
206 memset (ctx->x, 0, filter->nb * sizeof (gdouble));
211 memset (ctx->y, 0, filter->na * sizeof (gdouble));
214 g_free (filter->channels);
215 filter->channels = NULL;
224 if (filter->nchannels && !filter->channels) {
225 GstAudioFXBaseIIRFilterChannelCtx *ctx;
228 g_new0 (GstAudioFXBaseIIRFilterChannelCtx, filter->nchannels);
229 for (i = 0; i < filter->nchannels; i++) {
230 ctx = &filter->channels[i];
232 ctx->x = g_new0 (gdouble, filter->nb);
233 ctx->y = g_new0 (gdouble, filter->na);
237 g_mutex_unlock (&filter->lock);
240 /* GstAudioFilter vmethod implementations */
243 gst_audio_fx_base_iir_filter_setup (GstAudioFilter * base,
244 const GstAudioInfo * info)
246 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (base);
250 g_mutex_lock (&filter->lock);
251 switch (GST_AUDIO_INFO_FORMAT (info)) {
252 case GST_AUDIO_FORMAT_F32:
253 filter->process = (GstAudioFXBaseIIRFilterProcessFunc)
256 case GST_AUDIO_FORMAT_F64:
257 filter->process = (GstAudioFXBaseIIRFilterProcessFunc)
265 channels = GST_AUDIO_INFO_CHANNELS (info);
267 if (channels != filter->nchannels) {
269 GstAudioFXBaseIIRFilterChannelCtx *ctx;
271 if (filter->channels) {
272 for (i = 0; i < filter->nchannels; i++) {
273 ctx = &filter->channels[i];
278 g_free (filter->channels);
281 filter->channels = g_new0 (GstAudioFXBaseIIRFilterChannelCtx, channels);
282 for (i = 0; i < channels; i++) {
283 ctx = &filter->channels[i];
285 ctx->x = g_new0 (gdouble, filter->nb);
286 ctx->y = g_new0 (gdouble, filter->na);
288 filter->nchannels = channels;
290 g_mutex_unlock (&filter->lock);
295 static inline gdouble
296 process (GstAudioFXBaseIIRFilter * filter,
297 GstAudioFXBaseIIRFilterChannelCtx * ctx, gdouble x0)
299 gdouble val = filter->b[0] * x0;
302 for (i = 1, j = ctx->x_pos; i < filter->nb; i++) {
303 val += filter->b[i] * ctx->x[j];
309 for (i = 1, j = ctx->y_pos; i < filter->na; i++) {
310 val -= filter->a[i] * ctx->y[j];
319 if (ctx->x_pos >= filter->nb)
321 ctx->x[ctx->x_pos] = x0;
325 if (ctx->y_pos >= filter->na)
328 ctx->y[ctx->y_pos] = val;
334 #define DEFINE_PROCESS_FUNC(width,ctype) \
336 process_##width (GstAudioFXBaseIIRFilter * filter, \
337 g##ctype * data, guint num_samples) \
339 gint i, j, channels = filter->nchannels; \
342 for (i = 0; i < num_samples / channels; i++) { \
343 for (j = 0; j < channels; j++) { \
344 val = process (filter, &filter->channels[j], *data); \
350 DEFINE_PROCESS_FUNC (32, float);
351 DEFINE_PROCESS_FUNC (64, double);
353 #undef DEFINE_PROCESS_FUNC
355 /* GstBaseTransform vmethod implementations */
357 gst_audio_fx_base_iir_filter_transform_ip (GstBaseTransform * base,
360 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (base);
362 GstClockTime timestamp, stream_time;
365 timestamp = GST_BUFFER_TIMESTAMP (buf);
367 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
369 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
370 GST_TIME_ARGS (timestamp));
372 if (GST_CLOCK_TIME_IS_VALID (stream_time))
373 gst_object_sync_values (GST_OBJECT (filter), stream_time);
375 g_return_val_if_fail (filter->a != NULL, GST_FLOW_ERROR);
377 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
378 num_samples = map.size / GST_AUDIO_FILTER_BPS (filter);
380 g_mutex_lock (&filter->lock);
381 filter->process (filter, map.data, num_samples);
382 g_mutex_unlock (&filter->lock);
384 gst_buffer_unmap (buf, &map);
391 gst_audio_fx_base_iir_filter_stop (GstBaseTransform * base)
393 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (base);
394 guint channels = filter->nchannels;
395 GstAudioFXBaseIIRFilterChannelCtx *ctx;
398 /* Reset the history of input and output values if
399 * already existing */
400 if (channels && filter->channels) {
401 for (i = 0; i < channels; i++) {
402 ctx = &filter->channels[i];
406 g_free (filter->channels);
408 filter->channels = NULL;
409 filter->nchannels = 0;