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 (g_str_equal (mimetype, "video/x-raw")) {
1427 /* raw caps, parse into video info */
1428 if (!gst_video_info_from_caps (info, caps))
1429 goto invalid_format;
1431 switch (GST_VIDEO_INFO_FORMAT (info)) {
1432 case GST_VIDEO_FORMAT_I420:
1433 fourcc = V4L2_PIX_FMT_YUV420;
1435 case GST_VIDEO_FORMAT_YUY2:
1436 fourcc = V4L2_PIX_FMT_YUYV;
1439 case GST_VIDEO_FORMAT_Y41P:
1440 fourcc = V4L2_PIX_FMT_Y41P;
1443 case GST_VIDEO_FORMAT_UYVY:
1444 fourcc = V4L2_PIX_FMT_UYVY;
1446 case GST_VIDEO_FORMAT_YV12:
1447 fourcc = V4L2_PIX_FMT_YVU420;
1449 case GST_VIDEO_FORMAT_Y41B:
1450 fourcc = V4L2_PIX_FMT_YUV411P;
1452 case GST_VIDEO_FORMAT_Y42B:
1453 fourcc = V4L2_PIX_FMT_YUV422P;
1455 case GST_VIDEO_FORMAT_NV12:
1456 fourcc = V4L2_PIX_FMT_NV12;
1458 case GST_VIDEO_FORMAT_NV21:
1459 fourcc = V4L2_PIX_FMT_NV21;
1461 #ifdef V4L2_PIX_FMT_YVYU
1462 case GST_VIDEO_FORMAT_YVYU:
1463 fourcc = V4L2_PIX_FMT_YVYU;
1466 case GST_VIDEO_FORMAT_RGB15:
1467 fourcc = V4L2_PIX_FMT_RGB555;
1469 case GST_VIDEO_FORMAT_RGB16:
1470 fourcc = V4L2_PIX_FMT_RGB565;
1472 case GST_VIDEO_FORMAT_RGB:
1473 fourcc = V4L2_PIX_FMT_RGB24;
1475 case GST_VIDEO_FORMAT_BGR:
1476 fourcc = V4L2_PIX_FMT_BGR24;
1478 case GST_VIDEO_FORMAT_RGBx:
1479 case GST_VIDEO_FORMAT_RGBA:
1480 fourcc = V4L2_PIX_FMT_RGB32;
1482 case GST_VIDEO_FORMAT_BGRx:
1483 case GST_VIDEO_FORMAT_BGRA:
1484 fourcc = V4L2_PIX_FMT_BGR32;
1486 case GST_VIDEO_FORMAT_GRAY8:
1487 fourcc = V4L2_PIX_FMT_GREY;
1492 gboolean dimensions = TRUE;
1494 /* no video caps, construct videoinfo ourselves */
1495 gst_video_info_init (info);
1497 if (g_str_equal (mimetype, "video/mpegts")) {
1498 fourcc = V4L2_PIX_FMT_MPEG;
1500 } else if (g_str_equal (mimetype, "video/x-dv")) {
1501 fourcc = V4L2_PIX_FMT_DV;
1502 } else if (g_str_equal (mimetype, "image/jpeg")) {
1503 fourcc = V4L2_PIX_FMT_JPEG;
1504 #ifdef V4L2_PIX_FMT_MPEG4
1505 } else if (g_str_equal (mimetype, "video/mpeg")) {
1506 fourcc = V4L2_PIX_FMT_MPEG4;
1508 #ifdef V4L2_PIX_FMT_H263
1509 } else if (g_str_equal (mimetype, "video/x-h263")) {
1510 fourcc = V4L2_PIX_FMT_H263;
1512 #ifdef V4L2_PIX_FMT_H264
1513 } else if (g_str_equal (mimetype, "video/x-h264")) {
1514 fourcc = V4L2_PIX_FMT_H264;
1516 #ifdef V4L2_PIX_FMT_SBGGR8
1517 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1518 fourcc = V4L2_PIX_FMT_SBGGR8;
1520 #ifdef V4L2_PIX_FMT_SN9C10X
1521 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1522 fourcc = V4L2_PIX_FMT_SN9C10X;
1524 #ifdef V4L2_PIX_FMT_PWC1
1525 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1526 fourcc = V4L2_PIX_FMT_PWC1;
1528 #ifdef V4L2_PIX_FMT_PWC2
1529 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1530 fourcc = V4L2_PIX_FMT_PWC2;
1535 const gchar *interlace_mode;
1537 if (!gst_structure_get_int (structure, "width", &info->width))
1540 if (!gst_structure_get_int (structure, "height", &info->height))
1543 interlace_mode = gst_structure_get_string (structure, "interlace-mode");
1544 if (g_str_equal (interlace_mode, "progressive")) {
1545 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1547 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
1549 if (!gst_structure_get_fraction (structure, "framerate", &info->fps_n,
1556 goto unhandled_format;
1558 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1560 goto unsupported_format;
1569 GST_DEBUG_OBJECT (v4l2object, "no width");
1574 GST_DEBUG_OBJECT (v4l2object, "no height");
1579 GST_DEBUG_OBJECT (v4l2object, "no framerate");
1584 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1589 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1594 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1601 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1602 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced);
1605 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1606 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1607 static GstStructure *
1608 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1609 guint32 pixelformat,
1610 guint32 width, guint32 height, const GstStructure * template)
1612 gint fd = v4l2object->video_fd;
1613 struct v4l2_frmivalenum ival;
1616 GValue rates = { 0, };
1617 gboolean interlaced;
1618 gint int_width = width;
1619 gint int_height = height;
1621 if (!strcmp ((char *) v4l2object->vcap.driver, "uvcvideo")) {
1623 * UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
1624 * causes expensive and slow USB IO, so don't probe them for interlaced
1628 /* Interlaced detection using VIDIOC_TRY/S_FMT */
1629 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat,
1630 &int_width, &int_height, &interlaced))
1634 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1636 ival.pixel_format = pixelformat;
1638 ival.height = height;
1640 GST_LOG_OBJECT (v4l2object->element,
1641 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1642 GST_FOURCC_ARGS (pixelformat));
1644 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1645 * fraction to get framerate */
1646 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1647 goto enum_frameintervals_failed;
1649 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1650 GValue rate = { 0, };
1652 g_value_init (&rates, GST_TYPE_LIST);
1653 g_value_init (&rate, GST_TYPE_FRACTION);
1656 num = ival.discrete.numerator;
1657 denom = ival.discrete.denominator;
1659 if (num > G_MAXINT || denom > G_MAXINT) {
1660 /* let us hope we don't get here... */
1665 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1668 /* swap to get the framerate */
1669 gst_value_set_fraction (&rate, denom, num);
1670 gst_value_list_append_value (&rates, &rate);
1673 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1674 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1675 GValue min = { 0, };
1676 GValue step = { 0, };
1677 GValue max = { 0, };
1678 gboolean added = FALSE;
1679 guint32 minnum, mindenom;
1680 guint32 maxnum, maxdenom;
1682 g_value_init (&rates, GST_TYPE_LIST);
1684 g_value_init (&min, GST_TYPE_FRACTION);
1685 g_value_init (&step, GST_TYPE_FRACTION);
1686 g_value_init (&max, GST_TYPE_FRACTION);
1689 minnum = ival.stepwise.min.numerator;
1690 mindenom = ival.stepwise.min.denominator;
1691 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1695 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1697 gst_value_set_fraction (&min, minnum, mindenom);
1700 maxnum = ival.stepwise.max.numerator;
1701 maxdenom = ival.stepwise.max.denominator;
1702 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1707 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1709 gst_value_set_fraction (&max, maxnum, maxdenom);
1712 num = ival.stepwise.step.numerator;
1713 denom = ival.stepwise.step.denominator;
1714 if (num > G_MAXINT || denom > G_MAXINT) {
1719 if (num == 0 || denom == 0) {
1720 /* in this case we have a wrong fraction or no step, set the step to max
1721 * so that we only add the min value in the loop below */
1726 /* since we only have gst_value_fraction_subtract and not add, negate the
1728 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1730 gst_value_set_fraction (&step, -num, denom);
1732 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
1733 GValue rate = { 0, };
1735 num = gst_value_get_fraction_numerator (&min);
1736 denom = gst_value_get_fraction_denominator (&min);
1737 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1740 /* invert to get the framerate */
1741 g_value_init (&rate, GST_TYPE_FRACTION);
1742 gst_value_set_fraction (&rate, denom, num);
1743 gst_value_list_append_value (&rates, &rate);
1746 /* we're actually adding because step was negated above. This is because
1747 * there is no _add function... */
1748 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1749 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1754 /* no range was added, leave the default range from the template */
1755 GST_WARNING_OBJECT (v4l2object->element,
1756 "no range added, leaving default");
1757 g_value_unset (&rates);
1759 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1760 guint32 maxnum, maxdenom;
1762 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1764 num = ival.stepwise.min.numerator;
1765 denom = ival.stepwise.min.denominator;
1766 if (num > G_MAXINT || denom > G_MAXINT) {
1771 maxnum = ival.stepwise.max.numerator;
1772 maxdenom = ival.stepwise.max.denominator;
1773 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1778 GST_LOG_OBJECT (v4l2object->element,
1779 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
1782 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1788 s = gst_structure_copy (template);
1789 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
1790 "height", G_TYPE_INT, (gint) height,
1791 "interlace-mode", G_TYPE_STRING, (interlaced ? "mixed" : "progressive"),
1792 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, NULL);
1794 if (G_IS_VALUE (&rates)) {
1795 /* only change the framerate on the template when we have a valid probed new
1797 gst_structure_set_value (s, "framerate", &rates);
1798 g_value_unset (&rates);
1800 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
1806 enum_frameintervals_failed:
1808 GST_DEBUG_OBJECT (v4l2object->element,
1809 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
1810 GST_FOURCC_ARGS (pixelformat), width, height);
1815 /* I don't see how this is actually an error, we ignore the format then */
1816 GST_WARNING_OBJECT (v4l2object->element,
1817 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
1818 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
1822 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
1824 #ifdef VIDIOC_ENUM_FRAMESIZES
1826 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
1830 gst_structure_get_int (s1, "width", &w1);
1831 gst_structure_get_int (s1, "height", &h1);
1832 gst_structure_get_int (s2, "width", &w2);
1833 gst_structure_get_int (s2, "height", &h2);
1835 /* I think it's safe to assume that this won't overflow for a while */
1836 return ((w2 * h2) - (w1 * h1));
1841 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
1842 guint32 pixelformat, const GstStructure * template)
1844 GstCaps *ret = gst_caps_new_empty ();
1847 #ifdef VIDIOC_ENUM_FRAMESIZES
1848 gint fd = v4l2object->video_fd;
1849 struct v4l2_frmsizeenum size;
1850 GList *results = NULL;
1853 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1854 return gst_caps_new_empty_simple ("video/mpegts");
1856 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
1858 size.pixel_format = pixelformat;
1860 GST_DEBUG_OBJECT (v4l2object->element, "Enumerating frame sizes");
1862 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
1863 goto enum_framesizes_failed;
1865 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1867 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
1868 size.discrete.width, size.discrete.height);
1870 w = MIN (size.discrete.width, G_MAXINT);
1871 h = MIN (size.discrete.height, G_MAXINT);
1875 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1876 pixelformat, w, h, template);
1879 results = g_list_prepend (results, tmp);
1883 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
1884 GST_DEBUG_OBJECT (v4l2object->element,
1885 "done iterating discrete frame sizes");
1886 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
1887 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
1888 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1889 size.stepwise.min_width);
1890 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1891 size.stepwise.min_height);
1892 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1893 size.stepwise.max_width);
1894 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1895 size.stepwise.max_height);
1896 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
1897 size.stepwise.step_width);
1898 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
1899 size.stepwise.step_height);
1901 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
1902 w <= size.stepwise.max_width && h <= size.stepwise.max_height;
1903 w += size.stepwise.step_width, h += size.stepwise.step_height) {
1904 if (w == 0 || h == 0)
1908 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1909 pixelformat, w, h, template);
1912 results = g_list_prepend (results, tmp);
1914 GST_DEBUG_OBJECT (v4l2object->element,
1915 "done iterating stepwise frame sizes");
1916 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
1919 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
1920 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1921 size.stepwise.min_width);
1922 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1923 size.stepwise.min_height);
1924 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1925 size.stepwise.max_width);
1926 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1927 size.stepwise.max_height);
1929 w = MAX (size.stepwise.min_width, 1);
1930 h = MAX (size.stepwise.min_height, 1);
1931 maxw = MIN (size.stepwise.max_width, G_MAXINT);
1932 maxh = MIN (size.stepwise.max_height, G_MAXINT);
1935 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
1938 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
1939 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
1942 /* no point using the results list here, since there's only one struct */
1943 gst_caps_append_structure (ret, tmp);
1949 /* we use an intermediary list to store and then sort the results of the
1950 * probing because we can't make any assumptions about the order in which
1951 * the driver will give us the sizes, but we want the final caps to contain
1952 * the results starting with the highest resolution and having the lowest
1953 * resolution last, since order in caps matters for things like fixation. */
1954 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
1955 while (results != NULL) {
1956 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
1957 results = g_list_delete_link (results, results);
1960 if (gst_caps_is_empty (ret))
1961 goto enum_framesizes_no_results;
1966 enum_framesizes_failed:
1968 /* I don't see how this is actually an error */
1969 GST_DEBUG_OBJECT (v4l2object->element,
1970 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
1971 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
1972 goto default_frame_sizes;
1974 enum_framesizes_no_results:
1976 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
1977 * question doesn't actually support it yet */
1978 GST_DEBUG_OBJECT (v4l2object->element,
1979 "No results for pixelformat %" GST_FOURCC_FORMAT
1980 " enumerating frame sizes, trying fallback",
1981 GST_FOURCC_ARGS (pixelformat));
1982 goto default_frame_sizes;
1986 GST_WARNING_OBJECT (v4l2object->element,
1987 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
1988 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
1989 goto default_frame_sizes;
1991 default_frame_sizes:
1992 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
1994 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
1995 gboolean interlaced;
1997 /* This code is for Linux < 2.6.19 */
1999 max_w = max_h = GST_V4L2_MAX_SIZE;
2000 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2001 &min_h, &interlaced)) {
2002 GST_WARNING_OBJECT (v4l2object->element,
2003 "Could not probe minimum capture size for pixelformat %"
2004 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2006 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2007 &max_h, &interlaced)) {
2008 GST_WARNING_OBJECT (v4l2object->element,
2009 "Could not probe maximum capture size for pixelformat %"
2010 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2013 /* Since we can't get framerate directly, try to use the current norm */
2014 if (v4l2object->tv_norm && v4l2object->norms) {
2016 GstTunerNorm *norm = NULL;
2017 GstTunerNorm *current =
2018 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2020 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2021 norm = (GstTunerNorm *) norms->data;
2022 if (!strcmp (norm->label, current->label))
2025 /* If it's possible, set framerate to that (discrete) value */
2027 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2028 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2032 tmp = gst_structure_copy (template);
2034 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2037 /* if norm can't be used, copy the template framerate */
2038 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2043 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2045 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2048 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2050 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2052 gst_structure_set (tmp, "interlace-mode", G_TYPE_STRING,
2053 (interlaced ? "mixed" : "progressive"), NULL);
2054 gst_structure_set (tmp, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
2057 gst_caps_append_structure (ret, tmp);
2064 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2065 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced)
2067 struct v4l2_format fmt, prevfmt;
2072 g_return_val_if_fail (width != NULL, FALSE);
2073 g_return_val_if_fail (height != NULL, FALSE);
2075 GST_LOG_OBJECT (v4l2object->element,
2076 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2077 *width, *height, GST_FOURCC_ARGS (pixelformat));
2079 fd = v4l2object->video_fd;
2081 /* Some drivers are buggy and will modify the currently set format
2082 when processing VIDIOC_TRY_FMT, so we remember what is set at the
2083 minute, and will reset it when done. */
2084 prevfmt.type = v4l2object->type;
2085 prevfmt_valid = (v4l2_ioctl (fd, VIDIOC_G_FMT, &prevfmt) >= 0);
2087 /* get size delimiters */
2088 memset (&fmt, 0, sizeof (fmt));
2089 fmt.type = v4l2object->type;
2090 fmt.fmt.pix.width = *width;
2091 fmt.fmt.pix.height = *height;
2092 fmt.fmt.pix.pixelformat = pixelformat;
2093 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2095 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2096 if (r < 0 && errno == EINVAL) {
2097 /* try again with interlaced video */
2098 fmt.fmt.pix.width = *width;
2099 fmt.fmt.pix.height = *height;
2100 fmt.fmt.pix.pixelformat = pixelformat;
2101 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2102 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2106 /* The driver might not implement TRY_FMT, in which case we will try
2108 if (errno != ENOTTY)
2111 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
2112 be, because we're still probing */
2113 if (GST_V4L2_IS_ACTIVE (v4l2object))
2116 GST_LOG_OBJECT (v4l2object->element,
2117 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
2119 fmt.fmt.pix.width = *width;
2120 fmt.fmt.pix.height = *height;
2122 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2123 if (r < 0 && errno == EINVAL) {
2124 /* try again with progressive video */
2125 fmt.fmt.pix.width = *width;
2126 fmt.fmt.pix.height = *height;
2127 fmt.fmt.pix.pixelformat = pixelformat;
2128 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2129 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2136 GST_LOG_OBJECT (v4l2object->element,
2137 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2139 *width = fmt.fmt.pix.width;
2140 *height = fmt.fmt.pix.height;
2142 switch (fmt.fmt.pix.field) {
2143 case V4L2_FIELD_ANY:
2144 case V4L2_FIELD_NONE:
2145 *interlaced = FALSE;
2147 case V4L2_FIELD_INTERLACED:
2148 case V4L2_FIELD_INTERLACED_TB:
2149 case V4L2_FIELD_INTERLACED_BT:
2153 GST_WARNING_OBJECT (v4l2object->element,
2154 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u",
2155 GST_FOURCC_ARGS (pixelformat), *width, *height);
2160 v4l2_ioctl (fd, VIDIOC_S_FMT, &prevfmt);
2165 v4l2_ioctl (fd, VIDIOC_S_FMT, &prevfmt);
2170 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2174 GST_DEBUG_OBJECT (v4l2object->element, "initializing the capture system");
2176 GST_V4L2_CHECK_OPEN (v4l2object);
2177 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2179 /* find transport */
2180 mode = v4l2object->req_mode;
2182 if (v4l2object->vcap.capabilities & V4L2_CAP_READWRITE) {
2183 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2184 mode = GST_V4L2_IO_RW;
2185 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2186 goto method_not_supported;
2188 if (v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
2189 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2190 mode = GST_V4L2_IO_MMAP;
2191 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2192 goto method_not_supported;
2194 /* if still no transport selected, error out */
2195 if (mode == GST_V4L2_IO_AUTO)
2196 goto no_supported_capture_method;
2198 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2199 v4l2object->mode = mode;
2201 /* Map the buffers */
2202 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2204 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2205 goto buffer_pool_new_failed;
2207 GST_V4L2_SET_ACTIVE (v4l2object);
2212 buffer_pool_new_failed:
2214 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2215 (_("Could not map buffers from device '%s'"),
2216 v4l2object->videodev),
2217 ("Failed to create buffer pool: %s", g_strerror (errno)));
2220 method_not_supported:
2222 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2223 (_("The driver of device '%s' does not support the IO method %d"),
2224 v4l2object->videodev, mode), (NULL));
2227 no_supported_capture_method:
2229 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2230 (_("The driver of device '%s' does not support any known IO "
2231 "method."), v4l2object->videodev), (NULL));
2237 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps)
2239 gint fd = v4l2object->video_fd;
2240 struct v4l2_format format;
2241 struct v4l2_streamparm streamparm;
2242 enum v4l2_field field;
2243 guint32 pixelformat;
2244 struct v4l2_fmtdesc *fmtdesc;
2246 gint width, height, fps_n, fps_d, stride;
2248 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
2251 pixelformat = fmtdesc->pixelformat;
2252 width = GST_VIDEO_INFO_WIDTH (&info);
2253 height = GST_VIDEO_INFO_HEIGHT (&info);
2254 fps_n = GST_VIDEO_INFO_FPS_N (&info);
2255 fps_d = GST_VIDEO_INFO_FPS_D (&info);
2256 stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
2258 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
2259 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
2260 /* ideally we would differentiate between types of interlaced video
2261 * but there is not sufficient information in the caps..
2263 field = V4L2_FIELD_INTERLACED;
2265 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
2266 field = V4L2_FIELD_NONE;
2269 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
2270 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
2271 GST_FOURCC_ARGS (pixelformat), stride);
2273 GST_V4L2_CHECK_OPEN (v4l2object);
2274 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2276 /* MPEG-TS source cameras don't get their format set for some reason.
2277 * It looks wrong and we weren't able to track down the reason for that code
2278 * so it is disabled until someone who has an mpeg-ts camera complains...
2281 /* Only unconditionally accept mpegts for sources */
2282 if ((v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
2283 (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')))
2287 memset (&format, 0x00, sizeof (struct v4l2_format));
2288 format.type = v4l2object->type;
2290 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
2291 goto get_fmt_failed;
2293 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2294 "%" GST_FOURCC_FORMAT " bytesperline %d, colorspace %d",
2295 format.fmt.pix.width, format.fmt.pix.height,
2296 GST_FOURCC_ARGS (format.fmt.pix.pixelformat), format.fmt.pix.bytesperline,
2297 format.fmt.pix.colorspace);
2299 if (format.type != v4l2object->type ||
2300 format.fmt.pix.width != width ||
2301 format.fmt.pix.height != height ||
2302 format.fmt.pix.pixelformat != pixelformat ||
2303 format.fmt.pix.field != field || format.fmt.pix.bytesperline != stride) {
2304 /* something different, set the format */
2305 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2306 "%" GST_FOURCC_FORMAT " bytesperline %d", width, height,
2307 GST_FOURCC_ARGS (pixelformat), stride);
2309 format.type = v4l2object->type;
2310 format.fmt.pix.width = width;
2311 format.fmt.pix.height = height;
2312 format.fmt.pix.pixelformat = pixelformat;
2313 format.fmt.pix.field = field;
2314 /* try to ask our prefered stride */
2315 format.fmt.pix.bytesperline = stride;
2317 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
2318 goto set_fmt_failed;
2320 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2321 "%" GST_FOURCC_FORMAT " stride %d", format.fmt.pix.width,
2322 format.fmt.pix.height, GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2323 format.fmt.pix.bytesperline);
2325 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
2326 goto invalid_dimensions;
2328 if (format.fmt.pix.pixelformat != pixelformat)
2329 goto invalid_pixelformat;
2332 /* figure out the frame layout */
2333 v4l2object->bytesperline = format.fmt.pix.bytesperline;
2334 v4l2object->sizeimage = format.fmt.pix.sizeimage;
2336 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %u",
2337 v4l2object->sizeimage);
2339 /* Is there a reason we require the caller to always specify a framerate? */
2340 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
2343 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
2344 streamparm.type = v4l2object->type;
2346 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
2347 goto get_parm_failed;
2349 GST_VIDEO_INFO_FPS_N (&info) =
2350 streamparm.parm.capture.timeperframe.denominator;
2351 GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
2353 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2354 GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
2355 streamparm.parm.capture.timeperframe.denominator,
2356 streamparm.parm.capture.timeperframe.numerator);
2358 /* We used to skip frame rate setup if the camera was already setup
2359 * with the requested frame rate. This breaks some cameras though,
2360 * causing them to not output data (several models of Thinkpad cameras
2361 * have this problem at least).
2362 * So, don't skip. */
2363 GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
2365 /* We want to change the frame rate, so check whether we can. Some cheap USB
2366 * cameras don't have the capability */
2367 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
2368 GST_DEBUG_OBJECT (v4l2object->element,
2369 "Not setting framerate (not supported)");
2373 /* Note: V4L2 wants the frame interval, we have the frame rate */
2374 streamparm.parm.capture.timeperframe.numerator = fps_d;
2375 streamparm.parm.capture.timeperframe.denominator = fps_n;
2377 /* some cheap USB cam's won't accept any change */
2378 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
2379 goto set_parm_failed;
2381 /* get new values */
2382 fps_d = streamparm.parm.capture.timeperframe.numerator;
2383 fps_n = streamparm.parm.capture.timeperframe.denominator;
2385 GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
2388 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
2389 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
2393 /* if we have a framerate pre-calculate duration */
2394 if (fps_n > 0 && fps_d > 0) {
2395 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
2397 v4l2object->duration = GST_CLOCK_TIME_NONE;
2399 v4l2object->info = info;
2400 v4l2object->fmtdesc = fmtdesc;
2402 /* now configure ther pools */
2403 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
2411 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
2417 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2418 (_("Device '%s' does not support video capture"),
2419 v4l2object->videodev),
2420 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
2425 if (errno == EBUSY) {
2426 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, BUSY,
2427 (_("Device '%s' is busy"), v4l2object->videodev),
2428 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2429 GST_FOURCC_ARGS (pixelformat), width, height,
2430 g_strerror (errno)));
2432 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2433 (_("Device '%s' cannot capture at %dx%d"),
2434 v4l2object->videodev, width, height),
2435 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2436 GST_FOURCC_ARGS (pixelformat), width, height,
2437 g_strerror (errno)));
2443 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2444 (_("Device '%s' cannot capture at %dx%d"),
2445 v4l2object->videodev, width, height),
2446 ("Tried to capture at %dx%d, but device returned size %dx%d",
2447 width, height, format.fmt.pix.width, format.fmt.pix.height));
2450 invalid_pixelformat:
2452 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2453 (_("Device '%s' cannot capture in the specified format"),
2454 v4l2object->videodev),
2455 ("Tried to capture in %" GST_FOURCC_FORMAT
2456 ", but device returned format" " %" GST_FOURCC_FORMAT,
2457 GST_FOURCC_ARGS (pixelformat),
2458 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
2463 /* it's possible that this call is not supported */
2464 if (errno != EINVAL) {
2465 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2466 (_("Could not get parameters on device '%s'"),
2467 v4l2object->videodev), GST_ERROR_SYSTEM);
2473 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2474 (_("Video device did not accept new frame rate setting.")),
2480 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2481 (_("Video device could not create buffer pool.")), GST_ERROR_SYSTEM);
2487 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
2489 GST_LOG_OBJECT (v4l2object->element, "flush poll");
2490 gst_poll_set_flushing (v4l2object->poll, TRUE);
2496 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
2498 GST_LOG_OBJECT (v4l2object->element, "flush stop poll");
2499 gst_poll_set_flushing (v4l2object->poll, FALSE);
2505 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
2507 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
2509 if (!GST_V4L2_IS_OPEN (v4l2object))
2511 if (!GST_V4L2_IS_ACTIVE (v4l2object))
2514 if (v4l2object->pool) {
2515 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
2516 gst_buffer_pool_set_active (GST_BUFFER_POOL_CAST (v4l2object->pool), FALSE);
2517 gst_object_unref (v4l2object->pool);
2518 v4l2object->pool = NULL;
2521 GST_V4L2_SET_INACTIVE (v4l2object);
2528 gst_v4l2_object_copy (GstV4l2Object * v4l2object, GstBuffer * dest,
2531 const GstVideoFormatInfo *finfo = v4l2object->info.finfo;
2533 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
2534 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
2535 GstVideoFrame src_frame, dest_frame;
2537 GST_DEBUG_OBJECT (v4l2object->element, "copy video frame");
2539 /* we have raw video, use videoframe copy to get strides right */
2540 if (!gst_video_frame_map (&src_frame, &v4l2object->info, src, GST_MAP_READ))
2541 goto invalid_buffer;
2543 if (!gst_video_frame_map (&dest_frame, &v4l2object->info, dest,
2545 gst_video_frame_unmap (&src_frame);
2546 goto invalid_buffer;
2549 gst_video_frame_copy (&dest_frame, &src_frame);
2551 gst_video_frame_unmap (&src_frame);
2552 gst_video_frame_unmap (&dest_frame);
2556 GST_DEBUG_OBJECT (v4l2object->element, "copy raw bytes");
2557 gst_buffer_map (src, &map, GST_MAP_READ);
2558 gst_buffer_fill (dest, 0, map.data, map.size);
2559 gst_buffer_unmap (src, &map);
2560 gst_buffer_resize (dest, 0, map.size);
2562 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, v4l2object->element,
2563 "slow copy into buffer %p", dest);
2570 /* No Window available to put our image into */
2571 GST_WARNING_OBJECT (v4l2object->element, "could not map image");