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