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