3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
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,
21 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
22 * with newer GLib versions (>= 2.31.0) */
23 #define GLIB_DISABLE_DEPRECATION_WARNINGS
36 #include <gudev/gudev.h>
39 #include "v4l2_calls.h"
40 #include "gstv4l2tuner.h"
41 #include "gstv4l2colorbalance.h"
43 #include "gst/gst-i18n-plugin.h"
45 #include <gst/video/video.h>
47 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
48 #define GST_CAT_DEFAULT v4l2_debug
50 #define DEFAULT_PROP_DEVICE_NAME NULL
51 #define DEFAULT_PROP_DEVICE_FD -1
52 #define DEFAULT_PROP_FLAGS 0
53 #define DEFAULT_PROP_TV_NORM 0
54 #define DEFAULT_PROP_CHANNEL NULL
55 #define DEFAULT_PROP_FREQUENCY 0
56 #define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
58 #define ENCODED_BUFFER_SIZE (1 * 1024 * 1024)
63 V4L2_STD_OBJECT_PROPS,
67 * common format / caps utilities:
71 GST_V4L2_RAW = 1 << 0,
72 GST_V4L2_CODEC = 1 << 1,
73 GST_V4L2_TRANSPORT = 1 << 2,
74 GST_V4L2_NO_PARSE = 1 << 3,
82 GstV4L2FormatFlags flags;
85 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
87 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
88 {V4L2_PIX_FMT_ARGB555, TRUE, GST_V4L2_RAW},
89 {V4L2_PIX_FMT_XRGB555, TRUE, GST_V4L2_RAW},
90 {V4L2_PIX_FMT_ARGB555X, TRUE, GST_V4L2_RAW},
91 {V4L2_PIX_FMT_XRGB555X, TRUE, GST_V4L2_RAW},
92 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
93 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
94 {V4L2_PIX_FMT_BGR666, TRUE, GST_V4L2_RAW},
95 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
96 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
97 {V4L2_PIX_FMT_ABGR32, TRUE, GST_V4L2_RAW},
98 {V4L2_PIX_FMT_XBGR32, TRUE, GST_V4L2_RAW},
99 {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
100 {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},
102 /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
103 {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
104 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
105 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
106 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
107 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
110 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
111 {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
112 {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
113 {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
114 {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
115 {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
116 {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
117 {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},
119 /* Palette formats */
120 {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},
122 /* Chrominance formats */
123 {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},
125 /* Luminance+Chrominance formats */
126 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
127 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
128 {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
129 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
130 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
131 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
132 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
133 {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
134 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
135 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
136 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
137 {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
138 {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
139 {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
140 {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
141 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
142 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
143 {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
144 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
145 {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
146 {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
148 /* two planes -- one Y, one Cr + Cb interleaved */
149 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
150 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
151 {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
152 {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
153 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
154 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
155 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
156 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
158 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
159 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
160 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
162 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
163 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
164 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_CODEC},
165 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_CODEC},
166 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_CODEC},
168 /* compressed formats */
169 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
170 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
171 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
172 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
173 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
174 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
175 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
176 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
177 {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
178 {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
179 {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
180 {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
181 {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
182 {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
183 {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
184 {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
186 /* Vendor-specific formats */
187 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
188 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
189 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
190 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
193 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
195 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
198 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
200 gst_v4l2_device_get_type (void)
202 static GType v4l2_device_type = 0;
204 if (v4l2_device_type == 0) {
205 static const GFlagsValue values[] = {
206 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
207 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
208 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
210 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
211 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
213 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
214 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
220 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
223 return v4l2_device_type;
226 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
228 gst_v4l2_tv_norm_get_type (void)
230 static GType v4l2_tv_norm = 0;
233 static const GEnumValue tv_norms[] = {
236 {V4L2_STD_NTSC, "NTSC", "NTSC"},
237 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
238 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
239 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
240 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
242 {V4L2_STD_PAL, "PAL", "PAL"},
243 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
244 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
245 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
246 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
247 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
248 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
249 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
250 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
251 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
252 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
253 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
254 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
255 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
256 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
258 {V4L2_STD_SECAM, "SECAM", "SECAM"},
259 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
260 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
261 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
262 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
263 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
264 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
265 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
266 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
267 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
272 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
279 gst_v4l2_io_mode_get_type (void)
281 static GType v4l2_io_mode = 0;
284 static const GEnumValue io_modes[] = {
285 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
286 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
287 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
288 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
289 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
290 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
295 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
301 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
302 const char *default_device)
304 g_object_class_install_property (gobject_class, PROP_DEVICE,
305 g_param_spec_string ("device", "Device", "Device location",
306 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
308 g_param_spec_string ("device-name", "Device name",
309 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
310 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
312 g_param_spec_int ("device-fd", "File descriptor",
313 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
314 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
315 g_object_class_install_property (gobject_class, PROP_FLAGS,
316 g_param_spec_flags ("flags", "Flags", "Device type flags",
317 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
318 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
321 * GstV4l2Src:brightness:
323 * Picture brightness, or more precisely, the black level
325 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
326 g_param_spec_int ("brightness", "Brightness",
327 "Picture brightness, or more precisely, the black level", G_MININT,
329 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
331 * GstV4l2Src:contrast:
333 * Picture contrast or luma gain
335 g_object_class_install_property (gobject_class, PROP_CONTRAST,
336 g_param_spec_int ("contrast", "Contrast",
337 "Picture contrast or luma gain", G_MININT,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
341 * GstV4l2Src:saturation:
343 * Picture color saturation or chroma gain
345 g_object_class_install_property (gobject_class, PROP_SATURATION,
346 g_param_spec_int ("saturation", "Saturation",
347 "Picture color saturation or chroma gain", G_MININT,
349 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
353 * Hue or color balance
355 g_object_class_install_property (gobject_class, PROP_HUE,
356 g_param_spec_int ("hue", "Hue",
357 "Hue or color balance", G_MININT,
359 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
366 g_object_class_install_property (gobject_class, PROP_TV_NORM,
367 g_param_spec_enum ("norm", "TV norm",
369 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
370 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 * GstV4l2Src:io-mode:
377 g_object_class_install_property (gobject_class, PROP_IO_MODE,
378 g_param_spec_enum ("io-mode", "IO mode",
380 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384 * GstV4l2Src:extra-controls:
386 * Additional v4l2 controls for the device. The controls are identified
387 * by the control name (lowercase with '_' for any non-alphanumeric
392 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
393 g_param_spec_boxed ("extra-controls", "Extra Controls",
394 "Extra v4l2 controls (CIDs) for the device",
395 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 * GstV4l2Src:pixel-aspect-ratio:
400 * The pixel aspect ratio of the device. This overwrites the pixel aspect
401 * ratio queried from the device.
405 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
406 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
407 "Overwrite the pixel aspect ratio of the device", "1/1",
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 * GstV4l2Src:force-aspect-ratio:
413 * When enabled, the pixel aspect ratio queried from the device or set
414 * with the pixel-aspect-ratio property will be enforced.
418 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
419 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
420 "When enabled, the pixel aspect ratio will be enforced", TRUE,
421 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
428 g_object_class_install_property (gobject_class, PROP_DEVICE,
429 g_param_spec_string ("device", "Device", "Device location",
430 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
432 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
433 g_param_spec_string ("device-name", "Device name",
434 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
435 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
438 g_param_spec_int ("device-fd", "File descriptor",
439 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
440 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
442 g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
443 g_param_spec_enum ("output-io-mode", "Output IO mode",
444 "Output side I/O mode (matches sink pad)",
445 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
446 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
449 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
450 "Capture I/O mode (matches src pad)",
451 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
452 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
454 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
455 g_param_spec_boxed ("extra-controls", "Extra Controls",
456 "Extra v4l2 controls (CIDs) for the device",
457 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 gst_v4l2_object_new (GstElement * element,
462 enum v4l2_buf_type type,
463 const char *default_device,
464 GstV4l2GetInOutFunction get_in_out_func,
465 GstV4l2SetInOutFunction set_in_out_func,
466 GstV4l2UpdateFpsFunction update_fps_func)
468 GstV4l2Object *v4l2object;
471 * some default values
473 v4l2object = g_new0 (GstV4l2Object, 1);
475 v4l2object->type = type;
476 v4l2object->formats = NULL;
478 v4l2object->element = element;
479 v4l2object->get_in_out_func = get_in_out_func;
480 v4l2object->set_in_out_func = set_in_out_func;
481 v4l2object->update_fps_func = update_fps_func;
483 v4l2object->video_fd = -1;
484 v4l2object->active = FALSE;
485 v4l2object->videodev = g_strdup (default_device);
487 v4l2object->norms = NULL;
488 v4l2object->channels = NULL;
489 v4l2object->colors = NULL;
491 v4l2object->xwindow_id = 0;
493 v4l2object->keep_aspect = TRUE;
495 v4l2object->n_v4l2_planes = 0;
497 v4l2object->no_initial_format = FALSE;
502 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
506 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
508 g_return_if_fail (v4l2object != NULL);
510 g_free (v4l2object->videodev);
512 g_free (v4l2object->channel);
514 if (v4l2object->formats) {
515 gst_v4l2_object_clear_format_list (v4l2object);
518 if (v4l2object->probed_caps) {
519 gst_caps_unref (v4l2object->probed_caps);
527 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
529 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
530 g_slist_free (v4l2object->formats);
531 v4l2object->formats = NULL;
537 gst_v4l2_object_prop_to_cid (guint prop_id)
542 case PROP_BRIGHTNESS:
543 cid = V4L2_CID_BRIGHTNESS;
546 cid = V4L2_CID_CONTRAST;
548 case PROP_SATURATION:
549 cid = V4L2_CID_SATURATION;
555 GST_WARNING ("unmapped property id: %d", prop_id);
562 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
563 guint prop_id, const GValue * value, GParamSpec * pspec)
567 g_free (v4l2object->videodev);
568 v4l2object->videodev = g_value_dup_string (value);
570 case PROP_BRIGHTNESS:
572 case PROP_SATURATION:
575 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
578 if (GST_V4L2_IS_OPEN (v4l2object)) {
579 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
586 v4l2object->tv_norm = g_value_get_enum (value);
590 if (GST_V4L2_IS_OPEN (v4l2object)) {
591 GstTuner *tuner = GST_TUNER (v4l2object->element);
592 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
593 (gchar *) g_value_get_string (value));
596 /* like gst_tuner_set_channel (tuner, channel)
597 without g_object_notify */
598 gst_v4l2_tuner_set_channel (v4l2object, channel);
601 g_free (v4l2object->channel);
602 v4l2object->channel = g_value_dup_string (value);
606 if (GST_V4L2_IS_OPEN (v4l2object)) {
607 GstTuner *tuner = GST_TUNER (v4l2object->element);
608 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
611 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
613 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
614 without g_object_notify */
615 gst_v4l2_tuner_set_frequency (v4l2object, channel,
616 g_value_get_ulong (value));
619 v4l2object->frequency = g_value_get_ulong (value);
625 v4l2object->req_mode = g_value_get_enum (value);
627 case PROP_CAPTURE_IO_MODE:
628 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
629 v4l2object->req_mode = g_value_get_enum (value);
631 case PROP_OUTPUT_IO_MODE:
632 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
633 v4l2object->req_mode = g_value_get_enum (value);
635 case PROP_EXTRA_CONTROLS:{
636 const GstStructure *s = gst_value_get_structure (value);
638 if (v4l2object->extra_controls)
639 gst_structure_free (v4l2object->extra_controls);
641 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
642 if (GST_V4L2_IS_OPEN (v4l2object))
643 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
646 case PROP_PIXEL_ASPECT_RATIO:
647 g_free (v4l2object->par);
648 v4l2object->par = g_new0 (GValue, 1);
649 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
650 if (!g_value_transform (value, v4l2object->par)) {
651 g_warning ("Could not transform string to aspect ratio");
652 gst_value_set_fraction (v4l2object->par, 1, 1);
654 GST_DEBUG_OBJECT (v4l2object->element, "set PAR to %d/%d",
655 gst_value_get_fraction_numerator (v4l2object->par),
656 gst_value_get_fraction_denominator (v4l2object->par));
658 case PROP_FORCE_ASPECT_RATIO:
659 v4l2object->keep_aspect = g_value_get_boolean (value);
670 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
671 guint prop_id, GValue * value, GParamSpec * pspec)
675 g_value_set_string (value, v4l2object->videodev);
677 case PROP_DEVICE_NAME:
679 const guchar *new = NULL;
681 if (GST_V4L2_IS_OPEN (v4l2object)) {
682 new = v4l2object->vcap.card;
683 } else if (gst_v4l2_open (v4l2object)) {
684 new = v4l2object->vcap.card;
685 gst_v4l2_close (v4l2object);
687 g_value_set_string (value, (gchar *) new);
692 if (GST_V4L2_IS_OPEN (v4l2object))
693 g_value_set_int (value, v4l2object->video_fd);
695 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
702 if (GST_V4L2_IS_OPEN (v4l2object)) {
703 flags |= v4l2object->vcap.capabilities &
704 (V4L2_CAP_VIDEO_CAPTURE |
705 V4L2_CAP_VIDEO_OUTPUT |
706 V4L2_CAP_VIDEO_OVERLAY |
707 V4L2_CAP_VBI_CAPTURE |
708 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
710 if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
711 flags |= V4L2_CAP_VIDEO_CAPTURE;
713 if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
714 flags |= V4L2_CAP_VIDEO_OUTPUT;
716 g_value_set_flags (value, flags);
719 case PROP_BRIGHTNESS:
721 case PROP_SATURATION:
724 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
727 if (GST_V4L2_IS_OPEN (v4l2object)) {
729 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
730 g_value_set_int (value, v);
738 g_value_set_enum (value, v4l2object->tv_norm);
741 g_value_set_enum (value, v4l2object->req_mode);
743 case PROP_CAPTURE_IO_MODE:
744 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
745 g_value_set_enum (value, v4l2object->req_mode);
747 case PROP_OUTPUT_IO_MODE:
748 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
749 g_value_set_enum (value, v4l2object->req_mode);
751 case PROP_EXTRA_CONTROLS:
752 gst_value_set_structure (value, v4l2object->extra_controls);
754 case PROP_PIXEL_ASPECT_RATIO:
756 g_value_transform (v4l2object->par, value);
758 case PROP_FORCE_ASPECT_RATIO:
759 g_value_set_boolean (value, v4l2object->keep_aspect);
769 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
771 struct v4l2_control control = { 0, };
773 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
775 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
776 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
778 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
780 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
781 GST_DEBUG_OBJECT (v4l2object->element,
782 "driver requires a minimum of %d buffers", control.value);
783 v4l2object->min_buffers = control.value;
785 v4l2object->min_buffers = 0;
790 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
792 GstTunerNorm *norm = NULL;
793 GstTunerChannel *channel = NULL;
796 if (!GST_IS_TUNER (v4l2object->element))
799 tuner = GST_TUNER (v4l2object->element);
801 if (v4l2object->tv_norm)
802 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
803 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
804 "norm=%p", (guint64) v4l2object->tv_norm, norm);
806 gst_tuner_set_norm (tuner, norm);
809 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
811 v4l2object->tv_norm =
812 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
813 gst_tuner_norm_changed (tuner, norm);
817 if (v4l2object->channel)
818 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
820 gst_tuner_set_channel (tuner, channel);
823 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
824 (v4l2object->element)));
826 g_free (v4l2object->channel);
827 v4l2object->channel = g_strdup (channel->label);
828 gst_tuner_channel_changed (tuner, channel);
833 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
834 if (v4l2object->frequency != 0) {
835 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
837 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
838 if (v4l2object->frequency == 0) {
840 gst_tuner_set_frequency (tuner, channel, 1000);
848 gst_v4l2_object_open (GstV4l2Object * v4l2object)
850 if (gst_v4l2_open (v4l2object))
851 gst_v4l2_set_defaults (v4l2object);
859 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
863 ret = gst_v4l2_dup (v4l2object, other);
869 gst_v4l2_object_close (GstV4l2Object * v4l2object)
871 if (!gst_v4l2_close (v4l2object))
874 gst_caps_replace (&v4l2object->probed_caps, NULL);
876 if (v4l2object->formats) {
877 gst_v4l2_object_clear_format_list (v4l2object);
883 static struct v4l2_fmtdesc *
884 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
887 struct v4l2_fmtdesc *fmt;
893 walk = gst_v4l2_object_get_format_list (v4l2object);
895 fmt = (struct v4l2_fmtdesc *) walk->data;
896 if (fmt->pixelformat == fourcc)
898 /* special case for jpeg */
899 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
900 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
901 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
902 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
903 fourcc == V4L2_PIX_FMT_PJPG) {
907 walk = g_slist_next (walk);
915 /* complete made up ranking, the values themselves are meaningless */
916 /* These ranks MUST be X such that X<<15 fits on a signed int - see
917 the comment at the end of gst_v4l2_object_format_get_rank. */
918 #define YUV_BASE_RANK 1000
919 #define JPEG_BASE_RANK 500
920 #define DV_BASE_RANK 200
921 #define RGB_BASE_RANK 100
922 #define YUV_ODD_BASE_RANK 50
923 #define RGB_ODD_BASE_RANK 25
924 #define BAYER_BASE_RANK 15
925 #define S910_BASE_RANK 10
926 #define GREY_BASE_RANK 5
927 #define PWC_BASE_RANK 1
930 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
932 guint32 fourcc = fmt->pixelformat;
933 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
937 case V4L2_PIX_FMT_MJPEG:
938 case V4L2_PIX_FMT_PJPG:
939 rank = JPEG_BASE_RANK;
941 case V4L2_PIX_FMT_JPEG:
942 rank = JPEG_BASE_RANK + 1;
944 case V4L2_PIX_FMT_MPEG: /* MPEG */
945 rank = JPEG_BASE_RANK + 2;
948 case V4L2_PIX_FMT_RGB332:
949 case V4L2_PIX_FMT_ARGB555:
950 case V4L2_PIX_FMT_XRGB555:
951 case V4L2_PIX_FMT_RGB555:
952 case V4L2_PIX_FMT_ARGB555X:
953 case V4L2_PIX_FMT_XRGB555X:
954 case V4L2_PIX_FMT_RGB555X:
955 case V4L2_PIX_FMT_BGR666:
956 case V4L2_PIX_FMT_RGB565:
957 case V4L2_PIX_FMT_RGB565X:
958 case V4L2_PIX_FMT_RGB444:
959 case V4L2_PIX_FMT_Y4:
960 case V4L2_PIX_FMT_Y6:
961 case V4L2_PIX_FMT_Y10:
962 case V4L2_PIX_FMT_Y12:
963 case V4L2_PIX_FMT_Y10BPACK:
964 case V4L2_PIX_FMT_YUV555:
965 case V4L2_PIX_FMT_YUV565:
966 case V4L2_PIX_FMT_YUV32:
967 case V4L2_PIX_FMT_NV12MT_16X16:
968 case V4L2_PIX_FMT_NV42:
969 case V4L2_PIX_FMT_H264_MVC:
970 rank = RGB_ODD_BASE_RANK;
973 case V4L2_PIX_FMT_RGB24:
974 case V4L2_PIX_FMT_BGR24:
975 rank = RGB_BASE_RANK - 1;
978 case V4L2_PIX_FMT_RGB32:
979 case V4L2_PIX_FMT_BGR32:
980 case V4L2_PIX_FMT_ABGR32:
981 case V4L2_PIX_FMT_XBGR32:
982 case V4L2_PIX_FMT_ARGB32:
983 case V4L2_PIX_FMT_XRGB32:
984 rank = RGB_BASE_RANK;
987 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
988 rank = GREY_BASE_RANK;
991 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
992 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
993 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
994 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
995 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
996 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
997 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
998 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
999 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1000 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1001 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1002 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1003 rank = YUV_ODD_BASE_RANK;
1006 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1007 rank = YUV_BASE_RANK + 3;
1009 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1010 rank = YUV_BASE_RANK + 2;
1012 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1013 case V4L2_PIX_FMT_YUV420M:
1014 rank = YUV_BASE_RANK + 7;
1016 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1017 rank = YUV_BASE_RANK + 10;
1019 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1020 rank = YUV_BASE_RANK + 6;
1022 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1023 rank = YUV_BASE_RANK + 9;
1025 case V4L2_PIX_FMT_YUV444:
1026 rank = YUV_BASE_RANK + 6;
1028 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1029 rank = YUV_BASE_RANK + 5;
1031 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1032 rank = YUV_BASE_RANK + 4;
1034 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1035 rank = YUV_BASE_RANK + 8;
1038 case V4L2_PIX_FMT_DV:
1039 rank = DV_BASE_RANK;
1042 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1046 case V4L2_PIX_FMT_SBGGR8:
1047 case V4L2_PIX_FMT_SGBRG8:
1048 case V4L2_PIX_FMT_SGRBG8:
1049 case V4L2_PIX_FMT_SRGGB8:
1050 rank = BAYER_BASE_RANK;
1053 case V4L2_PIX_FMT_SN9C10X:
1054 rank = S910_BASE_RANK;
1057 case V4L2_PIX_FMT_PWC1:
1058 rank = PWC_BASE_RANK;
1060 case V4L2_PIX_FMT_PWC2:
1061 rank = PWC_BASE_RANK;
1069 /* All ranks are below 1<<15 so a shift by 15
1070 * will a) make all non-emulated formats larger
1071 * than emulated and b) will not overflow
1082 format_cmp_func (gconstpointer a, gconstpointer b)
1084 const struct v4l2_fmtdesc *fa = a;
1085 const struct v4l2_fmtdesc *fb = b;
1087 if (fa->pixelformat == fb->pixelformat)
1090 return gst_v4l2_object_format_get_rank (fb) -
1091 gst_v4l2_object_format_get_rank (fa);
1094 /******************************************************
1095 * gst_v4l2_object_fill_format_list():
1096 * create list of supported capture formats
1097 * return value: TRUE on success, FALSE on error
1098 ******************************************************/
1100 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1101 enum v4l2_buf_type type)
1104 struct v4l2_fmtdesc *format;
1106 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1108 /* format enumeration */
1110 format = g_new0 (struct v4l2_fmtdesc, 1);
1113 format->type = type;
1115 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1116 if (errno == EINVAL) {
1118 break; /* end of enumeration */
1124 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1125 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1126 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1127 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1128 format->description);
1129 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1130 GST_FOURCC_ARGS (format->pixelformat));
1132 /* sort formats according to our preference; we do this, because caps
1133 * are probed in the order the formats are in the list, and the order of
1134 * formats in the final probed caps matters for things like fixation */
1135 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1136 (GCompareFunc) format_cmp_func);
1139 #ifndef GST_DISABLE_GST_DEBUG
1143 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1144 for (l = v4l2object->formats; l != NULL; l = l->next) {
1147 GST_INFO_OBJECT (v4l2object->element,
1148 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1149 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1159 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1160 (_("Failed to enumerate possible video formats device '%s' can work with"), v4l2object->videodev), ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)", n, v4l2object->videodev, errno, g_strerror (errno)));
1167 * Get the list of supported capture formats, a list of
1168 * <code>struct v4l2_fmtdesc</code>.
1171 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1173 if (!v4l2object->formats) {
1175 /* check usual way */
1176 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1178 /* if our driver supports multi-planar
1179 * and if formats are still empty then we can workaround driver bug
1180 * by also looking up formats as if our device was not supporting
1182 if (!v4l2object->formats) {
1183 switch (v4l2object->type) {
1184 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1185 gst_v4l2_object_fill_format_list (v4l2object,
1186 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1189 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1190 gst_v4l2_object_fill_format_list (v4l2object,
1191 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1199 return v4l2object->formats;
1202 static GstVideoFormat
1203 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1205 GstVideoFormat format;
1208 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1209 format = GST_VIDEO_FORMAT_GRAY8;
1211 case V4L2_PIX_FMT_Y16:
1212 format = GST_VIDEO_FORMAT_GRAY16_LE;
1214 case V4L2_PIX_FMT_Y16_BE:
1215 format = GST_VIDEO_FORMAT_GRAY16_BE;
1217 case V4L2_PIX_FMT_XRGB555:
1218 case V4L2_PIX_FMT_RGB555:
1219 format = GST_VIDEO_FORMAT_RGB15;
1221 case V4L2_PIX_FMT_XRGB555X:
1222 case V4L2_PIX_FMT_RGB555X:
1223 format = GST_VIDEO_FORMAT_BGR15;
1225 case V4L2_PIX_FMT_RGB565:
1226 format = GST_VIDEO_FORMAT_RGB16;
1228 case V4L2_PIX_FMT_RGB24:
1229 format = GST_VIDEO_FORMAT_RGB;
1231 case V4L2_PIX_FMT_BGR24:
1232 format = GST_VIDEO_FORMAT_BGR;
1234 case V4L2_PIX_FMT_XRGB32:
1235 case V4L2_PIX_FMT_RGB32:
1236 format = GST_VIDEO_FORMAT_xRGB;
1238 case V4L2_PIX_FMT_XBGR32:
1239 case V4L2_PIX_FMT_BGR32:
1240 format = GST_VIDEO_FORMAT_BGRx;
1242 case V4L2_PIX_FMT_ABGR32:
1243 format = GST_VIDEO_FORMAT_BGRA;
1245 case V4L2_PIX_FMT_ARGB32:
1246 format = GST_VIDEO_FORMAT_ARGB;
1248 case V4L2_PIX_FMT_NV12:
1249 case V4L2_PIX_FMT_NV12M:
1250 format = GST_VIDEO_FORMAT_NV12;
1252 case V4L2_PIX_FMT_NV12MT:
1253 format = GST_VIDEO_FORMAT_NV12_64Z32;
1255 case V4L2_PIX_FMT_NV21:
1256 case V4L2_PIX_FMT_NV21M:
1257 format = GST_VIDEO_FORMAT_NV21;
1259 case V4L2_PIX_FMT_YVU410:
1260 format = GST_VIDEO_FORMAT_YVU9;
1262 case V4L2_PIX_FMT_YUV410:
1263 format = GST_VIDEO_FORMAT_YUV9;
1265 case V4L2_PIX_FMT_YUV420:
1266 case V4L2_PIX_FMT_YUV420M:
1267 format = GST_VIDEO_FORMAT_I420;
1269 case V4L2_PIX_FMT_YUYV:
1270 format = GST_VIDEO_FORMAT_YUY2;
1272 case V4L2_PIX_FMT_YVU420:
1273 format = GST_VIDEO_FORMAT_YV12;
1275 case V4L2_PIX_FMT_UYVY:
1276 format = GST_VIDEO_FORMAT_UYVY;
1278 case V4L2_PIX_FMT_YUV411P:
1279 format = GST_VIDEO_FORMAT_Y41B;
1281 case V4L2_PIX_FMT_YUV422P:
1282 format = GST_VIDEO_FORMAT_Y42B;
1284 case V4L2_PIX_FMT_YVYU:
1285 format = GST_VIDEO_FORMAT_YVYU;
1287 case V4L2_PIX_FMT_NV16:
1288 case V4L2_PIX_FMT_NV16M:
1289 format = GST_VIDEO_FORMAT_NV16;
1291 case V4L2_PIX_FMT_NV61:
1292 case V4L2_PIX_FMT_NV61M:
1293 format = GST_VIDEO_FORMAT_NV61;
1295 case V4L2_PIX_FMT_NV24:
1296 format = GST_VIDEO_FORMAT_NV24;
1299 format = GST_VIDEO_FORMAT_UNKNOWN;
1307 gst_v4l2_object_v4l2fourcc_is_rgb (guint32 fourcc)
1309 gboolean ret = FALSE;
1312 case V4L2_PIX_FMT_XRGB555:
1313 case V4L2_PIX_FMT_RGB555:
1314 case V4L2_PIX_FMT_XRGB555X:
1315 case V4L2_PIX_FMT_RGB555X:
1316 case V4L2_PIX_FMT_RGB565:
1317 case V4L2_PIX_FMT_RGB24:
1318 case V4L2_PIX_FMT_BGR24:
1319 case V4L2_PIX_FMT_XRGB32:
1320 case V4L2_PIX_FMT_RGB32:
1321 case V4L2_PIX_FMT_XBGR32:
1322 case V4L2_PIX_FMT_BGR32:
1323 case V4L2_PIX_FMT_ABGR32:
1324 case V4L2_PIX_FMT_ARGB32:
1334 static GstStructure *
1335 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1337 GstStructure *structure = NULL;
1340 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1341 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1342 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1343 structure = gst_structure_new_empty ("image/jpeg");
1345 case V4L2_PIX_FMT_MPEG1:
1346 structure = gst_structure_new ("video/mpeg",
1347 "mpegversion", G_TYPE_INT, 2, NULL);
1349 case V4L2_PIX_FMT_MPEG2:
1350 structure = gst_structure_new ("video/mpeg",
1351 "mpegversion", G_TYPE_INT, 2, NULL);
1353 case V4L2_PIX_FMT_MPEG4:
1354 case V4L2_PIX_FMT_XVID:
1355 structure = gst_structure_new ("video/mpeg",
1356 "mpegversion", G_TYPE_INT, 4, "systemstream",
1357 G_TYPE_BOOLEAN, FALSE, NULL);
1359 case V4L2_PIX_FMT_H263:
1360 structure = gst_structure_new ("video/x-h263",
1361 "variant", G_TYPE_STRING, "itu", NULL);
1363 case V4L2_PIX_FMT_H264: /* H.264 */
1364 structure = gst_structure_new ("video/x-h264",
1365 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1366 G_TYPE_STRING, "au", NULL);
1368 case V4L2_PIX_FMT_H264_NO_SC:
1369 structure = gst_structure_new ("video/x-h264",
1370 "stream-format", G_TYPE_STRING, "avc", "alignment",
1371 G_TYPE_STRING, "au", NULL);
1373 case V4L2_PIX_FMT_VC1_ANNEX_G:
1374 case V4L2_PIX_FMT_VC1_ANNEX_L:
1375 structure = gst_structure_new ("video/x-wmv",
1376 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1378 case V4L2_PIX_FMT_VP8:
1379 structure = gst_structure_new_empty ("video/x-vp8");
1381 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1382 case V4L2_PIX_FMT_Y16:
1383 case V4L2_PIX_FMT_Y16_BE:
1384 case V4L2_PIX_FMT_XRGB555:
1385 case V4L2_PIX_FMT_RGB555:
1386 case V4L2_PIX_FMT_XRGB555X:
1387 case V4L2_PIX_FMT_RGB555X:
1388 case V4L2_PIX_FMT_RGB565:
1389 case V4L2_PIX_FMT_RGB24:
1390 case V4L2_PIX_FMT_BGR24:
1391 case V4L2_PIX_FMT_RGB32:
1392 case V4L2_PIX_FMT_XRGB32:
1393 case V4L2_PIX_FMT_ARGB32:
1394 case V4L2_PIX_FMT_BGR32:
1395 case V4L2_PIX_FMT_XBGR32:
1396 case V4L2_PIX_FMT_ABGR32:
1397 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1398 case V4L2_PIX_FMT_NV12M:
1399 case V4L2_PIX_FMT_NV12MT:
1400 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1401 case V4L2_PIX_FMT_NV21M:
1402 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1403 case V4L2_PIX_FMT_NV16M:
1404 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1405 case V4L2_PIX_FMT_NV61M:
1406 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1407 case V4L2_PIX_FMT_YVU410:
1408 case V4L2_PIX_FMT_YUV410:
1409 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1410 case V4L2_PIX_FMT_YUV420M:
1411 case V4L2_PIX_FMT_YUYV:
1412 case V4L2_PIX_FMT_YVU420:
1413 case V4L2_PIX_FMT_UYVY:
1414 case V4L2_PIX_FMT_YUV422P:
1415 case V4L2_PIX_FMT_YVYU:
1416 case V4L2_PIX_FMT_YUV411P:{
1417 GstVideoFormat format;
1418 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1419 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1420 structure = gst_structure_new ("video/x-raw",
1421 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1424 case V4L2_PIX_FMT_DV:
1426 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1429 case V4L2_PIX_FMT_MPEG: /* MPEG */
1430 structure = gst_structure_new ("video/mpegts",
1431 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1433 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1435 case V4L2_PIX_FMT_SBGGR8:
1436 case V4L2_PIX_FMT_SGBRG8:
1437 case V4L2_PIX_FMT_SGRBG8:
1438 case V4L2_PIX_FMT_SRGGB8:
1439 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1440 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1441 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1442 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1443 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1445 case V4L2_PIX_FMT_SN9C10X:
1446 structure = gst_structure_new_empty ("video/x-sonix");
1448 case V4L2_PIX_FMT_PWC1:
1449 structure = gst_structure_new_empty ("video/x-pwc1");
1451 case V4L2_PIX_FMT_PWC2:
1452 structure = gst_structure_new_empty ("video/x-pwc2");
1454 case V4L2_PIX_FMT_RGB332:
1455 case V4L2_PIX_FMT_BGR666:
1456 case V4L2_PIX_FMT_ARGB555X:
1457 case V4L2_PIX_FMT_RGB565X:
1458 case V4L2_PIX_FMT_RGB444:
1459 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1460 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1461 case V4L2_PIX_FMT_Y4:
1462 case V4L2_PIX_FMT_Y6:
1463 case V4L2_PIX_FMT_Y10:
1464 case V4L2_PIX_FMT_Y12:
1465 case V4L2_PIX_FMT_Y10BPACK:
1466 case V4L2_PIX_FMT_YUV444:
1467 case V4L2_PIX_FMT_YUV555:
1468 case V4L2_PIX_FMT_YUV565:
1469 case V4L2_PIX_FMT_Y41P:
1470 case V4L2_PIX_FMT_YUV32:
1471 case V4L2_PIX_FMT_NV12MT_16X16:
1472 case V4L2_PIX_FMT_NV42:
1473 case V4L2_PIX_FMT_H264_MVC:
1475 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1476 fourcc, GST_FOURCC_ARGS (fourcc));
1484 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1486 GstStructure *template;
1489 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1491 if (template == NULL)
1494 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1495 if (gst_v4l2_formats[i].format != fourcc)
1498 if (gst_v4l2_formats[i].dimensions) {
1499 gst_structure_set (template,
1500 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1501 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1502 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1513 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1515 GstStructure *structure;
1519 caps = gst_caps_new_empty ();
1520 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1522 if ((gst_v4l2_formats[i].flags & flags) == 0)
1526 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1529 GstStructure *alt_s = NULL;
1531 if (gst_v4l2_formats[i].dimensions) {
1532 gst_structure_set (structure,
1533 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1534 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1535 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1538 switch (gst_v4l2_formats[i].format) {
1539 case V4L2_PIX_FMT_RGB32:
1540 alt_s = gst_structure_copy (structure);
1541 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1543 case V4L2_PIX_FMT_BGR32:
1544 alt_s = gst_structure_copy (structure);
1545 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1550 gst_caps_append_structure (caps, structure);
1553 gst_caps_append_structure (caps, alt_s);
1557 return gst_caps_simplify (caps);
1561 gst_v4l2_object_get_all_caps (void)
1563 static GstCaps *caps = NULL;
1566 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1568 return gst_caps_ref (caps);
1572 gst_v4l2_object_get_raw_caps (void)
1574 static GstCaps *caps = NULL;
1577 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1579 return gst_caps_ref (caps);
1583 gst_v4l2_object_get_codec_caps (void)
1585 static GstCaps *caps = NULL;
1588 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1590 return gst_caps_ref (caps);
1593 /* collect data for the given caps
1594 * @caps: given input caps
1595 * @format: location for the v4l format
1596 * @w/@h: location for width and height
1597 * @fps_n/@fps_d: location for framerate
1598 * @size: location for expected size of the frame or 0 if unknown
1601 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1602 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1604 GstStructure *structure;
1605 guint32 fourcc = 0, fourcc_nc = 0;
1606 const gchar *mimetype;
1607 struct v4l2_fmtdesc *fmt = NULL;
1609 structure = gst_caps_get_structure (caps, 0);
1611 mimetype = gst_structure_get_name (structure);
1613 if (!gst_video_info_from_caps (info, caps))
1614 goto invalid_format;
1616 if (g_str_equal (mimetype, "video/x-raw")) {
1617 switch (GST_VIDEO_INFO_FORMAT (info)) {
1618 case GST_VIDEO_FORMAT_I420:
1619 fourcc = V4L2_PIX_FMT_YUV420;
1620 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1622 case GST_VIDEO_FORMAT_YUY2:
1623 fourcc = V4L2_PIX_FMT_YUYV;
1625 case GST_VIDEO_FORMAT_UYVY:
1626 fourcc = V4L2_PIX_FMT_UYVY;
1628 case GST_VIDEO_FORMAT_YV12:
1629 fourcc = V4L2_PIX_FMT_YVU420;
1631 case GST_VIDEO_FORMAT_Y41B:
1632 fourcc = V4L2_PIX_FMT_YUV411P;
1634 case GST_VIDEO_FORMAT_Y42B:
1635 fourcc = V4L2_PIX_FMT_YUV422P;
1637 case GST_VIDEO_FORMAT_NV12:
1638 fourcc = V4L2_PIX_FMT_NV12;
1639 fourcc_nc = V4L2_PIX_FMT_NV12M;
1641 case GST_VIDEO_FORMAT_NV12_64Z32:
1642 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1644 case GST_VIDEO_FORMAT_NV21:
1645 fourcc = V4L2_PIX_FMT_NV21;
1646 fourcc_nc = V4L2_PIX_FMT_NV21M;
1648 case GST_VIDEO_FORMAT_NV16:
1649 fourcc = V4L2_PIX_FMT_NV16;
1650 fourcc_nc = V4L2_PIX_FMT_NV16M;
1652 case GST_VIDEO_FORMAT_NV61:
1653 fourcc = V4L2_PIX_FMT_NV61;
1654 fourcc_nc = V4L2_PIX_FMT_NV61M;
1656 case GST_VIDEO_FORMAT_NV24:
1657 fourcc = V4L2_PIX_FMT_NV24;
1659 case GST_VIDEO_FORMAT_YVYU:
1660 fourcc = V4L2_PIX_FMT_YVYU;
1662 case GST_VIDEO_FORMAT_RGB15:
1663 fourcc = V4L2_PIX_FMT_RGB555;
1665 case GST_VIDEO_FORMAT_RGB16:
1666 fourcc = V4L2_PIX_FMT_RGB565;
1668 case GST_VIDEO_FORMAT_RGB:
1669 fourcc = V4L2_PIX_FMT_RGB24;
1671 case GST_VIDEO_FORMAT_BGR:
1672 fourcc = V4L2_PIX_FMT_BGR24;
1674 case GST_VIDEO_FORMAT_xRGB:
1675 fourcc = V4L2_PIX_FMT_RGB32;
1676 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1678 case GST_VIDEO_FORMAT_ARGB:
1679 fourcc = V4L2_PIX_FMT_RGB32;
1680 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1682 case GST_VIDEO_FORMAT_BGRx:
1683 fourcc = V4L2_PIX_FMT_BGR32;
1684 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1686 case GST_VIDEO_FORMAT_BGRA:
1687 fourcc = V4L2_PIX_FMT_BGR32;
1688 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1690 case GST_VIDEO_FORMAT_GRAY8:
1691 fourcc = V4L2_PIX_FMT_GREY;
1693 case GST_VIDEO_FORMAT_GRAY16_LE:
1694 fourcc = V4L2_PIX_FMT_Y16;
1696 case GST_VIDEO_FORMAT_GRAY16_BE:
1697 fourcc = V4L2_PIX_FMT_Y16_BE;
1703 if (g_str_equal (mimetype, "video/mpegts")) {
1704 fourcc = V4L2_PIX_FMT_MPEG;
1705 } else if (g_str_equal (mimetype, "video/x-dv")) {
1706 fourcc = V4L2_PIX_FMT_DV;
1707 } else if (g_str_equal (mimetype, "image/jpeg")) {
1708 fourcc = V4L2_PIX_FMT_JPEG;
1709 } else if (g_str_equal (mimetype, "video/mpeg")) {
1711 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1714 fourcc = V4L2_PIX_FMT_MPEG1;
1717 fourcc = V4L2_PIX_FMT_MPEG2;
1720 fourcc = V4L2_PIX_FMT_MPEG4;
1721 fourcc_nc = V4L2_PIX_FMT_XVID;
1727 } else if (g_str_equal (mimetype, "video/x-h263")) {
1728 fourcc = V4L2_PIX_FMT_H263;
1729 } else if (g_str_equal (mimetype, "video/x-h264")) {
1730 const gchar *stream_format =
1731 gst_structure_get_string (structure, "stream-format");
1732 if (g_str_equal (stream_format, "avc"))
1733 fourcc = V4L2_PIX_FMT_H264_NO_SC;
1735 fourcc = V4L2_PIX_FMT_H264;
1736 } else if (g_str_equal (mimetype, "video/x-vp8")) {
1737 fourcc = V4L2_PIX_FMT_VP8;
1738 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1739 const gchar *format = gst_structure_get_string (structure, "format");
1741 if (!g_ascii_strcasecmp (format, "bggr"))
1742 fourcc = V4L2_PIX_FMT_SBGGR8;
1743 else if (!g_ascii_strcasecmp (format, "gbrg"))
1744 fourcc = V4L2_PIX_FMT_SGBRG8;
1745 else if (!g_ascii_strcasecmp (format, "grbg"))
1746 fourcc = V4L2_PIX_FMT_SGRBG8;
1747 else if (!g_ascii_strcasecmp (format, "rggb"))
1748 fourcc = V4L2_PIX_FMT_SRGGB8;
1750 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1751 fourcc = V4L2_PIX_FMT_SN9C10X;
1752 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1753 fourcc = V4L2_PIX_FMT_PWC1;
1754 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1755 fourcc = V4L2_PIX_FMT_PWC2;
1760 /* Prefer the non-contiguous if supported */
1761 v4l2object->prefered_non_contiguous = TRUE;
1764 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
1765 else if (fourcc == 0)
1766 goto unhandled_format;
1769 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1770 v4l2object->prefered_non_contiguous = FALSE;
1774 goto unsupported_format;
1783 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1788 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1793 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1799 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1800 guint32 pixelformat, gint * width, gint * height);
1803 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1805 struct v4l2_cropcap cropcap;
1806 int num = 1, den = 1;
1808 if (!v4l2object->keep_aspect)
1811 if (v4l2object->par) {
1812 num = gst_value_get_fraction_numerator (v4l2object->par);
1813 den = gst_value_get_fraction_denominator (v4l2object->par);
1817 memset (&cropcap, 0, sizeof (cropcap));
1819 cropcap.type = v4l2object->type;
1820 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
1821 goto cropcap_failed;
1823 num = cropcap.pixelaspect.numerator;
1824 den = cropcap.pixelaspect.denominator;
1826 /* Ignore PAR that are 0/0 */
1831 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, num, den,
1836 if (errno != ENOTTY)
1837 GST_WARNING_OBJECT (v4l2object->element,
1838 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
1839 g_strerror (errno));
1843 /* returns TRUE if the value was changed in place, otherwise FALSE */
1845 gst_v4l2src_value_simplify (GValue * val)
1847 /* simplify list of one value to one value */
1848 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
1849 const GValue *list_val;
1850 GValue new_val = G_VALUE_INIT;
1852 list_val = gst_value_list_get_value (val, 0);
1853 g_value_init (&new_val, G_VALUE_TYPE (list_val));
1854 g_value_copy (list_val, &new_val);
1855 g_value_unset (val);
1864 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
1865 GstVideoInterlaceMode * interlace_mode)
1867 /* NB: If you add new return values, please fix mode_strings in
1868 * gst_v4l2_object_add_interlace_mode */
1870 case V4L2_FIELD_ANY:
1872 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
1874 case V4L2_FIELD_NONE:
1875 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1877 case V4L2_FIELD_INTERLACED:
1878 case V4L2_FIELD_INTERLACED_TB:
1879 case V4L2_FIELD_INTERLACED_BT:
1880 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1883 GST_ERROR ("Unknown enum v4l2_field %d", field);
1889 gst_v4l2_object_get_colorspace (enum v4l2_colorspace colorspace,
1890 enum v4l2_quantization range, enum v4l2_ycbcr_encoding matrix,
1891 enum v4l2_xfer_func transfer, gboolean is_rgb, GstVideoColorimetry * cinfo)
1893 gboolean ret = TRUE;
1895 /* First step, set the defaults for each primaries */
1896 switch (colorspace) {
1897 case V4L2_COLORSPACE_SMPTE170M:
1898 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1899 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1900 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1901 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1903 case V4L2_COLORSPACE_REC709:
1904 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1905 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1906 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1907 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1909 case V4L2_COLORSPACE_SRGB:
1910 case V4L2_COLORSPACE_JPEG:
1911 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1912 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1913 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
1914 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1916 case V4L2_COLORSPACE_ADOBERGB:
1917 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1918 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1919 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
1920 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
1922 case V4L2_COLORSPACE_BT2020:
1923 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1924 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1925 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1926 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
1928 case V4L2_COLORSPACE_SMPTE240M:
1929 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1930 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1931 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
1932 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
1934 case V4L2_COLORSPACE_470_SYSTEM_M:
1935 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1936 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1937 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1938 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
1940 case V4L2_COLORSPACE_470_SYSTEM_BG:
1941 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1942 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1943 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1944 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
1946 case V4L2_COLORSPACE_RAW:
1947 /* Explicitly unknown */
1948 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
1949 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
1950 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
1951 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
1954 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
1962 /* Second step, apply any custom variation */
1964 case V4L2_QUANTIZATION_FULL_RANGE:
1965 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1967 case V4L2_QUANTIZATION_LIM_RANGE:
1968 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1970 case V4L2_QUANTIZATION_DEFAULT:
1971 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
1972 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
1973 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1974 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601
1975 || matrix == V4L2_YCBCR_ENC_XV709
1976 || colorspace == V4L2_COLORSPACE_JPEG)
1977 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1979 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1982 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
1983 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
1988 case V4L2_YCBCR_ENC_XV601:
1989 case V4L2_YCBCR_ENC_SYCC:
1990 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
1992 case V4L2_YCBCR_ENC_601:
1993 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1995 case V4L2_YCBCR_ENC_XV709:
1996 GST_FIXME ("XV709 not defined, assuming 709");
1998 case V4L2_YCBCR_ENC_709:
1999 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2001 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2002 GST_FIXME ("BT2020 with constant lumma is not defined, assuming BT2020");
2004 case V4L2_YCBCR_ENC_BT2020:
2005 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2007 case V4L2_YCBCR_ENC_SMPTE240M:
2008 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2010 case V4L2_YCBCR_ENC_DEFAULT:
2011 /* nothing, just use defaults for colorspace */
2014 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2015 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2020 case V4L2_XFER_FUNC_709:
2021 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2023 case V4L2_XFER_FUNC_SRGB:
2024 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2026 case V4L2_XFER_FUNC_ADOBERGB:
2027 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2029 case V4L2_XFER_FUNC_SMPTE240M:
2030 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2032 case V4L2_XFER_FUNC_NONE:
2033 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2035 case V4L2_XFER_FUNC_DEFAULT:
2036 /* nothing, just use defaults for colorspace */
2039 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2040 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2049 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2050 struct v4l2_format *try_fmt)
2052 int fd = v4l2object->video_fd;
2053 struct v4l2_format fmt;
2056 memcpy (&fmt, try_fmt, sizeof (fmt));
2057 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2059 if (r < 0 && errno == ENOTTY) {
2060 /* The driver might not implement TRY_FMT, in which case we will try
2062 if (GST_V4L2_IS_ACTIVE (v4l2object))
2065 memcpy (&fmt, try_fmt, sizeof (fmt));
2066 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2068 memcpy (try_fmt, &fmt, sizeof (fmt));
2072 memcpy (try_fmt, &fmt, sizeof (fmt));
2073 GST_WARNING_OBJECT (v4l2object->element,
2074 "Unable to try format: %s", g_strerror (errno));
2080 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2081 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2083 struct v4l2_format fmt;
2084 GValue interlace_formats = { 0, };
2085 GstVideoInterlaceMode interlace_mode, prev = -1;
2087 const gchar *mode_strings[] = { "progressive",
2092 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2095 if (v4l2object->never_interlaced) {
2096 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2100 g_value_init (&interlace_formats, GST_TYPE_LIST);
2102 /* Try twice - once for NONE, once for INTERLACED. */
2103 memset (&fmt, 0, sizeof (fmt));
2104 fmt.type = v4l2object->type;
2105 fmt.fmt.pix.width = width;
2106 fmt.fmt.pix.height = height;
2107 fmt.fmt.pix.pixelformat = pixelformat;
2108 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2110 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2111 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2112 GValue interlace_enum = { 0, };
2113 g_value_init (&interlace_enum, G_TYPE_STRING);
2114 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2115 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2116 prev = interlace_mode;
2119 memset (&fmt, 0, sizeof (fmt));
2120 fmt.type = v4l2object->type;
2121 fmt.fmt.pix.width = width;
2122 fmt.fmt.pix.height = height;
2123 fmt.fmt.pix.pixelformat = pixelformat;
2124 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2126 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2127 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode) &&
2128 prev != interlace_mode) {
2129 GValue interlace_enum = { 0, };
2130 g_value_init (&interlace_enum, G_TYPE_STRING);
2131 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2132 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2135 gst_v4l2src_value_simplify (&interlace_formats);
2136 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2141 gst_v4l2_object_fill_colorimetry_list (GValue * list,
2142 GstVideoColorimetry * cinfo)
2144 GValue colorimetry = G_VALUE_INIT;
2147 gboolean found = FALSE;
2149 g_value_init (&colorimetry, G_TYPE_STRING);
2150 g_value_take_string (&colorimetry, gst_video_colorimetry_to_string (cinfo));
2152 /* only insert if no duplicate */
2153 size = gst_value_list_get_size (list);
2154 for (i = 0; i < size; i++) {
2157 tmp = gst_value_list_get_value (list, i);
2158 if (gst_value_compare (&colorimetry, tmp) == GST_VALUE_EQUAL) {
2165 gst_value_list_append_and_take_value (list, &colorimetry);
2167 g_value_unset (&colorimetry);
2171 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2172 guint32 width, guint32 height, guint32 pixelformat)
2174 struct v4l2_format fmt;
2175 GValue list = G_VALUE_INIT;
2176 GstVideoColorimetry cinfo;
2177 enum v4l2_colorspace req_cspace;
2178 gboolean is_rgb = gst_v4l2_object_v4l2fourcc_is_rgb (pixelformat);
2180 memset (&fmt, 0, sizeof (fmt));
2181 fmt.type = v4l2object->type;
2182 fmt.fmt.pix.width = width;
2183 fmt.fmt.pix.height = height;
2184 fmt.fmt.pix.pixelformat = pixelformat;
2186 g_value_init (&list, GST_TYPE_LIST);
2188 /* step 1: get device default colorspace and insert it first as
2189 * it should be the preferred one */
2190 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2191 enum v4l2_colorspace colorspace;
2192 enum v4l2_quantization range;
2193 enum v4l2_ycbcr_encoding matrix;
2194 enum v4l2_xfer_func transfer;
2196 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
2197 colorspace = fmt.fmt.pix_mp.colorspace;
2198 range = fmt.fmt.pix_mp.quantization;
2199 matrix = fmt.fmt.pix_mp.ycbcr_enc;
2200 transfer = fmt.fmt.pix_mp.xfer_func;
2202 colorspace = fmt.fmt.pix.colorspace;
2203 range = fmt.fmt.pix.quantization;
2204 matrix = fmt.fmt.pix.ycbcr_enc;
2205 transfer = fmt.fmt.pix.xfer_func;
2208 if (gst_v4l2_object_get_colorspace (colorspace, range, matrix, transfer,
2210 /* Set identity matrix for R'G'B' formats to avoid creating
2211 * confusion. This though is cosmetic as it's now properly ignored by
2212 * the video info API and videoconvert. */
2214 cinfo.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2216 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2220 /* step 2: probe all colorspace other than default
2221 * We don't probe all colorspace, range, matrix and transfer combination to
2222 * avoid ioctl flooding which could greatly increase initialization time
2223 * with low-speed devices (UVC...) */
2224 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2225 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++) {
2226 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2227 if (req_cspace == V4L2_COLORSPACE_BT878)
2230 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2231 fmt.fmt.pix_mp.colorspace = req_cspace;
2233 fmt.fmt.pix.colorspace = req_cspace;
2235 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2236 enum v4l2_colorspace colorspace;
2237 enum v4l2_quantization range;
2238 enum v4l2_ycbcr_encoding matrix;
2239 enum v4l2_xfer_func transfer;
2241 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
2242 colorspace = fmt.fmt.pix_mp.colorspace;
2243 range = fmt.fmt.pix_mp.quantization;
2244 matrix = fmt.fmt.pix_mp.ycbcr_enc;
2245 transfer = fmt.fmt.pix_mp.xfer_func;
2247 colorspace = fmt.fmt.pix.colorspace;
2248 range = fmt.fmt.pix.quantization;
2249 matrix = fmt.fmt.pix.ycbcr_enc;
2250 transfer = fmt.fmt.pix.xfer_func;
2253 if (colorspace == req_cspace) {
2254 if (gst_v4l2_object_get_colorspace (colorspace, range, matrix, transfer,
2256 /* Set identity matrix for R'G'B' formats to avoid creating
2257 * confusion. This though is cosmetic as it's now properly ignored by
2258 * the video info API and videoconvert. */
2260 cinfo.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2262 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2268 if (gst_value_list_get_size (&list) > 0)
2269 gst_structure_take_value (s, "colorimetry", &list);
2271 g_value_unset (&list);
2276 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2277 static GstStructure *
2278 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2279 guint32 pixelformat,
2280 guint32 width, guint32 height, const GstStructure * template)
2282 gint fd = v4l2object->video_fd;
2283 struct v4l2_frmivalenum ival;
2286 GValue rates = { 0, };
2288 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2290 ival.pixel_format = pixelformat;
2292 ival.height = height;
2294 GST_LOG_OBJECT (v4l2object->element,
2295 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2296 GST_FOURCC_ARGS (pixelformat));
2298 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2299 * fraction to get framerate */
2300 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2301 goto enum_frameintervals_failed;
2303 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2304 GValue rate = { 0, };
2306 g_value_init (&rates, GST_TYPE_LIST);
2307 g_value_init (&rate, GST_TYPE_FRACTION);
2310 num = ival.discrete.numerator;
2311 denom = ival.discrete.denominator;
2313 if (num > G_MAXINT || denom > G_MAXINT) {
2314 /* let us hope we don't get here... */
2319 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
2322 /* swap to get the framerate */
2323 gst_value_set_fraction (&rate, denom, num);
2324 gst_value_list_append_value (&rates, &rate);
2327 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2328 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2329 GValue min = { 0, };
2330 GValue step = { 0, };
2331 GValue max = { 0, };
2332 gboolean added = FALSE;
2333 guint32 minnum, mindenom;
2334 guint32 maxnum, maxdenom;
2336 g_value_init (&rates, GST_TYPE_LIST);
2338 g_value_init (&min, GST_TYPE_FRACTION);
2339 g_value_init (&step, GST_TYPE_FRACTION);
2340 g_value_init (&max, GST_TYPE_FRACTION);
2343 minnum = ival.stepwise.min.numerator;
2344 mindenom = ival.stepwise.min.denominator;
2345 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2349 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
2351 gst_value_set_fraction (&min, minnum, mindenom);
2354 maxnum = ival.stepwise.max.numerator;
2355 maxdenom = ival.stepwise.max.denominator;
2356 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2361 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
2363 gst_value_set_fraction (&max, maxnum, maxdenom);
2366 num = ival.stepwise.step.numerator;
2367 denom = ival.stepwise.step.denominator;
2368 if (num > G_MAXINT || denom > G_MAXINT) {
2373 if (num == 0 || denom == 0) {
2374 /* in this case we have a wrong fraction or no step, set the step to max
2375 * so that we only add the min value in the loop below */
2380 /* since we only have gst_value_fraction_subtract and not add, negate the
2382 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
2384 gst_value_set_fraction (&step, -num, denom);
2386 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2387 GValue rate = { 0, };
2389 num = gst_value_get_fraction_numerator (&min);
2390 denom = gst_value_get_fraction_denominator (&min);
2391 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
2394 /* invert to get the framerate */
2395 g_value_init (&rate, GST_TYPE_FRACTION);
2396 gst_value_set_fraction (&rate, denom, num);
2397 gst_value_list_append_value (&rates, &rate);
2400 /* we're actually adding because step was negated above. This is because
2401 * there is no _add function... */
2402 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2403 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
2408 /* no range was added, leave the default range from the template */
2409 GST_WARNING_OBJECT (v4l2object->element,
2410 "no range added, leaving default");
2411 g_value_unset (&rates);
2413 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2414 guint32 maxnum, maxdenom;
2416 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2418 num = ival.stepwise.min.numerator;
2419 denom = ival.stepwise.min.denominator;
2420 if (num > G_MAXINT || denom > G_MAXINT) {
2425 maxnum = ival.stepwise.max.numerator;
2426 maxdenom = ival.stepwise.max.denominator;
2427 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2432 GST_LOG_OBJECT (v4l2object->element,
2433 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2436 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2442 s = gst_structure_copy (template);
2443 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2444 "height", G_TYPE_INT, (gint) height, NULL);
2445 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2446 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2448 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2450 if (G_IS_VALUE (&rates)) {
2451 gst_v4l2src_value_simplify (&rates);
2452 /* only change the framerate on the template when we have a valid probed new
2454 gst_structure_take_value (s, "framerate", &rates);
2455 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2456 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2457 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2463 enum_frameintervals_failed:
2465 GST_DEBUG_OBJECT (v4l2object->element,
2466 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2467 GST_FOURCC_ARGS (pixelformat), width, height);
2472 /* I don't see how this is actually an error, we ignore the format then */
2473 GST_WARNING_OBJECT (v4l2object->element,
2474 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2475 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2481 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2485 gst_structure_get_int (s1, "width", &w1);
2486 gst_structure_get_int (s1, "height", &h1);
2487 gst_structure_get_int (s2, "width", &w2);
2488 gst_structure_get_int (s2, "height", &h2);
2490 /* I think it's safe to assume that this won't overflow for a while */
2491 return ((w2 * h2) - (w1 * h1));
2495 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2496 guint32 format, GstCaps * caps, GstStructure * s)
2498 GstStructure *alt_s = NULL;
2500 /* Encoded stream on output buffer need to be parsed */
2501 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2502 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2505 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2506 if (format == gst_v4l2_formats[i].format &&
2507 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2508 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2509 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2515 if (v4l2object->has_alpha_component &&
2516 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2517 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2519 case V4L2_PIX_FMT_RGB32:
2520 alt_s = gst_structure_copy (s);
2521 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2523 case V4L2_PIX_FMT_BGR32:
2524 alt_s = gst_structure_copy (s);
2525 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2532 gst_caps_append_structure (caps, s);
2535 gst_caps_append_structure (caps, alt_s);
2539 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2540 guint32 pixelformat, const GstStructure * template)
2542 GstCaps *ret = gst_caps_new_empty ();
2544 gint fd = v4l2object->video_fd;
2545 struct v4l2_frmsizeenum size;
2546 GList *results = NULL;
2549 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2550 gst_caps_append_structure (ret, gst_structure_copy (template));
2554 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2556 size.pixel_format = pixelformat;
2558 GST_DEBUG_OBJECT (v4l2object->element,
2559 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2560 GST_FOURCC_ARGS (pixelformat));
2562 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2563 goto enum_framesizes_failed;
2565 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2567 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
2568 size.discrete.width, size.discrete.height);
2570 w = MIN (size.discrete.width, G_MAXINT);
2571 h = MIN (size.discrete.height, G_MAXINT);
2575 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2576 pixelformat, w, h, template);
2579 results = g_list_prepend (results, tmp);
2583 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2584 GST_DEBUG_OBJECT (v4l2object->element,
2585 "done iterating discrete frame sizes");
2586 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2587 guint32 maxw, maxh, step_w, step_h;
2589 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
2590 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2591 size.stepwise.min_width);
2592 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2593 size.stepwise.min_height);
2594 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2595 size.stepwise.max_width);
2596 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2597 size.stepwise.max_height);
2598 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
2599 size.stepwise.step_width);
2600 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
2601 size.stepwise.step_height);
2603 w = MAX (size.stepwise.min_width, 1);
2604 h = MAX (size.stepwise.min_height, 1);
2605 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2606 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2608 step_w = MAX (size.stepwise.step_width, 1);
2609 step_h = MAX (size.stepwise.step_height, 1);
2611 /* FIXME: check for sanity and that min/max are multiples of the steps */
2613 /* we only query details for the max width/height since it's likely the
2614 * most restricted if there are any resolution-dependent restrictions */
2615 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2616 pixelformat, maxw, maxh, template);
2619 GValue step_range = G_VALUE_INIT;
2621 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2622 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2623 gst_structure_set_value (tmp, "width", &step_range);
2625 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2626 gst_structure_take_value (tmp, "height", &step_range);
2628 /* no point using the results list here, since there's only one struct */
2629 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2631 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2634 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
2635 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2636 size.stepwise.min_width);
2637 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2638 size.stepwise.min_height);
2639 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2640 size.stepwise.max_width);
2641 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2642 size.stepwise.max_height);
2644 w = MAX (size.stepwise.min_width, 1);
2645 h = MAX (size.stepwise.min_height, 1);
2646 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2647 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2650 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2653 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2654 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2657 /* no point using the results list here, since there's only one struct */
2658 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2664 /* we use an intermediary list to store and then sort the results of the
2665 * probing because we can't make any assumptions about the order in which
2666 * the driver will give us the sizes, but we want the final caps to contain
2667 * the results starting with the highest resolution and having the lowest
2668 * resolution last, since order in caps matters for things like fixation. */
2669 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2670 while (results != NULL) {
2671 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
2673 results = g_list_delete_link (results, results);
2676 if (gst_caps_is_empty (ret))
2677 goto enum_framesizes_no_results;
2682 enum_framesizes_failed:
2684 /* I don't see how this is actually an error */
2685 GST_DEBUG_OBJECT (v4l2object->element,
2686 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2687 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2688 goto default_frame_sizes;
2690 enum_framesizes_no_results:
2692 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2693 * question doesn't actually support it yet */
2694 GST_DEBUG_OBJECT (v4l2object->element,
2695 "No results for pixelformat %" GST_FOURCC_FORMAT
2696 " enumerating frame sizes, trying fallback",
2697 GST_FOURCC_ARGS (pixelformat));
2698 goto default_frame_sizes;
2702 GST_WARNING_OBJECT (v4l2object->element,
2703 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2704 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2705 goto default_frame_sizes;
2708 default_frame_sizes:
2710 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2712 /* This code is for Linux < 2.6.19 */
2714 max_w = max_h = GST_V4L2_MAX_SIZE;
2715 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2717 GST_WARNING_OBJECT (v4l2object->element,
2718 "Could not probe minimum capture size for pixelformat %"
2719 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2721 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2723 GST_WARNING_OBJECT (v4l2object->element,
2724 "Could not probe maximum capture size for pixelformat %"
2725 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2728 /* Since we can't get framerate directly, try to use the current norm */
2729 if (v4l2object->tv_norm && v4l2object->norms) {
2731 GstTunerNorm *norm = NULL;
2732 GstTunerNorm *current =
2733 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2735 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2736 norm = (GstTunerNorm *) norms->data;
2737 if (!strcmp (norm->label, current->label))
2740 /* If it's possible, set framerate to that (discrete) value */
2742 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2743 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2747 tmp = gst_structure_copy (template);
2749 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2751 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2752 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2753 /* if norm can't be used, copy the template framerate */
2754 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2759 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2761 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2764 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2766 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2768 /* We could consider setting interlace mode from min and max. */
2769 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
2771 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2772 /* We could consider to check colorspace for min too, in case it depends on
2773 * the size. But in this case, min and max could not be enough */
2774 gst_v4l2_object_add_colorspace (v4l2object, tmp, max_w, max_h, pixelformat);
2776 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2782 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2783 guint32 pixelformat, gint * width, gint * height)
2785 struct v4l2_format fmt;
2786 gboolean ret = FALSE;
2787 GstVideoInterlaceMode interlace_mode;
2789 g_return_val_if_fail (width != NULL, FALSE);
2790 g_return_val_if_fail (height != NULL, FALSE);
2792 GST_LOG_OBJECT (v4l2object->element,
2793 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2794 *width, *height, GST_FOURCC_ARGS (pixelformat));
2796 memset (&fmt, 0, sizeof (struct v4l2_format));
2798 /* get size delimiters */
2799 memset (&fmt, 0, sizeof (fmt));
2800 fmt.type = v4l2object->type;
2801 fmt.fmt.pix.width = *width;
2802 fmt.fmt.pix.height = *height;
2803 fmt.fmt.pix.pixelformat = pixelformat;
2804 fmt.fmt.pix.field = V4L2_FIELD_ANY;
2806 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
2809 GST_LOG_OBJECT (v4l2object->element,
2810 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2812 *width = fmt.fmt.pix.width;
2813 *height = fmt.fmt.pix.height;
2815 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2816 GST_WARNING_OBJECT (v4l2object->element,
2817 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
2818 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
2826 GST_WARNING_OBJECT (v4l2object->element,
2827 "Unable to try format: %s", g_strerror (errno));
2834 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2838 GST_DEBUG_OBJECT (v4l2object->element, "initializing the %s system",
2839 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
2841 GST_V4L2_CHECK_OPEN (v4l2object);
2842 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2844 /* find transport */
2845 mode = v4l2object->req_mode;
2847 if (v4l2object->vcap.capabilities & V4L2_CAP_READWRITE) {
2848 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2849 mode = GST_V4L2_IO_RW;
2850 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2851 goto method_not_supported;
2853 if (v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
2854 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2855 mode = GST_V4L2_IO_MMAP;
2856 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2857 goto method_not_supported;
2859 /* if still no transport selected, error out */
2860 if (mode == GST_V4L2_IO_AUTO)
2861 goto no_supported_capture_method;
2863 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2864 v4l2object->mode = mode;
2866 /* If min_buffers is not set, the driver either does not support the control or
2867 it has not been asked yet via propose_allocation/decide_allocation. */
2868 if (!v4l2object->min_buffers)
2869 gst_v4l2_get_driver_min_buffers (v4l2object);
2871 /* Map the buffers */
2872 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2874 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2875 goto buffer_pool_new_failed;
2877 GST_V4L2_SET_ACTIVE (v4l2object);
2882 buffer_pool_new_failed:
2884 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2885 (_("Could not map buffers from device '%s'"),
2886 v4l2object->videodev),
2887 ("Failed to create buffer pool: %s", g_strerror (errno)));
2890 method_not_supported:
2892 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2893 (_("The driver of device '%s' does not support the IO method %d"),
2894 v4l2object->videodev, mode), (NULL));
2897 no_supported_capture_method:
2899 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2900 (_("The driver of device '%s' does not support any known IO "
2901 "method."), v4l2object->videodev), (NULL));
2907 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
2908 gint plane, gint stride)
2910 const GstVideoFormatInfo *finfo = info->finfo;
2912 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2913 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
2916 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
2917 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2918 tile_height = 1 << hs;
2920 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
2921 info->height + align->padding_top + align->padding_bottom);
2922 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
2924 x_tiles = stride >> ws;
2925 y_tiles = padded_height >> hs;
2926 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
2928 info->stride[plane] = stride;
2933 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
2934 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
2936 const GstVideoFormatInfo *finfo = info->finfo;
2937 gint i, estride, padded_height;
2940 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
2942 padded_height = info->height + align->padding_top + align->padding_bottom;
2944 for (i = 0; i < finfo->n_planes; i++) {
2945 estride = gst_v4l2_object_extrapolate_stride (finfo, i, stride);
2947 gst_v4l2_object_set_stride (info, align, i, estride);
2949 info->offset[i] = offs;
2951 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
2953 GST_DEBUG_OBJECT (v4l2object->element,
2954 "Extrapolated for plane %d with base stride %d: "
2955 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
2961 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
2962 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
2963 GstVideoInfo * info, GstVideoAlignment * align)
2965 const GstVideoFormatInfo *finfo = info->finfo;
2966 gboolean standard_stride = TRUE;
2967 gint stride, padded_width, padded_height, i;
2969 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
2970 v4l2object->n_v4l2_planes = 1;
2971 info->size = format->fmt.pix.sizeimage;
2975 /* adjust right padding */
2976 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2977 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
2979 stride = format->fmt.pix.bytesperline;
2981 padded_width = stride / GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
2983 if (padded_width < format->fmt.pix.width)
2984 GST_WARNING_OBJECT (v4l2object->element,
2985 "Driver bug detected, stride is too small for the width");
2987 align->padding_right = padded_width - info->width - align->padding_left;
2989 /* adjust bottom padding */
2990 padded_height = format->fmt.pix.height;
2992 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2993 guint hs, tile_height;
2995 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2996 tile_height = 1 << hs;
2998 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3001 align->padding_bottom = padded_height - info->height - align->padding_top;
3003 /* setup the strides and offset */
3004 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
3005 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
3007 /* figure out the frame layout */
3008 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
3010 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
3011 stride = pix_mp->plane_fmt[i].bytesperline;
3013 if (info->stride[i] != stride)
3014 standard_stride = FALSE;
3016 gst_v4l2_object_set_stride (info, align, i, stride);
3017 info->offset[i] = info->size;
3018 info->size += pix_mp->plane_fmt[i].sizeimage;
3021 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3022 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
3023 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3024 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3027 /* only one plane in non-MPLANE mode */
3028 v4l2object->n_v4l2_planes = 1;
3029 info->size = format->fmt.pix.sizeimage;
3030 stride = format->fmt.pix.bytesperline;
3032 if (info->stride[0] != stride)
3033 standard_stride = FALSE;
3035 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3038 /* adjust the offset to take into account left and top */
3039 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3040 if ((align->padding_left + align->padding_top) > 0)
3041 GST_WARNING_OBJECT (v4l2object->element,
3042 "Left and top padding is not permitted for tiled formats");
3044 for (i = 0; i < finfo->n_planes; i++) {
3047 /* FIXME we assume plane as component as this is true for all supported
3048 * format we support. */
3050 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left);
3051 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top);
3053 info->offset[i] += (vedge * info->stride[i]) +
3054 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i));
3059 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %" G_GSIZE_FORMAT,
3062 /* to avoid copies we need video meta if top or left padding */
3063 v4l2object->need_video_meta =
3064 ((align->padding_top + align->padding_left) != 0);
3066 /* ... or if stride is non "standard" */
3067 if (!standard_stride)
3068 v4l2object->need_video_meta = TRUE;
3070 /* ... or also video meta if we use multiple, non-contiguous, planes */
3071 if (v4l2object->n_v4l2_planes > 1)
3072 v4l2object->need_video_meta = TRUE;
3074 v4l2object->info = *info;
3075 v4l2object->align = *align;
3076 v4l2object->format = *format;
3077 v4l2object->fmtdesc = fmtdesc;
3079 /* if we have a framerate pre-calculate duration */
3080 if (info->fps_n > 0 && info->fps_d > 0) {
3081 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
3084 v4l2object->duration = GST_CLOCK_TIME_NONE;
3089 gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
3090 gint plane, gint stride)
3094 switch (finfo->format) {
3095 case GST_VIDEO_FORMAT_NV12:
3096 case GST_VIDEO_FORMAT_NV12_64Z32:
3097 case GST_VIDEO_FORMAT_NV21:
3098 case GST_VIDEO_FORMAT_NV16:
3099 case GST_VIDEO_FORMAT_NV61:
3100 case GST_VIDEO_FORMAT_NV24:
3101 estride = (plane == 0 ? 1 : 2) *
3102 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3105 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3113 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3116 gint fd = v4l2object->video_fd;
3117 struct v4l2_format format;
3118 struct v4l2_streamparm streamparm;
3119 enum v4l2_field field;
3120 guint32 pixelformat;
3121 struct v4l2_fmtdesc *fmtdesc;
3123 GstVideoAlignment align;
3124 gint width, height, fps_n, fps_d;
3128 enum v4l2_colorspace colorspace = 0;
3129 enum v4l2_quantization range = 0;
3130 enum v4l2_ycbcr_encoding matrix = 0;
3131 enum v4l2_xfer_func transfer = 0;
3133 GST_V4L2_CHECK_OPEN (v4l2object);
3135 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3137 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3139 gst_video_info_init (&info);
3140 gst_video_alignment_reset (&align);
3142 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3145 pixelformat = fmtdesc->pixelformat;
3146 width = GST_VIDEO_INFO_WIDTH (&info);
3147 height = GST_VIDEO_INFO_HEIGHT (&info);
3148 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3149 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3151 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3152 * or if contiguous is prefered */
3153 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3154 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3157 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
3158 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
3159 /* ideally we would differentiate between types of interlaced video
3160 * but there is not sufficient information in the caps..
3162 field = V4L2_FIELD_INTERLACED;
3164 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
3165 field = V4L2_FIELD_NONE;
3168 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3169 /* We first pick th main colorspace from the primaries */
3170 switch (info.colorimetry.primaries) {
3171 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3172 /* There is two colorspaces using these primaries, use the range to
3174 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3175 colorspace = V4L2_COLORSPACE_REC709;
3177 colorspace = V4L2_COLORSPACE_SRGB;
3179 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3180 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3182 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3183 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3185 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3186 colorspace = V4L2_COLORSPACE_SMPTE170M;
3188 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3189 colorspace = V4L2_COLORSPACE_SMPTE240M;
3192 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3193 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3194 /* We don't know, we will guess */
3198 GST_WARNING_OBJECT (v4l2object->element,
3199 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3203 switch (info.colorimetry.range) {
3204 case GST_VIDEO_COLOR_RANGE_0_255:
3205 range = V4L2_QUANTIZATION_FULL_RANGE;
3207 case GST_VIDEO_COLOR_RANGE_16_235:
3208 range = V4L2_QUANTIZATION_LIM_RANGE;
3210 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3211 /* We let the driver pick a default one */
3214 GST_WARNING_OBJECT (v4l2object->element,
3215 "Unknown colorimetry range %d", info.colorimetry.range);
3219 switch (info.colorimetry.matrix) {
3220 case GST_VIDEO_COLOR_MATRIX_RGB:
3221 /* Unspecified, leave to default */
3223 /* FCC is about the same as BT601 with less digit */
3224 case GST_VIDEO_COLOR_MATRIX_FCC:
3225 case GST_VIDEO_COLOR_MATRIX_BT601:
3226 matrix = V4L2_YCBCR_ENC_601;
3228 case GST_VIDEO_COLOR_MATRIX_BT709:
3229 matrix = V4L2_YCBCR_ENC_709;
3231 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3232 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3234 case GST_VIDEO_COLOR_MATRIX_BT2020:
3235 matrix = V4L2_YCBCR_ENC_BT2020;
3237 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3238 /* We let the driver pick a default one */
3241 GST_WARNING_OBJECT (v4l2object->element,
3242 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3246 switch (info.colorimetry.transfer) {
3247 case GST_VIDEO_TRANSFER_GAMMA18:
3248 case GST_VIDEO_TRANSFER_GAMMA20:
3249 case GST_VIDEO_TRANSFER_GAMMA22:
3250 case GST_VIDEO_TRANSFER_GAMMA28:
3251 GST_WARNING_OBJECT (v4l2object->element,
3252 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3254 case GST_VIDEO_TRANSFER_GAMMA10:
3255 transfer = V4L2_XFER_FUNC_NONE;
3257 case GST_VIDEO_TRANSFER_BT709:
3258 transfer = V4L2_XFER_FUNC_709;
3260 case GST_VIDEO_TRANSFER_SMPTE240M:
3261 transfer = V4L2_XFER_FUNC_SMPTE240M;
3263 case GST_VIDEO_TRANSFER_SRGB:
3264 transfer = V4L2_XFER_FUNC_SRGB;
3266 case GST_VIDEO_TRANSFER_LOG100:
3267 case GST_VIDEO_TRANSFER_LOG316:
3268 GST_WARNING_OBJECT (v4l2object->element,
3269 "LOG 100, 316 transfer functions not supported");
3270 /* FIXME No known sensible default, maybe AdobeRGB ? */
3272 case GST_VIDEO_TRANSFER_UNKNOWN:
3273 /* We let the driver pick a default one */
3276 GST_WARNING_OBJECT (v4l2object->element,
3277 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
3281 if (colorspace == 0) {
3282 /* Try to guess colorspace according to pixelformat and size */
3283 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3284 /* SD streams likely use SMPTE170M and HD streams REC709 */
3285 if (width <= 720 && height <= 576)
3286 colorspace = V4L2_COLORSPACE_SMPTE170M;
3288 colorspace = V4L2_COLORSPACE_REC709;
3289 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3290 colorspace = V4L2_COLORSPACE_SRGB;
3291 transfer = V4L2_XFER_FUNC_NONE;
3296 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
3297 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3298 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3300 memset (&format, 0x00, sizeof (struct v4l2_format));
3301 format.type = v4l2object->type;
3304 format.type = v4l2object->type;
3305 format.fmt.pix_mp.pixelformat = pixelformat;
3306 format.fmt.pix_mp.width = width;
3307 format.fmt.pix_mp.height = height;
3308 format.fmt.pix_mp.field = field;
3309 format.fmt.pix_mp.num_planes = n_v4l_planes;
3311 /* try to ask our prefered stride but it's not a failure if not
3313 for (i = 0; i < n_v4l_planes; i++) {
3314 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3316 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3317 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3318 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3320 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3323 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3324 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3326 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3328 format.type = v4l2object->type;
3329 format.fmt.pix.width = width;
3330 format.fmt.pix.height = height;
3331 format.fmt.pix.pixelformat = pixelformat;
3332 format.fmt.pix.field = field;
3334 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3335 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3336 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3338 /* try to ask our prefered stride */
3339 format.fmt.pix.bytesperline = stride;
3341 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3342 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3345 GST_DEBUG_OBJECT (v4l2object->element, "Desired format is %dx%d, format "
3346 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3347 format.fmt.pix_mp.height,
3348 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3349 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3351 #ifndef GST_DISABLE_GST_DEBUG
3353 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3354 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3355 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3357 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3358 format.fmt.pix.bytesperline);
3362 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3364 format.fmt.pix_mp.colorspace = colorspace;
3365 format.fmt.pix_mp.quantization = range;
3366 format.fmt.pix_mp.ycbcr_enc = matrix;
3367 format.fmt.pix_mp.xfer_func = transfer;
3369 format.fmt.pix.colorspace = colorspace;
3370 format.fmt.pix.quantization = range;
3371 format.fmt.pix.ycbcr_enc = matrix;
3372 format.fmt.pix.xfer_func = transfer;
3375 GST_DEBUG_OBJECT (v4l2object->element, "Desired colorspace is %d:%d:%d:%d",
3376 colorspace, range, matrix, transfer);
3380 if (v4l2_ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3381 goto try_fmt_failed;
3383 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3384 goto set_fmt_failed;
3387 GST_DEBUG_OBJECT (v4l2object->element, "Got format of %dx%d, format "
3388 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d",
3389 format.fmt.pix.width, format.fmt.pix_mp.height,
3390 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3391 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3392 is_mplane ? format.fmt.pix_mp.colorspace : format.fmt.pix.colorspace);
3394 #ifndef GST_DISABLE_GST_DEBUG
3396 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3397 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3398 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3399 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3401 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3402 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3406 if (format.fmt.pix.pixelformat != pixelformat)
3407 goto invalid_pixelformat;
3409 /* Only negotiate size with raw data.
3410 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3411 * in ASF mode for example, there is also not reason for a driver to
3412 * change the size. */
3413 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3414 /* We can crop larger images */
3415 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3416 goto invalid_dimensions;
3418 /* Note, this will be adjusted if upstream has non-centered cropping. */
3419 align.padding_top = 0;
3420 align.padding_bottom = format.fmt.pix.height - height;
3421 align.padding_left = 0;
3422 align.padding_right = format.fmt.pix.width - width;
3425 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3426 goto invalid_planes;
3428 if (try_only) /* good enough for trying only */
3431 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
3432 struct v4l2_control ctl = { 0, };
3433 ctl.id = V4L2_CID_ALPHA_COMPONENT;
3436 if (v4l2_ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
3437 GST_WARNING_OBJECT (v4l2object->element,
3438 "Failed to set alpha component value");
3441 /* Is there a reason we require the caller to always specify a framerate? */
3442 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
3445 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
3446 streamparm.type = v4l2object->type;
3448 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
3449 goto get_parm_failed;
3451 GST_VIDEO_INFO_FPS_N (&info) =
3452 streamparm.parm.capture.timeperframe.denominator;
3453 GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
3455 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
3456 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3457 GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
3458 streamparm.parm.capture.timeperframe.denominator,
3459 streamparm.parm.capture.timeperframe.numerator);
3461 /* We used to skip frame rate setup if the camera was already setup
3462 * with the requested frame rate. This breaks some cameras though,
3463 * causing them to not output data (several models of Thinkpad cameras
3464 * have this problem at least).
3465 * So, don't skip. */
3466 GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
3468 /* We want to change the frame rate, so check whether we can. Some cheap USB
3469 * cameras don't have the capability */
3470 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3471 GST_DEBUG_OBJECT (v4l2object->element,
3472 "Not setting framerate (not supported)");
3476 /* Note: V4L2 wants the frame interval, we have the frame rate */
3477 streamparm.parm.capture.timeperframe.numerator = fps_d;
3478 streamparm.parm.capture.timeperframe.denominator = fps_n;
3480 /* some cheap USB cam's won't accept any change */
3481 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3482 goto set_parm_failed;
3484 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
3485 streamparm.parm.capture.timeperframe.denominator > 0) {
3486 /* get new values */
3487 fps_d = streamparm.parm.capture.timeperframe.numerator;
3488 fps_n = streamparm.parm.capture.timeperframe.denominator;
3490 GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
3493 /* fix v4l2 capture driver to provide framerate values */
3494 GST_WARNING_OBJECT (v4l2object->element,
3495 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
3498 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3499 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3503 /* add boolean return, so we can fail on drivers bugs */
3504 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
3506 /* now configure the pool */
3507 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
3515 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
3521 if (errno == EBUSY) {
3522 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, BUSY,
3523 (_("Device '%s' is busy"), v4l2object->videodev),
3524 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3525 GST_FOURCC_ARGS (pixelformat), width, height,
3526 g_strerror (errno)));
3532 if (errno == EBUSY) {
3533 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, BUSY,
3534 (_("Device '%s' is busy"), v4l2object->videodev),
3535 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3536 GST_FOURCC_ARGS (pixelformat), width, height,
3537 g_strerror (errno)));
3539 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3540 (_("Device '%s' cannot capture at %dx%d"),
3541 v4l2object->videodev, width, height),
3542 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3543 GST_FOURCC_ARGS (pixelformat), width, height,
3544 g_strerror (errno)));
3551 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3552 (_("Device '%s' cannot capture at %dx%d"),
3553 v4l2object->videodev, width, height),
3554 ("Tried to capture at %dx%d, but device returned size %dx%d",
3555 width, height, format.fmt.pix.width, format.fmt.pix.height));
3559 invalid_pixelformat:
3562 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3563 (_("Device '%s' cannot capture in the specified format"),
3564 v4l2object->videodev),
3565 ("Tried to capture in %" GST_FOURCC_FORMAT
3566 ", but device returned format" " %" GST_FOURCC_FORMAT,
3567 GST_FOURCC_ARGS (pixelformat),
3568 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
3575 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3576 (_("Device '%s' does support non-contiguous planes"),
3577 v4l2object->videodev),
3578 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
3584 /* it's possible that this call is not supported */
3585 if (errno != EINVAL && errno != ENOTTY) {
3586 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3587 (_("Could not get parameters on device '%s'"),
3588 v4l2object->videodev), GST_ERROR_SYSTEM);
3594 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3595 (_("Video device did not accept new frame rate setting.")),
3601 /* setup_pool already send the error */
3607 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps)
3609 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE);
3613 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps)
3615 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE);
3619 * gst_v4l2_object_acquire_format:
3620 * @v4l2object the object
3621 * @info a GstVideoInfo to be filled
3623 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
3624 * the output format is choosen by the HW.
3626 * Returns: %TRUE on success, %FALSE on failure.
3629 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
3631 struct v4l2_fmtdesc *fmtdesc;
3632 struct v4l2_format fmt;
3633 struct v4l2_crop crop;
3634 GstVideoFormat format;
3635 guint width, height;
3636 GstVideoAlignment align;
3638 gst_video_info_init (info);
3639 gst_video_alignment_reset (&align);
3641 memset (&fmt, 0x00, sizeof (struct v4l2_format));
3642 fmt.type = v4l2object->type;
3643 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
3644 goto get_fmt_failed;
3646 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
3647 fmt.fmt.pix.pixelformat);
3648 if (fmtdesc == NULL)
3649 goto unsupported_format;
3651 /* No need to care about mplane, the four first params are the same */
3652 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
3654 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
3655 if (format == GST_VIDEO_FORMAT_UNKNOWN)
3656 goto unsupported_format;
3658 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
3659 goto invalid_dimensions;
3661 width = fmt.fmt.pix.width;
3662 height = fmt.fmt.pix.height;
3664 memset (&crop, 0, sizeof (struct v4l2_crop));
3665 crop.type = v4l2object->type;
3666 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0) {
3667 align.padding_left = crop.c.left;
3668 align.padding_top = crop.c.top;
3669 align.padding_right = width - crop.c.width - crop.c.left;
3670 align.padding_bottom = height - crop.c.height - crop.c.top;
3671 width = crop.c.width;
3672 height = crop.c.height;
3675 gst_video_info_set_format (info, format, width, height);
3677 switch (fmt.fmt.pix.field) {
3678 case V4L2_FIELD_ANY:
3679 case V4L2_FIELD_NONE:
3680 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
3682 case V4L2_FIELD_INTERLACED:
3683 case V4L2_FIELD_INTERLACED_TB:
3684 case V4L2_FIELD_INTERLACED_BT:
3685 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
3688 goto unsupported_field;
3691 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
3693 /* Shall we setup the pool ? */
3699 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3700 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
3705 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3706 (_("Video device returned invalid dimensions.")),
3707 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
3708 fmt.fmt.pix.height));
3713 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3714 (_("Video devices uses an unsupported interlacing method.")),
3715 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
3720 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3721 (_("Video devices uses an unsupported pixel format.")),
3722 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
3723 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
3729 gst_v4l2_object_set_crop (GstV4l2Object * obj)
3731 struct v4l2_crop crop = { 0 };
3733 crop.type = obj->type;
3734 crop.c.left = obj->align.padding_left;
3735 crop.c.top = obj->align.padding_top;
3736 crop.c.width = obj->info.width;
3737 crop.c.height = obj->info.height;
3739 if (obj->align.padding_left + obj->align.padding_top +
3740 obj->align.padding_right + obj->align.padding_bottom == 0) {
3741 GST_DEBUG_OBJECT (obj->element, "no cropping needed");
3745 GST_DEBUG_OBJECT (obj->element,
3746 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3747 crop.c.width, crop.c.height);
3749 if (v4l2_ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
3750 GST_WARNING_OBJECT (obj->element, "VIDIOC_S_CROP failed");
3754 if (v4l2_ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
3755 GST_WARNING_OBJECT (obj->element, "VIDIOC_G_CROP failed");
3759 GST_DEBUG_OBJECT (obj->element,
3760 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3761 crop.c.width, crop.c.height);
3767 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
3769 GstStructure *config;
3773 if (!v4l2object->pool)
3776 config = gst_buffer_pool_get_config (v4l2object->pool);
3777 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
3779 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
3781 gst_structure_free (config);
3787 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
3789 gboolean ret = TRUE;
3791 GST_LOG_OBJECT (v4l2object->element, "start flushing");
3793 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3794 gst_buffer_pool_set_flushing (v4l2object->pool, TRUE);
3800 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
3802 gboolean ret = TRUE;
3804 GST_LOG_OBJECT (v4l2object->element, "stop flushing");
3806 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3807 gst_buffer_pool_set_flushing (v4l2object->pool, FALSE);
3813 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
3815 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
3817 if (!GST_V4L2_IS_OPEN (v4l2object))
3819 if (!GST_V4L2_IS_ACTIVE (v4l2object))
3822 if (v4l2object->pool) {
3823 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
3824 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
3825 gst_object_unref (v4l2object->pool);
3826 v4l2object->pool = NULL;
3829 GST_V4L2_SET_INACTIVE (v4l2object);
3836 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3842 formats = gst_v4l2_object_get_format_list (v4l2object);
3844 ret = gst_caps_new_empty ();
3846 for (walk = formats; walk; walk = walk->next) {
3847 struct v4l2_fmtdesc *format;
3848 GstStructure *template;
3850 format = (struct v4l2_fmtdesc *) walk->data;
3852 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
3857 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
3858 format->pixelformat, template);
3860 gst_caps_append (ret, tmp);
3862 gst_structure_free (template);
3864 GST_DEBUG_OBJECT (v4l2object->element, "unknown format %u",
3865 format->pixelformat);
3870 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
3872 ret = gst_caps_ref (ret);
3879 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3883 if (v4l2object->probed_caps == NULL)
3884 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
3887 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
3888 GST_CAPS_INTERSECT_FIRST);
3890 ret = gst_caps_ref (v4l2object->probed_caps);
3893 GST_INFO_OBJECT (v4l2object->element, "probed caps: %" GST_PTR_FORMAT, ret);
3894 LOG_CAPS (v4l2object->element, ret);
3900 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
3903 GstBufferPool *pool = NULL, *other_pool = NULL;
3904 GstStructure *config;
3905 guint size, min, max, own_min = 0;
3907 gboolean has_video_meta;
3908 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
3909 GstAllocator *allocator = NULL;
3910 GstAllocationParams params = { 0 };
3912 GST_DEBUG_OBJECT (obj->element, "decide allocation");
3914 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3915 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
3917 gst_query_parse_allocation (query, &caps, NULL);
3919 if (obj->pool == NULL) {
3920 if (!gst_v4l2_object_setup_pool (obj, caps))
3924 if (gst_query_get_n_allocation_params (query) > 0)
3925 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3927 if (gst_query_get_n_allocation_pools (query) > 0) {
3928 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3937 GST_DEBUG_OBJECT (obj->element, "allocation: size:%u min:%u max:%u pool:%"
3938 GST_PTR_FORMAT, size, min, max, pool);
3941 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
3943 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
3945 gst_v4l2_get_driver_min_buffers (obj);
3946 /* We can't share our own pool, if it exceed V4L2 capacity */
3947 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
3948 can_share_own_pool = FALSE;
3951 switch (obj->mode) {
3952 case GST_V4L2_IO_RW:
3954 /* in READ/WRITE mode, prefer a downstream pool because our own pool
3955 * doesn't help much, we have to write to it as well */
3956 GST_DEBUG_OBJECT (obj->element,
3957 "read/write mode: using downstream pool");
3958 /* use the bigest size, when we use our own pool we can't really do any
3959 * other size than what the hardware gives us but for downstream pools
3961 size = MAX (size, obj->info.size);
3962 } else if (can_share_own_pool) {
3963 /* no downstream pool, use our own then */
3964 GST_DEBUG_OBJECT (obj->element,
3965 "read/write mode: no downstream pool, using our own");
3966 pool = gst_object_ref (obj->pool);
3967 size = obj->info.size;
3968 pushing_from_our_pool = TRUE;
3972 case GST_V4L2_IO_USERPTR:
3973 case GST_V4L2_IO_DMABUF_IMPORT:
3974 /* in importing mode, prefer our own pool, and pass the other pool to
3975 * our own, so it can serve itself */
3977 goto no_downstream_pool;
3978 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj->pool),
3981 gst_object_unref (pool);
3982 pool = gst_object_ref (obj->pool);
3983 size = obj->info.size;
3986 case GST_V4L2_IO_MMAP:
3987 case GST_V4L2_IO_DMABUF:
3988 /* in streaming mode, prefer our own pool */
3989 /* Check if we can use it ... */
3990 if (can_share_own_pool) {
3992 gst_object_unref (pool);
3993 pool = gst_object_ref (obj->pool);
3994 size = obj->info.size;
3995 GST_DEBUG_OBJECT (obj->element,
3996 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
3997 pushing_from_our_pool = TRUE;
3999 GST_DEBUG_OBJECT (obj->element,
4000 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
4003 GST_DEBUG_OBJECT (obj->element,
4004 "streaming mode: no usable pool, copying to generic pool");
4005 size = MAX (size, obj->info.size);
4008 case GST_V4L2_IO_AUTO:
4010 GST_WARNING_OBJECT (obj->element, "unhandled mode");
4017 /* If pushing from our own pool, configure it with queried minimum,
4018 * otherwise use the minimum required */
4019 if (pushing_from_our_pool) {
4020 /* When pushing from our own pool, we need what downstream one, to be able
4021 * to fill the pipeline, the minimum required to decoder according to the
4022 * driver and 1 more, so we don't endup up with everything downstream or
4023 * held by the decoder. */
4024 own_min = min + obj->min_buffers + 1;
4026 /* If no allocation parameters where provided, allow for a little more
4027 * buffers and enable copy threshold */
4030 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4033 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4038 /* In this case we'll have to configure two buffer pool. For our buffer
4039 * pool, we'll need what the driver one, and one more, so we can dequeu */
4040 own_min = obj->min_buffers + 1;
4041 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS);
4043 /* for the downstream pool, we keep what downstream wants, though ensure
4044 * at least a minimum if downstream didn't suggest anything (we are
4045 * expecting the base class to create a default one for the context) */
4046 min = MAX (min, GST_V4L2_MIN_BUFFERS);
4048 /* To import we need the other pool to hold at least own_min */
4049 if (obj->pool == pool)
4053 /* Request a bigger max, if one was suggested but it's too small */
4055 max = MAX (min, max);
4057 /* First step, configure our own pool */
4058 config = gst_buffer_pool_get_config (obj->pool);
4060 if (obj->need_video_meta || has_video_meta) {
4061 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
4062 gst_buffer_pool_config_add_option (config,
4063 GST_BUFFER_POOL_OPTION_VIDEO_META);
4066 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4067 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
4069 GST_DEBUG_OBJECT (obj->element, "setting own pool config to %"
4070 GST_PTR_FORMAT, config);
4072 /* Our pool often need to adjust the value */
4073 if (!gst_buffer_pool_set_config (obj->pool, config)) {
4074 config = gst_buffer_pool_get_config (obj->pool);
4076 GST_DEBUG_OBJECT (obj->element, "own pool config changed to %"
4077 GST_PTR_FORMAT, config);
4079 /* our pool will adjust the maximum buffer, which we are fine with */
4080 if (!gst_buffer_pool_set_config (obj->pool, config))
4084 /* Now configure the other pool if different */
4085 if (obj->pool != pool)
4089 config = gst_buffer_pool_get_config (other_pool);
4090 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4091 gst_buffer_pool_config_set_params (config, caps, size, min, max);
4093 GST_DEBUG_OBJECT (obj->element, "setting other pool config to %"
4094 GST_PTR_FORMAT, config);
4096 /* if downstream supports video metadata, add this to the pool config */
4097 if (has_video_meta) {
4098 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
4099 gst_buffer_pool_config_add_option (config,
4100 GST_BUFFER_POOL_OPTION_VIDEO_META);
4103 if (!gst_buffer_pool_set_config (other_pool, config)) {
4104 config = gst_buffer_pool_get_config (other_pool);
4106 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
4108 gst_structure_free (config);
4112 if (!gst_buffer_pool_set_config (other_pool, config))
4118 /* For simplicity, simply read back the active configuration, so our base
4119 * class get the right information */
4120 config = gst_buffer_pool_get_config (pool);
4121 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
4122 gst_structure_free (config);
4126 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
4128 gst_query_add_allocation_pool (query, pool, size, min, max);
4131 gst_object_unref (allocator);
4134 gst_object_unref (pool);
4140 /* setup_pool already send the error */
4145 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4146 (_("Failed to configure internal buffer pool.")), (NULL));
4151 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4152 (_("Video device did not suggest any buffer size.")), (NULL));
4158 gst_object_unref (allocator);
4161 gst_object_unref (pool);
4166 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4167 (_("No downstream pool to import from.")),
4168 ("When importing DMABUF or USERPTR, we need a pool to import from"));
4174 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
4176 GstBufferPool *pool;
4177 /* we need at least 2 buffers to operate */
4178 guint size, min, max;
4182 /* Set defaults allocation parameters */
4183 size = obj->info.size;
4184 min = GST_V4L2_MIN_BUFFERS;
4185 max = VIDEO_MAX_FRAME;
4187 gst_query_parse_allocation (query, &caps, &need_pool);
4192 if ((pool = obj->pool))
4193 gst_object_ref (pool);
4197 GstStructure *config;
4199 /* we had a pool, check caps */
4200 config = gst_buffer_pool_get_config (pool);
4201 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
4203 GST_DEBUG_OBJECT (obj->element,
4204 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
4205 if (!gst_caps_is_equal (caps, pcaps)) {
4206 gst_structure_free (config);
4207 gst_object_unref (pool);
4208 goto different_caps;
4210 gst_structure_free (config);
4212 gst_v4l2_get_driver_min_buffers (obj);
4214 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS);
4216 gst_query_add_allocation_pool (query, pool, size, min, max);
4218 /* we also support various metadata */
4219 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4222 gst_object_unref (pool);
4229 GST_DEBUG_OBJECT (obj->element, "no caps specified");
4234 /* different caps, we can't use this pool */
4235 GST_DEBUG_OBJECT (obj->element, "pool has different caps");