]> git.sesse.net Git - nageru/blob - nageru/v4l_output.cpp
IWYU-fix nageru/*.cpp.
[nageru] / nageru / v4l_output.cpp
1 #include "v4l_output.h"
2
3 #include <assert.h>
4 #include <fcntl.h>
5 #include <linux/videodev2.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/ioctl.h>
11 #include <unistd.h>
12
13 #include "shared/memcpy_interleaved.h"
14
15 V4LOutput::V4LOutput(const char *device_path, unsigned width, unsigned height)
16         : width(width), height(height),
17           image_size_bytes(width * height + (width / 2) * (height / 2) * 2),
18           yuv420_buf(new uint8_t[image_size_bytes])
19 {
20         video_fd = open(device_path, O_WRONLY);
21         if (video_fd == -1) {
22                 perror(device_path);
23                 exit(1);
24         }
25
26         v4l2_format fmt;
27         memset(&fmt, 0, sizeof(fmt));
28         fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
29         fmt.fmt.pix.width = width;
30         fmt.fmt.pix.height = width;
31         fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
32         fmt.fmt.pix.field = V4L2_FIELD_NONE;
33         fmt.fmt.pix.bytesperline = 0;
34         fmt.fmt.pix.sizeimage = image_size_bytes;
35         fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
36         int err = ioctl(video_fd, VIDIOC_S_FMT, &fmt);
37         if (err == -1) {
38                 perror("ioctl(VIDIOC_S_FMT)");
39                 exit(1);
40         }
41 }
42
43 V4LOutput::~V4LOutput()
44 {
45         close(video_fd);
46 }
47
48 void V4LOutput::send_frame(const uint8_t *data)
49 {
50         // Seemingly NV12 isn't a very common format among V4L2 consumers,
51         // so we convert from our usual NV12 to YUV420. We get an unneeded
52         // memcpy() of the luma data, but hopefully, we'll manage.
53         const size_t luma_size = width * height;
54         const size_t chroma_size = (width / 2) * (height / 2);
55         memcpy(yuv420_buf.get(), data, luma_size);
56         memcpy_interleaved(
57                 yuv420_buf.get() + luma_size,
58                 yuv420_buf.get() + luma_size + chroma_size,
59                 data + luma_size, 2 * chroma_size);
60
61         const uint8_t *ptr = yuv420_buf.get();
62         size_t bytes_left = image_size_bytes;
63         while (bytes_left > 0) {
64                 int err = write(video_fd, ptr, bytes_left);
65                 if (err == -1) {
66                         perror("V4L write");
67                         exit(1);
68                 }
69                 if (err == 0) {
70                         fprintf(stderr, "WARNING: Short V4L write() (only wrote %zu of %zu bytes), skipping rest of frame.\n",
71                                 image_size_bytes - bytes_left, image_size_bytes);
72                         return;
73                 }
74                 assert(err > 0);
75                 bytes_left -= err;
76                 ptr += err;
77         }
78 }