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>
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.
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.
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.
23 * SECTION:element-capsfilter
25 * The element does not modify data as such, but can enforce limitations on the
29 * <title>Example launch line</title>
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
34 * gst-launch-1.0 videotestsrc ! video/x-raw,format=GRAY8 ! videoconvert ! autovideosink
35 * ]| which is a short notation for the capsfilter element.
43 #include "../../gst/gst-i18n-lib.h"
44 #include "gstcapsfilter.h"
53 #define DEFAULT_CAPS_CHANGE_MODE (GST_CAPS_FILTER_CAPS_CHANGE_MODE_IMMEDIATE)
55 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
60 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
66 GST_DEBUG_CATEGORY_STATIC (gst_capsfilter_debug);
67 #define GST_CAT_DEFAULT gst_capsfilter_debug
69 /* TODO: Add a drop-buffers mode */
70 #define GST_TYPE_CAPS_FILTER_CAPS_CHANGE_MODE (gst_caps_filter_caps_change_mode_get_type())
72 gst_caps_filter_caps_change_mode_get_type (void)
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"},
84 type = g_enum_register_static ("GstCapsFilterCapsChangeMode", data);
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,
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);
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,
109 static GstFlowReturn gst_capsfilter_prepare_buf (GstBaseTransform * trans,
110 GstBuffer * input, GstBuffer ** buf);
111 static gboolean gst_capsfilter_sink_event (GstBaseTransform * trans,
113 static gboolean gst_capsfilter_stop (GstBaseTransform * trans);
116 gst_capsfilter_class_init (GstCapsFilterClass * klass)
118 GObjectClass *gobject_class;
119 GstElementClass *gstelement_class;
120 GstBaseTransformClass *trans_class;
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;
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));
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));
142 gstelement_class = GST_ELEMENT_CLASS (klass);
143 gst_element_class_set_static_metadata (gstelement_class,
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);
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);
163 gst_capsfilter_init (GstCapsFilter * filter)
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;
174 gst_capsfilter_set_property (GObject * object, guint prop_id,
175 const GValue * value, GParamSpec * pspec)
177 GstCapsFilter *capsfilter = GST_CAPS_FILTER (object);
180 case PROP_FILTER_CAPS:{
183 const GstCaps *new_caps_val = gst_value_get_caps (value);
185 if (new_caps_val == NULL) {
186 new_caps = gst_caps_new_any ();
188 new_caps = (GstCaps *) new_caps_val;
189 gst_caps_ref (new_caps);
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;
206 capsfilter->filter_caps_used = FALSE;
207 GST_OBJECT_UNLOCK (capsfilter);
209 gst_caps_unref (old_caps);
211 GST_DEBUG_OBJECT (capsfilter, "set new caps %" GST_PTR_FORMAT, new_caps);
213 gst_base_transform_reconfigure_sink (GST_BASE_TRANSFORM (object));
216 case PROP_CAPS_CHANGE_MODE:{
217 GstCapsFilterCapsChangeMode old_change_mode;
219 GST_OBJECT_LOCK (capsfilter);
220 old_change_mode = capsfilter->caps_change_mode;
221 capsfilter->caps_change_mode = g_value_get_enum (value);
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;
228 GST_OBJECT_UNLOCK (capsfilter);
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 gst_capsfilter_get_property (GObject * object, guint prop_id, GValue * value,
241 GstCapsFilter *capsfilter = GST_CAPS_FILTER (object);
244 case PROP_FILTER_CAPS:
245 GST_OBJECT_LOCK (capsfilter);
246 gst_value_set_caps (value, capsfilter->filter_caps);
247 GST_OBJECT_UNLOCK (capsfilter);
249 case PROP_CAPS_CHANGE_MODE:
250 g_value_set_enum (value, capsfilter->caps_change_mode);
253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259 gst_capsfilter_dispose (GObject * object)
261 GstCapsFilter *filter = GST_CAPS_FILTER (object);
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;
267 G_OBJECT_CLASS (parent_class)->dispose (object);
271 gst_capsfilter_transform_caps (GstBaseTransform * base,
272 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
274 GstCapsFilter *capsfilter = GST_CAPS_FILTER (base);
275 GstCaps *ret, *filter_caps, *tmp;
276 gboolean retried = FALSE;
277 GstCapsFilterCapsChangeMode caps_change_mode;
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);
288 gst_caps_intersect_full (filter, filter_caps, GST_CAPS_INTERSECT_FIRST);
289 gst_caps_unref (filter_caps);
293 ret = gst_caps_intersect_full (filter_caps, caps, GST_CAPS_INTERSECT_FIRST);
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,
299 GST_DEBUG_OBJECT (capsfilter, "intersect: %" GST_PTR_FORMAT, ret);
301 if (gst_caps_is_empty (ret)
302 && caps_change_mode ==
303 GST_CAPS_FILTER_CAPS_CHANGE_MODE_DELAYED && capsfilter->previous_caps
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));
316 GST_OBJECT_UNLOCK (capsfilter);
321 gst_caps_unref (filter_caps);
327 gst_capsfilter_accept_caps (GstBaseTransform * base,
328 GstPadDirection direction, GstCaps * caps)
330 GstCapsFilter *capsfilter = GST_CAPS_FILTER (base);
331 GstCaps *filter_caps;
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);
339 ret = gst_caps_can_intersect (caps, filter_caps);
340 GST_DEBUG_OBJECT (capsfilter, "can intersect: %d", ret);
342 && capsfilter->caps_change_mode ==
343 GST_CAPS_FILTER_CAPS_CHANGE_MODE_DELAYED) {
346 GST_OBJECT_LOCK (capsfilter);
347 for (l = capsfilter->previous_caps; l; l = l->next) {
348 ret = gst_caps_can_intersect (caps, l->data);
352 GST_OBJECT_UNLOCK (capsfilter);
354 /* Tell upstream to reconfigure, it's still
355 * looking at old caps */
357 gst_base_transform_reconfigure_sink (base);
360 gst_caps_unref (filter_caps);
366 gst_capsfilter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
368 /* No actual work here. It's all done in the prepare output buffer
374 gst_capsfilter_push_pending_events (GstCapsFilter * filter, GList * events)
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
384 g_list_free (events);
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.
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
397 gst_capsfilter_prepare_buf (GstBaseTransform * trans, GstBuffer * input,
400 GstFlowReturn ret = GST_FLOW_OK;
401 GstCapsFilter *filter = GST_CAPS_FILTER (trans);
403 /* always return the input as output buffer */
406 if (GST_PAD_MODE (trans->srcpad) == GST_PAD_MODE_PUSH
407 && !gst_pad_has_current_caps (trans->sinkpad)) {
409 /* No caps. See if the output pad only supports fixed caps */
411 GList *pending_events = filter->pending_events;
413 GST_LOG_OBJECT (trans, "Input pad does not have caps");
415 filter->pending_events = NULL;
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);
423 out_caps = gst_caps_simplify (out_caps);
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);
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;
436 ret = GST_FLOW_NOT_NEGOTIATED;
439 gst_capsfilter_push_pending_events (filter, pending_events);
440 pending_events = NULL;
443 g_list_free_full (pending_events, (GDestroyNotify) gst_event_unref);
444 gst_caps_unref (out_caps);
446 gchar *caps_str = gst_caps_to_string (out_caps);
448 GST_DEBUG_OBJECT (trans, "Cannot choose caps. Have unfixed output caps %"
449 GST_PTR_FORMAT, out_caps);
450 gst_caps_unref (out_caps);
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));
457 g_list_free_full (pending_events, (GDestroyNotify) gst_event_unref);
459 ret = GST_FLOW_ERROR;
461 } else if (G_UNLIKELY (filter->pending_events)) {
462 GList *events = filter->pending_events;
464 filter->pending_events = NULL;
466 /* push pending events before a buffer */
467 gst_capsfilter_push_pending_events (filter, events);
473 /* Queue the segment event if there was no caps event */
475 gst_capsfilter_sink_event (GstBaseTransform * trans, GstEvent * event)
477 GstCapsFilter *filter = GST_CAPS_FILTER (trans);
480 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
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);
493 if (!GST_EVENT_IS_STICKY (event)
494 || GST_EVENT_TYPE (event) <= GST_EVENT_CAPS)
497 /* If we get EOS before any buffers, just push all pending events */
498 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
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);
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));
512 filter->pending_events = g_list_prepend (filter->pending_events, event);
519 GST_LOG_OBJECT (trans, "Forwarding %s event", GST_EVENT_TYPE_NAME (event));
521 GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans,
522 gst_event_ref (event));
524 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS
525 && filter->caps_change_mode == GST_CAPS_FILTER_CAPS_CHANGE_MODE_DELAYED) {
529 gst_event_parse_caps (event, &caps);
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)) {
538 gst_caps_unref (l->next->data);
539 l = g_list_delete_link (l, l->next);
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;
549 GST_OBJECT_UNLOCK (filter);
551 gst_event_unref (event);
557 gst_capsfilter_stop (GstBaseTransform * trans)
559 GstCapsFilter *filter = GST_CAPS_FILTER (trans);
561 g_list_free_full (filter->pending_events, (GDestroyNotify) gst_event_unref);
562 filter->pending_events = NULL;
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);