circuitpython/ports/cxd56/common-hal/camera/Camera.c

212 lines
6.6 KiB
C
Raw Normal View History

2020-09-02 09:43:37 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright 2020 Sony Semiconductor Solutions Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include <fcntl.h>
#include <arch/board/board.h>
#include <nuttx/video/video.h>
#include <nuttx/arch.h>
#include "py/runtime.h"
#include "shared-bindings/camera/Camera.h"
typedef struct {
2021-03-15 09:57:36 -04:00
const char *devpath;
2020-09-02 09:43:37 -04:00
int fd;
} camera_dev_t;
STATIC camera_dev_t camera_dev = {"/dev/video", -1};
2020-09-14 06:55:53 -04:00
typedef struct {
uint16_t width;
uint16_t height;
} image_size_t;
2023-02-01 06:37:29 -05:00
STATIC const image_size_t isx012_image_size_table[] = {
2020-09-14 06:55:53 -04:00
{ VIDEO_HSIZE_QVGA, VIDEO_VSIZE_QVGA },
{ VIDEO_HSIZE_VGA, VIDEO_VSIZE_VGA },
{ VIDEO_HSIZE_HD, VIDEO_VSIZE_HD },
{ VIDEO_HSIZE_QUADVGA, VIDEO_VSIZE_QUADVGA },
{ VIDEO_HSIZE_FULLHD, VIDEO_VSIZE_FULLHD },
{ VIDEO_HSIZE_3M, VIDEO_VSIZE_3M },
{ VIDEO_HSIZE_5M, VIDEO_VSIZE_5M },
};
2023-02-01 06:37:29 -05:00
STATIC const image_size_t isx019_image_size_table[] = {
{ VIDEO_HSIZE_QVGA, VIDEO_VSIZE_QVGA },
{ VIDEO_HSIZE_VGA, VIDEO_VSIZE_VGA },
{ VIDEO_HSIZE_HD, VIDEO_VSIZE_HD },
{ VIDEO_HSIZE_QUADVGA, VIDEO_VSIZE_QUADVGA },
};
static const char *get_imgsensor_name() {
static struct v4l2_capability cap;
ioctl(camera_dev.fd, VIDIOC_QUERYCAP, (unsigned long)&cap);
return (const char *)cap.driver;
}
2020-09-11 07:27:11 -04:00
static bool camera_check_width_and_height(uint16_t width, uint16_t height) {
2023-02-01 06:37:29 -05:00
const char *sensor;
sensor = get_imgsensor_name();
if (strncmp(sensor, "ISX012", strlen("ISX012")) == 0) {
for (int i = 0; i < MP_ARRAY_SIZE(isx012_image_size_table); i++) {
if (isx012_image_size_table[i].width == width && isx012_image_size_table[i].height == height) {
return true;
}
}
} else if (strncmp(sensor, "ISX019", strlen("ISX019"))) {
for (int i = 0; i < MP_ARRAY_SIZE(isx019_image_size_table); i++) {
if (isx019_image_size_table[i].width == width && isx019_image_size_table[i].height == height) {
return true;
}
2020-09-14 06:55:53 -04:00
}
2020-09-11 07:27:11 -04:00
}
2023-02-01 06:37:29 -05:00
2020-09-14 06:55:53 -04:00
return false;
2020-09-11 07:27:11 -04:00
}
static bool camera_check_buffer_length(uint16_t width, uint16_t height, camera_imageformat_t format, size_t length) {
if (format == IMAGEFORMAT_JPG) {
// In SPRESENSE SDK, JPEG compression quality=80 by default.
// In such setting, the maximum actual measured size of JPEG image
// is about width * height * 2 / 9.
2020-09-14 06:55:53 -04:00
return length >= (size_t)(width * height * 2 / 9);
2020-09-11 07:27:11 -04:00
} else {
return false;
}
}
static bool camera_check_format(camera_imageformat_t format) {
2020-09-14 06:55:53 -04:00
return format == IMAGEFORMAT_JPG;
2020-09-02 09:43:37 -04:00
}
static void camera_set_format(enum v4l2_buf_type type, uint32_t pixformat, uint16_t width, uint16_t height) {
v4l2_requestbuffers_t req = {0};
// Set Buffer Mode.
req.type = type;
req.memory = V4L2_MEMORY_USERPTR;
req.count = 1;
req.mode = V4L2_BUF_MODE_RING;
ioctl(camera_dev.fd, VIDIOC_REQBUFS, (unsigned long)&req);
v4l2_format_t fmt = {0};
// Set Format.
fmt.type = type;
fmt.fmt.pix.width = width;
fmt.fmt.pix.height = height;
fmt.fmt.pix.field = V4L2_FIELD_ANY;
fmt.fmt.pix.pixelformat = pixformat;
ioctl(camera_dev.fd, VIDIOC_S_FMT, (unsigned long)&fmt);
}
static void camera_start_streaming(enum v4l2_buf_type type) {
ioctl(camera_dev.fd, VIDIOC_STREAMON, (unsigned long)&type);
}
static void camera_start_preview() {
camera_set_format(V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_PIX_FMT_UYVY, VIDEO_HSIZE_QVGA, VIDEO_VSIZE_QVGA);
v4l2_buffer_t buf;
memset(&buf, 0, sizeof(v4l2_buffer_t));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_USERPTR;
ioctl(camera_dev.fd, VIDIOC_QBUF, (unsigned long)&buf);
camera_start_streaming(V4L2_BUF_TYPE_VIDEO_CAPTURE);
}
void common_hal_camera_construct(camera_obj_t *self) {
2020-09-02 09:43:37 -04:00
if (camera_dev.fd < 0) {
if (video_initialize(camera_dev.devpath) < 0) {
mp_raise_RuntimeError(translate("Camera init"));
2020-09-02 09:43:37 -04:00
}
camera_dev.fd = open(camera_dev.devpath, 0);
if (camera_dev.fd < 0) {
mp_raise_RuntimeError(translate("Camera init"));
2020-09-02 09:43:37 -04:00
}
}
camera_start_preview();
camera_start_streaming(V4L2_BUF_TYPE_STILL_CAPTURE);
sleep(1);
}
void common_hal_camera_deinit(camera_obj_t *self) {
if (common_hal_camera_deinited(self)) {
return;
}
video_uninitialize();
close(camera_dev.fd);
camera_dev.fd = -1;
}
bool common_hal_camera_deinited(camera_obj_t *self) {
return camera_dev.fd < 0;
}
size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_t len, uint16_t width, uint16_t height, camera_imageformat_t format) {
if (!camera_check_width_and_height(width, height)) {
2020-09-11 07:27:11 -04:00
mp_raise_ValueError(translate("Size not supported"));
}
if (!camera_check_buffer_length(width, height, format, len)) {
2020-09-11 07:27:11 -04:00
mp_raise_ValueError(translate("Buffer is too small"));
}
if (!camera_check_format(format)) {
mp_raise_ValueError(translate("Format not supported"));
}
camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, width, height);
2020-09-11 07:27:11 -04:00
2020-09-02 09:43:37 -04:00
v4l2_buffer_t buf;
memset(&buf, 0, sizeof(v4l2_buffer_t));
buf.type = V4L2_BUF_TYPE_STILL_CAPTURE;
buf.memory = V4L2_MEMORY_USERPTR;
2020-09-11 07:27:11 -04:00
buf.m.userptr = (unsigned long)buffer;
buf.length = len;
2020-09-02 09:43:37 -04:00
ioctl(camera_dev.fd, VIDIOC_QBUF, (unsigned long)&buf);
ioctl(camera_dev.fd, VIDIOC_TAKEPICT_START, 0);
ioctl(camera_dev.fd, VIDIOC_DQBUF, (unsigned long)&buf);
ioctl(camera_dev.fd, VIDIOC_TAKEPICT_STOP, false);
2020-09-11 07:27:11 -04:00
return (size_t)buf.bytesused;
2020-09-02 09:43:37 -04:00
}