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