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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, 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. Elements
30 * that are suitable for implementation using #GstBaseTransform are ones
31 * where the size and caps of the output is known entirely from the input
32 * caps and buffer sizes. These include elements that directly transform
33 * one buffer into another, modify the contents of a buffer in-place, as
34 * well as elements that collate multiple input buffers into one output buffer,
35 * or that expand one input buffer into multiple output buffers. See below
36 * for more concrete use cases.
40 * <listitem><para>one sinkpad and one srcpad</para></listitem>
42 * Possible formats on sink and source pad implemented
43 * with custom transform_caps function. By default uses
44 * same format on sink and source.
46 * <listitem><para>Handles state changes</para></listitem>
47 * <listitem><para>Does flushing</para></listitem>
48 * <listitem><para>Push mode</para></listitem>
50 * Pull mode if the sub-class transform can operate on arbitrary data
55 * <title>Use Cases</title>
59 * <itemizedlist><title>Passthrough mode</title>
61 * Element has no interest in modifying the buffer. It may want to inspect it,
62 * in which case the element should have a transform_ip function. If there
63 * is no transform_ip function in passthrough mode, the buffer is pushed
67 * The #GstBaseTransformClass.passthrough_on_same_caps variable
68 * will automatically set/unset passthrough based on whether the
69 * element negotiates the same caps on both pads.
72 * #GstBaseTransformClass.passthrough_on_same_caps on an element that
73 * doesn't implement a transform_caps function is useful for elements that
74 * only inspect data (such as level)
78 * <title>Example elements</title>
79 * <listitem>Level</listitem>
80 * <listitem>Videoscale, audioconvert, videoconvert, audioresample in
81 * certain modes.</listitem>
86 * <title>Modifications in-place - input buffer and output buffer are the
89 * The element must implement a transform_ip function.
92 * Output buffer size must <= input buffer size
95 * If the always_in_place flag is set, non-writable buffers will be copied
96 * and passed to the transform_ip function, otherwise a new buffer will be
97 * created and the transform function called.
100 * Incoming writable buffers will be passed to the transform_ip function
101 * immediately. </para></listitem>
103 * only implementing transform_ip and not transform implies always_in_place
108 * <title>Example elements</title>
109 * <listitem>Volume</listitem>
110 * <listitem>Audioconvert in certain modes (signed/unsigned
111 * conversion)</listitem>
112 * <listitem>videoconvert in certain modes (endianness
113 * swapping)</listitem>
118 * <title>Modifications only to the caps/metadata of a buffer</title>
120 * The element does not require writable data, but non-writable buffers
121 * should be subbuffered so that the meta-information can be replaced.
124 * Elements wishing to operate in this mode should replace the
125 * prepare_output_buffer method to create subbuffers of the input buffer
126 * and set always_in_place to %TRUE
130 * <title>Example elements</title>
131 * <listitem>Capsfilter when setting caps on outgoing buffers that have
133 * <listitem>identity when it is going to re-timestamp buffers by
134 * datarate.</listitem>
138 * <itemizedlist><title>Normal mode</title>
140 * always_in_place flag is not set, or there is no transform_ip function
143 * Element will receive an input buffer and output buffer to operate on.
146 * Output buffer is allocated by calling the prepare_output_buffer function.
150 * <title>Example elements</title>
151 * <listitem>Videoscale, videoconvert, audioconvert when doing
152 * scaling/conversions</listitem>
156 * <itemizedlist><title>Special output buffer allocations</title>
158 * Elements which need to do special allocation of their output buffers
159 * beyond allocating output buffers via the negotiated allocator or
160 * buffer pool should implement the prepare_output_buffer method.
164 * <title>Example elements</title>
165 * <listitem>efence</listitem>
172 * <title>Sub-class settable flags on GstBaseTransform</title>
176 * <itemizedlist><title>passthrough</title>
178 * Implies that in the current configuration, the sub-class is not
179 * interested in modifying the buffers.
182 * Elements which are always in passthrough mode whenever the same caps
183 * has been negotiated on both pads can set the class variable
184 * passthrough_on_same_caps to have this behaviour automatically.
189 * <itemizedlist><title>always_in_place</title>
191 * Determines whether a non-writable buffer will be copied before passing
192 * to the transform_ip function.
195 * Implied %TRUE if no transform function is implemented.
198 * Implied %FALSE if ONLY transform function is implemented.
214 #include "../../../gst/gst_private.h"
215 #include "../../../gst/gst-i18n-lib.h"
216 #include "../../../gst/glib-compat-private.h"
217 #include "gstbasetransform.h"
219 GST_DEBUG_CATEGORY_STATIC (gst_base_transform_debug);
220 #define GST_CAT_DEFAULT gst_base_transform_debug
222 /* BaseTransform signals and args */
229 #define DEFAULT_PROP_QOS FALSE
237 #define GST_BASE_TRANSFORM_GET_PRIVATE(obj) \
238 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_TRANSFORM, GstBaseTransformPrivate))
240 struct _GstBaseTransformPrivate
242 /* Set by sub-class */
243 gboolean passthrough;
244 gboolean always_in_place;
246 GstCaps *cache_caps1;
247 gsize cache_caps1_size;
248 GstCaps *cache_caps2;
249 gsize cache_caps2_size;
250 gboolean have_same_caps;
254 /* QoS *//* with LOCK */
255 gboolean qos_enabled;
257 GstClockTime earliest_time;
258 /* previous buffer had a discont */
264 gboolean prefer_passthrough;
270 GstClockTime position_out;
273 gboolean pool_active;
274 GstAllocator *allocator;
275 GstAllocationParams params;
280 static GstElementClass *parent_class = NULL;
282 static void gst_base_transform_class_init (GstBaseTransformClass * klass);
283 static void gst_base_transform_init (GstBaseTransform * trans,
284 GstBaseTransformClass * klass);
285 static GstFlowReturn default_submit_input_buffer (GstBaseTransform * trans,
286 gboolean is_discont, GstBuffer * input);
287 static GstFlowReturn default_generate_output (GstBaseTransform * trans,
288 GstBuffer ** outbuf);
290 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
291 * method to get to the padtemplates */
293 gst_base_transform_get_type (void)
295 static volatile gsize base_transform_type = 0;
297 if (g_once_init_enter (&base_transform_type)) {
299 static const GTypeInfo base_transform_info = {
300 sizeof (GstBaseTransformClass),
303 (GClassInitFunc) gst_base_transform_class_init,
306 sizeof (GstBaseTransform),
308 (GInstanceInitFunc) gst_base_transform_init,
311 _type = g_type_register_static (GST_TYPE_ELEMENT,
312 "GstBaseTransform", &base_transform_info, G_TYPE_FLAG_ABSTRACT);
313 g_once_init_leave (&base_transform_type, _type);
315 return base_transform_type;
318 static void gst_base_transform_finalize (GObject * object);
319 static void gst_base_transform_set_property (GObject * object, guint prop_id,
320 const GValue * value, GParamSpec * pspec);
321 static void gst_base_transform_get_property (GObject * object, guint prop_id,
322 GValue * value, GParamSpec * pspec);
323 static gboolean gst_base_transform_src_activate_mode (GstPad * pad,
324 GstObject * parent, GstPadMode mode, gboolean active);
325 static gboolean gst_base_transform_sink_activate_mode (GstPad * pad,
326 GstObject * parent, GstPadMode mode, gboolean active);
327 static gboolean gst_base_transform_activate (GstBaseTransform * trans,
329 static gboolean gst_base_transform_get_unit_size (GstBaseTransform * trans,
330 GstCaps * caps, gsize * size);
332 static gboolean gst_base_transform_src_event (GstPad * pad, GstObject * parent,
334 static gboolean gst_base_transform_src_eventfunc (GstBaseTransform * trans,
336 static gboolean gst_base_transform_sink_event (GstPad * pad, GstObject * parent,
338 static gboolean gst_base_transform_sink_eventfunc (GstBaseTransform * trans,
340 static GstFlowReturn gst_base_transform_getrange (GstPad * pad,
341 GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
342 static GstFlowReturn gst_base_transform_chain (GstPad * pad, GstObject * parent,
344 static GstCaps *gst_base_transform_default_transform_caps (GstBaseTransform *
345 trans, GstPadDirection direction, GstCaps * caps, GstCaps * filter);
346 static GstCaps *gst_base_transform_default_fixate_caps (GstBaseTransform *
347 trans, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
348 static GstCaps *gst_base_transform_query_caps (GstBaseTransform * trans,
349 GstPad * pad, GstCaps * filter);
350 static gboolean gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
351 GstPadDirection direction, GstCaps * caps);
352 static gboolean gst_base_transform_setcaps (GstBaseTransform * trans,
353 GstPad * pad, GstCaps * caps);
354 static gboolean gst_base_transform_default_decide_allocation (GstBaseTransform
355 * trans, GstQuery * query);
356 static gboolean gst_base_transform_default_propose_allocation (GstBaseTransform
357 * trans, GstQuery * decide_query, GstQuery * query);
358 static gboolean gst_base_transform_query (GstPad * pad, GstObject * parent,
360 static gboolean gst_base_transform_default_query (GstBaseTransform * trans,
361 GstPadDirection direction, GstQuery * query);
362 static gboolean gst_base_transform_default_transform_size (GstBaseTransform *
363 trans, GstPadDirection direction, GstCaps * caps, gsize size,
364 GstCaps * othercaps, gsize * othersize);
366 static GstFlowReturn default_prepare_output_buffer (GstBaseTransform * trans,
367 GstBuffer * inbuf, GstBuffer ** outbuf);
368 static gboolean default_copy_metadata (GstBaseTransform * trans,
369 GstBuffer * inbuf, GstBuffer * outbuf);
371 gst_base_transform_default_transform_meta (GstBaseTransform * trans,
372 GstBuffer * inbuf, GstMeta * meta, GstBuffer * outbuf);
374 /* static guint gst_base_transform_signals[LAST_SIGNAL] = { 0 }; */
378 gst_base_transform_finalize (GObject * object)
380 G_OBJECT_CLASS (parent_class)->finalize (object);
384 gst_base_transform_class_init (GstBaseTransformClass * klass)
386 GObjectClass *gobject_class;
388 gobject_class = G_OBJECT_CLASS (klass);
390 GST_DEBUG_CATEGORY_INIT (gst_base_transform_debug, "basetransform", 0,
391 "basetransform element");
393 GST_DEBUG ("gst_base_transform_class_init");
395 g_type_class_add_private (klass, sizeof (GstBaseTransformPrivate));
397 parent_class = g_type_class_peek_parent (klass);
399 gobject_class->set_property = gst_base_transform_set_property;
400 gobject_class->get_property = gst_base_transform_get_property;
402 g_object_class_install_property (gobject_class, PROP_QOS,
403 g_param_spec_boolean ("qos", "QoS", "Handle Quality-of-Service events",
404 DEFAULT_PROP_QOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406 gobject_class->finalize = gst_base_transform_finalize;
408 klass->passthrough_on_same_caps = FALSE;
409 klass->transform_ip_on_passthrough = TRUE;
411 klass->transform_caps =
412 GST_DEBUG_FUNCPTR (gst_base_transform_default_transform_caps);
414 GST_DEBUG_FUNCPTR (gst_base_transform_default_fixate_caps);
416 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps_default);
417 klass->query = GST_DEBUG_FUNCPTR (gst_base_transform_default_query);
418 klass->decide_allocation =
419 GST_DEBUG_FUNCPTR (gst_base_transform_default_decide_allocation);
420 klass->propose_allocation =
421 GST_DEBUG_FUNCPTR (gst_base_transform_default_propose_allocation);
422 klass->transform_size =
423 GST_DEBUG_FUNCPTR (gst_base_transform_default_transform_size);
424 klass->transform_meta =
425 GST_DEBUG_FUNCPTR (gst_base_transform_default_transform_meta);
427 klass->sink_event = GST_DEBUG_FUNCPTR (gst_base_transform_sink_eventfunc);
428 klass->src_event = GST_DEBUG_FUNCPTR (gst_base_transform_src_eventfunc);
429 klass->prepare_output_buffer =
430 GST_DEBUG_FUNCPTR (default_prepare_output_buffer);
431 klass->copy_metadata = GST_DEBUG_FUNCPTR (default_copy_metadata);
432 klass->submit_input_buffer = GST_DEBUG_FUNCPTR (default_submit_input_buffer);
433 klass->generate_output = GST_DEBUG_FUNCPTR (default_generate_output);
437 gst_base_transform_init (GstBaseTransform * trans,
438 GstBaseTransformClass * bclass)
440 GstPadTemplate *pad_template;
441 GstBaseTransformPrivate *priv;
443 GST_DEBUG ("gst_base_transform_init");
445 priv = trans->priv = GST_BASE_TRANSFORM_GET_PRIVATE (trans);
448 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
449 g_return_if_fail (pad_template != NULL);
450 trans->sinkpad = gst_pad_new_from_template (pad_template, "sink");
451 gst_pad_set_event_function (trans->sinkpad,
452 GST_DEBUG_FUNCPTR (gst_base_transform_sink_event));
453 gst_pad_set_chain_function (trans->sinkpad,
454 GST_DEBUG_FUNCPTR (gst_base_transform_chain));
455 gst_pad_set_activatemode_function (trans->sinkpad,
456 GST_DEBUG_FUNCPTR (gst_base_transform_sink_activate_mode));
457 gst_pad_set_query_function (trans->sinkpad,
458 GST_DEBUG_FUNCPTR (gst_base_transform_query));
459 gst_element_add_pad (GST_ELEMENT (trans), trans->sinkpad);
462 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
463 g_return_if_fail (pad_template != NULL);
464 trans->srcpad = gst_pad_new_from_template (pad_template, "src");
465 gst_pad_set_event_function (trans->srcpad,
466 GST_DEBUG_FUNCPTR (gst_base_transform_src_event));
467 gst_pad_set_getrange_function (trans->srcpad,
468 GST_DEBUG_FUNCPTR (gst_base_transform_getrange));
469 gst_pad_set_activatemode_function (trans->srcpad,
470 GST_DEBUG_FUNCPTR (gst_base_transform_src_activate_mode));
471 gst_pad_set_query_function (trans->srcpad,
472 GST_DEBUG_FUNCPTR (gst_base_transform_query));
473 gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
475 priv->qos_enabled = DEFAULT_PROP_QOS;
476 priv->cache_caps1 = NULL;
477 priv->cache_caps2 = NULL;
478 priv->pad_mode = GST_PAD_MODE_NONE;
479 priv->gap_aware = FALSE;
480 priv->prefer_passthrough = TRUE;
482 priv->passthrough = FALSE;
483 if (bclass->transform == NULL) {
484 /* If no transform function, always_in_place is TRUE */
485 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
486 priv->always_in_place = TRUE;
488 if (bclass->transform_ip == NULL) {
489 GST_DEBUG_OBJECT (trans, "setting passthrough TRUE");
490 priv->passthrough = TRUE;
499 gst_base_transform_default_transform_caps (GstBaseTransform * trans,
500 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
504 GST_DEBUG_OBJECT (trans, "identity from: %" GST_PTR_FORMAT, caps);
505 /* no transform function, use the identity transform */
507 ret = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
509 ret = gst_caps_ref (caps);
514 /* given @caps on the src or sink pad (given by @direction)
515 * calculate the possible caps on the other pad.
517 * Returns new caps, unref after usage.
520 gst_base_transform_transform_caps (GstBaseTransform * trans,
521 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
524 GstBaseTransformClass *klass;
529 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
531 /* if there is a custom transform function, use this */
532 if (klass->transform_caps) {
533 GST_DEBUG_OBJECT (trans, "transform caps (direction = %d)", direction);
535 GST_LOG_OBJECT (trans, "from: %" GST_PTR_FORMAT, caps);
536 ret = klass->transform_caps (trans, direction, caps, filter);
537 GST_LOG_OBJECT (trans, " to: %" GST_PTR_FORMAT, ret);
539 #ifdef GST_ENABLE_EXTRA_CHECKS
541 if (!gst_caps_is_subset (ret, filter)) {
542 GstCaps *intersection;
544 GST_ERROR_OBJECT (trans,
545 "transform_caps returned caps %" GST_PTR_FORMAT
546 " which are not a real subset of the filter caps %"
547 GST_PTR_FORMAT, ret, filter);
548 g_warning ("%s: transform_caps returned caps which are not a real "
549 "subset of the filter caps", GST_ELEMENT_NAME (trans));
552 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
553 gst_caps_unref (ret);
560 GST_DEBUG_OBJECT (trans, "to: %" GST_PTR_FORMAT, ret);
566 gst_base_transform_default_transform_meta (GstBaseTransform * trans,
567 GstBuffer * inbuf, GstMeta * meta, GstBuffer * outbuf)
569 const GstMetaInfo *info = meta->info;
570 const gchar *const *tags;
572 tags = gst_meta_api_type_get_tags (info->api);
581 gst_base_transform_default_transform_size (GstBaseTransform * trans,
582 GstPadDirection direction, GstCaps * caps, gsize size,
583 GstCaps * othercaps, gsize * othersize)
585 gsize inunitsize, outunitsize, units;
586 GstBaseTransformClass *klass;
588 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
590 if (klass->get_unit_size == NULL) {
591 /* if there is no transform_size and no unit_size, it means the
592 * element does not modify the size of a buffer */
595 /* there is no transform_size function, we have to use the unit_size
596 * functions. This method assumes there is a fixed unit_size associated with
597 * each caps. We provide the same amount of units on both sides. */
598 if (!gst_base_transform_get_unit_size (trans, caps, &inunitsize))
601 GST_DEBUG_OBJECT (trans,
602 "input size %" G_GSIZE_FORMAT ", input unit size %" G_GSIZE_FORMAT,
605 /* input size must be a multiple of the unit_size of the input caps */
606 if (inunitsize == 0 || (size % inunitsize != 0))
609 /* get the amount of units */
610 units = size / inunitsize;
612 /* now get the unit size of the output */
613 if (!gst_base_transform_get_unit_size (trans, othercaps, &outunitsize))
616 /* the output size is the unit_size times the amount of units on the
618 *othersize = units * outunitsize;
619 GST_DEBUG_OBJECT (trans, "transformed size to %" G_GSIZE_FORMAT,
627 GST_DEBUG_OBJECT (trans, "could not get in_size");
628 g_warning ("%s: could not get in_size", GST_ELEMENT_NAME (trans));
633 GST_DEBUG_OBJECT (trans, "Size %" G_GSIZE_FORMAT " is not a multiple of"
634 "unit size %" G_GSIZE_FORMAT, size, inunitsize);
635 g_warning ("%s: size %" G_GSIZE_FORMAT " is not a multiple of unit size %"
636 G_GSIZE_FORMAT, GST_ELEMENT_NAME (trans), size, inunitsize);
641 GST_DEBUG_OBJECT (trans, "could not get out_size");
642 g_warning ("%s: could not get out_size", GST_ELEMENT_NAME (trans));
647 /* transform a buffer of @size with @caps on the pad with @direction to
648 * the size of a buffer with @othercaps and store the result in @othersize
650 * We have two ways of doing this:
651 * 1) use a custom transform size function, this is for complicated custom
652 * cases with no fixed unit_size.
653 * 2) use the unit_size functions where there is a relationship between the
654 * caps and the size of a buffer.
657 gst_base_transform_transform_size (GstBaseTransform * trans,
658 GstPadDirection direction, GstCaps * caps,
659 gsize size, GstCaps * othercaps, gsize * othersize)
661 GstBaseTransformClass *klass;
662 gboolean ret = FALSE;
664 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
666 GST_DEBUG_OBJECT (trans,
667 "asked to transform size %" G_GSIZE_FORMAT " for caps %"
668 GST_PTR_FORMAT " to size for caps %" GST_PTR_FORMAT " in direction %s",
669 size, caps, othercaps, direction == GST_PAD_SRC ? "SRC" : "SINK");
671 if (klass->transform_size) {
672 /* if there is a custom transform function, use this */
673 ret = klass->transform_size (trans, direction, caps, size, othercaps,
679 /* get the caps that can be handled by @pad. We perform:
681 * - take the caps of peer of otherpad,
682 * - filter against the padtemplate of otherpad,
683 * - calculate all transforms of remaining caps
684 * - filter against template of @pad
686 * If there is no peer, we simply return the caps of the padtemplate of pad.
689 gst_base_transform_query_caps (GstBaseTransform * trans, GstPad * pad,
693 GstCaps *peercaps = NULL, *caps, *temp, *peerfilter = NULL;
694 GstCaps *templ, *otempl;
696 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
698 templ = gst_pad_get_pad_template_caps (pad);
699 otempl = gst_pad_get_pad_template_caps (otherpad);
701 /* first prepare the filter to be send onwards. We need to filter and
702 * transform it to valid caps for the otherpad. */
704 GST_DEBUG_OBJECT (pad, "filter caps %" GST_PTR_FORMAT, filter);
706 /* filtered against our padtemplate of this pad */
707 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
708 temp = gst_caps_intersect_full (filter, templ, GST_CAPS_INTERSECT_FIRST);
709 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
711 /* then see what we can transform this to */
712 peerfilter = gst_base_transform_transform_caps (trans,
713 GST_PAD_DIRECTION (pad), temp, NULL);
714 GST_DEBUG_OBJECT (pad, "transformed %" GST_PTR_FORMAT, peerfilter);
715 gst_caps_unref (temp);
717 if (!gst_caps_is_empty (peerfilter)) {
718 /* and filter against the template of the other pad */
719 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, otempl);
720 /* We keep the caps sorted like the returned caps */
722 gst_caps_intersect_full (peerfilter, otempl,
723 GST_CAPS_INTERSECT_FIRST);
724 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
725 gst_caps_unref (peerfilter);
730 GST_DEBUG_OBJECT (pad, "peer filter caps %" GST_PTR_FORMAT, peerfilter);
732 if (peerfilter && gst_caps_is_empty (peerfilter)) {
733 GST_DEBUG_OBJECT (pad, "peer filter caps are empty");
739 /* query the peer with the transformed filter */
740 peercaps = gst_pad_peer_query_caps (otherpad, peerfilter);
743 gst_caps_unref (peerfilter);
746 GST_DEBUG_OBJECT (pad, "peer caps %" GST_PTR_FORMAT, peercaps);
748 /* filtered against our padtemplate on the other side */
749 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, otempl);
750 temp = gst_caps_intersect_full (peercaps, otempl, GST_CAPS_INTERSECT_FIRST);
751 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
753 temp = gst_caps_ref (otempl);
756 /* then see what we can transform this to */
757 caps = gst_base_transform_transform_caps (trans,
758 GST_PAD_DIRECTION (otherpad), temp, filter);
759 GST_DEBUG_OBJECT (pad, "transformed %" GST_PTR_FORMAT, caps);
760 gst_caps_unref (temp);
761 if (caps == NULL || gst_caps_is_empty (caps))
765 /* and filter against the template of this pad */
766 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
767 /* We keep the caps sorted like the returned caps */
768 temp = gst_caps_intersect_full (caps, templ, GST_CAPS_INTERSECT_FIRST);
769 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
770 gst_caps_unref (caps);
773 if (trans->priv->prefer_passthrough) {
774 /* Now try if we can put the untransformed downstream caps first */
775 temp = gst_caps_intersect_full (peercaps, caps, GST_CAPS_INTERSECT_FIRST);
776 if (!gst_caps_is_empty (temp)) {
777 caps = gst_caps_merge (temp, caps);
779 gst_caps_unref (temp);
783 gst_caps_unref (caps);
784 /* no peer or the peer can do anything, our padtemplate is enough then */
786 caps = gst_caps_intersect_full (filter, templ, GST_CAPS_INTERSECT_FIRST);
788 caps = gst_caps_ref (templ);
793 GST_DEBUG_OBJECT (trans, "returning %" GST_PTR_FORMAT, caps);
796 gst_caps_unref (peercaps);
798 gst_caps_unref (templ);
799 gst_caps_unref (otempl);
804 /* takes ownership of the pool, allocator and query */
806 gst_base_transform_set_allocation (GstBaseTransform * trans,
807 GstBufferPool * pool, GstAllocator * allocator,
808 GstAllocationParams * params, GstQuery * query)
810 GstAllocator *oldalloc;
811 GstBufferPool *oldpool;
813 GstBaseTransformPrivate *priv = trans->priv;
815 GST_OBJECT_LOCK (trans);
816 oldpool = priv->pool;
818 priv->pool_active = FALSE;
820 oldalloc = priv->allocator;
821 priv->allocator = allocator;
823 oldquery = priv->query;
827 priv->params = *params;
829 gst_allocation_params_init (&priv->params);
830 GST_OBJECT_UNLOCK (trans);
833 GST_DEBUG_OBJECT (trans, "deactivating old pool %p", oldpool);
834 gst_buffer_pool_set_active (oldpool, FALSE);
835 gst_object_unref (oldpool);
838 gst_object_unref (oldalloc);
841 gst_query_unref (oldquery);
847 gst_base_transform_default_decide_allocation (GstBaseTransform * trans,
851 GstBaseTransformClass *klass;
854 guint size, min, max;
855 GstAllocator *allocator;
856 GstAllocationParams params;
857 GstStructure *config;
858 gboolean update_allocator;
860 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
862 n_metas = gst_query_get_n_allocation_metas (query);
863 for (i = 0; i < n_metas; i++) {
865 const GstStructure *params;
868 api = gst_query_parse_nth_allocation_meta (query, i, ¶ms);
870 /* by default we remove all metadata, subclasses should implement a
871 * filter_meta function */
872 if (gst_meta_api_type_has_tag (api, _gst_meta_tag_memory)) {
873 /* remove all memory dependent metadata because we are going to have to
874 * allocate different memory for input and output. */
875 GST_LOG_OBJECT (trans, "removing memory specific metadata %s",
878 } else if (G_LIKELY (klass->filter_meta)) {
879 /* remove if the subclass said so */
880 remove = !klass->filter_meta (trans, query, api, params);
881 GST_LOG_OBJECT (trans, "filter_meta for api %s returned: %s",
882 g_type_name (api), (remove ? "remove" : "keep"));
884 GST_LOG_OBJECT (trans, "removing metadata %s", g_type_name (api));
889 gst_query_remove_nth_allocation_meta (query, i);
895 gst_query_parse_allocation (query, &outcaps, NULL);
897 /* we got configuration from our peer or the decide_allocation method,
899 if (gst_query_get_n_allocation_params (query) > 0) {
900 /* try the allocator */
901 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
902 update_allocator = TRUE;
905 gst_allocation_params_init (¶ms);
906 update_allocator = FALSE;
909 if (gst_query_get_n_allocation_pools (query) > 0) {
910 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
913 /* no pool, we can make our own */
914 GST_DEBUG_OBJECT (trans, "no pool, making new pool");
915 pool = gst_buffer_pool_new ();
919 size = min = max = 0;
924 config = gst_buffer_pool_get_config (pool);
925 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
926 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
928 /* buffer pool may have to do some changes */
929 if (!gst_buffer_pool_set_config (pool, config)) {
930 config = gst_buffer_pool_get_config (pool);
932 /* If change are not acceptable, fallback to generic pool */
933 if (!gst_buffer_pool_config_validate_params (config, outcaps, size, min,
935 GST_DEBUG_OBJECT (trans, "unsuported pool, making new pool");
937 gst_object_unref (pool);
938 pool = gst_buffer_pool_new ();
939 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
940 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
943 if (!gst_buffer_pool_set_config (pool, config))
948 if (update_allocator)
949 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
951 gst_query_add_allocation_param (query, allocator, ¶ms);
953 gst_object_unref (allocator);
956 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
957 gst_object_unref (pool);
964 gst_object_unref (pool);
966 GST_ELEMENT_ERROR (trans, RESOURCE, SETTINGS,
967 ("Failed to configure the buffer pool"),
968 ("Configuration is most likely invalid, please report this issue."));
973 gst_base_transform_do_bufferpool (GstBaseTransform * trans, GstCaps * outcaps)
976 gboolean result = TRUE;
977 GstBufferPool *pool = NULL;
978 GstBaseTransformClass *klass;
979 GstBaseTransformPrivate *priv = trans->priv;
980 GstAllocator *allocator;
981 GstAllocationParams params;
983 /* there are these possibilities:
985 * 1) we negotiated passthrough, we can proxy the bufferpool directly and we
986 * will do that whenever some upstream does an allocation query.
987 * 2) we need to do a transform, we need to get a bufferpool from downstream
988 * and configure it. When upstream does the ALLOCATION query, the
989 * propose_allocation vmethod will be called and we will configure the
990 * upstream allocator with our proposed values then.
992 if (priv->passthrough || priv->always_in_place) {
993 /* we are in passthrough, the input buffer is never copied and always passed
994 * along. We never allocate an output buffer on the srcpad. What we do is
995 * let the upstream element decide if it wants to use a bufferpool and
996 * then we will proxy the downstream pool */
997 GST_DEBUG_OBJECT (trans, "we're passthough, delay bufferpool");
998 gst_base_transform_set_allocation (trans, NULL, NULL, NULL, NULL);
1002 /* not passthrough, we need to allocate */
1003 /* find a pool for the negotiated caps now */
1004 GST_DEBUG_OBJECT (trans, "doing allocation query");
1005 query = gst_query_new_allocation (outcaps, TRUE);
1006 if (!gst_pad_peer_query (trans->srcpad, query)) {
1007 /* not a problem, just debug a little */
1008 GST_DEBUG_OBJECT (trans, "peer ALLOCATION query failed");
1011 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1013 GST_DEBUG_OBJECT (trans, "calling decide_allocation");
1014 g_assert (klass->decide_allocation != NULL);
1015 result = klass->decide_allocation (trans, query);
1017 GST_DEBUG_OBJECT (trans, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
1021 goto no_decide_allocation;
1023 /* we got configuration from our peer or the decide_allocation method,
1025 if (gst_query_get_n_allocation_params (query) > 0) {
1026 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1029 gst_allocation_params_init (¶ms);
1032 if (gst_query_get_n_allocation_pools (query) > 0)
1033 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
1037 gst_base_transform_set_allocation (trans, pool, allocator, ¶ms,
1043 no_decide_allocation:
1045 GST_WARNING_OBJECT (trans, "Subclass failed to decide allocation");
1046 gst_query_unref (query);
1052 /* function triggered when the in and out caps are negotiated and need
1053 * to be configured in the subclass. */
1055 gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
1058 gboolean ret = TRUE;
1059 GstBaseTransformClass *klass;
1060 GstBaseTransformPrivate *priv = trans->priv;
1062 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1064 GST_DEBUG_OBJECT (trans, "in caps: %" GST_PTR_FORMAT, in);
1065 GST_DEBUG_OBJECT (trans, "out caps: %" GST_PTR_FORMAT, out);
1067 /* clear the cache */
1068 gst_caps_replace (&priv->cache_caps1, NULL);
1069 gst_caps_replace (&priv->cache_caps2, NULL);
1071 /* figure out same caps state */
1072 priv->have_same_caps = gst_caps_is_equal (in, out);
1073 GST_DEBUG_OBJECT (trans, "have_same_caps: %d", priv->have_same_caps);
1075 /* Set the passthrough if the class wants passthrough_on_same_caps
1076 * and we have the same caps on each pad */
1077 if (klass->passthrough_on_same_caps)
1078 gst_base_transform_set_passthrough (trans, priv->have_same_caps);
1080 /* now configure the element with the caps */
1081 if (klass->set_caps) {
1082 GST_DEBUG_OBJECT (trans, "Calling set_caps method to setup caps");
1083 ret = klass->set_caps (trans, in, out);
1090 gst_base_transform_default_fixate_caps (GstBaseTransform * trans,
1091 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
1093 othercaps = gst_caps_fixate (othercaps);
1094 GST_DEBUG_OBJECT (trans, "fixated to %" GST_PTR_FORMAT, othercaps);
1099 /* given a fixed @caps on @pad, create the best possible caps for the
1101 * @caps must be fixed when calling this function.
1103 * This function calls the transform caps vmethod of the basetransform to figure
1104 * out the possible target formats. It then tries to select the best format from
1107 * - attempt passthrough if the target caps is a superset of the input caps
1108 * - fixating by using peer caps
1109 * - fixating with transform fixate function
1110 * - fixating with pad fixate functions.
1112 * this function returns a caps that can be transformed into and is accepted by
1116 gst_base_transform_find_transform (GstBaseTransform * trans, GstPad * pad,
1119 GstBaseTransformClass *klass;
1120 GstPad *otherpad, *otherpeer;
1124 /* caps must be fixed here, this is a programming error if it's not */
1125 g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
1127 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1129 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1130 otherpeer = gst_pad_get_peer (otherpad);
1132 /* see how we can transform the input caps. We need to do this even for
1133 * passthrough because it might be possible that this element cannot support
1134 * passthrough at all. */
1135 othercaps = gst_base_transform_transform_caps (trans,
1136 GST_PAD_DIRECTION (pad), caps, NULL);
1138 /* The caps we can actually output is the intersection of the transformed
1139 * caps with the pad template for the pad */
1140 if (othercaps && !gst_caps_is_empty (othercaps)) {
1141 GstCaps *intersect, *templ_caps;
1143 templ_caps = gst_pad_get_pad_template_caps (otherpad);
1144 GST_DEBUG_OBJECT (trans,
1145 "intersecting against padtemplate %" GST_PTR_FORMAT, templ_caps);
1148 gst_caps_intersect_full (othercaps, templ_caps,
1149 GST_CAPS_INTERSECT_FIRST);
1151 gst_caps_unref (othercaps);
1152 gst_caps_unref (templ_caps);
1153 othercaps = intersect;
1156 /* check if transform is empty */
1157 if (!othercaps || gst_caps_is_empty (othercaps))
1160 /* if the othercaps are not fixed, we need to fixate them, first attempt
1161 * is by attempting passthrough if the othercaps are a superset of caps. */
1162 /* FIXME. maybe the caps is not fixed because it has multiple structures of
1164 is_fixed = gst_caps_is_fixed (othercaps);
1166 GST_DEBUG_OBJECT (trans,
1167 "transform returned non fixed %" GST_PTR_FORMAT, othercaps);
1169 /* Now let's see what the peer suggests based on our transformed caps */
1171 GstCaps *peercaps, *intersection, *templ_caps;
1173 GST_DEBUG_OBJECT (trans,
1174 "Checking peer caps with filter %" GST_PTR_FORMAT, othercaps);
1176 peercaps = gst_pad_query_caps (otherpeer, othercaps);
1177 GST_DEBUG_OBJECT (trans, "Resulted in %" GST_PTR_FORMAT, peercaps);
1178 if (!gst_caps_is_empty (peercaps)) {
1179 templ_caps = gst_pad_get_pad_template_caps (otherpad);
1181 GST_DEBUG_OBJECT (trans,
1182 "Intersecting with template caps %" GST_PTR_FORMAT, templ_caps);
1185 gst_caps_intersect_full (peercaps, templ_caps,
1186 GST_CAPS_INTERSECT_FIRST);
1187 GST_DEBUG_OBJECT (trans, "Intersection: %" GST_PTR_FORMAT,
1189 gst_caps_unref (peercaps);
1190 gst_caps_unref (templ_caps);
1191 peercaps = intersection;
1193 GST_DEBUG_OBJECT (trans,
1194 "Intersecting with transformed caps %" GST_PTR_FORMAT, othercaps);
1196 gst_caps_intersect_full (peercaps, othercaps,
1197 GST_CAPS_INTERSECT_FIRST);
1198 GST_DEBUG_OBJECT (trans, "Intersection: %" GST_PTR_FORMAT,
1200 gst_caps_unref (peercaps);
1201 gst_caps_unref (othercaps);
1202 othercaps = intersection;
1204 gst_caps_unref (othercaps);
1205 othercaps = peercaps;
1208 is_fixed = gst_caps_is_fixed (othercaps);
1210 GST_DEBUG_OBJECT (trans, "no peer, doing passthrough");
1211 gst_caps_unref (othercaps);
1212 othercaps = gst_caps_ref (caps);
1216 if (gst_caps_is_empty (othercaps))
1217 goto no_transform_possible;
1219 GST_DEBUG ("have %sfixed caps %" GST_PTR_FORMAT, (is_fixed ? "" : "non-"),
1222 /* second attempt at fixation, call the fixate vmethod */
1223 /* caps could be fixed but the subclass may want to add fields */
1224 if (klass->fixate_caps) {
1225 GST_DEBUG_OBJECT (trans, "calling fixate_caps for %" GST_PTR_FORMAT
1226 " using caps %" GST_PTR_FORMAT " on pad %s:%s", othercaps, caps,
1227 GST_DEBUG_PAD_NAME (otherpad));
1228 /* note that we pass the complete array of structures to the fixate
1229 * function, it needs to truncate itself */
1231 klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
1232 is_fixed = gst_caps_is_fixed (othercaps);
1233 GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
1236 /* caps should be fixed now, if not we have to fail. */
1238 goto could_not_fixate;
1240 /* and peer should accept */
1241 if (otherpeer && !gst_pad_query_accept_caps (otherpeer, othercaps))
1242 goto peer_no_accept;
1244 GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT
1245 ", and got final caps %" GST_PTR_FORMAT, caps, othercaps);
1248 gst_object_unref (otherpeer);
1255 GST_DEBUG_OBJECT (trans,
1256 "transform returned useless %" GST_PTR_FORMAT, othercaps);
1259 no_transform_possible:
1261 GST_DEBUG_OBJECT (trans,
1262 "transform could not transform %" GST_PTR_FORMAT
1263 " in anything we support", caps);
1268 GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
1273 GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
1274 " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1280 gst_object_unref (otherpeer);
1282 gst_caps_unref (othercaps);
1288 gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
1289 GstPadDirection direction, GstCaps * caps)
1291 GstPad *pad, *otherpad;
1292 GstCaps *templ, *otempl, *ocaps = NULL;
1293 gboolean ret = TRUE;
1297 GST_PAD_SINK) ? GST_BASE_TRANSFORM_SINK_PAD (trans) :
1298 GST_BASE_TRANSFORM_SRC_PAD (trans);
1301 GST_PAD_SINK) ? GST_BASE_TRANSFORM_SRC_PAD (trans) :
1302 GST_BASE_TRANSFORM_SINK_PAD (trans);
1304 GST_DEBUG_OBJECT (trans, "accept caps %" GST_PTR_FORMAT, caps);
1306 templ = gst_pad_get_pad_template_caps (pad);
1307 otempl = gst_pad_get_pad_template_caps (otherpad);
1309 /* get all the formats we can handle on this pad */
1310 GST_DEBUG_OBJECT (trans, "intersect with pad template: %" GST_PTR_FORMAT,
1312 if (!gst_caps_can_intersect (caps, templ))
1315 GST_DEBUG_OBJECT (trans, "trying to transform with filter: %"
1316 GST_PTR_FORMAT " (the other pad template)", otempl);
1317 ocaps = gst_base_transform_transform_caps (trans, direction, caps, otempl);
1318 if (!ocaps || gst_caps_is_empty (ocaps))
1319 goto no_transform_possible;
1322 GST_DEBUG_OBJECT (trans, "accept-caps result: %d", ret);
1324 gst_caps_unref (ocaps);
1325 gst_caps_unref (templ);
1326 gst_caps_unref (otempl);
1332 GST_DEBUG_OBJECT (trans, "caps can't intersect with the template");
1336 no_transform_possible:
1338 GST_DEBUG_OBJECT (trans,
1339 "transform could not transform %" GST_PTR_FORMAT
1340 " in anything we support", caps);
1346 /* called when new caps arrive on the sink pad,
1347 * We try to find the best caps for the other side using our _find_transform()
1348 * function. If there are caps, we configure the transform for this new
1352 gst_base_transform_setcaps (GstBaseTransform * trans, GstPad * pad,
1355 GstBaseTransformPrivate *priv = trans->priv;
1356 GstCaps *outcaps, *prev_incaps = NULL, *prev_outcaps = NULL;
1357 gboolean ret = TRUE;
1359 GST_DEBUG_OBJECT (pad, "have new caps %p %" GST_PTR_FORMAT, incaps, incaps);
1361 /* find best possible caps for the other pad */
1362 outcaps = gst_base_transform_find_transform (trans, pad, incaps);
1363 if (!outcaps || gst_caps_is_empty (outcaps))
1364 goto no_transform_possible;
1366 /* configure the element now */
1368 /* if we have the same caps, we can optimize and reuse the input caps */
1369 if (gst_caps_is_equal (incaps, outcaps)) {
1370 GST_INFO_OBJECT (trans, "reuse caps");
1371 gst_caps_unref (outcaps);
1372 outcaps = gst_caps_ref (incaps);
1375 prev_incaps = gst_pad_get_current_caps (trans->sinkpad);
1376 prev_outcaps = gst_pad_get_current_caps (trans->srcpad);
1377 if (prev_incaps && prev_outcaps && gst_caps_is_equal (prev_incaps, incaps)
1378 && gst_caps_is_equal (prev_outcaps, outcaps)) {
1379 GST_DEBUG_OBJECT (trans,
1380 "New caps equal to old ones: %" GST_PTR_FORMAT " -> %" GST_PTR_FORMAT,
1384 /* call configure now */
1385 if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
1386 goto failed_configure;
1388 if (!prev_outcaps || !gst_caps_is_equal (outcaps, prev_outcaps))
1389 /* let downstream know about our caps */
1390 ret = gst_pad_set_caps (trans->srcpad, outcaps);
1394 /* try to get a pool when needed */
1395 ret = gst_base_transform_do_bufferpool (trans, outcaps);
1400 gst_caps_unref (outcaps);
1402 gst_caps_unref (prev_incaps);
1404 gst_caps_unref (prev_outcaps);
1406 GST_OBJECT_LOCK (trans);
1407 priv->negotiated = ret;
1408 GST_OBJECT_UNLOCK (trans);
1413 no_transform_possible:
1415 GST_WARNING_OBJECT (trans,
1416 "transform could not transform %" GST_PTR_FORMAT
1417 " in anything we support", incaps);
1423 GST_WARNING_OBJECT (trans, "FAILED to configure incaps %" GST_PTR_FORMAT
1424 " and outcaps %" GST_PTR_FORMAT, incaps, outcaps);
1431 gst_base_transform_default_propose_allocation (GstBaseTransform * trans,
1432 GstQuery * decide_query, GstQuery * query)
1436 if (decide_query == NULL) {
1437 GST_DEBUG_OBJECT (trans, "doing passthrough query");
1438 ret = gst_pad_peer_query (trans->srcpad, query);
1441 /* non-passthrough, copy all metadata, decide_query does not contain the
1442 * metadata anymore that depends on the buffer memory */
1443 n_metas = gst_query_get_n_allocation_metas (decide_query);
1444 for (i = 0; i < n_metas; i++) {
1446 const GstStructure *params;
1448 api = gst_query_parse_nth_allocation_meta (decide_query, i, ¶ms);
1449 GST_DEBUG_OBJECT (trans, "proposing metadata %s", g_type_name (api));
1450 gst_query_add_allocation_meta (query, api, params);
1458 gst_base_transform_reconfigure (GstBaseTransform * trans)
1460 gboolean reconfigure, ret = TRUE;
1462 reconfigure = gst_pad_check_reconfigure (trans->srcpad);
1464 if (G_UNLIKELY (reconfigure)) {
1467 GST_DEBUG_OBJECT (trans, "we had a pending reconfigure");
1469 incaps = gst_pad_get_current_caps (trans->sinkpad);
1473 /* if we need to reconfigure we pretend new caps arrived. This
1474 * will reconfigure the transform with the new output format. */
1475 if (!gst_base_transform_setcaps (trans, trans->sinkpad, incaps)) {
1476 GST_ELEMENT_WARNING (trans, STREAM, FORMAT,
1477 ("not negotiated"), ("not negotiated"));
1481 gst_caps_unref (incaps);
1487 gst_pad_mark_reconfigure (trans->srcpad);
1493 gst_base_transform_default_query (GstBaseTransform * trans,
1494 GstPadDirection direction, GstQuery * query)
1496 gboolean ret = FALSE;
1497 GstPad *pad, *otherpad;
1498 GstBaseTransformClass *klass;
1499 GstBaseTransformPrivate *priv = trans->priv;
1501 if (direction == GST_PAD_SRC) {
1502 pad = trans->srcpad;
1503 otherpad = trans->sinkpad;
1505 pad = trans->sinkpad;
1506 otherpad = trans->srcpad;
1509 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1511 switch (GST_QUERY_TYPE (query)) {
1512 case GST_QUERY_ALLOCATION:
1514 GstQuery *decide_query = NULL;
1516 /* can only be done on the sinkpad */
1517 if (direction != GST_PAD_SINK)
1520 ret = gst_base_transform_reconfigure (trans);
1521 if (G_UNLIKELY (!ret))
1524 GST_OBJECT_LOCK (trans);
1525 if (!priv->negotiated && !priv->passthrough && (klass->set_caps != NULL)) {
1526 GST_DEBUG_OBJECT (trans,
1527 "not negotiated yet but need negotiation, can't answer ALLOCATION query");
1528 GST_OBJECT_UNLOCK (trans);
1532 decide_query = trans->priv->query;
1533 trans->priv->query = NULL;
1534 GST_OBJECT_UNLOCK (trans);
1536 GST_DEBUG_OBJECT (trans,
1537 "calling propose allocation with query %" GST_PTR_FORMAT,
1540 /* pass the query to the propose_allocation vmethod if any */
1541 if (G_LIKELY (klass->propose_allocation))
1542 ret = klass->propose_allocation (trans, decide_query, query);
1547 GST_OBJECT_LOCK (trans);
1549 if (trans->priv->query == NULL)
1550 trans->priv->query = decide_query;
1552 gst_query_unref (decide_query);
1554 GST_OBJECT_UNLOCK (trans);
1557 GST_DEBUG_OBJECT (trans, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret,
1561 case GST_QUERY_POSITION:
1565 gst_query_parse_position (query, &format, NULL);
1566 if (format == GST_FORMAT_TIME && trans->segment.format == GST_FORMAT_TIME) {
1570 if ((direction == GST_PAD_SINK)
1571 || (trans->priv->position_out == GST_CLOCK_TIME_NONE)) {
1573 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1574 trans->segment.position);
1576 pos = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1577 trans->priv->position_out);
1579 gst_query_set_position (query, format, pos);
1581 ret = gst_pad_peer_query (otherpad, query);
1585 case GST_QUERY_ACCEPT_CAPS:
1589 gst_query_parse_accept_caps (query, &caps);
1590 if (klass->accept_caps) {
1591 ret = klass->accept_caps (trans, direction, caps);
1592 gst_query_set_accept_caps_result (query, ret);
1593 /* return TRUE, we answered the query */
1598 case GST_QUERY_CAPS:
1600 GstCaps *filter, *caps;
1602 gst_query_parse_caps (query, &filter);
1603 caps = gst_base_transform_query_caps (trans, pad, filter);
1604 gst_query_set_caps_result (query, caps);
1605 gst_caps_unref (caps);
1610 ret = gst_pad_peer_query (otherpad, query);
1619 gst_base_transform_query (GstPad * pad, GstObject * parent, GstQuery * query)
1621 GstBaseTransform *trans;
1622 GstBaseTransformClass *bclass;
1623 gboolean ret = FALSE;
1625 trans = GST_BASE_TRANSFORM (parent);
1626 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1629 ret = bclass->query (trans, GST_PAD_DIRECTION (pad), query);
1634 /* this function either returns the input buffer without incrementing the
1635 * refcount or it allocates a new (writable) buffer */
1636 static GstFlowReturn
1637 default_prepare_output_buffer (GstBaseTransform * trans,
1638 GstBuffer * inbuf, GstBuffer ** outbuf)
1640 GstBaseTransformPrivate *priv;
1642 GstBaseTransformClass *bclass;
1643 GstCaps *incaps, *outcaps;
1644 gsize insize, outsize;
1648 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1650 /* figure out how to allocate an output buffer */
1651 if (priv->passthrough) {
1652 /* passthrough, we will not modify the incoming buffer so we can just
1654 GST_DEBUG_OBJECT (trans, "passthrough: reusing input buffer");
1659 /* we can't reuse the input buffer */
1661 if (!priv->pool_active) {
1662 GST_DEBUG_OBJECT (trans, "setting pool %p active", priv->pool);
1663 if (!gst_buffer_pool_set_active (priv->pool, TRUE))
1664 goto activate_failed;
1665 priv->pool_active = TRUE;
1667 GST_DEBUG_OBJECT (trans, "using pool alloc");
1668 ret = gst_buffer_pool_acquire_buffer (priv->pool, outbuf, NULL);
1669 if (ret != GST_FLOW_OK)
1675 /* no pool, we need to figure out the size of the output buffer first */
1676 if ((bclass->transform_ip != NULL) && priv->always_in_place) {
1677 /* we want to do an in-place alloc */
1678 if (gst_buffer_is_writable (inbuf)) {
1679 GST_DEBUG_OBJECT (trans, "inplace reuse writable input buffer");
1682 GST_DEBUG_OBJECT (trans, "making writable buffer copy");
1683 /* we make a copy of the input buffer */
1684 *outbuf = gst_buffer_copy (inbuf);
1689 /* else use the transform function to get the size */
1690 incaps = gst_pad_get_current_caps (trans->sinkpad);
1691 outcaps = gst_pad_get_current_caps (trans->srcpad);
1693 /* srcpad might be flushing already if we're being shut down */
1694 if (outcaps == NULL)
1697 GST_DEBUG_OBJECT (trans, "getting output size for alloc");
1698 /* copy transform, figure out the output size */
1699 insize = gst_buffer_get_size (inbuf);
1700 res = gst_base_transform_transform_size (trans,
1701 GST_PAD_SINK, incaps, insize, outcaps, &outsize);
1703 gst_caps_unref (incaps);
1704 gst_caps_unref (outcaps);
1709 GST_DEBUG_OBJECT (trans, "doing alloc of size %" G_GSIZE_FORMAT, outsize);
1710 *outbuf = gst_buffer_new_allocate (priv->allocator, outsize, &priv->params);
1712 ret = GST_FLOW_ERROR;
1717 /* copy the metadata */
1718 if (bclass->copy_metadata)
1719 if (!bclass->copy_metadata (trans, inbuf, *outbuf)) {
1720 /* something failed, post a warning */
1721 GST_ELEMENT_WARNING (trans, STREAM, NOT_IMPLEMENTED,
1722 ("could not copy metadata"), (NULL));
1731 GST_ELEMENT_ERROR (trans, RESOURCE, SETTINGS,
1732 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
1733 return GST_FLOW_ERROR;
1737 GST_ERROR_OBJECT (trans, "unknown output size");
1738 return GST_FLOW_ERROR;
1742 GST_DEBUG_OBJECT (trans, "could not allocate buffer from pool");
1747 GST_DEBUG_OBJECT (trans, "no output caps, source pad has been deactivated");
1748 gst_caps_unref (incaps);
1749 return GST_FLOW_FLUSHING;
1755 GstBaseTransform *trans;
1760 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
1762 CopyMetaData *data = user_data;
1763 GstBaseTransform *trans = data->trans;
1764 GstBaseTransformClass *klass;
1765 const GstMetaInfo *info = (*meta)->info;
1766 GstBuffer *outbuf = data->outbuf;
1767 gboolean do_copy = FALSE;
1769 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1771 if (gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory)) {
1772 /* never call the transform_meta with memory specific metadata */
1773 GST_DEBUG_OBJECT (trans, "not copying memory specific metadata %s",
1774 g_type_name (info->api));
1776 } else if (klass->transform_meta) {
1777 do_copy = klass->transform_meta (trans, outbuf, *meta, inbuf);
1778 GST_DEBUG_OBJECT (trans, "transformed metadata %s: copy: %d",
1779 g_type_name (info->api), do_copy);
1782 /* we only copy metadata when the subclass implemented a transform_meta
1783 * function and when it returns %TRUE */
1785 GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
1786 GST_DEBUG_OBJECT (trans, "copy metadata %s", g_type_name (info->api));
1787 /* simply copy then */
1788 info->transform_func (outbuf, *meta, inbuf,
1789 _gst_meta_transform_copy, ©_data);
1795 default_copy_metadata (GstBaseTransform * trans,
1796 GstBuffer * inbuf, GstBuffer * outbuf)
1798 GstBaseTransformPrivate *priv = trans->priv;
1801 /* now copy the metadata */
1802 GST_DEBUG_OBJECT (trans, "copying metadata");
1804 /* this should not happen, buffers allocated from a pool or with
1805 * new_allocate should always be writable. */
1806 if (!gst_buffer_is_writable (outbuf))
1809 /* when we get here, the metadata should be writable */
1810 gst_buffer_copy_into (outbuf, inbuf,
1811 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1813 /* clear the GAP flag when the subclass does not understand it */
1814 if (!priv->gap_aware)
1815 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
1819 data.outbuf = outbuf;
1821 gst_buffer_foreach_meta (inbuf, foreach_metadata, &data);
1828 GST_WARNING_OBJECT (trans, "buffer %p not writable", outbuf);
1833 /* Given @caps calcultate the size of one unit.
1835 * For video caps, this is the size of one frame (and thus one buffer).
1836 * For audio caps, this is the size of one sample.
1838 * These values are cached since they do not change and the calculation
1839 * potentially involves parsing caps and other expensive stuff.
1841 * We have two cache locations to store the size, one for the source caps
1842 * and one for the sink caps.
1844 * this function returns %FALSE if no size could be calculated.
1847 gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
1850 gboolean res = FALSE;
1851 GstBaseTransformClass *bclass;
1852 GstBaseTransformPrivate *priv = trans->priv;
1854 /* see if we have the result cached */
1855 if (priv->cache_caps1 == caps) {
1856 *size = priv->cache_caps1_size;
1857 GST_DEBUG_OBJECT (trans,
1858 "returned %" G_GSIZE_FORMAT " from first cache", *size);
1861 if (priv->cache_caps2 == caps) {
1862 *size = priv->cache_caps2_size;
1863 GST_DEBUG_OBJECT (trans,
1864 "returned %" G_GSIZE_FORMAT " from second cached", *size);
1868 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1869 res = bclass->get_unit_size (trans, caps, size);
1870 GST_DEBUG_OBJECT (trans,
1871 "caps %" GST_PTR_FORMAT ") has unit size %" G_GSIZE_FORMAT ", res %s",
1872 caps, *size, res ? "TRUE" : "FALSE");
1875 /* and cache the values */
1876 if (priv->cache_caps1 == NULL) {
1877 gst_caps_replace (&priv->cache_caps1, caps);
1878 priv->cache_caps1_size = *size;
1879 GST_DEBUG_OBJECT (trans,
1880 "caching %" G_GSIZE_FORMAT " in first cache", *size);
1881 } else if (priv->cache_caps2 == NULL) {
1882 gst_caps_replace (&priv->cache_caps2, caps);
1883 priv->cache_caps2_size = *size;
1884 GST_DEBUG_OBJECT (trans,
1885 "caching %" G_GSIZE_FORMAT " in second cache", *size);
1887 GST_DEBUG_OBJECT (trans, "no free spot to cache unit_size");
1894 gst_base_transform_sink_event (GstPad * pad, GstObject * parent,
1897 GstBaseTransform *trans;
1898 GstBaseTransformClass *bclass;
1899 gboolean ret = TRUE;
1901 trans = GST_BASE_TRANSFORM (parent);
1902 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1904 if (bclass->sink_event)
1905 ret = bclass->sink_event (trans, event);
1907 gst_event_unref (event);
1913 gst_base_transform_sink_eventfunc (GstBaseTransform * trans, GstEvent * event)
1915 gboolean ret = TRUE, forward = TRUE;
1916 GstBaseTransformPrivate *priv = trans->priv;
1918 switch (GST_EVENT_TYPE (event)) {
1919 case GST_EVENT_FLUSH_START:
1921 case GST_EVENT_FLUSH_STOP:
1922 GST_OBJECT_LOCK (trans);
1923 /* reset QoS parameters */
1924 priv->proportion = 1.0;
1925 priv->earliest_time = -1;
1926 priv->discont = FALSE;
1927 priv->processed = 0;
1929 GST_OBJECT_UNLOCK (trans);
1930 /* we need new segment info after the flush. */
1931 trans->have_segment = FALSE;
1932 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
1933 priv->position_out = GST_CLOCK_TIME_NONE;
1939 case GST_EVENT_CAPS:
1943 gst_event_parse_caps (event, &caps);
1944 /* clear any pending reconfigure flag */
1945 gst_pad_check_reconfigure (trans->srcpad);
1946 ret = gst_base_transform_setcaps (trans, trans->sinkpad, caps);
1948 gst_pad_mark_reconfigure (trans->srcpad);
1953 case GST_EVENT_SEGMENT:
1955 gst_event_copy_segment (event, &trans->segment);
1956 trans->have_segment = TRUE;
1958 GST_DEBUG_OBJECT (trans, "received SEGMENT %" GST_SEGMENT_FORMAT,
1967 ret = gst_pad_push_event (trans->srcpad, event);
1969 gst_event_unref (event);
1975 gst_base_transform_src_event (GstPad * pad, GstObject * parent,
1978 GstBaseTransform *trans;
1979 GstBaseTransformClass *bclass;
1980 gboolean ret = TRUE;
1982 trans = GST_BASE_TRANSFORM (parent);
1983 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1985 if (bclass->src_event)
1986 ret = bclass->src_event (trans, event);
1988 gst_event_unref (event);
1994 gst_base_transform_src_eventfunc (GstBaseTransform * trans, GstEvent * event)
1998 GST_DEBUG_OBJECT (trans, "handling event %p %" GST_PTR_FORMAT, event, event);
2000 switch (GST_EVENT_TYPE (event)) {
2001 case GST_EVENT_SEEK:
2003 case GST_EVENT_NAVIGATION:
2008 GstClockTimeDiff diff;
2009 GstClockTime timestamp;
2011 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
2012 gst_base_transform_update_qos (trans, proportion, diff, timestamp);
2019 ret = gst_pad_push_event (trans->sinkpad, event);
2024 /* Takes the input buffer */
2025 static GstFlowReturn
2026 default_submit_input_buffer (GstBaseTransform * trans, gboolean is_discont,
2029 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2030 GstBaseTransformPrivate *priv = trans->priv;
2031 GstFlowReturn ret = GST_FLOW_OK;
2032 GstClockTime running_time;
2033 GstClockTime timestamp;
2035 if (G_UNLIKELY (!gst_base_transform_reconfigure (trans)))
2036 goto not_negotiated;
2038 if (GST_BUFFER_OFFSET_IS_VALID (inbuf))
2039 GST_DEBUG_OBJECT (trans,
2040 "handling buffer %p of size %" G_GSIZE_FORMAT ", PTS %" GST_TIME_FORMAT
2041 " and offset %" G_GUINT64_FORMAT, inbuf, gst_buffer_get_size (inbuf),
2042 GST_TIME_ARGS (GST_BUFFER_PTS (inbuf)), GST_BUFFER_OFFSET (inbuf));
2044 GST_DEBUG_OBJECT (trans,
2045 "handling buffer %p of size %" G_GSIZE_FORMAT ", PTS %" GST_TIME_FORMAT
2046 " and offset NONE", inbuf, gst_buffer_get_size (inbuf),
2047 GST_TIME_ARGS (GST_BUFFER_PTS (inbuf)));
2049 /* Don't allow buffer handling before negotiation, except in passthrough mode
2050 * or if the class doesn't implement a set_caps function (in which case it doesn't
2053 if (!priv->negotiated && !priv->passthrough && (bclass->set_caps != NULL))
2054 goto not_negotiated;
2056 /* can only do QoS if the segment is in TIME */
2057 if (trans->segment.format != GST_FORMAT_TIME)
2060 /* QOS is done on the running time of the buffer, get it now */
2061 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
2062 running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
2065 if (running_time != -1) {
2067 GstClockTime earliest_time;
2070 /* lock for getting the QoS parameters that are set (in a different thread)
2071 * with the QOS events */
2072 GST_OBJECT_LOCK (trans);
2073 earliest_time = priv->earliest_time;
2074 proportion = priv->proportion;
2075 /* check for QoS, don't perform conversion for buffers
2076 * that are known to be late. */
2077 need_skip = priv->qos_enabled &&
2078 earliest_time != -1 && running_time <= earliest_time;
2079 GST_OBJECT_UNLOCK (trans);
2082 GstMessage *qos_msg;
2083 GstClockTime duration;
2084 guint64 stream_time;
2087 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "skipping transform: qostime %"
2088 GST_TIME_FORMAT " <= %" GST_TIME_FORMAT,
2089 GST_TIME_ARGS (running_time), GST_TIME_ARGS (earliest_time));
2093 duration = GST_BUFFER_DURATION (inbuf);
2095 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
2097 jitter = GST_CLOCK_DIFF (running_time, earliest_time);
2100 gst_message_new_qos (GST_OBJECT_CAST (trans), FALSE, running_time,
2101 stream_time, timestamp, duration);
2102 gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
2103 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
2104 priv->processed, priv->dropped);
2105 gst_element_post_message (GST_ELEMENT_CAST (trans), qos_msg);
2107 /* mark discont for next buffer */
2108 priv->discont = TRUE;
2109 ret = GST_BASE_TRANSFORM_FLOW_DROPPED;
2115 /* Stash input buffer where the default generate_output
2116 * function can find it */
2117 if (trans->queued_buf)
2118 gst_buffer_unref (trans->queued_buf);
2119 trans->queued_buf = inbuf;
2122 gst_buffer_unref (inbuf);
2127 gst_buffer_unref (inbuf);
2128 if (GST_PAD_IS_FLUSHING (trans->srcpad))
2129 return GST_FLOW_FLUSHING;
2130 return GST_FLOW_NOT_NEGOTIATED;
2134 static GstFlowReturn
2135 default_generate_output (GstBaseTransform * trans, GstBuffer ** outbuf)
2137 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2138 GstBaseTransformPrivate *priv = trans->priv;
2139 GstFlowReturn ret = GST_FLOW_OK;
2141 gboolean want_in_place;
2143 /* Retrieve stashed input buffer, if the default submit_input_buffer
2144 * was run. Takes ownership back from there */
2145 inbuf = trans->queued_buf;
2146 trans->queued_buf = NULL;
2148 /* This default processing method needs one input buffer to feed to
2149 * the transform functions, we can't do anything without it */
2153 /* first try to allocate an output buffer based on the currently negotiated
2154 * format. outbuf will contain a buffer suitable for doing the configured
2155 * transform after this function. */
2156 if (bclass->prepare_output_buffer == NULL)
2159 GST_DEBUG_OBJECT (trans, "calling prepare buffer");
2160 ret = bclass->prepare_output_buffer (trans, inbuf, outbuf);
2162 if (ret != GST_FLOW_OK || *outbuf == NULL)
2165 GST_DEBUG_OBJECT (trans, "using allocated buffer in %p, out %p", inbuf,
2168 /* now perform the needed transform */
2169 if (priv->passthrough) {
2170 /* In passthrough mode, give transform_ip a look at the
2171 * buffer, without making it writable, or just push the
2173 if (bclass->transform_ip_on_passthrough && bclass->transform_ip) {
2174 GST_DEBUG_OBJECT (trans, "doing passthrough transform_ip");
2175 ret = bclass->transform_ip (trans, *outbuf);
2177 GST_DEBUG_OBJECT (trans, "element is in passthrough");
2180 want_in_place = (bclass->transform_ip != NULL) && priv->always_in_place;
2182 if (want_in_place) {
2183 GST_DEBUG_OBJECT (trans, "doing inplace transform");
2184 ret = bclass->transform_ip (trans, *outbuf);
2186 GST_DEBUG_OBJECT (trans, "doing non-inplace transform");
2188 if (bclass->transform)
2189 ret = bclass->transform (trans, inbuf, *outbuf);
2191 ret = GST_FLOW_NOT_SUPPORTED;
2195 /* only unref input buffer if we allocated a new outbuf buffer. If we reused
2196 * the input buffer, no refcount is changed to keep the input buffer writable
2198 if (*outbuf != inbuf)
2199 gst_buffer_unref (inbuf);
2206 gst_buffer_unref (inbuf);
2207 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
2208 ("Sub-class has no prepare_output_buffer implementation"), (NULL));
2209 return GST_FLOW_NOT_SUPPORTED;
2213 gst_buffer_unref (inbuf);
2215 GST_WARNING_OBJECT (trans, "could not get buffer from pool: %s",
2216 gst_flow_get_name (ret));
2221 /* FIXME, getrange is broken, need to pull range from the other
2222 * end based on the transform_size result.
2224 static GstFlowReturn
2225 gst_base_transform_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2226 guint length, GstBuffer ** buffer)
2228 GstBaseTransformClass *klass = GST_BASE_TRANSFORM_GET_CLASS (parent);
2229 GstBaseTransform *trans = GST_BASE_TRANSFORM (parent);
2230 GstBaseTransformPrivate *priv = trans->priv;
2232 GstBuffer *inbuf = NULL;
2233 GstBuffer *outbuf = NULL;
2235 /* Try and generate a buffer, if the sub-class wants more data,
2236 * pull some and repeat until a buffer (or error) is produced */
2238 ret = klass->generate_output (trans, &outbuf);
2240 /* Consume the DROPPED return value and go get more data */
2241 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED)
2244 if (ret != GST_FLOW_OK || outbuf != NULL)
2247 /* No buffer generated, try and pull data */
2248 ret = gst_pad_pull_range (trans->sinkpad, offset, length, &inbuf);
2249 if (G_UNLIKELY (ret != GST_FLOW_OK))
2252 if (klass->before_transform)
2253 klass->before_transform (trans, inbuf);
2255 /* Set discont flag so we can mark the next outgoing buffer */
2256 if (GST_BUFFER_IS_DISCONT (inbuf)) {
2257 GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", inbuf);
2258 priv->discont = TRUE;
2261 /* FIXME: Input offsets and lengths need to be translated, as per
2262 * the FIXME above. For now, just advance somewhat */
2263 offset += gst_buffer_get_size (inbuf);
2265 ret = klass->submit_input_buffer (trans, priv->discont, inbuf);
2266 if (ret != GST_FLOW_OK) {
2267 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED)
2271 } while (ret == GST_FLOW_OK && outbuf == NULL);
2275 /* apply DISCONT flag if the buffer is not yet marked as such */
2276 if (priv->discont) {
2277 GST_DEBUG_OBJECT (trans, "we have a pending DISCONT");
2278 if (!GST_BUFFER_IS_DISCONT (outbuf)) {
2279 GST_DEBUG_OBJECT (trans, "marking DISCONT on output buffer");
2280 outbuf = gst_buffer_make_writable (outbuf);
2281 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2283 priv->discont = FALSE;
2293 GST_DEBUG_OBJECT (trans, "failed to pull a buffer: %s",
2294 gst_flow_get_name (ret));
2299 /* The flow of the chain function is the reverse of the
2300 * getrange() function - we have data, feed it to the sub-class
2301 * and then iterate, pushing buffers it generates until it either
2302 * wants more data or returns an error */
2303 static GstFlowReturn
2304 gst_base_transform_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2306 GstBaseTransform *trans = GST_BASE_TRANSFORM (parent);
2307 GstBaseTransformClass *klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2308 GstBaseTransformPrivate *priv = trans->priv;
2310 GstClockTime position = GST_CLOCK_TIME_NONE;
2311 GstClockTime timestamp, duration;
2312 GstBuffer *outbuf = NULL;
2314 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2315 duration = GST_BUFFER_DURATION (buffer);
2317 /* calculate end position of the incoming buffer */
2318 if (timestamp != GST_CLOCK_TIME_NONE) {
2319 if (duration != GST_CLOCK_TIME_NONE)
2320 position = timestamp + duration;
2322 position = timestamp;
2325 if (klass->before_transform)
2326 klass->before_transform (trans, buffer);
2328 /* Set discont flag so we can mark the outgoing buffer */
2329 if (GST_BUFFER_IS_DISCONT (buffer)) {
2330 GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", buffer);
2331 priv->discont = TRUE;
2334 /* Takes ownership of input buffer */
2335 ret = klass->submit_input_buffer (trans, priv->discont, buffer);
2336 if (ret != GST_FLOW_OK)
2342 ret = klass->generate_output (trans, &outbuf);
2344 /* outbuf can be NULL, this means a dropped buffer, if we have a buffer but
2345 * GST_BASE_TRANSFORM_FLOW_DROPPED we will not push either. */
2346 if (outbuf != NULL) {
2347 if (ret == GST_FLOW_OK) {
2348 GstClockTime position_out = GST_CLOCK_TIME_NONE;
2350 /* Remember last stop position */
2351 if (position != GST_CLOCK_TIME_NONE &&
2352 trans->segment.format == GST_FORMAT_TIME)
2353 trans->segment.position = position;
2355 if (GST_BUFFER_TIMESTAMP_IS_VALID (outbuf)) {
2356 position_out = GST_BUFFER_TIMESTAMP (outbuf);
2357 if (GST_BUFFER_DURATION_IS_VALID (outbuf))
2358 position_out += GST_BUFFER_DURATION (outbuf);
2359 } else if (position != GST_CLOCK_TIME_NONE) {
2360 position_out = position;
2362 if (position_out != GST_CLOCK_TIME_NONE
2363 && trans->segment.format == GST_FORMAT_TIME)
2364 priv->position_out = position_out;
2366 /* apply DISCONT flag if the buffer is not yet marked as such */
2367 if (trans->priv->discont) {
2368 GST_DEBUG_OBJECT (trans, "we have a pending DISCONT");
2369 if (!GST_BUFFER_IS_DISCONT (outbuf)) {
2370 GST_DEBUG_OBJECT (trans, "marking DISCONT on output buffer");
2371 outbuf = gst_buffer_make_writable (outbuf);
2372 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2374 priv->discont = FALSE;
2378 ret = gst_pad_push (trans->srcpad, outbuf);
2380 GST_DEBUG_OBJECT (trans, "we got return %s", gst_flow_get_name (ret));
2381 gst_buffer_unref (outbuf);
2384 } while (ret == GST_FLOW_OK && outbuf != NULL);
2387 /* convert internal flow to OK and mark discont for the next buffer. */
2388 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED) {
2389 GST_DEBUG_OBJECT (trans, "dropped a buffer, marking DISCONT");
2390 priv->discont = TRUE;
2398 gst_base_transform_set_property (GObject * object, guint prop_id,
2399 const GValue * value, GParamSpec * pspec)
2401 GstBaseTransform *trans;
2403 trans = GST_BASE_TRANSFORM (object);
2407 gst_base_transform_set_qos_enabled (trans, g_value_get_boolean (value));
2410 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2416 gst_base_transform_get_property (GObject * object, guint prop_id,
2417 GValue * value, GParamSpec * pspec)
2419 GstBaseTransform *trans;
2421 trans = GST_BASE_TRANSFORM (object);
2425 g_value_set_boolean (value, gst_base_transform_is_qos_enabled (trans));
2428 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2433 /* not a vmethod of anything, just an internal method */
2435 gst_base_transform_activate (GstBaseTransform * trans, gboolean active)
2437 GstBaseTransformClass *bclass;
2438 GstBaseTransformPrivate *priv = trans->priv;
2439 gboolean result = TRUE;
2441 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2444 GstCaps *incaps, *outcaps;
2446 if (priv->pad_mode == GST_PAD_MODE_NONE && bclass->start)
2447 result &= bclass->start (trans);
2449 incaps = gst_pad_get_current_caps (trans->sinkpad);
2450 outcaps = gst_pad_get_current_caps (trans->srcpad);
2452 GST_OBJECT_LOCK (trans);
2453 if (incaps && outcaps)
2454 priv->have_same_caps =
2455 gst_caps_is_equal (incaps, outcaps) || priv->passthrough;
2457 priv->have_same_caps = priv->passthrough;
2458 GST_DEBUG_OBJECT (trans, "have_same_caps %d", priv->have_same_caps);
2459 priv->negotiated = FALSE;
2460 trans->have_segment = FALSE;
2461 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
2462 priv->position_out = GST_CLOCK_TIME_NONE;
2463 priv->proportion = 1.0;
2464 priv->earliest_time = -1;
2465 priv->discont = FALSE;
2466 priv->processed = 0;
2468 GST_OBJECT_UNLOCK (trans);
2471 gst_caps_unref (incaps);
2473 gst_caps_unref (outcaps);
2475 /* We must make sure streaming has finished before resetting things
2476 * and calling the ::stop vfunc */
2477 GST_PAD_STREAM_LOCK (trans->sinkpad);
2478 GST_PAD_STREAM_UNLOCK (trans->sinkpad);
2480 priv->have_same_caps = FALSE;
2481 /* We can only reset the passthrough mode if the instance told us to
2482 handle it in configure_caps */
2483 if (bclass->passthrough_on_same_caps) {
2484 gst_base_transform_set_passthrough (trans, FALSE);
2486 gst_caps_replace (&priv->cache_caps1, NULL);
2487 gst_caps_replace (&priv->cache_caps2, NULL);
2489 /* Make sure any left over buffer is freed */
2490 gst_buffer_replace (&trans->queued_buf, NULL);
2492 if (priv->pad_mode != GST_PAD_MODE_NONE && bclass->stop)
2493 result &= bclass->stop (trans);
2495 gst_base_transform_set_allocation (trans, NULL, NULL, NULL, NULL);
2502 gst_base_transform_sink_activate_mode (GstPad * pad, GstObject * parent,
2503 GstPadMode mode, gboolean active)
2505 gboolean result = FALSE;
2506 GstBaseTransform *trans;
2508 trans = GST_BASE_TRANSFORM (parent);
2511 case GST_PAD_MODE_PUSH:
2513 result = gst_base_transform_activate (trans, active);
2516 trans->priv->pad_mode = active ? GST_PAD_MODE_PUSH : GST_PAD_MODE_NONE;
2528 gst_base_transform_src_activate_mode (GstPad * pad, GstObject * parent,
2529 GstPadMode mode, gboolean active)
2531 gboolean result = FALSE;
2532 GstBaseTransform *trans;
2534 trans = GST_BASE_TRANSFORM (parent);
2537 case GST_PAD_MODE_PULL:
2540 gst_pad_activate_mode (trans->sinkpad, GST_PAD_MODE_PULL, active);
2543 result &= gst_base_transform_activate (trans, active);
2546 trans->priv->pad_mode = active ? mode : GST_PAD_MODE_NONE;
2558 * gst_base_transform_set_passthrough:
2559 * @trans: the #GstBaseTransform to set
2560 * @passthrough: boolean indicating passthrough mode.
2562 * Set passthrough mode for this filter by default. This is mostly
2563 * useful for filters that do not care about negotiation.
2565 * Always %TRUE for filters which don't implement either a transform
2566 * or transform_ip method.
2571 gst_base_transform_set_passthrough (GstBaseTransform * trans,
2572 gboolean passthrough)
2574 GstBaseTransformClass *bclass;
2576 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2578 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2580 GST_OBJECT_LOCK (trans);
2582 if (bclass->transform_ip || bclass->transform)
2583 trans->priv->passthrough = FALSE;
2585 trans->priv->passthrough = TRUE;
2588 GST_DEBUG_OBJECT (trans, "set passthrough %d", trans->priv->passthrough);
2589 GST_OBJECT_UNLOCK (trans);
2593 * gst_base_transform_is_passthrough:
2594 * @trans: the #GstBaseTransform to query
2596 * See if @trans is configured as a passthrough transform.
2598 * Returns: %TRUE is the transform is configured in passthrough mode.
2603 gst_base_transform_is_passthrough (GstBaseTransform * trans)
2607 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2609 GST_OBJECT_LOCK (trans);
2610 result = trans->priv->passthrough;
2611 GST_OBJECT_UNLOCK (trans);
2617 * gst_base_transform_set_in_place:
2618 * @trans: the #GstBaseTransform to modify
2619 * @in_place: Boolean value indicating that we would like to operate
2620 * on in_place buffers.
2622 * Determines whether a non-writable buffer will be copied before passing
2623 * to the transform_ip function.
2625 * <listitem>Always %TRUE if no transform function is implemented.</listitem>
2626 * <listitem>Always %FALSE if ONLY transform function is implemented.</listitem>
2632 gst_base_transform_set_in_place (GstBaseTransform * trans, gboolean in_place)
2634 GstBaseTransformClass *bclass;
2636 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2638 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2640 GST_OBJECT_LOCK (trans);
2643 if (bclass->transform_ip) {
2644 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
2645 trans->priv->always_in_place = TRUE;
2648 if (bclass->transform) {
2649 GST_DEBUG_OBJECT (trans, "setting in_place FALSE");
2650 trans->priv->always_in_place = FALSE;
2654 GST_OBJECT_UNLOCK (trans);
2658 * gst_base_transform_is_in_place:
2659 * @trans: the #GstBaseTransform to query
2661 * See if @trans is configured as a in_place transform.
2663 * Returns: %TRUE is the transform is configured in in_place mode.
2668 gst_base_transform_is_in_place (GstBaseTransform * trans)
2672 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2674 GST_OBJECT_LOCK (trans);
2675 result = trans->priv->always_in_place;
2676 GST_OBJECT_UNLOCK (trans);
2682 * gst_base_transform_update_qos:
2683 * @trans: a #GstBaseTransform
2684 * @proportion: the proportion
2685 * @diff: the diff against the clock
2686 * @timestamp: the timestamp of the buffer generating the QoS expressed in
2689 * Set the QoS parameters in the transform. This function is called internally
2690 * when a QOS event is received but subclasses can provide custom information
2696 gst_base_transform_update_qos (GstBaseTransform * trans,
2697 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2699 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2701 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans,
2702 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2703 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2705 GST_OBJECT_LOCK (trans);
2706 trans->priv->proportion = proportion;
2707 trans->priv->earliest_time = timestamp + diff;
2708 GST_OBJECT_UNLOCK (trans);
2712 * gst_base_transform_set_qos_enabled:
2713 * @trans: a #GstBaseTransform
2714 * @enabled: new state
2716 * Enable or disable QoS handling in the transform.
2721 gst_base_transform_set_qos_enabled (GstBaseTransform * trans, gboolean enabled)
2723 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2725 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "enabled: %d", enabled);
2727 GST_OBJECT_LOCK (trans);
2728 trans->priv->qos_enabled = enabled;
2729 GST_OBJECT_UNLOCK (trans);
2733 * gst_base_transform_is_qos_enabled:
2734 * @trans: a #GstBaseTransform
2736 * Queries if the transform will handle QoS.
2738 * Returns: %TRUE if QoS is enabled.
2743 gst_base_transform_is_qos_enabled (GstBaseTransform * trans)
2747 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2749 GST_OBJECT_LOCK (trans);
2750 result = trans->priv->qos_enabled;
2751 GST_OBJECT_UNLOCK (trans);
2757 * gst_base_transform_set_gap_aware:
2758 * @trans: a #GstBaseTransform
2759 * @gap_aware: New state
2761 * If @gap_aware is %FALSE (the default), output buffers will have the
2762 * %GST_BUFFER_FLAG_GAP flag unset.
2764 * If set to %TRUE, the element must handle output buffers with this flag set
2765 * correctly, i.e. it can assume that the buffer contains neutral data but must
2766 * unset the flag if the output is no neutral data.
2771 gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware)
2773 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2775 GST_OBJECT_LOCK (trans);
2776 trans->priv->gap_aware = gap_aware;
2777 GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware);
2778 GST_OBJECT_UNLOCK (trans);
2782 * gst_base_transform_set_prefer_passthrough:
2783 * @trans: a #GstBaseTransform
2784 * @prefer_passthrough: New state
2786 * If @prefer_passthrough is %TRUE (the default), @trans will check and
2787 * prefer passthrough caps from the list of caps returned by the
2788 * transform_caps vmethod.
2790 * If set to %FALSE, the element must order the caps returned from the
2791 * transform_caps function in such a way that the preferred format is
2792 * first in the list. This can be interesting for transforms that can do
2793 * passthrough transforms but prefer to do something else, like a
2801 gst_base_transform_set_prefer_passthrough (GstBaseTransform * trans,
2802 gboolean prefer_passthrough)
2804 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2806 GST_OBJECT_LOCK (trans);
2807 trans->priv->prefer_passthrough = prefer_passthrough;
2808 GST_DEBUG_OBJECT (trans, "prefer passthrough %d", prefer_passthrough);
2809 GST_OBJECT_UNLOCK (trans);
2813 * gst_base_transform_reconfigure_sink:
2814 * @trans: a #GstBaseTransform
2816 * Instructs @trans to request renegotiation upstream. This function is
2817 * typically called after properties on the transform were set that
2818 * influence the input format.
2821 gst_base_transform_reconfigure_sink (GstBaseTransform * trans)
2823 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2825 /* push the renegotiate event */
2826 if (!gst_pad_push_event (GST_BASE_TRANSFORM_SINK_PAD (trans),
2827 gst_event_new_reconfigure ()))
2828 GST_DEBUG_OBJECT (trans, "Renegotiate event wasn't handled");
2832 * gst_base_transform_reconfigure_src:
2833 * @trans: a #GstBaseTransform
2835 * Instructs @trans to renegotiate a new downstream transform on the next
2836 * buffer. This function is typically called after properties on the transform
2837 * were set that influence the output format.
2840 gst_base_transform_reconfigure_src (GstBaseTransform * trans)
2842 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2844 gst_pad_mark_reconfigure (trans->srcpad);
2848 * gst_base_transform_get_buffer_pool:
2849 * @trans: a #GstBaseTransform
2851 * Returns: (transfer full): the instance of the #GstBufferPool used
2852 * by @trans; free it after use it
2855 gst_base_transform_get_buffer_pool (GstBaseTransform * trans)
2857 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), NULL);
2859 if (trans->priv->pool)
2860 return gst_object_ref (trans->priv->pool);
2866 * gst_base_transform_get_allocator:
2867 * @trans: a #GstBaseTransform
2868 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
2870 * @params: (out) (allow-none) (transfer full): the
2871 * #GstAllocationParams of @allocator
2873 * Lets #GstBaseTransform sub-classes to know the memory @allocator
2874 * used by the base class and its @params.
2876 * Unref the @allocator after use it.
2879 gst_base_transform_get_allocator (GstBaseTransform * trans,
2880 GstAllocator ** allocator, GstAllocationParams * params)
2882 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2885 *allocator = trans->priv->allocator ?
2886 gst_object_ref (trans->priv->allocator) : NULL;
2889 *params = trans->priv->params;
2893 * gst_base_transform_update_src_caps:
2894 * @trans: a #GstBaseTransform
2895 * @updated_caps: An updated version of the srcpad caps to be pushed
2898 * Updates the srcpad caps and send the caps downstream. This function
2899 * can be used by subclasses when they have already negotiated their caps
2900 * but found a change in them (or computed new informations). This way,
2901 * they can notify downstream about that change without loosing any
2904 * Returns: %TRUE if the caps could be send downstream %FALSE otherwise
2909 gst_base_transform_update_src_caps (GstBaseTransform * trans,
2910 GstCaps * updated_caps)
2912 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2914 if (gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (trans),
2915 gst_event_new_caps (updated_caps))) {
2916 gst_pad_mark_reconfigure (trans->srcpad);