3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@gmail.com>
6 * gstv4l2object.c: base class for V4L2 elements
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Library General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. This library is distributed in the hope
12 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
30 #include <sys/ioctl.h>
34 #include <gudev/gudev.h>
37 #include "ext/videodev2.h"
38 #include "gstv4l2object.h"
39 #include "gstv4l2tuner.h"
40 #include "gstv4l2colorbalance.h"
42 #include "gst/gst-i18n-plugin.h"
44 #include <gst/video/video.h>
45 #include <gst/allocators/gstdmabuf.h>
47 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
48 #define GST_CAT_DEFAULT v4l2_debug
50 #define DEFAULT_PROP_DEVICE_NAME NULL
51 #define DEFAULT_PROP_DEVICE_FD -1
52 #define DEFAULT_PROP_FLAGS 0
53 #define DEFAULT_PROP_TV_NORM 0
54 #define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
56 #define ENCODED_BUFFER_SIZE (2 * 1024 * 1024)
61 V4L2_STD_OBJECT_PROPS,
65 * common format / caps utilities:
69 GST_V4L2_RAW = 1 << 0,
70 GST_V4L2_CODEC = 1 << 1,
71 GST_V4L2_TRANSPORT = 1 << 2,
72 GST_V4L2_NO_PARSE = 1 << 3,
80 GstV4L2FormatFlags flags;
83 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
85 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
86 {V4L2_PIX_FMT_ARGB555, TRUE, GST_V4L2_RAW},
87 {V4L2_PIX_FMT_XRGB555, TRUE, GST_V4L2_RAW},
88 {V4L2_PIX_FMT_ARGB555X, TRUE, GST_V4L2_RAW},
89 {V4L2_PIX_FMT_XRGB555X, TRUE, GST_V4L2_RAW},
90 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
91 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
92 {V4L2_PIX_FMT_BGR666, TRUE, GST_V4L2_RAW},
93 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
94 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
95 {V4L2_PIX_FMT_ABGR32, TRUE, GST_V4L2_RAW},
96 {V4L2_PIX_FMT_XBGR32, TRUE, GST_V4L2_RAW},
97 {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
98 {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},
100 /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
101 {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
102 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
103 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
104 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
105 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
108 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
109 {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
110 {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
111 {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
112 {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
113 {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
114 {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
115 {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},
117 /* Palette formats */
118 {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},
120 /* Chrominance formats */
121 {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},
123 /* Luminance+Chrominance formats */
124 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
125 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
126 {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
127 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
128 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
129 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
130 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
131 {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
132 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
133 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
134 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
135 {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
136 {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
137 {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
138 {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
139 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
140 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
141 {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
142 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
143 {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
144 {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
146 /* two planes -- one Y, one Cr + Cb interleaved */
147 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
148 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
149 {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
150 {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
151 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
152 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
153 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
154 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
155 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
156 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
158 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
160 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
161 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
162 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_CODEC},
163 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_CODEC},
164 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_CODEC},
166 /* compressed formats */
167 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
168 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
169 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
170 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
171 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
172 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
173 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
174 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
175 {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
176 {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
177 {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
178 {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
179 {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
180 {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
181 {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
182 {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
183 {V4L2_PIX_FMT_VP9, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
185 /* Vendor-specific formats */
186 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
187 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
188 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
189 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
192 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
194 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
197 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
199 gst_v4l2_device_get_type (void)
201 static GType v4l2_device_type = 0;
203 if (v4l2_device_type == 0) {
204 static const GFlagsValue values[] = {
205 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
206 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
207 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
209 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
210 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
212 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
213 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
219 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
222 return v4l2_device_type;
225 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
227 gst_v4l2_tv_norm_get_type (void)
229 static GType v4l2_tv_norm = 0;
232 static const GEnumValue tv_norms[] = {
235 {V4L2_STD_NTSC, "NTSC", "NTSC"},
236 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
237 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
238 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
239 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
241 {V4L2_STD_PAL, "PAL", "PAL"},
242 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
243 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
244 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
245 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
246 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
247 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
248 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
249 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
250 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
251 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
252 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
253 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
254 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
255 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
257 {V4L2_STD_SECAM, "SECAM", "SECAM"},
258 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
259 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
260 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
261 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
262 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
263 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
264 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
265 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
266 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
271 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
278 gst_v4l2_io_mode_get_type (void)
280 static GType v4l2_io_mode = 0;
283 static const GEnumValue io_modes[] = {
284 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
285 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
286 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
287 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
288 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
289 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
294 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
300 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
301 const char *default_device)
303 g_object_class_install_property (gobject_class, PROP_DEVICE,
304 g_param_spec_string ("device", "Device", "Device location",
305 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
307 g_param_spec_string ("device-name", "Device name",
308 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
309 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
310 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
311 g_param_spec_int ("device-fd", "File descriptor",
312 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
313 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
314 g_object_class_install_property (gobject_class, PROP_FLAGS,
315 g_param_spec_flags ("flags", "Flags", "Device type flags",
316 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
317 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
320 * GstV4l2Src:brightness:
322 * Picture brightness, or more precisely, the black level
324 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
325 g_param_spec_int ("brightness", "Brightness",
326 "Picture brightness, or more precisely, the black level", G_MININT,
328 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
330 * GstV4l2Src:contrast:
332 * Picture contrast or luma gain
334 g_object_class_install_property (gobject_class, PROP_CONTRAST,
335 g_param_spec_int ("contrast", "Contrast",
336 "Picture contrast or luma gain", G_MININT,
338 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
340 * GstV4l2Src:saturation:
342 * Picture color saturation or chroma gain
344 g_object_class_install_property (gobject_class, PROP_SATURATION,
345 g_param_spec_int ("saturation", "Saturation",
346 "Picture color saturation or chroma gain", G_MININT,
348 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
352 * Hue or color balance
354 g_object_class_install_property (gobject_class, PROP_HUE,
355 g_param_spec_int ("hue", "Hue",
356 "Hue or color balance", G_MININT,
358 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
365 g_object_class_install_property (gobject_class, PROP_TV_NORM,
366 g_param_spec_enum ("norm", "TV norm",
368 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
369 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372 * GstV4l2Src:io-mode:
376 g_object_class_install_property (gobject_class, PROP_IO_MODE,
377 g_param_spec_enum ("io-mode", "IO mode",
379 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
380 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 * GstV4l2Src:extra-controls:
385 * Additional v4l2 controls for the device. The controls are identified
386 * by the control name (lowercase with '_' for any non-alphanumeric
391 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
392 g_param_spec_boxed ("extra-controls", "Extra Controls",
393 "Extra v4l2 controls (CIDs) for the device",
394 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
397 * GstV4l2Src:pixel-aspect-ratio:
399 * The pixel aspect ratio of the device. This overwrites the pixel aspect
400 * ratio queried from the device.
404 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
405 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
406 "Overwrite the pixel aspect ratio of the device", "1/1",
407 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
410 * GstV4l2Src:force-aspect-ratio:
412 * When enabled, the pixel aspect ratio queried from the device or set
413 * with the pixel-aspect-ratio property will be enforced.
417 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
418 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
419 "When enabled, the pixel aspect ratio will be enforced", TRUE,
420 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
425 gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
427 g_object_class_install_property (gobject_class, PROP_DEVICE,
428 g_param_spec_string ("device", "Device", "Device location",
429 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
431 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
432 g_param_spec_string ("device-name", "Device name",
433 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
434 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
436 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
437 g_param_spec_int ("device-fd", "File descriptor",
438 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
439 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
441 g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
442 g_param_spec_enum ("output-io-mode", "Output IO mode",
443 "Output side I/O mode (matches sink pad)",
444 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
445 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
447 g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
448 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
449 "Capture I/O mode (matches src pad)",
450 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
451 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
453 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
454 g_param_spec_boxed ("extra-controls", "Extra Controls",
455 "Extra v4l2 controls (CIDs) for the device",
456 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
459 /* Support for 32bit off_t, this wrapper is casting off_t to gint64 */
464 v4l2_mmap_wrapper (gpointer start, gsize length, gint prot, gint flags, gint fd,
467 return v4l2_mmap (start, length, prot, flags, fd, (gint64) offset);
470 #define v4l2_mmap v4l2_mmap_wrapper
472 #endif /* SIZEOF_OFF_T < 8 */
473 #endif /* HAVE_LIBV4L2 */
476 gst_v4l2_object_new (GstElement * element,
477 GstObject * debug_object,
478 enum v4l2_buf_type type,
479 const char *default_device,
480 GstV4l2GetInOutFunction get_in_out_func,
481 GstV4l2SetInOutFunction set_in_out_func,
482 GstV4l2UpdateFpsFunction update_fps_func)
484 GstV4l2Object *v4l2object;
487 * some default values
489 v4l2object = g_new0 (GstV4l2Object, 1);
491 v4l2object->type = type;
492 v4l2object->formats = NULL;
494 v4l2object->element = element;
495 v4l2object->dbg_obj = debug_object;
496 v4l2object->get_in_out_func = get_in_out_func;
497 v4l2object->set_in_out_func = set_in_out_func;
498 v4l2object->update_fps_func = update_fps_func;
500 v4l2object->video_fd = -1;
501 v4l2object->active = FALSE;
502 v4l2object->videodev = g_strdup (default_device);
504 v4l2object->norms = NULL;
505 v4l2object->channels = NULL;
506 v4l2object->colors = NULL;
508 v4l2object->keep_aspect = TRUE;
510 v4l2object->n_v4l2_planes = 0;
512 v4l2object->no_initial_format = FALSE;
514 /* We now disable libv4l2 by default, but have an env to enable it. */
516 if (g_getenv ("GST_V4L2_USE_LIBV4L2")) {
517 v4l2object->fd_open = v4l2_fd_open;
518 v4l2object->close = v4l2_close;
519 v4l2object->dup = v4l2_dup;
520 v4l2object->ioctl = v4l2_ioctl;
521 v4l2object->read = v4l2_read;
522 v4l2object->mmap = v4l2_mmap;
523 v4l2object->munmap = v4l2_munmap;
527 v4l2object->fd_open = NULL;
528 v4l2object->close = close;
529 v4l2object->dup = dup;
530 v4l2object->ioctl = ioctl;
531 v4l2object->read = read;
532 v4l2object->mmap = mmap;
533 v4l2object->munmap = munmap;
539 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
543 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
545 g_return_if_fail (v4l2object != NULL);
547 g_free (v4l2object->videodev);
549 g_free (v4l2object->channel);
551 if (v4l2object->formats) {
552 gst_v4l2_object_clear_format_list (v4l2object);
555 if (v4l2object->probed_caps) {
556 gst_caps_unref (v4l2object->probed_caps);
559 if (v4l2object->extra_controls) {
560 gst_structure_free (v4l2object->extra_controls);
568 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
570 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
571 g_slist_free (v4l2object->formats);
572 v4l2object->formats = NULL;
578 gst_v4l2_object_prop_to_cid (guint prop_id)
583 case PROP_BRIGHTNESS:
584 cid = V4L2_CID_BRIGHTNESS;
587 cid = V4L2_CID_CONTRAST;
589 case PROP_SATURATION:
590 cid = V4L2_CID_SATURATION;
596 GST_WARNING ("unmapped property id: %d", prop_id);
603 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
604 guint prop_id, const GValue * value, GParamSpec * pspec)
608 g_free (v4l2object->videodev);
609 v4l2object->videodev = g_value_dup_string (value);
611 case PROP_BRIGHTNESS:
613 case PROP_SATURATION:
616 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
619 if (GST_V4L2_IS_OPEN (v4l2object)) {
620 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
627 v4l2object->tv_norm = g_value_get_enum (value);
631 if (GST_V4L2_IS_OPEN (v4l2object)) {
632 GstTuner *tuner = GST_TUNER (v4l2object->element);
633 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
634 (gchar *) g_value_get_string (value));
637 /* like gst_tuner_set_channel (tuner, channel)
638 without g_object_notify */
639 gst_v4l2_tuner_set_channel (v4l2object, channel);
642 g_free (v4l2object->channel);
643 v4l2object->channel = g_value_dup_string (value);
647 if (GST_V4L2_IS_OPEN (v4l2object)) {
648 GstTuner *tuner = GST_TUNER (v4l2object->element);
649 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
652 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
654 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
655 without g_object_notify */
656 gst_v4l2_tuner_set_frequency (v4l2object, channel,
657 g_value_get_ulong (value));
660 v4l2object->frequency = g_value_get_ulong (value);
666 v4l2object->req_mode = g_value_get_enum (value);
668 case PROP_CAPTURE_IO_MODE:
669 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
670 v4l2object->req_mode = g_value_get_enum (value);
672 case PROP_OUTPUT_IO_MODE:
673 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
674 v4l2object->req_mode = g_value_get_enum (value);
676 case PROP_EXTRA_CONTROLS:{
677 const GstStructure *s = gst_value_get_structure (value);
679 if (v4l2object->extra_controls)
680 gst_structure_free (v4l2object->extra_controls);
682 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
683 if (GST_V4L2_IS_OPEN (v4l2object))
684 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
687 case PROP_PIXEL_ASPECT_RATIO:
688 if (v4l2object->par) {
689 g_value_unset (v4l2object->par);
690 g_free (v4l2object->par);
692 v4l2object->par = g_new0 (GValue, 1);
693 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
694 if (!g_value_transform (value, v4l2object->par)) {
695 g_warning ("Could not transform string to aspect ratio");
696 gst_value_set_fraction (v4l2object->par, 1, 1);
699 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "set PAR to %d/%d",
700 gst_value_get_fraction_numerator (v4l2object->par),
701 gst_value_get_fraction_denominator (v4l2object->par));
703 case PROP_FORCE_ASPECT_RATIO:
704 v4l2object->keep_aspect = g_value_get_boolean (value);
715 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
716 guint prop_id, GValue * value, GParamSpec * pspec)
720 g_value_set_string (value, v4l2object->videodev);
722 case PROP_DEVICE_NAME:
724 const guchar *name = NULL;
726 if (GST_V4L2_IS_OPEN (v4l2object))
727 name = v4l2object->vcap.card;
729 g_value_set_string (value, (gchar *) name);
734 if (GST_V4L2_IS_OPEN (v4l2object))
735 g_value_set_int (value, v4l2object->video_fd);
737 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
744 if (GST_V4L2_IS_OPEN (v4l2object)) {
745 flags |= v4l2object->device_caps &
746 (V4L2_CAP_VIDEO_CAPTURE |
747 V4L2_CAP_VIDEO_OUTPUT |
748 V4L2_CAP_VIDEO_OVERLAY |
749 V4L2_CAP_VBI_CAPTURE |
750 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
752 if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
753 flags |= V4L2_CAP_VIDEO_CAPTURE;
755 if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
756 flags |= V4L2_CAP_VIDEO_OUTPUT;
758 g_value_set_flags (value, flags);
761 case PROP_BRIGHTNESS:
763 case PROP_SATURATION:
766 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
769 if (GST_V4L2_IS_OPEN (v4l2object)) {
771 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
772 g_value_set_int (value, v);
780 g_value_set_enum (value, v4l2object->tv_norm);
783 g_value_set_enum (value, v4l2object->req_mode);
785 case PROP_CAPTURE_IO_MODE:
786 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
787 g_value_set_enum (value, v4l2object->req_mode);
789 case PROP_OUTPUT_IO_MODE:
790 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
791 g_value_set_enum (value, v4l2object->req_mode);
793 case PROP_EXTRA_CONTROLS:
794 gst_value_set_structure (value, v4l2object->extra_controls);
796 case PROP_PIXEL_ASPECT_RATIO:
798 g_value_transform (v4l2object->par, value);
800 case PROP_FORCE_ASPECT_RATIO:
801 g_value_set_boolean (value, v4l2object->keep_aspect);
811 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
813 struct v4l2_control control = { 0, };
815 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
817 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
818 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
820 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
822 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
823 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
824 "driver requires a minimum of %d buffers", control.value);
825 v4l2object->min_buffers = control.value;
827 v4l2object->min_buffers = 0;
832 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
834 GstTunerNorm *norm = NULL;
835 GstTunerChannel *channel = NULL;
838 if (!GST_IS_TUNER (v4l2object->element))
841 tuner = GST_TUNER (v4l2object->element);
843 if (v4l2object->tv_norm)
844 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
845 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
846 "norm=%p", (guint64) v4l2object->tv_norm, norm);
848 gst_tuner_set_norm (tuner, norm);
851 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
853 v4l2object->tv_norm =
854 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
855 gst_tuner_norm_changed (tuner, norm);
859 if (v4l2object->channel)
860 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
862 gst_tuner_set_channel (tuner, channel);
865 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
866 (v4l2object->element)));
868 g_free (v4l2object->channel);
869 v4l2object->channel = g_strdup (channel->label);
870 gst_tuner_channel_changed (tuner, channel);
875 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
876 if (v4l2object->frequency != 0) {
877 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
879 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
880 if (v4l2object->frequency == 0) {
882 gst_tuner_set_frequency (tuner, channel, 1000);
890 gst_v4l2_object_open (GstV4l2Object * v4l2object)
892 if (gst_v4l2_open (v4l2object))
893 gst_v4l2_set_defaults (v4l2object);
901 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
905 ret = gst_v4l2_dup (v4l2object, other);
911 gst_v4l2_object_close (GstV4l2Object * v4l2object)
913 if (!gst_v4l2_close (v4l2object))
916 gst_caps_replace (&v4l2object->probed_caps, NULL);
918 /* reset our copy of the device caps */
919 v4l2object->device_caps = 0;
921 if (v4l2object->formats) {
922 gst_v4l2_object_clear_format_list (v4l2object);
925 if (v4l2object->par) {
926 g_value_unset (v4l2object->par);
927 g_free (v4l2object->par);
928 v4l2object->par = NULL;
931 if (v4l2object->channel) {
932 g_free (v4l2object->channel);
933 v4l2object->channel = NULL;
939 static struct v4l2_fmtdesc *
940 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
943 struct v4l2_fmtdesc *fmt;
949 walk = gst_v4l2_object_get_format_list (v4l2object);
951 fmt = (struct v4l2_fmtdesc *) walk->data;
952 if (fmt->pixelformat == fourcc)
954 /* special case for jpeg */
955 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
956 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
957 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
958 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
959 fourcc == V4L2_PIX_FMT_PJPG) {
963 walk = g_slist_next (walk);
971 /* complete made up ranking, the values themselves are meaningless */
972 /* These ranks MUST be X such that X<<15 fits on a signed int - see
973 the comment at the end of gst_v4l2_object_format_get_rank. */
974 #define YUV_BASE_RANK 1000
975 #define JPEG_BASE_RANK 500
976 #define DV_BASE_RANK 200
977 #define RGB_BASE_RANK 100
978 #define YUV_ODD_BASE_RANK 50
979 #define RGB_ODD_BASE_RANK 25
980 #define BAYER_BASE_RANK 15
981 #define S910_BASE_RANK 10
982 #define GREY_BASE_RANK 5
983 #define PWC_BASE_RANK 1
986 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
988 guint32 fourcc = fmt->pixelformat;
989 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
993 case V4L2_PIX_FMT_MJPEG:
994 case V4L2_PIX_FMT_PJPG:
995 rank = JPEG_BASE_RANK;
997 case V4L2_PIX_FMT_JPEG:
998 rank = JPEG_BASE_RANK + 1;
1000 case V4L2_PIX_FMT_MPEG: /* MPEG */
1001 rank = JPEG_BASE_RANK + 2;
1004 case V4L2_PIX_FMT_RGB332:
1005 case V4L2_PIX_FMT_ARGB555:
1006 case V4L2_PIX_FMT_XRGB555:
1007 case V4L2_PIX_FMT_RGB555:
1008 case V4L2_PIX_FMT_ARGB555X:
1009 case V4L2_PIX_FMT_XRGB555X:
1010 case V4L2_PIX_FMT_RGB555X:
1011 case V4L2_PIX_FMT_BGR666:
1012 case V4L2_PIX_FMT_RGB565:
1013 case V4L2_PIX_FMT_RGB565X:
1014 case V4L2_PIX_FMT_RGB444:
1015 case V4L2_PIX_FMT_Y4:
1016 case V4L2_PIX_FMT_Y6:
1017 case V4L2_PIX_FMT_Y10:
1018 case V4L2_PIX_FMT_Y12:
1019 case V4L2_PIX_FMT_Y10BPACK:
1020 case V4L2_PIX_FMT_YUV555:
1021 case V4L2_PIX_FMT_YUV565:
1022 case V4L2_PIX_FMT_YUV32:
1023 case V4L2_PIX_FMT_NV12MT_16X16:
1024 case V4L2_PIX_FMT_NV42:
1025 case V4L2_PIX_FMT_H264_MVC:
1026 rank = RGB_ODD_BASE_RANK;
1029 case V4L2_PIX_FMT_RGB24:
1030 case V4L2_PIX_FMT_BGR24:
1031 rank = RGB_BASE_RANK - 1;
1034 case V4L2_PIX_FMT_RGB32:
1035 case V4L2_PIX_FMT_BGR32:
1036 case V4L2_PIX_FMT_ABGR32:
1037 case V4L2_PIX_FMT_XBGR32:
1038 case V4L2_PIX_FMT_ARGB32:
1039 case V4L2_PIX_FMT_XRGB32:
1040 rank = RGB_BASE_RANK;
1043 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1044 rank = GREY_BASE_RANK;
1047 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1048 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
1049 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
1050 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1051 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1052 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1053 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1054 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1055 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1056 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1057 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1058 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1059 rank = YUV_ODD_BASE_RANK;
1062 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1063 rank = YUV_BASE_RANK + 3;
1065 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1066 rank = YUV_BASE_RANK + 2;
1068 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1069 case V4L2_PIX_FMT_YUV420M:
1070 rank = YUV_BASE_RANK + 7;
1072 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1073 rank = YUV_BASE_RANK + 10;
1075 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1076 rank = YUV_BASE_RANK + 6;
1078 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1079 rank = YUV_BASE_RANK + 9;
1081 case V4L2_PIX_FMT_YUV444:
1082 rank = YUV_BASE_RANK + 6;
1084 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1085 rank = YUV_BASE_RANK + 5;
1087 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1088 rank = YUV_BASE_RANK + 4;
1090 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1091 rank = YUV_BASE_RANK + 8;
1094 case V4L2_PIX_FMT_DV:
1095 rank = DV_BASE_RANK;
1098 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1102 case V4L2_PIX_FMT_SBGGR8:
1103 case V4L2_PIX_FMT_SGBRG8:
1104 case V4L2_PIX_FMT_SGRBG8:
1105 case V4L2_PIX_FMT_SRGGB8:
1106 rank = BAYER_BASE_RANK;
1109 case V4L2_PIX_FMT_SN9C10X:
1110 rank = S910_BASE_RANK;
1113 case V4L2_PIX_FMT_PWC1:
1114 rank = PWC_BASE_RANK;
1116 case V4L2_PIX_FMT_PWC2:
1117 rank = PWC_BASE_RANK;
1125 /* All ranks are below 1<<15 so a shift by 15
1126 * will a) make all non-emulated formats larger
1127 * than emulated and b) will not overflow
1138 format_cmp_func (gconstpointer a, gconstpointer b)
1140 const struct v4l2_fmtdesc *fa = a;
1141 const struct v4l2_fmtdesc *fb = b;
1143 if (fa->pixelformat == fb->pixelformat)
1146 return gst_v4l2_object_format_get_rank (fb) -
1147 gst_v4l2_object_format_get_rank (fa);
1150 /******************************************************
1151 * gst_v4l2_object_fill_format_list():
1152 * create list of supported capture formats
1153 * return value: TRUE on success, FALSE on error
1154 ******************************************************/
1156 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1157 enum v4l2_buf_type type)
1160 struct v4l2_fmtdesc *format;
1162 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting src format enumerations");
1164 /* format enumeration */
1166 format = g_new0 (struct v4l2_fmtdesc, 1);
1169 format->type = type;
1171 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1172 if (errno == EINVAL) {
1174 break; /* end of enumeration */
1180 GST_LOG_OBJECT (v4l2object->dbg_obj, "index: %u", format->index);
1181 GST_LOG_OBJECT (v4l2object->dbg_obj, "type: %d", format->type);
1182 GST_LOG_OBJECT (v4l2object->dbg_obj, "flags: %08x", format->flags);
1183 GST_LOG_OBJECT (v4l2object->dbg_obj, "description: '%s'",
1184 format->description);
1185 GST_LOG_OBJECT (v4l2object->dbg_obj, "pixelformat: %" GST_FOURCC_FORMAT,
1186 GST_FOURCC_ARGS (format->pixelformat));
1188 /* sort formats according to our preference; we do this, because caps
1189 * are probed in the order the formats are in the list, and the order of
1190 * formats in the final probed caps matters for things like fixation */
1191 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1192 (GCompareFunc) format_cmp_func);
1195 #ifndef GST_DISABLE_GST_DEBUG
1199 GST_INFO_OBJECT (v4l2object->dbg_obj, "got %d format(s):", n);
1200 for (l = v4l2object->formats; l != NULL; l = l->next) {
1203 GST_INFO_OBJECT (v4l2object->dbg_obj,
1204 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1205 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1217 if (v4l2object->element)
1220 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1221 (_("Failed to enumerate possible video formats device '%s' can work "
1222 "with"), v4l2object->videodev),
1223 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1224 n, v4l2object->videodev, errno, g_strerror (errno)));
1231 * Get the list of supported capture formats, a list of
1232 * <code>struct v4l2_fmtdesc</code>.
1235 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1237 if (!v4l2object->formats) {
1239 /* check usual way */
1240 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1242 /* if our driver supports multi-planar
1243 * and if formats are still empty then we can workaround driver bug
1244 * by also looking up formats as if our device was not supporting
1246 if (!v4l2object->formats) {
1247 switch (v4l2object->type) {
1248 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1249 gst_v4l2_object_fill_format_list (v4l2object,
1250 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1253 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1254 gst_v4l2_object_fill_format_list (v4l2object,
1255 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1263 return v4l2object->formats;
1266 static GstVideoFormat
1267 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1269 GstVideoFormat format;
1272 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1273 format = GST_VIDEO_FORMAT_GRAY8;
1275 case V4L2_PIX_FMT_Y16:
1276 format = GST_VIDEO_FORMAT_GRAY16_LE;
1278 case V4L2_PIX_FMT_Y16_BE:
1279 format = GST_VIDEO_FORMAT_GRAY16_BE;
1281 case V4L2_PIX_FMT_XRGB555:
1282 case V4L2_PIX_FMT_RGB555:
1283 format = GST_VIDEO_FORMAT_RGB15;
1285 case V4L2_PIX_FMT_XRGB555X:
1286 case V4L2_PIX_FMT_RGB555X:
1287 format = GST_VIDEO_FORMAT_BGR15;
1289 case V4L2_PIX_FMT_RGB565:
1290 format = GST_VIDEO_FORMAT_RGB16;
1292 case V4L2_PIX_FMT_RGB24:
1293 format = GST_VIDEO_FORMAT_RGB;
1295 case V4L2_PIX_FMT_BGR24:
1296 format = GST_VIDEO_FORMAT_BGR;
1298 case V4L2_PIX_FMT_XRGB32:
1299 case V4L2_PIX_FMT_RGB32:
1300 format = GST_VIDEO_FORMAT_xRGB;
1302 case V4L2_PIX_FMT_XBGR32:
1303 case V4L2_PIX_FMT_BGR32:
1304 format = GST_VIDEO_FORMAT_BGRx;
1306 case V4L2_PIX_FMT_ABGR32:
1307 format = GST_VIDEO_FORMAT_BGRA;
1309 case V4L2_PIX_FMT_ARGB32:
1310 format = GST_VIDEO_FORMAT_ARGB;
1312 case V4L2_PIX_FMT_NV12:
1313 case V4L2_PIX_FMT_NV12M:
1314 format = GST_VIDEO_FORMAT_NV12;
1316 case V4L2_PIX_FMT_NV12MT:
1317 format = GST_VIDEO_FORMAT_NV12_64Z32;
1319 case V4L2_PIX_FMT_NV21:
1320 case V4L2_PIX_FMT_NV21M:
1321 format = GST_VIDEO_FORMAT_NV21;
1323 case V4L2_PIX_FMT_YVU410:
1324 format = GST_VIDEO_FORMAT_YVU9;
1326 case V4L2_PIX_FMT_YUV410:
1327 format = GST_VIDEO_FORMAT_YUV9;
1329 case V4L2_PIX_FMT_YUV420:
1330 case V4L2_PIX_FMT_YUV420M:
1331 format = GST_VIDEO_FORMAT_I420;
1333 case V4L2_PIX_FMT_YUYV:
1334 format = GST_VIDEO_FORMAT_YUY2;
1336 case V4L2_PIX_FMT_YVU420:
1337 format = GST_VIDEO_FORMAT_YV12;
1339 case V4L2_PIX_FMT_UYVY:
1340 format = GST_VIDEO_FORMAT_UYVY;
1342 case V4L2_PIX_FMT_YUV411P:
1343 format = GST_VIDEO_FORMAT_Y41B;
1345 case V4L2_PIX_FMT_YUV422P:
1346 format = GST_VIDEO_FORMAT_Y42B;
1348 case V4L2_PIX_FMT_YVYU:
1349 format = GST_VIDEO_FORMAT_YVYU;
1351 case V4L2_PIX_FMT_NV16:
1352 case V4L2_PIX_FMT_NV16M:
1353 format = GST_VIDEO_FORMAT_NV16;
1355 case V4L2_PIX_FMT_NV61:
1356 case V4L2_PIX_FMT_NV61M:
1357 format = GST_VIDEO_FORMAT_NV61;
1359 case V4L2_PIX_FMT_NV24:
1360 format = GST_VIDEO_FORMAT_NV24;
1363 format = GST_VIDEO_FORMAT_UNKNOWN;
1371 gst_v4l2_object_v4l2fourcc_is_rgb (guint32 fourcc)
1373 gboolean ret = FALSE;
1376 case V4L2_PIX_FMT_XRGB555:
1377 case V4L2_PIX_FMT_RGB555:
1378 case V4L2_PIX_FMT_XRGB555X:
1379 case V4L2_PIX_FMT_RGB555X:
1380 case V4L2_PIX_FMT_RGB565:
1381 case V4L2_PIX_FMT_RGB24:
1382 case V4L2_PIX_FMT_BGR24:
1383 case V4L2_PIX_FMT_XRGB32:
1384 case V4L2_PIX_FMT_RGB32:
1385 case V4L2_PIX_FMT_XBGR32:
1386 case V4L2_PIX_FMT_BGR32:
1387 case V4L2_PIX_FMT_ABGR32:
1388 case V4L2_PIX_FMT_ARGB32:
1398 static GstStructure *
1399 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1401 GstStructure *structure = NULL;
1404 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1405 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1406 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1407 structure = gst_structure_new_empty ("image/jpeg");
1409 case V4L2_PIX_FMT_MPEG1:
1410 structure = gst_structure_new ("video/mpeg",
1411 "mpegversion", G_TYPE_INT, 2, NULL);
1413 case V4L2_PIX_FMT_MPEG2:
1414 structure = gst_structure_new ("video/mpeg",
1415 "mpegversion", G_TYPE_INT, 2, NULL);
1417 case V4L2_PIX_FMT_MPEG4:
1418 case V4L2_PIX_FMT_XVID:
1419 structure = gst_structure_new ("video/mpeg",
1420 "mpegversion", G_TYPE_INT, 4, "systemstream",
1421 G_TYPE_BOOLEAN, FALSE, NULL);
1423 case V4L2_PIX_FMT_H263:
1424 structure = gst_structure_new ("video/x-h263",
1425 "variant", G_TYPE_STRING, "itu", NULL);
1427 case V4L2_PIX_FMT_H264: /* H.264 */
1428 structure = gst_structure_new ("video/x-h264",
1429 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1430 G_TYPE_STRING, "au", NULL);
1432 case V4L2_PIX_FMT_H264_NO_SC:
1433 structure = gst_structure_new ("video/x-h264",
1434 "stream-format", G_TYPE_STRING, "avc", "alignment",
1435 G_TYPE_STRING, "au", NULL);
1437 case V4L2_PIX_FMT_VC1_ANNEX_G:
1438 case V4L2_PIX_FMT_VC1_ANNEX_L:
1439 structure = gst_structure_new ("video/x-wmv",
1440 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1442 case V4L2_PIX_FMT_VP8:
1443 structure = gst_structure_new_empty ("video/x-vp8");
1445 case V4L2_PIX_FMT_VP9:
1446 structure = gst_structure_new_empty ("video/x-vp9");
1448 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1449 case V4L2_PIX_FMT_Y16:
1450 case V4L2_PIX_FMT_Y16_BE:
1451 case V4L2_PIX_FMT_XRGB555:
1452 case V4L2_PIX_FMT_RGB555:
1453 case V4L2_PIX_FMT_XRGB555X:
1454 case V4L2_PIX_FMT_RGB555X:
1455 case V4L2_PIX_FMT_RGB565:
1456 case V4L2_PIX_FMT_RGB24:
1457 case V4L2_PIX_FMT_BGR24:
1458 case V4L2_PIX_FMT_RGB32:
1459 case V4L2_PIX_FMT_XRGB32:
1460 case V4L2_PIX_FMT_ARGB32:
1461 case V4L2_PIX_FMT_BGR32:
1462 case V4L2_PIX_FMT_XBGR32:
1463 case V4L2_PIX_FMT_ABGR32:
1464 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1465 case V4L2_PIX_FMT_NV12M:
1466 case V4L2_PIX_FMT_NV12MT:
1467 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1468 case V4L2_PIX_FMT_NV21M:
1469 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1470 case V4L2_PIX_FMT_NV16M:
1471 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1472 case V4L2_PIX_FMT_NV61M:
1473 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1474 case V4L2_PIX_FMT_YVU410:
1475 case V4L2_PIX_FMT_YUV410:
1476 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1477 case V4L2_PIX_FMT_YUV420M:
1478 case V4L2_PIX_FMT_YUYV:
1479 case V4L2_PIX_FMT_YVU420:
1480 case V4L2_PIX_FMT_UYVY:
1481 case V4L2_PIX_FMT_YUV422P:
1482 case V4L2_PIX_FMT_YVYU:
1483 case V4L2_PIX_FMT_YUV411P:{
1484 GstVideoFormat format;
1485 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1486 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1487 structure = gst_structure_new ("video/x-raw",
1488 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1491 case V4L2_PIX_FMT_DV:
1493 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1496 case V4L2_PIX_FMT_MPEG: /* MPEG */
1497 structure = gst_structure_new ("video/mpegts",
1498 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1500 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1502 case V4L2_PIX_FMT_SBGGR8:
1503 case V4L2_PIX_FMT_SGBRG8:
1504 case V4L2_PIX_FMT_SGRBG8:
1505 case V4L2_PIX_FMT_SRGGB8:
1506 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1507 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1508 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1509 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1510 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1512 case V4L2_PIX_FMT_SN9C10X:
1513 structure = gst_structure_new_empty ("video/x-sonix");
1515 case V4L2_PIX_FMT_PWC1:
1516 structure = gst_structure_new_empty ("video/x-pwc1");
1518 case V4L2_PIX_FMT_PWC2:
1519 structure = gst_structure_new_empty ("video/x-pwc2");
1521 case V4L2_PIX_FMT_RGB332:
1522 case V4L2_PIX_FMT_BGR666:
1523 case V4L2_PIX_FMT_ARGB555X:
1524 case V4L2_PIX_FMT_RGB565X:
1525 case V4L2_PIX_FMT_RGB444:
1526 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1527 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1528 case V4L2_PIX_FMT_Y4:
1529 case V4L2_PIX_FMT_Y6:
1530 case V4L2_PIX_FMT_Y10:
1531 case V4L2_PIX_FMT_Y12:
1532 case V4L2_PIX_FMT_Y10BPACK:
1533 case V4L2_PIX_FMT_YUV444:
1534 case V4L2_PIX_FMT_YUV555:
1535 case V4L2_PIX_FMT_YUV565:
1536 case V4L2_PIX_FMT_Y41P:
1537 case V4L2_PIX_FMT_YUV32:
1538 case V4L2_PIX_FMT_NV12MT_16X16:
1539 case V4L2_PIX_FMT_NV42:
1540 case V4L2_PIX_FMT_H264_MVC:
1542 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1543 fourcc, GST_FOURCC_ARGS (fourcc));
1551 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1553 GstStructure *template;
1556 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1558 if (template == NULL)
1561 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1562 if (gst_v4l2_formats[i].format != fourcc)
1565 if (gst_v4l2_formats[i].dimensions) {
1566 gst_structure_set (template,
1567 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1568 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1569 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1580 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1582 GstStructure *structure;
1586 caps = gst_caps_new_empty ();
1587 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1589 if ((gst_v4l2_formats[i].flags & flags) == 0)
1593 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1596 GstStructure *alt_s = NULL;
1598 if (gst_v4l2_formats[i].dimensions) {
1599 gst_structure_set (structure,
1600 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1601 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1602 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1605 switch (gst_v4l2_formats[i].format) {
1606 case V4L2_PIX_FMT_RGB32:
1607 alt_s = gst_structure_copy (structure);
1608 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1610 case V4L2_PIX_FMT_BGR32:
1611 alt_s = gst_structure_copy (structure);
1612 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1617 gst_caps_append_structure (caps, structure);
1620 gst_caps_append_structure (caps, alt_s);
1624 return gst_caps_simplify (caps);
1628 gst_v4l2_object_get_all_caps (void)
1630 static GstCaps *caps = NULL;
1632 if (g_once_init_enter (&caps)) {
1633 GstCaps *all_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1634 GST_MINI_OBJECT_FLAG_SET (all_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1635 g_once_init_leave (&caps, all_caps);
1642 gst_v4l2_object_get_raw_caps (void)
1644 static GstCaps *caps = NULL;
1646 if (g_once_init_enter (&caps)) {
1647 GstCaps *raw_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1648 GST_MINI_OBJECT_FLAG_SET (raw_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1649 g_once_init_leave (&caps, raw_caps);
1656 gst_v4l2_object_get_codec_caps (void)
1658 static GstCaps *caps = NULL;
1660 if (g_once_init_enter (&caps)) {
1661 GstCaps *codec_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1662 GST_MINI_OBJECT_FLAG_SET (codec_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1663 g_once_init_leave (&caps, codec_caps);
1669 /* collect data for the given caps
1670 * @caps: given input caps
1671 * @format: location for the v4l format
1672 * @w/@h: location for width and height
1673 * @fps_n/@fps_d: location for framerate
1674 * @size: location for expected size of the frame or 0 if unknown
1677 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1678 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1680 GstStructure *structure;
1681 guint32 fourcc = 0, fourcc_nc = 0;
1682 const gchar *mimetype;
1683 struct v4l2_fmtdesc *fmt = NULL;
1685 structure = gst_caps_get_structure (caps, 0);
1687 mimetype = gst_structure_get_name (structure);
1689 if (!gst_video_info_from_caps (info, caps))
1690 goto invalid_format;
1692 if (g_str_equal (mimetype, "video/x-raw")) {
1693 switch (GST_VIDEO_INFO_FORMAT (info)) {
1694 case GST_VIDEO_FORMAT_I420:
1695 fourcc = V4L2_PIX_FMT_YUV420;
1696 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1698 case GST_VIDEO_FORMAT_YUY2:
1699 fourcc = V4L2_PIX_FMT_YUYV;
1701 case GST_VIDEO_FORMAT_UYVY:
1702 fourcc = V4L2_PIX_FMT_UYVY;
1704 case GST_VIDEO_FORMAT_YV12:
1705 fourcc = V4L2_PIX_FMT_YVU420;
1707 case GST_VIDEO_FORMAT_Y41B:
1708 fourcc = V4L2_PIX_FMT_YUV411P;
1710 case GST_VIDEO_FORMAT_Y42B:
1711 fourcc = V4L2_PIX_FMT_YUV422P;
1713 case GST_VIDEO_FORMAT_NV12:
1714 fourcc = V4L2_PIX_FMT_NV12;
1715 fourcc_nc = V4L2_PIX_FMT_NV12M;
1717 case GST_VIDEO_FORMAT_NV12_64Z32:
1718 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1720 case GST_VIDEO_FORMAT_NV21:
1721 fourcc = V4L2_PIX_FMT_NV21;
1722 fourcc_nc = V4L2_PIX_FMT_NV21M;
1724 case GST_VIDEO_FORMAT_NV16:
1725 fourcc = V4L2_PIX_FMT_NV16;
1726 fourcc_nc = V4L2_PIX_FMT_NV16M;
1728 case GST_VIDEO_FORMAT_NV61:
1729 fourcc = V4L2_PIX_FMT_NV61;
1730 fourcc_nc = V4L2_PIX_FMT_NV61M;
1732 case GST_VIDEO_FORMAT_NV24:
1733 fourcc = V4L2_PIX_FMT_NV24;
1735 case GST_VIDEO_FORMAT_YVYU:
1736 fourcc = V4L2_PIX_FMT_YVYU;
1738 case GST_VIDEO_FORMAT_RGB15:
1739 fourcc = V4L2_PIX_FMT_RGB555;
1740 fourcc_nc = V4L2_PIX_FMT_XRGB555;
1742 case GST_VIDEO_FORMAT_RGB16:
1743 fourcc = V4L2_PIX_FMT_RGB565;
1745 case GST_VIDEO_FORMAT_RGB:
1746 fourcc = V4L2_PIX_FMT_RGB24;
1748 case GST_VIDEO_FORMAT_BGR:
1749 fourcc = V4L2_PIX_FMT_BGR24;
1751 case GST_VIDEO_FORMAT_xRGB:
1752 fourcc = V4L2_PIX_FMT_RGB32;
1753 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1755 case GST_VIDEO_FORMAT_ARGB:
1756 fourcc = V4L2_PIX_FMT_RGB32;
1757 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1759 case GST_VIDEO_FORMAT_BGRx:
1760 fourcc = V4L2_PIX_FMT_BGR32;
1761 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1763 case GST_VIDEO_FORMAT_BGRA:
1764 fourcc = V4L2_PIX_FMT_BGR32;
1765 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1767 case GST_VIDEO_FORMAT_GRAY8:
1768 fourcc = V4L2_PIX_FMT_GREY;
1770 case GST_VIDEO_FORMAT_GRAY16_LE:
1771 fourcc = V4L2_PIX_FMT_Y16;
1773 case GST_VIDEO_FORMAT_GRAY16_BE:
1774 fourcc = V4L2_PIX_FMT_Y16_BE;
1780 if (g_str_equal (mimetype, "video/mpegts")) {
1781 fourcc = V4L2_PIX_FMT_MPEG;
1782 } else if (g_str_equal (mimetype, "video/x-dv")) {
1783 fourcc = V4L2_PIX_FMT_DV;
1784 } else if (g_str_equal (mimetype, "image/jpeg")) {
1785 fourcc = V4L2_PIX_FMT_JPEG;
1786 } else if (g_str_equal (mimetype, "video/mpeg")) {
1788 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1791 fourcc = V4L2_PIX_FMT_MPEG1;
1794 fourcc = V4L2_PIX_FMT_MPEG2;
1797 fourcc = V4L2_PIX_FMT_MPEG4;
1798 fourcc_nc = V4L2_PIX_FMT_XVID;
1804 } else if (g_str_equal (mimetype, "video/x-h263")) {
1805 fourcc = V4L2_PIX_FMT_H263;
1806 } else if (g_str_equal (mimetype, "video/x-h264")) {
1807 const gchar *stream_format =
1808 gst_structure_get_string (structure, "stream-format");
1809 if (g_str_equal (stream_format, "avc"))
1810 fourcc = V4L2_PIX_FMT_H264_NO_SC;
1812 fourcc = V4L2_PIX_FMT_H264;
1813 } else if (g_str_equal (mimetype, "video/x-vp8")) {
1814 fourcc = V4L2_PIX_FMT_VP8;
1815 } else if (g_str_equal (mimetype, "video/x-vp9")) {
1816 fourcc = V4L2_PIX_FMT_VP9;
1817 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1818 const gchar *format = gst_structure_get_string (structure, "format");
1820 if (!g_ascii_strcasecmp (format, "bggr"))
1821 fourcc = V4L2_PIX_FMT_SBGGR8;
1822 else if (!g_ascii_strcasecmp (format, "gbrg"))
1823 fourcc = V4L2_PIX_FMT_SGBRG8;
1824 else if (!g_ascii_strcasecmp (format, "grbg"))
1825 fourcc = V4L2_PIX_FMT_SGRBG8;
1826 else if (!g_ascii_strcasecmp (format, "rggb"))
1827 fourcc = V4L2_PIX_FMT_SRGGB8;
1829 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1830 fourcc = V4L2_PIX_FMT_SN9C10X;
1831 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1832 fourcc = V4L2_PIX_FMT_PWC1;
1833 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1834 fourcc = V4L2_PIX_FMT_PWC2;
1839 /* Prefer the non-contiguous if supported */
1840 v4l2object->prefered_non_contiguous = TRUE;
1843 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
1844 else if (fourcc == 0)
1845 goto unhandled_format;
1848 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1849 v4l2object->prefered_non_contiguous = FALSE;
1853 goto unsupported_format;
1862 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1867 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1872 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1878 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1879 guint32 pixelformat, gint * width, gint * height);
1882 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1884 if (v4l2object->keep_aspect && v4l2object->par)
1885 gst_structure_set_value (s, "pixel-aspect-ratio", v4l2object->par);
1888 /* returns TRUE if the value was changed in place, otherwise FALSE */
1890 gst_v4l2src_value_simplify (GValue * val)
1892 /* simplify list of one value to one value */
1893 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
1894 const GValue *list_val;
1895 GValue new_val = G_VALUE_INIT;
1897 list_val = gst_value_list_get_value (val, 0);
1898 g_value_init (&new_val, G_VALUE_TYPE (list_val));
1899 g_value_copy (list_val, &new_val);
1900 g_value_unset (val);
1909 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
1910 GstVideoInterlaceMode * interlace_mode)
1912 /* NB: If you add new return values, please fix mode_strings in
1913 * gst_v4l2_object_add_interlace_mode */
1915 case V4L2_FIELD_ANY:
1917 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
1919 case V4L2_FIELD_NONE:
1920 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1922 case V4L2_FIELD_INTERLACED:
1923 case V4L2_FIELD_INTERLACED_TB:
1924 case V4L2_FIELD_INTERLACED_BT:
1925 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1928 GST_ERROR ("Unknown enum v4l2_field %d", field);
1934 gst_v4l2_object_get_colorspace (struct v4l2_format *fmt,
1935 GstVideoColorimetry * cinfo)
1938 gst_v4l2_object_v4l2fourcc_is_rgb (fmt->fmt.pix.pixelformat);
1939 enum v4l2_colorspace colorspace;
1940 enum v4l2_quantization range;
1941 enum v4l2_ycbcr_encoding matrix;
1942 enum v4l2_xfer_func transfer;
1943 gboolean ret = TRUE;
1945 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
1946 colorspace = fmt->fmt.pix_mp.colorspace;
1947 range = fmt->fmt.pix_mp.quantization;
1948 matrix = fmt->fmt.pix_mp.ycbcr_enc;
1949 transfer = fmt->fmt.pix_mp.xfer_func;
1951 colorspace = fmt->fmt.pix.colorspace;
1952 range = fmt->fmt.pix.quantization;
1953 matrix = fmt->fmt.pix.ycbcr_enc;
1954 transfer = fmt->fmt.pix.xfer_func;
1957 /* First step, set the defaults for each primaries */
1958 switch (colorspace) {
1959 case V4L2_COLORSPACE_SMPTE170M:
1960 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1961 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1962 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1963 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1965 case V4L2_COLORSPACE_REC709:
1966 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1967 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1968 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1969 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1971 case V4L2_COLORSPACE_SRGB:
1972 case V4L2_COLORSPACE_JPEG:
1973 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1974 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1975 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
1976 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1978 case V4L2_COLORSPACE_ADOBERGB:
1979 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1980 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1981 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
1982 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
1984 case V4L2_COLORSPACE_BT2020:
1985 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1986 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1987 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
1988 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
1990 case V4L2_COLORSPACE_SMPTE240M:
1991 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1992 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1993 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
1994 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
1996 case V4L2_COLORSPACE_470_SYSTEM_M:
1997 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1998 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1999 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2000 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
2002 case V4L2_COLORSPACE_470_SYSTEM_BG:
2003 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2004 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2005 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2006 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
2008 case V4L2_COLORSPACE_RAW:
2009 /* Explicitly unknown */
2010 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2011 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2012 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2013 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
2016 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
2024 /* Second step, apply any custom variation */
2026 case V4L2_QUANTIZATION_FULL_RANGE:
2027 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2029 case V4L2_QUANTIZATION_LIM_RANGE:
2030 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2032 case V4L2_QUANTIZATION_DEFAULT:
2033 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
2034 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
2035 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2036 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601
2037 || matrix == V4L2_YCBCR_ENC_XV709
2038 || colorspace == V4L2_COLORSPACE_JPEG)
2039 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2041 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2044 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
2045 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2050 case V4L2_YCBCR_ENC_XV601:
2051 case V4L2_YCBCR_ENC_SYCC:
2052 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
2054 case V4L2_YCBCR_ENC_601:
2055 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2057 case V4L2_YCBCR_ENC_XV709:
2058 GST_FIXME ("XV709 not defined, assuming 709");
2060 case V4L2_YCBCR_ENC_709:
2061 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2063 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2064 GST_FIXME ("BT2020 with constant luma is not defined, assuming BT2020");
2066 case V4L2_YCBCR_ENC_BT2020:
2067 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2069 case V4L2_YCBCR_ENC_SMPTE240M:
2070 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2072 case V4L2_YCBCR_ENC_DEFAULT:
2073 /* nothing, just use defaults for colorspace */
2076 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2077 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2081 /* Set identity matrix for R'G'B' formats to avoid creating
2082 * confusion. This though is cosmetic as it's now properly ignored by
2083 * the video info API and videoconvert. */
2085 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2088 case V4L2_XFER_FUNC_709:
2089 if (colorspace == V4L2_COLORSPACE_BT2020 && fmt->fmt.pix.height >= 2160)
2090 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2092 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2094 case V4L2_XFER_FUNC_SRGB:
2095 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2097 case V4L2_XFER_FUNC_ADOBERGB:
2098 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2100 case V4L2_XFER_FUNC_SMPTE240M:
2101 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2103 case V4L2_XFER_FUNC_NONE:
2104 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2106 case V4L2_XFER_FUNC_DEFAULT:
2107 /* nothing, just use defaults for colorspace */
2110 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2111 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2120 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2121 struct v4l2_format *try_fmt)
2123 int fd = v4l2object->video_fd;
2124 struct v4l2_format fmt;
2127 memcpy (&fmt, try_fmt, sizeof (fmt));
2128 r = v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2130 if (r < 0 && errno == ENOTTY) {
2131 /* The driver might not implement TRY_FMT, in which case we will try
2133 if (GST_V4L2_IS_ACTIVE (v4l2object))
2136 memcpy (&fmt, try_fmt, sizeof (fmt));
2137 r = v4l2object->ioctl (fd, VIDIOC_S_FMT, &fmt);
2139 memcpy (try_fmt, &fmt, sizeof (fmt));
2144 memcpy (try_fmt, &fmt, sizeof (fmt));
2145 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2146 "Unable to try format: %s", g_strerror (errno));
2152 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2153 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2155 struct v4l2_format fmt;
2156 GValue interlace_formats = { 0, };
2157 GstVideoInterlaceMode interlace_mode, prev = -1;
2159 const gchar *mode_strings[] = { "progressive",
2164 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2167 if (v4l2object->never_interlaced) {
2168 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2172 g_value_init (&interlace_formats, GST_TYPE_LIST);
2174 /* Try twice - once for NONE, once for INTERLACED. */
2175 memset (&fmt, 0, sizeof (fmt));
2176 fmt.type = v4l2object->type;
2177 fmt.fmt.pix.width = width;
2178 fmt.fmt.pix.height = height;
2179 fmt.fmt.pix.pixelformat = pixelformat;
2180 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2182 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2183 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2184 GValue interlace_enum = { 0, };
2185 g_value_init (&interlace_enum, G_TYPE_STRING);
2186 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2187 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2188 prev = interlace_mode;
2191 memset (&fmt, 0, sizeof (fmt));
2192 fmt.type = v4l2object->type;
2193 fmt.fmt.pix.width = width;
2194 fmt.fmt.pix.height = height;
2195 fmt.fmt.pix.pixelformat = pixelformat;
2196 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2198 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2199 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode) &&
2200 prev != interlace_mode) {
2201 GValue interlace_enum = { 0, };
2202 g_value_init (&interlace_enum, G_TYPE_STRING);
2203 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2204 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2207 if (gst_v4l2src_value_simplify (&interlace_formats)
2208 || gst_value_list_get_size (&interlace_formats) > 0)
2209 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2211 GST_WARNING_OBJECT (v4l2object, "Failed to determine interlace mode");
2217 gst_v4l2_object_fill_colorimetry_list (GValue * list,
2218 GstVideoColorimetry * cinfo)
2220 GValue colorimetry = G_VALUE_INIT;
2223 gboolean found = FALSE;
2225 g_value_init (&colorimetry, G_TYPE_STRING);
2226 g_value_take_string (&colorimetry, gst_video_colorimetry_to_string (cinfo));
2228 /* only insert if no duplicate */
2229 size = gst_value_list_get_size (list);
2230 for (i = 0; i < size; i++) {
2233 tmp = gst_value_list_get_value (list, i);
2234 if (gst_value_compare (&colorimetry, tmp) == GST_VALUE_EQUAL) {
2241 gst_value_list_append_and_take_value (list, &colorimetry);
2243 g_value_unset (&colorimetry);
2247 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2248 guint32 width, guint32 height, guint32 pixelformat)
2250 struct v4l2_format fmt;
2251 GValue list = G_VALUE_INIT;
2252 GstVideoColorimetry cinfo;
2253 enum v4l2_colorspace req_cspace;
2255 memset (&fmt, 0, sizeof (fmt));
2256 fmt.type = v4l2object->type;
2257 fmt.fmt.pix.width = width;
2258 fmt.fmt.pix.height = height;
2259 fmt.fmt.pix.pixelformat = pixelformat;
2261 g_value_init (&list, GST_TYPE_LIST);
2263 /* step 1: get device default colorspace and insert it first as
2264 * it should be the preferred one */
2265 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2266 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2267 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2270 /* step 2: probe all colorspace other than default
2271 * We don't probe all colorspace, range, matrix and transfer combination to
2272 * avoid ioctl flooding which could greatly increase initialization time
2273 * with low-speed devices (UVC...) */
2274 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2275 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++) {
2276 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2277 if (req_cspace == V4L2_COLORSPACE_BT878)
2280 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2281 fmt.fmt.pix_mp.colorspace = req_cspace;
2283 fmt.fmt.pix.colorspace = req_cspace;
2285 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2286 enum v4l2_colorspace colorspace;
2288 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2289 colorspace = fmt.fmt.pix_mp.colorspace;
2291 colorspace = fmt.fmt.pix.colorspace;
2293 if (colorspace == req_cspace) {
2294 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2295 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2300 if (gst_value_list_get_size (&list) > 0)
2301 gst_structure_take_value (s, "colorimetry", &list);
2303 g_value_unset (&list);
2308 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2309 static GstStructure *
2310 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2311 guint32 pixelformat,
2312 guint32 width, guint32 height, const GstStructure * template)
2314 gint fd = v4l2object->video_fd;
2315 struct v4l2_frmivalenum ival;
2318 GValue rates = { 0, };
2320 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2322 ival.pixel_format = pixelformat;
2324 ival.height = height;
2326 GST_LOG_OBJECT (v4l2object->dbg_obj,
2327 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2328 GST_FOURCC_ARGS (pixelformat));
2330 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2331 * fraction to get framerate */
2332 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2333 goto enum_frameintervals_failed;
2335 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2336 GValue rate = { 0, };
2338 g_value_init (&rates, GST_TYPE_LIST);
2339 g_value_init (&rate, GST_TYPE_FRACTION);
2342 num = ival.discrete.numerator;
2343 denom = ival.discrete.denominator;
2345 if (num > G_MAXINT || denom > G_MAXINT) {
2346 /* let us hope we don't get here... */
2351 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding discrete framerate: %d/%d",
2354 /* swap to get the framerate */
2355 gst_value_set_fraction (&rate, denom, num);
2356 gst_value_list_append_value (&rates, &rate);
2359 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2360 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2361 GValue min = { 0, };
2362 GValue step = { 0, };
2363 GValue max = { 0, };
2364 gboolean added = FALSE;
2365 guint32 minnum, mindenom;
2366 guint32 maxnum, maxdenom;
2368 g_value_init (&rates, GST_TYPE_LIST);
2370 g_value_init (&min, GST_TYPE_FRACTION);
2371 g_value_init (&step, GST_TYPE_FRACTION);
2372 g_value_init (&max, GST_TYPE_FRACTION);
2375 minnum = ival.stepwise.min.numerator;
2376 mindenom = ival.stepwise.min.denominator;
2377 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2381 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise min frame interval: %d/%d",
2383 gst_value_set_fraction (&min, minnum, mindenom);
2386 maxnum = ival.stepwise.max.numerator;
2387 maxdenom = ival.stepwise.max.denominator;
2388 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2393 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise max frame interval: %d/%d",
2395 gst_value_set_fraction (&max, maxnum, maxdenom);
2398 num = ival.stepwise.step.numerator;
2399 denom = ival.stepwise.step.denominator;
2400 if (num > G_MAXINT || denom > G_MAXINT) {
2405 if (num == 0 || denom == 0) {
2406 /* in this case we have a wrong fraction or no step, set the step to max
2407 * so that we only add the min value in the loop below */
2412 /* since we only have gst_value_fraction_subtract and not add, negate the
2414 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise step frame interval: %d/%d",
2416 gst_value_set_fraction (&step, -num, denom);
2418 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2419 GValue rate = { 0, };
2421 num = gst_value_get_fraction_numerator (&min);
2422 denom = gst_value_get_fraction_denominator (&min);
2423 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding stepwise framerate: %d/%d",
2426 /* invert to get the framerate */
2427 g_value_init (&rate, GST_TYPE_FRACTION);
2428 gst_value_set_fraction (&rate, denom, num);
2429 gst_value_list_append_value (&rates, &rate);
2432 /* we're actually adding because step was negated above. This is because
2433 * there is no _add function... */
2434 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2435 GST_WARNING_OBJECT (v4l2object->dbg_obj, "could not step fraction!");
2440 /* no range was added, leave the default range from the template */
2441 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2442 "no range added, leaving default");
2443 g_value_unset (&rates);
2445 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2446 guint32 maxnum, maxdenom;
2448 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2450 num = ival.stepwise.min.numerator;
2451 denom = ival.stepwise.min.denominator;
2452 if (num > G_MAXINT || denom > G_MAXINT) {
2457 maxnum = ival.stepwise.max.numerator;
2458 maxdenom = ival.stepwise.max.denominator;
2459 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2464 GST_LOG_OBJECT (v4l2object->dbg_obj,
2465 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2468 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2474 s = gst_structure_copy (template);
2475 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2476 "height", G_TYPE_INT, (gint) height, NULL);
2478 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2480 if (!v4l2object->skip_try_fmt_probes) {
2481 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2483 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2486 if (G_IS_VALUE (&rates)) {
2487 gst_v4l2src_value_simplify (&rates);
2488 /* only change the framerate on the template when we have a valid probed new
2490 gst_structure_take_value (s, "framerate", &rates);
2491 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2492 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2493 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2499 enum_frameintervals_failed:
2501 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2502 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2503 GST_FOURCC_ARGS (pixelformat), width, height);
2508 /* I don't see how this is actually an error, we ignore the format then */
2509 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2510 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2511 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2517 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2521 gst_structure_get_int (s1, "width", &w1);
2522 gst_structure_get_int (s1, "height", &h1);
2523 gst_structure_get_int (s2, "width", &w2);
2524 gst_structure_get_int (s2, "height", &h2);
2526 /* I think it's safe to assume that this won't overflow for a while */
2527 return ((w2 * h2) - (w1 * h1));
2531 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2532 guint32 format, GstCaps * caps, GstStructure * s)
2534 GstStructure *alt_s = NULL;
2536 /* Encoded stream on output buffer need to be parsed */
2537 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2538 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2541 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2542 if (format == gst_v4l2_formats[i].format &&
2543 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2544 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2545 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2551 if (v4l2object->has_alpha_component &&
2552 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2553 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2555 case V4L2_PIX_FMT_RGB32:
2556 alt_s = gst_structure_copy (s);
2557 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2559 case V4L2_PIX_FMT_BGR32:
2560 alt_s = gst_structure_copy (s);
2561 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2568 gst_caps_append_structure (caps, s);
2571 gst_caps_append_structure (caps, alt_s);
2575 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2576 guint32 pixelformat, const GstStructure * template)
2578 GstCaps *ret = gst_caps_new_empty ();
2580 gint fd = v4l2object->video_fd;
2581 struct v4l2_frmsizeenum size;
2582 GList *results = NULL;
2585 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2586 gst_caps_append_structure (ret, gst_structure_copy (template));
2590 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2592 size.pixel_format = pixelformat;
2594 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2595 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2596 GST_FOURCC_ARGS (pixelformat));
2598 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2599 goto enum_framesizes_failed;
2601 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2603 GST_LOG_OBJECT (v4l2object->dbg_obj, "got discrete frame size %dx%d",
2604 size.discrete.width, size.discrete.height);
2606 w = MIN (size.discrete.width, G_MAXINT);
2607 h = MIN (size.discrete.height, G_MAXINT);
2611 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2612 pixelformat, w, h, template);
2615 results = g_list_prepend (results, tmp);
2619 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2620 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2621 "done iterating discrete frame sizes");
2622 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2623 guint32 maxw, maxh, step_w, step_h;
2625 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have stepwise frame sizes:");
2626 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2627 size.stepwise.min_width);
2628 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2629 size.stepwise.min_height);
2630 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2631 size.stepwise.max_width);
2632 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2633 size.stepwise.max_height);
2634 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step width: %d",
2635 size.stepwise.step_width);
2636 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step height: %d",
2637 size.stepwise.step_height);
2639 w = MAX (size.stepwise.min_width, 1);
2640 h = MAX (size.stepwise.min_height, 1);
2641 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2642 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2644 step_w = MAX (size.stepwise.step_width, 1);
2645 step_h = MAX (size.stepwise.step_height, 1);
2647 /* FIXME: check for sanity and that min/max are multiples of the steps */
2649 /* we only query details for the max width/height since it's likely the
2650 * most restricted if there are any resolution-dependent restrictions */
2651 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2652 pixelformat, maxw, maxh, template);
2655 GValue step_range = G_VALUE_INIT;
2657 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2658 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2659 gst_structure_set_value (tmp, "width", &step_range);
2661 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2662 gst_structure_take_value (tmp, "height", &step_range);
2664 /* no point using the results list here, since there's only one struct */
2665 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2667 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2670 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have continuous frame sizes:");
2671 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2672 size.stepwise.min_width);
2673 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2674 size.stepwise.min_height);
2675 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2676 size.stepwise.max_width);
2677 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2678 size.stepwise.max_height);
2680 w = MAX (size.stepwise.min_width, 1);
2681 h = MAX (size.stepwise.min_height, 1);
2682 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2683 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2686 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2689 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2690 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2693 /* no point using the results list here, since there's only one struct */
2694 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2700 /* we use an intermediary list to store and then sort the results of the
2701 * probing because we can't make any assumptions about the order in which
2702 * the driver will give us the sizes, but we want the final caps to contain
2703 * the results starting with the highest resolution and having the lowest
2704 * resolution last, since order in caps matters for things like fixation. */
2705 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2706 while (results != NULL) {
2707 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
2709 results = g_list_delete_link (results, results);
2712 if (gst_caps_is_empty (ret))
2713 goto enum_framesizes_no_results;
2718 enum_framesizes_failed:
2720 /* I don't see how this is actually an error */
2721 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2722 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2723 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2724 goto default_frame_sizes;
2726 enum_framesizes_no_results:
2728 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2729 * question doesn't actually support it yet */
2730 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2731 "No results for pixelformat %" GST_FOURCC_FORMAT
2732 " enumerating frame sizes, trying fallback",
2733 GST_FOURCC_ARGS (pixelformat));
2734 goto default_frame_sizes;
2738 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2739 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2740 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2741 goto default_frame_sizes;
2744 default_frame_sizes:
2746 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2748 /* This code is for Linux < 2.6.19 */
2750 max_w = max_h = GST_V4L2_MAX_SIZE;
2751 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2753 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2754 "Could not probe minimum capture size for pixelformat %"
2755 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2757 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2759 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2760 "Could not probe maximum capture size for pixelformat %"
2761 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2764 /* Since we can't get framerate directly, try to use the current norm */
2765 if (v4l2object->tv_norm && v4l2object->norms) {
2767 GstTunerNorm *norm = NULL;
2768 GstTunerNorm *current =
2769 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2771 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2772 norm = (GstTunerNorm *) norms->data;
2773 if (!strcmp (norm->label, current->label))
2776 /* If it's possible, set framerate to that (discrete) value */
2778 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2779 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2783 tmp = gst_structure_copy (template);
2785 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2787 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2788 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2789 /* if norm can't be used, copy the template framerate */
2790 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2795 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2797 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2800 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2802 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2804 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2806 if (!v4l2object->skip_try_fmt_probes) {
2807 /* We could consider setting interlace mode from min and max. */
2808 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
2810 /* We could consider to check colorspace for min too, in case it depends on
2811 * the size. But in this case, min and max could not be enough */
2812 gst_v4l2_object_add_colorspace (v4l2object, tmp, max_w, max_h,
2816 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2822 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2823 guint32 pixelformat, gint * width, gint * height)
2825 struct v4l2_format fmt;
2826 gboolean ret = FALSE;
2827 GstVideoInterlaceMode interlace_mode;
2829 g_return_val_if_fail (width != NULL, FALSE);
2830 g_return_val_if_fail (height != NULL, FALSE);
2832 GST_LOG_OBJECT (v4l2object->dbg_obj,
2833 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2834 *width, *height, GST_FOURCC_ARGS (pixelformat));
2836 memset (&fmt, 0, sizeof (struct v4l2_format));
2838 /* get size delimiters */
2839 memset (&fmt, 0, sizeof (fmt));
2840 fmt.type = v4l2object->type;
2841 fmt.fmt.pix.width = *width;
2842 fmt.fmt.pix.height = *height;
2843 fmt.fmt.pix.pixelformat = pixelformat;
2844 fmt.fmt.pix.field = V4L2_FIELD_ANY;
2846 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
2849 GST_LOG_OBJECT (v4l2object->dbg_obj,
2850 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2852 *width = fmt.fmt.pix.width;
2853 *height = fmt.fmt.pix.height;
2855 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2856 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2857 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
2858 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
2866 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2867 "Unable to try format: %s", g_strerror (errno));
2874 gst_v4l2_object_is_dmabuf_supported (GstV4l2Object * v4l2object)
2876 gboolean ret = TRUE;
2877 struct v4l2_exportbuffer expbuf = {
2878 .type = v4l2object->type,
2881 .flags = O_CLOEXEC | O_RDWR,
2884 if (v4l2object->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
2885 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2886 "libv4l2 converter detected, disabling DMABuf");
2890 /* Expected to fail, but ENOTTY tells us that it is not implemented. */
2891 v4l2object->ioctl (v4l2object->video_fd, VIDIOC_EXPBUF, &expbuf);
2892 if (errno == ENOTTY)
2899 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2903 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "initializing the %s system",
2904 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
2906 GST_V4L2_CHECK_OPEN (v4l2object);
2907 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2909 /* find transport */
2910 mode = v4l2object->req_mode;
2912 if (v4l2object->device_caps & V4L2_CAP_READWRITE) {
2913 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2914 mode = GST_V4L2_IO_RW;
2915 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2916 goto method_not_supported;
2918 if (v4l2object->device_caps & V4L2_CAP_STREAMING) {
2919 if (v4l2object->req_mode == GST_V4L2_IO_AUTO) {
2920 if (!V4L2_TYPE_IS_OUTPUT (v4l2object->type) &&
2921 gst_v4l2_object_is_dmabuf_supported (v4l2object)) {
2922 mode = GST_V4L2_IO_DMABUF;
2924 mode = GST_V4L2_IO_MMAP;
2927 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP ||
2928 v4l2object->req_mode == GST_V4L2_IO_DMABUF)
2929 goto method_not_supported;
2931 /* if still no transport selected, error out */
2932 if (mode == GST_V4L2_IO_AUTO)
2933 goto no_supported_capture_method;
2935 GST_INFO_OBJECT (v4l2object->dbg_obj, "accessing buffers via mode %d", mode);
2936 v4l2object->mode = mode;
2938 /* If min_buffers is not set, the driver either does not support the control or
2939 it has not been asked yet via propose_allocation/decide_allocation. */
2940 if (!v4l2object->min_buffers)
2941 gst_v4l2_get_driver_min_buffers (v4l2object);
2943 /* Map the buffers */
2944 GST_LOG_OBJECT (v4l2object->dbg_obj, "initiating buffer pool");
2946 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2947 goto buffer_pool_new_failed;
2949 GST_V4L2_SET_ACTIVE (v4l2object);
2954 buffer_pool_new_failed:
2956 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2957 (_("Could not map buffers from device '%s'"),
2958 v4l2object->videodev),
2959 ("Failed to create buffer pool: %s", g_strerror (errno)));
2962 method_not_supported:
2964 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2965 (_("The driver of device '%s' does not support the IO method %d"),
2966 v4l2object->videodev, mode), (NULL));
2969 no_supported_capture_method:
2971 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2972 (_("The driver of device '%s' does not support any known IO "
2973 "method."), v4l2object->videodev), (NULL));
2979 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
2980 gint plane, gint stride)
2982 const GstVideoFormatInfo *finfo = info->finfo;
2984 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2985 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
2988 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
2989 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2990 tile_height = 1 << hs;
2992 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
2993 info->height + align->padding_top + align->padding_bottom);
2994 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
2996 x_tiles = stride >> ws;
2997 y_tiles = padded_height >> hs;
2998 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
3000 info->stride[plane] = stride;
3005 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
3006 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
3008 const GstVideoFormatInfo *finfo = info->finfo;
3009 gint i, estride, padded_height;
3012 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
3014 padded_height = info->height + align->padding_top + align->padding_bottom;
3016 for (i = 0; i < finfo->n_planes; i++) {
3017 estride = gst_v4l2_object_extrapolate_stride (finfo, i, stride);
3019 gst_v4l2_object_set_stride (info, align, i, estride);
3021 info->offset[i] = offs;
3023 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
3025 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3026 "Extrapolated for plane %d with base stride %d: "
3027 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
3031 /* Update the image size according the amount of data we are going to
3032 * read/write. This workaround bugs in driver where the sizeimage provided
3033 * by TRY/S_FMT represent the buffer length (maximum size) rather then the expected
3034 * bytesused (buffer size). */
3035 if (offs < info->size)
3040 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
3041 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
3042 GstVideoInfo * info, GstVideoAlignment * align)
3044 const GstVideoFormatInfo *finfo = info->finfo;
3045 gboolean standard_stride = TRUE;
3046 gint stride, pstride, padded_width, padded_height, i;
3048 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
3049 v4l2object->n_v4l2_planes = 1;
3050 info->size = format->fmt.pix.sizeimage;
3054 /* adjust right padding */
3055 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
3056 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3058 stride = format->fmt.pix.bytesperline;
3060 pstride = GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
3062 padded_width = stride / pstride;
3064 /* pstride can be 0 for complex formats */
3065 GST_WARNING_OBJECT (v4l2object->element,
3066 "format %s has a pstride of 0, cannot compute padded with",
3067 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
3068 padded_width = stride;
3071 if (padded_width < format->fmt.pix.width)
3072 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3073 "Driver bug detected, stride (%d) is too small for the width (%d)",
3074 padded_width, format->fmt.pix.width);
3076 align->padding_right = padded_width - info->width - align->padding_left;
3078 /* adjust bottom padding */
3079 padded_height = format->fmt.pix.height;
3081 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3082 guint hs, tile_height;
3084 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
3085 tile_height = 1 << hs;
3087 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3090 align->padding_bottom = padded_height - info->height - align->padding_top;
3092 /* setup the strides and offset */
3093 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
3094 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
3096 /* figure out the frame layout */
3097 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
3099 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
3100 stride = pix_mp->plane_fmt[i].bytesperline;
3102 if (info->stride[i] != stride)
3103 standard_stride = FALSE;
3105 gst_v4l2_object_set_stride (info, align, i, stride);
3106 info->offset[i] = info->size;
3107 info->size += pix_mp->plane_fmt[i].sizeimage;
3110 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3111 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
3112 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3113 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3116 /* only one plane in non-MPLANE mode */
3117 v4l2object->n_v4l2_planes = 1;
3118 info->size = format->fmt.pix.sizeimage;
3119 stride = format->fmt.pix.bytesperline;
3121 if (info->stride[0] != stride)
3122 standard_stride = FALSE;
3124 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3127 /* adjust the offset to take into account left and top */
3128 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3129 if ((align->padding_left + align->padding_top) > 0)
3130 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3131 "Left and top padding is not permitted for tiled formats");
3133 for (i = 0; i < finfo->n_planes; i++) {
3136 /* FIXME we assume plane as component as this is true for all supported
3137 * format we support. */
3139 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left);
3140 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top);
3142 info->offset[i] += (vedge * info->stride[i]) +
3143 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i));
3148 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got sizeimage %" G_GSIZE_FORMAT,
3151 /* to avoid copies we need video meta if there is padding */
3152 v4l2object->need_video_meta =
3153 ((align->padding_top + align->padding_left + align->padding_right +
3154 align->padding_bottom) != 0);
3156 /* ... or if stride is non "standard" */
3157 if (!standard_stride)
3158 v4l2object->need_video_meta = TRUE;
3160 /* ... or also video meta if we use multiple, non-contiguous, planes */
3161 if (v4l2object->n_v4l2_planes > 1)
3162 v4l2object->need_video_meta = TRUE;
3164 v4l2object->info = *info;
3165 v4l2object->align = *align;
3166 v4l2object->format = *format;
3167 v4l2object->fmtdesc = fmtdesc;
3169 /* if we have a framerate pre-calculate duration */
3170 if (info->fps_n > 0 && info->fps_d > 0) {
3171 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
3174 v4l2object->duration = GST_CLOCK_TIME_NONE;
3179 gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
3180 gint plane, gint stride)
3184 switch (finfo->format) {
3185 case GST_VIDEO_FORMAT_NV12:
3186 case GST_VIDEO_FORMAT_NV12_64Z32:
3187 case GST_VIDEO_FORMAT_NV21:
3188 case GST_VIDEO_FORMAT_NV16:
3189 case GST_VIDEO_FORMAT_NV61:
3190 case GST_VIDEO_FORMAT_NV24:
3191 estride = (plane == 0 ? 1 : 2) *
3192 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3195 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3203 gst_v4l2_video_colorimetry_matches (const GstVideoColorimetry * cinfo,
3204 const gchar * color)
3206 GstVideoColorimetry ci;
3207 static const GstVideoColorimetry ci_likely_jpeg = {
3208 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3209 GST_VIDEO_TRANSFER_UNKNOWN, GST_VIDEO_COLOR_PRIMARIES_UNKNOWN };
3210 static const GstVideoColorimetry ci_jpeg = {
3211 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3212 GST_VIDEO_TRANSFER_SRGB, GST_VIDEO_COLOR_PRIMARIES_BT709 };
3214 if (!gst_video_colorimetry_from_string (&ci, color))
3217 if (gst_video_colorimetry_is_equal (&ci, cinfo))
3220 /* Allow 1:4:0:0 (produced by jpegdec) if the device expects 1:4:7:1 */
3221 if (gst_video_colorimetry_is_equal (&ci, &ci_likely_jpeg)
3222 && gst_video_colorimetry_is_equal (cinfo, &ci_jpeg))
3229 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3230 gboolean try_only, GstV4l2Error * error)
3232 gint fd = v4l2object->video_fd;
3233 struct v4l2_format format;
3234 struct v4l2_streamparm streamparm;
3235 enum v4l2_field field;
3236 guint32 pixelformat;
3237 struct v4l2_fmtdesc *fmtdesc;
3239 GstVideoAlignment align;
3240 gint width, height, fps_n, fps_d;
3244 enum v4l2_colorspace colorspace = 0;
3245 enum v4l2_quantization range = 0;
3246 enum v4l2_ycbcr_encoding matrix = 0;
3247 enum v4l2_xfer_func transfer = 0;
3250 g_return_val_if_fail (!v4l2object->skip_try_fmt_probes ||
3251 gst_caps_is_writable (caps), FALSE);
3253 GST_V4L2_CHECK_OPEN (v4l2object);
3255 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3257 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3259 gst_video_info_init (&info);
3260 gst_video_alignment_reset (&align);
3262 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3265 pixelformat = fmtdesc->pixelformat;
3266 width = GST_VIDEO_INFO_WIDTH (&info);
3267 height = GST_VIDEO_INFO_HEIGHT (&info);
3268 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3269 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3271 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3272 * or if contiguous is prefered */
3273 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3274 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3277 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
3278 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "interlaced video");
3279 /* ideally we would differentiate between types of interlaced video
3280 * but there is not sufficient information in the caps..
3282 field = V4L2_FIELD_INTERLACED;
3284 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "progressive video");
3285 field = V4L2_FIELD_NONE;
3288 /* We first pick the main colorspace from the primaries */
3289 switch (info.colorimetry.primaries) {
3290 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3291 /* There is two colorspaces using these primaries, use the range to
3293 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3294 colorspace = V4L2_COLORSPACE_REC709;
3296 colorspace = V4L2_COLORSPACE_SRGB;
3298 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
3299 colorspace = V4L2_COLORSPACE_BT2020;
3301 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3302 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3304 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3305 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3307 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3308 colorspace = V4L2_COLORSPACE_SMPTE170M;
3310 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3311 colorspace = V4L2_COLORSPACE_SMPTE240M;
3314 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3315 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3316 /* We don't know, we will guess */
3320 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3321 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3325 switch (info.colorimetry.range) {
3326 case GST_VIDEO_COLOR_RANGE_0_255:
3327 range = V4L2_QUANTIZATION_FULL_RANGE;
3329 case GST_VIDEO_COLOR_RANGE_16_235:
3330 range = V4L2_QUANTIZATION_LIM_RANGE;
3332 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3333 /* We let the driver pick a default one */
3336 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3337 "Unknown colorimetry range %d", info.colorimetry.range);
3341 switch (info.colorimetry.matrix) {
3342 case GST_VIDEO_COLOR_MATRIX_RGB:
3343 /* Unspecified, leave to default */
3345 /* FCC is about the same as BT601 with less digit */
3346 case GST_VIDEO_COLOR_MATRIX_FCC:
3347 case GST_VIDEO_COLOR_MATRIX_BT601:
3348 matrix = V4L2_YCBCR_ENC_601;
3350 case GST_VIDEO_COLOR_MATRIX_BT709:
3351 matrix = V4L2_YCBCR_ENC_709;
3353 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3354 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3356 case GST_VIDEO_COLOR_MATRIX_BT2020:
3357 matrix = V4L2_YCBCR_ENC_BT2020;
3359 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3360 /* We let the driver pick a default one */
3363 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3364 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3368 switch (info.colorimetry.transfer) {
3369 case GST_VIDEO_TRANSFER_GAMMA18:
3370 case GST_VIDEO_TRANSFER_GAMMA20:
3371 case GST_VIDEO_TRANSFER_GAMMA22:
3372 case GST_VIDEO_TRANSFER_GAMMA28:
3373 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3374 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3376 case GST_VIDEO_TRANSFER_GAMMA10:
3377 transfer = V4L2_XFER_FUNC_NONE;
3379 case GST_VIDEO_TRANSFER_BT2020_12:
3380 case GST_VIDEO_TRANSFER_BT709:
3381 transfer = V4L2_XFER_FUNC_709;
3383 case GST_VIDEO_TRANSFER_SMPTE240M:
3384 transfer = V4L2_XFER_FUNC_SMPTE240M;
3386 case GST_VIDEO_TRANSFER_SRGB:
3387 transfer = V4L2_XFER_FUNC_SRGB;
3389 case GST_VIDEO_TRANSFER_LOG100:
3390 case GST_VIDEO_TRANSFER_LOG316:
3391 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3392 "LOG 100, 316 transfer functions not supported");
3393 /* FIXME No known sensible default, maybe AdobeRGB ? */
3395 case GST_VIDEO_TRANSFER_UNKNOWN:
3396 /* We let the driver pick a default one */
3399 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3400 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
3404 if (colorspace == 0) {
3405 /* Try to guess colorspace according to pixelformat and size */
3406 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3407 if (range == V4L2_QUANTIZATION_FULL_RANGE
3408 && matrix == V4L2_YCBCR_ENC_601 && transfer == 0) {
3409 /* Full range BT.601 YCbCr encoding with unknown primaries and transfer
3410 * function most likely is JPEG */
3411 colorspace = V4L2_COLORSPACE_JPEG;
3412 transfer = V4L2_XFER_FUNC_SRGB;
3414 /* SD streams likely use SMPTE170M and HD streams REC709 */
3415 if (width <= 720 && height <= 576)
3416 colorspace = V4L2_COLORSPACE_SMPTE170M;
3418 colorspace = V4L2_COLORSPACE_REC709;
3420 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3421 colorspace = V4L2_COLORSPACE_SRGB;
3422 transfer = V4L2_XFER_FUNC_NONE;
3426 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format %dx%d, format "
3427 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3428 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3430 memset (&format, 0x00, sizeof (struct v4l2_format));
3431 format.type = v4l2object->type;
3434 format.type = v4l2object->type;
3435 format.fmt.pix_mp.pixelformat = pixelformat;
3436 format.fmt.pix_mp.width = width;
3437 format.fmt.pix_mp.height = height;
3438 format.fmt.pix_mp.field = field;
3439 format.fmt.pix_mp.num_planes = n_v4l_planes;
3441 /* try to ask our prefered stride but it's not a failure if not
3443 for (i = 0; i < n_v4l_planes; i++) {
3444 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3446 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3447 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3448 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3450 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3453 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3454 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3456 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3458 format.type = v4l2object->type;
3460 format.fmt.pix.width = width;
3461 format.fmt.pix.height = height;
3462 format.fmt.pix.pixelformat = pixelformat;
3463 format.fmt.pix.field = field;
3465 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3466 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3467 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3469 /* try to ask our prefered stride */
3470 format.fmt.pix.bytesperline = stride;
3472 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3473 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3476 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format is %dx%d, format "
3477 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3478 format.fmt.pix_mp.height,
3479 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3480 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3482 #ifndef GST_DISABLE_GST_DEBUG
3484 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3485 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3486 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3488 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3489 format.fmt.pix.bytesperline);
3494 format.fmt.pix_mp.colorspace = colorspace;
3495 format.fmt.pix_mp.quantization = range;
3496 format.fmt.pix_mp.ycbcr_enc = matrix;
3497 format.fmt.pix_mp.xfer_func = transfer;
3499 format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
3500 format.fmt.pix.colorspace = colorspace;
3501 format.fmt.pix.quantization = range;
3502 format.fmt.pix.ycbcr_enc = matrix;
3503 format.fmt.pix.xfer_func = transfer;
3506 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired colorspace is %d:%d:%d:%d",
3507 colorspace, range, matrix, transfer);
3510 if (v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3511 goto try_fmt_failed;
3513 if (v4l2object->ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3514 goto set_fmt_failed;
3518 colorspace = format.fmt.pix_mp.colorspace;
3519 range = format.fmt.pix_mp.quantization;
3520 matrix = format.fmt.pix_mp.ycbcr_enc;
3521 transfer = format.fmt.pix_mp.xfer_func;
3523 colorspace = format.fmt.pix.colorspace;
3524 range = format.fmt.pix.quantization;
3525 matrix = format.fmt.pix.ycbcr_enc;
3526 transfer = format.fmt.pix.xfer_func;
3529 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got format of %dx%d, format "
3530 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d:%d:%d:%d",
3531 format.fmt.pix.width, format.fmt.pix_mp.height,
3532 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3533 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3534 colorspace, range, matrix, transfer);
3536 #ifndef GST_DISABLE_GST_DEBUG
3538 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3539 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3540 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3541 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3543 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3544 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3548 if (format.fmt.pix.pixelformat != pixelformat)
3549 goto invalid_pixelformat;
3551 /* Only negotiate size with raw data.
3552 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3553 * in ASF mode for example, there is also not reason for a driver to
3554 * change the size. */
3555 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3556 /* We can crop larger images */
3557 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3558 goto invalid_dimensions;
3560 /* Note, this will be adjusted if upstream has non-centered cropping. */
3561 align.padding_top = 0;
3562 align.padding_bottom = format.fmt.pix.height - height;
3563 align.padding_left = 0;
3564 align.padding_right = format.fmt.pix.width - width;
3567 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3568 goto invalid_planes;
3570 if ((is_mplane && format.fmt.pix_mp.field != field)
3571 || format.fmt.pix.field != field)
3574 gst_v4l2_object_get_colorspace (&format, &info.colorimetry);
3576 s = gst_caps_get_structure (caps, 0);
3577 if (gst_structure_has_field (s, "colorimetry")) {
3578 if (!gst_v4l2_video_colorimetry_matches (&info.colorimetry,
3579 gst_structure_get_string (s, "colorimetry")))
3580 goto invalid_colorimetry;
3583 /* In case we have skipped the try_fmt probes, we'll need to set the
3584 * colorimetry and interlace-mode back into the caps. */
3585 if (v4l2object->skip_try_fmt_probes) {
3586 if (!gst_structure_has_field (s, "colorimetry")) {
3587 gchar *str = gst_video_colorimetry_to_string (&info.colorimetry);
3588 gst_structure_set (s, "colorimetry", G_TYPE_STRING, str, NULL);
3592 if (!gst_structure_has_field (s, "interlace-mode"))
3593 gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
3594 gst_video_interlace_mode_to_string (info.interlace_mode), NULL);
3597 if (try_only) /* good enough for trying only */
3600 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
3601 struct v4l2_control ctl = { 0, };
3602 ctl.id = V4L2_CID_ALPHA_COMPONENT;
3605 if (v4l2object->ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
3606 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3607 "Failed to set alpha component value");
3610 /* Is there a reason we require the caller to always specify a framerate? */
3611 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired framerate: %u/%u", fps_n,
3614 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
3615 streamparm.type = v4l2object->type;
3617 if (v4l2object->ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
3618 goto get_parm_failed;
3620 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
3621 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3622 GST_VIDEO_INFO_FPS_N (&info) =
3623 streamparm.parm.capture.timeperframe.denominator;
3624 GST_VIDEO_INFO_FPS_D (&info) =
3625 streamparm.parm.capture.timeperframe.numerator;
3627 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got capture framerate: %u/%u",
3628 streamparm.parm.capture.timeperframe.denominator,
3629 streamparm.parm.capture.timeperframe.numerator);
3631 /* We used to skip frame rate setup if the camera was already setup
3632 * with the requested frame rate. This breaks some cameras though,
3633 * causing them to not output data (several models of Thinkpad cameras
3634 * have this problem at least).
3635 * So, don't skip. */
3636 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting capture framerate to %u/%u",
3638 /* We want to change the frame rate, so check whether we can. Some cheap USB
3639 * cameras don't have the capability */
3640 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3641 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3642 "Not setting capture framerate (not supported)");
3646 /* Note: V4L2 wants the frame interval, we have the frame rate */
3647 streamparm.parm.capture.timeperframe.numerator = fps_d;
3648 streamparm.parm.capture.timeperframe.denominator = fps_n;
3650 /* some cheap USB cam's won't accept any change */
3651 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3652 goto set_parm_failed;
3654 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
3655 streamparm.parm.capture.timeperframe.denominator > 0) {
3656 /* get new values */
3657 fps_d = streamparm.parm.capture.timeperframe.numerator;
3658 fps_n = streamparm.parm.capture.timeperframe.denominator;
3660 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set capture framerate to %u/%u",
3663 /* fix v4l2 capture driver to provide framerate values */
3664 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3665 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
3668 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3669 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3670 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
3671 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
3672 GST_VIDEO_INFO_FPS_N (&info) =
3673 streamparm.parm.output.timeperframe.denominator;
3674 GST_VIDEO_INFO_FPS_D (&info) =
3675 streamparm.parm.output.timeperframe.numerator;
3677 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got output framerate: %u/%u",
3678 streamparm.parm.output.timeperframe.denominator,
3679 streamparm.parm.output.timeperframe.numerator);
3681 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting output framerate to %u/%u",
3683 if ((streamparm.parm.output.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3684 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3685 "Not setting output framerate (not supported)");
3689 /* Note: V4L2 wants the frame interval, we have the frame rate */
3690 streamparm.parm.output.timeperframe.numerator = fps_d;
3691 streamparm.parm.output.timeperframe.denominator = fps_n;
3693 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3694 goto set_parm_failed;
3696 if (streamparm.parm.output.timeperframe.numerator > 0 &&
3697 streamparm.parm.output.timeperframe.denominator > 0) {
3698 /* get new values */
3699 fps_d = streamparm.parm.output.timeperframe.numerator;
3700 fps_n = streamparm.parm.output.timeperframe.denominator;
3702 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set output framerate to %u/%u",
3705 /* fix v4l2 output driver to provide framerate values */
3706 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3707 "Reuse caps framerate %u/%u - fix v4l2 output driver", fps_n, fps_d);
3710 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3711 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3715 /* add boolean return, so we can fail on drivers bugs */
3716 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
3718 /* now configure the pool */
3719 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
3727 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "can't parse caps %" GST_PTR_FORMAT,
3733 if (errno == EINVAL) {
3734 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3735 (_("Device '%s' has no supported format"), v4l2object->videodev),
3736 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3737 GST_FOURCC_ARGS (pixelformat), width, height,
3738 g_strerror (errno)));
3740 GST_V4L2_ERROR (error, RESOURCE, FAILED,
3741 (_("Device '%s' failed during initialization"),
3742 v4l2object->videodev),
3743 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3744 GST_FOURCC_ARGS (pixelformat), width, height,
3745 g_strerror (errno)));
3751 if (errno == EBUSY) {
3752 GST_V4L2_ERROR (error, RESOURCE, BUSY,
3753 (_("Device '%s' is busy"), v4l2object->videodev),
3754 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3755 GST_FOURCC_ARGS (pixelformat), width, height,
3756 g_strerror (errno)));
3757 } else if (errno == EINVAL) {
3758 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3759 (_("Device '%s' has no supported format"), v4l2object->videodev),
3760 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3761 GST_FOURCC_ARGS (pixelformat), width, height,
3762 g_strerror (errno)));
3764 GST_V4L2_ERROR (error, RESOURCE, FAILED,
3765 (_("Device '%s' failed during initialization"),
3766 v4l2object->videodev),
3767 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3768 GST_FOURCC_ARGS (pixelformat), width, height,
3769 g_strerror (errno)));
3775 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3776 (_("Device '%s' cannot capture at %dx%d"),
3777 v4l2object->videodev, width, height),
3778 ("Tried to capture at %dx%d, but device returned size %dx%d",
3779 width, height, format.fmt.pix.width, format.fmt.pix.height));
3782 invalid_pixelformat:
3784 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3785 (_("Device '%s' cannot capture in the specified format"),
3786 v4l2object->videodev),
3787 ("Tried to capture in %" GST_FOURCC_FORMAT
3788 ", but device returned format" " %" GST_FOURCC_FORMAT,
3789 GST_FOURCC_ARGS (pixelformat),
3790 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
3795 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3796 (_("Device '%s' does support non-contiguous planes"),
3797 v4l2object->videodev),
3798 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
3803 enum v4l2_field wanted_field;
3806 wanted_field = format.fmt.pix_mp.field;
3808 wanted_field = format.fmt.pix.field;
3810 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3811 (_("Device '%s' does not support %s interlacing"),
3812 v4l2object->videodev,
3813 field == V4L2_FIELD_NONE ? "progressive" : "interleaved"),
3814 ("Device wants %s interlacing",
3815 wanted_field == V4L2_FIELD_NONE ? "progressive" : "interleaved"));
3818 invalid_colorimetry:
3820 gchar *wanted_colorimetry;
3822 wanted_colorimetry = gst_video_colorimetry_to_string (&info.colorimetry);
3824 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3825 (_("Device '%s' does not support %s colorimetry"),
3826 v4l2object->videodev, gst_structure_get_string (s, "colorimetry")),
3827 ("Device wants %s colorimetry", wanted_colorimetry));
3829 g_free (wanted_colorimetry);
3834 /* it's possible that this call is not supported */
3835 if (errno != EINVAL && errno != ENOTTY) {
3836 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3837 (_("Could not get parameters on device '%s'"),
3838 v4l2object->videodev), GST_ERROR_SYSTEM);
3844 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3845 (_("Video device did not accept new frame rate setting.")),
3851 /* setup_pool already send the error */
3857 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps,
3858 GstV4l2Error * error)
3860 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Setting format to %" GST_PTR_FORMAT,
3862 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE, error);
3866 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps,
3867 GstV4l2Error * error)
3869 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying format %" GST_PTR_FORMAT,
3871 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE, error);
3875 * gst_v4l2_object_acquire_format:
3876 * @v4l2object the object
3877 * @info a GstVideoInfo to be filled
3879 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
3880 * the output format is choosen by the HW.
3882 * Returns: %TRUE on success, %FALSE on failure.
3885 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
3887 struct v4l2_fmtdesc *fmtdesc;
3888 struct v4l2_format fmt;
3889 struct v4l2_crop crop;
3890 struct v4l2_selection sel;
3891 struct v4l2_rect *r = NULL;
3892 GstVideoFormat format;
3893 guint width, height;
3894 GstVideoAlignment align;
3896 gst_video_info_init (info);
3897 gst_video_alignment_reset (&align);
3899 memset (&fmt, 0x00, sizeof (struct v4l2_format));
3900 fmt.type = v4l2object->type;
3901 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
3902 goto get_fmt_failed;
3904 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
3905 fmt.fmt.pix.pixelformat);
3906 if (fmtdesc == NULL)
3907 goto unsupported_format;
3909 /* No need to care about mplane, the four first params are the same */
3910 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
3912 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
3913 if (format == GST_VIDEO_FORMAT_UNKNOWN)
3914 goto unsupported_format;
3916 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
3917 goto invalid_dimensions;
3919 width = fmt.fmt.pix.width;
3920 height = fmt.fmt.pix.height;
3922 /* Use the default compose rectangle */
3923 memset (&sel, 0, sizeof (struct v4l2_selection));
3924 sel.type = v4l2object->type;
3925 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
3926 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0) {
3929 /* For ancient kernels, fall back to G_CROP */
3930 memset (&crop, 0, sizeof (struct v4l2_crop));
3931 crop.type = v4l2object->type;
3932 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
3936 align.padding_left = r->left;
3937 align.padding_top = r->top;
3938 align.padding_right = width - r->width - r->left;
3939 align.padding_bottom = height - r->height - r->top;
3944 gst_video_info_set_format (info, format, width, height);
3946 switch (fmt.fmt.pix.field) {
3947 case V4L2_FIELD_ANY:
3948 case V4L2_FIELD_NONE:
3949 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
3951 case V4L2_FIELD_INTERLACED:
3952 case V4L2_FIELD_INTERLACED_TB:
3953 case V4L2_FIELD_INTERLACED_BT:
3954 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
3957 goto unsupported_field;
3960 gst_v4l2_object_get_colorspace (&fmt, &info->colorimetry);
3962 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
3964 /* Shall we setup the pool ? */
3970 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3971 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
3976 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3977 (_("Video device returned invalid dimensions.")),
3978 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
3979 fmt.fmt.pix.height));
3984 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3985 (_("Video device uses an unsupported interlacing method.")),
3986 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
3991 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3992 (_("Video device uses an unsupported pixel format.")),
3993 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
3994 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
4000 gst_v4l2_object_set_crop (GstV4l2Object * obj)
4002 struct v4l2_selection sel = { 0 };
4003 struct v4l2_crop crop = { 0 };
4005 sel.type = obj->type;
4006 sel.target = V4L2_SEL_TGT_CROP;
4008 sel.r.left = obj->align.padding_left;
4009 sel.r.top = obj->align.padding_top;
4010 sel.r.width = obj->info.width;
4011 sel.r.height = obj->info.height;
4013 crop.type = obj->type;
4016 if (obj->align.padding_left + obj->align.padding_top +
4017 obj->align.padding_right + obj->align.padding_bottom == 0) {
4018 GST_DEBUG_OBJECT (obj->dbg_obj, "no cropping needed");
4022 GST_DEBUG_OBJECT (obj->dbg_obj,
4023 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4024 crop.c.width, crop.c.height);
4026 if (obj->ioctl (obj->video_fd, VIDIOC_S_SELECTION, &sel) < 0) {
4027 if (errno != ENOTTY) {
4028 GST_WARNING_OBJECT (obj->dbg_obj,
4029 "Failed to set crop rectangle with VIDIOC_S_SELECTION: %s",
4030 g_strerror (errno));
4033 if (obj->ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
4034 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_S_CROP failed");
4038 if (obj->ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
4039 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_G_CROP failed");
4047 GST_DEBUG_OBJECT (obj->dbg_obj,
4048 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4049 crop.c.width, crop.c.height);
4055 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
4057 GstStructure *config;
4061 if (!v4l2object->pool)
4064 config = gst_buffer_pool_get_config (v4l2object->pool);
4065 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4067 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
4069 gst_structure_free (config);
4075 gst_v4l2_object_caps_is_subset (GstV4l2Object * v4l2object, GstCaps * caps)
4077 GstStructure *config;
4081 if (!v4l2object->pool)
4084 config = gst_buffer_pool_get_config (v4l2object->pool);
4085 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4087 ret = oldcaps && gst_caps_is_subset (oldcaps, caps);
4089 gst_structure_free (config);
4095 gst_v4l2_object_get_current_caps (GstV4l2Object * v4l2object)
4097 GstStructure *config;
4100 if (!v4l2object->pool)
4103 config = gst_buffer_pool_get_config (v4l2object->pool);
4104 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4107 gst_caps_ref (oldcaps);
4109 gst_structure_free (config);
4115 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
4117 gboolean ret = TRUE;
4119 GST_LOG_OBJECT (v4l2object->dbg_obj, "start flushing");
4121 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
4122 gst_buffer_pool_set_flushing (v4l2object->pool, TRUE);
4128 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
4130 gboolean ret = TRUE;
4132 GST_LOG_OBJECT (v4l2object->dbg_obj, "stop flushing");
4134 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
4135 gst_buffer_pool_set_flushing (v4l2object->pool, FALSE);
4141 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
4143 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "stopping");
4145 if (!GST_V4L2_IS_OPEN (v4l2object))
4147 if (!GST_V4L2_IS_ACTIVE (v4l2object))
4150 if (v4l2object->pool) {
4151 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "deactivating pool");
4152 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
4153 gst_object_unref (v4l2object->pool);
4154 v4l2object->pool = NULL;
4157 GST_V4L2_SET_INACTIVE (v4l2object);
4164 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4170 formats = gst_v4l2_object_get_format_list (v4l2object);
4172 ret = gst_caps_new_empty ();
4174 if (v4l2object->keep_aspect && !v4l2object->par) {
4175 struct v4l2_cropcap cropcap;
4177 memset (&cropcap, 0, sizeof (cropcap));
4179 cropcap.type = v4l2object->type;
4180 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0) {
4181 if (errno != ENOTTY)
4182 GST_WARNING_OBJECT (v4l2object->dbg_obj,
4183 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
4184 g_strerror (errno));
4186 v4l2object->par = g_new0 (GValue, 1);
4187 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
4188 gst_value_set_fraction (v4l2object->par, cropcap.pixelaspect.numerator,
4189 cropcap.pixelaspect.denominator);
4193 for (walk = formats; walk; walk = walk->next) {
4194 struct v4l2_fmtdesc *format;
4195 GstStructure *template;
4198 format = (struct v4l2_fmtdesc *) walk->data;
4200 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
4203 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
4204 "unknown format %" GST_FOURCC_FORMAT,
4205 GST_FOURCC_ARGS (format->pixelformat));
4209 /* If we have a filter, check if we need to probe this format or not */
4211 GstCaps *format_caps = gst_caps_new_empty ();
4213 gst_caps_append_structure (format_caps, gst_structure_copy (template));
4215 if (!gst_caps_can_intersect (format_caps, filter)) {
4216 gst_caps_unref (format_caps);
4217 gst_structure_free (template);
4221 gst_caps_unref (format_caps);
4224 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
4225 format->pixelformat, template);
4227 gst_caps_append (ret, tmp);
4229 gst_structure_free (template);
4236 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
4237 gst_caps_unref (tmp);
4240 GST_INFO_OBJECT (v4l2object->dbg_obj, "probed caps: %" GST_PTR_FORMAT, ret);
4246 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4250 if (v4l2object->probed_caps == NULL)
4251 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
4254 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
4255 GST_CAPS_INTERSECT_FIRST);
4257 ret = gst_caps_ref (v4l2object->probed_caps);
4264 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
4267 GstBufferPool *pool = NULL, *other_pool = NULL;
4268 GstStructure *config;
4269 guint size, min, max, own_min = 0;
4271 gboolean has_video_meta;
4272 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
4273 GstAllocator *allocator = NULL;
4274 GstAllocationParams params = { 0 };
4276 GST_DEBUG_OBJECT (obj->dbg_obj, "decide allocation");
4278 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4279 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
4281 gst_query_parse_allocation (query, &caps, NULL);
4283 if (obj->pool == NULL) {
4284 if (!gst_v4l2_object_setup_pool (obj, caps))
4288 if (gst_query_get_n_allocation_params (query) > 0)
4289 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
4291 if (gst_query_get_n_allocation_pools (query) > 0) {
4292 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
4301 GST_DEBUG_OBJECT (obj->dbg_obj, "allocation: size:%u min:%u max:%u pool:%"
4302 GST_PTR_FORMAT, size, min, max, pool);
4305 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4307 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
4309 gst_v4l2_get_driver_min_buffers (obj);
4310 /* We can't share our own pool, if it exceed V4L2 capacity */
4311 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
4312 can_share_own_pool = FALSE;
4315 switch (obj->mode) {
4316 case GST_V4L2_IO_RW:
4318 /* in READ/WRITE mode, prefer a downstream pool because our own pool
4319 * doesn't help much, we have to write to it as well */
4320 GST_DEBUG_OBJECT (obj->dbg_obj,
4321 "read/write mode: using downstream pool");
4322 /* use the bigest size, when we use our own pool we can't really do any
4323 * other size than what the hardware gives us but for downstream pools
4325 size = MAX (size, obj->info.size);
4326 } else if (can_share_own_pool) {
4327 /* no downstream pool, use our own then */
4328 GST_DEBUG_OBJECT (obj->dbg_obj,
4329 "read/write mode: no downstream pool, using our own");
4330 pool = gst_object_ref (obj->pool);
4331 size = obj->info.size;
4332 pushing_from_our_pool = TRUE;
4336 case GST_V4L2_IO_USERPTR:
4337 case GST_V4L2_IO_DMABUF_IMPORT:
4338 /* in importing mode, prefer our own pool, and pass the other pool to
4339 * our own, so it can serve itself */
4341 goto no_downstream_pool;
4342 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj->pool),
4345 gst_object_unref (pool);
4346 pool = gst_object_ref (obj->pool);
4347 size = obj->info.size;
4350 case GST_V4L2_IO_MMAP:
4351 case GST_V4L2_IO_DMABUF:
4352 /* in streaming mode, prefer our own pool */
4353 /* Check if we can use it ... */
4354 if (can_share_own_pool) {
4356 gst_object_unref (pool);
4357 pool = gst_object_ref (obj->pool);
4358 size = obj->info.size;
4359 GST_DEBUG_OBJECT (obj->dbg_obj,
4360 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
4361 pushing_from_our_pool = TRUE;
4363 GST_DEBUG_OBJECT (obj->dbg_obj,
4364 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
4367 GST_DEBUG_OBJECT (obj->dbg_obj,
4368 "streaming mode: no usable pool, copying to generic pool");
4369 size = MAX (size, obj->info.size);
4372 case GST_V4L2_IO_AUTO:
4374 GST_WARNING_OBJECT (obj->dbg_obj, "unhandled mode");
4381 /* If pushing from our own pool, configure it with queried minimum,
4382 * otherwise use the minimum required */
4383 if (pushing_from_our_pool) {
4384 /* When pushing from our own pool, we need what downstream one, to be able
4385 * to fill the pipeline, the minimum required to decoder according to the
4386 * driver and 2 more, so we don't endup up with everything downstream or
4387 * held by the decoder. We account 2 buffers for v4l2 so when one is being
4388 * pushed downstream the other one can already be queued for the next
4390 own_min = min + obj->min_buffers + 2;
4392 /* If no allocation parameters where provided, allow for a little more
4393 * buffers and enable copy threshold */
4396 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4399 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4404 /* In this case we'll have to configure two buffer pool. For our buffer
4405 * pool, we'll need what the driver one, and one more, so we can dequeu */
4406 own_min = obj->min_buffers + 1;
4407 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS);
4409 /* for the downstream pool, we keep what downstream wants, though ensure
4410 * at least a minimum if downstream didn't suggest anything (we are
4411 * expecting the base class to create a default one for the context) */
4412 min = MAX (min, GST_V4L2_MIN_BUFFERS);
4414 /* To import we need the other pool to hold at least own_min */
4415 if (obj->pool == pool)
4419 /* Request a bigger max, if one was suggested but it's too small */
4421 max = MAX (min, max);
4423 /* First step, configure our own pool */
4424 config = gst_buffer_pool_get_config (obj->pool);
4426 if (obj->need_video_meta || has_video_meta) {
4427 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
4428 gst_buffer_pool_config_add_option (config,
4429 GST_BUFFER_POOL_OPTION_VIDEO_META);
4432 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4433 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
4435 GST_DEBUG_OBJECT (obj->dbg_obj, "setting own pool config to %"
4436 GST_PTR_FORMAT, config);
4438 /* Our pool often need to adjust the value */
4439 if (!gst_buffer_pool_set_config (obj->pool, config)) {
4440 config = gst_buffer_pool_get_config (obj->pool);
4442 GST_DEBUG_OBJECT (obj->dbg_obj, "own pool config changed to %"
4443 GST_PTR_FORMAT, config);
4445 /* our pool will adjust the maximum buffer, which we are fine with */
4446 if (!gst_buffer_pool_set_config (obj->pool, config))
4450 /* Now configure the other pool if different */
4451 if (obj->pool != pool)
4455 config = gst_buffer_pool_get_config (other_pool);
4456 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4457 gst_buffer_pool_config_set_params (config, caps, size, min, max);
4459 GST_DEBUG_OBJECT (obj->dbg_obj, "setting other pool config to %"
4460 GST_PTR_FORMAT, config);
4462 /* if downstream supports video metadata, add this to the pool config */
4463 if (has_video_meta) {
4464 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
4465 gst_buffer_pool_config_add_option (config,
4466 GST_BUFFER_POOL_OPTION_VIDEO_META);
4469 if (!gst_buffer_pool_set_config (other_pool, config)) {
4470 config = gst_buffer_pool_get_config (other_pool);
4472 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
4474 gst_structure_free (config);
4478 if (!gst_buffer_pool_set_config (other_pool, config))
4484 /* For simplicity, simply read back the active configuration, so our base
4485 * class get the right information */
4486 config = gst_buffer_pool_get_config (pool);
4487 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
4488 gst_structure_free (config);
4492 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
4494 gst_query_add_allocation_pool (query, pool, size, min, max);
4497 gst_object_unref (allocator);
4500 gst_object_unref (pool);
4506 /* setup_pool already send the error */
4511 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4512 (_("Failed to configure internal buffer pool.")), (NULL));
4517 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4518 (_("Video device did not suggest any buffer size.")), (NULL));
4524 gst_object_unref (allocator);
4527 gst_object_unref (pool);
4532 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4533 (_("No downstream pool to import from.")),
4534 ("When importing DMABUF or USERPTR, we need a pool to import from"));
4540 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
4542 GstBufferPool *pool;
4543 /* we need at least 2 buffers to operate */
4544 guint size, min, max;
4548 /* Set defaults allocation parameters */
4549 size = obj->info.size;
4550 min = GST_V4L2_MIN_BUFFERS;
4551 max = VIDEO_MAX_FRAME;
4553 gst_query_parse_allocation (query, &caps, &need_pool);
4558 if ((pool = obj->pool))
4559 gst_object_ref (pool);
4563 GstStructure *config;
4565 /* we had a pool, check caps */
4566 config = gst_buffer_pool_get_config (pool);
4567 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
4569 GST_DEBUG_OBJECT (obj->dbg_obj,
4570 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
4571 if (!gst_caps_is_equal (caps, pcaps)) {
4572 gst_structure_free (config);
4573 gst_object_unref (pool);
4574 goto different_caps;
4576 gst_structure_free (config);
4578 gst_v4l2_get_driver_min_buffers (obj);
4580 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS);
4582 gst_query_add_allocation_pool (query, pool, size, min, max);
4584 /* we also support various metadata */
4585 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4588 gst_object_unref (pool);
4595 GST_DEBUG_OBJECT (obj->dbg_obj, "no caps specified");
4600 /* different caps, we can't use this pool */
4601 GST_DEBUG_OBJECT (obj->dbg_obj, "pool has different caps");
4607 gst_v4l2_object_try_import (GstV4l2Object * obj, GstBuffer * buffer)
4609 GstVideoMeta *vmeta;
4610 guint n_mem = gst_buffer_n_memory (buffer);
4612 /* only import if requested */
4613 switch (obj->mode) {
4614 case GST_V4L2_IO_USERPTR:
4615 case GST_V4L2_IO_DMABUF_IMPORT:
4618 GST_DEBUG_OBJECT (obj->dbg_obj,
4619 "The io-mode does not enable importation");
4623 vmeta = gst_buffer_get_video_meta (buffer);
4624 if (!vmeta && obj->need_video_meta) {
4625 GST_DEBUG_OBJECT (obj->dbg_obj, "Downstream buffer uses standard "
4626 "stride/offset while the driver does not.");
4630 /* we need matching strides/offsets and size */
4633 gboolean need_fmt_update = FALSE;
4635 if (vmeta->n_planes != GST_VIDEO_INFO_N_PLANES (&obj->info)) {
4636 GST_WARNING_OBJECT (obj->dbg_obj,
4637 "Cannot import buffers with different number planes");
4641 for (p = 0; p < vmeta->n_planes; p++) {
4642 if (vmeta->stride[p] < obj->info.stride[p]) {
4643 GST_DEBUG_OBJECT (obj->dbg_obj,
4644 "Not importing as remote stride %i is smaller then %i on plane %u",
4645 vmeta->stride[p], obj->info.stride[p], p);
4647 } else if (vmeta->stride[p] > obj->info.stride[p]) {
4648 need_fmt_update = TRUE;
4651 if (vmeta->offset[p] < obj->info.offset[p]) {
4652 GST_DEBUG_OBJECT (obj->dbg_obj,
4653 "Not importing as offset %" G_GSIZE_FORMAT
4654 " is smaller then %" G_GSIZE_FORMAT " on plane %u",
4655 vmeta->offset[p], obj->info.offset[p], p);
4657 } else if (vmeta->offset[p] > obj->info.offset[p]) {
4658 need_fmt_update = TRUE;
4662 if (need_fmt_update) {
4663 struct v4l2_format format;
4664 gint wanted_stride[GST_VIDEO_MAX_PLANES] = { 0, };
4666 format = obj->format;
4668 /* update the current format with the stride we want to import from */
4669 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
4672 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted strides:");
4674 for (i = 0; i < obj->n_v4l2_planes; i++) {
4675 gint stride = vmeta->stride[i];
4677 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4678 stride = GST_VIDEO_TILE_X_TILES (stride) <<
4679 GST_VIDEO_FORMAT_INFO_TILE_WS (obj->info.finfo);
4681 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
4682 wanted_stride[i] = stride;
4683 GST_DEBUG_OBJECT (obj->dbg_obj, " [%u] %i", i, wanted_stride[i]);
4686 gint stride = vmeta->stride[0];
4688 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted stride: %i", stride);
4690 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4691 stride = GST_VIDEO_TILE_X_TILES (stride) <<
4692 GST_VIDEO_FORMAT_INFO_TILE_WS (obj->info.finfo);
4694 format.fmt.pix.bytesperline = stride;
4695 wanted_stride[0] = stride;
4698 if (obj->ioctl (obj->video_fd, VIDIOC_S_FMT, &format) < 0) {
4699 GST_WARNING_OBJECT (obj->dbg_obj,
4700 "Something went wrong trying to update current format: %s",
4701 g_strerror (errno));
4705 gst_v4l2_object_save_format (obj, obj->fmtdesc, &format, &obj->info,
4708 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
4711 for (i = 0; i < obj->n_v4l2_planes; i++) {
4712 if (format.fmt.pix_mp.plane_fmt[i].bytesperline != wanted_stride[i]) {
4713 GST_DEBUG_OBJECT (obj->dbg_obj,
4714 "[%i] Driver did not accept the new stride (wants %i, got %i)",
4715 i, format.fmt.pix_mp.plane_fmt[i].bytesperline,
4721 if (format.fmt.pix.bytesperline != wanted_stride[0]) {
4722 GST_DEBUG_OBJECT (obj->dbg_obj,
4723 "Driver did not accept the new stride (wants %i, got %i)",
4724 format.fmt.pix.bytesperline, wanted_stride[0]);
4731 /* we can always import single memory buffer, but otherwise we need the same
4732 * amount of memory object. */
4733 if (n_mem != 1 && n_mem != obj->n_v4l2_planes) {
4734 GST_DEBUG_OBJECT (obj->dbg_obj, "Can only import %i memory, "
4735 "buffers contains %u memory", obj->n_v4l2_planes, n_mem);
4739 /* For DMABuf importation we need DMABuf of course */
4740 if (obj->mode == GST_V4L2_IO_DMABUF_IMPORT) {
4743 for (i = 0; i < n_mem; i++) {
4744 GstMemory *mem = gst_buffer_peek_memory (buffer, i);
4746 if (!gst_is_dmabuf_memory (mem)) {
4747 GST_DEBUG_OBJECT (obj->dbg_obj, "Cannot import non-DMABuf memory.");
4753 /* for the remaining, only the kernel driver can tell */