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