Debian Codegraph @

cvd/Linux/v4lbuffer.h

raw symbols SCIP
1 //-*- c++ -*-
2 #ifndef CVD_V4LBUFFER_H
3 #define CVD_V4LBUFFER_H
4
5 #include <vector>
6
7 #include <linux/videodev2.h>
8
9 #include <cvd/byte.h>
10 #include <cvd/colourspaces.h>
11 #include <cvd/rgb.h>
12 #include <cvd/rgb8.h>
13 #include <cvd/timer.h>
14 #include <cvd/videobuffer.h>
15 #include <fcntl.h>
16
17 namespace CVD
18 {
19
20 namespace Exceptions
21 {
22 /// @ingroup gException
23 namespace V4LBuffer
24 {
25 /// @ingroup gException
26 struct All : public CVD::Exceptions::VideoBuffer::All
27 {
28 using Exceptions::VideoBuffer::All::All;
29 };
30 /// Error opening the device
31 /// @ingroup gException
32 struct DeviceOpen : public All
33 {
34 DeviceOpen(std::string dev); ///< Construct from the device name
35 };
36
37 /// Device was OK, but could not provide the requested colourspace
38 /// @ingroup gException
39 struct NoColourspace : public All
40 {
41 NoColourspace(std::string dev, std::string space); ///< Construct from the device name and requested colourspace
42 };
43
44 /// Error setting up the device
45 /// @ingroup gException
46 struct DeviceSetup : public All
47 {
48 DeviceSetup(std::string dev, std::string action); ///< Construct from the device string and an error string
49 };
50 /// Error in a put_frame() call
51 /// @ingroup gException
52 struct PutFrame : public All
53 {
54 PutFrame(std::string dev, std::string msg); ///< Construct from the device name
55 };
56 /// Error in a get_frame() call
57 /// @ingroup gException
58 struct GetFrame : public All
59 {
60 GetFrame(std::string dev, std::string msg); ///< Construct from the device name
61 };
62 }
63 }
64
65 namespace V4L
66 {
67 #ifndef DOXYGEN_IGNORE_INTERNAL
68 template <class C>
69 struct format;
70
71 template <>
72 struct format<byte>
73 {
74 static const unsigned int v4l2_fmt = V4L2_PIX_FMT_GREY;
75 };
76
77 template <>
78 struct format<bayer_grbg>
79 {
80 static const unsigned int v4l2_fmt = V4L2_PIX_FMT_SBGGR8;
81 };
82 template <>
83 struct format<bayer_bggr>
84 {
85 static const unsigned int v4l2_fmt = V4L2_PIX_FMT_SBGGR8;
86 };
87
88 template <>
89 struct format<yuv422>
90 {
91 static const unsigned int v4l2_fmt = V4L2_PIX_FMT_YUYV;
92 };
93
94 template <>
95 struct format<vuy422>
96 {
97 static const unsigned int v4l2_fmt = V4L2_PIX_FMT_UYVY;
98 };
99
100 template <>
101 struct format<yuv420p>
102 {
103 static const unsigned int v4l2_fmt = V4L2_PIX_FMT_YUV420;
104 };
105
106 template <>
107 struct format<Rgb<byte>>
108 {
109 static const unsigned int v4l2_fmt = V4L2_PIX_FMT_RGB24;
110 };
111
112 template <>
113 struct format<Rgb8>
114 {
115 static const unsigned int v4l2_fmt = V4L2_PIX_FMT_RGB32;
116 };
117 #endif
118
119 class RawV4LBuffer : public virtual RawVideoBuffer
120 {
121 public:
122 struct Buffer
123 {
124 int id;
125 unsigned char* data;
126 double when;
127 };
128
129 RawV4LBuffer(const std::string& dev, unsigned int fmt, ImageRef size, int input, bool fields, int frame_per_second, bool verbose);
130 ImageRef getSize();
131 Buffer getFrame();
132 void releaseFrame(int id);
133 double getRate();
134 bool pendingFrame();
135 virtual ~RawV4LBuffer();
136
137 int num_buffers()
138 {
139 return num_bufs;
140 }
141
142 const std::string& device_name() const
143 {
144 return dev;
145 }
146
147 private:
148 int num_bufs;
149 struct State;
150 State* state;
151 std::string dev;
152 };
153
154 class V4L1Client;
155
156 };
157
158 /// A live video buffer which uses the Video for Linux 2 (V4L2) API.
159 /// A replacement for the (deprecated?) V4L2Buffer
160 /// @ingroup gVideoBuffer
161 template <class T>
162 class V4LBuffer : public VideoBuffer<T>, public V4L::RawV4LBuffer
163 {
164 public:
165 V4LBuffer(const std::string& dev, ImageRef size, int input = -1, bool fields = false, int frames_per_second = 0, bool verbose = 0)
166 : VideoBuffer<T>(VideoBufferType::Flushable)
167 , RawV4LBuffer(dev, V4L::format<T>::v4l2_fmt, size, input, fields, frames_per_second, verbose)
168 {
169 }
170
171 virtual ImageRef size()
172 {
173 return getSize();
174 }
175
176 virtual VideoFrame<T>* get_frame()
177 {
178 V4L::RawV4LBuffer::Buffer buffer = getFrame();
179 return new V4LFrame(buffer.id, buffer.when, buffer.data, getSize());
180 }
181
182 virtual void put_frame(VideoFrame<T>* f)
183 {
184 V4LFrame* vf = dynamic_cast<V4LFrame*>(f);
185 if(vf == 0)
186 throw Exceptions::V4LBuffer::PutFrame(device_name(), "Invalid VideoFrame");
187 int id = vf->id;
188 delete vf;
189
190 releaseFrame(id);
191 }
192
193 virtual bool frame_pending()
194 {
195 return pendingFrame();
196 }
197
198 virtual double frame_rate()
199 {
200 return getRate();
201 }
202
203 int num_buffers()
204 {
205 return num_buffers();
206 }
207
208 private:
209 struct V4LFrame : public VideoFrame<T>
210 {
211 V4LFrame(int i, double t, void* data, ImageRef size)
212 : VideoFrame<T>(t, reinterpret_cast<T*>(data), size)
213 , id(i)
214 {
215 }
216
217 int id;
218 friend class V4LBuffer<T>;
219 };
220
221 V4LBuffer(V4LBuffer&);
222 void operator=(V4LBuffer&);
223 };
224
225 };
226 #endif