3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
6 * gstv4l2object.c: base class for V4L2 elements
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Library General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. This library is distributed in the hope
12 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU 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 Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
32 #include <gudev/gudev.h>
35 #include "v4l2_calls.h"
36 #include "gstv4l2tuner.h"
37 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
38 #include "gstv4l2xoverlay.h"
40 #include "gstv4l2colorbalance.h"
42 #include "gst/gst-i18n-plugin.h"
45 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
46 #define GST_CAT_DEFAULT v4l2_debug
49 #define DEFAULT_PROP_DEVICE_NAME NULL
50 #define DEFAULT_PROP_DEVICE_FD -1
51 #define DEFAULT_PROP_FLAGS 0
52 #define DEFAULT_PROP_NORM NULL
53 #define DEFAULT_PROP_CHANNEL NULL
54 #define DEFAULT_PROP_FREQUENCY 0
59 V4L2_STD_OBJECT_PROPS,
63 gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
65 GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
66 static GList *list = NULL;
68 /* well, not perfect, but better than no locking at all.
69 * In the worst case we leak a list node, so who cares? */
70 GST_CLASS_LOCK (GST_OBJECT_CLASS (klass));
73 list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
76 GST_CLASS_UNLOCK (GST_OBJECT_CLASS (klass));
81 static gboolean init = FALSE;
82 static GList *devices = NULL;
86 gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
87 GList ** klass_devices)
89 GUdevClient *client = NULL;
94 gchar *device = devices->data;
95 devices = g_list_remove (devices, device);
99 GST_INFO ("Enumerating video4linux devices from udev");
100 client = g_udev_client_new (NULL);
102 GST_WARNING ("Failed to initialize gudev client");
106 item = g_udev_client_query_by_subsystem (client, "video4linux");
108 GUdevDevice *device = item->data;
109 gchar *devnode = g_strdup (g_udev_device_get_device_file (device));
110 gint api = g_udev_device_get_property_as_int (device, "ID_V4L_VERSION");
111 GST_INFO ("Found new device: %s, API: %d", devnode, api);
112 /* Append v4l2 devices only. If api is 0 probably v4l_id has
113 been stripped out of the current udev installation, append
117 ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
119 if ((api == 2 || api == 0)) {
120 devices = g_list_append (devices, devnode);
124 g_object_unref (device);
133 g_object_unref (client);
136 *klass_devices = devices;
140 #endif /* HAVE_GUDEV */
143 gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
144 GList ** klass_devices)
147 const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
151 gchar *device = devices->data;
152 devices = g_list_remove (devices, device);
157 * detect /dev entries
159 for (n = 0; n < 64; n++) {
160 for (base = 0; dev_base[base] != NULL; base++) {
162 gchar *device = g_strdup_printf ("%s%d",
167 * does the /dev/ entry exist at all?
169 if (stat (device, &s) == 0) {
171 * yes: is a device attached?
173 if (S_ISCHR (s.st_mode)) {
175 if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
179 devices = g_list_append (devices, device);
190 *klass_devices = devices;
196 gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
197 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
199 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
204 if (!gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices))
205 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
206 #else /* !HAVE_GUDEV */
207 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
208 #endif /* HAVE_GUDEV */
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
217 gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
218 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
220 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
221 gboolean ret = FALSE;
227 !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
228 #else /* !HAVE_GUDEV */
229 ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
230 #endif /* HAVE_GUDEV */
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
240 gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
243 GValue value = { 0 };
249 array = g_value_array_new (g_list_length (*klass_devices));
250 item = *klass_devices;
251 g_value_init (&value, G_TYPE_STRING);
253 gchar *device = item->data;
255 g_value_set_string (&value, device);
256 g_value_array_append (array, &value);
260 g_value_unset (&value);
266 gst_v4l2_probe_get_values (GstPropertyProbe * probe,
267 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
269 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
270 GValueArray *array = NULL;
274 array = gst_v4l2_class_list_devices (klass, klass_devices);
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
284 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
286 gst_v4l2_device_get_type (void)
288 static GType v4l2_device_type = 0;
290 if (v4l2_device_type == 0) {
291 static const GFlagsValue values[] = {
292 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
293 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
294 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
296 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
297 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
299 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
300 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
306 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
309 return v4l2_device_type;
313 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
314 const char *default_device)
316 g_object_class_install_property (gobject_class, PROP_DEVICE,
317 g_param_spec_string ("device", "Device", "Device location",
318 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
319 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
320 g_param_spec_string ("device-name", "Device name",
321 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
322 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
324 g_param_spec_int ("device-fd", "File descriptor",
325 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
326 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
327 g_object_class_install_property (gobject_class, PROP_FLAGS,
328 g_param_spec_flags ("flags", "Flags", "Device type flags",
329 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
330 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
334 gst_v4l2_object_new (GstElement * element,
335 enum v4l2_buf_type type,
336 const char *default_device,
337 GstV4l2GetInOutFunction get_in_out_func,
338 GstV4l2SetInOutFunction set_in_out_func,
339 GstV4l2UpdateFpsFunction update_fps_func)
341 GstV4l2Object *v4l2object;
344 * some default values
346 v4l2object = g_new0 (GstV4l2Object, 1);
348 v4l2object->type = type;
349 v4l2object->formats = NULL;
351 v4l2object->element = element;
352 v4l2object->get_in_out_func = get_in_out_func;
353 v4l2object->set_in_out_func = set_in_out_func;
354 v4l2object->update_fps_func = update_fps_func;
356 v4l2object->video_fd = -1;
357 v4l2object->poll = gst_poll_new (TRUE);
358 v4l2object->buffer = NULL;
359 v4l2object->videodev = g_strdup (default_device);
361 v4l2object->norms = NULL;
362 v4l2object->channels = NULL;
363 v4l2object->colors = NULL;
365 v4l2object->xwindow_id = 0;
370 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
374 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
376 g_return_if_fail (v4l2object != NULL);
378 if (v4l2object->videodev)
379 g_free (v4l2object->videodev);
381 if (v4l2object->poll)
382 gst_poll_free (v4l2object->poll);
384 if (v4l2object->channel)
385 g_free (v4l2object->channel);
387 if (v4l2object->norm)
388 g_free (v4l2object->norm);
390 if (v4l2object->formats) {
391 gst_v4l2_object_clear_format_list (v4l2object);
399 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
401 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
402 g_slist_free (v4l2object->formats);
403 v4l2object->formats = NULL;
410 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
411 guint prop_id, const GValue * value, GParamSpec * pspec)
415 g_free (v4l2object->videodev);
416 v4l2object->videodev = g_value_dup_string (value);
420 if (GST_V4L2_IS_OPEN (v4l2object)) {
421 GstTuner *tuner = GST_TUNER (v4l2object->element);
422 GstTunerNorm *norm = gst_tuner_find_norm_by_name (tuner,
423 (gchar *) g_value_get_string (value));
426 /* like gst_tuner_set_norm (tuner, norm)
427 without g_object_notify */
428 gst_v4l2_tuner_set_norm (v4l2object, norm);
431 g_free (v4l2object->norm);
432 v4l2object->norm = g_value_dup_string (value);
436 if (GST_V4L2_IS_OPEN (v4l2object)) {
437 GstTuner *tuner = GST_TUNER (v4l2object->element);
438 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
439 (gchar *) g_value_get_string (value));
442 /* like gst_tuner_set_channel (tuner, channel)
443 without g_object_notify */
444 gst_v4l2_tuner_set_channel (v4l2object, channel);
447 g_free (v4l2object->channel);
448 v4l2object->channel = g_value_dup_string (value);
452 if (GST_V4L2_IS_OPEN (v4l2object)) {
453 GstTuner *tuner = GST_TUNER (v4l2object->element);
454 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
457 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
459 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
460 without g_object_notify */
461 gst_v4l2_tuner_set_frequency (v4l2object, channel,
462 g_value_get_ulong (value));
465 v4l2object->frequency = g_value_get_ulong (value);
478 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
479 guint prop_id, GValue * value, GParamSpec * pspec)
483 g_value_set_string (value, v4l2object->videodev);
485 case PROP_DEVICE_NAME:
487 const guchar *new = NULL;
489 if (GST_V4L2_IS_OPEN (v4l2object)) {
490 new = v4l2object->vcap.card;
491 } else if (gst_v4l2_open (v4l2object)) {
492 new = v4l2object->vcap.card;
493 gst_v4l2_close (v4l2object);
495 g_value_set_string (value, (gchar *) new);
500 if (GST_V4L2_IS_OPEN (v4l2object))
501 g_value_set_int (value, v4l2object->video_fd);
503 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
510 if (GST_V4L2_IS_OPEN (v4l2object)) {
511 flags |= v4l2object->vcap.capabilities &
512 (V4L2_CAP_VIDEO_CAPTURE |
513 V4L2_CAP_VIDEO_OUTPUT |
514 V4L2_CAP_VIDEO_OVERLAY |
515 V4L2_CAP_VBI_CAPTURE |
516 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
518 g_value_set_flags (value, flags);
529 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
531 GstTunerNorm *norm = NULL;
532 GstTunerChannel *channel = NULL;
535 if (!GST_IS_TUNER (v4l2object->element))
538 tuner = GST_TUNER (v4l2object->element);
540 if (v4l2object->norm)
541 norm = gst_tuner_find_norm_by_name (tuner, v4l2object->norm);
543 gst_tuner_set_norm (tuner, norm);
546 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
548 g_free (v4l2object->norm);
549 v4l2object->norm = g_strdup (norm->label);
550 gst_tuner_norm_changed (tuner, norm);
554 if (v4l2object->channel)
555 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
557 gst_tuner_set_channel (tuner, channel);
560 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
561 (v4l2object->element)));
563 g_free (v4l2object->channel);
564 v4l2object->channel = g_strdup (channel->label);
565 gst_tuner_channel_changed (tuner, channel);
570 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
571 if (v4l2object->frequency != 0) {
572 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
574 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
575 if (v4l2object->frequency == 0) {
577 gst_tuner_set_frequency (tuner, channel, 1000);
585 gst_v4l2_object_start (GstV4l2Object * v4l2object)
587 if (gst_v4l2_open (v4l2object))
588 gst_v4l2_set_defaults (v4l2object);
592 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
593 gst_v4l2_xoverlay_start (v4l2object);
600 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
602 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
603 gst_v4l2_xoverlay_stop (v4l2object);
606 if (!gst_v4l2_close (v4l2object))
609 if (v4l2object->formats) {
610 gst_v4l2_object_clear_format_list (v4l2object);
618 * common format / caps utilities:
626 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
627 /* from Linux 2.6.15 videodev2.h */
628 {V4L2_PIX_FMT_RGB332, TRUE},
629 {V4L2_PIX_FMT_RGB555, TRUE},
630 {V4L2_PIX_FMT_RGB565, TRUE},
631 {V4L2_PIX_FMT_RGB555X, TRUE},
632 {V4L2_PIX_FMT_RGB565X, TRUE},
633 {V4L2_PIX_FMT_BGR24, TRUE},
634 {V4L2_PIX_FMT_RGB24, TRUE},
635 {V4L2_PIX_FMT_BGR32, TRUE},
636 {V4L2_PIX_FMT_RGB32, TRUE},
637 {V4L2_PIX_FMT_GREY, TRUE},
638 {V4L2_PIX_FMT_YVU410, TRUE},
639 {V4L2_PIX_FMT_YVU420, TRUE},
640 {V4L2_PIX_FMT_YUYV, TRUE},
641 {V4L2_PIX_FMT_UYVY, TRUE},
642 {V4L2_PIX_FMT_YUV422P, TRUE},
643 {V4L2_PIX_FMT_YUV411P, TRUE},
644 {V4L2_PIX_FMT_Y41P, TRUE},
646 /* two planes -- one Y, one Cr + Cb interleaved */
647 {V4L2_PIX_FMT_NV12, TRUE},
648 {V4L2_PIX_FMT_NV21, TRUE},
650 /* The following formats are not defined in the V4L2 specification */
651 {V4L2_PIX_FMT_YUV410, TRUE},
652 {V4L2_PIX_FMT_YUV420, TRUE},
653 {V4L2_PIX_FMT_YYUV, TRUE},
654 {V4L2_PIX_FMT_HI240, TRUE},
656 /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
657 #ifdef V4L2_PIX_FMT_SBGGR8
658 {V4L2_PIX_FMT_SBGGR8, TRUE},
661 /* compressed formats */
662 {V4L2_PIX_FMT_MJPEG, TRUE},
663 {V4L2_PIX_FMT_JPEG, TRUE},
664 {V4L2_PIX_FMT_DV, TRUE},
665 {V4L2_PIX_FMT_MPEG, FALSE},
667 /* Vendor-specific formats */
668 {V4L2_PIX_FMT_WNVA, TRUE},
670 #ifdef V4L2_PIX_FMT_SN9C10X
671 {V4L2_PIX_FMT_SN9C10X, TRUE},
673 #ifdef V4L2_PIX_FMT_PWC1
674 {V4L2_PIX_FMT_PWC1, TRUE},
676 #ifdef V4L2_PIX_FMT_PWC2
677 {V4L2_PIX_FMT_PWC2, TRUE},
679 #ifdef V4L2_PIX_FMT_YVYU
680 {V4L2_PIX_FMT_YVYU, TRUE},
684 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
687 static struct v4l2_fmtdesc *
688 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
691 struct v4l2_fmtdesc *fmt;
697 walk = gst_v4l2_object_get_format_list (v4l2object);
699 fmt = (struct v4l2_fmtdesc *) walk->data;
700 if (fmt->pixelformat == fourcc)
702 /* special case for jpeg */
703 if ((fmt->pixelformat == V4L2_PIX_FMT_MJPEG && fourcc == V4L2_PIX_FMT_JPEG)
704 || (fmt->pixelformat == V4L2_PIX_FMT_JPEG
705 && fourcc == V4L2_PIX_FMT_MJPEG)) {
708 walk = g_slist_next (walk);
716 /* complete made up ranking, the values themselves are meaningless */
717 #define YUV_BASE_RANK 1000
718 #define JPEG_BASE_RANK 500
719 #define DV_BASE_RANK 200
720 #define RGB_BASE_RANK 100
721 #define YUV_ODD_BASE_RANK 50
722 #define RGB_ODD_BASE_RANK 25
723 #define BAYER_BASE_RANK 15
724 #define S910_BASE_RANK 10
725 #define GREY_BASE_RANK 5
726 #define PWC_BASE_RANK 1
728 /* This flag is already used by libv4l2 although
729 * it was added to the Linux kernel in 2.6.32
731 #ifndef V4L2_FMT_FLAG_EMULATED
732 #define V4L2_FMT_FLAG_EMULATED 0x0002
736 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
738 guint32 fourcc = fmt->pixelformat;
739 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
743 case V4L2_PIX_FMT_MJPEG:
744 rank = JPEG_BASE_RANK;
746 case V4L2_PIX_FMT_JPEG:
747 rank = JPEG_BASE_RANK + 1;
749 case V4L2_PIX_FMT_MPEG: /* MPEG */
750 rank = JPEG_BASE_RANK + 2;
753 case V4L2_PIX_FMT_RGB332:
754 case V4L2_PIX_FMT_RGB555:
755 case V4L2_PIX_FMT_RGB555X:
756 case V4L2_PIX_FMT_RGB565:
757 case V4L2_PIX_FMT_RGB565X:
758 rank = RGB_ODD_BASE_RANK;
761 case V4L2_PIX_FMT_RGB24:
762 case V4L2_PIX_FMT_BGR24:
763 rank = RGB_BASE_RANK - 1;
766 case V4L2_PIX_FMT_RGB32:
767 case V4L2_PIX_FMT_BGR32:
768 rank = RGB_BASE_RANK;
771 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
772 rank = GREY_BASE_RANK;
775 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
776 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
777 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
778 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
779 rank = YUV_ODD_BASE_RANK;
782 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
783 rank = YUV_BASE_RANK + 3;
785 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
786 rank = YUV_BASE_RANK + 2;
788 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
789 rank = YUV_BASE_RANK + 7;
791 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
792 rank = YUV_BASE_RANK + 10;
794 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
795 rank = YUV_BASE_RANK + 6;
797 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
798 rank = YUV_BASE_RANK + 9;
800 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
801 rank = YUV_BASE_RANK + 5;
803 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
804 rank = YUV_BASE_RANK + 4;
806 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
807 rank = YUV_BASE_RANK + 8;
810 case V4L2_PIX_FMT_DV:
814 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
818 #ifdef V4L2_PIX_FMT_SBGGR8
819 case V4L2_PIX_FMT_SBGGR8:
820 rank = BAYER_BASE_RANK;
824 #ifdef V4L2_PIX_FMT_SN9C10X
825 case V4L2_PIX_FMT_SN9C10X:
826 rank = S910_BASE_RANK;
830 #ifdef V4L2_PIX_FMT_PWC1
831 case V4L2_PIX_FMT_PWC1:
832 rank = PWC_BASE_RANK;
835 #ifdef V4L2_PIX_FMT_PWC2
836 case V4L2_PIX_FMT_PWC2:
837 rank = PWC_BASE_RANK;
846 /* All ranks are below 1<<15 so a shift by 15
847 * will a) make all non-emulated formats larger
848 * than emulated and b) will not overflow
859 format_cmp_func (gconstpointer a, gconstpointer b)
861 const struct v4l2_fmtdesc *fa = a;
862 const struct v4l2_fmtdesc *fb = b;
864 if (fa->pixelformat == fb->pixelformat)
867 return gst_v4l2_object_format_get_rank (fa) -
868 gst_v4l2_object_format_get_rank (fb);
871 /******************************************************
872 * gst_v4l2_object_fill_format_list():
873 * create list of supported capture formats
874 * return value: TRUE on success, FALSE on error
875 ******************************************************/
877 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object)
880 struct v4l2_fmtdesc *format;
882 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
884 /* format enumeration */
886 format = g_new0 (struct v4l2_fmtdesc, 1);
889 format->type = v4l2object->type;
891 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
892 if (errno == EINVAL) {
894 break; /* end of enumeration */
900 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
901 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
902 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
903 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
904 format->description);
905 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
906 GST_FOURCC_ARGS (format->pixelformat));
908 /* sort formats according to our preference; we do this, because caps
909 * are probed in the order the formats are in the list, and the order of
910 * formats in the final probed caps matters for things like fixation */
911 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
912 (GCompareFunc) format_cmp_func);
915 GST_DEBUG_OBJECT (v4l2object->element, "got %d format(s)", n);
922 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
923 (_("Failed to enumerate possible video formats device '%s' can work with"), v4l2object->videodev), ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)", n, v4l2object->videodev, errno, g_strerror (errno)));
930 * Get the list of supported capture formats, a list of
931 * <code>struct v4l2_fmtdesc</code>.
934 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
936 if (!v4l2object->formats)
937 gst_v4l2_object_fill_format_list (v4l2object);
938 return v4l2object->formats;
943 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
945 GstStructure *structure = NULL;
948 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
949 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
950 structure = gst_structure_new ("image/jpeg", NULL);
952 case V4L2_PIX_FMT_RGB332:
953 case V4L2_PIX_FMT_RGB555:
954 case V4L2_PIX_FMT_RGB555X:
955 case V4L2_PIX_FMT_RGB565:
956 case V4L2_PIX_FMT_RGB565X:
957 case V4L2_PIX_FMT_RGB24:
958 case V4L2_PIX_FMT_BGR24:
959 case V4L2_PIX_FMT_RGB32:
960 case V4L2_PIX_FMT_BGR32:{
961 guint depth = 0, bpp = 0;
965 guint32 r_mask = 0, b_mask = 0, g_mask = 0;
968 case V4L2_PIX_FMT_RGB332:
970 endianness = G_BYTE_ORDER; /* 'like, whatever' */
975 case V4L2_PIX_FMT_RGB555:
976 case V4L2_PIX_FMT_RGB555X:
980 fourcc == V4L2_PIX_FMT_RGB555X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
985 case V4L2_PIX_FMT_RGB565:
986 case V4L2_PIX_FMT_RGB565X:
989 fourcc == V4L2_PIX_FMT_RGB565X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
994 case V4L2_PIX_FMT_RGB24:
996 endianness = G_BIG_ENDIAN;
1001 case V4L2_PIX_FMT_BGR24:
1003 endianness = G_BIG_ENDIAN;
1008 case V4L2_PIX_FMT_RGB32:
1010 endianness = G_BIG_ENDIAN;
1011 r_mask = 0xff000000;
1012 g_mask = 0x00ff0000;
1013 b_mask = 0x0000ff00;
1015 case V4L2_PIX_FMT_BGR32:
1017 endianness = G_BIG_ENDIAN;
1018 r_mask = 0x000000ff;
1019 g_mask = 0x0000ff00;
1020 b_mask = 0x00ff0000;
1023 g_assert_not_reached ();
1026 structure = gst_structure_new ("video/x-raw-rgb",
1027 "bpp", G_TYPE_INT, bpp,
1028 "depth", G_TYPE_INT, depth,
1029 "red_mask", G_TYPE_INT, r_mask,
1030 "green_mask", G_TYPE_INT, g_mask,
1031 "blue_mask", G_TYPE_INT, b_mask,
1032 "endianness", G_TYPE_INT, endianness, NULL);
1035 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1036 structure = gst_structure_new ("video/x-raw-gray",
1037 "bpp", G_TYPE_INT, 8, NULL);
1039 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1040 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1041 /* FIXME: get correct fourccs here */
1043 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1044 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1045 case V4L2_PIX_FMT_YVU410:
1046 case V4L2_PIX_FMT_YUV410:
1047 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1048 case V4L2_PIX_FMT_YUYV:
1049 case V4L2_PIX_FMT_YVU420:
1050 case V4L2_PIX_FMT_UYVY:
1051 case V4L2_PIX_FMT_Y41P:
1052 case V4L2_PIX_FMT_YUV422P:
1053 #ifdef V4L2_PIX_FMT_YVYU
1054 case V4L2_PIX_FMT_YVYU:
1056 case V4L2_PIX_FMT_YUV411P:{
1060 case V4L2_PIX_FMT_NV12:
1061 fcc = GST_MAKE_FOURCC ('N', 'V', '1', '2');
1063 case V4L2_PIX_FMT_NV21:
1064 fcc = GST_MAKE_FOURCC ('N', 'V', '2', '1');
1066 case V4L2_PIX_FMT_YVU410:
1067 fcc = GST_MAKE_FOURCC ('Y', 'V', 'U', '9');
1069 case V4L2_PIX_FMT_YUV410:
1070 fcc = GST_MAKE_FOURCC ('Y', 'U', 'V', '9');
1072 case V4L2_PIX_FMT_YUV420:
1073 fcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
1075 case V4L2_PIX_FMT_YUYV:
1076 fcc = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
1078 case V4L2_PIX_FMT_YVU420:
1079 fcc = GST_MAKE_FOURCC ('Y', 'V', '1', '2');
1081 case V4L2_PIX_FMT_UYVY:
1082 fcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
1084 case V4L2_PIX_FMT_Y41P:
1085 fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'P');
1087 case V4L2_PIX_FMT_YUV411P:
1088 fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
1090 case V4L2_PIX_FMT_YUV422P:
1091 fcc = GST_MAKE_FOURCC ('Y', '4', '2', 'B');
1093 #ifdef V4L2_PIX_FMT_YVYU
1094 case V4L2_PIX_FMT_YVYU:
1095 fcc = GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U');
1099 g_assert_not_reached ();
1102 structure = gst_structure_new ("video/x-raw-yuv",
1103 "format", GST_TYPE_FOURCC, fcc, NULL);
1106 case V4L2_PIX_FMT_DV:
1108 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1111 case V4L2_PIX_FMT_MPEG: /* MPEG */
1112 structure = gst_structure_new ("video/mpegts", NULL);
1114 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1116 #ifdef V4L2_PIX_FMT_SBGGR8
1117 case V4L2_PIX_FMT_SBGGR8:
1118 structure = gst_structure_new ("video/x-raw-bayer", NULL);
1121 #ifdef V4L2_PIX_FMT_SN9C10X
1122 case V4L2_PIX_FMT_SN9C10X:
1123 structure = gst_structure_new ("video/x-sonix", NULL);
1126 #ifdef V4L2_PIX_FMT_PWC1
1127 case V4L2_PIX_FMT_PWC1:
1128 structure = gst_structure_new ("video/x-pwc1", NULL);
1131 #ifdef V4L2_PIX_FMT_PWC2
1132 case V4L2_PIX_FMT_PWC2:
1133 structure = gst_structure_new ("video/x-pwc2", NULL);
1137 GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
1138 fourcc, GST_FOURCC_ARGS (fourcc));
1148 gst_v4l2_object_get_all_caps (void)
1150 static GstCaps *caps = NULL;
1153 GstStructure *structure;
1157 caps = gst_caps_new_empty ();
1158 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1160 gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
1162 if (gst_v4l2_formats[i].dimensions) {
1163 gst_structure_set (structure,
1164 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1165 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1166 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
1168 gst_caps_append_structure (caps, structure);
1173 return gst_caps_ref (caps);
1177 /* collect data for the given caps
1178 * @caps: given input caps
1179 * @format: location for the v4l format
1180 * @w/@h: location for width and height
1181 * @fps_n/@fps_d: location for framerate
1182 * @size: location for expected size of the frame or 0 if unknown
1185 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1186 struct v4l2_fmtdesc ** format, gint * w, gint * h, guint * fps_n,
1187 guint * fps_d, guint * size)
1189 GstStructure *structure;
1190 const GValue *framerate;
1192 const gchar *mimetype;
1195 /* default unknown values */
1199 structure = gst_caps_get_structure (caps, 0);
1201 mimetype = gst_structure_get_name (structure);
1203 if (strcmp (mimetype, "video/mpegts") == 0) {
1204 fourcc = V4L2_PIX_FMT_MPEG;
1211 if (!gst_structure_get_int (structure, "width", w))
1214 if (!gst_structure_get_int (structure, "height", h))
1217 framerate = gst_structure_get_value (structure, "framerate");
1221 *fps_n = gst_value_get_fraction_numerator (framerate);
1222 *fps_d = gst_value_get_fraction_denominator (framerate);
1224 if (!strcmp (mimetype, "video/x-raw-yuv")) {
1225 gst_structure_get_fourcc (structure, "format", &fourcc);
1228 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
1229 case GST_MAKE_FOURCC ('I', 'Y', 'U', 'V'):
1230 fourcc = V4L2_PIX_FMT_YUV420;
1231 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1232 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1234 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
1235 fourcc = V4L2_PIX_FMT_YUYV;
1236 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1238 case GST_MAKE_FOURCC ('Y', '4', '1', 'P'):
1239 fourcc = V4L2_PIX_FMT_Y41P;
1240 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1242 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
1243 fourcc = V4L2_PIX_FMT_UYVY;
1244 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1246 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
1247 fourcc = V4L2_PIX_FMT_YVU420;
1248 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1249 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1251 case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
1252 fourcc = V4L2_PIX_FMT_YUV411P;
1253 outsize = GST_ROUND_UP_4 (*w) * *h;
1254 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 4) * *h);
1256 case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
1257 fourcc = V4L2_PIX_FMT_YUV422P;
1258 outsize = GST_ROUND_UP_4 (*w) * *h;
1259 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * *h);
1261 case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
1262 fourcc = V4L2_PIX_FMT_NV12;
1263 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1264 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1266 case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
1267 fourcc = V4L2_PIX_FMT_NV21;
1268 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1269 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1271 #ifdef V4L2_PIX_FMT_YVYU
1272 case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
1273 fourcc = V4L2_PIX_FMT_YVYU;
1274 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1278 } else if (!strcmp (mimetype, "video/x-raw-rgb")) {
1279 gint depth, endianness, r_mask;
1281 gst_structure_get_int (structure, "depth", &depth);
1282 gst_structure_get_int (structure, "endianness", &endianness);
1283 gst_structure_get_int (structure, "red_mask", &r_mask);
1287 fourcc = V4L2_PIX_FMT_RGB332;
1290 fourcc = (endianness == G_LITTLE_ENDIAN) ?
1291 V4L2_PIX_FMT_RGB555 : V4L2_PIX_FMT_RGB555X;
1294 fourcc = (endianness == G_LITTLE_ENDIAN) ?
1295 V4L2_PIX_FMT_RGB565 : V4L2_PIX_FMT_RGB565X;
1298 fourcc = (r_mask == 0xFF) ? V4L2_PIX_FMT_BGR24 : V4L2_PIX_FMT_RGB24;
1301 fourcc = (r_mask == 0xFF) ? V4L2_PIX_FMT_BGR32 : V4L2_PIX_FMT_RGB32;
1304 } else if (strcmp (mimetype, "video/x-dv") == 0) {
1305 fourcc = V4L2_PIX_FMT_DV;
1306 } else if (strcmp (mimetype, "image/jpeg") == 0) {
1307 fourcc = V4L2_PIX_FMT_JPEG;
1308 #ifdef V4L2_PIX_FMT_SBGGR8
1309 } else if (strcmp (mimetype, "video/x-raw-bayer") == 0) {
1310 fourcc = V4L2_PIX_FMT_SBGGR8;
1312 #ifdef V4L2_PIX_FMT_SN9C10X
1313 } else if (strcmp (mimetype, "video/x-sonix") == 0) {
1314 fourcc = V4L2_PIX_FMT_SN9C10X;
1316 #ifdef V4L2_PIX_FMT_PWC1
1317 } else if (strcmp (mimetype, "video/x-pwc1") == 0) {
1318 fourcc = V4L2_PIX_FMT_PWC1;
1320 #ifdef V4L2_PIX_FMT_PWC2
1321 } else if (strcmp (mimetype, "video/x-pwc2") == 0) {
1322 fourcc = V4L2_PIX_FMT_PWC2;
1324 } else if (strcmp (mimetype, "video/x-raw-gray") == 0) {
1325 fourcc = V4L2_PIX_FMT_GREY;
1332 *format = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1340 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1341 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1342 static GstStructure *
1343 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1344 guint32 pixelformat,
1345 guint32 width, guint32 height, const GstStructure * template)
1347 gint fd = v4l2object->video_fd;
1348 struct v4l2_frmivalenum ival;
1351 GValue rates = { 0, };
1353 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1355 ival.pixel_format = pixelformat;
1357 ival.height = height;
1359 GST_LOG_OBJECT (v4l2object->element,
1360 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1361 GST_FOURCC_ARGS (pixelformat));
1363 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1364 * fraction to get framerate */
1365 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1366 goto enum_frameintervals_failed;
1368 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1369 GValue rate = { 0, };
1371 g_value_init (&rates, GST_TYPE_LIST);
1372 g_value_init (&rate, GST_TYPE_FRACTION);
1375 num = ival.discrete.numerator;
1376 denom = ival.discrete.denominator;
1378 if (num > G_MAXINT || denom > G_MAXINT) {
1379 /* let us hope we don't get here... */
1384 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1387 /* swap to get the framerate */
1388 gst_value_set_fraction (&rate, denom, num);
1389 gst_value_list_append_value (&rates, &rate);
1392 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1393 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1394 GValue min = { 0, };
1395 GValue step = { 0, };
1396 GValue max = { 0, };
1397 gboolean added = FALSE;
1398 guint32 minnum, mindenom;
1399 guint32 maxnum, maxdenom;
1401 g_value_init (&rates, GST_TYPE_LIST);
1403 g_value_init (&min, GST_TYPE_FRACTION);
1404 g_value_init (&step, GST_TYPE_FRACTION);
1405 g_value_init (&max, GST_TYPE_FRACTION);
1408 minnum = ival.stepwise.min.numerator;
1409 mindenom = ival.stepwise.min.denominator;
1410 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1414 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1416 gst_value_set_fraction (&min, minnum, mindenom);
1419 maxnum = ival.stepwise.max.numerator;
1420 maxdenom = ival.stepwise.max.denominator;
1421 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1426 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1428 gst_value_set_fraction (&max, maxnum, maxdenom);
1431 num = ival.stepwise.step.numerator;
1432 denom = ival.stepwise.step.denominator;
1433 if (num > G_MAXINT || denom > G_MAXINT) {
1438 if (num == 0 || denom == 0) {
1439 /* in this case we have a wrong fraction or no step, set the step to max
1440 * so that we only add the min value in the loop below */
1445 /* since we only have gst_value_fraction_subtract and not add, negate the
1447 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1449 gst_value_set_fraction (&step, -num, denom);
1451 while (gst_value_compare (&min, &max) <= 0) {
1452 GValue rate = { 0, };
1454 num = gst_value_get_fraction_numerator (&min);
1455 denom = gst_value_get_fraction_denominator (&min);
1456 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1459 /* invert to get the framerate */
1460 g_value_init (&rate, GST_TYPE_FRACTION);
1461 gst_value_set_fraction (&rate, denom, num);
1462 gst_value_list_append_value (&rates, &rate);
1465 /* we're actually adding because step was negated above. This is because
1466 * there is no _add function... */
1467 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1468 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1473 /* no range was added, leave the default range from the template */
1474 GST_WARNING_OBJECT (v4l2object->element,
1475 "no range added, leaving default");
1476 g_value_unset (&rates);
1478 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1479 guint32 maxnum, maxdenom;
1481 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1483 num = ival.stepwise.min.numerator;
1484 denom = ival.stepwise.min.denominator;
1485 if (num > G_MAXINT || denom > G_MAXINT) {
1490 maxnum = ival.stepwise.max.numerator;
1491 maxdenom = ival.stepwise.max.denominator;
1492 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1497 GST_LOG_OBJECT (v4l2object->element,
1498 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
1501 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1507 s = gst_structure_copy (template);
1508 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
1509 "height", G_TYPE_INT, (gint) height, NULL);
1511 if (G_IS_VALUE (&rates)) {
1512 /* only change the framerate on the template when we have a valid probed new
1514 gst_structure_set_value (s, "framerate", &rates);
1515 g_value_unset (&rates);
1517 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
1523 enum_frameintervals_failed:
1525 GST_DEBUG_OBJECT (v4l2object->element,
1526 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
1527 GST_FOURCC_ARGS (pixelformat), width, height);
1532 /* I don't see how this is actually an error, we ignore the format then */
1533 GST_WARNING_OBJECT (v4l2object->element,
1534 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
1535 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
1539 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
1541 #ifdef VIDIOC_ENUM_FRAMESIZES
1543 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
1547 gst_structure_get_int (s1, "width", &w1);
1548 gst_structure_get_int (s1, "height", &h1);
1549 gst_structure_get_int (s2, "width", &w2);
1550 gst_structure_get_int (s2, "height", &h2);
1552 /* I think it's safe to assume that this won't overflow for a while */
1553 return ((w2 * h2) - (w1 * h1));
1558 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1559 guint32 pixelformat, gint * width, gint * height);
1562 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
1563 guint32 pixelformat, const GstStructure * template)
1565 GstCaps *ret = gst_caps_new_empty ();
1568 #ifdef VIDIOC_ENUM_FRAMESIZES
1569 gint fd = v4l2object->video_fd;
1570 struct v4l2_frmsizeenum size;
1571 GList *results = NULL;
1574 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1575 return gst_caps_new_simple ("video/mpegts", NULL);
1577 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
1579 size.pixel_format = pixelformat;
1581 GST_DEBUG_OBJECT (v4l2object->element, "Enumerating frame sizes");
1583 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
1584 goto enum_framesizes_failed;
1586 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1588 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
1589 size.discrete.width, size.discrete.height);
1591 w = MIN (size.discrete.width, G_MAXINT);
1592 h = MIN (size.discrete.height, G_MAXINT);
1596 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1597 pixelformat, w, h, template);
1600 results = g_list_prepend (results, tmp);
1604 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
1605 GST_DEBUG_OBJECT (v4l2object->element,
1606 "done iterating discrete frame sizes");
1607 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
1608 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
1609 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1610 size.stepwise.min_width);
1611 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1612 size.stepwise.min_height);
1613 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1614 size.stepwise.max_width);
1615 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1616 size.stepwise.max_height);
1617 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
1618 size.stepwise.step_width);
1619 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
1620 size.stepwise.step_height);
1622 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
1623 w < size.stepwise.max_width && h < size.stepwise.max_height;
1624 w += size.stepwise.step_width, h += size.stepwise.step_height) {
1625 if (w == 0 || h == 0)
1629 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1630 pixelformat, w, h, template);
1633 results = g_list_prepend (results, tmp);
1635 GST_DEBUG_OBJECT (v4l2object->element,
1636 "done iterating stepwise frame sizes");
1637 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
1640 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
1641 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1642 size.stepwise.min_width);
1643 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1644 size.stepwise.min_height);
1645 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1646 size.stepwise.max_width);
1647 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1648 size.stepwise.max_height);
1650 w = MAX (size.stepwise.min_width, 1);
1651 h = MAX (size.stepwise.min_height, 1);
1652 maxw = MIN (size.stepwise.max_width, G_MAXINT);
1653 maxh = MIN (size.stepwise.max_height, G_MAXINT);
1656 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
1659 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
1660 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
1663 /* no point using the results list here, since there's only one struct */
1664 gst_caps_append_structure (ret, tmp);
1670 /* we use an intermediary list to store and then sort the results of the
1671 * probing because we can't make any assumptions about the order in which
1672 * the driver will give us the sizes, but we want the final caps to contain
1673 * the results starting with the highest resolution and having the lowest
1674 * resolution last, since order in caps matters for things like fixation. */
1675 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
1676 while (results != NULL) {
1677 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
1678 results = g_list_delete_link (results, results);
1681 if (gst_caps_is_empty (ret))
1682 goto enum_framesizes_no_results;
1687 enum_framesizes_failed:
1689 /* I don't see how this is actually an error */
1690 GST_DEBUG_OBJECT (v4l2object->element,
1691 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
1692 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
1693 goto default_frame_sizes;
1695 enum_framesizes_no_results:
1697 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
1698 * question doesn't actually support it yet */
1699 GST_DEBUG_OBJECT (v4l2object->element,
1700 "No results for pixelformat %" GST_FOURCC_FORMAT
1701 " enumerating frame sizes, trying fallback",
1702 GST_FOURCC_ARGS (pixelformat));
1703 goto default_frame_sizes;
1707 GST_WARNING_OBJECT (v4l2object->element,
1708 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
1709 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
1710 goto default_frame_sizes;
1712 default_frame_sizes:
1713 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
1715 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
1717 /* This code is for Linux < 2.6.19 */
1719 max_w = max_h = GST_V4L2_MAX_SIZE;
1720 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
1722 GST_WARNING_OBJECT (v4l2object->element,
1723 "Could not probe minimum capture size for pixelformat %"
1724 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1726 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
1728 GST_WARNING_OBJECT (v4l2object->element,
1729 "Could not probe maximum capture size for pixelformat %"
1730 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1733 /* Since we can't get framerate directly, try to use the current norm */
1734 if (v4l2object->norm && v4l2object->norms) {
1736 GstTunerNorm *norm = NULL;
1738 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
1739 norm = (GstTunerNorm *) norms->data;
1740 if (!strcmp (norm->label, v4l2object->norm))
1743 /* If it's possible, set framerate to that (discrete) value */
1745 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
1746 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
1750 tmp = gst_structure_copy (template);
1752 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
1755 /* if norm can't be used, copy the template framerate */
1756 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
1761 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
1763 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
1766 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
1768 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
1770 gst_caps_append_structure (ret, tmp);
1777 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1778 guint32 pixelformat, gint * width, gint * height)
1780 struct v4l2_format fmt;
1784 g_return_val_if_fail (width != NULL, FALSE);
1785 g_return_val_if_fail (height != NULL, FALSE);
1787 GST_LOG_OBJECT (v4l2object->element,
1788 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
1789 *width, *height, GST_FOURCC_ARGS (pixelformat));
1791 fd = v4l2object->video_fd;
1793 /* get size delimiters */
1794 memset (&fmt, 0, sizeof (fmt));
1795 fmt.type = v4l2object->type;
1796 fmt.fmt.pix.width = *width;
1797 fmt.fmt.pix.height = *height;
1798 fmt.fmt.pix.pixelformat = pixelformat;
1799 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1801 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
1802 if (r < 0 && errno == EINVAL) {
1803 /* try again with progressive video */
1804 fmt.fmt.pix.width = *width;
1805 fmt.fmt.pix.height = *height;
1806 fmt.fmt.pix.pixelformat = pixelformat;
1807 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1808 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
1812 /* The driver might not implement TRY_FMT, in which case we will try
1814 if (errno != ENOTTY)
1817 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
1818 be, because we're still probing */
1819 if (GST_V4L2_IS_ACTIVE (v4l2object))
1822 GST_LOG_OBJECT (v4l2object->element,
1823 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
1825 fmt.fmt.pix.width = *width;
1826 fmt.fmt.pix.height = *height;
1828 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
1829 if (r < 0 && errno == EINVAL) {
1830 /* try again with progressive video */
1831 fmt.fmt.pix.width = *width;
1832 fmt.fmt.pix.height = *height;
1833 fmt.fmt.pix.pixelformat = pixelformat;
1834 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1835 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
1842 GST_LOG_OBJECT (v4l2object->element,
1843 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
1845 *width = fmt.fmt.pix.width;
1846 *height = fmt.fmt.pix.height;
1853 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, guint32 pixelformat,
1854 guint32 width, guint32 height)
1856 gint fd = v4l2object->video_fd;
1857 struct v4l2_format format;
1859 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
1860 "%" GST_FOURCC_FORMAT, width, height, GST_FOURCC_ARGS (pixelformat));
1862 GST_V4L2_CHECK_OPEN (v4l2object);
1863 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
1865 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1868 memset (&format, 0x00, sizeof (struct v4l2_format));
1869 format.type = v4l2object->type;
1871 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
1872 goto get_fmt_failed;
1874 if (format.type == v4l2object->type &&
1875 format.fmt.pix.width == width &&
1876 format.fmt.pix.height == height &&
1877 format.fmt.pix.pixelformat == pixelformat) {
1878 /* Nothing to do. We want to succeed immediately
1879 * here because setting the same format back
1880 * can still fail due to EBUSY. By short-circuiting
1881 * here, we allow pausing and re-playing pipelines
1882 * with changed caps, as long as the changed caps
1883 * do not change the webcam's format. Otherwise,
1884 * any caps change would require us to go to NULL
1885 * state to close the device and set format.
1890 format.type = v4l2object->type;
1891 format.fmt.pix.width = width;
1892 format.fmt.pix.height = height;
1893 format.fmt.pix.pixelformat = pixelformat;
1894 /* FIXME: request whole frames; need to use gstreamer interlace support
1895 * (INTERLACED mode returns frames where the fields have already been
1896 * combined, there are other modes for requesting fields individually) */
1897 format.fmt.pix.field = V4L2_FIELD_INTERLACED;
1899 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0) {
1900 /* we might also get EBUSY here */
1901 if (errno != EINVAL)
1902 goto set_fmt_failed;
1904 GST_DEBUG_OBJECT (v4l2object->element, "trying again...");
1906 /* try again with progressive video */
1907 format.fmt.pix.width = width;
1908 format.fmt.pix.height = height;
1909 format.fmt.pix.pixelformat = pixelformat;
1910 format.fmt.pix.field = V4L2_FIELD_NONE;
1911 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
1912 goto set_fmt_failed;
1915 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
1916 goto invalid_dimensions;
1918 if (format.fmt.pix.pixelformat != pixelformat)
1919 goto invalid_pixelformat;
1926 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1927 (_("Device '%s' does not support video capture"),
1928 v4l2object->videodev),
1929 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
1934 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1935 (_("Device '%s' cannot capture at %dx%d"),
1936 v4l2object->videodev, width, height),
1937 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
1938 GST_FOURCC_ARGS (pixelformat), width, height, g_strerror (errno)));
1943 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1944 (_("Device '%s' cannot capture at %dx%d"),
1945 v4l2object->videodev, width, height),
1946 ("Tried to capture at %dx%d, but device returned size %dx%d",
1947 width, height, format.fmt.pix.width, format.fmt.pix.height));
1950 invalid_pixelformat:
1952 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1953 (_("Device '%s' cannot capture in the specified format"),
1954 v4l2object->videodev),
1955 ("Tried to capture in %" GST_FOURCC_FORMAT
1956 ", but device returned format" " %" GST_FOURCC_FORMAT,
1957 GST_FOURCC_ARGS (pixelformat),
1958 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
1964 gst_v4l2_object_start_streaming (GstV4l2Object * v4l2object)
1966 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_STREAMON,
1967 &(v4l2object->type)) < 0) {
1968 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ,
1969 (_("Error starting streaming on device '%s'."), v4l2object->videodev),
1977 gst_v4l2_object_stop_streaming (GstV4l2Object * v4l2object)
1979 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_STREAMOFF,
1980 &(v4l2object->type)) < 0) {
1981 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ,
1982 (_("Error stopping streaming on device '%s'."), v4l2object->videodev),