2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2005 Wim Taymans <wim@fluendo.com>
5 * 2005 David Schleef <ds@schleef.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include "../../gst/gst-i18n-lib.h"
29 #include <gst/base/gstbasetransform.h>
32 static GstElementDetails gst_capsfilter_details =
33 GST_ELEMENT_DETAILS ("CapsFilter",
35 "Pass data without modification, limiting formats",
36 "David Schleef <ds@schleef.org>");
39 #define GST_TYPE_CAPSFILTER \
40 (gst_capsfilter_get_type())
41 #define GST_CAPSFILTER(obj) \
42 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_CAPSFILTER,GstCapsFilter))
43 #define GST_CAPSFILTER_CLASS(klass) \
44 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_CAPSFILTER,GstCapsFilterClass))
45 #define GST_IS_CAPSFILTER(obj) \
46 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_CAPSFILTER))
47 #define GST_IS_CAPSFILTER_CLASS(obj) \
48 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_CAPSFILTER))
50 typedef struct _GstCapsFilter GstCapsFilter;
51 typedef struct _GstCapsFilterClass GstCapsFilterClass;
55 GstBaseTransform trans;
60 struct _GstCapsFilterClass
62 GstBaseTransformClass trans_class;
65 GType gst_capsfilter_get_type (void);
75 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
80 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
86 GST_DEBUG_CATEGORY_STATIC (gst_capsfilter_debug);
87 #define GST_CAT_DEFAULT gst_capsfilter_debug
89 #define _do_init(bla) \
90 GST_DEBUG_CATEGORY_INIT (gst_capsfilter_debug, "capsfilter", 0, \
91 "capsfilter element");
93 GST_BOILERPLATE_FULL (GstCapsFilter, gst_capsfilter, GstBaseTransform,
94 GST_TYPE_BASE_TRANSFORM, _do_init);
97 static void gst_capsfilter_set_property (GObject * object, guint prop_id,
98 const GValue * value, GParamSpec * pspec);
99 static void gst_capsfilter_get_property (GObject * object, guint prop_id,
100 GValue * value, GParamSpec * pspec);
101 static void gst_capsfilter_dispose (GObject * object);
102 static GstCaps *gst_capsfilter_transform_caps (GstBaseTransform * base,
103 GstPadDirection direction, GstCaps * caps);
104 static GstFlowReturn gst_capsfilter_transform_ip (GstBaseTransform * base,
106 static GstFlowReturn gst_capsfilter_prepare_buf (GstBaseTransform * trans,
107 GstBuffer * input, gint size, GstCaps * caps, GstBuffer ** buf);
110 gst_capsfilter_base_init (gpointer g_class)
112 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
114 gst_element_class_add_pad_template (element_class,
115 gst_static_pad_template_get (&srctemplate));
116 gst_element_class_add_pad_template (element_class,
117 gst_static_pad_template_get (&sinktemplate));
118 gst_element_class_set_details (element_class, &gst_capsfilter_details);
122 gst_capsfilter_class_init (GstCapsFilterClass * klass)
124 GObjectClass *gobject_class;
125 GstBaseTransformClass *trans_class;
127 gobject_class = (GObjectClass *) klass;
128 gobject_class->set_property = gst_capsfilter_set_property;
129 gobject_class->get_property = gst_capsfilter_get_property;
130 gobject_class->dispose = gst_capsfilter_dispose;
132 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILTER_CAPS,
133 g_param_spec_boxed ("caps", _("Filter caps"),
134 _("Restrict the possible allowed capabilities (NULL means ANY)"),
135 GST_TYPE_CAPS, G_PARAM_READWRITE));
137 trans_class = (GstBaseTransformClass *) klass;
138 trans_class->transform_caps = gst_capsfilter_transform_caps;
139 trans_class->transform_ip = gst_capsfilter_transform_ip;
140 trans_class->prepare_output_buffer = gst_capsfilter_prepare_buf;
144 gst_capsfilter_init (GstCapsFilter * filter, GstCapsFilterClass * g_class)
146 filter->filter_caps = gst_caps_new_any ();
150 gst_capsfilter_set_property (GObject * object, guint prop_id,
151 const GValue * value, GParamSpec * pspec)
153 GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
156 case PROP_FILTER_CAPS:{
159 const GstCaps *new_caps_val = gst_value_get_caps (value);
161 if (new_caps_val == NULL) {
162 new_caps = gst_caps_new_any ();
164 new_caps = gst_caps_copy (new_caps_val);
167 old_caps = capsfilter->filter_caps;
168 capsfilter->filter_caps = new_caps;
169 gst_caps_unref (old_caps);
171 GST_DEBUG_OBJECT (capsfilter, "set new caps %" GST_PTR_FORMAT, new_caps);
173 /* FIXME: Need to activate these caps on the pads */
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 gst_capsfilter_get_property (GObject * object, guint prop_id, GValue * value,
186 GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
189 case PROP_FILTER_CAPS:
190 gst_value_set_caps (value, capsfilter->filter_caps);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 gst_capsfilter_dispose (GObject * object)
201 GstCapsFilter *filter = GST_CAPSFILTER (object);
203 gst_caps_replace (&filter->filter_caps, NULL);
205 G_OBJECT_CLASS (parent_class)->dispose (object);
209 gst_capsfilter_transform_caps (GstBaseTransform * base,
210 GstPadDirection direction, GstCaps * caps)
212 GstCapsFilter *capsfilter = GST_CAPSFILTER (base);
215 ret = gst_caps_intersect (caps, capsfilter->filter_caps);
221 gst_capsfilter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
223 /* No actual work here. It's all done in the prepare output buffer
228 /* Output buffer preparation... if the buffer has no caps, and
229 * our allowed output caps is fixed, then give the caps to the
231 * This ensures that outgoing buffers have caps if we can, so
232 * that pipelines like:
233 * gst-launch filesrc location=rawsamples.raw !
234 * audio/x-raw-int,width=16,depth=16,rate=48000,channels=2,
235 * endianness=4321,signed='(boolean)'true ! alsasink
239 gst_capsfilter_prepare_buf (GstBaseTransform * trans, GstBuffer * input,
240 gint size, GstCaps * caps, GstBuffer ** buf)
242 if (GST_BUFFER_CAPS (input) != NULL) {
243 /* Output buffer already has caps */
244 GST_DEBUG_OBJECT (trans, "Input buffer already has caps");
245 gst_buffer_ref (input);
248 /* Buffer has no caps. See if the output pad only supports fixed caps */
251 out_caps = GST_PAD_CAPS (trans->srcpad);
253 if (out_caps != NULL) {
254 gst_caps_ref (out_caps);
256 out_caps = gst_pad_get_allowed_caps (trans->srcpad);
257 g_return_val_if_fail (out_caps != NULL, GST_FLOW_ERROR);
260 if (gst_caps_is_fixed (out_caps) && !gst_caps_is_empty (out_caps)) {
261 GST_DEBUG_OBJECT (trans, "Have fixed output caps %"
262 GST_PTR_FORMAT " to apply to buffer with no caps", out_caps);
263 if (gst_buffer_is_writable (input)) {
264 gst_buffer_ref (input);
267 GST_DEBUG_OBJECT (trans, "Creating sub-buffer and setting caps");
268 *buf = gst_buffer_create_sub (input, 0, GST_BUFFER_SIZE (input));
270 GST_BUFFER_CAPS (*buf) = out_caps;
272 if (GST_PAD_CAPS (trans->srcpad) == NULL)
273 gst_pad_set_caps (trans->srcpad, out_caps);
275 gst_caps_unref (out_caps);