YARP
Yet Another Robot Platform
V4L_camera.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 
20 #ifndef YARP_DEVICE_USBCAMERA_LINUX_V4L_CAMERA_H
21 #define YARP_DEVICE_USBCAMERA_LINUX_V4L_CAMERA_H
22 
23 #include <yarp/os/PeriodicThread.h>
24 #include <yarp/os/Semaphore.h>
25 
26 #include <yarp/dev/DeviceDriver.h>
28 #include <yarp/dev/IVisualParams.h>
30 
31 #include <asm/types.h>
32 #include <opencv2/opencv.hpp>
33 #include <cerrno>
34 #include <fcntl.h>
35 #include <getopt.h>
36 #include <iostream>
37 #include <jpeglib.h>
38 #include <libv4l2.h>
39 #include <libv4lconvert.h>
40 #include <linux/videodev2.h>
41 #include <malloc.h>
42 #include <map>
43 #include <cstdlib>
44 #include <cstring>
45 #include <sys/ioctl.h>
46 #include <sys/mman.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <sys/types.h>
50 #include <unistd.h>
51 
52 #define CLEAR(x) memset(&(x), 0, sizeof(x))
53 
54 // minimum number of buffers to request in VIDIOC_REQBUFS call
55 #define DEFAULT_WIDTH 640
56 #define DEFAULT_HEIGHT 480
57 #define DEFAULT_FRAMERATE 30
58 #define VIDIOC_REQBUFS_COUNT 2
59 
60 typedef enum
61 {
65 } io_method;
66 
67 typedef enum
68 {
72 
73 
74 struct buffer
75 {
76  void* start;
77  size_t length;
78 };
79 
80 
81 typedef struct
82 {
83  int fd;
84  std::string deviceId;
85 
87  int resizeOffset_x, resizeOffset_y;
88  int resizeWidth, resizeHeight;
89 
90  __u32 user_width;
91  __u32 user_height;
92 
93  double horizontalFov;
94  double verticalFov;
96  bool dual;
97 
99  int fps;
100 
101  // Temporary step required for leopard python camera only
102  // The image has to be converted into standard bayer format
103  // in order to be correctly converted into rgb
104  unsigned char* raw_image;
105  unsigned int raw_image_size;
106 
107  // this is a helper pointer set either to raw_image or src_image,
108  // depending if the source is custom or standard
109  unsigned char* read_image;
110 
111  // src image: standard image type read from the camera sensor
112  // used as input for color conversion
113  unsigned char* src_image;
114  unsigned int src_image_size;
115 
116  // RGB image after color conversion. The size may not be the one
117  // requested by the user and a rescaling may be required afterwards
118  unsigned char* dst_image_rgb;
119  unsigned int dst_image_size_rgb;
120 
121  // OpenCV object to perform the final rescaling of the image
122  cv::Mat outMat; // OpenCV output
123 
125  bool flip;
126 
127  unsigned int n_buffers;
128  struct buffer* buffers;
129  struct v4l2_format src_fmt;
130  struct v4l2_format dst_fmt;
131  struct v4l2_requestbuffers req;
132  size_t pixelType;
133  supported_cams camModel; // In case some camera requires custom procedure
134 } Video_params;
135 
136 
137 /*
138  * Device handling
139  */
140 
141 class V4L_camera :
149 {
150 public:
151  V4L_camera();
152 
153  // DeviceDriver Interface
154  bool open(yarp::os::Searchable& config) override;
155  bool close() override;
156 
158 
159  // IFrameGrabberRgb Interface
160  bool getRgbBuffer(unsigned char* buffer) override;
161 
162  // IFrameGrabber Interface
163  bool getRawBuffer(unsigned char* buffer) override;
164  int getRawBufferSize() override;
165 
170  int height() const override;
171 
176  int width() const override;
177 
178  /*Implementation of IRgbVisualParams interface*/
179  int getRgbHeight() override;
180  int getRgbWidth() override;
182  bool getRgbResolution(int& width, int& height) override;
183  bool setRgbResolution(int width, int height) override;
184  bool getRgbFOV(double& horizontalFov, double& verticalFov) override;
185  bool setRgbFOV(double horizontalFov, double verticalFov) override;
186  bool getRgbIntrinsicParam(yarp::os::Property& intrinsic) override;
187  bool getRgbMirroring(bool& mirror) override;
188  bool setRgbMirroring(bool mirror) override;
189 
190 
191  /* Implementation of IFrameGrabberControls interface */
192  bool getCameraDescription(CameraDescriptor* camera) override;
193  bool hasFeature(int feature, bool* hasFeature) override;
194  bool setFeature(int feature, double value) override;
195  bool getFeature(int feature, double* value) override;
196  bool setFeature(int feature, double value1, double value2) override;
197  bool getFeature(int feature, double* value1, double* value2) override;
198  bool hasOnOff(int feature, bool* _hasOnOff) override;
199  bool setActive(int feature, bool onoff) override;
200  bool getActive(int feature, bool* _isActive) override;
201  bool hasAuto(int feature, bool* _hasAuto) override;
202  bool hasManual(int feature, bool* _hasManual) override;
203  bool hasOnePush(int feature, bool* _hasOnePush) override;
204  bool setMode(int feature, FeatureMode mode) override;
205  bool getMode(int feature, FeatureMode* mode) override;
206  bool setOnePush(int feature) override;
207 
208 private:
209  bool verbose;
210  v4lconvert_data* _v4lconvert_data;
211  bool use_exposure_absolute;
212 
213  yarp::os::Stamp timeStamp;
214  Video_params param;
215  yarp::os::Semaphore mutex;
216  bool configFx, configFy;
217  bool configPPx, configPPy;
218  bool configRet, configDistM;
219  bool configIntrins;
220  bool configured;
221  bool doCropping;
222  bool isActive_vector[YARP_FEATURE_NUMBER_OF];
223  double timeStart, timeTot, timeNow, timeElapsed;
224  int myCounter;
225  int frameCounter;
226 
227  std::map<std::string, supported_cams> camMap;
228 
229  bool fromConfig(yarp::os::Searchable& config);
230 
231  void populateConfigurations();
232 
233  int convertV4L_to_YARP_format(int format);
234 
235  double checkDouble(yarp::os::Searchable& config, const char* key);
236 
237  // initialize device
238  bool deviceInit();
239 
240  // de-initialize device
241  bool deviceUninit();
242 
243  void captureStart();
244  void captureStop();
245 
246  bool threadInit() override;
247  void run() override;
248  void threadRelease() override;
249 
250 
251  /*
252  * Inintialize different types of reading frame
253  */
254 
255  // some description
256  bool readInit(unsigned int buffer_size);
257 
258  // some description
259  bool mmapInit();
260 
261  // some description
262  bool userptrInit(unsigned int buffer_size);
263 
264 
265  // use the device for something
269  bool frameRead();
270 
271  bool full_FrameRead();
272 
273  /*
274  * This function is intended to perform custom code to adapt
275  * non standard pixel types to a standard one, in order to
276  * use standard conversion libraries afterward.
277  */
278  void imagePreProcess();
279 
280  /*
281  * This function is intended to perform all the required conversions
282  * from the camera pixel type to the RGB one and eventually rescaling
283  * to size requested by the user.
284  */
285  void imageProcess();
286 
287  int getfd();
288 
289 private:
290  // low level stuff - all functions here uses the Linux V4L specific definitions
299  int xioctl(int fd, int request, void* argp);
300 
301  int convertYARP_to_V4L(int feature);
302  void enumerate_menu();
303  bool enumerate_controls();
304  bool check_V4L2_control(uint32_t id);
305  bool set_V4L2_control(u_int32_t id, double value, bool verbatim = false);
306  double get_V4L2_control(uint32_t id, bool verbatim = false); // verbatim = do not convert value, for enum types
307 
308  double toEpochOffset;
309 
310  // leopard de-bayer test
311  int bit_shift;
312  int bit_bayer;
313  int pixel_fmt_leo;
314 };
315 
316 #endif // YARP_DEVICE_USBCAMERA_LINUX_V4L_CAMERA_H
CameraDescriptor
Definition: FrameGrabberInterfaces.h:35
LEOPARD_PYTHON
@ LEOPARD_PYTHON
Definition: V4L_camera.h:70
Video_params::intrinsic
yarp::os::Property intrinsic
Definition: V4L_camera.h:95
V4L_camera
Definition: V4L_camera.h:149
Video_params::dual
bool dual
Definition: V4L_camera.h:96
Video_params::fps
int fps
Definition: V4L_camera.h:99
YARP_FEATURE_NUMBER_OF
@ YARP_FEATURE_NUMBER_OF
Definition: FrameGrabberInterfaces.h:109
Video_params::deviceId
std::string deviceId
Definition: V4L_camera.h:84
V4L_camera::setOnePush
bool setOnePush(int feature) override
Set the requested feature to a value (saturation, brightness ...
Definition: V4L_camera.cpp:2038
Video_params::raw_image
unsigned char * raw_image
Definition: V4L_camera.h:104
Video_params::verticalFov
double verticalFov
Definition: V4L_camera.h:94
V4L_camera::getFeature
bool getFeature(int feature, double *value) override
Get the current value for the requested feature.
Definition: V4L_camera.cpp:1684
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::os::Semaphore
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:29
V4L_camera::getRgbFOV
bool getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
Definition: V4L_camera.cpp:348
V4L_camera::height
int height() const override
Return the height of each frame.
Definition: V4L_camera.cpp:952
V4L_camera::getActive
bool getActive(int feature, bool *_isActive) override
Get the current status of the feature, on or off.
Definition: V4L_camera.cpp:1810
io_method
io_method
Definition: V4L_camera.h:61
IPreciselyTimed.h
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
Video_params::camModel
supported_cams camModel
Definition: V4L_camera.h:133
Video_params::configurations
yarp::sig::VectorOf< yarp::dev::CameraConfig > configurations
Definition: V4L_camera.h:124
yarp::dev::IFrameGrabber
Common interface to a FrameGrabber.
Definition: FrameGrabberInterfaces.h:169
V4L_camera::setRgbFOV
bool setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
Definition: V4L_camera.cpp:355
Video_params::flip
bool flip
Definition: V4L_camera.h:125
V4L_camera::hasManual
bool hasManual(int feature, bool *_hasManual) override
Check if the requested feature has the 'manual' mode.
Definition: V4L_camera.cpp:1873
V4L_camera::close
bool close() override
close device
Definition: V4L_camera.cpp:882
Video_params::src_image
unsigned char * src_image
Definition: V4L_camera.h:113
Video_params::user_height
__u32 user_height
Definition: V4L_camera.h:91
Video_params::raw_image_size
unsigned int raw_image_size
Definition: V4L_camera.h:105
Video_params::read_image
unsigned char * read_image
Definition: V4L_camera.h:109
buffer::start
void * start
Definition: V4L_camera.h:76
V4L_camera::setRgbMirroring
bool setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
Definition: V4L_camera.cpp:374
V4L_camera::getRgbWidth
int getRgbWidth() override
Return the width of each frame.
Definition: V4L_camera.cpp:318
V4L_camera::getRgbHeight
int getRgbHeight() override
Return the height of each frame.
Definition: V4L_camera.cpp:313
supported_cams
supported_cams
Definition: V4L_camera.h:68
V4L_camera::setFeature
bool setFeature(int feature, double value) override
Set the requested feature to a value (saturation, brightness ...
Definition: V4L_camera.cpp:1665
V4L_camera::getRgbBuffer
bool getRgbBuffer(unsigned char *buffer) override
Get a rgb buffer from the frame grabber, if required demosaicking/color reconstruction is applied.
Definition: V4L_camera.cpp:903
yarp::sig::VectorOf< yarp::dev::CameraConfig >
IO_METHOD_USERPTR
@ IO_METHOD_USERPTR
Definition: V4L_camera.h:64
V4L_camera::hasAuto
bool hasAuto(int feature, bool *_hasAuto) override
Check if the requested feature has the 'auto' mode.
Definition: V4L_camera.cpp:1843
buffer::length
size_t length
Definition: V4L_camera.h:77
Video_params::n_buffers
unsigned int n_buffers
Definition: V4L_camera.h:127
V4L_camera::getLastInputStamp
yarp::os::Stamp getLastInputStamp() override
Return the time stamp relative to the last acquisition.
Definition: V4L_camera.cpp:154
V4L_camera::setRgbResolution
bool setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
Definition: V4L_camera.cpp:335
Video_params::pixelType
size_t pixelType
Definition: V4L_camera.h:132
Video_params::horizontalFov
double horizontalFov
Definition: V4L_camera.h:93
Video_params::fd
int fd
Definition: V4L_camera.h:83
buffer
Definition: V4L_camera.h:75
IO_METHOD_READ
@ IO_METHOD_READ
Definition: V4L_camera.h:62
V4L_camera::hasOnePush
bool hasOnePush(int feature, bool *_hasOnePush) override
Check if the requested feature has the 'onePush' mode.
Definition: V4L_camera.cpp:1887
V4L_camera::width
int width() const override
Return the width of each frame.
Definition: V4L_camera.cpp:965
V4L_camera::getRgbSupportedConfigurations
bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
Definition: V4L_camera.cpp:323
yarp::dev::IFrameGrabberControls
Control interface for frame grabber devices.
Definition: FrameGrabberInterfaces.h:365
Semaphore.h
Video_params::src_image_size
unsigned int src_image_size
Definition: V4L_camera.h:114
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
FrameGrabberInterfaces.h
define common interfaces to discover remote camera capabilities
Video_params::user_width
__u32 user_width
Definition: V4L_camera.h:90
Video_params::dst_image_size_rgb
unsigned int dst_image_size_rgb
Definition: V4L_camera.h:119
PeriodicThread.h
yarp::os::Stamp
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25
V4L_camera::V4L_camera
V4L_camera()
Definition: V4L_camera.cpp:100
yarp::dev::IRgbVisualParams
An interface for retrieving intrinsic parameter from a rgb camera.
Definition: IVisualParams.h:73
V4L_camera::getRgbResolution
bool getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
Definition: V4L_camera.cpp:328
Video_params
Definition: V4L_camera.h:82
FeatureMode
FeatureMode
Definition: FrameGrabberInterfaces.h:29
Video_params::resizeWidth
int resizeWidth
Definition: V4L_camera.h:88
IO_METHOD_MMAP
@ IO_METHOD_MMAP
Definition: V4L_camera.h:63
STANDARD_UVC
@ STANDARD_UVC
Definition: V4L_camera.h:69
V4L_camera::getRgbIntrinsicParam
bool getRgbIntrinsicParam(yarp::os::Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
Definition: V4L_camera.cpp:361
V4L_camera::getRgbMirroring
bool getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
Definition: V4L_camera.cpp:367
Video_params::outMat
cv::Mat outMat
Definition: V4L_camera.h:122
V4L_camera::getCameraDescription
bool getCameraDescription(CameraDescriptor *camera) override
Get a basic description of the camera hw.
Definition: V4L_camera.cpp:1631
V4L_camera::getRawBuffer
bool getRawBuffer(unsigned char *buffer) override
Get the raw buffer from the frame grabber.
Definition: V4L_camera.cpp:927
Video_params::dst_image_rgb
unsigned char * dst_image_rgb
Definition: V4L_camera.h:118
Video_params::addictionalResize
bool addictionalResize
Definition: V4L_camera.h:86
V4L_camera::setMode
bool setMode(int feature, FeatureMode mode) override
Set the requested mode for the feature.
Definition: V4L_camera.cpp:1902
V4L_camera::open
bool open(yarp::os::Searchable &config) override
open device
Definition: V4L_camera.cpp:234
IVisualParams.h
V4L_camera::hasFeature
bool hasFeature(int feature, bool *hasFeature) override
Check if camera has the requested feature (saturation, brightness ...
Definition: V4L_camera.cpp:1638
V4L_camera::getMode
bool getMode(int feature, FeatureMode *mode) override
Get the current mode for the feature.
Definition: V4L_camera.cpp:1971
yarp::dev::IPreciselyTimed
Definition: IPreciselyTimed.h:21
V4L_camera::getRawBufferSize
int getRawBufferSize() override
Get the size of the card's internal buffer, the user should use this method to allocate the storage t...
Definition: V4L_camera.cpp:943
Video_params::resizeOffset_y
int resizeOffset_y
Definition: V4L_camera.h:87
Video_params::io
io_method io
Definition: V4L_camera.h:98
yarp::dev::IFrameGrabberRgb
RGB Interface to a FrameGrabber device.
Definition: FrameGrabberInterfaces.h:210
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
DeviceDriver.h
V4L_camera::hasOnOff
bool hasOnOff(int feature, bool *_hasOnOff) override
Check if the camera has the ability to turn on/off the requested feature.
Definition: V4L_camera.cpp:1732
V4L_camera::setActive
bool setActive(int feature, bool onoff) override
Set the requested feature on or off.
Definition: V4L_camera.cpp:1760