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