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,
77 G_LOCK_DEFINE_STATIC (probe_lock);
80 gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
82 GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
83 static GList *list = NULL;
88 list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
91 G_UNLOCK (probe_lock);
96 static gboolean init = FALSE;
97 static GList *devices = NULL;
101 gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
102 GList ** klass_devices)
104 GUdevClient *client = NULL;
109 gchar *device = devices->data;
110 devices = g_list_remove (devices, device);
114 GST_INFO ("Enumerating video4linux devices from udev");
115 client = g_udev_client_new (NULL);
117 GST_WARNING ("Failed to initialize gudev client");
121 item = g_udev_client_query_by_subsystem (client, "video4linux");
123 GUdevDevice *device = item->data;
124 gchar *devnode = g_strdup (g_udev_device_get_device_file (device));
125 gint api = g_udev_device_get_property_as_int (device, "ID_V4L_VERSION");
126 GST_INFO ("Found new device: %s, API: %d", devnode, api);
127 /* Append v4l2 devices only. If api is 0 probably v4l_id has
128 been stripped out of the current udev installation, append
132 ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
134 if ((api == 2 || api == 0)) {
135 devices = g_list_append (devices, devnode);
139 g_object_unref (device);
148 g_object_unref (client);
151 *klass_devices = devices;
155 #endif /* HAVE_GUDEV */
158 gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
159 GList ** klass_devices)
162 const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
166 gchar *device = devices->data;
167 devices = g_list_remove (devices, device);
172 * detect /dev entries
174 for (n = 0; n < 64; n++) {
175 for (base = 0; dev_base[base] != NULL; base++) {
177 gchar *device = g_strdup_printf ("%s%d",
182 * does the /dev/ entry exist at all?
184 if (stat (device, &s) == 0) {
186 * yes: is a device attached?
188 if (S_ISCHR (s.st_mode)) {
190 if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
194 devices = g_list_append (devices, device);
205 *klass_devices = devices;
211 gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
212 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
214 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
219 if (!gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices))
220 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
221 #else /* !HAVE_GUDEV */
222 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
223 #endif /* HAVE_GUDEV */
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
232 gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
233 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
235 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
236 gboolean ret = FALSE;
242 !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
243 #else /* !HAVE_GUDEV */
244 ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
245 #endif /* HAVE_GUDEV */
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
255 gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
258 GValue value = { 0 };
264 array = g_value_array_new (g_list_length (*klass_devices));
265 item = *klass_devices;
266 g_value_init (&value, G_TYPE_STRING);
268 gchar *device = item->data;
270 g_value_set_string (&value, device);
271 g_value_array_append (array, &value);
275 g_value_unset (&value);
281 gst_v4l2_probe_get_values (GstPropertyProbe * probe,
282 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
284 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
285 GValueArray *array = NULL;
289 array = gst_v4l2_class_list_devices (klass, klass_devices);
292 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
300 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
302 gst_v4l2_device_get_type (void)
304 static GType v4l2_device_type = 0;
306 if (v4l2_device_type == 0) {
307 static const GFlagsValue values[] = {
308 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
309 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
310 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
312 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
313 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
315 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
316 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
322 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
325 return v4l2_device_type;
328 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
330 gst_v4l2_tv_norm_get_type (void)
332 static GType v4l2_tv_norm = 0;
335 static const GEnumValue tv_norms[] = {
338 {V4L2_STD_NTSC, "NTSC", "NTSC"},
339 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
340 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
341 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
342 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
344 {V4L2_STD_PAL, "PAL", "PAL"},
345 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
346 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
347 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
348 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
349 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
350 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
351 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
352 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
353 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
354 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
355 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
356 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
357 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
358 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
360 {V4L2_STD_SECAM, "SECAM", "SECAM"},
361 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
362 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
363 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
364 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
365 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
366 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
367 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
368 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
369 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
374 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
380 #define GST_TYPE_V4L2_IO_MODE (gst_v4l2_io_mode_get_type ())
382 gst_v4l2_io_mode_get_type (void)
384 static GType v4l2_io_mode = 0;
387 static const GEnumValue io_modes[] = {
388 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
389 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
390 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
391 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
392 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
396 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
402 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
403 const char *default_device)
405 g_object_class_install_property (gobject_class, PROP_DEVICE,
406 g_param_spec_string ("device", "Device", "Device location",
407 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
409 g_param_spec_string ("device-name", "Device name",
410 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
411 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
412 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
413 g_param_spec_int ("device-fd", "File descriptor",
414 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
415 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
416 g_object_class_install_property (gobject_class, PROP_FLAGS,
417 g_param_spec_flags ("flags", "Flags", "Device type flags",
418 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
419 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
422 * GstV4l2Src:brightness
424 * Picture brightness, or more precisely, the black level
428 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
429 g_param_spec_int ("brightness", "Brightness",
430 "Picture brightness, or more precisely, the black level", G_MININT,
432 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
434 * GstV4l2Src:contrast
436 * Picture contrast or luma gain
440 g_object_class_install_property (gobject_class, PROP_CONTRAST,
441 g_param_spec_int ("contrast", "Contrast",
442 "Picture contrast or luma gain", G_MININT,
444 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
446 * GstV4l2Src:saturation
448 * Picture color saturation or chroma gain
452 g_object_class_install_property (gobject_class, PROP_SATURATION,
453 g_param_spec_int ("saturation", "Saturation",
454 "Picture color saturation or chroma gain", G_MININT,
456 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
460 * Hue or color balance
464 g_object_class_install_property (gobject_class, PROP_HUE,
465 g_param_spec_int ("hue", "Hue",
466 "Hue or color balance", G_MININT,
468 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
477 g_object_class_install_property (gobject_class, PROP_TV_NORM,
478 g_param_spec_enum ("norm", "TV norm",
480 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
481 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
488 g_object_class_install_property (gobject_class, PROP_IO_MODE,
489 g_param_spec_enum ("io-mode", "IO mode",
491 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
492 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
496 gst_v4l2_object_new (GstElement * element,
497 enum v4l2_buf_type type,
498 const char *default_device,
499 GstV4l2GetInOutFunction get_in_out_func,
500 GstV4l2SetInOutFunction set_in_out_func,
501 GstV4l2UpdateFpsFunction update_fps_func)
503 GstV4l2Object *v4l2object;
506 * some default values
508 v4l2object = g_new0 (GstV4l2Object, 1);
510 v4l2object->type = type;
511 v4l2object->formats = NULL;
513 v4l2object->element = element;
514 v4l2object->get_in_out_func = get_in_out_func;
515 v4l2object->set_in_out_func = set_in_out_func;
516 v4l2object->update_fps_func = update_fps_func;
518 v4l2object->video_fd = -1;
519 v4l2object->poll = gst_poll_new (TRUE);
520 v4l2object->active = FALSE;
521 v4l2object->videodev = g_strdup (default_device);
523 v4l2object->norms = NULL;
524 v4l2object->channels = NULL;
525 v4l2object->colors = NULL;
527 v4l2object->xwindow_id = 0;
532 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
536 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
538 g_return_if_fail (v4l2object != NULL);
540 if (v4l2object->videodev)
541 g_free (v4l2object->videodev);
543 if (v4l2object->poll)
544 gst_poll_free (v4l2object->poll);
546 if (v4l2object->channel)
547 g_free (v4l2object->channel);
549 if (v4l2object->formats) {
550 gst_v4l2_object_clear_format_list (v4l2object);
558 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
560 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
561 g_slist_free (v4l2object->formats);
562 v4l2object->formats = NULL;
568 gst_v4l2_object_prop_to_cid (guint prop_id)
573 case PROP_BRIGHTNESS:
574 cid = V4L2_CID_BRIGHTNESS;
577 cid = V4L2_CID_CONTRAST;
579 case PROP_SATURATION:
580 cid = V4L2_CID_SATURATION;
586 GST_WARNING ("unmapped property id: %d", prop_id);
593 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
594 guint prop_id, const GValue * value, GParamSpec * pspec)
598 g_free (v4l2object->videodev);
599 v4l2object->videodev = g_value_dup_string (value);
601 case PROP_BRIGHTNESS:
603 case PROP_SATURATION:
606 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
609 if (GST_V4L2_IS_OPEN (v4l2object)) {
610 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
617 v4l2object->tv_norm = g_value_get_enum (value);
621 if (GST_V4L2_IS_OPEN (v4l2object)) {
622 GstTuner *tuner = GST_TUNER (v4l2object->element);
623 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
624 (gchar *) g_value_get_string (value));
627 /* like gst_tuner_set_channel (tuner, channel)
628 without g_object_notify */
629 gst_v4l2_tuner_set_channel (v4l2object, channel);
632 g_free (v4l2object->channel);
633 v4l2object->channel = g_value_dup_string (value);
637 if (GST_V4L2_IS_OPEN (v4l2object)) {
638 GstTuner *tuner = GST_TUNER (v4l2object->element);
639 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
642 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
644 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
645 without g_object_notify */
646 gst_v4l2_tuner_set_frequency (v4l2object, channel,
647 g_value_get_ulong (value));
650 v4l2object->frequency = g_value_get_ulong (value);
655 v4l2object->req_mode = g_value_get_enum (value);
666 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
667 guint prop_id, GValue * value, GParamSpec * pspec)
671 g_value_set_string (value, v4l2object->videodev);
673 case PROP_DEVICE_NAME:
675 const guchar *new = NULL;
677 if (GST_V4L2_IS_OPEN (v4l2object)) {
678 new = v4l2object->vcap.card;
679 } else if (gst_v4l2_open (v4l2object)) {
680 new = v4l2object->vcap.card;
681 gst_v4l2_close (v4l2object);
683 g_value_set_string (value, (gchar *) new);
688 if (GST_V4L2_IS_OPEN (v4l2object))
689 g_value_set_int (value, v4l2object->video_fd);
691 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
698 if (GST_V4L2_IS_OPEN (v4l2object)) {
699 flags |= v4l2object->vcap.capabilities &
700 (V4L2_CAP_VIDEO_CAPTURE |
701 V4L2_CAP_VIDEO_OUTPUT |
702 V4L2_CAP_VIDEO_OVERLAY |
703 V4L2_CAP_VBI_CAPTURE |
704 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
706 g_value_set_flags (value, flags);
709 case PROP_BRIGHTNESS:
711 case PROP_SATURATION:
714 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
717 if (GST_V4L2_IS_OPEN (v4l2object)) {
719 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
720 g_value_set_int (value, v);
728 g_value_set_enum (value, v4l2object->tv_norm);
731 g_value_set_enum (value, v4l2object->req_mode);
741 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
743 GstTunerNorm *norm = NULL;
744 GstTunerChannel *channel = NULL;
747 if (!GST_IS_TUNER (v4l2object->element))
750 tuner = GST_TUNER (v4l2object->element);
752 if (v4l2object->tv_norm)
753 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
754 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
755 "norm=%p", (guint64) v4l2object->tv_norm, norm);
757 gst_tuner_set_norm (tuner, norm);
760 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
762 v4l2object->tv_norm =
763 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
764 gst_tuner_norm_changed (tuner, norm);
768 if (v4l2object->channel)
769 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
771 gst_tuner_set_channel (tuner, channel);
774 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
775 (v4l2object->element)));
777 g_free (v4l2object->channel);
778 v4l2object->channel = g_strdup (channel->label);
779 gst_tuner_channel_changed (tuner, channel);
784 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
785 if (v4l2object->frequency != 0) {
786 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
788 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
789 if (v4l2object->frequency == 0) {
791 gst_tuner_set_frequency (tuner, channel, 1000);
799 gst_v4l2_object_open (GstV4l2Object * v4l2object)
801 if (gst_v4l2_open (v4l2object))
802 gst_v4l2_set_defaults (v4l2object);
807 gst_v4l2_video_overlay_start (v4l2object);
814 gst_v4l2_object_close (GstV4l2Object * v4l2object)
817 gst_v4l2_video_overlay_stop (v4l2object);
820 if (!gst_v4l2_close (v4l2object))
823 if (v4l2object->formats) {
824 gst_v4l2_object_clear_format_list (v4l2object);
832 * common format / caps utilities:
840 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
841 /* from Linux 2.6.15 videodev2.h */
842 {V4L2_PIX_FMT_RGB332, TRUE},
843 {V4L2_PIX_FMT_RGB555, TRUE},
844 {V4L2_PIX_FMT_RGB565, TRUE},
845 {V4L2_PIX_FMT_RGB555X, TRUE},
846 {V4L2_PIX_FMT_RGB565X, TRUE},
847 {V4L2_PIX_FMT_BGR24, TRUE},
848 {V4L2_PIX_FMT_RGB24, TRUE},
849 {V4L2_PIX_FMT_BGR32, TRUE},
850 {V4L2_PIX_FMT_RGB32, TRUE},
851 {V4L2_PIX_FMT_GREY, TRUE},
852 {V4L2_PIX_FMT_YVU410, TRUE},
853 {V4L2_PIX_FMT_YVU420, TRUE},
854 {V4L2_PIX_FMT_YUYV, TRUE},
855 {V4L2_PIX_FMT_UYVY, TRUE},
856 {V4L2_PIX_FMT_YUV422P, TRUE},
857 {V4L2_PIX_FMT_YUV411P, TRUE},
858 {V4L2_PIX_FMT_Y41P, TRUE},
860 /* two planes -- one Y, one Cr + Cb interleaved */
861 {V4L2_PIX_FMT_NV12, TRUE},
862 {V4L2_PIX_FMT_NV21, TRUE},
864 /* The following formats are not defined in the V4L2 specification */
865 {V4L2_PIX_FMT_YUV410, TRUE},
866 {V4L2_PIX_FMT_YUV420, TRUE},
867 {V4L2_PIX_FMT_YYUV, TRUE},
868 {V4L2_PIX_FMT_HI240, TRUE},
870 /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
871 #ifdef V4L2_PIX_FMT_SBGGR8
872 {V4L2_PIX_FMT_SBGGR8, TRUE},
875 /* compressed formats */
876 {V4L2_PIX_FMT_MJPEG, TRUE},
877 {V4L2_PIX_FMT_JPEG, TRUE},
878 #ifdef V4L2_PIX_FMT_PJPG
879 {V4L2_PIX_FMT_PJPG, TRUE},
881 {V4L2_PIX_FMT_DV, TRUE},
882 {V4L2_PIX_FMT_MPEG, FALSE},
883 #ifdef V4L2_PIX_FMT_MPEG4
884 {V4L2_PIX_FMT_MPEG4, TRUE},
887 #ifdef V4L2_PIX_FMT_H263
888 {V4L2_PIX_FMT_H263, TRUE},
890 #ifdef V4L2_PIX_FMT_H264
891 {V4L2_PIX_FMT_H264, TRUE},
894 /* Vendor-specific formats */
895 {V4L2_PIX_FMT_WNVA, TRUE},
897 #ifdef V4L2_PIX_FMT_SN9C10X
898 {V4L2_PIX_FMT_SN9C10X, TRUE},
900 #ifdef V4L2_PIX_FMT_PWC1
901 {V4L2_PIX_FMT_PWC1, TRUE},
903 #ifdef V4L2_PIX_FMT_PWC2
904 {V4L2_PIX_FMT_PWC2, TRUE},
906 #ifdef V4L2_PIX_FMT_YVYU
907 {V4L2_PIX_FMT_YVYU, TRUE},
911 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
914 static struct v4l2_fmtdesc *
915 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
918 struct v4l2_fmtdesc *fmt;
924 walk = gst_v4l2_object_get_format_list (v4l2object);
926 fmt = (struct v4l2_fmtdesc *) walk->data;
927 if (fmt->pixelformat == fourcc)
929 /* special case for jpeg */
930 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
931 fmt->pixelformat == V4L2_PIX_FMT_JPEG
932 #ifdef V4L2_PIX_FMT_PJPG
933 || fmt->pixelformat == V4L2_PIX_FMT_PJPG
936 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG
937 #ifdef V4L2_PIX_FMT_PJPG
938 || fourcc == V4L2_PIX_FMT_PJPG
944 walk = g_slist_next (walk);
952 /* complete made up ranking, the values themselves are meaningless */
953 /* These ranks MUST be X such that X<<15 fits on a signed int - see
954 the comment at the end of gst_v4l2_object_format_get_rank. */
955 #define YUV_BASE_RANK 1000
956 #define JPEG_BASE_RANK 500
957 #define DV_BASE_RANK 200
958 #define RGB_BASE_RANK 100
959 #define YUV_ODD_BASE_RANK 50
960 #define RGB_ODD_BASE_RANK 25
961 #define BAYER_BASE_RANK 15
962 #define S910_BASE_RANK 10
963 #define GREY_BASE_RANK 5
964 #define PWC_BASE_RANK 1
966 /* This flag is already used by libv4l2 although
967 * it was added to the Linux kernel in 2.6.32
969 #ifndef V4L2_FMT_FLAG_EMULATED
970 #define V4L2_FMT_FLAG_EMULATED 0x0002
974 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
976 guint32 fourcc = fmt->pixelformat;
977 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
981 case V4L2_PIX_FMT_MJPEG:
982 #ifdef V4L2_PIX_FMT_PJPG
983 case V4L2_PIX_FMT_PJPG:
984 rank = JPEG_BASE_RANK;
987 case V4L2_PIX_FMT_JPEG:
988 rank = JPEG_BASE_RANK + 1;
990 case V4L2_PIX_FMT_MPEG: /* MPEG */
991 rank = JPEG_BASE_RANK + 2;
994 case V4L2_PIX_FMT_RGB332:
995 case V4L2_PIX_FMT_RGB555:
996 case V4L2_PIX_FMT_RGB555X:
997 case V4L2_PIX_FMT_RGB565:
998 case V4L2_PIX_FMT_RGB565X:
999 rank = RGB_ODD_BASE_RANK;
1002 case V4L2_PIX_FMT_RGB24:
1003 case V4L2_PIX_FMT_BGR24:
1004 rank = RGB_BASE_RANK - 1;
1007 case V4L2_PIX_FMT_RGB32:
1008 case V4L2_PIX_FMT_BGR32:
1009 rank = RGB_BASE_RANK;
1012 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1013 rank = GREY_BASE_RANK;
1016 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1017 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1018 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1019 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1020 rank = YUV_ODD_BASE_RANK;
1023 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1024 rank = YUV_BASE_RANK + 3;
1026 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1027 rank = YUV_BASE_RANK + 2;
1029 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1030 rank = YUV_BASE_RANK + 7;
1032 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1033 rank = YUV_BASE_RANK + 10;
1035 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1036 rank = YUV_BASE_RANK + 6;
1038 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1039 rank = YUV_BASE_RANK + 9;
1041 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1042 rank = YUV_BASE_RANK + 5;
1044 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1045 rank = YUV_BASE_RANK + 4;
1047 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1048 rank = YUV_BASE_RANK + 8;
1051 case V4L2_PIX_FMT_DV:
1052 rank = DV_BASE_RANK;
1055 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1059 #ifdef V4L2_PIX_FMT_SBGGR8
1060 case V4L2_PIX_FMT_SBGGR8:
1061 rank = BAYER_BASE_RANK;
1065 #ifdef V4L2_PIX_FMT_SN9C10X
1066 case V4L2_PIX_FMT_SN9C10X:
1067 rank = S910_BASE_RANK;
1071 #ifdef V4L2_PIX_FMT_PWC1
1072 case V4L2_PIX_FMT_PWC1:
1073 rank = PWC_BASE_RANK;
1076 #ifdef V4L2_PIX_FMT_PWC2
1077 case V4L2_PIX_FMT_PWC2:
1078 rank = PWC_BASE_RANK;
1087 /* All ranks are below 1<<15 so a shift by 15
1088 * will a) make all non-emulated formats larger
1089 * than emulated and b) will not overflow
1100 format_cmp_func (gconstpointer a, gconstpointer b)
1102 const struct v4l2_fmtdesc *fa = a;
1103 const struct v4l2_fmtdesc *fb = b;
1105 if (fa->pixelformat == fb->pixelformat)
1108 return gst_v4l2_object_format_get_rank (fb) -
1109 gst_v4l2_object_format_get_rank (fa);
1112 /******************************************************
1113 * gst_v4l2_object_fill_format_list():
1114 * create list of supported capture formats
1115 * return value: TRUE on success, FALSE on error
1116 ******************************************************/
1118 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object)
1121 struct v4l2_fmtdesc *format;
1123 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1125 /* format enumeration */
1127 format = g_new0 (struct v4l2_fmtdesc, 1);
1130 format->type = v4l2object->type;
1132 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1133 if (errno == EINVAL) {
1135 break; /* end of enumeration */
1141 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1142 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1143 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1144 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1145 format->description);
1146 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1147 GST_FOURCC_ARGS (format->pixelformat));
1149 /* sort formats according to our preference; we do this, because caps
1150 * are probed in the order the formats are in the list, and the order of
1151 * formats in the final probed caps matters for things like fixation */
1152 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1153 (GCompareFunc) format_cmp_func);
1156 #ifndef GST_DISABLE_GST_DEBUG
1160 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1161 for (l = v4l2object->formats; l != NULL; l = l->next) {
1164 GST_INFO_OBJECT (v4l2object->element,
1165 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1166 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1176 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1177 (_("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)));
1184 * Get the list of supported capture formats, a list of
1185 * <code>struct v4l2_fmtdesc</code>.
1188 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1190 if (!v4l2object->formats)
1191 gst_v4l2_object_fill_format_list (v4l2object);
1192 return v4l2object->formats;
1197 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1199 GstStructure *structure = NULL;
1202 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1203 #ifdef V4L2_PIX_FMT_PJPG
1204 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1206 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1207 structure = gst_structure_new_empty ("image/jpeg");
1209 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1210 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1211 /* FIXME: get correct fourccs here */
1213 #ifdef V4L2_PIX_FMT_MPEG4
1214 case V4L2_PIX_FMT_MPEG4:
1215 structure = gst_structure_new ("video/mpeg",
1216 "mpegversion", G_TYPE_INT, 4, "systemstream",
1217 G_TYPE_BOOLEAN, FALSE, NULL);
1220 #ifdef V4L2_PIX_FMT_H263
1221 case V4L2_PIX_FMT_H263:
1222 structure = gst_structure_new ("video/x-h263",
1223 "variant", G_TYPE_STRING, "itu", NULL);
1226 #ifdef V4L2_PIX_FMT_H264
1227 case V4L2_PIX_FMT_H264: /* H.264 */
1228 structure = gst_structure_new_empty ("video/x-h264");
1231 case V4L2_PIX_FMT_RGB332:
1232 case V4L2_PIX_FMT_RGB555X:
1233 case V4L2_PIX_FMT_RGB565X:
1234 /* FIXME: get correct fourccs here */
1236 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1237 case V4L2_PIX_FMT_RGB555:
1238 case V4L2_PIX_FMT_RGB565:
1239 case V4L2_PIX_FMT_RGB24:
1240 case V4L2_PIX_FMT_BGR24:
1241 case V4L2_PIX_FMT_RGB32:
1242 case V4L2_PIX_FMT_BGR32:
1243 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1244 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1245 case V4L2_PIX_FMT_YVU410:
1246 case V4L2_PIX_FMT_YUV410:
1247 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1248 case V4L2_PIX_FMT_YUYV:
1249 case V4L2_PIX_FMT_YVU420:
1250 case V4L2_PIX_FMT_UYVY:
1252 case V4L2_PIX_FMT_Y41P:
1254 case V4L2_PIX_FMT_YUV422P:
1255 #ifdef V4L2_PIX_FMT_YVYU
1256 case V4L2_PIX_FMT_YVYU:
1258 case V4L2_PIX_FMT_YUV411P:{
1259 GstVideoFormat format;
1262 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1263 format = GST_VIDEO_FORMAT_GRAY8;
1265 case V4L2_PIX_FMT_RGB555:
1266 format = GST_VIDEO_FORMAT_RGB15;
1268 case V4L2_PIX_FMT_RGB565:
1269 format = GST_VIDEO_FORMAT_RGB16;
1271 case V4L2_PIX_FMT_RGB24:
1272 format = GST_VIDEO_FORMAT_RGB;
1274 case V4L2_PIX_FMT_BGR24:
1275 format = GST_VIDEO_FORMAT_BGR;
1277 case V4L2_PIX_FMT_RGB32:
1278 format = GST_VIDEO_FORMAT_RGBx;
1280 case V4L2_PIX_FMT_BGR32:
1281 format = GST_VIDEO_FORMAT_BGRx;
1283 case V4L2_PIX_FMT_NV12:
1284 format = GST_VIDEO_FORMAT_NV12;
1286 case V4L2_PIX_FMT_NV21:
1287 format = GST_VIDEO_FORMAT_NV21;
1289 case V4L2_PIX_FMT_YVU410:
1290 format = GST_VIDEO_FORMAT_YVU9;
1292 case V4L2_PIX_FMT_YUV410:
1293 format = GST_VIDEO_FORMAT_YUV9;
1295 case V4L2_PIX_FMT_YUV420:
1296 format = GST_VIDEO_FORMAT_I420;
1298 case V4L2_PIX_FMT_YUYV:
1299 format = GST_VIDEO_FORMAT_YUY2;
1301 case V4L2_PIX_FMT_YVU420:
1302 format = GST_VIDEO_FORMAT_YV12;
1304 case V4L2_PIX_FMT_UYVY:
1305 format = GST_VIDEO_FORMAT_UYVY;
1308 case V4L2_PIX_FMT_Y41P:
1309 format = GST_VIDEO_FORMAT_Y41P;
1312 case V4L2_PIX_FMT_YUV411P:
1313 format = GST_VIDEO_FORMAT_Y41B;
1315 case V4L2_PIX_FMT_YUV422P:
1316 format = GST_VIDEO_FORMAT_Y42B;
1318 #ifdef V4L2_PIX_FMT_YVYU
1319 case V4L2_PIX_FMT_YVYU:
1320 format = GST_VIDEO_FORMAT_YVYU;
1324 format = GST_VIDEO_FORMAT_UNKNOWN;
1325 g_assert_not_reached ();
1328 structure = gst_structure_new ("video/x-raw",
1329 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1332 case V4L2_PIX_FMT_DV:
1334 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1337 case V4L2_PIX_FMT_MPEG: /* MPEG */
1338 structure = gst_structure_new_empty ("video/mpegts");
1340 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1342 #ifdef V4L2_PIX_FMT_SBGGR8
1343 case V4L2_PIX_FMT_SBGGR8:
1344 structure = gst_structure_new_empty ("video/x-bayer");
1347 #ifdef V4L2_PIX_FMT_SN9C10X
1348 case V4L2_PIX_FMT_SN9C10X:
1349 structure = gst_structure_new_empty ("video/x-sonix");
1352 #ifdef V4L2_PIX_FMT_PWC1
1353 case V4L2_PIX_FMT_PWC1:
1354 structure = gst_structure_new_empty ("video/x-pwc1");
1357 #ifdef V4L2_PIX_FMT_PWC2
1358 case V4L2_PIX_FMT_PWC2:
1359 structure = gst_structure_new_empty ("video/x-pwc2");
1363 GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
1364 fourcc, GST_FOURCC_ARGS (fourcc));
1374 gst_v4l2_object_get_all_caps (void)
1376 static GstCaps *caps = NULL;
1379 GstStructure *structure;
1383 caps = gst_caps_new_empty ();
1384 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1386 gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
1388 if (gst_v4l2_formats[i].dimensions) {
1389 gst_structure_set (structure,
1390 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1391 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1392 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
1394 gst_caps_append_structure (caps, structure);
1399 return gst_caps_ref (caps);
1403 /* collect data for the given caps
1404 * @caps: given input caps
1405 * @format: location for the v4l format
1406 * @w/@h: location for width and height
1407 * @fps_n/@fps_d: location for framerate
1408 * @size: location for expected size of the frame or 0 if unknown
1411 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1412 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1414 GstStructure *structure;
1416 const gchar *mimetype;
1417 struct v4l2_fmtdesc *fmt;
1419 /* default unknown values */
1422 structure = gst_caps_get_structure (caps, 0);
1424 mimetype = gst_structure_get_name (structure);
1426 if (!gst_video_info_from_caps (info, caps))
1427 goto invalid_format;
1429 if (g_str_equal (mimetype, "video/x-raw")) {
1430 switch (GST_VIDEO_INFO_FORMAT (info)) {
1431 case GST_VIDEO_FORMAT_I420:
1432 fourcc = V4L2_PIX_FMT_YUV420;
1434 case GST_VIDEO_FORMAT_YUY2:
1435 fourcc = V4L2_PIX_FMT_YUYV;
1438 case GST_VIDEO_FORMAT_Y41P:
1439 fourcc = V4L2_PIX_FMT_Y41P;
1442 case GST_VIDEO_FORMAT_UYVY:
1443 fourcc = V4L2_PIX_FMT_UYVY;
1445 case GST_VIDEO_FORMAT_YV12:
1446 fourcc = V4L2_PIX_FMT_YVU420;
1448 case GST_VIDEO_FORMAT_Y41B:
1449 fourcc = V4L2_PIX_FMT_YUV411P;
1451 case GST_VIDEO_FORMAT_Y42B:
1452 fourcc = V4L2_PIX_FMT_YUV422P;
1454 case GST_VIDEO_FORMAT_NV12:
1455 fourcc = V4L2_PIX_FMT_NV12;
1457 case GST_VIDEO_FORMAT_NV21:
1458 fourcc = V4L2_PIX_FMT_NV21;
1460 #ifdef V4L2_PIX_FMT_YVYU
1461 case GST_VIDEO_FORMAT_YVYU:
1462 fourcc = V4L2_PIX_FMT_YVYU;
1465 case GST_VIDEO_FORMAT_RGB15:
1466 fourcc = V4L2_PIX_FMT_RGB555;
1468 case GST_VIDEO_FORMAT_RGB16:
1469 fourcc = V4L2_PIX_FMT_RGB565;
1471 case GST_VIDEO_FORMAT_RGB:
1472 fourcc = V4L2_PIX_FMT_RGB24;
1474 case GST_VIDEO_FORMAT_BGR:
1475 fourcc = V4L2_PIX_FMT_BGR24;
1477 case GST_VIDEO_FORMAT_RGBx:
1478 case GST_VIDEO_FORMAT_RGBA:
1479 fourcc = V4L2_PIX_FMT_RGB32;
1481 case GST_VIDEO_FORMAT_BGRx:
1482 case GST_VIDEO_FORMAT_BGRA:
1483 fourcc = V4L2_PIX_FMT_BGR32;
1485 case GST_VIDEO_FORMAT_GRAY8:
1486 fourcc = V4L2_PIX_FMT_GREY;
1491 gboolean dimensions = TRUE;
1493 if (g_str_equal (mimetype, "video/mpegts")) {
1494 fourcc = V4L2_PIX_FMT_MPEG;
1496 } else if (g_str_equal (mimetype, "video/x-dv")) {
1497 fourcc = V4L2_PIX_FMT_DV;
1498 } else if (g_str_equal (mimetype, "image/jpeg")) {
1499 fourcc = V4L2_PIX_FMT_JPEG;
1500 #ifdef V4L2_PIX_FMT_MPEG4
1501 } else if (g_str_equal (mimetype, "video/mpeg")) {
1502 fourcc = V4L2_PIX_FMT_MPEG4;
1504 #ifdef V4L2_PIX_FMT_H263
1505 } else if (g_str_equal (mimetype, "video/x-h263")) {
1506 fourcc = V4L2_PIX_FMT_H263;
1508 #ifdef V4L2_PIX_FMT_H264
1509 } else if (g_str_equal (mimetype, "video/x-h264")) {
1510 fourcc = V4L2_PIX_FMT_H264;
1512 #ifdef V4L2_PIX_FMT_SBGGR8
1513 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1514 fourcc = V4L2_PIX_FMT_SBGGR8;
1516 #ifdef V4L2_PIX_FMT_SN9C10X
1517 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1518 fourcc = V4L2_PIX_FMT_SN9C10X;
1520 #ifdef V4L2_PIX_FMT_PWC1
1521 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1522 fourcc = V4L2_PIX_FMT_PWC1;
1524 #ifdef V4L2_PIX_FMT_PWC2
1525 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1526 fourcc = V4L2_PIX_FMT_PWC2;
1531 const gchar *interlace_mode;
1533 if (!gst_structure_get_int (structure, "width", &info->width))
1536 if (!gst_structure_get_int (structure, "height", &info->height))
1539 interlace_mode = gst_structure_get_string (structure, "interlace-mode");
1540 if (g_str_equal (interlace_mode, "progressive")) {
1541 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1543 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
1545 if (!gst_structure_get_fraction (structure, "framerate", &info->fps_n,
1552 goto unhandled_format;
1554 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1556 goto unsupported_format;
1565 GST_DEBUG_OBJECT (v4l2object, "no width");
1570 GST_DEBUG_OBJECT (v4l2object, "no height");
1575 GST_DEBUG_OBJECT (v4l2object, "no framerate");
1580 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1585 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1590 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1597 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1598 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced);
1601 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1602 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1603 static GstStructure *
1604 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1605 guint32 pixelformat,
1606 guint32 width, guint32 height, const GstStructure * template)
1608 gint fd = v4l2object->video_fd;
1609 struct v4l2_frmivalenum ival;
1612 GValue rates = { 0, };
1613 gboolean interlaced;
1614 gint int_width = width;
1615 gint int_height = height;
1617 if (!strcmp ((char *) v4l2object->vcap.driver, "uvcvideo")) {
1619 * UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
1620 * causes expensive and slow USB IO, so don't probe them for interlaced
1624 /* Interlaced detection using VIDIOC_TRY/S_FMT */
1625 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat,
1626 &int_width, &int_height, &interlaced))
1630 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1632 ival.pixel_format = pixelformat;
1634 ival.height = height;
1636 GST_LOG_OBJECT (v4l2object->element,
1637 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1638 GST_FOURCC_ARGS (pixelformat));
1640 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1641 * fraction to get framerate */
1642 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1643 goto enum_frameintervals_failed;
1645 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1646 GValue rate = { 0, };
1648 g_value_init (&rates, GST_TYPE_LIST);
1649 g_value_init (&rate, GST_TYPE_FRACTION);
1652 num = ival.discrete.numerator;
1653 denom = ival.discrete.denominator;
1655 if (num > G_MAXINT || denom > G_MAXINT) {
1656 /* let us hope we don't get here... */
1661 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1664 /* swap to get the framerate */
1665 gst_value_set_fraction (&rate, denom, num);
1666 gst_value_list_append_value (&rates, &rate);
1669 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1670 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1671 GValue min = { 0, };
1672 GValue step = { 0, };
1673 GValue max = { 0, };
1674 gboolean added = FALSE;
1675 guint32 minnum, mindenom;
1676 guint32 maxnum, maxdenom;
1678 g_value_init (&rates, GST_TYPE_LIST);
1680 g_value_init (&min, GST_TYPE_FRACTION);
1681 g_value_init (&step, GST_TYPE_FRACTION);
1682 g_value_init (&max, GST_TYPE_FRACTION);
1685 minnum = ival.stepwise.min.numerator;
1686 mindenom = ival.stepwise.min.denominator;
1687 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1691 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1693 gst_value_set_fraction (&min, minnum, mindenom);
1696 maxnum = ival.stepwise.max.numerator;
1697 maxdenom = ival.stepwise.max.denominator;
1698 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1703 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1705 gst_value_set_fraction (&max, maxnum, maxdenom);
1708 num = ival.stepwise.step.numerator;
1709 denom = ival.stepwise.step.denominator;
1710 if (num > G_MAXINT || denom > G_MAXINT) {
1715 if (num == 0 || denom == 0) {
1716 /* in this case we have a wrong fraction or no step, set the step to max
1717 * so that we only add the min value in the loop below */
1722 /* since we only have gst_value_fraction_subtract and not add, negate the
1724 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1726 gst_value_set_fraction (&step, -num, denom);
1728 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
1729 GValue rate = { 0, };
1731 num = gst_value_get_fraction_numerator (&min);
1732 denom = gst_value_get_fraction_denominator (&min);
1733 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1736 /* invert to get the framerate */
1737 g_value_init (&rate, GST_TYPE_FRACTION);
1738 gst_value_set_fraction (&rate, denom, num);
1739 gst_value_list_append_value (&rates, &rate);
1742 /* we're actually adding because step was negated above. This is because
1743 * there is no _add function... */
1744 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1745 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1750 /* no range was added, leave the default range from the template */
1751 GST_WARNING_OBJECT (v4l2object->element,
1752 "no range added, leaving default");
1753 g_value_unset (&rates);
1755 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1756 guint32 maxnum, maxdenom;
1758 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1760 num = ival.stepwise.min.numerator;
1761 denom = ival.stepwise.min.denominator;
1762 if (num > G_MAXINT || denom > G_MAXINT) {
1767 maxnum = ival.stepwise.max.numerator;
1768 maxdenom = ival.stepwise.max.denominator;
1769 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1774 GST_LOG_OBJECT (v4l2object->element,
1775 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
1778 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1784 s = gst_structure_copy (template);
1785 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
1786 "height", G_TYPE_INT, (gint) height,
1787 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, NULL);
1788 if (g_str_equal (gst_structure_get_name (s), "video/x-raw"))
1789 gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
1790 (interlaced ? "mixed" : "progressive"), NULL);
1792 if (G_IS_VALUE (&rates)) {
1793 /* only change the framerate on the template when we have a valid probed new
1795 gst_structure_set_value (s, "framerate", &rates);
1796 g_value_unset (&rates);
1798 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
1804 enum_frameintervals_failed:
1806 GST_DEBUG_OBJECT (v4l2object->element,
1807 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
1808 GST_FOURCC_ARGS (pixelformat), width, height);
1813 /* I don't see how this is actually an error, we ignore the format then */
1814 GST_WARNING_OBJECT (v4l2object->element,
1815 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
1816 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
1820 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
1822 #ifdef VIDIOC_ENUM_FRAMESIZES
1824 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
1828 gst_structure_get_int (s1, "width", &w1);
1829 gst_structure_get_int (s1, "height", &h1);
1830 gst_structure_get_int (s2, "width", &w2);
1831 gst_structure_get_int (s2, "height", &h2);
1833 /* I think it's safe to assume that this won't overflow for a while */
1834 return ((w2 * h2) - (w1 * h1));
1839 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
1840 guint32 pixelformat, const GstStructure * template)
1842 GstCaps *ret = gst_caps_new_empty ();
1845 #ifdef VIDIOC_ENUM_FRAMESIZES
1846 gint fd = v4l2object->video_fd;
1847 struct v4l2_frmsizeenum size;
1848 GList *results = NULL;
1851 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1852 return gst_caps_new_empty_simple ("video/mpegts");
1854 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
1856 size.pixel_format = pixelformat;
1858 GST_DEBUG_OBJECT (v4l2object->element, "Enumerating frame sizes");
1860 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
1861 goto enum_framesizes_failed;
1863 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1865 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
1866 size.discrete.width, size.discrete.height);
1868 w = MIN (size.discrete.width, G_MAXINT);
1869 h = MIN (size.discrete.height, G_MAXINT);
1873 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1874 pixelformat, w, h, template);
1877 results = g_list_prepend (results, tmp);
1881 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
1882 GST_DEBUG_OBJECT (v4l2object->element,
1883 "done iterating discrete frame sizes");
1884 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
1885 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
1886 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1887 size.stepwise.min_width);
1888 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1889 size.stepwise.min_height);
1890 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1891 size.stepwise.max_width);
1892 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1893 size.stepwise.max_height);
1894 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
1895 size.stepwise.step_width);
1896 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
1897 size.stepwise.step_height);
1899 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
1900 w <= size.stepwise.max_width && h <= size.stepwise.max_height;
1901 w += size.stepwise.step_width, h += size.stepwise.step_height) {
1902 if (w == 0 || h == 0)
1906 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1907 pixelformat, w, h, template);
1910 results = g_list_prepend (results, tmp);
1912 GST_DEBUG_OBJECT (v4l2object->element,
1913 "done iterating stepwise frame sizes");
1914 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
1917 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
1918 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1919 size.stepwise.min_width);
1920 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1921 size.stepwise.min_height);
1922 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1923 size.stepwise.max_width);
1924 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1925 size.stepwise.max_height);
1927 w = MAX (size.stepwise.min_width, 1);
1928 h = MAX (size.stepwise.min_height, 1);
1929 maxw = MIN (size.stepwise.max_width, G_MAXINT);
1930 maxh = MIN (size.stepwise.max_height, G_MAXINT);
1933 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
1936 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
1937 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
1940 /* no point using the results list here, since there's only one struct */
1941 gst_caps_append_structure (ret, tmp);
1947 /* we use an intermediary list to store and then sort the results of the
1948 * probing because we can't make any assumptions about the order in which
1949 * the driver will give us the sizes, but we want the final caps to contain
1950 * the results starting with the highest resolution and having the lowest
1951 * resolution last, since order in caps matters for things like fixation. */
1952 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
1953 while (results != NULL) {
1954 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
1955 results = g_list_delete_link (results, results);
1958 if (gst_caps_is_empty (ret))
1959 goto enum_framesizes_no_results;
1964 enum_framesizes_failed:
1966 /* I don't see how this is actually an error */
1967 GST_DEBUG_OBJECT (v4l2object->element,
1968 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
1969 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
1970 goto default_frame_sizes;
1972 enum_framesizes_no_results:
1974 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
1975 * question doesn't actually support it yet */
1976 GST_DEBUG_OBJECT (v4l2object->element,
1977 "No results for pixelformat %" GST_FOURCC_FORMAT
1978 " enumerating frame sizes, trying fallback",
1979 GST_FOURCC_ARGS (pixelformat));
1980 goto default_frame_sizes;
1984 GST_WARNING_OBJECT (v4l2object->element,
1985 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
1986 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
1987 goto default_frame_sizes;
1989 default_frame_sizes:
1990 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
1992 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
1993 gboolean interlaced;
1995 /* This code is for Linux < 2.6.19 */
1997 max_w = max_h = GST_V4L2_MAX_SIZE;
1998 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
1999 &min_h, &interlaced)) {
2000 GST_WARNING_OBJECT (v4l2object->element,
2001 "Could not probe minimum capture size for pixelformat %"
2002 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2004 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2005 &max_h, &interlaced)) {
2006 GST_WARNING_OBJECT (v4l2object->element,
2007 "Could not probe maximum capture size for pixelformat %"
2008 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2011 /* Since we can't get framerate directly, try to use the current norm */
2012 if (v4l2object->tv_norm && v4l2object->norms) {
2014 GstTunerNorm *norm = NULL;
2015 GstTunerNorm *current =
2016 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2018 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2019 norm = (GstTunerNorm *) norms->data;
2020 if (!strcmp (norm->label, current->label))
2023 /* If it's possible, set framerate to that (discrete) value */
2025 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2026 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2030 tmp = gst_structure_copy (template);
2032 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2035 /* if norm can't be used, copy the template framerate */
2036 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2041 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2043 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2046 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2048 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2050 if (g_str_equal (gst_structure_get_name (tmp), "video/x-raw"))
2051 gst_structure_set (tmp, "interlace-mode", G_TYPE_STRING,
2052 (interlaced ? "mixed" : "progressive"), NULL);
2053 gst_structure_set (tmp, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
2056 gst_caps_append_structure (ret, tmp);
2063 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2064 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced)
2066 struct v4l2_format fmt, prevfmt;
2071 g_return_val_if_fail (width != NULL, FALSE);
2072 g_return_val_if_fail (height != NULL, FALSE);
2074 GST_LOG_OBJECT (v4l2object->element,
2075 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2076 *width, *height, GST_FOURCC_ARGS (pixelformat));
2078 fd = v4l2object->video_fd;
2080 /* Some drivers are buggy and will modify the currently set format
2081 when processing VIDIOC_TRY_FMT, so we remember what is set at the
2082 minute, and will reset it when done. */
2083 prevfmt.type = v4l2object->type;
2084 prevfmt_valid = (v4l2_ioctl (fd, VIDIOC_G_FMT, &prevfmt) >= 0);
2086 /* get size delimiters */
2087 memset (&fmt, 0, sizeof (fmt));
2088 fmt.type = v4l2object->type;
2089 fmt.fmt.pix.width = *width;
2090 fmt.fmt.pix.height = *height;
2091 fmt.fmt.pix.pixelformat = pixelformat;
2092 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2094 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2095 if (r < 0 && errno == EINVAL) {
2096 /* try again with interlaced video */
2097 fmt.fmt.pix.width = *width;
2098 fmt.fmt.pix.height = *height;
2099 fmt.fmt.pix.pixelformat = pixelformat;
2100 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2101 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2105 /* The driver might not implement TRY_FMT, in which case we will try
2107 if (errno != ENOTTY)
2110 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
2111 be, because we're still probing */
2112 if (GST_V4L2_IS_ACTIVE (v4l2object))
2115 GST_LOG_OBJECT (v4l2object->element,
2116 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
2118 fmt.fmt.pix.width = *width;
2119 fmt.fmt.pix.height = *height;
2121 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2122 if (r < 0 && errno == EINVAL) {
2123 /* try again with progressive video */
2124 fmt.fmt.pix.width = *width;
2125 fmt.fmt.pix.height = *height;
2126 fmt.fmt.pix.pixelformat = pixelformat;
2127 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2128 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2135 GST_LOG_OBJECT (v4l2object->element,
2136 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2138 *width = fmt.fmt.pix.width;
2139 *height = fmt.fmt.pix.height;
2141 switch (fmt.fmt.pix.field) {
2142 case V4L2_FIELD_ANY:
2143 case V4L2_FIELD_NONE:
2144 *interlaced = FALSE;
2146 case V4L2_FIELD_INTERLACED:
2147 case V4L2_FIELD_INTERLACED_TB:
2148 case V4L2_FIELD_INTERLACED_BT:
2152 GST_WARNING_OBJECT (v4l2object->element,
2153 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u",
2154 GST_FOURCC_ARGS (pixelformat), *width, *height);
2159 v4l2_ioctl (fd, VIDIOC_S_FMT, &prevfmt);
2164 v4l2_ioctl (fd, VIDIOC_S_FMT, &prevfmt);
2169 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2173 GST_DEBUG_OBJECT (v4l2object->element, "initializing the capture system");
2175 GST_V4L2_CHECK_OPEN (v4l2object);
2176 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2178 /* find transport */
2179 mode = v4l2object->req_mode;
2181 if (v4l2object->vcap.capabilities & V4L2_CAP_READWRITE) {
2182 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2183 mode = GST_V4L2_IO_RW;
2184 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2185 goto method_not_supported;
2187 if (v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
2188 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2189 mode = GST_V4L2_IO_MMAP;
2190 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2191 goto method_not_supported;
2193 /* if still no transport selected, error out */
2194 if (mode == GST_V4L2_IO_AUTO)
2195 goto no_supported_capture_method;
2197 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2198 v4l2object->mode = mode;
2200 /* Map the buffers */
2201 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2203 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2204 goto buffer_pool_new_failed;
2206 GST_V4L2_SET_ACTIVE (v4l2object);
2211 buffer_pool_new_failed:
2213 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2214 (_("Could not map buffers from device '%s'"),
2215 v4l2object->videodev),
2216 ("Failed to create buffer pool: %s", g_strerror (errno)));
2219 method_not_supported:
2221 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2222 (_("The driver of device '%s' does not support the IO method %d"),
2223 v4l2object->videodev, mode), (NULL));
2226 no_supported_capture_method:
2228 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2229 (_("The driver of device '%s' does not support any known IO "
2230 "method."), v4l2object->videodev), (NULL));
2236 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps)
2238 gint fd = v4l2object->video_fd;
2239 struct v4l2_format format;
2240 struct v4l2_streamparm streamparm;
2241 enum v4l2_field field;
2242 guint32 pixelformat;
2243 struct v4l2_fmtdesc *fmtdesc;
2245 gint width, height, fps_n, fps_d, stride;
2247 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
2250 pixelformat = fmtdesc->pixelformat;
2251 width = GST_VIDEO_INFO_WIDTH (&info);
2252 height = GST_VIDEO_INFO_HEIGHT (&info);
2253 fps_n = GST_VIDEO_INFO_FPS_N (&info);
2254 fps_d = GST_VIDEO_INFO_FPS_D (&info);
2255 stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
2257 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
2258 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
2259 /* ideally we would differentiate between types of interlaced video
2260 * but there is not sufficient information in the caps..
2262 field = V4L2_FIELD_INTERLACED;
2264 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
2265 field = V4L2_FIELD_NONE;
2268 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
2269 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
2270 GST_FOURCC_ARGS (pixelformat), stride);
2272 GST_V4L2_CHECK_OPEN (v4l2object);
2273 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2275 /* MPEG-TS source cameras don't get their format set for some reason.
2276 * It looks wrong and we weren't able to track down the reason for that code
2277 * so it is disabled until someone who has an mpeg-ts camera complains...
2280 /* Only unconditionally accept mpegts for sources */
2281 if ((v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
2282 (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')))
2286 memset (&format, 0x00, sizeof (struct v4l2_format));
2287 format.type = v4l2object->type;
2289 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
2290 goto get_fmt_failed;
2292 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2293 "%" GST_FOURCC_FORMAT " bytesperline %d, colorspace %d",
2294 format.fmt.pix.width, format.fmt.pix.height,
2295 GST_FOURCC_ARGS (format.fmt.pix.pixelformat), format.fmt.pix.bytesperline,
2296 format.fmt.pix.colorspace);
2298 if (format.type != v4l2object->type ||
2299 format.fmt.pix.width != width ||
2300 format.fmt.pix.height != height ||
2301 format.fmt.pix.pixelformat != pixelformat ||
2302 format.fmt.pix.field != field || format.fmt.pix.bytesperline != stride) {
2303 /* something different, set the format */
2304 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2305 "%" GST_FOURCC_FORMAT " bytesperline %d", width, height,
2306 GST_FOURCC_ARGS (pixelformat), stride);
2308 format.type = v4l2object->type;
2309 format.fmt.pix.width = width;
2310 format.fmt.pix.height = height;
2311 format.fmt.pix.pixelformat = pixelformat;
2312 format.fmt.pix.field = field;
2313 /* try to ask our prefered stride */
2314 format.fmt.pix.bytesperline = stride;
2316 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
2317 goto set_fmt_failed;
2319 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2320 "%" GST_FOURCC_FORMAT " stride %d", format.fmt.pix.width,
2321 format.fmt.pix.height, GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2322 format.fmt.pix.bytesperline);
2324 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
2325 goto invalid_dimensions;
2327 if (format.fmt.pix.pixelformat != pixelformat)
2328 goto invalid_pixelformat;
2331 /* figure out the frame layout */
2332 v4l2object->bytesperline = format.fmt.pix.bytesperline;
2333 v4l2object->sizeimage = format.fmt.pix.sizeimage;
2335 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %u",
2336 v4l2object->sizeimage);
2338 /* Is there a reason we require the caller to always specify a framerate? */
2339 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
2342 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
2343 streamparm.type = v4l2object->type;
2345 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
2346 goto get_parm_failed;
2348 GST_VIDEO_INFO_FPS_N (&info) =
2349 streamparm.parm.capture.timeperframe.denominator;
2350 GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
2352 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2353 GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
2354 streamparm.parm.capture.timeperframe.denominator,
2355 streamparm.parm.capture.timeperframe.numerator);
2357 /* We used to skip frame rate setup if the camera was already setup
2358 * with the requested frame rate. This breaks some cameras though,
2359 * causing them to not output data (several models of Thinkpad cameras
2360 * have this problem at least).
2361 * So, don't skip. */
2362 GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
2364 /* We want to change the frame rate, so check whether we can. Some cheap USB
2365 * cameras don't have the capability */
2366 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
2367 GST_DEBUG_OBJECT (v4l2object->element,
2368 "Not setting framerate (not supported)");
2372 /* Note: V4L2 wants the frame interval, we have the frame rate */
2373 streamparm.parm.capture.timeperframe.numerator = fps_d;
2374 streamparm.parm.capture.timeperframe.denominator = fps_n;
2376 /* some cheap USB cam's won't accept any change */
2377 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
2378 goto set_parm_failed;
2380 /* get new values */
2381 fps_d = streamparm.parm.capture.timeperframe.numerator;
2382 fps_n = streamparm.parm.capture.timeperframe.denominator;
2384 GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
2387 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
2388 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
2392 /* if we have a framerate pre-calculate duration */
2393 if (fps_n > 0 && fps_d > 0) {
2394 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
2396 v4l2object->duration = GST_CLOCK_TIME_NONE;
2398 v4l2object->info = info;
2399 v4l2object->fmtdesc = fmtdesc;
2401 /* now configure ther pools */
2402 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
2410 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
2416 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2417 (_("Device '%s' does not support video capture"),
2418 v4l2object->videodev),
2419 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
2424 if (errno == EBUSY) {
2425 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, BUSY,
2426 (_("Device '%s' is busy"), v4l2object->videodev),
2427 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2428 GST_FOURCC_ARGS (pixelformat), width, height,
2429 g_strerror (errno)));
2431 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2432 (_("Device '%s' cannot capture at %dx%d"),
2433 v4l2object->videodev, width, height),
2434 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2435 GST_FOURCC_ARGS (pixelformat), width, height,
2436 g_strerror (errno)));
2442 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2443 (_("Device '%s' cannot capture at %dx%d"),
2444 v4l2object->videodev, width, height),
2445 ("Tried to capture at %dx%d, but device returned size %dx%d",
2446 width, height, format.fmt.pix.width, format.fmt.pix.height));
2449 invalid_pixelformat:
2451 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2452 (_("Device '%s' cannot capture in the specified format"),
2453 v4l2object->videodev),
2454 ("Tried to capture in %" GST_FOURCC_FORMAT
2455 ", but device returned format" " %" GST_FOURCC_FORMAT,
2456 GST_FOURCC_ARGS (pixelformat),
2457 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
2462 /* it's possible that this call is not supported */
2463 if (errno != EINVAL && errno != ENOTTY) {
2464 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2465 (_("Could not get parameters on device '%s'"),
2466 v4l2object->videodev), GST_ERROR_SYSTEM);
2472 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2473 (_("Video device did not accept new frame rate setting.")),
2479 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2480 (_("Video device could not create buffer pool.")), GST_ERROR_SYSTEM);
2486 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
2491 if (!v4l2object->pool)
2494 s = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (v4l2object->pool));
2495 gst_buffer_pool_config_get_params (s, &oldcaps, NULL, NULL, NULL);
2497 return oldcaps && gst_caps_is_equal (caps, oldcaps);
2501 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
2503 GST_LOG_OBJECT (v4l2object->element, "flush poll");
2504 gst_poll_set_flushing (v4l2object->poll, TRUE);
2510 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
2512 GST_LOG_OBJECT (v4l2object->element, "flush stop poll");
2513 gst_poll_set_flushing (v4l2object->poll, FALSE);
2519 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
2521 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
2523 if (!GST_V4L2_IS_OPEN (v4l2object))
2525 if (!GST_V4L2_IS_ACTIVE (v4l2object))
2528 if (v4l2object->pool) {
2529 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
2530 gst_buffer_pool_set_active (GST_BUFFER_POOL_CAST (v4l2object->pool), FALSE);
2531 gst_object_unref (v4l2object->pool);
2532 v4l2object->pool = NULL;
2535 GST_V4L2_SET_INACTIVE (v4l2object);
2542 gst_v4l2_object_copy (GstV4l2Object * v4l2object, GstBuffer * dest,
2545 const GstVideoFormatInfo *finfo = v4l2object->info.finfo;
2547 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
2548 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
2549 GstVideoFrame src_frame, dest_frame;
2551 GST_DEBUG_OBJECT (v4l2object->element, "copy video frame");
2553 /* we have raw video, use videoframe copy to get strides right */
2554 if (!gst_video_frame_map (&src_frame, &v4l2object->info, src, GST_MAP_READ))
2555 goto invalid_buffer;
2557 if (!gst_video_frame_map (&dest_frame, &v4l2object->info, dest,
2559 gst_video_frame_unmap (&src_frame);
2560 goto invalid_buffer;
2563 gst_video_frame_copy (&dest_frame, &src_frame);
2565 gst_video_frame_unmap (&src_frame);
2566 gst_video_frame_unmap (&dest_frame);
2570 GST_DEBUG_OBJECT (v4l2object->element, "copy raw bytes");
2571 gst_buffer_map (src, &map, GST_MAP_READ);
2572 gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
2573 gst_buffer_unmap (src, &map);
2574 gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
2576 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, v4l2object->element,
2577 "slow copy into buffer %p", dest);
2584 /* No Window available to put our image into */
2585 GST_WARNING_OBJECT (v4l2object->element, "could not map image");