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"
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));
333 * GstV4l2Src:brightness
335 * Picture brightness, or more precisely, the black level
339 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
340 g_param_spec_int ("brightness", "Brightness",
341 "Picture brightness, or more precisely, the black level", G_MININT,
343 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
345 * GstV4l2Src:contrast
347 * Picture contrast or luma gain
351 g_object_class_install_property (gobject_class, PROP_CONTRAST,
352 g_param_spec_int ("contrast", "Contrast",
353 "Picture contrast or luma gain", G_MININT,
355 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
357 * GstV4l2Src:saturation
359 * Picture color saturation or chroma gain
363 g_object_class_install_property (gobject_class, PROP_SATURATION,
364 g_param_spec_int ("saturation", "Saturation",
365 "Picture color saturation or chroma gain", G_MININT,
367 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
371 * Hue or color balance
375 g_object_class_install_property (gobject_class, PROP_HUE,
376 g_param_spec_int ("hue", "Hue",
377 "Hue or color balance", G_MININT,
379 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
383 gst_v4l2_object_new (GstElement * element,
384 enum v4l2_buf_type type,
385 const char *default_device,
386 GstV4l2GetInOutFunction get_in_out_func,
387 GstV4l2SetInOutFunction set_in_out_func,
388 GstV4l2UpdateFpsFunction update_fps_func)
390 GstV4l2Object *v4l2object;
393 * some default values
395 v4l2object = g_new0 (GstV4l2Object, 1);
397 v4l2object->type = type;
398 v4l2object->formats = NULL;
400 v4l2object->element = element;
401 v4l2object->get_in_out_func = get_in_out_func;
402 v4l2object->set_in_out_func = set_in_out_func;
403 v4l2object->update_fps_func = update_fps_func;
405 v4l2object->video_fd = -1;
406 v4l2object->poll = gst_poll_new (TRUE);
407 v4l2object->buffer = NULL;
408 v4l2object->videodev = g_strdup (default_device);
410 v4l2object->norms = NULL;
411 v4l2object->channels = NULL;
412 v4l2object->colors = NULL;
414 v4l2object->xwindow_id = 0;
419 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
423 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
425 g_return_if_fail (v4l2object != NULL);
427 if (v4l2object->videodev)
428 g_free (v4l2object->videodev);
430 if (v4l2object->poll)
431 gst_poll_free (v4l2object->poll);
433 if (v4l2object->channel)
434 g_free (v4l2object->channel);
436 if (v4l2object->norm)
437 g_free (v4l2object->norm);
439 if (v4l2object->formats) {
440 gst_v4l2_object_clear_format_list (v4l2object);
448 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
450 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
451 g_slist_free (v4l2object->formats);
452 v4l2object->formats = NULL;
458 gst_v4l2_object_prop_to_cid (guint prop_id)
463 case PROP_BRIGHTNESS:
464 cid = V4L2_CID_BRIGHTNESS;
467 cid = V4L2_CID_CONTRAST;
469 case PROP_SATURATION:
470 cid = V4L2_CID_SATURATION;
476 GST_WARNING ("unmapped property id: %d", prop_id);
483 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
484 guint prop_id, const GValue * value, GParamSpec * pspec)
488 g_free (v4l2object->videodev);
489 v4l2object->videodev = g_value_dup_string (value);
491 case PROP_BRIGHTNESS:
493 case PROP_SATURATION:
496 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
499 if (GST_V4L2_IS_OPEN (v4l2object)) {
500 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
508 if (GST_V4L2_IS_OPEN (v4l2object)) {
509 GstTuner *tuner = GST_TUNER (v4l2object->element);
510 GstTunerNorm *norm = gst_tuner_find_norm_by_name (tuner,
511 (gchar *) g_value_get_string (value));
514 /* like gst_tuner_set_norm (tuner, norm)
515 without g_object_notify */
516 gst_v4l2_tuner_set_norm (v4l2object, norm);
519 g_free (v4l2object->norm);
520 v4l2object->norm = g_value_dup_string (value);
524 if (GST_V4L2_IS_OPEN (v4l2object)) {
525 GstTuner *tuner = GST_TUNER (v4l2object->element);
526 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
527 (gchar *) g_value_get_string (value));
530 /* like gst_tuner_set_channel (tuner, channel)
531 without g_object_notify */
532 gst_v4l2_tuner_set_channel (v4l2object, channel);
535 g_free (v4l2object->channel);
536 v4l2object->channel = g_value_dup_string (value);
540 if (GST_V4L2_IS_OPEN (v4l2object)) {
541 GstTuner *tuner = GST_TUNER (v4l2object->element);
542 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
545 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
547 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
548 without g_object_notify */
549 gst_v4l2_tuner_set_frequency (v4l2object, channel,
550 g_value_get_ulong (value));
553 v4l2object->frequency = g_value_get_ulong (value);
566 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
567 guint prop_id, GValue * value, GParamSpec * pspec)
571 g_value_set_string (value, v4l2object->videodev);
573 case PROP_DEVICE_NAME:
575 const guchar *new = NULL;
577 if (GST_V4L2_IS_OPEN (v4l2object)) {
578 new = v4l2object->vcap.card;
579 } else if (gst_v4l2_open (v4l2object)) {
580 new = v4l2object->vcap.card;
581 gst_v4l2_close (v4l2object);
583 g_value_set_string (value, (gchar *) new);
588 if (GST_V4L2_IS_OPEN (v4l2object))
589 g_value_set_int (value, v4l2object->video_fd);
591 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
598 if (GST_V4L2_IS_OPEN (v4l2object)) {
599 flags |= v4l2object->vcap.capabilities &
600 (V4L2_CAP_VIDEO_CAPTURE |
601 V4L2_CAP_VIDEO_OUTPUT |
602 V4L2_CAP_VIDEO_OVERLAY |
603 V4L2_CAP_VBI_CAPTURE |
604 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
606 g_value_set_flags (value, flags);
609 case PROP_BRIGHTNESS:
611 case PROP_SATURATION:
614 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
617 if (GST_V4L2_IS_OPEN (v4l2object)) {
619 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
620 g_value_set_int (value, v);
635 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
637 GstTunerNorm *norm = NULL;
638 GstTunerChannel *channel = NULL;
641 if (!GST_IS_TUNER (v4l2object->element))
644 tuner = GST_TUNER (v4l2object->element);
646 if (v4l2object->norm)
647 norm = gst_tuner_find_norm_by_name (tuner, v4l2object->norm);
649 gst_tuner_set_norm (tuner, norm);
652 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
654 g_free (v4l2object->norm);
655 v4l2object->norm = g_strdup (norm->label);
656 gst_tuner_norm_changed (tuner, norm);
660 if (v4l2object->channel)
661 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
663 gst_tuner_set_channel (tuner, channel);
666 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
667 (v4l2object->element)));
669 g_free (v4l2object->channel);
670 v4l2object->channel = g_strdup (channel->label);
671 gst_tuner_channel_changed (tuner, channel);
676 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
677 if (v4l2object->frequency != 0) {
678 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
680 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
681 if (v4l2object->frequency == 0) {
683 gst_tuner_set_frequency (tuner, channel, 1000);
691 gst_v4l2_object_start (GstV4l2Object * v4l2object)
693 if (gst_v4l2_open (v4l2object))
694 gst_v4l2_set_defaults (v4l2object);
699 gst_v4l2_xoverlay_start (v4l2object);
706 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
709 gst_v4l2_xoverlay_stop (v4l2object);
712 if (!gst_v4l2_close (v4l2object))
715 if (v4l2object->formats) {
716 gst_v4l2_object_clear_format_list (v4l2object);
724 * common format / caps utilities:
732 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
733 /* from Linux 2.6.15 videodev2.h */
734 {V4L2_PIX_FMT_RGB332, TRUE},
735 {V4L2_PIX_FMT_RGB555, TRUE},
736 {V4L2_PIX_FMT_RGB565, TRUE},
737 {V4L2_PIX_FMT_RGB555X, TRUE},
738 {V4L2_PIX_FMT_RGB565X, TRUE},
739 {V4L2_PIX_FMT_BGR24, TRUE},
740 {V4L2_PIX_FMT_RGB24, TRUE},
741 {V4L2_PIX_FMT_BGR32, TRUE},
742 {V4L2_PIX_FMT_RGB32, TRUE},
743 {V4L2_PIX_FMT_GREY, TRUE},
744 {V4L2_PIX_FMT_YVU410, TRUE},
745 {V4L2_PIX_FMT_YVU420, TRUE},
746 {V4L2_PIX_FMT_YUYV, TRUE},
747 {V4L2_PIX_FMT_UYVY, TRUE},
748 {V4L2_PIX_FMT_YUV422P, TRUE},
749 {V4L2_PIX_FMT_YUV411P, TRUE},
750 {V4L2_PIX_FMT_Y41P, TRUE},
752 /* two planes -- one Y, one Cr + Cb interleaved */
753 {V4L2_PIX_FMT_NV12, TRUE},
754 {V4L2_PIX_FMT_NV21, TRUE},
756 /* The following formats are not defined in the V4L2 specification */
757 {V4L2_PIX_FMT_YUV410, TRUE},
758 {V4L2_PIX_FMT_YUV420, TRUE},
759 {V4L2_PIX_FMT_YYUV, TRUE},
760 {V4L2_PIX_FMT_HI240, TRUE},
762 /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
763 #ifdef V4L2_PIX_FMT_SBGGR8
764 {V4L2_PIX_FMT_SBGGR8, TRUE},
767 /* compressed formats */
768 {V4L2_PIX_FMT_MJPEG, TRUE},
769 {V4L2_PIX_FMT_JPEG, TRUE},
770 {V4L2_PIX_FMT_DV, TRUE},
771 {V4L2_PIX_FMT_MPEG, FALSE},
773 /* Vendor-specific formats */
774 {V4L2_PIX_FMT_WNVA, TRUE},
776 #ifdef V4L2_PIX_FMT_SN9C10X
777 {V4L2_PIX_FMT_SN9C10X, TRUE},
779 #ifdef V4L2_PIX_FMT_PWC1
780 {V4L2_PIX_FMT_PWC1, TRUE},
782 #ifdef V4L2_PIX_FMT_PWC2
783 {V4L2_PIX_FMT_PWC2, TRUE},
785 #ifdef V4L2_PIX_FMT_YVYU
786 {V4L2_PIX_FMT_YVYU, TRUE},
790 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
793 static struct v4l2_fmtdesc *
794 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
797 struct v4l2_fmtdesc *fmt;
803 walk = gst_v4l2_object_get_format_list (v4l2object);
805 fmt = (struct v4l2_fmtdesc *) walk->data;
806 if (fmt->pixelformat == fourcc)
808 /* special case for jpeg */
809 if ((fmt->pixelformat == V4L2_PIX_FMT_MJPEG && fourcc == V4L2_PIX_FMT_JPEG)
810 || (fmt->pixelformat == V4L2_PIX_FMT_JPEG
811 && fourcc == V4L2_PIX_FMT_MJPEG)) {
814 walk = g_slist_next (walk);
822 /* complete made up ranking, the values themselves are meaningless */
823 #define YUV_BASE_RANK 1000
824 #define JPEG_BASE_RANK 500
825 #define DV_BASE_RANK 200
826 #define RGB_BASE_RANK 100
827 #define YUV_ODD_BASE_RANK 50
828 #define RGB_ODD_BASE_RANK 25
829 #define BAYER_BASE_RANK 15
830 #define S910_BASE_RANK 10
831 #define GREY_BASE_RANK 5
832 #define PWC_BASE_RANK 1
834 /* This flag is already used by libv4l2 although
835 * it was added to the Linux kernel in 2.6.32
837 #ifndef V4L2_FMT_FLAG_EMULATED
838 #define V4L2_FMT_FLAG_EMULATED 0x0002
842 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
844 guint32 fourcc = fmt->pixelformat;
845 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
849 case V4L2_PIX_FMT_MJPEG:
850 rank = JPEG_BASE_RANK;
852 case V4L2_PIX_FMT_JPEG:
853 rank = JPEG_BASE_RANK + 1;
855 case V4L2_PIX_FMT_MPEG: /* MPEG */
856 rank = JPEG_BASE_RANK + 2;
859 case V4L2_PIX_FMT_RGB332:
860 case V4L2_PIX_FMT_RGB555:
861 case V4L2_PIX_FMT_RGB555X:
862 case V4L2_PIX_FMT_RGB565:
863 case V4L2_PIX_FMT_RGB565X:
864 rank = RGB_ODD_BASE_RANK;
867 case V4L2_PIX_FMT_RGB24:
868 case V4L2_PIX_FMT_BGR24:
869 rank = RGB_BASE_RANK - 1;
872 case V4L2_PIX_FMT_RGB32:
873 case V4L2_PIX_FMT_BGR32:
874 rank = RGB_BASE_RANK;
877 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
878 rank = GREY_BASE_RANK;
881 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
882 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
883 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
884 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
885 rank = YUV_ODD_BASE_RANK;
888 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
889 rank = YUV_BASE_RANK + 3;
891 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
892 rank = YUV_BASE_RANK + 2;
894 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
895 rank = YUV_BASE_RANK + 7;
897 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
898 rank = YUV_BASE_RANK + 10;
900 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
901 rank = YUV_BASE_RANK + 6;
903 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
904 rank = YUV_BASE_RANK + 9;
906 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
907 rank = YUV_BASE_RANK + 5;
909 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
910 rank = YUV_BASE_RANK + 4;
912 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
913 rank = YUV_BASE_RANK + 8;
916 case V4L2_PIX_FMT_DV:
920 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
924 #ifdef V4L2_PIX_FMT_SBGGR8
925 case V4L2_PIX_FMT_SBGGR8:
926 rank = BAYER_BASE_RANK;
930 #ifdef V4L2_PIX_FMT_SN9C10X
931 case V4L2_PIX_FMT_SN9C10X:
932 rank = S910_BASE_RANK;
936 #ifdef V4L2_PIX_FMT_PWC1
937 case V4L2_PIX_FMT_PWC1:
938 rank = PWC_BASE_RANK;
941 #ifdef V4L2_PIX_FMT_PWC2
942 case V4L2_PIX_FMT_PWC2:
943 rank = PWC_BASE_RANK;
952 /* All ranks are below 1<<15 so a shift by 15
953 * will a) make all non-emulated formats larger
954 * than emulated and b) will not overflow
965 format_cmp_func (gconstpointer a, gconstpointer b)
967 const struct v4l2_fmtdesc *fa = a;
968 const struct v4l2_fmtdesc *fb = b;
970 if (fa->pixelformat == fb->pixelformat)
973 return gst_v4l2_object_format_get_rank (fb) -
974 gst_v4l2_object_format_get_rank (fa);
977 /******************************************************
978 * gst_v4l2_object_fill_format_list():
979 * create list of supported capture formats
980 * return value: TRUE on success, FALSE on error
981 ******************************************************/
983 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object)
986 struct v4l2_fmtdesc *format;
988 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
990 /* format enumeration */
992 format = g_new0 (struct v4l2_fmtdesc, 1);
995 format->type = v4l2object->type;
997 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
998 if (errno == EINVAL) {
1000 break; /* end of enumeration */
1006 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1007 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1008 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1009 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1010 format->description);
1011 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1012 GST_FOURCC_ARGS (format->pixelformat));
1014 /* sort formats according to our preference; we do this, because caps
1015 * are probed in the order the formats are in the list, and the order of
1016 * formats in the final probed caps matters for things like fixation */
1017 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1018 (GCompareFunc) format_cmp_func);
1021 #ifndef GST_DISABLE_GST_DEBUG
1025 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1026 for (l = v4l2object->formats; l != NULL; l = l->next) {
1029 GST_INFO_OBJECT (v4l2object->element,
1030 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1031 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1041 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1042 (_("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)));
1049 * Get the list of supported capture formats, a list of
1050 * <code>struct v4l2_fmtdesc</code>.
1053 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1055 if (!v4l2object->formats)
1056 gst_v4l2_object_fill_format_list (v4l2object);
1057 return v4l2object->formats;
1062 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1064 GstStructure *structure = NULL;
1067 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1068 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1069 structure = gst_structure_new ("image/jpeg", NULL);
1071 case V4L2_PIX_FMT_RGB332:
1072 case V4L2_PIX_FMT_RGB555:
1073 case V4L2_PIX_FMT_RGB555X:
1074 case V4L2_PIX_FMT_RGB565:
1075 case V4L2_PIX_FMT_RGB565X:
1076 case V4L2_PIX_FMT_RGB24:
1077 case V4L2_PIX_FMT_BGR24:
1078 case V4L2_PIX_FMT_RGB32:
1079 case V4L2_PIX_FMT_BGR32:{
1080 guint depth = 0, bpp = 0;
1082 gint endianness = 0;
1084 guint32 r_mask = 0, b_mask = 0, g_mask = 0;
1087 case V4L2_PIX_FMT_RGB332:
1089 endianness = G_BYTE_ORDER; /* 'like, whatever' */
1094 case V4L2_PIX_FMT_RGB555:
1095 case V4L2_PIX_FMT_RGB555X:
1099 fourcc == V4L2_PIX_FMT_RGB555X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
1104 case V4L2_PIX_FMT_RGB565:
1105 case V4L2_PIX_FMT_RGB565X:
1108 fourcc == V4L2_PIX_FMT_RGB565X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
1113 case V4L2_PIX_FMT_RGB24:
1115 endianness = G_BIG_ENDIAN;
1120 case V4L2_PIX_FMT_BGR24:
1122 endianness = G_BIG_ENDIAN;
1127 case V4L2_PIX_FMT_RGB32:
1129 endianness = G_BIG_ENDIAN;
1130 r_mask = 0xff000000;
1131 g_mask = 0x00ff0000;
1132 b_mask = 0x0000ff00;
1134 case V4L2_PIX_FMT_BGR32:
1136 endianness = G_BIG_ENDIAN;
1137 r_mask = 0x000000ff;
1138 g_mask = 0x0000ff00;
1139 b_mask = 0x00ff0000;
1142 g_assert_not_reached ();
1145 structure = gst_structure_new ("video/x-raw-rgb",
1146 "bpp", G_TYPE_INT, bpp,
1147 "depth", G_TYPE_INT, depth,
1148 "red_mask", G_TYPE_INT, r_mask,
1149 "green_mask", G_TYPE_INT, g_mask,
1150 "blue_mask", G_TYPE_INT, b_mask,
1151 "endianness", G_TYPE_INT, endianness, NULL);
1154 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1155 structure = gst_structure_new ("video/x-raw-gray",
1156 "bpp", G_TYPE_INT, 8, NULL);
1158 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1159 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1160 /* FIXME: get correct fourccs here */
1162 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1163 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1164 case V4L2_PIX_FMT_YVU410:
1165 case V4L2_PIX_FMT_YUV410:
1166 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1167 case V4L2_PIX_FMT_YUYV:
1168 case V4L2_PIX_FMT_YVU420:
1169 case V4L2_PIX_FMT_UYVY:
1170 case V4L2_PIX_FMT_Y41P:
1171 case V4L2_PIX_FMT_YUV422P:
1172 #ifdef V4L2_PIX_FMT_YVYU
1173 case V4L2_PIX_FMT_YVYU:
1175 case V4L2_PIX_FMT_YUV411P:{
1179 case V4L2_PIX_FMT_NV12:
1180 fcc = GST_MAKE_FOURCC ('N', 'V', '1', '2');
1182 case V4L2_PIX_FMT_NV21:
1183 fcc = GST_MAKE_FOURCC ('N', 'V', '2', '1');
1185 case V4L2_PIX_FMT_YVU410:
1186 fcc = GST_MAKE_FOURCC ('Y', 'V', 'U', '9');
1188 case V4L2_PIX_FMT_YUV410:
1189 fcc = GST_MAKE_FOURCC ('Y', 'U', 'V', '9');
1191 case V4L2_PIX_FMT_YUV420:
1192 fcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
1194 case V4L2_PIX_FMT_YUYV:
1195 fcc = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
1197 case V4L2_PIX_FMT_YVU420:
1198 fcc = GST_MAKE_FOURCC ('Y', 'V', '1', '2');
1200 case V4L2_PIX_FMT_UYVY:
1201 fcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
1203 case V4L2_PIX_FMT_Y41P:
1204 fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'P');
1206 case V4L2_PIX_FMT_YUV411P:
1207 fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
1209 case V4L2_PIX_FMT_YUV422P:
1210 fcc = GST_MAKE_FOURCC ('Y', '4', '2', 'B');
1212 #ifdef V4L2_PIX_FMT_YVYU
1213 case V4L2_PIX_FMT_YVYU:
1214 fcc = GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U');
1218 g_assert_not_reached ();
1221 structure = gst_structure_new ("video/x-raw-yuv",
1222 "format", GST_TYPE_FOURCC, fcc, NULL);
1225 case V4L2_PIX_FMT_DV:
1227 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1230 case V4L2_PIX_FMT_MPEG: /* MPEG */
1231 structure = gst_structure_new ("video/mpegts", NULL);
1233 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1235 #ifdef V4L2_PIX_FMT_SBGGR8
1236 case V4L2_PIX_FMT_SBGGR8:
1237 structure = gst_structure_new ("video/x-raw-bayer", NULL);
1240 #ifdef V4L2_PIX_FMT_SN9C10X
1241 case V4L2_PIX_FMT_SN9C10X:
1242 structure = gst_structure_new ("video/x-sonix", NULL);
1245 #ifdef V4L2_PIX_FMT_PWC1
1246 case V4L2_PIX_FMT_PWC1:
1247 structure = gst_structure_new ("video/x-pwc1", NULL);
1250 #ifdef V4L2_PIX_FMT_PWC2
1251 case V4L2_PIX_FMT_PWC2:
1252 structure = gst_structure_new ("video/x-pwc2", NULL);
1256 GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
1257 fourcc, GST_FOURCC_ARGS (fourcc));
1267 gst_v4l2_object_get_all_caps (void)
1269 static GstCaps *caps = NULL;
1272 GstStructure *structure;
1276 caps = gst_caps_new_empty ();
1277 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1279 gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
1281 if (gst_v4l2_formats[i].dimensions) {
1282 gst_structure_set (structure,
1283 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1284 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1285 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
1287 gst_caps_append_structure (caps, structure);
1292 return gst_caps_ref (caps);
1296 /* collect data for the given caps
1297 * @caps: given input caps
1298 * @format: location for the v4l format
1299 * @w/@h: location for width and height
1300 * @fps_n/@fps_d: location for framerate
1301 * @size: location for expected size of the frame or 0 if unknown
1304 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1305 struct v4l2_fmtdesc ** format, gint * w, gint * h,
1306 gboolean * interlaced, guint * fps_n, guint * fps_d, guint * size)
1308 GstStructure *structure;
1309 const GValue *framerate;
1311 const gchar *mimetype;
1314 /* default unknown values */
1318 structure = gst_caps_get_structure (caps, 0);
1320 mimetype = gst_structure_get_name (structure);
1322 if (strcmp (mimetype, "video/mpegts") == 0) {
1323 fourcc = V4L2_PIX_FMT_MPEG;
1329 if (!gst_structure_get_int (structure, "width", w))
1332 if (!gst_structure_get_int (structure, "height", h))
1335 if (!gst_structure_get_boolean (structure, "interlaced", interlaced))
1336 *interlaced = FALSE;
1338 framerate = gst_structure_get_value (structure, "framerate");
1342 *fps_n = gst_value_get_fraction_numerator (framerate);
1343 *fps_d = gst_value_get_fraction_denominator (framerate);
1345 if (!strcmp (mimetype, "video/x-raw-yuv")) {
1346 gst_structure_get_fourcc (structure, "format", &fourcc);
1349 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
1350 case GST_MAKE_FOURCC ('I', 'Y', 'U', 'V'):
1351 fourcc = V4L2_PIX_FMT_YUV420;
1352 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1353 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1355 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
1356 fourcc = V4L2_PIX_FMT_YUYV;
1357 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1359 case GST_MAKE_FOURCC ('Y', '4', '1', 'P'):
1360 fourcc = V4L2_PIX_FMT_Y41P;
1361 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1363 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
1364 fourcc = V4L2_PIX_FMT_UYVY;
1365 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1367 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
1368 fourcc = V4L2_PIX_FMT_YVU420;
1369 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1370 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1372 case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
1373 fourcc = V4L2_PIX_FMT_YUV411P;
1374 outsize = GST_ROUND_UP_4 (*w) * *h;
1375 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 4) * *h);
1377 case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
1378 fourcc = V4L2_PIX_FMT_YUV422P;
1379 outsize = GST_ROUND_UP_4 (*w) * *h;
1380 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * *h);
1382 case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
1383 fourcc = V4L2_PIX_FMT_NV12;
1384 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1385 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1387 case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
1388 fourcc = V4L2_PIX_FMT_NV21;
1389 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1390 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1392 #ifdef V4L2_PIX_FMT_YVYU
1393 case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
1394 fourcc = V4L2_PIX_FMT_YVYU;
1395 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1399 } else if (!strcmp (mimetype, "video/x-raw-rgb")) {
1400 gint depth, endianness, r_mask;
1402 gst_structure_get_int (structure, "depth", &depth);
1403 gst_structure_get_int (structure, "endianness", &endianness);
1404 gst_structure_get_int (structure, "red_mask", &r_mask);
1408 fourcc = V4L2_PIX_FMT_RGB332;
1411 fourcc = (endianness == G_LITTLE_ENDIAN) ?
1412 V4L2_PIX_FMT_RGB555 : V4L2_PIX_FMT_RGB555X;
1415 fourcc = (endianness == G_LITTLE_ENDIAN) ?
1416 V4L2_PIX_FMT_RGB565 : V4L2_PIX_FMT_RGB565X;
1419 fourcc = (r_mask == 0xFF) ? V4L2_PIX_FMT_BGR24 : V4L2_PIX_FMT_RGB24;
1422 fourcc = (r_mask == 0xFF) ? V4L2_PIX_FMT_BGR32 : V4L2_PIX_FMT_RGB32;
1425 } else if (strcmp (mimetype, "video/x-dv") == 0) {
1426 fourcc = V4L2_PIX_FMT_DV;
1427 } else if (strcmp (mimetype, "image/jpeg") == 0) {
1428 fourcc = V4L2_PIX_FMT_JPEG;
1429 #ifdef V4L2_PIX_FMT_SBGGR8
1430 } else if (strcmp (mimetype, "video/x-raw-bayer") == 0) {
1431 fourcc = V4L2_PIX_FMT_SBGGR8;
1433 #ifdef V4L2_PIX_FMT_SN9C10X
1434 } else if (strcmp (mimetype, "video/x-sonix") == 0) {
1435 fourcc = V4L2_PIX_FMT_SN9C10X;
1437 #ifdef V4L2_PIX_FMT_PWC1
1438 } else if (strcmp (mimetype, "video/x-pwc1") == 0) {
1439 fourcc = V4L2_PIX_FMT_PWC1;
1441 #ifdef V4L2_PIX_FMT_PWC2
1442 } else if (strcmp (mimetype, "video/x-pwc2") == 0) {
1443 fourcc = V4L2_PIX_FMT_PWC2;
1445 } else if (strcmp (mimetype, "video/x-raw-gray") == 0) {
1446 fourcc = V4L2_PIX_FMT_GREY;
1453 *format = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1461 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1462 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced);
1465 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1466 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1467 static GstStructure *
1468 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1469 guint32 pixelformat,
1470 guint32 width, guint32 height, const GstStructure * template)
1472 gint fd = v4l2object->video_fd;
1473 struct v4l2_frmivalenum ival;
1476 GValue rates = { 0, };
1477 gboolean interlaced;
1478 gint int_width = width;
1479 gint int_height = height;
1481 /* interlaced detection using VIDIOC_TRY/S_FMT */
1482 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat,
1483 &int_width, &int_height, &interlaced))
1486 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1488 ival.pixel_format = pixelformat;
1490 ival.height = height;
1492 GST_LOG_OBJECT (v4l2object->element,
1493 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1494 GST_FOURCC_ARGS (pixelformat));
1496 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1497 * fraction to get framerate */
1498 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1499 goto enum_frameintervals_failed;
1501 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1502 GValue rate = { 0, };
1504 g_value_init (&rates, GST_TYPE_LIST);
1505 g_value_init (&rate, GST_TYPE_FRACTION);
1508 num = ival.discrete.numerator;
1509 denom = ival.discrete.denominator;
1511 if (num > G_MAXINT || denom > G_MAXINT) {
1512 /* let us hope we don't get here... */
1517 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1520 /* swap to get the framerate */
1521 gst_value_set_fraction (&rate, denom, num);
1522 gst_value_list_append_value (&rates, &rate);
1525 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1526 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1527 GValue min = { 0, };
1528 GValue step = { 0, };
1529 GValue max = { 0, };
1530 gboolean added = FALSE;
1531 guint32 minnum, mindenom;
1532 guint32 maxnum, maxdenom;
1534 g_value_init (&rates, GST_TYPE_LIST);
1536 g_value_init (&min, GST_TYPE_FRACTION);
1537 g_value_init (&step, GST_TYPE_FRACTION);
1538 g_value_init (&max, GST_TYPE_FRACTION);
1541 minnum = ival.stepwise.min.numerator;
1542 mindenom = ival.stepwise.min.denominator;
1543 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1547 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1549 gst_value_set_fraction (&min, minnum, mindenom);
1552 maxnum = ival.stepwise.max.numerator;
1553 maxdenom = ival.stepwise.max.denominator;
1554 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1559 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1561 gst_value_set_fraction (&max, maxnum, maxdenom);
1564 num = ival.stepwise.step.numerator;
1565 denom = ival.stepwise.step.denominator;
1566 if (num > G_MAXINT || denom > G_MAXINT) {
1571 if (num == 0 || denom == 0) {
1572 /* in this case we have a wrong fraction or no step, set the step to max
1573 * so that we only add the min value in the loop below */
1578 /* since we only have gst_value_fraction_subtract and not add, negate the
1580 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1582 gst_value_set_fraction (&step, -num, denom);
1584 while (gst_value_compare (&min, &max) <= 0) {
1585 GValue rate = { 0, };
1587 num = gst_value_get_fraction_numerator (&min);
1588 denom = gst_value_get_fraction_denominator (&min);
1589 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1592 /* invert to get the framerate */
1593 g_value_init (&rate, GST_TYPE_FRACTION);
1594 gst_value_set_fraction (&rate, denom, num);
1595 gst_value_list_append_value (&rates, &rate);
1598 /* we're actually adding because step was negated above. This is because
1599 * there is no _add function... */
1600 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1601 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1606 /* no range was added, leave the default range from the template */
1607 GST_WARNING_OBJECT (v4l2object->element,
1608 "no range added, leaving default");
1609 g_value_unset (&rates);
1611 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1612 guint32 maxnum, maxdenom;
1614 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1616 num = ival.stepwise.min.numerator;
1617 denom = ival.stepwise.min.denominator;
1618 if (num > G_MAXINT || denom > G_MAXINT) {
1623 maxnum = ival.stepwise.max.numerator;
1624 maxdenom = ival.stepwise.max.denominator;
1625 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1630 GST_LOG_OBJECT (v4l2object->element,
1631 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
1634 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1640 s = gst_structure_copy (template);
1641 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
1642 "height", G_TYPE_INT, (gint) height,
1643 "interlaced", G_TYPE_BOOLEAN, interlaced, NULL);
1645 if (G_IS_VALUE (&rates)) {
1646 /* only change the framerate on the template when we have a valid probed new
1648 gst_structure_set_value (s, "framerate", &rates);
1649 g_value_unset (&rates);
1651 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
1657 enum_frameintervals_failed:
1659 GST_DEBUG_OBJECT (v4l2object->element,
1660 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
1661 GST_FOURCC_ARGS (pixelformat), width, height);
1666 /* I don't see how this is actually an error, we ignore the format then */
1667 GST_WARNING_OBJECT (v4l2object->element,
1668 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
1669 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
1673 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
1675 #ifdef VIDIOC_ENUM_FRAMESIZES
1677 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
1681 gst_structure_get_int (s1, "width", &w1);
1682 gst_structure_get_int (s1, "height", &h1);
1683 gst_structure_get_int (s2, "width", &w2);
1684 gst_structure_get_int (s2, "height", &h2);
1686 /* I think it's safe to assume that this won't overflow for a while */
1687 return ((w2 * h2) - (w1 * h1));
1692 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
1693 guint32 pixelformat, const GstStructure * template)
1695 GstCaps *ret = gst_caps_new_empty ();
1698 #ifdef VIDIOC_ENUM_FRAMESIZES
1699 gint fd = v4l2object->video_fd;
1700 struct v4l2_frmsizeenum size;
1701 GList *results = NULL;
1704 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1705 return gst_caps_new_simple ("video/mpegts", NULL);
1707 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
1709 size.pixel_format = pixelformat;
1711 GST_DEBUG_OBJECT (v4l2object->element, "Enumerating frame sizes");
1713 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
1714 goto enum_framesizes_failed;
1716 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1718 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
1719 size.discrete.width, size.discrete.height);
1721 w = MIN (size.discrete.width, G_MAXINT);
1722 h = MIN (size.discrete.height, G_MAXINT);
1726 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1727 pixelformat, w, h, template);
1730 results = g_list_prepend (results, tmp);
1734 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
1735 GST_DEBUG_OBJECT (v4l2object->element,
1736 "done iterating discrete frame sizes");
1737 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
1738 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
1739 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1740 size.stepwise.min_width);
1741 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1742 size.stepwise.min_height);
1743 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1744 size.stepwise.max_width);
1745 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1746 size.stepwise.max_height);
1747 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
1748 size.stepwise.step_width);
1749 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
1750 size.stepwise.step_height);
1752 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
1753 w < size.stepwise.max_width && h < size.stepwise.max_height;
1754 w += size.stepwise.step_width, h += size.stepwise.step_height) {
1755 if (w == 0 || h == 0)
1759 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1760 pixelformat, w, h, template);
1763 results = g_list_prepend (results, tmp);
1765 GST_DEBUG_OBJECT (v4l2object->element,
1766 "done iterating stepwise frame sizes");
1767 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
1770 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
1771 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1772 size.stepwise.min_width);
1773 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1774 size.stepwise.min_height);
1775 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1776 size.stepwise.max_width);
1777 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1778 size.stepwise.max_height);
1780 w = MAX (size.stepwise.min_width, 1);
1781 h = MAX (size.stepwise.min_height, 1);
1782 maxw = MIN (size.stepwise.max_width, G_MAXINT);
1783 maxh = MIN (size.stepwise.max_height, G_MAXINT);
1786 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
1789 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
1790 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
1793 /* no point using the results list here, since there's only one struct */
1794 gst_caps_append_structure (ret, tmp);
1800 /* we use an intermediary list to store and then sort the results of the
1801 * probing because we can't make any assumptions about the order in which
1802 * the driver will give us the sizes, but we want the final caps to contain
1803 * the results starting with the highest resolution and having the lowest
1804 * resolution last, since order in caps matters for things like fixation. */
1805 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
1806 while (results != NULL) {
1807 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
1808 results = g_list_delete_link (results, results);
1811 if (gst_caps_is_empty (ret))
1812 goto enum_framesizes_no_results;
1817 enum_framesizes_failed:
1819 /* I don't see how this is actually an error */
1820 GST_DEBUG_OBJECT (v4l2object->element,
1821 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
1822 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
1823 goto default_frame_sizes;
1825 enum_framesizes_no_results:
1827 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
1828 * question doesn't actually support it yet */
1829 GST_DEBUG_OBJECT (v4l2object->element,
1830 "No results for pixelformat %" GST_FOURCC_FORMAT
1831 " enumerating frame sizes, trying fallback",
1832 GST_FOURCC_ARGS (pixelformat));
1833 goto default_frame_sizes;
1837 GST_WARNING_OBJECT (v4l2object->element,
1838 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
1839 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
1840 goto default_frame_sizes;
1842 default_frame_sizes:
1843 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
1845 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
1846 gboolean interlaced;
1848 /* This code is for Linux < 2.6.19 */
1850 max_w = max_h = GST_V4L2_MAX_SIZE;
1851 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
1852 &min_h, &interlaced)) {
1853 GST_WARNING_OBJECT (v4l2object->element,
1854 "Could not probe minimum capture size for pixelformat %"
1855 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1857 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
1858 &max_h, &interlaced)) {
1859 GST_WARNING_OBJECT (v4l2object->element,
1860 "Could not probe maximum capture size for pixelformat %"
1861 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1864 /* Since we can't get framerate directly, try to use the current norm */
1865 if (v4l2object->norm && v4l2object->norms) {
1867 GstTunerNorm *norm = NULL;
1869 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
1870 norm = (GstTunerNorm *) norms->data;
1871 if (!strcmp (norm->label, v4l2object->norm))
1874 /* If it's possible, set framerate to that (discrete) value */
1876 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
1877 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
1881 tmp = gst_structure_copy (template);
1883 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
1886 /* if norm can't be used, copy the template framerate */
1887 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
1892 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
1894 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
1897 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
1899 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
1901 gst_structure_set (tmp, "interlaced", G_TYPE_BOOLEAN, interlaced, NULL);
1903 gst_caps_append_structure (ret, tmp);
1910 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1911 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced)
1913 struct v4l2_format fmt;
1917 g_return_val_if_fail (width != NULL, FALSE);
1918 g_return_val_if_fail (height != NULL, FALSE);
1920 GST_LOG_OBJECT (v4l2object->element,
1921 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
1922 *width, *height, GST_FOURCC_ARGS (pixelformat));
1924 fd = v4l2object->video_fd;
1926 /* get size delimiters */
1927 memset (&fmt, 0, sizeof (fmt));
1928 fmt.type = v4l2object->type;
1929 fmt.fmt.pix.width = *width;
1930 fmt.fmt.pix.height = *height;
1931 fmt.fmt.pix.pixelformat = pixelformat;
1932 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1934 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
1935 if (r < 0 && errno == EINVAL) {
1936 /* try again with interlaced video */
1937 fmt.fmt.pix.width = *width;
1938 fmt.fmt.pix.height = *height;
1939 fmt.fmt.pix.pixelformat = pixelformat;
1940 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1941 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
1945 /* The driver might not implement TRY_FMT, in which case we will try
1947 if (errno != ENOTTY)
1950 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
1951 be, because we're still probing */
1952 if (GST_V4L2_IS_ACTIVE (v4l2object))
1955 GST_LOG_OBJECT (v4l2object->element,
1956 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
1958 fmt.fmt.pix.width = *width;
1959 fmt.fmt.pix.height = *height;
1961 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
1962 if (r < 0 && errno == EINVAL) {
1963 /* try again with progressive video */
1964 fmt.fmt.pix.width = *width;
1965 fmt.fmt.pix.height = *height;
1966 fmt.fmt.pix.pixelformat = pixelformat;
1967 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1968 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
1975 GST_LOG_OBJECT (v4l2object->element,
1976 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
1978 *width = fmt.fmt.pix.width;
1979 *height = fmt.fmt.pix.height;
1981 switch (fmt.fmt.pix.field) {
1982 case V4L2_FIELD_ANY:
1983 case V4L2_FIELD_NONE:
1984 *interlaced = FALSE;
1986 case V4L2_FIELD_INTERLACED:
1987 case V4L2_FIELD_INTERLACED_TB:
1988 case V4L2_FIELD_INTERLACED_BT:
1992 GST_WARNING_OBJECT (v4l2object->element,
1993 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u",
1994 GST_FOURCC_ARGS (pixelformat), *width, *height);
2003 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, guint32 pixelformat,
2004 guint32 width, guint32 height, gboolean interlaced)
2006 gint fd = v4l2object->video_fd;
2007 struct v4l2_format format;
2008 enum v4l2_field field;
2011 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
2012 /* ideally we would differentiate between types of interlaced video
2013 * but there is not sufficient information in the caps..
2015 field = V4L2_FIELD_SEQ_TB;
2017 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
2018 field = V4L2_FIELD_NONE;
2021 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2022 "%" GST_FOURCC_FORMAT, width, height, GST_FOURCC_ARGS (pixelformat));
2024 GST_V4L2_CHECK_OPEN (v4l2object);
2025 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2027 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
2030 memset (&format, 0x00, sizeof (struct v4l2_format));
2031 format.type = v4l2object->type;
2033 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
2034 goto get_fmt_failed;
2036 if (format.type == v4l2object->type &&
2037 format.fmt.pix.width == width &&
2038 format.fmt.pix.height == height &&
2039 format.fmt.pix.pixelformat == pixelformat &&
2040 format.fmt.pix.field == field) {
2041 /* Nothing to do. We want to succeed immediately
2042 * here because setting the same format back
2043 * can still fail due to EBUSY. By short-circuiting
2044 * here, we allow pausing and re-playing pipelines
2045 * with changed caps, as long as the changed caps
2046 * do not change the webcam's format. Otherwise,
2047 * any caps change would require us to go to NULL
2048 * state to close the device and set format.
2053 format.type = v4l2object->type;
2054 format.fmt.pix.width = width;
2055 format.fmt.pix.height = height;
2056 format.fmt.pix.pixelformat = pixelformat;
2057 format.fmt.pix.field = field;
2059 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0) {
2060 goto set_fmt_failed;
2063 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
2064 goto invalid_dimensions;
2066 if (format.fmt.pix.pixelformat != pixelformat)
2067 goto invalid_pixelformat;
2074 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2075 (_("Device '%s' does not support video capture"),
2076 v4l2object->videodev),
2077 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
2082 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2083 (_("Device '%s' cannot capture at %dx%d"),
2084 v4l2object->videodev, width, height),
2085 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2086 GST_FOURCC_ARGS (pixelformat), width, height, g_strerror (errno)));
2091 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2092 (_("Device '%s' cannot capture at %dx%d"),
2093 v4l2object->videodev, width, height),
2094 ("Tried to capture at %dx%d, but device returned size %dx%d",
2095 width, height, format.fmt.pix.width, format.fmt.pix.height));
2098 invalid_pixelformat:
2100 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2101 (_("Device '%s' cannot capture in the specified format"),
2102 v4l2object->videodev),
2103 ("Tried to capture in %" GST_FOURCC_FORMAT
2104 ", but device returned format" " %" GST_FOURCC_FORMAT,
2105 GST_FOURCC_ARGS (pixelformat),
2106 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
2112 gst_v4l2_object_start_streaming (GstV4l2Object * v4l2object)
2114 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_STREAMON,
2115 &(v4l2object->type)) < 0) {
2116 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ,
2117 (_("Error starting streaming on device '%s'."), v4l2object->videodev),
2125 gst_v4l2_object_stop_streaming (GstV4l2Object * v4l2object)
2127 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_STREAMOFF,
2128 &(v4l2object->type)) < 0) {
2129 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ,
2130 (_("Error stopping streaming on device '%s'."), v4l2object->videodev),