elements: use new gst_element_class_add_static_pad_template()
[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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, 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-1.0 videotestsrc ! capsfilter caps=video/x-raw,format=GRAY8 ! videoconvert ! autovideosink
32  * ]| Limits acceptable video from videotestsrc to be grayscale. Equivalent to
33  * |[
34  * gst-launch-1.0 videotestsrc ! video/x-raw,format=GRAY8 ! videoconvert ! autovideosink
35  * ]| which is a short notation for the capsfilter element.
36  * </refsect2>
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include "../../gst/gst-i18n-lib.h"
44 #include "gstcapsfilter.h"
45
46 enum
47 {
48   PROP_0,
49   PROP_FILTER_CAPS,
50   PROP_CAPS_CHANGE_MODE
51 };
52
53 #define DEFAULT_CAPS_CHANGE_MODE (GST_CAPS_FILTER_CAPS_CHANGE_MODE_IMMEDIATE)
54
55 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
56     GST_PAD_SINK,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS_ANY);
59
60 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61     GST_PAD_SRC,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS_ANY);
64
65
66 GST_DEBUG_CATEGORY_STATIC (gst_capsfilter_debug);
67 #define GST_CAT_DEFAULT gst_capsfilter_debug
68
69 /* TODO: Add a drop-buffers mode */
70 #define GST_TYPE_CAPS_FILTER_CAPS_CHANGE_MODE (gst_caps_filter_caps_change_mode_get_type())
71 static GType
72 gst_caps_filter_caps_change_mode_get_type (void)
73 {
74   static GType type = 0;
75   static const GEnumValue data[] = {
76     {GST_CAPS_FILTER_CAPS_CHANGE_MODE_IMMEDIATE,
77         "Only accept the current filter caps", "immediate"},
78     {GST_CAPS_FILTER_CAPS_CHANGE_MODE_DELAYED,
79         "Temporarily accept previous filter caps", "delayed"},
80     {0, NULL, NULL},
81   };
82
83   if (!type) {
84     type = g_enum_register_static ("GstCapsFilterCapsChangeMode", data);
85   }
86   return type;
87 }
88
89 #define _do_init \
90     GST_DEBUG_CATEGORY_INIT (gst_capsfilter_debug, "capsfilter", 0, \
91     "capsfilter element");
92 #define gst_capsfilter_parent_class parent_class
93 G_DEFINE_TYPE_WITH_CODE (GstCapsFilter, gst_capsfilter, GST_TYPE_BASE_TRANSFORM,
94     _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
103 static GstCaps *gst_capsfilter_transform_caps (GstBaseTransform * base,
104     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
105 static gboolean gst_capsfilter_accept_caps (GstBaseTransform * base,
106     GstPadDirection direction, GstCaps * caps);
107 static GstFlowReturn gst_capsfilter_transform_ip (GstBaseTransform * base,
108     GstBuffer * buf);
109 static GstFlowReturn gst_capsfilter_prepare_buf (GstBaseTransform * trans,
110     GstBuffer * input, GstBuffer ** buf);
111 static gboolean gst_capsfilter_sink_event (GstBaseTransform * trans,
112     GstEvent * event);
113 static gboolean gst_capsfilter_stop (GstBaseTransform * trans);
114
115 static void
116 gst_capsfilter_class_init (GstCapsFilterClass * klass)
117 {
118   GObjectClass *gobject_class;
119   GstElementClass *gstelement_class;
120   GstBaseTransformClass *trans_class;
121
122   gobject_class = G_OBJECT_CLASS (klass);
123   gobject_class->set_property = gst_capsfilter_set_property;
124   gobject_class->get_property = gst_capsfilter_get_property;
125   gobject_class->dispose = gst_capsfilter_dispose;
126
127   g_object_class_install_property (gobject_class, PROP_FILTER_CAPS,
128       g_param_spec_boxed ("caps", _("Filter caps"),
129           _("Restrict the possible allowed capabilities (NULL means ANY). "
130               "Setting this property takes a reference to the supplied GstCaps "
131               "object."), GST_TYPE_CAPS,
132           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
133           G_PARAM_STATIC_STRINGS));
134
135   g_object_class_install_property (gobject_class, PROP_CAPS_CHANGE_MODE,
136       g_param_spec_enum ("caps-change-mode", _("Caps Change Mode"),
137           _("Filter caps change behaviour"),
138           GST_TYPE_CAPS_FILTER_CAPS_CHANGE_MODE, DEFAULT_CAPS_CHANGE_MODE,
139           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
140           G_PARAM_STATIC_STRINGS));
141
142   gstelement_class = GST_ELEMENT_CLASS (klass);
143   gst_element_class_set_static_metadata (gstelement_class,
144       "CapsFilter",
145       "Generic",
146       "Pass data without modification, limiting formats",
147       "David Schleef <ds@schleef.org>");
148   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
149   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
150
151   trans_class = GST_BASE_TRANSFORM_CLASS (klass);
152   trans_class->transform_caps =
153       GST_DEBUG_FUNCPTR (gst_capsfilter_transform_caps);
154   trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_capsfilter_transform_ip);
155   trans_class->accept_caps = GST_DEBUG_FUNCPTR (gst_capsfilter_accept_caps);
156   trans_class->prepare_output_buffer =
157       GST_DEBUG_FUNCPTR (gst_capsfilter_prepare_buf);
158   trans_class->sink_event = GST_DEBUG_FUNCPTR (gst_capsfilter_sink_event);
159   trans_class->stop = GST_DEBUG_FUNCPTR (gst_capsfilter_stop);
160 }
161
162 static void
163 gst_capsfilter_init (GstCapsFilter * filter)
164 {
165   GstBaseTransform *trans = GST_BASE_TRANSFORM (filter);
166   gst_base_transform_set_gap_aware (trans, TRUE);
167   gst_base_transform_set_prefer_passthrough (trans, FALSE);
168   filter->filter_caps = gst_caps_new_any ();
169   filter->filter_caps_used = FALSE;
170   filter->caps_change_mode = DEFAULT_CAPS_CHANGE_MODE;
171 }
172
173 static void
174 gst_capsfilter_set_property (GObject * object, guint prop_id,
175     const GValue * value, GParamSpec * pspec)
176 {
177   GstCapsFilter *capsfilter = GST_CAPS_FILTER (object);
178
179   switch (prop_id) {
180     case PROP_FILTER_CAPS:{
181       GstCaps *new_caps;
182       GstCaps *old_caps;
183       const GstCaps *new_caps_val = gst_value_get_caps (value);
184
185       if (new_caps_val == NULL) {
186         new_caps = gst_caps_new_any ();
187       } else {
188         new_caps = (GstCaps *) new_caps_val;
189         gst_caps_ref (new_caps);
190       }
191
192       GST_OBJECT_LOCK (capsfilter);
193       old_caps = capsfilter->filter_caps;
194       capsfilter->filter_caps = new_caps;
195       if (old_caps && capsfilter->filter_caps_used &&
196           capsfilter->caps_change_mode ==
197           GST_CAPS_FILTER_CAPS_CHANGE_MODE_DELAYED) {
198         capsfilter->previous_caps =
199             g_list_prepend (capsfilter->previous_caps, gst_caps_ref (old_caps));
200       } else if (capsfilter->caps_change_mode !=
201           GST_CAPS_FILTER_CAPS_CHANGE_MODE_DELAYED) {
202         g_list_free_full (capsfilter->previous_caps,
203             (GDestroyNotify) gst_caps_unref);
204         capsfilter->previous_caps = NULL;
205       }
206       capsfilter->filter_caps_used = FALSE;
207       GST_OBJECT_UNLOCK (capsfilter);
208
209       gst_caps_unref (old_caps);
210
211       GST_DEBUG_OBJECT (capsfilter, "set new caps %" GST_PTR_FORMAT, new_caps);
212
213       gst_base_transform_reconfigure_sink (GST_BASE_TRANSFORM (object));
214       break;
215     }
216     case PROP_CAPS_CHANGE_MODE:{
217       GstCapsFilterCapsChangeMode old_change_mode;
218
219       GST_OBJECT_LOCK (capsfilter);
220       old_change_mode = capsfilter->caps_change_mode;
221       capsfilter->caps_change_mode = g_value_get_enum (value);
222
223       if (capsfilter->caps_change_mode != old_change_mode) {
224         g_list_free_full (capsfilter->previous_caps,
225             (GDestroyNotify) gst_caps_unref);
226         capsfilter->previous_caps = NULL;
227       }
228       GST_OBJECT_UNLOCK (capsfilter);
229       break;
230     }
231     default:
232       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233       break;
234   }
235 }
236
237 static void
238 gst_capsfilter_get_property (GObject * object, guint prop_id, GValue * value,
239     GParamSpec * pspec)
240 {
241   GstCapsFilter *capsfilter = GST_CAPS_FILTER (object);
242
243   switch (prop_id) {
244     case PROP_FILTER_CAPS:
245       GST_OBJECT_LOCK (capsfilter);
246       gst_value_set_caps (value, capsfilter->filter_caps);
247       GST_OBJECT_UNLOCK (capsfilter);
248       break;
249     case PROP_CAPS_CHANGE_MODE:
250       g_value_set_enum (value, capsfilter->caps_change_mode);
251       break;
252     default:
253       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254       break;
255   }
256 }
257
258 static void
259 gst_capsfilter_dispose (GObject * object)
260 {
261   GstCapsFilter *filter = GST_CAPS_FILTER (object);
262
263   gst_caps_replace (&filter->filter_caps, NULL);
264   g_list_free_full (filter->pending_events, (GDestroyNotify) gst_event_unref);
265   filter->pending_events = NULL;
266
267   G_OBJECT_CLASS (parent_class)->dispose (object);
268 }
269
270 static GstCaps *
271 gst_capsfilter_transform_caps (GstBaseTransform * base,
272     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
273 {
274   GstCapsFilter *capsfilter = GST_CAPS_FILTER (base);
275   GstCaps *ret, *filter_caps, *tmp;
276   gboolean retried = FALSE;
277   GstCapsFilterCapsChangeMode caps_change_mode;
278
279   GST_OBJECT_LOCK (capsfilter);
280   filter_caps = gst_caps_ref (capsfilter->filter_caps);
281   capsfilter->filter_caps_used = TRUE;
282   caps_change_mode = capsfilter->caps_change_mode;
283   GST_OBJECT_UNLOCK (capsfilter);
284
285 retry:
286   if (filter) {
287     tmp =
288         gst_caps_intersect_full (filter, filter_caps, GST_CAPS_INTERSECT_FIRST);
289     gst_caps_unref (filter_caps);
290     filter_caps = tmp;
291   }
292
293   ret = gst_caps_intersect_full (filter_caps, caps, GST_CAPS_INTERSECT_FIRST);
294
295   GST_DEBUG_OBJECT (capsfilter, "input:     %" GST_PTR_FORMAT, caps);
296   GST_DEBUG_OBJECT (capsfilter, "filter:    %" GST_PTR_FORMAT, filter);
297   GST_DEBUG_OBJECT (capsfilter, "caps filter:    %" GST_PTR_FORMAT,
298       filter_caps);
299   GST_DEBUG_OBJECT (capsfilter, "intersect: %" GST_PTR_FORMAT, ret);
300
301   if (gst_caps_is_empty (ret)
302       && caps_change_mode ==
303       GST_CAPS_FILTER_CAPS_CHANGE_MODE_DELAYED && capsfilter->previous_caps
304       && !retried) {
305     GList *l;
306
307     GST_DEBUG_OBJECT (capsfilter,
308         "Current filter caps are not compatible, retry with previous");
309     GST_OBJECT_LOCK (capsfilter);
310     gst_caps_unref (filter_caps);
311     gst_caps_unref (ret);
312     filter_caps = gst_caps_new_empty ();
313     for (l = capsfilter->previous_caps; l; l = l->next) {
314       filter_caps = gst_caps_merge (filter_caps, gst_caps_ref (l->data));
315     }
316     GST_OBJECT_UNLOCK (capsfilter);
317     retried = TRUE;
318     goto retry;
319   }
320
321   gst_caps_unref (filter_caps);
322
323   return ret;
324 }
325
326 static gboolean
327 gst_capsfilter_accept_caps (GstBaseTransform * base,
328     GstPadDirection direction, GstCaps * caps)
329 {
330   GstCapsFilter *capsfilter = GST_CAPS_FILTER (base);
331   GstCaps *filter_caps;
332   gboolean ret;
333
334   GST_OBJECT_LOCK (capsfilter);
335   filter_caps = gst_caps_ref (capsfilter->filter_caps);
336   capsfilter->filter_caps_used = TRUE;
337   GST_OBJECT_UNLOCK (capsfilter);
338
339   ret = gst_caps_can_intersect (caps, filter_caps);
340   GST_DEBUG_OBJECT (capsfilter, "can intersect: %d", ret);
341   if (!ret
342       && capsfilter->caps_change_mode ==
343       GST_CAPS_FILTER_CAPS_CHANGE_MODE_DELAYED) {
344     GList *l;
345
346     GST_OBJECT_LOCK (capsfilter);
347     for (l = capsfilter->previous_caps; l; l = l->next) {
348       ret = gst_caps_can_intersect (caps, l->data);
349       if (ret)
350         break;
351     }
352     GST_OBJECT_UNLOCK (capsfilter);
353
354     /* Tell upstream to reconfigure, it's still
355      * looking at old caps */
356     if (ret)
357       gst_base_transform_reconfigure_sink (base);
358   }
359
360   gst_caps_unref (filter_caps);
361
362   return ret;
363 }
364
365 static GstFlowReturn
366 gst_capsfilter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
367 {
368   /* No actual work here. It's all done in the prepare output buffer
369    * func. */
370   return GST_FLOW_OK;
371 }
372
373 static void
374 gst_capsfilter_push_pending_events (GstCapsFilter * filter, GList * events)
375 {
376   GList *l;
377
378   for (l = g_list_last (events); l; l = l->prev) {
379     GST_LOG_OBJECT (filter, "Forwarding %s event",
380         GST_EVENT_TYPE_NAME (l->data));
381     GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (GST_BASE_TRANSFORM_CAST
382         (filter), l->data);
383   }
384   g_list_free (events);
385 }
386
387 /* Ouput buffer preparation ... if the buffer has no caps, and our allowed
388  * output caps is fixed, then send the caps downstream, making sure caps are
389  * sent before segment event.
390  *
391  * This ensures that caps event is sent if we can, so that pipelines like:
392  *   gst-launch filesrc location=rawsamples.raw !
393  *       audio/x-raw,format=S16LE,rate=48000,channels=2 ! alsasink
394  * will work.
395  */
396 static GstFlowReturn
397 gst_capsfilter_prepare_buf (GstBaseTransform * trans, GstBuffer * input,
398     GstBuffer ** buf)
399 {
400   GstFlowReturn ret = GST_FLOW_OK;
401   GstCapsFilter *filter = GST_CAPS_FILTER (trans);
402
403   /* always return the input as output buffer */
404   *buf = input;
405
406   if (GST_PAD_MODE (trans->srcpad) == GST_PAD_MODE_PUSH
407       && !gst_pad_has_current_caps (trans->sinkpad)) {
408
409     /* No caps. See if the output pad only supports fixed caps */
410     GstCaps *out_caps;
411     GList *pending_events = filter->pending_events;
412
413     GST_LOG_OBJECT (trans, "Input pad does not have caps");
414
415     filter->pending_events = NULL;
416
417     out_caps = gst_pad_get_current_caps (trans->srcpad);
418     if (out_caps == NULL) {
419       out_caps = gst_pad_get_allowed_caps (trans->srcpad);
420       g_return_val_if_fail (out_caps != NULL, GST_FLOW_ERROR);
421     }
422
423     out_caps = gst_caps_simplify (out_caps);
424
425     if (gst_caps_is_fixed (out_caps) && !gst_caps_is_empty (out_caps)) {
426       GST_DEBUG_OBJECT (trans, "Have fixed output caps %"
427           GST_PTR_FORMAT " to apply to srcpad", out_caps);
428
429       if (!gst_pad_has_current_caps (trans->srcpad)) {
430         if (gst_pad_set_caps (trans->srcpad, out_caps)) {
431           if (pending_events) {
432             gst_capsfilter_push_pending_events (filter, pending_events);
433             pending_events = NULL;
434           }
435         } else {
436           ret = GST_FLOW_NOT_NEGOTIATED;
437         }
438       } else {
439         gst_capsfilter_push_pending_events (filter, pending_events);
440         pending_events = NULL;
441       }
442
443       g_list_free_full (pending_events, (GDestroyNotify) gst_event_unref);
444       gst_caps_unref (out_caps);
445     } else {
446       gchar *caps_str = gst_caps_to_string (out_caps);
447
448       GST_DEBUG_OBJECT (trans, "Cannot choose caps. Have unfixed output caps %"
449           GST_PTR_FORMAT, out_caps);
450       gst_caps_unref (out_caps);
451
452       GST_ELEMENT_ERROR (trans, STREAM, FORMAT,
453           ("Filter caps do not completely specify the output format"),
454           ("Output caps are unfixed: %s", caps_str));
455
456       g_free (caps_str);
457       g_list_free_full (pending_events, (GDestroyNotify) gst_event_unref);
458
459       ret = GST_FLOW_ERROR;
460     }
461   } else if (G_UNLIKELY (filter->pending_events)) {
462     GList *events = filter->pending_events;
463
464     filter->pending_events = NULL;
465
466     /* push pending events before a buffer */
467     gst_capsfilter_push_pending_events (filter, events);
468   }
469
470   return ret;
471 }
472
473 /* Queue the segment event if there was no caps event */
474 static gboolean
475 gst_capsfilter_sink_event (GstBaseTransform * trans, GstEvent * event)
476 {
477   GstCapsFilter *filter = GST_CAPS_FILTER (trans);
478   gboolean ret;
479
480   if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
481     GList *l;
482
483     for (l = filter->pending_events; l; l = l->next) {
484       if (GST_EVENT_TYPE (l->data) == GST_EVENT_SEGMENT ||
485           GST_EVENT_TYPE (l->data) == GST_EVENT_EOS) {
486         gst_event_unref (l->data);
487         filter->pending_events = g_list_delete_link (filter->pending_events, l);
488         break;
489       }
490     }
491   }
492
493   if (!GST_EVENT_IS_STICKY (event)
494       || GST_EVENT_TYPE (event) <= GST_EVENT_CAPS)
495     goto done;
496
497   /* If we get EOS before any buffers, just push all pending events */
498   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
499     GList *l;
500
501     for (l = g_list_last (filter->pending_events); l; l = l->prev) {
502       GST_LOG_OBJECT (trans, "Forwarding %s event",
503           GST_EVENT_TYPE_NAME (l->data));
504       GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, l->data);
505     }
506     g_list_free (filter->pending_events);
507     filter->pending_events = NULL;
508   } else if (!gst_pad_has_current_caps (trans->sinkpad)) {
509     GST_LOG_OBJECT (trans, "Got %s event before caps, queueing",
510         GST_EVENT_TYPE_NAME (event));
511
512     filter->pending_events = g_list_prepend (filter->pending_events, event);
513
514     return TRUE;
515   }
516
517 done:
518
519   GST_LOG_OBJECT (trans, "Forwarding %s event", GST_EVENT_TYPE_NAME (event));
520   ret =
521       GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans,
522       gst_event_ref (event));
523
524   if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS
525       && filter->caps_change_mode == GST_CAPS_FILTER_CAPS_CHANGE_MODE_DELAYED) {
526     GList *l;
527     GstCaps *caps;
528
529     gst_event_parse_caps (event, &caps);
530
531     /* Remove all previous caps up to one that works.
532      * Note that this might keep some leftover caps if there
533      * are multiple compatible caps */
534     GST_OBJECT_LOCK (filter);
535     for (l = g_list_last (filter->previous_caps); l; l = l->prev) {
536       if (gst_caps_can_intersect (caps, l->data)) {
537         while (l->next) {
538           gst_caps_unref (l->next->data);
539           l = g_list_delete_link (l, l->next);
540         }
541         break;
542       }
543     }
544     if (!l && gst_caps_can_intersect (caps, filter->filter_caps)) {
545       g_list_free_full (filter->previous_caps, (GDestroyNotify) gst_caps_unref);
546       filter->previous_caps = NULL;
547       filter->filter_caps_used = TRUE;
548     }
549     GST_OBJECT_UNLOCK (filter);
550   }
551   gst_event_unref (event);
552
553   return ret;
554 }
555
556 static gboolean
557 gst_capsfilter_stop (GstBaseTransform * trans)
558 {
559   GstCapsFilter *filter = GST_CAPS_FILTER (trans);
560
561   g_list_free_full (filter->pending_events, (GDestroyNotify) gst_event_unref);
562   filter->pending_events = NULL;
563
564   GST_OBJECT_LOCK (filter);
565   g_list_free_full (filter->previous_caps, (GDestroyNotify) gst_caps_unref);
566   filter->previous_caps = NULL;
567   GST_OBJECT_UNLOCK (filter);
568
569   return TRUE;
570 }