Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 1 | #ifndef FRC971_VISION_V4L2_READER_H_ |
| 2 | #define FRC971_VISION_V4L2_READER_H_ |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 3 | |
| 4 | #include <array> |
| 5 | #include <string> |
| 6 | |
| 7 | #include "absl/types/span.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 8 | #include "glog/logging.h" |
| 9 | |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 10 | #include "aos/containers/ring_buffer.h" |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame] | 11 | #include "aos/events/epoll.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 12 | #include "aos/events/event_loop.h" |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 13 | #include "aos/ftrace.h" |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 14 | #include "aos/realtime.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 15 | #include "aos/scoped/scoped_fd.h" |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 16 | #include "aos/util/threaded_consumer.h" |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 17 | #include "frc971/vision/vision_generated.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 18 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 19 | namespace frc971::vision { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 20 | |
| 21 | // Reads images from a V4L2 capture device (aka camera). |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 22 | class V4L2ReaderBase { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 23 | public: |
| 24 | // device_name is the name of the device file (like "/dev/video0"). |
milind-u | fd08c43 | 2023-02-05 15:15:21 -0800 | [diff] [blame] | 25 | // image_channel is the channel to send images on |
| 26 | V4L2ReaderBase(aos::EventLoop *event_loop, std::string_view device_name, |
| 27 | std::string_view image_channel); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 28 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 29 | V4L2ReaderBase(const V4L2ReaderBase &) = delete; |
| 30 | V4L2ReaderBase &operator=(const V4L2ReaderBase &) = delete; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 31 | |
| 32 | // Reads the latest image. |
| 33 | // |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 34 | // Returns false if no image was available since the last image was read. |
| 35 | // Call LatestImage() to get a reference to the data, which will be valid |
| 36 | // until this method is called again. |
| 37 | bool ReadLatestImage(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 38 | |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 39 | void MaybeEnqueue(); |
| 40 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 41 | // Sends the latest image. |
| 42 | // |
| 43 | // ReadLatestImage() must have returned a non-empty span the last time it was |
| 44 | // called. After calling this, the data which was returned from |
| 45 | // ReadLatestImage() will no longer be valid. |
| 46 | void SendLatestImage(); |
| 47 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 48 | const CameraImage &LatestImage() { |
| 49 | Buffer *const buffer = &buffers_[saved_buffer_.index]; |
| 50 | return *flatbuffers::GetTemporaryPointer(*buffer->builder.fbb(), |
| 51 | buffer->message_offset); |
| 52 | } |
| 53 | |
milind-u | daebe9b | 2022-01-09 18:25:24 -0800 | [diff] [blame] | 54 | // Sets the exposure duration of the camera. duration is the number of 100 |
| 55 | // microsecond units. |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 56 | virtual void SetExposure(size_t duration); |
milind-u | daebe9b | 2022-01-09 18:25:24 -0800 | [diff] [blame] | 57 | |
| 58 | // Switches from manual to auto exposure. |
| 59 | void UseAutoExposure(); |
| 60 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 61 | protected: |
| 62 | void StreamOff(); |
| 63 | void StreamOn(); |
| 64 | |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 65 | // Enqueues a buffer for v4l2 to stream into (expensive). |
| 66 | void EnqueueBuffer(int buffer_index); |
| 67 | |
| 68 | // Initializations that need to happen in the main thread. |
| 69 | // |
| 70 | // Implementations of MarkBufferToBeEnqueued should call this before calling |
| 71 | // EnqueueBuffer. |
| 72 | void ReinitializeBuffer(int buffer_index) { |
| 73 | CHECK_GE(buffer_index, 0); |
| 74 | CHECK_LT(buffer_index, static_cast<int>(buffers_.size())); |
| 75 | buffers_[buffer_index].InitializeMessage(ImageSize()); |
| 76 | } |
| 77 | |
| 78 | // Submits a buffer to be enqueued later in a lower priority thread. |
| 79 | // Legacy V4L2Reader still does this in the main thread. |
| 80 | virtual void MarkBufferToBeEnqueued(int buffer_index); |
| 81 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 82 | int Ioctl(unsigned long number, void *arg); |
| 83 | |
| 84 | bool multiplanar() const { return multiplanar_; } |
| 85 | |
| 86 | // TODO(Brian): This concept won't exist once we start using variable-size |
| 87 | // H.264 frames. |
milind-u | 6b09409 | 2023-01-09 19:26:12 -0800 | [diff] [blame] | 88 | size_t ImageSize() const { return ImageSize(rows_, cols_); } |
| 89 | static size_t ImageSize(int rows, int cols) { |
| 90 | return rows * cols * 2 /* bytes per pixel */; |
| 91 | } |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 92 | |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame] | 93 | const aos::ScopedFD &fd() { return fd_; }; |
| 94 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 95 | static constexpr int kNumberBuffers = 4; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 96 | |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 97 | private: |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 98 | struct Buffer { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 99 | void InitializeMessage(size_t max_image_size); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 100 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 101 | void PrepareMessage(int rows, int cols, size_t image_size, |
| 102 | aos::monotonic_clock::time_point monotonic_eof); |
| 103 | |
| 104 | void Send() { |
Ravago Jones | da1b008 | 2023-01-21 15:33:19 -0800 | [diff] [blame] | 105 | builder.CheckOk(builder.Send(message_offset)); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 106 | message_offset = flatbuffers::Offset<CameraImage>(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | absl::Span<const char> DataSpan(size_t image_size) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 110 | return absl::Span<const char>( |
| 111 | reinterpret_cast<char *>(CHECK_NOTNULL(data_pointer)), image_size); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | aos::Sender<CameraImage> sender; |
| 115 | aos::Sender<CameraImage>::Builder builder; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 116 | flatbuffers::Offset<CameraImage> message_offset; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 117 | |
| 118 | uint8_t *data_pointer = nullptr; |
milind-u | fd08c43 | 2023-02-05 15:15:21 -0800 | [diff] [blame] | 119 | |
| 120 | std::string_view image_channel_; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 123 | struct BufferInfo { |
| 124 | int index = -1; |
| 125 | aos::monotonic_clock::time_point monotonic_eof = |
| 126 | aos::monotonic_clock::min_time; |
| 127 | |
| 128 | explicit operator bool() const { return index != -1; } |
| 129 | |
| 130 | void Clear() { |
| 131 | index = -1; |
| 132 | monotonic_eof = aos::monotonic_clock::min_time; |
| 133 | } |
| 134 | }; |
| 135 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 136 | // Attempts to dequeue a buffer (nonblocking). Returns the index of the new |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 137 | // buffer, or BufferInfo() if there wasn't a frame to dequeue. |
| 138 | BufferInfo DequeueBuffer(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 139 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 140 | // The mmaped V4L2 buffers. |
| 141 | std::array<Buffer, kNumberBuffers> buffers_; |
| 142 | |
| 143 | // If this is non-negative, it's the buffer number we're currently holding |
| 144 | // onto. |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 145 | BufferInfo saved_buffer_; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 146 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 147 | bool multiplanar_ = false; |
| 148 | |
| 149 | int rows_ = 0; |
| 150 | int cols_ = 0; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 151 | |
| 152 | aos::ScopedFD fd_; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 153 | |
| 154 | aos::EventLoop *event_loop_; |
| 155 | aos::Ftrace ftrace_; |
milind-u | fd08c43 | 2023-02-05 15:15:21 -0800 | [diff] [blame] | 156 | |
| 157 | std::string_view image_channel_; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | // Generic V4L2 reader for pi's and older. |
| 161 | class V4L2Reader : public V4L2ReaderBase { |
| 162 | public: |
milind-u | fd08c43 | 2023-02-05 15:15:21 -0800 | [diff] [blame] | 163 | V4L2Reader(aos::EventLoop *event_loop, std::string_view device_name, |
| 164 | std::string_view image_channel = "/camera"); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | // Rockpi specific v4l2 reader. This assumes that the media device has been |
| 168 | // properly configured before this class is constructed. |
| 169 | class RockchipV4L2Reader : public V4L2ReaderBase { |
| 170 | public: |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame] | 171 | RockchipV4L2Reader(aos::EventLoop *event_loop, aos::internal::EPoll *epoll, |
milind-u | fd08c43 | 2023-02-05 15:15:21 -0800 | [diff] [blame] | 172 | std::string_view device_name, |
| 173 | std::string_view image_sensor_subdev, |
| 174 | std::string_view image_channel = "/camera"); |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 175 | |
milind-u | fd08c43 | 2023-02-05 15:15:21 -0800 | [diff] [blame] | 176 | virtual ~RockchipV4L2Reader(); |
Ravago Jones | fd8aa20 | 2023-01-16 14:21:45 -0800 | [diff] [blame] | 177 | |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 178 | void SetExposure(size_t duration) override; |
| 179 | |
| 180 | void SetGain(size_t gain); |
Ravago Jones | a0a2e06 | 2023-01-03 21:45:18 -0800 | [diff] [blame] | 181 | void SetGainExt(size_t gain); |
| 182 | |
Ravago Jones | da1b008 | 2023-01-21 15:33:19 -0800 | [diff] [blame] | 183 | void SetVerticalBlanking(size_t vblank); |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame] | 184 | |
| 185 | private: |
| 186 | void OnImageReady(); |
| 187 | |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 188 | void MarkBufferToBeEnqueued(int buffer) override; |
| 189 | |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 190 | int ImageSensorIoctl(unsigned long number, void *arg); |
| 191 | |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame] | 192 | aos::internal::EPoll *epoll_; |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 193 | |
| 194 | aos::ScopedFD image_sensor_fd_; |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 195 | |
Austin Schuh | 2cd5fe8 | 2023-02-04 16:21:19 -0800 | [diff] [blame] | 196 | static constexpr int kEnqueueFifoPriority = 1; |
| 197 | |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame] | 198 | aos::util::ThreadedConsumer<int, kNumberBuffers> buffer_requeuer_; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 199 | }; |
| 200 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 201 | } // namespace frc971::vision |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 202 | |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 203 | #endif // FRC971_VISION_V4L2_READER_H_ |