Merge branch 'master' into 0.11
[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 gboolean
135 copy_func (GQuark field_id, const GValue * value, GstStructure * dest)
136 {
137   gst_structure_id_set_value (dest, field_id, value);
138
139   return TRUE;
140 }
141
142 static void
143 gst_capsfilter_set_property (GObject * object, guint prop_id,
144     const GValue * value, GParamSpec * pspec)
145 {
146   GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
147
148   switch (prop_id) {
149     case PROP_FILTER_CAPS:{
150       GstCaps *new_caps;
151       GstCaps *old_caps, *suggest, *nego;
152       const GstCaps *new_caps_val = gst_value_get_caps (value);
153
154       if (new_caps_val == NULL) {
155         new_caps = gst_caps_new_any ();
156       } else {
157         new_caps = (GstCaps *) new_caps_val;
158         gst_caps_ref (new_caps);
159       }
160
161       GST_OBJECT_LOCK (capsfilter);
162       old_caps = capsfilter->filter_caps;
163       capsfilter->filter_caps = new_caps;
164       GST_OBJECT_UNLOCK (capsfilter);
165
166       gst_caps_unref (old_caps);
167
168       GST_DEBUG_OBJECT (capsfilter, "set new caps %" GST_PTR_FORMAT, new_caps);
169
170       /* filter the currently negotiated format against the new caps */
171       nego = gst_pad_get_current_caps (GST_BASE_TRANSFORM_SINK_PAD (object));
172       if (nego) {
173         GST_DEBUG_OBJECT (capsfilter, "we had negotiated caps %" GST_PTR_FORMAT,
174             nego);
175
176         if (G_UNLIKELY (gst_caps_is_any (new_caps))) {
177           GST_DEBUG_OBJECT (capsfilter, "not settings any suggestion");
178
179           suggest = NULL;
180         } else {
181           GstStructure *s1, *s2;
182
183           /* first check if the name is the same */
184           s1 = gst_caps_get_structure (nego, 0);
185           s2 = gst_caps_get_structure (new_caps, 0);
186
187           if (gst_structure_get_name_id (s1) == gst_structure_get_name_id (s2)) {
188             /* same name, copy all fields from the new caps into the previously
189              * negotiated caps */
190             suggest = gst_caps_copy (nego);
191             s1 = gst_caps_get_structure (suggest, 0);
192             gst_structure_foreach (s2, (GstStructureForeachFunc) copy_func, s1);
193             GST_DEBUG_OBJECT (capsfilter, "copied structure fields");
194           } else {
195             GST_DEBUG_OBJECT (capsfilter, "different structure names");
196             /* different names, we can only suggest the complete caps */
197             suggest = gst_caps_copy (new_caps);
198           }
199         }
200         gst_caps_unref (nego);
201       } else {
202         GST_DEBUG_OBJECT (capsfilter, "no negotiated caps");
203         /* Suggest the new caps, we can't just rely on _get_caps as this may
204          * already be called at this point even though no buffer has been
205          * pushed yet */
206         suggest = gst_caps_copy (new_caps);
207       }
208
209       GST_DEBUG_OBJECT (capsfilter, "suggesting new caps %" GST_PTR_FORMAT,
210           suggest);
211       gst_base_transform_suggest (GST_BASE_TRANSFORM (object), suggest, 0);
212       if (suggest)
213         gst_caps_unref (suggest);
214
215       break;
216     }
217     default:
218       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219       break;
220   }
221 }
222
223 static void
224 gst_capsfilter_get_property (GObject * object, guint prop_id, GValue * value,
225     GParamSpec * pspec)
226 {
227   GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
228
229   switch (prop_id) {
230     case PROP_FILTER_CAPS:
231       GST_OBJECT_LOCK (capsfilter);
232       gst_value_set_caps (value, capsfilter->filter_caps);
233       GST_OBJECT_UNLOCK (capsfilter);
234       break;
235     default:
236       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237       break;
238   }
239 }
240
241 static void
242 gst_capsfilter_dispose (GObject * object)
243 {
244   GstCapsFilter *filter = GST_CAPSFILTER (object);
245
246   gst_caps_replace (&filter->filter_caps, NULL);
247
248   G_OBJECT_CLASS (parent_class)->dispose (object);
249 }
250
251 static GstCaps *
252 gst_capsfilter_transform_caps (GstBaseTransform * base,
253     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
254 {
255   GstCapsFilter *capsfilter = GST_CAPSFILTER (base);
256   GstCaps *ret, *filter_caps, *tmp;
257
258   GST_OBJECT_LOCK (capsfilter);
259   filter_caps = gst_caps_ref (capsfilter->filter_caps);
260   GST_OBJECT_UNLOCK (capsfilter);
261
262   if (filter) {
263     tmp =
264         gst_caps_intersect_full (filter, filter_caps, GST_CAPS_INTERSECT_FIRST);
265     gst_caps_unref (filter_caps);
266     filter_caps = tmp;
267   }
268
269   ret = gst_caps_intersect_full (filter_caps, caps, GST_CAPS_INTERSECT_FIRST);
270
271   GST_DEBUG_OBJECT (capsfilter, "input:     %" GST_PTR_FORMAT, caps);
272   GST_DEBUG_OBJECT (capsfilter, "filter:    %" GST_PTR_FORMAT, filter);
273   GST_DEBUG_OBJECT (capsfilter, "caps filter:    %" GST_PTR_FORMAT,
274       filter_caps);
275   GST_DEBUG_OBJECT (capsfilter, "intersect: %" GST_PTR_FORMAT, ret);
276
277   gst_caps_unref (filter_caps);
278
279   return ret;
280 }
281
282 static gboolean
283 gst_capsfilter_accept_caps (GstBaseTransform * base,
284     GstPadDirection direction, GstCaps * caps)
285 {
286   GstCapsFilter *capsfilter = GST_CAPSFILTER (base);
287   GstCaps *filter_caps;
288   gboolean ret;
289
290   GST_OBJECT_LOCK (capsfilter);
291   filter_caps = gst_caps_ref (capsfilter->filter_caps);
292   GST_OBJECT_UNLOCK (capsfilter);
293
294   ret = gst_caps_can_intersect (caps, filter_caps);
295   GST_DEBUG_OBJECT (capsfilter, "can intersect: %d", ret);
296   if (ret) {
297     /* if we can intersect, see if the other end also accepts */
298     if (direction == GST_PAD_SRC)
299       ret = gst_pad_peer_accept_caps (GST_BASE_TRANSFORM_SINK_PAD (base), caps);
300     else
301       ret = gst_pad_peer_accept_caps (GST_BASE_TRANSFORM_SRC_PAD (base), caps);
302     GST_DEBUG_OBJECT (capsfilter, "peer accept: %d", ret);
303   }
304
305   gst_caps_unref (filter_caps);
306
307   return ret;
308 }
309
310 static GstFlowReturn
311 gst_capsfilter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
312 {
313   /* No actual work here. It's all done in the prepare output buffer
314    * func. */
315   return GST_FLOW_OK;
316 }
317
318 /* Output buffer preparation... if the buffer has no caps, and
319  * our allowed output caps is fixed, then give the caps to the
320  * buffer.
321  * This ensures that outgoing buffers have caps if we can, so
322  * that pipelines like:
323  *   gst-launch filesrc location=rawsamples.raw !
324  *       audio/x-raw-int,width=16,depth=16,rate=48000,channels=2,
325  *       endianness=4321,signed='(boolean)'true ! alsasink
326  * will work.
327  */
328 static GstFlowReturn
329 gst_capsfilter_prepare_buf (GstBaseTransform * trans, GstBuffer * input,
330     GstBuffer ** buf)
331 {
332   GstFlowReturn ret = GST_FLOW_OK;
333
334   /* always return the input as output buffer */
335   *buf = input;
336
337   if (!gst_pad_has_current_caps (trans->sinkpad)) {
338     /* Buffer has no caps. See if the output pad only supports fixed caps */
339     GstCaps *out_caps;
340
341     GST_LOG_OBJECT (trans, "Input pad does not have caps");
342
343     out_caps = gst_pad_get_current_caps (trans->srcpad);
344     if (out_caps == NULL) {
345       out_caps = gst_pad_get_allowed_caps (trans->srcpad);
346       g_return_val_if_fail (out_caps != NULL, GST_FLOW_ERROR);
347     }
348
349     out_caps = gst_caps_make_writable (out_caps);
350     gst_caps_do_simplify (out_caps);
351
352     if (gst_caps_is_fixed (out_caps) && !gst_caps_is_empty (out_caps)) {
353       GST_DEBUG_OBJECT (trans, "Have fixed output caps %"
354           GST_PTR_FORMAT " to apply to srcpad", out_caps);
355
356       if (!gst_pad_has_current_caps (trans->srcpad))
357         gst_pad_push_event (trans->srcpad, gst_event_new_caps (out_caps));
358       gst_caps_unref (out_caps);
359     } else {
360       gchar *caps_str = gst_caps_to_string (out_caps);
361
362       GST_DEBUG_OBJECT (trans, "Cannot choose caps. Have unfixed output caps %"
363           GST_PTR_FORMAT, out_caps);
364       gst_caps_unref (out_caps);
365
366       ret = GST_FLOW_ERROR;
367       GST_ELEMENT_ERROR (trans, STREAM, FORMAT,
368           ("Filter caps do not completely specify the output format"),
369           ("Output caps are unfixed: %s", caps_str));
370       g_free (caps_str);
371     }
372   }
373
374   return ret;
375 }