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