rename filter-caps to caps property
[platform/upstream/gstreamer.git] / gst / 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-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       const GstCaps *new_caps_val = gst_value_get_caps (value);
158       GstCaps *new_caps;
159       GstCaps *old_caps;
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       /* FIXME: Need to activate these caps on the pads */
172       break;
173     }
174     default:
175       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176       break;
177   }
178 }
179
180 static void
181 gst_capsfilter_get_property (GObject * object, guint prop_id, GValue * value,
182     GParamSpec * pspec)
183 {
184   GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
185
186   switch (prop_id) {
187     case PROP_FILTER_CAPS:
188       gst_value_set_caps (value, capsfilter->filter_caps);
189       break;
190     default:
191       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192       break;
193   }
194 }
195
196 static void
197 gst_capsfilter_dispose (GObject * object)
198 {
199   GstCapsFilter *filter = GST_CAPSFILTER (object);
200
201   gst_caps_replace (&filter->filter_caps, NULL);
202
203   G_OBJECT_CLASS (parent_class)->dispose (object);
204 }
205
206 static GstCaps *
207 gst_capsfilter_transform_caps (GstBaseTransform * base,
208     GstPadDirection direction, GstCaps * caps)
209 {
210   GstCapsFilter *capsfilter = GST_CAPSFILTER (base);
211   GstCaps *ret;
212
213   ret = gst_caps_intersect (caps, capsfilter->filter_caps);
214
215   return ret;
216 }
217
218 static GstFlowReturn
219 gst_capsfilter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
220 {
221   /* No actual work here. It's all done in the prepare output buffer
222    * func. */
223   return GST_FLOW_OK;
224 }
225
226 /* Output buffer preparation... if the buffer has no caps, and
227  * our allowed output caps is fixed, then give the caps to the
228  * buffer.
229  * This ensures that outgoing buffers have caps if we can, so 
230  * that pipelines like:
231  *   gst-launch filesrc location=rawsamples.raw ! 
232  *       audio/x-raw-int,width=16,depth=16,rate=48000,channels=2,
233  *       endianness=4321,signed='(boolean)'true ! alsasink
234  * will work.
235  */
236 static GstFlowReturn
237 gst_capsfilter_prepare_buf (GstBaseTransform * trans, GstBuffer * input,
238     gint size, GstCaps * caps, GstBuffer ** buf)
239 {
240   if (GST_BUFFER_CAPS (input) != NULL) {
241     /* Output buffer already has caps */
242     GST_DEBUG_OBJECT (trans, "Input buffer already has caps");
243     gst_buffer_ref (input);
244     *buf = input;
245   } else {
246     /* Buffer has no caps. See if the output pad only supports fixed caps */
247     GstCaps *out_caps;
248
249     if (GST_PAD_CAPS (trans->srcpad) != NULL) {
250       gst_caps_ref (GST_PAD_CAPS (trans->srcpad));
251       out_caps = GST_PAD_CAPS (trans->srcpad);
252     } else {
253       out_caps = gst_pad_get_allowed_caps (trans->srcpad);
254       g_return_val_if_fail (out_caps != NULL, GST_FLOW_ERROR);
255     }
256
257     if (gst_caps_is_fixed (out_caps) && !gst_caps_is_empty (out_caps)) {
258       GST_DEBUG_OBJECT (trans, "Have fixed output caps %"
259           GST_PTR_FORMAT " to apply to buffer with no caps", out_caps);
260       if (gst_buffer_is_writable (input)) {
261         gst_buffer_ref (input);
262         *buf = input;
263       } else {
264         GST_DEBUG_OBJECT (trans, "Creating sub-buffer and setting caps");
265         *buf = gst_buffer_create_sub (input, 0, GST_BUFFER_SIZE (input));
266       }
267       GST_BUFFER_CAPS (input) = out_caps;
268
269       if (GST_PAD_CAPS (trans->srcpad) == NULL)
270         gst_pad_set_caps (trans->srcpad, out_caps);
271     } else {
272       gst_caps_unref (out_caps);
273     }
274   }
275
276   return GST_FLOW_OK;
277 }