blob: 22e436709a8bdea199318178169e00b88d20dcdc [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 Jonesc6b919f2023-01-01 21:34:12 -08008#include "aos/containers/ring_buffer.h"
Ravago Jonesdc524752022-12-27 01:15:13 -08009#include "aos/events/epoll.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -080010#include "aos/events/event_loop.h"
Austin Schuhe4acc1e2022-12-26 14:01:34 -080011#include "aos/ftrace.h"
Ravago Jonesc6b919f2023-01-01 21:34:12 -080012#include "aos/realtime.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -080013#include "aos/scoped/scoped_fd.h"
Ravago Jonesc6b919f2023-01-01 21:34:12 -080014#include "aos/util/threaded_consumer.h"
Jim Ostrowski977850f2022-01-22 21:04:22 -080015#include "frc971/vision/vision_generated.h"
Austin Schuh77d0bbd2022-12-26 14:00:51 -080016#include "glog/logging.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -080017
18namespace frc971 {
19namespace vision {
20
21// Reads images from a V4L2 capture device (aka camera).
Austin Schuh77d0bbd2022-12-26 14:00:51 -080022class V4L2ReaderBase {
Brian Silverman9dd793b2020-01-31 23:52:21 -080023 public:
24 // device_name is the name of the device file (like "/dev/video0").
Austin Schuh77d0bbd2022-12-26 14:00:51 -080025 V4L2ReaderBase(aos::EventLoop *event_loop, const std::string &device_name);
Brian Silverman9dd793b2020-01-31 23:52:21 -080026
Austin Schuh77d0bbd2022-12-26 14:00:51 -080027 V4L2ReaderBase(const V4L2ReaderBase &) = delete;
28 V4L2ReaderBase &operator=(const V4L2ReaderBase &) = delete;
Brian Silverman9dd793b2020-01-31 23:52:21 -080029
30 // Reads the latest image.
31 //
Brian Silverman967e5df2020-02-09 16:43:34 -080032 // Returns false if no image was available since the last image was read.
33 // Call LatestImage() to get a reference to the data, which will be valid
34 // until this method is called again.
35 bool ReadLatestImage();
Brian Silverman9dd793b2020-01-31 23:52:21 -080036
Ravago Jones65469be2023-01-13 21:28:23 -080037 void MaybeEnqueue();
38
Brian Silverman9dd793b2020-01-31 23:52:21 -080039 // Sends the latest image.
40 //
41 // ReadLatestImage() must have returned a non-empty span the last time it was
42 // called. After calling this, the data which was returned from
43 // ReadLatestImage() will no longer be valid.
44 void SendLatestImage();
45
Brian Silverman967e5df2020-02-09 16:43:34 -080046 const CameraImage &LatestImage() {
47 Buffer *const buffer = &buffers_[saved_buffer_.index];
48 return *flatbuffers::GetTemporaryPointer(*buffer->builder.fbb(),
49 buffer->message_offset);
50 }
51
milind-udaebe9b2022-01-09 18:25:24 -080052 // Sets the exposure duration of the camera. duration is the number of 100
53 // microsecond units.
Ravago Jones65469be2023-01-13 21:28:23 -080054 virtual void SetExposure(size_t duration);
milind-udaebe9b2022-01-09 18:25:24 -080055
56 // Switches from manual to auto exposure.
57 void UseAutoExposure();
58
Austin Schuh77d0bbd2022-12-26 14:00:51 -080059 protected:
60 void StreamOff();
61 void StreamOn();
62
Ravago Jonesc6b919f2023-01-01 21:34:12 -080063 // Enqueues a buffer for v4l2 to stream into (expensive).
64 void EnqueueBuffer(int buffer_index);
65
66 // Initializations that need to happen in the main thread.
67 //
68 // Implementations of MarkBufferToBeEnqueued should call this before calling
69 // EnqueueBuffer.
70 void ReinitializeBuffer(int buffer_index) {
71 CHECK_GE(buffer_index, 0);
72 CHECK_LT(buffer_index, static_cast<int>(buffers_.size()));
73 buffers_[buffer_index].InitializeMessage(ImageSize());
74 }
75
76 // Submits a buffer to be enqueued later in a lower priority thread.
77 // Legacy V4L2Reader still does this in the main thread.
78 virtual void MarkBufferToBeEnqueued(int buffer_index);
79
Austin Schuh77d0bbd2022-12-26 14:00:51 -080080 int Ioctl(unsigned long number, void *arg);
81
82 bool multiplanar() const { return multiplanar_; }
83
84 // TODO(Brian): This concept won't exist once we start using variable-size
85 // H.264 frames.
milind-u6b094092023-01-09 19:26:12 -080086 size_t ImageSize() const { return ImageSize(rows_, cols_); }
87 static size_t ImageSize(int rows, int cols) {
88 return rows * cols * 2 /* bytes per pixel */;
89 }
Austin Schuh77d0bbd2022-12-26 14:00:51 -080090
Ravago Jonesdc524752022-12-27 01:15:13 -080091 const aos::ScopedFD &fd() { return fd_; };
92
Austin Schuh77d0bbd2022-12-26 14:00:51 -080093 static constexpr int kNumberBuffers = 4;
Brian Silverman9dd793b2020-01-31 23:52:21 -080094
Ravago Jonesc6b919f2023-01-01 21:34:12 -080095 private:
Brian Silverman9dd793b2020-01-31 23:52:21 -080096 struct Buffer {
Brian Silverman967e5df2020-02-09 16:43:34 -080097 void InitializeMessage(size_t max_image_size);
Brian Silverman9dd793b2020-01-31 23:52:21 -080098
Brian Silverman967e5df2020-02-09 16:43:34 -080099 void PrepareMessage(int rows, int cols, size_t image_size,
100 aos::monotonic_clock::time_point monotonic_eof);
101
102 void Send() {
Ravago Jonesda1b0082023-01-21 15:33:19 -0800103 builder.CheckOk(builder.Send(message_offset));
Brian Silverman967e5df2020-02-09 16:43:34 -0800104 message_offset = flatbuffers::Offset<CameraImage>();
Brian Silverman9dd793b2020-01-31 23:52:21 -0800105 }
106
107 absl::Span<const char> DataSpan(size_t image_size) {
Brian Silverman967e5df2020-02-09 16:43:34 -0800108 return absl::Span<const char>(
109 reinterpret_cast<char *>(CHECK_NOTNULL(data_pointer)), image_size);
Brian Silverman9dd793b2020-01-31 23:52:21 -0800110 }
111
112 aos::Sender<CameraImage> sender;
113 aos::Sender<CameraImage>::Builder builder;
Brian Silverman967e5df2020-02-09 16:43:34 -0800114 flatbuffers::Offset<CameraImage> message_offset;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800115
116 uint8_t *data_pointer = nullptr;
117 };
118
Brian Silverman967e5df2020-02-09 16:43:34 -0800119 struct BufferInfo {
120 int index = -1;
121 aos::monotonic_clock::time_point monotonic_eof =
122 aos::monotonic_clock::min_time;
123
124 explicit operator bool() const { return index != -1; }
125
126 void Clear() {
127 index = -1;
128 monotonic_eof = aos::monotonic_clock::min_time;
129 }
130 };
131
Brian Silverman9dd793b2020-01-31 23:52:21 -0800132 // Attempts to dequeue a buffer (nonblocking). Returns the index of the new
Brian Silverman967e5df2020-02-09 16:43:34 -0800133 // buffer, or BufferInfo() if there wasn't a frame to dequeue.
134 BufferInfo DequeueBuffer();
Brian Silverman9dd793b2020-01-31 23:52:21 -0800135
Brian Silverman9dd793b2020-01-31 23:52:21 -0800136 // The mmaped V4L2 buffers.
137 std::array<Buffer, kNumberBuffers> buffers_;
138
139 // If this is non-negative, it's the buffer number we're currently holding
140 // onto.
Brian Silverman967e5df2020-02-09 16:43:34 -0800141 BufferInfo saved_buffer_;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800142
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800143 bool multiplanar_ = false;
144
145 int rows_ = 0;
146 int cols_ = 0;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800147
148 aos::ScopedFD fd_;
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800149
150 aos::EventLoop *event_loop_;
151 aos::Ftrace ftrace_;
152};
153
154// Generic V4L2 reader for pi's and older.
155class V4L2Reader : public V4L2ReaderBase {
156 public:
157 V4L2Reader(aos::EventLoop *event_loop, const std::string &device_name);
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800158};
159
160// Rockpi specific v4l2 reader. This assumes that the media device has been
161// properly configured before this class is constructed.
162class RockchipV4L2Reader : public V4L2ReaderBase {
163 public:
Ravago Jonesdc524752022-12-27 01:15:13 -0800164 RockchipV4L2Reader(aos::EventLoop *event_loop, aos::internal::EPoll *epoll,
Ravago Jones65469be2023-01-13 21:28:23 -0800165 const std::string &device_name,
166 const std::string &image_sensor_subdev);
167
Ravago Jonesfd8aa202023-01-16 14:21:45 -0800168 ~RockchipV4L2Reader();
169
Ravago Jones65469be2023-01-13 21:28:23 -0800170 void SetExposure(size_t duration) override;
171
172 void SetGain(size_t gain);
Ravago Jonesa0a2e062023-01-03 21:45:18 -0800173 void SetGainExt(size_t gain);
174
Ravago Jonesda1b0082023-01-21 15:33:19 -0800175 void SetVerticalBlanking(size_t vblank);
Ravago Jonesdc524752022-12-27 01:15:13 -0800176
177 private:
178 void OnImageReady();
179
Ravago Jonesc6b919f2023-01-01 21:34:12 -0800180 void MarkBufferToBeEnqueued(int buffer) override;
181
Ravago Jones65469be2023-01-13 21:28:23 -0800182 int ImageSensorIoctl(unsigned long number, void *arg);
183
Ravago Jonesdc524752022-12-27 01:15:13 -0800184 aos::internal::EPoll *epoll_;
Ravago Jones65469be2023-01-13 21:28:23 -0800185
186 aos::ScopedFD image_sensor_fd_;
Ravago Jonesc6b919f2023-01-01 21:34:12 -0800187
Austin Schuh2cd5fe82023-02-04 16:21:19 -0800188 static constexpr int kEnqueueFifoPriority = 1;
189
Ravago Jonesc6b919f2023-01-01 21:34:12 -0800190 aos::util::ThreadedConsumer<int, kNumberBuffers> buffer_requeuer_;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800191};
192
193} // namespace vision
194} // namespace frc971
195
Jim Ostrowski977850f2022-01-22 21:04:22 -0800196#endif // FRC971_VISION_V4L2_READER_H_