1 /* GStreamer audio filter base class
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2003> David Schleef <ds@schleef.org>
4  * Copyright (C) <2007> Tim-Philipp Müller <tim centricular net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 /**
23  * SECTION:gstaudiofilter
24  * @title: GstAudioFilter
25  * @short_description: Base class for simple audio filters
26  *
27  * #GstAudioFilter is a #GstBaseTransform<!-- -->-derived base class for simple audio
28  * filters, ie. those that output the same format that they get as input.
29  *
30  * #GstAudioFilter will parse the input format for you (with error checking)
31  * before calling your setup function. Also, elements deriving from
32  * #GstAudioFilter may use gst_audio_filter_class_add_pad_templates() from
33  * their class_init function to easily configure the set of caps/formats that
34  * the element is able to handle.
35  *
36  * Derived classes should override the #GstAudioFilterClass.setup() and
37  * #GstBaseTransformClass.transform_ip() and/or
38  * #GstBaseTransformClass.transform()
39  * virtual functions in their class_init function.
40  */
41 
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45 
46 #include "gstaudiofilter.h"
47 
48 #include <string.h>
49 
50 GST_DEBUG_CATEGORY_STATIC (audiofilter_dbg);
51 #define GST_CAT_DEFAULT audiofilter_dbg
52 
53 static GstStateChangeReturn gst_audio_filter_change_state (GstElement * element,
54     GstStateChange transition);
55 static gboolean gst_audio_filter_set_caps (GstBaseTransform * btrans,
56     GstCaps * incaps, GstCaps * outcaps);
57 static gboolean gst_audio_filter_get_unit_size (GstBaseTransform * btrans,
58     GstCaps * caps, gsize * size);
59 static GstFlowReturn gst_audio_filter_submit_input_buffer (GstBaseTransform *
60     btrans, gboolean is_discont, GstBuffer * input);
61 
62 #define do_init G_STMT_START { \
63     GST_DEBUG_CATEGORY_INIT (audiofilter_dbg, "audiofilter", 0, "audiofilter"); \
64 } G_STMT_END
65 
66 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstAudioFilter, gst_audio_filter,
67     GST_TYPE_BASE_TRANSFORM, do_init);
68 
69 static gboolean
gst_audio_filter_transform_meta(GstBaseTransform * trans,GstBuffer * inbuf,GstMeta * meta,GstBuffer * outbuf)70 gst_audio_filter_transform_meta (GstBaseTransform * trans, GstBuffer * inbuf,
71     GstMeta * meta, GstBuffer * outbuf)
72 {
73   const GstMetaInfo *info = meta->info;
74   const gchar *const *tags;
75 
76   tags = gst_meta_api_type_get_tags (info->api);
77 
78   if (!tags || (g_strv_length ((gchar **) tags) == 1
79           && gst_meta_api_type_has_tag (info->api,
80               g_quark_from_string (GST_META_TAG_AUDIO_STR))))
81     return TRUE;
82 
83   return
84       GST_BASE_TRANSFORM_CLASS (gst_audio_filter_parent_class)->transform_meta
85       (trans, inbuf, meta, outbuf);
86 }
87 
88 static void
gst_audio_filter_class_init(GstAudioFilterClass * klass)89 gst_audio_filter_class_init (GstAudioFilterClass * klass)
90 {
91   GstBaseTransformClass *basetrans_class = (GstBaseTransformClass *) klass;
92   GstElementClass *gstelement_class = (GstElementClass *) klass;
93 
94   gstelement_class->change_state =
95       GST_DEBUG_FUNCPTR (gst_audio_filter_change_state);
96   basetrans_class->set_caps = GST_DEBUG_FUNCPTR (gst_audio_filter_set_caps);
97   basetrans_class->get_unit_size =
98       GST_DEBUG_FUNCPTR (gst_audio_filter_get_unit_size);
99   basetrans_class->transform_meta = gst_audio_filter_transform_meta;
100   basetrans_class->submit_input_buffer = gst_audio_filter_submit_input_buffer;
101 }
102 
103 static void
gst_audio_filter_init(GstAudioFilter * self)104 gst_audio_filter_init (GstAudioFilter * self)
105 {
106   gst_audio_info_init (&self->info);
107 }
108 
109 /* we override the state change vfunc here instead of GstBaseTransform's stop
110  * vfunc, so GstAudioFilter-derived elements can override ::stop() for their
111  * own purposes without having to worry about chaining up */
112 static GstStateChangeReturn
gst_audio_filter_change_state(GstElement * element,GstStateChange transition)113 gst_audio_filter_change_state (GstElement * element, GstStateChange transition)
114 {
115   GstStateChangeReturn ret;
116   GstAudioFilter *filter = GST_AUDIO_FILTER (element);
117 
118   ret =
119       GST_ELEMENT_CLASS (gst_audio_filter_parent_class)->change_state (element,
120       transition);
121   if (ret == GST_STATE_CHANGE_FAILURE)
122     return ret;
123 
124   switch (transition) {
125     case GST_STATE_CHANGE_PAUSED_TO_READY:
126     case GST_STATE_CHANGE_READY_TO_NULL:
127       gst_audio_info_init (&filter->info);
128       break;
129     default:
130       break;
131   }
132 
133   return ret;
134 }
135 
136 static gboolean
gst_audio_filter_set_caps(GstBaseTransform * btrans,GstCaps * incaps,GstCaps * outcaps)137 gst_audio_filter_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
138     GstCaps * outcaps)
139 {
140   GstAudioFilterClass *klass;
141   GstAudioFilter *filter = GST_AUDIO_FILTER (btrans);
142   GstAudioInfo info;
143   gboolean ret = TRUE;
144 
145   GST_LOG_OBJECT (filter, "caps: %" GST_PTR_FORMAT, incaps);
146   GST_LOG_OBJECT (filter, "info: %d", GST_AUDIO_FILTER_RATE (filter));
147 
148   if (!gst_audio_info_from_caps (&info, incaps))
149     goto invalid_format;
150 
151   klass = GST_AUDIO_FILTER_GET_CLASS (filter);
152 
153   if (klass->setup)
154     ret = klass->setup (filter, &info);
155 
156   if (ret) {
157     filter->info = info;
158     GST_LOG_OBJECT (filter, "configured caps: %" GST_PTR_FORMAT, incaps);
159   }
160 
161   return ret;
162 
163   /* ERROR */
164 invalid_format:
165   {
166     GST_WARNING_OBJECT (filter, "couldn't parse %" GST_PTR_FORMAT, incaps);
167     return FALSE;
168   }
169 }
170 
171 static GstFlowReturn
gst_audio_filter_submit_input_buffer(GstBaseTransform * btrans,gboolean is_discont,GstBuffer * input)172 gst_audio_filter_submit_input_buffer (GstBaseTransform * btrans,
173     gboolean is_discont, GstBuffer * input)
174 {
175   GstAudioFilter *filter = GST_AUDIO_FILTER (btrans);
176 
177   if (btrans->segment.format == GST_FORMAT_TIME) {
178     input =
179         gst_audio_buffer_clip (input, &btrans->segment, filter->info.rate,
180         filter->info.bpf);
181 
182     if (!input)
183       return GST_FLOW_OK;
184   }
185 
186   return
187       GST_BASE_TRANSFORM_CLASS
188       (gst_audio_filter_parent_class)->submit_input_buffer (btrans, is_discont,
189       input);
190 }
191 
192 static gboolean
gst_audio_filter_get_unit_size(GstBaseTransform * btrans,GstCaps * caps,gsize * size)193 gst_audio_filter_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
194     gsize * size)
195 {
196   GstAudioInfo info;
197 
198   if (!gst_audio_info_from_caps (&info, caps))
199     return FALSE;
200 
201   *size = GST_AUDIO_INFO_BPF (&info);
202 
203   return TRUE;
204 }
205 
206 /**
207  * gst_audio_filter_class_add_pad_templates:
208  * @klass: an #GstAudioFilterClass
209  * @allowed_caps: what formats the filter can handle, as #GstCaps
210  *
211  * Convenience function to add pad templates to this element class, with
212  * @allowed_caps as the caps that can be handled.
213  *
214  * This function is usually used from within a GObject class_init function.
215  */
216 void
gst_audio_filter_class_add_pad_templates(GstAudioFilterClass * klass,GstCaps * allowed_caps)217 gst_audio_filter_class_add_pad_templates (GstAudioFilterClass * klass,
218     GstCaps * allowed_caps)
219 {
220   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
221   GstPadTemplate *pad_template;
222 
223   g_return_if_fail (GST_IS_AUDIO_FILTER_CLASS (klass));
224   g_return_if_fail (GST_IS_CAPS (allowed_caps));
225 
226   pad_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
227       allowed_caps);
228   gst_element_class_add_pad_template (element_class, pad_template);
229 
230   pad_template = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
231       allowed_caps);
232   gst_element_class_add_pad_template (element_class, pad_template);
233 }
234