1 /*  RetroArch - A frontend for libretro.
2  *  Copyright (C) 2010-2014 - Hans-Kristian Arntzen
3  *  Copyright (C) 2011-2017 - Daniel De Matteis
4  *  Copyright (C) 2012-2015 - Michael Lelli
5  *
6  *  RetroArch is free software: you can redistribute it and/or modify it under the terms
7  *  of the GNU General Public License as published by the Free Software Found-
8  *  ation, either version 3 of the License, or (at your option) any later version.
9  *
10  *  RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  *  PURPOSE.  See the GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along with RetroArch.
15  *  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdio.h>
19 #if !defined(__FreeBSD__) || __FreeBSD__ < 5
20 #include <stdlib.h>
21 #endif
22 #include <string.h>
23 #include <assert.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/ioctl.h>
32 
33 #ifndef __FreeBSD__
34 #include <sys/types.h>
35 #endif
36 #include <linux/videodev2.h>
37 
38 #include <memmap.h>
39 
40 #include <retro_assert.h>
41 #include <retro_miscellaneous.h>
42 #include <gfx/scaler/scaler.h>
43 #include <gfx/video_frame.h>
44 #include <file/file_path.h>
45 
46 #include <compat/strl.h>
47 
48 #include "../../retroarch.h"
49 #include "../../verbosity.h"
50 
51 struct buffer
52 {
53    void *start;
54    size_t length;
55 };
56 
57 typedef struct video4linux
58 {
59    int fd;
60    struct buffer *buffers;
61    unsigned n_buffers;
62    unsigned width;
63    unsigned height;
64    size_t pitch;
65 
66    struct scaler_ctx scaler;
67    uint32_t *buffer_output;
68    bool ready;
69 
70    char dev_name[255];
71 } video4linux_t;
72 
xioctl(int fd,unsigned long request,void * args)73 static int xioctl(int fd, unsigned long request, void *args)
74 {
75    int r;
76 
77    do
78    {
79       r = ioctl(fd, request, args);
80    } while (r == -1 && errno == EINTR);
81 
82    return r;
83 }
84 
init_mmap(void * data)85 static bool init_mmap(void *data)
86 {
87    struct v4l2_requestbuffers req = {0};
88    video4linux_t *v4l             = (video4linux_t*)data;
89 
90    req.count                      = 4;
91    req.type                       = V4L2_BUF_TYPE_VIDEO_CAPTURE;
92    req.memory                     = V4L2_MEMORY_MMAP;
93 
94    if (xioctl(v4l->fd, VIDIOC_REQBUFS, &req) == -1)
95    {
96       if (errno == EINVAL)
97          RARCH_ERR("[V4L2]: %s does not support memory mapping.\n", v4l->dev_name);
98       else
99          RARCH_ERR("[V4L2]: xioctl of VIDIOC_REQBUFS failed.\n");
100       return false;
101    }
102 
103    if (req.count < 2)
104    {
105       RARCH_ERR("[V4L2]: Insufficient buffer memory on %s.\n", v4l->dev_name);
106       return false;
107    }
108 
109    v4l->buffers = (struct buffer*)calloc(req.count, sizeof(*v4l->buffers));
110 
111    if (!v4l->buffers)
112    {
113       RARCH_ERR("[V4L2]: Out of memory allocating V4L2 buffers.\n");
114       return false;
115    }
116 
117    for (v4l->n_buffers = 0; v4l->n_buffers < req.count; v4l->n_buffers++)
118    {
119       struct v4l2_buffer buf = {0};
120 
121       buf.type   = V4L2_BUF_TYPE_VIDEO_CAPTURE;
122       buf.memory = V4L2_MEMORY_MMAP;
123       buf.index  = v4l->n_buffers;
124 
125       if (xioctl(v4l->fd, VIDIOC_QUERYBUF, &buf) == -1)
126       {
127          RARCH_ERR("[V4L2]: Error - xioctl VIDIOC_QUERYBUF.\n");
128          return false;
129       }
130 
131       v4l->buffers[v4l->n_buffers].length = buf.length;
132       v4l->buffers[v4l->n_buffers].start  = mmap(NULL,
133             buf.length, PROT_READ | PROT_WRITE,
134             MAP_SHARED,
135             v4l->fd, buf.m.offset);
136 
137       if (v4l->buffers[v4l->n_buffers].start == MAP_FAILED)
138       {
139          RARCH_ERR("[V4L2]: Error - mmap.\n");
140          return false;
141       }
142    }
143 
144    return true;
145 }
146 
init_device(void * data)147 static bool init_device(void *data)
148 {
149    struct v4l2_crop crop;
150    struct v4l2_capability cap;
151    struct v4l2_format      fmt = {0};
152    struct v4l2_cropcap cropcap = {0};
153    video4linux_t          *v4l = (video4linux_t*)data;
154 
155    if (xioctl(v4l->fd, VIDIOC_QUERYCAP, &cap) < 0)
156    {
157       if (errno == EINVAL)
158          RARCH_ERR("[V4L2]: %s is no V4L2 device.\n", v4l->dev_name);
159       else
160          RARCH_ERR("[V4L2]: Error - VIDIOC_QUERYCAP.\n");
161       return false;
162    }
163 
164    if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
165    {
166       RARCH_ERR("[V4L2]: %s is no video capture device.\n", v4l->dev_name);
167       return false;
168    }
169 
170    if (!(cap.capabilities & V4L2_CAP_STREAMING))
171    {
172       RARCH_ERR("[V4L2]: %s does not support streaming I/O (V4L2_CAP_STREAMING).\n",
173             v4l->dev_name);
174       return false;
175    }
176 
177    cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
178 
179    if (xioctl(v4l->fd, VIDIOC_CROPCAP, &cropcap) == 0)
180    {
181       crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
182       crop.c    = cropcap.defrect;
183       /* Ignore errors here. */
184       xioctl(v4l->fd, VIDIOC_S_CROP, &crop);
185    }
186 
187    fmt.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;
188    fmt.fmt.pix.width       = v4l->width;
189    fmt.fmt.pix.height      = v4l->height;
190    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
191    fmt.fmt.pix.field       = V4L2_FIELD_NONE;
192 
193    if (xioctl(v4l->fd, VIDIOC_S_FMT, &fmt) < 0)
194    {
195       RARCH_ERR("[V4L2]: Error - VIDIOC_S_FMT\n");
196       return false;
197    }
198 
199    /* VIDIOC_S_FMT may change width, height and pitch. */
200    v4l->width  = fmt.fmt.pix.width;
201    v4l->height = fmt.fmt.pix.height;
202    v4l->pitch  = MAX(fmt.fmt.pix.bytesperline, v4l->width * 2);
203 
204    /* Sanity check to see if our assumptions are met.
205     * It is possible to support whatever the device gives us,
206     * but this dramatically increases complexity.
207     */
208    if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV)
209    {
210       RARCH_ERR("[V4L2]: The V4L2 device doesn't support YUYV.\n");
211       return false;
212    }
213 
214    if (fmt.fmt.pix.field != V4L2_FIELD_NONE
215          && fmt.fmt.pix.field != V4L2_FIELD_INTERLACED)
216    {
217       RARCH_ERR("[V4L2]: The V4L2 device doesn't support progressive nor interlaced video.\n");
218       return false;
219    }
220 
221    RARCH_LOG("[V4L2]: device: %u x %u.\n", v4l->width, v4l->height);
222 
223    return init_mmap(v4l);
224 }
225 
v4l_stop(void * data)226 static void v4l_stop(void *data)
227 {
228    video4linux_t *v4l = (video4linux_t*)data;
229    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
230 
231    if (xioctl(v4l->fd, VIDIOC_STREAMOFF, &type) == -1)
232       RARCH_ERR("[V4L2]: Error - VIDIOC_STREAMOFF.\n");
233 
234    v4l->ready = false;
235 }
236 
v4l_start(void * data)237 static bool v4l_start(void *data)
238 {
239    unsigned i;
240    enum v4l2_buf_type type;
241    video4linux_t *v4l = (video4linux_t*)data;
242 
243    for (i = 0; i < v4l->n_buffers; i++)
244    {
245       struct v4l2_buffer buf = {0};
246 
247       buf.type   = V4L2_BUF_TYPE_VIDEO_CAPTURE;
248       buf.memory = V4L2_MEMORY_MMAP;
249       buf.index  = i;
250 
251       if (xioctl(v4l->fd, VIDIOC_QBUF, &buf) == -1)
252       {
253          RARCH_ERR("[V4L2]: Error - VIDIOC_QBUF.\n");
254          return false;
255       }
256    }
257 
258    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
259 
260    if (xioctl(v4l->fd, VIDIOC_STREAMON, &type) == -1)
261    {
262       RARCH_ERR("[V4L2]: Error - VIDIOC_STREAMON.\n");
263       return false;
264    }
265 
266    v4l->ready = true;
267 
268    return true;
269 }
270 
v4l_free(void * data)271 static void v4l_free(void *data)
272 {
273    video4linux_t *v4l = (video4linux_t*)data;
274 
275    unsigned i;
276    for (i = 0; i < v4l->n_buffers; i++)
277       if (munmap(v4l->buffers[i].start, v4l->buffers[i].length) == -1)
278          RARCH_ERR("[V4L2]: munmap failed.\n");
279 
280    if (v4l->fd >= 0)
281       close(v4l->fd);
282 
283    free(v4l->buffer_output);
284    scaler_ctx_gen_reset(&v4l->scaler);
285    free(v4l);
286 }
287 
v4l_init(const char * device,uint64_t caps,unsigned width,unsigned height)288 static void *v4l_init(const char *device, uint64_t caps,
289       unsigned width, unsigned height)
290 {
291    video4linux_t *v4l = NULL;
292 
293    if ((caps & (UINT64_C(1) << RETRO_CAMERA_BUFFER_RAW_FRAMEBUFFER)) == 0)
294    {
295       RARCH_ERR("[V4L2]: Returns raw framebuffers.\n");
296       return NULL;
297    }
298 
299    v4l = (video4linux_t*)calloc(1, sizeof(video4linux_t));
300    if (!v4l)
301       return NULL;
302 
303    strlcpy(v4l->dev_name, device ? device : "/dev/video0",
304          sizeof(v4l->dev_name));
305 
306    v4l->width  = width;
307    v4l->height = height;
308    v4l->ready  = false;
309 
310    if (!path_is_character_special(v4l->dev_name))
311    {
312       RARCH_ERR("[V4L2]: %s is no device.\n", v4l->dev_name);
313       goto error;
314    }
315 
316    v4l->fd = open(v4l->dev_name, O_RDWR | O_NONBLOCK, 0);
317 
318    if (v4l->fd == -1)
319    {
320       RARCH_ERR("[V4L2]: Cannot open '%s': %d, %s\n", v4l->dev_name,
321             errno, strerror(errno));
322       goto error;
323    }
324 
325    if (!init_device(v4l))
326       goto error;
327 
328    v4l->buffer_output = (uint32_t*)
329       malloc(v4l->width * v4l->height * sizeof(uint32_t));
330 
331    if (!v4l->buffer_output)
332    {
333       RARCH_ERR("[V4L2]: Failed to allocate output buffer.\n");
334       goto error;
335    }
336 
337    v4l->scaler.in_width   = v4l->scaler.out_width = v4l->width;
338    v4l->scaler.in_height  = v4l->scaler.out_height = v4l->height;
339    v4l->scaler.in_fmt     = SCALER_FMT_YUYV;
340    v4l->scaler.out_fmt    = SCALER_FMT_ARGB8888;
341    v4l->scaler.in_stride  = v4l->pitch;
342    v4l->scaler.out_stride = v4l->width * 4;
343 
344    if (!scaler_ctx_gen_filter(&v4l->scaler))
345    {
346       RARCH_ERR("[V4L2]: Failed to create scaler.\n");
347       goto error;
348    }
349 
350    return v4l;
351 
352 error:
353    RARCH_ERR("[V4L2]: Failed to initialize camera.\n");
354    v4l_free(v4l);
355    return NULL;
356 }
357 
preprocess_image(void * data)358 static bool preprocess_image(void *data)
359 {
360    struct scaler_ctx *ctx = NULL;
361    video4linux_t     *v4l = (video4linux_t*)data;
362    struct v4l2_buffer buf = {0};
363 
364    buf.type   = V4L2_BUF_TYPE_VIDEO_CAPTURE;
365    buf.memory = V4L2_MEMORY_MMAP;
366 
367    if (xioctl(v4l->fd, VIDIOC_DQBUF, &buf) == -1)
368    {
369       switch (errno)
370       {
371          case EAGAIN:
372             break;
373          default:
374             RARCH_ERR("[V4L2]: VIDIOC_DQBUF.\n");
375             break;
376       }
377 
378       return false;
379    }
380 
381    retro_assert(buf.index < v4l->n_buffers);
382 
383    ctx = &v4l->scaler;
384 
385    scaler_ctx_scale_direct(ctx, v4l->buffer_output, (const uint8_t*)v4l->buffers[buf.index].start);
386 
387    if (xioctl(v4l->fd, VIDIOC_QBUF, &buf) == -1)
388       RARCH_ERR("[V4L2]: VIDIOC_QBUF\n");
389 
390    return true;
391 }
392 
v4l_poll(void * data,retro_camera_frame_raw_framebuffer_t frame_raw_cb,retro_camera_frame_opengl_texture_t frame_gl_cb)393 static bool v4l_poll(void *data,
394       retro_camera_frame_raw_framebuffer_t frame_raw_cb,
395       retro_camera_frame_opengl_texture_t frame_gl_cb)
396 {
397    video4linux_t *v4l = (video4linux_t*)data;
398    if (!v4l->ready)
399       return false;
400 
401    (void)frame_gl_cb;
402 
403    if (preprocess_image(data))
404    {
405       if (frame_raw_cb)
406          frame_raw_cb(v4l->buffer_output, v4l->width,
407                v4l->height, v4l->width * 4);
408       return true;
409    }
410 
411    return false;
412 }
413 
414 camera_driver_t camera_v4l2 = {
415    v4l_init,
416    v4l_free,
417    v4l_start,
418    v4l_stop,
419    v4l_poll,
420    "video4linux2",
421 };
422