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