3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
6 * gstv4l2object.c: base class for V4L2 elements
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Library General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. This library is distributed in the hope
12 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
21 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
22 * with newer GLib versions (>= 2.31.0) */
23 #define GLIB_DISABLE_DEPRECATION_WARNINGS
36 #include <gudev/gudev.h>
39 #include "v4l2_calls.h"
40 #include "gstv4l2tuner.h"
41 #include "gstv4l2colorbalance.h"
43 #include "gst/gst-i18n-plugin.h"
45 #include <gst/video/video.h>
47 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
48 #define GST_CAT_DEFAULT v4l2_debug
50 #define DEFAULT_PROP_DEVICE_NAME NULL
51 #define DEFAULT_PROP_DEVICE_FD -1
52 #define DEFAULT_PROP_FLAGS 0
53 #define DEFAULT_PROP_TV_NORM 0
54 #define DEFAULT_PROP_CHANNEL NULL
55 #define DEFAULT_PROP_FREQUENCY 0
56 #define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
58 #define ENCODED_BUFFER_SIZE (1 * 1024 * 1024)
63 V4L2_STD_OBJECT_PROPS,
67 * common format / caps utilities:
71 GST_V4L2_RAW = 1 << 0,
72 GST_V4L2_CODEC = 1 << 1,
73 GST_V4L2_TRANSPORT = 1 << 2,
74 GST_V4L2_NO_PARSE = 1 << 3,
82 GstV4L2FormatFlags flags;
85 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
87 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
88 {V4L2_PIX_FMT_ARGB555, TRUE, GST_V4L2_RAW},
89 {V4L2_PIX_FMT_XRGB555, TRUE, GST_V4L2_RAW},
90 {V4L2_PIX_FMT_ARGB555X, TRUE, GST_V4L2_RAW},
91 {V4L2_PIX_FMT_XRGB555X, TRUE, GST_V4L2_RAW},
92 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
93 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
94 {V4L2_PIX_FMT_BGR666, TRUE, GST_V4L2_RAW},
95 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
96 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
97 {V4L2_PIX_FMT_ABGR32, TRUE, GST_V4L2_RAW},
98 {V4L2_PIX_FMT_XBGR32, TRUE, GST_V4L2_RAW},
99 {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
100 {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},
102 /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
103 {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
104 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
105 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
106 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
107 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
110 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
111 {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
112 {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
113 {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
114 {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
115 {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
116 {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
117 {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},
119 /* Palette formats */
120 {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},
122 /* Chrominance formats */
123 {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},
125 /* Luminance+Chrominance formats */
126 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
127 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
128 {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
129 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
130 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
131 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
132 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
133 {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
134 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
135 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
136 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
137 {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
138 {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
139 {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
140 {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
141 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
142 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
143 {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
144 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
145 {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
146 {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
148 /* two planes -- one Y, one Cr + Cb interleaved */
149 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
150 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
151 {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
152 {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
153 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
154 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
155 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
156 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
158 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
159 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
160 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
162 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
163 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
164 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_CODEC},
165 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_CODEC},
166 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_CODEC},
168 /* compressed formats */
169 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
170 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
171 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
172 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
173 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
174 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
175 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
176 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
177 {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
178 {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
179 {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
180 {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
181 {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
182 {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
183 {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
184 {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
186 /* Vendor-specific formats */
187 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
188 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
189 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
190 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
193 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
195 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
198 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
200 gst_v4l2_device_get_type (void)
202 static GType v4l2_device_type = 0;
204 if (v4l2_device_type == 0) {
205 static const GFlagsValue values[] = {
206 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
207 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
208 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
210 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
211 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
213 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
214 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
220 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
223 return v4l2_device_type;
226 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
228 gst_v4l2_tv_norm_get_type (void)
230 static GType v4l2_tv_norm = 0;
233 static const GEnumValue tv_norms[] = {
236 {V4L2_STD_NTSC, "NTSC", "NTSC"},
237 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
238 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
239 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
240 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
242 {V4L2_STD_PAL, "PAL", "PAL"},
243 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
244 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
245 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
246 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
247 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
248 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
249 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
250 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
251 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
252 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
253 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
254 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
255 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
256 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
258 {V4L2_STD_SECAM, "SECAM", "SECAM"},
259 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
260 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
261 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
262 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
263 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
264 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
265 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
266 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
267 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
272 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
279 gst_v4l2_io_mode_get_type (void)
281 static GType v4l2_io_mode = 0;
284 static const GEnumValue io_modes[] = {
285 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
286 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
287 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
288 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
289 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
290 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
295 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
301 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
302 const char *default_device)
304 g_object_class_install_property (gobject_class, PROP_DEVICE,
305 g_param_spec_string ("device", "Device", "Device location",
306 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
308 g_param_spec_string ("device-name", "Device name",
309 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
310 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
312 g_param_spec_int ("device-fd", "File descriptor",
313 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
314 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
315 g_object_class_install_property (gobject_class, PROP_FLAGS,
316 g_param_spec_flags ("flags", "Flags", "Device type flags",
317 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
318 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
321 * GstV4l2Src:brightness:
323 * Picture brightness, or more precisely, the black level
325 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
326 g_param_spec_int ("brightness", "Brightness",
327 "Picture brightness, or more precisely, the black level", G_MININT,
329 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
331 * GstV4l2Src:contrast:
333 * Picture contrast or luma gain
335 g_object_class_install_property (gobject_class, PROP_CONTRAST,
336 g_param_spec_int ("contrast", "Contrast",
337 "Picture contrast or luma gain", G_MININT,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
341 * GstV4l2Src:saturation:
343 * Picture color saturation or chroma gain
345 g_object_class_install_property (gobject_class, PROP_SATURATION,
346 g_param_spec_int ("saturation", "Saturation",
347 "Picture color saturation or chroma gain", G_MININT,
349 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
353 * Hue or color balance
355 g_object_class_install_property (gobject_class, PROP_HUE,
356 g_param_spec_int ("hue", "Hue",
357 "Hue or color balance", G_MININT,
359 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
366 g_object_class_install_property (gobject_class, PROP_TV_NORM,
367 g_param_spec_enum ("norm", "TV norm",
369 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
370 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 * GstV4l2Src:io-mode:
377 g_object_class_install_property (gobject_class, PROP_IO_MODE,
378 g_param_spec_enum ("io-mode", "IO mode",
380 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384 * GstV4l2Src:extra-controls:
386 * Additional v4l2 controls for the device. The controls are identified
387 * by the control name (lowercase with '_' for any non-alphanumeric
392 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
393 g_param_spec_boxed ("extra-controls", "Extra Controls",
394 "Extra v4l2 controls (CIDs) for the device",
395 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 * GstV4l2Src:pixel-aspect-ratio:
400 * The pixel aspect ratio of the device. This overwrites the pixel aspect
401 * ratio queried from the device.
405 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
406 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
407 "Overwrite the pixel aspect ratio of the device", "1/1",
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 * GstV4l2Src:force-aspect-ratio:
413 * When enabled, the pixel aspect ratio queried from the device or set
414 * with the pixel-aspect-ratio property will be enforced.
418 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
419 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
420 "When enabled, the pixel aspect ratio will be enforced", TRUE,
421 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
428 g_object_class_install_property (gobject_class, PROP_DEVICE,
429 g_param_spec_string ("device", "Device", "Device location",
430 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
432 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
433 g_param_spec_string ("device-name", "Device name",
434 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
435 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
438 g_param_spec_int ("device-fd", "File descriptor",
439 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
440 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
442 g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
443 g_param_spec_enum ("output-io-mode", "Output IO mode",
444 "Output side I/O mode (matches sink pad)",
445 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
446 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
449 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
450 "Capture I/O mode (matches src pad)",
451 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
452 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
454 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
455 g_param_spec_boxed ("extra-controls", "Extra Controls",
456 "Extra v4l2 controls (CIDs) for the device",
457 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 gst_v4l2_object_new (GstElement * element,
462 enum v4l2_buf_type type,
463 const char *default_device,
464 GstV4l2GetInOutFunction get_in_out_func,
465 GstV4l2SetInOutFunction set_in_out_func,
466 GstV4l2UpdateFpsFunction update_fps_func)
468 GstV4l2Object *v4l2object;
471 * some default values
473 v4l2object = g_new0 (GstV4l2Object, 1);
475 v4l2object->type = type;
476 v4l2object->formats = NULL;
478 v4l2object->element = element;
479 v4l2object->get_in_out_func = get_in_out_func;
480 v4l2object->set_in_out_func = set_in_out_func;
481 v4l2object->update_fps_func = update_fps_func;
483 v4l2object->video_fd = -1;
484 v4l2object->active = FALSE;
485 v4l2object->videodev = g_strdup (default_device);
487 v4l2object->norms = NULL;
488 v4l2object->channels = NULL;
489 v4l2object->colors = NULL;
491 v4l2object->xwindow_id = 0;
493 v4l2object->keep_aspect = TRUE;
495 v4l2object->n_v4l2_planes = 0;
497 v4l2object->no_initial_format = FALSE;
502 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
506 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
508 g_return_if_fail (v4l2object != NULL);
510 g_free (v4l2object->videodev);
512 g_free (v4l2object->channel);
514 if (v4l2object->formats) {
515 gst_v4l2_object_clear_format_list (v4l2object);
518 if (v4l2object->probed_caps) {
519 gst_caps_unref (v4l2object->probed_caps);
527 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
529 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
530 g_slist_free (v4l2object->formats);
531 v4l2object->formats = NULL;
537 gst_v4l2_object_prop_to_cid (guint prop_id)
542 case PROP_BRIGHTNESS:
543 cid = V4L2_CID_BRIGHTNESS;
546 cid = V4L2_CID_CONTRAST;
548 case PROP_SATURATION:
549 cid = V4L2_CID_SATURATION;
555 GST_WARNING ("unmapped property id: %d", prop_id);
562 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
563 guint prop_id, const GValue * value, GParamSpec * pspec)
567 g_free (v4l2object->videodev);
568 v4l2object->videodev = g_value_dup_string (value);
570 case PROP_BRIGHTNESS:
572 case PROP_SATURATION:
575 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
578 if (GST_V4L2_IS_OPEN (v4l2object)) {
579 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
586 v4l2object->tv_norm = g_value_get_enum (value);
590 if (GST_V4L2_IS_OPEN (v4l2object)) {
591 GstTuner *tuner = GST_TUNER (v4l2object->element);
592 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
593 (gchar *) g_value_get_string (value));
596 /* like gst_tuner_set_channel (tuner, channel)
597 without g_object_notify */
598 gst_v4l2_tuner_set_channel (v4l2object, channel);
601 g_free (v4l2object->channel);
602 v4l2object->channel = g_value_dup_string (value);
606 if (GST_V4L2_IS_OPEN (v4l2object)) {
607 GstTuner *tuner = GST_TUNER (v4l2object->element);
608 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
611 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
613 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
614 without g_object_notify */
615 gst_v4l2_tuner_set_frequency (v4l2object, channel,
616 g_value_get_ulong (value));
619 v4l2object->frequency = g_value_get_ulong (value);
625 v4l2object->req_mode = g_value_get_enum (value);
627 case PROP_CAPTURE_IO_MODE:
628 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
629 v4l2object->req_mode = g_value_get_enum (value);
631 case PROP_OUTPUT_IO_MODE:
632 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
633 v4l2object->req_mode = g_value_get_enum (value);
635 case PROP_EXTRA_CONTROLS:{
636 const GstStructure *s = gst_value_get_structure (value);
638 if (v4l2object->extra_controls)
639 gst_structure_free (v4l2object->extra_controls);
641 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
642 if (GST_V4L2_IS_OPEN (v4l2object))
643 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
646 case PROP_PIXEL_ASPECT_RATIO:
647 g_free (v4l2object->par);
648 v4l2object->par = g_new0 (GValue, 1);
649 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
650 if (!g_value_transform (value, v4l2object->par)) {
651 g_warning ("Could not transform string to aspect ratio");
652 gst_value_set_fraction (v4l2object->par, 1, 1);
654 GST_DEBUG_OBJECT (v4l2object->element, "set PAR to %d/%d",
655 gst_value_get_fraction_numerator (v4l2object->par),
656 gst_value_get_fraction_denominator (v4l2object->par));
658 case PROP_FORCE_ASPECT_RATIO:
659 v4l2object->keep_aspect = g_value_get_boolean (value);
670 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
671 guint prop_id, GValue * value, GParamSpec * pspec)
675 g_value_set_string (value, v4l2object->videodev);
677 case PROP_DEVICE_NAME:
679 const guchar *new = NULL;
681 if (GST_V4L2_IS_OPEN (v4l2object)) {
682 new = v4l2object->vcap.card;
683 } else if (gst_v4l2_open (v4l2object)) {
684 new = v4l2object->vcap.card;
685 gst_v4l2_close (v4l2object);
687 g_value_set_string (value, (gchar *) new);
692 if (GST_V4L2_IS_OPEN (v4l2object))
693 g_value_set_int (value, v4l2object->video_fd);
695 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
702 if (GST_V4L2_IS_OPEN (v4l2object)) {
703 flags |= v4l2object->vcap.capabilities &
704 (V4L2_CAP_VIDEO_CAPTURE |
705 V4L2_CAP_VIDEO_OUTPUT |
706 V4L2_CAP_VIDEO_OVERLAY |
707 V4L2_CAP_VBI_CAPTURE |
708 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
710 if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
711 flags |= V4L2_CAP_VIDEO_CAPTURE;
713 if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
714 flags |= V4L2_CAP_VIDEO_OUTPUT;
716 g_value_set_flags (value, flags);
719 case PROP_BRIGHTNESS:
721 case PROP_SATURATION:
724 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
727 if (GST_V4L2_IS_OPEN (v4l2object)) {
729 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
730 g_value_set_int (value, v);
738 g_value_set_enum (value, v4l2object->tv_norm);
741 g_value_set_enum (value, v4l2object->req_mode);
743 case PROP_CAPTURE_IO_MODE:
744 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
745 g_value_set_enum (value, v4l2object->req_mode);
747 case PROP_OUTPUT_IO_MODE:
748 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
749 g_value_set_enum (value, v4l2object->req_mode);
751 case PROP_EXTRA_CONTROLS:
752 gst_value_set_structure (value, v4l2object->extra_controls);
754 case PROP_PIXEL_ASPECT_RATIO:
756 g_value_transform (v4l2object->par, value);
758 case PROP_FORCE_ASPECT_RATIO:
759 g_value_set_boolean (value, v4l2object->keep_aspect);
769 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
771 struct v4l2_control control = { 0, };
773 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
775 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
776 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
778 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
780 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
781 GST_DEBUG_OBJECT (v4l2object->element,
782 "driver requires a minimum of %d buffers", control.value);
783 v4l2object->min_buffers = control.value;
785 v4l2object->min_buffers = 0;
790 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
792 GstTunerNorm *norm = NULL;
793 GstTunerChannel *channel = NULL;
796 if (!GST_IS_TUNER (v4l2object->element))
799 tuner = GST_TUNER (v4l2object->element);
801 if (v4l2object->tv_norm)
802 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
803 GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
804 "norm=%p", (guint64) v4l2object->tv_norm, norm);
806 gst_tuner_set_norm (tuner, norm);
809 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
811 v4l2object->tv_norm =
812 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
813 gst_tuner_norm_changed (tuner, norm);
817 if (v4l2object->channel)
818 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
820 gst_tuner_set_channel (tuner, channel);
823 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
824 (v4l2object->element)));
826 g_free (v4l2object->channel);
827 v4l2object->channel = g_strdup (channel->label);
828 gst_tuner_channel_changed (tuner, channel);
833 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
834 if (v4l2object->frequency != 0) {
835 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
837 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
838 if (v4l2object->frequency == 0) {
840 gst_tuner_set_frequency (tuner, channel, 1000);
848 gst_v4l2_object_open (GstV4l2Object * v4l2object)
850 if (gst_v4l2_open (v4l2object))
851 gst_v4l2_set_defaults (v4l2object);
859 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
863 ret = gst_v4l2_dup (v4l2object, other);
869 gst_v4l2_object_close (GstV4l2Object * v4l2object)
871 if (!gst_v4l2_close (v4l2object))
874 gst_caps_replace (&v4l2object->probed_caps, NULL);
876 if (v4l2object->formats) {
877 gst_v4l2_object_clear_format_list (v4l2object);
883 static struct v4l2_fmtdesc *
884 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
887 struct v4l2_fmtdesc *fmt;
893 walk = gst_v4l2_object_get_format_list (v4l2object);
895 fmt = (struct v4l2_fmtdesc *) walk->data;
896 if (fmt->pixelformat == fourcc)
898 /* special case for jpeg */
899 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
900 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
901 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
902 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
903 fourcc == V4L2_PIX_FMT_PJPG) {
907 walk = g_slist_next (walk);
915 /* complete made up ranking, the values themselves are meaningless */
916 /* These ranks MUST be X such that X<<15 fits on a signed int - see
917 the comment at the end of gst_v4l2_object_format_get_rank. */
918 #define YUV_BASE_RANK 1000
919 #define JPEG_BASE_RANK 500
920 #define DV_BASE_RANK 200
921 #define RGB_BASE_RANK 100
922 #define YUV_ODD_BASE_RANK 50
923 #define RGB_ODD_BASE_RANK 25
924 #define BAYER_BASE_RANK 15
925 #define S910_BASE_RANK 10
926 #define GREY_BASE_RANK 5
927 #define PWC_BASE_RANK 1
930 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
932 guint32 fourcc = fmt->pixelformat;
933 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
937 case V4L2_PIX_FMT_MJPEG:
938 case V4L2_PIX_FMT_PJPG:
939 rank = JPEG_BASE_RANK;
941 case V4L2_PIX_FMT_JPEG:
942 rank = JPEG_BASE_RANK + 1;
944 case V4L2_PIX_FMT_MPEG: /* MPEG */
945 rank = JPEG_BASE_RANK + 2;
948 case V4L2_PIX_FMT_RGB332:
949 case V4L2_PIX_FMT_ARGB555:
950 case V4L2_PIX_FMT_XRGB555:
951 case V4L2_PIX_FMT_RGB555:
952 case V4L2_PIX_FMT_ARGB555X:
953 case V4L2_PIX_FMT_XRGB555X:
954 case V4L2_PIX_FMT_RGB555X:
955 case V4L2_PIX_FMT_BGR666:
956 case V4L2_PIX_FMT_RGB565:
957 case V4L2_PIX_FMT_RGB565X:
958 case V4L2_PIX_FMT_RGB444:
959 case V4L2_PIX_FMT_Y4:
960 case V4L2_PIX_FMT_Y6:
961 case V4L2_PIX_FMT_Y10:
962 case V4L2_PIX_FMT_Y12:
963 case V4L2_PIX_FMT_Y10BPACK:
964 case V4L2_PIX_FMT_YUV555:
965 case V4L2_PIX_FMT_YUV565:
966 case V4L2_PIX_FMT_YUV32:
967 case V4L2_PIX_FMT_NV12MT_16X16:
968 case V4L2_PIX_FMT_NV42:
969 case V4L2_PIX_FMT_H264_MVC:
970 rank = RGB_ODD_BASE_RANK;
973 case V4L2_PIX_FMT_RGB24:
974 case V4L2_PIX_FMT_BGR24:
975 rank = RGB_BASE_RANK - 1;
978 case V4L2_PIX_FMT_RGB32:
979 case V4L2_PIX_FMT_BGR32:
980 case V4L2_PIX_FMT_ABGR32:
981 case V4L2_PIX_FMT_XBGR32:
982 case V4L2_PIX_FMT_ARGB32:
983 case V4L2_PIX_FMT_XRGB32:
984 rank = RGB_BASE_RANK;
987 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
988 rank = GREY_BASE_RANK;
991 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
992 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
993 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
994 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
995 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
996 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
997 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
998 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
999 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1000 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1001 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1002 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1003 rank = YUV_ODD_BASE_RANK;
1006 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1007 rank = YUV_BASE_RANK + 3;
1009 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1010 rank = YUV_BASE_RANK + 2;
1012 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1013 case V4L2_PIX_FMT_YUV420M:
1014 rank = YUV_BASE_RANK + 7;
1016 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1017 rank = YUV_BASE_RANK + 10;
1019 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1020 rank = YUV_BASE_RANK + 6;
1022 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1023 rank = YUV_BASE_RANK + 9;
1025 case V4L2_PIX_FMT_YUV444:
1026 rank = YUV_BASE_RANK + 6;
1028 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1029 rank = YUV_BASE_RANK + 5;
1031 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1032 rank = YUV_BASE_RANK + 4;
1034 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1035 rank = YUV_BASE_RANK + 8;
1038 case V4L2_PIX_FMT_DV:
1039 rank = DV_BASE_RANK;
1042 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1046 case V4L2_PIX_FMT_SBGGR8:
1047 case V4L2_PIX_FMT_SGBRG8:
1048 case V4L2_PIX_FMT_SGRBG8:
1049 case V4L2_PIX_FMT_SRGGB8:
1050 rank = BAYER_BASE_RANK;
1053 case V4L2_PIX_FMT_SN9C10X:
1054 rank = S910_BASE_RANK;
1057 case V4L2_PIX_FMT_PWC1:
1058 rank = PWC_BASE_RANK;
1060 case V4L2_PIX_FMT_PWC2:
1061 rank = PWC_BASE_RANK;
1069 /* All ranks are below 1<<15 so a shift by 15
1070 * will a) make all non-emulated formats larger
1071 * than emulated and b) will not overflow
1082 format_cmp_func (gconstpointer a, gconstpointer b)
1084 const struct v4l2_fmtdesc *fa = a;
1085 const struct v4l2_fmtdesc *fb = b;
1087 if (fa->pixelformat == fb->pixelformat)
1090 return gst_v4l2_object_format_get_rank (fb) -
1091 gst_v4l2_object_format_get_rank (fa);
1094 /******************************************************
1095 * gst_v4l2_object_fill_format_list():
1096 * create list of supported capture formats
1097 * return value: TRUE on success, FALSE on error
1098 ******************************************************/
1100 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1101 enum v4l2_buf_type type)
1104 struct v4l2_fmtdesc *format;
1106 GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");
1108 /* format enumeration */
1110 format = g_new0 (struct v4l2_fmtdesc, 1);
1113 format->type = type;
1115 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1116 if (errno == EINVAL) {
1118 break; /* end of enumeration */
1124 GST_LOG_OBJECT (v4l2object->element, "index: %u", format->index);
1125 GST_LOG_OBJECT (v4l2object->element, "type: %d", format->type);
1126 GST_LOG_OBJECT (v4l2object->element, "flags: %08x", format->flags);
1127 GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
1128 format->description);
1129 GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
1130 GST_FOURCC_ARGS (format->pixelformat));
1132 /* sort formats according to our preference; we do this, because caps
1133 * are probed in the order the formats are in the list, and the order of
1134 * formats in the final probed caps matters for things like fixation */
1135 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1136 (GCompareFunc) format_cmp_func);
1139 #ifndef GST_DISABLE_GST_DEBUG
1143 GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
1144 for (l = v4l2object->formats; l != NULL; l = l->next) {
1147 GST_INFO_OBJECT (v4l2object->element,
1148 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1149 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1159 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1160 (_("Failed to enumerate possible video formats device '%s' can work with"), v4l2object->videodev), ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)", n, v4l2object->videodev, errno, g_strerror (errno)));
1167 * Get the list of supported capture formats, a list of
1168 * <code>struct v4l2_fmtdesc</code>.
1171 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1173 if (!v4l2object->formats) {
1175 /* check usual way */
1176 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1178 /* if our driver supports multi-planar
1179 * and if formats are still empty then we can workaround driver bug
1180 * by also looking up formats as if our device was not supporting
1182 if (!v4l2object->formats) {
1183 switch (v4l2object->type) {
1184 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1185 gst_v4l2_object_fill_format_list (v4l2object,
1186 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1189 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1190 gst_v4l2_object_fill_format_list (v4l2object,
1191 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1199 return v4l2object->formats;
1202 static GstVideoFormat
1203 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1205 GstVideoFormat format;
1208 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1209 format = GST_VIDEO_FORMAT_GRAY8;
1211 case V4L2_PIX_FMT_Y16:
1212 format = GST_VIDEO_FORMAT_GRAY16_LE;
1214 case V4L2_PIX_FMT_Y16_BE:
1215 format = GST_VIDEO_FORMAT_GRAY16_BE;
1217 case V4L2_PIX_FMT_XRGB555:
1218 case V4L2_PIX_FMT_RGB555:
1219 format = GST_VIDEO_FORMAT_RGB15;
1221 case V4L2_PIX_FMT_XRGB555X:
1222 case V4L2_PIX_FMT_RGB555X:
1223 format = GST_VIDEO_FORMAT_BGR15;
1225 case V4L2_PIX_FMT_RGB565:
1226 format = GST_VIDEO_FORMAT_RGB16;
1228 case V4L2_PIX_FMT_RGB24:
1229 format = GST_VIDEO_FORMAT_RGB;
1231 case V4L2_PIX_FMT_BGR24:
1232 format = GST_VIDEO_FORMAT_BGR;
1234 case V4L2_PIX_FMT_XRGB32:
1235 case V4L2_PIX_FMT_RGB32:
1236 format = GST_VIDEO_FORMAT_xRGB;
1238 case V4L2_PIX_FMT_XBGR32:
1239 case V4L2_PIX_FMT_BGR32:
1240 format = GST_VIDEO_FORMAT_BGRx;
1242 case V4L2_PIX_FMT_ABGR32:
1243 format = GST_VIDEO_FORMAT_BGRA;
1245 case V4L2_PIX_FMT_ARGB32:
1246 format = GST_VIDEO_FORMAT_ARGB;
1248 case V4L2_PIX_FMT_NV12:
1249 case V4L2_PIX_FMT_NV12M:
1250 format = GST_VIDEO_FORMAT_NV12;
1252 case V4L2_PIX_FMT_NV12MT:
1253 format = GST_VIDEO_FORMAT_NV12_64Z32;
1255 case V4L2_PIX_FMT_NV21:
1256 case V4L2_PIX_FMT_NV21M:
1257 format = GST_VIDEO_FORMAT_NV21;
1259 case V4L2_PIX_FMT_YVU410:
1260 format = GST_VIDEO_FORMAT_YVU9;
1262 case V4L2_PIX_FMT_YUV410:
1263 format = GST_VIDEO_FORMAT_YUV9;
1265 case V4L2_PIX_FMT_YUV420:
1266 case V4L2_PIX_FMT_YUV420M:
1267 format = GST_VIDEO_FORMAT_I420;
1269 case V4L2_PIX_FMT_YUYV:
1270 format = GST_VIDEO_FORMAT_YUY2;
1272 case V4L2_PIX_FMT_YVU420:
1273 format = GST_VIDEO_FORMAT_YV12;
1275 case V4L2_PIX_FMT_UYVY:
1276 format = GST_VIDEO_FORMAT_UYVY;
1278 case V4L2_PIX_FMT_YUV411P:
1279 format = GST_VIDEO_FORMAT_Y41B;
1281 case V4L2_PIX_FMT_YUV422P:
1282 format = GST_VIDEO_FORMAT_Y42B;
1284 case V4L2_PIX_FMT_YVYU:
1285 format = GST_VIDEO_FORMAT_YVYU;
1287 case V4L2_PIX_FMT_NV16:
1288 case V4L2_PIX_FMT_NV16M:
1289 format = GST_VIDEO_FORMAT_NV16;
1291 case V4L2_PIX_FMT_NV61:
1292 case V4L2_PIX_FMT_NV61M:
1293 format = GST_VIDEO_FORMAT_NV61;
1295 case V4L2_PIX_FMT_NV24:
1296 format = GST_VIDEO_FORMAT_NV24;
1299 format = GST_VIDEO_FORMAT_UNKNOWN;
1307 gst_v4l2_object_v4l2fourcc_is_yuv (guint32 fourcc)
1309 gboolean ret = FALSE;
1312 case V4L2_PIX_FMT_NV12:
1313 case V4L2_PIX_FMT_NV12M:
1314 case V4L2_PIX_FMT_NV12MT:
1315 case V4L2_PIX_FMT_NV21:
1316 case V4L2_PIX_FMT_NV21M:
1317 case V4L2_PIX_FMT_YVU410:
1318 case V4L2_PIX_FMT_YUV410:
1319 case V4L2_PIX_FMT_YUV420:
1320 case V4L2_PIX_FMT_YUV420M:
1321 case V4L2_PIX_FMT_YUYV:
1322 case V4L2_PIX_FMT_YVU420:
1323 case V4L2_PIX_FMT_UYVY:
1324 case V4L2_PIX_FMT_YUV411P:
1325 case V4L2_PIX_FMT_YUV422P:
1326 case V4L2_PIX_FMT_YVYU:
1327 case V4L2_PIX_FMT_NV16:
1328 case V4L2_PIX_FMT_NV16M:
1329 case V4L2_PIX_FMT_NV61:
1330 case V4L2_PIX_FMT_NV61M:
1331 case V4L2_PIX_FMT_NV24:
1341 static GstStructure *
1342 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1344 GstStructure *structure = NULL;
1347 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1348 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1349 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1350 structure = gst_structure_new_empty ("image/jpeg");
1352 case V4L2_PIX_FMT_MPEG1:
1353 structure = gst_structure_new ("video/mpeg",
1354 "mpegversion", G_TYPE_INT, 2, NULL);
1356 case V4L2_PIX_FMT_MPEG2:
1357 structure = gst_structure_new ("video/mpeg",
1358 "mpegversion", G_TYPE_INT, 2, NULL);
1360 case V4L2_PIX_FMT_MPEG4:
1361 case V4L2_PIX_FMT_XVID:
1362 structure = gst_structure_new ("video/mpeg",
1363 "mpegversion", G_TYPE_INT, 4, "systemstream",
1364 G_TYPE_BOOLEAN, FALSE, NULL);
1366 case V4L2_PIX_FMT_H263:
1367 structure = gst_structure_new ("video/x-h263",
1368 "variant", G_TYPE_STRING, "itu", NULL);
1370 case V4L2_PIX_FMT_H264: /* H.264 */
1371 structure = gst_structure_new ("video/x-h264",
1372 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1373 G_TYPE_STRING, "au", NULL);
1375 case V4L2_PIX_FMT_H264_NO_SC:
1376 structure = gst_structure_new ("video/x-h264",
1377 "stream-format", G_TYPE_STRING, "avc", "alignment",
1378 G_TYPE_STRING, "au", NULL);
1380 case V4L2_PIX_FMT_VC1_ANNEX_G:
1381 case V4L2_PIX_FMT_VC1_ANNEX_L:
1382 structure = gst_structure_new ("video/x-wmv",
1383 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1385 case V4L2_PIX_FMT_VP8:
1386 structure = gst_structure_new_empty ("video/x-vp8");
1388 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1389 case V4L2_PIX_FMT_Y16:
1390 case V4L2_PIX_FMT_Y16_BE:
1391 case V4L2_PIX_FMT_XRGB555:
1392 case V4L2_PIX_FMT_RGB555:
1393 case V4L2_PIX_FMT_XRGB555X:
1394 case V4L2_PIX_FMT_RGB555X:
1395 case V4L2_PIX_FMT_RGB565:
1396 case V4L2_PIX_FMT_RGB24:
1397 case V4L2_PIX_FMT_BGR24:
1398 case V4L2_PIX_FMT_RGB32:
1399 case V4L2_PIX_FMT_XRGB32:
1400 case V4L2_PIX_FMT_ARGB32:
1401 case V4L2_PIX_FMT_BGR32:
1402 case V4L2_PIX_FMT_XBGR32:
1403 case V4L2_PIX_FMT_ABGR32:
1404 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1405 case V4L2_PIX_FMT_NV12M:
1406 case V4L2_PIX_FMT_NV12MT:
1407 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1408 case V4L2_PIX_FMT_NV21M:
1409 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1410 case V4L2_PIX_FMT_NV16M:
1411 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1412 case V4L2_PIX_FMT_NV61M:
1413 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1414 case V4L2_PIX_FMT_YVU410:
1415 case V4L2_PIX_FMT_YUV410:
1416 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1417 case V4L2_PIX_FMT_YUV420M:
1418 case V4L2_PIX_FMT_YUYV:
1419 case V4L2_PIX_FMT_YVU420:
1420 case V4L2_PIX_FMT_UYVY:
1421 case V4L2_PIX_FMT_YUV422P:
1422 case V4L2_PIX_FMT_YVYU:
1423 case V4L2_PIX_FMT_YUV411P:{
1424 GstVideoFormat format;
1425 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1426 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1427 structure = gst_structure_new ("video/x-raw",
1428 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1431 case V4L2_PIX_FMT_DV:
1433 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1436 case V4L2_PIX_FMT_MPEG: /* MPEG */
1437 structure = gst_structure_new ("video/mpegts",
1438 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1440 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1442 case V4L2_PIX_FMT_SBGGR8:
1443 case V4L2_PIX_FMT_SGBRG8:
1444 case V4L2_PIX_FMT_SGRBG8:
1445 case V4L2_PIX_FMT_SRGGB8:
1446 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1447 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1448 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1449 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1450 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1452 case V4L2_PIX_FMT_SN9C10X:
1453 structure = gst_structure_new_empty ("video/x-sonix");
1455 case V4L2_PIX_FMT_PWC1:
1456 structure = gst_structure_new_empty ("video/x-pwc1");
1458 case V4L2_PIX_FMT_PWC2:
1459 structure = gst_structure_new_empty ("video/x-pwc2");
1461 case V4L2_PIX_FMT_RGB332:
1462 case V4L2_PIX_FMT_BGR666:
1463 case V4L2_PIX_FMT_ARGB555X:
1464 case V4L2_PIX_FMT_RGB565X:
1465 case V4L2_PIX_FMT_RGB444:
1466 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1467 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1468 case V4L2_PIX_FMT_Y4:
1469 case V4L2_PIX_FMT_Y6:
1470 case V4L2_PIX_FMT_Y10:
1471 case V4L2_PIX_FMT_Y12:
1472 case V4L2_PIX_FMT_Y10BPACK:
1473 case V4L2_PIX_FMT_YUV444:
1474 case V4L2_PIX_FMT_YUV555:
1475 case V4L2_PIX_FMT_YUV565:
1476 case V4L2_PIX_FMT_Y41P:
1477 case V4L2_PIX_FMT_YUV32:
1478 case V4L2_PIX_FMT_NV12MT_16X16:
1479 case V4L2_PIX_FMT_NV42:
1480 case V4L2_PIX_FMT_H264_MVC:
1482 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1483 fourcc, GST_FOURCC_ARGS (fourcc));
1491 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1493 GstStructure *template;
1496 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1498 if (template == NULL)
1501 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1502 if (gst_v4l2_formats[i].format != fourcc)
1505 if (gst_v4l2_formats[i].dimensions) {
1506 gst_structure_set (template,
1507 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1508 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1509 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1520 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1522 GstStructure *structure;
1526 caps = gst_caps_new_empty ();
1527 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1529 if ((gst_v4l2_formats[i].flags & flags) == 0)
1533 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1536 GstStructure *alt_s = NULL;
1538 if (gst_v4l2_formats[i].dimensions) {
1539 gst_structure_set (structure,
1540 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1541 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1542 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1545 switch (gst_v4l2_formats[i].format) {
1546 case V4L2_PIX_FMT_RGB32:
1547 alt_s = gst_structure_copy (structure);
1548 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1550 case V4L2_PIX_FMT_BGR32:
1551 alt_s = gst_structure_copy (structure);
1552 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1557 gst_caps_append_structure (caps, structure);
1560 gst_caps_append_structure (caps, alt_s);
1564 return gst_caps_simplify (caps);
1568 gst_v4l2_object_get_all_caps (void)
1570 static GstCaps *caps = NULL;
1573 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1575 return gst_caps_ref (caps);
1579 gst_v4l2_object_get_raw_caps (void)
1581 static GstCaps *caps = NULL;
1584 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1586 return gst_caps_ref (caps);
1590 gst_v4l2_object_get_codec_caps (void)
1592 static GstCaps *caps = NULL;
1595 caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1597 return gst_caps_ref (caps);
1600 /* collect data for the given caps
1601 * @caps: given input caps
1602 * @format: location for the v4l format
1603 * @w/@h: location for width and height
1604 * @fps_n/@fps_d: location for framerate
1605 * @size: location for expected size of the frame or 0 if unknown
1608 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1609 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1611 GstStructure *structure;
1612 guint32 fourcc = 0, fourcc_nc = 0;
1613 const gchar *mimetype;
1614 struct v4l2_fmtdesc *fmt = NULL;
1616 structure = gst_caps_get_structure (caps, 0);
1618 mimetype = gst_structure_get_name (structure);
1620 if (!gst_video_info_from_caps (info, caps))
1621 goto invalid_format;
1623 if (g_str_equal (mimetype, "video/x-raw")) {
1624 switch (GST_VIDEO_INFO_FORMAT (info)) {
1625 case GST_VIDEO_FORMAT_I420:
1626 fourcc = V4L2_PIX_FMT_YUV420;
1627 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1629 case GST_VIDEO_FORMAT_YUY2:
1630 fourcc = V4L2_PIX_FMT_YUYV;
1632 case GST_VIDEO_FORMAT_UYVY:
1633 fourcc = V4L2_PIX_FMT_UYVY;
1635 case GST_VIDEO_FORMAT_YV12:
1636 fourcc = V4L2_PIX_FMT_YVU420;
1638 case GST_VIDEO_FORMAT_Y41B:
1639 fourcc = V4L2_PIX_FMT_YUV411P;
1641 case GST_VIDEO_FORMAT_Y42B:
1642 fourcc = V4L2_PIX_FMT_YUV422P;
1644 case GST_VIDEO_FORMAT_NV12:
1645 fourcc = V4L2_PIX_FMT_NV12;
1646 fourcc_nc = V4L2_PIX_FMT_NV12M;
1648 case GST_VIDEO_FORMAT_NV12_64Z32:
1649 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1651 case GST_VIDEO_FORMAT_NV21:
1652 fourcc = V4L2_PIX_FMT_NV21;
1653 fourcc_nc = V4L2_PIX_FMT_NV21M;
1655 case GST_VIDEO_FORMAT_NV16:
1656 fourcc = V4L2_PIX_FMT_NV16;
1657 fourcc_nc = V4L2_PIX_FMT_NV16M;
1659 case GST_VIDEO_FORMAT_NV61:
1660 fourcc = V4L2_PIX_FMT_NV61;
1661 fourcc_nc = V4L2_PIX_FMT_NV61M;
1663 case GST_VIDEO_FORMAT_NV24:
1664 fourcc = V4L2_PIX_FMT_NV24;
1666 case GST_VIDEO_FORMAT_YVYU:
1667 fourcc = V4L2_PIX_FMT_YVYU;
1669 case GST_VIDEO_FORMAT_RGB15:
1670 fourcc = V4L2_PIX_FMT_RGB555;
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 fourcc = V4L2_PIX_FMT_SBGGR8;
1747 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1748 fourcc = V4L2_PIX_FMT_SN9C10X;
1749 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1750 fourcc = V4L2_PIX_FMT_PWC1;
1751 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1752 fourcc = V4L2_PIX_FMT_PWC2;
1757 /* Prefer the non-contiguous if supported */
1758 v4l2object->prefered_non_contiguous = TRUE;
1761 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
1762 else if (fourcc == 0)
1763 goto unhandled_format;
1766 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1767 v4l2object->prefered_non_contiguous = FALSE;
1771 goto unsupported_format;
1780 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1785 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1790 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1796 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1797 guint32 pixelformat, gint * width, gint * height);
1800 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1802 struct v4l2_cropcap cropcap;
1803 int num = 1, den = 1;
1805 if (!v4l2object->keep_aspect)
1808 if (v4l2object->par) {
1809 num = gst_value_get_fraction_numerator (v4l2object->par);
1810 den = gst_value_get_fraction_denominator (v4l2object->par);
1814 memset (&cropcap, 0, sizeof (cropcap));
1816 cropcap.type = v4l2object->type;
1817 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
1818 goto cropcap_failed;
1820 num = cropcap.pixelaspect.numerator;
1821 den = cropcap.pixelaspect.denominator;
1823 /* Ignore PAR that are 0/0 */
1828 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, num, den,
1833 if (errno != ENOTTY)
1834 GST_WARNING_OBJECT (v4l2object->element,
1835 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
1836 g_strerror (errno));
1840 /* returns TRUE if the value was changed in place, otherwise FALSE */
1842 gst_v4l2src_value_simplify (GValue * val)
1844 /* simplify list of one value to one value */
1845 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
1846 const GValue *list_val;
1847 GValue new_val = G_VALUE_INIT;
1849 list_val = gst_value_list_get_value (val, 0);
1850 g_value_init (&new_val, G_VALUE_TYPE (list_val));
1851 g_value_copy (list_val, &new_val);
1852 g_value_unset (val);
1861 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
1862 GstVideoInterlaceMode * interlace_mode)
1864 /* NB: If you add new return values, please fix mode_strings in
1865 * gst_v4l2_object_add_interlace_mode */
1867 case V4L2_FIELD_ANY:
1869 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
1871 case V4L2_FIELD_NONE:
1872 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1874 case V4L2_FIELD_INTERLACED:
1875 case V4L2_FIELD_INTERLACED_TB:
1876 case V4L2_FIELD_INTERLACED_BT:
1877 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1880 GST_ERROR ("Unknown enum v4l2_field %d", field);
1886 gst_v4l2_object_get_colorspace (enum v4l2_colorspace colorspace,
1887 enum v4l2_quantization range, enum v4l2_ycbcr_encoding matrix,
1888 enum v4l2_xfer_func transfer, GstVideoColorimetry * cinfo)
1890 gboolean ret = TRUE;
1892 /* First step, set the defaults for each primaries */
1893 switch (colorspace) {
1894 case V4L2_COLORSPACE_SMPTE170M:
1895 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1896 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1897 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1898 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1900 case V4L2_COLORSPACE_REC709:
1901 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1902 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1903 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1904 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1906 case V4L2_COLORSPACE_SRGB:
1907 case V4L2_COLORSPACE_JPEG:
1908 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1909 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1910 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
1911 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1913 case V4L2_COLORSPACE_ADOBERGB:
1914 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1915 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1916 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
1917 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
1919 case V4L2_COLORSPACE_BT2020:
1920 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1921 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1922 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1923 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
1925 case V4L2_COLORSPACE_SMPTE240M:
1926 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1927 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1928 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
1929 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
1931 case V4L2_COLORSPACE_470_SYSTEM_M:
1932 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1933 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1934 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1935 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
1937 case V4L2_COLORSPACE_470_SYSTEM_BG:
1938 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1939 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1940 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1941 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
1943 case V4L2_COLORSPACE_RAW:
1944 /* Explicitly unknown */
1945 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
1946 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
1947 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
1948 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
1951 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
1959 /* Second step, apply any custom variation */
1961 case V4L2_QUANTIZATION_FULL_RANGE:
1962 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1964 case V4L2_QUANTIZATION_LIM_RANGE:
1965 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1967 case V4L2_QUANTIZATION_DEFAULT:
1968 /* nothing, just use defaults for colorspace */
1971 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
1972 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
1977 case V4L2_YCBCR_ENC_XV601:
1978 case V4L2_YCBCR_ENC_SYCC:
1979 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
1981 case V4L2_YCBCR_ENC_601:
1982 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1984 case V4L2_YCBCR_ENC_XV709:
1985 GST_FIXME ("XV709 not defined, assuming 709");
1987 case V4L2_YCBCR_ENC_709:
1988 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1990 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
1991 GST_FIXME ("BT2020 with constant lumma is not defined, assuming BT2020");
1993 case V4L2_YCBCR_ENC_BT2020:
1994 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1996 case V4L2_YCBCR_ENC_SMPTE240M:
1997 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1999 case V4L2_YCBCR_ENC_DEFAULT:
2000 /* nothing, just use defaults for colorspace */
2003 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2004 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2009 case V4L2_XFER_FUNC_709:
2010 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2012 case V4L2_XFER_FUNC_SRGB:
2013 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2015 case V4L2_XFER_FUNC_ADOBERGB:
2016 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2018 case V4L2_XFER_FUNC_SMPTE240M:
2019 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2021 case V4L2_XFER_FUNC_NONE:
2022 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2024 case V4L2_XFER_FUNC_DEFAULT:
2025 /* nothing, just use defaults for colorspace */
2028 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2029 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2038 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2039 struct v4l2_format *try_fmt)
2041 int fd = v4l2object->video_fd;
2042 struct v4l2_format fmt;
2045 memcpy (&fmt, try_fmt, sizeof (fmt));
2046 r = v4l2_ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2048 if (r < 0 && errno == ENOTTY) {
2049 /* The driver might not implement TRY_FMT, in which case we will try
2051 if (GST_V4L2_IS_ACTIVE (v4l2object))
2054 memcpy (&fmt, try_fmt, sizeof (fmt));
2055 r = v4l2_ioctl (fd, VIDIOC_S_FMT, &fmt);
2057 memcpy (try_fmt, &fmt, sizeof (fmt));
2061 memcpy (try_fmt, &fmt, sizeof (fmt));
2062 GST_WARNING_OBJECT (v4l2object->element,
2063 "Unable to try format: %s", g_strerror (errno));
2069 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2070 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2072 struct v4l2_format fmt;
2073 GValue interlace_formats = { 0, };
2074 GstVideoInterlaceMode interlace_mode, prev = -1;
2076 const gchar *mode_strings[] = { "progressive",
2081 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2084 if (v4l2object->never_interlaced) {
2085 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2089 g_value_init (&interlace_formats, GST_TYPE_LIST);
2091 /* Try twice - once for NONE, once for INTERLACED. */
2092 memset (&fmt, 0, sizeof (fmt));
2093 fmt.type = v4l2object->type;
2094 fmt.fmt.pix.width = width;
2095 fmt.fmt.pix.height = height;
2096 fmt.fmt.pix.pixelformat = pixelformat;
2097 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2099 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2100 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2101 GValue interlace_enum = { 0, };
2102 g_value_init (&interlace_enum, G_TYPE_STRING);
2103 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2104 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2105 prev = interlace_mode;
2108 memset (&fmt, 0, sizeof (fmt));
2109 fmt.type = v4l2object->type;
2110 fmt.fmt.pix.width = width;
2111 fmt.fmt.pix.height = height;
2112 fmt.fmt.pix.pixelformat = pixelformat;
2113 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2115 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2116 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode) &&
2117 prev != interlace_mode) {
2118 GValue interlace_enum = { 0, };
2119 g_value_init (&interlace_enum, G_TYPE_STRING);
2120 g_value_set_string (&interlace_enum, mode_strings[interlace_mode]);
2121 gst_value_list_append_and_take_value (&interlace_formats, &interlace_enum);
2124 gst_v4l2src_value_simplify (&interlace_formats);
2125 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2130 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2131 guint32 width, guint32 height, guint32 pixelformat)
2133 struct v4l2_format fmt;
2134 GValue colorimetry = G_VALUE_INIT;
2135 GstVideoColorimetry cinfo;
2137 /* Don't expose colorimetry unless this is a YUV format */
2138 if (!gst_v4l2_object_v4l2fourcc_is_yuv (pixelformat))
2141 memset (&fmt, 0, sizeof (fmt));
2142 fmt.type = v4l2object->type;
2143 fmt.fmt.pix.width = width;
2144 fmt.fmt.pix.height = height;
2145 fmt.fmt.pix.pixelformat = pixelformat;
2147 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2148 enum v4l2_colorspace colorspace;
2149 enum v4l2_quantization range;
2150 enum v4l2_ycbcr_encoding matrix;
2151 enum v4l2_xfer_func transfer;
2153 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
2154 colorspace = fmt.fmt.pix_mp.colorspace;
2155 range = fmt.fmt.pix_mp.quantization;
2156 matrix = fmt.fmt.pix_mp.ycbcr_enc;
2157 transfer = fmt.fmt.pix_mp.xfer_func;
2159 colorspace = fmt.fmt.pix.colorspace;
2160 range = fmt.fmt.pix.quantization;
2161 matrix = fmt.fmt.pix.ycbcr_enc;
2162 transfer = fmt.fmt.pix.xfer_func;
2165 if (gst_v4l2_object_get_colorspace (colorspace, range, matrix, transfer,
2167 g_value_init (&colorimetry, G_TYPE_STRING);
2168 g_value_take_string (&colorimetry,
2169 gst_video_colorimetry_to_string (&cinfo));
2170 gst_structure_take_value (s, "colorimetry", &colorimetry);
2175 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2176 static GstStructure *
2177 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2178 guint32 pixelformat,
2179 guint32 width, guint32 height, const GstStructure * template)
2181 gint fd = v4l2object->video_fd;
2182 struct v4l2_frmivalenum ival;
2185 GValue rates = { 0, };
2187 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2189 ival.pixel_format = pixelformat;
2191 ival.height = height;
2193 GST_LOG_OBJECT (v4l2object->element,
2194 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2195 GST_FOURCC_ARGS (pixelformat));
2197 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2198 * fraction to get framerate */
2199 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2200 goto enum_frameintervals_failed;
2202 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2203 GValue rate = { 0, };
2205 g_value_init (&rates, GST_TYPE_LIST);
2206 g_value_init (&rate, GST_TYPE_FRACTION);
2209 num = ival.discrete.numerator;
2210 denom = ival.discrete.denominator;
2212 if (num > G_MAXINT || denom > G_MAXINT) {
2213 /* let us hope we don't get here... */
2218 GST_LOG_OBJECT (v4l2object->element, "adding discrete framerate: %d/%d",
2221 /* swap to get the framerate */
2222 gst_value_set_fraction (&rate, denom, num);
2223 gst_value_list_append_value (&rates, &rate);
2226 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2227 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2228 GValue min = { 0, };
2229 GValue step = { 0, };
2230 GValue max = { 0, };
2231 gboolean added = FALSE;
2232 guint32 minnum, mindenom;
2233 guint32 maxnum, maxdenom;
2235 g_value_init (&rates, GST_TYPE_LIST);
2237 g_value_init (&min, GST_TYPE_FRACTION);
2238 g_value_init (&step, GST_TYPE_FRACTION);
2239 g_value_init (&max, GST_TYPE_FRACTION);
2242 minnum = ival.stepwise.min.numerator;
2243 mindenom = ival.stepwise.min.denominator;
2244 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2248 GST_LOG_OBJECT (v4l2object->element, "stepwise min frame interval: %d/%d",
2250 gst_value_set_fraction (&min, minnum, mindenom);
2253 maxnum = ival.stepwise.max.numerator;
2254 maxdenom = ival.stepwise.max.denominator;
2255 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2260 GST_LOG_OBJECT (v4l2object->element, "stepwise max frame interval: %d/%d",
2262 gst_value_set_fraction (&max, maxnum, maxdenom);
2265 num = ival.stepwise.step.numerator;
2266 denom = ival.stepwise.step.denominator;
2267 if (num > G_MAXINT || denom > G_MAXINT) {
2272 if (num == 0 || denom == 0) {
2273 /* in this case we have a wrong fraction or no step, set the step to max
2274 * so that we only add the min value in the loop below */
2279 /* since we only have gst_value_fraction_subtract and not add, negate the
2281 GST_LOG_OBJECT (v4l2object->element, "stepwise step frame interval: %d/%d",
2283 gst_value_set_fraction (&step, -num, denom);
2285 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2286 GValue rate = { 0, };
2288 num = gst_value_get_fraction_numerator (&min);
2289 denom = gst_value_get_fraction_denominator (&min);
2290 GST_LOG_OBJECT (v4l2object->element, "adding stepwise framerate: %d/%d",
2293 /* invert to get the framerate */
2294 g_value_init (&rate, GST_TYPE_FRACTION);
2295 gst_value_set_fraction (&rate, denom, num);
2296 gst_value_list_append_value (&rates, &rate);
2299 /* we're actually adding because step was negated above. This is because
2300 * there is no _add function... */
2301 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2302 GST_WARNING_OBJECT (v4l2object->element, "could not step fraction!");
2307 /* no range was added, leave the default range from the template */
2308 GST_WARNING_OBJECT (v4l2object->element,
2309 "no range added, leaving default");
2310 g_value_unset (&rates);
2312 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2313 guint32 maxnum, maxdenom;
2315 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2317 num = ival.stepwise.min.numerator;
2318 denom = ival.stepwise.min.denominator;
2319 if (num > G_MAXINT || denom > G_MAXINT) {
2324 maxnum = ival.stepwise.max.numerator;
2325 maxdenom = ival.stepwise.max.denominator;
2326 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2331 GST_LOG_OBJECT (v4l2object->element,
2332 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2335 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2341 s = gst_structure_copy (template);
2342 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2343 "height", G_TYPE_INT, (gint) height, NULL);
2344 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2345 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2347 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2349 if (G_IS_VALUE (&rates)) {
2350 gst_v4l2src_value_simplify (&rates);
2351 /* only change the framerate on the template when we have a valid probed new
2353 gst_structure_take_value (s, "framerate", &rates);
2354 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2355 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2356 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2362 enum_frameintervals_failed:
2364 GST_DEBUG_OBJECT (v4l2object->element,
2365 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2366 GST_FOURCC_ARGS (pixelformat), width, height);
2371 /* I don't see how this is actually an error, we ignore the format then */
2372 GST_WARNING_OBJECT (v4l2object->element,
2373 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2374 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2380 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2384 gst_structure_get_int (s1, "width", &w1);
2385 gst_structure_get_int (s1, "height", &h1);
2386 gst_structure_get_int (s2, "width", &w2);
2387 gst_structure_get_int (s2, "height", &h2);
2389 /* I think it's safe to assume that this won't overflow for a while */
2390 return ((w2 * h2) - (w1 * h1));
2394 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2395 guint32 format, GstCaps * caps, GstStructure * s)
2397 GstStructure *alt_s = NULL;
2399 /* Encoded stream on output buffer need to be parsed */
2400 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2401 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2404 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2405 if (format == gst_v4l2_formats[i].format &&
2406 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2407 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2408 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2414 if (v4l2object->has_alpha_component &&
2415 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2416 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2418 case V4L2_PIX_FMT_RGB32:
2419 alt_s = gst_structure_copy (s);
2420 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2422 case V4L2_PIX_FMT_BGR32:
2423 alt_s = gst_structure_copy (s);
2424 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2431 gst_caps_append_structure (caps, s);
2434 gst_caps_append_structure (caps, alt_s);
2438 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2439 guint32 pixelformat, const GstStructure * template)
2441 GstCaps *ret = gst_caps_new_empty ();
2443 gint fd = v4l2object->video_fd;
2444 struct v4l2_frmsizeenum size;
2445 GList *results = NULL;
2448 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2449 gst_caps_append_structure (ret, gst_structure_copy (template));
2453 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2455 size.pixel_format = pixelformat;
2457 GST_DEBUG_OBJECT (v4l2object->element,
2458 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2459 GST_FOURCC_ARGS (pixelformat));
2461 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2462 goto enum_framesizes_failed;
2464 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2466 GST_LOG_OBJECT (v4l2object->element, "got discrete frame size %dx%d",
2467 size.discrete.width, size.discrete.height);
2469 w = MIN (size.discrete.width, G_MAXINT);
2470 h = MIN (size.discrete.height, G_MAXINT);
2474 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2475 pixelformat, w, h, template);
2478 results = g_list_prepend (results, tmp);
2482 } while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2483 GST_DEBUG_OBJECT (v4l2object->element,
2484 "done iterating discrete frame sizes");
2485 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2486 guint32 maxw, maxh, step_w, step_h;
2488 GST_DEBUG_OBJECT (v4l2object->element, "we have stepwise frame sizes:");
2489 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2490 size.stepwise.min_width);
2491 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2492 size.stepwise.min_height);
2493 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2494 size.stepwise.max_width);
2495 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2496 size.stepwise.max_height);
2497 GST_DEBUG_OBJECT (v4l2object->element, "step width: %d",
2498 size.stepwise.step_width);
2499 GST_DEBUG_OBJECT (v4l2object->element, "step height: %d",
2500 size.stepwise.step_height);
2502 w = MAX (size.stepwise.min_width, 1);
2503 h = MAX (size.stepwise.min_height, 1);
2504 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2505 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2507 step_w = MAX (size.stepwise.step_width, 1);
2508 step_h = MAX (size.stepwise.step_height, 1);
2510 /* FIXME: check for sanity and that min/max are multiples of the steps */
2512 /* we only query details for the max width/height since it's likely the
2513 * most restricted if there are any resolution-dependent restrictions */
2514 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2515 pixelformat, maxw, maxh, template);
2518 GValue step_range = G_VALUE_INIT;
2520 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2521 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2522 gst_structure_set_value (tmp, "width", &step_range);
2524 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2525 gst_structure_take_value (tmp, "height", &step_range);
2527 /* no point using the results list here, since there's only one struct */
2528 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2530 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2533 GST_DEBUG_OBJECT (v4l2object->element, "we have continuous frame sizes:");
2534 GST_DEBUG_OBJECT (v4l2object->element, "min width: %d",
2535 size.stepwise.min_width);
2536 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2537 size.stepwise.min_height);
2538 GST_DEBUG_OBJECT (v4l2object->element, "max width: %d",
2539 size.stepwise.max_width);
2540 GST_DEBUG_OBJECT (v4l2object->element, "min height: %d",
2541 size.stepwise.max_height);
2543 w = MAX (size.stepwise.min_width, 1);
2544 h = MAX (size.stepwise.min_height, 1);
2545 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2546 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2549 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2552 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2553 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2556 /* no point using the results list here, since there's only one struct */
2557 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2563 /* we use an intermediary list to store and then sort the results of the
2564 * probing because we can't make any assumptions about the order in which
2565 * the driver will give us the sizes, but we want the final caps to contain
2566 * the results starting with the highest resolution and having the lowest
2567 * resolution last, since order in caps matters for things like fixation. */
2568 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2569 while (results != NULL) {
2570 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
2572 results = g_list_delete_link (results, results);
2575 if (gst_caps_is_empty (ret))
2576 goto enum_framesizes_no_results;
2581 enum_framesizes_failed:
2583 /* I don't see how this is actually an error */
2584 GST_DEBUG_OBJECT (v4l2object->element,
2585 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2586 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2587 goto default_frame_sizes;
2589 enum_framesizes_no_results:
2591 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2592 * question doesn't actually support it yet */
2593 GST_DEBUG_OBJECT (v4l2object->element,
2594 "No results for pixelformat %" GST_FOURCC_FORMAT
2595 " enumerating frame sizes, trying fallback",
2596 GST_FOURCC_ARGS (pixelformat));
2597 goto default_frame_sizes;
2601 GST_WARNING_OBJECT (v4l2object->element,
2602 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2603 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2604 goto default_frame_sizes;
2607 default_frame_sizes:
2609 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2611 /* This code is for Linux < 2.6.19 */
2613 max_w = max_h = GST_V4L2_MAX_SIZE;
2614 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2616 GST_WARNING_OBJECT (v4l2object->element,
2617 "Could not probe minimum capture size for pixelformat %"
2618 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2620 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2622 GST_WARNING_OBJECT (v4l2object->element,
2623 "Could not probe maximum capture size for pixelformat %"
2624 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2627 /* Since we can't get framerate directly, try to use the current norm */
2628 if (v4l2object->tv_norm && v4l2object->norms) {
2630 GstTunerNorm *norm = NULL;
2631 GstTunerNorm *current =
2632 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2634 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2635 norm = (GstTunerNorm *) norms->data;
2636 if (!strcmp (norm->label, current->label))
2639 /* If it's possible, set framerate to that (discrete) value */
2641 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2642 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2646 tmp = gst_structure_copy (template);
2648 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2650 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2651 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2652 /* if norm can't be used, copy the template framerate */
2653 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2658 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2660 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2663 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2665 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2667 /* We could consider setting interlace mode from min and max. */
2668 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
2670 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2671 /* We could consider to check colorspace for min too, in case it depends on
2672 * the size. But in this case, min and max could not be enough */
2673 gst_v4l2_object_add_colorspace (v4l2object, tmp, max_w, max_h, pixelformat);
2675 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2681 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2682 guint32 pixelformat, gint * width, gint * height)
2684 struct v4l2_format fmt;
2685 gboolean ret = FALSE;
2686 GstVideoInterlaceMode interlace_mode;
2688 g_return_val_if_fail (width != NULL, FALSE);
2689 g_return_val_if_fail (height != NULL, FALSE);
2691 GST_LOG_OBJECT (v4l2object->element,
2692 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2693 *width, *height, GST_FOURCC_ARGS (pixelformat));
2695 memset (&fmt, 0, sizeof (struct v4l2_format));
2697 /* get size delimiters */
2698 memset (&fmt, 0, sizeof (fmt));
2699 fmt.type = v4l2object->type;
2700 fmt.fmt.pix.width = *width;
2701 fmt.fmt.pix.height = *height;
2702 fmt.fmt.pix.pixelformat = pixelformat;
2703 fmt.fmt.pix.field = V4L2_FIELD_ANY;
2705 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
2708 GST_LOG_OBJECT (v4l2object->element,
2709 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2711 *width = fmt.fmt.pix.width;
2712 *height = fmt.fmt.pix.height;
2714 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2715 GST_WARNING_OBJECT (v4l2object->element,
2716 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
2717 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
2725 GST_WARNING_OBJECT (v4l2object->element,
2726 "Unable to try format: %s", g_strerror (errno));
2733 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2737 GST_DEBUG_OBJECT (v4l2object->element, "initializing the %s system",
2738 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
2740 GST_V4L2_CHECK_OPEN (v4l2object);
2741 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2743 /* find transport */
2744 mode = v4l2object->req_mode;
2746 if (v4l2object->vcap.capabilities & V4L2_CAP_READWRITE) {
2747 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2748 mode = GST_V4L2_IO_RW;
2749 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2750 goto method_not_supported;
2752 if (v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
2753 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2754 mode = GST_V4L2_IO_MMAP;
2755 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP)
2756 goto method_not_supported;
2758 /* if still no transport selected, error out */
2759 if (mode == GST_V4L2_IO_AUTO)
2760 goto no_supported_capture_method;
2762 GST_INFO_OBJECT (v4l2object->element, "accessing buffers via mode %d", mode);
2763 v4l2object->mode = mode;
2765 /* If min_buffers is not set, the driver either does not support the control or
2766 it has not been asked yet via propose_allocation/decide_allocation. */
2767 if (!v4l2object->min_buffers)
2768 gst_v4l2_get_driver_min_buffers (v4l2object);
2770 /* Map the buffers */
2771 GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
2773 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2774 goto buffer_pool_new_failed;
2776 GST_V4L2_SET_ACTIVE (v4l2object);
2781 buffer_pool_new_failed:
2783 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2784 (_("Could not map buffers from device '%s'"),
2785 v4l2object->videodev),
2786 ("Failed to create buffer pool: %s", g_strerror (errno)));
2789 method_not_supported:
2791 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2792 (_("The driver of device '%s' does not support the IO method %d"),
2793 v4l2object->videodev, mode), (NULL));
2796 no_supported_capture_method:
2798 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2799 (_("The driver of device '%s' does not support any known IO "
2800 "method."), v4l2object->videodev), (NULL));
2806 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
2807 gint plane, gint stride)
2809 const GstVideoFormatInfo *finfo = info->finfo;
2811 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2812 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
2815 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
2816 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2817 tile_height = 1 << hs;
2819 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
2820 info->height + align->padding_top + align->padding_bottom);
2821 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
2823 x_tiles = stride >> ws;
2824 y_tiles = padded_height >> hs;
2825 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
2827 info->stride[plane] = stride;
2832 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
2833 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
2835 const GstVideoFormatInfo *finfo = info->finfo;
2836 gint i, estride, padded_height;
2839 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
2841 padded_height = info->height + align->padding_top + align->padding_bottom;
2843 for (i = 0; i < finfo->n_planes; i++) {
2844 estride = gst_v4l2_object_extrapolate_stride (finfo, i, stride);
2846 gst_v4l2_object_set_stride (info, align, i, estride);
2848 info->offset[i] = offs;
2850 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
2852 GST_DEBUG_OBJECT (v4l2object->element,
2853 "Extrapolated for plane %d with base stride %d: "
2854 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
2860 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
2861 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
2862 GstVideoInfo * info, GstVideoAlignment * align)
2864 const GstVideoFormatInfo *finfo = info->finfo;
2865 gboolean standard_stride = TRUE;
2866 gint stride, padded_width, padded_height, i;
2868 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
2869 v4l2object->n_v4l2_planes = 1;
2870 info->size = format->fmt.pix.sizeimage;
2874 /* adjust right padding */
2875 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2876 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
2878 stride = format->fmt.pix.bytesperline;
2880 padded_width = stride / GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
2882 if (padded_width < format->fmt.pix.width)
2883 GST_WARNING_OBJECT (v4l2object->element,
2884 "Driver bug detected, stride is too small for the width");
2886 align->padding_right = padded_width - info->width - align->padding_left;
2888 /* adjust bottom padding */
2889 padded_height = format->fmt.pix.height;
2891 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2892 guint hs, tile_height;
2894 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2895 tile_height = 1 << hs;
2897 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
2900 align->padding_bottom = padded_height - info->height - align->padding_top;
2902 /* setup the strides and offset */
2903 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
2904 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
2906 /* figure out the frame layout */
2907 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
2909 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
2910 stride = pix_mp->plane_fmt[i].bytesperline;
2912 if (info->stride[i] != stride)
2913 standard_stride = FALSE;
2915 gst_v4l2_object_set_stride (info, align, i, stride);
2916 info->offset[i] = info->size;
2917 info->size += pix_mp->plane_fmt[i].sizeimage;
2920 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
2921 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
2922 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
2923 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
2926 /* only one plane in non-MPLANE mode */
2927 v4l2object->n_v4l2_planes = 1;
2928 info->size = format->fmt.pix.sizeimage;
2929 stride = format->fmt.pix.bytesperline;
2931 if (info->stride[0] != stride)
2932 standard_stride = FALSE;
2934 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
2937 /* adjust the offset to take into account left and top */
2938 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2939 if ((align->padding_left + align->padding_top) > 0)
2940 GST_WARNING_OBJECT (v4l2object->element,
2941 "Left and top padding is not permitted for tiled formats");
2943 for (i = 0; i < finfo->n_planes; i++) {
2946 /* FIXME we assume plane as component as this is true for all supported
2947 * format we support. */
2949 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left);
2950 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top);
2952 info->offset[i] += (vedge * info->stride[i]) +
2953 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i));
2958 GST_DEBUG_OBJECT (v4l2object->element, "Got sizeimage %" G_GSIZE_FORMAT,
2961 /* to avoid copies we need video meta if top or left padding */
2962 v4l2object->need_video_meta =
2963 ((align->padding_top + align->padding_left) != 0);
2965 /* ... or if stride is non "standard" */
2966 if (!standard_stride)
2967 v4l2object->need_video_meta = TRUE;
2969 /* ... or also video meta if we use multiple, non-contiguous, planes */
2970 if (v4l2object->n_v4l2_planes > 1)
2971 v4l2object->need_video_meta = TRUE;
2973 v4l2object->info = *info;
2974 v4l2object->align = *align;
2975 v4l2object->format = *format;
2976 v4l2object->fmtdesc = fmtdesc;
2978 /* if we have a framerate pre-calculate duration */
2979 if (info->fps_n > 0 && info->fps_d > 0) {
2980 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
2983 v4l2object->duration = GST_CLOCK_TIME_NONE;
2988 gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
2989 gint plane, gint stride)
2993 switch (finfo->format) {
2994 case GST_VIDEO_FORMAT_NV12:
2995 case GST_VIDEO_FORMAT_NV12_64Z32:
2996 case GST_VIDEO_FORMAT_NV21:
2997 case GST_VIDEO_FORMAT_NV16:
2998 case GST_VIDEO_FORMAT_NV61:
2999 case GST_VIDEO_FORMAT_NV24:
3000 estride = (plane == 0 ? 1 : 2) *
3001 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3004 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3012 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3015 gint fd = v4l2object->video_fd;
3016 struct v4l2_format format;
3017 struct v4l2_streamparm streamparm;
3018 enum v4l2_field field;
3019 guint32 pixelformat;
3020 struct v4l2_fmtdesc *fmtdesc;
3022 GstVideoAlignment align;
3023 gint width, height, fps_n, fps_d;
3027 enum v4l2_colorspace colorspace = 0;
3028 enum v4l2_quantization range = 0;
3029 enum v4l2_ycbcr_encoding matrix = 0;
3030 enum v4l2_xfer_func transfer = 0;
3032 GST_V4L2_CHECK_OPEN (v4l2object);
3034 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3036 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3038 gst_video_info_init (&info);
3039 gst_video_alignment_reset (&align);
3041 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3044 pixelformat = fmtdesc->pixelformat;
3045 width = GST_VIDEO_INFO_WIDTH (&info);
3046 height = GST_VIDEO_INFO_HEIGHT (&info);
3047 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3048 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3050 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3051 * or if contiguous is prefered */
3052 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3053 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3056 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
3057 GST_DEBUG_OBJECT (v4l2object->element, "interlaced video");
3058 /* ideally we would differentiate between types of interlaced video
3059 * but there is not sufficient information in the caps..
3061 field = V4L2_FIELD_INTERLACED;
3063 GST_DEBUG_OBJECT (v4l2object->element, "progressive video");
3064 field = V4L2_FIELD_NONE;
3067 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3068 /* We first pick th main colorspace from the primaries */
3069 switch (info.colorimetry.primaries) {
3070 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3071 /* There is two colorspaces using these primaries, use the range to
3073 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3074 colorspace = V4L2_COLORSPACE_REC709;
3076 colorspace = V4L2_COLORSPACE_SRGB;
3078 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3079 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3081 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3082 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3084 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3085 colorspace = V4L2_COLORSPACE_SMPTE170M;
3087 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3088 colorspace = V4L2_COLORSPACE_SMPTE240M;
3091 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3092 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3093 /* We don't know, we will guess */
3097 GST_WARNING_OBJECT (v4l2object->element,
3098 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3102 switch (info.colorimetry.range) {
3103 case GST_VIDEO_COLOR_RANGE_0_255:
3104 range = V4L2_QUANTIZATION_FULL_RANGE;
3106 case GST_VIDEO_COLOR_RANGE_16_235:
3107 range = V4L2_QUANTIZATION_LIM_RANGE;
3109 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3110 /* We let the driver pick a default one */
3113 GST_WARNING_OBJECT (v4l2object->element,
3114 "Unknown colorimetry range %d", info.colorimetry.range);
3118 switch (info.colorimetry.matrix) {
3119 case GST_VIDEO_COLOR_MATRIX_RGB:
3120 /* Unspecified, leave to default */
3122 /* FCC is about the same as BT601 with less digit */
3123 case GST_VIDEO_COLOR_MATRIX_FCC:
3124 case GST_VIDEO_COLOR_MATRIX_BT601:
3125 matrix = V4L2_YCBCR_ENC_601;
3127 case GST_VIDEO_COLOR_MATRIX_BT709:
3128 matrix = V4L2_YCBCR_ENC_709;
3130 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3131 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3133 case GST_VIDEO_COLOR_MATRIX_BT2020:
3134 matrix = V4L2_YCBCR_ENC_BT2020;
3136 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3137 /* We let the driver pick a default one */
3140 GST_WARNING_OBJECT (v4l2object->element,
3141 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3145 switch (info.colorimetry.transfer) {
3146 case GST_VIDEO_TRANSFER_GAMMA18:
3147 case GST_VIDEO_TRANSFER_GAMMA20:
3148 case GST_VIDEO_TRANSFER_GAMMA22:
3149 case GST_VIDEO_TRANSFER_GAMMA28:
3150 GST_WARNING_OBJECT (v4l2object->element,
3151 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3153 case GST_VIDEO_TRANSFER_GAMMA10:
3154 transfer = V4L2_XFER_FUNC_NONE;
3156 case GST_VIDEO_TRANSFER_BT709:
3157 transfer = V4L2_XFER_FUNC_709;
3159 case GST_VIDEO_TRANSFER_SMPTE240M:
3160 transfer = V4L2_XFER_FUNC_SMPTE240M;
3162 case GST_VIDEO_TRANSFER_SRGB:
3163 transfer = V4L2_XFER_FUNC_SRGB;
3165 case GST_VIDEO_TRANSFER_LOG100:
3166 case GST_VIDEO_TRANSFER_LOG316:
3167 GST_WARNING_OBJECT (v4l2object->element,
3168 "LOG 100, 316 transfer functions not supported");
3169 /* FIXME No known sensible default, maybe AdobeRGB ? */
3171 case GST_VIDEO_TRANSFER_UNKNOWN:
3172 /* We let the driver pick a default one */
3175 GST_WARNING_OBJECT (v4l2object->element,
3176 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
3180 if (colorspace == 0) {
3181 /* Try to guess colorspace according to pixelformat and size */
3182 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3183 /* SD streams likely use SMPTE170M and HD streams REC709 */
3184 if (width <= 720 && height <= 576)
3185 colorspace = V4L2_COLORSPACE_SMPTE170M;
3187 colorspace = V4L2_COLORSPACE_REC709;
3188 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3189 colorspace = V4L2_COLORSPACE_SRGB;
3190 transfer = V4L2_XFER_FUNC_NONE;
3195 GST_DEBUG_OBJECT (v4l2object->element, "Desired format %dx%d, format "
3196 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3197 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3199 memset (&format, 0x00, sizeof (struct v4l2_format));
3200 format.type = v4l2object->type;
3203 format.type = v4l2object->type;
3204 format.fmt.pix_mp.pixelformat = pixelformat;
3205 format.fmt.pix_mp.width = width;
3206 format.fmt.pix_mp.height = height;
3207 format.fmt.pix_mp.field = field;
3208 format.fmt.pix_mp.num_planes = n_v4l_planes;
3210 /* try to ask our prefered stride but it's not a failure if not
3212 for (i = 0; i < n_v4l_planes; i++) {
3213 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3215 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3216 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3217 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3219 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3222 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3223 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3225 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3227 format.type = v4l2object->type;
3228 format.fmt.pix.width = width;
3229 format.fmt.pix.height = height;
3230 format.fmt.pix.pixelformat = pixelformat;
3231 format.fmt.pix.field = field;
3233 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3234 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3235 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3237 /* try to ask our prefered stride */
3238 format.fmt.pix.bytesperline = stride;
3240 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3241 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3244 GST_DEBUG_OBJECT (v4l2object->element, "Desired format is %dx%d, format "
3245 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3246 format.fmt.pix_mp.height,
3247 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3248 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3250 #ifndef GST_DISABLE_GST_DEBUG
3252 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3253 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3254 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3256 GST_DEBUG_OBJECT (v4l2object->element, " stride %d",
3257 format.fmt.pix.bytesperline);
3261 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type)) {
3263 format.fmt.pix_mp.colorspace = colorspace;
3264 format.fmt.pix_mp.quantization = range;
3265 format.fmt.pix_mp.ycbcr_enc = matrix;
3266 format.fmt.pix_mp.xfer_func = transfer;
3268 format.fmt.pix.colorspace = colorspace;
3269 format.fmt.pix.quantization = range;
3270 format.fmt.pix.ycbcr_enc = matrix;
3271 format.fmt.pix.xfer_func = transfer;
3274 GST_DEBUG_OBJECT (v4l2object->element, "Desired colorspace is %d:%d:%d:%d",
3275 colorspace, range, matrix, transfer);
3279 if (v4l2_ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3280 goto try_fmt_failed;
3282 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3283 goto set_fmt_failed;
3286 GST_DEBUG_OBJECT (v4l2object->element, "Got format of %dx%d, format "
3287 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d",
3288 format.fmt.pix.width, format.fmt.pix_mp.height,
3289 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3290 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3291 is_mplane ? format.fmt.pix_mp.colorspace : format.fmt.pix.colorspace);
3293 #ifndef GST_DISABLE_GST_DEBUG
3295 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3296 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3297 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3298 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3300 GST_DEBUG_OBJECT (v4l2object->element, " stride %d, sizeimage %d",
3301 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3305 if (format.fmt.pix.pixelformat != pixelformat)
3306 goto invalid_pixelformat;
3308 /* Only negotiate size with raw data.
3309 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3310 * in ASF mode for example, there is also not reason for a driver to
3311 * change the size. */
3312 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3313 /* We can crop larger images */
3314 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3315 goto invalid_dimensions;
3317 /* Note, this will be adjusted if upstream has non-centered cropping. */
3318 align.padding_top = 0;
3319 align.padding_bottom = format.fmt.pix.height - height;
3320 align.padding_left = 0;
3321 align.padding_right = format.fmt.pix.width - width;
3324 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3325 goto invalid_planes;
3327 if (try_only) /* good enough for trying only */
3330 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
3331 struct v4l2_control ctl = { 0, };
3332 ctl.id = V4L2_CID_ALPHA_COMPONENT;
3335 if (v4l2_ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
3336 GST_WARNING_OBJECT (v4l2object->element,
3337 "Failed to set alpha component value");
3340 /* Is there a reason we require the caller to always specify a framerate? */
3341 GST_DEBUG_OBJECT (v4l2object->element, "Desired framerate: %u/%u", fps_n,
3344 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
3345 streamparm.type = v4l2object->type;
3347 if (v4l2_ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
3348 goto get_parm_failed;
3350 GST_VIDEO_INFO_FPS_N (&info) =
3351 streamparm.parm.capture.timeperframe.denominator;
3352 GST_VIDEO_INFO_FPS_D (&info) = streamparm.parm.capture.timeperframe.numerator;
3354 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
3355 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3356 GST_DEBUG_OBJECT (v4l2object->element, "Got framerate: %u/%u",
3357 streamparm.parm.capture.timeperframe.denominator,
3358 streamparm.parm.capture.timeperframe.numerator);
3360 /* We used to skip frame rate setup if the camera was already setup
3361 * with the requested frame rate. This breaks some cameras though,
3362 * causing them to not output data (several models of Thinkpad cameras
3363 * have this problem at least).
3364 * So, don't skip. */
3365 GST_LOG_OBJECT (v4l2object->element, "Setting framerate to %u/%u", fps_n,
3367 /* We want to change the frame rate, so check whether we can. Some cheap USB
3368 * cameras don't have the capability */
3369 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3370 GST_DEBUG_OBJECT (v4l2object->element,
3371 "Not setting framerate (not supported)");
3375 /* Note: V4L2 wants the frame interval, we have the frame rate */
3376 streamparm.parm.capture.timeperframe.numerator = fps_d;
3377 streamparm.parm.capture.timeperframe.denominator = fps_n;
3379 /* some cheap USB cam's won't accept any change */
3380 if (v4l2_ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3381 goto set_parm_failed;
3383 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
3384 streamparm.parm.capture.timeperframe.denominator > 0) {
3385 /* get new values */
3386 fps_d = streamparm.parm.capture.timeperframe.numerator;
3387 fps_n = streamparm.parm.capture.timeperframe.denominator;
3389 GST_INFO_OBJECT (v4l2object->element, "Set framerate to %u/%u", fps_n,
3392 /* fix v4l2 capture driver to provide framerate values */
3393 GST_WARNING_OBJECT (v4l2object->element,
3394 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
3397 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3398 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3402 /* add boolean return, so we can fail on drivers bugs */
3403 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
3405 /* now configure the pool */
3406 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
3414 GST_DEBUG_OBJECT (v4l2object->element, "can't parse caps %" GST_PTR_FORMAT,
3420 if (errno == EBUSY) {
3421 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, BUSY,
3422 (_("Device '%s' is busy"), v4l2object->videodev),
3423 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3424 GST_FOURCC_ARGS (pixelformat), width, height,
3425 g_strerror (errno)));
3431 if (errno == EBUSY) {
3432 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, BUSY,
3433 (_("Device '%s' is busy"), v4l2object->videodev),
3434 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3435 GST_FOURCC_ARGS (pixelformat), width, height,
3436 g_strerror (errno)));
3438 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3439 (_("Device '%s' cannot capture at %dx%d"),
3440 v4l2object->videodev, width, height),
3441 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3442 GST_FOURCC_ARGS (pixelformat), width, height,
3443 g_strerror (errno)));
3450 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3451 (_("Device '%s' cannot capture at %dx%d"),
3452 v4l2object->videodev, width, height),
3453 ("Tried to capture at %dx%d, but device returned size %dx%d",
3454 width, height, format.fmt.pix.width, format.fmt.pix.height));
3458 invalid_pixelformat:
3461 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3462 (_("Device '%s' cannot capture in the specified format"),
3463 v4l2object->videodev),
3464 ("Tried to capture in %" GST_FOURCC_FORMAT
3465 ", but device returned format" " %" GST_FOURCC_FORMAT,
3466 GST_FOURCC_ARGS (pixelformat),
3467 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
3474 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3475 (_("Device '%s' does support non-contiguous planes"),
3476 v4l2object->videodev),
3477 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
3483 /* it's possible that this call is not supported */
3484 if (errno != EINVAL && errno != ENOTTY) {
3485 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3486 (_("Could not get parameters on device '%s'"),
3487 v4l2object->videodev), GST_ERROR_SYSTEM);
3493 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3494 (_("Video device did not accept new frame rate setting.")),
3500 /* setup_pool already send the error */
3506 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps)
3508 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE);
3512 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps)
3514 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE);
3518 * gst_v4l2_object_acquire_format:
3519 * @v4l2object the object
3520 * @info a GstVideoInfo to be filled
3522 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
3523 * the output format is choosen by the HW.
3525 * Returns: %TRUE on success, %FALSE on failure.
3528 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
3530 struct v4l2_fmtdesc *fmtdesc;
3531 struct v4l2_format fmt;
3532 struct v4l2_crop crop;
3533 GstVideoFormat format;
3534 guint width, height;
3535 GstVideoAlignment align;
3537 gst_video_info_init (info);
3538 gst_video_alignment_reset (&align);
3540 memset (&fmt, 0x00, sizeof (struct v4l2_format));
3541 fmt.type = v4l2object->type;
3542 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
3543 goto get_fmt_failed;
3545 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
3546 fmt.fmt.pix.pixelformat);
3547 if (fmtdesc == NULL)
3548 goto unsupported_format;
3550 /* No need to care about mplane, the four first params are the same */
3551 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
3553 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
3554 if (format == GST_VIDEO_FORMAT_UNKNOWN)
3555 goto unsupported_format;
3557 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
3558 goto invalid_dimensions;
3560 width = fmt.fmt.pix.width;
3561 height = fmt.fmt.pix.height;
3563 memset (&crop, 0, sizeof (struct v4l2_crop));
3564 crop.type = v4l2object->type;
3565 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0) {
3566 align.padding_left = crop.c.left;
3567 align.padding_top = crop.c.top;
3568 align.padding_right = width - crop.c.width - crop.c.left;
3569 align.padding_bottom = height - crop.c.height - crop.c.top;
3570 width = crop.c.width;
3571 height = crop.c.height;
3574 gst_video_info_set_format (info, format, width, height);
3576 switch (fmt.fmt.pix.field) {
3577 case V4L2_FIELD_ANY:
3578 case V4L2_FIELD_NONE:
3579 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
3581 case V4L2_FIELD_INTERLACED:
3582 case V4L2_FIELD_INTERLACED_TB:
3583 case V4L2_FIELD_INTERLACED_BT:
3584 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
3587 goto unsupported_field;
3590 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
3592 /* Shall we setup the pool ? */
3598 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3599 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
3604 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3605 (_("Video device returned invalid dimensions.")),
3606 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
3607 fmt.fmt.pix.height));
3612 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3613 (_("Video devices uses an unsupported interlacing method.")),
3614 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
3619 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
3620 (_("Video devices uses an unsupported pixel format.")),
3621 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
3622 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
3628 gst_v4l2_object_set_crop (GstV4l2Object * obj)
3630 struct v4l2_crop crop = { 0 };
3632 crop.type = obj->type;
3633 crop.c.left = obj->align.padding_left;
3634 crop.c.top = obj->align.padding_top;
3635 crop.c.width = obj->info.width;
3636 crop.c.height = obj->info.height;
3638 if (obj->align.padding_left + obj->align.padding_top +
3639 obj->align.padding_right + obj->align.padding_bottom == 0) {
3640 GST_DEBUG_OBJECT (obj->element, "no cropping needed");
3644 GST_DEBUG_OBJECT (obj->element,
3645 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3646 crop.c.width, crop.c.height);
3648 if (v4l2_ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
3649 GST_WARNING_OBJECT (obj->element, "VIDIOC_S_CROP failed");
3653 if (v4l2_ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
3654 GST_WARNING_OBJECT (obj->element, "VIDIOC_G_CROP failed");
3658 GST_DEBUG_OBJECT (obj->element,
3659 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
3660 crop.c.width, crop.c.height);
3666 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
3668 GstStructure *config;
3672 if (!v4l2object->pool)
3675 config = gst_buffer_pool_get_config (v4l2object->pool);
3676 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
3678 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
3680 gst_structure_free (config);
3686 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
3688 gboolean ret = TRUE;
3690 GST_LOG_OBJECT (v4l2object->element, "start flushing");
3692 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3693 gst_buffer_pool_set_flushing (v4l2object->pool, TRUE);
3699 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
3701 gboolean ret = TRUE;
3703 GST_LOG_OBJECT (v4l2object->element, "stop flushing");
3705 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
3706 gst_buffer_pool_set_flushing (v4l2object->pool, FALSE);
3712 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
3714 GST_DEBUG_OBJECT (v4l2object->element, "stopping");
3716 if (!GST_V4L2_IS_OPEN (v4l2object))
3718 if (!GST_V4L2_IS_ACTIVE (v4l2object))
3721 if (v4l2object->pool) {
3722 GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
3723 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
3724 gst_object_unref (v4l2object->pool);
3725 v4l2object->pool = NULL;
3728 GST_V4L2_SET_INACTIVE (v4l2object);
3735 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3741 formats = gst_v4l2_object_get_format_list (v4l2object);
3743 ret = gst_caps_new_empty ();
3745 for (walk = formats; walk; walk = walk->next) {
3746 struct v4l2_fmtdesc *format;
3747 GstStructure *template;
3749 format = (struct v4l2_fmtdesc *) walk->data;
3751 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
3756 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
3757 format->pixelformat, template);
3759 gst_caps_append (ret, tmp);
3761 gst_structure_free (template);
3763 GST_DEBUG_OBJECT (v4l2object->element, "unknown format %u",
3764 format->pixelformat);
3769 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
3771 ret = gst_caps_ref (ret);
3778 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
3782 if (v4l2object->probed_caps == NULL)
3783 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
3786 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
3787 GST_CAPS_INTERSECT_FIRST);
3789 ret = gst_caps_ref (v4l2object->probed_caps);
3792 GST_INFO_OBJECT (v4l2object->element, "probed caps: %" GST_PTR_FORMAT, ret);
3793 LOG_CAPS (v4l2object->element, ret);
3799 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
3802 GstBufferPool *pool = NULL, *other_pool = NULL;
3803 GstStructure *config;
3804 guint size, min, max, own_min = 0;
3806 gboolean has_video_meta;
3807 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
3808 GstAllocator *allocator = NULL;
3809 GstAllocationParams params = { 0 };
3811 GST_DEBUG_OBJECT (obj->element, "decide allocation");
3813 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3814 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
3816 gst_query_parse_allocation (query, &caps, NULL);
3818 if (obj->pool == NULL) {
3819 if (!gst_v4l2_object_setup_pool (obj, caps))
3823 if (gst_query_get_n_allocation_params (query) > 0)
3824 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3826 if (gst_query_get_n_allocation_pools (query) > 0) {
3827 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3836 GST_DEBUG_OBJECT (obj->element, "allocation: size:%u min:%u max:%u pool:%"
3837 GST_PTR_FORMAT, size, min, max, pool);
3840 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
3842 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
3844 gst_v4l2_get_driver_min_buffers (obj);
3845 /* We can't share our own pool, if it exceed V4L2 capacity */
3846 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
3847 can_share_own_pool = FALSE;
3850 switch (obj->mode) {
3851 case GST_V4L2_IO_RW:
3853 /* in READ/WRITE mode, prefer a downstream pool because our own pool
3854 * doesn't help much, we have to write to it as well */
3855 GST_DEBUG_OBJECT (obj->element,
3856 "read/write mode: using downstream pool");
3857 /* use the bigest size, when we use our own pool we can't really do any
3858 * other size than what the hardware gives us but for downstream pools
3860 size = MAX (size, obj->info.size);
3861 } else if (can_share_own_pool) {
3862 /* no downstream pool, use our own then */
3863 GST_DEBUG_OBJECT (obj->element,
3864 "read/write mode: no downstream pool, using our own");
3865 pool = gst_object_ref (obj->pool);
3866 size = obj->info.size;
3867 pushing_from_our_pool = TRUE;
3871 case GST_V4L2_IO_USERPTR:
3872 case GST_V4L2_IO_DMABUF_IMPORT:
3873 /* in importing mode, prefer our own pool, and pass the other pool to
3874 * our own, so it can serve itself */
3876 goto no_downstream_pool;
3877 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj->pool),
3880 gst_object_unref (pool);
3881 pool = gst_object_ref (obj->pool);
3882 size = obj->info.size;
3885 case GST_V4L2_IO_MMAP:
3886 case GST_V4L2_IO_DMABUF:
3887 /* in streaming mode, prefer our own pool */
3888 /* Check if we can use it ... */
3889 if (can_share_own_pool) {
3891 gst_object_unref (pool);
3892 pool = gst_object_ref (obj->pool);
3893 size = obj->info.size;
3894 GST_DEBUG_OBJECT (obj->element,
3895 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
3896 pushing_from_our_pool = TRUE;
3898 GST_DEBUG_OBJECT (obj->element,
3899 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
3902 GST_DEBUG_OBJECT (obj->element,
3903 "streaming mode: no usable pool, copying to generic pool");
3904 size = MAX (size, obj->info.size);
3907 case GST_V4L2_IO_AUTO:
3909 GST_WARNING_OBJECT (obj->element, "unhandled mode");
3916 /* If pushing from our own pool, configure it with queried minimum,
3917 * otherwise use the minimum required */
3918 if (pushing_from_our_pool) {
3919 /* When pushing from our own pool, we need what downstream one, to be able
3920 * to fill the pipeline, the minimum required to decoder according to the
3921 * driver and 1 more, so we don't endup up with everything downstream or
3922 * held by the decoder. */
3923 own_min = min + obj->min_buffers + 1;
3925 /* If no allocation parameters where provided, allow for a little more
3926 * buffers and enable copy threshold */
3929 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
3932 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
3937 /* In this case we'll have to configure two buffer pool. For our buffer
3938 * pool, we'll need what the driver one, and one more, so we can dequeu */
3939 own_min = obj->min_buffers + 1;
3940 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS);
3942 /* for the downstream pool, we keep what downstream wants, though ensure
3943 * at least a minimum if downstream didn't suggest anything (we are
3944 * expecting the base class to create a default one for the context) */
3945 min = MAX (min, GST_V4L2_MIN_BUFFERS);
3947 /* To import we need the other pool to hold at least own_min */
3948 if (obj->pool == pool)
3952 /* Request a bigger max, if one was suggested but it's too small */
3954 max = MAX (min, max);
3956 /* First step, configure our own pool */
3957 config = gst_buffer_pool_get_config (obj->pool);
3959 if (obj->need_video_meta || has_video_meta) {
3960 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
3961 gst_buffer_pool_config_add_option (config,
3962 GST_BUFFER_POOL_OPTION_VIDEO_META);
3965 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
3966 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
3968 GST_DEBUG_OBJECT (obj->element, "setting own pool config to %"
3969 GST_PTR_FORMAT, config);
3971 /* Our pool often need to adjust the value */
3972 if (!gst_buffer_pool_set_config (obj->pool, config)) {
3973 config = gst_buffer_pool_get_config (obj->pool);
3975 GST_DEBUG_OBJECT (obj->element, "own pool config changed to %"
3976 GST_PTR_FORMAT, config);
3978 /* our pool will adjust the maximum buffer, which we are fine with */
3979 if (!gst_buffer_pool_set_config (obj->pool, config))
3983 /* Now configure the other pool if different */
3984 if (obj->pool != pool)
3988 config = gst_buffer_pool_get_config (other_pool);
3989 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
3990 gst_buffer_pool_config_set_params (config, caps, size, min, max);
3992 GST_DEBUG_OBJECT (obj->element, "setting other pool config to %"
3993 GST_PTR_FORMAT, config);
3995 /* if downstream supports video metadata, add this to the pool config */
3996 if (has_video_meta) {
3997 GST_DEBUG_OBJECT (obj->element, "activate Video Meta");
3998 gst_buffer_pool_config_add_option (config,
3999 GST_BUFFER_POOL_OPTION_VIDEO_META);
4002 if (!gst_buffer_pool_set_config (other_pool, config)) {
4003 config = gst_buffer_pool_get_config (other_pool);
4005 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
4007 gst_structure_free (config);
4011 if (!gst_buffer_pool_set_config (other_pool, config))
4017 /* For simplicity, simply read back the active configuration, so our base
4018 * class get the right information */
4019 config = gst_buffer_pool_get_config (pool);
4020 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
4021 gst_structure_free (config);
4025 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
4027 gst_query_add_allocation_pool (query, pool, size, min, max);
4030 gst_object_unref (allocator);
4033 gst_object_unref (pool);
4039 /* setup_pool already send the error */
4044 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4045 (_("Failed to configure internal buffer pool.")), (NULL));
4050 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4051 (_("Video device did not suggest any buffer size.")), (NULL));
4057 gst_object_unref (allocator);
4060 gst_object_unref (pool);
4065 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4066 (_("No downstream pool to import from.")),
4067 ("When importing DMABUF or USERPTR, we need a pool to import from"));
4073 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
4075 GstBufferPool *pool;
4076 /* we need at least 2 buffers to operate */
4077 guint size, min, max;
4081 /* Set defaults allocation parameters */
4082 size = obj->info.size;
4083 min = GST_V4L2_MIN_BUFFERS;
4084 max = VIDEO_MAX_FRAME;
4086 gst_query_parse_allocation (query, &caps, &need_pool);
4091 if ((pool = obj->pool))
4092 gst_object_ref (pool);
4096 GstStructure *config;
4098 /* we had a pool, check caps */
4099 config = gst_buffer_pool_get_config (pool);
4100 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
4102 GST_DEBUG_OBJECT (obj->element,
4103 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
4104 if (!gst_caps_is_equal (caps, pcaps)) {
4105 gst_structure_free (config);
4106 gst_object_unref (pool);
4107 goto different_caps;
4109 gst_structure_free (config);
4111 gst_v4l2_get_driver_min_buffers (obj);
4113 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS);
4115 gst_query_add_allocation_pool (query, pool, size, min, max);
4117 /* we also support various metadata */
4118 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4121 gst_object_unref (pool);
4128 GST_DEBUG_OBJECT (obj->element, "no caps specified");
4133 /* different caps, we can't use this pool */
4134 GST_DEBUG_OBJECT (obj->element, "pool has different caps");