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 #define GST_BASE_TRANSFORM_GET_PRIVATE(obj) \
167 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_TRANSFORM, GstBaseTransformPrivate))
169 struct _GstBaseTransformPrivate
171 /* Set by sub-class */
172 gboolean passthrough;
173 gboolean always_in_place;
175 GstCaps *cache_caps1;
176 gsize cache_caps1_size;
177 GstCaps *cache_caps2;
178 gsize cache_caps2_size;
179 gboolean have_same_caps;
183 /* QoS *//* with LOCK */
184 gboolean qos_enabled;
186 GstClockTime earliest_time;
187 /* previous buffer had a discont */
193 gboolean prefer_passthrough;
199 GstClockTime position_out;
202 gboolean pool_active;
203 GstAllocator *allocator;
204 GstAllocationParams params;
209 static GstElementClass *parent_class = NULL;
211 static void gst_base_transform_class_init (GstBaseTransformClass * klass);
212 static void gst_base_transform_init (GstBaseTransform * trans,
213 GstBaseTransformClass * klass);
214 static GstFlowReturn default_submit_input_buffer (GstBaseTransform * trans,
215 gboolean is_discont, GstBuffer * input);
216 static GstFlowReturn default_generate_output (GstBaseTransform * trans,
217 GstBuffer ** outbuf);
219 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
220 * method to get to the padtemplates */
222 gst_base_transform_get_type (void)
224 static volatile gsize base_transform_type = 0;
226 if (g_once_init_enter (&base_transform_type)) {
228 static const GTypeInfo base_transform_info = {
229 sizeof (GstBaseTransformClass),
232 (GClassInitFunc) gst_base_transform_class_init,
235 sizeof (GstBaseTransform),
237 (GInstanceInitFunc) gst_base_transform_init,
240 _type = g_type_register_static (GST_TYPE_ELEMENT,
241 "GstBaseTransform", &base_transform_info, G_TYPE_FLAG_ABSTRACT);
242 g_once_init_leave (&base_transform_type, _type);
244 return base_transform_type;
247 static void gst_base_transform_finalize (GObject * object);
248 static void gst_base_transform_set_property (GObject * object, guint prop_id,
249 const GValue * value, GParamSpec * pspec);
250 static void gst_base_transform_get_property (GObject * object, guint prop_id,
251 GValue * value, GParamSpec * pspec);
252 static gboolean gst_base_transform_src_activate_mode (GstPad * pad,
253 GstObject * parent, GstPadMode mode, gboolean active);
254 static gboolean gst_base_transform_sink_activate_mode (GstPad * pad,
255 GstObject * parent, GstPadMode mode, gboolean active);
256 static gboolean gst_base_transform_activate (GstBaseTransform * trans,
258 static gboolean gst_base_transform_get_unit_size (GstBaseTransform * trans,
259 GstCaps * caps, gsize * size);
261 static gboolean gst_base_transform_src_event (GstPad * pad, GstObject * parent,
263 static gboolean gst_base_transform_src_eventfunc (GstBaseTransform * trans,
265 static gboolean gst_base_transform_sink_event (GstPad * pad, GstObject * parent,
267 static gboolean gst_base_transform_sink_eventfunc (GstBaseTransform * trans,
269 static GstFlowReturn gst_base_transform_getrange (GstPad * pad,
270 GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
271 static GstFlowReturn gst_base_transform_chain (GstPad * pad, GstObject * parent,
273 static GstCaps *gst_base_transform_default_transform_caps (GstBaseTransform *
274 trans, GstPadDirection direction, GstCaps * caps, GstCaps * filter);
275 static GstCaps *gst_base_transform_default_fixate_caps (GstBaseTransform *
276 trans, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
277 static GstCaps *gst_base_transform_query_caps (GstBaseTransform * trans,
278 GstPad * pad, GstCaps * filter);
279 static gboolean gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
280 GstPadDirection direction, GstCaps * caps);
281 static gboolean gst_base_transform_setcaps (GstBaseTransform * trans,
282 GstPad * pad, GstCaps * caps);
283 static gboolean gst_base_transform_default_decide_allocation (GstBaseTransform
284 * trans, GstQuery * query);
285 static gboolean gst_base_transform_default_propose_allocation (GstBaseTransform
286 * trans, GstQuery * decide_query, GstQuery * query);
287 static gboolean gst_base_transform_query (GstPad * pad, GstObject * parent,
289 static gboolean gst_base_transform_default_query (GstBaseTransform * trans,
290 GstPadDirection direction, GstQuery * query);
291 static gboolean gst_base_transform_default_transform_size (GstBaseTransform *
292 trans, GstPadDirection direction, GstCaps * caps, gsize size,
293 GstCaps * othercaps, gsize * othersize);
295 static GstFlowReturn default_prepare_output_buffer (GstBaseTransform * trans,
296 GstBuffer * inbuf, GstBuffer ** outbuf);
297 static gboolean default_copy_metadata (GstBaseTransform * trans,
298 GstBuffer * inbuf, GstBuffer * outbuf);
300 gst_base_transform_default_transform_meta (GstBaseTransform * trans,
301 GstBuffer * inbuf, GstMeta * meta, GstBuffer * outbuf);
303 /* static guint gst_base_transform_signals[LAST_SIGNAL] = { 0 }; */
307 gst_base_transform_finalize (GObject * object)
309 G_OBJECT_CLASS (parent_class)->finalize (object);
313 gst_base_transform_class_init (GstBaseTransformClass * klass)
315 GObjectClass *gobject_class;
317 gobject_class = G_OBJECT_CLASS (klass);
319 GST_DEBUG_CATEGORY_INIT (gst_base_transform_debug, "basetransform", 0,
320 "basetransform element");
322 GST_DEBUG ("gst_base_transform_class_init");
324 g_type_class_add_private (klass, sizeof (GstBaseTransformPrivate));
326 parent_class = g_type_class_peek_parent (klass);
328 gobject_class->set_property = gst_base_transform_set_property;
329 gobject_class->get_property = gst_base_transform_get_property;
331 g_object_class_install_property (gobject_class, PROP_QOS,
332 g_param_spec_boolean ("qos", "QoS", "Handle Quality-of-Service events",
333 DEFAULT_PROP_QOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 gobject_class->finalize = gst_base_transform_finalize;
337 klass->passthrough_on_same_caps = FALSE;
338 klass->transform_ip_on_passthrough = TRUE;
340 klass->transform_caps =
341 GST_DEBUG_FUNCPTR (gst_base_transform_default_transform_caps);
343 GST_DEBUG_FUNCPTR (gst_base_transform_default_fixate_caps);
345 GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps_default);
346 klass->query = GST_DEBUG_FUNCPTR (gst_base_transform_default_query);
347 klass->decide_allocation =
348 GST_DEBUG_FUNCPTR (gst_base_transform_default_decide_allocation);
349 klass->propose_allocation =
350 GST_DEBUG_FUNCPTR (gst_base_transform_default_propose_allocation);
351 klass->transform_size =
352 GST_DEBUG_FUNCPTR (gst_base_transform_default_transform_size);
353 klass->transform_meta =
354 GST_DEBUG_FUNCPTR (gst_base_transform_default_transform_meta);
356 klass->sink_event = GST_DEBUG_FUNCPTR (gst_base_transform_sink_eventfunc);
357 klass->src_event = GST_DEBUG_FUNCPTR (gst_base_transform_src_eventfunc);
358 klass->prepare_output_buffer =
359 GST_DEBUG_FUNCPTR (default_prepare_output_buffer);
360 klass->copy_metadata = GST_DEBUG_FUNCPTR (default_copy_metadata);
361 klass->submit_input_buffer = GST_DEBUG_FUNCPTR (default_submit_input_buffer);
362 klass->generate_output = GST_DEBUG_FUNCPTR (default_generate_output);
366 gst_base_transform_init (GstBaseTransform * trans,
367 GstBaseTransformClass * bclass)
369 GstPadTemplate *pad_template;
370 GstBaseTransformPrivate *priv;
372 GST_DEBUG ("gst_base_transform_init");
374 priv = trans->priv = GST_BASE_TRANSFORM_GET_PRIVATE (trans);
377 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
378 g_return_if_fail (pad_template != NULL);
379 trans->sinkpad = gst_pad_new_from_template (pad_template, "sink");
380 gst_pad_set_event_function (trans->sinkpad,
381 GST_DEBUG_FUNCPTR (gst_base_transform_sink_event));
382 gst_pad_set_chain_function (trans->sinkpad,
383 GST_DEBUG_FUNCPTR (gst_base_transform_chain));
384 gst_pad_set_activatemode_function (trans->sinkpad,
385 GST_DEBUG_FUNCPTR (gst_base_transform_sink_activate_mode));
386 gst_pad_set_query_function (trans->sinkpad,
387 GST_DEBUG_FUNCPTR (gst_base_transform_query));
388 gst_element_add_pad (GST_ELEMENT (trans), trans->sinkpad);
391 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
392 g_return_if_fail (pad_template != NULL);
393 trans->srcpad = gst_pad_new_from_template (pad_template, "src");
394 gst_pad_set_event_function (trans->srcpad,
395 GST_DEBUG_FUNCPTR (gst_base_transform_src_event));
396 gst_pad_set_getrange_function (trans->srcpad,
397 GST_DEBUG_FUNCPTR (gst_base_transform_getrange));
398 gst_pad_set_activatemode_function (trans->srcpad,
399 GST_DEBUG_FUNCPTR (gst_base_transform_src_activate_mode));
400 gst_pad_set_query_function (trans->srcpad,
401 GST_DEBUG_FUNCPTR (gst_base_transform_query));
402 gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
404 priv->qos_enabled = DEFAULT_PROP_QOS;
405 priv->cache_caps1 = NULL;
406 priv->cache_caps2 = NULL;
407 priv->pad_mode = GST_PAD_MODE_NONE;
408 priv->gap_aware = FALSE;
409 priv->prefer_passthrough = TRUE;
411 priv->passthrough = FALSE;
412 if (bclass->transform == NULL) {
413 /* If no transform function, always_in_place is TRUE */
414 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
415 priv->always_in_place = TRUE;
417 if (bclass->transform_ip == NULL) {
418 GST_DEBUG_OBJECT (trans, "setting passthrough TRUE");
419 priv->passthrough = TRUE;
428 gst_base_transform_default_transform_caps (GstBaseTransform * trans,
429 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
433 GST_DEBUG_OBJECT (trans, "identity from: %" GST_PTR_FORMAT, caps);
434 /* no transform function, use the identity transform */
436 ret = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
438 ret = gst_caps_ref (caps);
443 /* given @caps on the src or sink pad (given by @direction)
444 * calculate the possible caps on the other pad.
446 * Returns new caps, unref after usage.
449 gst_base_transform_transform_caps (GstBaseTransform * trans,
450 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
453 GstBaseTransformClass *klass;
458 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
460 /* if there is a custom transform function, use this */
461 if (klass->transform_caps) {
462 GST_DEBUG_OBJECT (trans, "transform caps (direction = %d)", direction);
464 GST_LOG_OBJECT (trans, "from: %" GST_PTR_FORMAT, caps);
465 ret = klass->transform_caps (trans, direction, caps, filter);
466 GST_LOG_OBJECT (trans, " to: %" GST_PTR_FORMAT, ret);
468 #ifdef GST_ENABLE_EXTRA_CHECKS
470 if (!gst_caps_is_subset (ret, filter)) {
471 GstCaps *intersection;
473 GST_ERROR_OBJECT (trans,
474 "transform_caps returned caps %" GST_PTR_FORMAT
475 " which are not a real subset of the filter caps %"
476 GST_PTR_FORMAT, ret, filter);
477 g_warning ("%s: transform_caps returned caps which are not a real "
478 "subset of the filter caps", GST_ELEMENT_NAME (trans));
481 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
482 gst_caps_unref (ret);
489 GST_DEBUG_OBJECT (trans, "to: %" GST_PTR_FORMAT, ret);
495 gst_base_transform_default_transform_meta (GstBaseTransform * trans,
496 GstBuffer * inbuf, GstMeta * meta, GstBuffer * outbuf)
498 const GstMetaInfo *info = meta->info;
499 const gchar *const *tags;
501 tags = gst_meta_api_type_get_tags (info->api);
510 gst_base_transform_default_transform_size (GstBaseTransform * trans,
511 GstPadDirection direction, GstCaps * caps, gsize size,
512 GstCaps * othercaps, gsize * othersize)
514 gsize inunitsize, outunitsize, units;
515 GstBaseTransformClass *klass;
517 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
519 if (klass->get_unit_size == NULL) {
520 /* if there is no transform_size and no unit_size, it means the
521 * element does not modify the size of a buffer */
524 /* there is no transform_size function, we have to use the unit_size
525 * functions. This method assumes there is a fixed unit_size associated with
526 * each caps. We provide the same amount of units on both sides. */
527 if (!gst_base_transform_get_unit_size (trans, caps, &inunitsize))
530 GST_DEBUG_OBJECT (trans,
531 "input size %" G_GSIZE_FORMAT ", input unit size %" G_GSIZE_FORMAT,
534 /* input size must be a multiple of the unit_size of the input caps */
535 if (inunitsize == 0 || (size % inunitsize != 0))
538 /* get the amount of units */
539 units = size / inunitsize;
541 /* now get the unit size of the output */
542 if (!gst_base_transform_get_unit_size (trans, othercaps, &outunitsize))
545 /* the output size is the unit_size times the amount of units on the
547 *othersize = units * outunitsize;
548 GST_DEBUG_OBJECT (trans, "transformed size to %" G_GSIZE_FORMAT,
556 GST_DEBUG_OBJECT (trans, "could not get in_size");
557 g_warning ("%s: could not get in_size", GST_ELEMENT_NAME (trans));
562 GST_DEBUG_OBJECT (trans, "Size %" G_GSIZE_FORMAT " is not a multiple of"
563 "unit size %" G_GSIZE_FORMAT, size, inunitsize);
564 g_warning ("%s: size %" G_GSIZE_FORMAT " is not a multiple of unit size %"
565 G_GSIZE_FORMAT, GST_ELEMENT_NAME (trans), size, inunitsize);
570 GST_DEBUG_OBJECT (trans, "could not get out_size");
571 g_warning ("%s: could not get out_size", GST_ELEMENT_NAME (trans));
576 /* transform a buffer of @size with @caps on the pad with @direction to
577 * the size of a buffer with @othercaps and store the result in @othersize
579 * We have two ways of doing this:
580 * 1) use a custom transform size function, this is for complicated custom
581 * cases with no fixed unit_size.
582 * 2) use the unit_size functions where there is a relationship between the
583 * caps and the size of a buffer.
586 gst_base_transform_transform_size (GstBaseTransform * trans,
587 GstPadDirection direction, GstCaps * caps,
588 gsize size, GstCaps * othercaps, gsize * othersize)
590 GstBaseTransformClass *klass;
591 gboolean ret = FALSE;
593 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
595 GST_DEBUG_OBJECT (trans,
596 "asked to transform size %" G_GSIZE_FORMAT " for caps %"
597 GST_PTR_FORMAT " to size for caps %" GST_PTR_FORMAT " in direction %s",
598 size, caps, othercaps, direction == GST_PAD_SRC ? "SRC" : "SINK");
600 if (klass->transform_size) {
601 /* if there is a custom transform function, use this */
602 ret = klass->transform_size (trans, direction, caps, size, othercaps,
608 /* get the caps that can be handled by @pad. We perform:
610 * - take the caps of peer of otherpad,
611 * - filter against the padtemplate of otherpad,
612 * - calculate all transforms of remaining caps
613 * - filter against template of @pad
615 * If there is no peer, we simply return the caps of the padtemplate of pad.
618 gst_base_transform_query_caps (GstBaseTransform * trans, GstPad * pad,
622 GstCaps *peercaps = NULL, *caps, *temp, *peerfilter = NULL;
623 GstCaps *templ, *otempl;
625 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
627 templ = gst_pad_get_pad_template_caps (pad);
628 otempl = gst_pad_get_pad_template_caps (otherpad);
630 /* first prepare the filter to be send onwards. We need to filter and
631 * transform it to valid caps for the otherpad. */
633 GST_DEBUG_OBJECT (pad, "filter caps %" GST_PTR_FORMAT, filter);
635 /* filtered against our padtemplate of this pad */
636 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
637 temp = gst_caps_intersect_full (filter, templ, GST_CAPS_INTERSECT_FIRST);
638 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
640 /* then see what we can transform this to */
641 peerfilter = gst_base_transform_transform_caps (trans,
642 GST_PAD_DIRECTION (pad), temp, NULL);
643 GST_DEBUG_OBJECT (pad, "transformed %" GST_PTR_FORMAT, peerfilter);
644 gst_caps_unref (temp);
646 if (!gst_caps_is_empty (peerfilter)) {
647 /* and filter against the template of the other pad */
648 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, otempl);
649 /* We keep the caps sorted like the returned caps */
651 gst_caps_intersect_full (peerfilter, otempl,
652 GST_CAPS_INTERSECT_FIRST);
653 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
654 gst_caps_unref (peerfilter);
659 GST_DEBUG_OBJECT (pad, "peer filter caps %" GST_PTR_FORMAT, peerfilter);
661 if (peerfilter && gst_caps_is_empty (peerfilter)) {
662 GST_DEBUG_OBJECT (pad, "peer filter caps are empty");
668 /* query the peer with the transformed filter */
669 peercaps = gst_pad_peer_query_caps (otherpad, peerfilter);
672 gst_caps_unref (peerfilter);
675 GST_DEBUG_OBJECT (pad, "peer caps %" GST_PTR_FORMAT, peercaps);
677 /* filtered against our padtemplate on the other side */
678 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, otempl);
679 temp = gst_caps_intersect_full (peercaps, otempl, GST_CAPS_INTERSECT_FIRST);
680 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
682 temp = gst_caps_ref (otempl);
685 /* then see what we can transform this to */
686 caps = gst_base_transform_transform_caps (trans,
687 GST_PAD_DIRECTION (otherpad), temp, filter);
688 GST_DEBUG_OBJECT (pad, "transformed %" GST_PTR_FORMAT, caps);
689 gst_caps_unref (temp);
690 if (caps == NULL || gst_caps_is_empty (caps))
694 /* and filter against the template of this pad */
695 GST_DEBUG_OBJECT (pad, "our template %" GST_PTR_FORMAT, templ);
696 /* We keep the caps sorted like the returned caps */
697 temp = gst_caps_intersect_full (caps, templ, GST_CAPS_INTERSECT_FIRST);
698 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
699 gst_caps_unref (caps);
702 if (trans->priv->prefer_passthrough) {
703 /* Now try if we can put the untransformed downstream caps first */
704 temp = gst_caps_intersect_full (peercaps, caps, GST_CAPS_INTERSECT_FIRST);
705 if (!gst_caps_is_empty (temp)) {
706 caps = gst_caps_merge (temp, caps);
708 gst_caps_unref (temp);
712 gst_caps_unref (caps);
713 /* no peer or the peer can do anything, our padtemplate is enough then */
715 caps = gst_caps_intersect_full (filter, templ, GST_CAPS_INTERSECT_FIRST);
717 caps = gst_caps_ref (templ);
722 GST_DEBUG_OBJECT (trans, "returning %" GST_PTR_FORMAT, caps);
725 gst_caps_unref (peercaps);
727 gst_caps_unref (templ);
728 gst_caps_unref (otempl);
733 /* takes ownership of the pool, allocator and query */
735 gst_base_transform_set_allocation (GstBaseTransform * trans,
736 GstBufferPool * pool, GstAllocator * allocator,
737 GstAllocationParams * params, GstQuery * query)
739 GstAllocator *oldalloc;
740 GstBufferPool *oldpool;
742 GstBaseTransformPrivate *priv = trans->priv;
744 GST_OBJECT_LOCK (trans);
745 oldpool = priv->pool;
747 priv->pool_active = FALSE;
749 oldalloc = priv->allocator;
750 priv->allocator = allocator;
752 oldquery = priv->query;
756 priv->params = *params;
758 gst_allocation_params_init (&priv->params);
759 GST_OBJECT_UNLOCK (trans);
762 GST_DEBUG_OBJECT (trans, "deactivating old pool %p", oldpool);
763 gst_buffer_pool_set_active (oldpool, FALSE);
764 gst_object_unref (oldpool);
767 gst_object_unref (oldalloc);
770 gst_query_unref (oldquery);
776 gst_base_transform_default_decide_allocation (GstBaseTransform * trans,
780 GstBaseTransformClass *klass;
783 guint size, min, max;
784 GstAllocator *allocator;
785 GstAllocationParams params;
786 GstStructure *config;
787 gboolean update_allocator;
789 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
791 n_metas = gst_query_get_n_allocation_metas (query);
792 for (i = 0; i < n_metas; i++) {
794 const GstStructure *params;
797 api = gst_query_parse_nth_allocation_meta (query, i, ¶ms);
799 /* by default we remove all metadata, subclasses should implement a
800 * filter_meta function */
801 if (gst_meta_api_type_has_tag (api, _gst_meta_tag_memory)) {
802 /* remove all memory dependent metadata because we are going to have to
803 * allocate different memory for input and output. */
804 GST_LOG_OBJECT (trans, "removing memory specific metadata %s",
807 } else if (G_LIKELY (klass->filter_meta)) {
808 /* remove if the subclass said so */
809 remove = !klass->filter_meta (trans, query, api, params);
810 GST_LOG_OBJECT (trans, "filter_meta for api %s returned: %s",
811 g_type_name (api), (remove ? "remove" : "keep"));
813 GST_LOG_OBJECT (trans, "removing metadata %s", g_type_name (api));
818 gst_query_remove_nth_allocation_meta (query, i);
824 gst_query_parse_allocation (query, &outcaps, NULL);
826 /* we got configuration from our peer or the decide_allocation method,
828 if (gst_query_get_n_allocation_params (query) > 0) {
829 /* try the allocator */
830 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
831 update_allocator = TRUE;
834 gst_allocation_params_init (¶ms);
835 update_allocator = FALSE;
838 if (gst_query_get_n_allocation_pools (query) > 0) {
839 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
842 /* no pool, we can make our own */
843 GST_DEBUG_OBJECT (trans, "no pool, making new pool");
844 pool = gst_buffer_pool_new ();
848 size = min = max = 0;
853 config = gst_buffer_pool_get_config (pool);
854 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
855 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
857 /* buffer pool may have to do some changes */
858 if (!gst_buffer_pool_set_config (pool, config)) {
859 config = gst_buffer_pool_get_config (pool);
861 /* If change are not acceptable, fallback to generic pool */
862 if (!gst_buffer_pool_config_validate_params (config, outcaps, size, min,
864 GST_DEBUG_OBJECT (trans, "unsuported pool, making new pool");
866 gst_object_unref (pool);
867 pool = gst_buffer_pool_new ();
868 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
869 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
872 if (!gst_buffer_pool_set_config (pool, config))
877 if (update_allocator)
878 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
880 gst_query_add_allocation_param (query, allocator, ¶ms);
882 gst_object_unref (allocator);
885 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
886 gst_object_unref (pool);
893 gst_object_unref (pool);
895 GST_ELEMENT_ERROR (trans, RESOURCE, SETTINGS,
896 ("Failed to configure the buffer pool"),
897 ("Configuration is most likely invalid, please report this issue."));
902 gst_base_transform_do_bufferpool (GstBaseTransform * trans, GstCaps * outcaps)
905 gboolean result = TRUE;
906 GstBufferPool *pool = NULL;
907 GstBaseTransformClass *klass;
908 GstBaseTransformPrivate *priv = trans->priv;
909 GstAllocator *allocator;
910 GstAllocationParams params;
912 /* there are these possibilities:
914 * 1) we negotiated passthrough, we can proxy the bufferpool directly and we
915 * will do that whenever some upstream does an allocation query.
916 * 2) we need to do a transform, we need to get a bufferpool from downstream
917 * and configure it. When upstream does the ALLOCATION query, the
918 * propose_allocation vmethod will be called and we will configure the
919 * upstream allocator with our proposed values then.
921 if (priv->passthrough || priv->always_in_place) {
922 /* we are in passthrough, the input buffer is never copied and always passed
923 * along. We never allocate an output buffer on the srcpad. What we do is
924 * let the upstream element decide if it wants to use a bufferpool and
925 * then we will proxy the downstream pool */
926 GST_DEBUG_OBJECT (trans, "we're passthough, delay bufferpool");
927 gst_base_transform_set_allocation (trans, NULL, NULL, NULL, NULL);
931 /* not passthrough, we need to allocate */
932 /* find a pool for the negotiated caps now */
933 GST_DEBUG_OBJECT (trans, "doing allocation query");
934 query = gst_query_new_allocation (outcaps, TRUE);
935 if (!gst_pad_peer_query (trans->srcpad, query)) {
936 /* not a problem, just debug a little */
937 GST_DEBUG_OBJECT (trans, "peer ALLOCATION query failed");
940 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
942 GST_DEBUG_OBJECT (trans, "calling decide_allocation");
943 g_assert (klass->decide_allocation != NULL);
944 result = klass->decide_allocation (trans, query);
946 GST_DEBUG_OBJECT (trans, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
950 goto no_decide_allocation;
952 /* we got configuration from our peer or the decide_allocation method,
954 if (gst_query_get_n_allocation_params (query) > 0) {
955 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
958 gst_allocation_params_init (¶ms);
961 if (gst_query_get_n_allocation_pools (query) > 0)
962 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
966 gst_base_transform_set_allocation (trans, pool, allocator, ¶ms,
972 no_decide_allocation:
974 GST_WARNING_OBJECT (trans, "Subclass failed to decide allocation");
975 gst_query_unref (query);
981 /* function triggered when the in and out caps are negotiated and need
982 * to be configured in the subclass. */
984 gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
988 GstBaseTransformClass *klass;
989 GstBaseTransformPrivate *priv = trans->priv;
991 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
993 GST_DEBUG_OBJECT (trans, "in caps: %" GST_PTR_FORMAT, in);
994 GST_DEBUG_OBJECT (trans, "out caps: %" GST_PTR_FORMAT, out);
996 /* clear the cache */
997 gst_caps_replace (&priv->cache_caps1, NULL);
998 gst_caps_replace (&priv->cache_caps2, NULL);
1000 /* figure out same caps state */
1001 priv->have_same_caps = gst_caps_is_equal (in, out);
1002 GST_DEBUG_OBJECT (trans, "have_same_caps: %d", priv->have_same_caps);
1004 /* Set the passthrough if the class wants passthrough_on_same_caps
1005 * and we have the same caps on each pad */
1006 if (klass->passthrough_on_same_caps)
1007 gst_base_transform_set_passthrough (trans, priv->have_same_caps);
1009 /* now configure the element with the caps */
1010 if (klass->set_caps) {
1011 GST_DEBUG_OBJECT (trans, "Calling set_caps method to setup caps");
1012 ret = klass->set_caps (trans, in, out);
1019 gst_base_transform_default_fixate_caps (GstBaseTransform * trans,
1020 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
1022 othercaps = gst_caps_fixate (othercaps);
1023 GST_DEBUG_OBJECT (trans, "fixated to %" GST_PTR_FORMAT, othercaps);
1028 /* given a fixed @caps on @pad, create the best possible caps for the
1030 * @caps must be fixed when calling this function.
1032 * This function calls the transform caps vmethod of the basetransform to figure
1033 * out the possible target formats. It then tries to select the best format from
1036 * - attempt passthrough if the target caps is a superset of the input caps
1037 * - fixating by using peer caps
1038 * - fixating with transform fixate function
1039 * - fixating with pad fixate functions.
1041 * this function returns a caps that can be transformed into and is accepted by
1045 gst_base_transform_find_transform (GstBaseTransform * trans, GstPad * pad,
1048 GstBaseTransformClass *klass;
1049 GstPad *otherpad, *otherpeer;
1053 /* caps must be fixed here, this is a programming error if it's not */
1054 g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
1056 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1058 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1059 otherpeer = gst_pad_get_peer (otherpad);
1061 /* see how we can transform the input caps. We need to do this even for
1062 * passthrough because it might be possible that this element cannot support
1063 * passthrough at all. */
1064 othercaps = gst_base_transform_transform_caps (trans,
1065 GST_PAD_DIRECTION (pad), caps, NULL);
1067 /* The caps we can actually output is the intersection of the transformed
1068 * caps with the pad template for the pad */
1069 if (othercaps && !gst_caps_is_empty (othercaps)) {
1070 GstCaps *intersect, *templ_caps;
1072 templ_caps = gst_pad_get_pad_template_caps (otherpad);
1073 GST_DEBUG_OBJECT (trans,
1074 "intersecting against padtemplate %" GST_PTR_FORMAT, templ_caps);
1077 gst_caps_intersect_full (othercaps, templ_caps,
1078 GST_CAPS_INTERSECT_FIRST);
1080 gst_caps_unref (othercaps);
1081 gst_caps_unref (templ_caps);
1082 othercaps = intersect;
1085 /* check if transform is empty */
1086 if (!othercaps || gst_caps_is_empty (othercaps))
1089 /* if the othercaps are not fixed, we need to fixate them, first attempt
1090 * is by attempting passthrough if the othercaps are a superset of caps. */
1091 /* FIXME. maybe the caps is not fixed because it has multiple structures of
1093 is_fixed = gst_caps_is_fixed (othercaps);
1095 GST_DEBUG_OBJECT (trans,
1096 "transform returned non fixed %" GST_PTR_FORMAT, othercaps);
1098 /* Now let's see what the peer suggests based on our transformed caps */
1100 GstCaps *peercaps, *intersection, *templ_caps;
1102 GST_DEBUG_OBJECT (trans,
1103 "Checking peer caps with filter %" GST_PTR_FORMAT, othercaps);
1105 peercaps = gst_pad_query_caps (otherpeer, othercaps);
1106 GST_DEBUG_OBJECT (trans, "Resulted in %" GST_PTR_FORMAT, peercaps);
1107 if (!gst_caps_is_empty (peercaps)) {
1108 templ_caps = gst_pad_get_pad_template_caps (otherpad);
1110 GST_DEBUG_OBJECT (trans,
1111 "Intersecting with template caps %" GST_PTR_FORMAT, templ_caps);
1114 gst_caps_intersect_full (peercaps, templ_caps,
1115 GST_CAPS_INTERSECT_FIRST);
1116 GST_DEBUG_OBJECT (trans, "Intersection: %" GST_PTR_FORMAT,
1118 gst_caps_unref (peercaps);
1119 gst_caps_unref (templ_caps);
1120 peercaps = intersection;
1122 GST_DEBUG_OBJECT (trans,
1123 "Intersecting with transformed caps %" GST_PTR_FORMAT, othercaps);
1125 gst_caps_intersect_full (peercaps, othercaps,
1126 GST_CAPS_INTERSECT_FIRST);
1127 GST_DEBUG_OBJECT (trans, "Intersection: %" GST_PTR_FORMAT,
1129 gst_caps_unref (peercaps);
1130 gst_caps_unref (othercaps);
1131 othercaps = intersection;
1133 gst_caps_unref (othercaps);
1134 othercaps = peercaps;
1137 is_fixed = gst_caps_is_fixed (othercaps);
1139 GST_DEBUG_OBJECT (trans, "no peer, doing passthrough");
1140 gst_caps_unref (othercaps);
1141 othercaps = gst_caps_ref (caps);
1145 if (gst_caps_is_empty (othercaps))
1146 goto no_transform_possible;
1148 GST_DEBUG ("have %sfixed caps %" GST_PTR_FORMAT, (is_fixed ? "" : "non-"),
1151 /* second attempt at fixation, call the fixate vmethod */
1152 /* caps could be fixed but the subclass may want to add fields */
1153 if (klass->fixate_caps) {
1154 GST_DEBUG_OBJECT (trans, "calling fixate_caps for %" GST_PTR_FORMAT
1155 " using caps %" GST_PTR_FORMAT " on pad %s:%s", othercaps, caps,
1156 GST_DEBUG_PAD_NAME (otherpad));
1157 /* note that we pass the complete array of structures to the fixate
1158 * function, it needs to truncate itself */
1160 klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
1161 is_fixed = gst_caps_is_fixed (othercaps);
1162 GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
1165 /* caps should be fixed now, if not we have to fail. */
1167 goto could_not_fixate;
1169 /* and peer should accept */
1170 if (otherpeer && !gst_pad_query_accept_caps (otherpeer, othercaps))
1171 goto peer_no_accept;
1173 GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT
1174 ", and got final caps %" GST_PTR_FORMAT, caps, othercaps);
1177 gst_object_unref (otherpeer);
1184 GST_DEBUG_OBJECT (trans,
1185 "transform returned useless %" GST_PTR_FORMAT, othercaps);
1188 no_transform_possible:
1190 GST_DEBUG_OBJECT (trans,
1191 "transform could not transform %" GST_PTR_FORMAT
1192 " in anything we support", caps);
1197 GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
1202 GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
1203 " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1209 gst_object_unref (otherpeer);
1211 gst_caps_unref (othercaps);
1217 gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
1218 GstPadDirection direction, GstCaps * caps)
1220 GstPad *pad, *otherpad;
1221 GstCaps *templ, *otempl, *ocaps = NULL;
1222 gboolean ret = TRUE;
1226 GST_PAD_SINK) ? GST_BASE_TRANSFORM_SINK_PAD (trans) :
1227 GST_BASE_TRANSFORM_SRC_PAD (trans);
1230 GST_PAD_SINK) ? GST_BASE_TRANSFORM_SRC_PAD (trans) :
1231 GST_BASE_TRANSFORM_SINK_PAD (trans);
1233 GST_DEBUG_OBJECT (trans, "accept caps %" GST_PTR_FORMAT, caps);
1235 templ = gst_pad_get_pad_template_caps (pad);
1236 otempl = gst_pad_get_pad_template_caps (otherpad);
1238 /* get all the formats we can handle on this pad */
1239 GST_DEBUG_OBJECT (trans, "intersect with pad template: %" GST_PTR_FORMAT,
1241 if (!gst_caps_can_intersect (caps, templ))
1244 GST_DEBUG_OBJECT (trans, "trying to transform with filter: %"
1245 GST_PTR_FORMAT " (the other pad template)", otempl);
1246 ocaps = gst_base_transform_transform_caps (trans, direction, caps, otempl);
1247 if (!ocaps || gst_caps_is_empty (ocaps))
1248 goto no_transform_possible;
1251 GST_DEBUG_OBJECT (trans, "accept-caps result: %d", ret);
1253 gst_caps_unref (ocaps);
1254 gst_caps_unref (templ);
1255 gst_caps_unref (otempl);
1261 GST_DEBUG_OBJECT (trans, "caps can't intersect with the template");
1265 no_transform_possible:
1267 GST_DEBUG_OBJECT (trans,
1268 "transform could not transform %" GST_PTR_FORMAT
1269 " in anything we support", caps);
1275 /* called when new caps arrive on the sink pad,
1276 * We try to find the best caps for the other side using our _find_transform()
1277 * function. If there are caps, we configure the transform for this new
1281 gst_base_transform_setcaps (GstBaseTransform * trans, GstPad * pad,
1284 GstBaseTransformPrivate *priv = trans->priv;
1285 GstCaps *outcaps, *prev_incaps = NULL, *prev_outcaps = NULL;
1286 gboolean ret = TRUE;
1288 GST_DEBUG_OBJECT (pad, "have new caps %p %" GST_PTR_FORMAT, incaps, incaps);
1290 /* find best possible caps for the other pad */
1291 outcaps = gst_base_transform_find_transform (trans, pad, incaps);
1292 if (!outcaps || gst_caps_is_empty (outcaps))
1293 goto no_transform_possible;
1295 /* configure the element now */
1297 /* if we have the same caps, we can optimize and reuse the input caps */
1298 if (gst_caps_is_equal (incaps, outcaps)) {
1299 GST_INFO_OBJECT (trans, "reuse caps");
1300 gst_caps_unref (outcaps);
1301 outcaps = gst_caps_ref (incaps);
1304 prev_incaps = gst_pad_get_current_caps (trans->sinkpad);
1305 prev_outcaps = gst_pad_get_current_caps (trans->srcpad);
1306 if (prev_incaps && prev_outcaps && gst_caps_is_equal (prev_incaps, incaps)
1307 && gst_caps_is_equal (prev_outcaps, outcaps)) {
1308 GST_DEBUG_OBJECT (trans,
1309 "New caps equal to old ones: %" GST_PTR_FORMAT " -> %" GST_PTR_FORMAT,
1313 /* call configure now */
1314 if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
1315 goto failed_configure;
1317 if (!prev_outcaps || !gst_caps_is_equal (outcaps, prev_outcaps))
1318 /* let downstream know about our caps */
1319 ret = gst_pad_set_caps (trans->srcpad, outcaps);
1323 /* try to get a pool when needed */
1324 ret = gst_base_transform_do_bufferpool (trans, outcaps);
1329 gst_caps_unref (outcaps);
1331 gst_caps_unref (prev_incaps);
1333 gst_caps_unref (prev_outcaps);
1335 GST_OBJECT_LOCK (trans);
1336 priv->negotiated = ret;
1337 GST_OBJECT_UNLOCK (trans);
1342 no_transform_possible:
1344 GST_WARNING_OBJECT (trans,
1345 "transform could not transform %" GST_PTR_FORMAT
1346 " in anything we support", incaps);
1352 GST_WARNING_OBJECT (trans, "FAILED to configure incaps %" GST_PTR_FORMAT
1353 " and outcaps %" GST_PTR_FORMAT, incaps, outcaps);
1360 gst_base_transform_default_propose_allocation (GstBaseTransform * trans,
1361 GstQuery * decide_query, GstQuery * query)
1365 if (decide_query == NULL) {
1366 GST_DEBUG_OBJECT (trans, "doing passthrough query");
1367 ret = gst_pad_peer_query (trans->srcpad, query);
1370 /* non-passthrough, copy all metadata, decide_query does not contain the
1371 * metadata anymore that depends on the buffer memory */
1372 n_metas = gst_query_get_n_allocation_metas (decide_query);
1373 for (i = 0; i < n_metas; i++) {
1375 const GstStructure *params;
1377 api = gst_query_parse_nth_allocation_meta (decide_query, i, ¶ms);
1378 GST_DEBUG_OBJECT (trans, "proposing metadata %s", g_type_name (api));
1379 gst_query_add_allocation_meta (query, api, params);
1387 gst_base_transform_reconfigure (GstBaseTransform * trans)
1389 gboolean reconfigure, ret = TRUE;
1391 reconfigure = gst_pad_check_reconfigure (trans->srcpad);
1393 if (G_UNLIKELY (reconfigure)) {
1396 GST_DEBUG_OBJECT (trans, "we had a pending reconfigure");
1398 incaps = gst_pad_get_current_caps (trans->sinkpad);
1402 /* if we need to reconfigure we pretend new caps arrived. This
1403 * will reconfigure the transform with the new output format. */
1404 if (!gst_base_transform_setcaps (trans, trans->sinkpad, incaps)) {
1405 GST_ELEMENT_WARNING (trans, STREAM, FORMAT,
1406 ("not negotiated"), ("not negotiated"));
1410 gst_caps_unref (incaps);
1416 gst_pad_mark_reconfigure (trans->srcpad);
1422 gst_base_transform_default_query (GstBaseTransform * trans,
1423 GstPadDirection direction, GstQuery * query)
1425 gboolean ret = FALSE;
1426 GstPad *pad, *otherpad;
1427 GstBaseTransformClass *klass;
1428 GstBaseTransformPrivate *priv = trans->priv;
1430 if (direction == GST_PAD_SRC) {
1431 pad = trans->srcpad;
1432 otherpad = trans->sinkpad;
1434 pad = trans->sinkpad;
1435 otherpad = trans->srcpad;
1438 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1440 switch (GST_QUERY_TYPE (query)) {
1441 case GST_QUERY_ALLOCATION:
1443 GstQuery *decide_query = NULL;
1445 /* can only be done on the sinkpad */
1446 if (direction != GST_PAD_SINK)
1449 ret = gst_base_transform_reconfigure (trans);
1450 if (G_UNLIKELY (!ret))
1453 GST_OBJECT_LOCK (trans);
1454 if (!priv->negotiated && !priv->passthrough && (klass->set_caps != NULL)) {
1455 GST_DEBUG_OBJECT (trans,
1456 "not negotiated yet but need negotiation, can't answer ALLOCATION query");
1457 GST_OBJECT_UNLOCK (trans);
1461 decide_query = trans->priv->query;
1462 trans->priv->query = NULL;
1463 GST_OBJECT_UNLOCK (trans);
1465 GST_DEBUG_OBJECT (trans,
1466 "calling propose allocation with query %" GST_PTR_FORMAT,
1469 /* pass the query to the propose_allocation vmethod if any */
1470 if (G_LIKELY (klass->propose_allocation))
1471 ret = klass->propose_allocation (trans, decide_query, query);
1476 GST_OBJECT_LOCK (trans);
1478 if (trans->priv->query == NULL)
1479 trans->priv->query = decide_query;
1481 gst_query_unref (decide_query);
1483 GST_OBJECT_UNLOCK (trans);
1486 GST_DEBUG_OBJECT (trans, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret,
1490 case GST_QUERY_POSITION:
1494 gst_query_parse_position (query, &format, NULL);
1495 if (format == GST_FORMAT_TIME && trans->segment.format == GST_FORMAT_TIME) {
1499 if ((direction == GST_PAD_SINK)
1500 || (trans->priv->position_out == GST_CLOCK_TIME_NONE)) {
1502 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1503 trans->segment.position);
1505 pos = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1506 trans->priv->position_out);
1508 gst_query_set_position (query, format, pos);
1510 ret = gst_pad_peer_query (otherpad, query);
1514 case GST_QUERY_ACCEPT_CAPS:
1518 gst_query_parse_accept_caps (query, &caps);
1519 if (klass->accept_caps) {
1520 ret = klass->accept_caps (trans, direction, caps);
1521 gst_query_set_accept_caps_result (query, ret);
1522 /* return TRUE, we answered the query */
1527 case GST_QUERY_CAPS:
1529 GstCaps *filter, *caps;
1531 gst_query_parse_caps (query, &filter);
1532 caps = gst_base_transform_query_caps (trans, pad, filter);
1533 gst_query_set_caps_result (query, caps);
1534 gst_caps_unref (caps);
1539 ret = gst_pad_peer_query (otherpad, query);
1548 gst_base_transform_query (GstPad * pad, GstObject * parent, GstQuery * query)
1550 GstBaseTransform *trans;
1551 GstBaseTransformClass *bclass;
1552 gboolean ret = FALSE;
1554 trans = GST_BASE_TRANSFORM (parent);
1555 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1558 ret = bclass->query (trans, GST_PAD_DIRECTION (pad), query);
1563 /* this function either returns the input buffer without incrementing the
1564 * refcount or it allocates a new (writable) buffer */
1565 static GstFlowReturn
1566 default_prepare_output_buffer (GstBaseTransform * trans,
1567 GstBuffer * inbuf, GstBuffer ** outbuf)
1569 GstBaseTransformPrivate *priv;
1571 GstBaseTransformClass *bclass;
1572 GstCaps *incaps, *outcaps;
1573 gsize insize, outsize;
1577 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1579 /* figure out how to allocate an output buffer */
1580 if (priv->passthrough) {
1581 /* passthrough, we will not modify the incoming buffer so we can just
1583 GST_DEBUG_OBJECT (trans, "passthrough: reusing input buffer");
1588 /* we can't reuse the input buffer */
1590 if (!priv->pool_active) {
1591 GST_DEBUG_OBJECT (trans, "setting pool %p active", priv->pool);
1592 if (!gst_buffer_pool_set_active (priv->pool, TRUE))
1593 goto activate_failed;
1594 priv->pool_active = TRUE;
1596 GST_DEBUG_OBJECT (trans, "using pool alloc");
1597 ret = gst_buffer_pool_acquire_buffer (priv->pool, outbuf, NULL);
1598 if (ret != GST_FLOW_OK)
1604 /* no pool, we need to figure out the size of the output buffer first */
1605 if ((bclass->transform_ip != NULL) && priv->always_in_place) {
1606 /* we want to do an in-place alloc */
1607 if (gst_buffer_is_writable (inbuf)) {
1608 GST_DEBUG_OBJECT (trans, "inplace reuse writable input buffer");
1611 GST_DEBUG_OBJECT (trans, "making writable buffer copy");
1612 /* we make a copy of the input buffer */
1613 *outbuf = gst_buffer_copy (inbuf);
1618 /* else use the transform function to get the size */
1619 incaps = gst_pad_get_current_caps (trans->sinkpad);
1620 outcaps = gst_pad_get_current_caps (trans->srcpad);
1622 /* srcpad might be flushing already if we're being shut down */
1623 if (outcaps == NULL)
1626 GST_DEBUG_OBJECT (trans, "getting output size for alloc");
1627 /* copy transform, figure out the output size */
1628 insize = gst_buffer_get_size (inbuf);
1629 res = gst_base_transform_transform_size (trans,
1630 GST_PAD_SINK, incaps, insize, outcaps, &outsize);
1632 gst_caps_unref (incaps);
1633 gst_caps_unref (outcaps);
1638 GST_DEBUG_OBJECT (trans, "doing alloc of size %" G_GSIZE_FORMAT, outsize);
1639 *outbuf = gst_buffer_new_allocate (priv->allocator, outsize, &priv->params);
1641 ret = GST_FLOW_ERROR;
1646 /* copy the metadata */
1647 if (bclass->copy_metadata)
1648 if (!bclass->copy_metadata (trans, inbuf, *outbuf)) {
1649 /* something failed, post a warning */
1650 GST_ELEMENT_WARNING (trans, STREAM, NOT_IMPLEMENTED,
1651 ("could not copy metadata"), (NULL));
1660 GST_ELEMENT_ERROR (trans, RESOURCE, SETTINGS,
1661 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
1662 return GST_FLOW_ERROR;
1666 GST_ERROR_OBJECT (trans, "unknown output size");
1667 return GST_FLOW_ERROR;
1671 GST_DEBUG_OBJECT (trans, "could not allocate buffer from pool");
1676 GST_DEBUG_OBJECT (trans, "no output caps, source pad has been deactivated");
1677 gst_caps_unref (incaps);
1678 return GST_FLOW_FLUSHING;
1684 GstBaseTransform *trans;
1689 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
1691 CopyMetaData *data = user_data;
1692 GstBaseTransform *trans = data->trans;
1693 GstBaseTransformClass *klass;
1694 const GstMetaInfo *info = (*meta)->info;
1695 GstBuffer *outbuf = data->outbuf;
1696 gboolean do_copy = FALSE;
1698 klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1700 if (gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory)) {
1701 /* never call the transform_meta with memory specific metadata */
1702 GST_DEBUG_OBJECT (trans, "not copying memory specific metadata %s",
1703 g_type_name (info->api));
1705 } else if (klass->transform_meta) {
1706 do_copy = klass->transform_meta (trans, outbuf, *meta, inbuf);
1707 GST_DEBUG_OBJECT (trans, "transformed metadata %s: copy: %d",
1708 g_type_name (info->api), do_copy);
1711 /* we only copy metadata when the subclass implemented a transform_meta
1712 * function and when it returns %TRUE */
1714 GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
1715 GST_DEBUG_OBJECT (trans, "copy metadata %s", g_type_name (info->api));
1716 /* simply copy then */
1717 info->transform_func (outbuf, *meta, inbuf,
1718 _gst_meta_transform_copy, ©_data);
1724 default_copy_metadata (GstBaseTransform * trans,
1725 GstBuffer * inbuf, GstBuffer * outbuf)
1727 GstBaseTransformPrivate *priv = trans->priv;
1730 /* now copy the metadata */
1731 GST_DEBUG_OBJECT (trans, "copying metadata");
1733 /* this should not happen, buffers allocated from a pool or with
1734 * new_allocate should always be writable. */
1735 if (!gst_buffer_is_writable (outbuf))
1738 /* when we get here, the metadata should be writable */
1739 gst_buffer_copy_into (outbuf, inbuf,
1740 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1742 /* clear the GAP flag when the subclass does not understand it */
1743 if (!priv->gap_aware)
1744 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
1748 data.outbuf = outbuf;
1750 gst_buffer_foreach_meta (inbuf, foreach_metadata, &data);
1757 GST_WARNING_OBJECT (trans, "buffer %p not writable", outbuf);
1762 /* Given @caps calcultate the size of one unit.
1764 * For video caps, this is the size of one frame (and thus one buffer).
1765 * For audio caps, this is the size of one sample.
1767 * These values are cached since they do not change and the calculation
1768 * potentially involves parsing caps and other expensive stuff.
1770 * We have two cache locations to store the size, one for the source caps
1771 * and one for the sink caps.
1773 * this function returns %FALSE if no size could be calculated.
1776 gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
1779 gboolean res = FALSE;
1780 GstBaseTransformClass *bclass;
1781 GstBaseTransformPrivate *priv = trans->priv;
1783 /* see if we have the result cached */
1784 if (priv->cache_caps1 == caps) {
1785 *size = priv->cache_caps1_size;
1786 GST_DEBUG_OBJECT (trans,
1787 "returned %" G_GSIZE_FORMAT " from first cache", *size);
1790 if (priv->cache_caps2 == caps) {
1791 *size = priv->cache_caps2_size;
1792 GST_DEBUG_OBJECT (trans,
1793 "returned %" G_GSIZE_FORMAT " from second cached", *size);
1797 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1798 res = bclass->get_unit_size (trans, caps, size);
1799 GST_DEBUG_OBJECT (trans,
1800 "caps %" GST_PTR_FORMAT " has unit size %" G_GSIZE_FORMAT ", res %s",
1801 caps, *size, res ? "TRUE" : "FALSE");
1804 /* and cache the values */
1805 if (priv->cache_caps1 == NULL) {
1806 gst_caps_replace (&priv->cache_caps1, caps);
1807 priv->cache_caps1_size = *size;
1808 GST_DEBUG_OBJECT (trans,
1809 "caching %" G_GSIZE_FORMAT " in first cache", *size);
1810 } else if (priv->cache_caps2 == NULL) {
1811 gst_caps_replace (&priv->cache_caps2, caps);
1812 priv->cache_caps2_size = *size;
1813 GST_DEBUG_OBJECT (trans,
1814 "caching %" G_GSIZE_FORMAT " in second cache", *size);
1816 GST_DEBUG_OBJECT (trans, "no free spot to cache unit_size");
1823 gst_base_transform_sink_event (GstPad * pad, GstObject * parent,
1826 GstBaseTransform *trans;
1827 GstBaseTransformClass *bclass;
1828 gboolean ret = TRUE;
1830 trans = GST_BASE_TRANSFORM (parent);
1831 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1833 if (bclass->sink_event)
1834 ret = bclass->sink_event (trans, event);
1836 gst_event_unref (event);
1842 gst_base_transform_sink_eventfunc (GstBaseTransform * trans, GstEvent * event)
1844 gboolean ret = TRUE, forward = TRUE;
1845 GstBaseTransformPrivate *priv = trans->priv;
1847 switch (GST_EVENT_TYPE (event)) {
1848 case GST_EVENT_FLUSH_START:
1850 case GST_EVENT_FLUSH_STOP:
1851 GST_OBJECT_LOCK (trans);
1852 /* reset QoS parameters */
1853 priv->proportion = 1.0;
1854 priv->earliest_time = -1;
1855 priv->discont = FALSE;
1856 priv->processed = 0;
1858 GST_OBJECT_UNLOCK (trans);
1859 /* we need new segment info after the flush. */
1860 trans->have_segment = FALSE;
1861 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
1862 priv->position_out = GST_CLOCK_TIME_NONE;
1868 case GST_EVENT_CAPS:
1872 gst_event_parse_caps (event, &caps);
1873 /* clear any pending reconfigure flag */
1874 gst_pad_check_reconfigure (trans->srcpad);
1875 ret = gst_base_transform_setcaps (trans, trans->sinkpad, caps);
1877 gst_pad_mark_reconfigure (trans->srcpad);
1882 case GST_EVENT_SEGMENT:
1884 gst_event_copy_segment (event, &trans->segment);
1885 trans->have_segment = TRUE;
1887 GST_DEBUG_OBJECT (trans, "received SEGMENT %" GST_SEGMENT_FORMAT,
1896 ret = gst_pad_push_event (trans->srcpad, event);
1898 gst_event_unref (event);
1904 gst_base_transform_src_event (GstPad * pad, GstObject * parent,
1907 GstBaseTransform *trans;
1908 GstBaseTransformClass *bclass;
1909 gboolean ret = TRUE;
1911 trans = GST_BASE_TRANSFORM (parent);
1912 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1914 if (bclass->src_event)
1915 ret = bclass->src_event (trans, event);
1917 gst_event_unref (event);
1923 gst_base_transform_src_eventfunc (GstBaseTransform * trans, GstEvent * event)
1927 GST_DEBUG_OBJECT (trans, "handling event %p %" GST_PTR_FORMAT, event, event);
1929 switch (GST_EVENT_TYPE (event)) {
1930 case GST_EVENT_SEEK:
1932 case GST_EVENT_NAVIGATION:
1937 GstClockTimeDiff diff;
1938 GstClockTime timestamp;
1940 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
1941 gst_base_transform_update_qos (trans, proportion, diff, timestamp);
1948 ret = gst_pad_push_event (trans->sinkpad, event);
1953 /* Takes the input buffer */
1954 static GstFlowReturn
1955 default_submit_input_buffer (GstBaseTransform * trans, gboolean is_discont,
1958 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1959 GstBaseTransformPrivate *priv = trans->priv;
1960 GstFlowReturn ret = GST_FLOW_OK;
1961 GstClockTime running_time;
1962 GstClockTime timestamp;
1964 if (G_UNLIKELY (!gst_base_transform_reconfigure (trans)))
1965 goto not_negotiated;
1967 if (GST_BUFFER_OFFSET_IS_VALID (inbuf))
1968 GST_DEBUG_OBJECT (trans,
1969 "handling buffer %p of size %" G_GSIZE_FORMAT ", PTS %" GST_TIME_FORMAT
1970 " and offset %" G_GUINT64_FORMAT, inbuf, gst_buffer_get_size (inbuf),
1971 GST_TIME_ARGS (GST_BUFFER_PTS (inbuf)), GST_BUFFER_OFFSET (inbuf));
1973 GST_DEBUG_OBJECT (trans,
1974 "handling buffer %p of size %" G_GSIZE_FORMAT ", PTS %" GST_TIME_FORMAT
1975 " and offset NONE", inbuf, gst_buffer_get_size (inbuf),
1976 GST_TIME_ARGS (GST_BUFFER_PTS (inbuf)));
1978 /* Don't allow buffer handling before negotiation, except in passthrough mode
1979 * or if the class doesn't implement a set_caps function (in which case it doesn't
1982 if (!priv->negotiated && !priv->passthrough && (bclass->set_caps != NULL))
1983 goto not_negotiated;
1985 /* can only do QoS if the segment is in TIME */
1986 if (trans->segment.format != GST_FORMAT_TIME)
1989 /* QOS is done on the running time of the buffer, get it now */
1990 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
1991 running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
1994 if (running_time != -1) {
1996 GstClockTime earliest_time;
1999 /* lock for getting the QoS parameters that are set (in a different thread)
2000 * with the QOS events */
2001 GST_OBJECT_LOCK (trans);
2002 earliest_time = priv->earliest_time;
2003 proportion = priv->proportion;
2004 /* check for QoS, don't perform conversion for buffers
2005 * that are known to be late. */
2006 need_skip = priv->qos_enabled &&
2007 earliest_time != -1 && running_time <= earliest_time;
2008 GST_OBJECT_UNLOCK (trans);
2011 GstMessage *qos_msg;
2012 GstClockTime duration;
2013 guint64 stream_time;
2016 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "skipping transform: qostime %"
2017 GST_TIME_FORMAT " <= %" GST_TIME_FORMAT,
2018 GST_TIME_ARGS (running_time), GST_TIME_ARGS (earliest_time));
2022 duration = GST_BUFFER_DURATION (inbuf);
2024 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
2026 jitter = GST_CLOCK_DIFF (running_time, earliest_time);
2029 gst_message_new_qos (GST_OBJECT_CAST (trans), FALSE, running_time,
2030 stream_time, timestamp, duration);
2031 gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
2032 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
2033 priv->processed, priv->dropped);
2034 gst_element_post_message (GST_ELEMENT_CAST (trans), qos_msg);
2036 /* mark discont for next buffer */
2037 priv->discont = TRUE;
2038 ret = GST_BASE_TRANSFORM_FLOW_DROPPED;
2044 /* Stash input buffer where the default generate_output
2045 * function can find it */
2046 if (trans->queued_buf)
2047 gst_buffer_unref (trans->queued_buf);
2048 trans->queued_buf = inbuf;
2051 gst_buffer_unref (inbuf);
2056 gst_buffer_unref (inbuf);
2057 if (GST_PAD_IS_FLUSHING (trans->srcpad))
2058 return GST_FLOW_FLUSHING;
2059 return GST_FLOW_NOT_NEGOTIATED;
2063 static GstFlowReturn
2064 default_generate_output (GstBaseTransform * trans, GstBuffer ** outbuf)
2066 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2067 GstBaseTransformPrivate *priv = trans->priv;
2068 GstFlowReturn ret = GST_FLOW_OK;
2070 gboolean want_in_place;
2072 /* Retrieve stashed input buffer, if the default submit_input_buffer
2073 * was run. Takes ownership back from there */
2074 inbuf = trans->queued_buf;
2075 trans->queued_buf = NULL;
2077 /* This default processing method needs one input buffer to feed to
2078 * the transform functions, we can't do anything without it */
2082 /* first try to allocate an output buffer based on the currently negotiated
2083 * format. outbuf will contain a buffer suitable for doing the configured
2084 * transform after this function. */
2085 if (bclass->prepare_output_buffer == NULL)
2088 GST_DEBUG_OBJECT (trans, "calling prepare buffer");
2089 ret = bclass->prepare_output_buffer (trans, inbuf, outbuf);
2091 if (ret != GST_FLOW_OK || *outbuf == NULL)
2094 GST_DEBUG_OBJECT (trans, "using allocated buffer in %p, out %p", inbuf,
2097 /* now perform the needed transform */
2098 if (priv->passthrough) {
2099 /* In passthrough mode, give transform_ip a look at the
2100 * buffer, without making it writable, or just push the
2102 if (bclass->transform_ip_on_passthrough && bclass->transform_ip) {
2103 GST_DEBUG_OBJECT (trans, "doing passthrough transform_ip");
2104 ret = bclass->transform_ip (trans, *outbuf);
2106 GST_DEBUG_OBJECT (trans, "element is in passthrough");
2109 want_in_place = (bclass->transform_ip != NULL) && priv->always_in_place;
2111 if (want_in_place) {
2112 GST_DEBUG_OBJECT (trans, "doing inplace transform");
2113 ret = bclass->transform_ip (trans, *outbuf);
2115 GST_DEBUG_OBJECT (trans, "doing non-inplace transform");
2117 if (bclass->transform)
2118 ret = bclass->transform (trans, inbuf, *outbuf);
2120 ret = GST_FLOW_NOT_SUPPORTED;
2124 /* only unref input buffer if we allocated a new outbuf buffer. If we reused
2125 * the input buffer, no refcount is changed to keep the input buffer writable
2127 if (*outbuf != inbuf)
2128 gst_buffer_unref (inbuf);
2135 gst_buffer_unref (inbuf);
2136 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
2137 ("Sub-class has no prepare_output_buffer implementation"), (NULL));
2138 return GST_FLOW_NOT_SUPPORTED;
2142 gst_buffer_unref (inbuf);
2144 GST_WARNING_OBJECT (trans, "could not get buffer from pool: %s",
2145 gst_flow_get_name (ret));
2150 /* FIXME, getrange is broken, need to pull range from the other
2151 * end based on the transform_size result.
2153 static GstFlowReturn
2154 gst_base_transform_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2155 guint length, GstBuffer ** buffer)
2157 GstBaseTransformClass *klass = GST_BASE_TRANSFORM_GET_CLASS (parent);
2158 GstBaseTransform *trans = GST_BASE_TRANSFORM (parent);
2159 GstBaseTransformPrivate *priv = trans->priv;
2161 GstBuffer *inbuf = NULL;
2162 GstBuffer *outbuf = NULL;
2164 /* Try and generate a buffer, if the sub-class wants more data,
2165 * pull some and repeat until a buffer (or error) is produced */
2167 ret = klass->generate_output (trans, &outbuf);
2169 /* Consume the DROPPED return value and go get more data */
2170 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED)
2173 if (ret != GST_FLOW_OK || outbuf != NULL)
2176 /* No buffer generated, try and pull data */
2177 ret = gst_pad_pull_range (trans->sinkpad, offset, length, &inbuf);
2178 if (G_UNLIKELY (ret != GST_FLOW_OK))
2181 if (klass->before_transform)
2182 klass->before_transform (trans, inbuf);
2184 /* Set discont flag so we can mark the next outgoing buffer */
2185 if (GST_BUFFER_IS_DISCONT (inbuf)) {
2186 GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", inbuf);
2187 priv->discont = TRUE;
2190 /* FIXME: Input offsets and lengths need to be translated, as per
2191 * the FIXME above. For now, just advance somewhat */
2192 offset += gst_buffer_get_size (inbuf);
2194 ret = klass->submit_input_buffer (trans, priv->discont, inbuf);
2195 if (ret != GST_FLOW_OK) {
2196 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED)
2200 } while (ret == GST_FLOW_OK && outbuf == NULL);
2204 /* apply DISCONT flag if the buffer is not yet marked as such */
2205 if (priv->discont) {
2206 GST_DEBUG_OBJECT (trans, "we have a pending DISCONT");
2207 if (!GST_BUFFER_IS_DISCONT (outbuf)) {
2208 GST_DEBUG_OBJECT (trans, "marking DISCONT on output buffer");
2209 outbuf = gst_buffer_make_writable (outbuf);
2210 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2212 priv->discont = FALSE;
2222 GST_DEBUG_OBJECT (trans, "failed to pull a buffer: %s",
2223 gst_flow_get_name (ret));
2228 /* The flow of the chain function is the reverse of the
2229 * getrange() function - we have data, feed it to the sub-class
2230 * and then iterate, pushing buffers it generates until it either
2231 * wants more data or returns an error */
2232 static GstFlowReturn
2233 gst_base_transform_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2235 GstBaseTransform *trans = GST_BASE_TRANSFORM (parent);
2236 GstBaseTransformClass *klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2237 GstBaseTransformPrivate *priv = trans->priv;
2239 GstClockTime position = GST_CLOCK_TIME_NONE;
2240 GstClockTime timestamp, duration;
2241 GstBuffer *outbuf = NULL;
2243 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2244 duration = GST_BUFFER_DURATION (buffer);
2246 /* calculate end position of the incoming buffer */
2247 if (timestamp != GST_CLOCK_TIME_NONE) {
2248 if (duration != GST_CLOCK_TIME_NONE)
2249 position = timestamp + duration;
2251 position = timestamp;
2254 if (klass->before_transform)
2255 klass->before_transform (trans, buffer);
2257 /* Set discont flag so we can mark the outgoing buffer */
2258 if (GST_BUFFER_IS_DISCONT (buffer)) {
2259 GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", buffer);
2260 priv->discont = TRUE;
2263 /* Takes ownership of input buffer */
2264 ret = klass->submit_input_buffer (trans, priv->discont, buffer);
2265 if (ret != GST_FLOW_OK)
2271 ret = klass->generate_output (trans, &outbuf);
2273 /* outbuf can be NULL, this means a dropped buffer, if we have a buffer but
2274 * GST_BASE_TRANSFORM_FLOW_DROPPED we will not push either. */
2275 if (outbuf != NULL) {
2276 if (ret == GST_FLOW_OK) {
2277 GstClockTime position_out = GST_CLOCK_TIME_NONE;
2279 /* Remember last stop position */
2280 if (position != GST_CLOCK_TIME_NONE &&
2281 trans->segment.format == GST_FORMAT_TIME)
2282 trans->segment.position = position;
2284 if (GST_BUFFER_TIMESTAMP_IS_VALID (outbuf)) {
2285 position_out = GST_BUFFER_TIMESTAMP (outbuf);
2286 if (GST_BUFFER_DURATION_IS_VALID (outbuf))
2287 position_out += GST_BUFFER_DURATION (outbuf);
2288 } else if (position != GST_CLOCK_TIME_NONE) {
2289 position_out = position;
2291 if (position_out != GST_CLOCK_TIME_NONE
2292 && trans->segment.format == GST_FORMAT_TIME)
2293 priv->position_out = position_out;
2295 /* apply DISCONT flag if the buffer is not yet marked as such */
2296 if (trans->priv->discont) {
2297 GST_DEBUG_OBJECT (trans, "we have a pending DISCONT");
2298 if (!GST_BUFFER_IS_DISCONT (outbuf)) {
2299 GST_DEBUG_OBJECT (trans, "marking DISCONT on output buffer");
2300 outbuf = gst_buffer_make_writable (outbuf);
2301 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2303 priv->discont = FALSE;
2307 ret = gst_pad_push (trans->srcpad, outbuf);
2309 GST_DEBUG_OBJECT (trans, "we got return %s", gst_flow_get_name (ret));
2310 gst_buffer_unref (outbuf);
2313 } while (ret == GST_FLOW_OK && outbuf != NULL);
2316 /* convert internal flow to OK and mark discont for the next buffer. */
2317 if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED) {
2318 GST_DEBUG_OBJECT (trans, "dropped a buffer, marking DISCONT");
2319 priv->discont = TRUE;
2327 gst_base_transform_set_property (GObject * object, guint prop_id,
2328 const GValue * value, GParamSpec * pspec)
2330 GstBaseTransform *trans;
2332 trans = GST_BASE_TRANSFORM (object);
2336 gst_base_transform_set_qos_enabled (trans, g_value_get_boolean (value));
2339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2345 gst_base_transform_get_property (GObject * object, guint prop_id,
2346 GValue * value, GParamSpec * pspec)
2348 GstBaseTransform *trans;
2350 trans = GST_BASE_TRANSFORM (object);
2354 g_value_set_boolean (value, gst_base_transform_is_qos_enabled (trans));
2357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2362 /* not a vmethod of anything, just an internal method */
2364 gst_base_transform_activate (GstBaseTransform * trans, gboolean active)
2366 GstBaseTransformClass *bclass;
2367 GstBaseTransformPrivate *priv = trans->priv;
2368 gboolean result = TRUE;
2370 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2373 GstCaps *incaps, *outcaps;
2375 if (priv->pad_mode == GST_PAD_MODE_NONE && bclass->start)
2376 result &= bclass->start (trans);
2378 incaps = gst_pad_get_current_caps (trans->sinkpad);
2379 outcaps = gst_pad_get_current_caps (trans->srcpad);
2381 GST_OBJECT_LOCK (trans);
2382 if (incaps && outcaps)
2383 priv->have_same_caps =
2384 gst_caps_is_equal (incaps, outcaps) || priv->passthrough;
2386 priv->have_same_caps = priv->passthrough;
2387 GST_DEBUG_OBJECT (trans, "have_same_caps %d", priv->have_same_caps);
2388 priv->negotiated = FALSE;
2389 trans->have_segment = FALSE;
2390 gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
2391 priv->position_out = GST_CLOCK_TIME_NONE;
2392 priv->proportion = 1.0;
2393 priv->earliest_time = -1;
2394 priv->discont = FALSE;
2395 priv->processed = 0;
2397 GST_OBJECT_UNLOCK (trans);
2400 gst_caps_unref (incaps);
2402 gst_caps_unref (outcaps);
2404 /* We must make sure streaming has finished before resetting things
2405 * and calling the ::stop vfunc */
2406 GST_PAD_STREAM_LOCK (trans->sinkpad);
2407 GST_PAD_STREAM_UNLOCK (trans->sinkpad);
2409 priv->have_same_caps = FALSE;
2410 /* We can only reset the passthrough mode if the instance told us to
2411 handle it in configure_caps */
2412 if (bclass->passthrough_on_same_caps) {
2413 gst_base_transform_set_passthrough (trans, FALSE);
2415 gst_caps_replace (&priv->cache_caps1, NULL);
2416 gst_caps_replace (&priv->cache_caps2, NULL);
2418 /* Make sure any left over buffer is freed */
2419 gst_buffer_replace (&trans->queued_buf, NULL);
2421 if (priv->pad_mode != GST_PAD_MODE_NONE && bclass->stop)
2422 result &= bclass->stop (trans);
2424 gst_base_transform_set_allocation (trans, NULL, NULL, NULL, NULL);
2431 gst_base_transform_sink_activate_mode (GstPad * pad, GstObject * parent,
2432 GstPadMode mode, gboolean active)
2434 gboolean result = FALSE;
2435 GstBaseTransform *trans;
2437 trans = GST_BASE_TRANSFORM (parent);
2440 case GST_PAD_MODE_PUSH:
2442 result = gst_base_transform_activate (trans, active);
2445 trans->priv->pad_mode = active ? GST_PAD_MODE_PUSH : GST_PAD_MODE_NONE;
2457 gst_base_transform_src_activate_mode (GstPad * pad, GstObject * parent,
2458 GstPadMode mode, gboolean active)
2460 gboolean result = FALSE;
2461 GstBaseTransform *trans;
2463 trans = GST_BASE_TRANSFORM (parent);
2466 case GST_PAD_MODE_PULL:
2469 gst_pad_activate_mode (trans->sinkpad, GST_PAD_MODE_PULL, active);
2472 result &= gst_base_transform_activate (trans, active);
2475 trans->priv->pad_mode = active ? mode : GST_PAD_MODE_NONE;
2487 * gst_base_transform_set_passthrough:
2488 * @trans: the #GstBaseTransform to set
2489 * @passthrough: boolean indicating passthrough mode.
2491 * Set passthrough mode for this filter by default. This is mostly
2492 * useful for filters that do not care about negotiation.
2494 * Always %TRUE for filters which don't implement either a transform
2495 * or transform_ip method.
2500 gst_base_transform_set_passthrough (GstBaseTransform * trans,
2501 gboolean passthrough)
2503 GstBaseTransformClass *bclass;
2505 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2507 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2509 GST_OBJECT_LOCK (trans);
2511 if (bclass->transform_ip || bclass->transform)
2512 trans->priv->passthrough = FALSE;
2514 trans->priv->passthrough = TRUE;
2517 GST_DEBUG_OBJECT (trans, "set passthrough %d", trans->priv->passthrough);
2518 GST_OBJECT_UNLOCK (trans);
2522 * gst_base_transform_is_passthrough:
2523 * @trans: the #GstBaseTransform to query
2525 * See if @trans is configured as a passthrough transform.
2527 * Returns: %TRUE is the transform is configured in passthrough mode.
2532 gst_base_transform_is_passthrough (GstBaseTransform * trans)
2536 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2538 GST_OBJECT_LOCK (trans);
2539 result = trans->priv->passthrough;
2540 GST_OBJECT_UNLOCK (trans);
2546 * gst_base_transform_set_in_place:
2547 * @trans: the #GstBaseTransform to modify
2548 * @in_place: Boolean value indicating that we would like to operate
2549 * on in_place buffers.
2551 * Determines whether a non-writable buffer will be copied before passing
2552 * to the transform_ip function.
2554 * * Always %TRUE if no transform function is implemented.
2555 * * Always %FALSE if ONLY transform function is implemented.
2560 gst_base_transform_set_in_place (GstBaseTransform * trans, gboolean in_place)
2562 GstBaseTransformClass *bclass;
2564 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2566 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2568 GST_OBJECT_LOCK (trans);
2571 if (bclass->transform_ip) {
2572 GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
2573 trans->priv->always_in_place = TRUE;
2576 if (bclass->transform) {
2577 GST_DEBUG_OBJECT (trans, "setting in_place FALSE");
2578 trans->priv->always_in_place = FALSE;
2582 GST_OBJECT_UNLOCK (trans);
2586 * gst_base_transform_is_in_place:
2587 * @trans: the #GstBaseTransform to query
2589 * See if @trans is configured as a in_place transform.
2591 * Returns: %TRUE is the transform is configured in in_place mode.
2596 gst_base_transform_is_in_place (GstBaseTransform * trans)
2600 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2602 GST_OBJECT_LOCK (trans);
2603 result = trans->priv->always_in_place;
2604 GST_OBJECT_UNLOCK (trans);
2610 * gst_base_transform_update_qos:
2611 * @trans: a #GstBaseTransform
2612 * @proportion: the proportion
2613 * @diff: the diff against the clock
2614 * @timestamp: the timestamp of the buffer generating the QoS expressed in
2617 * Set the QoS parameters in the transform. This function is called internally
2618 * when a QOS event is received but subclasses can provide custom information
2624 gst_base_transform_update_qos (GstBaseTransform * trans,
2625 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2627 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2629 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans,
2630 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2631 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2633 GST_OBJECT_LOCK (trans);
2634 trans->priv->proportion = proportion;
2635 trans->priv->earliest_time = timestamp + diff;
2636 GST_OBJECT_UNLOCK (trans);
2640 * gst_base_transform_set_qos_enabled:
2641 * @trans: a #GstBaseTransform
2642 * @enabled: new state
2644 * Enable or disable QoS handling in the transform.
2649 gst_base_transform_set_qos_enabled (GstBaseTransform * trans, gboolean enabled)
2651 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2653 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "enabled: %d", enabled);
2655 GST_OBJECT_LOCK (trans);
2656 trans->priv->qos_enabled = enabled;
2657 GST_OBJECT_UNLOCK (trans);
2661 * gst_base_transform_is_qos_enabled:
2662 * @trans: a #GstBaseTransform
2664 * Queries if the transform will handle QoS.
2666 * Returns: %TRUE if QoS is enabled.
2671 gst_base_transform_is_qos_enabled (GstBaseTransform * trans)
2675 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2677 GST_OBJECT_LOCK (trans);
2678 result = trans->priv->qos_enabled;
2679 GST_OBJECT_UNLOCK (trans);
2685 * gst_base_transform_set_gap_aware:
2686 * @trans: a #GstBaseTransform
2687 * @gap_aware: New state
2689 * If @gap_aware is %FALSE (the default), output buffers will have the
2690 * %GST_BUFFER_FLAG_GAP flag unset.
2692 * If set to %TRUE, the element must handle output buffers with this flag set
2693 * correctly, i.e. it can assume that the buffer contains neutral data but must
2694 * unset the flag if the output is no neutral data.
2699 gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware)
2701 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2703 GST_OBJECT_LOCK (trans);
2704 trans->priv->gap_aware = gap_aware;
2705 GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware);
2706 GST_OBJECT_UNLOCK (trans);
2710 * gst_base_transform_set_prefer_passthrough:
2711 * @trans: a #GstBaseTransform
2712 * @prefer_passthrough: New state
2714 * If @prefer_passthrough is %TRUE (the default), @trans will check and
2715 * prefer passthrough caps from the list of caps returned by the
2716 * transform_caps vmethod.
2718 * If set to %FALSE, the element must order the caps returned from the
2719 * transform_caps function in such a way that the preferred format is
2720 * first in the list. This can be interesting for transforms that can do
2721 * passthrough transforms but prefer to do something else, like a
2729 gst_base_transform_set_prefer_passthrough (GstBaseTransform * trans,
2730 gboolean prefer_passthrough)
2732 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2734 GST_OBJECT_LOCK (trans);
2735 trans->priv->prefer_passthrough = prefer_passthrough;
2736 GST_DEBUG_OBJECT (trans, "prefer passthrough %d", prefer_passthrough);
2737 GST_OBJECT_UNLOCK (trans);
2741 * gst_base_transform_reconfigure_sink:
2742 * @trans: a #GstBaseTransform
2744 * Instructs @trans to request renegotiation upstream. This function is
2745 * typically called after properties on the transform were set that
2746 * influence the input format.
2749 gst_base_transform_reconfigure_sink (GstBaseTransform * trans)
2751 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2753 /* push the renegotiate event */
2754 if (!gst_pad_push_event (GST_BASE_TRANSFORM_SINK_PAD (trans),
2755 gst_event_new_reconfigure ()))
2756 GST_DEBUG_OBJECT (trans, "Renegotiate event wasn't handled");
2760 * gst_base_transform_reconfigure_src:
2761 * @trans: a #GstBaseTransform
2763 * Instructs @trans to renegotiate a new downstream transform on the next
2764 * buffer. This function is typically called after properties on the transform
2765 * were set that influence the output format.
2768 gst_base_transform_reconfigure_src (GstBaseTransform * trans)
2770 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2772 gst_pad_mark_reconfigure (trans->srcpad);
2776 * gst_base_transform_get_buffer_pool:
2777 * @trans: a #GstBaseTransform
2779 * Returns: (transfer full): the instance of the #GstBufferPool used
2780 * by @trans; free it after use it
2783 gst_base_transform_get_buffer_pool (GstBaseTransform * trans)
2785 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), NULL);
2787 if (trans->priv->pool)
2788 return gst_object_ref (trans->priv->pool);
2794 * gst_base_transform_get_allocator:
2795 * @trans: a #GstBaseTransform
2796 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
2798 * @params: (out) (allow-none) (transfer full): the
2799 * #GstAllocationParams of @allocator
2801 * Lets #GstBaseTransform sub-classes to know the memory @allocator
2802 * used by the base class and its @params.
2804 * Unref the @allocator after use it.
2807 gst_base_transform_get_allocator (GstBaseTransform * trans,
2808 GstAllocator ** allocator, GstAllocationParams * params)
2810 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2813 *allocator = trans->priv->allocator ?
2814 gst_object_ref (trans->priv->allocator) : NULL;
2817 *params = trans->priv->params;
2821 * gst_base_transform_update_src_caps:
2822 * @trans: a #GstBaseTransform
2823 * @updated_caps: An updated version of the srcpad caps to be pushed
2826 * Updates the srcpad caps and send the caps downstream. This function
2827 * can be used by subclasses when they have already negotiated their caps
2828 * but found a change in them (or computed new informations). This way,
2829 * they can notify downstream about that change without loosing any
2832 * Returns: %TRUE if the caps could be send downstream %FALSE otherwise
2837 gst_base_transform_update_src_caps (GstBaseTransform * trans,
2838 GstCaps * updated_caps)
2840 g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2842 if (gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (trans),
2843 gst_event_new_caps (updated_caps))) {
2844 gst_pad_mark_reconfigure (trans->srcpad);