|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright 2020 Sony Semiconductor Solutions Corporation |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <string.h> |
| 28 | +#include <fcntl.h> |
| 29 | + |
| 30 | +#include <arch/board/board.h> |
| 31 | +#include <nuttx/video/video.h> |
| 32 | +#include <nuttx/arch.h> |
| 33 | + |
| 34 | +#include "py/runtime.h" |
| 35 | + |
| 36 | +#include "shared-bindings/camera/Camera.h" |
| 37 | + |
| 38 | +#define JPG_COMPRESS_RATIO (9) |
| 39 | +#define SPRESENSE_CAMIMAGE_MEM_ALIGN (32) |
| 40 | + |
| 41 | +typedef struct { |
| 42 | + const char* devpath; |
| 43 | + int fd; |
| 44 | +} camera_dev_t; |
| 45 | + |
| 46 | +STATIC camera_dev_t camera_dev = {"/dev/video", -1}; |
| 47 | + |
| 48 | +static void camera_size_to_width_and_height(camera_imagesize_t size, uint16_t *width, uint16_t *height) { |
| 49 | + switch (size) { |
| 50 | + case IMAGESIZE_320x240: |
| 51 | + *height = VIDEO_VSIZE_QVGA; |
| 52 | + *width = VIDEO_HSIZE_QVGA; |
| 53 | + break; |
| 54 | + case IMAGESIZE_640x320: |
| 55 | + *height = VIDEO_VSIZE_VGA; |
| 56 | + *width = VIDEO_HSIZE_VGA; |
| 57 | + break; |
| 58 | + case IMAGESIZE_1280x720: |
| 59 | + *height = VIDEO_VSIZE_HD; |
| 60 | + *width = VIDEO_HSIZE_HD; |
| 61 | + break; |
| 62 | + case IMAGESIZE_1280x960: |
| 63 | + *height = VIDEO_VSIZE_QUADVGA; |
| 64 | + *width = VIDEO_HSIZE_QUADVGA; |
| 65 | + break; |
| 66 | + case IMAGESIZE_1920x1080: |
| 67 | + *height = VIDEO_VSIZE_FULLHD; |
| 68 | + *width = VIDEO_HSIZE_FULLHD; |
| 69 | + break; |
| 70 | + case IMAGESIZE_2048x1536: |
| 71 | + *height = VIDEO_VSIZE_3M; |
| 72 | + *width = VIDEO_HSIZE_3M; |
| 73 | + break; |
| 74 | + case IMAGESIZE_2560x1920: |
| 75 | + *height = VIDEO_VSIZE_5M; |
| 76 | + *width = VIDEO_HSIZE_5M; |
| 77 | + break; |
| 78 | + default: |
| 79 | + mp_raise_ValueError(translate("Size not supported")); |
| 80 | + break; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +static void camera_set_format(enum v4l2_buf_type type, uint32_t pixformat, uint16_t width, uint16_t height) { |
| 85 | + v4l2_requestbuffers_t req = {0}; |
| 86 | + |
| 87 | + // Set Buffer Mode. |
| 88 | + req.type = type; |
| 89 | + req.memory = V4L2_MEMORY_USERPTR; |
| 90 | + req.count = 1; |
| 91 | + req.mode = V4L2_BUF_MODE_RING; |
| 92 | + ioctl(camera_dev.fd, VIDIOC_REQBUFS, (unsigned long)&req); |
| 93 | + v4l2_format_t fmt = {0}; |
| 94 | + |
| 95 | + // Set Format. |
| 96 | + fmt.type = type; |
| 97 | + fmt.fmt.pix.width = width; |
| 98 | + fmt.fmt.pix.height = height; |
| 99 | + fmt.fmt.pix.field = V4L2_FIELD_ANY; |
| 100 | + fmt.fmt.pix.pixelformat = pixformat; |
| 101 | + ioctl(camera_dev.fd, VIDIOC_S_FMT, (unsigned long)&fmt); |
| 102 | +} |
| 103 | + |
| 104 | +static void camera_start_streaming(enum v4l2_buf_type type) { |
| 105 | + ioctl(camera_dev.fd, VIDIOC_STREAMON, (unsigned long)&type); |
| 106 | +} |
| 107 | + |
| 108 | +static void camera_start_preview() { |
| 109 | + camera_set_format(V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_PIX_FMT_UYVY, VIDEO_HSIZE_QVGA, VIDEO_VSIZE_QVGA); |
| 110 | + |
| 111 | + v4l2_buffer_t buf; |
| 112 | + |
| 113 | + memset(&buf, 0, sizeof(v4l2_buffer_t)); |
| 114 | + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 115 | + buf.memory = V4L2_MEMORY_USERPTR; |
| 116 | + ioctl(camera_dev.fd, VIDIOC_QBUF, (unsigned long)&buf); |
| 117 | + |
| 118 | + camera_start_streaming(V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 119 | +} |
| 120 | + |
| 121 | +void common_hal_camera_construct(camera_obj_t *self, camera_imagesize_t size) { |
| 122 | + if (camera_dev.fd < 0) { |
| 123 | + if (video_initialize(camera_dev.devpath) < 0) { |
| 124 | + mp_raise_ValueError(translate("Could not initialize Camera")); |
| 125 | + } |
| 126 | + camera_dev.fd = open(camera_dev.devpath, 0); |
| 127 | + if (camera_dev.fd < 0) { |
| 128 | + mp_raise_ValueError(translate("Could not initialize Camera")); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + uint16_t width, height; |
| 133 | + |
| 134 | + camera_size_to_width_and_height(size, &width, &height); |
| 135 | + |
| 136 | + self->size = size; |
| 137 | + |
| 138 | + // In SPRESENSE SDK, JPEG compression quality=80 by default. |
| 139 | + // In such setting, the maximum actual measured size of JPEG image |
| 140 | + // is about width * height * 2 / 9. |
| 141 | + self->buffer_size = (size_t)(width * height * 2 / JPG_COMPRESS_RATIO);; |
| 142 | + self->buffer = m_malloc(self->buffer_size, true); |
| 143 | + if (self->buffer == NULL) { |
| 144 | + mp_raise_msg(&mp_type_MemoryError, translate("Couldn't allocate picture buffer")); |
| 145 | + } |
| 146 | + self->picture_buffer = self->buffer; |
| 147 | + while ((uint32_t)self->picture_buffer % SPRESENSE_CAMIMAGE_MEM_ALIGN != 0) { |
| 148 | + self->picture_buffer++; |
| 149 | + } |
| 150 | + |
| 151 | + camera_start_preview(); |
| 152 | + |
| 153 | + camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, width, height); |
| 154 | + |
| 155 | + camera_start_streaming(V4L2_BUF_TYPE_STILL_CAPTURE); |
| 156 | + |
| 157 | + sleep(1); |
| 158 | +} |
| 159 | + |
| 160 | +void common_hal_camera_deinit(camera_obj_t *self) { |
| 161 | + if (common_hal_camera_deinited(self)) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + video_uninitialize(); |
| 166 | + |
| 167 | + m_free(self->buffer); |
| 168 | + |
| 169 | + close(camera_dev.fd); |
| 170 | + camera_dev.fd = -1; |
| 171 | +} |
| 172 | + |
| 173 | +bool common_hal_camera_deinited(camera_obj_t *self) { |
| 174 | + return camera_dev.fd < 0; |
| 175 | +} |
| 176 | + |
| 177 | +void common_hal_camera_take_picture(camera_obj_t *self) { |
| 178 | + v4l2_buffer_t buf; |
| 179 | + |
| 180 | + memset(&buf, 0, sizeof(v4l2_buffer_t)); |
| 181 | + buf.type = V4L2_BUF_TYPE_STILL_CAPTURE; |
| 182 | + buf.memory = V4L2_MEMORY_USERPTR; |
| 183 | + buf.m.userptr = (unsigned long)self->picture_buffer; |
| 184 | + buf.length = self->buffer_size - SPRESENSE_CAMIMAGE_MEM_ALIGN; |
| 185 | + ioctl(camera_dev.fd, VIDIOC_QBUF, (unsigned long)&buf); |
| 186 | + |
| 187 | + ioctl(camera_dev.fd, VIDIOC_TAKEPICT_START, 0); |
| 188 | + |
| 189 | + ioctl(camera_dev.fd, VIDIOC_DQBUF, (unsigned long)&buf); |
| 190 | + |
| 191 | + ioctl(camera_dev.fd, VIDIOC_TAKEPICT_STOP, false); |
| 192 | + |
| 193 | + self->picture_size = (size_t)buf.bytesused; |
| 194 | +} |
| 195 | + |
| 196 | +uint8_t *common_hal_camera_get_picture_buffer(camera_obj_t *self) { |
| 197 | + return self->picture_buffer; |
| 198 | +} |
| 199 | + |
| 200 | +size_t common_hal_camera_get_picture_size(camera_obj_t *self) { |
| 201 | + return self->picture_size; |
| 202 | +} |
| 203 | + |
| 204 | +camera_imagesize_t common_hal_camera_get_size(camera_obj_t *self) { |
| 205 | + return self->size; |
| 206 | +} |
| 207 | + |
| 208 | +void common_hal_camera_set_size(camera_obj_t *self, camera_imagesize_t size) { |
| 209 | + uint16_t width, height; |
| 210 | + |
| 211 | + camera_size_to_width_and_height(size, &width, &height); |
| 212 | + |
| 213 | + self->buffer_size = (size_t)(width * height * 2 / JPG_COMPRESS_RATIO);; |
| 214 | + self->buffer = m_realloc(self->buffer, self->buffer_size); |
| 215 | + if (self->buffer == NULL) { |
| 216 | + mp_raise_msg(&mp_type_MemoryError, translate("Couldn't allocate picture buffer")); |
| 217 | + } |
| 218 | + self->picture_buffer = self->buffer; |
| 219 | + while ((uint32_t)self->picture_buffer % SPRESENSE_CAMIMAGE_MEM_ALIGN != 0) { |
| 220 | + self->picture_buffer++; |
| 221 | + } |
| 222 | + |
| 223 | + camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, width, height); |
| 224 | +} |
0 commit comments