libcvd
v4lbuffer.h
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 {
23  namespace V4LBuffer
24  {
27  {
28  using Exceptions::VideoBuffer::All::All;
29  };
32  struct DeviceOpen : public All
33  {
34  DeviceOpen(std::string dev);
35  };
36 
39  struct NoColourspace : public All
40  {
41  NoColourspace(std::string dev, std::string space);
42  };
43 
46  struct DeviceSetup : public All
47  {
48  DeviceSetup(std::string dev, std::string action);
49  };
52  struct PutFrame : public All
53  {
54  PutFrame(std::string dev, std::string msg);
55  };
58  struct GetFrame : public All
59  {
60  GetFrame(std::string dev, std::string msg);
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 <>
79  {
80  static const unsigned int v4l2_fmt = V4L2_PIX_FMT_SBGGR8;
81  };
82  template <>
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 
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)
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 
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 
222  void operator=(V4LBuffer&);
223 };
224 
225 };
226 #endif
Base class which provides untyped access to video grabber objects.
Definition: videobuffer.h:39
A datatype to represent yuv420p (yy...u...v) data.
Definition: colourspaces.h:202
A colour consisting of red, green and blue components.
Definition: rgb.h:25
Device was OK, but could not provide the requested colourspace.
Definition: v4lbuffer.h:39
All classes and functions are within the CVD namespace.
Definition: argb.h:6
virtual bool frame_pending()
Is there a frame waiting in the buffer? This function does not block.
Definition: v4lbuffer.h:193
Base class for objects which provide a typed video stream.
Definition: videobuffer.h:88
A live video buffer which uses the Video for Linux 2 (V4L2) API.
Definition: v4lbuffer.h:162
Definition: v4lbuffer.h:122
Definition: v4lbuffer.cc:129
Definition: v4lbuffer.h:69
Error in a put_frame() call.
Definition: v4lbuffer.h:52
A 32-bit colour.
Definition: rgb8.h:11
virtual double frame_rate()
What is the (expected) frame rate of this video buffer, in frames per second?
Definition: v4lbuffer.h:198
Error setting up the device.
Definition: v4lbuffer.h:46
A datatype to represent the other yuv422 (uyvy) data.
Definition: colourspaces.h:213
virtual ImageRef size()
The size of the VideoFrames returned by this buffer.
Definition: v4lbuffer.h:171
virtual VideoFrame< T > * get_frame()
Returns the next frame from the buffer. This function blocks until a frame is ready.
Definition: v4lbuffer.h:176
Definition: image_ref.h:29
Bayer datatype representing the colour filter pattern GRBG.
Definition: colourspaces.h:40
unsigned char byte
An 8-bit datatype.
Definition: byte.h:8
Base class for all VideoBuffer exceptions.
Definition: videobuffer.h:153
A frame from a VideoBuffer.
Definition: videoframe.h:35
Bayer datatype representing the colour filter pattern BGGR.
Definition: colourspaces.h:14
Definition: v4lbuffer.h:26
Error in a get_frame() call.
Definition: v4lbuffer.h:58
A datatype to represent yuv422 (yuyv) data.
Definition: colourspaces.h:192
The buffer is flushable: it is live and VideoBuffer::frame_pending() returns an accurate result...
Definition: videobuffer.h:26
Error opening the device.
Definition: v4lbuffer.h:32
Definition: v4lbuffer.h:119
virtual void put_frame(VideoFrame< T > *f)
Tell the buffer that you are finished with this frame.
Definition: v4lbuffer.h:182