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_NV12M_8L128, TRUE, GST_V4L2_RAW},
156 {V4L2_PIX_FMT_NV12M_10BE_8L128, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
158 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
159 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
160 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
161 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
162 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
163 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
164 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
165 {V4L2_PIX_FMT_MM21, TRUE, GST_V4L2_RAW},
167 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
168 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_RAW},
169 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_RAW},
170 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_RAW},
171 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_RAW},
173 /* compressed formats */
174 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
175 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
176 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
177 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
178 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
179 {V4L2_PIX_FMT_FWHT, FALSE, GST_V4L2_CODEC},
180 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
181 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
182 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
183 {V4L2_PIX_FMT_HEVC, FALSE, GST_V4L2_CODEC},
184 {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
185 {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
186 {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
187 {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
188 {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
189 {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
190 {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
191 {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
192 {V4L2_PIX_FMT_VP9, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
194 /* Vendor-specific formats */
195 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
196 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
197 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
198 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
201 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
203 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
206 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
208 gst_v4l2_device_get_type (void)
210 static GType v4l2_device_type = 0;
212 if (v4l2_device_type == 0) {
213 static const GFlagsValue values[] = {
214 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
215 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
216 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
218 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
219 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
221 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
222 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
228 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
231 return v4l2_device_type;
234 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
236 gst_v4l2_tv_norm_get_type (void)
238 static GType v4l2_tv_norm = 0;
241 static const GEnumValue tv_norms[] = {
244 {V4L2_STD_NTSC, "NTSC", "NTSC"},
245 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
246 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
247 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
248 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
250 {V4L2_STD_PAL, "PAL", "PAL"},
251 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
252 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
253 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
254 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
255 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
256 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
257 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
258 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
259 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
260 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
261 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
262 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
263 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
264 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
266 {V4L2_STD_SECAM, "SECAM", "SECAM"},
267 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
268 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
269 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
270 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
271 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
272 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
273 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
274 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
275 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
280 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
287 gst_v4l2_io_mode_get_type (void)
289 static GType v4l2_io_mode = 0;
292 static const GEnumValue io_modes[] = {
293 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
294 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
295 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
296 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
297 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
298 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
303 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
309 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
310 const char *default_device)
312 g_object_class_install_property (gobject_class, PROP_DEVICE,
313 g_param_spec_string ("device", "Device", "Device location",
314 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
315 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
316 g_param_spec_string ("device-name", "Device name",
317 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
318 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
319 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
320 g_param_spec_int ("device-fd", "File descriptor",
321 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
322 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_FLAGS,
324 g_param_spec_flags ("flags", "Flags", "Device type flags",
325 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
326 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
329 * GstV4l2Src:brightness:
331 * Picture brightness, or more precisely, the black level
333 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
334 g_param_spec_int ("brightness", "Brightness",
335 "Picture brightness, or more precisely, the black level", G_MININT,
337 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
339 * GstV4l2Src:contrast:
341 * Picture contrast or luma gain
343 g_object_class_install_property (gobject_class, PROP_CONTRAST,
344 g_param_spec_int ("contrast", "Contrast",
345 "Picture contrast or luma gain", G_MININT,
347 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
349 * GstV4l2Src:saturation:
351 * Picture color saturation or chroma gain
353 g_object_class_install_property (gobject_class, PROP_SATURATION,
354 g_param_spec_int ("saturation", "Saturation",
355 "Picture color saturation or chroma gain", G_MININT,
357 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
361 * Hue or color balance
363 g_object_class_install_property (gobject_class, PROP_HUE,
364 g_param_spec_int ("hue", "Hue",
365 "Hue or color balance", G_MININT,
367 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
374 g_object_class_install_property (gobject_class, PROP_TV_NORM,
375 g_param_spec_enum ("norm", "TV norm",
377 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
378 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
381 * GstV4l2Src:io-mode:
385 g_object_class_install_property (gobject_class, PROP_IO_MODE,
386 g_param_spec_enum ("io-mode", "IO mode",
388 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
389 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 * GstV4l2Src:extra-controls:
394 * Additional v4l2 controls for the device. The controls are identified
395 * by the control name (lowercase with '_' for any non-alphanumeric
400 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
401 g_param_spec_boxed ("extra-controls", "Extra Controls",
402 "Extra v4l2 controls (CIDs) for the device",
403 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406 * GstV4l2Src:pixel-aspect-ratio:
408 * The pixel aspect ratio of the device. This overwrites the pixel aspect
409 * ratio queried from the device.
413 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
414 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
415 "Overwrite the pixel aspect ratio of the device", "1/1",
416 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
419 * GstV4l2Src:force-aspect-ratio:
421 * When enabled, the pixel aspect ratio queried from the device or set
422 * with the pixel-aspect-ratio property will be enforced.
426 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
427 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
428 "When enabled, the pixel aspect ratio will be enforced", TRUE,
429 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
431 gst_type_mark_as_plugin_api (GST_TYPE_V4L2_DEVICE_FLAGS, 0);
432 gst_type_mark_as_plugin_api (GST_TYPE_V4L2_TV_NORM, 0);
433 gst_type_mark_as_plugin_api (GST_TYPE_V4L2_IO_MODE, 0);
437 gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
439 g_object_class_install_property (gobject_class, PROP_DEVICE,
440 g_param_spec_string ("device", "Device", "Device location",
441 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
443 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
444 g_param_spec_string ("device-name", "Device name",
445 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
446 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
448 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
449 g_param_spec_int ("device-fd", "File descriptor",
450 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
451 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
453 g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
454 g_param_spec_enum ("output-io-mode", "Output IO mode",
455 "Output side I/O mode (matches sink pad)",
456 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
457 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
459 g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
460 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
461 "Capture I/O mode (matches src pad)",
462 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
463 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
465 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
466 g_param_spec_boxed ("extra-controls", "Extra Controls",
467 "Extra v4l2 controls (CIDs) for the device",
468 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
471 /* Support for 32bit off_t, this wrapper is casting off_t to gint64 */
476 v4l2_mmap_wrapper (gpointer start, gsize length, gint prot, gint flags, gint fd,
479 return v4l2_mmap (start, length, prot, flags, fd, (gint64) offset);
482 #define v4l2_mmap v4l2_mmap_wrapper
484 #endif /* SIZEOF_OFF_T < 8 */
485 #endif /* HAVE_LIBV4L2 */
488 gst_v4l2_object_new (GstElement * element,
489 GstObject * debug_object,
490 enum v4l2_buf_type type,
491 const char *default_device,
492 GstV4l2GetInOutFunction get_in_out_func,
493 GstV4l2SetInOutFunction set_in_out_func,
494 GstV4l2UpdateFpsFunction update_fps_func)
496 GstV4l2Object *v4l2object;
499 * some default values
501 v4l2object = g_new0 (GstV4l2Object, 1);
503 v4l2object->type = type;
504 v4l2object->formats = NULL;
506 v4l2object->element = element;
507 v4l2object->dbg_obj = debug_object;
508 v4l2object->get_in_out_func = get_in_out_func;
509 v4l2object->set_in_out_func = set_in_out_func;
510 v4l2object->update_fps_func = update_fps_func;
512 v4l2object->video_fd = -1;
513 v4l2object->active = FALSE;
514 v4l2object->videodev = g_strdup (default_device);
516 v4l2object->norms = NULL;
517 v4l2object->channels = NULL;
518 v4l2object->colors = NULL;
520 v4l2object->keep_aspect = TRUE;
522 v4l2object->n_v4l2_planes = 0;
524 v4l2object->no_initial_format = FALSE;
526 /* We now disable libv4l2 by default, but have an env to enable it. */
528 if (g_getenv ("GST_V4L2_USE_LIBV4L2")) {
529 v4l2object->fd_open = v4l2_fd_open;
530 v4l2object->close = v4l2_close;
531 v4l2object->dup = v4l2_dup;
532 v4l2object->ioctl = v4l2_ioctl;
533 v4l2object->read = v4l2_read;
534 v4l2object->mmap = v4l2_mmap;
535 v4l2object->munmap = v4l2_munmap;
539 v4l2object->fd_open = NULL;
540 v4l2object->close = close;
541 v4l2object->dup = dup;
542 v4l2object->ioctl = ioctl;
543 v4l2object->read = read;
544 v4l2object->mmap = mmap;
545 v4l2object->munmap = munmap;
552 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
554 g_return_if_fail (v4l2object != NULL);
556 g_free (v4l2object->videodev);
557 g_free (v4l2object->par);
558 g_free (v4l2object->channel);
560 if (v4l2object->formats) {
561 gst_v4l2_object_clear_format_list (v4l2object);
564 if (v4l2object->probed_caps) {
565 gst_caps_unref (v4l2object->probed_caps);
568 if (v4l2object->extra_controls) {
569 gst_structure_free (v4l2object->extra_controls);
577 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
579 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
580 g_slist_free (v4l2object->formats);
581 v4l2object->formats = NULL;
587 gst_v4l2_object_prop_to_cid (guint prop_id)
592 case PROP_BRIGHTNESS:
593 cid = V4L2_CID_BRIGHTNESS;
596 cid = V4L2_CID_CONTRAST;
598 case PROP_SATURATION:
599 cid = V4L2_CID_SATURATION;
605 GST_WARNING ("unmapped property id: %d", prop_id);
612 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
613 guint prop_id, const GValue * value, GParamSpec * pspec)
617 g_free (v4l2object->videodev);
618 v4l2object->videodev = g_value_dup_string (value);
620 case PROP_BRIGHTNESS:
622 case PROP_SATURATION:
625 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
628 if (GST_V4L2_IS_OPEN (v4l2object)) {
629 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
636 v4l2object->tv_norm = g_value_get_enum (value);
640 if (GST_V4L2_IS_OPEN (v4l2object)) {
641 GstTuner *tuner = GST_TUNER (v4l2object->element);
642 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
643 (gchar *) g_value_get_string (value));
646 /* like gst_tuner_set_channel (tuner, channel)
647 without g_object_notify */
648 gst_v4l2_tuner_set_channel (v4l2object, channel);
651 g_free (v4l2object->channel);
652 v4l2object->channel = g_value_dup_string (value);
656 if (GST_V4L2_IS_OPEN (v4l2object)) {
657 GstTuner *tuner = GST_TUNER (v4l2object->element);
658 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
661 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
663 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
664 without g_object_notify */
665 gst_v4l2_tuner_set_frequency (v4l2object, channel,
666 g_value_get_ulong (value));
669 v4l2object->frequency = g_value_get_ulong (value);
675 v4l2object->req_mode = g_value_get_enum (value);
677 case PROP_CAPTURE_IO_MODE:
678 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
679 v4l2object->req_mode = g_value_get_enum (value);
681 case PROP_OUTPUT_IO_MODE:
682 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
683 v4l2object->req_mode = g_value_get_enum (value);
685 case PROP_EXTRA_CONTROLS:{
686 const GstStructure *s = gst_value_get_structure (value);
688 if (v4l2object->extra_controls)
689 gst_structure_free (v4l2object->extra_controls);
691 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
692 if (GST_V4L2_IS_OPEN (v4l2object))
693 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
696 case PROP_PIXEL_ASPECT_RATIO:
697 if (v4l2object->par) {
698 g_value_unset (v4l2object->par);
699 g_free (v4l2object->par);
701 v4l2object->par = g_new0 (GValue, 1);
702 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
703 if (!g_value_transform (value, v4l2object->par)) {
704 g_warning ("Could not transform string to aspect ratio");
705 gst_value_set_fraction (v4l2object->par, 1, 1);
708 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "set PAR to %d/%d",
709 gst_value_get_fraction_numerator (v4l2object->par),
710 gst_value_get_fraction_denominator (v4l2object->par));
712 case PROP_FORCE_ASPECT_RATIO:
713 v4l2object->keep_aspect = g_value_get_boolean (value);
724 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
725 guint prop_id, GValue * value, GParamSpec * pspec)
729 g_value_set_string (value, v4l2object->videodev);
731 case PROP_DEVICE_NAME:
733 const guchar *name = NULL;
735 if (GST_V4L2_IS_OPEN (v4l2object))
736 name = v4l2object->vcap.card;
738 g_value_set_string (value, (gchar *) name);
743 if (GST_V4L2_IS_OPEN (v4l2object))
744 g_value_set_int (value, v4l2object->video_fd);
746 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
753 if (GST_V4L2_IS_OPEN (v4l2object)) {
754 flags |= v4l2object->device_caps &
755 (V4L2_CAP_VIDEO_CAPTURE |
756 V4L2_CAP_VIDEO_OUTPUT |
757 V4L2_CAP_VIDEO_OVERLAY |
758 V4L2_CAP_VBI_CAPTURE |
759 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
761 if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
762 flags |= V4L2_CAP_VIDEO_CAPTURE;
764 if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
765 flags |= V4L2_CAP_VIDEO_OUTPUT;
767 g_value_set_flags (value, flags);
770 case PROP_BRIGHTNESS:
772 case PROP_SATURATION:
775 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
778 if (GST_V4L2_IS_OPEN (v4l2object)) {
780 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
781 g_value_set_int (value, v);
789 g_value_set_enum (value, v4l2object->tv_norm);
792 g_value_set_enum (value, v4l2object->req_mode);
794 case PROP_CAPTURE_IO_MODE:
795 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
796 g_value_set_enum (value, v4l2object->req_mode);
798 case PROP_OUTPUT_IO_MODE:
799 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
800 g_value_set_enum (value, v4l2object->req_mode);
802 case PROP_EXTRA_CONTROLS:
803 gst_value_set_structure (value, v4l2object->extra_controls);
805 case PROP_PIXEL_ASPECT_RATIO:
807 g_value_transform (v4l2object->par, value);
809 case PROP_FORCE_ASPECT_RATIO:
810 g_value_set_boolean (value, v4l2object->keep_aspect);
820 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
822 struct v4l2_control control = { 0, };
824 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
826 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
827 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
829 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
831 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
832 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
833 "driver requires a minimum of %d buffers", control.value);
834 v4l2object->min_buffers = control.value;
836 v4l2object->min_buffers = 0;
841 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
843 GstTunerNorm *norm = NULL;
844 GstTunerChannel *channel = NULL;
847 if (!GST_IS_TUNER (v4l2object->element))
850 tuner = GST_TUNER (v4l2object->element);
852 if (v4l2object->tv_norm)
853 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
854 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
855 "norm=%p", (guint64) v4l2object->tv_norm, norm);
857 gst_tuner_set_norm (tuner, norm);
860 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
862 v4l2object->tv_norm =
863 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
864 gst_tuner_norm_changed (tuner, norm);
868 if (v4l2object->channel)
869 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
871 gst_tuner_set_channel (tuner, channel);
874 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
875 (v4l2object->element)));
877 g_free (v4l2object->channel);
878 v4l2object->channel = g_strdup (channel->label);
879 gst_tuner_channel_changed (tuner, channel);
884 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
885 if (v4l2object->frequency != 0) {
886 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
888 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
889 if (v4l2object->frequency == 0) {
891 gst_tuner_set_frequency (tuner, channel, 1000);
899 gst_v4l2_object_open (GstV4l2Object * v4l2object, GstV4l2Error * error)
901 if (gst_v4l2_open (v4l2object, error))
902 gst_v4l2_set_defaults (v4l2object);
910 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
914 ret = gst_v4l2_dup (v4l2object, other);
920 gst_v4l2_object_close (GstV4l2Object * v4l2object)
922 if (!gst_v4l2_close (v4l2object))
925 gst_caps_replace (&v4l2object->probed_caps, NULL);
927 /* reset our copy of the device caps */
928 v4l2object->device_caps = 0;
930 if (v4l2object->formats) {
931 gst_v4l2_object_clear_format_list (v4l2object);
934 if (v4l2object->par) {
935 g_value_unset (v4l2object->par);
936 g_free (v4l2object->par);
937 v4l2object->par = NULL;
940 if (v4l2object->channel) {
941 g_free (v4l2object->channel);
942 v4l2object->channel = NULL;
948 static struct v4l2_fmtdesc *
949 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
952 struct v4l2_fmtdesc *fmt;
958 walk = gst_v4l2_object_get_format_list (v4l2object);
960 fmt = (struct v4l2_fmtdesc *) walk->data;
961 if (fmt->pixelformat == fourcc)
963 /* special case for jpeg */
964 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
965 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
966 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
967 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
968 fourcc == V4L2_PIX_FMT_PJPG) {
972 walk = g_slist_next (walk);
980 /* complete made up ranking, the values themselves are meaningless */
981 /* These ranks MUST be X such that X<<15 fits on a signed int - see
982 the comment at the end of gst_v4l2_object_format_get_rank. */
983 #define YUV_BASE_RANK 1000
984 #define JPEG_BASE_RANK 500
985 #define DV_BASE_RANK 200
986 #define RGB_BASE_RANK 100
987 #define YUV_ODD_BASE_RANK 50
988 #define RGB_ODD_BASE_RANK 25
989 #define BAYER_BASE_RANK 15
990 #define S910_BASE_RANK 10
991 #define GREY_BASE_RANK 5
992 #define PWC_BASE_RANK 1
995 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
997 guint32 fourcc = fmt->pixelformat;
998 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
1002 case V4L2_PIX_FMT_MJPEG:
1003 case V4L2_PIX_FMT_PJPG:
1004 rank = JPEG_BASE_RANK;
1006 case V4L2_PIX_FMT_JPEG:
1007 rank = JPEG_BASE_RANK + 1;
1009 case V4L2_PIX_FMT_MPEG: /* MPEG */
1010 rank = JPEG_BASE_RANK + 2;
1013 case V4L2_PIX_FMT_RGB332:
1014 case V4L2_PIX_FMT_ARGB555:
1015 case V4L2_PIX_FMT_XRGB555:
1016 case V4L2_PIX_FMT_RGB555:
1017 case V4L2_PIX_FMT_ARGB555X:
1018 case V4L2_PIX_FMT_XRGB555X:
1019 case V4L2_PIX_FMT_RGB555X:
1020 case V4L2_PIX_FMT_BGR666:
1021 case V4L2_PIX_FMT_RGB565:
1022 case V4L2_PIX_FMT_RGB565X:
1023 case V4L2_PIX_FMT_RGB444:
1024 case V4L2_PIX_FMT_Y4:
1025 case V4L2_PIX_FMT_Y6:
1026 case V4L2_PIX_FMT_Y10:
1027 case V4L2_PIX_FMT_Y12:
1028 case V4L2_PIX_FMT_Y10BPACK:
1029 case V4L2_PIX_FMT_YUV555:
1030 case V4L2_PIX_FMT_YUV565:
1031 case V4L2_PIX_FMT_YUV32:
1032 case V4L2_PIX_FMT_NV12MT_16X16:
1033 case V4L2_PIX_FMT_NV42:
1034 case V4L2_PIX_FMT_H264_MVC:
1035 rank = RGB_ODD_BASE_RANK;
1038 case V4L2_PIX_FMT_RGB24:
1039 case V4L2_PIX_FMT_BGR24:
1040 rank = RGB_BASE_RANK - 1;
1043 case V4L2_PIX_FMT_RGB32:
1044 case V4L2_PIX_FMT_BGR32:
1045 case V4L2_PIX_FMT_ABGR32:
1046 case V4L2_PIX_FMT_XBGR32:
1047 case V4L2_PIX_FMT_BGRA32:
1048 case V4L2_PIX_FMT_BGRX32:
1049 case V4L2_PIX_FMT_RGBA32:
1050 case V4L2_PIX_FMT_RGBX32:
1051 case V4L2_PIX_FMT_ARGB32:
1052 case V4L2_PIX_FMT_XRGB32:
1053 rank = RGB_BASE_RANK;
1056 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1057 rank = GREY_BASE_RANK;
1060 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
1061 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1062 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1063 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1064 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1065 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1066 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1067 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1068 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1069 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1070 case V4L2_PIX_FMT_MM21: /* NV12 Y 16x32, UV 16x16 tile */
1071 case V4L2_PIX_FMT_NV12M_8L128:
1072 case V4L2_PIX_FMT_NV12M_10BE_8L128:
1073 rank = YUV_ODD_BASE_RANK;
1076 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1077 rank = YUV_BASE_RANK + 3;
1079 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1080 rank = YUV_BASE_RANK + 2;
1082 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1083 case V4L2_PIX_FMT_YUV420M:
1084 rank = YUV_BASE_RANK + 7;
1086 case V4L2_PIX_FMT_NV12: /* Y/CbCr 4:2:0, 12 bits per pixel */
1087 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
1088 rank = YUV_BASE_RANK + 8;
1090 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1091 rank = YUV_BASE_RANK + 10;
1093 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1094 rank = YUV_BASE_RANK + 6;
1096 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1097 rank = YUV_BASE_RANK + 9;
1099 case V4L2_PIX_FMT_YUV444:
1100 rank = YUV_BASE_RANK + 6;
1102 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1103 rank = YUV_BASE_RANK + 5;
1105 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1106 rank = YUV_BASE_RANK + 4;
1108 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1109 rank = YUV_BASE_RANK + 8;
1112 case V4L2_PIX_FMT_DV:
1113 rank = DV_BASE_RANK;
1116 case V4L2_PIX_FMT_WNVA: /* Winnov hw compress */
1120 case V4L2_PIX_FMT_SBGGR8:
1121 case V4L2_PIX_FMT_SGBRG8:
1122 case V4L2_PIX_FMT_SGRBG8:
1123 case V4L2_PIX_FMT_SRGGB8:
1124 rank = BAYER_BASE_RANK;
1127 case V4L2_PIX_FMT_SN9C10X:
1128 rank = S910_BASE_RANK;
1131 case V4L2_PIX_FMT_PWC1:
1132 rank = PWC_BASE_RANK;
1134 case V4L2_PIX_FMT_PWC2:
1135 rank = PWC_BASE_RANK;
1143 /* All ranks are below 1<<15 so a shift by 15
1144 * will a) make all non-emulated formats larger
1145 * than emulated and b) will not overflow
1156 format_cmp_func (gconstpointer a, gconstpointer b)
1158 const struct v4l2_fmtdesc *fa = a;
1159 const struct v4l2_fmtdesc *fb = b;
1161 if (fa->pixelformat == fb->pixelformat)
1164 return gst_v4l2_object_format_get_rank (fb) -
1165 gst_v4l2_object_format_get_rank (fa);
1168 /******************************************************
1169 * gst_v4l2_object_fill_format_list():
1170 * create list of supported capture formats
1171 * return value: TRUE on success, FALSE on error
1172 ******************************************************/
1174 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1175 enum v4l2_buf_type type)
1178 struct v4l2_fmtdesc *format;
1180 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting src format enumerations");
1182 /* format enumeration */
1184 format = g_new0 (struct v4l2_fmtdesc, 1);
1187 format->type = type;
1189 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1190 if (errno == EINVAL) {
1192 break; /* end of enumeration */
1198 GST_LOG_OBJECT (v4l2object->dbg_obj, "index: %u", format->index);
1199 GST_LOG_OBJECT (v4l2object->dbg_obj, "type: %d", format->type);
1200 GST_LOG_OBJECT (v4l2object->dbg_obj, "flags: %08x", format->flags);
1201 GST_LOG_OBJECT (v4l2object->dbg_obj, "description: '%s'",
1202 format->description);
1203 GST_LOG_OBJECT (v4l2object->dbg_obj, "pixelformat: %" GST_FOURCC_FORMAT,
1204 GST_FOURCC_ARGS (format->pixelformat));
1206 /* sort formats according to our preference; we do this, because caps
1207 * are probed in the order the formats are in the list, and the order of
1208 * formats in the final probed caps matters for things like fixation */
1209 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1210 (GCompareFunc) format_cmp_func);
1213 #ifndef GST_DISABLE_GST_DEBUG
1217 GST_INFO_OBJECT (v4l2object->dbg_obj, "got %d format(s):", n);
1218 for (l = v4l2object->formats; l != NULL; l = l->next) {
1221 GST_INFO_OBJECT (v4l2object->dbg_obj,
1222 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1223 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1235 if (v4l2object->element)
1238 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1239 (_("Failed to enumerate possible video formats device '%s' can work "
1240 "with"), v4l2object->videodev),
1241 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1242 n, v4l2object->videodev, errno, g_strerror (errno)));
1249 * Get the list of supported capture formats, a list of
1250 * `struct v4l2_fmtdesc`.
1253 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1255 if (!v4l2object->formats) {
1257 /* check usual way */
1258 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1260 /* if our driver supports multi-planar
1261 * and if formats are still empty then we can workaround driver bug
1262 * by also looking up formats as if our device was not supporting
1264 if (!v4l2object->formats) {
1265 switch (v4l2object->type) {
1266 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1267 gst_v4l2_object_fill_format_list (v4l2object,
1268 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1271 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1272 gst_v4l2_object_fill_format_list (v4l2object,
1273 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1281 return v4l2object->formats;
1284 static GstVideoFormat
1285 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1287 GstVideoFormat format;
1290 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1291 format = GST_VIDEO_FORMAT_GRAY8;
1293 case V4L2_PIX_FMT_Y16:
1294 format = GST_VIDEO_FORMAT_GRAY16_LE;
1296 case V4L2_PIX_FMT_Y16_BE:
1297 format = GST_VIDEO_FORMAT_GRAY16_BE;
1299 case V4L2_PIX_FMT_XRGB555:
1300 case V4L2_PIX_FMT_RGB555:
1301 format = GST_VIDEO_FORMAT_RGB15;
1303 case V4L2_PIX_FMT_XRGB555X:
1304 case V4L2_PIX_FMT_RGB555X:
1305 format = GST_VIDEO_FORMAT_BGR15;
1307 case V4L2_PIX_FMT_RGB565:
1308 format = GST_VIDEO_FORMAT_RGB16;
1310 case V4L2_PIX_FMT_RGB24:
1311 format = GST_VIDEO_FORMAT_RGB;
1313 case V4L2_PIX_FMT_BGR24:
1314 format = GST_VIDEO_FORMAT_BGR;
1316 case V4L2_PIX_FMT_XRGB32:
1317 case V4L2_PIX_FMT_RGB32:
1318 format = GST_VIDEO_FORMAT_xRGB;
1320 case V4L2_PIX_FMT_RGBX32:
1321 format = GST_VIDEO_FORMAT_RGBx;
1323 case V4L2_PIX_FMT_XBGR32:
1324 case V4L2_PIX_FMT_BGR32:
1325 format = GST_VIDEO_FORMAT_BGRx;
1327 case V4L2_PIX_FMT_BGRX32:
1328 format = GST_VIDEO_FORMAT_xBGR;
1330 case V4L2_PIX_FMT_ABGR32:
1331 format = GST_VIDEO_FORMAT_BGRA;
1333 case V4L2_PIX_FMT_BGRA32:
1334 format = GST_VIDEO_FORMAT_ABGR;
1336 case V4L2_PIX_FMT_RGBA32:
1337 format = GST_VIDEO_FORMAT_RGBA;
1339 case V4L2_PIX_FMT_ARGB32:
1340 format = GST_VIDEO_FORMAT_ARGB;
1342 case V4L2_PIX_FMT_NV12:
1343 case V4L2_PIX_FMT_NV12M:
1344 format = GST_VIDEO_FORMAT_NV12;
1346 case V4L2_PIX_FMT_NV12MT:
1347 format = GST_VIDEO_FORMAT_NV12_64Z32;
1349 case V4L2_PIX_FMT_MM21:
1350 format = GST_VIDEO_FORMAT_NV12_16L32S;
1352 case V4L2_PIX_FMT_NV12M_8L128:
1353 format = GST_VIDEO_FORMAT_NV12_8L128;
1355 case V4L2_PIX_FMT_NV12M_10BE_8L128:
1356 format = GST_VIDEO_FORMAT_NV12_10BE_8L128;
1358 case V4L2_PIX_FMT_NV21:
1359 case V4L2_PIX_FMT_NV21M:
1360 format = GST_VIDEO_FORMAT_NV21;
1362 case V4L2_PIX_FMT_YVU410:
1363 format = GST_VIDEO_FORMAT_YVU9;
1365 case V4L2_PIX_FMT_YUV410:
1366 format = GST_VIDEO_FORMAT_YUV9;
1368 case V4L2_PIX_FMT_YUV420:
1369 case V4L2_PIX_FMT_YUV420M:
1370 format = GST_VIDEO_FORMAT_I420;
1372 case V4L2_PIX_FMT_YUYV:
1373 format = GST_VIDEO_FORMAT_YUY2;
1375 case V4L2_PIX_FMT_YVU420:
1376 format = GST_VIDEO_FORMAT_YV12;
1378 case V4L2_PIX_FMT_UYVY:
1379 format = GST_VIDEO_FORMAT_UYVY;
1381 case V4L2_PIX_FMT_YUV411P:
1382 format = GST_VIDEO_FORMAT_Y41B;
1384 case V4L2_PIX_FMT_YUV422P:
1385 format = GST_VIDEO_FORMAT_Y42B;
1387 case V4L2_PIX_FMT_YVYU:
1388 format = GST_VIDEO_FORMAT_YVYU;
1390 case V4L2_PIX_FMT_NV16:
1391 case V4L2_PIX_FMT_NV16M:
1392 format = GST_VIDEO_FORMAT_NV16;
1394 case V4L2_PIX_FMT_NV61:
1395 case V4L2_PIX_FMT_NV61M:
1396 format = GST_VIDEO_FORMAT_NV61;
1398 case V4L2_PIX_FMT_NV24:
1399 format = GST_VIDEO_FORMAT_NV24;
1402 format = GST_VIDEO_FORMAT_UNKNOWN;
1410 gst_v4l2_object_v4l2fourcc_is_rgb (guint32 fourcc)
1412 gboolean ret = FALSE;
1415 case V4L2_PIX_FMT_XRGB555:
1416 case V4L2_PIX_FMT_RGB555:
1417 case V4L2_PIX_FMT_XRGB555X:
1418 case V4L2_PIX_FMT_RGB555X:
1419 case V4L2_PIX_FMT_RGB565:
1420 case V4L2_PIX_FMT_RGB24:
1421 case V4L2_PIX_FMT_BGR24:
1422 case V4L2_PIX_FMT_XRGB32:
1423 case V4L2_PIX_FMT_RGB32:
1424 case V4L2_PIX_FMT_RGBA32:
1425 case V4L2_PIX_FMT_RGBX32:
1426 case V4L2_PIX_FMT_XBGR32:
1427 case V4L2_PIX_FMT_BGR32:
1428 case V4L2_PIX_FMT_BGRA32:
1429 case V4L2_PIX_FMT_BGRX32:
1430 case V4L2_PIX_FMT_ABGR32:
1431 case V4L2_PIX_FMT_ARGB32:
1432 case V4L2_PIX_FMT_SBGGR8:
1433 case V4L2_PIX_FMT_SGBRG8:
1434 case V4L2_PIX_FMT_SGRBG8:
1435 case V4L2_PIX_FMT_SRGGB8:
1445 static GstStructure *
1446 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1448 GstStructure *structure = NULL;
1451 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1452 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1453 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1454 structure = gst_structure_new_empty ("image/jpeg");
1456 case V4L2_PIX_FMT_MPEG1:
1457 structure = gst_structure_new ("video/mpeg",
1458 "mpegversion", G_TYPE_INT, 1, NULL);
1460 case V4L2_PIX_FMT_MPEG2:
1461 structure = gst_structure_new ("video/mpeg",
1462 "mpegversion", G_TYPE_INT, 2, NULL);
1464 case V4L2_PIX_FMT_MPEG4:
1465 case V4L2_PIX_FMT_XVID:
1466 structure = gst_structure_new ("video/mpeg",
1467 "mpegversion", G_TYPE_INT, 4, "systemstream",
1468 G_TYPE_BOOLEAN, FALSE, NULL);
1470 case V4L2_PIX_FMT_FWHT:
1471 structure = gst_structure_new_empty ("video/x-fwht");
1473 case V4L2_PIX_FMT_H263:
1474 structure = gst_structure_new ("video/x-h263",
1475 "variant", G_TYPE_STRING, "itu", NULL);
1477 case V4L2_PIX_FMT_H264: /* H.264 */
1478 structure = gst_structure_new ("video/x-h264",
1479 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1480 G_TYPE_STRING, "au", NULL);
1482 case V4L2_PIX_FMT_H264_NO_SC:
1483 structure = gst_structure_new ("video/x-h264",
1484 "stream-format", G_TYPE_STRING, "avc", "alignment",
1485 G_TYPE_STRING, "au", NULL);
1487 case V4L2_PIX_FMT_HEVC: /* H.265 */
1488 structure = gst_structure_new ("video/x-h265",
1489 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1490 G_TYPE_STRING, "au", NULL);
1492 case V4L2_PIX_FMT_VC1_ANNEX_G:
1493 case V4L2_PIX_FMT_VC1_ANNEX_L:
1494 structure = gst_structure_new ("video/x-wmv",
1495 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1497 case V4L2_PIX_FMT_VP8:
1498 structure = gst_structure_new_empty ("video/x-vp8");
1500 case V4L2_PIX_FMT_VP9:
1501 structure = gst_structure_new_empty ("video/x-vp9");
1503 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1504 case V4L2_PIX_FMT_Y16:
1505 case V4L2_PIX_FMT_Y16_BE:
1506 case V4L2_PIX_FMT_XRGB555:
1507 case V4L2_PIX_FMT_RGB555:
1508 case V4L2_PIX_FMT_XRGB555X:
1509 case V4L2_PIX_FMT_RGB555X:
1510 case V4L2_PIX_FMT_RGB565:
1511 case V4L2_PIX_FMT_RGB24:
1512 case V4L2_PIX_FMT_BGR24:
1513 case V4L2_PIX_FMT_RGB32:
1514 case V4L2_PIX_FMT_XRGB32:
1515 case V4L2_PIX_FMT_ARGB32:
1516 case V4L2_PIX_FMT_RGBX32:
1517 case V4L2_PIX_FMT_RGBA32:
1518 case V4L2_PIX_FMT_BGR32:
1519 case V4L2_PIX_FMT_BGRX32:
1520 case V4L2_PIX_FMT_BGRA32:
1521 case V4L2_PIX_FMT_XBGR32:
1522 case V4L2_PIX_FMT_ABGR32:
1523 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1524 case V4L2_PIX_FMT_NV12M:
1525 case V4L2_PIX_FMT_NV12MT:
1526 case V4L2_PIX_FMT_MM21:
1527 case V4L2_PIX_FMT_NV12M_8L128:
1528 case V4L2_PIX_FMT_NV12M_10BE_8L128:
1529 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1530 case V4L2_PIX_FMT_NV21M:
1531 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1532 case V4L2_PIX_FMT_NV16M:
1533 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1534 case V4L2_PIX_FMT_NV61M:
1535 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1536 case V4L2_PIX_FMT_YVU410:
1537 case V4L2_PIX_FMT_YUV410:
1538 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1539 case V4L2_PIX_FMT_YUV420M:
1540 case V4L2_PIX_FMT_YUYV:
1541 case V4L2_PIX_FMT_YVU420:
1542 case V4L2_PIX_FMT_UYVY:
1543 case V4L2_PIX_FMT_YUV422P:
1544 case V4L2_PIX_FMT_YVYU:
1545 case V4L2_PIX_FMT_YUV411P:{
1546 GstVideoFormat format;
1547 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1548 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1549 structure = gst_structure_new ("video/x-raw",
1550 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1553 case V4L2_PIX_FMT_DV:
1555 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1558 case V4L2_PIX_FMT_MPEG: /* MPEG */
1559 structure = gst_structure_new ("video/mpegts",
1560 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1562 case V4L2_PIX_FMT_WNVA: /* Winnov hw compress */
1564 case V4L2_PIX_FMT_SBGGR8:
1565 case V4L2_PIX_FMT_SGBRG8:
1566 case V4L2_PIX_FMT_SGRBG8:
1567 case V4L2_PIX_FMT_SRGGB8:
1568 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1569 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1570 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1571 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1572 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1574 case V4L2_PIX_FMT_SN9C10X:
1575 structure = gst_structure_new_empty ("video/x-sonix");
1577 case V4L2_PIX_FMT_PWC1:
1578 structure = gst_structure_new_empty ("video/x-pwc1");
1580 case V4L2_PIX_FMT_PWC2:
1581 structure = gst_structure_new_empty ("video/x-pwc2");
1583 case V4L2_PIX_FMT_RGB332:
1584 case V4L2_PIX_FMT_BGR666:
1585 case V4L2_PIX_FMT_ARGB555X:
1586 case V4L2_PIX_FMT_RGB565X:
1587 case V4L2_PIX_FMT_RGB444:
1588 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1589 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1590 case V4L2_PIX_FMT_Y4:
1591 case V4L2_PIX_FMT_Y6:
1592 case V4L2_PIX_FMT_Y10:
1593 case V4L2_PIX_FMT_Y12:
1594 case V4L2_PIX_FMT_Y10BPACK:
1595 case V4L2_PIX_FMT_YUV444:
1596 case V4L2_PIX_FMT_YUV555:
1597 case V4L2_PIX_FMT_YUV565:
1598 case V4L2_PIX_FMT_Y41P:
1599 case V4L2_PIX_FMT_YUV32:
1600 case V4L2_PIX_FMT_NV12MT_16X16:
1601 case V4L2_PIX_FMT_NV42:
1602 case V4L2_PIX_FMT_H264_MVC:
1604 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1605 fourcc, GST_FOURCC_ARGS (fourcc));
1613 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1615 GstStructure *template;
1618 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1620 if (template == NULL)
1623 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1624 if (gst_v4l2_formats[i].format != fourcc)
1627 if (gst_v4l2_formats[i].dimensions) {
1628 gst_structure_set (template,
1629 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1630 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1631 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1640 /* Add an 'alternate' variant of the caps with the feature */
1642 add_alternate_variant (GstV4l2Object * v4l2object, GstCaps * caps,
1643 GstStructure * structure)
1645 GstStructure *alt_s;
1647 if (v4l2object && v4l2object->never_interlaced)
1650 if (!gst_structure_has_name (structure, "video/x-raw"))
1653 alt_s = gst_structure_copy (structure);
1654 gst_structure_set (alt_s, "interlace-mode", G_TYPE_STRING, "alternate", NULL);
1656 gst_caps_append_structure_full (caps, alt_s,
1657 gst_caps_features_new (GST_CAPS_FEATURE_FORMAT_INTERLACED, NULL));
1661 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1663 GstStructure *structure;
1664 GstCaps *caps, *caps_interlaced;
1667 caps = gst_caps_new_empty ();
1668 caps_interlaced = gst_caps_new_empty ();
1669 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1671 if ((gst_v4l2_formats[i].flags & flags) == 0)
1675 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1678 GstStructure *alt_s = NULL;
1680 if (gst_v4l2_formats[i].dimensions) {
1681 gst_structure_set (structure,
1682 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1683 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1684 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1687 switch (gst_v4l2_formats[i].format) {
1688 case V4L2_PIX_FMT_RGB32:
1689 alt_s = gst_structure_copy (structure);
1690 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1692 case V4L2_PIX_FMT_BGR32:
1693 alt_s = gst_structure_copy (structure);
1694 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1699 gst_caps_append_structure (caps, structure);
1702 gst_caps_append_structure (caps, alt_s);
1703 add_alternate_variant (NULL, caps_interlaced, alt_s);
1706 add_alternate_variant (NULL, caps_interlaced, structure);
1710 caps = gst_caps_simplify (caps);
1711 caps_interlaced = gst_caps_simplify (caps_interlaced);
1713 return gst_caps_merge (caps, caps_interlaced);
1717 gst_v4l2_object_get_all_caps (void)
1719 static GstCaps *caps = NULL;
1721 if (g_once_init_enter (&caps)) {
1722 GstCaps *all_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1723 GST_MINI_OBJECT_FLAG_SET (all_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1724 g_once_init_leave (&caps, all_caps);
1731 gst_v4l2_object_get_raw_caps (void)
1733 static GstCaps *caps = NULL;
1735 if (g_once_init_enter (&caps)) {
1736 GstCaps *raw_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1737 GST_MINI_OBJECT_FLAG_SET (raw_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1738 g_once_init_leave (&caps, raw_caps);
1745 gst_v4l2_object_get_codec_caps (void)
1747 static GstCaps *caps = NULL;
1749 if (g_once_init_enter (&caps)) {
1750 GstCaps *codec_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1751 GST_MINI_OBJECT_FLAG_SET (codec_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1752 g_once_init_leave (&caps, codec_caps);
1758 /* collect data for the given caps
1759 * @caps: given input caps
1760 * @format: location for the v4l format
1761 * @w/@h: location for width and height
1762 * @fps_n/@fps_d: location for framerate
1763 * @size: location for expected size of the frame or 0 if unknown
1766 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1767 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1769 GstStructure *structure;
1770 guint32 fourcc = 0, fourcc_nc = 0;
1771 const gchar *mimetype;
1772 struct v4l2_fmtdesc *fmt = NULL;
1774 structure = gst_caps_get_structure (caps, 0);
1776 mimetype = gst_structure_get_name (structure);
1778 if (!gst_video_info_from_caps (info, caps))
1779 goto invalid_format;
1781 if (g_str_equal (mimetype, "video/x-raw")) {
1782 switch (GST_VIDEO_INFO_FORMAT (info)) {
1783 case GST_VIDEO_FORMAT_I420:
1784 fourcc = V4L2_PIX_FMT_YUV420;
1785 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1787 case GST_VIDEO_FORMAT_YUY2:
1788 fourcc = V4L2_PIX_FMT_YUYV;
1790 case GST_VIDEO_FORMAT_UYVY:
1791 fourcc = V4L2_PIX_FMT_UYVY;
1793 case GST_VIDEO_FORMAT_YV12:
1794 fourcc = V4L2_PIX_FMT_YVU420;
1796 case GST_VIDEO_FORMAT_Y41B:
1797 fourcc = V4L2_PIX_FMT_YUV411P;
1799 case GST_VIDEO_FORMAT_Y42B:
1800 fourcc = V4L2_PIX_FMT_YUV422P;
1802 case GST_VIDEO_FORMAT_NV12:
1803 fourcc = V4L2_PIX_FMT_NV12;
1804 fourcc_nc = V4L2_PIX_FMT_NV12M;
1806 case GST_VIDEO_FORMAT_NV12_64Z32:
1807 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1809 case GST_VIDEO_FORMAT_NV12_16L32S:
1810 fourcc_nc = V4L2_PIX_FMT_MM21;
1812 case GST_VIDEO_FORMAT_NV12_10BE_8L128:
1813 fourcc_nc = V4L2_PIX_FMT_NV12M_10BE_8L128;
1815 case GST_VIDEO_FORMAT_NV21:
1816 fourcc = V4L2_PIX_FMT_NV21;
1817 fourcc_nc = V4L2_PIX_FMT_NV21M;
1819 case GST_VIDEO_FORMAT_NV16:
1820 fourcc = V4L2_PIX_FMT_NV16;
1821 fourcc_nc = V4L2_PIX_FMT_NV16M;
1823 case GST_VIDEO_FORMAT_NV61:
1824 fourcc = V4L2_PIX_FMT_NV61;
1825 fourcc_nc = V4L2_PIX_FMT_NV61M;
1827 case GST_VIDEO_FORMAT_NV24:
1828 fourcc = V4L2_PIX_FMT_NV24;
1830 case GST_VIDEO_FORMAT_YVYU:
1831 fourcc = V4L2_PIX_FMT_YVYU;
1833 case GST_VIDEO_FORMAT_RGB15:
1834 fourcc = V4L2_PIX_FMT_RGB555;
1835 fourcc_nc = V4L2_PIX_FMT_XRGB555;
1837 case GST_VIDEO_FORMAT_RGB16:
1838 fourcc = V4L2_PIX_FMT_RGB565;
1840 case GST_VIDEO_FORMAT_RGB:
1841 fourcc = V4L2_PIX_FMT_RGB24;
1843 case GST_VIDEO_FORMAT_BGR:
1844 fourcc = V4L2_PIX_FMT_BGR24;
1846 case GST_VIDEO_FORMAT_xRGB:
1847 fourcc = V4L2_PIX_FMT_RGB32;
1848 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1850 case GST_VIDEO_FORMAT_RGBx:
1851 fourcc = V4L2_PIX_FMT_RGBX32;
1853 case GST_VIDEO_FORMAT_ARGB:
1854 fourcc = V4L2_PIX_FMT_RGB32;
1855 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1857 case GST_VIDEO_FORMAT_RGBA:
1858 fourcc = V4L2_PIX_FMT_RGBA32;
1860 case GST_VIDEO_FORMAT_BGRx:
1861 fourcc = V4L2_PIX_FMT_BGR32;
1862 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1864 case GST_VIDEO_FORMAT_xBGR:
1865 fourcc = V4L2_PIX_FMT_BGRX32;
1867 case GST_VIDEO_FORMAT_BGRA:
1868 fourcc = V4L2_PIX_FMT_BGR32;
1869 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1871 case GST_VIDEO_FORMAT_ABGR:
1872 fourcc = V4L2_PIX_FMT_BGRA32;
1874 case GST_VIDEO_FORMAT_GRAY8:
1875 fourcc = V4L2_PIX_FMT_GREY;
1877 case GST_VIDEO_FORMAT_GRAY16_LE:
1878 fourcc = V4L2_PIX_FMT_Y16;
1880 case GST_VIDEO_FORMAT_GRAY16_BE:
1881 fourcc = V4L2_PIX_FMT_Y16_BE;
1883 case GST_VIDEO_FORMAT_BGR15:
1884 fourcc = V4L2_PIX_FMT_RGB555X;
1885 fourcc_nc = V4L2_PIX_FMT_XRGB555X;
1891 if (g_str_equal (mimetype, "video/mpegts")) {
1892 fourcc = V4L2_PIX_FMT_MPEG;
1893 } else if (g_str_equal (mimetype, "video/x-dv")) {
1894 fourcc = V4L2_PIX_FMT_DV;
1895 } else if (g_str_equal (mimetype, "image/jpeg")) {
1896 fourcc = V4L2_PIX_FMT_JPEG;
1897 } else if (g_str_equal (mimetype, "video/mpeg")) {
1899 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1902 fourcc = V4L2_PIX_FMT_MPEG1;
1905 fourcc = V4L2_PIX_FMT_MPEG2;
1908 fourcc = V4L2_PIX_FMT_MPEG4;
1909 fourcc_nc = V4L2_PIX_FMT_XVID;
1915 } else if (g_str_equal (mimetype, "video/x-fwht")) {
1916 fourcc = V4L2_PIX_FMT_FWHT;
1917 } else if (g_str_equal (mimetype, "video/x-h263")) {
1918 fourcc = V4L2_PIX_FMT_H263;
1919 } else if (g_str_equal (mimetype, "video/x-h264")) {
1920 const gchar *stream_format =
1921 gst_structure_get_string (structure, "stream-format");
1922 if (g_str_equal (stream_format, "avc"))
1923 fourcc = V4L2_PIX_FMT_H264_NO_SC;
1925 fourcc = V4L2_PIX_FMT_H264;
1926 } else if (g_str_equal (mimetype, "video/x-h265")) {
1927 fourcc = V4L2_PIX_FMT_HEVC;
1928 } else if (g_str_equal (mimetype, "video/x-vp8")) {
1929 fourcc = V4L2_PIX_FMT_VP8;
1930 } else if (g_str_equal (mimetype, "video/x-vp9")) {
1931 fourcc = V4L2_PIX_FMT_VP9;
1932 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1933 const gchar *format = gst_structure_get_string (structure, "format");
1935 if (!g_ascii_strcasecmp (format, "bggr"))
1936 fourcc = V4L2_PIX_FMT_SBGGR8;
1937 else if (!g_ascii_strcasecmp (format, "gbrg"))
1938 fourcc = V4L2_PIX_FMT_SGBRG8;
1939 else if (!g_ascii_strcasecmp (format, "grbg"))
1940 fourcc = V4L2_PIX_FMT_SGRBG8;
1941 else if (!g_ascii_strcasecmp (format, "rggb"))
1942 fourcc = V4L2_PIX_FMT_SRGGB8;
1944 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1945 fourcc = V4L2_PIX_FMT_SN9C10X;
1946 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1947 fourcc = V4L2_PIX_FMT_PWC1;
1948 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1949 fourcc = V4L2_PIX_FMT_PWC2;
1954 /* Prefer the non-contiguous if supported */
1955 v4l2object->prefered_non_contiguous = TRUE;
1958 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
1959 else if (fourcc == 0)
1960 goto unhandled_format;
1963 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1964 v4l2object->prefered_non_contiguous = FALSE;
1968 goto unsupported_format;
1977 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1982 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1987 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1993 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1994 guint32 pixelformat, gint * width, gint * height);
1997 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1999 if (v4l2object->keep_aspect && v4l2object->par)
2000 gst_structure_set_value (s, "pixel-aspect-ratio", v4l2object->par);
2003 /* returns TRUE if the value was changed in place, otherwise FALSE */
2005 gst_v4l2src_value_simplify (GValue * val)
2007 /* simplify list of one value to one value */
2008 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
2009 const GValue *list_val;
2010 GValue new_val = G_VALUE_INIT;
2012 list_val = gst_value_list_get_value (val, 0);
2013 g_value_init (&new_val, G_VALUE_TYPE (list_val));
2014 g_value_copy (list_val, &new_val);
2015 g_value_unset (val);
2024 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
2025 GstVideoInterlaceMode * interlace_mode)
2028 case V4L2_FIELD_ANY:
2030 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
2032 case V4L2_FIELD_NONE:
2033 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
2035 case V4L2_FIELD_INTERLACED:
2036 case V4L2_FIELD_INTERLACED_TB:
2037 case V4L2_FIELD_INTERLACED_BT:
2038 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
2040 case V4L2_FIELD_ALTERNATE:
2041 *interlace_mode = GST_VIDEO_INTERLACE_MODE_ALTERNATE;
2044 GST_ERROR ("Unknown enum v4l2_field %d", field);
2050 gst_v4l2_object_get_colorspace (GstV4l2Object * v4l2object,
2051 struct v4l2_format *fmt, GstVideoColorimetry * cinfo)
2054 gst_v4l2_object_v4l2fourcc_is_rgb (fmt->fmt.pix.pixelformat);
2055 enum v4l2_colorspace colorspace;
2056 enum v4l2_quantization range;
2057 enum v4l2_ycbcr_encoding matrix;
2058 enum v4l2_xfer_func transfer;
2059 gboolean ret = TRUE;
2061 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
2062 colorspace = fmt->fmt.pix_mp.colorspace;
2063 range = fmt->fmt.pix_mp.quantization;
2064 matrix = fmt->fmt.pix_mp.ycbcr_enc;
2065 transfer = fmt->fmt.pix_mp.xfer_func;
2067 colorspace = fmt->fmt.pix.colorspace;
2068 range = fmt->fmt.pix.quantization;
2069 matrix = fmt->fmt.pix.ycbcr_enc;
2070 transfer = fmt->fmt.pix.xfer_func;
2073 /* First step, set the defaults for each primaries */
2074 switch (colorspace) {
2075 case V4L2_COLORSPACE_SMPTE170M:
2076 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2077 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2078 cinfo->transfer = GST_VIDEO_TRANSFER_BT601;
2079 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
2081 case V4L2_COLORSPACE_REC709:
2082 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2083 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2084 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2085 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
2087 case V4L2_COLORSPACE_SRGB:
2088 case V4L2_COLORSPACE_JPEG:
2089 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2090 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2091 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2092 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
2094 case V4L2_COLORSPACE_OPRGB:
2095 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2096 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2097 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2098 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
2100 case V4L2_COLORSPACE_BT2020:
2101 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2102 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2103 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2104 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
2106 case V4L2_COLORSPACE_SMPTE240M:
2107 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2108 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2109 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2110 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
2112 case V4L2_COLORSPACE_470_SYSTEM_M:
2113 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2114 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2115 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2116 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
2118 case V4L2_COLORSPACE_470_SYSTEM_BG:
2119 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2120 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2121 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2122 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
2124 case V4L2_COLORSPACE_RAW:
2125 /* Explicitly unknown */
2126 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2127 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2128 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2129 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
2132 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
2140 /* Second step, apply any custom variation */
2142 case V4L2_QUANTIZATION_FULL_RANGE:
2143 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2145 case V4L2_QUANTIZATION_LIM_RANGE:
2146 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2148 case V4L2_QUANTIZATION_DEFAULT:
2149 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
2150 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
2151 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2152 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601
2153 || matrix == V4L2_YCBCR_ENC_XV709
2154 || colorspace == V4L2_COLORSPACE_JPEG)
2155 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2157 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2160 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
2161 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2166 case V4L2_YCBCR_ENC_XV601:
2167 case V4L2_YCBCR_ENC_SYCC:
2168 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
2170 case V4L2_YCBCR_ENC_601:
2171 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2173 case V4L2_YCBCR_ENC_XV709:
2174 GST_FIXME ("XV709 not defined, assuming 709");
2176 case V4L2_YCBCR_ENC_709:
2177 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2179 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2180 GST_FIXME ("BT2020 with constant luma is not defined, assuming BT2020");
2182 case V4L2_YCBCR_ENC_BT2020:
2183 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2185 case V4L2_YCBCR_ENC_SMPTE240M:
2186 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2188 case V4L2_YCBCR_ENC_DEFAULT:
2189 /* nothing, just use defaults for colorspace */
2192 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2193 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2197 /* Set identity matrix for R'G'B' formats to avoid creating
2198 * confusion. This though is cosmetic as it's now properly ignored by
2199 * the video info API and videoconvert. */
2201 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2204 case V4L2_XFER_FUNC_709:
2205 if (colorspace == V4L2_COLORSPACE_BT2020 && fmt->fmt.pix.height >= 2160)
2206 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2207 else if (colorspace == V4L2_COLORSPACE_SMPTE170M)
2208 cinfo->transfer = GST_VIDEO_TRANSFER_BT601;
2210 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2212 if (v4l2object->transfer)
2213 cinfo->transfer = v4l2object->transfer;
2215 case V4L2_XFER_FUNC_SRGB:
2216 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2218 case V4L2_XFER_FUNC_OPRGB:
2219 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2221 case V4L2_XFER_FUNC_SMPTE240M:
2222 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2224 case V4L2_XFER_FUNC_NONE:
2225 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2227 case V4L2_XFER_FUNC_SMPTE2084:
2228 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE2084;
2230 case V4L2_XFER_FUNC_DEFAULT:
2231 /* nothing, just use defaults for colorspace */
2234 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2235 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2244 gst_v4l2_object_get_streamparm (GstV4l2Object * v4l2object, GstVideoInfo * info)
2246 struct v4l2_streamparm streamparm;
2247 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
2248 streamparm.type = v4l2object->type;
2249 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_PARM, &streamparm) < 0) {
2250 GST_WARNING_OBJECT (v4l2object->dbg_obj, "VIDIOC_G_PARM failed");
2253 if ((streamparm.parm.capture.timeperframe.numerator != 0)
2254 && (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
2255 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2256 GST_VIDEO_INFO_FPS_N (info) =
2257 streamparm.parm.capture.timeperframe.denominator;
2258 GST_VIDEO_INFO_FPS_D (info) =
2259 streamparm.parm.capture.timeperframe.numerator;
2265 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2266 struct v4l2_format *try_fmt)
2268 int fd = v4l2object->video_fd;
2269 struct v4l2_format fmt;
2272 memcpy (&fmt, try_fmt, sizeof (fmt));
2273 r = v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2275 if (r < 0 && errno == ENOTTY) {
2276 /* The driver might not implement TRY_FMT, in which case we will try
2278 if (GST_V4L2_IS_ACTIVE (v4l2object))
2281 memcpy (&fmt, try_fmt, sizeof (fmt));
2282 r = v4l2object->ioctl (fd, VIDIOC_S_FMT, &fmt);
2284 memcpy (try_fmt, &fmt, sizeof (fmt));
2289 memcpy (try_fmt, &fmt, sizeof (fmt));
2290 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2291 "Unable to try format: %s", g_strerror (errno));
2297 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2298 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2300 struct v4l2_format fmt;
2301 GValue interlace_formats = { 0, };
2302 enum v4l2_field formats[] = { V4L2_FIELD_NONE,
2303 V4L2_FIELD_INTERLACED, V4L2_FIELD_ALTERNATE
2306 GstVideoInterlaceMode interlace_mode, prev = -1;
2308 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2311 if (v4l2object->never_interlaced) {
2312 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2316 g_value_init (&interlace_formats, GST_TYPE_LIST);
2318 /* Try thrice - once for NONE, once for INTERLACED and once for ALTERNATE. */
2319 for (i = 0; i < G_N_ELEMENTS (formats); i++) {
2320 memset (&fmt, 0, sizeof (fmt));
2321 fmt.type = v4l2object->type;
2322 fmt.fmt.pix.width = width;
2323 fmt.fmt.pix.height = height;
2324 fmt.fmt.pix.pixelformat = pixelformat;
2325 fmt.fmt.pix.field = formats[i];
2327 if (fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE)
2328 fmt.fmt.pix.height /= 2;
2330 /* if skip_try_fmt_probes is set it's up to the caller to filter out the
2331 * formats from the formats requested by peer.
2332 * For this negotiation to work with 'alternate' we need the caps to contain
2333 * the feature so we have an intersection with downstream caps.
2335 if (!v4l2object->skip_try_fmt_probes
2336 && gst_v4l2_object_try_fmt (v4l2object, &fmt) != 0)
2339 if (gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)
2340 && prev != interlace_mode) {
2341 GValue interlace_enum = { 0, };
2342 const gchar *mode_string;
2343 g_value_init (&interlace_enum, G_TYPE_STRING);
2344 mode_string = gst_video_interlace_mode_to_string (interlace_mode);
2345 g_value_set_string (&interlace_enum, mode_string);
2346 gst_value_list_append_and_take_value (&interlace_formats,
2348 prev = interlace_mode;
2352 if (gst_v4l2src_value_simplify (&interlace_formats)
2353 || gst_value_list_get_size (&interlace_formats) > 0)
2354 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2356 GST_WARNING_OBJECT (v4l2object, "Failed to determine interlace mode");
2362 gst_v4l2_object_fill_colorimetry_list (GValue * list,
2363 GstVideoColorimetry * cinfo)
2365 GValue colorimetry = G_VALUE_INIT;
2368 gboolean found = FALSE;
2370 g_value_init (&colorimetry, G_TYPE_STRING);
2371 g_value_take_string (&colorimetry, gst_video_colorimetry_to_string (cinfo));
2373 /* only insert if no duplicate */
2374 size = gst_value_list_get_size (list);
2375 for (i = 0; i < size; i++) {
2378 tmp = gst_value_list_get_value (list, i);
2379 if (gst_value_compare (&colorimetry, tmp) == GST_VALUE_EQUAL) {
2386 gst_value_list_append_and_take_value (list, &colorimetry);
2388 g_value_unset (&colorimetry);
2392 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2393 guint32 width, guint32 height, guint32 pixelformat)
2395 struct v4l2_format fmt;
2396 GValue list = G_VALUE_INIT;
2397 GstVideoColorimetry cinfo;
2398 enum v4l2_colorspace req_cspace;
2400 memset (&fmt, 0, sizeof (fmt));
2401 fmt.type = v4l2object->type;
2402 fmt.fmt.pix.width = width;
2403 fmt.fmt.pix.height = height;
2404 fmt.fmt.pix.pixelformat = pixelformat;
2406 g_value_init (&list, GST_TYPE_LIST);
2408 /* step 1: get device default colorspace and insert it first as
2409 * it should be the preferred one */
2410 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2411 if (gst_v4l2_object_get_colorspace (v4l2object, &fmt, &cinfo))
2412 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2415 /* step 2: probe all colorspace other than default
2416 * We don't probe all colorspace, range, matrix and transfer combination to
2417 * avoid ioctl flooding which could greatly increase initialization time
2418 * with low-speed devices (UVC...) */
2419 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2420 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++) {
2421 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2422 if (req_cspace == V4L2_COLORSPACE_BT878)
2425 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2426 fmt.fmt.pix_mp.colorspace = req_cspace;
2428 fmt.fmt.pix.colorspace = req_cspace;
2430 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2431 enum v4l2_colorspace colorspace;
2433 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2434 colorspace = fmt.fmt.pix_mp.colorspace;
2436 colorspace = fmt.fmt.pix.colorspace;
2438 if (colorspace == req_cspace) {
2439 if (gst_v4l2_object_get_colorspace (v4l2object, &fmt, &cinfo))
2440 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2445 if (gst_value_list_get_size (&list) > 0)
2446 gst_structure_take_value (s, "colorimetry", &list);
2448 g_value_unset (&list);
2453 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2454 static GstStructure *
2455 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2456 guint32 pixelformat,
2457 guint32 width, guint32 height, const GstStructure * template)
2459 gint fd = v4l2object->video_fd;
2460 struct v4l2_frmivalenum ival;
2463 GValue rates = { 0, };
2465 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2467 ival.pixel_format = pixelformat;
2469 ival.height = height;
2471 GST_LOG_OBJECT (v4l2object->dbg_obj,
2472 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2473 GST_FOURCC_ARGS (pixelformat));
2475 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2476 * fraction to get framerate */
2477 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2478 goto enum_frameintervals_failed;
2480 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2481 GValue rate = { 0, };
2483 g_value_init (&rates, GST_TYPE_LIST);
2484 g_value_init (&rate, GST_TYPE_FRACTION);
2487 num = ival.discrete.numerator;
2488 denom = ival.discrete.denominator;
2490 if (num > G_MAXINT || denom > G_MAXINT) {
2491 /* let us hope we don't get here... */
2496 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding discrete framerate: %d/%d",
2499 /* swap to get the framerate */
2500 gst_value_set_fraction (&rate, denom, num);
2501 gst_value_list_append_value (&rates, &rate);
2504 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2505 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2506 GValue min = { 0, };
2507 GValue step = { 0, };
2508 GValue max = { 0, };
2509 gboolean added = FALSE;
2510 guint32 minnum, mindenom;
2511 guint32 maxnum, maxdenom;
2513 g_value_init (&rates, GST_TYPE_LIST);
2515 g_value_init (&min, GST_TYPE_FRACTION);
2516 g_value_init (&step, GST_TYPE_FRACTION);
2517 g_value_init (&max, GST_TYPE_FRACTION);
2520 minnum = ival.stepwise.min.numerator;
2521 mindenom = ival.stepwise.min.denominator;
2522 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2526 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise min frame interval: %d/%d",
2528 gst_value_set_fraction (&min, minnum, mindenom);
2531 maxnum = ival.stepwise.max.numerator;
2532 maxdenom = ival.stepwise.max.denominator;
2533 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2538 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise max frame interval: %d/%d",
2540 gst_value_set_fraction (&max, maxnum, maxdenom);
2543 num = ival.stepwise.step.numerator;
2544 denom = ival.stepwise.step.denominator;
2545 if (num > G_MAXINT || denom > G_MAXINT) {
2550 if (num == 0 || denom == 0) {
2551 /* in this case we have a wrong fraction or no step, set the step to max
2552 * so that we only add the min value in the loop below */
2557 /* since we only have gst_value_fraction_subtract and not add, negate the
2559 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise step frame interval: %d/%d",
2561 gst_value_set_fraction (&step, -num, denom);
2563 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2564 GValue rate = { 0, };
2566 num = gst_value_get_fraction_numerator (&min);
2567 denom = gst_value_get_fraction_denominator (&min);
2568 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding stepwise framerate: %d/%d",
2571 /* invert to get the framerate */
2572 g_value_init (&rate, GST_TYPE_FRACTION);
2573 gst_value_set_fraction (&rate, denom, num);
2574 gst_value_list_append_value (&rates, &rate);
2577 /* we're actually adding because step was negated above. This is because
2578 * there is no _add function... */
2579 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2580 GST_WARNING_OBJECT (v4l2object->dbg_obj, "could not step fraction!");
2585 /* no range was added, leave the default range from the template */
2586 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2587 "no range added, leaving default");
2588 g_value_unset (&rates);
2590 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2591 guint32 maxnum, maxdenom;
2593 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2595 num = ival.stepwise.min.numerator;
2596 denom = ival.stepwise.min.denominator;
2597 if (num > G_MAXINT || denom > G_MAXINT) {
2602 maxnum = ival.stepwise.max.numerator;
2603 maxdenom = ival.stepwise.max.denominator;
2604 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2609 GST_LOG_OBJECT (v4l2object->dbg_obj,
2610 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2613 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2619 s = gst_structure_copy (template);
2620 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2621 "height", G_TYPE_INT, (gint) height, NULL);
2623 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2625 if (!v4l2object->skip_try_fmt_probes) {
2626 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2628 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2631 if (G_IS_VALUE (&rates)) {
2632 gst_v4l2src_value_simplify (&rates);
2633 /* only change the framerate on the template when we have a valid probed new
2635 gst_structure_take_value (s, "framerate", &rates);
2636 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2637 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2638 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2644 enum_frameintervals_failed:
2646 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2647 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2648 GST_FOURCC_ARGS (pixelformat), width, height);
2653 /* I don't see how this is actually an error, we ignore the format then */
2654 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2655 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2656 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2662 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2666 gst_structure_get_int (s1, "width", &w1);
2667 gst_structure_get_int (s1, "height", &h1);
2668 gst_structure_get_int (s2, "width", &w2);
2669 gst_structure_get_int (s2, "height", &h2);
2671 /* I think it's safe to assume that this won't overflow for a while */
2672 return ((w2 * h2) - (w1 * h1));
2676 check_alternate_and_append_struct (GstCaps * caps, GstStructure * s)
2680 mode = gst_structure_get_value (s, "interlace-mode");
2684 if (G_VALUE_HOLDS_STRING (mode)) {
2685 /* Add the INTERLACED feature if the mode is alternate */
2686 if (!g_strcmp0 (gst_structure_get_string (s, "interlace-mode"),
2688 GstCapsFeatures *feat;
2690 feat = gst_caps_features_new (GST_CAPS_FEATURE_FORMAT_INTERLACED, NULL);
2691 gst_caps_set_features (caps, gst_caps_get_size (caps) - 1, feat);
2693 } else if (GST_VALUE_HOLDS_LIST (mode)) {
2694 /* If the mode is a list containing alternate, remove it from the list and add a
2695 * variant with interlace-mode=alternate and the INTERLACED feature. */
2696 GValue alter = G_VALUE_INIT;
2697 GValue inter = G_VALUE_INIT;
2699 g_value_init (&alter, G_TYPE_STRING);
2700 g_value_set_string (&alter, "alternate");
2702 /* Cannot use gst_value_can_intersect() as it requires args to have the
2704 if (gst_value_intersect (&inter, mode, &alter)) {
2705 GValue minus_alter = G_VALUE_INIT;
2708 gst_value_subtract (&minus_alter, mode, &alter);
2709 gst_structure_take_value (s, "interlace-mode", &minus_alter);
2711 copy = gst_structure_copy (s);
2712 gst_structure_take_value (copy, "interlace-mode", &inter);
2713 gst_caps_append_structure_full (caps, copy,
2714 gst_caps_features_new (GST_CAPS_FEATURE_FORMAT_INTERLACED, NULL));
2716 g_value_unset (&alter);
2720 gst_caps_append_structure (caps, s);
2724 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2725 guint32 format, GstCaps * caps, GstStructure * s)
2727 GstStructure *alt_s = NULL;
2729 /* Encoded stream on output buffer need to be parsed */
2730 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2731 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2734 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2735 if (format == gst_v4l2_formats[i].format &&
2736 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2737 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2738 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2744 if (v4l2object->has_alpha_component &&
2745 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2746 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2748 case V4L2_PIX_FMT_RGB32:
2749 alt_s = gst_structure_copy (s);
2750 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2752 case V4L2_PIX_FMT_BGR32:
2753 alt_s = gst_structure_copy (s);
2754 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2761 check_alternate_and_append_struct (caps, s);
2764 check_alternate_and_append_struct (caps, alt_s);
2769 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2770 guint32 pixelformat, const GstStructure * template)
2772 GstCaps *ret = gst_caps_new_empty ();
2774 gint fd = v4l2object->video_fd;
2775 struct v4l2_frmsizeenum size;
2776 GList *results = NULL;
2779 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2780 gst_caps_append_structure (ret, gst_structure_copy (template));
2784 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2786 size.pixel_format = pixelformat;
2788 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2789 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2790 GST_FOURCC_ARGS (pixelformat));
2792 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2793 goto enum_framesizes_failed;
2795 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2797 GST_LOG_OBJECT (v4l2object->dbg_obj, "got discrete frame size %dx%d",
2798 size.discrete.width, size.discrete.height);
2800 w = MIN (size.discrete.width, G_MAXINT);
2801 h = MIN (size.discrete.height, G_MAXINT);
2805 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2806 pixelformat, w, h, template);
2809 results = g_list_prepend (results, tmp);
2813 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2814 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2815 "done iterating discrete frame sizes");
2816 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2817 guint32 maxw, maxh, step_w, step_h;
2819 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have stepwise frame sizes:");
2820 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2821 size.stepwise.min_width);
2822 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2823 size.stepwise.min_height);
2824 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2825 size.stepwise.max_width);
2826 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2827 size.stepwise.max_height);
2828 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step width: %d",
2829 size.stepwise.step_width);
2830 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step height: %d",
2831 size.stepwise.step_height);
2833 w = MAX (size.stepwise.min_width, 1);
2834 h = MAX (size.stepwise.min_height, 1);
2835 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2836 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2838 step_w = MAX (size.stepwise.step_width, 1);
2839 step_h = MAX (size.stepwise.step_height, 1);
2841 /* FIXME: check for sanity and that min/max are multiples of the steps */
2843 /* we only query details for the max width/height since it's likely the
2844 * most restricted if there are any resolution-dependent restrictions */
2845 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2846 pixelformat, maxw, maxh, template);
2849 GValue step_range = G_VALUE_INIT;
2851 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2852 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2853 gst_structure_set_value (tmp, "width", &step_range);
2855 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2856 gst_structure_take_value (tmp, "height", &step_range);
2858 /* no point using the results list here, since there's only one struct */
2859 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2861 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2864 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have continuous frame sizes:");
2865 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2866 size.stepwise.min_width);
2867 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2868 size.stepwise.min_height);
2869 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2870 size.stepwise.max_width);
2871 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2872 size.stepwise.max_height);
2874 w = MAX (size.stepwise.min_width, 1);
2875 h = MAX (size.stepwise.min_height, 1);
2876 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2877 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2880 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2883 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2884 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2887 /* no point using the results list here, since there's only one struct */
2888 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2894 /* we use an intermediary list to store and then sort the results of the
2895 * probing because we can't make any assumptions about the order in which
2896 * the driver will give us the sizes, but we want the final caps to contain
2897 * the results starting with the highest resolution and having the lowest
2898 * resolution last, since order in caps matters for things like fixation. */
2899 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2900 while (results != NULL) {
2901 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
2903 results = g_list_delete_link (results, results);
2906 if (gst_caps_is_empty (ret))
2907 goto enum_framesizes_no_results;
2912 enum_framesizes_failed:
2914 /* I don't see how this is actually an error */
2915 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2916 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2917 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2918 goto default_frame_sizes;
2920 enum_framesizes_no_results:
2922 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2923 * question doesn't actually support it yet */
2924 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2925 "No results for pixelformat %" GST_FOURCC_FORMAT
2926 " enumerating frame sizes, trying fallback",
2927 GST_FOURCC_ARGS (pixelformat));
2928 goto default_frame_sizes;
2932 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2933 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2934 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2935 goto default_frame_sizes;
2938 default_frame_sizes:
2940 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2942 /* This code is for Linux < 2.6.19 */
2944 max_w = max_h = GST_V4L2_MAX_SIZE;
2945 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2947 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2948 "Could not probe minimum capture size for pixelformat %"
2949 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2951 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2953 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2954 "Could not probe maximum capture size for pixelformat %"
2955 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2957 if (min_w == 0 || min_h == 0)
2959 if (max_w == 0 || max_h == 0)
2960 max_w = max_h = GST_V4L2_MAX_SIZE;
2962 /* Since we can't get framerate directly, try to use the current norm */
2963 if (v4l2object->tv_norm && v4l2object->norms) {
2965 GstTunerNorm *norm = NULL;
2966 GstTunerNorm *current =
2967 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2969 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2970 norm = (GstTunerNorm *) norms->data;
2971 if (!strcmp (norm->label, current->label))
2974 /* If it's possible, set framerate to that (discrete) value */
2976 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2977 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2981 tmp = gst_structure_copy (template);
2983 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2985 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2986 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2987 /* if norm can't be used, copy the template framerate */
2988 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2993 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2995 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2998 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
3000 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
3002 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
3004 /* We could consider setting interlace mode from min and max. */
3005 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
3008 if (!v4l2object->skip_try_fmt_probes) {
3009 gint probed_w, probed_h;
3010 if (v4l2object->info.width >= min_w && v4l2object->info.width <= max_w &&
3011 v4l2object->info.height >= min_h
3012 && v4l2object->info.height <= max_h) {
3013 probed_w = v4l2object->info.width;
3014 probed_h = v4l2object->info.height;
3019 /* We could consider to check colorspace for min too, in case it depends on
3020 * the size. But in this case, min and max could not be enough */
3021 gst_v4l2_object_add_colorspace (v4l2object, tmp, probed_w, probed_h,
3025 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
3031 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
3032 guint32 pixelformat, gint * width, gint * height)
3034 struct v4l2_format fmt;
3035 gboolean ret = FALSE;
3036 GstVideoInterlaceMode interlace_mode;
3038 g_return_val_if_fail (width != NULL, FALSE);
3039 g_return_val_if_fail (height != NULL, FALSE);
3041 GST_LOG_OBJECT (v4l2object->dbg_obj,
3042 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
3043 *width, *height, GST_FOURCC_ARGS (pixelformat));
3045 memset (&fmt, 0, sizeof (struct v4l2_format));
3047 /* get size delimiters */
3048 memset (&fmt, 0, sizeof (fmt));
3049 fmt.type = v4l2object->type;
3050 fmt.fmt.pix.width = *width;
3051 fmt.fmt.pix.height = *height;
3052 fmt.fmt.pix.pixelformat = pixelformat;
3053 fmt.fmt.pix.field = V4L2_FIELD_ANY;
3055 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
3058 GST_LOG_OBJECT (v4l2object->dbg_obj,
3059 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
3061 *width = fmt.fmt.pix.width;
3062 *height = fmt.fmt.pix.height;
3064 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
3065 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3066 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
3067 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
3075 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3076 "Unable to try format: %s", g_strerror (errno));
3083 gst_v4l2_object_is_dmabuf_supported (GstV4l2Object * v4l2object)
3085 gboolean ret = TRUE;
3086 struct v4l2_exportbuffer expbuf = {
3087 .type = v4l2object->type,
3090 .flags = O_CLOEXEC | O_RDWR,
3093 if (v4l2object->fmtdesc &&
3094 v4l2object->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
3095 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3096 "libv4l2 converter detected, disabling DMABuf");
3100 /* Expected to fail, but ENOTTY tells us that it is not implemented. */
3101 v4l2object->ioctl (v4l2object->video_fd, VIDIOC_EXPBUF, &expbuf);
3102 if (errno == ENOTTY)
3109 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
3113 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "initializing the %s system",
3114 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
3116 GST_V4L2_CHECK_OPEN (v4l2object);
3117 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3119 /* find transport */
3120 mode = v4l2object->req_mode;
3122 if (v4l2object->device_caps & V4L2_CAP_READWRITE) {
3123 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
3124 mode = GST_V4L2_IO_RW;
3125 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
3126 goto method_not_supported;
3128 if (v4l2object->device_caps & V4L2_CAP_STREAMING) {
3129 if (v4l2object->req_mode == GST_V4L2_IO_AUTO) {
3130 if (!V4L2_TYPE_IS_OUTPUT (v4l2object->type) &&
3131 gst_v4l2_object_is_dmabuf_supported (v4l2object)) {
3132 mode = GST_V4L2_IO_DMABUF;
3134 mode = GST_V4L2_IO_MMAP;
3137 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP ||
3138 v4l2object->req_mode == GST_V4L2_IO_DMABUF)
3139 goto method_not_supported;
3141 /* if still no transport selected, error out */
3142 if (mode == GST_V4L2_IO_AUTO)
3143 goto no_supported_capture_method;
3145 GST_INFO_OBJECT (v4l2object->dbg_obj, "accessing buffers via mode %d", mode);
3146 v4l2object->mode = mode;
3148 /* If min_buffers is not set, the driver either does not support the control or
3149 it has not been asked yet via propose_allocation/decide_allocation. */
3150 if (!v4l2object->min_buffers)
3151 gst_v4l2_get_driver_min_buffers (v4l2object);
3153 /* Map the buffers */
3154 GST_LOG_OBJECT (v4l2object->dbg_obj, "initiating buffer pool");
3156 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
3157 goto buffer_pool_new_failed;
3159 GST_V4L2_SET_ACTIVE (v4l2object);
3164 buffer_pool_new_failed:
3166 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
3167 (_("Could not map buffers from device '%s'"),
3168 v4l2object->videodev),
3169 ("Failed to create buffer pool: %s", g_strerror (errno)));
3172 method_not_supported:
3174 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
3175 (_("The driver of device '%s' does not support the IO method %d"),
3176 v4l2object->videodev, mode), (NULL));
3179 no_supported_capture_method:
3181 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
3182 (_("The driver of device '%s' does not support any known IO "
3183 "method."), v4l2object->videodev), (NULL));
3189 gst_v4l2_object_reset_compose_region (GstV4l2Object * obj)
3191 struct v4l2_selection sel = { 0 };
3193 GST_V4L2_CHECK_OPEN (obj);
3194 GST_V4L2_CHECK_NOT_ACTIVE (obj);
3196 sel.type = obj->type;
3197 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
3199 if (obj->ioctl (obj->video_fd, VIDIOC_G_SELECTION, &sel) < 0) {
3200 if (errno == ENOTTY) {
3201 /* No-op when selection API is not supported */
3204 GST_WARNING_OBJECT (obj->dbg_obj,
3205 "Failed to get default compose rectangle with VIDIOC_G_SELECTION: %s",
3206 g_strerror (errno));
3211 sel.target = V4L2_SEL_TGT_COMPOSE;
3213 if (obj->ioctl (obj->video_fd, VIDIOC_S_SELECTION, &sel) < 0) {
3214 GST_WARNING_OBJECT (obj->dbg_obj,
3215 "Failed to set default compose rectangle with VIDIOC_S_SELECTION: %s",
3216 g_strerror (errno));
3224 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
3225 gint plane, gint stride)
3227 const GstVideoFormatInfo *finfo = info->finfo;
3229 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3230 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
3232 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
3233 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
3235 /* this only works for what we support, NV12 subsampled tiles */
3236 if (GST_VIDEO_FORMAT_INFO_HAS_SUBTILES (finfo) && plane == 1)
3239 tile_height = 1 << hs;
3241 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
3242 info->height + align->padding_top + align->padding_bottom);
3243 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3245 x_tiles = stride >> ws;
3246 y_tiles = padded_height >> hs;
3247 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
3249 info->stride[plane] = stride;
3254 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
3255 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
3257 const GstVideoFormatInfo *finfo = info->finfo;
3258 gint i, estride, padded_height;
3261 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
3264 GST_VIDEO_INFO_FIELD_HEIGHT (info) + align->padding_top +
3265 align->padding_bottom;
3267 for (i = 0; i < finfo->n_planes; i++) {
3268 estride = gst_video_format_info_extrapolate_stride (finfo, i, stride);
3270 gst_v4l2_object_set_stride (info, align, i, estride);
3272 info->offset[i] = offs;
3274 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
3276 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3277 "Extrapolated for plane %d with base stride %d: "
3278 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
3282 /* Update the image size according the amount of data we are going to
3283 * read/write. This workaround bugs in driver where the sizeimage provided
3284 * by TRY/S_FMT represent the buffer length (maximum size) rather then the expected
3285 * bytesused (buffer size). */
3286 if (offs < info->size)
3291 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
3292 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
3293 GstVideoInfo * info, GstVideoAlignment * align)
3295 const GstVideoFormatInfo *finfo = info->finfo;
3296 gboolean standard_stride = TRUE;
3297 gint stride, pstride, padded_width, padded_height, i;
3299 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
3300 v4l2object->n_v4l2_planes = 1;
3301 info->size = format->fmt.pix.sizeimage;
3305 /* adjust right padding */
3306 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
3307 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3309 stride = format->fmt.pix.bytesperline;
3311 pstride = GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
3313 padded_width = stride / pstride;
3315 /* pstride can be 0 for complex formats */
3316 GST_WARNING_OBJECT (v4l2object->element,
3317 "format %s has a pstride of 0, cannot compute padded with",
3318 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
3319 padded_width = stride;
3322 if (padded_width < format->fmt.pix.width)
3323 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3324 "Driver bug detected, stride (%d) is too small for the width (%d)",
3325 padded_width, format->fmt.pix.width);
3327 align->padding_right = padded_width - info->width - align->padding_left;
3329 /* adjust bottom padding */
3330 padded_height = format->fmt.pix.height;
3332 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3333 guint hs, tile_height;
3335 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
3336 tile_height = 1 << hs;
3338 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3341 align->padding_bottom =
3342 padded_height - GST_VIDEO_INFO_FIELD_HEIGHT (info) - align->padding_top;
3344 /* setup the strides and offset */
3345 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
3346 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
3348 /* figure out the frame layout */
3349 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
3351 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
3352 stride = pix_mp->plane_fmt[i].bytesperline;
3354 if (info->stride[i] != stride)
3355 standard_stride = FALSE;
3357 gst_v4l2_object_set_stride (info, align, i, stride);
3358 info->offset[i] = info->size;
3359 info->size += pix_mp->plane_fmt[i].sizeimage;
3362 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3363 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
3364 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3365 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3368 /* only one plane in non-MPLANE mode */
3369 v4l2object->n_v4l2_planes = 1;
3370 info->size = format->fmt.pix.sizeimage;
3371 stride = format->fmt.pix.bytesperline;
3373 if (info->stride[0] != stride)
3374 standard_stride = FALSE;
3376 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3379 /* adjust the offset to take into account left and top */
3380 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3381 if ((align->padding_left + align->padding_top) > 0)
3382 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3383 "Left and top padding is not permitted for tiled formats");
3385 for (i = 0; i < finfo->n_planes; i++) {
3388 /* FIXME we assume plane as component as this is true for all supported
3389 * format we support. */
3391 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left);
3392 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top);
3394 info->offset[i] += (vedge * info->stride[i]) +
3395 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i));
3400 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got sizeimage %" G_GSIZE_FORMAT,
3403 /* to avoid copies we need video meta if there is padding */
3404 v4l2object->need_video_meta =
3405 ((align->padding_top + align->padding_left + align->padding_right +
3406 align->padding_bottom) != 0);
3408 /* ... or if stride is non "standard" */
3409 if (!standard_stride)
3410 v4l2object->need_video_meta = TRUE;
3412 /* ... or also video meta if we use multiple, non-contiguous, planes */
3413 if (v4l2object->n_v4l2_planes > 1)
3414 v4l2object->need_video_meta = TRUE;
3416 v4l2object->info = *info;
3417 v4l2object->align = *align;
3418 v4l2object->format = *format;
3419 v4l2object->fmtdesc = fmtdesc;
3421 /* if we have a framerate pre-calculate duration */
3422 if (info->fps_n > 0 && info->fps_d > 0) {
3423 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
3425 if (GST_VIDEO_INFO_INTERLACE_MODE (info) ==
3426 GST_VIDEO_INTERLACE_MODE_ALTERNATE)
3427 v4l2object->duration /= 2;
3429 v4l2object->duration = GST_CLOCK_TIME_NONE;
3433 static enum v4l2_field
3434 get_v4l2_field_for_info (GstVideoInfo * info)
3436 if (!GST_VIDEO_INFO_IS_INTERLACED (info))
3437 return V4L2_FIELD_NONE;
3439 if (GST_VIDEO_INFO_INTERLACE_MODE (info) ==
3440 GST_VIDEO_INTERLACE_MODE_ALTERNATE)
3441 return V4L2_FIELD_ALTERNATE;
3443 switch (GST_VIDEO_INFO_FIELD_ORDER (info)) {
3444 case GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST:
3445 return V4L2_FIELD_INTERLACED_TB;
3446 case GST_VIDEO_FIELD_ORDER_BOTTOM_FIELD_FIRST:
3447 return V4L2_FIELD_INTERLACED_BT;
3448 case GST_VIDEO_FIELD_ORDER_UNKNOWN:
3450 return V4L2_FIELD_INTERLACED;
3455 gst_v4l2_video_colorimetry_matches (const GstVideoColorimetry * cinfo,
3459 static const GstVideoColorimetry ci_likely_jpeg = {
3460 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3461 GST_VIDEO_TRANSFER_UNKNOWN, GST_VIDEO_COLOR_PRIMARIES_UNKNOWN
3463 static const GstVideoColorimetry ci_jpeg = {
3464 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3465 GST_VIDEO_TRANSFER_SRGB, GST_VIDEO_COLOR_PRIMARIES_BT709
3468 if (!gst_video_info_from_caps (&info, caps))
3471 /* if colorimetry in caps is unknown, use the default one */
3472 if (info.colorimetry.primaries == GST_VIDEO_COLOR_PRIMARIES_UNKNOWN)
3473 info.colorimetry.primaries = cinfo->primaries;
3474 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_UNKNOWN)
3475 info.colorimetry.range = cinfo->range;
3476 if (info.colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN)
3477 info.colorimetry.matrix = cinfo->matrix;
3478 if (info.colorimetry.transfer == GST_VIDEO_TRANSFER_UNKNOWN)
3479 info.colorimetry.transfer = cinfo->transfer;
3481 if (gst_video_colorimetry_is_equal (&info.colorimetry, cinfo))
3484 /* Allow 1:4:0:0 (produced by jpegdec) if the device expects 1:4:7:1 */
3485 if (gst_video_colorimetry_is_equal (&info.colorimetry, &ci_likely_jpeg)
3486 && gst_video_colorimetry_is_equal (cinfo, &ci_jpeg))
3492 static const gchar *
3493 field_to_str (enum v4l2_field f)
3496 case V4L2_FIELD_ANY:
3498 case V4L2_FIELD_NONE:
3500 case V4L2_FIELD_TOP:
3502 case V4L2_FIELD_BOTTOM:
3504 case V4L2_FIELD_INTERLACED:
3505 return "interlaced";
3506 case V4L2_FIELD_SEQ_TB:
3508 case V4L2_FIELD_SEQ_BT:
3510 case V4L2_FIELD_ALTERNATE:
3512 case V4L2_FIELD_INTERLACED_TB:
3513 return "interlaced-tb";
3514 case V4L2_FIELD_INTERLACED_BT:
3515 return "interlaced-bt";
3522 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3523 gboolean try_only, GstV4l2Error * error)
3525 gint fd = v4l2object->video_fd;
3526 struct v4l2_format format;
3527 struct v4l2_streamparm streamparm;
3528 enum v4l2_field field;
3529 guint32 pixelformat;
3530 struct v4l2_fmtdesc *fmtdesc;
3532 GstVideoAlignment align;
3533 gint width, height, fps_n, fps_d;
3537 enum v4l2_colorspace colorspace = 0;
3538 enum v4l2_quantization range = 0;
3539 enum v4l2_ycbcr_encoding matrix = 0;
3540 enum v4l2_xfer_func transfer = 0;
3542 gboolean disable_interlacing = FALSE;
3543 gboolean disable_colorimetry = FALSE;
3545 g_return_val_if_fail (!v4l2object->skip_try_fmt_probes ||
3546 gst_caps_is_writable (caps), FALSE);
3548 GST_V4L2_CHECK_OPEN (v4l2object);
3550 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3552 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3554 gst_video_info_init (&info);
3555 gst_video_alignment_reset (&align);
3556 v4l2object->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
3558 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3561 pixelformat = fmtdesc->pixelformat;
3562 width = GST_VIDEO_INFO_WIDTH (&info);
3563 height = GST_VIDEO_INFO_FIELD_HEIGHT (&info);
3564 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3565 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3567 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3568 * or if contiguous is preferred */
3569 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3570 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3573 field = get_v4l2_field_for_info (&info);
3574 if (field != V4L2_FIELD_NONE)
3575 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
3577 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
3579 /* We first pick the main colorspace from the primaries */
3580 switch (info.colorimetry.primaries) {
3581 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3582 /* There is two colorspaces using these primaries, use the range to
3584 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3585 colorspace = V4L2_COLORSPACE_REC709;
3587 colorspace = V4L2_COLORSPACE_SRGB;
3589 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
3590 colorspace = V4L2_COLORSPACE_BT2020;
3592 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3593 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3595 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3596 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3598 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3599 colorspace = V4L2_COLORSPACE_SMPTE170M;
3601 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3602 colorspace = V4L2_COLORSPACE_SMPTE240M;
3605 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3606 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3607 /* We don't know, we will guess */
3611 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3612 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3616 switch (info.colorimetry.range) {
3617 case GST_VIDEO_COLOR_RANGE_0_255:
3618 range = V4L2_QUANTIZATION_FULL_RANGE;
3620 case GST_VIDEO_COLOR_RANGE_16_235:
3621 range = V4L2_QUANTIZATION_LIM_RANGE;
3623 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3624 /* We let the driver pick a default one */
3627 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3628 "Unknown colorimetry range %d", info.colorimetry.range);
3632 switch (info.colorimetry.matrix) {
3633 case GST_VIDEO_COLOR_MATRIX_RGB:
3634 /* Unspecified, leave to default */
3636 /* FCC is about the same as BT601 with less digit */
3637 case GST_VIDEO_COLOR_MATRIX_FCC:
3638 case GST_VIDEO_COLOR_MATRIX_BT601:
3639 matrix = V4L2_YCBCR_ENC_601;
3641 case GST_VIDEO_COLOR_MATRIX_BT709:
3642 matrix = V4L2_YCBCR_ENC_709;
3644 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3645 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3647 case GST_VIDEO_COLOR_MATRIX_BT2020:
3648 matrix = V4L2_YCBCR_ENC_BT2020;
3650 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3651 /* We let the driver pick a default one */
3654 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3655 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3659 switch (info.colorimetry.transfer) {
3660 case GST_VIDEO_TRANSFER_GAMMA18:
3661 case GST_VIDEO_TRANSFER_GAMMA20:
3662 case GST_VIDEO_TRANSFER_GAMMA22:
3663 case GST_VIDEO_TRANSFER_GAMMA28:
3664 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3665 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3667 case GST_VIDEO_TRANSFER_GAMMA10:
3668 transfer = V4L2_XFER_FUNC_NONE;
3670 case GST_VIDEO_TRANSFER_SMPTE2084:
3671 transfer = V4L2_XFER_FUNC_SMPTE2084;
3673 case GST_VIDEO_TRANSFER_BT601:
3674 case GST_VIDEO_TRANSFER_BT2020_12:
3675 case GST_VIDEO_TRANSFER_BT2020_10:
3676 case GST_VIDEO_TRANSFER_BT709:
3677 v4l2object->transfer = info.colorimetry.transfer;
3678 transfer = V4L2_XFER_FUNC_709;
3680 case GST_VIDEO_TRANSFER_SMPTE240M:
3681 transfer = V4L2_XFER_FUNC_SMPTE240M;
3683 case GST_VIDEO_TRANSFER_SRGB:
3684 transfer = V4L2_XFER_FUNC_SRGB;
3686 case GST_VIDEO_TRANSFER_LOG100:
3687 case GST_VIDEO_TRANSFER_LOG316:
3688 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3689 "LOG 100, 316 transfer functions not supported");
3690 /* FIXME No known sensible default, maybe AdobeRGB ? */
3692 case GST_VIDEO_TRANSFER_UNKNOWN:
3693 /* We let the driver pick a default one */
3696 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3697 "Unknown colorimetry transfer %d", info.colorimetry.transfer);
3701 if (colorspace == 0) {
3702 /* Try to guess colorspace according to pixelformat and size */
3703 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3704 if (range == V4L2_QUANTIZATION_FULL_RANGE
3705 && matrix == V4L2_YCBCR_ENC_601 && transfer == 0) {
3706 /* Full range BT.601 YCbCr encoding with unknown primaries and transfer
3707 * function most likely is JPEG */
3708 colorspace = V4L2_COLORSPACE_JPEG;
3709 transfer = V4L2_XFER_FUNC_SRGB;
3711 /* SD streams likely use SMPTE170M and HD streams REC709 */
3712 if (width <= 720 && GST_VIDEO_INFO_HEIGHT (&info) <= 576)
3713 colorspace = V4L2_COLORSPACE_SMPTE170M;
3715 colorspace = V4L2_COLORSPACE_REC709;
3717 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3718 colorspace = V4L2_COLORSPACE_SRGB;
3719 transfer = V4L2_XFER_FUNC_NONE;
3723 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format %dx%d, format "
3724 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3725 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3727 memset (&format, 0x00, sizeof (struct v4l2_format));
3728 format.type = v4l2object->type;
3731 format.type = v4l2object->type;
3732 format.fmt.pix_mp.pixelformat = pixelformat;
3733 format.fmt.pix_mp.width = width;
3734 format.fmt.pix_mp.height = height;
3735 format.fmt.pix_mp.field = field;
3736 format.fmt.pix_mp.num_planes = n_v4l_planes;
3738 /* try to ask our preferred stride but it's not a failure if not
3740 for (i = 0; i < n_v4l_planes; i++) {
3741 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3743 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3744 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3745 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3747 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3750 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3751 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3753 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3755 format.type = v4l2object->type;
3757 format.fmt.pix.width = width;
3758 format.fmt.pix.height = height;
3759 format.fmt.pix.pixelformat = pixelformat;
3760 format.fmt.pix.field = field;
3762 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3763 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3764 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3766 /* try to ask our preferred stride */
3767 format.fmt.pix.bytesperline = stride;
3769 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3770 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3773 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format is %dx%d, format "
3774 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3775 format.fmt.pix_mp.height,
3776 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3777 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3779 #ifndef GST_DISABLE_GST_DEBUG
3781 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3782 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3783 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3785 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3786 format.fmt.pix.bytesperline);
3791 format.fmt.pix_mp.colorspace = colorspace;
3792 format.fmt.pix_mp.quantization = range;
3793 format.fmt.pix_mp.ycbcr_enc = matrix;
3794 format.fmt.pix_mp.xfer_func = transfer;
3796 format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
3797 format.fmt.pix.colorspace = colorspace;
3798 format.fmt.pix.quantization = range;
3799 format.fmt.pix.ycbcr_enc = matrix;
3800 format.fmt.pix.xfer_func = transfer;
3803 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired colorspace is %d:%d:%d:%d",
3804 colorspace, range, matrix, transfer);
3807 if (v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3808 goto try_fmt_failed;
3810 if (v4l2object->ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3811 goto set_fmt_failed;
3815 colorspace = format.fmt.pix_mp.colorspace;
3816 range = format.fmt.pix_mp.quantization;
3817 matrix = format.fmt.pix_mp.ycbcr_enc;
3818 transfer = format.fmt.pix_mp.xfer_func;
3820 colorspace = format.fmt.pix.colorspace;
3821 range = format.fmt.pix.quantization;
3822 matrix = format.fmt.pix.ycbcr_enc;
3823 transfer = format.fmt.pix.xfer_func;
3826 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got format of %dx%d, format "
3827 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d:%d:%d:%d field: %s",
3828 format.fmt.pix.width, format.fmt.pix_mp.height,
3829 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3830 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3831 colorspace, range, matrix, transfer, field_to_str (format.fmt.pix.field));
3833 #ifndef GST_DISABLE_GST_DEBUG
3835 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3836 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3837 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3838 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3840 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3841 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3845 if (format.fmt.pix.pixelformat != pixelformat)
3846 goto invalid_pixelformat;
3848 /* Only negotiate size with raw data.
3849 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3850 * in ASF mode for example, there is also not reason for a driver to
3851 * change the size. */
3852 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3853 /* We can crop larger images */
3854 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3855 goto invalid_dimensions;
3857 /* Note, this will be adjusted if upstream has non-centered cropping. */
3858 align.padding_top = 0;
3859 align.padding_bottom = format.fmt.pix.height - height;
3860 align.padding_left = 0;
3861 align.padding_right = format.fmt.pix.width - width;
3864 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3865 goto invalid_planes;
3867 /* used to check colorimetry and interlace mode fields presence */
3868 s = gst_caps_get_structure (caps, 0);
3870 if (gst_v4l2_object_get_interlace_mode (format.fmt.pix.field,
3871 &info.interlace_mode)) {
3872 if (gst_structure_has_field (s, "interlace-mode")) {
3873 if (format.fmt.pix.field != field)
3877 /* The driver (or libv4l2) is miss-behaving, just ignore interlace-mode from
3879 disable_interlacing = TRUE;
3880 if (gst_structure_has_field (s, "interlace-mode"))
3881 gst_structure_remove_field (s, "interlace-mode");
3884 if (gst_v4l2_object_get_colorspace (v4l2object, &format, &info.colorimetry)) {
3885 if (gst_structure_has_field (s, "colorimetry")) {
3886 if (!gst_v4l2_video_colorimetry_matches (&info.colorimetry, caps))
3887 goto invalid_colorimetry;
3890 /* The driver (or libv4l2) is miss-behaving, just ignore colorimetry from
3892 disable_colorimetry = TRUE;
3893 if (gst_structure_has_field (s, "colorimetry"))
3894 gst_structure_remove_field (s, "colorimetry");
3897 /* In case we have skipped the try_fmt probes, we'll need to set the
3898 * interlace-mode and colorimetry back into the caps. */
3899 if (v4l2object->skip_try_fmt_probes) {
3900 if (!disable_interlacing && !gst_structure_has_field (s, "interlace-mode")) {
3901 gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
3902 gst_video_interlace_mode_to_string (info.interlace_mode), NULL);
3904 if (!disable_colorimetry && !gst_structure_has_field (s, "colorimetry")) {
3905 gchar *str = gst_video_colorimetry_to_string (&info.colorimetry);
3906 gst_structure_set (s, "colorimetry", G_TYPE_STRING, str, NULL);
3911 if (try_only) /* good enough for trying only */
3914 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
3915 struct v4l2_control ctl = { 0, };
3916 ctl.id = V4L2_CID_ALPHA_COMPONENT;
3919 if (v4l2object->ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
3920 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3921 "Failed to set alpha component value");
3924 /* Is there a reason we require the caller to always specify a framerate? */
3925 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired framerate: %u/%u", fps_n,
3928 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
3929 streamparm.type = v4l2object->type;
3931 if (v4l2object->ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
3932 goto get_parm_failed;
3934 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
3935 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3936 GST_VIDEO_INFO_FPS_N (&info) =
3937 streamparm.parm.capture.timeperframe.denominator;
3938 GST_VIDEO_INFO_FPS_D (&info) =
3939 streamparm.parm.capture.timeperframe.numerator;
3941 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got capture framerate: %u/%u",
3942 streamparm.parm.capture.timeperframe.denominator,
3943 streamparm.parm.capture.timeperframe.numerator);
3945 /* We used to skip frame rate setup if the camera was already setup
3946 * with the requested frame rate. This breaks some cameras though,
3947 * causing them to not output data (several models of Thinkpad cameras
3948 * have this problem at least).
3949 * So, don't skip. */
3950 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting capture framerate to %u/%u",
3952 /* We want to change the frame rate, so check whether we can. Some cheap USB
3953 * cameras don't have the capability */
3954 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3955 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3956 "Not setting capture framerate (not supported)");
3960 /* Note: V4L2 wants the frame interval, we have the frame rate */
3961 streamparm.parm.capture.timeperframe.numerator = fps_d;
3962 streamparm.parm.capture.timeperframe.denominator = fps_n;
3964 /* some cheap USB cam's won't accept any change */
3965 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3966 goto set_parm_failed;
3968 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
3969 streamparm.parm.capture.timeperframe.denominator > 0) {
3970 /* get new values */
3971 fps_d = streamparm.parm.capture.timeperframe.numerator;
3972 fps_n = streamparm.parm.capture.timeperframe.denominator;
3974 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set capture framerate to %u/%u",
3977 /* fix v4l2 capture driver to provide framerate values */
3978 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3979 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
3982 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3983 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3984 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
3985 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
3986 GST_VIDEO_INFO_FPS_N (&info) =
3987 streamparm.parm.output.timeperframe.denominator;
3988 GST_VIDEO_INFO_FPS_D (&info) =
3989 streamparm.parm.output.timeperframe.numerator;
3991 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got output framerate: %u/%u",
3992 streamparm.parm.output.timeperframe.denominator,
3993 streamparm.parm.output.timeperframe.numerator);
3995 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting output framerate to %u/%u",
3997 if ((streamparm.parm.output.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3998 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3999 "Not setting output framerate (not supported)");
4003 /* Note: V4L2 wants the frame interval, we have the frame rate */
4004 streamparm.parm.output.timeperframe.numerator = fps_d;
4005 streamparm.parm.output.timeperframe.denominator = fps_n;
4007 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
4008 goto set_parm_failed;
4010 if (streamparm.parm.output.timeperframe.numerator > 0 &&
4011 streamparm.parm.output.timeperframe.denominator > 0) {
4012 /* get new values */
4013 fps_d = streamparm.parm.output.timeperframe.numerator;
4014 fps_n = streamparm.parm.output.timeperframe.denominator;
4016 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set output framerate to %u/%u",
4019 /* fix v4l2 output driver to provide framerate values */
4020 GST_WARNING_OBJECT (v4l2object->dbg_obj,
4021 "Reuse caps framerate %u/%u - fix v4l2 output driver", fps_n, fps_d);
4024 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
4025 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
4029 /* add boolean return, so we can fail on drivers bugs */
4030 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
4032 /* reset composition region to match the S_FMT size */
4033 gst_v4l2_object_reset_compose_region (v4l2object);
4035 /* now configure the pool */
4036 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
4044 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "can't parse caps %" GST_PTR_FORMAT,
4047 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4048 (_("Invalid caps")), ("Can't parse caps %" GST_PTR_FORMAT, caps));
4053 if (errno == EINVAL) {
4054 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4055 (_("Device '%s' has no supported format"), v4l2object->videodev),
4056 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4057 GST_FOURCC_ARGS (pixelformat), width, height,
4058 g_strerror (errno)));
4060 GST_V4L2_ERROR (error, RESOURCE, FAILED,
4061 (_("Device '%s' failed during initialization"),
4062 v4l2object->videodev),
4063 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4064 GST_FOURCC_ARGS (pixelformat), width, height,
4065 g_strerror (errno)));
4071 if (errno == EBUSY) {
4072 GST_V4L2_ERROR (error, RESOURCE, BUSY,
4073 (_("Device '%s' is busy"), v4l2object->videodev),
4074 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4075 GST_FOURCC_ARGS (pixelformat), width, height,
4076 g_strerror (errno)));
4077 } else if (errno == EINVAL) {
4078 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4079 (_("Device '%s' has no supported format"), v4l2object->videodev),
4080 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4081 GST_FOURCC_ARGS (pixelformat), width, height,
4082 g_strerror (errno)));
4084 GST_V4L2_ERROR (error, RESOURCE, FAILED,
4085 (_("Device '%s' failed during initialization"),
4086 v4l2object->videodev),
4087 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4088 GST_FOURCC_ARGS (pixelformat), width, height,
4089 g_strerror (errno)));
4095 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4096 (_("Device '%s' cannot capture at %dx%d"),
4097 v4l2object->videodev, width, height),
4098 ("Tried to capture at %dx%d, but device returned size %dx%d",
4099 width, height, format.fmt.pix.width, format.fmt.pix.height));
4102 invalid_pixelformat:
4104 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4105 (_("Device '%s' cannot capture in the specified format"),
4106 v4l2object->videodev),
4107 ("Tried to capture in %" GST_FOURCC_FORMAT
4108 ", but device returned format" " %" GST_FOURCC_FORMAT,
4109 GST_FOURCC_ARGS (pixelformat),
4110 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
4115 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4116 (_("Device '%s' does support non-contiguous planes"),
4117 v4l2object->videodev),
4118 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
4123 enum v4l2_field wanted_field;
4126 wanted_field = format.fmt.pix_mp.field;
4128 wanted_field = format.fmt.pix.field;
4130 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4131 (_("Device '%s' does not support %s interlacing"),
4132 v4l2object->videodev,
4133 field == V4L2_FIELD_NONE ? "progressive" : "interleaved"),
4134 ("Device wants %s interlacing",
4135 wanted_field == V4L2_FIELD_NONE ? "progressive" : "interleaved"));
4138 invalid_colorimetry:
4140 gchar *wanted_colorimetry;
4142 wanted_colorimetry = gst_video_colorimetry_to_string (&info.colorimetry);
4144 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4145 (_("Device '%s' does not support %s colorimetry"),
4146 v4l2object->videodev, gst_structure_get_string (s, "colorimetry")),
4147 ("Device wants %s colorimetry", wanted_colorimetry));
4149 g_free (wanted_colorimetry);
4154 /* it's possible that this call is not supported */
4155 if (errno != EINVAL && errno != ENOTTY) {
4156 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4157 (_("Could not get parameters on device '%s'"),
4158 v4l2object->videodev), GST_ERROR_SYSTEM);
4164 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4165 (_("Video device did not accept new frame rate setting.")),
4171 /* setup_pool already send the error */
4177 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps,
4178 GstV4l2Error * error)
4180 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Setting format to %" GST_PTR_FORMAT,
4182 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE, error);
4186 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps,
4187 GstV4l2Error * error)
4189 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying format %" GST_PTR_FORMAT,
4191 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE, error);
4195 * gst_v4l2_object_acquire_format:
4196 * @v4l2object: the object
4197 * @info: a GstVideoInfo to be filled
4199 * Acquire the driver chosen format. This is useful in decoder or encoder elements where
4200 * the output format is chosen by the HW.
4202 * Returns: %TRUE on success, %FALSE on failure.
4205 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
4207 struct v4l2_fmtdesc *fmtdesc;
4208 struct v4l2_format fmt;
4209 struct v4l2_crop crop;
4210 struct v4l2_selection sel;
4211 struct v4l2_rect *r = NULL;
4212 GstVideoFormat format;
4213 guint width, height;
4214 GstVideoAlignment align;
4215 GstVideoInterlaceMode interlace_mode;
4217 gst_video_info_init (info);
4218 gst_video_alignment_reset (&align);
4220 memset (&fmt, 0x00, sizeof (struct v4l2_format));
4221 fmt.type = v4l2object->type;
4222 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
4223 goto get_fmt_failed;
4225 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
4226 fmt.fmt.pix.pixelformat);
4227 if (fmtdesc == NULL)
4228 goto unsupported_format;
4230 /* No need to care about mplane, the four first params are the same */
4231 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
4233 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
4234 if (format == GST_VIDEO_FORMAT_UNKNOWN)
4235 goto unsupported_format;
4237 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
4238 goto invalid_dimensions;
4240 width = fmt.fmt.pix.width;
4241 height = fmt.fmt.pix.height;
4243 /* Use the default compose rectangle */
4244 memset (&sel, 0, sizeof (struct v4l2_selection));
4245 sel.type = v4l2object->type;
4246 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
4247 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0) {
4250 /* For ancient kernels, fall back to G_CROP */
4251 memset (&crop, 0, sizeof (struct v4l2_crop));
4252 crop.type = v4l2object->type;
4253 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
4257 align.padding_left = r->left;
4258 align.padding_top = r->top;
4259 align.padding_right = width - r->width - r->left;
4260 align.padding_bottom = height - r->height - r->top;
4265 switch (fmt.fmt.pix.field) {
4266 case V4L2_FIELD_ANY:
4267 case V4L2_FIELD_NONE:
4268 interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
4270 case V4L2_FIELD_INTERLACED:
4271 case V4L2_FIELD_INTERLACED_TB:
4272 case V4L2_FIELD_INTERLACED_BT:
4273 interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
4275 case V4L2_FIELD_ALTERNATE:
4276 interlace_mode = GST_VIDEO_INTERLACE_MODE_ALTERNATE;
4279 goto unsupported_field;
4282 gst_video_info_set_interlaced_format (info, format, interlace_mode, width,
4285 gst_v4l2_object_get_colorspace (v4l2object, &fmt, &info->colorimetry);
4286 gst_v4l2_object_get_streamparm (v4l2object, info);
4287 if ((info->fps_n == 0 && v4l2object->info.fps_d != 0)
4288 && (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
4289 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
4290 info->fps_d = v4l2object->info.fps_d;
4291 info->fps_n = v4l2object->info.fps_n;
4292 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Set capture fps to %d/%d",
4293 info->fps_n, info->fps_d);
4296 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
4298 /* Shall we setup the pool ? */
4304 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
4305 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
4310 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
4311 (_("Video device returned invalid dimensions.")),
4312 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
4313 fmt.fmt.pix.height));
4318 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
4319 (_("Video device uses an unsupported interlacing method.")),
4320 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
4325 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
4326 (_("Video device uses an unsupported pixel format.")),
4327 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
4328 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
4334 * gst_v4l2_object_set_crop:
4336 * @crop_rect: the region to crop
4338 * Crop the video data to the regions specified in the @crop_rect.
4340 * For capture devices, this crop the image sensor / video stream provided by
4342 * For output devices, this crops the memory buffer that GStreamer passed to
4345 * The crop_rect may be modified by the V4L2 device to a region that
4346 * fulfills H/W requirements.
4348 * Returns: %TRUE on success, %FALSE on failure.
4351 gst_v4l2_object_set_crop (GstV4l2Object * obj, struct v4l2_rect * crop_rect)
4353 struct v4l2_selection sel = { 0 };
4354 struct v4l2_crop crop = { 0 };
4356 GST_V4L2_CHECK_OPEN (obj);
4357 GST_V4L2_CHECK_NOT_ACTIVE (obj);
4359 sel.type = obj->type;
4360 sel.target = V4L2_SEL_TGT_CROP;
4364 crop.type = obj->type;
4367 GST_DEBUG_OBJECT (obj->dbg_obj,
4368 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4369 crop.c.width, crop.c.height);
4371 if (obj->ioctl (obj->video_fd, VIDIOC_S_SELECTION, &sel) < 0) {
4372 if (errno != ENOTTY) {
4373 GST_WARNING_OBJECT (obj->dbg_obj,
4374 "Failed to set crop rectangle with VIDIOC_S_SELECTION: %s",
4375 g_strerror (errno));
4378 if (obj->ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
4379 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_S_CROP failed");
4383 if (obj->ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
4384 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_G_CROP failed");
4392 GST_DEBUG_OBJECT (obj->dbg_obj,
4393 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4394 crop.c.width, crop.c.height);
4400 * gst_v4l2_object_setup_padding:
4403 * Crop away the padding around the video data as specified
4404 * in GstVideoAlignement data stored in @obj.
4406 * For capture devices, this crop the image sensor / video stream provided by
4408 * For output devices, this crops the memory buffer that GStreamer passed to
4411 * Returns: %TRUE on success, %FALSE on failure.
4414 gst_v4l2_object_setup_padding (GstV4l2Object * obj)
4416 GstVideoAlignment *align = &obj->align;
4417 struct v4l2_rect crop;
4419 if (align->padding_left + align->padding_top
4420 + align->padding_right + align->padding_bottom == 0) {
4421 GST_DEBUG_OBJECT (obj->dbg_obj, "no cropping needed");
4425 crop.left = align->padding_left;
4426 crop.top = align->padding_top;
4427 crop.width = obj->info.width;
4428 crop.height = GST_VIDEO_INFO_FIELD_HEIGHT (&obj->info);
4430 return gst_v4l2_object_set_crop (obj, &crop);
4434 gst_v4l2_object_get_crop_rect (GstV4l2Object * obj, guint target,
4435 struct v4l2_rect *result)
4437 struct v4l2_rect *res_rect;
4439 struct v4l2_selection sel = { 0 };
4440 struct v4l2_cropcap cropcap = { 0 };
4442 GST_V4L2_CHECK_OPEN (obj);
4444 if (target != V4L2_SEL_TGT_CROP_BOUNDS && target != V4L2_SEL_TGT_CROP_DEFAULT)
4447 sel.type = obj->type;
4448 sel.target = target;
4452 if (obj->ioctl (obj->video_fd, VIDIOC_G_SELECTION, &sel) < 0) {
4453 if (errno != ENOTTY) {
4454 GST_WARNING_OBJECT (obj->dbg_obj,
4455 "Failed to get default crop rectangle with VIDIOC_G_SELECTION: %s",
4456 g_strerror (errno));
4459 if (obj->ioctl (obj->video_fd, VIDIOC_CROPCAP, &cropcap) < 0) {
4460 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_CROPCAP failed");
4463 if (target == V4L2_SEL_TGT_CROP_BOUNDS)
4464 res_rect = &cropcap.bounds;
4465 else if (target == V4L2_SEL_TGT_CROP_DEFAULT)
4466 res_rect = &cropcap.defrect;
4470 *result = *res_rect;
4475 gst_v4l2_object_get_crop_bounds (GstV4l2Object * obj, struct v4l2_rect * result)
4477 return gst_v4l2_object_get_crop_rect (obj, V4L2_SEL_TGT_CROP_BOUNDS, result);
4481 gst_v4l2_object_get_crop_default (GstV4l2Object * obj,
4482 struct v4l2_rect * result)
4484 return gst_v4l2_object_get_crop_rect (obj, V4L2_SEL_TGT_CROP_DEFAULT, result);
4488 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
4490 GstStructure *config;
4494 if (!v4l2object->pool)
4497 config = gst_buffer_pool_get_config (v4l2object->pool);
4498 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4500 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
4502 gst_structure_free (config);
4508 gst_v4l2_object_caps_is_subset (GstV4l2Object * v4l2object, GstCaps * caps)
4510 GstStructure *config;
4514 if (!v4l2object->pool)
4517 config = gst_buffer_pool_get_config (v4l2object->pool);
4518 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4520 ret = oldcaps && gst_caps_is_subset (oldcaps, caps);
4522 gst_structure_free (config);
4528 gst_v4l2_object_get_current_caps (GstV4l2Object * v4l2object)
4530 GstStructure *config;
4533 if (!v4l2object->pool)
4536 config = gst_buffer_pool_get_config (v4l2object->pool);
4537 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4540 gst_caps_ref (oldcaps);
4542 gst_structure_free (config);
4548 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
4550 gboolean ret = TRUE;
4552 GST_LOG_OBJECT (v4l2object->dbg_obj, "start flushing");
4554 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
4555 gst_buffer_pool_set_flushing (v4l2object->pool, TRUE);
4561 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
4563 gboolean ret = TRUE;
4565 GST_LOG_OBJECT (v4l2object->dbg_obj, "stop flushing");
4567 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
4568 gst_buffer_pool_set_flushing (v4l2object->pool, FALSE);
4574 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
4576 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "stopping");
4578 if (!GST_V4L2_IS_OPEN (v4l2object))
4580 if (!GST_V4L2_IS_ACTIVE (v4l2object))
4583 if (v4l2object->pool) {
4584 if (!gst_v4l2_buffer_pool_orphan (&v4l2object->pool)) {
4585 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "deactivating pool");
4586 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
4587 gst_object_unref (v4l2object->pool);
4589 v4l2object->pool = NULL;
4592 GST_V4L2_SET_INACTIVE (v4l2object);
4599 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4605 formats = gst_v4l2_object_get_format_list (v4l2object);
4607 ret = gst_caps_new_empty ();
4609 if (v4l2object->keep_aspect && !v4l2object->par) {
4610 struct v4l2_cropcap cropcap;
4612 memset (&cropcap, 0, sizeof (cropcap));
4614 cropcap.type = v4l2object->type;
4615 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0) {
4616 if (errno != ENOTTY)
4617 GST_WARNING_OBJECT (v4l2object->dbg_obj,
4618 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
4619 g_strerror (errno));
4620 } else if (cropcap.pixelaspect.numerator && cropcap.pixelaspect.denominator) {
4621 v4l2object->par = g_new0 (GValue, 1);
4622 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
4623 gst_value_set_fraction (v4l2object->par, cropcap.pixelaspect.numerator,
4624 cropcap.pixelaspect.denominator);
4628 for (walk = formats; walk; walk = walk->next) {
4629 struct v4l2_fmtdesc *format;
4630 GstStructure *template;
4633 format = (struct v4l2_fmtdesc *) walk->data;
4635 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
4638 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
4639 "unknown format %" GST_FOURCC_FORMAT,
4640 GST_FOURCC_ARGS (format->pixelformat));
4644 /* If we have a filter, check if we need to probe this format or not */
4646 GstCaps *format_caps = gst_caps_new_empty ();
4648 gst_caps_append_structure (format_caps, gst_structure_copy (template));
4650 if (!gst_caps_can_intersect (format_caps, filter)) {
4651 gst_caps_unref (format_caps);
4652 gst_structure_free (template);
4656 gst_caps_unref (format_caps);
4659 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
4660 format->pixelformat, template);
4662 gst_caps_append (ret, tmp);
4664 /* Add a variant of the caps with the Interlaced feature so we can negotiate it if needed */
4665 add_alternate_variant (v4l2object, ret, gst_caps_get_structure (ret,
4666 gst_caps_get_size (ret) - 1));
4669 gst_structure_free (template);
4676 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
4677 gst_caps_unref (tmp);
4680 GST_INFO_OBJECT (v4l2object->dbg_obj, "probed caps: %" GST_PTR_FORMAT, ret);
4686 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4690 if (v4l2object->probed_caps == NULL)
4691 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
4694 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
4695 GST_CAPS_INTERSECT_FIRST);
4697 ret = gst_caps_ref (v4l2object->probed_caps);
4704 gst_v4l2_object_match_buffer_layout (GstV4l2Object * obj, guint n_planes,
4705 gsize offset[GST_VIDEO_MAX_PLANES], gint stride[GST_VIDEO_MAX_PLANES],
4706 gsize buffer_size, guint padded_height)
4709 gboolean need_fmt_update = FALSE;
4711 if (n_planes != GST_VIDEO_INFO_N_PLANES (&obj->info)) {
4712 GST_WARNING_OBJECT (obj->dbg_obj,
4713 "Cannot match buffers with different number planes");
4717 for (p = 0; p < n_planes; p++) {
4718 if (stride[p] < obj->info.stride[p]) {
4719 GST_DEBUG_OBJECT (obj->dbg_obj,
4720 "Not matching as remote stride %i is smaller than %i on plane %u",
4721 stride[p], obj->info.stride[p], p);
4723 } else if (stride[p] > obj->info.stride[p]) {
4724 GST_LOG_OBJECT (obj->dbg_obj,
4725 "remote stride %i is higher than %i on plane %u",
4726 stride[p], obj->info.stride[p], p);
4727 need_fmt_update = TRUE;
4730 if (offset[p] < obj->info.offset[p]) {
4731 GST_DEBUG_OBJECT (obj->dbg_obj,
4732 "Not matching as offset %" G_GSIZE_FORMAT
4733 " is smaller than %" G_GSIZE_FORMAT " on plane %u",
4734 offset[p], obj->info.offset[p], p);
4736 } else if (offset[p] > obj->info.offset[p]) {
4737 GST_LOG_OBJECT (obj->dbg_obj,
4738 "Remote offset %" G_GSIZE_FORMAT
4739 " is higher than %" G_GSIZE_FORMAT " on plane %u",
4740 offset[p], obj->info.offset[p], p);
4741 need_fmt_update = TRUE;
4744 if (padded_height) {
4747 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
4748 fmt_height = obj->format.fmt.pix_mp.height;
4750 fmt_height = obj->format.fmt.pix.height;
4752 if (padded_height > fmt_height)
4753 need_fmt_update = TRUE;
4757 if (need_fmt_update) {
4758 struct v4l2_format format;
4759 gint wanted_stride[GST_VIDEO_MAX_PLANES] = { 0, };
4761 format = obj->format;
4763 if (padded_height) {
4764 GST_DEBUG_OBJECT (obj->dbg_obj, "Padded height %u", padded_height);
4766 obj->align.padding_bottom =
4767 padded_height - GST_VIDEO_INFO_FIELD_HEIGHT (&obj->info);
4769 GST_WARNING_OBJECT (obj->dbg_obj,
4770 "Failed to compute padded height; keep the default one");
4771 padded_height = format.fmt.pix_mp.height;
4774 /* update the current format with the stride we want to import from */
4775 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
4778 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted strides:");
4780 for (i = 0; i < obj->n_v4l2_planes; i++) {
4781 gint plane_stride = stride[i];
4783 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4784 plane_stride = GST_VIDEO_TILE_X_TILES (plane_stride) <<
4785 GST_VIDEO_FORMAT_INFO_TILE_WS (obj->info.finfo);
4787 format.fmt.pix_mp.plane_fmt[i].bytesperline = plane_stride;
4788 format.fmt.pix_mp.height = padded_height;
4789 wanted_stride[i] = plane_stride;
4790 GST_DEBUG_OBJECT (obj->dbg_obj, " [%u] %i", i, wanted_stride[i]);
4793 gint plane_stride = stride[0];
4795 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted stride: %i", plane_stride);
4797 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4798 plane_stride = GST_VIDEO_TILE_X_TILES (plane_stride) <<
4799 GST_VIDEO_FORMAT_INFO_TILE_WS (obj->info.finfo);
4801 format.fmt.pix.bytesperline = plane_stride;
4802 format.fmt.pix.height = padded_height;
4803 wanted_stride[0] = plane_stride;
4806 if (obj->ioctl (obj->video_fd, VIDIOC_S_FMT, &format) < 0) {
4807 GST_WARNING_OBJECT (obj->dbg_obj,
4808 "Something went wrong trying to update current format: %s",
4809 g_strerror (errno));
4813 gst_v4l2_object_save_format (obj, obj->fmtdesc, &format, &obj->info,
4816 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
4819 for (i = 0; i < obj->n_v4l2_planes; i++) {
4820 if (format.fmt.pix_mp.plane_fmt[i].bytesperline != wanted_stride[i]) {
4821 GST_DEBUG_OBJECT (obj->dbg_obj,
4822 "[%i] Driver did not accept the new stride (wants %i, got %i)",
4823 i, wanted_stride[i], format.fmt.pix_mp.plane_fmt[i].bytesperline);
4828 if (format.fmt.pix_mp.height != padded_height) {
4829 GST_DEBUG_OBJECT (obj->dbg_obj,
4830 "Driver did not accept the padded height (wants %i, got %i)",
4831 padded_height, format.fmt.pix_mp.height);
4834 if (format.fmt.pix.bytesperline != wanted_stride[0]) {
4835 GST_DEBUG_OBJECT (obj->dbg_obj,
4836 "Driver did not accept the new stride (wants %i, got %i)",
4837 wanted_stride[0], format.fmt.pix.bytesperline);
4841 if (format.fmt.pix.height != padded_height) {
4842 GST_DEBUG_OBJECT (obj->dbg_obj,
4843 "Driver did not accept the padded height (wants %i, got %i)",
4844 padded_height, format.fmt.pix.height);
4849 if (obj->align.padding_bottom) {
4850 /* Crop because of vertical padding */
4851 GST_DEBUG_OBJECT (obj->dbg_obj, "crop because of bottom padding of %d",
4852 obj->align.padding_bottom);
4853 gst_v4l2_object_setup_padding (obj);
4860 validate_video_meta_struct (GstV4l2Object * obj, const GstStructure * s)
4864 for (i = 0; i < gst_structure_n_fields (s); i++) {
4865 const gchar *name = gst_structure_nth_field_name (s, i);
4867 if (!g_str_equal (name, "padding-top")
4868 && !g_str_equal (name, "padding-bottom")
4869 && !g_str_equal (name, "padding-left")
4870 && !g_str_equal (name, "padding-right")) {
4871 GST_WARNING_OBJECT (obj->dbg_obj, "Unknown video meta field: '%s'", name);
4880 gst_v4l2_object_match_buffer_layout_from_struct (GstV4l2Object * obj,
4881 const GstStructure * s, GstCaps * caps, guint buffer_size)
4884 GstVideoAlignment align;
4885 gsize plane_size[GST_VIDEO_MAX_PLANES];
4887 if (!validate_video_meta_struct (obj, s))
4890 if (!gst_video_info_from_caps (&info, caps)) {
4891 GST_WARNING_OBJECT (obj->dbg_obj, "Failed to create video info");
4895 gst_video_alignment_reset (&align);
4897 gst_structure_get_uint (s, "padding-top", &align.padding_top);
4898 gst_structure_get_uint (s, "padding-bottom", &align.padding_bottom);
4899 gst_structure_get_uint (s, "padding-left", &align.padding_left);
4900 gst_structure_get_uint (s, "padding-right", &align.padding_right);
4902 if (align.padding_top || align.padding_bottom || align.padding_left ||
4903 align.padding_right) {
4904 GST_DEBUG_OBJECT (obj->dbg_obj,
4905 "Upstream requested padding (top: %d bottom: %d left: %d right: %d)",
4906 align.padding_top, align.padding_bottom, align.padding_left,
4907 align.padding_right);
4910 if (!gst_video_info_align_full (&info, &align, plane_size)) {
4911 GST_WARNING_OBJECT (obj->dbg_obj, "Failed to align video info");
4915 if (GST_VIDEO_INFO_SIZE (&info) != buffer_size) {
4916 GST_WARNING_OBJECT (obj->dbg_obj,
4917 "Requested buffer size (%d) doesn't match video info size (%"
4918 G_GSIZE_FORMAT ")", buffer_size, GST_VIDEO_INFO_SIZE (&info));
4922 GST_DEBUG_OBJECT (obj->dbg_obj,
4923 "try matching buffer layout requested by downstream");
4925 gst_v4l2_object_match_buffer_layout (obj, GST_VIDEO_INFO_N_PLANES (&info),
4926 info.offset, info.stride, buffer_size,
4927 GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size));
4933 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
4936 GstBufferPool *pool = NULL, *other_pool = NULL;
4937 GstStructure *config;
4938 guint size, min, max, own_min = 0;
4940 gboolean has_video_meta;
4941 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
4942 GstAllocator *allocator = NULL;
4943 GstAllocationParams params = { 0 };
4946 GST_DEBUG_OBJECT (obj->dbg_obj, "decide allocation");
4948 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4949 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
4951 gst_query_parse_allocation (query, &caps, NULL);
4953 if (obj->pool == NULL) {
4954 if (!gst_v4l2_object_setup_pool (obj, caps))
4958 if (gst_query_get_n_allocation_params (query) > 0)
4959 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
4961 if (gst_query_get_n_allocation_pools (query) > 0) {
4962 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
4971 GST_DEBUG_OBJECT (obj->dbg_obj, "allocation: size:%u min:%u max:%u pool:%"
4972 GST_PTR_FORMAT, size, min, max, pool);
4975 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE,
4978 if (has_video_meta) {
4979 const GstStructure *params;
4980 gst_query_parse_nth_allocation_meta (query, video_idx, ¶ms);
4983 gst_v4l2_object_match_buffer_layout_from_struct (obj, params, caps, size);
4986 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
4988 gst_v4l2_get_driver_min_buffers (obj);
4989 /* We can't share our own pool, if it exceed V4L2 capacity */
4990 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
4991 can_share_own_pool = FALSE;
4994 switch (obj->mode) {
4995 case GST_V4L2_IO_RW:
4997 /* in READ/WRITE mode, prefer a downstream pool because our own pool
4998 * doesn't help much, we have to write to it as well */
4999 GST_DEBUG_OBJECT (obj->dbg_obj,
5000 "read/write mode: using downstream pool");
5001 /* use the bigest size, when we use our own pool we can't really do any
5002 * other size than what the hardware gives us but for downstream pools
5004 size = MAX (size, obj->info.size);
5005 } else if (can_share_own_pool) {
5006 /* no downstream pool, use our own then */
5007 GST_DEBUG_OBJECT (obj->dbg_obj,
5008 "read/write mode: no downstream pool, using our own");
5009 pool = gst_object_ref (obj->pool);
5010 size = obj->info.size;
5011 pushing_from_our_pool = TRUE;
5015 case GST_V4L2_IO_USERPTR:
5016 case GST_V4L2_IO_DMABUF_IMPORT:
5017 /* in importing mode, prefer our own pool, and pass the other pool to
5018 * our own, so it can serve itself */
5020 goto no_downstream_pool;
5021 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj->pool),
5024 gst_object_unref (pool);
5025 pool = gst_object_ref (obj->pool);
5026 size = obj->info.size;
5029 case GST_V4L2_IO_MMAP:
5030 case GST_V4L2_IO_DMABUF:
5031 /* in streaming mode, prefer our own pool */
5032 /* Check if we can use it ... */
5033 if (can_share_own_pool) {
5035 gst_object_unref (pool);
5036 pool = gst_object_ref (obj->pool);
5037 size = obj->info.size;
5038 GST_DEBUG_OBJECT (obj->dbg_obj,
5039 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
5040 pushing_from_our_pool = TRUE;
5042 GST_DEBUG_OBJECT (obj->dbg_obj,
5043 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
5046 GST_DEBUG_OBJECT (obj->dbg_obj,
5047 "streaming mode: no usable pool, copying to generic pool");
5048 size = MAX (size, obj->info.size);
5051 case GST_V4L2_IO_AUTO:
5053 GST_WARNING_OBJECT (obj->dbg_obj, "unhandled mode");
5060 /* If pushing from our own pool, configure it with queried minimum,
5061 * otherwise use the minimum required */
5062 if (pushing_from_our_pool) {
5063 /* When pushing from our own pool, we need what downstream one, to be able
5064 * to fill the pipeline, the minimum required to decoder according to the
5065 * driver and 2 more, so we don't endup up with everything downstream or
5066 * held by the decoder. We account 2 buffers for v4l2 so when one is being
5067 * pushed downstream the other one can already be queued for the next
5069 own_min = min + obj->min_buffers + 2;
5071 /* If no allocation parameters where provided, allow for a little more
5072 * buffers and enable copy threshold */
5075 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
5078 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
5083 /* In this case we'll have to configure two buffer pool. For our buffer
5084 * pool, we'll need what the driver one, and one more, so we can dequeu */
5085 own_min = obj->min_buffers + 1;
5086 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS (obj));
5088 /* for the downstream pool, we keep what downstream wants, though ensure
5089 * at least a minimum if downstream didn't suggest anything (we are
5090 * expecting the base class to create a default one for the context) */
5091 min = MAX (min, GST_V4L2_MIN_BUFFERS (obj));
5093 /* To import we need the other pool to hold at least own_min */
5094 if (obj->pool == pool)
5098 /* Request a bigger max, if one was suggested but it's too small */
5100 max = MAX (min, max);
5102 /* First step, configure our own pool */
5103 config = gst_buffer_pool_get_config (obj->pool);
5105 if (obj->need_video_meta || has_video_meta) {
5106 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
5107 gst_buffer_pool_config_add_option (config,
5108 GST_BUFFER_POOL_OPTION_VIDEO_META);
5111 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
5112 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
5114 GST_DEBUG_OBJECT (obj->dbg_obj, "setting own pool config to %"
5115 GST_PTR_FORMAT, config);
5117 /* Our pool often need to adjust the value */
5118 if (!gst_buffer_pool_set_config (obj->pool, config)) {
5119 config = gst_buffer_pool_get_config (obj->pool);
5121 GST_DEBUG_OBJECT (obj->dbg_obj, "own pool config changed to %"
5122 GST_PTR_FORMAT, config);
5124 /* our pool will adjust the maximum buffer, which we are fine with */
5125 if (!gst_buffer_pool_set_config (obj->pool, config))
5129 /* Now configure the other pool if different */
5130 if (obj->pool != pool)
5134 config = gst_buffer_pool_get_config (other_pool);
5135 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
5136 gst_buffer_pool_config_set_params (config, caps, size, min, max);
5138 GST_DEBUG_OBJECT (obj->dbg_obj, "setting other pool config to %"
5139 GST_PTR_FORMAT, config);
5141 /* if downstream supports video metadata, add this to the pool config */
5142 if (has_video_meta) {
5143 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
5144 gst_buffer_pool_config_add_option (config,
5145 GST_BUFFER_POOL_OPTION_VIDEO_META);
5148 if (!gst_buffer_pool_set_config (other_pool, config)) {
5149 config = gst_buffer_pool_get_config (other_pool);
5151 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
5153 gst_structure_free (config);
5157 if (!gst_buffer_pool_set_config (other_pool, config))
5163 /* For simplicity, simply read back the active configuration, so our base
5164 * class get the right information */
5165 config = gst_buffer_pool_get_config (pool);
5166 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
5167 gst_structure_free (config);
5171 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
5173 gst_query_add_allocation_pool (query, pool, size, min, max);
5176 gst_object_unref (allocator);
5179 gst_object_unref (pool);
5185 /* setup_pool already send the error */
5190 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
5191 (_("Failed to configure internal buffer pool.")), (NULL));
5196 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
5197 (_("Video device did not suggest any buffer size.")), (NULL));
5203 gst_object_unref (allocator);
5206 gst_object_unref (pool);
5211 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
5212 (_("No downstream pool to import from.")),
5213 ("When importing DMABUF or USERPTR, we need a pool to import from"));
5219 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
5221 GstBufferPool *pool = NULL;
5222 /* we need at least 2 buffers to operate */
5223 guint size, min, max;
5227 /* Set defaults allocation parameters */
5228 size = obj->info.size;
5229 min = GST_V4L2_MIN_BUFFERS (obj);
5230 max = VIDEO_MAX_FRAME;
5232 gst_query_parse_allocation (query, &caps, &need_pool);
5237 switch (obj->mode) {
5238 case GST_V4L2_IO_MMAP:
5239 case GST_V4L2_IO_DMABUF:
5240 if (need_pool && obj->pool) {
5241 if (!gst_buffer_pool_is_active (obj->pool))
5242 pool = gst_object_ref (obj->pool);
5251 GstStructure *config;
5253 /* we had a pool, check caps */
5254 config = gst_buffer_pool_get_config (pool);
5255 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
5257 GST_DEBUG_OBJECT (obj->dbg_obj,
5258 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
5259 if (!gst_caps_is_equal (caps, pcaps)) {
5260 gst_structure_free (config);
5261 gst_object_unref (pool);
5262 goto different_caps;
5264 gst_structure_free (config);
5266 gst_v4l2_get_driver_min_buffers (obj);
5268 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS (obj));
5270 gst_query_add_allocation_pool (query, pool, size, min, max);
5272 /* we also support various metadata */
5273 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
5276 gst_object_unref (pool);
5283 GST_DEBUG_OBJECT (obj->dbg_obj, "no caps specified");
5288 /* different caps, we can't use this pool */
5289 GST_DEBUG_OBJECT (obj->dbg_obj, "pool has different caps");
5295 gst_v4l2_object_try_import (GstV4l2Object * obj, GstBuffer * buffer)
5297 GstVideoMeta *vmeta;
5298 guint n_mem = gst_buffer_n_memory (buffer);
5300 /* only import if requested */
5301 switch (obj->mode) {
5302 case GST_V4L2_IO_USERPTR:
5303 case GST_V4L2_IO_DMABUF_IMPORT:
5306 GST_DEBUG_OBJECT (obj->dbg_obj,
5307 "The io-mode does not enable importation");
5311 vmeta = gst_buffer_get_video_meta (buffer);
5312 if (!vmeta && obj->need_video_meta) {
5313 GST_DEBUG_OBJECT (obj->dbg_obj, "Downstream buffer uses standard "
5314 "stride/offset while the driver does not.");
5318 /* we need matching strides/offsets and size */
5320 guint plane_height[GST_VIDEO_MAX_PLANES] = { 0, };
5322 gst_video_meta_get_plane_height (vmeta, plane_height);
5324 if (!gst_v4l2_object_match_buffer_layout (obj, vmeta->n_planes,
5325 vmeta->offset, vmeta->stride, gst_buffer_get_size (buffer),
5330 /* we can always import single memory buffer, but otherwise we need the same
5331 * amount of memory object. */
5332 if (n_mem != 1 && n_mem != obj->n_v4l2_planes) {
5333 GST_DEBUG_OBJECT (obj->dbg_obj, "Can only import %i memory, "
5334 "buffers contains %u memory", obj->n_v4l2_planes, n_mem);
5338 /* For DMABuf importation we need DMABuf of course */
5339 if (obj->mode == GST_V4L2_IO_DMABUF_IMPORT) {
5342 for (i = 0; i < n_mem; i++) {
5343 GstMemory *mem = gst_buffer_peek_memory (buffer, i);
5345 if (!gst_is_dmabuf_memory (mem)) {
5346 GST_DEBUG_OBJECT (obj->dbg_obj, "Cannot import non-DMABuf memory.");
5352 /* for the remaining, only the kernel driver can tell */