3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
6 * gstv4l2object.c: base class for V4L2 elements
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Library General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. This library is distributed in the hope
12 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
21 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
22 * with newer GLib versions (>= 2.31.0) */
23 #define GLIB_DISABLE_DEPRECATION_WARNINGS
36 #include <gudev/gudev.h>
39 #include "v4l2_calls.h"
40 #include "gstv4l2tuner.h"
42 #include "gstv4l2videooverlay.h"
44 #include "gstv4l2colorbalance.h"
46 #include "gst/gst-i18n-plugin.h"
48 #include <gst/video/video.h>
50 /* videodev2.h is not versioned and we can't easily check for the presence
51 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
52 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
53 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
54 #define V4L2_FIELD_INTERLACED_TB 8
55 #define V4L2_FIELD_INTERLACED_BT 9
58 #ifndef V4L2_PIX_FMT_NV12M
59 #define V4L2_PIX_FMT_NV12M GST_MAKE_FOURCC ('N', 'M', '1', '2')
61 #ifndef V4L2_PIX_FMT_NV21M
62 #define V4L2_PIX_FMT_NV21M GST_MAKE_FOURCC ('N', 'M', '2', '1')
64 #ifndef V4L2_PIX_FMT_MPEG1
65 #define V4L2_PIX_FMT_MPEG1 GST_MAKE_FOURCC ('M', 'P', 'G', '1')
67 #ifndef V4L2_PIX_FMT_MPEG2
68 #define V4L2_PIX_FMT_MPEG2 GST_MAKE_FOURCC ('M', 'P', 'G', '2')
70 #ifndef V4L2_PIX_FMT_MPEG4
71 #define V4L2_PIX_FMT_MPEG4 GST_MAKE_FOURCC ('M', 'P', 'G', '4')
74 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
75 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
76 #define GST_CAT_DEFAULT v4l2_debug
78 #define DEFAULT_PROP_DEVICE_NAME NULL
79 #define DEFAULT_PROP_DEVICE_FD -1
80 #define DEFAULT_PROP_FLAGS 0
81 #define DEFAULT_PROP_TV_NORM 0
82 #define DEFAULT_PROP_CHANNEL NULL
83 #define DEFAULT_PROP_FREQUENCY 0
84 #define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
86 #define ENCODED_BUFFER_SIZE (1 * 1024 * 1024)
91 V4L2_STD_OBJECT_PROPS,
94 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
97 G_LOCK_DEFINE_STATIC (probe_lock);
100 gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
102 GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
103 static GList *list = NULL;
108 list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
111 G_UNLOCK (probe_lock);
116 static gboolean init = FALSE;
117 static GList *devices = NULL;
121 gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
122 GList ** klass_devices)
124 GUdevClient *client = NULL;
129 gchar *device = devices->data;
130 devices = g_list_remove (devices, device);
134 GST_INFO ("Enumerating video4linux devices from udev");
135 client = g_udev_client_new (NULL);
137 GST_WARNING ("Failed to initialize gudev client");
141 item = g_udev_client_query_by_subsystem (client, "video4linux");
143 GUdevDevice *device = item->data;
144 gchar *devnode = g_strdup (g_udev_device_get_device_file (device));
145 gint api = g_udev_device_get_property_as_int (device, "ID_V4L_VERSION");
146 GST_INFO ("Found new device: %s, API: %d", devnode, api);
147 /* Append v4l2 devices only. If api is 0 probably v4l_id has
148 been stripped out of the current udev installation, append
152 ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
154 if ((api == 2 || api == 0)) {
155 devices = g_list_append (devices, devnode);
159 g_object_unref (device);
168 g_object_unref (client);
171 *klass_devices = devices;
175 #endif /* HAVE_GUDEV */
178 gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
179 GList ** klass_devices)
182 const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
186 gchar *device = devices->data;
187 devices = g_list_remove (devices, device);
192 * detect /dev entries
194 for (n = 0; n < 64; n++) {
195 for (base = 0; dev_base[base] != NULL; base++) {
197 gchar *device = g_strdup_printf ("%s%d",
202 * does the /dev/ entry exist at all?
204 if (stat (device, &s) == 0) {
206 * yes: is a device attached?
208 if (S_ISCHR (s.st_mode)) {
210 if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
214 devices = g_list_append (devices, device);
225 *klass_devices = devices;
231 gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
232 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
234 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
239 if (!gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices))
240 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
241 #else /* !HAVE_GUDEV */
242 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
243 #endif /* HAVE_GUDEV */
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
252 gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
253 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
255 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
256 gboolean ret = FALSE;
262 !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
263 #else /* !HAVE_GUDEV */
264 ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
265 #endif /* HAVE_GUDEV */
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
275 gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
278 GValue value = { 0 };
284 array = g_value_array_new (g_list_length (*klass_devices));
285 item = *klass_devices;
286 g_value_init (&value, G_TYPE_STRING);
288 gchar *device = item->data;
290 g_value_set_string (&value, device);
291 g_value_array_append (array, &value);
295 g_value_unset (&value);
301 gst_v4l2_probe_get_values (GstPropertyProbe * probe,
302 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
304 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
305 GValueArray *array = NULL;
309 array = gst_v4l2_class_list_devices (klass, klass_devices);
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
320 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
322 gst_v4l2_device_get_type (void)
324 static GType v4l2_device_type = 0;
326 if (v4l2_device_type == 0) {
327 static const GFlagsValue values[] = {
328 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
329 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
330 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
332 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
333 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
335 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
336 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
342 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
345 return v4l2_device_type;
348 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
350 gst_v4l2_tv_norm_get_type (void)
352 static GType v4l2_tv_norm = 0;
355 static const GEnumValue tv_norms[] = {
358 {V4L2_STD_NTSC, "NTSC", "NTSC"},
359 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
360 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
361 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
362 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
364 {V4L2_STD_PAL, "PAL", "PAL"},
365 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
366 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
367 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
368 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
369 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
370 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
371 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
372 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
373 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
374 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
375 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
376 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
377 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
378 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
380 {V4L2_STD_SECAM, "SECAM", "SECAM"},
381 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
382 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
383 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
384 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
385 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
386 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
387 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
388 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
389 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
394 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
401 gst_v4l2_io_mode_get_type (void)
403 static GType v4l2_io_mode = 0;
406 static const GEnumValue io_modes[] = {
407 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
408 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
409 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
410 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
411 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
415 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
421 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
422 const char *default_device)
424 g_object_class_install_property (gobject_class, PROP_DEVICE,
425 g_param_spec_string ("device", "Device", "Device location",
426 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
427 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
428 g_param_spec_string ("device-name", "Device name",
429 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
430 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
431 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
432 g_param_spec_int ("device-fd", "File descriptor",
433 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
434 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
435 g_object_class_install_property (gobject_class, PROP_FLAGS,
436 g_param_spec_flags ("flags", "Flags", "Device type flags",
437 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
438 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
441 * GstV4l2Src:brightness:
443 * Picture brightness, or more precisely, the black level
445 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
446 g_param_spec_int ("brightness", "Brightness",
447 "Picture brightness, or more precisely, the black level", G_MININT,
449 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
451 * GstV4l2Src:contrast:
453 * Picture contrast or luma gain
455 g_object_class_install_property (gobject_class, PROP_CONTRAST,
456 g_param_spec_int ("contrast", "Contrast",
457 "Picture contrast or luma gain", G_MININT,
459 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
461 * GstV4l2Src:saturation:
463 * Picture color saturation or chroma gain
465 g_object_class_install_property (gobject_class, PROP_SATURATION,
466 g_param_spec_int ("saturation", "Saturation",
467 "Picture color saturation or chroma gain", G_MININT,
469 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
473 * Hue or color balance
475 g_object_class_install_property (gobject_class, PROP_HUE,
476 g_param_spec_int ("hue", "Hue",
477 "Hue or color balance", G_MININT,
479 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
486 g_object_class_install_property (gobject_class, PROP_TV_NORM,
487 g_param_spec_enum ("norm", "TV norm",
489 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
490 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
493 * GstV4l2Src:io-mode:
497 g_object_class_install_property (gobject_class, PROP_IO_MODE,
498 g_param_spec_enum ("io-mode", "IO mode",
500 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
501 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
504 * GstV4l2Src:extra-controls:
506 * Additional v4l2 controls for the device. The controls are identified
507 * by the control name (lowercase with '_' for any non-alphanumeric
512 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
513 g_param_spec_boxed ("extra-controls", "Extra Controls",
514 "Extra v4l2 controls (CIDs) for the device",
515 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
518 * GstV4l2Src:pixel-aspect-ratio:
520 * The pixel aspect ratio of the device. This overwrites the pixel aspect
521 * ratio queried from the device.
525 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
526 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
527 "Overwrite the pixel aspect ratio of the device", "1/1",
528 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
531 * GstV4l2Src:force-aspect-ratio:
533 * When enabled, the pixel aspect ratio queried from the device or set
534 * with the pixel-aspect-ratio property will be enforced.
538 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
539 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
540 "When enabled, the pixel aspect ratio will be enforced", TRUE,
541 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
546 gst_v4l2_object_new (GstElement * element,
547 enum v4l2_buf_type type,
548 const char *default_device,
549 GstV4l2GetInOutFunction get_in_out_func,
550 GstV4l2SetInOutFunction set_in_out_func,
551 GstV4l2UpdateFpsFunction update_fps_func)
553 GstV4l2Object *v4l2object;
556 * some default values
558 v4l2object = g_new0 (GstV4l2Object, 1);
560 v4l2object->type = type;
561 v4l2object->formats = NULL;
563 v4l2object->element = element;
564 v4l2object->get_in_out_func = get_in_out_func;
565 v4l2object->set_in_out_func = set_in_out_func;
566 v4l2object->update_fps_func = update_fps_func;
568 v4l2object->video_fd = -1;
569 v4l2object->poll = gst_poll_new (TRUE);
570 v4l2object->active = FALSE;
571 v4l2object->videodev = g_strdup (default_device);
573 v4l2object->norms = NULL;
574 v4l2object->channels = NULL;
575 v4l2object->colors = NULL;
577 v4l2object->xwindow_id = 0;
579 v4l2object->keep_aspect = TRUE;
581 v4l2object->n_v4l2_planes = 0;
584 * this boolean only applies in v4l2-MPLANE mode.
585 * TRUE: means it prefers to use several v4l2 (non contiguous)
586 * planes. For example if the device supports NV12 and NV12M
587 * both in MPLANE mode, then it will prefer NV12M
588 * FALSE: means it prefers to use one v4l2 plane (which contains
589 * all gst planes as if it was working in non-v4l2-MPLANE mode.
590 * For example if the device supports NV12 and NV12M
591 * both in MPLANE mode, then it will prefer NV12
593 * this boolean is also used to manage the case where the
594 * device only supports the mode MPLANE and at the same time it
595 * does not support both NV12 and NV12M. So in this case we first
596 * try to use the prefered config, and at least try the other case
597 * if it fails. For example in MPLANE mode if it has NV12 and not
598 * NV21M then even if you set prefered_non_contiguous to TRUE it will
601 v4l2object->prefered_non_contiguous = TRUE;
603 v4l2object->no_initial_format = FALSE;
608 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
612 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
614 g_return_if_fail (v4l2object != NULL);
616 if (v4l2object->videodev)
617 g_free (v4l2object->videodev);
619 if (v4l2object->poll)
620 gst_poll_free (v4l2object->poll);
622 if (v4l2object->channel)
623 g_free (v4l2object->channel);
625 if (v4l2object->formats) {
626 gst_v4l2_object_clear_format_list (v4l2object);
629 if (v4l2object->probed_caps) {
630 gst_caps_unref (v4l2object->probed_caps);
638 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
640 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
641 g_slist_free (v4l2object->formats);
642 v4l2object->formats = NULL;
648 gst_v4l2_object_prop_to_cid (guint prop_id)
653 case PROP_BRIGHTNESS:
654 cid = V4L2_CID_BRIGHTNESS;
657 cid = V4L2_CID_CONTRAST;
659 case PROP_SATURATION:
660 cid = V4L2_CID_SATURATION;
666 GST_WARNING ("unmapped property id: %d", prop_id);
673 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
674 guint prop_id, const GValue * value, GParamSpec * pspec)
678 g_free (v4l2object->videodev);
679 v4l2object->videodev = g_value_dup_string (value);
681 case PROP_BRIGHTNESS:
683 case PROP_SATURATION:
686 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
689 if (GST_V4L2_IS_OPEN (v4l2object)) {
690 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
697 v4l2object->tv_norm = g_value_get_enum (value);
701 if (GST_V4L2_IS_OPEN (v4l2object)) {
702 GstTuner *tuner = GST_TUNER (v4l2object->element);
703 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
704 (gchar *) g_value_get_string (value));
707 /* like gst_tuner_set_channel (tuner, channel)
708 without g_object_notify */
709 gst_v4l2_tuner_set_channel (v4l2object, channel);
712 g_free (v4l2object->channel);
713 v4l2object->channel = g_value_dup_string (value);
717 if (GST_V4L2_IS_OPEN (v4l2object)) {
718 GstTuner *tuner = GST_TUNER (v4l2object->element);
719 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
722 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
724 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
725 without g_object_notify */
726 gst_v4l2_tuner_set_frequency (v4l2object, channel,
727 g_value_get_ulong (value));
730 v4l2object->frequency = g_value_get_ulong (value);
735 v4l2object->req_mode = g_value_get_enum (value);
737 case PROP_EXTRA_CONTROLS:{
738 const GstStructure *s = gst_value_get_structure (value);
740 if (v4l2object->extra_controls)
741 gst_structure_free (v4l2object->extra_controls);
743 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
744 if (GST_V4L2_IS_OPEN (v4l2object))
745 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
748 case PROP_PIXEL_ASPECT_RATIO:
749 g_free (v4l2object->par);
750 v4l2object->par = g_new0 (GValue, 1);
751 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
752 if (!g_value_transform (value, v4l2object->par)) {
753 g_warning ("Could not transform string to aspect ratio");
754 gst_value_set_fraction (v4l2object->par, 1, 1);
756 GST_DEBUG_OBJECT (v4l2object->element, "set PAR to %d/%d",
757 gst_value_get_fraction_numerator (v4l2object->par),
758 gst_value_get_fraction_denominator (v4l2object->par));
760 case PROP_FORCE_ASPECT_RATIO:
761 v4l2object->keep_aspect = g_value_get_boolean (value);
772 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
773 guint prop_id, GValue * value, GParamSpec * pspec)
777 g_value_set_string (value, v4l2object->videodev);
779 case PROP_DEVICE_NAME:
781 const guchar *new = NULL;
783 if (GST_V4L2_IS_OPEN (v4l2object)) {
784 new = v4l2object->vcap.card;
785 } else if (gst_v4l2_open (v4l2object)) {
786 new = v4l2object->vcap.card;
787 gst_v4l2_close (v4l2object);
789 g_value_set_string (value, (gchar *) new);
794 if (GST_V4L2_IS_OPEN (v4l2object))
795 g_value_set_int (value, v4l2object->video_fd);
797 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
804 if (GST_V4L2_IS_OPEN (v4l2object)) {
805 flags |= v4l2object->vcap.capabilities &
806 (V4L2_CAP_VIDEO_CAPTURE |
807 V4L2_CAP_VIDEO_OUTPUT |
808 V4L2_CAP_VIDEO_OVERLAY |
809 V4L2_CAP_VBI_CAPTURE |
810 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
812 if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
813 flags |= V4L2_CAP_VIDEO_CAPTURE;
815 if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
816 flags |= V4L2_CAP_VIDEO_OUTPUT;
818 g_value_set_flags (value, flags);
821 case PROP_BRIGHTNESS:
823 case PROP_SATURATION:
826 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
829 if (GST_V4L2_IS_OPEN (v4l2object)) {
831 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
832 g_value_set_int (value, v);
840 g_value_set_enum (value, v4l2object->tv_norm);
843 g_value_set_enum (value, v4l2object->req_mode);
845 case PROP_EXTRA_CONTROLS:
846 gst_value_set_structure (value, v4l2object->extra_controls);
848 case PROP_PIXEL_ASPECT_RATIO:
850 g_value_transform (v4l2object->par, value);
852 case PROP_FORCE_ASPECT_RATIO:
853 g_value_set_boolean (value, v4l2object->keep_aspect);
863 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
865 GstTunerNorm *norm = NULL;
866 GstTunerChannel *channel = NULL;
869 if (!GST_IS_TUNER (v4l2object->element))
872 tuner = GST_TUNER (v4l2object->element);
874 if (v4l2object->tv_norm)
875 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
876 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
877 "norm=%p", (guint64) v4l2object->tv_norm, norm);
879 gst_tuner_set_norm (tuner, norm);
882 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
884 v4l2object->tv_norm =
885 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
886 gst_tuner_norm_changed (tuner, norm);
890 if (v4l2object->channel)
891 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
893 gst_tuner_set_channel (tuner, channel);
896 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
897 (v4l2object->element)));
899 g_free (v4l2object->channel);
900 v4l2object->channel = g_strdup (channel->label);
901 gst_tuner_channel_changed (tuner, channel);
906 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
907 if (v4l2object->frequency != 0) {
908 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
910 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
911 if (v4l2object->frequency == 0) {
913 gst_tuner_set_frequency (tuner, channel, 1000);
921 gst_v4l2_object_open (GstV4l2Object * v4l2object)
923 if (gst_v4l2_open (v4l2object))
924 gst_v4l2_set_defaults (v4l2object);
929 gst_v4l2_video_overlay_start (v4l2object);
936 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
940 ret = gst_v4l2_dup (v4l2object, other);
943 gst_v4l2_video_overlay_start (v4l2object);
950 gst_v4l2_object_close (GstV4l2Object * v4l2object)
953 gst_v4l2_video_overlay_stop (v4l2object);
956 if (!gst_v4l2_close (v4l2object))
959 gst_caps_replace (&v4l2object->probed_caps, NULL);
961 if (v4l2object->formats) {
962 gst_v4l2_object_clear_format_list (v4l2object);
970 * common format / caps utilities:
974 GST_V4L2_RAW = 1 << 0,
975 GST_V4L2_CODEC = 1 << 1,
976 GST_V4L2_TRANSPORT = 1 << 2,
977 GST_V4L2_ALL = 0xffff
978 } GstV4L2FormatFlags;
984 GstV4L2FormatFlags flags;
987 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
988 /* from Linux 2.6.15 videodev2.h */
989 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
990 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
991 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
992 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
993 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
994 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
995 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
996 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
997 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
998 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
999 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
1000 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
1001 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
1002 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
1003 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
1004 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
1005 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
1007 /* two planes -- one Y, one Cr + Cb interleaved */
1008 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
1009 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
1010 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
1011 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
1013 /* The following formats are not defined in the V4L2 specification */
1014 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
1015 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
1016 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
1017 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
1019 /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
1020 #ifdef V4L2_PIX_FMT_SBGGR8
1021 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
1024 /* compressed formats */
1025 {V4L2_PIX_FMT_MJPEG, TRUE, GST_V4L2_CODEC},
1026 {V4L2_PIX_FMT_JPEG, TRUE, GST_V4L2_CODEC},
1027 #ifdef V4L2_PIX_FMT_PJPG
1028 {V4L2_PIX_FMT_PJPG, TRUE, GST_V4L2_CODEC},
1030 {V4L2_PIX_FMT_DV, TRUE, GST_V4L2_TRANSPORT},
1031 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
1032 {V4L2_PIX_FMT_MPEG1, TRUE, GST_V4L2_CODEC},
1033 {V4L2_PIX_FMT_MPEG2, TRUE, GST_V4L2_CODEC},
1034 {V4L2_PIX_FMT_MPEG4, TRUE, GST_V4L2_CODEC},
1036 #ifdef V4L2_PIX_FMT_H263
1037 {V4L2_PIX_FMT_H263, TRUE, GST_V4L2_CODEC},
1039 #ifdef V4L2_PIX_FMT_H264
1040 {V4L2_PIX_FMT_H264, TRUE, GST_V4L2_CODEC},
1042 #ifdef V4L2_PIX_FMT_VP8
1043 {V4L2_PIX_FMT_VP8, TRUE, GST_V4L2_CODEC},
1046 /* Vendor-specific formats */
1047 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
1049 #ifdef V4L2_PIX_FMT_SN9C10X
1050 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
1052 #ifdef V4L2_PIX_FMT_PWC1
1053 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
1055 #ifdef V4L2_PIX_FMT_PWC2
1056 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
1058 #ifdef V4L2_PIX_FMT_YVYU
1059 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
1063 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
1066 static struct v4l2_fmtdesc *
1067 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
1070 struct v4l2_fmtdesc *fmt;
1076 walk = gst_v4l2_object_get_format_list (v4l2object);
1078 fmt = (struct v4l2_fmtdesc *) walk->data;
1079 if (fmt->pixelformat == fourcc)
1081 /* special case for jpeg */
1082 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
1083 fmt->pixelformat == V4L2_PIX_FMT_JPEG
1084 #ifdef V4L2_PIX_FMT_PJPG
1085 || fmt->pixelformat == V4L2_PIX_FMT_PJPG
1088 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG
1089 #ifdef V4L2_PIX_FMT_PJPG
1090 || fourcc == V4L2_PIX_FMT_PJPG
1096 walk = g_slist_next (walk);
1104 /* complete made up ranking, the values themselves are meaningless */
1105 /* These ranks MUST be X such that X<<15 fits on a signed int - see
1106 the comment at the end of gst_v4l2_object_format_get_rank. */
1107 #define YUV_BASE_RANK 1000
1108 #define JPEG_BASE_RANK 500
1109 #define DV_BASE_RANK 200
1110 #define RGB_BASE_RANK 100
1111 #define YUV_ODD_BASE_RANK 50
1112 #define RGB_ODD_BASE_RANK 25
1113 #define BAYER_BASE_RANK 15
1114 #define S910_BASE_RANK 10
1115 #define GREY_BASE_RANK 5
1116 #define PWC_BASE_RANK 1
1118 /* This flag is already used by libv4l2 although
1119 * it was added to the Linux kernel in 2.6.32
1121 #ifndef V4L2_FMT_FLAG_EMULATED
1122 #define V4L2_FMT_FLAG_EMULATED 0x0002
1126 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
1128 guint32 fourcc = fmt->pixelformat;
1129 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
1133 case V4L2_PIX_FMT_MJPEG:
1134 #ifdef V4L2_PIX_FMT_PJPG
1135 case V4L2_PIX_FMT_PJPG:
1136 rank = JPEG_BASE_RANK;
1139 case V4L2_PIX_FMT_JPEG:
1140 rank = JPEG_BASE_RANK + 1;
1142 case V4L2_PIX_FMT_MPEG: /* MPEG */
1143 rank = JPEG_BASE_RANK + 2;
1146 case V4L2_PIX_FMT_RGB332:
1147 case V4L2_PIX_FMT_RGB555:
1148 case V4L2_PIX_FMT_RGB555X:
1149 case V4L2_PIX_FMT_RGB565:
1150 case V4L2_PIX_FMT_RGB565X:
1151 rank = RGB_ODD_BASE_RANK;
1154 case V4L2_PIX_FMT_RGB24:
1155 case V4L2_PIX_FMT_BGR24:
1156 rank = RGB_BASE_RANK - 1;
1159 case V4L2_PIX_FMT_RGB32:
1160 case V4L2_PIX_FMT_BGR32:
1161 rank = RGB_BASE_RANK;
1164 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1165 rank = GREY_BASE_RANK;
1168 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1169 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
1170 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1171 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1172 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1173 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1174 rank = YUV_ODD_BASE_RANK;
1177 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1178 rank = YUV_BASE_RANK + 3;
1180 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1181 rank = YUV_BASE_RANK + 2;
1183 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1184 rank = YUV_BASE_RANK + 7;
1186 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1187 rank = YUV_BASE_RANK + 10;
1189 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1190 rank = YUV_BASE_RANK + 6;
1192 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1193 rank = YUV_BASE_RANK + 9;
1195 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1196 rank = YUV_BASE_RANK + 5;
1198 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1199 rank = YUV_BASE_RANK + 4;
1201 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1202 rank = YUV_BASE_RANK + 8;
1205 case V4L2_PIX_FMT_DV:
1206 rank = DV_BASE_RANK;
1209 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1213 #ifdef V4L2_PIX_FMT_SBGGR8
1214 case V4L2_PIX_FMT_SBGGR8:
1215 rank = BAYER_BASE_RANK;
1219 #ifdef V4L2_PIX_FMT_SN9C10X
1220 case V4L2_PIX_FMT_SN9C10X:
1221 rank = S910_BASE_RANK;
1225 #ifdef V4L2_PIX_FMT_PWC1
1226 case V4L2_PIX_FMT_PWC1:
1227 rank = PWC_BASE_RANK;
1230 #ifdef V4L2_PIX_FMT_PWC2
1231 case V4L2_PIX_FMT_PWC2:
1232 rank = PWC_BASE_RANK;
1241 /* All ranks are below 1<<15 so a shift by 15
1242 * will a) make all non-emulated formats larger
1243 * than emulated and b) will not overflow
1254 format_cmp_func (gconstpointer a, gconstpointer b)
1256 const struct v4l2_fmtdesc *fa = a;
1257 const struct v4l2_fmtdesc *fb = b;
1259 if (fa->pixelformat == fb->pixelformat)
1262 return gst_v4l2_object_format_get_rank (fb) -
1263 gst_v4l2_object_format_get_rank (fa);
1266 /******************************************************
1267 * gst_v4l2_object_fill_format_list():
1268 * create list of supported capture formats
1269 * return value: TRUE on success, FALSE on error
1270 ******************************************************/
1272 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1273 enum v4l2_buf_type type)
1276 struct v4l2_fmtdesc *format;
1278 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1280 /* format enumeration */
1282 format = g_new0 (struct v4l2_fmtdesc, 1);
1285 format->type = type;
1287 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1288 if (errno == EINVAL) {
1290 break; /* end of enumeration */
1296 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1297 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1298 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1299 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1300 format->description);
1301 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1302 GST_FOURCC_ARGS (format->pixelformat));
1304 /* sort formats according to our preference; we do this, because caps
1305 * are probed in the order the formats are in the list, and the order of
1306 * formats in the final probed caps matters for things like fixation */
1307 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1308 (GCompareFunc) format_cmp_func);
1311 #ifndef GST_DISABLE_GST_DEBUG
1315 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1316 for (l = v4l2object->formats; l != NULL; l = l->next) {
1319 GST_INFO_OBJECT (v4l2object->element,
1320 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1321 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1331 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1332 (_("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)));
1339 * Get the list of supported capture formats, a list of
1340 * <code>struct v4l2_fmtdesc</code>.
1343 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1345 if (!v4l2object->formats) {
1347 /* check usual way */
1348 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1350 /* if our driver supports multi-planar
1351 * and if formats are still empty then we can workaround driver bug
1352 * by also looking up formats as if our device was not supporting
1354 if (!v4l2object->formats) {
1355 switch (v4l2object->type) {
1356 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1357 gst_v4l2_object_fill_format_list (v4l2object,
1358 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1361 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1362 gst_v4l2_object_fill_format_list (v4l2object,
1363 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1371 return v4l2object->formats;
1374 static GstVideoFormat
1375 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1377 GstVideoFormat format;
1380 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1381 format = GST_VIDEO_FORMAT_GRAY8;
1383 case V4L2_PIX_FMT_RGB555:
1384 format = GST_VIDEO_FORMAT_RGB15;
1386 case V4L2_PIX_FMT_RGB565:
1387 format = GST_VIDEO_FORMAT_RGB16;
1389 case V4L2_PIX_FMT_RGB24:
1390 format = GST_VIDEO_FORMAT_RGB;
1392 case V4L2_PIX_FMT_BGR24:
1393 format = GST_VIDEO_FORMAT_BGR;
1395 case V4L2_PIX_FMT_RGB32:
1396 format = GST_VIDEO_FORMAT_RGBx;
1398 case V4L2_PIX_FMT_BGR32:
1399 format = GST_VIDEO_FORMAT_BGRx;
1401 case V4L2_PIX_FMT_NV12:
1402 case V4L2_PIX_FMT_NV12M:
1403 format = GST_VIDEO_FORMAT_NV12;
1405 case V4L2_PIX_FMT_NV21:
1406 case V4L2_PIX_FMT_NV21M:
1407 format = GST_VIDEO_FORMAT_NV21;
1409 case V4L2_PIX_FMT_YVU410:
1410 format = GST_VIDEO_FORMAT_YVU9;
1412 case V4L2_PIX_FMT_YUV410:
1413 format = GST_VIDEO_FORMAT_YUV9;
1415 case V4L2_PIX_FMT_YUV420:
1416 format = GST_VIDEO_FORMAT_I420;
1418 case V4L2_PIX_FMT_YUYV:
1419 format = GST_VIDEO_FORMAT_YUY2;
1421 case V4L2_PIX_FMT_YVU420:
1422 format = GST_VIDEO_FORMAT_YV12;
1424 case V4L2_PIX_FMT_UYVY:
1425 format = GST_VIDEO_FORMAT_UYVY;
1428 case V4L2_PIX_FMT_Y41P:
1429 format = GST_VIDEO_FORMAT_Y41P;
1432 case V4L2_PIX_FMT_YUV411P:
1433 format = GST_VIDEO_FORMAT_Y41B;
1435 case V4L2_PIX_FMT_YUV422P:
1436 format = GST_VIDEO_FORMAT_Y42B;
1438 #ifdef V4L2_PIX_FMT_YVYU
1439 case V4L2_PIX_FMT_YVYU:
1440 format = GST_VIDEO_FORMAT_YVYU;
1444 format = GST_VIDEO_FORMAT_UNKNOWN;
1445 g_assert_not_reached ();
1452 static GstStructure *
1453 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1455 GstStructure *structure = NULL;
1458 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1459 #ifdef V4L2_PIX_FMT_PJPG
1460 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1462 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1463 structure = gst_structure_new_empty ("image/jpeg");
1465 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1466 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1467 /* FIXME: get correct fourccs here */
1469 case V4L2_PIX_FMT_MPEG1:
1470 structure = gst_structure_new ("video/mpeg",
1471 "mpegversion", G_TYPE_INT, 2, NULL);
1473 case V4L2_PIX_FMT_MPEG2:
1474 structure = gst_structure_new ("video/mpeg",
1475 "mpegversion", G_TYPE_INT, 2, NULL);
1477 case V4L2_PIX_FMT_MPEG4:
1478 structure = gst_structure_new ("video/mpeg",
1479 "mpegversion", G_TYPE_INT, 4, "systemstream",
1480 G_TYPE_BOOLEAN, FALSE, NULL);
1482 #ifdef V4L2_PIX_FMT_H263
1483 case V4L2_PIX_FMT_H263:
1484 structure = gst_structure_new ("video/x-h263",
1485 "variant", G_TYPE_STRING, "itu", NULL);
1488 #ifdef V4L2_PIX_FMT_H264
1489 case V4L2_PIX_FMT_H264: /* H.264 */
1490 structure = gst_structure_new ("video/x-h264",
1491 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1492 G_TYPE_STRING, "au", NULL);
1495 #ifdef V4L2_PIX_FMT_VP8
1496 case V4L2_PIX_FMT_VP8:
1497 structure = gst_structure_new_empty ("video/x-vp8");
1500 case V4L2_PIX_FMT_RGB332:
1501 case V4L2_PIX_FMT_RGB555X:
1502 case V4L2_PIX_FMT_RGB565X:
1503 /* FIXME: get correct fourccs here */
1505 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1506 case V4L2_PIX_FMT_RGB555:
1507 case V4L2_PIX_FMT_RGB565:
1508 case V4L2_PIX_FMT_RGB24:
1509 case V4L2_PIX_FMT_BGR24:
1510 case V4L2_PIX_FMT_RGB32:
1511 case V4L2_PIX_FMT_BGR32:
1512 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1513 case V4L2_PIX_FMT_NV12M:
1514 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1515 case V4L2_PIX_FMT_NV21M:
1516 case V4L2_PIX_FMT_YVU410:
1517 case V4L2_PIX_FMT_YUV410:
1518 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1519 case V4L2_PIX_FMT_YUYV:
1520 case V4L2_PIX_FMT_YVU420:
1521 case V4L2_PIX_FMT_UYVY:
1523 case V4L2_PIX_FMT_Y41P:
1525 case V4L2_PIX_FMT_YUV422P:
1526 #ifdef V4L2_PIX_FMT_YVYU
1527 case V4L2_PIX_FMT_YVYU:
1529 case V4L2_PIX_FMT_YUV411P:{
1530 GstVideoFormat format;
1531 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1532 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1533 structure = gst_structure_new ("video/x-raw",
1534 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1537 case V4L2_PIX_FMT_DV:
1539 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1542 case V4L2_PIX_FMT_MPEG: /* MPEG */
1543 structure = gst_structure_new_empty ("video/mpegts");
1545 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1547 #ifdef V4L2_PIX_FMT_SBGGR8
1548 case V4L2_PIX_FMT_SBGGR8:
1549 structure = gst_structure_new_empty ("video/x-bayer");
1552 #ifdef V4L2_PIX_FMT_SN9C10X
1553 case V4L2_PIX_FMT_SN9C10X:
1554 structure = gst_structure_new_empty ("video/x-sonix");
1557 #ifdef V4L2_PIX_FMT_PWC1
1558 case V4L2_PIX_FMT_PWC1:
1559 structure = gst_structure_new_empty ("video/x-pwc1");
1562 #ifdef V4L2_PIX_FMT_PWC2
1563 case V4L2_PIX_FMT_PWC2:
1564 structure = gst_structure_new_empty ("video/x-pwc2");
1568 GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
1569 fourcc, GST_FOURCC_ARGS (fourcc));
1578 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1580 GstStructure *structure;
1584 caps = gst_caps_new_empty ();
1585 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1587 if ((gst_v4l2_formats[i].flags & flags) == 0)
1591 gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
1593 if (gst_v4l2_formats[i].dimensions) {
1594 gst_structure_set (structure,
1595 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1596 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1597 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
1599 gst_caps_append_structure (caps, structure);
1603 return gst_caps_simplify (caps);
1607 gst_v4l2_object_get_all_caps (void)
1609 static GstCaps *caps = NULL;
1612 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1614 return gst_caps_ref (caps);
1618 gst_v4l2_object_get_raw_caps (void)
1620 static GstCaps *caps = NULL;
1623 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1625 return gst_caps_ref (caps);
1629 gst_v4l2_object_get_codec_caps (void)
1631 static GstCaps *caps = NULL;
1634 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1636 return gst_caps_ref (caps);
1639 /* gst_v4l2_object_choose_fourcc:
1640 * @obj a #GstV4l2Object
1641 * @fourcc_splane The format type in single plane representation
1642 * @fourcc_mplane The format type in multi-plane representation
1643 * @fourcc Set to the first format to try
1644 * @fourcc_alt The alternative format to use, or zero if mplane is not
1645 * supported. Note that if alternate is used, the prefered_non_contiguous
1646 * setting need to be inversed.
1648 * Certain format can be stored into multi-planar buffer type with two
1649 * representation. As an example, NV12, which has two planes, can be stored
1650 * into 1 plane of multi-planar buffer sturcture, or two. This function will
1651 * choose the right format to use base on the object settings.
1654 gst_v4l2_object_choose_fourcc (GstV4l2Object * obj, guint32 fourcc_splane,
1655 guint32 fourcc_mplane, guint32 * fourcc, guint32 * fourcc_alt)
1657 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
1658 if (obj->prefered_non_contiguous) {
1659 *fourcc = fourcc_mplane;
1660 *fourcc_alt = fourcc_splane;
1662 *fourcc = fourcc_splane;
1663 *fourcc_alt = fourcc_mplane;
1666 *fourcc = fourcc_splane;
1671 /* collect data for the given caps
1672 * @caps: given input caps
1673 * @format: location for the v4l format
1674 * @w/@h: location for width and height
1675 * @fps_n/@fps_d: location for framerate
1676 * @size: location for expected size of the frame or 0 if unknown
1679 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1680 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1682 GstStructure *structure;
1683 guint32 fourcc, fourcc_alt = 0;
1684 const gchar *mimetype;
1685 struct v4l2_fmtdesc *fmt;
1687 /* default unknown values */
1690 structure = gst_caps_get_structure (caps, 0);
1692 mimetype = gst_structure_get_name (structure);
1694 if (!gst_video_info_from_caps (info, caps))
1695 goto invalid_format;
1697 if (g_str_equal (mimetype, "video/x-raw")) {
1698 switch (GST_VIDEO_INFO_FORMAT (info)) {
1699 case GST_VIDEO_FORMAT_I420:
1700 fourcc = V4L2_PIX_FMT_YUV420;
1702 case GST_VIDEO_FORMAT_YUY2:
1703 fourcc = V4L2_PIX_FMT_YUYV;
1706 case GST_VIDEO_FORMAT_Y41P:
1707 fourcc = V4L2_PIX_FMT_Y41P;
1710 case GST_VIDEO_FORMAT_UYVY:
1711 fourcc = V4L2_PIX_FMT_UYVY;
1713 case GST_VIDEO_FORMAT_YV12:
1714 fourcc = V4L2_PIX_FMT_YVU420;
1716 case GST_VIDEO_FORMAT_Y41B:
1717 fourcc = V4L2_PIX_FMT_YUV411P;
1719 case GST_VIDEO_FORMAT_Y42B:
1720 fourcc = V4L2_PIX_FMT_YUV422P;
1722 case GST_VIDEO_FORMAT_NV12:
1723 gst_v4l2_object_choose_fourcc (v4l2object, V4L2_PIX_FMT_NV12,
1724 V4L2_PIX_FMT_NV12M, &fourcc, &fourcc_alt);
1726 case GST_VIDEO_FORMAT_NV21:
1727 gst_v4l2_object_choose_fourcc (v4l2object, V4L2_PIX_FMT_NV21,
1728 V4L2_PIX_FMT_NV21M, &fourcc, &fourcc_alt);
1730 #ifdef V4L2_PIX_FMT_YVYU
1731 case GST_VIDEO_FORMAT_YVYU:
1732 fourcc = V4L2_PIX_FMT_YVYU;
1735 case GST_VIDEO_FORMAT_RGB15:
1736 fourcc = V4L2_PIX_FMT_RGB555;
1738 case GST_VIDEO_FORMAT_RGB16:
1739 fourcc = V4L2_PIX_FMT_RGB565;
1741 case GST_VIDEO_FORMAT_RGB:
1742 fourcc = V4L2_PIX_FMT_RGB24;
1744 case GST_VIDEO_FORMAT_BGR:
1745 fourcc = V4L2_PIX_FMT_BGR24;
1747 case GST_VIDEO_FORMAT_RGBx:
1748 case GST_VIDEO_FORMAT_RGBA:
1749 fourcc = V4L2_PIX_FMT_RGB32;
1751 case GST_VIDEO_FORMAT_BGRx:
1752 case GST_VIDEO_FORMAT_BGRA:
1753 fourcc = V4L2_PIX_FMT_BGR32;
1755 case GST_VIDEO_FORMAT_GRAY8:
1756 fourcc = V4L2_PIX_FMT_GREY;
1761 if (g_str_equal (mimetype, "video/mpegts")) {
1762 fourcc = V4L2_PIX_FMT_MPEG;
1763 } else if (g_str_equal (mimetype, "video/x-dv")) {
1764 fourcc = V4L2_PIX_FMT_DV;
1765 } else if (g_str_equal (mimetype, "image/jpeg")) {
1766 fourcc = V4L2_PIX_FMT_JPEG;
1767 } else if (g_str_equal (mimetype, "video/mpeg")) {
1769 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1772 fourcc = V4L2_PIX_FMT_MPEG1;
1775 fourcc = V4L2_PIX_FMT_MPEG2;
1778 fourcc = V4L2_PIX_FMT_MPEG4;
1784 #ifdef V4L2_PIX_FMT_H263
1785 } else if (g_str_equal (mimetype, "video/x-h263")) {
1786 fourcc = V4L2_PIX_FMT_H263;
1788 #ifdef V4L2_PIX_FMT_H264
1789 } else if (g_str_equal (mimetype, "video/x-h264")) {
1790 fourcc = V4L2_PIX_FMT_H264;
1792 #ifdef V4L2_PIX_FMT_VP8
1793 } else if (g_str_equal (mimetype, "video/x-vp8")) {
1794 fourcc = V4L2_PIX_FMT_VP8;
1796 #ifdef V4L2_PIX_FMT_SBGGR8
1797 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1798 fourcc = V4L2_PIX_FMT_SBGGR8;
1800 #ifdef V4L2_PIX_FMT_SN9C10X
1801 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1802 fourcc = V4L2_PIX_FMT_SN9C10X;
1804 #ifdef V4L2_PIX_FMT_PWC1
1805 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1806 fourcc = V4L2_PIX_FMT_PWC1;
1808 #ifdef V4L2_PIX_FMT_PWC2
1809 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1810 fourcc = V4L2_PIX_FMT_PWC2;
1816 goto unhandled_format;
1818 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1820 if (fmt == NULL && fourcc_alt != 0) {
1821 GST_DEBUG_OBJECT (v4l2object, "No support for %" GST_FOURCC_FORMAT
1822 " trying %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc),
1823 GST_FOURCC_ARGS (fourcc_alt));
1824 v4l2object->prefered_non_contiguous = !v4l2object->prefered_non_contiguous;
1825 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_alt);
1829 goto unsupported_format;
1838 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1843 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1848 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1854 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1855 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced);
1858 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1860 struct v4l2_cropcap cropcap;
1861 int num = 1, den = 1;
1863 if (!v4l2object->keep_aspect)
1866 if (v4l2object->par) {
1867 num = gst_value_get_fraction_numerator (v4l2object->par);
1868 den = gst_value_get_fraction_denominator (v4l2object->par);
1872 memset (&cropcap, 0, sizeof (cropcap));
1874 cropcap.type = v4l2object->type;
1875 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
1876 goto cropcap_failed;
1878 num = cropcap.pixelaspect.numerator;
1879 den = cropcap.pixelaspect.denominator;
1882 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, num, den,
1887 if (errno != ENOTTY)
1888 GST_WARNING_OBJECT (v4l2object->element,
1889 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
1890 g_strerror (errno));
1895 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1896 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1897 static GstStructure *
1898 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1899 guint32 pixelformat,
1900 guint32 width, guint32 height, const GstStructure * template)
1902 gint fd = v4l2object->video_fd;
1903 struct v4l2_frmivalenum ival;
1906 GValue rates = { 0, };
1907 gboolean interlaced;
1908 gint int_width = width;
1909 gint int_height = height;
1911 if (v4l2object->never_interlaced) {
1914 /* Interlaced detection using VIDIOC_TRY/S_FMT */
1915 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat,
1916 &int_width, &int_height, &interlaced))
1920 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1922 ival.pixel_format = pixelformat;
1924 ival.height = height;
1926 GST_LOG_OBJECT (v4l2object->element,
1927 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1928 GST_FOURCC_ARGS (pixelformat));
1930 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1931 * fraction to get framerate */
1932 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1933 goto enum_frameintervals_failed;
1935 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1936 GValue rate = { 0, };
1938 g_value_init (&rates, GST_TYPE_LIST);
1939 g_value_init (&rate, GST_TYPE_FRACTION);
1942 num = ival.discrete.numerator;
1943 denom = ival.discrete.denominator;
1945 if (num > G_MAXINT || denom > G_MAXINT) {
1946 /* let us hope we don't get here... */
1951 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1954 /* swap to get the framerate */
1955 gst_value_set_fraction (&rate, denom, num);
1956 gst_value_list_append_value (&rates, &rate);
1959 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1960 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1961 GValue min = { 0, };
1962 GValue step = { 0, };
1963 GValue max = { 0, };
1964 gboolean added = FALSE;
1965 guint32 minnum, mindenom;
1966 guint32 maxnum, maxdenom;
1968 g_value_init (&rates, GST_TYPE_LIST);
1970 g_value_init (&min, GST_TYPE_FRACTION);
1971 g_value_init (&step, GST_TYPE_FRACTION);
1972 g_value_init (&max, GST_TYPE_FRACTION);
1975 minnum = ival.stepwise.min.numerator;
1976 mindenom = ival.stepwise.min.denominator;
1977 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1981 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1983 gst_value_set_fraction (&min, minnum, mindenom);
1986 maxnum = ival.stepwise.max.numerator;
1987 maxdenom = ival.stepwise.max.denominator;
1988 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1993 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1995 gst_value_set_fraction (&max, maxnum, maxdenom);
1998 num = ival.stepwise.step.numerator;
1999 denom = ival.stepwise.step.denominator;
2000 if (num > G_MAXINT || denom > G_MAXINT) {
2005 if (num == 0 || denom == 0) {
2006 /* in this case we have a wrong fraction or no step, set the step to max
2007 * so that we only add the min value in the loop below */
2012 /* since we only have gst_value_fraction_subtract and not add, negate the
2014 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
2016 gst_value_set_fraction (&step, -num, denom);
2018 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2019 GValue rate = { 0, };
2021 num = gst_value_get_fraction_numerator (&min);
2022 denom = gst_value_get_fraction_denominator (&min);
2023 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
2026 /* invert to get the framerate */
2027 g_value_init (&rate, GST_TYPE_FRACTION);
2028 gst_value_set_fraction (&rate, denom, num);
2029 gst_value_list_append_value (&rates, &rate);
2032 /* we're actually adding because step was negated above. This is because
2033 * there is no _add function... */
2034 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2035 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
2040 /* no range was added, leave the default range from the template */
2041 GST_WARNING_OBJECT (v4l2object->element,
2042 "no range added, leaving default");
2043 g_value_unset (&rates);
2045 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2046 guint32 maxnum, maxdenom;
2048 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2050 num = ival.stepwise.min.numerator;
2051 denom = ival.stepwise.min.denominator;
2052 if (num > G_MAXINT || denom > G_MAXINT) {
2057 maxnum = ival.stepwise.max.numerator;
2058 maxdenom = ival.stepwise.max.denominator;
2059 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2064 GST_LOG_OBJECT (v4l2object->element,
2065 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2068 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2074 s = gst_structure_copy (template);
2075 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2076 "height", G_TYPE_INT, (gint) height, NULL);
2077 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2078 if (g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2079 gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
2080 (interlaced ? "mixed" : "progressive"), NULL);
2082 if (G_IS_VALUE (&rates)) {
2083 /* only change the framerate on the template when we have a valid probed new
2085 gst_structure_set_value (s, "framerate", &rates);
2086 g_value_unset (&rates);
2087 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2088 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2089 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
2095 enum_frameintervals_failed:
2097 GST_DEBUG_OBJECT (v4l2object->element,
2098 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2099 GST_FOURCC_ARGS (pixelformat), width, height);
2104 /* I don't see how this is actually an error, we ignore the format then */
2105 GST_WARNING_OBJECT (v4l2object->element,
2106 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2107 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2111 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
2113 #ifdef VIDIOC_ENUM_FRAMESIZES
2115 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2119 gst_structure_get_int (s1, "width", &w1);
2120 gst_structure_get_int (s1, "height", &h1);
2121 gst_structure_get_int (s2, "width", &w2);
2122 gst_structure_get_int (s2, "height", &h2);
2124 /* I think it's safe to assume that this won't overflow for a while */
2125 return ((w2 * h2) - (w1 * h1));
2130 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2131 guint32 pixelformat, const GstStructure * template)
2133 GstCaps *ret = gst_caps_new_empty ();
2136 #ifdef VIDIOC_ENUM_FRAMESIZES
2137 gint fd = v4l2object->video_fd;
2138 struct v4l2_frmsizeenum size;
2139 GList *results = NULL;
2142 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
2143 return gst_caps_new_empty_simple ("video/mpegts");
2145 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2147 size.pixel_format = pixelformat;
2149 GST_DEBUG_OBJECT (v4l2object->element,
2150 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2151 GST_FOURCC_ARGS (pixelformat));
2153 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2154 goto enum_framesizes_failed;
2156 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2158 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
2159 size.discrete.width, size.discrete.height);
2161 w = MIN (size.discrete.width, G_MAXINT);
2162 h = MIN (size.discrete.height, G_MAXINT);
2166 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2167 pixelformat, w, h, template);
2170 results = g_list_prepend (results, tmp);
2174 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2175 GST_DEBUG_OBJECT (v4l2object->element,
2176 "done iterating discrete frame sizes");
2177 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2178 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
2179 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2180 size.stepwise.min_width);
2181 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2182 size.stepwise.min_height);
2183 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2184 size.stepwise.max_width);
2185 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2186 size.stepwise.max_height);
2187 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
2188 size.stepwise.step_width);
2189 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
2190 size.stepwise.step_height);
2192 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
2193 w <= size.stepwise.max_width && h <= size.stepwise.max_height;
2194 w += size.stepwise.step_width, h += size.stepwise.step_height) {
2195 if (w == 0 || h == 0)
2199 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2200 pixelformat, w, h, template);
2203 results = g_list_prepend (results, tmp);
2205 GST_DEBUG_OBJECT (v4l2object->element,
2206 "done iterating stepwise frame sizes");
2207 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2210 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
2211 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2212 size.stepwise.min_width);
2213 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2214 size.stepwise.min_height);
2215 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2216 size.stepwise.max_width);
2217 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2218 size.stepwise.max_height);
2220 w = MAX (size.stepwise.min_width, 1);
2221 h = MAX (size.stepwise.min_height, 1);
2222 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2223 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2226 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2229 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2230 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2233 /* no point using the results list here, since there's only one struct */
2234 gst_caps_append_structure (ret, tmp);
2240 /* we use an intermediary list to store and then sort the results of the
2241 * probing because we can't make any assumptions about the order in which
2242 * the driver will give us the sizes, but we want the final caps to contain
2243 * the results starting with the highest resolution and having the lowest
2244 * resolution last, since order in caps matters for things like fixation. */
2245 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2246 while (results != NULL) {
2247 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
2248 results = g_list_delete_link (results, results);
2251 if (gst_caps_is_empty (ret))
2252 goto enum_framesizes_no_results;
2257 enum_framesizes_failed:
2259 /* I don't see how this is actually an error */
2260 GST_DEBUG_OBJECT (v4l2object->element,
2261 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2262 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2263 goto default_frame_sizes;
2265 enum_framesizes_no_results:
2267 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2268 * question doesn't actually support it yet */
2269 GST_DEBUG_OBJECT (v4l2object->element,
2270 "No results for pixelformat %" GST_FOURCC_FORMAT
2271 " enumerating frame sizes, trying fallback",
2272 GST_FOURCC_ARGS (pixelformat));
2273 goto default_frame_sizes;
2277 GST_WARNING_OBJECT (v4l2object->element,
2278 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2279 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2280 goto default_frame_sizes;
2282 default_frame_sizes:
2283 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
2285 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2286 gboolean interlaced;
2288 /* This code is for Linux < 2.6.19 */
2290 max_w = max_h = GST_V4L2_MAX_SIZE;
2291 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2292 &min_h, &interlaced)) {
2293 GST_WARNING_OBJECT (v4l2object->element,
2294 "Could not probe minimum capture size for pixelformat %"
2295 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2297 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2298 &max_h, &interlaced)) {
2299 GST_WARNING_OBJECT (v4l2object->element,
2300 "Could not probe maximum capture size for pixelformat %"
2301 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2304 /* Since we can't get framerate directly, try to use the current norm */
2305 if (v4l2object->tv_norm && v4l2object->norms) {
2307 GstTunerNorm *norm = NULL;
2308 GstTunerNorm *current =
2309 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2311 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2312 norm = (GstTunerNorm *) norms->data;
2313 if (!strcmp (norm->label, current->label))
2316 /* If it's possible, set framerate to that (discrete) value */
2318 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2319 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2323 tmp = gst_structure_copy (template);
2325 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2327 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2328 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2329 /* if norm can't be used, copy the template framerate */
2330 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2335 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2337 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2340 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2342 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2344 if (g_str_equal (gst_structure_get_name (tmp), "video/x-raw"))
2345 gst_structure_set (tmp, "interlace-mode", G_TYPE_STRING,
2346 (interlaced ? "mixed" : "progressive"), NULL);
2347 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2349 gst_caps_append_structure (ret, tmp);
2356 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2357 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced)
2359 struct v4l2_format fmt, prevfmt;
2362 int prevfmt_valid = FALSE;
2363 gboolean ret = FALSE;
2365 g_return_val_if_fail (width != NULL, FALSE);
2366 g_return_val_if_fail (height != NULL, FALSE);
2368 GST_LOG_OBJECT (v4l2object->element,
2369 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2370 *width, *height, GST_FOURCC_ARGS (pixelformat));
2372 fd = v4l2object->video_fd;
2374 memset (&fmt, 0, sizeof (struct v4l2_format));
2375 memset (&prevfmt, 0, sizeof (struct v4l2_format));
2377 /* Some drivers are buggy and will modify the currently set format
2378 when processing VIDIOC_TRY_FMT, so we remember what is set at the
2379 minute, and will reset it when done. */
2380 if (!v4l2object->no_initial_format) {
2381 prevfmt.type = v4l2object->type;
2382 prevfmt_valid = (v4l2_ioctl (fd, VIDIOC_G_FMT, &prevfmt) >= 0);
2385 /* get size delimiters */
2386 memset (&fmt, 0, sizeof (fmt));
2387 fmt.type = v4l2object->type;
2388 fmt.fmt.pix.width = *width;
2389 fmt.fmt.pix.height = *height;
2390 fmt.fmt.pix.pixelformat = pixelformat;
2391 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2393 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2394 if (r < 0 && errno == EINVAL) {
2395 /* try again with interlaced video */
2396 fmt.fmt.pix.width = *width;
2397 fmt.fmt.pix.height = *height;
2398 fmt.fmt.pix.pixelformat = pixelformat;
2399 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2400 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2404 /* The driver might not implement TRY_FMT, in which case we will try
2406 if (errno != ENOTTY)
2409 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
2410 be, because we're still probing */
2411 if (GST_V4L2_IS_ACTIVE (v4l2object))
2414 GST_LOG_OBJECT (v4l2object->element,
2415 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
2417 fmt.fmt.pix.width = *width;
2418 fmt.fmt.pix.height = *height;
2420 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2421 if (r < 0 && errno == EINVAL) {
2422 /* try again with progressive video */
2423 fmt.fmt.pix.width = *width;
2424 fmt.fmt.pix.height = *height;
2425 fmt.fmt.pix.pixelformat = pixelformat;
2426 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2427 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2434 GST_LOG_OBJECT (v4l2object->element,
2435 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2437 *width = fmt.fmt.pix.width;
2438 *height = fmt.fmt.pix.height;
2440 switch (fmt.fmt.pix.field) {
2441 case V4L2_FIELD_ANY:
2442 case V4L2_FIELD_NONE:
2443 *interlaced = FALSE;
2445 case V4L2_FIELD_INTERLACED:
2446 case V4L2_FIELD_INTERLACED_TB:
2447 case V4L2_FIELD_INTERLACED_BT:
2451 GST_WARNING_OBJECT (v4l2object->element,
2452 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u",
2453 GST_FOURCC_ARGS (pixelformat), *width, *height);
2461 GST_WARNING_OBJECT (v4l2object->element,
2462 "Unable to try format: %s", g_strerror (errno));
2465 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &prevfmt) < 0) {
2466 GST_WARNING_OBJECT (v4l2object->element,
2467 "Unable to restore format after trying format: %s",
2468 g_strerror (errno));
2475 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2479 GST_DEBUG_OBJECT (v4l2object->element, "initializing the capture system");
2481 GST_V4L2_CHECK_OPEN (v4l2object);
2482 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2484 /* find transport */
2485 mode = v4l2object->req_mode;
2487 if (v4l2object->vcap.capabilities & V4L2_CAP_READWRITE) {
2488 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2489 mode = GST_V4L2_IO_RW;
2490 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2491 goto method_not_supported;
2493 if (v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
2494 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2495 mode = GST_V4L2_IO_MMAP;
2496 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2497 goto method_not_supported;
2499 /* if still no transport selected, error out */
2500 if (mode == GST_V4L2_IO_AUTO)
2501 goto no_supported_capture_method;
2503 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2504 v4l2object->mode = mode;
2506 /* Map the buffers */
2507 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2509 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2510 goto buffer_pool_new_failed;
2512 GST_V4L2_SET_ACTIVE (v4l2object);
2517 buffer_pool_new_failed:
2519 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2520 (_("Could not map buffers from device '%s'"),
2521 v4l2object->videodev),
2522 ("Failed to create buffer pool: %s", g_strerror (errno)));
2525 method_not_supported:
2527 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2528 (_("The driver of device '%s' does not support the IO method %d"),
2529 v4l2object->videodev, mode), (NULL));
2532 no_supported_capture_method:
2534 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2535 (_("The driver of device '%s' does not support any known IO "
2536 "method."), v4l2object->videodev), (NULL));
2542 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
2543 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
2544 GstVideoInfo * info)
2546 const GstVideoFormatInfo *finfo = info->finfo;
2549 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
2550 /* figure out the frame layout */
2551 v4l2object->n_v4l2_planes = MAX (1, format->fmt.pix_mp.num_planes);
2552 v4l2object->sizeimage = 0;
2553 for (i = 0; i < format->fmt.pix_mp.num_planes; i++) {
2554 v4l2object->bytesperline[i] =
2555 format->fmt.pix_mp.plane_fmt[i].bytesperline;
2556 v4l2object->sizeimage += format->fmt.pix_mp.plane_fmt[i].sizeimage;
2559 /* only one plane in non-MPLANE mode */
2560 v4l2object->n_v4l2_planes = 1;
2562 /* figure out the frame layout */
2563 for (i = 0; i < finfo->n_planes; i++) {
2564 guint stride = format->fmt.pix.bytesperline;
2566 switch (finfo->format) {
2567 case GST_VIDEO_FORMAT_NV12:
2568 case GST_VIDEO_FORMAT_NV21:
2569 case GST_VIDEO_FORMAT_NV16:
2570 case GST_VIDEO_FORMAT_NV24:
2571 v4l2object->bytesperline[i] = (i == 0 ? 1 : 2) *
2572 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, stride);
2575 v4l2object->bytesperline[i] =
2576 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, stride);
2580 GST_DEBUG_OBJECT (v4l2object->element,
2581 "Extrapolated stride for plane %d from %d to %d", i, stride,
2582 v4l2object->bytesperline[i]);
2585 v4l2object->sizeimage = format->fmt.pix.sizeimage;
2588 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %u",
2589 v4l2object->sizeimage);
2591 v4l2object->info = *info;
2592 v4l2object->fmtdesc = fmtdesc;
2594 /* if we have a framerate pre-calculate duration */
2595 if (info->fps_n > 0 && info->fps_d > 0) {
2596 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
2599 v4l2object->duration = GST_CLOCK_TIME_NONE;
2604 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps)
2606 gint fd = v4l2object->video_fd;
2607 struct v4l2_format format;
2608 struct v4l2_streamparm streamparm;
2609 enum v4l2_field field;
2610 guint32 pixelformat;
2611 struct v4l2_fmtdesc *fmtdesc;
2613 gint width, height, fps_n, fps_d;
2616 GST_V4L2_CHECK_OPEN (v4l2object);
2617 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2619 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
2622 pixelformat = fmtdesc->pixelformat;
2623 width = GST_VIDEO_INFO_WIDTH (&info);
2624 height = GST_VIDEO_INFO_HEIGHT (&info);
2625 fps_n = GST_VIDEO_INFO_FPS_N (&info);
2626 fps_d = GST_VIDEO_INFO_FPS_D (&info);
2628 /* get bytesperline for each plane */
2629 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&info); i++)
2630 v4l2object->bytesperline[i] = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
2632 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
2633 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
2634 /* ideally we would differentiate between types of interlaced video
2635 * but there is not sufficient information in the caps..
2637 field = V4L2_FIELD_INTERLACED;
2639 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
2640 field = V4L2_FIELD_NONE;
2643 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
2644 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
2645 GST_FOURCC_ARGS (pixelformat), v4l2object->bytesperline[0]);
2647 /* MPEG-TS source cameras don't get their format set for some reason.
2648 * It looks wrong and we weren't able to track down the reason for that code
2649 * so it is disabled until someone who has an mpeg-ts camera complains...
2652 /* Only unconditionally accept mpegts for sources */
2653 if ((v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
2654 (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')))
2658 memset (&format, 0x00, sizeof (struct v4l2_format));
2659 format.type = v4l2object->type;
2661 if (!v4l2object->no_initial_format) {
2662 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
2663 goto get_fmt_failed;
2666 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
2667 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2668 "%" GST_FOURCC_FORMAT " colorspace %d, nb planes %d",
2669 format.fmt.pix_mp.width, format.fmt.pix_mp.height,
2670 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2671 format.fmt.pix_mp.colorspace, format.fmt.pix_mp.num_planes);
2673 if (format.type != v4l2object->type ||
2674 format.fmt.pix_mp.width != width ||
2675 format.fmt.pix_mp.height != height ||
2676 format.fmt.pix_mp.pixelformat != pixelformat ||
2677 format.fmt.pix_mp.field != field) {
2678 /* even in v4l2 multiplanar mode we can work in contiguous mode
2679 * if the device supports it */
2680 gint n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
2682 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
2683 * or if contiguous is prefered */
2684 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
2687 /* something different, set the format */
2688 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2689 "%" GST_FOURCC_FORMAT, width, height, GST_FOURCC_ARGS (pixelformat));
2691 format.type = v4l2object->type;
2692 format.fmt.pix_mp.pixelformat = pixelformat;
2693 format.fmt.pix_mp.width = width;
2694 format.fmt.pix_mp.height = height;
2695 format.fmt.pix_mp.field = field;
2696 format.fmt.pix_mp.num_planes = n_v4l_planes;
2697 /* try to ask our prefered stride but it's not a failure
2698 * if not accepted */
2699 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
2700 format.fmt.pix_mp.plane_fmt[i].bytesperline =
2701 v4l2object->bytesperline[i];
2703 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED) {
2704 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
2707 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
2708 goto set_fmt_failed;
2710 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2711 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
2712 format.fmt.pix_mp.height,
2713 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2714 format.fmt.pix_mp.num_planes);
2716 #ifndef GST_DISABLE_GST_DEBUG
2717 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
2718 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
2719 format.fmt.pix_mp.plane_fmt[i].bytesperline);
2722 if (format.fmt.pix_mp.pixelformat != pixelformat)
2723 goto invalid_pixelformat;
2725 /* we set the dimensions just in case but don't validate them afterwards
2726 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
2727 * in ASF mode for example. */
2728 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
2729 if (format.fmt.pix_mp.width != width
2730 || format.fmt.pix_mp.height != height)
2731 goto invalid_dimensions;
2734 if (format.fmt.pix_mp.num_planes != n_v4l_planes)
2735 goto invalid_planes;
2738 /* figure out the frame layout */
2739 v4l2object->n_v4l2_planes = format.fmt.pix_mp.num_planes;
2740 v4l2object->sizeimage = 0;
2741 for (i = 0; i < format.fmt.pix_mp.num_planes; i++) {
2742 v4l2object->bytesperline[i] = format.fmt.pix_mp.plane_fmt[i].bytesperline;
2743 v4l2object->sizeimage += format.fmt.pix_mp.plane_fmt[i].sizeimage;
2746 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2747 "%" GST_FOURCC_FORMAT " bytesperline %d, colorspace %d",
2748 format.fmt.pix.width, format.fmt.pix.height,
2749 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2750 format.fmt.pix.bytesperline, format.fmt.pix.colorspace);
2752 if (format.type != v4l2object->type ||
2753 format.fmt.pix.width != width ||
2754 format.fmt.pix.height != height ||
2755 format.fmt.pix.pixelformat != pixelformat ||
2756 format.fmt.pix.field != field) {
2757 /* something different, set the format */
2758 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2759 "%" GST_FOURCC_FORMAT " bytesperline %d", width, height,
2760 GST_FOURCC_ARGS (pixelformat), v4l2object->bytesperline[0]);
2762 format.type = v4l2object->type;
2763 format.fmt.pix.width = width;
2764 format.fmt.pix.height = height;
2765 format.fmt.pix.pixelformat = pixelformat;
2766 format.fmt.pix.field = field;
2767 /* try to ask our prefered stride */
2768 format.fmt.pix.bytesperline = v4l2object->bytesperline[0];
2770 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED) {
2771 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
2774 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
2775 goto set_fmt_failed;
2777 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2778 "%" GST_FOURCC_FORMAT " stride %d", format.fmt.pix.width,
2779 format.fmt.pix.height, GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2780 format.fmt.pix.bytesperline);
2782 /* we set the dimensions just in case but don't validate them afterwards
2783 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
2784 * in ASF mode for example. */
2785 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
2786 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
2787 goto invalid_dimensions;
2790 if (format.fmt.pix.pixelformat != pixelformat)
2791 goto invalid_pixelformat;
2795 /* Is there a reason we require the caller to always specify a framerate? */
2796 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
2799 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
2800 streamparm.type = v4l2object->type;
2802 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
2803 goto get_parm_failed;
2805 GST_VIDEO_INFO_FPS_N (&info) =
2806 streamparm.parm.capture.timeperframe.denominator;
2807 GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
2809 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
2810 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2811 GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
2812 streamparm.parm.capture.timeperframe.denominator,
2813 streamparm.parm.capture.timeperframe.numerator);
2815 /* We used to skip frame rate setup if the camera was already setup
2816 * with the requested frame rate. This breaks some cameras though,
2817 * causing them to not output data (several models of Thinkpad cameras
2818 * have this problem at least).
2819 * So, don't skip. */
2820 GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
2822 /* We want to change the frame rate, so check whether we can. Some cheap USB
2823 * cameras don't have the capability */
2824 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
2825 GST_DEBUG_OBJECT (v4l2object->element,
2826 "Not setting framerate (not supported)");
2830 /* Note: V4L2 wants the frame interval, we have the frame rate */
2831 streamparm.parm.capture.timeperframe.numerator = fps_d;
2832 streamparm.parm.capture.timeperframe.denominator = fps_n;
2834 /* some cheap USB cam's won't accept any change */
2835 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
2836 goto set_parm_failed;
2838 /* get new values */
2839 fps_d = streamparm.parm.capture.timeperframe.numerator;
2840 fps_n = streamparm.parm.capture.timeperframe.denominator;
2842 GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
2845 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
2846 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
2850 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info);
2852 /* now configure ther pools */
2853 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
2861 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
2867 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2868 (_("Device '%s' does not support video capture"),
2869 v4l2object->videodev),
2870 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
2875 if (errno == EBUSY) {
2876 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, BUSY,
2877 (_("Device '%s' is busy"), v4l2object->videodev),
2878 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2879 GST_FOURCC_ARGS (pixelformat), width, height,
2880 g_strerror (errno)));
2882 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2883 (_("Device '%s' cannot capture at %dx%d"),
2884 v4l2object->videodev, width, height),
2885 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2886 GST_FOURCC_ARGS (pixelformat), width, height,
2887 g_strerror (errno)));
2893 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2894 (_("Device '%s' cannot capture at %dx%d"),
2895 v4l2object->videodev, width, height),
2896 ("Tried to capture at %dx%d, but device returned size %dx%d",
2897 width, height, format.fmt.pix.width, format.fmt.pix.height));
2900 invalid_pixelformat:
2902 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2903 (_("Device '%s' cannot capture in the specified format"),
2904 v4l2object->videodev),
2905 ("Tried to capture in %" GST_FOURCC_FORMAT
2906 ", but device returned format" " %" GST_FOURCC_FORMAT,
2907 GST_FOURCC_ARGS (pixelformat),
2908 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
2913 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2914 (_("Device '%s' does support non-contiguous planes"),
2915 v4l2object->videodev),
2916 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
2921 /* it's possible that this call is not supported */
2922 if (errno != EINVAL && errno != ENOTTY) {
2923 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2924 (_("Could not get parameters on device '%s'"),
2925 v4l2object->videodev), GST_ERROR_SYSTEM);
2931 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2932 (_("Video device did not accept new frame rate setting.")),
2938 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2939 (_("Video device could not create buffer pool.")), GST_ERROR_SYSTEM);
2945 * gst_v4l2_object_setup_format:
2946 * @v4l2object the object
2947 * @info a GstVideoInfo to be filled
2948 * @align a GstVideoAlignment to be filled
2950 * Setup the format base on the currently configured format. This is useful in
2951 * decoder or encoder elements where the output format is dictated by the
2954 * Returns: %TRUE on success, %FALSE on failure.
2957 gst_v4l2_object_setup_format (GstV4l2Object * v4l2object,
2958 GstVideoInfo * info, GstVideoAlignment * align)
2960 struct v4l2_fmtdesc *fmtdesc;
2961 struct v4l2_format fmt;
2962 struct v4l2_crop crop;
2963 GstVideoFormat format;
2964 guint width, height;
2966 gst_video_info_init (info);
2967 gst_video_alignment_reset (align);
2969 memset (&fmt, 0x00, sizeof (struct v4l2_format));
2970 fmt.type = v4l2object->type;
2971 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
2972 goto get_fmt_failed;
2974 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
2975 fmt.fmt.pix.pixelformat);
2976 if (fmtdesc == NULL)
2977 goto unsupported_format;
2979 /* No need to care about mplane, the four first params are the same */
2980 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
2982 /* FIXME do more work in the whole function if
2983 * format is GST_VIDEO_FORMAT_ENCODED
2984 * Also gst_v4l2_object_v4l2fourcc_to_video_format should be improved
2985 * because for now it never returns GST_VIDEO_FORMAT_ENCODED
2988 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
2989 if (format == GST_VIDEO_FORMAT_UNKNOWN)
2990 goto unsupported_format;
2992 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
2993 goto invalid_dimensions;
2995 width = fmt.fmt.pix.width;
2996 height = fmt.fmt.pix.height;
2998 memset (&crop, 0, sizeof (struct v4l2_crop));
2999 crop.type = v4l2object->type;
3000 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0) {
3001 align->padding_left = crop.c.left;
3002 align->padding_top = crop.c.top;
3003 align->padding_right = width - crop.c.width - crop.c.left;
3004 align->padding_bottom = height - crop.c.height - crop.c.top;
3005 width = crop.c.width;
3006 height = crop.c.height;
3009 gst_video_info_set_format (info, format, width, height);
3011 switch (fmt.fmt.pix.field) {
3012 case V4L2_FIELD_ANY:
3013 case V4L2_FIELD_NONE:
3014 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
3016 case V4L2_FIELD_INTERLACED:
3017 case V4L2_FIELD_INTERLACED_TB:
3018 case V4L2_FIELD_INTERLACED_BT:
3019 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
3022 goto unsupported_field;
3025 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info);
3027 /* Shall we setup the pool ? */
3033 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3034 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
3039 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3040 (_("Video device returned invalid dimensions.")),
3041 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
3042 fmt.fmt.pix.height));
3047 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3048 (_("Video devices uses an unsupported interlacing method.")),
3049 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
3054 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3055 (_("Video devices uses an unsupported pixel format.")),
3056 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
3057 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
3063 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
3068 if (!v4l2object->pool)
3071 s = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (v4l2object->pool));
3072 gst_buffer_pool_config_get_params (s, &oldcaps, NULL, NULL, NULL);
3074 return oldcaps && gst_caps_is_equal (caps, oldcaps);
3078 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
3080 GST_LOG_OBJECT (v4l2object->element, "flush poll");
3081 gst_poll_set_flushing (v4l2object->poll, TRUE);
3087 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
3089 GST_LOG_OBJECT (v4l2object->element, "flush stop poll");
3090 gst_poll_set_flushing (v4l2object->poll, FALSE);
3096 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
3098 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
3100 if (!GST_V4L2_IS_OPEN (v4l2object))
3102 if (!GST_V4L2_IS_ACTIVE (v4l2object))
3105 if (v4l2object->pool) {
3106 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
3107 gst_buffer_pool_set_active (GST_BUFFER_POOL_CAST (v4l2object->pool), FALSE);
3108 gst_object_unref (v4l2object->pool);
3109 v4l2object->pool = NULL;
3112 GST_V4L2_SET_INACTIVE (v4l2object);
3119 gst_v4l2_object_copy (GstV4l2Object * v4l2object, GstBuffer * dest,
3122 const GstVideoFormatInfo *finfo = v4l2object->info.finfo;
3124 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
3125 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
3126 GstVideoFrame src_frame, dest_frame;
3128 GST_DEBUG_OBJECT (v4l2object->element, "copy video frame");
3130 /* we have raw video, use videoframe copy to get strides right */
3131 if (!gst_video_frame_map (&src_frame, &v4l2object->info, src, GST_MAP_READ))
3132 goto invalid_buffer;
3134 if (!gst_video_frame_map (&dest_frame, &v4l2object->info, dest,
3136 gst_video_frame_unmap (&src_frame);
3137 goto invalid_buffer;
3140 gst_video_frame_copy (&dest_frame, &src_frame);
3142 gst_video_frame_unmap (&src_frame);
3143 gst_video_frame_unmap (&dest_frame);
3147 GST_DEBUG_OBJECT (v4l2object->element, "copy raw bytes");
3148 gst_buffer_map (src, &map, GST_MAP_READ);
3149 gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
3150 gst_buffer_unmap (src, &map);
3151 gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
3153 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, v4l2object->element,
3154 "slow copy into buffer %p", dest);
3161 /* No Window available to put our image into */
3162 GST_WARNING_OBJECT (v4l2object->element, "could not map image");
3168 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3174 if (v4l2object->probed_caps == NULL) {
3175 formats = gst_v4l2_object_get_format_list (v4l2object);
3177 ret = gst_caps_new_empty ();
3179 for (walk = formats; walk; walk = walk->next) {
3180 struct v4l2_fmtdesc *format;
3181 GstStructure *template;
3183 format = (struct v4l2_fmtdesc *) walk->data;
3185 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
3190 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
3191 format->pixelformat, template);
3193 gst_caps_append (ret, tmp);
3195 gst_structure_free (template);
3197 GST_DEBUG_OBJECT (v4l2object->element, "unknown format %u",
3198 format->pixelformat);
3201 v4l2object->probed_caps = ret;
3205 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
3206 GST_CAPS_INTERSECT_FIRST);
3208 ret = gst_caps_ref (v4l2object->probed_caps);
3211 GST_INFO_OBJECT (v4l2object->element, "probed caps: %" GST_PTR_FORMAT, ret);
3212 LOG_CAPS (v4l2object->element, ret);
3218 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
3220 GstBufferPool *pool;
3221 guint size, min, max;
3223 struct v4l2_control ctl = { 0, };
3225 GST_DEBUG_OBJECT (obj->element, "decide allocation");
3227 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3228 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
3230 if (obj->pool == NULL) {
3232 gst_query_parse_allocation (query, &caps, NULL);
3234 if (!gst_v4l2_object_setup_pool (obj, caps))
3238 if (gst_query_get_n_allocation_pools (query) > 0) {
3239 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3248 GST_DEBUG_OBJECT (obj->element, "allocation: size:%u min:%u max:%u pool:%"
3249 GST_PTR_FORMAT, size, min, max, pool);
3252 /* if there is a min-buffers suggestion, use it. We add 1 because we need 1
3253 * buffer extra to capture while the other two buffers are downstream */
3259 /* Certain driver may expose a minimum through controls */
3260 ctl.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
3261 if (v4l2_ioctl (obj->video_fd, VIDIOC_G_CTRL, &ctl) >= 0) {
3262 GST_DEBUG_OBJECT (obj->element, "driver require a minimum of %d buffers",
3267 /* Request a bigger max, if one was suggested but it's too small */
3268 if (max != 0 && max < min)
3272 switch (obj->mode) {
3273 case GST_V4L2_IO_RW:
3275 /* no downstream pool, use our own then */
3276 GST_DEBUG_OBJECT (obj->element,
3277 "read/write mode: no downstream pool, using our own");
3278 pool = GST_BUFFER_POOL_CAST (obj->pool);
3279 size = obj->sizeimage;
3281 /* in READ/WRITE mode, prefer a downstream pool because our own pool
3282 * doesn't help much, we have to write to it as well */
3283 GST_DEBUG_OBJECT (obj->element,
3284 "read/write mode: using downstream pool");
3285 /* use the bigest size, when we use our own pool we can't really do any
3286 * other size than what the hardware gives us but for downstream pools
3288 size = MAX (size, obj->sizeimage);
3291 case GST_V4L2_IO_MMAP:
3292 case GST_V4L2_IO_USERPTR:
3293 case GST_V4L2_IO_DMABUF:
3294 /* in streaming mode, prefer our own pool */
3295 pool = GST_BUFFER_POOL_CAST (obj->pool);
3296 size = obj->sizeimage;
3298 GST_DEBUG_OBJECT (obj->element,
3299 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
3301 case GST_V4L2_IO_AUTO:
3303 GST_WARNING_OBJECT (obj->element, "unhandled mode");
3308 GstStructure *config;
3311 config = gst_buffer_pool_get_config (pool);
3312 gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL);
3313 gst_buffer_pool_config_set_params (config, caps, size, min, max);
3315 /* if downstream supports video metadata, add this to the pool config */
3316 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
3317 GST_DEBUG_OBJECT (pool, "activate Video Meta");
3318 gst_buffer_pool_config_add_option (config,
3319 GST_BUFFER_POOL_OPTION_VIDEO_META);
3322 gst_buffer_pool_set_config (pool, config);
3326 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
3328 gst_query_add_allocation_pool (query, pool, size, min, max);
3334 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
3335 (_("Video device could not create buffer pool.")), GST_ERROR_SYSTEM);