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