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 <glib/gi18n-lib.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 GstV4l2Error error = GST_V4L2_ERROR_INIT;
120 GST_DEBUG_OBJECT (self, "Opening");
122 if (!gst_v4l2_object_open (self->v4l2output, &error))
125 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
128 self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
129 gst_v4l2_object_get_raw_caps ());
131 if (gst_caps_is_empty (self->probed_sinkcaps))
132 goto no_input_format;
134 self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
135 gst_v4l2_object_get_raw_caps ());
137 if (gst_caps_is_empty (self->probed_srccaps))
138 goto no_output_format;
143 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
144 (_("Converter on device %s has no supported input format"),
145 self->v4l2output->videodev), (NULL));
150 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
151 (_("Converter on device %s has no supported output format"),
152 self->v4l2output->videodev), (NULL));
156 if (GST_V4L2_IS_OPEN (self->v4l2output))
157 gst_v4l2_object_close (self->v4l2output);
159 if (GST_V4L2_IS_OPEN (self->v4l2capture))
160 gst_v4l2_object_close (self->v4l2capture);
162 gst_caps_replace (&self->probed_srccaps, NULL);
163 gst_caps_replace (&self->probed_sinkcaps, NULL);
165 gst_v4l2_error (self, &error);
171 gst_v4l2_transform_close (GstV4l2Transform * self)
173 GST_DEBUG_OBJECT (self, "Closing");
175 gst_v4l2_object_close (self->v4l2output);
176 gst_v4l2_object_close (self->v4l2capture);
178 gst_caps_replace (&self->probed_srccaps, NULL);
179 gst_caps_replace (&self->probed_sinkcaps, NULL);
183 gst_v4l2_transform_stop (GstBaseTransform * trans)
185 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
187 GST_DEBUG_OBJECT (self, "Stop");
189 gst_v4l2_object_stop (self->v4l2output);
190 gst_v4l2_object_stop (self->v4l2capture);
191 gst_caps_replace (&self->incaps, NULL);
192 gst_caps_replace (&self->outcaps, NULL);
198 gst_v4l2_transform_set_caps (GstBaseTransform * trans, GstCaps * incaps,
201 GstV4l2Error error = GST_V4L2_ERROR_INIT;
202 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
204 if (self->disable_passthrough)
205 gst_base_transform_set_passthrough (trans, FALSE);
207 if (self->incaps && self->outcaps) {
208 if (gst_caps_is_equal (incaps, self->incaps) &&
209 gst_caps_is_equal (outcaps, self->outcaps)) {
210 GST_DEBUG_OBJECT (trans, "Caps did not changed");
215 gst_v4l2_object_stop (self->v4l2output);
216 gst_v4l2_object_stop (self->v4l2capture);
218 if (!gst_v4l2_object_set_format (self->v4l2output, incaps, &error))
221 if (!gst_v4l2_object_set_format (self->v4l2capture, outcaps, &error))
224 gst_caps_replace (&self->incaps, incaps);
225 gst_caps_replace (&self->outcaps, outcaps);
227 /* FIXME implement fallback if crop not supported */
228 if (!gst_v4l2_object_setup_padding (self->v4l2output))
231 if (!gst_v4l2_object_setup_padding (self->v4l2capture))
238 GST_ERROR_OBJECT (self, "failed to set input caps: %" GST_PTR_FORMAT,
240 gst_v4l2_error (self, &error);
245 gst_v4l2_object_stop (self->v4l2output);
246 GST_ERROR_OBJECT (self, "failed to set output caps: %" GST_PTR_FORMAT,
248 gst_v4l2_error (self, &error);
256 gst_v4l2_transform_query (GstBaseTransform * trans, GstPadDirection direction,
259 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
262 switch (GST_QUERY_TYPE (query)) {
263 case GST_QUERY_CAPS:{
264 GstCaps *filter, *caps = NULL, *result = NULL;
265 GstPad *pad, *otherpad;
267 gst_query_parse_caps (query, &filter);
269 if (direction == GST_PAD_SRC) {
270 pad = GST_BASE_TRANSFORM_SRC_PAD (trans);
271 otherpad = GST_BASE_TRANSFORM_SINK_PAD (trans);
272 if (self->probed_srccaps)
273 caps = gst_caps_ref (self->probed_srccaps);
275 pad = GST_BASE_TRANSFORM_SINK_PAD (trans);
276 otherpad = GST_BASE_TRANSFORM_SRC_PAD (trans);
277 if (self->probed_sinkcaps)
278 caps = gst_caps_ref (self->probed_sinkcaps);
282 caps = gst_pad_get_pad_template_caps (pad);
286 caps = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
287 gst_caps_unref (tmp);
290 result = gst_pad_peer_query_caps (otherpad, caps);
291 result = gst_caps_make_writable (result);
292 gst_caps_append (result, caps);
294 GST_DEBUG_OBJECT (self, "Returning %s caps %" GST_PTR_FORMAT,
295 GST_PAD_NAME (pad), result);
297 gst_query_set_caps_result (query, result);
298 gst_caps_unref (result);
303 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
312 gst_v4l2_transform_decide_allocation (GstBaseTransform * trans,
315 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
316 gboolean ret = FALSE;
318 GST_DEBUG_OBJECT (self, "called");
320 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query)) {
321 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2capture->pool);
323 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
326 if (!gst_buffer_pool_set_active (pool, TRUE))
327 goto activate_failed;
333 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
334 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
339 gst_v4l2_transform_propose_allocation (GstBaseTransform * trans,
340 GstQuery * decide_query, GstQuery * query)
342 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
343 gboolean ret = FALSE;
345 GST_DEBUG_OBJECT (self, "called");
347 if (decide_query == NULL)
350 ret = gst_v4l2_object_propose_allocation (self->v4l2output, query);
353 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
354 decide_query, query);
359 /* copies the given caps */
361 gst_v4l2_transform_caps_remove_format_info (GstCaps * caps)
368 res = gst_caps_new_empty ();
370 n = gst_caps_get_size (caps);
371 for (i = 0; i < n; i++) {
372 st = gst_caps_get_structure (caps, i);
373 f = gst_caps_get_features (caps, i);
375 /* If this is already expressed by the existing caps
376 * skip this structure */
377 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
380 st = gst_structure_copy (st);
381 /* Only remove format info for the cases when we can actually convert */
382 if (!gst_caps_features_is_any (f)
383 && gst_caps_features_is_equal (f,
384 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
385 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
386 "width", "height", "pixel-aspect-ratio", NULL);
388 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
394 /* The caps can be transformed into any other caps with format info removed.
395 * However, we should prefer passthrough, so if passthrough is possible,
396 * put it first in the list. */
398 gst_v4l2_transform_transform_caps (GstBaseTransform * btrans,
399 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
404 /* Get all possible caps that we can transform to */
405 tmp = gst_v4l2_transform_caps_remove_format_info (caps);
408 tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
409 gst_caps_unref (tmp);
415 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
416 GST_PTR_FORMAT, caps, result);
422 gst_v4l2_transform_fixate_caps (GstBaseTransform * trans,
423 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
425 GstStructure *ins, *outs;
426 const GValue *from_par, *to_par;
427 GValue fpar = { 0, }, tpar = {
430 othercaps = gst_caps_truncate (othercaps);
431 othercaps = gst_caps_make_writable (othercaps);
433 GST_DEBUG_OBJECT (trans, "trying to fixate othercaps %" GST_PTR_FORMAT
434 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
436 ins = gst_caps_get_structure (caps, 0);
437 outs = gst_caps_get_structure (othercaps, 0);
440 const gchar *in_format;
442 in_format = gst_structure_get_string (ins, "format");
444 /* Try to set output format for pass through */
445 gst_structure_fixate_field_string (outs, "format", in_format);
450 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
451 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
453 /* If we're fixating from the sinkpad we always set the PAR and
454 * assume that missing PAR on the sinkpad means 1/1 and
455 * missing PAR on the srcpad means undefined
457 if (direction == GST_PAD_SINK) {
459 g_value_init (&fpar, GST_TYPE_FRACTION);
460 gst_value_set_fraction (&fpar, 1, 1);
464 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
465 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
470 g_value_init (&tpar, GST_TYPE_FRACTION);
471 gst_value_set_fraction (&tpar, 1, 1);
474 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
478 g_value_init (&fpar, GST_TYPE_FRACTION);
479 gst_value_set_fraction (&fpar, 1, 1);
484 /* we have both PAR but they might not be fixated */
486 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
488 gint from_dar_n, from_dar_d;
491 /* from_par should be fixed */
492 g_return_val_if_fail (gst_value_is_fixed (from_par), othercaps);
494 from_par_n = gst_value_get_fraction_numerator (from_par);
495 from_par_d = gst_value_get_fraction_denominator (from_par);
497 gst_structure_get_int (ins, "width", &from_w);
498 gst_structure_get_int (ins, "height", &from_h);
500 gst_structure_get_int (outs, "width", &w);
501 gst_structure_get_int (outs, "height", &h);
503 /* if both width and height are already fixed, we can't do anything
504 * about it anymore */
508 GST_DEBUG_OBJECT (trans, "dimensions already set to %dx%d, not fixating",
510 if (!gst_value_is_fixed (to_par)) {
511 if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
512 from_par_n, from_par_d, w, h)) {
513 GST_DEBUG_OBJECT (trans, "fixating to_par to %dx%d", n, d);
514 if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
515 gst_structure_fixate_field_nearest_fraction (outs,
516 "pixel-aspect-ratio", n, d);
518 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
525 /* Calculate input DAR */
526 if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
527 &from_dar_n, &from_dar_d)) {
528 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
529 ("Error calculating the output scaled size - integer overflow"));
533 GST_DEBUG_OBJECT (trans, "Input DAR is %d/%d", from_dar_n, from_dar_d);
535 /* If either width or height are fixed there's not much we
536 * can do either except choosing a height or width and PAR
537 * that matches the DAR as good as possible
541 gint set_w, set_par_n, set_par_d;
543 GST_DEBUG_OBJECT (trans, "height is fixed (%d)", h);
545 /* If the PAR is fixed too, there's not much to do
546 * except choosing the width that is nearest to the
547 * width with the same DAR */
548 if (gst_value_is_fixed (to_par)) {
549 to_par_n = gst_value_get_fraction_numerator (to_par);
550 to_par_d = gst_value_get_fraction_denominator (to_par);
552 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
554 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
555 to_par_n, &num, &den)) {
556 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
557 ("Error calculating the output scaled size - integer overflow"));
561 w = (guint) gst_util_uint64_scale_int (h, num, den);
562 gst_structure_fixate_field_nearest_int (outs, "width", w);
567 /* The PAR is not fixed and it's quite likely that we can set
568 * an arbitrary PAR. */
570 /* Check if we can keep the input width */
571 tmp = gst_structure_copy (outs);
572 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
573 gst_structure_get_int (tmp, "width", &set_w);
575 /* Might have failed but try to keep the DAR nonetheless by
576 * adjusting the PAR */
577 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
578 &to_par_n, &to_par_d)) {
579 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
580 ("Error calculating the output scaled size - integer overflow"));
581 gst_structure_free (tmp);
585 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
586 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
587 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
589 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
591 gst_structure_free (tmp);
593 /* Check if the adjusted PAR is accepted */
594 if (set_par_n == to_par_n && set_par_d == to_par_d) {
595 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
596 set_par_n != set_par_d)
597 gst_structure_set (outs, "width", G_TYPE_INT, set_w,
598 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
603 /* Otherwise scale the width to the new PAR and check if the
604 * adjusted with is accepted. If all that fails we can't keep
606 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
607 set_par_n, &num, &den)) {
608 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
609 ("Error calculating the output scaled size - integer overflow"));
613 w = (guint) gst_util_uint64_scale_int (h, num, den);
614 gst_structure_fixate_field_nearest_int (outs, "width", w);
615 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
616 set_par_n != set_par_d)
617 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
618 set_par_n, set_par_d, NULL);
623 gint set_h, set_par_n, set_par_d;
625 GST_DEBUG_OBJECT (trans, "width is fixed (%d)", w);
627 /* If the PAR is fixed too, there's not much to do
628 * except choosing the height that is nearest to the
629 * height with the same DAR */
630 if (gst_value_is_fixed (to_par)) {
631 to_par_n = gst_value_get_fraction_numerator (to_par);
632 to_par_d = gst_value_get_fraction_denominator (to_par);
634 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
636 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
637 to_par_n, &num, &den)) {
638 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
639 ("Error calculating the output scaled size - integer overflow"));
643 h = (guint) gst_util_uint64_scale_int (w, den, num);
644 gst_structure_fixate_field_nearest_int (outs, "height", h);
649 /* The PAR is not fixed and it's quite likely that we can set
650 * an arbitrary PAR. */
652 /* Check if we can keep the input height */
653 tmp = gst_structure_copy (outs);
654 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
655 gst_structure_get_int (tmp, "height", &set_h);
657 /* Might have failed but try to keep the DAR nonetheless by
658 * adjusting the PAR */
659 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
660 &to_par_n, &to_par_d)) {
661 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
662 ("Error calculating the output scaled size - integer overflow"));
663 gst_structure_free (tmp);
666 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
667 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
668 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
670 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
672 gst_structure_free (tmp);
674 /* Check if the adjusted PAR is accepted */
675 if (set_par_n == to_par_n && set_par_d == to_par_d) {
676 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
677 set_par_n != set_par_d)
678 gst_structure_set (outs, "height", G_TYPE_INT, set_h,
679 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
684 /* Otherwise scale the height to the new PAR and check if the
685 * adjusted with is accepted. If all that fails we can't keep
687 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
688 set_par_n, &num, &den)) {
689 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
690 ("Error calculating the output scaled size - integer overflow"));
694 h = (guint) gst_util_uint64_scale_int (w, den, num);
695 gst_structure_fixate_field_nearest_int (outs, "height", h);
696 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
697 set_par_n != set_par_d)
698 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
699 set_par_n, set_par_d, NULL);
702 } else if (gst_value_is_fixed (to_par)) {
704 gint set_h, set_w, f_h, f_w;
706 to_par_n = gst_value_get_fraction_numerator (to_par);
707 to_par_d = gst_value_get_fraction_denominator (to_par);
709 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
711 /* Calculate scale factor for the PAR change */
712 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
713 to_par_n, &num, &den)) {
714 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
715 ("Error calculating the output scaled size - integer overflow"));
719 /* Try to keep the input height (because of interlacing) */
720 tmp = gst_structure_copy (outs);
721 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
722 gst_structure_get_int (tmp, "height", &set_h);
724 /* This might have failed but try to scale the width
725 * to keep the DAR nonetheless */
726 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
727 gst_structure_fixate_field_nearest_int (tmp, "width", w);
728 gst_structure_get_int (tmp, "width", &set_w);
729 gst_structure_free (tmp);
731 /* We kept the DAR and the height is nearest to the original height */
733 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
734 G_TYPE_INT, set_h, NULL);
741 /* If the former failed, try to keep the input width at least */
742 tmp = gst_structure_copy (outs);
743 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
744 gst_structure_get_int (tmp, "width", &set_w);
746 /* This might have failed but try to scale the width
747 * to keep the DAR nonetheless */
748 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
749 gst_structure_fixate_field_nearest_int (tmp, "height", h);
750 gst_structure_get_int (tmp, "height", &set_h);
751 gst_structure_free (tmp);
753 /* We kept the DAR and the width is nearest to the original width */
755 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
756 G_TYPE_INT, set_h, NULL);
760 /* If all this failed, keep the height that was nearest to the original
761 * height and the nearest possible width. This changes the DAR but
762 * there's not much else to do here.
764 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
769 gint set_h, set_w, set_par_n, set_par_d, tmp2;
771 /* width, height and PAR are not fixed but passthrough is not possible */
773 /* First try to keep the height and width as good as possible
775 tmp = gst_structure_copy (outs);
776 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
777 gst_structure_get_int (tmp, "height", &set_h);
778 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
779 gst_structure_get_int (tmp, "width", &set_w);
781 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
782 &to_par_n, &to_par_d)) {
783 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
784 ("Error calculating the output scaled size - integer overflow"));
785 gst_structure_free (tmp);
789 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
790 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
791 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
793 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
795 gst_structure_free (tmp);
797 if (set_par_n == to_par_n && set_par_d == to_par_d) {
798 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
799 G_TYPE_INT, set_h, NULL);
801 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
802 set_par_n != set_par_d)
803 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
804 set_par_n, set_par_d, NULL);
808 /* Otherwise try to scale width to keep the DAR with the set
810 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
811 set_par_n, &num, &den)) {
812 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
813 ("Error calculating the output scaled size - integer overflow"));
817 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
818 tmp = gst_structure_copy (outs);
819 gst_structure_fixate_field_nearest_int (tmp, "width", w);
820 gst_structure_get_int (tmp, "width", &tmp2);
821 gst_structure_free (tmp);
824 gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
825 G_TYPE_INT, set_h, NULL);
826 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
827 set_par_n != set_par_d)
828 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
829 set_par_n, set_par_d, NULL);
833 /* ... or try the same with the height */
834 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
835 tmp = gst_structure_copy (outs);
836 gst_structure_fixate_field_nearest_int (tmp, "height", h);
837 gst_structure_get_int (tmp, "height", &tmp2);
838 gst_structure_free (tmp);
841 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
842 G_TYPE_INT, tmp2, NULL);
843 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
844 set_par_n != set_par_d)
845 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
846 set_par_n, set_par_d, NULL);
850 /* If all fails we can't keep the DAR and take the nearest values
851 * for everything from the first try */
852 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
853 G_TYPE_INT, set_h, NULL);
854 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
855 set_par_n != set_par_d)
856 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
857 set_par_n, set_par_d, NULL);
862 GST_DEBUG_OBJECT (trans, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
864 if (from_par == &fpar)
865 g_value_unset (&fpar);
867 g_value_unset (&tpar);
869 /* fixate remaining fields */
870 othercaps = gst_caps_fixate (othercaps);
872 if (direction == GST_PAD_SINK) {
873 if (gst_caps_is_subset (caps, othercaps)) {
874 gst_caps_replace (&othercaps, caps);
882 gst_v4l2_transform_prepare_output_buffer (GstBaseTransform * trans,
883 GstBuffer * inbuf, GstBuffer ** outbuf)
885 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
886 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
887 GstFlowReturn ret = GST_FLOW_OK;
888 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_CLASS (parent_class);
890 if (gst_base_transform_is_passthrough (trans)) {
891 GST_DEBUG_OBJECT (self, "Passthrough, no need to do anything");
896 /* Ensure input internal pool is active */
897 if (!gst_buffer_pool_is_active (pool)) {
898 GstStructure *config = gst_buffer_pool_get_config (pool);
899 gint min = MAX (GST_V4L2_MIN_BUFFERS (self->v4l2output),
900 self->v4l2output->min_buffers);
902 if (self->v4l2output->mode == GST_V4L2_IO_USERPTR ||
903 self->v4l2output->mode == GST_V4L2_IO_DMABUF_IMPORT) {
904 if (!gst_v4l2_object_try_import (self->v4l2output, inbuf)) {
905 GST_ERROR_OBJECT (self, "cannot import buffers from upstream");
906 return GST_FLOW_ERROR;
909 if (self->v4l2output->need_video_meta) {
910 /* We may need video meta if imported buffer is using non-standard
912 gst_buffer_pool_config_add_option (config,
913 GST_BUFFER_POOL_OPTION_VIDEO_META);
917 gst_buffer_pool_config_set_params (config, self->incaps,
918 self->v4l2output->info.size, min, min);
920 /* There is no reason to refuse this config */
921 if (!gst_buffer_pool_set_config (pool, config))
922 goto activate_failed;
924 if (!gst_buffer_pool_set_active (pool, TRUE))
925 goto activate_failed;
928 GST_DEBUG_OBJECT (self, "Queue input buffer");
930 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool), &inbuf, NULL);
931 if (G_UNLIKELY (ret != GST_FLOW_OK))
935 pool = gst_base_transform_get_buffer_pool (trans);
937 if (!gst_buffer_pool_set_active (pool, TRUE))
938 goto activate_failed;
940 GST_DEBUG_OBJECT (self, "Dequeue output buffer");
941 ret = gst_buffer_pool_acquire_buffer (pool, outbuf, NULL);
942 g_object_unref (pool);
944 if (ret != GST_FLOW_OK)
947 pool = self->v4l2capture->pool;
949 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool), outbuf,
952 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
954 if (ret != GST_FLOW_OK) {
955 gst_buffer_unref (*outbuf);
959 if (bclass->copy_metadata)
960 if (!bclass->copy_metadata (trans, inbuf, *outbuf)) {
961 /* something failed, post a warning */
962 GST_ELEMENT_WARNING (self, STREAM, NOT_IMPLEMENTED,
963 ("could not copy metadata"), (NULL));
970 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
971 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
972 g_object_unref (pool);
973 return GST_FLOW_ERROR;
976 GST_DEBUG_OBJECT (self, "could not allocate buffer from pool");
981 gst_v4l2_transform_transform (GstBaseTransform * trans, GstBuffer * inbuf,
989 gst_v4l2_transform_sink_event (GstBaseTransform * trans, GstEvent * event)
991 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
993 GstEventType type = GST_EVENT_TYPE (event);
995 /* Nothing to flush in passthrough */
996 if (gst_base_transform_is_passthrough (trans))
997 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
1000 case GST_EVENT_FLUSH_START:
1001 GST_DEBUG_OBJECT (self, "flush start");
1002 gst_v4l2_object_unlock (self->v4l2output);
1003 gst_v4l2_object_unlock (self->v4l2capture);
1009 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
1012 case GST_EVENT_FLUSH_STOP:
1013 /* Buffer should be back now */
1014 GST_DEBUG_OBJECT (self, "flush stop");
1015 gst_v4l2_object_unlock_stop (self->v4l2capture);
1016 gst_v4l2_object_unlock_stop (self->v4l2output);
1017 if (self->v4l2output->pool)
1018 gst_v4l2_buffer_pool_flush (self->v4l2output->pool);
1019 if (self->v4l2capture->pool)
1020 gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);
1029 static GstStateChangeReturn
1030 gst_v4l2_transform_change_state (GstElement * element,
1031 GstStateChange transition)
1033 GstV4l2Transform *self = GST_V4L2_TRANSFORM (element);
1034 GstStateChangeReturn ret;
1036 switch (transition) {
1037 case GST_STATE_CHANGE_NULL_TO_READY:
1038 if (!gst_v4l2_transform_open (self))
1039 return GST_STATE_CHANGE_FAILURE;
1041 case GST_STATE_CHANGE_PAUSED_TO_READY:
1042 gst_v4l2_object_unlock (self->v4l2output);
1043 gst_v4l2_object_unlock (self->v4l2capture);
1049 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1051 switch (transition) {
1052 case GST_STATE_CHANGE_READY_TO_NULL:
1053 gst_v4l2_transform_close (self);
1063 gst_v4l2_transform_dispose (GObject * object)
1065 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
1067 gst_caps_replace (&self->probed_sinkcaps, NULL);
1068 gst_caps_replace (&self->probed_srccaps, NULL);
1070 G_OBJECT_CLASS (parent_class)->dispose (object);
1074 gst_v4l2_transform_finalize (GObject * object)
1076 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
1078 gst_v4l2_object_destroy (self->v4l2capture);
1079 gst_v4l2_object_destroy (self->v4l2output);
1081 G_OBJECT_CLASS (parent_class)->finalize (object);
1085 gst_v4l2_transform_init (GstV4l2Transform * self)
1087 /* V4L2 object are created in subinstance_init */
1089 gst_base_transform_set_qos_enabled (GST_BASE_TRANSFORM (self), TRUE);
1093 gst_v4l2_transform_subinstance_init (GTypeInstance * instance, gpointer g_class)
1095 GstV4l2TransformClass *klass = GST_V4L2_TRANSFORM_CLASS (g_class);
1096 GstV4l2Transform *self = GST_V4L2_TRANSFORM (instance);
1098 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
1099 GST_OBJECT (GST_BASE_TRANSFORM_SINK_PAD (self)),
1100 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1101 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
1102 self->v4l2output->no_initial_format = TRUE;
1103 self->v4l2output->keep_aspect = FALSE;
1105 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
1106 GST_OBJECT (GST_BASE_TRANSFORM_SRC_PAD (self)),
1107 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1108 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
1112 gst_v4l2_transform_class_init (GstV4l2TransformClass * klass)
1114 GstElementClass *element_class;
1115 GObjectClass *gobject_class;
1116 GstBaseTransformClass *base_transform_class;
1118 element_class = (GstElementClass *) klass;
1119 gobject_class = (GObjectClass *) klass;
1120 base_transform_class = (GstBaseTransformClass *) klass;
1122 GST_DEBUG_CATEGORY_INIT (gst_v4l2_transform_debug, "v4l2transform", 0,
1125 gst_element_class_set_static_metadata (element_class,
1126 "V4L2 Video Converter",
1127 "Filter/Converter/Video/Scaler",
1128 "Transform streams via V4L2 API",
1129 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1131 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_transform_dispose);
1132 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_transform_finalize);
1133 gobject_class->set_property =
1134 GST_DEBUG_FUNCPTR (gst_v4l2_transform_set_property);
1135 gobject_class->get_property =
1136 GST_DEBUG_FUNCPTR (gst_v4l2_transform_get_property);
1138 base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_transform_stop);
1139 base_transform_class->set_caps =
1140 GST_DEBUG_FUNCPTR (gst_v4l2_transform_set_caps);
1141 base_transform_class->query = GST_DEBUG_FUNCPTR (gst_v4l2_transform_query);
1142 base_transform_class->sink_event =
1143 GST_DEBUG_FUNCPTR (gst_v4l2_transform_sink_event);
1144 base_transform_class->decide_allocation =
1145 GST_DEBUG_FUNCPTR (gst_v4l2_transform_decide_allocation);
1146 base_transform_class->propose_allocation =
1147 GST_DEBUG_FUNCPTR (gst_v4l2_transform_propose_allocation);
1148 base_transform_class->transform_caps =
1149 GST_DEBUG_FUNCPTR (gst_v4l2_transform_transform_caps);
1150 base_transform_class->fixate_caps =
1151 GST_DEBUG_FUNCPTR (gst_v4l2_transform_fixate_caps);
1152 base_transform_class->prepare_output_buffer =
1153 GST_DEBUG_FUNCPTR (gst_v4l2_transform_prepare_output_buffer);
1154 base_transform_class->transform =
1155 GST_DEBUG_FUNCPTR (gst_v4l2_transform_transform);
1157 base_transform_class->passthrough_on_same_caps = TRUE;
1159 element_class->change_state =
1160 GST_DEBUG_FUNCPTR (gst_v4l2_transform_change_state);
1162 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
1164 g_object_class_install_property (gobject_class, PROP_DISABLE_PASSTHROUGH,
1165 g_param_spec_boolean ("disable-passthrough", "Disable Passthrough",
1166 "Forces passing buffers through the converter", FALSE,
1167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1171 gst_v4l2_transform_subclass_init (gpointer g_class, gpointer data)
1173 GstV4l2TransformClass *klass = GST_V4L2_TRANSFORM_CLASS (g_class);
1174 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1175 GstV4l2TransformCData *cdata = data;
1177 klass->default_device = cdata->device;
1179 gst_element_class_add_pad_template (element_class,
1180 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1182 gst_element_class_add_pad_template (element_class,
1183 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1186 gst_caps_unref (cdata->sink_caps);
1187 gst_caps_unref (cdata->src_caps);
1191 /* Probing functions */
1193 gst_v4l2_is_transform (GstCaps * sink_caps, GstCaps * src_caps)
1195 gboolean ret = FALSE;
1197 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_raw_caps ())
1198 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1205 gst_v4l2_transform_register (GstPlugin * plugin, const gchar * basename,
1206 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
1208 GTypeQuery type_query;
1209 GTypeInfo type_info = { 0, };
1210 GType type, subtype;
1212 GstV4l2TransformCData *cdata;
1214 cdata = g_new0 (GstV4l2TransformCData, 1);
1215 cdata->device = g_strdup (device_path);
1216 cdata->sink_caps = gst_caps_ref (sink_caps);
1217 cdata->src_caps = gst_caps_ref (src_caps);
1219 type = gst_v4l2_transform_get_type ();
1220 g_type_query (type, &type_query);
1221 memset (&type_info, 0, sizeof (type_info));
1222 type_info.class_size = type_query.class_size;
1223 type_info.instance_size = type_query.instance_size;
1224 type_info.class_init = gst_v4l2_transform_subclass_init;
1225 type_info.class_data = cdata;
1226 type_info.instance_init = gst_v4l2_transform_subinstance_init;
1228 if (g_type_from_name ("v4l2convert") != 0)
1229 type_name = g_strdup_printf ("v4l2%sconvert", basename);
1231 type_name = g_strdup ("v4l2convert");
1232 subtype = g_type_register_static (type, type_name, &type_info, 0);
1234 if (!gst_element_register (plugin, type_name, GST_RANK_NONE, subtype))
1235 GST_WARNING ("Failed to register plugin '%s'", type_name);