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