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