basetransform/basesrc: Do bufferpool configuration inside the default decide_allocati...
[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   GstCaps *outcaps;
806   GstBufferPool *pool;
807   guint size, min, max;
808   GstAllocator *allocator;
809   GstAllocationParams params;
810   GstStructure *config;
811   gboolean update_allocator;
812
813   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
814
815   n_metas = gst_query_get_n_allocation_metas (query);
816   for (i = 0; i < n_metas; i++) {
817     GType api;
818     gboolean remove;
819
820     api = gst_query_parse_nth_allocation_meta (query, i);
821
822     /* by default we remove all metadata, subclasses should implement a
823      * filter_meta function */
824     if (gst_meta_api_type_has_tag (api, GST_META_TAG_MEMORY)) {
825       /* remove all memory dependent metadata because we are going to have to
826        * allocate different memory for input and output. */
827       GST_LOG_OBJECT (trans, "removing memory specific metadata %s",
828           g_type_name (api));
829       remove = TRUE;
830     } else if (G_LIKELY (klass->filter_meta)) {
831       /* remove if the subclass said so */
832       remove = !klass->filter_meta (trans, query, api);
833       GST_LOG_OBJECT (trans, "filter_meta for api %s returned: %s",
834           g_type_name (api), (remove ? "remove" : "keep"));
835     } else {
836       GST_LOG_OBJECT (trans, "removing metadata %s", g_type_name (api));
837       remove = TRUE;
838     }
839
840     if (remove) {
841       gst_query_remove_nth_allocation_meta (query, i);
842       i--;
843       n_metas--;
844     }
845   }
846
847   gst_query_parse_allocation (query, &outcaps, NULL);
848
849   /* we got configuration from our peer or the decide_allocation method,
850    * parse them */
851   if (gst_query_get_n_allocation_params (query) > 0) {
852     /* try the allocator */
853     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
854     update_allocator = TRUE;
855   } else {
856     allocator = NULL;
857     gst_allocation_params_init (&params);
858     update_allocator = FALSE;
859   }
860
861   if (gst_query_get_n_allocation_pools (query) > 0) {
862     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
863
864     if (pool == NULL) {
865       /* no pool, we can make our own */
866       GST_DEBUG_OBJECT (trans, "no pool, making new pool");
867       pool = gst_buffer_pool_new ();
868     }
869   } else {
870     pool = NULL;
871     size = min = max = 0;
872   }
873
874   /* now configure */
875   if (pool) {
876     config = gst_buffer_pool_get_config (pool);
877     gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
878     gst_buffer_pool_config_set_allocator (config, allocator, &params);
879     gst_buffer_pool_set_config (pool, config);
880   }
881
882   if (update_allocator)
883     gst_query_set_nth_allocation_param (query, 0, allocator, &params);
884   else
885     gst_query_add_allocation_param (query, allocator, &params);
886   if (allocator)
887     gst_allocator_unref (allocator);
888
889   if (pool) {
890     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
891     gst_object_unref (pool);
892   }
893
894   return TRUE;
895 }
896
897 static gboolean
898 gst_base_transform_do_bufferpool (GstBaseTransform * trans, GstCaps * outcaps)
899 {
900   GstQuery *query;
901   gboolean result = TRUE;
902   GstBufferPool *pool = NULL;
903   GstBaseTransformClass *klass;
904   GstBaseTransformPrivate *priv = trans->priv;
905   GstAllocator *allocator;
906   GstAllocationParams params;
907
908   /* there are these possibilities:
909    *
910    * 1) we negotiated passthrough, we can proxy the bufferpool directly and we
911    *    will do that whenever some upstream does an allocation query.
912    * 2) we need to do a transform, we need to get a bufferpool from downstream
913    *    and configure it. When upstream does the ALLOCATION query, the
914    *    propose_allocation vmethod will be called and we will configure the
915    *    upstream allocator with our proposed values then.
916    */
917   if (priv->passthrough || priv->always_in_place) {
918     /* we are in passthrough, the input buffer is never copied and always passed
919      * along. We never allocate an output buffer on the srcpad. What we do is
920      * let the upstream element decide if it wants to use a bufferpool and
921      * then we will proxy the downstream pool */
922     GST_DEBUG_OBJECT (trans, "we're passthough, delay bufferpool");
923     gst_base_transform_set_allocation (trans, NULL, NULL, NULL, NULL);
924     return TRUE;
925   }
926
927   /* not passthrough, we need to allocate */
928   /* find a pool for the negotiated caps now */
929   GST_DEBUG_OBJECT (trans, "doing allocation query");
930   query = gst_query_new_allocation (outcaps, TRUE);
931   if (!gst_pad_peer_query (trans->srcpad, query)) {
932     /* not a problem, just debug a little */
933     GST_DEBUG_OBJECT (trans, "peer ALLOCATION query failed");
934   }
935
936   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
937
938   GST_DEBUG_OBJECT (trans, "calling decide_allocation");
939   g_assert (klass->decide_allocation != NULL);
940   result = klass->decide_allocation (trans, query);
941
942   GST_DEBUG_OBJECT (trans, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
943       query);
944
945   if (!result)
946     goto no_decide_allocation;
947
948   /* we got configuration from our peer or the decide_allocation method,
949    * parse them */
950   if (gst_query_get_n_allocation_params (query) > 0) {
951     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
952   } else {
953     allocator = NULL;
954     gst_allocation_params_init (&params);
955   }
956
957   if (gst_query_get_n_allocation_pools (query) > 0)
958     gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
959
960   /* now store */
961   result =
962       gst_base_transform_set_allocation (trans, pool, allocator, &params,
963       query);
964
965   return result;
966
967   /* Errors */
968 no_decide_allocation:
969   {
970     GST_WARNING_OBJECT (trans, "Subclass failed to decide allocation");
971     gst_query_unref (query);
972
973     return result;
974   }
975 }
976
977 /* function triggered when the in and out caps are negotiated and need
978  * to be configured in the subclass. */
979 static gboolean
980 gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
981     GstCaps * out)
982 {
983   gboolean ret = TRUE;
984   GstBaseTransformClass *klass;
985   GstBaseTransformPrivate *priv = trans->priv;
986
987   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
988
989   GST_DEBUG_OBJECT (trans, "in caps:  %" GST_PTR_FORMAT, in);
990   GST_DEBUG_OBJECT (trans, "out caps: %" GST_PTR_FORMAT, out);
991
992   /* clear the cache */
993   gst_caps_replace (&priv->cache_caps1, NULL);
994   gst_caps_replace (&priv->cache_caps2, NULL);
995
996   /* figure out same caps state */
997   priv->have_same_caps = gst_caps_is_equal (in, out);
998   GST_DEBUG_OBJECT (trans, "have_same_caps: %d", priv->have_same_caps);
999
1000   /* Set the passthrough if the class wants passthrough_on_same_caps
1001    * and we have the same caps on each pad */
1002   if (klass->passthrough_on_same_caps)
1003     gst_base_transform_set_passthrough (trans, priv->have_same_caps);
1004
1005   /* now configure the element with the caps */
1006   if (klass->set_caps) {
1007     GST_DEBUG_OBJECT (trans, "Calling set_caps method to setup caps");
1008     ret = klass->set_caps (trans, in, out);
1009   }
1010
1011   return ret;
1012 }
1013
1014 static GstCaps *
1015 gst_base_transform_default_fixate_caps (GstBaseTransform * trans,
1016     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
1017 {
1018   othercaps = gst_caps_fixate (othercaps);
1019   GST_DEBUG_OBJECT (trans, "fixated to %" GST_PTR_FORMAT, othercaps);
1020
1021   return othercaps;
1022 }
1023
1024 /* given a fixed @caps on @pad, create the best possible caps for the
1025  * other pad.
1026  * @caps must be fixed when calling this function.
1027  *
1028  * This function calls the transform caps vmethod of the basetransform to figure
1029  * out the possible target formats. It then tries to select the best format from
1030  * this list by:
1031  *
1032  * - attempt passthrough if the target caps is a superset of the input caps
1033  * - fixating by using peer caps
1034  * - fixating with transform fixate function
1035  * - fixating with pad fixate functions.
1036  *
1037  * this function returns a caps that can be transformed into and is accepted by
1038  * the peer element.
1039  */
1040 static GstCaps *
1041 gst_base_transform_find_transform (GstBaseTransform * trans, GstPad * pad,
1042     GstCaps * caps)
1043 {
1044   GstBaseTransformClass *klass;
1045   GstPad *otherpad, *otherpeer;
1046   GstCaps *othercaps;
1047   gboolean is_fixed;
1048
1049   /* caps must be fixed here, this is a programming error if it's not */
1050   g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
1051
1052   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1053
1054   otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1055   otherpeer = gst_pad_get_peer (otherpad);
1056
1057   /* see how we can transform the input caps. We need to do this even for
1058    * passthrough because it might be possible that this element cannot support
1059    * passthrough at all. */
1060   othercaps = gst_base_transform_transform_caps (trans,
1061       GST_PAD_DIRECTION (pad), caps, NULL);
1062
1063   /* The caps we can actually output is the intersection of the transformed
1064    * caps with the pad template for the pad */
1065   if (othercaps) {
1066     GstCaps *intersect, *templ_caps;
1067
1068     templ_caps = gst_pad_get_pad_template_caps (otherpad);
1069     GST_DEBUG_OBJECT (trans,
1070         "intersecting against padtemplate %" GST_PTR_FORMAT, templ_caps);
1071
1072     intersect =
1073         gst_caps_intersect_full (othercaps, templ_caps,
1074         GST_CAPS_INTERSECT_FIRST);
1075
1076     gst_caps_unref (othercaps);
1077     gst_caps_unref (templ_caps);
1078     othercaps = intersect;
1079   }
1080
1081   /* check if transform is empty */
1082   if (!othercaps || gst_caps_is_empty (othercaps))
1083     goto no_transform;
1084
1085   /* if the othercaps are not fixed, we need to fixate them, first attempt
1086    * is by attempting passthrough if the othercaps are a superset of caps. */
1087   /* FIXME. maybe the caps is not fixed because it has multiple structures of
1088    * fixed caps */
1089   is_fixed = gst_caps_is_fixed (othercaps);
1090   if (!is_fixed) {
1091     GST_DEBUG_OBJECT (trans,
1092         "transform returned non fixed  %" GST_PTR_FORMAT, othercaps);
1093
1094     /* Now let's see what the peer suggests based on our transformed caps */
1095     if (otherpeer) {
1096       GstCaps *peercaps, *intersection, *templ_caps;
1097
1098       GST_DEBUG_OBJECT (trans,
1099           "Checking peer caps with filter %" GST_PTR_FORMAT, othercaps);
1100
1101       peercaps = gst_pad_query_caps (otherpeer, othercaps);
1102       GST_DEBUG_OBJECT (trans, "Resulted in %" GST_PTR_FORMAT, peercaps);
1103       if (!gst_caps_is_empty (peercaps)) {
1104         templ_caps = gst_pad_get_pad_template_caps (otherpad);
1105
1106         GST_DEBUG_OBJECT (trans,
1107             "Intersecting with template caps %" GST_PTR_FORMAT, templ_caps);
1108
1109         intersection =
1110             gst_caps_intersect_full (peercaps, templ_caps,
1111             GST_CAPS_INTERSECT_FIRST);
1112         GST_DEBUG_OBJECT (trans, "Intersection: %" GST_PTR_FORMAT,
1113             intersection);
1114         gst_caps_unref (peercaps);
1115         gst_caps_unref (templ_caps);
1116         peercaps = intersection;
1117
1118         GST_DEBUG_OBJECT (trans,
1119             "Intersecting with transformed caps %" GST_PTR_FORMAT, othercaps);
1120         intersection =
1121             gst_caps_intersect_full (peercaps, othercaps,
1122             GST_CAPS_INTERSECT_FIRST);
1123         GST_DEBUG_OBJECT (trans, "Intersection: %" GST_PTR_FORMAT,
1124             intersection);
1125         gst_caps_unref (peercaps);
1126         gst_caps_unref (othercaps);
1127         othercaps = intersection;
1128       } else {
1129         gst_caps_unref (othercaps);
1130         othercaps = peercaps;
1131       }
1132
1133       is_fixed = gst_caps_is_fixed (othercaps);
1134     } else {
1135       GST_DEBUG_OBJECT (trans, "no peer, doing passthrough");
1136       gst_caps_unref (othercaps);
1137       othercaps = gst_caps_ref (caps);
1138       is_fixed = TRUE;
1139     }
1140   }
1141   if (gst_caps_is_empty (othercaps))
1142     goto no_transform_possible;
1143
1144   GST_DEBUG ("have %sfixed caps %" GST_PTR_FORMAT, (is_fixed ? "" : "non-"),
1145       othercaps);
1146
1147   /* second attempt at fixation, call the fixate vmethod */
1148   /* caps could be fixed but the subclass may want to add fields */
1149   if (klass->fixate_caps) {
1150     GST_DEBUG_OBJECT (trans, "calling fixate_caps for %" GST_PTR_FORMAT
1151         " using caps %" GST_PTR_FORMAT " on pad %s:%s", othercaps, caps,
1152         GST_DEBUG_PAD_NAME (otherpad));
1153     /* note that we pass the complete array of structures to the fixate
1154      * function, it needs to truncate itself */
1155     othercaps =
1156         klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
1157     is_fixed = gst_caps_is_fixed (othercaps);
1158     GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
1159   }
1160
1161   /* caps should be fixed now, if not we have to fail. */
1162   if (!is_fixed)
1163     goto could_not_fixate;
1164
1165   /* and peer should accept */
1166   if (otherpeer && !gst_pad_query_accept_caps (otherpeer, othercaps))
1167     goto peer_no_accept;
1168
1169   GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT
1170       ", and got final caps %" GST_PTR_FORMAT, caps, othercaps);
1171
1172   if (otherpeer)
1173     gst_object_unref (otherpeer);
1174
1175   return othercaps;
1176
1177   /* ERRORS */
1178 no_transform:
1179   {
1180     GST_DEBUG_OBJECT (trans,
1181         "transform returned useless  %" GST_PTR_FORMAT, othercaps);
1182     goto error_cleanup;
1183   }
1184 no_transform_possible:
1185   {
1186     GST_DEBUG_OBJECT (trans,
1187         "transform could not transform %" GST_PTR_FORMAT
1188         " in anything we support", caps);
1189     goto error_cleanup;
1190   }
1191 could_not_fixate:
1192   {
1193     GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
1194     goto error_cleanup;
1195   }
1196 peer_no_accept:
1197   {
1198     GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
1199         " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
1200     goto error_cleanup;
1201   }
1202 error_cleanup:
1203   {
1204     if (otherpeer)
1205       gst_object_unref (otherpeer);
1206     if (othercaps)
1207       gst_caps_unref (othercaps);
1208     return NULL;
1209   }
1210 }
1211
1212 static gboolean
1213 gst_base_transform_acceptcaps_default (GstBaseTransform * trans,
1214     GstPadDirection direction, GstCaps * caps)
1215 {
1216 #if 0
1217   GstPad *otherpad;
1218   GstCaps *othercaps = NULL;
1219 #endif
1220   gboolean ret = TRUE;
1221
1222 #if 0
1223   otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
1224
1225   /* we need fixed caps for the check, fall back to the default implementation
1226    * if we don't */
1227   if (!gst_caps_is_fixed (caps))
1228 #endif
1229   {
1230     GstCaps *allowed;
1231
1232     GST_DEBUG_OBJECT (trans, "accept caps %" GST_PTR_FORMAT, caps);
1233
1234     /* get all the formats we can handle on this pad */
1235     if (direction == GST_PAD_SRC)
1236       allowed = gst_pad_query_caps (trans->srcpad, NULL);
1237     else
1238       allowed = gst_pad_query_caps (trans->sinkpad, NULL);
1239
1240     if (!allowed) {
1241       GST_DEBUG_OBJECT (trans, "gst_pad_query_caps() failed");
1242       goto no_transform_possible;
1243     }
1244
1245     GST_DEBUG_OBJECT (trans, "allowed caps %" GST_PTR_FORMAT, allowed);
1246
1247     /* intersect with the requested format */
1248     ret = gst_caps_is_subset (caps, allowed);
1249     gst_caps_unref (allowed);
1250
1251     if (!ret)
1252       goto no_transform_possible;
1253   }
1254 #if 0
1255   else {
1256     GST_DEBUG_OBJECT (pad, "accept caps %" GST_PTR_FORMAT, caps);
1257
1258     /* find best possible caps for the other pad as a way to see if we can
1259      * transform this caps. */
1260     othercaps = gst_base_transform_find_transform (trans, pad, caps, FALSE);
1261     if (!othercaps || gst_caps_is_empty (othercaps))
1262       goto no_transform_possible;
1263
1264     GST_DEBUG_OBJECT (pad, "we can transform to %" GST_PTR_FORMAT, othercaps);
1265   }
1266 #endif
1267
1268 done:
1269 #if 0
1270   /* We know it's always NULL since we never use it */
1271   if (othercaps)
1272     gst_caps_unref (othercaps);
1273 #endif
1274
1275   return ret;
1276
1277   /* ERRORS */
1278 no_transform_possible:
1279   {
1280     GST_DEBUG_OBJECT (trans,
1281         "transform could not transform %" GST_PTR_FORMAT
1282         " in anything we support", caps);
1283     ret = FALSE;
1284     goto done;
1285   }
1286 }
1287
1288 /* called when new caps arrive on the sink pad,
1289  * We try to find the best caps for the other side using our _find_transform()
1290  * function. If there are caps, we configure the transform for this new
1291  * transformation.
1292  */
1293 static gboolean
1294 gst_base_transform_setcaps (GstBaseTransform * trans, GstPad * pad,
1295     GstCaps * incaps)
1296 {
1297   GstBaseTransformPrivate *priv = trans->priv;
1298   GstCaps *outcaps;
1299   gboolean ret = TRUE;
1300
1301   GST_DEBUG_OBJECT (pad, "have new caps %p %" GST_PTR_FORMAT, incaps, incaps);
1302
1303   /* find best possible caps for the other pad */
1304   outcaps = gst_base_transform_find_transform (trans, pad, incaps);
1305   if (!outcaps || gst_caps_is_empty (outcaps))
1306     goto no_transform_possible;
1307
1308   /* configure the element now */
1309
1310   /* if we have the same caps, we can optimize and reuse the input caps */
1311   if (gst_caps_is_equal (incaps, outcaps)) {
1312     GST_INFO_OBJECT (trans, "reuse caps");
1313     gst_caps_unref (outcaps);
1314     outcaps = gst_caps_ref (incaps);
1315   }
1316
1317   /* call configure now */
1318   if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
1319     goto failed_configure;
1320
1321   GST_OBJECT_LOCK (trans->sinkpad);
1322   GST_OBJECT_FLAG_UNSET (trans->srcpad, GST_PAD_FLAG_NEED_RECONFIGURE);
1323   trans->priv->reconfigure = FALSE;
1324   GST_OBJECT_UNLOCK (trans->sinkpad);
1325
1326   /* let downstream know about our caps */
1327   gst_pad_push_event (trans->srcpad, gst_event_new_caps (outcaps));
1328
1329   if (ret) {
1330     /* try to get a pool when needed */
1331     ret = gst_base_transform_do_bufferpool (trans, outcaps);
1332   }
1333
1334 done:
1335   if (outcaps)
1336     gst_caps_unref (outcaps);
1337
1338   GST_OBJECT_LOCK (trans);
1339   priv->negotiated = ret;
1340   GST_OBJECT_UNLOCK (trans);
1341
1342   return ret;
1343
1344   /* ERRORS */
1345 no_transform_possible:
1346   {
1347     GST_WARNING_OBJECT (trans,
1348         "transform could not transform %" GST_PTR_FORMAT
1349         " in anything we support", incaps);
1350     ret = FALSE;
1351     goto done;
1352   }
1353 failed_configure:
1354   {
1355     GST_WARNING_OBJECT (trans, "FAILED to configure incaps %" GST_PTR_FORMAT
1356         " and outcaps %" GST_PTR_FORMAT, incaps, outcaps);
1357     ret = FALSE;
1358     goto done;
1359   }
1360 }
1361
1362 static gboolean
1363 gst_base_transform_default_propose_allocation (GstBaseTransform * trans,
1364     GstQuery * decide_query, GstQuery * query)
1365 {
1366   gboolean ret;
1367
1368   if (decide_query == NULL) {
1369     GST_DEBUG_OBJECT (trans, "doing passthrough query");
1370     ret = gst_pad_peer_query (trans->srcpad, query);
1371   } else {
1372     guint i, n_metas;
1373     /* non-passthrough, copy all metadata, decide_query does not contain the
1374      * metadata anymore that depends on the buffer memory */
1375     n_metas = gst_query_get_n_allocation_metas (decide_query);
1376     for (i = 0; i < n_metas; i++) {
1377       GType api;
1378
1379       api = gst_query_parse_nth_allocation_meta (decide_query, i);
1380       GST_DEBUG_OBJECT (trans, "proposing metadata %s", g_type_name (api));
1381       gst_query_add_allocation_meta (query, api);
1382     }
1383     ret = TRUE;
1384   }
1385   return ret;
1386 }
1387
1388 static gboolean
1389 gst_base_transform_default_query (GstBaseTransform * trans,
1390     GstPadDirection direction, GstQuery * query)
1391 {
1392   gboolean ret = FALSE;
1393   GstPad *pad, *otherpad;
1394   GstBaseTransformClass *klass;
1395   GstBaseTransformPrivate *priv = trans->priv;
1396
1397   if (direction == GST_PAD_SRC) {
1398     pad = trans->srcpad;
1399     otherpad = trans->sinkpad;
1400   } else {
1401     pad = trans->sinkpad;
1402     otherpad = trans->srcpad;
1403   }
1404
1405   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1406
1407   switch (GST_QUERY_TYPE (query)) {
1408     case GST_QUERY_ALLOCATION:
1409     {
1410       GstQuery *decide_query = NULL;
1411       gboolean negotiated;
1412
1413       /* can only be done on the sinkpad */
1414       if (direction != GST_PAD_SINK)
1415         goto done;
1416
1417       GST_OBJECT_LOCK (trans);
1418       if (G_UNLIKELY (!(negotiated = priv->negotiated))) {
1419         GST_DEBUG_OBJECT (trans,
1420             "not negotiated yet, can't answer ALLOCATION query");
1421         GST_OBJECT_UNLOCK (trans);
1422         goto done;
1423       }
1424       if ((decide_query = trans->priv->query))
1425         gst_query_ref (decide_query);
1426       GST_OBJECT_UNLOCK (trans);
1427
1428       GST_DEBUG_OBJECT (trans,
1429           "calling propose allocation with query %" GST_PTR_FORMAT,
1430           decide_query);
1431
1432       /* pass the query to the propose_allocation vmethod if any */
1433       if (G_LIKELY (klass->propose_allocation))
1434         ret = klass->propose_allocation (trans, decide_query, query);
1435       else
1436         ret = FALSE;
1437
1438       if (decide_query)
1439         gst_query_unref (decide_query);
1440
1441       GST_DEBUG_OBJECT (trans, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret,
1442           query);
1443       break;
1444     }
1445     case GST_QUERY_POSITION:
1446     {
1447       GstFormat format;
1448
1449       gst_query_parse_position (query, &format, NULL);
1450       if (format == GST_FORMAT_TIME && trans->segment.format == GST_FORMAT_TIME) {
1451         gint64 pos;
1452         ret = TRUE;
1453
1454         if ((direction == GST_PAD_SINK)
1455             || (trans->priv->position_out == GST_CLOCK_TIME_NONE)) {
1456           pos =
1457               gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1458               trans->segment.position);
1459         } else {
1460           pos = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
1461               trans->priv->position_out);
1462         }
1463         gst_query_set_position (query, format, pos);
1464       } else {
1465         ret = gst_pad_peer_query (otherpad, query);
1466       }
1467       break;
1468     }
1469     case GST_QUERY_ACCEPT_CAPS:
1470     {
1471       GstCaps *caps;
1472
1473       gst_query_parse_accept_caps (query, &caps);
1474       if (klass->accept_caps) {
1475         ret = klass->accept_caps (trans, direction, caps);
1476         gst_query_set_accept_caps_result (query, ret);
1477         /* return TRUE, we answered the query */
1478         ret = TRUE;
1479       }
1480       break;
1481     }
1482     case GST_QUERY_CAPS:
1483     {
1484       GstCaps *filter, *caps;
1485
1486       gst_query_parse_caps (query, &filter);
1487       caps = gst_base_transform_query_caps (trans, pad, filter);
1488       gst_query_set_caps_result (query, caps);
1489       gst_caps_unref (caps);
1490       ret = TRUE;
1491       break;
1492     }
1493     default:
1494       ret = gst_pad_peer_query (otherpad, query);
1495       break;
1496   }
1497
1498 done:
1499   return ret;
1500 }
1501
1502 static gboolean
1503 gst_base_transform_query (GstPad * pad, GstObject * parent, GstQuery * query)
1504 {
1505   GstBaseTransform *trans;
1506   GstBaseTransformClass *bclass;
1507   gboolean ret = FALSE;
1508
1509   trans = GST_BASE_TRANSFORM (parent);
1510   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1511
1512   if (bclass->query)
1513     ret = bclass->query (trans, GST_PAD_DIRECTION (pad), query);
1514
1515   return ret;
1516 }
1517
1518 /* this function either returns the input buffer without incrementing the
1519  * refcount or it allocates a new (writable) buffer */
1520 static GstFlowReturn
1521 default_prepare_output_buffer (GstBaseTransform * trans,
1522     GstBuffer * inbuf, GstBuffer ** outbuf)
1523 {
1524   GstBaseTransformPrivate *priv;
1525   GstFlowReturn ret = GST_FLOW_OK;
1526   GstBaseTransformClass *bclass;
1527   GstCaps *incaps, *outcaps;
1528   gsize insize, outsize;
1529   gboolean res;
1530
1531   priv = trans->priv;
1532   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1533
1534   /* figure out how to allocate an output buffer */
1535   if (priv->passthrough) {
1536     /* passthrough, we will not modify the incomming buffer so we can just
1537      * reuse it */
1538     GST_DEBUG_OBJECT (trans, "passthrough: reusing input buffer");
1539     *outbuf = inbuf;
1540     goto done;
1541   }
1542
1543   /* we can't reuse the input buffer */
1544   if (priv->pool) {
1545     if (!priv->pool_active) {
1546       GST_DEBUG_OBJECT (trans, "setting pool %p active", priv->pool);
1547       if (!gst_buffer_pool_set_active (priv->pool, TRUE))
1548         goto activate_failed;
1549       priv->pool_active = TRUE;
1550     }
1551     GST_DEBUG_OBJECT (trans, "using pool alloc");
1552     ret = gst_buffer_pool_acquire_buffer (priv->pool, outbuf, NULL);
1553     goto copy_meta;
1554   }
1555
1556   /* no pool, we need to figure out the size of the output buffer first */
1557   if ((bclass->transform_ip != NULL) && priv->always_in_place) {
1558     /* we want to do an in-place alloc */
1559     if (gst_buffer_is_writable (inbuf)) {
1560       GST_DEBUG_OBJECT (trans, "inplace reuse writable input buffer");
1561       *outbuf = inbuf;
1562     } else {
1563       GST_DEBUG_OBJECT (trans, "making writable buffer copy");
1564       /* we make a copy of the input buffer */
1565       *outbuf = gst_buffer_copy (inbuf);
1566     }
1567     goto done;
1568   }
1569
1570   /* else use the transform function to get the size */
1571   incaps = gst_pad_get_current_caps (trans->sinkpad);
1572   outcaps = gst_pad_get_current_caps (trans->srcpad);
1573
1574   GST_DEBUG_OBJECT (trans, "getting output size for alloc");
1575   /* copy transform, figure out the output size */
1576   insize = gst_buffer_get_size (inbuf);
1577   res = gst_base_transform_transform_size (trans,
1578       GST_PAD_SINK, incaps, insize, outcaps, &outsize);
1579
1580   gst_caps_unref (incaps);
1581   gst_caps_unref (outcaps);
1582
1583   if (!res)
1584     goto unknown_size;
1585
1586   GST_DEBUG_OBJECT (trans, "doing alloc of size %" G_GSIZE_FORMAT, outsize);
1587   *outbuf = gst_buffer_new_allocate (priv->allocator, outsize, &priv->params);
1588
1589 copy_meta:
1590   /* copy the metadata */
1591   if (bclass->copy_metadata)
1592     if (!bclass->copy_metadata (trans, inbuf, *outbuf)) {
1593       /* something failed, post a warning */
1594       GST_ELEMENT_WARNING (trans, STREAM, NOT_IMPLEMENTED,
1595           ("could not copy metadata"), (NULL));
1596     }
1597
1598 done:
1599   return ret;
1600
1601   /* ERRORS */
1602   /* ERRORS */
1603 activate_failed:
1604   {
1605     GST_ELEMENT_ERROR (trans, RESOURCE, SETTINGS,
1606         ("failed to activate bufferpool"), ("failed to activate bufferpool"));
1607     return GST_FLOW_ERROR;
1608   }
1609 unknown_size:
1610   {
1611     GST_ERROR_OBJECT (trans, "unknown output size");
1612     return GST_FLOW_ERROR;
1613   }
1614 }
1615
1616 typedef struct
1617 {
1618   GstBaseTransform *trans;
1619   GstBuffer *outbuf;
1620 } CopyMetaData;
1621
1622 static gboolean
1623 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
1624 {
1625   CopyMetaData *data = user_data;
1626   GstBaseTransform *trans = data->trans;
1627   GstBaseTransformClass *klass;
1628   const GstMetaInfo *info = (*meta)->info;
1629   GstBuffer *outbuf = data->outbuf;
1630   gboolean do_copy;
1631
1632   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1633
1634   if (GST_META_FLAG_IS_SET (*meta, GST_META_FLAG_POOLED)) {
1635     /* never call the transform_meta with pool private metadata */
1636     GST_DEBUG_OBJECT (trans, "not copying pooled metadata %s",
1637         g_type_name (info->api));
1638     do_copy = FALSE;
1639   } else if (gst_meta_api_type_has_tag (info->api, GST_META_TAG_MEMORY)) {
1640     /* never call the transform_meta with memory specific metadata */
1641     GST_DEBUG_OBJECT (trans, "not copying memory specific metadata %s",
1642         g_type_name (info->api));
1643     do_copy = FALSE;
1644   } else if (klass->transform_meta) {
1645     do_copy = klass->transform_meta (trans, outbuf, *meta, inbuf);
1646     GST_DEBUG_OBJECT (trans, "transformed metadata %s: copy: %d",
1647         g_type_name (info->api), do_copy);
1648   } else {
1649     do_copy = FALSE;
1650     GST_DEBUG_OBJECT (trans, "not copying metadata %s",
1651         g_type_name (info->api));
1652   }
1653
1654   /* we only copy metadata when the subclass implemented a transform_meta
1655    * function and when it returns TRUE */
1656   if (do_copy) {
1657     GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
1658     GST_DEBUG_OBJECT (trans, "copy metadata %s", g_type_name (info->api));
1659     /* simply copy then */
1660     info->transform_func (outbuf, *meta, inbuf,
1661         _gst_meta_transform_copy, &copy_data);
1662   }
1663   return TRUE;
1664 }
1665
1666 static gboolean
1667 default_copy_metadata (GstBaseTransform * trans,
1668     GstBuffer * inbuf, GstBuffer * outbuf)
1669 {
1670   GstBaseTransformPrivate *priv = trans->priv;
1671   CopyMetaData data;
1672
1673   /* now copy the metadata */
1674   GST_DEBUG_OBJECT (trans, "copying metadata");
1675
1676   /* this should not happen, buffers allocated from a pool or with
1677    * new_allocate should always be writable. */
1678   if (!gst_buffer_is_writable (outbuf))
1679     goto not_writable;
1680
1681   /* when we get here, the metadata should be writable */
1682   gst_buffer_copy_into (outbuf, inbuf,
1683       GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1684
1685   /* clear the GAP flag when the subclass does not understand it */
1686   if (!priv->gap_aware)
1687     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
1688
1689
1690   data.trans = trans;
1691   data.outbuf = outbuf;
1692
1693   gst_buffer_foreach_meta (inbuf, foreach_metadata, &data);
1694
1695   return TRUE;
1696
1697   /* ERRORS */
1698 not_writable:
1699   {
1700     GST_WARNING_OBJECT (trans, "buffer %p not writable", outbuf);
1701     return FALSE;
1702   }
1703 }
1704
1705 /* Given @caps calcultate the size of one unit.
1706  *
1707  * For video caps, this is the size of one frame (and thus one buffer).
1708  * For audio caps, this is the size of one sample.
1709  *
1710  * These values are cached since they do not change and the calculation
1711  * potentially involves parsing caps and other expensive stuff.
1712  *
1713  * We have two cache locations to store the size, one for the source caps
1714  * and one for the sink caps.
1715  *
1716  * this function returns FALSE if no size could be calculated.
1717  */
1718 static gboolean
1719 gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
1720     gsize * size)
1721 {
1722   gboolean res = FALSE;
1723   GstBaseTransformClass *bclass;
1724   GstBaseTransformPrivate *priv = trans->priv;
1725
1726   /* see if we have the result cached */
1727   if (priv->cache_caps1 == caps) {
1728     *size = priv->cache_caps1_size;
1729     GST_DEBUG_OBJECT (trans,
1730         "returned %" G_GSIZE_FORMAT " from first cache", *size);
1731     return TRUE;
1732   }
1733   if (priv->cache_caps2 == caps) {
1734     *size = priv->cache_caps2_size;
1735     GST_DEBUG_OBJECT (trans,
1736         "returned %" G_GSIZE_FORMAT " from second cached", *size);
1737     return TRUE;
1738   }
1739
1740   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1741   res = bclass->get_unit_size (trans, caps, size);
1742   GST_DEBUG_OBJECT (trans,
1743       "caps %" GST_PTR_FORMAT ") has unit size %" G_GSIZE_FORMAT ", res %s",
1744       caps, *size, res ? "TRUE" : "FALSE");
1745
1746   if (res) {
1747     /* and cache the values */
1748     if (priv->cache_caps1 == NULL) {
1749       gst_caps_replace (&priv->cache_caps1, caps);
1750       priv->cache_caps1_size = *size;
1751       GST_DEBUG_OBJECT (trans,
1752           "caching %" G_GSIZE_FORMAT " in first cache", *size);
1753     } else if (priv->cache_caps2 == NULL) {
1754       gst_caps_replace (&priv->cache_caps2, caps);
1755       priv->cache_caps2_size = *size;
1756       GST_DEBUG_OBJECT (trans,
1757           "caching %" G_GSIZE_FORMAT " in second cache", *size);
1758     } else {
1759       GST_DEBUG_OBJECT (trans, "no free spot to cache unit_size");
1760     }
1761   }
1762   return res;
1763 }
1764
1765 static gboolean
1766 gst_base_transform_sink_event (GstPad * pad, GstObject * parent,
1767     GstEvent * event)
1768 {
1769   GstBaseTransform *trans;
1770   GstBaseTransformClass *bclass;
1771   gboolean ret = TRUE;
1772
1773   trans = GST_BASE_TRANSFORM (parent);
1774   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1775
1776   if (bclass->sink_event)
1777     ret = bclass->sink_event (trans, event);
1778   else
1779     gst_event_unref (event);
1780
1781   return ret;
1782 }
1783
1784 static gboolean
1785 gst_base_transform_sink_eventfunc (GstBaseTransform * trans, GstEvent * event)
1786 {
1787   gboolean ret = TRUE, forward = TRUE;
1788   GstBaseTransformPrivate *priv = trans->priv;
1789
1790   switch (GST_EVENT_TYPE (event)) {
1791     case GST_EVENT_FLUSH_START:
1792       break;
1793     case GST_EVENT_FLUSH_STOP:
1794       GST_OBJECT_LOCK (trans);
1795       /* reset QoS parameters */
1796       priv->proportion = 1.0;
1797       priv->earliest_time = -1;
1798       priv->discont = FALSE;
1799       priv->processed = 0;
1800       priv->dropped = 0;
1801       GST_OBJECT_UNLOCK (trans);
1802       /* we need new segment info after the flush. */
1803       trans->have_segment = FALSE;
1804       gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
1805       priv->position_out = GST_CLOCK_TIME_NONE;
1806       break;
1807     case GST_EVENT_EOS:
1808       break;
1809     case GST_EVENT_TAG:
1810       break;
1811     case GST_EVENT_CAPS:
1812     {
1813       GstCaps *caps;
1814
1815       gst_event_parse_caps (event, &caps);
1816       ret = gst_base_transform_setcaps (trans, trans->sinkpad, caps);
1817
1818       forward = FALSE;
1819       break;
1820     }
1821     case GST_EVENT_SEGMENT:
1822     {
1823       gst_event_copy_segment (event, &trans->segment);
1824       trans->have_segment = TRUE;
1825
1826       GST_DEBUG_OBJECT (trans, "received SEGMENT %" GST_SEGMENT_FORMAT,
1827           &trans->segment);
1828       break;
1829     }
1830     default:
1831       break;
1832   }
1833
1834   if (ret && forward)
1835     ret = gst_pad_push_event (trans->srcpad, event);
1836   else
1837     gst_event_unref (event);
1838
1839   return ret;
1840 }
1841
1842 static gboolean
1843 gst_base_transform_src_event (GstPad * pad, GstObject * parent,
1844     GstEvent * event)
1845 {
1846   GstBaseTransform *trans;
1847   GstBaseTransformClass *bclass;
1848   gboolean ret = TRUE;
1849
1850   trans = GST_BASE_TRANSFORM (parent);
1851   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1852
1853   if (bclass->src_event)
1854     ret = bclass->src_event (trans, event);
1855   else
1856     gst_event_unref (event);
1857
1858   return ret;
1859 }
1860
1861 static gboolean
1862 gst_base_transform_src_eventfunc (GstBaseTransform * trans, GstEvent * event)
1863 {
1864   gboolean ret;
1865
1866   GST_DEBUG_OBJECT (trans, "handling event %p %" GST_PTR_FORMAT, event, event);
1867
1868   switch (GST_EVENT_TYPE (event)) {
1869     case GST_EVENT_SEEK:
1870       break;
1871     case GST_EVENT_NAVIGATION:
1872       break;
1873     case GST_EVENT_QOS:
1874     {
1875       gdouble proportion;
1876       GstClockTimeDiff diff;
1877       GstClockTime timestamp;
1878
1879       gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
1880       gst_base_transform_update_qos (trans, proportion, diff, timestamp);
1881       break;
1882     }
1883     default:
1884       break;
1885   }
1886
1887   ret = gst_pad_push_event (trans->sinkpad, event);
1888
1889   return ret;
1890 }
1891
1892 /* perform a transform on @inbuf and put the result in @outbuf.
1893  *
1894  * This function is common to the push and pull-based operations.
1895  *
1896  * This function takes ownership of @inbuf */
1897 static GstFlowReturn
1898 gst_base_transform_handle_buffer (GstBaseTransform * trans, GstBuffer * inbuf,
1899     GstBuffer ** outbuf)
1900 {
1901   GstBaseTransformClass *bclass;
1902   GstBaseTransformPrivate *priv = trans->priv;
1903   GstFlowReturn ret = GST_FLOW_OK;
1904   gboolean want_in_place;
1905   GstClockTime running_time;
1906   GstClockTime timestamp;
1907   gboolean reconfigure;
1908
1909   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
1910
1911   GST_OBJECT_LOCK (trans->sinkpad);
1912   reconfigure = GST_PAD_NEEDS_RECONFIGURE (trans->srcpad)
1913       || priv->reconfigure;
1914   GST_OBJECT_FLAG_UNSET (trans->srcpad, GST_PAD_FLAG_NEED_RECONFIGURE);
1915   priv->reconfigure = FALSE;
1916   GST_OBJECT_UNLOCK (trans->sinkpad);
1917
1918   if (G_UNLIKELY (reconfigure)) {
1919     GstCaps *incaps;
1920
1921     GST_DEBUG_OBJECT (trans, "we had a pending reconfigure");
1922
1923     incaps = gst_pad_get_current_caps (trans->sinkpad);
1924     if (incaps == NULL)
1925       goto no_reconfigure;
1926
1927     /* if we need to reconfigure we pretend new caps arrived. This
1928      * will reconfigure the transform with the new output format. */
1929     if (!gst_base_transform_setcaps (trans, trans->sinkpad, incaps)) {
1930       gst_caps_unref (incaps);
1931       goto not_negotiated;
1932     }
1933     gst_caps_unref (incaps);
1934   }
1935
1936 no_reconfigure:
1937   if (GST_BUFFER_OFFSET_IS_VALID (inbuf))
1938     GST_DEBUG_OBJECT (trans,
1939         "handling buffer %p of size %" G_GSIZE_FORMAT " and offset %"
1940         G_GUINT64_FORMAT, inbuf, gst_buffer_get_size (inbuf),
1941         GST_BUFFER_OFFSET (inbuf));
1942   else
1943     GST_DEBUG_OBJECT (trans,
1944         "handling buffer %p of size %" G_GSIZE_FORMAT " and offset NONE", inbuf,
1945         gst_buffer_get_size (inbuf));
1946
1947   /* Don't allow buffer handling before negotiation, except in passthrough mode
1948    * or if the class doesn't implement a set_caps function (in which case it doesn't
1949    * care about caps)
1950    */
1951   if (!priv->negotiated && !priv->passthrough && (bclass->set_caps != NULL))
1952     goto not_negotiated;
1953
1954   /* Set discont flag so we can mark the outgoing buffer */
1955   if (GST_BUFFER_IS_DISCONT (inbuf)) {
1956     GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", inbuf);
1957     priv->discont = TRUE;
1958   }
1959
1960   /* can only do QoS if the segment is in TIME */
1961   if (trans->segment.format != GST_FORMAT_TIME)
1962     goto no_qos;
1963
1964   /* QOS is done on the running time of the buffer, get it now */
1965   timestamp = GST_BUFFER_TIMESTAMP (inbuf);
1966   running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
1967       timestamp);
1968
1969   if (running_time != -1) {
1970     gboolean need_skip;
1971     GstClockTime earliest_time;
1972     gdouble proportion;
1973
1974     /* lock for getting the QoS parameters that are set (in a different thread)
1975      * with the QOS events */
1976     GST_OBJECT_LOCK (trans);
1977     earliest_time = priv->earliest_time;
1978     proportion = priv->proportion;
1979     /* check for QoS, don't perform conversion for buffers
1980      * that are known to be late. */
1981     need_skip = priv->qos_enabled &&
1982         earliest_time != -1 && running_time <= earliest_time;
1983     GST_OBJECT_UNLOCK (trans);
1984
1985     if (need_skip) {
1986       GstMessage *qos_msg;
1987       GstClockTime duration;
1988       guint64 stream_time;
1989       gint64 jitter;
1990
1991       GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "skipping transform: qostime %"
1992           GST_TIME_FORMAT " <= %" GST_TIME_FORMAT,
1993           GST_TIME_ARGS (running_time), GST_TIME_ARGS (earliest_time));
1994
1995       priv->dropped++;
1996
1997       duration = GST_BUFFER_DURATION (inbuf);
1998       stream_time =
1999           gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
2000           timestamp);
2001       jitter = GST_CLOCK_DIFF (running_time, earliest_time);
2002
2003       qos_msg =
2004           gst_message_new_qos (GST_OBJECT_CAST (trans), FALSE, running_time,
2005           stream_time, timestamp, duration);
2006       gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
2007       gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
2008           priv->processed, priv->dropped);
2009       gst_element_post_message (GST_ELEMENT_CAST (trans), qos_msg);
2010
2011       /* mark discont for next buffer */
2012       priv->discont = TRUE;
2013       goto skip;
2014     }
2015   }
2016
2017 no_qos:
2018
2019   /* first try to allocate an output buffer based on the currently negotiated
2020    * format. outbuf will contain a buffer suitable for doing the configured
2021    * transform after this function. */
2022   if (bclass->prepare_output_buffer == NULL)
2023     goto no_prepare;
2024
2025   GST_DEBUG_OBJECT (trans, "calling prepare buffer");
2026   ret = bclass->prepare_output_buffer (trans, inbuf, outbuf);
2027
2028   if (ret != GST_FLOW_OK || *outbuf == NULL)
2029     goto no_buffer;
2030
2031   GST_DEBUG_OBJECT (trans, "using allocated buffer in %p, out %p", inbuf,
2032       *outbuf);
2033
2034   /* now perform the needed transform */
2035   if (priv->passthrough) {
2036     /* In passthrough mode, give transform_ip a look at the
2037      * buffer, without making it writable, or just push the
2038      * data through */
2039     if (bclass->transform_ip_on_passthrough && bclass->transform_ip) {
2040       GST_DEBUG_OBJECT (trans, "doing passthrough transform_ip");
2041       ret = bclass->transform_ip (trans, *outbuf);
2042     } else {
2043       GST_DEBUG_OBJECT (trans, "element is in passthrough");
2044     }
2045   } else {
2046     want_in_place = (bclass->transform_ip != NULL) && priv->always_in_place;
2047
2048     if (want_in_place) {
2049       GST_DEBUG_OBJECT (trans, "doing inplace transform");
2050       ret = bclass->transform_ip (trans, *outbuf);
2051     } else {
2052       GST_DEBUG_OBJECT (trans, "doing non-inplace transform");
2053
2054       if (bclass->transform)
2055         ret = bclass->transform (trans, inbuf, *outbuf);
2056       else
2057         ret = GST_FLOW_NOT_SUPPORTED;
2058     }
2059   }
2060
2061 skip:
2062   /* only unref input buffer if we allocated a new outbuf buffer. If we reused
2063    * the input buffer, no refcount is changed to keep the input buffer writable
2064    * when needed. */
2065   if (*outbuf != inbuf)
2066     gst_buffer_unref (inbuf);
2067
2068   return ret;
2069
2070   /* ERRORS */
2071 not_negotiated:
2072   {
2073     gst_buffer_unref (inbuf);
2074     *outbuf = NULL;
2075     GST_ELEMENT_ERROR (trans, STREAM, FORMAT,
2076         ("not negotiated"), ("not negotiated"));
2077     return GST_FLOW_NOT_NEGOTIATED;
2078   }
2079 no_prepare:
2080   {
2081     gst_buffer_unref (inbuf);
2082     GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
2083         ("Sub-class has no prepare_output_buffer implementation"), (NULL));
2084     return GST_FLOW_NOT_SUPPORTED;
2085   }
2086 no_buffer:
2087   {
2088     gst_buffer_unref (inbuf);
2089     *outbuf = NULL;
2090     GST_WARNING_OBJECT (trans, "could not get buffer from pool: %s",
2091         gst_flow_get_name (ret));
2092     return ret;
2093   }
2094 }
2095
2096 /* FIXME, getrange is broken, need to pull range from the other
2097  * end based on the transform_size result.
2098  */
2099 static GstFlowReturn
2100 gst_base_transform_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2101     guint length, GstBuffer ** buffer)
2102 {
2103   GstBaseTransform *trans;
2104   GstBaseTransformClass *klass;
2105   GstFlowReturn ret;
2106   GstBuffer *inbuf = NULL;
2107
2108   trans = GST_BASE_TRANSFORM (parent);
2109
2110   ret = gst_pad_pull_range (trans->sinkpad, offset, length, &inbuf);
2111   if (G_UNLIKELY (ret != GST_FLOW_OK))
2112     goto pull_error;
2113
2114   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2115   if (klass->before_transform)
2116     klass->before_transform (trans, inbuf);
2117
2118   ret = gst_base_transform_handle_buffer (trans, inbuf, buffer);
2119
2120 done:
2121   return ret;
2122
2123   /* ERRORS */
2124 pull_error:
2125   {
2126     GST_DEBUG_OBJECT (trans, "failed to pull a buffer: %s",
2127         gst_flow_get_name (ret));
2128     goto done;
2129   }
2130 }
2131
2132 static GstFlowReturn
2133 gst_base_transform_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2134 {
2135   GstBaseTransform *trans;
2136   GstBaseTransformClass *klass;
2137   GstBaseTransformPrivate *priv;
2138   GstFlowReturn ret;
2139   GstClockTime position = GST_CLOCK_TIME_NONE;
2140   GstClockTime timestamp, duration;
2141   GstBuffer *outbuf = NULL;
2142
2143   trans = GST_BASE_TRANSFORM (parent);
2144   priv = trans->priv;
2145
2146   timestamp = GST_BUFFER_TIMESTAMP (buffer);
2147   duration = GST_BUFFER_DURATION (buffer);
2148
2149   /* calculate end position of the incoming buffer */
2150   if (timestamp != GST_CLOCK_TIME_NONE) {
2151     if (duration != GST_CLOCK_TIME_NONE)
2152       position = timestamp + duration;
2153     else
2154       position = timestamp;
2155   }
2156
2157   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2158   if (klass->before_transform)
2159     klass->before_transform (trans, buffer);
2160
2161   /* protect transform method and concurrent buffer alloc */
2162   ret = gst_base_transform_handle_buffer (trans, buffer, &outbuf);
2163
2164   /* outbuf can be NULL, this means a dropped buffer, if we have a buffer but
2165    * GST_BASE_TRANSFORM_FLOW_DROPPED we will not push either. */
2166   if (outbuf != NULL) {
2167     if (ret == GST_FLOW_OK) {
2168       GstClockTime position_out = GST_CLOCK_TIME_NONE;
2169
2170       /* Remember last stop position */
2171       if (position != GST_CLOCK_TIME_NONE &&
2172           trans->segment.format == GST_FORMAT_TIME)
2173         trans->segment.position = position;
2174
2175       if (GST_BUFFER_TIMESTAMP_IS_VALID (outbuf)) {
2176         position_out = GST_BUFFER_TIMESTAMP (outbuf);
2177         if (GST_BUFFER_DURATION_IS_VALID (outbuf))
2178           position_out += GST_BUFFER_DURATION (outbuf);
2179       } else if (position != GST_CLOCK_TIME_NONE) {
2180         position_out = position;
2181       }
2182       if (position_out != GST_CLOCK_TIME_NONE
2183           && trans->segment.format == GST_FORMAT_TIME)
2184         priv->position_out = position_out;
2185
2186       /* apply DISCONT flag if the buffer is not yet marked as such */
2187       if (trans->priv->discont) {
2188         GST_DEBUG_OBJECT (trans, "we have a pending DISCONT");
2189         if (!GST_BUFFER_IS_DISCONT (outbuf)) {
2190           GST_DEBUG_OBJECT (trans, "marking DISCONT on output buffer");
2191           outbuf = gst_buffer_make_writable (outbuf);
2192           GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2193         }
2194         priv->discont = FALSE;
2195       }
2196       priv->processed++;
2197
2198       ret = gst_pad_push (trans->srcpad, outbuf);
2199     } else {
2200       GST_DEBUG_OBJECT (trans, "we got return %s", gst_flow_get_name (ret));
2201       gst_buffer_unref (outbuf);
2202     }
2203   }
2204
2205   /* convert internal flow to OK and mark discont for the next buffer. */
2206   if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED) {
2207     GST_DEBUG_OBJECT (trans, "dropped a buffer, marking DISCONT");
2208     priv->discont = TRUE;
2209     ret = GST_FLOW_OK;
2210   }
2211
2212   return ret;
2213 }
2214
2215 static void
2216 gst_base_transform_set_property (GObject * object, guint prop_id,
2217     const GValue * value, GParamSpec * pspec)
2218 {
2219   GstBaseTransform *trans;
2220
2221   trans = GST_BASE_TRANSFORM (object);
2222
2223   switch (prop_id) {
2224     case PROP_QOS:
2225       gst_base_transform_set_qos_enabled (trans, g_value_get_boolean (value));
2226       break;
2227     default:
2228       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2229       break;
2230   }
2231 }
2232
2233 static void
2234 gst_base_transform_get_property (GObject * object, guint prop_id,
2235     GValue * value, GParamSpec * pspec)
2236 {
2237   GstBaseTransform *trans;
2238
2239   trans = GST_BASE_TRANSFORM (object);
2240
2241   switch (prop_id) {
2242     case PROP_QOS:
2243       g_value_set_boolean (value, gst_base_transform_is_qos_enabled (trans));
2244       break;
2245     default:
2246       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2247       break;
2248   }
2249 }
2250
2251 /* not a vmethod of anything, just an internal method */
2252 static gboolean
2253 gst_base_transform_activate (GstBaseTransform * trans, gboolean active)
2254 {
2255   GstBaseTransformClass *bclass;
2256   GstBaseTransformPrivate *priv = trans->priv;
2257   gboolean result = TRUE;
2258
2259   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2260
2261   if (active) {
2262     GstCaps *incaps, *outcaps;
2263
2264     if (priv->pad_mode == GST_PAD_MODE_NONE && bclass->start)
2265       result &= bclass->start (trans);
2266
2267     incaps = gst_pad_get_current_caps (trans->sinkpad);
2268     outcaps = gst_pad_get_current_caps (trans->srcpad);
2269
2270     GST_OBJECT_LOCK (trans);
2271     if (incaps && outcaps)
2272       priv->have_same_caps =
2273           gst_caps_is_equal (incaps, outcaps) || priv->passthrough;
2274     else
2275       priv->have_same_caps = priv->passthrough;
2276     GST_DEBUG_OBJECT (trans, "have_same_caps %d", priv->have_same_caps);
2277     priv->negotiated = FALSE;
2278     trans->have_segment = FALSE;
2279     gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
2280     priv->position_out = GST_CLOCK_TIME_NONE;
2281     priv->proportion = 1.0;
2282     priv->earliest_time = -1;
2283     priv->discont = FALSE;
2284     priv->processed = 0;
2285     priv->dropped = 0;
2286     GST_OBJECT_UNLOCK (trans);
2287
2288     if (incaps)
2289       gst_caps_unref (incaps);
2290     if (outcaps)
2291       gst_caps_unref (outcaps);
2292   } else {
2293     /* We must make sure streaming has finished before resetting things
2294      * and calling the ::stop vfunc */
2295     GST_PAD_STREAM_LOCK (trans->sinkpad);
2296     GST_PAD_STREAM_UNLOCK (trans->sinkpad);
2297
2298     priv->have_same_caps = FALSE;
2299     /* We can only reset the passthrough mode if the instance told us to 
2300        handle it in configure_caps */
2301     if (bclass->passthrough_on_same_caps) {
2302       gst_base_transform_set_passthrough (trans, FALSE);
2303     }
2304     gst_caps_replace (&priv->cache_caps1, NULL);
2305     gst_caps_replace (&priv->cache_caps2, NULL);
2306
2307     if (priv->pad_mode != GST_PAD_MODE_NONE && bclass->stop)
2308       result &= bclass->stop (trans);
2309
2310     gst_base_transform_set_allocation (trans, NULL, NULL, NULL, NULL);
2311   }
2312
2313   return result;
2314 }
2315
2316 static gboolean
2317 gst_base_transform_sink_activate_mode (GstPad * pad, GstObject * parent,
2318     GstPadMode mode, gboolean active)
2319 {
2320   gboolean result = FALSE;
2321   GstBaseTransform *trans;
2322
2323   trans = GST_BASE_TRANSFORM (parent);
2324
2325   switch (mode) {
2326     case GST_PAD_MODE_PUSH:
2327     {
2328       result = gst_base_transform_activate (trans, active);
2329
2330       if (result)
2331         trans->priv->pad_mode = active ? GST_PAD_MODE_PUSH : GST_PAD_MODE_NONE;
2332
2333       break;
2334     }
2335     default:
2336       result = TRUE;
2337       break;
2338   }
2339   return result;
2340 }
2341
2342 static gboolean
2343 gst_base_transform_src_activate_mode (GstPad * pad, GstObject * parent,
2344     GstPadMode mode, gboolean active)
2345 {
2346   gboolean result = FALSE;
2347   GstBaseTransform *trans;
2348
2349   trans = GST_BASE_TRANSFORM (parent);
2350
2351   switch (mode) {
2352     case GST_PAD_MODE_PULL:
2353     {
2354       result =
2355           gst_pad_activate_mode (trans->sinkpad, GST_PAD_MODE_PULL, active);
2356
2357       if (result)
2358         result &= gst_base_transform_activate (trans, active);
2359
2360       if (result)
2361         trans->priv->pad_mode = active ? mode : GST_PAD_MODE_NONE;
2362       break;
2363     }
2364     default:
2365       result = TRUE;
2366       break;
2367   }
2368
2369   return result;
2370 }
2371
2372 /**
2373  * gst_base_transform_set_passthrough:
2374  * @trans: the #GstBaseTransform to set
2375  * @passthrough: boolean indicating passthrough mode.
2376  *
2377  * Set passthrough mode for this filter by default. This is mostly
2378  * useful for filters that do not care about negotiation.
2379  *
2380  * Always TRUE for filters which don't implement either a transform
2381  * or transform_ip method.
2382  *
2383  * MT safe.
2384  */
2385 void
2386 gst_base_transform_set_passthrough (GstBaseTransform * trans,
2387     gboolean passthrough)
2388 {
2389   GstBaseTransformClass *bclass;
2390
2391   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2392
2393   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2394
2395   GST_OBJECT_LOCK (trans);
2396   if (passthrough == FALSE) {
2397     if (bclass->transform_ip || bclass->transform)
2398       trans->priv->passthrough = FALSE;
2399   } else {
2400     trans->priv->passthrough = TRUE;
2401   }
2402
2403   GST_DEBUG_OBJECT (trans, "set passthrough %d", trans->priv->passthrough);
2404   GST_OBJECT_UNLOCK (trans);
2405 }
2406
2407 /**
2408  * gst_base_transform_is_passthrough:
2409  * @trans: the #GstBaseTransform to query
2410  *
2411  * See if @trans is configured as a passthrough transform.
2412  *
2413  * Returns: TRUE is the transform is configured in passthrough mode.
2414  *
2415  * MT safe.
2416  */
2417 gboolean
2418 gst_base_transform_is_passthrough (GstBaseTransform * trans)
2419 {
2420   gboolean result;
2421
2422   g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2423
2424   GST_OBJECT_LOCK (trans);
2425   result = trans->priv->passthrough;
2426   GST_OBJECT_UNLOCK (trans);
2427
2428   return result;
2429 }
2430
2431 /**
2432  * gst_base_transform_set_in_place:
2433  * @trans: the #GstBaseTransform to modify
2434  * @in_place: Boolean value indicating that we would like to operate
2435  * on in_place buffers.
2436  *
2437  * Determines whether a non-writable buffer will be copied before passing
2438  * to the transform_ip function.
2439  * <itemizedlist>
2440  *   <listitem>Always TRUE if no transform function is implemented.</listitem>
2441  *   <listitem>Always FALSE if ONLY transform function is implemented.</listitem>
2442  * </itemizedlist>
2443  *
2444  * MT safe.
2445  */
2446 void
2447 gst_base_transform_set_in_place (GstBaseTransform * trans, gboolean in_place)
2448 {
2449   GstBaseTransformClass *bclass;
2450
2451   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2452
2453   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
2454
2455   GST_OBJECT_LOCK (trans);
2456
2457   if (in_place) {
2458     if (bclass->transform_ip) {
2459       GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
2460       trans->priv->always_in_place = TRUE;
2461     }
2462   } else {
2463     if (bclass->transform) {
2464       GST_DEBUG_OBJECT (trans, "setting in_place FALSE");
2465       trans->priv->always_in_place = FALSE;
2466     }
2467   }
2468
2469   GST_OBJECT_UNLOCK (trans);
2470 }
2471
2472 /**
2473  * gst_base_transform_is_in_place:
2474  * @trans: the #GstBaseTransform to query
2475  *
2476  * See if @trans is configured as a in_place transform.
2477  *
2478  * Returns: TRUE is the transform is configured in in_place mode.
2479  *
2480  * MT safe.
2481  */
2482 gboolean
2483 gst_base_transform_is_in_place (GstBaseTransform * trans)
2484 {
2485   gboolean result;
2486
2487   g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2488
2489   GST_OBJECT_LOCK (trans);
2490   result = trans->priv->always_in_place;
2491   GST_OBJECT_UNLOCK (trans);
2492
2493   return result;
2494 }
2495
2496 /**
2497  * gst_base_transform_update_qos:
2498  * @trans: a #GstBaseTransform
2499  * @proportion: the proportion
2500  * @diff: the diff against the clock
2501  * @timestamp: the timestamp of the buffer generating the QoS expressed in
2502  * running_time.
2503  *
2504  * Set the QoS parameters in the transform. This function is called internally
2505  * when a QOS event is received but subclasses can provide custom information
2506  * when needed.
2507  *
2508  * MT safe.
2509  *
2510  * Since: 0.10.5
2511  */
2512 void
2513 gst_base_transform_update_qos (GstBaseTransform * trans,
2514     gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2515 {
2516   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2517
2518   GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans,
2519       "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2520       GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2521
2522   GST_OBJECT_LOCK (trans);
2523   trans->priv->proportion = proportion;
2524   trans->priv->earliest_time = timestamp + diff;
2525   GST_OBJECT_UNLOCK (trans);
2526 }
2527
2528 /**
2529  * gst_base_transform_set_qos_enabled:
2530  * @trans: a #GstBaseTransform
2531  * @enabled: new state
2532  *
2533  * Enable or disable QoS handling in the transform.
2534  *
2535  * MT safe.
2536  *
2537  * Since: 0.10.5
2538  */
2539 void
2540 gst_base_transform_set_qos_enabled (GstBaseTransform * trans, gboolean enabled)
2541 {
2542   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2543
2544   GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "enabled: %d", enabled);
2545
2546   GST_OBJECT_LOCK (trans);
2547   trans->priv->qos_enabled = enabled;
2548   GST_OBJECT_UNLOCK (trans);
2549 }
2550
2551 /**
2552  * gst_base_transform_is_qos_enabled:
2553  * @trans: a #GstBaseTransform
2554  *
2555  * Queries if the transform will handle QoS.
2556  *
2557  * Returns: TRUE if QoS is enabled.
2558  *
2559  * MT safe.
2560  *
2561  * Since: 0.10.5
2562  */
2563 gboolean
2564 gst_base_transform_is_qos_enabled (GstBaseTransform * trans)
2565 {
2566   gboolean result;
2567
2568   g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
2569
2570   GST_OBJECT_LOCK (trans);
2571   result = trans->priv->qos_enabled;
2572   GST_OBJECT_UNLOCK (trans);
2573
2574   return result;
2575 }
2576
2577 /**
2578  * gst_base_transform_set_gap_aware:
2579  * @trans: a #GstBaseTransform
2580  * @gap_aware: New state
2581  *
2582  * If @gap_aware is %FALSE (the default), output buffers will have the
2583  * %GST_BUFFER_FLAG_GAP flag unset.
2584  *
2585  * If set to %TRUE, the element must handle output buffers with this flag set
2586  * correctly, i.e. it can assume that the buffer contains neutral data but must
2587  * unset the flag if the output is no neutral data.
2588  *
2589  * MT safe.
2590  *
2591  * Since: 0.10.16
2592  */
2593 void
2594 gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware)
2595 {
2596   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2597
2598   GST_OBJECT_LOCK (trans);
2599   trans->priv->gap_aware = gap_aware;
2600   GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware);
2601   GST_OBJECT_UNLOCK (trans);
2602 }
2603
2604 /**
2605  * gst_base_transform_reconfigure_sink:
2606  * @trans: a #GstBaseTransform
2607  *
2608  * Instructs @trans to request renegotiation upstream. This function is
2609  * typically called after properties on the transform were set that
2610  * influence the input format.
2611  */
2612 void
2613 gst_base_transform_reconfigure_sink (GstBaseTransform * trans)
2614 {
2615   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2616
2617   /* push the renegotiate event */
2618   if (!gst_pad_push_event (GST_BASE_TRANSFORM_SINK_PAD (trans),
2619           gst_event_new_reconfigure ()))
2620     GST_DEBUG_OBJECT (trans, "Renegotiate event wasn't handled");
2621 }
2622
2623 /**
2624  * gst_base_transform_reconfigure_src:
2625  * @trans: a #GstBaseTransform
2626  *
2627  * Instructs @trans to renegotiate a new downstream transform on the next
2628  * buffer. This function is typically called after properties on the transform
2629  * were set that influence the output format.
2630  *
2631  * Since: 0.10.21
2632  */
2633 void
2634 gst_base_transform_reconfigure_src (GstBaseTransform * trans)
2635 {
2636   g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
2637
2638   GST_OBJECT_LOCK (trans);
2639   GST_DEBUG_OBJECT (trans, "marking reconfigure");
2640   trans->priv->reconfigure = TRUE;
2641   GST_OBJECT_UNLOCK (trans);
2642 }