Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpV4l2Grabber.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Video for linux two framegrabber.
32 */
33
38
39#ifndef vpV4l2Grabber_hh
40#define vpV4l2Grabber_hh
41
42#include <visp3/core/vpConfig.h>
43
44#ifdef VISP_HAVE_V4L2
45
46#include <libv4l2.h> // Video For Linux Two interface
47#include <linux/videodev2.h> // Video For Linux Two interface
48#include <string>
49
50#include <visp3/core/vpFrameGrabber.h>
51#include <visp3/core/vpImage.h>
52#include <visp3/core/vpRGBa.h>
53#include <visp3/core/vpRect.h>
54
129class VISP_EXPORT vpV4l2Grabber : public vpFrameGrabber
130{
131public:
132 static const unsigned int DEFAULT_INPUT;
133 static const unsigned int DEFAULT_SCALE;
134 static const __u32 MAX_INPUTS;
135 static const __u32 MAX_NORM;
136 static const __u32 MAX_FORMAT;
137 static const unsigned int MAX_CTRL;
138 static const unsigned int MAX_BUFFERS;
139 static const unsigned int FRAME_SIZE;
140
144 typedef enum
145 {
148 } vpV4l2FramerateType;
149
153 typedef enum
154 {
157 } vpV4l2FrameFormatType;
158
171
172#ifndef DOXYGEN_SHOULD_SKIP_THIS
173 struct ng_video_fmt
174 {
175 unsigned int pixelformat; /* VIDEO_* */
176 unsigned int width;
177 unsigned int height;
178 unsigned int bytesperline; /* zero for compressed formats */
179 };
180
181 struct ng_video_buf
182 {
183 struct ng_video_fmt fmt;
184 size_t size;
185 unsigned char *data;
186 int refcount;
187 };
188#endif
189
190 // private:
191 //#ifndef DOXYGEN_SHOULD_SKIP_THIS
192 // vpV4l2Grabber(const vpV4l2Grabber &)
193 // : fd(-1), device(), cap(), streamparm(), inp(nullptr), std(nullptr),
194 // fmt(nullptr), ctl(nullptr),
195 // fmt_v4l2(), fmt_me(), reqbufs(), buf_v4l2(nullptr), buf_me(nullptr),
196 // queue(0), waiton_cpt(0), index_buffer(0), m_verbose(false),
197 // m_nbuffers(3), field(0), streaming(false),
198 // m_input(vpV4l2Grabber::DEFAULT_INPUT),
199 // m_framerate(vpV4l2Grabber::framerate_25fps),
200 // m_frameformat(vpV4l2Grabber::V4L2_FRAME_FORMAT),
201 // m_pixelformat(vpV4l2Grabber::V4L2_YUYV_FORMAT)
202 // {
203 // throw vpException(vpException::functionNotImplementedError,"Not
204 // implemented!");
205 // }
206 // vpV4l2Grabber &operator=(const vpV4l2Grabber &){
207 // throw vpException(vpException::functionNotImplementedError,"Not
208 // implemented!"); return *this;
209 // }
210 //#endif
211
212#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
213 vpV4l2Grabber(const vpV4l2Grabber &) = delete; // non construction-copyable
214 vpV4l2Grabber &operator=(const vpV4l2Grabber &) = delete; // non copyable
215#endif
216
217public:
219 VP_EXPLICIT vpV4l2Grabber(bool verbose);
220 vpV4l2Grabber(unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
221 vpV4l2Grabber(vpImage<unsigned char> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
222 vpV4l2Grabber(vpImage<vpRGBa> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
223 virtual ~vpV4l2Grabber();
224
225public:
227 void open(vpImage<vpRGBa> &I);
228
230 void acquire(vpImage<unsigned char> &I, const vpRect &roi);
231 void acquire(vpImage<unsigned char> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
232 void acquire(vpImage<vpRGBa> &I);
233 void acquire(vpImage<vpRGBa> &I, const vpRect &roi);
234 void acquire(vpImage<vpRGBa> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
235 bool getField();
244 inline vpV4l2PixelFormatType getPixelFormat() { return (this->m_pixelformat); }
245
247 vpV4l2Grabber &operator>>(vpImage<vpRGBa> &I);
248
253 void setVerboseMode(bool verbose) { this->m_verbose = verbose; }
254 void setFramerate(vpV4l2FramerateType framerate);
255
256 void setInput(unsigned input = vpV4l2Grabber::DEFAULT_INPUT);
257
262 inline void setWidth(unsigned w) { this->width = w; }
267 inline void setHeight(unsigned h) { this->height = h; }
268
269 void setScale(unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
270
282 inline void setNBuffers(unsigned nbuffers) { this->m_nbuffers = nbuffers; }
283
290 inline void setDevice(const std::string &devname) { device = devname; }
299 inline void setPixelFormat(vpV4l2PixelFormatType pixelformat)
300 {
301 this->m_pixelformat = pixelformat;
302 if (this->m_pixelformat >= V4L2_MAX_FORMAT)
303 this->m_pixelformat = V4L2_RGB24_FORMAT;
304 }
305
306 void close();
307
308private:
309 void setFormat();
317 inline void setFrameFormat(vpV4l2FrameFormatType frameformat) { this->m_frameformat = frameformat; }
318 void open();
319 void getCapabilities();
320 void startStreaming();
321 void stopStreaming();
322 unsigned char *waiton(__u32 &index, struct timeval &timestamp);
323 int queueBuffer();
324 void queueAll();
325 void printBufInfo(struct v4l2_buffer buf);
326
327 int fd;
328 std::string device;
329 /* device descriptions */
330 struct v4l2_capability cap;
331 struct v4l2_streamparm streamparm;
332 struct v4l2_input *inp; //[vpV4l2Grabber::MAX_INPUTS];
333 struct v4l2_standard *std; //[vpV4l2Grabber::MAX_NORM];
334 struct v4l2_fmtdesc *fmt; //[vpV4l2Grabber::MAX_FORMAT];
335 struct v4l2_queryctrl *ctl; //[vpV4l2Grabber::MAX_CTRL*2];
336
337 /* capture */
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 m_verbose;
348 unsigned m_nbuffers;
349 unsigned int field;
350 bool streaming;
351
352 unsigned m_input;
353 vpV4l2FramerateType m_framerate;
354 vpV4l2FrameFormatType m_frameformat;
355 vpV4l2PixelFormatType m_pixelformat;
356};
357END_VISP_NAMESPACE
358#endif
359#endif
virtual void open(vpImage< unsigned char > &I)=0
unsigned int height
Number of rows in the image.
unsigned int width
Number of columns in the image.
virtual void acquire(vpImage< unsigned char > &I)=0
virtual void close()=0
Definition of the vpImage class member functions.
Definition vpImage.h:131
Defines a rectangle in the plane.
Definition vpRect.h:79
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
@ framerate_50fps
50 frames per second
@ framerate_25fps
25 frames per second
void setWidth(unsigned w)
static const __u32 MAX_NORM
static const unsigned int MAX_CTRL
static const unsigned int MAX_BUFFERS
vpV4l2Grabber & operator=(const vpV4l2Grabber &)=delete
static const __u32 MAX_INPUTS
static const unsigned int DEFAULT_INPUT
void setVerboseMode(bool verbose)
vpV4l2Grabber(const vpV4l2Grabber &)=delete
vpV4l2PixelFormatType getPixelFormat()
static const unsigned int FRAME_SIZE
static const unsigned int DEFAULT_SCALE
static const __u32 MAX_FORMAT
void setHeight(unsigned h)
void setPixelFormat(vpV4l2PixelFormatType pixelformat)
void setNBuffers(unsigned nbuffers)
vpV4l2FramerateType getFramerate()
void setDevice(const std::string &devname)