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