3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
6 * gstv4l2object.c: base class for V4L2 elements
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Library General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. This library is distributed in the hope
12 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
32 #include <gudev/gudev.h>
35 #include "v4l2_calls.h"
36 #include "gstv4l2tuner.h"
38 #include "gstv4l2xoverlay.h"
40 #include "gstv4l2colorbalance.h"
42 #include "gst/gst-i18n-plugin.h"
44 #include <gst/video/video.h>
46 /* videodev2.h is not versioned and we can't easily check for the presence
47 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
48 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
49 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
50 #define V4L2_FIELD_INTERLACED_TB 8
51 #define V4L2_FIELD_INTERLACED_BT 9
54 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
55 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
56 #define GST_CAT_DEFAULT v4l2_debug
58 #define DEFAULT_PROP_DEVICE_NAME NULL
59 #define DEFAULT_PROP_DEVICE_FD -1
60 #define DEFAULT_PROP_FLAGS 0
61 #define DEFAULT_PROP_TV_NORM 0
62 #define DEFAULT_PROP_CHANNEL NULL
63 #define DEFAULT_PROP_FREQUENCY 0
64 #define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
69 V4L2_STD_OBJECT_PROPS,
72 G_LOCK_DEFINE_STATIC (probe_lock);
75 gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
77 GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
78 static GList *list = NULL;
83 list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
86 G_UNLOCK (probe_lock);
91 static gboolean init = FALSE;
92 static GList *devices = NULL;
96 gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
97 GList ** klass_devices)
99 GUdevClient *client = NULL;
104 gchar *device = devices->data;
105 devices = g_list_remove (devices, device);
109 GST_INFO ("Enumerating video4linux devices from udev");
110 client = g_udev_client_new (NULL);
112 GST_WARNING ("Failed to initialize gudev client");
116 item = g_udev_client_query_by_subsystem (client, "video4linux");
118 GUdevDevice *device = item->data;
119 gchar *devnode = g_strdup (g_udev_device_get_device_file (device));
120 gint api = g_udev_device_get_property_as_int (device, "ID_V4L_VERSION");
121 GST_INFO ("Found new device: %s, API: %d", devnode, api);
122 /* Append v4l2 devices only. If api is 0 probably v4l_id has
123 been stripped out of the current udev installation, append
127 ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
129 if ((api == 2 || api == 0)) {
130 devices = g_list_append (devices, devnode);
134 g_object_unref (device);
143 g_object_unref (client);
146 *klass_devices = devices;
150 #endif /* HAVE_GUDEV */
153 gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
154 GList ** klass_devices)
157 const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
161 gchar *device = devices->data;
162 devices = g_list_remove (devices, device);
167 * detect /dev entries
169 for (n = 0; n < 64; n++) {
170 for (base = 0; dev_base[base] != NULL; base++) {
172 gchar *device = g_strdup_printf ("%s%d",
177 * does the /dev/ entry exist at all?
179 if (stat (device, &s) == 0) {
181 * yes: is a device attached?
183 if (S_ISCHR (s.st_mode)) {
185 if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
189 devices = g_list_append (devices, device);
200 *klass_devices = devices;
206 gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
207 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
209 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
214 if (!gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices))
215 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
216 #else /* !HAVE_GUDEV */
217 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
218 #endif /* HAVE_GUDEV */
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
227 gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
228 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
230 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
231 gboolean ret = FALSE;
237 !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
238 #else /* !HAVE_GUDEV */
239 ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
240 #endif /* HAVE_GUDEV */
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
250 gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
253 GValue value = { 0 };
259 array = g_value_array_new (g_list_length (*klass_devices));
260 item = *klass_devices;
261 g_value_init (&value, G_TYPE_STRING);
263 gchar *device = item->data;
265 g_value_set_string (&value, device);
266 g_value_array_append (array, &value);
270 g_value_unset (&value);
276 gst_v4l2_probe_get_values (GstPropertyProbe * probe,
277 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
279 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
280 GValueArray *array = NULL;
284 array = gst_v4l2_class_list_devices (klass, klass_devices);
287 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
294 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
296 gst_v4l2_device_get_type (void)
298 static GType v4l2_device_type = 0;
300 if (v4l2_device_type == 0) {
301 static const GFlagsValue values[] = {
302 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
303 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
304 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
306 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
307 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
309 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
310 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
316 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
319 return v4l2_device_type;
322 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
324 gst_v4l2_tv_norm_get_type (void)
326 static GType v4l2_tv_norm = 0;
329 static const GEnumValue tv_norms[] = {
332 {V4L2_STD_NTSC, "NTSC", "NTSC"},
333 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
334 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
335 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
336 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
338 {V4L2_STD_PAL, "PAL", "PAL"},
339 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
340 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
341 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
342 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
343 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
344 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
345 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
346 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
347 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
348 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
349 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
350 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
351 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
352 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
354 {V4L2_STD_SECAM, "SECAM", "SECAM"},
355 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
356 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
357 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
358 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
359 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
360 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
361 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
362 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
363 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
368 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
374 #define GST_TYPE_V4L2_IO_MODE (gst_v4l2_io_mode_get_type ())
376 gst_v4l2_io_mode_get_type (void)
378 static GType v4l2_io_mode = 0;
381 static const GEnumValue io_modes[] = {
382 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
383 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
384 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
385 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
389 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
395 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
396 const char *default_device)
398 g_object_class_install_property (gobject_class, PROP_DEVICE,
399 g_param_spec_string ("device", "Device", "Device location",
400 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
401 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
402 g_param_spec_string ("device-name", "Device name",
403 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
404 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
405 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
406 g_param_spec_int ("device-fd", "File descriptor",
407 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
408 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
409 g_object_class_install_property (gobject_class, PROP_FLAGS,
410 g_param_spec_flags ("flags", "Flags", "Device type flags",
411 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
412 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
415 * GstV4l2Src:brightness
417 * Picture brightness, or more precisely, the black level
421 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
422 g_param_spec_int ("brightness", "Brightness",
423 "Picture brightness, or more precisely, the black level", G_MININT,
425 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
427 * GstV4l2Src:contrast
429 * Picture contrast or luma gain
433 g_object_class_install_property (gobject_class, PROP_CONTRAST,
434 g_param_spec_int ("contrast", "Contrast",
435 "Picture contrast or luma gain", G_MININT,
437 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
439 * GstV4l2Src:saturation
441 * Picture color saturation or chroma gain
445 g_object_class_install_property (gobject_class, PROP_SATURATION,
446 g_param_spec_int ("saturation", "Saturation",
447 "Picture color saturation or chroma gain", G_MININT,
449 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
453 * Hue or color balance
457 g_object_class_install_property (gobject_class, PROP_HUE,
458 g_param_spec_int ("hue", "Hue",
459 "Hue or color balance", G_MININT,
461 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
470 g_object_class_install_property (gobject_class, PROP_TV_NORM,
471 g_param_spec_enum ("norm", "TV norm",
473 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
474 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
481 g_object_class_install_property (gobject_class, PROP_IO_MODE,
482 g_param_spec_enum ("io-mode", "IO mode",
484 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
485 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
489 gst_v4l2_object_new (GstElement * element,
490 enum v4l2_buf_type type,
491 const char *default_device,
492 GstV4l2GetInOutFunction get_in_out_func,
493 GstV4l2SetInOutFunction set_in_out_func,
494 GstV4l2UpdateFpsFunction update_fps_func)
496 GstV4l2Object *v4l2object;
499 * some default values
501 v4l2object = g_new0 (GstV4l2Object, 1);
503 v4l2object->type = type;
504 v4l2object->formats = NULL;
506 v4l2object->element = element;
507 v4l2object->get_in_out_func = get_in_out_func;
508 v4l2object->set_in_out_func = set_in_out_func;
509 v4l2object->update_fps_func = update_fps_func;
511 v4l2object->video_fd = -1;
512 v4l2object->poll = gst_poll_new (TRUE);
513 v4l2object->active = FALSE;
514 v4l2object->videodev = g_strdup (default_device);
516 v4l2object->norms = NULL;
517 v4l2object->channels = NULL;
518 v4l2object->colors = NULL;
520 v4l2object->xwindow_id = 0;
525 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
529 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
531 g_return_if_fail (v4l2object != NULL);
533 if (v4l2object->videodev)
534 g_free (v4l2object->videodev);
536 if (v4l2object->poll)
537 gst_poll_free (v4l2object->poll);
539 if (v4l2object->channel)
540 g_free (v4l2object->channel);
542 if (v4l2object->formats) {
543 gst_v4l2_object_clear_format_list (v4l2object);
551 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
553 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
554 g_slist_free (v4l2object->formats);
555 v4l2object->formats = NULL;
561 gst_v4l2_object_prop_to_cid (guint prop_id)
566 case PROP_BRIGHTNESS:
567 cid = V4L2_CID_BRIGHTNESS;
570 cid = V4L2_CID_CONTRAST;
572 case PROP_SATURATION:
573 cid = V4L2_CID_SATURATION;
579 GST_WARNING ("unmapped property id: %d", prop_id);
586 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
587 guint prop_id, const GValue * value, GParamSpec * pspec)
591 g_free (v4l2object->videodev);
592 v4l2object->videodev = g_value_dup_string (value);
594 case PROP_BRIGHTNESS:
596 case PROP_SATURATION:
599 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
602 if (GST_V4L2_IS_OPEN (v4l2object)) {
603 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
610 v4l2object->tv_norm = g_value_get_enum (value);
614 if (GST_V4L2_IS_OPEN (v4l2object)) {
615 GstTuner *tuner = GST_TUNER (v4l2object->element);
616 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
617 (gchar *) g_value_get_string (value));
620 /* like gst_tuner_set_channel (tuner, channel)
621 without g_object_notify */
622 gst_v4l2_tuner_set_channel (v4l2object, channel);
625 g_free (v4l2object->channel);
626 v4l2object->channel = g_value_dup_string (value);
630 if (GST_V4L2_IS_OPEN (v4l2object)) {
631 GstTuner *tuner = GST_TUNER (v4l2object->element);
632 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
635 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
637 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
638 without g_object_notify */
639 gst_v4l2_tuner_set_frequency (v4l2object, channel,
640 g_value_get_ulong (value));
643 v4l2object->frequency = g_value_get_ulong (value);
648 v4l2object->req_mode = g_value_get_enum (value);
659 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
660 guint prop_id, GValue * value, GParamSpec * pspec)
664 g_value_set_string (value, v4l2object->videodev);
666 case PROP_DEVICE_NAME:
668 const guchar *new = NULL;
670 if (GST_V4L2_IS_OPEN (v4l2object)) {
671 new = v4l2object->vcap.card;
672 } else if (gst_v4l2_open (v4l2object)) {
673 new = v4l2object->vcap.card;
674 gst_v4l2_close (v4l2object);
676 g_value_set_string (value, (gchar *) new);
681 if (GST_V4L2_IS_OPEN (v4l2object))
682 g_value_set_int (value, v4l2object->video_fd);
684 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
691 if (GST_V4L2_IS_OPEN (v4l2object)) {
692 flags |= v4l2object->vcap.capabilities &
693 (V4L2_CAP_VIDEO_CAPTURE |
694 V4L2_CAP_VIDEO_OUTPUT |
695 V4L2_CAP_VIDEO_OVERLAY |
696 V4L2_CAP_VBI_CAPTURE |
697 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
699 g_value_set_flags (value, flags);
702 case PROP_BRIGHTNESS:
704 case PROP_SATURATION:
707 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
710 if (GST_V4L2_IS_OPEN (v4l2object)) {
712 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
713 g_value_set_int (value, v);
721 g_value_set_enum (value, v4l2object->tv_norm);
724 g_value_set_enum (value, v4l2object->req_mode);
734 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
736 GstTunerNorm *norm = NULL;
737 GstTunerChannel *channel = NULL;
740 if (!GST_IS_TUNER (v4l2object->element))
743 tuner = GST_TUNER (v4l2object->element);
745 if (v4l2object->tv_norm)
746 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
747 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=%d, norm=%p",
748 v4l2object->tv_norm, norm);
750 gst_tuner_set_norm (tuner, norm);
753 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
755 v4l2object->tv_norm =
756 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
757 gst_tuner_norm_changed (tuner, norm);
761 if (v4l2object->channel)
762 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
764 gst_tuner_set_channel (tuner, channel);
767 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
768 (v4l2object->element)));
770 g_free (v4l2object->channel);
771 v4l2object->channel = g_strdup (channel->label);
772 gst_tuner_channel_changed (tuner, channel);
777 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
778 if (v4l2object->frequency != 0) {
779 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
781 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
782 if (v4l2object->frequency == 0) {
784 gst_tuner_set_frequency (tuner, channel, 1000);
792 gst_v4l2_object_open (GstV4l2Object * v4l2object)
794 if (gst_v4l2_open (v4l2object))
795 gst_v4l2_set_defaults (v4l2object);
800 gst_v4l2_xoverlay_start (v4l2object);
807 gst_v4l2_object_close (GstV4l2Object * v4l2object)
810 gst_v4l2_xoverlay_stop (v4l2object);
813 if (!gst_v4l2_close (v4l2object))
816 if (v4l2object->formats) {
817 gst_v4l2_object_clear_format_list (v4l2object);
825 * common format / caps utilities:
833 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
834 /* from Linux 2.6.15 videodev2.h */
835 {V4L2_PIX_FMT_RGB332, TRUE},
836 {V4L2_PIX_FMT_RGB555, TRUE},
837 {V4L2_PIX_FMT_RGB565, TRUE},
838 {V4L2_PIX_FMT_RGB555X, TRUE},
839 {V4L2_PIX_FMT_RGB565X, TRUE},
840 {V4L2_PIX_FMT_BGR24, TRUE},
841 {V4L2_PIX_FMT_RGB24, TRUE},
842 {V4L2_PIX_FMT_BGR32, TRUE},
843 {V4L2_PIX_FMT_RGB32, TRUE},
844 {V4L2_PIX_FMT_GREY, TRUE},
845 {V4L2_PIX_FMT_YVU410, TRUE},
846 {V4L2_PIX_FMT_YVU420, TRUE},
847 {V4L2_PIX_FMT_YUYV, TRUE},
848 {V4L2_PIX_FMT_UYVY, TRUE},
849 {V4L2_PIX_FMT_YUV422P, TRUE},
850 {V4L2_PIX_FMT_YUV411P, TRUE},
851 {V4L2_PIX_FMT_Y41P, TRUE},
853 /* two planes -- one Y, one Cr + Cb interleaved */
854 {V4L2_PIX_FMT_NV12, TRUE},
855 {V4L2_PIX_FMT_NV21, TRUE},
857 /* The following formats are not defined in the V4L2 specification */
858 {V4L2_PIX_FMT_YUV410, TRUE},
859 {V4L2_PIX_FMT_YUV420, TRUE},
860 {V4L2_PIX_FMT_YYUV, TRUE},
861 {V4L2_PIX_FMT_HI240, TRUE},
863 /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
864 #ifdef V4L2_PIX_FMT_SBGGR8
865 {V4L2_PIX_FMT_SBGGR8, TRUE},
868 /* compressed formats */
869 {V4L2_PIX_FMT_MJPEG, TRUE},
870 {V4L2_PIX_FMT_JPEG, TRUE},
871 #ifdef V4L2_PIX_FMT_PJPG
872 {V4L2_PIX_FMT_PJPG, TRUE},
874 {V4L2_PIX_FMT_DV, TRUE},
875 {V4L2_PIX_FMT_MPEG, FALSE},
877 /* Vendor-specific formats */
878 {V4L2_PIX_FMT_WNVA, TRUE},
880 #ifdef V4L2_PIX_FMT_SN9C10X
881 {V4L2_PIX_FMT_SN9C10X, TRUE},
883 #ifdef V4L2_PIX_FMT_PWC1
884 {V4L2_PIX_FMT_PWC1, TRUE},
886 #ifdef V4L2_PIX_FMT_PWC2
887 {V4L2_PIX_FMT_PWC2, TRUE},
889 #ifdef V4L2_PIX_FMT_YVYU
890 {V4L2_PIX_FMT_YVYU, TRUE},
894 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
897 static struct v4l2_fmtdesc *
898 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
901 struct v4l2_fmtdesc *fmt;
907 walk = gst_v4l2_object_get_format_list (v4l2object);
909 fmt = (struct v4l2_fmtdesc *) walk->data;
910 if (fmt->pixelformat == fourcc)
912 /* special case for jpeg */
913 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
914 fmt->pixelformat == V4L2_PIX_FMT_JPEG
915 #ifdef V4L2_PIX_FMT_PJPG
916 || fmt->pixelformat == V4L2_PIX_FMT_PJPG
919 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG
920 #ifdef V4L2_PIX_FMT_PJPG
921 || fourcc == V4L2_PIX_FMT_PJPG
927 walk = g_slist_next (walk);
935 /* complete made up ranking, the values themselves are meaningless */
936 #define YUV_BASE_RANK 1000
937 #define JPEG_BASE_RANK 500
938 #define DV_BASE_RANK 200
939 #define RGB_BASE_RANK 100
940 #define YUV_ODD_BASE_RANK 50
941 #define RGB_ODD_BASE_RANK 25
942 #define BAYER_BASE_RANK 15
943 #define S910_BASE_RANK 10
944 #define GREY_BASE_RANK 5
945 #define PWC_BASE_RANK 1
947 /* This flag is already used by libv4l2 although
948 * it was added to the Linux kernel in 2.6.32
950 #ifndef V4L2_FMT_FLAG_EMULATED
951 #define V4L2_FMT_FLAG_EMULATED 0x0002
955 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
957 guint32 fourcc = fmt->pixelformat;
958 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
962 case V4L2_PIX_FMT_MJPEG:
963 #ifdef V4L2_PIX_FMT_PJPG
964 case V4L2_PIX_FMT_PJPG:
965 rank = JPEG_BASE_RANK;
968 case V4L2_PIX_FMT_JPEG:
969 rank = JPEG_BASE_RANK + 1;
971 case V4L2_PIX_FMT_MPEG: /* MPEG */
972 rank = JPEG_BASE_RANK + 2;
975 case V4L2_PIX_FMT_RGB332:
976 case V4L2_PIX_FMT_RGB555:
977 case V4L2_PIX_FMT_RGB555X:
978 case V4L2_PIX_FMT_RGB565:
979 case V4L2_PIX_FMT_RGB565X:
980 rank = RGB_ODD_BASE_RANK;
983 case V4L2_PIX_FMT_RGB24:
984 case V4L2_PIX_FMT_BGR24:
985 rank = RGB_BASE_RANK - 1;
988 case V4L2_PIX_FMT_RGB32:
989 case V4L2_PIX_FMT_BGR32:
990 rank = RGB_BASE_RANK;
993 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
994 rank = GREY_BASE_RANK;
997 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
998 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
999 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1000 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1001 rank = YUV_ODD_BASE_RANK;
1004 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1005 rank = YUV_BASE_RANK + 3;
1007 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1008 rank = YUV_BASE_RANK + 2;
1010 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1011 rank = YUV_BASE_RANK + 7;
1013 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1014 rank = YUV_BASE_RANK + 10;
1016 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1017 rank = YUV_BASE_RANK + 6;
1019 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1020 rank = YUV_BASE_RANK + 9;
1022 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1023 rank = YUV_BASE_RANK + 5;
1025 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1026 rank = YUV_BASE_RANK + 4;
1028 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1029 rank = YUV_BASE_RANK + 8;
1032 case V4L2_PIX_FMT_DV:
1033 rank = DV_BASE_RANK;
1036 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1040 #ifdef V4L2_PIX_FMT_SBGGR8
1041 case V4L2_PIX_FMT_SBGGR8:
1042 rank = BAYER_BASE_RANK;
1046 #ifdef V4L2_PIX_FMT_SN9C10X
1047 case V4L2_PIX_FMT_SN9C10X:
1048 rank = S910_BASE_RANK;
1052 #ifdef V4L2_PIX_FMT_PWC1
1053 case V4L2_PIX_FMT_PWC1:
1054 rank = PWC_BASE_RANK;
1057 #ifdef V4L2_PIX_FMT_PWC2
1058 case V4L2_PIX_FMT_PWC2:
1059 rank = PWC_BASE_RANK;
1068 /* All ranks are below 1<<15 so a shift by 15
1069 * will a) make all non-emulated formats larger
1070 * than emulated and b) will not overflow
1081 format_cmp_func (gconstpointer a, gconstpointer b)
1083 const struct v4l2_fmtdesc *fa = a;
1084 const struct v4l2_fmtdesc *fb = b;
1086 if (fa->pixelformat == fb->pixelformat)
1089 return gst_v4l2_object_format_get_rank (fb) -
1090 gst_v4l2_object_format_get_rank (fa);
1093 /******************************************************
1094 * gst_v4l2_object_fill_format_list():
1095 * create list of supported capture formats
1096 * return value: TRUE on success, FALSE on error
1097 ******************************************************/
1099 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object)
1102 struct v4l2_fmtdesc *format;
1104 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1106 /* format enumeration */
1108 format = g_new0 (struct v4l2_fmtdesc, 1);
1111 format->type = v4l2object->type;
1113 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1114 if (errno == EINVAL) {
1116 break; /* end of enumeration */
1122 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1123 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1124 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1125 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1126 format->description);
1127 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1128 GST_FOURCC_ARGS (format->pixelformat));
1130 /* sort formats according to our preference; we do this, because caps
1131 * are probed in the order the formats are in the list, and the order of
1132 * formats in the final probed caps matters for things like fixation */
1133 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1134 (GCompareFunc) format_cmp_func);
1137 #ifndef GST_DISABLE_GST_DEBUG
1141 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1142 for (l = v4l2object->formats; l != NULL; l = l->next) {
1145 GST_INFO_OBJECT (v4l2object->element,
1146 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1147 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1157 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1158 (_("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)));
1165 * Get the list of supported capture formats, a list of
1166 * <code>struct v4l2_fmtdesc</code>.
1169 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1171 if (!v4l2object->formats)
1172 gst_v4l2_object_fill_format_list (v4l2object);
1173 return v4l2object->formats;
1178 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1180 GstStructure *structure = NULL;
1183 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1184 #ifdef V4L2_PIX_FMT_PJPG
1185 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1187 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1188 structure = gst_structure_new ("image/jpeg", NULL);
1190 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1191 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1192 /* FIXME: get correct fourccs here */
1194 case V4L2_PIX_FMT_RGB332:
1195 case V4L2_PIX_FMT_RGB555X:
1196 case V4L2_PIX_FMT_RGB565X:
1197 /* FIXME: get correct fourccs here */
1199 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1200 case V4L2_PIX_FMT_RGB555:
1201 case V4L2_PIX_FMT_RGB565:
1202 case V4L2_PIX_FMT_RGB24:
1203 case V4L2_PIX_FMT_BGR24:
1204 case V4L2_PIX_FMT_RGB32:
1205 case V4L2_PIX_FMT_BGR32:
1206 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1207 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1208 case V4L2_PIX_FMT_YVU410:
1209 case V4L2_PIX_FMT_YUV410:
1210 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1211 case V4L2_PIX_FMT_YUYV:
1212 case V4L2_PIX_FMT_YVU420:
1213 case V4L2_PIX_FMT_UYVY:
1215 case V4L2_PIX_FMT_Y41P:
1217 case V4L2_PIX_FMT_YUV422P:
1218 #ifdef V4L2_PIX_FMT_YVYU
1219 case V4L2_PIX_FMT_YVYU:
1221 case V4L2_PIX_FMT_YUV411P:{
1222 GstVideoFormat format;
1225 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1226 format = GST_VIDEO_FORMAT_GRAY8;
1228 case V4L2_PIX_FMT_RGB555:
1229 format = GST_VIDEO_FORMAT_RGB15;
1231 case V4L2_PIX_FMT_RGB565:
1232 format = GST_VIDEO_FORMAT_RGB16;
1234 case V4L2_PIX_FMT_RGB24:
1235 format = GST_VIDEO_FORMAT_RGB;
1237 case V4L2_PIX_FMT_BGR24:
1238 format = GST_VIDEO_FORMAT_BGR;
1240 case V4L2_PIX_FMT_RGB32:
1241 format = GST_VIDEO_FORMAT_RGBx;
1243 case V4L2_PIX_FMT_BGR32:
1244 format = GST_VIDEO_FORMAT_BGRx;
1246 case V4L2_PIX_FMT_NV12:
1247 format = GST_VIDEO_FORMAT_NV12;
1249 case V4L2_PIX_FMT_NV21:
1250 format = GST_VIDEO_FORMAT_NV21;
1252 case V4L2_PIX_FMT_YVU410:
1253 format = GST_VIDEO_FORMAT_YVU9;
1255 case V4L2_PIX_FMT_YUV410:
1256 format = GST_VIDEO_FORMAT_YUV9;
1258 case V4L2_PIX_FMT_YUV420:
1259 format = GST_VIDEO_FORMAT_I420;
1261 case V4L2_PIX_FMT_YUYV:
1262 format = GST_VIDEO_FORMAT_YUY2;
1264 case V4L2_PIX_FMT_YVU420:
1265 format = GST_VIDEO_FORMAT_YV12;
1267 case V4L2_PIX_FMT_UYVY:
1268 format = GST_VIDEO_FORMAT_UYVY;
1271 case V4L2_PIX_FMT_Y41P:
1272 format = GST_VIDEO_FORMAT_Y41P;
1275 case V4L2_PIX_FMT_YUV411P:
1276 format = GST_VIDEO_FORMAT_Y41B;
1278 case V4L2_PIX_FMT_YUV422P:
1279 format = GST_VIDEO_FORMAT_Y42B;
1281 #ifdef V4L2_PIX_FMT_YVYU
1282 case V4L2_PIX_FMT_YVYU:
1283 format = GST_VIDEO_FORMAT_YVYU;
1287 g_assert_not_reached ();
1290 structure = gst_structure_new ("video/x-raw",
1291 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1294 case V4L2_PIX_FMT_DV:
1296 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1299 case V4L2_PIX_FMT_MPEG: /* MPEG */
1300 structure = gst_structure_new ("video/mpegts", NULL);
1302 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1304 #ifdef V4L2_PIX_FMT_SBGGR8
1305 case V4L2_PIX_FMT_SBGGR8:
1306 structure = gst_structure_new ("video/x-raw-bayer", NULL);
1309 #ifdef V4L2_PIX_FMT_SN9C10X
1310 case V4L2_PIX_FMT_SN9C10X:
1311 structure = gst_structure_new ("video/x-sonix", NULL);
1314 #ifdef V4L2_PIX_FMT_PWC1
1315 case V4L2_PIX_FMT_PWC1:
1316 structure = gst_structure_new ("video/x-pwc1", NULL);
1319 #ifdef V4L2_PIX_FMT_PWC2
1320 case V4L2_PIX_FMT_PWC2:
1321 structure = gst_structure_new ("video/x-pwc2", NULL);
1325 GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
1326 fourcc, GST_FOURCC_ARGS (fourcc));
1336 gst_v4l2_object_get_all_caps (void)
1338 static GstCaps *caps = NULL;
1341 GstStructure *structure;
1345 caps = gst_caps_new_empty ();
1346 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1348 gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
1350 if (gst_v4l2_formats[i].dimensions) {
1351 gst_structure_set (structure,
1352 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1353 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1354 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
1356 gst_caps_append_structure (caps, structure);
1361 return gst_caps_ref (caps);
1365 /* collect data for the given caps
1366 * @caps: given input caps
1367 * @format: location for the v4l format
1368 * @w/@h: location for width and height
1369 * @fps_n/@fps_d: location for framerate
1370 * @size: location for expected size of the frame or 0 if unknown
1373 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1374 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1376 GstStructure *structure;
1378 const gchar *mimetype;
1379 struct v4l2_fmtdesc *fmt;
1381 /* default unknown values */
1384 structure = gst_caps_get_structure (caps, 0);
1386 mimetype = gst_structure_get_name (structure);
1388 if (g_str_equal (mimetype, "video/x-raw")) {
1389 /* raw caps, parse into video info */
1390 if (!gst_video_info_from_caps (info, caps))
1391 goto invalid_format;
1393 switch (GST_VIDEO_INFO_FORMAT (info)) {
1394 case GST_VIDEO_FORMAT_I420:
1395 fourcc = V4L2_PIX_FMT_YUV420;
1397 case GST_VIDEO_FORMAT_YUY2:
1398 fourcc = V4L2_PIX_FMT_YUYV;
1401 case GST_VIDEO_FORMAT_Y41P:
1402 fourcc = V4L2_PIX_FMT_Y41P;
1405 case GST_VIDEO_FORMAT_UYVY:
1406 fourcc = V4L2_PIX_FMT_UYVY;
1408 case GST_VIDEO_FORMAT_YV12:
1409 fourcc = V4L2_PIX_FMT_YVU420;
1411 case GST_VIDEO_FORMAT_Y41B:
1412 fourcc = V4L2_PIX_FMT_YUV411P;
1414 case GST_VIDEO_FORMAT_Y42B:
1415 fourcc = V4L2_PIX_FMT_YUV422P;
1417 case GST_VIDEO_FORMAT_NV12:
1418 fourcc = V4L2_PIX_FMT_NV12;
1420 case GST_VIDEO_FORMAT_NV21:
1421 fourcc = V4L2_PIX_FMT_NV21;
1423 #ifdef V4L2_PIX_FMT_YVYU
1424 case GST_VIDEO_FORMAT_YVYU:
1425 fourcc = V4L2_PIX_FMT_YVYU;
1428 case GST_VIDEO_FORMAT_RGB15:
1429 fourcc = V4L2_PIX_FMT_RGB555;
1431 case GST_VIDEO_FORMAT_RGB16:
1432 fourcc = V4L2_PIX_FMT_RGB565;
1434 case GST_VIDEO_FORMAT_RGB:
1435 fourcc = V4L2_PIX_FMT_RGB24;
1437 case GST_VIDEO_FORMAT_BGR:
1438 fourcc = V4L2_PIX_FMT_BGR24;
1440 case GST_VIDEO_FORMAT_RGBx:
1441 case GST_VIDEO_FORMAT_RGBA:
1442 fourcc = V4L2_PIX_FMT_RGB32;
1444 case GST_VIDEO_FORMAT_BGRx:
1445 case GST_VIDEO_FORMAT_BGRA:
1446 fourcc = V4L2_PIX_FMT_BGR32;
1448 case GST_VIDEO_FORMAT_GRAY8:
1449 fourcc = V4L2_PIX_FMT_GREY;
1454 gboolean dimensions = TRUE;
1456 /* no video caps, construct videoinfo ourselves */
1457 gst_video_info_init (info);
1459 if (g_str_equal (mimetype, "video/mpegts")) {
1460 fourcc = V4L2_PIX_FMT_MPEG;
1462 } else if (g_str_equal (mimetype, "video/x-dv")) {
1463 fourcc = V4L2_PIX_FMT_DV;
1464 } else if (g_str_equal (mimetype, "image/jpeg")) {
1465 fourcc = V4L2_PIX_FMT_JPEG;
1466 #ifdef V4L2_PIX_FMT_SBGGR8
1467 } else if (g_str_equal (mimetype, "video/x-raw-bayer")) {
1468 fourcc = V4L2_PIX_FMT_SBGGR8;
1470 #ifdef V4L2_PIX_FMT_SN9C10X
1471 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1472 fourcc = V4L2_PIX_FMT_SN9C10X;
1474 #ifdef V4L2_PIX_FMT_PWC1
1475 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1476 fourcc = V4L2_PIX_FMT_PWC1;
1478 #ifdef V4L2_PIX_FMT_PWC2
1479 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1480 fourcc = V4L2_PIX_FMT_PWC2;
1485 gboolean interlaced;
1487 if (!gst_structure_get_int (structure, "width", &info->width))
1490 if (!gst_structure_get_int (structure, "height", &info->height))
1493 if (!gst_structure_get_boolean (structure, "interlaced", &interlaced))
1496 info->flags |= GST_VIDEO_FLAG_INTERLACED;
1498 if (!gst_structure_get_fraction (structure, "framerate", &info->fps_n,
1505 goto unhandled_format;
1507 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1509 goto unsupported_format;
1518 GST_DEBUG_OBJECT (v4l2object, "no width");
1523 GST_DEBUG_OBJECT (v4l2object, "no height");
1528 GST_DEBUG_OBJECT (v4l2object, "no framerate");
1533 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1538 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1543 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1550 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1551 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced);
1554 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1555 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1556 static GstStructure *
1557 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1558 guint32 pixelformat,
1559 guint32 width, guint32 height, const GstStructure * template)
1561 gint fd = v4l2object->video_fd;
1562 struct v4l2_frmivalenum ival;
1565 GValue rates = { 0, };
1566 gboolean interlaced;
1567 gint int_width = width;
1568 gint int_height = height;
1570 /* interlaced detection using VIDIOC_TRY/S_FMT */
1571 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat,
1572 &int_width, &int_height, &interlaced))
1575 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1577 ival.pixel_format = pixelformat;
1579 ival.height = height;
1581 GST_LOG_OBJECT (v4l2object->element,
1582 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1583 GST_FOURCC_ARGS (pixelformat));
1585 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1586 * fraction to get framerate */
1587 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1588 goto enum_frameintervals_failed;
1590 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1591 GValue rate = { 0, };
1593 g_value_init (&rates, GST_TYPE_LIST);
1594 g_value_init (&rate, GST_TYPE_FRACTION);
1597 num = ival.discrete.numerator;
1598 denom = ival.discrete.denominator;
1600 if (num > G_MAXINT || denom > G_MAXINT) {
1601 /* let us hope we don't get here... */
1606 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1609 /* swap to get the framerate */
1610 gst_value_set_fraction (&rate, denom, num);
1611 gst_value_list_append_value (&rates, &rate);
1614 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1615 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1616 GValue min = { 0, };
1617 GValue step = { 0, };
1618 GValue max = { 0, };
1619 gboolean added = FALSE;
1620 guint32 minnum, mindenom;
1621 guint32 maxnum, maxdenom;
1623 g_value_init (&rates, GST_TYPE_LIST);
1625 g_value_init (&min, GST_TYPE_FRACTION);
1626 g_value_init (&step, GST_TYPE_FRACTION);
1627 g_value_init (&max, GST_TYPE_FRACTION);
1630 minnum = ival.stepwise.min.numerator;
1631 mindenom = ival.stepwise.min.denominator;
1632 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1636 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1638 gst_value_set_fraction (&min, minnum, mindenom);
1641 maxnum = ival.stepwise.max.numerator;
1642 maxdenom = ival.stepwise.max.denominator;
1643 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1648 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1650 gst_value_set_fraction (&max, maxnum, maxdenom);
1653 num = ival.stepwise.step.numerator;
1654 denom = ival.stepwise.step.denominator;
1655 if (num > G_MAXINT || denom > G_MAXINT) {
1660 if (num == 0 || denom == 0) {
1661 /* in this case we have a wrong fraction or no step, set the step to max
1662 * so that we only add the min value in the loop below */
1667 /* since we only have gst_value_fraction_subtract and not add, negate the
1669 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1671 gst_value_set_fraction (&step, -num, denom);
1673 while (gst_value_compare (&min, &max) <= 0) {
1674 GValue rate = { 0, };
1676 num = gst_value_get_fraction_numerator (&min);
1677 denom = gst_value_get_fraction_denominator (&min);
1678 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1681 /* invert to get the framerate */
1682 g_value_init (&rate, GST_TYPE_FRACTION);
1683 gst_value_set_fraction (&rate, denom, num);
1684 gst_value_list_append_value (&rates, &rate);
1687 /* we're actually adding because step was negated above. This is because
1688 * there is no _add function... */
1689 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1690 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1695 /* no range was added, leave the default range from the template */
1696 GST_WARNING_OBJECT (v4l2object->element,
1697 "no range added, leaving default");
1698 g_value_unset (&rates);
1700 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1701 guint32 maxnum, maxdenom;
1703 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1705 num = ival.stepwise.min.numerator;
1706 denom = ival.stepwise.min.denominator;
1707 if (num > G_MAXINT || denom > G_MAXINT) {
1712 maxnum = ival.stepwise.max.numerator;
1713 maxdenom = ival.stepwise.max.denominator;
1714 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1719 GST_LOG_OBJECT (v4l2object->element,
1720 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
1723 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1729 s = gst_structure_copy (template);
1730 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
1731 "height", G_TYPE_INT, (gint) height,
1732 "interlaced", G_TYPE_BOOLEAN, interlaced, NULL);
1734 if (G_IS_VALUE (&rates)) {
1735 /* only change the framerate on the template when we have a valid probed new
1737 gst_structure_set_value (s, "framerate", &rates);
1738 g_value_unset (&rates);
1740 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
1746 enum_frameintervals_failed:
1748 GST_DEBUG_OBJECT (v4l2object->element,
1749 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
1750 GST_FOURCC_ARGS (pixelformat), width, height);
1755 /* I don't see how this is actually an error, we ignore the format then */
1756 GST_WARNING_OBJECT (v4l2object->element,
1757 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
1758 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
1762 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
1764 #ifdef VIDIOC_ENUM_FRAMESIZES
1766 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
1770 gst_structure_get_int (s1, "width", &w1);
1771 gst_structure_get_int (s1, "height", &h1);
1772 gst_structure_get_int (s2, "width", &w2);
1773 gst_structure_get_int (s2, "height", &h2);
1775 /* I think it's safe to assume that this won't overflow for a while */
1776 return ((w2 * h2) - (w1 * h1));
1781 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
1782 guint32 pixelformat, const GstStructure * template)
1784 GstCaps *ret = gst_caps_new_empty ();
1787 #ifdef VIDIOC_ENUM_FRAMESIZES
1788 gint fd = v4l2object->video_fd;
1789 struct v4l2_frmsizeenum size;
1790 GList *results = NULL;
1793 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1794 return gst_caps_new_simple ("video/mpegts", NULL);
1796 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
1798 size.pixel_format = pixelformat;
1800 GST_DEBUG_OBJECT (v4l2object->element, "Enumerating frame sizes");
1802 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
1803 goto enum_framesizes_failed;
1805 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1807 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
1808 size.discrete.width, size.discrete.height);
1810 w = MIN (size.discrete.width, G_MAXINT);
1811 h = MIN (size.discrete.height, G_MAXINT);
1815 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1816 pixelformat, w, h, template);
1819 results = g_list_prepend (results, tmp);
1823 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
1824 GST_DEBUG_OBJECT (v4l2object->element,
1825 "done iterating discrete frame sizes");
1826 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
1827 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
1828 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1829 size.stepwise.min_width);
1830 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1831 size.stepwise.min_height);
1832 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1833 size.stepwise.max_width);
1834 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1835 size.stepwise.max_height);
1836 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
1837 size.stepwise.step_width);
1838 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
1839 size.stepwise.step_height);
1841 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
1842 w < size.stepwise.max_width && h < size.stepwise.max_height;
1843 w += size.stepwise.step_width, h += size.stepwise.step_height) {
1844 if (w == 0 || h == 0)
1848 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1849 pixelformat, w, h, template);
1852 results = g_list_prepend (results, tmp);
1854 GST_DEBUG_OBJECT (v4l2object->element,
1855 "done iterating stepwise frame sizes");
1856 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
1859 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
1860 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1861 size.stepwise.min_width);
1862 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1863 size.stepwise.min_height);
1864 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1865 size.stepwise.max_width);
1866 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1867 size.stepwise.max_height);
1869 w = MAX (size.stepwise.min_width, 1);
1870 h = MAX (size.stepwise.min_height, 1);
1871 maxw = MIN (size.stepwise.max_width, G_MAXINT);
1872 maxh = MIN (size.stepwise.max_height, G_MAXINT);
1875 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
1878 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
1879 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
1882 /* no point using the results list here, since there's only one struct */
1883 gst_caps_append_structure (ret, tmp);
1889 /* we use an intermediary list to store and then sort the results of the
1890 * probing because we can't make any assumptions about the order in which
1891 * the driver will give us the sizes, but we want the final caps to contain
1892 * the results starting with the highest resolution and having the lowest
1893 * resolution last, since order in caps matters for things like fixation. */
1894 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
1895 while (results != NULL) {
1896 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
1897 results = g_list_delete_link (results, results);
1900 if (gst_caps_is_empty (ret))
1901 goto enum_framesizes_no_results;
1906 enum_framesizes_failed:
1908 /* I don't see how this is actually an error */
1909 GST_DEBUG_OBJECT (v4l2object->element,
1910 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
1911 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
1912 goto default_frame_sizes;
1914 enum_framesizes_no_results:
1916 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
1917 * question doesn't actually support it yet */
1918 GST_DEBUG_OBJECT (v4l2object->element,
1919 "No results for pixelformat %" GST_FOURCC_FORMAT
1920 " enumerating frame sizes, trying fallback",
1921 GST_FOURCC_ARGS (pixelformat));
1922 goto default_frame_sizes;
1926 GST_WARNING_OBJECT (v4l2object->element,
1927 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
1928 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
1929 goto default_frame_sizes;
1931 default_frame_sizes:
1932 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
1934 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
1935 gboolean interlaced;
1937 /* This code is for Linux < 2.6.19 */
1939 max_w = max_h = GST_V4L2_MAX_SIZE;
1940 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
1941 &min_h, &interlaced)) {
1942 GST_WARNING_OBJECT (v4l2object->element,
1943 "Could not probe minimum capture size for pixelformat %"
1944 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1946 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
1947 &max_h, &interlaced)) {
1948 GST_WARNING_OBJECT (v4l2object->element,
1949 "Could not probe maximum capture size for pixelformat %"
1950 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1953 /* Since we can't get framerate directly, try to use the current norm */
1954 if (v4l2object->tv_norm && v4l2object->norms) {
1956 GstTunerNorm *norm = NULL;
1957 GstTunerNorm *current =
1958 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
1960 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
1961 norm = (GstTunerNorm *) norms->data;
1962 if (!strcmp (norm->label, current->label))
1965 /* If it's possible, set framerate to that (discrete) value */
1967 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
1968 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
1972 tmp = gst_structure_copy (template);
1974 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
1977 /* if norm can't be used, copy the template framerate */
1978 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
1983 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
1985 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
1988 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
1990 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
1992 gst_structure_set (tmp, "interlaced", G_TYPE_BOOLEAN, interlaced, NULL);
1994 gst_caps_append_structure (ret, tmp);
2001 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2002 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced)
2004 struct v4l2_format fmt;
2008 g_return_val_if_fail (width != NULL, FALSE);
2009 g_return_val_if_fail (height != NULL, FALSE);
2011 GST_LOG_OBJECT (v4l2object->element,
2012 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2013 *width, *height, GST_FOURCC_ARGS (pixelformat));
2015 fd = v4l2object->video_fd;
2017 /* get size delimiters */
2018 memset (&fmt, 0, sizeof (fmt));
2019 fmt.type = v4l2object->type;
2020 fmt.fmt.pix.width = *width;
2021 fmt.fmt.pix.height = *height;
2022 fmt.fmt.pix.pixelformat = pixelformat;
2023 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2025 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2026 if (r < 0 && errno == EINVAL) {
2027 /* try again with interlaced video */
2028 fmt.fmt.pix.width = *width;
2029 fmt.fmt.pix.height = *height;
2030 fmt.fmt.pix.pixelformat = pixelformat;
2031 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2032 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2036 /* The driver might not implement TRY_FMT, in which case we will try
2038 if (errno != ENOTTY)
2041 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
2042 be, because we're still probing */
2043 if (GST_V4L2_IS_ACTIVE (v4l2object))
2046 GST_LOG_OBJECT (v4l2object->element,
2047 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
2049 fmt.fmt.pix.width = *width;
2050 fmt.fmt.pix.height = *height;
2052 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2053 if (r < 0 && errno == EINVAL) {
2054 /* try again with progressive video */
2055 fmt.fmt.pix.width = *width;
2056 fmt.fmt.pix.height = *height;
2057 fmt.fmt.pix.pixelformat = pixelformat;
2058 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2059 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2066 GST_LOG_OBJECT (v4l2object->element,
2067 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2069 *width = fmt.fmt.pix.width;
2070 *height = fmt.fmt.pix.height;
2072 switch (fmt.fmt.pix.field) {
2073 case V4L2_FIELD_ANY:
2074 case V4L2_FIELD_NONE:
2075 *interlaced = FALSE;
2077 case V4L2_FIELD_INTERLACED:
2078 case V4L2_FIELD_INTERLACED_TB:
2079 case V4L2_FIELD_INTERLACED_BT:
2083 GST_WARNING_OBJECT (v4l2object->element,
2084 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u",
2085 GST_FOURCC_ARGS (pixelformat), *width, *height);
2093 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2097 GST_DEBUG_OBJECT (v4l2object->element, "initializing the capture system");
2099 GST_V4L2_CHECK_OPEN (v4l2object);
2100 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2102 /* find transport */
2103 mode = v4l2object->req_mode;
2105 if (v4l2object->vcap.capabilities & V4L2_CAP_READWRITE) {
2106 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2107 mode = GST_V4L2_IO_RW;
2108 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2109 goto method_not_supported;
2111 if (v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
2112 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2113 mode = GST_V4L2_IO_MMAP;
2114 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2115 goto method_not_supported;
2117 /* if still no transport selected, error out */
2118 if (mode == GST_V4L2_IO_AUTO)
2119 goto no_supported_capture_method;
2121 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2122 v4l2object->mode = mode;
2124 /* Map the buffers */
2125 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2127 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object)))
2128 goto buffer_pool_new_failed;
2130 GST_V4L2_SET_ACTIVE (v4l2object);
2135 buffer_pool_new_failed:
2137 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2138 (_("Could not map buffers from device '%s'"),
2139 v4l2object->videodev),
2140 ("Failed to create buffer pool: %s", g_strerror (errno)));
2143 method_not_supported:
2145 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2146 (_("The driver of device '%s' does not support the IO method %d"),
2147 v4l2object->videodev, mode), (NULL));
2150 no_supported_capture_method:
2152 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2153 (_("The driver of device '%s' does not support any known IO "
2154 "method."), v4l2object->videodev), (NULL));
2160 /* Note about fraction simplification
2161 * * n1/d1 == n2/d2 is also written as n1 == ( n2 * d1 ) / d2
2163 #define fractions_are_equal(n1,d1,n2,d2) ((n1) == gst_util_uint64_scale_int((n2), (d1), (d2)))
2166 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps)
2168 gint fd = v4l2object->video_fd;
2169 struct v4l2_format format;
2170 struct v4l2_streamparm streamparm;
2171 enum v4l2_field field;
2172 guint32 pixelformat;
2173 struct v4l2_fmtdesc *fmtdesc;
2175 gint width, height, fps_n, fps_d, stride;
2177 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
2180 pixelformat = fmtdesc->pixelformat;
2181 width = GST_VIDEO_INFO_WIDTH (&info);
2182 height = GST_VIDEO_INFO_HEIGHT (&info);
2183 fps_n = GST_VIDEO_INFO_FPS_N (&info);
2184 fps_d = GST_VIDEO_INFO_FPS_D (&info);
2185 stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
2187 if (info.flags & GST_VIDEO_FLAG_INTERLACED) {
2188 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
2189 /* ideally we would differentiate between types of interlaced video
2190 * but there is not sufficient information in the caps..
2192 field = V4L2_FIELD_INTERLACED;
2194 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
2195 field = V4L2_FIELD_NONE;
2198 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
2199 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
2200 GST_FOURCC_ARGS (pixelformat), stride);
2202 GST_V4L2_CHECK_OPEN (v4l2object);
2203 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2205 /* Only unconditionally accept mpegts for sources */
2206 if ((v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
2207 (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')))
2210 memset (&format, 0x00, sizeof (struct v4l2_format));
2211 format.type = v4l2object->type;
2213 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
2214 goto get_fmt_failed;
2216 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2217 "%" GST_FOURCC_FORMAT " bytesperline %d, colorspace %d",
2218 format.fmt.pix.width, format.fmt.pix.height,
2219 GST_FOURCC_ARGS (format.fmt.pix.pixelformat), format.fmt.pix.bytesperline,
2220 format.fmt.pix.colorspace);
2222 if (format.type != v4l2object->type ||
2223 format.fmt.pix.width != width ||
2224 format.fmt.pix.height != height ||
2225 format.fmt.pix.pixelformat != pixelformat ||
2226 format.fmt.pix.field != field || format.fmt.pix.bytesperline != stride) {
2227 /* something different, set the format */
2228 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2229 "%" GST_FOURCC_FORMAT " bytesperline %d", width, height,
2230 GST_FOURCC_ARGS (pixelformat), stride);
2232 format.type = v4l2object->type;
2233 format.fmt.pix.width = width;
2234 format.fmt.pix.height = height;
2235 format.fmt.pix.pixelformat = pixelformat;
2236 format.fmt.pix.field = field;
2237 /* try to ask our prefered stride */
2238 format.fmt.pix.bytesperline = stride;
2240 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
2241 goto set_fmt_failed;
2243 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2244 "%" GST_FOURCC_FORMAT " stride %d", format.fmt.pix.width,
2245 format.fmt.pix.height, GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2246 format.fmt.pix.bytesperline);
2248 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
2249 goto invalid_dimensions;
2251 if (format.fmt.pix.pixelformat != pixelformat)
2252 goto invalid_pixelformat;
2255 /* figure out the frame layout */
2256 v4l2object->bytesperline = format.fmt.pix.bytesperline;
2257 v4l2object->sizeimage = format.fmt.pix.sizeimage;
2259 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %u",
2260 v4l2object->sizeimage);
2262 /* Is there a reason we require the caller to always specify a framerate? */
2263 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
2266 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
2267 streamparm.type = v4l2object->type;
2269 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
2270 goto get_parm_failed;
2272 GST_VIDEO_INFO_FPS_N (&info) =
2273 streamparm.parm.capture.timeperframe.denominator;
2274 GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
2276 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2277 GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
2278 streamparm.parm.capture.timeperframe.denominator,
2279 streamparm.parm.capture.timeperframe.numerator);
2281 /* Note: V4L2 provides the frame interval, we have the frame rate */
2282 if (!fractions_are_equal (streamparm.parm.capture.timeperframe.numerator,
2283 streamparm.parm.capture.timeperframe.denominator, fps_d, fps_n)) {
2284 GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
2286 /* We want to change the frame rate, so check whether we can. Some cheap USB
2287 * cameras don't have the capability */
2288 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
2289 GST_DEBUG_OBJECT (v4l2object->element,
2290 "Not setting framerate (not supported)");
2294 /* Note: V4L2 wants the frame interval, we have the frame rate */
2295 streamparm.parm.capture.timeperframe.numerator = fps_d;
2296 streamparm.parm.capture.timeperframe.denominator = fps_n;
2298 /* some cheap USB cam's won't accept any change */
2299 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
2300 goto set_parm_failed;
2302 /* get new values */
2303 fps_d = streamparm.parm.capture.timeperframe.numerator;
2304 fps_n = streamparm.parm.capture.timeperframe.denominator;
2306 GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
2309 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
2310 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
2315 /* if we have a framerate pre-calculate duration */
2316 if (fps_n > 0 && fps_d > 0) {
2317 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
2319 v4l2object->duration = GST_CLOCK_TIME_NONE;
2321 v4l2object->info = info;
2322 v4l2object->fmtdesc = fmtdesc;
2324 /* now configure ther pools */
2325 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
2333 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
2339 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2340 (_("Device '%s' does not support video capture"),
2341 v4l2object->videodev),
2342 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
2347 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2348 (_("Device '%s' cannot capture at %dx%d"),
2349 v4l2object->videodev, width, height),
2350 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2351 GST_FOURCC_ARGS (pixelformat), width, height, g_strerror (errno)));
2356 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2357 (_("Device '%s' cannot capture at %dx%d"),
2358 v4l2object->videodev, width, height),
2359 ("Tried to capture at %dx%d, but device returned size %dx%d",
2360 width, height, format.fmt.pix.width, format.fmt.pix.height));
2363 invalid_pixelformat:
2365 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2366 (_("Device '%s' cannot capture in the specified format"),
2367 v4l2object->videodev),
2368 ("Tried to capture in %" GST_FOURCC_FORMAT
2369 ", but device returned format" " %" GST_FOURCC_FORMAT,
2370 GST_FOURCC_ARGS (pixelformat),
2371 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
2376 /* it's possible that this call is not supported */
2377 if (errno != EINVAL) {
2378 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2379 (_("Could not get parameters on device '%s'"),
2380 v4l2object->videodev), GST_ERROR_SYSTEM);
2386 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2387 (_("Video device did not accept new frame rate setting.")),
2393 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2394 (_("Video device could not create buffer pool.")), GST_ERROR_SYSTEM);
2400 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
2402 GST_LOG_OBJECT (v4l2object->element, "flush poll");
2403 gst_poll_set_flushing (v4l2object->poll, TRUE);
2409 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
2411 GST_LOG_OBJECT (v4l2object->element, "flush stop poll");
2412 gst_poll_set_flushing (v4l2object->poll, FALSE);
2418 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
2420 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
2422 if (!GST_V4L2_IS_OPEN (v4l2object))
2424 if (!GST_V4L2_IS_ACTIVE (v4l2object))
2427 if (v4l2object->pool) {
2428 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
2429 gst_buffer_pool_set_active (GST_BUFFER_POOL_CAST (v4l2object->pool), FALSE);
2430 gst_object_unref (v4l2object->pool);
2431 v4l2object->pool = NULL;
2434 GST_V4L2_SET_INACTIVE (v4l2object);
2441 static GstFlowReturn
2442 gst_v4l2_object_get_mmap (GstV4l2Object * v4l2object, GstBuffer ** buf)
2445 #define NUM_TRIALS 50
2446 GstBufferPool *pool;
2447 gint32 trials = NUM_TRIALS;
2448 GstBuffer *pool_buffer;
2451 pool = v4l2object->pool;
2453 goto no_buffer_pool;
2455 GST_DEBUG_OBJECT (v4l2object->element, "grab frame");
2458 if ((res = gst_v4l2_object_poll (v4l2object)) != GST_FLOW_OK)
2461 res = gst_buffer_pool_acquire_buffer (pool, &pool_buffer, NULL);
2462 if (res != GST_FLOW_OK)
2465 if (v4l2object->size > 0) {
2466 gsize size = gst_buffer_get_size (pool_buffer);
2468 /* if size does not match what we expected, try again */
2469 if (size != v4l2object->size) {
2470 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, READ,
2471 (_("Got unexpected frame size of %u instead of %u."),
2472 size, v4l2object->size), (NULL));
2473 gst_buffer_unref (pool_buffer);
2477 /* when we get here all is fine */
2481 GST_WARNING_OBJECT (v4l2object->element, "trials=%d", trials);
2483 /* if the sync() got interrupted, we can retry */
2488 return GST_FLOW_ERROR;
2494 /* try again, until too many trials */
2498 /* check nr. of attempts to capture */
2499 if (--trials == -1) {
2500 goto too_many_trials;
2505 /* if we are handing out the last buffer in the pool, we need to make a
2506 * copy and bring the buffer back in the pool. */
2507 need_copy = v4l2object->always_copy
2508 || !gst_v4l2_buffer_pool_available_buffers (pool);
2510 if (G_UNLIKELY (need_copy)) {
2511 if (!v4l2object->always_copy) {
2512 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, v4l2object->element,
2513 "running out of buffers, making a copy to reuse current one");
2515 *buf = gst_buffer_copy (pool_buffer);
2516 /* this will requeue */
2517 gst_buffer_unref (pool_buffer);
2527 GST_DEBUG_OBJECT (v4l2object->element, "no buffer pool");
2528 return GST_FLOW_WRONG_STATE;
2536 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, FAILED,
2537 (_("Failed trying to get video frames from device '%s'."),
2538 v4l2object->videodev),
2539 (_("Failed after %d tries. device %s. system error: %s"),
2540 NUM_TRIALS, v4l2object->videodev, g_strerror (errno)));
2541 return GST_FLOW_ERROR;
2547 gst_v4l2_object_copy (GstV4l2Object * v4l2object, GstBuffer * dest,
2553 if (v4l2object->info.finfo) {
2554 GstVideoFrame src_frame, dest_frame;
2556 GST_DEBUG_OBJECT (v4l2object->element, "copy video frame");
2558 /* we have raw video, use videoframe copy to get strides right */
2559 if (!gst_video_frame_map (&src_frame, &v4l2object->info, src, GST_MAP_READ))
2560 goto invalid_buffer;
2562 if (!gst_video_frame_map (&dest_frame, &v4l2object->info, dest,
2564 gst_video_frame_unmap (&src_frame);
2565 goto invalid_buffer;
2568 gst_video_frame_copy (&dest_frame, &src_frame);
2570 gst_video_frame_unmap (&src_frame);
2571 gst_video_frame_unmap (&dest_frame);
2573 GST_DEBUG_OBJECT (v4l2object->element, "copy raw bytes");
2574 data = gst_buffer_map (src, &size, NULL, GST_MAP_READ);
2575 gst_buffer_fill (dest, 0, data, size);
2576 gst_buffer_unmap (src, data, size);
2578 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, v4l2object->element,
2579 "slow copy into buffer %p", dest);
2586 /* No Window available to put our image into */
2587 GST_WARNING_OBJECT (v4l2object->element, "could not map image");