do CVS surgery and related build fixery to move elements and indexers in a new gstrea...
[platform/upstream/gstreamer.git] / plugins / elements / gstcapsfilter.c
1 /* GStreamer
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>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "../../gst/gst-i18n-lib.h"
28 #include <gst/gst.h>
29 #include <gst/base/gstbasetransform.h>
30
31
32 static GstElementDetails gst_capsfilter_details =
33 GST_ELEMENT_DETAILS ("CapsFilter",
34     "Generic",
35     "Pass data without modification, limiting formats",
36     "David Schleef <ds@schleef.org>");
37
38
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))
49
50 typedef struct _GstCapsFilter GstCapsFilter;
51 typedef struct _GstCapsFilterClass GstCapsFilterClass;
52
53 struct _GstCapsFilter
54 {
55   GstBaseTransform trans;
56
57   GstCaps *filter_caps;
58 };
59
60 struct _GstCapsFilterClass
61 {
62   GstBaseTransformClass trans_class;
63 };
64
65 GType gst_capsfilter_get_type (void);
66
67
68 enum
69 {
70   PROP_0,
71   PROP_FILTER_CAPS
72 };
73
74
75 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
76     GST_PAD_SINK,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS_ANY);
79
80 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
81     GST_PAD_SRC,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS_ANY);
84
85
86 GST_DEBUG_CATEGORY_STATIC (gst_capsfilter_debug);
87 #define GST_CAT_DEFAULT gst_capsfilter_debug
88
89 #define _do_init(bla) \
90     GST_DEBUG_CATEGORY_INIT (gst_capsfilter_debug, "capsfilter", 0, \
91     "capsfilter element");
92
93 GST_BOILERPLATE_FULL (GstCapsFilter, gst_capsfilter, GstBaseTransform,
94     GST_TYPE_BASE_TRANSFORM, _do_init);
95
96
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,
105     GstBuffer * buf);
106 static GstFlowReturn gst_capsfilter_prepare_buf (GstBaseTransform * trans,
107     GstBuffer * input, gint size, GstCaps * caps, GstBuffer ** buf);
108
109 static void
110 gst_capsfilter_base_init (gpointer g_class)
111 {
112   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
113
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);
119 }
120
121 static void
122 gst_capsfilter_class_init (GstCapsFilterClass * klass)
123 {
124   GObjectClass *gobject_class;
125   GstBaseTransformClass *trans_class;
126
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;
131
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));
136
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;
141 }
142
143 static void
144 gst_capsfilter_init (GstCapsFilter * filter, GstCapsFilterClass * g_class)
145 {
146   filter->filter_caps = gst_caps_new_any ();
147 }
148
149 static void
150 gst_capsfilter_set_property (GObject * object, guint prop_id,
151     const GValue * value, GParamSpec * pspec)
152 {
153   GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
154
155   switch (prop_id) {
156     case PROP_FILTER_CAPS:{
157       GstCaps *new_caps;
158       GstCaps *old_caps;
159       const GstCaps *new_caps_val = gst_value_get_caps (value);
160
161       if (new_caps_val == NULL) {
162         new_caps = gst_caps_new_any ();
163       } else {
164         new_caps = gst_caps_copy (new_caps_val);
165       }
166
167       old_caps = capsfilter->filter_caps;
168       capsfilter->filter_caps = new_caps;
169       gst_caps_unref (old_caps);
170
171       GST_DEBUG_OBJECT (capsfilter, "set new caps %" GST_PTR_FORMAT, new_caps);
172
173       /* FIXME: Need to activate these caps on the pads */
174       break;
175     }
176     default:
177       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178       break;
179   }
180 }
181
182 static void
183 gst_capsfilter_get_property (GObject * object, guint prop_id, GValue * value,
184     GParamSpec * pspec)
185 {
186   GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
187
188   switch (prop_id) {
189     case PROP_FILTER_CAPS:
190       gst_value_set_caps (value, capsfilter->filter_caps);
191       break;
192     default:
193       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194       break;
195   }
196 }
197
198 static void
199 gst_capsfilter_dispose (GObject * object)
200 {
201   GstCapsFilter *filter = GST_CAPSFILTER (object);
202
203   gst_caps_replace (&filter->filter_caps, NULL);
204
205   G_OBJECT_CLASS (parent_class)->dispose (object);
206 }
207
208 static GstCaps *
209 gst_capsfilter_transform_caps (GstBaseTransform * base,
210     GstPadDirection direction, GstCaps * caps)
211 {
212   GstCapsFilter *capsfilter = GST_CAPSFILTER (base);
213   GstCaps *ret;
214
215   ret = gst_caps_intersect (caps, capsfilter->filter_caps);
216
217   return ret;
218 }
219
220 static GstFlowReturn
221 gst_capsfilter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
222 {
223   /* No actual work here. It's all done in the prepare output buffer
224    * func. */
225   return GST_FLOW_OK;
226 }
227
228 /* Output buffer preparation... if the buffer has no caps, and
229  * our allowed output caps is fixed, then give the caps to the
230  * buffer.
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
236  * will work.
237  */
238 static GstFlowReturn
239 gst_capsfilter_prepare_buf (GstBaseTransform * trans, GstBuffer * input,
240     gint size, GstCaps * caps, GstBuffer ** buf)
241 {
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);
246     *buf = input;
247   } else {
248     /* Buffer has no caps. See if the output pad only supports fixed caps */
249     GstCaps *out_caps;
250
251     out_caps = GST_PAD_CAPS (trans->srcpad);
252
253     if (out_caps != NULL) {
254       gst_caps_ref (out_caps);
255     } else {
256       out_caps = gst_pad_get_allowed_caps (trans->srcpad);
257       g_return_val_if_fail (out_caps != NULL, GST_FLOW_ERROR);
258     }
259
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);
265         *buf = input;
266       } else {
267         GST_DEBUG_OBJECT (trans, "Creating sub-buffer and setting caps");
268         *buf = gst_buffer_create_sub (input, 0, GST_BUFFER_SIZE (input));
269       }
270       GST_BUFFER_CAPS (*buf) = out_caps;
271
272       if (GST_PAD_CAPS (trans->srcpad) == NULL)
273         gst_pad_set_caps (trans->srcpad, out_caps);
274     } else {
275       gst_caps_unref (out_caps);
276     }
277   }
278
279   return GST_FLOW_OK;
280 }