blob: 2013113770d351b5fd7634d1c5767b2d38e9c1f9 [file] [log] [blame]
Jim Ostrowski977850f2022-01-22 21:04:22 -08001#ifndef FRC971_VISION_V4L2_READER_H_
2#define FRC971_VISION_V4L2_READER_H_
Brian Silverman9dd793b2020-01-31 23:52:21 -08003
4#include <array>
5#include <string>
6
7#include "absl/types/span.h"
Ravago Jonesdc524752022-12-27 01:15:13 -08008#include "aos/events/epoll.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -08009#include "aos/events/event_loop.h"
Austin Schuhe4acc1e2022-12-26 14:01:34 -080010#include "aos/ftrace.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -080011#include "aos/scoped/scoped_fd.h"
Jim Ostrowski977850f2022-01-22 21:04:22 -080012#include "frc971/vision/vision_generated.h"
Austin Schuh77d0bbd2022-12-26 14:00:51 -080013#include "glog/logging.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -080014
15namespace frc971 {
16namespace vision {
17
18// Reads images from a V4L2 capture device (aka camera).
Austin Schuh77d0bbd2022-12-26 14:00:51 -080019class V4L2ReaderBase {
Brian Silverman9dd793b2020-01-31 23:52:21 -080020 public:
21 // device_name is the name of the device file (like "/dev/video0").
Austin Schuh77d0bbd2022-12-26 14:00:51 -080022 V4L2ReaderBase(aos::EventLoop *event_loop, const std::string &device_name);
Brian Silverman9dd793b2020-01-31 23:52:21 -080023
Austin Schuh77d0bbd2022-12-26 14:00:51 -080024 V4L2ReaderBase(const V4L2ReaderBase &) = delete;
25 V4L2ReaderBase &operator=(const V4L2ReaderBase &) = delete;
Brian Silverman9dd793b2020-01-31 23:52:21 -080026
27 // Reads the latest image.
28 //
Brian Silverman967e5df2020-02-09 16:43:34 -080029 // 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 Silverman9dd793b2020-01-31 23:52:21 -080033
Ravago Jones65469be2023-01-13 21:28:23 -080034 void MaybeEnqueue();
35
Brian Silverman9dd793b2020-01-31 23:52:21 -080036 // 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 Silverman967e5df2020-02-09 16:43:34 -080043 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-udaebe9b2022-01-09 18:25:24 -080049 // Sets the exposure duration of the camera. duration is the number of 100
50 // microsecond units.
Ravago Jones65469be2023-01-13 21:28:23 -080051 virtual void SetExposure(size_t duration);
milind-udaebe9b2022-01-09 18:25:24 -080052
53 // Switches from manual to auto exposure.
54 void UseAutoExposure();
55
Austin Schuh77d0bbd2022-12-26 14:00:51 -080056 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.
66 size_t ImageSize() const { return rows_ * cols_ * 2 /* bytes per pixel */; }
67
Ravago Jonesdc524752022-12-27 01:15:13 -080068 const aos::ScopedFD &fd() { return fd_; };
69
Brian Silverman9dd793b2020-01-31 23:52:21 -080070 private:
Austin Schuh77d0bbd2022-12-26 14:00:51 -080071 static constexpr int kNumberBuffers = 4;
Brian Silverman9dd793b2020-01-31 23:52:21 -080072
73 struct Buffer {
Brian Silverman967e5df2020-02-09 16:43:34 -080074 void InitializeMessage(size_t max_image_size);
Brian Silverman9dd793b2020-01-31 23:52:21 -080075
Brian Silverman967e5df2020-02-09 16:43:34 -080076 void PrepareMessage(int rows, int cols, size_t image_size,
77 aos::monotonic_clock::time_point monotonic_eof);
78
79 void Send() {
milind1f1dca32021-07-03 13:50:07 -070080 (void)builder.Send(message_offset);
Brian Silverman967e5df2020-02-09 16:43:34 -080081 message_offset = flatbuffers::Offset<CameraImage>();
Brian Silverman9dd793b2020-01-31 23:52:21 -080082 }
83
84 absl::Span<const char> DataSpan(size_t image_size) {
Brian Silverman967e5df2020-02-09 16:43:34 -080085 return absl::Span<const char>(
86 reinterpret_cast<char *>(CHECK_NOTNULL(data_pointer)), image_size);
Brian Silverman9dd793b2020-01-31 23:52:21 -080087 }
88
89 aos::Sender<CameraImage> sender;
90 aos::Sender<CameraImage>::Builder builder;
Brian Silverman967e5df2020-02-09 16:43:34 -080091 flatbuffers::Offset<CameraImage> message_offset;
Brian Silverman9dd793b2020-01-31 23:52:21 -080092
93 uint8_t *data_pointer = nullptr;
94 };
95
Brian Silverman967e5df2020-02-09 16:43:34 -080096 struct BufferInfo {
97 int index = -1;
98 aos::monotonic_clock::time_point monotonic_eof =
99 aos::monotonic_clock::min_time;
100
101 explicit operator bool() const { return index != -1; }
102
103 void Clear() {
104 index = -1;
105 monotonic_eof = aos::monotonic_clock::min_time;
106 }
107 };
108
Brian Silverman9dd793b2020-01-31 23:52:21 -0800109 // Attempts to dequeue a buffer (nonblocking). Returns the index of the new
Brian Silverman967e5df2020-02-09 16:43:34 -0800110 // buffer, or BufferInfo() if there wasn't a frame to dequeue.
111 BufferInfo DequeueBuffer();
Brian Silverman9dd793b2020-01-31 23:52:21 -0800112
113 void EnqueueBuffer(int buffer);
114
Brian Silverman9dd793b2020-01-31 23:52:21 -0800115 // The mmaped V4L2 buffers.
116 std::array<Buffer, kNumberBuffers> buffers_;
117
118 // If this is non-negative, it's the buffer number we're currently holding
119 // onto.
Brian Silverman967e5df2020-02-09 16:43:34 -0800120 BufferInfo saved_buffer_;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800121
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800122 bool multiplanar_ = false;
123
124 int rows_ = 0;
125 int cols_ = 0;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800126
127 aos::ScopedFD fd_;
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800128
129 aos::EventLoop *event_loop_;
130 aos::Ftrace ftrace_;
131};
132
133// Generic V4L2 reader for pi's and older.
134class V4L2Reader : public V4L2ReaderBase {
135 public:
136 V4L2Reader(aos::EventLoop *event_loop, const std::string &device_name);
137
138 private:
139 const int rows_ = 480;
140 const int cols_ = 640;
141};
142
143// Rockpi specific v4l2 reader. This assumes that the media device has been
144// properly configured before this class is constructed.
145class RockchipV4L2Reader : public V4L2ReaderBase {
146 public:
Ravago Jonesdc524752022-12-27 01:15:13 -0800147 RockchipV4L2Reader(aos::EventLoop *event_loop, aos::internal::EPoll *epoll,
Ravago Jones65469be2023-01-13 21:28:23 -0800148 const std::string &device_name,
149 const std::string &image_sensor_subdev);
150
151 void SetExposure(size_t duration) override;
152
153 void SetGain(size_t gain);
Ravago Jonesdc524752022-12-27 01:15:13 -0800154
155 private:
156 void OnImageReady();
157
Ravago Jones65469be2023-01-13 21:28:23 -0800158 int ImageSensorIoctl(unsigned long number, void *arg);
159
Ravago Jonesdc524752022-12-27 01:15:13 -0800160 aos::internal::EPoll *epoll_;
Ravago Jones65469be2023-01-13 21:28:23 -0800161
162 aos::ScopedFD image_sensor_fd_;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800163};
164
165} // namespace vision
166} // namespace frc971
167
Jim Ostrowski977850f2022-01-22 21:04:22 -0800168#endif // FRC971_VISION_V4L2_READER_H_