1 /* GStreamer
2 *
3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@gmail.com>
5 *
6 * gstv4l2object.c: base class for V4L2 elements
7 *
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,
18 * USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/mman.h>
30 #include <sys/ioctl.h>
31
32
33 #ifdef HAVE_GUDEV
34 #include <gudev/gudev.h>
35 #endif
36
37 #include "ext/videodev2.h"
38 #include "gstv4l2object.h"
39 #include "gstv4l2tuner.h"
40 #include "gstv4l2colorbalance.h"
41
42 #include "gst/gst-i18n-plugin.h"
43
44 #include <gst/video/video.h>
45 #include <gst/allocators/gstdmabuf.h>
46
47 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
48 #define GST_CAT_DEFAULT v4l2_debug
49
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_IO_MODE GST_V4L2_IO_AUTO
55
56 #define ENCODED_BUFFER_SIZE (2 * 1024 * 1024)
57
58 enum
59 {
60 PROP_0,
61 V4L2_STD_OBJECT_PROPS,
62 };
63
64 /*
65 * common format / caps utilities:
66 */
67 typedef enum
68 {
69 GST_V4L2_RAW = 1 << 0,
70 GST_V4L2_CODEC = 1 << 1,
71 GST_V4L2_TRANSPORT = 1 << 2,
72 GST_V4L2_NO_PARSE = 1 << 3,
73 GST_V4L2_ALL = 0xffff
74 } GstV4L2FormatFlags;
75
76 typedef struct
77 {
78 guint32 format;
79 gboolean dimensions;
80 GstV4L2FormatFlags flags;
81 } GstV4L2FormatDesc;
82
83 static const GstV4L2FormatDesc gst_v4l2_formats[] = {
84 /* RGB formats */
85 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
86 {V4L2_PIX_FMT_ARGB555, TRUE, GST_V4L2_RAW},
87 {V4L2_PIX_FMT_XRGB555, TRUE, GST_V4L2_RAW},
88 {V4L2_PIX_FMT_ARGB555X, TRUE, GST_V4L2_RAW},
89 {V4L2_PIX_FMT_XRGB555X, TRUE, GST_V4L2_RAW},
90 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
91 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
92 {V4L2_PIX_FMT_BGR666, TRUE, GST_V4L2_RAW},
93 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
94 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
95 {V4L2_PIX_FMT_ABGR32, TRUE, GST_V4L2_RAW},
96 {V4L2_PIX_FMT_XBGR32, TRUE, GST_V4L2_RAW},
97 {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
98 {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},
99
100 /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
101 {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
102 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
103 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
104 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
105 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
106
107 /* Grey formats */
108 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
109 {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
110 {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
111 {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
112 {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
113 {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
114 {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
115 {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},
116
117 /* Palette formats */
118 {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},
119
120 /* Chrominance formats */
121 {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},
122
123 /* Luminance+Chrominance formats */
124 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
125 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
126 {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
127 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
128 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
129 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
130 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
131 {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
132 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
133 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
134 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
135 {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
136 {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
137 {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
138 {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
139 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
140 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
141 {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
142 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
143 {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
144 {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
145
146 /* two planes -- one Y, one Cr + Cb interleaved */
147 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
148 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
149 {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
150 {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
151 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
152 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
153 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
154 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
155 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
156 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
158 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
159
160 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
161 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_RAW},
162 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_RAW},
163 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_RAW},
164 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_RAW},
165
166 /* compressed formats */
167 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
168 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
169 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
170 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
171 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
172 {V4L2_PIX_FMT_FWHT, FALSE, GST_V4L2_CODEC},
173 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
174 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
175 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
176 {V4L2_PIX_FMT_HEVC, 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},
185 {V4L2_PIX_FMT_VP9, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
186
187 /* Vendor-specific formats */
188 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
189 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
190 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
191 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
192 };
193
194 #define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))
195
196 static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);
197
198
199 #define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
200 static GType
gst_v4l2_device_get_type(void)201 gst_v4l2_device_get_type (void)
202 {
203 static GType v4l2_device_type = 0;
204
205 if (v4l2_device_type == 0) {
206 static const GFlagsValue values[] = {
207 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
208 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
209 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
210
211 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
212 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
213
214 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
215 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
216
217 {0, NULL, NULL}
218 };
219
220 v4l2_device_type =
221 g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
222 }
223
224 return v4l2_device_type;
225 }
226
227 #define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
228 static GType
gst_v4l2_tv_norm_get_type(void)229 gst_v4l2_tv_norm_get_type (void)
230 {
231 static GType v4l2_tv_norm = 0;
232
233 if (!v4l2_tv_norm) {
234 static const GEnumValue tv_norms[] = {
235 {0, "none", "none"},
236
237 {V4L2_STD_NTSC, "NTSC", "NTSC"},
238 {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
239 {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
240 {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
241 {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},
242
243 {V4L2_STD_PAL, "PAL", "PAL"},
244 {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
245 {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
246 {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
247 {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
248 {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
249 {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
250 {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
251 {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
252 {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
253 {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
254 {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
255 {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
256 {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
257 {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},
258
259 {V4L2_STD_SECAM, "SECAM", "SECAM"},
260 {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
261 {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
262 {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
263 {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
264 {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
265 {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
266 {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
267 {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
268 {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},
269
270 {0, NULL, NULL}
271 };
272
273 v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
274 }
275
276 return v4l2_tv_norm;
277 }
278
279 GType
gst_v4l2_io_mode_get_type(void)280 gst_v4l2_io_mode_get_type (void)
281 {
282 static GType v4l2_io_mode = 0;
283
284 if (!v4l2_io_mode) {
285 static const GEnumValue io_modes[] = {
286 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
287 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
288 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
289 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
290 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
291 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
292 "dmabuf-import"},
293
294 {0, NULL, NULL}
295 };
296 v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
297 }
298 return v4l2_io_mode;
299 }
300
301 void
gst_v4l2_object_install_properties_helper(GObjectClass * gobject_class,const char * default_device)302 gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
303 const char *default_device)
304 {
305 g_object_class_install_property (gobject_class, PROP_DEVICE,
306 g_param_spec_string ("device", "Device", "Device location",
307 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
309 g_param_spec_string ("device-name", "Device name",
310 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
311 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
313 g_param_spec_int ("device-fd", "File descriptor",
314 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
315 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (gobject_class, PROP_FLAGS,
317 g_param_spec_flags ("flags", "Flags", "Device type flags",
318 GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
319 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
320
321 /**
322 * GstV4l2Src:brightness:
323 *
324 * Picture brightness, or more precisely, the black level
325 */
326 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
327 g_param_spec_int ("brightness", "Brightness",
328 "Picture brightness, or more precisely, the black level", G_MININT,
329 G_MAXINT, 0,
330 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
331 /**
332 * GstV4l2Src:contrast:
333 *
334 * Picture contrast or luma gain
335 */
336 g_object_class_install_property (gobject_class, PROP_CONTRAST,
337 g_param_spec_int ("contrast", "Contrast",
338 "Picture contrast or luma gain", G_MININT,
339 G_MAXINT, 0,
340 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
341 /**
342 * GstV4l2Src:saturation:
343 *
344 * Picture color saturation or chroma gain
345 */
346 g_object_class_install_property (gobject_class, PROP_SATURATION,
347 g_param_spec_int ("saturation", "Saturation",
348 "Picture color saturation or chroma gain", G_MININT,
349 G_MAXINT, 0,
350 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
351 /**
352 * GstV4l2Src:hue:
353 *
354 * Hue or color balance
355 */
356 g_object_class_install_property (gobject_class, PROP_HUE,
357 g_param_spec_int ("hue", "Hue",
358 "Hue or color balance", G_MININT,
359 G_MAXINT, 0,
360 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
361
362 /**
363 * GstV4l2Src:norm:
364 *
365 * TV norm
366 */
367 g_object_class_install_property (gobject_class, PROP_TV_NORM,
368 g_param_spec_enum ("norm", "TV norm",
369 "video standard",
370 GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
371 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372
373 /**
374 * GstV4l2Src:io-mode:
375 *
376 * IO Mode
377 */
378 g_object_class_install_property (gobject_class, PROP_IO_MODE,
379 g_param_spec_enum ("io-mode", "IO mode",
380 "I/O mode",
381 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
382 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383
384 /**
385 * GstV4l2Src:extra-controls:
386 *
387 * Additional v4l2 controls for the device. The controls are identified
388 * by the control name (lowercase with '_' for any non-alphanumeric
389 * characters).
390 *
391 * Since: 1.2
392 */
393 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
394 g_param_spec_boxed ("extra-controls", "Extra Controls",
395 "Extra v4l2 controls (CIDs) for the device",
396 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
397
398 /**
399 * GstV4l2Src:pixel-aspect-ratio:
400 *
401 * The pixel aspect ratio of the device. This overwrites the pixel aspect
402 * ratio queried from the device.
403 *
404 * Since: 1.2
405 */
406 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
407 g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
408 "Overwrite the pixel aspect ratio of the device", "1/1",
409 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
410
411 /**
412 * GstV4l2Src:force-aspect-ratio:
413 *
414 * When enabled, the pixel aspect ratio queried from the device or set
415 * with the pixel-aspect-ratio property will be enforced.
416 *
417 * Since: 1.2
418 */
419 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
420 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
421 "When enabled, the pixel aspect ratio will be enforced", TRUE,
422 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
423
424 }
425
426 void
gst_v4l2_object_install_m2m_properties_helper(GObjectClass * gobject_class)427 gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
428 {
429 g_object_class_install_property (gobject_class, PROP_DEVICE,
430 g_param_spec_string ("device", "Device", "Device location",
431 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
432
433 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
434 g_param_spec_string ("device-name", "Device name",
435 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
436 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
437
438 g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
439 g_param_spec_int ("device-fd", "File descriptor",
440 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
441 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
442
443 g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
444 g_param_spec_enum ("output-io-mode", "Output IO mode",
445 "Output side I/O mode (matches sink pad)",
446 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
447 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448
449 g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
450 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
451 "Capture I/O mode (matches src pad)",
452 GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
453 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
454
455 g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
456 g_param_spec_boxed ("extra-controls", "Extra Controls",
457 "Extra v4l2 controls (CIDs) for the device",
458 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
459 }
460
461 /* Support for 32bit off_t, this wrapper is casting off_t to gint64 */
462 #ifdef HAVE_LIBV4L2
463 #if SIZEOF_OFF_T < 8
464
465 static gpointer
v4l2_mmap_wrapper(gpointer start,gsize length,gint prot,gint flags,gint fd,off_t offset)466 v4l2_mmap_wrapper (gpointer start, gsize length, gint prot, gint flags, gint fd,
467 off_t offset)
468 {
469 return v4l2_mmap (start, length, prot, flags, fd, (gint64) offset);
470 }
471
472 #define v4l2_mmap v4l2_mmap_wrapper
473
474 #endif /* SIZEOF_OFF_T < 8 */
475 #endif /* HAVE_LIBV4L2 */
476
477 GstV4l2Object *
gst_v4l2_object_new(GstElement * element,GstObject * debug_object,enum v4l2_buf_type type,const char * default_device,GstV4l2GetInOutFunction get_in_out_func,GstV4l2SetInOutFunction set_in_out_func,GstV4l2UpdateFpsFunction update_fps_func)478 gst_v4l2_object_new (GstElement * element,
479 GstObject * debug_object,
480 enum v4l2_buf_type type,
481 const char *default_device,
482 GstV4l2GetInOutFunction get_in_out_func,
483 GstV4l2SetInOutFunction set_in_out_func,
484 GstV4l2UpdateFpsFunction update_fps_func)
485 {
486 GstV4l2Object *v4l2object;
487
488 /*
489 * some default values
490 */
491 v4l2object = g_new0 (GstV4l2Object, 1);
492
493 v4l2object->type = type;
494 v4l2object->formats = NULL;
495
496 v4l2object->element = element;
497 v4l2object->dbg_obj = debug_object;
498 v4l2object->get_in_out_func = get_in_out_func;
499 v4l2object->set_in_out_func = set_in_out_func;
500 v4l2object->update_fps_func = update_fps_func;
501
502 v4l2object->video_fd = -1;
503 v4l2object->active = FALSE;
504 v4l2object->videodev = g_strdup (default_device);
505
506 v4l2object->norms = NULL;
507 v4l2object->channels = NULL;
508 v4l2object->colors = NULL;
509
510 v4l2object->keep_aspect = TRUE;
511
512 v4l2object->n_v4l2_planes = 0;
513
514 v4l2object->no_initial_format = FALSE;
515
516 /* We now disable libv4l2 by default, but have an env to enable it. */
517 #ifdef HAVE_LIBV4L2
518 if (g_getenv ("GST_V4L2_USE_LIBV4L2")) {
519 v4l2object->fd_open = v4l2_fd_open;
520 v4l2object->close = v4l2_close;
521 v4l2object->dup = v4l2_dup;
522 v4l2object->ioctl = v4l2_ioctl;
523 v4l2object->read = v4l2_read;
524 v4l2object->mmap = v4l2_mmap;
525 v4l2object->munmap = v4l2_munmap;
526 } else
527 #endif
528 {
529 v4l2object->fd_open = NULL;
530 v4l2object->close = close;
531 v4l2object->dup = dup;
532 v4l2object->ioctl = ioctl;
533 v4l2object->read = read;
534 v4l2object->mmap = mmap;
535 v4l2object->munmap = munmap;
536 }
537
538 return v4l2object;
539 }
540
541 static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);
542
543
544 void
gst_v4l2_object_destroy(GstV4l2Object * v4l2object)545 gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
546 {
547 g_return_if_fail (v4l2object != NULL);
548
549 g_free (v4l2object->videodev);
550
551 g_free (v4l2object->channel);
552
553 if (v4l2object->formats) {
554 gst_v4l2_object_clear_format_list (v4l2object);
555 }
556
557 if (v4l2object->probed_caps) {
558 gst_caps_unref (v4l2object->probed_caps);
559 }
560
561 if (v4l2object->extra_controls) {
562 gst_structure_free (v4l2object->extra_controls);
563 }
564
565 g_free (v4l2object);
566 }
567
568
569 static gboolean
gst_v4l2_object_clear_format_list(GstV4l2Object * v4l2object)570 gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
571 {
572 g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
573 g_slist_free (v4l2object->formats);
574 v4l2object->formats = NULL;
575
576 return TRUE;
577 }
578
579 static gint
gst_v4l2_object_prop_to_cid(guint prop_id)580 gst_v4l2_object_prop_to_cid (guint prop_id)
581 {
582 gint cid = -1;
583
584 switch (prop_id) {
585 case PROP_BRIGHTNESS:
586 cid = V4L2_CID_BRIGHTNESS;
587 break;
588 case PROP_CONTRAST:
589 cid = V4L2_CID_CONTRAST;
590 break;
591 case PROP_SATURATION:
592 cid = V4L2_CID_SATURATION;
593 break;
594 case PROP_HUE:
595 cid = V4L2_CID_HUE;
596 break;
597 default:
598 GST_WARNING ("unmapped property id: %d", prop_id);
599 }
600 return cid;
601 }
602
603
604 gboolean
gst_v4l2_object_set_property_helper(GstV4l2Object * v4l2object,guint prop_id,const GValue * value,GParamSpec * pspec)605 gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
606 guint prop_id, const GValue * value, GParamSpec * pspec)
607 {
608 switch (prop_id) {
609 case PROP_DEVICE:
610 g_free (v4l2object->videodev);
611 v4l2object->videodev = g_value_dup_string (value);
612 break;
613 case PROP_BRIGHTNESS:
614 case PROP_CONTRAST:
615 case PROP_SATURATION:
616 case PROP_HUE:
617 {
618 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
619
620 if (cid != -1) {
621 if (GST_V4L2_IS_OPEN (v4l2object)) {
622 gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
623 }
624 }
625 return TRUE;
626 }
627 break;
628 case PROP_TV_NORM:
629 v4l2object->tv_norm = g_value_get_enum (value);
630 break;
631 #if 0
632 case PROP_CHANNEL:
633 if (GST_V4L2_IS_OPEN (v4l2object)) {
634 GstTuner *tuner = GST_TUNER (v4l2object->element);
635 GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
636 (gchar *) g_value_get_string (value));
637
638 if (channel) {
639 /* like gst_tuner_set_channel (tuner, channel)
640 without g_object_notify */
641 gst_v4l2_tuner_set_channel (v4l2object, channel);
642 }
643 } else {
644 g_free (v4l2object->channel);
645 v4l2object->channel = g_value_dup_string (value);
646 }
647 break;
648 case PROP_FREQUENCY:
649 if (GST_V4L2_IS_OPEN (v4l2object)) {
650 GstTuner *tuner = GST_TUNER (v4l2object->element);
651 GstTunerChannel *channel = gst_tuner_get_channel (tuner);
652
653 if (channel &&
654 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
655 /* like
656 gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
657 without g_object_notify */
658 gst_v4l2_tuner_set_frequency (v4l2object, channel,
659 g_value_get_ulong (value));
660 }
661 } else {
662 v4l2object->frequency = g_value_get_ulong (value);
663 }
664 break;
665 #endif
666
667 case PROP_IO_MODE:
668 v4l2object->req_mode = g_value_get_enum (value);
669 break;
670 case PROP_CAPTURE_IO_MODE:
671 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
672 v4l2object->req_mode = g_value_get_enum (value);
673 break;
674 case PROP_OUTPUT_IO_MODE:
675 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
676 v4l2object->req_mode = g_value_get_enum (value);
677 break;
678 case PROP_EXTRA_CONTROLS:{
679 const GstStructure *s = gst_value_get_structure (value);
680
681 if (v4l2object->extra_controls)
682 gst_structure_free (v4l2object->extra_controls);
683
684 v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
685 if (GST_V4L2_IS_OPEN (v4l2object))
686 gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
687 break;
688 }
689 case PROP_PIXEL_ASPECT_RATIO:
690 if (v4l2object->par) {
691 g_value_unset (v4l2object->par);
692 g_free (v4l2object->par);
693 }
694 v4l2object->par = g_new0 (GValue, 1);
695 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
696 if (!g_value_transform (value, v4l2object->par)) {
697 g_warning ("Could not transform string to aspect ratio");
698 gst_value_set_fraction (v4l2object->par, 1, 1);
699 }
700
701 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "set PAR to %d/%d",
702 gst_value_get_fraction_numerator (v4l2object->par),
703 gst_value_get_fraction_denominator (v4l2object->par));
704 break;
705 case PROP_FORCE_ASPECT_RATIO:
706 v4l2object->keep_aspect = g_value_get_boolean (value);
707 break;
708 default:
709 return FALSE;
710 break;
711 }
712 return TRUE;
713 }
714
715
716 gboolean
gst_v4l2_object_get_property_helper(GstV4l2Object * v4l2object,guint prop_id,GValue * value,GParamSpec * pspec)717 gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
718 guint prop_id, GValue * value, GParamSpec * pspec)
719 {
720 switch (prop_id) {
721 case PROP_DEVICE:
722 g_value_set_string (value, v4l2object->videodev);
723 break;
724 case PROP_DEVICE_NAME:
725 {
726 const guchar *name = NULL;
727
728 if (GST_V4L2_IS_OPEN (v4l2object))
729 name = v4l2object->vcap.card;
730
731 g_value_set_string (value, (gchar *) name);
732 break;
733 }
734 case PROP_DEVICE_FD:
735 {
736 if (GST_V4L2_IS_OPEN (v4l2object))
737 g_value_set_int (value, v4l2object->video_fd);
738 else
739 g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
740 break;
741 }
742 case PROP_FLAGS:
743 {
744 guint flags = 0;
745
746 if (GST_V4L2_IS_OPEN (v4l2object)) {
747 flags |= v4l2object->device_caps &
748 (V4L2_CAP_VIDEO_CAPTURE |
749 V4L2_CAP_VIDEO_OUTPUT |
750 V4L2_CAP_VIDEO_OVERLAY |
751 V4L2_CAP_VBI_CAPTURE |
752 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
753
754 if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
755 flags |= V4L2_CAP_VIDEO_CAPTURE;
756
757 if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
758 flags |= V4L2_CAP_VIDEO_OUTPUT;
759 }
760 g_value_set_flags (value, flags);
761 break;
762 }
763 case PROP_BRIGHTNESS:
764 case PROP_CONTRAST:
765 case PROP_SATURATION:
766 case PROP_HUE:
767 {
768 gint cid = gst_v4l2_object_prop_to_cid (prop_id);
769
770 if (cid != -1) {
771 if (GST_V4L2_IS_OPEN (v4l2object)) {
772 gint v;
773 if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
774 g_value_set_int (value, v);
775 }
776 }
777 }
778 return TRUE;
779 }
780 break;
781 case PROP_TV_NORM:
782 g_value_set_enum (value, v4l2object->tv_norm);
783 break;
784 case PROP_IO_MODE:
785 g_value_set_enum (value, v4l2object->req_mode);
786 break;
787 case PROP_CAPTURE_IO_MODE:
788 g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
789 g_value_set_enum (value, v4l2object->req_mode);
790 break;
791 case PROP_OUTPUT_IO_MODE:
792 g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
793 g_value_set_enum (value, v4l2object->req_mode);
794 break;
795 case PROP_EXTRA_CONTROLS:
796 gst_value_set_structure (value, v4l2object->extra_controls);
797 break;
798 case PROP_PIXEL_ASPECT_RATIO:
799 if (v4l2object->par)
800 g_value_transform (v4l2object->par, value);
801 break;
802 case PROP_FORCE_ASPECT_RATIO:
803 g_value_set_boolean (value, v4l2object->keep_aspect);
804 break;
805 default:
806 return FALSE;
807 break;
808 }
809 return TRUE;
810 }
811
812 static void
gst_v4l2_get_driver_min_buffers(GstV4l2Object * v4l2object)813 gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
814 {
815 struct v4l2_control control = { 0, };
816
817 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
818
819 if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
820 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
821 else
822 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
823
824 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
825 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
826 "driver requires a minimum of %d buffers", control.value);
827 v4l2object->min_buffers = control.value;
828 } else {
829 v4l2object->min_buffers = 0;
830 }
831 }
832
833 static void
gst_v4l2_set_defaults(GstV4l2Object * v4l2object)834 gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
835 {
836 GstTunerNorm *norm = NULL;
837 GstTunerChannel *channel = NULL;
838 GstTuner *tuner;
839
840 if (!GST_IS_TUNER (v4l2object->element))
841 return;
842
843 tuner = GST_TUNER (v4l2object->element);
844
845 if (v4l2object->tv_norm)
846 norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
847 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
848 "norm=%p", (guint64) v4l2object->tv_norm, norm);
849 if (norm) {
850 gst_tuner_set_norm (tuner, norm);
851 } else {
852 norm =
853 GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
854 if (norm) {
855 v4l2object->tv_norm =
856 gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
857 gst_tuner_norm_changed (tuner, norm);
858 }
859 }
860
861 if (v4l2object->channel)
862 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
863 if (channel) {
864 gst_tuner_set_channel (tuner, channel);
865 } else {
866 channel =
867 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
868 (v4l2object->element)));
869 if (channel) {
870 g_free (v4l2object->channel);
871 v4l2object->channel = g_strdup (channel->label);
872 gst_tuner_channel_changed (tuner, channel);
873 }
874 }
875
876 if (channel
877 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
878 if (v4l2object->frequency != 0) {
879 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
880 } else {
881 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
882 if (v4l2object->frequency == 0) {
883 /* guess */
884 gst_tuner_set_frequency (tuner, channel, 1000);
885 } else {
886 }
887 }
888 }
889 }
890
891 gboolean
gst_v4l2_object_open(GstV4l2Object * v4l2object)892 gst_v4l2_object_open (GstV4l2Object * v4l2object)
893 {
894 if (gst_v4l2_open (v4l2object))
895 gst_v4l2_set_defaults (v4l2object);
896 else
897 return FALSE;
898
899 return TRUE;
900 }
901
902 gboolean
gst_v4l2_object_open_shared(GstV4l2Object * v4l2object,GstV4l2Object * other)903 gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
904 {
905 gboolean ret;
906
907 ret = gst_v4l2_dup (v4l2object, other);
908
909 return ret;
910 }
911
912 gboolean
gst_v4l2_object_close(GstV4l2Object * v4l2object)913 gst_v4l2_object_close (GstV4l2Object * v4l2object)
914 {
915 if (!gst_v4l2_close (v4l2object))
916 return FALSE;
917
918 gst_caps_replace (&v4l2object->probed_caps, NULL);
919
920 /* reset our copy of the device caps */
921 v4l2object->device_caps = 0;
922
923 if (v4l2object->formats) {
924 gst_v4l2_object_clear_format_list (v4l2object);
925 }
926
927 if (v4l2object->par) {
928 g_value_unset (v4l2object->par);
929 g_free (v4l2object->par);
930 v4l2object->par = NULL;
931 }
932
933 if (v4l2object->channel) {
934 g_free (v4l2object->channel);
935 v4l2object->channel = NULL;
936 }
937
938 return TRUE;
939 }
940
941 static struct v4l2_fmtdesc *
gst_v4l2_object_get_format_from_fourcc(GstV4l2Object * v4l2object,guint32 fourcc)942 gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
943 guint32 fourcc)
944 {
945 struct v4l2_fmtdesc *fmt;
946 GSList *walk;
947
948 if (fourcc == 0)
949 return NULL;
950
951 walk = gst_v4l2_object_get_format_list (v4l2object);
952 while (walk) {
953 fmt = (struct v4l2_fmtdesc *) walk->data;
954 if (fmt->pixelformat == fourcc)
955 return fmt;
956 /* special case for jpeg */
957 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
958 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
959 fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
960 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
961 fourcc == V4L2_PIX_FMT_PJPG) {
962 return fmt;
963 }
964 }
965 walk = g_slist_next (walk);
966 }
967
968 return NULL;
969 }
970
971
972
973 /* complete made up ranking, the values themselves are meaningless */
974 /* These ranks MUST be X such that X<<15 fits on a signed int - see
975 the comment at the end of gst_v4l2_object_format_get_rank. */
976 #define YUV_BASE_RANK 1000
977 #define JPEG_BASE_RANK 500
978 #define DV_BASE_RANK 200
979 #define RGB_BASE_RANK 100
980 #define YUV_ODD_BASE_RANK 50
981 #define RGB_ODD_BASE_RANK 25
982 #define BAYER_BASE_RANK 15
983 #define S910_BASE_RANK 10
984 #define GREY_BASE_RANK 5
985 #define PWC_BASE_RANK 1
986
987 static gint
gst_v4l2_object_format_get_rank(const struct v4l2_fmtdesc * fmt)988 gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
989 {
990 guint32 fourcc = fmt->pixelformat;
991 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
992 gint rank = 0;
993
994 switch (fourcc) {
995 case V4L2_PIX_FMT_MJPEG:
996 case V4L2_PIX_FMT_PJPG:
997 rank = JPEG_BASE_RANK;
998 break;
999 case V4L2_PIX_FMT_JPEG:
1000 rank = JPEG_BASE_RANK + 1;
1001 break;
1002 case V4L2_PIX_FMT_MPEG: /* MPEG */
1003 rank = JPEG_BASE_RANK + 2;
1004 break;
1005
1006 case V4L2_PIX_FMT_RGB332:
1007 case V4L2_PIX_FMT_ARGB555:
1008 case V4L2_PIX_FMT_XRGB555:
1009 case V4L2_PIX_FMT_RGB555:
1010 case V4L2_PIX_FMT_ARGB555X:
1011 case V4L2_PIX_FMT_XRGB555X:
1012 case V4L2_PIX_FMT_RGB555X:
1013 case V4L2_PIX_FMT_BGR666:
1014 case V4L2_PIX_FMT_RGB565:
1015 case V4L2_PIX_FMT_RGB565X:
1016 case V4L2_PIX_FMT_RGB444:
1017 case V4L2_PIX_FMT_Y4:
1018 case V4L2_PIX_FMT_Y6:
1019 case V4L2_PIX_FMT_Y10:
1020 case V4L2_PIX_FMT_Y12:
1021 case V4L2_PIX_FMT_Y10BPACK:
1022 case V4L2_PIX_FMT_YUV555:
1023 case V4L2_PIX_FMT_YUV565:
1024 case V4L2_PIX_FMT_YUV32:
1025 case V4L2_PIX_FMT_NV12MT_16X16:
1026 case V4L2_PIX_FMT_NV42:
1027 case V4L2_PIX_FMT_H264_MVC:
1028 rank = RGB_ODD_BASE_RANK;
1029 break;
1030
1031 case V4L2_PIX_FMT_RGB24:
1032 case V4L2_PIX_FMT_BGR24:
1033 rank = RGB_BASE_RANK - 1;
1034 break;
1035
1036 case V4L2_PIX_FMT_RGB32:
1037 case V4L2_PIX_FMT_BGR32:
1038 case V4L2_PIX_FMT_ABGR32:
1039 case V4L2_PIX_FMT_XBGR32:
1040 case V4L2_PIX_FMT_ARGB32:
1041 case V4L2_PIX_FMT_XRGB32:
1042 rank = RGB_BASE_RANK;
1043 break;
1044
1045 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1046 rank = GREY_BASE_RANK;
1047 break;
1048
1049 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1050 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
1051 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
1052 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1053 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1054 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1055 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1056 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1057 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1058 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1059 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1060 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1061 rank = YUV_ODD_BASE_RANK;
1062 break;
1063
1064 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1065 rank = YUV_BASE_RANK + 3;
1066 break;
1067 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1068 rank = YUV_BASE_RANK + 2;
1069 break;
1070 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1071 case V4L2_PIX_FMT_YUV420M:
1072 rank = YUV_BASE_RANK + 7;
1073 break;
1074 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1075 rank = YUV_BASE_RANK + 10;
1076 break;
1077 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1078 rank = YUV_BASE_RANK + 6;
1079 break;
1080 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1081 rank = YUV_BASE_RANK + 9;
1082 break;
1083 case V4L2_PIX_FMT_YUV444:
1084 rank = YUV_BASE_RANK + 6;
1085 break;
1086 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1087 rank = YUV_BASE_RANK + 5;
1088 break;
1089 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1090 rank = YUV_BASE_RANK + 4;
1091 break;
1092 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1093 rank = YUV_BASE_RANK + 8;
1094 break;
1095
1096 case V4L2_PIX_FMT_DV:
1097 rank = DV_BASE_RANK;
1098 break;
1099
1100 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1101 rank = 0;
1102 break;
1103
1104 case V4L2_PIX_FMT_SBGGR8:
1105 case V4L2_PIX_FMT_SGBRG8:
1106 case V4L2_PIX_FMT_SGRBG8:
1107 case V4L2_PIX_FMT_SRGGB8:
1108 rank = BAYER_BASE_RANK;
1109 break;
1110
1111 case V4L2_PIX_FMT_SN9C10X:
1112 rank = S910_BASE_RANK;
1113 break;
1114
1115 case V4L2_PIX_FMT_PWC1:
1116 rank = PWC_BASE_RANK;
1117 break;
1118 case V4L2_PIX_FMT_PWC2:
1119 rank = PWC_BASE_RANK;
1120 break;
1121
1122 default:
1123 rank = 0;
1124 break;
1125 }
1126
1127 /* All ranks are below 1<<15 so a shift by 15
1128 * will a) make all non-emulated formats larger
1129 * than emulated and b) will not overflow
1130 */
1131 if (!emulated)
1132 rank <<= 15;
1133
1134 return rank;
1135 }
1136
1137
1138
1139 static gint
format_cmp_func(gconstpointer a,gconstpointer b)1140 format_cmp_func (gconstpointer a, gconstpointer b)
1141 {
1142 const struct v4l2_fmtdesc *fa = a;
1143 const struct v4l2_fmtdesc *fb = b;
1144
1145 if (fa->pixelformat == fb->pixelformat)
1146 return 0;
1147
1148 return gst_v4l2_object_format_get_rank (fb) -
1149 gst_v4l2_object_format_get_rank (fa);
1150 }
1151
1152 /******************************************************
1153 * gst_v4l2_object_fill_format_list():
1154 * create list of supported capture formats
1155 * return value: TRUE on success, FALSE on error
1156 ******************************************************/
1157 static gboolean
gst_v4l2_object_fill_format_list(GstV4l2Object * v4l2object,enum v4l2_buf_type type)1158 gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
1159 enum v4l2_buf_type type)
1160 {
1161 gint n;
1162 struct v4l2_fmtdesc *format;
1163
1164 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting src format enumerations");
1165
1166 /* format enumeration */
1167 for (n = 0;; n++) {
1168 format = g_new0 (struct v4l2_fmtdesc, 1);
1169
1170 format->index = n;
1171 format->type = type;
1172
1173 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1174 if (errno == EINVAL) {
1175 g_free (format);
1176 break; /* end of enumeration */
1177 } else {
1178 goto failed;
1179 }
1180 }
1181
1182 GST_LOG_OBJECT (v4l2object->dbg_obj, "index: %u", format->index);
1183 GST_LOG_OBJECT (v4l2object->dbg_obj, "type: %d", format->type);
1184 GST_LOG_OBJECT (v4l2object->dbg_obj, "flags: %08x", format->flags);
1185 GST_LOG_OBJECT (v4l2object->dbg_obj, "description: '%s'",
1186 format->description);
1187 GST_LOG_OBJECT (v4l2object->dbg_obj, "pixelformat: %" GST_FOURCC_FORMAT,
1188 GST_FOURCC_ARGS (format->pixelformat));
1189
1190 /* sort formats according to our preference; we do this, because caps
1191 * are probed in the order the formats are in the list, and the order of
1192 * formats in the final probed caps matters for things like fixation */
1193 v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
1194 (GCompareFunc) format_cmp_func);
1195 }
1196
1197 #ifndef GST_DISABLE_GST_DEBUG
1198 {
1199 GSList *l;
1200
1201 GST_INFO_OBJECT (v4l2object->dbg_obj, "got %d format(s):", n);
1202 for (l = v4l2object->formats; l != NULL; l = l->next) {
1203 format = l->data;
1204
1205 GST_INFO_OBJECT (v4l2object->dbg_obj,
1206 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
1207 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1208 }
1209 }
1210 #endif
1211
1212 return TRUE;
1213
1214 /* ERRORS */
1215 failed:
1216 {
1217 g_free (format);
1218
1219 if (v4l2object->element)
1220 return FALSE;
1221
1222 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1223 (_("Failed to enumerate possible video formats device '%s' can work "
1224 "with"), v4l2object->videodev),
1225 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1226 n, v4l2object->videodev, errno, g_strerror (errno)));
1227
1228 return FALSE;
1229 }
1230 }
1231
1232 /*
1233 * Get the list of supported capture formats, a list of
1234 * <code>struct v4l2_fmtdesc</code>.
1235 */
1236 static GSList *
gst_v4l2_object_get_format_list(GstV4l2Object * v4l2object)1237 gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
1238 {
1239 if (!v4l2object->formats) {
1240
1241 /* check usual way */
1242 gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);
1243
1244 /* if our driver supports multi-planar
1245 * and if formats are still empty then we can workaround driver bug
1246 * by also looking up formats as if our device was not supporting
1247 * multiplanar */
1248 if (!v4l2object->formats) {
1249 switch (v4l2object->type) {
1250 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1251 gst_v4l2_object_fill_format_list (v4l2object,
1252 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1253 break;
1254
1255 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1256 gst_v4l2_object_fill_format_list (v4l2object,
1257 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1258 break;
1259
1260 default:
1261 break;
1262 }
1263 }
1264 }
1265 return v4l2object->formats;
1266 }
1267
1268 static GstVideoFormat
gst_v4l2_object_v4l2fourcc_to_video_format(guint32 fourcc)1269 gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
1270 {
1271 GstVideoFormat format;
1272
1273 switch (fourcc) {
1274 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1275 format = GST_VIDEO_FORMAT_GRAY8;
1276 break;
1277 case V4L2_PIX_FMT_Y16:
1278 format = GST_VIDEO_FORMAT_GRAY16_LE;
1279 break;
1280 case V4L2_PIX_FMT_Y16_BE:
1281 format = GST_VIDEO_FORMAT_GRAY16_BE;
1282 break;
1283 case V4L2_PIX_FMT_XRGB555:
1284 case V4L2_PIX_FMT_RGB555:
1285 format = GST_VIDEO_FORMAT_RGB15;
1286 break;
1287 case V4L2_PIX_FMT_XRGB555X:
1288 case V4L2_PIX_FMT_RGB555X:
1289 format = GST_VIDEO_FORMAT_BGR15;
1290 break;
1291 case V4L2_PIX_FMT_RGB565:
1292 format = GST_VIDEO_FORMAT_RGB16;
1293 break;
1294 case V4L2_PIX_FMT_RGB24:
1295 format = GST_VIDEO_FORMAT_RGB;
1296 break;
1297 case V4L2_PIX_FMT_BGR24:
1298 format = GST_VIDEO_FORMAT_BGR;
1299 break;
1300 case V4L2_PIX_FMT_XRGB32:
1301 case V4L2_PIX_FMT_RGB32:
1302 format = GST_VIDEO_FORMAT_xRGB;
1303 break;
1304 case V4L2_PIX_FMT_XBGR32:
1305 case V4L2_PIX_FMT_BGR32:
1306 format = GST_VIDEO_FORMAT_BGRx;
1307 break;
1308 case V4L2_PIX_FMT_ABGR32:
1309 format = GST_VIDEO_FORMAT_BGRA;
1310 break;
1311 case V4L2_PIX_FMT_ARGB32:
1312 format = GST_VIDEO_FORMAT_ARGB;
1313 break;
1314 case V4L2_PIX_FMT_NV12:
1315 case V4L2_PIX_FMT_NV12M:
1316 format = GST_VIDEO_FORMAT_NV12;
1317 break;
1318 case V4L2_PIX_FMT_NV12MT:
1319 format = GST_VIDEO_FORMAT_NV12_64Z32;
1320 break;
1321 case V4L2_PIX_FMT_NV21:
1322 case V4L2_PIX_FMT_NV21M:
1323 format = GST_VIDEO_FORMAT_NV21;
1324 break;
1325 case V4L2_PIX_FMT_YVU410:
1326 format = GST_VIDEO_FORMAT_YVU9;
1327 break;
1328 case V4L2_PIX_FMT_YUV410:
1329 format = GST_VIDEO_FORMAT_YUV9;
1330 break;
1331 case V4L2_PIX_FMT_YUV420:
1332 case V4L2_PIX_FMT_YUV420M:
1333 format = GST_VIDEO_FORMAT_I420;
1334 break;
1335 case V4L2_PIX_FMT_YUYV:
1336 format = GST_VIDEO_FORMAT_YUY2;
1337 break;
1338 case V4L2_PIX_FMT_YVU420:
1339 format = GST_VIDEO_FORMAT_YV12;
1340 break;
1341 case V4L2_PIX_FMT_UYVY:
1342 format = GST_VIDEO_FORMAT_UYVY;
1343 break;
1344 case V4L2_PIX_FMT_YUV411P:
1345 format = GST_VIDEO_FORMAT_Y41B;
1346 break;
1347 case V4L2_PIX_FMT_YUV422P:
1348 format = GST_VIDEO_FORMAT_Y42B;
1349 break;
1350 case V4L2_PIX_FMT_YVYU:
1351 format = GST_VIDEO_FORMAT_YVYU;
1352 break;
1353 case V4L2_PIX_FMT_NV16:
1354 case V4L2_PIX_FMT_NV16M:
1355 format = GST_VIDEO_FORMAT_NV16;
1356 break;
1357 case V4L2_PIX_FMT_NV61:
1358 case V4L2_PIX_FMT_NV61M:
1359 format = GST_VIDEO_FORMAT_NV61;
1360 break;
1361 case V4L2_PIX_FMT_NV24:
1362 format = GST_VIDEO_FORMAT_NV24;
1363 break;
1364 default:
1365 format = GST_VIDEO_FORMAT_UNKNOWN;
1366 break;
1367 }
1368
1369 return format;
1370 }
1371
1372 static gboolean
gst_v4l2_object_v4l2fourcc_is_rgb(guint32 fourcc)1373 gst_v4l2_object_v4l2fourcc_is_rgb (guint32 fourcc)
1374 {
1375 gboolean ret = FALSE;
1376
1377 switch (fourcc) {
1378 case V4L2_PIX_FMT_XRGB555:
1379 case V4L2_PIX_FMT_RGB555:
1380 case V4L2_PIX_FMT_XRGB555X:
1381 case V4L2_PIX_FMT_RGB555X:
1382 case V4L2_PIX_FMT_RGB565:
1383 case V4L2_PIX_FMT_RGB24:
1384 case V4L2_PIX_FMT_BGR24:
1385 case V4L2_PIX_FMT_XRGB32:
1386 case V4L2_PIX_FMT_RGB32:
1387 case V4L2_PIX_FMT_XBGR32:
1388 case V4L2_PIX_FMT_BGR32:
1389 case V4L2_PIX_FMT_ABGR32:
1390 case V4L2_PIX_FMT_ARGB32:
1391 case V4L2_PIX_FMT_SBGGR8:
1392 case V4L2_PIX_FMT_SGBRG8:
1393 case V4L2_PIX_FMT_SGRBG8:
1394 case V4L2_PIX_FMT_SRGGB8:
1395 ret = TRUE;
1396 break;
1397 default:
1398 break;
1399 }
1400
1401 return ret;
1402 }
1403
1404 static GstStructure *
gst_v4l2_object_v4l2fourcc_to_bare_struct(guint32 fourcc)1405 gst_v4l2_object_v4l2fourcc_to_bare_struct (guint32 fourcc)
1406 {
1407 GstStructure *structure = NULL;
1408
1409 switch (fourcc) {
1410 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1411 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1412 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1413 structure = gst_structure_new_empty ("image/jpeg");
1414 break;
1415 case V4L2_PIX_FMT_MPEG1:
1416 structure = gst_structure_new ("video/mpeg",
1417 "mpegversion", G_TYPE_INT, 1, NULL);
1418 break;
1419 case V4L2_PIX_FMT_MPEG2:
1420 structure = gst_structure_new ("video/mpeg",
1421 "mpegversion", G_TYPE_INT, 2, NULL);
1422 break;
1423 case V4L2_PIX_FMT_MPEG4:
1424 case V4L2_PIX_FMT_XVID:
1425 structure = gst_structure_new ("video/mpeg",
1426 "mpegversion", G_TYPE_INT, 4, "systemstream",
1427 G_TYPE_BOOLEAN, FALSE, NULL);
1428 break;
1429 case V4L2_PIX_FMT_FWHT:
1430 structure = gst_structure_new_empty ("video/x-fwht");
1431 break;
1432 case V4L2_PIX_FMT_H263:
1433 structure = gst_structure_new ("video/x-h263",
1434 "variant", G_TYPE_STRING, "itu", NULL);
1435 break;
1436 case V4L2_PIX_FMT_H264: /* H.264 */
1437 structure = gst_structure_new ("video/x-h264",
1438 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1439 G_TYPE_STRING, "au", NULL);
1440 break;
1441 case V4L2_PIX_FMT_H264_NO_SC:
1442 structure = gst_structure_new ("video/x-h264",
1443 "stream-format", G_TYPE_STRING, "avc", "alignment",
1444 G_TYPE_STRING, "au", NULL);
1445 break;
1446 case V4L2_PIX_FMT_HEVC: /* H.265 */
1447 structure = gst_structure_new ("video/x-h265",
1448 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1449 G_TYPE_STRING, "au", NULL);
1450 break;
1451 case V4L2_PIX_FMT_VC1_ANNEX_G:
1452 case V4L2_PIX_FMT_VC1_ANNEX_L:
1453 structure = gst_structure_new ("video/x-wmv",
1454 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1455 break;
1456 case V4L2_PIX_FMT_VP8:
1457 structure = gst_structure_new_empty ("video/x-vp8");
1458 break;
1459 case V4L2_PIX_FMT_VP9:
1460 structure = gst_structure_new_empty ("video/x-vp9");
1461 break;
1462 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1463 case V4L2_PIX_FMT_Y16:
1464 case V4L2_PIX_FMT_Y16_BE:
1465 case V4L2_PIX_FMT_XRGB555:
1466 case V4L2_PIX_FMT_RGB555:
1467 case V4L2_PIX_FMT_XRGB555X:
1468 case V4L2_PIX_FMT_RGB555X:
1469 case V4L2_PIX_FMT_RGB565:
1470 case V4L2_PIX_FMT_RGB24:
1471 case V4L2_PIX_FMT_BGR24:
1472 case V4L2_PIX_FMT_RGB32:
1473 case V4L2_PIX_FMT_XRGB32:
1474 case V4L2_PIX_FMT_ARGB32:
1475 case V4L2_PIX_FMT_BGR32:
1476 case V4L2_PIX_FMT_XBGR32:
1477 case V4L2_PIX_FMT_ABGR32:
1478 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1479 case V4L2_PIX_FMT_NV12M:
1480 case V4L2_PIX_FMT_NV12MT:
1481 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1482 case V4L2_PIX_FMT_NV21M:
1483 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1484 case V4L2_PIX_FMT_NV16M:
1485 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1486 case V4L2_PIX_FMT_NV61M:
1487 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1488 case V4L2_PIX_FMT_YVU410:
1489 case V4L2_PIX_FMT_YUV410:
1490 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1491 case V4L2_PIX_FMT_YUV420M:
1492 case V4L2_PIX_FMT_YUYV:
1493 case V4L2_PIX_FMT_YVU420:
1494 case V4L2_PIX_FMT_UYVY:
1495 case V4L2_PIX_FMT_YUV422P:
1496 case V4L2_PIX_FMT_YVYU:
1497 case V4L2_PIX_FMT_YUV411P:{
1498 GstVideoFormat format;
1499 format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1500 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1501 structure = gst_structure_new ("video/x-raw",
1502 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1503 break;
1504 }
1505 case V4L2_PIX_FMT_DV:
1506 structure =
1507 gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1508 NULL);
1509 break;
1510 case V4L2_PIX_FMT_MPEG: /* MPEG */
1511 structure = gst_structure_new ("video/mpegts",
1512 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1513 break;
1514 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1515 break;
1516 case V4L2_PIX_FMT_SBGGR8:
1517 case V4L2_PIX_FMT_SGBRG8:
1518 case V4L2_PIX_FMT_SGRBG8:
1519 case V4L2_PIX_FMT_SRGGB8:
1520 structure = gst_structure_new ("video/x-bayer", "format", G_TYPE_STRING,
1521 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" :
1522 fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg" :
1523 fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg" :
1524 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb", NULL);
1525 break;
1526 case V4L2_PIX_FMT_SN9C10X:
1527 structure = gst_structure_new_empty ("video/x-sonix");
1528 break;
1529 case V4L2_PIX_FMT_PWC1:
1530 structure = gst_structure_new_empty ("video/x-pwc1");
1531 break;
1532 case V4L2_PIX_FMT_PWC2:
1533 structure = gst_structure_new_empty ("video/x-pwc2");
1534 break;
1535 case V4L2_PIX_FMT_RGB332:
1536 case V4L2_PIX_FMT_BGR666:
1537 case V4L2_PIX_FMT_ARGB555X:
1538 case V4L2_PIX_FMT_RGB565X:
1539 case V4L2_PIX_FMT_RGB444:
1540 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1541 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1542 case V4L2_PIX_FMT_Y4:
1543 case V4L2_PIX_FMT_Y6:
1544 case V4L2_PIX_FMT_Y10:
1545 case V4L2_PIX_FMT_Y12:
1546 case V4L2_PIX_FMT_Y10BPACK:
1547 case V4L2_PIX_FMT_YUV444:
1548 case V4L2_PIX_FMT_YUV555:
1549 case V4L2_PIX_FMT_YUV565:
1550 case V4L2_PIX_FMT_Y41P:
1551 case V4L2_PIX_FMT_YUV32:
1552 case V4L2_PIX_FMT_NV12MT_16X16:
1553 case V4L2_PIX_FMT_NV42:
1554 case V4L2_PIX_FMT_H264_MVC:
1555 default:
1556 GST_DEBUG ("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1557 fourcc, GST_FOURCC_ARGS (fourcc));
1558 break;
1559 }
1560
1561 return structure;
1562 }
1563
1564 GstStructure *
gst_v4l2_object_v4l2fourcc_to_structure(guint32 fourcc)1565 gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
1566 {
1567 GstStructure *template;
1568 gint i;
1569
1570 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (fourcc);
1571
1572 if (template == NULL)
1573 goto done;
1574
1575 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1576 if (gst_v4l2_formats[i].format != fourcc)
1577 continue;
1578
1579 if (gst_v4l2_formats[i].dimensions) {
1580 gst_structure_set (template,
1581 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1582 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1583 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1584 }
1585 break;
1586 }
1587
1588 done:
1589 return template;
1590 }
1591
1592
1593 static GstCaps *
gst_v4l2_object_get_caps_helper(GstV4L2FormatFlags flags)1594 gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
1595 {
1596 GstStructure *structure;
1597 GstCaps *caps;
1598 guint i;
1599
1600 caps = gst_caps_new_empty ();
1601 for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1602
1603 if ((gst_v4l2_formats[i].flags & flags) == 0)
1604 continue;
1605
1606 structure =
1607 gst_v4l2_object_v4l2fourcc_to_bare_struct (gst_v4l2_formats[i].format);
1608
1609 if (structure) {
1610 GstStructure *alt_s = NULL;
1611
1612 if (gst_v4l2_formats[i].dimensions) {
1613 gst_structure_set (structure,
1614 "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1615 "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
1616 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1617 }
1618
1619 switch (gst_v4l2_formats[i].format) {
1620 case V4L2_PIX_FMT_RGB32:
1621 alt_s = gst_structure_copy (structure);
1622 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1623 break;
1624 case V4L2_PIX_FMT_BGR32:
1625 alt_s = gst_structure_copy (structure);
1626 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1627 default:
1628 break;
1629 }
1630
1631 gst_caps_append_structure (caps, structure);
1632
1633 if (alt_s)
1634 gst_caps_append_structure (caps, alt_s);
1635 }
1636 }
1637
1638 return gst_caps_simplify (caps);
1639 }
1640
1641 GstCaps *
gst_v4l2_object_get_all_caps(void)1642 gst_v4l2_object_get_all_caps (void)
1643 {
1644 static GstCaps *caps = NULL;
1645
1646 if (g_once_init_enter (&caps)) {
1647 GstCaps *all_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
1648 GST_MINI_OBJECT_FLAG_SET (all_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1649 g_once_init_leave (&caps, all_caps);
1650 }
1651
1652 return caps;
1653 }
1654
1655 GstCaps *
gst_v4l2_object_get_raw_caps(void)1656 gst_v4l2_object_get_raw_caps (void)
1657 {
1658 static GstCaps *caps = NULL;
1659
1660 if (g_once_init_enter (&caps)) {
1661 GstCaps *raw_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
1662 GST_MINI_OBJECT_FLAG_SET (raw_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1663 g_once_init_leave (&caps, raw_caps);
1664 }
1665
1666 return caps;
1667 }
1668
1669 GstCaps *
gst_v4l2_object_get_codec_caps(void)1670 gst_v4l2_object_get_codec_caps (void)
1671 {
1672 static GstCaps *caps = NULL;
1673
1674 if (g_once_init_enter (&caps)) {
1675 GstCaps *codec_caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);
1676 GST_MINI_OBJECT_FLAG_SET (codec_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1677 g_once_init_leave (&caps, codec_caps);
1678 }
1679
1680 return caps;
1681 }
1682
1683 /* collect data for the given caps
1684 * @caps: given input caps
1685 * @format: location for the v4l format
1686 * @w/@h: location for width and height
1687 * @fps_n/@fps_d: location for framerate
1688 * @size: location for expected size of the frame or 0 if unknown
1689 */
1690 static gboolean
gst_v4l2_object_get_caps_info(GstV4l2Object * v4l2object,GstCaps * caps,struct v4l2_fmtdesc ** format,GstVideoInfo * info)1691 gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
1692 struct v4l2_fmtdesc **format, GstVideoInfo * info)
1693 {
1694 GstStructure *structure;
1695 guint32 fourcc = 0, fourcc_nc = 0;
1696 const gchar *mimetype;
1697 struct v4l2_fmtdesc *fmt = NULL;
1698
1699 structure = gst_caps_get_structure (caps, 0);
1700
1701 mimetype = gst_structure_get_name (structure);
1702
1703 if (!gst_video_info_from_caps (info, caps))
1704 goto invalid_format;
1705
1706 if (g_str_equal (mimetype, "video/x-raw")) {
1707 switch (GST_VIDEO_INFO_FORMAT (info)) {
1708 case GST_VIDEO_FORMAT_I420:
1709 fourcc = V4L2_PIX_FMT_YUV420;
1710 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1711 break;
1712 case GST_VIDEO_FORMAT_YUY2:
1713 fourcc = V4L2_PIX_FMT_YUYV;
1714 break;
1715 case GST_VIDEO_FORMAT_UYVY:
1716 fourcc = V4L2_PIX_FMT_UYVY;
1717 break;
1718 case GST_VIDEO_FORMAT_YV12:
1719 fourcc = V4L2_PIX_FMT_YVU420;
1720 break;
1721 case GST_VIDEO_FORMAT_Y41B:
1722 fourcc = V4L2_PIX_FMT_YUV411P;
1723 break;
1724 case GST_VIDEO_FORMAT_Y42B:
1725 fourcc = V4L2_PIX_FMT_YUV422P;
1726 break;
1727 case GST_VIDEO_FORMAT_NV12:
1728 fourcc = V4L2_PIX_FMT_NV12;
1729 fourcc_nc = V4L2_PIX_FMT_NV12M;
1730 break;
1731 case GST_VIDEO_FORMAT_NV12_64Z32:
1732 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1733 break;
1734 case GST_VIDEO_FORMAT_NV21:
1735 fourcc = V4L2_PIX_FMT_NV21;
1736 fourcc_nc = V4L2_PIX_FMT_NV21M;
1737 break;
1738 case GST_VIDEO_FORMAT_NV16:
1739 fourcc = V4L2_PIX_FMT_NV16;
1740 fourcc_nc = V4L2_PIX_FMT_NV16M;
1741 break;
1742 case GST_VIDEO_FORMAT_NV61:
1743 fourcc = V4L2_PIX_FMT_NV61;
1744 fourcc_nc = V4L2_PIX_FMT_NV61M;
1745 break;
1746 case GST_VIDEO_FORMAT_NV24:
1747 fourcc = V4L2_PIX_FMT_NV24;
1748 break;
1749 case GST_VIDEO_FORMAT_YVYU:
1750 fourcc = V4L2_PIX_FMT_YVYU;
1751 break;
1752 case GST_VIDEO_FORMAT_RGB15:
1753 fourcc = V4L2_PIX_FMT_RGB555;
1754 fourcc_nc = V4L2_PIX_FMT_XRGB555;
1755 break;
1756 case GST_VIDEO_FORMAT_RGB16:
1757 fourcc = V4L2_PIX_FMT_RGB565;
1758 break;
1759 case GST_VIDEO_FORMAT_RGB:
1760 fourcc = V4L2_PIX_FMT_RGB24;
1761 break;
1762 case GST_VIDEO_FORMAT_BGR:
1763 fourcc = V4L2_PIX_FMT_BGR24;
1764 break;
1765 case GST_VIDEO_FORMAT_xRGB:
1766 fourcc = V4L2_PIX_FMT_RGB32;
1767 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1768 break;
1769 case GST_VIDEO_FORMAT_ARGB:
1770 fourcc = V4L2_PIX_FMT_RGB32;
1771 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1772 break;
1773 case GST_VIDEO_FORMAT_BGRx:
1774 fourcc = V4L2_PIX_FMT_BGR32;
1775 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1776 break;
1777 case GST_VIDEO_FORMAT_BGRA:
1778 fourcc = V4L2_PIX_FMT_BGR32;
1779 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1780 break;
1781 case GST_VIDEO_FORMAT_GRAY8:
1782 fourcc = V4L2_PIX_FMT_GREY;
1783 break;
1784 case GST_VIDEO_FORMAT_GRAY16_LE:
1785 fourcc = V4L2_PIX_FMT_Y16;
1786 break;
1787 case GST_VIDEO_FORMAT_GRAY16_BE:
1788 fourcc = V4L2_PIX_FMT_Y16_BE;
1789 break;
1790 default:
1791 break;
1792 }
1793 } else {
1794 if (g_str_equal (mimetype, "video/mpegts")) {
1795 fourcc = V4L2_PIX_FMT_MPEG;
1796 } else if (g_str_equal (mimetype, "video/x-dv")) {
1797 fourcc = V4L2_PIX_FMT_DV;
1798 } else if (g_str_equal (mimetype, "image/jpeg")) {
1799 fourcc = V4L2_PIX_FMT_JPEG;
1800 } else if (g_str_equal (mimetype, "video/mpeg")) {
1801 gint version;
1802 if (gst_structure_get_int (structure, "mpegversion", &version)) {
1803 switch (version) {
1804 case 1:
1805 fourcc = V4L2_PIX_FMT_MPEG1;
1806 break;
1807 case 2:
1808 fourcc = V4L2_PIX_FMT_MPEG2;
1809 break;
1810 case 4:
1811 fourcc = V4L2_PIX_FMT_MPEG4;
1812 fourcc_nc = V4L2_PIX_FMT_XVID;
1813 break;
1814 default:
1815 break;
1816 }
1817 }
1818 } else if (g_str_equal (mimetype, "video/x-fwht")) {
1819 fourcc = V4L2_PIX_FMT_FWHT;
1820 } else if (g_str_equal (mimetype, "video/x-h263")) {
1821 fourcc = V4L2_PIX_FMT_H263;
1822 } else if (g_str_equal (mimetype, "video/x-h264")) {
1823 const gchar *stream_format =
1824 gst_structure_get_string (structure, "stream-format");
1825 if (g_str_equal (stream_format, "avc"))
1826 fourcc = V4L2_PIX_FMT_H264_NO_SC;
1827 else
1828 fourcc = V4L2_PIX_FMT_H264;
1829 } else if (g_str_equal (mimetype, "video/x-h265")) {
1830 fourcc = V4L2_PIX_FMT_HEVC;
1831 } else if (g_str_equal (mimetype, "video/x-vp8")) {
1832 fourcc = V4L2_PIX_FMT_VP8;
1833 } else if (g_str_equal (mimetype, "video/x-vp9")) {
1834 fourcc = V4L2_PIX_FMT_VP9;
1835 } else if (g_str_equal (mimetype, "video/x-bayer")) {
1836 const gchar *format = gst_structure_get_string (structure, "format");
1837 if (format) {
1838 if (!g_ascii_strcasecmp (format, "bggr"))
1839 fourcc = V4L2_PIX_FMT_SBGGR8;
1840 else if (!g_ascii_strcasecmp (format, "gbrg"))
1841 fourcc = V4L2_PIX_FMT_SGBRG8;
1842 else if (!g_ascii_strcasecmp (format, "grbg"))
1843 fourcc = V4L2_PIX_FMT_SGRBG8;
1844 else if (!g_ascii_strcasecmp (format, "rggb"))
1845 fourcc = V4L2_PIX_FMT_SRGGB8;
1846 }
1847 } else if (g_str_equal (mimetype, "video/x-sonix")) {
1848 fourcc = V4L2_PIX_FMT_SN9C10X;
1849 } else if (g_str_equal (mimetype, "video/x-pwc1")) {
1850 fourcc = V4L2_PIX_FMT_PWC1;
1851 } else if (g_str_equal (mimetype, "video/x-pwc2")) {
1852 fourcc = V4L2_PIX_FMT_PWC2;
1853 }
1854 }
1855
1856
1857 /* Prefer the non-contiguous if supported */
1858 v4l2object->prefered_non_contiguous = TRUE;
1859
1860 if (fourcc_nc)
1861 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc_nc);
1862 else if (fourcc == 0)
1863 goto unhandled_format;
1864
1865 if (fmt == NULL) {
1866 fmt = gst_v4l2_object_get_format_from_fourcc (v4l2object, fourcc);
1867 v4l2object->prefered_non_contiguous = FALSE;
1868 }
1869
1870 if (fmt == NULL)
1871 goto unsupported_format;
1872
1873 *format = fmt;
1874
1875 return TRUE;
1876
1877 /* ERRORS */
1878 invalid_format:
1879 {
1880 GST_DEBUG_OBJECT (v4l2object, "invalid format");
1881 return FALSE;
1882 }
1883 unhandled_format:
1884 {
1885 GST_DEBUG_OBJECT (v4l2object, "unhandled format");
1886 return FALSE;
1887 }
1888 unsupported_format:
1889 {
1890 GST_DEBUG_OBJECT (v4l2object, "unsupported format");
1891 return FALSE;
1892 }
1893 }
1894
1895 static gboolean
1896 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
1897 guint32 pixelformat, gint * width, gint * height);
1898
1899 static void
gst_v4l2_object_add_aspect_ratio(GstV4l2Object * v4l2object,GstStructure * s)1900 gst_v4l2_object_add_aspect_ratio (GstV4l2Object * v4l2object, GstStructure * s)
1901 {
1902 if (v4l2object->keep_aspect && v4l2object->par)
1903 gst_structure_set_value (s, "pixel-aspect-ratio", v4l2object->par);
1904 }
1905
1906 /* returns TRUE if the value was changed in place, otherwise FALSE */
1907 static gboolean
gst_v4l2src_value_simplify(GValue * val)1908 gst_v4l2src_value_simplify (GValue * val)
1909 {
1910 /* simplify list of one value to one value */
1911 if (GST_VALUE_HOLDS_LIST (val) && gst_value_list_get_size (val) == 1) {
1912 const GValue *list_val;
1913 GValue new_val = G_VALUE_INIT;
1914
1915 list_val = gst_value_list_get_value (val, 0);
1916 g_value_init (&new_val, G_VALUE_TYPE (list_val));
1917 g_value_copy (list_val, &new_val);
1918 g_value_unset (val);
1919 *val = new_val;
1920 return TRUE;
1921 }
1922
1923 return FALSE;
1924 }
1925
1926 static gboolean
gst_v4l2_object_get_interlace_mode(enum v4l2_field field,GstVideoInterlaceMode * interlace_mode)1927 gst_v4l2_object_get_interlace_mode (enum v4l2_field field,
1928 GstVideoInterlaceMode * interlace_mode)
1929 {
1930 switch (field) {
1931 case V4L2_FIELD_ANY:
1932 GST_ERROR
1933 ("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
1934 /* fallthrough */
1935 case V4L2_FIELD_NONE:
1936 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1937 return TRUE;
1938 case V4L2_FIELD_INTERLACED:
1939 case V4L2_FIELD_INTERLACED_TB:
1940 case V4L2_FIELD_INTERLACED_BT:
1941 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1942 return TRUE;
1943 default:
1944 GST_ERROR ("Unknown enum v4l2_field %d", field);
1945 return FALSE;
1946 }
1947 }
1948
1949 static gboolean
gst_v4l2_object_get_colorspace(struct v4l2_format * fmt,GstVideoColorimetry * cinfo)1950 gst_v4l2_object_get_colorspace (struct v4l2_format *fmt,
1951 GstVideoColorimetry * cinfo)
1952 {
1953 gboolean is_rgb =
1954 gst_v4l2_object_v4l2fourcc_is_rgb (fmt->fmt.pix.pixelformat);
1955 enum v4l2_colorspace colorspace;
1956 enum v4l2_quantization range;
1957 enum v4l2_ycbcr_encoding matrix;
1958 enum v4l2_xfer_func transfer;
1959 gboolean ret = TRUE;
1960
1961 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
1962 colorspace = fmt->fmt.pix_mp.colorspace;
1963 range = fmt->fmt.pix_mp.quantization;
1964 matrix = fmt->fmt.pix_mp.ycbcr_enc;
1965 transfer = fmt->fmt.pix_mp.xfer_func;
1966 } else {
1967 colorspace = fmt->fmt.pix.colorspace;
1968 range = fmt->fmt.pix.quantization;
1969 matrix = fmt->fmt.pix.ycbcr_enc;
1970 transfer = fmt->fmt.pix.xfer_func;
1971 }
1972
1973 /* First step, set the defaults for each primaries */
1974 switch (colorspace) {
1975 case V4L2_COLORSPACE_SMPTE170M:
1976 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1977 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1978 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1979 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1980 break;
1981 case V4L2_COLORSPACE_REC709:
1982 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1983 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1984 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
1985 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1986 break;
1987 case V4L2_COLORSPACE_SRGB:
1988 case V4L2_COLORSPACE_JPEG:
1989 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
1990 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1991 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
1992 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1993 break;
1994 case V4L2_COLORSPACE_OPRGB:
1995 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
1996 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1997 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
1998 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
1999 break;
2000 case V4L2_COLORSPACE_BT2020:
2001 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2002 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2003 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2004 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
2005 break;
2006 case V4L2_COLORSPACE_SMPTE240M:
2007 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2008 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2009 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2010 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
2011 break;
2012 case V4L2_COLORSPACE_470_SYSTEM_M:
2013 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2014 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2015 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2016 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
2017 break;
2018 case V4L2_COLORSPACE_470_SYSTEM_BG:
2019 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2020 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2021 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2022 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
2023 break;
2024 case V4L2_COLORSPACE_RAW:
2025 /* Explicitly unknown */
2026 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2027 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2028 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2029 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
2030 break;
2031 default:
2032 GST_DEBUG ("Unknown enum v4l2_colorspace %d", colorspace);
2033 ret = FALSE;
2034 break;
2035 }
2036
2037 if (!ret)
2038 goto done;
2039
2040 /* Second step, apply any custom variation */
2041 switch (range) {
2042 case V4L2_QUANTIZATION_FULL_RANGE:
2043 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2044 break;
2045 case V4L2_QUANTIZATION_LIM_RANGE:
2046 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2047 break;
2048 case V4L2_QUANTIZATION_DEFAULT:
2049 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
2050 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
2051 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2052 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601
2053 || matrix == V4L2_YCBCR_ENC_XV709
2054 || colorspace == V4L2_COLORSPACE_JPEG)
2055 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2056 else
2057 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2058 break;
2059 default:
2060 GST_WARNING ("Unknown enum v4l2_quantization value %d", range);
2061 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2062 break;
2063 }
2064
2065 switch (matrix) {
2066 case V4L2_YCBCR_ENC_XV601:
2067 case V4L2_YCBCR_ENC_SYCC:
2068 GST_FIXME ("XV601 and SYCC not defined, assuming 601");
2069 /* fallthrough */
2070 case V4L2_YCBCR_ENC_601:
2071 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2072 break;
2073 case V4L2_YCBCR_ENC_XV709:
2074 GST_FIXME ("XV709 not defined, assuming 709");
2075 /* fallthrough */
2076 case V4L2_YCBCR_ENC_709:
2077 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2078 break;
2079 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2080 GST_FIXME ("BT2020 with constant luma is not defined, assuming BT2020");
2081 /* fallthrough */
2082 case V4L2_YCBCR_ENC_BT2020:
2083 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2084 break;
2085 case V4L2_YCBCR_ENC_SMPTE240M:
2086 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2087 break;
2088 case V4L2_YCBCR_ENC_DEFAULT:
2089 /* nothing, just use defaults for colorspace */
2090 break;
2091 default:
2092 GST_WARNING ("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2093 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2094 break;
2095 }
2096
2097 /* Set identity matrix for R'G'B' formats to avoid creating
2098 * confusion. This though is cosmetic as it's now properly ignored by
2099 * the video info API and videoconvert. */
2100 if (is_rgb)
2101 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2102
2103 switch (transfer) {
2104 case V4L2_XFER_FUNC_709:
2105 if (colorspace == V4L2_COLORSPACE_BT2020 && fmt->fmt.pix.height >= 2160)
2106 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2107 else
2108 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2109 break;
2110 case V4L2_XFER_FUNC_SRGB:
2111 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2112 break;
2113 case V4L2_XFER_FUNC_OPRGB:
2114 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2115 break;
2116 case V4L2_XFER_FUNC_SMPTE240M:
2117 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2118 break;
2119 case V4L2_XFER_FUNC_NONE:
2120 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2121 break;
2122 case V4L2_XFER_FUNC_DEFAULT:
2123 /* nothing, just use defaults for colorspace */
2124 break;
2125 default:
2126 GST_WARNING ("Unknown enum v4l2_xfer_func value %d", transfer);
2127 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2128 break;
2129 }
2130
2131 done:
2132 return ret;
2133 }
2134
2135 static int
gst_v4l2_object_try_fmt(GstV4l2Object * v4l2object,struct v4l2_format * try_fmt)2136 gst_v4l2_object_try_fmt (GstV4l2Object * v4l2object,
2137 struct v4l2_format *try_fmt)
2138 {
2139 int fd = v4l2object->video_fd;
2140 struct v4l2_format fmt;
2141 int r;
2142
2143 memcpy (&fmt, try_fmt, sizeof (fmt));
2144 r = v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2145
2146 if (r < 0 && errno == ENOTTY) {
2147 /* The driver might not implement TRY_FMT, in which case we will try
2148 S_FMT to probe */
2149 if (GST_V4L2_IS_ACTIVE (v4l2object))
2150 goto error;
2151
2152 memcpy (&fmt, try_fmt, sizeof (fmt));
2153 r = v4l2object->ioctl (fd, VIDIOC_S_FMT, &fmt);
2154 }
2155 memcpy (try_fmt, &fmt, sizeof (fmt));
2156
2157 return r;
2158
2159 error:
2160 memcpy (try_fmt, &fmt, sizeof (fmt));
2161 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2162 "Unable to try format: %s", g_strerror (errno));
2163 return r;
2164 }
2165
2166
2167 static void
gst_v4l2_object_add_interlace_mode(GstV4l2Object * v4l2object,GstStructure * s,guint32 width,guint32 height,guint32 pixelformat)2168 gst_v4l2_object_add_interlace_mode (GstV4l2Object * v4l2object,
2169 GstStructure * s, guint32 width, guint32 height, guint32 pixelformat)
2170 {
2171 struct v4l2_format fmt;
2172 GValue interlace_formats = { 0, };
2173 enum v4l2_field formats[] = { V4L2_FIELD_NONE, V4L2_FIELD_INTERLACED };
2174 gsize i;
2175 GstVideoInterlaceMode interlace_mode, prev = -1;
2176
2177 if (!g_str_equal (gst_structure_get_name (s), "video/x-raw"))
2178 return;
2179
2180 if (v4l2object->never_interlaced) {
2181 gst_structure_set (s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2182 return;
2183 }
2184
2185 g_value_init (&interlace_formats, GST_TYPE_LIST);
2186
2187 /* Try twice - once for NONE, once for INTERLACED. */
2188 for (i = 0; i < G_N_ELEMENTS (formats); i++) {
2189 memset (&fmt, 0, sizeof (fmt));
2190 fmt.type = v4l2object->type;
2191 fmt.fmt.pix.width = width;
2192 fmt.fmt.pix.height = height;
2193 fmt.fmt.pix.pixelformat = pixelformat;
2194 fmt.fmt.pix.field = formats[i];
2195
2196 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0 &&
2197 gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)
2198 && prev != interlace_mode) {
2199 GValue interlace_enum = { 0, };
2200 const gchar *mode_string;
2201 g_value_init (&interlace_enum, G_TYPE_STRING);
2202 mode_string = gst_video_interlace_mode_to_string (interlace_mode);
2203 g_value_set_string (&interlace_enum, mode_string);
2204 gst_value_list_append_and_take_value (&interlace_formats,
2205 &interlace_enum);
2206 prev = interlace_mode;
2207 }
2208 }
2209
2210 if (gst_v4l2src_value_simplify (&interlace_formats)
2211 || gst_value_list_get_size (&interlace_formats) > 0)
2212 gst_structure_take_value (s, "interlace-mode", &interlace_formats);
2213 else
2214 GST_WARNING_OBJECT (v4l2object, "Failed to determine interlace mode");
2215
2216 return;
2217 }
2218
2219 static void
gst_v4l2_object_fill_colorimetry_list(GValue * list,GstVideoColorimetry * cinfo)2220 gst_v4l2_object_fill_colorimetry_list (GValue * list,
2221 GstVideoColorimetry * cinfo)
2222 {
2223 GValue colorimetry = G_VALUE_INIT;
2224 guint size;
2225 guint i;
2226 gboolean found = FALSE;
2227
2228 g_value_init (&colorimetry, G_TYPE_STRING);
2229 g_value_take_string (&colorimetry, gst_video_colorimetry_to_string (cinfo));
2230
2231 /* only insert if no duplicate */
2232 size = gst_value_list_get_size (list);
2233 for (i = 0; i < size; i++) {
2234 const GValue *tmp;
2235
2236 tmp = gst_value_list_get_value (list, i);
2237 if (gst_value_compare (&colorimetry, tmp) == GST_VALUE_EQUAL) {
2238 found = TRUE;
2239 break;
2240 }
2241 }
2242
2243 if (!found)
2244 gst_value_list_append_and_take_value (list, &colorimetry);
2245 else
2246 g_value_unset (&colorimetry);
2247 }
2248
2249 static void
gst_v4l2_object_add_colorspace(GstV4l2Object * v4l2object,GstStructure * s,guint32 width,guint32 height,guint32 pixelformat)2250 gst_v4l2_object_add_colorspace (GstV4l2Object * v4l2object, GstStructure * s,
2251 guint32 width, guint32 height, guint32 pixelformat)
2252 {
2253 struct v4l2_format fmt;
2254 GValue list = G_VALUE_INIT;
2255 GstVideoColorimetry cinfo;
2256 enum v4l2_colorspace req_cspace;
2257
2258 memset (&fmt, 0, sizeof (fmt));
2259 fmt.type = v4l2object->type;
2260 fmt.fmt.pix.width = width;
2261 fmt.fmt.pix.height = height;
2262 fmt.fmt.pix.pixelformat = pixelformat;
2263
2264 g_value_init (&list, GST_TYPE_LIST);
2265
2266 /* step 1: get device default colorspace and insert it first as
2267 * it should be the preferred one */
2268 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2269 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2270 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2271 }
2272
2273 /* step 2: probe all colorspace other than default
2274 * We don't probe all colorspace, range, matrix and transfer combination to
2275 * avoid ioctl flooding which could greatly increase initialization time
2276 * with low-speed devices (UVC...) */
2277 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2278 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++) {
2279 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2280 if (req_cspace == V4L2_COLORSPACE_BT878)
2281 continue;
2282
2283 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2284 fmt.fmt.pix_mp.colorspace = req_cspace;
2285 else
2286 fmt.fmt.pix.colorspace = req_cspace;
2287
2288 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) == 0) {
2289 enum v4l2_colorspace colorspace;
2290
2291 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
2292 colorspace = fmt.fmt.pix_mp.colorspace;
2293 else
2294 colorspace = fmt.fmt.pix.colorspace;
2295
2296 if (colorspace == req_cspace) {
2297 if (gst_v4l2_object_get_colorspace (&fmt, &cinfo))
2298 gst_v4l2_object_fill_colorimetry_list (&list, &cinfo);
2299 }
2300 }
2301 }
2302
2303 if (gst_value_list_get_size (&list) > 0)
2304 gst_structure_take_value (s, "colorimetry", &list);
2305 else
2306 g_value_unset (&list);
2307
2308 return;
2309 }
2310
2311 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
2312 static GstStructure *
gst_v4l2_object_probe_caps_for_format_and_size(GstV4l2Object * v4l2object,guint32 pixelformat,guint32 width,guint32 height,const GstStructure * template)2313 gst_v4l2_object_probe_caps_for_format_and_size (GstV4l2Object * v4l2object,
2314 guint32 pixelformat,
2315 guint32 width, guint32 height, const GstStructure * template)
2316 {
2317 gint fd = v4l2object->video_fd;
2318 struct v4l2_frmivalenum ival;
2319 guint32 num, denom;
2320 GstStructure *s;
2321 GValue rates = { 0, };
2322
2323 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
2324 ival.index = 0;
2325 ival.pixel_format = pixelformat;
2326 ival.width = width;
2327 ival.height = height;
2328
2329 GST_LOG_OBJECT (v4l2object->dbg_obj,
2330 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2331 GST_FOURCC_ARGS (pixelformat));
2332
2333 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2334 * fraction to get framerate */
2335 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2336 goto enum_frameintervals_failed;
2337
2338 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
2339 GValue rate = { 0, };
2340
2341 g_value_init (&rates, GST_TYPE_LIST);
2342 g_value_init (&rate, GST_TYPE_FRACTION);
2343
2344 do {
2345 num = ival.discrete.numerator;
2346 denom = ival.discrete.denominator;
2347
2348 if (num > G_MAXINT || denom > G_MAXINT) {
2349 /* let us hope we don't get here... */
2350 num >>= 1;
2351 denom >>= 1;
2352 }
2353
2354 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding discrete framerate: %d/%d",
2355 denom, num);
2356
2357 /* swap to get the framerate */
2358 gst_value_set_fraction (&rate, denom, num);
2359 gst_value_list_append_value (&rates, &rate);
2360
2361 ival.index++;
2362 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2363 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
2364 GValue min = { 0, };
2365 GValue step = { 0, };
2366 GValue max = { 0, };
2367 gboolean added = FALSE;
2368 guint32 minnum, mindenom;
2369 guint32 maxnum, maxdenom;
2370
2371 g_value_init (&rates, GST_TYPE_LIST);
2372
2373 g_value_init (&min, GST_TYPE_FRACTION);
2374 g_value_init (&step, GST_TYPE_FRACTION);
2375 g_value_init (&max, GST_TYPE_FRACTION);
2376
2377 /* get the min */
2378 minnum = ival.stepwise.min.numerator;
2379 mindenom = ival.stepwise.min.denominator;
2380 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
2381 minnum >>= 1;
2382 mindenom >>= 1;
2383 }
2384 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise min frame interval: %d/%d",
2385 minnum, mindenom);
2386 gst_value_set_fraction (&min, minnum, mindenom);
2387
2388 /* get the max */
2389 maxnum = ival.stepwise.max.numerator;
2390 maxdenom = ival.stepwise.max.denominator;
2391 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2392 maxnum >>= 1;
2393 maxdenom >>= 1;
2394 }
2395
2396 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise max frame interval: %d/%d",
2397 maxnum, maxdenom);
2398 gst_value_set_fraction (&max, maxnum, maxdenom);
2399
2400 /* get the step */
2401 num = ival.stepwise.step.numerator;
2402 denom = ival.stepwise.step.denominator;
2403 if (num > G_MAXINT || denom > G_MAXINT) {
2404 num >>= 1;
2405 denom >>= 1;
2406 }
2407
2408 if (num == 0 || denom == 0) {
2409 /* in this case we have a wrong fraction or no step, set the step to max
2410 * so that we only add the min value in the loop below */
2411 num = maxnum;
2412 denom = maxdenom;
2413 }
2414
2415 /* since we only have gst_value_fraction_subtract and not add, negate the
2416 * numerator */
2417 GST_LOG_OBJECT (v4l2object->dbg_obj, "stepwise step frame interval: %d/%d",
2418 num, denom);
2419 gst_value_set_fraction (&step, -num, denom);
2420
2421 while (gst_value_compare (&min, &max) != GST_VALUE_GREATER_THAN) {
2422 GValue rate = { 0, };
2423
2424 num = gst_value_get_fraction_numerator (&min);
2425 denom = gst_value_get_fraction_denominator (&min);
2426 GST_LOG_OBJECT (v4l2object->dbg_obj, "adding stepwise framerate: %d/%d",
2427 denom, num);
2428
2429 /* invert to get the framerate */
2430 g_value_init (&rate, GST_TYPE_FRACTION);
2431 gst_value_set_fraction (&rate, denom, num);
2432 gst_value_list_append_value (&rates, &rate);
2433 added = TRUE;
2434
2435 /* we're actually adding because step was negated above. This is because
2436 * there is no _add function... */
2437 if (!gst_value_fraction_subtract (&min, &min, &step)) {
2438 GST_WARNING_OBJECT (v4l2object->dbg_obj, "could not step fraction!");
2439 break;
2440 }
2441 }
2442 if (!added) {
2443 /* no range was added, leave the default range from the template */
2444 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2445 "no range added, leaving default");
2446 g_value_unset (&rates);
2447 }
2448 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
2449 guint32 maxnum, maxdenom;
2450
2451 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
2452
2453 num = ival.stepwise.min.numerator;
2454 denom = ival.stepwise.min.denominator;
2455 if (num > G_MAXINT || denom > G_MAXINT) {
2456 num >>= 1;
2457 denom >>= 1;
2458 }
2459
2460 maxnum = ival.stepwise.max.numerator;
2461 maxdenom = ival.stepwise.max.denominator;
2462 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
2463 maxnum >>= 1;
2464 maxdenom >>= 1;
2465 }
2466
2467 GST_LOG_OBJECT (v4l2object->dbg_obj,
2468 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2469 num);
2470
2471 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
2472 } else {
2473 goto unknown_type;
2474 }
2475
2476 return_data:
2477 s = gst_structure_copy (template);
2478 gst_structure_set (s, "width", G_TYPE_INT, (gint) width,
2479 "height", G_TYPE_INT, (gint) height, NULL);
2480
2481 gst_v4l2_object_add_aspect_ratio (v4l2object, s);
2482
2483 if (!v4l2object->skip_try_fmt_probes) {
2484 gst_v4l2_object_add_interlace_mode (v4l2object, s, width, height,
2485 pixelformat);
2486 gst_v4l2_object_add_colorspace (v4l2object, s, width, height, pixelformat);
2487 }
2488
2489 if (G_IS_VALUE (&rates)) {
2490 gst_v4l2src_value_simplify (&rates);
2491 /* only change the framerate on the template when we have a valid probed new
2492 * value */
2493 gst_structure_take_value (s, "framerate", &rates);
2494 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2495 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2496 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2497 1, NULL);
2498 }
2499 return s;
2500
2501 /* ERRORS */
2502 enum_frameintervals_failed:
2503 {
2504 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2505 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2506 GST_FOURCC_ARGS (pixelformat), width, height);
2507 goto return_data;
2508 }
2509 unknown_type:
2510 {
2511 /* I don't see how this is actually an error, we ignore the format then */
2512 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2513 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2514 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
2515 return NULL;
2516 }
2517 }
2518
2519 static gint
sort_by_frame_size(GstStructure * s1,GstStructure * s2)2520 sort_by_frame_size (GstStructure * s1, GstStructure * s2)
2521 {
2522 int w1, h1, w2, h2;
2523
2524 gst_structure_get_int (s1, "width", &w1);
2525 gst_structure_get_int (s1, "height", &h1);
2526 gst_structure_get_int (s2, "width", &w2);
2527 gst_structure_get_int (s2, "height", &h2);
2528
2529 /* I think it's safe to assume that this won't overflow for a while */
2530 return ((w2 * h2) - (w1 * h1));
2531 }
2532
2533 static void
gst_v4l2_object_update_and_append(GstV4l2Object * v4l2object,guint32 format,GstCaps * caps,GstStructure * s)2534 gst_v4l2_object_update_and_append (GstV4l2Object * v4l2object,
2535 guint32 format, GstCaps * caps, GstStructure * s)
2536 {
2537 GstStructure *alt_s = NULL;
2538
2539 /* Encoded stream on output buffer need to be parsed */
2540 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2541 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2542 gint i = 0;
2543
2544 for (; i < GST_V4L2_FORMAT_COUNT; i++) {
2545 if (format == gst_v4l2_formats[i].format &&
2546 gst_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2547 !(gst_v4l2_formats[i].flags & GST_V4L2_NO_PARSE)) {
2548 gst_structure_set (s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2549 break;
2550 }
2551 }
2552 }
2553
2554 if (v4l2object->has_alpha_component &&
2555 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2556 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2557 switch (format) {
2558 case V4L2_PIX_FMT_RGB32:
2559 alt_s = gst_structure_copy (s);
2560 gst_structure_set (alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2561 break;
2562 case V4L2_PIX_FMT_BGR32:
2563 alt_s = gst_structure_copy (s);
2564 gst_structure_set (alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2565 break;
2566 default:
2567 break;
2568 }
2569 }
2570
2571 gst_caps_append_structure (caps, s);
2572
2573 if (alt_s)
2574 gst_caps_append_structure (caps, alt_s);
2575 }
2576
2577 static GstCaps *
gst_v4l2_object_probe_caps_for_format(GstV4l2Object * v4l2object,guint32 pixelformat,const GstStructure * template)2578 gst_v4l2_object_probe_caps_for_format (GstV4l2Object * v4l2object,
2579 guint32 pixelformat, const GstStructure * template)
2580 {
2581 GstCaps *ret = gst_caps_new_empty ();
2582 GstStructure *tmp;
2583 gint fd = v4l2object->video_fd;
2584 struct v4l2_frmsizeenum size;
2585 GList *results = NULL;
2586 guint32 w, h;
2587
2588 if (pixelformat == GST_MAKE_FOURCC ('M', 'P', 'E', 'G')) {
2589 gst_caps_append_structure (ret, gst_structure_copy (template));
2590 return ret;
2591 }
2592
2593 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
2594 size.index = 0;
2595 size.pixel_format = pixelformat;
2596
2597 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2598 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2599 GST_FOURCC_ARGS (pixelformat));
2600
2601 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2602 goto enum_framesizes_failed;
2603
2604 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
2605 do {
2606 GST_LOG_OBJECT (v4l2object->dbg_obj, "got discrete frame size %dx%d",
2607 size.discrete.width, size.discrete.height);
2608
2609 w = MIN (size.discrete.width, G_MAXINT);
2610 h = MIN (size.discrete.height, G_MAXINT);
2611
2612 if (w && h) {
2613 tmp =
2614 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2615 pixelformat, w, h, template);
2616
2617 if (tmp)
2618 results = g_list_prepend (results, tmp);
2619 }
2620
2621 size.index++;
2622 } while (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2623 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2624 "done iterating discrete frame sizes");
2625 } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
2626 guint32 maxw, maxh, step_w, step_h;
2627
2628 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have stepwise frame sizes:");
2629 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2630 size.stepwise.min_width);
2631 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2632 size.stepwise.min_height);
2633 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2634 size.stepwise.max_width);
2635 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2636 size.stepwise.max_height);
2637 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step width: %d",
2638 size.stepwise.step_width);
2639 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "step height: %d",
2640 size.stepwise.step_height);
2641
2642 w = MAX (size.stepwise.min_width, 1);
2643 h = MAX (size.stepwise.min_height, 1);
2644 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2645 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2646
2647 step_w = MAX (size.stepwise.step_width, 1);
2648 step_h = MAX (size.stepwise.step_height, 1);
2649
2650 /* FIXME: check for sanity and that min/max are multiples of the steps */
2651
2652 /* we only query details for the max width/height since it's likely the
2653 * most restricted if there are any resolution-dependent restrictions */
2654 tmp = gst_v4l2_object_probe_caps_for_format_and_size (v4l2object,
2655 pixelformat, maxw, maxh, template);
2656
2657 if (tmp) {
2658 GValue step_range = G_VALUE_INIT;
2659
2660 g_value_init (&step_range, GST_TYPE_INT_RANGE);
2661 gst_value_set_int_range_step (&step_range, w, maxw, step_w);
2662 gst_structure_set_value (tmp, "width", &step_range);
2663
2664 gst_value_set_int_range_step (&step_range, h, maxh, step_h);
2665 gst_structure_take_value (tmp, "height", &step_range);
2666
2667 /* no point using the results list here, since there's only one struct */
2668 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2669 }
2670 } else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
2671 guint32 maxw, maxh;
2672
2673 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "we have continuous frame sizes:");
2674 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min width: %d",
2675 size.stepwise.min_width);
2676 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2677 size.stepwise.min_height);
2678 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "max width: %d",
2679 size.stepwise.max_width);
2680 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "min height: %d",
2681 size.stepwise.max_height);
2682
2683 w = MAX (size.stepwise.min_width, 1);
2684 h = MAX (size.stepwise.min_height, 1);
2685 maxw = MIN (size.stepwise.max_width, G_MAXINT);
2686 maxh = MIN (size.stepwise.max_height, G_MAXINT);
2687
2688 tmp =
2689 gst_v4l2_object_probe_caps_for_format_and_size (v4l2object, pixelformat,
2690 w, h, template);
2691 if (tmp) {
2692 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, (gint) w,
2693 (gint) maxw, "height", GST_TYPE_INT_RANGE, (gint) h, (gint) maxh,
2694 NULL);
2695
2696 /* no point using the results list here, since there's only one struct */
2697 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2698 }
2699 } else {
2700 goto unknown_type;
2701 }
2702
2703 /* we use an intermediary list to store and then sort the results of the
2704 * probing because we can't make any assumptions about the order in which
2705 * the driver will give us the sizes, but we want the final caps to contain
2706 * the results starting with the highest resolution and having the lowest
2707 * resolution last, since order in caps matters for things like fixation. */
2708 results = g_list_sort (results, (GCompareFunc) sort_by_frame_size);
2709 while (results != NULL) {
2710 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret,
2711 results->data);
2712 results = g_list_delete_link (results, results);
2713 }
2714
2715 if (gst_caps_is_empty (ret))
2716 goto enum_framesizes_no_results;
2717
2718 return ret;
2719
2720 /* ERRORS */
2721 enum_framesizes_failed:
2722 {
2723 /* I don't see how this is actually an error */
2724 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2725 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2726 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
2727 goto default_frame_sizes;
2728 }
2729 enum_framesizes_no_results:
2730 {
2731 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2732 * question doesn't actually support it yet */
2733 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2734 "No results for pixelformat %" GST_FOURCC_FORMAT
2735 " enumerating frame sizes, trying fallback",
2736 GST_FOURCC_ARGS (pixelformat));
2737 goto default_frame_sizes;
2738 }
2739 unknown_type:
2740 {
2741 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2742 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2743 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2744 goto default_frame_sizes;
2745 }
2746
2747 default_frame_sizes:
2748 {
2749 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2750
2751 /* This code is for Linux < 2.6.19 */
2752 min_w = min_h = 1;
2753 max_w = max_h = GST_V4L2_MAX_SIZE;
2754 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &min_w,
2755 &min_h)) {
2756 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2757 "Could not probe minimum capture size for pixelformat %"
2758 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2759 }
2760 if (!gst_v4l2_object_get_nearest_size (v4l2object, pixelformat, &max_w,
2761 &max_h)) {
2762 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2763 "Could not probe maximum capture size for pixelformat %"
2764 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2765 }
2766
2767 /* Since we can't get framerate directly, try to use the current norm */
2768 if (v4l2object->tv_norm && v4l2object->norms) {
2769 GList *norms;
2770 GstTunerNorm *norm = NULL;
2771 GstTunerNorm *current =
2772 gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
2773
2774 for (norms = v4l2object->norms; norms != NULL; norms = norms->next) {
2775 norm = (GstTunerNorm *) norms->data;
2776 if (!strcmp (norm->label, current->label))
2777 break;
2778 }
2779 /* If it's possible, set framerate to that (discrete) value */
2780 if (norm) {
2781 fix_num = gst_value_get_fraction_numerator (&norm->framerate);
2782 fix_denom = gst_value_get_fraction_denominator (&norm->framerate);
2783 }
2784 }
2785
2786 tmp = gst_structure_copy (template);
2787 if (fix_num) {
2788 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2789 fix_denom, NULL);
2790 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2791 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2792 /* if norm can't be used, copy the template framerate */
2793 gst_structure_set (tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2794 G_MAXINT, 1, NULL);
2795 }
2796
2797 if (min_w == max_w)
2798 gst_structure_set (tmp, "width", G_TYPE_INT, max_w, NULL);
2799 else
2800 gst_structure_set (tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2801
2802 if (min_h == max_h)
2803 gst_structure_set (tmp, "height", G_TYPE_INT, max_h, NULL);
2804 else
2805 gst_structure_set (tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2806
2807 gst_v4l2_object_add_aspect_ratio (v4l2object, tmp);
2808
2809 if (!v4l2object->skip_try_fmt_probes) {
2810 /* We could consider setting interlace mode from min and max. */
2811 gst_v4l2_object_add_interlace_mode (v4l2object, tmp, max_w, max_h,
2812 pixelformat);
2813 /* We could consider to check colorspace for min too, in case it depends on
2814 * the size. But in this case, min and max could not be enough */
2815 gst_v4l2_object_add_colorspace (v4l2object, tmp, max_w, max_h,
2816 pixelformat);
2817 }
2818
2819 gst_v4l2_object_update_and_append (v4l2object, pixelformat, ret, tmp);
2820 return ret;
2821 }
2822 }
2823
2824 static gboolean
gst_v4l2_object_get_nearest_size(GstV4l2Object * v4l2object,guint32 pixelformat,gint * width,gint * height)2825 gst_v4l2_object_get_nearest_size (GstV4l2Object * v4l2object,
2826 guint32 pixelformat, gint * width, gint * height)
2827 {
2828 struct v4l2_format fmt;
2829 gboolean ret = FALSE;
2830 GstVideoInterlaceMode interlace_mode;
2831
2832 g_return_val_if_fail (width != NULL, FALSE);
2833 g_return_val_if_fail (height != NULL, FALSE);
2834
2835 GST_LOG_OBJECT (v4l2object->dbg_obj,
2836 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2837 *width, *height, GST_FOURCC_ARGS (pixelformat));
2838
2839 memset (&fmt, 0, sizeof (struct v4l2_format));
2840
2841 /* get size delimiters */
2842 memset (&fmt, 0, sizeof (fmt));
2843 fmt.type = v4l2object->type;
2844 fmt.fmt.pix.width = *width;
2845 fmt.fmt.pix.height = *height;
2846 fmt.fmt.pix.pixelformat = pixelformat;
2847 fmt.fmt.pix.field = V4L2_FIELD_ANY;
2848
2849 if (gst_v4l2_object_try_fmt (v4l2object, &fmt) < 0)
2850 goto error;
2851
2852 GST_LOG_OBJECT (v4l2object->dbg_obj,
2853 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2854
2855 *width = fmt.fmt.pix.width;
2856 *height = fmt.fmt.pix.height;
2857
2858 if (!gst_v4l2_object_get_interlace_mode (fmt.fmt.pix.field, &interlace_mode)) {
2859 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2860 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
2861 GST_FOURCC_ARGS (pixelformat), *width, *height, fmt.fmt.pix.field);
2862 goto error;
2863 }
2864
2865 ret = TRUE;
2866
2867 error:
2868 if (!ret) {
2869 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2870 "Unable to try format: %s", g_strerror (errno));
2871 }
2872
2873 return ret;
2874 }
2875
2876 static gboolean
gst_v4l2_object_is_dmabuf_supported(GstV4l2Object * v4l2object)2877 gst_v4l2_object_is_dmabuf_supported (GstV4l2Object * v4l2object)
2878 {
2879 gboolean ret = TRUE;
2880 struct v4l2_exportbuffer expbuf = {
2881 .type = v4l2object->type,
2882 .index = -1,
2883 .plane = -1,
2884 .flags = O_CLOEXEC | O_RDWR,
2885 };
2886
2887 if (v4l2object->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
2888 GST_WARNING_OBJECT (v4l2object->dbg_obj,
2889 "libv4l2 converter detected, disabling DMABuf");
2890 ret = FALSE;
2891 }
2892
2893 /* Expected to fail, but ENOTTY tells us that it is not implemented. */
2894 /* FreeBSD cuse sends EINVAL? */
2895 v4l2object->ioctl (v4l2object->video_fd, VIDIOC_EXPBUF, &expbuf);
2896 if (errno == ENOTTY || errno == EINVAL)
2897 ret = FALSE;
2898
2899 return ret;
2900 }
2901
2902 static gboolean
gst_v4l2_object_setup_pool(GstV4l2Object * v4l2object,GstCaps * caps)2903 gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object, GstCaps * caps)
2904 {
2905 GstV4l2IOMode mode;
2906
2907 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "initializing the %s system",
2908 V4L2_TYPE_IS_OUTPUT (v4l2object->type) ? "output" : "capture");
2909
2910 GST_V4L2_CHECK_OPEN (v4l2object);
2911 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
2912
2913 /* find transport */
2914 mode = v4l2object->req_mode;
2915
2916 if (v4l2object->device_caps & V4L2_CAP_READWRITE) {
2917 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
2918 mode = GST_V4L2_IO_RW;
2919 } else if (v4l2object->req_mode == GST_V4L2_IO_RW)
2920 goto method_not_supported;
2921
2922 if (v4l2object->device_caps & V4L2_CAP_STREAMING) {
2923 if (v4l2object->req_mode == GST_V4L2_IO_AUTO) {
2924 if (!V4L2_TYPE_IS_OUTPUT (v4l2object->type) &&
2925 gst_v4l2_object_is_dmabuf_supported (v4l2object)) {
2926 mode = GST_V4L2_IO_DMABUF;
2927 } else {
2928 mode = GST_V4L2_IO_MMAP;
2929 }
2930 }
2931 } else if (v4l2object->req_mode == GST_V4L2_IO_MMAP ||
2932 v4l2object->req_mode == GST_V4L2_IO_DMABUF)
2933 goto method_not_supported;
2934
2935 /* if still no transport selected, error out */
2936 if (mode == GST_V4L2_IO_AUTO)
2937 goto no_supported_capture_method;
2938
2939 GST_INFO_OBJECT (v4l2object->dbg_obj, "accessing buffers via mode %d", mode);
2940 v4l2object->mode = mode;
2941
2942 /* If min_buffers is not set, the driver either does not support the control or
2943 it has not been asked yet via propose_allocation/decide_allocation. */
2944 if (!v4l2object->min_buffers)
2945 gst_v4l2_get_driver_min_buffers (v4l2object);
2946
2947 /* Map the buffers */
2948 GST_LOG_OBJECT (v4l2object->dbg_obj, "initiating buffer pool");
2949
2950 if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object, caps)))
2951 goto buffer_pool_new_failed;
2952
2953 GST_V4L2_SET_ACTIVE (v4l2object);
2954
2955 return TRUE;
2956
2957 /* ERRORS */
2958 buffer_pool_new_failed:
2959 {
2960 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2961 (_("Could not map buffers from device '%s'"),
2962 v4l2object->videodev),
2963 ("Failed to create buffer pool: %s", g_strerror (errno)));
2964 return FALSE;
2965 }
2966 method_not_supported:
2967 {
2968 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2969 (_("The driver of device '%s' does not support the IO method %d"),
2970 v4l2object->videodev, mode), (NULL));
2971 return FALSE;
2972 }
2973 no_supported_capture_method:
2974 {
2975 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
2976 (_("The driver of device '%s' does not support any known IO "
2977 "method."), v4l2object->videodev), (NULL));
2978 return FALSE;
2979 }
2980 }
2981
2982 static void
gst_v4l2_object_set_stride(GstVideoInfo * info,GstVideoAlignment * align,gint plane,gint stride)2983 gst_v4l2_object_set_stride (GstVideoInfo * info, GstVideoAlignment * align,
2984 gint plane, gint stride)
2985 {
2986 const GstVideoFormatInfo *finfo = info->finfo;
2987
2988 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
2989 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
2990
2991
2992 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
2993 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
2994 tile_height = 1 << hs;
2995
2996 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
2997 info->height + align->padding_top + align->padding_bottom);
2998 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
2999
3000 x_tiles = stride >> ws;
3001 y_tiles = padded_height >> hs;
3002 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
3003 } else {
3004 info->stride[plane] = stride;
3005 }
3006 }
3007
3008 static void
gst_v4l2_object_extrapolate_info(GstV4l2Object * v4l2object,GstVideoInfo * info,GstVideoAlignment * align,gint stride)3009 gst_v4l2_object_extrapolate_info (GstV4l2Object * v4l2object,
3010 GstVideoInfo * info, GstVideoAlignment * align, gint stride)
3011 {
3012 const GstVideoFormatInfo *finfo = info->finfo;
3013 gint i, estride, padded_height;
3014 gsize offs = 0;
3015
3016 g_return_if_fail (v4l2object->n_v4l2_planes == 1);
3017
3018 padded_height = info->height + align->padding_top + align->padding_bottom;
3019
3020 for (i = 0; i < finfo->n_planes; i++) {
3021 estride = gst_v4l2_object_extrapolate_stride (finfo, i, stride);
3022
3023 gst_v4l2_object_set_stride (info, align, i, estride);
3024
3025 info->offset[i] = offs;
3026 offs += estride *
3027 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, padded_height);
3028
3029 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3030 "Extrapolated for plane %d with base stride %d: "
3031 "stride %d, offset %" G_GSIZE_FORMAT, i, stride, info->stride[i],
3032 info->offset[i]);
3033 }
3034
3035 /* Update the image size according the amount of data we are going to
3036 * read/write. This workaround bugs in driver where the sizeimage provided
3037 * by TRY/S_FMT represent the buffer length (maximum size) rather then the expected
3038 * bytesused (buffer size). */
3039 if (offs < info->size)
3040 info->size = offs;
3041 }
3042
3043 static void
gst_v4l2_object_save_format(GstV4l2Object * v4l2object,struct v4l2_fmtdesc * fmtdesc,struct v4l2_format * format,GstVideoInfo * info,GstVideoAlignment * align)3044 gst_v4l2_object_save_format (GstV4l2Object * v4l2object,
3045 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
3046 GstVideoInfo * info, GstVideoAlignment * align)
3047 {
3048 const GstVideoFormatInfo *finfo = info->finfo;
3049 gboolean standard_stride = TRUE;
3050 gint stride, pstride, padded_width, padded_height, i;
3051
3052 if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_ENCODED) {
3053 v4l2object->n_v4l2_planes = 1;
3054 info->size = format->fmt.pix.sizeimage;
3055 goto store_info;
3056 }
3057
3058 /* adjust right padding */
3059 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type))
3060 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3061 else
3062 stride = format->fmt.pix.bytesperline;
3063
3064 pstride = GST_VIDEO_FORMAT_INFO_PSTRIDE (finfo, 0);
3065 if (pstride) {
3066 padded_width = stride / pstride;
3067 } else {
3068 /* pstride can be 0 for complex formats */
3069 GST_WARNING_OBJECT (v4l2object->element,
3070 "format %s has a pstride of 0, cannot compute padded with",
3071 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
3072 padded_width = stride;
3073 }
3074
3075 if (padded_width < format->fmt.pix.width)
3076 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3077 "Driver bug detected, stride (%d) is too small for the width (%d)",
3078 padded_width, format->fmt.pix.width);
3079
3080 align->padding_right = padded_width - info->width - align->padding_left;
3081
3082 /* adjust bottom padding */
3083 padded_height = format->fmt.pix.height;
3084
3085 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3086 guint hs, tile_height;
3087
3088 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
3089 tile_height = 1 << hs;
3090
3091 padded_height = GST_ROUND_UP_N (padded_height, tile_height);
3092 }
3093
3094 align->padding_bottom = padded_height - info->height - align->padding_top;
3095
3096 /* setup the strides and offset */
3097 if (V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type)) {
3098 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
3099
3100 /* figure out the frame layout */
3101 v4l2object->n_v4l2_planes = MAX (1, pix_mp->num_planes);
3102 info->size = 0;
3103 for (i = 0; i < v4l2object->n_v4l2_planes; i++) {
3104 stride = pix_mp->plane_fmt[i].bytesperline;
3105
3106 if (info->stride[i] != stride)
3107 standard_stride = FALSE;
3108
3109 gst_v4l2_object_set_stride (info, align, i, stride);
3110 info->offset[i] = info->size;
3111 info->size += pix_mp->plane_fmt[i].sizeimage;
3112 }
3113
3114 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3115 if (v4l2object->n_v4l2_planes < finfo->n_planes) {
3116 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3117 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3118 }
3119 } else {
3120 /* only one plane in non-MPLANE mode */
3121 v4l2object->n_v4l2_planes = 1;
3122 info->size = format->fmt.pix.sizeimage;
3123 stride = format->fmt.pix.bytesperline;
3124
3125 if (info->stride[0] != stride)
3126 standard_stride = FALSE;
3127
3128 gst_v4l2_object_extrapolate_info (v4l2object, info, align, stride);
3129 }
3130
3131 /* adjust the offset to take into account left and top */
3132 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
3133 if ((align->padding_left + align->padding_top) > 0)
3134 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3135 "Left and top padding is not permitted for tiled formats");
3136 } else {
3137 for (i = 0; i < finfo->n_planes; i++) {
3138 gint vedge, hedge;
3139
3140 /* FIXME we assume plane as component as this is true for all supported
3141 * format we support. */
3142
3143 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left);
3144 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top);
3145
3146 info->offset[i] += (vedge * info->stride[i]) +
3147 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i));
3148 }
3149 }
3150
3151 store_info:
3152 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got sizeimage %" G_GSIZE_FORMAT,
3153 info->size);
3154
3155 /* to avoid copies we need video meta if there is padding */
3156 v4l2object->need_video_meta =
3157 ((align->padding_top + align->padding_left + align->padding_right +
3158 align->padding_bottom) != 0);
3159
3160 /* ... or if stride is non "standard" */
3161 if (!standard_stride)
3162 v4l2object->need_video_meta = TRUE;
3163
3164 /* ... or also video meta if we use multiple, non-contiguous, planes */
3165 if (v4l2object->n_v4l2_planes > 1)
3166 v4l2object->need_video_meta = TRUE;
3167
3168 v4l2object->info = *info;
3169 v4l2object->align = *align;
3170 v4l2object->format = *format;
3171 v4l2object->fmtdesc = fmtdesc;
3172
3173 /* if we have a framerate pre-calculate duration */
3174 if (info->fps_n > 0 && info->fps_d > 0) {
3175 v4l2object->duration = gst_util_uint64_scale_int (GST_SECOND, info->fps_d,
3176 info->fps_n);
3177 } else {
3178 v4l2object->duration = GST_CLOCK_TIME_NONE;
3179 }
3180 }
3181
3182 gint
gst_v4l2_object_extrapolate_stride(const GstVideoFormatInfo * finfo,gint plane,gint stride)3183 gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
3184 gint plane, gint stride)
3185 {
3186 gint estride;
3187
3188 switch (finfo->format) {
3189 case GST_VIDEO_FORMAT_NV12:
3190 case GST_VIDEO_FORMAT_NV12_64Z32:
3191 case GST_VIDEO_FORMAT_NV21:
3192 case GST_VIDEO_FORMAT_NV16:
3193 case GST_VIDEO_FORMAT_NV61:
3194 case GST_VIDEO_FORMAT_NV24:
3195 estride = (plane == 0 ? 1 : 2) *
3196 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3197 break;
3198 default:
3199 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
3200 break;
3201 }
3202
3203 return estride;
3204 }
3205
3206 static gboolean
gst_v4l2_video_colorimetry_matches(const GstVideoColorimetry * cinfo,const gchar * color)3207 gst_v4l2_video_colorimetry_matches (const GstVideoColorimetry * cinfo,
3208 const gchar * color)
3209 {
3210 GstVideoColorimetry ci;
3211 static const GstVideoColorimetry ci_likely_jpeg = {
3212 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3213 GST_VIDEO_TRANSFER_UNKNOWN, GST_VIDEO_COLOR_PRIMARIES_UNKNOWN
3214 };
3215 static const GstVideoColorimetry ci_jpeg = {
3216 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3217 GST_VIDEO_TRANSFER_SRGB, GST_VIDEO_COLOR_PRIMARIES_BT709
3218 };
3219
3220 if (!gst_video_colorimetry_from_string (&ci, color))
3221 return FALSE;
3222
3223 if (gst_video_colorimetry_is_equal (&ci, cinfo))
3224 return TRUE;
3225
3226 /* Allow 1:4:0:0 (produced by jpegdec) if the device expects 1:4:7:1 */
3227 if (gst_video_colorimetry_is_equal (&ci, &ci_likely_jpeg)
3228 && gst_video_colorimetry_is_equal (cinfo, &ci_jpeg))
3229 return TRUE;
3230
3231 return FALSE;
3232 }
3233
3234 static gboolean
gst_v4l2_object_set_format_full(GstV4l2Object * v4l2object,GstCaps * caps,gboolean try_only,GstV4l2Error * error)3235 gst_v4l2_object_set_format_full (GstV4l2Object * v4l2object, GstCaps * caps,
3236 gboolean try_only, GstV4l2Error * error)
3237 {
3238 gint fd = v4l2object->video_fd;
3239 struct v4l2_format format;
3240 struct v4l2_streamparm streamparm;
3241 enum v4l2_field field;
3242 guint32 pixelformat;
3243 struct v4l2_fmtdesc *fmtdesc;
3244 GstVideoInfo info;
3245 GstVideoAlignment align;
3246 gint width, height, fps_n, fps_d;
3247 gint n_v4l_planes;
3248 gint i = 0;
3249 gboolean is_mplane;
3250 enum v4l2_colorspace colorspace = 0;
3251 enum v4l2_quantization range = 0;
3252 enum v4l2_ycbcr_encoding matrix = 0;
3253 enum v4l2_xfer_func transfer = 0;
3254 GstStructure *s;
3255 gboolean disable_colorimetry = FALSE;
3256
3257 g_return_val_if_fail (!v4l2object->skip_try_fmt_probes ||
3258 gst_caps_is_writable (caps), FALSE);
3259
3260 GST_V4L2_CHECK_OPEN (v4l2object);
3261 if (!try_only)
3262 GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
3263
3264 is_mplane = V4L2_TYPE_IS_MULTIPLANAR (v4l2object->type);
3265
3266 gst_video_info_init (&info);
3267 gst_video_alignment_reset (&align);
3268
3269 if (!gst_v4l2_object_get_caps_info (v4l2object, caps, &fmtdesc, &info))
3270 goto invalid_caps;
3271
3272 pixelformat = fmtdesc->pixelformat;
3273 width = GST_VIDEO_INFO_WIDTH (&info);
3274 height = GST_VIDEO_INFO_HEIGHT (&info);
3275 fps_n = GST_VIDEO_INFO_FPS_N (&info);
3276 fps_d = GST_VIDEO_INFO_FPS_D (&info);
3277
3278 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3279 * or if contiguous is prefered */
3280 n_v4l_planes = GST_VIDEO_INFO_N_PLANES (&info);
3281 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3282 n_v4l_planes = 1;
3283
3284 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
3285 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "interlaced video");
3286 /* ideally we would differentiate between types of interlaced video
3287 * but there is not sufficient information in the caps..
3288 */
3289 field = V4L2_FIELD_INTERLACED;
3290 } else {
3291 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "progressive video");
3292 field = V4L2_FIELD_NONE;
3293 }
3294
3295 /* We first pick the main colorspace from the primaries */
3296 switch (info.colorimetry.primaries) {
3297 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3298 /* There is two colorspaces using these primaries, use the range to
3299 * differentiate */
3300 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3301 colorspace = V4L2_COLORSPACE_REC709;
3302 else
3303 colorspace = V4L2_COLORSPACE_SRGB;
3304 break;
3305 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
3306 colorspace = V4L2_COLORSPACE_BT2020;
3307 break;
3308 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
3309 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
3310 break;
3311 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
3312 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
3313 break;
3314 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
3315 colorspace = V4L2_COLORSPACE_SMPTE170M;
3316 break;
3317 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
3318 colorspace = V4L2_COLORSPACE_SMPTE240M;
3319 break;
3320
3321 case GST_VIDEO_COLOR_PRIMARIES_FILM:
3322 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
3323 /* We don't know, we will guess */
3324 break;
3325
3326 default:
3327 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3328 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
3329 break;
3330 }
3331
3332 switch (info.colorimetry.range) {
3333 case GST_VIDEO_COLOR_RANGE_0_255:
3334 range = V4L2_QUANTIZATION_FULL_RANGE;
3335 break;
3336 case GST_VIDEO_COLOR_RANGE_16_235:
3337 range = V4L2_QUANTIZATION_LIM_RANGE;
3338 break;
3339 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
3340 /* We let the driver pick a default one */
3341 break;
3342 default:
3343 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3344 "Unknown colorimetry range %d", info.colorimetry.range);
3345 break;
3346 }
3347
3348 switch (info.colorimetry.matrix) {
3349 case GST_VIDEO_COLOR_MATRIX_RGB:
3350 /* Unspecified, leave to default */
3351 break;
3352 /* FCC is about the same as BT601 with less digit */
3353 case GST_VIDEO_COLOR_MATRIX_FCC:
3354 case GST_VIDEO_COLOR_MATRIX_BT601:
3355 matrix = V4L2_YCBCR_ENC_601;
3356 break;
3357 case GST_VIDEO_COLOR_MATRIX_BT709:
3358 matrix = V4L2_YCBCR_ENC_709;
3359 break;
3360 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
3361 matrix = V4L2_YCBCR_ENC_SMPTE240M;
3362 break;
3363 case GST_VIDEO_COLOR_MATRIX_BT2020:
3364 matrix = V4L2_YCBCR_ENC_BT2020;
3365 break;
3366 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
3367 /* We let the driver pick a default one */
3368 break;
3369 default:
3370 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3371 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
3372 break;
3373 }
3374
3375 switch (info.colorimetry.transfer) {
3376 case GST_VIDEO_TRANSFER_GAMMA18:
3377 case GST_VIDEO_TRANSFER_GAMMA20:
3378 case GST_VIDEO_TRANSFER_GAMMA22:
3379 case GST_VIDEO_TRANSFER_GAMMA28:
3380 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3381 "GAMMA 18, 20, 22, 28 transfer functions not supported");
3382 /* fallthrough */
3383 case GST_VIDEO_TRANSFER_GAMMA10:
3384 transfer = V4L2_XFER_FUNC_NONE;
3385 break;
3386 case GST_VIDEO_TRANSFER_BT2020_12:
3387 case GST_VIDEO_TRANSFER_BT709:
3388 transfer = V4L2_XFER_FUNC_709;
3389 break;
3390 case GST_VIDEO_TRANSFER_SMPTE240M:
3391 transfer = V4L2_XFER_FUNC_SMPTE240M;
3392 break;
3393 case GST_VIDEO_TRANSFER_SRGB:
3394 transfer = V4L2_XFER_FUNC_SRGB;
3395 break;
3396 case GST_VIDEO_TRANSFER_LOG100:
3397 case GST_VIDEO_TRANSFER_LOG316:
3398 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3399 "LOG 100, 316 transfer functions not supported");
3400 /* FIXME No known sensible default, maybe AdobeRGB ? */
3401 break;
3402 case GST_VIDEO_TRANSFER_UNKNOWN:
3403 /* We let the driver pick a default one */
3404 break;
3405 default:
3406 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3407 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
3408 break;
3409 }
3410
3411 if (colorspace == 0) {
3412 /* Try to guess colorspace according to pixelformat and size */
3413 if (GST_VIDEO_INFO_IS_YUV (&info)) {
3414 if (range == V4L2_QUANTIZATION_FULL_RANGE
3415 && matrix == V4L2_YCBCR_ENC_601 && transfer == 0) {
3416 /* Full range BT.601 YCbCr encoding with unknown primaries and transfer
3417 * function most likely is JPEG */
3418 colorspace = V4L2_COLORSPACE_JPEG;
3419 transfer = V4L2_XFER_FUNC_SRGB;
3420 } else {
3421 /* SD streams likely use SMPTE170M and HD streams REC709 */
3422 if (width <= 720 && height <= 576)
3423 colorspace = V4L2_COLORSPACE_SMPTE170M;
3424 else
3425 colorspace = V4L2_COLORSPACE_REC709;
3426 }
3427 } else if (GST_VIDEO_INFO_IS_RGB (&info)) {
3428 colorspace = V4L2_COLORSPACE_SRGB;
3429 transfer = V4L2_XFER_FUNC_NONE;
3430 }
3431 }
3432
3433 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format %dx%d, format "
3434 "%" GST_FOURCC_FORMAT " stride: %d", width, height,
3435 GST_FOURCC_ARGS (pixelformat), GST_VIDEO_INFO_PLANE_STRIDE (&info, 0));
3436
3437 memset (&format, 0x00, sizeof (struct v4l2_format));
3438 format.type = v4l2object->type;
3439
3440 if (is_mplane) {
3441 format.type = v4l2object->type;
3442 format.fmt.pix_mp.pixelformat = pixelformat;
3443 format.fmt.pix_mp.width = width;
3444 format.fmt.pix_mp.height = height;
3445 format.fmt.pix_mp.field = field;
3446 format.fmt.pix_mp.num_planes = n_v4l_planes;
3447
3448 /* try to ask our prefered stride but it's not a failure if not
3449 * accepted */
3450 for (i = 0; i < n_v4l_planes; i++) {
3451 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, i);
3452
3453 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3454 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3455 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3456
3457 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
3458 }
3459
3460 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3461 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
3462 } else {
3463 gint stride = GST_VIDEO_INFO_PLANE_STRIDE (&info, 0);
3464
3465 format.type = v4l2object->type;
3466
3467 format.fmt.pix.width = width;
3468 format.fmt.pix.height = height;
3469 format.fmt.pix.pixelformat = pixelformat;
3470 format.fmt.pix.field = field;
3471
3472 if (GST_VIDEO_FORMAT_INFO_IS_TILED (info.finfo))
3473 stride = GST_VIDEO_TILE_X_TILES (stride) <<
3474 GST_VIDEO_FORMAT_INFO_TILE_WS (info.finfo);
3475
3476 /* try to ask our prefered stride */
3477 format.fmt.pix.bytesperline = stride;
3478
3479 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ENCODED)
3480 format.fmt.pix.sizeimage = ENCODED_BUFFER_SIZE;
3481 }
3482
3483 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired format is %dx%d, format "
3484 "%" GST_FOURCC_FORMAT ", nb planes %d", format.fmt.pix.width,
3485 format.fmt.pix_mp.height,
3486 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3487 is_mplane ? format.fmt.pix_mp.num_planes : 1);
3488
3489 #ifndef GST_DISABLE_GST_DEBUG
3490 if (is_mplane) {
3491 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3492 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3493 format.fmt.pix_mp.plane_fmt[i].bytesperline);
3494 } else {
3495 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d",
3496 format.fmt.pix.bytesperline);
3497 }
3498 #endif
3499
3500 if (is_mplane) {
3501 format.fmt.pix_mp.colorspace = colorspace;
3502 format.fmt.pix_mp.quantization = range;
3503 format.fmt.pix_mp.ycbcr_enc = matrix;
3504 format.fmt.pix_mp.xfer_func = transfer;
3505 } else {
3506 format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
3507 format.fmt.pix.colorspace = colorspace;
3508 format.fmt.pix.quantization = range;
3509 format.fmt.pix.ycbcr_enc = matrix;
3510 format.fmt.pix.xfer_func = transfer;
3511 }
3512
3513 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired colorspace is %d:%d:%d:%d",
3514 colorspace, range, matrix, transfer);
3515
3516 if (try_only) {
3517 if (v4l2object->ioctl (fd, VIDIOC_TRY_FMT, &format) < 0)
3518 goto try_fmt_failed;
3519 } else {
3520 if (v4l2object->ioctl (fd, VIDIOC_S_FMT, &format) < 0)
3521 goto set_fmt_failed;
3522 }
3523
3524 if (is_mplane) {
3525 colorspace = format.fmt.pix_mp.colorspace;
3526 range = format.fmt.pix_mp.quantization;
3527 matrix = format.fmt.pix_mp.ycbcr_enc;
3528 transfer = format.fmt.pix_mp.xfer_func;
3529 } else {
3530 colorspace = format.fmt.pix.colorspace;
3531 range = format.fmt.pix.quantization;
3532 matrix = format.fmt.pix.ycbcr_enc;
3533 transfer = format.fmt.pix.xfer_func;
3534 }
3535
3536 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got format of %dx%d, format "
3537 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d:%d:%d:%d",
3538 format.fmt.pix.width, format.fmt.pix_mp.height,
3539 GST_FOURCC_ARGS (format.fmt.pix.pixelformat),
3540 is_mplane ? format.fmt.pix_mp.num_planes : 1,
3541 colorspace, range, matrix, transfer);
3542
3543 #ifndef GST_DISABLE_GST_DEBUG
3544 if (is_mplane) {
3545 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
3546 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3547 format.fmt.pix_mp.plane_fmt[i].bytesperline,
3548 format.fmt.pix_mp.plane_fmt[i].sizeimage);
3549 } else {
3550 GST_DEBUG_OBJECT (v4l2object->dbg_obj, " stride %d, sizeimage %d",
3551 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
3552 }
3553 #endif
3554
3555 if (format.fmt.pix.pixelformat != pixelformat)
3556 goto invalid_pixelformat;
3557
3558 /* Only negotiate size with raw data.
3559 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
3560 * in ASF mode for example, there is also not reason for a driver to
3561 * change the size. */
3562 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED) {
3563 /* We can crop larger images */
3564 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
3565 goto invalid_dimensions;
3566
3567 /* Note, this will be adjusted if upstream has non-centered cropping. */
3568 align.padding_top = 0;
3569 align.padding_bottom = format.fmt.pix.height - height;
3570 align.padding_left = 0;
3571 align.padding_right = format.fmt.pix.width - width;
3572 }
3573
3574 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
3575 goto invalid_planes;
3576
3577 /* used to check colorimetry and interlace mode fields presence */
3578 s = gst_caps_get_structure (caps, 0);
3579
3580 if (!gst_v4l2_object_get_interlace_mode (format.fmt.pix.field,
3581 &info.interlace_mode))
3582 goto invalid_field;
3583 if (gst_structure_has_field (s, "interlace-mode")) {
3584 if (format.fmt.pix.field != field)
3585 goto invalid_field;
3586 }
3587
3588 if (gst_v4l2_object_get_colorspace (&format, &info.colorimetry)) {
3589 if (gst_structure_has_field (s, "colorimetry")) {
3590 if (!gst_v4l2_video_colorimetry_matches (&info.colorimetry,
3591 gst_structure_get_string (s, "colorimetry")))
3592 goto invalid_colorimetry;
3593 }
3594 } else {
3595 /* The driver (or libv4l2) is miss-behaving, just ignore colorimetry from
3596 * the TRY_FMT */
3597 disable_colorimetry = TRUE;
3598 if (gst_structure_has_field (s, "colorimetry"))
3599 gst_structure_remove_field (s, "colorimetry");
3600 }
3601
3602 /* In case we have skipped the try_fmt probes, we'll need to set the
3603 * colorimetry and interlace-mode back into the caps. */
3604 if (v4l2object->skip_try_fmt_probes) {
3605 if (!disable_colorimetry && !gst_structure_has_field (s, "colorimetry")) {
3606 gchar *str = gst_video_colorimetry_to_string (&info.colorimetry);
3607 gst_structure_set (s, "colorimetry", G_TYPE_STRING, str, NULL);
3608 g_free (str);
3609 }
3610
3611 if (!gst_structure_has_field (s, "interlace-mode"))
3612 gst_structure_set (s, "interlace-mode", G_TYPE_STRING,
3613 gst_video_interlace_mode_to_string (info.interlace_mode), NULL);
3614 }
3615
3616 if (try_only) /* good enough for trying only */
3617 return TRUE;
3618
3619 if (GST_VIDEO_INFO_HAS_ALPHA (&info)) {
3620 struct v4l2_control ctl = { 0, };
3621 ctl.id = V4L2_CID_ALPHA_COMPONENT;
3622 ctl.value = 0xff;
3623
3624 if (v4l2object->ioctl (fd, VIDIOC_S_CTRL, &ctl) < 0)
3625 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3626 "Failed to set alpha component value");
3627 }
3628
3629 /* Is there a reason we require the caller to always specify a framerate? */
3630 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Desired framerate: %u/%u", fps_n,
3631 fps_d);
3632
3633 memset (&streamparm, 0x00, sizeof (struct v4l2_streamparm));
3634 streamparm.type = v4l2object->type;
3635
3636 if (v4l2object->ioctl (fd, VIDIOC_G_PARM, &streamparm) < 0)
3637 goto get_parm_failed;
3638
3639 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
3640 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
3641 GST_VIDEO_INFO_FPS_N (&info) =
3642 streamparm.parm.capture.timeperframe.denominator;
3643 GST_VIDEO_INFO_FPS_D (&info) =
3644 streamparm.parm.capture.timeperframe.numerator;
3645
3646 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got capture framerate: %u/%u",
3647 streamparm.parm.capture.timeperframe.denominator,
3648 streamparm.parm.capture.timeperframe.numerator);
3649
3650 /* We used to skip frame rate setup if the camera was already setup
3651 * with the requested frame rate. This breaks some cameras though,
3652 * causing them to not output data (several models of Thinkpad cameras
3653 * have this problem at least).
3654 * So, don't skip. */
3655 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting capture framerate to %u/%u",
3656 fps_n, fps_d);
3657 /* We want to change the frame rate, so check whether we can. Some cheap USB
3658 * cameras don't have the capability */
3659 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3660 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3661 "Not setting capture framerate (not supported)");
3662 goto done;
3663 }
3664
3665 /* Note: V4L2 wants the frame interval, we have the frame rate */
3666 streamparm.parm.capture.timeperframe.numerator = fps_d;
3667 streamparm.parm.capture.timeperframe.denominator = fps_n;
3668
3669 /* some cheap USB cam's won't accept any change */
3670 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3671 goto set_parm_failed;
3672
3673 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
3674 streamparm.parm.capture.timeperframe.denominator > 0) {
3675 /* get new values */
3676 fps_d = streamparm.parm.capture.timeperframe.numerator;
3677 fps_n = streamparm.parm.capture.timeperframe.denominator;
3678
3679 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set capture framerate to %u/%u",
3680 fps_n, fps_d);
3681 } else {
3682 /* fix v4l2 capture driver to provide framerate values */
3683 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3684 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
3685 }
3686
3687 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3688 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3689 } else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
3690 || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
3691 GST_VIDEO_INFO_FPS_N (&info) =
3692 streamparm.parm.output.timeperframe.denominator;
3693 GST_VIDEO_INFO_FPS_D (&info) =
3694 streamparm.parm.output.timeperframe.numerator;
3695
3696 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Got output framerate: %u/%u",
3697 streamparm.parm.output.timeperframe.denominator,
3698 streamparm.parm.output.timeperframe.numerator);
3699
3700 GST_LOG_OBJECT (v4l2object->dbg_obj, "Setting output framerate to %u/%u",
3701 fps_n, fps_d);
3702 if ((streamparm.parm.output.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
3703 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
3704 "Not setting output framerate (not supported)");
3705 goto done;
3706 }
3707
3708 /* Note: V4L2 wants the frame interval, we have the frame rate */
3709 streamparm.parm.output.timeperframe.numerator = fps_d;
3710 streamparm.parm.output.timeperframe.denominator = fps_n;
3711
3712 if (v4l2object->ioctl (fd, VIDIOC_S_PARM, &streamparm) < 0)
3713 goto set_parm_failed;
3714
3715 if (streamparm.parm.output.timeperframe.numerator > 0 &&
3716 streamparm.parm.output.timeperframe.denominator > 0) {
3717 /* get new values */
3718 fps_d = streamparm.parm.output.timeperframe.numerator;
3719 fps_n = streamparm.parm.output.timeperframe.denominator;
3720
3721 GST_INFO_OBJECT (v4l2object->dbg_obj, "Set output framerate to %u/%u",
3722 fps_n, fps_d);
3723 } else {
3724 /* fix v4l2 output driver to provide framerate values */
3725 GST_WARNING_OBJECT (v4l2object->dbg_obj,
3726 "Reuse caps framerate %u/%u - fix v4l2 output driver", fps_n, fps_d);
3727 }
3728
3729 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
3730 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
3731 }
3732
3733 done:
3734 /* add boolean return, so we can fail on drivers bugs */
3735 gst_v4l2_object_save_format (v4l2object, fmtdesc, &format, &info, &align);
3736
3737 /* now configure the pool */
3738 if (!gst_v4l2_object_setup_pool (v4l2object, caps))
3739 goto pool_failed;
3740
3741 return TRUE;
3742
3743 /* ERRORS */
3744 invalid_caps:
3745 {
3746 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "can't parse caps %" GST_PTR_FORMAT,
3747 caps);
3748 return FALSE;
3749 }
3750 try_fmt_failed:
3751 {
3752 if (errno == EINVAL) {
3753 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3754 (_("Device '%s' has no supported format"), v4l2object->videodev),
3755 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3756 GST_FOURCC_ARGS (pixelformat), width, height,
3757 g_strerror (errno)));
3758 } else {
3759 GST_V4L2_ERROR (error, RESOURCE, FAILED,
3760 (_("Device '%s' failed during initialization"),
3761 v4l2object->videodev),
3762 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3763 GST_FOURCC_ARGS (pixelformat), width, height,
3764 g_strerror (errno)));
3765 }
3766 return FALSE;
3767 }
3768 set_fmt_failed:
3769 {
3770 if (errno == EBUSY) {
3771 GST_V4L2_ERROR (error, RESOURCE, BUSY,
3772 (_("Device '%s' is busy"), v4l2object->videodev),
3773 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3774 GST_FOURCC_ARGS (pixelformat), width, height,
3775 g_strerror (errno)));
3776 } else if (errno == EINVAL) {
3777 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3778 (_("Device '%s' has no supported format"), v4l2object->videodev),
3779 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3780 GST_FOURCC_ARGS (pixelformat), width, height,
3781 g_strerror (errno)));
3782 } else {
3783 GST_V4L2_ERROR (error, RESOURCE, FAILED,
3784 (_("Device '%s' failed during initialization"),
3785 v4l2object->videodev),
3786 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
3787 GST_FOURCC_ARGS (pixelformat), width, height,
3788 g_strerror (errno)));
3789 }
3790 return FALSE;
3791 }
3792 invalid_dimensions:
3793 {
3794 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3795 (_("Device '%s' cannot capture at %dx%d"),
3796 v4l2object->videodev, width, height),
3797 ("Tried to capture at %dx%d, but device returned size %dx%d",
3798 width, height, format.fmt.pix.width, format.fmt.pix.height));
3799 return FALSE;
3800 }
3801 invalid_pixelformat:
3802 {
3803 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3804 (_("Device '%s' cannot capture in the specified format"),
3805 v4l2object->videodev),
3806 ("Tried to capture in %" GST_FOURCC_FORMAT
3807 ", but device returned format" " %" GST_FOURCC_FORMAT,
3808 GST_FOURCC_ARGS (pixelformat),
3809 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
3810 return FALSE;
3811 }
3812 invalid_planes:
3813 {
3814 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3815 (_("Device '%s' does support non-contiguous planes"),
3816 v4l2object->videodev),
3817 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
3818 return FALSE;
3819 }
3820 invalid_field:
3821 {
3822 enum v4l2_field wanted_field;
3823
3824 if (is_mplane)
3825 wanted_field = format.fmt.pix_mp.field;
3826 else
3827 wanted_field = format.fmt.pix.field;
3828
3829 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3830 (_("Device '%s' does not support %s interlacing"),
3831 v4l2object->videodev,
3832 field == V4L2_FIELD_NONE ? "progressive" : "interleaved"),
3833 ("Device wants %s interlacing",
3834 wanted_field == V4L2_FIELD_NONE ? "progressive" : "interleaved"));
3835 return FALSE;
3836 }
3837 invalid_colorimetry:
3838 {
3839 gchar *wanted_colorimetry;
3840
3841 wanted_colorimetry = gst_video_colorimetry_to_string (&info.colorimetry);
3842
3843 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3844 (_("Device '%s' does not support %s colorimetry"),
3845 v4l2object->videodev, gst_structure_get_string (s, "colorimetry")),
3846 ("Device wants %s colorimetry", wanted_colorimetry));
3847
3848 g_free (wanted_colorimetry);
3849 return FALSE;
3850 }
3851 get_parm_failed:
3852 {
3853 /* it's possible that this call is not supported */
3854 if (errno != EINVAL && errno != ENOTTY) {
3855 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3856 (_("Could not get parameters on device '%s'"),
3857 v4l2object->videodev), GST_ERROR_SYSTEM);
3858 }
3859 goto done;
3860 }
3861 set_parm_failed:
3862 {
3863 GST_V4L2_ERROR (error, RESOURCE, SETTINGS,
3864 (_("Video device did not accept new frame rate setting.")),
3865 GST_ERROR_SYSTEM);
3866 goto done;
3867 }
3868 pool_failed:
3869 {
3870 /* setup_pool already send the error */
3871 return FALSE;
3872 }
3873 }
3874
3875 gboolean
gst_v4l2_object_set_format(GstV4l2Object * v4l2object,GstCaps * caps,GstV4l2Error * error)3876 gst_v4l2_object_set_format (GstV4l2Object * v4l2object, GstCaps * caps,
3877 GstV4l2Error * error)
3878 {
3879 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Setting format to %" GST_PTR_FORMAT,
3880 caps);
3881 return gst_v4l2_object_set_format_full (v4l2object, caps, FALSE, error);
3882 }
3883
3884 gboolean
gst_v4l2_object_try_format(GstV4l2Object * v4l2object,GstCaps * caps,GstV4l2Error * error)3885 gst_v4l2_object_try_format (GstV4l2Object * v4l2object, GstCaps * caps,
3886 GstV4l2Error * error)
3887 {
3888 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Trying format %" GST_PTR_FORMAT,
3889 caps);
3890 return gst_v4l2_object_set_format_full (v4l2object, caps, TRUE, error);
3891 }
3892
3893 /**
3894 * gst_v4l2_object_acquire_format:
3895 * @v4l2object the object
3896 * @info a GstVideoInfo to be filled
3897 *
3898 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
3899 * the output format is choosen by the HW.
3900 *
3901 * Returns: %TRUE on success, %FALSE on failure.
3902 */
3903 gboolean
gst_v4l2_object_acquire_format(GstV4l2Object * v4l2object,GstVideoInfo * info)3904 gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info)
3905 {
3906 struct v4l2_fmtdesc *fmtdesc;
3907 struct v4l2_format fmt;
3908 struct v4l2_crop crop;
3909 struct v4l2_selection sel;
3910 struct v4l2_rect *r = NULL;
3911 GstVideoFormat format;
3912 guint width, height;
3913 GstVideoAlignment align;
3914
3915 gst_video_info_init (info);
3916 gst_video_alignment_reset (&align);
3917
3918 memset (&fmt, 0x00, sizeof (struct v4l2_format));
3919 fmt.type = v4l2object->type;
3920 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
3921 goto get_fmt_failed;
3922
3923 fmtdesc = gst_v4l2_object_get_format_from_fourcc (v4l2object,
3924 fmt.fmt.pix.pixelformat);
3925 if (fmtdesc == NULL)
3926 goto unsupported_format;
3927
3928 /* No need to care about mplane, the four first params are the same */
3929 format = gst_v4l2_object_v4l2fourcc_to_video_format (fmt.fmt.pix.pixelformat);
3930
3931 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
3932 if (format == GST_VIDEO_FORMAT_UNKNOWN)
3933 goto unsupported_format;
3934
3935 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
3936 goto invalid_dimensions;
3937
3938 width = fmt.fmt.pix.width;
3939 height = fmt.fmt.pix.height;
3940
3941 /* Use the default compose rectangle */
3942 memset (&sel, 0, sizeof (struct v4l2_selection));
3943 sel.type = v4l2object->type;
3944 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
3945 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0) {
3946 r = &sel.r;
3947 } else {
3948 /* For ancient kernels, fall back to G_CROP */
3949 memset (&crop, 0, sizeof (struct v4l2_crop));
3950 crop.type = v4l2object->type;
3951 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
3952 r = &crop.c;
3953 }
3954 if (r) {
3955 align.padding_left = r->left;
3956 align.padding_top = r->top;
3957 align.padding_right = width - r->width - r->left;
3958 align.padding_bottom = height - r->height - r->top;
3959 width = r->width;
3960 height = r->height;
3961 }
3962
3963 gst_video_info_set_format (info, format, width, height);
3964
3965 switch (fmt.fmt.pix.field) {
3966 case V4L2_FIELD_ANY:
3967 case V4L2_FIELD_NONE:
3968 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
3969 break;
3970 case V4L2_FIELD_INTERLACED:
3971 case V4L2_FIELD_INTERLACED_TB:
3972 case V4L2_FIELD_INTERLACED_BT:
3973 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
3974 break;
3975 default:
3976 goto unsupported_field;
3977 }
3978
3979 gst_v4l2_object_get_colorspace (&fmt, &info->colorimetry);
3980
3981 gst_v4l2_object_save_format (v4l2object, fmtdesc, &fmt, info, &align);
3982
3983 /* Shall we setup the pool ? */
3984
3985 return TRUE;
3986
3987 get_fmt_failed:
3988 {
3989 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3990 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
3991 return FALSE;
3992 }
3993 invalid_dimensions:
3994 {
3995 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
3996 (_("Video device returned invalid dimensions.")),
3997 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
3998 fmt.fmt.pix.height));
3999 return FALSE;
4000 }
4001 unsupported_field:
4002 {
4003 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
4004 (_("Video device uses an unsupported interlacing method.")),
4005 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
4006 return FALSE;
4007 }
4008 unsupported_format:
4009 {
4010 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
4011 (_("Video device uses an unsupported pixel format.")),
4012 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
4013 GST_FOURCC_ARGS (fmt.fmt.pix.pixelformat)));
4014 return FALSE;
4015 }
4016 }
4017
4018 gboolean
gst_v4l2_object_set_crop(GstV4l2Object * obj)4019 gst_v4l2_object_set_crop (GstV4l2Object * obj)
4020 {
4021 struct v4l2_selection sel = { 0 };
4022 struct v4l2_crop crop = { 0 };
4023
4024 sel.type = obj->type;
4025 sel.target = V4L2_SEL_TGT_CROP;
4026 sel.flags = 0;
4027 sel.r.left = obj->align.padding_left;
4028 sel.r.top = obj->align.padding_top;
4029 sel.r.width = obj->info.width;
4030 sel.r.height = obj->info.height;
4031
4032 crop.type = obj->type;
4033 crop.c = sel.r;
4034
4035 if (obj->align.padding_left + obj->align.padding_top +
4036 obj->align.padding_right + obj->align.padding_bottom == 0) {
4037 GST_DEBUG_OBJECT (obj->dbg_obj, "no cropping needed");
4038 return TRUE;
4039 }
4040
4041 GST_DEBUG_OBJECT (obj->dbg_obj,
4042 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4043 crop.c.width, crop.c.height);
4044
4045 if (obj->ioctl (obj->video_fd, VIDIOC_S_SELECTION, &sel) < 0) {
4046 if (errno != ENOTTY) {
4047 GST_WARNING_OBJECT (obj->dbg_obj,
4048 "Failed to set crop rectangle with VIDIOC_S_SELECTION: %s",
4049 g_strerror (errno));
4050 return FALSE;
4051 } else {
4052 if (obj->ioctl (obj->video_fd, VIDIOC_S_CROP, &crop) < 0) {
4053 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_S_CROP failed");
4054 return FALSE;
4055 }
4056
4057 if (obj->ioctl (obj->video_fd, VIDIOC_G_CROP, &crop) < 0) {
4058 GST_WARNING_OBJECT (obj->dbg_obj, "VIDIOC_G_CROP failed");
4059 return FALSE;
4060 }
4061
4062 sel.r = crop.c;
4063 }
4064 }
4065
4066 GST_DEBUG_OBJECT (obj->dbg_obj,
4067 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4068 crop.c.width, crop.c.height);
4069
4070 return TRUE;
4071 }
4072
4073 gboolean
gst_v4l2_object_caps_equal(GstV4l2Object * v4l2object,GstCaps * caps)4074 gst_v4l2_object_caps_equal (GstV4l2Object * v4l2object, GstCaps * caps)
4075 {
4076 GstStructure *config;
4077 GstCaps *oldcaps;
4078 gboolean ret;
4079
4080 if (!v4l2object->pool)
4081 return FALSE;
4082
4083 config = gst_buffer_pool_get_config (v4l2object->pool);
4084 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4085
4086 ret = oldcaps && gst_caps_is_equal (caps, oldcaps);
4087
4088 gst_structure_free (config);
4089
4090 return ret;
4091 }
4092
4093 gboolean
gst_v4l2_object_caps_is_subset(GstV4l2Object * v4l2object,GstCaps * caps)4094 gst_v4l2_object_caps_is_subset (GstV4l2Object * v4l2object, GstCaps * caps)
4095 {
4096 GstStructure *config;
4097 GstCaps *oldcaps;
4098 gboolean ret;
4099
4100 if (!v4l2object->pool)
4101 return FALSE;
4102
4103 config = gst_buffer_pool_get_config (v4l2object->pool);
4104 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4105
4106 ret = oldcaps && gst_caps_is_subset (oldcaps, caps);
4107
4108 gst_structure_free (config);
4109
4110 return ret;
4111 }
4112
4113 GstCaps *
gst_v4l2_object_get_current_caps(GstV4l2Object * v4l2object)4114 gst_v4l2_object_get_current_caps (GstV4l2Object * v4l2object)
4115 {
4116 GstStructure *config;
4117 GstCaps *oldcaps;
4118
4119 if (!v4l2object->pool)
4120 return NULL;
4121
4122 config = gst_buffer_pool_get_config (v4l2object->pool);
4123 gst_buffer_pool_config_get_params (config, &oldcaps, NULL, NULL, NULL);
4124
4125 if (oldcaps)
4126 gst_caps_ref (oldcaps);
4127
4128 gst_structure_free (config);
4129
4130 return oldcaps;
4131 }
4132
4133 gboolean
gst_v4l2_object_unlock(GstV4l2Object * v4l2object)4134 gst_v4l2_object_unlock (GstV4l2Object * v4l2object)
4135 {
4136 gboolean ret = TRUE;
4137
4138 GST_LOG_OBJECT (v4l2object->dbg_obj, "start flushing");
4139
4140 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
4141 gst_buffer_pool_set_flushing (v4l2object->pool, TRUE);
4142
4143 return ret;
4144 }
4145
4146 gboolean
gst_v4l2_object_unlock_stop(GstV4l2Object * v4l2object)4147 gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object)
4148 {
4149 gboolean ret = TRUE;
4150
4151 GST_LOG_OBJECT (v4l2object->dbg_obj, "stop flushing");
4152
4153 if (v4l2object->pool && gst_buffer_pool_is_active (v4l2object->pool))
4154 gst_buffer_pool_set_flushing (v4l2object->pool, FALSE);
4155
4156 return ret;
4157 }
4158
4159 gboolean
gst_v4l2_object_stop(GstV4l2Object * v4l2object)4160 gst_v4l2_object_stop (GstV4l2Object * v4l2object)
4161 {
4162 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "stopping");
4163
4164 if (!GST_V4L2_IS_OPEN (v4l2object))
4165 goto done;
4166 if (!GST_V4L2_IS_ACTIVE (v4l2object))
4167 goto done;
4168
4169 if (v4l2object->pool) {
4170 if (!gst_v4l2_buffer_pool_orphan (&v4l2object->pool)) {
4171 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "deactivating pool");
4172 gst_buffer_pool_set_active (v4l2object->pool, FALSE);
4173 gst_object_unref (v4l2object->pool);
4174 }
4175 v4l2object->pool = NULL;
4176 }
4177
4178 GST_V4L2_SET_INACTIVE (v4l2object);
4179
4180 done:
4181 return TRUE;
4182 }
4183
4184 GstCaps *
gst_v4l2_object_probe_caps(GstV4l2Object * v4l2object,GstCaps * filter)4185 gst_v4l2_object_probe_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4186 {
4187 GstCaps *ret;
4188 GSList *walk;
4189 GSList *formats;
4190
4191 formats = gst_v4l2_object_get_format_list (v4l2object);
4192
4193 ret = gst_caps_new_empty ();
4194
4195 if (v4l2object->keep_aspect && !v4l2object->par) {
4196 struct v4l2_cropcap cropcap;
4197
4198 memset (&cropcap, 0, sizeof (cropcap));
4199
4200 cropcap.type = v4l2object->type;
4201 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0) {
4202 if (errno != ENOTTY)
4203 GST_WARNING_OBJECT (v4l2object->dbg_obj,
4204 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
4205 g_strerror (errno));
4206 } else if (cropcap.pixelaspect.numerator && cropcap.pixelaspect.denominator) {
4207 v4l2object->par = g_new0 (GValue, 1);
4208 g_value_init (v4l2object->par, GST_TYPE_FRACTION);
4209 gst_value_set_fraction (v4l2object->par, cropcap.pixelaspect.numerator,
4210 cropcap.pixelaspect.denominator);
4211 }
4212 }
4213
4214 for (walk = formats; walk; walk = walk->next) {
4215 struct v4l2_fmtdesc *format;
4216 GstStructure *template;
4217 GstCaps *tmp;
4218
4219 format = (struct v4l2_fmtdesc *) walk->data;
4220
4221 template = gst_v4l2_object_v4l2fourcc_to_bare_struct (format->pixelformat);
4222
4223 if (!template) {
4224 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
4225 "unknown format %" GST_FOURCC_FORMAT,
4226 GST_FOURCC_ARGS (format->pixelformat));
4227 continue;
4228 }
4229
4230 /* If we have a filter, check if we need to probe this format or not */
4231 if (filter) {
4232 GstCaps *format_caps = gst_caps_new_empty ();
4233
4234 gst_caps_append_structure (format_caps, gst_structure_copy (template));
4235
4236 if (!gst_caps_can_intersect (format_caps, filter)) {
4237 gst_caps_unref (format_caps);
4238 gst_structure_free (template);
4239 continue;
4240 }
4241
4242 gst_caps_unref (format_caps);
4243 }
4244
4245 tmp = gst_v4l2_object_probe_caps_for_format (v4l2object,
4246 format->pixelformat, template);
4247 if (tmp)
4248 gst_caps_append (ret, tmp);
4249
4250 gst_structure_free (template);
4251 }
4252
4253 if (filter) {
4254 GstCaps *tmp;
4255
4256 tmp = ret;
4257 ret = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
4258 gst_caps_unref (tmp);
4259 }
4260
4261 GST_INFO_OBJECT (v4l2object->dbg_obj, "probed caps: %" GST_PTR_FORMAT, ret);
4262
4263 return ret;
4264 }
4265
4266 GstCaps *
gst_v4l2_object_get_caps(GstV4l2Object * v4l2object,GstCaps * filter)4267 gst_v4l2_object_get_caps (GstV4l2Object * v4l2object, GstCaps * filter)
4268 {
4269 GstCaps *ret;
4270
4271 if (v4l2object->probed_caps == NULL)
4272 v4l2object->probed_caps = gst_v4l2_object_probe_caps (v4l2object, NULL);
4273
4274 if (filter) {
4275 ret = gst_caps_intersect_full (filter, v4l2object->probed_caps,
4276 GST_CAPS_INTERSECT_FIRST);
4277 } else {
4278 ret = gst_caps_ref (v4l2object->probed_caps);
4279 }
4280
4281 return ret;
4282 }
4283
4284 gboolean
gst_v4l2_object_decide_allocation(GstV4l2Object * obj,GstQuery * query)4285 gst_v4l2_object_decide_allocation (GstV4l2Object * obj, GstQuery * query)
4286 {
4287 GstCaps *caps;
4288 GstBufferPool *pool = NULL, *other_pool = NULL;
4289 GstStructure *config;
4290 guint size, min, max, own_min = 0;
4291 gboolean update;
4292 gboolean has_video_meta;
4293 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
4294 GstAllocator *allocator = NULL;
4295 GstAllocationParams params = { 0 };
4296
4297 GST_DEBUG_OBJECT (obj->dbg_obj, "decide allocation");
4298
4299 g_return_val_if_fail (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4300 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, FALSE);
4301
4302 gst_query_parse_allocation (query, &caps, NULL);
4303
4304 if (obj->pool == NULL) {
4305 if (!gst_v4l2_object_setup_pool (obj, caps))
4306 goto pool_failed;
4307 }
4308
4309 if (gst_query_get_n_allocation_params (query) > 0)
4310 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
4311
4312 if (gst_query_get_n_allocation_pools (query) > 0) {
4313 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
4314 update = TRUE;
4315 } else {
4316 pool = NULL;
4317 min = max = 0;
4318 size = 0;
4319 update = FALSE;
4320 }
4321
4322 GST_DEBUG_OBJECT (obj->dbg_obj, "allocation: size:%u min:%u max:%u pool:%"
4323 GST_PTR_FORMAT, size, min, max, pool);
4324
4325 has_video_meta =
4326 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4327
4328 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
4329
4330 gst_v4l2_get_driver_min_buffers (obj);
4331 /* We can't share our own pool, if it exceed V4L2 capacity */
4332 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
4333 can_share_own_pool = FALSE;
4334
4335 /* select a pool */
4336 switch (obj->mode) {
4337 case GST_V4L2_IO_RW:
4338 if (pool) {
4339 /* in READ/WRITE mode, prefer a downstream pool because our own pool
4340 * doesn't help much, we have to write to it as well */
4341 GST_DEBUG_OBJECT (obj->dbg_obj,
4342 "read/write mode: using downstream pool");
4343 /* use the bigest size, when we use our own pool we can't really do any
4344 * other size than what the hardware gives us but for downstream pools
4345 * we can try */
4346 size = MAX (size, obj->info.size);
4347 } else if (can_share_own_pool) {
4348 /* no downstream pool, use our own then */
4349 GST_DEBUG_OBJECT (obj->dbg_obj,
4350 "read/write mode: no downstream pool, using our own");
4351 pool = gst_object_ref (obj->pool);
4352 size = obj->info.size;
4353 pushing_from_our_pool = TRUE;
4354 }
4355 break;
4356
4357 case GST_V4L2_IO_USERPTR:
4358 case GST_V4L2_IO_DMABUF_IMPORT:
4359 /* in importing mode, prefer our own pool, and pass the other pool to
4360 * our own, so it can serve itself */
4361 if (pool == NULL)
4362 goto no_downstream_pool;
4363 gst_v4l2_buffer_pool_set_other_pool (GST_V4L2_BUFFER_POOL (obj->pool),
4364 pool);
4365 other_pool = pool;
4366 gst_object_unref (pool);
4367 pool = gst_object_ref (obj->pool);
4368 size = obj->info.size;
4369 break;
4370
4371 case GST_V4L2_IO_MMAP:
4372 case GST_V4L2_IO_DMABUF:
4373 /* in streaming mode, prefer our own pool */
4374 /* Check if we can use it ... */
4375 if (can_share_own_pool) {
4376 if (pool)
4377 gst_object_unref (pool);
4378 pool = gst_object_ref (obj->pool);
4379 size = obj->info.size;
4380 GST_DEBUG_OBJECT (obj->dbg_obj,
4381 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
4382 pushing_from_our_pool = TRUE;
4383 } else if (pool) {
4384 GST_DEBUG_OBJECT (obj->dbg_obj,
4385 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
4386 pool);
4387 } else {
4388 GST_DEBUG_OBJECT (obj->dbg_obj,
4389 "streaming mode: no usable pool, copying to generic pool");
4390 size = MAX (size, obj->info.size);
4391 }
4392 break;
4393 case GST_V4L2_IO_AUTO:
4394 default:
4395 GST_WARNING_OBJECT (obj->dbg_obj, "unhandled mode");
4396 break;
4397 }
4398
4399 if (size == 0)
4400 goto no_size;
4401
4402 /* If pushing from our own pool, configure it with queried minimum,
4403 * otherwise use the minimum required */
4404 if (pushing_from_our_pool) {
4405 /* When pushing from our own pool, we need what downstream one, to be able
4406 * to fill the pipeline, the minimum required to decoder according to the
4407 * driver and 2 more, so we don't endup up with everything downstream or
4408 * held by the decoder. We account 2 buffers for v4l2 so when one is being
4409 * pushed downstream the other one can already be queued for the next
4410 * frame. */
4411 own_min = min + obj->min_buffers + 2;
4412
4413 /* If no allocation parameters where provided, allow for a little more
4414 * buffers and enable copy threshold */
4415 if (!update) {
4416 own_min += 2;
4417 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4418 TRUE);
4419 } else {
4420 gst_v4l2_buffer_pool_copy_at_threshold (GST_V4L2_BUFFER_POOL (pool),
4421 FALSE);
4422 }
4423
4424 } else {
4425 /* In this case we'll have to configure two buffer pool. For our buffer
4426 * pool, we'll need what the driver one, and one more, so we can dequeu */
4427 own_min = obj->min_buffers + 1;
4428 own_min = MAX (own_min, GST_V4L2_MIN_BUFFERS);
4429
4430 /* for the downstream pool, we keep what downstream wants, though ensure
4431 * at least a minimum if downstream didn't suggest anything (we are
4432 * expecting the base class to create a default one for the context) */
4433 min = MAX (min, GST_V4L2_MIN_BUFFERS);
4434
4435 /* To import we need the other pool to hold at least own_min */
4436 if (obj->pool == pool)
4437 min += own_min;
4438 }
4439
4440 /* Request a bigger max, if one was suggested but it's too small */
4441 if (max != 0)
4442 max = MAX (min, max);
4443
4444 /* First step, configure our own pool */
4445 config = gst_buffer_pool_get_config (obj->pool);
4446
4447 if (obj->need_video_meta || has_video_meta) {
4448 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
4449 gst_buffer_pool_config_add_option (config,
4450 GST_BUFFER_POOL_OPTION_VIDEO_META);
4451 }
4452
4453 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4454 gst_buffer_pool_config_set_params (config, caps, size, own_min, 0);
4455
4456 GST_DEBUG_OBJECT (obj->dbg_obj, "setting own pool config to %"
4457 GST_PTR_FORMAT, config);
4458
4459 /* Our pool often need to adjust the value */
4460 if (!gst_buffer_pool_set_config (obj->pool, config)) {
4461 config = gst_buffer_pool_get_config (obj->pool);
4462
4463 GST_DEBUG_OBJECT (obj->dbg_obj, "own pool config changed to %"
4464 GST_PTR_FORMAT, config);
4465
4466 /* our pool will adjust the maximum buffer, which we are fine with */
4467 if (!gst_buffer_pool_set_config (obj->pool, config))
4468 goto config_failed;
4469 }
4470
4471 /* Now configure the other pool if different */
4472 if (obj->pool != pool)
4473 other_pool = pool;
4474
4475 if (other_pool) {
4476 config = gst_buffer_pool_get_config (other_pool);
4477 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
4478 gst_buffer_pool_config_set_params (config, caps, size, min, max);
4479
4480 GST_DEBUG_OBJECT (obj->dbg_obj, "setting other pool config to %"
4481 GST_PTR_FORMAT, config);
4482
4483 /* if downstream supports video metadata, add this to the pool config */
4484 if (has_video_meta) {
4485 GST_DEBUG_OBJECT (obj->dbg_obj, "activate Video Meta");
4486 gst_buffer_pool_config_add_option (config,
4487 GST_BUFFER_POOL_OPTION_VIDEO_META);
4488 }
4489
4490 if (!gst_buffer_pool_set_config (other_pool, config)) {
4491 config = gst_buffer_pool_get_config (other_pool);
4492
4493 if (!gst_buffer_pool_config_validate_params (config, caps, size, min,
4494 max)) {
4495 gst_structure_free (config);
4496 goto config_failed;
4497 }
4498
4499 if (!gst_buffer_pool_set_config (other_pool, config))
4500 goto config_failed;
4501 }
4502 }
4503
4504 if (pool) {
4505 /* For simplicity, simply read back the active configuration, so our base
4506 * class get the right information */
4507 config = gst_buffer_pool_get_config (pool);
4508 gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
4509 gst_structure_free (config);
4510 }
4511
4512 if (update)
4513 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
4514 else
4515 gst_query_add_allocation_pool (query, pool, size, min, max);
4516
4517 if (allocator)
4518 gst_object_unref (allocator);
4519
4520 if (pool)
4521 gst_object_unref (pool);
4522
4523 return TRUE;
4524
4525 pool_failed:
4526 {
4527 /* setup_pool already send the error */
4528 goto cleanup;
4529 }
4530 config_failed:
4531 {
4532 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4533 (_("Failed to configure internal buffer pool.")), (NULL));
4534 goto cleanup;
4535 }
4536 no_size:
4537 {
4538 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4539 (_("Video device did not suggest any buffer size.")), (NULL));
4540 goto cleanup;
4541 }
4542 cleanup:
4543 {
4544 if (allocator)
4545 gst_object_unref (allocator);
4546
4547 if (pool)
4548 gst_object_unref (pool);
4549 return FALSE;
4550 }
4551 no_downstream_pool:
4552 {
4553 GST_ELEMENT_ERROR (obj->element, RESOURCE, SETTINGS,
4554 (_("No downstream pool to import from.")),
4555 ("When importing DMABUF or USERPTR, we need a pool to import from"));
4556 return FALSE;
4557 }
4558 }
4559
4560 gboolean
gst_v4l2_object_propose_allocation(GstV4l2Object * obj,GstQuery * query)4561 gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query)
4562 {
4563 GstBufferPool *pool;
4564 /* we need at least 2 buffers to operate */
4565 guint size, min, max;
4566 GstCaps *caps;
4567 gboolean need_pool;
4568
4569 /* Set defaults allocation parameters */
4570 size = obj->info.size;
4571 min = GST_V4L2_MIN_BUFFERS;
4572 max = VIDEO_MAX_FRAME;
4573
4574 gst_query_parse_allocation (query, &caps, &need_pool);
4575
4576 if (caps == NULL)
4577 goto no_caps;
4578
4579 switch (obj->mode) {
4580 case GST_V4L2_IO_MMAP:
4581 case GST_V4L2_IO_DMABUF:
4582 if ((pool = obj->pool))
4583 gst_object_ref (pool);
4584 break;
4585 default:
4586 pool = NULL;
4587 break;
4588 }
4589
4590 if (pool != NULL) {
4591 GstCaps *pcaps;
4592 GstStructure *config;
4593
4594 /* we had a pool, check caps */
4595 config = gst_buffer_pool_get_config (pool);
4596 gst_buffer_pool_config_get_params (config, &pcaps, NULL, NULL, NULL);
4597
4598 GST_DEBUG_OBJECT (obj->dbg_obj,
4599 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
4600 if (!gst_caps_is_equal (caps, pcaps)) {
4601 gst_structure_free (config);
4602 gst_object_unref (pool);
4603 goto different_caps;
4604 }
4605 gst_structure_free (config);
4606 }
4607 gst_v4l2_get_driver_min_buffers (obj);
4608
4609 min = MAX (obj->min_buffers, GST_V4L2_MIN_BUFFERS);
4610
4611 gst_query_add_allocation_pool (query, pool, size, min, max);
4612
4613 /* we also support various metadata */
4614 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
4615
4616 if (pool)
4617 gst_object_unref (pool);
4618
4619 return TRUE;
4620
4621 /* ERRORS */
4622 no_caps:
4623 {
4624 GST_DEBUG_OBJECT (obj->dbg_obj, "no caps specified");
4625 return FALSE;
4626 }
4627 different_caps:
4628 {
4629 /* different caps, we can't use this pool */
4630 GST_DEBUG_OBJECT (obj->dbg_obj, "pool has different caps");
4631 return FALSE;
4632 }
4633 }
4634
4635 gboolean
gst_v4l2_object_try_import(GstV4l2Object * obj,GstBuffer * buffer)4636 gst_v4l2_object_try_import (GstV4l2Object * obj, GstBuffer * buffer)
4637 {
4638 GstVideoMeta *vmeta;
4639 guint n_mem = gst_buffer_n_memory (buffer);
4640
4641 /* only import if requested */
4642 switch (obj->mode) {
4643 case GST_V4L2_IO_USERPTR:
4644 case GST_V4L2_IO_DMABUF_IMPORT:
4645 break;
4646 default:
4647 GST_DEBUG_OBJECT (obj->dbg_obj,
4648 "The io-mode does not enable importation");
4649 return FALSE;
4650 }
4651
4652 vmeta = gst_buffer_get_video_meta (buffer);
4653 if (!vmeta && obj->need_video_meta) {
4654 GST_DEBUG_OBJECT (obj->dbg_obj, "Downstream buffer uses standard "
4655 "stride/offset while the driver does not.");
4656 return FALSE;
4657 }
4658
4659 /* we need matching strides/offsets and size */
4660 if (vmeta) {
4661 guint p;
4662 gboolean need_fmt_update = FALSE;
4663
4664 if (vmeta->n_planes != GST_VIDEO_INFO_N_PLANES (&obj->info)) {
4665 GST_WARNING_OBJECT (obj->dbg_obj,
4666 "Cannot import buffers with different number planes");
4667 return FALSE;
4668 }
4669
4670 for (p = 0; p < vmeta->n_planes; p++) {
4671 if (vmeta->stride[p] < obj->info.stride[p]) {
4672 GST_DEBUG_OBJECT (obj->dbg_obj,
4673 "Not importing as remote stride %i is smaller then %i on plane %u",
4674 vmeta->stride[p], obj->info.stride[p], p);
4675 return FALSE;
4676 } else if (vmeta->stride[p] > obj->info.stride[p]) {
4677 need_fmt_update = TRUE;
4678 }
4679
4680 if (vmeta->offset[p] < obj->info.offset[p]) {
4681 GST_DEBUG_OBJECT (obj->dbg_obj,
4682 "Not importing as offset %" G_GSIZE_FORMAT
4683 " is smaller then %" G_GSIZE_FORMAT " on plane %u",
4684 vmeta->offset[p], obj->info.offset[p], p);
4685 return FALSE;
4686 } else if (vmeta->offset[p] > obj->info.offset[p]) {
4687 need_fmt_update = TRUE;
4688 }
4689 }
4690
4691 if (need_fmt_update) {
4692 struct v4l2_format format;
4693 gint wanted_stride[GST_VIDEO_MAX_PLANES] = { 0, };
4694
4695 format = obj->format;
4696
4697 /* update the current format with the stride we want to import from */
4698 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
4699 guint i;
4700
4701 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted strides:");
4702
4703 for (i = 0; i < obj->n_v4l2_planes; i++) {
4704 gint stride = vmeta->stride[i];
4705
4706 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4707 stride = GST_VIDEO_TILE_X_TILES (stride) <<
4708 GST_VIDEO_FORMAT_INFO_TILE_WS (obj->info.finfo);
4709
4710 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
4711 wanted_stride[i] = stride;
4712 GST_DEBUG_OBJECT (obj->dbg_obj, " [%u] %i", i, wanted_stride[i]);
4713 }
4714 } else {
4715 gint stride = vmeta->stride[0];
4716
4717 GST_DEBUG_OBJECT (obj->dbg_obj, "Wanted stride: %i", stride);
4718
4719 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo))
4720 stride = GST_VIDEO_TILE_X_TILES (stride) <<
4721 GST_VIDEO_FORMAT_INFO_TILE_WS (obj->info.finfo);
4722
4723 format.fmt.pix.bytesperline = stride;
4724 wanted_stride[0] = stride;
4725 }
4726
4727 if (obj->ioctl (obj->video_fd, VIDIOC_S_FMT, &format) < 0) {
4728 GST_WARNING_OBJECT (obj->dbg_obj,
4729 "Something went wrong trying to update current format: %s",
4730 g_strerror (errno));
4731 return FALSE;
4732 }
4733
4734 gst_v4l2_object_save_format (obj, obj->fmtdesc, &format, &obj->info,
4735 &obj->align);
4736
4737 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
4738 guint i;
4739
4740 for (i = 0; i < obj->n_v4l2_planes; i++) {
4741 if (format.fmt.pix_mp.plane_fmt[i].bytesperline != wanted_stride[i]) {
4742 GST_DEBUG_OBJECT (obj->dbg_obj,
4743 "[%i] Driver did not accept the new stride (wants %i, got %i)",
4744 i, format.fmt.pix_mp.plane_fmt[i].bytesperline,
4745 wanted_stride[i]);
4746 return FALSE;
4747 }
4748 }
4749 } else {
4750 if (format.fmt.pix.bytesperline != wanted_stride[0]) {
4751 GST_DEBUG_OBJECT (obj->dbg_obj,
4752 "Driver did not accept the new stride (wants %i, got %i)",
4753 format.fmt.pix.bytesperline, wanted_stride[0]);
4754 return FALSE;
4755 }
4756 }
4757 }
4758 }
4759
4760 /* we can always import single memory buffer, but otherwise we need the same
4761 * amount of memory object. */
4762 if (n_mem != 1 && n_mem != obj->n_v4l2_planes) {
4763 GST_DEBUG_OBJECT (obj->dbg_obj, "Can only import %i memory, "
4764 "buffers contains %u memory", obj->n_v4l2_planes, n_mem);
4765 return FALSE;
4766 }
4767
4768 /* For DMABuf importation we need DMABuf of course */
4769 if (obj->mode == GST_V4L2_IO_DMABUF_IMPORT) {
4770 guint i;
4771
4772 for (i = 0; i < n_mem; i++) {
4773 GstMemory *mem = gst_buffer_peek_memory (buffer, i);
4774
4775 if (!gst_is_dmabuf_memory (mem)) {
4776 GST_DEBUG_OBJECT (obj->dbg_obj, "Cannot import non-DMABuf memory.");
4777 return FALSE;
4778 }
4779 }
4780 }
4781
4782 /* for the remaining, only the kernel driver can tell */
4783 return TRUE;
4784 }
4785