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);
522 if (v4l2object->extra_controls) {
523 gst_structure_free (v4l2object->extra_controls);
531 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
533 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
534 g_slist_free (v4l2object->formats);
535 v4l2object->formats = NULL;
541 gst_v4l2_object_prop_to_cid (guint prop_id)
546 case PROP_BRIGHTNESS:
547 cid = V4L2_CID_BRIGHTNESS;
550 cid = V4L2_CID_CONTRAST;
552 case PROP_SATURATION:
553 cid = V4L2_CID_SATURATION;
559 GST_WARNING ("unmapped property id: %d", prop_id);
566 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
567 guint prop_id, const GValue * value, GParamSpec * pspec)
571 g_free (v4l2object->videodev);
572 v4l2object->videodev = g_value_dup_string (value);
574 case PROP_BRIGHTNESS:
576 case PROP_SATURATION:
579 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
582 if (GST_V4L2_IS_OPEN (v4l2object)) {
583 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
590 v4l2object->tv_norm = g_value_get_enum (value);
594 if (GST_V4L2_IS_OPEN (v4l2object)) {
595 GstTuner *tuner = GST_TUNER (v4l2object->element);
596 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
597 (gchar *) g_value_get_string (value));
600 /* like gst_tuner_set_channel (tuner, channel)
601 without g_object_notify */
602 gst_v4l2_tuner_set_channel (v4l2object, channel);
605 g_free (v4l2object->channel);
606 v4l2object->channel = g_value_dup_string (value);
610 if (GST_V4L2_IS_OPEN (v4l2object)) {
611 GstTuner *tuner = GST_TUNER (v4l2object->element);
612 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
615 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
617 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
618 without g_object_notify */
619 gst_v4l2_tuner_set_frequency (v4l2object, channel,
620 g_value_get_ulong (value));
623 v4l2object->frequency = g_value_get_ulong (value);
629 v4l2object->req_mode = g_value_get_enum (value);
631 case PROP_CAPTURE_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_OUTPUT_IO_MODE:
636 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
637 v4l2object->req_mode = g_value_get_enum (value);
639 case PROP_EXTRA_CONTROLS:{
640 const GstStructure *s = gst_value_get_structure (value);
642 if (v4l2object->extra_controls)
643 gst_structure_free (v4l2object->extra_controls);
645 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
646 if (GST_V4L2_IS_OPEN (v4l2object))
647 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
650 case PROP_PIXEL_ASPECT_RATIO:
651 g_free (v4l2object->par);
652 v4l2object->par = g_new0 (GValue, 1);
653 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
654 if (!g_value_transform (value, v4l2object->par)) {
655 g_warning ("Could not transform string to aspect ratio");
656 gst_value_set_fraction (v4l2object->par, 1, 1);
658 GST_DEBUG_OBJECT (v4l2object->element, "set PAR to %d/%d",
659 gst_value_get_fraction_numerator (v4l2object->par),
660 gst_value_get_fraction_denominator (v4l2object->par));
662 case PROP_FORCE_ASPECT_RATIO:
663 v4l2object->keep_aspect = g_value_get_boolean (value);
674 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
675 guint prop_id, GValue * value, GParamSpec * pspec)
679 g_value_set_string (value, v4l2object->videodev);
681 case PROP_DEVICE_NAME:
683 const guchar *new = NULL;
685 if (GST_V4L2_IS_OPEN (v4l2object)) {
686 new = v4l2object->vcap.card;
687 } else if (gst_v4l2_open (v4l2object)) {
688 new = v4l2object->vcap.card;
689 gst_v4l2_close (v4l2object);
691 g_value_set_string (value, (gchar *) new);
696 if (GST_V4L2_IS_OPEN (v4l2object))
697 g_value_set_int (value, v4l2object->video_fd);
699 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
706 if (GST_V4L2_IS_OPEN (v4l2object)) {
707 flags |= v4l2object->device_caps &
708 (V4L2_CAP_VIDEO_CAPTURE |
709 V4L2_CAP_VIDEO_OUTPUT |
710 V4L2_CAP_VIDEO_OVERLAY |
711 V4L2_CAP_VBI_CAPTURE |
712 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
714 if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
715 flags |= V4L2_CAP_VIDEO_CAPTURE;
717 if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
718 flags |= V4L2_CAP_VIDEO_OUTPUT;
720 g_value_set_flags (value, flags);
723 case PROP_BRIGHTNESS:
725 case PROP_SATURATION:
728 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
731 if (GST_V4L2_IS_OPEN (v4l2object)) {
733 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
734 g_value_set_int (value, v);
742 g_value_set_enum (value, v4l2object->tv_norm);
745 g_value_set_enum (value, v4l2object->req_mode);
747 case PROP_CAPTURE_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_OUTPUT_IO_MODE:
752 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
753 g_value_set_enum (value, v4l2object->req_mode);
755 case PROP_EXTRA_CONTROLS:
756 gst_value_set_structure (value, v4l2object->extra_controls);
758 case PROP_PIXEL_ASPECT_RATIO:
760 g_value_transform (v4l2object->par, value);
762 case PROP_FORCE_ASPECT_RATIO:
763 g_value_set_boolean (value, v4l2object->keep_aspect);
773 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
775 struct v4l2_control control = { 0, };
777 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
779 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
780 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
782 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
784 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
785 GST_DEBUG_OBJECT (v4l2object->element,
786 "driver requires a minimum of %d buffers", control.value);
787 v4l2object->min_buffers = control.value;
789 v4l2object->min_buffers = 0;
794 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
796 GstTunerNorm *norm = NULL;
797 GstTunerChannel *channel = NULL;
800 if (!GST_IS_TUNER (v4l2object->element))
803 tuner = GST_TUNER (v4l2object->element);
805 if (v4l2object->tv_norm)
806 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
807 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
808 "norm=%p", (guint64) v4l2object->tv_norm, norm);
810 gst_tuner_set_norm (tuner, norm);
813 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
815 v4l2object->tv_norm =
816 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
817 gst_tuner_norm_changed (tuner, norm);
821 if (v4l2object->channel)
822 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
824 gst_tuner_set_channel (tuner, channel);
827 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
828 (v4l2object->element)));
830 g_free (v4l2object->channel);
831 v4l2object->channel = g_strdup (channel->label);
832 gst_tuner_channel_changed (tuner, channel);
837 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
838 if (v4l2object->frequency != 0) {
839 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
841 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
842 if (v4l2object->frequency == 0) {
844 gst_tuner_set_frequency (tuner, channel, 1000);
852 gst_v4l2_object_open (GstV4l2Object * v4l2object)
854 if (gst_v4l2_open (v4l2object))
855 gst_v4l2_set_defaults (v4l2object);
863 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
867 ret = gst_v4l2_dup (v4l2object, other);
873 gst_v4l2_object_close (GstV4l2Object * v4l2object)
875 if (!gst_v4l2_close (v4l2object))
878 gst_caps_replace (&v4l2object->probed_caps, NULL);
880 /* reset our copy of the device caps */
881 v4l2object->device_caps = 0;
883 if (v4l2object->formats) {
884 gst_v4l2_object_clear_format_list (v4l2object);
890 static struct v4l2_fmtdesc *
891 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
894 struct v4l2_fmtdesc *fmt;
900 walk = gst_v4l2_object_get_format_list (v4l2object);
902 fmt = (struct v4l2_fmtdesc *) walk->data;
903 if (fmt->pixelformat == fourcc)
905 /* special case for jpeg */
906 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
907 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
908 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
909 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
910 fourcc == V4L2_PIX_FMT_PJPG) {
914 walk = g_slist_next (walk);
922 /* complete made up ranking, the values themselves are meaningless */
923 /* These ranks MUST be X such that X<<15 fits on a signed int - see
924 the comment at the end of gst_v4l2_object_format_get_rank. */
925 #define YUV_BASE_RANK 1000
926 #define JPEG_BASE_RANK 500
927 #define DV_BASE_RANK 200
928 #define RGB_BASE_RANK 100
929 #define YUV_ODD_BASE_RANK 50
930 #define RGB_ODD_BASE_RANK 25
931 #define BAYER_BASE_RANK 15
932 #define S910_BASE_RANK 10
933 #define GREY_BASE_RANK 5
934 #define PWC_BASE_RANK 1
937 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
939 guint32 fourcc = fmt->pixelformat;
940 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
944 case V4L2_PIX_FMT_MJPEG:
945 case V4L2_PIX_FMT_PJPG:
946 rank = JPEG_BASE_RANK;
948 case V4L2_PIX_FMT_JPEG:
949 rank = JPEG_BASE_RANK + 1;
951 case V4L2_PIX_FMT_MPEG: /* MPEG */
952 rank = JPEG_BASE_RANK + 2;
955 case V4L2_PIX_FMT_RGB332:
956 case V4L2_PIX_FMT_ARGB555:
957 case V4L2_PIX_FMT_XRGB555:
958 case V4L2_PIX_FMT_RGB555:
959 case V4L2_PIX_FMT_ARGB555X:
960 case V4L2_PIX_FMT_XRGB555X:
961 case V4L2_PIX_FMT_RGB555X:
962 case V4L2_PIX_FMT_BGR666:
963 case V4L2_PIX_FMT_RGB565:
964 case V4L2_PIX_FMT_RGB565X:
965 case V4L2_PIX_FMT_RGB444:
966 case V4L2_PIX_FMT_Y4:
967 case V4L2_PIX_FMT_Y6:
968 case V4L2_PIX_FMT_Y10:
969 case V4L2_PIX_FMT_Y12:
970 case V4L2_PIX_FMT_Y10BPACK:
971 case V4L2_PIX_FMT_YUV555:
972 case V4L2_PIX_FMT_YUV565:
973 case V4L2_PIX_FMT_YUV32:
974 case V4L2_PIX_FMT_NV12MT_16X16:
975 case V4L2_PIX_FMT_NV42:
976 case V4L2_PIX_FMT_H264_MVC:
977 rank = RGB_ODD_BASE_RANK;
980 case V4L2_PIX_FMT_RGB24:
981 case V4L2_PIX_FMT_BGR24:
982 rank = RGB_BASE_RANK - 1;
985 case V4L2_PIX_FMT_RGB32:
986 case V4L2_PIX_FMT_BGR32:
987 case V4L2_PIX_FMT_ABGR32:
988 case V4L2_PIX_FMT_XBGR32:
989 case V4L2_PIX_FMT_ARGB32:
990 case V4L2_PIX_FMT_XRGB32:
991 rank = RGB_BASE_RANK;
994 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
995 rank = GREY_BASE_RANK;
998 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
999 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
1000 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
1001 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1002 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1003 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1004 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1005 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1006 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1007 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1008 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1009 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1010 rank = YUV_ODD_BASE_RANK;
1013 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1014 rank = YUV_BASE_RANK + 3;
1016 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1017 rank = YUV_BASE_RANK + 2;
1019 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1020 case V4L2_PIX_FMT_YUV420M:
1021 rank = YUV_BASE_RANK + 7;
1023 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1024 rank = YUV_BASE_RANK + 10;
1026 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1027 rank = YUV_BASE_RANK + 6;
1029 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1030 rank = YUV_BASE_RANK + 9;
1032 case V4L2_PIX_FMT_YUV444:
1033 rank = YUV_BASE_RANK + 6;
1035 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1036 rank = YUV_BASE_RANK + 5;
1038 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1039 rank = YUV_BASE_RANK + 4;
1041 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1042 rank = YUV_BASE_RANK + 8;
1045 case V4L2_PIX_FMT_DV:
1046 rank = DV_BASE_RANK;
1049 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1053 case V4L2_PIX_FMT_SBGGR8:
1054 case V4L2_PIX_FMT_SGBRG8:
1055 case V4L2_PIX_FMT_SGRBG8:
1056 case V4L2_PIX_FMT_SRGGB8:
1057 rank = BAYER_BASE_RANK;
1060 case V4L2_PIX_FMT_SN9C10X:
1061 rank = S910_BASE_RANK;
1064 case V4L2_PIX_FMT_PWC1:
1065 rank = PWC_BASE_RANK;
1067 case V4L2_PIX_FMT_PWC2:
1068 rank = PWC_BASE_RANK;
1076 /* All ranks are below 1<<15 so a shift by 15
1077 * will a) make all non-emulated formats larger
1078 * than emulated and b) will not overflow
1089 format_cmp_func (gconstpointer a, gconstpointer b)
1091 const struct v4l2_fmtdesc *fa = a;
1092 const struct v4l2_fmtdesc *fb = b;
1094 if (fa->pixelformat == fb->pixelformat)
1097 return gst_v4l2_object_format_get_rank (fb) -
1098 gst_v4l2_object_format_get_rank (fa);
1101 /******************************************************
1102 * gst_v4l2_object_fill_format_list():
1103 * create list of supported capture formats
1104 * return value: TRUE on success, FALSE on error
1105 ******************************************************/
1107 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1108 enum v4l2_buf_type type)
1111 struct v4l2_fmtdesc *format;
1113 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1115 /* format enumeration */
1117 format = g_new0 (struct v4l2_fmtdesc, 1);
1120 format->type = type;
1122 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1123 if (errno == EINVAL) {
1125 break; /* end of enumeration */
1131 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1132 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1133 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1134 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1135 format->description);
1136 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1137 GST_FOURCC_ARGS (format->pixelformat));
1139 /* sort formats according to our preference; we do this, because caps
1140 * are probed in the order the formats are in the list, and the order of
1141 * formats in the final probed caps matters for things like fixation */
1142 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1143 (GCompareFunc) format_cmp_func);
1146 #ifndef GST_DISABLE_GST_DEBUG
1150 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1151 for (l = v4l2object->formats; l != NULL; l = l->next) {
1154 GST_INFO_OBJECT (v4l2object->element,
1155 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1156 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1168 if (!GST_IS_ELEMENT (v4l2object->element))
1171 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1172 (_("Failed to enumerate possible video formats device '%s' can work "
1173 "with"), v4l2object->videodev),
1174 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1175 n, v4l2object->videodev, errno, g_strerror (errno)));
1182 * Get the list of supported capture formats, a list of
1183 * <code>struct v4l2_fmtdesc</code>.
1186 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1188 if (!v4l2object->formats) {
1190 /* check usual way */
1191 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1193 /* if our driver supports multi-planar
1194 * and if formats are still empty then we can workaround driver bug
1195 * by also looking up formats as if our device was not supporting
1197 if (!v4l2object->formats) {
1198 switch (v4l2object->type) {
1199 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1200 gst_v4l2_object_fill_format_list (v4l2object,
1201 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1204 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1205 gst_v4l2_object_fill_format_list (v4l2object,
1206 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1214 return v4l2object->formats;
1217 static GstVideoFormat
1218 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1220 GstVideoFormat format;
1223 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1224 format = GST_VIDEO_FORMAT_GRAY8;
1226 case V4L2_PIX_FMT_Y16:
1227 format = GST_VIDEO_FORMAT_GRAY16_LE;
1229 case V4L2_PIX_FMT_Y16_BE:
1230 format = GST_VIDEO_FORMAT_GRAY16_BE;
1232 case V4L2_PIX_FMT_XRGB555:
1233 case V4L2_PIX_FMT_RGB555:
1234 format = GST_VIDEO_FORMAT_RGB15;
1236 case V4L2_PIX_FMT_XRGB555X:
1237 case V4L2_PIX_FMT_RGB555X:
1238 format = GST_VIDEO_FORMAT_BGR15;
1240 case V4L2_PIX_FMT_RGB565:
1241 format = GST_VIDEO_FORMAT_RGB16;
1243 case V4L2_PIX_FMT_RGB24:
1244 format = GST_VIDEO_FORMAT_RGB;
1246 case V4L2_PIX_FMT_BGR24:
1247 format = GST_VIDEO_FORMAT_BGR;
1249 case V4L2_PIX_FMT_XRGB32:
1250 case V4L2_PIX_FMT_RGB32:
1251 format = GST_VIDEO_FORMAT_xRGB;
1253 case V4L2_PIX_FMT_XBGR32:
1254 case V4L2_PIX_FMT_BGR32:
1255 format = GST_VIDEO_FORMAT_BGRx;
1257 case V4L2_PIX_FMT_ABGR32:
1258 format = GST_VIDEO_FORMAT_BGRA;
1260 case V4L2_PIX_FMT_ARGB32:
1261 format = GST_VIDEO_FORMAT_ARGB;
1263 case V4L2_PIX_FMT_NV12:
1264 case V4L2_PIX_FMT_NV12M:
1265 format = GST_VIDEO_FORMAT_NV12;
1267 case V4L2_PIX_FMT_NV12MT:
1268 format = GST_VIDEO_FORMAT_NV12_64Z32;
1270 case V4L2_PIX_FMT_NV21:
1271 case V4L2_PIX_FMT_NV21M:
1272 format = GST_VIDEO_FORMAT_NV21;
1274 case V4L2_PIX_FMT_YVU410:
1275 format = GST_VIDEO_FORMAT_YVU9;
1277 case V4L2_PIX_FMT_YUV410:
1278 format = GST_VIDEO_FORMAT_YUV9;
1280 case V4L2_PIX_FMT_YUV420:
1281 case V4L2_PIX_FMT_YUV420M:
1282 format = GST_VIDEO_FORMAT_I420;
1284 case V4L2_PIX_FMT_YUYV:
1285 format = GST_VIDEO_FORMAT_YUY2;
1287 case V4L2_PIX_FMT_YVU420:
1288 format = GST_VIDEO_FORMAT_YV12;
1290 case V4L2_PIX_FMT_UYVY:
1291 format = GST_VIDEO_FORMAT_UYVY;
1293 case V4L2_PIX_FMT_YUV411P:
1294 format = GST_VIDEO_FORMAT_Y41B;
1296 case V4L2_PIX_FMT_YUV422P:
1297 format = GST_VIDEO_FORMAT_Y42B;
1299 case V4L2_PIX_FMT_YVYU:
1300 format = GST_VIDEO_FORMAT_YVYU;
1302 case V4L2_PIX_FMT_NV16:
1303 case V4L2_PIX_FMT_NV16M:
1304 format = GST_VIDEO_FORMAT_NV16;
1306 case V4L2_PIX_FMT_NV61:
1307 case V4L2_PIX_FMT_NV61M:
1308 format = GST_VIDEO_FORMAT_NV61;
1310 case V4L2_PIX_FMT_NV24:
1311 format = GST_VIDEO_FORMAT_NV24;
1314 format = GST_VIDEO_FORMAT_UNKNOWN;
1322 gst_v4l2_object_v4l2fourcc_is_rgb (guint32 fourcc)
1324 gboolean ret = FALSE;
1327 case V4L2_PIX_FMT_XRGB555:
1328 case V4L2_PIX_FMT_RGB555:
1329 case V4L2_PIX_FMT_XRGB555X:
1330 case V4L2_PIX_FMT_RGB555X:
1331 case V4L2_PIX_FMT_RGB565:
1332 case V4L2_PIX_FMT_RGB24:
1333 case V4L2_PIX_FMT_BGR24:
1334 case V4L2_PIX_FMT_XRGB32:
1335 case V4L2_PIX_FMT_RGB32:
1336 case V4L2_PIX_FMT_XBGR32:
1337 case V4L2_PIX_FMT_BGR32:
1338 case V4L2_PIX_FMT_ABGR32:
1339 case V4L2_PIX_FMT_ARGB32:
1349 static GstStructure *
1350 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1352 GstStructure *structure = NULL;
1355 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1356 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1357 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1358 structure = gst_structure_new_empty ("image/jpeg");
1360 case V4L2_PIX_FMT_MPEG1:
1361 structure = gst_structure_new ("video/mpeg",
1362 "mpegversion", G_TYPE_INT, 2, NULL);
1364 case V4L2_PIX_FMT_MPEG2:
1365 structure = gst_structure_new ("video/mpeg",
1366 "mpegversion", G_TYPE_INT, 2, NULL);
1368 case V4L2_PIX_FMT_MPEG4:
1369 case V4L2_PIX_FMT_XVID:
1370 structure = gst_structure_new ("video/mpeg",
1371 "mpegversion", G_TYPE_INT, 4, "systemstream",
1372 G_TYPE_BOOLEAN, FALSE, NULL);
1374 case V4L2_PIX_FMT_H263:
1375 structure = gst_structure_new ("video/x-h263",
1376 "variant", G_TYPE_STRING, "itu", NULL);
1378 case V4L2_PIX_FMT_H264: /* H.264 */
1379 structure = gst_structure_new ("video/x-h264",
1380 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1381 G_TYPE_STRING, "au", NULL);
1383 case V4L2_PIX_FMT_H264_NO_SC:
1384 structure = gst_structure_new ("video/x-h264",
1385 "stream-format", G_TYPE_STRING, "avc", "alignment",
1386 G_TYPE_STRING, "au", NULL);
1388 case V4L2_PIX_FMT_VC1_ANNEX_G:
1389 case V4L2_PIX_FMT_VC1_ANNEX_L:
1390 structure = gst_structure_new ("video/x-wmv",
1391 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1393 case V4L2_PIX_FMT_VP8:
1394 structure = gst_structure_new_empty ("video/x-vp8");
1396 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1397 case V4L2_PIX_FMT_Y16:
1398 case V4L2_PIX_FMT_Y16_BE:
1399 case V4L2_PIX_FMT_XRGB555:
1400 case V4L2_PIX_FMT_RGB555:
1401 case V4L2_PIX_FMT_XRGB555X:
1402 case V4L2_PIX_FMT_RGB555X:
1403 case V4L2_PIX_FMT_RGB565:
1404 case V4L2_PIX_FMT_RGB24:
1405 case V4L2_PIX_FMT_BGR24:
1406 case V4L2_PIX_FMT_RGB32:
1407 case V4L2_PIX_FMT_XRGB32:
1408 case V4L2_PIX_FMT_ARGB32:
1409 case V4L2_PIX_FMT_BGR32:
1410 case V4L2_PIX_FMT_XBGR32:
1411 case V4L2_PIX_FMT_ABGR32:
1412 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1413 case V4L2_PIX_FMT_NV12M:
1414 case V4L2_PIX_FMT_NV12MT:
1415 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1416 case V4L2_PIX_FMT_NV21M:
1417 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1418 case V4L2_PIX_FMT_NV16M:
1419 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1420 case V4L2_PIX_FMT_NV61M:
1421 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1422 case V4L2_PIX_FMT_YVU410:
1423 case V4L2_PIX_FMT_YUV410:
1424 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1425 case V4L2_PIX_FMT_YUV420M:
1426 case V4L2_PIX_FMT_YUYV:
1427 case V4L2_PIX_FMT_YVU420:
1428 case V4L2_PIX_FMT_UYVY:
1429 case V4L2_PIX_FMT_YUV422P:
1430 case V4L2_PIX_FMT_YVYU:
1431 case V4L2_PIX_FMT_YUV411P:{
1432 GstVideoFormat format;
1433 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1434 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1435 structure = gst_structure_new ("video/x-raw",
1436 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1439 case V4L2_PIX_FMT_DV:
1441 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1444 case V4L2_PIX_FMT_MPEG: /* MPEG */
1445 structure = gst_structure_new ("video/mpegts",
1446 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1448 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1450 case V4L2_PIX_FMT_SBGGR8:
1451 case V4L2_PIX_FMT_SGBRG8:
1452 case V4L2_PIX_FMT_SGRBG8:
1453 case V4L2_PIX_FMT_SRGGB8:
1454 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1455 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1456 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1457 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1458 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1460 case V4L2_PIX_FMT_SN9C10X:
1461 structure = gst_structure_new_empty ("video/x-sonix");
1463 case V4L2_PIX_FMT_PWC1:
1464 structure = gst_structure_new_empty ("video/x-pwc1");
1466 case V4L2_PIX_FMT_PWC2:
1467 structure = gst_structure_new_empty ("video/x-pwc2");
1469 case V4L2_PIX_FMT_RGB332:
1470 case V4L2_PIX_FMT_BGR666:
1471 case V4L2_PIX_FMT_ARGB555X:
1472 case V4L2_PIX_FMT_RGB565X:
1473 case V4L2_PIX_FMT_RGB444:
1474 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1475 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1476 case V4L2_PIX_FMT_Y4:
1477 case V4L2_PIX_FMT_Y6:
1478 case V4L2_PIX_FMT_Y10:
1479 case V4L2_PIX_FMT_Y12:
1480 case V4L2_PIX_FMT_Y10BPACK:
1481 case V4L2_PIX_FMT_YUV444:
1482 case V4L2_PIX_FMT_YUV555:
1483 case V4L2_PIX_FMT_YUV565:
1484 case V4L2_PIX_FMT_Y41P:
1485 case V4L2_PIX_FMT_YUV32:
1486 case V4L2_PIX_FMT_NV12MT_16X16:
1487 case V4L2_PIX_FMT_NV42:
1488 case V4L2_PIX_FMT_H264_MVC:
1490 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1491 fourcc, GST_FOURCC_ARGS (fourcc));
1499 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1501 GstStructure *template;
1504 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1506 if (template == NULL)
1509 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1510 if (gst_v4l2_formats[i].format != fourcc)
1513 if (gst_v4l2_formats[i].dimensions) {
1514 gst_structure_set (template,
1515 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1516 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1517 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1528 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1530 GstStructure *structure;
1534 caps = gst_caps_new_empty ();
1535 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1537 if ((gst_v4l2_formats[i].flags & flags) == 0)
1541 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1544 GstStructure *alt_s = NULL;
1546 if (gst_v4l2_formats[i].dimensions) {
1547 gst_structure_set (structure,
1548 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1549 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1550 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1553 switch (gst_v4l2_formats[i].format) {
1554 case V4L2_PIX_FMT_RGB32:
1555 alt_s = gst_structure_copy (structure);
1556 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1558 case V4L2_PIX_FMT_BGR32:
1559 alt_s = gst_structure_copy (structure);
1560 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1565 gst_caps_append_structure (caps, structure);
1568 gst_caps_append_structure (caps, alt_s);
1572 return gst_caps_simplify (caps);
1576 gst_v4l2_object_get_all_caps (void)
1578 static GstCaps *caps = NULL;
1581 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1583 return gst_caps_ref (caps);
1587 gst_v4l2_object_get_raw_caps (void)
1589 static GstCaps *caps = NULL;
1592 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1594 return gst_caps_ref (caps);
1598 gst_v4l2_object_get_codec_caps (void)
1600 static GstCaps *caps = NULL;
1603 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1605 return gst_caps_ref (caps);
1608 /* collect data for the given caps
1609 * @caps: given input caps
1610 * @format: location for the v4l format
1611 * @w/@h: location for width and height
1612 * @fps_n/@fps_d: location for framerate
1613 * @size: location for expected size of the frame or 0 if unknown
1616 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1617 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1619 GstStructure *structure;
1620 guint32 fourcc = 0, fourcc_nc = 0;
1621 const gchar *mimetype;
1622 struct v4l2_fmtdesc *fmt = NULL;
1624 structure = gst_caps_get_structure (caps, 0);
1626 mimetype = gst_structure_get_name (structure);
1628 if (!gst_video_info_from_caps (info, caps))
1629 goto invalid_format;
1631 if (g_str_equal (mimetype, "video/x-raw")) {
1632 switch (GST_VIDEO_INFO_FORMAT (info)) {
1633 case GST_VIDEO_FORMAT_I420:
1634 fourcc = V4L2_PIX_FMT_YUV420;
1635 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1637 case GST_VIDEO_FORMAT_YUY2:
1638 fourcc = V4L2_PIX_FMT_YUYV;
1640 case GST_VIDEO_FORMAT_UYVY:
1641 fourcc = V4L2_PIX_FMT_UYVY;
1643 case GST_VIDEO_FORMAT_YV12:
1644 fourcc = V4L2_PIX_FMT_YVU420;
1646 case GST_VIDEO_FORMAT_Y41B:
1647 fourcc = V4L2_PIX_FMT_YUV411P;
1649 case GST_VIDEO_FORMAT_Y42B:
1650 fourcc = V4L2_PIX_FMT_YUV422P;
1652 case GST_VIDEO_FORMAT_NV12:
1653 fourcc = V4L2_PIX_FMT_NV12;
1654 fourcc_nc = V4L2_PIX_FMT_NV12M;
1656 case GST_VIDEO_FORMAT_NV12_64Z32:
1657 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1659 case GST_VIDEO_FORMAT_NV21:
1660 fourcc = V4L2_PIX_FMT_NV21;
1661 fourcc_nc = V4L2_PIX_FMT_NV21M;
1663 case GST_VIDEO_FORMAT_NV16:
1664 fourcc = V4L2_PIX_FMT_NV16;
1665 fourcc_nc = V4L2_PIX_FMT_NV16M;
1667 case GST_VIDEO_FORMAT_NV61:
1668 fourcc = V4L2_PIX_FMT_NV61;
1669 fourcc_nc = V4L2_PIX_FMT_NV61M;
1671 case GST_VIDEO_FORMAT_NV24:
1672 fourcc = V4L2_PIX_FMT_NV24;
1674 case GST_VIDEO_FORMAT_YVYU:
1675 fourcc = V4L2_PIX_FMT_YVYU;
1677 case GST_VIDEO_FORMAT_RGB15:
1678 fourcc = V4L2_PIX_FMT_RGB555;
1679 fourcc_nc = V4L2_PIX_FMT_XRGB555;
1681 case GST_VIDEO_FORMAT_RGB16:
1682 fourcc = V4L2_PIX_FMT_RGB565;
1684 case GST_VIDEO_FORMAT_RGB:
1685 fourcc = V4L2_PIX_FMT_RGB24;
1687 case GST_VIDEO_FORMAT_BGR:
1688 fourcc = V4L2_PIX_FMT_BGR24;
1690 case GST_VIDEO_FORMAT_xRGB:
1691 fourcc = V4L2_PIX_FMT_RGB32;
1692 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1694 case GST_VIDEO_FORMAT_ARGB:
1695 fourcc = V4L2_PIX_FMT_RGB32;
1696 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1698 case GST_VIDEO_FORMAT_BGRx:
1699 fourcc = V4L2_PIX_FMT_BGR32;
1700 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1702 case GST_VIDEO_FORMAT_BGRA:
1703 fourcc = V4L2_PIX_FMT_BGR32;
1704 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1706 case GST_VIDEO_FORMAT_GRAY8:
1707 fourcc = V4L2_PIX_FMT_GREY;
1709 case GST_VIDEO_FORMAT_GRAY16_LE:
1710 fourcc = V4L2_PIX_FMT_Y16;
1712 case GST_VIDEO_FORMAT_GRAY16_BE:
1713 fourcc = V4L2_PIX_FMT_Y16_BE;
1719 if (g_str_equal (mimetype, "video/mpegts")) {
1720 fourcc = V4L2_PIX_FMT_MPEG;
1721 } else if (g_str_equal (mimetype, "video/x-dv")) {
1722 fourcc = V4L2_PIX_FMT_DV;
1723 } else if (g_str_equal (mimetype, "image/jpeg")) {
1724 fourcc = V4L2_PIX_FMT_JPEG;
1725 } else if (g_str_equal (mimetype, "video/mpeg")) {
1727 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1730 fourcc = V4L2_PIX_FMT_MPEG1;
1733 fourcc = V4L2_PIX_FMT_MPEG2;
1736 fourcc = V4L2_PIX_FMT_MPEG4;
1737 fourcc_nc = V4L2_PIX_FMT_XVID;
1743 } else if (g_str_equal (mimetype, "video/x-h263")) {
1744 fourcc = V4L2_PIX_FMT_H263;
1745 } else if (g_str_equal (mimetype, "video/x-h264")) {
1746 const gchar *stream_format =
1747 gst_structure_get_string (structure, "stream-format");
1748 if (g_str_equal (stream_format, "avc"))
1749 fourcc = V4L2_PIX_FMT_H264_NO_SC;
1751 fourcc = V4L2_PIX_FMT_H264;
1752 } else if (g_str_equal (mimetype, "video/x-vp8")) {
1753 fourcc = V4L2_PIX_FMT_VP8;
1754 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1755 const gchar *format = gst_structure_get_string (structure, "format");
1757 if (!g_ascii_strcasecmp (format, "bggr"))
1758 fourcc = V4L2_PIX_FMT_SBGGR8;
1759 else if (!g_ascii_strcasecmp (format, "gbrg"))
1760 fourcc = V4L2_PIX_FMT_SGBRG8;
1761 else if (!g_ascii_strcasecmp (format, "grbg"))
1762 fourcc = V4L2_PIX_FMT_SGRBG8;
1763 else if (!g_ascii_strcasecmp (format, "rggb"))
1764 fourcc = V4L2_PIX_FMT_SRGGB8;
1766 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1767 fourcc = V4L2_PIX_FMT_SN9C10X;
1768 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1769 fourcc = V4L2_PIX_FMT_PWC1;
1770 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1771 fourcc = V4L2_PIX_FMT_PWC2;
1776 /* Prefer the non-contiguous if supported */
1777 v4l2object->prefered_non_contiguous = TRUE;
1780 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
1781 else if (fourcc == 0)
1782 goto unhandled_format;
1785 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1786 v4l2object->prefered_non_contiguous = FALSE;
1790 goto unsupported_format;
1799 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1804 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1809 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1815 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1816 guint32 pixelformat, gint * width, gint * height);
1819 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1821 struct v4l2_cropcap cropcap;
1822 int num = 1, den = 1;
1824 if (!v4l2object->keep_aspect)
1827 if (v4l2object->par) {
1828 num = gst_value_get_fraction_numerator (v4l2object->par);
1829 den = gst_value_get_fraction_denominator (v4l2object->par);
1833 memset (&cropcap, 0, sizeof (cropcap));
1835 cropcap.type = v4l2object->type;
1836 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
1837 goto cropcap_failed;
1839 num = cropcap.pixelaspect.numerator;
1840 den = cropcap.pixelaspect.denominator;
1842 /* Ignore PAR that are 0/0 */
1847 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, num, den,
1852 if (errno != ENOTTY)
1853 GST_WARNING_OBJECT (v4l2object->element,
1854 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
1855 g_strerror (errno));
1859 /* returns TRUE if the value was changed in place, otherwise FALSE */
1861 gst_v4l2src_value_simplify (GValue * val)
1863 /* simplify list of one value to one value */
1864 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
1865 const GValue *list_val;
1866 GValue new_val = G_VALUE_INIT;
1868 list_val = gst_value_list_get_value (val, 0);
1869 g_value_init (&new_val, G_VALUE_TYPE (list_val));
1870 g_value_copy (list_val, &new_val);
1871 g_value_unset (val);
1880 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
1881 GstVideoInterlaceMode * interlace_mode)
1883 /* NB: If you add new return values, please fix mode_strings in
1884 * gst_v4l2_object_add_interlace_mode */
1886 case V4L2_FIELD_ANY:
1888 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
1890 case V4L2_FIELD_NONE:
1891 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1893 case V4L2_FIELD_INTERLACED:
1894 case V4L2_FIELD_INTERLACED_TB:
1895 case V4L2_FIELD_INTERLACED_BT:
1896 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1899 GST_ERROR ("Unknown enum v4l2_field %d", field);
1905 gst_v4l2_object_get_colorspace (struct v4l2_format *fmt,
1906 GstVideoColorimetry * cinfo)
1909 gst_v4l2_object_v4l2fourcc_is_rgb (fmt->fmt.pix.pixelformat);
1910 enum v4l2_colorspace colorspace;
1911 enum v4l2_quantization range;
1912 enum v4l2_ycbcr_encoding matrix;
1913 enum v4l2_xfer_func transfer;
1914 gboolean ret = TRUE;
1916 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
1917 colorspace = fmt->fmt.pix_mp.colorspace;
1918 range = fmt->fmt.pix_mp.quantization;
1919 matrix = fmt->fmt.pix_mp.ycbcr_enc;
1920 transfer = fmt->fmt.pix_mp.xfer_func;
1922 colorspace = fmt->fmt.pix.colorspace;
1923 range = fmt->fmt.pix.quantization;
1924 matrix = fmt->fmt.pix.ycbcr_enc;
1925 transfer = fmt->fmt.pix.xfer_func;
1928 /* First step, set the defaults for each primaries */
1929 switch (colorspace) {
1930 case V4L2_COLORSPACE_SMPTE170M:
1931 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1932 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1933 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1934 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1936 case V4L2_COLORSPACE_REC709:
1937 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1938 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1939 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1940 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1942 case V4L2_COLORSPACE_SRGB:
1943 case V4L2_COLORSPACE_JPEG:
1944 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1945 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1946 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
1947 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1949 case V4L2_COLORSPACE_ADOBERGB:
1950 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1951 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1952 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
1953 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
1955 case V4L2_COLORSPACE_BT2020:
1956 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1957 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1958 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1959 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
1961 case V4L2_COLORSPACE_SMPTE240M:
1962 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1963 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1964 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
1965 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
1967 case V4L2_COLORSPACE_470_SYSTEM_M:
1968 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1969 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1970 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1971 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
1973 case V4L2_COLORSPACE_470_SYSTEM_BG:
1974 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1975 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1976 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1977 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
1979 case V4L2_COLORSPACE_RAW:
1980 /* Explicitly unknown */
1981 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
1982 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
1983 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
1984 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
1987 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
1995 /* Second step, apply any custom variation */
1997 case V4L2_QUANTIZATION_FULL_RANGE:
1998 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2000 case V4L2_QUANTIZATION_LIM_RANGE:
2001 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2003 case V4L2_QUANTIZATION_DEFAULT:
2004 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
2005 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
2006 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2007 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601
2008 || matrix == V4L2_YCBCR_ENC_XV709
2009 || colorspace == V4L2_COLORSPACE_JPEG)
2010 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2012 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2015 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
2016 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2021 case V4L2_YCBCR_ENC_XV601:
2022 case V4L2_YCBCR_ENC_SYCC:
2023 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
2025 case V4L2_YCBCR_ENC_601:
2026 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2028 case V4L2_YCBCR_ENC_XV709:
2029 GST_FIXME ("XV709 not defined, assuming 709");
2031 case V4L2_YCBCR_ENC_709:
2032 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2034 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2035 GST_FIXME ("BT2020 with constant lumma is not defined, assuming BT2020");
2037 case V4L2_YCBCR_ENC_BT2020:
2038 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2040 case V4L2_YCBCR_ENC_SMPTE240M:
2041 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2043 case V4L2_YCBCR_ENC_DEFAULT:
2044 /* nothing, just use defaults for colorspace */
2047 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2048 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2052 /* Set identity matrix for R'G'B' formats to avoid creating
2053 * confusion. This though is cosmetic as it's now properly ignored by
2054 * the video info API and videoconvert. */
2056 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2059 case V4L2_XFER_FUNC_709:
2060 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2062 case V4L2_XFER_FUNC_SRGB:
2063 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2065 case V4L2_XFER_FUNC_ADOBERGB:
2066 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2068 case V4L2_XFER_FUNC_SMPTE240M:
2069 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2071 case V4L2_XFER_FUNC_NONE:
2072 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2074 case V4L2_XFER_FUNC_DEFAULT:
2075 /* nothing, just use defaults for colorspace */
2078 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2079 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2088 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2089 struct v4l2_format *try_fmt)
2091 int fd = v4l2object->video_fd;
2092 struct v4l2_format fmt;
2095 memcpy (&fmt, try_fmt, sizeof (fmt));
2096 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2098 if (r < 0 && errno == ENOTTY) {
2099 /* The driver might not implement TRY_FMT, in which case we will try
2101 if (GST_V4L2_IS_ACTIVE (v4l2object))
2104 memcpy (&fmt, try_fmt, sizeof (fmt));
2105 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2107 memcpy (try_fmt, &fmt, sizeof (fmt));
2111 memcpy (try_fmt, &fmt, sizeof (fmt));
2112 GST_WARNING_OBJECT (v4l2object->element,
2113 "Unable to try format: %s", g_strerror (errno));
2119 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2120 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2122 struct v4l2_format fmt;
2123 GValue interlace_formats = { 0, };
2124 GstVideoInterlaceMode interlace_mode, prev = -1;
2126 const gchar *mode_strings[] = { "progressive",
2131 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2134 if (v4l2object->never_interlaced) {
2135 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2139 g_value_init (&interlace_formats, GST_TYPE_LIST);
2141 /* Try twice - once for NONE, once for INTERLACED. */
2142 memset (&fmt, 0, sizeof (fmt));
2143 fmt.type = v4l2object->type;
2144 fmt.fmt.pix.width = width;
2145 fmt.fmt.pix.height = height;
2146 fmt.fmt.pix.pixelformat = pixelformat;
2147 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2149 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2150 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2151 GValue interlace_enum = { 0, };
2152 g_value_init (&interlace_enum, G_TYPE_STRING);
2153 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2154 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2155 prev = interlace_mode;
2158 memset (&fmt, 0, sizeof (fmt));
2159 fmt.type = v4l2object->type;
2160 fmt.fmt.pix.width = width;
2161 fmt.fmt.pix.height = height;
2162 fmt.fmt.pix.pixelformat = pixelformat;
2163 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2165 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2166 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode) &&
2167 prev != interlace_mode) {
2168 GValue interlace_enum = { 0, };
2169 g_value_init (&interlace_enum, G_TYPE_STRING);
2170 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2171 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2174 if (gst_v4l2src_value_simplify (&interlace_formats)
2175 || gst_value_list_get_size (&interlace_formats) > 0)
2176 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2178 GST_WARNING_OBJECT (v4l2object, "Failed to determine interlace mode");
2184 gst_v4l2_object_fill_colorimetry_list (GValue * list,
2185 GstVideoColorimetry * cinfo)
2187 GValue colorimetry = G_VALUE_INIT;
2190 gboolean found = FALSE;
2192 g_value_init (&colorimetry, G_TYPE_STRING);
2193 g_value_take_string (&colorimetry, gst_video_colorimetry_to_string (cinfo));
2195 /* only insert if no duplicate */
2196 size = gst_value_list_get_size (list);
2197 for (i = 0; i < size; i++) {
2200 tmp = gst_value_list_get_value (list, i);
2201 if (gst_value_compare (&colorimetry, tmp) == GST_VALUE_EQUAL) {
2208 gst_value_list_append_and_take_value (list, &colorimetry);
2210 g_value_unset (&colorimetry);
2214 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2215 guint32 width, guint32 height, guint32 pixelformat)
2217 struct v4l2_format fmt;
2218 GValue list = G_VALUE_INIT;
2219 GstVideoColorimetry cinfo;
2220 enum v4l2_colorspace req_cspace;
2222 memset (&fmt, 0, sizeof (fmt));
2223 fmt.type = v4l2object->type;
2224 fmt.fmt.pix.width = width;
2225 fmt.fmt.pix.height = height;
2226 fmt.fmt.pix.pixelformat = pixelformat;
2228 g_value_init (&list, GST_TYPE_LIST);
2230 /* step 1: get device default colorspace and insert it first as
2231 * it should be the preferred one */
2232 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2233 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2234 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2237 /* step 2: probe all colorspace other than default
2238 * We don't probe all colorspace, range, matrix and transfer combination to
2239 * avoid ioctl flooding which could greatly increase initialization time
2240 * with low-speed devices (UVC...) */
2241 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2242 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++) {
2243 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2244 if (req_cspace == V4L2_COLORSPACE_BT878)
2247 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2248 fmt.fmt.pix_mp.colorspace = req_cspace;
2250 fmt.fmt.pix.colorspace = req_cspace;
2252 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2253 enum v4l2_colorspace colorspace;
2255 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2256 colorspace = fmt.fmt.pix_mp.colorspace;
2258 colorspace = fmt.fmt.pix.colorspace;
2260 if (colorspace == req_cspace) {
2261 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2262 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2267 if (gst_value_list_get_size (&list) > 0)
2268 gst_structure_take_value (s, "colorimetry", &list);
2270 g_value_unset (&list);
2275 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2276 static GstStructure *
2277 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2278 guint32 pixelformat,
2279 guint32 width, guint32 height, const GstStructure * template)
2281 gint fd = v4l2object->video_fd;
2282 struct v4l2_frmivalenum ival;
2285 GValue rates = { 0, };
2287 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2289 ival.pixel_format = pixelformat;
2291 ival.height = height;
2293 GST_LOG_OBJECT (v4l2object->element,
2294 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2295 GST_FOURCC_ARGS (pixelformat));
2297 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2298 * fraction to get framerate */
2299 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2300 goto enum_frameintervals_failed;
2302 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2303 GValue rate = { 0, };
2305 g_value_init (&rates, GST_TYPE_LIST);
2306 g_value_init (&rate, GST_TYPE_FRACTION);
2309 num = ival.discrete.numerator;
2310 denom = ival.discrete.denominator;
2312 if (num > G_MAXINT || denom > G_MAXINT) {
2313 /* let us hope we don't get here... */
2318 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
2321 /* swap to get the framerate */
2322 gst_value_set_fraction (&rate, denom, num);
2323 gst_value_list_append_value (&rates, &rate);
2326 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2327 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2328 GValue min = { 0, };
2329 GValue step = { 0, };
2330 GValue max = { 0, };
2331 gboolean added = FALSE;
2332 guint32 minnum, mindenom;
2333 guint32 maxnum, maxdenom;
2335 g_value_init (&rates, GST_TYPE_LIST);
2337 g_value_init (&min, GST_TYPE_FRACTION);
2338 g_value_init (&step, GST_TYPE_FRACTION);
2339 g_value_init (&max, GST_TYPE_FRACTION);
2342 minnum = ival.stepwise.min.numerator;
2343 mindenom = ival.stepwise.min.denominator;
2344 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2348 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
2350 gst_value_set_fraction (&min, minnum, mindenom);
2353 maxnum = ival.stepwise.max.numerator;
2354 maxdenom = ival.stepwise.max.denominator;
2355 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2360 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
2362 gst_value_set_fraction (&max, maxnum, maxdenom);
2365 num = ival.stepwise.step.numerator;
2366 denom = ival.stepwise.step.denominator;
2367 if (num > G_MAXINT || denom > G_MAXINT) {
2372 if (num == 0 || denom == 0) {
2373 /* in this case we have a wrong fraction or no step, set the step to max
2374 * so that we only add the min value in the loop below */
2379 /* since we only have gst_value_fraction_subtract and not add, negate the
2381 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
2383 gst_value_set_fraction (&step, -num, denom);
2385 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2386 GValue rate = { 0, };
2388 num = gst_value_get_fraction_numerator (&min);
2389 denom = gst_value_get_fraction_denominator (&min);
2390 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
2393 /* invert to get the framerate */
2394 g_value_init (&rate, GST_TYPE_FRACTION);
2395 gst_value_set_fraction (&rate, denom, num);
2396 gst_value_list_append_value (&rates, &rate);
2399 /* we're actually adding because step was negated above. This is because
2400 * there is no _add function... */
2401 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2402 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
2407 /* no range was added, leave the default range from the template */
2408 GST_WARNING_OBJECT (v4l2object->element,
2409 "no range added, leaving default");
2410 g_value_unset (&rates);
2412 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2413 guint32 maxnum, maxdenom;
2415 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2417 num = ival.stepwise.min.numerator;
2418 denom = ival.stepwise.min.denominator;
2419 if (num > G_MAXINT || denom > G_MAXINT) {
2424 maxnum = ival.stepwise.max.numerator;
2425 maxdenom = ival.stepwise.max.denominator;
2426 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2431 GST_LOG_OBJECT (v4l2object->element,
2432 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2435 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2441 s = gst_structure_copy (template);
2442 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2443 "height", G_TYPE_INT, (gint) height, NULL);
2444 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2445 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2447 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2449 if (G_IS_VALUE (&rates)) {
2450 gst_v4l2src_value_simplify (&rates);
2451 /* only change the framerate on the template when we have a valid probed new
2453 gst_structure_take_value (s, "framerate", &rates);
2454 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2455 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2456 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2462 enum_frameintervals_failed:
2464 GST_DEBUG_OBJECT (v4l2object->element,
2465 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2466 GST_FOURCC_ARGS (pixelformat), width, height);
2471 /* I don't see how this is actually an error, we ignore the format then */
2472 GST_WARNING_OBJECT (v4l2object->element,
2473 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2474 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2480 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2484 gst_structure_get_int (s1, "width", &w1);
2485 gst_structure_get_int (s1, "height", &h1);
2486 gst_structure_get_int (s2, "width", &w2);
2487 gst_structure_get_int (s2, "height", &h2);
2489 /* I think it's safe to assume that this won't overflow for a while */
2490 return ((w2 * h2) - (w1 * h1));
2494 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2495 guint32 format, GstCaps * caps, GstStructure * s)
2497 GstStructure *alt_s = NULL;
2499 /* Encoded stream on output buffer need to be parsed */
2500 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2501 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2504 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2505 if (format == gst_v4l2_formats[i].format &&
2506 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2507 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2508 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2514 if (v4l2object->has_alpha_component &&
2515 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2516 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2518 case V4L2_PIX_FMT_RGB32:
2519 alt_s = gst_structure_copy (s);
2520 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2522 case V4L2_PIX_FMT_BGR32:
2523 alt_s = gst_structure_copy (s);
2524 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2531 gst_caps_append_structure (caps, s);
2534 gst_caps_append_structure (caps, alt_s);
2538 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2539 guint32 pixelformat, const GstStructure * template)
2541 GstCaps *ret = gst_caps_new_empty ();
2543 gint fd = v4l2object->video_fd;
2544 struct v4l2_frmsizeenum size;
2545 GList *results = NULL;
2548 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2549 gst_caps_append_structure (ret, gst_structure_copy (template));
2553 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2555 size.pixel_format = pixelformat;
2557 GST_DEBUG_OBJECT (v4l2object->element,
2558 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2559 GST_FOURCC_ARGS (pixelformat));
2561 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2562 goto enum_framesizes_failed;
2564 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2566 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
2567 size.discrete.width, size.discrete.height);
2569 w = MIN (size.discrete.width, G_MAXINT);
2570 h = MIN (size.discrete.height, G_MAXINT);
2574 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2575 pixelformat, w, h, template);
2578 results = g_list_prepend (results, tmp);
2582 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2583 GST_DEBUG_OBJECT (v4l2object->element,
2584 "done iterating discrete frame sizes");
2585 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2586 guint32 maxw, maxh, step_w, step_h;
2588 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
2589 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2590 size.stepwise.min_width);
2591 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2592 size.stepwise.min_height);
2593 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2594 size.stepwise.max_width);
2595 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2596 size.stepwise.max_height);
2597 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
2598 size.stepwise.step_width);
2599 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
2600 size.stepwise.step_height);
2602 w = MAX (size.stepwise.min_width, 1);
2603 h = MAX (size.stepwise.min_height, 1);
2604 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2605 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2607 step_w = MAX (size.stepwise.step_width, 1);
2608 step_h = MAX (size.stepwise.step_height, 1);
2610 /* FIXME: check for sanity and that min/max are multiples of the steps */
2612 /* we only query details for the max width/height since it's likely the
2613 * most restricted if there are any resolution-dependent restrictions */
2614 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2615 pixelformat, maxw, maxh, template);
2618 GValue step_range = G_VALUE_INIT;
2620 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2621 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2622 gst_structure_set_value (tmp, "width", &step_range);
2624 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2625 gst_structure_take_value (tmp, "height", &step_range);
2627 /* no point using the results list here, since there's only one struct */
2628 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2630 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2633 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
2634 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2635 size.stepwise.min_width);
2636 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2637 size.stepwise.min_height);
2638 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2639 size.stepwise.max_width);
2640 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2641 size.stepwise.max_height);
2643 w = MAX (size.stepwise.min_width, 1);
2644 h = MAX (size.stepwise.min_height, 1);
2645 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2646 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2649 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2652 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2653 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2656 /* no point using the results list here, since there's only one struct */
2657 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2663 /* we use an intermediary list to store and then sort the results of the
2664 * probing because we can't make any assumptions about the order in which
2665 * the driver will give us the sizes, but we want the final caps to contain
2666 * the results starting with the highest resolution and having the lowest
2667 * resolution last, since order in caps matters for things like fixation. */
2668 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2669 while (results != NULL) {
2670 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
2672 results = g_list_delete_link (results, results);
2675 if (gst_caps_is_empty (ret))
2676 goto enum_framesizes_no_results;
2681 enum_framesizes_failed:
2683 /* I don't see how this is actually an error */
2684 GST_DEBUG_OBJECT (v4l2object->element,
2685 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2686 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2687 goto default_frame_sizes;
2689 enum_framesizes_no_results:
2691 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2692 * question doesn't actually support it yet */
2693 GST_DEBUG_OBJECT (v4l2object->element,
2694 "No results for pixelformat %" GST_FOURCC_FORMAT
2695 " enumerating frame sizes, trying fallback",
2696 GST_FOURCC_ARGS (pixelformat));
2697 goto default_frame_sizes;
2701 GST_WARNING_OBJECT (v4l2object->element,
2702 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2703 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2704 goto default_frame_sizes;
2707 default_frame_sizes:
2709 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2711 /* This code is for Linux < 2.6.19 */
2713 max_w = max_h = GST_V4L2_MAX_SIZE;
2714 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2716 GST_WARNING_OBJECT (v4l2object->element,
2717 "Could not probe minimum capture size for pixelformat %"
2718 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2720 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2722 GST_WARNING_OBJECT (v4l2object->element,
2723 "Could not probe maximum capture size for pixelformat %"
2724 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2727 /* Since we can't get framerate directly, try to use the current norm */
2728 if (v4l2object->tv_norm && v4l2object->norms) {
2730 GstTunerNorm *norm = NULL;
2731 GstTunerNorm *current =
2732 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2734 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2735 norm = (GstTunerNorm *) norms->data;
2736 if (!strcmp (norm->label, current->label))
2739 /* If it's possible, set framerate to that (discrete) value */
2741 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2742 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2746 tmp = gst_structure_copy (template);
2748 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2750 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2751 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2752 /* if norm can't be used, copy the template framerate */
2753 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2758 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2760 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2763 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2765 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2767 /* We could consider setting interlace mode from min and max. */
2768 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
2770 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2771 /* We could consider to check colorspace for min too, in case it depends on
2772 * the size. But in this case, min and max could not be enough */
2773 gst_v4l2_object_add_colorspace (v4l2object, tmp, max_w, max_h, pixelformat);
2775 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2781 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2782 guint32 pixelformat, gint * width, gint * height)
2784 struct v4l2_format fmt;
2785 gboolean ret = FALSE;
2786 GstVideoInterlaceMode interlace_mode;
2788 g_return_val_if_fail (width != NULL, FALSE);
2789 g_return_val_if_fail (height != NULL, FALSE);
2791 GST_LOG_OBJECT (v4l2object->element,
2792 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2793 *width, *height, GST_FOURCC_ARGS (pixelformat));
2795 memset (&fmt, 0, sizeof (struct v4l2_format));
2797 /* get size delimiters */
2798 memset (&fmt, 0, sizeof (fmt));
2799 fmt.type = v4l2object->type;
2800 fmt.fmt.pix.width = *width;
2801 fmt.fmt.pix.height = *height;
2802 fmt.fmt.pix.pixelformat = pixelformat;
2803 fmt.fmt.pix.field = V4L2_FIELD_ANY;
2805 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
2808 GST_LOG_OBJECT (v4l2object->element,
2809 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2811 *width = fmt.fmt.pix.width;
2812 *height = fmt.fmt.pix.height;
2814 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2815 GST_WARNING_OBJECT (v4l2object->element,
2816 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
2817 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
2825 GST_WARNING_OBJECT (v4l2object->element,
2826 "Unable to try format: %s", g_strerror (errno));
2833 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2837 GST_DEBUG_OBJECT (v4l2object->element, "initializing the %s system",
2838 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
2840 GST_V4L2_CHECK_OPEN (v4l2object);
2841 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2843 /* find transport */
2844 mode = v4l2object->req_mode;
2846 if (v4l2object->device_caps & V4L2_CAP_READWRITE) {
2847 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2848 mode = GST_V4L2_IO_RW;
2849 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2850 goto method_not_supported;
2852 if (v4l2object->device_caps & V4L2_CAP_STREAMING) {
2853 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2854 mode = GST_V4L2_IO_MMAP;
2855 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2856 goto method_not_supported;
2858 /* if still no transport selected, error out */
2859 if (mode == GST_V4L2_IO_AUTO)
2860 goto no_supported_capture_method;
2862 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2863 v4l2object->mode = mode;
2865 /* If min_buffers is not set, the driver either does not support the control or
2866 it has not been asked yet via propose_allocation/decide_allocation. */
2867 if (!v4l2object->min_buffers)
2868 gst_v4l2_get_driver_min_buffers (v4l2object);
2870 /* Map the buffers */
2871 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2873 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2874 goto buffer_pool_new_failed;
2876 GST_V4L2_SET_ACTIVE (v4l2object);
2881 buffer_pool_new_failed:
2883 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2884 (_("Could not map buffers from device '%s'"),
2885 v4l2object->videodev),
2886 ("Failed to create buffer pool: %s", g_strerror (errno)));
2889 method_not_supported:
2891 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2892 (_("The driver of device '%s' does not support the IO method %d"),
2893 v4l2object->videodev, mode), (NULL));
2896 no_supported_capture_method:
2898 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2899 (_("The driver of device '%s' does not support any known IO "
2900 "method."), v4l2object->videodev), (NULL));
2906 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
2907 gint plane, gint stride)
2909 const GstVideoFormatInfo *finfo = info->finfo;
2911 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2912 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
2915 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
2916 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2917 tile_height = 1 << hs;
2919 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
2920 info->height + align->padding_top + align->padding_bottom);
2921 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
2923 x_tiles = stride >> ws;
2924 y_tiles = padded_height >> hs;
2925 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
2927 info->stride[plane] = stride;
2932 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
2933 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
2935 const GstVideoFormatInfo *finfo = info->finfo;
2936 gint i, estride, padded_height;
2939 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
2941 padded_height = info->height + align->padding_top + align->padding_bottom;
2943 for (i = 0; i < finfo->n_planes; i++) {
2944 estride = gst_v4l2_object_extrapolate_stride (finfo, i, stride);
2946 gst_v4l2_object_set_stride (info, align, i, estride);
2948 info->offset[i] = offs;
2950 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
2952 GST_DEBUG_OBJECT (v4l2object->element,
2953 "Extrapolated for plane %d with base stride %d: "
2954 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
2960 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
2961 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
2962 GstVideoInfo * info, GstVideoAlignment * align)
2964 const GstVideoFormatInfo *finfo = info->finfo;
2965 gboolean standard_stride = TRUE;
2966 gint stride, padded_width, padded_height, i;
2968 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
2969 v4l2object->n_v4l2_planes = 1;
2970 info->size = format->fmt.pix.sizeimage;
2974 /* adjust right padding */
2975 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2976 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
2978 stride = format->fmt.pix.bytesperline;
2980 padded_width = stride / GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
2982 if (padded_width < format->fmt.pix.width)
2983 GST_WARNING_OBJECT (v4l2object->element,
2984 "Driver bug detected, stride is too small for the width");
2986 align->padding_right = padded_width - info->width - align->padding_left;
2988 /* adjust bottom padding */
2989 padded_height = format->fmt.pix.height;
2991 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2992 guint hs, tile_height;
2994 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2995 tile_height = 1 << hs;
2997 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3000 align->padding_bottom = padded_height - info->height - align->padding_top;
3002 /* setup the strides and offset */
3003 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
3004 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
3006 /* figure out the frame layout */
3007 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
3009 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
3010 stride = pix_mp->plane_fmt[i].bytesperline;
3012 if (info->stride[i] != stride)
3013 standard_stride = FALSE;
3015 gst_v4l2_object_set_stride (info, align, i, stride);
3016 info->offset[i] = info->size;
3017 info->size += pix_mp->plane_fmt[i].sizeimage;
3020 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3021 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
3022 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3023 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3026 /* only one plane in non-MPLANE mode */
3027 v4l2object->n_v4l2_planes = 1;
3028 info->size = format->fmt.pix.sizeimage;
3029 stride = format->fmt.pix.bytesperline;
3031 if (info->stride[0] != stride)
3032 standard_stride = FALSE;
3034 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3037 /* adjust the offset to take into account left and top */
3038 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3039 if ((align->padding_left + align->padding_top) > 0)
3040 GST_WARNING_OBJECT (v4l2object->element,
3041 "Left and top padding is not permitted for tiled formats");
3043 for (i = 0; i < finfo->n_planes; i++) {
3046 /* FIXME we assume plane as component as this is true for all supported
3047 * format we support. */
3049 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left);
3050 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top);
3052 info->offset[i] += (vedge * info->stride[i]) +
3053 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i));
3058 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %" G_GSIZE_FORMAT,
3061 /* to avoid copies we need video meta if top or left padding */
3062 v4l2object->need_video_meta =
3063 ((align->padding_top + align->padding_left) != 0);
3065 /* ... or if stride is non "standard" */
3066 if (!standard_stride)
3067 v4l2object->need_video_meta = TRUE;
3069 /* ... or also video meta if we use multiple, non-contiguous, planes */
3070 if (v4l2object->n_v4l2_planes > 1)
3071 v4l2object->need_video_meta = TRUE;
3073 v4l2object->info = *info;
3074 v4l2object->align = *align;
3075 v4l2object->format = *format;
3076 v4l2object->fmtdesc = fmtdesc;
3078 /* if we have a framerate pre-calculate duration */
3079 if (info->fps_n > 0 && info->fps_d > 0) {
3080 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
3083 v4l2object->duration = GST_CLOCK_TIME_NONE;
3088 gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
3089 gint plane, gint stride)
3093 switch (finfo->format) {
3094 case GST_VIDEO_FORMAT_NV12:
3095 case GST_VIDEO_FORMAT_NV12_64Z32:
3096 case GST_VIDEO_FORMAT_NV21:
3097 case GST_VIDEO_FORMAT_NV16:
3098 case GST_VIDEO_FORMAT_NV61:
3099 case GST_VIDEO_FORMAT_NV24:
3100 estride = (plane == 0 ? 1 : 2) *
3101 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3104 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3112 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3113 gboolean try_only, GstV4l2Error * error)
3115 gint fd = v4l2object->video_fd;
3116 struct v4l2_format format;
3117 struct v4l2_streamparm streamparm;
3118 enum v4l2_field field;
3119 guint32 pixelformat;
3120 struct v4l2_fmtdesc *fmtdesc;
3122 GstVideoAlignment align;
3123 gint width, height, fps_n, fps_d;
3127 enum v4l2_colorspace colorspace = 0;
3128 enum v4l2_quantization range = 0;
3129 enum v4l2_ycbcr_encoding matrix = 0;
3130 enum v4l2_xfer_func transfer = 0;
3132 GST_V4L2_CHECK_OPEN (v4l2object);
3134 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3136 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3138 gst_video_info_init (&info);
3139 gst_video_alignment_reset (&align);
3141 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3144 pixelformat = fmtdesc->pixelformat;
3145 width = GST_VIDEO_INFO_WIDTH (&info);
3146 height = GST_VIDEO_INFO_HEIGHT (&info);
3147 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3148 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3150 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3151 * or if contiguous is prefered */
3152 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3153 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3156 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
3157 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
3158 /* ideally we would differentiate between types of interlaced video
3159 * but there is not sufficient information in the caps..
3161 field = V4L2_FIELD_INTERLACED;
3163 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
3164 field = V4L2_FIELD_NONE;
3167 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3168 /* We first pick th main colorspace from the primaries */
3169 switch (info.colorimetry.primaries) {
3170 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3171 /* There is two colorspaces using these primaries, use the range to
3173 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3174 colorspace = V4L2_COLORSPACE_REC709;
3176 colorspace = V4L2_COLORSPACE_SRGB;
3178 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3179 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3181 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3182 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3184 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3185 colorspace = V4L2_COLORSPACE_SMPTE170M;
3187 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3188 colorspace = V4L2_COLORSPACE_SMPTE240M;
3191 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3192 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3193 /* We don't know, we will guess */
3197 GST_WARNING_OBJECT (v4l2object->element,
3198 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3202 switch (info.colorimetry.range) {
3203 case GST_VIDEO_COLOR_RANGE_0_255:
3204 range = V4L2_QUANTIZATION_FULL_RANGE;
3206 case GST_VIDEO_COLOR_RANGE_16_235:
3207 range = V4L2_QUANTIZATION_LIM_RANGE;
3209 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3210 /* We let the driver pick a default one */
3213 GST_WARNING_OBJECT (v4l2object->element,
3214 "Unknown colorimetry range %d", info.colorimetry.range);
3218 switch (info.colorimetry.matrix) {
3219 case GST_VIDEO_COLOR_MATRIX_RGB:
3220 /* Unspecified, leave to default */
3222 /* FCC is about the same as BT601 with less digit */
3223 case GST_VIDEO_COLOR_MATRIX_FCC:
3224 case GST_VIDEO_COLOR_MATRIX_BT601:
3225 matrix = V4L2_YCBCR_ENC_601;
3227 case GST_VIDEO_COLOR_MATRIX_BT709:
3228 matrix = V4L2_YCBCR_ENC_709;
3230 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3231 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3233 case GST_VIDEO_COLOR_MATRIX_BT2020:
3234 matrix = V4L2_YCBCR_ENC_BT2020;
3236 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3237 /* We let the driver pick a default one */
3240 GST_WARNING_OBJECT (v4l2object->element,
3241 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3245 switch (info.colorimetry.transfer) {
3246 case GST_VIDEO_TRANSFER_GAMMA18:
3247 case GST_VIDEO_TRANSFER_GAMMA20:
3248 case GST_VIDEO_TRANSFER_GAMMA22:
3249 case GST_VIDEO_TRANSFER_GAMMA28:
3250 GST_WARNING_OBJECT (v4l2object->element,
3251 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3253 case GST_VIDEO_TRANSFER_GAMMA10:
3254 transfer = V4L2_XFER_FUNC_NONE;
3256 case GST_VIDEO_TRANSFER_BT709:
3257 transfer = V4L2_XFER_FUNC_709;
3259 case GST_VIDEO_TRANSFER_SMPTE240M:
3260 transfer = V4L2_XFER_FUNC_SMPTE240M;
3262 case GST_VIDEO_TRANSFER_SRGB:
3263 transfer = V4L2_XFER_FUNC_SRGB;
3265 case GST_VIDEO_TRANSFER_LOG100:
3266 case GST_VIDEO_TRANSFER_LOG316:
3267 GST_WARNING_OBJECT (v4l2object->element,
3268 "LOG 100, 316 transfer functions not supported");
3269 /* FIXME No known sensible default, maybe AdobeRGB ? */
3271 case GST_VIDEO_TRANSFER_UNKNOWN:
3272 /* We let the driver pick a default one */
3275 GST_WARNING_OBJECT (v4l2object->element,
3276 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
3280 if (colorspace == 0) {
3281 /* Try to guess colorspace according to pixelformat and size */
3282 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3283 /* SD streams likely use SMPTE170M and HD streams REC709 */
3284 if (width <= 720 && height <= 576)
3285 colorspace = V4L2_COLORSPACE_SMPTE170M;
3287 colorspace = V4L2_COLORSPACE_REC709;
3288 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3289 colorspace = V4L2_COLORSPACE_SRGB;
3290 transfer = V4L2_XFER_FUNC_NONE;
3295 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
3296 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3297 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3299 memset (&format, 0x00, sizeof (struct v4l2_format));
3300 format.type = v4l2object->type;
3303 format.type = v4l2object->type;
3304 format.fmt.pix_mp.pixelformat = pixelformat;
3305 format.fmt.pix_mp.width = width;
3306 format.fmt.pix_mp.height = height;
3307 format.fmt.pix_mp.field = field;
3308 format.fmt.pix_mp.num_planes = n_v4l_planes;
3310 /* try to ask our prefered stride but it's not a failure if not
3312 for (i = 0; i < n_v4l_planes; i++) {
3313 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3315 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3316 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3317 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3319 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3322 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3323 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3325 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3327 format.type = v4l2object->type;
3328 format.fmt.pix.width = width;
3329 format.fmt.pix.height = height;
3330 format.fmt.pix.pixelformat = pixelformat;
3331 format.fmt.pix.field = field;
3333 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3334 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3335 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3337 /* try to ask our prefered stride */
3338 format.fmt.pix.bytesperline = stride;
3340 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3341 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3344 GST_DEBUG_OBJECT (v4l2object->element, "Desired format is %dx%d, format "
3345 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3346 format.fmt.pix_mp.height,
3347 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3348 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3350 #ifndef GST_DISABLE_GST_DEBUG
3352 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3353 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3354 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3356 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3357 format.fmt.pix.bytesperline);
3361 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3363 format.fmt.pix_mp.colorspace = colorspace;
3364 format.fmt.pix_mp.quantization = range;
3365 format.fmt.pix_mp.ycbcr_enc = matrix;
3366 format.fmt.pix_mp.xfer_func = transfer;
3368 format.fmt.pix.colorspace = colorspace;
3369 format.fmt.pix.quantization = range;
3370 format.fmt.pix.ycbcr_enc = matrix;
3371 format.fmt.pix.xfer_func = transfer;
3374 GST_DEBUG_OBJECT (v4l2object->element, "Desired colorspace is %d:%d:%d:%d",
3375 colorspace, range, matrix, transfer);
3379 if (v4l2_ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3380 goto try_fmt_failed;
3382 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3383 goto set_fmt_failed;
3386 GST_DEBUG_OBJECT (v4l2object->element, "Got format of %dx%d, format "
3387 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d",
3388 format.fmt.pix.width, format.fmt.pix_mp.height,
3389 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3390 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3391 is_mplane ? format.fmt.pix_mp.colorspace : format.fmt.pix.colorspace);
3393 #ifndef GST_DISABLE_GST_DEBUG
3395 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3396 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3397 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3398 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3400 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3401 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3405 if (format.fmt.pix.pixelformat != pixelformat)
3406 goto invalid_pixelformat;
3408 /* Only negotiate size with raw data.
3409 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3410 * in ASF mode for example, there is also not reason for a driver to
3411 * change the size. */
3412 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3413 /* We can crop larger images */
3414 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3415 goto invalid_dimensions;
3417 /* Note, this will be adjusted if upstream has non-centered cropping. */
3418 align.padding_top = 0;
3419 align.padding_bottom = format.fmt.pix.height - height;
3420 align.padding_left = 0;
3421 align.padding_right = format.fmt.pix.width - width;
3424 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3425 goto invalid_planes;
3427 if (try_only) /* good enough for trying only */
3430 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
3431 struct v4l2_control ctl = { 0, };
3432 ctl.id = V4L2_CID_ALPHA_COMPONENT;
3435 if (v4l2_ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
3436 GST_WARNING_OBJECT (v4l2object->element,
3437 "Failed to set alpha component value");
3440 /* Is there a reason we require the caller to always specify a framerate? */
3441 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
3444 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
3445 streamparm.type = v4l2object->type;
3447 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
3448 goto get_parm_failed;
3450 GST_VIDEO_INFO_FPS_N (&info) =
3451 streamparm.parm.capture.timeperframe.denominator;
3452 GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
3454 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
3455 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3456 GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
3457 streamparm.parm.capture.timeperframe.denominator,
3458 streamparm.parm.capture.timeperframe.numerator);
3460 /* We used to skip frame rate setup if the camera was already setup
3461 * with the requested frame rate. This breaks some cameras though,
3462 * causing them to not output data (several models of Thinkpad cameras
3463 * have this problem at least).
3464 * So, don't skip. */
3465 GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
3467 /* We want to change the frame rate, so check whether we can. Some cheap USB
3468 * cameras don't have the capability */
3469 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3470 GST_DEBUG_OBJECT (v4l2object->element,
3471 "Not setting framerate (not supported)");
3475 /* Note: V4L2 wants the frame interval, we have the frame rate */
3476 streamparm.parm.capture.timeperframe.numerator = fps_d;
3477 streamparm.parm.capture.timeperframe.denominator = fps_n;
3479 /* some cheap USB cam's won't accept any change */
3480 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3481 goto set_parm_failed;
3483 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
3484 streamparm.parm.capture.timeperframe.denominator > 0) {
3485 /* get new values */
3486 fps_d = streamparm.parm.capture.timeperframe.numerator;
3487 fps_n = streamparm.parm.capture.timeperframe.denominator;
3489 GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
3492 /* fix v4l2 capture driver to provide framerate values */
3493 GST_WARNING_OBJECT (v4l2object->element,
3494 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
3497 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3498 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3502 /* add boolean return, so we can fail on drivers bugs */
3503 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
3505 /* now configure the pool */
3506 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
3514 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
3520 if (errno == EBUSY) {
3521 GST_V4L2_ERROR (error, RESOURCE, BUSY,
3522 (_("Device '%s' is busy"), v4l2object->videodev),
3523 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3524 GST_FOURCC_ARGS (pixelformat), width, height,
3525 g_strerror (errno)));
3531 if (errno == EBUSY) {
3532 GST_V4L2_ERROR (error, RESOURCE, BUSY,
3533 (_("Device '%s' is busy"), v4l2object->videodev),
3534 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3535 GST_FOURCC_ARGS (pixelformat), width, height,
3536 g_strerror (errno)));
3538 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3539 (_("Device '%s' cannot capture at %dx%d"),
3540 v4l2object->videodev, width, height),
3541 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3542 GST_FOURCC_ARGS (pixelformat), width, height,
3543 g_strerror (errno)));
3550 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3551 (_("Device '%s' cannot capture at %dx%d"),
3552 v4l2object->videodev, width, height),
3553 ("Tried to capture at %dx%d, but device returned size %dx%d",
3554 width, height, format.fmt.pix.width, format.fmt.pix.height));
3558 invalid_pixelformat:
3561 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3562 (_("Device '%s' cannot capture in the specified format"),
3563 v4l2object->videodev),
3564 ("Tried to capture in %" GST_FOURCC_FORMAT
3565 ", but device returned format" " %" GST_FOURCC_FORMAT,
3566 GST_FOURCC_ARGS (pixelformat),
3567 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
3574 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3575 (_("Device '%s' does support non-contiguous planes"),
3576 v4l2object->videodev),
3577 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
3583 /* it's possible that this call is not supported */
3584 if (errno != EINVAL && errno != ENOTTY) {
3585 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3586 (_("Could not get parameters on device '%s'"),
3587 v4l2object->videodev), GST_ERROR_SYSTEM);
3593 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3594 (_("Video device did not accept new frame rate setting.")),
3600 /* setup_pool already send the error */
3606 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps,
3607 GstV4l2Error * error)
3609 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE, error);
3613 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps,
3614 GstV4l2Error * error)
3616 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE, error);
3620 * gst_v4l2_object_acquire_format:
3621 * @v4l2object the object
3622 * @info a GstVideoInfo to be filled
3624 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
3625 * the output format is choosen by the HW.
3627 * Returns: %TRUE on success, %FALSE on failure.
3630 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
3632 struct v4l2_fmtdesc *fmtdesc;
3633 struct v4l2_format fmt;
3634 struct v4l2_crop crop;
3635 struct v4l2_selection sel;
3636 struct v4l2_rect *r = NULL;
3637 GstVideoFormat format;
3638 guint width, height;
3639 GstVideoAlignment align;
3641 gst_video_info_init (info);
3642 gst_video_alignment_reset (&align);
3644 memset (&fmt, 0x00, sizeof (struct v4l2_format));
3645 fmt.type = v4l2object->type;
3646 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
3647 goto get_fmt_failed;
3649 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
3650 fmt.fmt.pix.pixelformat);
3651 if (fmtdesc == NULL)
3652 goto unsupported_format;
3654 /* No need to care about mplane, the four first params are the same */
3655 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
3657 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
3658 if (format == GST_VIDEO_FORMAT_UNKNOWN)
3659 goto unsupported_format;
3661 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
3662 goto invalid_dimensions;
3664 width = fmt.fmt.pix.width;
3665 height = fmt.fmt.pix.height;
3667 /* Use the default compose rectangle */
3668 memset (&sel, 0, sizeof (struct v4l2_selection));
3669 sel.type = v4l2object->type;
3670 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
3671 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0) {
3674 /* For ancient kernels, fall back to G_CROP */
3675 memset (&crop, 0, sizeof (struct v4l2_crop));
3676 crop.type = v4l2object->type;
3677 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
3681 align.padding_left = r->left;
3682 align.padding_top = r->top;
3683 align.padding_right = width - r->width - r->left;
3684 align.padding_bottom = height - r->height - r->top;
3689 gst_video_info_set_format (info, format, width, height);
3691 switch (fmt.fmt.pix.field) {
3692 case V4L2_FIELD_ANY:
3693 case V4L2_FIELD_NONE:
3694 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
3696 case V4L2_FIELD_INTERLACED:
3697 case V4L2_FIELD_INTERLACED_TB:
3698 case V4L2_FIELD_INTERLACED_BT:
3699 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
3702 goto unsupported_field;
3705 gst_v4l2_object_get_colorspace (&fmt, &info->colorimetry);
3707 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
3709 /* Shall we setup the pool ? */
3715 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3716 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
3721 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3722 (_("Video device returned invalid dimensions.")),
3723 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
3724 fmt.fmt.pix.height));
3729 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3730 (_("Video devices uses an unsupported interlacing method.")),
3731 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
3736 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3737 (_("Video devices uses an unsupported pixel format.")),
3738 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
3739 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
3745 gst_v4l2_object_set_crop (GstV4l2Object * obj)
3747 struct v4l2_crop crop = { 0 };
3749 crop.type = obj->type;
3750 crop.c.left = obj->align.padding_left;
3751 crop.c.top = obj->align.padding_top;
3752 crop.c.width = obj->info.width;
3753 crop.c.height = obj->info.height;
3755 if (obj->align.padding_left + obj->align.padding_top +
3756 obj->align.padding_right + obj->align.padding_bottom == 0) {
3757 GST_DEBUG_OBJECT (obj->element, "no cropping needed");
3761 GST_DEBUG_OBJECT (obj->element,
3762 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3763 crop.c.width, crop.c.height);
3765 if (v4l2_ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
3766 GST_WARNING_OBJECT (obj->element, "VIDIOC_S_CROP failed");
3770 if (v4l2_ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
3771 GST_WARNING_OBJECT (obj->element, "VIDIOC_G_CROP failed");
3775 GST_DEBUG_OBJECT (obj->element,
3776 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3777 crop.c.width, crop.c.height);
3783 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
3785 GstStructure *config;
3789 if (!v4l2object->pool)
3792 config = gst_buffer_pool_get_config (v4l2object->pool);
3793 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
3795 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
3797 gst_structure_free (config);
3803 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
3805 gboolean ret = TRUE;
3807 GST_LOG_OBJECT (v4l2object->element, "start flushing");
3809 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3810 gst_buffer_pool_set_flushing (v4l2object->pool, TRUE);
3816 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
3818 gboolean ret = TRUE;
3820 GST_LOG_OBJECT (v4l2object->element, "stop flushing");
3822 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3823 gst_buffer_pool_set_flushing (v4l2object->pool, FALSE);
3829 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
3831 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
3833 if (!GST_V4L2_IS_OPEN (v4l2object))
3835 if (!GST_V4L2_IS_ACTIVE (v4l2object))
3838 if (v4l2object->pool) {
3839 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
3840 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
3841 gst_object_unref (v4l2object->pool);
3842 v4l2object->pool = NULL;
3845 GST_V4L2_SET_INACTIVE (v4l2object);
3852 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3858 formats = gst_v4l2_object_get_format_list (v4l2object);
3860 ret = gst_caps_new_empty ();
3862 for (walk = formats; walk; walk = walk->next) {
3863 struct v4l2_fmtdesc *format;
3864 GstStructure *template;
3866 format = (struct v4l2_fmtdesc *) walk->data;
3868 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
3873 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
3874 format->pixelformat, template);
3876 gst_caps_append (ret, tmp);
3878 gst_structure_free (template);
3880 GST_DEBUG_OBJECT (v4l2object->element, "unknown format %u",
3881 format->pixelformat);
3889 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
3890 gst_caps_unref (tmp);
3897 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3901 if (v4l2object->probed_caps == NULL)
3902 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
3905 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
3906 GST_CAPS_INTERSECT_FIRST);
3908 ret = gst_caps_ref (v4l2object->probed_caps);
3911 GST_INFO_OBJECT (v4l2object->element, "probed caps: %" GST_PTR_FORMAT, ret);
3912 LOG_CAPS (v4l2object->element, ret);
3918 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
3921 GstBufferPool *pool = NULL, *other_pool = NULL;
3922 GstStructure *config;
3923 guint size, min, max, own_min = 0;
3925 gboolean has_video_meta;
3926 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
3927 GstAllocator *allocator = NULL;
3928 GstAllocationParams params = { 0 };
3930 GST_DEBUG_OBJECT (obj->element, "decide allocation");
3932 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3933 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
3935 gst_query_parse_allocation (query, &caps, NULL);
3937 if (obj->pool == NULL) {
3938 if (!gst_v4l2_object_setup_pool (obj, caps))
3942 if (gst_query_get_n_allocation_params (query) > 0)
3943 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3945 if (gst_query_get_n_allocation_pools (query) > 0) {
3946 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3955 GST_DEBUG_OBJECT (obj->element, "allocation: size:%u min:%u max:%u pool:%"
3956 GST_PTR_FORMAT, size, min, max, pool);
3959 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
3961 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
3963 gst_v4l2_get_driver_min_buffers (obj);
3964 /* We can't share our own pool, if it exceed V4L2 capacity */
3965 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
3966 can_share_own_pool = FALSE;
3969 switch (obj->mode) {
3970 case GST_V4L2_IO_RW:
3972 /* in READ/WRITE mode, prefer a downstream pool because our own pool
3973 * doesn't help much, we have to write to it as well */
3974 GST_DEBUG_OBJECT (obj->element,
3975 "read/write mode: using downstream pool");
3976 /* use the bigest size, when we use our own pool we can't really do any
3977 * other size than what the hardware gives us but for downstream pools
3979 size = MAX (size, obj->info.size);
3980 } else if (can_share_own_pool) {
3981 /* no downstream pool, use our own then */
3982 GST_DEBUG_OBJECT (obj->element,
3983 "read/write mode: no downstream pool, using our own");
3984 pool = gst_object_ref (obj->pool);
3985 size = obj->info.size;
3986 pushing_from_our_pool = TRUE;
3990 case GST_V4L2_IO_USERPTR:
3991 case GST_V4L2_IO_DMABUF_IMPORT:
3992 /* in importing mode, prefer our own pool, and pass the other pool to
3993 * our own, so it can serve itself */
3995 goto no_downstream_pool;
3996 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj->pool),
3999 gst_object_unref (pool);
4000 pool = gst_object_ref (obj->pool);
4001 size = obj->info.size;
4004 case GST_V4L2_IO_MMAP:
4005 case GST_V4L2_IO_DMABUF:
4006 /* in streaming mode, prefer our own pool */
4007 /* Check if we can use it ... */
4008 if (can_share_own_pool) {
4010 gst_object_unref (pool);
4011 pool = gst_object_ref (obj->pool);
4012 size = obj->info.size;
4013 GST_DEBUG_OBJECT (obj->element,
4014 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
4015 pushing_from_our_pool = TRUE;
4017 GST_DEBUG_OBJECT (obj->element,
4018 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
4021 GST_DEBUG_OBJECT (obj->element,
4022 "streaming mode: no usable pool, copying to generic pool");
4023 size = MAX (size, obj->info.size);
4026 case GST_V4L2_IO_AUTO:
4028 GST_WARNING_OBJECT (obj->element, "unhandled mode");
4035 /* If pushing from our own pool, configure it with queried minimum,
4036 * otherwise use the minimum required */
4037 if (pushing_from_our_pool) {
4038 /* When pushing from our own pool, we need what downstream one, to be able
4039 * to fill the pipeline, the minimum required to decoder according to the
4040 * driver and 1 more, so we don't endup up with everything downstream or
4041 * held by the decoder. */
4042 own_min = min + obj->min_buffers + 1;
4044 /* If no allocation parameters where provided, allow for a little more
4045 * buffers and enable copy threshold */
4048 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4051 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4056 /* In this case we'll have to configure two buffer pool. For our buffer
4057 * pool, we'll need what the driver one, and one more, so we can dequeu */
4058 own_min = obj->min_buffers + 1;
4059 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS);
4061 /* for the downstream pool, we keep what downstream wants, though ensure
4062 * at least a minimum if downstream didn't suggest anything (we are
4063 * expecting the base class to create a default one for the context) */
4064 min = MAX (min, GST_V4L2_MIN_BUFFERS);
4066 /* To import we need the other pool to hold at least own_min */
4067 if (obj->pool == pool)
4071 /* Request a bigger max, if one was suggested but it's too small */
4073 max = MAX (min, max);
4075 /* First step, configure our own pool */
4076 config = gst_buffer_pool_get_config (obj->pool);
4078 if (obj->need_video_meta || has_video_meta) {
4079 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
4080 gst_buffer_pool_config_add_option (config,
4081 GST_BUFFER_POOL_OPTION_VIDEO_META);
4084 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4085 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
4087 GST_DEBUG_OBJECT (obj->element, "setting own pool config to %"
4088 GST_PTR_FORMAT, config);
4090 /* Our pool often need to adjust the value */
4091 if (!gst_buffer_pool_set_config (obj->pool, config)) {
4092 config = gst_buffer_pool_get_config (obj->pool);
4094 GST_DEBUG_OBJECT (obj->element, "own pool config changed to %"
4095 GST_PTR_FORMAT, config);
4097 /* our pool will adjust the maximum buffer, which we are fine with */
4098 if (!gst_buffer_pool_set_config (obj->pool, config))
4102 /* Now configure the other pool if different */
4103 if (obj->pool != pool)
4107 config = gst_buffer_pool_get_config (other_pool);
4108 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4109 gst_buffer_pool_config_set_params (config, caps, size, min, max);
4111 GST_DEBUG_OBJECT (obj->element, "setting other pool config to %"
4112 GST_PTR_FORMAT, config);
4114 /* if downstream supports video metadata, add this to the pool config */
4115 if (has_video_meta) {
4116 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
4117 gst_buffer_pool_config_add_option (config,
4118 GST_BUFFER_POOL_OPTION_VIDEO_META);
4121 if (!gst_buffer_pool_set_config (other_pool, config)) {
4122 config = gst_buffer_pool_get_config (other_pool);
4124 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
4126 gst_structure_free (config);
4130 if (!gst_buffer_pool_set_config (other_pool, config))
4136 /* For simplicity, simply read back the active configuration, so our base
4137 * class get the right information */
4138 config = gst_buffer_pool_get_config (pool);
4139 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
4140 gst_structure_free (config);
4144 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
4146 gst_query_add_allocation_pool (query, pool, size, min, max);
4149 gst_object_unref (allocator);
4152 gst_object_unref (pool);
4158 /* setup_pool already send the error */
4163 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4164 (_("Failed to configure internal buffer pool.")), (NULL));
4169 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4170 (_("Video device did not suggest any buffer size.")), (NULL));
4176 gst_object_unref (allocator);
4179 gst_object_unref (pool);
4184 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4185 (_("No downstream pool to import from.")),
4186 ("When importing DMABUF or USERPTR, we need a pool to import from"));
4192 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
4194 GstBufferPool *pool;
4195 /* we need at least 2 buffers to operate */
4196 guint size, min, max;
4200 /* Set defaults allocation parameters */
4201 size = obj->info.size;
4202 min = GST_V4L2_MIN_BUFFERS;
4203 max = VIDEO_MAX_FRAME;
4205 gst_query_parse_allocation (query, &caps, &need_pool);
4210 if ((pool = obj->pool))
4211 gst_object_ref (pool);
4215 GstStructure *config;
4217 /* we had a pool, check caps */
4218 config = gst_buffer_pool_get_config (pool);
4219 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
4221 GST_DEBUG_OBJECT (obj->element,
4222 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
4223 if (!gst_caps_is_equal (caps, pcaps)) {
4224 gst_structure_free (config);
4225 gst_object_unref (pool);
4226 goto different_caps;
4228 gst_structure_free (config);
4230 gst_v4l2_get_driver_min_buffers (obj);
4232 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS);
4234 gst_query_add_allocation_pool (query, pool, size, min, max);
4236 /* we also support various metadata */
4237 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4240 gst_object_unref (pool);
4247 GST_DEBUG_OBJECT (obj->element, "no caps specified");
4252 /* different caps, we can't use this pool */
4253 GST_DEBUG_OBJECT (obj->element, "pool has different caps");