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