ViSP  2.7.0
vpV4l2Grabber.h
1 /****************************************************************************
2  *
3  * $Id: vpV4l2Grabber.h 4107 2013-02-06 10:04:49Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Video for linux two framegrabber.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 
49 #ifndef vpV4l2Grabber_hh
50 #define vpV4l2Grabber_hh
51 
52 #include <visp/vpConfig.h>
53 
54 #ifdef VISP_HAVE_V4L2
55 
56 #include <linux/types.h>
57 #include <linux/kernel.h>
58 #include <linux/videodev2.h> // Video For Linux Two interface
59 #include <libv4l2.h> // Video For Linux Two interface
60 
61 #include <visp/vpImage.h>
62 #include <visp/vpFrameGrabber.h>
63 #include <visp/vpRGBa.h>
64 
65 
66 
67 
134 class VISP_EXPORT vpV4l2Grabber : public vpFrameGrabber
135 {
136 public:
137  static const unsigned int DEFAULT_INPUT;
138  static const unsigned int DEFAULT_SCALE;
139  static const __u32 MAX_INPUTS;
140  static const __u32 MAX_NORM;
141  static const __u32 MAX_FORMAT;
142  static const unsigned int MAX_CTRL;
143  static const unsigned int MAX_BUFFERS;
144  static const unsigned int FRAME_SIZE;
145 
149  typedef enum
150  {
152  framerate_25fps
153  } vpV4l2FramerateType;
154 
158  typedef enum
159  {
161  V4L2_IMAGE_FORMAT
162  } vpV4l2FrameFormatType;
163 
167  typedef enum {
173  V4L2_MAX_FORMAT
174  } vpV4l2PixelFormatType;
175 
176 #ifndef DOXYGEN_SHOULD_SKIP_THIS
177  struct ng_video_fmt {
178  unsigned int pixelformat; /* VIDEO_* */
179  unsigned int width;
180  unsigned int height;
181  unsigned int bytesperline; /* zero for compressed formats */
182  };
183 
184 
185  struct ng_video_buf {
186  struct ng_video_fmt fmt;
187  size_t size;
188  unsigned char *data;
189  int refcount;
190  };
191 #endif
192 
193 public:
194  vpV4l2Grabber();
195  vpV4l2Grabber(bool verbose);
196  vpV4l2Grabber(unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
198  unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
200  unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
201  virtual ~vpV4l2Grabber() ;
202 
203 public:
204  void open(vpImage<unsigned char> &I) ;
205  void open(vpImage<vpRGBa> &I) ;
206 
208  void acquire(vpImage<unsigned char> &I, struct timeval &timestamp) ;
209  void acquire(vpImage<vpRGBa> &I) ;
210  void acquire(vpImage<vpRGBa> &I, struct timeval &timestamp) ;
211  bool getField();
212  vpV4l2FramerateType getFramerate();
221  {
222  return (this->pixelformat);
223  }
228  void setVerboseMode(bool verbose) {
229  this->verbose = verbose;
230  };
231  void setFramerate(vpV4l2FramerateType framerate);
232 
233  void setInput(unsigned input = vpV4l2Grabber::DEFAULT_INPUT) ;
234 
239  inline void setWidth(unsigned width)
240  {
241  this->_width = width;
242  }
247  inline void setHeight(unsigned height)
248  {
249  this->_height = height;
250  }
251 
252  void setScale(unsigned scale = vpV4l2Grabber::DEFAULT_SCALE) ;
253 
265  inline void setNBuffers(unsigned nbuffers)
266  {
267  this->nbuffers = nbuffers;
268  }
269 
276  inline void setDevice(const char *devname)
277  {
278  sprintf(device, "%s", devname);
279  }
288  inline void setPixelFormat(vpV4l2PixelFormatType pixelformat)
289  {
290  this->pixelformat = pixelformat;
291  if (this->pixelformat >= V4L2_MAX_FORMAT)
292  this->pixelformat = V4L2_RGB24_FORMAT;
293  }
294 
295  void close();
296 
297 private:
298 
299  void setFormat();
307  inline void setFrameFormat(vpV4l2FrameFormatType frameformat)
308  {
309  this->frameformat = frameformat;
310  }
311  void open();
312  void getCapabilities();
313  void startStreaming();
314  void stopStreaming();
315  unsigned char * waiton(__u32 &index, struct timeval &timestamp);
316  int queueBuffer();
317  void queueAll();
318  void printBufInfo(struct v4l2_buffer buf);
319 
320 
321  int fd;
322  char device[FILENAME_MAX];
323  /* device descriptions */
324  struct v4l2_capability cap;
325  struct v4l2_streamparm streamparm;
326  struct v4l2_input *inp; //[vpV4l2Grabber::MAX_INPUTS];
327  struct v4l2_standard *std; //[vpV4l2Grabber::MAX_NORM];
328  struct v4l2_fmtdesc *fmt; //[vpV4l2Grabber::MAX_FORMAT];
329  struct v4l2_queryctrl *ctl; //[vpV4l2Grabber::MAX_CTRL*2];
330 
331  /* capture */
332  int fps;
333  struct v4l2_format fmt_v4l2;
334  struct ng_video_fmt fmt_me;
335  struct v4l2_requestbuffers reqbufs;
336  struct v4l2_buffer *buf_v4l2; //[vpV4l2Grabber::MAX_BUFFERS];
337  struct ng_video_buf *buf_me; //[vpV4l2Grabber::MAX_BUFFERS];
338  unsigned int queue;
339  unsigned int waiton_cpt;
340  __u32 index_buffer;
341 
342  bool verbose;
343  unsigned nbuffers;
344  unsigned int field;
345  bool streaming;
346 
347  unsigned input;
348  unsigned _width;
349  unsigned _height;
350  vpV4l2FramerateType framerate;
351  vpV4l2FrameFormatType frameformat;
352  vpV4l2PixelFormatType pixelformat;
353 } ;
354 
355 #endif
356 #endif
357 
static const unsigned int MAX_CTRL
50 frames per second
static const __u32 MAX_FORMAT
void setDevice(const char *devname)
void setWidth(unsigned width)
vpV4l2PixelFormatType getPixelFormat()
void setVerboseMode(bool verbose)
static const unsigned int DEFAULT_INPUT
virtual void open(vpImage< unsigned char > &I)=0
void setHeight(unsigned height)
static const unsigned int FRAME_SIZE
unsigned int height
Number of rows in the image.
static const __u32 MAX_INPUTS
static const __u32 MAX_NORM
Base class for all video devices. It is designed to provide a front end to video sources.
Class for the Video4Linux2 video device.
virtual void close()=0
virtual void acquire(vpImage< unsigned char > &I)=0
void setNBuffers(unsigned nbuffers)
unsigned int width
Number of columns in the image.
void setPixelFormat(vpV4l2PixelFormatType pixelformat)
static const unsigned int DEFAULT_SCALE
static const unsigned int MAX_BUFFERS