Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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 base_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, guint * 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   /* FIXME: Ref the GstRingerBuffer class to get it's debug category
83    * initialized. gst_ring_buffer_parse_caps () which we use later
84    * uses this debug category.
85    */
86   g_type_class_ref (GST_TYPE_RING_BUFFER);
87 }
88
89 static void
90 gst_audio_filter_init (GstAudioFilter * self)
91 {
92   /* nothing to do here */
93 }
94
95 /* we override the state change vfunc here instead of GstBaseTransform's stop
96  * vfunc, so GstAudioFilter-derived elements can override ::stop() for their
97  * own purposes without having to worry about chaining up */
98 static GstStateChangeReturn
99 gst_audio_filter_change_state (GstElement * element, GstStateChange transition)
100 {
101   GstStateChangeReturn ret;
102   GstAudioFilter *filter = GST_AUDIO_FILTER (element);
103
104   switch (transition) {
105     case GST_STATE_CHANGE_NULL_TO_READY:
106       memset (&filter->format, 0, sizeof (GstRingBufferSpec));
107       /* to make gst_buffer_spec_parse_caps() happy */
108       filter->format.latency_time = GST_SECOND;
109       break;
110     default:
111       break;
112   }
113
114   ret =
115       GST_ELEMENT_CLASS (gst_audio_filter_parent_class)->change_state (element,
116       transition);
117   if (ret == GST_STATE_CHANGE_FAILURE)
118     return ret;
119
120   switch (transition) {
121     case GST_STATE_CHANGE_PAUSED_TO_READY:
122     case GST_STATE_CHANGE_READY_TO_NULL:
123       gst_caps_replace (&filter->format.caps, NULL);
124       break;
125     default:
126       break;
127   }
128
129   return ret;
130 }
131
132 static gboolean
133 gst_audio_filter_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
134     GstCaps * outcaps)
135 {
136   GstAudioFilterClass *klass;
137   GstAudioFilter *filter = GST_AUDIO_FILTER (btrans);
138   gboolean ret = TRUE;
139
140   GST_LOG_OBJECT (filter, "caps: %" GST_PTR_FORMAT, incaps);
141
142   if (!gst_ring_buffer_parse_caps (&filter->format, incaps)) {
143     GST_WARNING_OBJECT (filter, "couldn't parse %" GST_PTR_FORMAT, incaps);
144     return FALSE;
145   }
146
147   klass = GST_AUDIO_FILTER_CLASS_CAST (G_OBJECT_GET_CLASS (filter));
148
149   if (klass->setup)
150     ret = klass->setup (filter, &filter->format);
151
152   return ret;
153 }
154
155 static gboolean
156 gst_audio_filter_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
157     guint * size)
158 {
159   GstStructure *structure;
160   gboolean ret = TRUE;
161   gint width, channels;
162
163   structure = gst_caps_get_structure (caps, 0);
164
165   ret &= gst_structure_get_int (structure, "width", &width);
166   ret &= gst_structure_get_int (structure, "channels", &channels);
167
168   if (ret)
169     *size = (width / 8) * channels;
170
171   return ret;
172 }
173
174 /**
175  * gst_audio_filter_class_add_pad_templates:
176  * @klass: an #GstAudioFilterClass
177  * @allowed_caps: what formats the filter can handle, as #GstCaps
178  *
179  * Convenience function to add pad templates to this element class, with
180  * @allowed_caps as the caps that can be handled.
181  *
182  * This function is usually used from within a GObject base_init function.
183  *
184  * Since: 0.10.12
185  */
186 void
187 gst_audio_filter_class_add_pad_templates (GstAudioFilterClass * klass,
188     const GstCaps * allowed_caps)
189 {
190   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
191   GstPadTemplate *pad_template;
192
193   g_return_if_fail (GST_IS_AUDIO_FILTER_CLASS (klass));
194   g_return_if_fail (GST_IS_CAPS (allowed_caps));
195
196   pad_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
197       gst_caps_copy (allowed_caps));
198   gst_element_class_add_pad_template (element_class, pad_template);
199   gst_object_unref (pad_template);
200
201   pad_template = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
202       gst_caps_copy (allowed_caps));
203   gst_element_class_add_pad_template (element_class, pad_template);
204   gst_object_unref (pad_template);
205 }