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