Apollo  6.0
Open source self driving car software
usb_cam.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2014, Robert Bosch LLC.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the Robert Bosch nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  *********************************************************************/
36 
37 #pragma once
38 
39 #include <asm/types.h> /* for videodev2.h */
40 #include <malloc.h>
41 #include <sys/ioctl.h>
42 #include <sys/mman.h>
43 
44 #ifndef __aarch64__
45 #include <immintrin.h>
46 #include <x86intrin.h>
47 #endif
48 
49 extern "C" {
50 #include <libavcodec/avcodec.h>
51 #include <libavutil/mem.h>
52 #include <libswscale/swscale.h>
53 #include <linux/videodev2.h>
54 }
55 
56 #include <libavcodec/version.h>
57 #if LIBAVCODEC_VERSION_MAJOR < 55
58 #define AV_CODEC_ID_MJPEG CODEC_ID_MJPEG
59 #endif
60 
61 #include <memory>
62 #include <sstream>
63 #include <string>
64 
65 #include "cyber/cyber.h"
66 
67 #include "modules/drivers/camera/proto/config.pb.h"
68 
69 namespace apollo {
70 namespace drivers {
71 namespace camera {
72 
73 using apollo::drivers::camera::config::Config;
74 using apollo::drivers::camera::config::IO_METHOD_MMAP;
75 using apollo::drivers::camera::config::IO_METHOD_READ;
76 using apollo::drivers::camera::config::IO_METHOD_UNKNOWN;
77 using apollo::drivers::camera::config::IO_METHOD_USERPTR;
78 using apollo::drivers::camera::config::RGB;
79 using apollo::drivers::camera::config::YUYV;
80 
81 // camera raw image struct
82 struct CameraImage {
83  int width;
84  int height;
87  int is_new;
88  int tv_sec;
89  int tv_usec;
90  char* image;
91 
93  if (image != nullptr) {
94  free(reinterpret_cast<void*>(image));
95  image = nullptr;
96  }
97  }
98 };
99 
100 typedef std::shared_ptr<CameraImage> CameraImagePtr;
101 
102 struct buffer {
103  void* start;
104  size_t length;
105 };
106 
107 class UsbCam {
108  public:
109  UsbCam();
110  virtual ~UsbCam();
111 
112  virtual bool init(const std::shared_ptr<Config>& camera_config);
113  // user use this function to get camera frame data
114  virtual bool poll(const CameraImagePtr& raw_image);
115 
116  bool is_capturing();
117  bool wait_for_device(void);
118 
119  private:
120  int xioctl(int fd, int request, void* arg);
121  bool init_device(void);
122  bool uninit_device(void);
123 
124  void set_device_config();
125  // enables/disable auto focus
126  void set_auto_focus(int value);
127  // set video device parameters
128  void set_v4l_parameter(const std::string& param, int value);
129  void set_v4l_parameter(const std::string& param, const std::string& value);
130 
131  int init_mjpeg_decoder(int image_width, int image_height);
132  void mjpeg2rgb(char* mjepg_buffer, int len, char* rgb_buffer, int pixels);
133 
134 #ifdef __aarch64__
135  int convert_yuv_to_rgb_pixel(int y, int u, int v);
136  int convert_yuv_to_rgb_buffer(unsigned char* yuv, unsigned char* rgb,
137  unsigned int width, unsigned int height);
138 #endif
139 
140  bool init_read(unsigned int buffer_size);
141  bool init_mmap(void);
142  bool init_userp(unsigned int buffer_size);
143  bool set_adv_trigger(void);
144  bool close_device(void);
145  bool open_device(void);
146  bool read_frame(CameraImagePtr raw_image);
147  bool process_image(void* src, int len, CameraImagePtr dest);
148  bool start_capturing(void);
149  bool stop_capturing(void);
150  void reconnect();
151  void reset_device();
152 
153  std::shared_ptr<Config> config_;
154  int pixel_format_;
155  int fd_;
156  buffer* buffers_;
157  unsigned int n_buffers_;
158  bool is_capturing_;
159  uint64_t image_seq_;
160 
161  AVFrame* avframe_camera_;
162  AVFrame* avframe_rgb_;
163  AVCodec* avcodec_;
164  AVDictionary* avoptions_;
165  AVCodecContext* avcodec_context_;
166  int avframe_camera_size_;
167  int avframe_rgb_size_;
168  struct SwsContext* video_sws_;
169 
170  float frame_warning_interval_ = 0.0;
171  float device_wait_sec_ = 0.0;
172  uint64_t last_nsec_ = 0;
173  float frame_drop_interval_ = 0.0;
174 };
175 } // namespace camera
176 } // namespace drivers
177 } // namespace apollo
int is_new
Definition: usb_cam.h:87
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
int tv_sec
Definition: usb_cam.h:88
int height
Definition: usb_cam.h:84
std::shared_ptr< CameraImage > CameraImagePtr
Definition: usb_cam.h:100
~CameraImage()
Definition: usb_cam.h:92
size_t length
Definition: usb_cam.h:104
Definition: usb_cam.h:82
void * start
Definition: usb_cam.h:103
int width
Definition: usb_cam.h:83
Definition: usb_cam.h:102
apollo::cyber::base::std value
Definition: usb_cam.h:107
int image_size
Definition: usb_cam.h:86
int bytes_per_pixel
Definition: usb_cam.h:85
int tv_usec
Definition: usb_cam.h:89
char * image
Definition: usb_cam.h:90