Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 1 | #include "aos/vision/image/reader.h" |
| 2 | |
| 3 | #include <errno.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <malloc.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/mman.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <sys/time.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <unistd.h> |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 14 | |
| 15 | #include "aos/common/logging/logging.h" |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 16 | #include "aos/common/time.h" |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 17 | |
| 18 | #define CLEAR(x) memset(&(x), 0, sizeof(x)) |
| 19 | |
| 20 | namespace camera { |
| 21 | |
| 22 | struct Reader::Buffer { |
| 23 | void *start; |
| 24 | size_t length; // for munmap |
| 25 | }; |
| 26 | |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 27 | aos::vision::CameraParams MakeCameraParams(int32_t width, int32_t height, |
| 28 | int32_t exposure, int32_t brightness, |
| 29 | int32_t gain, int32_t fps) { |
| 30 | aos::vision::CameraParams cam; |
| 31 | cam.set_width(width); |
| 32 | cam.set_height(height); |
| 33 | cam.set_exposure(exposure); |
| 34 | cam.set_brightness(brightness); |
| 35 | cam.set_gain(gain); |
| 36 | cam.set_fps(fps); |
| 37 | return cam; |
| 38 | } |
| 39 | |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 40 | Reader::Reader(const std::string &dev_name, ProcessCb process, |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 41 | aos::vision::CameraParams params) |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 42 | : dev_name_(dev_name), process_(std::move(process)), params_(params) { |
| 43 | struct stat st; |
| 44 | if (stat(dev_name.c_str(), &st) == -1) { |
| 45 | PLOG(FATAL, "Cannot identify '%s'", dev_name.c_str()); |
| 46 | } |
| 47 | if (!S_ISCHR(st.st_mode)) { |
| 48 | PLOG(FATAL, "%s is no device\n", dev_name.c_str()); |
| 49 | } |
| 50 | |
| 51 | fd_ = open(dev_name.c_str(), O_RDWR /* required */ | O_NONBLOCK, 0); |
| 52 | if (fd_ == -1) { |
| 53 | PLOG(FATAL, "Cannot open '%s'", dev_name.c_str()); |
| 54 | } |
| 55 | |
| 56 | Init(); |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 57 | |
| 58 | InitMMap(); |
| 59 | LOG(INFO, "Bat Vision Successfully Initialized.\n"); |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void Reader::QueueBuffer(v4l2_buffer *buf) { |
| 63 | if (xioctl(fd_, VIDIOC_QBUF, buf) == -1) { |
| 64 | PLOG(WARNING, |
| 65 | "ioctl VIDIOC_QBUF(%d, %p)." |
| 66 | " losing buf #%" PRIu32 "\n", |
| 67 | fd_, &buf, buf->index); |
| 68 | } else { |
| 69 | // LOG(DEBUG, "put buf #%" PRIu32 " into driver's queue\n", buf->index); |
| 70 | ++queued_; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void Reader::HandleFrame() { |
| 75 | v4l2_buffer buf; |
| 76 | CLEAR(buf); |
| 77 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 78 | buf.memory = V4L2_MEMORY_MMAP; |
| 79 | |
| 80 | if (xioctl(fd_, VIDIOC_DQBUF, &buf) == -1) { |
| 81 | if (errno != EAGAIN) { |
| 82 | PLOG(ERROR, "ioctl VIDIOC_DQBUF(%d, %p)", fd_, &buf); |
| 83 | } |
| 84 | return; |
| 85 | } |
| 86 | --queued_; |
| 87 | |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 88 | if (tick_id_ % 10 == 0) { |
| 89 | if (!SetCameraControl(V4L2_CID_EXPOSURE_AUTO, "V4L2_CID_EXPOSURE_AUTO", |
| 90 | V4L2_EXPOSURE_MANUAL)) { |
| 91 | LOG(FATAL, "Failed to set exposure\n"); |
| 92 | } |
| 93 | |
| 94 | if (!SetCameraControl(V4L2_CID_EXPOSURE_ABSOLUTE, |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 95 | "V4L2_CID_EXPOSURE_ABSOLUTE", params_.exposure())) { |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 96 | LOG(FATAL, "Failed to set exposure\n"); |
| 97 | } |
| 98 | } |
| 99 | ++tick_id_; |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 100 | // Get a timestamp now as proxy for when the image was taken |
| 101 | // TODO(ben): the image should come with a timestamp, parker |
| 102 | // will know how to get it. |
| 103 | auto time = aos::monotonic_clock::now(); |
| 104 | |
| 105 | process_(aos::vision::DataRef( |
| 106 | reinterpret_cast<const char *>(buffers_[buf.index].start), |
| 107 | buf.bytesused), |
| 108 | time); |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 109 | |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 110 | QueueBuffer(&buf); |
| 111 | } |
| 112 | |
| 113 | void Reader::MMapBuffers() { |
| 114 | buffers_ = new Buffer[kNumBuffers]; |
| 115 | v4l2_buffer buf; |
| 116 | for (unsigned int n = 0; n < kNumBuffers; ++n) { |
| 117 | memset(&buf, 0, sizeof(buf)); |
| 118 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 119 | buf.memory = V4L2_MEMORY_MMAP; |
| 120 | buf.index = n; |
| 121 | if (xioctl(fd_, VIDIOC_QUERYBUF, &buf) == -1) { |
| 122 | PLOG(FATAL, "ioctl VIDIOC_QUERYBUF(%d, %p)", fd_, &buf); |
| 123 | } |
| 124 | buffers_[n].length = buf.length; |
| 125 | buffers_[n].start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, |
| 126 | MAP_SHARED, fd_, buf.m.offset); |
| 127 | if (buffers_[n].start == MAP_FAILED) { |
| 128 | PLOG(FATAL, |
| 129 | "mmap(NULL, %zd, PROT_READ | PROT_WRITE, MAP_SHARED, %d, %jd)", |
| 130 | (size_t)buf.length, fd_, static_cast<intmax_t>(buf.m.offset)); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void Reader::InitMMap() { |
| 136 | v4l2_requestbuffers req; |
| 137 | CLEAR(req); |
| 138 | req.count = kNumBuffers; |
| 139 | req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 140 | req.memory = V4L2_MEMORY_MMAP; |
| 141 | if (xioctl(fd_, VIDIOC_REQBUFS, &req) == -1) { |
| 142 | if (EINVAL == errno) { |
| 143 | LOG(FATAL, "%s does not support memory mapping\n", dev_name_.c_str()); |
| 144 | } else { |
| 145 | PLOG(FATAL, "ioctl VIDIOC_REQBUFS(%d, %p)\n", fd_, &req); |
| 146 | } |
| 147 | } |
| 148 | queued_ = kNumBuffers; |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 149 | if (req.count != kNumBuffers) { |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 150 | LOG(FATAL, "Insufficient buffer memory on %s\n", dev_name_.c_str()); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Sets one of the camera's user-control values. |
| 155 | // Prints the old and new values. |
| 156 | // Just prints a message if the camera doesn't support this control or value. |
| 157 | bool Reader::SetCameraControl(uint32_t id, const char *name, int value) { |
| 158 | struct v4l2_control getArg = {id, 0U}; |
| 159 | int r = xioctl(fd_, VIDIOC_G_CTRL, &getArg); |
| 160 | if (r == 0) { |
| 161 | if (getArg.value == value) { |
| 162 | LOG(DEBUG, "Camera control %s was already %d\n", name, getArg.value); |
| 163 | return true; |
| 164 | } |
| 165 | } else if (errno == EINVAL) { |
| 166 | LOG(DEBUG, "Camera control %s is invalid\n", name); |
| 167 | errno = 0; |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | struct v4l2_control setArg = {id, value}; |
| 172 | r = xioctl(fd_, VIDIOC_S_CTRL, &setArg); |
| 173 | if (r == 0) { |
| 174 | LOG(DEBUG, "Set camera control %s from %d to %d\n", name, getArg.value, |
| 175 | value); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | LOG(DEBUG, "Couldn't set camera control %s to %d", name, value); |
| 180 | errno = 0; |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | void Reader::Init() { |
| 185 | v4l2_capability cap; |
| 186 | if (xioctl(fd_, VIDIOC_QUERYCAP, &cap) == -1) { |
| 187 | if (EINVAL == errno) { |
| 188 | LOG(FATAL, "%s is no V4L2 device\n", dev_name_.c_str()); |
| 189 | } else { |
| 190 | PLOG(FATAL, "ioctl VIDIOC_QUERYCAP(%d, %p)", fd_, &cap); |
| 191 | } |
| 192 | } |
| 193 | if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { |
| 194 | LOG(FATAL, "%s is no video capture device\n", dev_name_.c_str()); |
| 195 | } |
| 196 | if (!(cap.capabilities & V4L2_CAP_STREAMING)) { |
| 197 | LOG(FATAL, "%s does not support streaming i/o\n", dev_name_.c_str()); |
| 198 | } |
| 199 | |
| 200 | /* Select video input, video standard and tune here. */ |
| 201 | |
| 202 | v4l2_cropcap cropcap; |
| 203 | CLEAR(cropcap); |
| 204 | cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 205 | if (xioctl(fd_, VIDIOC_CROPCAP, &cropcap) == 0) { |
| 206 | v4l2_crop crop; |
| 207 | crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 208 | crop.c = cropcap.defrect; /* reset to default */ |
| 209 | |
| 210 | if (xioctl(fd_, VIDIOC_S_CROP, &crop) == -1) { |
| 211 | switch (errno) { |
| 212 | case EINVAL: |
| 213 | /* Cropping not supported. */ |
| 214 | break; |
| 215 | default: |
| 216 | /* Errors ignored. */ |
| 217 | PLOG(WARNING, "xioctl VIDIOC_S_CROP"); |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | } else { |
| 222 | PLOG(WARNING, "xioctl VIDIOC_CROPCAP"); |
| 223 | } |
| 224 | |
| 225 | v4l2_format fmt; |
| 226 | CLEAR(fmt); |
| 227 | fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 228 | fmt.fmt.pix.width = params_.width(); |
| 229 | fmt.fmt.pix.height = params_.height(); |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 230 | fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG; |
| 231 | fmt.fmt.pix.field = V4L2_FIELD_ANY; |
| 232 | // fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
| 233 | // fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; |
| 234 | if (xioctl(fd_, VIDIOC_S_FMT, &fmt) == -1) { |
| 235 | LOG(FATAL, "ioctl VIDIC_S_FMT(%d, %p) failed with %d: %s\n", fd_, &fmt, |
| 236 | errno, strerror(errno)); |
| 237 | } |
| 238 | /* Note VIDIOC_S_FMT may change width and height. */ |
| 239 | |
| 240 | /* Buggy driver paranoia. */ |
| 241 | unsigned int min = fmt.fmt.pix.width * 2; |
| 242 | if (fmt.fmt.pix.bytesperline < min) fmt.fmt.pix.bytesperline = min; |
| 243 | min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height; |
| 244 | if (fmt.fmt.pix.sizeimage < min) fmt.fmt.pix.sizeimage = min; |
| 245 | |
| 246 | if (!SetCameraControl(V4L2_CID_EXPOSURE_AUTO, "V4L2_CID_EXPOSURE_AUTO", |
| 247 | V4L2_EXPOSURE_MANUAL)) { |
| 248 | LOG(FATAL, "Failed to set exposure\n"); |
| 249 | } |
| 250 | |
| 251 | if (!SetCameraControl(V4L2_CID_EXPOSURE_ABSOLUTE, |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 252 | "V4L2_CID_EXPOSURE_ABSOLUTE", params_.exposure())) { |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 253 | LOG(FATAL, "Failed to set exposure\n"); |
| 254 | } |
| 255 | |
| 256 | if (!SetCameraControl(V4L2_CID_BRIGHTNESS, "V4L2_CID_BRIGHTNESS", |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 257 | params_.brightness())) { |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 258 | LOG(FATAL, "Failed to set up camera\n"); |
| 259 | } |
| 260 | |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 261 | if (!SetCameraControl(V4L2_CID_GAIN, "V4L2_CID_GAIN", params_.gain())) { |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 262 | LOG(FATAL, "Failed to set up camera\n"); |
| 263 | } |
| 264 | |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 265 | // set framerate |
| 266 | struct v4l2_streamparm *setfps; |
| 267 | setfps = (struct v4l2_streamparm *)calloc(1, sizeof(struct v4l2_streamparm)); |
| 268 | memset(setfps, 0, sizeof(struct v4l2_streamparm)); |
| 269 | setfps->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 270 | setfps->parm.capture.timeperframe.numerator = 1; |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 271 | setfps->parm.capture.timeperframe.denominator = params_.fps(); |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 272 | if (xioctl(fd_, VIDIOC_S_PARM, setfps) == -1) { |
| 273 | PLOG(FATAL, "ioctl VIDIOC_S_PARM(%d, %p)\n", fd_, setfps); |
| 274 | } |
| 275 | LOG(INFO, "framerate ended up at %d/%d\n", |
| 276 | setfps->parm.capture.timeperframe.numerator, |
| 277 | setfps->parm.capture.timeperframe.denominator); |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | aos::vision::ImageFormat Reader::get_format() { |
| 281 | struct v4l2_format fmt; |
| 282 | fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 283 | if (xioctl(fd_, VIDIOC_G_FMT, &fmt) == -1) { |
| 284 | PLOG(FATAL, "ioctl VIDIC_G_FMT(%d, %p)\n", fd_, &fmt); |
| 285 | } |
| 286 | |
| 287 | return aos::vision::ImageFormat{(int)fmt.fmt.pix.width, |
| 288 | (int)fmt.fmt.pix.height}; |
| 289 | } |
| 290 | |
| 291 | void Reader::Start() { |
| 292 | LOG(DEBUG, "queueing buffers for the first time\n"); |
| 293 | v4l2_buffer buf; |
| 294 | for (unsigned int i = 0; i < kNumBuffers; ++i) { |
| 295 | CLEAR(buf); |
| 296 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 297 | buf.memory = V4L2_MEMORY_MMAP; |
| 298 | buf.index = i; |
| 299 | QueueBuffer(&buf); |
| 300 | } |
| 301 | LOG(DEBUG, "done with first queue\n"); |
| 302 | |
| 303 | v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 304 | if (xioctl(fd_, VIDIOC_STREAMON, &type) == -1) { |
| 305 | PLOG(FATAL, "ioctl VIDIOC_STREAMON(%d, %p)\n", fd_, &type); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | } // namespace camera |