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