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
50 * <itemizedlist><title>Passthrough mode</title>
52 * Element has no interest in modifying the buffer. It may want to inspect it,
53 * in which case the element should have a transform_ip function. If there
54 * is no transform_ip function in passthrough mode, the buffer is pushed
58 * On the GstBaseTransformClass is the passthrough_on_same_caps variable
59 * which will automatically set/unset passthrough based on whether the
60 * element negotiates the same caps on both pads.
63 * passthrough_on_same_caps on an element that doesn't implement a
64 * transform_caps function is useful for elements that only inspect data
69 * <title>Example elements</title>
70 * <listitem>Level</listitem>
71 * <listitem>Videoscale, audioconvert, ffmpegcolorspace, audioresample in
72 * certain modes.</listitem>
77 * <title>Modifications in-place - input buffer and output buffer are the
80 * The element must implement a transform_ip function.
83 * Output buffer size must <= input buffer size
86 * If the always_in_place flag is set, non-writable buffers will be copied
87 * and passed to the transform_ip function, otherwise a new buffer will be
88 * created and the transform function called.
91 * Incoming writable buffers will be passed to the transform_ip function
92 * immediately. </para></listitem>
94 * only implementing transform_ip and not transform implies always_in_place
99 * <title>Example elements</title>
100 * <listitem>Volume</listitem>
101 * <listitem>Audioconvert in certain modes (signed/unsigned
102 * conversion)</listitem>
103 * <listitem>ffmpegcolorspace in certain modes (endianness
104 * swapping)</listitem>
109 * <title>Modifications only to the caps/metadata of a buffer</title>
111 * The element does not require writable data, but non-writable buffers
112 * should be subbuffered so that the meta-information can be replaced.
115 * Elements wishing to operate in this mode should replace the
116 * prepare_output_buffer method to create subbuffers of the input buffer
117 * and set always_in_place to TRUE
121 * <title>Example elements</title>
122 * <listitem>Capsfilter when setting caps on outgoing buffers that have
124 * <listitem>identity when it is going to re-timestamp buffers by
125 * datarate.</listitem>
129 * <itemizedlist><title>Normal mode</title>
131 * always_in_place flag is not set, or there is no transform_ip function
134 * Element will receive an input buffer and output buffer to operate on.
137 * Output buffer is allocated by calling the prepare_output_buffer function.
141 * <title>Example elements</title>
142 * <listitem>Videoscale, ffmpegcolorspace, audioconvert when doing
143 * scaling/conversions</listitem>
147 * <itemizedlist><title>Special output buffer allocations</title>
149 * Elements which need to do special allocation of their output buffers
150 * other than what gst_buffer_pad_alloc allows should implement a
151 * prepare_output_buffer method, which calls the parent implementation and
152 * passes the newly allocated buffer.
156 * <title>Example elements</title>
157 * <listitem>efence</listitem>
162 * <itemizedlist><title>Sub-class settable flags on GstBaseTransform</title>
164 * <itemizedlist><title>passthrough</title>
166 * Implies that in the current configuration, the sub-class is not
167 * interested in modifying the buffers.
170 * Elements which are always in passthrough mode whenever the same caps
171 * has been negotiated on both pads can set the class variable
172 * passthrough_on_same_caps to have this behaviour automatically.
177 * <itemizedlist><title>always_in_place</title>
179 * Determines whether a non-writable buffer will be copied before passing
180 * to the transform_ip function.
183 * Implied TRUE if no transform function is implemented.
186 * Implied FALSE if ONLY transform function is implemented.
201 #include "../../../gst/gst_private.h"
202 #include "../../../gst/gst-i18n-lib.h"
203 #include "gstbasetransform.h"
204 #include <gst/gstmarshal.h>
206 GST_DEBUG_CATEGORY_STATIC (gst_base_transform_debug);
207 #define GST_CAT_DEFAULT gst_base_transform_debug
209 /* BaseTransform signals and args */
216 #define DEFAULT_PROP_QOS FALSE
224 #define GST_BASE_TRANSFORM_GET_PRIVATE(obj) \
225 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_TRANSFORM, GstBaseTransformPrivate))
227 struct _GstBaseTransformPrivate
229 /* QoS *//* with LOCK */
230 gboolean qos_enabled;
232 GstClockTime earliest_time;
233 /* previous buffer had a discont */
236 GstActivateMode pad_mode;
240 /* caps used for allocating buffers */
241 gboolean proxy_alloc;
244 /* upstream caps and size suggestions */
245 GstCaps *sink_suggest;
249 static GstElementClass *parent_class = NULL;
251 static void gst_base_transform_class_init (GstBaseTransformClass * klass);
252 static void gst_base_transform_init (GstBaseTransform * trans,
253 GstBaseTransformClass * klass);
254 static GstFlowReturn gst_base_transform_prepare_output_buffer (GstBaseTransform
255 * trans, GstBuffer * input, GstBuffer ** buf);
258 gst_base_transform_get_type (void)
260 static GType base_transform_type = 0;
262 if (!base_transform_type) {
263 static const GTypeInfo base_transform_info = {
264 sizeof (GstBaseTransformClass),
267 (GClassInitFunc) gst_base_transform_class_init,
270 sizeof (GstBaseTransform),
272 (GInstanceInitFunc) gst_base_transform_init,
275 base_transform_type = g_type_register_static (GST_TYPE_ELEMENT,
276 "GstBaseTransform", &base_transform_info, G_TYPE_FLAG_ABSTRACT);
278 return base_transform_type;
281 static void gst_base_transform_finalize (GObject * object);
282 static void gst_base_transform_set_property (GObject * object, guint prop_id,
283 const GValue * value, GParamSpec * pspec);
284 static void gst_base_transform_get_property (GObject * object, guint prop_id,
285 GValue * value, GParamSpec * pspec);
286 static gboolean gst_base_transform_src_activate_pull (GstPad * pad,
288 static gboolean gst_base_transform_sink_activate_push (GstPad * pad,
290 static gboolean gst_base_transform_activate (GstBaseTransform * trans,
292 static gboolean gst_base_transform_get_unit_size (GstBaseTransform * trans,
293 GstCaps * caps, guint * size);
295 static gboolean gst_base_transform_src_event (GstPad * pad, GstEvent * event);
296 static gboolean gst_base_transform_src_eventfunc (GstBaseTransform * trans,
298 static gboolean gst_base_transform_sink_event (GstPad * pad, GstEvent * event);
299 static gboolean gst_base_transform_sink_eventfunc (GstBaseTransform * trans,
301 static gboolean gst_base_transform_check_get_range (GstPad * pad);
302 static GstFlowReturn gst_base_transform_getrange (GstPad * pad, guint64 offset,
303 guint length, GstBuffer ** buffer);
304 static GstFlowReturn gst_base_transform_chain (GstPad * pad,
306 static GstCaps *gst_base_transform_getcaps (GstPad * pad);
307 static gboolean gst_base_transform_setcaps (GstPad * pad, GstCaps * caps);
308 static GstFlowReturn gst_base_transform_buffer_alloc (GstPad * pad,
309 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
311 /* static guint gst_base_transform_signals[LAST_SIGNAL] = { 0 }; */
314 gst_base_transform_finalize (GObject * object)
316 GstBaseTransform *trans;
318 trans = GST_BASE_TRANSFORM (object);
320 gst_caps_replace (&trans->priv->sink_suggest, NULL);
321 g_mutex_free (trans->transform_lock);
323 G_OBJECT_CLASS (parent_class)->finalize (object);
327 gst_base_transform_class_init (GstBaseTransformClass * klass)
329 GObjectClass *gobject_class;
331 gobject_class = G_OBJECT_CLASS (klass);
333 GST_DEBUG_CATEGORY_INIT (gst_base_transform_debug, "basetransform", 0,
334 "basetransform element");
336 GST_DEBUG ("gst_base_transform_class_init");
338 g_type_class_add_private (klass, sizeof (GstBaseTransformPrivate));
340 parent_class = g_type_class_peek_parent (klass);
342 gobject_class->set_property =
343 GST_DEBUG_FUNCPTR (gst_base_transform_set_property);
344 gobject_class->get_property =
345 GST_DEBUG_FUNCPTR (gst_base_transform_get_property);
347 g_object_class_install_property (gobject_class, PROP_QOS,
348 g_param_spec_boolean ("qos", "QoS", "Handle Quality-of-Service events",
349 DEFAULT_PROP_QOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
351 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_transform_finalize);
353 klass->passthrough_on_same_caps = FALSE;
354 klass->event = GST_DEBUG_FUNCPTR (gst_base_transform_sink_eventfunc);
355 klass->src_event = GST_DEBUG_FUNCPTR (gst_base_transform_src_eventfunc);
359 gst_base_transform_init (GstBaseTransform * trans,
360 GstBaseTransformClass * bclass)
362 GstPadTemplate *pad_template;
364 GST_DEBUG ("gst_base_transform_init");
366 trans->priv = GST_BASE_TRANSFORM_GET_PRIVATE (trans);
369 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
370 g_return_if_fail (pad_template != NULL);
371 trans->sinkpad = gst_pad_new_from_template (pad_template, "sink");
372 gst_pad_set_getcaps_function (trans->sinkpad,
373 GST_DEBUG_FUNCPTR (gst_base_transform_getcaps));
374 gst_pad_set_setcaps_function (trans->sinkpad,
375 GST_DEBUG_FUNCPTR (gst_base_transform_setcaps));
376 gst_pad_set_event_function (trans->sinkpad,
377 GST_DEBUG_FUNCPTR (gst_base_transform_sink_event));
378 gst_pad_set_chain_function (trans->sinkpad,
379 GST_DEBUG_FUNCPTR (gst_base_transform_chain));
380 gst_pad_set_activatepush_function (trans->sinkpad,
381 GST_DEBUG_FUNCPTR (gst_base_transform_sink_activate_push));
382 gst_pad_set_bufferalloc_function (trans->sinkpad,
383 GST_DEBUG_FUNCPTR (gst_base_transform_buffer_alloc));
384 gst_element_add_pad (GST_ELEMENT (trans), trans->sinkpad);
387 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
388 g_return_if_fail (pad_template != NULL);
389 trans->srcpad = gst_pad_new_from_template (pad_template, "src");
390 gst_pad_set_getcaps_function (trans->srcpad,
391 GST_DEBUG_FUNCPTR (gst_base_transform_getcaps));
392 gst_pad_set_event_function (trans->srcpad,
393 GST_DEBUG_FUNCPTR (gst_base_transform_src_event));
394 gst_pad_set_checkgetrange_function (trans->srcpad,
395 GST_DEBUG_FUNCPTR (gst_base_transform_check_get_range));
396 gst_pad_set_getrange_function (trans->srcpad,
397 GST_DEBUG_FUNCPTR (gst_base_transform_getrange));
398 gst_pad_set_activatepull_function (trans->srcpad,
399 GST_DEBUG_FUNCPTR (gst_base_transform_src_activate_pull));
400 gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
402 trans->transform_lock = g_mutex_new ();
403 trans->pending_configure = FALSE;
404 trans->priv->qos_enabled = DEFAULT_PROP_QOS;
405 trans->cache_caps1 = NULL;
406 trans->cache_caps2 = NULL;
407 trans->priv->pad_mode = GST_ACTIVATE_NONE;
408 trans->priv->gap_aware = FALSE;
410 trans->passthrough = FALSE;
411 if (bclass->transform == NULL) {
412 /* If no transform function, always_in_place is TRUE */
413 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
414 trans->always_in_place = TRUE;
416 if (bclass->transform_ip == NULL) {
417 GST_DEBUG_OBJECT (trans, "setting passthrough TRUE");
418 trans->passthrough = TRUE;
423 /* given @caps on the src or sink pad (given by @direction)
424 * calculate the possible caps on the other pad.
426 * Returns new caps, unref after usage.
429 gst_base_transform_transform_caps (GstBaseTransform * trans,
430 GstPadDirection direction, GstCaps * caps)
433 GstBaseTransformClass *klass;
438 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
440 /* if there is a custom transform function, use this */
441 if (klass->transform_caps) {
445 /* start with empty caps */
446 ret = gst_caps_new_empty ();
447 GST_DEBUG_OBJECT (trans, "transform caps (direction = %d)", direction);
449 if (gst_caps_is_any (caps)) {
450 /* for any caps we still have to call the transform function */
451 GST_DEBUG_OBJECT (trans, "from: ANY");
452 temp = klass->transform_caps (trans, direction, caps);
453 GST_DEBUG_OBJECT (trans, " to: %" GST_PTR_FORMAT, temp);
455 temp = gst_caps_make_writable (temp);
456 gst_caps_append (ret, temp);
458 /* we send caps with just one structure to the transform
459 * function as this is easier for the element */
460 for (i = 0; i < gst_caps_get_size (caps); i++) {
463 nth = gst_caps_copy_nth (caps, i);
464 GST_LOG_OBJECT (trans, "from[%d]: %" GST_PTR_FORMAT, i, nth);
465 temp = klass->transform_caps (trans, direction, nth);
466 gst_caps_unref (nth);
467 GST_LOG_OBJECT (trans, " to[%d]: %" GST_PTR_FORMAT, i, temp);
469 temp = gst_caps_make_writable (temp);
470 /* here we need to only append those structures, that are not yet
471 * in there, we use the merge function for this */
472 gst_caps_merge (ret, temp);
474 GST_LOG_OBJECT (trans, " merged[%d]: %" GST_PTR_FORMAT, i, ret);
476 GST_LOG_OBJECT (trans, "merged: (%d)", gst_caps_get_size (ret));
477 /* we can't do much simplification here because we don't really want to
478 * change the caps order */
481 GST_DEBUG_OBJECT (trans, "identity from: %" GST_PTR_FORMAT, caps);
482 /* no transform function, use the identity transform */
483 ret = gst_caps_ref (caps);
486 GST_DEBUG_OBJECT (trans, "to: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (ret),
492 /* transform a buffer of @size with @caps on the pad with @direction to
493 * the size of a buffer with @othercaps and store the result in @othersize
495 * We have two ways of doing this:
496 * 1) use a custom transform size function, this is for complicated custom
497 * cases with no fixed unit_size.
498 * 2) use the unit_size functions where there is a relationship between the
499 * caps and the size of a buffer.
502 gst_base_transform_transform_size (GstBaseTransform * trans,
503 GstPadDirection direction, GstCaps * caps,
504 guint size, GstCaps * othercaps, guint * othersize)
506 guint inunitsize, outunitsize, units;
507 GstBaseTransformClass *klass;
510 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
512 GST_DEBUG_OBJECT (trans, "asked to transform size %d for caps %"
513 GST_PTR_FORMAT " to size for caps %" GST_PTR_FORMAT " in direction %s",
514 size, caps, othercaps, direction == GST_PAD_SRC ? "SRC" : "SINK");
516 if (klass->transform_size) {
517 /* if there is a custom transform function, use this */
518 ret = klass->transform_size (trans, direction, caps, size, othercaps,
521 /* there is no transform_size function, we have to use the unit_size
522 * functions. This method assumes there is a fixed unit_size associated with
523 * each caps. We provide the same amount of units on both sides. */
524 if (!gst_base_transform_get_unit_size (trans, caps, &inunitsize))
527 GST_DEBUG_OBJECT (trans, "input size %d, input unit size %d", size,
530 /* input size must be a multiple of the unit_size of the input caps */
531 if (inunitsize == 0 || (size % inunitsize != 0))
534 /* get the amount of units */
535 units = size / inunitsize;
537 /* now get the unit size of the output */
538 if (!gst_base_transform_get_unit_size (trans, othercaps, &outunitsize))
541 /* the output size is the unit_size times the amount of units on the
543 *othersize = units * outunitsize;
544 GST_DEBUG_OBJECT (trans, "transformed size to %d", *othersize);
553 GST_DEBUG_OBJECT (trans, "could not get in_size");
554 g_warning ("%s: could not get in_size", GST_ELEMENT_NAME (trans));
559 GST_DEBUG_OBJECT (trans, "Size %u is not a multiple of unit size %u", size,
561 g_warning ("%s: size %u is not a multiple of unit size %u",
562 GST_ELEMENT_NAME (trans), size, inunitsize);
567 GST_DEBUG_OBJECT (trans, "could not get out_size");
568 g_warning ("%s: could not get out_size", GST_ELEMENT_NAME (trans));
573 /* get the caps that can be handled by @pad. We perforn:
575 * - take the caps of peer of otherpad,
576 * - filter against the padtemplate of otherpad,
577 * - calculate all transforms of remaining caps
578 * - filter against template of @pad
580 * If there is no peer, we simply return the caps of the padtemplate of pad.
583 gst_base_transform_getcaps (GstPad * pad)
585 GstBaseTransform *trans;
589 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
591 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
593 /* we can do what the peer can */
594 caps = gst_pad_peer_get_caps (otherpad);
597 const GstCaps *templ;
599 GST_DEBUG_OBJECT (pad, "peer caps %" GST_PTR_FORMAT, caps);
601 /* filtered against our padtemplate */
602 templ = gst_pad_get_pad_template_caps (otherpad);
603 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
604 temp = gst_caps_intersect (caps, templ);
605 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
606 gst_caps_unref (caps);
607 /* then see what we can transform this to */
608 caps = gst_base_transform_transform_caps (trans,
609 GST_PAD_DIRECTION (otherpad), temp);
610 GST_DEBUG_OBJECT (pad, "transformed %" GST_PTR_FORMAT, caps);
611 gst_caps_unref (temp);
615 /* and filter against the template again */
616 templ = gst_pad_get_pad_template_caps (pad);
617 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
618 temp = gst_caps_intersect (caps, templ);
619 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
620 gst_caps_unref (caps);
621 /* this is what we can do */
624 /* no peer, our padtemplate is enough then */
625 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
629 GST_DEBUG_OBJECT (trans, "returning %" GST_PTR_FORMAT, caps);
631 gst_object_unref (trans);
636 /* function triggered when the in and out caps are negotiated and need
637 * to be configured in the subclass. */
639 gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
643 GstBaseTransformClass *klass;
645 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
647 GST_DEBUG_OBJECT (trans, "in caps: %" GST_PTR_FORMAT, in);
648 GST_DEBUG_OBJECT (trans, "out caps: %" GST_PTR_FORMAT, out);
650 /* clear the cache */
651 gst_caps_replace (&trans->cache_caps1, NULL);
652 gst_caps_replace (&trans->cache_caps2, NULL);
654 /* figure out same caps state */
655 trans->have_same_caps = gst_caps_is_equal (in, out);
656 GST_DEBUG_OBJECT (trans, "have_same_caps: %d", trans->have_same_caps);
658 /* If we've a transform_ip method and same input/output caps, set in_place
659 * by default. If for some reason the sub-class prefers using a transform
660 * function, it can clear the in place flag in the set_caps */
661 gst_base_transform_set_in_place (trans,
662 klass->transform_ip && trans->have_same_caps);
664 /* Set the passthrough if the class wants passthrough_on_same_caps
665 * and we have the same caps on each pad */
666 if (klass->passthrough_on_same_caps)
667 gst_base_transform_set_passthrough (trans, trans->have_same_caps);
669 /* now configure the element with the caps */
670 if (klass->set_caps) {
671 GST_DEBUG_OBJECT (trans, "Calling set_caps method to setup caps");
672 ret = klass->set_caps (trans, in, out);
675 trans->negotiated = ret;
680 /* check if caps @in on @pad can be transformed to @out on the other pad.
681 * We don't have a vmethod to test this yet so we have to do a somewhat less
682 * efficient check for this.
685 gst_base_transform_can_transform (GstBaseTransform * trans, GstPad * pad,
686 GstCaps * in, GstCaps * out)
690 /* convert the in caps to all possible out caps */
692 gst_base_transform_transform_caps (trans, GST_PAD_DIRECTION (pad), in);
694 /* check if transform is empty */
695 if (!othercaps || gst_caps_is_empty (othercaps))
698 /* check if the out caps is a subset of the othercaps */
699 if (!gst_caps_is_subset (out, othercaps))
703 gst_caps_unref (othercaps);
705 GST_DEBUG_OBJECT (trans, "from %" GST_PTR_FORMAT, in);
706 GST_DEBUG_OBJECT (trans, "to %" GST_PTR_FORMAT, out);
713 GST_DEBUG_OBJECT (trans,
714 "transform returned useless %" GST_PTR_FORMAT, othercaps);
716 gst_caps_unref (othercaps);
721 GST_DEBUG_OBJECT (trans, "no subset");
723 gst_caps_unref (othercaps);
728 /* given a fixed @caps on @pad, create the best possible caps for the
730 * @caps must be fixed when calling this function.
732 * This function calls the transform caps vmethod of the basetransform to figure
733 * out the possible target formats. It then tries to select the best format from
736 * - attempt passthrough if the target caps is a superset of the input caps
737 * - fixating by using peer caps
738 * - fixating with transform fixate function
739 * - fixating with pad fixate functions.
741 * this function returns a caps that can be transformed into and is accepted by
745 gst_base_transform_find_transform (GstBaseTransform * trans, GstPad * pad,
748 GstBaseTransformClass *klass;
749 GstPad *otherpad, *otherpeer;
751 gboolean peer_checked = FALSE;
753 /* caps must be fixed here, this is a programming error if it's not */
754 g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
756 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
758 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
759 otherpeer = gst_pad_get_peer (otherpad);
761 /* see how we can transform the input caps. We need to do this even for
762 * passthrough because it might be possible that this element cannot support
763 * passthrough at all. */
764 othercaps = gst_base_transform_transform_caps (trans,
765 GST_PAD_DIRECTION (pad), caps);
767 /* The caps we can actually output is the intersection of the transformed
768 * caps with the pad template for the pad */
771 const GstCaps *templ_caps;
773 templ_caps = gst_pad_get_pad_template_caps (otherpad);
774 GST_DEBUG_OBJECT (trans,
775 "intersecting against padtemplate %" GST_PTR_FORMAT, templ_caps);
777 intersect = gst_caps_intersect (othercaps, templ_caps);
779 gst_caps_unref (othercaps);
780 othercaps = intersect;
783 /* check if transform is empty */
784 if (!othercaps || gst_caps_is_empty (othercaps))
787 /* if the othercaps are not fixed, we need to fixate them, first attempt
788 * is by attempting passthrough if the othercaps are a superset of caps. */
789 /* FIXME. maybe the caps is not fixed because it has multiple structures of
791 if (!gst_caps_is_fixed (othercaps)) {
794 GST_DEBUG_OBJECT (trans,
795 "transform returned non fixed %" GST_PTR_FORMAT, othercaps);
797 /* see if the target caps are a superset of the source caps, in this
798 * case we can try to perform passthrough */
799 temp = gst_caps_intersect (othercaps, caps);
800 GST_DEBUG_OBJECT (trans, "intersect returned %" GST_PTR_FORMAT, temp);
802 if (!gst_caps_is_empty (temp)) {
803 GST_DEBUG_OBJECT (trans, "try passthrough with %" GST_PTR_FORMAT, caps);
805 /* try passthrough. we know it's fixed, because caps is fixed */
806 if (gst_pad_accept_caps (otherpeer, caps)) {
807 GST_DEBUG_OBJECT (trans, "peer accepted %" GST_PTR_FORMAT, caps);
808 /* peer accepted unmodified caps, we free the original non-fixed
809 * caps and work with the passthrough caps */
810 gst_caps_unref (othercaps);
811 othercaps = gst_caps_ref (caps);
812 /* mark that we checked othercaps with the peer, this
813 * makes sure we don't call accept_caps again with these same
817 GST_DEBUG_OBJECT (trans,
818 "peer did not accept %" GST_PTR_FORMAT, caps);
821 GST_DEBUG_OBJECT (trans, "no peer, doing passthrough");
822 gst_caps_unref (othercaps);
823 othercaps = gst_caps_ref (caps);
826 gst_caps_unref (temp);
830 /* second attempt at fixation is done by intersecting with
832 if (!gst_caps_is_fixed (othercaps) && otherpeer) {
833 /* intersect against what the peer can do */
837 GST_DEBUG_OBJECT (trans, "othercaps now %" GST_PTR_FORMAT, othercaps);
839 peercaps = gst_pad_get_caps (otherpeer);
840 intersect = gst_caps_intersect (peercaps, othercaps);
841 gst_caps_unref (peercaps);
842 gst_caps_unref (othercaps);
843 othercaps = intersect;
844 peer_checked = FALSE;
846 GST_DEBUG_OBJECT (trans,
847 "filtering against peer yields %" GST_PTR_FORMAT, othercaps);
850 if (gst_caps_is_empty (othercaps))
851 goto no_transform_possible;
853 /* third attempt at fixation, call the fixate vmethod and
854 * ultimately call the pad fixate function. */
855 if (!gst_caps_is_fixed (othercaps)) {
858 GST_DEBUG_OBJECT (trans,
859 "trying to fixate %" GST_PTR_FORMAT " on pad %s:%s",
860 othercaps, GST_DEBUG_PAD_NAME (otherpad));
862 /* since we have no other way to fixate left, we might as well just take
863 * the first of the caps list and fixate that */
865 /* FIXME: when fixating using the vmethod, it might make sense to fixate
866 * each of the caps; but Wim doesn't see a use case for that yet */
867 temp = gst_caps_copy_nth (othercaps, 0);
868 gst_caps_unref (othercaps);
870 peer_checked = FALSE;
872 if (klass->fixate_caps) {
873 GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
874 " using caps %" GST_PTR_FORMAT
875 " on pad %s:%s using fixate_caps vmethod", othercaps, caps,
876 GST_DEBUG_PAD_NAME (otherpad));
877 klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
879 /* if still not fixed, no other option but to let the default pad fixate
880 * function do its job */
881 if (!gst_caps_is_fixed (othercaps)) {
882 GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
883 " on pad %s:%s using gst_pad_fixate_caps", othercaps,
884 GST_DEBUG_PAD_NAME (otherpad));
885 gst_pad_fixate_caps (otherpad, othercaps);
887 GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
890 /* caps should be fixed now, if not we have to fail. */
891 if (!gst_caps_is_fixed (othercaps))
892 goto could_not_fixate;
894 /* and peer should accept, don't check again if we already checked the
895 * othercaps against the peer. */
896 if (!peer_checked && otherpeer && !gst_pad_accept_caps (otherpeer, othercaps))
899 GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT
900 ", and got final caps %" GST_PTR_FORMAT, caps, othercaps);
903 gst_object_unref (otherpeer);
910 GST_DEBUG_OBJECT (trans,
911 "transform returned useless %" GST_PTR_FORMAT, othercaps);
914 no_transform_possible:
916 GST_DEBUG_OBJECT (trans,
917 "transform could not transform %" GST_PTR_FORMAT
918 " in anything we support", caps);
923 GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
928 GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
929 " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
935 gst_object_unref (otherpeer);
937 gst_caps_unref (othercaps);
942 /* called when new caps arrive on the sink or source pad,
943 * We try to find the best caps for the other side using our _find_transform()
944 * function. If there are caps, we configure the transform for this new
947 * FIXME, this function is currently commutative but this should not really be
948 * because we never set caps starting from the srcpad.
951 gst_base_transform_setcaps (GstPad * pad, GstCaps * caps)
953 GstBaseTransform *trans;
954 GstBaseTransformClass *klass;
955 GstPad *otherpad, *otherpeer;
956 GstCaps *othercaps = NULL;
958 GstCaps *incaps, *outcaps;
960 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
961 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
963 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
964 otherpeer = gst_pad_get_peer (otherpad);
966 /* if we get called recursively, we bail out now to avoid an
968 if (GST_PAD_IS_IN_SETCAPS (otherpad))
971 GST_DEBUG_OBJECT (pad, "have new caps %" GST_PTR_FORMAT, caps);
973 /* find best possible caps for the other pad */
974 othercaps = gst_base_transform_find_transform (trans, pad, caps);
975 if (!othercaps || gst_caps_is_empty (othercaps))
976 goto no_transform_possible;
978 /* configure the element now */
979 /* make sure in and out caps are correct */
980 if (pad == trans->sinkpad) {
989 /* if we have the same caps, we can optimize and reuse the input caps */
990 if (gst_caps_is_equal (incaps, outcaps)) {
991 GST_INFO_OBJECT (trans, "reuse caps");
992 gst_caps_unref (outcaps);
993 outcaps = gst_caps_ref (incaps);
997 /* call configure now */
998 if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
999 goto failed_configure;
1001 /* we know this will work, we implement the setcaps */
1002 gst_pad_set_caps (otherpad, othercaps);
1004 if (pad == trans->srcpad && trans->priv->pad_mode == GST_ACTIVATE_PULL) {
1006 ret &= gst_pad_set_caps (otherpeer, othercaps);
1008 GST_INFO_OBJECT (trans, "otherpeer setcaps(%" GST_PTR_FORMAT ") failed",
1015 gst_object_unref (otherpeer);
1017 gst_caps_unref (othercaps);
1019 trans->negotiated = ret;
1021 gst_object_unref (trans);
1026 no_transform_possible:
1028 GST_WARNING_OBJECT (trans,
1029 "transform could not transform %" GST_PTR_FORMAT
1030 " in anything we support", caps);
1036 GST_WARNING_OBJECT (trans, "FAILED to configure caps %" GST_PTR_FORMAT
1037 " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1043 /* Allocate a buffer using gst_pad_alloc_buffer
1045 * This function can do renegotiation on the source pad
1047 * The output buffer is always writable. outbuf can be equal to
1048 * inbuf, the caller should be prepared for this and perform
1049 * appropriate refcounting.
1051 static GstFlowReturn
1052 gst_base_transform_prepare_output_buffer (GstBaseTransform * trans,
1053 GstBuffer * in_buf, GstBuffer ** out_buf)
1055 GstBaseTransformClass *bclass;
1056 GstBaseTransformPrivate *priv;
1057 GstFlowReturn ret = GST_FLOW_OK;
1058 guint outsize, newsize;
1060 GstCaps *incaps, *oldcaps, *newcaps;
1062 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1066 /* figure out how to allocate a buffer based on the current configuration */
1067 if (trans->passthrough) {
1068 GST_DEBUG_OBJECT (trans, "doing passthrough alloc");
1069 /* passthrough, we don't really need to call pad alloc but we still need to
1070 * in order to get upstream negotiation. The output size is the same as the
1072 outsize = GST_BUFFER_SIZE (in_buf);
1073 /* we always alloc and discard here */
1076 gboolean want_in_place = (bclass->transform_ip != NULL)
1077 && trans->always_in_place;
1079 if (want_in_place) {
1080 GST_DEBUG_OBJECT (trans, "doing inplace alloc");
1081 /* we alloc a buffer of the same size as the input */
1082 outsize = GST_BUFFER_SIZE (in_buf);
1083 /* only discard it when the input was not writable, otherwise, we reuse
1084 * the input buffer. */
1085 discard = gst_buffer_is_writable (in_buf);
1087 GST_DEBUG_OBJECT (trans, "getting output size for copy transform");
1088 /* copy transform, figure out the output size */
1089 if (!gst_base_transform_transform_size (trans,
1090 GST_PAD_SINK, GST_PAD_CAPS (trans->sinkpad),
1091 GST_BUFFER_SIZE (in_buf), GST_PAD_CAPS (trans->srcpad),
1095 /* never discard this buffer, we need it for storing the output */
1100 oldcaps = GST_PAD_CAPS (trans->srcpad);
1102 if (bclass->prepare_output_buffer) {
1103 GST_DEBUG_OBJECT (trans,
1104 "calling prepare buffer with caps %" GST_PTR_FORMAT, oldcaps);
1106 bclass->prepare_output_buffer (trans, in_buf, outsize, oldcaps,
1110 * decrease refcount again if vmethod returned refcounted in_buf. This
1111 * is because we need to make sure that the buffer is writable for the
1112 * in_place transform. The docs of the vmethod say that you should return
1113 * a reffed inbuf, which is exactly what we don't want :), oh well.. */
1114 if (in_buf == *out_buf)
1115 gst_buffer_unref (in_buf);
1117 /* never discard the buffer from the prepare_buffer method */
1120 GST_DEBUG_OBJECT (trans, "doing alloc with caps %" GST_PTR_FORMAT, oldcaps);
1122 ret = gst_pad_alloc_buffer (trans->srcpad,
1123 GST_BUFFER_OFFSET (in_buf), outsize, oldcaps, out_buf);
1126 if (ret != GST_FLOW_OK)
1129 /* must always return a buffer */
1130 g_assert (*out_buf != NULL);
1132 /* check if we got different caps on this new output buffer */
1133 newcaps = GST_BUFFER_CAPS (*out_buf);
1134 newsize = GST_BUFFER_SIZE (*out_buf);
1135 if (!gst_caps_is_equal (newcaps, oldcaps)) {
1137 gboolean can_convert;
1139 GST_DEBUG_OBJECT (trans, "received new caps %" GST_PTR_FORMAT, newcaps);
1141 incaps = GST_PAD_CAPS (trans->sinkpad);
1143 /* it's possible that the buffer we got is of the wrong size, get the
1144 * expected size here */
1145 gst_base_transform_transform_size (trans,
1146 GST_PAD_SINK, incaps, GST_BUFFER_SIZE (in_buf), newcaps, &outsize);
1148 /* check if we can convert the current incaps to the new target caps */
1150 gst_base_transform_can_transform (trans, trans->sinkpad, incaps,
1154 GST_DEBUG_OBJECT (trans, "reconfigure transform for current buffer");
1155 /* caps not empty, try to renegotiate to the new format */
1156 if (!gst_base_transform_configure_caps (trans, incaps, newcaps)) {
1157 /* not sure we need to fail hard here, we can simply continue our
1158 * conversion with what we negotiated before */
1159 goto failed_configure;
1161 /* new format configure, and use the new output buffer */
1162 gst_pad_set_caps (trans->srcpad, newcaps);
1165 GST_DEBUG_OBJECT (trans, "cannot perform transform on current buffer");
1167 /* we cannot convert the current buffer but we might be able to suggest a
1168 * new format upstream, try to find what the best format is. */
1170 gst_base_transform_find_transform (trans, trans->srcpad, newcaps);
1173 GST_DEBUG_OBJECT (trans, "incompatible caps, ignoring");
1174 /* we received caps that we cannot transform. Upstream is behaving badly
1175 * because it should have checked if we could handle these caps. We can
1176 * simply ignore these caps and produce a buffer with our original caps. */
1180 GST_DEBUG_OBJECT (trans, "getting size of suggestion");
1182 /* not a subset, we have a new upstream suggestion, remember it and
1183 * allocate a default buffer. First we try to convert the size */
1184 if (gst_base_transform_transform_size (trans,
1185 GST_PAD_SRC, newcaps, outsize, othercaps, &size_suggest)) {
1187 /* ok, remember the suggestions now */
1188 GST_DEBUG_OBJECT (trans,
1189 "storing new caps and size suggestion of %u and %" GST_PTR_FORMAT,
1190 size_suggest, othercaps);
1192 GST_OBJECT_LOCK (trans->sinkpad);
1193 if (priv->sink_suggest)
1194 gst_caps_unref (priv->sink_suggest);
1195 priv->sink_suggest = gst_caps_ref (othercaps);
1196 priv->size_suggest = size_suggest;
1197 GST_OBJECT_UNLOCK (trans->sinkpad);
1199 gst_caps_unref (othercaps);
1201 if (in_buf != *out_buf)
1202 gst_buffer_unref (*out_buf);
1206 /* if we got a buffer of the wrong size, discard it now */
1207 if (newsize != outsize) {
1208 if (in_buf != *out_buf)
1209 gst_buffer_unref (*out_buf);
1215 if (*out_buf == NULL) {
1217 GST_DEBUG_OBJECT (trans, "make default output buffer");
1218 /* no valid buffer yet, make one */
1219 *out_buf = gst_buffer_new_and_alloc (outsize);
1220 gst_buffer_copy_metadata (*out_buf, in_buf,
1221 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
1223 GST_DEBUG_OBJECT (trans, "reuse input buffer");
1228 GST_DEBUG_OBJECT (trans, "discard buffer, reuse input buffer");
1229 gst_buffer_unref (*out_buf);
1232 GST_DEBUG_OBJECT (trans, "using pad-alloc buffer");
1233 gst_buffer_copy_metadata (*out_buf, in_buf,
1234 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
1237 gst_buffer_set_caps (*out_buf, GST_PAD_CAPS (trans->srcpad));
1239 /* clear the GAP flag when the subclass does not understand it */
1240 if (!trans->priv->gap_aware)
1241 GST_BUFFER_FLAG_UNSET (*out_buf, GST_BUFFER_FLAG_GAP);
1248 GST_WARNING_OBJECT (trans, "pad-alloc failed: %s", gst_flow_get_name (ret));
1253 GST_ERROR_OBJECT (trans, "unknown output size");
1254 return GST_FLOW_ERROR;
1258 GST_WARNING_OBJECT (trans, "failed to configure caps");
1259 return GST_FLOW_NOT_NEGOTIATED;
1263 /* Given @caps calcultate the size of one unit.
1265 * For video caps, this is the size of one frame (and thus one buffer).
1266 * For audio caps, this is the size of one sample.
1268 * These values are cached since they do not change and the calculation
1269 * potentially involves parsing caps and other expensive stuff.
1271 * We have two cache locations to store the size, one for the source caps
1272 * and one for the sink caps.
1274 * this function returns FALSE if no size could be calculated.
1277 gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
1280 gboolean res = FALSE;
1281 GstBaseTransformClass *bclass;
1283 /* see if we have the result cached */
1284 if (trans->cache_caps1 == caps) {
1285 *size = trans->cache_caps1_size;
1286 GST_DEBUG_OBJECT (trans, "returned %d from first cache", *size);
1289 if (trans->cache_caps2 == caps) {
1290 *size = trans->cache_caps2_size;
1291 GST_DEBUG_OBJECT (trans, "returned %d from second cached", *size);
1295 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1296 if (bclass->get_unit_size) {
1297 res = bclass->get_unit_size (trans, caps, size);
1298 GST_DEBUG_OBJECT (trans, "caps %" GST_PTR_FORMAT
1299 ") has unit size %d, result %s", caps, *size, res ? "TRUE" : "FALSE");
1302 /* and cache the values */
1303 if (trans->cache_caps1 == NULL) {
1304 gst_caps_replace (&trans->cache_caps1, caps);
1305 trans->cache_caps1_size = *size;
1306 GST_DEBUG_OBJECT (trans, "caching %d in first cache", *size);
1307 } else if (trans->cache_caps2 == NULL) {
1308 gst_caps_replace (&trans->cache_caps2, caps);
1309 trans->cache_caps2_size = *size;
1310 GST_DEBUG_OBJECT (trans, "caching %d in second cache", *size);
1312 GST_DEBUG_OBJECT (trans, "no free spot to cache unit_size");
1316 GST_DEBUG_OBJECT (trans, "Sub-class does not implement get_unit_size");
1321 /* your upstream peer wants to send you a buffer
1322 * that buffer has the given offset, size and caps
1323 * you're requested to allocate a buffer
1325 static GstFlowReturn
1326 gst_base_transform_buffer_alloc (GstPad * pad, guint64 offset, guint size,
1327 GstCaps * caps, GstBuffer ** buf)
1329 GstBaseTransform *trans;
1330 GstBaseTransformPrivate *priv;
1332 gboolean proxy = FALSE, suggest = FALSE;
1333 GstCaps *sink_suggest;
1334 guint size_suggest = 0;
1336 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1339 GST_DEBUG_OBJECT (pad, "alloc with caps %" GST_PTR_FORMAT ", size %u", caps,
1344 /* we remember our previous alloc request to quickly see if we can proxy or
1346 if (caps && gst_caps_is_equal (priv->sink_alloc, caps)) {
1347 /* we have seen this before, see if we need to proxy */
1348 GST_DEBUG_OBJECT (trans, "have old caps");
1349 sink_suggest = caps;
1353 GstCaps *temp, *othercaps;
1354 const GstCaps *templ;
1357 GST_DEBUG_OBJECT (trans, "new format %" GST_PTR_FORMAT, caps);
1359 /* if we have a suggestion, pretend we got these as input */
1360 GST_OBJECT_LOCK (pad);
1361 if ((priv->sink_suggest && !gst_caps_is_equal (caps, priv->sink_suggest))) {
1362 sink_suggest = gst_caps_ref (priv->sink_suggest);
1363 size_suggest = priv->size_suggest;
1364 GST_DEBUG_OBJECT (trans, "have suggestion %" GST_PTR_FORMAT,
1368 GST_DEBUG_OBJECT (trans, "using caps %" GST_PTR_FORMAT, caps);
1369 sink_suggest = caps;
1372 GST_OBJECT_UNLOCK (pad);
1374 /* check if we actually handle this format on the sinkpad */
1376 templ = gst_pad_get_pad_template_caps (pad);
1377 temp = gst_caps_intersect (sink_suggest, templ);
1379 empty = gst_caps_is_empty (temp);
1380 gst_caps_unref (temp);
1386 /* find the best format for the other side */
1387 if (sink_suggest == NULL) {
1388 /* always proxy when the caps are NULL. When this is a new format, see if
1389 * we can proxy it downstream */
1390 GST_DEBUG_OBJECT (trans, "null caps, marking for proxy");
1391 priv->proxy_alloc = TRUE;
1393 othercaps = gst_base_transform_find_transform (trans, pad, sink_suggest);
1394 if (!othercaps || gst_caps_is_empty (othercaps)) {
1395 /* no transform, can't proxy */
1396 GST_DEBUG_OBJECT (trans, "can't find transform, disable proxy");
1397 priv->proxy_alloc = FALSE;
1399 if (gst_caps_is_equal (caps, othercaps)) {
1400 GST_DEBUG_OBJECT (trans,
1401 "best caps same as input, marking for proxy");
1402 priv->proxy_alloc = TRUE;
1404 GST_DEBUG_OBJECT (trans,
1405 "best caps different from input, disable proxy");
1406 priv->proxy_alloc = FALSE;
1410 gst_caps_unref (othercaps);
1413 /* remember the new caps */
1414 gst_caps_replace (&priv->sink_alloc, sink_suggest);
1415 proxy = priv->proxy_alloc;
1416 GST_DEBUG_OBJECT (trans, "doing default alloc, proxy %d", proxy);
1421 GST_DEBUG_OBJECT (trans, "proxy buffer-alloc with caps %" GST_PTR_FORMAT
1422 ", size %u", caps, size);
1424 res = gst_pad_alloc_buffer (trans->srcpad, offset, size, caps, buf);
1425 if (res != GST_FLOW_OK)
1428 /* check if the caps changed */
1429 newcaps = GST_BUFFER_CAPS (*buf);
1431 GST_DEBUG_OBJECT (trans, "got caps %" GST_PTR_FORMAT, newcaps);
1433 if (newcaps != caps) {
1434 GST_DEBUG_OBJECT (trans, "got new caps %" GST_PTR_FORMAT, newcaps);
1436 /* we have new caps, see if we can proxy downstream */
1437 if (gst_pad_peer_accept_caps (trans->sinkpad, newcaps)) {
1438 /* peer accepts the caps, return a buffer in this format */
1439 GST_DEBUG_OBJECT (trans, "peer accepted new caps");
1440 /* remember the format */
1441 gst_caps_replace (&priv->sink_alloc, newcaps);
1443 GST_DEBUG_OBJECT (trans, "peer did not accept new caps");
1444 /* peer does not accept the caps, create a buffer of the requested
1446 gst_buffer_unref (*buf);
1453 *buf = gst_buffer_new_and_alloc (size_suggest);
1454 GST_DEBUG_OBJECT (trans,
1455 "doing suggestion of size %u, caps %" GST_PTR_FORMAT, size_suggest,
1457 GST_BUFFER_CAPS (*buf) = sink_suggest;
1461 gst_object_unref (trans);
1468 GST_DEBUG_OBJECT (trans, "pad alloc failed: %s", gst_flow_get_name (res));
1470 gst_caps_unref (sink_suggest);
1471 gst_object_unref (trans);
1476 GST_DEBUG_OBJECT (trans, "pad alloc with unsupported caps");
1478 gst_caps_unref (sink_suggest);
1479 gst_object_unref (trans);
1480 return GST_FLOW_NOT_NEGOTIATED;
1485 gst_base_transform_sink_event (GstPad * pad, GstEvent * event)
1487 GstBaseTransform *trans;
1488 GstBaseTransformClass *bclass;
1489 gboolean ret = TRUE;
1490 gboolean forward = TRUE;
1492 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1493 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1496 forward = bclass->event (trans, event);
1498 /* FIXME, do this in the default event handler so the subclass can do
1499 * something different. */
1501 ret = gst_pad_push_event (trans->srcpad, event);
1503 gst_event_unref (event);
1505 gst_object_unref (trans);
1511 gst_base_transform_sink_eventfunc (GstBaseTransform * trans, GstEvent * event)
1513 switch (GST_EVENT_TYPE (event)) {
1514 case GST_EVENT_FLUSH_START:
1516 case GST_EVENT_FLUSH_STOP:
1517 GST_OBJECT_LOCK (trans);
1518 /* reset QoS parameters */
1519 trans->priv->proportion = 1.0;
1520 trans->priv->earliest_time = -1;
1521 trans->priv->discont = FALSE;
1522 GST_OBJECT_UNLOCK (trans);
1523 /* we need new segment info after the flush. */
1524 trans->have_newsegment = FALSE;
1525 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
1531 case GST_EVENT_NEWSEGMENT:
1534 gdouble rate, arate;
1535 gint64 start, stop, time;
1538 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1539 &start, &stop, &time);
1541 trans->have_newsegment = TRUE;
1543 gst_segment_set_newsegment_full (&trans->segment, update, rate, arate,
1544 format, start, stop, time);
1546 if (format == GST_FORMAT_TIME) {
1547 GST_DEBUG_OBJECT (trans, "received TIME NEW_SEGMENT %" GST_TIME_FORMAT
1548 " -- %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT
1549 ", accum %" GST_TIME_FORMAT,
1550 GST_TIME_ARGS (trans->segment.start),
1551 GST_TIME_ARGS (trans->segment.stop),
1552 GST_TIME_ARGS (trans->segment.time),
1553 GST_TIME_ARGS (trans->segment.accum));
1555 GST_DEBUG_OBJECT (trans, "received NEW_SEGMENT %" G_GINT64_FORMAT
1556 " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT
1557 ", accum %" G_GINT64_FORMAT,
1558 trans->segment.start, trans->segment.stop,
1559 trans->segment.time, trans->segment.accum);
1571 gst_base_transform_src_event (GstPad * pad, GstEvent * event)
1573 GstBaseTransform *trans;
1574 GstBaseTransformClass *bclass;
1575 gboolean ret = TRUE;
1577 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1578 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1580 if (bclass->src_event)
1581 ret = bclass->src_event (trans, event);
1583 gst_object_unref (trans);
1589 gst_base_transform_src_eventfunc (GstBaseTransform * trans, GstEvent * event)
1593 switch (GST_EVENT_TYPE (event)) {
1594 case GST_EVENT_SEEK:
1596 case GST_EVENT_NAVIGATION:
1601 GstClockTimeDiff diff;
1602 GstClockTime timestamp;
1604 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1605 gst_base_transform_update_qos (trans, proportion, diff, timestamp);
1612 ret = gst_pad_push_event (trans->sinkpad, event);
1617 /* perform a transform on @inbuf and put the result in @outbuf.
1619 * This function is common to the push and pull-based operations.
1621 * This function takes ownership of @inbuf */
1622 static GstFlowReturn
1623 gst_base_transform_handle_buffer (GstBaseTransform * trans, GstBuffer * inbuf,
1624 GstBuffer ** outbuf)
1626 GstBaseTransformClass *bclass;
1627 GstFlowReturn ret = GST_FLOW_OK;
1628 gboolean want_in_place;
1629 GstClockTime qostime;
1631 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1633 if (GST_BUFFER_OFFSET_IS_VALID (inbuf))
1634 GST_DEBUG_OBJECT (trans, "handling buffer %p of size %d and offset %"
1635 G_GUINT64_FORMAT, inbuf, GST_BUFFER_SIZE (inbuf),
1636 GST_BUFFER_OFFSET (inbuf));
1638 GST_DEBUG_OBJECT (trans, "handling buffer %p of size %d and offset NONE",
1639 inbuf, GST_BUFFER_SIZE (inbuf));
1641 /* Don't allow buffer handling before negotiation, except in passthrough mode
1642 * or if the class doesn't implement a set_caps function (in which case it doesn't
1645 if (!trans->negotiated && !trans->passthrough && (bclass->set_caps != NULL))
1646 goto not_negotiated;
1648 /* Set discont flag so we can mark the outgoing buffer */
1649 if (GST_BUFFER_IS_DISCONT (inbuf)) {
1650 GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", inbuf);
1651 trans->priv->discont = TRUE;
1654 /* can only do QoS if the segment is in TIME */
1655 if (trans->segment.format != GST_FORMAT_TIME)
1658 /* QOS is done on the running time of the buffer, get it now */
1659 qostime = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
1660 GST_BUFFER_TIMESTAMP (inbuf));
1662 if (qostime != -1) {
1664 GstClockTime earliest_time;
1666 /* lock for getting the QoS parameters that are set (in a different thread)
1667 * with the QOS events */
1668 GST_OBJECT_LOCK (trans);
1669 earliest_time = trans->priv->earliest_time;
1670 /* check for QoS, don't perform conversion for buffers
1671 * that are known to be late. */
1672 need_skip = trans->priv->qos_enabled &&
1673 earliest_time != -1 && qostime != -1 && qostime <= earliest_time;
1674 GST_OBJECT_UNLOCK (trans);
1677 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "skipping transform: qostime %"
1678 GST_TIME_FORMAT " <= %" GST_TIME_FORMAT,
1679 GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
1680 /* mark discont for next buffer */
1681 trans->priv->discont = TRUE;
1688 /* first try to allocate an output buffer based on the currently negotiated
1689 * format. While we call pad-alloc we could renegotiate the srcpad format or
1690 * have a new suggestion for upstream buffer-alloc.
1691 * In any case, outbuf will contain a buffer suitable for doing the configured
1692 * transform after this function. */
1693 ret = gst_base_transform_prepare_output_buffer (trans, inbuf, outbuf);
1694 if (G_UNLIKELY (ret != GST_FLOW_OK))
1697 /* now perform the needed transform */
1698 if (trans->passthrough) {
1699 /* In passthrough mode, give transform_ip a look at the
1700 * buffer, without making it writable, or just push the
1702 if (bclass->transform_ip) {
1703 GST_DEBUG_OBJECT (trans, "doing passthrough transform");
1704 ret = bclass->transform_ip (trans, *outbuf);
1706 GST_DEBUG_OBJECT (trans, "element is in passthrough");
1709 want_in_place = (bclass->transform_ip != NULL) && trans->always_in_place;
1711 if (want_in_place) {
1712 GST_DEBUG_OBJECT (trans, "doing inplace transform");
1714 if (inbuf != *outbuf) {
1715 /* different buffers, copy the input to the output first, we then do an
1716 * in-place transform on the output buffer. */
1717 memcpy (GST_BUFFER_DATA (*outbuf), GST_BUFFER_DATA (inbuf),
1718 GST_BUFFER_SIZE (inbuf));
1720 ret = bclass->transform_ip (trans, *outbuf);
1722 GST_DEBUG_OBJECT (trans, "doing non-inplace transform");
1724 if (bclass->transform)
1725 ret = bclass->transform (trans, inbuf, *outbuf);
1727 ret = GST_FLOW_NOT_SUPPORTED;
1732 /* only unref input buffer if we allocated a new outbuf buffer */
1733 if (*outbuf != inbuf)
1734 gst_buffer_unref (inbuf);
1741 gst_buffer_unref (inbuf);
1742 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
1743 ("not negotiated"), ("not negotiated"));
1744 return GST_FLOW_NOT_NEGOTIATED;
1748 gst_buffer_unref (inbuf);
1749 GST_WARNING_OBJECT (trans, "could not get buffer from pool: %s",
1750 gst_flow_get_name (ret));
1756 gst_base_transform_check_get_range (GstPad * pad)
1758 GstBaseTransform *trans;
1761 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1763 ret = gst_pad_check_pull_range (trans->sinkpad);
1765 gst_object_unref (trans);
1770 /* FIXME, getrange is broken, need to pull range from the other
1771 * end based on the transform_size result.
1773 static GstFlowReturn
1774 gst_base_transform_getrange (GstPad * pad, guint64 offset,
1775 guint length, GstBuffer ** buffer)
1777 GstBaseTransform *trans;
1781 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1783 ret = gst_pad_pull_range (trans->sinkpad, offset, length, &inbuf);
1784 if (ret == GST_FLOW_OK) {
1785 ret = gst_base_transform_handle_buffer (trans, inbuf, buffer);
1788 gst_object_unref (trans);
1793 static GstFlowReturn
1794 gst_base_transform_chain (GstPad * pad, GstBuffer * buffer)
1796 GstBaseTransform *trans;
1798 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
1799 GstBuffer *outbuf = NULL;
1801 trans = GST_BASE_TRANSFORM (GST_OBJECT_PARENT (pad));
1803 /* calculate end position of the incoming buffer */
1804 if (GST_BUFFER_TIMESTAMP (buffer) != GST_CLOCK_TIME_NONE) {
1805 if (GST_BUFFER_DURATION (buffer) != GST_CLOCK_TIME_NONE)
1806 last_stop = GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1808 last_stop = GST_BUFFER_TIMESTAMP (buffer);
1811 /* protect transform method and concurrent buffer alloc */
1812 ret = gst_base_transform_handle_buffer (trans, buffer, &outbuf);
1814 /* outbuf can be NULL, this means a dropped buffer, if we have a buffer but
1815 * GST_BASE_TRANSFORM_FLOW_DROPPED we will not push either. */
1816 if (outbuf != NULL) {
1817 if ((ret == GST_FLOW_OK)) {
1818 /* Remember last stop position */
1819 if ((last_stop != GST_CLOCK_TIME_NONE) &&
1820 (trans->segment.format == GST_FORMAT_TIME))
1821 gst_segment_set_last_stop (&trans->segment, GST_FORMAT_TIME, last_stop);
1823 /* apply DISCONT flag if the buffer is not yet marked as such */
1824 if (trans->priv->discont) {
1825 if (!GST_BUFFER_IS_DISCONT (outbuf)) {
1826 outbuf = gst_buffer_make_metadata_writable (outbuf);
1827 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1829 trans->priv->discont = FALSE;
1831 ret = gst_pad_push (trans->srcpad, outbuf);
1833 gst_buffer_unref (outbuf);
1836 /* convert internal flow to OK and mark discont for the next buffer. */
1837 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED) {
1838 trans->priv->discont = TRUE;
1846 gst_base_transform_set_property (GObject * object, guint prop_id,
1847 const GValue * value, GParamSpec * pspec)
1849 GstBaseTransform *trans;
1851 trans = GST_BASE_TRANSFORM (object);
1855 gst_base_transform_set_qos_enabled (trans, g_value_get_boolean (value));
1858 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1864 gst_base_transform_get_property (GObject * object, guint prop_id,
1865 GValue * value, GParamSpec * pspec)
1867 GstBaseTransform *trans;
1869 trans = GST_BASE_TRANSFORM (object);
1873 g_value_set_boolean (value, gst_base_transform_is_qos_enabled (trans));
1876 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1881 /* not a vmethod of anything, just an internal method */
1883 gst_base_transform_activate (GstBaseTransform * trans, gboolean active)
1885 GstBaseTransformClass *bclass;
1886 gboolean result = TRUE;
1888 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1891 if (trans->priv->pad_mode == GST_ACTIVATE_NONE && bclass->start)
1892 result &= bclass->start (trans);
1894 GST_OBJECT_LOCK (trans);
1896 if (GST_PAD_CAPS (trans->sinkpad) && GST_PAD_CAPS (trans->srcpad))
1897 trans->have_same_caps =
1898 gst_caps_is_equal (GST_PAD_CAPS (trans->sinkpad),
1899 GST_PAD_CAPS (trans->srcpad)) || trans->passthrough;
1901 trans->have_same_caps = trans->passthrough;
1902 GST_DEBUG_OBJECT (trans, "have_same_caps %d", trans->have_same_caps);
1903 trans->negotiated = FALSE;
1904 trans->have_newsegment = FALSE;
1905 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
1906 trans->priv->proportion = 1.0;
1907 trans->priv->earliest_time = -1;
1908 trans->priv->discont = FALSE;
1909 gst_caps_replace (&trans->priv->sink_suggest, NULL);
1911 GST_OBJECT_UNLOCK (trans);
1913 /* We must make sure streaming has finished before resetting things
1914 * and calling the ::stop vfunc */
1915 GST_PAD_STREAM_LOCK (trans->sinkpad);
1916 GST_PAD_STREAM_UNLOCK (trans->sinkpad);
1918 trans->have_same_caps = FALSE;
1919 /* We can only reset the passthrough mode if the instance told us to
1920 handle it in configure_caps */
1921 if (bclass->passthrough_on_same_caps) {
1922 gst_base_transform_set_passthrough (trans, FALSE);
1924 gst_caps_replace (&trans->cache_caps1, NULL);
1925 gst_caps_replace (&trans->cache_caps2, NULL);
1926 gst_caps_replace (&trans->priv->sink_alloc, NULL);
1927 gst_caps_replace (&trans->priv->sink_suggest, NULL);
1929 if (trans->priv->pad_mode != GST_ACTIVATE_NONE && bclass->stop)
1930 result &= bclass->stop (trans);
1937 gst_base_transform_sink_activate_push (GstPad * pad, gboolean active)
1939 gboolean result = TRUE;
1940 GstBaseTransform *trans;
1942 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1944 result = gst_base_transform_activate (trans, active);
1947 trans->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
1949 gst_object_unref (trans);
1955 gst_base_transform_src_activate_pull (GstPad * pad, gboolean active)
1957 gboolean result = FALSE;
1958 GstBaseTransform *trans;
1960 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
1962 result = gst_pad_activate_pull (trans->sinkpad, active);
1965 result &= gst_base_transform_activate (trans, active);
1968 trans->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
1970 gst_object_unref (trans);
1976 * gst_base_transform_set_passthrough:
1977 * @trans: the #GstBaseTransform to set
1978 * @passthrough: boolean indicating passthrough mode.
1980 * Set passthrough mode for this filter by default. This is mostly
1981 * useful for filters that do not care about negotiation.
1983 * Always TRUE for filters which don't implement either a transform
1984 * or transform_ip method.
1989 gst_base_transform_set_passthrough (GstBaseTransform * trans,
1990 gboolean passthrough)
1992 GstBaseTransformClass *bclass;
1994 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
1996 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1998 GST_OBJECT_LOCK (trans);
1999 if (passthrough == FALSE) {
2000 if (bclass->transform_ip || bclass->transform)
2001 trans->passthrough = FALSE;
2003 trans->passthrough = TRUE;
2006 GST_DEBUG_OBJECT (trans, "set passthrough %d", trans->passthrough);
2007 GST_OBJECT_UNLOCK (trans);
2011 * gst_base_transform_is_passthrough:
2012 * @trans: the #GstBaseTransform to query
2014 * See if @trans is configured as a passthrough transform.
2016 * Returns: TRUE is the transform is configured in passthrough mode.
2021 gst_base_transform_is_passthrough (GstBaseTransform * trans)
2025 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2027 GST_OBJECT_LOCK (trans);
2028 result = trans->passthrough;
2029 GST_OBJECT_UNLOCK (trans);
2035 * gst_base_transform_set_in_place:
2036 * @trans: the #GstBaseTransform to modify
2037 * @in_place: Boolean value indicating that we would like to operate
2038 * on in_place buffers.
2040 * Determines whether a non-writable buffer will be copied before passing
2041 * to the transform_ip function.
2043 * <listitem>Always TRUE if no transform function is implemented.</listitem>
2044 * <listitem>Always FALSE if ONLY transform function is implemented.</listitem>
2050 gst_base_transform_set_in_place (GstBaseTransform * trans, gboolean in_place)
2052 GstBaseTransformClass *bclass;
2054 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2056 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2058 GST_OBJECT_LOCK (trans);
2061 if (bclass->transform_ip) {
2062 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
2063 trans->always_in_place = TRUE;
2066 if (bclass->transform) {
2067 GST_DEBUG_OBJECT (trans, "setting in_place FALSE");
2068 trans->always_in_place = FALSE;
2072 GST_OBJECT_UNLOCK (trans);
2076 * gst_base_transform_is_in_place:
2077 * @trans: the #GstBaseTransform to query
2079 * See if @trans is configured as a in_place transform.
2081 * Returns: TRUE is the transform is configured in in_place mode.
2086 gst_base_transform_is_in_place (GstBaseTransform * trans)
2090 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2092 GST_OBJECT_LOCK (trans);
2093 result = trans->always_in_place;
2094 GST_OBJECT_UNLOCK (trans);
2100 * gst_base_transform_update_qos:
2101 * @trans: a #GstBaseTransform
2102 * @proportion: the proportion
2103 * @diff: the diff against the clock
2104 * @timestamp: the timestamp of the buffer generating the QoS expressed in
2107 * Set the QoS parameters in the transform. This function is called internally
2108 * when a QOS event is received but subclasses can provide custom information
2116 gst_base_transform_update_qos (GstBaseTransform * trans,
2117 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2120 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2122 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans,
2123 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2124 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2126 GST_OBJECT_LOCK (trans);
2127 trans->priv->proportion = proportion;
2128 trans->priv->earliest_time = timestamp + diff;
2129 GST_OBJECT_UNLOCK (trans);
2133 * gst_base_transform_set_qos_enabled:
2134 * @trans: a #GstBaseTransform
2135 * @enabled: new state
2137 * Enable or disable QoS handling in the transform.
2144 gst_base_transform_set_qos_enabled (GstBaseTransform * trans, gboolean enabled)
2146 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2148 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "enabled: %d", enabled);
2150 GST_OBJECT_LOCK (trans);
2151 trans->priv->qos_enabled = enabled;
2152 GST_OBJECT_UNLOCK (trans);
2156 * gst_base_transform_is_qos_enabled:
2157 * @trans: a #GstBaseTransform
2159 * Queries if the transform will handle QoS.
2161 * Returns: TRUE if QoS is enabled.
2168 gst_base_transform_is_qos_enabled (GstBaseTransform * trans)
2172 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2174 GST_OBJECT_LOCK (trans);
2175 result = trans->priv->qos_enabled;
2176 GST_OBJECT_UNLOCK (trans);
2182 * gst_base_transform_set_gap_aware:
2183 * @trans: a #GstBaseTransform
2184 * @gap_aware: New state
2186 * If @gap_aware is %FALSE (the default), output buffers will have the
2187 * %GST_BUFFER_FLAG_GAP flag unset.
2189 * If set to %TRUE, the element must handle output buffers with this flag set
2190 * correctly, i.e. it can assume that the buffer contains neutral data but must
2191 * unset the flag if the output is no neutral data.
2198 gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware)
2200 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2202 GST_OBJECT_LOCK (trans);
2203 trans->priv->gap_aware = gap_aware;
2204 GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware);
2205 GST_OBJECT_UNLOCK (trans);
2209 * gst_base_transform_suggest:
2210 * @trans: a #GstBaseTransform
2211 * @gcaps: caps to suggest
2213 * Instructs @trans to suggest new @caps upstream.
2218 gst_base_transform_suggest (GstBaseTransform * trans, GstCaps * caps,
2221 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2223 GST_OBJECT_LOCK (trans->sinkpad);
2224 if (trans->priv->sink_suggest)
2225 gst_caps_unref (trans->priv->sink_suggest);
2226 trans->priv->sink_suggest = gst_caps_copy (caps);
2227 trans->priv->size_suggest = size;
2228 gst_caps_replace (&trans->priv->sink_alloc, NULL);
2229 GST_DEBUG_OBJECT (trans, "new suggest %" GST_PTR_FORMAT, caps);
2230 GST_OBJECT_UNLOCK (trans->sinkpad);
2235 * gst_base_transform_reconfigure:
2236 * @trans: a #GstBaseTransform
2238 * Instructs @trans to renegotiate a new downstream transform on the next
2241 * Note: Not yet implemented.
2246 gst_base_transform_reconfigure (GstBaseTransform * trans)