v4l2object: Split caps in different categories
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2object.c
1 /* GStreamer
2  *
3  * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4  *               2006 Edgard Lima <edgard.lima@indt.org.br>
5  *
6  * gstv4l2object.c: base class for V4L2 elements
7  *
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,
18  * USA.
19  */
20
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
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <string.h>
34
35 #ifdef HAVE_GUDEV
36 #include <gudev/gudev.h>
37 #endif
38
39 #include "v4l2_calls.h"
40 #include "gstv4l2tuner.h"
41 #ifdef HAVE_XVIDEO
42 #include "gstv4l2videooverlay.h"
43 #endif
44 #include "gstv4l2colorbalance.h"
45
46 #include "gst/gst-i18n-plugin.h"
47
48 #include <gst/video/video.h>
49
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
56 #endif
57
58 #ifndef V4L2_PIX_FMT_NV12M
59 #define V4L2_PIX_FMT_NV12M GST_MAKE_FOURCC ('N', 'M', '1', '2')
60 #endif
61 #ifndef V4L2_PIX_FMT_NV21M
62 #define V4L2_PIX_FMT_NV21M GST_MAKE_FOURCC ('N', 'M', '2', '1')
63 #endif
64
65 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
66 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
67 #define GST_CAT_DEFAULT v4l2_debug
68
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
76
77 enum
78 {
79   PROP_0,
80   V4L2_STD_OBJECT_PROPS,
81 };
82
83 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
84
85 #if 0
86 G_LOCK_DEFINE_STATIC (probe_lock);
87
88 const GList *
89 gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
90 {
91   GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
92   static GList *list = NULL;
93
94   G_LOCK (probe_lock);
95
96   if (!list) {
97     list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
98   }
99
100   G_UNLOCK (probe_lock);
101
102   return list;
103 }
104
105 static gboolean init = FALSE;
106 static GList *devices = NULL;
107
108 #ifdef HAVE_GUDEV
109 static gboolean
110 gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
111     GList ** klass_devices)
112 {
113   GUdevClient *client = NULL;
114   GList *item;
115
116   if (!check) {
117     while (devices) {
118       gchar *device = devices->data;
119       devices = g_list_remove (devices, device);
120       g_free (device);
121     }
122
123     GST_INFO ("Enumerating video4linux devices from udev");
124     client = g_udev_client_new (NULL);
125     if (!client) {
126       GST_WARNING ("Failed to initialize gudev client");
127       goto finish;
128     }
129
130     item = g_udev_client_query_by_subsystem (client, "video4linux");
131     while (item) {
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
138          anyway */
139       if (api == 0) {
140         GST_WARNING
141             ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
142       }
143       if ((api == 2 || api == 0)) {
144         devices = g_list_append (devices, devnode);
145       } else {
146         g_free (devnode);
147       }
148       g_object_unref (device);
149       item = item->next;
150     }
151     g_list_free (item);
152     init = TRUE;
153   }
154
155 finish:
156   if (client) {
157     g_object_unref (client);
158   }
159
160   *klass_devices = devices;
161
162   return init;
163 }
164 #endif /* HAVE_GUDEV */
165
166 static gboolean
167 gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
168     GList ** klass_devices)
169 {
170   if (!check) {
171     const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
172     gint base, n, fd;
173
174     while (devices) {
175       gchar *device = devices->data;
176       devices = g_list_remove (devices, device);
177       g_free (device);
178     }
179
180     /*
181      * detect /dev entries
182      */
183     for (n = 0; n < 64; n++) {
184       for (base = 0; dev_base[base] != NULL; base++) {
185         struct stat s;
186         gchar *device = g_strdup_printf ("%s%d",
187             dev_base[base],
188             n);
189
190         /*
191          * does the /dev/ entry exist at all?
192          */
193         if (stat (device, &s) == 0) {
194           /*
195            * yes: is a device attached?
196            */
197           if (S_ISCHR (s.st_mode)) {
198
199             if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
200               if (fd > 0)
201                 close (fd);
202
203               devices = g_list_append (devices, device);
204               break;
205             }
206           }
207         }
208         g_free (device);
209       }
210     }
211     init = TRUE;
212   }
213
214   *klass_devices = devices;
215
216   return init;
217 }
218
219 void
220 gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
221     guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
222 {
223   GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
224
225   switch (prop_id) {
226     case PROP_DEVICE:
227 #ifdef HAVE_GUDEV
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 */
233       break;
234     default:
235       G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
236       break;
237   }
238 }
239
240 gboolean
241 gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
242     guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
243 {
244   GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
245   gboolean ret = FALSE;
246
247   switch (prop_id) {
248     case PROP_DEVICE:
249 #ifdef HAVE_GUDEV
250       ret =
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 */
255       break;
256     default:
257       G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
258       break;
259   }
260   return ret;
261 }
262
263 static GValueArray *
264 gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
265 {
266   GValueArray *array;
267   GValue value = { 0 };
268   GList *item;
269
270   if (!*klass_devices)
271     return NULL;
272
273   array = g_value_array_new (g_list_length (*klass_devices));
274   item = *klass_devices;
275   g_value_init (&value, G_TYPE_STRING);
276   while (item) {
277     gchar *device = item->data;
278
279     g_value_set_string (&value, device);
280     g_value_array_append (array, &value);
281
282     item = item->next;
283   }
284   g_value_unset (&value);
285
286   return array;
287 }
288
289 GValueArray *
290 gst_v4l2_probe_get_values (GstPropertyProbe * probe,
291     guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
292 {
293   GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
294   GValueArray *array = NULL;
295
296   switch (prop_id) {
297     case PROP_DEVICE:
298       array = gst_v4l2_class_list_devices (klass, klass_devices);
299       break;
300     default:
301       G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
302       break;
303   }
304
305   return array;
306 }
307 #endif
308
309 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
310 static GType
311 gst_v4l2_device_get_type (void)
312 {
313   static GType v4l2_device_type = 0;
314
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"},
320
321       {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
322       {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
323
324       {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
325       {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
326
327       {0, NULL, NULL}
328     };
329
330     v4l2_device_type =
331         g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
332   }
333
334   return v4l2_device_type;
335 }
336
337 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
338 static GType
339 gst_v4l2_tv_norm_get_type (void)
340 {
341   static GType v4l2_tv_norm = 0;
342
343   if (!v4l2_tv_norm) {
344     static const GEnumValue tv_norms[] = {
345       {0, "none", "none"},
346
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"},
352
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"},
368
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"},
379
380       {0, NULL, NULL}
381     };
382
383     v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
384   }
385
386   return v4l2_tv_norm;
387 }
388
389 #define GST_TYPE_V4L2_IO_MODE (gst_v4l2_io_mode_get_type ())
390 static GType
391 gst_v4l2_io_mode_get_type (void)
392 {
393   static GType v4l2_io_mode = 0;
394
395   if (!v4l2_io_mode) {
396     static const GEnumValue io_modes[] = {
397       {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
398       {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
399       {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
400       {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
401       {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
402
403       {0, NULL, NULL}
404     };
405     v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
406   }
407   return v4l2_io_mode;
408 }
409
410 void
411 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
412     const char *default_device)
413 {
414   g_object_class_install_property (gobject_class, PROP_DEVICE,
415       g_param_spec_string ("device", "Device", "Device location",
416           default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
417   g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
418       g_param_spec_string ("device-name", "Device name",
419           "Name of the device", DEFAULT_PROP_DEVICE_NAME,
420           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
421   g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
422       g_param_spec_int ("device-fd", "File descriptor",
423           "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
424           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
425   g_object_class_install_property (gobject_class, PROP_FLAGS,
426       g_param_spec_flags ("flags", "Flags", "Device type flags",
427           GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
428           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
429
430   /**
431    * GstV4l2Src:brightness:
432    *
433    * Picture brightness, or more precisely, the black level
434    */
435   g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
436       g_param_spec_int ("brightness", "Brightness",
437           "Picture brightness, or more precisely, the black level", G_MININT,
438           G_MAXINT, 0,
439           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
440   /**
441    * GstV4l2Src:contrast:
442    *
443    * Picture contrast or luma gain
444    */
445   g_object_class_install_property (gobject_class, PROP_CONTRAST,
446       g_param_spec_int ("contrast", "Contrast",
447           "Picture contrast or luma gain", G_MININT,
448           G_MAXINT, 0,
449           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
450   /**
451    * GstV4l2Src:saturation:
452    *
453    * Picture color saturation or chroma gain
454    */
455   g_object_class_install_property (gobject_class, PROP_SATURATION,
456       g_param_spec_int ("saturation", "Saturation",
457           "Picture color saturation or chroma gain", G_MININT,
458           G_MAXINT, 0,
459           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
460   /**
461    * GstV4l2Src:hue:
462    *
463    * Hue or color balance
464    */
465   g_object_class_install_property (gobject_class, PROP_HUE,
466       g_param_spec_int ("hue", "Hue",
467           "Hue or color balance", G_MININT,
468           G_MAXINT, 0,
469           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
470
471   /**
472    * GstV4l2Src:norm:
473    *
474    * TV norm
475    */
476   g_object_class_install_property (gobject_class, PROP_TV_NORM,
477       g_param_spec_enum ("norm", "TV norm",
478           "video standard",
479           GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
480           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
481
482   /**
483    * GstV4l2Src:io-mode:
484    *
485    * IO Mode
486    */
487   g_object_class_install_property (gobject_class, PROP_IO_MODE,
488       g_param_spec_enum ("io-mode", "IO mode",
489           "I/O mode",
490           GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
491           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
492
493   /**
494    * GstV4l2Src:extra-controls:
495    *
496    * Additional v4l2 controls for the device. The controls are identified
497    * by the control name (lowercase with '_' for any non-alphanumeric
498    * characters).
499    *
500    * Since: 1.2
501    */
502   g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
503       g_param_spec_boxed ("extra-controls", "Extra Controls",
504           "Extra v4l2 controls (CIDs) for the device",
505           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
506
507   /**
508    * GstV4l2Src:pixel-aspect-ratio:
509    *
510    * The pixel aspect ratio of the device. This overwrites the pixel aspect
511    * ratio queried from the device.
512    *
513    * Since: 1.2
514    */
515   g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
516       g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
517           "Overwrite the pixel aspect ratio of the device", "1/1",
518           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
519
520   /**
521    * GstV4l2Src:force-aspect-ratio:
522    *
523    * When enabled, the pixel aspect ratio queried from the device or set
524    * with the pixel-aspect-ratio property will be enforced.
525    *
526    * Since: 1.2
527    */
528   g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
529       g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
530           "When enabled, the pixel aspect ratio will be enforced", TRUE,
531           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
532
533 }
534
535 GstV4l2Object *
536 gst_v4l2_object_new (GstElement * element,
537     enum v4l2_buf_type type,
538     const char *default_device,
539     GstV4l2GetInOutFunction get_in_out_func,
540     GstV4l2SetInOutFunction set_in_out_func,
541     GstV4l2UpdateFpsFunction update_fps_func)
542 {
543   GstV4l2Object *v4l2object;
544
545   /*
546    * some default values
547    */
548   v4l2object = g_new0 (GstV4l2Object, 1);
549
550   v4l2object->type = type;
551   v4l2object->formats = NULL;
552
553   v4l2object->element = element;
554   v4l2object->get_in_out_func = get_in_out_func;
555   v4l2object->set_in_out_func = set_in_out_func;
556   v4l2object->update_fps_func = update_fps_func;
557
558   v4l2object->video_fd = -1;
559   v4l2object->poll = gst_poll_new (TRUE);
560   v4l2object->active = FALSE;
561   v4l2object->videodev = g_strdup (default_device);
562
563   v4l2object->norms = NULL;
564   v4l2object->channels = NULL;
565   v4l2object->colors = NULL;
566
567   v4l2object->xwindow_id = 0;
568
569   v4l2object->keep_aspect = TRUE;
570
571   v4l2object->n_v4l2_planes = 0;
572
573   /*
574    * this boolean only applies in v4l2-MPLANE mode.
575    * TRUE: means it prefers to use several v4l2 (non contiguous)
576    * planes. For example if the device supports NV12 and NV12M
577    * both in MPLANE mode, then it will prefer NV12M
578    * FALSE: means it prefers to use one v4l2 plane (which contains
579    * all gst planes as if it was working in non-v4l2-MPLANE mode.
580    * For example if the device supports NV12 and NV12M
581    * both in MPLANE mode, then it will prefer NV12
582    *
583    * this boolean is also used to manage the case where the
584    * device only supports the mode MPLANE and at the same time it
585    * does not support both NV12 and NV12M. So in this case we first
586    * try to use the prefered config, and at least try the other case
587    * if it fails. For example in MPLANE mode if it has NV12 and not
588    * NV21M then even if you set prefered_non_contiguous to TRUE it will
589    * try NV21 as well.
590    */
591   v4l2object->prefered_non_contiguous = TRUE;
592
593   return v4l2object;
594 }
595
596 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
597
598
599 void
600 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
601 {
602   g_return_if_fail (v4l2object != NULL);
603
604   if (v4l2object->videodev)
605     g_free (v4l2object->videodev);
606
607   if (v4l2object->poll)
608     gst_poll_free (v4l2object->poll);
609
610   if (v4l2object->channel)
611     g_free (v4l2object->channel);
612
613   if (v4l2object->formats) {
614     gst_v4l2_object_clear_format_list (v4l2object);
615   }
616
617   if (v4l2object->probed_caps) {
618     gst_caps_unref (v4l2object->probed_caps);
619   }
620
621   g_free (v4l2object);
622 }
623
624
625 static gboolean
626 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
627 {
628   g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
629   g_slist_free (v4l2object->formats);
630   v4l2object->formats = NULL;
631
632   return TRUE;
633 }
634
635 static gint
636 gst_v4l2_object_prop_to_cid (guint prop_id)
637 {
638   gint cid = -1;
639
640   switch (prop_id) {
641     case PROP_BRIGHTNESS:
642       cid = V4L2_CID_BRIGHTNESS;
643       break;
644     case PROP_CONTRAST:
645       cid = V4L2_CID_CONTRAST;
646       break;
647     case PROP_SATURATION:
648       cid = V4L2_CID_SATURATION;
649       break;
650     case PROP_HUE:
651       cid = V4L2_CID_HUE;
652       break;
653     default:
654       GST_WARNING ("unmapped property id: %d", prop_id);
655   }
656   return cid;
657 }
658
659
660 gboolean
661 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
662     guint prop_id, const GValue * value, GParamSpec * pspec)
663 {
664   switch (prop_id) {
665     case PROP_DEVICE:
666       g_free (v4l2object->videodev);
667       v4l2object->videodev = g_value_dup_string (value);
668       break;
669     case PROP_BRIGHTNESS:
670     case PROP_CONTRAST:
671     case PROP_SATURATION:
672     case PROP_HUE:
673     {
674       gint cid = gst_v4l2_object_prop_to_cid (prop_id);
675
676       if (cid != -1) {
677         if (GST_V4L2_IS_OPEN (v4l2object)) {
678           gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
679         }
680       }
681       return TRUE;
682     }
683       break;
684     case PROP_TV_NORM:
685       v4l2object->tv_norm = g_value_get_enum (value);
686       break;
687 #if 0
688     case PROP_CHANNEL:
689       if (GST_V4L2_IS_OPEN (v4l2object)) {
690         GstTuner *tuner = GST_TUNER (v4l2object->element);
691         GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
692             (gchar *) g_value_get_string (value));
693
694         if (channel) {
695           /* like gst_tuner_set_channel (tuner, channel)
696              without g_object_notify */
697           gst_v4l2_tuner_set_channel (v4l2object, channel);
698         }
699       } else {
700         g_free (v4l2object->channel);
701         v4l2object->channel = g_value_dup_string (value);
702       }
703       break;
704     case PROP_FREQUENCY:
705       if (GST_V4L2_IS_OPEN (v4l2object)) {
706         GstTuner *tuner = GST_TUNER (v4l2object->element);
707         GstTunerChannel *channel = gst_tuner_get_channel (tuner);
708
709         if (channel &&
710             GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
711           /* like
712              gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
713              without g_object_notify */
714           gst_v4l2_tuner_set_frequency (v4l2object, channel,
715               g_value_get_ulong (value));
716         }
717       } else {
718         v4l2object->frequency = g_value_get_ulong (value);
719       }
720       break;
721 #endif
722     case PROP_IO_MODE:
723       v4l2object->req_mode = g_value_get_enum (value);
724       break;
725     case PROP_EXTRA_CONTROLS:{
726       const GstStructure *s = gst_value_get_structure (value);
727
728       if (v4l2object->extra_controls)
729         gst_structure_free (v4l2object->extra_controls);
730
731       v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
732       if (GST_V4L2_IS_OPEN (v4l2object))
733         gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
734       break;
735     }
736     case PROP_PIXEL_ASPECT_RATIO:
737       g_free (v4l2object->par);
738       v4l2object->par = g_new0 (GValue, 1);
739       g_value_init (v4l2object->par, GST_TYPE_FRACTION);
740       if (!g_value_transform (value, v4l2object->par)) {
741         g_warning ("Could not transform string to aspect ratio");
742         gst_value_set_fraction (v4l2object->par, 1, 1);
743       }
744       GST_DEBUG_OBJECT (v4l2object->element, "set PAR to %d/%d",
745           gst_value_get_fraction_numerator (v4l2object->par),
746           gst_value_get_fraction_denominator (v4l2object->par));
747       break;
748     case PROP_FORCE_ASPECT_RATIO:
749       v4l2object->keep_aspect = g_value_get_boolean (value);
750       break;
751     default:
752       return FALSE;
753       break;
754   }
755   return TRUE;
756 }
757
758
759 gboolean
760 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
761     guint prop_id, GValue * value, GParamSpec * pspec)
762 {
763   switch (prop_id) {
764     case PROP_DEVICE:
765       g_value_set_string (value, v4l2object->videodev);
766       break;
767     case PROP_DEVICE_NAME:
768     {
769       const guchar *new = NULL;
770
771       if (GST_V4L2_IS_OPEN (v4l2object)) {
772         new = v4l2object->vcap.card;
773       } else if (gst_v4l2_open (v4l2object)) {
774         new = v4l2object->vcap.card;
775         gst_v4l2_close (v4l2object);
776       }
777       g_value_set_string (value, (gchar *) new);
778       break;
779     }
780     case PROP_DEVICE_FD:
781     {
782       if (GST_V4L2_IS_OPEN (v4l2object))
783         g_value_set_int (value, v4l2object->video_fd);
784       else
785         g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
786       break;
787     }
788     case PROP_FLAGS:
789     {
790       guint flags = 0;
791
792       if (GST_V4L2_IS_OPEN (v4l2object)) {
793         flags |= v4l2object->vcap.capabilities &
794             (V4L2_CAP_VIDEO_CAPTURE |
795             V4L2_CAP_VIDEO_OUTPUT |
796             V4L2_CAP_VIDEO_OVERLAY |
797             V4L2_CAP_VBI_CAPTURE |
798             V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
799
800         if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
801           flags |= V4L2_CAP_VIDEO_CAPTURE;
802
803         if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
804           flags |= V4L2_CAP_VIDEO_OUTPUT;
805       }
806       g_value_set_flags (value, flags);
807       break;
808     }
809     case PROP_BRIGHTNESS:
810     case PROP_CONTRAST:
811     case PROP_SATURATION:
812     case PROP_HUE:
813     {
814       gint cid = gst_v4l2_object_prop_to_cid (prop_id);
815
816       if (cid != -1) {
817         if (GST_V4L2_IS_OPEN (v4l2object)) {
818           gint v;
819           if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
820             g_value_set_int (value, v);
821           }
822         }
823       }
824       return TRUE;
825     }
826       break;
827     case PROP_TV_NORM:
828       g_value_set_enum (value, v4l2object->tv_norm);
829       break;
830     case PROP_IO_MODE:
831       g_value_set_enum (value, v4l2object->req_mode);
832       break;
833     case PROP_EXTRA_CONTROLS:
834       gst_value_set_structure (value, v4l2object->extra_controls);
835       break;
836     case PROP_PIXEL_ASPECT_RATIO:
837       if (v4l2object->par)
838         g_value_transform (v4l2object->par, value);
839       break;
840     case PROP_FORCE_ASPECT_RATIO:
841       g_value_set_boolean (value, v4l2object->keep_aspect);
842       break;
843     default:
844       return FALSE;
845       break;
846   }
847   return TRUE;
848 }
849
850 static void
851 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
852 {
853   GstTunerNorm *norm = NULL;
854   GstTunerChannel *channel = NULL;
855   GstTuner *tuner;
856
857   if (!GST_IS_TUNER (v4l2object->element))
858     return;
859
860   tuner = GST_TUNER (v4l2object->element);
861
862   if (v4l2object->tv_norm)
863     norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
864   GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
865       "norm=%p", (guint64) v4l2object->tv_norm, norm);
866   if (norm) {
867     gst_tuner_set_norm (tuner, norm);
868   } else {
869     norm =
870         GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
871     if (norm) {
872       v4l2object->tv_norm =
873           gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
874       gst_tuner_norm_changed (tuner, norm);
875     }
876   }
877
878   if (v4l2object->channel)
879     channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
880   if (channel) {
881     gst_tuner_set_channel (tuner, channel);
882   } else {
883     channel =
884         GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
885             (v4l2object->element)));
886     if (channel) {
887       g_free (v4l2object->channel);
888       v4l2object->channel = g_strdup (channel->label);
889       gst_tuner_channel_changed (tuner, channel);
890     }
891   }
892
893   if (channel
894       && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
895     if (v4l2object->frequency != 0) {
896       gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
897     } else {
898       v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
899       if (v4l2object->frequency == 0) {
900         /* guess */
901         gst_tuner_set_frequency (tuner, channel, 1000);
902       } else {
903       }
904     }
905   }
906 }
907
908 gboolean
909 gst_v4l2_object_open (GstV4l2Object * v4l2object)
910 {
911   if (gst_v4l2_open (v4l2object))
912     gst_v4l2_set_defaults (v4l2object);
913   else
914     return FALSE;
915
916 #ifdef HAVE_XVIDEO
917   gst_v4l2_video_overlay_start (v4l2object);
918 #endif
919
920   return TRUE;
921 }
922
923 gboolean
924 gst_v4l2_object_close (GstV4l2Object * v4l2object)
925 {
926 #ifdef HAVE_XVIDEO
927   gst_v4l2_video_overlay_stop (v4l2object);
928 #endif
929
930   if (!gst_v4l2_close (v4l2object))
931     return FALSE;
932
933   gst_caps_replace (&v4l2object->probed_caps, NULL);
934
935   if (v4l2object->formats) {
936     gst_v4l2_object_clear_format_list (v4l2object);
937   }
938
939   return TRUE;
940 }
941
942
943 /*
944  * common format / caps utilities:
945  */
946 typedef enum
947 {
948   GST_V4L2_RAW = 1 << 0,
949   GST_V4L2_CODEC = 1 << 1,
950   GST_V4L2_TRANSPORT = 1 << 2,
951   GST_V4L2_ALL = 0xffff
952 } GstV4L2FormatFlags;
953
954 typedef struct
955 {
956   guint32 format;
957   gboolean dimensions;
958   GstV4L2FormatFlags flags;
959 } GstV4L2FormatDesc;
960
961 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
962   /* from Linux 2.6.15 videodev2.h */
963   {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
964   {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
965   {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
966   {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
967   {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
968   {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
969   {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
970   {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
971   {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
972   {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
973   {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
974   {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
975   {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
976   {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
977   {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
978   {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
979   {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
980
981   /* two planes -- one Y, one Cr + Cb interleaved  */
982   {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
983   {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
984   {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
985   {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
986
987   /*  The following formats are not defined in the V4L2 specification */
988   {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
989   {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
990   {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
991   {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
992
993   /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
994 #ifdef V4L2_PIX_FMT_SBGGR8
995   {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
996 #endif
997
998   /* compressed formats */
999   {V4L2_PIX_FMT_MJPEG, TRUE, GST_V4L2_CODEC},
1000   {V4L2_PIX_FMT_JPEG, TRUE, GST_V4L2_CODEC},
1001 #ifdef V4L2_PIX_FMT_PJPG
1002   {V4L2_PIX_FMT_PJPG, TRUE, GST_V4L2_CODEC},
1003 #endif
1004   {V4L2_PIX_FMT_DV, TRUE, GST_V4L2_TRANSPORT},
1005   {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
1006 #ifdef V4L2_PIX_FMT_MPEG4
1007   {V4L2_PIX_FMT_MPEG4, TRUE, GST_V4L2_CODEC},
1008 #endif
1009
1010 #ifdef V4L2_PIX_FMT_H263
1011   {V4L2_PIX_FMT_H263, TRUE, GST_V4L2_CODEC},
1012 #endif
1013 #ifdef V4L2_PIX_FMT_H264
1014   {V4L2_PIX_FMT_H264, TRUE, GST_V4L2_CODEC},
1015 #endif
1016
1017   /*  Vendor-specific formats   */
1018   {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
1019
1020 #ifdef V4L2_PIX_FMT_SN9C10X
1021   {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
1022 #endif
1023 #ifdef V4L2_PIX_FMT_PWC1
1024   {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
1025 #endif
1026 #ifdef V4L2_PIX_FMT_PWC2
1027   {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
1028 #endif
1029 #ifdef V4L2_PIX_FMT_YVYU
1030   {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
1031 #endif
1032 };
1033
1034 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
1035
1036
1037 static struct v4l2_fmtdesc *
1038 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
1039     guint32 fourcc)
1040 {
1041   struct v4l2_fmtdesc *fmt;
1042   GSList *walk;
1043
1044   if (fourcc == 0)
1045     return NULL;
1046
1047   walk = gst_v4l2_object_get_format_list (v4l2object);
1048   while (walk) {
1049     fmt = (struct v4l2_fmtdesc *) walk->data;
1050     if (fmt->pixelformat == fourcc)
1051       return fmt;
1052     /* special case for jpeg */
1053     if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
1054         fmt->pixelformat == V4L2_PIX_FMT_JPEG
1055 #ifdef V4L2_PIX_FMT_PJPG
1056         || fmt->pixelformat == V4L2_PIX_FMT_PJPG
1057 #endif
1058         ) {
1059       if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG
1060 #ifdef V4L2_PIX_FMT_PJPG
1061           || fourcc == V4L2_PIX_FMT_PJPG
1062 #endif
1063           ) {
1064         return fmt;
1065       }
1066     }
1067     walk = g_slist_next (walk);
1068   }
1069
1070   return NULL;
1071 }
1072
1073
1074
1075 /* complete made up ranking, the values themselves are meaningless */
1076 /* These ranks MUST be X such that X<<15 fits on a signed int - see
1077    the comment at the end of gst_v4l2_object_format_get_rank. */
1078 #define YUV_BASE_RANK     1000
1079 #define JPEG_BASE_RANK     500
1080 #define DV_BASE_RANK       200
1081 #define RGB_BASE_RANK      100
1082 #define YUV_ODD_BASE_RANK   50
1083 #define RGB_ODD_BASE_RANK   25
1084 #define BAYER_BASE_RANK     15
1085 #define S910_BASE_RANK      10
1086 #define GREY_BASE_RANK       5
1087 #define PWC_BASE_RANK        1
1088
1089 /* This flag is already used by libv4l2 although
1090  * it was added to the Linux kernel in 2.6.32
1091  */
1092 #ifndef V4L2_FMT_FLAG_EMULATED
1093 #define V4L2_FMT_FLAG_EMULATED 0x0002
1094 #endif
1095
1096 static gint
1097 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
1098 {
1099   guint32 fourcc = fmt->pixelformat;
1100   gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
1101   gint rank = 0;
1102
1103   switch (fourcc) {
1104     case V4L2_PIX_FMT_MJPEG:
1105 #ifdef V4L2_PIX_FMT_PJPG
1106     case V4L2_PIX_FMT_PJPG:
1107       rank = JPEG_BASE_RANK;
1108       break;
1109 #endif
1110     case V4L2_PIX_FMT_JPEG:
1111       rank = JPEG_BASE_RANK + 1;
1112       break;
1113     case V4L2_PIX_FMT_MPEG:    /* MPEG          */
1114       rank = JPEG_BASE_RANK + 2;
1115       break;
1116
1117     case V4L2_PIX_FMT_RGB332:
1118     case V4L2_PIX_FMT_RGB555:
1119     case V4L2_PIX_FMT_RGB555X:
1120     case V4L2_PIX_FMT_RGB565:
1121     case V4L2_PIX_FMT_RGB565X:
1122       rank = RGB_ODD_BASE_RANK;
1123       break;
1124
1125     case V4L2_PIX_FMT_RGB24:
1126     case V4L2_PIX_FMT_BGR24:
1127       rank = RGB_BASE_RANK - 1;
1128       break;
1129
1130     case V4L2_PIX_FMT_RGB32:
1131     case V4L2_PIX_FMT_BGR32:
1132       rank = RGB_BASE_RANK;
1133       break;
1134
1135     case V4L2_PIX_FMT_GREY:    /*  8  Greyscale     */
1136       rank = GREY_BASE_RANK;
1137       break;
1138
1139     case V4L2_PIX_FMT_NV12:    /* 12  Y/CbCr 4:2:0  */
1140     case V4L2_PIX_FMT_NV12M:   /* Same as NV12      */
1141     case V4L2_PIX_FMT_NV21:    /* 12  Y/CrCb 4:2:0  */
1142     case V4L2_PIX_FMT_NV21M:   /* Same as NV21      */
1143     case V4L2_PIX_FMT_YYUV:    /* 16  YUV 4:2:2     */
1144     case V4L2_PIX_FMT_HI240:   /*  8  8-bit color   */
1145       rank = YUV_ODD_BASE_RANK;
1146       break;
1147
1148     case V4L2_PIX_FMT_YVU410:  /* YVU9,  9 bits per pixel */
1149       rank = YUV_BASE_RANK + 3;
1150       break;
1151     case V4L2_PIX_FMT_YUV410:  /* YUV9,  9 bits per pixel */
1152       rank = YUV_BASE_RANK + 2;
1153       break;
1154     case V4L2_PIX_FMT_YUV420:  /* I420, 12 bits per pixel */
1155       rank = YUV_BASE_RANK + 7;
1156       break;
1157     case V4L2_PIX_FMT_YUYV:    /* YUY2, 16 bits per pixel */
1158       rank = YUV_BASE_RANK + 10;
1159       break;
1160     case V4L2_PIX_FMT_YVU420:  /* YV12, 12 bits per pixel */
1161       rank = YUV_BASE_RANK + 6;
1162       break;
1163     case V4L2_PIX_FMT_UYVY:    /* UYVY, 16 bits per pixel */
1164       rank = YUV_BASE_RANK + 9;
1165       break;
1166     case V4L2_PIX_FMT_Y41P:    /* Y41P, 12 bits per pixel */
1167       rank = YUV_BASE_RANK + 5;
1168       break;
1169     case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1170       rank = YUV_BASE_RANK + 4;
1171       break;
1172     case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1173       rank = YUV_BASE_RANK + 8;
1174       break;
1175
1176     case V4L2_PIX_FMT_DV:
1177       rank = DV_BASE_RANK;
1178       break;
1179
1180     case V4L2_PIX_FMT_WNVA:    /* Winnov hw compres */
1181       rank = 0;
1182       break;
1183
1184 #ifdef V4L2_PIX_FMT_SBGGR8
1185     case V4L2_PIX_FMT_SBGGR8:
1186       rank = BAYER_BASE_RANK;
1187       break;
1188 #endif
1189
1190 #ifdef V4L2_PIX_FMT_SN9C10X
1191     case V4L2_PIX_FMT_SN9C10X:
1192       rank = S910_BASE_RANK;
1193       break;
1194 #endif
1195
1196 #ifdef V4L2_PIX_FMT_PWC1
1197     case V4L2_PIX_FMT_PWC1:
1198       rank = PWC_BASE_RANK;
1199       break;
1200 #endif
1201 #ifdef V4L2_PIX_FMT_PWC2
1202     case V4L2_PIX_FMT_PWC2:
1203       rank = PWC_BASE_RANK;
1204       break;
1205 #endif
1206
1207     default:
1208       rank = 0;
1209       break;
1210   }
1211
1212   /* All ranks are below 1<<15 so a shift by 15
1213    * will a) make all non-emulated formats larger
1214    * than emulated and b) will not overflow
1215    */
1216   if (!emulated)
1217     rank <<= 15;
1218
1219   return rank;
1220 }
1221
1222
1223
1224 static gint
1225 format_cmp_func (gconstpointer a, gconstpointer b)
1226 {
1227   const struct v4l2_fmtdesc *fa = a;
1228   const struct v4l2_fmtdesc *fb = b;
1229
1230   if (fa->pixelformat == fb->pixelformat)
1231     return 0;
1232
1233   return gst_v4l2_object_format_get_rank (fb) -
1234       gst_v4l2_object_format_get_rank (fa);
1235 }
1236
1237 /******************************************************
1238  * gst_v4l2_object_fill_format_list():
1239  *   create list of supported capture formats
1240  * return value: TRUE on success, FALSE on error
1241  ******************************************************/
1242 static gboolean
1243 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1244     enum v4l2_buf_type type)
1245 {
1246   gint n;
1247   struct v4l2_fmtdesc *format;
1248
1249   GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1250
1251   /* format enumeration */
1252   for (n = 0;; n++) {
1253     format = g_new0 (struct v4l2_fmtdesc, 1);
1254
1255     format->index = n;
1256     format->type = type;
1257
1258     if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1259       if (errno == EINVAL) {
1260         g_free (format);
1261         break;                  /* end of enumeration */
1262       } else {
1263         goto failed;
1264       }
1265     }
1266
1267     GST_LOG_OBJECT (v4l2object->element, "index:       %u", format->index);
1268     GST_LOG_OBJECT (v4l2object->element, "type:        %d", format->type);
1269     GST_LOG_OBJECT (v4l2object->element, "flags:       %08x", format->flags);
1270     GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1271         format->description);
1272     GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1273         GST_FOURCC_ARGS (format->pixelformat));
1274
1275     /* sort formats according to our preference;  we do this, because caps
1276      * are probed in the order the formats are in the list, and the order of
1277      * formats in the final probed caps matters for things like fixation */
1278     v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1279         (GCompareFunc) format_cmp_func);
1280   }
1281
1282 #ifndef GST_DISABLE_GST_DEBUG
1283   {
1284     GSList *l;
1285
1286     GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1287     for (l = v4l2object->formats; l != NULL; l = l->next) {
1288       format = l->data;
1289
1290       GST_INFO_OBJECT (v4l2object->element,
1291           "  %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1292           ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1293     }
1294   }
1295 #endif
1296
1297   return TRUE;
1298
1299   /* ERRORS */
1300 failed:
1301   {
1302     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1303         (_("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)));
1304     g_free (format);
1305     return FALSE;
1306   }
1307 }
1308
1309 /*
1310   * Get the list of supported capture formats, a list of
1311   * <code>struct v4l2_fmtdesc</code>.
1312   */
1313 static GSList *
1314 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1315 {
1316   if (!v4l2object->formats) {
1317
1318     /* check usual way */
1319     gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1320
1321     /* if our driver supports multi-planar
1322      * and if formats are still empty then we can workaround driver bug
1323      * by also looking up formats as if our device was not supporting
1324      * multiplanar */
1325     if (!v4l2object->formats) {
1326       switch (v4l2object->type) {
1327         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1328           gst_v4l2_object_fill_format_list (v4l2object,
1329               V4L2_BUF_TYPE_VIDEO_CAPTURE);
1330           break;
1331
1332         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1333           gst_v4l2_object_fill_format_list (v4l2object,
1334               V4L2_BUF_TYPE_VIDEO_OUTPUT);
1335           break;
1336
1337         default:
1338           break;
1339       }
1340     }
1341   }
1342   return v4l2object->formats;
1343 }
1344
1345
1346 GstStructure *
1347 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1348 {
1349   GstStructure *structure = NULL;
1350
1351   switch (fourcc) {
1352     case V4L2_PIX_FMT_MJPEG:   /* Motion-JPEG */
1353 #ifdef V4L2_PIX_FMT_PJPG
1354     case V4L2_PIX_FMT_PJPG:    /* Progressive-JPEG */
1355 #endif
1356     case V4L2_PIX_FMT_JPEG:    /* JFIF JPEG */
1357       structure = gst_structure_new_empty ("image/jpeg");
1358       break;
1359     case V4L2_PIX_FMT_YYUV:    /* 16  YUV 4:2:2     */
1360     case V4L2_PIX_FMT_HI240:   /*  8  8-bit color   */
1361       /* FIXME: get correct fourccs here */
1362       break;
1363 #ifdef V4L2_PIX_FMT_MPEG4
1364     case V4L2_PIX_FMT_MPEG4:
1365       structure = gst_structure_new ("video/mpeg",
1366           "mpegversion", G_TYPE_INT, 4, "systemstream",
1367           G_TYPE_BOOLEAN, FALSE, NULL);
1368       break;
1369 #endif
1370 #ifdef V4L2_PIX_FMT_H263
1371     case V4L2_PIX_FMT_H263:
1372       structure = gst_structure_new ("video/x-h263",
1373           "variant", G_TYPE_STRING, "itu", NULL);
1374       break;
1375 #endif
1376 #ifdef V4L2_PIX_FMT_H264
1377     case V4L2_PIX_FMT_H264:    /* H.264 */
1378       structure = gst_structure_new_empty ("video/x-h264");
1379       break;
1380 #endif
1381     case V4L2_PIX_FMT_RGB332:
1382     case V4L2_PIX_FMT_RGB555X:
1383     case V4L2_PIX_FMT_RGB565X:
1384       /* FIXME: get correct fourccs here */
1385       break;
1386     case V4L2_PIX_FMT_GREY:    /*  8  Greyscale     */
1387     case V4L2_PIX_FMT_RGB555:
1388     case V4L2_PIX_FMT_RGB565:
1389     case V4L2_PIX_FMT_RGB24:
1390     case V4L2_PIX_FMT_BGR24:
1391     case V4L2_PIX_FMT_RGB32:
1392     case V4L2_PIX_FMT_BGR32:
1393     case V4L2_PIX_FMT_NV12:    /* 12  Y/CbCr 4:2:0  */
1394     case V4L2_PIX_FMT_NV12M:
1395     case V4L2_PIX_FMT_NV21:    /* 12  Y/CrCb 4:2:0  */
1396     case V4L2_PIX_FMT_NV21M:
1397     case V4L2_PIX_FMT_YVU410:
1398     case V4L2_PIX_FMT_YUV410:
1399     case V4L2_PIX_FMT_YUV420:  /* I420/IYUV */
1400     case V4L2_PIX_FMT_YUYV:
1401     case V4L2_PIX_FMT_YVU420:
1402     case V4L2_PIX_FMT_UYVY:
1403 #if 0
1404     case V4L2_PIX_FMT_Y41P:
1405 #endif
1406     case V4L2_PIX_FMT_YUV422P:
1407 #ifdef V4L2_PIX_FMT_YVYU
1408     case V4L2_PIX_FMT_YVYU:
1409 #endif
1410     case V4L2_PIX_FMT_YUV411P:{
1411       GstVideoFormat format;
1412
1413       switch (fourcc) {
1414         case V4L2_PIX_FMT_GREY:        /*  8  Greyscale     */
1415           format = GST_VIDEO_FORMAT_GRAY8;
1416           break;
1417         case V4L2_PIX_FMT_RGB555:
1418           format = GST_VIDEO_FORMAT_RGB15;
1419           break;
1420         case V4L2_PIX_FMT_RGB565:
1421           format = GST_VIDEO_FORMAT_RGB16;
1422           break;
1423         case V4L2_PIX_FMT_RGB24:
1424           format = GST_VIDEO_FORMAT_RGB;
1425           break;
1426         case V4L2_PIX_FMT_BGR24:
1427           format = GST_VIDEO_FORMAT_BGR;
1428           break;
1429         case V4L2_PIX_FMT_RGB32:
1430           format = GST_VIDEO_FORMAT_RGBx;
1431           break;
1432         case V4L2_PIX_FMT_BGR32:
1433           format = GST_VIDEO_FORMAT_BGRx;
1434           break;
1435         case V4L2_PIX_FMT_NV12:
1436         case V4L2_PIX_FMT_NV12M:
1437           format = GST_VIDEO_FORMAT_NV12;
1438           break;
1439         case V4L2_PIX_FMT_NV21:
1440         case V4L2_PIX_FMT_NV21M:
1441           format = GST_VIDEO_FORMAT_NV21;
1442           break;
1443         case V4L2_PIX_FMT_YVU410:
1444           format = GST_VIDEO_FORMAT_YVU9;
1445           break;
1446         case V4L2_PIX_FMT_YUV410:
1447           format = GST_VIDEO_FORMAT_YUV9;
1448           break;
1449         case V4L2_PIX_FMT_YUV420:
1450           format = GST_VIDEO_FORMAT_I420;
1451           break;
1452         case V4L2_PIX_FMT_YUYV:
1453           format = GST_VIDEO_FORMAT_YUY2;
1454           break;
1455         case V4L2_PIX_FMT_YVU420:
1456           format = GST_VIDEO_FORMAT_YV12;
1457           break;
1458         case V4L2_PIX_FMT_UYVY:
1459           format = GST_VIDEO_FORMAT_UYVY;
1460           break;
1461 #if 0
1462         case V4L2_PIX_FMT_Y41P:
1463           format = GST_VIDEO_FORMAT_Y41P;
1464           break;
1465 #endif
1466         case V4L2_PIX_FMT_YUV411P:
1467           format = GST_VIDEO_FORMAT_Y41B;
1468           break;
1469         case V4L2_PIX_FMT_YUV422P:
1470           format = GST_VIDEO_FORMAT_Y42B;
1471           break;
1472 #ifdef V4L2_PIX_FMT_YVYU
1473         case V4L2_PIX_FMT_YVYU:
1474           format = GST_VIDEO_FORMAT_YVYU;
1475           break;
1476 #endif
1477         default:
1478           format = GST_VIDEO_FORMAT_UNKNOWN;
1479           g_assert_not_reached ();
1480           break;
1481       }
1482       if (format != GST_VIDEO_FORMAT_UNKNOWN)
1483         structure = gst_structure_new ("video/x-raw",
1484             "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1485       break;
1486     }
1487     case V4L2_PIX_FMT_DV:
1488       structure =
1489           gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1490           NULL);
1491       break;
1492     case V4L2_PIX_FMT_MPEG:    /* MPEG          */
1493       structure = gst_structure_new_empty ("video/mpegts");
1494       break;
1495     case V4L2_PIX_FMT_WNVA:    /* Winnov hw compres */
1496       break;
1497 #ifdef V4L2_PIX_FMT_SBGGR8
1498     case V4L2_PIX_FMT_SBGGR8:
1499       structure = gst_structure_new_empty ("video/x-bayer");
1500       break;
1501 #endif
1502 #ifdef V4L2_PIX_FMT_SN9C10X
1503     case V4L2_PIX_FMT_SN9C10X:
1504       structure = gst_structure_new_empty ("video/x-sonix");
1505       break;
1506 #endif
1507 #ifdef V4L2_PIX_FMT_PWC1
1508     case V4L2_PIX_FMT_PWC1:
1509       structure = gst_structure_new_empty ("video/x-pwc1");
1510       break;
1511 #endif
1512 #ifdef V4L2_PIX_FMT_PWC2
1513     case V4L2_PIX_FMT_PWC2:
1514       structure = gst_structure_new_empty ("video/x-pwc2");
1515       break;
1516 #endif
1517     default:
1518       GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
1519           fourcc, GST_FOURCC_ARGS (fourcc));
1520       break;
1521   }
1522
1523   return structure;
1524 }
1525
1526
1527 static GstCaps *
1528 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1529 {
1530   GstStructure *structure;
1531   GstCaps *caps;
1532   guint i;
1533
1534   caps = gst_caps_new_empty ();
1535   for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1536
1537     if ((gst_v4l2_formats[i].flags & flags) == 0)
1538       continue;
1539
1540     structure =
1541         gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
1542     if (structure) {
1543       if (gst_v4l2_formats[i].dimensions) {
1544         gst_structure_set (structure,
1545             "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1546             "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1547             "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
1548       }
1549       gst_caps_append_structure (caps, structure);
1550     }
1551   }
1552
1553   return gst_caps_simplify (caps);
1554 }
1555
1556 GstCaps *
1557 gst_v4l2_object_get_all_caps (void)
1558 {
1559   static GstCaps *caps = NULL;
1560
1561   if (caps == NULL)
1562     caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1563
1564   return gst_caps_ref (caps);
1565 }
1566
1567 GstCaps *
1568 gst_v4l2_object_get_raw_caps (void)
1569 {
1570   static GstCaps *caps = NULL;
1571
1572   if (caps == NULL)
1573     caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1574
1575   return gst_caps_ref (caps);
1576 }
1577
1578 GstCaps *
1579 gst_v4l2_object_get_codec_caps (void)
1580 {
1581   static GstCaps *caps = NULL;
1582
1583   if (caps == NULL)
1584     caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1585
1586   return gst_caps_ref (caps);
1587 }
1588
1589 /* if the device actually support multi-planar
1590  * we also allow to use plane in a contiguous manner
1591  * if the device can do that. Through prefered_non_contiguous */
1592 static gboolean
1593 gst_v4l2_object_has_mplane (GstV4l2Object * obj)
1594 {
1595   gboolean ret = FALSE;
1596
1597   switch (obj->type) {
1598     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1599       ret = (obj->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) != 0
1600           && obj->prefered_non_contiguous;
1601       break;
1602     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1603       ret = (obj->vcap.capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE) != 0
1604           && obj->prefered_non_contiguous;
1605       break;
1606     default:
1607       ret = FALSE;
1608       break;
1609   }
1610
1611   return ret;
1612 }
1613
1614 /* collect data for the given caps
1615  * @caps: given input caps
1616  * @format: location for the v4l format
1617  * @w/@h: location for width and height
1618  * @fps_n/@fps_d: location for framerate
1619  * @size: location for expected size of the frame or 0 if unknown
1620  */
1621 static gboolean
1622 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1623     struct v4l2_fmtdesc **format, GstVideoInfo * info)
1624 {
1625   GstStructure *structure;
1626   guint32 fourcc;
1627   const gchar *mimetype;
1628   struct v4l2_fmtdesc *fmt;
1629
1630   /* default unknown values */
1631   fourcc = 0;
1632
1633   structure = gst_caps_get_structure (caps, 0);
1634
1635   mimetype = gst_structure_get_name (structure);
1636
1637   if (!gst_video_info_from_caps (info, caps))
1638     goto invalid_format;
1639
1640   if (g_str_equal (mimetype, "video/x-raw")) {
1641     switch (GST_VIDEO_INFO_FORMAT (info)) {
1642       case GST_VIDEO_FORMAT_I420:
1643         fourcc = V4L2_PIX_FMT_YUV420;
1644         break;
1645       case GST_VIDEO_FORMAT_YUY2:
1646         fourcc = V4L2_PIX_FMT_YUYV;
1647         break;
1648 #if 0
1649       case GST_VIDEO_FORMAT_Y41P:
1650         fourcc = V4L2_PIX_FMT_Y41P;
1651         break;
1652 #endif
1653       case GST_VIDEO_FORMAT_UYVY:
1654         fourcc = V4L2_PIX_FMT_UYVY;
1655         break;
1656       case GST_VIDEO_FORMAT_YV12:
1657         fourcc = V4L2_PIX_FMT_YVU420;
1658         break;
1659       case GST_VIDEO_FORMAT_Y41B:
1660         fourcc = V4L2_PIX_FMT_YUV411P;
1661         break;
1662       case GST_VIDEO_FORMAT_Y42B:
1663         fourcc = V4L2_PIX_FMT_YUV422P;
1664         break;
1665       case GST_VIDEO_FORMAT_NV12:
1666         fourcc =
1667             gst_v4l2_object_has_mplane (v4l2object) ? V4L2_PIX_FMT_NV12M :
1668             V4L2_PIX_FMT_NV12;
1669         break;
1670       case GST_VIDEO_FORMAT_NV21:
1671         fourcc =
1672             gst_v4l2_object_has_mplane (v4l2object) ? V4L2_PIX_FMT_NV21M :
1673             V4L2_PIX_FMT_NV21;
1674         break;
1675 #ifdef V4L2_PIX_FMT_YVYU
1676       case GST_VIDEO_FORMAT_YVYU:
1677         fourcc = V4L2_PIX_FMT_YVYU;
1678         break;
1679 #endif
1680       case GST_VIDEO_FORMAT_RGB15:
1681         fourcc = V4L2_PIX_FMT_RGB555;
1682         break;
1683       case GST_VIDEO_FORMAT_RGB16:
1684         fourcc = V4L2_PIX_FMT_RGB565;
1685         break;
1686       case GST_VIDEO_FORMAT_RGB:
1687         fourcc = V4L2_PIX_FMT_RGB24;
1688         break;
1689       case GST_VIDEO_FORMAT_BGR:
1690         fourcc = V4L2_PIX_FMT_BGR24;
1691         break;
1692       case GST_VIDEO_FORMAT_RGBx:
1693       case GST_VIDEO_FORMAT_RGBA:
1694         fourcc = V4L2_PIX_FMT_RGB32;
1695         break;
1696       case GST_VIDEO_FORMAT_BGRx:
1697       case GST_VIDEO_FORMAT_BGRA:
1698         fourcc = V4L2_PIX_FMT_BGR32;
1699         break;
1700       case GST_VIDEO_FORMAT_GRAY8:
1701         fourcc = V4L2_PIX_FMT_GREY;
1702       default:
1703         break;
1704     }
1705   } else {
1706     if (g_str_equal (mimetype, "video/mpegts")) {
1707       fourcc = V4L2_PIX_FMT_MPEG;
1708     } else if (g_str_equal (mimetype, "video/x-dv")) {
1709       fourcc = V4L2_PIX_FMT_DV;
1710     } else if (g_str_equal (mimetype, "image/jpeg")) {
1711       fourcc = V4L2_PIX_FMT_JPEG;
1712 #ifdef V4L2_PIX_FMT_MPEG4
1713     } else if (g_str_equal (mimetype, "video/mpeg")) {
1714       fourcc = V4L2_PIX_FMT_MPEG4;
1715 #endif
1716 #ifdef V4L2_PIX_FMT_H263
1717     } else if (g_str_equal (mimetype, "video/x-h263")) {
1718       fourcc = V4L2_PIX_FMT_H263;
1719 #endif
1720 #ifdef V4L2_PIX_FMT_H264
1721     } else if (g_str_equal (mimetype, "video/x-h264")) {
1722       fourcc = V4L2_PIX_FMT_H264;
1723 #endif
1724 #ifdef V4L2_PIX_FMT_SBGGR8
1725     } else if (g_str_equal (mimetype, "video/x-bayer")) {
1726       fourcc = V4L2_PIX_FMT_SBGGR8;
1727 #endif
1728 #ifdef V4L2_PIX_FMT_SN9C10X
1729     } else if (g_str_equal (mimetype, "video/x-sonix")) {
1730       fourcc = V4L2_PIX_FMT_SN9C10X;
1731 #endif
1732 #ifdef V4L2_PIX_FMT_PWC1
1733     } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1734       fourcc = V4L2_PIX_FMT_PWC1;
1735 #endif
1736 #ifdef V4L2_PIX_FMT_PWC2
1737     } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1738       fourcc = V4L2_PIX_FMT_PWC2;
1739     }
1740 #endif
1741   }
1742
1743   if (fourcc == 0)
1744     goto unhandled_format;
1745
1746   fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1747   if (fmt == NULL)
1748     goto unsupported_format;
1749
1750   *format = fmt;
1751
1752   return TRUE;
1753
1754   /* ERRORS */
1755 invalid_format:
1756   {
1757     GST_DEBUG_OBJECT (v4l2object, "invalid format");
1758     return FALSE;
1759   }
1760 unhandled_format:
1761   {
1762     GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1763     return FALSE;
1764   }
1765 unsupported_format:
1766   {
1767     GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1768     return FALSE;
1769   }
1770 }
1771
1772
1773 static gboolean
1774 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1775     guint32 pixelformat, gint * width, gint * height, gboolean * interlaced);
1776
1777 static void
1778 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1779 {
1780   struct v4l2_cropcap cropcap;
1781   int num = 1, den = 1;
1782
1783   if (!v4l2object->keep_aspect)
1784     return;
1785
1786   if (v4l2object->par) {
1787     num = gst_value_get_fraction_numerator (v4l2object->par);
1788     den = gst_value_get_fraction_denominator (v4l2object->par);
1789     goto done;
1790   }
1791
1792   memset (&cropcap, 0, sizeof (cropcap));
1793
1794   cropcap.type = v4l2object->type;
1795   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
1796     goto cropcap_failed;
1797
1798   num = cropcap.pixelaspect.numerator;
1799   den = cropcap.pixelaspect.denominator;
1800
1801 done:
1802   gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, num, den,
1803       NULL);
1804   return;
1805
1806 cropcap_failed:
1807   if (errno != ENOTTY)
1808     GST_WARNING_OBJECT (v4l2object->element,
1809         "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
1810         g_strerror (errno));
1811   goto done;
1812 }
1813
1814
1815 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1816 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1817 static GstStructure *
1818 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
1819     guint32 pixelformat,
1820     guint32 width, guint32 height, const GstStructure * template)
1821 {
1822   gint fd = v4l2object->video_fd;
1823   struct v4l2_frmivalenum ival;
1824   guint32 num, denom;
1825   GstStructure *s;
1826   GValue rates = { 0, };
1827   gboolean interlaced;
1828   gint int_width = width;
1829   gint int_height = height;
1830
1831   if (!strcmp ((char *) v4l2object->vcap.driver, "uvcvideo")) {
1832     /*
1833      * UVC devices are never interlaced, and doing VIDIOC_TRY_FMT on them
1834      * causes expensive and slow USB IO, so don't probe them for interlaced
1835      */
1836     interlaced = FALSE;
1837   } else {
1838     /* Interlaced detection using VIDIOC_TRY/S_FMT */
1839     if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat,
1840             &int_width, &int_height, &interlaced))
1841       return NULL;
1842   }
1843
1844   memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1845   ival.index = 0;
1846   ival.pixel_format = pixelformat;
1847   ival.width = width;
1848   ival.height = height;
1849
1850   GST_LOG_OBJECT (v4l2object->element,
1851       "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
1852       GST_FOURCC_ARGS (pixelformat));
1853
1854   /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1855    * fraction to get framerate */
1856   if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1857     goto enum_frameintervals_failed;
1858
1859   if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1860     GValue rate = { 0, };
1861
1862     g_value_init (&rates, GST_TYPE_LIST);
1863     g_value_init (&rate, GST_TYPE_FRACTION);
1864
1865     do {
1866       num = ival.discrete.numerator;
1867       denom = ival.discrete.denominator;
1868
1869       if (num > G_MAXINT || denom > G_MAXINT) {
1870         /* let us hope we don't get here... */
1871         num >>= 1;
1872         denom >>= 1;
1873       }
1874
1875       GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
1876           denom, num);
1877
1878       /* swap to get the framerate */
1879       gst_value_set_fraction (&rate, denom, num);
1880       gst_value_list_append_value (&rates, &rate);
1881
1882       ival.index++;
1883     } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1884   } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1885     GValue min = { 0, };
1886     GValue step = { 0, };
1887     GValue max = { 0, };
1888     gboolean added = FALSE;
1889     guint32 minnum, mindenom;
1890     guint32 maxnum, maxdenom;
1891
1892     g_value_init (&rates, GST_TYPE_LIST);
1893
1894     g_value_init (&min, GST_TYPE_FRACTION);
1895     g_value_init (&step, GST_TYPE_FRACTION);
1896     g_value_init (&max, GST_TYPE_FRACTION);
1897
1898     /* get the min */
1899     minnum = ival.stepwise.min.numerator;
1900     mindenom = ival.stepwise.min.denominator;
1901     if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1902       minnum >>= 1;
1903       mindenom >>= 1;
1904     }
1905     GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
1906         minnum, mindenom);
1907     gst_value_set_fraction (&min, minnum, mindenom);
1908
1909     /* get the max */
1910     maxnum = ival.stepwise.max.numerator;
1911     maxdenom = ival.stepwise.max.denominator;
1912     if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1913       maxnum >>= 1;
1914       maxdenom >>= 1;
1915     }
1916
1917     GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
1918         maxnum, maxdenom);
1919     gst_value_set_fraction (&max, maxnum, maxdenom);
1920
1921     /* get the step */
1922     num = ival.stepwise.step.numerator;
1923     denom = ival.stepwise.step.denominator;
1924     if (num > G_MAXINT || denom > G_MAXINT) {
1925       num >>= 1;
1926       denom >>= 1;
1927     }
1928
1929     if (num == 0 || denom == 0) {
1930       /* in this case we have a wrong fraction or no step, set the step to max
1931        * so that we only add the min value in the loop below */
1932       num = maxnum;
1933       denom = maxdenom;
1934     }
1935
1936     /* since we only have gst_value_fraction_subtract and not add, negate the
1937      * numerator */
1938     GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
1939         num, denom);
1940     gst_value_set_fraction (&step, -num, denom);
1941
1942     while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
1943       GValue rate = { 0, };
1944
1945       num = gst_value_get_fraction_numerator (&min);
1946       denom = gst_value_get_fraction_denominator (&min);
1947       GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
1948           denom, num);
1949
1950       /* invert to get the framerate */
1951       g_value_init (&rate, GST_TYPE_FRACTION);
1952       gst_value_set_fraction (&rate, denom, num);
1953       gst_value_list_append_value (&rates, &rate);
1954       added = TRUE;
1955
1956       /* we're actually adding because step was negated above. This is because
1957        * there is no _add function... */
1958       if (!gst_value_fraction_subtract (&min, &min, &step)) {
1959         GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
1960         break;
1961       }
1962     }
1963     if (!added) {
1964       /* no range was added, leave the default range from the template */
1965       GST_WARNING_OBJECT (v4l2object->element,
1966           "no range added, leaving default");
1967       g_value_unset (&rates);
1968     }
1969   } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1970     guint32 maxnum, maxdenom;
1971
1972     g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1973
1974     num = ival.stepwise.min.numerator;
1975     denom = ival.stepwise.min.denominator;
1976     if (num > G_MAXINT || denom > G_MAXINT) {
1977       num >>= 1;
1978       denom >>= 1;
1979     }
1980
1981     maxnum = ival.stepwise.max.numerator;
1982     maxdenom = ival.stepwise.max.denominator;
1983     if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1984       maxnum >>= 1;
1985       maxdenom >>= 1;
1986     }
1987
1988     GST_LOG_OBJECT (v4l2object->element,
1989         "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
1990         num);
1991
1992     gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1993   } else {
1994     goto unknown_type;
1995   }
1996
1997 return_data:
1998   s = gst_structure_copy (template);
1999   gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2000       "height", G_TYPE_INT, (gint) height, NULL);
2001   gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2002   if (g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2003     gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
2004         (interlaced ? "mixed" : "progressive"), NULL);
2005
2006   if (G_IS_VALUE (&rates)) {
2007     /* only change the framerate on the template when we have a valid probed new
2008      * value */
2009     gst_structure_set_value (s, "framerate", &rates);
2010     g_value_unset (&rates);
2011   } else {
2012     gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1,
2013         NULL);
2014   }
2015   return s;
2016
2017   /* ERRORS */
2018 enum_frameintervals_failed:
2019   {
2020     GST_DEBUG_OBJECT (v4l2object->element,
2021         "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2022         GST_FOURCC_ARGS (pixelformat), width, height);
2023     goto return_data;
2024   }
2025 unknown_type:
2026   {
2027     /* I don't see how this is actually an error, we ignore the format then */
2028     GST_WARNING_OBJECT (v4l2object->element,
2029         "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2030         GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2031     return NULL;
2032   }
2033 }
2034 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
2035
2036 #ifdef VIDIOC_ENUM_FRAMESIZES
2037 static gint
2038 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2039 {
2040   int w1, h1, w2, h2;
2041
2042   gst_structure_get_int (s1, "width", &w1);
2043   gst_structure_get_int (s1, "height", &h1);
2044   gst_structure_get_int (s2, "width", &w2);
2045   gst_structure_get_int (s2, "height", &h2);
2046
2047   /* I think it's safe to assume that this won't overflow for a while */
2048   return ((w2 * h2) - (w1 * h1));
2049 }
2050 #endif
2051
2052 static GstCaps *
2053 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2054     guint32 pixelformat, const GstStructure * template)
2055 {
2056   GstCaps *ret = gst_caps_new_empty ();
2057   GstStructure *tmp;
2058
2059 #ifdef VIDIOC_ENUM_FRAMESIZES
2060   gint fd = v4l2object->video_fd;
2061   struct v4l2_frmsizeenum size;
2062   GList *results = NULL;
2063   guint32 w, h;
2064
2065   if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G'))
2066     return gst_caps_new_empty_simple ("video/mpegts");
2067
2068   memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2069   size.index = 0;
2070   size.pixel_format = pixelformat;
2071
2072   GST_DEBUG_OBJECT (v4l2object->element,
2073       "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2074       GST_FOURCC_ARGS (pixelformat));
2075
2076   if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2077     goto enum_framesizes_failed;
2078
2079   if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2080     do {
2081       GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
2082           size.discrete.width, size.discrete.height);
2083
2084       w = MIN (size.discrete.width, G_MAXINT);
2085       h = MIN (size.discrete.height, G_MAXINT);
2086
2087       if (w && h) {
2088         tmp =
2089             gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2090             pixelformat, w, h, template);
2091
2092         if (tmp)
2093           results = g_list_prepend (results, tmp);
2094       }
2095
2096       size.index++;
2097     } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2098     GST_DEBUG_OBJECT (v4l2object->element,
2099         "done iterating discrete frame sizes");
2100   } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2101     GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
2102     GST_DEBUG_OBJECT (v4l2object->element, "min width:   %d",
2103         size.stepwise.min_width);
2104     GST_DEBUG_OBJECT (v4l2object->element, "min height:  %d",
2105         size.stepwise.min_height);
2106     GST_DEBUG_OBJECT (v4l2object->element, "max width:   %d",
2107         size.stepwise.max_width);
2108     GST_DEBUG_OBJECT (v4l2object->element, "min height:  %d",
2109         size.stepwise.max_height);
2110     GST_DEBUG_OBJECT (v4l2object->element, "step width:  %d",
2111         size.stepwise.step_width);
2112     GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
2113         size.stepwise.step_height);
2114
2115     for (w = size.stepwise.min_width, h = size.stepwise.min_height;
2116         w <= size.stepwise.max_width && h <= size.stepwise.max_height;
2117         w += size.stepwise.step_width, h += size.stepwise.step_height) {
2118       if (w == 0 || h == 0)
2119         continue;
2120
2121       tmp =
2122           gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2123           pixelformat, w, h, template);
2124
2125       if (tmp)
2126         results = g_list_prepend (results, tmp);
2127     }
2128     GST_DEBUG_OBJECT (v4l2object->element,
2129         "done iterating stepwise frame sizes");
2130   } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2131     guint32 maxw, maxh;
2132
2133     GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
2134     GST_DEBUG_OBJECT (v4l2object->element, "min width:   %d",
2135         size.stepwise.min_width);
2136     GST_DEBUG_OBJECT (v4l2object->element, "min height:  %d",
2137         size.stepwise.min_height);
2138     GST_DEBUG_OBJECT (v4l2object->element, "max width:   %d",
2139         size.stepwise.max_width);
2140     GST_DEBUG_OBJECT (v4l2object->element, "min height:  %d",
2141         size.stepwise.max_height);
2142
2143     w = MAX (size.stepwise.min_width, 1);
2144     h = MAX (size.stepwise.min_height, 1);
2145     maxw = MIN (size.stepwise.max_width, G_MAXINT);
2146     maxh = MIN (size.stepwise.max_height, G_MAXINT);
2147
2148     tmp =
2149         gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2150         w, h, template);
2151     if (tmp) {
2152       gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2153           (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2154           NULL);
2155
2156       /* no point using the results list here, since there's only one struct */
2157       gst_caps_append_structure (ret, tmp);
2158     }
2159   } else {
2160     goto unknown_type;
2161   }
2162
2163   /* we use an intermediary list to store and then sort the results of the
2164    * probing because we can't make any assumptions about the order in which
2165    * the driver will give us the sizes, but we want the final caps to contain
2166    * the results starting with the highest resolution and having the lowest
2167    * resolution last, since order in caps matters for things like fixation. */
2168   results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2169   while (results != NULL) {
2170     gst_caps_append_structure (ret, GST_STRUCTURE (results->data));
2171     results = g_list_delete_link (results, results);
2172   }
2173
2174   if (gst_caps_is_empty (ret))
2175     goto enum_framesizes_no_results;
2176
2177   return ret;
2178
2179   /* ERRORS */
2180 enum_framesizes_failed:
2181   {
2182     /* I don't see how this is actually an error */
2183     GST_DEBUG_OBJECT (v4l2object->element,
2184         "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2185         " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2186     goto default_frame_sizes;
2187   }
2188 enum_framesizes_no_results:
2189   {
2190     /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2191      * question doesn't actually support it yet */
2192     GST_DEBUG_OBJECT (v4l2object->element,
2193         "No results for pixelformat %" GST_FOURCC_FORMAT
2194         " enumerating frame sizes, trying fallback",
2195         GST_FOURCC_ARGS (pixelformat));
2196     goto default_frame_sizes;
2197   }
2198 unknown_type:
2199   {
2200     GST_WARNING_OBJECT (v4l2object->element,
2201         "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2202         ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2203     goto default_frame_sizes;
2204   }
2205 default_frame_sizes:
2206 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
2207   {
2208     gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2209     gboolean interlaced;
2210
2211     /* This code is for Linux < 2.6.19 */
2212     min_w = min_h = 1;
2213     max_w = max_h = GST_V4L2_MAX_SIZE;
2214     if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2215             &min_h, &interlaced)) {
2216       GST_WARNING_OBJECT (v4l2object->element,
2217           "Could not probe minimum capture size for pixelformat %"
2218           GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2219     }
2220     if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2221             &max_h, &interlaced)) {
2222       GST_WARNING_OBJECT (v4l2object->element,
2223           "Could not probe maximum capture size for pixelformat %"
2224           GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2225     }
2226
2227     /* Since we can't get framerate directly, try to use the current norm */
2228     if (v4l2object->tv_norm && v4l2object->norms) {
2229       GList *norms;
2230       GstTunerNorm *norm = NULL;
2231       GstTunerNorm *current =
2232           gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2233
2234       for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2235         norm = (GstTunerNorm *) norms->data;
2236         if (!strcmp (norm->label, current->label))
2237           break;
2238       }
2239       /* If it's possible, set framerate to that (discrete) value */
2240       if (norm) {
2241         fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2242         fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2243       }
2244     }
2245
2246     tmp = gst_structure_copy (template);
2247     if (fix_num) {
2248       gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2249           fix_denom, NULL);
2250     } else {
2251       /* if norm can't be used, copy the template framerate */
2252       gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2253           100, 1, NULL);
2254     }
2255
2256     if (min_w == max_w)
2257       gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2258     else
2259       gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2260
2261     if (min_h == max_h)
2262       gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2263     else
2264       gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2265
2266     if (g_str_equal (gst_structure_get_name (tmp), "video/x-raw"))
2267       gst_structure_set (tmp, "interlace-mode", G_TYPE_STRING,
2268           (interlaced ? "mixed" : "progressive"), NULL);
2269     gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2270
2271     gst_caps_append_structure (ret, tmp);
2272
2273     return ret;
2274   }
2275 }
2276
2277 static gboolean
2278 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2279     guint32 pixelformat, gint * width, gint * height, gboolean * interlaced)
2280 {
2281   struct v4l2_format fmt, prevfmt;
2282   int fd;
2283   int r;
2284   int prevfmt_valid;
2285   gboolean ret = FALSE;
2286
2287   g_return_val_if_fail (width != NULL, FALSE);
2288   g_return_val_if_fail (height != NULL, FALSE);
2289
2290   GST_LOG_OBJECT (v4l2object->element,
2291       "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2292       *width, *height, GST_FOURCC_ARGS (pixelformat));
2293
2294   fd = v4l2object->video_fd;
2295
2296   /* Some drivers are buggy and will modify the currently set format
2297      when processing VIDIOC_TRY_FMT, so we remember what is set at the
2298      minute, and will reset it when done. */
2299   prevfmt.type = v4l2object->type;
2300   prevfmt_valid = (v4l2_ioctl (fd, VIDIOC_G_FMT, &prevfmt) >= 0);
2301
2302   /* get size delimiters */
2303   memset (&fmt, 0, sizeof (fmt));
2304   fmt.type = v4l2object->type;
2305   fmt.fmt.pix.width = *width;
2306   fmt.fmt.pix.height = *height;
2307   fmt.fmt.pix.pixelformat = pixelformat;
2308   fmt.fmt.pix.field = V4L2_FIELD_NONE;
2309
2310   r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2311   if (r < 0 && errno == EINVAL) {
2312     /* try again with interlaced video */
2313     fmt.fmt.pix.width = *width;
2314     fmt.fmt.pix.height = *height;
2315     fmt.fmt.pix.pixelformat = pixelformat;
2316     fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2317     r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2318   }
2319
2320   if (r < 0) {
2321     /* The driver might not implement TRY_FMT, in which case we will try
2322        S_FMT to probe */
2323     if (errno != ENOTTY)
2324       goto error;
2325
2326     /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
2327        be, because we're still probing */
2328     if (GST_V4L2_IS_ACTIVE (v4l2object))
2329       goto error;
2330
2331     GST_LOG_OBJECT (v4l2object->element,
2332         "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
2333
2334     fmt.fmt.pix.width = *width;
2335     fmt.fmt.pix.height = *height;
2336
2337     r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2338     if (r < 0 && errno == EINVAL) {
2339       /* try again with progressive video */
2340       fmt.fmt.pix.width = *width;
2341       fmt.fmt.pix.height = *height;
2342       fmt.fmt.pix.pixelformat = pixelformat;
2343       fmt.fmt.pix.field = V4L2_FIELD_NONE;
2344       r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2345     }
2346
2347     if (r < 0)
2348       goto error;
2349   }
2350
2351   GST_LOG_OBJECT (v4l2object->element,
2352       "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2353
2354   *width = fmt.fmt.pix.width;
2355   *height = fmt.fmt.pix.height;
2356
2357   switch (fmt.fmt.pix.field) {
2358     case V4L2_FIELD_ANY:
2359     case V4L2_FIELD_NONE:
2360       *interlaced = FALSE;
2361       break;
2362     case V4L2_FIELD_INTERLACED:
2363     case V4L2_FIELD_INTERLACED_TB:
2364     case V4L2_FIELD_INTERLACED_BT:
2365       *interlaced = TRUE;
2366       break;
2367     default:
2368       GST_WARNING_OBJECT (v4l2object->element,
2369           "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u",
2370           GST_FOURCC_ARGS (pixelformat), *width, *height);
2371       goto error;
2372   }
2373
2374   ret = TRUE;
2375
2376 error:
2377   if (!ret) {
2378     GST_WARNING_OBJECT (v4l2object->element,
2379         "Unable to try format: %s", g_strerror (errno));
2380   }
2381   if (prevfmt_valid)
2382     if (v4l2_ioctl (fd, VIDIOC_S_FMT, &prevfmt) < 0) {
2383       GST_WARNING_OBJECT (v4l2object->element,
2384           "Unable to restore format after trying format: %s",
2385           g_strerror (errno));
2386     }
2387
2388   return ret;
2389 }
2390
2391 static gboolean
2392 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2393 {
2394   GstV4l2IOMode mode;
2395
2396   GST_DEBUG_OBJECT (v4l2object->element, "initializing the capture system");
2397
2398   GST_V4L2_CHECK_OPEN (v4l2object);
2399   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2400
2401   /* find transport */
2402   mode = v4l2object->req_mode;
2403
2404   if (v4l2object->vcap.capabilities & V4L2_CAP_READWRITE) {
2405     if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2406       mode = GST_V4L2_IO_RW;
2407   } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2408     goto method_not_supported;
2409
2410   if (v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
2411     if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2412       mode = GST_V4L2_IO_MMAP;
2413   } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2414     goto method_not_supported;
2415
2416   /* if still no transport selected, error out */
2417   if (mode == GST_V4L2_IO_AUTO)
2418     goto no_supported_capture_method;
2419
2420   GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2421   v4l2object->mode = mode;
2422
2423   /* Map the buffers */
2424   GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2425
2426   if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2427     goto buffer_pool_new_failed;
2428
2429   GST_V4L2_SET_ACTIVE (v4l2object);
2430
2431   return TRUE;
2432
2433   /* ERRORS */
2434 buffer_pool_new_failed:
2435   {
2436     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2437         (_("Could not map buffers from device '%s'"),
2438             v4l2object->videodev),
2439         ("Failed to create buffer pool: %s", g_strerror (errno)));
2440     return FALSE;
2441   }
2442 method_not_supported:
2443   {
2444     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2445         (_("The driver of device '%s' does not support the IO method %d"),
2446             v4l2object->videodev, mode), (NULL));
2447     return FALSE;
2448   }
2449 no_supported_capture_method:
2450   {
2451     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2452         (_("The driver of device '%s' does not support any known IO "
2453                 "method."), v4l2object->videodev), (NULL));
2454     return FALSE;
2455   }
2456 }
2457
2458 gboolean
2459 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps)
2460 {
2461   gint fd = v4l2object->video_fd;
2462   struct v4l2_format format;
2463   struct v4l2_streamparm streamparm;
2464   enum v4l2_field field;
2465   guint32 pixelformat;
2466   struct v4l2_fmtdesc *fmtdesc;
2467   GstVideoInfo info;
2468   gint width, height, fps_n, fps_d, stride;
2469   gint i = 0;
2470
2471   if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
2472     /* gst does not distinguish GST_VIDEO_FORMAT_NV21 than GST_VIDEO_FORMAT_NV21M so
2473      * we have to check it blindly
2474      */
2475     if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info)) {
2476       /* for example if the device supports NV21 and not NV21M, let's try with
2477        * the non prefered one if we initially prefered_non_contiguous to TRUE
2478        * in this case.
2479        */
2480       GST_DEBUG_OBJECT (v4l2object->element,
2481           "prefered multiplanar does not exist, try the other one");
2482
2483       v4l2object->prefered_non_contiguous =
2484           !v4l2object->prefered_non_contiguous;
2485
2486       if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
2487         goto invalid_caps;
2488     }
2489   } else {
2490     if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
2491       goto invalid_caps;
2492   }
2493
2494   pixelformat = fmtdesc->pixelformat;
2495   width = GST_VIDEO_INFO_WIDTH (&info);
2496   height = GST_VIDEO_INFO_HEIGHT (&info);
2497   fps_n = GST_VIDEO_INFO_FPS_N (&info);
2498   fps_d = GST_VIDEO_INFO_FPS_D (&info);
2499   stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
2500
2501   /* get bytesperline for each plane */
2502   for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&info); i++)
2503     v4l2object->bytesperline[i] = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
2504
2505   if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
2506     GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
2507     /* ideally we would differentiate between types of interlaced video
2508      * but there is not sufficient information in the caps..
2509      */
2510     field = V4L2_FIELD_INTERLACED;
2511   } else {
2512     GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
2513     field = V4L2_FIELD_NONE;
2514   }
2515
2516   GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
2517       "%" GST_FOURCC_FORMAT " stride: %d", width, height,
2518       GST_FOURCC_ARGS (pixelformat), stride);
2519
2520   GST_V4L2_CHECK_OPEN (v4l2object);
2521   GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2522
2523   /* MPEG-TS source cameras don't get their format set for some reason.
2524    * It looks wrong and we weren't able to track down the reason for that code
2525    * so it is disabled until someone who has an mpeg-ts camera complains...
2526    */
2527 #if 0
2528   /* Only unconditionally accept mpegts for sources */
2529   if ((v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
2530       (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')))
2531     goto done;
2532 #endif
2533
2534   memset (&format, 0x00, sizeof (struct v4l2_format));
2535   format.type = v4l2object->type;
2536
2537   if (v4l2_ioctl (fd, VIDIOC_G_FMT, &format) < 0)
2538     goto get_fmt_failed;
2539
2540   if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
2541     /* even in v4l2 multiplanar mode we can work in contiguous mode
2542      * if the device supports it */
2543     gint n_v4l_planes =
2544         v4l2object->prefered_non_contiguous ? GST_VIDEO_INFO_N_PLANES (&info) :
2545         1;
2546
2547     GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2548         "%" GST_FOURCC_FORMAT " colorspace %d, nb planes %d",
2549         format.fmt.pix_mp.width, format.fmt.pix_mp.height,
2550         GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2551         format.fmt.pix_mp.colorspace, format.fmt.pix_mp.num_planes);
2552
2553     if (format.type != v4l2object->type ||
2554         format.fmt.pix_mp.width != width ||
2555         format.fmt.pix_mp.height != height ||
2556         format.fmt.pix_mp.pixelformat != pixelformat ||
2557         format.fmt.pix_mp.field != field ||
2558         format.fmt.pix_mp.num_planes != n_v4l_planes) {
2559       /* something different, set the format */
2560       GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2561           "%" GST_FOURCC_FORMAT, width, height, GST_FOURCC_ARGS (pixelformat));
2562
2563       format.type = v4l2object->type;
2564       format.fmt.pix_mp.pixelformat = pixelformat;
2565       format.fmt.pix_mp.width = width;
2566       format.fmt.pix_mp.height = height;
2567       format.fmt.pix_mp.field = field;
2568       format.fmt.pix_mp.num_planes = n_v4l_planes;
2569       /* try to ask our prefered stride but it's not a failure
2570        * if not accepted */
2571       for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
2572         format.fmt.pix_mp.plane_fmt[i].bytesperline =
2573             v4l2object->bytesperline[i];
2574
2575       if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
2576         goto set_fmt_failed;
2577
2578       GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2579           "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
2580           format.fmt.pix_mp.height,
2581           GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2582           format.fmt.pix_mp.num_planes);
2583
2584 #ifndef GST_DISABLE_GST_DEBUG
2585       for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
2586         GST_DEBUG_OBJECT (v4l2object->element, "  stride %d",
2587             format.fmt.pix_mp.plane_fmt[i].bytesperline);
2588 #endif
2589
2590       if (format.fmt.pix_mp.width != width
2591           || format.fmt.pix_mp.height != height)
2592         goto invalid_dimensions;
2593
2594       if (format.fmt.pix_mp.pixelformat != pixelformat)
2595         goto invalid_pixelformat;
2596
2597       if (format.fmt.pix_mp.num_planes != n_v4l_planes)
2598         goto invalid_planes;
2599     }
2600
2601     /* figure out the frame layout */
2602     v4l2object->n_v4l2_planes = format.fmt.pix_mp.num_planes;
2603     v4l2object->sizeimage = 0;
2604     for (i = 0; i < format.fmt.pix_mp.num_planes; i++) {
2605       /* For compatibility reasons with the non-v4l2-multiplanar mode
2606        * we have to use the bytesperline of the first v4l plane
2607        * See plane_fmt[0] instead of plane_fmt[i] in next line */
2608       v4l2object->bytesperline[i] = format.fmt.pix_mp.plane_fmt[0].bytesperline;
2609       v4l2object->sizeimage += format.fmt.pix_mp.plane_fmt[i].sizeimage;
2610     }
2611   } else {
2612     GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2613         "%" GST_FOURCC_FORMAT " bytesperline %d, colorspace %d",
2614         format.fmt.pix.width, format.fmt.pix.height,
2615         GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2616         format.fmt.pix.bytesperline, format.fmt.pix.colorspace);
2617
2618     if (format.type != v4l2object->type ||
2619         format.fmt.pix.width != width ||
2620         format.fmt.pix.height != height ||
2621         format.fmt.pix.pixelformat != pixelformat ||
2622         format.fmt.pix.field != field
2623         || format.fmt.pix.bytesperline != stride) {
2624       /* something different, set the format */
2625       GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %dx%d, format "
2626           "%" GST_FOURCC_FORMAT " bytesperline %d", width, height,
2627           GST_FOURCC_ARGS (pixelformat), stride);
2628
2629       format.type = v4l2object->type;
2630       format.fmt.pix.width = width;
2631       format.fmt.pix.height = height;
2632       format.fmt.pix.pixelformat = pixelformat;
2633       format.fmt.pix.field = field;
2634       /* try to ask our prefered stride */
2635       format.fmt.pix.bytesperline = stride;
2636
2637       if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
2638         goto set_fmt_failed;
2639
2640       GST_DEBUG_OBJECT (v4l2object->element, "Got format to %dx%d, format "
2641           "%" GST_FOURCC_FORMAT " stride %d", format.fmt.pix.width,
2642           format.fmt.pix.height, GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
2643           format.fmt.pix.bytesperline);
2644
2645       if (format.fmt.pix.width != width || format.fmt.pix.height != height)
2646         goto invalid_dimensions;
2647
2648       if (format.fmt.pix.pixelformat != pixelformat)
2649         goto invalid_pixelformat;
2650     }
2651
2652     /* only one plane in non-MPLANE mode */
2653     v4l2object->n_v4l2_planes = 1;
2654
2655     /* figure out the frame layout */
2656     for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
2657       /* In non-multiplanar mode, there is only one field for bytesperline
2658        * Just set it everywhere in order to the code factorized
2659        * with multiplanar case in gstv4l2bufferpool.c::alloc_buffer function
2660        */
2661       v4l2object->bytesperline[i] = format.fmt.pix.bytesperline;
2662     }
2663
2664     v4l2object->sizeimage = format.fmt.pix.sizeimage;
2665   }
2666
2667   GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %u",
2668       v4l2object->sizeimage);
2669
2670   /* Is there a reason we require the caller to always specify a framerate? */
2671   GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
2672       fps_d);
2673
2674   memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
2675   streamparm.type = v4l2object->type;
2676
2677   if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
2678     goto get_parm_failed;
2679
2680   GST_VIDEO_INFO_FPS_N (&info) =
2681       streamparm.parm.capture.timeperframe.denominator;
2682   GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
2683
2684   if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
2685       || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2686     GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
2687         streamparm.parm.capture.timeperframe.denominator,
2688         streamparm.parm.capture.timeperframe.numerator);
2689
2690     /* We used to skip frame rate setup if the camera was already setup
2691      * with the requested frame rate. This breaks some cameras though,
2692      * causing them to not output data (several models of Thinkpad cameras
2693      * have this problem at least).
2694      * So, don't skip. */
2695     GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
2696         fps_d);
2697     /* We want to change the frame rate, so check whether we can. Some cheap USB
2698      * cameras don't have the capability */
2699     if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
2700       GST_DEBUG_OBJECT (v4l2object->element,
2701           "Not setting framerate (not supported)");
2702       goto done;
2703     }
2704
2705     /* Note: V4L2 wants the frame interval, we have the frame rate */
2706     streamparm.parm.capture.timeperframe.numerator = fps_d;
2707     streamparm.parm.capture.timeperframe.denominator = fps_n;
2708
2709     /* some cheap USB cam's won't accept any change */
2710     if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
2711       goto set_parm_failed;
2712
2713     /* get new values */
2714     fps_d = streamparm.parm.capture.timeperframe.numerator;
2715     fps_n = streamparm.parm.capture.timeperframe.denominator;
2716
2717     GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
2718         fps_d);
2719
2720     GST_VIDEO_INFO_FPS_N (&info) = fps_n;
2721     GST_VIDEO_INFO_FPS_D (&info) = fps_d;
2722   }
2723
2724 done:
2725   /* if we have a framerate pre-calculate duration */
2726   if (fps_n > 0 && fps_d > 0) {
2727     v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
2728   } else {
2729     v4l2object->duration = GST_CLOCK_TIME_NONE;
2730   }
2731   v4l2object->info = info;
2732   v4l2object->fmtdesc = fmtdesc;
2733
2734   /* now configure ther pools */
2735   if (!gst_v4l2_object_setup_pool (v4l2object, caps))
2736     goto pool_failed;
2737
2738   return TRUE;
2739
2740   /* ERRORS */
2741 invalid_caps:
2742   {
2743     GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
2744         caps);
2745     return FALSE;
2746   }
2747 get_fmt_failed:
2748   {
2749     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2750         (_("Device '%s' does not support video capture"),
2751             v4l2object->videodev),
2752         ("Call to G_FMT failed: (%s)", g_strerror (errno)));
2753     return FALSE;
2754   }
2755 set_fmt_failed:
2756   {
2757     if (errno == EBUSY) {
2758       GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, BUSY,
2759           (_("Device '%s' is busy"), v4l2object->videodev),
2760           ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2761               GST_FOURCC_ARGS (pixelformat), width, height,
2762               g_strerror (errno)));
2763     } else {
2764       GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2765           (_("Device '%s' cannot capture at %dx%d"),
2766               v4l2object->videodev, width, height),
2767           ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
2768               GST_FOURCC_ARGS (pixelformat), width, height,
2769               g_strerror (errno)));
2770     }
2771     return FALSE;
2772   }
2773 invalid_dimensions:
2774   {
2775     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2776         (_("Device '%s' cannot capture at %dx%d"),
2777             v4l2object->videodev, width, height),
2778         ("Tried to capture at %dx%d, but device returned size %dx%d",
2779             width, height, format.fmt.pix.width, format.fmt.pix.height));
2780     return FALSE;
2781   }
2782 invalid_pixelformat:
2783   {
2784     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2785         (_("Device '%s' cannot capture in the specified format"),
2786             v4l2object->videodev),
2787         ("Tried to capture in %" GST_FOURCC_FORMAT
2788             ", but device returned format" " %" GST_FOURCC_FORMAT,
2789             GST_FOURCC_ARGS (pixelformat),
2790             GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
2791     return FALSE;
2792   }
2793 invalid_planes:
2794   {
2795     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2796         (_("Device '%s' does support non-contiguous planes"),
2797             v4l2object->videodev),
2798         ("Device wants %d planes", format.fmt.pix_mp.num_planes));
2799     return FALSE;
2800   }
2801 get_parm_failed:
2802   {
2803     /* it's possible that this call is not supported */
2804     if (errno != EINVAL && errno != ENOTTY) {
2805       GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2806           (_("Could not get parameters on device '%s'"),
2807               v4l2object->videodev), GST_ERROR_SYSTEM);
2808     }
2809     goto done;
2810   }
2811 set_parm_failed:
2812   {
2813     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
2814         (_("Video device did not accept new frame rate setting.")),
2815         GST_ERROR_SYSTEM);
2816     goto done;
2817   }
2818 pool_failed:
2819   {
2820     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
2821         (_("Video device could not create buffer pool.")), GST_ERROR_SYSTEM);
2822     return FALSE;
2823   }
2824 }
2825
2826 gboolean
2827 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
2828 {
2829   GstStructure *s;
2830   GstCaps *oldcaps;
2831
2832   if (!v4l2object->pool)
2833     return FALSE;
2834
2835   s = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (v4l2object->pool));
2836   gst_buffer_pool_config_get_params (s, &oldcaps, NULL, NULL, NULL);
2837
2838   return oldcaps && gst_caps_is_equal (caps, oldcaps);
2839 }
2840
2841 gboolean
2842 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
2843 {
2844   GST_LOG_OBJECT (v4l2object->element, "flush poll");
2845   gst_poll_set_flushing (v4l2object->poll, TRUE);
2846
2847   return TRUE;
2848 }
2849
2850 gboolean
2851 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
2852 {
2853   GST_LOG_OBJECT (v4l2object->element, "flush stop poll");
2854   gst_poll_set_flushing (v4l2object->poll, FALSE);
2855
2856   return TRUE;
2857 }
2858
2859 gboolean
2860 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
2861 {
2862   GST_DEBUG_OBJECT (v4l2object->element, "stopping");
2863
2864   if (!GST_V4L2_IS_OPEN (v4l2object))
2865     goto done;
2866   if (!GST_V4L2_IS_ACTIVE (v4l2object))
2867     goto done;
2868
2869   if (v4l2object->pool) {
2870     GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
2871     gst_buffer_pool_set_active (GST_BUFFER_POOL_CAST (v4l2object->pool), FALSE);
2872     gst_object_unref (v4l2object->pool);
2873     v4l2object->pool = NULL;
2874   }
2875
2876   GST_V4L2_SET_INACTIVE (v4l2object);
2877
2878 done:
2879   return TRUE;
2880 }
2881
2882 gboolean
2883 gst_v4l2_object_copy (GstV4l2Object * v4l2object, GstBuffer * dest,
2884     GstBuffer * src)
2885 {
2886   const GstVideoFormatInfo *finfo = v4l2object->info.finfo;
2887
2888   if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
2889           finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
2890     GstVideoFrame src_frame, dest_frame;
2891
2892     GST_DEBUG_OBJECT (v4l2object->element, "copy video frame");
2893
2894     /* we have raw video, use videoframe copy to get strides right */
2895     if (!gst_video_frame_map (&src_frame, &v4l2object->info, src, GST_MAP_READ))
2896       goto invalid_buffer;
2897
2898     if (!gst_video_frame_map (&dest_frame, &v4l2object->info, dest,
2899             GST_MAP_WRITE)) {
2900       gst_video_frame_unmap (&src_frame);
2901       goto invalid_buffer;
2902     }
2903
2904     gst_video_frame_copy (&dest_frame, &src_frame);
2905
2906     gst_video_frame_unmap (&src_frame);
2907     gst_video_frame_unmap (&dest_frame);
2908   } else {
2909     GstMapInfo map;
2910
2911     GST_DEBUG_OBJECT (v4l2object->element, "copy raw bytes");
2912     gst_buffer_map (src, &map, GST_MAP_READ);
2913     gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
2914     gst_buffer_unmap (src, &map);
2915     gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
2916   }
2917   GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, v4l2object->element,
2918       "slow copy into buffer %p", dest);
2919
2920   return TRUE;
2921
2922   /* ERRORS */
2923 invalid_buffer:
2924   {
2925     /* No Window available to put our image into */
2926     GST_WARNING_OBJECT (v4l2object->element, "could not map image");
2927     return FALSE;
2928   }
2929 }
2930
2931 GstCaps *
2932 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
2933 {
2934   GstCaps *ret;
2935   GSList *walk;
2936   GSList *formats;
2937
2938   if (v4l2object->probed_caps == NULL) {
2939     formats = gst_v4l2_object_get_format_list (v4l2object);
2940
2941     ret = gst_caps_new_empty ();
2942
2943     for (walk = formats; walk; walk = walk->next) {
2944       struct v4l2_fmtdesc *format;
2945       GstStructure *template;
2946
2947       format = (struct v4l2_fmtdesc *) walk->data;
2948
2949       template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
2950
2951       if (template) {
2952         GstCaps *tmp;
2953
2954         tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
2955             format->pixelformat, template);
2956         if (tmp)
2957           gst_caps_append (ret, tmp);
2958
2959         gst_structure_free (template);
2960       } else {
2961         GST_DEBUG_OBJECT (v4l2object->element, "unknown format %u",
2962             format->pixelformat);
2963       }
2964     }
2965     v4l2object->probed_caps = ret;
2966   }
2967
2968   if (filter) {
2969     ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
2970         GST_CAPS_INTERSECT_FIRST);
2971   } else {
2972     ret = gst_caps_ref (v4l2object->probed_caps);
2973   }
2974
2975   GST_INFO_OBJECT (v4l2object->element, "probed caps: %" GST_PTR_FORMAT, ret);
2976   LOG_CAPS (v4l2object->element, ret);
2977
2978   return ret;
2979 }