685e04dbcdeac862bf88b3ebb54e9d7303f9cf32
[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  * SECTION:element-capsfilter
24  *
25  * The element does not modify data as such, but can enforce limitations on the
26  * data format.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
31  * gst-launch videotestsrc ! video/x-raw-gray ! ffmpegcolorspace ! autovideosink
32  * ]| Limits acceptable video from videotestsrc to be grayscale.
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "../../gst/gst-i18n-lib.h"
41 #include "gstcapsfilter.h"
42
43 enum
44 {
45   PROP_0,
46   PROP_FILTER_CAPS
47 };
48
49
50 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
51     GST_PAD_SINK,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS_ANY);
54
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
56     GST_PAD_SRC,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS_ANY);
59
60
61 GST_DEBUG_CATEGORY_STATIC (gst_capsfilter_debug);
62 #define GST_CAT_DEFAULT gst_capsfilter_debug
63
64 #define _do_init \
65     GST_DEBUG_CATEGORY_INIT (gst_capsfilter_debug, "capsfilter", 0, \
66     "capsfilter element");
67 #define gst_capsfilter_parent_class parent_class
68 G_DEFINE_TYPE_WITH_CODE (GstCapsFilter, gst_capsfilter, GST_TYPE_BASE_TRANSFORM,
69     _do_init);
70
71
72 static void gst_capsfilter_set_property (GObject * object, guint prop_id,
73     const GValue * value, GParamSpec * pspec);
74 static void gst_capsfilter_get_property (GObject * object, guint prop_id,
75     GValue * value, GParamSpec * pspec);
76 static void gst_capsfilter_dispose (GObject * object);
77
78 static GstCaps *gst_capsfilter_transform_caps (GstBaseTransform * base,
79     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
80 static gboolean gst_capsfilter_accept_caps (GstBaseTransform * base,
81     GstPadDirection direction, GstCaps * caps);
82 static GstFlowReturn gst_capsfilter_transform_ip (GstBaseTransform * base,
83     GstBuffer * buf);
84 static GstFlowReturn gst_capsfilter_prepare_buf (GstBaseTransform * trans,
85     GstBuffer * input, GstBuffer ** buf);
86
87 static void
88 gst_capsfilter_class_init (GstCapsFilterClass * klass)
89 {
90   GObjectClass *gobject_class;
91   GstElementClass *gstelement_class;
92   GstBaseTransformClass *trans_class;
93
94   gobject_class = G_OBJECT_CLASS (klass);
95   gobject_class->set_property = gst_capsfilter_set_property;
96   gobject_class->get_property = gst_capsfilter_get_property;
97   gobject_class->dispose = gst_capsfilter_dispose;
98
99   g_object_class_install_property (gobject_class, PROP_FILTER_CAPS,
100       g_param_spec_boxed ("caps", _("Filter caps"),
101           _("Restrict the possible allowed capabilities (NULL means ANY). "
102               "Setting this property takes a reference to the supplied GstCaps "
103               "object."), GST_TYPE_CAPS,
104           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
105
106   gstelement_class = GST_ELEMENT_CLASS (klass);
107   gst_element_class_set_details_simple (gstelement_class,
108       "CapsFilter",
109       "Generic",
110       "Pass data without modification, limiting formats",
111       "David Schleef <ds@schleef.org>");
112   gst_element_class_add_pad_template (gstelement_class,
113       gst_static_pad_template_get (&srctemplate));
114   gst_element_class_add_pad_template (gstelement_class,
115       gst_static_pad_template_get (&sinktemplate));
116
117   trans_class = GST_BASE_TRANSFORM_CLASS (klass);
118   trans_class->transform_caps =
119       GST_DEBUG_FUNCPTR (gst_capsfilter_transform_caps);
120   trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_capsfilter_transform_ip);
121   trans_class->accept_caps = GST_DEBUG_FUNCPTR (gst_capsfilter_accept_caps);
122   trans_class->prepare_output_buffer =
123       GST_DEBUG_FUNCPTR (gst_capsfilter_prepare_buf);
124 }
125
126 static void
127 gst_capsfilter_init (GstCapsFilter * filter)
128 {
129   GstBaseTransform *trans = GST_BASE_TRANSFORM (filter);
130   gst_base_transform_set_gap_aware (trans, TRUE);
131   filter->filter_caps = gst_caps_new_any ();
132 }
133
134 static void
135 gst_capsfilter_set_property (GObject * object, guint prop_id,
136     const GValue * value, GParamSpec * pspec)
137 {
138   GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
139
140   switch (prop_id) {
141     case PROP_FILTER_CAPS:{
142       GstCaps *new_caps;
143       GstCaps *old_caps;
144       const GstCaps *new_caps_val = gst_value_get_caps (value);
145
146       if (new_caps_val == NULL) {
147         new_caps = gst_caps_new_any ();
148       } else {
149         new_caps = (GstCaps *) new_caps_val;
150         gst_caps_ref (new_caps);
151       }
152
153       GST_OBJECT_LOCK (capsfilter);
154       old_caps = capsfilter->filter_caps;
155       capsfilter->filter_caps = new_caps;
156       GST_OBJECT_UNLOCK (capsfilter);
157
158       gst_caps_unref (old_caps);
159
160       GST_DEBUG_OBJECT (capsfilter, "set new caps %" GST_PTR_FORMAT, new_caps);
161
162       gst_base_transform_reconfigure_sink (GST_BASE_TRANSFORM (object));
163       break;
164     }
165     default:
166       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167       break;
168   }
169 }
170
171 static void
172 gst_capsfilter_get_property (GObject * object, guint prop_id, GValue * value,
173     GParamSpec * pspec)
174 {
175   GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
176
177   switch (prop_id) {
178     case PROP_FILTER_CAPS:
179       GST_OBJECT_LOCK (capsfilter);
180       gst_value_set_caps (value, capsfilter->filter_caps);
181       GST_OBJECT_UNLOCK (capsfilter);
182       break;
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185       break;
186   }
187 }
188
189 static void
190 gst_capsfilter_dispose (GObject * object)
191 {
192   GstCapsFilter *filter = GST_CAPSFILTER (object);
193
194   gst_caps_replace (&filter->filter_caps, NULL);
195
196   G_OBJECT_CLASS (parent_class)->dispose (object);
197 }
198
199 static GstCaps *
200 gst_capsfilter_transform_caps (GstBaseTransform * base,
201     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
202 {
203   GstCapsFilter *capsfilter = GST_CAPSFILTER (base);
204   GstCaps *ret, *filter_caps, *tmp;
205
206   GST_OBJECT_LOCK (capsfilter);
207   filter_caps = gst_caps_ref (capsfilter->filter_caps);
208   GST_OBJECT_UNLOCK (capsfilter);
209
210   if (filter) {
211     tmp =
212         gst_caps_intersect_full (filter, filter_caps, GST_CAPS_INTERSECT_FIRST);
213     gst_caps_unref (filter_caps);
214     filter_caps = tmp;
215   }
216
217   ret = gst_caps_intersect_full (filter_caps, caps, GST_CAPS_INTERSECT_FIRST);
218
219   GST_DEBUG_OBJECT (capsfilter, "input:     %" GST_PTR_FORMAT, caps);
220   GST_DEBUG_OBJECT (capsfilter, "filter:    %" GST_PTR_FORMAT, filter);
221   GST_DEBUG_OBJECT (capsfilter, "caps filter:    %" GST_PTR_FORMAT,
222       filter_caps);
223   GST_DEBUG_OBJECT (capsfilter, "intersect: %" GST_PTR_FORMAT, ret);
224
225   gst_caps_unref (filter_caps);
226
227   return ret;
228 }
229
230 static gboolean
231 gst_capsfilter_accept_caps (GstBaseTransform * base,
232     GstPadDirection direction, GstCaps * caps)
233 {
234   GstCapsFilter *capsfilter = GST_CAPSFILTER (base);
235   GstCaps *filter_caps;
236   gboolean ret;
237
238   GST_OBJECT_LOCK (capsfilter);
239   filter_caps = gst_caps_ref (capsfilter->filter_caps);
240   GST_OBJECT_UNLOCK (capsfilter);
241
242   ret = gst_caps_can_intersect (caps, filter_caps);
243   GST_DEBUG_OBJECT (capsfilter, "can intersect: %d", ret);
244   if (ret) {
245     /* if we can intersect, see if the other end also accepts */
246     if (direction == GST_PAD_SRC)
247       ret =
248           gst_pad_peer_query_accept_caps (GST_BASE_TRANSFORM_SINK_PAD (base),
249           caps);
250     else
251       ret =
252           gst_pad_peer_query_accept_caps (GST_BASE_TRANSFORM_SRC_PAD (base),
253           caps);
254     GST_DEBUG_OBJECT (capsfilter, "peer accept: %d", ret);
255   }
256
257   gst_caps_unref (filter_caps);
258
259   return ret;
260 }
261
262 static GstFlowReturn
263 gst_capsfilter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
264 {
265   /* No actual work here. It's all done in the prepare output buffer
266    * func. */
267   return GST_FLOW_OK;
268 }
269
270 /* Output buffer preparation... if the buffer has no caps, and
271  * our allowed output caps is fixed, then give the caps to the
272  * buffer.
273  * This ensures that outgoing buffers have caps if we can, so
274  * that pipelines like:
275  *   gst-launch filesrc location=rawsamples.raw !
276  *       audio/x-raw-int,width=16,depth=16,rate=48000,channels=2,
277  *       endianness=4321,signed='(boolean)'true ! alsasink
278  * will work.
279  */
280 static GstFlowReturn
281 gst_capsfilter_prepare_buf (GstBaseTransform * trans, GstBuffer * input,
282     GstBuffer ** buf)
283 {
284   GstFlowReturn ret = GST_FLOW_OK;
285
286   /* always return the input as output buffer */
287   *buf = input;
288
289   if (!gst_pad_has_current_caps (trans->sinkpad)) {
290     /* Buffer has no caps. See if the output pad only supports fixed caps */
291     GstCaps *out_caps;
292
293     GST_LOG_OBJECT (trans, "Input pad does not have caps");
294
295     out_caps = gst_pad_get_current_caps (trans->srcpad);
296     if (out_caps == NULL) {
297       out_caps = gst_pad_get_allowed_caps (trans->srcpad);
298       g_return_val_if_fail (out_caps != NULL, GST_FLOW_ERROR);
299     }
300
301     out_caps = gst_caps_do_simplify (out_caps);
302
303     if (gst_caps_is_fixed (out_caps) && !gst_caps_is_empty (out_caps)) {
304       GST_DEBUG_OBJECT (trans, "Have fixed output caps %"
305           GST_PTR_FORMAT " to apply to srcpad", out_caps);
306
307       if (!gst_pad_has_current_caps (trans->srcpad))
308         gst_pad_push_event (trans->srcpad, gst_event_new_caps (out_caps));
309       gst_caps_unref (out_caps);
310     } else {
311       gchar *caps_str = gst_caps_to_string (out_caps);
312
313       GST_DEBUG_OBJECT (trans, "Cannot choose caps. Have unfixed output caps %"
314           GST_PTR_FORMAT, out_caps);
315       gst_caps_unref (out_caps);
316
317       ret = GST_FLOW_ERROR;
318       GST_ELEMENT_ERROR (trans, STREAM, FORMAT,
319           ("Filter caps do not completely specify the output format"),
320           ("Output caps are unfixed: %s", caps_str));
321       g_free (caps_str);
322     }
323   }
324
325   return ret;
326 }