2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2005 Wim Taymans <wim@fluendo.com>
4 * 2005 Andy Wingo <wingo@fluendo.com>
5 * 2005 Thomas Vander Stichele <thomas at apestaart dot org>
6 * 2008 Wim Taymans <wim.taymans@gmail.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * SECTION:gstbasetransform
26 * @short_description: Base class for simple transform filters
27 * @see_also: #GstBaseSrc, #GstBaseSink
29 * This base class is for filter elements that process data.
33 * <listitem><para>one sinkpad and one srcpad</para></listitem>
35 * Possible formats on sink and source pad implemented
36 * with custom transform_caps function. By default uses
37 * same format on sink and source.
39 * <listitem><para>Handles state changes</para></listitem>
40 * <listitem><para>Does flushing</para></listitem>
41 * <listitem><para>Push mode</para></listitem>
43 * Pull mode if the sub-class transform can operate on arbitrary data
48 * <title>Use Cases</title>
52 * <itemizedlist><title>Passthrough mode</title>
54 * Element has no interest in modifying the buffer. It may want to inspect it,
55 * in which case the element should have a transform_ip function. If there
56 * is no transform_ip function in passthrough mode, the buffer is pushed
60 * On the GstBaseTransformClass is the passthrough_on_same_caps variable
61 * which will automatically set/unset passthrough based on whether the
62 * element negotiates the same caps on both pads.
65 * passthrough_on_same_caps on an element that doesn't implement a
66 * transform_caps function is useful for elements that only inspect data
71 * <title>Example elements</title>
72 * <listitem>Level</listitem>
73 * <listitem>Videoscale, audioconvert, ffmpegcolorspace, audioresample in
74 * certain modes.</listitem>
79 * <title>Modifications in-place - input buffer and output buffer are the
82 * The element must implement a transform_ip function.
85 * Output buffer size must <= input buffer size
88 * If the always_in_place flag is set, non-writable buffers will be copied
89 * and passed to the transform_ip function, otherwise a new buffer will be
90 * created and the transform function called.
93 * Incoming writable buffers will be passed to the transform_ip function
94 * immediately. </para></listitem>
96 * only implementing transform_ip and not transform implies always_in_place
101 * <title>Example elements</title>
102 * <listitem>Volume</listitem>
103 * <listitem>Audioconvert in certain modes (signed/unsigned
104 * conversion)</listitem>
105 * <listitem>ffmpegcolorspace in certain modes (endianness
106 * swapping)</listitem>
111 * <title>Modifications only to the caps/metadata of a buffer</title>
113 * The element does not require writable data, but non-writable buffers
114 * should be subbuffered so that the meta-information can be replaced.
117 * Elements wishing to operate in this mode should replace the
118 * prepare_output_buffer method to create subbuffers of the input buffer
119 * and set always_in_place to TRUE
123 * <title>Example elements</title>
124 * <listitem>Capsfilter when setting caps on outgoing buffers that have
126 * <listitem>identity when it is going to re-timestamp buffers by
127 * datarate.</listitem>
131 * <itemizedlist><title>Normal mode</title>
133 * always_in_place flag is not set, or there is no transform_ip function
136 * Element will receive an input buffer and output buffer to operate on.
139 * Output buffer is allocated by calling the prepare_output_buffer function.
143 * <title>Example elements</title>
144 * <listitem>Videoscale, ffmpegcolorspace, audioconvert when doing
145 * scaling/conversions</listitem>
149 * <itemizedlist><title>Special output buffer allocations</title>
151 * Elements which need to do special allocation of their output buffers
152 * other than what gst_buffer_pad_alloc allows should implement a
153 * prepare_output_buffer method, which calls the parent implementation and
154 * passes the newly allocated buffer.
158 * <title>Example elements</title>
159 * <listitem>efence</listitem>
166 * <title>Sub-class settable flags on GstBaseTransform</title>
170 * <itemizedlist><title>passthrough</title>
172 * Implies that in the current configuration, the sub-class is not
173 * interested in modifying the buffers.
176 * Elements which are always in passthrough mode whenever the same caps
177 * has been negotiated on both pads can set the class variable
178 * passthrough_on_same_caps to have this behaviour automatically.
183 * <itemizedlist><title>always_in_place</title>
185 * Determines whether a non-writable buffer will be copied before passing
186 * to the transform_ip function.
189 * Implied TRUE if no transform function is implemented.
192 * Implied FALSE if ONLY transform function is implemented.
208 #include "../../../gst/gst_private.h"
209 #include "../../../gst/gst-i18n-lib.h"
210 #include "gstbasetransform.h"
211 #include <gst/gstmarshal.h>
213 GST_DEBUG_CATEGORY_STATIC (gst_base_transform_debug);
214 #define GST_CAT_DEFAULT gst_base_transform_debug
216 /* BaseTransform signals and args */
223 #define DEFAULT_PROP_QOS FALSE
231 #define GST_BASE_TRANSFORM_GET_PRIVATE(obj) \
232 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_TRANSFORM, GstBaseTransformPrivate))
234 struct _GstBaseTransformPrivate
236 /* QoS *//* with LOCK */
237 gboolean qos_enabled;
239 GstClockTime earliest_time;
240 /* previous buffer had a discont */
243 GstActivateMode pad_mode;
247 /* caps used for allocating buffers */
248 gboolean proxy_alloc;
253 * This flag controls if basetransform should explicitly
254 * do a pad alloc when it receives a buffer even if it operates on
255 * passthrough, this is needed to check for downstream caps suggestions
256 * and this newly alloc'ed buffer is discarded.
258 * Without this flag basetransform would try a pad alloc whenever it
259 * gets a new buffer and pipelines like:
260 * "src ! basetrans1 ! basetrans2 ! basetrans3 ! sink"
261 * Would have a 3 pad allocs for each buffer pushed downstream from the src.
263 * This flag is set to TRUE on start up, on setcaps and when a buffer is
264 * pushed downstream. It is set to FALSE after a pad alloc has been requested
266 * The rationale is that when a pad alloc flows through the pipeline, all
267 * basetransform elements on passthrough will avoid pad alloc'ing when they
270 gboolean force_alloc;
272 /* upstream caps and size suggestions */
273 GstCaps *sink_suggest;
275 gboolean suggest_pending;
277 gboolean reconfigure;
283 GstClockTime last_stop_out;
286 static GstElementClass *parent_class = NULL;
288 static void gst_base_transform_class_init (GstBaseTransformClass * klass);
289 static void gst_base_transform_init (GstBaseTransform * trans,
290 GstBaseTransformClass * klass);
291 static GstFlowReturn gst_base_transform_prepare_output_buffer (GstBaseTransform
292 * trans, GstBuffer * input, GstBuffer ** buf);
295 gst_base_transform_get_type (void)
297 static volatile gsize base_transform_type = 0;
299 if (g_once_init_enter (&base_transform_type)) {
301 static const GTypeInfo base_transform_info = {
302 sizeof (GstBaseTransformClass),
305 (GClassInitFunc) gst_base_transform_class_init,
308 sizeof (GstBaseTransform),
310 (GInstanceInitFunc) gst_base_transform_init,
313 _type = g_type_register_static (GST_TYPE_ELEMENT,
314 "GstBaseTransform", &base_transform_info, G_TYPE_FLAG_ABSTRACT);
315 g_once_init_leave (&base_transform_type, _type);
317 return base_transform_type;
320 static void gst_base_transform_finalize (GObject * object);
321 static void gst_base_transform_set_property (GObject * object, guint prop_id,
322 const GValue * value, GParamSpec * pspec);
323 static void gst_base_transform_get_property (GObject * object, guint prop_id,
324 GValue * value, GParamSpec * pspec);
325 static gboolean gst_base_transform_src_activate_pull (GstPad * pad,
327 static gboolean gst_base_transform_sink_activate_push (GstPad * pad,
329 static gboolean gst_base_transform_activate (GstBaseTransform * trans,
331 static gboolean gst_base_transform_get_unit_size (GstBaseTransform * trans,
332 GstCaps * caps, gsize * size);
334 static gboolean gst_base_transform_src_event (GstPad * pad, GstEvent * event);
335 static gboolean gst_base_transform_src_eventfunc (GstBaseTransform * trans,
337 static gboolean gst_base_transform_sink_event (GstPad * pad, GstEvent * event);
338 static gboolean gst_base_transform_sink_eventfunc (GstBaseTransform * trans,
340 static gboolean gst_base_transform_check_get_range (GstPad * pad);
341 static GstFlowReturn gst_base_transform_getrange (GstPad * pad, guint64 offset,
342 guint length, GstBuffer ** buffer);
343 static GstFlowReturn gst_base_transform_chain (GstPad * pad,
345 static GstCaps *gst_base_transform_getcaps (GstPad * pad);
346 static gboolean gst_base_transform_acceptcaps (GstPad * pad, GstCaps * caps);
347 static gboolean gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
348 GstPadDirection direction, GstCaps * caps);
349 static gboolean gst_base_transform_setcaps (GstPad * pad, GstCaps * caps);
350 static gboolean gst_base_transform_query (GstPad * pad, GstQuery * query);
351 static const GstQueryType *gst_base_transform_query_type (GstPad * pad);
353 /* static guint gst_base_transform_signals[LAST_SIGNAL] = { 0 }; */
356 gst_base_transform_finalize (GObject * object)
358 GstBaseTransform *trans;
360 trans = GST_BASE_TRANSFORM (object);
362 gst_caps_replace (&trans->priv->sink_suggest, NULL);
363 g_mutex_free (trans->transform_lock);
365 G_OBJECT_CLASS (parent_class)->finalize (object);
369 gst_base_transform_class_init (GstBaseTransformClass * klass)
371 GObjectClass *gobject_class;
373 gobject_class = G_OBJECT_CLASS (klass);
375 GST_DEBUG_CATEGORY_INIT (gst_base_transform_debug, "basetransform", 0,
376 "basetransform element");
378 GST_DEBUG ("gst_base_transform_class_init");
380 g_type_class_add_private (klass, sizeof (GstBaseTransformPrivate));
382 parent_class = g_type_class_peek_parent (klass);
384 gobject_class->set_property = gst_base_transform_set_property;
385 gobject_class->get_property = gst_base_transform_get_property;
387 g_object_class_install_property (gobject_class, PROP_QOS,
388 g_param_spec_boolean ("qos", "QoS", "Handle Quality-of-Service events",
389 DEFAULT_PROP_QOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
391 gobject_class->finalize = gst_base_transform_finalize;
393 klass->passthrough_on_same_caps = FALSE;
394 klass->event = GST_DEBUG_FUNCPTR (gst_base_transform_sink_eventfunc);
395 klass->src_event = GST_DEBUG_FUNCPTR (gst_base_transform_src_eventfunc);
397 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps_default);
401 gst_base_transform_init (GstBaseTransform * trans,
402 GstBaseTransformClass * bclass)
404 GstPadTemplate *pad_template;
406 GST_DEBUG ("gst_base_transform_init");
408 trans->priv = GST_BASE_TRANSFORM_GET_PRIVATE (trans);
411 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
412 g_return_if_fail (pad_template != NULL);
413 trans->sinkpad = gst_pad_new_from_template (pad_template, "sink");
414 gst_pad_set_getcaps_function (trans->sinkpad,
415 GST_DEBUG_FUNCPTR (gst_base_transform_getcaps));
416 gst_pad_set_acceptcaps_function (trans->sinkpad,
417 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps));
418 gst_pad_set_setcaps_function (trans->sinkpad,
419 GST_DEBUG_FUNCPTR (gst_base_transform_setcaps));
420 gst_pad_set_event_function (trans->sinkpad,
421 GST_DEBUG_FUNCPTR (gst_base_transform_sink_event));
422 gst_pad_set_chain_function (trans->sinkpad,
423 GST_DEBUG_FUNCPTR (gst_base_transform_chain));
424 gst_pad_set_activatepush_function (trans->sinkpad,
425 GST_DEBUG_FUNCPTR (gst_base_transform_sink_activate_push));
426 gst_pad_set_query_function (trans->sinkpad,
427 GST_DEBUG_FUNCPTR (gst_base_transform_query));
428 gst_pad_set_query_type_function (trans->sinkpad,
429 GST_DEBUG_FUNCPTR (gst_base_transform_query_type));
430 gst_element_add_pad (GST_ELEMENT (trans), trans->sinkpad);
433 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
434 g_return_if_fail (pad_template != NULL);
435 trans->srcpad = gst_pad_new_from_template (pad_template, "src");
436 gst_pad_set_getcaps_function (trans->srcpad,
437 GST_DEBUG_FUNCPTR (gst_base_transform_getcaps));
438 gst_pad_set_acceptcaps_function (trans->srcpad,
439 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps));
440 gst_pad_set_event_function (trans->srcpad,
441 GST_DEBUG_FUNCPTR (gst_base_transform_src_event));
442 gst_pad_set_checkgetrange_function (trans->srcpad,
443 GST_DEBUG_FUNCPTR (gst_base_transform_check_get_range));
444 gst_pad_set_getrange_function (trans->srcpad,
445 GST_DEBUG_FUNCPTR (gst_base_transform_getrange));
446 gst_pad_set_activatepull_function (trans->srcpad,
447 GST_DEBUG_FUNCPTR (gst_base_transform_src_activate_pull));
448 gst_pad_set_query_function (trans->srcpad,
449 GST_DEBUG_FUNCPTR (gst_base_transform_query));
450 gst_pad_set_query_type_function (trans->srcpad,
451 GST_DEBUG_FUNCPTR (gst_base_transform_query_type));
452 gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
454 trans->transform_lock = g_mutex_new ();
455 trans->pending_configure = FALSE;
456 trans->priv->qos_enabled = DEFAULT_PROP_QOS;
457 trans->cache_caps1 = NULL;
458 trans->cache_caps2 = NULL;
459 trans->priv->pad_mode = GST_ACTIVATE_NONE;
460 trans->priv->gap_aware = FALSE;
462 trans->passthrough = FALSE;
463 if (bclass->transform == NULL) {
464 /* If no transform function, always_in_place is TRUE */
465 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
466 trans->always_in_place = TRUE;
468 if (bclass->transform_ip == NULL) {
469 GST_DEBUG_OBJECT (trans, "setting passthrough TRUE");
470 trans->passthrough = TRUE;
474 trans->priv->processed = 0;
475 trans->priv->dropped = 0;
476 trans->priv->force_alloc = TRUE;
479 /* given @caps on the src or sink pad (given by @direction)
480 * calculate the possible caps on the other pad.
482 * Returns new caps, unref after usage.
485 gst_base_transform_transform_caps (GstBaseTransform * trans,
486 GstPadDirection direction, GstCaps * caps)
489 GstBaseTransformClass *klass;
494 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
496 /* if there is a custom transform function, use this */
497 if (klass->transform_caps) {
501 /* start with empty caps */
502 ret = gst_caps_new_empty ();
503 GST_DEBUG_OBJECT (trans, "transform caps (direction = %d)", direction);
505 if (gst_caps_is_any (caps)) {
506 /* for any caps we still have to call the transform function */
507 GST_DEBUG_OBJECT (trans, "from: ANY");
508 temp = klass->transform_caps (trans, direction, caps);
509 GST_DEBUG_OBJECT (trans, " to: %" GST_PTR_FORMAT, temp);
511 temp = gst_caps_make_writable (temp);
512 gst_caps_append (ret, temp);
514 gint n = gst_caps_get_size (caps);
515 /* we send caps with just one structure to the transform
516 * function as this is easier for the element */
517 for (i = 0; i < n; i++) {
520 nth = gst_caps_copy_nth (caps, i);
521 GST_LOG_OBJECT (trans, "from[%d]: %" GST_PTR_FORMAT, i, nth);
522 temp = klass->transform_caps (trans, direction, nth);
523 gst_caps_unref (nth);
524 GST_LOG_OBJECT (trans, " to[%d]: %" GST_PTR_FORMAT, i, temp);
526 temp = gst_caps_make_writable (temp);
528 /* here we need to only append those structures, that are not yet
529 * in there, we use the merge function for this */
530 gst_caps_merge (ret, temp);
532 GST_LOG_OBJECT (trans, " merged[%d]: %" GST_PTR_FORMAT, i, ret);
534 GST_LOG_OBJECT (trans, "merged: (%d)", gst_caps_get_size (ret));
535 /* FIXME: we can't do much simplification here because we don't really want to
536 * change the caps order
537 gst_caps_do_simplify (ret);
538 GST_DEBUG_OBJECT (trans, "simplified: (%d)", gst_caps_get_size (ret));
542 GST_DEBUG_OBJECT (trans, "identity from: %" GST_PTR_FORMAT, caps);
543 /* no transform function, use the identity transform */
544 ret = gst_caps_ref (caps);
547 GST_DEBUG_OBJECT (trans, "to: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (ret),
553 /* transform a buffer of @size with @caps on the pad with @direction to
554 * the size of a buffer with @othercaps and store the result in @othersize
556 * We have two ways of doing this:
557 * 1) use a custom transform size function, this is for complicated custom
558 * cases with no fixed unit_size.
559 * 2) use the unit_size functions where there is a relationship between the
560 * caps and the size of a buffer.
563 gst_base_transform_transform_size (GstBaseTransform * trans,
564 GstPadDirection direction, GstCaps * caps,
565 gsize size, GstCaps * othercaps, gsize * othersize)
567 gsize inunitsize, outunitsize, units;
568 GstBaseTransformClass *klass;
571 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
573 GST_DEBUG_OBJECT (trans, "asked to transform size %d for caps %"
574 GST_PTR_FORMAT " to size for caps %" GST_PTR_FORMAT " in direction %s",
575 size, caps, othercaps, direction == GST_PAD_SRC ? "SRC" : "SINK");
577 if (klass->transform_size) {
578 /* if there is a custom transform function, use this */
579 ret = klass->transform_size (trans, direction, caps, size, othercaps,
581 } else if (klass->get_unit_size == NULL) {
582 /* if there is no transform_size and no unit_size, it means the
583 * element does not modify the size of a buffer */
587 /* there is no transform_size function, we have to use the unit_size
588 * functions. This method assumes there is a fixed unit_size associated with
589 * each caps. We provide the same amount of units on both sides. */
590 if (!gst_base_transform_get_unit_size (trans, caps, &inunitsize))
593 GST_DEBUG_OBJECT (trans, "input size %d, input unit size %d", size,
596 /* input size must be a multiple of the unit_size of the input caps */
597 if (inunitsize == 0 || (size % inunitsize != 0))
600 /* get the amount of units */
601 units = size / inunitsize;
603 /* now get the unit size of the output */
604 if (!gst_base_transform_get_unit_size (trans, othercaps, &outunitsize))
607 /* the output size is the unit_size times the amount of units on the
609 *othersize = units * outunitsize;
610 GST_DEBUG_OBJECT (trans, "transformed size to %d", *othersize);
619 GST_DEBUG_OBJECT (trans, "could not get in_size");
620 g_warning ("%s: could not get in_size", GST_ELEMENT_NAME (trans));
625 GST_DEBUG_OBJECT (trans, "Size %u is not a multiple of unit size %u", size,
627 g_warning ("%s: size %" G_GSIZE_FORMAT " is not a multiple of unit size %"
628 G_GSIZE_FORMAT, GST_ELEMENT_NAME (trans), size, inunitsize);
633 GST_DEBUG_OBJECT (trans, "could not get out_size");
634 g_warning ("%s: could not get out_size", GST_ELEMENT_NAME (trans));
639 /* get the caps that can be handled by @pad. We perform:
641 * - take the caps of peer of otherpad,
642 * - filter against the padtemplate of otherpad,
643 * - calculate all transforms of remaining caps
644 * - filter against template of @pad
646 * If there is no peer, we simply return the caps of the padtemplate of pad.
649 gst_base_transform_getcaps (GstPad * pad)
651 GstBaseTransform *trans;
655 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
657 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
659 /* we can do what the peer can */
660 caps = gst_pad_peer_get_caps (otherpad);
663 const GstCaps *templ;
665 GST_DEBUG_OBJECT (pad, "peer caps %" GST_PTR_FORMAT, caps);
667 /* filtered against our padtemplate on the other side */
668 templ = gst_pad_get_pad_template_caps (otherpad);
669 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
670 temp = gst_caps_intersect (caps, templ);
671 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
672 gst_caps_unref (caps);
674 /* then see what we can transform this to */
675 caps = gst_base_transform_transform_caps (trans,
676 GST_PAD_DIRECTION (otherpad), temp);
677 GST_DEBUG_OBJECT (pad, "transformed %" GST_PTR_FORMAT, caps);
678 gst_caps_unref (temp);
682 /* and filter against the template of this pad */
683 templ = gst_pad_get_pad_template_caps (pad);
684 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
685 /* We keep the caps sorted like the returned caps */
686 temp = gst_caps_intersect_full (caps, templ, GST_CAPS_INTERSECT_FIRST);
687 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
688 gst_caps_unref (caps);
689 /* this is what we can do */
692 /* no peer or the peer can do anything, our padtemplate is enough then */
693 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
697 GST_DEBUG_OBJECT (trans, "returning %" GST_PTR_FORMAT, caps);
699 gst_object_unref (trans);
704 /* function triggered when the in and out caps are negotiated and need
705 * to be configured in the subclass. */
707 gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
711 GstBaseTransformClass *klass;
713 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
715 GST_DEBUG_OBJECT (trans, "in caps: %" GST_PTR_FORMAT, in);
716 GST_DEBUG_OBJECT (trans, "out caps: %" GST_PTR_FORMAT, out);
718 /* clear the cache */
719 gst_caps_replace (&trans->cache_caps1, NULL);
720 gst_caps_replace (&trans->cache_caps2, NULL);
722 /* figure out same caps state */
723 trans->have_same_caps = gst_caps_is_equal (in, out);
724 GST_DEBUG_OBJECT (trans, "have_same_caps: %d", trans->have_same_caps);
726 /* If we've a transform_ip method and same input/output caps, set in_place
727 * by default. If for some reason the sub-class prefers using a transform
728 * function, it can clear the in place flag in the set_caps */
729 gst_base_transform_set_in_place (trans,
730 klass->transform_ip && trans->have_same_caps);
732 /* Set the passthrough if the class wants passthrough_on_same_caps
733 * and we have the same caps on each pad */
734 if (klass->passthrough_on_same_caps)
735 gst_base_transform_set_passthrough (trans, trans->have_same_caps);
737 /* now configure the element with the caps */
738 if (klass->set_caps) {
739 GST_DEBUG_OBJECT (trans, "Calling set_caps method to setup caps");
740 ret = klass->set_caps (trans, in, out);
743 GST_OBJECT_LOCK (trans);
744 /* make sure we reevaluate how the buffer_alloc works wrt to proxy allocating
745 * the buffer. FIXME, this triggers some quite heavy codepaths that don't need
747 trans->priv->suggest_pending = TRUE;
748 GST_OBJECT_UNLOCK (trans);
749 trans->negotiated = ret;
754 /* check if caps @in on @pad can be transformed to @out on the other pad.
755 * We don't have a vmethod to test this yet so we have to do a somewhat less
756 * efficient check for this.
759 gst_base_transform_can_transform (GstBaseTransform * trans, GstPad * pad,
760 GstCaps * in, GstCaps * out)
764 /* convert the in caps to all possible out caps */
766 gst_base_transform_transform_caps (trans, GST_PAD_DIRECTION (pad), in);
768 /* check if transform is empty */
769 if (!othercaps || gst_caps_is_empty (othercaps))
772 /* check if the out caps is a subset of the othercaps */
773 if (!gst_caps_can_intersect (out, othercaps))
777 gst_caps_unref (othercaps);
779 GST_DEBUG_OBJECT (trans, "from %" GST_PTR_FORMAT, in);
780 GST_DEBUG_OBJECT (trans, "to %" GST_PTR_FORMAT, out);
787 GST_DEBUG_OBJECT (trans,
788 "transform returned useless %" GST_PTR_FORMAT, othercaps);
790 gst_caps_unref (othercaps);
795 GST_DEBUG_OBJECT (trans, "no subset");
797 gst_caps_unref (othercaps);
802 /* given a fixed @caps on @pad, create the best possible caps for the
804 * @caps must be fixed when calling this function.
806 * This function calls the transform caps vmethod of the basetransform to figure
807 * out the possible target formats. It then tries to select the best format from
810 * - attempt passthrough if the target caps is a superset of the input caps
811 * - fixating by using peer caps
812 * - fixating with transform fixate function
813 * - fixating with pad fixate functions.
815 * this function returns a caps that can be transformed into and is accepted by
819 gst_base_transform_find_transform (GstBaseTransform * trans, GstPad * pad,
822 GstBaseTransformClass *klass;
823 GstPad *otherpad, *otherpeer;
825 gboolean peer_checked = FALSE;
828 /* caps must be fixed here, this is a programming error if it's not */
829 g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
831 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
833 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
834 otherpeer = gst_pad_get_peer (otherpad);
836 /* see how we can transform the input caps. We need to do this even for
837 * passthrough because it might be possible that this element cannot support
838 * passthrough at all. */
839 othercaps = gst_base_transform_transform_caps (trans,
840 GST_PAD_DIRECTION (pad), caps);
842 /* The caps we can actually output is the intersection of the transformed
843 * caps with the pad template for the pad */
846 const GstCaps *templ_caps;
848 templ_caps = gst_pad_get_pad_template_caps (otherpad);
849 GST_DEBUG_OBJECT (trans,
850 "intersecting against padtemplate %" GST_PTR_FORMAT, templ_caps);
852 intersect = gst_caps_intersect (othercaps, templ_caps);
854 gst_caps_unref (othercaps);
855 othercaps = intersect;
858 /* check if transform is empty */
859 if (!othercaps || gst_caps_is_empty (othercaps))
862 /* if the othercaps are not fixed, we need to fixate them, first attempt
863 * is by attempting passthrough if the othercaps are a superset of caps. */
864 /* FIXME. maybe the caps is not fixed because it has multiple structures of
866 is_fixed = gst_caps_is_fixed (othercaps);
868 GST_DEBUG_OBJECT (trans,
869 "transform returned non fixed %" GST_PTR_FORMAT, othercaps);
871 /* see if the target caps are a superset of the source caps, in this
872 * case we can try to perform passthrough */
873 if (gst_caps_can_intersect (othercaps, caps)) {
874 GST_DEBUG_OBJECT (trans, "try passthrough with %" GST_PTR_FORMAT, caps);
876 /* try passthrough. we know it's fixed, because caps is fixed */
877 if (gst_pad_accept_caps (otherpeer, caps)) {
878 GST_DEBUG_OBJECT (trans, "peer accepted %" GST_PTR_FORMAT, caps);
879 /* peer accepted unmodified caps, we free the original non-fixed
880 * caps and work with the passthrough caps */
881 gst_caps_unref (othercaps);
882 othercaps = gst_caps_ref (caps);
884 /* mark that we checked othercaps with the peer, this
885 * makes sure we don't call accept_caps again with these same
889 GST_DEBUG_OBJECT (trans,
890 "peer did not accept %" GST_PTR_FORMAT, caps);
893 GST_DEBUG_OBJECT (trans, "no peer, doing passthrough");
894 gst_caps_unref (othercaps);
895 othercaps = gst_caps_ref (caps);
901 /* second attempt at fixation is done by intersecting with
903 if (!is_fixed && otherpeer) {
904 /* intersect against what the peer can do */
908 GST_DEBUG_OBJECT (trans, "othercaps now %" GST_PTR_FORMAT, othercaps);
910 peercaps = gst_pad_get_caps (otherpeer);
911 intersect = gst_caps_intersect (peercaps, othercaps);
912 gst_caps_unref (peercaps);
913 gst_caps_unref (othercaps);
914 othercaps = intersect;
915 peer_checked = FALSE;
917 is_fixed = gst_caps_is_fixed (othercaps);
919 GST_DEBUG_OBJECT (trans,
920 "filtering against peer yields %" GST_PTR_FORMAT, othercaps);
923 if (gst_caps_is_empty (othercaps))
924 goto no_transform_possible;
926 /* third attempt at fixation, call the fixate vmethod and
927 * ultimately call the pad fixate function. */
929 GST_DEBUG_OBJECT (trans,
930 "trying to fixate %" GST_PTR_FORMAT " on pad %s:%s",
931 othercaps, GST_DEBUG_PAD_NAME (otherpad));
933 /* since we have no other way to fixate left, we might as well just take
934 * the first of the caps list and fixate that */
936 /* FIXME: when fixating using the vmethod, it might make sense to fixate
937 * each of the caps; but Wim doesn't see a use case for that yet */
938 gst_caps_truncate (othercaps);
939 peer_checked = FALSE;
941 if (klass->fixate_caps) {
942 GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
943 " using caps %" GST_PTR_FORMAT
944 " on pad %s:%s using fixate_caps vmethod", othercaps, caps,
945 GST_DEBUG_PAD_NAME (otherpad));
946 klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
947 is_fixed = gst_caps_is_fixed (othercaps);
949 /* if still not fixed, no other option but to let the default pad fixate
950 * function do its job */
952 GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
953 " on pad %s:%s using gst_pad_fixate_caps", othercaps,
954 GST_DEBUG_PAD_NAME (otherpad));
955 gst_pad_fixate_caps (otherpad, othercaps);
956 is_fixed = gst_caps_is_fixed (othercaps);
958 GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
960 GST_DEBUG ("caps are fixed");
961 /* else caps are fixed but the subclass may want to add fields */
962 if (klass->fixate_caps) {
963 othercaps = gst_caps_make_writable (othercaps);
965 GST_DEBUG_OBJECT (trans, "doing fixate %" GST_PTR_FORMAT
966 " using caps %" GST_PTR_FORMAT
967 " on pad %s:%s using fixate_caps vmethod", othercaps, caps,
968 GST_DEBUG_PAD_NAME (otherpad));
970 klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
971 is_fixed = gst_caps_is_fixed (othercaps);
975 /* caps should be fixed now, if not we have to fail. */
977 goto could_not_fixate;
979 /* and peer should accept, don't check again if we already checked the
980 * othercaps against the peer. */
981 if (!peer_checked && otherpeer && !gst_pad_accept_caps (otherpeer, othercaps))
984 GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT
985 ", and got final caps %" GST_PTR_FORMAT, caps, othercaps);
988 gst_object_unref (otherpeer);
995 GST_DEBUG_OBJECT (trans,
996 "transform returned useless %" GST_PTR_FORMAT, othercaps);
999 no_transform_possible:
1001 GST_DEBUG_OBJECT (trans,
1002 "transform could not transform %" GST_PTR_FORMAT
1003 " in anything we support", caps);
1008 GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
1013 GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
1014 " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1020 gst_object_unref (otherpeer);
1022 gst_caps_unref (othercaps);
1028 gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
1029 GstPadDirection direction, GstCaps * caps)
1033 GstCaps *othercaps = NULL;
1035 gboolean ret = TRUE;
1038 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1040 /* we need fixed caps for the check, fall back to the default implementation
1042 if (!gst_caps_is_fixed (caps))
1047 GST_DEBUG_OBJECT (trans, "non fixed accept caps %" GST_PTR_FORMAT, caps);
1049 /* get all the formats we can handle on this pad */
1050 if (direction == GST_PAD_SRC)
1051 allowed = gst_pad_get_caps (trans->srcpad);
1053 allowed = gst_pad_get_caps (trans->sinkpad);
1056 GST_DEBUG_OBJECT (trans, "gst_pad_get_caps() failed");
1057 goto no_transform_possible;
1060 GST_DEBUG_OBJECT (trans, "allowed caps %" GST_PTR_FORMAT, allowed);
1062 /* intersect with the requested format */
1063 ret = gst_caps_can_intersect (allowed, caps);
1064 gst_caps_unref (allowed);
1067 goto no_transform_possible;
1071 GST_DEBUG_OBJECT (pad, "accept caps %" GST_PTR_FORMAT, caps);
1073 /* find best possible caps for the other pad as a way to see if we can
1074 * transform this caps. */
1075 othercaps = gst_base_transform_find_transform (trans, pad, caps);
1076 if (!othercaps || gst_caps_is_empty (othercaps))
1077 goto no_transform_possible;
1079 GST_DEBUG_OBJECT (pad, "we can transform to %" GST_PTR_FORMAT, othercaps);
1085 /* We know it's always NULL since we never use it */
1087 gst_caps_unref (othercaps);
1093 no_transform_possible:
1095 GST_DEBUG_OBJECT (trans,
1096 "transform could not transform %" GST_PTR_FORMAT
1097 " in anything we support", caps);
1104 gst_base_transform_acceptcaps (GstPad * pad, GstCaps * caps)
1106 gboolean ret = TRUE;
1107 GstBaseTransform *trans;
1108 GstBaseTransformClass *bclass;
1110 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1111 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1113 if (bclass->accept_caps)
1114 ret = bclass->accept_caps (trans, GST_PAD_DIRECTION (pad), caps);
1116 gst_object_unref (trans);
1121 /* called when new caps arrive on the sink or source pad,
1122 * We try to find the best caps for the other side using our _find_transform()
1123 * function. If there are caps, we configure the transform for this new
1126 * FIXME, this function is currently commutative but this should not really be
1127 * because we never set caps starting from the srcpad.
1130 gst_base_transform_setcaps (GstPad * pad, GstCaps * caps)
1132 GstBaseTransform *trans;
1133 GstPad *otherpad, *otherpeer;
1134 GstCaps *othercaps = NULL;
1135 gboolean ret = TRUE;
1136 GstCaps *incaps, *outcaps;
1138 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1140 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1141 otherpeer = gst_pad_get_peer (otherpad);
1143 /* if we get called recursively, we bail out now to avoid an
1145 if (GST_PAD_IS_IN_SETCAPS (otherpad))
1148 GST_DEBUG_OBJECT (pad, "have new caps %p %" GST_PTR_FORMAT, caps, caps);
1150 /* find best possible caps for the other pad */
1151 othercaps = gst_base_transform_find_transform (trans, pad, caps);
1152 if (!othercaps || gst_caps_is_empty (othercaps))
1153 goto no_transform_possible;
1155 /* configure the element now */
1156 /* make sure in and out caps are correct */
1157 if (pad == trans->sinkpad) {
1159 outcaps = othercaps;
1165 /* if we have the same caps, we can optimize and reuse the input caps */
1166 if (gst_caps_is_equal (incaps, outcaps)) {
1167 GST_INFO_OBJECT (trans, "reuse caps");
1168 gst_caps_unref (othercaps);
1169 outcaps = othercaps = gst_caps_ref (incaps);
1172 /* call configure now */
1173 if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
1174 goto failed_configure;
1176 /* we know this will work, we implement the setcaps */
1177 gst_pad_set_caps (otherpad, othercaps);
1179 if (pad == trans->srcpad && trans->priv->pad_mode == GST_ACTIVATE_PULL) {
1181 ret &= gst_pad_set_caps (otherpeer, othercaps);
1183 GST_INFO_OBJECT (trans, "otherpeer setcaps(%" GST_PTR_FORMAT ") failed",
1189 /* new caps, force alloc on next buffer on the chain */
1190 trans->priv->force_alloc = TRUE;
1192 gst_object_unref (otherpeer);
1194 gst_caps_unref (othercaps);
1196 trans->negotiated = ret;
1198 gst_object_unref (trans);
1203 no_transform_possible:
1205 GST_WARNING_OBJECT (trans,
1206 "transform could not transform %" GST_PTR_FORMAT
1207 " in anything we support", caps);
1213 GST_WARNING_OBJECT (trans, "FAILED to configure caps %" GST_PTR_FORMAT
1214 " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1221 gst_base_transform_query (GstPad * pad, GstQuery * query)
1223 gboolean ret = FALSE;
1224 GstBaseTransform *trans;
1227 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1228 if (G_UNLIKELY (trans == NULL))
1230 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1232 switch (GST_QUERY_TYPE (query)) {
1233 case GST_QUERY_POSITION:{
1236 gst_query_parse_position (query, &format, NULL);
1237 if (format == GST_FORMAT_TIME && trans->segment.format == GST_FORMAT_TIME) {
1241 if ((pad == trans->sinkpad)
1242 || (trans->priv->last_stop_out == GST_CLOCK_TIME_NONE)) {
1244 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1245 trans->segment.last_stop);
1247 pos = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1248 trans->priv->last_stop_out);
1250 gst_query_set_position (query, format, pos);
1252 ret = gst_pad_peer_query (otherpad, query);
1257 ret = gst_pad_peer_query (otherpad, query);
1261 gst_object_unref (trans);
1265 static const GstQueryType *
1266 gst_base_transform_query_type (GstPad * pad)
1268 static const GstQueryType types[] = {
1277 compute_upstream_suggestion (GstBaseTransform * trans, gsize expsize,
1281 GstBaseTransformPrivate *priv = trans->priv;
1283 GST_DEBUG_OBJECT (trans, "trying to find upstream suggestion");
1285 /* we cannot convert the current buffer but we might be able to suggest a
1286 * new format upstream, try to find what the best format is. */
1287 othercaps = gst_base_transform_find_transform (trans, trans->srcpad, caps);
1290 GST_DEBUG_OBJECT (trans, "incompatible caps, ignoring");
1291 /* we received caps that we cannot transform. Upstream is behaving badly
1292 * because it should have checked if we could handle these caps. We can
1293 * simply ignore these caps and produce a buffer with our original caps. */
1297 GST_DEBUG_OBJECT (trans, "getting size of suggestion");
1299 /* not a subset, we have a new upstream suggestion, remember it and
1300 * allocate a default buffer. First we try to convert the size */
1301 if (gst_base_transform_transform_size (trans,
1302 GST_PAD_SRC, caps, expsize, othercaps, &size_suggest)) {
1304 /* ok, remember the suggestions now */
1305 GST_DEBUG_OBJECT (trans,
1306 "storing new caps and size suggestion of %u and %" GST_PTR_FORMAT,
1307 size_suggest, othercaps);
1309 GST_OBJECT_LOCK (trans->sinkpad);
1310 if (priv->sink_suggest)
1311 gst_caps_unref (priv->sink_suggest);
1312 priv->sink_suggest = gst_caps_ref (othercaps);
1313 priv->size_suggest = size_suggest;
1314 trans->priv->suggest_pending = TRUE;
1315 GST_OBJECT_UNLOCK (trans->sinkpad);
1317 gst_caps_unref (othercaps);
1321 /* Allocate a buffer using gst_pad_alloc_buffer
1323 * This function can do renegotiation on the source pad
1325 * The output buffer is always writable. outbuf can be equal to
1326 * inbuf, the caller should be prepared for this and perform
1327 * appropriate refcounting.
1329 static GstFlowReturn
1330 gst_base_transform_prepare_output_buffer (GstBaseTransform * trans,
1331 GstBuffer * in_buf, GstBuffer ** out_buf)
1333 GstBaseTransformClass *bclass;
1334 GstBaseTransformPrivate *priv;
1335 GstFlowReturn ret = GST_FLOW_OK;
1336 gsize insize, outsize, newsize, expsize;
1337 gboolean discard, setcaps, copymeta;
1338 GstCaps *incaps, *oldcaps, *newcaps, *outcaps;
1340 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1346 insize = gst_buffer_get_size (in_buf);
1348 /* figure out how to allocate a buffer based on the current configuration */
1349 if (trans->passthrough) {
1350 GST_DEBUG_OBJECT (trans, "doing passthrough alloc");
1351 /* passthrough, we don't really need to call pad alloc but we still need to
1352 * in order to get upstream negotiation. The output size is the same as the
1355 /* we always alloc and discard here */
1358 gboolean want_in_place = (bclass->transform_ip != NULL)
1359 && trans->always_in_place;
1361 if (want_in_place) {
1362 GST_DEBUG_OBJECT (trans, "doing inplace alloc");
1363 /* we alloc a buffer of the same size as the input */
1365 /* only discard it when the input was not writable, otherwise, we reuse
1366 * the input buffer. */
1367 discard = gst_buffer_is_writable (in_buf);
1368 GST_DEBUG_OBJECT (trans, "discard: %d", discard);
1370 GST_DEBUG_OBJECT (trans, "getting output size for copy transform");
1371 /* copy transform, figure out the output size */
1372 if (!gst_base_transform_transform_size (trans,
1373 GST_PAD_SINK, GST_PAD_CAPS (trans->sinkpad),
1374 insize, GST_PAD_CAPS (trans->srcpad), &outsize)) {
1377 /* never discard this buffer, we need it for storing the output */
1382 oldcaps = GST_PAD_CAPS (trans->srcpad);
1384 if (bclass->prepare_output_buffer) {
1385 GST_DEBUG_OBJECT (trans,
1386 "calling prepare buffer with caps %p %" GST_PTR_FORMAT, oldcaps,
1389 bclass->prepare_output_buffer (trans, in_buf, outsize, oldcaps,
1392 /* get a new ref to the srcpad caps, the prepare_output_buffer function can
1393 * update the pad caps if it wants */
1394 oldcaps = GST_PAD_CAPS (trans->srcpad);
1397 * decrease refcount again if vmethod returned refcounted in_buf. This
1398 * is because we need to make sure that the buffer is writable for the
1399 * in_place transform. The docs of the vmethod say that you should return
1400 * a reffed inbuf, which is exactly what we don't want :), oh well.. */
1401 if (in_buf == *out_buf)
1402 gst_buffer_unref (in_buf);
1404 /* never discard the buffer from the prepare_buffer method */
1405 if (*out_buf != NULL)
1409 if (ret != GST_FLOW_OK)
1412 if (*out_buf == NULL) {
1413 if (trans->passthrough && !trans->priv->force_alloc) {
1414 GST_DEBUG_OBJECT (trans, "Avoiding pad alloc");
1415 *out_buf = gst_buffer_ref (in_buf);
1417 GST_DEBUG_OBJECT (trans, "doing alloc with caps %" GST_PTR_FORMAT,
1420 *out_buf = gst_buffer_new_and_alloc (outsize);
1421 gst_buffer_set_caps (*out_buf, oldcaps);
1423 ret = gst_pad_alloc_buffer (trans->srcpad,
1424 GST_BUFFER_OFFSET (in_buf), outsize, oldcaps, out_buf);
1425 if (ret != GST_FLOW_OK)
1431 /* must always have a buffer by now */
1432 if (*out_buf == NULL)
1435 /* check if we got different caps on this new output buffer */
1436 newcaps = GST_BUFFER_CAPS (*out_buf);
1437 newsize = gst_buffer_get_size (*out_buf);
1439 if (newcaps && !gst_caps_is_equal (newcaps, oldcaps)) {
1441 gboolean can_convert;
1443 GST_DEBUG_OBJECT (trans, "received new caps %" GST_PTR_FORMAT, newcaps);
1445 incaps = GST_PAD_CAPS (trans->sinkpad);
1447 /* check if we can convert the current incaps to the new target caps */
1449 gst_base_transform_can_transform (trans, trans->sinkpad, incaps,
1453 GST_DEBUG_OBJECT (trans, "cannot perform transform on current buffer");
1455 gst_base_transform_transform_size (trans,
1456 GST_PAD_SINK, incaps, insize, newcaps, &expsize);
1458 compute_upstream_suggestion (trans, expsize, newcaps);
1460 /* we got a suggested caps but we can't transform to it. See if there is
1461 * another downstream format that we can transform to */
1463 gst_base_transform_find_transform (trans, trans->sinkpad, incaps);
1465 if (othercaps && !gst_caps_is_empty (othercaps)) {
1466 GST_DEBUG_OBJECT (trans, "we found target caps %" GST_PTR_FORMAT,
1468 *out_buf = gst_buffer_make_writable (*out_buf);
1469 gst_buffer_set_caps (*out_buf, othercaps);
1470 gst_caps_unref (othercaps);
1471 newcaps = GST_BUFFER_CAPS (*out_buf);
1473 } else if (othercaps)
1474 gst_caps_unref (othercaps);
1477 /* it's possible that the buffer we got is of the wrong size, get the
1478 * expected size here, we will check the size if we are going to use the
1479 * buffer later on. */
1480 gst_base_transform_transform_size (trans,
1481 GST_PAD_SINK, incaps, insize, newcaps, &expsize);
1484 GST_DEBUG_OBJECT (trans, "reconfigure transform for current buffer");
1486 /* subclass might want to add fields to the caps */
1487 if (bclass->fixate_caps != NULL) {
1488 newcaps = gst_caps_copy (newcaps);
1490 GST_DEBUG_OBJECT (trans, "doing fixate %" GST_PTR_FORMAT
1491 " using caps %" GST_PTR_FORMAT
1492 " on pad %s:%s using fixate_caps vmethod", newcaps, incaps,
1493 GST_DEBUG_PAD_NAME (trans->srcpad));
1494 bclass->fixate_caps (trans, GST_PAD_SINK, incaps, newcaps);
1496 *out_buf = gst_buffer_make_writable (*out_buf);
1497 gst_buffer_set_caps (*out_buf, newcaps);
1498 gst_caps_unref (newcaps);
1499 newcaps = GST_BUFFER_CAPS (*out_buf);
1502 /* caps not empty, try to renegotiate to the new format */
1503 if (!gst_base_transform_configure_caps (trans, incaps, newcaps)) {
1504 /* not sure we need to fail hard here, we can simply continue our
1505 * conversion with what we negotiated before */
1506 goto failed_configure;
1508 /* new format configure, and use the new output buffer */
1509 gst_pad_set_caps (trans->srcpad, newcaps);
1511 /* clear previous cached sink-pad caps, so buffer_alloc knows that
1512 * it needs to revisit the decision about whether to proxy or not: */
1513 gst_caps_replace (&priv->sink_alloc, NULL);
1514 /* if we got a buffer of the wrong size, discard it now and make sure we
1515 * allocate a propertly sized buffer later. */
1516 if (newsize != expsize) {
1517 if (in_buf != *out_buf)
1518 gst_buffer_unref (*out_buf);
1523 compute_upstream_suggestion (trans, expsize, newcaps);
1525 if (in_buf != *out_buf)
1526 gst_buffer_unref (*out_buf);
1529 } else if (outsize != newsize) {
1530 GST_WARNING_OBJECT (trans, "Caps did not change but allocated size does "
1531 "not match expected size (%d != %d)", newsize, outsize);
1532 if (in_buf != *out_buf)
1533 gst_buffer_unref (*out_buf);
1537 /* these are the final output caps */
1538 outcaps = GST_PAD_CAPS (trans->srcpad);
1541 if (*out_buf == NULL) {
1543 GST_DEBUG_OBJECT (trans, "make default output buffer of size %d",
1545 /* no valid buffer yet, make one, metadata is writable */
1546 *out_buf = gst_buffer_new_and_alloc (outsize);
1547 gst_buffer_copy_into (*out_buf, in_buf,
1548 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1550 GST_DEBUG_OBJECT (trans, "reuse input buffer");
1554 if (trans->passthrough && in_buf != *out_buf) {
1555 /* we are asked to perform a passthrough transform but the input and
1556 * output buffers are different. We have to discard the output buffer and
1557 * reuse the input buffer. */
1558 GST_DEBUG_OBJECT (trans, "passthrough but different buffers");
1562 GST_DEBUG_OBJECT (trans, "discard buffer, reuse input buffer");
1563 gst_buffer_unref (*out_buf);
1566 GST_DEBUG_OBJECT (trans, "using allocated buffer in %p, out %p", in_buf,
1568 /* if we have different buffers, check if the metadata is ok */
1569 if (*out_buf != in_buf) {
1572 mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
1573 GST_BUFFER_FLAG_DELTA_UNIT | GST_BUFFER_FLAG_DISCONT |
1574 GST_BUFFER_FLAG_GAP | GST_BUFFER_FLAG_MEDIA1 |
1575 GST_BUFFER_FLAG_MEDIA2 | GST_BUFFER_FLAG_MEDIA3;
1576 /* see if the flags and timestamps match */
1578 (GST_MINI_OBJECT_FLAGS (*out_buf) & mask) ==
1579 (GST_MINI_OBJECT_FLAGS (in_buf) & mask);
1581 GST_BUFFER_TIMESTAMP (*out_buf) != GST_BUFFER_TIMESTAMP (in_buf) ||
1582 GST_BUFFER_DURATION (*out_buf) != GST_BUFFER_DURATION (in_buf) ||
1583 GST_BUFFER_OFFSET (*out_buf) != GST_BUFFER_OFFSET (in_buf) ||
1584 GST_BUFFER_OFFSET_END (*out_buf) != GST_BUFFER_OFFSET_END (in_buf);
1589 /* check if we need to make things writable. We need this when we need to
1590 * update the caps or the metadata on the output buffer. */
1591 newcaps = GST_BUFFER_CAPS (*out_buf);
1592 /* we check the pointers as a quick check and then go to the more involved
1593 * check. This is needed when we receive different pointers on the sinkpad
1594 * that mean the same caps. What we then want to do is prefer those caps over
1595 * the ones on the srcpad and set the srcpad caps to the buffer caps */
1596 setcaps = !newcaps || ((newcaps != outcaps)
1597 && (!gst_caps_is_equal (newcaps, outcaps)));
1598 /* we need to modify the metadata when the element is not gap aware,
1599 * passthrough is not used and the gap flag is set */
1600 copymeta |= !trans->priv->gap_aware && !trans->passthrough
1601 && (GST_MINI_OBJECT_FLAGS (*out_buf) & GST_BUFFER_FLAG_GAP);
1603 if (setcaps || copymeta) {
1604 GST_DEBUG_OBJECT (trans, "setcaps %d, copymeta %d", setcaps, copymeta);
1605 if (!gst_buffer_is_writable (*out_buf)) {
1606 GST_DEBUG_OBJECT (trans, "buffer %p not writable", *out_buf);
1607 if (in_buf == *out_buf)
1608 *out_buf = gst_buffer_copy (in_buf);
1610 *out_buf = gst_buffer_make_writable (*out_buf);
1612 /* when we get here, the metadata should be writable */
1614 gst_buffer_set_caps (*out_buf, outcaps);
1616 gst_buffer_copy_into (*out_buf, in_buf,
1617 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1618 /* clear the GAP flag when the subclass does not understand it */
1619 if (!trans->priv->gap_aware)
1620 GST_BUFFER_FLAG_UNSET (*out_buf, GST_BUFFER_FLAG_GAP);
1628 GST_WARNING_OBJECT (trans, "pad-alloc failed: %s", gst_flow_get_name (ret));
1633 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
1634 ("Sub-class failed to provide an output buffer"), (NULL));
1635 return GST_FLOW_ERROR;
1639 GST_ERROR_OBJECT (trans, "unknown output size");
1640 return GST_FLOW_ERROR;
1644 GST_WARNING_OBJECT (trans, "failed to configure caps");
1645 return GST_FLOW_NOT_NEGOTIATED;
1649 /* Given @caps calcultate the size of one unit.
1651 * For video caps, this is the size of one frame (and thus one buffer).
1652 * For audio caps, this is the size of one sample.
1654 * These values are cached since they do not change and the calculation
1655 * potentially involves parsing caps and other expensive stuff.
1657 * We have two cache locations to store the size, one for the source caps
1658 * and one for the sink caps.
1660 * this function returns FALSE if no size could be calculated.
1663 gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
1666 gboolean res = FALSE;
1667 GstBaseTransformClass *bclass;
1669 /* see if we have the result cached */
1670 if (trans->cache_caps1 == caps) {
1671 *size = trans->cache_caps1_size;
1672 GST_DEBUG_OBJECT (trans, "returned %d from first cache", *size);
1675 if (trans->cache_caps2 == caps) {
1676 *size = trans->cache_caps2_size;
1677 GST_DEBUG_OBJECT (trans, "returned %d from second cached", *size);
1681 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1682 if (bclass->get_unit_size) {
1683 res = bclass->get_unit_size (trans, caps, size);
1684 GST_DEBUG_OBJECT (trans, "caps %" GST_PTR_FORMAT
1685 ") has unit size %d, result %s", caps, *size, res ? "TRUE" : "FALSE");
1688 /* and cache the values */
1689 if (trans->cache_caps1 == NULL) {
1690 gst_caps_replace (&trans->cache_caps1, caps);
1691 trans->cache_caps1_size = *size;
1692 GST_DEBUG_OBJECT (trans, "caching %d in first cache", *size);
1693 } else if (trans->cache_caps2 == NULL) {
1694 gst_caps_replace (&trans->cache_caps2, caps);
1695 trans->cache_caps2_size = *size;
1696 GST_DEBUG_OBJECT (trans, "caching %d in second cache", *size);
1698 GST_DEBUG_OBJECT (trans, "no free spot to cache unit_size");
1702 GST_DEBUG_OBJECT (trans, "Sub-class does not implement get_unit_size");
1708 gst_base_transform_sink_event (GstPad * pad, GstEvent * event)
1710 GstBaseTransform *trans;
1711 GstBaseTransformClass *bclass;
1712 gboolean ret = TRUE;
1713 gboolean forward = TRUE;
1715 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1716 if (G_UNLIKELY (trans == NULL)) {
1717 gst_event_unref (event);
1720 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1723 forward = bclass->event (trans, event);
1725 /* FIXME, do this in the default event handler so the subclass can do
1726 * something different. */
1728 ret = gst_pad_push_event (trans->srcpad, event);
1730 gst_event_unref (event);
1732 gst_object_unref (trans);
1738 gst_base_transform_sink_eventfunc (GstBaseTransform * trans, GstEvent * event)
1740 switch (GST_EVENT_TYPE (event)) {
1741 case GST_EVENT_FLUSH_START:
1743 case GST_EVENT_FLUSH_STOP:
1744 GST_OBJECT_LOCK (trans);
1745 /* reset QoS parameters */
1746 trans->priv->proportion = 1.0;
1747 trans->priv->earliest_time = -1;
1748 trans->priv->discont = FALSE;
1749 trans->priv->processed = 0;
1750 trans->priv->dropped = 0;
1751 GST_OBJECT_UNLOCK (trans);
1752 /* we need new segment info after the flush. */
1753 trans->have_newsegment = FALSE;
1754 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
1755 trans->priv->last_stop_out = GST_CLOCK_TIME_NONE;
1761 case GST_EVENT_NEWSEGMENT:
1764 gdouble rate, arate;
1765 gint64 start, stop, time;
1768 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1769 &start, &stop, &time);
1771 trans->have_newsegment = TRUE;
1773 gst_segment_set_newsegment_full (&trans->segment, update, rate, arate,
1774 format, start, stop, time);
1776 if (format == GST_FORMAT_TIME) {
1777 GST_DEBUG_OBJECT (trans, "received TIME NEW_SEGMENT %" GST_TIME_FORMAT
1778 " -- %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT
1779 ", accum %" GST_TIME_FORMAT,
1780 GST_TIME_ARGS (trans->segment.start),
1781 GST_TIME_ARGS (trans->segment.stop),
1782 GST_TIME_ARGS (trans->segment.time),
1783 GST_TIME_ARGS (trans->segment.accum));
1785 GST_DEBUG_OBJECT (trans, "received NEW_SEGMENT %" G_GINT64_FORMAT
1786 " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT
1787 ", accum %" G_GINT64_FORMAT,
1788 trans->segment.start, trans->segment.stop,
1789 trans->segment.time, trans->segment.accum);
1801 gst_base_transform_src_event (GstPad * pad, GstEvent * event)
1803 GstBaseTransform *trans;
1804 GstBaseTransformClass *bclass;
1805 gboolean ret = TRUE;
1807 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1808 if (G_UNLIKELY (trans == NULL)) {
1809 gst_event_unref (event);
1813 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1815 if (bclass->src_event)
1816 ret = bclass->src_event (trans, event);
1818 gst_event_unref (event);
1820 gst_object_unref (trans);
1826 gst_base_transform_src_eventfunc (GstBaseTransform * trans, GstEvent * event)
1830 GST_DEBUG_OBJECT (trans, "handling event %p %" GST_PTR_FORMAT, event, event);
1832 switch (GST_EVENT_TYPE (event)) {
1833 case GST_EVENT_SEEK:
1835 case GST_EVENT_NAVIGATION:
1840 GstClockTimeDiff diff;
1841 GstClockTime timestamp;
1843 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1844 gst_base_transform_update_qos (trans, proportion, diff, timestamp);
1851 ret = gst_pad_push_event (trans->sinkpad, event);
1856 /* perform a transform on @inbuf and put the result in @outbuf.
1858 * This function is common to the push and pull-based operations.
1860 * This function takes ownership of @inbuf */
1861 static GstFlowReturn
1862 gst_base_transform_handle_buffer (GstBaseTransform * trans, GstBuffer * inbuf,
1863 GstBuffer ** outbuf)
1865 GstBaseTransformClass *bclass;
1866 GstFlowReturn ret = GST_FLOW_OK;
1867 gboolean want_in_place, reconfigure;
1868 GstClockTime running_time;
1869 GstClockTime timestamp;
1873 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1875 if (G_LIKELY ((incaps = GST_BUFFER_CAPS (inbuf)))) {
1876 GST_OBJECT_LOCK (trans);
1877 reconfigure = trans->priv->reconfigure;
1878 trans->priv->reconfigure = FALSE;
1879 GST_OBJECT_UNLOCK (trans);
1881 if (G_UNLIKELY (reconfigure)) {
1882 GST_DEBUG_OBJECT (trans, "we had a pending reconfigure");
1883 /* if we need to reconfigure we pretend a buffer with new caps arrived. This
1884 * will reconfigure the transform with the new output format. We can only
1885 * do this if the buffer actually has caps. */
1886 if (!gst_base_transform_setcaps (trans->sinkpad, incaps))
1887 goto not_negotiated;
1891 insize = gst_buffer_get_size (inbuf);
1893 if (GST_BUFFER_OFFSET_IS_VALID (inbuf))
1894 GST_DEBUG_OBJECT (trans,
1895 "handling buffer %p of size %" G_GSIZE_FORMAT " and offset %"
1896 G_GUINT64_FORMAT, inbuf, insize, GST_BUFFER_OFFSET (inbuf));
1898 GST_DEBUG_OBJECT (trans,
1899 "handling buffer %p of size %" G_GSIZE_FORMAT " and offset NONE", inbuf,
1902 /* Don't allow buffer handling before negotiation, except in passthrough mode
1903 * or if the class doesn't implement a set_caps function (in which case it doesn't
1906 if (!trans->negotiated && !trans->passthrough && (bclass->set_caps != NULL))
1907 goto not_negotiated;
1909 /* Set discont flag so we can mark the outgoing buffer */
1910 if (GST_BUFFER_IS_DISCONT (inbuf)) {
1911 GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", inbuf);
1912 trans->priv->discont = TRUE;
1915 /* can only do QoS if the segment is in TIME */
1916 if (trans->segment.format != GST_FORMAT_TIME)
1919 /* QOS is done on the running time of the buffer, get it now */
1920 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
1921 running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
1924 if (running_time != -1) {
1926 GstClockTime earliest_time;
1929 /* lock for getting the QoS parameters that are set (in a different thread)
1930 * with the QOS events */
1931 GST_OBJECT_LOCK (trans);
1932 earliest_time = trans->priv->earliest_time;
1933 proportion = trans->priv->proportion;
1934 /* check for QoS, don't perform conversion for buffers
1935 * that are known to be late. */
1936 need_skip = trans->priv->qos_enabled &&
1937 earliest_time != -1 && running_time <= earliest_time;
1938 GST_OBJECT_UNLOCK (trans);
1941 GstMessage *qos_msg;
1942 GstClockTime duration;
1943 guint64 stream_time;
1946 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "skipping transform: qostime %"
1947 GST_TIME_FORMAT " <= %" GST_TIME_FORMAT,
1948 GST_TIME_ARGS (running_time), GST_TIME_ARGS (earliest_time));
1950 trans->priv->dropped++;
1952 duration = GST_BUFFER_DURATION (inbuf);
1954 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1956 jitter = GST_CLOCK_DIFF (running_time, earliest_time);
1959 gst_message_new_qos (GST_OBJECT_CAST (trans), FALSE, running_time,
1960 stream_time, timestamp, duration);
1961 gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
1962 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
1963 trans->priv->processed, trans->priv->dropped);
1964 gst_element_post_message (GST_ELEMENT_CAST (trans), qos_msg);
1966 /* mark discont for next buffer */
1967 trans->priv->discont = TRUE;
1974 /* first try to allocate an output buffer based on the currently negotiated
1975 * format. While we call pad-alloc we could renegotiate the srcpad format or
1976 * have a new suggestion for upstream buffer-alloc.
1977 * In any case, outbuf will contain a buffer suitable for doing the configured
1978 * transform after this function. */
1979 ret = gst_base_transform_prepare_output_buffer (trans, inbuf, outbuf);
1980 if (G_UNLIKELY (ret != GST_FLOW_OK))
1983 /* now perform the needed transform */
1984 if (trans->passthrough) {
1985 /* In passthrough mode, give transform_ip a look at the
1986 * buffer, without making it writable, or just push the
1988 if (bclass->transform_ip) {
1989 GST_DEBUG_OBJECT (trans, "doing passthrough transform");
1990 ret = bclass->transform_ip (trans, *outbuf);
1992 GST_DEBUG_OBJECT (trans, "element is in passthrough");
1995 want_in_place = (bclass->transform_ip != NULL) && trans->always_in_place;
1997 if (want_in_place) {
1998 GST_DEBUG_OBJECT (trans, "doing inplace transform");
2000 if (inbuf != *outbuf) {
2001 guint8 *indata, *outdata;
2002 gsize insize, outsize;
2004 /* Different buffer. The data can still be the same when we are dealing
2005 * with subbuffers of the same buffer. Note that because of the FIXME in
2006 * prepare_output_buffer() we have decreased the refcounts of inbuf and
2007 * outbuf to keep them writable */
2008 indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
2009 outdata = gst_buffer_map (*outbuf, &outsize, NULL, GST_MAP_WRITE);
2011 if (indata != outdata)
2012 memcpy (outdata, indata, insize);
2014 gst_buffer_unmap (inbuf, indata, insize);
2015 gst_buffer_unmap (*outbuf, outdata, outsize);
2017 ret = bclass->transform_ip (trans, *outbuf);
2019 GST_DEBUG_OBJECT (trans, "doing non-inplace transform");
2021 if (bclass->transform)
2022 ret = bclass->transform (trans, inbuf, *outbuf);
2024 ret = GST_FLOW_NOT_SUPPORTED;
2029 /* only unref input buffer if we allocated a new outbuf buffer */
2030 if (*outbuf != inbuf)
2031 gst_buffer_unref (inbuf);
2033 /* pushed a buffer, we can now try an alloc */
2034 GST_DEBUG_OBJECT (trans, "Pushed a buffer, setting force alloc to true");
2035 trans->priv->force_alloc = TRUE;
2041 gst_buffer_unref (inbuf);
2042 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
2043 ("not negotiated"), ("not negotiated"));
2044 return GST_FLOW_NOT_NEGOTIATED;
2048 gst_buffer_unref (inbuf);
2049 GST_WARNING_OBJECT (trans, "could not get buffer from pool: %s",
2050 gst_flow_get_name (ret));
2056 gst_base_transform_check_get_range (GstPad * pad)
2058 GstBaseTransform *trans;
2061 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2063 ret = gst_pad_check_pull_range (trans->sinkpad);
2065 gst_object_unref (trans);
2070 /* FIXME, getrange is broken, need to pull range from the other
2071 * end based on the transform_size result.
2073 static GstFlowReturn
2074 gst_base_transform_getrange (GstPad * pad, guint64 offset,
2075 guint length, GstBuffer ** buffer)
2077 GstBaseTransform *trans;
2078 GstBaseTransformClass *klass;
2082 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2084 ret = gst_pad_pull_range (trans->sinkpad, offset, length, &inbuf);
2085 if (G_UNLIKELY (ret != GST_FLOW_OK))
2088 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2089 if (klass->before_transform)
2090 klass->before_transform (trans, inbuf);
2092 GST_BASE_TRANSFORM_LOCK (trans);
2093 ret = gst_base_transform_handle_buffer (trans, inbuf, buffer);
2094 GST_BASE_TRANSFORM_UNLOCK (trans);
2097 gst_object_unref (trans);
2104 GST_DEBUG_OBJECT (trans, "failed to pull a buffer: %s",
2105 gst_flow_get_name (ret));
2110 static GstFlowReturn
2111 gst_base_transform_chain (GstPad * pad, GstBuffer * buffer)
2113 GstBaseTransform *trans;
2114 GstBaseTransformClass *klass;
2116 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
2117 GstClockTime timestamp, duration;
2118 GstBuffer *outbuf = NULL;
2120 trans = GST_BASE_TRANSFORM (GST_OBJECT_PARENT (pad));
2122 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2123 duration = GST_BUFFER_DURATION (buffer);
2125 /* calculate end position of the incoming buffer */
2126 if (timestamp != GST_CLOCK_TIME_NONE) {
2127 if (duration != GST_CLOCK_TIME_NONE)
2128 last_stop = timestamp + duration;
2130 last_stop = timestamp;
2133 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2134 if (klass->before_transform)
2135 klass->before_transform (trans, buffer);
2137 /* protect transform method and concurrent buffer alloc */
2138 GST_BASE_TRANSFORM_LOCK (trans);
2139 ret = gst_base_transform_handle_buffer (trans, buffer, &outbuf);
2140 GST_BASE_TRANSFORM_UNLOCK (trans);
2142 /* outbuf can be NULL, this means a dropped buffer, if we have a buffer but
2143 * GST_BASE_TRANSFORM_FLOW_DROPPED we will not push either. */
2144 if (outbuf != NULL) {
2145 if ((ret == GST_FLOW_OK)) {
2146 GstClockTime last_stop_out = GST_CLOCK_TIME_NONE;
2148 /* Remember last stop position */
2149 if (last_stop != GST_CLOCK_TIME_NONE &&
2150 trans->segment.format == GST_FORMAT_TIME)
2151 gst_segment_set_last_stop (&trans->segment, GST_FORMAT_TIME, last_stop);
2153 if (GST_BUFFER_TIMESTAMP_IS_VALID (outbuf)) {
2154 last_stop_out = GST_BUFFER_TIMESTAMP (outbuf);
2155 if (GST_BUFFER_DURATION_IS_VALID (outbuf))
2156 last_stop_out += GST_BUFFER_DURATION (outbuf);
2157 } else if (last_stop != GST_CLOCK_TIME_NONE) {
2158 last_stop_out = last_stop;
2160 if (last_stop_out != GST_CLOCK_TIME_NONE
2161 && trans->segment.format == GST_FORMAT_TIME)
2162 trans->priv->last_stop_out = last_stop_out;
2164 /* apply DISCONT flag if the buffer is not yet marked as such */
2165 if (trans->priv->discont) {
2166 if (!GST_BUFFER_IS_DISCONT (outbuf)) {
2167 outbuf = gst_buffer_make_writable (outbuf);
2168 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2170 trans->priv->discont = FALSE;
2172 trans->priv->processed++;
2173 ret = gst_pad_push (trans->srcpad, outbuf);
2175 gst_buffer_unref (outbuf);
2179 /* convert internal flow to OK and mark discont for the next buffer. */
2180 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED) {
2181 trans->priv->discont = TRUE;
2189 gst_base_transform_set_property (GObject * object, guint prop_id,
2190 const GValue * value, GParamSpec * pspec)
2192 GstBaseTransform *trans;
2194 trans = GST_BASE_TRANSFORM (object);
2198 gst_base_transform_set_qos_enabled (trans, g_value_get_boolean (value));
2201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2207 gst_base_transform_get_property (GObject * object, guint prop_id,
2208 GValue * value, GParamSpec * pspec)
2210 GstBaseTransform *trans;
2212 trans = GST_BASE_TRANSFORM (object);
2216 g_value_set_boolean (value, gst_base_transform_is_qos_enabled (trans));
2219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2224 /* not a vmethod of anything, just an internal method */
2226 gst_base_transform_activate (GstBaseTransform * trans, gboolean active)
2228 GstBaseTransformClass *bclass;
2229 gboolean result = TRUE;
2231 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2234 if (trans->priv->pad_mode == GST_ACTIVATE_NONE && bclass->start)
2235 result &= bclass->start (trans);
2237 GST_OBJECT_LOCK (trans);
2239 if (GST_PAD_CAPS (trans->sinkpad) && GST_PAD_CAPS (trans->srcpad))
2240 trans->have_same_caps =
2241 gst_caps_is_equal (GST_PAD_CAPS (trans->sinkpad),
2242 GST_PAD_CAPS (trans->srcpad)) || trans->passthrough;
2244 trans->have_same_caps = trans->passthrough;
2245 GST_DEBUG_OBJECT (trans, "have_same_caps %d", trans->have_same_caps);
2246 trans->negotiated = FALSE;
2247 trans->have_newsegment = FALSE;
2248 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
2249 trans->priv->last_stop_out = GST_CLOCK_TIME_NONE;
2250 trans->priv->proportion = 1.0;
2251 trans->priv->earliest_time = -1;
2252 trans->priv->discont = FALSE;
2253 gst_caps_replace (&trans->priv->sink_suggest, NULL);
2254 trans->priv->processed = 0;
2255 trans->priv->dropped = 0;
2256 trans->priv->force_alloc = TRUE;
2258 GST_OBJECT_UNLOCK (trans);
2260 /* We must make sure streaming has finished before resetting things
2261 * and calling the ::stop vfunc */
2262 GST_PAD_STREAM_LOCK (trans->sinkpad);
2263 GST_PAD_STREAM_UNLOCK (trans->sinkpad);
2265 trans->have_same_caps = FALSE;
2266 /* We can only reset the passthrough mode if the instance told us to
2267 handle it in configure_caps */
2268 if (bclass->passthrough_on_same_caps) {
2269 gst_base_transform_set_passthrough (trans, FALSE);
2271 gst_caps_replace (&trans->cache_caps1, NULL);
2272 gst_caps_replace (&trans->cache_caps2, NULL);
2273 gst_caps_replace (&trans->priv->sink_alloc, NULL);
2274 gst_caps_replace (&trans->priv->sink_suggest, NULL);
2276 if (trans->priv->pad_mode != GST_ACTIVATE_NONE && bclass->stop)
2277 result &= bclass->stop (trans);
2284 gst_base_transform_sink_activate_push (GstPad * pad, gboolean active)
2286 gboolean result = TRUE;
2287 GstBaseTransform *trans;
2289 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2291 result = gst_base_transform_activate (trans, active);
2294 trans->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
2296 gst_object_unref (trans);
2302 gst_base_transform_src_activate_pull (GstPad * pad, gboolean active)
2304 gboolean result = FALSE;
2305 GstBaseTransform *trans;
2307 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2309 result = gst_pad_activate_pull (trans->sinkpad, active);
2312 result &= gst_base_transform_activate (trans, active);
2315 trans->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
2317 gst_object_unref (trans);
2323 * gst_base_transform_set_passthrough:
2324 * @trans: the #GstBaseTransform to set
2325 * @passthrough: boolean indicating passthrough mode.
2327 * Set passthrough mode for this filter by default. This is mostly
2328 * useful for filters that do not care about negotiation.
2330 * Always TRUE for filters which don't implement either a transform
2331 * or transform_ip method.
2336 gst_base_transform_set_passthrough (GstBaseTransform * trans,
2337 gboolean passthrough)
2339 GstBaseTransformClass *bclass;
2341 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2343 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2345 GST_OBJECT_LOCK (trans);
2346 if (passthrough == FALSE) {
2347 if (bclass->transform_ip || bclass->transform)
2348 trans->passthrough = FALSE;
2350 trans->passthrough = TRUE;
2353 GST_DEBUG_OBJECT (trans, "set passthrough %d", trans->passthrough);
2354 GST_OBJECT_UNLOCK (trans);
2358 * gst_base_transform_is_passthrough:
2359 * @trans: the #GstBaseTransform to query
2361 * See if @trans is configured as a passthrough transform.
2363 * Returns: TRUE is the transform is configured in passthrough mode.
2368 gst_base_transform_is_passthrough (GstBaseTransform * trans)
2372 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2374 GST_OBJECT_LOCK (trans);
2375 result = trans->passthrough;
2376 GST_OBJECT_UNLOCK (trans);
2382 * gst_base_transform_set_in_place:
2383 * @trans: the #GstBaseTransform to modify
2384 * @in_place: Boolean value indicating that we would like to operate
2385 * on in_place buffers.
2387 * Determines whether a non-writable buffer will be copied before passing
2388 * to the transform_ip function.
2390 * <listitem>Always TRUE if no transform function is implemented.</listitem>
2391 * <listitem>Always FALSE if ONLY transform function is implemented.</listitem>
2397 gst_base_transform_set_in_place (GstBaseTransform * trans, gboolean in_place)
2399 GstBaseTransformClass *bclass;
2401 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2403 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2405 GST_OBJECT_LOCK (trans);
2408 if (bclass->transform_ip) {
2409 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
2410 trans->always_in_place = TRUE;
2413 if (bclass->transform) {
2414 GST_DEBUG_OBJECT (trans, "setting in_place FALSE");
2415 trans->always_in_place = FALSE;
2419 GST_OBJECT_UNLOCK (trans);
2423 * gst_base_transform_is_in_place:
2424 * @trans: the #GstBaseTransform to query
2426 * See if @trans is configured as a in_place transform.
2428 * Returns: TRUE is the transform is configured in in_place mode.
2433 gst_base_transform_is_in_place (GstBaseTransform * trans)
2437 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2439 GST_OBJECT_LOCK (trans);
2440 result = trans->always_in_place;
2441 GST_OBJECT_UNLOCK (trans);
2447 * gst_base_transform_update_qos:
2448 * @trans: a #GstBaseTransform
2449 * @proportion: the proportion
2450 * @diff: the diff against the clock
2451 * @timestamp: the timestamp of the buffer generating the QoS expressed in
2454 * Set the QoS parameters in the transform. This function is called internally
2455 * when a QOS event is received but subclasses can provide custom information
2463 gst_base_transform_update_qos (GstBaseTransform * trans,
2464 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2467 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2469 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans,
2470 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2471 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2473 GST_OBJECT_LOCK (trans);
2474 trans->priv->proportion = proportion;
2475 trans->priv->earliest_time = timestamp + diff;
2476 GST_OBJECT_UNLOCK (trans);
2480 * gst_base_transform_set_qos_enabled:
2481 * @trans: a #GstBaseTransform
2482 * @enabled: new state
2484 * Enable or disable QoS handling in the transform.
2491 gst_base_transform_set_qos_enabled (GstBaseTransform * trans, gboolean enabled)
2493 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2495 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "enabled: %d", enabled);
2497 GST_OBJECT_LOCK (trans);
2498 trans->priv->qos_enabled = enabled;
2499 GST_OBJECT_UNLOCK (trans);
2503 * gst_base_transform_is_qos_enabled:
2504 * @trans: a #GstBaseTransform
2506 * Queries if the transform will handle QoS.
2508 * Returns: TRUE if QoS is enabled.
2515 gst_base_transform_is_qos_enabled (GstBaseTransform * trans)
2519 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2521 GST_OBJECT_LOCK (trans);
2522 result = trans->priv->qos_enabled;
2523 GST_OBJECT_UNLOCK (trans);
2529 * gst_base_transform_set_gap_aware:
2530 * @trans: a #GstBaseTransform
2531 * @gap_aware: New state
2533 * If @gap_aware is %FALSE (the default), output buffers will have the
2534 * %GST_BUFFER_FLAG_GAP flag unset.
2536 * If set to %TRUE, the element must handle output buffers with this flag set
2537 * correctly, i.e. it can assume that the buffer contains neutral data but must
2538 * unset the flag if the output is no neutral data.
2545 gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware)
2547 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2549 GST_OBJECT_LOCK (trans);
2550 trans->priv->gap_aware = gap_aware;
2551 GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware);
2552 GST_OBJECT_UNLOCK (trans);
2556 * gst_base_transform_suggest:
2557 * @trans: a #GstBaseTransform
2558 * @caps: (transfer none): caps to suggest
2559 * @size: buffer size to suggest
2561 * Instructs @trans to suggest new @caps upstream. A copy of @caps will be
2567 gst_base_transform_suggest (GstBaseTransform * trans, GstCaps * caps,
2570 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2572 GST_OBJECT_LOCK (trans->sinkpad);
2573 if (trans->priv->sink_suggest)
2574 gst_caps_unref (trans->priv->sink_suggest);
2576 caps = gst_caps_copy (caps);
2577 trans->priv->sink_suggest = caps;
2578 trans->priv->size_suggest = size;
2579 trans->priv->suggest_pending = TRUE;
2580 GST_DEBUG_OBJECT (trans, "new suggest %" GST_PTR_FORMAT, caps);
2581 GST_OBJECT_UNLOCK (trans->sinkpad);
2585 * gst_base_transform_reconfigure:
2586 * @trans: a #GstBaseTransform
2588 * Instructs @trans to renegotiate a new downstream transform on the next
2589 * buffer. This function is typically called after properties on the transform
2590 * were set that influence the output format.
2595 gst_base_transform_reconfigure (GstBaseTransform * trans)
2597 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2599 GST_OBJECT_LOCK (trans);
2600 GST_DEBUG_OBJECT (trans, "marking reconfigure");
2601 trans->priv->reconfigure = TRUE;
2602 gst_caps_replace (&trans->priv->sink_alloc, NULL);
2603 GST_OBJECT_UNLOCK (trans);