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