Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 1 | #include "frc971/vision/v4l2_reader.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 2 | |
| 3 | #include <fcntl.h> |
| 4 | #include <linux/videodev2.h> |
| 5 | #include <sys/ioctl.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <sys/types.h> |
| 8 | |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 9 | DEFINE_bool(ignore_timestamps, false, |
| 10 | "Don't require timestamps on images. Used to allow webcams"); |
| 11 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 12 | namespace frc971::vision { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 13 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 14 | V4L2ReaderBase::V4L2ReaderBase(aos::EventLoop *event_loop, |
milind-u | fd08c43 | 2023-02-05 15:15:21 -0800 | [diff] [blame] | 15 | std::string_view device_name, |
| 16 | std::string_view image_channel) |
| 17 | : fd_(open(device_name.data(), O_RDWR | O_NONBLOCK)), |
| 18 | event_loop_(event_loop), |
| 19 | image_channel_(image_channel) { |
Jim Ostrowski | fec0c33 | 2022-02-06 23:28:26 -0800 | [diff] [blame] | 20 | PCHECK(fd_.get() != -1) << " Failed to open device " << device_name; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 21 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 22 | // Figure out if we are multi-planar or not. |
| 23 | { |
| 24 | struct v4l2_capability capability; |
| 25 | memset(&capability, 0, sizeof(capability)); |
| 26 | PCHECK(Ioctl(VIDIOC_QUERYCAP, &capability) == 0); |
| 27 | |
| 28 | LOG(INFO) << "Opening " << device_name; |
| 29 | LOG(INFO) << " driver " << capability.driver; |
| 30 | LOG(INFO) << " card " << capability.card; |
| 31 | LOG(INFO) << " bus_info " << capability.bus_info; |
| 32 | if (capability.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) { |
| 33 | LOG(INFO) << " Multi-planar"; |
| 34 | multiplanar_ = true; |
| 35 | } |
| 36 | } |
| 37 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 38 | // First, clean up after anybody else who left the device streaming. |
Brian Silverman | 8f24adb | 2020-02-02 17:15:58 -0800 | [diff] [blame] | 39 | StreamOff(); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 40 | } |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 41 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 42 | void V4L2ReaderBase::StreamOn() { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 43 | { |
| 44 | struct v4l2_requestbuffers request; |
| 45 | memset(&request, 0, sizeof(request)); |
| 46 | request.count = buffers_.size(); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 47 | request.type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 48 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 49 | request.memory = V4L2_MEMORY_USERPTR; |
| 50 | PCHECK(Ioctl(VIDIOC_REQBUFS, &request) == 0); |
| 51 | CHECK_EQ(request.count, buffers_.size()) |
| 52 | << ": Kernel refused to give us the number of buffers we asked for"; |
| 53 | } |
| 54 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 55 | { |
| 56 | struct v4l2_format format; |
| 57 | memset(&format, 0, sizeof(format)); |
| 58 | format.type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 59 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 60 | PCHECK(Ioctl(VIDIOC_G_FMT, &format) == 0); |
| 61 | |
| 62 | if (multiplanar()) { |
| 63 | cols_ = format.fmt.pix_mp.width; |
| 64 | rows_ = format.fmt.pix_mp.height; |
| 65 | LOG(INFO) << "Format is " << cols_ << ", " << rows_; |
| 66 | CHECK_EQ(format.fmt.pix_mp.pixelformat, V4L2_PIX_FMT_YUYV) |
| 67 | << ": Invalid pixel format"; |
| 68 | |
| 69 | CHECK_EQ(format.fmt.pix_mp.num_planes, 1u); |
| 70 | |
| 71 | CHECK_EQ(static_cast<int>(format.fmt.pix_mp.plane_fmt[0].bytesperline), |
| 72 | cols_ * 2 /* bytes per pixel */); |
| 73 | CHECK_EQ(format.fmt.pix_mp.plane_fmt[0].sizeimage, ImageSize()); |
| 74 | } else { |
| 75 | cols_ = format.fmt.pix.width; |
| 76 | rows_ = format.fmt.pix.height; |
| 77 | LOG(INFO) << "Format is " << cols_ << ", " << rows_; |
| 78 | CHECK_EQ(format.fmt.pix.pixelformat, V4L2_PIX_FMT_YUYV) |
| 79 | << ": Invalid pixel format"; |
| 80 | |
| 81 | CHECK_EQ(static_cast<int>(format.fmt.pix.bytesperline), |
| 82 | cols_ * 2 /* bytes per pixel */); |
| 83 | CHECK_EQ(format.fmt.pix.sizeimage, ImageSize()); |
| 84 | } |
| 85 | } |
| 86 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 87 | for (size_t i = 0; i < buffers_.size(); ++i) { |
milind-u | fd08c43 | 2023-02-05 15:15:21 -0800 | [diff] [blame] | 88 | buffers_[i].sender = event_loop_->MakeSender<CameraImage>(image_channel_); |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 89 | MarkBufferToBeEnqueued(i); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 90 | } |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 91 | int type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 92 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 93 | PCHECK(Ioctl(VIDIOC_STREAMON, &type) == 0); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 96 | void V4L2ReaderBase::MarkBufferToBeEnqueued(int buffer_index) { |
| 97 | ReinitializeBuffer(buffer_index); |
| 98 | EnqueueBuffer(buffer_index); |
| 99 | } |
| 100 | |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 101 | void V4L2ReaderBase::MaybeEnqueue() { |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 102 | // First, enqueue any old buffer we already have. This is the one which |
| 103 | // may have been sent. |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 104 | if (saved_buffer_) { |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 105 | MarkBufferToBeEnqueued(saved_buffer_.index); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 106 | saved_buffer_.Clear(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 107 | } |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 108 | ftrace_.FormatMessage("Enqueued previous buffer %d", saved_buffer_.index); |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool V4L2ReaderBase::ReadLatestImage() { |
| 112 | MaybeEnqueue(); |
| 113 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 114 | while (true) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 115 | const BufferInfo previous_buffer = saved_buffer_; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 116 | saved_buffer_ = DequeueBuffer(); |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 117 | ftrace_.FormatMessage("Dequeued %d", saved_buffer_.index); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 118 | if (saved_buffer_) { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 119 | // We got a new buffer. Return the previous one (if relevant) and keep |
| 120 | // going. |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 121 | if (previous_buffer) { |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 122 | ftrace_.FormatMessage("Previous %d", previous_buffer.index); |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 123 | MarkBufferToBeEnqueued(previous_buffer.index); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 124 | } |
| 125 | continue; |
| 126 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 127 | if (!previous_buffer) { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 128 | // There were no images to read. Return an indication of that. |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 129 | ftrace_.FormatMessage("No images to read"); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 130 | return false; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 131 | } |
| 132 | // We didn't get a new one, but we already got one in a previous |
| 133 | // iteration, which means we found an image so return it. |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 134 | ftrace_.FormatMessage("Got saved buffer %d", saved_buffer_.index); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 135 | saved_buffer_ = previous_buffer; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 136 | buffers_[saved_buffer_.index].PrepareMessage(rows_, cols_, ImageSize(), |
| 137 | saved_buffer_.monotonic_eof); |
| 138 | return true; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 142 | void V4L2ReaderBase::SendLatestImage() { |
| 143 | buffers_[saved_buffer_.index].Send(); |
| 144 | |
| 145 | MarkBufferToBeEnqueued(saved_buffer_.index); |
| 146 | saved_buffer_.Clear(); |
| 147 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 148 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 149 | void V4L2ReaderBase::SetExposure(size_t duration) { |
milind-u | daebe9b | 2022-01-09 18:25:24 -0800 | [diff] [blame] | 150 | v4l2_control manual_control; |
| 151 | manual_control.id = V4L2_CID_EXPOSURE_AUTO; |
| 152 | manual_control.value = V4L2_EXPOSURE_MANUAL; |
| 153 | PCHECK(Ioctl(VIDIOC_S_CTRL, &manual_control) == 0); |
| 154 | |
| 155 | v4l2_control exposure_control; |
| 156 | exposure_control.id = V4L2_CID_EXPOSURE_ABSOLUTE; |
| 157 | exposure_control.value = static_cast<int>(duration); // 100 micro s units |
| 158 | PCHECK(Ioctl(VIDIOC_S_CTRL, &exposure_control) == 0); |
| 159 | } |
| 160 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 161 | void V4L2ReaderBase::UseAutoExposure() { |
milind-u | daebe9b | 2022-01-09 18:25:24 -0800 | [diff] [blame] | 162 | v4l2_control control; |
| 163 | control.id = V4L2_CID_EXPOSURE_AUTO; |
| 164 | control.value = V4L2_EXPOSURE_AUTO; |
| 165 | PCHECK(Ioctl(VIDIOC_S_CTRL, &control) == 0); |
| 166 | } |
| 167 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 168 | void V4L2ReaderBase::Buffer::InitializeMessage(size_t max_image_size) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 169 | message_offset = flatbuffers::Offset<CameraImage>(); |
| 170 | builder = aos::Sender<CameraImage>::Builder(); |
| 171 | builder = sender.MakeBuilder(); |
| 172 | // The kernel has an undocumented requirement that the buffer is aligned |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 173 | // to 128 bytes. If you give it a nonaligned pointer, it will return EINVAL |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 174 | // and only print something in dmesg with the relevant dynamic debug |
| 175 | // prints turned on. |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 176 | builder.fbb()->StartIndeterminateVector(max_image_size, 1, 128, |
| 177 | &data_pointer); |
| 178 | CHECK_EQ(reinterpret_cast<uintptr_t>(data_pointer) % 128, 0u) |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 179 | << ": Flatbuffers failed to align things as requested"; |
| 180 | } |
| 181 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 182 | void V4L2ReaderBase::Buffer::PrepareMessage( |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 183 | int rows, int cols, size_t image_size, |
| 184 | aos::monotonic_clock::time_point monotonic_eof) { |
| 185 | CHECK(data_pointer != nullptr); |
| 186 | data_pointer = nullptr; |
| 187 | |
| 188 | const auto data_offset = builder.fbb()->EndIndeterminateVector(image_size, 1); |
| 189 | auto image_builder = builder.MakeBuilder<CameraImage>(); |
| 190 | image_builder.add_data(data_offset); |
| 191 | image_builder.add_rows(rows); |
| 192 | image_builder.add_cols(cols); |
| 193 | image_builder.add_monotonic_timestamp_ns( |
| 194 | std::chrono::nanoseconds(monotonic_eof.time_since_epoch()).count()); |
| 195 | message_offset = image_builder.Finish(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 198 | int V4L2ReaderBase::Ioctl(unsigned long number, void *arg) { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 199 | return ioctl(fd_.get(), number, arg); |
| 200 | } |
| 201 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 202 | V4L2ReaderBase::BufferInfo V4L2ReaderBase::DequeueBuffer() { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 203 | struct v4l2_buffer buffer; |
| 204 | memset(&buffer, 0, sizeof(buffer)); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 205 | buffer.memory = V4L2_MEMORY_USERPTR; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 206 | if (multiplanar()) { |
| 207 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 208 | struct v4l2_plane planes[1]; |
| 209 | std::memset(planes, 0, sizeof(planes)); |
| 210 | buffer.m.planes = planes; |
| 211 | buffer.length = 1; |
| 212 | const int result = Ioctl(VIDIOC_DQBUF, &buffer); |
| 213 | if (result == -1 && errno == EAGAIN) { |
| 214 | return BufferInfo(); |
| 215 | } |
| 216 | PCHECK(result == 0) << ": VIDIOC_DQBUF failed"; |
| 217 | CHECK_LT(buffer.index, buffers_.size()); |
| 218 | |
| 219 | CHECK_EQ(reinterpret_cast<uintptr_t>(buffers_[buffer.index].data_pointer), |
| 220 | planes[0].m.userptr); |
| 221 | |
| 222 | CHECK_EQ(ImageSize(), planes[0].length); |
| 223 | } else { |
| 224 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 225 | const int result = Ioctl(VIDIOC_DQBUF, &buffer); |
| 226 | if (result == -1 && errno == EAGAIN) { |
| 227 | return BufferInfo(); |
| 228 | } |
| 229 | PCHECK(result == 0) << ": VIDIOC_DQBUF failed"; |
| 230 | CHECK_LT(buffer.index, buffers_.size()); |
| 231 | CHECK_EQ(reinterpret_cast<uintptr_t>(buffers_[buffer.index].data_pointer), |
| 232 | buffer.m.userptr); |
| 233 | CHECK_EQ(ImageSize(), buffer.length); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 234 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 235 | CHECK(buffer.flags & V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC); |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 236 | if (!FLAGS_ignore_timestamps) { |
| 237 | // Require that we have good timestamp on images |
| 238 | CHECK_EQ(buffer.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK, |
| 239 | static_cast<uint32_t>(V4L2_BUF_FLAG_TSTAMP_SRC_EOF)); |
| 240 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 241 | return {static_cast<int>(buffer.index), |
| 242 | aos::time::from_timeval(buffer.timestamp)}; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 245 | void V4L2ReaderBase::EnqueueBuffer(int buffer_number) { |
| 246 | // TODO(austin): Detect multiplanar and do this all automatically. |
| 247 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 248 | CHECK_GE(buffer_number, 0); |
| 249 | CHECK_LT(buffer_number, static_cast<int>(buffers_.size())); |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 250 | CHECK(buffers_[buffer_number].data_pointer != nullptr); |
| 251 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 252 | struct v4l2_buffer buffer; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 253 | struct v4l2_plane planes[1]; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 254 | memset(&buffer, 0, sizeof(buffer)); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 255 | memset(&planes, 0, sizeof(planes)); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 256 | buffer.memory = V4L2_MEMORY_USERPTR; |
| 257 | buffer.index = buffer_number; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 258 | if (multiplanar()) { |
| 259 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 260 | buffer.m.planes = planes; |
| 261 | buffer.length = 1; |
| 262 | planes[0].m.userptr = |
| 263 | reinterpret_cast<uintptr_t>(buffers_[buffer_number].data_pointer); |
| 264 | planes[0].length = ImageSize(); |
| 265 | planes[0].bytesused = planes[0].length; |
| 266 | } else { |
| 267 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 268 | buffer.m.userptr = |
| 269 | reinterpret_cast<uintptr_t>(buffers_[buffer_number].data_pointer); |
| 270 | buffer.length = ImageSize(); |
| 271 | } |
| 272 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 273 | PCHECK(Ioctl(VIDIOC_QBUF, &buffer) == 0); |
| 274 | } |
| 275 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 276 | void V4L2ReaderBase::StreamOff() { |
| 277 | int type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 278 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Brian Silverman | 8f24adb | 2020-02-02 17:15:58 -0800 | [diff] [blame] | 279 | const int result = Ioctl(VIDIOC_STREAMOFF, &type); |
| 280 | if (result == 0) { |
| 281 | return; |
| 282 | } |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 283 | // Some devices (like Alex's webcam) return this if streaming isn't |
| 284 | // currently on, unlike what the documentations says should happen. |
Brian Silverman | 8f24adb | 2020-02-02 17:15:58 -0800 | [diff] [blame] | 285 | if (errno == EBUSY) { |
| 286 | return; |
| 287 | } |
| 288 | PLOG(FATAL) << "VIDIOC_STREAMOFF failed"; |
| 289 | } |
| 290 | |
milind-u | fd08c43 | 2023-02-05 15:15:21 -0800 | [diff] [blame] | 291 | V4L2Reader::V4L2Reader(aos::EventLoop *event_loop, std::string_view device_name, |
| 292 | std::string_view image_channel) |
| 293 | : V4L2ReaderBase(event_loop, device_name, image_channel) { |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 294 | // Don't know why this magic call to SetExposure is required (before the |
| 295 | // camera settings are configured) to make things work on boot of the pi, but |
| 296 | // it seems to be-- without it, the image exposure is wrong (too dark). Note-- |
| 297 | // any valid value seems to work-- just choosing 1 for now |
| 298 | |
| 299 | SetExposure(1); |
| 300 | |
| 301 | struct v4l2_format format; |
| 302 | memset(&format, 0, sizeof(format)); |
| 303 | format.type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 304 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
milind-u | 6b09409 | 2023-01-09 19:26:12 -0800 | [diff] [blame] | 305 | |
| 306 | constexpr int kWidth = 640; |
| 307 | constexpr int kHeight = 480; |
| 308 | format.fmt.pix.width = kWidth; |
| 309 | format.fmt.pix.height = kHeight; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 310 | format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
| 311 | // This means we want to capture from a progressive (non-interlaced) |
| 312 | // source. |
| 313 | format.fmt.pix.field = V4L2_FIELD_NONE; |
| 314 | PCHECK(Ioctl(VIDIOC_S_FMT, &format) == 0); |
milind-u | 6b09409 | 2023-01-09 19:26:12 -0800 | [diff] [blame] | 315 | CHECK_EQ(static_cast<int>(format.fmt.pix.width), kWidth); |
| 316 | CHECK_EQ(static_cast<int>(format.fmt.pix.height), kHeight); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 317 | CHECK_EQ(static_cast<int>(format.fmt.pix.bytesperline), |
milind-u | 6b09409 | 2023-01-09 19:26:12 -0800 | [diff] [blame] | 318 | kWidth * 2 /* bytes per pixel */); |
| 319 | CHECK_EQ(format.fmt.pix.sizeimage, ImageSize(kHeight, kWidth)); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 320 | |
| 321 | StreamOn(); |
| 322 | } |
| 323 | |
| 324 | RockchipV4L2Reader::RockchipV4L2Reader(aos::EventLoop *event_loop, |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame] | 325 | aos::internal::EPoll *epoll, |
milind-u | fd08c43 | 2023-02-05 15:15:21 -0800 | [diff] [blame] | 326 | std::string_view device_name, |
| 327 | std::string_view image_sensor_subdev, |
| 328 | std::string_view image_channel) |
| 329 | : V4L2ReaderBase(event_loop, device_name, image_channel), |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 330 | epoll_(epoll), |
milind-u | fd08c43 | 2023-02-05 15:15:21 -0800 | [diff] [blame] | 331 | image_sensor_fd_(open(image_sensor_subdev.data(), O_RDWR | O_NONBLOCK)), |
Austin Schuh | 2cd5fe8 | 2023-02-04 16:21:19 -0800 | [diff] [blame] | 332 | buffer_requeuer_([this](int buffer) { EnqueueBuffer(buffer); }, |
| 333 | kEnqueueFifoPriority) { |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 334 | PCHECK(image_sensor_fd_.get() != -1) |
| 335 | << " Failed to open device " << device_name; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 336 | StreamOn(); |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame] | 337 | epoll_->OnReadable(fd().get(), [this]() { OnImageReady(); }); |
| 338 | } |
| 339 | |
Ravago Jones | fd8aa20 | 2023-01-16 14:21:45 -0800 | [diff] [blame] | 340 | RockchipV4L2Reader::~RockchipV4L2Reader() { epoll_->DeleteFd(fd().get()); } |
| 341 | |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 342 | void RockchipV4L2Reader::MarkBufferToBeEnqueued(int buffer) { |
| 343 | ReinitializeBuffer(buffer); |
| 344 | buffer_requeuer_.Push(buffer); |
| 345 | } |
| 346 | |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame] | 347 | void RockchipV4L2Reader::OnImageReady() { |
| 348 | if (!ReadLatestImage()) { |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | SendLatestImage(); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 355 | int RockchipV4L2Reader::ImageSensorIoctl(unsigned long number, void *arg) { |
| 356 | return ioctl(image_sensor_fd_.get(), number, arg); |
| 357 | } |
| 358 | |
| 359 | void RockchipV4L2Reader::SetExposure(size_t duration) { |
| 360 | v4l2_control exposure_control; |
| 361 | exposure_control.id = V4L2_CID_EXPOSURE; |
| 362 | exposure_control.value = static_cast<int>(duration); |
| 363 | PCHECK(ImageSensorIoctl(VIDIOC_S_CTRL, &exposure_control) == 0); |
| 364 | } |
| 365 | |
| 366 | void RockchipV4L2Reader::SetGain(size_t gain) { |
Ravago Jones | a0a2e06 | 2023-01-03 21:45:18 -0800 | [diff] [blame] | 367 | v4l2_control gain_control; |
| 368 | gain_control.id = V4L2_CID_GAIN; |
| 369 | gain_control.value = static_cast<int>(gain); |
| 370 | PCHECK(ImageSensorIoctl(VIDIOC_S_CTRL, &gain_control) == 0); |
| 371 | } |
| 372 | |
| 373 | void RockchipV4L2Reader::SetGainExt(size_t gain) { |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 374 | struct v4l2_ext_controls controls; |
| 375 | memset(&controls, 0, sizeof(controls)); |
| 376 | struct v4l2_ext_control control[1]; |
| 377 | memset(&control, 0, sizeof(control)); |
| 378 | |
| 379 | controls.ctrl_class = V4L2_CTRL_CLASS_IMAGE_SOURCE; |
| 380 | controls.count = 1; |
| 381 | controls.controls = control; |
| 382 | control[0].id = V4L2_CID_ANALOGUE_GAIN; |
| 383 | control[0].value = gain; |
| 384 | |
| 385 | PCHECK(ImageSensorIoctl(VIDIOC_S_EXT_CTRLS, &controls) == 0); |
| 386 | } |
| 387 | |
Ravago Jones | da1b008 | 2023-01-21 15:33:19 -0800 | [diff] [blame] | 388 | void RockchipV4L2Reader::SetVerticalBlanking(size_t vblank) { |
| 389 | struct v4l2_ext_controls controls; |
| 390 | memset(&controls, 0, sizeof(controls)); |
| 391 | struct v4l2_ext_control control[1]; |
| 392 | memset(&control, 0, sizeof(control)); |
Ravago Jones | a0a2e06 | 2023-01-03 21:45:18 -0800 | [diff] [blame] | 393 | |
Ravago Jones | da1b008 | 2023-01-21 15:33:19 -0800 | [diff] [blame] | 394 | controls.ctrl_class = V4L2_CTRL_CLASS_IMAGE_SOURCE; |
| 395 | controls.count = 1; |
| 396 | controls.controls = control; |
| 397 | control[0].id = V4L2_CID_VBLANK; |
| 398 | control[0].value = vblank; |
| 399 | |
| 400 | PCHECK(ImageSensorIoctl(VIDIOC_S_EXT_CTRLS, &controls) == 0); |
Ravago Jones | a0a2e06 | 2023-01-03 21:45:18 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 403 | } // namespace frc971::vision |