basetransform: make more stuff private
[platform/upstream/gstreamer.git] / libs / gst / base / gstbasetransform.c
1 /* GStreamer
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>
7  *
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.
12  *
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.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstbasetransform
26  * @short_description: Base class for simple transform filters
27  * @see_also: #GstBaseSrc, #GstBaseSink
28  *
29  * This base class is for filter elements that process data.
30  *
31  * It provides for:
32  * <itemizedlist>
33  *   <listitem><para>one sinkpad and one srcpad</para></listitem>
34  *   <listitem><para>
35  *      Possible formats on sink and source pad implemented
36  *      with custom transform_caps function. By default uses
37  *      same format on sink and source.
38  *   </para></listitem>
39  *   <listitem><para>Handles state changes</para></listitem>
40  *   <listitem><para>Does flushing</para></listitem>
41  *   <listitem><para>Push mode</para></listitem>
42  *   <listitem><para>
43  *       Pull mode if the sub-class transform can operate on arbitrary data
44  *    </para></listitem>
45  * </itemizedlist>
46  *
47  * <refsect2>
48  * <title>Use Cases</title>
49  * <para>
50  * <orderedlist>
51  * <listitem>
52  *   <itemizedlist><title>Passthrough mode</title>
53  *   <listitem><para>
54  *     Element has no interest in modifying the buffer. It may want to inspect it,
55  *     in which case the element should have a transform_ip function. If there
56  *     is no transform_ip function in passthrough mode, the buffer is pushed
57  *     intact.
58  *   </para></listitem>
59  *   <listitem><para>
60  *     On the GstBaseTransformClass is the passthrough_on_same_caps variable
61  *     which will automatically set/unset passthrough based on whether the
62  *     element negotiates the same caps on both pads.
63  *   </para></listitem>
64  *   <listitem><para>
65  *     passthrough_on_same_caps on an element that doesn't implement a
66  *     transform_caps function is useful for elements that only inspect data
67  *     (such as level)
68  *   </para></listitem>
69  *   </itemizedlist>
70  *   <itemizedlist>
71  *   <title>Example elements</title>
72  *     <listitem>Level</listitem>
73  *     <listitem>Videoscale, audioconvert, ffmpegcolorspace, audioresample in
74  *     certain modes.</listitem>
75  *   </itemizedlist>
76  * </listitem>
77  * <listitem>
78  *   <itemizedlist>
79  *     <title>Modifications in-place - input buffer and output buffer are the
80  *     same thing.</title>
81  *   <listitem><para>
82  *     The element must implement a transform_ip function.
83  *   </para></listitem>
84  *   <listitem><para>
85  *     Output buffer size must <= input buffer size
86  *   </para></listitem>
87  *   <listitem><para>
88  *     If the always_in_place flag is set, non-writable buffers will be copied
89  *     and passed to the transform_ip function, otherwise a new buffer will be
90  *     created and the transform function called.
91  *   </para></listitem>
92  *   <listitem><para>
93  *     Incoming writable buffers will be passed to the transform_ip function
94  *     immediately.  </para></listitem>
95  *   <listitem><para>
96  *     only implementing transform_ip and not transform implies always_in_place
97  *     = TRUE
98  *   </para></listitem>
99  *   </itemizedlist>
100  *   <itemizedlist>
101  *   <title>Example elements</title>
102  *     <listitem>Volume</listitem>
103  *     <listitem>Audioconvert in certain modes (signed/unsigned
104  *     conversion)</listitem>
105  *     <listitem>ffmpegcolorspace in certain modes (endianness
106  *     swapping)</listitem>
107  *   </itemizedlist>
108  *  </listitem>
109  * <listitem>
110  *   <itemizedlist>
111  *   <title>Modifications only to the caps/metadata of a buffer</title>
112  *   <listitem><para>
113  *     The element does not require writable data, but non-writable buffers
114  *     should be subbuffered so that the meta-information can be replaced.
115  *   </para></listitem>
116  *   <listitem><para>
117  *     Elements wishing to operate in this mode should replace the
118  *     prepare_output_buffer method to create subbuffers of the input buffer
119  *     and set always_in_place to TRUE
120  *   </para></listitem>
121  *   </itemizedlist>
122  *   <itemizedlist>
123  *   <title>Example elements</title>
124  *     <listitem>Capsfilter when setting caps on outgoing buffers that have
125  *     none.</listitem>
126  *     <listitem>identity when it is going to re-timestamp buffers by
127  *     datarate.</listitem>
128  *   </itemizedlist>
129  * </listitem>
130  * <listitem>
131  *   <itemizedlist><title>Normal mode</title>
132  *   <listitem><para>
133  *     always_in_place flag is not set, or there is no transform_ip function
134  *   </para></listitem>
135  *   <listitem><para>
136  *     Element will receive an input buffer and output buffer to operate on.
137  *   </para></listitem>
138  *   <listitem><para>
139  *     Output buffer is allocated by calling the prepare_output_buffer function.
140  *   </para></listitem>
141  *   </itemizedlist>
142  *   <itemizedlist>
143  *   <title>Example elements</title>
144  *     <listitem>Videoscale, ffmpegcolorspace, audioconvert when doing
145  *     scaling/conversions</listitem>
146  *   </itemizedlist>
147  * </listitem>
148  * <listitem>
149  *   <itemizedlist><title>Special output buffer allocations</title>
150  *   <listitem><para>
151  *     Elements which need to do special allocation of their output buffers
152  *     other than what gst_buffer_pad_alloc allows should implement a
153  *     prepare_output_buffer method, which calls the parent implementation and
154  *     passes the newly allocated buffer.
155  *   </para></listitem>
156  *   </itemizedlist>
157  *   <itemizedlist>
158  *   <title>Example elements</title>
159  *     <listitem>efence</listitem>
160  *   </itemizedlist>
161  * </listitem>
162  * </orderedlist>
163  * </para>
164  * </refsect2>
165  * <refsect2>
166  * <title>Sub-class settable flags on GstBaseTransform</title>
167  * <para>
168  * <itemizedlist>
169  * <listitem><para>
170  *   <itemizedlist><title>passthrough</title>
171  *     <listitem><para>
172  *       Implies that in the current configuration, the sub-class is not
173  *       interested in modifying the buffers.
174  *     </para></listitem>
175  *     <listitem><para>
176  *       Elements which are always in passthrough mode whenever the same caps
177  *       has been negotiated on both pads can set the class variable
178  *       passthrough_on_same_caps to have this behaviour automatically.
179  *     </para></listitem>
180  *   </itemizedlist>
181  * </para></listitem>
182  * <listitem><para>
183  *   <itemizedlist><title>always_in_place</title>
184  *     <listitem><para>
185  *       Determines whether a non-writable buffer will be copied before passing
186  *       to the transform_ip function.
187  *     </para></listitem>
188  *     <listitem><para>
189  *       Implied TRUE if no transform function is implemented.
190  *     </para></listitem>
191  *     <listitem><para>
192  *       Implied FALSE if ONLY transform function is implemented.
193  *     </para></listitem>
194  *   </itemizedlist>
195  * </para></listitem>
196  * </itemizedlist>
197  * </para>
198  * </refsect2>
199  */
200
201 #ifdef HAVE_CONFIG_H
202 #  include "config.h"
203 #endif
204
205 #include <stdlib.h>
206 #include <string.h>
207
208 #include "../../../gst/gst_private.h"
209 #include "../../../gst/gst-i18n-lib.h"
210 #include "../../../gst/glib-compat-private.h"
211 #include "gstbasetransform.h"
212
213 GST_DEBUG_CATEGORY_STATIC (gst_base_transform_debug);
214 #define GST_CAT_DEFAULT gst_base_transform_debug
215
216 /* BaseTransform signals and args */
217 enum
218 {
219   /* FILL ME */
220   LAST_SIGNAL
221 };
222
223 #define DEFAULT_PROP_QOS        FALSE
224
225 enum
226 {
227   PROP_0,
228   PROP_QOS
229 };
230
231 #define GST_BASE_TRANSFORM_GET_PRIVATE(obj)  \
232     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_TRANSFORM, GstBaseTransformPrivate))
233
234 struct _GstBaseTransformPrivate
235 {
236   /* Set by sub-class */
237   gboolean passthrough;
238   gboolean always_in_place;
239
240   GstCaps *cache_caps1;
241   gsize cache_caps1_size;
242   GstCaps *cache_caps2;
243   gsize cache_caps2_size;
244   gboolean have_same_caps;
245
246   gboolean negotiated;
247
248   /* QoS *//* with LOCK */
249   gboolean qos_enabled;
250   gdouble proportion;
251   GstClockTime earliest_time;
252   /* previous buffer had a discont */
253   gboolean discont;
254
255   GstPadMode pad_mode;
256
257   gboolean gap_aware;
258
259   gboolean reconfigure;
260
261   /* QoS stats */
262   guint64 processed;
263   guint64 dropped;
264
265   GstClockTime position_out;
266
267   GstBufferPool *pool;
268   gboolean pool_active;
269   GstAllocator *allocator;
270   GstAllocationParams params;
271   GstQuery *query;
272 };
273
274
275 static GstElementClass *parent_class = NULL;
276
277 static void gst_base_transform_class_init (GstBaseTransformClass * klass);
278 static void gst_base_transform_init (GstBaseTransform * trans,
279     GstBaseTransformClass * klass);
280
281 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
282  * method to get to the padtemplates */
283 GType
284 gst_base_transform_get_type (void)
285 {
286   static volatile gsize base_transform_type = 0;
287
288   if (g_once_init_enter (&base_transform_type)) {
289     GType _type;
290     static const GTypeInfo base_transform_info = {
291       sizeof (GstBaseTransformClass),
292       NULL,
293       NULL,
294       (GClassInitFunc) gst_base_transform_class_init,
295       NULL,
296       NULL,
297       sizeof (GstBaseTransform),
298       0,
299       (GInstanceInitFunc) gst_base_transform_init,
300     };
301
302     _type = g_type_register_static (GST_TYPE_ELEMENT,
303         "GstBaseTransform", &base_transform_info, G_TYPE_FLAG_ABSTRACT);
304     g_once_init_leave (&base_transform_type, _type);
305   }
306   return base_transform_type;
307 }
308
309 static void gst_base_transform_finalize (GObject * object);
310 static void gst_base_transform_set_property (GObject * object, guint prop_id,
311     const GValue * value, GParamSpec * pspec);
312 static void gst_base_transform_get_property (GObject * object, guint prop_id,
313     GValue * value, GParamSpec * pspec);
314 static gboolean gst_base_transform_src_activate_mode (GstPad * pad,
315     GstObject * parent, GstPadMode mode, gboolean active);
316 static gboolean gst_base_transform_sink_activate_mode (GstPad * pad,
317     GstObject * parent, GstPadMode mode, gboolean active);
318 static gboolean gst_base_transform_activate (GstBaseTransform * trans,
319     gboolean active);
320 static gboolean gst_base_transform_get_unit_size (GstBaseTransform * trans,
321     GstCaps * caps, gsize * size);
322
323 static gboolean gst_base_transform_src_event (GstPad * pad, GstObject * parent,
324     GstEvent * event);
325 static gboolean gst_base_transform_src_eventfunc (GstBaseTransform * trans,
326     GstEvent * event);
327 static gboolean gst_base_transform_sink_event (GstPad * pad, GstObject * parent,
328     GstEvent * event);
329 static gboolean gst_base_transform_sink_eventfunc (GstBaseTransform * trans,
330     GstEvent * event);
331 static GstFlowReturn gst_base_transform_getrange (GstPad * pad,
332     GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
333 static GstFlowReturn gst_base_transform_chain (GstPad * pad, GstObject * parent,
334     GstBuffer * buffer);
335 static GstCaps *gst_base_transform_default_transform_caps (GstBaseTransform *
336     trans, GstPadDirection direction, GstCaps * caps, GstCaps * filter);
337 static GstCaps *gst_base_transform_default_fixate_caps (GstBaseTransform *
338     trans, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
339 static GstCaps *gst_base_transform_query_caps (GstBaseTransform * trans,
340     GstPad * pad, GstCaps * filter);
341 static gboolean gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
342     GstPadDirection direction, GstCaps * caps);
343 static gboolean gst_base_transform_setcaps (GstBaseTransform * trans,
344     GstPad * pad, GstCaps * caps);
345 static gboolean gst_base_transform_default_decide_allocation (GstBaseTransform
346     * trans, GstQuery * query);
347 static gboolean gst_base_transform_default_propose_allocation (GstBaseTransform
348     * trans, GstQuery * decide_query, GstQuery * query);
349 static gboolean gst_base_transform_query (GstPad * pad, GstObject * parent,
350     GstQuery * query);
351 static gboolean gst_base_transform_default_query (GstBaseTransform * trans,
352     GstPadDirection direction, GstQuery * query);
353 static gboolean gst_base_transform_default_transform_size (GstBaseTransform *
354     trans, GstPadDirection direction, GstCaps * caps, gsize size,
355     GstCaps * othercaps, gsize * othersize);
356
357 static GstFlowReturn default_prepare_output_buffer (GstBaseTransform * trans,
358     GstBuffer * inbuf, GstBuffer ** outbuf);
359 static gboolean default_copy_metadata (GstBaseTransform * trans,
360     GstBuffer * inbuf, GstBuffer * outbuf);
361
362 /* static guint gst_base_transform_signals[LAST_SIGNAL] = { 0 }; */
363
364
365 static void
366 gst_base_transform_finalize (GObject * object)
367 {
368   G_OBJECT_CLASS (parent_class)->finalize (object);
369 }
370
371 static void
372 gst_base_transform_class_init (GstBaseTransformClass * klass)
373 {
374   GObjectClass *gobject_class;
375
376   gobject_class = G_OBJECT_CLASS (klass);
377
378   GST_DEBUG_CATEGORY_INIT (gst_base_transform_debug, "basetransform", 0,
379       "basetransform element");
380
381   GST_DEBUG ("gst_base_transform_class_init");
382
383   g_type_class_add_private (klass, sizeof (GstBaseTransformPrivate));
384
385   parent_class = g_type_class_peek_parent (klass);
386
387   gobject_class->set_property = gst_base_transform_set_property;
388   gobject_class->get_property = gst_base_transform_get_property;
389
390   g_object_class_install_property (gobject_class, PROP_QOS,
391       g_param_spec_boolean ("qos", "QoS", "Handle Quality-of-Service events",
392           DEFAULT_PROP_QOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
393
394   gobject_class->finalize = gst_base_transform_finalize;
395
396   klass->passthrough_on_same_caps = FALSE;
397
398   klass->transform_caps =
399       GST_DEBUG_FUNCPTR (gst_base_transform_default_transform_caps);
400   klass->fixate_caps =
401       GST_DEBUG_FUNCPTR (gst_base_transform_default_fixate_caps);
402   klass->accept_caps =
403       GST_DEBUG_FUNCPTR (gst_base_transform_acceptcaps_default);
404   klass->query = GST_DEBUG_FUNCPTR (gst_base_transform_default_query);
405   klass->decide_allocation =
406       GST_DEBUG_FUNCPTR (gst_base_transform_default_decide_allocation);
407   klass->propose_allocation =
408       GST_DEBUG_FUNCPTR (gst_base_transform_default_propose_allocation);
409   klass->transform_size =
410       GST_DEBUG_FUNCPTR (gst_base_transform_default_transform_size);
411
412   klass->sink_event = GST_DEBUG_FUNCPTR (gst_base_transform_sink_eventfunc);
413   klass->src_event = GST_DEBUG_FUNCPTR (gst_base_transform_src_eventfunc);
414   klass->prepare_output_buffer =
415       GST_DEBUG_FUNCPTR (default_prepare_output_buffer);
416   klass->copy_metadata = GST_DEBUG_FUNCPTR (default_copy_metadata);
417 }
418
419 static void
420 gst_base_transform_init (GstBaseTransform * trans,
421     GstBaseTransformClass * bclass)
422 {
423   GstPadTemplate *pad_template;
424   GstBaseTransformPrivate *priv;
425
426   GST_DEBUG ("gst_base_transform_init");
427
428   priv = trans->priv = GST_BASE_TRANSFORM_GET_PRIVATE (trans);
429
430   pad_template =
431       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
432   g_return_if_fail (pad_template != NULL);
433   trans->sinkpad = gst_pad_new_from_template (pad_template, "sink");
434   gst_pad_set_event_function (trans->sinkpad,
435       GST_DEBUG_FUNCPTR (gst_base_transform_sink_event));
436   gst_pad_set_chain_function (trans->sinkpad,
437       GST_DEBUG_FUNCPTR (gst_base_transform_chain));
438   gst_pad_set_activatemode_function (trans->sinkpad,
439       GST_DEBUG_FUNCPTR (gst_base_transform_sink_activate_mode));
440   gst_pad_set_query_function (trans->sinkpad,
441       GST_DEBUG_FUNCPTR (gst_base_transform_query));
442   gst_element_add_pad (GST_ELEMENT (trans), trans->sinkpad);
443
444   pad_template =
445       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
446   g_return_if_fail (pad_template != NULL);
447   trans->srcpad = gst_pad_new_from_template (pad_template, "src");
448   gst_pad_set_event_function (trans->srcpad,
449       GST_DEBUG_FUNCPTR (gst_base_transform_src_event));
450   gst_pad_set_getrange_function (trans->srcpad,
451       GST_DEBUG_FUNCPTR (gst_base_transform_getrange));
452   gst_pad_set_activatemode_function (trans->srcpad,
453       GST_DEBUG_FUNCPTR (gst_base_transform_src_activate_mode));
454   gst_pad_set_query_function (trans->srcpad,
455       GST_DEBUG_FUNCPTR (gst_base_transform_query));
456   gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
457
458   priv->qos_enabled = DEFAULT_PROP_QOS;
459   priv->cache_caps1 = NULL;
460   priv->cache_caps2 = NULL;
461   priv->pad_mode = GST_PAD_MODE_NONE;
462   priv->gap_aware = FALSE;
463
464   priv->passthrough = FALSE;
465   if (bclass->transform == NULL) {
466     /* If no transform function, always_in_place is TRUE */
467     GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
468     priv->always_in_place = TRUE;
469
470     if (bclass->transform_ip == NULL) {
471       GST_DEBUG_OBJECT (trans, "setting passthrough TRUE");
472       priv->passthrough = TRUE;
473     }
474   }
475
476   priv->processed = 0;
477   priv->dropped = 0;
478 }
479
480 static GstCaps *
481 gst_base_transform_default_transform_caps (GstBaseTransform * trans,
482     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
483 {
484   GstCaps *ret;
485
486   GST_DEBUG_OBJECT (trans, "identity from: %" GST_PTR_FORMAT, caps);
487   /* no transform function, use the identity transform */
488   if (filter) {
489     ret = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
490   } else {
491     ret = gst_caps_ref (caps);
492   }
493   return ret;
494 }
495
496 /* given @caps on the src or sink pad (given by @direction)
497  * calculate the possible caps on the other pad.
498  *
499  * Returns new caps, unref after usage.
500  */
501 static GstCaps *
502 gst_base_transform_transform_caps (GstBaseTransform * trans,
503     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
504 {
505   GstCaps *ret = NULL;
506   GstBaseTransformClass *klass;
507
508   if (caps == NULL)
509     return NULL;
510
511   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
512
513   /* if there is a custom transform function, use this */
514   if (klass->transform_caps) {
515     GST_DEBUG_OBJECT (trans, "transform caps (direction = %d)", direction);
516
517     GST_LOG_OBJECT (trans, "from: %" GST_PTR_FORMAT, caps);
518     ret = klass->transform_caps (trans, direction, caps, filter);
519     GST_LOG_OBJECT (trans, "  to: %" GST_PTR_FORMAT, ret);
520
521 #ifndef G_DISABLE_ASSERT
522     if (filter) {
523       if (!gst_caps_is_subset (ret, filter)) {
524         GstCaps *intersection;
525
526         GST_ERROR_OBJECT (trans,
527             "transform_caps returned caps %" GST_PTR_FORMAT
528             " which are not a real subset of the filter caps %"
529             GST_PTR_FORMAT, ret, filter);
530         g_warning ("%s: transform_caps returned caps which are not a real "
531             "subset of the filter caps", GST_ELEMENT_NAME (trans));
532
533         intersection =
534             gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
535         gst_caps_unref (ret);
536         ret = intersection;
537       }
538     }
539 #endif
540   }
541
542   GST_DEBUG_OBJECT (trans, "to: %" GST_PTR_FORMAT, ret);
543
544   return ret;
545 }
546
547 static gboolean
548 gst_base_transform_default_transform_size (GstBaseTransform * trans,
549     GstPadDirection direction, GstCaps * caps, gsize size,
550     GstCaps * othercaps, gsize * othersize)
551 {
552   gsize inunitsize, outunitsize, units;
553   GstBaseTransformClass *klass;
554
555   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
556
557   if (klass->get_unit_size == NULL) {
558     /* if there is no transform_size and no unit_size, it means the
559      * element does not modify the size of a buffer */
560     *othersize = size;
561   } else {
562     /* there is no transform_size function, we have to use the unit_size
563      * functions. This method assumes there is a fixed unit_size associated with
564      * each caps. We provide the same amount of units on both sides. */
565     if (!gst_base_transform_get_unit_size (trans, caps, &inunitsize))
566       goto no_in_size;
567
568     GST_DEBUG_OBJECT (trans,
569         "input size %" G_GSIZE_FORMAT ", input unit size %" G_GSIZE_FORMAT,
570         size, inunitsize);
571
572     /* input size must be a multiple of the unit_size of the input caps */
573     if (inunitsize == 0 || (size % inunitsize != 0))
574       goto no_multiple;
575
576     /* get the amount of units */
577     units = size / inunitsize;
578
579     /* now get the unit size of the output */
580     if (!gst_base_transform_get_unit_size (trans, othercaps, &outunitsize))
581       goto no_out_size;
582
583     /* the output size is the unit_size times the amount of units on the
584      * input */
585     *othersize = units * outunitsize;
586     GST_DEBUG_OBJECT (trans, "transformed size to %" G_GSIZE_FORMAT,
587         *othersize);
588   }
589   return TRUE;
590
591   /* ERRORS */
592 no_in_size:
593   {
594     GST_DEBUG_OBJECT (trans, "could not get in_size");
595     g_warning ("%s: could not get in_size", GST_ELEMENT_NAME (trans));
596     return FALSE;
597   }
598 no_multiple:
599   {
600     GST_DEBUG_OBJECT (trans, "Size %" G_GSIZE_FORMAT " is not a multiple of"
601         "unit size %" G_GSIZE_FORMAT, size, inunitsize);
602     g_warning ("%s: size %" G_GSIZE_FORMAT " is not a multiple of unit size %"
603         G_GSIZE_FORMAT, GST_ELEMENT_NAME (trans), size, inunitsize);
604     return FALSE;
605   }
606 no_out_size:
607   {
608     GST_DEBUG_OBJECT (trans, "could not get out_size");
609     g_warning ("%s: could not get out_size", GST_ELEMENT_NAME (trans));
610     return FALSE;
611   }
612 }
613
614 /* transform a buffer of @size with @caps on the pad with @direction to
615  * the size of a buffer with @othercaps and store the result in @othersize
616  *
617  * We have two ways of doing this:
618  *  1) use a custom transform size function, this is for complicated custom
619  *     cases with no fixed unit_size.
620  *  2) use the unit_size functions where there is a relationship between the
621  *     caps and the size of a buffer.
622  */
623 static gboolean
624 gst_base_transform_transform_size (GstBaseTransform * trans,
625     GstPadDirection direction, GstCaps * caps,
626     gsize size, GstCaps * othercaps, gsize * othersize)
627 {
628   GstBaseTransformClass *klass;
629   gboolean ret = FALSE;
630
631   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
632
633   GST_DEBUG_OBJECT (trans,
634       "asked to transform size %" G_GSIZE_FORMAT " for caps %"
635       GST_PTR_FORMAT " to size for caps %" GST_PTR_FORMAT " in direction %s",
636       size, caps, othercaps, direction == GST_PAD_SRC ? "SRC" : "SINK");
637
638   if (klass->transform_size) {
639     /* if there is a custom transform function, use this */
640     ret = klass->transform_size (trans, direction, caps, size, othercaps,
641         othersize);
642   }
643   return ret;
644 }
645
646 /* get the caps that can be handled by @pad. We perform:
647  *
648  *  - take the caps of peer of otherpad,
649  *  - filter against the padtemplate of otherpad, 
650  *  - calculate all transforms of remaining caps
651  *  - filter against template of @pad
652  *
653  * If there is no peer, we simply return the caps of the padtemplate of pad.
654  */
655 static GstCaps *
656 gst_base_transform_query_caps (GstBaseTransform * trans, GstPad * pad,
657     GstCaps * filter)
658 {
659   GstPad *otherpad;
660   GstCaps *peercaps, *caps, *temp, *peerfilter = NULL;
661   GstCaps *templ, *otempl;
662
663   otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
664
665   templ = gst_pad_get_pad_template_caps (pad);
666   otempl = gst_pad_get_pad_template_caps (otherpad);
667
668   /* we can do what the peer can */
669   if (filter) {
670     GST_DEBUG_OBJECT (pad, "filter caps  %" GST_PTR_FORMAT, filter);
671
672     /* filtered against our padtemplate on the other side */
673     GST_DEBUG_OBJECT (pad, "our template  %" GST_PTR_FORMAT, templ);
674     temp = gst_caps_intersect_full (filter, templ, GST_CAPS_INTERSECT_FIRST);
675     GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
676
677     /* then see what we can transform this to */
678     peerfilter = gst_base_transform_transform_caps (trans,
679         GST_PAD_DIRECTION (pad), temp, NULL);
680     GST_DEBUG_OBJECT (pad, "transformed  %" GST_PTR_FORMAT, peerfilter);
681     gst_caps_unref (temp);
682
683     /* and filter against the template of this pad */
684     GST_DEBUG_OBJECT (pad, "our template  %" GST_PTR_FORMAT, otempl);
685     /* We keep the caps sorted like the returned caps */
686     temp =
687         gst_caps_intersect_full (peerfilter, otempl, GST_CAPS_INTERSECT_FIRST);
688     GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
689     gst_caps_unref (peerfilter);
690     peerfilter = temp;
691   }
692
693   peercaps = gst_pad_peer_query_caps (otherpad, peerfilter);
694
695   if (peerfilter)
696     gst_caps_unref (peerfilter);
697
698   if (peercaps) {
699     GST_DEBUG_OBJECT (pad, "peer caps  %" GST_PTR_FORMAT, peercaps);
700
701     /* filtered against our padtemplate on the other side */
702     GST_DEBUG_OBJECT (pad, "our template  %" GST_PTR_FORMAT, otempl);
703     temp = gst_caps_intersect_full (peercaps, otempl, GST_CAPS_INTERSECT_FIRST);
704     GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
705   } else {
706     temp = gst_caps_ref (otempl);
707   }
708
709   /* then see what we can transform this to */
710   caps = gst_base_transform_transform_caps (trans,
711       GST_PAD_DIRECTION (otherpad), temp, filter);
712   GST_DEBUG_OBJECT (pad, "transformed  %" GST_PTR_FORMAT, caps);
713   gst_caps_unref (temp);
714   if (caps == NULL)
715     goto done;
716
717   if (peercaps) {
718     /* and filter against the template of this pad */
719     GST_DEBUG_OBJECT (pad, "our template  %" GST_PTR_FORMAT, templ);
720     /* We keep the caps sorted like the returned caps */
721     temp = gst_caps_intersect_full (caps, templ, GST_CAPS_INTERSECT_FIRST);
722     GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, temp);
723     gst_caps_unref (caps);
724     caps = temp;
725
726     /* Now try if we can put the untransformed downstream caps first */
727     temp = gst_caps_intersect_full (peercaps, caps, GST_CAPS_INTERSECT_FIRST);
728     if (!gst_caps_is_empty (temp)) {
729       caps = gst_caps_merge (temp, caps);
730     } else {
731       gst_caps_unref (temp);
732     }
733   } else {
734     gst_caps_unref (caps);
735     /* no peer or the peer can do anything, our padtemplate is enough then */
736     if (filter) {
737       caps = gst_caps_intersect_full (filter, templ, GST_CAPS_INTERSECT_FIRST);
738     } else {
739       caps = gst_caps_ref (templ);
740     }
741   }
742
743 done:
744   GST_DEBUG_OBJECT (trans, "returning  %" GST_PTR_FORMAT, caps);
745
746   if (peercaps)
747     gst_caps_unref (peercaps);
748
749   gst_caps_unref (templ);
750   gst_caps_unref (otempl);
751
752   return caps;
753 }
754
755 /* takes ownership of the pool, allocator and query */
756 static gboolean
757 gst_base_transform_set_allocation (GstBaseTransform * trans,
758     GstBufferPool * pool, GstAllocator * allocator,
759     GstAllocationParams * params, GstQuery * query)
760 {
761   GstAllocator *oldalloc;
762   GstBufferPool *oldpool;
763   GstQuery *oldquery;
764   GstBaseTransformPrivate *priv = trans->priv;
765
766   GST_OBJECT_LOCK (trans);
767   oldpool = priv->pool;
768   priv->pool = pool;
769   priv->pool_active = FALSE;
770
771   oldalloc = priv->allocator;
772   priv->allocator = allocator;
773
774   oldquery = priv->query;
775   priv->query = query;
776
777   if (params)
778     priv->params = *params;
779   else
780     gst_allocation_params_init (&priv->params);
781   GST_OBJECT_UNLOCK (trans);
782
783   if (oldpool) {
784     GST_DEBUG_OBJECT (trans, "deactivating old pool %p", oldpool);
785     gst_buffer_pool_set_active (oldpool, FALSE);
786     gst_object_unref (oldpool);
787   }
788   if (oldalloc) {
789     gst_allocator_unref (oldalloc);
790   }
791   if (oldquery) {
792     gst_query_unref (oldquery);
793   }
794   return TRUE;
795
796 }
797
798 static gboolean
799 gst_base_transform_default_decide_allocation (GstBaseTransform * trans,
800     GstQuery * query)
801 {
802   guint i, n_metas;
803   GstBaseTransformClass *klass;
804
805   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
806
807   n_metas = gst_query_get_n_allocation_metas (query);
808   for (i = 0; i < n_metas; i++) {
809     GType api;
810     gboolean remove;
811
812     api = gst_query_parse_nth_allocation_meta (query, i);
813
814     /* by default we remove all metadata, subclasses should implement a
815      * filter_meta function */
816     if (gst_meta_api_type_has_tag (api, GST_META_TAG_MEMORY)) {
817       /* remove all memory dependent metadata because we are going to have to
818        * allocate different memory for input and output. */
819       GST_LOG_OBJECT (trans, "removing memory specific metadata %s",
820           g_type_name (api));
821       remove = TRUE;
822     } else if (G_LIKELY (klass->filter_meta)) {
823       /* remove if the subclass said so */
824       remove = !klass->filter_meta (trans, query, api);
825       GST_LOG_OBJECT (trans, "filter_meta for api %s returned: %s",
826           g_type_name (api), (remove ? "remove" : "keep"));
827     } else {
828       GST_LOG_OBJECT (trans, "removing metadata %s", g_type_name (api));
829       remove = TRUE;
830     }
831
832     if (remove) {
833       gst_query_remove_nth_allocation_meta (query, i);
834       i--;
835       n_metas--;
836     }
837   }
838   return TRUE;
839 }
840
841 static gboolean
842 gst_base_transform_do_bufferpool (GstBaseTransform * trans, GstCaps * outcaps)
843 {
844   GstQuery *query;
845   gboolean result = TRUE;
846   GstBufferPool *pool;
847   guint size, min, max;
848   GstBaseTransformClass *klass;
849   GstBaseTransformPrivate *priv = trans->priv;
850   GstAllocator *allocator;
851   GstAllocationParams params;
852
853   /* there are these possibilities:
854    *
855    * 1) we negotiated passthrough, we can proxy the bufferpool directly and we
856    *    will do that whenever some upstream does an allocation query.
857    * 2) we need to do a transform, we need to get a bufferpool from downstream
858    *    and configure it. When upstream does the ALLOCATION query, the
859    *    propose_allocation vmethod will be called and we will configure the
860    *    upstream allocator with our proposed values then.
861    */
862   if (priv->passthrough || priv->always_in_place) {
863     /* we are in passthrough, the input buffer is never copied and always passed
864      * along. We never allocate an output buffer on the srcpad. What we do is
865      * let the upstream element decide if it wants to use a bufferpool and
866      * then we will proxy the downstream pool */
867     GST_DEBUG_OBJECT (trans, "we're passthough, delay bufferpool");
868     gst_base_transform_set_allocation (trans, NULL, NULL, NULL, NULL);
869     return TRUE;
870   }
871
872   /* not passthrough, we need to allocate */
873   /* find a pool for the negotiated caps now */
874   GST_DEBUG_OBJECT (trans, "doing allocation query");
875   query = gst_query_new_allocation (outcaps, TRUE);
876   if (!gst_pad_peer_query (trans->srcpad, query)) {
877     /* not a problem, just debug a little */
878     GST_DEBUG_OBJECT (trans, "peer ALLOCATION query failed");
879   }
880
881   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
882
883   GST_DEBUG_OBJECT (trans, "calling decide_allocation");
884   if (G_LIKELY (klass->decide_allocation))
885     if ((result = klass->decide_allocation (trans, query)) == FALSE)
886       goto no_decide_allocation;
887
888   /* we got configuration from our peer or the decide_allocation method,
889    * parse them */
890   if (gst_query_get_n_allocation_params (query) > 0) {
891     /* try the allocator */
892     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
893   } else {
894     allocator = NULL;
895     gst_allocation_params_init (&params);
896   }
897
898   if (gst_query_get_n_allocation_pools (query) > 0) {
899     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
900
901     if (pool == NULL) {
902       /* no pool, just parameters, we can make our own */
903       GST_DEBUG_OBJECT (trans, "no pool, making new pool");
904       pool = gst_buffer_pool_new ();
905     }
906   } else {
907     pool = NULL;
908     size = min = max = 0;
909   }
910
911   /* now configure */
912   if (pool) {
913     GstStructure *config;
914
915     config = gst_buffer_pool_get_config (pool);
916     gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
917     gst_buffer_pool_config_set_allocator (config, allocator, &params);
918     gst_buffer_pool_set_config (pool, config);
919   }
920   /* and store */
921   result =
922       gst_base_transform_set_allocation (trans, pool, allocator, &params,
923       query);
924
925   return result;
926
927   /* Errors */
928 no_decide_allocation:
929   {
930     GST_WARNING_OBJECT (trans, "Subclass failed to decide allocation");
931     gst_query_unref (query);
932
933     return result;
934   }
935 }
936
937 /* function triggered when the in and out caps are negotiated and need
938  * to be configured in the subclass. */
939 static gboolean
940 gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
941     GstCaps * out)
942 {
943   gboolean ret = TRUE;
944   GstBaseTransformClass *klass;
945   GstBaseTransformPrivate *priv = trans->priv;
946
947   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
948
949   GST_DEBUG_OBJECT (trans, "in caps:  %" GST_PTR_FORMAT, in);
950   GST_DEBUG_OBJECT (trans, "out caps: %" GST_PTR_FORMAT, out);
951
952   /* clear the cache */
953   gst_caps_replace (&priv->cache_caps1, NULL);
954   gst_caps_replace (&priv->cache_caps2, NULL);
955
956   /* figure out same caps state */
957   priv->have_same_caps = gst_caps_is_equal (in, out);
958   GST_DEBUG_OBJECT (trans, "have_same_caps: %d", priv->have_same_caps);
959
960   /* If we've a transform_ip method and same input/output caps, set in_place
961    * by default. If for some reason the sub-class prefers using a transform
962    * function, it can clear the in place flag in the set_caps */
963   gst_base_transform_set_in_place (trans,
964       klass->transform_ip && priv->have_same_caps);
965
966   /* Set the passthrough if the class wants passthrough_on_same_caps
967    * and we have the same caps on each pad */
968   if (klass->passthrough_on_same_caps)
969     gst_base_transform_set_passthrough (trans, priv->have_same_caps);
970
971   /* now configure the element with the caps */
972   if (klass->set_caps) {
973     GST_DEBUG_OBJECT (trans, "Calling set_caps method to setup caps");
974     ret = klass->set_caps (trans, in, out);
975   }
976
977   return ret;
978 }
979
980 static GstCaps *
981 gst_base_transform_default_fixate_caps (GstBaseTransform * trans,
982     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
983 {
984   othercaps = gst_caps_fixate (othercaps);
985   GST_DEBUG_OBJECT (trans, "fixated to %" GST_PTR_FORMAT, othercaps);
986
987   return othercaps;
988 }
989
990 /* given a fixed @caps on @pad, create the best possible caps for the
991  * other pad.
992  * @caps must be fixed when calling this function.
993  *
994  * This function calls the transform caps vmethod of the basetransform to figure
995  * out the possible target formats. It then tries to select the best format from
996  * this list by:
997  *
998  * - attempt passthrough if the target caps is a superset of the input caps
999  * - fixating by using peer caps
1000  * - fixating with transform fixate function
1001  * - fixating with pad fixate functions.
1002  *
1003  * this function returns a caps that can be transformed into and is accepted by
1004  * the peer element.
1005  */
1006 static GstCaps *
1007 gst_base_transform_find_transform (GstBaseTransform * trans, GstPad * pad,
1008     GstCaps * caps)
1009 {
1010   GstBaseTransformClass *klass;
1011   GstPad *otherpad, *otherpeer;
1012   GstCaps *othercaps;
1013   gboolean is_fixed;
1014
1015   /* caps must be fixed here, this is a programming error if it's not */
1016   g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
1017
1018   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1019
1020   otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1021   otherpeer = gst_pad_get_peer (otherpad);
1022
1023   /* see how we can transform the input caps. We need to do this even for
1024    * passthrough because it might be possible that this element cannot support
1025    * passthrough at all. */
1026   othercaps = gst_base_transform_transform_caps (trans,
1027       GST_PAD_DIRECTION (pad), caps, NULL);
1028
1029   /* The caps we can actually output is the intersection of the transformed
1030    * caps with the pad template for the pad */
1031   if (othercaps) {
1032     GstCaps *intersect, *templ_caps;
1033
1034     templ_caps = gst_pad_get_pad_template_caps (otherpad);
1035     GST_DEBUG_OBJECT (trans,
1036         "intersecting against padtemplate %" GST_PTR_FORMAT, templ_caps);
1037
1038     intersect =
1039         gst_caps_intersect_full (othercaps, templ_caps,
1040         GST_CAPS_INTERSECT_FIRST);
1041
1042     gst_caps_unref (othercaps);
1043     gst_caps_unref (templ_caps);
1044     othercaps = intersect;
1045   }
1046
1047   /* check if transform is empty */
1048   if (!othercaps || gst_caps_is_empty (othercaps))
1049     goto no_transform;
1050
1051   /* if the othercaps are not fixed, we need to fixate them, first attempt
1052    * is by attempting passthrough if the othercaps are a superset of caps. */
1053   /* FIXME. maybe the caps is not fixed because it has multiple structures of
1054    * fixed caps */
1055   is_fixed = gst_caps_is_fixed (othercaps);
1056   if (!is_fixed) {
1057     GST_DEBUG_OBJECT (trans,
1058         "transform returned non fixed  %" GST_PTR_FORMAT, othercaps);
1059
1060     /* Now let's see what the peer suggests based on our transformed caps */
1061     if (otherpeer) {
1062       GstCaps *peercaps, *intersection, *templ_caps;
1063
1064       GST_DEBUG_OBJECT (trans,
1065           "Checking peer caps with filter %" GST_PTR_FORMAT, othercaps);
1066
1067       peercaps = gst_pad_query_caps (otherpeer, othercaps);
1068       GST_DEBUG_OBJECT (trans, "Resulted in %" GST_PTR_FORMAT, peercaps);
1069       if (!gst_caps_is_empty (peercaps)) {
1070         templ_caps = gst_pad_get_pad_template_caps (otherpad);
1071
1072         GST_DEBUG_OBJECT (trans,
1073             "Intersecting with template caps %" GST_PTR_FORMAT, templ_caps);
1074
1075         intersection =
1076             gst_caps_intersect_full (peercaps, templ_caps,
1077             GST_CAPS_INTERSECT_FIRST);
1078         GST_DEBUG_OBJECT (trans, "Intersection: %" GST_PTR_FORMAT,
1079             intersection);
1080         gst_caps_unref (peercaps);
1081         gst_caps_unref (templ_caps);
1082         peercaps = intersection;
1083
1084         GST_DEBUG_OBJECT (trans,
1085             "Intersecting with transformed caps %" GST_PTR_FORMAT, othercaps);
1086         intersection =
1087             gst_caps_intersect_full (peercaps, othercaps,
1088             GST_CAPS_INTERSECT_FIRST);
1089         GST_DEBUG_OBJECT (trans, "Intersection: %" GST_PTR_FORMAT,
1090             intersection);
1091         gst_caps_unref (peercaps);
1092         gst_caps_unref (othercaps);
1093         othercaps = intersection;
1094       } else {
1095         othercaps = peercaps;
1096       }
1097
1098       is_fixed = gst_caps_is_fixed (othercaps);
1099     } else {
1100       GST_DEBUG_OBJECT (trans, "no peer, doing passthrough");
1101       gst_caps_unref (othercaps);
1102       othercaps = gst_caps_ref (caps);
1103       is_fixed = TRUE;
1104     }
1105   }
1106   if (gst_caps_is_empty (othercaps))
1107     goto no_transform_possible;
1108
1109   GST_DEBUG ("have %sfixed caps %" GST_PTR_FORMAT, (is_fixed ? "" : "non-"),
1110       othercaps);
1111
1112   /* second attempt at fixation, call the fixate vmethod */
1113   /* caps could be fixed but the subclass may want to add fields */
1114   if (klass->fixate_caps) {
1115     GST_DEBUG_OBJECT (trans, "calling fixate_caps for %" GST_PTR_FORMAT
1116         " using caps %" GST_PTR_FORMAT " on pad %s:%s", othercaps, caps,
1117         GST_DEBUG_PAD_NAME (otherpad));
1118     /* note that we pass the complete array of structures to the fixate
1119      * function, it needs to truncate itself */
1120     othercaps =
1121         klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
1122     is_fixed = gst_caps_is_fixed (othercaps);
1123     GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
1124   }
1125
1126   /* caps should be fixed now, if not we have to fail. */
1127   if (!is_fixed)
1128     goto could_not_fixate;
1129
1130   /* and peer should accept */
1131   if (otherpeer && !gst_pad_query_accept_caps (otherpeer, othercaps))
1132     goto peer_no_accept;
1133
1134   GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT
1135       ", and got final caps %" GST_PTR_FORMAT, caps, othercaps);
1136
1137   if (otherpeer)
1138     gst_object_unref (otherpeer);
1139
1140   return othercaps;
1141
1142   /* ERRORS */
1143 no_transform:
1144   {
1145     GST_DEBUG_OBJECT (trans,
1146         "transform returned useless  %" GST_PTR_FORMAT, othercaps);
1147     goto error_cleanup;
1148   }
1149 no_transform_possible:
1150   {
1151     GST_DEBUG_OBJECT (trans,
1152         "transform could not transform %" GST_PTR_FORMAT
1153         " in anything we support", caps);
1154     goto error_cleanup;
1155   }
1156 could_not_fixate:
1157   {
1158     GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
1159     goto error_cleanup;
1160   }
1161 peer_no_accept:
1162   {
1163     GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
1164         " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1165     goto error_cleanup;
1166   }
1167 error_cleanup:
1168   {
1169     if (otherpeer)
1170       gst_object_unref (otherpeer);
1171     if (othercaps)
1172       gst_caps_unref (othercaps);
1173     return NULL;
1174   }
1175 }
1176
1177 static gboolean
1178 gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
1179     GstPadDirection direction, GstCaps * caps)
1180 {
1181 #if 0
1182   GstPad *otherpad;
1183   GstCaps *othercaps = NULL;
1184 #endif
1185   gboolean ret = TRUE;
1186
1187 #if 0
1188   otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1189
1190   /* we need fixed caps for the check, fall back to the default implementation
1191    * if we don't */
1192   if (!gst_caps_is_fixed (caps))
1193 #endif
1194   {
1195     GstCaps *allowed;
1196
1197     GST_DEBUG_OBJECT (trans, "accept caps %" GST_PTR_FORMAT, caps);
1198
1199     /* get all the formats we can handle on this pad */
1200     if (direction == GST_PAD_SRC)
1201       allowed = gst_pad_query_caps (trans->srcpad, NULL);
1202     else
1203       allowed = gst_pad_query_caps (trans->sinkpad, NULL);
1204
1205     if (!allowed) {
1206       GST_DEBUG_OBJECT (trans, "gst_pad_query_caps() failed");
1207       goto no_transform_possible;
1208     }
1209
1210     GST_DEBUG_OBJECT (trans, "allowed caps %" GST_PTR_FORMAT, allowed);
1211
1212     /* intersect with the requested format */
1213     ret = gst_caps_is_subset (caps, allowed);
1214     gst_caps_unref (allowed);
1215
1216     if (!ret)
1217       goto no_transform_possible;
1218   }
1219 #if 0
1220   else {
1221     GST_DEBUG_OBJECT (pad, "accept caps %" GST_PTR_FORMAT, caps);
1222
1223     /* find best possible caps for the other pad as a way to see if we can
1224      * transform this caps. */
1225     othercaps = gst_base_transform_find_transform (trans, pad, caps, FALSE);
1226     if (!othercaps || gst_caps_is_empty (othercaps))
1227       goto no_transform_possible;
1228
1229     GST_DEBUG_OBJECT (pad, "we can transform to %" GST_PTR_FORMAT, othercaps);
1230   }
1231 #endif
1232
1233 done:
1234 #if 0
1235   /* We know it's always NULL since we never use it */
1236   if (othercaps)
1237     gst_caps_unref (othercaps);
1238 #endif
1239
1240   return ret;
1241
1242   /* ERRORS */
1243 no_transform_possible:
1244   {
1245     GST_DEBUG_OBJECT (trans,
1246         "transform could not transform %" GST_PTR_FORMAT
1247         " in anything we support", caps);
1248     ret = FALSE;
1249     goto done;
1250   }
1251 }
1252
1253 /* called when new caps arrive on the sink pad,
1254  * We try to find the best caps for the other side using our _find_transform()
1255  * function. If there are caps, we configure the transform for this new
1256  * transformation.
1257  */
1258 static gboolean
1259 gst_base_transform_setcaps (GstBaseTransform * trans, GstPad * pad,
1260     GstCaps * incaps)
1261 {
1262   GstBaseTransformPrivate *priv = trans->priv;
1263   GstCaps *outcaps;
1264   gboolean ret = TRUE;
1265
1266   GST_DEBUG_OBJECT (pad, "have new caps %p %" GST_PTR_FORMAT, incaps, incaps);
1267
1268   /* find best possible caps for the other pad */
1269   outcaps = gst_base_transform_find_transform (trans, pad, incaps);
1270   if (!outcaps || gst_caps_is_empty (outcaps))
1271     goto no_transform_possible;
1272
1273   /* configure the element now */
1274
1275   /* if we have the same caps, we can optimize and reuse the input caps */
1276   if (gst_caps_is_equal (incaps, outcaps)) {
1277     GST_INFO_OBJECT (trans, "reuse caps");
1278     gst_caps_unref (outcaps);
1279     outcaps = gst_caps_ref (incaps);
1280   }
1281
1282   /* call configure now */
1283   if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
1284     goto failed_configure;
1285
1286   GST_OBJECT_LOCK (trans->sinkpad);
1287   GST_OBJECT_FLAG_UNSET (trans->srcpad, GST_PAD_FLAG_NEED_RECONFIGURE);
1288   trans->priv->reconfigure = FALSE;
1289   GST_OBJECT_UNLOCK (trans->sinkpad);
1290
1291   /* let downstream know about our caps */
1292   gst_pad_push_event (trans->srcpad, gst_event_new_caps (outcaps));
1293
1294   if (ret) {
1295     /* try to get a pool when needed */
1296     ret = gst_base_transform_do_bufferpool (trans, outcaps);
1297   }
1298
1299 done:
1300   if (outcaps)
1301     gst_caps_unref (outcaps);
1302
1303   GST_OBJECT_LOCK (trans);
1304   priv->negotiated = ret;
1305   GST_OBJECT_UNLOCK (trans);
1306
1307   return ret;
1308
1309   /* ERRORS */
1310 no_transform_possible:
1311   {
1312     GST_WARNING_OBJECT (trans,
1313         "transform could not transform %" GST_PTR_FORMAT
1314         " in anything we support", incaps);
1315     ret = FALSE;
1316     goto done;
1317   }
1318 failed_configure:
1319   {
1320     GST_WARNING_OBJECT (trans, "FAILED to configure incaps %" GST_PTR_FORMAT
1321         " and outcaps %" GST_PTR_FORMAT, incaps, outcaps);
1322     ret = FALSE;
1323     goto done;
1324   }
1325 }
1326
1327 static gboolean
1328 gst_base_transform_default_propose_allocation (GstBaseTransform * trans,
1329     GstQuery * decide_query, GstQuery * query)
1330 {
1331   gboolean ret;
1332
1333   if (decide_query == NULL) {
1334     GST_DEBUG_OBJECT (trans, "doing passthrough query");
1335     ret = gst_pad_peer_query (trans->srcpad, query);
1336   } else {
1337     guint i, n_metas;
1338     /* non-passthrough, copy all metadata, decide_query does not contain the
1339      * metadata anymore that depends on the buffer memory */
1340     n_metas = gst_query_get_n_allocation_metas (decide_query);
1341     for (i = 0; i < n_metas; i++) {
1342       GType api;
1343
1344       api = gst_query_parse_nth_allocation_meta (decide_query, i);
1345       GST_DEBUG_OBJECT (trans, "proposing metadata %s", g_type_name (api));
1346       gst_query_add_allocation_meta (query, api);
1347     }
1348     ret = TRUE;
1349   }
1350   return ret;
1351 }
1352
1353 static gboolean
1354 gst_base_transform_default_query (GstBaseTransform * trans,
1355     GstPadDirection direction, GstQuery * query)
1356 {
1357   gboolean ret = FALSE;
1358   GstPad *pad, *otherpad;
1359   GstBaseTransformClass *klass;
1360   GstBaseTransformPrivate *priv = trans->priv;
1361
1362   if (direction == GST_PAD_SRC) {
1363     pad = trans->srcpad;
1364     otherpad = trans->sinkpad;
1365   } else {
1366     pad = trans->sinkpad;
1367     otherpad = trans->srcpad;
1368   }
1369
1370   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1371
1372   switch (GST_QUERY_TYPE (query)) {
1373     case GST_QUERY_ALLOCATION:
1374     {
1375       GstQuery *decide_query = NULL;
1376       gboolean negotiated;
1377
1378       /* can only be done on the sinkpad */
1379       if (direction != GST_PAD_SINK)
1380         goto done;
1381
1382       GST_OBJECT_LOCK (trans);
1383       if (G_UNLIKELY (!(negotiated = priv->negotiated))) {
1384         GST_DEBUG_OBJECT (trans,
1385             "not negotiated yet, can't answer ALLOCATION query");
1386         GST_OBJECT_UNLOCK (trans);
1387         goto done;
1388       }
1389       if ((decide_query = trans->priv->query))
1390         gst_query_ref (decide_query);
1391       GST_OBJECT_UNLOCK (trans);
1392
1393       GST_DEBUG_OBJECT (trans,
1394           "calling propose allocation with query %" GST_PTR_FORMAT,
1395           decide_query);
1396
1397       /* pass the query to the propose_allocation vmethod if any */
1398       if (G_LIKELY (klass->propose_allocation))
1399         ret = klass->propose_allocation (trans, decide_query, query);
1400       else
1401         ret = FALSE;
1402
1403       if (decide_query)
1404         gst_query_unref (decide_query);
1405
1406       GST_DEBUG_OBJECT (trans, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret,
1407           query);
1408       break;
1409     }
1410     case GST_QUERY_POSITION:
1411     {
1412       GstFormat format;
1413
1414       gst_query_parse_position (query, &format, NULL);
1415       if (format == GST_FORMAT_TIME && trans->segment.format == GST_FORMAT_TIME) {
1416         gint64 pos;
1417         ret = TRUE;
1418
1419         if ((direction == GST_PAD_SINK)
1420             || (trans->priv->position_out == GST_CLOCK_TIME_NONE)) {
1421           pos =
1422               gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1423               trans->segment.position);
1424         } else {
1425           pos = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1426               trans->priv->position_out);
1427         }
1428         gst_query_set_position (query, format, pos);
1429       } else {
1430         ret = gst_pad_peer_query (otherpad, query);
1431       }
1432       break;
1433     }
1434     case GST_QUERY_ACCEPT_CAPS:
1435     {
1436       GstCaps *caps;
1437
1438       gst_query_parse_accept_caps (query, &caps);
1439       if (klass->accept_caps) {
1440         ret = klass->accept_caps (trans, direction, caps);
1441         gst_query_set_accept_caps_result (query, ret);
1442         /* return TRUE, we answered the query */
1443         ret = TRUE;
1444       }
1445       break;
1446     }
1447     case GST_QUERY_CAPS:
1448     {
1449       GstCaps *filter, *caps;
1450
1451       gst_query_parse_caps (query, &filter);
1452       caps = gst_base_transform_query_caps (trans, pad, filter);
1453       gst_query_set_caps_result (query, caps);
1454       gst_caps_unref (caps);
1455       ret = TRUE;
1456       break;
1457     }
1458     default:
1459       ret = gst_pad_peer_query (otherpad, query);
1460       break;
1461   }
1462
1463 done:
1464   return ret;
1465 }
1466
1467 static gboolean
1468 gst_base_transform_query (GstPad * pad, GstObject * parent, GstQuery * query)
1469 {
1470   GstBaseTransform *trans;
1471   GstBaseTransformClass *bclass;
1472   gboolean ret = FALSE;
1473
1474   trans = GST_BASE_TRANSFORM (parent);
1475   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1476
1477   if (bclass->query)
1478     ret = bclass->query (trans, GST_PAD_DIRECTION (pad), query);
1479
1480   return ret;
1481 }
1482
1483 /* this function either returns the input buffer without incrementing the
1484  * refcount or it allocates a new (writable) buffer */
1485 static GstFlowReturn
1486 default_prepare_output_buffer (GstBaseTransform * trans,
1487     GstBuffer * inbuf, GstBuffer ** outbuf)
1488 {
1489   GstBaseTransformPrivate *priv;
1490   GstFlowReturn ret = GST_FLOW_OK;
1491   GstBaseTransformClass *bclass;
1492   GstCaps *incaps, *outcaps;
1493   gsize insize, outsize;
1494   gboolean res;
1495
1496   priv = trans->priv;
1497   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1498
1499   /* figure out how to allocate an output buffer */
1500   if (priv->passthrough) {
1501     /* passthrough, we will not modify the incomming buffer so we can just
1502      * reuse it */
1503     GST_DEBUG_OBJECT (trans, "passthrough: reusing input buffer");
1504     *outbuf = inbuf;
1505     goto done;
1506   }
1507
1508   /* we can't reuse the input buffer */
1509   if (priv->pool) {
1510     if (!priv->pool_active) {
1511       GST_DEBUG_OBJECT (trans, "setting pool %p active", priv->pool);
1512       if (!gst_buffer_pool_set_active (priv->pool, TRUE))
1513         goto activate_failed;
1514       priv->pool_active = TRUE;
1515     }
1516     GST_DEBUG_OBJECT (trans, "using pool alloc");
1517     ret = gst_buffer_pool_acquire_buffer (priv->pool, outbuf, NULL);
1518     goto copy_meta;
1519   }
1520
1521   /* no pool, we need to figure out the size of the output buffer first */
1522   if ((bclass->transform_ip != NULL) && priv->always_in_place) {
1523     /* we want to do an in-place alloc */
1524     if (gst_buffer_is_writable (inbuf)) {
1525       GST_DEBUG_OBJECT (trans, "inplace reuse writable input buffer");
1526       *outbuf = inbuf;
1527     } else {
1528       GST_DEBUG_OBJECT (trans, "making writable buffer copy");
1529       /* we make a copy of the input buffer */
1530       *outbuf = gst_buffer_copy (inbuf);
1531     }
1532     goto done;
1533   }
1534
1535   /* else use the transform function to get the size */
1536   incaps = gst_pad_get_current_caps (trans->sinkpad);
1537   outcaps = gst_pad_get_current_caps (trans->srcpad);
1538
1539   GST_DEBUG_OBJECT (trans, "getting output size for alloc");
1540   /* copy transform, figure out the output size */
1541   insize = gst_buffer_get_size (inbuf);
1542   res = gst_base_transform_transform_size (trans,
1543       GST_PAD_SINK, incaps, insize, outcaps, &outsize);
1544
1545   gst_caps_unref (incaps);
1546   gst_caps_unref (outcaps);
1547
1548   if (!res)
1549     goto unknown_size;
1550
1551   GST_DEBUG_OBJECT (trans, "doing alloc of size %" G_GSIZE_FORMAT, outsize);
1552   *outbuf = gst_buffer_new_allocate (priv->allocator, outsize, &priv->params);
1553
1554 copy_meta:
1555   /* copy the metadata */
1556   if (bclass->copy_metadata)
1557     if (!bclass->copy_metadata (trans, inbuf, *outbuf)) {
1558       /* something failed, post a warning */
1559       GST_ELEMENT_WARNING (trans, STREAM, NOT_IMPLEMENTED,
1560           ("could not copy metadata"), (NULL));
1561     }
1562
1563 done:
1564   return ret;
1565
1566   /* ERRORS */
1567   /* ERRORS */
1568 activate_failed:
1569   {
1570     GST_ELEMENT_ERROR (trans, RESOURCE, SETTINGS,
1571         ("failed to activate bufferpool"), ("failed to activate bufferpool"));
1572     return GST_FLOW_ERROR;
1573   }
1574 unknown_size:
1575   {
1576     GST_ERROR_OBJECT (trans, "unknown output size");
1577     return GST_FLOW_ERROR;
1578   }
1579 }
1580
1581 typedef struct
1582 {
1583   GstBaseTransform *trans;
1584   GstBuffer *outbuf;
1585 } CopyMetaData;
1586
1587 static gboolean
1588 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
1589 {
1590   CopyMetaData *data = user_data;
1591   GstBaseTransform *trans = data->trans;
1592   GstBaseTransformClass *klass;
1593   const GstMetaInfo *info = (*meta)->info;
1594   GstBuffer *outbuf = data->outbuf;
1595   gboolean do_copy;
1596
1597   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1598
1599   if (GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED)) {
1600     /* never call the transform_meta with pool private metadata */
1601     GST_DEBUG_OBJECT (trans, "not copying pooled metadata %s",
1602         g_type_name (info->api));
1603     do_copy = FALSE;
1604   } else if (gst_meta_api_type_has_tag (info->api, GST_META_TAG_MEMORY)) {
1605     /* never call the transform_meta with memory specific metadata */
1606     GST_DEBUG_OBJECT (trans, "not copying memory specific metadata %s",
1607         g_type_name (info->api));
1608     do_copy = FALSE;
1609   } else if (klass->transform_meta) {
1610     do_copy = klass->transform_meta (trans, outbuf, *meta, inbuf);
1611     GST_DEBUG_OBJECT (trans, "transformed metadata %s: copy: %d",
1612         g_type_name (info->api), do_copy);
1613   } else {
1614     do_copy = FALSE;
1615     GST_DEBUG_OBJECT (trans, "not copying metadata %s",
1616         g_type_name (info->api));
1617   }
1618
1619   /* we only copy metadata when the subclass implemented a transform_meta
1620    * function and when it returns TRUE */
1621   if (do_copy) {
1622     GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
1623     GST_DEBUG_OBJECT (trans, "copy metadata %s", g_type_name (info->api));
1624     /* simply copy then */
1625     info->transform_func (outbuf, *meta, inbuf,
1626         _gst_meta_transform_copy, &copy_data);
1627   }
1628   return TRUE;
1629 }
1630
1631 static gboolean
1632 default_copy_metadata (GstBaseTransform * trans,
1633     GstBuffer * inbuf, GstBuffer * outbuf)
1634 {
1635   GstBaseTransformPrivate *priv = trans->priv;
1636   CopyMetaData data;
1637
1638   /* now copy the metadata */
1639   GST_DEBUG_OBJECT (trans, "copying metadata");
1640
1641   /* this should not happen, buffers allocated from a pool or with
1642    * new_allocate should always be writable. */
1643   if (!gst_buffer_is_writable (outbuf))
1644     goto not_writable;
1645
1646   /* when we get here, the metadata should be writable */
1647   gst_buffer_copy_into (outbuf, inbuf,
1648       GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1649
1650   /* clear the GAP flag when the subclass does not understand it */
1651   if (!priv->gap_aware)
1652     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
1653
1654
1655   data.trans = trans;
1656   data.outbuf = outbuf;
1657
1658   gst_buffer_foreach_meta (inbuf, foreach_metadata, &data);
1659
1660   return TRUE;
1661
1662   /* ERRORS */
1663 not_writable:
1664   {
1665     GST_WARNING_OBJECT (trans, "buffer %p not writable", outbuf);
1666     return FALSE;
1667   }
1668 }
1669
1670 /* Given @caps calcultate the size of one unit.
1671  *
1672  * For video caps, this is the size of one frame (and thus one buffer).
1673  * For audio caps, this is the size of one sample.
1674  *
1675  * These values are cached since they do not change and the calculation
1676  * potentially involves parsing caps and other expensive stuff.
1677  *
1678  * We have two cache locations to store the size, one for the source caps
1679  * and one for the sink caps.
1680  *
1681  * this function returns FALSE if no size could be calculated.
1682  */
1683 static gboolean
1684 gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
1685     gsize * size)
1686 {
1687   gboolean res = FALSE;
1688   GstBaseTransformClass *bclass;
1689   GstBaseTransformPrivate *priv = trans->priv;
1690
1691   /* see if we have the result cached */
1692   if (priv->cache_caps1 == caps) {
1693     *size = priv->cache_caps1_size;
1694     GST_DEBUG_OBJECT (trans,
1695         "returned %" G_GSIZE_FORMAT " from first cache", *size);
1696     return TRUE;
1697   }
1698   if (priv->cache_caps2 == caps) {
1699     *size = priv->cache_caps2_size;
1700     GST_DEBUG_OBJECT (trans,
1701         "returned %" G_GSIZE_FORMAT " from second cached", *size);
1702     return TRUE;
1703   }
1704
1705   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1706   res = bclass->get_unit_size (trans, caps, size);
1707   GST_DEBUG_OBJECT (trans,
1708       "caps %" GST_PTR_FORMAT ") has unit size %" G_GSIZE_FORMAT ", res %s",
1709       caps, *size, res ? "TRUE" : "FALSE");
1710
1711   if (res) {
1712     /* and cache the values */
1713     if (priv->cache_caps1 == NULL) {
1714       gst_caps_replace (&priv->cache_caps1, caps);
1715       priv->cache_caps1_size = *size;
1716       GST_DEBUG_OBJECT (trans,
1717           "caching %" G_GSIZE_FORMAT " in first cache", *size);
1718     } else if (priv->cache_caps2 == NULL) {
1719       gst_caps_replace (&priv->cache_caps2, caps);
1720       priv->cache_caps2_size = *size;
1721       GST_DEBUG_OBJECT (trans,
1722           "caching %" G_GSIZE_FORMAT " in second cache", *size);
1723     } else {
1724       GST_DEBUG_OBJECT (trans, "no free spot to cache unit_size");
1725     }
1726   }
1727   return res;
1728 }
1729
1730 static gboolean
1731 gst_base_transform_sink_event (GstPad * pad, GstObject * parent,
1732     GstEvent * event)
1733 {
1734   GstBaseTransform *trans;
1735   GstBaseTransformClass *bclass;
1736   gboolean ret = TRUE;
1737
1738   trans = GST_BASE_TRANSFORM (parent);
1739   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1740
1741   if (bclass->sink_event)
1742     ret = bclass->sink_event (trans, event);
1743   else
1744     gst_event_unref (event);
1745
1746   return ret;
1747 }
1748
1749 static gboolean
1750 gst_base_transform_sink_eventfunc (GstBaseTransform * trans, GstEvent * event)
1751 {
1752   gboolean ret = TRUE, forward = TRUE;
1753   GstBaseTransformPrivate *priv = trans->priv;
1754
1755   switch (GST_EVENT_TYPE (event)) {
1756     case GST_EVENT_FLUSH_START:
1757       break;
1758     case GST_EVENT_FLUSH_STOP:
1759       GST_OBJECT_LOCK (trans);
1760       /* reset QoS parameters */
1761       priv->proportion = 1.0;
1762       priv->earliest_time = -1;
1763       priv->discont = FALSE;
1764       priv->processed = 0;
1765       priv->dropped = 0;
1766       GST_OBJECT_UNLOCK (trans);
1767       /* we need new segment info after the flush. */
1768       trans->have_segment = FALSE;
1769       gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
1770       priv->position_out = GST_CLOCK_TIME_NONE;
1771       break;
1772     case GST_EVENT_EOS:
1773       break;
1774     case GST_EVENT_TAG:
1775       break;
1776     case GST_EVENT_CAPS:
1777     {
1778       GstCaps *caps;
1779
1780       gst_event_parse_caps (event, &caps);
1781       ret = gst_base_transform_setcaps (trans, trans->sinkpad, caps);
1782
1783       forward = FALSE;
1784       break;
1785     }
1786     case GST_EVENT_SEGMENT:
1787     {
1788       gst_event_copy_segment (event, &trans->segment);
1789       trans->have_segment = TRUE;
1790
1791       GST_DEBUG_OBJECT (trans, "received SEGMENT %" GST_SEGMENT_FORMAT,
1792           &trans->segment);
1793       break;
1794     }
1795     default:
1796       break;
1797   }
1798
1799   if (ret && forward)
1800     ret = gst_pad_push_event (trans->srcpad, event);
1801   else
1802     gst_event_unref (event);
1803
1804   return ret;
1805 }
1806
1807 static gboolean
1808 gst_base_transform_src_event (GstPad * pad, GstObject * parent,
1809     GstEvent * event)
1810 {
1811   GstBaseTransform *trans;
1812   GstBaseTransformClass *bclass;
1813   gboolean ret = TRUE;
1814
1815   trans = GST_BASE_TRANSFORM (parent);
1816   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1817
1818   if (bclass->src_event)
1819     ret = bclass->src_event (trans, event);
1820   else
1821     gst_event_unref (event);
1822
1823   return ret;
1824 }
1825
1826 static gboolean
1827 gst_base_transform_src_eventfunc (GstBaseTransform * trans, GstEvent * event)
1828 {
1829   gboolean ret;
1830
1831   GST_DEBUG_OBJECT (trans, "handling event %p %" GST_PTR_FORMAT, event, event);
1832
1833   switch (GST_EVENT_TYPE (event)) {
1834     case GST_EVENT_SEEK:
1835       break;
1836     case GST_EVENT_NAVIGATION:
1837       break;
1838     case GST_EVENT_QOS:
1839     {
1840       gdouble proportion;
1841       GstClockTimeDiff diff;
1842       GstClockTime timestamp;
1843
1844       gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
1845       gst_base_transform_update_qos (trans, proportion, diff, timestamp);
1846       break;
1847     }
1848     default:
1849       break;
1850   }
1851
1852   ret = gst_pad_push_event (trans->sinkpad, event);
1853
1854   return ret;
1855 }
1856
1857 /* perform a transform on @inbuf and put the result in @outbuf.
1858  *
1859  * This function is common to the push and pull-based operations.
1860  *
1861  * This function takes ownership of @inbuf */
1862 static GstFlowReturn
1863 gst_base_transform_handle_buffer (GstBaseTransform * trans, GstBuffer * inbuf,
1864     GstBuffer ** outbuf)
1865 {
1866   GstBaseTransformClass *bclass;
1867   GstBaseTransformPrivate *priv = trans->priv;
1868   GstFlowReturn ret = GST_FLOW_OK;
1869   gboolean want_in_place;
1870   GstClockTime running_time;
1871   GstClockTime timestamp;
1872   gboolean reconfigure;
1873
1874   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1875
1876   GST_OBJECT_LOCK (trans->sinkpad);
1877   reconfigure = GST_PAD_NEEDS_RECONFIGURE (trans->srcpad)
1878       || priv->reconfigure;
1879   GST_OBJECT_FLAG_UNSET (trans->srcpad, GST_PAD_FLAG_NEED_RECONFIGURE);
1880   priv->reconfigure = FALSE;
1881   GST_OBJECT_UNLOCK (trans->sinkpad);
1882
1883   if (G_UNLIKELY (reconfigure)) {
1884     GstCaps *incaps;
1885
1886     GST_DEBUG_OBJECT (trans, "we had a pending reconfigure");
1887
1888     incaps = gst_pad_get_current_caps (trans->sinkpad);
1889     if (incaps == NULL)
1890       goto no_reconfigure;
1891
1892     /* if we need to reconfigure we pretend new caps arrived. This
1893      * will reconfigure the transform with the new output format. */
1894     if (!gst_base_transform_setcaps (trans, trans->sinkpad, incaps)) {
1895       gst_caps_unref (incaps);
1896       goto not_negotiated;
1897     }
1898     gst_caps_unref (incaps);
1899   }
1900
1901 no_reconfigure:
1902   if (GST_BUFFER_OFFSET_IS_VALID (inbuf))
1903     GST_DEBUG_OBJECT (trans,
1904         "handling buffer %p of size %" G_GSIZE_FORMAT " and offset %"
1905         G_GUINT64_FORMAT, inbuf, gst_buffer_get_size (inbuf),
1906         GST_BUFFER_OFFSET (inbuf));
1907   else
1908     GST_DEBUG_OBJECT (trans,
1909         "handling buffer %p of size %" G_GSIZE_FORMAT " and offset NONE", inbuf,
1910         gst_buffer_get_size (inbuf));
1911
1912   /* Don't allow buffer handling before negotiation, except in passthrough mode
1913    * or if the class doesn't implement a set_caps function (in which case it doesn't
1914    * care about caps)
1915    */
1916   if (!priv->negotiated && !priv->passthrough && (bclass->set_caps != NULL))
1917     goto not_negotiated;
1918
1919   /* Set discont flag so we can mark the outgoing buffer */
1920   if (GST_BUFFER_IS_DISCONT (inbuf)) {
1921     GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", inbuf);
1922     priv->discont = TRUE;
1923   }
1924
1925   /* can only do QoS if the segment is in TIME */
1926   if (trans->segment.format != GST_FORMAT_TIME)
1927     goto no_qos;
1928
1929   /* QOS is done on the running time of the buffer, get it now */
1930   timestamp = GST_BUFFER_TIMESTAMP (inbuf);
1931   running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
1932       timestamp);
1933
1934   if (running_time != -1) {
1935     gboolean need_skip;
1936     GstClockTime earliest_time;
1937     gdouble proportion;
1938
1939     /* lock for getting the QoS parameters that are set (in a different thread)
1940      * with the QOS events */
1941     GST_OBJECT_LOCK (trans);
1942     earliest_time = priv->earliest_time;
1943     proportion = priv->proportion;
1944     /* check for QoS, don't perform conversion for buffers
1945      * that are known to be late. */
1946     need_skip = priv->qos_enabled &&
1947         earliest_time != -1 && running_time <= earliest_time;
1948     GST_OBJECT_UNLOCK (trans);
1949
1950     if (need_skip) {
1951       GstMessage *qos_msg;
1952       GstClockTime duration;
1953       guint64 stream_time;
1954       gint64 jitter;
1955
1956       GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "skipping transform: qostime %"
1957           GST_TIME_FORMAT " <= %" GST_TIME_FORMAT,
1958           GST_TIME_ARGS (running_time), GST_TIME_ARGS (earliest_time));
1959
1960       priv->dropped++;
1961
1962       duration = GST_BUFFER_DURATION (inbuf);
1963       stream_time =
1964           gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1965           timestamp);
1966       jitter = GST_CLOCK_DIFF (running_time, earliest_time);
1967
1968       qos_msg =
1969           gst_message_new_qos (GST_OBJECT_CAST (trans), FALSE, running_time,
1970           stream_time, timestamp, duration);
1971       gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
1972       gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
1973           priv->processed, priv->dropped);
1974       gst_element_post_message (GST_ELEMENT_CAST (trans), qos_msg);
1975
1976       /* mark discont for next buffer */
1977       priv->discont = TRUE;
1978       goto skip;
1979     }
1980   }
1981
1982 no_qos:
1983
1984   /* first try to allocate an output buffer based on the currently negotiated
1985    * format. outbuf will contain a buffer suitable for doing the configured
1986    * transform after this function. */
1987   if (bclass->prepare_output_buffer == NULL)
1988     goto no_prepare;
1989
1990   GST_DEBUG_OBJECT (trans, "calling prepare buffer");
1991   ret = bclass->prepare_output_buffer (trans, inbuf, outbuf);
1992
1993   if (ret != GST_FLOW_OK || *outbuf == NULL)
1994     goto no_buffer;
1995
1996   GST_DEBUG_OBJECT (trans, "using allocated buffer in %p, out %p", inbuf,
1997       *outbuf);
1998
1999   /* now perform the needed transform */
2000   if (priv->passthrough) {
2001     /* In passthrough mode, give transform_ip a look at the
2002      * buffer, without making it writable, or just push the
2003      * data through */
2004     if (bclass->transform_ip) {
2005       GST_DEBUG_OBJECT (trans, "doing passthrough transform");
2006       ret = bclass->transform_ip (trans, *outbuf);
2007     } else {
2008       GST_DEBUG_OBJECT (trans, "element is in passthrough");
2009     }
2010   } else {
2011     want_in_place = (bclass->transform_ip != NULL) && priv->always_in_place;
2012
2013     if (want_in_place) {
2014       GST_DEBUG_OBJECT (trans, "doing inplace transform");
2015       ret = bclass->transform_ip (trans, *outbuf);
2016     } else {
2017       GST_DEBUG_OBJECT (trans, "doing non-inplace transform");
2018
2019       if (bclass->transform)
2020         ret = bclass->transform (trans, inbuf, *outbuf);
2021       else
2022         ret = GST_FLOW_NOT_SUPPORTED;
2023     }
2024   }
2025
2026 skip:
2027   /* only unref input buffer if we allocated a new outbuf buffer. If we reused
2028    * the input buffer, no refcount is changed to keep the input buffer writable
2029    * when needed. */
2030   if (*outbuf != inbuf)
2031     gst_buffer_unref (inbuf);
2032
2033   return ret;
2034
2035   /* ERRORS */
2036 not_negotiated:
2037   {
2038     gst_buffer_unref (inbuf);
2039     *outbuf = NULL;
2040     GST_ELEMENT_ERROR (trans, STREAM, FORMAT,
2041         ("not negotiated"), ("not negotiated"));
2042     return GST_FLOW_NOT_NEGOTIATED;
2043   }
2044 no_prepare:
2045   {
2046     gst_buffer_unref (inbuf);
2047     GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
2048         ("Sub-class has no prepare_output_buffer implementation"), (NULL));
2049     return GST_FLOW_NOT_SUPPORTED;
2050   }
2051 no_buffer:
2052   {
2053     gst_buffer_unref (inbuf);
2054     *outbuf = NULL;
2055     GST_WARNING_OBJECT (trans, "could not get buffer from pool: %s",
2056         gst_flow_get_name (ret));
2057     return ret;
2058   }
2059 }
2060
2061 /* FIXME, getrange is broken, need to pull range from the other
2062  * end based on the transform_size result.
2063  */
2064 static GstFlowReturn
2065 gst_base_transform_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2066     guint length, GstBuffer ** buffer)
2067 {
2068   GstBaseTransform *trans;
2069   GstBaseTransformClass *klass;
2070   GstFlowReturn ret;
2071   GstBuffer *inbuf = NULL;
2072
2073   trans = GST_BASE_TRANSFORM (parent);
2074
2075   ret = gst_pad_pull_range (trans->sinkpad, offset, length, &inbuf);
2076   if (G_UNLIKELY (ret != GST_FLOW_OK))
2077     goto pull_error;
2078
2079   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2080   if (klass->before_transform)
2081     klass->before_transform (trans, inbuf);
2082
2083   ret = gst_base_transform_handle_buffer (trans, inbuf, buffer);
2084
2085 done:
2086   return ret;
2087
2088   /* ERRORS */
2089 pull_error:
2090   {
2091     GST_DEBUG_OBJECT (trans, "failed to pull a buffer: %s",
2092         gst_flow_get_name (ret));
2093     goto done;
2094   }
2095 }
2096
2097 static GstFlowReturn
2098 gst_base_transform_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2099 {
2100   GstBaseTransform *trans;
2101   GstBaseTransformClass *klass;
2102   GstBaseTransformPrivate *priv;
2103   GstFlowReturn ret;
2104   GstClockTime position = GST_CLOCK_TIME_NONE;
2105   GstClockTime timestamp, duration;
2106   GstBuffer *outbuf = NULL;
2107
2108   trans = GST_BASE_TRANSFORM (parent);
2109   priv = trans->priv;
2110
2111   timestamp = GST_BUFFER_TIMESTAMP (buffer);
2112   duration = GST_BUFFER_DURATION (buffer);
2113
2114   /* calculate end position of the incoming buffer */
2115   if (timestamp != GST_CLOCK_TIME_NONE) {
2116     if (duration != GST_CLOCK_TIME_NONE)
2117       position = timestamp + duration;
2118     else
2119       position = timestamp;
2120   }
2121
2122   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2123   if (klass->before_transform)
2124     klass->before_transform (trans, buffer);
2125
2126   /* protect transform method and concurrent buffer alloc */
2127   ret = gst_base_transform_handle_buffer (trans, buffer, &outbuf);
2128
2129   /* outbuf can be NULL, this means a dropped buffer, if we have a buffer but
2130    * GST_BASE_TRANSFORM_FLOW_DROPPED we will not push either. */
2131   if (outbuf != NULL) {
2132     if (ret == GST_FLOW_OK) {
2133       GstClockTime position_out = GST_CLOCK_TIME_NONE;
2134
2135       /* Remember last stop position */
2136       if (position != GST_CLOCK_TIME_NONE &&
2137           trans->segment.format == GST_FORMAT_TIME)
2138         trans->segment.position = position;
2139
2140       if (GST_BUFFER_TIMESTAMP_IS_VALID (outbuf)) {
2141         position_out = GST_BUFFER_TIMESTAMP (outbuf);
2142         if (GST_BUFFER_DURATION_IS_VALID (outbuf))
2143           position_out += GST_BUFFER_DURATION (outbuf);
2144       } else if (position != GST_CLOCK_TIME_NONE) {
2145         position_out = position;
2146       }
2147       if (position_out != GST_CLOCK_TIME_NONE
2148           && trans->segment.format == GST_FORMAT_TIME)
2149         priv->position_out = position_out;
2150
2151       /* apply DISCONT flag if the buffer is not yet marked as such */
2152       if (trans->priv->discont) {
2153         GST_DEBUG_OBJECT (trans, "we have a pending DISCONT");
2154         if (!GST_BUFFER_IS_DISCONT (outbuf)) {
2155           GST_DEBUG_OBJECT (trans, "marking DISCONT on output buffer");
2156           outbuf = gst_buffer_make_writable (outbuf);
2157           GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2158         }
2159         priv->discont = FALSE;
2160       }
2161       priv->processed++;
2162
2163       ret = gst_pad_push (trans->srcpad, outbuf);
2164     } else {
2165       GST_DEBUG_OBJECT (trans, "we got return %s", gst_flow_get_name (ret));
2166       gst_buffer_unref (outbuf);
2167     }
2168   }
2169
2170   /* convert internal flow to OK and mark discont for the next buffer. */
2171   if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED) {
2172     GST_DEBUG_OBJECT (trans, "dropped a buffer, marking DISCONT");
2173     priv->discont = TRUE;
2174     ret = GST_FLOW_OK;
2175   }
2176
2177   return ret;
2178 }
2179
2180 static void
2181 gst_base_transform_set_property (GObject * object, guint prop_id,
2182     const GValue * value, GParamSpec * pspec)
2183 {
2184   GstBaseTransform *trans;
2185
2186   trans = GST_BASE_TRANSFORM (object);
2187
2188   switch (prop_id) {
2189     case PROP_QOS:
2190       gst_base_transform_set_qos_enabled (trans, g_value_get_boolean (value));
2191       break;
2192     default:
2193       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2194       break;
2195   }
2196 }
2197
2198 static void
2199 gst_base_transform_get_property (GObject * object, guint prop_id,
2200     GValue * value, GParamSpec * pspec)
2201 {
2202   GstBaseTransform *trans;
2203
2204   trans = GST_BASE_TRANSFORM (object);
2205
2206   switch (prop_id) {
2207     case PROP_QOS:
2208       g_value_set_boolean (value, gst_base_transform_is_qos_enabled (trans));
2209       break;
2210     default:
2211       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2212       break;
2213   }
2214 }
2215
2216 /* not a vmethod of anything, just an internal method */
2217 static gboolean
2218 gst_base_transform_activate (GstBaseTransform * trans, gboolean active)
2219 {
2220   GstBaseTransformClass *bclass;
2221   GstBaseTransformPrivate *priv = trans->priv;
2222   gboolean result = TRUE;
2223
2224   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2225
2226   if (active) {
2227     GstCaps *incaps, *outcaps;
2228
2229     if (priv->pad_mode == GST_PAD_MODE_NONE && bclass->start)
2230       result &= bclass->start (trans);
2231
2232     incaps = gst_pad_get_current_caps (trans->sinkpad);
2233     outcaps = gst_pad_get_current_caps (trans->srcpad);
2234
2235     GST_OBJECT_LOCK (trans);
2236     if (incaps && outcaps)
2237       priv->have_same_caps =
2238           gst_caps_is_equal (incaps, outcaps) || priv->passthrough;
2239     else
2240       priv->have_same_caps = priv->passthrough;
2241     GST_DEBUG_OBJECT (trans, "have_same_caps %d", priv->have_same_caps);
2242     priv->negotiated = FALSE;
2243     trans->have_segment = FALSE;
2244     gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
2245     priv->position_out = GST_CLOCK_TIME_NONE;
2246     priv->proportion = 1.0;
2247     priv->earliest_time = -1;
2248     priv->discont = FALSE;
2249     priv->processed = 0;
2250     priv->dropped = 0;
2251     GST_OBJECT_UNLOCK (trans);
2252
2253     if (incaps)
2254       gst_caps_unref (incaps);
2255     if (outcaps)
2256       gst_caps_unref (outcaps);
2257   } else {
2258     /* We must make sure streaming has finished before resetting things
2259      * and calling the ::stop vfunc */
2260     GST_PAD_STREAM_LOCK (trans->sinkpad);
2261     GST_PAD_STREAM_UNLOCK (trans->sinkpad);
2262
2263     priv->have_same_caps = FALSE;
2264     /* We can only reset the passthrough mode if the instance told us to 
2265        handle it in configure_caps */
2266     if (bclass->passthrough_on_same_caps) {
2267       gst_base_transform_set_passthrough (trans, FALSE);
2268     }
2269     gst_caps_replace (&priv->cache_caps1, NULL);
2270     gst_caps_replace (&priv->cache_caps2, NULL);
2271
2272     if (priv->pad_mode != GST_PAD_MODE_NONE && bclass->stop)
2273       result &= bclass->stop (trans);
2274
2275     gst_base_transform_set_allocation (trans, NULL, NULL, NULL, NULL);
2276   }
2277
2278   return result;
2279 }
2280
2281 static gboolean
2282 gst_base_transform_sink_activate_mode (GstPad * pad, GstObject * parent,
2283     GstPadMode mode, gboolean active)
2284 {
2285   gboolean result = FALSE;
2286   GstBaseTransform *trans;
2287
2288   trans = GST_BASE_TRANSFORM (parent);
2289
2290   switch (mode) {
2291     case GST_PAD_MODE_PUSH:
2292     {
2293       result = gst_base_transform_activate (trans, active);
2294
2295       if (result)
2296         trans->priv->pad_mode = active ? GST_PAD_MODE_PUSH : GST_PAD_MODE_NONE;
2297
2298       break;
2299     }
2300     default:
2301       result = TRUE;
2302       break;
2303   }
2304   return result;
2305 }
2306
2307 static gboolean
2308 gst_base_transform_src_activate_mode (GstPad * pad, GstObject * parent,
2309     GstPadMode mode, gboolean active)
2310 {
2311   gboolean result = FALSE;
2312   GstBaseTransform *trans;
2313
2314   trans = GST_BASE_TRANSFORM (parent);
2315
2316   switch (mode) {
2317     case GST_PAD_MODE_PULL:
2318     {
2319       result =
2320           gst_pad_activate_mode (trans->sinkpad, GST_PAD_MODE_PULL, active);
2321
2322       if (result)
2323         result &= gst_base_transform_activate (trans, active);
2324
2325       if (result)
2326         trans->priv->pad_mode = active ? mode : GST_PAD_MODE_NONE;
2327       break;
2328     }
2329     default:
2330       result = TRUE;
2331       break;
2332   }
2333
2334   return result;
2335 }
2336
2337 /**
2338  * gst_base_transform_set_passthrough:
2339  * @trans: the #GstBaseTransform to set
2340  * @passthrough: boolean indicating passthrough mode.
2341  *
2342  * Set passthrough mode for this filter by default. This is mostly
2343  * useful for filters that do not care about negotiation.
2344  *
2345  * Always TRUE for filters which don't implement either a transform
2346  * or transform_ip method.
2347  *
2348  * MT safe.
2349  */
2350 void
2351 gst_base_transform_set_passthrough (GstBaseTransform * trans,
2352     gboolean passthrough)
2353 {
2354   GstBaseTransformClass *bclass;
2355
2356   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2357
2358   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2359
2360   GST_OBJECT_LOCK (trans);
2361   if (passthrough == FALSE) {
2362     if (bclass->transform_ip || bclass->transform)
2363       trans->priv->passthrough = FALSE;
2364   } else {
2365     trans->priv->passthrough = TRUE;
2366   }
2367
2368   GST_DEBUG_OBJECT (trans, "set passthrough %d", trans->priv->passthrough);
2369   GST_OBJECT_UNLOCK (trans);
2370 }
2371
2372 /**
2373  * gst_base_transform_is_passthrough:
2374  * @trans: the #GstBaseTransform to query
2375  *
2376  * See if @trans is configured as a passthrough transform.
2377  *
2378  * Returns: TRUE is the transform is configured in passthrough mode.
2379  *
2380  * MT safe.
2381  */
2382 gboolean
2383 gst_base_transform_is_passthrough (GstBaseTransform * trans)
2384 {
2385   gboolean result;
2386
2387   g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2388
2389   GST_OBJECT_LOCK (trans);
2390   result = trans->priv->passthrough;
2391   GST_OBJECT_UNLOCK (trans);
2392
2393   return result;
2394 }
2395
2396 /**
2397  * gst_base_transform_set_in_place:
2398  * @trans: the #GstBaseTransform to modify
2399  * @in_place: Boolean value indicating that we would like to operate
2400  * on in_place buffers.
2401  *
2402  * Determines whether a non-writable buffer will be copied before passing
2403  * to the transform_ip function.
2404  * <itemizedlist>
2405  *   <listitem>Always TRUE if no transform function is implemented.</listitem>
2406  *   <listitem>Always FALSE if ONLY transform function is implemented.</listitem>
2407  * </itemizedlist>
2408  *
2409  * MT safe.
2410  */
2411 void
2412 gst_base_transform_set_in_place (GstBaseTransform * trans, gboolean in_place)
2413 {
2414   GstBaseTransformClass *bclass;
2415
2416   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2417
2418   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2419
2420   GST_OBJECT_LOCK (trans);
2421
2422   if (in_place) {
2423     if (bclass->transform_ip) {
2424       GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
2425       trans->priv->always_in_place = TRUE;
2426     }
2427   } else {
2428     if (bclass->transform) {
2429       GST_DEBUG_OBJECT (trans, "setting in_place FALSE");
2430       trans->priv->always_in_place = FALSE;
2431     }
2432   }
2433
2434   GST_OBJECT_UNLOCK (trans);
2435 }
2436
2437 /**
2438  * gst_base_transform_is_in_place:
2439  * @trans: the #GstBaseTransform to query
2440  *
2441  * See if @trans is configured as a in_place transform.
2442  *
2443  * Returns: TRUE is the transform is configured in in_place mode.
2444  *
2445  * MT safe.
2446  */
2447 gboolean
2448 gst_base_transform_is_in_place (GstBaseTransform * trans)
2449 {
2450   gboolean result;
2451
2452   g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2453
2454   GST_OBJECT_LOCK (trans);
2455   result = trans->priv->always_in_place;
2456   GST_OBJECT_UNLOCK (trans);
2457
2458   return result;
2459 }
2460
2461 /**
2462  * gst_base_transform_update_qos:
2463  * @trans: a #GstBaseTransform
2464  * @proportion: the proportion
2465  * @diff: the diff against the clock
2466  * @timestamp: the timestamp of the buffer generating the QoS expressed in
2467  * running_time.
2468  *
2469  * Set the QoS parameters in the transform. This function is called internally
2470  * when a QOS event is received but subclasses can provide custom information
2471  * when needed.
2472  *
2473  * MT safe.
2474  *
2475  * Since: 0.10.5
2476  */
2477 void
2478 gst_base_transform_update_qos (GstBaseTransform * trans,
2479     gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2480 {
2481   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2482
2483   GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans,
2484       "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2485       GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2486
2487   GST_OBJECT_LOCK (trans);
2488   trans->priv->proportion = proportion;
2489   trans->priv->earliest_time = timestamp + diff;
2490   GST_OBJECT_UNLOCK (trans);
2491 }
2492
2493 /**
2494  * gst_base_transform_set_qos_enabled:
2495  * @trans: a #GstBaseTransform
2496  * @enabled: new state
2497  *
2498  * Enable or disable QoS handling in the transform.
2499  *
2500  * MT safe.
2501  *
2502  * Since: 0.10.5
2503  */
2504 void
2505 gst_base_transform_set_qos_enabled (GstBaseTransform * trans, gboolean enabled)
2506 {
2507   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2508
2509   GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "enabled: %d", enabled);
2510
2511   GST_OBJECT_LOCK (trans);
2512   trans->priv->qos_enabled = enabled;
2513   GST_OBJECT_UNLOCK (trans);
2514 }
2515
2516 /**
2517  * gst_base_transform_is_qos_enabled:
2518  * @trans: a #GstBaseTransform
2519  *
2520  * Queries if the transform will handle QoS.
2521  *
2522  * Returns: TRUE if QoS is enabled.
2523  *
2524  * MT safe.
2525  *
2526  * Since: 0.10.5
2527  */
2528 gboolean
2529 gst_base_transform_is_qos_enabled (GstBaseTransform * trans)
2530 {
2531   gboolean result;
2532
2533   g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2534
2535   GST_OBJECT_LOCK (trans);
2536   result = trans->priv->qos_enabled;
2537   GST_OBJECT_UNLOCK (trans);
2538
2539   return result;
2540 }
2541
2542 /**
2543  * gst_base_transform_set_gap_aware:
2544  * @trans: a #GstBaseTransform
2545  * @gap_aware: New state
2546  *
2547  * If @gap_aware is %FALSE (the default), output buffers will have the
2548  * %GST_BUFFER_FLAG_GAP flag unset.
2549  *
2550  * If set to %TRUE, the element must handle output buffers with this flag set
2551  * correctly, i.e. it can assume that the buffer contains neutral data but must
2552  * unset the flag if the output is no neutral data.
2553  *
2554  * MT safe.
2555  *
2556  * Since: 0.10.16
2557  */
2558 void
2559 gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware)
2560 {
2561   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2562
2563   GST_OBJECT_LOCK (trans);
2564   trans->priv->gap_aware = gap_aware;
2565   GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware);
2566   GST_OBJECT_UNLOCK (trans);
2567 }
2568
2569 /**
2570  * gst_base_transform_reconfigure_sink:
2571  * @trans: a #GstBaseTransform
2572  *
2573  * Instructs @trans to request renegotiation upstream. This function is
2574  * typically called after properties on the transform were set that
2575  * influence the input format.
2576  */
2577 void
2578 gst_base_transform_reconfigure_sink (GstBaseTransform * trans)
2579 {
2580   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2581
2582   /* push the renegotiate event */
2583   if (!gst_pad_push_event (GST_BASE_TRANSFORM_SINK_PAD (trans),
2584           gst_event_new_reconfigure ()))
2585     GST_DEBUG_OBJECT (trans, "Renegotiate event wasn't handled");
2586 }
2587
2588 /**
2589  * gst_base_transform_reconfigure_src:
2590  * @trans: a #GstBaseTransform
2591  *
2592  * Instructs @trans to renegotiate a new downstream transform on the next
2593  * buffer. This function is typically called after properties on the transform
2594  * were set that influence the output format.
2595  *
2596  * Since: 0.10.21
2597  */
2598 void
2599 gst_base_transform_reconfigure_src (GstBaseTransform * trans)
2600 {
2601   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2602
2603   GST_OBJECT_LOCK (trans);
2604   GST_DEBUG_OBJECT (trans, "marking reconfigure");
2605   trans->priv->reconfigure = TRUE;
2606   GST_OBJECT_UNLOCK (trans);
2607 }