audioiirfilter: Fix possible NULL pointer dereference
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audioiirfilter.c
1 /* 
2  * GStreamer
3  * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
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.
9  *
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.
14  *
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.
19  * 
20  */
21
22 /**
23  * SECTION:element-audioiirfilter
24  *
25  * audioiirfilter implements a generic audio <ulink url="http://en.wikipedia.org/wiki/Infinite_impulse_response">IIR filter</ulink>. Before usage the
26  * "a" and "b" properties have to be set to the filter coefficients that
27  * should be used.
28  *
29  * The filter coefficients describe the numerator and denominator of the
30  * transfer function.
31  *
32  * To change the filter coefficients whenever the sampling rate changes the
33  * "rate-changed" signal can be used. This should be done for most
34  * IIR filters as they're depending on the sampling rate.
35  *
36  * <refsect2>
37  * <title>Example application</title>
38  * |[
39  * <xi:include xmlns:xi="http://www.w3.org/2003/XInclude" parse="text" href="../../../../tests/examples/audiofx/iirfilter-example.c" />
40  * ]|
41  * </refsect2>
42  */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <string.h>
49 #include <math.h>
50 #include <gst/gst.h>
51 #include <gst/audio/gstaudiofilter.h>
52 #include <gst/controller/gstcontroller.h>
53
54 #include "audioiirfilter.h"
55
56 #define GST_CAT_DEFAULT gst_audio_iir_filter_debug
57 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
58
59 enum
60 {
61   SIGNAL_RATE_CHANGED,
62   LAST_SIGNAL
63 };
64
65 enum
66 {
67   PROP_0,
68   PROP_A,
69   PROP_B
70 };
71
72 static guint gst_audio_iir_filter_signals[LAST_SIGNAL] = { 0, };
73
74 #define DEBUG_INIT(bla) \
75   GST_DEBUG_CATEGORY_INIT (gst_audio_iir_filter_debug, "audioiirfilter", 0, \
76       "Generic audio IIR filter plugin");
77
78 GST_BOILERPLATE_FULL (GstAudioIIRFilter, gst_audio_iir_filter, GstAudioFilter,
79     GST_TYPE_AUDIO_FX_BASE_IIR_FILTER, DEBUG_INIT);
80
81 static void gst_audio_iir_filter_set_property (GObject * object, guint prop_id,
82     const GValue * value, GParamSpec * pspec);
83 static void gst_audio_iir_filter_get_property (GObject * object, guint prop_id,
84     GValue * value, GParamSpec * pspec);
85 static void gst_audio_iir_filter_finalize (GObject * object);
86
87 static gboolean gst_audio_iir_filter_setup (GstAudioFilter * base,
88     GstRingBufferSpec * format);
89
90 /* Element class */
91 static void
92 gst_audio_iir_filter_base_init (gpointer g_class)
93 {
94   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
95
96   gst_element_class_set_details_simple (element_class,
97       "Audio IIR filter", "Filter/Effect/Audio",
98       "Generic audio IIR filter with custom filter kernel",
99       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
100 }
101
102 static void
103 gst_audio_iir_filter_class_init (GstAudioIIRFilterClass * klass)
104 {
105   GObjectClass *gobject_class = (GObjectClass *) klass;
106   GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
107
108   gobject_class->set_property = gst_audio_iir_filter_set_property;
109   gobject_class->get_property = gst_audio_iir_filter_get_property;
110   gobject_class->finalize = gst_audio_iir_filter_finalize;
111
112   g_object_class_install_property (gobject_class, PROP_A,
113       g_param_spec_value_array ("a", "A",
114           "Filter coefficients (numerator of transfer function)",
115           g_param_spec_double ("Coefficient", "Filter Coefficient",
116               "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
117               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
118           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
119   g_object_class_install_property (gobject_class, PROP_B,
120       g_param_spec_value_array ("b", "B",
121           "Filter coefficients (denominator of transfer function)",
122           g_param_spec_double ("Coefficient", "Filter Coefficient",
123               "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
124               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
125           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126
127   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_iir_filter_setup);
128
129   /**
130    * GstAudioIIRFilter::rate-changed:
131    * @filter: the filter on which the signal is emitted
132    * @rate: the new sampling rate
133    *
134    * Will be emitted when the sampling rate changes. The callbacks
135    * will be called from the streaming thread and processing will
136    * stop until the event is handled.
137    */
138   gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED] =
139       g_signal_new ("rate-changed", G_TYPE_FROM_CLASS (klass),
140       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAudioIIRFilterClass, rate_changed),
141       NULL, NULL, gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
142 }
143
144 static void
145 gst_audio_iir_filter_update_coefficients (GstAudioIIRFilter * self,
146     GValueArray * va, GValueArray * vb)
147 {
148   gdouble *a = NULL, *b = NULL;
149   guint i;
150
151   if (va) {
152     if (self->a)
153       g_value_array_free (self->a);
154
155     self->a = va;
156   }
157   if (vb) {
158     if (self->b)
159       g_value_array_free (self->b);
160
161     self->b = vb;
162   }
163
164   if (self->a && self->a->n_values > 0) {
165     a = g_new (gdouble, self->a->n_values);
166
167     for (i = 0; i < self->a->n_values; i++) {
168       GValue *v = g_value_array_get_nth (self->a, i);
169       a[i] = g_value_get_double (v);
170     }
171   }
172
173   if (self->b && self->b->n_values > 0) {
174     b = g_new (gdouble, self->b->n_values);
175     for (i = 0; i < self->b->n_values; i++) {
176       GValue *v = g_value_array_get_nth (self->b, i);
177       b[i] = g_value_get_double (v);
178     }
179   }
180
181   gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
182       (self), a, (self->a) ? self->a->n_values : 0, b,
183       (self->b) ? self->b->n_values : 0);
184 }
185
186 static void
187 gst_audio_iir_filter_init (GstAudioIIRFilter * self,
188     GstAudioIIRFilterClass * g_class)
189 {
190   GValue v = { 0, };
191   GValueArray *a, *b;
192
193   a = g_value_array_new (1);
194
195   g_value_init (&v, G_TYPE_DOUBLE);
196   g_value_set_double (&v, 1.0);
197   g_value_array_append (a, &v);
198   g_value_unset (&v);
199
200   b = NULL;
201   gst_audio_iir_filter_update_coefficients (self, a, b);
202
203   self->lock = g_mutex_new ();
204 }
205
206 /* GstAudioFilter vmethod implementations */
207
208 /* get notified of caps and plug in the correct process function */
209 static gboolean
210 gst_audio_iir_filter_setup (GstAudioFilter * base, GstRingBufferSpec * format)
211 {
212   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (base);
213
214   if (self->rate != format->rate) {
215     g_signal_emit (G_OBJECT (self),
216         gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED], 0, format->rate);
217     self->rate = format->rate;
218   }
219
220   return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, format);
221 }
222
223 static void
224 gst_audio_iir_filter_finalize (GObject * object)
225 {
226   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
227
228   g_mutex_free (self->lock);
229   self->lock = NULL;
230
231   if (self->a)
232     g_value_array_free (self->a);
233   self->a = NULL;
234   if (self->b)
235     g_value_array_free (self->b);
236   self->b = NULL;
237
238   G_OBJECT_CLASS (parent_class)->finalize (object);
239 }
240
241 static void
242 gst_audio_iir_filter_set_property (GObject * object, guint prop_id,
243     const GValue * value, GParamSpec * pspec)
244 {
245   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
246
247   g_return_if_fail (GST_IS_AUDIO_IIR_FILTER (self));
248
249   switch (prop_id) {
250     case PROP_A:
251       g_mutex_lock (self->lock);
252       gst_audio_iir_filter_update_coefficients (self, g_value_dup_boxed (value),
253           NULL);
254       g_mutex_unlock (self->lock);
255       break;
256     case PROP_B:
257       g_mutex_lock (self->lock);
258       gst_audio_iir_filter_update_coefficients (self, NULL,
259           g_value_dup_boxed (value));
260       g_mutex_unlock (self->lock);
261       break;
262     default:
263       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264       break;
265   }
266 }
267
268 static void
269 gst_audio_iir_filter_get_property (GObject * object, guint prop_id,
270     GValue * value, GParamSpec * pspec)
271 {
272   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
273
274   switch (prop_id) {
275     case PROP_A:
276       g_value_set_boxed (value, self->a);
277       break;
278     case PROP_B:
279       g_value_set_boxed (value, self->b);
280       break;
281     default:
282       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283       break;
284   }
285 }