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_BGRA32, TRUE, GST_V4L2_RAW},
98 {V4L2_PIX_FMT_BGRX32, TRUE, GST_V4L2_RAW},
99 {V4L2_PIX_FMT_RGBA32, TRUE, GST_V4L2_RAW},
100 {V4L2_PIX_FMT_RGBX32, TRUE, GST_V4L2_RAW},
101 {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
102 {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},
104 /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
105 {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
106 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
107 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
108 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
109 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
112 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
113 {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
114 {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
115 {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
116 {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
117 {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
118 {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
119 {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},
121 /* Palette formats */
122 {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},
124 /* Chrominance formats */
125 {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},
127 /* Luminance+Chrominance formats */
128 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
129 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
130 {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
131 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
132 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
133 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
134 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
135 {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
136 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
137 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
138 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
139 {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
140 {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
141 {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
142 {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
143 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
144 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
145 {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
146 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
147 {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
148 {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
150 /* two planes -- one Y, one Cr + Cb interleaved */
151 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
152 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
153 {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
154 {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
155 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
156 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
158 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
159 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
160 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
161 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
162 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
163 {V4L2_PIX_FMT_MM21, TRUE, GST_V4L2_RAW},
165 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
166 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_RAW},
167 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_RAW},
168 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_RAW},
169 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_RAW},
171 /* compressed formats */
172 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
173 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
174 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
175 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
176 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
177 {V4L2_PIX_FMT_FWHT, FALSE, GST_V4L2_CODEC},
178 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
179 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
180 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
181 {V4L2_PIX_FMT_HEVC, FALSE, GST_V4L2_CODEC},
182 {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
183 {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
184 {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
185 {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
186 {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
187 {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
188 {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
189 {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
190 {V4L2_PIX_FMT_VP9, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
192 /* Vendor-specific formats */
193 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
194 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
195 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
196 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
199 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
201 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
204 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
206 gst_v4l2_device_get_type (void)
208 static GType v4l2_device_type = 0;
210 if (v4l2_device_type == 0) {
211 static const GFlagsValue values[] = {
212 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
213 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
214 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
216 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
217 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
219 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
220 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
226 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
229 return v4l2_device_type;
232 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
234 gst_v4l2_tv_norm_get_type (void)
236 static GType v4l2_tv_norm = 0;
239 static const GEnumValue tv_norms[] = {
242 {V4L2_STD_NTSC, "NTSC", "NTSC"},
243 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
244 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
245 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
246 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
248 {V4L2_STD_PAL, "PAL", "PAL"},
249 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
250 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
251 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
252 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
253 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
254 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
255 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
256 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
257 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
258 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
259 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
260 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
261 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
262 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
264 {V4L2_STD_SECAM, "SECAM", "SECAM"},
265 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
266 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
267 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
268 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
269 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
270 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
271 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
272 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
273 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
278 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
285 gst_v4l2_io_mode_get_type (void)
287 static GType v4l2_io_mode = 0;
290 static const GEnumValue io_modes[] = {
291 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
292 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
293 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
294 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
295 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
296 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
301 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
307 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
308 const char *default_device)
310 g_object_class_install_property (gobject_class, PROP_DEVICE,
311 g_param_spec_string ("device", "Device", "Device location",
312 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
313 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
314 g_param_spec_string ("device-name", "Device name",
315 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
316 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
317 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
318 g_param_spec_int ("device-fd", "File descriptor",
319 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
320 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (gobject_class, PROP_FLAGS,
322 g_param_spec_flags ("flags", "Flags", "Device type flags",
323 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
324 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
327 * GstV4l2Src:brightness:
329 * Picture brightness, or more precisely, the black level
331 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
332 g_param_spec_int ("brightness", "Brightness",
333 "Picture brightness, or more precisely, the black level", G_MININT,
335 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
337 * GstV4l2Src:contrast:
339 * Picture contrast or luma gain
341 g_object_class_install_property (gobject_class, PROP_CONTRAST,
342 g_param_spec_int ("contrast", "Contrast",
343 "Picture contrast or luma gain", G_MININT,
345 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
347 * GstV4l2Src:saturation:
349 * Picture color saturation or chroma gain
351 g_object_class_install_property (gobject_class, PROP_SATURATION,
352 g_param_spec_int ("saturation", "Saturation",
353 "Picture color saturation or chroma gain", G_MININT,
355 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
359 * Hue or color balance
361 g_object_class_install_property (gobject_class, PROP_HUE,
362 g_param_spec_int ("hue", "Hue",
363 "Hue or color balance", G_MININT,
365 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
372 g_object_class_install_property (gobject_class, PROP_TV_NORM,
373 g_param_spec_enum ("norm", "TV norm",
375 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
376 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
379 * GstV4l2Src:io-mode:
383 g_object_class_install_property (gobject_class, PROP_IO_MODE,
384 g_param_spec_enum ("io-mode", "IO mode",
386 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
387 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
390 * GstV4l2Src:extra-controls:
392 * Additional v4l2 controls for the device. The controls are identified
393 * by the control name (lowercase with '_' for any non-alphanumeric
398 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
399 g_param_spec_boxed ("extra-controls", "Extra Controls",
400 "Extra v4l2 controls (CIDs) for the device",
401 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
404 * GstV4l2Src:pixel-aspect-ratio:
406 * The pixel aspect ratio of the device. This overwrites the pixel aspect
407 * ratio queried from the device.
411 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
412 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
413 "Overwrite the pixel aspect ratio of the device", "1/1",
414 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
417 * GstV4l2Src:force-aspect-ratio:
419 * When enabled, the pixel aspect ratio queried from the device or set
420 * with the pixel-aspect-ratio property will be enforced.
424 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
425 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
426 "When enabled, the pixel aspect ratio will be enforced", TRUE,
427 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
429 gst_type_mark_as_plugin_api (GST_TYPE_V4L2_DEVICE_FLAGS, 0);
430 gst_type_mark_as_plugin_api (GST_TYPE_V4L2_TV_NORM, 0);
431 gst_type_mark_as_plugin_api (GST_TYPE_V4L2_IO_MODE, 0);
435 gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
437 g_object_class_install_property (gobject_class, PROP_DEVICE,
438 g_param_spec_string ("device", "Device", "Device location",
439 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
441 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
442 g_param_spec_string ("device-name", "Device name",
443 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
444 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
446 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
447 g_param_spec_int ("device-fd", "File descriptor",
448 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
449 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
451 g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
452 g_param_spec_enum ("output-io-mode", "Output IO mode",
453 "Output side I/O mode (matches sink pad)",
454 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
455 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
457 g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
458 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
459 "Capture I/O mode (matches src pad)",
460 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
461 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
463 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
464 g_param_spec_boxed ("extra-controls", "Extra Controls",
465 "Extra v4l2 controls (CIDs) for the device",
466 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
469 /* Support for 32bit off_t, this wrapper is casting off_t to gint64 */
474 v4l2_mmap_wrapper (gpointer start, gsize length, gint prot, gint flags, gint fd,
477 return v4l2_mmap (start, length, prot, flags, fd, (gint64) offset);
480 #define v4l2_mmap v4l2_mmap_wrapper
482 #endif /* SIZEOF_OFF_T < 8 */
483 #endif /* HAVE_LIBV4L2 */
486 gst_v4l2_object_new (GstElement * element,
487 GstObject * debug_object,
488 enum v4l2_buf_type type,
489 const char *default_device,
490 GstV4l2GetInOutFunction get_in_out_func,
491 GstV4l2SetInOutFunction set_in_out_func,
492 GstV4l2UpdateFpsFunction update_fps_func)
494 GstV4l2Object *v4l2object;
497 * some default values
499 v4l2object = g_new0 (GstV4l2Object, 1);
501 v4l2object->type = type;
502 v4l2object->formats = NULL;
504 v4l2object->element = element;
505 v4l2object->dbg_obj = debug_object;
506 v4l2object->get_in_out_func = get_in_out_func;
507 v4l2object->set_in_out_func = set_in_out_func;
508 v4l2object->update_fps_func = update_fps_func;
510 v4l2object->video_fd = -1;
511 v4l2object->active = FALSE;
512 v4l2object->videodev = g_strdup (default_device);
514 v4l2object->norms = NULL;
515 v4l2object->channels = NULL;
516 v4l2object->colors = NULL;
518 v4l2object->keep_aspect = TRUE;
520 v4l2object->n_v4l2_planes = 0;
522 v4l2object->no_initial_format = FALSE;
524 /* We now disable libv4l2 by default, but have an env to enable it. */
526 if (g_getenv ("GST_V4L2_USE_LIBV4L2")) {
527 v4l2object->fd_open = v4l2_fd_open;
528 v4l2object->close = v4l2_close;
529 v4l2object->dup = v4l2_dup;
530 v4l2object->ioctl = v4l2_ioctl;
531 v4l2object->read = v4l2_read;
532 v4l2object->mmap = v4l2_mmap;
533 v4l2object->munmap = v4l2_munmap;
537 v4l2object->fd_open = NULL;
538 v4l2object->close = close;
539 v4l2object->dup = dup;
540 v4l2object->ioctl = ioctl;
541 v4l2object->read = read;
542 v4l2object->mmap = mmap;
543 v4l2object->munmap = munmap;
549 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
553 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
555 g_return_if_fail (v4l2object != NULL);
557 g_free (v4l2object->videodev);
558 g_free (v4l2object->par);
559 g_free (v4l2object->channel);
561 if (v4l2object->formats) {
562 gst_v4l2_object_clear_format_list (v4l2object);
565 if (v4l2object->probed_caps) {
566 gst_caps_unref (v4l2object->probed_caps);
569 if (v4l2object->extra_controls) {
570 gst_structure_free (v4l2object->extra_controls);
578 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
580 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
581 g_slist_free (v4l2object->formats);
582 v4l2object->formats = NULL;
588 gst_v4l2_object_prop_to_cid (guint prop_id)
593 case PROP_BRIGHTNESS:
594 cid = V4L2_CID_BRIGHTNESS;
597 cid = V4L2_CID_CONTRAST;
599 case PROP_SATURATION:
600 cid = V4L2_CID_SATURATION;
606 GST_WARNING ("unmapped property id: %d", prop_id);
613 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
614 guint prop_id, const GValue * value, GParamSpec * pspec)
618 g_free (v4l2object->videodev);
619 v4l2object->videodev = g_value_dup_string (value);
621 case PROP_BRIGHTNESS:
623 case PROP_SATURATION:
626 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
629 if (GST_V4L2_IS_OPEN (v4l2object)) {
630 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
637 v4l2object->tv_norm = g_value_get_enum (value);
641 if (GST_V4L2_IS_OPEN (v4l2object)) {
642 GstTuner *tuner = GST_TUNER (v4l2object->element);
643 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
644 (gchar *) g_value_get_string (value));
647 /* like gst_tuner_set_channel (tuner, channel)
648 without g_object_notify */
649 gst_v4l2_tuner_set_channel (v4l2object, channel);
652 g_free (v4l2object->channel);
653 v4l2object->channel = g_value_dup_string (value);
657 if (GST_V4L2_IS_OPEN (v4l2object)) {
658 GstTuner *tuner = GST_TUNER (v4l2object->element);
659 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
662 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
664 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
665 without g_object_notify */
666 gst_v4l2_tuner_set_frequency (v4l2object, channel,
667 g_value_get_ulong (value));
670 v4l2object->frequency = g_value_get_ulong (value);
676 v4l2object->req_mode = g_value_get_enum (value);
678 case PROP_CAPTURE_IO_MODE:
679 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
680 v4l2object->req_mode = g_value_get_enum (value);
682 case PROP_OUTPUT_IO_MODE:
683 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
684 v4l2object->req_mode = g_value_get_enum (value);
686 case PROP_EXTRA_CONTROLS:{
687 const GstStructure *s = gst_value_get_structure (value);
689 if (v4l2object->extra_controls)
690 gst_structure_free (v4l2object->extra_controls);
692 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
693 if (GST_V4L2_IS_OPEN (v4l2object))
694 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
697 case PROP_PIXEL_ASPECT_RATIO:
698 if (v4l2object->par) {
699 g_value_unset (v4l2object->par);
700 g_free (v4l2object->par);
702 v4l2object->par = g_new0 (GValue, 1);
703 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
704 if (!g_value_transform (value, v4l2object->par)) {
705 g_warning ("Could not transform string to aspect ratio");
706 gst_value_set_fraction (v4l2object->par, 1, 1);
709 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "set PAR to %d/%d",
710 gst_value_get_fraction_numerator (v4l2object->par),
711 gst_value_get_fraction_denominator (v4l2object->par));
713 case PROP_FORCE_ASPECT_RATIO:
714 v4l2object->keep_aspect = g_value_get_boolean (value);
725 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
726 guint prop_id, GValue * value, GParamSpec * pspec)
730 g_value_set_string (value, v4l2object->videodev);
732 case PROP_DEVICE_NAME:
734 const guchar *name = NULL;
736 if (GST_V4L2_IS_OPEN (v4l2object))
737 name = v4l2object->vcap.card;
739 g_value_set_string (value, (gchar *) name);
744 if (GST_V4L2_IS_OPEN (v4l2object))
745 g_value_set_int (value, v4l2object->video_fd);
747 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
754 if (GST_V4L2_IS_OPEN (v4l2object)) {
755 flags |= v4l2object->device_caps &
756 (V4L2_CAP_VIDEO_CAPTURE |
757 V4L2_CAP_VIDEO_OUTPUT |
758 V4L2_CAP_VIDEO_OVERLAY |
759 V4L2_CAP_VBI_CAPTURE |
760 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
762 if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
763 flags |= V4L2_CAP_VIDEO_CAPTURE;
765 if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
766 flags |= V4L2_CAP_VIDEO_OUTPUT;
768 g_value_set_flags (value, flags);
771 case PROP_BRIGHTNESS:
773 case PROP_SATURATION:
776 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
779 if (GST_V4L2_IS_OPEN (v4l2object)) {
781 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
782 g_value_set_int (value, v);
790 g_value_set_enum (value, v4l2object->tv_norm);
793 g_value_set_enum (value, v4l2object->req_mode);
795 case PROP_CAPTURE_IO_MODE:
796 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
797 g_value_set_enum (value, v4l2object->req_mode);
799 case PROP_OUTPUT_IO_MODE:
800 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
801 g_value_set_enum (value, v4l2object->req_mode);
803 case PROP_EXTRA_CONTROLS:
804 gst_value_set_structure (value, v4l2object->extra_controls);
806 case PROP_PIXEL_ASPECT_RATIO:
808 g_value_transform (v4l2object->par, value);
810 case PROP_FORCE_ASPECT_RATIO:
811 g_value_set_boolean (value, v4l2object->keep_aspect);
821 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
823 struct v4l2_control control = { 0, };
825 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
827 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
828 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
830 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
832 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
833 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
834 "driver requires a minimum of %d buffers", control.value);
835 v4l2object->min_buffers = control.value;
837 v4l2object->min_buffers = 0;
842 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
844 GstTunerNorm *norm = NULL;
845 GstTunerChannel *channel = NULL;
848 if (!GST_IS_TUNER (v4l2object->element))
851 tuner = GST_TUNER (v4l2object->element);
853 if (v4l2object->tv_norm)
854 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
855 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
856 "norm=%p", (guint64) v4l2object->tv_norm, norm);
858 gst_tuner_set_norm (tuner, norm);
861 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
863 v4l2object->tv_norm =
864 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
865 gst_tuner_norm_changed (tuner, norm);
869 if (v4l2object->channel)
870 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
872 gst_tuner_set_channel (tuner, channel);
875 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
876 (v4l2object->element)));
878 g_free (v4l2object->channel);
879 v4l2object->channel = g_strdup (channel->label);
880 gst_tuner_channel_changed (tuner, channel);
885 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
886 if (v4l2object->frequency != 0) {
887 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
889 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
890 if (v4l2object->frequency == 0) {
892 gst_tuner_set_frequency (tuner, channel, 1000);
900 gst_v4l2_object_open (GstV4l2Object * v4l2object, GstV4l2Error * error)
902 if (gst_v4l2_open (v4l2object, error))
903 gst_v4l2_set_defaults (v4l2object);
911 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
915 ret = gst_v4l2_dup (v4l2object, other);
921 gst_v4l2_object_close (GstV4l2Object * v4l2object)
923 if (!gst_v4l2_close (v4l2object))
926 gst_caps_replace (&v4l2object->probed_caps, NULL);
928 /* reset our copy of the device caps */
929 v4l2object->device_caps = 0;
931 if (v4l2object->formats) {
932 gst_v4l2_object_clear_format_list (v4l2object);
935 if (v4l2object->par) {
936 g_value_unset (v4l2object->par);
937 g_free (v4l2object->par);
938 v4l2object->par = NULL;
941 if (v4l2object->channel) {
942 g_free (v4l2object->channel);
943 v4l2object->channel = NULL;
949 static struct v4l2_fmtdesc *
950 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
953 struct v4l2_fmtdesc *fmt;
959 walk = gst_v4l2_object_get_format_list (v4l2object);
961 fmt = (struct v4l2_fmtdesc *) walk->data;
962 if (fmt->pixelformat == fourcc)
964 /* special case for jpeg */
965 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
966 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
967 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
968 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
969 fourcc == V4L2_PIX_FMT_PJPG) {
973 walk = g_slist_next (walk);
981 /* complete made up ranking, the values themselves are meaningless */
982 /* These ranks MUST be X such that X<<15 fits on a signed int - see
983 the comment at the end of gst_v4l2_object_format_get_rank. */
984 #define YUV_BASE_RANK 1000
985 #define JPEG_BASE_RANK 500
986 #define DV_BASE_RANK 200
987 #define RGB_BASE_RANK 100
988 #define YUV_ODD_BASE_RANK 50
989 #define RGB_ODD_BASE_RANK 25
990 #define BAYER_BASE_RANK 15
991 #define S910_BASE_RANK 10
992 #define GREY_BASE_RANK 5
993 #define PWC_BASE_RANK 1
996 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
998 guint32 fourcc = fmt->pixelformat;
999 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
1003 case V4L2_PIX_FMT_MJPEG:
1004 case V4L2_PIX_FMT_PJPG:
1005 rank = JPEG_BASE_RANK;
1007 case V4L2_PIX_FMT_JPEG:
1008 rank = JPEG_BASE_RANK + 1;
1010 case V4L2_PIX_FMT_MPEG: /* MPEG */
1011 rank = JPEG_BASE_RANK + 2;
1014 case V4L2_PIX_FMT_RGB332:
1015 case V4L2_PIX_FMT_ARGB555:
1016 case V4L2_PIX_FMT_XRGB555:
1017 case V4L2_PIX_FMT_RGB555:
1018 case V4L2_PIX_FMT_ARGB555X:
1019 case V4L2_PIX_FMT_XRGB555X:
1020 case V4L2_PIX_FMT_RGB555X:
1021 case V4L2_PIX_FMT_BGR666:
1022 case V4L2_PIX_FMT_RGB565:
1023 case V4L2_PIX_FMT_RGB565X:
1024 case V4L2_PIX_FMT_RGB444:
1025 case V4L2_PIX_FMT_Y4:
1026 case V4L2_PIX_FMT_Y6:
1027 case V4L2_PIX_FMT_Y10:
1028 case V4L2_PIX_FMT_Y12:
1029 case V4L2_PIX_FMT_Y10BPACK:
1030 case V4L2_PIX_FMT_YUV555:
1031 case V4L2_PIX_FMT_YUV565:
1032 case V4L2_PIX_FMT_YUV32:
1033 case V4L2_PIX_FMT_NV12MT_16X16:
1034 case V4L2_PIX_FMT_NV42:
1035 case V4L2_PIX_FMT_H264_MVC:
1036 rank = RGB_ODD_BASE_RANK;
1039 case V4L2_PIX_FMT_RGB24:
1040 case V4L2_PIX_FMT_BGR24:
1041 rank = RGB_BASE_RANK - 1;
1044 case V4L2_PIX_FMT_RGB32:
1045 case V4L2_PIX_FMT_BGR32:
1046 case V4L2_PIX_FMT_ABGR32:
1047 case V4L2_PIX_FMT_XBGR32:
1048 case V4L2_PIX_FMT_BGRA32:
1049 case V4L2_PIX_FMT_BGRX32:
1050 case V4L2_PIX_FMT_RGBA32:
1051 case V4L2_PIX_FMT_RGBX32:
1052 case V4L2_PIX_FMT_ARGB32:
1053 case V4L2_PIX_FMT_XRGB32:
1054 rank = RGB_BASE_RANK;
1057 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1058 rank = GREY_BASE_RANK;
1061 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
1062 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1063 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1064 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1065 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1066 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1067 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1068 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1069 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1070 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1071 case V4L2_PIX_FMT_MM21: /* NV12 Y 16x32, UV 16x16 tile */
1072 rank = YUV_ODD_BASE_RANK;
1075 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1076 rank = YUV_BASE_RANK + 3;
1078 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1079 rank = YUV_BASE_RANK + 2;
1081 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1082 case V4L2_PIX_FMT_YUV420M:
1083 rank = YUV_BASE_RANK + 7;
1085 case V4L2_PIX_FMT_NV12: /* Y/CbCr 4:2:0, 12 bits per pixel */
1086 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
1087 rank = YUV_BASE_RANK + 8;
1089 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1090 rank = YUV_BASE_RANK + 10;
1092 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1093 rank = YUV_BASE_RANK + 6;
1095 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1096 rank = YUV_BASE_RANK + 9;
1098 case V4L2_PIX_FMT_YUV444:
1099 rank = YUV_BASE_RANK + 6;
1101 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1102 rank = YUV_BASE_RANK + 5;
1104 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1105 rank = YUV_BASE_RANK + 4;
1107 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1108 rank = YUV_BASE_RANK + 8;
1111 case V4L2_PIX_FMT_DV:
1112 rank = DV_BASE_RANK;
1115 case V4L2_PIX_FMT_WNVA: /* Winnov hw compress */
1119 case V4L2_PIX_FMT_SBGGR8:
1120 case V4L2_PIX_FMT_SGBRG8:
1121 case V4L2_PIX_FMT_SGRBG8:
1122 case V4L2_PIX_FMT_SRGGB8:
1123 rank = BAYER_BASE_RANK;
1126 case V4L2_PIX_FMT_SN9C10X:
1127 rank = S910_BASE_RANK;
1130 case V4L2_PIX_FMT_PWC1:
1131 rank = PWC_BASE_RANK;
1133 case V4L2_PIX_FMT_PWC2:
1134 rank = PWC_BASE_RANK;
1142 /* All ranks are below 1<<15 so a shift by 15
1143 * will a) make all non-emulated formats larger
1144 * than emulated and b) will not overflow
1155 format_cmp_func (gconstpointer a, gconstpointer b)
1157 const struct v4l2_fmtdesc *fa = a;
1158 const struct v4l2_fmtdesc *fb = b;
1160 if (fa->pixelformat == fb->pixelformat)
1163 return gst_v4l2_object_format_get_rank (fb) -
1164 gst_v4l2_object_format_get_rank (fa);
1167 /******************************************************
1168 * gst_v4l2_object_fill_format_list():
1169 * create list of supported capture formats
1170 * return value: TRUE on success, FALSE on error
1171 ******************************************************/
1173 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1174 enum v4l2_buf_type type)
1177 struct v4l2_fmtdesc *format;
1179 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting src format enumerations");
1181 /* format enumeration */
1183 format = g_new0 (struct v4l2_fmtdesc, 1);
1186 format->type = type;
1188 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1189 if (errno == EINVAL) {
1191 break; /* end of enumeration */
1197 GST_LOG_OBJECT (v4l2object->dbg_obj, "index: %u", format->index);
1198 GST_LOG_OBJECT (v4l2object->dbg_obj, "type: %d", format->type);
1199 GST_LOG_OBJECT (v4l2object->dbg_obj, "flags: %08x", format->flags);
1200 GST_LOG_OBJECT (v4l2object->dbg_obj, "description: '%s'",
1201 format->description);
1202 GST_LOG_OBJECT (v4l2object->dbg_obj, "pixelformat: %" GST_FOURCC_FORMAT,
1203 GST_FOURCC_ARGS (format->pixelformat));
1205 /* sort formats according to our preference; we do this, because caps
1206 * are probed in the order the formats are in the list, and the order of
1207 * formats in the final probed caps matters for things like fixation */
1208 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1209 (GCompareFunc) format_cmp_func);
1212 #ifndef GST_DISABLE_GST_DEBUG
1216 GST_INFO_OBJECT (v4l2object->dbg_obj, "got %d format(s):", n);
1217 for (l = v4l2object->formats; l != NULL; l = l->next) {
1220 GST_INFO_OBJECT (v4l2object->dbg_obj,
1221 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1222 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1234 if (v4l2object->element)
1237 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1238 (_("Failed to enumerate possible video formats device '%s' can work "
1239 "with"), v4l2object->videodev),
1240 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1241 n, v4l2object->videodev, errno, g_strerror (errno)));
1248 * Get the list of supported capture formats, a list of
1249 * `struct v4l2_fmtdesc`.
1252 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1254 if (!v4l2object->formats) {
1256 /* check usual way */
1257 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1259 /* if our driver supports multi-planar
1260 * and if formats are still empty then we can workaround driver bug
1261 * by also looking up formats as if our device was not supporting
1263 if (!v4l2object->formats) {
1264 switch (v4l2object->type) {
1265 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1266 gst_v4l2_object_fill_format_list (v4l2object,
1267 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1270 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1271 gst_v4l2_object_fill_format_list (v4l2object,
1272 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1280 return v4l2object->formats;
1283 static GstVideoFormat
1284 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1286 GstVideoFormat format;
1289 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1290 format = GST_VIDEO_FORMAT_GRAY8;
1292 case V4L2_PIX_FMT_Y16:
1293 format = GST_VIDEO_FORMAT_GRAY16_LE;
1295 case V4L2_PIX_FMT_Y16_BE:
1296 format = GST_VIDEO_FORMAT_GRAY16_BE;
1298 case V4L2_PIX_FMT_XRGB555:
1299 case V4L2_PIX_FMT_RGB555:
1300 format = GST_VIDEO_FORMAT_RGB15;
1302 case V4L2_PIX_FMT_XRGB555X:
1303 case V4L2_PIX_FMT_RGB555X:
1304 format = GST_VIDEO_FORMAT_BGR15;
1306 case V4L2_PIX_FMT_RGB565:
1307 format = GST_VIDEO_FORMAT_RGB16;
1309 case V4L2_PIX_FMT_RGB24:
1310 format = GST_VIDEO_FORMAT_RGB;
1312 case V4L2_PIX_FMT_BGR24:
1313 format = GST_VIDEO_FORMAT_BGR;
1315 case V4L2_PIX_FMT_XRGB32:
1316 case V4L2_PIX_FMT_RGB32:
1317 format = GST_VIDEO_FORMAT_xRGB;
1319 case V4L2_PIX_FMT_RGBX32:
1320 format = GST_VIDEO_FORMAT_RGBx;
1322 case V4L2_PIX_FMT_XBGR32:
1323 case V4L2_PIX_FMT_BGR32:
1324 format = GST_VIDEO_FORMAT_BGRx;
1326 case V4L2_PIX_FMT_BGRX32:
1327 format = GST_VIDEO_FORMAT_xBGR;
1329 case V4L2_PIX_FMT_ABGR32:
1330 format = GST_VIDEO_FORMAT_BGRA;
1332 case V4L2_PIX_FMT_BGRA32:
1333 format = GST_VIDEO_FORMAT_ABGR;
1335 case V4L2_PIX_FMT_RGBA32:
1336 format = GST_VIDEO_FORMAT_RGBA;
1338 case V4L2_PIX_FMT_ARGB32:
1339 format = GST_VIDEO_FORMAT_ARGB;
1341 case V4L2_PIX_FMT_NV12:
1342 case V4L2_PIX_FMT_NV12M:
1343 format = GST_VIDEO_FORMAT_NV12;
1345 case V4L2_PIX_FMT_NV12MT:
1346 format = GST_VIDEO_FORMAT_NV12_64Z32;
1348 case V4L2_PIX_FMT_MM21:
1349 format = GST_VIDEO_FORMAT_NV12_16L32S;
1351 case V4L2_PIX_FMT_NV21:
1352 case V4L2_PIX_FMT_NV21M:
1353 format = GST_VIDEO_FORMAT_NV21;
1355 case V4L2_PIX_FMT_YVU410:
1356 format = GST_VIDEO_FORMAT_YVU9;
1358 case V4L2_PIX_FMT_YUV410:
1359 format = GST_VIDEO_FORMAT_YUV9;
1361 case V4L2_PIX_FMT_YUV420:
1362 case V4L2_PIX_FMT_YUV420M:
1363 format = GST_VIDEO_FORMAT_I420;
1365 case V4L2_PIX_FMT_YUYV:
1366 format = GST_VIDEO_FORMAT_YUY2;
1368 case V4L2_PIX_FMT_YVU420:
1369 format = GST_VIDEO_FORMAT_YV12;
1371 case V4L2_PIX_FMT_UYVY:
1372 format = GST_VIDEO_FORMAT_UYVY;
1374 case V4L2_PIX_FMT_YUV411P:
1375 format = GST_VIDEO_FORMAT_Y41B;
1377 case V4L2_PIX_FMT_YUV422P:
1378 format = GST_VIDEO_FORMAT_Y42B;
1380 case V4L2_PIX_FMT_YVYU:
1381 format = GST_VIDEO_FORMAT_YVYU;
1383 case V4L2_PIX_FMT_NV16:
1384 case V4L2_PIX_FMT_NV16M:
1385 format = GST_VIDEO_FORMAT_NV16;
1387 case V4L2_PIX_FMT_NV61:
1388 case V4L2_PIX_FMT_NV61M:
1389 format = GST_VIDEO_FORMAT_NV61;
1391 case V4L2_PIX_FMT_NV24:
1392 format = GST_VIDEO_FORMAT_NV24;
1395 format = GST_VIDEO_FORMAT_UNKNOWN;
1403 gst_v4l2_object_v4l2fourcc_is_rgb (guint32 fourcc)
1405 gboolean ret = FALSE;
1408 case V4L2_PIX_FMT_XRGB555:
1409 case V4L2_PIX_FMT_RGB555:
1410 case V4L2_PIX_FMT_XRGB555X:
1411 case V4L2_PIX_FMT_RGB555X:
1412 case V4L2_PIX_FMT_RGB565:
1413 case V4L2_PIX_FMT_RGB24:
1414 case V4L2_PIX_FMT_BGR24:
1415 case V4L2_PIX_FMT_XRGB32:
1416 case V4L2_PIX_FMT_RGB32:
1417 case V4L2_PIX_FMT_RGBA32:
1418 case V4L2_PIX_FMT_RGBX32:
1419 case V4L2_PIX_FMT_XBGR32:
1420 case V4L2_PIX_FMT_BGR32:
1421 case V4L2_PIX_FMT_BGRA32:
1422 case V4L2_PIX_FMT_BGRX32:
1423 case V4L2_PIX_FMT_ABGR32:
1424 case V4L2_PIX_FMT_ARGB32:
1425 case V4L2_PIX_FMT_SBGGR8:
1426 case V4L2_PIX_FMT_SGBRG8:
1427 case V4L2_PIX_FMT_SGRBG8:
1428 case V4L2_PIX_FMT_SRGGB8:
1438 static GstStructure *
1439 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1441 GstStructure *structure = NULL;
1444 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1445 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1446 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1447 structure = gst_structure_new_empty ("image/jpeg");
1449 case V4L2_PIX_FMT_MPEG1:
1450 structure = gst_structure_new ("video/mpeg",
1451 "mpegversion", G_TYPE_INT, 1, NULL);
1453 case V4L2_PIX_FMT_MPEG2:
1454 structure = gst_structure_new ("video/mpeg",
1455 "mpegversion", G_TYPE_INT, 2, NULL);
1457 case V4L2_PIX_FMT_MPEG4:
1458 case V4L2_PIX_FMT_XVID:
1459 structure = gst_structure_new ("video/mpeg",
1460 "mpegversion", G_TYPE_INT, 4, "systemstream",
1461 G_TYPE_BOOLEAN, FALSE, NULL);
1463 case V4L2_PIX_FMT_FWHT:
1464 structure = gst_structure_new_empty ("video/x-fwht");
1466 case V4L2_PIX_FMT_H263:
1467 structure = gst_structure_new ("video/x-h263",
1468 "variant", G_TYPE_STRING, "itu", NULL);
1470 case V4L2_PIX_FMT_H264: /* H.264 */
1471 structure = gst_structure_new ("video/x-h264",
1472 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1473 G_TYPE_STRING, "au", NULL);
1475 case V4L2_PIX_FMT_H264_NO_SC:
1476 structure = gst_structure_new ("video/x-h264",
1477 "stream-format", G_TYPE_STRING, "avc", "alignment",
1478 G_TYPE_STRING, "au", NULL);
1480 case V4L2_PIX_FMT_HEVC: /* H.265 */
1481 structure = gst_structure_new ("video/x-h265",
1482 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1483 G_TYPE_STRING, "au", NULL);
1485 case V4L2_PIX_FMT_VC1_ANNEX_G:
1486 case V4L2_PIX_FMT_VC1_ANNEX_L:
1487 structure = gst_structure_new ("video/x-wmv",
1488 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1490 case V4L2_PIX_FMT_VP8:
1491 structure = gst_structure_new_empty ("video/x-vp8");
1493 case V4L2_PIX_FMT_VP9:
1494 structure = gst_structure_new_empty ("video/x-vp9");
1496 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1497 case V4L2_PIX_FMT_Y16:
1498 case V4L2_PIX_FMT_Y16_BE:
1499 case V4L2_PIX_FMT_XRGB555:
1500 case V4L2_PIX_FMT_RGB555:
1501 case V4L2_PIX_FMT_XRGB555X:
1502 case V4L2_PIX_FMT_RGB555X:
1503 case V4L2_PIX_FMT_RGB565:
1504 case V4L2_PIX_FMT_RGB24:
1505 case V4L2_PIX_FMT_BGR24:
1506 case V4L2_PIX_FMT_RGB32:
1507 case V4L2_PIX_FMT_XRGB32:
1508 case V4L2_PIX_FMT_ARGB32:
1509 case V4L2_PIX_FMT_RGBX32:
1510 case V4L2_PIX_FMT_RGBA32:
1511 case V4L2_PIX_FMT_BGR32:
1512 case V4L2_PIX_FMT_BGRX32:
1513 case V4L2_PIX_FMT_BGRA32:
1514 case V4L2_PIX_FMT_XBGR32:
1515 case V4L2_PIX_FMT_ABGR32:
1516 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1517 case V4L2_PIX_FMT_NV12M:
1518 case V4L2_PIX_FMT_NV12MT:
1519 case V4L2_PIX_FMT_MM21:
1520 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1521 case V4L2_PIX_FMT_NV21M:
1522 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1523 case V4L2_PIX_FMT_NV16M:
1524 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1525 case V4L2_PIX_FMT_NV61M:
1526 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1527 case V4L2_PIX_FMT_YVU410:
1528 case V4L2_PIX_FMT_YUV410:
1529 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1530 case V4L2_PIX_FMT_YUV420M:
1531 case V4L2_PIX_FMT_YUYV:
1532 case V4L2_PIX_FMT_YVU420:
1533 case V4L2_PIX_FMT_UYVY:
1534 case V4L2_PIX_FMT_YUV422P:
1535 case V4L2_PIX_FMT_YVYU:
1536 case V4L2_PIX_FMT_YUV411P:{
1537 GstVideoFormat format;
1538 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1539 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1540 structure = gst_structure_new ("video/x-raw",
1541 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1544 case V4L2_PIX_FMT_DV:
1546 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1549 case V4L2_PIX_FMT_MPEG: /* MPEG */
1550 structure = gst_structure_new ("video/mpegts",
1551 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1553 case V4L2_PIX_FMT_WNVA: /* Winnov hw compress */
1555 case V4L2_PIX_FMT_SBGGR8:
1556 case V4L2_PIX_FMT_SGBRG8:
1557 case V4L2_PIX_FMT_SGRBG8:
1558 case V4L2_PIX_FMT_SRGGB8:
1559 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1560 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1561 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1562 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1563 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1565 case V4L2_PIX_FMT_SN9C10X:
1566 structure = gst_structure_new_empty ("video/x-sonix");
1568 case V4L2_PIX_FMT_PWC1:
1569 structure = gst_structure_new_empty ("video/x-pwc1");
1571 case V4L2_PIX_FMT_PWC2:
1572 structure = gst_structure_new_empty ("video/x-pwc2");
1574 case V4L2_PIX_FMT_RGB332:
1575 case V4L2_PIX_FMT_BGR666:
1576 case V4L2_PIX_FMT_ARGB555X:
1577 case V4L2_PIX_FMT_RGB565X:
1578 case V4L2_PIX_FMT_RGB444:
1579 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1580 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1581 case V4L2_PIX_FMT_Y4:
1582 case V4L2_PIX_FMT_Y6:
1583 case V4L2_PIX_FMT_Y10:
1584 case V4L2_PIX_FMT_Y12:
1585 case V4L2_PIX_FMT_Y10BPACK:
1586 case V4L2_PIX_FMT_YUV444:
1587 case V4L2_PIX_FMT_YUV555:
1588 case V4L2_PIX_FMT_YUV565:
1589 case V4L2_PIX_FMT_Y41P:
1590 case V4L2_PIX_FMT_YUV32:
1591 case V4L2_PIX_FMT_NV12MT_16X16:
1592 case V4L2_PIX_FMT_NV42:
1593 case V4L2_PIX_FMT_H264_MVC:
1595 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1596 fourcc, GST_FOURCC_ARGS (fourcc));
1604 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1606 GstStructure *template;
1609 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1611 if (template == NULL)
1614 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1615 if (gst_v4l2_formats[i].format != fourcc)
1618 if (gst_v4l2_formats[i].dimensions) {
1619 gst_structure_set (template,
1620 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1621 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1622 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1631 /* Add an 'alternate' variant of the caps with the feature */
1633 add_alternate_variant (GstV4l2Object * v4l2object, GstCaps * caps,
1634 GstStructure * structure)
1636 GstStructure *alt_s;
1638 if (v4l2object && v4l2object->never_interlaced)
1641 if (!gst_structure_has_name (structure, "video/x-raw"))
1644 alt_s = gst_structure_copy (structure);
1645 gst_structure_set (alt_s, "interlace-mode", G_TYPE_STRING, "alternate", NULL);
1647 gst_caps_append_structure_full (caps, alt_s,
1648 gst_caps_features_new (GST_CAPS_FEATURE_FORMAT_INTERLACED, NULL));
1652 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1654 GstStructure *structure;
1655 GstCaps *caps, *caps_interlaced;
1658 caps = gst_caps_new_empty ();
1659 caps_interlaced = gst_caps_new_empty ();
1660 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1662 if ((gst_v4l2_formats[i].flags & flags) == 0)
1666 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1669 GstStructure *alt_s = NULL;
1671 if (gst_v4l2_formats[i].dimensions) {
1672 gst_structure_set (structure,
1673 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1674 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1675 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1678 switch (gst_v4l2_formats[i].format) {
1679 case V4L2_PIX_FMT_RGB32:
1680 alt_s = gst_structure_copy (structure);
1681 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1683 case V4L2_PIX_FMT_BGR32:
1684 alt_s = gst_structure_copy (structure);
1685 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1690 gst_caps_append_structure (caps, structure);
1693 gst_caps_append_structure (caps, alt_s);
1694 add_alternate_variant (NULL, caps_interlaced, alt_s);
1697 add_alternate_variant (NULL, caps_interlaced, structure);
1701 caps = gst_caps_simplify (caps);
1702 caps_interlaced = gst_caps_simplify (caps_interlaced);
1704 return gst_caps_merge (caps, caps_interlaced);
1708 gst_v4l2_object_get_all_caps (void)
1710 static GstCaps *caps = NULL;
1712 if (g_once_init_enter (&caps)) {
1713 GstCaps *all_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1714 GST_MINI_OBJECT_FLAG_SET (all_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1715 g_once_init_leave (&caps, all_caps);
1722 gst_v4l2_object_get_raw_caps (void)
1724 static GstCaps *caps = NULL;
1726 if (g_once_init_enter (&caps)) {
1727 GstCaps *raw_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1728 GST_MINI_OBJECT_FLAG_SET (raw_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1729 g_once_init_leave (&caps, raw_caps);
1736 gst_v4l2_object_get_codec_caps (void)
1738 static GstCaps *caps = NULL;
1740 if (g_once_init_enter (&caps)) {
1741 GstCaps *codec_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1742 GST_MINI_OBJECT_FLAG_SET (codec_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1743 g_once_init_leave (&caps, codec_caps);
1749 /* collect data for the given caps
1750 * @caps: given input caps
1751 * @format: location for the v4l format
1752 * @w/@h: location for width and height
1753 * @fps_n/@fps_d: location for framerate
1754 * @size: location for expected size of the frame or 0 if unknown
1757 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1758 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1760 GstStructure *structure;
1761 guint32 fourcc = 0, fourcc_nc = 0;
1762 const gchar *mimetype;
1763 struct v4l2_fmtdesc *fmt = NULL;
1765 structure = gst_caps_get_structure (caps, 0);
1767 mimetype = gst_structure_get_name (structure);
1769 if (!gst_video_info_from_caps (info, caps))
1770 goto invalid_format;
1772 if (g_str_equal (mimetype, "video/x-raw")) {
1773 switch (GST_VIDEO_INFO_FORMAT (info)) {
1774 case GST_VIDEO_FORMAT_I420:
1775 fourcc = V4L2_PIX_FMT_YUV420;
1776 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1778 case GST_VIDEO_FORMAT_YUY2:
1779 fourcc = V4L2_PIX_FMT_YUYV;
1781 case GST_VIDEO_FORMAT_UYVY:
1782 fourcc = V4L2_PIX_FMT_UYVY;
1784 case GST_VIDEO_FORMAT_YV12:
1785 fourcc = V4L2_PIX_FMT_YVU420;
1787 case GST_VIDEO_FORMAT_Y41B:
1788 fourcc = V4L2_PIX_FMT_YUV411P;
1790 case GST_VIDEO_FORMAT_Y42B:
1791 fourcc = V4L2_PIX_FMT_YUV422P;
1793 case GST_VIDEO_FORMAT_NV12:
1794 fourcc = V4L2_PIX_FMT_NV12;
1795 fourcc_nc = V4L2_PIX_FMT_NV12M;
1797 case GST_VIDEO_FORMAT_NV12_64Z32:
1798 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1800 case GST_VIDEO_FORMAT_NV12_16L32S:
1801 fourcc_nc = V4L2_PIX_FMT_MM21;
1803 case GST_VIDEO_FORMAT_NV21:
1804 fourcc = V4L2_PIX_FMT_NV21;
1805 fourcc_nc = V4L2_PIX_FMT_NV21M;
1807 case GST_VIDEO_FORMAT_NV16:
1808 fourcc = V4L2_PIX_FMT_NV16;
1809 fourcc_nc = V4L2_PIX_FMT_NV16M;
1811 case GST_VIDEO_FORMAT_NV61:
1812 fourcc = V4L2_PIX_FMT_NV61;
1813 fourcc_nc = V4L2_PIX_FMT_NV61M;
1815 case GST_VIDEO_FORMAT_NV24:
1816 fourcc = V4L2_PIX_FMT_NV24;
1818 case GST_VIDEO_FORMAT_YVYU:
1819 fourcc = V4L2_PIX_FMT_YVYU;
1821 case GST_VIDEO_FORMAT_RGB15:
1822 fourcc = V4L2_PIX_FMT_RGB555;
1823 fourcc_nc = V4L2_PIX_FMT_XRGB555;
1825 case GST_VIDEO_FORMAT_RGB16:
1826 fourcc = V4L2_PIX_FMT_RGB565;
1828 case GST_VIDEO_FORMAT_RGB:
1829 fourcc = V4L2_PIX_FMT_RGB24;
1831 case GST_VIDEO_FORMAT_BGR:
1832 fourcc = V4L2_PIX_FMT_BGR24;
1834 case GST_VIDEO_FORMAT_xRGB:
1835 fourcc = V4L2_PIX_FMT_RGB32;
1836 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1838 case GST_VIDEO_FORMAT_RGBx:
1839 fourcc = V4L2_PIX_FMT_RGBX32;
1841 case GST_VIDEO_FORMAT_ARGB:
1842 fourcc = V4L2_PIX_FMT_RGB32;
1843 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1845 case GST_VIDEO_FORMAT_RGBA:
1846 fourcc = V4L2_PIX_FMT_RGBA32;
1848 case GST_VIDEO_FORMAT_BGRx:
1849 fourcc = V4L2_PIX_FMT_BGR32;
1850 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1852 case GST_VIDEO_FORMAT_xBGR:
1853 fourcc = V4L2_PIX_FMT_BGRX32;
1855 case GST_VIDEO_FORMAT_BGRA:
1856 fourcc = V4L2_PIX_FMT_BGR32;
1857 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1859 case GST_VIDEO_FORMAT_ABGR:
1860 fourcc = V4L2_PIX_FMT_BGRA32;
1862 case GST_VIDEO_FORMAT_GRAY8:
1863 fourcc = V4L2_PIX_FMT_GREY;
1865 case GST_VIDEO_FORMAT_GRAY16_LE:
1866 fourcc = V4L2_PIX_FMT_Y16;
1868 case GST_VIDEO_FORMAT_GRAY16_BE:
1869 fourcc = V4L2_PIX_FMT_Y16_BE;
1871 case GST_VIDEO_FORMAT_BGR15:
1872 fourcc = V4L2_PIX_FMT_RGB555X;
1873 fourcc_nc = V4L2_PIX_FMT_XRGB555X;
1879 if (g_str_equal (mimetype, "video/mpegts")) {
1880 fourcc = V4L2_PIX_FMT_MPEG;
1881 } else if (g_str_equal (mimetype, "video/x-dv")) {
1882 fourcc = V4L2_PIX_FMT_DV;
1883 } else if (g_str_equal (mimetype, "image/jpeg")) {
1884 fourcc = V4L2_PIX_FMT_JPEG;
1885 } else if (g_str_equal (mimetype, "video/mpeg")) {
1887 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1890 fourcc = V4L2_PIX_FMT_MPEG1;
1893 fourcc = V4L2_PIX_FMT_MPEG2;
1896 fourcc = V4L2_PIX_FMT_MPEG4;
1897 fourcc_nc = V4L2_PIX_FMT_XVID;
1903 } else if (g_str_equal (mimetype, "video/x-fwht")) {
1904 fourcc = V4L2_PIX_FMT_FWHT;
1905 } else if (g_str_equal (mimetype, "video/x-h263")) {
1906 fourcc = V4L2_PIX_FMT_H263;
1907 } else if (g_str_equal (mimetype, "video/x-h264")) {
1908 const gchar *stream_format =
1909 gst_structure_get_string (structure, "stream-format");
1910 if (g_str_equal (stream_format, "avc"))
1911 fourcc = V4L2_PIX_FMT_H264_NO_SC;
1913 fourcc = V4L2_PIX_FMT_H264;
1914 } else if (g_str_equal (mimetype, "video/x-h265")) {
1915 fourcc = V4L2_PIX_FMT_HEVC;
1916 } else if (g_str_equal (mimetype, "video/x-vp8")) {
1917 fourcc = V4L2_PIX_FMT_VP8;
1918 } else if (g_str_equal (mimetype, "video/x-vp9")) {
1919 fourcc = V4L2_PIX_FMT_VP9;
1920 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1921 const gchar *format = gst_structure_get_string (structure, "format");
1923 if (!g_ascii_strcasecmp (format, "bggr"))
1924 fourcc = V4L2_PIX_FMT_SBGGR8;
1925 else if (!g_ascii_strcasecmp (format, "gbrg"))
1926 fourcc = V4L2_PIX_FMT_SGBRG8;
1927 else if (!g_ascii_strcasecmp (format, "grbg"))
1928 fourcc = V4L2_PIX_FMT_SGRBG8;
1929 else if (!g_ascii_strcasecmp (format, "rggb"))
1930 fourcc = V4L2_PIX_FMT_SRGGB8;
1932 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1933 fourcc = V4L2_PIX_FMT_SN9C10X;
1934 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1935 fourcc = V4L2_PIX_FMT_PWC1;
1936 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1937 fourcc = V4L2_PIX_FMT_PWC2;
1942 /* Prefer the non-contiguous if supported */
1943 v4l2object->prefered_non_contiguous = TRUE;
1946 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
1947 else if (fourcc == 0)
1948 goto unhandled_format;
1951 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1952 v4l2object->prefered_non_contiguous = FALSE;
1956 goto unsupported_format;
1965 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1970 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1975 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1981 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1982 guint32 pixelformat, gint * width, gint * height);
1985 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1987 if (v4l2object->keep_aspect && v4l2object->par)
1988 gst_structure_set_value (s, "pixel-aspect-ratio", v4l2object->par);
1991 /* returns TRUE if the value was changed in place, otherwise FALSE */
1993 gst_v4l2src_value_simplify (GValue * val)
1995 /* simplify list of one value to one value */
1996 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
1997 const GValue *list_val;
1998 GValue new_val = G_VALUE_INIT;
2000 list_val = gst_value_list_get_value (val, 0);
2001 g_value_init (&new_val, G_VALUE_TYPE (list_val));
2002 g_value_copy (list_val, &new_val);
2003 g_value_unset (val);
2012 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
2013 GstVideoInterlaceMode * interlace_mode)
2016 case V4L2_FIELD_ANY:
2018 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
2020 case V4L2_FIELD_NONE:
2021 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
2023 case V4L2_FIELD_INTERLACED:
2024 case V4L2_FIELD_INTERLACED_TB:
2025 case V4L2_FIELD_INTERLACED_BT:
2026 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
2028 case V4L2_FIELD_ALTERNATE:
2029 *interlace_mode = GST_VIDEO_INTERLACE_MODE_ALTERNATE;
2032 GST_ERROR ("Unknown enum v4l2_field %d", field);
2038 gst_v4l2_object_get_colorspace (GstV4l2Object * v4l2object,
2039 struct v4l2_format *fmt, GstVideoColorimetry * cinfo)
2042 gst_v4l2_object_v4l2fourcc_is_rgb (fmt->fmt.pix.pixelformat);
2043 enum v4l2_colorspace colorspace;
2044 enum v4l2_quantization range;
2045 enum v4l2_ycbcr_encoding matrix;
2046 enum v4l2_xfer_func transfer;
2047 gboolean ret = TRUE;
2049 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
2050 colorspace = fmt->fmt.pix_mp.colorspace;
2051 range = fmt->fmt.pix_mp.quantization;
2052 matrix = fmt->fmt.pix_mp.ycbcr_enc;
2053 transfer = fmt->fmt.pix_mp.xfer_func;
2055 colorspace = fmt->fmt.pix.colorspace;
2056 range = fmt->fmt.pix.quantization;
2057 matrix = fmt->fmt.pix.ycbcr_enc;
2058 transfer = fmt->fmt.pix.xfer_func;
2061 /* First step, set the defaults for each primaries */
2062 switch (colorspace) {
2063 case V4L2_COLORSPACE_SMPTE170M:
2064 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2065 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2066 cinfo->transfer = GST_VIDEO_TRANSFER_BT601;
2067 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
2069 case V4L2_COLORSPACE_REC709:
2070 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2071 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2072 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2073 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
2075 case V4L2_COLORSPACE_SRGB:
2076 case V4L2_COLORSPACE_JPEG:
2077 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2078 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2079 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2080 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
2082 case V4L2_COLORSPACE_OPRGB:
2083 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2084 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2085 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2086 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
2088 case V4L2_COLORSPACE_BT2020:
2089 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2090 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2091 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2092 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
2094 case V4L2_COLORSPACE_SMPTE240M:
2095 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2096 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2097 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2098 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
2100 case V4L2_COLORSPACE_470_SYSTEM_M:
2101 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2102 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2103 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2104 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
2106 case V4L2_COLORSPACE_470_SYSTEM_BG:
2107 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2108 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2109 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2110 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
2112 case V4L2_COLORSPACE_RAW:
2113 /* Explicitly unknown */
2114 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2115 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2116 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2117 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
2120 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
2128 /* Second step, apply any custom variation */
2130 case V4L2_QUANTIZATION_FULL_RANGE:
2131 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2133 case V4L2_QUANTIZATION_LIM_RANGE:
2134 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2136 case V4L2_QUANTIZATION_DEFAULT:
2137 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
2138 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
2139 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2140 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601
2141 || matrix == V4L2_YCBCR_ENC_XV709
2142 || colorspace == V4L2_COLORSPACE_JPEG)
2143 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2145 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2148 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
2149 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2154 case V4L2_YCBCR_ENC_XV601:
2155 case V4L2_YCBCR_ENC_SYCC:
2156 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
2158 case V4L2_YCBCR_ENC_601:
2159 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2161 case V4L2_YCBCR_ENC_XV709:
2162 GST_FIXME ("XV709 not defined, assuming 709");
2164 case V4L2_YCBCR_ENC_709:
2165 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2167 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2168 GST_FIXME ("BT2020 with constant luma is not defined, assuming BT2020");
2170 case V4L2_YCBCR_ENC_BT2020:
2171 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2173 case V4L2_YCBCR_ENC_SMPTE240M:
2174 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2176 case V4L2_YCBCR_ENC_DEFAULT:
2177 /* nothing, just use defaults for colorspace */
2180 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2181 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2185 /* Set identity matrix for R'G'B' formats to avoid creating
2186 * confusion. This though is cosmetic as it's now properly ignored by
2187 * the video info API and videoconvert. */
2189 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2192 case V4L2_XFER_FUNC_709:
2193 if (colorspace == V4L2_COLORSPACE_BT2020 && fmt->fmt.pix.height >= 2160)
2194 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2195 else if (colorspace == V4L2_COLORSPACE_SMPTE170M)
2196 cinfo->transfer = GST_VIDEO_TRANSFER_BT601;
2198 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2200 if (v4l2object->transfer)
2201 cinfo->transfer = v4l2object->transfer;
2203 case V4L2_XFER_FUNC_SRGB:
2204 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2206 case V4L2_XFER_FUNC_OPRGB:
2207 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2209 case V4L2_XFER_FUNC_SMPTE240M:
2210 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2212 case V4L2_XFER_FUNC_NONE:
2213 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2215 case V4L2_XFER_FUNC_SMPTE2084:
2216 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE2084;
2218 case V4L2_XFER_FUNC_DEFAULT:
2219 /* nothing, just use defaults for colorspace */
2222 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2223 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2232 gst_v4l2_object_get_streamparm (GstV4l2Object * v4l2object, GstVideoInfo * info)
2234 struct v4l2_streamparm streamparm;
2235 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
2236 streamparm.type = v4l2object->type;
2237 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_PARM, &streamparm) < 0) {
2238 GST_WARNING_OBJECT (v4l2object->dbg_obj, "VIDIOC_G_PARM failed");
2241 if ((streamparm.parm.capture.timeperframe.numerator != 0)
2242 && (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
2243 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2244 GST_VIDEO_INFO_FPS_N (info) =
2245 streamparm.parm.capture.timeperframe.denominator;
2246 GST_VIDEO_INFO_FPS_D (info) =
2247 streamparm.parm.capture.timeperframe.numerator;
2253 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2254 struct v4l2_format *try_fmt)
2256 int fd = v4l2object->video_fd;
2257 struct v4l2_format fmt;
2260 memcpy (&fmt, try_fmt, sizeof (fmt));
2261 r = v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2263 if (r < 0 && errno == ENOTTY) {
2264 /* The driver might not implement TRY_FMT, in which case we will try
2266 if (GST_V4L2_IS_ACTIVE (v4l2object))
2269 memcpy (&fmt, try_fmt, sizeof (fmt));
2270 r = v4l2object->ioctl (fd, VIDIOC_S_FMT, &fmt);
2272 memcpy (try_fmt, &fmt, sizeof (fmt));
2277 memcpy (try_fmt, &fmt, sizeof (fmt));
2278 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2279 "Unable to try format: %s", g_strerror (errno));
2285 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2286 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2288 struct v4l2_format fmt;
2289 GValue interlace_formats = { 0, };
2290 enum v4l2_field formats[] = { V4L2_FIELD_NONE,
2291 V4L2_FIELD_INTERLACED, V4L2_FIELD_ALTERNATE
2294 GstVideoInterlaceMode interlace_mode, prev = -1;
2296 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2299 if (v4l2object->never_interlaced) {
2300 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2304 g_value_init (&interlace_formats, GST_TYPE_LIST);
2306 /* Try thrice - once for NONE, once for INTERLACED and once for ALTERNATE. */
2307 for (i = 0; i < G_N_ELEMENTS (formats); i++) {
2308 memset (&fmt, 0, sizeof (fmt));
2309 fmt.type = v4l2object->type;
2310 fmt.fmt.pix.width = width;
2311 fmt.fmt.pix.height = height;
2312 fmt.fmt.pix.pixelformat = pixelformat;
2313 fmt.fmt.pix.field = formats[i];
2315 if (fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE)
2316 fmt.fmt.pix.height /= 2;
2318 /* if skip_try_fmt_probes is set it's up to the caller to filter out the
2319 * formats from the formats requested by peer.
2320 * For this negotiation to work with 'alternate' we need the caps to contain
2321 * the feature so we have an intersection with downstream caps.
2323 if (!v4l2object->skip_try_fmt_probes
2324 && gst_v4l2_object_try_fmt (v4l2object, &fmt) != 0)
2327 if (gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)
2328 && prev != interlace_mode) {
2329 GValue interlace_enum = { 0, };
2330 const gchar *mode_string;
2331 g_value_init (&interlace_enum, G_TYPE_STRING);
2332 mode_string = gst_video_interlace_mode_to_string (interlace_mode);
2333 g_value_set_string (&interlace_enum, mode_string);
2334 gst_value_list_append_and_take_value (&interlace_formats,
2336 prev = interlace_mode;
2340 if (gst_v4l2src_value_simplify (&interlace_formats)
2341 || gst_value_list_get_size (&interlace_formats) > 0)
2342 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2344 GST_WARNING_OBJECT (v4l2object, "Failed to determine interlace mode");
2350 gst_v4l2_object_fill_colorimetry_list (GValue * list,
2351 GstVideoColorimetry * cinfo)
2353 GValue colorimetry = G_VALUE_INIT;
2356 gboolean found = FALSE;
2358 g_value_init (&colorimetry, G_TYPE_STRING);
2359 g_value_take_string (&colorimetry, gst_video_colorimetry_to_string (cinfo));
2361 /* only insert if no duplicate */
2362 size = gst_value_list_get_size (list);
2363 for (i = 0; i < size; i++) {
2366 tmp = gst_value_list_get_value (list, i);
2367 if (gst_value_compare (&colorimetry, tmp) == GST_VALUE_EQUAL) {
2374 gst_value_list_append_and_take_value (list, &colorimetry);
2376 g_value_unset (&colorimetry);
2380 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2381 guint32 width, guint32 height, guint32 pixelformat)
2383 struct v4l2_format fmt;
2384 GValue list = G_VALUE_INIT;
2385 GstVideoColorimetry cinfo;
2386 enum v4l2_colorspace req_cspace;
2388 memset (&fmt, 0, sizeof (fmt));
2389 fmt.type = v4l2object->type;
2390 fmt.fmt.pix.width = width;
2391 fmt.fmt.pix.height = height;
2392 fmt.fmt.pix.pixelformat = pixelformat;
2394 g_value_init (&list, GST_TYPE_LIST);
2396 /* step 1: get device default colorspace and insert it first as
2397 * it should be the preferred one */
2398 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2399 if (gst_v4l2_object_get_colorspace (v4l2object, &fmt, &cinfo))
2400 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2403 /* step 2: probe all colorspace other than default
2404 * We don't probe all colorspace, range, matrix and transfer combination to
2405 * avoid ioctl flooding which could greatly increase initialization time
2406 * with low-speed devices (UVC...) */
2407 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2408 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++) {
2409 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2410 if (req_cspace == V4L2_COLORSPACE_BT878)
2413 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2414 fmt.fmt.pix_mp.colorspace = req_cspace;
2416 fmt.fmt.pix.colorspace = req_cspace;
2418 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2419 enum v4l2_colorspace colorspace;
2421 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2422 colorspace = fmt.fmt.pix_mp.colorspace;
2424 colorspace = fmt.fmt.pix.colorspace;
2426 if (colorspace == req_cspace) {
2427 if (gst_v4l2_object_get_colorspace (v4l2object, &fmt, &cinfo))
2428 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2433 if (gst_value_list_get_size (&list) > 0)
2434 gst_structure_take_value (s, "colorimetry", &list);
2436 g_value_unset (&list);
2441 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2442 static GstStructure *
2443 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2444 guint32 pixelformat,
2445 guint32 width, guint32 height, const GstStructure * template)
2447 gint fd = v4l2object->video_fd;
2448 struct v4l2_frmivalenum ival;
2451 GValue rates = { 0, };
2453 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2455 ival.pixel_format = pixelformat;
2457 ival.height = height;
2459 GST_LOG_OBJECT (v4l2object->dbg_obj,
2460 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2461 GST_FOURCC_ARGS (pixelformat));
2463 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2464 * fraction to get framerate */
2465 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2466 goto enum_frameintervals_failed;
2468 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2469 GValue rate = { 0, };
2471 g_value_init (&rates, GST_TYPE_LIST);
2472 g_value_init (&rate, GST_TYPE_FRACTION);
2475 num = ival.discrete.numerator;
2476 denom = ival.discrete.denominator;
2478 if (num > G_MAXINT || denom > G_MAXINT) {
2479 /* let us hope we don't get here... */
2484 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding discrete framerate: %d/%d",
2487 /* swap to get the framerate */
2488 gst_value_set_fraction (&rate, denom, num);
2489 gst_value_list_append_value (&rates, &rate);
2492 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2493 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2494 GValue min = { 0, };
2495 GValue step = { 0, };
2496 GValue max = { 0, };
2497 gboolean added = FALSE;
2498 guint32 minnum, mindenom;
2499 guint32 maxnum, maxdenom;
2501 g_value_init (&rates, GST_TYPE_LIST);
2503 g_value_init (&min, GST_TYPE_FRACTION);
2504 g_value_init (&step, GST_TYPE_FRACTION);
2505 g_value_init (&max, GST_TYPE_FRACTION);
2508 minnum = ival.stepwise.min.numerator;
2509 mindenom = ival.stepwise.min.denominator;
2510 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2514 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise min frame interval: %d/%d",
2516 gst_value_set_fraction (&min, minnum, mindenom);
2519 maxnum = ival.stepwise.max.numerator;
2520 maxdenom = ival.stepwise.max.denominator;
2521 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2526 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise max frame interval: %d/%d",
2528 gst_value_set_fraction (&max, maxnum, maxdenom);
2531 num = ival.stepwise.step.numerator;
2532 denom = ival.stepwise.step.denominator;
2533 if (num > G_MAXINT || denom > G_MAXINT) {
2538 if (num == 0 || denom == 0) {
2539 /* in this case we have a wrong fraction or no step, set the step to max
2540 * so that we only add the min value in the loop below */
2545 /* since we only have gst_value_fraction_subtract and not add, negate the
2547 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise step frame interval: %d/%d",
2549 gst_value_set_fraction (&step, -num, denom);
2551 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2552 GValue rate = { 0, };
2554 num = gst_value_get_fraction_numerator (&min);
2555 denom = gst_value_get_fraction_denominator (&min);
2556 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding stepwise framerate: %d/%d",
2559 /* invert to get the framerate */
2560 g_value_init (&rate, GST_TYPE_FRACTION);
2561 gst_value_set_fraction (&rate, denom, num);
2562 gst_value_list_append_value (&rates, &rate);
2565 /* we're actually adding because step was negated above. This is because
2566 * there is no _add function... */
2567 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2568 GST_WARNING_OBJECT (v4l2object->dbg_obj, "could not step fraction!");
2573 /* no range was added, leave the default range from the template */
2574 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2575 "no range added, leaving default");
2576 g_value_unset (&rates);
2578 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2579 guint32 maxnum, maxdenom;
2581 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2583 num = ival.stepwise.min.numerator;
2584 denom = ival.stepwise.min.denominator;
2585 if (num > G_MAXINT || denom > G_MAXINT) {
2590 maxnum = ival.stepwise.max.numerator;
2591 maxdenom = ival.stepwise.max.denominator;
2592 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2597 GST_LOG_OBJECT (v4l2object->dbg_obj,
2598 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2601 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2607 s = gst_structure_copy (template);
2608 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2609 "height", G_TYPE_INT, (gint) height, NULL);
2611 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2613 if (!v4l2object->skip_try_fmt_probes) {
2614 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2616 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2619 if (G_IS_VALUE (&rates)) {
2620 gst_v4l2src_value_simplify (&rates);
2621 /* only change the framerate on the template when we have a valid probed new
2623 gst_structure_take_value (s, "framerate", &rates);
2624 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2625 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2626 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2632 enum_frameintervals_failed:
2634 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2635 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2636 GST_FOURCC_ARGS (pixelformat), width, height);
2641 /* I don't see how this is actually an error, we ignore the format then */
2642 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2643 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2644 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2650 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2654 gst_structure_get_int (s1, "width", &w1);
2655 gst_structure_get_int (s1, "height", &h1);
2656 gst_structure_get_int (s2, "width", &w2);
2657 gst_structure_get_int (s2, "height", &h2);
2659 /* I think it's safe to assume that this won't overflow for a while */
2660 return ((w2 * h2) - (w1 * h1));
2664 check_alternate_and_append_struct (GstCaps * caps, GstStructure * s)
2668 mode = gst_structure_get_value (s, "interlace-mode");
2672 if (G_VALUE_HOLDS_STRING (mode)) {
2673 /* Add the INTERLACED feature if the mode is alternate */
2674 if (!g_strcmp0 (gst_structure_get_string (s, "interlace-mode"),
2676 GstCapsFeatures *feat;
2678 feat = gst_caps_features_new (GST_CAPS_FEATURE_FORMAT_INTERLACED, NULL);
2679 gst_caps_set_features (caps, gst_caps_get_size (caps) - 1, feat);
2681 } else if (GST_VALUE_HOLDS_LIST (mode)) {
2682 /* If the mode is a list containing alternate, remove it from the list and add a
2683 * variant with interlace-mode=alternate and the INTERLACED feature. */
2684 GValue alter = G_VALUE_INIT;
2685 GValue inter = G_VALUE_INIT;
2687 g_value_init (&alter, G_TYPE_STRING);
2688 g_value_set_string (&alter, "alternate");
2690 /* Cannot use gst_value_can_intersect() as it requires args to have the
2692 if (gst_value_intersect (&inter, mode, &alter)) {
2693 GValue minus_alter = G_VALUE_INIT;
2696 gst_value_subtract (&minus_alter, mode, &alter);
2697 gst_structure_take_value (s, "interlace-mode", &minus_alter);
2699 copy = gst_structure_copy (s);
2700 gst_structure_take_value (copy, "interlace-mode", &inter);
2701 gst_caps_append_structure_full (caps, copy,
2702 gst_caps_features_new (GST_CAPS_FEATURE_FORMAT_INTERLACED, NULL));
2704 g_value_unset (&alter);
2708 gst_caps_append_structure (caps, s);
2712 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2713 guint32 format, GstCaps * caps, GstStructure * s)
2715 GstStructure *alt_s = NULL;
2717 /* Encoded stream on output buffer need to be parsed */
2718 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2719 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2722 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2723 if (format == gst_v4l2_formats[i].format &&
2724 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2725 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2726 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2732 if (v4l2object->has_alpha_component &&
2733 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2734 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2736 case V4L2_PIX_FMT_RGB32:
2737 alt_s = gst_structure_copy (s);
2738 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2740 case V4L2_PIX_FMT_BGR32:
2741 alt_s = gst_structure_copy (s);
2742 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2749 check_alternate_and_append_struct (caps, s);
2752 check_alternate_and_append_struct (caps, alt_s);
2757 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2758 guint32 pixelformat, const GstStructure * template)
2760 GstCaps *ret = gst_caps_new_empty ();
2762 gint fd = v4l2object->video_fd;
2763 struct v4l2_frmsizeenum size;
2764 GList *results = NULL;
2767 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2768 gst_caps_append_structure (ret, gst_structure_copy (template));
2772 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2774 size.pixel_format = pixelformat;
2776 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2777 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2778 GST_FOURCC_ARGS (pixelformat));
2780 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2781 goto enum_framesizes_failed;
2783 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2785 GST_LOG_OBJECT (v4l2object->dbg_obj, "got discrete frame size %dx%d",
2786 size.discrete.width, size.discrete.height);
2788 w = MIN (size.discrete.width, G_MAXINT);
2789 h = MIN (size.discrete.height, G_MAXINT);
2793 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2794 pixelformat, w, h, template);
2797 results = g_list_prepend (results, tmp);
2801 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2802 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2803 "done iterating discrete frame sizes");
2804 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2805 guint32 maxw, maxh, step_w, step_h;
2807 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have stepwise frame sizes:");
2808 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2809 size.stepwise.min_width);
2810 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2811 size.stepwise.min_height);
2812 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2813 size.stepwise.max_width);
2814 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2815 size.stepwise.max_height);
2816 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step width: %d",
2817 size.stepwise.step_width);
2818 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step height: %d",
2819 size.stepwise.step_height);
2821 w = MAX (size.stepwise.min_width, 1);
2822 h = MAX (size.stepwise.min_height, 1);
2823 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2824 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2826 step_w = MAX (size.stepwise.step_width, 1);
2827 step_h = MAX (size.stepwise.step_height, 1);
2829 /* FIXME: check for sanity and that min/max are multiples of the steps */
2831 /* we only query details for the max width/height since it's likely the
2832 * most restricted if there are any resolution-dependent restrictions */
2833 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2834 pixelformat, maxw, maxh, template);
2837 GValue step_range = G_VALUE_INIT;
2839 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2840 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2841 gst_structure_set_value (tmp, "width", &step_range);
2843 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2844 gst_structure_take_value (tmp, "height", &step_range);
2846 /* no point using the results list here, since there's only one struct */
2847 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2849 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2852 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have continuous frame sizes:");
2853 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2854 size.stepwise.min_width);
2855 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2856 size.stepwise.min_height);
2857 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2858 size.stepwise.max_width);
2859 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2860 size.stepwise.max_height);
2862 w = MAX (size.stepwise.min_width, 1);
2863 h = MAX (size.stepwise.min_height, 1);
2864 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2865 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2868 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2871 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2872 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2875 /* no point using the results list here, since there's only one struct */
2876 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2882 /* we use an intermediary list to store and then sort the results of the
2883 * probing because we can't make any assumptions about the order in which
2884 * the driver will give us the sizes, but we want the final caps to contain
2885 * the results starting with the highest resolution and having the lowest
2886 * resolution last, since order in caps matters for things like fixation. */
2887 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2888 while (results != NULL) {
2889 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
2891 results = g_list_delete_link (results, results);
2894 if (gst_caps_is_empty (ret))
2895 goto enum_framesizes_no_results;
2900 enum_framesizes_failed:
2902 /* I don't see how this is actually an error */
2903 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2904 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2905 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2906 goto default_frame_sizes;
2908 enum_framesizes_no_results:
2910 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2911 * question doesn't actually support it yet */
2912 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2913 "No results for pixelformat %" GST_FOURCC_FORMAT
2914 " enumerating frame sizes, trying fallback",
2915 GST_FOURCC_ARGS (pixelformat));
2916 goto default_frame_sizes;
2920 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2921 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2922 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2923 goto default_frame_sizes;
2926 default_frame_sizes:
2928 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2930 /* This code is for Linux < 2.6.19 */
2932 max_w = max_h = GST_V4L2_MAX_SIZE;
2933 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2935 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2936 "Could not probe minimum capture size for pixelformat %"
2937 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2939 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2941 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2942 "Could not probe maximum capture size for pixelformat %"
2943 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2945 if (min_w == 0 || min_h == 0)
2947 if (max_w == 0 || max_h == 0)
2948 max_w = max_h = GST_V4L2_MAX_SIZE;
2950 /* Since we can't get framerate directly, try to use the current norm */
2951 if (v4l2object->tv_norm && v4l2object->norms) {
2953 GstTunerNorm *norm = NULL;
2954 GstTunerNorm *current =
2955 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2957 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2958 norm = (GstTunerNorm *) norms->data;
2959 if (!strcmp (norm->label, current->label))
2962 /* If it's possible, set framerate to that (discrete) value */
2964 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2965 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2969 tmp = gst_structure_copy (template);
2971 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2973 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2974 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2975 /* if norm can't be used, copy the template framerate */
2976 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2981 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2983 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2986 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2988 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2990 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2992 /* We could consider setting interlace mode from min and max. */
2993 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
2996 if (!v4l2object->skip_try_fmt_probes) {
2997 gint probed_w, probed_h;
2998 if (v4l2object->info.width >= min_w && v4l2object->info.width <= max_w &&
2999 v4l2object->info.height >= min_h
3000 && v4l2object->info.height <= max_h) {
3001 probed_w = v4l2object->info.width;
3002 probed_h = v4l2object->info.height;
3007 /* We could consider to check colorspace for min too, in case it depends on
3008 * the size. But in this case, min and max could not be enough */
3009 gst_v4l2_object_add_colorspace (v4l2object, tmp, probed_w, probed_h,
3013 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
3019 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
3020 guint32 pixelformat, gint * width, gint * height)
3022 struct v4l2_format fmt;
3023 gboolean ret = FALSE;
3024 GstVideoInterlaceMode interlace_mode;
3026 g_return_val_if_fail (width != NULL, FALSE);
3027 g_return_val_if_fail (height != NULL, FALSE);
3029 GST_LOG_OBJECT (v4l2object->dbg_obj,
3030 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
3031 *width, *height, GST_FOURCC_ARGS (pixelformat));
3033 memset (&fmt, 0, sizeof (struct v4l2_format));
3035 /* get size delimiters */
3036 memset (&fmt, 0, sizeof (fmt));
3037 fmt.type = v4l2object->type;
3038 fmt.fmt.pix.width = *width;
3039 fmt.fmt.pix.height = *height;
3040 fmt.fmt.pix.pixelformat = pixelformat;
3041 fmt.fmt.pix.field = V4L2_FIELD_ANY;
3043 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
3046 GST_LOG_OBJECT (v4l2object->dbg_obj,
3047 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
3049 *width = fmt.fmt.pix.width;
3050 *height = fmt.fmt.pix.height;
3052 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
3053 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3054 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
3055 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
3063 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3064 "Unable to try format: %s", g_strerror (errno));
3071 gst_v4l2_object_is_dmabuf_supported (GstV4l2Object * v4l2object)
3073 gboolean ret = TRUE;
3074 struct v4l2_exportbuffer expbuf = {
3075 .type = v4l2object->type,
3078 .flags = O_CLOEXEC | O_RDWR,
3081 if (v4l2object->fmtdesc &&
3082 v4l2object->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
3083 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3084 "libv4l2 converter detected, disabling DMABuf");
3088 /* Expected to fail, but ENOTTY tells us that it is not implemented. */
3089 v4l2object->ioctl (v4l2object->video_fd, VIDIOC_EXPBUF, &expbuf);
3090 if (errno == ENOTTY)
3097 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
3101 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "initializing the %s system",
3102 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
3104 GST_V4L2_CHECK_OPEN (v4l2object);
3105 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3107 /* find transport */
3108 mode = v4l2object->req_mode;
3110 if (v4l2object->device_caps & V4L2_CAP_READWRITE) {
3111 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
3112 mode = GST_V4L2_IO_RW;
3113 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
3114 goto method_not_supported;
3116 if (v4l2object->device_caps & V4L2_CAP_STREAMING) {
3117 if (v4l2object->req_mode == GST_V4L2_IO_AUTO) {
3118 if (!V4L2_TYPE_IS_OUTPUT (v4l2object->type) &&
3119 gst_v4l2_object_is_dmabuf_supported (v4l2object)) {
3120 mode = GST_V4L2_IO_DMABUF;
3122 mode = GST_V4L2_IO_MMAP;
3125 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP ||
3126 v4l2object->req_mode == GST_V4L2_IO_DMABUF)
3127 goto method_not_supported;
3129 /* if still no transport selected, error out */
3130 if (mode == GST_V4L2_IO_AUTO)
3131 goto no_supported_capture_method;
3133 GST_INFO_OBJECT (v4l2object->dbg_obj, "accessing buffers via mode %d", mode);
3134 v4l2object->mode = mode;
3136 /* If min_buffers is not set, the driver either does not support the control or
3137 it has not been asked yet via propose_allocation/decide_allocation. */
3138 if (!v4l2object->min_buffers)
3139 gst_v4l2_get_driver_min_buffers (v4l2object);
3141 /* Map the buffers */
3142 GST_LOG_OBJECT (v4l2object->dbg_obj, "initiating buffer pool");
3144 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
3145 goto buffer_pool_new_failed;
3147 GST_V4L2_SET_ACTIVE (v4l2object);
3152 buffer_pool_new_failed:
3154 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
3155 (_("Could not map buffers from device '%s'"),
3156 v4l2object->videodev),
3157 ("Failed to create buffer pool: %s", g_strerror (errno)));
3160 method_not_supported:
3162 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
3163 (_("The driver of device '%s' does not support the IO method %d"),
3164 v4l2object->videodev, mode), (NULL));
3167 no_supported_capture_method:
3169 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
3170 (_("The driver of device '%s' does not support any known IO "
3171 "method."), v4l2object->videodev), (NULL));
3177 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
3178 gint plane, gint stride)
3180 const GstVideoFormatInfo *finfo = info->finfo;
3182 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3183 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
3185 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
3186 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
3188 /* this only works for what we support, NV12 subsampled tiles */
3189 if (GST_VIDEO_FORMAT_INFO_HAS_SUBTILES (finfo) && plane == 1)
3192 tile_height = 1 << hs;
3194 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
3195 info->height + align->padding_top + align->padding_bottom);
3196 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3198 x_tiles = stride >> ws;
3199 y_tiles = padded_height >> hs;
3200 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
3202 info->stride[plane] = stride;
3207 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
3208 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
3210 const GstVideoFormatInfo *finfo = info->finfo;
3211 gint i, estride, padded_height;
3214 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
3217 GST_VIDEO_INFO_FIELD_HEIGHT (info) + align->padding_top +
3218 align->padding_bottom;
3220 for (i = 0; i < finfo->n_planes; i++) {
3221 estride = gst_video_format_info_extrapolate_stride (finfo, i, stride);
3223 gst_v4l2_object_set_stride (info, align, i, estride);
3225 info->offset[i] = offs;
3227 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
3229 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3230 "Extrapolated for plane %d with base stride %d: "
3231 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
3235 /* Update the image size according the amount of data we are going to
3236 * read/write. This workaround bugs in driver where the sizeimage provided
3237 * by TRY/S_FMT represent the buffer length (maximum size) rather then the expected
3238 * bytesused (buffer size). */
3239 if (offs < info->size)
3244 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
3245 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
3246 GstVideoInfo * info, GstVideoAlignment * align)
3248 const GstVideoFormatInfo *finfo = info->finfo;
3249 gboolean standard_stride = TRUE;
3250 gint stride, pstride, padded_width, padded_height, i;
3252 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
3253 v4l2object->n_v4l2_planes = 1;
3254 info->size = format->fmt.pix.sizeimage;
3258 /* adjust right padding */
3259 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
3260 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3262 stride = format->fmt.pix.bytesperline;
3264 pstride = GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
3266 padded_width = stride / pstride;
3268 /* pstride can be 0 for complex formats */
3269 GST_WARNING_OBJECT (v4l2object->element,
3270 "format %s has a pstride of 0, cannot compute padded with",
3271 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
3272 padded_width = stride;
3275 if (padded_width < format->fmt.pix.width)
3276 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3277 "Driver bug detected, stride (%d) is too small for the width (%d)",
3278 padded_width, format->fmt.pix.width);
3280 align->padding_right = padded_width - info->width - align->padding_left;
3282 /* adjust bottom padding */
3283 padded_height = format->fmt.pix.height;
3285 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3286 guint hs, tile_height;
3288 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
3289 tile_height = 1 << hs;
3291 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3294 align->padding_bottom =
3295 padded_height - GST_VIDEO_INFO_FIELD_HEIGHT (info) - align->padding_top;
3297 /* setup the strides and offset */
3298 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
3299 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
3301 /* figure out the frame layout */
3302 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
3304 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
3305 stride = pix_mp->plane_fmt[i].bytesperline;
3307 if (info->stride[i] != stride)
3308 standard_stride = FALSE;
3310 gst_v4l2_object_set_stride (info, align, i, stride);
3311 info->offset[i] = info->size;
3312 info->size += pix_mp->plane_fmt[i].sizeimage;
3315 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3316 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
3317 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3318 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3321 /* only one plane in non-MPLANE mode */
3322 v4l2object->n_v4l2_planes = 1;
3323 info->size = format->fmt.pix.sizeimage;
3324 stride = format->fmt.pix.bytesperline;
3326 if (info->stride[0] != stride)
3327 standard_stride = FALSE;
3329 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3332 /* adjust the offset to take into account left and top */
3333 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3334 if ((align->padding_left + align->padding_top) > 0)
3335 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3336 "Left and top padding is not permitted for tiled formats");
3338 for (i = 0; i < finfo->n_planes; i++) {
3341 /* FIXME we assume plane as component as this is true for all supported
3342 * format we support. */
3344 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left);
3345 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top);
3347 info->offset[i] += (vedge * info->stride[i]) +
3348 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i));
3353 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got sizeimage %" G_GSIZE_FORMAT,
3356 /* to avoid copies we need video meta if there is padding */
3357 v4l2object->need_video_meta =
3358 ((align->padding_top + align->padding_left + align->padding_right +
3359 align->padding_bottom) != 0);
3361 /* ... or if stride is non "standard" */
3362 if (!standard_stride)
3363 v4l2object->need_video_meta = TRUE;
3365 /* ... or also video meta if we use multiple, non-contiguous, planes */
3366 if (v4l2object->n_v4l2_planes > 1)
3367 v4l2object->need_video_meta = TRUE;
3369 v4l2object->info = *info;
3370 v4l2object->align = *align;
3371 v4l2object->format = *format;
3372 v4l2object->fmtdesc = fmtdesc;
3374 /* if we have a framerate pre-calculate duration */
3375 if (info->fps_n > 0 && info->fps_d > 0) {
3376 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
3378 if (GST_VIDEO_INFO_INTERLACE_MODE (info) ==
3379 GST_VIDEO_INTERLACE_MODE_ALTERNATE)
3380 v4l2object->duration /= 2;
3382 v4l2object->duration = GST_CLOCK_TIME_NONE;
3386 static enum v4l2_field
3387 get_v4l2_field_for_info (GstVideoInfo * info)
3389 if (!GST_VIDEO_INFO_IS_INTERLACED (info))
3390 return V4L2_FIELD_NONE;
3392 if (GST_VIDEO_INFO_INTERLACE_MODE (info) ==
3393 GST_VIDEO_INTERLACE_MODE_ALTERNATE)
3394 return V4L2_FIELD_ALTERNATE;
3396 switch (GST_VIDEO_INFO_FIELD_ORDER (info)) {
3397 case GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST:
3398 return V4L2_FIELD_INTERLACED_TB;
3399 case GST_VIDEO_FIELD_ORDER_BOTTOM_FIELD_FIRST:
3400 return V4L2_FIELD_INTERLACED_BT;
3401 case GST_VIDEO_FIELD_ORDER_UNKNOWN:
3403 return V4L2_FIELD_INTERLACED;
3408 gst_v4l2_video_colorimetry_matches (const GstVideoColorimetry * cinfo,
3412 static const GstVideoColorimetry ci_likely_jpeg = {
3413 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3414 GST_VIDEO_TRANSFER_UNKNOWN, GST_VIDEO_COLOR_PRIMARIES_UNKNOWN
3416 static const GstVideoColorimetry ci_jpeg = {
3417 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3418 GST_VIDEO_TRANSFER_SRGB, GST_VIDEO_COLOR_PRIMARIES_BT709
3421 if (!gst_video_info_from_caps (&info, caps))
3424 /* if colorimetry in caps is unknown, use the default one */
3425 if (info.colorimetry.primaries == GST_VIDEO_COLOR_PRIMARIES_UNKNOWN)
3426 info.colorimetry.primaries = cinfo->primaries;
3427 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_UNKNOWN)
3428 info.colorimetry.range = cinfo->range;
3429 if (info.colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN)
3430 info.colorimetry.matrix = cinfo->matrix;
3431 if (info.colorimetry.transfer == GST_VIDEO_TRANSFER_UNKNOWN)
3432 info.colorimetry.transfer = cinfo->transfer;
3434 if (gst_video_colorimetry_is_equal (&info.colorimetry, cinfo))
3437 /* Allow 1:4:0:0 (produced by jpegdec) if the device expects 1:4:7:1 */
3438 if (gst_video_colorimetry_is_equal (&info.colorimetry, &ci_likely_jpeg)
3439 && gst_video_colorimetry_is_equal (cinfo, &ci_jpeg))
3445 static const gchar *
3446 field_to_str (enum v4l2_field f)
3449 case V4L2_FIELD_ANY:
3451 case V4L2_FIELD_NONE:
3453 case V4L2_FIELD_TOP:
3455 case V4L2_FIELD_BOTTOM:
3457 case V4L2_FIELD_INTERLACED:
3458 return "interlaced";
3459 case V4L2_FIELD_SEQ_TB:
3461 case V4L2_FIELD_SEQ_BT:
3463 case V4L2_FIELD_ALTERNATE:
3465 case V4L2_FIELD_INTERLACED_TB:
3466 return "interlaced-tb";
3467 case V4L2_FIELD_INTERLACED_BT:
3468 return "interlaced-bt";
3475 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3476 gboolean try_only, GstV4l2Error * error)
3478 gint fd = v4l2object->video_fd;
3479 struct v4l2_format format;
3480 struct v4l2_streamparm streamparm;
3481 enum v4l2_field field;
3482 guint32 pixelformat;
3483 struct v4l2_fmtdesc *fmtdesc;
3485 GstVideoAlignment align;
3486 gint width, height, fps_n, fps_d;
3490 enum v4l2_colorspace colorspace = 0;
3491 enum v4l2_quantization range = 0;
3492 enum v4l2_ycbcr_encoding matrix = 0;
3493 enum v4l2_xfer_func transfer = 0;
3495 gboolean disable_interlacing = FALSE;
3496 gboolean disable_colorimetry = FALSE;
3498 g_return_val_if_fail (!v4l2object->skip_try_fmt_probes ||
3499 gst_caps_is_writable (caps), FALSE);
3501 GST_V4L2_CHECK_OPEN (v4l2object);
3503 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3505 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3507 gst_video_info_init (&info);
3508 gst_video_alignment_reset (&align);
3509 v4l2object->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
3511 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3514 pixelformat = fmtdesc->pixelformat;
3515 width = GST_VIDEO_INFO_WIDTH (&info);
3516 height = GST_VIDEO_INFO_FIELD_HEIGHT (&info);
3517 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3518 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3520 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3521 * or if contiguous is preferred */
3522 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3523 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3526 field = get_v4l2_field_for_info (&info);
3527 if (field != V4L2_FIELD_NONE)
3528 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
3530 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
3532 /* We first pick the main colorspace from the primaries */
3533 switch (info.colorimetry.primaries) {
3534 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3535 /* There is two colorspaces using these primaries, use the range to
3537 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3538 colorspace = V4L2_COLORSPACE_REC709;
3540 colorspace = V4L2_COLORSPACE_SRGB;
3542 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
3543 colorspace = V4L2_COLORSPACE_BT2020;
3545 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3546 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3548 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3549 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3551 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3552 colorspace = V4L2_COLORSPACE_SMPTE170M;
3554 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3555 colorspace = V4L2_COLORSPACE_SMPTE240M;
3558 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3559 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3560 /* We don't know, we will guess */
3564 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3565 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3569 switch (info.colorimetry.range) {
3570 case GST_VIDEO_COLOR_RANGE_0_255:
3571 range = V4L2_QUANTIZATION_FULL_RANGE;
3573 case GST_VIDEO_COLOR_RANGE_16_235:
3574 range = V4L2_QUANTIZATION_LIM_RANGE;
3576 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3577 /* We let the driver pick a default one */
3580 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3581 "Unknown colorimetry range %d", info.colorimetry.range);
3585 switch (info.colorimetry.matrix) {
3586 case GST_VIDEO_COLOR_MATRIX_RGB:
3587 /* Unspecified, leave to default */
3589 /* FCC is about the same as BT601 with less digit */
3590 case GST_VIDEO_COLOR_MATRIX_FCC:
3591 case GST_VIDEO_COLOR_MATRIX_BT601:
3592 matrix = V4L2_YCBCR_ENC_601;
3594 case GST_VIDEO_COLOR_MATRIX_BT709:
3595 matrix = V4L2_YCBCR_ENC_709;
3597 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3598 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3600 case GST_VIDEO_COLOR_MATRIX_BT2020:
3601 matrix = V4L2_YCBCR_ENC_BT2020;
3603 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3604 /* We let the driver pick a default one */
3607 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3608 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3612 switch (info.colorimetry.transfer) {
3613 case GST_VIDEO_TRANSFER_GAMMA18:
3614 case GST_VIDEO_TRANSFER_GAMMA20:
3615 case GST_VIDEO_TRANSFER_GAMMA22:
3616 case GST_VIDEO_TRANSFER_GAMMA28:
3617 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3618 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3620 case GST_VIDEO_TRANSFER_GAMMA10:
3621 transfer = V4L2_XFER_FUNC_NONE;
3623 case GST_VIDEO_TRANSFER_SMPTE2084:
3624 transfer = V4L2_XFER_FUNC_SMPTE2084;
3626 case GST_VIDEO_TRANSFER_BT601:
3627 case GST_VIDEO_TRANSFER_BT2020_12:
3628 case GST_VIDEO_TRANSFER_BT2020_10:
3629 case GST_VIDEO_TRANSFER_BT709:
3630 v4l2object->transfer = info.colorimetry.transfer;
3631 transfer = V4L2_XFER_FUNC_709;
3633 case GST_VIDEO_TRANSFER_SMPTE240M:
3634 transfer = V4L2_XFER_FUNC_SMPTE240M;
3636 case GST_VIDEO_TRANSFER_SRGB:
3637 transfer = V4L2_XFER_FUNC_SRGB;
3639 case GST_VIDEO_TRANSFER_LOG100:
3640 case GST_VIDEO_TRANSFER_LOG316:
3641 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3642 "LOG 100, 316 transfer functions not supported");
3643 /* FIXME No known sensible default, maybe AdobeRGB ? */
3645 case GST_VIDEO_TRANSFER_UNKNOWN:
3646 /* We let the driver pick a default one */
3649 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3650 "Unknown colorimetry transfer %d", info.colorimetry.transfer);
3654 if (colorspace == 0) {
3655 /* Try to guess colorspace according to pixelformat and size */
3656 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3657 if (range == V4L2_QUANTIZATION_FULL_RANGE
3658 && matrix == V4L2_YCBCR_ENC_601 && transfer == 0) {
3659 /* Full range BT.601 YCbCr encoding with unknown primaries and transfer
3660 * function most likely is JPEG */
3661 colorspace = V4L2_COLORSPACE_JPEG;
3662 transfer = V4L2_XFER_FUNC_SRGB;
3664 /* SD streams likely use SMPTE170M and HD streams REC709 */
3665 if (width <= 720 && GST_VIDEO_INFO_HEIGHT (&info) <= 576)
3666 colorspace = V4L2_COLORSPACE_SMPTE170M;
3668 colorspace = V4L2_COLORSPACE_REC709;
3670 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3671 colorspace = V4L2_COLORSPACE_SRGB;
3672 transfer = V4L2_XFER_FUNC_NONE;
3676 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format %dx%d, format "
3677 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3678 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3680 memset (&format, 0x00, sizeof (struct v4l2_format));
3681 format.type = v4l2object->type;
3684 format.type = v4l2object->type;
3685 format.fmt.pix_mp.pixelformat = pixelformat;
3686 format.fmt.pix_mp.width = width;
3687 format.fmt.pix_mp.height = height;
3688 format.fmt.pix_mp.field = field;
3689 format.fmt.pix_mp.num_planes = n_v4l_planes;
3691 /* try to ask our preferred stride but it's not a failure if not
3693 for (i = 0; i < n_v4l_planes; i++) {
3694 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3696 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3697 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3698 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3700 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3703 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3704 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3706 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3708 format.type = v4l2object->type;
3710 format.fmt.pix.width = width;
3711 format.fmt.pix.height = height;
3712 format.fmt.pix.pixelformat = pixelformat;
3713 format.fmt.pix.field = field;
3715 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3716 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3717 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3719 /* try to ask our preferred stride */
3720 format.fmt.pix.bytesperline = stride;
3722 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3723 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3726 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format is %dx%d, format "
3727 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3728 format.fmt.pix_mp.height,
3729 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3730 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3732 #ifndef GST_DISABLE_GST_DEBUG
3734 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3735 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3736 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3738 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3739 format.fmt.pix.bytesperline);
3744 format.fmt.pix_mp.colorspace = colorspace;
3745 format.fmt.pix_mp.quantization = range;
3746 format.fmt.pix_mp.ycbcr_enc = matrix;
3747 format.fmt.pix_mp.xfer_func = transfer;
3749 format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
3750 format.fmt.pix.colorspace = colorspace;
3751 format.fmt.pix.quantization = range;
3752 format.fmt.pix.ycbcr_enc = matrix;
3753 format.fmt.pix.xfer_func = transfer;
3756 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired colorspace is %d:%d:%d:%d",
3757 colorspace, range, matrix, transfer);
3760 if (v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3761 goto try_fmt_failed;
3763 if (v4l2object->ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3764 goto set_fmt_failed;
3768 colorspace = format.fmt.pix_mp.colorspace;
3769 range = format.fmt.pix_mp.quantization;
3770 matrix = format.fmt.pix_mp.ycbcr_enc;
3771 transfer = format.fmt.pix_mp.xfer_func;
3773 colorspace = format.fmt.pix.colorspace;
3774 range = format.fmt.pix.quantization;
3775 matrix = format.fmt.pix.ycbcr_enc;
3776 transfer = format.fmt.pix.xfer_func;
3779 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got format of %dx%d, format "
3780 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d:%d:%d:%d field: %s",
3781 format.fmt.pix.width, format.fmt.pix_mp.height,
3782 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3783 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3784 colorspace, range, matrix, transfer, field_to_str (format.fmt.pix.field));
3786 #ifndef GST_DISABLE_GST_DEBUG
3788 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3789 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3790 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3791 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3793 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3794 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3798 if (format.fmt.pix.pixelformat != pixelformat)
3799 goto invalid_pixelformat;
3801 /* Only negotiate size with raw data.
3802 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3803 * in ASF mode for example, there is also not reason for a driver to
3804 * change the size. */
3805 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3806 /* We can crop larger images */
3807 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3808 goto invalid_dimensions;
3810 /* Note, this will be adjusted if upstream has non-centered cropping. */
3811 align.padding_top = 0;
3812 align.padding_bottom = format.fmt.pix.height - height;
3813 align.padding_left = 0;
3814 align.padding_right = format.fmt.pix.width - width;
3817 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3818 goto invalid_planes;
3820 /* used to check colorimetry and interlace mode fields presence */
3821 s = gst_caps_get_structure (caps, 0);
3823 if (gst_v4l2_object_get_interlace_mode (format.fmt.pix.field,
3824 &info.interlace_mode)) {
3825 if (gst_structure_has_field (s, "interlace-mode")) {
3826 if (format.fmt.pix.field != field)
3830 /* The driver (or libv4l2) is miss-behaving, just ignore interlace-mode from
3832 disable_interlacing = TRUE;
3833 if (gst_structure_has_field (s, "interlace-mode"))
3834 gst_structure_remove_field (s, "interlace-mode");
3837 if (gst_v4l2_object_get_colorspace (v4l2object, &format, &info.colorimetry)) {
3838 if (gst_structure_has_field (s, "colorimetry")) {
3839 if (!gst_v4l2_video_colorimetry_matches (&info.colorimetry, caps))
3840 goto invalid_colorimetry;
3843 /* The driver (or libv4l2) is miss-behaving, just ignore colorimetry from
3845 disable_colorimetry = TRUE;
3846 if (gst_structure_has_field (s, "colorimetry"))
3847 gst_structure_remove_field (s, "colorimetry");
3850 /* In case we have skipped the try_fmt probes, we'll need to set the
3851 * interlace-mode and colorimetry back into the caps. */
3852 if (v4l2object->skip_try_fmt_probes) {
3853 if (!disable_interlacing && !gst_structure_has_field (s, "interlace-mode")) {
3854 gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
3855 gst_video_interlace_mode_to_string (info.interlace_mode), NULL);
3857 if (!disable_colorimetry && !gst_structure_has_field (s, "colorimetry")) {
3858 gchar *str = gst_video_colorimetry_to_string (&info.colorimetry);
3859 gst_structure_set (s, "colorimetry", G_TYPE_STRING, str, NULL);
3864 if (try_only) /* good enough for trying only */
3867 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
3868 struct v4l2_control ctl = { 0, };
3869 ctl.id = V4L2_CID_ALPHA_COMPONENT;
3872 if (v4l2object->ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
3873 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3874 "Failed to set alpha component value");
3877 /* Is there a reason we require the caller to always specify a framerate? */
3878 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired framerate: %u/%u", fps_n,
3881 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
3882 streamparm.type = v4l2object->type;
3884 if (v4l2object->ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
3885 goto get_parm_failed;
3887 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
3888 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3889 GST_VIDEO_INFO_FPS_N (&info) =
3890 streamparm.parm.capture.timeperframe.denominator;
3891 GST_VIDEO_INFO_FPS_D (&info) =
3892 streamparm.parm.capture.timeperframe.numerator;
3894 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got capture framerate: %u/%u",
3895 streamparm.parm.capture.timeperframe.denominator,
3896 streamparm.parm.capture.timeperframe.numerator);
3898 /* We used to skip frame rate setup if the camera was already setup
3899 * with the requested frame rate. This breaks some cameras though,
3900 * causing them to not output data (several models of Thinkpad cameras
3901 * have this problem at least).
3902 * So, don't skip. */
3903 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting capture framerate to %u/%u",
3905 /* We want to change the frame rate, so check whether we can. Some cheap USB
3906 * cameras don't have the capability */
3907 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3908 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3909 "Not setting capture framerate (not supported)");
3913 /* Note: V4L2 wants the frame interval, we have the frame rate */
3914 streamparm.parm.capture.timeperframe.numerator = fps_d;
3915 streamparm.parm.capture.timeperframe.denominator = fps_n;
3917 /* some cheap USB cam's won't accept any change */
3918 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3919 goto set_parm_failed;
3921 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
3922 streamparm.parm.capture.timeperframe.denominator > 0) {
3923 /* get new values */
3924 fps_d = streamparm.parm.capture.timeperframe.numerator;
3925 fps_n = streamparm.parm.capture.timeperframe.denominator;
3927 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set capture framerate to %u/%u",
3930 /* fix v4l2 capture driver to provide framerate values */
3931 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3932 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
3935 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3936 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3937 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
3938 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
3939 GST_VIDEO_INFO_FPS_N (&info) =
3940 streamparm.parm.output.timeperframe.denominator;
3941 GST_VIDEO_INFO_FPS_D (&info) =
3942 streamparm.parm.output.timeperframe.numerator;
3944 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got output framerate: %u/%u",
3945 streamparm.parm.output.timeperframe.denominator,
3946 streamparm.parm.output.timeperframe.numerator);
3948 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting output framerate to %u/%u",
3950 if ((streamparm.parm.output.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3951 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3952 "Not setting output framerate (not supported)");
3956 /* Note: V4L2 wants the frame interval, we have the frame rate */
3957 streamparm.parm.output.timeperframe.numerator = fps_d;
3958 streamparm.parm.output.timeperframe.denominator = fps_n;
3960 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3961 goto set_parm_failed;
3963 if (streamparm.parm.output.timeperframe.numerator > 0 &&
3964 streamparm.parm.output.timeperframe.denominator > 0) {
3965 /* get new values */
3966 fps_d = streamparm.parm.output.timeperframe.numerator;
3967 fps_n = streamparm.parm.output.timeperframe.denominator;
3969 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set output framerate to %u/%u",
3972 /* fix v4l2 output driver to provide framerate values */
3973 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3974 "Reuse caps framerate %u/%u - fix v4l2 output driver", fps_n, fps_d);
3977 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3978 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3982 /* add boolean return, so we can fail on drivers bugs */
3983 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
3985 /* now configure the pool */
3986 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
3994 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "can't parse caps %" GST_PTR_FORMAT,
3997 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3998 (_("Invalid caps")), ("Can't parse caps %" GST_PTR_FORMAT, caps));
4003 if (errno == EINVAL) {
4004 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4005 (_("Device '%s' has no supported format"), v4l2object->videodev),
4006 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4007 GST_FOURCC_ARGS (pixelformat), width, height,
4008 g_strerror (errno)));
4010 GST_V4L2_ERROR (error, RESOURCE, FAILED,
4011 (_("Device '%s' failed during initialization"),
4012 v4l2object->videodev),
4013 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4014 GST_FOURCC_ARGS (pixelformat), width, height,
4015 g_strerror (errno)));
4021 if (errno == EBUSY) {
4022 GST_V4L2_ERROR (error, RESOURCE, BUSY,
4023 (_("Device '%s' is busy"), v4l2object->videodev),
4024 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4025 GST_FOURCC_ARGS (pixelformat), width, height,
4026 g_strerror (errno)));
4027 } else if (errno == EINVAL) {
4028 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4029 (_("Device '%s' has no supported format"), v4l2object->videodev),
4030 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4031 GST_FOURCC_ARGS (pixelformat), width, height,
4032 g_strerror (errno)));
4034 GST_V4L2_ERROR (error, RESOURCE, FAILED,
4035 (_("Device '%s' failed during initialization"),
4036 v4l2object->videodev),
4037 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4038 GST_FOURCC_ARGS (pixelformat), width, height,
4039 g_strerror (errno)));
4045 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4046 (_("Device '%s' cannot capture at %dx%d"),
4047 v4l2object->videodev, width, height),
4048 ("Tried to capture at %dx%d, but device returned size %dx%d",
4049 width, height, format.fmt.pix.width, format.fmt.pix.height));
4052 invalid_pixelformat:
4054 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4055 (_("Device '%s' cannot capture in the specified format"),
4056 v4l2object->videodev),
4057 ("Tried to capture in %" GST_FOURCC_FORMAT
4058 ", but device returned format" " %" GST_FOURCC_FORMAT,
4059 GST_FOURCC_ARGS (pixelformat),
4060 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
4065 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4066 (_("Device '%s' does support non-contiguous planes"),
4067 v4l2object->videodev),
4068 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
4073 enum v4l2_field wanted_field;
4076 wanted_field = format.fmt.pix_mp.field;
4078 wanted_field = format.fmt.pix.field;
4080 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4081 (_("Device '%s' does not support %s interlacing"),
4082 v4l2object->videodev,
4083 field == V4L2_FIELD_NONE ? "progressive" : "interleaved"),
4084 ("Device wants %s interlacing",
4085 wanted_field == V4L2_FIELD_NONE ? "progressive" : "interleaved"));
4088 invalid_colorimetry:
4090 gchar *wanted_colorimetry;
4092 wanted_colorimetry = gst_video_colorimetry_to_string (&info.colorimetry);
4094 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4095 (_("Device '%s' does not support %s colorimetry"),
4096 v4l2object->videodev, gst_structure_get_string (s, "colorimetry")),
4097 ("Device wants %s colorimetry", wanted_colorimetry));
4099 g_free (wanted_colorimetry);
4104 /* it's possible that this call is not supported */
4105 if (errno != EINVAL && errno != ENOTTY) {
4106 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4107 (_("Could not get parameters on device '%s'"),
4108 v4l2object->videodev), GST_ERROR_SYSTEM);
4114 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4115 (_("Video device did not accept new frame rate setting.")),
4121 /* setup_pool already send the error */
4127 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps,
4128 GstV4l2Error * error)
4130 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Setting format to %" GST_PTR_FORMAT,
4132 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE, error);
4136 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps,
4137 GstV4l2Error * error)
4139 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying format %" GST_PTR_FORMAT,
4141 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE, error);
4145 * gst_v4l2_object_acquire_format:
4146 * @v4l2object: the object
4147 * @info: a GstVideoInfo to be filled
4149 * Acquire the driver chosen format. This is useful in decoder or encoder elements where
4150 * the output format is chosen by the HW.
4152 * Returns: %TRUE on success, %FALSE on failure.
4155 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
4157 struct v4l2_fmtdesc *fmtdesc;
4158 struct v4l2_format fmt;
4159 struct v4l2_crop crop;
4160 struct v4l2_selection sel;
4161 struct v4l2_rect *r = NULL;
4162 GstVideoFormat format;
4163 guint width, height;
4164 GstVideoAlignment align;
4165 GstVideoInterlaceMode interlace_mode;
4167 gst_video_info_init (info);
4168 gst_video_alignment_reset (&align);
4170 memset (&fmt, 0x00, sizeof (struct v4l2_format));
4171 fmt.type = v4l2object->type;
4172 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
4173 goto get_fmt_failed;
4175 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
4176 fmt.fmt.pix.pixelformat);
4177 if (fmtdesc == NULL)
4178 goto unsupported_format;
4180 /* No need to care about mplane, the four first params are the same */
4181 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
4183 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
4184 if (format == GST_VIDEO_FORMAT_UNKNOWN)
4185 goto unsupported_format;
4187 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
4188 goto invalid_dimensions;
4190 width = fmt.fmt.pix.width;
4191 height = fmt.fmt.pix.height;
4193 /* Use the default compose rectangle */
4194 memset (&sel, 0, sizeof (struct v4l2_selection));
4195 sel.type = v4l2object->type;
4196 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
4197 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0) {
4200 /* For ancient kernels, fall back to G_CROP */
4201 memset (&crop, 0, sizeof (struct v4l2_crop));
4202 crop.type = v4l2object->type;
4203 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
4207 align.padding_left = r->left;
4208 align.padding_top = r->top;
4209 align.padding_right = width - r->width - r->left;
4210 align.padding_bottom = height - r->height - r->top;
4215 switch (fmt.fmt.pix.field) {
4216 case V4L2_FIELD_ANY:
4217 case V4L2_FIELD_NONE:
4218 interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
4220 case V4L2_FIELD_INTERLACED:
4221 case V4L2_FIELD_INTERLACED_TB:
4222 case V4L2_FIELD_INTERLACED_BT:
4223 interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
4225 case V4L2_FIELD_ALTERNATE:
4226 interlace_mode = GST_VIDEO_INTERLACE_MODE_ALTERNATE;
4229 goto unsupported_field;
4232 gst_video_info_set_interlaced_format (info, format, interlace_mode, width,
4235 gst_v4l2_object_get_colorspace (v4l2object, &fmt, &info->colorimetry);
4236 gst_v4l2_object_get_streamparm (v4l2object, info);
4237 if ((info->fps_n == 0 && v4l2object->info.fps_d != 0)
4238 && (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
4239 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
4240 info->fps_d = v4l2object->info.fps_d;
4241 info->fps_n = v4l2object->info.fps_n;
4242 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Set capture fps to %d/%d",
4243 info->fps_n, info->fps_d);
4246 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
4248 /* Shall we setup the pool ? */
4254 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
4255 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
4260 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
4261 (_("Video device returned invalid dimensions.")),
4262 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
4263 fmt.fmt.pix.height));
4268 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
4269 (_("Video device uses an unsupported interlacing method.")),
4270 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
4275 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
4276 (_("Video device uses an unsupported pixel format.")),
4277 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
4278 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
4284 gst_v4l2_object_set_crop (GstV4l2Object * obj)
4286 struct v4l2_selection sel = { 0 };
4287 struct v4l2_crop crop = { 0 };
4289 sel.type = obj->type;
4290 sel.target = V4L2_SEL_TGT_CROP;
4292 sel.r.left = obj->align.padding_left;
4293 sel.r.top = obj->align.padding_top;
4294 sel.r.width = obj->info.width;
4295 sel.r.height = GST_VIDEO_INFO_FIELD_HEIGHT (&obj->info);
4297 crop.type = obj->type;
4300 if (obj->align.padding_left + obj->align.padding_top +
4301 obj->align.padding_right + obj->align.padding_bottom == 0) {
4302 GST_DEBUG_OBJECT (obj->dbg_obj, "no cropping needed");
4306 GST_DEBUG_OBJECT (obj->dbg_obj,
4307 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4308 crop.c.width, crop.c.height);
4310 if (obj->ioctl (obj->video_fd, VIDIOC_S_SELECTION, &sel) < 0) {
4311 if (errno != ENOTTY) {
4312 GST_WARNING_OBJECT (obj->dbg_obj,
4313 "Failed to set crop rectangle with VIDIOC_S_SELECTION: %s",
4314 g_strerror (errno));
4317 if (obj->ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
4318 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_S_CROP failed");
4322 if (obj->ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
4323 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_G_CROP failed");
4331 GST_DEBUG_OBJECT (obj->dbg_obj,
4332 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4333 crop.c.width, crop.c.height);
4339 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
4341 GstStructure *config;
4345 if (!v4l2object->pool)
4348 config = gst_buffer_pool_get_config (v4l2object->pool);
4349 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4351 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
4353 gst_structure_free (config);
4359 gst_v4l2_object_caps_is_subset (GstV4l2Object * v4l2object, GstCaps * caps)
4361 GstStructure *config;
4365 if (!v4l2object->pool)
4368 config = gst_buffer_pool_get_config (v4l2object->pool);
4369 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4371 ret = oldcaps && gst_caps_is_subset (oldcaps, caps);
4373 gst_structure_free (config);
4379 gst_v4l2_object_get_current_caps (GstV4l2Object * v4l2object)
4381 GstStructure *config;
4384 if (!v4l2object->pool)
4387 config = gst_buffer_pool_get_config (v4l2object->pool);
4388 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4391 gst_caps_ref (oldcaps);
4393 gst_structure_free (config);
4399 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
4401 gboolean ret = TRUE;
4403 GST_LOG_OBJECT (v4l2object->dbg_obj, "start flushing");
4405 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
4406 gst_buffer_pool_set_flushing (v4l2object->pool, TRUE);
4412 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
4414 gboolean ret = TRUE;
4416 GST_LOG_OBJECT (v4l2object->dbg_obj, "stop flushing");
4418 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
4419 gst_buffer_pool_set_flushing (v4l2object->pool, FALSE);
4425 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
4427 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "stopping");
4429 if (!GST_V4L2_IS_OPEN (v4l2object))
4431 if (!GST_V4L2_IS_ACTIVE (v4l2object))
4434 if (v4l2object->pool) {
4435 if (!gst_v4l2_buffer_pool_orphan (&v4l2object->pool)) {
4436 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "deactivating pool");
4437 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
4438 gst_object_unref (v4l2object->pool);
4440 v4l2object->pool = NULL;
4443 GST_V4L2_SET_INACTIVE (v4l2object);
4450 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4456 formats = gst_v4l2_object_get_format_list (v4l2object);
4458 ret = gst_caps_new_empty ();
4460 if (v4l2object->keep_aspect && !v4l2object->par) {
4461 struct v4l2_cropcap cropcap;
4463 memset (&cropcap, 0, sizeof (cropcap));
4465 cropcap.type = v4l2object->type;
4466 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0) {
4467 if (errno != ENOTTY)
4468 GST_WARNING_OBJECT (v4l2object->dbg_obj,
4469 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
4470 g_strerror (errno));
4471 } else if (cropcap.pixelaspect.numerator && cropcap.pixelaspect.denominator) {
4472 v4l2object->par = g_new0 (GValue, 1);
4473 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
4474 gst_value_set_fraction (v4l2object->par, cropcap.pixelaspect.numerator,
4475 cropcap.pixelaspect.denominator);
4479 for (walk = formats; walk; walk = walk->next) {
4480 struct v4l2_fmtdesc *format;
4481 GstStructure *template;
4484 format = (struct v4l2_fmtdesc *) walk->data;
4486 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
4489 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
4490 "unknown format %" GST_FOURCC_FORMAT,
4491 GST_FOURCC_ARGS (format->pixelformat));
4495 /* If we have a filter, check if we need to probe this format or not */
4497 GstCaps *format_caps = gst_caps_new_empty ();
4499 gst_caps_append_structure (format_caps, gst_structure_copy (template));
4501 if (!gst_caps_can_intersect (format_caps, filter)) {
4502 gst_caps_unref (format_caps);
4503 gst_structure_free (template);
4507 gst_caps_unref (format_caps);
4510 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
4511 format->pixelformat, template);
4513 gst_caps_append (ret, tmp);
4515 /* Add a variant of the caps with the Interlaced feature so we can negotiate it if needed */
4516 add_alternate_variant (v4l2object, ret, gst_caps_get_structure (ret,
4517 gst_caps_get_size (ret) - 1));
4520 gst_structure_free (template);
4527 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
4528 gst_caps_unref (tmp);
4531 GST_INFO_OBJECT (v4l2object->dbg_obj, "probed caps: %" GST_PTR_FORMAT, ret);
4537 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4541 if (v4l2object->probed_caps == NULL)
4542 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
4545 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
4546 GST_CAPS_INTERSECT_FIRST);
4548 ret = gst_caps_ref (v4l2object->probed_caps);
4555 gst_v4l2_object_match_buffer_layout (GstV4l2Object * obj, guint n_planes,
4556 gsize offset[GST_VIDEO_MAX_PLANES], gint stride[GST_VIDEO_MAX_PLANES],
4557 gsize buffer_size, guint padded_height)
4560 gboolean need_fmt_update = FALSE;
4562 if (n_planes != GST_VIDEO_INFO_N_PLANES (&obj->info)) {
4563 GST_WARNING_OBJECT (obj->dbg_obj,
4564 "Cannot match buffers with different number planes");
4568 for (p = 0; p < n_planes; p++) {
4569 if (stride[p] < obj->info.stride[p]) {
4570 GST_DEBUG_OBJECT (obj->dbg_obj,
4571 "Not matching as remote stride %i is smaller than %i on plane %u",
4572 stride[p], obj->info.stride[p], p);
4574 } else if (stride[p] > obj->info.stride[p]) {
4575 GST_LOG_OBJECT (obj->dbg_obj,
4576 "remote stride %i is higher than %i on plane %u",
4577 stride[p], obj->info.stride[p], p);
4578 need_fmt_update = TRUE;
4581 if (offset[p] < obj->info.offset[p]) {
4582 GST_DEBUG_OBJECT (obj->dbg_obj,
4583 "Not matching as offset %" G_GSIZE_FORMAT
4584 " is smaller than %" G_GSIZE_FORMAT " on plane %u",
4585 offset[p], obj->info.offset[p], p);
4587 } else if (offset[p] > obj->info.offset[p]) {
4588 GST_LOG_OBJECT (obj->dbg_obj,
4589 "Remote offset %" G_GSIZE_FORMAT
4590 " is higher than %" G_GSIZE_FORMAT " on plane %u",
4591 offset[p], obj->info.offset[p], p);
4592 need_fmt_update = TRUE;
4595 if (padded_height) {
4598 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
4599 fmt_height = obj->format.fmt.pix_mp.height;
4601 fmt_height = obj->format.fmt.pix.height;
4603 if (padded_height > fmt_height)
4604 need_fmt_update = TRUE;
4608 if (need_fmt_update) {
4609 struct v4l2_format format;
4610 gint wanted_stride[GST_VIDEO_MAX_PLANES] = { 0, };
4612 format = obj->format;
4614 if (padded_height) {
4615 GST_DEBUG_OBJECT (obj->dbg_obj, "Padded height %u", padded_height);
4617 obj->align.padding_bottom =
4618 padded_height - GST_VIDEO_INFO_FIELD_HEIGHT (&obj->info);
4620 GST_WARNING_OBJECT (obj->dbg_obj,
4621 "Failed to compute padded height; keep the default one");
4622 padded_height = format.fmt.pix_mp.height;
4625 /* update the current format with the stride we want to import from */
4626 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
4629 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted strides:");
4631 for (i = 0; i < obj->n_v4l2_planes; i++) {
4632 gint plane_stride = stride[i];
4634 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4635 plane_stride = GST_VIDEO_TILE_X_TILES (plane_stride) <<
4636 GST_VIDEO_FORMAT_INFO_TILE_WS (obj->info.finfo);
4638 format.fmt.pix_mp.plane_fmt[i].bytesperline = plane_stride;
4639 format.fmt.pix_mp.height = padded_height;
4640 wanted_stride[i] = plane_stride;
4641 GST_DEBUG_OBJECT (obj->dbg_obj, " [%u] %i", i, wanted_stride[i]);
4644 gint plane_stride = stride[0];
4646 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted stride: %i", plane_stride);
4648 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4649 plane_stride = GST_VIDEO_TILE_X_TILES (plane_stride) <<
4650 GST_VIDEO_FORMAT_INFO_TILE_WS (obj->info.finfo);
4652 format.fmt.pix.bytesperline = plane_stride;
4653 format.fmt.pix.height = padded_height;
4654 wanted_stride[0] = plane_stride;
4657 if (obj->ioctl (obj->video_fd, VIDIOC_S_FMT, &format) < 0) {
4658 GST_WARNING_OBJECT (obj->dbg_obj,
4659 "Something went wrong trying to update current format: %s",
4660 g_strerror (errno));
4664 gst_v4l2_object_save_format (obj, obj->fmtdesc, &format, &obj->info,
4667 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
4670 for (i = 0; i < obj->n_v4l2_planes; i++) {
4671 if (format.fmt.pix_mp.plane_fmt[i].bytesperline != wanted_stride[i]) {
4672 GST_DEBUG_OBJECT (obj->dbg_obj,
4673 "[%i] Driver did not accept the new stride (wants %i, got %i)",
4674 i, wanted_stride[i], format.fmt.pix_mp.plane_fmt[i].bytesperline);
4679 if (format.fmt.pix_mp.height != padded_height) {
4680 GST_DEBUG_OBJECT (obj->dbg_obj,
4681 "Driver did not accept the padded height (wants %i, got %i)",
4682 padded_height, format.fmt.pix_mp.height);
4685 if (format.fmt.pix.bytesperline != wanted_stride[0]) {
4686 GST_DEBUG_OBJECT (obj->dbg_obj,
4687 "Driver did not accept the new stride (wants %i, got %i)",
4688 wanted_stride[0], format.fmt.pix.bytesperline);
4692 if (format.fmt.pix.height != padded_height) {
4693 GST_DEBUG_OBJECT (obj->dbg_obj,
4694 "Driver did not accept the padded height (wants %i, got %i)",
4695 padded_height, format.fmt.pix.height);
4700 if (obj->align.padding_bottom) {
4701 /* Crop because of vertical padding */
4702 GST_DEBUG_OBJECT (obj->dbg_obj, "crop because of bottom padding of %d",
4703 obj->align.padding_bottom);
4704 gst_v4l2_object_set_crop (obj);
4711 validate_video_meta_struct (GstV4l2Object * obj, const GstStructure * s)
4715 for (i = 0; i < gst_structure_n_fields (s); i++) {
4716 const gchar *name = gst_structure_nth_field_name (s, i);
4718 if (!g_str_equal (name, "padding-top")
4719 && !g_str_equal (name, "padding-bottom")
4720 && !g_str_equal (name, "padding-left")
4721 && !g_str_equal (name, "padding-right")) {
4722 GST_WARNING_OBJECT (obj->dbg_obj, "Unknown video meta field: '%s'", name);
4731 gst_v4l2_object_match_buffer_layout_from_struct (GstV4l2Object * obj,
4732 const GstStructure * s, GstCaps * caps, guint buffer_size)
4735 GstVideoAlignment align;
4736 gsize plane_size[GST_VIDEO_MAX_PLANES];
4738 if (!validate_video_meta_struct (obj, s))
4741 if (!gst_video_info_from_caps (&info, caps)) {
4742 GST_WARNING_OBJECT (obj->dbg_obj, "Failed to create video info");
4746 gst_video_alignment_reset (&align);
4748 gst_structure_get_uint (s, "padding-top", &align.padding_top);
4749 gst_structure_get_uint (s, "padding-bottom", &align.padding_bottom);
4750 gst_structure_get_uint (s, "padding-left", &align.padding_left);
4751 gst_structure_get_uint (s, "padding-right", &align.padding_right);
4753 if (align.padding_top || align.padding_bottom || align.padding_left ||
4754 align.padding_right) {
4755 GST_DEBUG_OBJECT (obj->dbg_obj,
4756 "Upstream requested padding (top: %d bottom: %d left: %d right: %d)",
4757 align.padding_top, align.padding_bottom, align.padding_left,
4758 align.padding_right);
4761 if (!gst_video_info_align_full (&info, &align, plane_size)) {
4762 GST_WARNING_OBJECT (obj->dbg_obj, "Failed to align video info");
4766 if (GST_VIDEO_INFO_SIZE (&info) != buffer_size) {
4767 GST_WARNING_OBJECT (obj->dbg_obj,
4768 "Requested buffer size (%d) doesn't match video info size (%"
4769 G_GSIZE_FORMAT ")", buffer_size, GST_VIDEO_INFO_SIZE (&info));
4773 GST_DEBUG_OBJECT (obj->dbg_obj,
4774 "try matching buffer layout requested by downstream");
4776 gst_v4l2_object_match_buffer_layout (obj, GST_VIDEO_INFO_N_PLANES (&info),
4777 info.offset, info.stride, buffer_size,
4778 GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size));
4784 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
4787 GstBufferPool *pool = NULL, *other_pool = NULL;
4788 GstStructure *config;
4789 guint size, min, max, own_min = 0;
4791 gboolean has_video_meta;
4792 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
4793 GstAllocator *allocator = NULL;
4794 GstAllocationParams params = { 0 };
4797 GST_DEBUG_OBJECT (obj->dbg_obj, "decide allocation");
4799 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4800 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
4802 gst_query_parse_allocation (query, &caps, NULL);
4804 if (obj->pool == NULL) {
4805 if (!gst_v4l2_object_setup_pool (obj, caps))
4809 if (gst_query_get_n_allocation_params (query) > 0)
4810 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
4812 if (gst_query_get_n_allocation_pools (query) > 0) {
4813 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
4822 GST_DEBUG_OBJECT (obj->dbg_obj, "allocation: size:%u min:%u max:%u pool:%"
4823 GST_PTR_FORMAT, size, min, max, pool);
4826 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE,
4829 if (has_video_meta) {
4830 const GstStructure *params;
4831 gst_query_parse_nth_allocation_meta (query, video_idx, ¶ms);
4834 gst_v4l2_object_match_buffer_layout_from_struct (obj, params, caps, size);
4837 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
4839 gst_v4l2_get_driver_min_buffers (obj);
4840 /* We can't share our own pool, if it exceed V4L2 capacity */
4841 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
4842 can_share_own_pool = FALSE;
4845 switch (obj->mode) {
4846 case GST_V4L2_IO_RW:
4848 /* in READ/WRITE mode, prefer a downstream pool because our own pool
4849 * doesn't help much, we have to write to it as well */
4850 GST_DEBUG_OBJECT (obj->dbg_obj,
4851 "read/write mode: using downstream pool");
4852 /* use the bigest size, when we use our own pool we can't really do any
4853 * other size than what the hardware gives us but for downstream pools
4855 size = MAX (size, obj->info.size);
4856 } else if (can_share_own_pool) {
4857 /* no downstream pool, use our own then */
4858 GST_DEBUG_OBJECT (obj->dbg_obj,
4859 "read/write mode: no downstream pool, using our own");
4860 pool = gst_object_ref (obj->pool);
4861 size = obj->info.size;
4862 pushing_from_our_pool = TRUE;
4866 case GST_V4L2_IO_USERPTR:
4867 case GST_V4L2_IO_DMABUF_IMPORT:
4868 /* in importing mode, prefer our own pool, and pass the other pool to
4869 * our own, so it can serve itself */
4871 goto no_downstream_pool;
4872 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj->pool),
4875 gst_object_unref (pool);
4876 pool = gst_object_ref (obj->pool);
4877 size = obj->info.size;
4880 case GST_V4L2_IO_MMAP:
4881 case GST_V4L2_IO_DMABUF:
4882 /* in streaming mode, prefer our own pool */
4883 /* Check if we can use it ... */
4884 if (can_share_own_pool) {
4886 gst_object_unref (pool);
4887 pool = gst_object_ref (obj->pool);
4888 size = obj->info.size;
4889 GST_DEBUG_OBJECT (obj->dbg_obj,
4890 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
4891 pushing_from_our_pool = TRUE;
4893 GST_DEBUG_OBJECT (obj->dbg_obj,
4894 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
4897 GST_DEBUG_OBJECT (obj->dbg_obj,
4898 "streaming mode: no usable pool, copying to generic pool");
4899 size = MAX (size, obj->info.size);
4902 case GST_V4L2_IO_AUTO:
4904 GST_WARNING_OBJECT (obj->dbg_obj, "unhandled mode");
4911 /* If pushing from our own pool, configure it with queried minimum,
4912 * otherwise use the minimum required */
4913 if (pushing_from_our_pool) {
4914 /* When pushing from our own pool, we need what downstream one, to be able
4915 * to fill the pipeline, the minimum required to decoder according to the
4916 * driver and 2 more, so we don't endup up with everything downstream or
4917 * held by the decoder. We account 2 buffers for v4l2 so when one is being
4918 * pushed downstream the other one can already be queued for the next
4920 own_min = min + obj->min_buffers + 2;
4922 /* If no allocation parameters where provided, allow for a little more
4923 * buffers and enable copy threshold */
4926 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4929 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4934 /* In this case we'll have to configure two buffer pool. For our buffer
4935 * pool, we'll need what the driver one, and one more, so we can dequeu */
4936 own_min = obj->min_buffers + 1;
4937 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS (obj));
4939 /* for the downstream pool, we keep what downstream wants, though ensure
4940 * at least a minimum if downstream didn't suggest anything (we are
4941 * expecting the base class to create a default one for the context) */
4942 min = MAX (min, GST_V4L2_MIN_BUFFERS (obj));
4944 /* To import we need the other pool to hold at least own_min */
4945 if (obj->pool == pool)
4949 /* Request a bigger max, if one was suggested but it's too small */
4951 max = MAX (min, max);
4953 /* First step, configure our own pool */
4954 config = gst_buffer_pool_get_config (obj->pool);
4956 if (obj->need_video_meta || has_video_meta) {
4957 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
4958 gst_buffer_pool_config_add_option (config,
4959 GST_BUFFER_POOL_OPTION_VIDEO_META);
4962 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4963 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
4965 GST_DEBUG_OBJECT (obj->dbg_obj, "setting own pool config to %"
4966 GST_PTR_FORMAT, config);
4968 /* Our pool often need to adjust the value */
4969 if (!gst_buffer_pool_set_config (obj->pool, config)) {
4970 config = gst_buffer_pool_get_config (obj->pool);
4972 GST_DEBUG_OBJECT (obj->dbg_obj, "own pool config changed to %"
4973 GST_PTR_FORMAT, config);
4975 /* our pool will adjust the maximum buffer, which we are fine with */
4976 if (!gst_buffer_pool_set_config (obj->pool, config))
4980 /* Now configure the other pool if different */
4981 if (obj->pool != pool)
4985 config = gst_buffer_pool_get_config (other_pool);
4986 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4987 gst_buffer_pool_config_set_params (config, caps, size, min, max);
4989 GST_DEBUG_OBJECT (obj->dbg_obj, "setting other pool config to %"
4990 GST_PTR_FORMAT, config);
4992 /* if downstream supports video metadata, add this to the pool config */
4993 if (has_video_meta) {
4994 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
4995 gst_buffer_pool_config_add_option (config,
4996 GST_BUFFER_POOL_OPTION_VIDEO_META);
4999 if (!gst_buffer_pool_set_config (other_pool, config)) {
5000 config = gst_buffer_pool_get_config (other_pool);
5002 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
5004 gst_structure_free (config);
5008 if (!gst_buffer_pool_set_config (other_pool, config))
5014 /* For simplicity, simply read back the active configuration, so our base
5015 * class get the right information */
5016 config = gst_buffer_pool_get_config (pool);
5017 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
5018 gst_structure_free (config);
5022 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
5024 gst_query_add_allocation_pool (query, pool, size, min, max);
5027 gst_object_unref (allocator);
5030 gst_object_unref (pool);
5036 /* setup_pool already send the error */
5041 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
5042 (_("Failed to configure internal buffer pool.")), (NULL));
5047 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
5048 (_("Video device did not suggest any buffer size.")), (NULL));
5054 gst_object_unref (allocator);
5057 gst_object_unref (pool);
5062 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
5063 (_("No downstream pool to import from.")),
5064 ("When importing DMABUF or USERPTR, we need a pool to import from"));
5070 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
5072 GstBufferPool *pool = NULL;
5073 /* we need at least 2 buffers to operate */
5074 guint size, min, max;
5078 /* Set defaults allocation parameters */
5079 size = obj->info.size;
5080 min = GST_V4L2_MIN_BUFFERS (obj);
5081 max = VIDEO_MAX_FRAME;
5083 gst_query_parse_allocation (query, &caps, &need_pool);
5088 switch (obj->mode) {
5089 case GST_V4L2_IO_MMAP:
5090 case GST_V4L2_IO_DMABUF:
5091 if (need_pool && obj->pool) {
5092 if (!gst_buffer_pool_is_active (obj->pool))
5093 pool = gst_object_ref (obj->pool);
5102 GstStructure *config;
5104 /* we had a pool, check caps */
5105 config = gst_buffer_pool_get_config (pool);
5106 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
5108 GST_DEBUG_OBJECT (obj->dbg_obj,
5109 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
5110 if (!gst_caps_is_equal (caps, pcaps)) {
5111 gst_structure_free (config);
5112 gst_object_unref (pool);
5113 goto different_caps;
5115 gst_structure_free (config);
5117 gst_v4l2_get_driver_min_buffers (obj);
5119 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS (obj));
5121 gst_query_add_allocation_pool (query, pool, size, min, max);
5123 /* we also support various metadata */
5124 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
5127 gst_object_unref (pool);
5134 GST_DEBUG_OBJECT (obj->dbg_obj, "no caps specified");
5139 /* different caps, we can't use this pool */
5140 GST_DEBUG_OBJECT (obj->dbg_obj, "pool has different caps");
5146 gst_v4l2_object_try_import (GstV4l2Object * obj, GstBuffer * buffer)
5148 GstVideoMeta *vmeta;
5149 guint n_mem = gst_buffer_n_memory (buffer);
5151 /* only import if requested */
5152 switch (obj->mode) {
5153 case GST_V4L2_IO_USERPTR:
5154 case GST_V4L2_IO_DMABUF_IMPORT:
5157 GST_DEBUG_OBJECT (obj->dbg_obj,
5158 "The io-mode does not enable importation");
5162 vmeta = gst_buffer_get_video_meta (buffer);
5163 if (!vmeta && obj->need_video_meta) {
5164 GST_DEBUG_OBJECT (obj->dbg_obj, "Downstream buffer uses standard "
5165 "stride/offset while the driver does not.");
5169 /* we need matching strides/offsets and size */
5171 guint plane_height[GST_VIDEO_MAX_PLANES] = { 0, };
5173 gst_video_meta_get_plane_height (vmeta, plane_height);
5175 if (!gst_v4l2_object_match_buffer_layout (obj, vmeta->n_planes,
5176 vmeta->offset, vmeta->stride, gst_buffer_get_size (buffer),
5181 /* we can always import single memory buffer, but otherwise we need the same
5182 * amount of memory object. */
5183 if (n_mem != 1 && n_mem != obj->n_v4l2_planes) {
5184 GST_DEBUG_OBJECT (obj->dbg_obj, "Can only import %i memory, "
5185 "buffers contains %u memory", obj->n_v4l2_planes, n_mem);
5189 /* For DMABuf importation we need DMABuf of course */
5190 if (obj->mode == GST_V4L2_IO_DMABUF_IMPORT) {
5193 for (i = 0; i < n_mem; i++) {
5194 GstMemory *mem = gst_buffer_peek_memory (buffer, i);
5196 if (!gst_is_dmabuf_memory (mem)) {
5197 GST_DEBUG_OBJECT (obj->dbg_obj, "Cannot import non-DMABuf memory.");
5203 /* for the remaining, only the kernel driver can tell */