audiofilter: fix get_unit_size
[platform/upstream/gstreamer.git] / gst-libs / gst / audio / gstaudiofilter.c
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:gstaudiofilter
24  * @short_description: Base class for simple audio filters
25  *
26  * #GstAudioFilter is a #GstBaseTransform<!-- -->-derived base class for simple audio
27  * filters, ie. those that output the same format that they get as input.
28  *
29  * #GstAudioFilter will parse the input format for you (with error checking)
30  * before calling your setup function. Also, elements deriving from
31  * #GstAudioFilter may use gst_audio_filter_class_add_pad_templates() from
32  * their class_init function to easily configure the set of caps/formats that
33  * the element is able to handle.
34  *
35  * Derived classes should override the #GstAudioFilterClass.setup() and
36  * #GstBaseTransformClass.transform_ip() and/or
37  * #GstBaseTransformClass.transform()
38  * virtual functions in their class_init function.
39  *
40  * Last reviewed on 2007-02-03 (0.10.11.1)
41  *
42  * Since: 0.10.12
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include "gstaudiofilter.h"
50
51 #include <string.h>
52
53 GST_DEBUG_CATEGORY_STATIC (audiofilter_dbg);
54 #define GST_CAT_DEFAULT audiofilter_dbg
55
56 static GstStateChangeReturn gst_audio_filter_change_state (GstElement * element,
57     GstStateChange transition);
58 static gboolean gst_audio_filter_set_caps (GstBaseTransform * btrans,
59     GstCaps * incaps, GstCaps * outcaps);
60 static gboolean gst_audio_filter_get_unit_size (GstBaseTransform * btrans,
61     GstCaps * caps, gsize * size);
62
63 #define do_init G_STMT_START { \
64     GST_DEBUG_CATEGORY_INIT (audiofilter_dbg, "audiofilter", 0, "audiofilter"); \
65 } G_STMT_END
66
67 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstAudioFilter, gst_audio_filter,
68     GST_TYPE_BASE_TRANSFORM, do_init);
69
70 static void
71 gst_audio_filter_class_init (GstAudioFilterClass * klass)
72 {
73   GstBaseTransformClass *basetrans_class = (GstBaseTransformClass *) klass;
74   GstElementClass *gstelement_class = (GstElementClass *) klass;
75
76   gstelement_class->change_state =
77       GST_DEBUG_FUNCPTR (gst_audio_filter_change_state);
78   basetrans_class->set_caps = GST_DEBUG_FUNCPTR (gst_audio_filter_set_caps);
79   basetrans_class->get_unit_size =
80       GST_DEBUG_FUNCPTR (gst_audio_filter_get_unit_size);
81 }
82
83 static void
84 gst_audio_filter_init (GstAudioFilter * self)
85 {
86   gst_audio_info_init (&self->info);
87 }
88
89 /* we override the state change vfunc here instead of GstBaseTransform's stop
90  * vfunc, so GstAudioFilter-derived elements can override ::stop() for their
91  * own purposes without having to worry about chaining up */
92 static GstStateChangeReturn
93 gst_audio_filter_change_state (GstElement * element, GstStateChange transition)
94 {
95   GstStateChangeReturn ret;
96   GstAudioFilter *filter = GST_AUDIO_FILTER (element);
97
98   ret =
99       GST_ELEMENT_CLASS (gst_audio_filter_parent_class)->change_state (element,
100       transition);
101   if (ret == GST_STATE_CHANGE_FAILURE)
102     return ret;
103
104   switch (transition) {
105     case GST_STATE_CHANGE_PAUSED_TO_READY:
106     case GST_STATE_CHANGE_READY_TO_NULL:
107       gst_audio_info_init (&filter->info);
108       break;
109     default:
110       break;
111   }
112
113   return ret;
114 }
115
116 static gboolean
117 gst_audio_filter_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
118     GstCaps * outcaps)
119 {
120   GstAudioFilterClass *klass;
121   GstAudioFilter *filter = GST_AUDIO_FILTER (btrans);
122   gboolean ret = TRUE;
123
124   GST_LOG_OBJECT (filter, "caps: %" GST_PTR_FORMAT, incaps);
125
126   if (!gst_audio_info_from_caps (&filter->info, incaps))
127     goto invalid_format;
128
129   klass = GST_AUDIO_FILTER_CLASS_CAST (G_OBJECT_GET_CLASS (filter));
130
131   if (klass->setup)
132     ret = klass->setup (filter, &filter->info);
133
134   return ret;
135
136   /* ERROR */
137 invalid_format:
138   {
139     GST_WARNING_OBJECT (filter, "couldn't parse %" GST_PTR_FORMAT, incaps);
140     return FALSE;
141   }
142 }
143
144 static gboolean
145 gst_audio_filter_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
146     gsize * size)
147 {
148   GstAudioInfo info;
149   gint width, channels;
150
151   if (!gst_audio_info_from_caps (&info, caps))
152     return FALSE;
153
154   width = GST_AUDIO_INFO_WIDTH (&info);
155   channels = GST_AUDIO_INFO_CHANNELS (&info);
156
157   *size = (width / 8) * channels;
158
159   return TRUE;
160 }
161
162 /**
163  * gst_audio_filter_class_add_pad_templates:
164  * @klass: an #GstAudioFilterClass
165  * @allowed_caps: what formats the filter can handle, as #GstCaps
166  *
167  * Convenience function to add pad templates to this element class, with
168  * @allowed_caps as the caps that can be handled.
169  *
170  * This function is usually used from within a GObject class_init function.
171  *
172  * Since: 0.10.12
173  */
174 void
175 gst_audio_filter_class_add_pad_templates (GstAudioFilterClass * klass,
176     GstCaps * allowed_caps)
177 {
178   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
179   GstPadTemplate *pad_template;
180
181   g_return_if_fail (GST_IS_AUDIO_FILTER_CLASS (klass));
182   g_return_if_fail (GST_IS_CAPS (allowed_caps));
183
184   pad_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
185       allowed_caps);
186   gst_element_class_add_pad_template (element_class, pad_template);
187
188   pad_template = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
189       allowed_caps);
190   gst_element_class_add_pad_template (element_class, pad_template);
191 }