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,
32 #include <gudev/gudev.h>
35 #include "v4l2_calls.h"
36 #include "gstv4l2tuner.h"
37 #include "gstv4l2colorbalance.h"
39 #include "gst/gst-i18n-plugin.h"
41 #include <gst/video/video.h>
43 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
44 #define GST_CAT_DEFAULT v4l2_debug
46 #define DEFAULT_PROP_DEVICE_NAME NULL
47 #define DEFAULT_PROP_DEVICE_FD -1
48 #define DEFAULT_PROP_FLAGS 0
49 #define DEFAULT_PROP_TV_NORM 0
50 #define DEFAULT_PROP_CHANNEL NULL
51 #define DEFAULT_PROP_FREQUENCY 0
52 #define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
54 #define ENCODED_BUFFER_SIZE (1 * 1024 * 1024)
59 V4L2_STD_OBJECT_PROPS,
63 * common format / caps utilities:
67 GST_V4L2_RAW = 1 << 0,
68 GST_V4L2_CODEC = 1 << 1,
69 GST_V4L2_TRANSPORT = 1 << 2,
70 GST_V4L2_NO_PARSE = 1 << 3,
78 GstV4L2FormatFlags flags;
81 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
83 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
84 {V4L2_PIX_FMT_ARGB555, TRUE, GST_V4L2_RAW},
85 {V4L2_PIX_FMT_XRGB555, TRUE, GST_V4L2_RAW},
86 {V4L2_PIX_FMT_ARGB555X, TRUE, GST_V4L2_RAW},
87 {V4L2_PIX_FMT_XRGB555X, TRUE, GST_V4L2_RAW},
88 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
89 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
90 {V4L2_PIX_FMT_BGR666, TRUE, GST_V4L2_RAW},
91 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
92 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
93 {V4L2_PIX_FMT_ABGR32, TRUE, GST_V4L2_RAW},
94 {V4L2_PIX_FMT_XBGR32, TRUE, GST_V4L2_RAW},
95 {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
96 {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},
98 /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
99 {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
100 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
101 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
102 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
103 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
106 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
107 {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
108 {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
109 {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
110 {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
111 {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
112 {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
113 {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},
114 {V4L2_PIX_FMT_INVZ, TRUE, GST_V4L2_RAW},
116 /* Palette formats */
117 {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},
119 /* Chrominance formats */
120 {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},
122 /* Luminance+Chrominance formats */
123 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
124 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
125 {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
126 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
127 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
128 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
129 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
130 {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
131 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
132 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
133 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
134 {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
135 {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
136 {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
137 {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
138 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
139 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
140 {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
141 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
142 {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
143 {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
145 /* two planes -- one Y, one Cr + Cb interleaved */
146 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
147 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
148 {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
149 {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
150 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
151 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
152 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
153 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
154 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
155 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
156 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
159 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
160 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
161 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_CODEC},
162 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_CODEC},
163 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_CODEC},
165 /* compressed formats */
166 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
167 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
168 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
169 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
170 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
171 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
172 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
173 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
174 {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
175 {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
176 {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
177 {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
178 {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
179 {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
180 {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
181 {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
183 /* Vendor-specific formats */
184 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
185 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
186 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
187 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
190 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
192 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
195 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
197 gst_v4l2_device_get_type (void)
199 static GType v4l2_device_type = 0;
201 if (v4l2_device_type == 0) {
202 static const GFlagsValue values[] = {
203 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
204 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
205 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
207 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
208 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
210 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
211 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
217 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
220 return v4l2_device_type;
223 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
225 gst_v4l2_tv_norm_get_type (void)
227 static GType v4l2_tv_norm = 0;
230 static const GEnumValue tv_norms[] = {
233 {V4L2_STD_NTSC, "NTSC", "NTSC"},
234 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
235 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
236 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
237 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
239 {V4L2_STD_PAL, "PAL", "PAL"},
240 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
241 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
242 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
243 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
244 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
245 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
246 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
247 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
248 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
249 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
250 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
251 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
252 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
253 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
255 {V4L2_STD_SECAM, "SECAM", "SECAM"},
256 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
257 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
258 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
259 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
260 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
261 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
262 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
263 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
264 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
269 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
276 gst_v4l2_io_mode_get_type (void)
278 static GType v4l2_io_mode = 0;
281 static const GEnumValue io_modes[] = {
282 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
283 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
284 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
285 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
286 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
287 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
292 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
298 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
299 const char *default_device)
301 g_object_class_install_property (gobject_class, PROP_DEVICE,
302 g_param_spec_string ("device", "Device", "Device location",
303 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
305 g_param_spec_string ("device-name", "Device name",
306 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
307 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
309 g_param_spec_int ("device-fd", "File descriptor",
310 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
311 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (gobject_class, PROP_FLAGS,
313 g_param_spec_flags ("flags", "Flags", "Device type flags",
314 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
315 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
318 * GstV4l2Src:brightness:
320 * Picture brightness, or more precisely, the black level
322 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
323 g_param_spec_int ("brightness", "Brightness",
324 "Picture brightness, or more precisely, the black level", G_MININT,
326 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
328 * GstV4l2Src:contrast:
330 * Picture contrast or luma gain
332 g_object_class_install_property (gobject_class, PROP_CONTRAST,
333 g_param_spec_int ("contrast", "Contrast",
334 "Picture contrast or luma gain", G_MININT,
336 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
338 * GstV4l2Src:saturation:
340 * Picture color saturation or chroma gain
342 g_object_class_install_property (gobject_class, PROP_SATURATION,
343 g_param_spec_int ("saturation", "Saturation",
344 "Picture color saturation or chroma gain", G_MININT,
346 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
350 * Hue or color balance
352 g_object_class_install_property (gobject_class, PROP_HUE,
353 g_param_spec_int ("hue", "Hue",
354 "Hue or color balance", G_MININT,
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
363 g_object_class_install_property (gobject_class, PROP_TV_NORM,
364 g_param_spec_enum ("norm", "TV norm",
366 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
367 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 * GstV4l2Src:io-mode:
374 g_object_class_install_property (gobject_class, PROP_IO_MODE,
375 g_param_spec_enum ("io-mode", "IO mode",
377 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
378 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
381 * GstV4l2Src:extra-controls:
383 * Additional v4l2 controls for the device. The controls are identified
384 * by the control name (lowercase with '_' for any non-alphanumeric
389 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
390 g_param_spec_boxed ("extra-controls", "Extra Controls",
391 "Extra v4l2 controls (CIDs) for the device",
392 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
395 * GstV4l2Src:pixel-aspect-ratio:
397 * The pixel aspect ratio of the device. This overwrites the pixel aspect
398 * ratio queried from the device.
402 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
403 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
404 "Overwrite the pixel aspect ratio of the device", "1/1",
405 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 * GstV4l2Src:force-aspect-ratio:
410 * When enabled, the pixel aspect ratio queried from the device or set
411 * with the pixel-aspect-ratio property will be enforced.
415 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
416 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
417 "When enabled, the pixel aspect ratio will be enforced", TRUE,
418 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
423 gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
425 g_object_class_install_property (gobject_class, PROP_DEVICE,
426 g_param_spec_string ("device", "Device", "Device location",
427 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
429 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
430 g_param_spec_string ("device-name", "Device name",
431 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
432 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
434 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
435 g_param_spec_int ("device-fd", "File descriptor",
436 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
437 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
439 g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
440 g_param_spec_enum ("output-io-mode", "Output IO mode",
441 "Output side I/O mode (matches sink pad)",
442 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
443 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445 g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
446 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
447 "Capture I/O mode (matches src pad)",
448 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
449 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
451 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
452 g_param_spec_boxed ("extra-controls", "Extra Controls",
453 "Extra v4l2 controls (CIDs) for the device",
454 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
458 gst_v4l2_object_new (GstElement * element,
459 enum v4l2_buf_type type,
460 const char *default_device,
461 GstV4l2GetInOutFunction get_in_out_func,
462 GstV4l2SetInOutFunction set_in_out_func,
463 GstV4l2UpdateFpsFunction update_fps_func)
465 GstV4l2Object *v4l2object;
468 * some default values
470 v4l2object = g_new0 (GstV4l2Object, 1);
472 v4l2object->type = type;
473 v4l2object->formats = NULL;
475 v4l2object->element = element;
476 v4l2object->get_in_out_func = get_in_out_func;
477 v4l2object->set_in_out_func = set_in_out_func;
478 v4l2object->update_fps_func = update_fps_func;
480 v4l2object->video_fd = -1;
481 v4l2object->active = FALSE;
482 v4l2object->videodev = g_strdup (default_device);
484 v4l2object->norms = NULL;
485 v4l2object->channels = NULL;
486 v4l2object->colors = NULL;
488 v4l2object->xwindow_id = 0;
490 v4l2object->keep_aspect = TRUE;
492 v4l2object->n_v4l2_planes = 0;
494 v4l2object->no_initial_format = FALSE;
499 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
503 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
505 g_return_if_fail (v4l2object != NULL);
507 g_free (v4l2object->videodev);
509 g_free (v4l2object->channel);
511 if (v4l2object->formats) {
512 gst_v4l2_object_clear_format_list (v4l2object);
515 if (v4l2object->probed_caps) {
516 gst_caps_unref (v4l2object->probed_caps);
519 if (v4l2object->extra_controls) {
520 gst_structure_free (v4l2object->extra_controls);
528 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
530 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
531 g_slist_free (v4l2object->formats);
532 v4l2object->formats = NULL;
538 gst_v4l2_object_prop_to_cid (guint prop_id)
543 case PROP_BRIGHTNESS:
544 cid = V4L2_CID_BRIGHTNESS;
547 cid = V4L2_CID_CONTRAST;
549 case PROP_SATURATION:
550 cid = V4L2_CID_SATURATION;
556 GST_WARNING ("unmapped property id: %d", prop_id);
563 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
564 guint prop_id, const GValue * value, GParamSpec * pspec)
568 g_free (v4l2object->videodev);
569 v4l2object->videodev = g_value_dup_string (value);
571 case PROP_BRIGHTNESS:
573 case PROP_SATURATION:
576 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
579 if (GST_V4L2_IS_OPEN (v4l2object)) {
580 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
587 v4l2object->tv_norm = g_value_get_enum (value);
591 if (GST_V4L2_IS_OPEN (v4l2object)) {
592 GstTuner *tuner = GST_TUNER (v4l2object->element);
593 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
594 (gchar *) g_value_get_string (value));
597 /* like gst_tuner_set_channel (tuner, channel)
598 without g_object_notify */
599 gst_v4l2_tuner_set_channel (v4l2object, channel);
602 g_free (v4l2object->channel);
603 v4l2object->channel = g_value_dup_string (value);
607 if (GST_V4L2_IS_OPEN (v4l2object)) {
608 GstTuner *tuner = GST_TUNER (v4l2object->element);
609 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
612 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
614 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
615 without g_object_notify */
616 gst_v4l2_tuner_set_frequency (v4l2object, channel,
617 g_value_get_ulong (value));
620 v4l2object->frequency = g_value_get_ulong (value);
626 v4l2object->req_mode = g_value_get_enum (value);
628 case PROP_CAPTURE_IO_MODE:
629 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
630 v4l2object->req_mode = g_value_get_enum (value);
632 case PROP_OUTPUT_IO_MODE:
633 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
634 v4l2object->req_mode = g_value_get_enum (value);
636 case PROP_EXTRA_CONTROLS:{
637 const GstStructure *s = gst_value_get_structure (value);
639 if (v4l2object->extra_controls)
640 gst_structure_free (v4l2object->extra_controls);
642 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
643 if (GST_V4L2_IS_OPEN (v4l2object))
644 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
647 case PROP_PIXEL_ASPECT_RATIO:
648 g_free (v4l2object->par);
649 v4l2object->par = g_new0 (GValue, 1);
650 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
651 if (!g_value_transform (value, v4l2object->par)) {
652 g_warning ("Could not transform string to aspect ratio");
653 gst_value_set_fraction (v4l2object->par, 1, 1);
655 GST_DEBUG_OBJECT (v4l2object->element, "set PAR to %d/%d",
656 gst_value_get_fraction_numerator (v4l2object->par),
657 gst_value_get_fraction_denominator (v4l2object->par));
659 case PROP_FORCE_ASPECT_RATIO:
660 v4l2object->keep_aspect = g_value_get_boolean (value);
671 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
672 guint prop_id, GValue * value, GParamSpec * pspec)
676 g_value_set_string (value, v4l2object->videodev);
678 case PROP_DEVICE_NAME:
680 const guchar *new = NULL;
682 if (GST_V4L2_IS_OPEN (v4l2object)) {
683 new = v4l2object->vcap.card;
684 } else if (gst_v4l2_open (v4l2object)) {
685 new = v4l2object->vcap.card;
686 gst_v4l2_close (v4l2object);
688 g_value_set_string (value, (gchar *) new);
693 if (GST_V4L2_IS_OPEN (v4l2object))
694 g_value_set_int (value, v4l2object->video_fd);
696 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
703 if (GST_V4L2_IS_OPEN (v4l2object)) {
704 flags |= v4l2object->device_caps &
705 (V4L2_CAP_VIDEO_CAPTURE |
706 V4L2_CAP_VIDEO_OUTPUT |
707 V4L2_CAP_VIDEO_OVERLAY |
708 V4L2_CAP_VBI_CAPTURE |
709 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
711 if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
712 flags |= V4L2_CAP_VIDEO_CAPTURE;
714 if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
715 flags |= V4L2_CAP_VIDEO_OUTPUT;
717 g_value_set_flags (value, flags);
720 case PROP_BRIGHTNESS:
722 case PROP_SATURATION:
725 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
728 if (GST_V4L2_IS_OPEN (v4l2object)) {
730 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
731 g_value_set_int (value, v);
739 g_value_set_enum (value, v4l2object->tv_norm);
742 g_value_set_enum (value, v4l2object->req_mode);
744 case PROP_CAPTURE_IO_MODE:
745 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
746 g_value_set_enum (value, v4l2object->req_mode);
748 case PROP_OUTPUT_IO_MODE:
749 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
750 g_value_set_enum (value, v4l2object->req_mode);
752 case PROP_EXTRA_CONTROLS:
753 gst_value_set_structure (value, v4l2object->extra_controls);
755 case PROP_PIXEL_ASPECT_RATIO:
757 g_value_transform (v4l2object->par, value);
759 case PROP_FORCE_ASPECT_RATIO:
760 g_value_set_boolean (value, v4l2object->keep_aspect);
770 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
772 struct v4l2_control control = { 0, };
774 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
776 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
777 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
779 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
781 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
782 GST_DEBUG_OBJECT (v4l2object->element,
783 "driver requires a minimum of %d buffers", control.value);
784 v4l2object->min_buffers = control.value;
786 v4l2object->min_buffers = 0;
791 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
793 GstTunerNorm *norm = NULL;
794 GstTunerChannel *channel = NULL;
797 if (!GST_IS_TUNER (v4l2object->element))
800 tuner = GST_TUNER (v4l2object->element);
802 if (v4l2object->tv_norm)
803 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
804 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
805 "norm=%p", (guint64) v4l2object->tv_norm, norm);
807 gst_tuner_set_norm (tuner, norm);
810 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
812 v4l2object->tv_norm =
813 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
814 gst_tuner_norm_changed (tuner, norm);
818 if (v4l2object->channel)
819 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
821 gst_tuner_set_channel (tuner, channel);
824 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
825 (v4l2object->element)));
827 g_free (v4l2object->channel);
828 v4l2object->channel = g_strdup (channel->label);
829 gst_tuner_channel_changed (tuner, channel);
834 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
835 if (v4l2object->frequency != 0) {
836 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
838 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
839 if (v4l2object->frequency == 0) {
841 gst_tuner_set_frequency (tuner, channel, 1000);
849 gst_v4l2_object_open (GstV4l2Object * v4l2object)
851 if (gst_v4l2_open (v4l2object))
852 gst_v4l2_set_defaults (v4l2object);
860 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
864 ret = gst_v4l2_dup (v4l2object, other);
870 gst_v4l2_object_close (GstV4l2Object * v4l2object)
872 if (!gst_v4l2_close (v4l2object))
875 gst_caps_replace (&v4l2object->probed_caps, NULL);
877 /* reset our copy of the device caps */
878 v4l2object->device_caps = 0;
880 if (v4l2object->formats) {
881 gst_v4l2_object_clear_format_list (v4l2object);
887 static struct v4l2_fmtdesc *
888 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
891 struct v4l2_fmtdesc *fmt;
897 walk = gst_v4l2_object_get_format_list (v4l2object);
899 fmt = (struct v4l2_fmtdesc *) walk->data;
900 if (fmt->pixelformat == fourcc)
902 /* special case for jpeg */
903 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
904 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
905 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
906 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
907 fourcc == V4L2_PIX_FMT_PJPG) {
911 walk = g_slist_next (walk);
919 /* complete made up ranking, the values themselves are meaningless */
920 /* These ranks MUST be X such that X<<15 fits on a signed int - see
921 the comment at the end of gst_v4l2_object_format_get_rank. */
922 #define YUV_BASE_RANK 1000
923 #define JPEG_BASE_RANK 500
924 #define DV_BASE_RANK 200
925 #define RGB_BASE_RANK 100
926 #define YUV_ODD_BASE_RANK 50
927 #define RGB_ODD_BASE_RANK 25
928 #define BAYER_BASE_RANK 15
929 #define S910_BASE_RANK 10
930 #define GREY_BASE_RANK 5
931 #define PWC_BASE_RANK 1
934 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
936 guint32 fourcc = fmt->pixelformat;
937 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
941 case V4L2_PIX_FMT_MJPEG:
942 case V4L2_PIX_FMT_PJPG:
943 rank = JPEG_BASE_RANK;
945 case V4L2_PIX_FMT_JPEG:
946 rank = JPEG_BASE_RANK + 1;
948 case V4L2_PIX_FMT_MPEG: /* MPEG */
949 rank = JPEG_BASE_RANK + 2;
952 case V4L2_PIX_FMT_RGB332:
953 case V4L2_PIX_FMT_ARGB555:
954 case V4L2_PIX_FMT_XRGB555:
955 case V4L2_PIX_FMT_RGB555:
956 case V4L2_PIX_FMT_ARGB555X:
957 case V4L2_PIX_FMT_XRGB555X:
958 case V4L2_PIX_FMT_RGB555X:
959 case V4L2_PIX_FMT_BGR666:
960 case V4L2_PIX_FMT_RGB565:
961 case V4L2_PIX_FMT_RGB565X:
962 case V4L2_PIX_FMT_RGB444:
963 case V4L2_PIX_FMT_Y4:
964 case V4L2_PIX_FMT_Y6:
965 case V4L2_PIX_FMT_Y10:
966 case V4L2_PIX_FMT_Y12:
967 case V4L2_PIX_FMT_Y10BPACK:
968 case V4L2_PIX_FMT_YUV555:
969 case V4L2_PIX_FMT_YUV565:
970 case V4L2_PIX_FMT_YUV32:
971 case V4L2_PIX_FMT_NV12MT_16X16:
972 case V4L2_PIX_FMT_NV42:
973 case V4L2_PIX_FMT_H264_MVC:
974 rank = RGB_ODD_BASE_RANK;
977 case V4L2_PIX_FMT_RGB24:
978 case V4L2_PIX_FMT_BGR24:
979 rank = RGB_BASE_RANK - 1;
982 case V4L2_PIX_FMT_RGB32:
983 case V4L2_PIX_FMT_BGR32:
984 case V4L2_PIX_FMT_ABGR32:
985 case V4L2_PIX_FMT_XBGR32:
986 case V4L2_PIX_FMT_ARGB32:
987 case V4L2_PIX_FMT_XRGB32:
988 rank = RGB_BASE_RANK;
991 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
992 case V4L2_PIX_FMT_INVZ:
993 rank = GREY_BASE_RANK;
996 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
997 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
998 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
999 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1000 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1001 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1002 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1003 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1004 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1005 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1006 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1007 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1008 rank = YUV_ODD_BASE_RANK;
1011 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1012 rank = YUV_BASE_RANK + 3;
1014 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1015 rank = YUV_BASE_RANK + 2;
1017 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1018 case V4L2_PIX_FMT_YUV420M:
1019 rank = YUV_BASE_RANK + 7;
1021 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1022 rank = YUV_BASE_RANK + 10;
1024 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1025 rank = YUV_BASE_RANK + 6;
1027 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1028 rank = YUV_BASE_RANK + 9;
1030 case V4L2_PIX_FMT_YUV444:
1031 rank = YUV_BASE_RANK + 6;
1033 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1034 rank = YUV_BASE_RANK + 5;
1036 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1037 rank = YUV_BASE_RANK + 4;
1039 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1040 rank = YUV_BASE_RANK + 8;
1043 case V4L2_PIX_FMT_DV:
1044 rank = DV_BASE_RANK;
1047 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1051 case V4L2_PIX_FMT_SBGGR8:
1052 case V4L2_PIX_FMT_SGBRG8:
1053 case V4L2_PIX_FMT_SGRBG8:
1054 case V4L2_PIX_FMT_SRGGB8:
1055 rank = BAYER_BASE_RANK;
1058 case V4L2_PIX_FMT_SN9C10X:
1059 rank = S910_BASE_RANK;
1062 case V4L2_PIX_FMT_PWC1:
1063 rank = PWC_BASE_RANK;
1065 case V4L2_PIX_FMT_PWC2:
1066 rank = PWC_BASE_RANK;
1074 /* All ranks are below 1<<15 so a shift by 15
1075 * will a) make all non-emulated formats larger
1076 * than emulated and b) will not overflow
1087 format_cmp_func (gconstpointer a, gconstpointer b)
1089 const struct v4l2_fmtdesc *fa = a;
1090 const struct v4l2_fmtdesc *fb = b;
1092 if (fa->pixelformat == fb->pixelformat)
1095 return gst_v4l2_object_format_get_rank (fb) -
1096 gst_v4l2_object_format_get_rank (fa);
1099 /******************************************************
1100 * gst_v4l2_object_fill_format_list():
1101 * create list of supported capture formats
1102 * return value: TRUE on success, FALSE on error
1103 ******************************************************/
1105 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1106 enum v4l2_buf_type type)
1109 struct v4l2_fmtdesc *format;
1111 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1113 /* format enumeration */
1115 format = g_new0 (struct v4l2_fmtdesc, 1);
1118 format->type = type;
1120 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1121 if (errno == EINVAL) {
1123 break; /* end of enumeration */
1129 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1130 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1131 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1132 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1133 format->description);
1134 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1135 GST_FOURCC_ARGS (format->pixelformat));
1137 /* sort formats according to our preference; we do this, because caps
1138 * are probed in the order the formats are in the list, and the order of
1139 * formats in the final probed caps matters for things like fixation */
1140 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1141 (GCompareFunc) format_cmp_func);
1144 #ifndef GST_DISABLE_GST_DEBUG
1148 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1149 for (l = v4l2object->formats; l != NULL; l = l->next) {
1152 GST_INFO_OBJECT (v4l2object->element,
1153 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1154 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1166 if (!GST_IS_ELEMENT (v4l2object->element))
1169 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1170 (_("Failed to enumerate possible video formats device '%s' can work "
1171 "with"), v4l2object->videodev),
1172 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1173 n, v4l2object->videodev, errno, g_strerror (errno)));
1180 * Get the list of supported capture formats, a list of
1181 * <code>struct v4l2_fmtdesc</code>.
1184 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1186 if (!v4l2object->formats) {
1188 /* check usual way */
1189 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1191 /* if our driver supports multi-planar
1192 * and if formats are still empty then we can workaround driver bug
1193 * by also looking up formats as if our device was not supporting
1195 if (!v4l2object->formats) {
1196 switch (v4l2object->type) {
1197 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1198 gst_v4l2_object_fill_format_list (v4l2object,
1199 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1202 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1203 gst_v4l2_object_fill_format_list (v4l2object,
1204 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1212 return v4l2object->formats;
1215 static GstVideoFormat
1216 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1218 GstVideoFormat format;
1221 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1222 format = GST_VIDEO_FORMAT_GRAY8;
1224 case V4L2_PIX_FMT_Y16:
1225 format = GST_VIDEO_FORMAT_GRAY16_LE;
1227 case V4L2_PIX_FMT_Y16_BE:
1228 format = GST_VIDEO_FORMAT_GRAY16_BE;
1230 case V4L2_PIX_FMT_XRGB555:
1231 case V4L2_PIX_FMT_RGB555:
1232 format = GST_VIDEO_FORMAT_RGB15;
1234 case V4L2_PIX_FMT_XRGB555X:
1235 case V4L2_PIX_FMT_RGB555X:
1236 format = GST_VIDEO_FORMAT_BGR15;
1238 case V4L2_PIX_FMT_RGB565:
1239 format = GST_VIDEO_FORMAT_RGB16;
1241 case V4L2_PIX_FMT_RGB24:
1242 format = GST_VIDEO_FORMAT_RGB;
1244 case V4L2_PIX_FMT_BGR24:
1245 format = GST_VIDEO_FORMAT_BGR;
1247 case V4L2_PIX_FMT_XRGB32:
1248 case V4L2_PIX_FMT_RGB32:
1249 format = GST_VIDEO_FORMAT_xRGB;
1251 case V4L2_PIX_FMT_XBGR32:
1252 case V4L2_PIX_FMT_BGR32:
1253 format = GST_VIDEO_FORMAT_BGRx;
1255 case V4L2_PIX_FMT_ABGR32:
1256 format = GST_VIDEO_FORMAT_BGRA;
1258 case V4L2_PIX_FMT_ARGB32:
1259 format = GST_VIDEO_FORMAT_ARGB;
1261 case V4L2_PIX_FMT_NV12:
1262 case V4L2_PIX_FMT_NV12M:
1263 format = GST_VIDEO_FORMAT_NV12;
1265 case V4L2_PIX_FMT_NV12MT:
1266 format = GST_VIDEO_FORMAT_NV12_64Z32;
1268 case V4L2_PIX_FMT_NV21:
1269 case V4L2_PIX_FMT_NV21M:
1270 format = GST_VIDEO_FORMAT_NV21;
1272 case V4L2_PIX_FMT_YVU410:
1273 format = GST_VIDEO_FORMAT_YVU9;
1275 case V4L2_PIX_FMT_YUV410:
1276 format = GST_VIDEO_FORMAT_YUV9;
1278 case V4L2_PIX_FMT_YUV420:
1279 case V4L2_PIX_FMT_YUV420M:
1280 format = GST_VIDEO_FORMAT_I420;
1282 case V4L2_PIX_FMT_YUYV:
1283 format = GST_VIDEO_FORMAT_YUY2;
1285 case V4L2_PIX_FMT_YVU420:
1286 format = GST_VIDEO_FORMAT_YV12;
1288 case V4L2_PIX_FMT_UYVY:
1289 format = GST_VIDEO_FORMAT_UYVY;
1291 case V4L2_PIX_FMT_YUV411P:
1292 format = GST_VIDEO_FORMAT_Y41B;
1294 case V4L2_PIX_FMT_YUV422P:
1295 format = GST_VIDEO_FORMAT_Y42B;
1297 case V4L2_PIX_FMT_YVYU:
1298 format = GST_VIDEO_FORMAT_YVYU;
1300 case V4L2_PIX_FMT_NV16:
1301 case V4L2_PIX_FMT_NV16M:
1302 format = GST_VIDEO_FORMAT_NV16;
1304 case V4L2_PIX_FMT_NV61:
1305 case V4L2_PIX_FMT_NV61M:
1306 format = GST_VIDEO_FORMAT_NV61;
1308 case V4L2_PIX_FMT_NV24:
1309 format = GST_VIDEO_FORMAT_NV24;
1311 case V4L2_PIX_FMT_INVZ:
1312 format = GST_VIDEO_FORMAT_INVZ;
1315 format = GST_VIDEO_FORMAT_UNKNOWN;
1323 gst_v4l2_object_v4l2fourcc_is_rgb (guint32 fourcc)
1325 gboolean ret = FALSE;
1328 case V4L2_PIX_FMT_XRGB555:
1329 case V4L2_PIX_FMT_RGB555:
1330 case V4L2_PIX_FMT_XRGB555X:
1331 case V4L2_PIX_FMT_RGB555X:
1332 case V4L2_PIX_FMT_RGB565:
1333 case V4L2_PIX_FMT_RGB24:
1334 case V4L2_PIX_FMT_BGR24:
1335 case V4L2_PIX_FMT_XRGB32:
1336 case V4L2_PIX_FMT_RGB32:
1337 case V4L2_PIX_FMT_XBGR32:
1338 case V4L2_PIX_FMT_BGR32:
1339 case V4L2_PIX_FMT_ABGR32:
1340 case V4L2_PIX_FMT_ARGB32:
1350 static GstStructure *
1351 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1353 GstStructure *structure = NULL;
1356 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1357 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1358 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1359 structure = gst_structure_new_empty ("image/jpeg");
1361 case V4L2_PIX_FMT_MPEG1:
1362 structure = gst_structure_new ("video/mpeg",
1363 "mpegversion", G_TYPE_INT, 2, NULL);
1365 case V4L2_PIX_FMT_MPEG2:
1366 structure = gst_structure_new ("video/mpeg",
1367 "mpegversion", G_TYPE_INT, 2, NULL);
1369 case V4L2_PIX_FMT_MPEG4:
1370 case V4L2_PIX_FMT_XVID:
1371 structure = gst_structure_new ("video/mpeg",
1372 "mpegversion", G_TYPE_INT, 4, "systemstream",
1373 G_TYPE_BOOLEAN, FALSE, NULL);
1375 case V4L2_PIX_FMT_H263:
1376 structure = gst_structure_new ("video/x-h263",
1377 "variant", G_TYPE_STRING, "itu", NULL);
1379 case V4L2_PIX_FMT_H264: /* H.264 */
1380 structure = gst_structure_new ("video/x-h264",
1381 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1382 G_TYPE_STRING, "au", NULL);
1384 case V4L2_PIX_FMT_H264_NO_SC:
1385 structure = gst_structure_new ("video/x-h264",
1386 "stream-format", G_TYPE_STRING, "avc", "alignment",
1387 G_TYPE_STRING, "au", NULL);
1389 case V4L2_PIX_FMT_VC1_ANNEX_G:
1390 case V4L2_PIX_FMT_VC1_ANNEX_L:
1391 structure = gst_structure_new ("video/x-wmv",
1392 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1394 case V4L2_PIX_FMT_VP8:
1395 structure = gst_structure_new_empty ("video/x-vp8");
1397 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1398 case V4L2_PIX_FMT_Y16:
1399 case V4L2_PIX_FMT_Y16_BE:
1400 case V4L2_PIX_FMT_XRGB555:
1401 case V4L2_PIX_FMT_RGB555:
1402 case V4L2_PIX_FMT_XRGB555X:
1403 case V4L2_PIX_FMT_RGB555X:
1404 case V4L2_PIX_FMT_RGB565:
1405 case V4L2_PIX_FMT_RGB24:
1406 case V4L2_PIX_FMT_BGR24:
1407 case V4L2_PIX_FMT_RGB32:
1408 case V4L2_PIX_FMT_XRGB32:
1409 case V4L2_PIX_FMT_ARGB32:
1410 case V4L2_PIX_FMT_BGR32:
1411 case V4L2_PIX_FMT_XBGR32:
1412 case V4L2_PIX_FMT_ABGR32:
1413 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1414 case V4L2_PIX_FMT_NV12M:
1415 case V4L2_PIX_FMT_NV12MT:
1416 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1417 case V4L2_PIX_FMT_NV21M:
1418 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1419 case V4L2_PIX_FMT_NV16M:
1420 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1421 case V4L2_PIX_FMT_NV61M:
1422 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1423 case V4L2_PIX_FMT_YVU410:
1424 case V4L2_PIX_FMT_YUV410:
1425 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1426 case V4L2_PIX_FMT_YUV420M:
1427 case V4L2_PIX_FMT_YUYV:
1428 case V4L2_PIX_FMT_YVU420:
1429 case V4L2_PIX_FMT_UYVY:
1430 case V4L2_PIX_FMT_YUV422P:
1431 case V4L2_PIX_FMT_YVYU:
1432 case V4L2_PIX_FMT_YUV411P:
1433 case V4L2_PIX_FMT_INVZ:{
1434 GstVideoFormat format;
1435 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1436 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1437 structure = gst_structure_new ("video/x-raw",
1438 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1441 case V4L2_PIX_FMT_DV:
1443 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1446 case V4L2_PIX_FMT_MPEG: /* MPEG */
1447 structure = gst_structure_new ("video/mpegts",
1448 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1450 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1452 case V4L2_PIX_FMT_SBGGR8:
1453 case V4L2_PIX_FMT_SGBRG8:
1454 case V4L2_PIX_FMT_SGRBG8:
1455 case V4L2_PIX_FMT_SRGGB8:
1456 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1457 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1458 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1459 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1460 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1462 case V4L2_PIX_FMT_SN9C10X:
1463 structure = gst_structure_new_empty ("video/x-sonix");
1465 case V4L2_PIX_FMT_PWC1:
1466 structure = gst_structure_new_empty ("video/x-pwc1");
1468 case V4L2_PIX_FMT_PWC2:
1469 structure = gst_structure_new_empty ("video/x-pwc2");
1471 case V4L2_PIX_FMT_RGB332:
1472 case V4L2_PIX_FMT_BGR666:
1473 case V4L2_PIX_FMT_ARGB555X:
1474 case V4L2_PIX_FMT_RGB565X:
1475 case V4L2_PIX_FMT_RGB444:
1476 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1477 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1478 case V4L2_PIX_FMT_Y4:
1479 case V4L2_PIX_FMT_Y6:
1480 case V4L2_PIX_FMT_Y10:
1481 case V4L2_PIX_FMT_Y12:
1482 case V4L2_PIX_FMT_Y10BPACK:
1483 case V4L2_PIX_FMT_YUV444:
1484 case V4L2_PIX_FMT_YUV555:
1485 case V4L2_PIX_FMT_YUV565:
1486 case V4L2_PIX_FMT_Y41P:
1487 case V4L2_PIX_FMT_YUV32:
1488 case V4L2_PIX_FMT_NV12MT_16X16:
1489 case V4L2_PIX_FMT_NV42:
1490 case V4L2_PIX_FMT_H264_MVC:
1492 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1493 fourcc, GST_FOURCC_ARGS (fourcc));
1501 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1503 GstStructure *template;
1506 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1508 if (template == NULL)
1511 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1512 if (gst_v4l2_formats[i].format != fourcc)
1515 if (gst_v4l2_formats[i].dimensions) {
1516 gst_structure_set (template,
1517 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1518 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1519 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1530 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1532 GstStructure *structure;
1536 caps = gst_caps_new_empty ();
1537 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1539 if ((gst_v4l2_formats[i].flags & flags) == 0)
1543 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1546 GstStructure *alt_s = NULL;
1548 if (gst_v4l2_formats[i].dimensions) {
1549 gst_structure_set (structure,
1550 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1551 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1552 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1555 switch (gst_v4l2_formats[i].format) {
1556 case V4L2_PIX_FMT_RGB32:
1557 alt_s = gst_structure_copy (structure);
1558 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1560 case V4L2_PIX_FMT_BGR32:
1561 alt_s = gst_structure_copy (structure);
1562 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1567 gst_caps_append_structure (caps, structure);
1570 gst_caps_append_structure (caps, alt_s);
1574 return gst_caps_simplify (caps);
1578 gst_v4l2_object_get_all_caps (void)
1580 static GstCaps *caps = NULL;
1582 if (g_once_init_enter (&caps)) {
1583 GstCaps *all_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1584 GST_MINI_OBJECT_FLAG_SET (all_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1585 g_once_init_leave (&caps, all_caps);
1592 gst_v4l2_object_get_raw_caps (void)
1594 static GstCaps *caps = NULL;
1596 if (g_once_init_enter (&caps)) {
1597 GstCaps *raw_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1598 GST_MINI_OBJECT_FLAG_SET (raw_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1599 g_once_init_leave (&caps, raw_caps);
1606 gst_v4l2_object_get_codec_caps (void)
1608 static GstCaps *caps = NULL;
1610 if (g_once_init_enter (&caps)) {
1611 GstCaps *codec_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1612 GST_MINI_OBJECT_FLAG_SET (codec_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1613 g_once_init_leave (&caps, codec_caps);
1619 /* collect data for the given caps
1620 * @caps: given input caps
1621 * @format: location for the v4l format
1622 * @w/@h: location for width and height
1623 * @fps_n/@fps_d: location for framerate
1624 * @size: location for expected size of the frame or 0 if unknown
1627 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1628 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1630 GstStructure *structure;
1631 guint32 fourcc = 0, fourcc_nc = 0;
1632 const gchar *mimetype;
1633 struct v4l2_fmtdesc *fmt = NULL;
1635 structure = gst_caps_get_structure (caps, 0);
1637 mimetype = gst_structure_get_name (structure);
1639 if (!gst_video_info_from_caps (info, caps))
1640 goto invalid_format;
1642 if (g_str_equal (mimetype, "video/x-raw")) {
1643 switch (GST_VIDEO_INFO_FORMAT (info)) {
1644 case GST_VIDEO_FORMAT_I420:
1645 fourcc = V4L2_PIX_FMT_YUV420;
1646 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1648 case GST_VIDEO_FORMAT_YUY2:
1649 fourcc = V4L2_PIX_FMT_YUYV;
1651 case GST_VIDEO_FORMAT_UYVY:
1652 fourcc = V4L2_PIX_FMT_UYVY;
1654 case GST_VIDEO_FORMAT_YV12:
1655 fourcc = V4L2_PIX_FMT_YVU420;
1657 case GST_VIDEO_FORMAT_Y41B:
1658 fourcc = V4L2_PIX_FMT_YUV411P;
1660 case GST_VIDEO_FORMAT_Y42B:
1661 fourcc = V4L2_PIX_FMT_YUV422P;
1663 case GST_VIDEO_FORMAT_NV12:
1664 fourcc = V4L2_PIX_FMT_NV12;
1665 fourcc_nc = V4L2_PIX_FMT_NV12M;
1667 case GST_VIDEO_FORMAT_NV12_64Z32:
1668 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1670 case GST_VIDEO_FORMAT_NV21:
1671 fourcc = V4L2_PIX_FMT_NV21;
1672 fourcc_nc = V4L2_PIX_FMT_NV21M;
1674 case GST_VIDEO_FORMAT_NV16:
1675 fourcc = V4L2_PIX_FMT_NV16;
1676 fourcc_nc = V4L2_PIX_FMT_NV16M;
1678 case GST_VIDEO_FORMAT_NV61:
1679 fourcc = V4L2_PIX_FMT_NV61;
1680 fourcc_nc = V4L2_PIX_FMT_NV61M;
1682 case GST_VIDEO_FORMAT_NV24:
1683 fourcc = V4L2_PIX_FMT_NV24;
1685 case GST_VIDEO_FORMAT_YVYU:
1686 fourcc = V4L2_PIX_FMT_YVYU;
1688 case GST_VIDEO_FORMAT_RGB15:
1689 fourcc = V4L2_PIX_FMT_RGB555;
1690 fourcc_nc = V4L2_PIX_FMT_XRGB555;
1692 case GST_VIDEO_FORMAT_RGB16:
1693 fourcc = V4L2_PIX_FMT_RGB565;
1695 case GST_VIDEO_FORMAT_RGB:
1696 fourcc = V4L2_PIX_FMT_RGB24;
1698 case GST_VIDEO_FORMAT_BGR:
1699 fourcc = V4L2_PIX_FMT_BGR24;
1701 case GST_VIDEO_FORMAT_xRGB:
1702 fourcc = V4L2_PIX_FMT_RGB32;
1703 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1705 case GST_VIDEO_FORMAT_ARGB:
1706 fourcc = V4L2_PIX_FMT_RGB32;
1707 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1709 case GST_VIDEO_FORMAT_BGRx:
1710 fourcc = V4L2_PIX_FMT_BGR32;
1711 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1713 case GST_VIDEO_FORMAT_BGRA:
1714 fourcc = V4L2_PIX_FMT_BGR32;
1715 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1717 case GST_VIDEO_FORMAT_GRAY8:
1718 fourcc = V4L2_PIX_FMT_GREY;
1720 case GST_VIDEO_FORMAT_GRAY16_LE:
1721 fourcc = V4L2_PIX_FMT_Y16;
1723 case GST_VIDEO_FORMAT_GRAY16_BE:
1724 fourcc = V4L2_PIX_FMT_Y16_BE;
1726 case GST_VIDEO_FORMAT_INVZ:
1727 fourcc = V4L2_PIX_FMT_INVZ;
1733 if (g_str_equal (mimetype, "video/mpegts")) {
1734 fourcc = V4L2_PIX_FMT_MPEG;
1735 } else if (g_str_equal (mimetype, "video/x-dv")) {
1736 fourcc = V4L2_PIX_FMT_DV;
1737 } else if (g_str_equal (mimetype, "image/jpeg")) {
1738 fourcc = V4L2_PIX_FMT_JPEG;
1739 } else if (g_str_equal (mimetype, "video/mpeg")) {
1741 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1744 fourcc = V4L2_PIX_FMT_MPEG1;
1747 fourcc = V4L2_PIX_FMT_MPEG2;
1750 fourcc = V4L2_PIX_FMT_MPEG4;
1751 fourcc_nc = V4L2_PIX_FMT_XVID;
1757 } else if (g_str_equal (mimetype, "video/x-h263")) {
1758 fourcc = V4L2_PIX_FMT_H263;
1759 } else if (g_str_equal (mimetype, "video/x-h264")) {
1760 const gchar *stream_format =
1761 gst_structure_get_string (structure, "stream-format");
1762 if (g_str_equal (stream_format, "avc"))
1763 fourcc = V4L2_PIX_FMT_H264_NO_SC;
1765 fourcc = V4L2_PIX_FMT_H264;
1766 } else if (g_str_equal (mimetype, "video/x-vp8")) {
1767 fourcc = V4L2_PIX_FMT_VP8;
1768 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1769 const gchar *format = gst_structure_get_string (structure, "format");
1771 if (!g_ascii_strcasecmp (format, "bggr"))
1772 fourcc = V4L2_PIX_FMT_SBGGR8;
1773 else if (!g_ascii_strcasecmp (format, "gbrg"))
1774 fourcc = V4L2_PIX_FMT_SGBRG8;
1775 else if (!g_ascii_strcasecmp (format, "grbg"))
1776 fourcc = V4L2_PIX_FMT_SGRBG8;
1777 else if (!g_ascii_strcasecmp (format, "rggb"))
1778 fourcc = V4L2_PIX_FMT_SRGGB8;
1780 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1781 fourcc = V4L2_PIX_FMT_SN9C10X;
1782 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1783 fourcc = V4L2_PIX_FMT_PWC1;
1784 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1785 fourcc = V4L2_PIX_FMT_PWC2;
1790 /* Prefer the non-contiguous if supported */
1791 v4l2object->prefered_non_contiguous = TRUE;
1794 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
1795 else if (fourcc == 0)
1796 goto unhandled_format;
1799 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1800 v4l2object->prefered_non_contiguous = FALSE;
1804 goto unsupported_format;
1813 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1818 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1823 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1829 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1830 guint32 pixelformat, gint * width, gint * height);
1833 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1835 struct v4l2_cropcap cropcap;
1836 int num = 1, den = 1;
1838 if (!v4l2object->keep_aspect)
1841 if (v4l2object->par) {
1842 num = gst_value_get_fraction_numerator (v4l2object->par);
1843 den = gst_value_get_fraction_denominator (v4l2object->par);
1847 memset (&cropcap, 0, sizeof (cropcap));
1849 cropcap.type = v4l2object->type;
1850 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
1851 goto cropcap_failed;
1853 num = cropcap.pixelaspect.numerator;
1854 den = cropcap.pixelaspect.denominator;
1856 /* Ignore PAR that are 0/0 */
1861 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, num, den,
1866 if (errno != ENOTTY)
1867 GST_WARNING_OBJECT (v4l2object->element,
1868 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
1869 g_strerror (errno));
1873 /* returns TRUE if the value was changed in place, otherwise FALSE */
1875 gst_v4l2src_value_simplify (GValue * val)
1877 /* simplify list of one value to one value */
1878 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
1879 const GValue *list_val;
1880 GValue new_val = G_VALUE_INIT;
1882 list_val = gst_value_list_get_value (val, 0);
1883 g_value_init (&new_val, G_VALUE_TYPE (list_val));
1884 g_value_copy (list_val, &new_val);
1885 g_value_unset (val);
1894 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
1895 GstVideoInterlaceMode * interlace_mode)
1897 /* NB: If you add new return values, please fix mode_strings in
1898 * gst_v4l2_object_add_interlace_mode */
1900 case V4L2_FIELD_ANY:
1902 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
1904 case V4L2_FIELD_NONE:
1905 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1907 case V4L2_FIELD_INTERLACED:
1908 case V4L2_FIELD_INTERLACED_TB:
1909 case V4L2_FIELD_INTERLACED_BT:
1910 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1913 GST_ERROR ("Unknown enum v4l2_field %d", field);
1919 gst_v4l2_object_get_colorspace (struct v4l2_format *fmt,
1920 GstVideoColorimetry * cinfo)
1923 gst_v4l2_object_v4l2fourcc_is_rgb (fmt->fmt.pix.pixelformat);
1924 enum v4l2_colorspace colorspace;
1925 enum v4l2_quantization range;
1926 enum v4l2_ycbcr_encoding matrix;
1927 enum v4l2_xfer_func transfer;
1928 gboolean ret = TRUE;
1930 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
1931 colorspace = fmt->fmt.pix_mp.colorspace;
1932 range = fmt->fmt.pix_mp.quantization;
1933 matrix = fmt->fmt.pix_mp.ycbcr_enc;
1934 transfer = fmt->fmt.pix_mp.xfer_func;
1936 colorspace = fmt->fmt.pix.colorspace;
1937 range = fmt->fmt.pix.quantization;
1938 matrix = fmt->fmt.pix.ycbcr_enc;
1939 transfer = fmt->fmt.pix.xfer_func;
1942 /* First step, set the defaults for each primaries */
1943 switch (colorspace) {
1944 case V4L2_COLORSPACE_SMPTE170M:
1945 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1946 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1947 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1948 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1950 case V4L2_COLORSPACE_REC709:
1951 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1952 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1953 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1954 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1956 case V4L2_COLORSPACE_SRGB:
1957 case V4L2_COLORSPACE_JPEG:
1958 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1959 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1960 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
1961 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1963 case V4L2_COLORSPACE_ADOBERGB:
1964 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1965 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1966 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
1967 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
1969 case V4L2_COLORSPACE_BT2020:
1970 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1971 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1972 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1973 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
1975 case V4L2_COLORSPACE_SMPTE240M:
1976 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1977 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1978 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
1979 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
1981 case V4L2_COLORSPACE_470_SYSTEM_M:
1982 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1983 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1984 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1985 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
1987 case V4L2_COLORSPACE_470_SYSTEM_BG:
1988 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1989 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1990 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1991 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
1993 case V4L2_COLORSPACE_RAW:
1994 /* Explicitly unknown */
1995 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
1996 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
1997 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
1998 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
2001 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
2009 /* Second step, apply any custom variation */
2011 case V4L2_QUANTIZATION_FULL_RANGE:
2012 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2014 case V4L2_QUANTIZATION_LIM_RANGE:
2015 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2017 case V4L2_QUANTIZATION_DEFAULT:
2018 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
2019 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
2020 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2021 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601
2022 || matrix == V4L2_YCBCR_ENC_XV709
2023 || colorspace == V4L2_COLORSPACE_JPEG)
2024 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2026 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2029 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
2030 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2035 case V4L2_YCBCR_ENC_XV601:
2036 case V4L2_YCBCR_ENC_SYCC:
2037 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
2039 case V4L2_YCBCR_ENC_601:
2040 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2042 case V4L2_YCBCR_ENC_XV709:
2043 GST_FIXME ("XV709 not defined, assuming 709");
2045 case V4L2_YCBCR_ENC_709:
2046 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2048 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2049 GST_FIXME ("BT2020 with constant lumma is not defined, assuming BT2020");
2051 case V4L2_YCBCR_ENC_BT2020:
2052 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2054 case V4L2_YCBCR_ENC_SMPTE240M:
2055 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2057 case V4L2_YCBCR_ENC_DEFAULT:
2058 /* nothing, just use defaults for colorspace */
2061 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2062 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2066 /* Set identity matrix for R'G'B' formats to avoid creating
2067 * confusion. This though is cosmetic as it's now properly ignored by
2068 * the video info API and videoconvert. */
2070 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2073 case V4L2_XFER_FUNC_709:
2074 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2076 case V4L2_XFER_FUNC_SRGB:
2077 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2079 case V4L2_XFER_FUNC_ADOBERGB:
2080 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2082 case V4L2_XFER_FUNC_SMPTE240M:
2083 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2085 case V4L2_XFER_FUNC_NONE:
2086 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2088 case V4L2_XFER_FUNC_DEFAULT:
2089 /* nothing, just use defaults for colorspace */
2092 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2093 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2102 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2103 struct v4l2_format *try_fmt)
2105 int fd = v4l2object->video_fd;
2106 struct v4l2_format fmt;
2109 memcpy (&fmt, try_fmt, sizeof (fmt));
2110 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2112 if (r < 0 && errno == ENOTTY) {
2113 /* The driver might not implement TRY_FMT, in which case we will try
2115 if (GST_V4L2_IS_ACTIVE (v4l2object))
2118 memcpy (&fmt, try_fmt, sizeof (fmt));
2119 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2121 memcpy (try_fmt, &fmt, sizeof (fmt));
2125 memcpy (try_fmt, &fmt, sizeof (fmt));
2126 GST_WARNING_OBJECT (v4l2object->element,
2127 "Unable to try format: %s", g_strerror (errno));
2133 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2134 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2136 struct v4l2_format fmt;
2137 GValue interlace_formats = { 0, };
2138 GstVideoInterlaceMode interlace_mode, prev = -1;
2140 const gchar *mode_strings[] = { "progressive",
2145 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2148 if (v4l2object->never_interlaced) {
2149 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2153 g_value_init (&interlace_formats, GST_TYPE_LIST);
2155 /* Try twice - once for NONE, once for INTERLACED. */
2156 memset (&fmt, 0, sizeof (fmt));
2157 fmt.type = v4l2object->type;
2158 fmt.fmt.pix.width = width;
2159 fmt.fmt.pix.height = height;
2160 fmt.fmt.pix.pixelformat = pixelformat;
2161 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2163 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2164 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2165 GValue interlace_enum = { 0, };
2166 g_value_init (&interlace_enum, G_TYPE_STRING);
2167 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2168 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2169 prev = interlace_mode;
2172 memset (&fmt, 0, sizeof (fmt));
2173 fmt.type = v4l2object->type;
2174 fmt.fmt.pix.width = width;
2175 fmt.fmt.pix.height = height;
2176 fmt.fmt.pix.pixelformat = pixelformat;
2177 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2179 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2180 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode) &&
2181 prev != interlace_mode) {
2182 GValue interlace_enum = { 0, };
2183 g_value_init (&interlace_enum, G_TYPE_STRING);
2184 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2185 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2188 if (gst_v4l2src_value_simplify (&interlace_formats)
2189 || gst_value_list_get_size (&interlace_formats) > 0)
2190 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2192 GST_WARNING_OBJECT (v4l2object, "Failed to determine interlace mode");
2198 gst_v4l2_object_fill_colorimetry_list (GValue * list,
2199 GstVideoColorimetry * cinfo)
2201 GValue colorimetry = G_VALUE_INIT;
2204 gboolean found = FALSE;
2206 g_value_init (&colorimetry, G_TYPE_STRING);
2207 g_value_take_string (&colorimetry, gst_video_colorimetry_to_string (cinfo));
2209 /* only insert if no duplicate */
2210 size = gst_value_list_get_size (list);
2211 for (i = 0; i < size; i++) {
2214 tmp = gst_value_list_get_value (list, i);
2215 if (gst_value_compare (&colorimetry, tmp) == GST_VALUE_EQUAL) {
2222 gst_value_list_append_and_take_value (list, &colorimetry);
2224 g_value_unset (&colorimetry);
2228 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2229 guint32 width, guint32 height, guint32 pixelformat)
2231 struct v4l2_format fmt;
2232 GValue list = G_VALUE_INIT;
2233 GstVideoColorimetry cinfo;
2234 enum v4l2_colorspace req_cspace;
2236 memset (&fmt, 0, sizeof (fmt));
2237 fmt.type = v4l2object->type;
2238 fmt.fmt.pix.width = width;
2239 fmt.fmt.pix.height = height;
2240 fmt.fmt.pix.pixelformat = pixelformat;
2242 g_value_init (&list, GST_TYPE_LIST);
2244 /* step 1: get device default colorspace and insert it first as
2245 * it should be the preferred one */
2246 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2247 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2248 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2251 /* step 2: probe all colorspace other than default
2252 * We don't probe all colorspace, range, matrix and transfer combination to
2253 * avoid ioctl flooding which could greatly increase initialization time
2254 * with low-speed devices (UVC...) */
2255 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2256 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++) {
2257 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2258 if (req_cspace == V4L2_COLORSPACE_BT878)
2261 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2262 fmt.fmt.pix_mp.colorspace = req_cspace;
2264 fmt.fmt.pix.colorspace = req_cspace;
2266 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2267 enum v4l2_colorspace colorspace;
2269 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2270 colorspace = fmt.fmt.pix_mp.colorspace;
2272 colorspace = fmt.fmt.pix.colorspace;
2274 if (colorspace == req_cspace) {
2275 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2276 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2281 if (gst_value_list_get_size (&list) > 0)
2282 gst_structure_take_value (s, "colorimetry", &list);
2284 g_value_unset (&list);
2289 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2290 static GstStructure *
2291 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2292 guint32 pixelformat,
2293 guint32 width, guint32 height, const GstStructure * template)
2295 gint fd = v4l2object->video_fd;
2296 struct v4l2_frmivalenum ival;
2299 GValue rates = { 0, };
2301 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2303 ival.pixel_format = pixelformat;
2305 ival.height = height;
2307 GST_LOG_OBJECT (v4l2object->element,
2308 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2309 GST_FOURCC_ARGS (pixelformat));
2311 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2312 * fraction to get framerate */
2313 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2314 goto enum_frameintervals_failed;
2316 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2317 GValue rate = { 0, };
2319 g_value_init (&rates, GST_TYPE_LIST);
2320 g_value_init (&rate, GST_TYPE_FRACTION);
2323 num = ival.discrete.numerator;
2324 denom = ival.discrete.denominator;
2326 if (num > G_MAXINT || denom > G_MAXINT) {
2327 /* let us hope we don't get here... */
2332 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
2335 /* swap to get the framerate */
2336 gst_value_set_fraction (&rate, denom, num);
2337 gst_value_list_append_value (&rates, &rate);
2340 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2341 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2342 GValue min = { 0, };
2343 GValue step = { 0, };
2344 GValue max = { 0, };
2345 gboolean added = FALSE;
2346 guint32 minnum, mindenom;
2347 guint32 maxnum, maxdenom;
2349 g_value_init (&rates, GST_TYPE_LIST);
2351 g_value_init (&min, GST_TYPE_FRACTION);
2352 g_value_init (&step, GST_TYPE_FRACTION);
2353 g_value_init (&max, GST_TYPE_FRACTION);
2356 minnum = ival.stepwise.min.numerator;
2357 mindenom = ival.stepwise.min.denominator;
2358 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2362 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
2364 gst_value_set_fraction (&min, minnum, mindenom);
2367 maxnum = ival.stepwise.max.numerator;
2368 maxdenom = ival.stepwise.max.denominator;
2369 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2374 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
2376 gst_value_set_fraction (&max, maxnum, maxdenom);
2379 num = ival.stepwise.step.numerator;
2380 denom = ival.stepwise.step.denominator;
2381 if (num > G_MAXINT || denom > G_MAXINT) {
2386 if (num == 0 || denom == 0) {
2387 /* in this case we have a wrong fraction or no step, set the step to max
2388 * so that we only add the min value in the loop below */
2393 /* since we only have gst_value_fraction_subtract and not add, negate the
2395 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
2397 gst_value_set_fraction (&step, -num, denom);
2399 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2400 GValue rate = { 0, };
2402 num = gst_value_get_fraction_numerator (&min);
2403 denom = gst_value_get_fraction_denominator (&min);
2404 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
2407 /* invert to get the framerate */
2408 g_value_init (&rate, GST_TYPE_FRACTION);
2409 gst_value_set_fraction (&rate, denom, num);
2410 gst_value_list_append_value (&rates, &rate);
2413 /* we're actually adding because step was negated above. This is because
2414 * there is no _add function... */
2415 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2416 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
2421 /* no range was added, leave the default range from the template */
2422 GST_WARNING_OBJECT (v4l2object->element,
2423 "no range added, leaving default");
2424 g_value_unset (&rates);
2426 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2427 guint32 maxnum, maxdenom;
2429 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2431 num = ival.stepwise.min.numerator;
2432 denom = ival.stepwise.min.denominator;
2433 if (num > G_MAXINT || denom > G_MAXINT) {
2438 maxnum = ival.stepwise.max.numerator;
2439 maxdenom = ival.stepwise.max.denominator;
2440 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2445 GST_LOG_OBJECT (v4l2object->element,
2446 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2449 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2455 s = gst_structure_copy (template);
2456 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2457 "height", G_TYPE_INT, (gint) height, NULL);
2458 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2459 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2461 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2463 if (G_IS_VALUE (&rates)) {
2464 gst_v4l2src_value_simplify (&rates);
2465 /* only change the framerate on the template when we have a valid probed new
2467 gst_structure_take_value (s, "framerate", &rates);
2468 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2469 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2470 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2476 enum_frameintervals_failed:
2478 GST_DEBUG_OBJECT (v4l2object->element,
2479 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2480 GST_FOURCC_ARGS (pixelformat), width, height);
2485 /* I don't see how this is actually an error, we ignore the format then */
2486 GST_WARNING_OBJECT (v4l2object->element,
2487 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2488 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2494 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2498 gst_structure_get_int (s1, "width", &w1);
2499 gst_structure_get_int (s1, "height", &h1);
2500 gst_structure_get_int (s2, "width", &w2);
2501 gst_structure_get_int (s2, "height", &h2);
2503 /* I think it's safe to assume that this won't overflow for a while */
2504 return ((w2 * h2) - (w1 * h1));
2508 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2509 guint32 format, GstCaps * caps, GstStructure * s)
2511 GstStructure *alt_s = NULL;
2513 /* Encoded stream on output buffer need to be parsed */
2514 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2515 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2518 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2519 if (format == gst_v4l2_formats[i].format &&
2520 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2521 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2522 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2528 if (v4l2object->has_alpha_component &&
2529 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2530 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2532 case V4L2_PIX_FMT_RGB32:
2533 alt_s = gst_structure_copy (s);
2534 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2536 case V4L2_PIX_FMT_BGR32:
2537 alt_s = gst_structure_copy (s);
2538 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2545 gst_caps_append_structure (caps, s);
2548 gst_caps_append_structure (caps, alt_s);
2552 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2553 guint32 pixelformat, const GstStructure * template)
2555 GstCaps *ret = gst_caps_new_empty ();
2557 gint fd = v4l2object->video_fd;
2558 struct v4l2_frmsizeenum size;
2559 GList *results = NULL;
2562 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2563 gst_caps_append_structure (ret, gst_structure_copy (template));
2567 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2569 size.pixel_format = pixelformat;
2571 GST_DEBUG_OBJECT (v4l2object->element,
2572 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2573 GST_FOURCC_ARGS (pixelformat));
2575 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2576 goto enum_framesizes_failed;
2578 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2580 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
2581 size.discrete.width, size.discrete.height);
2583 w = MIN (size.discrete.width, G_MAXINT);
2584 h = MIN (size.discrete.height, G_MAXINT);
2588 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2589 pixelformat, w, h, template);
2592 results = g_list_prepend (results, tmp);
2596 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2597 GST_DEBUG_OBJECT (v4l2object->element,
2598 "done iterating discrete frame sizes");
2599 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2600 guint32 maxw, maxh, step_w, step_h;
2602 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
2603 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2604 size.stepwise.min_width);
2605 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2606 size.stepwise.min_height);
2607 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2608 size.stepwise.max_width);
2609 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2610 size.stepwise.max_height);
2611 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
2612 size.stepwise.step_width);
2613 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
2614 size.stepwise.step_height);
2616 w = MAX (size.stepwise.min_width, 1);
2617 h = MAX (size.stepwise.min_height, 1);
2618 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2619 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2621 step_w = MAX (size.stepwise.step_width, 1);
2622 step_h = MAX (size.stepwise.step_height, 1);
2624 /* FIXME: check for sanity and that min/max are multiples of the steps */
2626 /* we only query details for the max width/height since it's likely the
2627 * most restricted if there are any resolution-dependent restrictions */
2628 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2629 pixelformat, maxw, maxh, template);
2632 GValue step_range = G_VALUE_INIT;
2634 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2635 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2636 gst_structure_set_value (tmp, "width", &step_range);
2638 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2639 gst_structure_take_value (tmp, "height", &step_range);
2641 /* no point using the results list here, since there's only one struct */
2642 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2644 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2647 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
2648 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2649 size.stepwise.min_width);
2650 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2651 size.stepwise.min_height);
2652 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2653 size.stepwise.max_width);
2654 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2655 size.stepwise.max_height);
2657 w = MAX (size.stepwise.min_width, 1);
2658 h = MAX (size.stepwise.min_height, 1);
2659 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2660 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2663 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2666 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2667 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2670 /* no point using the results list here, since there's only one struct */
2671 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2677 /* we use an intermediary list to store and then sort the results of the
2678 * probing because we can't make any assumptions about the order in which
2679 * the driver will give us the sizes, but we want the final caps to contain
2680 * the results starting with the highest resolution and having the lowest
2681 * resolution last, since order in caps matters for things like fixation. */
2682 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2683 while (results != NULL) {
2684 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
2686 results = g_list_delete_link (results, results);
2689 if (gst_caps_is_empty (ret))
2690 goto enum_framesizes_no_results;
2695 enum_framesizes_failed:
2697 /* I don't see how this is actually an error */
2698 GST_DEBUG_OBJECT (v4l2object->element,
2699 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2700 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2701 goto default_frame_sizes;
2703 enum_framesizes_no_results:
2705 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2706 * question doesn't actually support it yet */
2707 GST_DEBUG_OBJECT (v4l2object->element,
2708 "No results for pixelformat %" GST_FOURCC_FORMAT
2709 " enumerating frame sizes, trying fallback",
2710 GST_FOURCC_ARGS (pixelformat));
2711 goto default_frame_sizes;
2715 GST_WARNING_OBJECT (v4l2object->element,
2716 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2717 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2718 goto default_frame_sizes;
2721 default_frame_sizes:
2723 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2725 /* This code is for Linux < 2.6.19 */
2727 max_w = max_h = GST_V4L2_MAX_SIZE;
2728 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2730 GST_WARNING_OBJECT (v4l2object->element,
2731 "Could not probe minimum capture size for pixelformat %"
2732 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2734 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2736 GST_WARNING_OBJECT (v4l2object->element,
2737 "Could not probe maximum capture size for pixelformat %"
2738 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2741 /* Since we can't get framerate directly, try to use the current norm */
2742 if (v4l2object->tv_norm && v4l2object->norms) {
2744 GstTunerNorm *norm = NULL;
2745 GstTunerNorm *current =
2746 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2748 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2749 norm = (GstTunerNorm *) norms->data;
2750 if (!strcmp (norm->label, current->label))
2753 /* If it's possible, set framerate to that (discrete) value */
2755 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2756 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2760 tmp = gst_structure_copy (template);
2762 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2764 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2765 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2766 /* if norm can't be used, copy the template framerate */
2767 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2772 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2774 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2777 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2779 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2781 /* We could consider setting interlace mode from min and max. */
2782 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
2784 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2785 /* We could consider to check colorspace for min too, in case it depends on
2786 * the size. But in this case, min and max could not be enough */
2787 gst_v4l2_object_add_colorspace (v4l2object, tmp, max_w, max_h, pixelformat);
2789 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2795 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2796 guint32 pixelformat, gint * width, gint * height)
2798 struct v4l2_format fmt;
2799 gboolean ret = FALSE;
2800 GstVideoInterlaceMode interlace_mode;
2802 g_return_val_if_fail (width != NULL, FALSE);
2803 g_return_val_if_fail (height != NULL, FALSE);
2805 GST_LOG_OBJECT (v4l2object->element,
2806 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2807 *width, *height, GST_FOURCC_ARGS (pixelformat));
2809 memset (&fmt, 0, sizeof (struct v4l2_format));
2811 /* get size delimiters */
2812 memset (&fmt, 0, sizeof (fmt));
2813 fmt.type = v4l2object->type;
2814 fmt.fmt.pix.width = *width;
2815 fmt.fmt.pix.height = *height;
2816 fmt.fmt.pix.pixelformat = pixelformat;
2817 fmt.fmt.pix.field = V4L2_FIELD_ANY;
2819 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
2822 GST_LOG_OBJECT (v4l2object->element,
2823 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2825 *width = fmt.fmt.pix.width;
2826 *height = fmt.fmt.pix.height;
2828 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2829 GST_WARNING_OBJECT (v4l2object->element,
2830 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
2831 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
2839 GST_WARNING_OBJECT (v4l2object->element,
2840 "Unable to try format: %s", g_strerror (errno));
2847 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2851 GST_DEBUG_OBJECT (v4l2object->element, "initializing the %s system",
2852 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
2854 GST_V4L2_CHECK_OPEN (v4l2object);
2855 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2857 /* find transport */
2858 mode = v4l2object->req_mode;
2860 if (v4l2object->device_caps & V4L2_CAP_READWRITE) {
2861 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2862 mode = GST_V4L2_IO_RW;
2863 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2864 goto method_not_supported;
2866 if (v4l2object->device_caps & V4L2_CAP_STREAMING) {
2867 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2868 mode = GST_V4L2_IO_MMAP;
2869 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2870 goto method_not_supported;
2872 /* if still no transport selected, error out */
2873 if (mode == GST_V4L2_IO_AUTO)
2874 goto no_supported_capture_method;
2876 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2877 v4l2object->mode = mode;
2879 /* If min_buffers is not set, the driver either does not support the control or
2880 it has not been asked yet via propose_allocation/decide_allocation. */
2881 if (!v4l2object->min_buffers)
2882 gst_v4l2_get_driver_min_buffers (v4l2object);
2884 /* Map the buffers */
2885 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2887 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2888 goto buffer_pool_new_failed;
2890 GST_V4L2_SET_ACTIVE (v4l2object);
2895 buffer_pool_new_failed:
2897 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2898 (_("Could not map buffers from device '%s'"),
2899 v4l2object->videodev),
2900 ("Failed to create buffer pool: %s", g_strerror (errno)));
2903 method_not_supported:
2905 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2906 (_("The driver of device '%s' does not support the IO method %d"),
2907 v4l2object->videodev, mode), (NULL));
2910 no_supported_capture_method:
2912 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2913 (_("The driver of device '%s' does not support any known IO "
2914 "method."), v4l2object->videodev), (NULL));
2920 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
2921 gint plane, gint stride)
2923 const GstVideoFormatInfo *finfo = info->finfo;
2925 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2926 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
2929 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
2930 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2931 tile_height = 1 << hs;
2933 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
2934 info->height + align->padding_top + align->padding_bottom);
2935 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
2937 x_tiles = stride >> ws;
2938 y_tiles = padded_height >> hs;
2939 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
2941 info->stride[plane] = stride;
2946 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
2947 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
2949 const GstVideoFormatInfo *finfo = info->finfo;
2950 gint i, estride, padded_height;
2953 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
2955 padded_height = info->height + align->padding_top + align->padding_bottom;
2957 for (i = 0; i < finfo->n_planes; i++) {
2958 estride = gst_v4l2_object_extrapolate_stride (finfo, i, stride);
2960 gst_v4l2_object_set_stride (info, align, i, estride);
2962 info->offset[i] = offs;
2964 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
2966 GST_DEBUG_OBJECT (v4l2object->element,
2967 "Extrapolated for plane %d with base stride %d: "
2968 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
2972 /* Update the image size according the amount of data we are going to
2973 * read/write. This workaround bugs in driver where the sizeimage provided
2974 * by TRY/S_FMT represent the buffer length (maximum size) rather then the expected
2975 * bytesused (buffer size). */
2976 if (offs < info->size)
2981 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
2982 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
2983 GstVideoInfo * info, GstVideoAlignment * align)
2985 const GstVideoFormatInfo *finfo = info->finfo;
2986 gboolean standard_stride = TRUE;
2987 gint stride, padded_width, padded_height, i;
2989 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
2990 v4l2object->n_v4l2_planes = 1;
2991 info->size = format->fmt.pix.sizeimage;
2995 /* adjust right padding */
2996 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2997 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
2999 stride = format->fmt.pix.bytesperline;
3001 padded_width = stride / GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
3003 if (padded_width < format->fmt.pix.width)
3004 GST_WARNING_OBJECT (v4l2object->element,
3005 "Driver bug detected, stride is too small for the width");
3007 align->padding_right = padded_width - info->width - align->padding_left;
3009 /* adjust bottom padding */
3010 padded_height = format->fmt.pix.height;
3012 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3013 guint hs, tile_height;
3015 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
3016 tile_height = 1 << hs;
3018 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3021 align->padding_bottom = padded_height - info->height - align->padding_top;
3023 /* setup the strides and offset */
3024 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
3025 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
3027 /* figure out the frame layout */
3028 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
3030 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
3031 stride = pix_mp->plane_fmt[i].bytesperline;
3033 if (info->stride[i] != stride)
3034 standard_stride = FALSE;
3036 gst_v4l2_object_set_stride (info, align, i, stride);
3037 info->offset[i] = info->size;
3038 info->size += pix_mp->plane_fmt[i].sizeimage;
3041 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3042 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
3043 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3044 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3047 /* only one plane in non-MPLANE mode */
3048 v4l2object->n_v4l2_planes = 1;
3049 info->size = format->fmt.pix.sizeimage;
3050 stride = format->fmt.pix.bytesperline;
3052 if (info->stride[0] != stride)
3053 standard_stride = FALSE;
3055 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3058 /* adjust the offset to take into account left and top */
3059 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3060 if ((align->padding_left + align->padding_top) > 0)
3061 GST_WARNING_OBJECT (v4l2object->element,
3062 "Left and top padding is not permitted for tiled formats");
3064 for (i = 0; i < finfo->n_planes; i++) {
3067 /* FIXME we assume plane as component as this is true for all supported
3068 * format we support. */
3070 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left);
3071 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top);
3073 info->offset[i] += (vedge * info->stride[i]) +
3074 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i));
3079 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %" G_GSIZE_FORMAT,
3082 /* to avoid copies we need video meta if there is padding */
3083 v4l2object->need_video_meta =
3084 ((align->padding_top + align->padding_left + align->padding_right +
3085 align->padding_bottom) != 0);
3087 /* ... or if stride is non "standard" */
3088 if (!standard_stride)
3089 v4l2object->need_video_meta = TRUE;
3091 /* ... or also video meta if we use multiple, non-contiguous, planes */
3092 if (v4l2object->n_v4l2_planes > 1)
3093 v4l2object->need_video_meta = TRUE;
3095 v4l2object->info = *info;
3096 v4l2object->align = *align;
3097 v4l2object->format = *format;
3098 v4l2object->fmtdesc = fmtdesc;
3100 /* if we have a framerate pre-calculate duration */
3101 if (info->fps_n > 0 && info->fps_d > 0) {
3102 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
3105 v4l2object->duration = GST_CLOCK_TIME_NONE;
3110 gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
3111 gint plane, gint stride)
3115 switch (finfo->format) {
3116 case GST_VIDEO_FORMAT_NV12:
3117 case GST_VIDEO_FORMAT_NV12_64Z32:
3118 case GST_VIDEO_FORMAT_NV21:
3119 case GST_VIDEO_FORMAT_NV16:
3120 case GST_VIDEO_FORMAT_NV61:
3121 case GST_VIDEO_FORMAT_NV24:
3122 estride = (plane == 0 ? 1 : 2) *
3123 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3126 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3134 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3135 gboolean try_only, GstV4l2Error * error)
3137 gint fd = v4l2object->video_fd;
3138 struct v4l2_format format;
3139 struct v4l2_streamparm streamparm;
3140 enum v4l2_field field;
3141 guint32 pixelformat;
3142 struct v4l2_fmtdesc *fmtdesc;
3144 GstVideoAlignment align;
3145 gint width, height, fps_n, fps_d;
3149 enum v4l2_colorspace colorspace = 0;
3150 enum v4l2_quantization range = 0;
3151 enum v4l2_ycbcr_encoding matrix = 0;
3152 enum v4l2_xfer_func transfer = 0;
3154 GST_V4L2_CHECK_OPEN (v4l2object);
3156 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3158 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3160 gst_video_info_init (&info);
3161 gst_video_alignment_reset (&align);
3163 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3166 pixelformat = fmtdesc->pixelformat;
3167 width = GST_VIDEO_INFO_WIDTH (&info);
3168 height = GST_VIDEO_INFO_HEIGHT (&info);
3169 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3170 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3172 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3173 * or if contiguous is prefered */
3174 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3175 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3178 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
3179 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
3180 /* ideally we would differentiate between types of interlaced video
3181 * but there is not sufficient information in the caps..
3183 field = V4L2_FIELD_INTERLACED;
3185 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
3186 field = V4L2_FIELD_NONE;
3189 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3190 /* We first pick th main colorspace from the primaries */
3191 switch (info.colorimetry.primaries) {
3192 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3193 /* There is two colorspaces using these primaries, use the range to
3195 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3196 colorspace = V4L2_COLORSPACE_REC709;
3198 colorspace = V4L2_COLORSPACE_SRGB;
3200 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3201 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3203 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3204 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3206 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3207 colorspace = V4L2_COLORSPACE_SMPTE170M;
3209 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3210 colorspace = V4L2_COLORSPACE_SMPTE240M;
3213 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3214 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3215 /* We don't know, we will guess */
3219 GST_WARNING_OBJECT (v4l2object->element,
3220 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3224 switch (info.colorimetry.range) {
3225 case GST_VIDEO_COLOR_RANGE_0_255:
3226 range = V4L2_QUANTIZATION_FULL_RANGE;
3228 case GST_VIDEO_COLOR_RANGE_16_235:
3229 range = V4L2_QUANTIZATION_LIM_RANGE;
3231 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3232 /* We let the driver pick a default one */
3235 GST_WARNING_OBJECT (v4l2object->element,
3236 "Unknown colorimetry range %d", info.colorimetry.range);
3240 switch (info.colorimetry.matrix) {
3241 case GST_VIDEO_COLOR_MATRIX_RGB:
3242 /* Unspecified, leave to default */
3244 /* FCC is about the same as BT601 with less digit */
3245 case GST_VIDEO_COLOR_MATRIX_FCC:
3246 case GST_VIDEO_COLOR_MATRIX_BT601:
3247 matrix = V4L2_YCBCR_ENC_601;
3249 case GST_VIDEO_COLOR_MATRIX_BT709:
3250 matrix = V4L2_YCBCR_ENC_709;
3252 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3253 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3255 case GST_VIDEO_COLOR_MATRIX_BT2020:
3256 matrix = V4L2_YCBCR_ENC_BT2020;
3258 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3259 /* We let the driver pick a default one */
3262 GST_WARNING_OBJECT (v4l2object->element,
3263 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3267 switch (info.colorimetry.transfer) {
3268 case GST_VIDEO_TRANSFER_GAMMA18:
3269 case GST_VIDEO_TRANSFER_GAMMA20:
3270 case GST_VIDEO_TRANSFER_GAMMA22:
3271 case GST_VIDEO_TRANSFER_GAMMA28:
3272 GST_WARNING_OBJECT (v4l2object->element,
3273 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3275 case GST_VIDEO_TRANSFER_GAMMA10:
3276 transfer = V4L2_XFER_FUNC_NONE;
3278 case GST_VIDEO_TRANSFER_BT709:
3279 transfer = V4L2_XFER_FUNC_709;
3281 case GST_VIDEO_TRANSFER_SMPTE240M:
3282 transfer = V4L2_XFER_FUNC_SMPTE240M;
3284 case GST_VIDEO_TRANSFER_SRGB:
3285 transfer = V4L2_XFER_FUNC_SRGB;
3287 case GST_VIDEO_TRANSFER_LOG100:
3288 case GST_VIDEO_TRANSFER_LOG316:
3289 GST_WARNING_OBJECT (v4l2object->element,
3290 "LOG 100, 316 transfer functions not supported");
3291 /* FIXME No known sensible default, maybe AdobeRGB ? */
3293 case GST_VIDEO_TRANSFER_UNKNOWN:
3294 /* We let the driver pick a default one */
3297 GST_WARNING_OBJECT (v4l2object->element,
3298 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
3302 if (colorspace == 0) {
3303 /* Try to guess colorspace according to pixelformat and size */
3304 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3305 /* SD streams likely use SMPTE170M and HD streams REC709 */
3306 if (width <= 720 && height <= 576)
3307 colorspace = V4L2_COLORSPACE_SMPTE170M;
3309 colorspace = V4L2_COLORSPACE_REC709;
3310 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3311 colorspace = V4L2_COLORSPACE_SRGB;
3312 transfer = V4L2_XFER_FUNC_NONE;
3317 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
3318 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3319 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3321 memset (&format, 0x00, sizeof (struct v4l2_format));
3322 format.type = v4l2object->type;
3325 format.type = v4l2object->type;
3326 format.fmt.pix_mp.pixelformat = pixelformat;
3327 format.fmt.pix_mp.width = width;
3328 format.fmt.pix_mp.height = height;
3329 format.fmt.pix_mp.field = field;
3330 format.fmt.pix_mp.num_planes = n_v4l_planes;
3332 /* try to ask our prefered stride but it's not a failure if not
3334 for (i = 0; i < n_v4l_planes; i++) {
3335 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3337 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3338 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3339 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3341 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3344 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3345 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3347 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3349 format.type = v4l2object->type;
3350 format.fmt.pix.width = width;
3351 format.fmt.pix.height = height;
3352 format.fmt.pix.pixelformat = pixelformat;
3353 format.fmt.pix.field = field;
3355 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3356 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3357 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3359 /* try to ask our prefered stride */
3360 format.fmt.pix.bytesperline = stride;
3362 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3363 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3366 GST_DEBUG_OBJECT (v4l2object->element, "Desired format is %dx%d, format "
3367 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3368 format.fmt.pix_mp.height,
3369 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3370 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3372 #ifndef GST_DISABLE_GST_DEBUG
3374 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3375 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3376 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3378 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3379 format.fmt.pix.bytesperline);
3383 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3385 format.fmt.pix_mp.colorspace = colorspace;
3386 format.fmt.pix_mp.quantization = range;
3387 format.fmt.pix_mp.ycbcr_enc = matrix;
3388 format.fmt.pix_mp.xfer_func = transfer;
3390 format.fmt.pix.colorspace = colorspace;
3391 format.fmt.pix.quantization = range;
3392 format.fmt.pix.ycbcr_enc = matrix;
3393 format.fmt.pix.xfer_func = transfer;
3396 GST_DEBUG_OBJECT (v4l2object->element, "Desired colorspace is %d:%d:%d:%d",
3397 colorspace, range, matrix, transfer);
3401 if (v4l2_ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3402 goto try_fmt_failed;
3404 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3405 goto set_fmt_failed;
3408 GST_DEBUG_OBJECT (v4l2object->element, "Got format of %dx%d, format "
3409 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d",
3410 format.fmt.pix.width, format.fmt.pix_mp.height,
3411 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3412 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3413 is_mplane ? format.fmt.pix_mp.colorspace : format.fmt.pix.colorspace);
3415 #ifndef GST_DISABLE_GST_DEBUG
3417 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3418 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3419 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3420 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3422 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3423 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3427 if (format.fmt.pix.pixelformat != pixelformat)
3428 goto invalid_pixelformat;
3430 /* Only negotiate size with raw data.
3431 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3432 * in ASF mode for example, there is also not reason for a driver to
3433 * change the size. */
3434 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3435 /* We can crop larger images */
3436 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3437 goto invalid_dimensions;
3439 /* Note, this will be adjusted if upstream has non-centered cropping. */
3440 align.padding_top = 0;
3441 align.padding_bottom = format.fmt.pix.height - height;
3442 align.padding_left = 0;
3443 align.padding_right = format.fmt.pix.width - width;
3446 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3447 goto invalid_planes;
3449 if (try_only) /* good enough for trying only */
3452 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
3453 struct v4l2_control ctl = { 0, };
3454 ctl.id = V4L2_CID_ALPHA_COMPONENT;
3457 if (v4l2_ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
3458 GST_WARNING_OBJECT (v4l2object->element,
3459 "Failed to set alpha component value");
3462 /* Is there a reason we require the caller to always specify a framerate? */
3463 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
3466 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
3467 streamparm.type = v4l2object->type;
3469 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
3470 goto get_parm_failed;
3472 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
3473 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3474 GST_VIDEO_INFO_FPS_N (&info) =
3475 streamparm.parm.capture.timeperframe.denominator;
3476 GST_VIDEO_INFO_FPS_D (&info) =
3477 streamparm.parm.capture.timeperframe.numerator;
3479 GST_DEBUG_OBJECT (v4l2object->element, "Got capture framerate: %u/%u",
3480 streamparm.parm.capture.timeperframe.denominator,
3481 streamparm.parm.capture.timeperframe.numerator);
3483 /* We used to skip frame rate setup if the camera was already setup
3484 * with the requested frame rate. This breaks some cameras though,
3485 * causing them to not output data (several models of Thinkpad cameras
3486 * have this problem at least).
3487 * So, don't skip. */
3488 GST_LOG_OBJECT (v4l2object->element, "Setting capture framerate to %u/%u",
3490 /* We want to change the frame rate, so check whether we can. Some cheap USB
3491 * cameras don't have the capability */
3492 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3493 GST_DEBUG_OBJECT (v4l2object->element,
3494 "Not setting capture framerate (not supported)");
3498 /* Note: V4L2 wants the frame interval, we have the frame rate */
3499 streamparm.parm.capture.timeperframe.numerator = fps_d;
3500 streamparm.parm.capture.timeperframe.denominator = fps_n;
3502 /* some cheap USB cam's won't accept any change */
3503 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3504 goto set_parm_failed;
3506 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
3507 streamparm.parm.capture.timeperframe.denominator > 0) {
3508 /* get new values */
3509 fps_d = streamparm.parm.capture.timeperframe.numerator;
3510 fps_n = streamparm.parm.capture.timeperframe.denominator;
3512 GST_INFO_OBJECT (v4l2object->element, "Set capture framerate to %u/%u",
3515 /* fix v4l2 capture driver to provide framerate values */
3516 GST_WARNING_OBJECT (v4l2object->element,
3517 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
3520 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3521 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3522 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
3523 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
3524 GST_VIDEO_INFO_FPS_N (&info) =
3525 streamparm.parm.output.timeperframe.denominator;
3526 GST_VIDEO_INFO_FPS_D (&info) =
3527 streamparm.parm.output.timeperframe.numerator;
3529 GST_DEBUG_OBJECT (v4l2object->element, "Got output framerate: %u/%u",
3530 streamparm.parm.output.timeperframe.denominator,
3531 streamparm.parm.output.timeperframe.numerator);
3533 GST_LOG_OBJECT (v4l2object->element, "Setting output framerate to %u/%u",
3535 if ((streamparm.parm.output.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3536 GST_DEBUG_OBJECT (v4l2object->element,
3537 "Not setting output framerate (not supported)");
3541 /* Note: V4L2 wants the frame interval, we have the frame rate */
3542 streamparm.parm.output.timeperframe.numerator = fps_d;
3543 streamparm.parm.output.timeperframe.denominator = fps_n;
3545 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3546 goto set_parm_failed;
3548 if (streamparm.parm.output.timeperframe.numerator > 0 &&
3549 streamparm.parm.output.timeperframe.denominator > 0) {
3550 /* get new values */
3551 fps_d = streamparm.parm.output.timeperframe.numerator;
3552 fps_n = streamparm.parm.output.timeperframe.denominator;
3554 GST_INFO_OBJECT (v4l2object->element, "Set output framerate to %u/%u",
3557 /* fix v4l2 output driver to provide framerate values */
3558 GST_WARNING_OBJECT (v4l2object->element,
3559 "Reuse caps framerate %u/%u - fix v4l2 output driver", fps_n, fps_d);
3562 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3563 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3567 /* add boolean return, so we can fail on drivers bugs */
3568 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
3570 /* now configure the pool */
3571 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
3579 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
3585 if (errno == EBUSY) {
3586 GST_V4L2_ERROR (error, RESOURCE, BUSY,
3587 (_("Device '%s' is busy"), v4l2object->videodev),
3588 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3589 GST_FOURCC_ARGS (pixelformat), width, height,
3590 g_strerror (errno)));
3596 if (errno == EBUSY) {
3597 GST_V4L2_ERROR (error, RESOURCE, BUSY,
3598 (_("Device '%s' is busy"), v4l2object->videodev),
3599 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3600 GST_FOURCC_ARGS (pixelformat), width, height,
3601 g_strerror (errno)));
3603 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3604 (_("Device '%s' cannot capture at %dx%d"),
3605 v4l2object->videodev, width, height),
3606 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3607 GST_FOURCC_ARGS (pixelformat), width, height,
3608 g_strerror (errno)));
3615 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3616 (_("Device '%s' cannot capture at %dx%d"),
3617 v4l2object->videodev, width, height),
3618 ("Tried to capture at %dx%d, but device returned size %dx%d",
3619 width, height, format.fmt.pix.width, format.fmt.pix.height));
3623 invalid_pixelformat:
3626 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3627 (_("Device '%s' cannot capture in the specified format"),
3628 v4l2object->videodev),
3629 ("Tried to capture in %" GST_FOURCC_FORMAT
3630 ", but device returned format" " %" GST_FOURCC_FORMAT,
3631 GST_FOURCC_ARGS (pixelformat),
3632 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
3639 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3640 (_("Device '%s' does support non-contiguous planes"),
3641 v4l2object->videodev),
3642 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
3648 /* it's possible that this call is not supported */
3649 if (errno != EINVAL && errno != ENOTTY) {
3650 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3651 (_("Could not get parameters on device '%s'"),
3652 v4l2object->videodev), GST_ERROR_SYSTEM);
3658 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3659 (_("Video device did not accept new frame rate setting.")),
3665 /* setup_pool already send the error */
3671 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps,
3672 GstV4l2Error * error)
3674 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE, error);
3678 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps,
3679 GstV4l2Error * error)
3681 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE, error);
3685 * gst_v4l2_object_acquire_format:
3686 * @v4l2object the object
3687 * @info a GstVideoInfo to be filled
3689 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
3690 * the output format is choosen by the HW.
3692 * Returns: %TRUE on success, %FALSE on failure.
3695 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
3697 struct v4l2_fmtdesc *fmtdesc;
3698 struct v4l2_format fmt;
3699 struct v4l2_crop crop;
3700 struct v4l2_selection sel;
3701 struct v4l2_rect *r = NULL;
3702 GstVideoFormat format;
3703 guint width, height;
3704 GstVideoAlignment align;
3706 gst_video_info_init (info);
3707 gst_video_alignment_reset (&align);
3709 memset (&fmt, 0x00, sizeof (struct v4l2_format));
3710 fmt.type = v4l2object->type;
3711 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
3712 goto get_fmt_failed;
3714 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
3715 fmt.fmt.pix.pixelformat);
3716 if (fmtdesc == NULL)
3717 goto unsupported_format;
3719 /* No need to care about mplane, the four first params are the same */
3720 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
3722 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
3723 if (format == GST_VIDEO_FORMAT_UNKNOWN)
3724 goto unsupported_format;
3726 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
3727 goto invalid_dimensions;
3729 width = fmt.fmt.pix.width;
3730 height = fmt.fmt.pix.height;
3732 /* Use the default compose rectangle */
3733 memset (&sel, 0, sizeof (struct v4l2_selection));
3734 sel.type = v4l2object->type;
3735 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
3736 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0) {
3739 /* For ancient kernels, fall back to G_CROP */
3740 memset (&crop, 0, sizeof (struct v4l2_crop));
3741 crop.type = v4l2object->type;
3742 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
3746 align.padding_left = r->left;
3747 align.padding_top = r->top;
3748 align.padding_right = width - r->width - r->left;
3749 align.padding_bottom = height - r->height - r->top;
3754 gst_video_info_set_format (info, format, width, height);
3756 switch (fmt.fmt.pix.field) {
3757 case V4L2_FIELD_ANY:
3758 case V4L2_FIELD_NONE:
3759 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
3761 case V4L2_FIELD_INTERLACED:
3762 case V4L2_FIELD_INTERLACED_TB:
3763 case V4L2_FIELD_INTERLACED_BT:
3764 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
3767 goto unsupported_field;
3770 gst_v4l2_object_get_colorspace (&fmt, &info->colorimetry);
3772 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
3774 /* Shall we setup the pool ? */
3780 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3781 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
3786 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3787 (_("Video device returned invalid dimensions.")),
3788 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
3789 fmt.fmt.pix.height));
3794 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3795 (_("Video device uses an unsupported interlacing method.")),
3796 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
3801 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3802 (_("Video device uses an unsupported pixel format.")),
3803 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
3804 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
3810 gst_v4l2_object_set_crop (GstV4l2Object * obj)
3812 struct v4l2_crop crop = { 0 };
3814 crop.type = obj->type;
3815 crop.c.left = obj->align.padding_left;
3816 crop.c.top = obj->align.padding_top;
3817 crop.c.width = obj->info.width;
3818 crop.c.height = obj->info.height;
3820 if (obj->align.padding_left + obj->align.padding_top +
3821 obj->align.padding_right + obj->align.padding_bottom == 0) {
3822 GST_DEBUG_OBJECT (obj->element, "no cropping needed");
3826 GST_DEBUG_OBJECT (obj->element,
3827 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3828 crop.c.width, crop.c.height);
3830 if (v4l2_ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
3831 GST_WARNING_OBJECT (obj->element, "VIDIOC_S_CROP failed");
3835 if (v4l2_ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
3836 GST_WARNING_OBJECT (obj->element, "VIDIOC_G_CROP failed");
3840 GST_DEBUG_OBJECT (obj->element,
3841 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3842 crop.c.width, crop.c.height);
3848 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
3850 GstStructure *config;
3854 if (!v4l2object->pool)
3857 config = gst_buffer_pool_get_config (v4l2object->pool);
3858 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
3860 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
3862 gst_structure_free (config);
3868 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
3870 gboolean ret = TRUE;
3872 GST_LOG_OBJECT (v4l2object->element, "start flushing");
3874 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3875 gst_buffer_pool_set_flushing (v4l2object->pool, TRUE);
3881 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
3883 gboolean ret = TRUE;
3885 GST_LOG_OBJECT (v4l2object->element, "stop flushing");
3887 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3888 gst_buffer_pool_set_flushing (v4l2object->pool, FALSE);
3894 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
3896 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
3898 if (!GST_V4L2_IS_OPEN (v4l2object))
3900 if (!GST_V4L2_IS_ACTIVE (v4l2object))
3903 if (v4l2object->pool) {
3904 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
3905 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
3906 gst_object_unref (v4l2object->pool);
3907 v4l2object->pool = NULL;
3910 GST_V4L2_SET_INACTIVE (v4l2object);
3917 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3923 formats = gst_v4l2_object_get_format_list (v4l2object);
3925 ret = gst_caps_new_empty ();
3927 for (walk = formats; walk; walk = walk->next) {
3928 struct v4l2_fmtdesc *format;
3929 GstStructure *template;
3931 format = (struct v4l2_fmtdesc *) walk->data;
3933 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
3938 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
3939 format->pixelformat, template);
3941 gst_caps_append (ret, tmp);
3943 gst_structure_free (template);
3945 GST_DEBUG_OBJECT (v4l2object->element, "unknown format %u",
3946 format->pixelformat);
3954 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
3955 gst_caps_unref (tmp);
3962 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3966 if (v4l2object->probed_caps == NULL)
3967 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
3970 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
3971 GST_CAPS_INTERSECT_FIRST);
3973 ret = gst_caps_ref (v4l2object->probed_caps);
3976 GST_INFO_OBJECT (v4l2object->element, "probed caps: %" GST_PTR_FORMAT, ret);
3982 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
3985 GstBufferPool *pool = NULL, *other_pool = NULL;
3986 GstStructure *config;
3987 guint size, min, max, own_min = 0;
3989 gboolean has_video_meta;
3990 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
3991 GstAllocator *allocator = NULL;
3992 GstAllocationParams params = { 0 };
3994 GST_DEBUG_OBJECT (obj->element, "decide allocation");
3996 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3997 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
3999 gst_query_parse_allocation (query, &caps, NULL);
4001 if (obj->pool == NULL) {
4002 if (!gst_v4l2_object_setup_pool (obj, caps))
4006 if (gst_query_get_n_allocation_params (query) > 0)
4007 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
4009 if (gst_query_get_n_allocation_pools (query) > 0) {
4010 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
4019 GST_DEBUG_OBJECT (obj->element, "allocation: size:%u min:%u max:%u pool:%"
4020 GST_PTR_FORMAT, size, min, max, pool);
4023 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4025 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
4027 gst_v4l2_get_driver_min_buffers (obj);
4028 /* We can't share our own pool, if it exceed V4L2 capacity */
4029 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
4030 can_share_own_pool = FALSE;
4033 switch (obj->mode) {
4034 case GST_V4L2_IO_RW:
4036 /* in READ/WRITE mode, prefer a downstream pool because our own pool
4037 * doesn't help much, we have to write to it as well */
4038 GST_DEBUG_OBJECT (obj->element,
4039 "read/write mode: using downstream pool");
4040 /* use the bigest size, when we use our own pool we can't really do any
4041 * other size than what the hardware gives us but for downstream pools
4043 size = MAX (size, obj->info.size);
4044 } else if (can_share_own_pool) {
4045 /* no downstream pool, use our own then */
4046 GST_DEBUG_OBJECT (obj->element,
4047 "read/write mode: no downstream pool, using our own");
4048 pool = gst_object_ref (obj->pool);
4049 size = obj->info.size;
4050 pushing_from_our_pool = TRUE;
4054 case GST_V4L2_IO_USERPTR:
4055 case GST_V4L2_IO_DMABUF_IMPORT:
4056 /* in importing mode, prefer our own pool, and pass the other pool to
4057 * our own, so it can serve itself */
4059 goto no_downstream_pool;
4060 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj->pool),
4063 gst_object_unref (pool);
4064 pool = gst_object_ref (obj->pool);
4065 size = obj->info.size;
4068 case GST_V4L2_IO_MMAP:
4069 case GST_V4L2_IO_DMABUF:
4070 /* in streaming mode, prefer our own pool */
4071 /* Check if we can use it ... */
4072 if (can_share_own_pool) {
4074 gst_object_unref (pool);
4075 pool = gst_object_ref (obj->pool);
4076 size = obj->info.size;
4077 GST_DEBUG_OBJECT (obj->element,
4078 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
4079 pushing_from_our_pool = TRUE;
4081 GST_DEBUG_OBJECT (obj->element,
4082 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
4085 GST_DEBUG_OBJECT (obj->element,
4086 "streaming mode: no usable pool, copying to generic pool");
4087 size = MAX (size, obj->info.size);
4090 case GST_V4L2_IO_AUTO:
4092 GST_WARNING_OBJECT (obj->element, "unhandled mode");
4099 /* If pushing from our own pool, configure it with queried minimum,
4100 * otherwise use the minimum required */
4101 if (pushing_from_our_pool) {
4102 /* When pushing from our own pool, we need what downstream one, to be able
4103 * to fill the pipeline, the minimum required to decoder according to the
4104 * driver and 1 more, so we don't endup up with everything downstream or
4105 * held by the decoder. */
4106 own_min = min + obj->min_buffers + 1;
4108 /* If no allocation parameters where provided, allow for a little more
4109 * buffers and enable copy threshold */
4112 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4115 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4120 /* In this case we'll have to configure two buffer pool. For our buffer
4121 * pool, we'll need what the driver one, and one more, so we can dequeu */
4122 own_min = obj->min_buffers + 1;
4123 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS);
4125 /* for the downstream pool, we keep what downstream wants, though ensure
4126 * at least a minimum if downstream didn't suggest anything (we are
4127 * expecting the base class to create a default one for the context) */
4128 min = MAX (min, GST_V4L2_MIN_BUFFERS);
4130 /* To import we need the other pool to hold at least own_min */
4131 if (obj->pool == pool)
4135 /* Request a bigger max, if one was suggested but it's too small */
4137 max = MAX (min, max);
4139 /* First step, configure our own pool */
4140 config = gst_buffer_pool_get_config (obj->pool);
4142 if (obj->need_video_meta || has_video_meta) {
4143 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
4144 gst_buffer_pool_config_add_option (config,
4145 GST_BUFFER_POOL_OPTION_VIDEO_META);
4148 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4149 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
4151 GST_DEBUG_OBJECT (obj->element, "setting own pool config to %"
4152 GST_PTR_FORMAT, config);
4154 /* Our pool often need to adjust the value */
4155 if (!gst_buffer_pool_set_config (obj->pool, config)) {
4156 config = gst_buffer_pool_get_config (obj->pool);
4158 GST_DEBUG_OBJECT (obj->element, "own pool config changed to %"
4159 GST_PTR_FORMAT, config);
4161 /* our pool will adjust the maximum buffer, which we are fine with */
4162 if (!gst_buffer_pool_set_config (obj->pool, config))
4166 /* Now configure the other pool if different */
4167 if (obj->pool != pool)
4171 config = gst_buffer_pool_get_config (other_pool);
4172 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4173 gst_buffer_pool_config_set_params (config, caps, size, min, max);
4175 GST_DEBUG_OBJECT (obj->element, "setting other pool config to %"
4176 GST_PTR_FORMAT, config);
4178 /* if downstream supports video metadata, add this to the pool config */
4179 if (has_video_meta) {
4180 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
4181 gst_buffer_pool_config_add_option (config,
4182 GST_BUFFER_POOL_OPTION_VIDEO_META);
4185 if (!gst_buffer_pool_set_config (other_pool, config)) {
4186 config = gst_buffer_pool_get_config (other_pool);
4188 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
4190 gst_structure_free (config);
4194 if (!gst_buffer_pool_set_config (other_pool, config))
4200 /* For simplicity, simply read back the active configuration, so our base
4201 * class get the right information */
4202 config = gst_buffer_pool_get_config (pool);
4203 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
4204 gst_structure_free (config);
4208 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
4210 gst_query_add_allocation_pool (query, pool, size, min, max);
4213 gst_object_unref (allocator);
4216 gst_object_unref (pool);
4222 /* setup_pool already send the error */
4227 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4228 (_("Failed to configure internal buffer pool.")), (NULL));
4233 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4234 (_("Video device did not suggest any buffer size.")), (NULL));
4240 gst_object_unref (allocator);
4243 gst_object_unref (pool);
4248 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4249 (_("No downstream pool to import from.")),
4250 ("When importing DMABUF or USERPTR, we need a pool to import from"));
4256 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
4258 GstBufferPool *pool;
4259 /* we need at least 2 buffers to operate */
4260 guint size, min, max;
4264 /* Set defaults allocation parameters */
4265 size = obj->info.size;
4266 min = GST_V4L2_MIN_BUFFERS;
4267 max = VIDEO_MAX_FRAME;
4269 gst_query_parse_allocation (query, &caps, &need_pool);
4274 if ((pool = obj->pool))
4275 gst_object_ref (pool);
4279 GstStructure *config;
4281 /* we had a pool, check caps */
4282 config = gst_buffer_pool_get_config (pool);
4283 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
4285 GST_DEBUG_OBJECT (obj->element,
4286 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
4287 if (!gst_caps_is_equal (caps, pcaps)) {
4288 gst_structure_free (config);
4289 gst_object_unref (pool);
4290 goto different_caps;
4292 gst_structure_free (config);
4294 gst_v4l2_get_driver_min_buffers (obj);
4296 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS);
4298 gst_query_add_allocation_pool (query, pool, size, min, max);
4300 /* we also support various metadata */
4301 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4304 gst_object_unref (pool);
4311 GST_DEBUG_OBJECT (obj->element, "no caps specified");
4316 /* different caps, we can't use this pool */
4317 GST_DEBUG_OBJECT (obj->element, "pool has different caps");