1 #include "v4l_output.h"
5 #include <linux/videodev2.h>
10 #include <sys/ioctl.h>
13 #include "shared/memcpy_interleaved.h"
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])
20 video_fd = open(device_path, O_WRONLY);
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);
38 perror("ioctl(VIDIOC_S_FMT)");
43 V4LOutput::~V4LOutput()
48 void V4LOutput::send_frame(const uint8_t *data)
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);
57 yuv420_buf.get() + luma_size,
58 yuv420_buf.get() + luma_size + chroma_size,
59 data + luma_size, 2 * chroma_size);
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);
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);