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"
37 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
38 #include "gstv4l2xoverlay.h"
40 #include "gstv4l2colorbalance.h"
42 #include "gst/gst-i18n-plugin.h"
45 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
46 #define GST_CAT_DEFAULT v4l2_debug
49 #define DEFAULT_PROP_DEVICE_NAME NULL
50 #define DEFAULT_PROP_DEVICE_FD -1
51 #define DEFAULT_PROP_FLAGS 0
52 #define DEFAULT_PROP_NORM NULL
53 #define DEFAULT_PROP_CHANNEL NULL
54 #define DEFAULT_PROP_FREQUENCY 0
59 V4L2_STD_OBJECT_PROPS,
63 gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
65 GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
66 static GList *list = NULL;
68 /* well, not perfect, but better than no locking at all.
69 * In the worst case we leak a list node, so who cares? */
70 GST_CLASS_LOCK (GST_OBJECT_CLASS (klass));
73 list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
76 GST_CLASS_UNLOCK (GST_OBJECT_CLASS (klass));
81 static gboolean init = FALSE;
82 static GList *devices = NULL;
86 gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
87 GList ** klass_devices)
94 gchar *device = devices->data;
95 devices = g_list_remove (devices, device);
99 GST_INFO ("Enumerating video4linux devices from udev");
100 client = g_udev_client_new (NULL);
102 GST_WARNING ("Failed to initialize gudev client");
106 item = g_udev_client_query_by_subsystem (client, "video4linux");
108 GUdevDevice *device = item->data;
109 gchar *devnode = g_strdup (g_udev_device_get_device_file (device));
110 gint api = g_udev_device_get_property_as_int (device, "ID_V4L_VERSION");
111 GST_INFO ("Found new device: %s, API: %d", devnode, api);
112 /* Append v4l2 devices only. If api is 0 probably v4l_id has
113 been stripped out of the current udev installation, append
117 ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
119 if ((api == 2 || api == 0)) {
120 devices = g_list_append (devices, devnode);
124 g_object_unref (device);
133 g_object_unref (client);
136 *klass_devices = devices;
140 #endif /* HAVE_GUDEV */
143 gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
144 GList ** klass_devices)
147 const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
151 gchar *device = devices->data;
152 devices = g_list_remove (devices, device);
157 * detect /dev entries
159 for (n = 0; n < 64; n++) {
160 for (base = 0; dev_base[base] != NULL; base++) {
162 gchar *device = g_strdup_printf ("%s%d",
167 * does the /dev/ entry exist at all?
169 if (stat (device, &s) == 0) {
171 * yes: is a device attached?
173 if (S_ISCHR (s.st_mode)) {
175 if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
179 devices = g_list_append (devices, device);
190 *klass_devices = devices;
196 gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
197 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
199 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
204 if (!gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices))
205 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
206 #else /* !HAVE_GUDEV */
207 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
208 #endif /* HAVE_GUDEV */
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
217 gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
218 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
220 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
221 gboolean ret = FALSE;
227 !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
228 #else /* !HAVE_GUDEV */
229 ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
230 #endif /* HAVE_GUDEV */
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
240 gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
243 GValue value = { 0 };
249 array = g_value_array_new (g_list_length (*klass_devices));
250 item = *klass_devices;
251 g_value_init (&value, G_TYPE_STRING);
253 gchar *device = item->data;
255 g_value_set_string (&value, device);
256 g_value_array_append (array, &value);
260 g_value_unset (&value);
266 gst_v4l2_probe_get_values (GstPropertyProbe * probe,
267 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
269 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
270 GValueArray *array = NULL;
274 array = gst_v4l2_class_list_devices (klass, klass_devices);
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
284 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
286 gst_v4l2_device_get_type (void)
288 static GType v4l2_device_type = 0;
290 if (v4l2_device_type == 0) {
291 static const GFlagsValue values[] = {
292 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
293 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
294 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
296 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
297 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
299 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
300 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
306 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
309 return v4l2_device_type;
313 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
314 const char *default_device)
316 g_object_class_install_property (gobject_class, PROP_DEVICE,
317 g_param_spec_string ("device", "Device", "Device location",
318 default_device, G_PARAM_READWRITE));
319 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
320 g_param_spec_string ("device-name", "Device name",
321 "Name of the device", DEFAULT_PROP_DEVICE_NAME, G_PARAM_READABLE));
322 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
323 g_param_spec_int ("device-fd", "File descriptor",
324 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
326 g_object_class_install_property (gobject_class, PROP_FLAGS,
327 g_param_spec_flags ("flags", "Flags", "Device type flags",
328 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS, G_PARAM_READABLE));
332 gst_v4l2_object_new (GstElement * element,
333 enum v4l2_buf_type type,
334 char *default_device,
335 GstV4l2GetInOutFunction get_in_out_func,
336 GstV4l2SetInOutFunction set_in_out_func,
337 GstV4l2UpdateFpsFunction update_fps_func)
339 GstV4l2Object *v4l2object;
342 * some default values
344 v4l2object = g_new0 (GstV4l2Object, 1);
346 v4l2object->type = type;
347 v4l2object->formats = NULL;
349 v4l2object->element = element;
350 v4l2object->get_in_out_func = get_in_out_func;
351 v4l2object->set_in_out_func = set_in_out_func;
352 v4l2object->update_fps_func = update_fps_func;
354 v4l2object->video_fd = -1;
355 v4l2object->poll = gst_poll_new (TRUE);
356 v4l2object->buffer = NULL;
357 v4l2object->videodev = g_strdup (default_device);
359 v4l2object->norms = NULL;
360 v4l2object->channels = NULL;
361 v4l2object->colors = NULL;
363 v4l2object->xwindow_id = 0;
368 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
372 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
374 g_return_if_fail (v4l2object != NULL);
376 if (v4l2object->videodev)
377 g_free (v4l2object->videodev);
379 if (v4l2object->poll)
380 gst_poll_free (v4l2object->poll);
382 if (v4l2object->channel)
383 g_free (v4l2object->channel);
385 if (v4l2object->norm)
386 g_free (v4l2object->norm);
388 if (v4l2object->formats) {
389 gst_v4l2_object_clear_format_list (v4l2object);
397 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
399 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
400 g_slist_free (v4l2object->formats);
401 v4l2object->formats = NULL;
408 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
409 guint prop_id, const GValue * value, GParamSpec * pspec)
413 g_free (v4l2object->videodev);
414 v4l2object->videodev = g_value_dup_string (value);
418 if (GST_V4L2_IS_OPEN (v4l2object)) {
419 GstTuner *tuner = GST_TUNER (v4l2object->element);
420 GstTunerNorm *norm = gst_tuner_find_norm_by_name (tuner,
421 (gchar *) g_value_get_string (value));
424 /* like gst_tuner_set_norm (tuner, norm)
425 without g_object_notify */
426 gst_v4l2_tuner_set_norm (v4l2object, norm);
429 g_free (v4l2object->norm);
430 v4l2object->norm = g_value_dup_string (value);
434 if (GST_V4L2_IS_OPEN (v4l2object)) {
435 GstTuner *tuner = GST_TUNER (v4l2object->element);
436 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
437 (gchar *) g_value_get_string (value));
440 /* like gst_tuner_set_channel (tuner, channel)
441 without g_object_notify */
442 gst_v4l2_tuner_set_channel (v4l2object, channel);
445 g_free (v4l2object->channel);
446 v4l2object->channel = g_value_dup_string (value);
450 if (GST_V4L2_IS_OPEN (v4l2object)) {
451 GstTuner *tuner = GST_TUNER (v4l2object->element);
452 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
455 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
457 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
458 without g_object_notify */
459 gst_v4l2_tuner_set_frequency (v4l2object, channel,
460 g_value_get_ulong (value));
463 v4l2object->frequency = g_value_get_ulong (value);
476 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
477 guint prop_id, GValue * value, GParamSpec * pspec)
481 g_value_set_string (value, v4l2object->videodev);
483 case PROP_DEVICE_NAME:
485 const guchar *new = NULL;
487 if (GST_V4L2_IS_OPEN (v4l2object)) {
488 new = v4l2object->vcap.card;
489 } else if (gst_v4l2_open (v4l2object)) {
490 new = v4l2object->vcap.card;
491 gst_v4l2_close (v4l2object);
493 g_value_set_string (value, (gchar *) new);
498 if (GST_V4L2_IS_OPEN (v4l2object))
499 g_value_set_int (value, v4l2object->video_fd);
501 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
508 if (GST_V4L2_IS_OPEN (v4l2object)) {
509 flags |= v4l2object->vcap.capabilities &
510 (V4L2_CAP_VIDEO_CAPTURE |
511 V4L2_CAP_VIDEO_OUTPUT |
512 V4L2_CAP_VIDEO_OVERLAY |
513 V4L2_CAP_VBI_CAPTURE |
514 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
516 g_value_set_flags (value, flags);
527 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
529 GstTunerNorm *norm = NULL;
530 GstTunerChannel *channel = NULL;
533 if (!GST_IS_TUNER (v4l2object->element))
536 tuner = GST_TUNER (v4l2object->element);
538 if (v4l2object->norm)
539 norm = gst_tuner_find_norm_by_name (tuner, v4l2object->norm);
541 gst_tuner_set_norm (tuner, norm);
544 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
546 g_free (v4l2object->norm);
547 v4l2object->norm = g_strdup (norm->label);
548 gst_tuner_norm_changed (tuner, norm);
552 if (v4l2object->channel)
553 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
555 gst_tuner_set_channel (tuner, channel);
558 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
559 (v4l2object->element)));
561 g_free (v4l2object->channel);
562 v4l2object->channel = g_strdup (channel->label);
563 gst_tuner_channel_changed (tuner, channel);
568 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
569 if (v4l2object->frequency != 0) {
570 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
572 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
573 if (v4l2object->frequency == 0) {
575 gst_tuner_set_frequency (tuner, channel, 1000);
583 gst_v4l2_object_start (GstV4l2Object * v4l2object)
585 if (gst_v4l2_open (v4l2object))
586 gst_v4l2_set_defaults (v4l2object);
590 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
591 gst_v4l2_xoverlay_start (v4l2object);
598 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
600 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
601 gst_v4l2_xoverlay_stop (v4l2object);
604 if (!gst_v4l2_close (v4l2object))
607 if (v4l2object->formats) {
608 gst_v4l2_object_clear_format_list (v4l2object);
616 * common format / caps utilities:
624 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
625 /* from Linux 2.6.15 videodev2.h */
626 {V4L2_PIX_FMT_RGB332, TRUE},
627 {V4L2_PIX_FMT_RGB555, TRUE},
628 {V4L2_PIX_FMT_RGB565, TRUE},
629 {V4L2_PIX_FMT_RGB555X, TRUE},
630 {V4L2_PIX_FMT_RGB565X, TRUE},
631 {V4L2_PIX_FMT_BGR24, TRUE},
632 {V4L2_PIX_FMT_RGB24, TRUE},
633 {V4L2_PIX_FMT_BGR32, TRUE},
634 {V4L2_PIX_FMT_RGB32, TRUE},
635 {V4L2_PIX_FMT_GREY, TRUE},
636 {V4L2_PIX_FMT_YVU410, TRUE},
637 {V4L2_PIX_FMT_YVU420, TRUE},
638 {V4L2_PIX_FMT_YUYV, TRUE},
639 {V4L2_PIX_FMT_UYVY, TRUE},
640 {V4L2_PIX_FMT_YUV422P, TRUE},
641 {V4L2_PIX_FMT_YUV411P, TRUE},
642 {V4L2_PIX_FMT_Y41P, TRUE},
644 /* two planes -- one Y, one Cr + Cb interleaved */
645 {V4L2_PIX_FMT_NV12, TRUE},
646 {V4L2_PIX_FMT_NV21, TRUE},
648 /* The following formats are not defined in the V4L2 specification */
649 {V4L2_PIX_FMT_YUV410, TRUE},
650 {V4L2_PIX_FMT_YUV420, TRUE},
651 {V4L2_PIX_FMT_YYUV, TRUE},
652 {V4L2_PIX_FMT_HI240, TRUE},
654 /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
655 #ifdef V4L2_PIX_FMT_SBGGR8
656 {V4L2_PIX_FMT_SBGGR8, TRUE},
659 /* compressed formats */
660 {V4L2_PIX_FMT_MJPEG, TRUE},
661 {V4L2_PIX_FMT_JPEG, TRUE},
662 {V4L2_PIX_FMT_DV, TRUE},
663 {V4L2_PIX_FMT_MPEG, FALSE},
665 /* Vendor-specific formats */
666 {V4L2_PIX_FMT_WNVA, TRUE},
668 #ifdef V4L2_PIX_FMT_SN9C10X
669 {V4L2_PIX_FMT_SN9C10X, TRUE},
671 #ifdef V4L2_PIX_FMT_PWC1
672 {V4L2_PIX_FMT_PWC1, TRUE},
674 #ifdef V4L2_PIX_FMT_PWC2
675 {V4L2_PIX_FMT_PWC2, TRUE},
677 #ifdef V4L2_PIX_FMT_YVYU
678 {V4L2_PIX_FMT_YVYU, TRUE},
682 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
685 static struct v4l2_fmtdesc *
686 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
689 struct v4l2_fmtdesc *fmt;
695 walk = gst_v4l2_object_get_format_list (v4l2object);
697 fmt = (struct v4l2_fmtdesc *) walk->data;
698 if (fmt->pixelformat == fourcc)
700 /* special case for jpeg */
701 if ((fmt->pixelformat == V4L2_PIX_FMT_MJPEG && fourcc == V4L2_PIX_FMT_JPEG)
702 || (fmt->pixelformat == V4L2_PIX_FMT_JPEG
703 && fourcc == V4L2_PIX_FMT_MJPEG)) {
706 walk = g_slist_next (walk);
714 /* complete made up ranking, the values themselves are meaningless */
715 #define YUV_BASE_RANK 1000
716 #define JPEG_BASE_RANK 500
717 #define DV_BASE_RANK 200
718 #define RGB_BASE_RANK 100
719 #define YUV_ODD_BASE_RANK 50
720 #define RGB_ODD_BASE_RANK 25
721 #define BAYER_BASE_RANK 15
722 #define S910_BASE_RANK 10
723 #define GREY_BASE_RANK 5
724 #define PWC_BASE_RANK 1
726 /* This flag is already used by libv4l2 although
727 * it was added to the Linux kernel in 2.6.32
729 #ifndef V4L2_FMT_FLAG_EMULATED
730 #define V4L2_FMT_FLAG_EMULATED 0x0002
734 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
736 guint32 fourcc = fmt->pixelformat;
737 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
741 case V4L2_PIX_FMT_MJPEG:
742 rank = JPEG_BASE_RANK;
744 case V4L2_PIX_FMT_JPEG:
745 rank = JPEG_BASE_RANK + 1;
747 case V4L2_PIX_FMT_MPEG: /* MPEG */
748 rank = JPEG_BASE_RANK + 2;
751 case V4L2_PIX_FMT_RGB332:
752 case V4L2_PIX_FMT_RGB555:
753 case V4L2_PIX_FMT_RGB555X:
754 case V4L2_PIX_FMT_RGB565:
755 case V4L2_PIX_FMT_RGB565X:
756 rank = RGB_ODD_BASE_RANK;
759 case V4L2_PIX_FMT_RGB24:
760 case V4L2_PIX_FMT_BGR24:
761 rank = RGB_BASE_RANK - 1;
764 case V4L2_PIX_FMT_RGB32:
765 case V4L2_PIX_FMT_BGR32:
766 rank = RGB_BASE_RANK;
769 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
770 rank = GREY_BASE_RANK;
773 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
774 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
775 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
776 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
777 rank = YUV_ODD_BASE_RANK;
780 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
781 rank = YUV_BASE_RANK + 3;
783 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
784 rank = YUV_BASE_RANK + 2;
786 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
787 rank = YUV_BASE_RANK + 7;
789 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
790 rank = YUV_BASE_RANK + 10;
792 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
793 rank = YUV_BASE_RANK + 6;
795 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
796 rank = YUV_BASE_RANK + 9;
798 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
799 rank = YUV_BASE_RANK + 5;
801 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
802 rank = YUV_BASE_RANK + 4;
804 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
805 rank = YUV_BASE_RANK + 8;
808 case V4L2_PIX_FMT_DV:
812 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
816 #ifdef V4L2_PIX_FMT_SBGGR8
817 case V4L2_PIX_FMT_SBGGR8:
818 rank = BAYER_BASE_RANK;
822 #ifdef V4L2_PIX_FMT_SN9C10X
823 case V4L2_PIX_FMT_SN9C10X:
824 rank = S910_BASE_RANK;
828 #ifdef V4L2_PIX_FMT_PWC1
829 case V4L2_PIX_FMT_PWC1:
830 rank = PWC_BASE_RANK;
833 #ifdef V4L2_PIX_FMT_PWC2
834 case V4L2_PIX_FMT_PWC2:
835 rank = PWC_BASE_RANK;
844 /* All ranks are below 1<<15 so a shift by 15
845 * will a) make all non-emulated formats larger
846 * than emulated and b) will not overflow
857 format_cmp_func (gconstpointer a, gconstpointer b)
859 const struct v4l2_fmtdesc *fa = a;
860 const struct v4l2_fmtdesc *fb = b;
862 if (fa->pixelformat == fb->pixelformat)
865 return gst_v4l2_object_format_get_rank (fa) -
866 gst_v4l2_object_format_get_rank (fb);
869 /******************************************************
870 * gst_v4l2_object_fill_format_list():
871 * create list of supported capture formats
872 * return value: TRUE on success, FALSE on error
873 ******************************************************/
875 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object)
878 struct v4l2_fmtdesc *format;
880 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
882 /* format enumeration */
884 format = g_new0 (struct v4l2_fmtdesc, 1);
887 format->type = v4l2object->type;
889 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
890 if (errno == EINVAL) {
892 break; /* end of enumeration */
898 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
899 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
900 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
901 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
902 format->description);
903 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
904 GST_FOURCC_ARGS (format->pixelformat));
906 /* sort formats according to our preference; we do this, because caps
907 * are probed in the order the formats are in the list, and the order of
908 * formats in the final probed caps matters for things like fixation */
909 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
910 (GCompareFunc) format_cmp_func);
913 GST_DEBUG_OBJECT (v4l2object->element, "got %d format(s)", n);
920 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
921 (_("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)));
928 * Get the list of supported capture formats, a list of
929 * <code>struct v4l2_fmtdesc</code>.
932 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
934 if (!v4l2object->formats)
935 gst_v4l2_object_fill_format_list (v4l2object);
936 return v4l2object->formats;
941 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
943 GstStructure *structure = NULL;
946 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
947 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
948 structure = gst_structure_new ("image/jpeg", NULL);
950 case V4L2_PIX_FMT_RGB332:
951 case V4L2_PIX_FMT_RGB555:
952 case V4L2_PIX_FMT_RGB555X:
953 case V4L2_PIX_FMT_RGB565:
954 case V4L2_PIX_FMT_RGB565X:
955 case V4L2_PIX_FMT_RGB24:
956 case V4L2_PIX_FMT_BGR24:
957 case V4L2_PIX_FMT_RGB32:
958 case V4L2_PIX_FMT_BGR32:{
959 guint depth = 0, bpp = 0;
963 guint32 r_mask = 0, b_mask = 0, g_mask = 0;
966 case V4L2_PIX_FMT_RGB332:
968 endianness = G_BYTE_ORDER; /* 'like, whatever' */
973 case V4L2_PIX_FMT_RGB555:
974 case V4L2_PIX_FMT_RGB555X:
978 fourcc == V4L2_PIX_FMT_RGB555X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
983 case V4L2_PIX_FMT_RGB565:
984 case V4L2_PIX_FMT_RGB565X:
987 fourcc == V4L2_PIX_FMT_RGB565X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
992 case V4L2_PIX_FMT_RGB24:
994 endianness = G_BIG_ENDIAN;
999 case V4L2_PIX_FMT_BGR24:
1001 endianness = G_BIG_ENDIAN;
1006 case V4L2_PIX_FMT_RGB32:
1008 endianness = G_BIG_ENDIAN;
1009 r_mask = 0xff000000;
1010 g_mask = 0x00ff0000;
1011 b_mask = 0x0000ff00;
1013 case V4L2_PIX_FMT_BGR32:
1015 endianness = G_BIG_ENDIAN;
1016 r_mask = 0x000000ff;
1017 g_mask = 0x0000ff00;
1018 b_mask = 0x00ff0000;
1021 g_assert_not_reached ();
1024 structure = gst_structure_new ("video/x-raw-rgb",
1025 "bpp", G_TYPE_INT, bpp,
1026 "depth", G_TYPE_INT, depth,
1027 "red_mask", G_TYPE_INT, r_mask,
1028 "green_mask", G_TYPE_INT, g_mask,
1029 "blue_mask", G_TYPE_INT, b_mask,
1030 "endianness", G_TYPE_INT, endianness, NULL);
1033 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1034 structure = gst_structure_new ("video/x-raw-gray",
1035 "bpp", G_TYPE_INT, 8, NULL);
1037 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1038 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1039 /* FIXME: get correct fourccs here */
1041 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1042 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1043 case V4L2_PIX_FMT_YVU410:
1044 case V4L2_PIX_FMT_YUV410:
1045 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1046 case V4L2_PIX_FMT_YUYV:
1047 case V4L2_PIX_FMT_YVU420:
1048 case V4L2_PIX_FMT_UYVY:
1049 case V4L2_PIX_FMT_Y41P:
1050 case V4L2_PIX_FMT_YUV422P:
1051 #ifdef V4L2_PIX_FMT_YVYU
1052 case V4L2_PIX_FMT_YVYU:
1054 case V4L2_PIX_FMT_YUV411P:{
1058 case V4L2_PIX_FMT_NV12:
1059 fcc = GST_MAKE_FOURCC ('N', 'V', '1', '2');
1061 case V4L2_PIX_FMT_NV21:
1062 fcc = GST_MAKE_FOURCC ('N', 'V', '2', '1');
1064 case V4L2_PIX_FMT_YVU410:
1065 fcc = GST_MAKE_FOURCC ('Y', 'V', 'U', '9');
1067 case V4L2_PIX_FMT_YUV410:
1068 fcc = GST_MAKE_FOURCC ('Y', 'U', 'V', '9');
1070 case V4L2_PIX_FMT_YUV420:
1071 fcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
1073 case V4L2_PIX_FMT_YUYV:
1074 fcc = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
1076 case V4L2_PIX_FMT_YVU420:
1077 fcc = GST_MAKE_FOURCC ('Y', 'V', '1', '2');
1079 case V4L2_PIX_FMT_UYVY:
1080 fcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
1082 case V4L2_PIX_FMT_Y41P:
1083 fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'P');
1085 case V4L2_PIX_FMT_YUV411P:
1086 fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
1088 case V4L2_PIX_FMT_YUV422P:
1089 fcc = GST_MAKE_FOURCC ('Y', '4', '2', 'B');
1091 #ifdef V4L2_PIX_FMT_YVYU
1092 case V4L2_PIX_FMT_YVYU:
1093 fcc = GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U');
1097 g_assert_not_reached ();
1100 structure = gst_structure_new ("video/x-raw-yuv",
1101 "format", GST_TYPE_FOURCC, fcc, NULL);
1104 case V4L2_PIX_FMT_DV:
1106 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1109 case V4L2_PIX_FMT_MPEG: /* MPEG */
1110 structure = gst_structure_new ("video/mpegts", NULL);
1112 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1114 #ifdef V4L2_PIX_FMT_SBGGR8
1115 case V4L2_PIX_FMT_SBGGR8:
1116 structure = gst_structure_new ("video/x-raw-bayer", NULL);
1119 #ifdef V4L2_PIX_FMT_SN9C10X
1120 case V4L2_PIX_FMT_SN9C10X:
1121 structure = gst_structure_new ("video/x-sonix", NULL);
1124 #ifdef V4L2_PIX_FMT_PWC1
1125 case V4L2_PIX_FMT_PWC1:
1126 structure = gst_structure_new ("video/x-pwc1", NULL);
1129 #ifdef V4L2_PIX_FMT_PWC2
1130 case V4L2_PIX_FMT_PWC2:
1131 structure = gst_structure_new ("video/x-pwc2", NULL);
1135 GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
1136 fourcc, GST_FOURCC_ARGS (fourcc));
1146 gst_v4l2_object_get_all_caps (void)
1148 static GstCaps *caps = NULL;
1151 GstStructure *structure;
1155 caps = gst_caps_new_empty ();
1156 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1158 gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
1160 if (gst_v4l2_formats[i].dimensions) {
1161 gst_structure_set (structure,
1162 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1163 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1164 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
1166 gst_caps_append_structure (caps, structure);
1171 return gst_caps_ref (caps);
1175 /* collect data for the given caps
1176 * @caps: given input caps
1177 * @format: location for the v4l format
1178 * @w/@h: location for width and height
1179 * @fps_n/@fps_d: location for framerate
1180 * @size: location for expected size of the frame or 0 if unknown
1183 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1184 struct v4l2_fmtdesc ** format, gint * w, gint * h, guint * fps_n,
1185 guint * fps_d, guint * size)
1187 GstStructure *structure;
1188 const GValue *framerate;
1190 const gchar *mimetype;
1193 /* default unknown values */
1197 structure = gst_caps_get_structure (caps, 0);
1199 mimetype = gst_structure_get_name (structure);
1201 if (strcmp (mimetype, "video/mpegts") == 0) {
1202 fourcc = V4L2_PIX_FMT_MPEG;
1209 if (!gst_structure_get_int (structure, "width", w))
1212 if (!gst_structure_get_int (structure, "height", h))
1215 framerate = gst_structure_get_value (structure, "framerate");
1219 *fps_n = gst_value_get_fraction_numerator (framerate);
1220 *fps_d = gst_value_get_fraction_denominator (framerate);
1222 if (!strcmp (mimetype, "video/x-raw-yuv")) {
1223 gst_structure_get_fourcc (structure, "format", &fourcc);
1226 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
1227 case GST_MAKE_FOURCC ('I', 'Y', 'U', 'V'):
1228 fourcc = V4L2_PIX_FMT_YUV420;
1229 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1230 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1232 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
1233 fourcc = V4L2_PIX_FMT_YUYV;
1234 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1236 case GST_MAKE_FOURCC ('Y', '4', '1', 'P'):
1237 fourcc = V4L2_PIX_FMT_Y41P;
1238 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1240 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
1241 fourcc = V4L2_PIX_FMT_UYVY;
1242 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1244 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
1245 fourcc = V4L2_PIX_FMT_YVU420;
1246 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1247 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1249 case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
1250 fourcc = V4L2_PIX_FMT_YUV411P;
1251 outsize = GST_ROUND_UP_4 (*w) * *h;
1252 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 4) * *h);
1254 case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
1255 fourcc = V4L2_PIX_FMT_YUV422P;
1256 outsize = GST_ROUND_UP_4 (*w) * *h;
1257 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * *h);
1259 case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
1260 fourcc = V4L2_PIX_FMT_NV12;
1261 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1262 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1264 case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
1265 fourcc = V4L2_PIX_FMT_NV21;
1266 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1267 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1269 #ifdef V4L2_PIX_FMT_YVYU
1270 case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
1271 fourcc = V4L2_PIX_FMT_YVYU;
1272 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1276 } else if (!strcmp (mimetype, "video/x-raw-rgb")) {
1277 gint depth, endianness, r_mask;
1279 gst_structure_get_int (structure, "depth", &depth);
1280 gst_structure_get_int (structure, "endianness", &endianness);
1281 gst_structure_get_int (structure, "red_mask", &r_mask);
1285 fourcc = V4L2_PIX_FMT_RGB332;
1288 fourcc = (endianness == G_LITTLE_ENDIAN) ?
1289 V4L2_PIX_FMT_RGB555 : V4L2_PIX_FMT_RGB555X;
1292 fourcc = (endianness == G_LITTLE_ENDIAN) ?
1293 V4L2_PIX_FMT_RGB565 : V4L2_PIX_FMT_RGB565X;
1296 fourcc = (r_mask == 0xFF) ? V4L2_PIX_FMT_BGR24 : V4L2_PIX_FMT_RGB24;
1299 fourcc = (r_mask == 0xFF) ? V4L2_PIX_FMT_BGR32 : V4L2_PIX_FMT_RGB32;
1302 } else if (strcmp (mimetype, "video/x-dv") == 0) {
1303 fourcc = V4L2_PIX_FMT_DV;
1304 } else if (strcmp (mimetype, "image/jpeg") == 0) {
1305 fourcc = V4L2_PIX_FMT_JPEG;
1306 #ifdef V4L2_PIX_FMT_SBGGR8
1307 } else if (strcmp (mimetype, "video/x-raw-bayer") == 0) {
1308 fourcc = V4L2_PIX_FMT_SBGGR8;
1310 #ifdef V4L2_PIX_FMT_SN9C10X
1311 } else if (strcmp (mimetype, "video/x-sonix") == 0) {
1312 fourcc = V4L2_PIX_FMT_SN9C10X;
1314 #ifdef V4L2_PIX_FMT_PWC1
1315 } else if (strcmp (mimetype, "video/x-pwc1") == 0) {
1316 fourcc = V4L2_PIX_FMT_PWC1;
1318 #ifdef V4L2_PIX_FMT_PWC2
1319 } else if (strcmp (mimetype, "video/x-pwc2") == 0) {
1320 fourcc = V4L2_PIX_FMT_PWC2;
1322 } else if (strcmp (mimetype, "video/x-raw-gray") == 0) {
1323 fourcc = V4L2_PIX_FMT_GREY;
1330 *format = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1338 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1339 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1340 static GstStructure *
1341 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1342 guint32 pixelformat,
1343 guint32 width, guint32 height, const GstStructure * template)
1345 gint fd = v4l2object->video_fd;
1346 struct v4l2_frmivalenum ival;
1349 GValue rates = { 0, };
1351 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1353 ival.pixel_format = pixelformat;
1355 ival.height = height;
1357 GST_LOG_OBJECT (v4l2object->element,
1358 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1359 GST_FOURCC_ARGS (pixelformat));
1361 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1362 * fraction to get framerate */
1363 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1364 goto enum_frameintervals_failed;
1366 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1367 GValue rate = { 0, };
1369 g_value_init (&rates, GST_TYPE_LIST);
1370 g_value_init (&rate, GST_TYPE_FRACTION);
1373 num = ival.discrete.numerator;
1374 denom = ival.discrete.denominator;
1376 if (num > G_MAXINT || denom > G_MAXINT) {
1377 /* let us hope we don't get here... */
1382 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1385 /* swap to get the framerate */
1386 gst_value_set_fraction (&rate, denom, num);
1387 gst_value_list_append_value (&rates, &rate);
1390 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1391 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1392 GValue min = { 0, };
1393 GValue step = { 0, };
1394 GValue max = { 0, };
1395 gboolean added = FALSE;
1396 guint32 minnum, mindenom;
1397 guint32 maxnum, maxdenom;
1399 g_value_init (&rates, GST_TYPE_LIST);
1401 g_value_init (&min, GST_TYPE_FRACTION);
1402 g_value_init (&step, GST_TYPE_FRACTION);
1403 g_value_init (&max, GST_TYPE_FRACTION);
1406 minnum = ival.stepwise.min.numerator;
1407 mindenom = ival.stepwise.min.denominator;
1408 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1412 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1414 gst_value_set_fraction (&min, minnum, mindenom);
1417 maxnum = ival.stepwise.max.numerator;
1418 maxdenom = ival.stepwise.max.denominator;
1419 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1424 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1426 gst_value_set_fraction (&max, maxnum, maxdenom);
1429 num = ival.stepwise.step.numerator;
1430 denom = ival.stepwise.step.denominator;
1431 if (num > G_MAXINT || denom > G_MAXINT) {
1436 if (num == 0 || denom == 0) {
1437 /* in this case we have a wrong fraction or no step, set the step to max
1438 * so that we only add the min value in the loop below */
1443 /* since we only have gst_value_fraction_subtract and not add, negate the
1445 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1447 gst_value_set_fraction (&step, -num, denom);
1449 while (gst_value_compare (&min, &max) <= 0) {
1450 GValue rate = { 0, };
1452 num = gst_value_get_fraction_numerator (&min);
1453 denom = gst_value_get_fraction_denominator (&min);
1454 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1457 /* invert to get the framerate */
1458 g_value_init (&rate, GST_TYPE_FRACTION);
1459 gst_value_set_fraction (&rate, denom, num);
1460 gst_value_list_append_value (&rates, &rate);
1463 /* we're actually adding because step was negated above. This is because
1464 * there is no _add function... */
1465 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1466 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1471 /* no range was added, leave the default range from the template */
1472 GST_WARNING_OBJECT (v4l2object->element,
1473 "no range added, leaving default");
1474 g_value_unset (&rates);
1476 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1477 guint32 maxnum, maxdenom;
1479 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1481 num = ival.stepwise.min.numerator;
1482 denom = ival.stepwise.min.denominator;
1483 if (num > G_MAXINT || denom > G_MAXINT) {
1488 maxnum = ival.stepwise.max.numerator;
1489 maxdenom = ival.stepwise.max.denominator;
1490 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1495 GST_LOG_OBJECT (v4l2object->element,
1496 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
1499 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1505 s = gst_structure_copy (template);
1506 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
1507 "height", G_TYPE_INT, (gint) height, NULL);
1509 if (G_IS_VALUE (&rates)) {
1510 /* only change the framerate on the template when we have a valid probed new
1512 gst_structure_set_value (s, "framerate", &rates);
1513 g_value_unset (&rates);
1515 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
1521 enum_frameintervals_failed:
1523 GST_DEBUG_OBJECT (v4l2object->element,
1524 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
1525 GST_FOURCC_ARGS (pixelformat), width, height);
1530 /* I don't see how this is actually an error, we ignore the format then */
1531 GST_WARNING_OBJECT (v4l2object->element,
1532 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
1533 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
1537 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
1539 #ifdef VIDIOC_ENUM_FRAMESIZES
1541 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
1545 gst_structure_get_int (s1, "width", &w1);
1546 gst_structure_get_int (s1, "height", &h1);
1547 gst_structure_get_int (s2, "width", &w2);
1548 gst_structure_get_int (s2, "height", &h2);
1550 /* I think it's safe to assume that this won't overflow for a while */
1551 return ((w2 * h2) - (w1 * h1));
1556 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1557 guint32 pixelformat, gint * width, gint * height);
1560 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
1561 guint32 pixelformat, const GstStructure * template)
1563 GstCaps *ret = gst_caps_new_empty ();
1566 #ifdef VIDIOC_ENUM_FRAMESIZES
1567 gint fd = v4l2object->video_fd;
1568 struct v4l2_frmsizeenum size;
1569 GList *results = NULL;
1572 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1573 return gst_caps_new_simple ("video/mpegts", NULL);
1575 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
1577 size.pixel_format = pixelformat;
1579 GST_DEBUG_OBJECT (v4l2object->element, "Enumerating frame sizes");
1581 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
1582 goto enum_framesizes_failed;
1584 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1586 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
1587 size.discrete.width, size.discrete.height);
1589 w = MIN (size.discrete.width, G_MAXINT);
1590 h = MIN (size.discrete.height, G_MAXINT);
1594 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1595 pixelformat, w, h, template);
1598 results = g_list_prepend (results, tmp);
1602 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
1603 GST_DEBUG_OBJECT (v4l2object->element,
1604 "done iterating discrete frame sizes");
1605 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
1606 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
1607 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1608 size.stepwise.min_width);
1609 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1610 size.stepwise.min_height);
1611 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1612 size.stepwise.max_width);
1613 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1614 size.stepwise.max_height);
1615 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
1616 size.stepwise.step_width);
1617 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
1618 size.stepwise.step_height);
1620 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
1621 w < size.stepwise.max_width && h < size.stepwise.max_height;
1622 w += size.stepwise.step_width, h += size.stepwise.step_height) {
1623 if (w == 0 || h == 0)
1627 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1628 pixelformat, w, h, template);
1631 results = g_list_prepend (results, tmp);
1633 GST_DEBUG_OBJECT (v4l2object->element,
1634 "done iterating stepwise frame sizes");
1635 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
1638 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
1639 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1640 size.stepwise.min_width);
1641 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1642 size.stepwise.min_height);
1643 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1644 size.stepwise.max_width);
1645 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1646 size.stepwise.max_height);
1648 w = MAX (size.stepwise.min_width, 1);
1649 h = MAX (size.stepwise.min_height, 1);
1650 maxw = MIN (size.stepwise.max_width, G_MAXINT);
1651 maxh = MIN (size.stepwise.max_height, G_MAXINT);
1654 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
1657 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
1658 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
1661 /* no point using the results list here, since there's only one struct */
1662 gst_caps_append_structure (ret, tmp);
1668 /* we use an intermediary list to store and then sort the results of the
1669 * probing because we can't make any assumptions about the order in which
1670 * the driver will give us the sizes, but we want the final caps to contain
1671 * the results starting with the highest resolution and having the lowest
1672 * resolution last, since order in caps matters for things like fixation. */
1673 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
1674 while (results != NULL) {
1675 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
1676 results = g_list_delete_link (results, results);
1679 if (gst_caps_is_empty (ret))
1680 goto enum_framesizes_no_results;
1685 enum_framesizes_failed:
1687 /* I don't see how this is actually an error */
1688 GST_DEBUG_OBJECT (v4l2object->element,
1689 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
1690 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
1691 goto default_frame_sizes;
1693 enum_framesizes_no_results:
1695 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
1696 * question doesn't actually support it yet */
1697 GST_DEBUG_OBJECT (v4l2object->element,
1698 "No results for pixelformat %" GST_FOURCC_FORMAT
1699 " enumerating frame sizes, trying fallback",
1700 GST_FOURCC_ARGS (pixelformat));
1701 goto default_frame_sizes;
1705 GST_WARNING_OBJECT (v4l2object->element,
1706 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
1707 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
1708 goto default_frame_sizes;
1710 default_frame_sizes:
1711 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
1713 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
1715 /* This code is for Linux < 2.6.19 */
1717 max_w = max_h = GST_V4L2_MAX_SIZE;
1718 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
1720 GST_WARNING_OBJECT (v4l2object->element,
1721 "Could not probe minimum capture size for pixelformat %"
1722 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1724 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
1726 GST_WARNING_OBJECT (v4l2object->element,
1727 "Could not probe maximum capture size for pixelformat %"
1728 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1731 /* Since we can't get framerate directly, try to use the current norm */
1732 if (v4l2object->norm && v4l2object->norms) {
1736 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
1737 norm = (GstTunerNorm *) norms->data;
1738 if (!strcmp (norm->label, v4l2object->norm))
1741 /* If it's possible, set framerate to that (discrete) value */
1743 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
1744 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
1748 tmp = gst_structure_copy (template);
1750 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
1753 /* if norm can't be used, copy the template framerate */
1754 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
1759 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
1761 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
1764 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
1766 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
1768 gst_caps_append_structure (ret, tmp);
1775 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1776 guint32 pixelformat, gint * width, gint * height)
1778 struct v4l2_format fmt;
1782 g_return_val_if_fail (width != NULL, FALSE);
1783 g_return_val_if_fail (height != NULL, FALSE);
1785 GST_LOG_OBJECT (v4l2object->element,
1786 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
1787 *width, *height, GST_FOURCC_ARGS (pixelformat));
1789 fd = v4l2object->video_fd;
1791 /* get size delimiters */
1792 memset (&fmt, 0, sizeof (fmt));
1793 fmt.type = v4l2object->type;
1794 fmt.fmt.pix.width = *width;
1795 fmt.fmt.pix.height = *height;
1796 fmt.fmt.pix.pixelformat = pixelformat;
1797 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1799 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
1800 if (r < 0 && errno == EINVAL) {
1801 /* try again with progressive video */
1802 fmt.fmt.pix.width = *width;
1803 fmt.fmt.pix.height = *height;
1804 fmt.fmt.pix.pixelformat = pixelformat;
1805 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1806 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
1810 /* The driver might not implement TRY_FMT, in which case we will try
1812 if (errno != ENOTTY)
1815 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
1816 be, because we're still probing */
1817 if (GST_V4L2_IS_ACTIVE (v4l2object))
1820 GST_LOG_OBJECT (v4l2object->element,
1821 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
1823 fmt.fmt.pix.width = *width;
1824 fmt.fmt.pix.height = *height;
1826 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
1827 if (r < 0 && errno == EINVAL) {
1828 /* try again with progressive video */
1829 fmt.fmt.pix.width = *width;
1830 fmt.fmt.pix.height = *height;
1831 fmt.fmt.pix.pixelformat = pixelformat;
1832 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1833 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
1840 GST_LOG_OBJECT (v4l2object->element,
1841 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
1843 *width = fmt.fmt.pix.width;
1844 *height = fmt.fmt.pix.height;
1851 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, guint32 pixelformat,
1852 guint32 width, guint32 height)
1854 gint fd = v4l2object->video_fd;
1855 struct v4l2_format format;
1857 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
1858 "%" GST_FOURCC_FORMAT, width, height, GST_FOURCC_ARGS (pixelformat));
1860 GST_V4L2_CHECK_OPEN (v4l2object);
1861 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
1863 memset (&format, 0x00, sizeof (struct v4l2_format));
1864 format.type = v4l2object->type;
1866 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1869 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
1870 goto get_fmt_failed;
1872 format.type = v4l2object->type;
1873 format.fmt.pix.width = width;
1874 format.fmt.pix.height = height;
1875 format.fmt.pix.pixelformat = pixelformat;
1876 /* request whole frames; change when gstreamer supports interlaced video
1877 * (INTERLACED mode returns frames where the fields have already been
1878 * combined, there are other modes for requesting fields individually) */
1879 format.fmt.pix.field = V4L2_FIELD_INTERLACED;
1881 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0) {
1882 if (errno != EINVAL)
1883 goto set_fmt_failed;
1885 GST_DEBUG_OBJECT (v4l2object->element, "trying again...");
1887 /* try again with progressive video */
1888 format.fmt.pix.width = width;
1889 format.fmt.pix.height = height;
1890 format.fmt.pix.pixelformat = pixelformat;
1891 format.fmt.pix.field = V4L2_FIELD_NONE;
1892 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
1893 goto set_fmt_failed;
1896 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
1897 goto invalid_dimensions;
1899 if (format.fmt.pix.pixelformat != pixelformat)
1900 goto invalid_pixelformat;
1907 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1908 (_("Device '%s' does not support video capture"),
1909 v4l2object->videodev),
1910 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
1915 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1916 (_("Device '%s' cannot capture at %dx%d"),
1917 v4l2object->videodev, width, height),
1918 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
1919 GST_FOURCC_ARGS (pixelformat), width, height, g_strerror (errno)));
1924 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1925 (_("Device '%s' cannot capture at %dx%d"),
1926 v4l2object->videodev, width, height),
1927 ("Tried to capture at %dx%d, but device returned size %dx%d",
1928 width, height, format.fmt.pix.width, format.fmt.pix.height));
1931 invalid_pixelformat:
1933 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1934 (_("Device '%s' cannot capture in the specified format"),
1935 v4l2object->videodev),
1936 ("Tried to capture in %" GST_FOURCC_FORMAT
1937 ", but device returned format" " %" GST_FOURCC_FORMAT,
1938 GST_FOURCC_ARGS (pixelformat),
1939 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
1945 gst_v4l2_object_start_streaming (GstV4l2Object * v4l2object)
1947 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_STREAMON,
1948 &(v4l2object->type)) < 0) {
1949 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ,
1950 (_("Error starting streaming on device '%s'."), v4l2object->videodev),
1958 gst_v4l2_object_stop_streaming (GstV4l2Object * v4l2object)
1960 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_STREAMOFF,
1961 &(v4l2object->type)) < 0) {
1962 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ,
1963 (_("Error stopping streaming on device '%s'."), v4l2object->videodev),