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