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