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