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 \
40 " format=(string){"GST_AUDIO_NE(F32)","GST_AUDIO_NE(F64)"}," \
41 " rate = (int) [ 1, MAX ]," \
42 " channels = (int) [ 1, MAX ]"
44 #define gst_audio_fx_base_iir_filter_parent_class parent_class
45 G_DEFINE_TYPE (GstAudioFXBaseIIRFilter,
46 gst_audio_fx_base_iir_filter, GST_TYPE_AUDIO_FILTER);
48 static gboolean gst_audio_fx_base_iir_filter_setup (GstAudioFilter * filter,
49 const GstAudioInfo * info);
51 gst_audio_fx_base_iir_filter_transform_ip (GstBaseTransform * base,
53 static gboolean gst_audio_fx_base_iir_filter_stop (GstBaseTransform * base);
55 static void process_64 (GstAudioFXBaseIIRFilter * filter,
56 gdouble * data, guint num_samples);
57 static void process_32 (GstAudioFXBaseIIRFilter * filter,
58 gfloat * data, guint num_samples);
60 /* GObject vmethod implementations */
63 gst_audio_fx_base_iir_filter_dispose (GObject * object)
65 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (object);
77 if (filter->channels) {
78 GstAudioFXBaseIIRFilterChannelCtx *ctx;
81 for (i = 0; i < filter->nchannels; i++) {
82 ctx = &filter->channels[i];
87 g_free (filter->channels);
88 filter->channels = NULL;
91 G_OBJECT_CLASS (parent_class)->dispose (object);
95 gst_audio_fx_base_iir_filter_class_init (GstAudioFXBaseIIRFilterClass * klass)
97 GObjectClass *gobject_class = (GObjectClass *) klass;
98 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
99 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
102 GST_DEBUG_CATEGORY_INIT (gst_audio_fx_base_iir_filter_debug,
103 "audiofxbaseiirfilter", 0, "Audio IIR Filter Base Class");
105 gobject_class->dispose = gst_audio_fx_base_iir_filter_dispose;
107 caps = gst_caps_from_string (ALLOWED_CAPS);
108 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
110 gst_caps_unref (caps);
112 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_fx_base_iir_filter_setup);
114 trans_class->transform_ip =
115 GST_DEBUG_FUNCPTR (gst_audio_fx_base_iir_filter_transform_ip);
116 trans_class->stop = GST_DEBUG_FUNCPTR (gst_audio_fx_base_iir_filter_stop);
120 gst_audio_fx_base_iir_filter_init (GstAudioFXBaseIIRFilter * filter)
122 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
128 filter->channels = NULL;
129 filter->nchannels = 0;
132 /* Evaluate the transfer function that corresponds to the IIR
133 * coefficients at zr + zi*I and return the magnitude */
135 gst_audio_fx_base_iir_filter_calculate_gain (gdouble * a, guint na, gdouble * b,
136 guint nb, gdouble zr, gdouble zi)
138 gdouble sum_ar, sum_ai;
139 gdouble sum_br, sum_bi;
140 gdouble gain_r, gain_i;
149 for (i = na - 1; i >= 0; i--) {
153 sum_ar = (sum_r_old * zr - sum_i_old * zi) + a[i];
154 sum_ai = (sum_r_old * zi + sum_i_old * zr) + 0.0;
159 for (i = nb - 1; i >= 0; i--) {
163 sum_br = (sum_r_old * zr - sum_i_old * zi) - b[i];
164 sum_bi = (sum_r_old * zi + sum_i_old * zr) - 0.0;
170 (sum_ar * sum_br + sum_ai * sum_bi) / (sum_br * sum_br + sum_bi * sum_bi);
172 (sum_ai * sum_br - sum_ar * sum_bi) / (sum_br * sum_br + sum_bi * sum_bi);
174 return (sqrt (gain_r * gain_r + gain_i * gain_i));
178 gst_audio_fx_base_iir_filter_set_coefficients (GstAudioFXBaseIIRFilter * filter,
179 gdouble * a, guint na, gdouble * b, guint nb)
183 g_return_if_fail (GST_IS_AUDIO_FX_BASE_IIR_FILTER (filter));
185 GST_BASE_TRANSFORM_LOCK (filter);
190 filter->a = filter->b = NULL;
192 if (filter->channels) {
193 GstAudioFXBaseIIRFilterChannelCtx *ctx;
194 gboolean free = (na != filter->na || nb != filter->nb);
196 for (i = 0; i < filter->nchannels; i++) {
197 ctx = &filter->channels[i];
202 memset (ctx->x, 0, filter->na * sizeof (gdouble));
207 memset (ctx->y, 0, filter->nb * sizeof (gdouble));
210 g_free (filter->channels);
211 filter->channels = NULL;
220 if (filter->nchannels && !filter->channels) {
221 GstAudioFXBaseIIRFilterChannelCtx *ctx;
224 g_new0 (GstAudioFXBaseIIRFilterChannelCtx, filter->nchannels);
225 for (i = 0; i < filter->nchannels; i++) {
226 ctx = &filter->channels[i];
228 ctx->x = g_new0 (gdouble, filter->na);
229 ctx->y = g_new0 (gdouble, filter->nb);
233 GST_BASE_TRANSFORM_UNLOCK (filter);
236 /* GstAudioFilter vmethod implementations */
239 gst_audio_fx_base_iir_filter_setup (GstAudioFilter * base,
240 const GstAudioInfo * info)
242 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (base);
246 switch (GST_AUDIO_INFO_FORMAT (info)) {
247 case GST_AUDIO_FORMAT_F32:
248 filter->process = (GstAudioFXBaseIIRFilterProcessFunc)
251 case GST_AUDIO_FORMAT_F64:
252 filter->process = (GstAudioFXBaseIIRFilterProcessFunc)
260 channels = GST_AUDIO_INFO_CHANNELS (info);
262 if (channels != filter->nchannels) {
264 GstAudioFXBaseIIRFilterChannelCtx *ctx;
266 if (filter->channels) {
267 for (i = 0; i < filter->nchannels; i++) {
268 ctx = &filter->channels[i];
273 g_free (filter->channels);
276 filter->channels = g_new0 (GstAudioFXBaseIIRFilterChannelCtx, channels);
277 for (i = 0; i < filter->nchannels; i++) {
278 ctx = &filter->channels[i];
280 ctx->x = g_new0 (gdouble, filter->na);
281 ctx->y = g_new0 (gdouble, filter->nb);
283 filter->nchannels = channels;
289 static inline gdouble
290 process (GstAudioFXBaseIIRFilter * filter,
291 GstAudioFXBaseIIRFilterChannelCtx * ctx, gdouble x0)
293 gdouble val = filter->a[0] * x0;
296 for (i = 1, j = ctx->x_pos; i < filter->na; i++) {
297 val += filter->a[i] * ctx->x[j];
303 for (i = 1, j = ctx->y_pos; i < filter->nb; i++) {
304 val += filter->b[i] * ctx->y[j];
312 if (ctx->x_pos >= filter->na)
314 ctx->x[ctx->x_pos] = x0;
318 if (ctx->y_pos >= filter->nb)
321 ctx->y[ctx->y_pos] = val;
327 #define DEFINE_PROCESS_FUNC(width,ctype) \
329 process_##width (GstAudioFXBaseIIRFilter * filter, \
330 g##ctype * data, guint num_samples) \
332 gint i, j, channels = filter->nchannels; \
335 for (i = 0; i < num_samples / channels; i++) { \
336 for (j = 0; j < channels; j++) { \
337 val = process (filter, &filter->channels[j], *data); \
343 DEFINE_PROCESS_FUNC (32, float);
344 DEFINE_PROCESS_FUNC (64, double);
346 #undef DEFINE_PROCESS_FUNC
348 /* GstBaseTransform vmethod implementations */
350 gst_audio_fx_base_iir_filter_transform_ip (GstBaseTransform * base,
353 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (base);
355 GstClockTime timestamp, stream_time;
359 timestamp = GST_BUFFER_TIMESTAMP (buf);
361 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
363 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
364 GST_TIME_ARGS (timestamp));
366 if (GST_CLOCK_TIME_IS_VALID (stream_time))
367 gst_object_sync_values (G_OBJECT (filter), stream_time);
369 if (gst_base_transform_is_passthrough (base))
372 g_return_val_if_fail (filter->a != NULL, GST_FLOW_ERROR);
374 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
375 num_samples = size / GST_AUDIO_FILTER_BPS (filter);
377 filter->process (filter, data, num_samples);
379 gst_buffer_unmap (buf, data, size);
386 gst_audio_fx_base_iir_filter_stop (GstBaseTransform * base)
388 GstAudioFXBaseIIRFilter *filter = GST_AUDIO_FX_BASE_IIR_FILTER (base);
389 guint channels = filter->nchannels;
390 GstAudioFXBaseIIRFilterChannelCtx *ctx;
393 /* Reset the history of input and output values if
394 * already existing */
395 if (channels && filter->channels) {
396 for (i = 0; i < channels; i++) {
397 ctx = &filter->channels[i];
401 g_free (filter->channels);
403 filter->channels = NULL;
404 filter->nchannels = 0;