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 <glib/gi18n-lib.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)
57 #define GST_V4L2_DEFAULT_WIDTH 320
58 #define GST_V4L2_DEFAULT_HEIGHT 240
63 V4L2_STD_OBJECT_PROPS,
67 * common format / caps utilities:
71 GST_V4L2_RAW = 1 << 0,
72 GST_V4L2_CODEC = 1 << 1,
73 GST_V4L2_TRANSPORT = 1 << 2,
74 GST_V4L2_NO_PARSE = 1 << 3,
82 GstV4L2FormatFlags flags;
85 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
87 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
88 {V4L2_PIX_FMT_ARGB555, TRUE, GST_V4L2_RAW},
89 {V4L2_PIX_FMT_XRGB555, TRUE, GST_V4L2_RAW},
90 {V4L2_PIX_FMT_ARGB555X, TRUE, GST_V4L2_RAW},
91 {V4L2_PIX_FMT_XRGB555X, TRUE, GST_V4L2_RAW},
92 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
93 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
94 {V4L2_PIX_FMT_BGR666, TRUE, GST_V4L2_RAW},
95 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
96 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
97 {V4L2_PIX_FMT_ABGR32, TRUE, GST_V4L2_RAW},
98 {V4L2_PIX_FMT_XBGR32, TRUE, GST_V4L2_RAW},
99 {V4L2_PIX_FMT_BGRA32, TRUE, GST_V4L2_RAW},
100 {V4L2_PIX_FMT_BGRX32, TRUE, GST_V4L2_RAW},
101 {V4L2_PIX_FMT_RGBA32, TRUE, GST_V4L2_RAW},
102 {V4L2_PIX_FMT_RGBX32, TRUE, GST_V4L2_RAW},
103 {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
104 {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},
106 /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
107 {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
108 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
109 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
110 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
111 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
114 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
115 {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
116 {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
117 {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
118 {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
119 {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
120 {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
121 {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},
122 {V4L2_PIX_FMT_INVZ, TRUE, GST_V4L2_RAW},
124 /* Palette formats */
125 {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},
127 /* Chrominance formats */
128 {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},
130 /* Luminance+Chrominance formats */
131 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
132 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
133 {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
134 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
135 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
136 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
137 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
138 {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
139 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
140 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
141 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
142 {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
143 {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
144 {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
145 {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
146 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
147 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
148 {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
149 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
150 {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
151 {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
153 /* two planes -- one Y, one Cr + Cb interleaved */
154 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
155 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
156 {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
158 {V4L2_PIX_FMT_NV12M_8L128, TRUE, GST_V4L2_RAW},
159 {V4L2_PIX_FMT_NV12M_10BE_8L128, TRUE, GST_V4L2_RAW},
160 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
161 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
162 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
163 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
164 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
165 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
166 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
167 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
168 {V4L2_PIX_FMT_MM21, TRUE, GST_V4L2_RAW},
170 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
171 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_RAW},
172 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_RAW},
173 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_RAW},
174 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_RAW},
176 /* compressed formats */
177 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
178 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
179 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
180 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
181 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
182 {V4L2_PIX_FMT_FWHT, FALSE, GST_V4L2_CODEC},
183 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
184 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
185 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
186 {V4L2_PIX_FMT_HEVC, FALSE, GST_V4L2_CODEC},
187 {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
188 {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
189 {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
190 {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
191 {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
192 {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
193 {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
194 {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
195 {V4L2_PIX_FMT_VP9, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
197 /* Vendor-specific formats */
198 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
199 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
200 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
201 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
204 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
206 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
209 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
211 gst_v4l2_device_get_type (void)
213 static GType v4l2_device_type = 0;
215 if (v4l2_device_type == 0) {
216 static const GFlagsValue values[] = {
217 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
218 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
219 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
221 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
222 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
224 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
225 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
231 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
234 return v4l2_device_type;
237 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
239 gst_v4l2_tv_norm_get_type (void)
241 static GType v4l2_tv_norm = 0;
244 static const GEnumValue tv_norms[] = {
247 {V4L2_STD_NTSC, "NTSC", "NTSC"},
248 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
249 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
250 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
251 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
253 {V4L2_STD_PAL, "PAL", "PAL"},
254 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
255 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
256 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
257 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
258 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
259 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
260 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
261 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
262 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
263 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
264 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
265 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
266 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
267 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
269 {V4L2_STD_SECAM, "SECAM", "SECAM"},
270 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
271 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
272 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
273 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
274 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
275 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
276 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
277 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
278 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
283 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
290 gst_v4l2_io_mode_get_type (void)
292 static GType v4l2_io_mode = 0;
295 static const GEnumValue io_modes[] = {
296 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
297 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
298 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
299 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
300 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
301 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
306 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
312 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
313 const char *default_device)
315 g_object_class_install_property (gobject_class, PROP_DEVICE,
316 g_param_spec_string ("device", "Device", "Device location",
317 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
318 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
319 g_param_spec_string ("device-name", "Device name",
320 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
321 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
323 g_param_spec_int ("device-fd", "File descriptor",
324 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
325 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_FLAGS,
327 g_param_spec_flags ("flags", "Flags", "Device type flags",
328 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
329 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
332 * GstV4l2Src:brightness:
334 * Picture brightness, or more precisely, the black level
336 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
337 g_param_spec_int ("brightness", "Brightness",
338 "Picture brightness, or more precisely, the black level", G_MININT,
340 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
342 * GstV4l2Src:contrast:
344 * Picture contrast or luma gain
346 g_object_class_install_property (gobject_class, PROP_CONTRAST,
347 g_param_spec_int ("contrast", "Contrast",
348 "Picture contrast or luma gain", G_MININT,
350 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
352 * GstV4l2Src:saturation:
354 * Picture color saturation or chroma gain
356 g_object_class_install_property (gobject_class, PROP_SATURATION,
357 g_param_spec_int ("saturation", "Saturation",
358 "Picture color saturation or chroma gain", G_MININT,
360 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
364 * Hue or color balance
366 g_object_class_install_property (gobject_class, PROP_HUE,
367 g_param_spec_int ("hue", "Hue",
368 "Hue or color balance", G_MININT,
370 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
377 g_object_class_install_property (gobject_class, PROP_TV_NORM,
378 g_param_spec_enum ("norm", "TV norm",
380 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384 * GstV4l2Src:io-mode:
388 g_object_class_install_property (gobject_class, PROP_IO_MODE,
389 g_param_spec_enum ("io-mode", "IO mode",
391 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
392 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
395 * GstV4l2Src:extra-controls:
397 * Additional v4l2 controls for the device. The controls are identified
398 * by the control name (lowercase with '_' for any non-alphanumeric
403 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
404 g_param_spec_boxed ("extra-controls", "Extra Controls",
405 "Extra v4l2 controls (CIDs) for the device",
406 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 * GstV4l2Src:pixel-aspect-ratio:
411 * The pixel aspect ratio of the device. This overwrites the pixel aspect
412 * ratio queried from the device.
416 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
417 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
418 "Overwrite the pixel aspect ratio of the device", "1/1",
419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
422 * GstV4l2Src:force-aspect-ratio:
424 * When enabled, the pixel aspect ratio queried from the device or set
425 * with the pixel-aspect-ratio property will be enforced.
429 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
430 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
431 "When enabled, the pixel aspect ratio will be enforced", TRUE,
432 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
434 gst_type_mark_as_plugin_api (GST_TYPE_V4L2_DEVICE_FLAGS, 0);
435 gst_type_mark_as_plugin_api (GST_TYPE_V4L2_TV_NORM, 0);
436 gst_type_mark_as_plugin_api (GST_TYPE_V4L2_IO_MODE, 0);
440 gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
442 g_object_class_install_property (gobject_class, PROP_DEVICE,
443 g_param_spec_string ("device", "Device", "Device location",
444 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
446 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
447 g_param_spec_string ("device-name", "Device name",
448 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
449 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
451 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
452 g_param_spec_int ("device-fd", "File descriptor",
453 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
454 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
456 g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
457 g_param_spec_enum ("output-io-mode", "Output IO mode",
458 "Output side I/O mode (matches sink pad)",
459 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
460 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
462 g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
463 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
464 "Capture I/O mode (matches src pad)",
465 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
466 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
468 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
469 g_param_spec_boxed ("extra-controls", "Extra Controls",
470 "Extra v4l2 controls (CIDs) for the device",
471 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474 /* Support for 32bit off_t, this wrapper is casting off_t to gint64 */
479 v4l2_mmap_wrapper (gpointer start, gsize length, gint prot, gint flags, gint fd,
482 return v4l2_mmap (start, length, prot, flags, fd, (gint64) offset);
485 #define v4l2_mmap v4l2_mmap_wrapper
487 #endif /* SIZEOF_OFF_T < 8 */
488 #endif /* HAVE_LIBV4L2 */
491 gst_v4l2_object_new (GstElement * element,
492 GstObject * debug_object,
493 enum v4l2_buf_type type,
494 const char *default_device,
495 GstV4l2GetInOutFunction get_in_out_func,
496 GstV4l2SetInOutFunction set_in_out_func,
497 GstV4l2UpdateFpsFunction update_fps_func)
499 GstV4l2Object *v4l2object;
502 * some default values
504 v4l2object = g_new0 (GstV4l2Object, 1);
506 v4l2object->type = type;
507 v4l2object->formats = NULL;
509 v4l2object->element = element;
510 v4l2object->dbg_obj = debug_object;
511 v4l2object->get_in_out_func = get_in_out_func;
512 v4l2object->set_in_out_func = set_in_out_func;
513 v4l2object->update_fps_func = update_fps_func;
515 v4l2object->video_fd = -1;
516 v4l2object->active = FALSE;
517 v4l2object->videodev = g_strdup (default_device);
519 v4l2object->norms = NULL;
520 v4l2object->channels = NULL;
521 v4l2object->colors = NULL;
523 v4l2object->keep_aspect = TRUE;
525 v4l2object->n_v4l2_planes = 0;
527 v4l2object->no_initial_format = FALSE;
529 v4l2object->poll = gst_poll_new (TRUE);
530 v4l2object->can_poll_device = TRUE;
532 /* We now disable libv4l2 by default, but have an env to enable it. */
534 #ifdef TIZEN_FEATURE_USE_LIBV4L2
536 #else /* TIZEN_FEATURE_USE_LIBV4L2 */
537 if (g_getenv ("GST_V4L2_USE_LIBV4L2")) {
538 #endif /* TIZEN_FEATURE_USE_LIBV4L2 */
539 v4l2object->fd_open = v4l2_fd_open;
540 v4l2object->close = v4l2_close;
541 v4l2object->dup = v4l2_dup;
542 v4l2object->ioctl = v4l2_ioctl;
543 v4l2object->read = v4l2_read;
544 v4l2object->mmap = v4l2_mmap;
545 v4l2object->munmap = v4l2_munmap;
549 v4l2object->fd_open = NULL;
550 v4l2object->close = close;
551 v4l2object->dup = dup;
552 v4l2object->ioctl = ioctl;
553 v4l2object->read = read;
554 v4l2object->mmap = mmap;
555 v4l2object->munmap = munmap;
563 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
565 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
566 g_slist_free (v4l2object->formats);
567 v4l2object->formats = NULL;
568 v4l2object->fmtdesc = NULL;
575 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
577 g_return_if_fail (v4l2object != NULL);
579 g_free (v4l2object->videodev);
580 g_free (v4l2object->par);
581 g_free (v4l2object->channel);
583 gst_poll_free (v4l2object->poll);
585 if (v4l2object->formats) {
586 gst_v4l2_object_clear_format_list (v4l2object);
589 if (v4l2object->probed_caps) {
590 gst_caps_unref (v4l2object->probed_caps);
593 if (v4l2object->extra_controls) {
594 gst_structure_free (v4l2object->extra_controls);
602 gst_v4l2_object_prop_to_cid (guint prop_id)
607 case PROP_BRIGHTNESS:
608 cid = V4L2_CID_BRIGHTNESS;
611 cid = V4L2_CID_CONTRAST;
613 case PROP_SATURATION:
614 cid = V4L2_CID_SATURATION;
620 GST_WARNING ("unmapped property id: %d", prop_id);
627 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
628 guint prop_id, const GValue * value, GParamSpec * pspec)
632 g_free (v4l2object->videodev);
633 v4l2object->videodev = g_value_dup_string (value);
635 case PROP_BRIGHTNESS:
637 case PROP_SATURATION:
640 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
643 if (GST_V4L2_IS_OPEN (v4l2object)) {
644 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
651 v4l2object->tv_norm = g_value_get_enum (value);
655 if (GST_V4L2_IS_OPEN (v4l2object)) {
656 GstTuner *tuner = GST_TUNER (v4l2object->element);
657 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
658 (gchar *) g_value_get_string (value));
661 /* like gst_tuner_set_channel (tuner, channel)
662 without g_object_notify */
663 gst_v4l2_tuner_set_channel (v4l2object, channel);
666 g_free (v4l2object->channel);
667 v4l2object->channel = g_value_dup_string (value);
671 if (GST_V4L2_IS_OPEN (v4l2object)) {
672 GstTuner *tuner = GST_TUNER (v4l2object->element);
673 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
676 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
678 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
679 without g_object_notify */
680 gst_v4l2_tuner_set_frequency (v4l2object, channel,
681 g_value_get_ulong (value));
684 v4l2object->frequency = g_value_get_ulong (value);
690 v4l2object->req_mode = g_value_get_enum (value);
692 case PROP_CAPTURE_IO_MODE:
693 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
694 v4l2object->req_mode = g_value_get_enum (value);
696 case PROP_OUTPUT_IO_MODE:
697 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
698 v4l2object->req_mode = g_value_get_enum (value);
700 case PROP_EXTRA_CONTROLS:{
701 const GstStructure *s = gst_value_get_structure (value);
703 if (v4l2object->extra_controls)
704 gst_structure_free (v4l2object->extra_controls);
706 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
707 if (GST_V4L2_IS_OPEN (v4l2object))
708 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
711 case PROP_PIXEL_ASPECT_RATIO:
712 if (v4l2object->par) {
713 g_value_unset (v4l2object->par);
714 g_free (v4l2object->par);
716 v4l2object->par = g_new0 (GValue, 1);
717 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
718 if (!g_value_transform (value, v4l2object->par)) {
719 g_warning ("Could not transform string to aspect ratio");
720 gst_value_set_fraction (v4l2object->par, 1, 1);
723 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "set PAR to %d/%d",
724 gst_value_get_fraction_numerator (v4l2object->par),
725 gst_value_get_fraction_denominator (v4l2object->par));
727 case PROP_FORCE_ASPECT_RATIO:
728 v4l2object->keep_aspect = g_value_get_boolean (value);
739 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
740 guint prop_id, GValue * value, GParamSpec * pspec)
744 g_value_set_string (value, v4l2object->videodev);
746 case PROP_DEVICE_NAME:
748 const guchar *name = NULL;
750 if (GST_V4L2_IS_OPEN (v4l2object))
751 name = v4l2object->vcap.card;
753 g_value_set_string (value, (gchar *) name);
758 if (GST_V4L2_IS_OPEN (v4l2object))
759 g_value_set_int (value, v4l2object->video_fd);
761 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
768 if (GST_V4L2_IS_OPEN (v4l2object)) {
769 flags |= v4l2object->device_caps &
770 (V4L2_CAP_VIDEO_CAPTURE |
771 V4L2_CAP_VIDEO_OUTPUT |
772 V4L2_CAP_VIDEO_OVERLAY |
773 V4L2_CAP_VBI_CAPTURE |
774 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
776 if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
777 flags |= V4L2_CAP_VIDEO_CAPTURE;
779 if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
780 flags |= V4L2_CAP_VIDEO_OUTPUT;
782 g_value_set_flags (value, flags);
785 case PROP_BRIGHTNESS:
787 case PROP_SATURATION:
790 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
793 if (GST_V4L2_IS_OPEN (v4l2object)) {
795 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
796 g_value_set_int (value, v);
804 g_value_set_enum (value, v4l2object->tv_norm);
807 g_value_set_enum (value, v4l2object->req_mode);
809 case PROP_CAPTURE_IO_MODE:
810 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
811 g_value_set_enum (value, v4l2object->req_mode);
813 case PROP_OUTPUT_IO_MODE:
814 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
815 g_value_set_enum (value, v4l2object->req_mode);
817 case PROP_EXTRA_CONTROLS:
818 gst_value_set_structure (value, v4l2object->extra_controls);
820 case PROP_PIXEL_ASPECT_RATIO:
822 g_value_transform (v4l2object->par, value);
824 case PROP_FORCE_ASPECT_RATIO:
825 g_value_set_boolean (value, v4l2object->keep_aspect);
835 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
837 struct v4l2_control control = { 0, };
839 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
841 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
842 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
844 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
846 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
847 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
848 "driver requires a minimum of %d buffers", control.value);
849 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
850 #define DEFAULT_DECODER_OUTPUT_BUFFER_COUNT 5
851 if (v4l2object->tbm_output &&
852 !V4L2_TYPE_IS_OUTPUT (v4l2object->type) && control.value == 1) {
853 v4l2object->min_buffers = DEFAULT_DECODER_OUTPUT_BUFFER_COUNT;
854 GST_WARNING_OBJECT (v4l2object->dbg_obj, "but SET MIN BUFFER COUNT[%d] and it will be [%d] later",
855 v4l2object->min_buffers, v4l2object->min_buffers + 1);
857 v4l2object->min_buffers = control.value;
859 #else /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
860 v4l2object->min_buffers = control.value;
861 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
863 v4l2object->min_buffers = 0;
868 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
870 GstTunerNorm *norm = NULL;
871 GstTunerChannel *channel = NULL;
874 if (!GST_IS_TUNER (v4l2object->element))
877 tuner = GST_TUNER (v4l2object->element);
879 if (v4l2object->tv_norm)
880 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
881 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
882 "norm=%p", (guint64) v4l2object->tv_norm, norm);
884 gst_tuner_set_norm (tuner, norm);
887 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
889 v4l2object->tv_norm =
890 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
891 gst_tuner_norm_changed (tuner, norm);
895 if (v4l2object->channel)
896 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
898 gst_tuner_set_channel (tuner, channel);
901 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
902 (v4l2object->element)));
904 g_free (v4l2object->channel);
905 v4l2object->channel = g_strdup (channel->label);
906 gst_tuner_channel_changed (tuner, channel);
911 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
912 if (v4l2object->frequency != 0) {
913 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
915 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
916 if (v4l2object->frequency == 0) {
918 gst_tuner_set_frequency (tuner, channel, 1000);
926 gst_v4l2_object_init_poll (GstV4l2Object * v4l2object)
928 gst_poll_fd_init (&v4l2object->pollfd);
929 v4l2object->pollfd.fd = v4l2object->video_fd;
930 gst_poll_add_fd (v4l2object->poll, &v4l2object->pollfd);
931 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
932 gst_poll_fd_ctl_write (v4l2object->poll, &v4l2object->pollfd, TRUE);
934 gst_poll_fd_ctl_read (v4l2object->poll, &v4l2object->pollfd, TRUE);
936 v4l2object->can_poll_device = TRUE;
940 gst_v4l2_object_open (GstV4l2Object * v4l2object, GstV4l2Error * error)
942 if (gst_v4l2_open (v4l2object, error))
943 gst_v4l2_set_defaults (v4l2object);
947 gst_v4l2_object_init_poll (v4l2object);
953 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
955 if (gst_v4l2_dup (v4l2object, other)) {
956 gst_v4l2_object_init_poll (v4l2object);
964 gst_v4l2_object_close (GstV4l2Object * v4l2object)
966 if (!gst_v4l2_close (v4l2object))
969 gst_caps_replace (&v4l2object->probed_caps, NULL);
971 /* reset our copy of the device caps */
972 v4l2object->device_caps = 0;
974 if (v4l2object->formats) {
975 gst_v4l2_object_clear_format_list (v4l2object);
978 if (v4l2object->par) {
979 g_value_unset (v4l2object->par);
980 g_free (v4l2object->par);
981 v4l2object->par = NULL;
984 if (v4l2object->channel) {
985 g_free (v4l2object->channel);
986 v4l2object->channel = NULL;
992 static struct v4l2_fmtdesc *
993 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
996 struct v4l2_fmtdesc *fmt;
1002 walk = gst_v4l2_object_get_format_list (v4l2object);
1004 fmt = (struct v4l2_fmtdesc *) walk->data;
1005 if (fmt->pixelformat == fourcc)
1007 /* special case for jpeg */
1008 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
1009 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
1010 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
1011 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
1012 fourcc == V4L2_PIX_FMT_PJPG) {
1016 walk = g_slist_next (walk);
1024 /* complete made up ranking, the values themselves are meaningless */
1025 /* These ranks MUST be X such that X<<15 fits on a signed int - see
1026 the comment at the end of gst_v4l2_object_format_get_rank. */
1027 #define YUV_BASE_RANK 1000
1028 #define JPEG_BASE_RANK 500
1029 #define DV_BASE_RANK 200
1030 #define RGB_BASE_RANK 100
1031 #define YUV_ODD_BASE_RANK 50
1032 #define RGB_ODD_BASE_RANK 25
1033 #define BAYER_BASE_RANK 15
1034 #define S910_BASE_RANK 10
1035 #define GREY_BASE_RANK 5
1036 #define PWC_BASE_RANK 1
1039 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
1041 guint32 fourcc = fmt->pixelformat;
1042 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
1046 case V4L2_PIX_FMT_MJPEG:
1047 case V4L2_PIX_FMT_PJPG:
1048 rank = JPEG_BASE_RANK;
1050 case V4L2_PIX_FMT_JPEG:
1051 rank = JPEG_BASE_RANK + 1;
1053 case V4L2_PIX_FMT_MPEG: /* MPEG */
1054 rank = JPEG_BASE_RANK + 2;
1057 case V4L2_PIX_FMT_RGB332:
1058 case V4L2_PIX_FMT_ARGB555:
1059 case V4L2_PIX_FMT_XRGB555:
1060 case V4L2_PIX_FMT_RGB555:
1061 case V4L2_PIX_FMT_ARGB555X:
1062 case V4L2_PIX_FMT_XRGB555X:
1063 case V4L2_PIX_FMT_RGB555X:
1064 case V4L2_PIX_FMT_BGR666:
1065 case V4L2_PIX_FMT_RGB565:
1066 case V4L2_PIX_FMT_RGB565X:
1067 case V4L2_PIX_FMT_RGB444:
1068 case V4L2_PIX_FMT_Y4:
1069 case V4L2_PIX_FMT_Y6:
1070 case V4L2_PIX_FMT_Y10:
1071 case V4L2_PIX_FMT_Y12:
1072 case V4L2_PIX_FMT_Y10BPACK:
1073 case V4L2_PIX_FMT_YUV555:
1074 case V4L2_PIX_FMT_YUV565:
1075 case V4L2_PIX_FMT_YUV32:
1076 case V4L2_PIX_FMT_NV12MT_16X16:
1077 case V4L2_PIX_FMT_NV42:
1078 case V4L2_PIX_FMT_H264_MVC:
1079 rank = RGB_ODD_BASE_RANK;
1082 case V4L2_PIX_FMT_RGB24:
1083 case V4L2_PIX_FMT_BGR24:
1084 rank = RGB_BASE_RANK - 1;
1087 case V4L2_PIX_FMT_RGB32:
1088 case V4L2_PIX_FMT_BGR32:
1089 case V4L2_PIX_FMT_ABGR32:
1090 case V4L2_PIX_FMT_XBGR32:
1091 case V4L2_PIX_FMT_BGRA32:
1092 case V4L2_PIX_FMT_BGRX32:
1093 case V4L2_PIX_FMT_RGBA32:
1094 case V4L2_PIX_FMT_RGBX32:
1095 case V4L2_PIX_FMT_ARGB32:
1096 case V4L2_PIX_FMT_XRGB32:
1097 rank = RGB_BASE_RANK;
1100 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1101 case V4L2_PIX_FMT_INVZ:
1102 rank = GREY_BASE_RANK;
1105 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
1106 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1107 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1108 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1109 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1110 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1111 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1112 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1113 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1114 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1115 case V4L2_PIX_FMT_MM21: /* NV12 Y 16x32, UV 16x16 tile */
1116 case V4L2_PIX_FMT_NV12M_8L128:
1117 case V4L2_PIX_FMT_NV12M_10BE_8L128:
1118 rank = YUV_ODD_BASE_RANK;
1121 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1122 rank = YUV_BASE_RANK + 3;
1124 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1125 rank = YUV_BASE_RANK + 2;
1127 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1128 case V4L2_PIX_FMT_YUV420M:
1129 rank = YUV_BASE_RANK + 7;
1131 case V4L2_PIX_FMT_NV12: /* Y/CbCr 4:2:0, 12 bits per pixel */
1132 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
1133 rank = YUV_BASE_RANK + 8;
1135 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1136 rank = YUV_BASE_RANK + 10;
1138 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1139 case V4L2_PIX_FMT_YVU420M:
1140 rank = YUV_BASE_RANK + 6;
1142 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1143 rank = YUV_BASE_RANK + 9;
1145 case V4L2_PIX_FMT_YUV444:
1146 rank = YUV_BASE_RANK + 6;
1148 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1149 rank = YUV_BASE_RANK + 5;
1151 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1152 rank = YUV_BASE_RANK + 4;
1154 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1155 rank = YUV_BASE_RANK + 8;
1158 case V4L2_PIX_FMT_DV:
1159 rank = DV_BASE_RANK;
1162 case V4L2_PIX_FMT_WNVA: /* Winnov hw compress */
1166 case V4L2_PIX_FMT_SBGGR8:
1167 case V4L2_PIX_FMT_SGBRG8:
1168 case V4L2_PIX_FMT_SGRBG8:
1169 case V4L2_PIX_FMT_SRGGB8:
1170 rank = BAYER_BASE_RANK;
1173 case V4L2_PIX_FMT_SN9C10X:
1174 rank = S910_BASE_RANK;
1177 case V4L2_PIX_FMT_PWC1:
1178 rank = PWC_BASE_RANK;
1180 case V4L2_PIX_FMT_PWC2:
1181 rank = PWC_BASE_RANK;
1189 /* All ranks are below 1<<15 so a shift by 15
1190 * will a) make all non-emulated formats larger
1191 * than emulated and b) will not overflow
1202 format_cmp_func (gconstpointer a, gconstpointer b)
1204 const struct v4l2_fmtdesc *fa = a;
1205 const struct v4l2_fmtdesc *fb = b;
1207 if (fa->pixelformat == fb->pixelformat)
1210 return gst_v4l2_object_format_get_rank (fb) -
1211 gst_v4l2_object_format_get_rank (fa);
1214 /******************************************************
1215 * gst_v4l2_object_fill_format_list():
1216 * create list of supported capture formats
1217 * return value: TRUE on success, FALSE on error
1218 ******************************************************/
1220 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1221 enum v4l2_buf_type type)
1224 struct v4l2_fmtdesc *format;
1226 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting src format enumerations");
1228 /* format enumeration */
1230 format = g_new0 (struct v4l2_fmtdesc, 1);
1233 format->type = type;
1235 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1236 if (errno == EINVAL) {
1238 break; /* end of enumeration */
1244 GST_LOG_OBJECT (v4l2object->dbg_obj, "index: %u", format->index);
1245 GST_LOG_OBJECT (v4l2object->dbg_obj, "type: %d", format->type);
1246 GST_LOG_OBJECT (v4l2object->dbg_obj, "flags: %08x", format->flags);
1247 GST_LOG_OBJECT (v4l2object->dbg_obj, "description: '%s'",
1248 format->description);
1249 GST_LOG_OBJECT (v4l2object->dbg_obj, "pixelformat: %" GST_FOURCC_FORMAT,
1250 GST_FOURCC_ARGS (format->pixelformat));
1252 /* sort formats according to our preference; we do this, because caps
1253 * are probed in the order the formats are in the list, and the order of
1254 * formats in the final probed caps matters for things like fixation */
1255 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1256 (GCompareFunc) format_cmp_func);
1259 #ifndef GST_DISABLE_GST_DEBUG
1263 GST_INFO_OBJECT (v4l2object->dbg_obj, "got %d format(s):", n);
1264 for (l = v4l2object->formats; l != NULL; l = l->next) {
1267 GST_INFO_OBJECT (v4l2object->dbg_obj,
1268 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1269 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1281 if (v4l2object->element)
1284 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1285 (_("Failed to enumerate possible video formats device '%s' can work "
1286 "with"), v4l2object->videodev),
1287 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1288 n, v4l2object->videodev, errno, g_strerror (errno)));
1295 * Get the list of supported capture formats, a list of
1296 * `struct v4l2_fmtdesc`.
1299 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1301 if (!v4l2object->formats) {
1303 /* check usual way */
1304 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1306 /* if our driver supports multi-planar
1307 * and if formats are still empty then we can workaround driver bug
1308 * by also looking up formats as if our device was not supporting
1310 if (!v4l2object->formats) {
1311 switch (v4l2object->type) {
1312 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1313 gst_v4l2_object_fill_format_list (v4l2object,
1314 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1317 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1318 gst_v4l2_object_fill_format_list (v4l2object,
1319 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1327 return v4l2object->formats;
1330 static GstVideoFormat
1331 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1333 GstVideoFormat format;
1336 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1337 format = GST_VIDEO_FORMAT_GRAY8;
1339 case V4L2_PIX_FMT_Y16:
1340 format = GST_VIDEO_FORMAT_GRAY16_LE;
1342 case V4L2_PIX_FMT_Y16_BE:
1343 format = GST_VIDEO_FORMAT_GRAY16_BE;
1345 case V4L2_PIX_FMT_XRGB555:
1346 case V4L2_PIX_FMT_RGB555:
1347 format = GST_VIDEO_FORMAT_RGB15;
1349 case V4L2_PIX_FMT_XRGB555X:
1350 case V4L2_PIX_FMT_RGB555X:
1351 format = GST_VIDEO_FORMAT_BGR15;
1353 case V4L2_PIX_FMT_RGB565:
1354 format = GST_VIDEO_FORMAT_RGB16;
1356 case V4L2_PIX_FMT_RGB24:
1357 format = GST_VIDEO_FORMAT_RGB;
1359 case V4L2_PIX_FMT_BGR24:
1360 format = GST_VIDEO_FORMAT_BGR;
1362 case V4L2_PIX_FMT_XRGB32:
1363 case V4L2_PIX_FMT_RGB32:
1364 format = GST_VIDEO_FORMAT_xRGB;
1366 case V4L2_PIX_FMT_RGBX32:
1367 format = GST_VIDEO_FORMAT_RGBx;
1369 case V4L2_PIX_FMT_XBGR32:
1370 case V4L2_PIX_FMT_BGR32:
1371 format = GST_VIDEO_FORMAT_BGRx;
1373 case V4L2_PIX_FMT_BGRX32:
1374 format = GST_VIDEO_FORMAT_xBGR;
1376 case V4L2_PIX_FMT_ABGR32:
1377 format = GST_VIDEO_FORMAT_BGRA;
1379 case V4L2_PIX_FMT_BGRA32:
1380 format = GST_VIDEO_FORMAT_ABGR;
1382 case V4L2_PIX_FMT_RGBA32:
1383 format = GST_VIDEO_FORMAT_RGBA;
1385 case V4L2_PIX_FMT_ARGB32:
1386 format = GST_VIDEO_FORMAT_ARGB;
1388 case V4L2_PIX_FMT_NV12:
1389 case V4L2_PIX_FMT_NV12M:
1390 format = GST_VIDEO_FORMAT_NV12;
1392 case V4L2_PIX_FMT_NV12MT:
1393 format = GST_VIDEO_FORMAT_NV12_64Z32;
1395 case V4L2_PIX_FMT_MM21:
1396 format = GST_VIDEO_FORMAT_NV12_16L32S;
1398 case V4L2_PIX_FMT_NV12M_8L128:
1399 format = GST_VIDEO_FORMAT_NV12_8L128;
1401 case V4L2_PIX_FMT_NV12M_10BE_8L128:
1402 format = GST_VIDEO_FORMAT_NV12_10BE_8L128;
1404 case V4L2_PIX_FMT_NV21:
1405 case V4L2_PIX_FMT_NV21M:
1406 format = GST_VIDEO_FORMAT_NV21;
1408 case V4L2_PIX_FMT_YVU410:
1409 format = GST_VIDEO_FORMAT_YVU9;
1411 case V4L2_PIX_FMT_YUV410:
1412 format = GST_VIDEO_FORMAT_YUV9;
1414 case V4L2_PIX_FMT_YUV420:
1415 case V4L2_PIX_FMT_YUV420M:
1416 format = GST_VIDEO_FORMAT_I420;
1418 case V4L2_PIX_FMT_YUYV:
1419 format = GST_VIDEO_FORMAT_YUY2;
1421 case V4L2_PIX_FMT_YVU420:
1422 case V4L2_PIX_FMT_YVU420M:
1423 format = GST_VIDEO_FORMAT_YV12;
1425 case V4L2_PIX_FMT_UYVY:
1426 format = GST_VIDEO_FORMAT_UYVY;
1428 case V4L2_PIX_FMT_YUV411P:
1429 format = GST_VIDEO_FORMAT_Y41B;
1431 case V4L2_PIX_FMT_YUV422P:
1432 format = GST_VIDEO_FORMAT_Y42B;
1434 case V4L2_PIX_FMT_YVYU:
1435 format = GST_VIDEO_FORMAT_YVYU;
1437 case V4L2_PIX_FMT_NV16:
1438 case V4L2_PIX_FMT_NV16M:
1439 format = GST_VIDEO_FORMAT_NV16;
1441 case V4L2_PIX_FMT_NV61:
1442 case V4L2_PIX_FMT_NV61M:
1443 format = GST_VIDEO_FORMAT_NV61;
1445 case V4L2_PIX_FMT_NV24:
1446 format = GST_VIDEO_FORMAT_NV24;
1448 case V4L2_PIX_FMT_INVZ:
1449 format = GST_VIDEO_FORMAT_INVZ;
1452 format = GST_VIDEO_FORMAT_UNKNOWN;
1460 gst_v4l2_object_v4l2fourcc_is_rgb (guint32 fourcc)
1462 gboolean ret = FALSE;
1465 case V4L2_PIX_FMT_XRGB555:
1466 case V4L2_PIX_FMT_RGB555:
1467 case V4L2_PIX_FMT_XRGB555X:
1468 case V4L2_PIX_FMT_RGB555X:
1469 case V4L2_PIX_FMT_RGB565:
1470 case V4L2_PIX_FMT_RGB24:
1471 case V4L2_PIX_FMT_BGR24:
1472 case V4L2_PIX_FMT_XRGB32:
1473 case V4L2_PIX_FMT_RGB32:
1474 case V4L2_PIX_FMT_RGBA32:
1475 case V4L2_PIX_FMT_RGBX32:
1476 case V4L2_PIX_FMT_XBGR32:
1477 case V4L2_PIX_FMT_BGR32:
1478 case V4L2_PIX_FMT_BGRA32:
1479 case V4L2_PIX_FMT_BGRX32:
1480 case V4L2_PIX_FMT_ABGR32:
1481 case V4L2_PIX_FMT_ARGB32:
1482 case V4L2_PIX_FMT_SBGGR8:
1483 case V4L2_PIX_FMT_SGBRG8:
1484 case V4L2_PIX_FMT_SGRBG8:
1485 case V4L2_PIX_FMT_SRGGB8:
1495 static GstStructure *
1496 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1498 GstStructure *structure = NULL;
1501 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1502 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1503 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1504 structure = gst_structure_new ("image/jpeg",
1505 "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
1507 case V4L2_PIX_FMT_MPEG1:
1508 structure = gst_structure_new ("video/mpeg",
1509 "mpegversion", G_TYPE_INT, 1, NULL);
1511 case V4L2_PIX_FMT_MPEG2:
1512 structure = gst_structure_new ("video/mpeg",
1513 "mpegversion", G_TYPE_INT, 2, NULL);
1515 case V4L2_PIX_FMT_MPEG4:
1516 case V4L2_PIX_FMT_XVID:
1517 structure = gst_structure_new ("video/mpeg",
1518 "mpegversion", G_TYPE_INT, 4, "systemstream",
1519 G_TYPE_BOOLEAN, FALSE, NULL);
1521 case V4L2_PIX_FMT_FWHT:
1522 structure = gst_structure_new_empty ("video/x-fwht");
1524 case V4L2_PIX_FMT_H263:
1525 structure = gst_structure_new ("video/x-h263",
1526 "variant", G_TYPE_STRING, "itu", NULL);
1528 case V4L2_PIX_FMT_H264: /* H.264 */
1529 structure = gst_structure_new ("video/x-h264",
1530 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1531 G_TYPE_STRING, "au", NULL);
1533 case V4L2_PIX_FMT_H264_NO_SC:
1534 structure = gst_structure_new ("video/x-h264",
1535 "stream-format", G_TYPE_STRING, "avc", "alignment",
1536 G_TYPE_STRING, "au", NULL);
1538 case V4L2_PIX_FMT_HEVC: /* H.265 */
1539 structure = gst_structure_new ("video/x-h265",
1540 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1541 G_TYPE_STRING, "au", NULL);
1543 case V4L2_PIX_FMT_VC1_ANNEX_G:
1544 case V4L2_PIX_FMT_VC1_ANNEX_L:
1545 structure = gst_structure_new ("video/x-wmv",
1546 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1548 case V4L2_PIX_FMT_VP8:
1549 structure = gst_structure_new_empty ("video/x-vp8");
1551 case V4L2_PIX_FMT_VP9:
1552 structure = gst_structure_new_empty ("video/x-vp9");
1554 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1555 case V4L2_PIX_FMT_Y16:
1556 case V4L2_PIX_FMT_Y16_BE:
1557 case V4L2_PIX_FMT_XRGB555:
1558 case V4L2_PIX_FMT_RGB555:
1559 case V4L2_PIX_FMT_XRGB555X:
1560 case V4L2_PIX_FMT_RGB555X:
1561 case V4L2_PIX_FMT_RGB565:
1562 case V4L2_PIX_FMT_RGB24:
1563 case V4L2_PIX_FMT_BGR24:
1564 case V4L2_PIX_FMT_RGB32:
1565 case V4L2_PIX_FMT_XRGB32:
1566 case V4L2_PIX_FMT_ARGB32:
1567 case V4L2_PIX_FMT_RGBX32:
1568 case V4L2_PIX_FMT_RGBA32:
1569 case V4L2_PIX_FMT_BGR32:
1570 case V4L2_PIX_FMT_BGRX32:
1571 case V4L2_PIX_FMT_BGRA32:
1572 case V4L2_PIX_FMT_XBGR32:
1573 case V4L2_PIX_FMT_ABGR32:
1574 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1575 case V4L2_PIX_FMT_NV12M:
1576 case V4L2_PIX_FMT_NV12MT:
1577 case V4L2_PIX_FMT_MM21:
1578 case V4L2_PIX_FMT_NV12M_8L128:
1579 case V4L2_PIX_FMT_NV12M_10BE_8L128:
1580 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1581 case V4L2_PIX_FMT_NV21M:
1582 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1583 case V4L2_PIX_FMT_NV16M:
1584 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1585 case V4L2_PIX_FMT_NV61M:
1586 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1587 case V4L2_PIX_FMT_YVU410:
1588 case V4L2_PIX_FMT_YUV410:
1589 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1590 case V4L2_PIX_FMT_YUV420M:
1591 case V4L2_PIX_FMT_YUYV:
1592 case V4L2_PIX_FMT_YVU420:
1593 case V4L2_PIX_FMT_YVU420M:
1594 case V4L2_PIX_FMT_UYVY:
1595 case V4L2_PIX_FMT_YUV422P:
1596 case V4L2_PIX_FMT_YVYU:
1597 case V4L2_PIX_FMT_YUV411P:
1598 case V4L2_PIX_FMT_INVZ:{
1599 GstVideoFormat format;
1600 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1601 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1602 structure = gst_structure_new ("video/x-raw",
1603 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1606 case V4L2_PIX_FMT_DV:
1608 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1611 case V4L2_PIX_FMT_MPEG: /* MPEG */
1612 structure = gst_structure_new ("video/mpegts",
1613 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1615 case V4L2_PIX_FMT_WNVA: /* Winnov hw compress */
1617 case V4L2_PIX_FMT_SBGGR8:
1618 case V4L2_PIX_FMT_SGBRG8:
1619 case V4L2_PIX_FMT_SGRBG8:
1620 case V4L2_PIX_FMT_SRGGB8:
1621 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1622 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1623 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1624 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1625 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1627 case V4L2_PIX_FMT_SN9C10X:
1628 structure = gst_structure_new_empty ("video/x-sonix");
1630 case V4L2_PIX_FMT_PWC1:
1631 structure = gst_structure_new_empty ("video/x-pwc1");
1633 case V4L2_PIX_FMT_PWC2:
1634 structure = gst_structure_new_empty ("video/x-pwc2");
1636 case V4L2_PIX_FMT_RGB332:
1637 case V4L2_PIX_FMT_BGR666:
1638 case V4L2_PIX_FMT_ARGB555X:
1639 case V4L2_PIX_FMT_RGB565X:
1640 case V4L2_PIX_FMT_RGB444:
1641 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1642 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1643 case V4L2_PIX_FMT_Y4:
1644 case V4L2_PIX_FMT_Y6:
1645 case V4L2_PIX_FMT_Y10:
1646 case V4L2_PIX_FMT_Y12:
1647 case V4L2_PIX_FMT_Y10BPACK:
1648 case V4L2_PIX_FMT_YUV444:
1649 case V4L2_PIX_FMT_YUV555:
1650 case V4L2_PIX_FMT_YUV565:
1651 case V4L2_PIX_FMT_Y41P:
1652 case V4L2_PIX_FMT_YUV32:
1653 case V4L2_PIX_FMT_NV12MT_16X16:
1654 case V4L2_PIX_FMT_NV42:
1655 case V4L2_PIX_FMT_H264_MVC:
1657 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1658 fourcc, GST_FOURCC_ARGS (fourcc));
1666 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1668 GstStructure *template;
1671 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1673 if (template == NULL)
1676 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1677 if (gst_v4l2_formats[i].format != fourcc)
1680 if (gst_v4l2_formats[i].dimensions) {
1681 gst_structure_set (template,
1682 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1683 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1684 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1694 gst_v4l2_object_is_raw (GstV4l2Object * v4l2object)
1698 if (GST_VIDEO_INFO_FORMAT (&v4l2object->info) != GST_VIDEO_FORMAT_ENCODED)
1701 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1702 if (gst_v4l2_formats[i].format == GST_V4L2_PIXELFORMAT (v4l2object)) {
1703 return ! !(gst_v4l2_formats[i].flags & GST_V4L2_RAW);
1709 /* Add an 'alternate' variant of the caps with the feature */
1711 add_alternate_variant (GstV4l2Object * v4l2object, GstCaps * caps,
1712 GstStructure * structure)
1714 GstStructure *alt_s;
1716 if (v4l2object && v4l2object->never_interlaced)
1719 if (!gst_structure_has_name (structure, "video/x-raw"))
1722 alt_s = gst_structure_copy (structure);
1723 gst_structure_set (alt_s, "interlace-mode", G_TYPE_STRING, "alternate", NULL);
1725 gst_caps_append_structure_full (caps, alt_s,
1726 gst_caps_features_new (GST_CAPS_FEATURE_FORMAT_INTERLACED, NULL));
1730 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1732 GstStructure *structure;
1733 GstCaps *caps, *caps_interlaced;
1736 caps = gst_caps_new_empty ();
1737 caps_interlaced = gst_caps_new_empty ();
1738 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1740 if ((gst_v4l2_formats[i].flags & flags) == 0)
1744 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1747 GstStructure *alt_s = NULL;
1749 if (gst_v4l2_formats[i].dimensions) {
1750 gst_structure_set (structure,
1751 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1752 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1753 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1756 switch (gst_v4l2_formats[i].format) {
1757 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
1758 case V4L2_PIX_FMT_YUV420:
1759 alt_s = gst_structure_copy (structure);
1760 gst_structure_set (alt_s, "format", G_TYPE_STRING, "S420", NULL);
1762 case V4L2_PIX_FMT_NV12:
1763 alt_s = gst_structure_copy (structure);
1764 gst_structure_set (alt_s, "format", G_TYPE_STRING, "SN12", NULL);
1766 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
1767 case V4L2_PIX_FMT_RGB32:
1768 alt_s = gst_structure_copy (structure);
1769 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1771 case V4L2_PIX_FMT_BGR32:
1772 alt_s = gst_structure_copy (structure);
1773 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1778 gst_caps_append_structure (caps, structure);
1781 gst_caps_append_structure (caps, alt_s);
1782 add_alternate_variant (NULL, caps_interlaced, alt_s);
1785 add_alternate_variant (NULL, caps_interlaced, structure);
1789 caps = gst_caps_simplify (caps);
1790 caps_interlaced = gst_caps_simplify (caps_interlaced);
1792 return gst_caps_merge (caps, caps_interlaced);
1796 gst_v4l2_object_get_all_caps (void)
1798 static GstCaps *caps = NULL;
1800 if (g_once_init_enter (&caps)) {
1801 GstCaps *all_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1802 GST_MINI_OBJECT_FLAG_SET (all_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1803 g_once_init_leave (&caps, all_caps);
1810 gst_v4l2_object_get_raw_caps (void)
1812 static GstCaps *caps = NULL;
1814 if (g_once_init_enter (&caps)) {
1815 GstCaps *raw_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1816 GST_MINI_OBJECT_FLAG_SET (raw_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1817 g_once_init_leave (&caps, raw_caps);
1824 gst_v4l2_object_get_codec_caps (void)
1826 static GstCaps *caps = NULL;
1828 if (g_once_init_enter (&caps)) {
1829 GstCaps *codec_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1830 GST_MINI_OBJECT_FLAG_SET (codec_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1831 g_once_init_leave (&caps, codec_caps);
1837 /* collect data for the given caps
1838 * @caps: given input caps
1839 * @format: location for the v4l format
1840 * @w/@h: location for width and height
1841 * @fps_n/@fps_d: location for framerate
1842 * @size: location for expected size of the frame or 0 if unknown
1845 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1846 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1848 GstStructure *structure;
1849 guint32 fourcc = 0, fourcc_nc = 0;
1850 const gchar *mimetype;
1851 struct v4l2_fmtdesc *fmt = NULL;
1853 structure = gst_caps_get_structure (caps, 0);
1855 mimetype = gst_structure_get_name (structure);
1857 if (!gst_video_info_from_caps (info, caps))
1858 goto invalid_format;
1860 if (g_str_equal (mimetype, "video/x-raw")) {
1861 switch (GST_VIDEO_INFO_FORMAT (info)) {
1862 case GST_VIDEO_FORMAT_I420:
1863 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
1864 case GST_VIDEO_FORMAT_S420:
1865 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
1866 fourcc = V4L2_PIX_FMT_YUV420;
1867 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1869 case GST_VIDEO_FORMAT_YUY2:
1870 fourcc = V4L2_PIX_FMT_YUYV;
1872 case GST_VIDEO_FORMAT_UYVY:
1873 fourcc = V4L2_PIX_FMT_UYVY;
1875 case GST_VIDEO_FORMAT_YV12:
1876 fourcc = V4L2_PIX_FMT_YVU420;
1877 fourcc_nc = V4L2_PIX_FMT_YVU420M;
1879 case GST_VIDEO_FORMAT_Y41B:
1880 fourcc = V4L2_PIX_FMT_YUV411P;
1882 case GST_VIDEO_FORMAT_Y42B:
1883 fourcc = V4L2_PIX_FMT_YUV422P;
1885 case GST_VIDEO_FORMAT_NV12:
1886 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
1887 case GST_VIDEO_FORMAT_SN12:
1888 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
1889 fourcc = V4L2_PIX_FMT_NV12;
1890 fourcc_nc = V4L2_PIX_FMT_NV12M;
1892 case GST_VIDEO_FORMAT_NV12_64Z32:
1893 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1895 case GST_VIDEO_FORMAT_NV12_16L32S:
1896 fourcc_nc = V4L2_PIX_FMT_MM21;
1898 case GST_VIDEO_FORMAT_NV12_8L128:
1899 fourcc_nc = V4L2_PIX_FMT_NV12M_8L128;
1901 case GST_VIDEO_FORMAT_NV12_10BE_8L128:
1902 fourcc_nc = V4L2_PIX_FMT_NV12M_10BE_8L128;
1904 case GST_VIDEO_FORMAT_NV21:
1905 fourcc = V4L2_PIX_FMT_NV21;
1906 fourcc_nc = V4L2_PIX_FMT_NV21M;
1908 case GST_VIDEO_FORMAT_NV16:
1909 fourcc = V4L2_PIX_FMT_NV16;
1910 fourcc_nc = V4L2_PIX_FMT_NV16M;
1912 case GST_VIDEO_FORMAT_NV61:
1913 fourcc = V4L2_PIX_FMT_NV61;
1914 fourcc_nc = V4L2_PIX_FMT_NV61M;
1916 case GST_VIDEO_FORMAT_NV24:
1917 fourcc = V4L2_PIX_FMT_NV24;
1919 case GST_VIDEO_FORMAT_YVYU:
1920 fourcc = V4L2_PIX_FMT_YVYU;
1922 case GST_VIDEO_FORMAT_RGB15:
1923 fourcc = V4L2_PIX_FMT_RGB555;
1924 fourcc_nc = V4L2_PIX_FMT_XRGB555;
1926 case GST_VIDEO_FORMAT_RGB16:
1927 fourcc = V4L2_PIX_FMT_RGB565;
1929 case GST_VIDEO_FORMAT_RGB:
1930 fourcc = V4L2_PIX_FMT_RGB24;
1932 case GST_VIDEO_FORMAT_BGR:
1933 fourcc = V4L2_PIX_FMT_BGR24;
1935 case GST_VIDEO_FORMAT_xRGB:
1936 fourcc = V4L2_PIX_FMT_RGB32;
1937 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1939 case GST_VIDEO_FORMAT_RGBx:
1940 fourcc = V4L2_PIX_FMT_RGBX32;
1942 case GST_VIDEO_FORMAT_ARGB:
1943 fourcc = V4L2_PIX_FMT_RGB32;
1944 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1946 case GST_VIDEO_FORMAT_RGBA:
1947 fourcc = V4L2_PIX_FMT_RGBA32;
1949 case GST_VIDEO_FORMAT_BGRx:
1950 fourcc = V4L2_PIX_FMT_BGR32;
1951 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1953 case GST_VIDEO_FORMAT_xBGR:
1954 fourcc = V4L2_PIX_FMT_BGRX32;
1956 case GST_VIDEO_FORMAT_BGRA:
1957 fourcc = V4L2_PIX_FMT_BGR32;
1958 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1960 case GST_VIDEO_FORMAT_ABGR:
1961 fourcc = V4L2_PIX_FMT_BGRA32;
1963 case GST_VIDEO_FORMAT_GRAY8:
1964 fourcc = V4L2_PIX_FMT_GREY;
1966 case GST_VIDEO_FORMAT_GRAY16_LE:
1967 fourcc = V4L2_PIX_FMT_Y16;
1969 case GST_VIDEO_FORMAT_GRAY16_BE:
1970 fourcc = V4L2_PIX_FMT_Y16_BE;
1972 case GST_VIDEO_FORMAT_BGR15:
1973 fourcc = V4L2_PIX_FMT_RGB555X;
1974 fourcc_nc = V4L2_PIX_FMT_XRGB555X;
1976 case GST_VIDEO_FORMAT_INVZ:
1977 fourcc = V4L2_PIX_FMT_INVZ;
1983 if (g_str_equal (mimetype, "video/mpegts")) {
1984 fourcc = V4L2_PIX_FMT_MPEG;
1985 } else if (g_str_equal (mimetype, "video/x-dv")) {
1986 fourcc = V4L2_PIX_FMT_DV;
1987 } else if (g_str_equal (mimetype, "image/jpeg")) {
1988 fourcc = V4L2_PIX_FMT_JPEG;
1989 } else if (g_str_equal (mimetype, "video/mpeg")) {
1991 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1994 fourcc = V4L2_PIX_FMT_MPEG1;
1997 fourcc = V4L2_PIX_FMT_MPEG2;
2000 fourcc = V4L2_PIX_FMT_MPEG4;
2001 fourcc_nc = V4L2_PIX_FMT_XVID;
2007 } else if (g_str_equal (mimetype, "video/x-fwht")) {
2008 fourcc = V4L2_PIX_FMT_FWHT;
2009 } else if (g_str_equal (mimetype, "video/x-h263")) {
2010 fourcc = V4L2_PIX_FMT_H263;
2011 } else if (g_str_equal (mimetype, "video/x-h264")) {
2012 const gchar *stream_format =
2013 gst_structure_get_string (structure, "stream-format");
2014 if (g_str_equal (stream_format, "avc"))
2015 fourcc = V4L2_PIX_FMT_H264_NO_SC;
2017 fourcc = V4L2_PIX_FMT_H264;
2018 } else if (g_str_equal (mimetype, "video/x-h265")) {
2019 fourcc = V4L2_PIX_FMT_HEVC;
2020 } else if (g_str_equal (mimetype, "video/x-vp8")) {
2021 fourcc = V4L2_PIX_FMT_VP8;
2022 } else if (g_str_equal (mimetype, "video/x-vp9")) {
2023 fourcc = V4L2_PIX_FMT_VP9;
2024 } else if (g_str_equal (mimetype, "video/x-bayer")) {
2025 const gchar *format = gst_structure_get_string (structure, "format");
2027 if (!g_ascii_strcasecmp (format, "bggr"))
2028 fourcc = V4L2_PIX_FMT_SBGGR8;
2029 else if (!g_ascii_strcasecmp (format, "gbrg"))
2030 fourcc = V4L2_PIX_FMT_SGBRG8;
2031 else if (!g_ascii_strcasecmp (format, "grbg"))
2032 fourcc = V4L2_PIX_FMT_SGRBG8;
2033 else if (!g_ascii_strcasecmp (format, "rggb"))
2034 fourcc = V4L2_PIX_FMT_SRGGB8;
2036 } else if (g_str_equal (mimetype, "video/x-sonix")) {
2037 fourcc = V4L2_PIX_FMT_SN9C10X;
2038 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
2039 fourcc = V4L2_PIX_FMT_PWC1;
2040 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
2041 fourcc = V4L2_PIX_FMT_PWC2;
2046 /* Prefer the non-contiguous if supported */
2047 v4l2object->prefered_non_contiguous = TRUE;
2050 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
2051 else if (fourcc == 0)
2052 goto unhandled_format;
2055 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
2056 v4l2object->prefered_non_contiguous = FALSE;
2060 goto unsupported_format;
2069 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "invalid format");
2074 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "unhandled format");
2079 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "unsupported format");
2085 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2086 guint32 pixelformat, gint * width, gint * height);
2089 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
2091 if (v4l2object->keep_aspect && v4l2object->par)
2092 gst_structure_set_value (s, "pixel-aspect-ratio", v4l2object->par);
2095 /* returns TRUE if the value was changed in place, otherwise FALSE */
2097 gst_v4l2src_value_simplify (GValue * val)
2099 /* simplify list of one value to one value */
2100 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
2101 const GValue *list_val;
2102 GValue new_val = G_VALUE_INIT;
2104 list_val = gst_value_list_get_value (val, 0);
2105 g_value_init (&new_val, G_VALUE_TYPE (list_val));
2106 g_value_copy (list_val, &new_val);
2107 g_value_unset (val);
2116 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
2117 GstVideoInterlaceMode * interlace_mode)
2120 case V4L2_FIELD_ANY:
2122 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
2124 case V4L2_FIELD_NONE:
2125 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
2127 case V4L2_FIELD_INTERLACED:
2128 case V4L2_FIELD_INTERLACED_TB:
2129 case V4L2_FIELD_INTERLACED_BT:
2130 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
2132 case V4L2_FIELD_ALTERNATE:
2133 *interlace_mode = GST_VIDEO_INTERLACE_MODE_ALTERNATE;
2136 GST_ERROR ("Unknown enum v4l2_field %d", field);
2142 gst_v4l2_object_get_colorspace (GstV4l2Object * v4l2object,
2143 struct v4l2_format *fmt, GstVideoColorimetry * cinfo)
2146 gst_v4l2_object_v4l2fourcc_is_rgb (fmt->fmt.pix.pixelformat);
2147 enum v4l2_colorspace colorspace;
2148 enum v4l2_quantization range;
2149 enum v4l2_ycbcr_encoding matrix;
2150 enum v4l2_xfer_func transfer;
2151 gboolean ret = TRUE;
2153 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
2154 colorspace = fmt->fmt.pix_mp.colorspace;
2155 range = fmt->fmt.pix_mp.quantization;
2156 matrix = fmt->fmt.pix_mp.ycbcr_enc;
2157 transfer = fmt->fmt.pix_mp.xfer_func;
2159 colorspace = fmt->fmt.pix.colorspace;
2160 range = fmt->fmt.pix.quantization;
2161 matrix = fmt->fmt.pix.ycbcr_enc;
2162 transfer = fmt->fmt.pix.xfer_func;
2165 /* First step, set the defaults for each primaries */
2166 switch (colorspace) {
2167 case V4L2_COLORSPACE_SMPTE170M:
2168 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2169 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2170 cinfo->transfer = GST_VIDEO_TRANSFER_BT601;
2171 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
2173 case V4L2_COLORSPACE_REC709:
2174 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2175 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2176 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2177 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
2179 case V4L2_COLORSPACE_SRGB:
2180 case V4L2_COLORSPACE_JPEG:
2181 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2182 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2183 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2184 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
2186 case V4L2_COLORSPACE_OPRGB:
2187 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2188 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2189 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2190 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
2192 case V4L2_COLORSPACE_BT2020:
2193 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2194 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2195 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2196 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
2198 case V4L2_COLORSPACE_SMPTE240M:
2199 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2200 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2201 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2202 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
2204 case V4L2_COLORSPACE_470_SYSTEM_M:
2205 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2206 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2207 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2208 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
2210 case V4L2_COLORSPACE_470_SYSTEM_BG:
2211 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2212 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2213 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2214 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
2216 case V4L2_COLORSPACE_RAW:
2217 /* Explicitly unknown */
2218 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2219 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2220 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2221 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
2224 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
2232 /* Second step, apply any custom variation */
2234 case V4L2_QUANTIZATION_FULL_RANGE:
2235 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2237 case V4L2_QUANTIZATION_LIM_RANGE:
2238 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2240 case V4L2_QUANTIZATION_DEFAULT:
2241 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
2242 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
2243 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2244 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601
2245 || matrix == V4L2_YCBCR_ENC_XV709
2246 || colorspace == V4L2_COLORSPACE_JPEG)
2247 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2249 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2252 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
2253 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2258 case V4L2_YCBCR_ENC_XV601:
2259 case V4L2_YCBCR_ENC_SYCC:
2260 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
2262 case V4L2_YCBCR_ENC_601:
2263 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2265 case V4L2_YCBCR_ENC_XV709:
2266 GST_FIXME ("XV709 not defined, assuming 709");
2268 case V4L2_YCBCR_ENC_709:
2269 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2271 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2272 GST_FIXME ("BT2020 with constant luma is not defined, assuming BT2020");
2274 case V4L2_YCBCR_ENC_BT2020:
2275 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2277 case V4L2_YCBCR_ENC_SMPTE240M:
2278 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2280 case V4L2_YCBCR_ENC_DEFAULT:
2281 /* nothing, just use defaults for colorspace */
2284 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2285 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2289 /* Set identity matrix for R'G'B' formats to avoid creating
2290 * confusion. This though is cosmetic as it's now properly ignored by
2291 * the video info API and videoconvert. */
2293 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2296 case V4L2_XFER_FUNC_709:
2297 if (colorspace == V4L2_COLORSPACE_BT2020 && fmt->fmt.pix.height >= 2160)
2298 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2299 else if (colorspace == V4L2_COLORSPACE_SMPTE170M)
2300 cinfo->transfer = GST_VIDEO_TRANSFER_BT601;
2302 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2304 if (v4l2object->transfer)
2305 cinfo->transfer = v4l2object->transfer;
2307 case V4L2_XFER_FUNC_SRGB:
2308 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2310 case V4L2_XFER_FUNC_OPRGB:
2311 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2313 case V4L2_XFER_FUNC_SMPTE240M:
2314 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2316 case V4L2_XFER_FUNC_NONE:
2317 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2319 case V4L2_XFER_FUNC_SMPTE2084:
2320 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE2084;
2322 case V4L2_XFER_FUNC_DEFAULT:
2323 /* nothing, just use defaults for colorspace */
2326 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2327 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2336 gst_v4l2_object_get_streamparm (GstV4l2Object * v4l2object, GstVideoInfo * info)
2338 struct v4l2_streamparm streamparm;
2339 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
2340 streamparm.type = v4l2object->type;
2341 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_PARM, &streamparm) < 0) {
2342 GST_WARNING_OBJECT (v4l2object->dbg_obj, "VIDIOC_G_PARM failed");
2345 if ((streamparm.parm.capture.timeperframe.numerator != 0)
2346 && (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
2347 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2348 GST_VIDEO_INFO_FPS_N (info) =
2349 streamparm.parm.capture.timeperframe.denominator;
2350 GST_VIDEO_INFO_FPS_D (info) =
2351 streamparm.parm.capture.timeperframe.numerator;
2357 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2358 struct v4l2_format *try_fmt)
2360 int fd = v4l2object->video_fd;
2361 struct v4l2_format fmt;
2364 memcpy (&fmt, try_fmt, sizeof (fmt));
2365 r = v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2367 if (r < 0 && errno == ENOTTY) {
2368 /* The driver might not implement TRY_FMT, in which case we will try
2370 if (GST_V4L2_IS_ACTIVE (v4l2object))
2373 memcpy (&fmt, try_fmt, sizeof (fmt));
2374 r = v4l2object->ioctl (fd, VIDIOC_S_FMT, &fmt);
2376 memcpy (try_fmt, &fmt, sizeof (fmt));
2381 memcpy (try_fmt, &fmt, sizeof (fmt));
2382 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2383 "Unable to try format: %s", g_strerror (errno));
2389 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2390 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2392 struct v4l2_format fmt;
2393 GValue interlace_formats = { 0, };
2394 enum v4l2_field formats[] = { V4L2_FIELD_NONE,
2395 V4L2_FIELD_INTERLACED, V4L2_FIELD_ALTERNATE
2398 GstVideoInterlaceMode interlace_mode, prev = -1;
2400 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2403 if (v4l2object->never_interlaced) {
2404 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2408 g_value_init (&interlace_formats, GST_TYPE_LIST);
2410 /* Try thrice - once for NONE, once for INTERLACED and once for ALTERNATE. */
2411 for (i = 0; i < G_N_ELEMENTS (formats); i++) {
2412 memset (&fmt, 0, sizeof (fmt));
2413 fmt.type = v4l2object->type;
2414 fmt.fmt.pix.width = width;
2415 fmt.fmt.pix.height = height;
2416 fmt.fmt.pix.pixelformat = pixelformat;
2417 fmt.fmt.pix.field = formats[i];
2419 if (fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE)
2420 fmt.fmt.pix.height /= 2;
2422 /* if skip_try_fmt_probes is set it's up to the caller to filter out the
2423 * formats from the formats requested by peer.
2424 * For this negotiation to work with 'alternate' we need the caps to contain
2425 * the feature so we have an intersection with downstream caps.
2427 if (!v4l2object->skip_try_fmt_probes
2428 && gst_v4l2_object_try_fmt (v4l2object, &fmt) != 0)
2431 if (gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)
2432 && prev != interlace_mode) {
2433 GValue interlace_enum = { 0, };
2434 const gchar *mode_string;
2435 g_value_init (&interlace_enum, G_TYPE_STRING);
2436 mode_string = gst_video_interlace_mode_to_string (interlace_mode);
2437 g_value_set_string (&interlace_enum, mode_string);
2438 gst_value_list_append_and_take_value (&interlace_formats,
2440 prev = interlace_mode;
2444 if (gst_v4l2src_value_simplify (&interlace_formats)
2445 || gst_value_list_get_size (&interlace_formats) > 0)
2446 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2448 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2449 "Failed to determine interlace mode");
2455 gst_v4l2_object_fill_colorimetry_list (GValue * list,
2456 GstVideoColorimetry * cinfo)
2458 GValue colorimetry = G_VALUE_INIT;
2461 gboolean found = FALSE;
2463 g_value_init (&colorimetry, G_TYPE_STRING);
2464 g_value_take_string (&colorimetry, gst_video_colorimetry_to_string (cinfo));
2466 /* only insert if no duplicate */
2467 size = gst_value_list_get_size (list);
2468 for (i = 0; i < size; i++) {
2471 tmp = gst_value_list_get_value (list, i);
2472 if (gst_value_compare (&colorimetry, tmp) == GST_VALUE_EQUAL) {
2479 gst_value_list_append_and_take_value (list, &colorimetry);
2481 g_value_unset (&colorimetry);
2485 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2486 guint32 width, guint32 height, guint32 pixelformat)
2488 struct v4l2_format fmt;
2489 GValue list = G_VALUE_INIT;
2490 GstVideoColorimetry cinfo;
2491 enum v4l2_colorspace req_cspace;
2493 #ifdef TIZEN_FEATURE_V4L2_SKIP_ADD_COLORSPACE
2494 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
2495 GST_WARNING_OBJECT (v4l2object->dbg_obj, "skip for output");
2499 memset (&fmt, 0, sizeof (fmt));
2500 fmt.type = v4l2object->type;
2501 fmt.fmt.pix.width = width;
2502 fmt.fmt.pix.height = height;
2503 fmt.fmt.pix.pixelformat = pixelformat;
2505 g_value_init (&list, GST_TYPE_LIST);
2507 /* step 1: get device default colorspace and insert it first as
2508 * it should be the preferred one */
2509 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2510 if (gst_v4l2_object_get_colorspace (v4l2object, &fmt, &cinfo))
2511 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2514 /* step 2: probe all colorspace other than default
2515 * We don't probe all colorspace, range, matrix and transfer combination to
2516 * avoid ioctl flooding which could greatly increase initialization time
2517 * with low-speed devices (UVC...) */
2518 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2519 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++) {
2520 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2521 if (req_cspace == V4L2_COLORSPACE_BT878)
2524 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2525 fmt.fmt.pix_mp.colorspace = req_cspace;
2527 fmt.fmt.pix.colorspace = req_cspace;
2529 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2530 enum v4l2_colorspace colorspace;
2532 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2533 colorspace = fmt.fmt.pix_mp.colorspace;
2535 colorspace = fmt.fmt.pix.colorspace;
2537 if (colorspace == req_cspace) {
2538 if (gst_v4l2_object_get_colorspace (v4l2object, &fmt, &cinfo))
2539 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2540 if (colorspace == V4L2_COLORSPACE_REC709) {
2541 /* support for full-range variants of colorspaces V4L2_COLORSPACE_REC709
2542 * (such as Apple's full-range bt709 variant 1:3:5:1) */
2543 struct v4l2_format alt_fmt;
2544 memcpy (&alt_fmt, &fmt, sizeof (alt_fmt));
2546 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2547 alt_fmt.fmt.pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
2549 alt_fmt.fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
2551 if (gst_v4l2_object_get_colorspace (v4l2object, &alt_fmt, &cinfo))
2552 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2558 if (gst_value_list_get_size (&list) > 0)
2559 gst_structure_take_value (s, "colorimetry", &list);
2561 g_value_unset (&list);
2566 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2567 static GstStructure *
2568 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2569 guint32 pixelformat,
2570 guint32 width, guint32 height, const GstStructure * template)
2572 gint fd = v4l2object->video_fd;
2573 struct v4l2_frmivalenum ival;
2576 GValue rates = { 0, };
2578 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2580 ival.pixel_format = pixelformat;
2582 ival.height = height;
2584 GST_LOG_OBJECT (v4l2object->dbg_obj,
2585 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2586 GST_FOURCC_ARGS (pixelformat));
2588 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2589 * fraction to get framerate */
2590 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2591 goto enum_frameintervals_failed;
2593 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2594 GValue rate = { 0, };
2596 g_value_init (&rates, GST_TYPE_LIST);
2597 g_value_init (&rate, GST_TYPE_FRACTION);
2600 num = ival.discrete.numerator;
2601 denom = ival.discrete.denominator;
2603 if (num > G_MAXINT || denom > G_MAXINT) {
2604 /* let us hope we don't get here... */
2609 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding discrete framerate: %d/%d",
2612 /* swap to get the framerate */
2613 gst_value_set_fraction (&rate, denom, num);
2614 gst_value_list_append_value (&rates, &rate);
2617 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2618 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2619 GValue min = { 0, };
2620 GValue step = { 0, };
2621 GValue max = { 0, };
2622 gboolean added = FALSE;
2623 guint32 minnum, mindenom;
2624 guint32 maxnum, maxdenom;
2626 g_value_init (&rates, GST_TYPE_LIST);
2628 g_value_init (&min, GST_TYPE_FRACTION);
2629 g_value_init (&step, GST_TYPE_FRACTION);
2630 g_value_init (&max, GST_TYPE_FRACTION);
2633 minnum = ival.stepwise.min.numerator;
2634 mindenom = ival.stepwise.min.denominator;
2635 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2639 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise min frame interval: %d/%d",
2641 gst_value_set_fraction (&min, minnum, mindenom);
2644 maxnum = ival.stepwise.max.numerator;
2645 maxdenom = ival.stepwise.max.denominator;
2646 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2651 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise max frame interval: %d/%d",
2653 gst_value_set_fraction (&max, maxnum, maxdenom);
2656 num = ival.stepwise.step.numerator;
2657 denom = ival.stepwise.step.denominator;
2658 if (num > G_MAXINT || denom > G_MAXINT) {
2663 if (num == 0 || denom == 0) {
2664 /* in this case we have a wrong fraction or no step, set the step to max
2665 * so that we only add the min value in the loop below */
2670 /* since we only have gst_value_fraction_subtract and not add, negate the
2672 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise step frame interval: %d/%d",
2674 gst_value_set_fraction (&step, -num, denom);
2676 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2677 GValue rate = { 0, };
2679 num = gst_value_get_fraction_numerator (&min);
2680 denom = gst_value_get_fraction_denominator (&min);
2681 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding stepwise framerate: %d/%d",
2684 /* invert to get the framerate */
2685 g_value_init (&rate, GST_TYPE_FRACTION);
2686 gst_value_set_fraction (&rate, denom, num);
2687 gst_value_list_append_value (&rates, &rate);
2690 /* we're actually adding because step was negated above. This is because
2691 * there is no _add function... */
2692 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2693 GST_WARNING_OBJECT (v4l2object->dbg_obj, "could not step fraction!");
2698 /* no range was added, leave the default range from the template */
2699 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2700 "no range added, leaving default");
2701 g_value_unset (&rates);
2703 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2704 guint32 maxnum, maxdenom;
2706 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2708 num = ival.stepwise.min.numerator;
2709 denom = ival.stepwise.min.denominator;
2710 if (num > G_MAXINT || denom > G_MAXINT) {
2715 maxnum = ival.stepwise.max.numerator;
2716 maxdenom = ival.stepwise.max.denominator;
2717 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2722 GST_LOG_OBJECT (v4l2object->dbg_obj,
2723 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2726 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2732 s = gst_structure_copy (template);
2733 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2734 "height", G_TYPE_INT, (gint) height, NULL);
2736 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2738 if (!v4l2object->skip_try_fmt_probes) {
2739 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2741 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2744 if (G_IS_VALUE (&rates)) {
2745 gst_v4l2src_value_simplify (&rates);
2746 /* only change the framerate on the template when we have a valid probed new
2748 gst_structure_take_value (s, "framerate", &rates);
2749 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2750 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2751 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2757 enum_frameintervals_failed:
2759 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2760 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2761 GST_FOURCC_ARGS (pixelformat), width, height);
2766 /* I don't see how this is actually an error, we ignore the format then */
2767 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2768 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2769 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2775 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2779 gst_structure_get_int (s1, "width", &w1);
2780 gst_structure_get_int (s1, "height", &h1);
2781 gst_structure_get_int (s2, "width", &w2);
2782 gst_structure_get_int (s2, "height", &h2);
2784 /* I think it's safe to assume that this won't overflow for a while */
2785 return ((w2 * h2) - (w1 * h1));
2789 check_alternate_and_append_struct (GstCaps * caps, GstStructure * s)
2793 mode = gst_structure_get_value (s, "interlace-mode");
2797 if (G_VALUE_HOLDS_STRING (mode)) {
2798 /* Add the INTERLACED feature if the mode is alternate */
2799 if (!g_strcmp0 (gst_structure_get_string (s, "interlace-mode"),
2801 GstCapsFeatures *feat;
2803 feat = gst_caps_features_new (GST_CAPS_FEATURE_FORMAT_INTERLACED, NULL);
2804 gst_caps_set_features (caps, gst_caps_get_size (caps) - 1, feat);
2806 } else if (GST_VALUE_HOLDS_LIST (mode)) {
2807 /* If the mode is a list containing alternate, remove it from the list and add a
2808 * variant with interlace-mode=alternate and the INTERLACED feature. */
2809 GValue alter = G_VALUE_INIT;
2810 GValue inter = G_VALUE_INIT;
2812 g_value_init (&alter, G_TYPE_STRING);
2813 g_value_set_string (&alter, "alternate");
2815 /* Cannot use gst_value_can_intersect() as it requires args to have the
2817 if (gst_value_intersect (&inter, mode, &alter)) {
2818 GValue minus_alter = G_VALUE_INIT;
2821 gst_value_subtract (&minus_alter, mode, &alter);
2822 gst_structure_take_value (s, "interlace-mode", &minus_alter);
2824 copy = gst_structure_copy (s);
2825 gst_structure_take_value (copy, "interlace-mode", &inter);
2826 gst_caps_append_structure_full (caps, copy,
2827 gst_caps_features_new (GST_CAPS_FEATURE_FORMAT_INTERLACED, NULL));
2829 g_value_unset (&alter);
2833 gst_caps_append_structure (caps, s);
2837 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2838 guint32 format, GstCaps * caps, GstStructure * s)
2840 GstStructure *alt_s = NULL;
2842 /* Encoded stream on output buffer need to be parsed */
2843 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2844 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2847 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2848 if (format == gst_v4l2_formats[i].format &&
2849 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2850 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2851 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2857 if (v4l2object->has_alpha_component &&
2858 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2859 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2861 case V4L2_PIX_FMT_RGB32:
2862 alt_s = gst_structure_copy (s);
2863 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2865 case V4L2_PIX_FMT_BGR32:
2866 alt_s = gst_structure_copy (s);
2867 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2874 check_alternate_and_append_struct (caps, s);
2877 check_alternate_and_append_struct (caps, alt_s);
2882 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2883 guint32 pixelformat, const GstStructure * template)
2885 GstCaps *ret = gst_caps_new_empty ();
2887 gint fd = v4l2object->video_fd;
2888 struct v4l2_frmsizeenum size;
2889 GList *results = NULL;
2892 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2893 gst_caps_append_structure (ret, gst_structure_copy (template));
2897 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2899 size.pixel_format = pixelformat;
2901 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2902 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2903 GST_FOURCC_ARGS (pixelformat));
2905 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2906 goto enum_framesizes_failed;
2908 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2910 GST_LOG_OBJECT (v4l2object->dbg_obj, "got discrete frame size %dx%d",
2911 size.discrete.width, size.discrete.height);
2913 w = MIN (size.discrete.width, G_MAXINT);
2914 h = MIN (size.discrete.height, G_MAXINT);
2918 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2919 pixelformat, w, h, template);
2922 results = g_list_prepend (results, tmp);
2926 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2927 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2928 "done iterating discrete frame sizes");
2929 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2930 guint32 maxw, maxh, step_w, step_h;
2932 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have stepwise frame sizes:");
2933 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2934 size.stepwise.min_width);
2935 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2936 size.stepwise.min_height);
2937 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2938 size.stepwise.max_width);
2939 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max height: %d",
2940 size.stepwise.max_height);
2941 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step width: %d",
2942 size.stepwise.step_width);
2943 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step height: %d",
2944 size.stepwise.step_height);
2946 step_w = MAX (size.stepwise.step_width, 1);
2947 step_h = MAX (size.stepwise.step_height, 1);
2948 w = MAX (size.stepwise.min_width, step_w);
2949 h = MAX (size.stepwise.min_height, step_h);
2950 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2951 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2953 /* FIXME: check for sanity and that min/max are multiples of the steps */
2955 /* we only query details for the max width/height since it's likely the
2956 * most restricted if there are any resolution-dependent restrictions */
2957 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2958 pixelformat, maxw, maxh, template);
2961 GValue step_range = G_VALUE_INIT;
2963 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2964 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2965 gst_structure_set_value (tmp, "width", &step_range);
2967 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2968 gst_structure_take_value (tmp, "height", &step_range);
2970 /* no point using the results list here, since there's only one struct */
2971 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2973 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2976 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have continuous frame sizes:");
2977 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2978 size.stepwise.min_width);
2979 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2980 size.stepwise.min_height);
2981 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2982 size.stepwise.max_width);
2983 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2984 size.stepwise.max_height);
2986 w = MAX (size.stepwise.min_width, 1);
2987 h = MAX (size.stepwise.min_height, 1);
2988 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2989 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2992 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2995 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2996 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2999 /* no point using the results list here, since there's only one struct */
3000 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
3006 /* we use an intermediary list to store and then sort the results of the
3007 * probing because we can't make any assumptions about the order in which
3008 * the driver will give us the sizes, but we want the final caps to contain
3009 * the results starting with the highest resolution and having the lowest
3010 * resolution last, since order in caps matters for things like fixation. */
3011 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
3012 while (results != NULL) {
3013 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
3015 results = g_list_delete_link (results, results);
3018 if (gst_caps_is_empty (ret))
3019 goto enum_framesizes_no_results;
3024 enum_framesizes_failed:
3026 /* I don't see how this is actually an error */
3027 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3028 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
3029 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
3030 goto default_frame_sizes;
3032 enum_framesizes_no_results:
3034 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
3035 * question doesn't actually support it yet */
3036 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3037 "No results for pixelformat %" GST_FOURCC_FORMAT
3038 " enumerating frame sizes, trying fallback",
3039 GST_FOURCC_ARGS (pixelformat));
3040 goto default_frame_sizes;
3044 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3045 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
3046 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
3047 goto default_frame_sizes;
3050 default_frame_sizes:
3052 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
3054 /* This code is for Linux < 2.6.19 */
3056 max_w = max_h = GST_V4L2_MAX_SIZE;
3057 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
3059 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3060 "Could not probe minimum capture size for pixelformat %"
3061 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
3063 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
3065 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3066 "Could not probe maximum capture size for pixelformat %"
3067 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
3069 if (min_w == 0 || min_h == 0)
3071 if (max_w == 0 || max_h == 0)
3072 max_w = max_h = GST_V4L2_MAX_SIZE;
3074 /* Since we can't get framerate directly, try to use the current norm */
3075 if (v4l2object->tv_norm && v4l2object->norms) {
3077 GstTunerNorm *norm = NULL;
3078 GstTunerNorm *current =
3079 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
3081 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
3082 norm = (GstTunerNorm *) norms->data;
3083 if (!strcmp (norm->label, current->label))
3086 /* If it's possible, set framerate to that (discrete) value */
3088 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
3089 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
3093 tmp = gst_structure_copy (template);
3095 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
3097 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3098 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3099 /* if norm can't be used, copy the template framerate */
3100 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
3105 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
3107 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
3110 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
3112 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
3114 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
3116 /* We could consider setting interlace mode from min and max. */
3117 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
3120 if (!v4l2object->skip_try_fmt_probes) {
3121 gint probed_w, probed_h;
3122 if (v4l2object->info.width >= min_w && v4l2object->info.width <= max_w &&
3123 v4l2object->info.height >= min_h
3124 && v4l2object->info.height <= max_h) {
3125 probed_w = v4l2object->info.width;
3126 probed_h = v4l2object->info.height;
3131 /* We could consider to check colorspace for min too, in case it depends on
3132 * the size. But in this case, min and max could not be enough */
3133 gst_v4l2_object_add_colorspace (v4l2object, tmp, probed_w, probed_h,
3137 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
3143 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
3144 guint32 pixelformat, gint * width, gint * height)
3146 struct v4l2_format fmt;
3147 gboolean ret = FALSE;
3148 GstVideoInterlaceMode interlace_mode;
3150 g_return_val_if_fail (width != NULL, FALSE);
3151 g_return_val_if_fail (height != NULL, FALSE);
3153 GST_LOG_OBJECT (v4l2object->dbg_obj,
3154 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
3155 *width, *height, GST_FOURCC_ARGS (pixelformat));
3157 memset (&fmt, 0, sizeof (struct v4l2_format));
3159 /* get size delimiters */
3160 memset (&fmt, 0, sizeof (fmt));
3161 fmt.type = v4l2object->type;
3162 fmt.fmt.pix.width = *width;
3163 fmt.fmt.pix.height = *height;
3164 fmt.fmt.pix.pixelformat = pixelformat;
3165 fmt.fmt.pix.field = V4L2_FIELD_ANY;
3167 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
3170 GST_LOG_OBJECT (v4l2object->dbg_obj,
3171 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
3173 *width = fmt.fmt.pix.width;
3174 *height = fmt.fmt.pix.height;
3176 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
3177 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3178 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
3179 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
3187 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3188 "Unable to try format: %s", g_strerror (errno));
3195 gst_v4l2_object_is_dmabuf_supported (GstV4l2Object * v4l2object)
3197 gboolean ret = TRUE;
3198 struct v4l2_exportbuffer expbuf = {
3199 .type = v4l2object->type,
3202 .flags = O_CLOEXEC | O_RDWR,
3205 if (v4l2object->fmtdesc &&
3206 v4l2object->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
3207 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3208 "libv4l2 converter detected, disabling DMABuf");
3212 /* Expected to fail, but ENOTTY tells us that it is not implemented. */
3213 v4l2object->ioctl (v4l2object->video_fd, VIDIOC_EXPBUF, &expbuf);
3214 if (errno == ENOTTY)
3221 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
3225 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "initializing the %s system",
3226 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
3228 GST_V4L2_CHECK_OPEN (v4l2object);
3229 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3231 /* find transport */
3232 mode = v4l2object->req_mode;
3234 if (v4l2object->device_caps & V4L2_CAP_READWRITE) {
3235 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
3236 mode = GST_V4L2_IO_RW;
3237 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
3238 goto method_not_supported;
3240 if (v4l2object->device_caps & V4L2_CAP_STREAMING) {
3241 if (v4l2object->req_mode == GST_V4L2_IO_AUTO) {
3242 if (!V4L2_TYPE_IS_OUTPUT (v4l2object->type) &&
3243 gst_v4l2_object_is_dmabuf_supported (v4l2object)) {
3244 mode = GST_V4L2_IO_DMABUF;
3246 mode = GST_V4L2_IO_MMAP;
3249 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP ||
3250 v4l2object->req_mode == GST_V4L2_IO_DMABUF)
3251 goto method_not_supported;
3253 /* if still no transport selected, error out */
3254 if (mode == GST_V4L2_IO_AUTO)
3255 goto no_supported_capture_method;
3257 GST_INFO_OBJECT (v4l2object->dbg_obj, "accessing buffers via mode %d", mode);
3258 v4l2object->mode = mode;
3260 /* If min_buffers is not set, the driver either does not support the control or
3261 it has not been asked yet via propose_allocation/decide_allocation. */
3262 if (!v4l2object->min_buffers)
3263 gst_v4l2_get_driver_min_buffers (v4l2object);
3265 /* Map the buffers */
3266 GST_LOG_OBJECT (v4l2object->dbg_obj, "initiating buffer pool");
3269 GstBufferPool *pool = gst_v4l2_buffer_pool_new (v4l2object, caps);
3270 GST_OBJECT_LOCK (v4l2object->element);
3271 v4l2object->pool = pool;
3272 GST_OBJECT_UNLOCK (v4l2object->element);
3274 goto buffer_pool_new_failed;
3277 GST_V4L2_SET_ACTIVE (v4l2object);
3282 buffer_pool_new_failed:
3284 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
3285 (_("Could not map buffers from device '%s'"),
3286 v4l2object->videodev),
3287 ("Failed to create buffer pool: %s", g_strerror (errno)));
3290 method_not_supported:
3292 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
3293 (_("The driver of device '%s' does not support the IO method %d"),
3294 v4l2object->videodev, mode), (NULL));
3297 no_supported_capture_method:
3299 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
3300 (_("The driver of device '%s' does not support any known IO "
3301 "method."), v4l2object->videodev), (NULL));
3307 gst_v4l2_object_reset_compose_region (GstV4l2Object * obj)
3309 struct v4l2_selection sel = { 0 };
3311 GST_V4L2_CHECK_OPEN (obj);
3313 sel.type = obj->type;
3314 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
3316 if (obj->ioctl (obj->video_fd, VIDIOC_G_SELECTION, &sel) < 0) {
3317 if (errno == ENOTTY) {
3318 /* No-op when selection API is not supported */
3321 GST_WARNING_OBJECT (obj->dbg_obj,
3322 "Failed to get default compose rectangle with VIDIOC_G_SELECTION: %s",
3323 g_strerror (errno));
3328 sel.target = V4L2_SEL_TGT_COMPOSE;
3330 if (obj->ioctl (obj->video_fd, VIDIOC_S_SELECTION, &sel) < 0) {
3331 GST_WARNING_OBJECT (obj->dbg_obj,
3332 "Failed to set default compose rectangle with VIDIOC_S_SELECTION: %s",
3333 g_strerror (errno));
3341 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
3342 gint plane, gint stride)
3344 const GstVideoFormatInfo *finfo = info->finfo;
3346 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3347 gint x_tiles, y_tiles, tile_height, padded_height;
3349 tile_height = GST_VIDEO_FORMAT_INFO_TILE_HEIGHT (finfo, plane);
3351 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
3352 info->height + align->padding_top + align->padding_bottom);
3354 x_tiles = stride / GST_VIDEO_FORMAT_INFO_TILE_STRIDE (finfo, plane);
3355 y_tiles = (padded_height + tile_height - 1) / tile_height;
3356 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
3358 info->stride[plane] = stride;
3363 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
3364 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
3366 const GstVideoFormatInfo *finfo = info->finfo;
3367 gint i, estride, padded_height;
3370 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
3373 GST_VIDEO_INFO_FIELD_HEIGHT (info) + align->padding_top +
3374 align->padding_bottom;
3376 for (i = 0; i < finfo->n_planes; i++) {
3377 estride = gst_video_format_info_extrapolate_stride (finfo, i, stride);
3379 gst_v4l2_object_set_stride (info, align, i, estride);
3381 info->offset[i] = offs;
3383 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
3385 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3386 "Extrapolated for plane %d with base stride %d: "
3387 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
3391 /* Update the image size according the amount of data we are going to
3392 * read/write. This workaround bugs in driver where the sizeimage provided
3393 * by TRY/S_FMT represent the buffer length (maximum size) rather then the expected
3394 * bytesused (buffer size). */
3395 if (offs < info->size)
3400 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
3401 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
3402 GstVideoInfo * info, GstVideoAlignment * align)
3404 const GstVideoFormatInfo *finfo = info->finfo;
3405 gboolean standard_stride = TRUE;
3406 gint stride, pstride, padded_width, padded_height, i;
3408 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
3409 v4l2object->n_v4l2_planes = 1;
3410 info->size = format->fmt.pix.sizeimage;
3414 /* adjust right padding */
3415 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
3416 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3418 stride = format->fmt.pix.bytesperline;
3420 pstride = GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
3422 padded_width = stride / pstride;
3424 /* pstride can be 0 for complex formats */
3425 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3426 "format %s has a pstride of 0, cannot compute padded with",
3427 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
3428 padded_width = stride;
3431 if (padded_width < format->fmt.pix.width)
3432 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3433 "Driver bug detected, stride (%d) is too small for the width (%d)",
3434 padded_width, format->fmt.pix.width);
3436 align->padding_right = padded_width - info->width - align->padding_left;
3438 /* adjust bottom padding */
3439 padded_height = format->fmt.pix.height;
3441 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3443 tile_height = GST_VIDEO_FORMAT_INFO_TILE_HEIGHT (finfo, 0);
3444 padded_height = (padded_height + tile_height - 1) / tile_height;
3447 align->padding_bottom =
3448 padded_height - GST_VIDEO_INFO_FIELD_HEIGHT (info) - align->padding_top;
3450 /* setup the strides and offset */
3451 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
3452 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
3454 /* figure out the frame layout */
3455 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
3457 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
3458 stride = pix_mp->plane_fmt[i].bytesperline;
3460 if (info->stride[i] != stride)
3461 standard_stride = FALSE;
3463 gst_v4l2_object_set_stride (info, align, i, stride);
3464 info->offset[i] = info->size;
3465 info->size += pix_mp->plane_fmt[i].sizeimage;
3468 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3469 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
3470 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3471 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3474 /* only one plane in non-MPLANE mode */
3475 v4l2object->n_v4l2_planes = 1;
3476 info->size = format->fmt.pix.sizeimage;
3477 stride = format->fmt.pix.bytesperline;
3479 if (info->stride[0] != stride)
3480 standard_stride = FALSE;
3482 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3485 /* adjust the offset to take into account left and top */
3486 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3487 if ((align->padding_left + align->padding_top) > 0)
3488 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3489 "Left and top padding is not permitted for tiled formats");
3490 memset (v4l2object->plane_size, 0,
3491 sizeof (v4l2object->plane_size[0] * GST_VIDEO_MAX_PLANES));
3493 if (!gst_video_info_align_full (info, align, v4l2object->plane_size)) {
3494 GST_WARNING_OBJECT (v4l2object->dbg_obj, "Failed to align video info");
3499 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got sizeimage %" G_GSIZE_FORMAT,
3502 /* to avoid copies we need video meta if there is padding */
3503 v4l2object->need_video_meta =
3504 ((align->padding_top + align->padding_left + align->padding_right +
3505 align->padding_bottom) != 0);
3507 /* ... or if stride is non "standard" */
3508 if (!standard_stride)
3509 v4l2object->need_video_meta = TRUE;
3511 /* ... or also video meta if we use multiple, non-contiguous, planes */
3512 if (v4l2object->n_v4l2_planes > 1)
3513 v4l2object->need_video_meta = TRUE;
3515 v4l2object->info = *info;
3516 v4l2object->align = *align;
3517 v4l2object->format = *format;
3518 v4l2object->fmtdesc = fmtdesc;
3520 /* if we have a framerate pre-calculate duration */
3521 if (info->fps_n > 0 && info->fps_d > 0) {
3522 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
3524 if (GST_VIDEO_INFO_INTERLACE_MODE (info) ==
3525 GST_VIDEO_INTERLACE_MODE_ALTERNATE)
3526 v4l2object->duration /= 2;
3528 v4l2object->duration = GST_CLOCK_TIME_NONE;
3532 static enum v4l2_field
3533 get_v4l2_field_for_info (GstVideoInfo * info)
3535 if (!GST_VIDEO_INFO_IS_INTERLACED (info))
3536 return V4L2_FIELD_NONE;
3538 if (GST_VIDEO_INFO_INTERLACE_MODE (info) ==
3539 GST_VIDEO_INTERLACE_MODE_ALTERNATE)
3540 return V4L2_FIELD_ALTERNATE;
3542 switch (GST_VIDEO_INFO_FIELD_ORDER (info)) {
3543 case GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST:
3544 return V4L2_FIELD_INTERLACED_TB;
3545 case GST_VIDEO_FIELD_ORDER_BOTTOM_FIELD_FIRST:
3546 return V4L2_FIELD_INTERLACED_BT;
3547 case GST_VIDEO_FIELD_ORDER_UNKNOWN:
3549 return V4L2_FIELD_INTERLACED;
3553 #ifndef TIZEN_FEATURE_V4L2_DISABLE_COLORIMETRY
3555 gst_v4l2_video_colorimetry_matches (const GstVideoColorimetry * cinfo,
3559 static const GstVideoColorimetry ci_likely_jpeg = {
3560 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3561 GST_VIDEO_TRANSFER_UNKNOWN, GST_VIDEO_COLOR_PRIMARIES_UNKNOWN
3563 static const GstVideoColorimetry ci_jpeg = {
3564 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3565 GST_VIDEO_TRANSFER_SRGB, GST_VIDEO_COLOR_PRIMARIES_BT709
3568 if (!gst_video_info_from_caps (&info, caps))
3571 /* if colorimetry in caps is unknown, use the default one */
3572 if (info.colorimetry.primaries == GST_VIDEO_COLOR_PRIMARIES_UNKNOWN)
3573 info.colorimetry.primaries = cinfo->primaries;
3574 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_UNKNOWN)
3575 info.colorimetry.range = cinfo->range;
3576 if (info.colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN)
3577 info.colorimetry.matrix = cinfo->matrix;
3578 if (info.colorimetry.transfer == GST_VIDEO_TRANSFER_UNKNOWN)
3579 info.colorimetry.transfer = cinfo->transfer;
3581 if (gst_video_colorimetry_is_equal (&info.colorimetry, cinfo))
3584 /* Allow 1:4:0:0 (produced by jpegdec) if the device expects 1:4:7:1 */
3585 if (gst_video_colorimetry_is_equal (&info.colorimetry, &ci_likely_jpeg)
3586 && gst_video_colorimetry_is_equal (cinfo, &ci_jpeg))
3589 /* bypass check the below transfer types, because those types are cast to
3590 * V4L2_XFER_FUNC_NONE type when try format or set format and V4L2_XFER_FUNC_NONE
3591 * type is cast to GST_VIDEO_TRANSFER_GAMMA10 type in gst_v4l2_object_get_colorspace */
3592 if ((info.colorimetry.transfer == GST_VIDEO_TRANSFER_GAMMA18) ||
3593 (info.colorimetry.transfer == GST_VIDEO_TRANSFER_GAMMA20) ||
3594 (info.colorimetry.transfer == GST_VIDEO_TRANSFER_GAMMA22) ||
3595 (info.colorimetry.transfer == GST_VIDEO_TRANSFER_GAMMA28)) {
3596 info.colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA10;
3597 if (gst_video_colorimetry_is_equal (&info.colorimetry, cinfo))
3605 static const gchar *
3606 field_to_str (enum v4l2_field f)
3609 case V4L2_FIELD_ANY:
3611 case V4L2_FIELD_NONE:
3613 case V4L2_FIELD_TOP:
3615 case V4L2_FIELD_BOTTOM:
3617 case V4L2_FIELD_INTERLACED:
3618 return "interlaced";
3619 case V4L2_FIELD_SEQ_TB:
3621 case V4L2_FIELD_SEQ_BT:
3623 case V4L2_FIELD_ALTERNATE:
3625 case V4L2_FIELD_INTERLACED_TB:
3626 return "interlaced-tb";
3627 case V4L2_FIELD_INTERLACED_BT:
3628 return "interlaced-bt";
3635 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3636 gboolean try_only, GstV4l2Error * error)
3638 gint fd = v4l2object->video_fd;
3639 struct v4l2_format format;
3640 struct v4l2_streamparm streamparm;
3641 enum v4l2_field field;
3642 guint32 pixelformat;
3643 struct v4l2_fmtdesc *fmtdesc;
3645 GstVideoAlignment align;
3646 gint width, height, fps_n, fps_d;
3650 enum v4l2_colorspace colorspace = 0;
3651 enum v4l2_quantization range = 0;
3652 enum v4l2_ycbcr_encoding matrix = 0;
3653 enum v4l2_xfer_func transfer = 0;
3655 gboolean disable_interlacing = FALSE;
3656 gboolean disable_colorimetry = FALSE;
3658 g_return_val_if_fail (!v4l2object->skip_try_fmt_probes ||
3659 gst_caps_is_writable (caps), FALSE);
3661 GST_V4L2_CHECK_OPEN (v4l2object);
3663 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3665 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3667 gst_video_info_init (&info);
3668 gst_video_alignment_reset (&align);
3669 v4l2object->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
3671 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3674 pixelformat = fmtdesc->pixelformat;
3675 width = GST_VIDEO_INFO_WIDTH (&info);
3676 height = GST_VIDEO_INFO_FIELD_HEIGHT (&info);
3677 /* if caps has no width and height info, use default value */
3678 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type) && width == 0 && height == 0) {
3679 width = GST_V4L2_DEFAULT_WIDTH;
3680 height = GST_V4L2_DEFAULT_HEIGHT;
3682 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3683 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3685 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3686 * or if contiguous is preferred */
3687 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3688 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3691 field = get_v4l2_field_for_info (&info);
3692 if (field != V4L2_FIELD_NONE)
3693 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "interlaced video");
3695 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "progressive video");
3697 /* We first pick the main colorspace from the primaries */
3698 switch (info.colorimetry.primaries) {
3699 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3700 /* There is two colorspaces using these primaries, use the range to
3702 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3703 colorspace = V4L2_COLORSPACE_REC709;
3705 colorspace = V4L2_COLORSPACE_SRGB;
3707 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
3708 colorspace = V4L2_COLORSPACE_BT2020;
3710 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3711 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3713 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3714 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3716 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3717 colorspace = V4L2_COLORSPACE_SMPTE170M;
3719 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3720 colorspace = V4L2_COLORSPACE_SMPTE240M;
3723 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3724 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3725 /* We don't know, we will guess */
3729 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3730 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3734 switch (info.colorimetry.range) {
3735 case GST_VIDEO_COLOR_RANGE_0_255:
3736 range = V4L2_QUANTIZATION_FULL_RANGE;
3738 case GST_VIDEO_COLOR_RANGE_16_235:
3739 range = V4L2_QUANTIZATION_LIM_RANGE;
3741 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3742 /* We let the driver pick a default one */
3745 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3746 "Unknown colorimetry range %d", info.colorimetry.range);
3750 switch (info.colorimetry.matrix) {
3751 case GST_VIDEO_COLOR_MATRIX_RGB:
3752 /* Unspecified, leave to default */
3754 /* FCC is about the same as BT601 with less digit */
3755 case GST_VIDEO_COLOR_MATRIX_FCC:
3756 case GST_VIDEO_COLOR_MATRIX_BT601:
3757 matrix = V4L2_YCBCR_ENC_601;
3759 case GST_VIDEO_COLOR_MATRIX_BT709:
3760 matrix = V4L2_YCBCR_ENC_709;
3762 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3763 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3765 case GST_VIDEO_COLOR_MATRIX_BT2020:
3766 matrix = V4L2_YCBCR_ENC_BT2020;
3768 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3769 /* We let the driver pick a default one */
3772 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3773 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3777 switch (info.colorimetry.transfer) {
3778 case GST_VIDEO_TRANSFER_GAMMA18:
3779 case GST_VIDEO_TRANSFER_GAMMA20:
3780 case GST_VIDEO_TRANSFER_GAMMA22:
3781 case GST_VIDEO_TRANSFER_GAMMA28:
3782 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3783 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3785 case GST_VIDEO_TRANSFER_GAMMA10:
3786 transfer = V4L2_XFER_FUNC_NONE;
3788 case GST_VIDEO_TRANSFER_SMPTE2084:
3789 transfer = V4L2_XFER_FUNC_SMPTE2084;
3791 case GST_VIDEO_TRANSFER_BT601:
3792 case GST_VIDEO_TRANSFER_BT2020_12:
3793 case GST_VIDEO_TRANSFER_BT2020_10:
3794 case GST_VIDEO_TRANSFER_BT709:
3795 v4l2object->transfer = info.colorimetry.transfer;
3796 transfer = V4L2_XFER_FUNC_709;
3798 case GST_VIDEO_TRANSFER_SMPTE240M:
3799 transfer = V4L2_XFER_FUNC_SMPTE240M;
3801 case GST_VIDEO_TRANSFER_SRGB:
3802 transfer = V4L2_XFER_FUNC_SRGB;
3804 case GST_VIDEO_TRANSFER_LOG100:
3805 case GST_VIDEO_TRANSFER_LOG316:
3806 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3807 "LOG 100, 316 transfer functions not supported");
3808 /* FIXME No known sensible default, maybe AdobeRGB ? */
3810 case GST_VIDEO_TRANSFER_UNKNOWN:
3811 /* We let the driver pick a default one */
3814 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3815 "Unknown colorimetry transfer %d", info.colorimetry.transfer);
3819 if (colorspace == 0) {
3820 /* Try to guess colorspace according to pixelformat and size */
3821 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3822 if (range == V4L2_QUANTIZATION_FULL_RANGE
3823 && matrix == V4L2_YCBCR_ENC_601 && transfer == 0) {
3824 /* Full range BT.601 YCbCr encoding with unknown primaries and transfer
3825 * function most likely is JPEG */
3826 colorspace = V4L2_COLORSPACE_JPEG;
3827 transfer = V4L2_XFER_FUNC_SRGB;
3829 /* SD streams likely use SMPTE170M and HD streams REC709 */
3830 if (width <= 720 && GST_VIDEO_INFO_HEIGHT (&info) <= 576)
3831 colorspace = V4L2_COLORSPACE_SMPTE170M;
3833 colorspace = V4L2_COLORSPACE_REC709;
3835 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3836 colorspace = V4L2_COLORSPACE_SRGB;
3837 transfer = V4L2_XFER_FUNC_NONE;
3841 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format %dx%d, format "
3842 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3843 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3845 memset (&format, 0x00, sizeof (struct v4l2_format));
3846 format.type = v4l2object->type;
3849 format.type = v4l2object->type;
3850 format.fmt.pix_mp.pixelformat = pixelformat;
3851 format.fmt.pix_mp.width = width;
3852 format.fmt.pix_mp.height = height;
3853 format.fmt.pix_mp.field = field;
3854 format.fmt.pix_mp.num_planes = n_v4l_planes;
3856 /* try to ask our preferred stride but it's not a failure if not
3858 for (i = 0; i < n_v4l_planes; i++) {
3859 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3861 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3862 stride = GST_VIDEO_TILE_X_TILES (stride) *
3863 GST_VIDEO_FORMAT_INFO_TILE_STRIDE (info.finfo, i);
3865 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3868 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3869 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3871 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3873 format.type = v4l2object->type;
3875 format.fmt.pix.width = width;
3876 format.fmt.pix.height = height;
3877 format.fmt.pix.pixelformat = pixelformat;
3878 format.fmt.pix.field = field;
3880 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3881 stride = GST_VIDEO_TILE_X_TILES (stride) *
3882 GST_VIDEO_FORMAT_INFO_TILE_STRIDE (info.finfo, i);
3884 /* try to ask our preferred stride */
3885 format.fmt.pix.bytesperline = stride;
3887 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3888 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3891 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format is %dx%d, format "
3892 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3893 format.fmt.pix_mp.height,
3894 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3895 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3897 #ifndef GST_DISABLE_GST_DEBUG
3899 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3900 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3901 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3903 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3904 format.fmt.pix.bytesperline);
3909 format.fmt.pix_mp.colorspace = colorspace;
3910 format.fmt.pix_mp.quantization = range;
3911 format.fmt.pix_mp.ycbcr_enc = matrix;
3912 format.fmt.pix_mp.xfer_func = transfer;
3914 format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
3915 format.fmt.pix.colorspace = colorspace;
3916 format.fmt.pix.quantization = range;
3917 format.fmt.pix.ycbcr_enc = matrix;
3918 format.fmt.pix.xfer_func = transfer;
3921 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired colorspace is %d:%d:%d:%d",
3922 colorspace, range, matrix, transfer);
3925 if (v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3926 goto try_fmt_failed;
3928 if (v4l2object->ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3929 goto set_fmt_failed;
3933 colorspace = format.fmt.pix_mp.colorspace;
3934 range = format.fmt.pix_mp.quantization;
3935 matrix = format.fmt.pix_mp.ycbcr_enc;
3936 transfer = format.fmt.pix_mp.xfer_func;
3938 colorspace = format.fmt.pix.colorspace;
3939 range = format.fmt.pix.quantization;
3940 matrix = format.fmt.pix.ycbcr_enc;
3941 transfer = format.fmt.pix.xfer_func;
3944 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got format of %dx%d, format "
3945 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d:%d:%d:%d field: %s",
3946 format.fmt.pix.width, format.fmt.pix_mp.height,
3947 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3948 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3949 colorspace, range, matrix, transfer, field_to_str (format.fmt.pix.field));
3951 #ifndef GST_DISABLE_GST_DEBUG
3953 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3954 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3955 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3956 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3958 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3959 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3963 if (format.fmt.pix.pixelformat != pixelformat)
3964 goto invalid_pixelformat;
3966 /* Only negotiate size with raw data.
3967 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3968 * in ASF mode for example, there is also not reason for a driver to
3969 * change the size. */
3970 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3971 /* We can crop larger images */
3972 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3973 goto invalid_dimensions;
3975 /* Note, this will be adjusted if upstream has non-centered cropping. */
3976 align.padding_top = 0;
3977 align.padding_bottom = format.fmt.pix.height - height;
3978 align.padding_left = 0;
3979 align.padding_right = format.fmt.pix.width - width;
3982 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3983 goto invalid_planes;
3985 /* used to check colorimetry and interlace mode fields presence */
3986 s = gst_caps_get_structure (caps, 0);
3988 if (gst_v4l2_object_get_interlace_mode (format.fmt.pix.field,
3989 &info.interlace_mode)) {
3990 if (gst_structure_has_field (s, "interlace-mode")) {
3991 if (format.fmt.pix.field != field)
3995 /* The driver (or libv4l2) is miss-behaving, just ignore interlace-mode from
3997 disable_interlacing = TRUE;
3998 if (gst_structure_has_field (s, "interlace-mode"))
3999 gst_structure_remove_field (s, "interlace-mode");
4002 #ifndef TIZEN_FEATURE_V4L2_DISABLE_COLORIMETRY
4003 if (gst_v4l2_object_get_colorspace (v4l2object, &format, &info.colorimetry)) {
4004 if (gst_structure_has_field (s, "colorimetry")) {
4005 if (!gst_v4l2_video_colorimetry_matches (&info.colorimetry, caps))
4006 goto invalid_colorimetry;
4010 /* The driver (or libv4l2) is miss-behaving, just ignore colorimetry from
4012 disable_colorimetry = TRUE;
4013 if (gst_structure_has_field (s, "colorimetry"))
4014 gst_structure_remove_field (s, "colorimetry");
4015 #ifndef TIZEN_FEATURE_V4L2_DISABLE_COLORIMETRY
4019 /* In case we have skipped the try_fmt probes, we'll need to set the
4020 * interlace-mode and colorimetry back into the caps. */
4021 if (v4l2object->skip_try_fmt_probes) {
4022 if (!disable_interlacing && !gst_structure_has_field (s, "interlace-mode")) {
4023 gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
4024 gst_video_interlace_mode_to_string (info.interlace_mode), NULL);
4026 if (!disable_colorimetry && !gst_structure_has_field (s, "colorimetry")) {
4027 gchar *str = gst_video_colorimetry_to_string (&info.colorimetry);
4028 gst_structure_set (s, "colorimetry", G_TYPE_STRING, str, NULL);
4033 if (try_only) /* good enough for trying only */
4036 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
4037 struct v4l2_control ctl = { 0, };
4038 ctl.id = V4L2_CID_ALPHA_COMPONENT;
4041 if (v4l2object->ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
4042 GST_WARNING_OBJECT (v4l2object->dbg_obj,
4043 "Failed to set alpha component value");
4046 /* Is there a reason we require the caller to always specify a framerate? */
4047 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired framerate: %u/%u", fps_n,
4050 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
4051 streamparm.type = v4l2object->type;
4053 if (v4l2object->ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
4054 goto get_parm_failed;
4056 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
4057 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
4058 GST_VIDEO_INFO_FPS_N (&info) =
4059 streamparm.parm.capture.timeperframe.denominator;
4060 GST_VIDEO_INFO_FPS_D (&info) =
4061 streamparm.parm.capture.timeperframe.numerator;
4063 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got capture framerate: %u/%u",
4064 streamparm.parm.capture.timeperframe.denominator,
4065 streamparm.parm.capture.timeperframe.numerator);
4067 /* We used to skip frame rate setup if the camera was already setup
4068 * with the requested frame rate. This breaks some cameras though,
4069 * causing them to not output data (several models of Thinkpad cameras
4070 * have this problem at least).
4071 * So, don't skip. */
4072 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting capture framerate to %u/%u",
4074 /* We want to change the frame rate, so check whether we can. Some cheap USB
4075 * cameras don't have the capability */
4076 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
4077 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
4078 "Not setting capture framerate (not supported)");
4082 /* Note: V4L2 wants the frame interval, we have the frame rate */
4083 streamparm.parm.capture.timeperframe.numerator = fps_d;
4084 streamparm.parm.capture.timeperframe.denominator = fps_n;
4086 /* some cheap USB cam's won't accept any change */
4087 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
4088 goto set_parm_failed;
4090 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
4091 streamparm.parm.capture.timeperframe.denominator > 0) {
4092 /* get new values */
4093 fps_d = streamparm.parm.capture.timeperframe.numerator;
4094 fps_n = streamparm.parm.capture.timeperframe.denominator;
4096 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set capture framerate to %u/%u",
4099 /* fix v4l2 capture driver to provide framerate values */
4100 GST_WARNING_OBJECT (v4l2object->dbg_obj,
4101 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
4104 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
4105 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
4106 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
4107 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
4108 GST_VIDEO_INFO_FPS_N (&info) =
4109 streamparm.parm.output.timeperframe.denominator;
4110 GST_VIDEO_INFO_FPS_D (&info) =
4111 streamparm.parm.output.timeperframe.numerator;
4113 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got output framerate: %u/%u",
4114 streamparm.parm.output.timeperframe.denominator,
4115 streamparm.parm.output.timeperframe.numerator);
4117 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting output framerate to %u/%u",
4119 if ((streamparm.parm.output.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
4120 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
4121 "Not setting output framerate (not supported)");
4125 /* Note: V4L2 wants the frame interval, we have the frame rate */
4126 streamparm.parm.output.timeperframe.numerator = fps_d;
4127 streamparm.parm.output.timeperframe.denominator = fps_n;
4129 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
4130 goto set_parm_failed;
4132 if (streamparm.parm.output.timeperframe.numerator > 0 &&
4133 streamparm.parm.output.timeperframe.denominator > 0) {
4134 /* get new values */
4135 fps_d = streamparm.parm.output.timeperframe.numerator;
4136 fps_n = streamparm.parm.output.timeperframe.denominator;
4138 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set output framerate to %u/%u",
4141 /* fix v4l2 output driver to provide framerate values */
4142 GST_WARNING_OBJECT (v4l2object->dbg_obj,
4143 "Reuse caps framerate %u/%u - fix v4l2 output driver", fps_n, fps_d);
4146 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
4147 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
4151 /* add boolean return, so we can fail on drivers bugs */
4152 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
4154 /* reset composition region to match the S_FMT size */
4155 gst_v4l2_object_reset_compose_region (v4l2object);
4157 /* now configure the pool */
4158 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
4166 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "can't parse caps %" GST_PTR_FORMAT,
4169 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4170 (_("Invalid caps")), ("Can't parse caps %" GST_PTR_FORMAT, caps));
4175 if (errno == EINVAL) {
4176 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4177 (_("Device '%s' has no supported format"), v4l2object->videodev),
4178 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4179 GST_FOURCC_ARGS (pixelformat), width, height,
4180 g_strerror (errno)));
4182 GST_V4L2_ERROR (error, RESOURCE, FAILED,
4183 (_("Device '%s' failed during initialization"),
4184 v4l2object->videodev),
4185 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4186 GST_FOURCC_ARGS (pixelformat), width, height,
4187 g_strerror (errno)));
4193 if (errno == EBUSY) {
4194 GST_V4L2_ERROR (error, RESOURCE, BUSY,
4195 (_("Device '%s' is busy"), v4l2object->videodev),
4196 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4197 GST_FOURCC_ARGS (pixelformat), width, height,
4198 g_strerror (errno)));
4199 } else if (errno == EINVAL) {
4200 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4201 (_("Device '%s' has no supported format"), v4l2object->videodev),
4202 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4203 GST_FOURCC_ARGS (pixelformat), width, height,
4204 g_strerror (errno)));
4206 GST_V4L2_ERROR (error, RESOURCE, FAILED,
4207 (_("Device '%s' failed during initialization"),
4208 v4l2object->videodev),
4209 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4210 GST_FOURCC_ARGS (pixelformat), width, height,
4211 g_strerror (errno)));
4217 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4218 (_("Device '%s' cannot capture at %dx%d"),
4219 v4l2object->videodev, width, height),
4220 ("Tried to capture at %dx%d, but device returned size %dx%d",
4221 width, height, format.fmt.pix.width, format.fmt.pix.height));
4224 invalid_pixelformat:
4226 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4227 (_("Device '%s' cannot capture in the specified format"),
4228 v4l2object->videodev),
4229 ("Tried to capture in %" GST_FOURCC_FORMAT
4230 ", but device returned format" " %" GST_FOURCC_FORMAT,
4231 GST_FOURCC_ARGS (pixelformat),
4232 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
4237 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4238 (_("Device '%s' does support non-contiguous planes"),
4239 v4l2object->videodev),
4240 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
4245 enum v4l2_field wanted_field;
4248 wanted_field = format.fmt.pix_mp.field;
4250 wanted_field = format.fmt.pix.field;
4252 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4253 (_("Device '%s' does not support %s interlacing"),
4254 v4l2object->videodev,
4255 field == V4L2_FIELD_NONE ? "progressive" : "interleaved"),
4256 ("Device wants %s interlacing",
4257 wanted_field == V4L2_FIELD_NONE ? "progressive" : "interleaved"));
4260 #ifndef TIZEN_FEATURE_V4L2_DISABLE_COLORIMETRY
4261 invalid_colorimetry:
4263 gchar *wanted_colorimetry;
4265 wanted_colorimetry = gst_video_colorimetry_to_string (&info.colorimetry);
4267 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4268 (_("Device '%s' does not support %s colorimetry"),
4269 v4l2object->videodev, gst_structure_get_string (s, "colorimetry")),
4270 ("Device wants %s colorimetry", wanted_colorimetry));
4272 g_free (wanted_colorimetry);
4278 /* it's possible that this call is not supported */
4279 if (errno != EINVAL && errno != ENOTTY) {
4280 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4281 (_("Could not get parameters on device '%s'"),
4282 v4l2object->videodev), GST_ERROR_SYSTEM);
4288 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
4289 (_("Video device did not accept new frame rate setting.")),
4295 /* setup_pool already send the error */
4301 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps,
4302 GstV4l2Error * error)
4304 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Setting format to %" GST_PTR_FORMAT,
4306 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE, error);
4310 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps,
4311 GstV4l2Error * error)
4313 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying format %" GST_PTR_FORMAT,
4315 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE, error);
4319 * gst_v4l2_object_acquire_format:
4320 * @v4l2object: the object
4321 * @info: a GstVideoInfo to be filled
4323 * Acquire the driver chosen format. This is useful in decoder or encoder elements where
4324 * the output format is chosen by the HW.
4326 * Returns: %TRUE on success, %FALSE on failure.
4329 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
4331 struct v4l2_fmtdesc *fmtdesc;
4332 struct v4l2_format fmt;
4333 struct v4l2_crop crop;
4334 struct v4l2_selection sel;
4335 struct v4l2_rect *r = NULL;
4336 GstVideoFormat format;
4337 guint width, height;
4338 GstVideoAlignment align;
4339 GstVideoInterlaceMode interlace_mode;
4341 gst_video_info_init (info);
4342 gst_video_alignment_reset (&align);
4343 v4l2object->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
4345 memset (&fmt, 0x00, sizeof (struct v4l2_format));
4346 fmt.type = v4l2object->type;
4347 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
4348 goto get_fmt_failed;
4350 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
4351 fmt.fmt.pix.pixelformat);
4352 if (fmtdesc == NULL)
4353 goto unsupported_format;
4355 /* No need to care about mplane, the four first params are the same */
4356 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
4358 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
4359 if (format == GST_VIDEO_FORMAT_UNKNOWN)
4360 goto unsupported_format;
4362 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
4363 goto invalid_dimensions;
4365 width = fmt.fmt.pix.width;
4366 height = fmt.fmt.pix.height;
4368 /* Use the default compose rectangle */
4369 memset (&sel, 0, sizeof (struct v4l2_selection));
4370 sel.type = v4l2object->type;
4371 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
4372 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0) {
4375 /* For ancient kernels, fall back to G_CROP */
4376 memset (&crop, 0, sizeof (struct v4l2_crop));
4377 crop.type = v4l2object->type;
4378 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
4382 align.padding_left = r->left;
4383 align.padding_top = r->top;
4384 align.padding_right = width - r->width - r->left;
4385 align.padding_bottom = height - r->height - r->top;
4390 switch (fmt.fmt.pix.field) {
4391 case V4L2_FIELD_ANY:
4392 case V4L2_FIELD_NONE:
4393 interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
4395 case V4L2_FIELD_INTERLACED:
4396 case V4L2_FIELD_INTERLACED_TB:
4397 case V4L2_FIELD_INTERLACED_BT:
4398 interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
4400 case V4L2_FIELD_ALTERNATE:
4401 interlace_mode = GST_VIDEO_INTERLACE_MODE_ALTERNATE;
4404 goto unsupported_field;
4407 if (!gst_video_info_set_interlaced_format (info, format, interlace_mode,
4409 goto invalid_dimensions;
4411 gst_v4l2_object_get_colorspace (v4l2object, &fmt, &info->colorimetry);
4412 gst_v4l2_object_get_streamparm (v4l2object, info);
4413 if ((info->fps_n == 0 && v4l2object->info.fps_d != 0)
4414 && (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
4415 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
4416 info->fps_d = v4l2object->info.fps_d;
4417 info->fps_n = v4l2object->info.fps_n;
4418 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Set capture fps to %d/%d",
4419 info->fps_n, info->fps_d);
4422 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
4424 /* Shall we setup the pool ? */
4430 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
4431 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
4436 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
4437 (_("Video device returned invalid dimensions.")),
4438 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
4439 fmt.fmt.pix.height));
4444 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
4445 (_("Video device uses an unsupported interlacing method.")),
4446 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
4451 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
4452 (_("Video device uses an unsupported pixel format.")),
4453 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
4454 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
4460 * gst_v4l2_object_set_crop:
4462 * @crop_rect: the region to crop
4464 * Crop the video data to the regions specified in the @crop_rect.
4466 * For capture devices, this crop the image sensor / video stream provided by
4468 * For output devices, this crops the memory buffer that GStreamer passed to
4471 * The crop_rect may be modified by the V4L2 device to a region that
4472 * fulfills H/W requirements.
4474 * Returns: %TRUE on success, %FALSE on failure.
4477 gst_v4l2_object_set_crop (GstV4l2Object * obj, struct v4l2_rect * crop_rect)
4479 struct v4l2_selection sel = { 0 };
4480 struct v4l2_crop crop = { 0 };
4482 GST_V4L2_CHECK_OPEN (obj);
4484 sel.type = obj->type;
4485 sel.target = V4L2_SEL_TGT_CROP;
4489 crop.type = obj->type;
4492 GST_DEBUG_OBJECT (obj->dbg_obj,
4493 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4494 crop.c.width, crop.c.height);
4496 if (obj->ioctl (obj->video_fd, VIDIOC_S_SELECTION, &sel) < 0) {
4497 if (errno != ENOTTY) {
4498 GST_WARNING_OBJECT (obj->dbg_obj,
4499 "Failed to set crop rectangle with VIDIOC_S_SELECTION: %s",
4500 g_strerror (errno));
4503 if (obj->ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
4504 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_S_CROP failed");
4508 if (obj->ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
4509 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_G_CROP failed");
4517 GST_DEBUG_OBJECT (obj->dbg_obj,
4518 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4519 crop.c.width, crop.c.height);
4525 * gst_v4l2_object_setup_padding:
4528 * Crop away the padding around the video data as specified
4529 * in GstVideoAlignement data stored in @obj.
4531 * For capture devices, this crop the image sensor / video stream provided by
4533 * For output devices, this crops the memory buffer that GStreamer passed to
4536 * Returns: %TRUE on success, %FALSE on failure.
4539 gst_v4l2_object_setup_padding (GstV4l2Object * obj)
4541 GstVideoAlignment *align = &obj->align;
4542 struct v4l2_rect crop;
4544 if (align->padding_left + align->padding_top
4545 + align->padding_right + align->padding_bottom == 0) {
4546 GST_DEBUG_OBJECT (obj->dbg_obj, "no cropping needed");
4550 crop.left = align->padding_left;
4551 crop.top = align->padding_top;
4552 crop.width = obj->info.width;
4553 crop.height = GST_VIDEO_INFO_FIELD_HEIGHT (&obj->info);
4555 return gst_v4l2_object_set_crop (obj, &crop);
4559 gst_v4l2_object_get_crop_rect (GstV4l2Object * obj, guint target,
4560 struct v4l2_rect *result)
4562 struct v4l2_rect *res_rect;
4564 struct v4l2_selection sel = { 0 };
4565 struct v4l2_cropcap cropcap = { 0 };
4567 GST_V4L2_CHECK_OPEN (obj);
4569 if (target != V4L2_SEL_TGT_CROP_BOUNDS && target != V4L2_SEL_TGT_CROP_DEFAULT)
4572 sel.type = obj->type;
4573 sel.target = target;
4577 if (obj->ioctl (obj->video_fd, VIDIOC_G_SELECTION, &sel) < 0) {
4578 if (errno != ENOTTY) {
4579 GST_WARNING_OBJECT (obj->dbg_obj,
4580 "Failed to get default crop rectangle with VIDIOC_G_SELECTION: %s",
4581 g_strerror (errno));
4584 if (obj->ioctl (obj->video_fd, VIDIOC_CROPCAP, &cropcap) < 0) {
4585 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_CROPCAP failed");
4588 if (target == V4L2_SEL_TGT_CROP_BOUNDS)
4589 res_rect = &cropcap.bounds;
4590 else if (target == V4L2_SEL_TGT_CROP_DEFAULT)
4591 res_rect = &cropcap.defrect;
4595 *result = *res_rect;
4600 gst_v4l2_object_get_crop_bounds (GstV4l2Object * obj, struct v4l2_rect * result)
4602 return gst_v4l2_object_get_crop_rect (obj, V4L2_SEL_TGT_CROP_BOUNDS, result);
4606 gst_v4l2_object_get_crop_default (GstV4l2Object * obj,
4607 struct v4l2_rect * result)
4609 return gst_v4l2_object_get_crop_rect (obj, V4L2_SEL_TGT_CROP_DEFAULT, result);
4613 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
4615 GstStructure *config;
4618 GstBufferPool *pool = gst_v4l2_object_get_buffer_pool (v4l2object);
4623 config = gst_buffer_pool_get_config (pool);
4624 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4626 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
4628 gst_structure_free (config);
4630 gst_object_unref (pool);
4635 gst_v4l2_object_caps_is_subset (GstV4l2Object * v4l2object, GstCaps * caps)
4637 GstStructure *config;
4640 GstBufferPool *pool = gst_v4l2_object_get_buffer_pool (v4l2object);
4645 config = gst_buffer_pool_get_config (pool);
4646 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4648 ret = oldcaps && gst_caps_is_subset (oldcaps, caps);
4650 gst_structure_free (config);
4652 gst_object_unref (pool);
4657 gst_v4l2_object_get_current_caps (GstV4l2Object * v4l2object)
4659 GstStructure *config;
4661 GstBufferPool *pool = gst_v4l2_object_get_buffer_pool (v4l2object);
4666 config = gst_buffer_pool_get_config (pool);
4667 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4670 gst_caps_ref (oldcaps);
4672 gst_structure_free (config);
4674 gst_object_unref (pool);
4679 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
4681 gboolean ret = TRUE;
4682 GstBufferPool *pool = gst_v4l2_object_get_buffer_pool (v4l2object);
4684 GST_LOG_OBJECT (v4l2object->dbg_obj, "start flushing");
4686 gst_poll_set_flushing (v4l2object->poll, TRUE);
4691 if (gst_buffer_pool_is_active (pool))
4692 gst_buffer_pool_set_flushing (pool, TRUE);
4694 gst_object_unref (pool);
4699 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
4701 gboolean ret = TRUE;
4702 GstBufferPool *pool = gst_v4l2_object_get_buffer_pool (v4l2object);
4704 GST_LOG_OBJECT (v4l2object->dbg_obj, "stop flushing");
4706 gst_poll_set_flushing (v4l2object->poll, FALSE);
4711 if (gst_buffer_pool_is_active (pool))
4712 gst_buffer_pool_set_flushing (pool, FALSE);
4714 gst_object_unref (pool);
4719 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
4721 GstBufferPool *pool;
4722 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "stopping");
4724 if (!GST_V4L2_IS_OPEN (v4l2object))
4726 if (!GST_V4L2_IS_ACTIVE (v4l2object))
4729 gst_poll_set_flushing (v4l2object->poll, TRUE);
4731 pool = gst_v4l2_object_get_buffer_pool (v4l2object);
4733 if (!gst_v4l2_buffer_pool_orphan (v4l2object)) {
4734 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "deactivating pool");
4735 gst_buffer_pool_set_active (pool, FALSE);
4738 GstBufferPool *old_pool;
4739 GST_OBJECT_LOCK (v4l2object->element);
4740 old_pool = v4l2object->pool;
4741 v4l2object->pool = NULL;
4742 GST_OBJECT_UNLOCK (v4l2object->element);
4744 gst_object_unref (old_pool);
4747 gst_object_unref (pool);
4750 GST_V4L2_SET_INACTIVE (v4l2object);
4757 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4764 if (v4l2object->fmtdesc)
4765 fourcc = GST_V4L2_PIXELFORMAT (v4l2object);
4767 gst_v4l2_object_clear_format_list (v4l2object);
4768 formats = gst_v4l2_object_get_format_list (v4l2object);
4770 /* Recover the fmtdesc, it may no longer exist, in which case it will be set
4773 v4l2object->fmtdesc =
4774 gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
4776 ret = gst_caps_new_empty ();
4778 if (v4l2object->keep_aspect && !v4l2object->par) {
4779 struct v4l2_cropcap cropcap;
4781 memset (&cropcap, 0, sizeof (cropcap));
4783 cropcap.type = v4l2object->type;
4784 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0) {
4785 if (errno != ENOTTY)
4786 GST_WARNING_OBJECT (v4l2object->dbg_obj,
4787 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
4788 g_strerror (errno));
4789 } else if (cropcap.pixelaspect.numerator && cropcap.pixelaspect.denominator) {
4790 v4l2object->par = g_new0 (GValue, 1);
4791 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
4792 gst_value_set_fraction (v4l2object->par, cropcap.pixelaspect.numerator,
4793 cropcap.pixelaspect.denominator);
4797 for (walk = formats; walk; walk = walk->next) {
4798 struct v4l2_fmtdesc *format;
4799 GstStructure *template;
4802 format = (struct v4l2_fmtdesc *) walk->data;
4804 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
4807 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
4808 "unknown format %" GST_FOURCC_FORMAT,
4809 GST_FOURCC_ARGS (format->pixelformat));
4813 /* If we have a filter, check if we need to probe this format or not */
4815 GstCaps *format_caps = gst_caps_new_empty ();
4817 gst_caps_append_structure (format_caps, gst_structure_copy (template));
4819 if (!gst_caps_can_intersect (format_caps, filter)) {
4820 gst_caps_unref (format_caps);
4821 gst_structure_free (template);
4825 gst_caps_unref (format_caps);
4828 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
4829 format->pixelformat, template);
4831 gst_caps_append (ret, tmp);
4833 /* Add a variant of the caps with the Interlaced feature so we can negotiate it if needed */
4834 add_alternate_variant (v4l2object, ret, gst_caps_get_structure (ret,
4835 gst_caps_get_size (ret) - 1));
4838 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
4839 if (format->pixelformat == V4L2_PIX_FMT_NV12 ||
4840 format->pixelformat == V4L2_PIX_FMT_YUV420) {
4841 GstStructure *alt_s = gst_structure_copy (template);
4843 if (format->pixelformat == V4L2_PIX_FMT_NV12)
4844 gst_structure_set (alt_s, "format", G_TYPE_STRING, "SN12", NULL);
4846 gst_structure_set (alt_s, "format", G_TYPE_STRING, "S420", NULL);
4848 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
4849 format->pixelformat, alt_s);
4852 gst_caps_append (ret, tmp);
4854 gst_structure_free (alt_s);
4856 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
4857 gst_structure_free (template);
4864 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
4865 gst_caps_unref (tmp);
4868 GST_INFO_OBJECT (v4l2object->dbg_obj, "probed caps: %" GST_PTR_FORMAT, ret);
4874 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4878 if (v4l2object->probed_caps == NULL)
4879 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
4882 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
4883 GST_CAPS_INTERSECT_FIRST);
4885 ret = gst_caps_ref (v4l2object->probed_caps);
4892 gst_v4l2_object_match_buffer_layout (GstV4l2Object * obj, guint n_planes,
4893 gsize offset[GST_VIDEO_MAX_PLANES], gint stride[GST_VIDEO_MAX_PLANES],
4894 gsize buffer_size, guint padded_height)
4897 gboolean need_fmt_update = FALSE;
4899 if (n_planes != GST_VIDEO_INFO_N_PLANES (&obj->info)) {
4900 GST_WARNING_OBJECT (obj->dbg_obj,
4901 "Cannot match buffers with different number planes");
4905 for (p = 0; p < n_planes; p++) {
4906 if (stride[p] < obj->info.stride[p]) {
4907 GST_DEBUG_OBJECT (obj->dbg_obj,
4908 "Not matching as remote stride %i is smaller than %i on plane %u",
4909 stride[p], obj->info.stride[p], p);
4911 } else if (stride[p] > obj->info.stride[p]) {
4912 GST_LOG_OBJECT (obj->dbg_obj,
4913 "remote stride %i is higher than %i on plane %u",
4914 stride[p], obj->info.stride[p], p);
4915 need_fmt_update = TRUE;
4918 if (offset[p] < obj->info.offset[p]) {
4919 GST_DEBUG_OBJECT (obj->dbg_obj,
4920 "Not matching as offset %" G_GSIZE_FORMAT
4921 " is smaller than %" G_GSIZE_FORMAT " on plane %u",
4922 offset[p], obj->info.offset[p], p);
4924 } else if (offset[p] > obj->info.offset[p]) {
4925 GST_LOG_OBJECT (obj->dbg_obj,
4926 "Remote offset %" G_GSIZE_FORMAT
4927 " is higher than %" G_GSIZE_FORMAT " on plane %u",
4928 offset[p], obj->info.offset[p], p);
4929 need_fmt_update = TRUE;
4932 if (padded_height) {
4935 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
4936 fmt_height = obj->format.fmt.pix_mp.height;
4938 fmt_height = obj->format.fmt.pix.height;
4940 if (padded_height > fmt_height)
4941 need_fmt_update = TRUE;
4945 if (need_fmt_update) {
4946 struct v4l2_format format;
4947 gint wanted_stride[GST_VIDEO_MAX_PLANES] = { 0, };
4949 format = obj->format;
4951 if (padded_height) {
4952 GST_DEBUG_OBJECT (obj->dbg_obj, "Padded height %u", padded_height);
4954 obj->align.padding_bottom =
4955 padded_height - GST_VIDEO_INFO_FIELD_HEIGHT (&obj->info);
4957 GST_WARNING_OBJECT (obj->dbg_obj,
4958 "Failed to compute padded height; keep the default one");
4959 padded_height = format.fmt.pix_mp.height;
4962 /* update the current format with the stride we want to import from */
4963 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
4966 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted strides:");
4968 for (i = 0; i < obj->n_v4l2_planes; i++) {
4969 gint plane_stride = stride[i];
4971 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4972 plane_stride = GST_VIDEO_TILE_X_TILES (plane_stride) *
4973 GST_VIDEO_FORMAT_INFO_TILE_STRIDE (obj->info.finfo, i);
4975 format.fmt.pix_mp.plane_fmt[i].bytesperline = plane_stride;
4976 format.fmt.pix_mp.height = padded_height;
4977 wanted_stride[i] = plane_stride;
4978 GST_DEBUG_OBJECT (obj->dbg_obj, " [%u] %i", i, wanted_stride[i]);
4981 gint plane_stride = stride[0];
4983 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted stride: %i", plane_stride);
4985 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4986 plane_stride = GST_VIDEO_TILE_X_TILES (plane_stride) *
4987 GST_VIDEO_FORMAT_INFO_TILE_STRIDE (obj->info.finfo, 0);
4989 format.fmt.pix.bytesperline = plane_stride;
4990 format.fmt.pix.height = padded_height;
4991 wanted_stride[0] = plane_stride;
4994 if (obj->ioctl (obj->video_fd, VIDIOC_S_FMT, &format) < 0) {
4995 GST_WARNING_OBJECT (obj->dbg_obj,
4996 "Something went wrong trying to update current format: %s",
4997 g_strerror (errno));
5001 gst_v4l2_object_save_format (obj, obj->fmtdesc, &format, &obj->info,
5004 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
5007 for (i = 0; i < obj->n_v4l2_planes; i++) {
5008 if (format.fmt.pix_mp.plane_fmt[i].bytesperline != wanted_stride[i]) {
5009 GST_DEBUG_OBJECT (obj->dbg_obj,
5010 "[%i] Driver did not accept the new stride (wants %i, got %i)",
5011 i, wanted_stride[i], format.fmt.pix_mp.plane_fmt[i].bytesperline);
5016 if (format.fmt.pix_mp.height != padded_height) {
5017 GST_DEBUG_OBJECT (obj->dbg_obj,
5018 "Driver did not accept the padded height (wants %i, got %i)",
5019 padded_height, format.fmt.pix_mp.height);
5022 if (format.fmt.pix.bytesperline != wanted_stride[0]) {
5023 GST_DEBUG_OBJECT (obj->dbg_obj,
5024 "Driver did not accept the new stride (wants %i, got %i)",
5025 wanted_stride[0], format.fmt.pix.bytesperline);
5029 if (format.fmt.pix.height != padded_height) {
5030 GST_DEBUG_OBJECT (obj->dbg_obj,
5031 "Driver did not accept the padded height (wants %i, got %i)",
5032 padded_height, format.fmt.pix.height);
5037 if (obj->align.padding_bottom) {
5038 /* Crop because of vertical padding */
5039 GST_DEBUG_OBJECT (obj->dbg_obj, "crop because of bottom padding of %d",
5040 obj->align.padding_bottom);
5041 gst_v4l2_object_setup_padding (obj);
5048 validate_video_meta_struct (GstV4l2Object * obj, const GstStructure * s)
5052 for (i = 0; i < gst_structure_n_fields (s); i++) {
5053 const gchar *name = gst_structure_nth_field_name (s, i);
5055 if (!g_str_equal (name, "padding-top")
5056 && !g_str_equal (name, "padding-bottom")
5057 && !g_str_equal (name, "padding-left")
5058 && !g_str_equal (name, "padding-right")) {
5059 GST_WARNING_OBJECT (obj->dbg_obj, "Unknown video meta field: '%s'", name);
5068 gst_v4l2_object_match_buffer_layout_from_struct (GstV4l2Object * obj,
5069 const GstStructure * s, GstCaps * caps, guint buffer_size)
5072 GstVideoAlignment align;
5073 gsize plane_size[GST_VIDEO_MAX_PLANES];
5075 if (!validate_video_meta_struct (obj, s))
5078 if (!gst_video_info_from_caps (&info, caps)) {
5079 GST_WARNING_OBJECT (obj->dbg_obj, "Failed to create video info");
5083 gst_video_alignment_reset (&align);
5085 gst_structure_get_uint (s, "padding-top", &align.padding_top);
5086 gst_structure_get_uint (s, "padding-bottom", &align.padding_bottom);
5087 gst_structure_get_uint (s, "padding-left", &align.padding_left);
5088 gst_structure_get_uint (s, "padding-right", &align.padding_right);
5090 if (align.padding_top || align.padding_bottom || align.padding_left ||
5091 align.padding_right) {
5092 GST_DEBUG_OBJECT (obj->dbg_obj,
5093 "Upstream requested padding (top: %d bottom: %d left: %d right: %d)",
5094 align.padding_top, align.padding_bottom, align.padding_left,
5095 align.padding_right);
5098 if (!gst_video_info_align_full (&info, &align, plane_size)) {
5099 GST_WARNING_OBJECT (obj->dbg_obj, "Failed to align video info");
5103 if (GST_VIDEO_INFO_SIZE (&info) != buffer_size) {
5104 GST_WARNING_OBJECT (obj->dbg_obj,
5105 "Requested buffer size (%d) doesn't match video info size (%"
5106 G_GSIZE_FORMAT ")", buffer_size, GST_VIDEO_INFO_SIZE (&info));
5110 GST_DEBUG_OBJECT (obj->dbg_obj,
5111 "try matching buffer layout requested by downstream");
5113 gst_v4l2_object_match_buffer_layout (obj, GST_VIDEO_INFO_N_PLANES (&info),
5114 info.offset, info.stride, buffer_size,
5115 GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size));
5121 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
5124 GstBufferPool *pool = NULL, *other_pool = NULL, *obj_pool = NULL;
5125 GstStructure *config;
5126 guint size, min, max, own_min = 0;
5128 gboolean has_video_meta;
5129 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
5130 GstAllocator *allocator = NULL;
5131 GstAllocationParams params = { 0 };
5134 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
5135 GST_INFO_OBJECT (obj->dbg_obj, "decide allocation - %s",
5136 V4L2_TYPE_IS_OUTPUT (obj->type) ? "output" : "capture");
5137 #else /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
5138 GST_DEBUG_OBJECT (obj->dbg_obj, "decide allocation");
5139 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
5141 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
5142 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
5144 gst_query_parse_allocation (query, &caps, NULL);
5146 obj_pool = gst_v4l2_object_get_buffer_pool (obj);
5147 if (obj_pool == NULL) {
5148 if (!gst_v4l2_object_setup_pool (obj, caps))
5150 obj_pool = gst_v4l2_object_get_buffer_pool (obj);
5151 if (obj_pool == NULL)
5155 if (gst_query_get_n_allocation_params (query) > 0)
5156 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
5158 if (gst_query_get_n_allocation_pools (query) > 0) {
5159 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
5168 GST_DEBUG_OBJECT (obj->dbg_obj, "allocation: size:%u min:%u max:%u pool:%"
5169 GST_PTR_FORMAT, size, min, max, pool);
5172 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE,
5175 if (has_video_meta) {
5176 const GstStructure *params;
5177 gst_query_parse_nth_allocation_meta (query, video_idx, ¶ms);
5180 gst_v4l2_object_match_buffer_layout_from_struct (obj, params, caps, size);
5183 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
5185 gst_v4l2_get_driver_min_buffers (obj);
5186 /* We can't share our own pool, if it exceed V4L2 capacity */
5187 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
5188 can_share_own_pool = FALSE;
5191 switch (obj->mode) {
5192 case GST_V4L2_IO_RW:
5194 /* in READ/WRITE mode, prefer a downstream pool because our own pool
5195 * doesn't help much, we have to write to it as well */
5196 GST_DEBUG_OBJECT (obj->dbg_obj,
5197 "read/write mode: using downstream pool");
5198 /* use the bigest size, when we use our own pool we can't really do any
5199 * other size than what the hardware gives us but for downstream pools
5201 size = MAX (size, obj->info.size);
5202 } else if (can_share_own_pool) {
5203 /* no downstream pool, use our own then */
5204 GST_DEBUG_OBJECT (obj->dbg_obj,
5205 "read/write mode: no downstream pool, using our own");
5206 pool = gst_object_ref (obj_pool);
5207 size = obj->info.size;
5208 pushing_from_our_pool = TRUE;
5212 case GST_V4L2_IO_USERPTR:
5213 case GST_V4L2_IO_DMABUF_IMPORT:
5214 /* in importing mode, prefer our own pool, and pass the other pool to
5215 * our own, so it can serve itself */
5217 goto no_downstream_pool;
5218 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj_pool),
5221 gst_object_unref (pool);
5222 pool = gst_object_ref (obj_pool);
5223 size = obj->info.size;
5226 case GST_V4L2_IO_MMAP:
5227 case GST_V4L2_IO_DMABUF:
5228 /* in streaming mode, prefer our own pool */
5229 /* Check if we can use it ... */
5230 if (can_share_own_pool) {
5232 gst_object_unref (pool);
5233 pool = gst_object_ref (obj_pool);
5234 size = obj->info.size;
5235 GST_DEBUG_OBJECT (obj->dbg_obj,
5236 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
5237 pushing_from_our_pool = TRUE;
5239 GST_DEBUG_OBJECT (obj->dbg_obj,
5240 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
5243 GST_DEBUG_OBJECT (obj->dbg_obj,
5244 "streaming mode: no usable pool, copying to generic pool");
5245 size = MAX (size, obj->info.size);
5248 case GST_V4L2_IO_AUTO:
5250 GST_WARNING_OBJECT (obj->dbg_obj, "unhandled mode");
5257 /* If pushing from our own pool, configure it with queried minimum,
5258 * otherwise use the minimum required */
5259 if (pushing_from_our_pool) {
5260 /* When pushing from our own pool, we need what downstream one, to be able
5261 * to fill the pipeline, the minimum required to decoder according to the
5262 * driver and 2 more, so we don't endup up with everything downstream or
5263 * held by the decoder. We account 2 buffers for v4l2 so when one is being
5264 * pushed downstream the other one can already be queued for the next
5266 own_min = min + obj->min_buffers + 2;
5268 /* If no allocation parameters where provided, allow for a little more
5269 * buffers and enable copy threshold */
5272 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
5275 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
5280 /* In this case we'll have to configure two buffer pool. For our buffer
5281 * pool, we'll need what the driver one, and one more, so we can dequeu */
5282 own_min = obj->min_buffers + 1;
5283 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS (obj));
5285 /* for the downstream pool, we keep what downstream wants, though ensure
5286 * at least a minimum if downstream didn't suggest anything (we are
5287 * expecting the base class to create a default one for the context) */
5288 min = MAX (min, GST_V4L2_MIN_BUFFERS (obj));
5290 /* To import we need the other pool to hold at least own_min */
5291 if (obj_pool == pool)
5295 /* Request a bigger max, if one was suggested but it's too small */
5297 max = MAX (min, max);
5299 /* First step, configure our own pool */
5300 config = gst_buffer_pool_get_config (obj_pool);
5302 if (obj->need_video_meta || has_video_meta) {
5303 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
5304 gst_buffer_pool_config_add_option (config,
5305 GST_BUFFER_POOL_OPTION_VIDEO_META);
5308 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
5309 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
5311 GST_DEBUG_OBJECT (obj->dbg_obj, "setting own pool config to %"
5312 GST_PTR_FORMAT, config);
5314 /* Our pool often need to adjust the value */
5315 if (!gst_buffer_pool_set_config (obj_pool, config)) {
5316 config = gst_buffer_pool_get_config (obj_pool);
5318 GST_DEBUG_OBJECT (obj->dbg_obj, "own pool config changed to %"
5319 GST_PTR_FORMAT, config);
5321 /* our pool will adjust the maximum buffer, which we are fine with */
5322 if (!gst_buffer_pool_set_config (obj_pool, config))
5326 /* Now configure the other pool if different */
5327 if (obj_pool != pool)
5331 config = gst_buffer_pool_get_config (other_pool);
5332 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
5333 gst_buffer_pool_config_set_params (config, caps, size, min, max);
5335 GST_DEBUG_OBJECT (obj->dbg_obj, "setting other pool config to %"
5336 GST_PTR_FORMAT, config);
5338 /* if downstream supports video metadata, add this to the pool config */
5339 if (has_video_meta) {
5340 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
5341 gst_buffer_pool_config_add_option (config,
5342 GST_BUFFER_POOL_OPTION_VIDEO_META);
5345 if (!gst_buffer_pool_set_config (other_pool, config)) {
5346 config = gst_buffer_pool_get_config (other_pool);
5348 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
5350 gst_structure_free (config);
5354 if (!gst_buffer_pool_set_config (other_pool, config))
5360 /* For simplicity, simply read back the active configuration, so our base
5361 * class get the right information */
5362 config = gst_buffer_pool_get_config (pool);
5363 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
5364 gst_structure_free (config);
5368 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
5370 gst_query_add_allocation_pool (query, pool, size, min, max);
5373 gst_object_unref (allocator);
5376 gst_object_unref (pool);
5379 gst_object_unref (obj_pool);
5385 /* setup_pool already send the error */
5390 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
5391 (_("Failed to configure internal buffer pool.")), (NULL));
5396 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
5397 (_("Video device did not suggest any buffer size.")), (NULL));
5402 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
5403 (_("No downstream pool to import from.")),
5404 ("When importing DMABUF or USERPTR, we need a pool to import from"));
5410 gst_object_unref (allocator);
5413 gst_object_unref (pool);
5416 gst_object_unref (obj_pool);
5422 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
5424 GstBufferPool *pool = NULL;
5425 /* we need at least 2 buffers to operate */
5426 guint size, min, max;
5430 /* Set defaults allocation parameters */
5431 size = obj->info.size;
5432 min = GST_V4L2_MIN_BUFFERS (obj);
5433 max = VIDEO_MAX_FRAME;
5435 gst_query_parse_allocation (query, &caps, &need_pool);
5440 switch (obj->mode) {
5441 case GST_V4L2_IO_MMAP:
5442 case GST_V4L2_IO_DMABUF:
5444 GstBufferPool *obj_pool = gst_v4l2_object_get_buffer_pool (obj);
5446 if (!gst_buffer_pool_is_active (obj_pool))
5447 pool = gst_object_ref (obj_pool);
5449 gst_object_unref (obj_pool);
5459 GstStructure *config;
5461 /* we had a pool, check caps */
5462 config = gst_buffer_pool_get_config (pool);
5463 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
5465 GST_DEBUG_OBJECT (obj->dbg_obj,
5466 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
5467 if (!gst_caps_is_equal (caps, pcaps)) {
5468 gst_structure_free (config);
5469 gst_object_unref (pool);
5470 goto different_caps;
5472 gst_structure_free (config);
5474 gst_v4l2_get_driver_min_buffers (obj);
5476 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS (obj));
5478 gst_query_add_allocation_pool (query, pool, size, min, max);
5480 /* we also support various metadata */
5481 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
5484 gst_object_unref (pool);
5491 GST_DEBUG_OBJECT (obj->dbg_obj, "no caps specified");
5496 /* different caps, we can't use this pool */
5497 GST_DEBUG_OBJECT (obj->dbg_obj, "pool has different caps");
5503 gst_v4l2_object_try_import (GstV4l2Object * obj, GstBuffer * buffer)
5505 GstVideoMeta *vmeta;
5506 guint n_mem = gst_buffer_n_memory (buffer);
5508 /* only import if requested */
5509 switch (obj->mode) {
5510 case GST_V4L2_IO_USERPTR:
5511 case GST_V4L2_IO_DMABUF_IMPORT:
5514 GST_DEBUG_OBJECT (obj->dbg_obj,
5515 "The io-mode does not enable importation");
5519 vmeta = gst_buffer_get_video_meta (buffer);
5520 if (!vmeta && obj->need_video_meta) {
5521 GST_DEBUG_OBJECT (obj->dbg_obj, "Downstream buffer uses standard "
5522 "stride/offset while the driver does not.");
5526 /* we need matching strides/offsets and size */
5528 guint plane_height[GST_VIDEO_MAX_PLANES] = { 0, };
5530 gst_video_meta_get_plane_height (vmeta, plane_height);
5532 if (!gst_v4l2_object_match_buffer_layout (obj, vmeta->n_planes,
5533 vmeta->offset, vmeta->stride, gst_buffer_get_size (buffer),
5538 /* we can always import single memory buffer, but otherwise we need the same
5539 * amount of memory object. */
5540 if (n_mem != 1 && n_mem != obj->n_v4l2_planes) {
5541 GST_DEBUG_OBJECT (obj->dbg_obj, "Can only import %i memory, "
5542 "buffers contains %u memory", obj->n_v4l2_planes, n_mem);
5546 /* For DMABuf importation we need DMABuf of course */
5547 if (obj->mode == GST_V4L2_IO_DMABUF_IMPORT) {
5550 for (i = 0; i < n_mem; i++) {
5551 GstMemory *mem = gst_buffer_peek_memory (buffer, i);
5553 if (!gst_is_dmabuf_memory (mem)) {
5554 GST_DEBUG_OBJECT (obj->dbg_obj, "Cannot import non-DMABuf memory.");
5560 /* for the remaining, only the kernel driver can tell */
5565 * gst_v4l2_object_get_buffer_pool:
5566 * @src: a #GstV4l2Object
5568 * Returns: (nullable) (transfer full): the instance of the #GstBufferPool used
5569 * by the v4l2object; unref it after usage.
5572 gst_v4l2_object_get_buffer_pool (GstV4l2Object * v4l2object)
5574 GstBufferPool *ret = NULL;
5576 g_return_val_if_fail (v4l2object != NULL, NULL);
5578 GST_OBJECT_LOCK (v4l2object->element);
5579 if (v4l2object->pool)
5580 ret = gst_object_ref (v4l2object->pool);
5581 GST_OBJECT_UNLOCK (v4l2object->element);
5587 * gst_v4l2_object_poll:
5588 * @v4l2object: a #GstV4l2Object
5589 * @timeout: timeout of type #GstClockTime
5591 * Poll the video file descriptor for read when this is a capture, write when
5592 * this is an output. It will also watch for errors and source change events.
5593 * If a source change event is received, %GST_V4L2_FLOW_RESOLUTION_CHANGE will
5594 * be returned. If the poll was interrupted, %GST_FLOW_FLUSHING is returned.
5595 * If there was no read or write indicator, %GST_V4L2_FLOW_LAST_BUFFER is
5596 * returned. It may also return %GST_FLOW_ERROR if some unexpected error
5599 * Returns: GST_FLOW_OK if buffers are ready to be queued or dequeued.
5602 gst_v4l2_object_poll (GstV4l2Object * v4l2object, GstClockTime timeout)
5606 if (!v4l2object->can_poll_device) {
5613 GST_LOG_OBJECT (v4l2object->dbg_obj, "polling device");
5616 ret = gst_poll_wait (v4l2object->poll, timeout);
5617 if (G_UNLIKELY (ret < 0)) {
5625 GST_WARNING_OBJECT (v4l2object->dbg_obj,
5626 "v4l2 device doesn't support polling. Disabling"
5627 " using libv4l2 in this case may cause deadlocks");
5628 v4l2object->can_poll_device = FALSE;
5635 if (gst_poll_fd_has_error (v4l2object->poll, &v4l2object->pollfd))
5638 /* PRI is used to signal that events are available */
5639 if (gst_poll_fd_has_pri (v4l2object->poll, &v4l2object->pollfd)) {
5640 struct v4l2_event event = { 0, };
5642 if (!gst_v4l2_dequeue_event (v4l2object, &event))
5643 goto dqevent_failed;
5645 if (event.type != V4L2_EVENT_SOURCE_CHANGE) {
5646 GST_INFO_OBJECT (v4l2object->dbg_obj,
5647 "Received unhandled event, ignoring.");
5651 if ((event.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION) == 0) {
5652 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
5653 "Received non-resolution source-change, ignoring.");
5657 if (v4l2object->formats)
5658 gst_v4l2_object_clear_format_list (v4l2object);
5660 return GST_V4L2_FLOW_RESOLUTION_CHANGE;
5672 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "stop called");
5673 return GST_FLOW_FLUSHING;
5677 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
5678 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
5679 return GST_FLOW_ERROR;
5683 return GST_V4L2_FLOW_LAST_BUFFER;
5687 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
5688 ("dqevent error: %s (%d)", g_strerror (errno), errno));
5689 return GST_FLOW_ERROR;
5694 * gst_v4l2_object_subscribe_event:
5695 * @v4l2object: a #GstV4l2Object
5696 * @event: the event ID
5698 * Subscribe to an event, and enable polling for these. Note that only
5699 * %V4L2_EVENT_SOURCE_CHANGE is currently supported by the poll helper.
5701 * Returns: %TRUE if the driver supports this event
5704 gst_v4l2_object_subscribe_event (GstV4l2Object * v4l2object, guint32 event)
5708 g_return_val_if_fail (v4l2object != NULL, FALSE);
5709 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), FALSE);
5711 v4l2object->get_in_out_func (v4l2object, &id);
5713 if (gst_v4l2_subscribe_event (v4l2object, event, id)) {
5714 gst_poll_fd_ctl_pri (v4l2object->poll, &v4l2object->pollfd, TRUE);