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