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')
65 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
66 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
67 #define GST_CAT_DEFAULT v4l2_debug
69 #define DEFAULT_PROP_DEVICE_NAME NULL
70 #define DEFAULT_PROP_DEVICE_FD -1
71 #define DEFAULT_PROP_FLAGS 0
72 #define DEFAULT_PROP_TV_NORM 0
73 #define DEFAULT_PROP_CHANNEL NULL
74 #define DEFAULT_PROP_FREQUENCY 0
75 #define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
80 V4L2_STD_OBJECT_PROPS,
83 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
86 G_LOCK_DEFINE_STATIC (probe_lock);
89 gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
91 GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
92 static GList *list = NULL;
97 list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
100 G_UNLOCK (probe_lock);
105 static gboolean init = FALSE;
106 static GList *devices = NULL;
110 gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
111 GList ** klass_devices)
113 GUdevClient *client = NULL;
118 gchar *device = devices->data;
119 devices = g_list_remove (devices, device);
123 GST_INFO ("Enumerating video4linux devices from udev");
124 client = g_udev_client_new (NULL);
126 GST_WARNING ("Failed to initialize gudev client");
130 item = g_udev_client_query_by_subsystem (client, "video4linux");
132 GUdevDevice *device = item->data;
133 gchar *devnode = g_strdup (g_udev_device_get_device_file (device));
134 gint api = g_udev_device_get_property_as_int (device, "ID_V4L_VERSION");
135 GST_INFO ("Found new device: %s, API: %d", devnode, api);
136 /* Append v4l2 devices only. If api is 0 probably v4l_id has
137 been stripped out of the current udev installation, append
141 ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
143 if ((api == 2 || api == 0)) {
144 devices = g_list_append (devices, devnode);
148 g_object_unref (device);
157 g_object_unref (client);
160 *klass_devices = devices;
164 #endif /* HAVE_GUDEV */
167 gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
168 GList ** klass_devices)
171 const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
175 gchar *device = devices->data;
176 devices = g_list_remove (devices, device);
181 * detect /dev entries
183 for (n = 0; n < 64; n++) {
184 for (base = 0; dev_base[base] != NULL; base++) {
186 gchar *device = g_strdup_printf ("%s%d",
191 * does the /dev/ entry exist at all?
193 if (stat (device, &s) == 0) {
195 * yes: is a device attached?
197 if (S_ISCHR (s.st_mode)) {
199 if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
203 devices = g_list_append (devices, device);
214 *klass_devices = devices;
220 gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
221 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
223 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
228 if (!gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices))
229 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
230 #else /* !HAVE_GUDEV */
231 gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
232 #endif /* HAVE_GUDEV */
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
241 gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
242 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
244 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
245 gboolean ret = FALSE;
251 !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
252 #else /* !HAVE_GUDEV */
253 ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
254 #endif /* HAVE_GUDEV */
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
264 gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
267 GValue value = { 0 };
273 array = g_value_array_new (g_list_length (*klass_devices));
274 item = *klass_devices;
275 g_value_init (&value, G_TYPE_STRING);
277 gchar *device = item->data;
279 g_value_set_string (&value, device);
280 g_value_array_append (array, &value);
284 g_value_unset (&value);
290 gst_v4l2_probe_get_values (GstPropertyProbe * probe,
291 guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
293 GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
294 GValueArray *array = NULL;
298 array = gst_v4l2_class_list_devices (klass, klass_devices);
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
309 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
311 gst_v4l2_device_get_type (void)
313 static GType v4l2_device_type = 0;
315 if (v4l2_device_type == 0) {
316 static const GFlagsValue values[] = {
317 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
318 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
319 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
321 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
322 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
324 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
325 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
331 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
334 return v4l2_device_type;
337 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
339 gst_v4l2_tv_norm_get_type (void)
341 static GType v4l2_tv_norm = 0;
344 static const GEnumValue tv_norms[] = {
347 {V4L2_STD_NTSC, "NTSC", "NTSC"},
348 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
349 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
350 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
351 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
353 {V4L2_STD_PAL, "PAL", "PAL"},
354 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
355 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
356 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
357 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
358 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
359 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
360 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
361 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
362 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
363 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
364 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
365 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
366 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
367 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
369 {V4L2_STD_SECAM, "SECAM", "SECAM"},
370 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
371 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
372 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
373 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
374 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
375 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
376 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
377 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
378 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
383 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
390 gst_v4l2_io_mode_get_type (void)
392 static GType v4l2_io_mode = 0;
395 static const GEnumValue io_modes[] = {
396 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
397 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
398 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
399 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
400 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
404 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
410 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
411 const char *default_device)
413 g_object_class_install_property (gobject_class, PROP_DEVICE,
414 g_param_spec_string ("device", "Device", "Device location",
415 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
416 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
417 g_param_spec_string ("device-name", "Device name",
418 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
419 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
420 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
421 g_param_spec_int ("device-fd", "File descriptor",
422 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
423 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
424 g_object_class_install_property (gobject_class, PROP_FLAGS,
425 g_param_spec_flags ("flags", "Flags", "Device type flags",
426 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
427 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
430 * GstV4l2Src:brightness:
432 * Picture brightness, or more precisely, the black level
434 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
435 g_param_spec_int ("brightness", "Brightness",
436 "Picture brightness, or more precisely, the black level", G_MININT,
438 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
440 * GstV4l2Src:contrast:
442 * Picture contrast or luma gain
444 g_object_class_install_property (gobject_class, PROP_CONTRAST,
445 g_param_spec_int ("contrast", "Contrast",
446 "Picture contrast or luma gain", G_MININT,
448 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
450 * GstV4l2Src:saturation:
452 * Picture color saturation or chroma gain
454 g_object_class_install_property (gobject_class, PROP_SATURATION,
455 g_param_spec_int ("saturation", "Saturation",
456 "Picture color saturation or chroma gain", G_MININT,
458 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
462 * Hue or color balance
464 g_object_class_install_property (gobject_class, PROP_HUE,
465 g_param_spec_int ("hue", "Hue",
466 "Hue or color balance", G_MININT,
468 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
475 g_object_class_install_property (gobject_class, PROP_TV_NORM,
476 g_param_spec_enum ("norm", "TV norm",
478 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
479 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
482 * GstV4l2Src:io-mode:
486 g_object_class_install_property (gobject_class, PROP_IO_MODE,
487 g_param_spec_enum ("io-mode", "IO mode",
489 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
490 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
493 * GstV4l2Src:extra-controls:
495 * Additional v4l2 controls for the device. The controls are identified
496 * by the control name (lowercase with '_' for any non-alphanumeric
501 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
502 g_param_spec_boxed ("extra-controls", "Extra Controls",
503 "Extra v4l2 controls (CIDs) for the device",
504 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
507 * GstV4l2Src:pixel-aspect-ratio:
509 * The pixel aspect ratio of the device. This overwrites the pixel aspect
510 * ratio queried from the device.
514 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
515 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
516 "Overwrite the pixel aspect ratio of the device", "1/1",
517 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
520 * GstV4l2Src:force-aspect-ratio:
522 * When enabled, the pixel aspect ratio queried from the device or set
523 * with the pixel-aspect-ratio property will be enforced.
527 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
528 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
529 "When enabled, the pixel aspect ratio will be enforced", TRUE,
530 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
535 gst_v4l2_object_new (GstElement * element,
536 enum v4l2_buf_type type,
537 const char *default_device,
538 GstV4l2GetInOutFunction get_in_out_func,
539 GstV4l2SetInOutFunction set_in_out_func,
540 GstV4l2UpdateFpsFunction update_fps_func)
542 GstV4l2Object *v4l2object;
545 * some default values
547 v4l2object = g_new0 (GstV4l2Object, 1);
549 v4l2object->type = type;
550 v4l2object->formats = NULL;
552 v4l2object->element = element;
553 v4l2object->get_in_out_func = get_in_out_func;
554 v4l2object->set_in_out_func = set_in_out_func;
555 v4l2object->update_fps_func = update_fps_func;
557 v4l2object->video_fd = -1;
558 v4l2object->poll = gst_poll_new (TRUE);
559 v4l2object->active = FALSE;
560 v4l2object->videodev = g_strdup (default_device);
562 v4l2object->norms = NULL;
563 v4l2object->channels = NULL;
564 v4l2object->colors = NULL;
566 v4l2object->xwindow_id = 0;
568 v4l2object->keep_aspect = TRUE;
570 v4l2object->n_v4l2_planes = 0;
573 * this boolean only applies in v4l2-MPLANE mode.
574 * TRUE: means it prefers to use several v4l2 (non contiguous)
575 * planes. For example if the device supports NV12 and NV12M
576 * both in MPLANE mode, then it will prefer NV12M
577 * FALSE: means it prefers to use one v4l2 plane (which contains
578 * all gst planes as if it was working in non-v4l2-MPLANE mode.
579 * For example if the device supports NV12 and NV12M
580 * both in MPLANE mode, then it will prefer NV12
582 * this boolean is also used to manage the case where the
583 * device only supports the mode MPLANE and at the same time it
584 * does not support both NV12 and NV12M. So in this case we first
585 * try to use the prefered config, and at least try the other case
586 * if it fails. For example in MPLANE mode if it has NV12 and not
587 * NV21M then even if you set prefered_non_contiguous to TRUE it will
590 v4l2object->prefered_non_contiguous = TRUE;
592 v4l2object->no_initial_format = FALSE;
597 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
601 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
603 g_return_if_fail (v4l2object != NULL);
605 if (v4l2object->videodev)
606 g_free (v4l2object->videodev);
608 if (v4l2object->poll)
609 gst_poll_free (v4l2object->poll);
611 if (v4l2object->channel)
612 g_free (v4l2object->channel);
614 if (v4l2object->formats) {
615 gst_v4l2_object_clear_format_list (v4l2object);
618 if (v4l2object->probed_caps) {
619 gst_caps_unref (v4l2object->probed_caps);
627 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
629 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
630 g_slist_free (v4l2object->formats);
631 v4l2object->formats = NULL;
637 gst_v4l2_object_prop_to_cid (guint prop_id)
642 case PROP_BRIGHTNESS:
643 cid = V4L2_CID_BRIGHTNESS;
646 cid = V4L2_CID_CONTRAST;
648 case PROP_SATURATION:
649 cid = V4L2_CID_SATURATION;
655 GST_WARNING ("unmapped property id: %d", prop_id);
662 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
663 guint prop_id, const GValue * value, GParamSpec * pspec)
667 g_free (v4l2object->videodev);
668 v4l2object->videodev = g_value_dup_string (value);
670 case PROP_BRIGHTNESS:
672 case PROP_SATURATION:
675 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
678 if (GST_V4L2_IS_OPEN (v4l2object)) {
679 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
686 v4l2object->tv_norm = g_value_get_enum (value);
690 if (GST_V4L2_IS_OPEN (v4l2object)) {
691 GstTuner *tuner = GST_TUNER (v4l2object->element);
692 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
693 (gchar *) g_value_get_string (value));
696 /* like gst_tuner_set_channel (tuner, channel)
697 without g_object_notify */
698 gst_v4l2_tuner_set_channel (v4l2object, channel);
701 g_free (v4l2object->channel);
702 v4l2object->channel = g_value_dup_string (value);
706 if (GST_V4L2_IS_OPEN (v4l2object)) {
707 GstTuner *tuner = GST_TUNER (v4l2object->element);
708 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
711 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
713 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
714 without g_object_notify */
715 gst_v4l2_tuner_set_frequency (v4l2object, channel,
716 g_value_get_ulong (value));
719 v4l2object->frequency = g_value_get_ulong (value);
724 v4l2object->req_mode = g_value_get_enum (value);
726 case PROP_EXTRA_CONTROLS:{
727 const GstStructure *s = gst_value_get_structure (value);
729 if (v4l2object->extra_controls)
730 gst_structure_free (v4l2object->extra_controls);
732 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
733 if (GST_V4L2_IS_OPEN (v4l2object))
734 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
737 case PROP_PIXEL_ASPECT_RATIO:
738 g_free (v4l2object->par);
739 v4l2object->par = g_new0 (GValue, 1);
740 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
741 if (!g_value_transform (value, v4l2object->par)) {
742 g_warning ("Could not transform string to aspect ratio");
743 gst_value_set_fraction (v4l2object->par, 1, 1);
745 GST_DEBUG_OBJECT (v4l2object->element, "set PAR to %d/%d",
746 gst_value_get_fraction_numerator (v4l2object->par),
747 gst_value_get_fraction_denominator (v4l2object->par));
749 case PROP_FORCE_ASPECT_RATIO:
750 v4l2object->keep_aspect = g_value_get_boolean (value);
761 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
762 guint prop_id, GValue * value, GParamSpec * pspec)
766 g_value_set_string (value, v4l2object->videodev);
768 case PROP_DEVICE_NAME:
770 const guchar *new = NULL;
772 if (GST_V4L2_IS_OPEN (v4l2object)) {
773 new = v4l2object->vcap.card;
774 } else if (gst_v4l2_open (v4l2object)) {
775 new = v4l2object->vcap.card;
776 gst_v4l2_close (v4l2object);
778 g_value_set_string (value, (gchar *) new);
783 if (GST_V4L2_IS_OPEN (v4l2object))
784 g_value_set_int (value, v4l2object->video_fd);
786 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
793 if (GST_V4L2_IS_OPEN (v4l2object)) {
794 flags |= v4l2object->vcap.capabilities &
795 (V4L2_CAP_VIDEO_CAPTURE |
796 V4L2_CAP_VIDEO_OUTPUT |
797 V4L2_CAP_VIDEO_OVERLAY |
798 V4L2_CAP_VBI_CAPTURE |
799 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
801 if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
802 flags |= V4L2_CAP_VIDEO_CAPTURE;
804 if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
805 flags |= V4L2_CAP_VIDEO_OUTPUT;
807 g_value_set_flags (value, flags);
810 case PROP_BRIGHTNESS:
812 case PROP_SATURATION:
815 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
818 if (GST_V4L2_IS_OPEN (v4l2object)) {
820 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
821 g_value_set_int (value, v);
829 g_value_set_enum (value, v4l2object->tv_norm);
832 g_value_set_enum (value, v4l2object->req_mode);
834 case PROP_EXTRA_CONTROLS:
835 gst_value_set_structure (value, v4l2object->extra_controls);
837 case PROP_PIXEL_ASPECT_RATIO:
839 g_value_transform (v4l2object->par, value);
841 case PROP_FORCE_ASPECT_RATIO:
842 g_value_set_boolean (value, v4l2object->keep_aspect);
852 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
854 GstTunerNorm *norm = NULL;
855 GstTunerChannel *channel = NULL;
858 if (!GST_IS_TUNER (v4l2object->element))
861 tuner = GST_TUNER (v4l2object->element);
863 if (v4l2object->tv_norm)
864 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
865 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
866 "norm=%p", (guint64) v4l2object->tv_norm, norm);
868 gst_tuner_set_norm (tuner, norm);
871 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
873 v4l2object->tv_norm =
874 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
875 gst_tuner_norm_changed (tuner, norm);
879 if (v4l2object->channel)
880 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
882 gst_tuner_set_channel (tuner, channel);
885 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
886 (v4l2object->element)));
888 g_free (v4l2object->channel);
889 v4l2object->channel = g_strdup (channel->label);
890 gst_tuner_channel_changed (tuner, channel);
895 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
896 if (v4l2object->frequency != 0) {
897 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
899 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
900 if (v4l2object->frequency == 0) {
902 gst_tuner_set_frequency (tuner, channel, 1000);
910 gst_v4l2_object_open (GstV4l2Object * v4l2object)
912 if (gst_v4l2_open (v4l2object))
913 gst_v4l2_set_defaults (v4l2object);
918 gst_v4l2_video_overlay_start (v4l2object);
925 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
929 ret = gst_v4l2_dup (v4l2object, other);
932 gst_v4l2_video_overlay_start (v4l2object);
939 gst_v4l2_object_close (GstV4l2Object * v4l2object)
942 gst_v4l2_video_overlay_stop (v4l2object);
945 if (!gst_v4l2_close (v4l2object))
948 gst_caps_replace (&v4l2object->probed_caps, NULL);
950 if (v4l2object->formats) {
951 gst_v4l2_object_clear_format_list (v4l2object);
959 * common format / caps utilities:
963 GST_V4L2_RAW = 1 << 0,
964 GST_V4L2_CODEC = 1 << 1,
965 GST_V4L2_TRANSPORT = 1 << 2,
966 GST_V4L2_ALL = 0xffff
967 } GstV4L2FormatFlags;
973 GstV4L2FormatFlags flags;
976 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
977 /* from Linux 2.6.15 videodev2.h */
978 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
979 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
980 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
981 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
982 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
983 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
984 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
985 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
986 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
987 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
988 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
989 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
990 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
991 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
992 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
993 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
994 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
996 /* two planes -- one Y, one Cr + Cb interleaved */
997 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
998 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
999 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
1000 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
1002 /* The following formats are not defined in the V4L2 specification */
1003 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
1004 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
1005 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
1006 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
1008 /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
1009 #ifdef V4L2_PIX_FMT_SBGGR8
1010 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
1013 /* compressed formats */
1014 {V4L2_PIX_FMT_MJPEG, TRUE, GST_V4L2_CODEC},
1015 {V4L2_PIX_FMT_JPEG, TRUE, GST_V4L2_CODEC},
1016 #ifdef V4L2_PIX_FMT_PJPG
1017 {V4L2_PIX_FMT_PJPG, TRUE, GST_V4L2_CODEC},
1019 {V4L2_PIX_FMT_DV, TRUE, GST_V4L2_TRANSPORT},
1020 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
1021 #ifdef V4L2_PIX_FMT_MPEG4
1022 {V4L2_PIX_FMT_MPEG4, TRUE, GST_V4L2_CODEC},
1025 #ifdef V4L2_PIX_FMT_H263
1026 {V4L2_PIX_FMT_H263, TRUE, GST_V4L2_CODEC},
1028 #ifdef V4L2_PIX_FMT_H264
1029 {V4L2_PIX_FMT_H264, TRUE, GST_V4L2_CODEC},
1032 /* Vendor-specific formats */
1033 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
1035 #ifdef V4L2_PIX_FMT_SN9C10X
1036 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
1038 #ifdef V4L2_PIX_FMT_PWC1
1039 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
1041 #ifdef V4L2_PIX_FMT_PWC2
1042 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
1044 #ifdef V4L2_PIX_FMT_YVYU
1045 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
1049 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
1052 static struct v4l2_fmtdesc *
1053 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
1056 struct v4l2_fmtdesc *fmt;
1062 walk = gst_v4l2_object_get_format_list (v4l2object);
1064 fmt = (struct v4l2_fmtdesc *) walk->data;
1065 if (fmt->pixelformat == fourcc)
1067 /* special case for jpeg */
1068 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
1069 fmt->pixelformat == V4L2_PIX_FMT_JPEG
1070 #ifdef V4L2_PIX_FMT_PJPG
1071 || fmt->pixelformat == V4L2_PIX_FMT_PJPG
1074 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG
1075 #ifdef V4L2_PIX_FMT_PJPG
1076 || fourcc == V4L2_PIX_FMT_PJPG
1082 walk = g_slist_next (walk);
1090 /* complete made up ranking, the values themselves are meaningless */
1091 /* These ranks MUST be X such that X<<15 fits on a signed int - see
1092 the comment at the end of gst_v4l2_object_format_get_rank. */
1093 #define YUV_BASE_RANK 1000
1094 #define JPEG_BASE_RANK 500
1095 #define DV_BASE_RANK 200
1096 #define RGB_BASE_RANK 100
1097 #define YUV_ODD_BASE_RANK 50
1098 #define RGB_ODD_BASE_RANK 25
1099 #define BAYER_BASE_RANK 15
1100 #define S910_BASE_RANK 10
1101 #define GREY_BASE_RANK 5
1102 #define PWC_BASE_RANK 1
1104 /* This flag is already used by libv4l2 although
1105 * it was added to the Linux kernel in 2.6.32
1107 #ifndef V4L2_FMT_FLAG_EMULATED
1108 #define V4L2_FMT_FLAG_EMULATED 0x0002
1112 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
1114 guint32 fourcc = fmt->pixelformat;
1115 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
1119 case V4L2_PIX_FMT_MJPEG:
1120 #ifdef V4L2_PIX_FMT_PJPG
1121 case V4L2_PIX_FMT_PJPG:
1122 rank = JPEG_BASE_RANK;
1125 case V4L2_PIX_FMT_JPEG:
1126 rank = JPEG_BASE_RANK + 1;
1128 case V4L2_PIX_FMT_MPEG: /* MPEG */
1129 rank = JPEG_BASE_RANK + 2;
1132 case V4L2_PIX_FMT_RGB332:
1133 case V4L2_PIX_FMT_RGB555:
1134 case V4L2_PIX_FMT_RGB555X:
1135 case V4L2_PIX_FMT_RGB565:
1136 case V4L2_PIX_FMT_RGB565X:
1137 rank = RGB_ODD_BASE_RANK;
1140 case V4L2_PIX_FMT_RGB24:
1141 case V4L2_PIX_FMT_BGR24:
1142 rank = RGB_BASE_RANK - 1;
1145 case V4L2_PIX_FMT_RGB32:
1146 case V4L2_PIX_FMT_BGR32:
1147 rank = RGB_BASE_RANK;
1150 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1151 rank = GREY_BASE_RANK;
1154 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1155 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
1156 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1157 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1158 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1159 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1160 rank = YUV_ODD_BASE_RANK;
1163 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1164 rank = YUV_BASE_RANK + 3;
1166 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1167 rank = YUV_BASE_RANK + 2;
1169 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1170 rank = YUV_BASE_RANK + 7;
1172 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1173 rank = YUV_BASE_RANK + 10;
1175 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1176 rank = YUV_BASE_RANK + 6;
1178 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1179 rank = YUV_BASE_RANK + 9;
1181 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1182 rank = YUV_BASE_RANK + 5;
1184 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1185 rank = YUV_BASE_RANK + 4;
1187 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1188 rank = YUV_BASE_RANK + 8;
1191 case V4L2_PIX_FMT_DV:
1192 rank = DV_BASE_RANK;
1195 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1199 #ifdef V4L2_PIX_FMT_SBGGR8
1200 case V4L2_PIX_FMT_SBGGR8:
1201 rank = BAYER_BASE_RANK;
1205 #ifdef V4L2_PIX_FMT_SN9C10X
1206 case V4L2_PIX_FMT_SN9C10X:
1207 rank = S910_BASE_RANK;
1211 #ifdef V4L2_PIX_FMT_PWC1
1212 case V4L2_PIX_FMT_PWC1:
1213 rank = PWC_BASE_RANK;
1216 #ifdef V4L2_PIX_FMT_PWC2
1217 case V4L2_PIX_FMT_PWC2:
1218 rank = PWC_BASE_RANK;
1227 /* All ranks are below 1<<15 so a shift by 15
1228 * will a) make all non-emulated formats larger
1229 * than emulated and b) will not overflow
1240 format_cmp_func (gconstpointer a, gconstpointer b)
1242 const struct v4l2_fmtdesc *fa = a;
1243 const struct v4l2_fmtdesc *fb = b;
1245 if (fa->pixelformat == fb->pixelformat)
1248 return gst_v4l2_object_format_get_rank (fb) -
1249 gst_v4l2_object_format_get_rank (fa);
1252 /******************************************************
1253 * gst_v4l2_object_fill_format_list():
1254 * create list of supported capture formats
1255 * return value: TRUE on success, FALSE on error
1256 ******************************************************/
1258 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1259 enum v4l2_buf_type type)
1262 struct v4l2_fmtdesc *format;
1264 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1266 /* format enumeration */
1268 format = g_new0 (struct v4l2_fmtdesc, 1);
1271 format->type = type;
1273 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1274 if (errno == EINVAL) {
1276 break; /* end of enumeration */
1282 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1283 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1284 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1285 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1286 format->description);
1287 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1288 GST_FOURCC_ARGS (format->pixelformat));
1290 /* sort formats according to our preference; we do this, because caps
1291 * are probed in the order the formats are in the list, and the order of
1292 * formats in the final probed caps matters for things like fixation */
1293 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1294 (GCompareFunc) format_cmp_func);
1297 #ifndef GST_DISABLE_GST_DEBUG
1301 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1302 for (l = v4l2object->formats; l != NULL; l = l->next) {
1305 GST_INFO_OBJECT (v4l2object->element,
1306 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1307 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1317 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1318 (_("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)));
1325 * Get the list of supported capture formats, a list of
1326 * <code>struct v4l2_fmtdesc</code>.
1329 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1331 if (!v4l2object->formats) {
1333 /* check usual way */
1334 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1336 /* if our driver supports multi-planar
1337 * and if formats are still empty then we can workaround driver bug
1338 * by also looking up formats as if our device was not supporting
1340 if (!v4l2object->formats) {
1341 switch (v4l2object->type) {
1342 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1343 gst_v4l2_object_fill_format_list (v4l2object,
1344 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1347 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1348 gst_v4l2_object_fill_format_list (v4l2object,
1349 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1357 return v4l2object->formats;
1360 static GstVideoFormat
1361 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1363 GstVideoFormat format;
1366 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1367 format = GST_VIDEO_FORMAT_GRAY8;
1369 case V4L2_PIX_FMT_RGB555:
1370 format = GST_VIDEO_FORMAT_RGB15;
1372 case V4L2_PIX_FMT_RGB565:
1373 format = GST_VIDEO_FORMAT_RGB16;
1375 case V4L2_PIX_FMT_RGB24:
1376 format = GST_VIDEO_FORMAT_RGB;
1378 case V4L2_PIX_FMT_BGR24:
1379 format = GST_VIDEO_FORMAT_BGR;
1381 case V4L2_PIX_FMT_RGB32:
1382 format = GST_VIDEO_FORMAT_RGBx;
1384 case V4L2_PIX_FMT_BGR32:
1385 format = GST_VIDEO_FORMAT_BGRx;
1387 case V4L2_PIX_FMT_NV12:
1388 case V4L2_PIX_FMT_NV12M:
1389 format = GST_VIDEO_FORMAT_NV12;
1391 case V4L2_PIX_FMT_NV21:
1392 case V4L2_PIX_FMT_NV21M:
1393 format = GST_VIDEO_FORMAT_NV21;
1395 case V4L2_PIX_FMT_YVU410:
1396 format = GST_VIDEO_FORMAT_YVU9;
1398 case V4L2_PIX_FMT_YUV410:
1399 format = GST_VIDEO_FORMAT_YUV9;
1401 case V4L2_PIX_FMT_YUV420:
1402 format = GST_VIDEO_FORMAT_I420;
1404 case V4L2_PIX_FMT_YUYV:
1405 format = GST_VIDEO_FORMAT_YUY2;
1407 case V4L2_PIX_FMT_YVU420:
1408 format = GST_VIDEO_FORMAT_YV12;
1410 case V4L2_PIX_FMT_UYVY:
1411 format = GST_VIDEO_FORMAT_UYVY;
1414 case V4L2_PIX_FMT_Y41P:
1415 format = GST_VIDEO_FORMAT_Y41P;
1418 case V4L2_PIX_FMT_YUV411P:
1419 format = GST_VIDEO_FORMAT_Y41B;
1421 case V4L2_PIX_FMT_YUV422P:
1422 format = GST_VIDEO_FORMAT_Y42B;
1424 #ifdef V4L2_PIX_FMT_YVYU
1425 case V4L2_PIX_FMT_YVYU:
1426 format = GST_VIDEO_FORMAT_YVYU;
1430 format = GST_VIDEO_FORMAT_UNKNOWN;
1431 g_assert_not_reached ();
1439 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1441 GstStructure *structure = NULL;
1444 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1445 #ifdef V4L2_PIX_FMT_PJPG
1446 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1448 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1449 structure = gst_structure_new_empty ("image/jpeg");
1451 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1452 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1453 /* FIXME: get correct fourccs here */
1455 #ifdef V4L2_PIX_FMT_MPEG4
1456 case V4L2_PIX_FMT_MPEG4:
1457 structure = gst_structure_new ("video/mpeg",
1458 "mpegversion", G_TYPE_INT, 4, "systemstream",
1459 G_TYPE_BOOLEAN, FALSE, NULL);
1462 #ifdef V4L2_PIX_FMT_H263
1463 case V4L2_PIX_FMT_H263:
1464 structure = gst_structure_new ("video/x-h263",
1465 "variant", G_TYPE_STRING, "itu", NULL);
1468 #ifdef V4L2_PIX_FMT_H264
1469 case V4L2_PIX_FMT_H264: /* H.264 */
1470 structure = gst_structure_new ("video/x-h264",
1471 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1472 G_TYPE_STRING, "au", NULL);
1475 case V4L2_PIX_FMT_RGB332:
1476 case V4L2_PIX_FMT_RGB555X:
1477 case V4L2_PIX_FMT_RGB565X:
1478 /* FIXME: get correct fourccs here */
1480 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1481 case V4L2_PIX_FMT_RGB555:
1482 case V4L2_PIX_FMT_RGB565:
1483 case V4L2_PIX_FMT_RGB24:
1484 case V4L2_PIX_FMT_BGR24:
1485 case V4L2_PIX_FMT_RGB32:
1486 case V4L2_PIX_FMT_BGR32:
1487 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1488 case V4L2_PIX_FMT_NV12M:
1489 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1490 case V4L2_PIX_FMT_NV21M:
1491 case V4L2_PIX_FMT_YVU410:
1492 case V4L2_PIX_FMT_YUV410:
1493 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1494 case V4L2_PIX_FMT_YUYV:
1495 case V4L2_PIX_FMT_YVU420:
1496 case V4L2_PIX_FMT_UYVY:
1498 case V4L2_PIX_FMT_Y41P:
1500 case V4L2_PIX_FMT_YUV422P:
1501 #ifdef V4L2_PIX_FMT_YVYU
1502 case V4L2_PIX_FMT_YVYU:
1504 case V4L2_PIX_FMT_YUV411P:{
1505 GstVideoFormat format;
1506 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1507 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1508 structure = gst_structure_new ("video/x-raw",
1509 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1512 case V4L2_PIX_FMT_DV:
1514 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1517 case V4L2_PIX_FMT_MPEG: /* MPEG */
1518 structure = gst_structure_new_empty ("video/mpegts");
1520 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1522 #ifdef V4L2_PIX_FMT_SBGGR8
1523 case V4L2_PIX_FMT_SBGGR8:
1524 structure = gst_structure_new_empty ("video/x-bayer");
1527 #ifdef V4L2_PIX_FMT_SN9C10X
1528 case V4L2_PIX_FMT_SN9C10X:
1529 structure = gst_structure_new_empty ("video/x-sonix");
1532 #ifdef V4L2_PIX_FMT_PWC1
1533 case V4L2_PIX_FMT_PWC1:
1534 structure = gst_structure_new_empty ("video/x-pwc1");
1537 #ifdef V4L2_PIX_FMT_PWC2
1538 case V4L2_PIX_FMT_PWC2:
1539 structure = gst_structure_new_empty ("video/x-pwc2");
1543 GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
1544 fourcc, GST_FOURCC_ARGS (fourcc));
1553 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1555 GstStructure *structure;
1559 caps = gst_caps_new_empty ();
1560 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1562 if ((gst_v4l2_formats[i].flags & flags) == 0)
1566 gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
1568 if (gst_v4l2_formats[i].dimensions) {
1569 gst_structure_set (structure,
1570 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1571 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1572 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
1574 gst_caps_append_structure (caps, structure);
1578 return gst_caps_simplify (caps);
1582 gst_v4l2_object_get_all_caps (void)
1584 static GstCaps *caps = NULL;
1587 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1589 return gst_caps_ref (caps);
1593 gst_v4l2_object_get_raw_caps (void)
1595 static GstCaps *caps = NULL;
1598 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1600 return gst_caps_ref (caps);
1604 gst_v4l2_object_get_codec_caps (void)
1606 static GstCaps *caps = NULL;
1609 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1611 return gst_caps_ref (caps);
1614 /* gst_v4l2_object_choose_fourcc:
1615 * @obj a #GstV4l2Object
1616 * @fourcc_splane The format type in single plane representation
1617 * @fourcc_mplane The format type in multi-plane representation
1618 * @fourcc Set to the first format to try
1619 * @fourcc_alt The alternative format to use, or zero if mplane is not
1620 * supported. Note that if alternate is used, the prefered_non_contiguous
1621 * setting need to be inversed.
1623 * Certain format can be stored into multi-planar buffer type with two
1624 * representation. As an example, NV12, which has two planes, can be stored
1625 * into 1 plane of multi-planar buffer sturcture, or two. This function will
1626 * choose the right format to use base on the object settings.
1629 gst_v4l2_object_choose_fourcc (GstV4l2Object * obj, guint32 fourcc_splane,
1630 guint32 fourcc_mplane, guint32 * fourcc, guint32 * fourcc_alt)
1632 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
1633 if (obj->prefered_non_contiguous) {
1634 *fourcc = fourcc_mplane;
1635 *fourcc_alt = fourcc_splane;
1637 *fourcc = fourcc_splane;
1638 *fourcc_alt = fourcc_mplane;
1641 *fourcc = fourcc_splane;
1646 /* collect data for the given caps
1647 * @caps: given input caps
1648 * @format: location for the v4l format
1649 * @w/@h: location for width and height
1650 * @fps_n/@fps_d: location for framerate
1651 * @size: location for expected size of the frame or 0 if unknown
1654 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1655 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1657 GstStructure *structure;
1658 guint32 fourcc, fourcc_alt = 0;
1659 const gchar *mimetype;
1660 struct v4l2_fmtdesc *fmt;
1662 /* default unknown values */
1665 structure = gst_caps_get_structure (caps, 0);
1667 mimetype = gst_structure_get_name (structure);
1669 if (!gst_video_info_from_caps (info, caps))
1670 goto invalid_format;
1672 if (g_str_equal (mimetype, "video/x-raw")) {
1673 switch (GST_VIDEO_INFO_FORMAT (info)) {
1674 case GST_VIDEO_FORMAT_I420:
1675 fourcc = V4L2_PIX_FMT_YUV420;
1677 case GST_VIDEO_FORMAT_YUY2:
1678 fourcc = V4L2_PIX_FMT_YUYV;
1681 case GST_VIDEO_FORMAT_Y41P:
1682 fourcc = V4L2_PIX_FMT_Y41P;
1685 case GST_VIDEO_FORMAT_UYVY:
1686 fourcc = V4L2_PIX_FMT_UYVY;
1688 case GST_VIDEO_FORMAT_YV12:
1689 fourcc = V4L2_PIX_FMT_YVU420;
1691 case GST_VIDEO_FORMAT_Y41B:
1692 fourcc = V4L2_PIX_FMT_YUV411P;
1694 case GST_VIDEO_FORMAT_Y42B:
1695 fourcc = V4L2_PIX_FMT_YUV422P;
1697 case GST_VIDEO_FORMAT_NV12:
1698 gst_v4l2_object_choose_fourcc (v4l2object, V4L2_PIX_FMT_NV12,
1699 V4L2_PIX_FMT_NV12M, &fourcc, &fourcc_alt);
1701 case GST_VIDEO_FORMAT_NV21:
1702 gst_v4l2_object_choose_fourcc (v4l2object, V4L2_PIX_FMT_NV21,
1703 V4L2_PIX_FMT_NV21M, &fourcc, &fourcc_alt);
1705 #ifdef V4L2_PIX_FMT_YVYU
1706 case GST_VIDEO_FORMAT_YVYU:
1707 fourcc = V4L2_PIX_FMT_YVYU;
1710 case GST_VIDEO_FORMAT_RGB15:
1711 fourcc = V4L2_PIX_FMT_RGB555;
1713 case GST_VIDEO_FORMAT_RGB16:
1714 fourcc = V4L2_PIX_FMT_RGB565;
1716 case GST_VIDEO_FORMAT_RGB:
1717 fourcc = V4L2_PIX_FMT_RGB24;
1719 case GST_VIDEO_FORMAT_BGR:
1720 fourcc = V4L2_PIX_FMT_BGR24;
1722 case GST_VIDEO_FORMAT_RGBx:
1723 case GST_VIDEO_FORMAT_RGBA:
1724 fourcc = V4L2_PIX_FMT_RGB32;
1726 case GST_VIDEO_FORMAT_BGRx:
1727 case GST_VIDEO_FORMAT_BGRA:
1728 fourcc = V4L2_PIX_FMT_BGR32;
1730 case GST_VIDEO_FORMAT_GRAY8:
1731 fourcc = V4L2_PIX_FMT_GREY;
1736 if (g_str_equal (mimetype, "video/mpegts")) {
1737 fourcc = V4L2_PIX_FMT_MPEG;
1738 } else if (g_str_equal (mimetype, "video/x-dv")) {
1739 fourcc = V4L2_PIX_FMT_DV;
1740 } else if (g_str_equal (mimetype, "image/jpeg")) {
1741 fourcc = V4L2_PIX_FMT_JPEG;
1742 #ifdef V4L2_PIX_FMT_MPEG4
1743 } else if (g_str_equal (mimetype, "video/mpeg")) {
1744 fourcc = V4L2_PIX_FMT_MPEG4;
1746 #ifdef V4L2_PIX_FMT_H263
1747 } else if (g_str_equal (mimetype, "video/x-h263")) {
1748 fourcc = V4L2_PIX_FMT_H263;
1750 #ifdef V4L2_PIX_FMT_H264
1751 } else if (g_str_equal (mimetype, "video/x-h264")) {
1752 fourcc = V4L2_PIX_FMT_H264;
1754 #ifdef V4L2_PIX_FMT_SBGGR8
1755 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1756 fourcc = V4L2_PIX_FMT_SBGGR8;
1758 #ifdef V4L2_PIX_FMT_SN9C10X
1759 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1760 fourcc = V4L2_PIX_FMT_SN9C10X;
1762 #ifdef V4L2_PIX_FMT_PWC1
1763 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1764 fourcc = V4L2_PIX_FMT_PWC1;
1766 #ifdef V4L2_PIX_FMT_PWC2
1767 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1768 fourcc = V4L2_PIX_FMT_PWC2;
1774 goto unhandled_format;
1776 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1778 if (fmt == NULL && fourcc_alt != 0) {
1779 GST_DEBUG_OBJECT (v4l2object, "No support for %" GST_FOURCC_FORMAT
1780 " trying %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc),
1781 GST_FOURCC_ARGS (fourcc_alt));
1782 v4l2object->prefered_non_contiguous = !v4l2object->prefered_non_contiguous;
1783 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_alt);
1787 goto unsupported_format;
1796 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1801 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1806 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1812 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1813 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced);
1816 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1818 struct v4l2_cropcap cropcap;
1819 int num = 1, den = 1;
1821 if (!v4l2object->keep_aspect)
1824 if (v4l2object->par) {
1825 num = gst_value_get_fraction_numerator (v4l2object->par);
1826 den = gst_value_get_fraction_denominator (v4l2object->par);
1830 memset (&cropcap, 0, sizeof (cropcap));
1832 cropcap.type = v4l2object->type;
1833 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
1834 goto cropcap_failed;
1836 num = cropcap.pixelaspect.numerator;
1837 den = cropcap.pixelaspect.denominator;
1840 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, num, den,
1845 if (errno != ENOTTY)
1846 GST_WARNING_OBJECT (v4l2object->element,
1847 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
1848 g_strerror (errno));
1853 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1854 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1855 static GstStructure *
1856 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1857 guint32 pixelformat,
1858 guint32 width, guint32 height, const GstStructure * template)
1860 gint fd = v4l2object->video_fd;
1861 struct v4l2_frmivalenum ival;
1864 GValue rates = { 0, };
1865 gboolean interlaced;
1866 gint int_width = width;
1867 gint int_height = height;
1869 if (v4l2object->never_interlaced) {
1872 /* Interlaced detection using VIDIOC_TRY/S_FMT */
1873 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat,
1874 &int_width, &int_height, &interlaced))
1878 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1880 ival.pixel_format = pixelformat;
1882 ival.height = height;
1884 GST_LOG_OBJECT (v4l2object->element,
1885 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1886 GST_FOURCC_ARGS (pixelformat));
1888 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1889 * fraction to get framerate */
1890 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1891 goto enum_frameintervals_failed;
1893 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1894 GValue rate = { 0, };
1896 g_value_init (&rates, GST_TYPE_LIST);
1897 g_value_init (&rate, GST_TYPE_FRACTION);
1900 num = ival.discrete.numerator;
1901 denom = ival.discrete.denominator;
1903 if (num > G_MAXINT || denom > G_MAXINT) {
1904 /* let us hope we don't get here... */
1909 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1912 /* swap to get the framerate */
1913 gst_value_set_fraction (&rate, denom, num);
1914 gst_value_list_append_value (&rates, &rate);
1917 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1918 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1919 GValue min = { 0, };
1920 GValue step = { 0, };
1921 GValue max = { 0, };
1922 gboolean added = FALSE;
1923 guint32 minnum, mindenom;
1924 guint32 maxnum, maxdenom;
1926 g_value_init (&rates, GST_TYPE_LIST);
1928 g_value_init (&min, GST_TYPE_FRACTION);
1929 g_value_init (&step, GST_TYPE_FRACTION);
1930 g_value_init (&max, GST_TYPE_FRACTION);
1933 minnum = ival.stepwise.min.numerator;
1934 mindenom = ival.stepwise.min.denominator;
1935 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1939 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1941 gst_value_set_fraction (&min, minnum, mindenom);
1944 maxnum = ival.stepwise.max.numerator;
1945 maxdenom = ival.stepwise.max.denominator;
1946 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1951 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1953 gst_value_set_fraction (&max, maxnum, maxdenom);
1956 num = ival.stepwise.step.numerator;
1957 denom = ival.stepwise.step.denominator;
1958 if (num > G_MAXINT || denom > G_MAXINT) {
1963 if (num == 0 || denom == 0) {
1964 /* in this case we have a wrong fraction or no step, set the step to max
1965 * so that we only add the min value in the loop below */
1970 /* since we only have gst_value_fraction_subtract and not add, negate the
1972 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1974 gst_value_set_fraction (&step, -num, denom);
1976 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
1977 GValue rate = { 0, };
1979 num = gst_value_get_fraction_numerator (&min);
1980 denom = gst_value_get_fraction_denominator (&min);
1981 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1984 /* invert to get the framerate */
1985 g_value_init (&rate, GST_TYPE_FRACTION);
1986 gst_value_set_fraction (&rate, denom, num);
1987 gst_value_list_append_value (&rates, &rate);
1990 /* we're actually adding because step was negated above. This is because
1991 * there is no _add function... */
1992 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1993 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1998 /* no range was added, leave the default range from the template */
1999 GST_WARNING_OBJECT (v4l2object->element,
2000 "no range added, leaving default");
2001 g_value_unset (&rates);
2003 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2004 guint32 maxnum, maxdenom;
2006 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2008 num = ival.stepwise.min.numerator;
2009 denom = ival.stepwise.min.denominator;
2010 if (num > G_MAXINT || denom > G_MAXINT) {
2015 maxnum = ival.stepwise.max.numerator;
2016 maxdenom = ival.stepwise.max.denominator;
2017 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2022 GST_LOG_OBJECT (v4l2object->element,
2023 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2026 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2032 s = gst_structure_copy (template);
2033 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2034 "height", G_TYPE_INT, (gint) height, NULL);
2035 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2036 if (g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2037 gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
2038 (interlaced ? "mixed" : "progressive"), NULL);
2040 if (G_IS_VALUE (&rates)) {
2041 /* only change the framerate on the template when we have a valid probed new
2043 gst_structure_set_value (s, "framerate", &rates);
2044 g_value_unset (&rates);
2046 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
2052 enum_frameintervals_failed:
2054 GST_DEBUG_OBJECT (v4l2object->element,
2055 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2056 GST_FOURCC_ARGS (pixelformat), width, height);
2061 /* I don't see how this is actually an error, we ignore the format then */
2062 GST_WARNING_OBJECT (v4l2object->element,
2063 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2064 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2068 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
2070 #ifdef VIDIOC_ENUM_FRAMESIZES
2072 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2076 gst_structure_get_int (s1, "width", &w1);
2077 gst_structure_get_int (s1, "height", &h1);
2078 gst_structure_get_int (s2, "width", &w2);
2079 gst_structure_get_int (s2, "height", &h2);
2081 /* I think it's safe to assume that this won't overflow for a while */
2082 return ((w2 * h2) - (w1 * h1));
2087 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2088 guint32 pixelformat, const GstStructure * template)
2090 GstCaps *ret = gst_caps_new_empty ();
2093 #ifdef VIDIOC_ENUM_FRAMESIZES
2094 gint fd = v4l2object->video_fd;
2095 struct v4l2_frmsizeenum size;
2096 GList *results = NULL;
2099 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
2100 return gst_caps_new_empty_simple ("video/mpegts");
2102 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2104 size.pixel_format = pixelformat;
2106 GST_DEBUG_OBJECT (v4l2object->element,
2107 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2108 GST_FOURCC_ARGS (pixelformat));
2110 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2111 goto enum_framesizes_failed;
2113 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2115 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
2116 size.discrete.width, size.discrete.height);
2118 w = MIN (size.discrete.width, G_MAXINT);
2119 h = MIN (size.discrete.height, G_MAXINT);
2123 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2124 pixelformat, w, h, template);
2127 results = g_list_prepend (results, tmp);
2131 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2132 GST_DEBUG_OBJECT (v4l2object->element,
2133 "done iterating discrete frame sizes");
2134 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2135 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
2136 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2137 size.stepwise.min_width);
2138 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2139 size.stepwise.min_height);
2140 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2141 size.stepwise.max_width);
2142 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2143 size.stepwise.max_height);
2144 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
2145 size.stepwise.step_width);
2146 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
2147 size.stepwise.step_height);
2149 for (w = size.stepwise.min_width, h = size.stepwise.min_height;
2150 w <= size.stepwise.max_width && h <= size.stepwise.max_height;
2151 w += size.stepwise.step_width, h += size.stepwise.step_height) {
2152 if (w == 0 || h == 0)
2156 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2157 pixelformat, w, h, template);
2160 results = g_list_prepend (results, tmp);
2162 GST_DEBUG_OBJECT (v4l2object->element,
2163 "done iterating stepwise frame sizes");
2164 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2167 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
2168 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2169 size.stepwise.min_width);
2170 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2171 size.stepwise.min_height);
2172 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2173 size.stepwise.max_width);
2174 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2175 size.stepwise.max_height);
2177 w = MAX (size.stepwise.min_width, 1);
2178 h = MAX (size.stepwise.min_height, 1);
2179 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2180 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2183 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2186 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2187 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2190 /* no point using the results list here, since there's only one struct */
2191 gst_caps_append_structure (ret, tmp);
2197 /* we use an intermediary list to store and then sort the results of the
2198 * probing because we can't make any assumptions about the order in which
2199 * the driver will give us the sizes, but we want the final caps to contain
2200 * the results starting with the highest resolution and having the lowest
2201 * resolution last, since order in caps matters for things like fixation. */
2202 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2203 while (results != NULL) {
2204 gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
2205 results = g_list_delete_link (results, results);
2208 if (gst_caps_is_empty (ret))
2209 goto enum_framesizes_no_results;
2214 enum_framesizes_failed:
2216 /* I don't see how this is actually an error */
2217 GST_DEBUG_OBJECT (v4l2object->element,
2218 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2219 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2220 goto default_frame_sizes;
2222 enum_framesizes_no_results:
2224 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2225 * question doesn't actually support it yet */
2226 GST_DEBUG_OBJECT (v4l2object->element,
2227 "No results for pixelformat %" GST_FOURCC_FORMAT
2228 " enumerating frame sizes, trying fallback",
2229 GST_FOURCC_ARGS (pixelformat));
2230 goto default_frame_sizes;
2234 GST_WARNING_OBJECT (v4l2object->element,
2235 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2236 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2237 goto default_frame_sizes;
2239 default_frame_sizes:
2240 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
2242 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2243 gboolean interlaced;
2245 /* This code is for Linux < 2.6.19 */
2247 max_w = max_h = GST_V4L2_MAX_SIZE;
2248 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2249 &min_h, &interlaced)) {
2250 GST_WARNING_OBJECT (v4l2object->element,
2251 "Could not probe minimum capture size for pixelformat %"
2252 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2254 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2255 &max_h, &interlaced)) {
2256 GST_WARNING_OBJECT (v4l2object->element,
2257 "Could not probe maximum capture size for pixelformat %"
2258 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2261 /* Since we can't get framerate directly, try to use the current norm */
2262 if (v4l2object->tv_norm && v4l2object->norms) {
2264 GstTunerNorm *norm = NULL;
2265 GstTunerNorm *current =
2266 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2268 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2269 norm = (GstTunerNorm *) norms->data;
2270 if (!strcmp (norm->label, current->label))
2273 /* If it's possible, set framerate to that (discrete) value */
2275 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2276 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2280 tmp = gst_structure_copy (template);
2282 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2285 /* if norm can't be used, copy the template framerate */
2286 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2291 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2293 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2296 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2298 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2300 if (g_str_equal (gst_structure_get_name (tmp), "video/x-raw"))
2301 gst_structure_set (tmp, "interlace-mode", G_TYPE_STRING,
2302 (interlaced ? "mixed" : "progressive"), NULL);
2303 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2305 gst_caps_append_structure (ret, tmp);
2312 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2313 guint32 pixelformat, gint * width, gint * height, gboolean * interlaced)
2315 struct v4l2_format fmt, prevfmt;
2318 int prevfmt_valid = FALSE;
2319 gboolean ret = FALSE;
2321 g_return_val_if_fail (width != NULL, FALSE);
2322 g_return_val_if_fail (height != NULL, FALSE);
2324 GST_LOG_OBJECT (v4l2object->element,
2325 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2326 *width, *height, GST_FOURCC_ARGS (pixelformat));
2328 fd = v4l2object->video_fd;
2330 memset (&fmt, 0, sizeof (struct v4l2_format));
2331 memset (&prevfmt, 0, sizeof (struct v4l2_format));
2333 /* Some drivers are buggy and will modify the currently set format
2334 when processing VIDIOC_TRY_FMT, so we remember what is set at the
2335 minute, and will reset it when done. */
2336 if (!v4l2object->no_initial_format) {
2337 prevfmt.type = v4l2object->type;
2338 prevfmt_valid = (v4l2_ioctl (fd, VIDIOC_G_FMT, &prevfmt) >= 0);
2341 /* get size delimiters */
2342 memset (&fmt, 0, sizeof (fmt));
2343 fmt.type = v4l2object->type;
2344 fmt.fmt.pix.width = *width;
2345 fmt.fmt.pix.height = *height;
2346 fmt.fmt.pix.pixelformat = pixelformat;
2347 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2349 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2350 if (r < 0 && errno == EINVAL) {
2351 /* try again with interlaced video */
2352 fmt.fmt.pix.width = *width;
2353 fmt.fmt.pix.height = *height;
2354 fmt.fmt.pix.pixelformat = pixelformat;
2355 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2356 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2360 /* The driver might not implement TRY_FMT, in which case we will try
2362 if (errno != ENOTTY)
2365 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
2366 be, because we're still probing */
2367 if (GST_V4L2_IS_ACTIVE (v4l2object))
2370 GST_LOG_OBJECT (v4l2object->element,
2371 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
2373 fmt.fmt.pix.width = *width;
2374 fmt.fmt.pix.height = *height;
2376 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2377 if (r < 0 && errno == EINVAL) {
2378 /* try again with progressive video */
2379 fmt.fmt.pix.width = *width;
2380 fmt.fmt.pix.height = *height;
2381 fmt.fmt.pix.pixelformat = pixelformat;
2382 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2383 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2390 GST_LOG_OBJECT (v4l2object->element,
2391 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2393 *width = fmt.fmt.pix.width;
2394 *height = fmt.fmt.pix.height;
2396 switch (fmt.fmt.pix.field) {
2397 case V4L2_FIELD_ANY:
2398 case V4L2_FIELD_NONE:
2399 *interlaced = FALSE;
2401 case V4L2_FIELD_INTERLACED:
2402 case V4L2_FIELD_INTERLACED_TB:
2403 case V4L2_FIELD_INTERLACED_BT:
2407 GST_WARNING_OBJECT (v4l2object->element,
2408 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u",
2409 GST_FOURCC_ARGS (pixelformat), *width, *height);
2417 GST_WARNING_OBJECT (v4l2object->element,
2418 "Unable to try format: %s", g_strerror (errno));
2421 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &prevfmt) < 0) {
2422 GST_WARNING_OBJECT (v4l2object->element,
2423 "Unable to restore format after trying format: %s",
2424 g_strerror (errno));
2431 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2435 GST_DEBUG_OBJECT (v4l2object->element, "initializing the capture system");
2437 GST_V4L2_CHECK_OPEN (v4l2object);
2438 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2440 /* find transport */
2441 mode = v4l2object->req_mode;
2443 if (v4l2object->vcap.capabilities & V4L2_CAP_READWRITE) {
2444 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2445 mode = GST_V4L2_IO_RW;
2446 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2447 goto method_not_supported;
2449 if (v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
2450 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2451 mode = GST_V4L2_IO_MMAP;
2452 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2453 goto method_not_supported;
2455 /* if still no transport selected, error out */
2456 if (mode == GST_V4L2_IO_AUTO)
2457 goto no_supported_capture_method;
2459 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2460 v4l2object->mode = mode;
2462 /* Map the buffers */
2463 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2465 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2466 goto buffer_pool_new_failed;
2468 GST_V4L2_SET_ACTIVE (v4l2object);
2473 buffer_pool_new_failed:
2475 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2476 (_("Could not map buffers from device '%s'"),
2477 v4l2object->videodev),
2478 ("Failed to create buffer pool: %s", g_strerror (errno)));
2481 method_not_supported:
2483 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2484 (_("The driver of device '%s' does not support the IO method %d"),
2485 v4l2object->videodev, mode), (NULL));
2488 no_supported_capture_method:
2490 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2491 (_("The driver of device '%s' does not support any known IO "
2492 "method."), v4l2object->videodev), (NULL));
2498 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps)
2500 gint fd = v4l2object->video_fd;
2501 struct v4l2_format format;
2502 struct v4l2_streamparm streamparm;
2503 enum v4l2_field field;
2504 guint32 pixelformat;
2505 struct v4l2_fmtdesc *fmtdesc;
2507 gint width, height, fps_n, fps_d;
2510 GST_V4L2_CHECK_OPEN (v4l2object);
2511 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2513 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
2516 pixelformat = fmtdesc->pixelformat;
2517 width = GST_VIDEO_INFO_WIDTH (&info);
2518 height = GST_VIDEO_INFO_HEIGHT (&info);
2519 fps_n = GST_VIDEO_INFO_FPS_N (&info);
2520 fps_d = GST_VIDEO_INFO_FPS_D (&info);
2522 /* get bytesperline for each plane */
2523 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&info); i++)
2524 v4l2object->bytesperline[i] = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
2526 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
2527 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
2528 /* ideally we would differentiate between types of interlaced video
2529 * but there is not sufficient information in the caps..
2531 field = V4L2_FIELD_INTERLACED;
2533 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
2534 field = V4L2_FIELD_NONE;
2537 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
2538 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
2539 GST_FOURCC_ARGS (pixelformat), v4l2object->bytesperline[0]);
2541 /* MPEG-TS source cameras don't get their format set for some reason.
2542 * It looks wrong and we weren't able to track down the reason for that code
2543 * so it is disabled until someone who has an mpeg-ts camera complains...
2546 /* Only unconditionally accept mpegts for sources */
2547 if ((v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
2548 (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')))
2552 memset (&format, 0x00, sizeof (struct v4l2_format));
2553 format.type = v4l2object->type;
2555 if (!v4l2object->no_initial_format) {
2556 if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
2557 goto get_fmt_failed;
2560 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
2561 /* even in v4l2 multiplanar mode we can work in contiguous mode
2562 * if the device supports it */
2564 v4l2object->prefered_non_contiguous ? GST_VIDEO_INFO_N_PLANES (&info) :
2567 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2568 "%" GST_FOURCC_FORMAT " colorspace %d, nb planes %d",
2569 format.fmt.pix_mp.width, format.fmt.pix_mp.height,
2570 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2571 format.fmt.pix_mp.colorspace, format.fmt.pix_mp.num_planes);
2573 if (format.type != v4l2object->type ||
2574 format.fmt.pix_mp.width != width ||
2575 format.fmt.pix_mp.height != height ||
2576 format.fmt.pix_mp.pixelformat != pixelformat ||
2577 format.fmt.pix_mp.field != field ||
2578 format.fmt.pix_mp.num_planes != n_v4l_planes) {
2579 /* something different, set the format */
2580 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2581 "%" GST_FOURCC_FORMAT, width, height, GST_FOURCC_ARGS (pixelformat));
2583 format.type = v4l2object->type;
2584 format.fmt.pix_mp.pixelformat = pixelformat;
2585 format.fmt.pix_mp.width = width;
2586 format.fmt.pix_mp.height = height;
2587 format.fmt.pix_mp.field = field;
2588 format.fmt.pix_mp.num_planes = n_v4l_planes;
2589 /* try to ask our prefered stride but it's not a failure
2590 * if not accepted */
2591 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
2592 format.fmt.pix_mp.plane_fmt[i].bytesperline =
2593 v4l2object->bytesperline[i];
2595 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
2596 goto set_fmt_failed;
2598 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2599 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
2600 format.fmt.pix_mp.height,
2601 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2602 format.fmt.pix_mp.num_planes);
2604 #ifndef GST_DISABLE_GST_DEBUG
2605 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
2606 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
2607 format.fmt.pix_mp.plane_fmt[i].bytesperline);
2610 if (format.fmt.pix_mp.pixelformat != pixelformat)
2611 goto invalid_pixelformat;
2613 /* we set the dimensions just in case but don't validate them afterwards
2614 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
2615 * in ASF mode for example. */
2616 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
2617 if (format.fmt.pix_mp.width != width
2618 || format.fmt.pix_mp.height != height)
2619 goto invalid_dimensions;
2622 if (format.fmt.pix_mp.num_planes != n_v4l_planes)
2623 goto invalid_planes;
2626 /* figure out the frame layout */
2627 v4l2object->n_v4l2_planes = format.fmt.pix_mp.num_planes;
2628 v4l2object->sizeimage = 0;
2629 for (i = 0; i < format.fmt.pix_mp.num_planes; i++) {
2630 v4l2object->bytesperline[i] = format.fmt.pix_mp.plane_fmt[i].bytesperline;
2631 v4l2object->sizeimage += format.fmt.pix_mp.plane_fmt[i].sizeimage;
2634 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2635 "%" GST_FOURCC_FORMAT " bytesperline %d, colorspace %d",
2636 format.fmt.pix.width, format.fmt.pix.height,
2637 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2638 format.fmt.pix.bytesperline, format.fmt.pix.colorspace);
2640 if (format.type != v4l2object->type ||
2641 format.fmt.pix.width != width ||
2642 format.fmt.pix.height != height ||
2643 format.fmt.pix.pixelformat != pixelformat ||
2644 format.fmt.pix.field != field
2645 || format.fmt.pix.bytesperline != stride) {
2646 /* something different, set the format */
2647 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2648 "%" GST_FOURCC_FORMAT " bytesperline %d", width, height,
2649 GST_FOURCC_ARGS (pixelformat), v4l2object->bytesperline[0]);
2651 format.type = v4l2object->type;
2652 format.fmt.pix.width = width;
2653 format.fmt.pix.height = height;
2654 format.fmt.pix.pixelformat = pixelformat;
2655 format.fmt.pix.field = field;
2656 /* try to ask our prefered stride */
2657 format.fmt.pix.bytesperline = v4l2object->bytesperline[0];
2659 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
2660 goto set_fmt_failed;
2662 GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2663 "%" GST_FOURCC_FORMAT " stride %d", format.fmt.pix.width,
2664 format.fmt.pix.height, GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2665 format.fmt.pix.bytesperline);
2667 /* we set the dimensions just in case but don't validate them afterwards
2668 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
2669 * in ASF mode for example. */
2670 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
2671 if (format.fmt.pix.width != width || format.fmt.pix.height != height)
2672 goto invalid_dimensions;
2675 if (format.fmt.pix.pixelformat != pixelformat)
2676 goto invalid_pixelformat;
2679 /* only one plane in non-MPLANE mode */
2680 v4l2object->n_v4l2_planes = 1;
2682 /* figure out the frame layout */
2683 for (i = 0; i < info.finfo->n_planes; i++) {
2684 const GstVideoFormatInfo *finfo = info.finfo;
2685 guint stride = format.fmt.pix.bytesperline;
2687 switch (info.finfo->format) {
2688 case GST_VIDEO_FORMAT_NV12:
2689 case GST_VIDEO_FORMAT_NV21:
2690 case GST_VIDEO_FORMAT_NV16:
2691 case GST_VIDEO_FORMAT_NV24:
2692 v4l2object->bytesperline[i] = (i == 0 ? 1 : 2) *
2693 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, stride);
2696 v4l2object->bytesperline[i] =
2697 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, stride);
2701 GST_DEBUG_OBJECT (v4l2object->element,
2702 "Extrapolated stride for plane %d from %d to %d", i, stride,
2703 v4l2object->bytesperline[i]);
2706 v4l2object->sizeimage = format.fmt.pix.sizeimage;
2709 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %u",
2710 v4l2object->sizeimage);
2712 /* Is there a reason we require the caller to always specify a framerate? */
2713 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
2716 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
2717 streamparm.type = v4l2object->type;
2719 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
2720 goto get_parm_failed;
2722 GST_VIDEO_INFO_FPS_N (&info) =
2723 streamparm.parm.capture.timeperframe.denominator;
2724 GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
2726 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
2727 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2728 GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
2729 streamparm.parm.capture.timeperframe.denominator,
2730 streamparm.parm.capture.timeperframe.numerator);
2732 /* We used to skip frame rate setup if the camera was already setup
2733 * with the requested frame rate. This breaks some cameras though,
2734 * causing them to not output data (several models of Thinkpad cameras
2735 * have this problem at least).
2736 * So, don't skip. */
2737 GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
2739 /* We want to change the frame rate, so check whether we can. Some cheap USB
2740 * cameras don't have the capability */
2741 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
2742 GST_DEBUG_OBJECT (v4l2object->element,
2743 "Not setting framerate (not supported)");
2747 /* Note: V4L2 wants the frame interval, we have the frame rate */
2748 streamparm.parm.capture.timeperframe.numerator = fps_d;
2749 streamparm.parm.capture.timeperframe.denominator = fps_n;
2751 /* some cheap USB cam's won't accept any change */
2752 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
2753 goto set_parm_failed;
2755 /* get new values */
2756 fps_d = streamparm.parm.capture.timeperframe.numerator;
2757 fps_n = streamparm.parm.capture.timeperframe.denominator;
2759 GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
2762 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
2763 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
2767 /* if we have a framerate pre-calculate duration */
2768 if (fps_n > 0 && fps_d > 0) {
2769 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
2771 v4l2object->duration = GST_CLOCK_TIME_NONE;
2773 v4l2object->info = info;
2774 v4l2object->fmtdesc = fmtdesc;
2776 /* now configure ther pools */
2777 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
2785 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
2791 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2792 (_("Device '%s' does not support video capture"),
2793 v4l2object->videodev),
2794 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
2799 if (errno == EBUSY) {
2800 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, BUSY,
2801 (_("Device '%s' is busy"), v4l2object->videodev),
2802 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2803 GST_FOURCC_ARGS (pixelformat), width, height,
2804 g_strerror (errno)));
2806 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2807 (_("Device '%s' cannot capture at %dx%d"),
2808 v4l2object->videodev, width, height),
2809 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2810 GST_FOURCC_ARGS (pixelformat), width, height,
2811 g_strerror (errno)));
2817 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2818 (_("Device '%s' cannot capture at %dx%d"),
2819 v4l2object->videodev, width, height),
2820 ("Tried to capture at %dx%d, but device returned size %dx%d",
2821 width, height, format.fmt.pix.width, format.fmt.pix.height));
2824 invalid_pixelformat:
2826 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2827 (_("Device '%s' cannot capture in the specified format"),
2828 v4l2object->videodev),
2829 ("Tried to capture in %" GST_FOURCC_FORMAT
2830 ", but device returned format" " %" GST_FOURCC_FORMAT,
2831 GST_FOURCC_ARGS (pixelformat),
2832 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
2837 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2838 (_("Device '%s' does support non-contiguous planes"),
2839 v4l2object->videodev),
2840 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
2845 /* it's possible that this call is not supported */
2846 if (errno != EINVAL && errno != ENOTTY) {
2847 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2848 (_("Could not get parameters on device '%s'"),
2849 v4l2object->videodev), GST_ERROR_SYSTEM);
2855 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2856 (_("Video device did not accept new frame rate setting.")),
2862 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2863 (_("Video device could not create buffer pool.")), GST_ERROR_SYSTEM);
2869 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
2874 if (!v4l2object->pool)
2877 s = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (v4l2object->pool));
2878 gst_buffer_pool_config_get_params (s, &oldcaps, NULL, NULL, NULL);
2880 return oldcaps && gst_caps_is_equal (caps, oldcaps);
2884 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
2886 GST_LOG_OBJECT (v4l2object->element, "flush poll");
2887 gst_poll_set_flushing (v4l2object->poll, TRUE);
2893 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
2895 GST_LOG_OBJECT (v4l2object->element, "flush stop poll");
2896 gst_poll_set_flushing (v4l2object->poll, FALSE);
2902 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
2904 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
2906 if (!GST_V4L2_IS_OPEN (v4l2object))
2908 if (!GST_V4L2_IS_ACTIVE (v4l2object))
2911 if (v4l2object->pool) {
2912 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
2913 gst_buffer_pool_set_active (GST_BUFFER_POOL_CAST (v4l2object->pool), FALSE);
2914 gst_object_unref (v4l2object->pool);
2915 v4l2object->pool = NULL;
2918 GST_V4L2_SET_INACTIVE (v4l2object);
2925 gst_v4l2_object_copy (GstV4l2Object * v4l2object, GstBuffer * dest,
2928 const GstVideoFormatInfo *finfo = v4l2object->info.finfo;
2930 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
2931 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
2932 GstVideoFrame src_frame, dest_frame;
2934 GST_DEBUG_OBJECT (v4l2object->element, "copy video frame");
2936 /* we have raw video, use videoframe copy to get strides right */
2937 if (!gst_video_frame_map (&src_frame, &v4l2object->info, src, GST_MAP_READ))
2938 goto invalid_buffer;
2940 if (!gst_video_frame_map (&dest_frame, &v4l2object->info, dest,
2942 gst_video_frame_unmap (&src_frame);
2943 goto invalid_buffer;
2946 gst_video_frame_copy (&dest_frame, &src_frame);
2948 gst_video_frame_unmap (&src_frame);
2949 gst_video_frame_unmap (&dest_frame);
2953 GST_DEBUG_OBJECT (v4l2object->element, "copy raw bytes");
2954 gst_buffer_map (src, &map, GST_MAP_READ);
2955 gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
2956 gst_buffer_unmap (src, &map);
2957 gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
2959 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, v4l2object->element,
2960 "slow copy into buffer %p", dest);
2967 /* No Window available to put our image into */
2968 GST_WARNING_OBJECT (v4l2object->element, "could not map image");
2974 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
2980 if (v4l2object->probed_caps == NULL) {
2981 formats = gst_v4l2_object_get_format_list (v4l2object);
2983 ret = gst_caps_new_empty ();
2985 for (walk = formats; walk; walk = walk->next) {
2986 struct v4l2_fmtdesc *format;
2987 GstStructure *template;
2989 format = (struct v4l2_fmtdesc *) walk->data;
2991 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
2996 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
2997 format->pixelformat, template);
2999 gst_caps_append (ret, tmp);
3001 gst_structure_free (template);
3003 GST_DEBUG_OBJECT (v4l2object->element, "unknown format %u",
3004 format->pixelformat);
3007 v4l2object->probed_caps = ret;
3011 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
3012 GST_CAPS_INTERSECT_FIRST);
3014 ret = gst_caps_ref (v4l2object->probed_caps);
3017 GST_INFO_OBJECT (v4l2object->element, "probed caps: %" GST_PTR_FORMAT, ret);
3018 LOG_CAPS (v4l2object->element, ret);