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