libcvd
dvbuffer.h
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 {
23  namespace DVBuffer
24  {
28  {
29  };
30 
33  struct Raw1394Setup : public All
34  {
35  Raw1394Setup(std::string action);
36  };
37 
40  struct DC1394Setup : public All
41  {
42  DC1394Setup(std::string action);
43  };
44 
47  struct BadCameraSelection : public All
48  {
49  BadCameraSelection(int num_cameras, int camera_no);
50  };
51 
54  struct BusReset : public All
55  {
56  BusReset();
57  };
58 
61  struct DeviceOpen : public All
62  {
63  DeviceOpen(std::string dev);
64  };
67  struct DeviceSetup : public All
68  {
69  DeviceSetup(std::string action);
70  };
73  struct PutFrame : public All
74  {
75  PutFrame(std::string dev);
76  };
79  struct GetFrame : public All
80  {
81  GetFrame(std::string dev);
82  };
83  }
84 }
85 
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 
132  {
133  public:
141  RawDCVideo(int camera_no, int num_dma_buffers, int bright, int exposure, int mode, double frame_rate);
142  ~RawDCVideo();
143 
145  ImageRef size();
147  VideoFrame<byte>* get_frame();
150  void put_frame(VideoFrame<byte>* f);
152  bool frame_pending();
153 
156  void set_shutter(unsigned int s);
158  unsigned int get_shutter();
159 
162  void set_iris(unsigned int i);
164  unsigned int get_iris();
165 
168  void set_sharpness(unsigned int s);
170  unsigned int get_sharpness();
171 
174  void set_gain(unsigned int g);
176  unsigned int get_gain();
177 
180  void set_exposure(unsigned int e);
182  unsigned int get_exposure();
183 
186  void set_brightness(unsigned int b);
188  unsigned int get_brightness();
189 
193  void set_feature_value(unsigned int feature, unsigned int value);
194 
197  unsigned int get_feature_value(unsigned int feature);
198 
201  std::pair<unsigned int, unsigned int> get_feature_min_max(unsigned int feature);
202 
206  void auto_on_off(unsigned int feature, unsigned int auto_value);
207 
209  double frame_rate();
210 
212  raw1394handle_t& handle();
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 
243 template <class T>
244 class DVBuffer2 : public VideoBuffer<T>, public DC::RawDCVideo
245 {
246  public:
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 
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 
297 
298 }
299 
300 #endif
Error in a put_frame() call.
Definition: dvbuffer.h:73
virtual bool frame_pending()
Is there a frame waiting in the buffer? This function does not block.
Definition: dvbuffer.h:285
A video buffer from a Firewire (IEEE 1394) camera.
Definition: dvbuffer.h:244
Error doing some later setup action.
Definition: dvbuffer.h:67
Internal (non type-safe) class used by DVBuffer2 to do the actual interfacing with the Firewire (IEE ...
Definition: dvbuffer.h:131
A colour consisting of red, green and blue components.
Definition: rgb.h:25
All classes and functions are within the CVD namespace.
Definition: argb.h:6
A datatype to represent yuv411 (uyyvyy) data, typically from firewire cameras.
Definition: colourspaces.h:176
Bad camera selection.
Definition: dvbuffer.h:47
Base class for objects which provide a typed video stream.
Definition: videobuffer.h:88
virtual void seek_to(double)
Go to a particular point in the video buffer (only implemented in buffers of recorded video) ...
Definition: dvbuffer.h:290
Error opening the device.
Definition: dvbuffer.h:61
Definition: dvbuffer.h:91
The semsntics of the videobuffer. See VideoFrame::type()
Definition: videobuffer.h:12
Error in a get_frame() call.
Definition: dvbuffer.h:79
DVBuffer2(int cam_no, int num_dma_buffers, int bright=-1, int exposure=-1, double fps=DC::cam_type< T >::fps)
Construct a video buffer.
Definition: dvbuffer.h:253
virtual void put_frame(VideoFrame< T > *f)
Tell the buffer that you are finished with this frame.
Definition: dvbuffer.h:280
double frame_rate()
What is the (expected) frame rate of this video buffer, in frames per second?
Definition: dvbuffer.h:265
Base class for all V4L2 exceptions.
Definition: dvbuffer.h:27
Definition: image_ref.h:29
unsigned char byte
An 8-bit datatype.
Definition: byte.h:8
Base class for all VideoBuffer exceptions.
Definition: videobuffer.h:153
Bus reset needed.
Definition: dvbuffer.h:54
virtual VideoFrame< T > * get_frame()
Returns the next frame from the buffer. This function blocks until a frame is ready.
Definition: dvbuffer.h:275
Error with RAW1394 setup.
Definition: dvbuffer.h:33
Definition: dvbuffer.h:119
Error with DC1394 setup.
Definition: dvbuffer.h:40
virtual ImageRef size()
The size of the VideoFrames returned by this buffer.
Definition: dvbuffer.h:270
DVBuffer2< byte > DVBuffer
An 8-bit greyscale video buffer from a Firewire (IEEE 1394) camera.
Definition: dvbuffer.h:296