2 * Copyright (C) 2014 Collabora Ltd.
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
32 #include "gstv4l2object.h"
33 #include "gstv4l2transform.h"
36 #include <gst/gst-i18n-plugin.h>
38 #define DEFAULT_PROP_DEVICE "/dev/video10"
40 #define V4L2_TRANSFORM_QUARK \
41 g_quark_from_static_string("gst-v4l2-transform-info")
43 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_transform_debug);
44 #define GST_CAT_DEFAULT gst_v4l2_transform_debug
50 V4L2_STD_OBJECT_PROPS,
51 PROP_DISABLE_PASSTHROUGH
59 } GstV4l2TransformCData;
61 #define gst_v4l2_transform_parent_class parent_class
62 G_DEFINE_ABSTRACT_TYPE (GstV4l2Transform, gst_v4l2_transform,
63 GST_TYPE_BASE_TRANSFORM);
66 gst_v4l2_transform_set_property (GObject * object,
67 guint prop_id, const GValue * value, GParamSpec * pspec)
69 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
72 case PROP_CAPTURE_IO_MODE:
73 gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, value,
76 case PROP_DISABLE_PASSTHROUGH:
77 self->disable_passthrough = g_value_get_boolean (value);
80 /* By default, only set on output */
82 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
83 prop_id, value, pspec)) {
84 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
91 gst_v4l2_transform_get_property (GObject * object,
92 guint prop_id, GValue * value, GParamSpec * pspec)
94 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
97 case PROP_CAPTURE_IO_MODE:
98 gst_v4l2_object_get_property_helper (self->v4l2capture, prop_id, value,
101 case PROP_DISABLE_PASSTHROUGH:
102 g_value_set_boolean (value, self->disable_passthrough);
105 /* By default read from output */
107 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
108 prop_id, value, pspec)) {
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116 gst_v4l2_transform_open (GstV4l2Transform * self)
118 GST_DEBUG_OBJECT (self, "Opening");
120 if (!gst_v4l2_object_open (self->v4l2output))
123 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
126 self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
127 gst_v4l2_object_get_raw_caps ());
129 if (gst_caps_is_empty (self->probed_sinkcaps))
130 goto no_input_format;
132 self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
133 gst_v4l2_object_get_raw_caps ());
135 if (gst_caps_is_empty (self->probed_srccaps))
136 goto no_output_format;
141 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
142 (_("Converter on device %s has no supported input format"),
143 self->v4l2output->videodev), (NULL));
148 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
149 (_("Converter on device %s has no supported output format"),
150 self->v4l2output->videodev), (NULL));
154 if (GST_V4L2_IS_OPEN (self->v4l2output))
155 gst_v4l2_object_close (self->v4l2output);
157 if (GST_V4L2_IS_OPEN (self->v4l2capture))
158 gst_v4l2_object_close (self->v4l2capture);
160 gst_caps_replace (&self->probed_srccaps, NULL);
161 gst_caps_replace (&self->probed_sinkcaps, NULL);
167 gst_v4l2_transform_close (GstV4l2Transform * self)
169 GST_DEBUG_OBJECT (self, "Closing");
171 gst_v4l2_object_close (self->v4l2output);
172 gst_v4l2_object_close (self->v4l2capture);
174 gst_caps_replace (&self->probed_srccaps, NULL);
175 gst_caps_replace (&self->probed_sinkcaps, NULL);
179 gst_v4l2_transform_stop (GstBaseTransform * trans)
181 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
183 GST_DEBUG_OBJECT (self, "Stop");
185 gst_v4l2_object_stop (self->v4l2output);
186 gst_v4l2_object_stop (self->v4l2capture);
187 gst_caps_replace (&self->incaps, NULL);
188 gst_caps_replace (&self->outcaps, NULL);
194 gst_v4l2_transform_set_caps (GstBaseTransform * trans, GstCaps * incaps,
197 GstV4l2Error error = GST_V4L2_ERROR_INIT;
198 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
200 if (self->disable_passthrough)
201 gst_base_transform_set_passthrough (trans, FALSE);
203 if (self->incaps && self->outcaps) {
204 if (gst_caps_is_equal (incaps, self->incaps) &&
205 gst_caps_is_equal (outcaps, self->outcaps)) {
206 GST_DEBUG_OBJECT (trans, "Caps did not changed");
211 /* TODO Add renegotiation support */
212 g_return_val_if_fail (!GST_V4L2_IS_ACTIVE (self->v4l2output), FALSE);
213 g_return_val_if_fail (!GST_V4L2_IS_ACTIVE (self->v4l2capture), FALSE);
215 gst_caps_replace (&self->incaps, incaps);
216 gst_caps_replace (&self->outcaps, outcaps);
218 if (!gst_v4l2_object_set_format (self->v4l2output, incaps, &error))
221 if (!gst_v4l2_object_set_format (self->v4l2capture, outcaps, &error))
224 /* FIXME implement fallback if crop not supported */
225 if (!gst_v4l2_object_set_crop (self->v4l2output))
228 if (!gst_v4l2_object_set_crop (self->v4l2capture))
235 GST_ERROR_OBJECT (self, "failed to set input caps: %" GST_PTR_FORMAT,
237 gst_v4l2_error (self, &error);
242 gst_v4l2_object_stop (self->v4l2output);
243 GST_ERROR_OBJECT (self, "failed to set output caps: %" GST_PTR_FORMAT,
245 gst_v4l2_error (self, &error);
253 gst_v4l2_transform_query (GstBaseTransform * trans, GstPadDirection direction,
256 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
259 switch (GST_QUERY_TYPE (query)) {
260 case GST_QUERY_CAPS:{
261 GstCaps *filter, *caps = NULL, *result = NULL;
262 GstPad *pad, *otherpad;
264 gst_query_parse_caps (query, &filter);
266 if (direction == GST_PAD_SRC) {
267 pad = GST_BASE_TRANSFORM_SRC_PAD (trans);
268 otherpad = GST_BASE_TRANSFORM_SINK_PAD (trans);
269 if (self->probed_srccaps)
270 caps = gst_caps_ref (self->probed_srccaps);
272 pad = GST_BASE_TRANSFORM_SINK_PAD (trans);
273 otherpad = GST_BASE_TRANSFORM_SRC_PAD (trans);
274 if (self->probed_sinkcaps)
275 caps = gst_caps_ref (self->probed_sinkcaps);
279 caps = gst_pad_get_pad_template_caps (pad);
283 caps = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
284 gst_caps_unref (tmp);
287 result = gst_pad_peer_query_caps (otherpad, caps);
288 result = gst_caps_make_writable (result);
289 gst_caps_append (result, caps);
291 GST_DEBUG_OBJECT (self, "Returning %s caps %" GST_PTR_FORMAT,
292 GST_PAD_NAME (pad), result);
294 gst_query_set_caps_result (query, result);
295 gst_caps_unref (result);
300 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
309 gst_v4l2_transform_decide_allocation (GstBaseTransform * trans,
312 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
313 gboolean ret = FALSE;
315 GST_DEBUG_OBJECT (self, "called");
317 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query)) {
318 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2capture->pool);
320 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
323 if (!gst_buffer_pool_set_active (pool, TRUE))
324 goto activate_failed;
330 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
331 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
336 gst_v4l2_transform_propose_allocation (GstBaseTransform * trans,
337 GstQuery * decide_query, GstQuery * query)
339 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
340 gboolean ret = FALSE;
342 GST_DEBUG_OBJECT (self, "called");
344 if (decide_query == NULL)
347 ret = gst_v4l2_object_propose_allocation (self->v4l2output, query);
350 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
351 decide_query, query);
356 /* copies the given caps */
358 gst_v4l2_transform_caps_remove_format_info (GstCaps * caps)
365 res = gst_caps_new_empty ();
367 n = gst_caps_get_size (caps);
368 for (i = 0; i < n; i++) {
369 st = gst_caps_get_structure (caps, i);
370 f = gst_caps_get_features (caps, i);
372 /* If this is already expressed by the existing caps
373 * skip this structure */
374 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
377 st = gst_structure_copy (st);
378 /* Only remove format info for the cases when we can actually convert */
379 if (!gst_caps_features_is_any (f)
380 && gst_caps_features_is_equal (f,
381 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
382 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
383 "width", "height", "pixel-aspect-ratio", NULL);
385 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
391 /* The caps can be transformed into any other caps with format info removed.
392 * However, we should prefer passthrough, so if passthrough is possible,
393 * put it first in the list. */
395 gst_v4l2_transform_transform_caps (GstBaseTransform * btrans,
396 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
401 /* Get all possible caps that we can transform to */
402 tmp = gst_v4l2_transform_caps_remove_format_info (caps);
405 tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
406 gst_caps_unref (tmp);
412 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
413 GST_PTR_FORMAT, caps, result);
419 gst_v4l2_transform_fixate_caps (GstBaseTransform * trans,
420 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
422 GstStructure *ins, *outs;
423 const GValue *from_par, *to_par;
424 GValue fpar = { 0, }, tpar = {
427 othercaps = gst_caps_truncate (othercaps);
428 othercaps = gst_caps_make_writable (othercaps);
430 GST_DEBUG_OBJECT (trans, "trying to fixate othercaps %" GST_PTR_FORMAT
431 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
433 ins = gst_caps_get_structure (caps, 0);
434 outs = gst_caps_get_structure (othercaps, 0);
437 const gchar *in_format;
439 in_format = gst_structure_get_string (ins, "format");
441 /* Try to set output format for pass through */
442 gst_structure_fixate_field_string (outs, "format", in_format);
447 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
448 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
450 /* If we're fixating from the sinkpad we always set the PAR and
451 * assume that missing PAR on the sinkpad means 1/1 and
452 * missing PAR on the srcpad means undefined
454 if (direction == GST_PAD_SINK) {
456 g_value_init (&fpar, GST_TYPE_FRACTION);
457 gst_value_set_fraction (&fpar, 1, 1);
461 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
462 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
467 g_value_init (&tpar, GST_TYPE_FRACTION);
468 gst_value_set_fraction (&tpar, 1, 1);
471 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
475 g_value_init (&fpar, GST_TYPE_FRACTION);
476 gst_value_set_fraction (&fpar, 1, 1);
481 /* we have both PAR but they might not be fixated */
483 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
485 gint from_dar_n, from_dar_d;
488 /* from_par should be fixed */
489 g_return_val_if_fail (gst_value_is_fixed (from_par), othercaps);
491 from_par_n = gst_value_get_fraction_numerator (from_par);
492 from_par_d = gst_value_get_fraction_denominator (from_par);
494 gst_structure_get_int (ins, "width", &from_w);
495 gst_structure_get_int (ins, "height", &from_h);
497 gst_structure_get_int (outs, "width", &w);
498 gst_structure_get_int (outs, "height", &h);
500 /* if both width and height are already fixed, we can't do anything
501 * about it anymore */
505 GST_DEBUG_OBJECT (trans, "dimensions already set to %dx%d, not fixating",
507 if (!gst_value_is_fixed (to_par)) {
508 if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
509 from_par_n, from_par_d, w, h)) {
510 GST_DEBUG_OBJECT (trans, "fixating to_par to %dx%d", n, d);
511 if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
512 gst_structure_fixate_field_nearest_fraction (outs,
513 "pixel-aspect-ratio", n, d);
515 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
522 /* Calculate input DAR */
523 if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
524 &from_dar_n, &from_dar_d)) {
525 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
526 ("Error calculating the output scaled size - integer overflow"));
530 GST_DEBUG_OBJECT (trans, "Input DAR is %d/%d", from_dar_n, from_dar_d);
532 /* If either width or height are fixed there's not much we
533 * can do either except choosing a height or width and PAR
534 * that matches the DAR as good as possible
538 gint set_w, set_par_n, set_par_d;
540 GST_DEBUG_OBJECT (trans, "height is fixed (%d)", h);
542 /* If the PAR is fixed too, there's not much to do
543 * except choosing the width that is nearest to the
544 * width with the same DAR */
545 if (gst_value_is_fixed (to_par)) {
546 to_par_n = gst_value_get_fraction_numerator (to_par);
547 to_par_d = gst_value_get_fraction_denominator (to_par);
549 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
551 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
552 to_par_n, &num, &den)) {
553 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
554 ("Error calculating the output scaled size - integer overflow"));
558 w = (guint) gst_util_uint64_scale_int (h, num, den);
559 gst_structure_fixate_field_nearest_int (outs, "width", w);
564 /* The PAR is not fixed and it's quite likely that we can set
565 * an arbitrary PAR. */
567 /* Check if we can keep the input width */
568 tmp = gst_structure_copy (outs);
569 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
570 gst_structure_get_int (tmp, "width", &set_w);
572 /* Might have failed but try to keep the DAR nonetheless by
573 * adjusting the PAR */
574 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
575 &to_par_n, &to_par_d)) {
576 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
577 ("Error calculating the output scaled size - integer overflow"));
578 gst_structure_free (tmp);
582 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
583 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
584 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
586 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
588 gst_structure_free (tmp);
590 /* Check if the adjusted PAR is accepted */
591 if (set_par_n == to_par_n && set_par_d == to_par_d) {
592 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
593 set_par_n != set_par_d)
594 gst_structure_set (outs, "width", G_TYPE_INT, set_w,
595 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
600 /* Otherwise scale the width to the new PAR and check if the
601 * adjusted with is accepted. If all that fails we can't keep
603 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
604 set_par_n, &num, &den)) {
605 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
606 ("Error calculating the output scaled size - integer overflow"));
610 w = (guint) gst_util_uint64_scale_int (h, num, den);
611 gst_structure_fixate_field_nearest_int (outs, "width", w);
612 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
613 set_par_n != set_par_d)
614 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
615 set_par_n, set_par_d, NULL);
620 gint set_h, set_par_n, set_par_d;
622 GST_DEBUG_OBJECT (trans, "width is fixed (%d)", w);
624 /* If the PAR is fixed too, there's not much to do
625 * except choosing the height that is nearest to the
626 * height with the same DAR */
627 if (gst_value_is_fixed (to_par)) {
628 to_par_n = gst_value_get_fraction_numerator (to_par);
629 to_par_d = gst_value_get_fraction_denominator (to_par);
631 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
633 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
634 to_par_n, &num, &den)) {
635 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
636 ("Error calculating the output scaled size - integer overflow"));
640 h = (guint) gst_util_uint64_scale_int (w, den, num);
641 gst_structure_fixate_field_nearest_int (outs, "height", h);
646 /* The PAR is not fixed and it's quite likely that we can set
647 * an arbitrary PAR. */
649 /* Check if we can keep the input height */
650 tmp = gst_structure_copy (outs);
651 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
652 gst_structure_get_int (tmp, "height", &set_h);
654 /* Might have failed but try to keep the DAR nonetheless by
655 * adjusting the PAR */
656 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
657 &to_par_n, &to_par_d)) {
658 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
659 ("Error calculating the output scaled size - integer overflow"));
660 gst_structure_free (tmp);
663 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
664 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
665 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
667 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
669 gst_structure_free (tmp);
671 /* Check if the adjusted PAR is accepted */
672 if (set_par_n == to_par_n && set_par_d == to_par_d) {
673 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
674 set_par_n != set_par_d)
675 gst_structure_set (outs, "height", G_TYPE_INT, set_h,
676 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
681 /* Otherwise scale the height to the new PAR and check if the
682 * adjusted with is accepted. If all that fails we can't keep
684 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
685 set_par_n, &num, &den)) {
686 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
687 ("Error calculating the output scaled size - integer overflow"));
691 h = (guint) gst_util_uint64_scale_int (w, den, num);
692 gst_structure_fixate_field_nearest_int (outs, "height", h);
693 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
694 set_par_n != set_par_d)
695 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
696 set_par_n, set_par_d, NULL);
699 } else if (gst_value_is_fixed (to_par)) {
701 gint set_h, set_w, f_h, f_w;
703 to_par_n = gst_value_get_fraction_numerator (to_par);
704 to_par_d = gst_value_get_fraction_denominator (to_par);
706 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
708 /* Calculate scale factor for the PAR change */
709 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
710 to_par_n, &num, &den)) {
711 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
712 ("Error calculating the output scaled size - integer overflow"));
716 /* Try to keep the input height (because of interlacing) */
717 tmp = gst_structure_copy (outs);
718 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
719 gst_structure_get_int (tmp, "height", &set_h);
721 /* This might have failed but try to scale the width
722 * to keep the DAR nonetheless */
723 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
724 gst_structure_fixate_field_nearest_int (tmp, "width", w);
725 gst_structure_get_int (tmp, "width", &set_w);
726 gst_structure_free (tmp);
728 /* We kept the DAR and the height is nearest to the original height */
730 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
731 G_TYPE_INT, set_h, NULL);
738 /* If the former failed, try to keep the input width at least */
739 tmp = gst_structure_copy (outs);
740 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
741 gst_structure_get_int (tmp, "width", &set_w);
743 /* This might have failed but try to scale the width
744 * to keep the DAR nonetheless */
745 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
746 gst_structure_fixate_field_nearest_int (tmp, "height", h);
747 gst_structure_get_int (tmp, "height", &set_h);
748 gst_structure_free (tmp);
750 /* We kept the DAR and the width is nearest to the original width */
752 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
753 G_TYPE_INT, set_h, NULL);
757 /* If all this failed, keep the height that was nearest to the orignal
758 * height and the nearest possible width. This changes the DAR but
759 * there's not much else to do here.
761 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
766 gint set_h, set_w, set_par_n, set_par_d, tmp2;
768 /* width, height and PAR are not fixed but passthrough is not possible */
770 /* First try to keep the height and width as good as possible
772 tmp = gst_structure_copy (outs);
773 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
774 gst_structure_get_int (tmp, "height", &set_h);
775 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
776 gst_structure_get_int (tmp, "width", &set_w);
778 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
779 &to_par_n, &to_par_d)) {
780 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
781 ("Error calculating the output scaled size - integer overflow"));
782 gst_structure_free (tmp);
786 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
787 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
788 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
790 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
792 gst_structure_free (tmp);
794 if (set_par_n == to_par_n && set_par_d == to_par_d) {
795 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
796 G_TYPE_INT, set_h, NULL);
798 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
799 set_par_n != set_par_d)
800 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
801 set_par_n, set_par_d, NULL);
805 /* Otherwise try to scale width to keep the DAR with the set
807 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
808 set_par_n, &num, &den)) {
809 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
810 ("Error calculating the output scaled size - integer overflow"));
814 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
815 tmp = gst_structure_copy (outs);
816 gst_structure_fixate_field_nearest_int (tmp, "width", w);
817 gst_structure_get_int (tmp, "width", &tmp2);
818 gst_structure_free (tmp);
821 gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
822 G_TYPE_INT, set_h, NULL);
823 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
824 set_par_n != set_par_d)
825 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
826 set_par_n, set_par_d, NULL);
830 /* ... or try the same with the height */
831 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
832 tmp = gst_structure_copy (outs);
833 gst_structure_fixate_field_nearest_int (tmp, "height", h);
834 gst_structure_get_int (tmp, "height", &tmp2);
835 gst_structure_free (tmp);
838 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
839 G_TYPE_INT, tmp2, NULL);
840 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
841 set_par_n != set_par_d)
842 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
843 set_par_n, set_par_d, NULL);
847 /* If all fails we can't keep the DAR and take the nearest values
848 * for everything from the first try */
849 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
850 G_TYPE_INT, set_h, NULL);
851 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
852 set_par_n != set_par_d)
853 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
854 set_par_n, set_par_d, NULL);
859 GST_DEBUG_OBJECT (trans, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
861 if (from_par == &fpar)
862 g_value_unset (&fpar);
864 g_value_unset (&tpar);
866 /* fixate remaining fields */
867 othercaps = gst_caps_fixate (othercaps);
869 if (direction == GST_PAD_SINK) {
870 if (gst_caps_is_subset (caps, othercaps)) {
871 gst_caps_replace (&othercaps, caps);
879 gst_v4l2_transform_prepare_output_buffer (GstBaseTransform * trans,
880 GstBuffer * inbuf, GstBuffer ** outbuf)
882 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
883 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
884 GstFlowReturn ret = GST_FLOW_OK;
885 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_CLASS (parent_class);
887 if (gst_base_transform_is_passthrough (trans)) {
888 GST_DEBUG_OBJECT (self, "Passthrough, no need to do anything");
893 /* Ensure input internal pool is active */
894 if (!gst_buffer_pool_is_active (pool)) {
895 GstStructure *config = gst_buffer_pool_get_config (pool);
896 gint min = self->v4l2output->min_buffers == 0 ? GST_V4L2_MIN_BUFFERS :
897 self->v4l2output->min_buffers;
898 gst_buffer_pool_config_set_params (config, self->incaps,
899 self->v4l2output->info.size, min, min);
901 /* There is no reason to refuse this config */
902 if (!gst_buffer_pool_set_config (pool, config))
903 goto activate_failed;
905 if (!gst_buffer_pool_set_active (pool, TRUE))
906 goto activate_failed;
909 GST_DEBUG_OBJECT (self, "Queue input buffer");
910 ret = gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool), &inbuf);
911 if (G_UNLIKELY (ret != GST_FLOW_OK))
915 pool = gst_base_transform_get_buffer_pool (trans);
917 if (!gst_buffer_pool_set_active (pool, TRUE))
918 goto activate_failed;
920 GST_DEBUG_OBJECT (self, "Dequeue output buffer");
921 ret = gst_buffer_pool_acquire_buffer (pool, outbuf, NULL);
922 g_object_unref (pool);
924 if (ret != GST_FLOW_OK)
927 pool = self->v4l2capture->pool;
928 ret = gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool), outbuf);
930 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
932 if (ret != GST_FLOW_OK) {
933 gst_buffer_unref (*outbuf);
937 if (bclass->copy_metadata)
938 if (!bclass->copy_metadata (trans, inbuf, *outbuf)) {
939 /* something failed, post a warning */
940 GST_ELEMENT_WARNING (self, STREAM, NOT_IMPLEMENTED,
941 ("could not copy metadata"), (NULL));
948 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
949 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
950 g_object_unref (pool);
951 return GST_FLOW_ERROR;
954 GST_DEBUG_OBJECT (self, "could not allocate buffer from pool");
959 gst_v4l2_transform_transform (GstBaseTransform * trans, GstBuffer * inbuf,
967 gst_v4l2_transform_sink_event (GstBaseTransform * trans, GstEvent * event)
969 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
971 GstEventType type = GST_EVENT_TYPE (event);
973 /* Nothing to flush in passthrough */
974 if (gst_base_transform_is_passthrough (trans))
975 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
978 case GST_EVENT_FLUSH_START:
979 GST_DEBUG_OBJECT (self, "flush start");
980 gst_v4l2_object_unlock (self->v4l2output);
981 gst_v4l2_object_unlock (self->v4l2capture);
987 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
990 case GST_EVENT_FLUSH_STOP:
991 /* Buffer should be back now */
992 GST_DEBUG_OBJECT (self, "flush stop");
993 gst_v4l2_object_unlock_stop (self->v4l2capture);
994 gst_v4l2_object_unlock_stop (self->v4l2output);
995 if (self->v4l2output->pool)
996 gst_v4l2_buffer_pool_flush (self->v4l2output->pool);
997 if (self->v4l2capture->pool)
998 gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);
1007 static GstStateChangeReturn
1008 gst_v4l2_transform_change_state (GstElement * element,
1009 GstStateChange transition)
1011 GstV4l2Transform *self = GST_V4L2_TRANSFORM (element);
1012 GstStateChangeReturn ret;
1014 switch (transition) {
1015 case GST_STATE_CHANGE_NULL_TO_READY:
1016 if (!gst_v4l2_transform_open (self))
1017 return GST_STATE_CHANGE_FAILURE;
1019 case GST_STATE_CHANGE_PAUSED_TO_READY:
1020 gst_v4l2_object_unlock (self->v4l2output);
1021 gst_v4l2_object_unlock (self->v4l2capture);
1027 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1029 switch (transition) {
1030 case GST_STATE_CHANGE_READY_TO_NULL:
1031 gst_v4l2_transform_close (self);
1041 gst_v4l2_transform_dispose (GObject * object)
1043 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
1045 gst_caps_replace (&self->probed_sinkcaps, NULL);
1046 gst_caps_replace (&self->probed_srccaps, NULL);
1048 G_OBJECT_CLASS (parent_class)->dispose (object);
1052 gst_v4l2_transform_finalize (GObject * object)
1054 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
1056 gst_v4l2_object_destroy (self->v4l2capture);
1057 gst_v4l2_object_destroy (self->v4l2output);
1059 G_OBJECT_CLASS (parent_class)->finalize (object);
1063 gst_v4l2_transform_init (GstV4l2Transform * self)
1065 /* V4L2 object are created in subinstance_init */
1067 gst_base_transform_set_qos_enabled (GST_BASE_TRANSFORM (self), TRUE);
1071 gst_v4l2_transform_subinstance_init (GTypeInstance * instance, gpointer g_class)
1073 GstV4l2TransformClass *klass = GST_V4L2_TRANSFORM_CLASS (g_class);
1074 GstV4l2Transform *self = GST_V4L2_TRANSFORM (instance);
1076 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
1077 GST_OBJECT (GST_BASE_TRANSFORM_SINK_PAD (self)),
1078 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1079 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
1080 self->v4l2output->no_initial_format = TRUE;
1081 self->v4l2output->keep_aspect = FALSE;
1083 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
1084 GST_OBJECT (GST_BASE_TRANSFORM_SRC_PAD (self)),
1085 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1086 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
1090 gst_v4l2_transform_class_init (GstV4l2TransformClass * klass)
1092 GstElementClass *element_class;
1093 GObjectClass *gobject_class;
1094 GstBaseTransformClass *base_transform_class;
1096 element_class = (GstElementClass *) klass;
1097 gobject_class = (GObjectClass *) klass;
1098 base_transform_class = (GstBaseTransformClass *) klass;
1100 GST_DEBUG_CATEGORY_INIT (gst_v4l2_transform_debug, "v4l2transform", 0,
1103 gst_element_class_set_static_metadata (element_class,
1104 "V4L2 Video Converter",
1105 "Filter/Converter/Video/Scaler",
1106 "Transform streams via V4L2 API",
1107 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1109 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_transform_dispose);
1110 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_transform_finalize);
1111 gobject_class->set_property =
1112 GST_DEBUG_FUNCPTR (gst_v4l2_transform_set_property);
1113 gobject_class->get_property =
1114 GST_DEBUG_FUNCPTR (gst_v4l2_transform_get_property);
1116 base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_transform_stop);
1117 base_transform_class->set_caps =
1118 GST_DEBUG_FUNCPTR (gst_v4l2_transform_set_caps);
1119 base_transform_class->query = GST_DEBUG_FUNCPTR (gst_v4l2_transform_query);
1120 base_transform_class->sink_event =
1121 GST_DEBUG_FUNCPTR (gst_v4l2_transform_sink_event);
1122 base_transform_class->decide_allocation =
1123 GST_DEBUG_FUNCPTR (gst_v4l2_transform_decide_allocation);
1124 base_transform_class->propose_allocation =
1125 GST_DEBUG_FUNCPTR (gst_v4l2_transform_propose_allocation);
1126 base_transform_class->transform_caps =
1127 GST_DEBUG_FUNCPTR (gst_v4l2_transform_transform_caps);
1128 base_transform_class->fixate_caps =
1129 GST_DEBUG_FUNCPTR (gst_v4l2_transform_fixate_caps);
1130 base_transform_class->prepare_output_buffer =
1131 GST_DEBUG_FUNCPTR (gst_v4l2_transform_prepare_output_buffer);
1132 base_transform_class->transform =
1133 GST_DEBUG_FUNCPTR (gst_v4l2_transform_transform);
1135 base_transform_class->passthrough_on_same_caps = TRUE;
1137 element_class->change_state =
1138 GST_DEBUG_FUNCPTR (gst_v4l2_transform_change_state);
1140 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
1142 g_object_class_install_property (gobject_class, PROP_DISABLE_PASSTHROUGH,
1143 g_param_spec_boolean ("disable-passthrough", "Disable Passthrough",
1144 "Forces passing buffers through the converter", FALSE,
1145 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1149 gst_v4l2_transform_subclass_init (gpointer g_class, gpointer data)
1151 GstV4l2TransformClass *klass = GST_V4L2_TRANSFORM_CLASS (g_class);
1152 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1153 GstV4l2TransformCData *cdata = data;
1155 klass->default_device = cdata->device;
1157 /* Note: gst_pad_template_new() take the floating ref from the caps */
1158 gst_element_class_add_pad_template (element_class,
1159 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1161 gst_element_class_add_pad_template (element_class,
1162 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1165 gst_caps_unref (cdata->sink_caps);
1166 gst_caps_unref (cdata->src_caps);
1170 /* Probing functions */
1172 gst_v4l2_is_transform (GstCaps * sink_caps, GstCaps * src_caps)
1174 gboolean ret = FALSE;
1176 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_raw_caps ())
1177 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1184 gst_v4l2_transform_register (GstPlugin * plugin, const gchar * basename,
1185 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
1187 GTypeQuery type_query;
1188 GTypeInfo type_info = { 0, };
1189 GType type, subtype;
1191 GstV4l2TransformCData *cdata;
1193 cdata = g_new0 (GstV4l2TransformCData, 1);
1194 cdata->device = g_strdup (device_path);
1195 cdata->sink_caps = gst_caps_ref (sink_caps);
1196 cdata->src_caps = gst_caps_ref (src_caps);
1198 type = gst_v4l2_transform_get_type ();
1199 g_type_query (type, &type_query);
1200 memset (&type_info, 0, sizeof (type_info));
1201 type_info.class_size = type_query.class_size;
1202 type_info.instance_size = type_query.instance_size;
1203 type_info.class_init = gst_v4l2_transform_subclass_init;
1204 type_info.class_data = cdata;
1205 type_info.instance_init = gst_v4l2_transform_subinstance_init;
1207 if (g_type_from_name ("v4l2convert") != 0)
1208 type_name = g_strdup_printf ("v4l2%sconvert", basename);
1210 type_name = g_strdup ("v4l2convert");
1211 subtype = g_type_register_static (type, type_name, &type_info, 0);
1213 if (!gst_element_register (plugin, type_name, GST_RANK_NONE, subtype))
1214 GST_WARNING ("Failed to register plugin '%s'", type_name);