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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
26 #include <gst/base/gstbasetransform.h>
27 #include <gst/audio/audio.h>
28 #include <gst/audio/gstaudiofilter.h>
29 #include <gst/controller/gstcontroller.h>
33 #include "audiofxbaseiirfilter.h"
35 #define GST_CAT_DEFAULT gst_audio_fx_base_iir_filter_debug
36 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
38 #define ALLOWED_CAPS \
39 "audio/x-raw-float," \
40 " width = (int) { 32, 64 }, " \
41 " endianness = (int) BYTE_ORDER," \
42 " rate = (int) [ 1, MAX ]," \
43 " channels = (int) [ 1, MAX ]"
45 #define gst_audio_fx_base_iir_filter_parent_class parent_class
46 G_DEFINE_TYPE (GstAudioFXBaseIIRFilter,
47 gst_audio_fx_base_iir_filter, GST_TYPE_AUDIO_FILTER);
49 static gboolean gst_audio_fx_base_iir_filter_setup (GstAudioFilter * filter,
50 GstRingBufferSpec * format);
52 gst_audio_fx_base_iir_filter_transform_ip (GstBaseTransform * base,
54 static gboolean gst_audio_fx_base_iir_filter_stop (GstBaseTransform * base);
56 static void process_64 (GstAudioFXBaseIIRFilter * filter,
57 gdouble * data, guint num_samples);
58 static void process_32 (GstAudioFXBaseIIRFilter * filter,
59 gfloat * data, guint num_samples);
61 /* GObject vmethod implementations */
64 gst_audio_fx_base_iir_filter_dispose (GObject * object)
66 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (object);
78 if (filter->channels) {
79 GstAudioFXBaseIIRFilterChannelCtx *ctx;
82 for (i = 0; i < filter->nchannels; i++) {
83 ctx = &filter->channels[i];
88 g_free (filter->channels);
89 filter->channels = NULL;
92 G_OBJECT_CLASS (parent_class)->dispose (object);
96 gst_audio_fx_base_iir_filter_class_init (GstAudioFXBaseIIRFilterClass * klass)
98 GObjectClass *gobject_class = (GObjectClass *) klass;
99 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
100 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
103 GST_DEBUG_CATEGORY_INIT (gst_audio_fx_base_iir_filter_debug,
104 "audiofxbaseiirfilter", 0, "Audio IIR Filter Base Class");
106 gobject_class->dispose = gst_audio_fx_base_iir_filter_dispose;
108 caps = gst_caps_from_string (ALLOWED_CAPS);
109 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
111 gst_caps_unref (caps);
113 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_fx_base_iir_filter_setup);
115 trans_class->transform_ip =
116 GST_DEBUG_FUNCPTR (gst_audio_fx_base_iir_filter_transform_ip);
117 trans_class->stop = GST_DEBUG_FUNCPTR (gst_audio_fx_base_iir_filter_stop);
121 gst_audio_fx_base_iir_filter_init (GstAudioFXBaseIIRFilter * filter)
123 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
129 filter->channels = NULL;
130 filter->nchannels = 0;
133 /* Evaluate the transfer function that corresponds to the IIR
134 * coefficients at zr + zi*I and return the magnitude */
136 gst_audio_fx_base_iir_filter_calculate_gain (gdouble * a, guint na, gdouble * b,
137 guint nb, gdouble zr, gdouble zi)
139 gdouble sum_ar, sum_ai;
140 gdouble sum_br, sum_bi;
141 gdouble gain_r, gain_i;
150 for (i = na - 1; i >= 0; i--) {
154 sum_ar = (sum_r_old * zr - sum_i_old * zi) + a[i];
155 sum_ai = (sum_r_old * zi + sum_i_old * zr) + 0.0;
160 for (i = nb - 1; i >= 0; i--) {
164 sum_br = (sum_r_old * zr - sum_i_old * zi) - b[i];
165 sum_bi = (sum_r_old * zi + sum_i_old * zr) - 0.0;
171 (sum_ar * sum_br + sum_ai * sum_bi) / (sum_br * sum_br + sum_bi * sum_bi);
173 (sum_ai * sum_br - sum_ar * sum_bi) / (sum_br * sum_br + sum_bi * sum_bi);
175 return (sqrt (gain_r * gain_r + gain_i * gain_i));
179 gst_audio_fx_base_iir_filter_set_coefficients (GstAudioFXBaseIIRFilter * filter,
180 gdouble * a, guint na, gdouble * b, guint nb)
184 g_return_if_fail (GST_IS_AUDIO_FX_BASE_IIR_FILTER (filter));
186 GST_BASE_TRANSFORM_LOCK (filter);
191 filter->a = filter->b = NULL;
193 if (filter->channels) {
194 GstAudioFXBaseIIRFilterChannelCtx *ctx;
195 gboolean free = (na != filter->na || nb != filter->nb);
197 for (i = 0; i < filter->nchannels; i++) {
198 ctx = &filter->channels[i];
203 memset (ctx->x, 0, filter->na * sizeof (gdouble));
208 memset (ctx->y, 0, filter->nb * sizeof (gdouble));
211 g_free (filter->channels);
212 filter->channels = NULL;
221 if (filter->nchannels && !filter->channels) {
222 GstAudioFXBaseIIRFilterChannelCtx *ctx;
225 g_new0 (GstAudioFXBaseIIRFilterChannelCtx, filter->nchannels);
226 for (i = 0; i < filter->nchannels; i++) {
227 ctx = &filter->channels[i];
229 ctx->x = g_new0 (gdouble, filter->na);
230 ctx->y = g_new0 (gdouble, filter->nb);
234 GST_BASE_TRANSFORM_UNLOCK (filter);
237 /* GstAudioFilter vmethod implementations */
240 gst_audio_fx_base_iir_filter_setup (GstAudioFilter * base,
241 GstRingBufferSpec * format)
243 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (base);
246 if (format->width == 32)
247 filter->process = (GstAudioFXBaseIIRFilterProcessFunc)
249 else if (format->width == 64)
250 filter->process = (GstAudioFXBaseIIRFilterProcessFunc)
255 if (format->channels != filter->nchannels) {
257 GstAudioFXBaseIIRFilterChannelCtx *ctx;
259 if (filter->channels) {
261 for (i = 0; i < filter->nchannels; i++) {
262 ctx = &filter->channels[i];
268 g_free (filter->channels);
269 filter->channels = NULL;
272 filter->nchannels = format->channels;
275 g_new0 (GstAudioFXBaseIIRFilterChannelCtx, filter->nchannels);
276 for (i = 0; i < filter->nchannels; i++) {
277 ctx = &filter->channels[i];
279 ctx->x = g_new0 (gdouble, filter->na);
280 ctx->y = g_new0 (gdouble, filter->nb);
287 static inline gdouble
288 process (GstAudioFXBaseIIRFilter * filter,
289 GstAudioFXBaseIIRFilterChannelCtx * ctx, gdouble x0)
291 gdouble val = filter->a[0] * x0;
294 for (i = 1, j = ctx->x_pos; i < filter->na; i++) {
295 val += filter->a[i] * ctx->x[j];
301 for (i = 1, j = ctx->y_pos; i < filter->nb; i++) {
302 val += filter->b[i] * ctx->y[j];
310 if (ctx->x_pos >= filter->na)
312 ctx->x[ctx->x_pos] = x0;
316 if (ctx->y_pos >= filter->nb)
319 ctx->y[ctx->y_pos] = val;
325 #define DEFINE_PROCESS_FUNC(width,ctype) \
327 process_##width (GstAudioFXBaseIIRFilter * filter, \
328 g##ctype * data, guint num_samples) \
330 gint i, j, channels = GST_AUDIO_FILTER (filter)->format.channels; \
333 for (i = 0; i < num_samples / channels; i++) { \
334 for (j = 0; j < channels; j++) { \
335 val = process (filter, &filter->channels[j], *data); \
341 DEFINE_PROCESS_FUNC (32, float);
342 DEFINE_PROCESS_FUNC (64, double);
344 #undef DEFINE_PROCESS_FUNC
346 /* GstBaseTransform vmethod implementations */
348 gst_audio_fx_base_iir_filter_transform_ip (GstBaseTransform * base,
351 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (base);
353 GstClockTime timestamp, stream_time;
357 timestamp = GST_BUFFER_TIMESTAMP (buf);
359 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
361 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
362 GST_TIME_ARGS (timestamp));
364 if (GST_CLOCK_TIME_IS_VALID (stream_time))
365 gst_object_sync_values (G_OBJECT (filter), stream_time);
367 if (gst_base_transform_is_passthrough (base))
370 g_return_val_if_fail (filter->a != NULL, GST_FLOW_ERROR);
372 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
373 num_samples = size / (GST_AUDIO_FILTER (filter)->format.width / 8);
375 filter->process (filter, data, num_samples);
377 gst_buffer_unmap (buf, data, size);
384 gst_audio_fx_base_iir_filter_stop (GstBaseTransform * base)
386 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (base);
387 guint channels = GST_AUDIO_FILTER (filter)->format.channels;
388 GstAudioFXBaseIIRFilterChannelCtx *ctx;
391 /* Reset the history of input and output values if
392 * already existing */
393 if (channels && filter->channels) {
394 for (i = 0; i < channels; i++) {
395 ctx = &filter->channels[i];
399 g_free (filter->channels);
401 filter->channels = NULL;