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, guint * 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 GstFlowReturn gst_base_transform_buffer_alloc (GstPad * pad,
351 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
352 static gboolean gst_base_transform_query (GstPad * pad, GstQuery * query);
353 static gboolean gst_base_transform_default_query (GstBaseTransform * trans,
354 GstPadDirection direction, GstQuery * query);
355 static const GstQueryType *gst_base_transform_query_type (GstPad * pad);
357 /* static guint gst_base_transform_signals[LAST_SIGNAL] = { 0 }; */
360 gst_base_transform_finalize (GObject * object)
362 GstBaseTransform *trans;
364 trans = GST_BASE_TRANSFORM (object);
366 gst_caps_replace (&trans->priv->sink_suggest, NULL);
367 g_mutex_free (trans->transform_lock);
369 G_OBJECT_CLASS (parent_class)->finalize (object);
373 gst_base_transform_class_init (GstBaseTransformClass * klass)
375 GObjectClass *gobject_class;
377 gobject_class = G_OBJECT_CLASS (klass);
379 GST_DEBUG_CATEGORY_INIT (gst_base_transform_debug, "basetransform", 0,
380 "basetransform element");
382 GST_DEBUG ("gst_base_transform_class_init");
384 g_type_class_add_private (klass, sizeof (GstBaseTransformPrivate));
386 parent_class = g_type_class_peek_parent (klass);
388 gobject_class->set_property = gst_base_transform_set_property;
389 gobject_class->get_property = gst_base_transform_get_property;
391 g_object_class_install_property (gobject_class, PROP_QOS,
392 g_param_spec_boolean ("qos", "QoS", "Handle Quality-of-Service events",
393 DEFAULT_PROP_QOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
395 gobject_class->finalize = gst_base_transform_finalize;
397 klass->passthrough_on_same_caps = FALSE;
398 klass->event = GST_DEBUG_FUNCPTR (gst_base_transform_sink_eventfunc);
399 klass->src_event = GST_DEBUG_FUNCPTR (gst_base_transform_src_eventfunc);
401 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps_default);
402 klass->query = GST_DEBUG_FUNCPTR (gst_base_transform_default_query);
406 gst_base_transform_init (GstBaseTransform * trans,
407 GstBaseTransformClass * bclass)
409 GstPadTemplate *pad_template;
411 GST_DEBUG ("gst_base_transform_init");
413 trans->priv = GST_BASE_TRANSFORM_GET_PRIVATE (trans);
416 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
417 g_return_if_fail (pad_template != NULL);
418 trans->sinkpad = gst_pad_new_from_template (pad_template, "sink");
419 gst_pad_set_getcaps_function (trans->sinkpad,
420 GST_DEBUG_FUNCPTR (gst_base_transform_getcaps));
421 gst_pad_set_acceptcaps_function (trans->sinkpad,
422 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps));
423 gst_pad_set_setcaps_function (trans->sinkpad,
424 GST_DEBUG_FUNCPTR (gst_base_transform_setcaps));
425 gst_pad_set_event_function (trans->sinkpad,
426 GST_DEBUG_FUNCPTR (gst_base_transform_sink_event));
427 gst_pad_set_chain_function (trans->sinkpad,
428 GST_DEBUG_FUNCPTR (gst_base_transform_chain));
429 gst_pad_set_activatepush_function (trans->sinkpad,
430 GST_DEBUG_FUNCPTR (gst_base_transform_sink_activate_push));
431 gst_pad_set_bufferalloc_function (trans->sinkpad,
432 GST_DEBUG_FUNCPTR (gst_base_transform_buffer_alloc));
433 gst_pad_set_query_function (trans->sinkpad,
434 GST_DEBUG_FUNCPTR (gst_base_transform_query));
435 gst_pad_set_query_type_function (trans->sinkpad,
436 GST_DEBUG_FUNCPTR (gst_base_transform_query_type));
437 gst_element_add_pad (GST_ELEMENT (trans), trans->sinkpad);
440 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
441 g_return_if_fail (pad_template != NULL);
442 trans->srcpad = gst_pad_new_from_template (pad_template, "src");
443 gst_pad_set_getcaps_function (trans->srcpad,
444 GST_DEBUG_FUNCPTR (gst_base_transform_getcaps));
445 gst_pad_set_acceptcaps_function (trans->srcpad,
446 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps));
447 gst_pad_set_event_function (trans->srcpad,
448 GST_DEBUG_FUNCPTR (gst_base_transform_src_event));
449 gst_pad_set_checkgetrange_function (trans->srcpad,
450 GST_DEBUG_FUNCPTR (gst_base_transform_check_get_range));
451 gst_pad_set_getrange_function (trans->srcpad,
452 GST_DEBUG_FUNCPTR (gst_base_transform_getrange));
453 gst_pad_set_activatepull_function (trans->srcpad,
454 GST_DEBUG_FUNCPTR (gst_base_transform_src_activate_pull));
455 gst_pad_set_query_function (trans->srcpad,
456 GST_DEBUG_FUNCPTR (gst_base_transform_query));
457 gst_pad_set_query_type_function (trans->srcpad,
458 GST_DEBUG_FUNCPTR (gst_base_transform_query_type));
459 gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
461 trans->transform_lock = g_mutex_new ();
462 trans->pending_configure = FALSE;
463 trans->priv->qos_enabled = DEFAULT_PROP_QOS;
464 trans->cache_caps1 = NULL;
465 trans->cache_caps2 = NULL;
466 trans->priv->pad_mode = GST_ACTIVATE_NONE;
467 trans->priv->gap_aware = FALSE;
469 trans->passthrough = FALSE;
470 if (bclass->transform == NULL) {
471 /* If no transform function, always_in_place is TRUE */
472 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
473 trans->always_in_place = TRUE;
475 if (bclass->transform_ip == NULL) {
476 GST_DEBUG_OBJECT (trans, "setting passthrough TRUE");
477 trans->passthrough = TRUE;
481 trans->priv->processed = 0;
482 trans->priv->dropped = 0;
483 trans->priv->force_alloc = TRUE;
486 /* given @caps on the src or sink pad (given by @direction)
487 * calculate the possible caps on the other pad.
489 * Returns new caps, unref after usage.
492 gst_base_transform_transform_caps (GstBaseTransform * trans,
493 GstPadDirection direction, GstCaps * caps)
496 GstBaseTransformClass *klass;
501 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
503 /* if there is a custom transform function, use this */
504 if (klass->transform_caps) {
508 /* start with empty caps */
509 ret = gst_caps_new_empty ();
510 GST_DEBUG_OBJECT (trans, "transform caps (direction = %d)", direction);
512 if (gst_caps_is_any (caps)) {
513 /* for any caps we still have to call the transform function */
514 GST_DEBUG_OBJECT (trans, "from: ANY");
515 temp = klass->transform_caps (trans, direction, caps);
516 GST_DEBUG_OBJECT (trans, " to: %" GST_PTR_FORMAT, temp);
518 temp = gst_caps_make_writable (temp);
519 gst_caps_append (ret, temp);
521 gint n = gst_caps_get_size (caps);
522 /* we send caps with just one structure to the transform
523 * function as this is easier for the element */
524 for (i = 0; i < n; i++) {
527 nth = gst_caps_copy_nth (caps, i);
528 GST_LOG_OBJECT (trans, "from[%d]: %" GST_PTR_FORMAT, i, nth);
529 temp = klass->transform_caps (trans, direction, nth);
530 gst_caps_unref (nth);
531 GST_LOG_OBJECT (trans, " to[%d]: %" GST_PTR_FORMAT, i, temp);
533 temp = gst_caps_make_writable (temp);
535 /* here we need to only append those structures, that are not yet
536 * in there, we use the merge function for this */
537 gst_caps_merge (ret, temp);
539 GST_LOG_OBJECT (trans, " merged[%d]: %" GST_PTR_FORMAT, i, ret);
541 GST_LOG_OBJECT (trans, "merged: (%d)", gst_caps_get_size (ret));
542 /* FIXME: we can't do much simplification here because we don't really want to
543 * change the caps order
544 gst_caps_do_simplify (ret);
545 GST_DEBUG_OBJECT (trans, "simplified: (%d)", gst_caps_get_size (ret));
549 GST_DEBUG_OBJECT (trans, "identity from: %" GST_PTR_FORMAT, caps);
550 /* no transform function, use the identity transform */
551 ret = gst_caps_ref (caps);
554 GST_DEBUG_OBJECT (trans, "to: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (ret),
560 /* transform a buffer of @size with @caps on the pad with @direction to
561 * the size of a buffer with @othercaps and store the result in @othersize
563 * We have two ways of doing this:
564 * 1) use a custom transform size function, this is for complicated custom
565 * cases with no fixed unit_size.
566 * 2) use the unit_size functions where there is a relationship between the
567 * caps and the size of a buffer.
570 gst_base_transform_transform_size (GstBaseTransform * trans,
571 GstPadDirection direction, GstCaps * caps,
572 guint size, GstCaps * othercaps, guint * othersize)
574 guint inunitsize, outunitsize, units;
575 GstBaseTransformClass *klass;
578 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
580 GST_DEBUG_OBJECT (trans, "asked to transform size %d for caps %"
581 GST_PTR_FORMAT " to size for caps %" GST_PTR_FORMAT " in direction %s",
582 size, caps, othercaps, direction == GST_PAD_SRC ? "SRC" : "SINK");
584 if (klass->transform_size) {
585 /* if there is a custom transform function, use this */
586 ret = klass->transform_size (trans, direction, caps, size, othercaps,
588 } else if (klass->get_unit_size == NULL) {
589 /* if there is no transform_size and no unit_size, it means the
590 * element does not modify the size of a buffer */
594 /* there is no transform_size function, we have to use the unit_size
595 * functions. This method assumes there is a fixed unit_size associated with
596 * each caps. We provide the same amount of units on both sides. */
597 if (!gst_base_transform_get_unit_size (trans, caps, &inunitsize))
600 GST_DEBUG_OBJECT (trans, "input size %d, input unit size %d", size,
603 /* input size must be a multiple of the unit_size of the input caps */
604 if (inunitsize == 0 || (size % inunitsize != 0))
607 /* get the amount of units */
608 units = size / inunitsize;
610 /* now get the unit size of the output */
611 if (!gst_base_transform_get_unit_size (trans, othercaps, &outunitsize))
614 /* the output size is the unit_size times the amount of units on the
616 *othersize = units * outunitsize;
617 GST_DEBUG_OBJECT (trans, "transformed size to %d", *othersize);
626 GST_DEBUG_OBJECT (trans, "could not get in_size");
627 g_warning ("%s: could not get in_size", GST_ELEMENT_NAME (trans));
632 GST_DEBUG_OBJECT (trans, "Size %u is not a multiple of unit size %u", size,
634 g_warning ("%s: size %u is not a multiple of unit size %u",
635 GST_ELEMENT_NAME (trans), size, inunitsize);
640 GST_DEBUG_OBJECT (trans, "could not get out_size");
641 g_warning ("%s: could not get out_size", GST_ELEMENT_NAME (trans));
646 /* get the caps that can be handled by @pad. We perform:
648 * - take the caps of peer of otherpad,
649 * - filter against the padtemplate of otherpad,
650 * - calculate all transforms of remaining caps
651 * - filter against template of @pad
653 * If there is no peer, we simply return the caps of the padtemplate of pad.
656 gst_base_transform_getcaps (GstPad * pad)
658 GstBaseTransform *trans;
660 const GstCaps *templ;
661 GstCaps *peercaps, *caps, *temp;
663 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
665 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
667 /* we can do what the peer can */
668 peercaps = gst_pad_peer_get_caps_reffed (otherpad);
670 GST_DEBUG_OBJECT (pad, "peer caps %" GST_PTR_FORMAT, peercaps);
672 /* filtered against our padtemplate on the other side */
673 templ = gst_pad_get_pad_template_caps (otherpad);
674 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
675 temp = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
676 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
678 temp = gst_caps_copy (gst_pad_get_pad_template_caps (otherpad));
679 GST_DEBUG_OBJECT (pad, "no peer, using our template caps %" GST_PTR_FORMAT,
683 /* then see what we can transform this to */
684 caps = gst_base_transform_transform_caps (trans,
685 GST_PAD_DIRECTION (otherpad), temp);
686 GST_DEBUG_OBJECT (pad, "transformed %" GST_PTR_FORMAT, caps);
687 gst_caps_unref (temp);
691 /* and filter against the template of this pad */
692 templ = gst_pad_get_pad_template_caps (pad);
693 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
694 /* We keep the caps sorted like the returned caps */
695 temp = gst_caps_intersect_full (caps, templ, GST_CAPS_INTERSECT_FIRST);
696 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
697 gst_caps_unref (caps);
701 /* Now try if we can put the untransformed downstream caps first */
702 temp = gst_caps_intersect_full (peercaps, caps, GST_CAPS_INTERSECT_FIRST);
703 if (!gst_caps_is_empty (temp)) {
704 gst_caps_merge (temp, caps);
707 gst_caps_unref (temp);
712 GST_DEBUG_OBJECT (trans, "returning %" GST_PTR_FORMAT, caps);
715 gst_caps_unref (peercaps);
717 gst_object_unref (trans);
722 /* function triggered when the in and out caps are negotiated and need
723 * to be configured in the subclass. */
725 gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
729 GstBaseTransformClass *klass;
731 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
733 GST_DEBUG_OBJECT (trans, "in caps: %" GST_PTR_FORMAT, in);
734 GST_DEBUG_OBJECT (trans, "out caps: %" GST_PTR_FORMAT, out);
736 /* clear the cache */
737 gst_caps_replace (&trans->cache_caps1, NULL);
738 gst_caps_replace (&trans->cache_caps2, NULL);
740 /* figure out same caps state */
741 trans->have_same_caps = gst_caps_is_equal (in, out);
742 GST_DEBUG_OBJECT (trans, "have_same_caps: %d", trans->have_same_caps);
744 /* If we've a transform_ip method and same input/output caps, set in_place
745 * by default. If for some reason the sub-class prefers using a transform
746 * function, it can clear the in place flag in the set_caps */
747 gst_base_transform_set_in_place (trans,
748 klass->transform_ip && trans->have_same_caps);
750 /* Set the passthrough if the class wants passthrough_on_same_caps
751 * and we have the same caps on each pad */
752 if (klass->passthrough_on_same_caps)
753 gst_base_transform_set_passthrough (trans, trans->have_same_caps);
755 /* now configure the element with the caps */
756 if (klass->set_caps) {
757 GST_DEBUG_OBJECT (trans, "Calling set_caps method to setup caps");
758 ret = klass->set_caps (trans, in, out);
761 GST_OBJECT_LOCK (trans);
762 /* make sure we reevaluate how the buffer_alloc works wrt to proxy allocating
763 * the buffer. FIXME, this triggers some quite heavy codepaths that don't need
765 trans->priv->suggest_pending = TRUE;
766 GST_OBJECT_UNLOCK (trans);
767 trans->negotiated = ret;
772 /* check if caps @in on @pad can be transformed to @out on the other pad.
773 * We don't have a vmethod to test this yet so we have to do a somewhat less
774 * efficient check for this.
777 gst_base_transform_can_transform (GstBaseTransform * trans, GstPad * pad,
778 GstCaps * in, GstCaps * out)
782 /* convert the in caps to all possible out caps */
784 gst_base_transform_transform_caps (trans, GST_PAD_DIRECTION (pad), in);
786 /* check if transform is empty */
787 if (!othercaps || gst_caps_is_empty (othercaps))
790 /* check if the out caps is a subset of the othercaps */
791 if (!gst_caps_can_intersect (out, othercaps))
795 gst_caps_unref (othercaps);
797 GST_DEBUG_OBJECT (trans, "from %" GST_PTR_FORMAT, in);
798 GST_DEBUG_OBJECT (trans, "to %" GST_PTR_FORMAT, out);
805 GST_DEBUG_OBJECT (trans,
806 "transform returned useless %" GST_PTR_FORMAT, othercaps);
808 gst_caps_unref (othercaps);
813 GST_DEBUG_OBJECT (trans, "no subset");
815 gst_caps_unref (othercaps);
820 /* given a fixed @caps on @pad, create the best possible caps for the
822 * @caps must be fixed when calling this function.
824 * This function calls the transform caps vmethod of the basetransform to figure
825 * out the possible target formats. It then tries to select the best format from
828 * - attempt passthrough if the target caps is a superset of the input caps
829 * - fixating by using peer caps
830 * - fixating with transform fixate function
831 * - fixating with pad fixate functions.
833 * this function returns a caps that can be transformed into and is accepted by
837 gst_base_transform_find_transform (GstBaseTransform * trans, GstPad * pad,
840 GstBaseTransformClass *klass;
841 GstPad *otherpad, *otherpeer;
843 gboolean peer_checked = FALSE;
846 /* caps must be fixed here, this is a programming error if it's not */
847 g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
849 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
851 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
852 otherpeer = gst_pad_get_peer (otherpad);
854 /* see how we can transform the input caps. We need to do this even for
855 * passthrough because it might be possible that this element cannot support
856 * passthrough at all. */
857 othercaps = gst_base_transform_transform_caps (trans,
858 GST_PAD_DIRECTION (pad), caps);
860 /* The caps we can actually output is the intersection of the transformed
861 * caps with the pad template for the pad */
864 const GstCaps *templ_caps;
866 templ_caps = gst_pad_get_pad_template_caps (otherpad);
867 GST_DEBUG_OBJECT (trans,
868 "intersecting against padtemplate %" GST_PTR_FORMAT, templ_caps);
871 gst_caps_intersect_full (othercaps, templ_caps,
872 GST_CAPS_INTERSECT_FIRST);
874 gst_caps_unref (othercaps);
875 othercaps = intersect;
878 /* check if transform is empty */
879 if (!othercaps || gst_caps_is_empty (othercaps))
882 /* if the othercaps are not fixed, we need to fixate them, first attempt
883 * is by attempting passthrough if the othercaps are a superset of caps. */
884 /* FIXME. maybe the caps is not fixed because it has multiple structures of
886 is_fixed = gst_caps_is_fixed (othercaps);
888 GST_DEBUG_OBJECT (trans,
889 "transform returned non fixed %" GST_PTR_FORMAT, othercaps);
891 /* see if the target caps are a superset of the source caps, in this
892 * case we can try to perform passthrough */
893 if (gst_caps_can_intersect (othercaps, caps)) {
894 GST_DEBUG_OBJECT (trans, "try passthrough with %" GST_PTR_FORMAT, caps);
896 /* try passthrough. we know it's fixed, because caps is fixed */
897 if (gst_pad_accept_caps (otherpeer, caps)) {
898 GST_DEBUG_OBJECT (trans, "peer accepted %" GST_PTR_FORMAT, caps);
899 /* peer accepted unmodified caps, we free the original non-fixed
900 * caps and work with the passthrough caps */
901 gst_caps_unref (othercaps);
902 othercaps = gst_caps_ref (caps);
904 /* mark that we checked othercaps with the peer, this
905 * makes sure we don't call accept_caps again with these same
909 GST_DEBUG_OBJECT (trans,
910 "peer did not accept %" GST_PTR_FORMAT, caps);
913 GST_DEBUG_OBJECT (trans, "no peer, doing passthrough");
914 gst_caps_unref (othercaps);
915 othercaps = gst_caps_ref (caps);
921 /* second attempt at fixation is done by intersecting with
923 if (!is_fixed && otherpeer) {
924 /* intersect against what the peer can do */
928 GST_DEBUG_OBJECT (trans, "othercaps now %" GST_PTR_FORMAT, othercaps);
930 peercaps = gst_pad_get_caps_reffed (otherpeer);
931 intersect = gst_caps_intersect (peercaps, othercaps);
932 gst_caps_unref (peercaps);
933 gst_caps_unref (othercaps);
934 othercaps = intersect;
935 peer_checked = FALSE;
937 is_fixed = gst_caps_is_fixed (othercaps);
939 GST_DEBUG_OBJECT (trans,
940 "filtering against peer yields %" GST_PTR_FORMAT, othercaps);
943 if (gst_caps_is_empty (othercaps))
944 goto no_transform_possible;
946 /* third attempt at fixation, call the fixate vmethod and
947 * ultimately call the pad fixate function. */
949 GST_DEBUG_OBJECT (trans,
950 "trying to fixate %" GST_PTR_FORMAT " on pad %s:%s",
951 othercaps, GST_DEBUG_PAD_NAME (otherpad));
953 /* since we have no other way to fixate left, we might as well just take
954 * the first of the caps list and fixate that */
956 /* FIXME: when fixating using the vmethod, it might make sense to fixate
957 * each of the caps; but Wim doesn't see a use case for that yet */
958 gst_caps_truncate (othercaps);
959 peer_checked = FALSE;
961 if (klass->fixate_caps) {
962 GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
963 " using caps %" GST_PTR_FORMAT
964 " on pad %s:%s using fixate_caps vmethod", othercaps, caps,
965 GST_DEBUG_PAD_NAME (otherpad));
966 klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
967 is_fixed = gst_caps_is_fixed (othercaps);
969 /* if still not fixed, no other option but to let the default pad fixate
970 * function do its job */
972 GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
973 " on pad %s:%s using gst_pad_fixate_caps", othercaps,
974 GST_DEBUG_PAD_NAME (otherpad));
975 gst_pad_fixate_caps (otherpad, othercaps);
976 is_fixed = gst_caps_is_fixed (othercaps);
978 GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
980 GST_DEBUG ("caps are fixed");
981 /* else caps are fixed but the subclass may want to add fields */
982 if (klass->fixate_caps) {
983 othercaps = gst_caps_make_writable (othercaps);
985 GST_DEBUG_OBJECT (trans, "doing fixate %" GST_PTR_FORMAT
986 " using caps %" GST_PTR_FORMAT
987 " on pad %s:%s using fixate_caps vmethod", othercaps, caps,
988 GST_DEBUG_PAD_NAME (otherpad));
990 klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
991 is_fixed = gst_caps_is_fixed (othercaps);
995 /* caps should be fixed now, if not we have to fail. */
997 goto could_not_fixate;
999 /* and peer should accept, don't check again if we already checked the
1000 * othercaps against the peer. */
1001 if (!peer_checked && otherpeer && !gst_pad_accept_caps (otherpeer, othercaps))
1002 goto peer_no_accept;
1004 GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT
1005 ", and got final caps %" GST_PTR_FORMAT, caps, othercaps);
1008 gst_object_unref (otherpeer);
1015 GST_DEBUG_OBJECT (trans,
1016 "transform returned useless %" GST_PTR_FORMAT, othercaps);
1019 no_transform_possible:
1021 GST_DEBUG_OBJECT (trans,
1022 "transform could not transform %" GST_PTR_FORMAT
1023 " in anything we support", caps);
1028 GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
1033 GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
1034 " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1040 gst_object_unref (otherpeer);
1042 gst_caps_unref (othercaps);
1048 gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
1049 GstPadDirection direction, GstCaps * caps)
1053 GstCaps *othercaps = NULL;
1055 gboolean ret = TRUE;
1058 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1060 /* we need fixed caps for the check, fall back to the default implementation
1062 if (!gst_caps_is_fixed (caps))
1067 GST_DEBUG_OBJECT (trans, "non fixed accept caps %" GST_PTR_FORMAT, caps);
1069 /* get all the formats we can handle on this pad */
1070 if (direction == GST_PAD_SRC)
1071 allowed = gst_pad_get_caps_reffed (trans->srcpad);
1073 allowed = gst_pad_get_caps_reffed (trans->sinkpad);
1076 GST_DEBUG_OBJECT (trans, "gst_pad_get_caps() failed");
1077 goto no_transform_possible;
1080 GST_DEBUG_OBJECT (trans, "allowed caps %" GST_PTR_FORMAT, allowed);
1082 /* intersect with the requested format */
1083 ret = gst_caps_can_intersect (allowed, caps);
1084 gst_caps_unref (allowed);
1087 goto no_transform_possible;
1091 GST_DEBUG_OBJECT (pad, "accept caps %" GST_PTR_FORMAT, caps);
1093 /* find best possible caps for the other pad as a way to see if we can
1094 * transform this caps. */
1095 othercaps = gst_base_transform_find_transform (trans, pad, caps);
1096 if (!othercaps || gst_caps_is_empty (othercaps))
1097 goto no_transform_possible;
1099 GST_DEBUG_OBJECT (pad, "we can transform to %" GST_PTR_FORMAT, othercaps);
1105 /* We know it's always NULL since we never use it */
1107 gst_caps_unref (othercaps);
1113 no_transform_possible:
1115 GST_DEBUG_OBJECT (trans,
1116 "transform could not transform %" GST_PTR_FORMAT
1117 " in anything we support", caps);
1124 gst_base_transform_acceptcaps (GstPad * pad, GstCaps * caps)
1126 gboolean ret = TRUE;
1127 GstBaseTransform *trans;
1128 GstBaseTransformClass *bclass;
1130 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1131 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1133 if (bclass->accept_caps)
1134 ret = bclass->accept_caps (trans, GST_PAD_DIRECTION (pad), caps);
1136 gst_object_unref (trans);
1141 /* called when new caps arrive on the sink or source pad,
1142 * We try to find the best caps for the other side using our _find_transform()
1143 * function. If there are caps, we configure the transform for this new
1146 * FIXME, this function is currently commutative but this should not really be
1147 * because we never set caps starting from the srcpad.
1150 gst_base_transform_setcaps (GstPad * pad, GstCaps * caps)
1152 GstBaseTransform *trans;
1153 GstPad *otherpad, *otherpeer;
1154 GstCaps *othercaps = NULL;
1155 gboolean ret = TRUE;
1156 GstCaps *incaps, *outcaps;
1158 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1160 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1161 otherpeer = gst_pad_get_peer (otherpad);
1163 /* if we get called recursively, we bail out now to avoid an
1165 if (GST_PAD_IS_IN_SETCAPS (otherpad))
1168 GST_DEBUG_OBJECT (pad, "have new caps %p %" GST_PTR_FORMAT, caps, caps);
1170 /* find best possible caps for the other pad */
1171 othercaps = gst_base_transform_find_transform (trans, pad, caps);
1172 if (!othercaps || gst_caps_is_empty (othercaps))
1173 goto no_transform_possible;
1175 /* configure the element now */
1176 /* make sure in and out caps are correct */
1177 if (pad == trans->sinkpad) {
1179 outcaps = othercaps;
1185 /* if we have the same caps, we can optimize and reuse the input caps */
1186 if (gst_caps_is_equal (incaps, outcaps)) {
1187 GST_INFO_OBJECT (trans, "reuse caps");
1188 gst_caps_unref (othercaps);
1189 outcaps = othercaps = gst_caps_ref (incaps);
1192 /* call configure now */
1193 if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
1194 goto failed_configure;
1196 /* we know this will work, we implement the setcaps */
1197 gst_pad_set_caps (otherpad, othercaps);
1199 if (pad == trans->srcpad && trans->priv->pad_mode == GST_ACTIVATE_PULL) {
1201 ret &= gst_pad_set_caps (otherpeer, othercaps);
1203 GST_INFO_OBJECT (trans, "otherpeer setcaps(%" GST_PTR_FORMAT ") failed",
1209 /* new caps, force alloc on next buffer on the chain */
1210 trans->priv->force_alloc = TRUE;
1212 gst_object_unref (otherpeer);
1214 gst_caps_unref (othercaps);
1216 trans->negotiated = ret;
1218 gst_object_unref (trans);
1223 no_transform_possible:
1225 GST_WARNING_OBJECT (trans,
1226 "transform could not transform %" GST_PTR_FORMAT
1227 " in anything we support", caps);
1233 GST_WARNING_OBJECT (trans, "FAILED to configure caps %" GST_PTR_FORMAT
1234 " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1241 gst_base_transform_default_query (GstBaseTransform * trans,
1242 GstPadDirection direction, GstQuery * query)
1244 gboolean ret = FALSE;
1247 otherpad = (direction == GST_PAD_SRC) ? trans->sinkpad : trans->srcpad;
1249 switch (GST_QUERY_TYPE (query)) {
1250 case GST_QUERY_POSITION:{
1253 gst_query_parse_position (query, &format, NULL);
1254 if (format == GST_FORMAT_TIME && trans->segment.format == GST_FORMAT_TIME) {
1258 if ((direction == GST_PAD_SINK)
1259 || (trans->priv->last_stop_out == GST_CLOCK_TIME_NONE)) {
1261 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1262 trans->segment.last_stop);
1264 pos = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1265 trans->priv->last_stop_out);
1267 gst_query_set_position (query, format, pos);
1269 ret = gst_pad_peer_query (otherpad, query);
1274 ret = gst_pad_peer_query (otherpad, query);
1282 gst_base_transform_query (GstPad * pad, GstQuery * query)
1284 GstBaseTransform *trans;
1285 GstBaseTransformClass *bclass;
1288 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1289 if (G_UNLIKELY (trans == NULL))
1292 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1295 ret = bclass->query (trans, GST_PAD_DIRECTION (pad), query);
1297 ret = gst_pad_query_default (pad, query);
1299 gst_object_unref (trans);
1304 static const GstQueryType *
1305 gst_base_transform_query_type (GstPad * pad)
1307 static const GstQueryType types[] = {
1316 compute_upstream_suggestion (GstBaseTransform * trans, guint expsize,
1320 GstBaseTransformPrivate *priv = trans->priv;
1322 GST_DEBUG_OBJECT (trans, "trying to find upstream suggestion");
1324 /* we cannot convert the current buffer but we might be able to suggest a
1325 * new format upstream, try to find what the best format is. */
1326 othercaps = gst_base_transform_find_transform (trans, trans->srcpad, caps);
1329 GST_DEBUG_OBJECT (trans, "incompatible caps, ignoring");
1330 /* we received caps that we cannot transform. Upstream is behaving badly
1331 * because it should have checked if we could handle these caps. We can
1332 * simply ignore these caps and produce a buffer with our original caps. */
1336 GST_DEBUG_OBJECT (trans, "getting size of suggestion");
1338 /* not a subset, we have a new upstream suggestion, remember it and
1339 * allocate a default buffer. First we try to convert the size */
1340 if (gst_base_transform_transform_size (trans,
1341 GST_PAD_SRC, caps, expsize, othercaps, &size_suggest)) {
1343 /* ok, remember the suggestions now */
1344 GST_DEBUG_OBJECT (trans,
1345 "storing new caps and size suggestion of %u and %" GST_PTR_FORMAT,
1346 size_suggest, othercaps);
1348 GST_OBJECT_LOCK (trans->sinkpad);
1349 if (priv->sink_suggest)
1350 gst_caps_unref (priv->sink_suggest);
1351 priv->sink_suggest = gst_caps_ref (othercaps);
1352 priv->size_suggest = size_suggest;
1353 trans->priv->suggest_pending = TRUE;
1354 GST_OBJECT_UNLOCK (trans->sinkpad);
1356 gst_caps_unref (othercaps);
1360 /* Allocate a buffer using gst_pad_alloc_buffer
1362 * This function can do renegotiation on the source pad
1364 * The output buffer is always writable. outbuf can be equal to
1365 * inbuf, the caller should be prepared for this and perform
1366 * appropriate refcounting.
1368 static GstFlowReturn
1369 gst_base_transform_prepare_output_buffer (GstBaseTransform * trans,
1370 GstBuffer * in_buf, GstBuffer ** out_buf)
1372 GstBaseTransformClass *bclass;
1373 GstBaseTransformPrivate *priv;
1374 GstFlowReturn ret = GST_FLOW_OK;
1375 guint outsize, newsize, expsize;
1376 gboolean discard, setcaps, copymeta;
1377 GstCaps *incaps, *oldcaps, *newcaps, *outcaps;
1379 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1385 /* figure out how to allocate a buffer based on the current configuration */
1386 if (trans->passthrough) {
1387 GST_DEBUG_OBJECT (trans, "doing passthrough alloc");
1388 /* passthrough, we don't really need to call pad alloc but we still need to
1389 * in order to get upstream negotiation. The output size is the same as the
1391 outsize = GST_BUFFER_SIZE (in_buf);
1392 /* we always alloc and discard here */
1395 gboolean want_in_place = (bclass->transform_ip != NULL)
1396 && trans->always_in_place;
1398 if (want_in_place) {
1399 GST_DEBUG_OBJECT (trans, "doing inplace alloc");
1400 /* we alloc a buffer of the same size as the input */
1401 outsize = GST_BUFFER_SIZE (in_buf);
1402 /* only discard it when the input was not writable, otherwise, we reuse
1403 * the input buffer. */
1404 discard = gst_buffer_is_writable (in_buf);
1405 GST_DEBUG_OBJECT (trans, "discard: %d", discard);
1407 GST_DEBUG_OBJECT (trans, "getting output size for copy transform");
1408 /* copy transform, figure out the output size */
1409 if (!gst_base_transform_transform_size (trans,
1410 GST_PAD_SINK, GST_PAD_CAPS (trans->sinkpad),
1411 GST_BUFFER_SIZE (in_buf), GST_PAD_CAPS (trans->srcpad),
1415 /* never discard this buffer, we need it for storing the output */
1420 oldcaps = GST_PAD_CAPS (trans->srcpad);
1422 if (bclass->prepare_output_buffer) {
1423 GST_DEBUG_OBJECT (trans,
1424 "calling prepare buffer with caps %p %" GST_PTR_FORMAT, oldcaps,
1427 bclass->prepare_output_buffer (trans, in_buf, outsize, oldcaps,
1430 /* get a new ref to the srcpad caps, the prepare_output_buffer function can
1431 * update the pad caps if it wants */
1432 oldcaps = GST_PAD_CAPS (trans->srcpad);
1435 * decrease refcount again if vmethod returned refcounted in_buf. This
1436 * is because we need to make sure that the buffer is writable for the
1437 * in_place transform. The docs of the vmethod say that you should return
1438 * a reffed inbuf, which is exactly what we don't want :), oh well.. */
1439 if (in_buf == *out_buf)
1440 gst_buffer_unref (in_buf);
1442 /* never discard the buffer from the prepare_buffer method */
1443 if (*out_buf != NULL)
1447 if (ret != GST_FLOW_OK)
1450 if (*out_buf == NULL) {
1451 if (trans->passthrough && !trans->priv->force_alloc) {
1452 GST_DEBUG_OBJECT (trans, "Avoiding pad alloc");
1453 *out_buf = gst_buffer_ref (in_buf);
1455 GST_DEBUG_OBJECT (trans, "doing alloc with caps %" GST_PTR_FORMAT,
1458 ret = gst_pad_alloc_buffer (trans->srcpad,
1459 GST_BUFFER_OFFSET (in_buf), outsize, oldcaps, out_buf);
1460 if (ret != GST_FLOW_OK)
1465 /* must always have a buffer by now */
1466 if (*out_buf == NULL)
1469 /* check if we got different caps on this new output buffer */
1470 newcaps = GST_BUFFER_CAPS (*out_buf);
1471 newsize = GST_BUFFER_SIZE (*out_buf);
1473 if (newcaps && !gst_caps_is_equal (newcaps, oldcaps)) {
1475 gboolean can_convert;
1477 GST_DEBUG_OBJECT (trans, "received new caps %" GST_PTR_FORMAT, newcaps);
1479 incaps = GST_PAD_CAPS (trans->sinkpad);
1481 /* check if we can convert the current incaps to the new target caps */
1483 gst_base_transform_can_transform (trans, trans->sinkpad, incaps,
1487 GST_DEBUG_OBJECT (trans, "cannot perform transform on current buffer");
1489 gst_base_transform_transform_size (trans,
1490 GST_PAD_SINK, incaps, GST_BUFFER_SIZE (in_buf), newcaps, &expsize);
1492 compute_upstream_suggestion (trans, expsize, newcaps);
1494 /* we got a suggested caps but we can't transform to it. See if there is
1495 * another downstream format that we can transform to */
1497 gst_base_transform_find_transform (trans, trans->sinkpad, incaps);
1499 if (othercaps && !gst_caps_is_empty (othercaps)) {
1500 GST_DEBUG_OBJECT (trans, "we found target caps %" GST_PTR_FORMAT,
1502 *out_buf = gst_buffer_make_metadata_writable (*out_buf);
1503 gst_buffer_set_caps (*out_buf, othercaps);
1504 gst_caps_unref (othercaps);
1505 newcaps = GST_BUFFER_CAPS (*out_buf);
1507 } else if (othercaps)
1508 gst_caps_unref (othercaps);
1511 /* it's possible that the buffer we got is of the wrong size, get the
1512 * expected size here, we will check the size if we are going to use the
1513 * buffer later on. */
1514 gst_base_transform_transform_size (trans,
1515 GST_PAD_SINK, incaps, GST_BUFFER_SIZE (in_buf), newcaps, &expsize);
1518 GST_DEBUG_OBJECT (trans, "reconfigure transform for current buffer");
1520 /* subclass might want to add fields to the caps */
1521 if (bclass->fixate_caps != NULL) {
1522 newcaps = gst_caps_copy (newcaps);
1524 GST_DEBUG_OBJECT (trans, "doing fixate %" GST_PTR_FORMAT
1525 " using caps %" GST_PTR_FORMAT
1526 " on pad %s:%s using fixate_caps vmethod", newcaps, incaps,
1527 GST_DEBUG_PAD_NAME (trans->srcpad));
1528 bclass->fixate_caps (trans, GST_PAD_SINK, incaps, newcaps);
1530 *out_buf = gst_buffer_make_metadata_writable (*out_buf);
1531 gst_buffer_set_caps (*out_buf, newcaps);
1532 gst_caps_unref (newcaps);
1533 newcaps = GST_BUFFER_CAPS (*out_buf);
1536 /* caps not empty, try to renegotiate to the new format */
1537 if (!gst_base_transform_configure_caps (trans, incaps, newcaps)) {
1538 /* not sure we need to fail hard here, we can simply continue our
1539 * conversion with what we negotiated before */
1540 goto failed_configure;
1542 /* new format configure, and use the new output buffer */
1543 gst_pad_set_caps (trans->srcpad, newcaps);
1545 /* clear previous cached sink-pad caps, so buffer_alloc knows that
1546 * it needs to revisit the decision about whether to proxy or not: */
1547 gst_caps_replace (&priv->sink_alloc, NULL);
1548 /* if we got a buffer of the wrong size, discard it now and make sure we
1549 * allocate a properly sized buffer later. */
1550 if (newsize != expsize) {
1551 if (in_buf != *out_buf)
1552 gst_buffer_unref (*out_buf);
1557 compute_upstream_suggestion (trans, expsize, newcaps);
1559 if (in_buf != *out_buf)
1560 gst_buffer_unref (*out_buf);
1563 } else if (outsize != newsize) {
1564 GST_WARNING_OBJECT (trans, "Caps did not change but allocated size does "
1565 "not match expected size (%d != %d)", newsize, outsize);
1566 if (in_buf != *out_buf)
1567 gst_buffer_unref (*out_buf);
1571 /* these are the final output caps */
1572 outcaps = GST_PAD_CAPS (trans->srcpad);
1575 if (*out_buf == NULL) {
1577 GST_DEBUG_OBJECT (trans, "make default output buffer of size %d",
1579 /* no valid buffer yet, make one, metadata is writable */
1580 *out_buf = gst_buffer_new_and_alloc (outsize);
1581 gst_buffer_copy_metadata (*out_buf, in_buf,
1582 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
1584 GST_DEBUG_OBJECT (trans, "reuse input buffer");
1588 if (trans->passthrough && in_buf != *out_buf) {
1589 /* we are asked to perform a passthrough transform but the input and
1590 * output buffers are different. We have to discard the output buffer and
1591 * reuse the input buffer. */
1592 GST_DEBUG_OBJECT (trans, "passthrough but different buffers");
1596 GST_DEBUG_OBJECT (trans, "discard buffer, reuse input buffer");
1597 gst_buffer_unref (*out_buf);
1600 GST_DEBUG_OBJECT (trans, "using allocated buffer in %p, out %p", in_buf,
1602 /* if we have different buffers, check if the metadata is ok */
1603 if (*out_buf != in_buf) {
1606 mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
1607 GST_BUFFER_FLAG_DELTA_UNIT | GST_BUFFER_FLAG_DISCONT |
1608 GST_BUFFER_FLAG_GAP | GST_BUFFER_FLAG_MEDIA1 |
1609 GST_BUFFER_FLAG_MEDIA2 | GST_BUFFER_FLAG_MEDIA3;
1610 /* see if the flags and timestamps match */
1612 (GST_MINI_OBJECT_FLAGS (*out_buf) & mask) ==
1613 (GST_MINI_OBJECT_FLAGS (in_buf) & mask);
1615 GST_BUFFER_TIMESTAMP (*out_buf) != GST_BUFFER_TIMESTAMP (in_buf) ||
1616 GST_BUFFER_DURATION (*out_buf) != GST_BUFFER_DURATION (in_buf) ||
1617 GST_BUFFER_OFFSET (*out_buf) != GST_BUFFER_OFFSET (in_buf) ||
1618 GST_BUFFER_OFFSET_END (*out_buf) != GST_BUFFER_OFFSET_END (in_buf);
1623 /* check if we need to make things writable. We need this when we need to
1624 * update the caps or the metadata on the output buffer. */
1625 newcaps = GST_BUFFER_CAPS (*out_buf);
1626 /* we check the pointers as a quick check and then go to the more involved
1627 * check. This is needed when we receive different pointers on the sinkpad
1628 * that mean the same caps. What we then want to do is prefer those caps over
1629 * the ones on the srcpad and set the srcpad caps to the buffer caps */
1630 setcaps = !newcaps || ((newcaps != outcaps)
1631 && (!gst_caps_is_equal (newcaps, outcaps)));
1632 /* we need to modify the metadata when the element is not gap aware,
1633 * passthrough is not used and the gap flag is set */
1634 copymeta |= !trans->priv->gap_aware && !trans->passthrough
1635 && (GST_MINI_OBJECT_FLAGS (*out_buf) & GST_BUFFER_FLAG_GAP);
1637 if (setcaps || copymeta) {
1638 GST_DEBUG_OBJECT (trans, "setcaps %d, copymeta %d", setcaps, copymeta);
1639 if (!gst_buffer_is_metadata_writable (*out_buf)) {
1640 GST_DEBUG_OBJECT (trans, "buffer metadata %p not writable", *out_buf);
1641 if (in_buf == *out_buf)
1642 *out_buf = gst_buffer_create_sub (in_buf, 0, GST_BUFFER_SIZE (in_buf));
1644 *out_buf = gst_buffer_make_metadata_writable (*out_buf);
1646 /* when we get here, the metadata should be writable */
1648 gst_buffer_set_caps (*out_buf, outcaps);
1650 gst_buffer_copy_metadata (*out_buf, in_buf,
1651 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
1652 /* clear the GAP flag when the subclass does not understand it */
1653 if (!trans->priv->gap_aware)
1654 GST_BUFFER_FLAG_UNSET (*out_buf, GST_BUFFER_FLAG_GAP);
1662 GST_WARNING_OBJECT (trans, "pad-alloc failed: %s", gst_flow_get_name (ret));
1667 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
1668 ("Sub-class failed to provide an output buffer"), (NULL));
1669 return GST_FLOW_ERROR;
1673 GST_ERROR_OBJECT (trans, "unknown output size");
1674 return GST_FLOW_ERROR;
1678 GST_WARNING_OBJECT (trans, "failed to configure caps");
1679 return GST_FLOW_NOT_NEGOTIATED;
1683 /* Given @caps calcultate the size of one unit.
1685 * For video caps, this is the size of one frame (and thus one buffer).
1686 * For audio caps, this is the size of one sample.
1688 * These values are cached since they do not change and the calculation
1689 * potentially involves parsing caps and other expensive stuff.
1691 * We have two cache locations to store the size, one for the source caps
1692 * and one for the sink caps.
1694 * this function returns FALSE if no size could be calculated.
1697 gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
1700 gboolean res = FALSE;
1701 GstBaseTransformClass *bclass;
1703 /* see if we have the result cached */
1704 if (trans->cache_caps1 == caps) {
1705 *size = trans->cache_caps1_size;
1706 GST_DEBUG_OBJECT (trans, "returned %d from first cache", *size);
1709 if (trans->cache_caps2 == caps) {
1710 *size = trans->cache_caps2_size;
1711 GST_DEBUG_OBJECT (trans, "returned %d from second cached", *size);
1715 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1716 if (bclass->get_unit_size) {
1717 res = bclass->get_unit_size (trans, caps, size);
1718 GST_DEBUG_OBJECT (trans, "caps %" GST_PTR_FORMAT
1719 ") has unit size %d, result %s", caps, *size, res ? "TRUE" : "FALSE");
1722 /* and cache the values */
1723 if (trans->cache_caps1 == NULL) {
1724 gst_caps_replace (&trans->cache_caps1, caps);
1725 trans->cache_caps1_size = *size;
1726 GST_DEBUG_OBJECT (trans, "caching %d in first cache", *size);
1727 } else if (trans->cache_caps2 == NULL) {
1728 gst_caps_replace (&trans->cache_caps2, caps);
1729 trans->cache_caps2_size = *size;
1730 GST_DEBUG_OBJECT (trans, "caching %d in second cache", *size);
1732 GST_DEBUG_OBJECT (trans, "no free spot to cache unit_size");
1736 GST_DEBUG_OBJECT (trans, "Sub-class does not implement get_unit_size");
1741 /* your upstream peer wants to send you a buffer
1742 * that buffer has the given offset, size and caps
1743 * you're requested to allocate a buffer
1745 static GstFlowReturn
1746 gst_base_transform_buffer_alloc (GstPad * pad, guint64 offset, guint size,
1747 GstCaps * caps, GstBuffer ** buf)
1749 GstBaseTransform *trans;
1750 GstBaseTransformClass *klass;
1751 GstBaseTransformPrivate *priv;
1753 gboolean alloced = FALSE;
1754 gboolean proxy, suggest, same_caps;
1755 GstCaps *sink_suggest = NULL;
1758 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1759 if (G_UNLIKELY (trans == NULL))
1760 return GST_FLOW_WRONG_STATE;
1761 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1764 GST_DEBUG_OBJECT (pad, "alloc with caps %p %" GST_PTR_FORMAT ", size %u",
1767 /* if the code below does not come up with a better buffer, we will return _OK
1768 * and an empty buffer. This will trigger the core to allocate a buffer with
1769 * given input size and caps. */
1773 /* we remember our previous alloc request to quickly see if we can proxy or
1774 * not. We skip this check if we have a pending suggestion. */
1775 GST_OBJECT_LOCK (pad);
1776 same_caps = !priv->suggest_pending && caps &&
1777 gst_caps_is_equal (priv->sink_alloc, caps);
1778 GST_OBJECT_UNLOCK (pad);
1781 /* we have seen this before, see below if we need to proxy */
1782 GST_DEBUG_OBJECT (trans, "have old caps %p, size %u", caps, size);
1783 gst_caps_replace (&sink_suggest, caps);
1784 size_suggest = size;
1787 GST_DEBUG_OBJECT (trans, "new format %p %" GST_PTR_FORMAT, caps, caps);
1789 /* if we have a suggestion, pretend we got these as input */
1790 GST_OBJECT_LOCK (pad);
1791 if ((priv->sink_suggest && !gst_caps_is_equal (caps, priv->sink_suggest))) {
1792 sink_suggest = gst_caps_ref (priv->sink_suggest);
1793 size_suggest = priv->size_suggest;
1794 GST_DEBUG_OBJECT (trans, "have suggestion %p %" GST_PTR_FORMAT " size %u",
1795 sink_suggest, sink_suggest, priv->size_suggest);
1796 /* suggest is TRUE when we have a custom suggestion pending that we need
1797 * to unref later. */
1800 GST_DEBUG_OBJECT (trans, "using caps %p %" GST_PTR_FORMAT " size %u",
1802 gst_caps_replace (&sink_suggest, caps);
1803 size_suggest = size;
1806 priv->suggest_pending = FALSE;
1807 GST_OBJECT_UNLOCK (pad);
1809 /* check if we actually handle this format on the sinkpad */
1811 const GstCaps *templ;
1813 if (!gst_caps_is_fixed (sink_suggest)) {
1816 GST_DEBUG_OBJECT (trans, "Suggested caps is not fixed: %"
1817 GST_PTR_FORMAT, sink_suggest);
1820 gst_pad_peer_get_caps_reffed (GST_BASE_TRANSFORM_SINK_PAD (trans));
1821 /* try fixating by intersecting with peer caps */
1826 gst_caps_intersect_full (sink_suggest, peercaps,
1827 GST_CAPS_INTERSECT_FIRST);
1828 gst_caps_unref (peercaps);
1829 gst_caps_unref (sink_suggest);
1830 sink_suggest = intersect;
1833 if (gst_caps_is_empty (sink_suggest))
1836 /* try the alloc caps if it is still not fixed */
1837 if (!gst_caps_is_fixed (sink_suggest)) {
1840 GST_DEBUG_OBJECT (trans, "Checking if the input caps is compatible "
1841 "with the non-fixed caps suggestion");
1843 gst_caps_intersect_full (sink_suggest, caps,
1844 GST_CAPS_INTERSECT_FIRST);
1845 if (!gst_caps_is_empty (intersect)) {
1846 GST_DEBUG_OBJECT (trans, "It is, using it");
1847 gst_caps_replace (&sink_suggest, caps);
1849 gst_caps_unref (intersect);
1852 /* be safe and call default fixate */
1853 sink_suggest = gst_caps_make_writable (sink_suggest);
1854 gst_pad_fixate_caps (GST_BASE_TRANSFORM_SINK_PAD (trans), sink_suggest);
1856 if (!gst_caps_is_fixed (sink_suggest)) {
1857 gst_caps_unref (sink_suggest);
1858 sink_suggest = NULL;
1861 GST_DEBUG_OBJECT (trans, "Caps fixed to: %" GST_PTR_FORMAT,
1866 templ = gst_pad_get_pad_template_caps (pad);
1868 if (!gst_caps_can_intersect (sink_suggest, templ)) {
1872 GST_DEBUG_OBJECT (trans,
1873 "Requested pad alloc caps are not supported: %" GST_PTR_FORMAT,
1875 /* the requested pad alloc caps are not supported, so let's try
1876 * picking something allowed between the pads (they are linked,
1877 * there must be something) */
1878 allowed = gst_pad_get_allowed_caps (pad);
1879 if (allowed && !gst_caps_is_empty (allowed)) {
1880 GST_DEBUG_OBJECT (trans,
1881 "pads could agree on one of the following caps: " "%"
1882 GST_PTR_FORMAT, allowed);
1883 allowed = gst_caps_make_writable (allowed);
1885 if (klass->fixate_caps) {
1887 gst_pad_get_allowed_caps (GST_BASE_TRANSFORM_SRC_PAD (trans));
1888 klass->fixate_caps (trans, GST_PAD_SRC, peercaps, allowed);
1889 gst_caps_unref (peercaps);
1892 /* Fixate them to be safe if the subclass didn't do it */
1893 gst_caps_truncate (allowed);
1894 gst_pad_fixate_caps (pad, allowed);
1895 gst_caps_replace (&sink_suggest, allowed);
1896 gst_caps_unref (allowed);
1900 GST_DEBUG_OBJECT (trans, "Fixated suggestion caps to %"
1901 GST_PTR_FORMAT, sink_suggest);
1904 gst_caps_unref (allowed);
1911 /* find the best format for the other side here we decide if we will proxy
1912 * the caps or not. */
1913 if (sink_suggest == NULL) {
1914 /* always proxy when the caps are NULL. When this is a new format, see if
1915 * we can proxy it downstream */
1916 GST_DEBUG_OBJECT (trans, "null caps, marking for proxy");
1917 priv->proxy_alloc = TRUE;
1921 /* we have a new format, see what we need to proxy to */
1922 othercaps = gst_base_transform_find_transform (trans, pad, sink_suggest);
1923 if (!othercaps || gst_caps_is_empty (othercaps)) {
1924 /* no transform possible, we certainly can't proxy */
1925 GST_DEBUG_OBJECT (trans, "can't find transform, disable proxy");
1926 priv->proxy_alloc = FALSE;
1928 /* we transformed into something */
1929 if (gst_caps_is_equal (sink_suggest, othercaps)) {
1930 GST_DEBUG_OBJECT (trans,
1931 "best caps same as input, marking for proxy");
1932 priv->proxy_alloc = TRUE;
1934 GST_DEBUG_OBJECT (trans,
1935 "best caps different from input, disable proxy");
1936 priv->proxy_alloc = FALSE;
1940 gst_caps_unref (othercaps);
1943 /* remember the new caps */
1944 GST_OBJECT_LOCK (pad);
1945 gst_caps_replace (&priv->sink_alloc, sink_suggest);
1946 GST_OBJECT_UNLOCK (pad);
1948 proxy = priv->proxy_alloc;
1949 GST_DEBUG_OBJECT (trans, "doing default alloc, proxy %d, suggest %d", proxy,
1952 /* we only want to proxy if we have no suggestion pending, FIXME */
1953 if (proxy && !suggest) {
1956 GST_DEBUG_OBJECT (trans, "proxy buffer-alloc with caps %p %" GST_PTR_FORMAT
1957 ", size %u", caps, caps, size);
1959 /* we always proxy the input caps, never the suggestion. The reason is that
1960 * We don't yet handle the caps of renegotiation in here. FIXME */
1961 res = gst_pad_alloc_buffer (trans->srcpad, offset, size, caps, buf);
1962 if (res != GST_FLOW_OK)
1966 /* check if the caps changed */
1967 newcaps = GST_BUFFER_CAPS (*buf);
1969 GST_DEBUG_OBJECT (trans, "got caps %" GST_PTR_FORMAT, newcaps);
1971 if (!gst_caps_is_equal (newcaps, caps)) {
1972 GST_DEBUG_OBJECT (trans, "caps are new");
1973 /* we have new caps, see if we can proxy downstream */
1974 if (gst_pad_peer_accept_caps (pad, newcaps)) {
1975 /* peer accepts the caps, return a buffer in this format */
1976 GST_DEBUG_OBJECT (trans, "peer accepted new caps");
1977 /* remember the format */
1978 GST_OBJECT_LOCK (pad);
1979 gst_caps_replace (&priv->sink_alloc, newcaps);
1980 GST_OBJECT_UNLOCK (pad);
1982 GST_DEBUG_OBJECT (trans, "peer did not accept new caps");
1983 /* peer does not accept the caps, disable proxy_alloc, free the
1984 * buffer we received and create a buffer of the requested format
1985 * by the default handler. */
1986 GST_DEBUG_OBJECT (trans, "disabling proxy");
1987 priv->proxy_alloc = FALSE;
1988 gst_buffer_unref (*buf);
1992 GST_DEBUG_OBJECT (trans, "received required caps from peer");
1997 /* there was a custom suggestion, create a buffer of this format and return
1998 * it. Note that this format */
1999 *buf = gst_buffer_new_and_alloc (size_suggest);
2000 GST_DEBUG_OBJECT (trans,
2001 "doing suggestion of size %u, caps %p %" GST_PTR_FORMAT, size_suggest,
2002 sink_suggest, sink_suggest);
2003 GST_BUFFER_CAPS (*buf) = sink_suggest;
2004 sink_suggest = NULL;
2008 gst_caps_unref (sink_suggest);
2010 if (res == GST_FLOW_OK && alloced) {
2011 /* just alloc'ed a buffer, so we only want to do this again if we
2012 * received a buffer */
2013 GST_DEBUG_OBJECT (trans, "Cleaning force alloc");
2014 trans->priv->force_alloc = FALSE;
2017 gst_object_unref (trans);
2023 GST_DEBUG_OBJECT (trans, "pad alloc failed: %s", gst_flow_get_name (res));
2025 gst_caps_unref (sink_suggest);
2026 gst_object_unref (trans);
2031 GST_DEBUG_OBJECT (trans, "pad alloc with unsupported caps");
2033 gst_caps_unref (sink_suggest);
2034 gst_object_unref (trans);
2035 return GST_FLOW_NOT_NEGOTIATED;
2040 gst_base_transform_sink_event (GstPad * pad, GstEvent * event)
2042 GstBaseTransform *trans;
2043 GstBaseTransformClass *bclass;
2044 gboolean ret = TRUE;
2045 gboolean forward = TRUE;
2047 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2048 if (G_UNLIKELY (trans == NULL)) {
2049 gst_event_unref (event);
2052 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2055 forward = bclass->event (trans, event);
2057 /* FIXME, do this in the default event handler so the subclass can do
2058 * something different. */
2060 ret = gst_pad_push_event (trans->srcpad, event);
2062 gst_event_unref (event);
2064 gst_object_unref (trans);
2070 gst_base_transform_sink_eventfunc (GstBaseTransform * trans, GstEvent * event)
2072 switch (GST_EVENT_TYPE (event)) {
2073 case GST_EVENT_FLUSH_START:
2075 case GST_EVENT_FLUSH_STOP:
2076 GST_OBJECT_LOCK (trans);
2077 /* reset QoS parameters */
2078 trans->priv->proportion = 1.0;
2079 trans->priv->earliest_time = -1;
2080 trans->priv->discont = FALSE;
2081 trans->priv->processed = 0;
2082 trans->priv->dropped = 0;
2083 GST_OBJECT_UNLOCK (trans);
2084 /* we need new segment info after the flush. */
2085 trans->have_newsegment = FALSE;
2086 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
2087 trans->priv->last_stop_out = GST_CLOCK_TIME_NONE;
2093 case GST_EVENT_NEWSEGMENT:
2096 gdouble rate, arate;
2097 gint64 start, stop, time;
2100 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
2101 &start, &stop, &time);
2103 trans->have_newsegment = TRUE;
2105 gst_segment_set_newsegment_full (&trans->segment, update, rate, arate,
2106 format, start, stop, time);
2108 if (format == GST_FORMAT_TIME) {
2109 GST_DEBUG_OBJECT (trans, "received TIME NEW_SEGMENT %" GST_TIME_FORMAT
2110 " -- %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT
2111 ", accum %" GST_TIME_FORMAT,
2112 GST_TIME_ARGS (trans->segment.start),
2113 GST_TIME_ARGS (trans->segment.stop),
2114 GST_TIME_ARGS (trans->segment.time),
2115 GST_TIME_ARGS (trans->segment.accum));
2117 GST_DEBUG_OBJECT (trans, "received NEW_SEGMENT %" G_GINT64_FORMAT
2118 " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT
2119 ", accum %" G_GINT64_FORMAT,
2120 trans->segment.start, trans->segment.stop,
2121 trans->segment.time, trans->segment.accum);
2133 gst_base_transform_src_event (GstPad * pad, GstEvent * event)
2135 GstBaseTransform *trans;
2136 GstBaseTransformClass *bclass;
2137 gboolean ret = TRUE;
2139 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2140 if (G_UNLIKELY (trans == NULL)) {
2141 gst_event_unref (event);
2145 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2147 if (bclass->src_event)
2148 ret = bclass->src_event (trans, event);
2150 gst_event_unref (event);
2152 gst_object_unref (trans);
2158 gst_base_transform_src_eventfunc (GstBaseTransform * trans, GstEvent * event)
2162 GST_DEBUG_OBJECT (trans, "handling event %p %" GST_PTR_FORMAT, event, event);
2164 switch (GST_EVENT_TYPE (event)) {
2165 case GST_EVENT_SEEK:
2167 case GST_EVENT_NAVIGATION:
2172 GstClockTimeDiff diff;
2173 GstClockTime timestamp;
2175 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
2176 gst_base_transform_update_qos (trans, proportion, diff, timestamp);
2183 ret = gst_pad_push_event (trans->sinkpad, event);
2188 /* perform a transform on @inbuf and put the result in @outbuf.
2190 * This function is common to the push and pull-based operations.
2192 * This function takes ownership of @inbuf */
2193 static GstFlowReturn
2194 gst_base_transform_handle_buffer (GstBaseTransform * trans, GstBuffer * inbuf,
2195 GstBuffer ** outbuf)
2197 GstBaseTransformClass *bclass;
2198 GstFlowReturn ret = GST_FLOW_OK;
2199 gboolean want_in_place, reconfigure;
2200 GstClockTime running_time;
2201 GstClockTime timestamp;
2204 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2206 if (G_LIKELY ((incaps = GST_BUFFER_CAPS (inbuf)))) {
2207 GST_OBJECT_LOCK (trans);
2208 reconfigure = trans->priv->reconfigure;
2209 trans->priv->reconfigure = FALSE;
2210 GST_OBJECT_UNLOCK (trans);
2212 if (G_UNLIKELY (reconfigure)) {
2213 GST_DEBUG_OBJECT (trans, "we had a pending reconfigure");
2214 /* if we need to reconfigure we pretend a buffer with new caps arrived. This
2215 * will reconfigure the transform with the new output format. We can only
2216 * do this if the buffer actually has caps. */
2217 if (!gst_base_transform_setcaps (trans->sinkpad, incaps))
2218 goto not_negotiated;
2222 if (GST_BUFFER_OFFSET_IS_VALID (inbuf))
2223 GST_DEBUG_OBJECT (trans, "handling buffer %p of size %d and offset %"
2224 G_GUINT64_FORMAT, inbuf, GST_BUFFER_SIZE (inbuf),
2225 GST_BUFFER_OFFSET (inbuf));
2227 GST_DEBUG_OBJECT (trans, "handling buffer %p of size %d and offset NONE",
2228 inbuf, GST_BUFFER_SIZE (inbuf));
2230 /* Don't allow buffer handling before negotiation, except in passthrough mode
2231 * or if the class doesn't implement a set_caps function (in which case it doesn't
2234 if (!trans->negotiated && !trans->passthrough && (bclass->set_caps != NULL))
2235 goto not_negotiated;
2237 /* Set discont flag so we can mark the outgoing buffer */
2238 if (GST_BUFFER_IS_DISCONT (inbuf)) {
2239 GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", inbuf);
2240 trans->priv->discont = TRUE;
2243 /* can only do QoS if the segment is in TIME */
2244 if (trans->segment.format != GST_FORMAT_TIME)
2247 /* QOS is done on the running time of the buffer, get it now */
2248 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
2249 running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
2252 if (running_time != -1) {
2254 GstClockTime earliest_time;
2257 /* lock for getting the QoS parameters that are set (in a different thread)
2258 * with the QOS events */
2259 GST_OBJECT_LOCK (trans);
2260 earliest_time = trans->priv->earliest_time;
2261 proportion = trans->priv->proportion;
2262 /* check for QoS, don't perform conversion for buffers
2263 * that are known to be late. */
2264 need_skip = trans->priv->qos_enabled &&
2265 earliest_time != -1 && running_time <= earliest_time;
2266 GST_OBJECT_UNLOCK (trans);
2269 GstMessage *qos_msg;
2270 GstClockTime duration;
2271 guint64 stream_time;
2274 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "skipping transform: qostime %"
2275 GST_TIME_FORMAT " <= %" GST_TIME_FORMAT,
2276 GST_TIME_ARGS (running_time), GST_TIME_ARGS (earliest_time));
2278 trans->priv->dropped++;
2280 duration = GST_BUFFER_DURATION (inbuf);
2282 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
2284 jitter = GST_CLOCK_DIFF (running_time, earliest_time);
2287 gst_message_new_qos (GST_OBJECT_CAST (trans), FALSE, running_time,
2288 stream_time, timestamp, duration);
2289 gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
2290 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
2291 trans->priv->processed, trans->priv->dropped);
2292 gst_element_post_message (GST_ELEMENT_CAST (trans), qos_msg);
2294 /* mark discont for next buffer */
2295 trans->priv->discont = TRUE;
2302 /* first try to allocate an output buffer based on the currently negotiated
2303 * format. While we call pad-alloc we could renegotiate the srcpad format or
2304 * have a new suggestion for upstream buffer-alloc.
2305 * In any case, outbuf will contain a buffer suitable for doing the configured
2306 * transform after this function. */
2307 ret = gst_base_transform_prepare_output_buffer (trans, inbuf, outbuf);
2308 if (G_UNLIKELY (ret != GST_FLOW_OK))
2311 /* now perform the needed transform */
2312 if (trans->passthrough) {
2313 /* In passthrough mode, give transform_ip a look at the
2314 * buffer, without making it writable, or just push the
2316 if (bclass->transform_ip) {
2317 GST_DEBUG_OBJECT (trans, "doing passthrough transform");
2318 ret = bclass->transform_ip (trans, *outbuf);
2320 GST_DEBUG_OBJECT (trans, "element is in passthrough");
2323 want_in_place = (bclass->transform_ip != NULL) && trans->always_in_place;
2325 if (want_in_place) {
2326 GST_DEBUG_OBJECT (trans, "doing inplace transform");
2328 if (inbuf != *outbuf) {
2329 guint8 *indata, *outdata;
2331 /* Different buffer. The data can still be the same when we are dealing
2332 * with subbuffers of the same buffer. Note that because of the FIXME in
2333 * prepare_output_buffer() we have decreased the refcounts of inbuf and
2334 * outbuf to keep them writable */
2335 indata = GST_BUFFER_DATA (inbuf);
2336 outdata = GST_BUFFER_DATA (*outbuf);
2338 if (indata != outdata)
2339 memcpy (outdata, indata, GST_BUFFER_SIZE (inbuf));
2341 ret = bclass->transform_ip (trans, *outbuf);
2343 GST_DEBUG_OBJECT (trans, "doing non-inplace transform");
2345 if (bclass->transform)
2346 ret = bclass->transform (trans, inbuf, *outbuf);
2348 ret = GST_FLOW_NOT_SUPPORTED;
2353 /* only unref input buffer if we allocated a new outbuf buffer */
2354 if (*outbuf != inbuf)
2355 gst_buffer_unref (inbuf);
2357 /* pushed a buffer, we can now try an alloc */
2358 GST_DEBUG_OBJECT (trans, "Pushed a buffer, setting force alloc to true");
2359 trans->priv->force_alloc = TRUE;
2365 gst_buffer_unref (inbuf);
2366 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
2367 ("not negotiated"), ("not negotiated"));
2368 return GST_FLOW_NOT_NEGOTIATED;
2372 gst_buffer_unref (inbuf);
2373 GST_WARNING_OBJECT (trans, "could not get buffer from pool: %s",
2374 gst_flow_get_name (ret));
2380 gst_base_transform_check_get_range (GstPad * pad)
2382 GstBaseTransform *trans;
2385 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2387 ret = gst_pad_check_pull_range (trans->sinkpad);
2389 gst_object_unref (trans);
2394 /* FIXME, getrange is broken, need to pull range from the other
2395 * end based on the transform_size result.
2397 static GstFlowReturn
2398 gst_base_transform_getrange (GstPad * pad, guint64 offset,
2399 guint length, GstBuffer ** buffer)
2401 GstBaseTransform *trans;
2402 GstBaseTransformClass *klass;
2406 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2408 ret = gst_pad_pull_range (trans->sinkpad, offset, length, &inbuf);
2409 if (G_UNLIKELY (ret != GST_FLOW_OK))
2412 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2413 if (klass->before_transform)
2414 klass->before_transform (trans, inbuf);
2416 GST_BASE_TRANSFORM_LOCK (trans);
2417 ret = gst_base_transform_handle_buffer (trans, inbuf, buffer);
2418 GST_BASE_TRANSFORM_UNLOCK (trans);
2421 gst_object_unref (trans);
2428 GST_DEBUG_OBJECT (trans, "failed to pull a buffer: %s",
2429 gst_flow_get_name (ret));
2434 static GstFlowReturn
2435 gst_base_transform_chain (GstPad * pad, GstBuffer * buffer)
2437 GstBaseTransform *trans;
2438 GstBaseTransformClass *klass;
2440 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
2441 GstClockTime timestamp, duration;
2442 GstBuffer *outbuf = NULL;
2444 trans = GST_BASE_TRANSFORM (GST_OBJECT_PARENT (pad));
2446 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2447 duration = GST_BUFFER_DURATION (buffer);
2449 /* calculate end position of the incoming buffer */
2450 if (timestamp != GST_CLOCK_TIME_NONE) {
2451 if (duration != GST_CLOCK_TIME_NONE)
2452 last_stop = timestamp + duration;
2454 last_stop = timestamp;
2457 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2458 if (klass->before_transform)
2459 klass->before_transform (trans, buffer);
2461 /* protect transform method and concurrent buffer alloc */
2462 GST_BASE_TRANSFORM_LOCK (trans);
2463 ret = gst_base_transform_handle_buffer (trans, buffer, &outbuf);
2464 GST_BASE_TRANSFORM_UNLOCK (trans);
2466 /* outbuf can be NULL, this means a dropped buffer, if we have a buffer but
2467 * GST_BASE_TRANSFORM_FLOW_DROPPED we will not push either. */
2468 if (outbuf != NULL) {
2469 if ((ret == GST_FLOW_OK)) {
2470 GstClockTime last_stop_out = GST_CLOCK_TIME_NONE;
2472 /* Remember last stop position */
2473 if (last_stop != GST_CLOCK_TIME_NONE &&
2474 trans->segment.format == GST_FORMAT_TIME)
2475 gst_segment_set_last_stop (&trans->segment, GST_FORMAT_TIME, last_stop);
2477 if (GST_BUFFER_TIMESTAMP_IS_VALID (outbuf)) {
2478 last_stop_out = GST_BUFFER_TIMESTAMP (outbuf);
2479 if (GST_BUFFER_DURATION_IS_VALID (outbuf))
2480 last_stop_out += GST_BUFFER_DURATION (outbuf);
2481 } else if (last_stop != GST_CLOCK_TIME_NONE) {
2482 last_stop_out = last_stop;
2484 if (last_stop_out != GST_CLOCK_TIME_NONE
2485 && trans->segment.format == GST_FORMAT_TIME)
2486 trans->priv->last_stop_out = last_stop_out;
2488 /* apply DISCONT flag if the buffer is not yet marked as such */
2489 if (trans->priv->discont) {
2490 if (!GST_BUFFER_IS_DISCONT (outbuf)) {
2491 outbuf = gst_buffer_make_metadata_writable (outbuf);
2492 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2494 trans->priv->discont = FALSE;
2496 trans->priv->processed++;
2497 ret = gst_pad_push (trans->srcpad, outbuf);
2499 gst_buffer_unref (outbuf);
2503 /* convert internal flow to OK and mark discont for the next buffer. */
2504 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED) {
2505 trans->priv->discont = TRUE;
2513 gst_base_transform_set_property (GObject * object, guint prop_id,
2514 const GValue * value, GParamSpec * pspec)
2516 GstBaseTransform *trans;
2518 trans = GST_BASE_TRANSFORM (object);
2522 gst_base_transform_set_qos_enabled (trans, g_value_get_boolean (value));
2525 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2531 gst_base_transform_get_property (GObject * object, guint prop_id,
2532 GValue * value, GParamSpec * pspec)
2534 GstBaseTransform *trans;
2536 trans = GST_BASE_TRANSFORM (object);
2540 g_value_set_boolean (value, gst_base_transform_is_qos_enabled (trans));
2543 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2548 /* not a vmethod of anything, just an internal method */
2550 gst_base_transform_activate (GstBaseTransform * trans, gboolean active)
2552 GstBaseTransformClass *bclass;
2553 gboolean result = TRUE;
2555 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2558 if (trans->priv->pad_mode == GST_ACTIVATE_NONE && bclass->start)
2559 result &= bclass->start (trans);
2561 GST_OBJECT_LOCK (trans);
2563 if (GST_PAD_CAPS (trans->sinkpad) && GST_PAD_CAPS (trans->srcpad))
2564 trans->have_same_caps =
2565 gst_caps_is_equal (GST_PAD_CAPS (trans->sinkpad),
2566 GST_PAD_CAPS (trans->srcpad)) || trans->passthrough;
2568 trans->have_same_caps = trans->passthrough;
2569 GST_DEBUG_OBJECT (trans, "have_same_caps %d", trans->have_same_caps);
2570 trans->negotiated = FALSE;
2571 trans->have_newsegment = FALSE;
2572 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
2573 trans->priv->last_stop_out = GST_CLOCK_TIME_NONE;
2574 trans->priv->proportion = 1.0;
2575 trans->priv->earliest_time = -1;
2576 trans->priv->discont = FALSE;
2577 gst_caps_replace (&trans->priv->sink_suggest, NULL);
2578 trans->priv->processed = 0;
2579 trans->priv->dropped = 0;
2580 trans->priv->force_alloc = TRUE;
2582 GST_OBJECT_UNLOCK (trans);
2584 /* We must make sure streaming has finished before resetting things
2585 * and calling the ::stop vfunc */
2586 GST_PAD_STREAM_LOCK (trans->sinkpad);
2587 GST_PAD_STREAM_UNLOCK (trans->sinkpad);
2589 trans->have_same_caps = FALSE;
2590 /* We can only reset the passthrough mode if the instance told us to
2591 handle it in configure_caps */
2592 if (bclass->passthrough_on_same_caps) {
2593 gst_base_transform_set_passthrough (trans, FALSE);
2595 gst_caps_replace (&trans->cache_caps1, NULL);
2596 gst_caps_replace (&trans->cache_caps2, NULL);
2597 gst_caps_replace (&trans->priv->sink_alloc, NULL);
2598 gst_caps_replace (&trans->priv->sink_suggest, NULL);
2600 if (trans->priv->pad_mode != GST_ACTIVATE_NONE && bclass->stop)
2601 result &= bclass->stop (trans);
2608 gst_base_transform_sink_activate_push (GstPad * pad, gboolean active)
2610 gboolean result = TRUE;
2611 GstBaseTransform *trans;
2613 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2615 result = gst_base_transform_activate (trans, active);
2618 trans->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
2620 gst_object_unref (trans);
2626 gst_base_transform_src_activate_pull (GstPad * pad, gboolean active)
2628 gboolean result = FALSE;
2629 GstBaseTransform *trans;
2631 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2633 result = gst_pad_activate_pull (trans->sinkpad, active);
2636 result &= gst_base_transform_activate (trans, active);
2639 trans->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
2641 gst_object_unref (trans);
2647 * gst_base_transform_set_passthrough:
2648 * @trans: the #GstBaseTransform to set
2649 * @passthrough: boolean indicating passthrough mode.
2651 * Set passthrough mode for this filter by default. This is mostly
2652 * useful for filters that do not care about negotiation.
2654 * Always TRUE for filters which don't implement either a transform
2655 * or transform_ip method.
2660 gst_base_transform_set_passthrough (GstBaseTransform * trans,
2661 gboolean passthrough)
2663 GstBaseTransformClass *bclass;
2665 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2667 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2669 GST_OBJECT_LOCK (trans);
2670 if (passthrough == FALSE) {
2671 if (bclass->transform_ip || bclass->transform)
2672 trans->passthrough = FALSE;
2674 trans->passthrough = TRUE;
2677 GST_DEBUG_OBJECT (trans, "set passthrough %d", trans->passthrough);
2678 GST_OBJECT_UNLOCK (trans);
2682 * gst_base_transform_is_passthrough:
2683 * @trans: the #GstBaseTransform to query
2685 * See if @trans is configured as a passthrough transform.
2687 * Returns: TRUE is the transform is configured in passthrough mode.
2692 gst_base_transform_is_passthrough (GstBaseTransform * trans)
2696 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2698 GST_OBJECT_LOCK (trans);
2699 result = trans->passthrough;
2700 GST_OBJECT_UNLOCK (trans);
2706 * gst_base_transform_set_in_place:
2707 * @trans: the #GstBaseTransform to modify
2708 * @in_place: Boolean value indicating that we would like to operate
2709 * on in_place buffers.
2711 * Determines whether a non-writable buffer will be copied before passing
2712 * to the transform_ip function.
2714 * <listitem>Always TRUE if no transform function is implemented.</listitem>
2715 * <listitem>Always FALSE if ONLY transform function is implemented.</listitem>
2721 gst_base_transform_set_in_place (GstBaseTransform * trans, gboolean in_place)
2723 GstBaseTransformClass *bclass;
2725 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2727 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2729 GST_OBJECT_LOCK (trans);
2732 if (bclass->transform_ip) {
2733 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
2734 trans->always_in_place = TRUE;
2737 if (bclass->transform) {
2738 GST_DEBUG_OBJECT (trans, "setting in_place FALSE");
2739 trans->always_in_place = FALSE;
2743 GST_OBJECT_UNLOCK (trans);
2747 * gst_base_transform_is_in_place:
2748 * @trans: the #GstBaseTransform to query
2750 * See if @trans is configured as a in_place transform.
2752 * Returns: TRUE is the transform is configured in in_place mode.
2757 gst_base_transform_is_in_place (GstBaseTransform * trans)
2761 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2763 GST_OBJECT_LOCK (trans);
2764 result = trans->always_in_place;
2765 GST_OBJECT_UNLOCK (trans);
2771 * gst_base_transform_update_qos:
2772 * @trans: a #GstBaseTransform
2773 * @proportion: the proportion
2774 * @diff: the diff against the clock
2775 * @timestamp: the timestamp of the buffer generating the QoS expressed in
2778 * Set the QoS parameters in the transform. This function is called internally
2779 * when a QOS event is received but subclasses can provide custom information
2787 gst_base_transform_update_qos (GstBaseTransform * trans,
2788 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2791 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2793 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans,
2794 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2795 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2797 GST_OBJECT_LOCK (trans);
2798 trans->priv->proportion = proportion;
2799 trans->priv->earliest_time = timestamp + diff;
2800 GST_OBJECT_UNLOCK (trans);
2804 * gst_base_transform_set_qos_enabled:
2805 * @trans: a #GstBaseTransform
2806 * @enabled: new state
2808 * Enable or disable QoS handling in the transform.
2815 gst_base_transform_set_qos_enabled (GstBaseTransform * trans, gboolean enabled)
2817 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2819 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "enabled: %d", enabled);
2821 GST_OBJECT_LOCK (trans);
2822 trans->priv->qos_enabled = enabled;
2823 GST_OBJECT_UNLOCK (trans);
2827 * gst_base_transform_is_qos_enabled:
2828 * @trans: a #GstBaseTransform
2830 * Queries if the transform will handle QoS.
2832 * Returns: TRUE if QoS is enabled.
2839 gst_base_transform_is_qos_enabled (GstBaseTransform * trans)
2843 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2845 GST_OBJECT_LOCK (trans);
2846 result = trans->priv->qos_enabled;
2847 GST_OBJECT_UNLOCK (trans);
2853 * gst_base_transform_set_gap_aware:
2854 * @trans: a #GstBaseTransform
2855 * @gap_aware: New state
2857 * If @gap_aware is %FALSE (the default), output buffers will have the
2858 * %GST_BUFFER_FLAG_GAP flag unset.
2860 * If set to %TRUE, the element must handle output buffers with this flag set
2861 * correctly, i.e. it can assume that the buffer contains neutral data but must
2862 * unset the flag if the output is no neutral data.
2869 gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware)
2871 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2873 GST_OBJECT_LOCK (trans);
2874 trans->priv->gap_aware = gap_aware;
2875 GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware);
2876 GST_OBJECT_UNLOCK (trans);
2880 * gst_base_transform_suggest:
2881 * @trans: a #GstBaseTransform
2882 * @caps: (transfer none): caps to suggest
2883 * @size: buffer size to suggest
2885 * Instructs @trans to suggest new @caps upstream. A copy of @caps will be
2891 gst_base_transform_suggest (GstBaseTransform * trans, GstCaps * caps,
2894 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2896 GST_OBJECT_LOCK (trans->sinkpad);
2897 if (trans->priv->sink_suggest)
2898 gst_caps_unref (trans->priv->sink_suggest);
2900 caps = gst_caps_copy (caps);
2901 trans->priv->sink_suggest = caps;
2902 trans->priv->size_suggest = size;
2903 trans->priv->suggest_pending = TRUE;
2904 GST_DEBUG_OBJECT (trans, "new suggest %" GST_PTR_FORMAT, caps);
2905 GST_OBJECT_UNLOCK (trans->sinkpad);
2909 * gst_base_transform_reconfigure:
2910 * @trans: a #GstBaseTransform
2912 * Instructs @trans to renegotiate a new downstream transform on the next
2913 * buffer. This function is typically called after properties on the transform
2914 * were set that influence the output format.
2919 gst_base_transform_reconfigure (GstBaseTransform * trans)
2921 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2923 GST_OBJECT_LOCK (trans);
2924 GST_DEBUG_OBJECT (trans, "marking reconfigure");
2925 trans->priv->reconfigure = TRUE;
2926 gst_caps_replace (&trans->priv->sink_alloc, NULL);
2927 GST_OBJECT_UNLOCK (trans);