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