3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
6 * gstv4l2object.c: base class for V4L2 elements
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Library General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. This library is distributed in the hope
12 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
21 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
22 * with newer GLib versions (>= 2.31.0) */
23 #define GLIB_DISABLE_DEPRECATION_WARNINGS
36 #include <gudev/gudev.h>
39 #include "v4l2_calls.h"
40 #include "gstv4l2tuner.h"
41 #include "gstv4l2colorbalance.h"
43 #include "gst/gst-i18n-plugin.h"
45 #include <gst/video/video.h>
47 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
48 #define GST_CAT_DEFAULT v4l2_debug
50 #define DEFAULT_PROP_DEVICE_NAME NULL
51 #define DEFAULT_PROP_DEVICE_FD -1
52 #define DEFAULT_PROP_FLAGS 0
53 #define DEFAULT_PROP_TV_NORM 0
54 #define DEFAULT_PROP_CHANNEL NULL
55 #define DEFAULT_PROP_FREQUENCY 0
56 #define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
58 #define ENCODED_BUFFER_SIZE (1 * 1024 * 1024)
63 V4L2_STD_OBJECT_PROPS,
67 * common format / caps utilities:
71 GST_V4L2_RAW = 1 << 0,
72 GST_V4L2_CODEC = 1 << 1,
73 GST_V4L2_TRANSPORT = 1 << 2,
74 GST_V4L2_NO_PARSE = 1 << 3,
82 GstV4L2FormatFlags flags;
85 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
87 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
88 {V4L2_PIX_FMT_ARGB555, TRUE, GST_V4L2_RAW},
89 {V4L2_PIX_FMT_XRGB555, TRUE, GST_V4L2_RAW},
90 {V4L2_PIX_FMT_ARGB555X, TRUE, GST_V4L2_RAW},
91 {V4L2_PIX_FMT_XRGB555X, TRUE, GST_V4L2_RAW},
92 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
93 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
94 {V4L2_PIX_FMT_BGR666, TRUE, GST_V4L2_RAW},
95 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
96 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
97 {V4L2_PIX_FMT_ABGR32, TRUE, GST_V4L2_RAW},
98 {V4L2_PIX_FMT_XBGR32, TRUE, GST_V4L2_RAW},
99 {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
100 {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},
102 /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
103 {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
104 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
105 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
106 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
107 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
110 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
111 {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
112 {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
113 {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
114 {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
115 {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
116 {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
117 {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},
119 /* Palette formats */
120 {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},
122 /* Chrominance formats */
123 {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},
125 /* Luminance+Chrominance formats */
126 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
127 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
128 {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
129 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
130 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
131 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
132 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
133 {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
134 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
135 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
136 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
137 {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
138 {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
139 {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
140 {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
141 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
142 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
143 {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
144 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
145 {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
146 {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
148 /* two planes -- one Y, one Cr + Cb interleaved */
149 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
150 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
151 {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
152 {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
153 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
154 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
155 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
156 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
158 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
159 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
160 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
162 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
163 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
164 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_CODEC},
165 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_CODEC},
166 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_CODEC},
168 /* compressed formats */
169 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
170 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
171 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
172 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
173 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
174 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
175 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
176 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
177 {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
178 {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
179 {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
180 {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
181 {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
182 {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
183 {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
184 {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
186 /* Vendor-specific formats */
187 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
188 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
189 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
190 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
193 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
195 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
198 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
200 gst_v4l2_device_get_type (void)
202 static GType v4l2_device_type = 0;
204 if (v4l2_device_type == 0) {
205 static const GFlagsValue values[] = {
206 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
207 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
208 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
210 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
211 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
213 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
214 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
220 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
223 return v4l2_device_type;
226 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
228 gst_v4l2_tv_norm_get_type (void)
230 static GType v4l2_tv_norm = 0;
233 static const GEnumValue tv_norms[] = {
236 {V4L2_STD_NTSC, "NTSC", "NTSC"},
237 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
238 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
239 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
240 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
242 {V4L2_STD_PAL, "PAL", "PAL"},
243 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
244 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
245 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
246 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
247 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
248 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
249 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
250 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
251 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
252 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
253 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
254 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
255 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
256 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
258 {V4L2_STD_SECAM, "SECAM", "SECAM"},
259 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
260 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
261 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
262 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
263 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
264 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
265 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
266 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
267 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
272 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
279 gst_v4l2_io_mode_get_type (void)
281 static GType v4l2_io_mode = 0;
284 static const GEnumValue io_modes[] = {
285 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
286 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
287 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
288 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
289 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
290 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
295 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
301 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
302 const char *default_device)
304 g_object_class_install_property (gobject_class, PROP_DEVICE,
305 g_param_spec_string ("device", "Device", "Device location",
306 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
308 g_param_spec_string ("device-name", "Device name",
309 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
310 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
312 g_param_spec_int ("device-fd", "File descriptor",
313 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
314 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
315 g_object_class_install_property (gobject_class, PROP_FLAGS,
316 g_param_spec_flags ("flags", "Flags", "Device type flags",
317 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
318 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
321 * GstV4l2Src:brightness:
323 * Picture brightness, or more precisely, the black level
325 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
326 g_param_spec_int ("brightness", "Brightness",
327 "Picture brightness, or more precisely, the black level", G_MININT,
329 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
331 * GstV4l2Src:contrast:
333 * Picture contrast or luma gain
335 g_object_class_install_property (gobject_class, PROP_CONTRAST,
336 g_param_spec_int ("contrast", "Contrast",
337 "Picture contrast or luma gain", G_MININT,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
341 * GstV4l2Src:saturation:
343 * Picture color saturation or chroma gain
345 g_object_class_install_property (gobject_class, PROP_SATURATION,
346 g_param_spec_int ("saturation", "Saturation",
347 "Picture color saturation or chroma gain", G_MININT,
349 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
353 * Hue or color balance
355 g_object_class_install_property (gobject_class, PROP_HUE,
356 g_param_spec_int ("hue", "Hue",
357 "Hue or color balance", G_MININT,
359 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
366 g_object_class_install_property (gobject_class, PROP_TV_NORM,
367 g_param_spec_enum ("norm", "TV norm",
369 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
370 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 * GstV4l2Src:io-mode:
377 g_object_class_install_property (gobject_class, PROP_IO_MODE,
378 g_param_spec_enum ("io-mode", "IO mode",
380 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384 * GstV4l2Src:extra-controls:
386 * Additional v4l2 controls for the device. The controls are identified
387 * by the control name (lowercase with '_' for any non-alphanumeric
392 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
393 g_param_spec_boxed ("extra-controls", "Extra Controls",
394 "Extra v4l2 controls (CIDs) for the device",
395 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 * GstV4l2Src:pixel-aspect-ratio:
400 * The pixel aspect ratio of the device. This overwrites the pixel aspect
401 * ratio queried from the device.
405 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
406 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
407 "Overwrite the pixel aspect ratio of the device", "1/1",
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 * GstV4l2Src:force-aspect-ratio:
413 * When enabled, the pixel aspect ratio queried from the device or set
414 * with the pixel-aspect-ratio property will be enforced.
418 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
419 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
420 "When enabled, the pixel aspect ratio will be enforced", TRUE,
421 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
428 g_object_class_install_property (gobject_class, PROP_DEVICE,
429 g_param_spec_string ("device", "Device", "Device location",
430 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
432 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
433 g_param_spec_string ("device-name", "Device name",
434 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
435 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
438 g_param_spec_int ("device-fd", "File descriptor",
439 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
440 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
442 g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
443 g_param_spec_enum ("output-io-mode", "Output IO mode",
444 "Output side I/O mode (matches sink pad)",
445 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
446 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
449 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
450 "Capture I/O mode (matches src pad)",
451 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
452 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
454 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
455 g_param_spec_boxed ("extra-controls", "Extra Controls",
456 "Extra v4l2 controls (CIDs) for the device",
457 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 gst_v4l2_object_new (GstElement * element,
462 enum v4l2_buf_type type,
463 const char *default_device,
464 GstV4l2GetInOutFunction get_in_out_func,
465 GstV4l2SetInOutFunction set_in_out_func,
466 GstV4l2UpdateFpsFunction update_fps_func)
468 GstV4l2Object *v4l2object;
471 * some default values
473 v4l2object = g_new0 (GstV4l2Object, 1);
475 v4l2object->type = type;
476 v4l2object->formats = NULL;
478 v4l2object->element = element;
479 v4l2object->get_in_out_func = get_in_out_func;
480 v4l2object->set_in_out_func = set_in_out_func;
481 v4l2object->update_fps_func = update_fps_func;
483 v4l2object->video_fd = -1;
484 v4l2object->active = FALSE;
485 v4l2object->videodev = g_strdup (default_device);
487 v4l2object->norms = NULL;
488 v4l2object->channels = NULL;
489 v4l2object->colors = NULL;
491 v4l2object->xwindow_id = 0;
493 v4l2object->keep_aspect = TRUE;
495 v4l2object->n_v4l2_planes = 0;
497 v4l2object->no_initial_format = FALSE;
502 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
506 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
508 g_return_if_fail (v4l2object != NULL);
510 g_free (v4l2object->videodev);
512 g_free (v4l2object->channel);
514 if (v4l2object->formats) {
515 gst_v4l2_object_clear_format_list (v4l2object);
518 if (v4l2object->probed_caps) {
519 gst_caps_unref (v4l2object->probed_caps);
527 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
529 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
530 g_slist_free (v4l2object->formats);
531 v4l2object->formats = NULL;
537 gst_v4l2_object_prop_to_cid (guint prop_id)
542 case PROP_BRIGHTNESS:
543 cid = V4L2_CID_BRIGHTNESS;
546 cid = V4L2_CID_CONTRAST;
548 case PROP_SATURATION:
549 cid = V4L2_CID_SATURATION;
555 GST_WARNING ("unmapped property id: %d", prop_id);
562 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
563 guint prop_id, const GValue * value, GParamSpec * pspec)
567 g_free (v4l2object->videodev);
568 v4l2object->videodev = g_value_dup_string (value);
570 case PROP_BRIGHTNESS:
572 case PROP_SATURATION:
575 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
578 if (GST_V4L2_IS_OPEN (v4l2object)) {
579 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
586 v4l2object->tv_norm = g_value_get_enum (value);
590 if (GST_V4L2_IS_OPEN (v4l2object)) {
591 GstTuner *tuner = GST_TUNER (v4l2object->element);
592 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
593 (gchar *) g_value_get_string (value));
596 /* like gst_tuner_set_channel (tuner, channel)
597 without g_object_notify */
598 gst_v4l2_tuner_set_channel (v4l2object, channel);
601 g_free (v4l2object->channel);
602 v4l2object->channel = g_value_dup_string (value);
606 if (GST_V4L2_IS_OPEN (v4l2object)) {
607 GstTuner *tuner = GST_TUNER (v4l2object->element);
608 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
611 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
613 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
614 without g_object_notify */
615 gst_v4l2_tuner_set_frequency (v4l2object, channel,
616 g_value_get_ulong (value));
619 v4l2object->frequency = g_value_get_ulong (value);
625 v4l2object->req_mode = g_value_get_enum (value);
627 case PROP_CAPTURE_IO_MODE:
628 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
629 v4l2object->req_mode = g_value_get_enum (value);
631 case PROP_OUTPUT_IO_MODE:
632 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
633 v4l2object->req_mode = g_value_get_enum (value);
635 case PROP_EXTRA_CONTROLS:{
636 const GstStructure *s = gst_value_get_structure (value);
638 if (v4l2object->extra_controls)
639 gst_structure_free (v4l2object->extra_controls);
641 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
642 if (GST_V4L2_IS_OPEN (v4l2object))
643 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
646 case PROP_PIXEL_ASPECT_RATIO:
647 g_free (v4l2object->par);
648 v4l2object->par = g_new0 (GValue, 1);
649 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
650 if (!g_value_transform (value, v4l2object->par)) {
651 g_warning ("Could not transform string to aspect ratio");
652 gst_value_set_fraction (v4l2object->par, 1, 1);
654 GST_DEBUG_OBJECT (v4l2object->element, "set PAR to %d/%d",
655 gst_value_get_fraction_numerator (v4l2object->par),
656 gst_value_get_fraction_denominator (v4l2object->par));
658 case PROP_FORCE_ASPECT_RATIO:
659 v4l2object->keep_aspect = g_value_get_boolean (value);
670 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
671 guint prop_id, GValue * value, GParamSpec * pspec)
675 g_value_set_string (value, v4l2object->videodev);
677 case PROP_DEVICE_NAME:
679 const guchar *new = NULL;
681 if (GST_V4L2_IS_OPEN (v4l2object)) {
682 new = v4l2object->vcap.card;
683 } else if (gst_v4l2_open (v4l2object)) {
684 new = v4l2object->vcap.card;
685 gst_v4l2_close (v4l2object);
687 g_value_set_string (value, (gchar *) new);
692 if (GST_V4L2_IS_OPEN (v4l2object))
693 g_value_set_int (value, v4l2object->video_fd);
695 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
702 if (GST_V4L2_IS_OPEN (v4l2object)) {
703 flags |= v4l2object->device_caps &
704 (V4L2_CAP_VIDEO_CAPTURE |
705 V4L2_CAP_VIDEO_OUTPUT |
706 V4L2_CAP_VIDEO_OVERLAY |
707 V4L2_CAP_VBI_CAPTURE |
708 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
710 if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
711 flags |= V4L2_CAP_VIDEO_CAPTURE;
713 if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
714 flags |= V4L2_CAP_VIDEO_OUTPUT;
716 g_value_set_flags (value, flags);
719 case PROP_BRIGHTNESS:
721 case PROP_SATURATION:
724 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
727 if (GST_V4L2_IS_OPEN (v4l2object)) {
729 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
730 g_value_set_int (value, v);
738 g_value_set_enum (value, v4l2object->tv_norm);
741 g_value_set_enum (value, v4l2object->req_mode);
743 case PROP_CAPTURE_IO_MODE:
744 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
745 g_value_set_enum (value, v4l2object->req_mode);
747 case PROP_OUTPUT_IO_MODE:
748 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
749 g_value_set_enum (value, v4l2object->req_mode);
751 case PROP_EXTRA_CONTROLS:
752 gst_value_set_structure (value, v4l2object->extra_controls);
754 case PROP_PIXEL_ASPECT_RATIO:
756 g_value_transform (v4l2object->par, value);
758 case PROP_FORCE_ASPECT_RATIO:
759 g_value_set_boolean (value, v4l2object->keep_aspect);
769 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
771 struct v4l2_control control = { 0, };
773 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
775 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
776 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
778 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
780 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
781 GST_DEBUG_OBJECT (v4l2object->element,
782 "driver requires a minimum of %d buffers", control.value);
783 v4l2object->min_buffers = control.value;
785 v4l2object->min_buffers = 0;
790 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
792 GstTunerNorm *norm = NULL;
793 GstTunerChannel *channel = NULL;
796 if (!GST_IS_TUNER (v4l2object->element))
799 tuner = GST_TUNER (v4l2object->element);
801 if (v4l2object->tv_norm)
802 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
803 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
804 "norm=%p", (guint64) v4l2object->tv_norm, norm);
806 gst_tuner_set_norm (tuner, norm);
809 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
811 v4l2object->tv_norm =
812 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
813 gst_tuner_norm_changed (tuner, norm);
817 if (v4l2object->channel)
818 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
820 gst_tuner_set_channel (tuner, channel);
823 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
824 (v4l2object->element)));
826 g_free (v4l2object->channel);
827 v4l2object->channel = g_strdup (channel->label);
828 gst_tuner_channel_changed (tuner, channel);
833 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
834 if (v4l2object->frequency != 0) {
835 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
837 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
838 if (v4l2object->frequency == 0) {
840 gst_tuner_set_frequency (tuner, channel, 1000);
848 gst_v4l2_object_open (GstV4l2Object * v4l2object)
850 if (gst_v4l2_open (v4l2object))
851 gst_v4l2_set_defaults (v4l2object);
859 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
863 ret = gst_v4l2_dup (v4l2object, other);
869 gst_v4l2_object_close (GstV4l2Object * v4l2object)
871 if (!gst_v4l2_close (v4l2object))
874 gst_caps_replace (&v4l2object->probed_caps, NULL);
876 /* reset our copy of the device caps */
877 v4l2object->device_caps = 0;
879 if (v4l2object->formats) {
880 gst_v4l2_object_clear_format_list (v4l2object);
886 static struct v4l2_fmtdesc *
887 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
890 struct v4l2_fmtdesc *fmt;
896 walk = gst_v4l2_object_get_format_list (v4l2object);
898 fmt = (struct v4l2_fmtdesc *) walk->data;
899 if (fmt->pixelformat == fourcc)
901 /* special case for jpeg */
902 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
903 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
904 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
905 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
906 fourcc == V4L2_PIX_FMT_PJPG) {
910 walk = g_slist_next (walk);
918 /* complete made up ranking, the values themselves are meaningless */
919 /* These ranks MUST be X such that X<<15 fits on a signed int - see
920 the comment at the end of gst_v4l2_object_format_get_rank. */
921 #define YUV_BASE_RANK 1000
922 #define JPEG_BASE_RANK 500
923 #define DV_BASE_RANK 200
924 #define RGB_BASE_RANK 100
925 #define YUV_ODD_BASE_RANK 50
926 #define RGB_ODD_BASE_RANK 25
927 #define BAYER_BASE_RANK 15
928 #define S910_BASE_RANK 10
929 #define GREY_BASE_RANK 5
930 #define PWC_BASE_RANK 1
933 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
935 guint32 fourcc = fmt->pixelformat;
936 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
940 case V4L2_PIX_FMT_MJPEG:
941 case V4L2_PIX_FMT_PJPG:
942 rank = JPEG_BASE_RANK;
944 case V4L2_PIX_FMT_JPEG:
945 rank = JPEG_BASE_RANK + 1;
947 case V4L2_PIX_FMT_MPEG: /* MPEG */
948 rank = JPEG_BASE_RANK + 2;
951 case V4L2_PIX_FMT_RGB332:
952 case V4L2_PIX_FMT_ARGB555:
953 case V4L2_PIX_FMT_XRGB555:
954 case V4L2_PIX_FMT_RGB555:
955 case V4L2_PIX_FMT_ARGB555X:
956 case V4L2_PIX_FMT_XRGB555X:
957 case V4L2_PIX_FMT_RGB555X:
958 case V4L2_PIX_FMT_BGR666:
959 case V4L2_PIX_FMT_RGB565:
960 case V4L2_PIX_FMT_RGB565X:
961 case V4L2_PIX_FMT_RGB444:
962 case V4L2_PIX_FMT_Y4:
963 case V4L2_PIX_FMT_Y6:
964 case V4L2_PIX_FMT_Y10:
965 case V4L2_PIX_FMT_Y12:
966 case V4L2_PIX_FMT_Y10BPACK:
967 case V4L2_PIX_FMT_YUV555:
968 case V4L2_PIX_FMT_YUV565:
969 case V4L2_PIX_FMT_YUV32:
970 case V4L2_PIX_FMT_NV12MT_16X16:
971 case V4L2_PIX_FMT_NV42:
972 case V4L2_PIX_FMT_H264_MVC:
973 rank = RGB_ODD_BASE_RANK;
976 case V4L2_PIX_FMT_RGB24:
977 case V4L2_PIX_FMT_BGR24:
978 rank = RGB_BASE_RANK - 1;
981 case V4L2_PIX_FMT_RGB32:
982 case V4L2_PIX_FMT_BGR32:
983 case V4L2_PIX_FMT_ABGR32:
984 case V4L2_PIX_FMT_XBGR32:
985 case V4L2_PIX_FMT_ARGB32:
986 case V4L2_PIX_FMT_XRGB32:
987 rank = RGB_BASE_RANK;
990 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
991 rank = GREY_BASE_RANK;
994 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
995 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
996 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
997 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
998 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
999 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1000 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1001 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1002 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1003 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1004 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1005 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1006 rank = YUV_ODD_BASE_RANK;
1009 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1010 rank = YUV_BASE_RANK + 3;
1012 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1013 rank = YUV_BASE_RANK + 2;
1015 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1016 case V4L2_PIX_FMT_YUV420M:
1017 rank = YUV_BASE_RANK + 7;
1019 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1020 rank = YUV_BASE_RANK + 10;
1022 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1023 rank = YUV_BASE_RANK + 6;
1025 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1026 rank = YUV_BASE_RANK + 9;
1028 case V4L2_PIX_FMT_YUV444:
1029 rank = YUV_BASE_RANK + 6;
1031 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1032 rank = YUV_BASE_RANK + 5;
1034 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1035 rank = YUV_BASE_RANK + 4;
1037 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1038 rank = YUV_BASE_RANK + 8;
1041 case V4L2_PIX_FMT_DV:
1042 rank = DV_BASE_RANK;
1045 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1049 case V4L2_PIX_FMT_SBGGR8:
1050 case V4L2_PIX_FMT_SGBRG8:
1051 case V4L2_PIX_FMT_SGRBG8:
1052 case V4L2_PIX_FMT_SRGGB8:
1053 rank = BAYER_BASE_RANK;
1056 case V4L2_PIX_FMT_SN9C10X:
1057 rank = S910_BASE_RANK;
1060 case V4L2_PIX_FMT_PWC1:
1061 rank = PWC_BASE_RANK;
1063 case V4L2_PIX_FMT_PWC2:
1064 rank = PWC_BASE_RANK;
1072 /* All ranks are below 1<<15 so a shift by 15
1073 * will a) make all non-emulated formats larger
1074 * than emulated and b) will not overflow
1085 format_cmp_func (gconstpointer a, gconstpointer b)
1087 const struct v4l2_fmtdesc *fa = a;
1088 const struct v4l2_fmtdesc *fb = b;
1090 if (fa->pixelformat == fb->pixelformat)
1093 return gst_v4l2_object_format_get_rank (fb) -
1094 gst_v4l2_object_format_get_rank (fa);
1097 /******************************************************
1098 * gst_v4l2_object_fill_format_list():
1099 * create list of supported capture formats
1100 * return value: TRUE on success, FALSE on error
1101 ******************************************************/
1103 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1104 enum v4l2_buf_type type)
1107 struct v4l2_fmtdesc *format;
1109 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1111 /* format enumeration */
1113 format = g_new0 (struct v4l2_fmtdesc, 1);
1116 format->type = type;
1118 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1119 if (errno == EINVAL) {
1121 break; /* end of enumeration */
1127 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1128 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1129 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1130 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1131 format->description);
1132 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1133 GST_FOURCC_ARGS (format->pixelformat));
1135 /* sort formats according to our preference; we do this, because caps
1136 * are probed in the order the formats are in the list, and the order of
1137 * formats in the final probed caps matters for things like fixation */
1138 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1139 (GCompareFunc) format_cmp_func);
1142 #ifndef GST_DISABLE_GST_DEBUG
1146 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1147 for (l = v4l2object->formats; l != NULL; l = l->next) {
1150 GST_INFO_OBJECT (v4l2object->element,
1151 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1152 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1162 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1163 (_("Failed to enumerate possible video formats device '%s' can work "
1164 "with"), v4l2object->videodev),
1165 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1166 n, v4l2object->videodev, errno, g_strerror (errno)));
1173 * Get the list of supported capture formats, a list of
1174 * <code>struct v4l2_fmtdesc</code>.
1177 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1179 if (!v4l2object->formats) {
1181 /* check usual way */
1182 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1184 /* if our driver supports multi-planar
1185 * and if formats are still empty then we can workaround driver bug
1186 * by also looking up formats as if our device was not supporting
1188 if (!v4l2object->formats) {
1189 switch (v4l2object->type) {
1190 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1191 gst_v4l2_object_fill_format_list (v4l2object,
1192 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1195 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1196 gst_v4l2_object_fill_format_list (v4l2object,
1197 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1205 return v4l2object->formats;
1208 static GstVideoFormat
1209 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1211 GstVideoFormat format;
1214 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1215 format = GST_VIDEO_FORMAT_GRAY8;
1217 case V4L2_PIX_FMT_Y16:
1218 format = GST_VIDEO_FORMAT_GRAY16_LE;
1220 case V4L2_PIX_FMT_Y16_BE:
1221 format = GST_VIDEO_FORMAT_GRAY16_BE;
1223 case V4L2_PIX_FMT_XRGB555:
1224 case V4L2_PIX_FMT_RGB555:
1225 format = GST_VIDEO_FORMAT_RGB15;
1227 case V4L2_PIX_FMT_XRGB555X:
1228 case V4L2_PIX_FMT_RGB555X:
1229 format = GST_VIDEO_FORMAT_BGR15;
1231 case V4L2_PIX_FMT_RGB565:
1232 format = GST_VIDEO_FORMAT_RGB16;
1234 case V4L2_PIX_FMT_RGB24:
1235 format = GST_VIDEO_FORMAT_RGB;
1237 case V4L2_PIX_FMT_BGR24:
1238 format = GST_VIDEO_FORMAT_BGR;
1240 case V4L2_PIX_FMT_XRGB32:
1241 case V4L2_PIX_FMT_RGB32:
1242 format = GST_VIDEO_FORMAT_xRGB;
1244 case V4L2_PIX_FMT_XBGR32:
1245 case V4L2_PIX_FMT_BGR32:
1246 format = GST_VIDEO_FORMAT_BGRx;
1248 case V4L2_PIX_FMT_ABGR32:
1249 format = GST_VIDEO_FORMAT_BGRA;
1251 case V4L2_PIX_FMT_ARGB32:
1252 format = GST_VIDEO_FORMAT_ARGB;
1254 case V4L2_PIX_FMT_NV12:
1255 case V4L2_PIX_FMT_NV12M:
1256 format = GST_VIDEO_FORMAT_NV12;
1258 case V4L2_PIX_FMT_NV12MT:
1259 format = GST_VIDEO_FORMAT_NV12_64Z32;
1261 case V4L2_PIX_FMT_NV21:
1262 case V4L2_PIX_FMT_NV21M:
1263 format = GST_VIDEO_FORMAT_NV21;
1265 case V4L2_PIX_FMT_YVU410:
1266 format = GST_VIDEO_FORMAT_YVU9;
1268 case V4L2_PIX_FMT_YUV410:
1269 format = GST_VIDEO_FORMAT_YUV9;
1271 case V4L2_PIX_FMT_YUV420:
1272 case V4L2_PIX_FMT_YUV420M:
1273 format = GST_VIDEO_FORMAT_I420;
1275 case V4L2_PIX_FMT_YUYV:
1276 format = GST_VIDEO_FORMAT_YUY2;
1278 case V4L2_PIX_FMT_YVU420:
1279 format = GST_VIDEO_FORMAT_YV12;
1281 case V4L2_PIX_FMT_UYVY:
1282 format = GST_VIDEO_FORMAT_UYVY;
1284 case V4L2_PIX_FMT_YUV411P:
1285 format = GST_VIDEO_FORMAT_Y41B;
1287 case V4L2_PIX_FMT_YUV422P:
1288 format = GST_VIDEO_FORMAT_Y42B;
1290 case V4L2_PIX_FMT_YVYU:
1291 format = GST_VIDEO_FORMAT_YVYU;
1293 case V4L2_PIX_FMT_NV16:
1294 case V4L2_PIX_FMT_NV16M:
1295 format = GST_VIDEO_FORMAT_NV16;
1297 case V4L2_PIX_FMT_NV61:
1298 case V4L2_PIX_FMT_NV61M:
1299 format = GST_VIDEO_FORMAT_NV61;
1301 case V4L2_PIX_FMT_NV24:
1302 format = GST_VIDEO_FORMAT_NV24;
1305 format = GST_VIDEO_FORMAT_UNKNOWN;
1313 gst_v4l2_object_v4l2fourcc_is_rgb (guint32 fourcc)
1315 gboolean ret = FALSE;
1318 case V4L2_PIX_FMT_XRGB555:
1319 case V4L2_PIX_FMT_RGB555:
1320 case V4L2_PIX_FMT_XRGB555X:
1321 case V4L2_PIX_FMT_RGB555X:
1322 case V4L2_PIX_FMT_RGB565:
1323 case V4L2_PIX_FMT_RGB24:
1324 case V4L2_PIX_FMT_BGR24:
1325 case V4L2_PIX_FMT_XRGB32:
1326 case V4L2_PIX_FMT_RGB32:
1327 case V4L2_PIX_FMT_XBGR32:
1328 case V4L2_PIX_FMT_BGR32:
1329 case V4L2_PIX_FMT_ABGR32:
1330 case V4L2_PIX_FMT_ARGB32:
1340 static GstStructure *
1341 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1343 GstStructure *structure = NULL;
1346 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1347 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1348 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1349 structure = gst_structure_new_empty ("image/jpeg");
1351 case V4L2_PIX_FMT_MPEG1:
1352 structure = gst_structure_new ("video/mpeg",
1353 "mpegversion", G_TYPE_INT, 2, NULL);
1355 case V4L2_PIX_FMT_MPEG2:
1356 structure = gst_structure_new ("video/mpeg",
1357 "mpegversion", G_TYPE_INT, 2, NULL);
1359 case V4L2_PIX_FMT_MPEG4:
1360 case V4L2_PIX_FMT_XVID:
1361 structure = gst_structure_new ("video/mpeg",
1362 "mpegversion", G_TYPE_INT, 4, "systemstream",
1363 G_TYPE_BOOLEAN, FALSE, NULL);
1365 case V4L2_PIX_FMT_H263:
1366 structure = gst_structure_new ("video/x-h263",
1367 "variant", G_TYPE_STRING, "itu", NULL);
1369 case V4L2_PIX_FMT_H264: /* H.264 */
1370 structure = gst_structure_new ("video/x-h264",
1371 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1372 G_TYPE_STRING, "au", NULL);
1374 case V4L2_PIX_FMT_H264_NO_SC:
1375 structure = gst_structure_new ("video/x-h264",
1376 "stream-format", G_TYPE_STRING, "avc", "alignment",
1377 G_TYPE_STRING, "au", NULL);
1379 case V4L2_PIX_FMT_VC1_ANNEX_G:
1380 case V4L2_PIX_FMT_VC1_ANNEX_L:
1381 structure = gst_structure_new ("video/x-wmv",
1382 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1384 case V4L2_PIX_FMT_VP8:
1385 structure = gst_structure_new_empty ("video/x-vp8");
1387 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1388 case V4L2_PIX_FMT_Y16:
1389 case V4L2_PIX_FMT_Y16_BE:
1390 case V4L2_PIX_FMT_XRGB555:
1391 case V4L2_PIX_FMT_RGB555:
1392 case V4L2_PIX_FMT_XRGB555X:
1393 case V4L2_PIX_FMT_RGB555X:
1394 case V4L2_PIX_FMT_RGB565:
1395 case V4L2_PIX_FMT_RGB24:
1396 case V4L2_PIX_FMT_BGR24:
1397 case V4L2_PIX_FMT_RGB32:
1398 case V4L2_PIX_FMT_XRGB32:
1399 case V4L2_PIX_FMT_ARGB32:
1400 case V4L2_PIX_FMT_BGR32:
1401 case V4L2_PIX_FMT_XBGR32:
1402 case V4L2_PIX_FMT_ABGR32:
1403 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1404 case V4L2_PIX_FMT_NV12M:
1405 case V4L2_PIX_FMT_NV12MT:
1406 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1407 case V4L2_PIX_FMT_NV21M:
1408 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1409 case V4L2_PIX_FMT_NV16M:
1410 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1411 case V4L2_PIX_FMT_NV61M:
1412 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1413 case V4L2_PIX_FMT_YVU410:
1414 case V4L2_PIX_FMT_YUV410:
1415 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1416 case V4L2_PIX_FMT_YUV420M:
1417 case V4L2_PIX_FMT_YUYV:
1418 case V4L2_PIX_FMT_YVU420:
1419 case V4L2_PIX_FMT_UYVY:
1420 case V4L2_PIX_FMT_YUV422P:
1421 case V4L2_PIX_FMT_YVYU:
1422 case V4L2_PIX_FMT_YUV411P:{
1423 GstVideoFormat format;
1424 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1425 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1426 structure = gst_structure_new ("video/x-raw",
1427 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1430 case V4L2_PIX_FMT_DV:
1432 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1435 case V4L2_PIX_FMT_MPEG: /* MPEG */
1436 structure = gst_structure_new ("video/mpegts",
1437 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1439 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1441 case V4L2_PIX_FMT_SBGGR8:
1442 case V4L2_PIX_FMT_SGBRG8:
1443 case V4L2_PIX_FMT_SGRBG8:
1444 case V4L2_PIX_FMT_SRGGB8:
1445 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1446 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1447 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1448 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1449 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1451 case V4L2_PIX_FMT_SN9C10X:
1452 structure = gst_structure_new_empty ("video/x-sonix");
1454 case V4L2_PIX_FMT_PWC1:
1455 structure = gst_structure_new_empty ("video/x-pwc1");
1457 case V4L2_PIX_FMT_PWC2:
1458 structure = gst_structure_new_empty ("video/x-pwc2");
1460 case V4L2_PIX_FMT_RGB332:
1461 case V4L2_PIX_FMT_BGR666:
1462 case V4L2_PIX_FMT_ARGB555X:
1463 case V4L2_PIX_FMT_RGB565X:
1464 case V4L2_PIX_FMT_RGB444:
1465 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1466 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1467 case V4L2_PIX_FMT_Y4:
1468 case V4L2_PIX_FMT_Y6:
1469 case V4L2_PIX_FMT_Y10:
1470 case V4L2_PIX_FMT_Y12:
1471 case V4L2_PIX_FMT_Y10BPACK:
1472 case V4L2_PIX_FMT_YUV444:
1473 case V4L2_PIX_FMT_YUV555:
1474 case V4L2_PIX_FMT_YUV565:
1475 case V4L2_PIX_FMT_Y41P:
1476 case V4L2_PIX_FMT_YUV32:
1477 case V4L2_PIX_FMT_NV12MT_16X16:
1478 case V4L2_PIX_FMT_NV42:
1479 case V4L2_PIX_FMT_H264_MVC:
1481 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1482 fourcc, GST_FOURCC_ARGS (fourcc));
1490 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1492 GstStructure *template;
1495 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1497 if (template == NULL)
1500 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1501 if (gst_v4l2_formats[i].format != fourcc)
1504 if (gst_v4l2_formats[i].dimensions) {
1505 gst_structure_set (template,
1506 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1507 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1508 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1519 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1521 GstStructure *structure;
1525 caps = gst_caps_new_empty ();
1526 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1528 if ((gst_v4l2_formats[i].flags & flags) == 0)
1532 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1535 GstStructure *alt_s = NULL;
1537 if (gst_v4l2_formats[i].dimensions) {
1538 gst_structure_set (structure,
1539 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1540 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1541 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1544 switch (gst_v4l2_formats[i].format) {
1545 case V4L2_PIX_FMT_RGB32:
1546 alt_s = gst_structure_copy (structure);
1547 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1549 case V4L2_PIX_FMT_BGR32:
1550 alt_s = gst_structure_copy (structure);
1551 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1556 gst_caps_append_structure (caps, structure);
1559 gst_caps_append_structure (caps, alt_s);
1563 return gst_caps_simplify (caps);
1567 gst_v4l2_object_get_all_caps (void)
1569 static GstCaps *caps = NULL;
1572 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1574 return gst_caps_ref (caps);
1578 gst_v4l2_object_get_raw_caps (void)
1580 static GstCaps *caps = NULL;
1583 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1585 return gst_caps_ref (caps);
1589 gst_v4l2_object_get_codec_caps (void)
1591 static GstCaps *caps = NULL;
1594 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1596 return gst_caps_ref (caps);
1599 /* collect data for the given caps
1600 * @caps: given input caps
1601 * @format: location for the v4l format
1602 * @w/@h: location for width and height
1603 * @fps_n/@fps_d: location for framerate
1604 * @size: location for expected size of the frame or 0 if unknown
1607 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1608 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1610 GstStructure *structure;
1611 guint32 fourcc = 0, fourcc_nc = 0;
1612 const gchar *mimetype;
1613 struct v4l2_fmtdesc *fmt = NULL;
1615 structure = gst_caps_get_structure (caps, 0);
1617 mimetype = gst_structure_get_name (structure);
1619 if (!gst_video_info_from_caps (info, caps))
1620 goto invalid_format;
1622 if (g_str_equal (mimetype, "video/x-raw")) {
1623 switch (GST_VIDEO_INFO_FORMAT (info)) {
1624 case GST_VIDEO_FORMAT_I420:
1625 fourcc = V4L2_PIX_FMT_YUV420;
1626 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1628 case GST_VIDEO_FORMAT_YUY2:
1629 fourcc = V4L2_PIX_FMT_YUYV;
1631 case GST_VIDEO_FORMAT_UYVY:
1632 fourcc = V4L2_PIX_FMT_UYVY;
1634 case GST_VIDEO_FORMAT_YV12:
1635 fourcc = V4L2_PIX_FMT_YVU420;
1637 case GST_VIDEO_FORMAT_Y41B:
1638 fourcc = V4L2_PIX_FMT_YUV411P;
1640 case GST_VIDEO_FORMAT_Y42B:
1641 fourcc = V4L2_PIX_FMT_YUV422P;
1643 case GST_VIDEO_FORMAT_NV12:
1644 fourcc = V4L2_PIX_FMT_NV12;
1645 fourcc_nc = V4L2_PIX_FMT_NV12M;
1647 case GST_VIDEO_FORMAT_NV12_64Z32:
1648 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1650 case GST_VIDEO_FORMAT_NV21:
1651 fourcc = V4L2_PIX_FMT_NV21;
1652 fourcc_nc = V4L2_PIX_FMT_NV21M;
1654 case GST_VIDEO_FORMAT_NV16:
1655 fourcc = V4L2_PIX_FMT_NV16;
1656 fourcc_nc = V4L2_PIX_FMT_NV16M;
1658 case GST_VIDEO_FORMAT_NV61:
1659 fourcc = V4L2_PIX_FMT_NV61;
1660 fourcc_nc = V4L2_PIX_FMT_NV61M;
1662 case GST_VIDEO_FORMAT_NV24:
1663 fourcc = V4L2_PIX_FMT_NV24;
1665 case GST_VIDEO_FORMAT_YVYU:
1666 fourcc = V4L2_PIX_FMT_YVYU;
1668 case GST_VIDEO_FORMAT_RGB15:
1669 fourcc = V4L2_PIX_FMT_RGB555;
1670 fourcc_nc = V4L2_PIX_FMT_XRGB555;
1672 case GST_VIDEO_FORMAT_RGB16:
1673 fourcc = V4L2_PIX_FMT_RGB565;
1675 case GST_VIDEO_FORMAT_RGB:
1676 fourcc = V4L2_PIX_FMT_RGB24;
1678 case GST_VIDEO_FORMAT_BGR:
1679 fourcc = V4L2_PIX_FMT_BGR24;
1681 case GST_VIDEO_FORMAT_xRGB:
1682 fourcc = V4L2_PIX_FMT_RGB32;
1683 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1685 case GST_VIDEO_FORMAT_ARGB:
1686 fourcc = V4L2_PIX_FMT_RGB32;
1687 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1689 case GST_VIDEO_FORMAT_BGRx:
1690 fourcc = V4L2_PIX_FMT_BGR32;
1691 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1693 case GST_VIDEO_FORMAT_BGRA:
1694 fourcc = V4L2_PIX_FMT_BGR32;
1695 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1697 case GST_VIDEO_FORMAT_GRAY8:
1698 fourcc = V4L2_PIX_FMT_GREY;
1700 case GST_VIDEO_FORMAT_GRAY16_LE:
1701 fourcc = V4L2_PIX_FMT_Y16;
1703 case GST_VIDEO_FORMAT_GRAY16_BE:
1704 fourcc = V4L2_PIX_FMT_Y16_BE;
1710 if (g_str_equal (mimetype, "video/mpegts")) {
1711 fourcc = V4L2_PIX_FMT_MPEG;
1712 } else if (g_str_equal (mimetype, "video/x-dv")) {
1713 fourcc = V4L2_PIX_FMT_DV;
1714 } else if (g_str_equal (mimetype, "image/jpeg")) {
1715 fourcc = V4L2_PIX_FMT_JPEG;
1716 } else if (g_str_equal (mimetype, "video/mpeg")) {
1718 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1721 fourcc = V4L2_PIX_FMT_MPEG1;
1724 fourcc = V4L2_PIX_FMT_MPEG2;
1727 fourcc = V4L2_PIX_FMT_MPEG4;
1728 fourcc_nc = V4L2_PIX_FMT_XVID;
1734 } else if (g_str_equal (mimetype, "video/x-h263")) {
1735 fourcc = V4L2_PIX_FMT_H263;
1736 } else if (g_str_equal (mimetype, "video/x-h264")) {
1737 const gchar *stream_format =
1738 gst_structure_get_string (structure, "stream-format");
1739 if (g_str_equal (stream_format, "avc"))
1740 fourcc = V4L2_PIX_FMT_H264_NO_SC;
1742 fourcc = V4L2_PIX_FMT_H264;
1743 } else if (g_str_equal (mimetype, "video/x-vp8")) {
1744 fourcc = V4L2_PIX_FMT_VP8;
1745 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1746 const gchar *format = gst_structure_get_string (structure, "format");
1748 if (!g_ascii_strcasecmp (format, "bggr"))
1749 fourcc = V4L2_PIX_FMT_SBGGR8;
1750 else if (!g_ascii_strcasecmp (format, "gbrg"))
1751 fourcc = V4L2_PIX_FMT_SGBRG8;
1752 else if (!g_ascii_strcasecmp (format, "grbg"))
1753 fourcc = V4L2_PIX_FMT_SGRBG8;
1754 else if (!g_ascii_strcasecmp (format, "rggb"))
1755 fourcc = V4L2_PIX_FMT_SRGGB8;
1757 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1758 fourcc = V4L2_PIX_FMT_SN9C10X;
1759 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1760 fourcc = V4L2_PIX_FMT_PWC1;
1761 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1762 fourcc = V4L2_PIX_FMT_PWC2;
1767 /* Prefer the non-contiguous if supported */
1768 v4l2object->prefered_non_contiguous = TRUE;
1771 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
1772 else if (fourcc == 0)
1773 goto unhandled_format;
1776 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1777 v4l2object->prefered_non_contiguous = FALSE;
1781 goto unsupported_format;
1790 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1795 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1800 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1806 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1807 guint32 pixelformat, gint * width, gint * height);
1810 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1812 struct v4l2_cropcap cropcap;
1813 int num = 1, den = 1;
1815 if (!v4l2object->keep_aspect)
1818 if (v4l2object->par) {
1819 num = gst_value_get_fraction_numerator (v4l2object->par);
1820 den = gst_value_get_fraction_denominator (v4l2object->par);
1824 memset (&cropcap, 0, sizeof (cropcap));
1826 cropcap.type = v4l2object->type;
1827 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
1828 goto cropcap_failed;
1830 num = cropcap.pixelaspect.numerator;
1831 den = cropcap.pixelaspect.denominator;
1833 /* Ignore PAR that are 0/0 */
1838 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, num, den,
1843 if (errno != ENOTTY)
1844 GST_WARNING_OBJECT (v4l2object->element,
1845 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
1846 g_strerror (errno));
1850 /* returns TRUE if the value was changed in place, otherwise FALSE */
1852 gst_v4l2src_value_simplify (GValue * val)
1854 /* simplify list of one value to one value */
1855 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
1856 const GValue *list_val;
1857 GValue new_val = G_VALUE_INIT;
1859 list_val = gst_value_list_get_value (val, 0);
1860 g_value_init (&new_val, G_VALUE_TYPE (list_val));
1861 g_value_copy (list_val, &new_val);
1862 g_value_unset (val);
1871 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
1872 GstVideoInterlaceMode * interlace_mode)
1874 /* NB: If you add new return values, please fix mode_strings in
1875 * gst_v4l2_object_add_interlace_mode */
1877 case V4L2_FIELD_ANY:
1879 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
1881 case V4L2_FIELD_NONE:
1882 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1884 case V4L2_FIELD_INTERLACED:
1885 case V4L2_FIELD_INTERLACED_TB:
1886 case V4L2_FIELD_INTERLACED_BT:
1887 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1890 GST_ERROR ("Unknown enum v4l2_field %d", field);
1896 gst_v4l2_object_get_colorspace (struct v4l2_format *fmt,
1897 GstVideoColorimetry * cinfo)
1900 gst_v4l2_object_v4l2fourcc_is_rgb (fmt->fmt.pix.pixelformat);
1901 enum v4l2_colorspace colorspace;
1902 enum v4l2_quantization range;
1903 enum v4l2_ycbcr_encoding matrix;
1904 enum v4l2_xfer_func transfer;
1905 gboolean ret = TRUE;
1907 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
1908 colorspace = fmt->fmt.pix_mp.colorspace;
1909 range = fmt->fmt.pix_mp.quantization;
1910 matrix = fmt->fmt.pix_mp.ycbcr_enc;
1911 transfer = fmt->fmt.pix_mp.xfer_func;
1913 colorspace = fmt->fmt.pix.colorspace;
1914 range = fmt->fmt.pix.quantization;
1915 matrix = fmt->fmt.pix.ycbcr_enc;
1916 transfer = fmt->fmt.pix.xfer_func;
1919 /* First step, set the defaults for each primaries */
1920 switch (colorspace) {
1921 case V4L2_COLORSPACE_SMPTE170M:
1922 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1923 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1924 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1925 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1927 case V4L2_COLORSPACE_REC709:
1928 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1929 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1930 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1931 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1933 case V4L2_COLORSPACE_SRGB:
1934 case V4L2_COLORSPACE_JPEG:
1935 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1936 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1937 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
1938 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1940 case V4L2_COLORSPACE_ADOBERGB:
1941 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1942 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1943 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
1944 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
1946 case V4L2_COLORSPACE_BT2020:
1947 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1948 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1949 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1950 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
1952 case V4L2_COLORSPACE_SMPTE240M:
1953 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1954 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1955 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
1956 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
1958 case V4L2_COLORSPACE_470_SYSTEM_M:
1959 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1960 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1961 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1962 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
1964 case V4L2_COLORSPACE_470_SYSTEM_BG:
1965 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1966 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1967 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1968 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
1970 case V4L2_COLORSPACE_RAW:
1971 /* Explicitly unknown */
1972 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
1973 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
1974 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
1975 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
1978 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
1986 /* Second step, apply any custom variation */
1988 case V4L2_QUANTIZATION_FULL_RANGE:
1989 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1991 case V4L2_QUANTIZATION_LIM_RANGE:
1992 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1994 case V4L2_QUANTIZATION_DEFAULT:
1995 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
1996 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
1997 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1998 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601
1999 || matrix == V4L2_YCBCR_ENC_XV709
2000 || colorspace == V4L2_COLORSPACE_JPEG)
2001 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2003 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2006 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
2007 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2012 case V4L2_YCBCR_ENC_XV601:
2013 case V4L2_YCBCR_ENC_SYCC:
2014 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
2016 case V4L2_YCBCR_ENC_601:
2017 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2019 case V4L2_YCBCR_ENC_XV709:
2020 GST_FIXME ("XV709 not defined, assuming 709");
2022 case V4L2_YCBCR_ENC_709:
2023 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2025 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2026 GST_FIXME ("BT2020 with constant lumma is not defined, assuming BT2020");
2028 case V4L2_YCBCR_ENC_BT2020:
2029 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2031 case V4L2_YCBCR_ENC_SMPTE240M:
2032 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2034 case V4L2_YCBCR_ENC_DEFAULT:
2035 /* nothing, just use defaults for colorspace */
2038 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2039 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2043 /* Set identity matrix for R'G'B' formats to avoid creating
2044 * confusion. This though is cosmetic as it's now properly ignored by
2045 * the video info API and videoconvert. */
2047 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2050 case V4L2_XFER_FUNC_709:
2051 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2053 case V4L2_XFER_FUNC_SRGB:
2054 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2056 case V4L2_XFER_FUNC_ADOBERGB:
2057 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2059 case V4L2_XFER_FUNC_SMPTE240M:
2060 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2062 case V4L2_XFER_FUNC_NONE:
2063 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2065 case V4L2_XFER_FUNC_DEFAULT:
2066 /* nothing, just use defaults for colorspace */
2069 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2070 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2079 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2080 struct v4l2_format *try_fmt)
2082 int fd = v4l2object->video_fd;
2083 struct v4l2_format fmt;
2086 memcpy (&fmt, try_fmt, sizeof (fmt));
2087 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2089 if (r < 0 && errno == ENOTTY) {
2090 /* The driver might not implement TRY_FMT, in which case we will try
2092 if (GST_V4L2_IS_ACTIVE (v4l2object))
2095 memcpy (&fmt, try_fmt, sizeof (fmt));
2096 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2098 memcpy (try_fmt, &fmt, sizeof (fmt));
2102 memcpy (try_fmt, &fmt, sizeof (fmt));
2103 GST_WARNING_OBJECT (v4l2object->element,
2104 "Unable to try format: %s", g_strerror (errno));
2110 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2111 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2113 struct v4l2_format fmt;
2114 GValue interlace_formats = { 0, };
2115 GstVideoInterlaceMode interlace_mode, prev = -1;
2117 const gchar *mode_strings[] = { "progressive",
2122 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2125 if (v4l2object->never_interlaced) {
2126 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2130 g_value_init (&interlace_formats, GST_TYPE_LIST);
2132 /* Try twice - once for NONE, once for INTERLACED. */
2133 memset (&fmt, 0, sizeof (fmt));
2134 fmt.type = v4l2object->type;
2135 fmt.fmt.pix.width = width;
2136 fmt.fmt.pix.height = height;
2137 fmt.fmt.pix.pixelformat = pixelformat;
2138 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2140 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2141 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2142 GValue interlace_enum = { 0, };
2143 g_value_init (&interlace_enum, G_TYPE_STRING);
2144 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2145 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2146 prev = interlace_mode;
2149 memset (&fmt, 0, sizeof (fmt));
2150 fmt.type = v4l2object->type;
2151 fmt.fmt.pix.width = width;
2152 fmt.fmt.pix.height = height;
2153 fmt.fmt.pix.pixelformat = pixelformat;
2154 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2156 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2157 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode) &&
2158 prev != interlace_mode) {
2159 GValue interlace_enum = { 0, };
2160 g_value_init (&interlace_enum, G_TYPE_STRING);
2161 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2162 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2165 gst_v4l2src_value_simplify (&interlace_formats);
2166 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2171 gst_v4l2_object_fill_colorimetry_list (GValue * list,
2172 GstVideoColorimetry * cinfo)
2174 GValue colorimetry = G_VALUE_INIT;
2177 gboolean found = FALSE;
2179 g_value_init (&colorimetry, G_TYPE_STRING);
2180 g_value_take_string (&colorimetry, gst_video_colorimetry_to_string (cinfo));
2182 /* only insert if no duplicate */
2183 size = gst_value_list_get_size (list);
2184 for (i = 0; i < size; i++) {
2187 tmp = gst_value_list_get_value (list, i);
2188 if (gst_value_compare (&colorimetry, tmp) == GST_VALUE_EQUAL) {
2195 gst_value_list_append_and_take_value (list, &colorimetry);
2197 g_value_unset (&colorimetry);
2201 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2202 guint32 width, guint32 height, guint32 pixelformat)
2204 struct v4l2_format fmt;
2205 GValue list = G_VALUE_INIT;
2206 GstVideoColorimetry cinfo;
2207 enum v4l2_colorspace req_cspace;
2209 memset (&fmt, 0, sizeof (fmt));
2210 fmt.type = v4l2object->type;
2211 fmt.fmt.pix.width = width;
2212 fmt.fmt.pix.height = height;
2213 fmt.fmt.pix.pixelformat = pixelformat;
2215 g_value_init (&list, GST_TYPE_LIST);
2217 /* step 1: get device default colorspace and insert it first as
2218 * it should be the preferred one */
2219 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2220 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2221 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2224 /* step 2: probe all colorspace other than default
2225 * We don't probe all colorspace, range, matrix and transfer combination to
2226 * avoid ioctl flooding which could greatly increase initialization time
2227 * with low-speed devices (UVC...) */
2228 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2229 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++) {
2230 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2231 if (req_cspace == V4L2_COLORSPACE_BT878)
2234 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2235 fmt.fmt.pix_mp.colorspace = req_cspace;
2237 fmt.fmt.pix.colorspace = req_cspace;
2239 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2240 enum v4l2_colorspace colorspace;
2242 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2243 colorspace = fmt.fmt.pix_mp.colorspace;
2245 colorspace = fmt.fmt.pix.colorspace;
2247 if (colorspace == req_cspace) {
2248 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2249 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2254 if (gst_value_list_get_size (&list) > 0)
2255 gst_structure_take_value (s, "colorimetry", &list);
2257 g_value_unset (&list);
2262 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2263 static GstStructure *
2264 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2265 guint32 pixelformat,
2266 guint32 width, guint32 height, const GstStructure * template)
2268 gint fd = v4l2object->video_fd;
2269 struct v4l2_frmivalenum ival;
2272 GValue rates = { 0, };
2274 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2276 ival.pixel_format = pixelformat;
2278 ival.height = height;
2280 GST_LOG_OBJECT (v4l2object->element,
2281 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2282 GST_FOURCC_ARGS (pixelformat));
2284 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2285 * fraction to get framerate */
2286 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2287 goto enum_frameintervals_failed;
2289 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2290 GValue rate = { 0, };
2292 g_value_init (&rates, GST_TYPE_LIST);
2293 g_value_init (&rate, GST_TYPE_FRACTION);
2296 num = ival.discrete.numerator;
2297 denom = ival.discrete.denominator;
2299 if (num > G_MAXINT || denom > G_MAXINT) {
2300 /* let us hope we don't get here... */
2305 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
2308 /* swap to get the framerate */
2309 gst_value_set_fraction (&rate, denom, num);
2310 gst_value_list_append_value (&rates, &rate);
2313 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2314 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2315 GValue min = { 0, };
2316 GValue step = { 0, };
2317 GValue max = { 0, };
2318 gboolean added = FALSE;
2319 guint32 minnum, mindenom;
2320 guint32 maxnum, maxdenom;
2322 g_value_init (&rates, GST_TYPE_LIST);
2324 g_value_init (&min, GST_TYPE_FRACTION);
2325 g_value_init (&step, GST_TYPE_FRACTION);
2326 g_value_init (&max, GST_TYPE_FRACTION);
2329 minnum = ival.stepwise.min.numerator;
2330 mindenom = ival.stepwise.min.denominator;
2331 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2335 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
2337 gst_value_set_fraction (&min, minnum, mindenom);
2340 maxnum = ival.stepwise.max.numerator;
2341 maxdenom = ival.stepwise.max.denominator;
2342 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2347 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
2349 gst_value_set_fraction (&max, maxnum, maxdenom);
2352 num = ival.stepwise.step.numerator;
2353 denom = ival.stepwise.step.denominator;
2354 if (num > G_MAXINT || denom > G_MAXINT) {
2359 if (num == 0 || denom == 0) {
2360 /* in this case we have a wrong fraction or no step, set the step to max
2361 * so that we only add the min value in the loop below */
2366 /* since we only have gst_value_fraction_subtract and not add, negate the
2368 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
2370 gst_value_set_fraction (&step, -num, denom);
2372 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2373 GValue rate = { 0, };
2375 num = gst_value_get_fraction_numerator (&min);
2376 denom = gst_value_get_fraction_denominator (&min);
2377 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
2380 /* invert to get the framerate */
2381 g_value_init (&rate, GST_TYPE_FRACTION);
2382 gst_value_set_fraction (&rate, denom, num);
2383 gst_value_list_append_value (&rates, &rate);
2386 /* we're actually adding because step was negated above. This is because
2387 * there is no _add function... */
2388 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2389 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
2394 /* no range was added, leave the default range from the template */
2395 GST_WARNING_OBJECT (v4l2object->element,
2396 "no range added, leaving default");
2397 g_value_unset (&rates);
2399 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2400 guint32 maxnum, maxdenom;
2402 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2404 num = ival.stepwise.min.numerator;
2405 denom = ival.stepwise.min.denominator;
2406 if (num > G_MAXINT || denom > G_MAXINT) {
2411 maxnum = ival.stepwise.max.numerator;
2412 maxdenom = ival.stepwise.max.denominator;
2413 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2418 GST_LOG_OBJECT (v4l2object->element,
2419 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2422 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2428 s = gst_structure_copy (template);
2429 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2430 "height", G_TYPE_INT, (gint) height, NULL);
2431 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2432 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2434 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2436 if (G_IS_VALUE (&rates)) {
2437 gst_v4l2src_value_simplify (&rates);
2438 /* only change the framerate on the template when we have a valid probed new
2440 gst_structure_take_value (s, "framerate", &rates);
2441 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2442 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2443 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2449 enum_frameintervals_failed:
2451 GST_DEBUG_OBJECT (v4l2object->element,
2452 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2453 GST_FOURCC_ARGS (pixelformat), width, height);
2458 /* I don't see how this is actually an error, we ignore the format then */
2459 GST_WARNING_OBJECT (v4l2object->element,
2460 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2461 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2467 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2471 gst_structure_get_int (s1, "width", &w1);
2472 gst_structure_get_int (s1, "height", &h1);
2473 gst_structure_get_int (s2, "width", &w2);
2474 gst_structure_get_int (s2, "height", &h2);
2476 /* I think it's safe to assume that this won't overflow for a while */
2477 return ((w2 * h2) - (w1 * h1));
2481 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2482 guint32 format, GstCaps * caps, GstStructure * s)
2484 GstStructure *alt_s = NULL;
2486 /* Encoded stream on output buffer need to be parsed */
2487 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2488 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2491 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2492 if (format == gst_v4l2_formats[i].format &&
2493 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2494 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2495 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2501 if (v4l2object->has_alpha_component &&
2502 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2503 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2505 case V4L2_PIX_FMT_RGB32:
2506 alt_s = gst_structure_copy (s);
2507 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2509 case V4L2_PIX_FMT_BGR32:
2510 alt_s = gst_structure_copy (s);
2511 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2518 gst_caps_append_structure (caps, s);
2521 gst_caps_append_structure (caps, alt_s);
2525 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2526 guint32 pixelformat, const GstStructure * template)
2528 GstCaps *ret = gst_caps_new_empty ();
2530 gint fd = v4l2object->video_fd;
2531 struct v4l2_frmsizeenum size;
2532 GList *results = NULL;
2535 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2536 gst_caps_append_structure (ret, gst_structure_copy (template));
2540 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2542 size.pixel_format = pixelformat;
2544 GST_DEBUG_OBJECT (v4l2object->element,
2545 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2546 GST_FOURCC_ARGS (pixelformat));
2548 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2549 goto enum_framesizes_failed;
2551 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2553 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
2554 size.discrete.width, size.discrete.height);
2556 w = MIN (size.discrete.width, G_MAXINT);
2557 h = MIN (size.discrete.height, G_MAXINT);
2561 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2562 pixelformat, w, h, template);
2565 results = g_list_prepend (results, tmp);
2569 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2570 GST_DEBUG_OBJECT (v4l2object->element,
2571 "done iterating discrete frame sizes");
2572 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2573 guint32 maxw, maxh, step_w, step_h;
2575 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
2576 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2577 size.stepwise.min_width);
2578 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2579 size.stepwise.min_height);
2580 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2581 size.stepwise.max_width);
2582 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2583 size.stepwise.max_height);
2584 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
2585 size.stepwise.step_width);
2586 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
2587 size.stepwise.step_height);
2589 w = MAX (size.stepwise.min_width, 1);
2590 h = MAX (size.stepwise.min_height, 1);
2591 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2592 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2594 step_w = MAX (size.stepwise.step_width, 1);
2595 step_h = MAX (size.stepwise.step_height, 1);
2597 /* FIXME: check for sanity and that min/max are multiples of the steps */
2599 /* we only query details for the max width/height since it's likely the
2600 * most restricted if there are any resolution-dependent restrictions */
2601 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2602 pixelformat, maxw, maxh, template);
2605 GValue step_range = G_VALUE_INIT;
2607 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2608 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2609 gst_structure_set_value (tmp, "width", &step_range);
2611 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2612 gst_structure_take_value (tmp, "height", &step_range);
2614 /* no point using the results list here, since there's only one struct */
2615 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2617 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2620 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
2621 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2622 size.stepwise.min_width);
2623 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2624 size.stepwise.min_height);
2625 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2626 size.stepwise.max_width);
2627 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2628 size.stepwise.max_height);
2630 w = MAX (size.stepwise.min_width, 1);
2631 h = MAX (size.stepwise.min_height, 1);
2632 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2633 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2636 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2639 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2640 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2643 /* no point using the results list here, since there's only one struct */
2644 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2650 /* we use an intermediary list to store and then sort the results of the
2651 * probing because we can't make any assumptions about the order in which
2652 * the driver will give us the sizes, but we want the final caps to contain
2653 * the results starting with the highest resolution and having the lowest
2654 * resolution last, since order in caps matters for things like fixation. */
2655 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2656 while (results != NULL) {
2657 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
2659 results = g_list_delete_link (results, results);
2662 if (gst_caps_is_empty (ret))
2663 goto enum_framesizes_no_results;
2668 enum_framesizes_failed:
2670 /* I don't see how this is actually an error */
2671 GST_DEBUG_OBJECT (v4l2object->element,
2672 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2673 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2674 goto default_frame_sizes;
2676 enum_framesizes_no_results:
2678 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2679 * question doesn't actually support it yet */
2680 GST_DEBUG_OBJECT (v4l2object->element,
2681 "No results for pixelformat %" GST_FOURCC_FORMAT
2682 " enumerating frame sizes, trying fallback",
2683 GST_FOURCC_ARGS (pixelformat));
2684 goto default_frame_sizes;
2688 GST_WARNING_OBJECT (v4l2object->element,
2689 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2690 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2691 goto default_frame_sizes;
2694 default_frame_sizes:
2696 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2698 /* This code is for Linux < 2.6.19 */
2700 max_w = max_h = GST_V4L2_MAX_SIZE;
2701 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2703 GST_WARNING_OBJECT (v4l2object->element,
2704 "Could not probe minimum capture size for pixelformat %"
2705 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2707 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2709 GST_WARNING_OBJECT (v4l2object->element,
2710 "Could not probe maximum capture size for pixelformat %"
2711 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2714 /* Since we can't get framerate directly, try to use the current norm */
2715 if (v4l2object->tv_norm && v4l2object->norms) {
2717 GstTunerNorm *norm = NULL;
2718 GstTunerNorm *current =
2719 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2721 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2722 norm = (GstTunerNorm *) norms->data;
2723 if (!strcmp (norm->label, current->label))
2726 /* If it's possible, set framerate to that (discrete) value */
2728 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2729 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2733 tmp = gst_structure_copy (template);
2735 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2737 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2738 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2739 /* if norm can't be used, copy the template framerate */
2740 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2745 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2747 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2750 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2752 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2754 /* We could consider setting interlace mode from min and max. */
2755 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
2757 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2758 /* We could consider to check colorspace for min too, in case it depends on
2759 * the size. But in this case, min and max could not be enough */
2760 gst_v4l2_object_add_colorspace (v4l2object, tmp, max_w, max_h, pixelformat);
2762 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2768 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2769 guint32 pixelformat, gint * width, gint * height)
2771 struct v4l2_format fmt;
2772 gboolean ret = FALSE;
2773 GstVideoInterlaceMode interlace_mode;
2775 g_return_val_if_fail (width != NULL, FALSE);
2776 g_return_val_if_fail (height != NULL, FALSE);
2778 GST_LOG_OBJECT (v4l2object->element,
2779 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2780 *width, *height, GST_FOURCC_ARGS (pixelformat));
2782 memset (&fmt, 0, sizeof (struct v4l2_format));
2784 /* get size delimiters */
2785 memset (&fmt, 0, sizeof (fmt));
2786 fmt.type = v4l2object->type;
2787 fmt.fmt.pix.width = *width;
2788 fmt.fmt.pix.height = *height;
2789 fmt.fmt.pix.pixelformat = pixelformat;
2790 fmt.fmt.pix.field = V4L2_FIELD_ANY;
2792 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
2795 GST_LOG_OBJECT (v4l2object->element,
2796 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2798 *width = fmt.fmt.pix.width;
2799 *height = fmt.fmt.pix.height;
2801 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2802 GST_WARNING_OBJECT (v4l2object->element,
2803 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
2804 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
2812 GST_WARNING_OBJECT (v4l2object->element,
2813 "Unable to try format: %s", g_strerror (errno));
2820 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2824 GST_DEBUG_OBJECT (v4l2object->element, "initializing the %s system",
2825 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
2827 GST_V4L2_CHECK_OPEN (v4l2object);
2828 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2830 /* find transport */
2831 mode = v4l2object->req_mode;
2833 if (v4l2object->device_caps & V4L2_CAP_READWRITE) {
2834 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2835 mode = GST_V4L2_IO_RW;
2836 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2837 goto method_not_supported;
2839 if (v4l2object->device_caps & V4L2_CAP_STREAMING) {
2840 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2841 mode = GST_V4L2_IO_MMAP;
2842 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2843 goto method_not_supported;
2845 /* if still no transport selected, error out */
2846 if (mode == GST_V4L2_IO_AUTO)
2847 goto no_supported_capture_method;
2849 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2850 v4l2object->mode = mode;
2852 /* If min_buffers is not set, the driver either does not support the control or
2853 it has not been asked yet via propose_allocation/decide_allocation. */
2854 if (!v4l2object->min_buffers)
2855 gst_v4l2_get_driver_min_buffers (v4l2object);
2857 /* Map the buffers */
2858 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2860 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2861 goto buffer_pool_new_failed;
2863 GST_V4L2_SET_ACTIVE (v4l2object);
2868 buffer_pool_new_failed:
2870 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2871 (_("Could not map buffers from device '%s'"),
2872 v4l2object->videodev),
2873 ("Failed to create buffer pool: %s", g_strerror (errno)));
2876 method_not_supported:
2878 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2879 (_("The driver of device '%s' does not support the IO method %d"),
2880 v4l2object->videodev, mode), (NULL));
2883 no_supported_capture_method:
2885 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2886 (_("The driver of device '%s' does not support any known IO "
2887 "method."), v4l2object->videodev), (NULL));
2893 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
2894 gint plane, gint stride)
2896 const GstVideoFormatInfo *finfo = info->finfo;
2898 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2899 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
2902 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
2903 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2904 tile_height = 1 << hs;
2906 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
2907 info->height + align->padding_top + align->padding_bottom);
2908 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
2910 x_tiles = stride >> ws;
2911 y_tiles = padded_height >> hs;
2912 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
2914 info->stride[plane] = stride;
2919 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
2920 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
2922 const GstVideoFormatInfo *finfo = info->finfo;
2923 gint i, estride, padded_height;
2926 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
2928 padded_height = info->height + align->padding_top + align->padding_bottom;
2930 for (i = 0; i < finfo->n_planes; i++) {
2931 estride = gst_v4l2_object_extrapolate_stride (finfo, i, stride);
2933 gst_v4l2_object_set_stride (info, align, i, estride);
2935 info->offset[i] = offs;
2937 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
2939 GST_DEBUG_OBJECT (v4l2object->element,
2940 "Extrapolated for plane %d with base stride %d: "
2941 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
2947 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
2948 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
2949 GstVideoInfo * info, GstVideoAlignment * align)
2951 const GstVideoFormatInfo *finfo = info->finfo;
2952 gboolean standard_stride = TRUE;
2953 gint stride, padded_width, padded_height, i;
2955 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
2956 v4l2object->n_v4l2_planes = 1;
2957 info->size = format->fmt.pix.sizeimage;
2961 /* adjust right padding */
2962 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2963 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
2965 stride = format->fmt.pix.bytesperline;
2967 padded_width = stride / GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
2969 if (padded_width < format->fmt.pix.width)
2970 GST_WARNING_OBJECT (v4l2object->element,
2971 "Driver bug detected, stride is too small for the width");
2973 align->padding_right = padded_width - info->width - align->padding_left;
2975 /* adjust bottom padding */
2976 padded_height = format->fmt.pix.height;
2978 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2979 guint hs, tile_height;
2981 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2982 tile_height = 1 << hs;
2984 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
2987 align->padding_bottom = padded_height - info->height - align->padding_top;
2989 /* setup the strides and offset */
2990 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
2991 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
2993 /* figure out the frame layout */
2994 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
2996 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
2997 stride = pix_mp->plane_fmt[i].bytesperline;
2999 if (info->stride[i] != stride)
3000 standard_stride = FALSE;
3002 gst_v4l2_object_set_stride (info, align, i, stride);
3003 info->offset[i] = info->size;
3004 info->size += pix_mp->plane_fmt[i].sizeimage;
3007 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3008 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
3009 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3010 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3013 /* only one plane in non-MPLANE mode */
3014 v4l2object->n_v4l2_planes = 1;
3015 info->size = format->fmt.pix.sizeimage;
3016 stride = format->fmt.pix.bytesperline;
3018 if (info->stride[0] != stride)
3019 standard_stride = FALSE;
3021 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3024 /* adjust the offset to take into account left and top */
3025 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3026 if ((align->padding_left + align->padding_top) > 0)
3027 GST_WARNING_OBJECT (v4l2object->element,
3028 "Left and top padding is not permitted for tiled formats");
3030 for (i = 0; i < finfo->n_planes; i++) {
3033 /* FIXME we assume plane as component as this is true for all supported
3034 * format we support. */
3036 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left);
3037 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top);
3039 info->offset[i] += (vedge * info->stride[i]) +
3040 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i));
3045 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %" G_GSIZE_FORMAT,
3048 /* to avoid copies we need video meta if top or left padding */
3049 v4l2object->need_video_meta =
3050 ((align->padding_top + align->padding_left) != 0);
3052 /* ... or if stride is non "standard" */
3053 if (!standard_stride)
3054 v4l2object->need_video_meta = TRUE;
3056 /* ... or also video meta if we use multiple, non-contiguous, planes */
3057 if (v4l2object->n_v4l2_planes > 1)
3058 v4l2object->need_video_meta = TRUE;
3060 v4l2object->info = *info;
3061 v4l2object->align = *align;
3062 v4l2object->format = *format;
3063 v4l2object->fmtdesc = fmtdesc;
3065 /* if we have a framerate pre-calculate duration */
3066 if (info->fps_n > 0 && info->fps_d > 0) {
3067 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
3070 v4l2object->duration = GST_CLOCK_TIME_NONE;
3075 gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
3076 gint plane, gint stride)
3080 switch (finfo->format) {
3081 case GST_VIDEO_FORMAT_NV12:
3082 case GST_VIDEO_FORMAT_NV12_64Z32:
3083 case GST_VIDEO_FORMAT_NV21:
3084 case GST_VIDEO_FORMAT_NV16:
3085 case GST_VIDEO_FORMAT_NV61:
3086 case GST_VIDEO_FORMAT_NV24:
3087 estride = (plane == 0 ? 1 : 2) *
3088 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3091 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3099 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3100 gboolean try_only, GstV4l2Error * error)
3102 gint fd = v4l2object->video_fd;
3103 struct v4l2_format format;
3104 struct v4l2_streamparm streamparm;
3105 enum v4l2_field field;
3106 guint32 pixelformat;
3107 struct v4l2_fmtdesc *fmtdesc;
3109 GstVideoAlignment align;
3110 gint width, height, fps_n, fps_d;
3114 enum v4l2_colorspace colorspace = 0;
3115 enum v4l2_quantization range = 0;
3116 enum v4l2_ycbcr_encoding matrix = 0;
3117 enum v4l2_xfer_func transfer = 0;
3119 GST_V4L2_CHECK_OPEN (v4l2object);
3121 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3123 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3125 gst_video_info_init (&info);
3126 gst_video_alignment_reset (&align);
3128 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3131 pixelformat = fmtdesc->pixelformat;
3132 width = GST_VIDEO_INFO_WIDTH (&info);
3133 height = GST_VIDEO_INFO_HEIGHT (&info);
3134 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3135 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3137 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3138 * or if contiguous is prefered */
3139 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3140 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3143 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
3144 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
3145 /* ideally we would differentiate between types of interlaced video
3146 * but there is not sufficient information in the caps..
3148 field = V4L2_FIELD_INTERLACED;
3150 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
3151 field = V4L2_FIELD_NONE;
3154 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3155 /* We first pick th main colorspace from the primaries */
3156 switch (info.colorimetry.primaries) {
3157 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3158 /* There is two colorspaces using these primaries, use the range to
3160 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3161 colorspace = V4L2_COLORSPACE_REC709;
3163 colorspace = V4L2_COLORSPACE_SRGB;
3165 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3166 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3168 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3169 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3171 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3172 colorspace = V4L2_COLORSPACE_SMPTE170M;
3174 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3175 colorspace = V4L2_COLORSPACE_SMPTE240M;
3178 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3179 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3180 /* We don't know, we will guess */
3184 GST_WARNING_OBJECT (v4l2object->element,
3185 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3189 switch (info.colorimetry.range) {
3190 case GST_VIDEO_COLOR_RANGE_0_255:
3191 range = V4L2_QUANTIZATION_FULL_RANGE;
3193 case GST_VIDEO_COLOR_RANGE_16_235:
3194 range = V4L2_QUANTIZATION_LIM_RANGE;
3196 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3197 /* We let the driver pick a default one */
3200 GST_WARNING_OBJECT (v4l2object->element,
3201 "Unknown colorimetry range %d", info.colorimetry.range);
3205 switch (info.colorimetry.matrix) {
3206 case GST_VIDEO_COLOR_MATRIX_RGB:
3207 /* Unspecified, leave to default */
3209 /* FCC is about the same as BT601 with less digit */
3210 case GST_VIDEO_COLOR_MATRIX_FCC:
3211 case GST_VIDEO_COLOR_MATRIX_BT601:
3212 matrix = V4L2_YCBCR_ENC_601;
3214 case GST_VIDEO_COLOR_MATRIX_BT709:
3215 matrix = V4L2_YCBCR_ENC_709;
3217 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3218 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3220 case GST_VIDEO_COLOR_MATRIX_BT2020:
3221 matrix = V4L2_YCBCR_ENC_BT2020;
3223 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3224 /* We let the driver pick a default one */
3227 GST_WARNING_OBJECT (v4l2object->element,
3228 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3232 switch (info.colorimetry.transfer) {
3233 case GST_VIDEO_TRANSFER_GAMMA18:
3234 case GST_VIDEO_TRANSFER_GAMMA20:
3235 case GST_VIDEO_TRANSFER_GAMMA22:
3236 case GST_VIDEO_TRANSFER_GAMMA28:
3237 GST_WARNING_OBJECT (v4l2object->element,
3238 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3240 case GST_VIDEO_TRANSFER_GAMMA10:
3241 transfer = V4L2_XFER_FUNC_NONE;
3243 case GST_VIDEO_TRANSFER_BT709:
3244 transfer = V4L2_XFER_FUNC_709;
3246 case GST_VIDEO_TRANSFER_SMPTE240M:
3247 transfer = V4L2_XFER_FUNC_SMPTE240M;
3249 case GST_VIDEO_TRANSFER_SRGB:
3250 transfer = V4L2_XFER_FUNC_SRGB;
3252 case GST_VIDEO_TRANSFER_LOG100:
3253 case GST_VIDEO_TRANSFER_LOG316:
3254 GST_WARNING_OBJECT (v4l2object->element,
3255 "LOG 100, 316 transfer functions not supported");
3256 /* FIXME No known sensible default, maybe AdobeRGB ? */
3258 case GST_VIDEO_TRANSFER_UNKNOWN:
3259 /* We let the driver pick a default one */
3262 GST_WARNING_OBJECT (v4l2object->element,
3263 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
3267 if (colorspace == 0) {
3268 /* Try to guess colorspace according to pixelformat and size */
3269 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3270 /* SD streams likely use SMPTE170M and HD streams REC709 */
3271 if (width <= 720 && height <= 576)
3272 colorspace = V4L2_COLORSPACE_SMPTE170M;
3274 colorspace = V4L2_COLORSPACE_REC709;
3275 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3276 colorspace = V4L2_COLORSPACE_SRGB;
3277 transfer = V4L2_XFER_FUNC_NONE;
3282 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
3283 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3284 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3286 memset (&format, 0x00, sizeof (struct v4l2_format));
3287 format.type = v4l2object->type;
3290 format.type = v4l2object->type;
3291 format.fmt.pix_mp.pixelformat = pixelformat;
3292 format.fmt.pix_mp.width = width;
3293 format.fmt.pix_mp.height = height;
3294 format.fmt.pix_mp.field = field;
3295 format.fmt.pix_mp.num_planes = n_v4l_planes;
3297 /* try to ask our prefered stride but it's not a failure if not
3299 for (i = 0; i < n_v4l_planes; i++) {
3300 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3302 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3303 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3304 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3306 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3309 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3310 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3312 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3314 format.type = v4l2object->type;
3315 format.fmt.pix.width = width;
3316 format.fmt.pix.height = height;
3317 format.fmt.pix.pixelformat = pixelformat;
3318 format.fmt.pix.field = field;
3320 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3321 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3322 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3324 /* try to ask our prefered stride */
3325 format.fmt.pix.bytesperline = stride;
3327 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3328 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3331 GST_DEBUG_OBJECT (v4l2object->element, "Desired format is %dx%d, format "
3332 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3333 format.fmt.pix_mp.height,
3334 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3335 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3337 #ifndef GST_DISABLE_GST_DEBUG
3339 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3340 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3341 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3343 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3344 format.fmt.pix.bytesperline);
3348 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3350 format.fmt.pix_mp.colorspace = colorspace;
3351 format.fmt.pix_mp.quantization = range;
3352 format.fmt.pix_mp.ycbcr_enc = matrix;
3353 format.fmt.pix_mp.xfer_func = transfer;
3355 format.fmt.pix.colorspace = colorspace;
3356 format.fmt.pix.quantization = range;
3357 format.fmt.pix.ycbcr_enc = matrix;
3358 format.fmt.pix.xfer_func = transfer;
3361 GST_DEBUG_OBJECT (v4l2object->element, "Desired colorspace is %d:%d:%d:%d",
3362 colorspace, range, matrix, transfer);
3366 if (v4l2_ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3367 goto try_fmt_failed;
3369 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3370 goto set_fmt_failed;
3373 GST_DEBUG_OBJECT (v4l2object->element, "Got format of %dx%d, format "
3374 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d",
3375 format.fmt.pix.width, format.fmt.pix_mp.height,
3376 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3377 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3378 is_mplane ? format.fmt.pix_mp.colorspace : format.fmt.pix.colorspace);
3380 #ifndef GST_DISABLE_GST_DEBUG
3382 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3383 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3384 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3385 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3387 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3388 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3392 if (format.fmt.pix.pixelformat != pixelformat)
3393 goto invalid_pixelformat;
3395 /* Only negotiate size with raw data.
3396 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3397 * in ASF mode for example, there is also not reason for a driver to
3398 * change the size. */
3399 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3400 /* We can crop larger images */
3401 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3402 goto invalid_dimensions;
3404 /* Note, this will be adjusted if upstream has non-centered cropping. */
3405 align.padding_top = 0;
3406 align.padding_bottom = format.fmt.pix.height - height;
3407 align.padding_left = 0;
3408 align.padding_right = format.fmt.pix.width - width;
3411 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3412 goto invalid_planes;
3414 if (try_only) /* good enough for trying only */
3417 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
3418 struct v4l2_control ctl = { 0, };
3419 ctl.id = V4L2_CID_ALPHA_COMPONENT;
3422 if (v4l2_ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
3423 GST_WARNING_OBJECT (v4l2object->element,
3424 "Failed to set alpha component value");
3427 /* Is there a reason we require the caller to always specify a framerate? */
3428 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
3431 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
3432 streamparm.type = v4l2object->type;
3434 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
3435 goto get_parm_failed;
3437 GST_VIDEO_INFO_FPS_N (&info) =
3438 streamparm.parm.capture.timeperframe.denominator;
3439 GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
3441 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
3442 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3443 GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
3444 streamparm.parm.capture.timeperframe.denominator,
3445 streamparm.parm.capture.timeperframe.numerator);
3447 /* We used to skip frame rate setup if the camera was already setup
3448 * with the requested frame rate. This breaks some cameras though,
3449 * causing them to not output data (several models of Thinkpad cameras
3450 * have this problem at least).
3451 * So, don't skip. */
3452 GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
3454 /* We want to change the frame rate, so check whether we can. Some cheap USB
3455 * cameras don't have the capability */
3456 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3457 GST_DEBUG_OBJECT (v4l2object->element,
3458 "Not setting framerate (not supported)");
3462 /* Note: V4L2 wants the frame interval, we have the frame rate */
3463 streamparm.parm.capture.timeperframe.numerator = fps_d;
3464 streamparm.parm.capture.timeperframe.denominator = fps_n;
3466 /* some cheap USB cam's won't accept any change */
3467 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3468 goto set_parm_failed;
3470 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
3471 streamparm.parm.capture.timeperframe.denominator > 0) {
3472 /* get new values */
3473 fps_d = streamparm.parm.capture.timeperframe.numerator;
3474 fps_n = streamparm.parm.capture.timeperframe.denominator;
3476 GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
3479 /* fix v4l2 capture driver to provide framerate values */
3480 GST_WARNING_OBJECT (v4l2object->element,
3481 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
3484 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3485 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3489 /* add boolean return, so we can fail on drivers bugs */
3490 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
3492 /* now configure the pool */
3493 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
3501 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
3507 if (errno == EBUSY) {
3508 GST_V4L2_ERROR (error, RESOURCE, BUSY,
3509 (_("Device '%s' is busy"), v4l2object->videodev),
3510 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3511 GST_FOURCC_ARGS (pixelformat), width, height,
3512 g_strerror (errno)));
3518 if (errno == EBUSY) {
3519 GST_V4L2_ERROR (error, RESOURCE, BUSY,
3520 (_("Device '%s' is busy"), v4l2object->videodev),
3521 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3522 GST_FOURCC_ARGS (pixelformat), width, height,
3523 g_strerror (errno)));
3525 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3526 (_("Device '%s' cannot capture at %dx%d"),
3527 v4l2object->videodev, width, height),
3528 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3529 GST_FOURCC_ARGS (pixelformat), width, height,
3530 g_strerror (errno)));
3537 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3538 (_("Device '%s' cannot capture at %dx%d"),
3539 v4l2object->videodev, width, height),
3540 ("Tried to capture at %dx%d, but device returned size %dx%d",
3541 width, height, format.fmt.pix.width, format.fmt.pix.height));
3545 invalid_pixelformat:
3548 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3549 (_("Device '%s' cannot capture in the specified format"),
3550 v4l2object->videodev),
3551 ("Tried to capture in %" GST_FOURCC_FORMAT
3552 ", but device returned format" " %" GST_FOURCC_FORMAT,
3553 GST_FOURCC_ARGS (pixelformat),
3554 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
3561 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3562 (_("Device '%s' does support non-contiguous planes"),
3563 v4l2object->videodev),
3564 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
3570 /* it's possible that this call is not supported */
3571 if (errno != EINVAL && errno != ENOTTY) {
3572 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3573 (_("Could not get parameters on device '%s'"),
3574 v4l2object->videodev), GST_ERROR_SYSTEM);
3580 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3581 (_("Video device did not accept new frame rate setting.")),
3587 /* setup_pool already send the error */
3593 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps,
3594 GstV4l2Error * error)
3596 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE, error);
3600 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps,
3601 GstV4l2Error * error)
3603 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE, error);
3607 * gst_v4l2_object_acquire_format:
3608 * @v4l2object the object
3609 * @info a GstVideoInfo to be filled
3611 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
3612 * the output format is choosen by the HW.
3614 * Returns: %TRUE on success, %FALSE on failure.
3617 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
3619 struct v4l2_fmtdesc *fmtdesc;
3620 struct v4l2_format fmt;
3621 struct v4l2_crop crop;
3622 struct v4l2_selection sel;
3623 struct v4l2_rect *r = NULL;
3624 GstVideoFormat format;
3625 guint width, height;
3626 GstVideoAlignment align;
3628 gst_video_info_init (info);
3629 gst_video_alignment_reset (&align);
3631 memset (&fmt, 0x00, sizeof (struct v4l2_format));
3632 fmt.type = v4l2object->type;
3633 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
3634 goto get_fmt_failed;
3636 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
3637 fmt.fmt.pix.pixelformat);
3638 if (fmtdesc == NULL)
3639 goto unsupported_format;
3641 /* No need to care about mplane, the four first params are the same */
3642 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
3644 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
3645 if (format == GST_VIDEO_FORMAT_UNKNOWN)
3646 goto unsupported_format;
3648 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
3649 goto invalid_dimensions;
3651 width = fmt.fmt.pix.width;
3652 height = fmt.fmt.pix.height;
3654 /* Use the default compose rectangle */
3655 memset (&sel, 0, sizeof (struct v4l2_selection));
3656 sel.type = v4l2object->type;
3657 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
3658 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0) {
3661 /* For ancient kernels, fall back to G_CROP */
3662 memset (&crop, 0, sizeof (struct v4l2_crop));
3663 crop.type = v4l2object->type;
3664 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
3668 align.padding_left = r->left;
3669 align.padding_top = r->top;
3670 align.padding_right = width - r->width - r->left;
3671 align.padding_bottom = height - r->height - r->top;
3676 gst_video_info_set_format (info, format, width, height);
3678 switch (fmt.fmt.pix.field) {
3679 case V4L2_FIELD_ANY:
3680 case V4L2_FIELD_NONE:
3681 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
3683 case V4L2_FIELD_INTERLACED:
3684 case V4L2_FIELD_INTERLACED_TB:
3685 case V4L2_FIELD_INTERLACED_BT:
3686 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
3689 goto unsupported_field;
3692 gst_v4l2_object_get_colorspace (&fmt, &info->colorimetry);
3694 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
3696 /* Shall we setup the pool ? */
3702 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3703 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
3708 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3709 (_("Video device returned invalid dimensions.")),
3710 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
3711 fmt.fmt.pix.height));
3716 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3717 (_("Video devices uses an unsupported interlacing method.")),
3718 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
3723 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3724 (_("Video devices uses an unsupported pixel format.")),
3725 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
3726 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
3732 gst_v4l2_object_set_crop (GstV4l2Object * obj)
3734 struct v4l2_crop crop = { 0 };
3736 crop.type = obj->type;
3737 crop.c.left = obj->align.padding_left;
3738 crop.c.top = obj->align.padding_top;
3739 crop.c.width = obj->info.width;
3740 crop.c.height = obj->info.height;
3742 if (obj->align.padding_left + obj->align.padding_top +
3743 obj->align.padding_right + obj->align.padding_bottom == 0) {
3744 GST_DEBUG_OBJECT (obj->element, "no cropping needed");
3748 GST_DEBUG_OBJECT (obj->element,
3749 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3750 crop.c.width, crop.c.height);
3752 if (v4l2_ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
3753 GST_WARNING_OBJECT (obj->element, "VIDIOC_S_CROP failed");
3757 if (v4l2_ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
3758 GST_WARNING_OBJECT (obj->element, "VIDIOC_G_CROP failed");
3762 GST_DEBUG_OBJECT (obj->element,
3763 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3764 crop.c.width, crop.c.height);
3770 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
3772 GstStructure *config;
3776 if (!v4l2object->pool)
3779 config = gst_buffer_pool_get_config (v4l2object->pool);
3780 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
3782 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
3784 gst_structure_free (config);
3790 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
3792 gboolean ret = TRUE;
3794 GST_LOG_OBJECT (v4l2object->element, "start flushing");
3796 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3797 gst_buffer_pool_set_flushing (v4l2object->pool, TRUE);
3803 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
3805 gboolean ret = TRUE;
3807 GST_LOG_OBJECT (v4l2object->element, "stop flushing");
3809 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3810 gst_buffer_pool_set_flushing (v4l2object->pool, FALSE);
3816 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
3818 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
3820 if (!GST_V4L2_IS_OPEN (v4l2object))
3822 if (!GST_V4L2_IS_ACTIVE (v4l2object))
3825 if (v4l2object->pool) {
3826 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
3827 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
3828 gst_object_unref (v4l2object->pool);
3829 v4l2object->pool = NULL;
3832 GST_V4L2_SET_INACTIVE (v4l2object);
3839 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3845 formats = gst_v4l2_object_get_format_list (v4l2object);
3847 ret = gst_caps_new_empty ();
3849 for (walk = formats; walk; walk = walk->next) {
3850 struct v4l2_fmtdesc *format;
3851 GstStructure *template;
3853 format = (struct v4l2_fmtdesc *) walk->data;
3855 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
3860 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
3861 format->pixelformat, template);
3863 gst_caps_append (ret, tmp);
3865 gst_structure_free (template);
3867 GST_DEBUG_OBJECT (v4l2object->element, "unknown format %u",
3868 format->pixelformat);
3876 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
3877 gst_caps_unref (tmp);
3884 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3888 if (v4l2object->probed_caps == NULL)
3889 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
3892 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
3893 GST_CAPS_INTERSECT_FIRST);
3895 ret = gst_caps_ref (v4l2object->probed_caps);
3898 GST_INFO_OBJECT (v4l2object->element, "probed caps: %" GST_PTR_FORMAT, ret);
3899 LOG_CAPS (v4l2object->element, ret);
3905 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
3908 GstBufferPool *pool = NULL, *other_pool = NULL;
3909 GstStructure *config;
3910 guint size, min, max, own_min = 0;
3912 gboolean has_video_meta;
3913 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
3914 GstAllocator *allocator = NULL;
3915 GstAllocationParams params = { 0 };
3917 GST_DEBUG_OBJECT (obj->element, "decide allocation");
3919 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3920 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
3922 gst_query_parse_allocation (query, &caps, NULL);
3924 if (obj->pool == NULL) {
3925 if (!gst_v4l2_object_setup_pool (obj, caps))
3929 if (gst_query_get_n_allocation_params (query) > 0)
3930 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3932 if (gst_query_get_n_allocation_pools (query) > 0) {
3933 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3942 GST_DEBUG_OBJECT (obj->element, "allocation: size:%u min:%u max:%u pool:%"
3943 GST_PTR_FORMAT, size, min, max, pool);
3946 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
3948 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
3950 gst_v4l2_get_driver_min_buffers (obj);
3951 /* We can't share our own pool, if it exceed V4L2 capacity */
3952 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
3953 can_share_own_pool = FALSE;
3956 switch (obj->mode) {
3957 case GST_V4L2_IO_RW:
3959 /* in READ/WRITE mode, prefer a downstream pool because our own pool
3960 * doesn't help much, we have to write to it as well */
3961 GST_DEBUG_OBJECT (obj->element,
3962 "read/write mode: using downstream pool");
3963 /* use the bigest size, when we use our own pool we can't really do any
3964 * other size than what the hardware gives us but for downstream pools
3966 size = MAX (size, obj->info.size);
3967 } else if (can_share_own_pool) {
3968 /* no downstream pool, use our own then */
3969 GST_DEBUG_OBJECT (obj->element,
3970 "read/write mode: no downstream pool, using our own");
3971 pool = gst_object_ref (obj->pool);
3972 size = obj->info.size;
3973 pushing_from_our_pool = TRUE;
3977 case GST_V4L2_IO_USERPTR:
3978 case GST_V4L2_IO_DMABUF_IMPORT:
3979 /* in importing mode, prefer our own pool, and pass the other pool to
3980 * our own, so it can serve itself */
3982 goto no_downstream_pool;
3983 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj->pool),
3986 gst_object_unref (pool);
3987 pool = gst_object_ref (obj->pool);
3988 size = obj->info.size;
3991 case GST_V4L2_IO_MMAP:
3992 case GST_V4L2_IO_DMABUF:
3993 /* in streaming mode, prefer our own pool */
3994 /* Check if we can use it ... */
3995 if (can_share_own_pool) {
3997 gst_object_unref (pool);
3998 pool = gst_object_ref (obj->pool);
3999 size = obj->info.size;
4000 GST_DEBUG_OBJECT (obj->element,
4001 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
4002 pushing_from_our_pool = TRUE;
4004 GST_DEBUG_OBJECT (obj->element,
4005 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
4008 GST_DEBUG_OBJECT (obj->element,
4009 "streaming mode: no usable pool, copying to generic pool");
4010 size = MAX (size, obj->info.size);
4013 case GST_V4L2_IO_AUTO:
4015 GST_WARNING_OBJECT (obj->element, "unhandled mode");
4022 /* If pushing from our own pool, configure it with queried minimum,
4023 * otherwise use the minimum required */
4024 if (pushing_from_our_pool) {
4025 /* When pushing from our own pool, we need what downstream one, to be able
4026 * to fill the pipeline, the minimum required to decoder according to the
4027 * driver and 1 more, so we don't endup up with everything downstream or
4028 * held by the decoder. */
4029 own_min = min + obj->min_buffers + 1;
4031 /* If no allocation parameters where provided, allow for a little more
4032 * buffers and enable copy threshold */
4035 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4038 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4043 /* In this case we'll have to configure two buffer pool. For our buffer
4044 * pool, we'll need what the driver one, and one more, so we can dequeu */
4045 own_min = obj->min_buffers + 1;
4046 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS);
4048 /* for the downstream pool, we keep what downstream wants, though ensure
4049 * at least a minimum if downstream didn't suggest anything (we are
4050 * expecting the base class to create a default one for the context) */
4051 min = MAX (min, GST_V4L2_MIN_BUFFERS);
4053 /* To import we need the other pool to hold at least own_min */
4054 if (obj->pool == pool)
4058 /* Request a bigger max, if one was suggested but it's too small */
4060 max = MAX (min, max);
4062 /* First step, configure our own pool */
4063 config = gst_buffer_pool_get_config (obj->pool);
4065 if (obj->need_video_meta || has_video_meta) {
4066 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
4067 gst_buffer_pool_config_add_option (config,
4068 GST_BUFFER_POOL_OPTION_VIDEO_META);
4071 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4072 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
4074 GST_DEBUG_OBJECT (obj->element, "setting own pool config to %"
4075 GST_PTR_FORMAT, config);
4077 /* Our pool often need to adjust the value */
4078 if (!gst_buffer_pool_set_config (obj->pool, config)) {
4079 config = gst_buffer_pool_get_config (obj->pool);
4081 GST_DEBUG_OBJECT (obj->element, "own pool config changed to %"
4082 GST_PTR_FORMAT, config);
4084 /* our pool will adjust the maximum buffer, which we are fine with */
4085 if (!gst_buffer_pool_set_config (obj->pool, config))
4089 /* Now configure the other pool if different */
4090 if (obj->pool != pool)
4094 config = gst_buffer_pool_get_config (other_pool);
4095 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4096 gst_buffer_pool_config_set_params (config, caps, size, min, max);
4098 GST_DEBUG_OBJECT (obj->element, "setting other pool config to %"
4099 GST_PTR_FORMAT, config);
4101 /* if downstream supports video metadata, add this to the pool config */
4102 if (has_video_meta) {
4103 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
4104 gst_buffer_pool_config_add_option (config,
4105 GST_BUFFER_POOL_OPTION_VIDEO_META);
4108 if (!gst_buffer_pool_set_config (other_pool, config)) {
4109 config = gst_buffer_pool_get_config (other_pool);
4111 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
4113 gst_structure_free (config);
4117 if (!gst_buffer_pool_set_config (other_pool, config))
4123 /* For simplicity, simply read back the active configuration, so our base
4124 * class get the right information */
4125 config = gst_buffer_pool_get_config (pool);
4126 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
4127 gst_structure_free (config);
4131 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
4133 gst_query_add_allocation_pool (query, pool, size, min, max);
4136 gst_object_unref (allocator);
4139 gst_object_unref (pool);
4145 /* setup_pool already send the error */
4150 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4151 (_("Failed to configure internal buffer pool.")), (NULL));
4156 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4157 (_("Video device did not suggest any buffer size.")), (NULL));
4163 gst_object_unref (allocator);
4166 gst_object_unref (pool);
4171 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4172 (_("No downstream pool to import from.")),
4173 ("When importing DMABUF or USERPTR, we need a pool to import from"));
4179 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
4181 GstBufferPool *pool;
4182 /* we need at least 2 buffers to operate */
4183 guint size, min, max;
4187 /* Set defaults allocation parameters */
4188 size = obj->info.size;
4189 min = GST_V4L2_MIN_BUFFERS;
4190 max = VIDEO_MAX_FRAME;
4192 gst_query_parse_allocation (query, &caps, &need_pool);
4197 if ((pool = obj->pool))
4198 gst_object_ref (pool);
4202 GstStructure *config;
4204 /* we had a pool, check caps */
4205 config = gst_buffer_pool_get_config (pool);
4206 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
4208 GST_DEBUG_OBJECT (obj->element,
4209 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
4210 if (!gst_caps_is_equal (caps, pcaps)) {
4211 gst_structure_free (config);
4212 gst_object_unref (pool);
4213 goto different_caps;
4215 gst_structure_free (config);
4217 gst_v4l2_get_driver_min_buffers (obj);
4219 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS);
4221 gst_query_add_allocation_pool (query, pool, size, min, max);
4223 /* we also support various metadata */
4224 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4227 gst_object_unref (pool);
4234 GST_DEBUG_OBJECT (obj->element, "no caps specified");
4239 /* different caps, we can't use this pool */
4240 GST_DEBUG_OBJECT (obj->element, "pool has different caps");