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