3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@gmail.com>
6 * gstv4l2object.c: base class for V4L2 elements
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Library General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. This library is distributed in the hope
12 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
30 #include <sys/ioctl.h>
34 #include <gudev/gudev.h>
37 #include "ext/videodev2.h"
38 #include "gstv4l2object.h"
39 #include "gstv4l2tuner.h"
40 #include "gstv4l2colorbalance.h"
42 #include "gst/gst-i18n-plugin.h"
44 #include <gst/video/video.h>
45 #include <gst/allocators/gstdmabuf.h>
47 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
48 #define GST_CAT_DEFAULT v4l2_debug
50 #define DEFAULT_PROP_DEVICE_NAME NULL
51 #define DEFAULT_PROP_DEVICE_FD -1
52 #define DEFAULT_PROP_FLAGS 0
53 #define DEFAULT_PROP_TV_NORM 0
54 #define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
56 #define ENCODED_BUFFER_SIZE (2 * 1024 * 1024)
61 V4L2_STD_OBJECT_PROPS,
65 * common format / caps utilities:
69 GST_V4L2_RAW = 1 << 0,
70 GST_V4L2_CODEC = 1 << 1,
71 GST_V4L2_TRANSPORT = 1 << 2,
72 GST_V4L2_NO_PARSE = 1 << 3,
80 GstV4L2FormatFlags flags;
83 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
85 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
86 {V4L2_PIX_FMT_ARGB555, TRUE, GST_V4L2_RAW},
87 {V4L2_PIX_FMT_XRGB555, TRUE, GST_V4L2_RAW},
88 {V4L2_PIX_FMT_ARGB555X, TRUE, GST_V4L2_RAW},
89 {V4L2_PIX_FMT_XRGB555X, TRUE, GST_V4L2_RAW},
90 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
91 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
92 {V4L2_PIX_FMT_BGR666, TRUE, GST_V4L2_RAW},
93 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
94 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
95 {V4L2_PIX_FMT_ABGR32, TRUE, GST_V4L2_RAW},
96 {V4L2_PIX_FMT_XBGR32, TRUE, GST_V4L2_RAW},
97 {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
98 {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},
100 /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
101 {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
102 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
103 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
104 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
105 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
108 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
109 {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
110 {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
111 {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
112 {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
113 {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
114 {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
115 {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},
117 /* Palette formats */
118 {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},
120 /* Chrominance formats */
121 {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},
123 /* Luminance+Chrominance formats */
124 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
125 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
126 {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
127 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
128 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
129 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
130 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
131 {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
132 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
133 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
134 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
135 {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
136 {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
137 {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
138 {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
139 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
140 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
141 {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
142 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
143 {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
144 {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
146 /* two planes -- one Y, one Cr + Cb interleaved */
147 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
148 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
149 {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
150 {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
151 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
152 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
153 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
154 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
155 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
156 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
158 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
160 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
161 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
162 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_CODEC},
163 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_CODEC},
164 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_CODEC},
166 /* compressed formats */
167 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
168 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
169 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
170 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
171 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
172 {V4L2_PIX_FMT_FWHT, FALSE, GST_V4L2_CODEC},
173 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
174 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
175 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
176 {V4L2_PIX_FMT_HEVC, FALSE, GST_V4L2_CODEC},
177 {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
178 {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
179 {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
180 {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
181 {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
182 {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
183 {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
184 {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
185 {V4L2_PIX_FMT_VP9, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
187 /* Vendor-specific formats */
188 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
189 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
190 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
191 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
194 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
196 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
199 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
201 gst_v4l2_device_get_type (void)
203 static GType v4l2_device_type = 0;
205 if (v4l2_device_type == 0) {
206 static const GFlagsValue values[] = {
207 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
208 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
209 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
211 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
212 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
214 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
215 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
221 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
224 return v4l2_device_type;
227 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
229 gst_v4l2_tv_norm_get_type (void)
231 static GType v4l2_tv_norm = 0;
234 static const GEnumValue tv_norms[] = {
237 {V4L2_STD_NTSC, "NTSC", "NTSC"},
238 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
239 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
240 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
241 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
243 {V4L2_STD_PAL, "PAL", "PAL"},
244 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
245 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
246 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
247 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
248 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
249 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
250 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
251 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
252 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
253 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
254 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
255 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
256 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
257 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
259 {V4L2_STD_SECAM, "SECAM", "SECAM"},
260 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
261 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
262 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
263 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
264 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
265 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
266 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
267 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
268 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
273 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
280 gst_v4l2_io_mode_get_type (void)
282 static GType v4l2_io_mode = 0;
285 static const GEnumValue io_modes[] = {
286 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
287 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
288 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
289 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
290 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
291 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
296 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
302 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
303 const char *default_device)
305 g_object_class_install_property (gobject_class, PROP_DEVICE,
306 g_param_spec_string ("device", "Device", "Device location",
307 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
309 g_param_spec_string ("device-name", "Device name",
310 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
311 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
313 g_param_spec_int ("device-fd", "File descriptor",
314 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
315 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (gobject_class, PROP_FLAGS,
317 g_param_spec_flags ("flags", "Flags", "Device type flags",
318 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
319 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
322 * GstV4l2Src:brightness:
324 * Picture brightness, or more precisely, the black level
326 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
327 g_param_spec_int ("brightness", "Brightness",
328 "Picture brightness, or more precisely, the black level", G_MININT,
330 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
332 * GstV4l2Src:contrast:
334 * Picture contrast or luma gain
336 g_object_class_install_property (gobject_class, PROP_CONTRAST,
337 g_param_spec_int ("contrast", "Contrast",
338 "Picture contrast or luma gain", G_MININT,
340 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
342 * GstV4l2Src:saturation:
344 * Picture color saturation or chroma gain
346 g_object_class_install_property (gobject_class, PROP_SATURATION,
347 g_param_spec_int ("saturation", "Saturation",
348 "Picture color saturation or chroma gain", G_MININT,
350 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
354 * Hue or color balance
356 g_object_class_install_property (gobject_class, PROP_HUE,
357 g_param_spec_int ("hue", "Hue",
358 "Hue or color balance", G_MININT,
360 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
367 g_object_class_install_property (gobject_class, PROP_TV_NORM,
368 g_param_spec_enum ("norm", "TV norm",
370 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
371 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
374 * GstV4l2Src:io-mode:
378 g_object_class_install_property (gobject_class, PROP_IO_MODE,
379 g_param_spec_enum ("io-mode", "IO mode",
381 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
382 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385 * GstV4l2Src:extra-controls:
387 * Additional v4l2 controls for the device. The controls are identified
388 * by the control name (lowercase with '_' for any non-alphanumeric
393 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
394 g_param_spec_boxed ("extra-controls", "Extra Controls",
395 "Extra v4l2 controls (CIDs) for the device",
396 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
399 * GstV4l2Src:pixel-aspect-ratio:
401 * The pixel aspect ratio of the device. This overwrites the pixel aspect
402 * ratio queried from the device.
406 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
407 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
408 "Overwrite the pixel aspect ratio of the device", "1/1",
409 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
412 * GstV4l2Src:force-aspect-ratio:
414 * When enabled, the pixel aspect ratio queried from the device or set
415 * with the pixel-aspect-ratio property will be enforced.
419 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
420 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
421 "When enabled, the pixel aspect ratio will be enforced", TRUE,
422 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
427 gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
429 g_object_class_install_property (gobject_class, PROP_DEVICE,
430 g_param_spec_string ("device", "Device", "Device location",
431 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
433 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
434 g_param_spec_string ("device-name", "Device name",
435 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
436 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
438 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
439 g_param_spec_int ("device-fd", "File descriptor",
440 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
441 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
443 g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
444 g_param_spec_enum ("output-io-mode", "Output IO mode",
445 "Output side I/O mode (matches sink pad)",
446 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
447 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
449 g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
450 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
451 "Capture I/O mode (matches src pad)",
452 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
453 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
455 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
456 g_param_spec_boxed ("extra-controls", "Extra Controls",
457 "Extra v4l2 controls (CIDs) for the device",
458 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 /* Support for 32bit off_t, this wrapper is casting off_t to gint64 */
466 v4l2_mmap_wrapper (gpointer start, gsize length, gint prot, gint flags, gint fd,
469 return v4l2_mmap (start, length, prot, flags, fd, (gint64) offset);
472 #define v4l2_mmap v4l2_mmap_wrapper
474 #endif /* SIZEOF_OFF_T < 8 */
475 #endif /* HAVE_LIBV4L2 */
478 gst_v4l2_object_new (GstElement * element,
479 GstObject * debug_object,
480 enum v4l2_buf_type type,
481 const char *default_device,
482 GstV4l2GetInOutFunction get_in_out_func,
483 GstV4l2SetInOutFunction set_in_out_func,
484 GstV4l2UpdateFpsFunction update_fps_func)
486 GstV4l2Object *v4l2object;
489 * some default values
491 v4l2object = g_new0 (GstV4l2Object, 1);
493 v4l2object->type = type;
494 v4l2object->formats = NULL;
496 v4l2object->element = element;
497 v4l2object->dbg_obj = debug_object;
498 v4l2object->get_in_out_func = get_in_out_func;
499 v4l2object->set_in_out_func = set_in_out_func;
500 v4l2object->update_fps_func = update_fps_func;
502 v4l2object->video_fd = -1;
503 v4l2object->active = FALSE;
504 v4l2object->videodev = g_strdup (default_device);
506 v4l2object->norms = NULL;
507 v4l2object->channels = NULL;
508 v4l2object->colors = NULL;
510 v4l2object->keep_aspect = TRUE;
512 v4l2object->n_v4l2_planes = 0;
514 v4l2object->no_initial_format = FALSE;
516 /* We now disable libv4l2 by default, but have an env to enable it. */
518 if (g_getenv ("GST_V4L2_USE_LIBV4L2")) {
519 v4l2object->fd_open = v4l2_fd_open;
520 v4l2object->close = v4l2_close;
521 v4l2object->dup = v4l2_dup;
522 v4l2object->ioctl = v4l2_ioctl;
523 v4l2object->read = v4l2_read;
524 v4l2object->mmap = v4l2_mmap;
525 v4l2object->munmap = v4l2_munmap;
529 v4l2object->fd_open = NULL;
530 v4l2object->close = close;
531 v4l2object->dup = dup;
532 v4l2object->ioctl = ioctl;
533 v4l2object->read = read;
534 v4l2object->mmap = mmap;
535 v4l2object->munmap = munmap;
541 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
545 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
547 g_return_if_fail (v4l2object != NULL);
549 g_free (v4l2object->videodev);
551 g_free (v4l2object->channel);
553 if (v4l2object->formats) {
554 gst_v4l2_object_clear_format_list (v4l2object);
557 if (v4l2object->probed_caps) {
558 gst_caps_unref (v4l2object->probed_caps);
561 if (v4l2object->extra_controls) {
562 gst_structure_free (v4l2object->extra_controls);
570 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
572 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
573 g_slist_free (v4l2object->formats);
574 v4l2object->formats = NULL;
580 gst_v4l2_object_prop_to_cid (guint prop_id)
585 case PROP_BRIGHTNESS:
586 cid = V4L2_CID_BRIGHTNESS;
589 cid = V4L2_CID_CONTRAST;
591 case PROP_SATURATION:
592 cid = V4L2_CID_SATURATION;
598 GST_WARNING ("unmapped property id: %d", prop_id);
605 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
606 guint prop_id, const GValue * value, GParamSpec * pspec)
610 g_free (v4l2object->videodev);
611 v4l2object->videodev = g_value_dup_string (value);
613 case PROP_BRIGHTNESS:
615 case PROP_SATURATION:
618 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
621 if (GST_V4L2_IS_OPEN (v4l2object)) {
622 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
629 v4l2object->tv_norm = g_value_get_enum (value);
633 if (GST_V4L2_IS_OPEN (v4l2object)) {
634 GstTuner *tuner = GST_TUNER (v4l2object->element);
635 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
636 (gchar *) g_value_get_string (value));
639 /* like gst_tuner_set_channel (tuner, channel)
640 without g_object_notify */
641 gst_v4l2_tuner_set_channel (v4l2object, channel);
644 g_free (v4l2object->channel);
645 v4l2object->channel = g_value_dup_string (value);
649 if (GST_V4L2_IS_OPEN (v4l2object)) {
650 GstTuner *tuner = GST_TUNER (v4l2object->element);
651 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
654 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
656 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
657 without g_object_notify */
658 gst_v4l2_tuner_set_frequency (v4l2object, channel,
659 g_value_get_ulong (value));
662 v4l2object->frequency = g_value_get_ulong (value);
668 v4l2object->req_mode = g_value_get_enum (value);
670 case PROP_CAPTURE_IO_MODE:
671 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
672 v4l2object->req_mode = g_value_get_enum (value);
674 case PROP_OUTPUT_IO_MODE:
675 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
676 v4l2object->req_mode = g_value_get_enum (value);
678 case PROP_EXTRA_CONTROLS:{
679 const GstStructure *s = gst_value_get_structure (value);
681 if (v4l2object->extra_controls)
682 gst_structure_free (v4l2object->extra_controls);
684 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
685 if (GST_V4L2_IS_OPEN (v4l2object))
686 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
689 case PROP_PIXEL_ASPECT_RATIO:
690 if (v4l2object->par) {
691 g_value_unset (v4l2object->par);
692 g_free (v4l2object->par);
694 v4l2object->par = g_new0 (GValue, 1);
695 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
696 if (!g_value_transform (value, v4l2object->par)) {
697 g_warning ("Could not transform string to aspect ratio");
698 gst_value_set_fraction (v4l2object->par, 1, 1);
701 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "set PAR to %d/%d",
702 gst_value_get_fraction_numerator (v4l2object->par),
703 gst_value_get_fraction_denominator (v4l2object->par));
705 case PROP_FORCE_ASPECT_RATIO:
706 v4l2object->keep_aspect = g_value_get_boolean (value);
717 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
718 guint prop_id, GValue * value, GParamSpec * pspec)
722 g_value_set_string (value, v4l2object->videodev);
724 case PROP_DEVICE_NAME:
726 const guchar *name = NULL;
728 if (GST_V4L2_IS_OPEN (v4l2object))
729 name = v4l2object->vcap.card;
731 g_value_set_string (value, (gchar *) name);
736 if (GST_V4L2_IS_OPEN (v4l2object))
737 g_value_set_int (value, v4l2object->video_fd);
739 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
746 if (GST_V4L2_IS_OPEN (v4l2object)) {
747 flags |= v4l2object->device_caps &
748 (V4L2_CAP_VIDEO_CAPTURE |
749 V4L2_CAP_VIDEO_OUTPUT |
750 V4L2_CAP_VIDEO_OVERLAY |
751 V4L2_CAP_VBI_CAPTURE |
752 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
754 if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
755 flags |= V4L2_CAP_VIDEO_CAPTURE;
757 if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
758 flags |= V4L2_CAP_VIDEO_OUTPUT;
760 g_value_set_flags (value, flags);
763 case PROP_BRIGHTNESS:
765 case PROP_SATURATION:
768 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
771 if (GST_V4L2_IS_OPEN (v4l2object)) {
773 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
774 g_value_set_int (value, v);
782 g_value_set_enum (value, v4l2object->tv_norm);
785 g_value_set_enum (value, v4l2object->req_mode);
787 case PROP_CAPTURE_IO_MODE:
788 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
789 g_value_set_enum (value, v4l2object->req_mode);
791 case PROP_OUTPUT_IO_MODE:
792 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
793 g_value_set_enum (value, v4l2object->req_mode);
795 case PROP_EXTRA_CONTROLS:
796 gst_value_set_structure (value, v4l2object->extra_controls);
798 case PROP_PIXEL_ASPECT_RATIO:
800 g_value_transform (v4l2object->par, value);
802 case PROP_FORCE_ASPECT_RATIO:
803 g_value_set_boolean (value, v4l2object->keep_aspect);
813 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
815 struct v4l2_control control = { 0, };
817 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
819 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
820 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
822 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
824 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
825 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
826 "driver requires a minimum of %d buffers", control.value);
827 v4l2object->min_buffers = control.value;
829 v4l2object->min_buffers = 0;
834 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
836 GstTunerNorm *norm = NULL;
837 GstTunerChannel *channel = NULL;
840 if (!GST_IS_TUNER (v4l2object->element))
843 tuner = GST_TUNER (v4l2object->element);
845 if (v4l2object->tv_norm)
846 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
847 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
848 "norm=%p", (guint64) v4l2object->tv_norm, norm);
850 gst_tuner_set_norm (tuner, norm);
853 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
855 v4l2object->tv_norm =
856 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
857 gst_tuner_norm_changed (tuner, norm);
861 if (v4l2object->channel)
862 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
864 gst_tuner_set_channel (tuner, channel);
867 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
868 (v4l2object->element)));
870 g_free (v4l2object->channel);
871 v4l2object->channel = g_strdup (channel->label);
872 gst_tuner_channel_changed (tuner, channel);
877 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
878 if (v4l2object->frequency != 0) {
879 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
881 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
882 if (v4l2object->frequency == 0) {
884 gst_tuner_set_frequency (tuner, channel, 1000);
892 gst_v4l2_object_open (GstV4l2Object * v4l2object)
894 if (gst_v4l2_open (v4l2object))
895 gst_v4l2_set_defaults (v4l2object);
903 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
907 ret = gst_v4l2_dup (v4l2object, other);
913 gst_v4l2_object_close (GstV4l2Object * v4l2object)
915 if (!gst_v4l2_close (v4l2object))
918 gst_caps_replace (&v4l2object->probed_caps, NULL);
920 /* reset our copy of the device caps */
921 v4l2object->device_caps = 0;
923 if (v4l2object->formats) {
924 gst_v4l2_object_clear_format_list (v4l2object);
927 if (v4l2object->par) {
928 g_value_unset (v4l2object->par);
929 g_free (v4l2object->par);
930 v4l2object->par = NULL;
933 if (v4l2object->channel) {
934 g_free (v4l2object->channel);
935 v4l2object->channel = NULL;
941 static struct v4l2_fmtdesc *
942 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
945 struct v4l2_fmtdesc *fmt;
951 walk = gst_v4l2_object_get_format_list (v4l2object);
953 fmt = (struct v4l2_fmtdesc *) walk->data;
954 if (fmt->pixelformat == fourcc)
956 /* special case for jpeg */
957 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
958 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
959 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
960 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
961 fourcc == V4L2_PIX_FMT_PJPG) {
965 walk = g_slist_next (walk);
973 /* complete made up ranking, the values themselves are meaningless */
974 /* These ranks MUST be X such that X<<15 fits on a signed int - see
975 the comment at the end of gst_v4l2_object_format_get_rank. */
976 #define YUV_BASE_RANK 1000
977 #define JPEG_BASE_RANK 500
978 #define DV_BASE_RANK 200
979 #define RGB_BASE_RANK 100
980 #define YUV_ODD_BASE_RANK 50
981 #define RGB_ODD_BASE_RANK 25
982 #define BAYER_BASE_RANK 15
983 #define S910_BASE_RANK 10
984 #define GREY_BASE_RANK 5
985 #define PWC_BASE_RANK 1
988 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
990 guint32 fourcc = fmt->pixelformat;
991 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
995 case V4L2_PIX_FMT_MJPEG:
996 case V4L2_PIX_FMT_PJPG:
997 rank = JPEG_BASE_RANK;
999 case V4L2_PIX_FMT_JPEG:
1000 rank = JPEG_BASE_RANK + 1;
1002 case V4L2_PIX_FMT_MPEG: /* MPEG */
1003 rank = JPEG_BASE_RANK + 2;
1006 case V4L2_PIX_FMT_RGB332:
1007 case V4L2_PIX_FMT_ARGB555:
1008 case V4L2_PIX_FMT_XRGB555:
1009 case V4L2_PIX_FMT_RGB555:
1010 case V4L2_PIX_FMT_ARGB555X:
1011 case V4L2_PIX_FMT_XRGB555X:
1012 case V4L2_PIX_FMT_RGB555X:
1013 case V4L2_PIX_FMT_BGR666:
1014 case V4L2_PIX_FMT_RGB565:
1015 case V4L2_PIX_FMT_RGB565X:
1016 case V4L2_PIX_FMT_RGB444:
1017 case V4L2_PIX_FMT_Y4:
1018 case V4L2_PIX_FMT_Y6:
1019 case V4L2_PIX_FMT_Y10:
1020 case V4L2_PIX_FMT_Y12:
1021 case V4L2_PIX_FMT_Y10BPACK:
1022 case V4L2_PIX_FMT_YUV555:
1023 case V4L2_PIX_FMT_YUV565:
1024 case V4L2_PIX_FMT_YUV32:
1025 case V4L2_PIX_FMT_NV12MT_16X16:
1026 case V4L2_PIX_FMT_NV42:
1027 case V4L2_PIX_FMT_H264_MVC:
1028 rank = RGB_ODD_BASE_RANK;
1031 case V4L2_PIX_FMT_RGB24:
1032 case V4L2_PIX_FMT_BGR24:
1033 rank = RGB_BASE_RANK - 1;
1036 case V4L2_PIX_FMT_RGB32:
1037 case V4L2_PIX_FMT_BGR32:
1038 case V4L2_PIX_FMT_ABGR32:
1039 case V4L2_PIX_FMT_XBGR32:
1040 case V4L2_PIX_FMT_ARGB32:
1041 case V4L2_PIX_FMT_XRGB32:
1042 rank = RGB_BASE_RANK;
1045 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1046 rank = GREY_BASE_RANK;
1049 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1050 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
1051 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
1052 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1053 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1054 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1055 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1056 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1057 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1058 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1059 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1060 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1061 rank = YUV_ODD_BASE_RANK;
1064 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1065 rank = YUV_BASE_RANK + 3;
1067 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1068 rank = YUV_BASE_RANK + 2;
1070 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1071 case V4L2_PIX_FMT_YUV420M:
1072 rank = YUV_BASE_RANK + 7;
1074 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1075 rank = YUV_BASE_RANK + 10;
1077 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1078 rank = YUV_BASE_RANK + 6;
1080 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1081 rank = YUV_BASE_RANK + 9;
1083 case V4L2_PIX_FMT_YUV444:
1084 rank = YUV_BASE_RANK + 6;
1086 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1087 rank = YUV_BASE_RANK + 5;
1089 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1090 rank = YUV_BASE_RANK + 4;
1092 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1093 rank = YUV_BASE_RANK + 8;
1096 case V4L2_PIX_FMT_DV:
1097 rank = DV_BASE_RANK;
1100 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1104 case V4L2_PIX_FMT_SBGGR8:
1105 case V4L2_PIX_FMT_SGBRG8:
1106 case V4L2_PIX_FMT_SGRBG8:
1107 case V4L2_PIX_FMT_SRGGB8:
1108 rank = BAYER_BASE_RANK;
1111 case V4L2_PIX_FMT_SN9C10X:
1112 rank = S910_BASE_RANK;
1115 case V4L2_PIX_FMT_PWC1:
1116 rank = PWC_BASE_RANK;
1118 case V4L2_PIX_FMT_PWC2:
1119 rank = PWC_BASE_RANK;
1127 /* All ranks are below 1<<15 so a shift by 15
1128 * will a) make all non-emulated formats larger
1129 * than emulated and b) will not overflow
1140 format_cmp_func (gconstpointer a, gconstpointer b)
1142 const struct v4l2_fmtdesc *fa = a;
1143 const struct v4l2_fmtdesc *fb = b;
1145 if (fa->pixelformat == fb->pixelformat)
1148 return gst_v4l2_object_format_get_rank (fb) -
1149 gst_v4l2_object_format_get_rank (fa);
1152 /******************************************************
1153 * gst_v4l2_object_fill_format_list():
1154 * create list of supported capture formats
1155 * return value: TRUE on success, FALSE on error
1156 ******************************************************/
1158 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1159 enum v4l2_buf_type type)
1162 struct v4l2_fmtdesc *format;
1164 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting src format enumerations");
1166 /* format enumeration */
1168 format = g_new0 (struct v4l2_fmtdesc, 1);
1171 format->type = type;
1173 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1174 if (errno == EINVAL) {
1176 break; /* end of enumeration */
1182 GST_LOG_OBJECT (v4l2object->dbg_obj, "index: %u", format->index);
1183 GST_LOG_OBJECT (v4l2object->dbg_obj, "type: %d", format->type);
1184 GST_LOG_OBJECT (v4l2object->dbg_obj, "flags: %08x", format->flags);
1185 GST_LOG_OBJECT (v4l2object->dbg_obj, "description: '%s'",
1186 format->description);
1187 GST_LOG_OBJECT (v4l2object->dbg_obj, "pixelformat: %" GST_FOURCC_FORMAT,
1188 GST_FOURCC_ARGS (format->pixelformat));
1190 /* sort formats according to our preference; we do this, because caps
1191 * are probed in the order the formats are in the list, and the order of
1192 * formats in the final probed caps matters for things like fixation */
1193 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1194 (GCompareFunc) format_cmp_func);
1197 #ifndef GST_DISABLE_GST_DEBUG
1201 GST_INFO_OBJECT (v4l2object->dbg_obj, "got %d format(s):", n);
1202 for (l = v4l2object->formats; l != NULL; l = l->next) {
1205 GST_INFO_OBJECT (v4l2object->dbg_obj,
1206 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1207 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1219 if (v4l2object->element)
1222 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1223 (_("Failed to enumerate possible video formats device '%s' can work "
1224 "with"), v4l2object->videodev),
1225 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1226 n, v4l2object->videodev, errno, g_strerror (errno)));
1233 * Get the list of supported capture formats, a list of
1234 * <code>struct v4l2_fmtdesc</code>.
1237 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1239 if (!v4l2object->formats) {
1241 /* check usual way */
1242 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1244 /* if our driver supports multi-planar
1245 * and if formats are still empty then we can workaround driver bug
1246 * by also looking up formats as if our device was not supporting
1248 if (!v4l2object->formats) {
1249 switch (v4l2object->type) {
1250 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1251 gst_v4l2_object_fill_format_list (v4l2object,
1252 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1255 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1256 gst_v4l2_object_fill_format_list (v4l2object,
1257 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1265 return v4l2object->formats;
1268 static GstVideoFormat
1269 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1271 GstVideoFormat format;
1274 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1275 format = GST_VIDEO_FORMAT_GRAY8;
1277 case V4L2_PIX_FMT_Y16:
1278 format = GST_VIDEO_FORMAT_GRAY16_LE;
1280 case V4L2_PIX_FMT_Y16_BE:
1281 format = GST_VIDEO_FORMAT_GRAY16_BE;
1283 case V4L2_PIX_FMT_XRGB555:
1284 case V4L2_PIX_FMT_RGB555:
1285 format = GST_VIDEO_FORMAT_RGB15;
1287 case V4L2_PIX_FMT_XRGB555X:
1288 case V4L2_PIX_FMT_RGB555X:
1289 format = GST_VIDEO_FORMAT_BGR15;
1291 case V4L2_PIX_FMT_RGB565:
1292 format = GST_VIDEO_FORMAT_RGB16;
1294 case V4L2_PIX_FMT_RGB24:
1295 format = GST_VIDEO_FORMAT_RGB;
1297 case V4L2_PIX_FMT_BGR24:
1298 format = GST_VIDEO_FORMAT_BGR;
1300 case V4L2_PIX_FMT_XRGB32:
1301 case V4L2_PIX_FMT_RGB32:
1302 format = GST_VIDEO_FORMAT_xRGB;
1304 case V4L2_PIX_FMT_XBGR32:
1305 case V4L2_PIX_FMT_BGR32:
1306 format = GST_VIDEO_FORMAT_BGRx;
1308 case V4L2_PIX_FMT_ABGR32:
1309 format = GST_VIDEO_FORMAT_BGRA;
1311 case V4L2_PIX_FMT_ARGB32:
1312 format = GST_VIDEO_FORMAT_ARGB;
1314 case V4L2_PIX_FMT_NV12:
1315 case V4L2_PIX_FMT_NV12M:
1316 format = GST_VIDEO_FORMAT_NV12;
1318 case V4L2_PIX_FMT_NV12MT:
1319 format = GST_VIDEO_FORMAT_NV12_64Z32;
1321 case V4L2_PIX_FMT_NV21:
1322 case V4L2_PIX_FMT_NV21M:
1323 format = GST_VIDEO_FORMAT_NV21;
1325 case V4L2_PIX_FMT_YVU410:
1326 format = GST_VIDEO_FORMAT_YVU9;
1328 case V4L2_PIX_FMT_YUV410:
1329 format = GST_VIDEO_FORMAT_YUV9;
1331 case V4L2_PIX_FMT_YUV420:
1332 case V4L2_PIX_FMT_YUV420M:
1333 format = GST_VIDEO_FORMAT_I420;
1335 case V4L2_PIX_FMT_YUYV:
1336 format = GST_VIDEO_FORMAT_YUY2;
1338 case V4L2_PIX_FMT_YVU420:
1339 format = GST_VIDEO_FORMAT_YV12;
1341 case V4L2_PIX_FMT_UYVY:
1342 format = GST_VIDEO_FORMAT_UYVY;
1344 case V4L2_PIX_FMT_YUV411P:
1345 format = GST_VIDEO_FORMAT_Y41B;
1347 case V4L2_PIX_FMT_YUV422P:
1348 format = GST_VIDEO_FORMAT_Y42B;
1350 case V4L2_PIX_FMT_YVYU:
1351 format = GST_VIDEO_FORMAT_YVYU;
1353 case V4L2_PIX_FMT_NV16:
1354 case V4L2_PIX_FMT_NV16M:
1355 format = GST_VIDEO_FORMAT_NV16;
1357 case V4L2_PIX_FMT_NV61:
1358 case V4L2_PIX_FMT_NV61M:
1359 format = GST_VIDEO_FORMAT_NV61;
1361 case V4L2_PIX_FMT_NV24:
1362 format = GST_VIDEO_FORMAT_NV24;
1365 format = GST_VIDEO_FORMAT_UNKNOWN;
1373 gst_v4l2_object_v4l2fourcc_is_rgb (guint32 fourcc)
1375 gboolean ret = FALSE;
1378 case V4L2_PIX_FMT_XRGB555:
1379 case V4L2_PIX_FMT_RGB555:
1380 case V4L2_PIX_FMT_XRGB555X:
1381 case V4L2_PIX_FMT_RGB555X:
1382 case V4L2_PIX_FMT_RGB565:
1383 case V4L2_PIX_FMT_RGB24:
1384 case V4L2_PIX_FMT_BGR24:
1385 case V4L2_PIX_FMT_XRGB32:
1386 case V4L2_PIX_FMT_RGB32:
1387 case V4L2_PIX_FMT_XBGR32:
1388 case V4L2_PIX_FMT_BGR32:
1389 case V4L2_PIX_FMT_ABGR32:
1390 case V4L2_PIX_FMT_ARGB32:
1400 static GstStructure *
1401 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1403 GstStructure *structure = NULL;
1406 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1407 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1408 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1409 structure = gst_structure_new_empty ("image/jpeg");
1411 case V4L2_PIX_FMT_MPEG1:
1412 structure = gst_structure_new ("video/mpeg",
1413 "mpegversion", G_TYPE_INT, 2, NULL);
1415 case V4L2_PIX_FMT_MPEG2:
1416 structure = gst_structure_new ("video/mpeg",
1417 "mpegversion", G_TYPE_INT, 2, NULL);
1419 case V4L2_PIX_FMT_MPEG4:
1420 case V4L2_PIX_FMT_XVID:
1421 structure = gst_structure_new ("video/mpeg",
1422 "mpegversion", G_TYPE_INT, 4, "systemstream",
1423 G_TYPE_BOOLEAN, FALSE, NULL);
1425 case V4L2_PIX_FMT_FWHT:
1426 structure = gst_structure_new_empty ("video/x-fwht");
1428 case V4L2_PIX_FMT_H263:
1429 structure = gst_structure_new ("video/x-h263",
1430 "variant", G_TYPE_STRING, "itu", NULL);
1432 case V4L2_PIX_FMT_H264: /* H.264 */
1433 structure = gst_structure_new ("video/x-h264",
1434 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1435 G_TYPE_STRING, "au", NULL);
1437 case V4L2_PIX_FMT_H264_NO_SC:
1438 structure = gst_structure_new ("video/x-h264",
1439 "stream-format", G_TYPE_STRING, "avc", "alignment",
1440 G_TYPE_STRING, "au", NULL);
1442 case V4L2_PIX_FMT_HEVC: /* H.265 */
1443 structure = gst_structure_new ("video/x-h265",
1444 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1445 G_TYPE_STRING, "au", NULL);
1447 case V4L2_PIX_FMT_VC1_ANNEX_G:
1448 case V4L2_PIX_FMT_VC1_ANNEX_L:
1449 structure = gst_structure_new ("video/x-wmv",
1450 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1452 case V4L2_PIX_FMT_VP8:
1453 structure = gst_structure_new_empty ("video/x-vp8");
1455 case V4L2_PIX_FMT_VP9:
1456 structure = gst_structure_new_empty ("video/x-vp9");
1458 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1459 case V4L2_PIX_FMT_Y16:
1460 case V4L2_PIX_FMT_Y16_BE:
1461 case V4L2_PIX_FMT_XRGB555:
1462 case V4L2_PIX_FMT_RGB555:
1463 case V4L2_PIX_FMT_XRGB555X:
1464 case V4L2_PIX_FMT_RGB555X:
1465 case V4L2_PIX_FMT_RGB565:
1466 case V4L2_PIX_FMT_RGB24:
1467 case V4L2_PIX_FMT_BGR24:
1468 case V4L2_PIX_FMT_RGB32:
1469 case V4L2_PIX_FMT_XRGB32:
1470 case V4L2_PIX_FMT_ARGB32:
1471 case V4L2_PIX_FMT_BGR32:
1472 case V4L2_PIX_FMT_XBGR32:
1473 case V4L2_PIX_FMT_ABGR32:
1474 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1475 case V4L2_PIX_FMT_NV12M:
1476 case V4L2_PIX_FMT_NV12MT:
1477 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1478 case V4L2_PIX_FMT_NV21M:
1479 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1480 case V4L2_PIX_FMT_NV16M:
1481 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1482 case V4L2_PIX_FMT_NV61M:
1483 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1484 case V4L2_PIX_FMT_YVU410:
1485 case V4L2_PIX_FMT_YUV410:
1486 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1487 case V4L2_PIX_FMT_YUV420M:
1488 case V4L2_PIX_FMT_YUYV:
1489 case V4L2_PIX_FMT_YVU420:
1490 case V4L2_PIX_FMT_UYVY:
1491 case V4L2_PIX_FMT_YUV422P:
1492 case V4L2_PIX_FMT_YVYU:
1493 case V4L2_PIX_FMT_YUV411P:{
1494 GstVideoFormat format;
1495 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1496 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1497 structure = gst_structure_new ("video/x-raw",
1498 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1501 case V4L2_PIX_FMT_DV:
1503 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1506 case V4L2_PIX_FMT_MPEG: /* MPEG */
1507 structure = gst_structure_new ("video/mpegts",
1508 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1510 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1512 case V4L2_PIX_FMT_SBGGR8:
1513 case V4L2_PIX_FMT_SGBRG8:
1514 case V4L2_PIX_FMT_SGRBG8:
1515 case V4L2_PIX_FMT_SRGGB8:
1516 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1517 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1518 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1519 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1520 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1522 case V4L2_PIX_FMT_SN9C10X:
1523 structure = gst_structure_new_empty ("video/x-sonix");
1525 case V4L2_PIX_FMT_PWC1:
1526 structure = gst_structure_new_empty ("video/x-pwc1");
1528 case V4L2_PIX_FMT_PWC2:
1529 structure = gst_structure_new_empty ("video/x-pwc2");
1531 case V4L2_PIX_FMT_RGB332:
1532 case V4L2_PIX_FMT_BGR666:
1533 case V4L2_PIX_FMT_ARGB555X:
1534 case V4L2_PIX_FMT_RGB565X:
1535 case V4L2_PIX_FMT_RGB444:
1536 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1537 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1538 case V4L2_PIX_FMT_Y4:
1539 case V4L2_PIX_FMT_Y6:
1540 case V4L2_PIX_FMT_Y10:
1541 case V4L2_PIX_FMT_Y12:
1542 case V4L2_PIX_FMT_Y10BPACK:
1543 case V4L2_PIX_FMT_YUV444:
1544 case V4L2_PIX_FMT_YUV555:
1545 case V4L2_PIX_FMT_YUV565:
1546 case V4L2_PIX_FMT_Y41P:
1547 case V4L2_PIX_FMT_YUV32:
1548 case V4L2_PIX_FMT_NV12MT_16X16:
1549 case V4L2_PIX_FMT_NV42:
1550 case V4L2_PIX_FMT_H264_MVC:
1552 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1553 fourcc, GST_FOURCC_ARGS (fourcc));
1561 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1563 GstStructure *template;
1566 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1568 if (template == NULL)
1571 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1572 if (gst_v4l2_formats[i].format != fourcc)
1575 if (gst_v4l2_formats[i].dimensions) {
1576 gst_structure_set (template,
1577 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1578 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1579 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1590 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1592 GstStructure *structure;
1596 caps = gst_caps_new_empty ();
1597 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1599 if ((gst_v4l2_formats[i].flags & flags) == 0)
1603 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1606 GstStructure *alt_s = NULL;
1608 if (gst_v4l2_formats[i].dimensions) {
1609 gst_structure_set (structure,
1610 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1611 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1612 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1615 switch (gst_v4l2_formats[i].format) {
1616 case V4L2_PIX_FMT_RGB32:
1617 alt_s = gst_structure_copy (structure);
1618 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1620 case V4L2_PIX_FMT_BGR32:
1621 alt_s = gst_structure_copy (structure);
1622 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1627 gst_caps_append_structure (caps, structure);
1630 gst_caps_append_structure (caps, alt_s);
1634 return gst_caps_simplify (caps);
1638 gst_v4l2_object_get_all_caps (void)
1640 static GstCaps *caps = NULL;
1642 if (g_once_init_enter (&caps)) {
1643 GstCaps *all_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1644 GST_MINI_OBJECT_FLAG_SET (all_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1645 g_once_init_leave (&caps, all_caps);
1652 gst_v4l2_object_get_raw_caps (void)
1654 static GstCaps *caps = NULL;
1656 if (g_once_init_enter (&caps)) {
1657 GstCaps *raw_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1658 GST_MINI_OBJECT_FLAG_SET (raw_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1659 g_once_init_leave (&caps, raw_caps);
1666 gst_v4l2_object_get_codec_caps (void)
1668 static GstCaps *caps = NULL;
1670 if (g_once_init_enter (&caps)) {
1671 GstCaps *codec_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1672 GST_MINI_OBJECT_FLAG_SET (codec_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1673 g_once_init_leave (&caps, codec_caps);
1679 /* collect data for the given caps
1680 * @caps: given input caps
1681 * @format: location for the v4l format
1682 * @w/@h: location for width and height
1683 * @fps_n/@fps_d: location for framerate
1684 * @size: location for expected size of the frame or 0 if unknown
1687 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1688 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1690 GstStructure *structure;
1691 guint32 fourcc = 0, fourcc_nc = 0;
1692 const gchar *mimetype;
1693 struct v4l2_fmtdesc *fmt = NULL;
1695 structure = gst_caps_get_structure (caps, 0);
1697 mimetype = gst_structure_get_name (structure);
1699 if (!gst_video_info_from_caps (info, caps))
1700 goto invalid_format;
1702 if (g_str_equal (mimetype, "video/x-raw")) {
1703 switch (GST_VIDEO_INFO_FORMAT (info)) {
1704 case GST_VIDEO_FORMAT_I420:
1705 fourcc = V4L2_PIX_FMT_YUV420;
1706 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1708 case GST_VIDEO_FORMAT_YUY2:
1709 fourcc = V4L2_PIX_FMT_YUYV;
1711 case GST_VIDEO_FORMAT_UYVY:
1712 fourcc = V4L2_PIX_FMT_UYVY;
1714 case GST_VIDEO_FORMAT_YV12:
1715 fourcc = V4L2_PIX_FMT_YVU420;
1717 case GST_VIDEO_FORMAT_Y41B:
1718 fourcc = V4L2_PIX_FMT_YUV411P;
1720 case GST_VIDEO_FORMAT_Y42B:
1721 fourcc = V4L2_PIX_FMT_YUV422P;
1723 case GST_VIDEO_FORMAT_NV12:
1724 fourcc = V4L2_PIX_FMT_NV12;
1725 fourcc_nc = V4L2_PIX_FMT_NV12M;
1727 case GST_VIDEO_FORMAT_NV12_64Z32:
1728 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1730 case GST_VIDEO_FORMAT_NV21:
1731 fourcc = V4L2_PIX_FMT_NV21;
1732 fourcc_nc = V4L2_PIX_FMT_NV21M;
1734 case GST_VIDEO_FORMAT_NV16:
1735 fourcc = V4L2_PIX_FMT_NV16;
1736 fourcc_nc = V4L2_PIX_FMT_NV16M;
1738 case GST_VIDEO_FORMAT_NV61:
1739 fourcc = V4L2_PIX_FMT_NV61;
1740 fourcc_nc = V4L2_PIX_FMT_NV61M;
1742 case GST_VIDEO_FORMAT_NV24:
1743 fourcc = V4L2_PIX_FMT_NV24;
1745 case GST_VIDEO_FORMAT_YVYU:
1746 fourcc = V4L2_PIX_FMT_YVYU;
1748 case GST_VIDEO_FORMAT_RGB15:
1749 fourcc = V4L2_PIX_FMT_RGB555;
1750 fourcc_nc = V4L2_PIX_FMT_XRGB555;
1752 case GST_VIDEO_FORMAT_RGB16:
1753 fourcc = V4L2_PIX_FMT_RGB565;
1755 case GST_VIDEO_FORMAT_RGB:
1756 fourcc = V4L2_PIX_FMT_RGB24;
1758 case GST_VIDEO_FORMAT_BGR:
1759 fourcc = V4L2_PIX_FMT_BGR24;
1761 case GST_VIDEO_FORMAT_xRGB:
1762 fourcc = V4L2_PIX_FMT_RGB32;
1763 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1765 case GST_VIDEO_FORMAT_ARGB:
1766 fourcc = V4L2_PIX_FMT_RGB32;
1767 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1769 case GST_VIDEO_FORMAT_BGRx:
1770 fourcc = V4L2_PIX_FMT_BGR32;
1771 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1773 case GST_VIDEO_FORMAT_BGRA:
1774 fourcc = V4L2_PIX_FMT_BGR32;
1775 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1777 case GST_VIDEO_FORMAT_GRAY8:
1778 fourcc = V4L2_PIX_FMT_GREY;
1780 case GST_VIDEO_FORMAT_GRAY16_LE:
1781 fourcc = V4L2_PIX_FMT_Y16;
1783 case GST_VIDEO_FORMAT_GRAY16_BE:
1784 fourcc = V4L2_PIX_FMT_Y16_BE;
1790 if (g_str_equal (mimetype, "video/mpegts")) {
1791 fourcc = V4L2_PIX_FMT_MPEG;
1792 } else if (g_str_equal (mimetype, "video/x-dv")) {
1793 fourcc = V4L2_PIX_FMT_DV;
1794 } else if (g_str_equal (mimetype, "image/jpeg")) {
1795 fourcc = V4L2_PIX_FMT_JPEG;
1796 } else if (g_str_equal (mimetype, "video/mpeg")) {
1798 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1801 fourcc = V4L2_PIX_FMT_MPEG1;
1804 fourcc = V4L2_PIX_FMT_MPEG2;
1807 fourcc = V4L2_PIX_FMT_MPEG4;
1808 fourcc_nc = V4L2_PIX_FMT_XVID;
1814 } else if (g_str_equal (mimetype, "video/x-fwht")) {
1815 fourcc = V4L2_PIX_FMT_FWHT;
1816 } else if (g_str_equal (mimetype, "video/x-h263")) {
1817 fourcc = V4L2_PIX_FMT_H263;
1818 } else if (g_str_equal (mimetype, "video/x-h264")) {
1819 const gchar *stream_format =
1820 gst_structure_get_string (structure, "stream-format");
1821 if (g_str_equal (stream_format, "avc"))
1822 fourcc = V4L2_PIX_FMT_H264_NO_SC;
1824 fourcc = V4L2_PIX_FMT_H264;
1825 } else if (g_str_equal (mimetype, "video/x-h265")) {
1826 fourcc = V4L2_PIX_FMT_HEVC;
1827 } else if (g_str_equal (mimetype, "video/x-vp8")) {
1828 fourcc = V4L2_PIX_FMT_VP8;
1829 } else if (g_str_equal (mimetype, "video/x-vp9")) {
1830 fourcc = V4L2_PIX_FMT_VP9;
1831 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1832 const gchar *format = gst_structure_get_string (structure, "format");
1834 if (!g_ascii_strcasecmp (format, "bggr"))
1835 fourcc = V4L2_PIX_FMT_SBGGR8;
1836 else if (!g_ascii_strcasecmp (format, "gbrg"))
1837 fourcc = V4L2_PIX_FMT_SGBRG8;
1838 else if (!g_ascii_strcasecmp (format, "grbg"))
1839 fourcc = V4L2_PIX_FMT_SGRBG8;
1840 else if (!g_ascii_strcasecmp (format, "rggb"))
1841 fourcc = V4L2_PIX_FMT_SRGGB8;
1843 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1844 fourcc = V4L2_PIX_FMT_SN9C10X;
1845 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1846 fourcc = V4L2_PIX_FMT_PWC1;
1847 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1848 fourcc = V4L2_PIX_FMT_PWC2;
1853 /* Prefer the non-contiguous if supported */
1854 v4l2object->prefered_non_contiguous = TRUE;
1857 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
1858 else if (fourcc == 0)
1859 goto unhandled_format;
1862 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1863 v4l2object->prefered_non_contiguous = FALSE;
1867 goto unsupported_format;
1876 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1881 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1886 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1892 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1893 guint32 pixelformat, gint * width, gint * height);
1896 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1898 if (v4l2object->keep_aspect && v4l2object->par)
1899 gst_structure_set_value (s, "pixel-aspect-ratio", v4l2object->par);
1902 /* returns TRUE if the value was changed in place, otherwise FALSE */
1904 gst_v4l2src_value_simplify (GValue * val)
1906 /* simplify list of one value to one value */
1907 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
1908 const GValue *list_val;
1909 GValue new_val = G_VALUE_INIT;
1911 list_val = gst_value_list_get_value (val, 0);
1912 g_value_init (&new_val, G_VALUE_TYPE (list_val));
1913 g_value_copy (list_val, &new_val);
1914 g_value_unset (val);
1923 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
1924 GstVideoInterlaceMode * interlace_mode)
1926 /* NB: If you add new return values, please fix mode_strings in
1927 * gst_v4l2_object_add_interlace_mode */
1929 case V4L2_FIELD_ANY:
1931 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
1933 case V4L2_FIELD_NONE:
1934 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1936 case V4L2_FIELD_INTERLACED:
1937 case V4L2_FIELD_INTERLACED_TB:
1938 case V4L2_FIELD_INTERLACED_BT:
1939 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1942 GST_ERROR ("Unknown enum v4l2_field %d", field);
1948 gst_v4l2_object_get_colorspace (struct v4l2_format *fmt,
1949 GstVideoColorimetry * cinfo)
1952 gst_v4l2_object_v4l2fourcc_is_rgb (fmt->fmt.pix.pixelformat);
1953 enum v4l2_colorspace colorspace;
1954 enum v4l2_quantization range;
1955 enum v4l2_ycbcr_encoding matrix;
1956 enum v4l2_xfer_func transfer;
1957 gboolean ret = TRUE;
1959 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
1960 colorspace = fmt->fmt.pix_mp.colorspace;
1961 range = fmt->fmt.pix_mp.quantization;
1962 matrix = fmt->fmt.pix_mp.ycbcr_enc;
1963 transfer = fmt->fmt.pix_mp.xfer_func;
1965 colorspace = fmt->fmt.pix.colorspace;
1966 range = fmt->fmt.pix.quantization;
1967 matrix = fmt->fmt.pix.ycbcr_enc;
1968 transfer = fmt->fmt.pix.xfer_func;
1971 /* First step, set the defaults for each primaries */
1972 switch (colorspace) {
1973 case V4L2_COLORSPACE_SMPTE170M:
1974 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1975 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1976 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1977 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1979 case V4L2_COLORSPACE_REC709:
1980 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1981 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1982 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1983 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1985 case V4L2_COLORSPACE_SRGB:
1986 case V4L2_COLORSPACE_JPEG:
1987 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1988 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1989 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
1990 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1992 case V4L2_COLORSPACE_ADOBERGB:
1993 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1994 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1995 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
1996 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
1998 case V4L2_COLORSPACE_BT2020:
1999 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2000 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2001 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2002 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
2004 case V4L2_COLORSPACE_SMPTE240M:
2005 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2006 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2007 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2008 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
2010 case V4L2_COLORSPACE_470_SYSTEM_M:
2011 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2012 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2013 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2014 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
2016 case V4L2_COLORSPACE_470_SYSTEM_BG:
2017 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2018 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2019 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2020 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
2022 case V4L2_COLORSPACE_RAW:
2023 /* Explicitly unknown */
2024 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2025 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2026 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2027 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
2030 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
2038 /* Second step, apply any custom variation */
2040 case V4L2_QUANTIZATION_FULL_RANGE:
2041 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2043 case V4L2_QUANTIZATION_LIM_RANGE:
2044 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2046 case V4L2_QUANTIZATION_DEFAULT:
2047 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
2048 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
2049 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2050 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601
2051 || matrix == V4L2_YCBCR_ENC_XV709
2052 || colorspace == V4L2_COLORSPACE_JPEG)
2053 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2055 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2058 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
2059 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2064 case V4L2_YCBCR_ENC_XV601:
2065 case V4L2_YCBCR_ENC_SYCC:
2066 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
2068 case V4L2_YCBCR_ENC_601:
2069 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2071 case V4L2_YCBCR_ENC_XV709:
2072 GST_FIXME ("XV709 not defined, assuming 709");
2074 case V4L2_YCBCR_ENC_709:
2075 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2077 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2078 GST_FIXME ("BT2020 with constant luma is not defined, assuming BT2020");
2080 case V4L2_YCBCR_ENC_BT2020:
2081 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2083 case V4L2_YCBCR_ENC_SMPTE240M:
2084 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2086 case V4L2_YCBCR_ENC_DEFAULT:
2087 /* nothing, just use defaults for colorspace */
2090 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2091 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2095 /* Set identity matrix for R'G'B' formats to avoid creating
2096 * confusion. This though is cosmetic as it's now properly ignored by
2097 * the video info API and videoconvert. */
2099 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2102 case V4L2_XFER_FUNC_709:
2103 if (colorspace == V4L2_COLORSPACE_BT2020 && fmt->fmt.pix.height >= 2160)
2104 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2106 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2108 case V4L2_XFER_FUNC_SRGB:
2109 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2111 case V4L2_XFER_FUNC_ADOBERGB:
2112 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2114 case V4L2_XFER_FUNC_SMPTE240M:
2115 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2117 case V4L2_XFER_FUNC_NONE:
2118 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2120 case V4L2_XFER_FUNC_DEFAULT:
2121 /* nothing, just use defaults for colorspace */
2124 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2125 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2134 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2135 struct v4l2_format *try_fmt)
2137 int fd = v4l2object->video_fd;
2138 struct v4l2_format fmt;
2141 memcpy (&fmt, try_fmt, sizeof (fmt));
2142 r = v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2144 if (r < 0 && errno == ENOTTY) {
2145 /* The driver might not implement TRY_FMT, in which case we will try
2147 if (GST_V4L2_IS_ACTIVE (v4l2object))
2150 memcpy (&fmt, try_fmt, sizeof (fmt));
2151 r = v4l2object->ioctl (fd, VIDIOC_S_FMT, &fmt);
2153 memcpy (try_fmt, &fmt, sizeof (fmt));
2158 memcpy (try_fmt, &fmt, sizeof (fmt));
2159 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2160 "Unable to try format: %s", g_strerror (errno));
2166 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2167 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2169 struct v4l2_format fmt;
2170 GValue interlace_formats = { 0, };
2171 GstVideoInterlaceMode interlace_mode, prev = -1;
2173 const gchar *mode_strings[] = { "progressive",
2178 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2181 if (v4l2object->never_interlaced) {
2182 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2186 g_value_init (&interlace_formats, GST_TYPE_LIST);
2188 /* Try twice - once for NONE, once for INTERLACED. */
2189 memset (&fmt, 0, sizeof (fmt));
2190 fmt.type = v4l2object->type;
2191 fmt.fmt.pix.width = width;
2192 fmt.fmt.pix.height = height;
2193 fmt.fmt.pix.pixelformat = pixelformat;
2194 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2196 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2197 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2198 GValue interlace_enum = { 0, };
2199 g_value_init (&interlace_enum, G_TYPE_STRING);
2200 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2201 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2202 prev = interlace_mode;
2205 memset (&fmt, 0, sizeof (fmt));
2206 fmt.type = v4l2object->type;
2207 fmt.fmt.pix.width = width;
2208 fmt.fmt.pix.height = height;
2209 fmt.fmt.pix.pixelformat = pixelformat;
2210 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2212 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2213 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode) &&
2214 prev != interlace_mode) {
2215 GValue interlace_enum = { 0, };
2216 g_value_init (&interlace_enum, G_TYPE_STRING);
2217 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2218 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2221 if (gst_v4l2src_value_simplify (&interlace_formats)
2222 || gst_value_list_get_size (&interlace_formats) > 0)
2223 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2225 GST_WARNING_OBJECT (v4l2object, "Failed to determine interlace mode");
2231 gst_v4l2_object_fill_colorimetry_list (GValue * list,
2232 GstVideoColorimetry * cinfo)
2234 GValue colorimetry = G_VALUE_INIT;
2237 gboolean found = FALSE;
2239 g_value_init (&colorimetry, G_TYPE_STRING);
2240 g_value_take_string (&colorimetry, gst_video_colorimetry_to_string (cinfo));
2242 /* only insert if no duplicate */
2243 size = gst_value_list_get_size (list);
2244 for (i = 0; i < size; i++) {
2247 tmp = gst_value_list_get_value (list, i);
2248 if (gst_value_compare (&colorimetry, tmp) == GST_VALUE_EQUAL) {
2255 gst_value_list_append_and_take_value (list, &colorimetry);
2257 g_value_unset (&colorimetry);
2261 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2262 guint32 width, guint32 height, guint32 pixelformat)
2264 struct v4l2_format fmt;
2265 GValue list = G_VALUE_INIT;
2266 GstVideoColorimetry cinfo;
2267 enum v4l2_colorspace req_cspace;
2269 memset (&fmt, 0, sizeof (fmt));
2270 fmt.type = v4l2object->type;
2271 fmt.fmt.pix.width = width;
2272 fmt.fmt.pix.height = height;
2273 fmt.fmt.pix.pixelformat = pixelformat;
2275 g_value_init (&list, GST_TYPE_LIST);
2277 /* step 1: get device default colorspace and insert it first as
2278 * it should be the preferred one */
2279 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2280 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2281 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2284 /* step 2: probe all colorspace other than default
2285 * We don't probe all colorspace, range, matrix and transfer combination to
2286 * avoid ioctl flooding which could greatly increase initialization time
2287 * with low-speed devices (UVC...) */
2288 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2289 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++) {
2290 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2291 if (req_cspace == V4L2_COLORSPACE_BT878)
2294 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2295 fmt.fmt.pix_mp.colorspace = req_cspace;
2297 fmt.fmt.pix.colorspace = req_cspace;
2299 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2300 enum v4l2_colorspace colorspace;
2302 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2303 colorspace = fmt.fmt.pix_mp.colorspace;
2305 colorspace = fmt.fmt.pix.colorspace;
2307 if (colorspace == req_cspace) {
2308 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2309 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2314 if (gst_value_list_get_size (&list) > 0)
2315 gst_structure_take_value (s, "colorimetry", &list);
2317 g_value_unset (&list);
2322 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2323 static GstStructure *
2324 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2325 guint32 pixelformat,
2326 guint32 width, guint32 height, const GstStructure * template)
2328 gint fd = v4l2object->video_fd;
2329 struct v4l2_frmivalenum ival;
2332 GValue rates = { 0, };
2334 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2336 ival.pixel_format = pixelformat;
2338 ival.height = height;
2340 GST_LOG_OBJECT (v4l2object->dbg_obj,
2341 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2342 GST_FOURCC_ARGS (pixelformat));
2344 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2345 * fraction to get framerate */
2346 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2347 goto enum_frameintervals_failed;
2349 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2350 GValue rate = { 0, };
2352 g_value_init (&rates, GST_TYPE_LIST);
2353 g_value_init (&rate, GST_TYPE_FRACTION);
2356 num = ival.discrete.numerator;
2357 denom = ival.discrete.denominator;
2359 if (num > G_MAXINT || denom > G_MAXINT) {
2360 /* let us hope we don't get here... */
2365 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding discrete framerate: %d/%d",
2368 /* swap to get the framerate */
2369 gst_value_set_fraction (&rate, denom, num);
2370 gst_value_list_append_value (&rates, &rate);
2373 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2374 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2375 GValue min = { 0, };
2376 GValue step = { 0, };
2377 GValue max = { 0, };
2378 gboolean added = FALSE;
2379 guint32 minnum, mindenom;
2380 guint32 maxnum, maxdenom;
2382 g_value_init (&rates, GST_TYPE_LIST);
2384 g_value_init (&min, GST_TYPE_FRACTION);
2385 g_value_init (&step, GST_TYPE_FRACTION);
2386 g_value_init (&max, GST_TYPE_FRACTION);
2389 minnum = ival.stepwise.min.numerator;
2390 mindenom = ival.stepwise.min.denominator;
2391 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2395 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise min frame interval: %d/%d",
2397 gst_value_set_fraction (&min, minnum, mindenom);
2400 maxnum = ival.stepwise.max.numerator;
2401 maxdenom = ival.stepwise.max.denominator;
2402 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2407 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise max frame interval: %d/%d",
2409 gst_value_set_fraction (&max, maxnum, maxdenom);
2412 num = ival.stepwise.step.numerator;
2413 denom = ival.stepwise.step.denominator;
2414 if (num > G_MAXINT || denom > G_MAXINT) {
2419 if (num == 0 || denom == 0) {
2420 /* in this case we have a wrong fraction or no step, set the step to max
2421 * so that we only add the min value in the loop below */
2426 /* since we only have gst_value_fraction_subtract and not add, negate the
2428 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise step frame interval: %d/%d",
2430 gst_value_set_fraction (&step, -num, denom);
2432 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2433 GValue rate = { 0, };
2435 num = gst_value_get_fraction_numerator (&min);
2436 denom = gst_value_get_fraction_denominator (&min);
2437 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding stepwise framerate: %d/%d",
2440 /* invert to get the framerate */
2441 g_value_init (&rate, GST_TYPE_FRACTION);
2442 gst_value_set_fraction (&rate, denom, num);
2443 gst_value_list_append_value (&rates, &rate);
2446 /* we're actually adding because step was negated above. This is because
2447 * there is no _add function... */
2448 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2449 GST_WARNING_OBJECT (v4l2object->dbg_obj, "could not step fraction!");
2454 /* no range was added, leave the default range from the template */
2455 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2456 "no range added, leaving default");
2457 g_value_unset (&rates);
2459 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2460 guint32 maxnum, maxdenom;
2462 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2464 num = ival.stepwise.min.numerator;
2465 denom = ival.stepwise.min.denominator;
2466 if (num > G_MAXINT || denom > G_MAXINT) {
2471 maxnum = ival.stepwise.max.numerator;
2472 maxdenom = ival.stepwise.max.denominator;
2473 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2478 GST_LOG_OBJECT (v4l2object->dbg_obj,
2479 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2482 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2488 s = gst_structure_copy (template);
2489 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2490 "height", G_TYPE_INT, (gint) height, NULL);
2492 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2494 if (!v4l2object->skip_try_fmt_probes) {
2495 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2497 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2500 if (G_IS_VALUE (&rates)) {
2501 gst_v4l2src_value_simplify (&rates);
2502 /* only change the framerate on the template when we have a valid probed new
2504 gst_structure_take_value (s, "framerate", &rates);
2505 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2506 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2507 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2513 enum_frameintervals_failed:
2515 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2516 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2517 GST_FOURCC_ARGS (pixelformat), width, height);
2522 /* I don't see how this is actually an error, we ignore the format then */
2523 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2524 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2525 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2531 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2535 gst_structure_get_int (s1, "width", &w1);
2536 gst_structure_get_int (s1, "height", &h1);
2537 gst_structure_get_int (s2, "width", &w2);
2538 gst_structure_get_int (s2, "height", &h2);
2540 /* I think it's safe to assume that this won't overflow for a while */
2541 return ((w2 * h2) - (w1 * h1));
2545 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2546 guint32 format, GstCaps * caps, GstStructure * s)
2548 GstStructure *alt_s = NULL;
2550 /* Encoded stream on output buffer need to be parsed */
2551 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2552 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2555 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2556 if (format == gst_v4l2_formats[i].format &&
2557 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2558 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2559 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2565 if (v4l2object->has_alpha_component &&
2566 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2567 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2569 case V4L2_PIX_FMT_RGB32:
2570 alt_s = gst_structure_copy (s);
2571 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2573 case V4L2_PIX_FMT_BGR32:
2574 alt_s = gst_structure_copy (s);
2575 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2582 gst_caps_append_structure (caps, s);
2585 gst_caps_append_structure (caps, alt_s);
2589 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2590 guint32 pixelformat, const GstStructure * template)
2592 GstCaps *ret = gst_caps_new_empty ();
2594 gint fd = v4l2object->video_fd;
2595 struct v4l2_frmsizeenum size;
2596 GList *results = NULL;
2599 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2600 gst_caps_append_structure (ret, gst_structure_copy (template));
2604 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2606 size.pixel_format = pixelformat;
2608 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2609 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2610 GST_FOURCC_ARGS (pixelformat));
2612 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2613 goto enum_framesizes_failed;
2615 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2617 GST_LOG_OBJECT (v4l2object->dbg_obj, "got discrete frame size %dx%d",
2618 size.discrete.width, size.discrete.height);
2620 w = MIN (size.discrete.width, G_MAXINT);
2621 h = MIN (size.discrete.height, G_MAXINT);
2625 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2626 pixelformat, w, h, template);
2629 results = g_list_prepend (results, tmp);
2633 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2634 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2635 "done iterating discrete frame sizes");
2636 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2637 guint32 maxw, maxh, step_w, step_h;
2639 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have stepwise frame sizes:");
2640 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2641 size.stepwise.min_width);
2642 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2643 size.stepwise.min_height);
2644 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2645 size.stepwise.max_width);
2646 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2647 size.stepwise.max_height);
2648 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step width: %d",
2649 size.stepwise.step_width);
2650 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step height: %d",
2651 size.stepwise.step_height);
2653 w = MAX (size.stepwise.min_width, 1);
2654 h = MAX (size.stepwise.min_height, 1);
2655 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2656 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2658 step_w = MAX (size.stepwise.step_width, 1);
2659 step_h = MAX (size.stepwise.step_height, 1);
2661 /* FIXME: check for sanity and that min/max are multiples of the steps */
2663 /* we only query details for the max width/height since it's likely the
2664 * most restricted if there are any resolution-dependent restrictions */
2665 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2666 pixelformat, maxw, maxh, template);
2669 GValue step_range = G_VALUE_INIT;
2671 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2672 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2673 gst_structure_set_value (tmp, "width", &step_range);
2675 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2676 gst_structure_take_value (tmp, "height", &step_range);
2678 /* no point using the results list here, since there's only one struct */
2679 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2681 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2684 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have continuous frame sizes:");
2685 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2686 size.stepwise.min_width);
2687 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2688 size.stepwise.min_height);
2689 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2690 size.stepwise.max_width);
2691 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2692 size.stepwise.max_height);
2694 w = MAX (size.stepwise.min_width, 1);
2695 h = MAX (size.stepwise.min_height, 1);
2696 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2697 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2700 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2703 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2704 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2707 /* no point using the results list here, since there's only one struct */
2708 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2714 /* we use an intermediary list to store and then sort the results of the
2715 * probing because we can't make any assumptions about the order in which
2716 * the driver will give us the sizes, but we want the final caps to contain
2717 * the results starting with the highest resolution and having the lowest
2718 * resolution last, since order in caps matters for things like fixation. */
2719 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2720 while (results != NULL) {
2721 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
2723 results = g_list_delete_link (results, results);
2726 if (gst_caps_is_empty (ret))
2727 goto enum_framesizes_no_results;
2732 enum_framesizes_failed:
2734 /* I don't see how this is actually an error */
2735 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2736 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2737 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2738 goto default_frame_sizes;
2740 enum_framesizes_no_results:
2742 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2743 * question doesn't actually support it yet */
2744 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2745 "No results for pixelformat %" GST_FOURCC_FORMAT
2746 " enumerating frame sizes, trying fallback",
2747 GST_FOURCC_ARGS (pixelformat));
2748 goto default_frame_sizes;
2752 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2753 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2754 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2755 goto default_frame_sizes;
2758 default_frame_sizes:
2760 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2762 /* This code is for Linux < 2.6.19 */
2764 max_w = max_h = GST_V4L2_MAX_SIZE;
2765 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2767 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2768 "Could not probe minimum capture size for pixelformat %"
2769 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2771 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2773 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2774 "Could not probe maximum capture size for pixelformat %"
2775 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2778 /* Since we can't get framerate directly, try to use the current norm */
2779 if (v4l2object->tv_norm && v4l2object->norms) {
2781 GstTunerNorm *norm = NULL;
2782 GstTunerNorm *current =
2783 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2785 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2786 norm = (GstTunerNorm *) norms->data;
2787 if (!strcmp (norm->label, current->label))
2790 /* If it's possible, set framerate to that (discrete) value */
2792 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2793 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2797 tmp = gst_structure_copy (template);
2799 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2801 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2802 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2803 /* if norm can't be used, copy the template framerate */
2804 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2809 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2811 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2814 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2816 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2818 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2820 if (!v4l2object->skip_try_fmt_probes) {
2821 /* We could consider setting interlace mode from min and max. */
2822 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
2824 /* We could consider to check colorspace for min too, in case it depends on
2825 * the size. But in this case, min and max could not be enough */
2826 gst_v4l2_object_add_colorspace (v4l2object, tmp, max_w, max_h,
2830 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2836 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2837 guint32 pixelformat, gint * width, gint * height)
2839 struct v4l2_format fmt;
2840 gboolean ret = FALSE;
2841 GstVideoInterlaceMode interlace_mode;
2843 g_return_val_if_fail (width != NULL, FALSE);
2844 g_return_val_if_fail (height != NULL, FALSE);
2846 GST_LOG_OBJECT (v4l2object->dbg_obj,
2847 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2848 *width, *height, GST_FOURCC_ARGS (pixelformat));
2850 memset (&fmt, 0, sizeof (struct v4l2_format));
2852 /* get size delimiters */
2853 memset (&fmt, 0, sizeof (fmt));
2854 fmt.type = v4l2object->type;
2855 fmt.fmt.pix.width = *width;
2856 fmt.fmt.pix.height = *height;
2857 fmt.fmt.pix.pixelformat = pixelformat;
2858 fmt.fmt.pix.field = V4L2_FIELD_ANY;
2860 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
2863 GST_LOG_OBJECT (v4l2object->dbg_obj,
2864 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2866 *width = fmt.fmt.pix.width;
2867 *height = fmt.fmt.pix.height;
2869 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2870 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2871 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
2872 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
2880 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2881 "Unable to try format: %s", g_strerror (errno));
2888 gst_v4l2_object_is_dmabuf_supported (GstV4l2Object * v4l2object)
2890 gboolean ret = TRUE;
2891 struct v4l2_exportbuffer expbuf = {
2892 .type = v4l2object->type,
2895 .flags = O_CLOEXEC | O_RDWR,
2898 if (v4l2object->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
2899 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2900 "libv4l2 converter detected, disabling DMABuf");
2904 /* Expected to fail, but ENOTTY tells us that it is not implemented. */
2905 v4l2object->ioctl (v4l2object->video_fd, VIDIOC_EXPBUF, &expbuf);
2906 if (errno == ENOTTY)
2913 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2917 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "initializing the %s system",
2918 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
2920 GST_V4L2_CHECK_OPEN (v4l2object);
2921 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2923 /* find transport */
2924 mode = v4l2object->req_mode;
2926 if (v4l2object->device_caps & V4L2_CAP_READWRITE) {
2927 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2928 mode = GST_V4L2_IO_RW;
2929 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2930 goto method_not_supported;
2932 if (v4l2object->device_caps & V4L2_CAP_STREAMING) {
2933 if (v4l2object->req_mode == GST_V4L2_IO_AUTO) {
2934 if (!V4L2_TYPE_IS_OUTPUT (v4l2object->type) &&
2935 gst_v4l2_object_is_dmabuf_supported (v4l2object)) {
2936 mode = GST_V4L2_IO_DMABUF;
2938 mode = GST_V4L2_IO_MMAP;
2941 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP ||
2942 v4l2object->req_mode == GST_V4L2_IO_DMABUF)
2943 goto method_not_supported;
2945 /* if still no transport selected, error out */
2946 if (mode == GST_V4L2_IO_AUTO)
2947 goto no_supported_capture_method;
2949 GST_INFO_OBJECT (v4l2object->dbg_obj, "accessing buffers via mode %d", mode);
2950 v4l2object->mode = mode;
2952 /* If min_buffers is not set, the driver either does not support the control or
2953 it has not been asked yet via propose_allocation/decide_allocation. */
2954 if (!v4l2object->min_buffers)
2955 gst_v4l2_get_driver_min_buffers (v4l2object);
2957 /* Map the buffers */
2958 GST_LOG_OBJECT (v4l2object->dbg_obj, "initiating buffer pool");
2960 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2961 goto buffer_pool_new_failed;
2963 GST_V4L2_SET_ACTIVE (v4l2object);
2968 buffer_pool_new_failed:
2970 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2971 (_("Could not map buffers from device '%s'"),
2972 v4l2object->videodev),
2973 ("Failed to create buffer pool: %s", g_strerror (errno)));
2976 method_not_supported:
2978 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2979 (_("The driver of device '%s' does not support the IO method %d"),
2980 v4l2object->videodev, mode), (NULL));
2983 no_supported_capture_method:
2985 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2986 (_("The driver of device '%s' does not support any known IO "
2987 "method."), v4l2object->videodev), (NULL));
2993 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
2994 gint plane, gint stride)
2996 const GstVideoFormatInfo *finfo = info->finfo;
2998 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2999 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
3002 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
3003 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
3004 tile_height = 1 << hs;
3006 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
3007 info->height + align->padding_top + align->padding_bottom);
3008 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3010 x_tiles = stride >> ws;
3011 y_tiles = padded_height >> hs;
3012 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
3014 info->stride[plane] = stride;
3019 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
3020 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
3022 const GstVideoFormatInfo *finfo = info->finfo;
3023 gint i, estride, padded_height;
3026 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
3028 padded_height = info->height + align->padding_top + align->padding_bottom;
3030 for (i = 0; i < finfo->n_planes; i++) {
3031 estride = gst_v4l2_object_extrapolate_stride (finfo, i, stride);
3033 gst_v4l2_object_set_stride (info, align, i, estride);
3035 info->offset[i] = offs;
3037 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
3039 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3040 "Extrapolated for plane %d with base stride %d: "
3041 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
3045 /* Update the image size according the amount of data we are going to
3046 * read/write. This workaround bugs in driver where the sizeimage provided
3047 * by TRY/S_FMT represent the buffer length (maximum size) rather then the expected
3048 * bytesused (buffer size). */
3049 if (offs < info->size)
3054 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
3055 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
3056 GstVideoInfo * info, GstVideoAlignment * align)
3058 const GstVideoFormatInfo *finfo = info->finfo;
3059 gboolean standard_stride = TRUE;
3060 gint stride, pstride, padded_width, padded_height, i;
3062 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
3063 v4l2object->n_v4l2_planes = 1;
3064 info->size = format->fmt.pix.sizeimage;
3068 /* adjust right padding */
3069 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
3070 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3072 stride = format->fmt.pix.bytesperline;
3074 pstride = GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
3076 padded_width = stride / pstride;
3078 /* pstride can be 0 for complex formats */
3079 GST_WARNING_OBJECT (v4l2object->element,
3080 "format %s has a pstride of 0, cannot compute padded with",
3081 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
3082 padded_width = stride;
3085 if (padded_width < format->fmt.pix.width)
3086 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3087 "Driver bug detected, stride (%d) is too small for the width (%d)",
3088 padded_width, format->fmt.pix.width);
3090 align->padding_right = padded_width - info->width - align->padding_left;
3092 /* adjust bottom padding */
3093 padded_height = format->fmt.pix.height;
3095 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3096 guint hs, tile_height;
3098 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
3099 tile_height = 1 << hs;
3101 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3104 align->padding_bottom = padded_height - info->height - align->padding_top;
3106 /* setup the strides and offset */
3107 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
3108 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
3110 /* figure out the frame layout */
3111 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
3113 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
3114 stride = pix_mp->plane_fmt[i].bytesperline;
3116 if (info->stride[i] != stride)
3117 standard_stride = FALSE;
3119 gst_v4l2_object_set_stride (info, align, i, stride);
3120 info->offset[i] = info->size;
3121 info->size += pix_mp->plane_fmt[i].sizeimage;
3124 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3125 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
3126 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3127 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3130 /* only one plane in non-MPLANE mode */
3131 v4l2object->n_v4l2_planes = 1;
3132 info->size = format->fmt.pix.sizeimage;
3133 stride = format->fmt.pix.bytesperline;
3135 if (info->stride[0] != stride)
3136 standard_stride = FALSE;
3138 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3141 /* adjust the offset to take into account left and top */
3142 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3143 if ((align->padding_left + align->padding_top) > 0)
3144 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3145 "Left and top padding is not permitted for tiled formats");
3147 for (i = 0; i < finfo->n_planes; i++) {
3150 /* FIXME we assume plane as component as this is true for all supported
3151 * format we support. */
3153 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left);
3154 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top);
3156 info->offset[i] += (vedge * info->stride[i]) +
3157 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i));
3162 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got sizeimage %" G_GSIZE_FORMAT,
3165 /* to avoid copies we need video meta if there is padding */
3166 v4l2object->need_video_meta =
3167 ((align->padding_top + align->padding_left + align->padding_right +
3168 align->padding_bottom) != 0);
3170 /* ... or if stride is non "standard" */
3171 if (!standard_stride)
3172 v4l2object->need_video_meta = TRUE;
3174 /* ... or also video meta if we use multiple, non-contiguous, planes */
3175 if (v4l2object->n_v4l2_planes > 1)
3176 v4l2object->need_video_meta = TRUE;
3178 v4l2object->info = *info;
3179 v4l2object->align = *align;
3180 v4l2object->format = *format;
3181 v4l2object->fmtdesc = fmtdesc;
3183 /* if we have a framerate pre-calculate duration */
3184 if (info->fps_n > 0 && info->fps_d > 0) {
3185 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
3188 v4l2object->duration = GST_CLOCK_TIME_NONE;
3193 gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
3194 gint plane, gint stride)
3198 switch (finfo->format) {
3199 case GST_VIDEO_FORMAT_NV12:
3200 case GST_VIDEO_FORMAT_NV12_64Z32:
3201 case GST_VIDEO_FORMAT_NV21:
3202 case GST_VIDEO_FORMAT_NV16:
3203 case GST_VIDEO_FORMAT_NV61:
3204 case GST_VIDEO_FORMAT_NV24:
3205 estride = (plane == 0 ? 1 : 2) *
3206 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3209 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3217 gst_v4l2_video_colorimetry_matches (const GstVideoColorimetry * cinfo,
3218 const gchar * color)
3220 GstVideoColorimetry ci;
3221 static const GstVideoColorimetry ci_likely_jpeg = {
3222 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3223 GST_VIDEO_TRANSFER_UNKNOWN, GST_VIDEO_COLOR_PRIMARIES_UNKNOWN
3225 static const GstVideoColorimetry ci_jpeg = {
3226 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3227 GST_VIDEO_TRANSFER_SRGB, GST_VIDEO_COLOR_PRIMARIES_BT709
3230 if (!gst_video_colorimetry_from_string (&ci, color))
3233 if (gst_video_colorimetry_is_equal (&ci, cinfo))
3236 /* Allow 1:4:0:0 (produced by jpegdec) if the device expects 1:4:7:1 */
3237 if (gst_video_colorimetry_is_equal (&ci, &ci_likely_jpeg)
3238 && gst_video_colorimetry_is_equal (cinfo, &ci_jpeg))
3245 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3246 gboolean try_only, GstV4l2Error * error)
3248 gint fd = v4l2object->video_fd;
3249 struct v4l2_format format;
3250 struct v4l2_streamparm streamparm;
3251 enum v4l2_field field;
3252 guint32 pixelformat;
3253 struct v4l2_fmtdesc *fmtdesc;
3255 GstVideoAlignment align;
3256 gint width, height, fps_n, fps_d;
3260 enum v4l2_colorspace colorspace = 0;
3261 enum v4l2_quantization range = 0;
3262 enum v4l2_ycbcr_encoding matrix = 0;
3263 enum v4l2_xfer_func transfer = 0;
3266 g_return_val_if_fail (!v4l2object->skip_try_fmt_probes ||
3267 gst_caps_is_writable (caps), FALSE);
3269 GST_V4L2_CHECK_OPEN (v4l2object);
3271 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3273 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3275 gst_video_info_init (&info);
3276 gst_video_alignment_reset (&align);
3278 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3281 pixelformat = fmtdesc->pixelformat;
3282 width = GST_VIDEO_INFO_WIDTH (&info);
3283 height = GST_VIDEO_INFO_HEIGHT (&info);
3284 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3285 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3287 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3288 * or if contiguous is prefered */
3289 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3290 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3293 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
3294 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "interlaced video");
3295 /* ideally we would differentiate between types of interlaced video
3296 * but there is not sufficient information in the caps..
3298 field = V4L2_FIELD_INTERLACED;
3300 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "progressive video");
3301 field = V4L2_FIELD_NONE;
3304 /* We first pick the main colorspace from the primaries */
3305 switch (info.colorimetry.primaries) {
3306 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3307 /* There is two colorspaces using these primaries, use the range to
3309 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3310 colorspace = V4L2_COLORSPACE_REC709;
3312 colorspace = V4L2_COLORSPACE_SRGB;
3314 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
3315 colorspace = V4L2_COLORSPACE_BT2020;
3317 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3318 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3320 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3321 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3323 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3324 colorspace = V4L2_COLORSPACE_SMPTE170M;
3326 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3327 colorspace = V4L2_COLORSPACE_SMPTE240M;
3330 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3331 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3332 /* We don't know, we will guess */
3336 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3337 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3341 switch (info.colorimetry.range) {
3342 case GST_VIDEO_COLOR_RANGE_0_255:
3343 range = V4L2_QUANTIZATION_FULL_RANGE;
3345 case GST_VIDEO_COLOR_RANGE_16_235:
3346 range = V4L2_QUANTIZATION_LIM_RANGE;
3348 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3349 /* We let the driver pick a default one */
3352 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3353 "Unknown colorimetry range %d", info.colorimetry.range);
3357 switch (info.colorimetry.matrix) {
3358 case GST_VIDEO_COLOR_MATRIX_RGB:
3359 /* Unspecified, leave to default */
3361 /* FCC is about the same as BT601 with less digit */
3362 case GST_VIDEO_COLOR_MATRIX_FCC:
3363 case GST_VIDEO_COLOR_MATRIX_BT601:
3364 matrix = V4L2_YCBCR_ENC_601;
3366 case GST_VIDEO_COLOR_MATRIX_BT709:
3367 matrix = V4L2_YCBCR_ENC_709;
3369 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3370 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3372 case GST_VIDEO_COLOR_MATRIX_BT2020:
3373 matrix = V4L2_YCBCR_ENC_BT2020;
3375 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3376 /* We let the driver pick a default one */
3379 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3380 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3384 switch (info.colorimetry.transfer) {
3385 case GST_VIDEO_TRANSFER_GAMMA18:
3386 case GST_VIDEO_TRANSFER_GAMMA20:
3387 case GST_VIDEO_TRANSFER_GAMMA22:
3388 case GST_VIDEO_TRANSFER_GAMMA28:
3389 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3390 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3392 case GST_VIDEO_TRANSFER_GAMMA10:
3393 transfer = V4L2_XFER_FUNC_NONE;
3395 case GST_VIDEO_TRANSFER_BT2020_12:
3396 case GST_VIDEO_TRANSFER_BT709:
3397 transfer = V4L2_XFER_FUNC_709;
3399 case GST_VIDEO_TRANSFER_SMPTE240M:
3400 transfer = V4L2_XFER_FUNC_SMPTE240M;
3402 case GST_VIDEO_TRANSFER_SRGB:
3403 transfer = V4L2_XFER_FUNC_SRGB;
3405 case GST_VIDEO_TRANSFER_LOG100:
3406 case GST_VIDEO_TRANSFER_LOG316:
3407 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3408 "LOG 100, 316 transfer functions not supported");
3409 /* FIXME No known sensible default, maybe AdobeRGB ? */
3411 case GST_VIDEO_TRANSFER_UNKNOWN:
3412 /* We let the driver pick a default one */
3415 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3416 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
3420 if (colorspace == 0) {
3421 /* Try to guess colorspace according to pixelformat and size */
3422 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3423 if (range == V4L2_QUANTIZATION_FULL_RANGE
3424 && matrix == V4L2_YCBCR_ENC_601 && transfer == 0) {
3425 /* Full range BT.601 YCbCr encoding with unknown primaries and transfer
3426 * function most likely is JPEG */
3427 colorspace = V4L2_COLORSPACE_JPEG;
3428 transfer = V4L2_XFER_FUNC_SRGB;
3430 /* SD streams likely use SMPTE170M and HD streams REC709 */
3431 if (width <= 720 && height <= 576)
3432 colorspace = V4L2_COLORSPACE_SMPTE170M;
3434 colorspace = V4L2_COLORSPACE_REC709;
3436 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3437 colorspace = V4L2_COLORSPACE_SRGB;
3438 transfer = V4L2_XFER_FUNC_NONE;
3442 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format %dx%d, format "
3443 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3444 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3446 memset (&format, 0x00, sizeof (struct v4l2_format));
3447 format.type = v4l2object->type;
3450 format.type = v4l2object->type;
3451 format.fmt.pix_mp.pixelformat = pixelformat;
3452 format.fmt.pix_mp.width = width;
3453 format.fmt.pix_mp.height = height;
3454 format.fmt.pix_mp.field = field;
3455 format.fmt.pix_mp.num_planes = n_v4l_planes;
3457 /* try to ask our prefered stride but it's not a failure if not
3459 for (i = 0; i < n_v4l_planes; i++) {
3460 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3462 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3463 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3464 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3466 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3469 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3470 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3472 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3474 format.type = v4l2object->type;
3476 format.fmt.pix.width = width;
3477 format.fmt.pix.height = height;
3478 format.fmt.pix.pixelformat = pixelformat;
3479 format.fmt.pix.field = field;
3481 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3482 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3483 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3485 /* try to ask our prefered stride */
3486 format.fmt.pix.bytesperline = stride;
3488 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3489 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3492 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format is %dx%d, format "
3493 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3494 format.fmt.pix_mp.height,
3495 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3496 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3498 #ifndef GST_DISABLE_GST_DEBUG
3500 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3501 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3502 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3504 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3505 format.fmt.pix.bytesperline);
3510 format.fmt.pix_mp.colorspace = colorspace;
3511 format.fmt.pix_mp.quantization = range;
3512 format.fmt.pix_mp.ycbcr_enc = matrix;
3513 format.fmt.pix_mp.xfer_func = transfer;
3515 format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
3516 format.fmt.pix.colorspace = colorspace;
3517 format.fmt.pix.quantization = range;
3518 format.fmt.pix.ycbcr_enc = matrix;
3519 format.fmt.pix.xfer_func = transfer;
3522 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired colorspace is %d:%d:%d:%d",
3523 colorspace, range, matrix, transfer);
3526 if (v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3527 goto try_fmt_failed;
3529 if (v4l2object->ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3530 goto set_fmt_failed;
3534 colorspace = format.fmt.pix_mp.colorspace;
3535 range = format.fmt.pix_mp.quantization;
3536 matrix = format.fmt.pix_mp.ycbcr_enc;
3537 transfer = format.fmt.pix_mp.xfer_func;
3539 colorspace = format.fmt.pix.colorspace;
3540 range = format.fmt.pix.quantization;
3541 matrix = format.fmt.pix.ycbcr_enc;
3542 transfer = format.fmt.pix.xfer_func;
3545 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got format of %dx%d, format "
3546 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d:%d:%d:%d",
3547 format.fmt.pix.width, format.fmt.pix_mp.height,
3548 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3549 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3550 colorspace, range, matrix, transfer);
3552 #ifndef GST_DISABLE_GST_DEBUG
3554 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3555 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3556 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3557 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3559 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3560 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3564 if (format.fmt.pix.pixelformat != pixelformat)
3565 goto invalid_pixelformat;
3567 /* Only negotiate size with raw data.
3568 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3569 * in ASF mode for example, there is also not reason for a driver to
3570 * change the size. */
3571 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3572 /* We can crop larger images */
3573 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3574 goto invalid_dimensions;
3576 /* Note, this will be adjusted if upstream has non-centered cropping. */
3577 align.padding_top = 0;
3578 align.padding_bottom = format.fmt.pix.height - height;
3579 align.padding_left = 0;
3580 align.padding_right = format.fmt.pix.width - width;
3583 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3584 goto invalid_planes;
3586 /* used to check colorimetry and interlace mode fields presence */
3587 s = gst_caps_get_structure (caps, 0);
3589 if (!gst_v4l2_object_get_interlace_mode (format.fmt.pix.field,
3590 &info.interlace_mode))
3592 if (gst_structure_has_field (s, "interlace-mode")) {
3593 if (format.fmt.pix.field != field)
3597 gst_v4l2_object_get_colorspace (&format, &info.colorimetry);
3598 if (gst_structure_has_field (s, "colorimetry")) {
3599 if (!gst_v4l2_video_colorimetry_matches (&info.colorimetry,
3600 gst_structure_get_string (s, "colorimetry")))
3601 goto invalid_colorimetry;
3604 /* In case we have skipped the try_fmt probes, we'll need to set the
3605 * colorimetry and interlace-mode back into the caps. */
3606 if (v4l2object->skip_try_fmt_probes) {
3607 if (!gst_structure_has_field (s, "colorimetry")) {
3608 gchar *str = gst_video_colorimetry_to_string (&info.colorimetry);
3609 gst_structure_set (s, "colorimetry", G_TYPE_STRING, str, NULL);
3613 if (!gst_structure_has_field (s, "interlace-mode"))
3614 gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
3615 gst_video_interlace_mode_to_string (info.interlace_mode), NULL);
3618 if (try_only) /* good enough for trying only */
3621 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
3622 struct v4l2_control ctl = { 0, };
3623 ctl.id = V4L2_CID_ALPHA_COMPONENT;
3626 if (v4l2object->ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
3627 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3628 "Failed to set alpha component value");
3631 /* Is there a reason we require the caller to always specify a framerate? */
3632 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired framerate: %u/%u", fps_n,
3635 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
3636 streamparm.type = v4l2object->type;
3638 if (v4l2object->ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
3639 goto get_parm_failed;
3641 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
3642 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3643 GST_VIDEO_INFO_FPS_N (&info) =
3644 streamparm.parm.capture.timeperframe.denominator;
3645 GST_VIDEO_INFO_FPS_D (&info) =
3646 streamparm.parm.capture.timeperframe.numerator;
3648 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got capture framerate: %u/%u",
3649 streamparm.parm.capture.timeperframe.denominator,
3650 streamparm.parm.capture.timeperframe.numerator);
3652 /* We used to skip frame rate setup if the camera was already setup
3653 * with the requested frame rate. This breaks some cameras though,
3654 * causing them to not output data (several models of Thinkpad cameras
3655 * have this problem at least).
3656 * So, don't skip. */
3657 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting capture framerate to %u/%u",
3659 /* We want to change the frame rate, so check whether we can. Some cheap USB
3660 * cameras don't have the capability */
3661 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3662 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3663 "Not setting capture framerate (not supported)");
3667 /* Note: V4L2 wants the frame interval, we have the frame rate */
3668 streamparm.parm.capture.timeperframe.numerator = fps_d;
3669 streamparm.parm.capture.timeperframe.denominator = fps_n;
3671 /* some cheap USB cam's won't accept any change */
3672 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3673 goto set_parm_failed;
3675 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
3676 streamparm.parm.capture.timeperframe.denominator > 0) {
3677 /* get new values */
3678 fps_d = streamparm.parm.capture.timeperframe.numerator;
3679 fps_n = streamparm.parm.capture.timeperframe.denominator;
3681 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set capture framerate to %u/%u",
3684 /* fix v4l2 capture driver to provide framerate values */
3685 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3686 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
3689 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3690 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3691 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
3692 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
3693 GST_VIDEO_INFO_FPS_N (&info) =
3694 streamparm.parm.output.timeperframe.denominator;
3695 GST_VIDEO_INFO_FPS_D (&info) =
3696 streamparm.parm.output.timeperframe.numerator;
3698 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got output framerate: %u/%u",
3699 streamparm.parm.output.timeperframe.denominator,
3700 streamparm.parm.output.timeperframe.numerator);
3702 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting output framerate to %u/%u",
3704 if ((streamparm.parm.output.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3705 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3706 "Not setting output framerate (not supported)");
3710 /* Note: V4L2 wants the frame interval, we have the frame rate */
3711 streamparm.parm.output.timeperframe.numerator = fps_d;
3712 streamparm.parm.output.timeperframe.denominator = fps_n;
3714 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3715 goto set_parm_failed;
3717 if (streamparm.parm.output.timeperframe.numerator > 0 &&
3718 streamparm.parm.output.timeperframe.denominator > 0) {
3719 /* get new values */
3720 fps_d = streamparm.parm.output.timeperframe.numerator;
3721 fps_n = streamparm.parm.output.timeperframe.denominator;
3723 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set output framerate to %u/%u",
3726 /* fix v4l2 output driver to provide framerate values */
3727 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3728 "Reuse caps framerate %u/%u - fix v4l2 output driver", fps_n, fps_d);
3731 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3732 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3736 /* add boolean return, so we can fail on drivers bugs */
3737 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
3739 /* now configure the pool */
3740 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
3748 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "can't parse caps %" GST_PTR_FORMAT,
3754 if (errno == EINVAL) {
3755 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3756 (_("Device '%s' has no supported format"), v4l2object->videodev),
3757 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3758 GST_FOURCC_ARGS (pixelformat), width, height,
3759 g_strerror (errno)));
3761 GST_V4L2_ERROR (error, RESOURCE, FAILED,
3762 (_("Device '%s' failed during initialization"),
3763 v4l2object->videodev),
3764 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3765 GST_FOURCC_ARGS (pixelformat), width, height,
3766 g_strerror (errno)));
3772 if (errno == EBUSY) {
3773 GST_V4L2_ERROR (error, RESOURCE, BUSY,
3774 (_("Device '%s' is busy"), v4l2object->videodev),
3775 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3776 GST_FOURCC_ARGS (pixelformat), width, height,
3777 g_strerror (errno)));
3778 } else if (errno == EINVAL) {
3779 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3780 (_("Device '%s' has no supported format"), v4l2object->videodev),
3781 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3782 GST_FOURCC_ARGS (pixelformat), width, height,
3783 g_strerror (errno)));
3785 GST_V4L2_ERROR (error, RESOURCE, FAILED,
3786 (_("Device '%s' failed during initialization"),
3787 v4l2object->videodev),
3788 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3789 GST_FOURCC_ARGS (pixelformat), width, height,
3790 g_strerror (errno)));
3796 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3797 (_("Device '%s' cannot capture at %dx%d"),
3798 v4l2object->videodev, width, height),
3799 ("Tried to capture at %dx%d, but device returned size %dx%d",
3800 width, height, format.fmt.pix.width, format.fmt.pix.height));
3803 invalid_pixelformat:
3805 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3806 (_("Device '%s' cannot capture in the specified format"),
3807 v4l2object->videodev),
3808 ("Tried to capture in %" GST_FOURCC_FORMAT
3809 ", but device returned format" " %" GST_FOURCC_FORMAT,
3810 GST_FOURCC_ARGS (pixelformat),
3811 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
3816 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3817 (_("Device '%s' does support non-contiguous planes"),
3818 v4l2object->videodev),
3819 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
3824 enum v4l2_field wanted_field;
3827 wanted_field = format.fmt.pix_mp.field;
3829 wanted_field = format.fmt.pix.field;
3831 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3832 (_("Device '%s' does not support %s interlacing"),
3833 v4l2object->videodev,
3834 field == V4L2_FIELD_NONE ? "progressive" : "interleaved"),
3835 ("Device wants %s interlacing",
3836 wanted_field == V4L2_FIELD_NONE ? "progressive" : "interleaved"));
3839 invalid_colorimetry:
3841 gchar *wanted_colorimetry;
3843 wanted_colorimetry = gst_video_colorimetry_to_string (&info.colorimetry);
3845 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3846 (_("Device '%s' does not support %s colorimetry"),
3847 v4l2object->videodev, gst_structure_get_string (s, "colorimetry")),
3848 ("Device wants %s colorimetry", wanted_colorimetry));
3850 g_free (wanted_colorimetry);
3855 /* it's possible that this call is not supported */
3856 if (errno != EINVAL && errno != ENOTTY) {
3857 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3858 (_("Could not get parameters on device '%s'"),
3859 v4l2object->videodev), GST_ERROR_SYSTEM);
3865 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3866 (_("Video device did not accept new frame rate setting.")),
3872 /* setup_pool already send the error */
3878 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps,
3879 GstV4l2Error * error)
3881 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Setting format to %" GST_PTR_FORMAT,
3883 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE, error);
3887 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps,
3888 GstV4l2Error * error)
3890 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying format %" GST_PTR_FORMAT,
3892 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE, error);
3896 * gst_v4l2_object_acquire_format:
3897 * @v4l2object the object
3898 * @info a GstVideoInfo to be filled
3900 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
3901 * the output format is choosen by the HW.
3903 * Returns: %TRUE on success, %FALSE on failure.
3906 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
3908 struct v4l2_fmtdesc *fmtdesc;
3909 struct v4l2_format fmt;
3910 struct v4l2_crop crop;
3911 struct v4l2_selection sel;
3912 struct v4l2_rect *r = NULL;
3913 GstVideoFormat format;
3914 guint width, height;
3915 GstVideoAlignment align;
3917 gst_video_info_init (info);
3918 gst_video_alignment_reset (&align);
3920 memset (&fmt, 0x00, sizeof (struct v4l2_format));
3921 fmt.type = v4l2object->type;
3922 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
3923 goto get_fmt_failed;
3925 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
3926 fmt.fmt.pix.pixelformat);
3927 if (fmtdesc == NULL)
3928 goto unsupported_format;
3930 /* No need to care about mplane, the four first params are the same */
3931 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
3933 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
3934 if (format == GST_VIDEO_FORMAT_UNKNOWN)
3935 goto unsupported_format;
3937 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
3938 goto invalid_dimensions;
3940 width = fmt.fmt.pix.width;
3941 height = fmt.fmt.pix.height;
3943 /* Use the default compose rectangle */
3944 memset (&sel, 0, sizeof (struct v4l2_selection));
3945 sel.type = v4l2object->type;
3946 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
3947 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0) {
3950 /* For ancient kernels, fall back to G_CROP */
3951 memset (&crop, 0, sizeof (struct v4l2_crop));
3952 crop.type = v4l2object->type;
3953 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
3957 align.padding_left = r->left;
3958 align.padding_top = r->top;
3959 align.padding_right = width - r->width - r->left;
3960 align.padding_bottom = height - r->height - r->top;
3965 gst_video_info_set_format (info, format, width, height);
3967 switch (fmt.fmt.pix.field) {
3968 case V4L2_FIELD_ANY:
3969 case V4L2_FIELD_NONE:
3970 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
3972 case V4L2_FIELD_INTERLACED:
3973 case V4L2_FIELD_INTERLACED_TB:
3974 case V4L2_FIELD_INTERLACED_BT:
3975 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
3978 goto unsupported_field;
3981 gst_v4l2_object_get_colorspace (&fmt, &info->colorimetry);
3983 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
3985 /* Shall we setup the pool ? */
3991 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3992 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
3997 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3998 (_("Video device returned invalid dimensions.")),
3999 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
4000 fmt.fmt.pix.height));
4005 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
4006 (_("Video device uses an unsupported interlacing method.")),
4007 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
4012 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
4013 (_("Video device uses an unsupported pixel format.")),
4014 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
4015 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
4021 gst_v4l2_object_set_crop (GstV4l2Object * obj)
4023 struct v4l2_selection sel = { 0 };
4024 struct v4l2_crop crop = { 0 };
4026 sel.type = obj->type;
4027 sel.target = V4L2_SEL_TGT_CROP;
4029 sel.r.left = obj->align.padding_left;
4030 sel.r.top = obj->align.padding_top;
4031 sel.r.width = obj->info.width;
4032 sel.r.height = obj->info.height;
4034 crop.type = obj->type;
4037 if (obj->align.padding_left + obj->align.padding_top +
4038 obj->align.padding_right + obj->align.padding_bottom == 0) {
4039 GST_DEBUG_OBJECT (obj->dbg_obj, "no cropping needed");
4043 GST_DEBUG_OBJECT (obj->dbg_obj,
4044 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4045 crop.c.width, crop.c.height);
4047 if (obj->ioctl (obj->video_fd, VIDIOC_S_SELECTION, &sel) < 0) {
4048 if (errno != ENOTTY) {
4049 GST_WARNING_OBJECT (obj->dbg_obj,
4050 "Failed to set crop rectangle with VIDIOC_S_SELECTION: %s",
4051 g_strerror (errno));
4054 if (obj->ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
4055 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_S_CROP failed");
4059 if (obj->ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
4060 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_G_CROP failed");
4068 GST_DEBUG_OBJECT (obj->dbg_obj,
4069 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4070 crop.c.width, crop.c.height);
4076 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
4078 GstStructure *config;
4082 if (!v4l2object->pool)
4085 config = gst_buffer_pool_get_config (v4l2object->pool);
4086 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4088 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
4090 gst_structure_free (config);
4096 gst_v4l2_object_caps_is_subset (GstV4l2Object * v4l2object, GstCaps * caps)
4098 GstStructure *config;
4102 if (!v4l2object->pool)
4105 config = gst_buffer_pool_get_config (v4l2object->pool);
4106 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4108 ret = oldcaps && gst_caps_is_subset (oldcaps, caps);
4110 gst_structure_free (config);
4116 gst_v4l2_object_get_current_caps (GstV4l2Object * v4l2object)
4118 GstStructure *config;
4121 if (!v4l2object->pool)
4124 config = gst_buffer_pool_get_config (v4l2object->pool);
4125 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4128 gst_caps_ref (oldcaps);
4130 gst_structure_free (config);
4136 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
4138 gboolean ret = TRUE;
4140 GST_LOG_OBJECT (v4l2object->dbg_obj, "start flushing");
4142 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
4143 gst_buffer_pool_set_flushing (v4l2object->pool, TRUE);
4149 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
4151 gboolean ret = TRUE;
4153 GST_LOG_OBJECT (v4l2object->dbg_obj, "stop flushing");
4155 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
4156 gst_buffer_pool_set_flushing (v4l2object->pool, FALSE);
4162 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
4164 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "stopping");
4166 if (!GST_V4L2_IS_OPEN (v4l2object))
4168 if (!GST_V4L2_IS_ACTIVE (v4l2object))
4171 if (v4l2object->pool) {
4172 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "deactivating pool");
4173 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
4174 gst_object_unref (v4l2object->pool);
4175 v4l2object->pool = NULL;
4178 GST_V4L2_SET_INACTIVE (v4l2object);
4185 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4191 formats = gst_v4l2_object_get_format_list (v4l2object);
4193 ret = gst_caps_new_empty ();
4195 if (v4l2object->keep_aspect && !v4l2object->par) {
4196 struct v4l2_cropcap cropcap;
4198 memset (&cropcap, 0, sizeof (cropcap));
4200 cropcap.type = v4l2object->type;
4201 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0) {
4202 if (errno != ENOTTY)
4203 GST_WARNING_OBJECT (v4l2object->dbg_obj,
4204 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
4205 g_strerror (errno));
4206 } else if (cropcap.pixelaspect.numerator && cropcap.pixelaspect.denominator) {
4207 v4l2object->par = g_new0 (GValue, 1);
4208 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
4209 gst_value_set_fraction (v4l2object->par, cropcap.pixelaspect.numerator,
4210 cropcap.pixelaspect.denominator);
4214 for (walk = formats; walk; walk = walk->next) {
4215 struct v4l2_fmtdesc *format;
4216 GstStructure *template;
4219 format = (struct v4l2_fmtdesc *) walk->data;
4221 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
4224 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
4225 "unknown format %" GST_FOURCC_FORMAT,
4226 GST_FOURCC_ARGS (format->pixelformat));
4230 /* If we have a filter, check if we need to probe this format or not */
4232 GstCaps *format_caps = gst_caps_new_empty ();
4234 gst_caps_append_structure (format_caps, gst_structure_copy (template));
4236 if (!gst_caps_can_intersect (format_caps, filter)) {
4237 gst_caps_unref (format_caps);
4238 gst_structure_free (template);
4242 gst_caps_unref (format_caps);
4245 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
4246 format->pixelformat, template);
4248 gst_caps_append (ret, tmp);
4250 gst_structure_free (template);
4257 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
4258 gst_caps_unref (tmp);
4261 GST_INFO_OBJECT (v4l2object->dbg_obj, "probed caps: %" GST_PTR_FORMAT, ret);
4267 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4271 if (v4l2object->probed_caps == NULL)
4272 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
4275 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
4276 GST_CAPS_INTERSECT_FIRST);
4278 ret = gst_caps_ref (v4l2object->probed_caps);
4285 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
4288 GstBufferPool *pool = NULL, *other_pool = NULL;
4289 GstStructure *config;
4290 guint size, min, max, own_min = 0;
4292 gboolean has_video_meta;
4293 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
4294 GstAllocator *allocator = NULL;
4295 GstAllocationParams params = { 0 };
4297 GST_DEBUG_OBJECT (obj->dbg_obj, "decide allocation");
4299 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4300 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
4302 gst_query_parse_allocation (query, &caps, NULL);
4304 if (obj->pool == NULL) {
4305 if (!gst_v4l2_object_setup_pool (obj, caps))
4309 if (gst_query_get_n_allocation_params (query) > 0)
4310 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
4312 if (gst_query_get_n_allocation_pools (query) > 0) {
4313 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
4322 GST_DEBUG_OBJECT (obj->dbg_obj, "allocation: size:%u min:%u max:%u pool:%"
4323 GST_PTR_FORMAT, size, min, max, pool);
4326 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4328 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
4330 gst_v4l2_get_driver_min_buffers (obj);
4331 /* We can't share our own pool, if it exceed V4L2 capacity */
4332 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
4333 can_share_own_pool = FALSE;
4336 switch (obj->mode) {
4337 case GST_V4L2_IO_RW:
4339 /* in READ/WRITE mode, prefer a downstream pool because our own pool
4340 * doesn't help much, we have to write to it as well */
4341 GST_DEBUG_OBJECT (obj->dbg_obj,
4342 "read/write mode: using downstream pool");
4343 /* use the bigest size, when we use our own pool we can't really do any
4344 * other size than what the hardware gives us but for downstream pools
4346 size = MAX (size, obj->info.size);
4347 } else if (can_share_own_pool) {
4348 /* no downstream pool, use our own then */
4349 GST_DEBUG_OBJECT (obj->dbg_obj,
4350 "read/write mode: no downstream pool, using our own");
4351 pool = gst_object_ref (obj->pool);
4352 size = obj->info.size;
4353 pushing_from_our_pool = TRUE;
4357 case GST_V4L2_IO_USERPTR:
4358 case GST_V4L2_IO_DMABUF_IMPORT:
4359 /* in importing mode, prefer our own pool, and pass the other pool to
4360 * our own, so it can serve itself */
4362 goto no_downstream_pool;
4363 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj->pool),
4366 gst_object_unref (pool);
4367 pool = gst_object_ref (obj->pool);
4368 size = obj->info.size;
4371 case GST_V4L2_IO_MMAP:
4372 case GST_V4L2_IO_DMABUF:
4373 /* in streaming mode, prefer our own pool */
4374 /* Check if we can use it ... */
4375 if (can_share_own_pool) {
4377 gst_object_unref (pool);
4378 pool = gst_object_ref (obj->pool);
4379 size = obj->info.size;
4380 GST_DEBUG_OBJECT (obj->dbg_obj,
4381 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
4382 pushing_from_our_pool = TRUE;
4384 GST_DEBUG_OBJECT (obj->dbg_obj,
4385 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
4388 GST_DEBUG_OBJECT (obj->dbg_obj,
4389 "streaming mode: no usable pool, copying to generic pool");
4390 size = MAX (size, obj->info.size);
4393 case GST_V4L2_IO_AUTO:
4395 GST_WARNING_OBJECT (obj->dbg_obj, "unhandled mode");
4402 /* If pushing from our own pool, configure it with queried minimum,
4403 * otherwise use the minimum required */
4404 if (pushing_from_our_pool) {
4405 /* When pushing from our own pool, we need what downstream one, to be able
4406 * to fill the pipeline, the minimum required to decoder according to the
4407 * driver and 2 more, so we don't endup up with everything downstream or
4408 * held by the decoder. We account 2 buffers for v4l2 so when one is being
4409 * pushed downstream the other one can already be queued for the next
4411 own_min = min + obj->min_buffers + 2;
4413 /* If no allocation parameters where provided, allow for a little more
4414 * buffers and enable copy threshold */
4417 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4420 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4425 /* In this case we'll have to configure two buffer pool. For our buffer
4426 * pool, we'll need what the driver one, and one more, so we can dequeu */
4427 own_min = obj->min_buffers + 1;
4428 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS);
4430 /* for the downstream pool, we keep what downstream wants, though ensure
4431 * at least a minimum if downstream didn't suggest anything (we are
4432 * expecting the base class to create a default one for the context) */
4433 min = MAX (min, GST_V4L2_MIN_BUFFERS);
4435 /* To import we need the other pool to hold at least own_min */
4436 if (obj->pool == pool)
4440 /* Request a bigger max, if one was suggested but it's too small */
4442 max = MAX (min, max);
4444 /* First step, configure our own pool */
4445 config = gst_buffer_pool_get_config (obj->pool);
4447 if (obj->need_video_meta || has_video_meta) {
4448 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
4449 gst_buffer_pool_config_add_option (config,
4450 GST_BUFFER_POOL_OPTION_VIDEO_META);
4453 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4454 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
4456 GST_DEBUG_OBJECT (obj->dbg_obj, "setting own pool config to %"
4457 GST_PTR_FORMAT, config);
4459 /* Our pool often need to adjust the value */
4460 if (!gst_buffer_pool_set_config (obj->pool, config)) {
4461 config = gst_buffer_pool_get_config (obj->pool);
4463 GST_DEBUG_OBJECT (obj->dbg_obj, "own pool config changed to %"
4464 GST_PTR_FORMAT, config);
4466 /* our pool will adjust the maximum buffer, which we are fine with */
4467 if (!gst_buffer_pool_set_config (obj->pool, config))
4471 /* Now configure the other pool if different */
4472 if (obj->pool != pool)
4476 config = gst_buffer_pool_get_config (other_pool);
4477 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4478 gst_buffer_pool_config_set_params (config, caps, size, min, max);
4480 GST_DEBUG_OBJECT (obj->dbg_obj, "setting other pool config to %"
4481 GST_PTR_FORMAT, config);
4483 /* if downstream supports video metadata, add this to the pool config */
4484 if (has_video_meta) {
4485 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
4486 gst_buffer_pool_config_add_option (config,
4487 GST_BUFFER_POOL_OPTION_VIDEO_META);
4490 if (!gst_buffer_pool_set_config (other_pool, config)) {
4491 config = gst_buffer_pool_get_config (other_pool);
4493 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
4495 gst_structure_free (config);
4499 if (!gst_buffer_pool_set_config (other_pool, config))
4505 /* For simplicity, simply read back the active configuration, so our base
4506 * class get the right information */
4507 config = gst_buffer_pool_get_config (pool);
4508 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
4509 gst_structure_free (config);
4513 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
4515 gst_query_add_allocation_pool (query, pool, size, min, max);
4518 gst_object_unref (allocator);
4521 gst_object_unref (pool);
4527 /* setup_pool already send the error */
4532 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4533 (_("Failed to configure internal buffer pool.")), (NULL));
4538 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4539 (_("Video device did not suggest any buffer size.")), (NULL));
4545 gst_object_unref (allocator);
4548 gst_object_unref (pool);
4553 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4554 (_("No downstream pool to import from.")),
4555 ("When importing DMABUF or USERPTR, we need a pool to import from"));
4561 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
4563 GstBufferPool *pool;
4564 /* we need at least 2 buffers to operate */
4565 guint size, min, max;
4569 /* Set defaults allocation parameters */
4570 size = obj->info.size;
4571 min = GST_V4L2_MIN_BUFFERS;
4572 max = VIDEO_MAX_FRAME;
4574 gst_query_parse_allocation (query, &caps, &need_pool);
4579 switch (obj->mode) {
4580 case GST_V4L2_IO_MMAP:
4581 case GST_V4L2_IO_DMABUF:
4582 if ((pool = obj->pool))
4583 gst_object_ref (pool);
4592 GstStructure *config;
4594 /* we had a pool, check caps */
4595 config = gst_buffer_pool_get_config (pool);
4596 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
4598 GST_DEBUG_OBJECT (obj->dbg_obj,
4599 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
4600 if (!gst_caps_is_equal (caps, pcaps)) {
4601 gst_structure_free (config);
4602 gst_object_unref (pool);
4603 goto different_caps;
4605 gst_structure_free (config);
4607 gst_v4l2_get_driver_min_buffers (obj);
4609 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS);
4611 gst_query_add_allocation_pool (query, pool, size, min, max);
4613 /* we also support various metadata */
4614 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4617 gst_object_unref (pool);
4624 GST_DEBUG_OBJECT (obj->dbg_obj, "no caps specified");
4629 /* different caps, we can't use this pool */
4630 GST_DEBUG_OBJECT (obj->dbg_obj, "pool has different caps");
4636 gst_v4l2_object_try_import (GstV4l2Object * obj, GstBuffer * buffer)
4638 GstVideoMeta *vmeta;
4639 guint n_mem = gst_buffer_n_memory (buffer);
4641 /* only import if requested */
4642 switch (obj->mode) {
4643 case GST_V4L2_IO_USERPTR:
4644 case GST_V4L2_IO_DMABUF_IMPORT:
4647 GST_DEBUG_OBJECT (obj->dbg_obj,
4648 "The io-mode does not enable importation");
4652 vmeta = gst_buffer_get_video_meta (buffer);
4653 if (!vmeta && obj->need_video_meta) {
4654 GST_DEBUG_OBJECT (obj->dbg_obj, "Downstream buffer uses standard "
4655 "stride/offset while the driver does not.");
4659 /* we need matching strides/offsets and size */
4662 gboolean need_fmt_update = FALSE;
4664 if (vmeta->n_planes != GST_VIDEO_INFO_N_PLANES (&obj->info)) {
4665 GST_WARNING_OBJECT (obj->dbg_obj,
4666 "Cannot import buffers with different number planes");
4670 for (p = 0; p < vmeta->n_planes; p++) {
4671 if (vmeta->stride[p] < obj->info.stride[p]) {
4672 GST_DEBUG_OBJECT (obj->dbg_obj,
4673 "Not importing as remote stride %i is smaller then %i on plane %u",
4674 vmeta->stride[p], obj->info.stride[p], p);
4676 } else if (vmeta->stride[p] > obj->info.stride[p]) {
4677 need_fmt_update = TRUE;
4680 if (vmeta->offset[p] < obj->info.offset[p]) {
4681 GST_DEBUG_OBJECT (obj->dbg_obj,
4682 "Not importing as offset %" G_GSIZE_FORMAT
4683 " is smaller then %" G_GSIZE_FORMAT " on plane %u",
4684 vmeta->offset[p], obj->info.offset[p], p);
4686 } else if (vmeta->offset[p] > obj->info.offset[p]) {
4687 need_fmt_update = TRUE;
4691 if (need_fmt_update) {
4692 struct v4l2_format format;
4693 gint wanted_stride[GST_VIDEO_MAX_PLANES] = { 0, };
4695 format = obj->format;
4697 /* update the current format with the stride we want to import from */
4698 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
4701 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted strides:");
4703 for (i = 0; i < obj->n_v4l2_planes; i++) {
4704 gint stride = vmeta->stride[i];
4706 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4707 stride = GST_VIDEO_TILE_X_TILES (stride) <<
4708 GST_VIDEO_FORMAT_INFO_TILE_WS (obj->info.finfo);
4710 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
4711 wanted_stride[i] = stride;
4712 GST_DEBUG_OBJECT (obj->dbg_obj, " [%u] %i", i, wanted_stride[i]);
4715 gint stride = vmeta->stride[0];
4717 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted stride: %i", stride);
4719 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4720 stride = GST_VIDEO_TILE_X_TILES (stride) <<
4721 GST_VIDEO_FORMAT_INFO_TILE_WS (obj->info.finfo);
4723 format.fmt.pix.bytesperline = stride;
4724 wanted_stride[0] = stride;
4727 if (obj->ioctl (obj->video_fd, VIDIOC_S_FMT, &format) < 0) {
4728 GST_WARNING_OBJECT (obj->dbg_obj,
4729 "Something went wrong trying to update current format: %s",
4730 g_strerror (errno));
4734 gst_v4l2_object_save_format (obj, obj->fmtdesc, &format, &obj->info,
4737 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
4740 for (i = 0; i < obj->n_v4l2_planes; i++) {
4741 if (format.fmt.pix_mp.plane_fmt[i].bytesperline != wanted_stride[i]) {
4742 GST_DEBUG_OBJECT (obj->dbg_obj,
4743 "[%i] Driver did not accept the new stride (wants %i, got %i)",
4744 i, format.fmt.pix_mp.plane_fmt[i].bytesperline,
4750 if (format.fmt.pix.bytesperline != wanted_stride[0]) {
4751 GST_DEBUG_OBJECT (obj->dbg_obj,
4752 "Driver did not accept the new stride (wants %i, got %i)",
4753 format.fmt.pix.bytesperline, wanted_stride[0]);
4760 /* we can always import single memory buffer, but otherwise we need the same
4761 * amount of memory object. */
4762 if (n_mem != 1 && n_mem != obj->n_v4l2_planes) {
4763 GST_DEBUG_OBJECT (obj->dbg_obj, "Can only import %i memory, "
4764 "buffers contains %u memory", obj->n_v4l2_planes, n_mem);
4768 /* For DMABuf importation we need DMABuf of course */
4769 if (obj->mode == GST_V4L2_IO_DMABUF_IMPORT) {
4772 for (i = 0; i < n_mem; i++) {
4773 GstMemory *mem = gst_buffer_peek_memory (buffer, i);
4775 if (!gst_is_dmabuf_memory (mem)) {
4776 GST_DEBUG_OBJECT (obj->dbg_obj, "Cannot import non-DMABuf memory.");
4782 /* for the remaining, only the kernel driver can tell */