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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
21 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
22 * with newer GLib versions (>= 2.31.0) */
23 #define GLIB_DISABLE_DEPRECATION_WARNINGS
36 #include <gudev/gudev.h>
39 #include "v4l2_calls.h"
40 #include "gstv4l2tuner.h"
42 #include "gstv4l2videooverlay.h"
44 #include "gstv4l2colorbalance.h"
46 #include "gst/gst-i18n-plugin.h"
48 #include <gst/video/video.h>
50 /* videodev2.h is not versioned and we can't easily check for the presence
51 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
52 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
53 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
54 #define V4L2_FIELD_INTERLACED_TB 8
55 #define V4L2_FIELD_INTERLACED_BT 9
58 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
59 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
60 #define GST_CAT_DEFAULT v4l2_debug
62 #define DEFAULT_PROP_DEVICE_NAME NULL
63 #define DEFAULT_PROP_DEVICE_FD -1
64 #define DEFAULT_PROP_FLAGS 0
65 #define DEFAULT_PROP_TV_NORM 0
66 #define DEFAULT_PROP_CHANNEL NULL
67 #define DEFAULT_PROP_FREQUENCY 0
68 #define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
73 V4L2_STD_OBJECT_PROPS,
76 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
79 G_LOCK_DEFINE_STATIC (probe_lock);
82 gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
84 GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
85 static GList *list = NULL;
90 list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
93 G_UNLOCK (probe_lock);
98 static gboolean init = FALSE;
99 static GList *devices = NULL;
103 gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
104 GList ** klass_devices)
106 GUdevClient *client = NULL;
111 gchar *device = devices->data;
112 devices = g_list_remove (devices, device);
116 GST_INFO ("Enumerating video4linux devices from udev");
117 client = g_udev_client_new (NULL);
119 GST_WARNING ("Failed to initialize gudev client");
123 item = g_udev_client_query_by_subsystem (client, "video4linux");
125 GUdevDevice *device = item->data;
126 gchar *devnode = g_strdup (g_udev_device_get_device_file (device));
127 gint api = g_udev_device_get_property_as_int (device, "ID_V4L_VERSION");
128 GST_INFO ("Found new device: %s, API: %d", devnode, api);
129 /* Append v4l2 devices only. If api is 0 probably v4l_id has
130 been stripped out of the current udev installation, append
134 ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
136 if ((api == 2 || api == 0)) {
137 devices = g_list_append (devices, devnode);
141 g_object_unref (device);
150 g_object_unref (client);
153 *klass_devices = devices;
157 #endif /* HAVE_GUDEV */
160 gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
161 GList ** klass_devices)
164 const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
168 gchar *device = devices->data;
169 devices = g_list_remove (devices, device);
174 * detect /dev entries
176 for (n = 0; n < 64; n++) {
177 for (base = 0; dev_base[base] != NULL; base++) {
179 gchar *device = g_strdup_printf ("%s%d",
184 * does the /dev/ entry exist at all?
186 if (stat (device, &s) == 0) {
188 * yes: is a device attached?
190 if (S_ISCHR (s.st_mode)) {
192 if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
196 devices = g_list_append (devices, device);
207 *klass_devices = devices;
213 gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
214 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
216 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
221 if (!gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices))
222 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
223 #else /* !HAVE_GUDEV */
224 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
225 #endif /* HAVE_GUDEV */
228 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
234 gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
235 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
237 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
238 gboolean ret = FALSE;
244 !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
245 #else /* !HAVE_GUDEV */
246 ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
247 #endif /* HAVE_GUDEV */
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
257 gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
260 GValue value = { 0 };
266 array = g_value_array_new (g_list_length (*klass_devices));
267 item = *klass_devices;
268 g_value_init (&value, G_TYPE_STRING);
270 gchar *device = item->data;
272 g_value_set_string (&value, device);
273 g_value_array_append (array, &value);
277 g_value_unset (&value);
283 gst_v4l2_probe_get_values (GstPropertyProbe * probe,
284 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
286 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
287 GValueArray *array = NULL;
291 array = gst_v4l2_class_list_devices (klass, klass_devices);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
302 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
304 gst_v4l2_device_get_type (void)
306 static GType v4l2_device_type = 0;
308 if (v4l2_device_type == 0) {
309 static const GFlagsValue values[] = {
310 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
311 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
312 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
314 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
315 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
317 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
318 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
324 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
327 return v4l2_device_type;
330 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
332 gst_v4l2_tv_norm_get_type (void)
334 static GType v4l2_tv_norm = 0;
337 static const GEnumValue tv_norms[] = {
340 {V4L2_STD_NTSC, "NTSC", "NTSC"},
341 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
342 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
343 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
344 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
346 {V4L2_STD_PAL, "PAL", "PAL"},
347 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
348 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
349 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
350 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
351 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
352 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
353 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
354 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
355 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
356 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
357 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
358 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
359 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
360 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
362 {V4L2_STD_SECAM, "SECAM", "SECAM"},
363 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
364 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
365 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
366 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
367 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
368 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
369 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
370 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
371 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
376 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
382 #define GST_TYPE_V4L2_IO_MODE (gst_v4l2_io_mode_get_type ())
384 gst_v4l2_io_mode_get_type (void)
386 static GType v4l2_io_mode = 0;
389 static const GEnumValue io_modes[] = {
390 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
391 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
392 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
393 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
394 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
398 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
404 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
405 const char *default_device)
407 g_object_class_install_property (gobject_class, PROP_DEVICE,
408 g_param_spec_string ("device", "Device", "Device location",
409 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
410 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
411 g_param_spec_string ("device-name", "Device name",
412 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
413 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
414 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
415 g_param_spec_int ("device-fd", "File descriptor",
416 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
417 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
418 g_object_class_install_property (gobject_class, PROP_FLAGS,
419 g_param_spec_flags ("flags", "Flags", "Device type flags",
420 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
421 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
424 * GstV4l2Src:brightness
426 * Picture brightness, or more precisely, the black level
430 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
431 g_param_spec_int ("brightness", "Brightness",
432 "Picture brightness, or more precisely, the black level", G_MININT,
434 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
436 * GstV4l2Src:contrast
438 * Picture contrast or luma gain
442 g_object_class_install_property (gobject_class, PROP_CONTRAST,
443 g_param_spec_int ("contrast", "Contrast",
444 "Picture contrast or luma gain", G_MININT,
446 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
448 * GstV4l2Src:saturation
450 * Picture color saturation or chroma gain
454 g_object_class_install_property (gobject_class, PROP_SATURATION,
455 g_param_spec_int ("saturation", "Saturation",
456 "Picture color saturation or chroma gain", G_MININT,
458 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
462 * Hue or color balance
466 g_object_class_install_property (gobject_class, PROP_HUE,
467 g_param_spec_int ("hue", "Hue",
468 "Hue or color balance", G_MININT,
470 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
479 g_object_class_install_property (gobject_class, PROP_TV_NORM,
480 g_param_spec_enum ("norm", "TV norm",
482 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
483 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
490 g_object_class_install_property (gobject_class, PROP_IO_MODE,
491 g_param_spec_enum ("io-mode", "IO mode",
493 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
494 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
497 * GstV4l2Src:extra-controls
499 * Additional v4l2 controls for the device. The controls are identified
500 * by the control name (lowercase with '_' for any non-alphanumeric
505 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
506 g_param_spec_boxed ("extra-controls", "Extra Controls",
507 "Extra v4l2 controls (CIDs) for the device",
508 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
511 * GstV4l2Src:pixel-aspect-ratio
513 * The pixel aspect ratio of the device. This overwrites the pixel aspect
514 * ratio queried from the device.
518 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
519 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
520 "Overwrite the pixel aspect ratio of the device", "1/1",
521 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
524 * GstV4l2Src:force-aspect-ratio
526 * When enabled, the pixel aspect ratio queried from the device or set
527 * with the pixel-aspect-ratio property will be enforced.
531 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
532 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
533 "When enabled, the pixel aspect ratio will be enforced", TRUE,
534 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
539 gst_v4l2_object_new (GstElement * element,
540 enum v4l2_buf_type type,
541 const char *default_device,
542 GstV4l2GetInOutFunction get_in_out_func,
543 GstV4l2SetInOutFunction set_in_out_func,
544 GstV4l2UpdateFpsFunction update_fps_func)
546 GstV4l2Object *v4l2object;
549 * some default values
551 v4l2object = g_new0 (GstV4l2Object, 1);
553 v4l2object->type = type;
554 v4l2object->formats = NULL;
556 v4l2object->element = element;
557 v4l2object->get_in_out_func = get_in_out_func;
558 v4l2object->set_in_out_func = set_in_out_func;
559 v4l2object->update_fps_func = update_fps_func;
561 v4l2object->video_fd = -1;
562 v4l2object->poll = gst_poll_new (TRUE);
563 v4l2object->active = FALSE;
564 v4l2object->videodev = g_strdup (default_device);
566 v4l2object->norms = NULL;
567 v4l2object->channels = NULL;
568 v4l2object->colors = NULL;
570 v4l2object->xwindow_id = 0;
572 v4l2object->keep_aspect = TRUE;
577 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
581 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
583 g_return_if_fail (v4l2object != NULL);
585 if (v4l2object->videodev)
586 g_free (v4l2object->videodev);
588 if (v4l2object->poll)
589 gst_poll_free (v4l2object->poll);
591 if (v4l2object->channel)
592 g_free (v4l2object->channel);
594 if (v4l2object->formats) {
595 gst_v4l2_object_clear_format_list (v4l2object);
598 if (v4l2object->probed_caps) {
599 gst_caps_unref (v4l2object->probed_caps);
607 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
609 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
610 g_slist_free (v4l2object->formats);
611 v4l2object->formats = NULL;
617 gst_v4l2_object_prop_to_cid (guint prop_id)
622 case PROP_BRIGHTNESS:
623 cid = V4L2_CID_BRIGHTNESS;
626 cid = V4L2_CID_CONTRAST;
628 case PROP_SATURATION:
629 cid = V4L2_CID_SATURATION;
635 GST_WARNING ("unmapped property id: %d", prop_id);
642 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
643 guint prop_id, const GValue * value, GParamSpec * pspec)
647 g_free (v4l2object->videodev);
648 v4l2object->videodev = g_value_dup_string (value);
650 case PROP_BRIGHTNESS:
652 case PROP_SATURATION:
655 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
658 if (GST_V4L2_IS_OPEN (v4l2object)) {
659 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
666 v4l2object->tv_norm = g_value_get_enum (value);
670 if (GST_V4L2_IS_OPEN (v4l2object)) {
671 GstTuner *tuner = GST_TUNER (v4l2object->element);
672 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
673 (gchar *) g_value_get_string (value));
676 /* like gst_tuner_set_channel (tuner, channel)
677 without g_object_notify */
678 gst_v4l2_tuner_set_channel (v4l2object, channel);
681 g_free (v4l2object->channel);
682 v4l2object->channel = g_value_dup_string (value);
686 if (GST_V4L2_IS_OPEN (v4l2object)) {
687 GstTuner *tuner = GST_TUNER (v4l2object->element);
688 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
691 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
693 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
694 without g_object_notify */
695 gst_v4l2_tuner_set_frequency (v4l2object, channel,
696 g_value_get_ulong (value));
699 v4l2object->frequency = g_value_get_ulong (value);
704 v4l2object->req_mode = g_value_get_enum (value);
706 case PROP_EXTRA_CONTROLS:{
707 const GstStructure *s = gst_value_get_structure (value);
709 if (v4l2object->extra_controls)
710 gst_structure_free (v4l2object->extra_controls);
712 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
713 if (GST_V4L2_IS_OPEN (v4l2object))
714 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
717 case PROP_PIXEL_ASPECT_RATIO:
718 g_free (v4l2object->par);
719 v4l2object->par = g_new0 (GValue, 1);
720 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
721 if (!g_value_transform (value, v4l2object->par)) {
722 g_warning ("Could not transform string to aspect ratio");
723 gst_value_set_fraction (v4l2object->par, 1, 1);
725 GST_DEBUG_OBJECT (v4l2object->element, "set PAR to %d/%d",
726 gst_value_get_fraction_numerator (v4l2object->par),
727 gst_value_get_fraction_denominator (v4l2object->par));
729 case PROP_FORCE_ASPECT_RATIO:
730 v4l2object->keep_aspect = g_value_get_boolean (value);
741 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
742 guint prop_id, GValue * value, GParamSpec * pspec)
746 g_value_set_string (value, v4l2object->videodev);
748 case PROP_DEVICE_NAME:
750 const guchar *new = NULL;
752 if (GST_V4L2_IS_OPEN (v4l2object)) {
753 new = v4l2object->vcap.card;
754 } else if (gst_v4l2_open (v4l2object)) {
755 new = v4l2object->vcap.card;
756 gst_v4l2_close (v4l2object);
758 g_value_set_string (value, (gchar *) new);
763 if (GST_V4L2_IS_OPEN (v4l2object))
764 g_value_set_int (value, v4l2object->video_fd);
766 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
773 if (GST_V4L2_IS_OPEN (v4l2object)) {
774 flags |= v4l2object->vcap.capabilities &
775 (V4L2_CAP_VIDEO_CAPTURE |
776 V4L2_CAP_VIDEO_OUTPUT |
777 V4L2_CAP_VIDEO_OVERLAY |
778 V4L2_CAP_VBI_CAPTURE |
779 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
781 g_value_set_flags (value, flags);
784 case PROP_BRIGHTNESS:
786 case PROP_SATURATION:
789 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
792 if (GST_V4L2_IS_OPEN (v4l2object)) {
794 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
795 g_value_set_int (value, v);
803 g_value_set_enum (value, v4l2object->tv_norm);
806 g_value_set_enum (value, v4l2object->req_mode);
808 case PROP_EXTRA_CONTROLS:
809 gst_value_set_structure (value, v4l2object->extra_controls);
811 case PROP_PIXEL_ASPECT_RATIO:
813 g_value_transform (v4l2object->par, value);
815 case PROP_FORCE_ASPECT_RATIO:
816 g_value_set_boolean (value, v4l2object->keep_aspect);
826 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
828 GstTunerNorm *norm = NULL;
829 GstTunerChannel *channel = NULL;
832 if (!GST_IS_TUNER (v4l2object->element))
835 tuner = GST_TUNER (v4l2object->element);
837 if (v4l2object->tv_norm)
838 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
839 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
840 "norm=%p", (guint64) v4l2object->tv_norm, norm);
842 gst_tuner_set_norm (tuner, norm);
845 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
847 v4l2object->tv_norm =
848 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
849 gst_tuner_norm_changed (tuner, norm);
853 if (v4l2object->channel)
854 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
856 gst_tuner_set_channel (tuner, channel);
859 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
860 (v4l2object->element)));
862 g_free (v4l2object->channel);
863 v4l2object->channel = g_strdup (channel->label);
864 gst_tuner_channel_changed (tuner, channel);
869 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
870 if (v4l2object->frequency != 0) {
871 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
873 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
874 if (v4l2object->frequency == 0) {
876 gst_tuner_set_frequency (tuner, channel, 1000);
884 gst_v4l2_object_open (GstV4l2Object * v4l2object)
886 if (gst_v4l2_open (v4l2object))
887 gst_v4l2_set_defaults (v4l2object);
892 gst_v4l2_video_overlay_start (v4l2object);
899 gst_v4l2_object_close (GstV4l2Object * v4l2object)
902 gst_v4l2_video_overlay_stop (v4l2object);
905 if (!gst_v4l2_close (v4l2object))
908 if (v4l2object->formats) {
909 gst_v4l2_object_clear_format_list (v4l2object);
917 * common format / caps utilities:
925 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
926 /* from Linux 2.6.15 videodev2.h */
927 {V4L2_PIX_FMT_RGB332, TRUE},
928 {V4L2_PIX_FMT_RGB555, TRUE},
929 {V4L2_PIX_FMT_RGB565, TRUE},
930 {V4L2_PIX_FMT_RGB555X, TRUE},
931 {V4L2_PIX_FMT_RGB565X, TRUE},
932 {V4L2_PIX_FMT_BGR24, TRUE},
933 {V4L2_PIX_FMT_RGB24, TRUE},
934 {V4L2_PIX_FMT_BGR32, TRUE},
935 {V4L2_PIX_FMT_RGB32, TRUE},
936 {V4L2_PIX_FMT_GREY, TRUE},
937 {V4L2_PIX_FMT_YVU410, TRUE},
938 {V4L2_PIX_FMT_YVU420, TRUE},
939 {V4L2_PIX_FMT_YUYV, TRUE},
940 {V4L2_PIX_FMT_UYVY, TRUE},
941 {V4L2_PIX_FMT_YUV422P, TRUE},
942 {V4L2_PIX_FMT_YUV411P, TRUE},
943 {V4L2_PIX_FMT_Y41P, TRUE},
945 /* two planes -- one Y, one Cr + Cb interleaved */
946 {V4L2_PIX_FMT_NV12, TRUE},
947 {V4L2_PIX_FMT_NV21, TRUE},
949 /* The following formats are not defined in the V4L2 specification */
950 {V4L2_PIX_FMT_YUV410, TRUE},
951 {V4L2_PIX_FMT_YUV420, TRUE},
952 {V4L2_PIX_FMT_YYUV, TRUE},
953 {V4L2_PIX_FMT_HI240, TRUE},
955 /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
956 #ifdef V4L2_PIX_FMT_SBGGR8
957 {V4L2_PIX_FMT_SBGGR8, TRUE},
960 /* compressed formats */
961 {V4L2_PIX_FMT_MJPEG, TRUE},
962 {V4L2_PIX_FMT_JPEG, TRUE},
963 #ifdef V4L2_PIX_FMT_PJPG
964 {V4L2_PIX_FMT_PJPG, TRUE},
966 {V4L2_PIX_FMT_DV, TRUE},
967 {V4L2_PIX_FMT_MPEG, FALSE},
968 #ifdef V4L2_PIX_FMT_MPEG4
969 {V4L2_PIX_FMT_MPEG4, TRUE},
972 #ifdef V4L2_PIX_FMT_H263
973 {V4L2_PIX_FMT_H263, TRUE},
975 #ifdef V4L2_PIX_FMT_H264
976 {V4L2_PIX_FMT_H264, TRUE},
979 /* Vendor-specific formats */
980 {V4L2_PIX_FMT_WNVA, TRUE},
982 #ifdef V4L2_PIX_FMT_SN9C10X
983 {V4L2_PIX_FMT_SN9C10X, TRUE},
985 #ifdef V4L2_PIX_FMT_PWC1
986 {V4L2_PIX_FMT_PWC1, TRUE},
988 #ifdef V4L2_PIX_FMT_PWC2
989 {V4L2_PIX_FMT_PWC2, TRUE},
991 #ifdef V4L2_PIX_FMT_YVYU
992 {V4L2_PIX_FMT_YVYU, TRUE},
996 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
999 static struct v4l2_fmtdesc *
1000 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
1003 struct v4l2_fmtdesc *fmt;
1009 walk = gst_v4l2_object_get_format_list (v4l2object);
1011 fmt = (struct v4l2_fmtdesc *) walk->data;
1012 if (fmt->pixelformat == fourcc)
1014 /* special case for jpeg */
1015 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
1016 fmt->pixelformat == V4L2_PIX_FMT_JPEG
1017 #ifdef V4L2_PIX_FMT_PJPG
1018 || fmt->pixelformat == V4L2_PIX_FMT_PJPG
1021 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG
1022 #ifdef V4L2_PIX_FMT_PJPG
1023 || fourcc == V4L2_PIX_FMT_PJPG
1029 walk = g_slist_next (walk);
1037 /* complete made up ranking, the values themselves are meaningless */
1038 /* These ranks MUST be X such that X<<15 fits on a signed int - see
1039 the comment at the end of gst_v4l2_object_format_get_rank. */
1040 #define YUV_BASE_RANK 1000
1041 #define JPEG_BASE_RANK 500
1042 #define DV_BASE_RANK 200
1043 #define RGB_BASE_RANK 100
1044 #define YUV_ODD_BASE_RANK 50
1045 #define RGB_ODD_BASE_RANK 25
1046 #define BAYER_BASE_RANK 15
1047 #define S910_BASE_RANK 10
1048 #define GREY_BASE_RANK 5
1049 #define PWC_BASE_RANK 1
1051 /* This flag is already used by libv4l2 although
1052 * it was added to the Linux kernel in 2.6.32
1054 #ifndef V4L2_FMT_FLAG_EMULATED
1055 #define V4L2_FMT_FLAG_EMULATED 0x0002
1059 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
1061 guint32 fourcc = fmt->pixelformat;
1062 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
1066 case V4L2_PIX_FMT_MJPEG:
1067 #ifdef V4L2_PIX_FMT_PJPG
1068 case V4L2_PIX_FMT_PJPG:
1069 rank = JPEG_BASE_RANK;
1072 case V4L2_PIX_FMT_JPEG:
1073 rank = JPEG_BASE_RANK + 1;
1075 case V4L2_PIX_FMT_MPEG: /* MPEG */
1076 rank = JPEG_BASE_RANK + 2;
1079 case V4L2_PIX_FMT_RGB332:
1080 case V4L2_PIX_FMT_RGB555:
1081 case V4L2_PIX_FMT_RGB555X:
1082 case V4L2_PIX_FMT_RGB565:
1083 case V4L2_PIX_FMT_RGB565X:
1084 rank = RGB_ODD_BASE_RANK;
1087 case V4L2_PIX_FMT_RGB24:
1088 case V4L2_PIX_FMT_BGR24:
1089 rank = RGB_BASE_RANK - 1;
1092 case V4L2_PIX_FMT_RGB32:
1093 case V4L2_PIX_FMT_BGR32:
1094 rank = RGB_BASE_RANK;
1097 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1098 rank = GREY_BASE_RANK;
1101 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1102 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1103 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1104 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1105 rank = YUV_ODD_BASE_RANK;
1108 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1109 rank = YUV_BASE_RANK + 3;
1111 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1112 rank = YUV_BASE_RANK + 2;
1114 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1115 rank = YUV_BASE_RANK + 7;
1117 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1118 rank = YUV_BASE_RANK + 10;
1120 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1121 rank = YUV_BASE_RANK + 6;
1123 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1124 rank = YUV_BASE_RANK + 9;
1126 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1127 rank = YUV_BASE_RANK + 5;
1129 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1130 rank = YUV_BASE_RANK + 4;
1132 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1133 rank = YUV_BASE_RANK + 8;
1136 case V4L2_PIX_FMT_DV:
1137 rank = DV_BASE_RANK;
1140 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1144 #ifdef V4L2_PIX_FMT_SBGGR8
1145 case V4L2_PIX_FMT_SBGGR8:
1146 rank = BAYER_BASE_RANK;
1150 #ifdef V4L2_PIX_FMT_SN9C10X
1151 case V4L2_PIX_FMT_SN9C10X:
1152 rank = S910_BASE_RANK;
1156 #ifdef V4L2_PIX_FMT_PWC1
1157 case V4L2_PIX_FMT_PWC1:
1158 rank = PWC_BASE_RANK;
1161 #ifdef V4L2_PIX_FMT_PWC2
1162 case V4L2_PIX_FMT_PWC2:
1163 rank = PWC_BASE_RANK;
1172 /* All ranks are below 1<<15 so a shift by 15
1173 * will a) make all non-emulated formats larger
1174 * than emulated and b) will not overflow
1185 format_cmp_func (gconstpointer a, gconstpointer b)
1187 const struct v4l2_fmtdesc *fa = a;
1188 const struct v4l2_fmtdesc *fb = b;
1190 if (fa->pixelformat == fb->pixelformat)
1193 return gst_v4l2_object_format_get_rank (fb) -
1194 gst_v4l2_object_format_get_rank (fa);
1197 /******************************************************
1198 * gst_v4l2_object_fill_format_list():
1199 * create list of supported capture formats
1200 * return value: TRUE on success, FALSE on error
1201 ******************************************************/
1203 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object)
1206 struct v4l2_fmtdesc *format;
1208 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1210 /* format enumeration */
1212 format = g_new0 (struct v4l2_fmtdesc, 1);
1215 format->type = v4l2object->type;
1217 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1218 if (errno == EINVAL) {
1220 break; /* end of enumeration */
1226 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1227 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1228 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1229 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1230 format->description);
1231 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1232 GST_FOURCC_ARGS (format->pixelformat));
1234 /* sort formats according to our preference; we do this, because caps
1235 * are probed in the order the formats are in the list, and the order of
1236 * formats in the final probed caps matters for things like fixation */
1237 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1238 (GCompareFunc) format_cmp_func);
1241 #ifndef GST_DISABLE_GST_DEBUG
1245 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1246 for (l = v4l2object->formats; l != NULL; l = l->next) {
1249 GST_INFO_OBJECT (v4l2object->element,
1250 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1251 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1261 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1262 (_("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)));
1269 * Get the list of supported capture formats, a list of
1270 * <code>struct v4l2_fmtdesc</code>.
1273 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1275 if (!v4l2object->formats)
1276 gst_v4l2_object_fill_format_list (v4l2object);
1277 return v4l2object->formats;
1282 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1284 GstStructure *structure = NULL;
1287 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1288 #ifdef V4L2_PIX_FMT_PJPG
1289 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1291 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1292 structure = gst_structure_new_empty ("image/jpeg");
1294 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1295 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1296 /* FIXME: get correct fourccs here */
1298 #ifdef V4L2_PIX_FMT_MPEG4
1299 case V4L2_PIX_FMT_MPEG4:
1300 structure = gst_structure_new ("video/mpeg",
1301 "mpegversion", G_TYPE_INT, 4, "systemstream",
1302 G_TYPE_BOOLEAN, FALSE, NULL);
1305 #ifdef V4L2_PIX_FMT_H263
1306 case V4L2_PIX_FMT_H263:
1307 structure = gst_structure_new ("video/x-h263",
1308 "variant", G_TYPE_STRING, "itu", NULL);
1311 #ifdef V4L2_PIX_FMT_H264
1312 case V4L2_PIX_FMT_H264: /* H.264 */
1313 structure = gst_structure_new_empty ("video/x-h264");
1316 case V4L2_PIX_FMT_RGB332:
1317 case V4L2_PIX_FMT_RGB555X:
1318 case V4L2_PIX_FMT_RGB565X:
1319 /* FIXME: get correct fourccs here */
1321 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1322 case V4L2_PIX_FMT_RGB555:
1323 case V4L2_PIX_FMT_RGB565:
1324 case V4L2_PIX_FMT_RGB24:
1325 case V4L2_PIX_FMT_BGR24:
1326 case V4L2_PIX_FMT_RGB32:
1327 case V4L2_PIX_FMT_BGR32:
1328 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1329 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1330 case V4L2_PIX_FMT_YVU410:
1331 case V4L2_PIX_FMT_YUV410:
1332 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1333 case V4L2_PIX_FMT_YUYV:
1334 case V4L2_PIX_FMT_YVU420:
1335 case V4L2_PIX_FMT_UYVY:
1337 case V4L2_PIX_FMT_Y41P:
1339 case V4L2_PIX_FMT_YUV422P:
1340 #ifdef V4L2_PIX_FMT_YVYU
1341 case V4L2_PIX_FMT_YVYU:
1343 case V4L2_PIX_FMT_YUV411P:{
1344 GstVideoFormat format;
1347 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1348 format = GST_VIDEO_FORMAT_GRAY8;
1350 case V4L2_PIX_FMT_RGB555:
1351 format = GST_VIDEO_FORMAT_RGB15;
1353 case V4L2_PIX_FMT_RGB565:
1354 format = GST_VIDEO_FORMAT_RGB16;
1356 case V4L2_PIX_FMT_RGB24:
1357 format = GST_VIDEO_FORMAT_RGB;
1359 case V4L2_PIX_FMT_BGR24:
1360 format = GST_VIDEO_FORMAT_BGR;
1362 case V4L2_PIX_FMT_RGB32:
1363 format = GST_VIDEO_FORMAT_RGBx;
1365 case V4L2_PIX_FMT_BGR32:
1366 format = GST_VIDEO_FORMAT_BGRx;
1368 case V4L2_PIX_FMT_NV12:
1369 format = GST_VIDEO_FORMAT_NV12;
1371 case V4L2_PIX_FMT_NV21:
1372 format = GST_VIDEO_FORMAT_NV21;
1374 case V4L2_PIX_FMT_YVU410:
1375 format = GST_VIDEO_FORMAT_YVU9;
1377 case V4L2_PIX_FMT_YUV410:
1378 format = GST_VIDEO_FORMAT_YUV9;
1380 case V4L2_PIX_FMT_YUV420:
1381 format = GST_VIDEO_FORMAT_I420;
1383 case V4L2_PIX_FMT_YUYV:
1384 format = GST_VIDEO_FORMAT_YUY2;
1386 case V4L2_PIX_FMT_YVU420:
1387 format = GST_VIDEO_FORMAT_YV12;
1389 case V4L2_PIX_FMT_UYVY:
1390 format = GST_VIDEO_FORMAT_UYVY;
1393 case V4L2_PIX_FMT_Y41P:
1394 format = GST_VIDEO_FORMAT_Y41P;
1397 case V4L2_PIX_FMT_YUV411P:
1398 format = GST_VIDEO_FORMAT_Y41B;
1400 case V4L2_PIX_FMT_YUV422P:
1401 format = GST_VIDEO_FORMAT_Y42B;
1403 #ifdef V4L2_PIX_FMT_YVYU
1404 case V4L2_PIX_FMT_YVYU:
1405 format = GST_VIDEO_FORMAT_YVYU;
1409 format = GST_VIDEO_FORMAT_UNKNOWN;
1410 g_assert_not_reached ();
1413 structure = gst_structure_new ("video/x-raw",
1414 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1417 case V4L2_PIX_FMT_DV:
1419 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1422 case V4L2_PIX_FMT_MPEG: /* MPEG */
1423 structure = gst_structure_new_empty ("video/mpegts");
1425 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1427 #ifdef V4L2_PIX_FMT_SBGGR8
1428 case V4L2_PIX_FMT_SBGGR8:
1429 structure = gst_structure_new_empty ("video/x-bayer");
1432 #ifdef V4L2_PIX_FMT_SN9C10X
1433 case V4L2_PIX_FMT_SN9C10X:
1434 structure = gst_structure_new_empty ("video/x-sonix");
1437 #ifdef V4L2_PIX_FMT_PWC1
1438 case V4L2_PIX_FMT_PWC1:
1439 structure = gst_structure_new_empty ("video/x-pwc1");
1442 #ifdef V4L2_PIX_FMT_PWC2
1443 case V4L2_PIX_FMT_PWC2:
1444 structure = gst_structure_new_empty ("video/x-pwc2");
1448 GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
1449 fourcc, GST_FOURCC_ARGS (fourcc));
1459 gst_v4l2_object_get_all_caps (void)
1461 static GstCaps *caps = NULL;
1464 GstStructure *structure;
1468 caps = gst_caps_new_empty ();
1469 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1471 gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
1473 if (gst_v4l2_formats[i].dimensions) {
1474 gst_structure_set (structure,
1475 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1476 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1477 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
1479 gst_caps_append_structure (caps, structure);
1484 return gst_caps_ref (caps);
1488 /* collect data for the given caps
1489 * @caps: given input caps
1490 * @format: location for the v4l format
1491 * @w/@h: location for width and height
1492 * @fps_n/@fps_d: location for framerate
1493 * @size: location for expected size of the frame or 0 if unknown
1496 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1497 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1499 GstStructure *structure;
1501 const gchar *mimetype;
1502 struct v4l2_fmtdesc *fmt;
1504 /* default unknown values */
1507 structure = gst_caps_get_structure (caps, 0);
1509 mimetype = gst_structure_get_name (structure);
1511 if (!gst_video_info_from_caps (info, caps))
1512 goto invalid_format;
1514 if (g_str_equal (mimetype, "video/x-raw")) {
1515 switch (GST_VIDEO_INFO_FORMAT (info)) {
1516 case GST_VIDEO_FORMAT_I420:
1517 fourcc = V4L2_PIX_FMT_YUV420;
1519 case GST_VIDEO_FORMAT_YUY2:
1520 fourcc = V4L2_PIX_FMT_YUYV;
1523 case GST_VIDEO_FORMAT_Y41P:
1524 fourcc = V4L2_PIX_FMT_Y41P;
1527 case GST_VIDEO_FORMAT_UYVY:
1528 fourcc = V4L2_PIX_FMT_UYVY;
1530 case GST_VIDEO_FORMAT_YV12:
1531 fourcc = V4L2_PIX_FMT_YVU420;
1533 case GST_VIDEO_FORMAT_Y41B:
1534 fourcc = V4L2_PIX_FMT_YUV411P;
1536 case GST_VIDEO_FORMAT_Y42B:
1537 fourcc = V4L2_PIX_FMT_YUV422P;
1539 case GST_VIDEO_FORMAT_NV12:
1540 fourcc = V4L2_PIX_FMT_NV12;
1542 case GST_VIDEO_FORMAT_NV21:
1543 fourcc = V4L2_PIX_FMT_NV21;
1545 #ifdef V4L2_PIX_FMT_YVYU
1546 case GST_VIDEO_FORMAT_YVYU:
1547 fourcc = V4L2_PIX_FMT_YVYU;
1550 case GST_VIDEO_FORMAT_RGB15:
1551 fourcc = V4L2_PIX_FMT_RGB555;
1553 case GST_VIDEO_FORMAT_RGB16:
1554 fourcc = V4L2_PIX_FMT_RGB565;
1556 case GST_VIDEO_FORMAT_RGB:
1557 fourcc = V4L2_PIX_FMT_RGB24;
1559 case GST_VIDEO_FORMAT_BGR:
1560 fourcc = V4L2_PIX_FMT_BGR24;
1562 case GST_VIDEO_FORMAT_RGBx:
1563 case GST_VIDEO_FORMAT_RGBA:
1564 fourcc = V4L2_PIX_FMT_RGB32;
1566 case GST_VIDEO_FORMAT_BGRx:
1567 case GST_VIDEO_FORMAT_BGRA:
1568 fourcc = V4L2_PIX_FMT_BGR32;
1570 case GST_VIDEO_FORMAT_GRAY8:
1571 fourcc = V4L2_PIX_FMT_GREY;
1576 if (g_str_equal (mimetype, "video/mpegts")) {
1577 fourcc = V4L2_PIX_FMT_MPEG;
1578 } else if (g_str_equal (mimetype, "video/x-dv")) {
1579 fourcc = V4L2_PIX_FMT_DV;
1580 } else if (g_str_equal (mimetype, "image/jpeg")) {
1581 fourcc = V4L2_PIX_FMT_JPEG;
1582 #ifdef V4L2_PIX_FMT_MPEG4
1583 } else if (g_str_equal (mimetype, "video/mpeg")) {
1584 fourcc = V4L2_PIX_FMT_MPEG4;
1586 #ifdef V4L2_PIX_FMT_H263
1587 } else if (g_str_equal (mimetype, "video/x-h263")) {
1588 fourcc = V4L2_PIX_FMT_H263;
1590 #ifdef V4L2_PIX_FMT_H264
1591 } else if (g_str_equal (mimetype, "video/x-h264")) {
1592 fourcc = V4L2_PIX_FMT_H264;
1594 #ifdef V4L2_PIX_FMT_SBGGR8
1595 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1596 fourcc = V4L2_PIX_FMT_SBGGR8;
1598 #ifdef V4L2_PIX_FMT_SN9C10X
1599 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1600 fourcc = V4L2_PIX_FMT_SN9C10X;
1602 #ifdef V4L2_PIX_FMT_PWC1
1603 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1604 fourcc = V4L2_PIX_FMT_PWC1;
1606 #ifdef V4L2_PIX_FMT_PWC2
1607 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1608 fourcc = V4L2_PIX_FMT_PWC2;
1614 goto unhandled_format;
1616 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1618 goto unsupported_format;
1627 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1632 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1637 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1644 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1645 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced);
1648 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1650 struct v4l2_cropcap cropcap;
1651 int num = 1, den = 1;
1653 if (!v4l2object->keep_aspect)
1656 if (v4l2object->par) {
1657 num = gst_value_get_fraction_numerator (v4l2object->par);
1658 den = gst_value_get_fraction_denominator (v4l2object->par);
1662 memset (&cropcap, 0, sizeof (cropcap));
1664 cropcap.type = v4l2object->type;
1665 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
1666 goto cropcap_failed;
1668 num = cropcap.pixelaspect.numerator;
1669 den = cropcap.pixelaspect.denominator;
1672 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, num, den,
1677 if (errno != ENOTTY)
1678 GST_WARNING_OBJECT (v4l2object->element,
1679 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
1680 g_strerror (errno));
1685 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1686 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1687 static GstStructure *
1688 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1689 guint32 pixelformat,
1690 guint32 width, guint32 height, const GstStructure * template)
1692 gint fd = v4l2object->video_fd;
1693 struct v4l2_frmivalenum ival;
1696 GValue rates = { 0, };
1697 gboolean interlaced;
1698 gint int_width = width;
1699 gint int_height = height;
1701 if (!strcmp ((char *) v4l2object->vcap.driver, "uvcvideo")) {
1703 * UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
1704 * causes expensive and slow USB IO, so don't probe them for interlaced
1708 /* Interlaced detection using VIDIOC_TRY/S_FMT */
1709 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat,
1710 &int_width, &int_height, &interlaced))
1714 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1716 ival.pixel_format = pixelformat;
1718 ival.height = height;
1720 GST_LOG_OBJECT (v4l2object->element,
1721 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1722 GST_FOURCC_ARGS (pixelformat));
1724 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1725 * fraction to get framerate */
1726 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1727 goto enum_frameintervals_failed;
1729 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1730 GValue rate = { 0, };
1732 g_value_init (&rates, GST_TYPE_LIST);
1733 g_value_init (&rate, GST_TYPE_FRACTION);
1736 num = ival.discrete.numerator;
1737 denom = ival.discrete.denominator;
1739 if (num > G_MAXINT || denom > G_MAXINT) {
1740 /* let us hope we don't get here... */
1745 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1748 /* swap to get the framerate */
1749 gst_value_set_fraction (&rate, denom, num);
1750 gst_value_list_append_value (&rates, &rate);
1753 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1754 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1755 GValue min = { 0, };
1756 GValue step = { 0, };
1757 GValue max = { 0, };
1758 gboolean added = FALSE;
1759 guint32 minnum, mindenom;
1760 guint32 maxnum, maxdenom;
1762 g_value_init (&rates, GST_TYPE_LIST);
1764 g_value_init (&min, GST_TYPE_FRACTION);
1765 g_value_init (&step, GST_TYPE_FRACTION);
1766 g_value_init (&max, GST_TYPE_FRACTION);
1769 minnum = ival.stepwise.min.numerator;
1770 mindenom = ival.stepwise.min.denominator;
1771 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1775 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1777 gst_value_set_fraction (&min, minnum, mindenom);
1780 maxnum = ival.stepwise.max.numerator;
1781 maxdenom = ival.stepwise.max.denominator;
1782 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1787 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1789 gst_value_set_fraction (&max, maxnum, maxdenom);
1792 num = ival.stepwise.step.numerator;
1793 denom = ival.stepwise.step.denominator;
1794 if (num > G_MAXINT || denom > G_MAXINT) {
1799 if (num == 0 || denom == 0) {
1800 /* in this case we have a wrong fraction or no step, set the step to max
1801 * so that we only add the min value in the loop below */
1806 /* since we only have gst_value_fraction_subtract and not add, negate the
1808 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1810 gst_value_set_fraction (&step, -num, denom);
1812 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
1813 GValue rate = { 0, };
1815 num = gst_value_get_fraction_numerator (&min);
1816 denom = gst_value_get_fraction_denominator (&min);
1817 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1820 /* invert to get the framerate */
1821 g_value_init (&rate, GST_TYPE_FRACTION);
1822 gst_value_set_fraction (&rate, denom, num);
1823 gst_value_list_append_value (&rates, &rate);
1826 /* we're actually adding because step was negated above. This is because
1827 * there is no _add function... */
1828 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1829 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1834 /* no range was added, leave the default range from the template */
1835 GST_WARNING_OBJECT (v4l2object->element,
1836 "no range added, leaving default");
1837 g_value_unset (&rates);
1839 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1840 guint32 maxnum, maxdenom;
1842 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1844 num = ival.stepwise.min.numerator;
1845 denom = ival.stepwise.min.denominator;
1846 if (num > G_MAXINT || denom > G_MAXINT) {
1851 maxnum = ival.stepwise.max.numerator;
1852 maxdenom = ival.stepwise.max.denominator;
1853 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1858 GST_LOG_OBJECT (v4l2object->element,
1859 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
1862 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1868 s = gst_structure_copy (template);
1869 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
1870 "height", G_TYPE_INT, (gint) height, NULL);
1871 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
1872 if (g_str_equal (gst_structure_get_name (s), "video/x-raw"))
1873 gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
1874 (interlaced ? "mixed" : "progressive"), NULL);
1876 if (G_IS_VALUE (&rates)) {
1877 /* only change the framerate on the template when we have a valid probed new
1879 gst_structure_set_value (s, "framerate", &rates);
1880 g_value_unset (&rates);
1882 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
1888 enum_frameintervals_failed:
1890 GST_DEBUG_OBJECT (v4l2object->element,
1891 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
1892 GST_FOURCC_ARGS (pixelformat), width, height);
1897 /* I don't see how this is actually an error, we ignore the format then */
1898 GST_WARNING_OBJECT (v4l2object->element,
1899 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
1900 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
1904 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
1906 #ifdef VIDIOC_ENUM_FRAMESIZES
1908 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
1912 gst_structure_get_int (s1, "width", &w1);
1913 gst_structure_get_int (s1, "height", &h1);
1914 gst_structure_get_int (s2, "width", &w2);
1915 gst_structure_get_int (s2, "height", &h2);
1917 /* I think it's safe to assume that this won't overflow for a while */
1918 return ((w2 * h2) - (w1 * h1));
1923 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
1924 guint32 pixelformat, const GstStructure * template)
1926 GstCaps *ret = gst_caps_new_empty ();
1929 #ifdef VIDIOC_ENUM_FRAMESIZES
1930 gint fd = v4l2object->video_fd;
1931 struct v4l2_frmsizeenum size;
1932 GList *results = NULL;
1935 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1936 return gst_caps_new_empty_simple ("video/mpegts");
1938 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
1940 size.pixel_format = pixelformat;
1942 GST_DEBUG_OBJECT (v4l2object->element,
1943 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
1944 GST_FOURCC_ARGS (pixelformat));
1946 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
1947 goto enum_framesizes_failed;
1949 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1951 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
1952 size.discrete.width, size.discrete.height);
1954 w = MIN (size.discrete.width, G_MAXINT);
1955 h = MIN (size.discrete.height, G_MAXINT);
1959 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1960 pixelformat, w, h, template);
1963 results = g_list_prepend (results, tmp);
1967 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
1968 GST_DEBUG_OBJECT (v4l2object->element,
1969 "done iterating discrete frame sizes");
1970 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
1971 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
1972 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1973 size.stepwise.min_width);
1974 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1975 size.stepwise.min_height);
1976 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1977 size.stepwise.max_width);
1978 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1979 size.stepwise.max_height);
1980 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
1981 size.stepwise.step_width);
1982 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
1983 size.stepwise.step_height);
1985 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
1986 w <= size.stepwise.max_width && h <= size.stepwise.max_height;
1987 w += size.stepwise.step_width, h += size.stepwise.step_height) {
1988 if (w == 0 || h == 0)
1992 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1993 pixelformat, w, h, template);
1996 results = g_list_prepend (results, tmp);
1998 GST_DEBUG_OBJECT (v4l2object->element,
1999 "done iterating stepwise frame sizes");
2000 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2003 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
2004 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2005 size.stepwise.min_width);
2006 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2007 size.stepwise.min_height);
2008 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2009 size.stepwise.max_width);
2010 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2011 size.stepwise.max_height);
2013 w = MAX (size.stepwise.min_width, 1);
2014 h = MAX (size.stepwise.min_height, 1);
2015 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2016 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2019 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2022 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2023 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2026 /* no point using the results list here, since there's only one struct */
2027 gst_caps_append_structure (ret, tmp);
2033 /* we use an intermediary list to store and then sort the results of the
2034 * probing because we can't make any assumptions about the order in which
2035 * the driver will give us the sizes, but we want the final caps to contain
2036 * the results starting with the highest resolution and having the lowest
2037 * resolution last, since order in caps matters for things like fixation. */
2038 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2039 while (results != NULL) {
2040 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
2041 results = g_list_delete_link (results, results);
2044 if (gst_caps_is_empty (ret))
2045 goto enum_framesizes_no_results;
2050 enum_framesizes_failed:
2052 /* I don't see how this is actually an error */
2053 GST_DEBUG_OBJECT (v4l2object->element,
2054 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2055 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2056 goto default_frame_sizes;
2058 enum_framesizes_no_results:
2060 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2061 * question doesn't actually support it yet */
2062 GST_DEBUG_OBJECT (v4l2object->element,
2063 "No results for pixelformat %" GST_FOURCC_FORMAT
2064 " enumerating frame sizes, trying fallback",
2065 GST_FOURCC_ARGS (pixelformat));
2066 goto default_frame_sizes;
2070 GST_WARNING_OBJECT (v4l2object->element,
2071 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2072 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2073 goto default_frame_sizes;
2075 default_frame_sizes:
2076 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
2078 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2079 gboolean interlaced;
2081 /* This code is for Linux < 2.6.19 */
2083 max_w = max_h = GST_V4L2_MAX_SIZE;
2084 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2085 &min_h, &interlaced)) {
2086 GST_WARNING_OBJECT (v4l2object->element,
2087 "Could not probe minimum capture size for pixelformat %"
2088 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2090 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2091 &max_h, &interlaced)) {
2092 GST_WARNING_OBJECT (v4l2object->element,
2093 "Could not probe maximum capture size for pixelformat %"
2094 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2097 /* Since we can't get framerate directly, try to use the current norm */
2098 if (v4l2object->tv_norm && v4l2object->norms) {
2100 GstTunerNorm *norm = NULL;
2101 GstTunerNorm *current =
2102 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2104 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2105 norm = (GstTunerNorm *) norms->data;
2106 if (!strcmp (norm->label, current->label))
2109 /* If it's possible, set framerate to that (discrete) value */
2111 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2112 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2116 tmp = gst_structure_copy (template);
2118 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2121 /* if norm can't be used, copy the template framerate */
2122 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2127 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2129 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2132 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2134 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2136 if (g_str_equal (gst_structure_get_name (tmp), "video/x-raw"))
2137 gst_structure_set (tmp, "interlace-mode", G_TYPE_STRING,
2138 (interlaced ? "mixed" : "progressive"), NULL);
2139 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2141 gst_caps_append_structure (ret, tmp);
2148 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2149 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced)
2151 struct v4l2_format fmt, prevfmt;
2156 g_return_val_if_fail (width != NULL, FALSE);
2157 g_return_val_if_fail (height != NULL, FALSE);
2159 GST_LOG_OBJECT (v4l2object->element,
2160 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2161 *width, *height, GST_FOURCC_ARGS (pixelformat));
2163 fd = v4l2object->video_fd;
2165 /* Some drivers are buggy and will modify the currently set format
2166 when processing VIDIOC_TRY_FMT, so we remember what is set at the
2167 minute, and will reset it when done. */
2168 prevfmt.type = v4l2object->type;
2169 prevfmt_valid = (v4l2_ioctl (fd, VIDIOC_G_FMT, &prevfmt) >= 0);
2171 /* get size delimiters */
2172 memset (&fmt, 0, sizeof (fmt));
2173 fmt.type = v4l2object->type;
2174 fmt.fmt.pix.width = *width;
2175 fmt.fmt.pix.height = *height;
2176 fmt.fmt.pix.pixelformat = pixelformat;
2177 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2179 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2180 if (r < 0 && errno == EINVAL) {
2181 /* try again with interlaced video */
2182 fmt.fmt.pix.width = *width;
2183 fmt.fmt.pix.height = *height;
2184 fmt.fmt.pix.pixelformat = pixelformat;
2185 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2186 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2190 /* The driver might not implement TRY_FMT, in which case we will try
2192 if (errno != ENOTTY)
2195 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
2196 be, because we're still probing */
2197 if (GST_V4L2_IS_ACTIVE (v4l2object))
2200 GST_LOG_OBJECT (v4l2object->element,
2201 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
2203 fmt.fmt.pix.width = *width;
2204 fmt.fmt.pix.height = *height;
2206 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2207 if (r < 0 && errno == EINVAL) {
2208 /* try again with progressive video */
2209 fmt.fmt.pix.width = *width;
2210 fmt.fmt.pix.height = *height;
2211 fmt.fmt.pix.pixelformat = pixelformat;
2212 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2213 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2220 GST_LOG_OBJECT (v4l2object->element,
2221 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2223 *width = fmt.fmt.pix.width;
2224 *height = fmt.fmt.pix.height;
2226 switch (fmt.fmt.pix.field) {
2227 case V4L2_FIELD_ANY:
2228 case V4L2_FIELD_NONE:
2229 *interlaced = FALSE;
2231 case V4L2_FIELD_INTERLACED:
2232 case V4L2_FIELD_INTERLACED_TB:
2233 case V4L2_FIELD_INTERLACED_BT:
2237 GST_WARNING_OBJECT (v4l2object->element,
2238 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u",
2239 GST_FOURCC_ARGS (pixelformat), *width, *height);
2244 v4l2_ioctl (fd, VIDIOC_S_FMT, &prevfmt);
2249 v4l2_ioctl (fd, VIDIOC_S_FMT, &prevfmt);
2254 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2258 GST_DEBUG_OBJECT (v4l2object->element, "initializing the capture system");
2260 GST_V4L2_CHECK_OPEN (v4l2object);
2261 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2263 /* find transport */
2264 mode = v4l2object->req_mode;
2266 if (v4l2object->vcap.capabilities & V4L2_CAP_READWRITE) {
2267 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2268 mode = GST_V4L2_IO_RW;
2269 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2270 goto method_not_supported;
2272 if (v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
2273 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2274 mode = GST_V4L2_IO_MMAP;
2275 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2276 goto method_not_supported;
2278 /* if still no transport selected, error out */
2279 if (mode == GST_V4L2_IO_AUTO)
2280 goto no_supported_capture_method;
2282 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2283 v4l2object->mode = mode;
2285 /* Map the buffers */
2286 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2288 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2289 goto buffer_pool_new_failed;
2291 GST_V4L2_SET_ACTIVE (v4l2object);
2296 buffer_pool_new_failed:
2298 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2299 (_("Could not map buffers from device '%s'"),
2300 v4l2object->videodev),
2301 ("Failed to create buffer pool: %s", g_strerror (errno)));
2304 method_not_supported:
2306 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2307 (_("The driver of device '%s' does not support the IO method %d"),
2308 v4l2object->videodev, mode), (NULL));
2311 no_supported_capture_method:
2313 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2314 (_("The driver of device '%s' does not support any known IO "
2315 "method."), v4l2object->videodev), (NULL));
2321 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps)
2323 gint fd = v4l2object->video_fd;
2324 struct v4l2_format format;
2325 struct v4l2_streamparm streamparm;
2326 enum v4l2_field field;
2327 guint32 pixelformat;
2328 struct v4l2_fmtdesc *fmtdesc;
2330 gint width, height, fps_n, fps_d, stride;
2332 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
2335 pixelformat = fmtdesc->pixelformat;
2336 width = GST_VIDEO_INFO_WIDTH (&info);
2337 height = GST_VIDEO_INFO_HEIGHT (&info);
2338 fps_n = GST_VIDEO_INFO_FPS_N (&info);
2339 fps_d = GST_VIDEO_INFO_FPS_D (&info);
2340 stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
2342 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
2343 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
2344 /* ideally we would differentiate between types of interlaced video
2345 * but there is not sufficient information in the caps..
2347 field = V4L2_FIELD_INTERLACED;
2349 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
2350 field = V4L2_FIELD_NONE;
2353 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
2354 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
2355 GST_FOURCC_ARGS (pixelformat), stride);
2357 GST_V4L2_CHECK_OPEN (v4l2object);
2358 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2360 /* MPEG-TS source cameras don't get their format set for some reason.
2361 * It looks wrong and we weren't able to track down the reason for that code
2362 * so it is disabled until someone who has an mpeg-ts camera complains...
2365 /* Only unconditionally accept mpegts for sources */
2366 if ((v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
2367 (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')))
2371 memset (&format, 0x00, sizeof (struct v4l2_format));
2372 format.type = v4l2object->type;
2374 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
2375 goto get_fmt_failed;
2377 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2378 "%" GST_FOURCC_FORMAT " bytesperline %d, colorspace %d",
2379 format.fmt.pix.width, format.fmt.pix.height,
2380 GST_FOURCC_ARGS (format.fmt.pix.pixelformat), format.fmt.pix.bytesperline,
2381 format.fmt.pix.colorspace);
2383 if (format.type != v4l2object->type ||
2384 format.fmt.pix.width != width ||
2385 format.fmt.pix.height != height ||
2386 format.fmt.pix.pixelformat != pixelformat ||
2387 format.fmt.pix.field != field || format.fmt.pix.bytesperline != stride) {
2388 /* something different, set the format */
2389 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2390 "%" GST_FOURCC_FORMAT " bytesperline %d", width, height,
2391 GST_FOURCC_ARGS (pixelformat), stride);
2393 format.type = v4l2object->type;
2394 format.fmt.pix.width = width;
2395 format.fmt.pix.height = height;
2396 format.fmt.pix.pixelformat = pixelformat;
2397 format.fmt.pix.field = field;
2398 /* try to ask our prefered stride */
2399 format.fmt.pix.bytesperline = stride;
2401 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
2402 goto set_fmt_failed;
2404 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2405 "%" GST_FOURCC_FORMAT " stride %d", format.fmt.pix.width,
2406 format.fmt.pix.height, GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2407 format.fmt.pix.bytesperline);
2409 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
2410 goto invalid_dimensions;
2412 if (format.fmt.pix.pixelformat != pixelformat)
2413 goto invalid_pixelformat;
2416 /* figure out the frame layout */
2417 v4l2object->bytesperline = format.fmt.pix.bytesperline;
2418 v4l2object->sizeimage = format.fmt.pix.sizeimage;
2420 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %u",
2421 v4l2object->sizeimage);
2423 /* Is there a reason we require the caller to always specify a framerate? */
2424 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
2427 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
2428 streamparm.type = v4l2object->type;
2430 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
2431 goto get_parm_failed;
2433 GST_VIDEO_INFO_FPS_N (&info) =
2434 streamparm.parm.capture.timeperframe.denominator;
2435 GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
2437 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2438 GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
2439 streamparm.parm.capture.timeperframe.denominator,
2440 streamparm.parm.capture.timeperframe.numerator);
2442 /* We used to skip frame rate setup if the camera was already setup
2443 * with the requested frame rate. This breaks some cameras though,
2444 * causing them to not output data (several models of Thinkpad cameras
2445 * have this problem at least).
2446 * So, don't skip. */
2447 GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
2449 /* We want to change the frame rate, so check whether we can. Some cheap USB
2450 * cameras don't have the capability */
2451 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
2452 GST_DEBUG_OBJECT (v4l2object->element,
2453 "Not setting framerate (not supported)");
2457 /* Note: V4L2 wants the frame interval, we have the frame rate */
2458 streamparm.parm.capture.timeperframe.numerator = fps_d;
2459 streamparm.parm.capture.timeperframe.denominator = fps_n;
2461 /* some cheap USB cam's won't accept any change */
2462 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
2463 goto set_parm_failed;
2465 /* get new values */
2466 fps_d = streamparm.parm.capture.timeperframe.numerator;
2467 fps_n = streamparm.parm.capture.timeperframe.denominator;
2469 GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
2472 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
2473 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
2477 /* if we have a framerate pre-calculate duration */
2478 if (fps_n > 0 && fps_d > 0) {
2479 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
2481 v4l2object->duration = GST_CLOCK_TIME_NONE;
2483 v4l2object->info = info;
2484 v4l2object->fmtdesc = fmtdesc;
2486 /* now configure ther pools */
2487 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
2495 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
2501 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2502 (_("Device '%s' does not support video capture"),
2503 v4l2object->videodev),
2504 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
2509 if (errno == EBUSY) {
2510 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, BUSY,
2511 (_("Device '%s' is busy"), v4l2object->videodev),
2512 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2513 GST_FOURCC_ARGS (pixelformat), width, height,
2514 g_strerror (errno)));
2516 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2517 (_("Device '%s' cannot capture at %dx%d"),
2518 v4l2object->videodev, width, height),
2519 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2520 GST_FOURCC_ARGS (pixelformat), width, height,
2521 g_strerror (errno)));
2527 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2528 (_("Device '%s' cannot capture at %dx%d"),
2529 v4l2object->videodev, width, height),
2530 ("Tried to capture at %dx%d, but device returned size %dx%d",
2531 width, height, format.fmt.pix.width, format.fmt.pix.height));
2534 invalid_pixelformat:
2536 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2537 (_("Device '%s' cannot capture in the specified format"),
2538 v4l2object->videodev),
2539 ("Tried to capture in %" GST_FOURCC_FORMAT
2540 ", but device returned format" " %" GST_FOURCC_FORMAT,
2541 GST_FOURCC_ARGS (pixelformat),
2542 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
2547 /* it's possible that this call is not supported */
2548 if (errno != EINVAL && errno != ENOTTY) {
2549 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2550 (_("Could not get parameters on device '%s'"),
2551 v4l2object->videodev), GST_ERROR_SYSTEM);
2557 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2558 (_("Video device did not accept new frame rate setting.")),
2564 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2565 (_("Video device could not create buffer pool.")), GST_ERROR_SYSTEM);
2571 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
2576 if (!v4l2object->pool)
2579 s = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (v4l2object->pool));
2580 gst_buffer_pool_config_get_params (s, &oldcaps, NULL, NULL, NULL);
2582 return oldcaps && gst_caps_is_equal (caps, oldcaps);
2586 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
2588 GST_LOG_OBJECT (v4l2object->element, "flush poll");
2589 gst_poll_set_flushing (v4l2object->poll, TRUE);
2595 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
2597 GST_LOG_OBJECT (v4l2object->element, "flush stop poll");
2598 gst_poll_set_flushing (v4l2object->poll, FALSE);
2604 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
2606 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
2608 if (!GST_V4L2_IS_OPEN (v4l2object))
2610 if (!GST_V4L2_IS_ACTIVE (v4l2object))
2613 if (v4l2object->pool) {
2614 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
2615 gst_buffer_pool_set_active (GST_BUFFER_POOL_CAST (v4l2object->pool), FALSE);
2616 gst_object_unref (v4l2object->pool);
2617 v4l2object->pool = NULL;
2620 GST_V4L2_SET_INACTIVE (v4l2object);
2627 gst_v4l2_object_copy (GstV4l2Object * v4l2object, GstBuffer * dest,
2630 const GstVideoFormatInfo *finfo = v4l2object->info.finfo;
2632 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
2633 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
2634 GstVideoFrame src_frame, dest_frame;
2636 GST_DEBUG_OBJECT (v4l2object->element, "copy video frame");
2638 /* we have raw video, use videoframe copy to get strides right */
2639 if (!gst_video_frame_map (&src_frame, &v4l2object->info, src, GST_MAP_READ))
2640 goto invalid_buffer;
2642 if (!gst_video_frame_map (&dest_frame, &v4l2object->info, dest,
2644 gst_video_frame_unmap (&src_frame);
2645 goto invalid_buffer;
2648 gst_video_frame_copy (&dest_frame, &src_frame);
2650 gst_video_frame_unmap (&src_frame);
2651 gst_video_frame_unmap (&dest_frame);
2655 GST_DEBUG_OBJECT (v4l2object->element, "copy raw bytes");
2656 gst_buffer_map (src, &map, GST_MAP_READ);
2657 gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
2658 gst_buffer_unmap (src, &map);
2659 gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
2661 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, v4l2object->element,
2662 "slow copy into buffer %p", dest);
2669 /* No Window available to put our image into */
2670 GST_WARNING_OBJECT (v4l2object->element, "could not map image");
2676 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
2682 if (v4l2object->probed_caps == NULL) {
2683 formats = gst_v4l2_object_get_format_list (v4l2object);
2685 ret = gst_caps_new_empty ();
2687 for (walk = formats; walk; walk = walk->next) {
2688 struct v4l2_fmtdesc *format;
2689 GstStructure *template;
2691 format = (struct v4l2_fmtdesc *) walk->data;
2693 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
2698 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
2699 format->pixelformat, template);
2701 gst_caps_append (ret, tmp);
2703 gst_structure_free (template);
2705 GST_DEBUG_OBJECT (v4l2object->element, "unknown format %u",
2706 format->pixelformat);
2709 v4l2object->probed_caps = ret;
2713 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
2714 GST_CAPS_INTERSECT_FIRST);
2716 ret = gst_caps_ref (v4l2object->probed_caps);
2719 GST_INFO_OBJECT (v4l2object->element, "probed caps: %" GST_PTR_FORMAT, ret);
2720 LOG_CAPS (v4l2object->element, ret);