APM:Libraries
VideoIn.h
Go to the documentation of this file.
1 /*
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 #pragma once
16 
17 #include "AP_HAL_Linux.h"
18 #include <linux/videodev2.h>
19 #include <vector>
20 
21 namespace Linux {
22 
23 struct buffer {
24  unsigned int size;
25  void *mem;
26 };
27 
28 class VideoIn {
29 public:
30  /* This structure implements the fields of the v4l2_pix_format struct
31  * that are considered useful for an optical flow application along
32  * with the v4l2_buffer fields timestamp and sequence*/
33  class Frame {
34  friend class VideoIn;
35  public:
36  uint32_t timestamp;
37  uint32_t sequence;
38  void *data;
39  private:
40  uint32_t buf_index;
41  };
42 
43  bool get_frame(Frame &frame);
44  void put_frame(Frame &frame);
45  void set_device_path(const char* path);
46  void init();
47  bool open_device(const char *device_path, uint32_t memtype);
48  bool allocate_buffers(uint32_t nbufs);
49  void get_pixel_formats(std::vector<uint32_t> *formats);
50  bool set_format(uint32_t *width, uint32_t *height, uint32_t *format,
51  uint32_t *bytesperline, uint32_t *sizeimage);
52  bool set_crop(uint32_t left, uint32_t top,
53  uint32_t width, uint32_t height);
54  void prepare_capture();
55 
56  static void shrink_8bpp(uint8_t *buffer, uint8_t *new_buffer,
57  uint32_t width, uint32_t height, uint32_t left,
58  uint32_t selection_width, uint32_t top,
59  uint32_t selection_height, uint32_t fx, uint32_t fy);
60 
61  static void crop_8bpp(uint8_t *buffer, uint8_t *new_buffer,
62  uint32_t width, uint32_t left,
63  uint32_t crop_width, uint32_t top,
64  uint32_t crop_height);
65 
66  static void yuyv_to_grey(uint8_t *buffer, uint32_t buffer_size,
67  uint8_t *new_buffer);
68 
69 private:
70  void _queue_buffer(int index);
71  bool _set_streaming(bool enable);
72  bool _dequeue_frame(Frame &frame);
73  uint32_t _timeval_to_us(struct timeval& tv);
74  int _fd = -1;
75  struct buffer *_buffers;
76  unsigned int _nbufs;
77  bool _streaming;
78  uint32_t _width;
79  uint32_t _height;
80  uint32_t _format;
81  uint32_t _bytesperline;
82  uint32_t _sizeimage;
83  uint32_t _memtype = V4L2_MEMORY_MMAP;
84 };
85 
86 }
uint32_t _height
Definition: VideoIn.h:79
uint32_t timestamp
Definition: VideoIn.h:36
struct buffer * _buffers
Definition: VideoIn.h:75
unsigned int size
Definition: VideoIn.h:24
uint32_t sequence
Definition: VideoIn.h:37
void * mem
Definition: VideoIn.h:25
uint32_t _width
Definition: VideoIn.h:78
uint32_t _sizeimage
Definition: VideoIn.h:82
unsigned int _nbufs
Definition: VideoIn.h:76
uint32_t buf_index
Definition: VideoIn.h:40
bool _streaming
Definition: VideoIn.h:77
uint32_t _format
Definition: VideoIn.h:80
uint32_t _bytesperline
Definition: VideoIn.h:81
void init()
Generic board initialization function.
Definition: system.cpp:136