ViSP  2.9.0
vpV4l2Grabber.h
1 /****************************************************************************
2  *
3  * $Id: vpV4l2Grabber.h 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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 
139 class VISP_EXPORT vpV4l2Grabber : public vpFrameGrabber
140 {
141 public:
142  static const unsigned int DEFAULT_INPUT;
143  static const unsigned int DEFAULT_SCALE;
144  static const __u32 MAX_INPUTS;
145  static const __u32 MAX_NORM;
146  static const __u32 MAX_FORMAT;
147  static const unsigned int MAX_CTRL;
148  static const unsigned int MAX_BUFFERS;
149  static const unsigned int FRAME_SIZE;
150 
154  typedef enum
155  {
157  framerate_25fps
158  } vpV4l2FramerateType;
159 
163  typedef enum
164  {
166  V4L2_IMAGE_FORMAT
167  } vpV4l2FrameFormatType;
168 
172  typedef enum {
178  V4L2_MAX_FORMAT
179  } vpV4l2PixelFormatType;
180 
181 #ifndef DOXYGEN_SHOULD_SKIP_THIS
182  struct ng_video_fmt {
183  unsigned int pixelformat; /* VIDEO_* */
184  unsigned int width;
185  unsigned int height;
186  unsigned int bytesperline; /* zero for compressed formats */
187  };
188 
189 
190  struct ng_video_buf {
191  struct ng_video_fmt fmt;
192  size_t size;
193  unsigned char *data;
194  int refcount;
195  };
196 #endif
197 
198 public:
199  vpV4l2Grabber();
200  vpV4l2Grabber(bool verbose);
201  vpV4l2Grabber(unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
203  unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
205  unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
206  virtual ~vpV4l2Grabber() ;
207 
208 public:
209  void open(vpImage<unsigned char> &I) ;
210  void open(vpImage<vpRGBa> &I) ;
211 
213  void acquire(vpImage<unsigned char> &I, struct timeval &timestamp) ;
214  void acquire(vpImage<vpRGBa> &I) ;
215  void acquire(vpImage<vpRGBa> &I, struct timeval &timestamp) ;
216  bool getField();
217  vpV4l2FramerateType getFramerate();
226  {
227  return (this->pixelformat);
228  }
233  void setVerboseMode(bool verbose) {
234  this->verbose = verbose;
235  };
236  void setFramerate(vpV4l2FramerateType framerate);
237 
238  void setInput(unsigned input = vpV4l2Grabber::DEFAULT_INPUT) ;
239 
244  inline void setWidth(unsigned width)
245  {
246  this->_width = width;
247  }
252  inline void setHeight(unsigned height)
253  {
254  this->_height = height;
255  }
256 
257  void setScale(unsigned scale = vpV4l2Grabber::DEFAULT_SCALE) ;
258 
270  inline void setNBuffers(unsigned nbuffers)
271  {
272  this->nbuffers = nbuffers;
273  }
274 
281  inline void setDevice(const char *devname)
282  {
283  sprintf(device, "%s", devname);
284  }
293  inline void setPixelFormat(vpV4l2PixelFormatType pixelformat)
294  {
295  this->pixelformat = pixelformat;
296  if (this->pixelformat >= V4L2_MAX_FORMAT)
297  this->pixelformat = V4L2_RGB24_FORMAT;
298  }
299 
300  void close();
301 
302 private:
303 
304  void setFormat();
312  inline void setFrameFormat(vpV4l2FrameFormatType frameformat)
313  {
314  this->frameformat = frameformat;
315  }
316  void open();
317  void getCapabilities();
318  void startStreaming();
319  void stopStreaming();
320  unsigned char * waiton(__u32 &index, struct timeval &timestamp);
321  int queueBuffer();
322  void queueAll();
323  void printBufInfo(struct v4l2_buffer buf);
324 
325 
326  int fd;
327  char device[FILENAME_MAX];
328  /* device descriptions */
329  struct v4l2_capability cap;
330  struct v4l2_streamparm streamparm;
331  struct v4l2_input *inp; //[vpV4l2Grabber::MAX_INPUTS];
332  struct v4l2_standard *std; //[vpV4l2Grabber::MAX_NORM];
333  struct v4l2_fmtdesc *fmt; //[vpV4l2Grabber::MAX_FORMAT];
334  struct v4l2_queryctrl *ctl; //[vpV4l2Grabber::MAX_CTRL*2];
335 
336  /* capture */
337  int fps;
338  struct v4l2_format fmt_v4l2;
339  struct ng_video_fmt fmt_me;
340  struct v4l2_requestbuffers reqbufs;
341  struct v4l2_buffer *buf_v4l2; //[vpV4l2Grabber::MAX_BUFFERS];
342  struct ng_video_buf *buf_me; //[vpV4l2Grabber::MAX_BUFFERS];
343  unsigned int queue;
344  unsigned int waiton_cpt;
345  __u32 index_buffer;
346 
347  bool verbose;
348  unsigned nbuffers;
349  unsigned int field;
350  bool streaming;
351 
352  unsigned input;
353  unsigned _width;
354  unsigned _height;
355  vpV4l2FramerateType framerate;
356  vpV4l2FrameFormatType frameformat;
357  vpV4l2PixelFormatType pixelformat;
358 } ;
359 
360 #endif
361 #endif
362 
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