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