From be2cd1e919950ea2101030e71beeca26bd173e1e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 8 Aug 2007 20:47:33 +0000 Subject: [PATCH] gst/filter/: Use GstAudioFilter as base class and don't leak the memory of the filter kernel and residue. Original commit message from CVS: * gst/filter/Makefile.am: * gst/filter/gstbpwsinc.c: (gst_bpwsinc_dispose), (gst_bpwsinc_base_init), (gst_bpwsinc_class_init), (gst_bpwsinc_init), (bpwsinc_setup): * gst/filter/gstbpwsinc.h: * gst/filter/gstlpwsinc.c: (gst_lpwsinc_dispose), (gst_lpwsinc_base_init), (gst_lpwsinc_class_init), (gst_lpwsinc_init), (lpwsinc_setup): * gst/filter/gstlpwsinc.h: Use GstAudioFilter as base class and don't leak the memory of the filter kernel and residue. --- gst/audiofx/audiowsincband.c | 75 +++++++++++++++++++++++-------------------- gst/audiofx/audiowsincband.h | 6 ++-- gst/audiofx/audiowsinclimit.c | 75 +++++++++++++++++++++++-------------------- gst/audiofx/audiowsinclimit.h | 6 ++-- 4 files changed, 86 insertions(+), 76 deletions(-) diff --git a/gst/audiofx/audiowsincband.c b/gst/audiofx/audiowsincband.c index 8ba87fd..7b881ae 100644 --- a/gst/audiofx/audiowsincband.c +++ b/gst/audiofx/audiowsincband.c @@ -27,7 +27,6 @@ * * FIXME: * - this filter is totally unoptimized ! - * - we do not destroy the allocated memory for filters and residue * - this might be improved upon with bytestream */ @@ -38,7 +37,7 @@ #include #include #include -#include +#include #include #include "gstbpwsinc.h" @@ -69,31 +68,18 @@ enum PROP_UPPER_FREQUENCY }; -static GstStaticPadTemplate bpwsinc_sink_template = -GST_STATIC_PAD_TEMPLATE ("sink", - GST_PAD_SINK, - GST_PAD_ALWAYS, - GST_STATIC_CAPS ("audio/x-raw-float, " - "rate = (int) [ 1, MAX ], " - "channels = (int) [ 1, MAX ], " - "endianness = (int) BYTE_ORDER, " "width = (int) 32") - ); - -static GstStaticPadTemplate bpwsinc_src_template = GST_STATIC_PAD_TEMPLATE - ("src", - GST_PAD_SRC, - GST_PAD_ALWAYS, - GST_STATIC_CAPS ("audio/x-raw-float, " - "rate = (int) [ 1, MAX ], " - "channels = (int) [ 1, MAX ], " - "endianness = (int) BYTE_ORDER, " "width = (int) 32") - ); +#define ALLOWED_CAPS \ + "audio/x-raw-float," \ + " width = (int) 32, " \ + " endianness = (int) BYTE_ORDER," \ + " rate = (int) [ 1, MAX ]," \ + " channels = (int) [ 1, MAX ]" #define DEBUG_INIT(bla) \ GST_DEBUG_CATEGORY_INIT (gst_bpwsinc_debug, "bpwsinc", 0, "Band-pass Windowed sinc filter plugin"); -GST_BOILERPLATE_FULL (GstBPWSinc, gst_bpwsinc, GstBaseTransform, - GST_TYPE_BASE_TRANSFORM, DEBUG_INIT); +GST_BOILERPLATE_FULL (GstBPWSinc, gst_bpwsinc, GstAudioFilter, + GST_TYPE_AUDIO_FILTER, DEBUG_INIT); static void bpwsinc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec); @@ -102,14 +88,26 @@ static void bpwsinc_get_property (GObject * object, guint prop_id, static GstFlowReturn bpwsinc_transform_ip (GstBaseTransform * base, GstBuffer * outbuf); -static gboolean bpwsinc_set_caps (GstBaseTransform * base, GstCaps * incaps, - GstCaps * outcaps); +static gboolean bpwsinc_setup (GstAudioFilter * base, + GstRingBufferSpec * format); /* Element class */ static void gst_bpwsinc_dispose (GObject * object) { + GstBPWSinc *this = GST_BPWSINC (object); + + if (this->residue) { + g_free (this->residue); + this->residue = NULL; + } + + if (this->kernel) { + g_free (this->kernel); + this->kernel = NULL; + } + G_OBJECT_CLASS (parent_class)->dispose (object); } @@ -117,12 +115,14 @@ static void gst_bpwsinc_base_init (gpointer g_class) { GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); + GstCaps *caps; - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&bpwsinc_src_template)); - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&bpwsinc_sink_template)); gst_element_class_set_details (element_class, &bpwsinc_details); + + caps = gst_caps_from_string (ALLOWED_CAPS); + gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (g_class), + caps); + gst_caps_unref (caps); } static void @@ -152,7 +152,7 @@ gst_bpwsinc_class_init (GstBPWSincClass * klass) 1, G_MAXINT, 1, G_PARAM_READWRITE)); trans_class->transform_ip = GST_DEBUG_FUNCPTR (bpwsinc_transform_ip); - trans_class->set_caps = GST_DEBUG_FUNCPTR (bpwsinc_set_caps); + GST_AUDIO_FILTER_CLASS (klass)->setup = GST_DEBUG_FUNCPTR (bpwsinc_setup); } static void @@ -162,14 +162,15 @@ gst_bpwsinc_init (GstBPWSinc * this, GstBPWSincClass * g_class) this->lower_frequency = 0.25; this->upper_frequency = 0.3; this->kernel = NULL; + this->residue = NULL; } -/* GstBaseTransform vmethod implementations */ +/* GstAudioFilter vmethod implementations */ /* get notified of caps and plug in the correct process function */ static gboolean -bpwsinc_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps) +bpwsinc_setup (GstAudioFilter * base, GstRingBufferSpec * format) { int i = 0; double sum = 0.0; @@ -177,9 +178,6 @@ bpwsinc_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps) double *kernel_lp, *kernel_hp; GstBPWSinc *this = GST_BPWSINC (base); - GST_DEBUG_OBJECT (this, - "set_caps: in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps, outcaps); - len = this->wing_size; /* fill the lp kernel * FIXME: refactor to own function, this is not caps related @@ -230,6 +228,8 @@ bpwsinc_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps) kernel_hp[i] /= sum; /* combine the two thiss */ + if (this->kernel) + g_free (this->kernel); this->kernel = (double *) g_malloc (sizeof (double) * (2 * len + 1)); for (i = 0; i <= len * 2; ++i) @@ -245,6 +245,9 @@ bpwsinc_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps) g_free (kernel_hp); /* set up the residue memory space */ + if (this->residue) + g_free (this->residue); + this->residue = (gfloat *) g_malloc (sizeof (gfloat) * (len * 2 + 1)); for (i = 0; i <= len * 2; ++i) this->residue[i] = 0.0; @@ -252,6 +255,8 @@ bpwsinc_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps) return TRUE; } +/* GstBaseTransform vmethod implementations */ + static GstFlowReturn bpwsinc_transform_ip (GstBaseTransform * base, GstBuffer * outbuf) { diff --git a/gst/audiofx/audiowsincband.h b/gst/audiofx/audiowsincband.h index 1d825de..af5938d 100644 --- a/gst/audiofx/audiowsincband.h +++ b/gst/audiofx/audiowsincband.h @@ -36,7 +36,7 @@ #include "gstfilter.h" #include -#include +#include G_BEGIN_DECLS @@ -60,7 +60,7 @@ typedef struct _GstBPWSincClass GstBPWSincClass; * Opaque data structure. */ struct _GstBPWSinc { - GstBaseTransform element; + GstAudioFilter element; double frequency; double lower_frequency, upper_frequency; @@ -72,7 +72,7 @@ struct _GstBPWSinc { }; struct _GstBPWSincClass { - GstBaseTransformClass parent_class; + GstAudioFilterClass parent_class; }; G_END_DECLS diff --git a/gst/audiofx/audiowsinclimit.c b/gst/audiofx/audiowsinclimit.c index c182a62..98a2e4d 100644 --- a/gst/audiofx/audiowsinclimit.c +++ b/gst/audiofx/audiowsinclimit.c @@ -27,7 +27,6 @@ * * FIXME: * - this filter is totally unoptimized ! - * - we do not destroy the allocated memory for filters and residue * - this might be improved upon with bytestream */ @@ -38,7 +37,7 @@ #include #include #include -#include +#include #include #include "gstlpwsinc.h" @@ -68,31 +67,18 @@ enum PROP_FREQUENCY }; -static GstStaticPadTemplate lpwsinc_sink_template = -GST_STATIC_PAD_TEMPLATE ("sink", - GST_PAD_SINK, - GST_PAD_ALWAYS, - GST_STATIC_CAPS ("audio/x-raw-float, " - "rate = (int) [ 1, MAX ], " - "channels = (int) [ 1, MAX ], " - "endianness = (int) BYTE_ORDER, " "width = (int) 32") - ); - -static GstStaticPadTemplate lpwsinc_src_template = GST_STATIC_PAD_TEMPLATE - ("src", - GST_PAD_SRC, - GST_PAD_ALWAYS, - GST_STATIC_CAPS ("audio/x-raw-float, " - "rate = (int) [ 1, MAX ], " - "channels = (int) [ 1, MAX ], " - "endianness = (int) BYTE_ORDER, " "width = (int) 32") - ); +#define ALLOWED_CAPS \ + "audio/x-raw-float," \ + " width = (int) 32, " \ + " endianness = (int) BYTE_ORDER," \ + " rate = (int) [ 1, MAX ]," \ + " channels = (int) [ 1, MAX ]" #define DEBUG_INIT(bla) \ GST_DEBUG_CATEGORY_INIT (gst_lpwsinc_debug, "lpwsinc", 0, "Low-pass Windowed sinc filter plugin"); -GST_BOILERPLATE_FULL (GstLPWSinc, gst_lpwsinc, GstBaseTransform, - GST_TYPE_BASE_TRANSFORM, DEBUG_INIT); +GST_BOILERPLATE_FULL (GstLPWSinc, gst_lpwsinc, GstAudioFilter, + GST_TYPE_AUDIO_FILTER, DEBUG_INIT); static void lpwsinc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec); @@ -101,14 +87,26 @@ static void lpwsinc_get_property (GObject * object, guint prop_id, static GstFlowReturn lpwsinc_transform_ip (GstBaseTransform * base, GstBuffer * outbuf); -static gboolean lpwsinc_set_caps (GstBaseTransform * base, GstCaps * incaps, - GstCaps * outcaps); +static gboolean lpwsinc_setup (GstAudioFilter * base, + GstRingBufferSpec * format); /* Element class */ static void gst_lpwsinc_dispose (GObject * object) { + GstLPWSinc *this = GST_LPWSINC (object); + + if (this->residue) { + g_free (this->residue); + this->residue = NULL; + } + + if (this->kernel) { + g_free (this->kernel); + this->kernel = NULL; + } + G_OBJECT_CLASS (parent_class)->dispose (object); } @@ -116,12 +114,14 @@ static void gst_lpwsinc_base_init (gpointer g_class) { GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); + GstCaps *caps; - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&lpwsinc_src_template)); - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&lpwsinc_sink_template)); gst_element_class_set_details (element_class, &lpwsinc_details); + + caps = gst_caps_from_string (ALLOWED_CAPS); + gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (g_class), + caps); + gst_caps_unref (caps); } static void @@ -148,7 +148,7 @@ gst_lpwsinc_class_init (GstLPWSincClass * klass) 1, G_MAXINT, 1, G_PARAM_READWRITE)); trans_class->transform_ip = GST_DEBUG_FUNCPTR (lpwsinc_transform_ip); - trans_class->set_caps = GST_DEBUG_FUNCPTR (lpwsinc_set_caps); + GST_AUDIO_FILTER_CLASS (klass)->setup = GST_DEBUG_FUNCPTR (lpwsinc_setup); } static void @@ -157,28 +157,29 @@ gst_lpwsinc_init (GstLPWSinc * this, GstLPWSincClass * g_class) this->wing_size = 50; this->frequency = 0.25; this->kernel = NULL; + this->residue = NULL; } -/* GstBaseTransform vmethod implementations */ +/* GstAudioFilter vmethod implementations */ /* get notified of caps and plug in the correct process function */ static gboolean -lpwsinc_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps) +lpwsinc_setup (GstAudioFilter * base, GstRingBufferSpec * format) { int i = 0; double sum = 0.0; int len = 0; GstLPWSinc *this = GST_LPWSINC (base); - GST_DEBUG_OBJECT (this, - "set_caps: in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps, outcaps); - /* FIXME: remember to free it */ /* fill the kernel */ g_print ("DEBUG: initing filter kernel\n"); len = this->wing_size; GST_DEBUG ("lpwsinc: initializing filter kernel of length %d", len * 2 + 1); + + if (this->kernel) + g_free (this->kernel); this->kernel = (double *) g_malloc (sizeof (double) * (2 * len + 1)); for (i = 0; i <= len * 2; ++i) { @@ -199,6 +200,8 @@ lpwsinc_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps) this->kernel[i] /= sum; /* set up the residue memory space */ + if (this->residue) + g_free (this->residue); this->residue = (gfloat *) g_malloc (sizeof (gfloat) * (len * 2 + 1)); for (i = 0; i <= len * 2; ++i) this->residue[i] = 0.0; @@ -206,6 +209,8 @@ lpwsinc_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps) return TRUE; } +/* GstBaseTransform vmethod implementations */ + static GstFlowReturn lpwsinc_transform_ip (GstBaseTransform * base, GstBuffer * outbuf) { diff --git a/gst/audiofx/audiowsinclimit.h b/gst/audiofx/audiowsinclimit.h index 58d5ec9..14791b5 100644 --- a/gst/audiofx/audiowsinclimit.h +++ b/gst/audiofx/audiowsinclimit.h @@ -36,7 +36,7 @@ #include "gstfilter.h" #include -#include +#include G_BEGIN_DECLS @@ -60,7 +60,7 @@ typedef struct _GstLPWSincClass GstLPWSincClass; * Opaque data structure. */ struct _GstLPWSinc { - GstBaseTransform element; + GstAudioFilter element; void (*process)(GstLPWSinc*, gpointer, gint); @@ -73,7 +73,7 @@ struct _GstLPWSinc { }; struct _GstLPWSincClass { - GstBaseTransformClass parent_class; + GstAudioFilterClass parent_class; }; G_END_DECLS -- 2.7.4