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 * @title: GstBaseTransform
27 * @short_description: Base class for simple transform filters
28 * @see_also: #GstBaseSrc, #GstBaseSink
30 * This base class is for filter elements that process data. Elements
31 * that are suitable for implementation using #GstBaseTransform are ones
32 * where the size and caps of the output is known entirely from the input
33 * caps and buffer sizes. These include elements that directly transform
34 * one buffer into another, modify the contents of a buffer in-place, as
35 * well as elements that collate multiple input buffers into one output buffer,
36 * or that expand one input buffer into multiple output buffers. See below
37 * for more concrete use cases.
41 * * one sinkpad and one srcpad
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 * * Handles state changes
49 * * Pull mode if the sub-class transform can operate on arbitrary data
55 * * Element has no interest in modifying the buffer. It may want to inspect it,
56 * in which case the element should have a transform_ip function. If there
57 * is no transform_ip function in passthrough mode, the buffer is pushed
60 * * The #GstBaseTransformClass.passthrough_on_same_caps variable
61 * will automatically set/unset passthrough based on whether the
62 * element negotiates the same caps on both pads.
64 * * #GstBaseTransformClass.passthrough_on_same_caps on an element that
65 * doesn't implement a transform_caps function is useful for elements that
66 * only inspect data (such as level)
71 * * Videoscale, audioconvert, videoconvert, audioresample in certain modes.
73 * ## Modifications in-place - input buffer and output buffer are the same thing.
75 * * The element must implement a transform_ip function.
76 * * Output buffer size must <= input buffer size
77 * * If the always_in_place flag is set, non-writable buffers will be copied
78 * and passed to the transform_ip function, otherwise a new buffer will be
79 * created and the transform function called.
81 * * Incoming writable buffers will be passed to the transform_ip function
83 * * only implementing transform_ip and not transform implies always_in_place = %TRUE
87 * * Audioconvert in certain modes (signed/unsigned conversion)
88 * * videoconvert in certain modes (endianness swapping)
90 * ## Modifications only to the caps/metadata of a buffer
92 * * The element does not require writable data, but non-writable buffers
93 * should be subbuffered so that the meta-information can be replaced.
95 * * Elements wishing to operate in this mode should replace the
96 * prepare_output_buffer method to create subbuffers of the input buffer
97 * and set always_in_place to %TRUE
100 * * Capsfilter when setting caps on outgoing buffers that have
102 * * identity when it is going to re-timestamp buffers by
106 * * always_in_place flag is not set, or there is no transform_ip function
107 * * Element will receive an input buffer and output buffer to operate on.
108 * * Output buffer is allocated by calling the prepare_output_buffer function.
109 * * Example elements:
110 * * Videoscale, videoconvert, audioconvert when doing
111 * scaling/conversions
113 * ## Special output buffer allocations
114 * * Elements which need to do special allocation of their output buffers
115 * beyond allocating output buffers via the negotiated allocator or
116 * buffer pool should implement the prepare_output_buffer method.
118 * * Example elements:
121 * # Sub-class settable flags on GstBaseTransform
125 * * Implies that in the current configuration, the sub-class is not interested in modifying the buffers.
126 * * Elements which are always in passthrough mode whenever the same caps has been negotiated on both pads can set the class variable passthrough_on_same_caps to have this behaviour automatically.
129 * * Determines whether a non-writable buffer will be copied before passing
130 * to the transform_ip function.
132 * * Implied %TRUE if no transform function is implemented.
133 * * Implied %FALSE if ONLY transform function is implemented.
143 #include "../../../gst/gst_private.h"
144 #include "../../../gst/gst-i18n-lib.h"
145 #include "../../../gst/glib-compat-private.h"
146 #include "gstbasetransform.h"
148 GST_DEBUG_CATEGORY_STATIC (gst_base_transform_debug);
149 #define GST_CAT_DEFAULT gst_base_transform_debug
151 /* BaseTransform signals and args */
158 #define DEFAULT_PROP_QOS FALSE
166 struct _GstBaseTransformPrivate
168 /* Set by sub-class */
169 gboolean passthrough;
170 gboolean always_in_place;
172 GstCaps *cache_caps1;
173 gsize cache_caps1_size;
174 GstCaps *cache_caps2;
175 gsize cache_caps2_size;
176 gboolean have_same_caps;
180 /* QoS *//* with LOCK */
181 gboolean qos_enabled;
183 GstClockTime earliest_time;
184 /* previous buffer had a discont */
190 gboolean prefer_passthrough;
196 GstClockTime position_out;
199 gboolean pool_active;
200 GstAllocator *allocator;
201 GstAllocationParams params;
206 static GstElementClass *parent_class = NULL;
207 static gint private_offset = 0;
209 static void gst_base_transform_class_init (GstBaseTransformClass * klass);
210 static void gst_base_transform_init (GstBaseTransform * trans,
211 GstBaseTransformClass * klass);
212 static GstFlowReturn default_submit_input_buffer (GstBaseTransform * trans,
213 gboolean is_discont, GstBuffer * input);
214 static GstFlowReturn default_generate_output (GstBaseTransform * trans,
215 GstBuffer ** outbuf);
217 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
218 * method to get to the padtemplates */
220 gst_base_transform_get_type (void)
222 static gsize base_transform_type = 0;
224 if (g_once_init_enter (&base_transform_type)) {
226 static const GTypeInfo base_transform_info = {
227 sizeof (GstBaseTransformClass),
230 (GClassInitFunc) gst_base_transform_class_init,
233 sizeof (GstBaseTransform),
235 (GInstanceInitFunc) gst_base_transform_init,
238 _type = g_type_register_static (GST_TYPE_ELEMENT,
239 "GstBaseTransform", &base_transform_info, G_TYPE_FLAG_ABSTRACT);
242 g_type_add_instance_private (_type, sizeof (GstBaseTransformPrivate));
244 g_once_init_leave (&base_transform_type, _type);
246 return base_transform_type;
249 static inline GstBaseTransformPrivate *
250 gst_base_transform_get_instance_private (GstBaseTransform * self)
252 return (G_STRUCT_MEMBER_P (self, private_offset));
255 static void gst_base_transform_finalize (GObject * object);
256 static void gst_base_transform_set_property (GObject * object, guint prop_id,
257 const GValue * value, GParamSpec * pspec);
258 static void gst_base_transform_get_property (GObject * object, guint prop_id,
259 GValue * value, GParamSpec * pspec);
260 static gboolean gst_base_transform_src_activate_mode (GstPad * pad,
261 GstObject * parent, GstPadMode mode, gboolean active);
262 static gboolean gst_base_transform_sink_activate_mode (GstPad * pad,
263 GstObject * parent, GstPadMode mode, gboolean active);
264 static gboolean gst_base_transform_activate (GstBaseTransform * trans,
266 static gboolean gst_base_transform_get_unit_size (GstBaseTransform * trans,
267 GstCaps * caps, gsize * size);
269 static gboolean gst_base_transform_src_event (GstPad * pad, GstObject * parent,
271 static gboolean gst_base_transform_src_eventfunc (GstBaseTransform * trans,
273 static gboolean gst_base_transform_sink_event (GstPad * pad, GstObject * parent,
275 static gboolean gst_base_transform_sink_eventfunc (GstBaseTransform * trans,
277 static GstFlowReturn gst_base_transform_getrange (GstPad * pad,
278 GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
279 static GstFlowReturn gst_base_transform_chain (GstPad * pad, GstObject * parent,
281 static GstCaps *gst_base_transform_default_transform_caps (GstBaseTransform *
282 trans, GstPadDirection direction, GstCaps * caps, GstCaps * filter);
283 static GstCaps *gst_base_transform_default_fixate_caps (GstBaseTransform *
284 trans, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
285 static GstCaps *gst_base_transform_query_caps (GstBaseTransform * trans,
286 GstPad * pad, GstCaps * filter);
287 static gboolean gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
288 GstPadDirection direction, GstCaps * caps);
289 static gboolean gst_base_transform_setcaps (GstBaseTransform * trans,
290 GstPad * pad, GstCaps * caps);
291 static gboolean gst_base_transform_default_decide_allocation (GstBaseTransform
292 * trans, GstQuery * query);
293 static gboolean gst_base_transform_default_propose_allocation (GstBaseTransform
294 * trans, GstQuery * decide_query, GstQuery * query);
295 static gboolean gst_base_transform_query (GstPad * pad, GstObject * parent,
297 static gboolean gst_base_transform_default_query (GstBaseTransform * trans,
298 GstPadDirection direction, GstQuery * query);
299 static gboolean gst_base_transform_default_transform_size (GstBaseTransform *
300 trans, GstPadDirection direction, GstCaps * caps, gsize size,
301 GstCaps * othercaps, gsize * othersize);
303 static GstFlowReturn default_prepare_output_buffer (GstBaseTransform * trans,
304 GstBuffer * inbuf, GstBuffer ** outbuf);
305 static gboolean default_copy_metadata (GstBaseTransform * trans,
306 GstBuffer * inbuf, GstBuffer * outbuf);
308 gst_base_transform_default_transform_meta (GstBaseTransform * trans,
309 GstBuffer * outbuf, GstMeta * meta, GstBuffer * inbuf);
311 /* static guint gst_base_transform_signals[LAST_SIGNAL] = { 0 }; */
315 gst_base_transform_finalize (GObject * object)
317 G_OBJECT_CLASS (parent_class)->finalize (object);
321 gst_base_transform_class_init (GstBaseTransformClass * klass)
323 GObjectClass *gobject_class;
325 gobject_class = G_OBJECT_CLASS (klass);
327 if (private_offset != 0)
328 g_type_class_adjust_private_offset (klass, &private_offset);
330 GST_DEBUG_CATEGORY_INIT (gst_base_transform_debug, "basetransform", 0,
331 "basetransform element");
333 GST_DEBUG ("gst_base_transform_class_init");
335 parent_class = g_type_class_peek_parent (klass);
337 gobject_class->set_property = gst_base_transform_set_property;
338 gobject_class->get_property = gst_base_transform_get_property;
340 g_object_class_install_property (gobject_class, PROP_QOS,
341 g_param_spec_boolean ("qos", "QoS", "Handle Quality-of-Service events",
342 DEFAULT_PROP_QOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 gobject_class->finalize = gst_base_transform_finalize;
346 klass->passthrough_on_same_caps = FALSE;
347 klass->transform_ip_on_passthrough = TRUE;
349 klass->transform_caps =
350 GST_DEBUG_FUNCPTR (gst_base_transform_default_transform_caps);
352 GST_DEBUG_FUNCPTR (gst_base_transform_default_fixate_caps);
354 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps_default);
355 klass->query = GST_DEBUG_FUNCPTR (gst_base_transform_default_query);
356 klass->decide_allocation =
357 GST_DEBUG_FUNCPTR (gst_base_transform_default_decide_allocation);
358 klass->propose_allocation =
359 GST_DEBUG_FUNCPTR (gst_base_transform_default_propose_allocation);
360 klass->transform_size =
361 GST_DEBUG_FUNCPTR (gst_base_transform_default_transform_size);
362 klass->transform_meta =
363 GST_DEBUG_FUNCPTR (gst_base_transform_default_transform_meta);
365 klass->sink_event = GST_DEBUG_FUNCPTR (gst_base_transform_sink_eventfunc);
366 klass->src_event = GST_DEBUG_FUNCPTR (gst_base_transform_src_eventfunc);
367 klass->prepare_output_buffer =
368 GST_DEBUG_FUNCPTR (default_prepare_output_buffer);
369 klass->copy_metadata = GST_DEBUG_FUNCPTR (default_copy_metadata);
370 klass->submit_input_buffer = GST_DEBUG_FUNCPTR (default_submit_input_buffer);
371 klass->generate_output = GST_DEBUG_FUNCPTR (default_generate_output);
375 gst_base_transform_init (GstBaseTransform * trans,
376 GstBaseTransformClass * bclass)
378 GstPadTemplate *pad_template;
379 GstBaseTransformPrivate *priv;
381 GST_DEBUG ("gst_base_transform_init");
383 priv = trans->priv = gst_base_transform_get_instance_private (trans);
386 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
387 g_return_if_fail (pad_template != NULL);
388 trans->sinkpad = gst_pad_new_from_template (pad_template, "sink");
389 gst_pad_set_event_function (trans->sinkpad,
390 GST_DEBUG_FUNCPTR (gst_base_transform_sink_event));
391 gst_pad_set_chain_function (trans->sinkpad,
392 GST_DEBUG_FUNCPTR (gst_base_transform_chain));
393 gst_pad_set_activatemode_function (trans->sinkpad,
394 GST_DEBUG_FUNCPTR (gst_base_transform_sink_activate_mode));
395 gst_pad_set_query_function (trans->sinkpad,
396 GST_DEBUG_FUNCPTR (gst_base_transform_query));
397 gst_element_add_pad (GST_ELEMENT (trans), trans->sinkpad);
400 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
401 g_return_if_fail (pad_template != NULL);
402 trans->srcpad = gst_pad_new_from_template (pad_template, "src");
403 gst_pad_set_event_function (trans->srcpad,
404 GST_DEBUG_FUNCPTR (gst_base_transform_src_event));
405 gst_pad_set_getrange_function (trans->srcpad,
406 GST_DEBUG_FUNCPTR (gst_base_transform_getrange));
407 gst_pad_set_activatemode_function (trans->srcpad,
408 GST_DEBUG_FUNCPTR (gst_base_transform_src_activate_mode));
409 gst_pad_set_query_function (trans->srcpad,
410 GST_DEBUG_FUNCPTR (gst_base_transform_query));
411 gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
413 priv->qos_enabled = DEFAULT_PROP_QOS;
414 priv->cache_caps1 = NULL;
415 priv->cache_caps2 = NULL;
416 priv->pad_mode = GST_PAD_MODE_NONE;
417 priv->gap_aware = FALSE;
418 priv->prefer_passthrough = TRUE;
420 priv->passthrough = FALSE;
421 if (bclass->transform == NULL) {
422 /* If no transform function, always_in_place is TRUE */
423 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
424 priv->always_in_place = TRUE;
426 if (bclass->transform_ip == NULL) {
427 GST_DEBUG_OBJECT (trans, "setting passthrough TRUE");
428 priv->passthrough = TRUE;
437 gst_base_transform_default_transform_caps (GstBaseTransform * trans,
438 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
442 GST_DEBUG_OBJECT (trans, "identity from: %" GST_PTR_FORMAT, caps);
443 /* no transform function, use the identity transform */
445 ret = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
447 ret = gst_caps_ref (caps);
452 /* given @caps on the src or sink pad (given by @direction)
453 * calculate the possible caps on the other pad.
455 * Returns new caps, unref after usage.
458 gst_base_transform_transform_caps (GstBaseTransform * trans,
459 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
462 GstBaseTransformClass *klass;
467 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
469 /* if there is a custom transform function, use this */
470 if (klass->transform_caps) {
471 GST_DEBUG_OBJECT (trans, "transform caps (direction = %d)", direction);
473 GST_LOG_OBJECT (trans, "from: %" GST_PTR_FORMAT, caps);
474 ret = klass->transform_caps (trans, direction, caps, filter);
475 GST_LOG_OBJECT (trans, " to: %" GST_PTR_FORMAT, ret);
477 #ifdef GST_ENABLE_EXTRA_CHECKS
479 if (!gst_caps_is_subset (ret, filter)) {
480 GstCaps *intersection;
482 GST_ERROR_OBJECT (trans,
483 "transform_caps returned caps %" GST_PTR_FORMAT
484 " which are not a real subset of the filter caps %"
485 GST_PTR_FORMAT, ret, filter);
486 g_warning ("%s: transform_caps returned caps which are not a real "
487 "subset of the filter caps", GST_ELEMENT_NAME (trans));
490 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
491 gst_caps_unref (ret);
498 GST_DEBUG_OBJECT (trans, "to: %" GST_PTR_FORMAT, ret);
504 gst_base_transform_default_transform_meta (GstBaseTransform * trans,
505 GstBuffer * outbuf, GstMeta * meta, GstBuffer * inbuf)
507 const GstMetaInfo *info = meta->info;
508 const gchar *const *tags;
510 tags = gst_meta_api_type_get_tags (info->api);
519 gst_base_transform_default_transform_size (GstBaseTransform * trans,
520 GstPadDirection direction, GstCaps * caps, gsize size,
521 GstCaps * othercaps, gsize * othersize)
523 gsize inunitsize, outunitsize, units;
524 GstBaseTransformClass *klass;
526 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
528 if (klass->get_unit_size == NULL) {
529 /* if there is no transform_size and no unit_size, it means the
530 * element does not modify the size of a buffer */
533 /* there is no transform_size function, we have to use the unit_size
534 * functions. This method assumes there is a fixed unit_size associated with
535 * each caps. We provide the same amount of units on both sides. */
536 if (!gst_base_transform_get_unit_size (trans, caps, &inunitsize))
539 GST_DEBUG_OBJECT (trans,
540 "input size %" G_GSIZE_FORMAT ", input unit size %" G_GSIZE_FORMAT,
543 /* input size must be a multiple of the unit_size of the input caps */
544 if (inunitsize == 0 || (size % inunitsize != 0))
547 /* get the amount of units */
548 units = size / inunitsize;
550 /* now get the unit size of the output */
551 if (!gst_base_transform_get_unit_size (trans, othercaps, &outunitsize))
554 /* the output size is the unit_size times the amount of units on the
556 *othersize = units * outunitsize;
557 GST_DEBUG_OBJECT (trans, "transformed size to %" G_GSIZE_FORMAT,
565 GST_DEBUG_OBJECT (trans, "could not get in_size");
566 g_warning ("%s: could not get in_size", GST_ELEMENT_NAME (trans));
571 GST_DEBUG_OBJECT (trans, "Size %" G_GSIZE_FORMAT " is not a multiple of"
572 "unit size %" G_GSIZE_FORMAT, size, inunitsize);
573 g_warning ("%s: size %" G_GSIZE_FORMAT " is not a multiple of unit size %"
574 G_GSIZE_FORMAT, GST_ELEMENT_NAME (trans), size, inunitsize);
579 GST_DEBUG_OBJECT (trans, "could not get out_size");
580 g_warning ("%s: could not get out_size", GST_ELEMENT_NAME (trans));
585 /* transform a buffer of @size with @caps on the pad with @direction to
586 * the size of a buffer with @othercaps and store the result in @othersize
588 * We have two ways of doing this:
589 * 1) use a custom transform size function, this is for complicated custom
590 * cases with no fixed unit_size.
591 * 2) use the unit_size functions where there is a relationship between the
592 * caps and the size of a buffer.
595 gst_base_transform_transform_size (GstBaseTransform * trans,
596 GstPadDirection direction, GstCaps * caps,
597 gsize size, GstCaps * othercaps, gsize * othersize)
599 GstBaseTransformClass *klass;
600 gboolean ret = FALSE;
602 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
604 GST_DEBUG_OBJECT (trans,
605 "asked to transform size %" G_GSIZE_FORMAT " for caps %"
606 GST_PTR_FORMAT " to size for caps %" GST_PTR_FORMAT " in direction %s",
607 size, caps, othercaps, direction == GST_PAD_SRC ? "SRC" : "SINK");
609 if (klass->transform_size) {
610 /* if there is a custom transform function, use this */
611 ret = klass->transform_size (trans, direction, caps, size, othercaps,
617 /* get the caps that can be handled by @pad. We perform:
619 * - take the caps of peer of otherpad,
620 * - filter against the padtemplate of otherpad,
621 * - calculate all transforms of remaining caps
622 * - filter against template of @pad
624 * If there is no peer, we simply return the caps of the padtemplate of pad.
627 gst_base_transform_query_caps (GstBaseTransform * trans, GstPad * pad,
631 GstCaps *peercaps = NULL, *caps, *temp, *peerfilter = NULL;
632 GstCaps *templ, *otempl;
634 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
636 templ = gst_pad_get_pad_template_caps (pad);
637 otempl = gst_pad_get_pad_template_caps (otherpad);
639 /* first prepare the filter to be sent onwards. We need to filter and
640 * transform it to valid caps for the otherpad. */
642 GST_DEBUG_OBJECT (pad, "filter caps %" GST_PTR_FORMAT, filter);
644 /* filtered against our padtemplate of this pad */
645 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
646 temp = gst_caps_intersect_full (filter, templ, GST_CAPS_INTERSECT_FIRST);
647 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
649 /* then see what we can transform this to */
650 peerfilter = gst_base_transform_transform_caps (trans,
651 GST_PAD_DIRECTION (pad), temp, NULL);
652 GST_DEBUG_OBJECT (pad, "transformed %" GST_PTR_FORMAT, peerfilter);
653 gst_caps_unref (temp);
656 if (!gst_caps_is_empty (peerfilter)) {
657 /* and filter against the template of the other pad */
658 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, otempl);
659 /* We keep the caps sorted like the returned caps */
661 gst_caps_intersect_full (peerfilter, otempl,
662 GST_CAPS_INTERSECT_FIRST);
663 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
664 gst_caps_unref (peerfilter);
668 /* If we filter out everything, bail out */
669 if (peerfilter && gst_caps_is_empty (peerfilter)) {
670 GST_DEBUG_OBJECT (pad, "peer filter caps are empty");
678 GST_DEBUG_OBJECT (pad, "peer filter caps %" GST_PTR_FORMAT, peerfilter);
680 /* query the peer with the transformed filter */
681 peercaps = gst_pad_peer_query_caps (otherpad, peerfilter);
684 gst_caps_unref (peerfilter);
687 GST_DEBUG_OBJECT (pad, "peer caps %" GST_PTR_FORMAT, peercaps);
689 /* filtered against our padtemplate on the other side */
690 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, otempl);
691 temp = gst_caps_intersect_full (peercaps, otempl, GST_CAPS_INTERSECT_FIRST);
692 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
694 temp = gst_caps_ref (otempl);
697 /* then see what we can transform this to */
698 caps = gst_base_transform_transform_caps (trans,
699 GST_PAD_DIRECTION (otherpad), temp, filter);
700 GST_DEBUG_OBJECT (pad, "transformed %" GST_PTR_FORMAT, caps);
701 gst_caps_unref (temp);
702 if (caps == NULL || gst_caps_is_empty (caps))
706 /* and filter against the template of this pad */
707 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
708 /* We keep the caps sorted like the returned caps */
709 temp = gst_caps_intersect_full (caps, templ, GST_CAPS_INTERSECT_FIRST);
710 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
711 gst_caps_unref (caps);
714 if (trans->priv->prefer_passthrough) {
715 /* Now try if we can put the untransformed downstream caps first */
716 temp = gst_caps_intersect_full (peercaps, caps, GST_CAPS_INTERSECT_FIRST);
717 if (!gst_caps_is_empty (temp)) {
718 caps = gst_caps_merge (temp, caps);
720 gst_caps_unref (temp);
724 gst_caps_unref (caps);
725 /* no peer or the peer can do anything, our padtemplate is enough then */
727 caps = gst_caps_intersect_full (filter, templ, GST_CAPS_INTERSECT_FIRST);
729 caps = gst_caps_ref (templ);
734 GST_DEBUG_OBJECT (trans, "returning %" GST_PTR_FORMAT, caps);
737 gst_caps_unref (peercaps);
739 gst_caps_unref (templ);
740 gst_caps_unref (otempl);
745 /* takes ownership of the pool, allocator and query */
747 gst_base_transform_set_allocation (GstBaseTransform * trans,
748 GstBufferPool * pool, GstAllocator * allocator,
749 const GstAllocationParams * params, GstQuery * query)
751 GstAllocator *oldalloc;
752 GstBufferPool *oldpool;
754 GstBaseTransformPrivate *priv = trans->priv;
756 GST_OBJECT_LOCK (trans);
757 oldpool = priv->pool;
759 priv->pool_active = FALSE;
761 oldalloc = priv->allocator;
762 priv->allocator = allocator;
764 oldquery = priv->query;
768 priv->params = *params;
770 gst_allocation_params_init (&priv->params);
771 GST_OBJECT_UNLOCK (trans);
774 GST_DEBUG_OBJECT (trans, "deactivating old pool %p", oldpool);
775 gst_buffer_pool_set_active (oldpool, FALSE);
776 gst_object_unref (oldpool);
779 gst_object_unref (oldalloc);
782 gst_query_unref (oldquery);
788 gst_base_transform_default_decide_allocation (GstBaseTransform * trans,
792 GstBaseTransformClass *klass;
795 guint size, min, max;
796 GstAllocator *allocator;
797 GstAllocationParams params;
798 GstStructure *config;
799 gboolean update_allocator;
801 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
803 n_metas = gst_query_get_n_allocation_metas (query);
804 for (i = 0; i < n_metas; i++) {
806 const GstStructure *params;
809 api = gst_query_parse_nth_allocation_meta (query, i, ¶ms);
811 /* by default we remove all metadata, subclasses should implement a
812 * filter_meta function */
813 if (gst_meta_api_type_has_tag (api, _gst_meta_tag_memory)) {
814 /* remove all memory dependent metadata because we are going to have to
815 * allocate different memory for input and output. */
816 GST_LOG_OBJECT (trans, "removing memory specific metadata %s",
819 } else if (G_LIKELY (klass->filter_meta)) {
820 /* remove if the subclass said so */
821 remove = !klass->filter_meta (trans, query, api, params);
822 GST_LOG_OBJECT (trans, "filter_meta for api %s returned: %s",
823 g_type_name (api), (remove ? "remove" : "keep"));
825 GST_LOG_OBJECT (trans, "removing metadata %s", g_type_name (api));
830 gst_query_remove_nth_allocation_meta (query, i);
836 gst_query_parse_allocation (query, &outcaps, NULL);
838 /* we got configuration from our peer or the decide_allocation method,
840 if (gst_query_get_n_allocation_params (query) > 0) {
841 /* try the allocator */
842 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
843 update_allocator = TRUE;
846 gst_allocation_params_init (¶ms);
847 update_allocator = FALSE;
850 if (gst_query_get_n_allocation_pools (query) > 0) {
851 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
854 /* no pool, we can make our own */
855 GST_DEBUG_OBJECT (trans, "no pool, making new pool");
856 pool = gst_buffer_pool_new ();
860 size = min = max = 0;
865 config = gst_buffer_pool_get_config (pool);
866 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
867 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
869 /* buffer pool may have to do some changes */
870 if (!gst_buffer_pool_set_config (pool, config)) {
871 config = gst_buffer_pool_get_config (pool);
873 /* If change are not acceptable, fallback to generic pool */
874 if (!gst_buffer_pool_config_validate_params (config, outcaps, size, min,
876 GST_DEBUG_OBJECT (trans, "unsupported pool, making new pool");
878 gst_object_unref (pool);
879 pool = gst_buffer_pool_new ();
880 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
881 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
884 if (!gst_buffer_pool_set_config (pool, config))
889 if (update_allocator)
890 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
892 gst_query_add_allocation_param (query, allocator, ¶ms);
894 gst_object_unref (allocator);
897 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
898 gst_object_unref (pool);
905 gst_object_unref (pool);
907 GST_ELEMENT_ERROR (trans, RESOURCE, SETTINGS,
908 ("Failed to configure the buffer pool"),
909 ("Configuration is most likely invalid, please report this issue."));
914 gst_base_transform_do_bufferpool (GstBaseTransform * trans, GstCaps * outcaps)
917 gboolean result = TRUE;
918 GstBufferPool *pool = NULL;
919 GstBaseTransformClass *klass;
920 GstBaseTransformPrivate *priv = trans->priv;
921 GstAllocator *allocator;
922 GstAllocationParams params;
924 /* there are these possibilities:
926 * 1) we negotiated passthrough, we can proxy the bufferpool directly and we
927 * will do that whenever some upstream does an allocation query.
928 * 2) we need to do a transform, we need to get a bufferpool from downstream
929 * and configure it. When upstream does the ALLOCATION query, the
930 * propose_allocation vmethod will be called and we will configure the
931 * upstream allocator with our proposed values then.
933 if (priv->passthrough || priv->always_in_place) {
934 /* we are in passthrough, the input buffer is never copied and always passed
935 * along. We never allocate an output buffer on the srcpad. What we do is
936 * let the upstream element decide if it wants to use a bufferpool and
937 * then we will proxy the downstream pool */
938 GST_DEBUG_OBJECT (trans, "we're passthough, delay bufferpool");
939 gst_base_transform_set_allocation (trans, NULL, NULL, NULL, NULL);
943 /* not passthrough, we need to allocate */
944 /* find a pool for the negotiated caps now */
945 GST_DEBUG_OBJECT (trans, "doing allocation query");
946 query = gst_query_new_allocation (outcaps, TRUE);
947 if (!gst_pad_peer_query (trans->srcpad, query)) {
948 /* not a problem, just debug a little */
949 GST_DEBUG_OBJECT (trans, "peer ALLOCATION query failed");
952 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
954 GST_DEBUG_OBJECT (trans, "calling decide_allocation");
955 g_assert (klass->decide_allocation != NULL);
956 result = klass->decide_allocation (trans, query);
958 GST_DEBUG_OBJECT (trans, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
962 goto no_decide_allocation;
964 /* check again in case the sub-class have switch to passthrough/in-place
965 * after looking at the meta APIs */
966 if (priv->passthrough || priv->always_in_place) {
967 GST_DEBUG_OBJECT (trans, "no doing passthrough, delay bufferpool");
968 gst_base_transform_set_allocation (trans, NULL, NULL, NULL, NULL);
969 gst_query_unref (query);
973 /* we got configuration from our peer or the decide_allocation method,
975 if (gst_query_get_n_allocation_params (query) > 0) {
976 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
979 gst_allocation_params_init (¶ms);
982 if (gst_query_get_n_allocation_pools (query) > 0)
983 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
987 gst_base_transform_set_allocation (trans, pool, allocator, ¶ms,
993 no_decide_allocation:
995 GST_WARNING_OBJECT (trans, "Subclass failed to decide allocation");
996 gst_query_unref (query);
1002 /* function triggered when the in and out caps are negotiated and need
1003 * to be configured in the subclass. */
1005 gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
1008 gboolean ret = TRUE;
1009 GstBaseTransformClass *klass;
1010 GstBaseTransformPrivate *priv = trans->priv;
1012 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1014 GST_DEBUG_OBJECT (trans, "in caps: %" GST_PTR_FORMAT, in);
1015 GST_DEBUG_OBJECT (trans, "out caps: %" GST_PTR_FORMAT, out);
1017 /* clear the cache */
1018 gst_caps_replace (&priv->cache_caps1, NULL);
1019 gst_caps_replace (&priv->cache_caps2, NULL);
1021 /* figure out same caps state */
1022 priv->have_same_caps = gst_caps_is_equal (in, out);
1023 GST_DEBUG_OBJECT (trans, "have_same_caps: %d", priv->have_same_caps);
1025 /* Set the passthrough if the class wants passthrough_on_same_caps
1026 * and we have the same caps on each pad */
1027 if (klass->passthrough_on_same_caps)
1028 gst_base_transform_set_passthrough (trans, priv->have_same_caps);
1030 /* now configure the element with the caps */
1031 if (klass->set_caps) {
1032 GST_DEBUG_OBJECT (trans, "Calling set_caps method to setup caps");
1033 ret = klass->set_caps (trans, in, out);
1040 gst_base_transform_default_fixate_caps (GstBaseTransform * trans,
1041 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
1043 othercaps = gst_caps_fixate (othercaps);
1044 GST_DEBUG_OBJECT (trans, "fixated to %" GST_PTR_FORMAT, othercaps);
1049 /* given a fixed @caps on @pad, create the best possible caps for the
1051 * @caps must be fixed when calling this function.
1053 * This function calls the transform caps vmethod of the basetransform to figure
1054 * out the possible target formats. It then tries to select the best format from
1057 * - attempt passthrough if the target caps is a superset of the input caps
1058 * - fixating by using peer caps
1059 * - fixating with transform fixate function
1060 * - fixating with pad fixate functions.
1062 * this function returns a caps that can be transformed into and is accepted by
1066 gst_base_transform_find_transform (GstBaseTransform * trans, GstPad * pad,
1069 GstBaseTransformClass *klass;
1070 GstPad *otherpad, *otherpeer;
1074 /* caps must be fixed here, this is a programming error if it's not */
1075 g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
1077 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1079 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1080 otherpeer = gst_pad_get_peer (otherpad);
1082 /* see how we can transform the input caps. We need to do this even for
1083 * passthrough because it might be possible that this element cannot support
1084 * passthrough at all. */
1085 othercaps = gst_base_transform_transform_caps (trans,
1086 GST_PAD_DIRECTION (pad), caps, NULL);
1088 /* The caps we can actually output is the intersection of the transformed
1089 * caps with the pad template for the pad */
1090 if (othercaps && !gst_caps_is_empty (othercaps)) {
1091 GstCaps *intersect, *templ_caps;
1093 templ_caps = gst_pad_get_pad_template_caps (otherpad);
1094 GST_DEBUG_OBJECT (trans,
1095 "intersecting against padtemplate %" GST_PTR_FORMAT, templ_caps);
1098 gst_caps_intersect_full (othercaps, templ_caps,
1099 GST_CAPS_INTERSECT_FIRST);
1101 gst_caps_unref (othercaps);
1102 gst_caps_unref (templ_caps);
1103 othercaps = intersect;
1106 /* check if transform is empty */
1107 if (!othercaps || gst_caps_is_empty (othercaps))
1110 /* if the othercaps are not fixed, we need to fixate them, first attempt
1111 * is by attempting passthrough if the othercaps are a superset of caps. */
1112 /* FIXME. maybe the caps is not fixed because it has multiple structures of
1114 is_fixed = gst_caps_is_fixed (othercaps);
1116 GST_DEBUG_OBJECT (trans,
1117 "transform returned non fixed %" GST_PTR_FORMAT, othercaps);
1119 /* Now let's see what the peer suggests based on our transformed caps */
1121 GstCaps *peercaps, *intersection, *templ_caps;
1123 GST_DEBUG_OBJECT (trans,
1124 "Checking peer caps with filter %" GST_PTR_FORMAT, othercaps);
1126 peercaps = gst_pad_query_caps (otherpeer, othercaps);
1127 GST_DEBUG_OBJECT (trans, "Resulted in %" GST_PTR_FORMAT, peercaps);
1128 if (!gst_caps_is_empty (peercaps)) {
1129 templ_caps = gst_pad_get_pad_template_caps (otherpad);
1131 GST_DEBUG_OBJECT (trans,
1132 "Intersecting with template caps %" GST_PTR_FORMAT, templ_caps);
1135 gst_caps_intersect_full (peercaps, templ_caps,
1136 GST_CAPS_INTERSECT_FIRST);
1137 GST_DEBUG_OBJECT (trans, "Intersection: %" GST_PTR_FORMAT,
1139 gst_caps_unref (peercaps);
1140 gst_caps_unref (templ_caps);
1141 peercaps = intersection;
1143 GST_DEBUG_OBJECT (trans,
1144 "Intersecting with transformed caps %" GST_PTR_FORMAT, othercaps);
1146 gst_caps_intersect_full (peercaps, othercaps,
1147 GST_CAPS_INTERSECT_FIRST);
1148 GST_DEBUG_OBJECT (trans, "Intersection: %" GST_PTR_FORMAT,
1150 gst_caps_unref (peercaps);
1151 gst_caps_unref (othercaps);
1152 othercaps = intersection;
1154 gst_caps_unref (othercaps);
1155 othercaps = peercaps;
1158 is_fixed = gst_caps_is_fixed (othercaps);
1160 GST_DEBUG_OBJECT (trans, "no peer, doing passthrough");
1161 gst_caps_unref (othercaps);
1162 othercaps = gst_caps_ref (caps);
1166 if (gst_caps_is_empty (othercaps))
1167 goto no_transform_possible;
1169 GST_DEBUG ("have %sfixed caps %" GST_PTR_FORMAT, (is_fixed ? "" : "non-"),
1172 /* second attempt at fixation, call the fixate vmethod */
1173 /* caps could be fixed but the subclass may want to add fields */
1174 if (klass->fixate_caps) {
1175 GST_DEBUG_OBJECT (trans, "calling fixate_caps for %" GST_PTR_FORMAT
1176 " using caps %" GST_PTR_FORMAT " on pad %s:%s", othercaps, caps,
1177 GST_DEBUG_PAD_NAME (otherpad));
1178 /* note that we pass the complete array of structures to the fixate
1179 * function, it needs to truncate itself */
1181 klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
1184 g_critical ("basetransform: second attempt to fixate caps returned "
1185 "invalid (NULL) caps on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1187 is_fixed = othercaps && gst_caps_is_fixed (othercaps);
1188 GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
1191 /* caps should be fixed now, if not we have to fail. */
1193 goto could_not_fixate;
1195 /* and peer should accept */
1196 if (otherpeer && !gst_pad_query_accept_caps (otherpeer, othercaps))
1197 goto peer_no_accept;
1199 GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT
1200 ", and got final caps %" GST_PTR_FORMAT, caps, othercaps);
1203 gst_object_unref (otherpeer);
1210 GST_DEBUG_OBJECT (trans,
1211 "transform returned useless %" GST_PTR_FORMAT, othercaps);
1214 no_transform_possible:
1216 GST_DEBUG_OBJECT (trans,
1217 "transform could not transform %" GST_PTR_FORMAT
1218 " in anything we support", caps);
1223 GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
1228 GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
1229 " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1235 gst_object_unref (otherpeer);
1237 gst_caps_unref (othercaps);
1243 gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
1244 GstPadDirection direction, GstCaps * caps)
1246 GstPad *pad, *otherpad;
1247 GstCaps *templ, *otempl, *ocaps = NULL;
1248 gboolean ret = TRUE;
1252 GST_PAD_SINK) ? GST_BASE_TRANSFORM_SINK_PAD (trans) :
1253 GST_BASE_TRANSFORM_SRC_PAD (trans);
1256 GST_PAD_SINK) ? GST_BASE_TRANSFORM_SRC_PAD (trans) :
1257 GST_BASE_TRANSFORM_SINK_PAD (trans);
1259 GST_DEBUG_OBJECT (trans, "accept caps %" GST_PTR_FORMAT, caps);
1261 templ = gst_pad_get_pad_template_caps (pad);
1262 otempl = gst_pad_get_pad_template_caps (otherpad);
1264 /* get all the formats we can handle on this pad */
1265 GST_DEBUG_OBJECT (trans, "intersect with pad template: %" GST_PTR_FORMAT,
1267 if (!gst_caps_can_intersect (caps, templ))
1270 GST_DEBUG_OBJECT (trans, "trying to transform with filter: %"
1271 GST_PTR_FORMAT " (the other pad template)", otempl);
1272 ocaps = gst_base_transform_transform_caps (trans, direction, caps, otempl);
1273 if (!ocaps || gst_caps_is_empty (ocaps))
1274 goto no_transform_possible;
1277 GST_DEBUG_OBJECT (trans, "accept-caps result: %d", ret);
1279 gst_caps_unref (ocaps);
1280 gst_caps_unref (templ);
1281 gst_caps_unref (otempl);
1287 GST_DEBUG_OBJECT (trans, "caps can't intersect with the template");
1291 no_transform_possible:
1293 GST_DEBUG_OBJECT (trans,
1294 "transform could not transform %" GST_PTR_FORMAT
1295 " in anything we support", caps);
1301 /* called when new caps arrive on the sink pad,
1302 * We try to find the best caps for the other side using our _find_transform()
1303 * function. If there are caps, we configure the transform for this new
1307 gst_base_transform_setcaps (GstBaseTransform * trans, GstPad * pad,
1310 GstBaseTransformPrivate *priv = trans->priv;
1311 GstCaps *outcaps, *prev_incaps = NULL, *prev_outcaps = NULL;
1312 gboolean ret = TRUE;
1314 GST_DEBUG_OBJECT (pad, "have new caps %p %" GST_PTR_FORMAT, incaps, incaps);
1316 /* find best possible caps for the other pad */
1317 outcaps = gst_base_transform_find_transform (trans, pad, incaps);
1318 if (!outcaps || gst_caps_is_empty (outcaps))
1319 goto no_transform_possible;
1321 /* configure the element now */
1323 /* if we have the same caps, we can optimize and reuse the input caps */
1324 if (gst_caps_is_equal (incaps, outcaps)) {
1325 GST_INFO_OBJECT (trans, "reuse caps");
1326 gst_caps_unref (outcaps);
1327 outcaps = gst_caps_ref (incaps);
1330 prev_incaps = gst_pad_get_current_caps (trans->sinkpad);
1331 prev_outcaps = gst_pad_get_current_caps (trans->srcpad);
1332 if (prev_incaps && prev_outcaps && gst_caps_is_equal (prev_incaps, incaps)
1333 && gst_caps_is_equal (prev_outcaps, outcaps)) {
1334 GST_DEBUG_OBJECT (trans,
1335 "New caps equal to old ones: %" GST_PTR_FORMAT " -> %" GST_PTR_FORMAT,
1339 /* call configure now */
1340 if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
1341 goto failed_configure;
1343 if (!prev_outcaps || !gst_caps_is_equal (outcaps, prev_outcaps))
1344 /* let downstream know about our caps */
1345 ret = gst_pad_set_caps (trans->srcpad, outcaps);
1349 /* try to get a pool when needed */
1350 ret = gst_base_transform_do_bufferpool (trans, outcaps);
1355 gst_caps_unref (outcaps);
1357 gst_caps_unref (prev_incaps);
1359 gst_caps_unref (prev_outcaps);
1361 GST_OBJECT_LOCK (trans);
1362 priv->negotiated = ret;
1363 GST_OBJECT_UNLOCK (trans);
1368 no_transform_possible:
1370 GST_WARNING_OBJECT (trans,
1371 "transform could not transform %" GST_PTR_FORMAT
1372 " in anything we support", incaps);
1378 GST_WARNING_OBJECT (trans, "FAILED to configure incaps %" GST_PTR_FORMAT
1379 " and outcaps %" GST_PTR_FORMAT, incaps, outcaps);
1386 gst_base_transform_default_propose_allocation (GstBaseTransform * trans,
1387 GstQuery * decide_query, GstQuery * query)
1391 if (decide_query == NULL) {
1392 GST_DEBUG_OBJECT (trans, "doing passthrough query");
1393 ret = gst_pad_peer_query (trans->srcpad, query);
1396 /* non-passthrough, copy all metadata, decide_query does not contain the
1397 * metadata anymore that depends on the buffer memory */
1398 n_metas = gst_query_get_n_allocation_metas (decide_query);
1399 for (i = 0; i < n_metas; i++) {
1401 const GstStructure *params;
1403 api = gst_query_parse_nth_allocation_meta (decide_query, i, ¶ms);
1404 GST_DEBUG_OBJECT (trans, "proposing metadata %s", g_type_name (api));
1405 gst_query_add_allocation_meta (query, api, params);
1413 gst_base_transform_reconfigure_unlocked (GstBaseTransform * trans)
1415 gboolean reconfigure, ret = TRUE;
1417 reconfigure = gst_pad_check_reconfigure (trans->srcpad);
1419 if (G_UNLIKELY (reconfigure)) {
1422 GST_DEBUG_OBJECT (trans, "we had a pending reconfigure");
1424 incaps = gst_pad_get_current_caps (trans->sinkpad);
1428 /* if we need to reconfigure we pretend new caps arrived. This
1429 * will reconfigure the transform with the new output format. */
1430 if (!gst_base_transform_setcaps (trans, trans->sinkpad, incaps)) {
1431 GST_ELEMENT_WARNING (trans, STREAM, FORMAT,
1432 ("not negotiated"), ("not negotiated"));
1436 gst_caps_unref (incaps);
1442 gst_pad_mark_reconfigure (trans->srcpad);
1448 * gst_base_transform_reconfigure:
1449 * @trans: the #GstBaseTransform to set
1451 * Negotiates src pad caps with downstream elements if the source pad is
1452 * marked as needing reconfiguring. Unmarks GST_PAD_FLAG_NEED_RECONFIGURE in
1453 * any case. But marks it again if negotiation fails.
1455 * Do not call this in the #GstBaseTransformClass::transform or
1456 * #GstBaseTransformClass::transform_ip vmethod. Call this in
1457 * #GstBaseTransformClass::submit_input_buffer,
1458 * #GstBaseTransformClass::prepare_output_buffer or in
1459 * #GstBaseTransformClass::generate_output _before_ any output buffer is
1462 * It will be default be called when handling an ALLOCATION query or at the
1463 * very beginning of the default #GstBaseTransformClass::submit_input_buffer
1466 * Returns: %TRUE if the negotiation succeeded, else %FALSE.
1471 gst_base_transform_reconfigure (GstBaseTransform * trans)
1475 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
1477 GST_PAD_STREAM_LOCK (trans->sinkpad);
1478 ret = gst_base_transform_reconfigure_unlocked (trans);
1480 gst_pad_mark_reconfigure (trans->srcpad);
1481 GST_PAD_STREAM_UNLOCK (trans->sinkpad);
1487 gst_base_transform_default_query (GstBaseTransform * trans,
1488 GstPadDirection direction, GstQuery * query)
1490 gboolean ret = FALSE;
1491 GstPad *pad, *otherpad;
1492 GstBaseTransformClass *klass;
1493 GstBaseTransformPrivate *priv = trans->priv;
1495 if (direction == GST_PAD_SRC) {
1496 pad = trans->srcpad;
1497 otherpad = trans->sinkpad;
1499 pad = trans->sinkpad;
1500 otherpad = trans->srcpad;
1503 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1505 switch (GST_QUERY_TYPE (query)) {
1506 case GST_QUERY_ALLOCATION:
1508 GstQuery *decide_query = NULL;
1510 /* can only be done on the sinkpad */
1511 if (direction != GST_PAD_SINK)
1514 ret = gst_base_transform_reconfigure_unlocked (trans);
1515 if (G_UNLIKELY (!ret))
1518 GST_OBJECT_LOCK (trans);
1519 if (!priv->negotiated && !priv->passthrough && (klass->set_caps != NULL)) {
1520 GST_DEBUG_OBJECT (trans,
1521 "not negotiated yet but need negotiation, can't answer ALLOCATION query");
1522 GST_OBJECT_UNLOCK (trans);
1526 decide_query = trans->priv->query;
1527 trans->priv->query = NULL;
1528 GST_OBJECT_UNLOCK (trans);
1530 GST_DEBUG_OBJECT (trans,
1531 "calling propose allocation with query %" GST_PTR_FORMAT,
1534 /* pass the query to the propose_allocation vmethod if any */
1535 if (G_LIKELY (klass->propose_allocation))
1536 ret = klass->propose_allocation (trans, decide_query, query);
1541 GST_OBJECT_LOCK (trans);
1543 if (trans->priv->query == NULL)
1544 trans->priv->query = decide_query;
1546 gst_query_unref (decide_query);
1548 GST_OBJECT_UNLOCK (trans);
1551 GST_DEBUG_OBJECT (trans, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret,
1555 case GST_QUERY_POSITION:
1559 gst_query_parse_position (query, &format, NULL);
1560 if (format == GST_FORMAT_TIME && trans->segment.format == GST_FORMAT_TIME) {
1564 if ((direction == GST_PAD_SINK)
1565 || (trans->priv->position_out == GST_CLOCK_TIME_NONE)) {
1567 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1568 trans->segment.position);
1570 pos = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1571 trans->priv->position_out);
1573 gst_query_set_position (query, format, pos);
1575 ret = gst_pad_peer_query (otherpad, query);
1579 case GST_QUERY_ACCEPT_CAPS:
1583 gst_query_parse_accept_caps (query, &caps);
1584 if (klass->accept_caps) {
1585 ret = klass->accept_caps (trans, direction, caps);
1586 gst_query_set_accept_caps_result (query, ret);
1587 /* return TRUE, we answered the query */
1592 case GST_QUERY_CAPS:
1594 GstCaps *filter, *caps;
1596 gst_query_parse_caps (query, &filter);
1597 caps = gst_base_transform_query_caps (trans, pad, filter);
1598 gst_query_set_caps_result (query, caps);
1599 gst_caps_unref (caps);
1604 ret = gst_pad_peer_query (otherpad, query);
1613 gst_base_transform_query (GstPad * pad, GstObject * parent, GstQuery * query)
1615 GstBaseTransform *trans;
1616 GstBaseTransformClass *bclass;
1617 gboolean ret = FALSE;
1619 trans = GST_BASE_TRANSFORM_CAST (parent);
1620 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1623 ret = bclass->query (trans, GST_PAD_DIRECTION (pad), query);
1628 /* this function either returns the input buffer without incrementing the
1629 * refcount or it allocates a new (writable) buffer */
1630 static GstFlowReturn
1631 default_prepare_output_buffer (GstBaseTransform * trans,
1632 GstBuffer * inbuf, GstBuffer ** outbuf)
1634 GstBaseTransformPrivate *priv;
1636 GstBaseTransformClass *bclass;
1637 GstCaps *incaps, *outcaps;
1638 gsize insize, outsize;
1642 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1644 /* figure out how to allocate an output buffer */
1645 if (priv->passthrough) {
1646 /* passthrough, we will not modify the incoming buffer so we can just
1648 GST_DEBUG_OBJECT (trans, "passthrough: reusing input buffer");
1653 /* we can't reuse the input buffer */
1655 if (!priv->pool_active) {
1656 GST_DEBUG_OBJECT (trans, "setting pool %p active", priv->pool);
1657 if (!gst_buffer_pool_set_active (priv->pool, TRUE))
1658 goto activate_failed;
1659 priv->pool_active = TRUE;
1661 GST_DEBUG_OBJECT (trans, "using pool alloc");
1662 ret = gst_buffer_pool_acquire_buffer (priv->pool, outbuf, NULL);
1663 if (ret != GST_FLOW_OK)
1669 /* no pool, we need to figure out the size of the output buffer first */
1670 if ((bclass->transform_ip != NULL) && priv->always_in_place) {
1671 /* we want to do an in-place alloc */
1672 if (gst_buffer_is_writable (inbuf)) {
1673 GST_DEBUG_OBJECT (trans, "inplace reuse writable input buffer");
1676 GST_DEBUG_OBJECT (trans, "making writable buffer copy");
1677 /* we make a copy of the input buffer */
1678 *outbuf = gst_buffer_copy (inbuf);
1683 /* else use the transform function to get the size */
1684 incaps = gst_pad_get_current_caps (trans->sinkpad);
1685 outcaps = gst_pad_get_current_caps (trans->srcpad);
1687 /* srcpad might be flushing already if we're being shut down */
1688 if (outcaps == NULL)
1691 GST_DEBUG_OBJECT (trans, "getting output size for alloc");
1692 /* copy transform, figure out the output size */
1693 insize = gst_buffer_get_size (inbuf);
1694 res = gst_base_transform_transform_size (trans,
1695 GST_PAD_SINK, incaps, insize, outcaps, &outsize);
1697 gst_caps_unref (incaps);
1698 gst_caps_unref (outcaps);
1703 GST_DEBUG_OBJECT (trans, "doing alloc of size %" G_GSIZE_FORMAT, outsize);
1704 *outbuf = gst_buffer_new_allocate (priv->allocator, outsize, &priv->params);
1706 ret = GST_FLOW_ERROR;
1711 /* copy the metadata */
1712 if (bclass->copy_metadata)
1713 if (!bclass->copy_metadata (trans, inbuf, *outbuf)) {
1714 /* something failed, post a warning */
1715 GST_ELEMENT_WARNING (trans, STREAM, NOT_IMPLEMENTED,
1716 ("could not copy metadata"), (NULL));
1725 GST_ELEMENT_ERROR (trans, RESOURCE, SETTINGS,
1726 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
1727 return GST_FLOW_ERROR;
1731 GST_ERROR_OBJECT (trans, "unknown output size");
1732 return GST_FLOW_ERROR;
1736 GST_DEBUG_OBJECT (trans, "could not allocate buffer from pool");
1741 GST_DEBUG_OBJECT (trans, "no output caps, source pad has been deactivated");
1742 gst_caps_unref (incaps);
1743 return GST_FLOW_FLUSHING;
1749 GstBaseTransform *trans;
1754 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
1756 CopyMetaData *data = user_data;
1757 GstBaseTransform *trans = data->trans;
1758 GstBaseTransformClass *klass;
1759 const GstMetaInfo *info = (*meta)->info;
1760 GstBuffer *outbuf = data->outbuf;
1761 gboolean do_copy = FALSE;
1763 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1765 if (gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory)) {
1766 /* never call the transform_meta with memory specific metadata */
1767 GST_DEBUG_OBJECT (trans, "not copying memory specific metadata %s",
1768 g_type_name (info->api));
1770 } else if (klass->transform_meta) {
1771 do_copy = klass->transform_meta (trans, outbuf, *meta, inbuf);
1772 GST_DEBUG_OBJECT (trans, "transformed metadata %s: copy: %d",
1773 g_type_name (info->api), do_copy);
1776 /* we only copy metadata when the subclass implemented a transform_meta
1777 * function and when it returns %TRUE */
1779 GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
1780 /* simply copy then */
1781 if (info->transform_func) {
1782 GST_DEBUG_OBJECT (trans, "copy metadata %s", g_type_name (info->api));
1783 info->transform_func (outbuf, *meta, inbuf,
1784 _gst_meta_transform_copy, ©_data);
1786 GST_DEBUG_OBJECT (trans, "couldn't copy metadata %s",
1787 g_type_name (info->api));
1794 default_copy_metadata (GstBaseTransform * trans,
1795 GstBuffer * inbuf, GstBuffer * outbuf)
1797 GstBaseTransformPrivate *priv = trans->priv;
1800 /* now copy the metadata */
1801 GST_DEBUG_OBJECT (trans, "copying metadata");
1803 /* this should not happen, buffers allocated from a pool or with
1804 * new_allocate should always be writable. */
1805 if (!gst_buffer_is_writable (outbuf))
1808 /* when we get here, the metadata should be writable */
1809 gst_buffer_copy_into (outbuf, inbuf,
1810 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1812 /* clear the GAP flag when the subclass does not understand it */
1813 if (!priv->gap_aware)
1814 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
1818 data.outbuf = outbuf;
1820 gst_buffer_foreach_meta (inbuf, foreach_metadata, &data);
1827 GST_WARNING_OBJECT (trans, "buffer %p not writable", outbuf);
1832 /* Given @caps calculate the size of one unit.
1834 * For video caps, this is the size of one frame (and thus one buffer).
1835 * For audio caps, this is the size of one sample.
1837 * These values are cached since they do not change and the calculation
1838 * potentially involves parsing caps and other expensive stuff.
1840 * We have two cache locations to store the size, one for the source caps
1841 * and one for the sink caps.
1843 * this function returns %FALSE if no size could be calculated.
1846 gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
1849 gboolean res = FALSE;
1850 GstBaseTransformClass *bclass;
1851 GstBaseTransformPrivate *priv = trans->priv;
1853 /* see if we have the result cached */
1854 if (priv->cache_caps1 == caps) {
1855 *size = priv->cache_caps1_size;
1856 GST_DEBUG_OBJECT (trans,
1857 "returned %" G_GSIZE_FORMAT " from first cache", *size);
1860 if (priv->cache_caps2 == caps) {
1861 *size = priv->cache_caps2_size;
1862 GST_DEBUG_OBJECT (trans,
1863 "returned %" G_GSIZE_FORMAT " from second cached", *size);
1867 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1868 res = bclass->get_unit_size (trans, caps, size);
1869 GST_DEBUG_OBJECT (trans,
1870 "caps %" GST_PTR_FORMAT " has unit size %" G_GSIZE_FORMAT ", res %s",
1871 caps, *size, res ? "TRUE" : "FALSE");
1874 /* and cache the values */
1875 if (priv->cache_caps1 == NULL) {
1876 gst_caps_replace (&priv->cache_caps1, caps);
1877 priv->cache_caps1_size = *size;
1878 GST_DEBUG_OBJECT (trans,
1879 "caching %" G_GSIZE_FORMAT " in first cache", *size);
1880 } else if (priv->cache_caps2 == NULL) {
1881 gst_caps_replace (&priv->cache_caps2, caps);
1882 priv->cache_caps2_size = *size;
1883 GST_DEBUG_OBJECT (trans,
1884 "caching %" G_GSIZE_FORMAT " in second cache", *size);
1886 GST_DEBUG_OBJECT (trans, "no free spot to cache unit_size");
1893 gst_base_transform_sink_event (GstPad * pad, GstObject * parent,
1896 GstBaseTransform *trans;
1897 GstBaseTransformClass *bclass;
1898 gboolean ret = TRUE;
1900 trans = GST_BASE_TRANSFORM_CAST (parent);
1901 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1903 if (bclass->sink_event)
1904 ret = bclass->sink_event (trans, event);
1906 gst_event_unref (event);
1912 gst_base_transform_sink_eventfunc (GstBaseTransform * trans, GstEvent * event)
1914 gboolean ret = TRUE, forward = TRUE;
1915 GstBaseTransformPrivate *priv = trans->priv;
1917 switch (GST_EVENT_TYPE (event)) {
1918 case GST_EVENT_FLUSH_START:
1920 case GST_EVENT_FLUSH_STOP:
1921 GST_OBJECT_LOCK (trans);
1922 /* reset QoS parameters */
1923 priv->proportion = 1.0;
1924 priv->earliest_time = -1;
1925 priv->discont = FALSE;
1926 priv->processed = 0;
1928 GST_OBJECT_UNLOCK (trans);
1929 /* we need new segment info after the flush. */
1930 trans->have_segment = FALSE;
1931 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
1932 priv->position_out = GST_CLOCK_TIME_NONE;
1938 case GST_EVENT_CAPS:
1942 gst_event_parse_caps (event, &caps);
1943 /* clear any pending reconfigure flag */
1944 gst_pad_check_reconfigure (trans->srcpad);
1945 ret = gst_base_transform_setcaps (trans, trans->sinkpad, caps);
1947 gst_pad_mark_reconfigure (trans->srcpad);
1952 case GST_EVENT_SEGMENT:
1954 gst_event_copy_segment (event, &trans->segment);
1955 trans->have_segment = TRUE;
1957 GST_DEBUG_OBJECT (trans, "received SEGMENT %" GST_SEGMENT_FORMAT,
1966 ret = gst_pad_push_event (trans->srcpad, event);
1968 gst_event_unref (event);
1974 gst_base_transform_src_event (GstPad * pad, GstObject * parent,
1977 GstBaseTransform *trans;
1978 GstBaseTransformClass *bclass;
1979 gboolean ret = TRUE;
1981 trans = GST_BASE_TRANSFORM_CAST (parent);
1982 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1984 if (bclass->src_event)
1985 ret = bclass->src_event (trans, event);
1987 gst_event_unref (event);
1993 gst_base_transform_src_eventfunc (GstBaseTransform * trans, GstEvent * event)
1997 GST_DEBUG_OBJECT (trans, "handling event %p %" GST_PTR_FORMAT, event, event);
1999 switch (GST_EVENT_TYPE (event)) {
2000 case GST_EVENT_SEEK:
2002 case GST_EVENT_NAVIGATION:
2007 GstClockTimeDiff diff;
2008 GstClockTime timestamp;
2010 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
2011 gst_base_transform_update_qos (trans, proportion, diff, timestamp);
2018 ret = gst_pad_push_event (trans->sinkpad, event);
2023 /* Takes the input buffer */
2024 static GstFlowReturn
2025 default_submit_input_buffer (GstBaseTransform * trans, gboolean is_discont,
2028 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2029 GstBaseTransformPrivate *priv = trans->priv;
2030 GstFlowReturn ret = GST_FLOW_OK;
2031 GstClockTime running_time;
2032 GstClockTime timestamp;
2034 if (G_UNLIKELY (!gst_base_transform_reconfigure_unlocked (trans)))
2035 goto not_negotiated;
2037 if (GST_BUFFER_OFFSET_IS_VALID (inbuf))
2038 GST_DEBUG_OBJECT (trans,
2039 "handling buffer %p of size %" G_GSIZE_FORMAT ", PTS %" GST_TIME_FORMAT
2040 " and offset %" G_GUINT64_FORMAT, inbuf, gst_buffer_get_size (inbuf),
2041 GST_TIME_ARGS (GST_BUFFER_PTS (inbuf)), GST_BUFFER_OFFSET (inbuf));
2043 GST_DEBUG_OBJECT (trans,
2044 "handling buffer %p of size %" G_GSIZE_FORMAT ", PTS %" GST_TIME_FORMAT
2045 " and offset NONE", inbuf, gst_buffer_get_size (inbuf),
2046 GST_TIME_ARGS (GST_BUFFER_PTS (inbuf)));
2048 /* Don't allow buffer handling before negotiation, except in passthrough mode
2049 * or if the class doesn't implement a set_caps function (in which case it doesn't
2052 if (!priv->negotiated && !priv->passthrough && (bclass->set_caps != NULL))
2053 goto not_negotiated;
2055 /* can only do QoS if the segment is in TIME */
2056 if (trans->segment.format != GST_FORMAT_TIME)
2059 /* QOS is done on the running time of the buffer, get it now */
2060 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
2061 running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
2064 if (running_time != -1) {
2066 GstClockTime earliest_time;
2069 /* lock for getting the QoS parameters that are set (in a different thread)
2070 * with the QOS events */
2071 GST_OBJECT_LOCK (trans);
2072 earliest_time = priv->earliest_time;
2073 proportion = priv->proportion;
2074 /* check for QoS, don't perform conversion for buffers
2075 * that are known to be late. */
2076 need_skip = priv->qos_enabled &&
2077 earliest_time != -1 && running_time <= earliest_time;
2078 GST_OBJECT_UNLOCK (trans);
2081 GstMessage *qos_msg;
2082 GstClockTime duration;
2083 guint64 stream_time;
2086 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "skipping transform: qostime %"
2087 GST_TIME_FORMAT " <= %" GST_TIME_FORMAT,
2088 GST_TIME_ARGS (running_time), GST_TIME_ARGS (earliest_time));
2092 duration = GST_BUFFER_DURATION (inbuf);
2094 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
2096 jitter = GST_CLOCK_DIFF (running_time, earliest_time);
2099 gst_message_new_qos (GST_OBJECT_CAST (trans), FALSE, running_time,
2100 stream_time, timestamp, duration);
2101 gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
2102 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
2103 priv->processed, priv->dropped);
2104 gst_element_post_message (GST_ELEMENT_CAST (trans), qos_msg);
2106 /* mark discont for next buffer */
2107 priv->discont = TRUE;
2108 ret = GST_BASE_TRANSFORM_FLOW_DROPPED;
2114 /* Stash input buffer where the default generate_output
2115 * function can find it */
2116 if (trans->queued_buf)
2117 gst_buffer_unref (trans->queued_buf);
2118 trans->queued_buf = inbuf;
2121 gst_buffer_unref (inbuf);
2126 gst_buffer_unref (inbuf);
2127 if (GST_PAD_IS_FLUSHING (trans->srcpad))
2128 return GST_FLOW_FLUSHING;
2129 return GST_FLOW_NOT_NEGOTIATED;
2133 static GstFlowReturn
2134 default_generate_output (GstBaseTransform * trans, GstBuffer ** outbuf)
2136 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2137 GstBaseTransformPrivate *priv = trans->priv;
2138 GstFlowReturn ret = GST_FLOW_OK;
2140 gboolean want_in_place;
2142 /* Retrieve stashed input buffer, if the default submit_input_buffer
2143 * was run. Takes ownership back from there */
2144 inbuf = trans->queued_buf;
2145 trans->queued_buf = NULL;
2147 /* This default processing method needs one input buffer to feed to
2148 * the transform functions, we can't do anything without it */
2152 /* first try to allocate an output buffer based on the currently negotiated
2153 * format. outbuf will contain a buffer suitable for doing the configured
2154 * transform after this function. */
2155 if (bclass->prepare_output_buffer == NULL)
2158 GST_DEBUG_OBJECT (trans, "calling prepare buffer");
2159 ret = bclass->prepare_output_buffer (trans, inbuf, outbuf);
2161 if (ret != GST_FLOW_OK || *outbuf == NULL)
2164 GST_DEBUG_OBJECT (trans, "using allocated buffer in %p, out %p", inbuf,
2167 /* now perform the needed transform */
2168 if (priv->passthrough) {
2169 /* In passthrough mode, give transform_ip a look at the
2170 * buffer, without making it writable, or just push the
2172 if (bclass->transform_ip_on_passthrough && bclass->transform_ip) {
2173 GST_DEBUG_OBJECT (trans, "doing passthrough transform_ip");
2174 ret = bclass->transform_ip (trans, *outbuf);
2176 GST_DEBUG_OBJECT (trans, "element is in passthrough");
2179 want_in_place = (bclass->transform_ip != NULL) && priv->always_in_place;
2181 if (want_in_place) {
2182 GST_DEBUG_OBJECT (trans, "doing inplace transform");
2183 ret = bclass->transform_ip (trans, *outbuf);
2185 GST_DEBUG_OBJECT (trans, "doing non-inplace transform");
2187 if (bclass->transform)
2188 ret = bclass->transform (trans, inbuf, *outbuf);
2190 ret = GST_FLOW_NOT_SUPPORTED;
2194 /* only unref input buffer if we allocated a new outbuf buffer. If we reused
2195 * the input buffer, no refcount is changed to keep the input buffer writable
2197 if (*outbuf != inbuf)
2198 gst_buffer_unref (inbuf);
2205 gst_buffer_unref (inbuf);
2206 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
2207 ("Sub-class has no prepare_output_buffer implementation"), (NULL));
2208 return GST_FLOW_NOT_SUPPORTED;
2212 gst_buffer_unref (inbuf);
2214 GST_WARNING_OBJECT (trans, "could not get buffer from pool: %s",
2215 gst_flow_get_name (ret));
2220 /* FIXME, getrange is broken, need to pull range from the other
2221 * end based on the transform_size result.
2223 static GstFlowReturn
2224 gst_base_transform_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2225 guint length, GstBuffer ** buffer)
2227 GstBaseTransformClass *klass = GST_BASE_TRANSFORM_GET_CLASS (parent);
2228 GstBaseTransform *trans = GST_BASE_TRANSFORM_CAST (parent);
2229 GstBaseTransformPrivate *priv = trans->priv;
2231 GstBuffer *inbuf = NULL;
2232 GstBuffer *outbuf = NULL;
2234 /* Try and generate a buffer, if the sub-class wants more data,
2235 * pull some and repeat until a buffer (or error) is produced */
2237 ret = klass->generate_output (trans, &outbuf);
2239 /* Consume the DROPPED return value and go get more data */
2240 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED)
2243 if (ret != GST_FLOW_OK || outbuf != NULL)
2246 /* No buffer generated, try and pull data */
2247 ret = gst_pad_pull_range (trans->sinkpad, offset, length, &inbuf);
2248 if (G_UNLIKELY (ret != GST_FLOW_OK))
2251 if (klass->before_transform)
2252 klass->before_transform (trans, inbuf);
2254 /* Set discont flag so we can mark the next outgoing buffer */
2255 if (GST_BUFFER_IS_DISCONT (inbuf)) {
2256 GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", inbuf);
2257 priv->discont = TRUE;
2260 /* FIXME: Input offsets and lengths need to be translated, as per
2261 * the FIXME above. For now, just advance somewhat */
2262 offset += gst_buffer_get_size (inbuf);
2264 ret = klass->submit_input_buffer (trans, priv->discont, inbuf);
2265 if (ret != GST_FLOW_OK) {
2266 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED)
2270 } while (ret == GST_FLOW_OK && outbuf == NULL);
2274 /* apply DISCONT flag if the buffer is not yet marked as such */
2275 if (priv->discont) {
2276 GST_DEBUG_OBJECT (trans, "we have a pending DISCONT");
2277 if (!GST_BUFFER_IS_DISCONT (outbuf)) {
2278 GST_DEBUG_OBJECT (trans, "marking DISCONT on output buffer");
2279 outbuf = gst_buffer_make_writable (outbuf);
2280 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2282 priv->discont = FALSE;
2292 GST_DEBUG_OBJECT (trans, "failed to pull a buffer: %s",
2293 gst_flow_get_name (ret));
2298 /* The flow of the chain function is the reverse of the
2299 * getrange() function - we have data, feed it to the sub-class
2300 * and then iterate, pushing buffers it generates until it either
2301 * wants more data or returns an error */
2302 static GstFlowReturn
2303 gst_base_transform_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2305 GstBaseTransform *trans = GST_BASE_TRANSFORM_CAST (parent);
2306 GstBaseTransformClass *klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2307 GstBaseTransformPrivate *priv = trans->priv;
2309 GstClockTime position = GST_CLOCK_TIME_NONE;
2310 GstClockTime timestamp, duration;
2311 GstBuffer *outbuf = NULL;
2313 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2314 duration = GST_BUFFER_DURATION (buffer);
2316 /* calculate end position of the incoming buffer */
2317 if (timestamp != GST_CLOCK_TIME_NONE) {
2318 if (duration != GST_CLOCK_TIME_NONE)
2319 position = timestamp + duration;
2321 position = timestamp;
2324 if (klass->before_transform)
2325 klass->before_transform (trans, buffer);
2327 /* Set discont flag so we can mark the outgoing buffer */
2328 if (GST_BUFFER_IS_DISCONT (buffer)) {
2329 GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", buffer);
2330 priv->discont = TRUE;
2333 /* Takes ownership of input buffer */
2334 ret = klass->submit_input_buffer (trans, priv->discont, buffer);
2335 if (ret != GST_FLOW_OK)
2341 ret = klass->generate_output (trans, &outbuf);
2343 /* outbuf can be NULL, this means a dropped buffer, if we have a buffer but
2344 * GST_BASE_TRANSFORM_FLOW_DROPPED we will not push either. */
2345 if (outbuf != NULL) {
2346 if (ret == GST_FLOW_OK) {
2347 GstClockTime position_out = GST_CLOCK_TIME_NONE;
2349 /* Remember last stop position */
2350 if (position != GST_CLOCK_TIME_NONE &&
2351 trans->segment.format == GST_FORMAT_TIME)
2352 trans->segment.position = position;
2354 if (GST_BUFFER_TIMESTAMP_IS_VALID (outbuf)) {
2355 position_out = GST_BUFFER_TIMESTAMP (outbuf);
2356 if (GST_BUFFER_DURATION_IS_VALID (outbuf))
2357 position_out += GST_BUFFER_DURATION (outbuf);
2358 } else if (position != GST_CLOCK_TIME_NONE) {
2359 position_out = position;
2361 if (position_out != GST_CLOCK_TIME_NONE
2362 && trans->segment.format == GST_FORMAT_TIME)
2363 priv->position_out = position_out;
2365 /* apply DISCONT flag if the buffer is not yet marked as such */
2366 if (trans->priv->discont) {
2367 GST_DEBUG_OBJECT (trans, "we have a pending DISCONT");
2368 if (!GST_BUFFER_IS_DISCONT (outbuf)) {
2369 GST_DEBUG_OBJECT (trans, "marking DISCONT on output buffer");
2370 outbuf = gst_buffer_make_writable (outbuf);
2371 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2373 priv->discont = FALSE;
2377 ret = gst_pad_push (trans->srcpad, outbuf);
2379 GST_DEBUG_OBJECT (trans, "we got return %s", gst_flow_get_name (ret));
2380 gst_buffer_unref (outbuf);
2383 } while (ret == GST_FLOW_OK && outbuf != NULL);
2386 /* convert internal flow to OK and mark discont for the next buffer. */
2387 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED) {
2388 GST_DEBUG_OBJECT (trans, "dropped a buffer, marking DISCONT");
2389 priv->discont = TRUE;
2397 gst_base_transform_set_property (GObject * object, guint prop_id,
2398 const GValue * value, GParamSpec * pspec)
2400 GstBaseTransform *trans;
2402 trans = GST_BASE_TRANSFORM_CAST (object);
2406 gst_base_transform_set_qos_enabled (trans, g_value_get_boolean (value));
2409 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2415 gst_base_transform_get_property (GObject * object, guint prop_id,
2416 GValue * value, GParamSpec * pspec)
2418 GstBaseTransform *trans;
2420 trans = GST_BASE_TRANSFORM_CAST (object);
2424 g_value_set_boolean (value, gst_base_transform_is_qos_enabled (trans));
2427 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2432 /* not a vmethod of anything, just an internal method */
2434 gst_base_transform_activate (GstBaseTransform * trans, gboolean active)
2436 GstBaseTransformClass *bclass;
2437 GstBaseTransformPrivate *priv = trans->priv;
2438 gboolean result = TRUE;
2440 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2443 GstCaps *incaps, *outcaps;
2445 if (priv->pad_mode == GST_PAD_MODE_NONE && bclass->start)
2446 result &= bclass->start (trans);
2448 incaps = gst_pad_get_current_caps (trans->sinkpad);
2449 outcaps = gst_pad_get_current_caps (trans->srcpad);
2451 GST_OBJECT_LOCK (trans);
2452 if (incaps && outcaps)
2453 priv->have_same_caps =
2454 gst_caps_is_equal (incaps, outcaps) || priv->passthrough;
2456 priv->have_same_caps = priv->passthrough;
2457 GST_DEBUG_OBJECT (trans, "have_same_caps %d", priv->have_same_caps);
2458 priv->negotiated = FALSE;
2459 trans->have_segment = FALSE;
2460 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
2461 priv->position_out = GST_CLOCK_TIME_NONE;
2462 priv->proportion = 1.0;
2463 priv->earliest_time = -1;
2464 priv->discont = FALSE;
2465 priv->processed = 0;
2467 GST_OBJECT_UNLOCK (trans);
2470 gst_caps_unref (incaps);
2472 gst_caps_unref (outcaps);
2474 /* We must make sure streaming has finished before resetting things
2475 * and calling the ::stop vfunc */
2476 GST_PAD_STREAM_LOCK (trans->sinkpad);
2477 GST_PAD_STREAM_UNLOCK (trans->sinkpad);
2479 priv->have_same_caps = FALSE;
2480 /* We can only reset the passthrough mode if the instance told us to
2481 handle it in configure_caps */
2482 if (bclass->passthrough_on_same_caps) {
2483 gst_base_transform_set_passthrough (trans, FALSE);
2485 gst_caps_replace (&priv->cache_caps1, NULL);
2486 gst_caps_replace (&priv->cache_caps2, NULL);
2488 /* Make sure any left over buffer is freed */
2489 gst_buffer_replace (&trans->queued_buf, NULL);
2491 if (priv->pad_mode != GST_PAD_MODE_NONE && bclass->stop)
2492 result &= bclass->stop (trans);
2494 gst_base_transform_set_allocation (trans, NULL, NULL, NULL, NULL);
2501 gst_base_transform_sink_activate_mode (GstPad * pad, GstObject * parent,
2502 GstPadMode mode, gboolean active)
2504 gboolean result = FALSE;
2505 GstBaseTransform *trans;
2507 trans = GST_BASE_TRANSFORM_CAST (parent);
2510 case GST_PAD_MODE_PUSH:
2512 result = gst_base_transform_activate (trans, active);
2515 trans->priv->pad_mode = active ? GST_PAD_MODE_PUSH : GST_PAD_MODE_NONE;
2527 gst_base_transform_src_activate_mode (GstPad * pad, GstObject * parent,
2528 GstPadMode mode, gboolean active)
2530 gboolean result = FALSE;
2531 GstBaseTransform *trans;
2533 trans = GST_BASE_TRANSFORM_CAST (parent);
2536 case GST_PAD_MODE_PULL:
2539 gst_pad_activate_mode (trans->sinkpad, GST_PAD_MODE_PULL, active);
2542 result &= gst_base_transform_activate (trans, active);
2545 trans->priv->pad_mode = active ? mode : GST_PAD_MODE_NONE;
2557 * gst_base_transform_set_passthrough:
2558 * @trans: the #GstBaseTransform to set
2559 * @passthrough: boolean indicating passthrough mode.
2561 * Set passthrough mode for this filter by default. This is mostly
2562 * useful for filters that do not care about negotiation.
2564 * Always %TRUE for filters which don't implement either a transform
2565 * or transform_ip or generate_output method.
2570 gst_base_transform_set_passthrough (GstBaseTransform * trans,
2571 gboolean passthrough)
2573 GstBaseTransformClass *bclass;
2575 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2577 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2579 GST_OBJECT_LOCK (trans);
2581 if (bclass->transform_ip || bclass->transform || (bclass->generate_output
2582 && bclass->generate_output != default_generate_output))
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 if 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 * * Always %TRUE if no transform function is implemented.
2626 * * Always %FALSE if ONLY transform function is implemented.
2631 gst_base_transform_set_in_place (GstBaseTransform * trans, gboolean in_place)
2633 GstBaseTransformClass *bclass;
2635 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2637 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2639 GST_OBJECT_LOCK (trans);
2642 if (bclass->transform_ip) {
2643 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
2644 trans->priv->always_in_place = TRUE;
2647 if (bclass->transform) {
2648 GST_DEBUG_OBJECT (trans, "setting in_place FALSE");
2649 trans->priv->always_in_place = FALSE;
2653 GST_OBJECT_UNLOCK (trans);
2657 * gst_base_transform_is_in_place:
2658 * @trans: the #GstBaseTransform to query
2660 * See if @trans is configured as a in_place transform.
2662 * Returns: %TRUE if the transform is configured in in_place mode.
2667 gst_base_transform_is_in_place (GstBaseTransform * trans)
2671 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2673 GST_OBJECT_LOCK (trans);
2674 result = trans->priv->always_in_place;
2675 GST_OBJECT_UNLOCK (trans);
2681 * gst_base_transform_update_qos:
2682 * @trans: a #GstBaseTransform
2683 * @proportion: the proportion
2684 * @diff: the diff against the clock
2685 * @timestamp: the timestamp of the buffer generating the QoS expressed in
2688 * Set the QoS parameters in the transform. This function is called internally
2689 * when a QOS event is received but subclasses can provide custom information
2695 gst_base_transform_update_qos (GstBaseTransform * trans,
2696 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2698 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2700 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans,
2701 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2702 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2704 GST_OBJECT_LOCK (trans);
2705 trans->priv->proportion = proportion;
2706 trans->priv->earliest_time = timestamp + diff;
2707 GST_OBJECT_UNLOCK (trans);
2711 * gst_base_transform_set_qos_enabled:
2712 * @trans: a #GstBaseTransform
2713 * @enabled: new state
2715 * Enable or disable QoS handling in the transform.
2720 gst_base_transform_set_qos_enabled (GstBaseTransform * trans, gboolean enabled)
2722 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2724 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "enabled: %d", enabled);
2726 GST_OBJECT_LOCK (trans);
2727 trans->priv->qos_enabled = enabled;
2728 GST_OBJECT_UNLOCK (trans);
2732 * gst_base_transform_is_qos_enabled:
2733 * @trans: a #GstBaseTransform
2735 * Queries if the transform will handle QoS.
2737 * Returns: %TRUE if QoS is enabled.
2742 gst_base_transform_is_qos_enabled (GstBaseTransform * trans)
2746 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2748 GST_OBJECT_LOCK (trans);
2749 result = trans->priv->qos_enabled;
2750 GST_OBJECT_UNLOCK (trans);
2756 * gst_base_transform_set_gap_aware:
2757 * @trans: a #GstBaseTransform
2758 * @gap_aware: New state
2760 * If @gap_aware is %FALSE (the default), output buffers will have the
2761 * %GST_BUFFER_FLAG_GAP flag unset.
2763 * If set to %TRUE, the element must handle output buffers with this flag set
2764 * correctly, i.e. it can assume that the buffer contains neutral data but must
2765 * unset the flag if the output is no neutral data.
2770 gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware)
2772 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2774 GST_OBJECT_LOCK (trans);
2775 trans->priv->gap_aware = gap_aware;
2776 GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware);
2777 GST_OBJECT_UNLOCK (trans);
2781 * gst_base_transform_set_prefer_passthrough:
2782 * @trans: a #GstBaseTransform
2783 * @prefer_passthrough: New state
2785 * If @prefer_passthrough is %TRUE (the default), @trans will check and
2786 * prefer passthrough caps from the list of caps returned by the
2787 * transform_caps vmethod.
2789 * If set to %FALSE, the element must order the caps returned from the
2790 * transform_caps function in such a way that the preferred format is
2791 * first in the list. This can be interesting for transforms that can do
2792 * passthrough transforms but prefer to do something else, like a
2800 gst_base_transform_set_prefer_passthrough (GstBaseTransform * trans,
2801 gboolean prefer_passthrough)
2803 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2805 GST_OBJECT_LOCK (trans);
2806 trans->priv->prefer_passthrough = prefer_passthrough;
2807 GST_DEBUG_OBJECT (trans, "prefer passthrough %d", prefer_passthrough);
2808 GST_OBJECT_UNLOCK (trans);
2812 * gst_base_transform_reconfigure_sink:
2813 * @trans: a #GstBaseTransform
2815 * Instructs @trans to request renegotiation upstream. This function is
2816 * typically called after properties on the transform were set that
2817 * influence the input format.
2820 gst_base_transform_reconfigure_sink (GstBaseTransform * trans)
2822 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2824 /* push the renegotiate event */
2825 if (!gst_pad_push_event (GST_BASE_TRANSFORM_SINK_PAD (trans),
2826 gst_event_new_reconfigure ()))
2827 GST_DEBUG_OBJECT (trans, "Renegotiate event wasn't handled");
2831 * gst_base_transform_reconfigure_src:
2832 * @trans: a #GstBaseTransform
2834 * Instructs @trans to renegotiate a new downstream transform on the next
2835 * buffer. This function is typically called after properties on the transform
2836 * were set that influence the output format.
2839 gst_base_transform_reconfigure_src (GstBaseTransform * trans)
2841 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2843 gst_pad_mark_reconfigure (trans->srcpad);
2847 * gst_base_transform_get_buffer_pool:
2848 * @trans: a #GstBaseTransform
2850 * Returns: (nullable) (transfer full): the instance of the #GstBufferPool used
2851 * by @trans; free it after use
2854 gst_base_transform_get_buffer_pool (GstBaseTransform * trans)
2856 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), NULL);
2858 if (trans->priv->pool)
2859 return gst_object_ref (trans->priv->pool);
2865 * gst_base_transform_get_allocator:
2866 * @trans: a #GstBaseTransform
2867 * @allocator: (out) (optional) (nullable) (transfer full): the #GstAllocator
2869 * @params: (out caller-allocates) (optional): the #GstAllocationParams of @allocator
2871 * Lets #GstBaseTransform sub-classes know the memory @allocator
2872 * used by the base class and its @params.
2874 * Unref the @allocator after use.
2877 gst_base_transform_get_allocator (GstBaseTransform * trans,
2878 GstAllocator ** allocator, GstAllocationParams * params)
2880 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2883 *allocator = trans->priv->allocator ?
2884 gst_object_ref (trans->priv->allocator) : NULL;
2887 *params = trans->priv->params;
2891 * gst_base_transform_update_src_caps:
2892 * @trans: a #GstBaseTransform
2893 * @updated_caps: An updated version of the srcpad caps to be pushed
2896 * Updates the srcpad caps and sends the caps downstream. This function
2897 * can be used by subclasses when they have already negotiated their caps
2898 * but found a change in them (or computed new information). This way,
2899 * they can notify downstream about that change without losing any
2902 * Returns: %TRUE if the caps could be sent downstream %FALSE otherwise
2907 gst_base_transform_update_src_caps (GstBaseTransform * trans,
2908 GstCaps * updated_caps)
2910 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2912 if (gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (trans),
2913 gst_event_new_caps (updated_caps))) {
2914 gst_pad_mark_reconfigure (trans->srcpad);