Debian Codegraph @

libs/gst/base/gstbasetransform.c

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