Fawkes API  Fawkes Development Version
v4l2.h
1 
2 /***************************************************************************
3  * v4l2.h - Video4Linux 2 camera access
4  *
5  * Generated: Sat Jul 5 20:40:20 2008
6  * Copyright 2008 Tobias Kellner
7  * 2010-2014 Tim Niemueller
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef _FIREVISION_CAMS_V4L2_H_
25 #define _FIREVISION_CAMS_V4L2_H_
26 
27 #include <fvcams/camera.h>
28 #include <fvcams/control/color.h>
29 #include <fvcams/control/image.h>
30 #include <linux/types.h>
31 #include <linux/videodev2.h>
32 
33 /* Number of buffers to use for memory mapped IO */
34 #define MMAP_NUM_BUFFERS 4;
35 
36 namespace firevision {
37 
38 class CameraArgumentParser;
39 class V4L2CameraData;
40 class V4LCamera;
41 
43 {
44  friend V4LCamera;
45 
46 public:
47  V4L2Camera(const char *device_name = "/dev/video0");
48  V4L2Camera(const CameraArgumentParser *cap);
49  virtual ~V4L2Camera();
50 
51  virtual void open();
52  virtual void start();
53  virtual void stop();
54  virtual void close();
55  virtual void flush();
56  virtual void capture();
57  virtual void print_info();
58  virtual bool ready();
59 
60  virtual unsigned char *buffer();
61  virtual unsigned int buffer_size();
62  virtual void dispose_buffer();
63 
64  virtual unsigned int pixel_width();
65  virtual unsigned int pixel_height();
66  virtual colorspace_t colorspace();
67  virtual fawkes::Time *capture_time();
68 
69  virtual void set_image_number(unsigned int n);
70 
71  virtual bool auto_gain();
72  virtual void set_auto_gain(bool enabled);
73  virtual bool auto_white_balance();
74  virtual void set_auto_white_balance(bool enabled);
75  virtual unsigned int exposure_auto();
76  virtual void set_exposure_auto(unsigned int exposure_auto);
77  virtual int red_balance();
78  virtual void set_red_balance(int red_balance);
79  virtual int blue_balance();
80  virtual void set_blue_balance(int blue_balance);
81  virtual int u_balance();
82  virtual void set_u_balance(int u_balance);
83  virtual int v_balance();
84  virtual void set_v_balance(int v_balance);
85  virtual unsigned int brightness();
86  virtual void set_brightness(unsigned int brightness);
87  virtual unsigned int contrast();
88  virtual void set_contrast(unsigned int contrast);
89  virtual unsigned int saturation();
90  virtual void set_saturation(unsigned int saturation);
91  virtual int hue();
92  virtual void set_hue(int hue);
93  virtual unsigned int exposure();
94  virtual void set_exposure(unsigned int exposure);
95  virtual unsigned int gain();
96  virtual void set_gain(unsigned int gain);
97 
98  virtual const char * format();
99  virtual void set_format(const char *format);
100  virtual unsigned int width();
101  virtual unsigned int height();
102  virtual void set_size(unsigned int width, unsigned int height);
103  virtual bool horiz_mirror();
104  virtual bool vert_mirror();
105  virtual void set_horiz_mirror(bool enabled);
106  virtual void set_vert_mirror(bool enabled);
107  virtual unsigned int fps();
108  virtual void set_fps(unsigned int fps);
109  virtual unsigned int lens_x_corr();
110  virtual unsigned int lens_y_corr();
111  virtual void set_lens_x_corr(unsigned int x_corr);
112  virtual void set_lens_y_corr(unsigned int y_corr);
113 
114  virtual bool exposure_auto_priority();
115  virtual void set_exposure_auto_priority(bool enabled);
116  virtual unsigned int white_balance_temperature();
117  virtual void set_white_balance_temperature(unsigned int white_balance_temperature);
118  virtual unsigned int exposure_absolute();
119  virtual void set_exposure_absolute(unsigned int exposure_absolute);
120  virtual unsigned int sharpness();
121  virtual void set_sharpness(unsigned int sharpness);
122 
123 protected:
124  V4L2Camera(const char *device_name, int dev);
125  virtual void set_one_control(const char *ctrl, unsigned int id, int value);
126  virtual int get_one_control(const char *ctrl, unsigned int id);
127 
128 private:
129  virtual void post_open();
130  virtual void select_read_method();
131  virtual void select_standard();
132  virtual void select_input();
133  virtual void select_format();
134  virtual void set_fps();
135  virtual void set_controls();
136  virtual void create_buffer();
137  virtual void reset_cropping();
138 
139 protected:
140  char *_device_name; ///< Device name
141 
142 private:
143  enum ReadMethod {
144  READ, ///< read() input
145  MMAP, ///< Memory mapping input
146  UPTR ///< User pointer input
147  };
148 
149  enum TriState {
150  NOT_SET, ///< parameter not set
151  TRUE, ///< parameter set to true
152  FALSE ///< parameter set to false
153  };
154 
155  struct FrameBuffer
156  {
157  unsigned char *buffer; ///< buffer
158  unsigned int size; ///< buffer size
159  };
160 
161  struct ControlParameterInt
162  {
163  bool set; ///< true if set
164  int value; ///< value
165  };
166 
167  int _dev; ///< Device file descriptor
168 
169  V4L2CameraData *_data;
170 
171  ReadMethod _read_method; ///< Used read method
172  bool _opened; ///< Device has been open()ed
173  bool _started; ///< Device has been start()ed
174  char * _standard; ///< Desired video standard
175  char * _input; ///< Desired video input
176  char _format[5]; ///< FourCC of the image format
177  colorspace_t _colorspace; ///< Used colorspace_t
178 
179  unsigned int _width; ///< Image width
180  unsigned int _height; ///< Image height
181  unsigned int _bytes_per_line; ///< Image bytes per line
182  FrameBuffer * _frame_buffers; ///< Image buffers
183  unsigned int _buffers_length; ///< Image buffer size
184  int _current_buffer; ///< Current Image buffer (-1 if not set)
185  fawkes::Time *_capture_time; ///< Time when last picture was captured
186 
187  bool _switch_u_v; ///< Switch U and V channels
188  unsigned int _fps; ///< Capture FPS
189 
190  TriState _awb; ///< Auto White Balance enabled
191  TriState _agc; ///< Auto Gain enabled
192  TriState _h_flip; ///< Horizontal mirror
193  TriState _v_flip; ///< Vertical mirror
194  ControlParameterInt _brightness; ///< Brightness [0-255] (def. 128)
195  ControlParameterInt _contrast; ///< Contrast [0-127] (def. 64)
196  ControlParameterInt _saturation; ///< Saturation [0-256] (def. 128)
197  ControlParameterInt _hue; ///< Hue [-180-180] (def. 0)
198  ControlParameterInt _red_balance; ///< Red Balance [0-255] (def. 128)
199  ControlParameterInt _blue_balance; ///< Blue Balance [0-255] (def. 128)
200  ControlParameterInt _exposure; ///< Exposure [0-65535] (def. 60)
201  ControlParameterInt _gain; ///< Gain [0-255] (def. 0)
202  ControlParameterInt _lens_x; ///< Lens Correction X [0-255] (def. 0)
203  ControlParameterInt _lens_y; ///< Lens Correction Y [0-255] (def. 0)
204 
205  ControlParameterInt _white_balance_temperature;
206  TriState _exposure_auto_priority;
207  ControlParameterInt _exposure_auto;
208  ControlParameterInt _exposure_absolute;
209  ControlParameterInt _sharpness;
210 
211  bool _nao_hacks; ///< Nao-specific hacks (bad driver)
212 };
213 
214 } // end namespace firevision
215 
216 #endif //FIREVISION_CAMS_V4L2_H__
firevision::V4L2Camera::lens_y_corr
virtual unsigned int lens_y_corr()
Get current lens y correction.
Definition: v4l2.cpp:1654
firevision::V4L2Camera::set_red_balance
virtual void set_red_balance(int red_balance)
Set red balance.
Definition: v4l2.cpp:1448
firevision::V4L2Camera::contrast
virtual unsigned int contrast()
Get current contrast.
Definition: v4l2.cpp:1505
firevision::V4L2Camera::fps
virtual unsigned int fps()
Get the number of frames per second that have been requested from the camera.
Definition: v4l2.cpp:1635
firevision::V4L2Camera
Video4Linux 2 camera access implementation.
Definition: v4l2.h:43
firevision::V4L2Camera::print_info
virtual void print_info()
Print out camera information.
Definition: v4l2.cpp:1674
firevision::V4L2Camera::set_auto_gain
virtual void set_auto_gain(bool enabled)
Enable/disable auto gain.
Definition: v4l2.cpp:1409
firevision::V4L2Camera::flush
virtual void flush()
Flush image queue.
Definition: v4l2.cpp:1155
firevision::V4L2Camera::saturation
virtual unsigned int saturation()
Get current saturation.
Definition: v4l2.cpp:1518
firevision::V4L2Camera::start
virtual void start()
Start image transfer from the camera.
Definition: v4l2.cpp:1074
firevision::V4L2Camera::set_exposure_absolute
virtual void set_exposure_absolute(unsigned int exposure_absolute)
set absolute exposure time (1/s)
Definition: v4l2.cpp:1375
firevision::V4L2Camera::sharpness
virtual unsigned int sharpness()
Get sharpness value.
Definition: v4l2.cpp:1386
firevision::CameraControlImage::size
virtual void size(unsigned int &width, unsigned int &height)
Get the current image size.
Definition: image.cpp:89
firevision::V4L2Camera::set_u_balance
virtual void set_u_balance(int u_balance)
Set u balance.
Definition: v4l2.cpp:1474
firevision::V4L2Camera::auto_gain
virtual bool auto_gain()
Return whether auto gain is enabled.
Definition: v4l2.cpp:1403
firevision::V4L2Camera::format
virtual const char * format()
Get the image format the camera currently uses.
Definition: v4l2.cpp:1570
firevision::V4L2Camera::pixel_width
virtual unsigned int pixel_width()
Width of image in pixels.
Definition: v4l2.cpp:1270
firevision::V4L2Camera::set_exposure_auto
virtual void set_exposure_auto(unsigned int exposure_auto)
Enable/disable auto exposure.
Definition: v4l2.cpp:1435
firevision::V4L2Camera::set_exposure
virtual void set_exposure(unsigned int exposure)
Set new exposure.
Definition: v4l2.cpp:1550
firevision::V4L2Camera::exposure_absolute
virtual unsigned int exposure_absolute()
Get absolute exposure time.
Definition: v4l2.cpp:1365
firevision::V4LCamera
General Video4Linux camera implementation.
Definition: v4l.h:35
firevision::V4L2Camera::v_balance
virtual int v_balance()
Get current v balance.
Definition: v4l2.cpp:1480
firevision::V4L2Camera::blue_balance
virtual int blue_balance()
Get current blue balance.
Definition: v4l2.cpp:1455
firevision::V4L2Camera::_device_name
char * _device_name
Device name.
Definition: v4l2.h:140
firevision::V4L2Camera::set_one_control
virtual void set_one_control(const char *ctrl, unsigned int id, int value)
Set one Camera control value.
Definition: v4l2.cpp:864
firevision::V4L2Camera::set_saturation
virtual void set_saturation(unsigned int saturation)
Set new saturation.
Definition: v4l2.cpp:1524
firevision::V4L2Camera::set_white_balance_temperature
virtual void set_white_balance_temperature(unsigned int white_balance_temperature)
Set white balance.
Definition: v4l2.cpp:1350
firevision::V4L2Camera::white_balance_temperature
virtual unsigned int white_balance_temperature()
Get absolute white balance setting.
Definition: v4l2.cpp:1340
firevision::V4L2Camera::~V4L2Camera
virtual ~V4L2Camera()
Destructor.
Definition: v4l2.cpp:400
firevision::V4L2Camera::set_lens_x_corr
virtual void set_lens_x_corr(unsigned int x_corr)
Set lens x correction.
Definition: v4l2.cpp:1660
firevision::V4L2Camera::stop
virtual void stop()
Stop image transfer from the camera.
Definition: v4l2.cpp:1121
firevision::V4L2Camera::colorspace
virtual colorspace_t colorspace()
Colorspace of returned image.
Definition: v4l2.cpp:1286
firevision::V4L2Camera::close
virtual void close()
Close camera.
Definition: v4l2.cpp:1031
firevision::V4L2Camera::auto_white_balance
virtual bool auto_white_balance()
Return whether auto white balance is enabled.
Definition: v4l2.cpp:1416
firevision::CameraControlImage
Camera image control interface.
Definition: image.h:33
firevision::V4L2Camera::set_auto_white_balance
virtual void set_auto_white_balance(bool enabled)
Enable/disable auto white balance.
Definition: v4l2.cpp:1422
firevision::V4L2Camera::open
virtual void open()
Open the camera.
Definition: v4l2.cpp:416
firevision::V4L2Camera::get_one_control
virtual int get_one_control(const char *ctrl, unsigned int id)
Get one Camera control value.
Definition: v4l2.cpp:903
firevision::V4L2Camera::width
virtual unsigned int width()
Get the current width of the image.
Definition: v4l2.cpp:1584
firevision::V4L2Camera::set_brightness
virtual void set_brightness(unsigned int brightness)
Set new brightness.
Definition: v4l2.cpp:1498
firevision::V4L2Camera::brightness
virtual unsigned int brightness()
Get current brightness.
Definition: v4l2.cpp:1492
firevision::V4L2Camera::buffer_size
virtual unsigned int buffer_size()
Size of buffer.
Definition: v4l2.cpp:1224
firevision::V4L2Camera::capture
virtual void capture()
Capture an image.
Definition: v4l2.cpp:1162
firevision::V4L2Camera::set_image_number
virtual void set_image_number(unsigned int n)
Set image number to retrieve.
Definition: v4l2.cpp:1303
firevision::V4L2Camera::set_fps
virtual void set_fps(unsigned int fps)
Set the number of frames per second the camera tries to deliver.
Definition: v4l2.cpp:1641
firevision::V4L2Camera::gain
virtual unsigned int gain()
Get current gain.
Definition: v4l2.cpp:1557
firevision::V4L2Camera::set_hue
virtual void set_hue(int hue)
Set new hue.
Definition: v4l2.cpp:1537
firevision::V4L2Camera::lens_x_corr
virtual unsigned int lens_x_corr()
Get current lens x correction.
Definition: v4l2.cpp:1648
firevision::V4L2Camera::set_blue_balance
virtual void set_blue_balance(int blue_balance)
Set blue balance.
Definition: v4l2.cpp:1461
firevision::CameraArgumentParser
Camera argument parser.
Definition: camargp.h:36
firevision::V4L2Camera::hue
virtual int hue()
Get current hue.
Definition: v4l2.cpp:1531
firevision::V4L2Camera::set_contrast
virtual void set_contrast(unsigned int contrast)
Set new contrast.
Definition: v4l2.cpp:1511
firevision::V4L2Camera::set_vert_mirror
virtual void set_vert_mirror(bool enabled)
Set whether the camera should mirror images vertically.
Definition: v4l2.cpp:1624
fawkes::Time
A class for handling time.
Definition: time.h:93
firevision::V4L2Camera::V4L2Camera
V4L2Camera(const char *device_name="/dev/video0")
Constructor.
Definition: v4l2.cpp:83
firevision::V4L2Camera::set_lens_y_corr
virtual void set_lens_y_corr(unsigned int y_corr)
Set lens y correction.
Definition: v4l2.cpp:1667
firevision::V4L2Camera::dispose_buffer
virtual void dispose_buffer()
Dispose current buffer.
Definition: v4l2.cpp:1232
firevision::V4L2Camera::red_balance
virtual int red_balance()
Get current red balance.
Definition: v4l2.cpp:1442
firevision::V4L2Camera::set_exposure_auto_priority
virtual void set_exposure_auto_priority(bool enabled)
Set exposure_auto_priority V4L2 control.
Definition: v4l2.cpp:1327
firevision::V4L2Camera::buffer
virtual unsigned char * buffer()
Get access to current image buffer.
Definition: v4l2.cpp:1216
firevision::CameraControlColor
Camera color control interface.
Definition: color.h:33
firevision::V4L2Camera::vert_mirror
virtual bool vert_mirror()
Return whether the camera image is vertically mirrored.
Definition: v4l2.cpp:1610
firevision::V4L2Camera::exposure_auto
virtual unsigned int exposure_auto()
Return whether auto exposure is enabled.
Definition: v4l2.cpp:1429
firevision::V4L2Camera::set_size
virtual void set_size(unsigned int width, unsigned int height)
Set the image size the camera should use.
Definition: v4l2.cpp:1596
firevision::Camera
Camera interface for image aquiring devices in FireVision.
Definition: camera.h:33
firevision::V4L2Camera::height
virtual unsigned int height()
Get the current height of the image.
Definition: v4l2.cpp:1590
firevision::V4L2Camera::set_v_balance
virtual void set_v_balance(int v_balance)
Set v balance.
Definition: v4l2.cpp:1486
firevision::V4L2Camera::set_gain
virtual void set_gain(unsigned int gain)
Set new gain.
Definition: v4l2.cpp:1563
firevision::V4L2Camera::u_balance
virtual int u_balance()
Get current u balance.
Definition: v4l2.cpp:1468
firevision::V4L2Camera::exposure
virtual unsigned int exposure()
Get current exposure.
Definition: v4l2.cpp:1544
firevision::V4L2Camera::ready
virtual bool ready()
Camera is ready for taking pictures.
Definition: v4l2.cpp:1149
firevision::V4L2Camera::pixel_height
virtual unsigned int pixel_height()
Height of image in pixels.
Definition: v4l2.cpp:1278
firevision::V4L2Camera::set_format
virtual void set_format(const char *format)
Set the image format the camera should use.
Definition: v4l2.cpp:1576
firevision::V4L2Camera::exposure_auto_priority
virtual bool exposure_auto_priority()
Get exposure_auto_priority V4L2 control.
Definition: v4l2.cpp:1317
firevision::V4L2Camera::set_horiz_mirror
virtual void set_horiz_mirror(bool enabled)
Set whether the camera should mirror images horizontally.
Definition: v4l2.cpp:1616
firevision::V4L2Camera::capture_time
virtual fawkes::Time * capture_time()
Get the Time of the last successfully captured image.
Definition: v4l2.cpp:1297
firevision::V4L2Camera::set_sharpness
virtual void set_sharpness(unsigned int sharpness)
Set sharpness.
Definition: v4l2.cpp:1396
firevision::V4L2Camera::horiz_mirror
virtual bool horiz_mirror()
Return whether the camera image is horizontally mirrored.
Definition: v4l2.cpp:1604