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,
25 #define _GNU_SOURCE /* Enable mmap64() */
33 #include <sys/ioctl.h>
37 #include <gudev/gudev.h>
40 #include "gstv4l2object.h"
41 #include "gstv4l2tuner.h"
42 #include "gstv4l2colorbalance.h"
44 #include "gst/gst-i18n-plugin.h"
46 #include <gst/video/video.h>
48 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
49 #define GST_CAT_DEFAULT v4l2_debug
51 #define DEFAULT_PROP_DEVICE_NAME NULL
52 #define DEFAULT_PROP_DEVICE_FD -1
53 #define DEFAULT_PROP_FLAGS 0
54 #define DEFAULT_PROP_TV_NORM 0
55 #define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
57 #define ENCODED_BUFFER_SIZE (2 * 1024 * 1024)
62 V4L2_STD_OBJECT_PROPS,
66 * common format / caps utilities:
70 GST_V4L2_RAW = 1 << 0,
71 GST_V4L2_CODEC = 1 << 1,
72 GST_V4L2_TRANSPORT = 1 << 2,
73 GST_V4L2_NO_PARSE = 1 << 3,
81 GstV4L2FormatFlags flags;
84 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
86 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
87 {V4L2_PIX_FMT_ARGB555, TRUE, GST_V4L2_RAW},
88 {V4L2_PIX_FMT_XRGB555, TRUE, GST_V4L2_RAW},
89 {V4L2_PIX_FMT_ARGB555X, TRUE, GST_V4L2_RAW},
90 {V4L2_PIX_FMT_XRGB555X, TRUE, GST_V4L2_RAW},
91 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
92 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
93 {V4L2_PIX_FMT_BGR666, TRUE, GST_V4L2_RAW},
94 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
95 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
96 {V4L2_PIX_FMT_ABGR32, TRUE, GST_V4L2_RAW},
97 {V4L2_PIX_FMT_XBGR32, TRUE, GST_V4L2_RAW},
98 {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
99 {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},
101 /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
102 {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
103 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
104 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
105 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
106 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
109 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
110 {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
111 {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
112 {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
113 {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
114 {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
115 {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
116 {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},
118 /* Palette formats */
119 {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},
121 /* Chrominance formats */
122 {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},
124 /* Luminance+Chrominance formats */
125 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
126 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
127 {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
128 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
129 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
130 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
131 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
132 {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
133 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
134 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
135 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
136 {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
137 {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
138 {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
139 {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
140 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
141 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
142 {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
143 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
144 {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
145 {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
147 /* two planes -- one Y, one Cr + Cb interleaved */
148 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
149 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
150 {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
151 {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
152 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
153 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
154 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
155 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
156 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
158 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
159 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
161 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
162 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
163 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_CODEC},
164 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_CODEC},
165 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_CODEC},
167 /* compressed formats */
168 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
169 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
170 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
171 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
172 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
173 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
174 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
175 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
176 {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
177 {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
178 {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
179 {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
180 {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
181 {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
182 {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
183 {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
184 {V4L2_PIX_FMT_VP9, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
186 /* Vendor-specific formats */
187 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
188 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
189 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
190 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
193 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
195 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
198 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
200 gst_v4l2_device_get_type (void)
202 static GType v4l2_device_type = 0;
204 if (v4l2_device_type == 0) {
205 static const GFlagsValue values[] = {
206 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
207 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
208 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
210 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
211 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
213 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
214 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
220 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
223 return v4l2_device_type;
226 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
228 gst_v4l2_tv_norm_get_type (void)
230 static GType v4l2_tv_norm = 0;
233 static const GEnumValue tv_norms[] = {
236 {V4L2_STD_NTSC, "NTSC", "NTSC"},
237 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
238 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
239 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
240 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
242 {V4L2_STD_PAL, "PAL", "PAL"},
243 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
244 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
245 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
246 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
247 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
248 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
249 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
250 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
251 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
252 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
253 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
254 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
255 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
256 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
258 {V4L2_STD_SECAM, "SECAM", "SECAM"},
259 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
260 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
261 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
262 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
263 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
264 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
265 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
266 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
267 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
272 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
279 gst_v4l2_io_mode_get_type (void)
281 static GType v4l2_io_mode = 0;
284 static const GEnumValue io_modes[] = {
285 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
286 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
287 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
288 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
289 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
290 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
295 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
301 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
302 const char *default_device)
304 g_object_class_install_property (gobject_class, PROP_DEVICE,
305 g_param_spec_string ("device", "Device", "Device location",
306 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
308 g_param_spec_string ("device-name", "Device name",
309 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
310 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
312 g_param_spec_int ("device-fd", "File descriptor",
313 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
314 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
315 g_object_class_install_property (gobject_class, PROP_FLAGS,
316 g_param_spec_flags ("flags", "Flags", "Device type flags",
317 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
318 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
321 * GstV4l2Src:brightness:
323 * Picture brightness, or more precisely, the black level
325 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
326 g_param_spec_int ("brightness", "Brightness",
327 "Picture brightness, or more precisely, the black level", G_MININT,
329 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
331 * GstV4l2Src:contrast:
333 * Picture contrast or luma gain
335 g_object_class_install_property (gobject_class, PROP_CONTRAST,
336 g_param_spec_int ("contrast", "Contrast",
337 "Picture contrast or luma gain", G_MININT,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
341 * GstV4l2Src:saturation:
343 * Picture color saturation or chroma gain
345 g_object_class_install_property (gobject_class, PROP_SATURATION,
346 g_param_spec_int ("saturation", "Saturation",
347 "Picture color saturation or chroma gain", G_MININT,
349 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
353 * Hue or color balance
355 g_object_class_install_property (gobject_class, PROP_HUE,
356 g_param_spec_int ("hue", "Hue",
357 "Hue or color balance", G_MININT,
359 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
366 g_object_class_install_property (gobject_class, PROP_TV_NORM,
367 g_param_spec_enum ("norm", "TV norm",
369 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
370 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 * GstV4l2Src:io-mode:
377 g_object_class_install_property (gobject_class, PROP_IO_MODE,
378 g_param_spec_enum ("io-mode", "IO mode",
380 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384 * GstV4l2Src:extra-controls:
386 * Additional v4l2 controls for the device. The controls are identified
387 * by the control name (lowercase with '_' for any non-alphanumeric
392 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
393 g_param_spec_boxed ("extra-controls", "Extra Controls",
394 "Extra v4l2 controls (CIDs) for the device",
395 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 * GstV4l2Src:pixel-aspect-ratio:
400 * The pixel aspect ratio of the device. This overwrites the pixel aspect
401 * ratio queried from the device.
405 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
406 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
407 "Overwrite the pixel aspect ratio of the device", "1/1",
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 * GstV4l2Src:force-aspect-ratio:
413 * When enabled, the pixel aspect ratio queried from the device or set
414 * with the pixel-aspect-ratio property will be enforced.
418 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
419 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
420 "When enabled, the pixel aspect ratio will be enforced", TRUE,
421 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
428 g_object_class_install_property (gobject_class, PROP_DEVICE,
429 g_param_spec_string ("device", "Device", "Device location",
430 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
432 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
433 g_param_spec_string ("device-name", "Device name",
434 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
435 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
438 g_param_spec_int ("device-fd", "File descriptor",
439 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
440 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
442 g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
443 g_param_spec_enum ("output-io-mode", "Output IO mode",
444 "Output side I/O mode (matches sink pad)",
445 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
446 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
449 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
450 "Capture I/O mode (matches src pad)",
451 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
452 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
454 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
455 g_param_spec_boxed ("extra-controls", "Extra Controls",
456 "Extra v4l2 controls (CIDs) for the device",
457 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 gst_v4l2_object_new (GstElement * element,
462 enum v4l2_buf_type type,
463 const char *default_device,
464 GstV4l2GetInOutFunction get_in_out_func,
465 GstV4l2SetInOutFunction set_in_out_func,
466 GstV4l2UpdateFpsFunction update_fps_func)
468 GstV4l2Object *v4l2object;
471 * some default values
473 v4l2object = g_new0 (GstV4l2Object, 1);
475 v4l2object->type = type;
476 v4l2object->formats = NULL;
478 v4l2object->element = element;
479 v4l2object->get_in_out_func = get_in_out_func;
480 v4l2object->set_in_out_func = set_in_out_func;
481 v4l2object->update_fps_func = update_fps_func;
483 v4l2object->video_fd = -1;
484 v4l2object->active = FALSE;
485 v4l2object->videodev = g_strdup (default_device);
487 v4l2object->norms = NULL;
488 v4l2object->channels = NULL;
489 v4l2object->colors = NULL;
491 v4l2object->keep_aspect = TRUE;
493 v4l2object->n_v4l2_planes = 0;
495 v4l2object->no_initial_format = FALSE;
497 /* We now disable libv4l2 by default, but have an env to enable it. */
499 if (g_getenv ("GST_V4L2_USE_LIBV4L2")) {
500 v4l2object->fd_open = v4l2_fd_open;
501 v4l2object->close = v4l2_close;
502 v4l2object->dup = v4l2_dup;
503 v4l2object->ioctl = v4l2_ioctl;
504 v4l2object->read = v4l2_read;
505 v4l2object->mmap = v4l2_mmap;
506 v4l2object->munmap = v4l2_munmap;
510 v4l2object->fd_open = NULL;
511 v4l2object->close = close;
512 v4l2object->dup = dup;
513 v4l2object->ioctl = ioctl;
514 v4l2object->read = read;
515 v4l2object->mmap = mmap64;
516 v4l2object->munmap = munmap;
522 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
526 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
528 g_return_if_fail (v4l2object != NULL);
530 g_free (v4l2object->videodev);
532 g_free (v4l2object->channel);
534 if (v4l2object->formats) {
535 gst_v4l2_object_clear_format_list (v4l2object);
538 if (v4l2object->probed_caps) {
539 gst_caps_unref (v4l2object->probed_caps);
542 if (v4l2object->extra_controls) {
543 gst_structure_free (v4l2object->extra_controls);
551 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
553 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
554 g_slist_free (v4l2object->formats);
555 v4l2object->formats = NULL;
561 gst_v4l2_object_prop_to_cid (guint prop_id)
566 case PROP_BRIGHTNESS:
567 cid = V4L2_CID_BRIGHTNESS;
570 cid = V4L2_CID_CONTRAST;
572 case PROP_SATURATION:
573 cid = V4L2_CID_SATURATION;
579 GST_WARNING ("unmapped property id: %d", prop_id);
586 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
587 guint prop_id, const GValue * value, GParamSpec * pspec)
591 g_free (v4l2object->videodev);
592 v4l2object->videodev = g_value_dup_string (value);
594 case PROP_BRIGHTNESS:
596 case PROP_SATURATION:
599 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
602 if (GST_V4L2_IS_OPEN (v4l2object)) {
603 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
610 v4l2object->tv_norm = g_value_get_enum (value);
614 if (GST_V4L2_IS_OPEN (v4l2object)) {
615 GstTuner *tuner = GST_TUNER (v4l2object->element);
616 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
617 (gchar *) g_value_get_string (value));
620 /* like gst_tuner_set_channel (tuner, channel)
621 without g_object_notify */
622 gst_v4l2_tuner_set_channel (v4l2object, channel);
625 g_free (v4l2object->channel);
626 v4l2object->channel = g_value_dup_string (value);
630 if (GST_V4L2_IS_OPEN (v4l2object)) {
631 GstTuner *tuner = GST_TUNER (v4l2object->element);
632 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
635 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
637 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
638 without g_object_notify */
639 gst_v4l2_tuner_set_frequency (v4l2object, channel,
640 g_value_get_ulong (value));
643 v4l2object->frequency = g_value_get_ulong (value);
649 v4l2object->req_mode = g_value_get_enum (value);
651 case PROP_CAPTURE_IO_MODE:
652 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
653 v4l2object->req_mode = g_value_get_enum (value);
655 case PROP_OUTPUT_IO_MODE:
656 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
657 v4l2object->req_mode = g_value_get_enum (value);
659 case PROP_EXTRA_CONTROLS:{
660 const GstStructure *s = gst_value_get_structure (value);
662 if (v4l2object->extra_controls)
663 gst_structure_free (v4l2object->extra_controls);
665 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
666 if (GST_V4L2_IS_OPEN (v4l2object))
667 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
670 case PROP_PIXEL_ASPECT_RATIO:
671 g_free (v4l2object->par);
672 v4l2object->par = g_new0 (GValue, 1);
673 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
674 if (!g_value_transform (value, v4l2object->par)) {
675 g_warning ("Could not transform string to aspect ratio");
676 gst_value_set_fraction (v4l2object->par, 1, 1);
678 GST_DEBUG_OBJECT (v4l2object->element, "set PAR to %d/%d",
679 gst_value_get_fraction_numerator (v4l2object->par),
680 gst_value_get_fraction_denominator (v4l2object->par));
682 case PROP_FORCE_ASPECT_RATIO:
683 v4l2object->keep_aspect = g_value_get_boolean (value);
694 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
695 guint prop_id, GValue * value, GParamSpec * pspec)
699 g_value_set_string (value, v4l2object->videodev);
701 case PROP_DEVICE_NAME:
703 const guchar *new = NULL;
705 if (GST_V4L2_IS_OPEN (v4l2object)) {
706 new = v4l2object->vcap.card;
707 } else if (gst_v4l2_open (v4l2object)) {
708 new = v4l2object->vcap.card;
709 gst_v4l2_close (v4l2object);
711 g_value_set_string (value, (gchar *) new);
716 if (GST_V4L2_IS_OPEN (v4l2object))
717 g_value_set_int (value, v4l2object->video_fd);
719 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
726 if (GST_V4L2_IS_OPEN (v4l2object)) {
727 flags |= v4l2object->device_caps &
728 (V4L2_CAP_VIDEO_CAPTURE |
729 V4L2_CAP_VIDEO_OUTPUT |
730 V4L2_CAP_VIDEO_OVERLAY |
731 V4L2_CAP_VBI_CAPTURE |
732 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
734 if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
735 flags |= V4L2_CAP_VIDEO_CAPTURE;
737 if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
738 flags |= V4L2_CAP_VIDEO_OUTPUT;
740 g_value_set_flags (value, flags);
743 case PROP_BRIGHTNESS:
745 case PROP_SATURATION:
748 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
751 if (GST_V4L2_IS_OPEN (v4l2object)) {
753 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
754 g_value_set_int (value, v);
762 g_value_set_enum (value, v4l2object->tv_norm);
765 g_value_set_enum (value, v4l2object->req_mode);
767 case PROP_CAPTURE_IO_MODE:
768 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
769 g_value_set_enum (value, v4l2object->req_mode);
771 case PROP_OUTPUT_IO_MODE:
772 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
773 g_value_set_enum (value, v4l2object->req_mode);
775 case PROP_EXTRA_CONTROLS:
776 gst_value_set_structure (value, v4l2object->extra_controls);
778 case PROP_PIXEL_ASPECT_RATIO:
780 g_value_transform (v4l2object->par, value);
782 case PROP_FORCE_ASPECT_RATIO:
783 g_value_set_boolean (value, v4l2object->keep_aspect);
793 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
795 struct v4l2_control control = { 0, };
797 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
799 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
800 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
802 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
804 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
805 GST_DEBUG_OBJECT (v4l2object->element,
806 "driver requires a minimum of %d buffers", control.value);
807 v4l2object->min_buffers = control.value;
809 v4l2object->min_buffers = 0;
814 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
816 GstTunerNorm *norm = NULL;
817 GstTunerChannel *channel = NULL;
820 if (!GST_IS_TUNER (v4l2object->element))
823 tuner = GST_TUNER (v4l2object->element);
825 if (v4l2object->tv_norm)
826 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
827 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
828 "norm=%p", (guint64) v4l2object->tv_norm, norm);
830 gst_tuner_set_norm (tuner, norm);
833 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
835 v4l2object->tv_norm =
836 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
837 gst_tuner_norm_changed (tuner, norm);
841 if (v4l2object->channel)
842 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
844 gst_tuner_set_channel (tuner, channel);
847 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
848 (v4l2object->element)));
850 g_free (v4l2object->channel);
851 v4l2object->channel = g_strdup (channel->label);
852 gst_tuner_channel_changed (tuner, channel);
857 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
858 if (v4l2object->frequency != 0) {
859 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
861 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
862 if (v4l2object->frequency == 0) {
864 gst_tuner_set_frequency (tuner, channel, 1000);
872 gst_v4l2_object_open (GstV4l2Object * v4l2object)
874 if (gst_v4l2_open (v4l2object))
875 gst_v4l2_set_defaults (v4l2object);
883 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
887 ret = gst_v4l2_dup (v4l2object, other);
893 gst_v4l2_object_close (GstV4l2Object * v4l2object)
895 if (!gst_v4l2_close (v4l2object))
898 gst_caps_replace (&v4l2object->probed_caps, NULL);
900 /* reset our copy of the device caps */
901 v4l2object->device_caps = 0;
903 if (v4l2object->formats) {
904 gst_v4l2_object_clear_format_list (v4l2object);
910 static struct v4l2_fmtdesc *
911 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
914 struct v4l2_fmtdesc *fmt;
920 walk = gst_v4l2_object_get_format_list (v4l2object);
922 fmt = (struct v4l2_fmtdesc *) walk->data;
923 if (fmt->pixelformat == fourcc)
925 /* special case for jpeg */
926 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
927 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
928 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
929 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
930 fourcc == V4L2_PIX_FMT_PJPG) {
934 walk = g_slist_next (walk);
942 /* complete made up ranking, the values themselves are meaningless */
943 /* These ranks MUST be X such that X<<15 fits on a signed int - see
944 the comment at the end of gst_v4l2_object_format_get_rank. */
945 #define YUV_BASE_RANK 1000
946 #define JPEG_BASE_RANK 500
947 #define DV_BASE_RANK 200
948 #define RGB_BASE_RANK 100
949 #define YUV_ODD_BASE_RANK 50
950 #define RGB_ODD_BASE_RANK 25
951 #define BAYER_BASE_RANK 15
952 #define S910_BASE_RANK 10
953 #define GREY_BASE_RANK 5
954 #define PWC_BASE_RANK 1
957 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
959 guint32 fourcc = fmt->pixelformat;
960 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
964 case V4L2_PIX_FMT_MJPEG:
965 case V4L2_PIX_FMT_PJPG:
966 rank = JPEG_BASE_RANK;
968 case V4L2_PIX_FMT_JPEG:
969 rank = JPEG_BASE_RANK + 1;
971 case V4L2_PIX_FMT_MPEG: /* MPEG */
972 rank = JPEG_BASE_RANK + 2;
975 case V4L2_PIX_FMT_RGB332:
976 case V4L2_PIX_FMT_ARGB555:
977 case V4L2_PIX_FMT_XRGB555:
978 case V4L2_PIX_FMT_RGB555:
979 case V4L2_PIX_FMT_ARGB555X:
980 case V4L2_PIX_FMT_XRGB555X:
981 case V4L2_PIX_FMT_RGB555X:
982 case V4L2_PIX_FMT_BGR666:
983 case V4L2_PIX_FMT_RGB565:
984 case V4L2_PIX_FMT_RGB565X:
985 case V4L2_PIX_FMT_RGB444:
986 case V4L2_PIX_FMT_Y4:
987 case V4L2_PIX_FMT_Y6:
988 case V4L2_PIX_FMT_Y10:
989 case V4L2_PIX_FMT_Y12:
990 case V4L2_PIX_FMT_Y10BPACK:
991 case V4L2_PIX_FMT_YUV555:
992 case V4L2_PIX_FMT_YUV565:
993 case V4L2_PIX_FMT_YUV32:
994 case V4L2_PIX_FMT_NV12MT_16X16:
995 case V4L2_PIX_FMT_NV42:
996 case V4L2_PIX_FMT_H264_MVC:
997 rank = RGB_ODD_BASE_RANK;
1000 case V4L2_PIX_FMT_RGB24:
1001 case V4L2_PIX_FMT_BGR24:
1002 rank = RGB_BASE_RANK - 1;
1005 case V4L2_PIX_FMT_RGB32:
1006 case V4L2_PIX_FMT_BGR32:
1007 case V4L2_PIX_FMT_ABGR32:
1008 case V4L2_PIX_FMT_XBGR32:
1009 case V4L2_PIX_FMT_ARGB32:
1010 case V4L2_PIX_FMT_XRGB32:
1011 rank = RGB_BASE_RANK;
1014 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1015 rank = GREY_BASE_RANK;
1018 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1019 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
1020 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
1021 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1022 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1023 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1024 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1025 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1026 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1027 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1028 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1029 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1030 rank = YUV_ODD_BASE_RANK;
1033 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1034 rank = YUV_BASE_RANK + 3;
1036 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1037 rank = YUV_BASE_RANK + 2;
1039 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1040 case V4L2_PIX_FMT_YUV420M:
1041 rank = YUV_BASE_RANK + 7;
1043 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1044 rank = YUV_BASE_RANK + 10;
1046 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1047 rank = YUV_BASE_RANK + 6;
1049 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1050 rank = YUV_BASE_RANK + 9;
1052 case V4L2_PIX_FMT_YUV444:
1053 rank = YUV_BASE_RANK + 6;
1055 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1056 rank = YUV_BASE_RANK + 5;
1058 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1059 rank = YUV_BASE_RANK + 4;
1061 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1062 rank = YUV_BASE_RANK + 8;
1065 case V4L2_PIX_FMT_DV:
1066 rank = DV_BASE_RANK;
1069 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1073 case V4L2_PIX_FMT_SBGGR8:
1074 case V4L2_PIX_FMT_SGBRG8:
1075 case V4L2_PIX_FMT_SGRBG8:
1076 case V4L2_PIX_FMT_SRGGB8:
1077 rank = BAYER_BASE_RANK;
1080 case V4L2_PIX_FMT_SN9C10X:
1081 rank = S910_BASE_RANK;
1084 case V4L2_PIX_FMT_PWC1:
1085 rank = PWC_BASE_RANK;
1087 case V4L2_PIX_FMT_PWC2:
1088 rank = PWC_BASE_RANK;
1096 /* All ranks are below 1<<15 so a shift by 15
1097 * will a) make all non-emulated formats larger
1098 * than emulated and b) will not overflow
1109 format_cmp_func (gconstpointer a, gconstpointer b)
1111 const struct v4l2_fmtdesc *fa = a;
1112 const struct v4l2_fmtdesc *fb = b;
1114 if (fa->pixelformat == fb->pixelformat)
1117 return gst_v4l2_object_format_get_rank (fb) -
1118 gst_v4l2_object_format_get_rank (fa);
1121 /******************************************************
1122 * gst_v4l2_object_fill_format_list():
1123 * create list of supported capture formats
1124 * return value: TRUE on success, FALSE on error
1125 ******************************************************/
1127 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1128 enum v4l2_buf_type type)
1131 struct v4l2_fmtdesc *format;
1133 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1135 /* format enumeration */
1137 format = g_new0 (struct v4l2_fmtdesc, 1);
1140 format->type = type;
1142 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1143 if (errno == EINVAL) {
1145 break; /* end of enumeration */
1151 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1152 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1153 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1154 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1155 format->description);
1156 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1157 GST_FOURCC_ARGS (format->pixelformat));
1159 /* sort formats according to our preference; we do this, because caps
1160 * are probed in the order the formats are in the list, and the order of
1161 * formats in the final probed caps matters for things like fixation */
1162 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1163 (GCompareFunc) format_cmp_func);
1166 #ifndef GST_DISABLE_GST_DEBUG
1170 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1171 for (l = v4l2object->formats; l != NULL; l = l->next) {
1174 GST_INFO_OBJECT (v4l2object->element,
1175 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1176 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1188 if (!GST_IS_ELEMENT (v4l2object->element))
1191 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1192 (_("Failed to enumerate possible video formats device '%s' can work "
1193 "with"), v4l2object->videodev),
1194 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1195 n, v4l2object->videodev, errno, g_strerror (errno)));
1202 * Get the list of supported capture formats, a list of
1203 * <code>struct v4l2_fmtdesc</code>.
1206 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1208 if (!v4l2object->formats) {
1210 /* check usual way */
1211 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1213 /* if our driver supports multi-planar
1214 * and if formats are still empty then we can workaround driver bug
1215 * by also looking up formats as if our device was not supporting
1217 if (!v4l2object->formats) {
1218 switch (v4l2object->type) {
1219 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1220 gst_v4l2_object_fill_format_list (v4l2object,
1221 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1224 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1225 gst_v4l2_object_fill_format_list (v4l2object,
1226 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1234 return v4l2object->formats;
1237 static GstVideoFormat
1238 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1240 GstVideoFormat format;
1243 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1244 format = GST_VIDEO_FORMAT_GRAY8;
1246 case V4L2_PIX_FMT_Y16:
1247 format = GST_VIDEO_FORMAT_GRAY16_LE;
1249 case V4L2_PIX_FMT_Y16_BE:
1250 format = GST_VIDEO_FORMAT_GRAY16_BE;
1252 case V4L2_PIX_FMT_XRGB555:
1253 case V4L2_PIX_FMT_RGB555:
1254 format = GST_VIDEO_FORMAT_RGB15;
1256 case V4L2_PIX_FMT_XRGB555X:
1257 case V4L2_PIX_FMT_RGB555X:
1258 format = GST_VIDEO_FORMAT_BGR15;
1260 case V4L2_PIX_FMT_RGB565:
1261 format = GST_VIDEO_FORMAT_RGB16;
1263 case V4L2_PIX_FMT_RGB24:
1264 format = GST_VIDEO_FORMAT_RGB;
1266 case V4L2_PIX_FMT_BGR24:
1267 format = GST_VIDEO_FORMAT_BGR;
1269 case V4L2_PIX_FMT_XRGB32:
1270 case V4L2_PIX_FMT_RGB32:
1271 format = GST_VIDEO_FORMAT_xRGB;
1273 case V4L2_PIX_FMT_XBGR32:
1274 case V4L2_PIX_FMT_BGR32:
1275 format = GST_VIDEO_FORMAT_BGRx;
1277 case V4L2_PIX_FMT_ABGR32:
1278 format = GST_VIDEO_FORMAT_BGRA;
1280 case V4L2_PIX_FMT_ARGB32:
1281 format = GST_VIDEO_FORMAT_ARGB;
1283 case V4L2_PIX_FMT_NV12:
1284 case V4L2_PIX_FMT_NV12M:
1285 format = GST_VIDEO_FORMAT_NV12;
1287 case V4L2_PIX_FMT_NV12MT:
1288 format = GST_VIDEO_FORMAT_NV12_64Z32;
1290 case V4L2_PIX_FMT_NV21:
1291 case V4L2_PIX_FMT_NV21M:
1292 format = GST_VIDEO_FORMAT_NV21;
1294 case V4L2_PIX_FMT_YVU410:
1295 format = GST_VIDEO_FORMAT_YVU9;
1297 case V4L2_PIX_FMT_YUV410:
1298 format = GST_VIDEO_FORMAT_YUV9;
1300 case V4L2_PIX_FMT_YUV420:
1301 case V4L2_PIX_FMT_YUV420M:
1302 format = GST_VIDEO_FORMAT_I420;
1304 case V4L2_PIX_FMT_YUYV:
1305 format = GST_VIDEO_FORMAT_YUY2;
1307 case V4L2_PIX_FMT_YVU420:
1308 format = GST_VIDEO_FORMAT_YV12;
1310 case V4L2_PIX_FMT_UYVY:
1311 format = GST_VIDEO_FORMAT_UYVY;
1313 case V4L2_PIX_FMT_YUV411P:
1314 format = GST_VIDEO_FORMAT_Y41B;
1316 case V4L2_PIX_FMT_YUV422P:
1317 format = GST_VIDEO_FORMAT_Y42B;
1319 case V4L2_PIX_FMT_YVYU:
1320 format = GST_VIDEO_FORMAT_YVYU;
1322 case V4L2_PIX_FMT_NV16:
1323 case V4L2_PIX_FMT_NV16M:
1324 format = GST_VIDEO_FORMAT_NV16;
1326 case V4L2_PIX_FMT_NV61:
1327 case V4L2_PIX_FMT_NV61M:
1328 format = GST_VIDEO_FORMAT_NV61;
1330 case V4L2_PIX_FMT_NV24:
1331 format = GST_VIDEO_FORMAT_NV24;
1334 format = GST_VIDEO_FORMAT_UNKNOWN;
1342 gst_v4l2_object_v4l2fourcc_is_rgb (guint32 fourcc)
1344 gboolean ret = FALSE;
1347 case V4L2_PIX_FMT_XRGB555:
1348 case V4L2_PIX_FMT_RGB555:
1349 case V4L2_PIX_FMT_XRGB555X:
1350 case V4L2_PIX_FMT_RGB555X:
1351 case V4L2_PIX_FMT_RGB565:
1352 case V4L2_PIX_FMT_RGB24:
1353 case V4L2_PIX_FMT_BGR24:
1354 case V4L2_PIX_FMT_XRGB32:
1355 case V4L2_PIX_FMT_RGB32:
1356 case V4L2_PIX_FMT_XBGR32:
1357 case V4L2_PIX_FMT_BGR32:
1358 case V4L2_PIX_FMT_ABGR32:
1359 case V4L2_PIX_FMT_ARGB32:
1369 static GstStructure *
1370 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1372 GstStructure *structure = NULL;
1375 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1376 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1377 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1378 structure = gst_structure_new_empty ("image/jpeg");
1380 case V4L2_PIX_FMT_MPEG1:
1381 structure = gst_structure_new ("video/mpeg",
1382 "mpegversion", G_TYPE_INT, 2, NULL);
1384 case V4L2_PIX_FMT_MPEG2:
1385 structure = gst_structure_new ("video/mpeg",
1386 "mpegversion", G_TYPE_INT, 2, NULL);
1388 case V4L2_PIX_FMT_MPEG4:
1389 case V4L2_PIX_FMT_XVID:
1390 structure = gst_structure_new ("video/mpeg",
1391 "mpegversion", G_TYPE_INT, 4, "systemstream",
1392 G_TYPE_BOOLEAN, FALSE, NULL);
1394 case V4L2_PIX_FMT_H263:
1395 structure = gst_structure_new ("video/x-h263",
1396 "variant", G_TYPE_STRING, "itu", NULL);
1398 case V4L2_PIX_FMT_H264: /* H.264 */
1399 structure = gst_structure_new ("video/x-h264",
1400 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1401 G_TYPE_STRING, "au", NULL);
1403 case V4L2_PIX_FMT_H264_NO_SC:
1404 structure = gst_structure_new ("video/x-h264",
1405 "stream-format", G_TYPE_STRING, "avc", "alignment",
1406 G_TYPE_STRING, "au", NULL);
1408 case V4L2_PIX_FMT_VC1_ANNEX_G:
1409 case V4L2_PIX_FMT_VC1_ANNEX_L:
1410 structure = gst_structure_new ("video/x-wmv",
1411 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1413 case V4L2_PIX_FMT_VP8:
1414 structure = gst_structure_new_empty ("video/x-vp8");
1416 case V4L2_PIX_FMT_VP9:
1417 structure = gst_structure_new_empty ("video/x-vp9");
1419 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1420 case V4L2_PIX_FMT_Y16:
1421 case V4L2_PIX_FMT_Y16_BE:
1422 case V4L2_PIX_FMT_XRGB555:
1423 case V4L2_PIX_FMT_RGB555:
1424 case V4L2_PIX_FMT_XRGB555X:
1425 case V4L2_PIX_FMT_RGB555X:
1426 case V4L2_PIX_FMT_RGB565:
1427 case V4L2_PIX_FMT_RGB24:
1428 case V4L2_PIX_FMT_BGR24:
1429 case V4L2_PIX_FMT_RGB32:
1430 case V4L2_PIX_FMT_XRGB32:
1431 case V4L2_PIX_FMT_ARGB32:
1432 case V4L2_PIX_FMT_BGR32:
1433 case V4L2_PIX_FMT_XBGR32:
1434 case V4L2_PIX_FMT_ABGR32:
1435 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1436 case V4L2_PIX_FMT_NV12M:
1437 case V4L2_PIX_FMT_NV12MT:
1438 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1439 case V4L2_PIX_FMT_NV21M:
1440 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1441 case V4L2_PIX_FMT_NV16M:
1442 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1443 case V4L2_PIX_FMT_NV61M:
1444 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1445 case V4L2_PIX_FMT_YVU410:
1446 case V4L2_PIX_FMT_YUV410:
1447 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1448 case V4L2_PIX_FMT_YUV420M:
1449 case V4L2_PIX_FMT_YUYV:
1450 case V4L2_PIX_FMT_YVU420:
1451 case V4L2_PIX_FMT_UYVY:
1452 case V4L2_PIX_FMT_YUV422P:
1453 case V4L2_PIX_FMT_YVYU:
1454 case V4L2_PIX_FMT_YUV411P:{
1455 GstVideoFormat format;
1456 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1457 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1458 structure = gst_structure_new ("video/x-raw",
1459 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1462 case V4L2_PIX_FMT_DV:
1464 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1467 case V4L2_PIX_FMT_MPEG: /* MPEG */
1468 structure = gst_structure_new ("video/mpegts",
1469 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1471 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1473 case V4L2_PIX_FMT_SBGGR8:
1474 case V4L2_PIX_FMT_SGBRG8:
1475 case V4L2_PIX_FMT_SGRBG8:
1476 case V4L2_PIX_FMT_SRGGB8:
1477 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1478 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1479 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1480 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1481 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1483 case V4L2_PIX_FMT_SN9C10X:
1484 structure = gst_structure_new_empty ("video/x-sonix");
1486 case V4L2_PIX_FMT_PWC1:
1487 structure = gst_structure_new_empty ("video/x-pwc1");
1489 case V4L2_PIX_FMT_PWC2:
1490 structure = gst_structure_new_empty ("video/x-pwc2");
1492 case V4L2_PIX_FMT_RGB332:
1493 case V4L2_PIX_FMT_BGR666:
1494 case V4L2_PIX_FMT_ARGB555X:
1495 case V4L2_PIX_FMT_RGB565X:
1496 case V4L2_PIX_FMT_RGB444:
1497 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1498 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1499 case V4L2_PIX_FMT_Y4:
1500 case V4L2_PIX_FMT_Y6:
1501 case V4L2_PIX_FMT_Y10:
1502 case V4L2_PIX_FMT_Y12:
1503 case V4L2_PIX_FMT_Y10BPACK:
1504 case V4L2_PIX_FMT_YUV444:
1505 case V4L2_PIX_FMT_YUV555:
1506 case V4L2_PIX_FMT_YUV565:
1507 case V4L2_PIX_FMT_Y41P:
1508 case V4L2_PIX_FMT_YUV32:
1509 case V4L2_PIX_FMT_NV12MT_16X16:
1510 case V4L2_PIX_FMT_NV42:
1511 case V4L2_PIX_FMT_H264_MVC:
1513 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1514 fourcc, GST_FOURCC_ARGS (fourcc));
1522 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1524 GstStructure *template;
1527 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1529 if (template == NULL)
1532 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1533 if (gst_v4l2_formats[i].format != fourcc)
1536 if (gst_v4l2_formats[i].dimensions) {
1537 gst_structure_set (template,
1538 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1539 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1540 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1551 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1553 GstStructure *structure;
1557 caps = gst_caps_new_empty ();
1558 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1560 if ((gst_v4l2_formats[i].flags & flags) == 0)
1564 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1567 GstStructure *alt_s = NULL;
1569 if (gst_v4l2_formats[i].dimensions) {
1570 gst_structure_set (structure,
1571 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1572 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1573 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1576 switch (gst_v4l2_formats[i].format) {
1577 case V4L2_PIX_FMT_RGB32:
1578 alt_s = gst_structure_copy (structure);
1579 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1581 case V4L2_PIX_FMT_BGR32:
1582 alt_s = gst_structure_copy (structure);
1583 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1588 gst_caps_append_structure (caps, structure);
1591 gst_caps_append_structure (caps, alt_s);
1595 return gst_caps_simplify (caps);
1599 gst_v4l2_object_get_all_caps (void)
1601 static GstCaps *caps = NULL;
1603 if (g_once_init_enter (&caps)) {
1604 GstCaps *all_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1605 GST_MINI_OBJECT_FLAG_SET (all_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1606 g_once_init_leave (&caps, all_caps);
1613 gst_v4l2_object_get_raw_caps (void)
1615 static GstCaps *caps = NULL;
1617 if (g_once_init_enter (&caps)) {
1618 GstCaps *raw_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1619 GST_MINI_OBJECT_FLAG_SET (raw_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1620 g_once_init_leave (&caps, raw_caps);
1627 gst_v4l2_object_get_codec_caps (void)
1629 static GstCaps *caps = NULL;
1631 if (g_once_init_enter (&caps)) {
1632 GstCaps *codec_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1633 GST_MINI_OBJECT_FLAG_SET (codec_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1634 g_once_init_leave (&caps, codec_caps);
1640 /* collect data for the given caps
1641 * @caps: given input caps
1642 * @format: location for the v4l format
1643 * @w/@h: location for width and height
1644 * @fps_n/@fps_d: location for framerate
1645 * @size: location for expected size of the frame or 0 if unknown
1648 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1649 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1651 GstStructure *structure;
1652 guint32 fourcc = 0, fourcc_nc = 0;
1653 const gchar *mimetype;
1654 struct v4l2_fmtdesc *fmt = NULL;
1656 structure = gst_caps_get_structure (caps, 0);
1658 mimetype = gst_structure_get_name (structure);
1660 if (!gst_video_info_from_caps (info, caps))
1661 goto invalid_format;
1663 if (g_str_equal (mimetype, "video/x-raw")) {
1664 switch (GST_VIDEO_INFO_FORMAT (info)) {
1665 case GST_VIDEO_FORMAT_I420:
1666 fourcc = V4L2_PIX_FMT_YUV420;
1667 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1669 case GST_VIDEO_FORMAT_YUY2:
1670 fourcc = V4L2_PIX_FMT_YUYV;
1672 case GST_VIDEO_FORMAT_UYVY:
1673 fourcc = V4L2_PIX_FMT_UYVY;
1675 case GST_VIDEO_FORMAT_YV12:
1676 fourcc = V4L2_PIX_FMT_YVU420;
1678 case GST_VIDEO_FORMAT_Y41B:
1679 fourcc = V4L2_PIX_FMT_YUV411P;
1681 case GST_VIDEO_FORMAT_Y42B:
1682 fourcc = V4L2_PIX_FMT_YUV422P;
1684 case GST_VIDEO_FORMAT_NV12:
1685 fourcc = V4L2_PIX_FMT_NV12;
1686 fourcc_nc = V4L2_PIX_FMT_NV12M;
1688 case GST_VIDEO_FORMAT_NV12_64Z32:
1689 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1691 case GST_VIDEO_FORMAT_NV21:
1692 fourcc = V4L2_PIX_FMT_NV21;
1693 fourcc_nc = V4L2_PIX_FMT_NV21M;
1695 case GST_VIDEO_FORMAT_NV16:
1696 fourcc = V4L2_PIX_FMT_NV16;
1697 fourcc_nc = V4L2_PIX_FMT_NV16M;
1699 case GST_VIDEO_FORMAT_NV61:
1700 fourcc = V4L2_PIX_FMT_NV61;
1701 fourcc_nc = V4L2_PIX_FMT_NV61M;
1703 case GST_VIDEO_FORMAT_NV24:
1704 fourcc = V4L2_PIX_FMT_NV24;
1706 case GST_VIDEO_FORMAT_YVYU:
1707 fourcc = V4L2_PIX_FMT_YVYU;
1709 case GST_VIDEO_FORMAT_RGB15:
1710 fourcc = V4L2_PIX_FMT_RGB555;
1711 fourcc_nc = V4L2_PIX_FMT_XRGB555;
1713 case GST_VIDEO_FORMAT_RGB16:
1714 fourcc = V4L2_PIX_FMT_RGB565;
1716 case GST_VIDEO_FORMAT_RGB:
1717 fourcc = V4L2_PIX_FMT_RGB24;
1719 case GST_VIDEO_FORMAT_BGR:
1720 fourcc = V4L2_PIX_FMT_BGR24;
1722 case GST_VIDEO_FORMAT_xRGB:
1723 fourcc = V4L2_PIX_FMT_RGB32;
1724 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1726 case GST_VIDEO_FORMAT_ARGB:
1727 fourcc = V4L2_PIX_FMT_RGB32;
1728 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1730 case GST_VIDEO_FORMAT_BGRx:
1731 fourcc = V4L2_PIX_FMT_BGR32;
1732 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1734 case GST_VIDEO_FORMAT_BGRA:
1735 fourcc = V4L2_PIX_FMT_BGR32;
1736 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1738 case GST_VIDEO_FORMAT_GRAY8:
1739 fourcc = V4L2_PIX_FMT_GREY;
1741 case GST_VIDEO_FORMAT_GRAY16_LE:
1742 fourcc = V4L2_PIX_FMT_Y16;
1744 case GST_VIDEO_FORMAT_GRAY16_BE:
1745 fourcc = V4L2_PIX_FMT_Y16_BE;
1751 if (g_str_equal (mimetype, "video/mpegts")) {
1752 fourcc = V4L2_PIX_FMT_MPEG;
1753 } else if (g_str_equal (mimetype, "video/x-dv")) {
1754 fourcc = V4L2_PIX_FMT_DV;
1755 } else if (g_str_equal (mimetype, "image/jpeg")) {
1756 fourcc = V4L2_PIX_FMT_JPEG;
1757 } else if (g_str_equal (mimetype, "video/mpeg")) {
1759 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1762 fourcc = V4L2_PIX_FMT_MPEG1;
1765 fourcc = V4L2_PIX_FMT_MPEG2;
1768 fourcc = V4L2_PIX_FMT_MPEG4;
1769 fourcc_nc = V4L2_PIX_FMT_XVID;
1775 } else if (g_str_equal (mimetype, "video/x-h263")) {
1776 fourcc = V4L2_PIX_FMT_H263;
1777 } else if (g_str_equal (mimetype, "video/x-h264")) {
1778 const gchar *stream_format =
1779 gst_structure_get_string (structure, "stream-format");
1780 if (g_str_equal (stream_format, "avc"))
1781 fourcc = V4L2_PIX_FMT_H264_NO_SC;
1783 fourcc = V4L2_PIX_FMT_H264;
1784 } else if (g_str_equal (mimetype, "video/x-vp8")) {
1785 fourcc = V4L2_PIX_FMT_VP8;
1786 } else if (g_str_equal (mimetype, "video/x-vp9")) {
1787 fourcc = V4L2_PIX_FMT_VP9;
1788 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1789 const gchar *format = gst_structure_get_string (structure, "format");
1791 if (!g_ascii_strcasecmp (format, "bggr"))
1792 fourcc = V4L2_PIX_FMT_SBGGR8;
1793 else if (!g_ascii_strcasecmp (format, "gbrg"))
1794 fourcc = V4L2_PIX_FMT_SGBRG8;
1795 else if (!g_ascii_strcasecmp (format, "grbg"))
1796 fourcc = V4L2_PIX_FMT_SGRBG8;
1797 else if (!g_ascii_strcasecmp (format, "rggb"))
1798 fourcc = V4L2_PIX_FMT_SRGGB8;
1800 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1801 fourcc = V4L2_PIX_FMT_SN9C10X;
1802 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1803 fourcc = V4L2_PIX_FMT_PWC1;
1804 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1805 fourcc = V4L2_PIX_FMT_PWC2;
1810 /* Prefer the non-contiguous if supported */
1811 v4l2object->prefered_non_contiguous = TRUE;
1814 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
1815 else if (fourcc == 0)
1816 goto unhandled_format;
1819 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1820 v4l2object->prefered_non_contiguous = FALSE;
1824 goto unsupported_format;
1833 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1838 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1843 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1849 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1850 guint32 pixelformat, gint * width, gint * height);
1853 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1855 if (v4l2object->keep_aspect && v4l2object->par)
1856 gst_structure_set_value (s, "pixel-aspect-ratio", v4l2object->par);
1859 /* returns TRUE if the value was changed in place, otherwise FALSE */
1861 gst_v4l2src_value_simplify (GValue * val)
1863 /* simplify list of one value to one value */
1864 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
1865 const GValue *list_val;
1866 GValue new_val = G_VALUE_INIT;
1868 list_val = gst_value_list_get_value (val, 0);
1869 g_value_init (&new_val, G_VALUE_TYPE (list_val));
1870 g_value_copy (list_val, &new_val);
1871 g_value_unset (val);
1880 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
1881 GstVideoInterlaceMode * interlace_mode)
1883 /* NB: If you add new return values, please fix mode_strings in
1884 * gst_v4l2_object_add_interlace_mode */
1886 case V4L2_FIELD_ANY:
1888 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
1890 case V4L2_FIELD_NONE:
1891 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1893 case V4L2_FIELD_INTERLACED:
1894 case V4L2_FIELD_INTERLACED_TB:
1895 case V4L2_FIELD_INTERLACED_BT:
1896 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1899 GST_ERROR ("Unknown enum v4l2_field %d", field);
1905 gst_v4l2_object_get_colorspace (struct v4l2_format *fmt,
1906 GstVideoColorimetry * cinfo)
1909 gst_v4l2_object_v4l2fourcc_is_rgb (fmt->fmt.pix.pixelformat);
1910 enum v4l2_colorspace colorspace;
1911 enum v4l2_quantization range;
1912 enum v4l2_ycbcr_encoding matrix;
1913 enum v4l2_xfer_func transfer;
1914 gboolean ret = TRUE;
1916 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
1917 colorspace = fmt->fmt.pix_mp.colorspace;
1918 range = fmt->fmt.pix_mp.quantization;
1919 matrix = fmt->fmt.pix_mp.ycbcr_enc;
1920 transfer = fmt->fmt.pix_mp.xfer_func;
1922 colorspace = fmt->fmt.pix.colorspace;
1923 range = fmt->fmt.pix.quantization;
1924 matrix = fmt->fmt.pix.ycbcr_enc;
1925 transfer = fmt->fmt.pix.xfer_func;
1928 /* First step, set the defaults for each primaries */
1929 switch (colorspace) {
1930 case V4L2_COLORSPACE_SMPTE170M:
1931 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1932 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1933 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1934 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1936 case V4L2_COLORSPACE_REC709:
1937 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1938 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1939 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1940 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1942 case V4L2_COLORSPACE_SRGB:
1943 case V4L2_COLORSPACE_JPEG:
1944 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1945 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1946 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
1947 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1949 case V4L2_COLORSPACE_ADOBERGB:
1950 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1951 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1952 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
1953 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
1955 case V4L2_COLORSPACE_BT2020:
1956 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1957 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1958 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
1959 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
1961 case V4L2_COLORSPACE_SMPTE240M:
1962 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1963 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1964 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
1965 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
1967 case V4L2_COLORSPACE_470_SYSTEM_M:
1968 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1969 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1970 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1971 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
1973 case V4L2_COLORSPACE_470_SYSTEM_BG:
1974 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1975 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1976 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1977 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
1979 case V4L2_COLORSPACE_RAW:
1980 /* Explicitly unknown */
1981 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
1982 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
1983 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
1984 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
1987 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
1995 /* Second step, apply any custom variation */
1997 case V4L2_QUANTIZATION_FULL_RANGE:
1998 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2000 case V4L2_QUANTIZATION_LIM_RANGE:
2001 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2003 case V4L2_QUANTIZATION_DEFAULT:
2004 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
2005 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
2006 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2007 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601
2008 || matrix == V4L2_YCBCR_ENC_XV709
2009 || colorspace == V4L2_COLORSPACE_JPEG)
2010 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2012 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2015 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
2016 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2021 case V4L2_YCBCR_ENC_XV601:
2022 case V4L2_YCBCR_ENC_SYCC:
2023 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
2025 case V4L2_YCBCR_ENC_601:
2026 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2028 case V4L2_YCBCR_ENC_XV709:
2029 GST_FIXME ("XV709 not defined, assuming 709");
2031 case V4L2_YCBCR_ENC_709:
2032 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2034 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2035 GST_FIXME ("BT2020 with constant lumma is not defined, assuming BT2020");
2037 case V4L2_YCBCR_ENC_BT2020:
2038 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2040 case V4L2_YCBCR_ENC_SMPTE240M:
2041 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2043 case V4L2_YCBCR_ENC_DEFAULT:
2044 /* nothing, just use defaults for colorspace */
2047 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2048 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2052 /* Set identity matrix for R'G'B' formats to avoid creating
2053 * confusion. This though is cosmetic as it's now properly ignored by
2054 * the video info API and videoconvert. */
2056 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2059 case V4L2_XFER_FUNC_709:
2060 if (fmt->fmt.pix.height >= 2160)
2061 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2063 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2065 case V4L2_XFER_FUNC_SRGB:
2066 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2068 case V4L2_XFER_FUNC_ADOBERGB:
2069 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2071 case V4L2_XFER_FUNC_SMPTE240M:
2072 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2074 case V4L2_XFER_FUNC_NONE:
2075 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2077 case V4L2_XFER_FUNC_DEFAULT:
2078 /* nothing, just use defaults for colorspace */
2081 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2082 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2091 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2092 struct v4l2_format *try_fmt)
2094 int fd = v4l2object->video_fd;
2095 struct v4l2_format fmt;
2098 memcpy (&fmt, try_fmt, sizeof (fmt));
2099 r = v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2101 if (r < 0 && errno == ENOTTY) {
2102 /* The driver might not implement TRY_FMT, in which case we will try
2104 if (GST_V4L2_IS_ACTIVE (v4l2object))
2107 memcpy (&fmt, try_fmt, sizeof (fmt));
2108 r = v4l2object->ioctl (fd, VIDIOC_S_FMT, &fmt);
2110 memcpy (try_fmt, &fmt, sizeof (fmt));
2115 memcpy (try_fmt, &fmt, sizeof (fmt));
2116 GST_WARNING_OBJECT (v4l2object->element,
2117 "Unable to try format: %s", g_strerror (errno));
2123 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2124 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2126 struct v4l2_format fmt;
2127 GValue interlace_formats = { 0, };
2128 GstVideoInterlaceMode interlace_mode, prev = -1;
2130 const gchar *mode_strings[] = { "progressive",
2135 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2138 if (v4l2object->never_interlaced) {
2139 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2143 g_value_init (&interlace_formats, GST_TYPE_LIST);
2145 /* Try twice - once for NONE, once for INTERLACED. */
2146 memset (&fmt, 0, sizeof (fmt));
2147 fmt.type = v4l2object->type;
2148 fmt.fmt.pix.width = width;
2149 fmt.fmt.pix.height = height;
2150 fmt.fmt.pix.pixelformat = pixelformat;
2151 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2153 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2154 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2155 GValue interlace_enum = { 0, };
2156 g_value_init (&interlace_enum, G_TYPE_STRING);
2157 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2158 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2159 prev = interlace_mode;
2162 memset (&fmt, 0, sizeof (fmt));
2163 fmt.type = v4l2object->type;
2164 fmt.fmt.pix.width = width;
2165 fmt.fmt.pix.height = height;
2166 fmt.fmt.pix.pixelformat = pixelformat;
2167 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2169 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2170 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode) &&
2171 prev != interlace_mode) {
2172 GValue interlace_enum = { 0, };
2173 g_value_init (&interlace_enum, G_TYPE_STRING);
2174 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2175 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2178 if (gst_v4l2src_value_simplify (&interlace_formats)
2179 || gst_value_list_get_size (&interlace_formats) > 0)
2180 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2182 GST_WARNING_OBJECT (v4l2object, "Failed to determine interlace mode");
2188 gst_v4l2_object_fill_colorimetry_list (GValue * list,
2189 GstVideoColorimetry * cinfo)
2191 GValue colorimetry = G_VALUE_INIT;
2194 gboolean found = FALSE;
2196 g_value_init (&colorimetry, G_TYPE_STRING);
2197 g_value_take_string (&colorimetry, gst_video_colorimetry_to_string (cinfo));
2199 /* only insert if no duplicate */
2200 size = gst_value_list_get_size (list);
2201 for (i = 0; i < size; i++) {
2204 tmp = gst_value_list_get_value (list, i);
2205 if (gst_value_compare (&colorimetry, tmp) == GST_VALUE_EQUAL) {
2212 gst_value_list_append_and_take_value (list, &colorimetry);
2214 g_value_unset (&colorimetry);
2218 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2219 guint32 width, guint32 height, guint32 pixelformat)
2221 struct v4l2_format fmt;
2222 GValue list = G_VALUE_INIT;
2223 GstVideoColorimetry cinfo;
2224 enum v4l2_colorspace req_cspace;
2226 memset (&fmt, 0, sizeof (fmt));
2227 fmt.type = v4l2object->type;
2228 fmt.fmt.pix.width = width;
2229 fmt.fmt.pix.height = height;
2230 fmt.fmt.pix.pixelformat = pixelformat;
2232 g_value_init (&list, GST_TYPE_LIST);
2234 /* step 1: get device default colorspace and insert it first as
2235 * it should be the preferred one */
2236 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2237 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2238 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2241 /* step 2: probe all colorspace other than default
2242 * We don't probe all colorspace, range, matrix and transfer combination to
2243 * avoid ioctl flooding which could greatly increase initialization time
2244 * with low-speed devices (UVC...) */
2245 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2246 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++) {
2247 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2248 if (req_cspace == V4L2_COLORSPACE_BT878)
2251 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2252 fmt.fmt.pix_mp.colorspace = req_cspace;
2254 fmt.fmt.pix.colorspace = req_cspace;
2256 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2257 enum v4l2_colorspace colorspace;
2259 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2260 colorspace = fmt.fmt.pix_mp.colorspace;
2262 colorspace = fmt.fmt.pix.colorspace;
2264 if (colorspace == req_cspace) {
2265 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2266 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2271 if (gst_value_list_get_size (&list) > 0)
2272 gst_structure_take_value (s, "colorimetry", &list);
2274 g_value_unset (&list);
2279 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2280 static GstStructure *
2281 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2282 guint32 pixelformat,
2283 guint32 width, guint32 height, const GstStructure * template)
2285 gint fd = v4l2object->video_fd;
2286 struct v4l2_frmivalenum ival;
2289 GValue rates = { 0, };
2291 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2293 ival.pixel_format = pixelformat;
2295 ival.height = height;
2297 GST_LOG_OBJECT (v4l2object->element,
2298 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2299 GST_FOURCC_ARGS (pixelformat));
2301 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2302 * fraction to get framerate */
2303 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2304 goto enum_frameintervals_failed;
2306 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2307 GValue rate = { 0, };
2309 g_value_init (&rates, GST_TYPE_LIST);
2310 g_value_init (&rate, GST_TYPE_FRACTION);
2313 num = ival.discrete.numerator;
2314 denom = ival.discrete.denominator;
2316 if (num > G_MAXINT || denom > G_MAXINT) {
2317 /* let us hope we don't get here... */
2322 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
2325 /* swap to get the framerate */
2326 gst_value_set_fraction (&rate, denom, num);
2327 gst_value_list_append_value (&rates, &rate);
2330 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2331 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2332 GValue min = { 0, };
2333 GValue step = { 0, };
2334 GValue max = { 0, };
2335 gboolean added = FALSE;
2336 guint32 minnum, mindenom;
2337 guint32 maxnum, maxdenom;
2339 g_value_init (&rates, GST_TYPE_LIST);
2341 g_value_init (&min, GST_TYPE_FRACTION);
2342 g_value_init (&step, GST_TYPE_FRACTION);
2343 g_value_init (&max, GST_TYPE_FRACTION);
2346 minnum = ival.stepwise.min.numerator;
2347 mindenom = ival.stepwise.min.denominator;
2348 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2352 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
2354 gst_value_set_fraction (&min, minnum, mindenom);
2357 maxnum = ival.stepwise.max.numerator;
2358 maxdenom = ival.stepwise.max.denominator;
2359 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2364 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
2366 gst_value_set_fraction (&max, maxnum, maxdenom);
2369 num = ival.stepwise.step.numerator;
2370 denom = ival.stepwise.step.denominator;
2371 if (num > G_MAXINT || denom > G_MAXINT) {
2376 if (num == 0 || denom == 0) {
2377 /* in this case we have a wrong fraction or no step, set the step to max
2378 * so that we only add the min value in the loop below */
2383 /* since we only have gst_value_fraction_subtract and not add, negate the
2385 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
2387 gst_value_set_fraction (&step, -num, denom);
2389 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2390 GValue rate = { 0, };
2392 num = gst_value_get_fraction_numerator (&min);
2393 denom = gst_value_get_fraction_denominator (&min);
2394 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
2397 /* invert to get the framerate */
2398 g_value_init (&rate, GST_TYPE_FRACTION);
2399 gst_value_set_fraction (&rate, denom, num);
2400 gst_value_list_append_value (&rates, &rate);
2403 /* we're actually adding because step was negated above. This is because
2404 * there is no _add function... */
2405 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2406 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
2411 /* no range was added, leave the default range from the template */
2412 GST_WARNING_OBJECT (v4l2object->element,
2413 "no range added, leaving default");
2414 g_value_unset (&rates);
2416 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2417 guint32 maxnum, maxdenom;
2419 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2421 num = ival.stepwise.min.numerator;
2422 denom = ival.stepwise.min.denominator;
2423 if (num > G_MAXINT || denom > G_MAXINT) {
2428 maxnum = ival.stepwise.max.numerator;
2429 maxdenom = ival.stepwise.max.denominator;
2430 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2435 GST_LOG_OBJECT (v4l2object->element,
2436 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2439 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2445 s = gst_structure_copy (template);
2446 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2447 "height", G_TYPE_INT, (gint) height, NULL);
2449 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2451 if (!v4l2object->skip_try_fmt_probes) {
2452 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2454 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2457 if (G_IS_VALUE (&rates)) {
2458 gst_v4l2src_value_simplify (&rates);
2459 /* only change the framerate on the template when we have a valid probed new
2461 gst_structure_take_value (s, "framerate", &rates);
2462 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2463 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2464 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2470 enum_frameintervals_failed:
2472 GST_DEBUG_OBJECT (v4l2object->element,
2473 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2474 GST_FOURCC_ARGS (pixelformat), width, height);
2479 /* I don't see how this is actually an error, we ignore the format then */
2480 GST_WARNING_OBJECT (v4l2object->element,
2481 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2482 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2488 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2492 gst_structure_get_int (s1, "width", &w1);
2493 gst_structure_get_int (s1, "height", &h1);
2494 gst_structure_get_int (s2, "width", &w2);
2495 gst_structure_get_int (s2, "height", &h2);
2497 /* I think it's safe to assume that this won't overflow for a while */
2498 return ((w2 * h2) - (w1 * h1));
2502 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2503 guint32 format, GstCaps * caps, GstStructure * s)
2505 GstStructure *alt_s = NULL;
2507 /* Encoded stream on output buffer need to be parsed */
2508 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2509 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2512 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2513 if (format == gst_v4l2_formats[i].format &&
2514 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2515 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2516 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2522 if (v4l2object->has_alpha_component &&
2523 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2524 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2526 case V4L2_PIX_FMT_RGB32:
2527 alt_s = gst_structure_copy (s);
2528 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2530 case V4L2_PIX_FMT_BGR32:
2531 alt_s = gst_structure_copy (s);
2532 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2539 gst_caps_append_structure (caps, s);
2542 gst_caps_append_structure (caps, alt_s);
2546 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2547 guint32 pixelformat, const GstStructure * template)
2549 GstCaps *ret = gst_caps_new_empty ();
2551 gint fd = v4l2object->video_fd;
2552 struct v4l2_frmsizeenum size;
2553 GList *results = NULL;
2556 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2557 gst_caps_append_structure (ret, gst_structure_copy (template));
2561 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2563 size.pixel_format = pixelformat;
2565 GST_DEBUG_OBJECT (v4l2object->element,
2566 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2567 GST_FOURCC_ARGS (pixelformat));
2569 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2570 goto enum_framesizes_failed;
2572 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2574 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
2575 size.discrete.width, size.discrete.height);
2577 w = MIN (size.discrete.width, G_MAXINT);
2578 h = MIN (size.discrete.height, G_MAXINT);
2582 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2583 pixelformat, w, h, template);
2586 results = g_list_prepend (results, tmp);
2590 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2591 GST_DEBUG_OBJECT (v4l2object->element,
2592 "done iterating discrete frame sizes");
2593 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2594 guint32 maxw, maxh, step_w, step_h;
2596 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
2597 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2598 size.stepwise.min_width);
2599 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2600 size.stepwise.min_height);
2601 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2602 size.stepwise.max_width);
2603 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2604 size.stepwise.max_height);
2605 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
2606 size.stepwise.step_width);
2607 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
2608 size.stepwise.step_height);
2610 w = MAX (size.stepwise.min_width, 1);
2611 h = MAX (size.stepwise.min_height, 1);
2612 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2613 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2615 step_w = MAX (size.stepwise.step_width, 1);
2616 step_h = MAX (size.stepwise.step_height, 1);
2618 /* FIXME: check for sanity and that min/max are multiples of the steps */
2620 /* we only query details for the max width/height since it's likely the
2621 * most restricted if there are any resolution-dependent restrictions */
2622 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2623 pixelformat, maxw, maxh, template);
2626 GValue step_range = G_VALUE_INIT;
2628 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2629 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2630 gst_structure_set_value (tmp, "width", &step_range);
2632 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2633 gst_structure_take_value (tmp, "height", &step_range);
2635 /* no point using the results list here, since there's only one struct */
2636 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2638 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2641 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
2642 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2643 size.stepwise.min_width);
2644 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2645 size.stepwise.min_height);
2646 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2647 size.stepwise.max_width);
2648 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2649 size.stepwise.max_height);
2651 w = MAX (size.stepwise.min_width, 1);
2652 h = MAX (size.stepwise.min_height, 1);
2653 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2654 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2657 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2660 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2661 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2664 /* no point using the results list here, since there's only one struct */
2665 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2671 /* we use an intermediary list to store and then sort the results of the
2672 * probing because we can't make any assumptions about the order in which
2673 * the driver will give us the sizes, but we want the final caps to contain
2674 * the results starting with the highest resolution and having the lowest
2675 * resolution last, since order in caps matters for things like fixation. */
2676 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2677 while (results != NULL) {
2678 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
2680 results = g_list_delete_link (results, results);
2683 if (gst_caps_is_empty (ret))
2684 goto enum_framesizes_no_results;
2689 enum_framesizes_failed:
2691 /* I don't see how this is actually an error */
2692 GST_DEBUG_OBJECT (v4l2object->element,
2693 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2694 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2695 goto default_frame_sizes;
2697 enum_framesizes_no_results:
2699 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2700 * question doesn't actually support it yet */
2701 GST_DEBUG_OBJECT (v4l2object->element,
2702 "No results for pixelformat %" GST_FOURCC_FORMAT
2703 " enumerating frame sizes, trying fallback",
2704 GST_FOURCC_ARGS (pixelformat));
2705 goto default_frame_sizes;
2709 GST_WARNING_OBJECT (v4l2object->element,
2710 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2711 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2712 goto default_frame_sizes;
2715 default_frame_sizes:
2717 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2719 /* This code is for Linux < 2.6.19 */
2721 max_w = max_h = GST_V4L2_MAX_SIZE;
2722 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2724 GST_WARNING_OBJECT (v4l2object->element,
2725 "Could not probe minimum capture size for pixelformat %"
2726 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2728 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2730 GST_WARNING_OBJECT (v4l2object->element,
2731 "Could not probe maximum capture size for pixelformat %"
2732 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2735 /* Since we can't get framerate directly, try to use the current norm */
2736 if (v4l2object->tv_norm && v4l2object->norms) {
2738 GstTunerNorm *norm = NULL;
2739 GstTunerNorm *current =
2740 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2742 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2743 norm = (GstTunerNorm *) norms->data;
2744 if (!strcmp (norm->label, current->label))
2747 /* If it's possible, set framerate to that (discrete) value */
2749 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2750 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2754 tmp = gst_structure_copy (template);
2756 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2758 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2759 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2760 /* if norm can't be used, copy the template framerate */
2761 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2766 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2768 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2771 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2773 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2775 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2777 if (!v4l2object->skip_try_fmt_probes) {
2778 /* We could consider setting interlace mode from min and max. */
2779 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
2781 /* We could consider to check colorspace for min too, in case it depends on
2782 * the size. But in this case, min and max could not be enough */
2783 gst_v4l2_object_add_colorspace (v4l2object, tmp, max_w, max_h,
2787 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2793 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2794 guint32 pixelformat, gint * width, gint * height)
2796 struct v4l2_format fmt;
2797 gboolean ret = FALSE;
2798 GstVideoInterlaceMode interlace_mode;
2800 g_return_val_if_fail (width != NULL, FALSE);
2801 g_return_val_if_fail (height != NULL, FALSE);
2803 GST_LOG_OBJECT (v4l2object->element,
2804 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2805 *width, *height, GST_FOURCC_ARGS (pixelformat));
2807 memset (&fmt, 0, sizeof (struct v4l2_format));
2809 /* get size delimiters */
2810 memset (&fmt, 0, sizeof (fmt));
2811 fmt.type = v4l2object->type;
2812 fmt.fmt.pix.width = *width;
2813 fmt.fmt.pix.height = *height;
2814 fmt.fmt.pix.pixelformat = pixelformat;
2815 fmt.fmt.pix.field = V4L2_FIELD_ANY;
2817 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
2820 GST_LOG_OBJECT (v4l2object->element,
2821 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2823 *width = fmt.fmt.pix.width;
2824 *height = fmt.fmt.pix.height;
2826 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2827 GST_WARNING_OBJECT (v4l2object->element,
2828 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
2829 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
2837 GST_WARNING_OBJECT (v4l2object->element,
2838 "Unable to try format: %s", g_strerror (errno));
2845 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2849 GST_DEBUG_OBJECT (v4l2object->element, "initializing the %s system",
2850 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
2852 GST_V4L2_CHECK_OPEN (v4l2object);
2853 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2855 /* find transport */
2856 mode = v4l2object->req_mode;
2858 if (v4l2object->device_caps & V4L2_CAP_READWRITE) {
2859 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2860 mode = GST_V4L2_IO_RW;
2861 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2862 goto method_not_supported;
2864 if (v4l2object->device_caps & V4L2_CAP_STREAMING) {
2865 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2866 mode = GST_V4L2_IO_MMAP;
2867 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2868 goto method_not_supported;
2870 /* if still no transport selected, error out */
2871 if (mode == GST_V4L2_IO_AUTO)
2872 goto no_supported_capture_method;
2874 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2875 v4l2object->mode = mode;
2877 /* If min_buffers is not set, the driver either does not support the control or
2878 it has not been asked yet via propose_allocation/decide_allocation. */
2879 if (!v4l2object->min_buffers)
2880 gst_v4l2_get_driver_min_buffers (v4l2object);
2882 /* Map the buffers */
2883 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2885 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2886 goto buffer_pool_new_failed;
2888 GST_V4L2_SET_ACTIVE (v4l2object);
2893 buffer_pool_new_failed:
2895 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2896 (_("Could not map buffers from device '%s'"),
2897 v4l2object->videodev),
2898 ("Failed to create buffer pool: %s", g_strerror (errno)));
2901 method_not_supported:
2903 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2904 (_("The driver of device '%s' does not support the IO method %d"),
2905 v4l2object->videodev, mode), (NULL));
2908 no_supported_capture_method:
2910 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2911 (_("The driver of device '%s' does not support any known IO "
2912 "method."), v4l2object->videodev), (NULL));
2918 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
2919 gint plane, gint stride)
2921 const GstVideoFormatInfo *finfo = info->finfo;
2923 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2924 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
2927 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
2928 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2929 tile_height = 1 << hs;
2931 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
2932 info->height + align->padding_top + align->padding_bottom);
2933 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
2935 x_tiles = stride >> ws;
2936 y_tiles = padded_height >> hs;
2937 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
2939 info->stride[plane] = stride;
2944 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
2945 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
2947 const GstVideoFormatInfo *finfo = info->finfo;
2948 gint i, estride, padded_height;
2951 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
2953 padded_height = info->height + align->padding_top + align->padding_bottom;
2955 for (i = 0; i < finfo->n_planes; i++) {
2956 estride = gst_v4l2_object_extrapolate_stride (finfo, i, stride);
2958 gst_v4l2_object_set_stride (info, align, i, estride);
2960 info->offset[i] = offs;
2962 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
2964 GST_DEBUG_OBJECT (v4l2object->element,
2965 "Extrapolated for plane %d with base stride %d: "
2966 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
2970 /* Update the image size according the amount of data we are going to
2971 * read/write. This workaround bugs in driver where the sizeimage provided
2972 * by TRY/S_FMT represent the buffer length (maximum size) rather then the expected
2973 * bytesused (buffer size). */
2974 if (offs < info->size)
2979 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
2980 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
2981 GstVideoInfo * info, GstVideoAlignment * align)
2983 const GstVideoFormatInfo *finfo = info->finfo;
2984 gboolean standard_stride = TRUE;
2985 gint stride, padded_width, padded_height, i;
2987 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
2988 v4l2object->n_v4l2_planes = 1;
2989 info->size = format->fmt.pix.sizeimage;
2993 /* adjust right padding */
2994 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2995 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
2997 stride = format->fmt.pix.bytesperline;
2999 padded_width = stride / GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
3001 if (padded_width < format->fmt.pix.width)
3002 GST_WARNING_OBJECT (v4l2object->element,
3003 "Driver bug detected, stride is too small for the width");
3005 align->padding_right = padded_width - info->width - align->padding_left;
3007 /* adjust bottom padding */
3008 padded_height = format->fmt.pix.height;
3010 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3011 guint hs, tile_height;
3013 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
3014 tile_height = 1 << hs;
3016 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3019 align->padding_bottom = padded_height - info->height - align->padding_top;
3021 /* setup the strides and offset */
3022 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
3023 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
3025 /* figure out the frame layout */
3026 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
3028 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
3029 stride = pix_mp->plane_fmt[i].bytesperline;
3031 if (info->stride[i] != stride)
3032 standard_stride = FALSE;
3034 gst_v4l2_object_set_stride (info, align, i, stride);
3035 info->offset[i] = info->size;
3036 info->size += pix_mp->plane_fmt[i].sizeimage;
3039 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3040 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
3041 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3042 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3045 /* only one plane in non-MPLANE mode */
3046 v4l2object->n_v4l2_planes = 1;
3047 info->size = format->fmt.pix.sizeimage;
3048 stride = format->fmt.pix.bytesperline;
3050 if (info->stride[0] != stride)
3051 standard_stride = FALSE;
3053 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3056 /* adjust the offset to take into account left and top */
3057 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3058 if ((align->padding_left + align->padding_top) > 0)
3059 GST_WARNING_OBJECT (v4l2object->element,
3060 "Left and top padding is not permitted for tiled formats");
3062 for (i = 0; i < finfo->n_planes; i++) {
3065 /* FIXME we assume plane as component as this is true for all supported
3066 * format we support. */
3068 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left);
3069 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top);
3071 info->offset[i] += (vedge * info->stride[i]) +
3072 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i));
3077 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %" G_GSIZE_FORMAT,
3080 /* to avoid copies we need video meta if there is padding */
3081 v4l2object->need_video_meta =
3082 ((align->padding_top + align->padding_left + align->padding_right +
3083 align->padding_bottom) != 0);
3085 /* ... or if stride is non "standard" */
3086 if (!standard_stride)
3087 v4l2object->need_video_meta = TRUE;
3089 /* ... or also video meta if we use multiple, non-contiguous, planes */
3090 if (v4l2object->n_v4l2_planes > 1)
3091 v4l2object->need_video_meta = TRUE;
3093 v4l2object->info = *info;
3094 v4l2object->align = *align;
3095 v4l2object->format = *format;
3096 v4l2object->fmtdesc = fmtdesc;
3098 /* if we have a framerate pre-calculate duration */
3099 if (info->fps_n > 0 && info->fps_d > 0) {
3100 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
3103 v4l2object->duration = GST_CLOCK_TIME_NONE;
3108 gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
3109 gint plane, gint stride)
3113 switch (finfo->format) {
3114 case GST_VIDEO_FORMAT_NV12:
3115 case GST_VIDEO_FORMAT_NV12_64Z32:
3116 case GST_VIDEO_FORMAT_NV21:
3117 case GST_VIDEO_FORMAT_NV16:
3118 case GST_VIDEO_FORMAT_NV61:
3119 case GST_VIDEO_FORMAT_NV24:
3120 estride = (plane == 0 ? 1 : 2) *
3121 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3124 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3132 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3133 gboolean try_only, GstV4l2Error * error)
3135 gint fd = v4l2object->video_fd;
3136 struct v4l2_format format;
3137 struct v4l2_streamparm streamparm;
3138 enum v4l2_field field;
3139 guint32 pixelformat;
3140 struct v4l2_fmtdesc *fmtdesc;
3142 GstVideoAlignment align;
3143 gint width, height, fps_n, fps_d;
3147 enum v4l2_colorspace colorspace = 0;
3148 enum v4l2_quantization range = 0;
3149 enum v4l2_ycbcr_encoding matrix = 0;
3150 enum v4l2_xfer_func transfer = 0;
3153 g_return_val_if_fail (!v4l2object->skip_try_fmt_probes ||
3154 gst_caps_is_writable (caps), FALSE);
3156 GST_V4L2_CHECK_OPEN (v4l2object);
3158 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3160 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3162 gst_video_info_init (&info);
3163 gst_video_alignment_reset (&align);
3165 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3168 pixelformat = fmtdesc->pixelformat;
3169 width = GST_VIDEO_INFO_WIDTH (&info);
3170 height = GST_VIDEO_INFO_HEIGHT (&info);
3171 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3172 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3174 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3175 * or if contiguous is prefered */
3176 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3177 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3180 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
3181 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
3182 /* ideally we would differentiate between types of interlaced video
3183 * but there is not sufficient information in the caps..
3185 field = V4L2_FIELD_INTERLACED;
3187 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
3188 field = V4L2_FIELD_NONE;
3191 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3192 /* We first pick th main colorspace from the primaries */
3193 switch (info.colorimetry.primaries) {
3194 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3195 /* There is two colorspaces using these primaries, use the range to
3197 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3198 colorspace = V4L2_COLORSPACE_REC709;
3200 colorspace = V4L2_COLORSPACE_SRGB;
3202 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3203 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3205 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3206 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3208 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3209 colorspace = V4L2_COLORSPACE_SMPTE170M;
3211 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3212 colorspace = V4L2_COLORSPACE_SMPTE240M;
3215 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3216 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3217 /* We don't know, we will guess */
3221 GST_WARNING_OBJECT (v4l2object->element,
3222 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3226 switch (info.colorimetry.range) {
3227 case GST_VIDEO_COLOR_RANGE_0_255:
3228 range = V4L2_QUANTIZATION_FULL_RANGE;
3230 case GST_VIDEO_COLOR_RANGE_16_235:
3231 range = V4L2_QUANTIZATION_LIM_RANGE;
3233 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3234 /* We let the driver pick a default one */
3237 GST_WARNING_OBJECT (v4l2object->element,
3238 "Unknown colorimetry range %d", info.colorimetry.range);
3242 switch (info.colorimetry.matrix) {
3243 case GST_VIDEO_COLOR_MATRIX_RGB:
3244 /* Unspecified, leave to default */
3246 /* FCC is about the same as BT601 with less digit */
3247 case GST_VIDEO_COLOR_MATRIX_FCC:
3248 case GST_VIDEO_COLOR_MATRIX_BT601:
3249 matrix = V4L2_YCBCR_ENC_601;
3251 case GST_VIDEO_COLOR_MATRIX_BT709:
3252 matrix = V4L2_YCBCR_ENC_709;
3254 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3255 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3257 case GST_VIDEO_COLOR_MATRIX_BT2020:
3258 matrix = V4L2_YCBCR_ENC_BT2020;
3260 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3261 /* We let the driver pick a default one */
3264 GST_WARNING_OBJECT (v4l2object->element,
3265 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3269 switch (info.colorimetry.transfer) {
3270 case GST_VIDEO_TRANSFER_GAMMA18:
3271 case GST_VIDEO_TRANSFER_GAMMA20:
3272 case GST_VIDEO_TRANSFER_GAMMA22:
3273 case GST_VIDEO_TRANSFER_GAMMA28:
3274 GST_WARNING_OBJECT (v4l2object->element,
3275 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3277 case GST_VIDEO_TRANSFER_GAMMA10:
3278 transfer = V4L2_XFER_FUNC_NONE;
3280 case GST_VIDEO_TRANSFER_BT709:
3281 transfer = V4L2_XFER_FUNC_709;
3283 case GST_VIDEO_TRANSFER_SMPTE240M:
3284 transfer = V4L2_XFER_FUNC_SMPTE240M;
3286 case GST_VIDEO_TRANSFER_SRGB:
3287 transfer = V4L2_XFER_FUNC_SRGB;
3289 case GST_VIDEO_TRANSFER_LOG100:
3290 case GST_VIDEO_TRANSFER_LOG316:
3291 GST_WARNING_OBJECT (v4l2object->element,
3292 "LOG 100, 316 transfer functions not supported");
3293 /* FIXME No known sensible default, maybe AdobeRGB ? */
3295 case GST_VIDEO_TRANSFER_UNKNOWN:
3296 /* We let the driver pick a default one */
3299 GST_WARNING_OBJECT (v4l2object->element,
3300 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
3304 if (colorspace == 0) {
3305 /* Try to guess colorspace according to pixelformat and size */
3306 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3307 /* SD streams likely use SMPTE170M and HD streams REC709 */
3308 if (width <= 720 && height <= 576)
3309 colorspace = V4L2_COLORSPACE_SMPTE170M;
3311 colorspace = V4L2_COLORSPACE_REC709;
3312 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3313 colorspace = V4L2_COLORSPACE_SRGB;
3314 transfer = V4L2_XFER_FUNC_NONE;
3319 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
3320 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3321 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3323 memset (&format, 0x00, sizeof (struct v4l2_format));
3324 format.type = v4l2object->type;
3327 format.type = v4l2object->type;
3328 format.fmt.pix_mp.pixelformat = pixelformat;
3329 format.fmt.pix_mp.width = width;
3330 format.fmt.pix_mp.height = height;
3331 format.fmt.pix_mp.field = field;
3332 format.fmt.pix_mp.num_planes = n_v4l_planes;
3334 /* try to ask our prefered stride but it's not a failure if not
3336 for (i = 0; i < n_v4l_planes; i++) {
3337 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3339 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3340 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3341 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3343 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3346 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3347 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3349 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3351 format.type = v4l2object->type;
3352 format.fmt.pix.width = width;
3353 format.fmt.pix.height = height;
3354 format.fmt.pix.pixelformat = pixelformat;
3355 format.fmt.pix.field = field;
3357 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3358 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3359 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3361 /* try to ask our prefered stride */
3362 format.fmt.pix.bytesperline = stride;
3364 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3365 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3368 GST_DEBUG_OBJECT (v4l2object->element, "Desired format is %dx%d, format "
3369 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3370 format.fmt.pix_mp.height,
3371 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3372 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3374 #ifndef GST_DISABLE_GST_DEBUG
3376 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3377 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3378 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3380 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3381 format.fmt.pix.bytesperline);
3385 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3387 format.fmt.pix_mp.colorspace = colorspace;
3388 format.fmt.pix_mp.quantization = range;
3389 format.fmt.pix_mp.ycbcr_enc = matrix;
3390 format.fmt.pix_mp.xfer_func = transfer;
3392 format.fmt.pix.colorspace = colorspace;
3393 format.fmt.pix.quantization = range;
3394 format.fmt.pix.ycbcr_enc = matrix;
3395 format.fmt.pix.xfer_func = transfer;
3398 GST_DEBUG_OBJECT (v4l2object->element, "Desired colorspace is %d:%d:%d:%d",
3399 colorspace, range, matrix, transfer);
3403 if (v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3404 goto try_fmt_failed;
3406 if (v4l2object->ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3407 goto set_fmt_failed;
3410 GST_DEBUG_OBJECT (v4l2object->element, "Got format of %dx%d, format "
3411 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d",
3412 format.fmt.pix.width, format.fmt.pix_mp.height,
3413 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3414 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3415 is_mplane ? format.fmt.pix_mp.colorspace : format.fmt.pix.colorspace);
3417 #ifndef GST_DISABLE_GST_DEBUG
3419 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3420 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3421 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3422 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3424 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3425 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3429 if (format.fmt.pix.pixelformat != pixelformat)
3430 goto invalid_pixelformat;
3432 /* Only negotiate size with raw data.
3433 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3434 * in ASF mode for example, there is also not reason for a driver to
3435 * change the size. */
3436 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3437 /* We can crop larger images */
3438 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3439 goto invalid_dimensions;
3441 /* Note, this will be adjusted if upstream has non-centered cropping. */
3442 align.padding_top = 0;
3443 align.padding_bottom = format.fmt.pix.height - height;
3444 align.padding_left = 0;
3445 align.padding_right = format.fmt.pix.width - width;
3448 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3449 goto invalid_planes;
3451 if ((is_mplane && format.fmt.pix_mp.field != field)
3452 || format.fmt.pix.field != field)
3455 gst_v4l2_object_get_colorspace (&format, &info.colorimetry);
3457 s = gst_caps_get_structure (caps, 0);
3458 if (gst_structure_has_field (s, "colorimetry")) {
3459 GstVideoColorimetry ci;
3460 if (!gst_video_colorimetry_from_string (&ci,
3461 gst_structure_get_string (s, "colorimetry"))
3462 || !gst_video_colorimetry_is_equal (&ci, &info.colorimetry))
3463 goto invalid_colorimetry;
3466 /* In case we have skipped the try_fmt probes, we'll need to set the
3467 * colorimetry and interlace-mode back into the caps. */
3468 if (v4l2object->skip_try_fmt_probes) {
3469 if (!gst_structure_has_field (s, "colorimetry")) {
3470 gchar *str = gst_video_colorimetry_to_string (&info.colorimetry);
3471 gst_structure_set (s, "colorimetry", G_TYPE_STRING, str, NULL);
3475 if (!gst_structure_has_field (s, "interlace-mode"))
3476 gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
3477 gst_video_interlace_mode_to_string (info.interlace_mode), NULL);
3480 if (try_only) /* good enough for trying only */
3483 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
3484 struct v4l2_control ctl = { 0, };
3485 ctl.id = V4L2_CID_ALPHA_COMPONENT;
3488 if (v4l2object->ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
3489 GST_WARNING_OBJECT (v4l2object->element,
3490 "Failed to set alpha component value");
3493 /* Is there a reason we require the caller to always specify a framerate? */
3494 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
3497 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
3498 streamparm.type = v4l2object->type;
3500 if (v4l2object->ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
3501 goto get_parm_failed;
3503 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
3504 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3505 GST_VIDEO_INFO_FPS_N (&info) =
3506 streamparm.parm.capture.timeperframe.denominator;
3507 GST_VIDEO_INFO_FPS_D (&info) =
3508 streamparm.parm.capture.timeperframe.numerator;
3510 GST_DEBUG_OBJECT (v4l2object->element, "Got capture framerate: %u/%u",
3511 streamparm.parm.capture.timeperframe.denominator,
3512 streamparm.parm.capture.timeperframe.numerator);
3514 /* We used to skip frame rate setup if the camera was already setup
3515 * with the requested frame rate. This breaks some cameras though,
3516 * causing them to not output data (several models of Thinkpad cameras
3517 * have this problem at least).
3518 * So, don't skip. */
3519 GST_LOG_OBJECT (v4l2object->element, "Setting capture framerate to %u/%u",
3521 /* We want to change the frame rate, so check whether we can. Some cheap USB
3522 * cameras don't have the capability */
3523 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3524 GST_DEBUG_OBJECT (v4l2object->element,
3525 "Not setting capture framerate (not supported)");
3529 /* Note: V4L2 wants the frame interval, we have the frame rate */
3530 streamparm.parm.capture.timeperframe.numerator = fps_d;
3531 streamparm.parm.capture.timeperframe.denominator = fps_n;
3533 /* some cheap USB cam's won't accept any change */
3534 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3535 goto set_parm_failed;
3537 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
3538 streamparm.parm.capture.timeperframe.denominator > 0) {
3539 /* get new values */
3540 fps_d = streamparm.parm.capture.timeperframe.numerator;
3541 fps_n = streamparm.parm.capture.timeperframe.denominator;
3543 GST_INFO_OBJECT (v4l2object->element, "Set capture framerate to %u/%u",
3546 /* fix v4l2 capture driver to provide framerate values */
3547 GST_WARNING_OBJECT (v4l2object->element,
3548 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
3551 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3552 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3553 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
3554 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
3555 GST_VIDEO_INFO_FPS_N (&info) =
3556 streamparm.parm.output.timeperframe.denominator;
3557 GST_VIDEO_INFO_FPS_D (&info) =
3558 streamparm.parm.output.timeperframe.numerator;
3560 GST_DEBUG_OBJECT (v4l2object->element, "Got output framerate: %u/%u",
3561 streamparm.parm.output.timeperframe.denominator,
3562 streamparm.parm.output.timeperframe.numerator);
3564 GST_LOG_OBJECT (v4l2object->element, "Setting output framerate to %u/%u",
3566 if ((streamparm.parm.output.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3567 GST_DEBUG_OBJECT (v4l2object->element,
3568 "Not setting output framerate (not supported)");
3572 /* Note: V4L2 wants the frame interval, we have the frame rate */
3573 streamparm.parm.output.timeperframe.numerator = fps_d;
3574 streamparm.parm.output.timeperframe.denominator = fps_n;
3576 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3577 goto set_parm_failed;
3579 if (streamparm.parm.output.timeperframe.numerator > 0 &&
3580 streamparm.parm.output.timeperframe.denominator > 0) {
3581 /* get new values */
3582 fps_d = streamparm.parm.output.timeperframe.numerator;
3583 fps_n = streamparm.parm.output.timeperframe.denominator;
3585 GST_INFO_OBJECT (v4l2object->element, "Set output framerate to %u/%u",
3588 /* fix v4l2 output driver to provide framerate values */
3589 GST_WARNING_OBJECT (v4l2object->element,
3590 "Reuse caps framerate %u/%u - fix v4l2 output driver", fps_n, fps_d);
3593 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3594 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3598 /* add boolean return, so we can fail on drivers bugs */
3599 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
3601 /* now configure the pool */
3602 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
3610 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
3616 if (errno == EINVAL) {
3617 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3618 (_("Device '%s' has no supported format"), v4l2object->videodev),
3619 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3620 GST_FOURCC_ARGS (pixelformat), width, height,
3621 g_strerror (errno)));
3623 GST_V4L2_ERROR (error, RESOURCE, FAILED,
3624 (_("Device '%s' failed during initialization"),
3625 v4l2object->videodev),
3626 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3627 GST_FOURCC_ARGS (pixelformat), width, height,
3628 g_strerror (errno)));
3634 if (errno == EBUSY) {
3635 GST_V4L2_ERROR (error, RESOURCE, BUSY,
3636 (_("Device '%s' is busy"), v4l2object->videodev),
3637 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3638 GST_FOURCC_ARGS (pixelformat), width, height,
3639 g_strerror (errno)));
3640 } else if (errno == EINVAL) {
3641 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3642 (_("Device '%s' has no supported format"), v4l2object->videodev),
3643 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3644 GST_FOURCC_ARGS (pixelformat), width, height,
3645 g_strerror (errno)));
3647 GST_V4L2_ERROR (error, RESOURCE, FAILED,
3648 (_("Device '%s' failed during initialization"),
3649 v4l2object->videodev),
3650 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3651 GST_FOURCC_ARGS (pixelformat), width, height,
3652 g_strerror (errno)));
3658 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3659 (_("Device '%s' cannot capture at %dx%d"),
3660 v4l2object->videodev, width, height),
3661 ("Tried to capture at %dx%d, but device returned size %dx%d",
3662 width, height, format.fmt.pix.width, format.fmt.pix.height));
3665 invalid_pixelformat:
3667 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3668 (_("Device '%s' cannot capture in the specified format"),
3669 v4l2object->videodev),
3670 ("Tried to capture in %" GST_FOURCC_FORMAT
3671 ", but device returned format" " %" GST_FOURCC_FORMAT,
3672 GST_FOURCC_ARGS (pixelformat),
3673 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
3678 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3679 (_("Device '%s' does support non-contiguous planes"),
3680 v4l2object->videodev),
3681 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
3686 enum v4l2_field wanted_field;
3689 wanted_field = format.fmt.pix_mp.field;
3691 wanted_field = format.fmt.pix.field;
3693 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3694 (_("Device '%s' does not support %s interlacing"),
3695 v4l2object->videodev,
3696 field == V4L2_FIELD_NONE ? "progressive" : "interleaved"),
3697 ("Device wants %s interlacing",
3698 wanted_field == V4L2_FIELD_NONE ? "progressive" : "interleaved"));
3701 invalid_colorimetry:
3703 gchar *wanted_colorimetry;
3705 wanted_colorimetry = gst_video_colorimetry_to_string (&info.colorimetry);
3707 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3708 (_("Device '%s' does not support %s colorimetry"),
3709 v4l2object->videodev, gst_structure_get_string (s, "colorimetry")),
3710 ("Device wants %s colorimetry", wanted_colorimetry));
3712 g_free (wanted_colorimetry);
3717 /* it's possible that this call is not supported */
3718 if (errno != EINVAL && errno != ENOTTY) {
3719 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3720 (_("Could not get parameters on device '%s'"),
3721 v4l2object->videodev), GST_ERROR_SYSTEM);
3727 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3728 (_("Video device did not accept new frame rate setting.")),
3734 /* setup_pool already send the error */
3740 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps,
3741 GstV4l2Error * error)
3743 GST_DEBUG_OBJECT (v4l2object->element, "Setting format to %" GST_PTR_FORMAT,
3745 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE, error);
3749 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps,
3750 GstV4l2Error * error)
3752 GST_DEBUG_OBJECT (v4l2object->element, "Trying format %" GST_PTR_FORMAT,
3754 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE, error);
3758 * gst_v4l2_object_acquire_format:
3759 * @v4l2object the object
3760 * @info a GstVideoInfo to be filled
3762 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
3763 * the output format is choosen by the HW.
3765 * Returns: %TRUE on success, %FALSE on failure.
3768 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
3770 struct v4l2_fmtdesc *fmtdesc;
3771 struct v4l2_format fmt;
3772 struct v4l2_crop crop;
3773 struct v4l2_selection sel;
3774 struct v4l2_rect *r = NULL;
3775 GstVideoFormat format;
3776 guint width, height;
3777 GstVideoAlignment align;
3779 gst_video_info_init (info);
3780 gst_video_alignment_reset (&align);
3782 memset (&fmt, 0x00, sizeof (struct v4l2_format));
3783 fmt.type = v4l2object->type;
3784 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
3785 goto get_fmt_failed;
3787 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
3788 fmt.fmt.pix.pixelformat);
3789 if (fmtdesc == NULL)
3790 goto unsupported_format;
3792 /* No need to care about mplane, the four first params are the same */
3793 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
3795 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
3796 if (format == GST_VIDEO_FORMAT_UNKNOWN)
3797 goto unsupported_format;
3799 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
3800 goto invalid_dimensions;
3802 width = fmt.fmt.pix.width;
3803 height = fmt.fmt.pix.height;
3805 /* Use the default compose rectangle */
3806 memset (&sel, 0, sizeof (struct v4l2_selection));
3807 sel.type = v4l2object->type;
3808 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
3809 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0) {
3812 /* For ancient kernels, fall back to G_CROP */
3813 memset (&crop, 0, sizeof (struct v4l2_crop));
3814 crop.type = v4l2object->type;
3815 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
3819 align.padding_left = r->left;
3820 align.padding_top = r->top;
3821 align.padding_right = width - r->width - r->left;
3822 align.padding_bottom = height - r->height - r->top;
3827 gst_video_info_set_format (info, format, width, height);
3829 switch (fmt.fmt.pix.field) {
3830 case V4L2_FIELD_ANY:
3831 case V4L2_FIELD_NONE:
3832 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
3834 case V4L2_FIELD_INTERLACED:
3835 case V4L2_FIELD_INTERLACED_TB:
3836 case V4L2_FIELD_INTERLACED_BT:
3837 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
3840 goto unsupported_field;
3843 gst_v4l2_object_get_colorspace (&fmt, &info->colorimetry);
3845 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
3847 /* Shall we setup the pool ? */
3853 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3854 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
3859 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3860 (_("Video device returned invalid dimensions.")),
3861 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
3862 fmt.fmt.pix.height));
3867 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3868 (_("Video device uses an unsupported interlacing method.")),
3869 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
3874 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3875 (_("Video device uses an unsupported pixel format.")),
3876 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
3877 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
3883 gst_v4l2_object_set_crop (GstV4l2Object * obj)
3885 struct v4l2_crop crop = { 0 };
3887 crop.type = obj->type;
3888 crop.c.left = obj->align.padding_left;
3889 crop.c.top = obj->align.padding_top;
3890 crop.c.width = obj->info.width;
3891 crop.c.height = obj->info.height;
3893 if (obj->align.padding_left + obj->align.padding_top +
3894 obj->align.padding_right + obj->align.padding_bottom == 0) {
3895 GST_DEBUG_OBJECT (obj->element, "no cropping needed");
3899 GST_DEBUG_OBJECT (obj->element,
3900 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3901 crop.c.width, crop.c.height);
3903 if (obj->ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
3904 GST_WARNING_OBJECT (obj->element, "VIDIOC_S_CROP failed");
3908 if (obj->ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
3909 GST_WARNING_OBJECT (obj->element, "VIDIOC_G_CROP failed");
3913 GST_DEBUG_OBJECT (obj->element,
3914 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3915 crop.c.width, crop.c.height);
3921 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
3923 GstStructure *config;
3927 if (!v4l2object->pool)
3930 config = gst_buffer_pool_get_config (v4l2object->pool);
3931 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
3933 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
3935 gst_structure_free (config);
3941 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
3943 gboolean ret = TRUE;
3945 GST_LOG_OBJECT (v4l2object->element, "start flushing");
3947 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3948 gst_buffer_pool_set_flushing (v4l2object->pool, TRUE);
3954 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
3956 gboolean ret = TRUE;
3958 GST_LOG_OBJECT (v4l2object->element, "stop flushing");
3960 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3961 gst_buffer_pool_set_flushing (v4l2object->pool, FALSE);
3967 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
3969 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
3971 if (!GST_V4L2_IS_OPEN (v4l2object))
3973 if (!GST_V4L2_IS_ACTIVE (v4l2object))
3976 if (v4l2object->pool) {
3977 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
3978 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
3979 gst_object_unref (v4l2object->pool);
3980 v4l2object->pool = NULL;
3983 GST_V4L2_SET_INACTIVE (v4l2object);
3990 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3996 formats = gst_v4l2_object_get_format_list (v4l2object);
3998 ret = gst_caps_new_empty ();
4000 if (v4l2object->keep_aspect && !v4l2object->par) {
4001 struct v4l2_cropcap cropcap;
4003 memset (&cropcap, 0, sizeof (cropcap));
4005 cropcap.type = v4l2object->type;
4006 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0) {
4007 if (errno != ENOTTY)
4008 GST_WARNING_OBJECT (v4l2object->element,
4009 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
4010 g_strerror (errno));
4012 v4l2object->par = g_new0 (GValue, 1);
4013 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
4014 gst_value_set_fraction (v4l2object->par, cropcap.pixelaspect.numerator,
4015 cropcap.pixelaspect.denominator);
4019 for (walk = formats; walk; walk = walk->next) {
4020 struct v4l2_fmtdesc *format;
4021 GstStructure *template;
4024 format = (struct v4l2_fmtdesc *) walk->data;
4026 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
4029 GST_DEBUG_OBJECT (v4l2object->element,
4030 "unknown format %" GST_FOURCC_FORMAT,
4031 GST_FOURCC_ARGS (format->pixelformat));
4035 /* If we have a filter, check if we need to probe this format or not */
4037 GstCaps *format_caps = gst_caps_new_empty ();
4039 gst_caps_append_structure (format_caps, gst_structure_copy (template));
4041 if (!gst_caps_can_intersect (format_caps, filter)) {
4042 gst_caps_unref (format_caps);
4043 gst_structure_free (template);
4047 gst_caps_unref (format_caps);
4050 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
4051 format->pixelformat, template);
4053 gst_caps_append (ret, tmp);
4055 gst_structure_free (template);
4062 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
4063 gst_caps_unref (tmp);
4066 GST_INFO_OBJECT (v4l2object->element, "probed caps: %" GST_PTR_FORMAT, ret);
4072 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4076 if (v4l2object->probed_caps == NULL)
4077 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
4080 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
4081 GST_CAPS_INTERSECT_FIRST);
4083 ret = gst_caps_ref (v4l2object->probed_caps);
4090 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
4093 GstBufferPool *pool = NULL, *other_pool = NULL;
4094 GstStructure *config;
4095 guint size, min, max, own_min = 0;
4097 gboolean has_video_meta;
4098 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
4099 GstAllocator *allocator = NULL;
4100 GstAllocationParams params = { 0 };
4102 GST_DEBUG_OBJECT (obj->element, "decide allocation");
4104 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4105 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
4107 gst_query_parse_allocation (query, &caps, NULL);
4109 if (obj->pool == NULL) {
4110 if (!gst_v4l2_object_setup_pool (obj, caps))
4114 if (gst_query_get_n_allocation_params (query) > 0)
4115 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
4117 if (gst_query_get_n_allocation_pools (query) > 0) {
4118 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
4127 GST_DEBUG_OBJECT (obj->element, "allocation: size:%u min:%u max:%u pool:%"
4128 GST_PTR_FORMAT, size, min, max, pool);
4131 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4133 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
4135 gst_v4l2_get_driver_min_buffers (obj);
4136 /* We can't share our own pool, if it exceed V4L2 capacity */
4137 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
4138 can_share_own_pool = FALSE;
4141 switch (obj->mode) {
4142 case GST_V4L2_IO_RW:
4144 /* in READ/WRITE mode, prefer a downstream pool because our own pool
4145 * doesn't help much, we have to write to it as well */
4146 GST_DEBUG_OBJECT (obj->element,
4147 "read/write mode: using downstream pool");
4148 /* use the bigest size, when we use our own pool we can't really do any
4149 * other size than what the hardware gives us but for downstream pools
4151 size = MAX (size, obj->info.size);
4152 } else if (can_share_own_pool) {
4153 /* no downstream pool, use our own then */
4154 GST_DEBUG_OBJECT (obj->element,
4155 "read/write mode: no downstream pool, using our own");
4156 pool = gst_object_ref (obj->pool);
4157 size = obj->info.size;
4158 pushing_from_our_pool = TRUE;
4162 case GST_V4L2_IO_USERPTR:
4163 case GST_V4L2_IO_DMABUF_IMPORT:
4164 /* in importing mode, prefer our own pool, and pass the other pool to
4165 * our own, so it can serve itself */
4167 goto no_downstream_pool;
4168 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj->pool),
4171 gst_object_unref (pool);
4172 pool = gst_object_ref (obj->pool);
4173 size = obj->info.size;
4176 case GST_V4L2_IO_MMAP:
4177 case GST_V4L2_IO_DMABUF:
4178 /* in streaming mode, prefer our own pool */
4179 /* Check if we can use it ... */
4180 if (can_share_own_pool) {
4182 gst_object_unref (pool);
4183 pool = gst_object_ref (obj->pool);
4184 size = obj->info.size;
4185 GST_DEBUG_OBJECT (obj->element,
4186 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
4187 pushing_from_our_pool = TRUE;
4189 GST_DEBUG_OBJECT (obj->element,
4190 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
4193 GST_DEBUG_OBJECT (obj->element,
4194 "streaming mode: no usable pool, copying to generic pool");
4195 size = MAX (size, obj->info.size);
4198 case GST_V4L2_IO_AUTO:
4200 GST_WARNING_OBJECT (obj->element, "unhandled mode");
4207 /* If pushing from our own pool, configure it with queried minimum,
4208 * otherwise use the minimum required */
4209 if (pushing_from_our_pool) {
4210 /* When pushing from our own pool, we need what downstream one, to be able
4211 * to fill the pipeline, the minimum required to decoder according to the
4212 * driver and 2 more, so we don't endup up with everything downstream or
4213 * held by the decoder. We account 2 buffers for v4l2 so when one is being
4214 * pushed downstream the other one can already be queued for the next
4216 own_min = min + obj->min_buffers + 2;
4218 /* If no allocation parameters where provided, allow for a little more
4219 * buffers and enable copy threshold */
4222 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4225 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4230 /* In this case we'll have to configure two buffer pool. For our buffer
4231 * pool, we'll need what the driver one, and one more, so we can dequeu */
4232 own_min = obj->min_buffers + 1;
4233 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS);
4235 /* for the downstream pool, we keep what downstream wants, though ensure
4236 * at least a minimum if downstream didn't suggest anything (we are
4237 * expecting the base class to create a default one for the context) */
4238 min = MAX (min, GST_V4L2_MIN_BUFFERS);
4240 /* To import we need the other pool to hold at least own_min */
4241 if (obj->pool == pool)
4245 /* Request a bigger max, if one was suggested but it's too small */
4247 max = MAX (min, max);
4249 /* First step, configure our own pool */
4250 config = gst_buffer_pool_get_config (obj->pool);
4252 if (obj->need_video_meta || has_video_meta) {
4253 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
4254 gst_buffer_pool_config_add_option (config,
4255 GST_BUFFER_POOL_OPTION_VIDEO_META);
4258 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4259 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
4261 GST_DEBUG_OBJECT (obj->element, "setting own pool config to %"
4262 GST_PTR_FORMAT, config);
4264 /* Our pool often need to adjust the value */
4265 if (!gst_buffer_pool_set_config (obj->pool, config)) {
4266 config = gst_buffer_pool_get_config (obj->pool);
4268 GST_DEBUG_OBJECT (obj->element, "own pool config changed to %"
4269 GST_PTR_FORMAT, config);
4271 /* our pool will adjust the maximum buffer, which we are fine with */
4272 if (!gst_buffer_pool_set_config (obj->pool, config))
4276 /* Now configure the other pool if different */
4277 if (obj->pool != pool)
4281 config = gst_buffer_pool_get_config (other_pool);
4282 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4283 gst_buffer_pool_config_set_params (config, caps, size, min, max);
4285 GST_DEBUG_OBJECT (obj->element, "setting other pool config to %"
4286 GST_PTR_FORMAT, config);
4288 /* if downstream supports video metadata, add this to the pool config */
4289 if (has_video_meta) {
4290 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
4291 gst_buffer_pool_config_add_option (config,
4292 GST_BUFFER_POOL_OPTION_VIDEO_META);
4295 if (!gst_buffer_pool_set_config (other_pool, config)) {
4296 config = gst_buffer_pool_get_config (other_pool);
4298 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
4300 gst_structure_free (config);
4304 if (!gst_buffer_pool_set_config (other_pool, config))
4310 /* For simplicity, simply read back the active configuration, so our base
4311 * class get the right information */
4312 config = gst_buffer_pool_get_config (pool);
4313 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
4314 gst_structure_free (config);
4318 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
4320 gst_query_add_allocation_pool (query, pool, size, min, max);
4323 gst_object_unref (allocator);
4326 gst_object_unref (pool);
4332 /* setup_pool already send the error */
4337 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4338 (_("Failed to configure internal buffer pool.")), (NULL));
4343 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4344 (_("Video device did not suggest any buffer size.")), (NULL));
4350 gst_object_unref (allocator);
4353 gst_object_unref (pool);
4358 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4359 (_("No downstream pool to import from.")),
4360 ("When importing DMABUF or USERPTR, we need a pool to import from"));
4366 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
4368 GstBufferPool *pool;
4369 /* we need at least 2 buffers to operate */
4370 guint size, min, max;
4374 /* Set defaults allocation parameters */
4375 size = obj->info.size;
4376 min = GST_V4L2_MIN_BUFFERS;
4377 max = VIDEO_MAX_FRAME;
4379 gst_query_parse_allocation (query, &caps, &need_pool);
4384 if ((pool = obj->pool))
4385 gst_object_ref (pool);
4389 GstStructure *config;
4391 /* we had a pool, check caps */
4392 config = gst_buffer_pool_get_config (pool);
4393 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
4395 GST_DEBUG_OBJECT (obj->element,
4396 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
4397 if (!gst_caps_is_equal (caps, pcaps)) {
4398 gst_structure_free (config);
4399 gst_object_unref (pool);
4400 goto different_caps;
4402 gst_structure_free (config);
4404 gst_v4l2_get_driver_min_buffers (obj);
4406 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS);
4408 gst_query_add_allocation_pool (query, pool, size, min, max);
4410 /* we also support various metadata */
4411 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4414 gst_object_unref (pool);
4421 GST_DEBUG_OBJECT (obj->element, "no caps specified");
4426 /* different caps, we can't use this pool */
4427 GST_DEBUG_OBJECT (obj->element, "pool has different caps");