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"
44 #include <gst/video/video.h>
46 /* videodev2.h is not versioned and we can't easily check for the presence
47 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
48 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
49 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
50 #define V4L2_FIELD_INTERLACED_TB 8
51 #define V4L2_FIELD_INTERLACED_BT 9
54 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
55 #define GST_CAT_DEFAULT v4l2_debug
58 #define DEFAULT_PROP_DEVICE_NAME NULL
59 #define DEFAULT_PROP_DEVICE_FD -1
60 #define DEFAULT_PROP_FLAGS 0
61 #define DEFAULT_PROP_TV_NORM 0
62 #define DEFAULT_PROP_CHANNEL NULL
63 #define DEFAULT_PROP_FREQUENCY 0
68 V4L2_STD_OBJECT_PROPS,
71 G_LOCK_DEFINE_STATIC (probe_lock);
74 gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
76 GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
77 static GList *list = NULL;
82 list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
85 G_UNLOCK (probe_lock);
90 static gboolean init = FALSE;
91 static GList *devices = NULL;
95 gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
96 GList ** klass_devices)
98 GUdevClient *client = NULL;
103 gchar *device = devices->data;
104 devices = g_list_remove (devices, device);
108 GST_INFO ("Enumerating video4linux devices from udev");
109 client = g_udev_client_new (NULL);
111 GST_WARNING ("Failed to initialize gudev client");
115 item = g_udev_client_query_by_subsystem (client, "video4linux");
117 GUdevDevice *device = item->data;
118 gchar *devnode = g_strdup (g_udev_device_get_device_file (device));
119 gint api = g_udev_device_get_property_as_int (device, "ID_V4L_VERSION");
120 GST_INFO ("Found new device: %s, API: %d", devnode, api);
121 /* Append v4l2 devices only. If api is 0 probably v4l_id has
122 been stripped out of the current udev installation, append
126 ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
128 if ((api == 2 || api == 0)) {
129 devices = g_list_append (devices, devnode);
133 g_object_unref (device);
142 g_object_unref (client);
145 *klass_devices = devices;
149 #endif /* HAVE_GUDEV */
152 gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
153 GList ** klass_devices)
156 const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
160 gchar *device = devices->data;
161 devices = g_list_remove (devices, device);
166 * detect /dev entries
168 for (n = 0; n < 64; n++) {
169 for (base = 0; dev_base[base] != NULL; base++) {
171 gchar *device = g_strdup_printf ("%s%d",
176 * does the /dev/ entry exist at all?
178 if (stat (device, &s) == 0) {
180 * yes: is a device attached?
182 if (S_ISCHR (s.st_mode)) {
184 if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
188 devices = g_list_append (devices, device);
199 *klass_devices = devices;
205 gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
206 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
208 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
213 if (!gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices))
214 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
215 #else /* !HAVE_GUDEV */
216 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
217 #endif /* HAVE_GUDEV */
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
226 gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
227 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
229 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
230 gboolean ret = FALSE;
236 !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
237 #else /* !HAVE_GUDEV */
238 ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
239 #endif /* HAVE_GUDEV */
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
249 gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
252 GValue value = { 0 };
258 array = g_value_array_new (g_list_length (*klass_devices));
259 item = *klass_devices;
260 g_value_init (&value, G_TYPE_STRING);
262 gchar *device = item->data;
264 g_value_set_string (&value, device);
265 g_value_array_append (array, &value);
269 g_value_unset (&value);
275 gst_v4l2_probe_get_values (GstPropertyProbe * probe,
276 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
278 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
279 GValueArray *array = NULL;
283 array = gst_v4l2_class_list_devices (klass, klass_devices);
286 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
293 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
295 gst_v4l2_device_get_type (void)
297 static GType v4l2_device_type = 0;
299 if (v4l2_device_type == 0) {
300 static const GFlagsValue values[] = {
301 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
302 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
303 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
305 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
306 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
308 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
309 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
315 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
318 return v4l2_device_type;
321 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
323 gst_v4l2_tv_norm_get_type (void)
325 static GType v4l2_tv_norm = 0;
328 static const GEnumValue tv_norms[] = {
331 {V4L2_STD_NTSC, "NTSC", "NTSC"},
332 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
333 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
334 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
335 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
337 {V4L2_STD_PAL, "PAL", "PAL"},
338 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
339 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
340 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
341 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
342 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
343 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
344 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
345 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
346 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
347 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
348 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
349 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
350 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
351 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
353 {V4L2_STD_SECAM, "SECAM", "SECAM"},
354 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
355 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
356 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
357 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
358 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
359 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
360 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
361 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
362 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
367 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
374 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
375 const char *default_device)
377 g_object_class_install_property (gobject_class, PROP_DEVICE,
378 g_param_spec_string ("device", "Device", "Device location",
379 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
380 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
381 g_param_spec_string ("device-name", "Device name",
382 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
383 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
384 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
385 g_param_spec_int ("device-fd", "File descriptor",
386 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
387 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
388 g_object_class_install_property (gobject_class, PROP_FLAGS,
389 g_param_spec_flags ("flags", "Flags", "Device type flags",
390 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
391 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
394 * GstV4l2Src:brightness
396 * Picture brightness, or more precisely, the black level
400 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
401 g_param_spec_int ("brightness", "Brightness",
402 "Picture brightness, or more precisely, the black level", G_MININT,
404 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
406 * GstV4l2Src:contrast
408 * Picture contrast or luma gain
412 g_object_class_install_property (gobject_class, PROP_CONTRAST,
413 g_param_spec_int ("contrast", "Contrast",
414 "Picture contrast or luma gain", G_MININT,
416 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
418 * GstV4l2Src:saturation
420 * Picture color saturation or chroma gain
424 g_object_class_install_property (gobject_class, PROP_SATURATION,
425 g_param_spec_int ("saturation", "Saturation",
426 "Picture color saturation or chroma gain", G_MININT,
428 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
432 * Hue or color balance
436 g_object_class_install_property (gobject_class, PROP_HUE,
437 g_param_spec_int ("hue", "Hue",
438 "Hue or color balance", G_MININT,
440 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
449 g_object_class_install_property (gobject_class, PROP_TV_NORM,
450 g_param_spec_enum ("norm", "TV norm",
452 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
453 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
457 gst_v4l2_object_new (GstElement * element,
458 enum v4l2_buf_type type,
459 const char *default_device,
460 GstV4l2GetInOutFunction get_in_out_func,
461 GstV4l2SetInOutFunction set_in_out_func,
462 GstV4l2UpdateFpsFunction update_fps_func)
464 GstV4l2Object *v4l2object;
467 * some default values
469 v4l2object = g_new0 (GstV4l2Object, 1);
471 v4l2object->type = type;
472 v4l2object->formats = NULL;
474 v4l2object->element = element;
475 v4l2object->get_in_out_func = get_in_out_func;
476 v4l2object->set_in_out_func = set_in_out_func;
477 v4l2object->update_fps_func = update_fps_func;
479 v4l2object->video_fd = -1;
480 v4l2object->poll = gst_poll_new (TRUE);
481 v4l2object->buffer = NULL;
482 v4l2object->videodev = g_strdup (default_device);
484 v4l2object->norms = NULL;
485 v4l2object->channels = NULL;
486 v4l2object->colors = NULL;
488 v4l2object->xwindow_id = 0;
493 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
497 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
499 g_return_if_fail (v4l2object != NULL);
501 if (v4l2object->videodev)
502 g_free (v4l2object->videodev);
504 if (v4l2object->poll)
505 gst_poll_free (v4l2object->poll);
507 if (v4l2object->channel)
508 g_free (v4l2object->channel);
510 if (v4l2object->formats) {
511 gst_v4l2_object_clear_format_list (v4l2object);
519 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
521 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
522 g_slist_free (v4l2object->formats);
523 v4l2object->formats = NULL;
529 gst_v4l2_object_prop_to_cid (guint prop_id)
534 case PROP_BRIGHTNESS:
535 cid = V4L2_CID_BRIGHTNESS;
538 cid = V4L2_CID_CONTRAST;
540 case PROP_SATURATION:
541 cid = V4L2_CID_SATURATION;
547 GST_WARNING ("unmapped property id: %d", prop_id);
554 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
555 guint prop_id, const GValue * value, GParamSpec * pspec)
559 g_free (v4l2object->videodev);
560 v4l2object->videodev = g_value_dup_string (value);
562 case PROP_BRIGHTNESS:
564 case PROP_SATURATION:
567 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
570 if (GST_V4L2_IS_OPEN (v4l2object)) {
571 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
578 v4l2object->tv_norm = g_value_get_enum (value);
582 if (GST_V4L2_IS_OPEN (v4l2object)) {
583 GstTuner *tuner = GST_TUNER (v4l2object->element);
584 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
585 (gchar *) g_value_get_string (value));
588 /* like gst_tuner_set_channel (tuner, channel)
589 without g_object_notify */
590 gst_v4l2_tuner_set_channel (v4l2object, channel);
593 g_free (v4l2object->channel);
594 v4l2object->channel = g_value_dup_string (value);
598 if (GST_V4L2_IS_OPEN (v4l2object)) {
599 GstTuner *tuner = GST_TUNER (v4l2object->element);
600 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
603 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
605 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
606 without g_object_notify */
607 gst_v4l2_tuner_set_frequency (v4l2object, channel,
608 g_value_get_ulong (value));
611 v4l2object->frequency = g_value_get_ulong (value);
624 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
625 guint prop_id, GValue * value, GParamSpec * pspec)
629 g_value_set_string (value, v4l2object->videodev);
631 case PROP_DEVICE_NAME:
633 const guchar *new = NULL;
635 if (GST_V4L2_IS_OPEN (v4l2object)) {
636 new = v4l2object->vcap.card;
637 } else if (gst_v4l2_open (v4l2object)) {
638 new = v4l2object->vcap.card;
639 gst_v4l2_close (v4l2object);
641 g_value_set_string (value, (gchar *) new);
646 if (GST_V4L2_IS_OPEN (v4l2object))
647 g_value_set_int (value, v4l2object->video_fd);
649 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
656 if (GST_V4L2_IS_OPEN (v4l2object)) {
657 flags |= v4l2object->vcap.capabilities &
658 (V4L2_CAP_VIDEO_CAPTURE |
659 V4L2_CAP_VIDEO_OUTPUT |
660 V4L2_CAP_VIDEO_OVERLAY |
661 V4L2_CAP_VBI_CAPTURE |
662 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
664 g_value_set_flags (value, flags);
667 case PROP_BRIGHTNESS:
669 case PROP_SATURATION:
672 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
675 if (GST_V4L2_IS_OPEN (v4l2object)) {
677 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
678 g_value_set_int (value, v);
686 g_value_set_enum (value, v4l2object->tv_norm);
696 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
698 GstTunerNorm *norm = NULL;
699 GstTunerChannel *channel = NULL;
702 if (!GST_IS_TUNER (v4l2object->element))
705 tuner = GST_TUNER (v4l2object->element);
707 if (v4l2object->tv_norm)
708 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
709 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=%d, norm=%p",
710 v4l2object->tv_norm, norm);
712 gst_tuner_set_norm (tuner, norm);
715 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
717 v4l2object->tv_norm =
718 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
719 gst_tuner_norm_changed (tuner, norm);
723 if (v4l2object->channel)
724 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
726 gst_tuner_set_channel (tuner, channel);
729 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
730 (v4l2object->element)));
732 g_free (v4l2object->channel);
733 v4l2object->channel = g_strdup (channel->label);
734 gst_tuner_channel_changed (tuner, channel);
739 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
740 if (v4l2object->frequency != 0) {
741 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
743 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
744 if (v4l2object->frequency == 0) {
746 gst_tuner_set_frequency (tuner, channel, 1000);
754 gst_v4l2_object_start (GstV4l2Object * v4l2object)
756 if (gst_v4l2_open (v4l2object))
757 gst_v4l2_set_defaults (v4l2object);
762 gst_v4l2_xoverlay_start (v4l2object);
769 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
772 gst_v4l2_xoverlay_stop (v4l2object);
775 if (!gst_v4l2_close (v4l2object))
778 if (v4l2object->formats) {
779 gst_v4l2_object_clear_format_list (v4l2object);
787 * common format / caps utilities:
795 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
796 /* from Linux 2.6.15 videodev2.h */
797 {V4L2_PIX_FMT_RGB332, TRUE},
798 {V4L2_PIX_FMT_RGB555, TRUE},
799 {V4L2_PIX_FMT_RGB565, TRUE},
800 {V4L2_PIX_FMT_RGB555X, TRUE},
801 {V4L2_PIX_FMT_RGB565X, TRUE},
802 {V4L2_PIX_FMT_BGR24, TRUE},
803 {V4L2_PIX_FMT_RGB24, TRUE},
804 {V4L2_PIX_FMT_BGR32, TRUE},
805 {V4L2_PIX_FMT_RGB32, TRUE},
806 {V4L2_PIX_FMT_GREY, TRUE},
807 {V4L2_PIX_FMT_YVU410, TRUE},
808 {V4L2_PIX_FMT_YVU420, TRUE},
809 {V4L2_PIX_FMT_YUYV, TRUE},
810 {V4L2_PIX_FMT_UYVY, TRUE},
811 {V4L2_PIX_FMT_YUV422P, TRUE},
812 {V4L2_PIX_FMT_YUV411P, TRUE},
813 {V4L2_PIX_FMT_Y41P, TRUE},
815 /* two planes -- one Y, one Cr + Cb interleaved */
816 {V4L2_PIX_FMT_NV12, TRUE},
817 {V4L2_PIX_FMT_NV21, TRUE},
819 /* The following formats are not defined in the V4L2 specification */
820 {V4L2_PIX_FMT_YUV410, TRUE},
821 {V4L2_PIX_FMT_YUV420, TRUE},
822 {V4L2_PIX_FMT_YYUV, TRUE},
823 {V4L2_PIX_FMT_HI240, TRUE},
825 /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
826 #ifdef V4L2_PIX_FMT_SBGGR8
827 {V4L2_PIX_FMT_SBGGR8, TRUE},
830 /* compressed formats */
831 {V4L2_PIX_FMT_MJPEG, TRUE},
832 {V4L2_PIX_FMT_JPEG, TRUE},
833 #ifdef V4L2_PIX_FMT_PJPG
834 {V4L2_PIX_FMT_PJPG, TRUE},
836 {V4L2_PIX_FMT_DV, TRUE},
837 {V4L2_PIX_FMT_MPEG, FALSE},
839 /* Vendor-specific formats */
840 {V4L2_PIX_FMT_WNVA, TRUE},
842 #ifdef V4L2_PIX_FMT_SN9C10X
843 {V4L2_PIX_FMT_SN9C10X, TRUE},
845 #ifdef V4L2_PIX_FMT_PWC1
846 {V4L2_PIX_FMT_PWC1, TRUE},
848 #ifdef V4L2_PIX_FMT_PWC2
849 {V4L2_PIX_FMT_PWC2, TRUE},
851 #ifdef V4L2_PIX_FMT_YVYU
852 {V4L2_PIX_FMT_YVYU, TRUE},
856 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
859 static struct v4l2_fmtdesc *
860 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
863 struct v4l2_fmtdesc *fmt;
869 walk = gst_v4l2_object_get_format_list (v4l2object);
871 fmt = (struct v4l2_fmtdesc *) walk->data;
872 if (fmt->pixelformat == fourcc)
874 /* special case for jpeg */
875 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
876 fmt->pixelformat == V4L2_PIX_FMT_JPEG
877 #ifdef V4L2_PIX_FMT_PJPG
878 || fmt->pixelformat == V4L2_PIX_FMT_PJPG
881 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG
882 #ifdef V4L2_PIX_FMT_PJPG
883 || fourcc == V4L2_PIX_FMT_PJPG
889 walk = g_slist_next (walk);
897 /* complete made up ranking, the values themselves are meaningless */
898 #define YUV_BASE_RANK 1000
899 #define JPEG_BASE_RANK 500
900 #define DV_BASE_RANK 200
901 #define RGB_BASE_RANK 100
902 #define YUV_ODD_BASE_RANK 50
903 #define RGB_ODD_BASE_RANK 25
904 #define BAYER_BASE_RANK 15
905 #define S910_BASE_RANK 10
906 #define GREY_BASE_RANK 5
907 #define PWC_BASE_RANK 1
909 /* This flag is already used by libv4l2 although
910 * it was added to the Linux kernel in 2.6.32
912 #ifndef V4L2_FMT_FLAG_EMULATED
913 #define V4L2_FMT_FLAG_EMULATED 0x0002
917 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
919 guint32 fourcc = fmt->pixelformat;
920 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
924 case V4L2_PIX_FMT_MJPEG:
925 #ifdef V4L2_PIX_FMT_PJPG
926 case V4L2_PIX_FMT_PJPG:
927 rank = JPEG_BASE_RANK;
930 case V4L2_PIX_FMT_JPEG:
931 rank = JPEG_BASE_RANK + 1;
933 case V4L2_PIX_FMT_MPEG: /* MPEG */
934 rank = JPEG_BASE_RANK + 2;
937 case V4L2_PIX_FMT_RGB332:
938 case V4L2_PIX_FMT_RGB555:
939 case V4L2_PIX_FMT_RGB555X:
940 case V4L2_PIX_FMT_RGB565:
941 case V4L2_PIX_FMT_RGB565X:
942 rank = RGB_ODD_BASE_RANK;
945 case V4L2_PIX_FMT_RGB24:
946 case V4L2_PIX_FMT_BGR24:
947 rank = RGB_BASE_RANK - 1;
950 case V4L2_PIX_FMT_RGB32:
951 case V4L2_PIX_FMT_BGR32:
952 rank = RGB_BASE_RANK;
955 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
956 rank = GREY_BASE_RANK;
959 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
960 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
961 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
962 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
963 rank = YUV_ODD_BASE_RANK;
966 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
967 rank = YUV_BASE_RANK + 3;
969 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
970 rank = YUV_BASE_RANK + 2;
972 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
973 rank = YUV_BASE_RANK + 7;
975 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
976 rank = YUV_BASE_RANK + 10;
978 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
979 rank = YUV_BASE_RANK + 6;
981 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
982 rank = YUV_BASE_RANK + 9;
984 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
985 rank = YUV_BASE_RANK + 5;
987 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
988 rank = YUV_BASE_RANK + 4;
990 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
991 rank = YUV_BASE_RANK + 8;
994 case V4L2_PIX_FMT_DV:
998 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1002 #ifdef V4L2_PIX_FMT_SBGGR8
1003 case V4L2_PIX_FMT_SBGGR8:
1004 rank = BAYER_BASE_RANK;
1008 #ifdef V4L2_PIX_FMT_SN9C10X
1009 case V4L2_PIX_FMT_SN9C10X:
1010 rank = S910_BASE_RANK;
1014 #ifdef V4L2_PIX_FMT_PWC1
1015 case V4L2_PIX_FMT_PWC1:
1016 rank = PWC_BASE_RANK;
1019 #ifdef V4L2_PIX_FMT_PWC2
1020 case V4L2_PIX_FMT_PWC2:
1021 rank = PWC_BASE_RANK;
1030 /* All ranks are below 1<<15 so a shift by 15
1031 * will a) make all non-emulated formats larger
1032 * than emulated and b) will not overflow
1043 format_cmp_func (gconstpointer a, gconstpointer b)
1045 const struct v4l2_fmtdesc *fa = a;
1046 const struct v4l2_fmtdesc *fb = b;
1048 if (fa->pixelformat == fb->pixelformat)
1051 return gst_v4l2_object_format_get_rank (fb) -
1052 gst_v4l2_object_format_get_rank (fa);
1055 /******************************************************
1056 * gst_v4l2_object_fill_format_list():
1057 * create list of supported capture formats
1058 * return value: TRUE on success, FALSE on error
1059 ******************************************************/
1061 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object)
1064 struct v4l2_fmtdesc *format;
1066 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1068 /* format enumeration */
1070 format = g_new0 (struct v4l2_fmtdesc, 1);
1073 format->type = v4l2object->type;
1075 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1076 if (errno == EINVAL) {
1078 break; /* end of enumeration */
1084 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1085 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1086 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1087 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1088 format->description);
1089 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1090 GST_FOURCC_ARGS (format->pixelformat));
1092 /* sort formats according to our preference; we do this, because caps
1093 * are probed in the order the formats are in the list, and the order of
1094 * formats in the final probed caps matters for things like fixation */
1095 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1096 (GCompareFunc) format_cmp_func);
1099 #ifndef GST_DISABLE_GST_DEBUG
1103 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1104 for (l = v4l2object->formats; l != NULL; l = l->next) {
1107 GST_INFO_OBJECT (v4l2object->element,
1108 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1109 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1119 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1120 (_("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)));
1127 * Get the list of supported capture formats, a list of
1128 * <code>struct v4l2_fmtdesc</code>.
1131 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1133 if (!v4l2object->formats)
1134 gst_v4l2_object_fill_format_list (v4l2object);
1135 return v4l2object->formats;
1140 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1142 GstStructure *structure = NULL;
1145 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1146 #ifdef V4L2_PIX_FMT_PJPG
1147 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1149 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1150 structure = gst_structure_new ("image/jpeg", NULL);
1152 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1153 structure = gst_structure_new ("video/x-raw-gray",
1154 "bpp", G_TYPE_INT, 8, NULL);
1156 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1157 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1158 /* FIXME: get correct fourccs here */
1160 case V4L2_PIX_FMT_RGB332:
1161 case V4L2_PIX_FMT_RGB555X:
1162 case V4L2_PIX_FMT_RGB565X:
1163 /* FIXME: get correct fourccs here */
1165 case V4L2_PIX_FMT_RGB555:
1166 case V4L2_PIX_FMT_RGB565:
1167 case V4L2_PIX_FMT_RGB24:
1168 case V4L2_PIX_FMT_BGR24:
1169 case V4L2_PIX_FMT_RGB32:
1170 case V4L2_PIX_FMT_BGR32:
1171 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1172 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1173 case V4L2_PIX_FMT_YVU410:
1174 case V4L2_PIX_FMT_YUV410:
1175 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1176 case V4L2_PIX_FMT_YUYV:
1177 case V4L2_PIX_FMT_YVU420:
1178 case V4L2_PIX_FMT_UYVY:
1180 case V4L2_PIX_FMT_Y41P:
1182 case V4L2_PIX_FMT_YUV422P:
1183 #ifdef V4L2_PIX_FMT_YVYU
1184 case V4L2_PIX_FMT_YVYU:
1186 case V4L2_PIX_FMT_YUV411P:{
1187 GstVideoFormat format;
1190 case V4L2_PIX_FMT_RGB555:
1191 format = GST_VIDEO_FORMAT_RGB15;
1193 case V4L2_PIX_FMT_RGB565:
1194 format = GST_VIDEO_FORMAT_RGB16;
1196 case V4L2_PIX_FMT_RGB24:
1197 format = GST_VIDEO_FORMAT_RGB;
1199 case V4L2_PIX_FMT_BGR24:
1200 format = GST_VIDEO_FORMAT_BGR;
1202 case V4L2_PIX_FMT_RGB32:
1203 format = GST_VIDEO_FORMAT_RGBx;
1205 case V4L2_PIX_FMT_BGR32:
1206 format = GST_VIDEO_FORMAT_BGRx;
1208 case V4L2_PIX_FMT_NV12:
1209 format = GST_VIDEO_FORMAT_NV12;
1211 case V4L2_PIX_FMT_NV21:
1212 format = GST_VIDEO_FORMAT_NV21;
1214 case V4L2_PIX_FMT_YVU410:
1215 format = GST_VIDEO_FORMAT_YVU9;
1217 case V4L2_PIX_FMT_YUV410:
1218 format = GST_VIDEO_FORMAT_YUV9;
1220 case V4L2_PIX_FMT_YUV420:
1221 format = GST_VIDEO_FORMAT_I420;
1223 case V4L2_PIX_FMT_YUYV:
1224 format = GST_VIDEO_FORMAT_YUY2;
1226 case V4L2_PIX_FMT_YVU420:
1227 format = GST_VIDEO_FORMAT_YV12;
1229 case V4L2_PIX_FMT_UYVY:
1230 format = GST_VIDEO_FORMAT_UYVY;
1233 case V4L2_PIX_FMT_Y41P:
1234 format = GST_VIDEO_FORMAT_Y41P;
1237 case V4L2_PIX_FMT_YUV411P:
1238 format = GST_VIDEO_FORMAT_Y41B;
1240 case V4L2_PIX_FMT_YUV422P:
1241 format = GST_VIDEO_FORMAT_Y42B;
1243 #ifdef V4L2_PIX_FMT_YVYU
1244 case V4L2_PIX_FMT_YVYU:
1245 format = GST_VIDEO_FORMAT_YVYU;
1249 g_assert_not_reached ();
1252 structure = gst_structure_new ("video/x-raw",
1253 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1256 case V4L2_PIX_FMT_DV:
1258 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1261 case V4L2_PIX_FMT_MPEG: /* MPEG */
1262 structure = gst_structure_new ("video/mpegts", NULL);
1264 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1266 #ifdef V4L2_PIX_FMT_SBGGR8
1267 case V4L2_PIX_FMT_SBGGR8:
1268 structure = gst_structure_new ("video/x-raw-bayer", NULL);
1271 #ifdef V4L2_PIX_FMT_SN9C10X
1272 case V4L2_PIX_FMT_SN9C10X:
1273 structure = gst_structure_new ("video/x-sonix", NULL);
1276 #ifdef V4L2_PIX_FMT_PWC1
1277 case V4L2_PIX_FMT_PWC1:
1278 structure = gst_structure_new ("video/x-pwc1", NULL);
1281 #ifdef V4L2_PIX_FMT_PWC2
1282 case V4L2_PIX_FMT_PWC2:
1283 structure = gst_structure_new ("video/x-pwc2", NULL);
1287 GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
1288 fourcc, GST_FOURCC_ARGS (fourcc));
1298 gst_v4l2_object_get_all_caps (void)
1300 static GstCaps *caps = NULL;
1303 GstStructure *structure;
1307 caps = gst_caps_new_empty ();
1308 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1310 gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
1312 if (gst_v4l2_formats[i].dimensions) {
1313 gst_structure_set (structure,
1314 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1315 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1316 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
1318 gst_caps_append_structure (caps, structure);
1323 return gst_caps_ref (caps);
1327 /* collect data for the given caps
1328 * @caps: given input caps
1329 * @format: location for the v4l format
1330 * @w/@h: location for width and height
1331 * @fps_n/@fps_d: location for framerate
1332 * @size: location for expected size of the frame or 0 if unknown
1335 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1336 struct v4l2_fmtdesc ** format, gint * w, gint * h,
1337 gboolean * interlaced, guint * fps_n, guint * fps_d, guint * size)
1339 GstStructure *structure;
1340 const GValue *framerate;
1342 const gchar *mimetype;
1345 /* default unknown values */
1349 structure = gst_caps_get_structure (caps, 0);
1351 mimetype = gst_structure_get_name (structure);
1353 if (strcmp (mimetype, "video/mpegts") == 0) {
1354 fourcc = V4L2_PIX_FMT_MPEG;
1360 if (!gst_structure_get_int (structure, "width", w))
1363 if (!gst_structure_get_int (structure, "height", h))
1366 if (!gst_structure_get_boolean (structure, "interlaced", interlaced))
1367 *interlaced = FALSE;
1369 framerate = gst_structure_get_value (structure, "framerate");
1373 *fps_n = gst_value_get_fraction_numerator (framerate);
1374 *fps_d = gst_value_get_fraction_denominator (framerate);
1376 if (!strcmp (mimetype, "video/x-raw")) {
1379 if (!gst_video_info_from_caps (&info, caps))
1380 goto invalid_format;
1382 switch (GST_VIDEO_INFO_FORMAT (&info)) {
1383 case GST_VIDEO_FORMAT_I420:
1384 fourcc = V4L2_PIX_FMT_YUV420;
1385 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1386 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1388 case GST_VIDEO_FORMAT_YUY2:
1389 fourcc = V4L2_PIX_FMT_YUYV;
1390 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1393 case GST_VIDEO_FORMAT_Y41P:
1394 fourcc = V4L2_PIX_FMT_Y41P;
1395 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1398 case GST_VIDEO_FORMAT_UYVY:
1399 fourcc = V4L2_PIX_FMT_UYVY;
1400 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1402 case GST_VIDEO_FORMAT_YV12:
1403 fourcc = V4L2_PIX_FMT_YVU420;
1404 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1405 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1407 case GST_VIDEO_FORMAT_Y41B:
1408 fourcc = V4L2_PIX_FMT_YUV411P;
1409 outsize = GST_ROUND_UP_4 (*w) * *h;
1410 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 4) * *h);
1412 case GST_VIDEO_FORMAT_Y42B:
1413 fourcc = V4L2_PIX_FMT_YUV422P;
1414 outsize = GST_ROUND_UP_4 (*w) * *h;
1415 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * *h);
1417 case GST_VIDEO_FORMAT_NV12:
1418 fourcc = V4L2_PIX_FMT_NV12;
1419 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1420 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1422 case GST_VIDEO_FORMAT_NV21:
1423 fourcc = V4L2_PIX_FMT_NV21;
1424 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1425 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1427 #ifdef V4L2_PIX_FMT_YVYU
1428 case GST_VIDEO_FORMAT_YVYU:
1429 fourcc = V4L2_PIX_FMT_YVYU;
1430 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1433 case GST_VIDEO_FORMAT_RGB15:
1434 fourcc = V4L2_PIX_FMT_RGB555;
1436 case GST_VIDEO_FORMAT_RGB16:
1437 fourcc = V4L2_PIX_FMT_RGB565;
1439 case GST_VIDEO_FORMAT_RGB:
1440 fourcc = V4L2_PIX_FMT_RGB24;
1442 case GST_VIDEO_FORMAT_BGR:
1443 fourcc = V4L2_PIX_FMT_BGR24;
1445 case GST_VIDEO_FORMAT_RGBx:
1446 case GST_VIDEO_FORMAT_RGBA:
1447 fourcc = V4L2_PIX_FMT_RGB32;
1449 case GST_VIDEO_FORMAT_BGRx:
1450 case GST_VIDEO_FORMAT_BGRA:
1451 fourcc = V4L2_PIX_FMT_BGR32;
1456 } else if (strcmp (mimetype, "video/x-dv") == 0) {
1457 fourcc = V4L2_PIX_FMT_DV;
1458 } else if (strcmp (mimetype, "image/jpeg") == 0) {
1459 fourcc = V4L2_PIX_FMT_JPEG;
1460 #ifdef V4L2_PIX_FMT_SBGGR8
1461 } else if (strcmp (mimetype, "video/x-raw-bayer") == 0) {
1462 fourcc = V4L2_PIX_FMT_SBGGR8;
1464 #ifdef V4L2_PIX_FMT_SN9C10X
1465 } else if (strcmp (mimetype, "video/x-sonix") == 0) {
1466 fourcc = V4L2_PIX_FMT_SN9C10X;
1468 #ifdef V4L2_PIX_FMT_PWC1
1469 } else if (strcmp (mimetype, "video/x-pwc1") == 0) {
1470 fourcc = V4L2_PIX_FMT_PWC1;
1472 #ifdef V4L2_PIX_FMT_PWC2
1473 } else if (strcmp (mimetype, "video/x-pwc2") == 0) {
1474 fourcc = V4L2_PIX_FMT_PWC2;
1476 } else if (strcmp (mimetype, "video/x-raw-gray") == 0) {
1477 fourcc = V4L2_PIX_FMT_GREY;
1481 goto unhandled_format;
1484 *format = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1492 GST_DEBUG_OBJECT (v4l2object, "no width");
1497 GST_DEBUG_OBJECT (v4l2object, "no height");
1502 GST_DEBUG_OBJECT (v4l2object, "no framerate");
1507 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1512 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1519 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1520 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced);
1523 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1524 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1525 static GstStructure *
1526 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1527 guint32 pixelformat,
1528 guint32 width, guint32 height, const GstStructure * template)
1530 gint fd = v4l2object->video_fd;
1531 struct v4l2_frmivalenum ival;
1534 GValue rates = { 0, };
1535 gboolean interlaced;
1536 gint int_width = width;
1537 gint int_height = height;
1539 /* interlaced detection using VIDIOC_TRY/S_FMT */
1540 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat,
1541 &int_width, &int_height, &interlaced))
1544 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1546 ival.pixel_format = pixelformat;
1548 ival.height = height;
1550 GST_LOG_OBJECT (v4l2object->element,
1551 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1552 GST_FOURCC_ARGS (pixelformat));
1554 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1555 * fraction to get framerate */
1556 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1557 goto enum_frameintervals_failed;
1559 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1560 GValue rate = { 0, };
1562 g_value_init (&rates, GST_TYPE_LIST);
1563 g_value_init (&rate, GST_TYPE_FRACTION);
1566 num = ival.discrete.numerator;
1567 denom = ival.discrete.denominator;
1569 if (num > G_MAXINT || denom > G_MAXINT) {
1570 /* let us hope we don't get here... */
1575 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1578 /* swap to get the framerate */
1579 gst_value_set_fraction (&rate, denom, num);
1580 gst_value_list_append_value (&rates, &rate);
1583 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1584 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1585 GValue min = { 0, };
1586 GValue step = { 0, };
1587 GValue max = { 0, };
1588 gboolean added = FALSE;
1589 guint32 minnum, mindenom;
1590 guint32 maxnum, maxdenom;
1592 g_value_init (&rates, GST_TYPE_LIST);
1594 g_value_init (&min, GST_TYPE_FRACTION);
1595 g_value_init (&step, GST_TYPE_FRACTION);
1596 g_value_init (&max, GST_TYPE_FRACTION);
1599 minnum = ival.stepwise.min.numerator;
1600 mindenom = ival.stepwise.min.denominator;
1601 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1605 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1607 gst_value_set_fraction (&min, minnum, mindenom);
1610 maxnum = ival.stepwise.max.numerator;
1611 maxdenom = ival.stepwise.max.denominator;
1612 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1617 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1619 gst_value_set_fraction (&max, maxnum, maxdenom);
1622 num = ival.stepwise.step.numerator;
1623 denom = ival.stepwise.step.denominator;
1624 if (num > G_MAXINT || denom > G_MAXINT) {
1629 if (num == 0 || denom == 0) {
1630 /* in this case we have a wrong fraction or no step, set the step to max
1631 * so that we only add the min value in the loop below */
1636 /* since we only have gst_value_fraction_subtract and not add, negate the
1638 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1640 gst_value_set_fraction (&step, -num, denom);
1642 while (gst_value_compare (&min, &max) <= 0) {
1643 GValue rate = { 0, };
1645 num = gst_value_get_fraction_numerator (&min);
1646 denom = gst_value_get_fraction_denominator (&min);
1647 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1650 /* invert to get the framerate */
1651 g_value_init (&rate, GST_TYPE_FRACTION);
1652 gst_value_set_fraction (&rate, denom, num);
1653 gst_value_list_append_value (&rates, &rate);
1656 /* we're actually adding because step was negated above. This is because
1657 * there is no _add function... */
1658 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1659 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1664 /* no range was added, leave the default range from the template */
1665 GST_WARNING_OBJECT (v4l2object->element,
1666 "no range added, leaving default");
1667 g_value_unset (&rates);
1669 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1670 guint32 maxnum, maxdenom;
1672 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1674 num = ival.stepwise.min.numerator;
1675 denom = ival.stepwise.min.denominator;
1676 if (num > G_MAXINT || denom > G_MAXINT) {
1681 maxnum = ival.stepwise.max.numerator;
1682 maxdenom = ival.stepwise.max.denominator;
1683 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1688 GST_LOG_OBJECT (v4l2object->element,
1689 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
1692 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1698 s = gst_structure_copy (template);
1699 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
1700 "height", G_TYPE_INT, (gint) height,
1701 "interlaced", G_TYPE_BOOLEAN, interlaced, NULL);
1703 if (G_IS_VALUE (&rates)) {
1704 /* only change the framerate on the template when we have a valid probed new
1706 gst_structure_set_value (s, "framerate", &rates);
1707 g_value_unset (&rates);
1709 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
1715 enum_frameintervals_failed:
1717 GST_DEBUG_OBJECT (v4l2object->element,
1718 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
1719 GST_FOURCC_ARGS (pixelformat), width, height);
1724 /* I don't see how this is actually an error, we ignore the format then */
1725 GST_WARNING_OBJECT (v4l2object->element,
1726 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
1727 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
1731 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
1733 #ifdef VIDIOC_ENUM_FRAMESIZES
1735 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
1739 gst_structure_get_int (s1, "width", &w1);
1740 gst_structure_get_int (s1, "height", &h1);
1741 gst_structure_get_int (s2, "width", &w2);
1742 gst_structure_get_int (s2, "height", &h2);
1744 /* I think it's safe to assume that this won't overflow for a while */
1745 return ((w2 * h2) - (w1 * h1));
1750 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
1751 guint32 pixelformat, const GstStructure * template)
1753 GstCaps *ret = gst_caps_new_empty ();
1756 #ifdef VIDIOC_ENUM_FRAMESIZES
1757 gint fd = v4l2object->video_fd;
1758 struct v4l2_frmsizeenum size;
1759 GList *results = NULL;
1762 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1763 return gst_caps_new_simple ("video/mpegts", NULL);
1765 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
1767 size.pixel_format = pixelformat;
1769 GST_DEBUG_OBJECT (v4l2object->element, "Enumerating frame sizes");
1771 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
1772 goto enum_framesizes_failed;
1774 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1776 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
1777 size.discrete.width, size.discrete.height);
1779 w = MIN (size.discrete.width, G_MAXINT);
1780 h = MIN (size.discrete.height, G_MAXINT);
1784 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1785 pixelformat, w, h, template);
1788 results = g_list_prepend (results, tmp);
1792 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
1793 GST_DEBUG_OBJECT (v4l2object->element,
1794 "done iterating discrete frame sizes");
1795 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
1796 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
1797 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1798 size.stepwise.min_width);
1799 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1800 size.stepwise.min_height);
1801 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1802 size.stepwise.max_width);
1803 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1804 size.stepwise.max_height);
1805 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
1806 size.stepwise.step_width);
1807 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
1808 size.stepwise.step_height);
1810 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
1811 w < size.stepwise.max_width && h < size.stepwise.max_height;
1812 w += size.stepwise.step_width, h += size.stepwise.step_height) {
1813 if (w == 0 || h == 0)
1817 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1818 pixelformat, w, h, template);
1821 results = g_list_prepend (results, tmp);
1823 GST_DEBUG_OBJECT (v4l2object->element,
1824 "done iterating stepwise frame sizes");
1825 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
1828 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
1829 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1830 size.stepwise.min_width);
1831 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1832 size.stepwise.min_height);
1833 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1834 size.stepwise.max_width);
1835 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1836 size.stepwise.max_height);
1838 w = MAX (size.stepwise.min_width, 1);
1839 h = MAX (size.stepwise.min_height, 1);
1840 maxw = MIN (size.stepwise.max_width, G_MAXINT);
1841 maxh = MIN (size.stepwise.max_height, G_MAXINT);
1844 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
1847 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
1848 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
1851 /* no point using the results list here, since there's only one struct */
1852 gst_caps_append_structure (ret, tmp);
1858 /* we use an intermediary list to store and then sort the results of the
1859 * probing because we can't make any assumptions about the order in which
1860 * the driver will give us the sizes, but we want the final caps to contain
1861 * the results starting with the highest resolution and having the lowest
1862 * resolution last, since order in caps matters for things like fixation. */
1863 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
1864 while (results != NULL) {
1865 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
1866 results = g_list_delete_link (results, results);
1869 if (gst_caps_is_empty (ret))
1870 goto enum_framesizes_no_results;
1875 enum_framesizes_failed:
1877 /* I don't see how this is actually an error */
1878 GST_DEBUG_OBJECT (v4l2object->element,
1879 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
1880 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
1881 goto default_frame_sizes;
1883 enum_framesizes_no_results:
1885 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
1886 * question doesn't actually support it yet */
1887 GST_DEBUG_OBJECT (v4l2object->element,
1888 "No results for pixelformat %" GST_FOURCC_FORMAT
1889 " enumerating frame sizes, trying fallback",
1890 GST_FOURCC_ARGS (pixelformat));
1891 goto default_frame_sizes;
1895 GST_WARNING_OBJECT (v4l2object->element,
1896 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
1897 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
1898 goto default_frame_sizes;
1900 default_frame_sizes:
1901 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
1903 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
1904 gboolean interlaced;
1906 /* This code is for Linux < 2.6.19 */
1908 max_w = max_h = GST_V4L2_MAX_SIZE;
1909 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
1910 &min_h, &interlaced)) {
1911 GST_WARNING_OBJECT (v4l2object->element,
1912 "Could not probe minimum capture size for pixelformat %"
1913 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1915 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
1916 &max_h, &interlaced)) {
1917 GST_WARNING_OBJECT (v4l2object->element,
1918 "Could not probe maximum capture size for pixelformat %"
1919 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1922 /* Since we can't get framerate directly, try to use the current norm */
1923 if (v4l2object->tv_norm && v4l2object->norms) {
1925 GstTunerNorm *norm = NULL;
1926 GstTunerNorm *current =
1927 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
1929 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
1930 norm = (GstTunerNorm *) norms->data;
1931 if (!strcmp (norm->label, current->label))
1934 /* If it's possible, set framerate to that (discrete) value */
1936 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
1937 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
1941 tmp = gst_structure_copy (template);
1943 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
1946 /* if norm can't be used, copy the template framerate */
1947 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
1952 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
1954 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
1957 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
1959 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
1961 gst_structure_set (tmp, "interlaced", G_TYPE_BOOLEAN, interlaced, NULL);
1963 gst_caps_append_structure (ret, tmp);
1970 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1971 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced)
1973 struct v4l2_format fmt;
1977 g_return_val_if_fail (width != NULL, FALSE);
1978 g_return_val_if_fail (height != NULL, FALSE);
1980 GST_LOG_OBJECT (v4l2object->element,
1981 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
1982 *width, *height, GST_FOURCC_ARGS (pixelformat));
1984 fd = v4l2object->video_fd;
1986 /* get size delimiters */
1987 memset (&fmt, 0, sizeof (fmt));
1988 fmt.type = v4l2object->type;
1989 fmt.fmt.pix.width = *width;
1990 fmt.fmt.pix.height = *height;
1991 fmt.fmt.pix.pixelformat = pixelformat;
1992 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1994 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
1995 if (r < 0 && errno == EINVAL) {
1996 /* try again with interlaced video */
1997 fmt.fmt.pix.width = *width;
1998 fmt.fmt.pix.height = *height;
1999 fmt.fmt.pix.pixelformat = pixelformat;
2000 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2001 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2005 /* The driver might not implement TRY_FMT, in which case we will try
2007 if (errno != ENOTTY)
2010 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
2011 be, because we're still probing */
2012 if (GST_V4L2_IS_ACTIVE (v4l2object))
2015 GST_LOG_OBJECT (v4l2object->element,
2016 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
2018 fmt.fmt.pix.width = *width;
2019 fmt.fmt.pix.height = *height;
2021 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2022 if (r < 0 && errno == EINVAL) {
2023 /* try again with progressive video */
2024 fmt.fmt.pix.width = *width;
2025 fmt.fmt.pix.height = *height;
2026 fmt.fmt.pix.pixelformat = pixelformat;
2027 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2028 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2035 GST_LOG_OBJECT (v4l2object->element,
2036 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2038 *width = fmt.fmt.pix.width;
2039 *height = fmt.fmt.pix.height;
2041 switch (fmt.fmt.pix.field) {
2042 case V4L2_FIELD_ANY:
2043 case V4L2_FIELD_NONE:
2044 *interlaced = FALSE;
2046 case V4L2_FIELD_INTERLACED:
2047 case V4L2_FIELD_INTERLACED_TB:
2048 case V4L2_FIELD_INTERLACED_BT:
2052 GST_WARNING_OBJECT (v4l2object->element,
2053 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u",
2054 GST_FOURCC_ARGS (pixelformat), *width, *height);
2063 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, guint32 pixelformat,
2064 guint32 width, guint32 height, gboolean interlaced)
2066 gint fd = v4l2object->video_fd;
2067 struct v4l2_format format;
2068 enum v4l2_field field;
2071 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
2072 /* ideally we would differentiate between types of interlaced video
2073 * but there is not sufficient information in the caps..
2075 field = V4L2_FIELD_INTERLACED;
2077 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
2078 field = V4L2_FIELD_NONE;
2081 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2082 "%" GST_FOURCC_FORMAT, width, height, GST_FOURCC_ARGS (pixelformat));
2084 GST_V4L2_CHECK_OPEN (v4l2object);
2085 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2087 /* Only unconditionally accept mpegts for sources */
2088 if ((v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
2089 (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')))
2092 memset (&format, 0x00, sizeof (struct v4l2_format));
2093 format.type = v4l2object->type;
2095 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
2096 goto get_fmt_failed;
2098 if (format.type == v4l2object->type &&
2099 format.fmt.pix.width == width &&
2100 format.fmt.pix.height == height &&
2101 format.fmt.pix.pixelformat == pixelformat &&
2102 format.fmt.pix.field == field) {
2103 /* Nothing to do. We want to succeed immediately
2104 * here because setting the same format back
2105 * can still fail due to EBUSY. By short-circuiting
2106 * here, we allow pausing and re-playing pipelines
2107 * with changed caps, as long as the changed caps
2108 * do not change the webcam's format. Otherwise,
2109 * any caps change would require us to go to NULL
2110 * state to close the device and set format.
2115 format.type = v4l2object->type;
2116 format.fmt.pix.width = width;
2117 format.fmt.pix.height = height;
2118 format.fmt.pix.pixelformat = pixelformat;
2119 format.fmt.pix.field = field;
2121 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0) {
2122 goto set_fmt_failed;
2125 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
2126 goto invalid_dimensions;
2128 if (format.fmt.pix.pixelformat != pixelformat)
2129 goto invalid_pixelformat;
2136 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2137 (_("Device '%s' does not support video capture"),
2138 v4l2object->videodev),
2139 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
2144 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2145 (_("Device '%s' cannot capture at %dx%d"),
2146 v4l2object->videodev, width, height),
2147 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2148 GST_FOURCC_ARGS (pixelformat), width, height, g_strerror (errno)));
2153 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2154 (_("Device '%s' cannot capture at %dx%d"),
2155 v4l2object->videodev, width, height),
2156 ("Tried to capture at %dx%d, but device returned size %dx%d",
2157 width, height, format.fmt.pix.width, format.fmt.pix.height));
2160 invalid_pixelformat:
2162 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2163 (_("Device '%s' cannot capture in the specified format"),
2164 v4l2object->videodev),
2165 ("Tried to capture in %" GST_FOURCC_FORMAT
2166 ", but device returned format" " %" GST_FOURCC_FORMAT,
2167 GST_FOURCC_ARGS (pixelformat),
2168 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
2174 gst_v4l2_object_start_streaming (GstV4l2Object * v4l2object)
2176 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_STREAMON,
2177 &(v4l2object->type)) < 0) {
2178 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ,
2179 (_("Error starting streaming on device '%s'."), v4l2object->videodev),
2187 gst_v4l2_object_stop_streaming (GstV4l2Object * v4l2object)
2189 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_STREAMOFF,
2190 &(v4l2object->type)) < 0) {
2191 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ,
2192 (_("Error stopping streaming on device '%s'."), v4l2object->videodev),