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