Debian Codegraph @

cvd/Linux/dvbuffer.h

raw symbols SCIP
1 // -*- c++ -*-
2 #ifndef __DVBUFFER_H
3 #define __DVBUFFER_H
4
5 #include <cvd/byte.h>
6 #include <cvd/colourspaces.h>
7 #include <cvd/exceptions.h>
8 #include <cvd/rgb.h>
9 #include <cvd/videobuffer.h>
10 #include <libdc1394/dc1394_control.h>
11 #include <libraw1394/raw1394.h>
12 #include <string>
13 #include <utility>
14 #include <vector>
15
16 namespace CVD
17 {
18
19 namespace Exceptions
20 {
21 /// %Exceptions specific to V4L2Buffer
22 /// @ingroup gException
23 namespace DVBuffer
24 {
25 /// Base class for all V4L2 exceptions
26 /// @ingroup gException
27 struct All : public CVD::Exceptions::VideoBuffer::All
28 {
29 };
30
31 /// Error with RAW1394 setup
32 /// @ingroup gException
33 struct Raw1394Setup : public All
34 {
35 Raw1394Setup(std::string action); ///< Construct from action which failed
36 };
37
38 /// Error with DC1394 setup
39 /// @ingroup gException
40 struct DC1394Setup : public All
41 {
42 DC1394Setup(std::string action); ///< Construct from action which failed
43 };
44
45 /// Bad camera selection
46 /// @ingroup gException
47 struct BadCameraSelection : public All
48 {
49 BadCameraSelection(int num_cameras, int camera_no); ///< Construct from the number of cameras found and the camera requested
50 };
51
52 /// Bus reset needed
53 /// @ingroup gException
54 struct BusReset : public All
55 {
56 BusReset(); ///< No arguments required
57 };
58
59 /// Error opening the device
60 /// @ingroup gException
61 struct DeviceOpen : public All
62 {
63 DeviceOpen(std::string dev); ///< Construct from the device name
64 };
65 /// Error doing some later setup action
66 /// @ingroup gException
67 struct DeviceSetup : public All
68 {
69 DeviceSetup(std::string action); ///< Construct from some action string
70 };
71 /// Error in a put_frame() call
72 /// @ingroup gException
73 struct PutFrame : public All
74 {
75 PutFrame(std::string dev); ///< Construct from the device name
76 };
77 /// Error in a get_frame() call
78 /// @ingroup gException
79 struct GetFrame : public All
80 {
81 GetFrame(std::string dev); ///< Construct from the device name
82 };
83 }
84 }
85
86 /// Internal DVBuffer2 helpers
87 namespace DC
88 {
89 #ifndef DOXYGEN_IGNORE_INTERNAL
90 template <class C>
91 struct cam_type
92 {
93 static const int mode = C::Error__type_not_valid_for_camera___Use_byte_or_yuv411_or_rgb_of_byte;
94 // We can't really set the frame rate, but the alternative is to give the above error twice
95 static const double fps;
96 };
97
98 template <>
99 struct cam_type<yuv411>
100 {
101 static const int mode = MODE_640x480_YUV411;
102 static const double fps; //30
103 };
104
105 template <>
106 struct cam_type<byte>
107 {
108 static const int mode = MODE_640x480_MONO;
109 static const double fps; //30
110 };
111
112 template <>
113 struct cam_type<Rgb<byte>>
114 {
115 static const int mode = MODE_640x480_RGB;
116 static const double fps; //15
117 };
118
119 struct raw_frame
120 {
121 unsigned char* data;
122 double timestamp;
123 int buffer;
124 };
125 #endif
126
127 /// Internal (non type-safe) class used by DVBuffer2 to do the actual interfacing with the
128 /// Firewire (IEE 1394) video hardware. A wrapper for the libdc1394 library, it assumes that
129 /// the firewire device is on <tt>/dev/video1394/0</tt>.
130 /// Use DVBuffer2 if you want 8-bit greyscale or 24-bit colour.
131 class RawDCVideo
132 {
133 public:
134 /// Construct a video buffer
135 /// @param camera_no The camera number (the first camera is 0)
136 /// @param num_dma_buffers The number of DMA buffers to use (at least 3 is recommended)
137 /// @param bright The brightness correction
138 /// @param exposure The exposure correction
139 /// @param mode The required mode
140 /// @param frame_rate The number of frames per second
141 RawDCVideo(int camera_no, int num_dma_buffers, int bright, int exposure, int mode, double frame_rate);
142 ~RawDCVideo();
143
144 /// The size of the VideoFrames returned by this buffer
145 ImageRef size();
146 /// Returns the next frame from the buffer. This function blocks until a frame is ready.
147 VideoFrame<byte>* get_frame();
148 /// Tell the buffer that you are finished with this frame
149 /// \param f The frame that you are finished with.
150 void put_frame(VideoFrame<byte>* f);
151 /// Is there a frame waiting in the buffer? This function does not block.
152 bool frame_pending();
153
154 /// Set the camera shutter speed
155 /// @param s The requested speed
156 void set_shutter(unsigned int s);
157 /// Get the camera shutter speed
158 unsigned int get_shutter();
159
160 /// Set the camera iris
161 /// @param i The requested iris
162 void set_iris(unsigned int i);
163 /// Get the camera iris
164 unsigned int get_iris();
165
166 /// Set the camera sharpness
167 /// @param s The requested sharpness
168 void set_sharpness(unsigned int s);
169 /// Get the camera sharpness
170 unsigned int get_sharpness();
171
172 /// Set the camera gain
173 /// @param g The requested gain
174 void set_gain(unsigned int g);
175 /// Get the camera gain
176 unsigned int get_gain();
177
178 /// Set the camera exposure
179 /// @param e The requested exposure
180 void set_exposure(unsigned int e);
181 /// Get the camera exposure
182 unsigned int get_exposure();
183
184 /// Set the camera brightness
185 /// @param b The requested brightness
186 void set_brightness(unsigned int b);
187 /// Get the camera brightness
188 unsigned int get_brightness();
189
190 /// Set any DC1394 camera feature value
191 /// @param feature The feature to be set - c.f. dc1394_control.h
192 /// @param value Requested feature value
193 void set_feature_value(unsigned int feature, unsigned int value);
194
195 /// Get any DC1394 camera feature value
196 /// @param feature The feature to be queried - c.f. dc1394_control.h
197 unsigned int get_feature_value(unsigned int feature);
198
199 /// Get the min and max value of any camera feature
200 /// @param feature The feature to be queried - c.f. dc1394_control.h
201 std::pair<unsigned int, unsigned int> get_feature_min_max(unsigned int feature);
202
203 /// Toggle auto on or off
204 /// @param feature The feature to be toggled
205 /// @param auto_value - 0 for auto off, nonzero for auto on
206 void auto_on_off(unsigned int feature, unsigned int auto_value);
207
208 /// Get the camera frame rate
209 double frame_rate();
210
211 /// What is the handle for this device?
212 raw1394handle_t& handle();
213 /// Which node is this device on?
214 nodeid_t& node();
215
216 private:
217 // components needed for the DMA based video capture
218 int my_channel;
219 unsigned char* my_ring_buffer;
220 int my_frame_size;
221 int my_num_buffers;
222 // int my_most_recent_frame; // the most recently filled buffer
223 int my_fd; // fd of the mmaped dma ring buffer
224 raw1394handle_t my_handle;
225 nodeid_t* my_camera_nodes; // member variable I guess unless we can make it be released
226 nodeid_t my_node;
227 ImageRef my_size;
228
229 std::vector<int> my_frame_sequence;
230 int my_next_frame;
231 int my_last_in_sequence;
232 double true_fps;
233 };
234
235 }
236
237 /// A video buffer from a Firewire (IEEE 1394) camera. The requested image format depends on the
238 /// templated pixel type. Frames of size 640 by 480 pixels are requested, at 30fps (except for
239 /// <code>CVD::Rgb<CVD::byte></code>, which is 15fps).
240 /// @param T The pixel type of the frames. Currently only <code><CVD::Rgb<CVD::byte> ></code>
241 /// <code>CVD::yuv411></code> and <code>CVD::byte></code> are supported.
242 /// @ingroup gVideoBuffer
243 template <class T>
244 class DVBuffer2 : public VideoBuffer<T>, public DC::RawDCVideo
245 {
246 public:
247 /// Construct a video buffer
248 /// @param cam_no The camera number (the first camera is 0)
249 /// @param num_dma_buffers The number of DMA buffers to use (at least 3 is recommended)
250 /// @param bright The brightness correction (default = -1 = automatic)
251 /// @param exposure The exposure correction (default = -1 = automatic)
252 /// @param fps The number of frames per second (default = 30fps)
253 DVBuffer2(int cam_no, int num_dma_buffers, int bright = -1, int exposure = -1, double fps = DC::cam_type<T>::fps)
254 : VideoBuffer<T>(VideoBufferType::Live)
255 , RawDCVideo(cam_no, num_dma_buffers, bright, exposure, DC::cam_type<T>::mode, fps)
256 {
257 //Apparently, DVBuffer isn't flushable.
258 //This should probably be fixed.
259 }
260
261 virtual ~DVBuffer2()
262 {
263 }
264
265 double frame_rate()
266 {
267 return RawDCVideo::frame_rate();
268 }
269
270 virtual ImageRef size()
271 {
272 return RawDCVideo::size();
273 }
274
275 virtual VideoFrame<T>* get_frame()
276 {
277 return reinterpret_cast<VideoFrame<T>*>(RawDCVideo::get_frame());
278 }
279
280 virtual void put_frame(VideoFrame<T>* f)
281 {
282 RawDCVideo::put_frame(reinterpret_cast<VideoFrame<byte>*>(f));
283 }
284
285 virtual bool frame_pending()
286 {
287 return RawDCVideo::frame_pending();
288 }
289
290 virtual void seek_to(double) { }
291 };
292
293 /// An 8-bit greyscale video buffer from a Firewire (IEEE 1394) camera.
294 /// Provides frames of type DVFrame.
295 /// @ingroup gVideoBuffer
296 typedef DVBuffer2<byte> DVBuffer;
297
298 }
299
300 #endif