Apollo 10.0
自动驾驶开放平台
usb_cam.h
浏览该文件的文档.
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
49extern "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
69namespace apollo {
70namespace drivers {
71namespace camera {
72
80
81// camera raw image struct
83 int width;
84 int height;
87 int is_new;
88 int tv_sec;
90 char* image;
91
93 if (image != nullptr) {
94 free(reinterpret_cast<void*>(image));
95 image = nullptr;
96 }
97 }
98};
99
100typedef std::shared_ptr<CameraImage> CameraImagePtr;
101
102struct buffer {
103 void* start;
104 size_t length;
105};
106
107class 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 const CameraImagePtr& sensor_raw_image);
116
117 bool is_capturing();
118 bool wait_for_device(void);
119
120 private:
121 int xioctl(int fd, int request, void* arg);
122 bool init_device(void);
123 bool uninit_device(void);
124
125 void set_device_config();
126 // enables/disable auto focus
127 void set_auto_focus(int value);
128 // set video device parameters
129 void set_v4l_parameter(const std::string& param, int value);
130 void set_v4l_parameter(const std::string& param, const std::string& value);
131
132 int init_mjpeg_decoder(int image_width, int image_height);
133 void mjpeg2rgb(char* mjepg_buffer, int len, char* rgb_buffer, int pixels);
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, CameraImagePtr sensor_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
virtual bool init(const std::shared_ptr< Config > &camera_config)
Definition usb_cam.cc:71
virtual bool poll(const CameraImagePtr &raw_image, const CameraImagePtr &sensor_raw_image)
Definition usb_cam.cc:231
std::shared_ptr< CameraImage > CameraImagePtr
Definition usb_cam.h:100
class register implement
Definition arena_queue.h:37