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