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
58 } GstV4l2TransformCData;
60 #define gst_v4l2_transform_parent_class parent_class
61 G_DEFINE_ABSTRACT_TYPE (GstV4l2Transform, gst_v4l2_transform,
62 GST_TYPE_BASE_TRANSFORM);
65 gst_v4l2_transform_set_property (GObject * object,
66 guint prop_id, const GValue * value, GParamSpec * pspec)
68 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
71 case PROP_OUTPUT_IO_MODE:
72 gst_v4l2_object_set_property_helper (self->v4l2output, prop_id, value,
75 case PROP_CAPTURE_IO_MODE:
76 gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, 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_OUTPUT_IO_MODE:
98 gst_v4l2_object_get_property_helper (self->v4l2output, prop_id, value,
101 case PROP_CAPTURE_IO_MODE:
102 gst_v4l2_object_get_property_helper (self->v4l2capture, prop_id, value,
106 /* By default read from output */
108 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
109 prop_id, value, pspec)) {
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 gst_v4l2_transform_open (GstV4l2Transform * self)
119 GST_DEBUG_OBJECT (self, "Opening");
121 if (!gst_v4l2_object_open (self->v4l2output))
124 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
127 self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
128 gst_v4l2_object_get_raw_caps ());
130 if (gst_caps_is_empty (self->probed_sinkcaps))
131 goto no_input_format;
133 self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
134 gst_v4l2_object_get_raw_caps ());
136 if (gst_caps_is_empty (self->probed_srccaps))
137 goto no_output_format;
142 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
143 (_("Converter on device %s has no supported input format"),
144 self->v4l2output->videodev), (NULL));
149 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
150 (_("Converter on device %s has no supported output format"),
151 self->v4l2output->videodev), (NULL));
155 if (GST_V4L2_IS_OPEN (self->v4l2output))
156 gst_v4l2_object_close (self->v4l2output);
158 if (GST_V4L2_IS_OPEN (self->v4l2capture))
159 gst_v4l2_object_close (self->v4l2capture);
161 gst_caps_replace (&self->probed_srccaps, NULL);
162 gst_caps_replace (&self->probed_sinkcaps, NULL);
168 gst_v4l2_transform_close (GstV4l2Transform * self)
170 GST_DEBUG_OBJECT (self, "Closing");
172 gst_v4l2_object_close (self->v4l2output);
173 gst_v4l2_object_close (self->v4l2capture);
175 gst_caps_replace (&self->probed_srccaps, NULL);
176 gst_caps_replace (&self->probed_sinkcaps, NULL);
180 gst_v4l2_transform_stop (GstBaseTransform * trans)
182 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
184 GST_DEBUG_OBJECT (self, "Stop");
186 gst_v4l2_object_stop (self->v4l2output);
187 gst_v4l2_object_stop (self->v4l2capture);
188 gst_caps_replace (&self->incaps, NULL);
189 gst_caps_replace (&self->outcaps, NULL);
195 gst_v4l2_transform_set_caps (GstBaseTransform * trans, GstCaps * incaps,
198 GstV4l2Error error = GST_V4L2_ERROR_INIT;
199 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
201 if (self->incaps && self->outcaps) {
202 if (gst_caps_is_equal (incaps, self->incaps) &&
203 gst_caps_is_equal (outcaps, self->outcaps)) {
204 GST_DEBUG_OBJECT (trans, "Caps did not changed");
209 /* TODO Add renegotiation support */
210 g_return_val_if_fail (!GST_V4L2_IS_ACTIVE (self->v4l2output), FALSE);
211 g_return_val_if_fail (!GST_V4L2_IS_ACTIVE (self->v4l2capture), FALSE);
213 gst_caps_replace (&self->incaps, incaps);
214 gst_caps_replace (&self->outcaps, outcaps);
216 if (!gst_v4l2_object_set_format (self->v4l2output, incaps, &error))
219 if (!gst_v4l2_object_set_format (self->v4l2capture, outcaps, &error))
222 /* FIXME implement fallback if crop not supported */
223 if (!gst_v4l2_object_set_crop (self->v4l2output))
226 if (!gst_v4l2_object_set_crop (self->v4l2capture))
233 GST_ERROR_OBJECT (self, "failed to set input caps: %" GST_PTR_FORMAT,
235 gst_v4l2_error (self, &error);
240 gst_v4l2_object_stop (self->v4l2output);
241 GST_ERROR_OBJECT (self, "failed to set output caps: %" GST_PTR_FORMAT,
243 gst_v4l2_error (self, &error);
251 gst_v4l2_transform_query (GstBaseTransform * trans, GstPadDirection direction,
254 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
257 switch (GST_QUERY_TYPE (query)) {
258 case GST_QUERY_CAPS:{
259 GstCaps *filter, *caps = NULL, *result = NULL;
260 GstPad *pad, *otherpad;
262 gst_query_parse_caps (query, &filter);
264 if (direction == GST_PAD_SRC) {
265 pad = GST_BASE_TRANSFORM_SRC_PAD (trans);
266 otherpad = GST_BASE_TRANSFORM_SINK_PAD (trans);
267 if (self->probed_srccaps)
268 caps = gst_caps_ref (self->probed_srccaps);
270 pad = GST_BASE_TRANSFORM_SINK_PAD (trans);
271 otherpad = GST_BASE_TRANSFORM_SRC_PAD (trans);
272 if (self->probed_sinkcaps)
273 caps = gst_caps_ref (self->probed_sinkcaps);
277 caps = gst_pad_get_pad_template_caps (pad);
281 caps = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
282 gst_caps_unref (tmp);
285 result = gst_pad_peer_query_caps (otherpad, caps);
286 result = gst_caps_make_writable (result);
287 gst_caps_append (result, caps);
289 GST_DEBUG_OBJECT (self, "Returning %s caps %" GST_PTR_FORMAT,
290 GST_PAD_NAME (pad), result);
292 gst_query_set_caps_result (query, result);
293 gst_caps_unref (result);
298 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
307 gst_v4l2_transform_decide_allocation (GstBaseTransform * trans,
310 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
311 gboolean ret = FALSE;
313 GST_DEBUG_OBJECT (self, "called");
315 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query)) {
316 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2capture->pool);
318 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
321 if (!gst_buffer_pool_set_active (pool, TRUE))
322 goto activate_failed;
328 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
329 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
334 gst_v4l2_transform_propose_allocation (GstBaseTransform * trans,
335 GstQuery * decide_query, GstQuery * query)
337 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
338 gboolean ret = FALSE;
340 GST_DEBUG_OBJECT (self, "called");
342 if (decide_query == NULL)
345 ret = gst_v4l2_object_propose_allocation (self->v4l2output, query);
348 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
349 decide_query, query);
354 /* copies the given caps */
356 gst_v4l2_transform_caps_remove_format_info (GstCaps * caps)
363 res = gst_caps_new_empty ();
365 n = gst_caps_get_size (caps);
366 for (i = 0; i < n; i++) {
367 st = gst_caps_get_structure (caps, i);
368 f = gst_caps_get_features (caps, i);
370 /* If this is already expressed by the existing caps
371 * skip this structure */
372 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
375 st = gst_structure_copy (st);
376 /* Only remove format info for the cases when we can actually convert */
377 if (!gst_caps_features_is_any (f)
378 && gst_caps_features_is_equal (f,
379 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
380 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
381 "width", "height", "pixel-aspect-ratio", NULL);
383 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
389 /* The caps can be transformed into any other caps with format info removed.
390 * However, we should prefer passthrough, so if passthrough is possible,
391 * put it first in the list. */
393 gst_v4l2_transform_transform_caps (GstBaseTransform * btrans,
394 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
399 /* Get all possible caps that we can transform to */
400 tmp = gst_v4l2_transform_caps_remove_format_info (caps);
403 tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
404 gst_caps_unref (tmp);
410 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
411 GST_PTR_FORMAT, caps, result);
417 gst_v4l2_transform_fixate_caps (GstBaseTransform * trans,
418 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
420 GstStructure *ins, *outs;
421 const GValue *from_par, *to_par;
422 GValue fpar = { 0, }, tpar = {
425 othercaps = gst_caps_truncate (othercaps);
426 othercaps = gst_caps_make_writable (othercaps);
428 GST_DEBUG_OBJECT (trans, "trying to fixate othercaps %" GST_PTR_FORMAT
429 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
431 ins = gst_caps_get_structure (caps, 0);
432 outs = gst_caps_get_structure (othercaps, 0);
435 const gchar *in_format;
437 in_format = gst_structure_get_string (ins, "format");
439 /* Try to set output format for pass through */
440 gst_structure_fixate_field_string (outs, "format", in_format);
445 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
446 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
448 /* If we're fixating from the sinkpad we always set the PAR and
449 * assume that missing PAR on the sinkpad means 1/1 and
450 * missing PAR on the srcpad means undefined
452 if (direction == GST_PAD_SINK) {
454 g_value_init (&fpar, GST_TYPE_FRACTION);
455 gst_value_set_fraction (&fpar, 1, 1);
459 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
460 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
465 g_value_init (&tpar, GST_TYPE_FRACTION);
466 gst_value_set_fraction (&tpar, 1, 1);
469 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
473 g_value_init (&fpar, GST_TYPE_FRACTION);
474 gst_value_set_fraction (&fpar, 1, 1);
479 /* we have both PAR but they might not be fixated */
481 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
483 gint from_dar_n, from_dar_d;
486 /* from_par should be fixed */
487 g_return_val_if_fail (gst_value_is_fixed (from_par), othercaps);
489 from_par_n = gst_value_get_fraction_numerator (from_par);
490 from_par_d = gst_value_get_fraction_denominator (from_par);
492 gst_structure_get_int (ins, "width", &from_w);
493 gst_structure_get_int (ins, "height", &from_h);
495 gst_structure_get_int (outs, "width", &w);
496 gst_structure_get_int (outs, "height", &h);
498 /* if both width and height are already fixed, we can't do anything
499 * about it anymore */
503 GST_DEBUG_OBJECT (trans, "dimensions already set to %dx%d, not fixating",
505 if (!gst_value_is_fixed (to_par)) {
506 if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
507 from_par_n, from_par_d, w, h)) {
508 GST_DEBUG_OBJECT (trans, "fixating to_par to %dx%d", n, d);
509 if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
510 gst_structure_fixate_field_nearest_fraction (outs,
511 "pixel-aspect-ratio", n, d);
513 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
520 /* Calculate input DAR */
521 if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
522 &from_dar_n, &from_dar_d)) {
523 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
524 ("Error calculating the output scaled size - integer overflow"));
528 GST_DEBUG_OBJECT (trans, "Input DAR is %d/%d", from_dar_n, from_dar_d);
530 /* If either width or height are fixed there's not much we
531 * can do either except choosing a height or width and PAR
532 * that matches the DAR as good as possible
536 gint set_w, set_par_n, set_par_d;
538 GST_DEBUG_OBJECT (trans, "height is fixed (%d)", h);
540 /* If the PAR is fixed too, there's not much to do
541 * except choosing the width that is nearest to the
542 * width with the same DAR */
543 if (gst_value_is_fixed (to_par)) {
544 to_par_n = gst_value_get_fraction_numerator (to_par);
545 to_par_d = gst_value_get_fraction_denominator (to_par);
547 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
549 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
550 to_par_n, &num, &den)) {
551 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
552 ("Error calculating the output scaled size - integer overflow"));
556 w = (guint) gst_util_uint64_scale_int (h, num, den);
557 gst_structure_fixate_field_nearest_int (outs, "width", w);
562 /* The PAR is not fixed and it's quite likely that we can set
563 * an arbitrary PAR. */
565 /* Check if we can keep the input width */
566 tmp = gst_structure_copy (outs);
567 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
568 gst_structure_get_int (tmp, "width", &set_w);
570 /* Might have failed but try to keep the DAR nonetheless by
571 * adjusting the PAR */
572 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
573 &to_par_n, &to_par_d)) {
574 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
575 ("Error calculating the output scaled size - integer overflow"));
576 gst_structure_free (tmp);
580 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
581 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
582 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
584 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
586 gst_structure_free (tmp);
588 /* Check if the adjusted PAR is accepted */
589 if (set_par_n == to_par_n && set_par_d == to_par_d) {
590 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
591 set_par_n != set_par_d)
592 gst_structure_set (outs, "width", G_TYPE_INT, set_w,
593 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
598 /* Otherwise scale the width to the new PAR and check if the
599 * adjusted with is accepted. If all that fails we can't keep
601 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
602 set_par_n, &num, &den)) {
603 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
604 ("Error calculating the output scaled size - integer overflow"));
608 w = (guint) gst_util_uint64_scale_int (h, num, den);
609 gst_structure_fixate_field_nearest_int (outs, "width", w);
610 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
611 set_par_n != set_par_d)
612 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
613 set_par_n, set_par_d, NULL);
618 gint set_h, set_par_n, set_par_d;
620 GST_DEBUG_OBJECT (trans, "width is fixed (%d)", w);
622 /* If the PAR is fixed too, there's not much to do
623 * except choosing the height that is nearest to the
624 * height with the same DAR */
625 if (gst_value_is_fixed (to_par)) {
626 to_par_n = gst_value_get_fraction_numerator (to_par);
627 to_par_d = gst_value_get_fraction_denominator (to_par);
629 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
631 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
632 to_par_n, &num, &den)) {
633 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
634 ("Error calculating the output scaled size - integer overflow"));
638 h = (guint) gst_util_uint64_scale_int (w, den, num);
639 gst_structure_fixate_field_nearest_int (outs, "height", h);
644 /* The PAR is not fixed and it's quite likely that we can set
645 * an arbitrary PAR. */
647 /* Check if we can keep the input height */
648 tmp = gst_structure_copy (outs);
649 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
650 gst_structure_get_int (tmp, "height", &set_h);
652 /* Might have failed but try to keep the DAR nonetheless by
653 * adjusting the PAR */
654 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
655 &to_par_n, &to_par_d)) {
656 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
657 ("Error calculating the output scaled size - integer overflow"));
658 gst_structure_free (tmp);
661 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
662 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
663 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
665 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
667 gst_structure_free (tmp);
669 /* Check if the adjusted PAR is accepted */
670 if (set_par_n == to_par_n && set_par_d == to_par_d) {
671 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
672 set_par_n != set_par_d)
673 gst_structure_set (outs, "height", G_TYPE_INT, set_h,
674 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
679 /* Otherwise scale the height to the new PAR and check if the
680 * adjusted with is accepted. If all that fails we can't keep
682 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
683 set_par_n, &num, &den)) {
684 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
685 ("Error calculating the output scaled size - integer overflow"));
689 h = (guint) gst_util_uint64_scale_int (w, den, num);
690 gst_structure_fixate_field_nearest_int (outs, "height", h);
691 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
692 set_par_n != set_par_d)
693 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
694 set_par_n, set_par_d, NULL);
697 } else if (gst_value_is_fixed (to_par)) {
699 gint set_h, set_w, f_h, f_w;
701 to_par_n = gst_value_get_fraction_numerator (to_par);
702 to_par_d = gst_value_get_fraction_denominator (to_par);
704 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
706 /* Calculate scale factor for the PAR change */
707 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
708 to_par_n, &num, &den)) {
709 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
710 ("Error calculating the output scaled size - integer overflow"));
714 /* Try to keep the input height (because of interlacing) */
715 tmp = gst_structure_copy (outs);
716 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
717 gst_structure_get_int (tmp, "height", &set_h);
719 /* This might have failed but try to scale the width
720 * to keep the DAR nonetheless */
721 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
722 gst_structure_fixate_field_nearest_int (tmp, "width", w);
723 gst_structure_get_int (tmp, "width", &set_w);
724 gst_structure_free (tmp);
726 /* We kept the DAR and the height is nearest to the original height */
728 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
729 G_TYPE_INT, set_h, NULL);
736 /* If the former failed, try to keep the input width at least */
737 tmp = gst_structure_copy (outs);
738 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
739 gst_structure_get_int (tmp, "width", &set_w);
741 /* This might have failed but try to scale the width
742 * to keep the DAR nonetheless */
743 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
744 gst_structure_fixate_field_nearest_int (tmp, "height", h);
745 gst_structure_get_int (tmp, "height", &set_h);
746 gst_structure_free (tmp);
748 /* We kept the DAR and the width is nearest to the original width */
750 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
751 G_TYPE_INT, set_h, NULL);
755 /* If all this failed, keep the height that was nearest to the orignal
756 * height and the nearest possible width. This changes the DAR but
757 * there's not much else to do here.
759 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
764 gint set_h, set_w, set_par_n, set_par_d, tmp2;
766 /* width, height and PAR are not fixed but passthrough is not possible */
768 /* First try to keep the height and width as good as possible
770 tmp = gst_structure_copy (outs);
771 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
772 gst_structure_get_int (tmp, "height", &set_h);
773 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
774 gst_structure_get_int (tmp, "width", &set_w);
776 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
777 &to_par_n, &to_par_d)) {
778 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
779 ("Error calculating the output scaled size - integer overflow"));
780 gst_structure_free (tmp);
784 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
785 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
786 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
788 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
790 gst_structure_free (tmp);
792 if (set_par_n == to_par_n && set_par_d == to_par_d) {
793 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
794 G_TYPE_INT, set_h, NULL);
796 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
797 set_par_n != set_par_d)
798 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
799 set_par_n, set_par_d, NULL);
803 /* Otherwise try to scale width to keep the DAR with the set
805 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
806 set_par_n, &num, &den)) {
807 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
808 ("Error calculating the output scaled size - integer overflow"));
812 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
813 tmp = gst_structure_copy (outs);
814 gst_structure_fixate_field_nearest_int (tmp, "width", w);
815 gst_structure_get_int (tmp, "width", &tmp2);
816 gst_structure_free (tmp);
819 gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
820 G_TYPE_INT, set_h, NULL);
821 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
822 set_par_n != set_par_d)
823 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
824 set_par_n, set_par_d, NULL);
828 /* ... or try the same with the height */
829 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
830 tmp = gst_structure_copy (outs);
831 gst_structure_fixate_field_nearest_int (tmp, "height", h);
832 gst_structure_get_int (tmp, "height", &tmp2);
833 gst_structure_free (tmp);
836 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
837 G_TYPE_INT, tmp2, NULL);
838 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
839 set_par_n != set_par_d)
840 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
841 set_par_n, set_par_d, NULL);
845 /* If all fails we can't keep the DAR and take the nearest values
846 * for everything from the first try */
847 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
848 G_TYPE_INT, set_h, NULL);
849 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
850 set_par_n != set_par_d)
851 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
852 set_par_n, set_par_d, NULL);
857 GST_DEBUG_OBJECT (trans, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
859 if (from_par == &fpar)
860 g_value_unset (&fpar);
862 g_value_unset (&tpar);
864 /* fixate remaining fields */
865 othercaps = gst_caps_fixate (othercaps);
867 if (direction == GST_PAD_SINK) {
868 if (gst_caps_is_subset (caps, othercaps)) {
869 gst_caps_replace (&othercaps, caps);
877 gst_v4l2_transform_prepare_output_buffer (GstBaseTransform * trans,
878 GstBuffer * inbuf, GstBuffer ** outbuf)
880 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
881 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
882 GstFlowReturn ret = GST_FLOW_OK;
883 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_CLASS (parent_class);
885 if (gst_base_transform_is_passthrough (trans)) {
886 GST_DEBUG_OBJECT (self, "Passthrough, no need to do anything");
891 /* Ensure input internal pool is active */
892 if (!gst_buffer_pool_is_active (pool)) {
893 GstStructure *config = gst_buffer_pool_get_config (pool);
894 gint min = self->v4l2output->min_buffers == 0 ? GST_V4L2_MIN_BUFFERS :
895 self->v4l2output->min_buffers;
896 gst_buffer_pool_config_set_params (config, self->incaps,
897 self->v4l2output->info.size, min, min);
899 /* There is no reason to refuse this config */
900 if (!gst_buffer_pool_set_config (pool, config))
901 goto activate_failed;
903 if (!gst_buffer_pool_set_active (pool, TRUE))
904 goto activate_failed;
907 GST_DEBUG_OBJECT (self, "Queue input buffer");
908 ret = gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool), &inbuf);
909 if (G_UNLIKELY (ret != GST_FLOW_OK))
913 pool = gst_base_transform_get_buffer_pool (trans);
915 if (!gst_buffer_pool_set_active (pool, TRUE))
916 goto activate_failed;
918 GST_DEBUG_OBJECT (self, "Dequeue output buffer");
919 ret = gst_buffer_pool_acquire_buffer (pool, outbuf, NULL);
920 g_object_unref (pool);
922 if (ret != GST_FLOW_OK)
925 pool = self->v4l2capture->pool;
926 ret = gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool), outbuf);
928 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
930 if (ret != GST_FLOW_OK) {
931 gst_buffer_unref (*outbuf);
935 if (bclass->copy_metadata)
936 if (!bclass->copy_metadata (trans, inbuf, *outbuf)) {
937 /* something failed, post a warning */
938 GST_ELEMENT_WARNING (self, STREAM, NOT_IMPLEMENTED,
939 ("could not copy metadata"), (NULL));
946 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
947 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
948 g_object_unref (pool);
949 return GST_FLOW_ERROR;
952 GST_DEBUG_OBJECT (self, "could not allocate buffer from pool");
957 gst_v4l2_transform_transform (GstBaseTransform * trans, GstBuffer * inbuf,
965 gst_v4l2_transform_sink_event (GstBaseTransform * trans, GstEvent * event)
967 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
970 /* Nothing to flush in passthrough */
971 if (gst_base_transform_is_passthrough (trans))
972 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
974 switch (GST_EVENT_TYPE (event)) {
975 case GST_EVENT_FLUSH_START:
976 GST_DEBUG_OBJECT (self, "flush start");
977 gst_v4l2_object_unlock (self->v4l2output);
978 gst_v4l2_object_unlock (self->v4l2capture);
984 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
986 switch (GST_EVENT_TYPE (event)) {
987 case GST_EVENT_FLUSH_STOP:
988 /* Buffer should be back now */
989 GST_DEBUG_OBJECT (self, "flush stop");
990 gst_v4l2_object_unlock_stop (self->v4l2capture);
991 gst_v4l2_object_unlock_stop (self->v4l2output);
992 gst_v4l2_buffer_pool_flush (self->v4l2output->pool);
993 gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);
1002 static GstStateChangeReturn
1003 gst_v4l2_transform_change_state (GstElement * element,
1004 GstStateChange transition)
1006 GstV4l2Transform *self = GST_V4L2_TRANSFORM (element);
1007 GstStateChangeReturn ret;
1009 switch (transition) {
1010 case GST_STATE_CHANGE_NULL_TO_READY:
1011 if (!gst_v4l2_transform_open (self))
1012 return GST_STATE_CHANGE_FAILURE;
1014 case GST_STATE_CHANGE_PAUSED_TO_READY:
1015 gst_v4l2_object_unlock (self->v4l2output);
1016 gst_v4l2_object_unlock (self->v4l2capture);
1022 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1024 switch (transition) {
1025 case GST_STATE_CHANGE_READY_TO_NULL:
1026 gst_v4l2_transform_close (self);
1036 gst_v4l2_transform_dispose (GObject * object)
1038 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
1040 gst_caps_replace (&self->probed_sinkcaps, NULL);
1041 gst_caps_replace (&self->probed_srccaps, NULL);
1043 G_OBJECT_CLASS (parent_class)->dispose (object);
1047 gst_v4l2_transform_finalize (GObject * object)
1049 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
1051 gst_v4l2_object_destroy (self->v4l2capture);
1052 gst_v4l2_object_destroy (self->v4l2output);
1054 G_OBJECT_CLASS (parent_class)->finalize (object);
1058 gst_v4l2_transform_init (GstV4l2Transform * self)
1060 /* V4L2 object are created in subinstance_init */
1062 gst_base_transform_set_qos_enabled (GST_BASE_TRANSFORM (self), TRUE);
1066 gst_v4l2_transform_subinstance_init (GTypeInstance * instance, gpointer g_class)
1068 GstV4l2TransformClass *klass = GST_V4L2_TRANSFORM_CLASS (g_class);
1069 GstV4l2Transform *self = GST_V4L2_TRANSFORM (instance);
1071 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
1072 GST_OBJECT (GST_BASE_TRANSFORM_SINK_PAD (self)),
1073 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1074 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
1075 self->v4l2output->no_initial_format = TRUE;
1076 self->v4l2output->keep_aspect = FALSE;
1078 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
1079 GST_OBJECT (GST_BASE_TRANSFORM_SRC_PAD (self)),
1080 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1081 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
1082 self->v4l2capture->no_initial_format = TRUE;
1083 self->v4l2output->keep_aspect = FALSE;
1087 gst_v4l2_transform_class_init (GstV4l2TransformClass * klass)
1089 GstElementClass *element_class;
1090 GObjectClass *gobject_class;
1091 GstBaseTransformClass *base_transform_class;
1093 element_class = (GstElementClass *) klass;
1094 gobject_class = (GObjectClass *) klass;
1095 base_transform_class = (GstBaseTransformClass *) klass;
1097 GST_DEBUG_CATEGORY_INIT (gst_v4l2_transform_debug, "v4l2transform", 0,
1100 gst_element_class_set_static_metadata (element_class,
1101 "V4L2 Video Converter",
1102 "Filter/Converter/Video/Scaler",
1103 "Transform streams via V4L2 API",
1104 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1106 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_transform_dispose);
1107 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_transform_finalize);
1108 gobject_class->set_property =
1109 GST_DEBUG_FUNCPTR (gst_v4l2_transform_set_property);
1110 gobject_class->get_property =
1111 GST_DEBUG_FUNCPTR (gst_v4l2_transform_get_property);
1113 base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_transform_stop);
1114 base_transform_class->set_caps =
1115 GST_DEBUG_FUNCPTR (gst_v4l2_transform_set_caps);
1116 base_transform_class->query = GST_DEBUG_FUNCPTR (gst_v4l2_transform_query);
1117 base_transform_class->sink_event =
1118 GST_DEBUG_FUNCPTR (gst_v4l2_transform_sink_event);
1119 base_transform_class->decide_allocation =
1120 GST_DEBUG_FUNCPTR (gst_v4l2_transform_decide_allocation);
1121 base_transform_class->propose_allocation =
1122 GST_DEBUG_FUNCPTR (gst_v4l2_transform_propose_allocation);
1123 base_transform_class->transform_caps =
1124 GST_DEBUG_FUNCPTR (gst_v4l2_transform_transform_caps);
1125 base_transform_class->fixate_caps =
1126 GST_DEBUG_FUNCPTR (gst_v4l2_transform_fixate_caps);
1127 base_transform_class->prepare_output_buffer =
1128 GST_DEBUG_FUNCPTR (gst_v4l2_transform_prepare_output_buffer);
1129 base_transform_class->transform =
1130 GST_DEBUG_FUNCPTR (gst_v4l2_transform_transform);
1132 base_transform_class->passthrough_on_same_caps = TRUE;
1134 element_class->change_state =
1135 GST_DEBUG_FUNCPTR (gst_v4l2_transform_change_state);
1137 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
1141 gst_v4l2_transform_subclass_init (gpointer g_class, gpointer data)
1143 GstV4l2TransformClass *klass = GST_V4L2_TRANSFORM_CLASS (g_class);
1144 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1145 GstV4l2TransformCData *cdata = data;
1147 klass->default_device = cdata->device;
1149 /* Note: gst_pad_template_new() take the floating ref from the caps */
1150 gst_element_class_add_pad_template (element_class,
1151 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1153 gst_element_class_add_pad_template (element_class,
1154 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1160 /* Probing functions */
1162 gst_v4l2_is_transform (GstCaps * sink_caps, GstCaps * src_caps)
1164 gboolean ret = FALSE;
1166 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_raw_caps ())
1167 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1174 gst_v4l2_transform_register (GstPlugin * plugin, const gchar * basename,
1175 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
1177 GTypeQuery type_query;
1178 GTypeInfo type_info = { 0, };
1179 GType type, subtype;
1181 GstV4l2TransformCData *cdata;
1183 cdata = g_new0 (GstV4l2TransformCData, 1);
1184 cdata->device = g_strdup (device_path);
1185 cdata->sink_caps = gst_caps_ref (sink_caps);
1186 cdata->src_caps = gst_caps_ref (src_caps);
1188 type = gst_v4l2_transform_get_type ();
1189 g_type_query (type, &type_query);
1190 memset (&type_info, 0, sizeof (type_info));
1191 type_info.class_size = type_query.class_size;
1192 type_info.instance_size = type_query.instance_size;
1193 type_info.class_init = gst_v4l2_transform_subclass_init;
1194 type_info.class_data = cdata;
1195 type_info.instance_init = gst_v4l2_transform_subinstance_init;
1197 type_name = g_strdup_printf ("v4l2%sconvert", basename);
1198 subtype = g_type_register_static (type, type_name, &type_info, 0);
1200 if (!gst_element_register (plugin, type_name, GST_RANK_NONE, subtype))
1201 GST_WARNING ("Failed to register plugin '%s'", type_name);