Debian Codegraph @

lib/libv4l2/v4l2convert.c

raw
1 /*
2 # open/close/ioctl/mmap/munmap library call wrapper doing format conversion
3 # for v4l2 applications which want to be able to simply capture bgr24 / yuv420
4 # from v4l2 devices with more exotic frame formats.
5
6 # (C) 2008 Hans de Goede <[email protected]>
7
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 2.1 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
21 */
22
23 /* prevent GCC 4.7 inlining error */
24 #undef _FORTIFY_SOURCE
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/mman.h>
32 #if defined(__OpenBSD__)
33 #include <sys/videoio.h>
34 #else
35 #include <linux/videodev2.h>
36 #endif
37 #include <libv4l2.h>
38 #include "../libv4lconvert/libv4lsyscall-priv.h"
39
40 /* Check that open/read/mmap is not a define */
41 #if defined open || defined read || defined mmap
42 #error open/read/mmap is a prepocessor macro !!
43 #endif
44
45 #if HAVE_VISIBILITY
46 #define LIBV4L_PUBLIC __attribute__ ((visibility("default")))
47 #else
48 #define LIBV4L_PUBLIC
49 #endif
50
51 LIBV4L_PUBLIC int open(const char *file, int oflag, ...)
52 {
53 int fd;
54 int v4l_device = 0;
55
56 /* check if we're opening a video4linux2 device */
57 if (!strncmp(file, "/dev/video", 10) || !strncmp(file, "/dev/v4l/", 9)) {
58 /* Some apps open the device read-only, but we need rw rights as the
59 buffers *MUST* be mapped rw */
60 oflag = (oflag & ~O_ACCMODE) | O_RDWR;
61 v4l_device = 1;
62 }
63
64 /* original open code */
65 if (oflag & O_CREAT) {
66 va_list ap;
67 mode_t mode;
68
69 va_start(ap, oflag);
70 mode = va_arg(ap, PROMOTED_MODE_T);
71
72 fd = SYS_OPEN(file, oflag, mode);
73
74 va_end(ap);
75 } else {
76 fd = SYS_OPEN(file, oflag, 0);
77 }
78 /* end of original open code */
79
80 if (fd == -1 || !v4l_device)
81 return fd;
82
83 /* Try to Register with libv4l2 (in case of failure pass the fd to the
84 application as is) */
85 v4l2_fd_open(fd, 0);
86
87 return fd;
88 }
89
90 #if defined(linux) && defined(__GLIBC__)
91 LIBV4L_PUBLIC int open64(const char *file, int oflag, ...)
92 {
93 int fd;
94
95 /* original open code */
96 if (oflag & O_CREAT) {
97 va_list ap;
98 mode_t mode;
99
100 va_start(ap, oflag);
101 mode = va_arg(ap, PROMOTED_MODE_T);
102
103 fd = open(file, oflag | O_LARGEFILE, mode);
104
105 va_end(ap);
106 } else {
107 fd = open(file, oflag | O_LARGEFILE);
108 }
109 /* end of original open code */
110
111 return fd;
112 }
113 #endif
114
115 #ifndef ANDROID
116 LIBV4L_PUBLIC int close(int fd)
117 {
118 return v4l2_close(fd);
119 }
120
121 LIBV4L_PUBLIC int dup(int fd)
122 {
123 return v4l2_dup(fd);
124 }
125
126 #ifdef LIBV4L_HAVE_POSIX_IOCTL
127 LIBV4L_PUBLIC int ioctl(int fd, int request, ...)
128 #else
129 LIBV4L_PUBLIC int ioctl(int fd, unsigned long int request, ...)
130 #endif
131 {
132 void *arg;
133 va_list ap;
134
135 va_start(ap, request);
136 arg = va_arg(ap, void *);
137 va_end(ap);
138
139 return v4l2_ioctl(fd, request, arg);
140 }
141
142 LIBV4L_PUBLIC ssize_t read(int fd, void *buffer, size_t n)
143 {
144 return v4l2_read(fd, buffer, n);
145 }
146
147 LIBV4L_PUBLIC void *mmap(void *start, size_t length, int prot, int flags, int fd,
148 off_t offset)
149 {
150 return v4l2_mmap(start, length, prot, flags, fd, offset);
151 }
152
153 #if defined(linux) && defined(__GLIBC__)
154 LIBV4L_PUBLIC void *mmap64(void *start, size_t length, int prot, int flags, int fd,
155 off64_t offset)
156 {
157 return v4l2_mmap(start, length, prot, flags, fd, offset);
158 }
159 #endif
160
161 LIBV4L_PUBLIC int munmap(void *start, size_t length)
162 {
163 return v4l2_munmap(start, length);
164 }
165 #endif