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 const GstQueryType *gst_base_transform_query_type (GstPad * pad);
355 /* static guint gst_base_transform_signals[LAST_SIGNAL] = { 0 }; */
358 gst_base_transform_finalize (GObject * object)
360 GstBaseTransform *trans;
362 trans = GST_BASE_TRANSFORM (object);
364 gst_caps_replace (&trans->priv->sink_suggest, NULL);
365 g_mutex_free (trans->transform_lock);
367 G_OBJECT_CLASS (parent_class)->finalize (object);
371 gst_base_transform_class_init (GstBaseTransformClass * klass)
373 GObjectClass *gobject_class;
375 gobject_class = G_OBJECT_CLASS (klass);
377 GST_DEBUG_CATEGORY_INIT (gst_base_transform_debug, "basetransform", 0,
378 "basetransform element");
380 GST_DEBUG ("gst_base_transform_class_init");
382 g_type_class_add_private (klass, sizeof (GstBaseTransformPrivate));
384 parent_class = g_type_class_peek_parent (klass);
386 gobject_class->set_property = gst_base_transform_set_property;
387 gobject_class->get_property = gst_base_transform_get_property;
389 g_object_class_install_property (gobject_class, PROP_QOS,
390 g_param_spec_boolean ("qos", "QoS", "Handle Quality-of-Service events",
391 DEFAULT_PROP_QOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
393 gobject_class->finalize = gst_base_transform_finalize;
395 klass->passthrough_on_same_caps = FALSE;
396 klass->event = GST_DEBUG_FUNCPTR (gst_base_transform_sink_eventfunc);
397 klass->src_event = GST_DEBUG_FUNCPTR (gst_base_transform_src_eventfunc);
399 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps_default);
403 gst_base_transform_init (GstBaseTransform * trans,
404 GstBaseTransformClass * bclass)
406 GstPadTemplate *pad_template;
408 GST_DEBUG ("gst_base_transform_init");
410 trans->priv = GST_BASE_TRANSFORM_GET_PRIVATE (trans);
413 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
414 g_return_if_fail (pad_template != NULL);
415 trans->sinkpad = gst_pad_new_from_template (pad_template, "sink");
416 gst_pad_set_getcaps_function (trans->sinkpad,
417 GST_DEBUG_FUNCPTR (gst_base_transform_getcaps));
418 gst_pad_set_acceptcaps_function (trans->sinkpad,
419 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps));
420 gst_pad_set_setcaps_function (trans->sinkpad,
421 GST_DEBUG_FUNCPTR (gst_base_transform_setcaps));
422 gst_pad_set_event_function (trans->sinkpad,
423 GST_DEBUG_FUNCPTR (gst_base_transform_sink_event));
424 gst_pad_set_chain_function (trans->sinkpad,
425 GST_DEBUG_FUNCPTR (gst_base_transform_chain));
426 gst_pad_set_activatepush_function (trans->sinkpad,
427 GST_DEBUG_FUNCPTR (gst_base_transform_sink_activate_push));
428 gst_pad_set_bufferalloc_function (trans->sinkpad,
429 GST_DEBUG_FUNCPTR (gst_base_transform_buffer_alloc));
430 gst_pad_set_query_function (trans->sinkpad,
431 GST_DEBUG_FUNCPTR (gst_base_transform_query));
432 gst_pad_set_query_type_function (trans->sinkpad,
433 GST_DEBUG_FUNCPTR (gst_base_transform_query_type));
434 gst_element_add_pad (GST_ELEMENT (trans), trans->sinkpad);
437 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
438 g_return_if_fail (pad_template != NULL);
439 trans->srcpad = gst_pad_new_from_template (pad_template, "src");
440 gst_pad_set_getcaps_function (trans->srcpad,
441 GST_DEBUG_FUNCPTR (gst_base_transform_getcaps));
442 gst_pad_set_acceptcaps_function (trans->srcpad,
443 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps));
444 gst_pad_set_event_function (trans->srcpad,
445 GST_DEBUG_FUNCPTR (gst_base_transform_src_event));
446 gst_pad_set_checkgetrange_function (trans->srcpad,
447 GST_DEBUG_FUNCPTR (gst_base_transform_check_get_range));
448 gst_pad_set_getrange_function (trans->srcpad,
449 GST_DEBUG_FUNCPTR (gst_base_transform_getrange));
450 gst_pad_set_activatepull_function (trans->srcpad,
451 GST_DEBUG_FUNCPTR (gst_base_transform_src_activate_pull));
452 gst_pad_set_query_function (trans->srcpad,
453 GST_DEBUG_FUNCPTR (gst_base_transform_query));
454 gst_pad_set_query_type_function (trans->srcpad,
455 GST_DEBUG_FUNCPTR (gst_base_transform_query_type));
456 gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
458 trans->transform_lock = g_mutex_new ();
459 trans->pending_configure = FALSE;
460 trans->priv->qos_enabled = DEFAULT_PROP_QOS;
461 trans->cache_caps1 = NULL;
462 trans->cache_caps2 = NULL;
463 trans->priv->pad_mode = GST_ACTIVATE_NONE;
464 trans->priv->gap_aware = FALSE;
466 trans->passthrough = FALSE;
467 if (bclass->transform == NULL) {
468 /* If no transform function, always_in_place is TRUE */
469 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
470 trans->always_in_place = TRUE;
472 if (bclass->transform_ip == NULL) {
473 GST_DEBUG_OBJECT (trans, "setting passthrough TRUE");
474 trans->passthrough = TRUE;
478 trans->priv->processed = 0;
479 trans->priv->dropped = 0;
480 trans->priv->force_alloc = TRUE;
483 /* given @caps on the src or sink pad (given by @direction)
484 * calculate the possible caps on the other pad.
486 * Returns new caps, unref after usage.
489 gst_base_transform_transform_caps (GstBaseTransform * trans,
490 GstPadDirection direction, GstCaps * caps)
493 GstBaseTransformClass *klass;
498 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
500 /* if there is a custom transform function, use this */
501 if (klass->transform_caps) {
505 /* start with empty caps */
506 ret = gst_caps_new_empty ();
507 GST_DEBUG_OBJECT (trans, "transform caps (direction = %d)", direction);
509 if (gst_caps_is_any (caps)) {
510 /* for any caps we still have to call the transform function */
511 GST_DEBUG_OBJECT (trans, "from: ANY");
512 temp = klass->transform_caps (trans, direction, caps);
513 GST_DEBUG_OBJECT (trans, " to: %" GST_PTR_FORMAT, temp);
515 temp = gst_caps_make_writable (temp);
516 gst_caps_append (ret, temp);
518 gint n = gst_caps_get_size (caps);
519 /* we send caps with just one structure to the transform
520 * function as this is easier for the element */
521 for (i = 0; i < n; i++) {
524 nth = gst_caps_copy_nth (caps, i);
525 GST_LOG_OBJECT (trans, "from[%d]: %" GST_PTR_FORMAT, i, nth);
526 temp = klass->transform_caps (trans, direction, nth);
527 gst_caps_unref (nth);
528 GST_LOG_OBJECT (trans, " to[%d]: %" GST_PTR_FORMAT, i, temp);
530 temp = gst_caps_make_writable (temp);
532 /* here we need to only append those structures, that are not yet
533 * in there, we use the merge function for this */
534 gst_caps_merge (ret, temp);
536 GST_LOG_OBJECT (trans, " merged[%d]: %" GST_PTR_FORMAT, i, ret);
538 GST_LOG_OBJECT (trans, "merged: (%d)", gst_caps_get_size (ret));
539 /* FIXME: we can't do much simplification here because we don't really want to
540 * change the caps order
541 gst_caps_do_simplify (ret);
542 GST_DEBUG_OBJECT (trans, "simplified: (%d)", gst_caps_get_size (ret));
546 GST_DEBUG_OBJECT (trans, "identity from: %" GST_PTR_FORMAT, caps);
547 /* no transform function, use the identity transform */
548 ret = gst_caps_ref (caps);
551 GST_DEBUG_OBJECT (trans, "to: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (ret),
557 /* transform a buffer of @size with @caps on the pad with @direction to
558 * the size of a buffer with @othercaps and store the result in @othersize
560 * We have two ways of doing this:
561 * 1) use a custom transform size function, this is for complicated custom
562 * cases with no fixed unit_size.
563 * 2) use the unit_size functions where there is a relationship between the
564 * caps and the size of a buffer.
567 gst_base_transform_transform_size (GstBaseTransform * trans,
568 GstPadDirection direction, GstCaps * caps,
569 guint size, GstCaps * othercaps, guint * othersize)
571 guint inunitsize, outunitsize, units;
572 GstBaseTransformClass *klass;
575 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
577 GST_DEBUG_OBJECT (trans, "asked to transform size %d for caps %"
578 GST_PTR_FORMAT " to size for caps %" GST_PTR_FORMAT " in direction %s",
579 size, caps, othercaps, direction == GST_PAD_SRC ? "SRC" : "SINK");
581 if (klass->transform_size) {
582 /* if there is a custom transform function, use this */
583 ret = klass->transform_size (trans, direction, caps, size, othercaps,
585 } else if (klass->get_unit_size == NULL) {
586 /* if there is no transform_size and no unit_size, it means the
587 * element does not modify the size of a buffer */
591 /* there is no transform_size function, we have to use the unit_size
592 * functions. This method assumes there is a fixed unit_size associated with
593 * each caps. We provide the same amount of units on both sides. */
594 if (!gst_base_transform_get_unit_size (trans, caps, &inunitsize))
597 GST_DEBUG_OBJECT (trans, "input size %d, input unit size %d", size,
600 /* input size must be a multiple of the unit_size of the input caps */
601 if (inunitsize == 0 || (size % inunitsize != 0))
604 /* get the amount of units */
605 units = size / inunitsize;
607 /* now get the unit size of the output */
608 if (!gst_base_transform_get_unit_size (trans, othercaps, &outunitsize))
611 /* the output size is the unit_size times the amount of units on the
613 *othersize = units * outunitsize;
614 GST_DEBUG_OBJECT (trans, "transformed size to %d", *othersize);
623 GST_DEBUG_OBJECT (trans, "could not get in_size");
624 g_warning ("%s: could not get in_size", GST_ELEMENT_NAME (trans));
629 GST_DEBUG_OBJECT (trans, "Size %u is not a multiple of unit size %u", size,
631 g_warning ("%s: size %u is not a multiple of unit size %u",
632 GST_ELEMENT_NAME (trans), size, inunitsize);
637 GST_DEBUG_OBJECT (trans, "could not get out_size");
638 g_warning ("%s: could not get out_size", GST_ELEMENT_NAME (trans));
643 /* get the caps that can be handled by @pad. We perform:
645 * - take the caps of peer of otherpad,
646 * - filter against the padtemplate of otherpad,
647 * - calculate all transforms of remaining caps
648 * - filter against template of @pad
650 * If there is no peer, we simply return the caps of the padtemplate of pad.
653 gst_base_transform_getcaps (GstPad * pad)
655 GstBaseTransform *trans;
657 GstCaps *peercaps, *caps;
659 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
661 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
663 /* we can do what the peer can */
664 peercaps = gst_pad_peer_get_caps_reffed (otherpad);
667 const GstCaps *templ;
669 GST_DEBUG_OBJECT (pad, "peer caps %" GST_PTR_FORMAT, peercaps);
671 /* filtered against our padtemplate on the other side */
672 templ = gst_pad_get_pad_template_caps (otherpad);
673 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
674 temp = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
675 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
677 /* then see what we can transform this to */
678 caps = gst_base_transform_transform_caps (trans,
679 GST_PAD_DIRECTION (otherpad), temp);
680 GST_DEBUG_OBJECT (pad, "transformed %" GST_PTR_FORMAT, caps);
681 gst_caps_unref (temp);
685 /* and filter against the template of this pad */
686 templ = gst_pad_get_pad_template_caps (pad);
687 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
688 /* We keep the caps sorted like the returned caps */
689 temp = gst_caps_intersect_full (caps, templ, GST_CAPS_INTERSECT_FIRST);
690 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
691 gst_caps_unref (caps);
694 /* Now try if we can put the untransformed downstream caps first */
695 temp = gst_caps_intersect_full (peercaps, caps, GST_CAPS_INTERSECT_FIRST);
696 if (!gst_caps_is_empty (temp)) {
697 gst_caps_merge (temp, caps);
700 gst_caps_unref (temp);
703 /* no peer or the peer can do anything, our padtemplate is enough then */
704 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
708 GST_DEBUG_OBJECT (trans, "returning %" GST_PTR_FORMAT, caps);
711 gst_caps_unref (peercaps);
713 gst_object_unref (trans);
718 /* function triggered when the in and out caps are negotiated and need
719 * to be configured in the subclass. */
721 gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
725 GstBaseTransformClass *klass;
727 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
729 GST_DEBUG_OBJECT (trans, "in caps: %" GST_PTR_FORMAT, in);
730 GST_DEBUG_OBJECT (trans, "out caps: %" GST_PTR_FORMAT, out);
732 /* clear the cache */
733 gst_caps_replace (&trans->cache_caps1, NULL);
734 gst_caps_replace (&trans->cache_caps2, NULL);
736 /* figure out same caps state */
737 trans->have_same_caps = gst_caps_is_equal (in, out);
738 GST_DEBUG_OBJECT (trans, "have_same_caps: %d", trans->have_same_caps);
740 /* If we've a transform_ip method and same input/output caps, set in_place
741 * by default. If for some reason the sub-class prefers using a transform
742 * function, it can clear the in place flag in the set_caps */
743 gst_base_transform_set_in_place (trans,
744 klass->transform_ip && trans->have_same_caps);
746 /* Set the passthrough if the class wants passthrough_on_same_caps
747 * and we have the same caps on each pad */
748 if (klass->passthrough_on_same_caps)
749 gst_base_transform_set_passthrough (trans, trans->have_same_caps);
751 /* now configure the element with the caps */
752 if (klass->set_caps) {
753 GST_DEBUG_OBJECT (trans, "Calling set_caps method to setup caps");
754 ret = klass->set_caps (trans, in, out);
757 GST_OBJECT_LOCK (trans);
758 /* make sure we reevaluate how the buffer_alloc works wrt to proxy allocating
759 * the buffer. FIXME, this triggers some quite heavy codepaths that don't need
761 trans->priv->suggest_pending = TRUE;
762 GST_OBJECT_UNLOCK (trans);
763 trans->negotiated = ret;
768 /* check if caps @in on @pad can be transformed to @out on the other pad.
769 * We don't have a vmethod to test this yet so we have to do a somewhat less
770 * efficient check for this.
773 gst_base_transform_can_transform (GstBaseTransform * trans, GstPad * pad,
774 GstCaps * in, GstCaps * out)
778 /* convert the in caps to all possible out caps */
780 gst_base_transform_transform_caps (trans, GST_PAD_DIRECTION (pad), in);
782 /* check if transform is empty */
783 if (!othercaps || gst_caps_is_empty (othercaps))
786 /* check if the out caps is a subset of the othercaps */
787 if (!gst_caps_can_intersect (out, othercaps))
791 gst_caps_unref (othercaps);
793 GST_DEBUG_OBJECT (trans, "from %" GST_PTR_FORMAT, in);
794 GST_DEBUG_OBJECT (trans, "to %" GST_PTR_FORMAT, out);
801 GST_DEBUG_OBJECT (trans,
802 "transform returned useless %" GST_PTR_FORMAT, othercaps);
804 gst_caps_unref (othercaps);
809 GST_DEBUG_OBJECT (trans, "no subset");
811 gst_caps_unref (othercaps);
816 /* given a fixed @caps on @pad, create the best possible caps for the
818 * @caps must be fixed when calling this function.
820 * This function calls the transform caps vmethod of the basetransform to figure
821 * out the possible target formats. It then tries to select the best format from
824 * - attempt passthrough if the target caps is a superset of the input caps
825 * - fixating by using peer caps
826 * - fixating with transform fixate function
827 * - fixating with pad fixate functions.
829 * this function returns a caps that can be transformed into and is accepted by
833 gst_base_transform_find_transform (GstBaseTransform * trans, GstPad * pad,
836 GstBaseTransformClass *klass;
837 GstPad *otherpad, *otherpeer;
839 gboolean peer_checked = FALSE;
842 /* caps must be fixed here, this is a programming error if it's not */
843 g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
845 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
847 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
848 otherpeer = gst_pad_get_peer (otherpad);
850 /* see how we can transform the input caps. We need to do this even for
851 * passthrough because it might be possible that this element cannot support
852 * passthrough at all. */
853 othercaps = gst_base_transform_transform_caps (trans,
854 GST_PAD_DIRECTION (pad), caps);
856 /* The caps we can actually output is the intersection of the transformed
857 * caps with the pad template for the pad */
860 const GstCaps *templ_caps;
862 templ_caps = gst_pad_get_pad_template_caps (otherpad);
863 GST_DEBUG_OBJECT (trans,
864 "intersecting against padtemplate %" GST_PTR_FORMAT, templ_caps);
867 gst_caps_intersect_full (othercaps, templ_caps,
868 GST_CAPS_INTERSECT_FIRST);
870 gst_caps_unref (othercaps);
871 othercaps = intersect;
874 /* check if transform is empty */
875 if (!othercaps || gst_caps_is_empty (othercaps))
878 /* if the othercaps are not fixed, we need to fixate them, first attempt
879 * is by attempting passthrough if the othercaps are a superset of caps. */
880 /* FIXME. maybe the caps is not fixed because it has multiple structures of
882 is_fixed = gst_caps_is_fixed (othercaps);
884 GST_DEBUG_OBJECT (trans,
885 "transform returned non fixed %" GST_PTR_FORMAT, othercaps);
887 /* see if the target caps are a superset of the source caps, in this
888 * case we can try to perform passthrough */
889 if (gst_caps_can_intersect (othercaps, caps)) {
890 GST_DEBUG_OBJECT (trans, "try passthrough with %" GST_PTR_FORMAT, caps);
892 /* try passthrough. we know it's fixed, because caps is fixed */
893 if (gst_pad_accept_caps (otherpeer, caps)) {
894 GST_DEBUG_OBJECT (trans, "peer accepted %" GST_PTR_FORMAT, caps);
895 /* peer accepted unmodified caps, we free the original non-fixed
896 * caps and work with the passthrough caps */
897 gst_caps_unref (othercaps);
898 othercaps = gst_caps_ref (caps);
900 /* mark that we checked othercaps with the peer, this
901 * makes sure we don't call accept_caps again with these same
905 GST_DEBUG_OBJECT (trans,
906 "peer did not accept %" GST_PTR_FORMAT, caps);
909 GST_DEBUG_OBJECT (trans, "no peer, doing passthrough");
910 gst_caps_unref (othercaps);
911 othercaps = gst_caps_ref (caps);
917 /* second attempt at fixation is done by intersecting with
919 if (!is_fixed && otherpeer) {
920 /* intersect against what the peer can do */
924 GST_DEBUG_OBJECT (trans, "othercaps now %" GST_PTR_FORMAT, othercaps);
926 peercaps = gst_pad_get_caps_reffed (otherpeer);
927 intersect = gst_caps_intersect (peercaps, othercaps);
928 gst_caps_unref (peercaps);
929 gst_caps_unref (othercaps);
930 othercaps = intersect;
931 peer_checked = FALSE;
933 is_fixed = gst_caps_is_fixed (othercaps);
935 GST_DEBUG_OBJECT (trans,
936 "filtering against peer yields %" GST_PTR_FORMAT, othercaps);
939 if (gst_caps_is_empty (othercaps))
940 goto no_transform_possible;
942 /* third attempt at fixation, call the fixate vmethod and
943 * ultimately call the pad fixate function. */
945 GST_DEBUG_OBJECT (trans,
946 "trying to fixate %" GST_PTR_FORMAT " on pad %s:%s",
947 othercaps, GST_DEBUG_PAD_NAME (otherpad));
949 /* since we have no other way to fixate left, we might as well just take
950 * the first of the caps list and fixate that */
952 /* FIXME: when fixating using the vmethod, it might make sense to fixate
953 * each of the caps; but Wim doesn't see a use case for that yet */
954 gst_caps_truncate (othercaps);
955 peer_checked = FALSE;
957 if (klass->fixate_caps) {
958 GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
959 " using caps %" GST_PTR_FORMAT
960 " on pad %s:%s using fixate_caps vmethod", othercaps, caps,
961 GST_DEBUG_PAD_NAME (otherpad));
962 klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
963 is_fixed = gst_caps_is_fixed (othercaps);
965 /* if still not fixed, no other option but to let the default pad fixate
966 * function do its job */
968 GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
969 " on pad %s:%s using gst_pad_fixate_caps", othercaps,
970 GST_DEBUG_PAD_NAME (otherpad));
971 gst_pad_fixate_caps (otherpad, othercaps);
972 is_fixed = gst_caps_is_fixed (othercaps);
974 GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
976 GST_DEBUG ("caps are fixed");
977 /* else caps are fixed but the subclass may want to add fields */
978 if (klass->fixate_caps) {
979 othercaps = gst_caps_make_writable (othercaps);
981 GST_DEBUG_OBJECT (trans, "doing fixate %" GST_PTR_FORMAT
982 " using caps %" GST_PTR_FORMAT
983 " on pad %s:%s using fixate_caps vmethod", othercaps, caps,
984 GST_DEBUG_PAD_NAME (otherpad));
986 klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
987 is_fixed = gst_caps_is_fixed (othercaps);
991 /* caps should be fixed now, if not we have to fail. */
993 goto could_not_fixate;
995 /* and peer should accept, don't check again if we already checked the
996 * othercaps against the peer. */
997 if (!peer_checked && otherpeer && !gst_pad_accept_caps (otherpeer, othercaps))
1000 GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT
1001 ", and got final caps %" GST_PTR_FORMAT, caps, othercaps);
1004 gst_object_unref (otherpeer);
1011 GST_DEBUG_OBJECT (trans,
1012 "transform returned useless %" GST_PTR_FORMAT, othercaps);
1015 no_transform_possible:
1017 GST_DEBUG_OBJECT (trans,
1018 "transform could not transform %" GST_PTR_FORMAT
1019 " in anything we support", caps);
1024 GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
1029 GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
1030 " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1036 gst_object_unref (otherpeer);
1038 gst_caps_unref (othercaps);
1044 gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
1045 GstPadDirection direction, GstCaps * caps)
1049 GstCaps *othercaps = NULL;
1051 gboolean ret = TRUE;
1054 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1056 /* we need fixed caps for the check, fall back to the default implementation
1058 if (!gst_caps_is_fixed (caps))
1063 GST_DEBUG_OBJECT (trans, "non fixed accept caps %" GST_PTR_FORMAT, caps);
1065 /* get all the formats we can handle on this pad */
1066 if (direction == GST_PAD_SRC)
1067 allowed = gst_pad_get_caps_reffed (trans->srcpad);
1069 allowed = gst_pad_get_caps_reffed (trans->sinkpad);
1072 GST_DEBUG_OBJECT (trans, "gst_pad_get_caps() failed");
1073 goto no_transform_possible;
1076 GST_DEBUG_OBJECT (trans, "allowed caps %" GST_PTR_FORMAT, allowed);
1078 /* intersect with the requested format */
1079 ret = gst_caps_can_intersect (allowed, caps);
1080 gst_caps_unref (allowed);
1083 goto no_transform_possible;
1087 GST_DEBUG_OBJECT (pad, "accept caps %" GST_PTR_FORMAT, caps);
1089 /* find best possible caps for the other pad as a way to see if we can
1090 * transform this caps. */
1091 othercaps = gst_base_transform_find_transform (trans, pad, caps);
1092 if (!othercaps || gst_caps_is_empty (othercaps))
1093 goto no_transform_possible;
1095 GST_DEBUG_OBJECT (pad, "we can transform to %" GST_PTR_FORMAT, othercaps);
1101 /* We know it's always NULL since we never use it */
1103 gst_caps_unref (othercaps);
1109 no_transform_possible:
1111 GST_DEBUG_OBJECT (trans,
1112 "transform could not transform %" GST_PTR_FORMAT
1113 " in anything we support", caps);
1120 gst_base_transform_acceptcaps (GstPad * pad, GstCaps * caps)
1122 gboolean ret = TRUE;
1123 GstBaseTransform *trans;
1124 GstBaseTransformClass *bclass;
1126 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1127 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1129 if (bclass->accept_caps)
1130 ret = bclass->accept_caps (trans, GST_PAD_DIRECTION (pad), caps);
1132 gst_object_unref (trans);
1137 /* called when new caps arrive on the sink or source pad,
1138 * We try to find the best caps for the other side using our _find_transform()
1139 * function. If there are caps, we configure the transform for this new
1142 * FIXME, this function is currently commutative but this should not really be
1143 * because we never set caps starting from the srcpad.
1146 gst_base_transform_setcaps (GstPad * pad, GstCaps * caps)
1148 GstBaseTransform *trans;
1149 GstPad *otherpad, *otherpeer;
1150 GstCaps *othercaps = NULL;
1151 gboolean ret = TRUE;
1152 GstCaps *incaps, *outcaps;
1154 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1156 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1157 otherpeer = gst_pad_get_peer (otherpad);
1159 /* if we get called recursively, we bail out now to avoid an
1161 if (GST_PAD_IS_IN_SETCAPS (otherpad))
1164 GST_DEBUG_OBJECT (pad, "have new caps %p %" GST_PTR_FORMAT, caps, caps);
1166 /* find best possible caps for the other pad */
1167 othercaps = gst_base_transform_find_transform (trans, pad, caps);
1168 if (!othercaps || gst_caps_is_empty (othercaps))
1169 goto no_transform_possible;
1171 /* configure the element now */
1172 /* make sure in and out caps are correct */
1173 if (pad == trans->sinkpad) {
1175 outcaps = othercaps;
1181 /* if we have the same caps, we can optimize and reuse the input caps */
1182 if (gst_caps_is_equal (incaps, outcaps)) {
1183 GST_INFO_OBJECT (trans, "reuse caps");
1184 gst_caps_unref (othercaps);
1185 outcaps = othercaps = gst_caps_ref (incaps);
1188 /* call configure now */
1189 if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
1190 goto failed_configure;
1192 /* we know this will work, we implement the setcaps */
1193 gst_pad_set_caps (otherpad, othercaps);
1195 if (pad == trans->srcpad && trans->priv->pad_mode == GST_ACTIVATE_PULL) {
1197 ret &= gst_pad_set_caps (otherpeer, othercaps);
1199 GST_INFO_OBJECT (trans, "otherpeer setcaps(%" GST_PTR_FORMAT ") failed",
1205 /* new caps, force alloc on next buffer on the chain */
1206 trans->priv->force_alloc = TRUE;
1208 gst_object_unref (otherpeer);
1210 gst_caps_unref (othercaps);
1212 trans->negotiated = ret;
1214 gst_object_unref (trans);
1219 no_transform_possible:
1221 GST_WARNING_OBJECT (trans,
1222 "transform could not transform %" GST_PTR_FORMAT
1223 " in anything we support", caps);
1229 GST_WARNING_OBJECT (trans, "FAILED to configure caps %" GST_PTR_FORMAT
1230 " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1237 gst_base_transform_query (GstPad * pad, GstQuery * query)
1239 gboolean ret = FALSE;
1240 GstBaseTransform *trans;
1243 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1244 if (G_UNLIKELY (trans == NULL))
1246 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1248 switch (GST_QUERY_TYPE (query)) {
1249 case GST_QUERY_POSITION:{
1252 gst_query_parse_position (query, &format, NULL);
1253 if (format == GST_FORMAT_TIME && trans->segment.format == GST_FORMAT_TIME) {
1257 if ((pad == trans->sinkpad)
1258 || (trans->priv->last_stop_out == GST_CLOCK_TIME_NONE)) {
1260 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1261 trans->segment.last_stop);
1263 pos = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1264 trans->priv->last_stop_out);
1266 gst_query_set_position (query, format, pos);
1268 ret = gst_pad_peer_query (otherpad, query);
1273 ret = gst_pad_peer_query (otherpad, query);
1277 gst_object_unref (trans);
1281 static const GstQueryType *
1282 gst_base_transform_query_type (GstPad * pad)
1284 static const GstQueryType types[] = {
1293 compute_upstream_suggestion (GstBaseTransform * trans, guint expsize,
1297 GstBaseTransformPrivate *priv = trans->priv;
1299 GST_DEBUG_OBJECT (trans, "trying to find upstream suggestion");
1301 /* we cannot convert the current buffer but we might be able to suggest a
1302 * new format upstream, try to find what the best format is. */
1303 othercaps = gst_base_transform_find_transform (trans, trans->srcpad, caps);
1306 GST_DEBUG_OBJECT (trans, "incompatible caps, ignoring");
1307 /* we received caps that we cannot transform. Upstream is behaving badly
1308 * because it should have checked if we could handle these caps. We can
1309 * simply ignore these caps and produce a buffer with our original caps. */
1313 GST_DEBUG_OBJECT (trans, "getting size of suggestion");
1315 /* not a subset, we have a new upstream suggestion, remember it and
1316 * allocate a default buffer. First we try to convert the size */
1317 if (gst_base_transform_transform_size (trans,
1318 GST_PAD_SRC, caps, expsize, othercaps, &size_suggest)) {
1320 /* ok, remember the suggestions now */
1321 GST_DEBUG_OBJECT (trans,
1322 "storing new caps and size suggestion of %u and %" GST_PTR_FORMAT,
1323 size_suggest, othercaps);
1325 GST_OBJECT_LOCK (trans->sinkpad);
1326 if (priv->sink_suggest)
1327 gst_caps_unref (priv->sink_suggest);
1328 priv->sink_suggest = gst_caps_ref (othercaps);
1329 priv->size_suggest = size_suggest;
1330 trans->priv->suggest_pending = TRUE;
1331 GST_OBJECT_UNLOCK (trans->sinkpad);
1333 gst_caps_unref (othercaps);
1337 /* Allocate a buffer using gst_pad_alloc_buffer
1339 * This function can do renegotiation on the source pad
1341 * The output buffer is always writable. outbuf can be equal to
1342 * inbuf, the caller should be prepared for this and perform
1343 * appropriate refcounting.
1345 static GstFlowReturn
1346 gst_base_transform_prepare_output_buffer (GstBaseTransform * trans,
1347 GstBuffer * in_buf, GstBuffer ** out_buf)
1349 GstBaseTransformClass *bclass;
1350 GstBaseTransformPrivate *priv;
1351 GstFlowReturn ret = GST_FLOW_OK;
1352 guint outsize, newsize, expsize;
1353 gboolean discard, setcaps, copymeta;
1354 GstCaps *incaps, *oldcaps, *newcaps, *outcaps;
1356 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1362 /* figure out how to allocate a buffer based on the current configuration */
1363 if (trans->passthrough) {
1364 GST_DEBUG_OBJECT (trans, "doing passthrough alloc");
1365 /* passthrough, we don't really need to call pad alloc but we still need to
1366 * in order to get upstream negotiation. The output size is the same as the
1368 outsize = GST_BUFFER_SIZE (in_buf);
1369 /* we always alloc and discard here */
1372 gboolean want_in_place = (bclass->transform_ip != NULL)
1373 && trans->always_in_place;
1375 if (want_in_place) {
1376 GST_DEBUG_OBJECT (trans, "doing inplace alloc");
1377 /* we alloc a buffer of the same size as the input */
1378 outsize = GST_BUFFER_SIZE (in_buf);
1379 /* only discard it when the input was not writable, otherwise, we reuse
1380 * the input buffer. */
1381 discard = gst_buffer_is_writable (in_buf);
1382 GST_DEBUG_OBJECT (trans, "discard: %d", discard);
1384 GST_DEBUG_OBJECT (trans, "getting output size for copy transform");
1385 /* copy transform, figure out the output size */
1386 if (!gst_base_transform_transform_size (trans,
1387 GST_PAD_SINK, GST_PAD_CAPS (trans->sinkpad),
1388 GST_BUFFER_SIZE (in_buf), GST_PAD_CAPS (trans->srcpad),
1392 /* never discard this buffer, we need it for storing the output */
1397 oldcaps = GST_PAD_CAPS (trans->srcpad);
1399 if (bclass->prepare_output_buffer) {
1400 GST_DEBUG_OBJECT (trans,
1401 "calling prepare buffer with caps %p %" GST_PTR_FORMAT, oldcaps,
1404 bclass->prepare_output_buffer (trans, in_buf, outsize, oldcaps,
1407 /* get a new ref to the srcpad caps, the prepare_output_buffer function can
1408 * update the pad caps if it wants */
1409 oldcaps = GST_PAD_CAPS (trans->srcpad);
1412 * decrease refcount again if vmethod returned refcounted in_buf. This
1413 * is because we need to make sure that the buffer is writable for the
1414 * in_place transform. The docs of the vmethod say that you should return
1415 * a reffed inbuf, which is exactly what we don't want :), oh well.. */
1416 if (in_buf == *out_buf)
1417 gst_buffer_unref (in_buf);
1419 /* never discard the buffer from the prepare_buffer method */
1420 if (*out_buf != NULL)
1424 if (ret != GST_FLOW_OK)
1427 if (*out_buf == NULL) {
1428 if (trans->passthrough && !trans->priv->force_alloc) {
1429 GST_DEBUG_OBJECT (trans, "Avoiding pad alloc");
1430 *out_buf = gst_buffer_ref (in_buf);
1432 GST_DEBUG_OBJECT (trans, "doing alloc with caps %" GST_PTR_FORMAT,
1435 ret = gst_pad_alloc_buffer (trans->srcpad,
1436 GST_BUFFER_OFFSET (in_buf), outsize, oldcaps, out_buf);
1437 if (ret != GST_FLOW_OK)
1442 /* must always have a buffer by now */
1443 if (*out_buf == NULL)
1446 /* check if we got different caps on this new output buffer */
1447 newcaps = GST_BUFFER_CAPS (*out_buf);
1448 newsize = GST_BUFFER_SIZE (*out_buf);
1450 if (newcaps && !gst_caps_is_equal (newcaps, oldcaps)) {
1452 gboolean can_convert;
1454 GST_DEBUG_OBJECT (trans, "received new caps %" GST_PTR_FORMAT, newcaps);
1456 incaps = GST_PAD_CAPS (trans->sinkpad);
1458 /* check if we can convert the current incaps to the new target caps */
1460 gst_base_transform_can_transform (trans, trans->sinkpad, incaps,
1464 GST_DEBUG_OBJECT (trans, "cannot perform transform on current buffer");
1466 gst_base_transform_transform_size (trans,
1467 GST_PAD_SINK, incaps, GST_BUFFER_SIZE (in_buf), newcaps, &expsize);
1469 compute_upstream_suggestion (trans, expsize, newcaps);
1471 /* we got a suggested caps but we can't transform to it. See if there is
1472 * another downstream format that we can transform to */
1474 gst_base_transform_find_transform (trans, trans->sinkpad, incaps);
1476 if (othercaps && !gst_caps_is_empty (othercaps)) {
1477 GST_DEBUG_OBJECT (trans, "we found target caps %" GST_PTR_FORMAT,
1479 *out_buf = gst_buffer_make_metadata_writable (*out_buf);
1480 gst_buffer_set_caps (*out_buf, othercaps);
1481 gst_caps_unref (othercaps);
1482 newcaps = GST_BUFFER_CAPS (*out_buf);
1484 } else if (othercaps)
1485 gst_caps_unref (othercaps);
1488 /* it's possible that the buffer we got is of the wrong size, get the
1489 * expected size here, we will check the size if we are going to use the
1490 * buffer later on. */
1491 gst_base_transform_transform_size (trans,
1492 GST_PAD_SINK, incaps, GST_BUFFER_SIZE (in_buf), newcaps, &expsize);
1495 GST_DEBUG_OBJECT (trans, "reconfigure transform for current buffer");
1497 /* subclass might want to add fields to the caps */
1498 if (bclass->fixate_caps != NULL) {
1499 newcaps = gst_caps_copy (newcaps);
1501 GST_DEBUG_OBJECT (trans, "doing fixate %" GST_PTR_FORMAT
1502 " using caps %" GST_PTR_FORMAT
1503 " on pad %s:%s using fixate_caps vmethod", newcaps, incaps,
1504 GST_DEBUG_PAD_NAME (trans->srcpad));
1505 bclass->fixate_caps (trans, GST_PAD_SINK, incaps, newcaps);
1507 *out_buf = gst_buffer_make_metadata_writable (*out_buf);
1508 gst_buffer_set_caps (*out_buf, newcaps);
1509 gst_caps_unref (newcaps);
1510 newcaps = GST_BUFFER_CAPS (*out_buf);
1513 /* caps not empty, try to renegotiate to the new format */
1514 if (!gst_base_transform_configure_caps (trans, incaps, newcaps)) {
1515 /* not sure we need to fail hard here, we can simply continue our
1516 * conversion with what we negotiated before */
1517 goto failed_configure;
1519 /* new format configure, and use the new output buffer */
1520 gst_pad_set_caps (trans->srcpad, newcaps);
1522 /* clear previous cached sink-pad caps, so buffer_alloc knows that
1523 * it needs to revisit the decision about whether to proxy or not: */
1524 gst_caps_replace (&priv->sink_alloc, NULL);
1525 /* if we got a buffer of the wrong size, discard it now and make sure we
1526 * allocate a propertly sized buffer later. */
1527 if (newsize != expsize) {
1528 if (in_buf != *out_buf)
1529 gst_buffer_unref (*out_buf);
1534 compute_upstream_suggestion (trans, expsize, newcaps);
1536 if (in_buf != *out_buf)
1537 gst_buffer_unref (*out_buf);
1540 } else if (outsize != newsize) {
1541 GST_WARNING_OBJECT (trans, "Caps did not change but allocated size does "
1542 "not match expected size (%d != %d)", newsize, outsize);
1543 if (in_buf != *out_buf)
1544 gst_buffer_unref (*out_buf);
1548 /* these are the final output caps */
1549 outcaps = GST_PAD_CAPS (trans->srcpad);
1552 if (*out_buf == NULL) {
1554 GST_DEBUG_OBJECT (trans, "make default output buffer of size %d",
1556 /* no valid buffer yet, make one, metadata is writable */
1557 *out_buf = gst_buffer_new_and_alloc (outsize);
1558 gst_buffer_copy_metadata (*out_buf, in_buf,
1559 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
1561 GST_DEBUG_OBJECT (trans, "reuse input buffer");
1565 if (trans->passthrough && in_buf != *out_buf) {
1566 /* we are asked to perform a passthrough transform but the input and
1567 * output buffers are different. We have to discard the output buffer and
1568 * reuse the input buffer. */
1569 GST_DEBUG_OBJECT (trans, "passthrough but different buffers");
1573 GST_DEBUG_OBJECT (trans, "discard buffer, reuse input buffer");
1574 gst_buffer_unref (*out_buf);
1577 GST_DEBUG_OBJECT (trans, "using allocated buffer in %p, out %p", in_buf,
1579 /* if we have different buffers, check if the metadata is ok */
1580 if (*out_buf != in_buf) {
1583 mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
1584 GST_BUFFER_FLAG_DELTA_UNIT | GST_BUFFER_FLAG_DISCONT |
1585 GST_BUFFER_FLAG_GAP | GST_BUFFER_FLAG_MEDIA1 |
1586 GST_BUFFER_FLAG_MEDIA2 | GST_BUFFER_FLAG_MEDIA3;
1587 /* see if the flags and timestamps match */
1589 (GST_MINI_OBJECT_FLAGS (*out_buf) & mask) ==
1590 (GST_MINI_OBJECT_FLAGS (in_buf) & mask);
1592 GST_BUFFER_TIMESTAMP (*out_buf) != GST_BUFFER_TIMESTAMP (in_buf) ||
1593 GST_BUFFER_DURATION (*out_buf) != GST_BUFFER_DURATION (in_buf) ||
1594 GST_BUFFER_OFFSET (*out_buf) != GST_BUFFER_OFFSET (in_buf) ||
1595 GST_BUFFER_OFFSET_END (*out_buf) != GST_BUFFER_OFFSET_END (in_buf);
1600 /* check if we need to make things writable. We need this when we need to
1601 * update the caps or the metadata on the output buffer. */
1602 newcaps = GST_BUFFER_CAPS (*out_buf);
1603 /* we check the pointers as a quick check and then go to the more involved
1604 * check. This is needed when we receive different pointers on the sinkpad
1605 * that mean the same caps. What we then want to do is prefer those caps over
1606 * the ones on the srcpad and set the srcpad caps to the buffer caps */
1607 setcaps = !newcaps || ((newcaps != outcaps)
1608 && (!gst_caps_is_equal (newcaps, outcaps)));
1609 /* we need to modify the metadata when the element is not gap aware,
1610 * passthrough is not used and the gap flag is set */
1611 copymeta |= !trans->priv->gap_aware && !trans->passthrough
1612 && (GST_MINI_OBJECT_FLAGS (*out_buf) & GST_BUFFER_FLAG_GAP);
1614 if (setcaps || copymeta) {
1615 GST_DEBUG_OBJECT (trans, "setcaps %d, copymeta %d", setcaps, copymeta);
1616 if (!gst_buffer_is_metadata_writable (*out_buf)) {
1617 GST_DEBUG_OBJECT (trans, "buffer metadata %p not writable", *out_buf);
1618 if (in_buf == *out_buf)
1619 *out_buf = gst_buffer_create_sub (in_buf, 0, GST_BUFFER_SIZE (in_buf));
1621 *out_buf = gst_buffer_make_metadata_writable (*out_buf);
1623 /* when we get here, the metadata should be writable */
1625 gst_buffer_set_caps (*out_buf, outcaps);
1627 gst_buffer_copy_metadata (*out_buf, in_buf,
1628 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
1629 /* clear the GAP flag when the subclass does not understand it */
1630 if (!trans->priv->gap_aware)
1631 GST_BUFFER_FLAG_UNSET (*out_buf, GST_BUFFER_FLAG_GAP);
1639 GST_WARNING_OBJECT (trans, "pad-alloc failed: %s", gst_flow_get_name (ret));
1644 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
1645 ("Sub-class failed to provide an output buffer"), (NULL));
1646 return GST_FLOW_ERROR;
1650 GST_ERROR_OBJECT (trans, "unknown output size");
1651 return GST_FLOW_ERROR;
1655 GST_WARNING_OBJECT (trans, "failed to configure caps");
1656 return GST_FLOW_NOT_NEGOTIATED;
1660 /* Given @caps calcultate the size of one unit.
1662 * For video caps, this is the size of one frame (and thus one buffer).
1663 * For audio caps, this is the size of one sample.
1665 * These values are cached since they do not change and the calculation
1666 * potentially involves parsing caps and other expensive stuff.
1668 * We have two cache locations to store the size, one for the source caps
1669 * and one for the sink caps.
1671 * this function returns FALSE if no size could be calculated.
1674 gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
1677 gboolean res = FALSE;
1678 GstBaseTransformClass *bclass;
1680 /* see if we have the result cached */
1681 if (trans->cache_caps1 == caps) {
1682 *size = trans->cache_caps1_size;
1683 GST_DEBUG_OBJECT (trans, "returned %d from first cache", *size);
1686 if (trans->cache_caps2 == caps) {
1687 *size = trans->cache_caps2_size;
1688 GST_DEBUG_OBJECT (trans, "returned %d from second cached", *size);
1692 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1693 if (bclass->get_unit_size) {
1694 res = bclass->get_unit_size (trans, caps, size);
1695 GST_DEBUG_OBJECT (trans, "caps %" GST_PTR_FORMAT
1696 ") has unit size %d, result %s", caps, *size, res ? "TRUE" : "FALSE");
1699 /* and cache the values */
1700 if (trans->cache_caps1 == NULL) {
1701 gst_caps_replace (&trans->cache_caps1, caps);
1702 trans->cache_caps1_size = *size;
1703 GST_DEBUG_OBJECT (trans, "caching %d in first cache", *size);
1704 } else if (trans->cache_caps2 == NULL) {
1705 gst_caps_replace (&trans->cache_caps2, caps);
1706 trans->cache_caps2_size = *size;
1707 GST_DEBUG_OBJECT (trans, "caching %d in second cache", *size);
1709 GST_DEBUG_OBJECT (trans, "no free spot to cache unit_size");
1713 GST_DEBUG_OBJECT (trans, "Sub-class does not implement get_unit_size");
1718 /* your upstream peer wants to send you a buffer
1719 * that buffer has the given offset, size and caps
1720 * you're requested to allocate a buffer
1722 static GstFlowReturn
1723 gst_base_transform_buffer_alloc (GstPad * pad, guint64 offset, guint size,
1724 GstCaps * caps, GstBuffer ** buf)
1726 GstBaseTransform *trans;
1727 GstBaseTransformClass *klass;
1728 GstBaseTransformPrivate *priv;
1730 gboolean alloced = FALSE;
1731 gboolean proxy, suggest, same_caps;
1732 GstCaps *sink_suggest = NULL;
1735 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1736 if (G_UNLIKELY (trans == NULL))
1737 return GST_FLOW_WRONG_STATE;
1738 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1741 GST_DEBUG_OBJECT (pad, "alloc with caps %p %" GST_PTR_FORMAT ", size %u",
1744 /* if the code below does not come up with a better buffer, we will return _OK
1745 * and an empty buffer. This will trigger the core to allocate a buffer with
1746 * given input size and caps. */
1750 /* we remember our previous alloc request to quickly see if we can proxy or
1751 * not. We skip this check if we have a pending suggestion. */
1752 GST_OBJECT_LOCK (pad);
1753 same_caps = !priv->suggest_pending && caps &&
1754 gst_caps_is_equal (priv->sink_alloc, caps);
1755 GST_OBJECT_UNLOCK (pad);
1758 /* we have seen this before, see below if we need to proxy */
1759 GST_DEBUG_OBJECT (trans, "have old caps %p, size %u", caps, size);
1760 gst_caps_replace (&sink_suggest, caps);
1761 size_suggest = size;
1764 GST_DEBUG_OBJECT (trans, "new format %p %" GST_PTR_FORMAT, caps, caps);
1766 /* if we have a suggestion, pretend we got these as input */
1767 GST_OBJECT_LOCK (pad);
1768 if ((priv->sink_suggest && !gst_caps_is_equal (caps, priv->sink_suggest))) {
1769 sink_suggest = gst_caps_ref (priv->sink_suggest);
1770 size_suggest = priv->size_suggest;
1771 GST_DEBUG_OBJECT (trans, "have suggestion %p %" GST_PTR_FORMAT " size %u",
1772 sink_suggest, sink_suggest, priv->size_suggest);
1773 /* suggest is TRUE when we have a custom suggestion pending that we need
1774 * to unref later. */
1777 GST_DEBUG_OBJECT (trans, "using caps %p %" GST_PTR_FORMAT " size %u",
1779 gst_caps_replace (&sink_suggest, caps);
1780 size_suggest = size;
1783 priv->suggest_pending = FALSE;
1784 GST_OBJECT_UNLOCK (pad);
1786 /* check if we actually handle this format on the sinkpad */
1788 const GstCaps *templ;
1790 if (!gst_caps_is_fixed (sink_suggest)) {
1793 GST_DEBUG_OBJECT (trans, "Suggested caps is not fixed: %"
1794 GST_PTR_FORMAT, sink_suggest);
1797 gst_pad_peer_get_caps_reffed (GST_BASE_TRANSFORM_SINK_PAD (trans));
1798 /* try fixating by intersecting with peer caps */
1803 gst_caps_intersect_full (sink_suggest, peercaps,
1804 GST_CAPS_INTERSECT_FIRST);
1805 gst_caps_unref (peercaps);
1806 gst_caps_unref (sink_suggest);
1807 sink_suggest = intersect;
1810 if (gst_caps_is_empty (sink_suggest))
1813 /* try the alloc caps if it is still not fixed */
1814 if (!gst_caps_is_fixed (sink_suggest)) {
1817 GST_DEBUG_OBJECT (trans, "Checking if the input caps is compatible "
1818 "with the non-fixed caps suggestion");
1820 gst_caps_intersect_full (sink_suggest, caps,
1821 GST_CAPS_INTERSECT_FIRST);
1822 if (!gst_caps_is_empty (intersect)) {
1823 GST_DEBUG_OBJECT (trans, "It is, using it");
1824 gst_caps_replace (&sink_suggest, caps);
1826 gst_caps_unref (intersect);
1829 /* be safe and call default fixate */
1830 sink_suggest = gst_caps_make_writable (sink_suggest);
1831 gst_pad_fixate_caps (GST_BASE_TRANSFORM_SINK_PAD (trans), sink_suggest);
1833 if (!gst_caps_is_fixed (sink_suggest)) {
1834 gst_caps_unref (sink_suggest);
1835 sink_suggest = NULL;
1838 GST_DEBUG_OBJECT (trans, "Caps fixed to: %" GST_PTR_FORMAT,
1843 templ = gst_pad_get_pad_template_caps (pad);
1845 if (!gst_caps_can_intersect (sink_suggest, templ)) {
1849 GST_DEBUG_OBJECT (trans,
1850 "Requested pad alloc caps are not supported: %" GST_PTR_FORMAT,
1852 /* the requested pad alloc caps are not supported, so let's try
1853 * picking something allowed between the pads (they are linked,
1854 * there must be something) */
1855 allowed = gst_pad_get_allowed_caps (pad);
1856 if (allowed && !gst_caps_is_empty (allowed)) {
1857 GST_DEBUG_OBJECT (trans,
1858 "pads could agree on one of the following caps: " "%"
1859 GST_PTR_FORMAT, allowed);
1860 allowed = gst_caps_make_writable (allowed);
1862 if (klass->fixate_caps) {
1864 gst_pad_get_allowed_caps (GST_BASE_TRANSFORM_SRC_PAD (trans));
1865 klass->fixate_caps (trans, GST_PAD_SRC, peercaps, allowed);
1866 gst_caps_unref (peercaps);
1869 /* Fixate them to be safe if the subclass didn't do it */
1870 gst_caps_truncate (allowed);
1871 gst_pad_fixate_caps (pad, allowed);
1872 gst_caps_replace (&sink_suggest, allowed);
1873 gst_caps_unref (allowed);
1877 GST_DEBUG_OBJECT (trans, "Fixated suggestion caps to %"
1878 GST_PTR_FORMAT, sink_suggest);
1881 gst_caps_unref (allowed);
1888 /* find the best format for the other side here we decide if we will proxy
1889 * the caps or not. */
1890 if (sink_suggest == NULL) {
1891 /* always proxy when the caps are NULL. When this is a new format, see if
1892 * we can proxy it downstream */
1893 GST_DEBUG_OBJECT (trans, "null caps, marking for proxy");
1894 priv->proxy_alloc = TRUE;
1898 /* we have a new format, see what we need to proxy to */
1899 othercaps = gst_base_transform_find_transform (trans, pad, sink_suggest);
1900 if (!othercaps || gst_caps_is_empty (othercaps)) {
1901 /* no transform possible, we certainly can't proxy */
1902 GST_DEBUG_OBJECT (trans, "can't find transform, disable proxy");
1903 priv->proxy_alloc = FALSE;
1905 /* we transformed into something */
1906 if (gst_caps_is_equal (sink_suggest, othercaps)) {
1907 GST_DEBUG_OBJECT (trans,
1908 "best caps same as input, marking for proxy");
1909 priv->proxy_alloc = TRUE;
1911 GST_DEBUG_OBJECT (trans,
1912 "best caps different from input, disable proxy");
1913 priv->proxy_alloc = FALSE;
1917 gst_caps_unref (othercaps);
1920 /* remember the new caps */
1921 GST_OBJECT_LOCK (pad);
1922 gst_caps_replace (&priv->sink_alloc, sink_suggest);
1923 GST_OBJECT_UNLOCK (pad);
1925 proxy = priv->proxy_alloc;
1926 GST_DEBUG_OBJECT (trans, "doing default alloc, proxy %d, suggest %d", proxy,
1929 /* we only want to proxy if we have no suggestion pending, FIXME */
1930 if (proxy && !suggest) {
1933 GST_DEBUG_OBJECT (trans, "proxy buffer-alloc with caps %p %" GST_PTR_FORMAT
1934 ", size %u", caps, caps, size);
1936 /* we always proxy the input caps, never the suggestion. The reason is that
1937 * We don't yet handle the caps of renegotiation in here. FIXME */
1938 res = gst_pad_alloc_buffer (trans->srcpad, offset, size, caps, buf);
1939 if (res != GST_FLOW_OK)
1943 /* check if the caps changed */
1944 newcaps = GST_BUFFER_CAPS (*buf);
1946 GST_DEBUG_OBJECT (trans, "got caps %" GST_PTR_FORMAT, newcaps);
1948 if (!gst_caps_is_equal (newcaps, caps)) {
1949 GST_DEBUG_OBJECT (trans, "caps are new");
1950 /* we have new caps, see if we can proxy downstream */
1951 if (gst_pad_peer_accept_caps (pad, newcaps)) {
1952 /* peer accepts the caps, return a buffer in this format */
1953 GST_DEBUG_OBJECT (trans, "peer accepted new caps");
1954 /* remember the format */
1955 GST_OBJECT_LOCK (pad);
1956 gst_caps_replace (&priv->sink_alloc, newcaps);
1957 GST_OBJECT_UNLOCK (pad);
1959 GST_DEBUG_OBJECT (trans, "peer did not accept new caps");
1960 /* peer does not accept the caps, disable proxy_alloc, free the
1961 * buffer we received and create a buffer of the requested format
1962 * by the default handler. */
1963 GST_DEBUG_OBJECT (trans, "disabling proxy");
1964 priv->proxy_alloc = FALSE;
1965 gst_buffer_unref (*buf);
1969 GST_DEBUG_OBJECT (trans, "received required caps from peer");
1974 /* there was a custom suggestion, create a buffer of this format and return
1975 * it. Note that this format */
1976 *buf = gst_buffer_new_and_alloc (size_suggest);
1977 GST_DEBUG_OBJECT (trans,
1978 "doing suggestion of size %u, caps %p %" GST_PTR_FORMAT, size_suggest,
1979 sink_suggest, sink_suggest);
1980 GST_BUFFER_CAPS (*buf) = sink_suggest;
1981 sink_suggest = NULL;
1985 gst_caps_unref (sink_suggest);
1987 if (res == GST_FLOW_OK && alloced) {
1988 /* just alloc'ed a buffer, so we only want to do this again if we
1989 * received a buffer */
1990 GST_DEBUG_OBJECT (trans, "Cleaning force alloc");
1991 trans->priv->force_alloc = FALSE;
1994 gst_object_unref (trans);
2000 GST_DEBUG_OBJECT (trans, "pad alloc failed: %s", gst_flow_get_name (res));
2002 gst_caps_unref (sink_suggest);
2003 gst_object_unref (trans);
2008 GST_DEBUG_OBJECT (trans, "pad alloc with unsupported caps");
2010 gst_caps_unref (sink_suggest);
2011 gst_object_unref (trans);
2012 return GST_FLOW_NOT_NEGOTIATED;
2017 gst_base_transform_sink_event (GstPad * pad, GstEvent * event)
2019 GstBaseTransform *trans;
2020 GstBaseTransformClass *bclass;
2021 gboolean ret = TRUE;
2022 gboolean forward = TRUE;
2024 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2025 if (G_UNLIKELY (trans == NULL)) {
2026 gst_event_unref (event);
2029 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2032 forward = bclass->event (trans, event);
2034 /* FIXME, do this in the default event handler so the subclass can do
2035 * something different. */
2037 ret = gst_pad_push_event (trans->srcpad, event);
2039 gst_event_unref (event);
2041 gst_object_unref (trans);
2047 gst_base_transform_sink_eventfunc (GstBaseTransform * trans, GstEvent * event)
2049 switch (GST_EVENT_TYPE (event)) {
2050 case GST_EVENT_FLUSH_START:
2052 case GST_EVENT_FLUSH_STOP:
2053 GST_OBJECT_LOCK (trans);
2054 /* reset QoS parameters */
2055 trans->priv->proportion = 1.0;
2056 trans->priv->earliest_time = -1;
2057 trans->priv->discont = FALSE;
2058 trans->priv->processed = 0;
2059 trans->priv->dropped = 0;
2060 GST_OBJECT_UNLOCK (trans);
2061 /* we need new segment info after the flush. */
2062 trans->have_newsegment = FALSE;
2063 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
2064 trans->priv->last_stop_out = GST_CLOCK_TIME_NONE;
2070 case GST_EVENT_NEWSEGMENT:
2073 gdouble rate, arate;
2074 gint64 start, stop, time;
2077 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
2078 &start, &stop, &time);
2080 trans->have_newsegment = TRUE;
2082 gst_segment_set_newsegment_full (&trans->segment, update, rate, arate,
2083 format, start, stop, time);
2085 if (format == GST_FORMAT_TIME) {
2086 GST_DEBUG_OBJECT (trans, "received TIME NEW_SEGMENT %" GST_TIME_FORMAT
2087 " -- %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT
2088 ", accum %" GST_TIME_FORMAT,
2089 GST_TIME_ARGS (trans->segment.start),
2090 GST_TIME_ARGS (trans->segment.stop),
2091 GST_TIME_ARGS (trans->segment.time),
2092 GST_TIME_ARGS (trans->segment.accum));
2094 GST_DEBUG_OBJECT (trans, "received NEW_SEGMENT %" G_GINT64_FORMAT
2095 " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT
2096 ", accum %" G_GINT64_FORMAT,
2097 trans->segment.start, trans->segment.stop,
2098 trans->segment.time, trans->segment.accum);
2110 gst_base_transform_src_event (GstPad * pad, GstEvent * event)
2112 GstBaseTransform *trans;
2113 GstBaseTransformClass *bclass;
2114 gboolean ret = TRUE;
2116 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2117 if (G_UNLIKELY (trans == NULL)) {
2118 gst_event_unref (event);
2122 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2124 if (bclass->src_event)
2125 ret = bclass->src_event (trans, event);
2127 gst_event_unref (event);
2129 gst_object_unref (trans);
2135 gst_base_transform_src_eventfunc (GstBaseTransform * trans, GstEvent * event)
2139 GST_DEBUG_OBJECT (trans, "handling event %p %" GST_PTR_FORMAT, event, event);
2141 switch (GST_EVENT_TYPE (event)) {
2142 case GST_EVENT_SEEK:
2144 case GST_EVENT_NAVIGATION:
2149 GstClockTimeDiff diff;
2150 GstClockTime timestamp;
2152 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
2153 gst_base_transform_update_qos (trans, proportion, diff, timestamp);
2160 ret = gst_pad_push_event (trans->sinkpad, event);
2165 /* perform a transform on @inbuf and put the result in @outbuf.
2167 * This function is common to the push and pull-based operations.
2169 * This function takes ownership of @inbuf */
2170 static GstFlowReturn
2171 gst_base_transform_handle_buffer (GstBaseTransform * trans, GstBuffer * inbuf,
2172 GstBuffer ** outbuf)
2174 GstBaseTransformClass *bclass;
2175 GstFlowReturn ret = GST_FLOW_OK;
2176 gboolean want_in_place, reconfigure;
2177 GstClockTime running_time;
2178 GstClockTime timestamp;
2181 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2183 if (G_LIKELY ((incaps = GST_BUFFER_CAPS (inbuf)))) {
2184 GST_OBJECT_LOCK (trans);
2185 reconfigure = trans->priv->reconfigure;
2186 trans->priv->reconfigure = FALSE;
2187 GST_OBJECT_UNLOCK (trans);
2189 if (G_UNLIKELY (reconfigure)) {
2190 GST_DEBUG_OBJECT (trans, "we had a pending reconfigure");
2191 /* if we need to reconfigure we pretend a buffer with new caps arrived. This
2192 * will reconfigure the transform with the new output format. We can only
2193 * do this if the buffer actually has caps. */
2194 if (!gst_base_transform_setcaps (trans->sinkpad, incaps))
2195 goto not_negotiated;
2199 if (GST_BUFFER_OFFSET_IS_VALID (inbuf))
2200 GST_DEBUG_OBJECT (trans, "handling buffer %p of size %d and offset %"
2201 G_GUINT64_FORMAT, inbuf, GST_BUFFER_SIZE (inbuf),
2202 GST_BUFFER_OFFSET (inbuf));
2204 GST_DEBUG_OBJECT (trans, "handling buffer %p of size %d and offset NONE",
2205 inbuf, GST_BUFFER_SIZE (inbuf));
2207 /* Don't allow buffer handling before negotiation, except in passthrough mode
2208 * or if the class doesn't implement a set_caps function (in which case it doesn't
2211 if (!trans->negotiated && !trans->passthrough && (bclass->set_caps != NULL))
2212 goto not_negotiated;
2214 /* Set discont flag so we can mark the outgoing buffer */
2215 if (GST_BUFFER_IS_DISCONT (inbuf)) {
2216 GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", inbuf);
2217 trans->priv->discont = TRUE;
2220 /* can only do QoS if the segment is in TIME */
2221 if (trans->segment.format != GST_FORMAT_TIME)
2224 /* QOS is done on the running time of the buffer, get it now */
2225 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
2226 running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
2229 if (running_time != -1) {
2231 GstClockTime earliest_time;
2234 /* lock for getting the QoS parameters that are set (in a different thread)
2235 * with the QOS events */
2236 GST_OBJECT_LOCK (trans);
2237 earliest_time = trans->priv->earliest_time;
2238 proportion = trans->priv->proportion;
2239 /* check for QoS, don't perform conversion for buffers
2240 * that are known to be late. */
2241 need_skip = trans->priv->qos_enabled &&
2242 earliest_time != -1 && running_time <= earliest_time;
2243 GST_OBJECT_UNLOCK (trans);
2246 GstMessage *qos_msg;
2247 GstClockTime duration;
2248 guint64 stream_time;
2251 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "skipping transform: qostime %"
2252 GST_TIME_FORMAT " <= %" GST_TIME_FORMAT,
2253 GST_TIME_ARGS (running_time), GST_TIME_ARGS (earliest_time));
2255 trans->priv->dropped++;
2257 duration = GST_BUFFER_DURATION (inbuf);
2259 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
2261 jitter = GST_CLOCK_DIFF (running_time, earliest_time);
2264 gst_message_new_qos (GST_OBJECT_CAST (trans), FALSE, running_time,
2265 stream_time, timestamp, duration);
2266 gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
2267 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
2268 trans->priv->processed, trans->priv->dropped);
2269 gst_element_post_message (GST_ELEMENT_CAST (trans), qos_msg);
2271 /* mark discont for next buffer */
2272 trans->priv->discont = TRUE;
2279 /* first try to allocate an output buffer based on the currently negotiated
2280 * format. While we call pad-alloc we could renegotiate the srcpad format or
2281 * have a new suggestion for upstream buffer-alloc.
2282 * In any case, outbuf will contain a buffer suitable for doing the configured
2283 * transform after this function. */
2284 ret = gst_base_transform_prepare_output_buffer (trans, inbuf, outbuf);
2285 if (G_UNLIKELY (ret != GST_FLOW_OK))
2288 /* now perform the needed transform */
2289 if (trans->passthrough) {
2290 /* In passthrough mode, give transform_ip a look at the
2291 * buffer, without making it writable, or just push the
2293 if (bclass->transform_ip) {
2294 GST_DEBUG_OBJECT (trans, "doing passthrough transform");
2295 ret = bclass->transform_ip (trans, *outbuf);
2297 GST_DEBUG_OBJECT (trans, "element is in passthrough");
2300 want_in_place = (bclass->transform_ip != NULL) && trans->always_in_place;
2302 if (want_in_place) {
2303 GST_DEBUG_OBJECT (trans, "doing inplace transform");
2305 if (inbuf != *outbuf) {
2306 guint8 *indata, *outdata;
2308 /* Different buffer. The data can still be the same when we are dealing
2309 * with subbuffers of the same buffer. Note that because of the FIXME in
2310 * prepare_output_buffer() we have decreased the refcounts of inbuf and
2311 * outbuf to keep them writable */
2312 indata = GST_BUFFER_DATA (inbuf);
2313 outdata = GST_BUFFER_DATA (*outbuf);
2315 if (indata != outdata)
2316 memcpy (outdata, indata, GST_BUFFER_SIZE (inbuf));
2318 ret = bclass->transform_ip (trans, *outbuf);
2320 GST_DEBUG_OBJECT (trans, "doing non-inplace transform");
2322 if (bclass->transform)
2323 ret = bclass->transform (trans, inbuf, *outbuf);
2325 ret = GST_FLOW_NOT_SUPPORTED;
2330 /* only unref input buffer if we allocated a new outbuf buffer */
2331 if (*outbuf != inbuf)
2332 gst_buffer_unref (inbuf);
2334 /* pushed a buffer, we can now try an alloc */
2335 GST_DEBUG_OBJECT (trans, "Pushed a buffer, setting force alloc to true");
2336 trans->priv->force_alloc = TRUE;
2342 gst_buffer_unref (inbuf);
2343 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
2344 ("not negotiated"), ("not negotiated"));
2345 return GST_FLOW_NOT_NEGOTIATED;
2349 gst_buffer_unref (inbuf);
2350 GST_WARNING_OBJECT (trans, "could not get buffer from pool: %s",
2351 gst_flow_get_name (ret));
2357 gst_base_transform_check_get_range (GstPad * pad)
2359 GstBaseTransform *trans;
2362 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2364 ret = gst_pad_check_pull_range (trans->sinkpad);
2366 gst_object_unref (trans);
2371 /* FIXME, getrange is broken, need to pull range from the other
2372 * end based on the transform_size result.
2374 static GstFlowReturn
2375 gst_base_transform_getrange (GstPad * pad, guint64 offset,
2376 guint length, GstBuffer ** buffer)
2378 GstBaseTransform *trans;
2379 GstBaseTransformClass *klass;
2383 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2385 ret = gst_pad_pull_range (trans->sinkpad, offset, length, &inbuf);
2386 if (G_UNLIKELY (ret != GST_FLOW_OK))
2389 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2390 if (klass->before_transform)
2391 klass->before_transform (trans, inbuf);
2393 GST_BASE_TRANSFORM_LOCK (trans);
2394 ret = gst_base_transform_handle_buffer (trans, inbuf, buffer);
2395 GST_BASE_TRANSFORM_UNLOCK (trans);
2398 gst_object_unref (trans);
2405 GST_DEBUG_OBJECT (trans, "failed to pull a buffer: %s",
2406 gst_flow_get_name (ret));
2411 static GstFlowReturn
2412 gst_base_transform_chain (GstPad * pad, GstBuffer * buffer)
2414 GstBaseTransform *trans;
2415 GstBaseTransformClass *klass;
2417 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
2418 GstClockTime timestamp, duration;
2419 GstBuffer *outbuf = NULL;
2421 trans = GST_BASE_TRANSFORM (GST_OBJECT_PARENT (pad));
2423 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2424 duration = GST_BUFFER_DURATION (buffer);
2426 /* calculate end position of the incoming buffer */
2427 if (timestamp != GST_CLOCK_TIME_NONE) {
2428 if (duration != GST_CLOCK_TIME_NONE)
2429 last_stop = timestamp + duration;
2431 last_stop = timestamp;
2434 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2435 if (klass->before_transform)
2436 klass->before_transform (trans, buffer);
2438 /* protect transform method and concurrent buffer alloc */
2439 GST_BASE_TRANSFORM_LOCK (trans);
2440 ret = gst_base_transform_handle_buffer (trans, buffer, &outbuf);
2441 GST_BASE_TRANSFORM_UNLOCK (trans);
2443 /* outbuf can be NULL, this means a dropped buffer, if we have a buffer but
2444 * GST_BASE_TRANSFORM_FLOW_DROPPED we will not push either. */
2445 if (outbuf != NULL) {
2446 if ((ret == GST_FLOW_OK)) {
2447 GstClockTime last_stop_out = GST_CLOCK_TIME_NONE;
2449 /* Remember last stop position */
2450 if (last_stop != GST_CLOCK_TIME_NONE &&
2451 trans->segment.format == GST_FORMAT_TIME)
2452 gst_segment_set_last_stop (&trans->segment, GST_FORMAT_TIME, last_stop);
2454 if (GST_BUFFER_TIMESTAMP_IS_VALID (outbuf)) {
2455 last_stop_out = GST_BUFFER_TIMESTAMP (outbuf);
2456 if (GST_BUFFER_DURATION_IS_VALID (outbuf))
2457 last_stop_out += GST_BUFFER_DURATION (outbuf);
2458 } else if (last_stop != GST_CLOCK_TIME_NONE) {
2459 last_stop_out = last_stop;
2461 if (last_stop_out != GST_CLOCK_TIME_NONE
2462 && trans->segment.format == GST_FORMAT_TIME)
2463 trans->priv->last_stop_out = last_stop_out;
2465 /* apply DISCONT flag if the buffer is not yet marked as such */
2466 if (trans->priv->discont) {
2467 if (!GST_BUFFER_IS_DISCONT (outbuf)) {
2468 outbuf = gst_buffer_make_metadata_writable (outbuf);
2469 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2471 trans->priv->discont = FALSE;
2473 trans->priv->processed++;
2474 ret = gst_pad_push (trans->srcpad, outbuf);
2476 gst_buffer_unref (outbuf);
2480 /* convert internal flow to OK and mark discont for the next buffer. */
2481 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED) {
2482 trans->priv->discont = TRUE;
2490 gst_base_transform_set_property (GObject * object, guint prop_id,
2491 const GValue * value, GParamSpec * pspec)
2493 GstBaseTransform *trans;
2495 trans = GST_BASE_TRANSFORM (object);
2499 gst_base_transform_set_qos_enabled (trans, g_value_get_boolean (value));
2502 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2508 gst_base_transform_get_property (GObject * object, guint prop_id,
2509 GValue * value, GParamSpec * pspec)
2511 GstBaseTransform *trans;
2513 trans = GST_BASE_TRANSFORM (object);
2517 g_value_set_boolean (value, gst_base_transform_is_qos_enabled (trans));
2520 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2525 /* not a vmethod of anything, just an internal method */
2527 gst_base_transform_activate (GstBaseTransform * trans, gboolean active)
2529 GstBaseTransformClass *bclass;
2530 gboolean result = TRUE;
2532 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2535 if (trans->priv->pad_mode == GST_ACTIVATE_NONE && bclass->start)
2536 result &= bclass->start (trans);
2538 GST_OBJECT_LOCK (trans);
2540 if (GST_PAD_CAPS (trans->sinkpad) && GST_PAD_CAPS (trans->srcpad))
2541 trans->have_same_caps =
2542 gst_caps_is_equal (GST_PAD_CAPS (trans->sinkpad),
2543 GST_PAD_CAPS (trans->srcpad)) || trans->passthrough;
2545 trans->have_same_caps = trans->passthrough;
2546 GST_DEBUG_OBJECT (trans, "have_same_caps %d", trans->have_same_caps);
2547 trans->negotiated = FALSE;
2548 trans->have_newsegment = FALSE;
2549 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
2550 trans->priv->last_stop_out = GST_CLOCK_TIME_NONE;
2551 trans->priv->proportion = 1.0;
2552 trans->priv->earliest_time = -1;
2553 trans->priv->discont = FALSE;
2554 gst_caps_replace (&trans->priv->sink_suggest, NULL);
2555 trans->priv->processed = 0;
2556 trans->priv->dropped = 0;
2557 trans->priv->force_alloc = TRUE;
2559 GST_OBJECT_UNLOCK (trans);
2561 /* We must make sure streaming has finished before resetting things
2562 * and calling the ::stop vfunc */
2563 GST_PAD_STREAM_LOCK (trans->sinkpad);
2564 GST_PAD_STREAM_UNLOCK (trans->sinkpad);
2566 trans->have_same_caps = FALSE;
2567 /* We can only reset the passthrough mode if the instance told us to
2568 handle it in configure_caps */
2569 if (bclass->passthrough_on_same_caps) {
2570 gst_base_transform_set_passthrough (trans, FALSE);
2572 gst_caps_replace (&trans->cache_caps1, NULL);
2573 gst_caps_replace (&trans->cache_caps2, NULL);
2574 gst_caps_replace (&trans->priv->sink_alloc, NULL);
2575 gst_caps_replace (&trans->priv->sink_suggest, NULL);
2577 if (trans->priv->pad_mode != GST_ACTIVATE_NONE && bclass->stop)
2578 result &= bclass->stop (trans);
2585 gst_base_transform_sink_activate_push (GstPad * pad, gboolean active)
2587 gboolean result = TRUE;
2588 GstBaseTransform *trans;
2590 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2592 result = gst_base_transform_activate (trans, active);
2595 trans->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
2597 gst_object_unref (trans);
2603 gst_base_transform_src_activate_pull (GstPad * pad, gboolean active)
2605 gboolean result = FALSE;
2606 GstBaseTransform *trans;
2608 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
2610 result = gst_pad_activate_pull (trans->sinkpad, active);
2613 result &= gst_base_transform_activate (trans, active);
2616 trans->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
2618 gst_object_unref (trans);
2624 * gst_base_transform_set_passthrough:
2625 * @trans: the #GstBaseTransform to set
2626 * @passthrough: boolean indicating passthrough mode.
2628 * Set passthrough mode for this filter by default. This is mostly
2629 * useful for filters that do not care about negotiation.
2631 * Always TRUE for filters which don't implement either a transform
2632 * or transform_ip method.
2637 gst_base_transform_set_passthrough (GstBaseTransform * trans,
2638 gboolean passthrough)
2640 GstBaseTransformClass *bclass;
2642 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2644 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2646 GST_OBJECT_LOCK (trans);
2647 if (passthrough == FALSE) {
2648 if (bclass->transform_ip || bclass->transform)
2649 trans->passthrough = FALSE;
2651 trans->passthrough = TRUE;
2654 GST_DEBUG_OBJECT (trans, "set passthrough %d", trans->passthrough);
2655 GST_OBJECT_UNLOCK (trans);
2659 * gst_base_transform_is_passthrough:
2660 * @trans: the #GstBaseTransform to query
2662 * See if @trans is configured as a passthrough transform.
2664 * Returns: TRUE is the transform is configured in passthrough mode.
2669 gst_base_transform_is_passthrough (GstBaseTransform * trans)
2673 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2675 GST_OBJECT_LOCK (trans);
2676 result = trans->passthrough;
2677 GST_OBJECT_UNLOCK (trans);
2683 * gst_base_transform_set_in_place:
2684 * @trans: the #GstBaseTransform to modify
2685 * @in_place: Boolean value indicating that we would like to operate
2686 * on in_place buffers.
2688 * Determines whether a non-writable buffer will be copied before passing
2689 * to the transform_ip function.
2691 * <listitem>Always TRUE if no transform function is implemented.</listitem>
2692 * <listitem>Always FALSE if ONLY transform function is implemented.</listitem>
2698 gst_base_transform_set_in_place (GstBaseTransform * trans, gboolean in_place)
2700 GstBaseTransformClass *bclass;
2702 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2704 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2706 GST_OBJECT_LOCK (trans);
2709 if (bclass->transform_ip) {
2710 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
2711 trans->always_in_place = TRUE;
2714 if (bclass->transform) {
2715 GST_DEBUG_OBJECT (trans, "setting in_place FALSE");
2716 trans->always_in_place = FALSE;
2720 GST_OBJECT_UNLOCK (trans);
2724 * gst_base_transform_is_in_place:
2725 * @trans: the #GstBaseTransform to query
2727 * See if @trans is configured as a in_place transform.
2729 * Returns: TRUE is the transform is configured in in_place mode.
2734 gst_base_transform_is_in_place (GstBaseTransform * trans)
2738 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2740 GST_OBJECT_LOCK (trans);
2741 result = trans->always_in_place;
2742 GST_OBJECT_UNLOCK (trans);
2748 * gst_base_transform_update_qos:
2749 * @trans: a #GstBaseTransform
2750 * @proportion: the proportion
2751 * @diff: the diff against the clock
2752 * @timestamp: the timestamp of the buffer generating the QoS expressed in
2755 * Set the QoS parameters in the transform. This function is called internally
2756 * when a QOS event is received but subclasses can provide custom information
2764 gst_base_transform_update_qos (GstBaseTransform * trans,
2765 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2768 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2770 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans,
2771 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2772 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2774 GST_OBJECT_LOCK (trans);
2775 trans->priv->proportion = proportion;
2776 trans->priv->earliest_time = timestamp + diff;
2777 GST_OBJECT_UNLOCK (trans);
2781 * gst_base_transform_set_qos_enabled:
2782 * @trans: a #GstBaseTransform
2783 * @enabled: new state
2785 * Enable or disable QoS handling in the transform.
2792 gst_base_transform_set_qos_enabled (GstBaseTransform * trans, gboolean enabled)
2794 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2796 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "enabled: %d", enabled);
2798 GST_OBJECT_LOCK (trans);
2799 trans->priv->qos_enabled = enabled;
2800 GST_OBJECT_UNLOCK (trans);
2804 * gst_base_transform_is_qos_enabled:
2805 * @trans: a #GstBaseTransform
2807 * Queries if the transform will handle QoS.
2809 * Returns: TRUE if QoS is enabled.
2816 gst_base_transform_is_qos_enabled (GstBaseTransform * trans)
2820 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2822 GST_OBJECT_LOCK (trans);
2823 result = trans->priv->qos_enabled;
2824 GST_OBJECT_UNLOCK (trans);
2830 * gst_base_transform_set_gap_aware:
2831 * @trans: a #GstBaseTransform
2832 * @gap_aware: New state
2834 * If @gap_aware is %FALSE (the default), output buffers will have the
2835 * %GST_BUFFER_FLAG_GAP flag unset.
2837 * If set to %TRUE, the element must handle output buffers with this flag set
2838 * correctly, i.e. it can assume that the buffer contains neutral data but must
2839 * unset the flag if the output is no neutral data.
2846 gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware)
2848 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2850 GST_OBJECT_LOCK (trans);
2851 trans->priv->gap_aware = gap_aware;
2852 GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware);
2853 GST_OBJECT_UNLOCK (trans);
2857 * gst_base_transform_suggest:
2858 * @trans: a #GstBaseTransform
2859 * @caps: (transfer none): caps to suggest
2860 * @size: buffer size to suggest
2862 * Instructs @trans to suggest new @caps upstream. A copy of @caps will be
2868 gst_base_transform_suggest (GstBaseTransform * trans, GstCaps * caps,
2871 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2873 GST_OBJECT_LOCK (trans->sinkpad);
2874 if (trans->priv->sink_suggest)
2875 gst_caps_unref (trans->priv->sink_suggest);
2877 caps = gst_caps_copy (caps);
2878 trans->priv->sink_suggest = caps;
2879 trans->priv->size_suggest = size;
2880 trans->priv->suggest_pending = TRUE;
2881 GST_DEBUG_OBJECT (trans, "new suggest %" GST_PTR_FORMAT, caps);
2882 GST_OBJECT_UNLOCK (trans->sinkpad);
2886 * gst_base_transform_reconfigure:
2887 * @trans: a #GstBaseTransform
2889 * Instructs @trans to renegotiate a new downstream transform on the next
2890 * buffer. This function is typically called after properties on the transform
2891 * were set that influence the output format.
2896 gst_base_transform_reconfigure (GstBaseTransform * trans)
2898 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2900 GST_OBJECT_LOCK (trans);
2901 GST_DEBUG_OBJECT (trans, "marking reconfigure");
2902 trans->priv->reconfigure = TRUE;
2903 gst_caps_replace (&trans->priv->sink_alloc, NULL);
2904 GST_OBJECT_UNLOCK (trans);