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 /* videodev2.h is not versioned and we can't easily check for the presence
45 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
46 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
47 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
48 #define V4L2_FIELD_INTERLACED_TB 8
49 #define V4L2_FIELD_INTERLACED_BT 9
52 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
53 #define GST_CAT_DEFAULT v4l2_debug
56 #define DEFAULT_PROP_DEVICE_NAME NULL
57 #define DEFAULT_PROP_DEVICE_FD -1
58 #define DEFAULT_PROP_FLAGS 0
59 #define DEFAULT_PROP_NORM NULL
60 #define DEFAULT_PROP_CHANNEL NULL
61 #define DEFAULT_PROP_FREQUENCY 0
66 V4L2_STD_OBJECT_PROPS,
70 gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
72 GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
73 static GList *list = NULL;
75 /* well, not perfect, but better than no locking at all.
76 * In the worst case we leak a list node, so who cares? */
77 GST_CLASS_LOCK (GST_OBJECT_CLASS (klass));
80 list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
83 GST_CLASS_UNLOCK (GST_OBJECT_CLASS (klass));
88 static gboolean init = FALSE;
89 static GList *devices = NULL;
93 gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
94 GList ** klass_devices)
96 GUdevClient *client = NULL;
101 gchar *device = devices->data;
102 devices = g_list_remove (devices, device);
106 GST_INFO ("Enumerating video4linux devices from udev");
107 client = g_udev_client_new (NULL);
109 GST_WARNING ("Failed to initialize gudev client");
113 item = g_udev_client_query_by_subsystem (client, "video4linux");
115 GUdevDevice *device = item->data;
116 gchar *devnode = g_strdup (g_udev_device_get_device_file (device));
117 gint api = g_udev_device_get_property_as_int (device, "ID_V4L_VERSION");
118 GST_INFO ("Found new device: %s, API: %d", devnode, api);
119 /* Append v4l2 devices only. If api is 0 probably v4l_id has
120 been stripped out of the current udev installation, append
124 ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
126 if ((api == 2 || api == 0)) {
127 devices = g_list_append (devices, devnode);
131 g_object_unref (device);
140 g_object_unref (client);
143 *klass_devices = devices;
147 #endif /* HAVE_GUDEV */
150 gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
151 GList ** klass_devices)
154 const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
158 gchar *device = devices->data;
159 devices = g_list_remove (devices, device);
164 * detect /dev entries
166 for (n = 0; n < 64; n++) {
167 for (base = 0; dev_base[base] != NULL; base++) {
169 gchar *device = g_strdup_printf ("%s%d",
174 * does the /dev/ entry exist at all?
176 if (stat (device, &s) == 0) {
178 * yes: is a device attached?
180 if (S_ISCHR (s.st_mode)) {
182 if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
186 devices = g_list_append (devices, device);
197 *klass_devices = devices;
203 gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
204 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
206 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
211 if (!gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices))
212 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
213 #else /* !HAVE_GUDEV */
214 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
215 #endif /* HAVE_GUDEV */
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
224 gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
225 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
227 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
228 gboolean ret = FALSE;
234 !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
235 #else /* !HAVE_GUDEV */
236 ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
237 #endif /* HAVE_GUDEV */
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
247 gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
250 GValue value = { 0 };
256 array = g_value_array_new (g_list_length (*klass_devices));
257 item = *klass_devices;
258 g_value_init (&value, G_TYPE_STRING);
260 gchar *device = item->data;
262 g_value_set_string (&value, device);
263 g_value_array_append (array, &value);
267 g_value_unset (&value);
273 gst_v4l2_probe_get_values (GstPropertyProbe * probe,
274 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
276 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
277 GValueArray *array = NULL;
281 array = gst_v4l2_class_list_devices (klass, klass_devices);
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
291 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
293 gst_v4l2_device_get_type (void)
295 static GType v4l2_device_type = 0;
297 if (v4l2_device_type == 0) {
298 static const GFlagsValue values[] = {
299 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
300 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
301 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
303 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
304 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
306 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
307 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
313 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
316 return v4l2_device_type;
320 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
321 const char *default_device)
323 g_object_class_install_property (gobject_class, PROP_DEVICE,
324 g_param_spec_string ("device", "Device", "Device location",
325 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
327 g_param_spec_string ("device-name", "Device name",
328 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
329 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
330 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
331 g_param_spec_int ("device-fd", "File descriptor",
332 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
333 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
334 g_object_class_install_property (gobject_class, PROP_FLAGS,
335 g_param_spec_flags ("flags", "Flags", "Device type flags",
336 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
337 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
340 * GstV4l2Src:brightness
342 * Picture brightness, or more precisely, the black level
346 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
347 g_param_spec_int ("brightness", "Brightness",
348 "Picture brightness, or more precisely, the black level", G_MININT,
350 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
352 * GstV4l2Src:contrast
354 * Picture contrast or luma gain
358 g_object_class_install_property (gobject_class, PROP_CONTRAST,
359 g_param_spec_int ("contrast", "Contrast",
360 "Picture contrast or luma gain", G_MININT,
362 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
364 * GstV4l2Src:saturation
366 * Picture color saturation or chroma gain
370 g_object_class_install_property (gobject_class, PROP_SATURATION,
371 g_param_spec_int ("saturation", "Saturation",
372 "Picture color saturation or chroma gain", G_MININT,
374 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
378 * Hue or color balance
382 g_object_class_install_property (gobject_class, PROP_HUE,
383 g_param_spec_int ("hue", "Hue",
384 "Hue or color balance", G_MININT,
386 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
390 gst_v4l2_object_new (GstElement * element,
391 enum v4l2_buf_type type,
392 const char *default_device,
393 GstV4l2GetInOutFunction get_in_out_func,
394 GstV4l2SetInOutFunction set_in_out_func,
395 GstV4l2UpdateFpsFunction update_fps_func)
397 GstV4l2Object *v4l2object;
400 * some default values
402 v4l2object = g_new0 (GstV4l2Object, 1);
404 v4l2object->type = type;
405 v4l2object->formats = NULL;
407 v4l2object->element = element;
408 v4l2object->get_in_out_func = get_in_out_func;
409 v4l2object->set_in_out_func = set_in_out_func;
410 v4l2object->update_fps_func = update_fps_func;
412 v4l2object->video_fd = -1;
413 v4l2object->poll = gst_poll_new (TRUE);
414 v4l2object->buffer = NULL;
415 v4l2object->videodev = g_strdup (default_device);
417 v4l2object->norms = NULL;
418 v4l2object->channels = NULL;
419 v4l2object->colors = NULL;
421 v4l2object->xwindow_id = 0;
426 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
430 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
432 g_return_if_fail (v4l2object != NULL);
434 if (v4l2object->videodev)
435 g_free (v4l2object->videodev);
437 if (v4l2object->poll)
438 gst_poll_free (v4l2object->poll);
440 if (v4l2object->channel)
441 g_free (v4l2object->channel);
443 if (v4l2object->norm)
444 g_free (v4l2object->norm);
446 if (v4l2object->formats) {
447 gst_v4l2_object_clear_format_list (v4l2object);
455 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
457 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
458 g_slist_free (v4l2object->formats);
459 v4l2object->formats = NULL;
465 gst_v4l2_object_prop_to_cid (guint prop_id)
470 case PROP_BRIGHTNESS:
471 cid = V4L2_CID_BRIGHTNESS;
474 cid = V4L2_CID_CONTRAST;
476 case PROP_SATURATION:
477 cid = V4L2_CID_SATURATION;
483 GST_WARNING ("unmapped property id: %d", prop_id);
490 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
491 guint prop_id, const GValue * value, GParamSpec * pspec)
495 g_free (v4l2object->videodev);
496 v4l2object->videodev = g_value_dup_string (value);
498 case PROP_BRIGHTNESS:
500 case PROP_SATURATION:
503 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
506 if (GST_V4L2_IS_OPEN (v4l2object)) {
507 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
515 if (GST_V4L2_IS_OPEN (v4l2object)) {
516 GstTuner *tuner = GST_TUNER (v4l2object->element);
517 GstTunerNorm *norm = gst_tuner_find_norm_by_name (tuner,
518 (gchar *) g_value_get_string (value));
521 /* like gst_tuner_set_norm (tuner, norm)
522 without g_object_notify */
523 gst_v4l2_tuner_set_norm (v4l2object, norm);
526 g_free (v4l2object->norm);
527 v4l2object->norm = g_value_dup_string (value);
531 if (GST_V4L2_IS_OPEN (v4l2object)) {
532 GstTuner *tuner = GST_TUNER (v4l2object->element);
533 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
534 (gchar *) g_value_get_string (value));
537 /* like gst_tuner_set_channel (tuner, channel)
538 without g_object_notify */
539 gst_v4l2_tuner_set_channel (v4l2object, channel);
542 g_free (v4l2object->channel);
543 v4l2object->channel = g_value_dup_string (value);
547 if (GST_V4L2_IS_OPEN (v4l2object)) {
548 GstTuner *tuner = GST_TUNER (v4l2object->element);
549 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
552 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
554 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
555 without g_object_notify */
556 gst_v4l2_tuner_set_frequency (v4l2object, channel,
557 g_value_get_ulong (value));
560 v4l2object->frequency = g_value_get_ulong (value);
573 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
574 guint prop_id, GValue * value, GParamSpec * pspec)
578 g_value_set_string (value, v4l2object->videodev);
580 case PROP_DEVICE_NAME:
582 const guchar *new = NULL;
584 if (GST_V4L2_IS_OPEN (v4l2object)) {
585 new = v4l2object->vcap.card;
586 } else if (gst_v4l2_open (v4l2object)) {
587 new = v4l2object->vcap.card;
588 gst_v4l2_close (v4l2object);
590 g_value_set_string (value, (gchar *) new);
595 if (GST_V4L2_IS_OPEN (v4l2object))
596 g_value_set_int (value, v4l2object->video_fd);
598 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
605 if (GST_V4L2_IS_OPEN (v4l2object)) {
606 flags |= v4l2object->vcap.capabilities &
607 (V4L2_CAP_VIDEO_CAPTURE |
608 V4L2_CAP_VIDEO_OUTPUT |
609 V4L2_CAP_VIDEO_OVERLAY |
610 V4L2_CAP_VBI_CAPTURE |
611 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
613 g_value_set_flags (value, flags);
616 case PROP_BRIGHTNESS:
618 case PROP_SATURATION:
621 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
624 if (GST_V4L2_IS_OPEN (v4l2object)) {
626 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
627 g_value_set_int (value, v);
642 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
644 GstTunerNorm *norm = NULL;
645 GstTunerChannel *channel = NULL;
648 if (!GST_IS_TUNER (v4l2object->element))
651 tuner = GST_TUNER (v4l2object->element);
653 if (v4l2object->norm)
654 norm = gst_tuner_find_norm_by_name (tuner, v4l2object->norm);
656 gst_tuner_set_norm (tuner, norm);
659 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
661 g_free (v4l2object->norm);
662 v4l2object->norm = g_strdup (norm->label);
663 gst_tuner_norm_changed (tuner, norm);
667 if (v4l2object->channel)
668 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
670 gst_tuner_set_channel (tuner, channel);
673 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
674 (v4l2object->element)));
676 g_free (v4l2object->channel);
677 v4l2object->channel = g_strdup (channel->label);
678 gst_tuner_channel_changed (tuner, channel);
683 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
684 if (v4l2object->frequency != 0) {
685 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
687 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
688 if (v4l2object->frequency == 0) {
690 gst_tuner_set_frequency (tuner, channel, 1000);
698 gst_v4l2_object_start (GstV4l2Object * v4l2object)
700 if (gst_v4l2_open (v4l2object))
701 gst_v4l2_set_defaults (v4l2object);
706 gst_v4l2_xoverlay_start (v4l2object);
713 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
716 gst_v4l2_xoverlay_stop (v4l2object);
719 if (!gst_v4l2_close (v4l2object))
722 if (v4l2object->formats) {
723 gst_v4l2_object_clear_format_list (v4l2object);
731 * common format / caps utilities:
739 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
740 /* from Linux 2.6.15 videodev2.h */
741 {V4L2_PIX_FMT_RGB332, TRUE},
742 {V4L2_PIX_FMT_RGB555, TRUE},
743 {V4L2_PIX_FMT_RGB565, TRUE},
744 {V4L2_PIX_FMT_RGB555X, TRUE},
745 {V4L2_PIX_FMT_RGB565X, TRUE},
746 {V4L2_PIX_FMT_BGR24, TRUE},
747 {V4L2_PIX_FMT_RGB24, TRUE},
748 {V4L2_PIX_FMT_BGR32, TRUE},
749 {V4L2_PIX_FMT_RGB32, TRUE},
750 {V4L2_PIX_FMT_GREY, TRUE},
751 {V4L2_PIX_FMT_YVU410, TRUE},
752 {V4L2_PIX_FMT_YVU420, TRUE},
753 {V4L2_PIX_FMT_YUYV, TRUE},
754 {V4L2_PIX_FMT_UYVY, TRUE},
755 {V4L2_PIX_FMT_YUV422P, TRUE},
756 {V4L2_PIX_FMT_YUV411P, TRUE},
757 {V4L2_PIX_FMT_Y41P, TRUE},
759 /* two planes -- one Y, one Cr + Cb interleaved */
760 {V4L2_PIX_FMT_NV12, TRUE},
761 {V4L2_PIX_FMT_NV21, TRUE},
763 /* The following formats are not defined in the V4L2 specification */
764 {V4L2_PIX_FMT_YUV410, TRUE},
765 {V4L2_PIX_FMT_YUV420, TRUE},
766 {V4L2_PIX_FMT_YYUV, TRUE},
767 {V4L2_PIX_FMT_HI240, TRUE},
769 /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
770 #ifdef V4L2_PIX_FMT_SBGGR8
771 {V4L2_PIX_FMT_SBGGR8, TRUE},
774 /* compressed formats */
775 {V4L2_PIX_FMT_MJPEG, TRUE},
776 {V4L2_PIX_FMT_JPEG, TRUE},
777 {V4L2_PIX_FMT_PJPG, TRUE},
778 {V4L2_PIX_FMT_DV, TRUE},
779 {V4L2_PIX_FMT_MPEG, FALSE},
781 /* Vendor-specific formats */
782 {V4L2_PIX_FMT_WNVA, TRUE},
784 #ifdef V4L2_PIX_FMT_SN9C10X
785 {V4L2_PIX_FMT_SN9C10X, TRUE},
787 #ifdef V4L2_PIX_FMT_PWC1
788 {V4L2_PIX_FMT_PWC1, TRUE},
790 #ifdef V4L2_PIX_FMT_PWC2
791 {V4L2_PIX_FMT_PWC2, TRUE},
793 #ifdef V4L2_PIX_FMT_YVYU
794 {V4L2_PIX_FMT_YVYU, TRUE},
798 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
801 static struct v4l2_fmtdesc *
802 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
805 struct v4l2_fmtdesc *fmt;
811 walk = gst_v4l2_object_get_format_list (v4l2object);
813 fmt = (struct v4l2_fmtdesc *) walk->data;
814 if (fmt->pixelformat == fourcc)
816 /* special case for jpeg */
817 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
818 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
819 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
820 if (fourcc == V4L2_PIX_FMT_JPEG
821 || fourcc == V4L2_PIX_FMT_MJPEG || fourcc == V4L2_PIX_FMT_PJPG) {
825 walk = g_slist_next (walk);
833 /* complete made up ranking, the values themselves are meaningless */
834 #define YUV_BASE_RANK 1000
835 #define JPEG_BASE_RANK 500
836 #define DV_BASE_RANK 200
837 #define RGB_BASE_RANK 100
838 #define YUV_ODD_BASE_RANK 50
839 #define RGB_ODD_BASE_RANK 25
840 #define BAYER_BASE_RANK 15
841 #define S910_BASE_RANK 10
842 #define GREY_BASE_RANK 5
843 #define PWC_BASE_RANK 1
845 /* This flag is already used by libv4l2 although
846 * it was added to the Linux kernel in 2.6.32
848 #ifndef V4L2_FMT_FLAG_EMULATED
849 #define V4L2_FMT_FLAG_EMULATED 0x0002
853 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
855 guint32 fourcc = fmt->pixelformat;
856 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
860 case V4L2_PIX_FMT_MJPEG:
861 case V4L2_PIX_FMT_PJPG:
862 rank = JPEG_BASE_RANK;
864 case V4L2_PIX_FMT_JPEG:
865 rank = JPEG_BASE_RANK + 1;
867 case V4L2_PIX_FMT_MPEG: /* MPEG */
868 rank = JPEG_BASE_RANK + 2;
871 case V4L2_PIX_FMT_RGB332:
872 case V4L2_PIX_FMT_RGB555:
873 case V4L2_PIX_FMT_RGB555X:
874 case V4L2_PIX_FMT_RGB565:
875 case V4L2_PIX_FMT_RGB565X:
876 rank = RGB_ODD_BASE_RANK;
879 case V4L2_PIX_FMT_RGB24:
880 case V4L2_PIX_FMT_BGR24:
881 rank = RGB_BASE_RANK - 1;
884 case V4L2_PIX_FMT_RGB32:
885 case V4L2_PIX_FMT_BGR32:
886 rank = RGB_BASE_RANK;
889 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
890 rank = GREY_BASE_RANK;
893 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
894 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
895 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
896 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
897 rank = YUV_ODD_BASE_RANK;
900 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
901 rank = YUV_BASE_RANK + 3;
903 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
904 rank = YUV_BASE_RANK + 2;
906 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
907 rank = YUV_BASE_RANK + 7;
909 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
910 rank = YUV_BASE_RANK + 10;
912 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
913 rank = YUV_BASE_RANK + 6;
915 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
916 rank = YUV_BASE_RANK + 9;
918 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
919 rank = YUV_BASE_RANK + 5;
921 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
922 rank = YUV_BASE_RANK + 4;
924 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
925 rank = YUV_BASE_RANK + 8;
928 case V4L2_PIX_FMT_DV:
932 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
936 #ifdef V4L2_PIX_FMT_SBGGR8
937 case V4L2_PIX_FMT_SBGGR8:
938 rank = BAYER_BASE_RANK;
942 #ifdef V4L2_PIX_FMT_SN9C10X
943 case V4L2_PIX_FMT_SN9C10X:
944 rank = S910_BASE_RANK;
948 #ifdef V4L2_PIX_FMT_PWC1
949 case V4L2_PIX_FMT_PWC1:
950 rank = PWC_BASE_RANK;
953 #ifdef V4L2_PIX_FMT_PWC2
954 case V4L2_PIX_FMT_PWC2:
955 rank = PWC_BASE_RANK;
964 /* All ranks are below 1<<15 so a shift by 15
965 * will a) make all non-emulated formats larger
966 * than emulated and b) will not overflow
977 format_cmp_func (gconstpointer a, gconstpointer b)
979 const struct v4l2_fmtdesc *fa = a;
980 const struct v4l2_fmtdesc *fb = b;
982 if (fa->pixelformat == fb->pixelformat)
985 return gst_v4l2_object_format_get_rank (fb) -
986 gst_v4l2_object_format_get_rank (fa);
989 /******************************************************
990 * gst_v4l2_object_fill_format_list():
991 * create list of supported capture formats
992 * return value: TRUE on success, FALSE on error
993 ******************************************************/
995 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object)
998 struct v4l2_fmtdesc *format;
1000 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1002 /* format enumeration */
1004 format = g_new0 (struct v4l2_fmtdesc, 1);
1007 format->type = v4l2object->type;
1009 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1010 if (errno == EINVAL) {
1012 break; /* end of enumeration */
1018 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1019 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1020 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1021 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1022 format->description);
1023 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1024 GST_FOURCC_ARGS (format->pixelformat));
1026 /* sort formats according to our preference; we do this, because caps
1027 * are probed in the order the formats are in the list, and the order of
1028 * formats in the final probed caps matters for things like fixation */
1029 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1030 (GCompareFunc) format_cmp_func);
1033 #ifndef GST_DISABLE_GST_DEBUG
1037 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1038 for (l = v4l2object->formats; l != NULL; l = l->next) {
1041 GST_INFO_OBJECT (v4l2object->element,
1042 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1043 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1053 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1054 (_("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)));
1061 * Get the list of supported capture formats, a list of
1062 * <code>struct v4l2_fmtdesc</code>.
1065 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1067 if (!v4l2object->formats)
1068 gst_v4l2_object_fill_format_list (v4l2object);
1069 return v4l2object->formats;
1074 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1076 GstStructure *structure = NULL;
1079 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1080 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1081 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1082 structure = gst_structure_new ("image/jpeg", NULL);
1084 case V4L2_PIX_FMT_RGB332:
1085 case V4L2_PIX_FMT_RGB555:
1086 case V4L2_PIX_FMT_RGB555X:
1087 case V4L2_PIX_FMT_RGB565:
1088 case V4L2_PIX_FMT_RGB565X:
1089 case V4L2_PIX_FMT_RGB24:
1090 case V4L2_PIX_FMT_BGR24:
1091 case V4L2_PIX_FMT_RGB32:
1092 case V4L2_PIX_FMT_BGR32:{
1093 guint depth = 0, bpp = 0;
1095 gint endianness = 0;
1097 guint32 r_mask = 0, b_mask = 0, g_mask = 0;
1100 case V4L2_PIX_FMT_RGB332:
1102 endianness = G_BYTE_ORDER; /* 'like, whatever' */
1107 case V4L2_PIX_FMT_RGB555:
1108 case V4L2_PIX_FMT_RGB555X:
1112 fourcc == V4L2_PIX_FMT_RGB555X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
1117 case V4L2_PIX_FMT_RGB565:
1118 case V4L2_PIX_FMT_RGB565X:
1121 fourcc == V4L2_PIX_FMT_RGB565X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
1126 case V4L2_PIX_FMT_RGB24:
1128 endianness = G_BIG_ENDIAN;
1133 case V4L2_PIX_FMT_BGR24:
1135 endianness = G_BIG_ENDIAN;
1140 case V4L2_PIX_FMT_RGB32:
1142 endianness = G_BIG_ENDIAN;
1143 r_mask = 0xff000000;
1144 g_mask = 0x00ff0000;
1145 b_mask = 0x0000ff00;
1147 case V4L2_PIX_FMT_BGR32:
1149 endianness = G_BIG_ENDIAN;
1150 r_mask = 0x000000ff;
1151 g_mask = 0x0000ff00;
1152 b_mask = 0x00ff0000;
1155 g_assert_not_reached ();
1158 structure = gst_structure_new ("video/x-raw-rgb",
1159 "bpp", G_TYPE_INT, bpp,
1160 "depth", G_TYPE_INT, depth,
1161 "red_mask", G_TYPE_INT, r_mask,
1162 "green_mask", G_TYPE_INT, g_mask,
1163 "blue_mask", G_TYPE_INT, b_mask,
1164 "endianness", G_TYPE_INT, endianness, NULL);
1167 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1168 structure = gst_structure_new ("video/x-raw-gray",
1169 "bpp", G_TYPE_INT, 8, NULL);
1171 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1172 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1173 /* FIXME: get correct fourccs here */
1175 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1176 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1177 case V4L2_PIX_FMT_YVU410:
1178 case V4L2_PIX_FMT_YUV410:
1179 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1180 case V4L2_PIX_FMT_YUYV:
1181 case V4L2_PIX_FMT_YVU420:
1182 case V4L2_PIX_FMT_UYVY:
1183 case V4L2_PIX_FMT_Y41P:
1184 case V4L2_PIX_FMT_YUV422P:
1185 #ifdef V4L2_PIX_FMT_YVYU
1186 case V4L2_PIX_FMT_YVYU:
1188 case V4L2_PIX_FMT_YUV411P:{
1192 case V4L2_PIX_FMT_NV12:
1193 fcc = GST_MAKE_FOURCC ('N', 'V', '1', '2');
1195 case V4L2_PIX_FMT_NV21:
1196 fcc = GST_MAKE_FOURCC ('N', 'V', '2', '1');
1198 case V4L2_PIX_FMT_YVU410:
1199 fcc = GST_MAKE_FOURCC ('Y', 'V', 'U', '9');
1201 case V4L2_PIX_FMT_YUV410:
1202 fcc = GST_MAKE_FOURCC ('Y', 'U', 'V', '9');
1204 case V4L2_PIX_FMT_YUV420:
1205 fcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
1207 case V4L2_PIX_FMT_YUYV:
1208 fcc = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
1210 case V4L2_PIX_FMT_YVU420:
1211 fcc = GST_MAKE_FOURCC ('Y', 'V', '1', '2');
1213 case V4L2_PIX_FMT_UYVY:
1214 fcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
1216 case V4L2_PIX_FMT_Y41P:
1217 fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'P');
1219 case V4L2_PIX_FMT_YUV411P:
1220 fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
1222 case V4L2_PIX_FMT_YUV422P:
1223 fcc = GST_MAKE_FOURCC ('Y', '4', '2', 'B');
1225 #ifdef V4L2_PIX_FMT_YVYU
1226 case V4L2_PIX_FMT_YVYU:
1227 fcc = GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U');
1231 g_assert_not_reached ();
1234 structure = gst_structure_new ("video/x-raw-yuv",
1235 "format", GST_TYPE_FOURCC, fcc, NULL);
1238 case V4L2_PIX_FMT_DV:
1240 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1243 case V4L2_PIX_FMT_MPEG: /* MPEG */
1244 structure = gst_structure_new ("video/mpegts", NULL);
1246 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1248 #ifdef V4L2_PIX_FMT_SBGGR8
1249 case V4L2_PIX_FMT_SBGGR8:
1250 structure = gst_structure_new ("video/x-raw-bayer", NULL);
1253 #ifdef V4L2_PIX_FMT_SN9C10X
1254 case V4L2_PIX_FMT_SN9C10X:
1255 structure = gst_structure_new ("video/x-sonix", NULL);
1258 #ifdef V4L2_PIX_FMT_PWC1
1259 case V4L2_PIX_FMT_PWC1:
1260 structure = gst_structure_new ("video/x-pwc1", NULL);
1263 #ifdef V4L2_PIX_FMT_PWC2
1264 case V4L2_PIX_FMT_PWC2:
1265 structure = gst_structure_new ("video/x-pwc2", NULL);
1269 GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
1270 fourcc, GST_FOURCC_ARGS (fourcc));
1280 gst_v4l2_object_get_all_caps (void)
1282 static GstCaps *caps = NULL;
1285 GstStructure *structure;
1289 caps = gst_caps_new_empty ();
1290 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1292 gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
1294 if (gst_v4l2_formats[i].dimensions) {
1295 gst_structure_set (structure,
1296 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1297 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1298 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
1300 gst_caps_append_structure (caps, structure);
1305 return gst_caps_ref (caps);
1309 /* collect data for the given caps
1310 * @caps: given input caps
1311 * @format: location for the v4l format
1312 * @w/@h: location for width and height
1313 * @fps_n/@fps_d: location for framerate
1314 * @size: location for expected size of the frame or 0 if unknown
1317 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1318 struct v4l2_fmtdesc ** format, gint * w, gint * h,
1319 gboolean * interlaced, guint * fps_n, guint * fps_d, guint * size)
1321 GstStructure *structure;
1322 const GValue *framerate;
1324 const gchar *mimetype;
1327 /* default unknown values */
1331 structure = gst_caps_get_structure (caps, 0);
1333 mimetype = gst_structure_get_name (structure);
1335 if (strcmp (mimetype, "video/mpegts") == 0) {
1336 fourcc = V4L2_PIX_FMT_MPEG;
1342 if (!gst_structure_get_int (structure, "width", w))
1345 if (!gst_structure_get_int (structure, "height", h))
1348 if (!gst_structure_get_boolean (structure, "interlaced", interlaced))
1349 *interlaced = FALSE;
1351 framerate = gst_structure_get_value (structure, "framerate");
1355 *fps_n = gst_value_get_fraction_numerator (framerate);
1356 *fps_d = gst_value_get_fraction_denominator (framerate);
1358 if (!strcmp (mimetype, "video/x-raw-yuv")) {
1359 gst_structure_get_fourcc (structure, "format", &fourcc);
1362 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
1363 case GST_MAKE_FOURCC ('I', 'Y', 'U', 'V'):
1364 fourcc = V4L2_PIX_FMT_YUV420;
1365 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1366 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1368 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
1369 fourcc = V4L2_PIX_FMT_YUYV;
1370 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1372 case GST_MAKE_FOURCC ('Y', '4', '1', 'P'):
1373 fourcc = V4L2_PIX_FMT_Y41P;
1374 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1376 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
1377 fourcc = V4L2_PIX_FMT_UYVY;
1378 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1380 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
1381 fourcc = V4L2_PIX_FMT_YVU420;
1382 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1383 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1385 case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
1386 fourcc = V4L2_PIX_FMT_YUV411P;
1387 outsize = GST_ROUND_UP_4 (*w) * *h;
1388 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 4) * *h);
1390 case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
1391 fourcc = V4L2_PIX_FMT_YUV422P;
1392 outsize = GST_ROUND_UP_4 (*w) * *h;
1393 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * *h);
1395 case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
1396 fourcc = V4L2_PIX_FMT_NV12;
1397 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1398 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1400 case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
1401 fourcc = V4L2_PIX_FMT_NV21;
1402 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1403 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1405 #ifdef V4L2_PIX_FMT_YVYU
1406 case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
1407 fourcc = V4L2_PIX_FMT_YVYU;
1408 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1412 } else if (!strcmp (mimetype, "video/x-raw-rgb")) {
1413 gint depth, endianness, r_mask;
1415 gst_structure_get_int (structure, "depth", &depth);
1416 gst_structure_get_int (structure, "endianness", &endianness);
1417 gst_structure_get_int (structure, "red_mask", &r_mask);
1421 fourcc = V4L2_PIX_FMT_RGB332;
1424 fourcc = (endianness == G_LITTLE_ENDIAN) ?
1425 V4L2_PIX_FMT_RGB555 : V4L2_PIX_FMT_RGB555X;
1428 fourcc = (endianness == G_LITTLE_ENDIAN) ?
1429 V4L2_PIX_FMT_RGB565 : V4L2_PIX_FMT_RGB565X;
1432 fourcc = (r_mask == 0xFF) ? V4L2_PIX_FMT_BGR24 : V4L2_PIX_FMT_RGB24;
1435 fourcc = (r_mask == 0xFF) ? V4L2_PIX_FMT_BGR32 : V4L2_PIX_FMT_RGB32;
1438 } else if (strcmp (mimetype, "video/x-dv") == 0) {
1439 fourcc = V4L2_PIX_FMT_DV;
1440 } else if (strcmp (mimetype, "image/jpeg") == 0) {
1441 fourcc = V4L2_PIX_FMT_JPEG;
1442 #ifdef V4L2_PIX_FMT_SBGGR8
1443 } else if (strcmp (mimetype, "video/x-raw-bayer") == 0) {
1444 fourcc = V4L2_PIX_FMT_SBGGR8;
1446 #ifdef V4L2_PIX_FMT_SN9C10X
1447 } else if (strcmp (mimetype, "video/x-sonix") == 0) {
1448 fourcc = V4L2_PIX_FMT_SN9C10X;
1450 #ifdef V4L2_PIX_FMT_PWC1
1451 } else if (strcmp (mimetype, "video/x-pwc1") == 0) {
1452 fourcc = V4L2_PIX_FMT_PWC1;
1454 #ifdef V4L2_PIX_FMT_PWC2
1455 } else if (strcmp (mimetype, "video/x-pwc2") == 0) {
1456 fourcc = V4L2_PIX_FMT_PWC2;
1458 } else if (strcmp (mimetype, "video/x-raw-gray") == 0) {
1459 fourcc = V4L2_PIX_FMT_GREY;
1466 *format = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1474 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1475 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced);
1478 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1479 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1480 static GstStructure *
1481 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1482 guint32 pixelformat,
1483 guint32 width, guint32 height, const GstStructure * template)
1485 gint fd = v4l2object->video_fd;
1486 struct v4l2_frmivalenum ival;
1489 GValue rates = { 0, };
1490 gboolean interlaced;
1491 gint int_width = width;
1492 gint int_height = height;
1494 /* interlaced detection using VIDIOC_TRY/S_FMT */
1495 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat,
1496 &int_width, &int_height, &interlaced))
1499 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1501 ival.pixel_format = pixelformat;
1503 ival.height = height;
1505 GST_LOG_OBJECT (v4l2object->element,
1506 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1507 GST_FOURCC_ARGS (pixelformat));
1509 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1510 * fraction to get framerate */
1511 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1512 goto enum_frameintervals_failed;
1514 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1515 GValue rate = { 0, };
1517 g_value_init (&rates, GST_TYPE_LIST);
1518 g_value_init (&rate, GST_TYPE_FRACTION);
1521 num = ival.discrete.numerator;
1522 denom = ival.discrete.denominator;
1524 if (num > G_MAXINT || denom > G_MAXINT) {
1525 /* let us hope we don't get here... */
1530 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1533 /* swap to get the framerate */
1534 gst_value_set_fraction (&rate, denom, num);
1535 gst_value_list_append_value (&rates, &rate);
1538 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1539 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1540 GValue min = { 0, };
1541 GValue step = { 0, };
1542 GValue max = { 0, };
1543 gboolean added = FALSE;
1544 guint32 minnum, mindenom;
1545 guint32 maxnum, maxdenom;
1547 g_value_init (&rates, GST_TYPE_LIST);
1549 g_value_init (&min, GST_TYPE_FRACTION);
1550 g_value_init (&step, GST_TYPE_FRACTION);
1551 g_value_init (&max, GST_TYPE_FRACTION);
1554 minnum = ival.stepwise.min.numerator;
1555 mindenom = ival.stepwise.min.denominator;
1556 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1560 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1562 gst_value_set_fraction (&min, minnum, mindenom);
1565 maxnum = ival.stepwise.max.numerator;
1566 maxdenom = ival.stepwise.max.denominator;
1567 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1572 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1574 gst_value_set_fraction (&max, maxnum, maxdenom);
1577 num = ival.stepwise.step.numerator;
1578 denom = ival.stepwise.step.denominator;
1579 if (num > G_MAXINT || denom > G_MAXINT) {
1584 if (num == 0 || denom == 0) {
1585 /* in this case we have a wrong fraction or no step, set the step to max
1586 * so that we only add the min value in the loop below */
1591 /* since we only have gst_value_fraction_subtract and not add, negate the
1593 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1595 gst_value_set_fraction (&step, -num, denom);
1597 while (gst_value_compare (&min, &max) <= 0) {
1598 GValue rate = { 0, };
1600 num = gst_value_get_fraction_numerator (&min);
1601 denom = gst_value_get_fraction_denominator (&min);
1602 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1605 /* invert to get the framerate */
1606 g_value_init (&rate, GST_TYPE_FRACTION);
1607 gst_value_set_fraction (&rate, denom, num);
1608 gst_value_list_append_value (&rates, &rate);
1611 /* we're actually adding because step was negated above. This is because
1612 * there is no _add function... */
1613 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1614 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1619 /* no range was added, leave the default range from the template */
1620 GST_WARNING_OBJECT (v4l2object->element,
1621 "no range added, leaving default");
1622 g_value_unset (&rates);
1624 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1625 guint32 maxnum, maxdenom;
1627 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1629 num = ival.stepwise.min.numerator;
1630 denom = ival.stepwise.min.denominator;
1631 if (num > G_MAXINT || denom > G_MAXINT) {
1636 maxnum = ival.stepwise.max.numerator;
1637 maxdenom = ival.stepwise.max.denominator;
1638 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1643 GST_LOG_OBJECT (v4l2object->element,
1644 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
1647 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1653 s = gst_structure_copy (template);
1654 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
1655 "height", G_TYPE_INT, (gint) height,
1656 "interlaced", G_TYPE_BOOLEAN, interlaced, NULL);
1658 if (G_IS_VALUE (&rates)) {
1659 /* only change the framerate on the template when we have a valid probed new
1661 gst_structure_set_value (s, "framerate", &rates);
1662 g_value_unset (&rates);
1664 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
1670 enum_frameintervals_failed:
1672 GST_DEBUG_OBJECT (v4l2object->element,
1673 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
1674 GST_FOURCC_ARGS (pixelformat), width, height);
1679 /* I don't see how this is actually an error, we ignore the format then */
1680 GST_WARNING_OBJECT (v4l2object->element,
1681 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
1682 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
1686 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
1688 #ifdef VIDIOC_ENUM_FRAMESIZES
1690 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
1694 gst_structure_get_int (s1, "width", &w1);
1695 gst_structure_get_int (s1, "height", &h1);
1696 gst_structure_get_int (s2, "width", &w2);
1697 gst_structure_get_int (s2, "height", &h2);
1699 /* I think it's safe to assume that this won't overflow for a while */
1700 return ((w2 * h2) - (w1 * h1));
1705 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
1706 guint32 pixelformat, const GstStructure * template)
1708 GstCaps *ret = gst_caps_new_empty ();
1711 #ifdef VIDIOC_ENUM_FRAMESIZES
1712 gint fd = v4l2object->video_fd;
1713 struct v4l2_frmsizeenum size;
1714 GList *results = NULL;
1717 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
1718 return gst_caps_new_simple ("video/mpegts", NULL);
1720 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
1722 size.pixel_format = pixelformat;
1724 GST_DEBUG_OBJECT (v4l2object->element, "Enumerating frame sizes");
1726 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
1727 goto enum_framesizes_failed;
1729 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1731 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
1732 size.discrete.width, size.discrete.height);
1734 w = MIN (size.discrete.width, G_MAXINT);
1735 h = MIN (size.discrete.height, G_MAXINT);
1739 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1740 pixelformat, w, h, template);
1743 results = g_list_prepend (results, tmp);
1747 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
1748 GST_DEBUG_OBJECT (v4l2object->element,
1749 "done iterating discrete frame sizes");
1750 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
1751 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
1752 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1753 size.stepwise.min_width);
1754 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1755 size.stepwise.min_height);
1756 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1757 size.stepwise.max_width);
1758 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1759 size.stepwise.max_height);
1760 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
1761 size.stepwise.step_width);
1762 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
1763 size.stepwise.step_height);
1765 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
1766 w < size.stepwise.max_width && h < size.stepwise.max_height;
1767 w += size.stepwise.step_width, h += size.stepwise.step_height) {
1768 if (w == 0 || h == 0)
1772 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
1773 pixelformat, w, h, template);
1776 results = g_list_prepend (results, tmp);
1778 GST_DEBUG_OBJECT (v4l2object->element,
1779 "done iterating stepwise frame sizes");
1780 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
1783 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
1784 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
1785 size.stepwise.min_width);
1786 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1787 size.stepwise.min_height);
1788 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
1789 size.stepwise.max_width);
1790 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
1791 size.stepwise.max_height);
1793 w = MAX (size.stepwise.min_width, 1);
1794 h = MAX (size.stepwise.min_height, 1);
1795 maxw = MIN (size.stepwise.max_width, G_MAXINT);
1796 maxh = MIN (size.stepwise.max_height, G_MAXINT);
1799 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
1802 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
1803 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
1806 /* no point using the results list here, since there's only one struct */
1807 gst_caps_append_structure (ret, tmp);
1813 /* we use an intermediary list to store and then sort the results of the
1814 * probing because we can't make any assumptions about the order in which
1815 * the driver will give us the sizes, but we want the final caps to contain
1816 * the results starting with the highest resolution and having the lowest
1817 * resolution last, since order in caps matters for things like fixation. */
1818 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
1819 while (results != NULL) {
1820 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
1821 results = g_list_delete_link (results, results);
1824 if (gst_caps_is_empty (ret))
1825 goto enum_framesizes_no_results;
1830 enum_framesizes_failed:
1832 /* I don't see how this is actually an error */
1833 GST_DEBUG_OBJECT (v4l2object->element,
1834 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
1835 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
1836 goto default_frame_sizes;
1838 enum_framesizes_no_results:
1840 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
1841 * question doesn't actually support it yet */
1842 GST_DEBUG_OBJECT (v4l2object->element,
1843 "No results for pixelformat %" GST_FOURCC_FORMAT
1844 " enumerating frame sizes, trying fallback",
1845 GST_FOURCC_ARGS (pixelformat));
1846 goto default_frame_sizes;
1850 GST_WARNING_OBJECT (v4l2object->element,
1851 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
1852 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
1853 goto default_frame_sizes;
1855 default_frame_sizes:
1856 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
1858 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
1859 gboolean interlaced;
1861 /* This code is for Linux < 2.6.19 */
1863 max_w = max_h = GST_V4L2_MAX_SIZE;
1864 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
1865 &min_h, &interlaced)) {
1866 GST_WARNING_OBJECT (v4l2object->element,
1867 "Could not probe minimum capture size for pixelformat %"
1868 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1870 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
1871 &max_h, &interlaced)) {
1872 GST_WARNING_OBJECT (v4l2object->element,
1873 "Could not probe maximum capture size for pixelformat %"
1874 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
1877 /* Since we can't get framerate directly, try to use the current norm */
1878 if (v4l2object->norm && v4l2object->norms) {
1880 GstTunerNorm *norm = NULL;
1882 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
1883 norm = (GstTunerNorm *) norms->data;
1884 if (!strcmp (norm->label, v4l2object->norm))
1887 /* If it's possible, set framerate to that (discrete) value */
1889 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
1890 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
1894 tmp = gst_structure_copy (template);
1896 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
1899 /* if norm can't be used, copy the template framerate */
1900 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
1905 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
1907 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
1910 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
1912 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
1914 gst_structure_set (tmp, "interlaced", G_TYPE_BOOLEAN, interlaced, NULL);
1916 gst_caps_append_structure (ret, tmp);
1923 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1924 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced)
1926 struct v4l2_format fmt;
1930 g_return_val_if_fail (width != NULL, FALSE);
1931 g_return_val_if_fail (height != NULL, FALSE);
1933 GST_LOG_OBJECT (v4l2object->element,
1934 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
1935 *width, *height, GST_FOURCC_ARGS (pixelformat));
1937 fd = v4l2object->video_fd;
1939 /* get size delimiters */
1940 memset (&fmt, 0, sizeof (fmt));
1941 fmt.type = v4l2object->type;
1942 fmt.fmt.pix.width = *width;
1943 fmt.fmt.pix.height = *height;
1944 fmt.fmt.pix.pixelformat = pixelformat;
1945 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1947 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
1948 if (r < 0 && errno == EINVAL) {
1949 /* try again with interlaced video */
1950 fmt.fmt.pix.width = *width;
1951 fmt.fmt.pix.height = *height;
1952 fmt.fmt.pix.pixelformat = pixelformat;
1953 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1954 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
1958 /* The driver might not implement TRY_FMT, in which case we will try
1960 if (errno != ENOTTY)
1963 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
1964 be, because we're still probing */
1965 if (GST_V4L2_IS_ACTIVE (v4l2object))
1968 GST_LOG_OBJECT (v4l2object->element,
1969 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
1971 fmt.fmt.pix.width = *width;
1972 fmt.fmt.pix.height = *height;
1974 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
1975 if (r < 0 && errno == EINVAL) {
1976 /* try again with progressive video */
1977 fmt.fmt.pix.width = *width;
1978 fmt.fmt.pix.height = *height;
1979 fmt.fmt.pix.pixelformat = pixelformat;
1980 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1981 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
1988 GST_LOG_OBJECT (v4l2object->element,
1989 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
1991 *width = fmt.fmt.pix.width;
1992 *height = fmt.fmt.pix.height;
1994 switch (fmt.fmt.pix.field) {
1995 case V4L2_FIELD_ANY:
1996 case V4L2_FIELD_NONE:
1997 *interlaced = FALSE;
1999 case V4L2_FIELD_INTERLACED:
2000 case V4L2_FIELD_INTERLACED_TB:
2001 case V4L2_FIELD_INTERLACED_BT:
2005 GST_WARNING_OBJECT (v4l2object->element,
2006 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u",
2007 GST_FOURCC_ARGS (pixelformat), *width, *height);
2016 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, guint32 pixelformat,
2017 guint32 width, guint32 height, gboolean interlaced)
2019 gint fd = v4l2object->video_fd;
2020 struct v4l2_format format;
2021 enum v4l2_field field;
2024 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
2025 /* ideally we would differentiate between types of interlaced video
2026 * but there is not sufficient information in the caps..
2028 field = V4L2_FIELD_INTERLACED;
2030 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
2031 field = V4L2_FIELD_NONE;
2034 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2035 "%" GST_FOURCC_FORMAT, width, height, GST_FOURCC_ARGS (pixelformat));
2037 GST_V4L2_CHECK_OPEN (v4l2object);
2038 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2040 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
2043 memset (&format, 0x00, sizeof (struct v4l2_format));
2044 format.type = v4l2object->type;
2046 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
2047 goto get_fmt_failed;
2049 if (format.type == v4l2object->type &&
2050 format.fmt.pix.width == width &&
2051 format.fmt.pix.height == height &&
2052 format.fmt.pix.pixelformat == pixelformat &&
2053 format.fmt.pix.field == field) {
2054 /* Nothing to do. We want to succeed immediately
2055 * here because setting the same format back
2056 * can still fail due to EBUSY. By short-circuiting
2057 * here, we allow pausing and re-playing pipelines
2058 * with changed caps, as long as the changed caps
2059 * do not change the webcam's format. Otherwise,
2060 * any caps change would require us to go to NULL
2061 * state to close the device and set format.
2066 format.type = v4l2object->type;
2067 format.fmt.pix.width = width;
2068 format.fmt.pix.height = height;
2069 format.fmt.pix.pixelformat = pixelformat;
2070 format.fmt.pix.field = field;
2072 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0) {
2073 goto set_fmt_failed;
2076 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
2077 goto invalid_dimensions;
2079 if (format.fmt.pix.pixelformat != pixelformat)
2080 goto invalid_pixelformat;
2087 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2088 (_("Device '%s' does not support video capture"),
2089 v4l2object->videodev),
2090 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
2095 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2096 (_("Device '%s' cannot capture at %dx%d"),
2097 v4l2object->videodev, width, height),
2098 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2099 GST_FOURCC_ARGS (pixelformat), width, height, g_strerror (errno)));
2104 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2105 (_("Device '%s' cannot capture at %dx%d"),
2106 v4l2object->videodev, width, height),
2107 ("Tried to capture at %dx%d, but device returned size %dx%d",
2108 width, height, format.fmt.pix.width, format.fmt.pix.height));
2111 invalid_pixelformat:
2113 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2114 (_("Device '%s' cannot capture in the specified format"),
2115 v4l2object->videodev),
2116 ("Tried to capture in %" GST_FOURCC_FORMAT
2117 ", but device returned format" " %" GST_FOURCC_FORMAT,
2118 GST_FOURCC_ARGS (pixelformat),
2119 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
2125 gst_v4l2_object_start_streaming (GstV4l2Object * v4l2object)
2127 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_STREAMON,
2128 &(v4l2object->type)) < 0) {
2129 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ,
2130 (_("Error starting streaming on device '%s'."), v4l2object->videodev),
2138 gst_v4l2_object_stop_streaming (GstV4l2Object * v4l2object)
2140 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_STREAMOFF,
2141 &(v4l2object->type)) < 0) {
2142 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ,
2143 (_("Error stopping streaming on device '%s'."), v4l2object->videodev),