Debian Codegraph @

lib/libv4l-mplane/libv4l-mplane.c

raw
1 /*
2 * (C) 2012 Mauro Carvalho Chehab
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17 */
18
19 #include <errno.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/syscall.h>
27
28 #if defined(__OpenBSD__)
29 #include <sys/videoio.h>
30 #include <sys/ioctl.h>
31 #else
32 #include <linux/videodev2.h>
33 #endif
34
35 #include "libv4l-plugin.h"
36
37 #define SYS_IOCTL(fd, cmd, arg) \
38 syscall(SYS_ioctl, (int)(fd), (unsigned long)(cmd), (void *)(arg))
39 #define SYS_READ(fd, buf, len) \
40 syscall(SYS_read, (int)(fd), (void *)(buf), (size_t)(len));
41 #define SYS_WRITE(fd, buf, len) \
42 syscall(SYS_write, (int)(fd), (const void *)(buf), (size_t)(len));
43
44
45 #if HAVE_VISIBILITY
46 #define PLUGIN_PUBLIC __attribute__ ((visibility("default")))
47 #else
48 #define PLUGIN_PUBLIC
49 #endif
50
51 struct mplane_plugin {
52 union {
53 struct {
54 unsigned int mplane_capture : 1;
55 unsigned int mplane_output : 1;
56 };
57 unsigned int mplane;
58 };
59 };
60
61 #define SIMPLE_CONVERT_IOCTL(fd, cmd, arg, __struc) ({ \
62 int __ret; \
63 struct __struc *req = arg; \
64 uint32_t type = req->type; \
65 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || \
66 type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { \
67 errno = EINVAL; \
68 __ret = -1; \
69 } else { \
70 req->type = convert_type(type); \
71 __ret = SYS_IOCTL(fd, cmd, arg); \
72 req->type = type; \
73 } \
74 __ret; \
75 })
76
77 static void *plugin_init(int fd)
78 {
79 struct v4l2_capability cap;
80 int ret;
81 struct mplane_plugin plugin, *ret_plugin;
82
83 memset(&plugin, 0, sizeof(plugin));
84
85 /* Check if device needs mplane plugin */
86 memset(&cap, 0, sizeof(cap));
87 ret = SYS_IOCTL(fd, VIDIOC_QUERYCAP, &cap);
88 if (ret) {
89 perror("Failed to query video capabilities");
90 return NULL;
91 }
92
93 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) &&
94 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE))
95 plugin.mplane_capture = 1;
96
97 if (!(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) &&
98 (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE))
99 plugin.mplane_output = 1;
100
101 /* Device doesn't need it. return NULL to disable the plugin */
102 if (!plugin.mplane)
103 return NULL;
104
105 /* Allocate and initialize private data */
106 ret_plugin = calloc(1, sizeof(*ret_plugin));
107 if (!ret_plugin) {
108 perror("Couldn't allocate memory for plugin");
109 return NULL;
110 }
111 *ret_plugin = plugin;
112
113 printf("Using mplane plugin for %s%s\n",
114 plugin.mplane_capture ? "capture " : "",
115 plugin.mplane_output ? "output " : "");
116
117 return ret_plugin;
118 }
119
120 static void plugin_close(void *dev_ops_priv) {
121 if (dev_ops_priv == NULL)
122 return;
123
124 free(dev_ops_priv);
125 }
126
127 static int querycap_ioctl(int fd, unsigned long int cmd,
128 struct v4l2_capability *arg)
129 {
130 struct v4l2_capability *cap = arg;
131 int ret;
132
133 ret = SYS_IOCTL(fd, cmd, cap);
134 if (ret)
135 return ret;
136
137 /* Report mplane as normal caps */
138 if (cap->capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
139 cap->capabilities |= V4L2_CAP_VIDEO_CAPTURE;
140
141 if (cap->capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
142 cap->capabilities |= V4L2_CAP_VIDEO_OUTPUT;
143
144 if (cap->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
145 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
146
147 if (cap->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
148 cap->device_caps |= V4L2_CAP_VIDEO_OUTPUT;
149
150 cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
151 cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
152
153 /*
154 * Don't report mplane caps, as this will be handled via
155 * this plugin
156 */
157 cap->capabilities &= ~(V4L2_CAP_VIDEO_OUTPUT_MPLANE |
158 V4L2_CAP_VIDEO_CAPTURE_MPLANE);
159 cap->device_caps &= ~(V4L2_CAP_VIDEO_OUTPUT_MPLANE |
160 V4L2_CAP_VIDEO_CAPTURE_MPLANE);
161
162 return 0;
163 }
164
165 static int convert_type(int type)
166 {
167 switch (type) {
168 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
169 return V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
170 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
171 return V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
172 default:
173 return type;
174 }
175 }
176
177 static void sanitize_format(struct v4l2_format *fmt)
178 {
179 unsigned int offset;
180
181 /*
182 * The v4l2_pix_format structure has been extended with fields that were
183 * not previously required to be set to zero by applications. The priv
184 * field, when set to a magic value, indicates the the extended fields
185 * are valid. We support these extended fields since struct
186 * v4l2_pix_format_mplane supports those fields as well.
187 *
188 * So this function will sanitize v4l2_pix_format if priv != PRIV_MAGIC
189 * by setting priv to that value and zeroing the remaining fields.
190 */
191
192 if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
193 return;
194
195 fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
196
197 offset = offsetof(struct v4l2_pix_format, priv)
198 + sizeof(fmt->fmt.pix.priv);
199 memset(((char *)&fmt->fmt.pix) + offset, 0,
200 sizeof(fmt->fmt.pix) - offset);
201 }
202
203 static int try_set_fmt_ioctl(int fd, unsigned long int cmd,
204 struct v4l2_format *arg)
205 {
206 struct v4l2_format fmt = { 0 };
207 struct v4l2_format *org = arg;
208 int ret;
209
210 switch (arg->type) {
211 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
212 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
213 break;
214 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
215 fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
216 break;
217 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
218 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
219 errno = EINVAL;
220 return -1;
221 default:
222 return SYS_IOCTL(fd, cmd, arg);
223 }
224
225 sanitize_format(org);
226
227 fmt.fmt.pix_mp.width = org->fmt.pix.width;
228 fmt.fmt.pix_mp.height = org->fmt.pix.height;
229 fmt.fmt.pix_mp.pixelformat = org->fmt.pix.pixelformat;
230 fmt.fmt.pix_mp.field = org->fmt.pix.field;
231 fmt.fmt.pix_mp.colorspace = org->fmt.pix.colorspace;
232 fmt.fmt.pix_mp.xfer_func = org->fmt.pix.xfer_func;
233 fmt.fmt.pix_mp.ycbcr_enc = org->fmt.pix.ycbcr_enc;
234 fmt.fmt.pix_mp.quantization = org->fmt.pix.quantization;
235 fmt.fmt.pix_mp.num_planes = 1;
236 fmt.fmt.pix_mp.flags = org->fmt.pix.flags;
237 fmt.fmt.pix_mp.plane_fmt[0].bytesperline = org->fmt.pix.bytesperline;
238 fmt.fmt.pix_mp.plane_fmt[0].sizeimage = org->fmt.pix.sizeimage;
239
240 ret = SYS_IOCTL(fd, cmd, &fmt);
241 if (ret)
242 return ret;
243
244 org->fmt.pix.width = fmt.fmt.pix_mp.width;
245 org->fmt.pix.height = fmt.fmt.pix_mp.height;
246 org->fmt.pix.pixelformat = fmt.fmt.pix_mp.pixelformat;
247 org->fmt.pix.field = fmt.fmt.pix_mp.field;
248 org->fmt.pix.colorspace = fmt.fmt.pix_mp.colorspace;
249 org->fmt.pix.xfer_func = fmt.fmt.pix_mp.xfer_func;
250 org->fmt.pix.ycbcr_enc = fmt.fmt.pix_mp.ycbcr_enc;
251 org->fmt.pix.quantization = fmt.fmt.pix_mp.quantization;
252 org->fmt.pix.bytesperline = fmt.fmt.pix_mp.plane_fmt[0].bytesperline;
253 org->fmt.pix.sizeimage = fmt.fmt.pix_mp.plane_fmt[0].sizeimage;
254 org->fmt.pix.flags = fmt.fmt.pix_mp.flags;
255
256 return 0;
257 }
258
259 static int create_bufs_ioctl(int fd, unsigned long int cmd,
260 struct v4l2_create_buffers *arg)
261 {
262 struct v4l2_create_buffers cbufs = { 0 };
263 struct v4l2_format *fmt = &cbufs.format;
264 struct v4l2_format *org = &arg->format;
265 int ret;
266
267 switch (arg->format.type) {
268 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
269 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
270 break;
271 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
272 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
273 break;
274 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
275 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
276 errno = EINVAL;
277 return -1;
278 default:
279 return SYS_IOCTL(fd, cmd, arg);
280 }
281
282 cbufs.index = arg->index;
283 cbufs.count = arg->count;
284 cbufs.memory = arg->memory;
285 sanitize_format(org);
286 fmt->fmt.pix_mp.width = org->fmt.pix.width;
287 fmt->fmt.pix_mp.height = org->fmt.pix.height;
288 fmt->fmt.pix_mp.pixelformat = org->fmt.pix.pixelformat;
289 fmt->fmt.pix_mp.field = org->fmt.pix.field;
290 fmt->fmt.pix_mp.colorspace = org->fmt.pix.colorspace;
291 fmt->fmt.pix_mp.xfer_func = org->fmt.pix.xfer_func;
292 fmt->fmt.pix_mp.ycbcr_enc = org->fmt.pix.ycbcr_enc;
293 fmt->fmt.pix_mp.quantization = org->fmt.pix.quantization;
294 fmt->fmt.pix_mp.num_planes = 1;
295 fmt->fmt.pix_mp.flags = org->fmt.pix.flags;
296 fmt->fmt.pix_mp.plane_fmt[0].bytesperline = org->fmt.pix.bytesperline;
297 fmt->fmt.pix_mp.plane_fmt[0].sizeimage = org->fmt.pix.sizeimage;
298
299 ret = SYS_IOCTL(fd, cmd, &cbufs);
300
301 arg->index = cbufs.index;
302 arg->count = cbufs.count;
303 org->fmt.pix.width = fmt->fmt.pix_mp.width;
304 org->fmt.pix.height = fmt->fmt.pix_mp.height;
305 org->fmt.pix.pixelformat = fmt->fmt.pix_mp.pixelformat;
306 org->fmt.pix.field = fmt->fmt.pix_mp.field;
307 org->fmt.pix.colorspace = fmt->fmt.pix_mp.colorspace;
308 org->fmt.pix.xfer_func = fmt->fmt.pix_mp.xfer_func;
309 org->fmt.pix.ycbcr_enc = fmt->fmt.pix_mp.ycbcr_enc;
310 org->fmt.pix.quantization = fmt->fmt.pix_mp.quantization;
311 org->fmt.pix.bytesperline = fmt->fmt.pix_mp.plane_fmt[0].bytesperline;
312 org->fmt.pix.sizeimage = fmt->fmt.pix_mp.plane_fmt[0].sizeimage;
313 org->fmt.pix.flags = fmt->fmt.pix_mp.flags;
314
315 return ret;
316 }
317
318 static int get_fmt_ioctl(int fd, unsigned long int cmd, struct v4l2_format *arg)
319 {
320 struct v4l2_format fmt = { 0 };
321 struct v4l2_format *org = arg;
322 int ret;
323
324 switch (arg->type) {
325 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
326 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
327 break;
328 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
329 fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
330 break;
331 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
332 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
333 errno = EINVAL;
334 return -1;
335 default:
336 return SYS_IOCTL(fd, cmd, arg);
337 }
338
339 ret = SYS_IOCTL(fd, cmd, &fmt);
340 if (ret)
341 return ret;
342
343 memset(&org->fmt.pix, 0, sizeof(org->fmt.pix));
344 org->fmt.pix.width = fmt.fmt.pix_mp.width;
345 org->fmt.pix.height = fmt.fmt.pix_mp.height;
346 org->fmt.pix.pixelformat = fmt.fmt.pix_mp.pixelformat;
347 org->fmt.pix.field = fmt.fmt.pix_mp.field;
348 org->fmt.pix.colorspace = fmt.fmt.pix_mp.colorspace;
349 org->fmt.pix.xfer_func = fmt.fmt.pix_mp.xfer_func;
350 org->fmt.pix.ycbcr_enc = fmt.fmt.pix_mp.ycbcr_enc;
351 org->fmt.pix.quantization = fmt.fmt.pix_mp.quantization;
352 org->fmt.pix.bytesperline = fmt.fmt.pix_mp.plane_fmt[0].bytesperline;
353 org->fmt.pix.sizeimage = fmt.fmt.pix_mp.plane_fmt[0].sizeimage;
354 org->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
355 org->fmt.pix.flags = fmt.fmt.pix_mp.flags;
356
357 /*
358 * If the device doesn't support just one plane, there's
359 * nothing we can do, except return an error condition.
360 */
361 if (fmt.fmt.pix_mp.num_planes > 1) {
362 errno = EINVAL;
363 return -1;
364 }
365
366 return ret;
367 }
368
369 static int buf_ioctl(int fd, unsigned long int cmd, struct v4l2_buffer *arg)
370 {
371 struct v4l2_buffer buf = *arg;
372 struct v4l2_plane plane = { 0 };
373 int ret;
374
375 if (arg->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
376 arg->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
377 errno = EINVAL;
378 return -1;
379 }
380
381 buf.type = convert_type(arg->type);
382
383 if (buf.type == arg->type)
384 return SYS_IOCTL(fd, cmd, &buf);
385
386 memcpy(&plane.m, &arg->m, sizeof(plane.m));
387 plane.length = arg->length;
388 plane.bytesused = arg->bytesused;
389
390 buf.m.planes = &plane;
391 buf.length = 1;
392
393 ret = SYS_IOCTL(fd, cmd, &buf);
394
395 arg->index = buf.index;
396 arg->memory = buf.memory;
397 arg->flags = buf.flags;
398 arg->field = buf.field;
399 arg->timestamp = buf.timestamp;
400 arg->timecode = buf.timecode;
401 arg->sequence = buf.sequence;
402
403 arg->length = plane.length;
404 arg->bytesused = plane.bytesused;
405 memcpy(&arg->m, &plane.m, sizeof(arg->m));
406
407 return ret;
408 }
409
410 static int plugin_ioctl(void *dev_ops_priv, int fd,
411 unsigned long int cmd, void *arg)
412 {
413 switch (cmd) {
414 case VIDIOC_QUERYCAP:
415 return querycap_ioctl(fd, cmd, arg);
416 case VIDIOC_TRY_FMT:
417 case VIDIOC_S_FMT:
418 return try_set_fmt_ioctl(fd, cmd, arg);
419 case VIDIOC_G_FMT:
420 return get_fmt_ioctl(fd, cmd, arg);
421 case VIDIOC_ENUM_FMT:
422 return SIMPLE_CONVERT_IOCTL(fd, cmd, arg, v4l2_fmtdesc);
423 case VIDIOC_S_PARM:
424 case VIDIOC_G_PARM:
425 return SIMPLE_CONVERT_IOCTL(fd, cmd, arg, v4l2_streamparm);
426 case VIDIOC_QBUF:
427 case VIDIOC_DQBUF:
428 case VIDIOC_QUERYBUF:
429 case VIDIOC_PREPARE_BUF:
430 return buf_ioctl(fd, cmd, arg);
431 case VIDIOC_CREATE_BUFS:
432 return create_bufs_ioctl(fd, cmd, arg);
433 case VIDIOC_REQBUFS:
434 return SIMPLE_CONVERT_IOCTL(fd, cmd, arg, v4l2_requestbuffers);
435 case VIDIOC_STREAMON:
436 case VIDIOC_STREAMOFF:
437 {
438 int type = *(int *)arg;
439
440 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
441 type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
442 errno = EINVAL;
443 return -1;
444 }
445 type = convert_type(type);
446
447 return SYS_IOCTL(fd, cmd, &type);
448 }
449 /* CASE VIDIOC_EXPBUF: */
450 default:
451 return SYS_IOCTL(fd, cmd, arg);
452 }
453 }
454
455 static ssize_t plugin_read(void *dev_ops_priv, int fd, void *buf, size_t len)
456 {
457 return SYS_READ(fd, buf, len);
458 }
459
460 static ssize_t plugin_write(void *dev_ops_priv, int fd, const void *buf,
461 size_t len)
462 {
463 return SYS_WRITE(fd, buf, len);
464 }
465
466 PLUGIN_PUBLIC const struct libv4l_dev_ops libv4l2_plugin = {
467 .init = &plugin_init,
468 .close = &plugin_close,
469 .ioctl = &plugin_ioctl,
470 .read = &plugin_read,
471 .write = &plugin_write,
472 };