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;
1380 /* default unknown values */
1383 structure = gst_caps_get_structure (caps, 0);
1385 mimetype = gst_structure_get_name (structure);
1387 if (g_str_equal (mimetype, "video/x-raw")) {
1388 /* raw caps, parse into video info */
1389 if (!gst_video_info_from_caps (info, caps))
1390 goto invalid_format;
1392 switch (GST_VIDEO_INFO_FORMAT (info)) {
1393 case GST_VIDEO_FORMAT_I420:
1394 fourcc = V4L2_PIX_FMT_YUV420;
1396 case GST_VIDEO_FORMAT_YUY2:
1397 fourcc = V4L2_PIX_FMT_YUYV;
1400 case GST_VIDEO_FORMAT_Y41P:
1401 fourcc = V4L2_PIX_FMT_Y41P;
1404 case GST_VIDEO_FORMAT_UYVY:
1405 fourcc = V4L2_PIX_FMT_UYVY;
1407 case GST_VIDEO_FORMAT_YV12:
1408 fourcc = V4L2_PIX_FMT_YVU420;
1410 case GST_VIDEO_FORMAT_Y41B:
1411 fourcc = V4L2_PIX_FMT_YUV411P;
1413 case GST_VIDEO_FORMAT_Y42B:
1414 fourcc = V4L2_PIX_FMT_YUV422P;
1416 case GST_VIDEO_FORMAT_NV12:
1417 fourcc = V4L2_PIX_FMT_NV12;
1419 case GST_VIDEO_FORMAT_NV21:
1420 fourcc = V4L2_PIX_FMT_NV21;
1422 #ifdef V4L2_PIX_FMT_YVYU
1423 case GST_VIDEO_FORMAT_YVYU:
1424 fourcc = V4L2_PIX_FMT_YVYU;
1427 case GST_VIDEO_FORMAT_RGB15:
1428 fourcc = V4L2_PIX_FMT_RGB555;
1430 case GST_VIDEO_FORMAT_RGB16:
1431 fourcc = V4L2_PIX_FMT_RGB565;
1433 case GST_VIDEO_FORMAT_RGB:
1434 fourcc = V4L2_PIX_FMT_RGB24;
1436 case GST_VIDEO_FORMAT_BGR:
1437 fourcc = V4L2_PIX_FMT_BGR24;
1439 case GST_VIDEO_FORMAT_RGBx:
1440 case GST_VIDEO_FORMAT_RGBA:
1441 fourcc = V4L2_PIX_FMT_RGB32;
1443 case GST_VIDEO_FORMAT_BGRx:
1444 case GST_VIDEO_FORMAT_BGRA:
1445 fourcc = V4L2_PIX_FMT_BGR32;
1447 case GST_VIDEO_FORMAT_GRAY8:
1448 fourcc = V4L2_PIX_FMT_GREY;
1453 gboolean dimensions = TRUE;
1455 /* no video caps, construct videoinfo ourselves */
1456 gst_video_info_init (info);
1458 if (g_str_equal (mimetype, "video/mpegts")) {
1459 fourcc = V4L2_PIX_FMT_MPEG;
1461 } else if (g_str_equal (mimetype, "video/x-dv")) {
1462 fourcc = V4L2_PIX_FMT_DV;
1463 } else if (g_str_equal (mimetype, "image/jpeg")) {
1464 fourcc = V4L2_PIX_FMT_JPEG;
1465 #ifdef V4L2_PIX_FMT_SBGGR8
1466 } else if (g_str_equal (mimetype, "video/x-raw-bayer")) {
1467 fourcc = V4L2_PIX_FMT_SBGGR8;
1469 #ifdef V4L2_PIX_FMT_SN9C10X
1470 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1471 fourcc = V4L2_PIX_FMT_SN9C10X;
1473 #ifdef V4L2_PIX_FMT_PWC1
1474 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1475 fourcc = V4L2_PIX_FMT_PWC1;
1477 #ifdef V4L2_PIX_FMT_PWC2
1478 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1479 fourcc = V4L2_PIX_FMT_PWC2;
1484 gboolean interlaced;
1486 if (!gst_structure_get_int (structure, "width", &info->width))
1489 if (!gst_structure_get_int (structure, "height", &info->height))
1492 if (!gst_structure_get_boolean (structure, "interlaced", &interlaced))
1495 info->flags |= GST_VIDEO_FLAG_INTERLACED;
1497 if (!gst_structure_get_fraction (structure, "framerate", &info->fps_n,
1504 goto unhandled_format;
1506 *format = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1513 GST_DEBUG_OBJECT (v4l2object, "no width");
1518 GST_DEBUG_OBJECT (v4l2object, "no height");
1523 GST_DEBUG_OBJECT (v4l2object, "no framerate");
1528 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1533 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1540 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1541 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced);
1544 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1545 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1546 static GstStructure *
1547 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1548 guint32 pixelformat,
1549 guint32 width, guint32 height, const GstStructure * template)
1551 gint fd = v4l2object->video_fd;
1552 struct v4l2_frmivalenum ival;
1555 GValue rates = { 0, };
1556 gboolean interlaced;
1557 gint int_width = width;
1558 gint int_height = height;
1560 /* interlaced detection using VIDIOC_TRY/S_FMT */
1561 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat,
1562 &int_width, &int_height, &interlaced))
1565 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1567 ival.pixel_format = pixelformat;
1569 ival.height = height;
1571 GST_LOG_OBJECT (v4l2object->element,
1572 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1573 GST_FOURCC_ARGS (pixelformat));
1575 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1576 * fraction to get framerate */
1577 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1578 goto enum_frameintervals_failed;
1580 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1581 GValue rate = { 0, };
1583 g_value_init (&rates, GST_TYPE_LIST);
1584 g_value_init (&rate, GST_TYPE_FRACTION);
1587 num = ival.discrete.numerator;
1588 denom = ival.discrete.denominator;
1590 if (num > G_MAXINT || denom > G_MAXINT) {
1591 /* let us hope we don't get here... */
1596 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1599 /* swap to get the framerate */
1600 gst_value_set_fraction (&rate, denom, num);
1601 gst_value_list_append_value (&rates, &rate);
1604 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1605 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1606 GValue min = { 0, };
1607 GValue step = { 0, };
1608 GValue max = { 0, };
1609 gboolean added = FALSE;
1610 guint32 minnum, mindenom;
1611 guint32 maxnum, maxdenom;
1613 g_value_init (&rates, GST_TYPE_LIST);
1615 g_value_init (&min, GST_TYPE_FRACTION);
1616 g_value_init (&step, GST_TYPE_FRACTION);
1617 g_value_init (&max, GST_TYPE_FRACTION);
1620 minnum = ival.stepwise.min.numerator;
1621 mindenom = ival.stepwise.min.denominator;
1622 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1626 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1628 gst_value_set_fraction (&min, minnum, mindenom);
1631 maxnum = ival.stepwise.max.numerator;
1632 maxdenom = ival.stepwise.max.denominator;
1633 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1638 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1640 gst_value_set_fraction (&max, maxnum, maxdenom);
1643 num = ival.stepwise.step.numerator;
1644 denom = ival.stepwise.step.denominator;
1645 if (num > G_MAXINT || denom > G_MAXINT) {
1650 if (num == 0 || denom == 0) {
1651 /* in this case we have a wrong fraction or no step, set the step to max
1652 * so that we only add the min value in the loop below */
1657 /* since we only have gst_value_fraction_subtract and not add, negate the
1659 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1661 gst_value_set_fraction (&step, -num, denom);
1663 while (gst_value_compare (&min, &max) <= 0) {
1664 GValue rate = { 0, };
1666 num = gst_value_get_fraction_numerator (&min);
1667 denom = gst_value_get_fraction_denominator (&min);
1668 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1671 /* invert to get the framerate */
1672 g_value_init (&rate, GST_TYPE_FRACTION);
1673 gst_value_set_fraction (&rate, denom, num);
1674 gst_value_list_append_value (&rates, &rate);
1677 /* we're actually adding because step was negated above. This is because
1678 * there is no _add function... */
1679 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1680 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1685 /* no range was added, leave the default range from the template */
1686 GST_WARNING_OBJECT (v4l2object->element,
1687 "no range added, leaving default");
1688 g_value_unset (&rates);
1690 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1691 guint32 maxnum, maxdenom;
1693 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1695 num = ival.stepwise.min.numerator;
1696 denom = ival.stepwise.min.denominator;
1697 if (num > G_MAXINT || denom > G_MAXINT) {
1702 maxnum = ival.stepwise.max.numerator;
1703 maxdenom = ival.stepwise.max.denominator;
1704 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1709 GST_LOG_OBJECT (v4l2object->element,
1710 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
1713 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1719 s = gst_structure_copy (template);
1720 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
1721 "height", G_TYPE_INT, (gint) height,
1722 "interlaced", G_TYPE_BOOLEAN, interlaced, NULL);
1724 if (G_IS_VALUE (&rates)) {
1725 /* only change the framerate on the template when we have a valid probed new
1727 gst_structure_set_value (s, "framerate", &rates);
1728 g_value_unset (&rates);
1730 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
1736 enum_frameintervals_failed:
1738 GST_DEBUG_OBJECT (v4l2object->element,
1739 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
1740 GST_FOURCC_ARGS (pixelformat), width, height);
1745 /* I don't see how this is actually an error, we ignore the format then */
1746 GST_WARNING_OBJECT (v4l2object->element,
1747 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
1748 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
1752 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
1754 #ifdef VIDIOC_ENUM_FRAMESIZES
1756 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
1760 gst_structure_get_int (s1, "width", &w1);
1761 gst_structure_get_int (s1, "height", &h1);
1762 gst_structure_get_int (s2, "width", &w2);
1763 gst_structure_get_int (s2, "height", &h2);
1765 /* I think it's safe to assume that this won't overflow for a while */
1766 return ((w2 * h2) - (w1 * h1));
1771 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
1772 guint32 pixelformat, const GstStructure * template)
1774 GstCaps *ret = gst_caps_new_empty ();
1777 #ifdef VIDIOC_ENUM_FRAMESIZES
1778 gint fd = v4l2object->video_fd;
1779 struct v4l2_frmsizeenum size;
1780 GList *results = NULL;
1783 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1784 return gst_caps_new_simple ("video/mpegts", NULL);
1786 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
1788 size.pixel_format = pixelformat;
1790 GST_DEBUG_OBJECT (v4l2object->element, "Enumerating frame sizes");
1792 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
1793 goto enum_framesizes_failed;
1795 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1797 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
1798 size.discrete.width, size.discrete.height);
1800 w = MIN (size.discrete.width, G_MAXINT);
1801 h = MIN (size.discrete.height, G_MAXINT);
1805 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1806 pixelformat, w, h, template);
1809 results = g_list_prepend (results, tmp);
1813 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
1814 GST_DEBUG_OBJECT (v4l2object->element,
1815 "done iterating discrete frame sizes");
1816 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
1817 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
1818 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1819 size.stepwise.min_width);
1820 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1821 size.stepwise.min_height);
1822 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1823 size.stepwise.max_width);
1824 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1825 size.stepwise.max_height);
1826 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
1827 size.stepwise.step_width);
1828 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
1829 size.stepwise.step_height);
1831 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
1832 w < size.stepwise.max_width && h < size.stepwise.max_height;
1833 w += size.stepwise.step_width, h += size.stepwise.step_height) {
1834 if (w == 0 || h == 0)
1838 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1839 pixelformat, w, h, template);
1842 results = g_list_prepend (results, tmp);
1844 GST_DEBUG_OBJECT (v4l2object->element,
1845 "done iterating stepwise frame sizes");
1846 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
1849 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
1850 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1851 size.stepwise.min_width);
1852 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1853 size.stepwise.min_height);
1854 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1855 size.stepwise.max_width);
1856 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1857 size.stepwise.max_height);
1859 w = MAX (size.stepwise.min_width, 1);
1860 h = MAX (size.stepwise.min_height, 1);
1861 maxw = MIN (size.stepwise.max_width, G_MAXINT);
1862 maxh = MIN (size.stepwise.max_height, G_MAXINT);
1865 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
1868 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
1869 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
1872 /* no point using the results list here, since there's only one struct */
1873 gst_caps_append_structure (ret, tmp);
1879 /* we use an intermediary list to store and then sort the results of the
1880 * probing because we can't make any assumptions about the order in which
1881 * the driver will give us the sizes, but we want the final caps to contain
1882 * the results starting with the highest resolution and having the lowest
1883 * resolution last, since order in caps matters for things like fixation. */
1884 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
1885 while (results != NULL) {
1886 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
1887 results = g_list_delete_link (results, results);
1890 if (gst_caps_is_empty (ret))
1891 goto enum_framesizes_no_results;
1896 enum_framesizes_failed:
1898 /* I don't see how this is actually an error */
1899 GST_DEBUG_OBJECT (v4l2object->element,
1900 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
1901 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
1902 goto default_frame_sizes;
1904 enum_framesizes_no_results:
1906 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
1907 * question doesn't actually support it yet */
1908 GST_DEBUG_OBJECT (v4l2object->element,
1909 "No results for pixelformat %" GST_FOURCC_FORMAT
1910 " enumerating frame sizes, trying fallback",
1911 GST_FOURCC_ARGS (pixelformat));
1912 goto default_frame_sizes;
1916 GST_WARNING_OBJECT (v4l2object->element,
1917 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
1918 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
1919 goto default_frame_sizes;
1921 default_frame_sizes:
1922 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
1924 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
1925 gboolean interlaced;
1927 /* This code is for Linux < 2.6.19 */
1929 max_w = max_h = GST_V4L2_MAX_SIZE;
1930 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
1931 &min_h, &interlaced)) {
1932 GST_WARNING_OBJECT (v4l2object->element,
1933 "Could not probe minimum capture size for pixelformat %"
1934 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1936 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
1937 &max_h, &interlaced)) {
1938 GST_WARNING_OBJECT (v4l2object->element,
1939 "Could not probe maximum capture size for pixelformat %"
1940 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1943 /* Since we can't get framerate directly, try to use the current norm */
1944 if (v4l2object->tv_norm && v4l2object->norms) {
1946 GstTunerNorm *norm = NULL;
1947 GstTunerNorm *current =
1948 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
1950 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
1951 norm = (GstTunerNorm *) norms->data;
1952 if (!strcmp (norm->label, current->label))
1955 /* If it's possible, set framerate to that (discrete) value */
1957 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
1958 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
1962 tmp = gst_structure_copy (template);
1964 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
1967 /* if norm can't be used, copy the template framerate */
1968 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
1973 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
1975 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
1978 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
1980 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
1982 gst_structure_set (tmp, "interlaced", G_TYPE_BOOLEAN, interlaced, NULL);
1984 gst_caps_append_structure (ret, tmp);
1991 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1992 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced)
1994 struct v4l2_format fmt;
1998 g_return_val_if_fail (width != NULL, FALSE);
1999 g_return_val_if_fail (height != NULL, FALSE);
2001 GST_LOG_OBJECT (v4l2object->element,
2002 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2003 *width, *height, GST_FOURCC_ARGS (pixelformat));
2005 fd = v4l2object->video_fd;
2007 /* get size delimiters */
2008 memset (&fmt, 0, sizeof (fmt));
2009 fmt.type = v4l2object->type;
2010 fmt.fmt.pix.width = *width;
2011 fmt.fmt.pix.height = *height;
2012 fmt.fmt.pix.pixelformat = pixelformat;
2013 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2015 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2016 if (r < 0 && errno == EINVAL) {
2017 /* try again with interlaced video */
2018 fmt.fmt.pix.width = *width;
2019 fmt.fmt.pix.height = *height;
2020 fmt.fmt.pix.pixelformat = pixelformat;
2021 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2022 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2026 /* The driver might not implement TRY_FMT, in which case we will try
2028 if (errno != ENOTTY)
2031 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
2032 be, because we're still probing */
2033 if (GST_V4L2_IS_ACTIVE (v4l2object))
2036 GST_LOG_OBJECT (v4l2object->element,
2037 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
2039 fmt.fmt.pix.width = *width;
2040 fmt.fmt.pix.height = *height;
2042 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2043 if (r < 0 && errno == EINVAL) {
2044 /* try again with progressive video */
2045 fmt.fmt.pix.width = *width;
2046 fmt.fmt.pix.height = *height;
2047 fmt.fmt.pix.pixelformat = pixelformat;
2048 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2049 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2056 GST_LOG_OBJECT (v4l2object->element,
2057 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2059 *width = fmt.fmt.pix.width;
2060 *height = fmt.fmt.pix.height;
2062 switch (fmt.fmt.pix.field) {
2063 case V4L2_FIELD_ANY:
2064 case V4L2_FIELD_NONE:
2065 *interlaced = FALSE;
2067 case V4L2_FIELD_INTERLACED:
2068 case V4L2_FIELD_INTERLACED_TB:
2069 case V4L2_FIELD_INTERLACED_BT:
2073 GST_WARNING_OBJECT (v4l2object->element,
2074 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u",
2075 GST_FOURCC_ARGS (pixelformat), *width, *height);
2083 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2086 GstStructure *config;
2088 GST_DEBUG_OBJECT (v4l2object->element, "initializing the capture system");
2090 GST_V4L2_CHECK_OPEN (v4l2object);
2091 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2093 /* use specified mode */
2094 v4l2object->mode = v4l2object->req_mode;
2096 if (v4l2object->req_mode == GST_V4L2_IO_AUTO) {
2097 /* automatic mode, find transport */
2098 if (v4l2object->vcap.capabilities & V4L2_CAP_READWRITE) {
2099 GST_INFO_OBJECT (v4l2object->element,
2100 "accessing buffers via read()/write()");
2101 v4l2object->mode = GST_V4L2_IO_RW;
2103 if (v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
2104 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mmap()");
2105 v4l2object->mode = GST_V4L2_IO_MMAP;
2108 /* if still no transport selected, error out */
2109 if (v4l2object->mode == GST_V4L2_IO_AUTO)
2110 goto no_supported_capture_method;
2112 /* keep track of current number of buffers */
2113 num_buffers = v4l2object->num_buffers;
2115 /* Map the buffers */
2116 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2118 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object)))
2119 goto buffer_pool_new_failed;
2121 config = gst_buffer_pool_get_config (v4l2object->pool);
2122 gst_buffer_pool_config_set (config, caps, v4l2object->info.size,
2123 num_buffers, num_buffers, 0, 0);
2124 gst_buffer_pool_set_config (v4l2object->pool, config);
2126 GST_V4L2_SET_ACTIVE (v4l2object);
2131 buffer_pool_new_failed:
2133 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2134 (_("Could not map buffers from device '%s'"),
2135 v4l2object->videodev),
2136 ("Failed to create buffer pool: %s", g_strerror (errno)));
2139 no_supported_capture_method:
2141 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2142 (_("The driver of device '%s' does not support any known capture "
2143 "method."), v4l2object->videodev), (NULL));
2149 /* Note about fraction simplification
2150 * * n1/d1 == n2/d2 is also written as n1 == ( n2 * d1 ) / d2
2152 #define fractions_are_equal(n1,d1,n2,d2) ((n1) == gst_util_uint64_scale_int((n2), (d1), (d2)))
2155 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps)
2157 gint fd = v4l2object->video_fd;
2158 struct v4l2_format format;
2159 struct v4l2_streamparm streamparm;
2160 enum v4l2_field field;
2161 guint32 pixelformat;
2162 struct v4l2_fmtdesc *fmtdesc;
2164 gint width, height, fps_n, fps_d, stride;
2166 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
2169 pixelformat = fmtdesc->pixelformat;
2170 width = GST_VIDEO_INFO_WIDTH (&info);
2171 height = GST_VIDEO_INFO_HEIGHT (&info);
2172 fps_n = GST_VIDEO_INFO_FPS_N (&info);
2173 fps_d = GST_VIDEO_INFO_FPS_D (&info);
2174 stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
2176 if (info.flags & GST_VIDEO_FLAG_INTERLACED) {
2177 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
2178 /* ideally we would differentiate between types of interlaced video
2179 * but there is not sufficient information in the caps..
2181 field = V4L2_FIELD_INTERLACED;
2183 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
2184 field = V4L2_FIELD_NONE;
2187 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
2188 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
2189 GST_FOURCC_ARGS (pixelformat), stride);
2191 GST_V4L2_CHECK_OPEN (v4l2object);
2192 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2194 /* Only unconditionally accept mpegts for sources */
2195 if ((v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
2196 (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')))
2199 memset (&format, 0x00, sizeof (struct v4l2_format));
2200 format.type = v4l2object->type;
2202 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
2203 goto get_fmt_failed;
2205 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2206 "%" GST_FOURCC_FORMAT " bytesperline %d", format.fmt.pix.width,
2207 format.fmt.pix.height, GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2208 format.fmt.pix.bytesperline);
2210 if (format.type != v4l2object->type ||
2211 format.fmt.pix.width != width ||
2212 format.fmt.pix.height != height ||
2213 format.fmt.pix.pixelformat != pixelformat ||
2214 format.fmt.pix.field != field || format.fmt.pix.bytesperline != stride) {
2215 /* something different, set the format */
2216 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2217 "%" GST_FOURCC_FORMAT " bytesperline %d", width, height,
2218 GST_FOURCC_ARGS (pixelformat), stride);
2220 format.type = v4l2object->type;
2221 format.fmt.pix.width = width;
2222 format.fmt.pix.height = height;
2223 format.fmt.pix.pixelformat = pixelformat;
2224 format.fmt.pix.field = field;
2225 /* try to ask our prefered stride */
2226 format.fmt.pix.bytesperline = stride;
2228 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
2229 goto set_fmt_failed;
2231 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2232 "%" GST_FOURCC_FORMAT " stride %d", format.fmt.pix.width,
2233 format.fmt.pix.height, GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2234 format.fmt.pix.bytesperline);
2236 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
2237 goto invalid_dimensions;
2239 if (format.fmt.pix.pixelformat != pixelformat)
2240 goto invalid_pixelformat;
2242 v4l2object->bytesperline = format.fmt.pix.bytesperline;
2243 /* FIXME, size for only one plane */
2244 v4l2object->size = v4l2object->bytesperline * height;
2246 /* Is there a reason we require the caller to always specify a framerate? */
2247 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
2250 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
2251 streamparm.type = v4l2object->type;
2253 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
2254 goto get_parm_failed;
2256 GST_VIDEO_INFO_FPS_N (&info) =
2257 streamparm.parm.capture.timeperframe.denominator;
2258 GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
2260 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2261 GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
2262 streamparm.parm.capture.timeperframe.denominator,
2263 streamparm.parm.capture.timeperframe.numerator);
2265 /* Note: V4L2 provides the frame interval, we have the frame rate */
2266 if (!fractions_are_equal (streamparm.parm.capture.timeperframe.numerator,
2267 streamparm.parm.capture.timeperframe.denominator, fps_d, fps_n)) {
2268 GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
2270 /* We want to change the frame rate, so check whether we can. Some cheap USB
2271 * cameras don't have the capability */
2272 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
2273 GST_DEBUG_OBJECT (v4l2object->element,
2274 "Not setting framerate (not supported)");
2278 /* Note: V4L2 wants the frame interval, we have the frame rate */
2279 streamparm.parm.capture.timeperframe.numerator = fps_d;
2280 streamparm.parm.capture.timeperframe.denominator = fps_n;
2282 /* some cheap USB cam's won't accept any change */
2283 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
2284 goto set_parm_failed;
2286 /* get new values */
2287 fps_d = streamparm.parm.capture.timeperframe.numerator;
2288 fps_n = streamparm.parm.capture.timeperframe.denominator;
2290 GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
2293 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
2294 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
2299 /* if we have a framerate pre-calculate duration */
2300 if (fps_n > 0 && fps_d > 0) {
2301 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
2303 v4l2object->duration = GST_CLOCK_TIME_NONE;
2305 v4l2object->info = info;
2306 v4l2object->fmtdesc = fmtdesc;
2308 /* now configure ther pools */
2309 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
2317 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
2323 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2324 (_("Device '%s' does not support video capture"),
2325 v4l2object->videodev),
2326 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
2331 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2332 (_("Device '%s' cannot capture at %dx%d"),
2333 v4l2object->videodev, width, height),
2334 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2335 GST_FOURCC_ARGS (pixelformat), width, height, g_strerror (errno)));
2340 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2341 (_("Device '%s' cannot capture at %dx%d"),
2342 v4l2object->videodev, width, height),
2343 ("Tried to capture at %dx%d, but device returned size %dx%d",
2344 width, height, format.fmt.pix.width, format.fmt.pix.height));
2347 invalid_pixelformat:
2349 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2350 (_("Device '%s' cannot capture in the specified format"),
2351 v4l2object->videodev),
2352 ("Tried to capture in %" GST_FOURCC_FORMAT
2353 ", but device returned format" " %" GST_FOURCC_FORMAT,
2354 GST_FOURCC_ARGS (pixelformat),
2355 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
2360 /* it's possible that this call is not supported */
2361 if (errno != EINVAL) {
2362 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2363 (_("Could not get parameters on device '%s'"),
2364 v4l2object->videodev), GST_ERROR_SYSTEM);
2370 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2371 (_("Video device did not accept new frame rate setting.")),
2377 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2378 (_("Video device could not create buffer pool.")), GST_ERROR_SYSTEM);
2384 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
2386 GST_LOG_OBJECT (v4l2object->element, "flush poll");
2387 gst_poll_set_flushing (v4l2object->poll, TRUE);
2393 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
2395 GST_LOG_OBJECT (v4l2object->element, "flush stop poll");
2396 gst_poll_set_flushing (v4l2object->poll, FALSE);
2402 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
2404 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
2406 if (!GST_V4L2_IS_OPEN (v4l2object))
2408 if (!GST_V4L2_IS_ACTIVE (v4l2object))
2411 if (v4l2object->pool) {
2412 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
2413 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
2414 gst_object_unref (v4l2object->pool);
2415 v4l2object->pool = NULL;
2418 GST_V4L2_SET_INACTIVE (v4l2object);
2425 static GstFlowReturn
2426 gst_v4l2_object_get_mmap (GstV4l2Object * v4l2object, GstBuffer ** buf)
2429 #define NUM_TRIALS 50
2430 GstBufferPool *pool;
2431 gint32 trials = NUM_TRIALS;
2432 GstBuffer *pool_buffer;
2435 pool = v4l2object->pool;
2437 goto no_buffer_pool;
2439 GST_DEBUG_OBJECT (v4l2object->element, "grab frame");
2442 if ((res = gst_v4l2_object_poll (v4l2object)) != GST_FLOW_OK)
2445 res = gst_buffer_pool_acquire_buffer (pool, &pool_buffer, NULL);
2446 if (res != GST_FLOW_OK)
2449 if (v4l2object->size > 0) {
2450 gsize size = gst_buffer_get_size (pool_buffer);
2452 /* if size does not match what we expected, try again */
2453 if (size != v4l2object->size) {
2454 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, READ,
2455 (_("Got unexpected frame size of %u instead of %u."),
2456 size, v4l2object->size), (NULL));
2457 gst_buffer_unref (pool_buffer);
2461 /* when we get here all is fine */
2465 GST_WARNING_OBJECT (v4l2object->element, "trials=%d", trials);
2467 /* if the sync() got interrupted, we can retry */
2472 return GST_FLOW_ERROR;
2478 /* try again, until too many trials */
2482 /* check nr. of attempts to capture */
2483 if (--trials == -1) {
2484 goto too_many_trials;
2489 /* if we are handing out the last buffer in the pool, we need to make a
2490 * copy and bring the buffer back in the pool. */
2491 need_copy = v4l2object->always_copy
2492 || !gst_v4l2_buffer_pool_available_buffers (pool);
2494 if (G_UNLIKELY (need_copy)) {
2495 if (!v4l2object->always_copy) {
2496 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, v4l2object->element,
2497 "running out of buffers, making a copy to reuse current one");
2499 *buf = gst_buffer_copy (pool_buffer);
2500 /* this will requeue */
2501 gst_buffer_unref (pool_buffer);
2511 GST_DEBUG_OBJECT (v4l2object->element, "no buffer pool");
2512 return GST_FLOW_WRONG_STATE;
2520 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, FAILED,
2521 (_("Failed trying to get video frames from device '%s'."),
2522 v4l2object->videodev),
2523 (_("Failed after %d tries. device %s. system error: %s"),
2524 NUM_TRIALS, v4l2object->videodev, g_strerror (errno)));
2525 return GST_FLOW_ERROR;
2531 gst_v4l2_object_copy (GstV4l2Object * v4l2object, GstBuffer * dest,
2537 if (v4l2object->info.finfo) {
2538 GstVideoFrame src_frame, dest_frame;
2540 GST_DEBUG_OBJECT (v4l2object->element, "copy video frame");
2542 /* we have raw video, use videoframe copy to get strides right */
2543 if (!gst_video_frame_map (&src_frame, &v4l2object->info, src, GST_MAP_READ))
2544 goto invalid_buffer;
2546 if (!gst_video_frame_map (&dest_frame, &v4l2object->info, dest,
2548 gst_video_frame_unmap (&src_frame);
2549 goto invalid_buffer;
2552 gst_video_frame_copy (&dest_frame, &src_frame);
2554 gst_video_frame_unmap (&src_frame);
2555 gst_video_frame_unmap (&dest_frame);
2557 GST_DEBUG_OBJECT (v4l2object->element, "copy raw bytes");
2558 data = gst_buffer_map (src, &size, NULL, GST_MAP_READ);
2559 gst_buffer_fill (dest, 0, data, size);
2560 gst_buffer_unmap (src, data, 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");
2578 gst_v4l2_object_process_buffer (GstV4l2Object * v4l2object, GstBuffer * buf)
2581 GstBuffer *to_queue = NULL;
2584 meta = GST_META_V4L2_GET (buf);
2586 if (meta && buf->pool == v4l2object->pool) {
2587 GST_LOG_OBJECT (v4l2object->element,
2588 "buffer from our pool, queueing directly");
2592 /* not our buffer */
2593 GST_LOG_OBJECT (v4l2object->element, "buffer not from our pool, copying");
2595 if (!gst_buffer_pool_set_active (v4l2object->pool, TRUE))
2596 goto activate_failed;
2598 ret = gst_buffer_pool_acquire_buffer (v4l2object->pool, &to_queue, NULL);
2599 if (ret != GST_FLOW_OK)
2600 goto acquire_failed;
2602 if (!gst_v4l2_object_copy (v4l2object, to_queue, buf))
2606 if (!gst_v4l2_buffer_pool_process (v4l2object->pool, to_queue))
2610 if (to_queue != buf)
2611 gst_buffer_unref (to_queue);
2618 GST_ERROR_OBJECT (v4l2object->element, "failed to activate bufferpool.");
2619 ret = GST_FLOW_ERROR;
2624 GST_DEBUG_OBJECT (v4l2object->element, "could not get buffer from pool");
2629 /* No Window available to put our image into */
2630 GST_WARNING_OBJECT (v4l2object->element, "could not copy image");
2636 GST_DEBUG_OBJECT (v4l2object->element, "failed to queue buffer");
2637 ret = GST_FLOW_ERROR;