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