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