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
define common interfaces to discover remote camera capabilities
@ YARP_FEATURE_NUMBER_OF
supported_cams
Definition: V4L_camera.h:68
@ STANDARD_UVC
Definition: V4L_camera.h:69
@ LEOPARD_PYTHON
Definition: V4L_camera.h:70
io_method
Definition: V4L_camera.h:61
@ IO_METHOD_MMAP
Definition: V4L_camera.h:63
@ IO_METHOD_READ
Definition: V4L_camera.h:62
@ IO_METHOD_USERPTR
Definition: V4L_camera.h:64
bool getMode(int feature, FeatureMode *mode) override
Get the current mode for the feature.
bool getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
Definition: V4L_camera.cpp:369
bool setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
Definition: V4L_camera.cpp:376
int getRgbWidth() override
Return the width of each frame.
Definition: V4L_camera.cpp:320
bool setMode(int feature, FeatureMode mode) override
Set the requested mode for the feature.
yarp::os::Stamp getLastInputStamp() override
Return the time stamp relative to the last acquisition.
Definition: V4L_camera.cpp:156
bool setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
Definition: V4L_camera.cpp:357
bool setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
Definition: V4L_camera.cpp:337
bool hasOnePush(int feature, bool *_hasOnePush) override
Check if the requested feature has the 'onePush' mode.
bool close() override
close device
Definition: V4L_camera.cpp:884
bool hasFeature(int feature, bool *hasFeature) override
Check if camera has the requested feature (saturation, brightness ...
bool getRgbIntrinsicParam(yarp::os::Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
Definition: V4L_camera.cpp:363
bool hasOnOff(int feature, bool *_hasOnOff) override
Check if the camera has the ability to turn on/off the requested feature.
bool setActive(int feature, bool onoff) override
Set the requested feature on or off.
bool getCameraDescription(CameraDescriptor *camera) override
Get a basic description of the camera hw.
int getRgbHeight() override
Return the height of each frame.
Definition: V4L_camera.cpp:315
bool getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
Definition: V4L_camera.cpp:350
bool hasAuto(int feature, bool *_hasAuto) override
Check if the requested feature has the 'auto' mode.
int width() const override
Return the width of each frame.
Definition: V4L_camera.cpp:967
bool getFeature(int feature, double *value) override
Get the current value for the requested feature.
bool setOnePush(int feature) override
Set the requested feature to a value (saturation, brightness ...
bool setFeature(int feature, double value) override
Set the requested feature to a value (saturation, brightness ...
bool getActive(int feature, bool *_isActive) override
Get the current status of the feature, on or off.
bool getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
Definition: V4L_camera.cpp:330
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:905
int height() const override
Return the height of each frame.
Definition: V4L_camera.cpp:954
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:945
bool getRawBuffer(unsigned char *buffer) override
Get the raw buffer from the frame grabber.
Definition: V4L_camera.cpp:929
bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
Definition: V4L_camera.cpp:325
bool open(yarp::os::Searchable &config) override
open device
Definition: V4L_camera.cpp:236
bool hasManual(int feature, bool *_hasManual) override
Check if the requested feature has the 'manual' mode.
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
Control interface for frame grabber devices.
RGB Interface to a FrameGrabber device.
Common interface to a FrameGrabber.
An interface for retrieving intrinsic parameter from a rgb camera.
Definition: IVisualParams.h:73
An abstraction for a periodic thread.
A class for storing options and configuration information.
Definition: Property.h:37
A base class for nested structures that can be searched.
Definition: Searchable.h:69
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:29
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25
std::string deviceId
Definition: V4L_camera.h:84
unsigned char * read_image
Definition: V4L_camera.h:109
unsigned int n_buffers
Definition: V4L_camera.h:127
yarp::sig::VectorOf< yarp::dev::CameraConfig > configurations
Definition: V4L_camera.h:124
size_t pixelType
Definition: V4L_camera.h:132
struct buffer * buffers
Definition: V4L_camera.h:128
__u32 user_height
Definition: V4L_camera.h:91
unsigned char * raw_image
Definition: V4L_camera.h:104
io_method io
Definition: V4L_camera.h:98
unsigned char * dst_image_rgb
Definition: V4L_camera.h:118
int resizeOffset_x
Definition: V4L_camera.h:87
int resizeHeight
Definition: V4L_camera.h:88
yarp::os::Property intrinsic
Definition: V4L_camera.h:95
__u32 user_width
Definition: V4L_camera.h:90
double horizontalFov
Definition: V4L_camera.h:93
unsigned char * src_image
Definition: V4L_camera.h:113
bool addictionalResize
Definition: V4L_camera.h:86
cv::Mat outMat
Definition: V4L_camera.h:122
supported_cams camModel
Definition: V4L_camera.h:133
double verticalFov
Definition: V4L_camera.h:94
unsigned int dst_image_size_rgb
Definition: V4L_camera.h:119
unsigned int src_image_size
Definition: V4L_camera.h:114
unsigned int raw_image_size
Definition: V4L_camera.h:105
size_t length
Definition: V4L_camera.h:77
void * start
Definition: V4L_camera.h:76