|
@@ -82,7 +82,7 @@ load_entity_ids(libmegapixels_camera *camera)
|
|
|
log_error("Could not find entity '%s'\n", cmd->entity_to);
|
|
|
return -1;
|
|
|
}
|
|
|
- } else if (cmd->type == LIBMEGAPIXELS_CMD_MODE) {
|
|
|
+ } else if (cmd->type == LIBMEGAPIXELS_CMD_MODE || cmd->type == LIBMEGAPIXELS_CMD_CROP) {
|
|
|
int found = 0;
|
|
|
for (int k = 0; k < topology.num_entities; k++) {
|
|
|
if (strncmp(entities[k].name, cmd->entity_from, strlen(cmd->entity_from)) == 0) {
|
|
@@ -201,7 +201,7 @@ libmegapixels_close(libmegapixels_camera *camera)
|
|
|
}
|
|
|
|
|
|
unsigned int
|
|
|
-libmegapixels_select_mode(libmegapixels_camera *camera, libmegapixels_mode *mode)
|
|
|
+libmegapixels_select_mode(libmegapixels_camera *camera, libmegapixels_mode *mode, struct v4l2_format *format)
|
|
|
{
|
|
|
assert(camera->video_fd != 0);
|
|
|
assert(camera->sensor_fd != 0);
|
|
@@ -211,9 +211,13 @@ libmegapixels_select_mode(libmegapixels_camera *camera, libmegapixels_mode *mode
|
|
|
for (int i = 0; i < mode->num_cmds; i++) {
|
|
|
libmegapixels_cmd *cmd = mode->cmds[i];
|
|
|
struct v4l2_subdev_format subdev_fmt = {};
|
|
|
+ struct v4l2_subdev_crop subdev_crop = {};
|
|
|
+ int found = 0;
|
|
|
+ libmegapixels_subdev *sd;
|
|
|
+
|
|
|
switch (cmd->type) {
|
|
|
case LIBMEGAPIXELS_CMD_LINK:
|
|
|
- log_debug(" Link %s -> %s\n", cmd->entity_from, cmd->entity_to);
|
|
|
+ log_debug(" Link %s:%d -> %s:%d\n", cmd->entity_from, cmd->pad_from, cmd->entity_to, cmd->pad_to);
|
|
|
if (setup_link(camera, cmd->entity_from_id, cmd->entity_to_id, cmd->pad_from, cmd->pad_to, 1) != 0) {
|
|
|
log_error("Could not link %d -> %d [%s -> %s] \n", cmd->entity_from_id, cmd->entity_to_id,
|
|
|
cmd->entity_from,
|
|
@@ -221,17 +225,16 @@ libmegapixels_select_mode(libmegapixels_camera *camera, libmegapixels_mode *mode
|
|
|
}
|
|
|
break;
|
|
|
case LIBMEGAPIXELS_CMD_MODE:
|
|
|
- subdev_fmt.pad = cmd->pad_to;
|
|
|
+ subdev_fmt.pad = cmd->pad_from;
|
|
|
subdev_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
|
|
|
subdev_fmt.format.width = cmd->width;
|
|
|
subdev_fmt.format.height = cmd->height;
|
|
|
- subdev_fmt.format.code = mode->media_busfmt;
|
|
|
+ subdev_fmt.format.code = libmegapixels_format_to_media_busfmt(cmd->format);
|
|
|
subdev_fmt.format.field = V4L2_FIELD_ANY;
|
|
|
+ log_debug(" Mode %s:%d [%dx%d %s]\n", cmd->entity_from, cmd->pad_from, cmd->width, cmd->height,
|
|
|
+ libmegapixels_format_name(cmd->format));
|
|
|
|
|
|
- log_debug(" Mode \n", cmd->entity_from);
|
|
|
-
|
|
|
- libmegapixels_subdev *sd;
|
|
|
- int found = 0;
|
|
|
+ found = 0;
|
|
|
for (int h = 0; h < camera->num_handles; h++) {
|
|
|
if (camera->handles[h]->entity_id == cmd->entity_from_id) {
|
|
|
sd = camera->handles[h];
|
|
@@ -252,25 +255,102 @@ libmegapixels_select_mode(libmegapixels_camera *camera, libmegapixels_mode *mode
|
|
|
}
|
|
|
|
|
|
if (xioctl(sd->fd, VIDIOC_SUBDEV_S_FMT, &subdev_fmt) == -1) {
|
|
|
- log_error("Could not set mode on sensor: %s\n", strerror(errno));
|
|
|
+ log_error("Could not set mode on %s:%d: %s\n", cmd->entity_from, cmd->pad_from, strerror(errno));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case LIBMEGAPIXELS_CMD_CROP:
|
|
|
+ subdev_crop.pad = cmd->pad_from;
|
|
|
+ subdev_crop.which = V4L2_SUBDEV_FORMAT_ACTIVE;
|
|
|
+ subdev_crop.rect.top = cmd->top;
|
|
|
+ subdev_crop.rect.left = cmd->left;
|
|
|
+ subdev_crop.rect.width = cmd->width;
|
|
|
+ subdev_crop.rect.height = cmd->height;
|
|
|
+
|
|
|
+ log_debug(" Crop %s:%d [%dx%d+%d+%d]\n", cmd->entity_from, cmd->pad_from, cmd->width, cmd->height,
|
|
|
+ cmd->top, cmd->left);
|
|
|
+
|
|
|
+ found = 0;
|
|
|
+ for (int h = 0; h < camera->num_handles; h++) {
|
|
|
+ if (camera->handles[h]->entity_id == cmd->entity_from_id) {
|
|
|
+ sd = camera->handles[h];
|
|
|
+ found++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (found != 1) {
|
|
|
+ log_error("Could not find handle for entity\n");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sd->fd == 0) {
|
|
|
+ sd->fd = open(sd->path, O_RDWR);
|
|
|
+ if (sd->fd < 0) {
|
|
|
+ log_error("Could not open %s\n", sd->path);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (xioctl(sd->fd, VIDIOC_SUBDEV_S_CROP, &subdev_crop) == -1) {
|
|
|
+ log_error("Could not set crop on entity: %s\n", strerror(errno));
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- struct v4l2_format format = {0};
|
|
|
- format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
- format.fmt.pix.width = mode->width;
|
|
|
- format.fmt.pix.height = mode->height;
|
|
|
- format.fmt.pix.pixelformat = mode->v4l_pixfmt;
|
|
|
- format.fmt.pix.field = V4L2_FIELD_ANY;
|
|
|
-
|
|
|
- if (xioctl(camera->video_fd, VIDIOC_S_FMT, &format) == -1) {
|
|
|
- log_error("Could not set mode on bridge: %s\n", strerror(errno));
|
|
|
+ struct v4l2_capability cap;
|
|
|
+ if (xioctl(camera->video_fd, VIDIOC_QUERYCAP, &cap) == -1) {
|
|
|
+ log_error("Could not query %s: %s\n", camera->video_path, strerror(errno));
|
|
|
return 0;
|
|
|
}
|
|
|
+ if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
|
|
|
+ format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
|
|
|
+ format->fmt.pix_mp.width = mode->width;
|
|
|
+ format->fmt.pix_mp.height = mode->height;
|
|
|
+ format->fmt.pix_mp.pixelformat = mode->v4l_pixfmt;
|
|
|
+ format->fmt.pix_mp.field = V4L2_FIELD_ANY;
|
|
|
+ } else {
|
|
|
+ format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
+ format->fmt.pix.width = mode->width;
|
|
|
+ format->fmt.pix.height = mode->height;
|
|
|
+ format->fmt.pix.pixelformat = mode->v4l_pixfmt;
|
|
|
+ format->fmt.pix.field = V4L2_FIELD_ANY;
|
|
|
+ }
|
|
|
|
|
|
+ if (xioctl(camera->video_fd, VIDIOC_S_FMT, format) == -1) {
|
|
|
+ log_error("Could not set mode on %s: %s\n", camera->video_path, strerror(errno));
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (xioctl(camera->video_fd, VIDIOC_G_FMT, format) == -1) {
|
|
|
+ log_error("Could not get mode of %s: %s\n", camera->video_path, strerror(errno));
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
|
|
|
+ if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
|
|
|
+ log_error("Capture driver changed capture type\n");
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (format->fmt.pix_mp.pixelformat != mode->v4l_pixfmt) {
|
|
|
+ log_error("Capture driver changed pixfmt to %c%c%c%c\n",
|
|
|
+ format->fmt.pix_mp.pixelformat & 0xff,
|
|
|
+ format->fmt.pix_mp.pixelformat >> 8 & 0xff,
|
|
|
+ format->fmt.pix_mp.pixelformat >> 16 & 0xff,
|
|
|
+ format->fmt.pix_mp.pixelformat >> 24 & 0xff);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
|
|
|
+ log_error("Capture driver changed capture type\n");
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (format->fmt.pix.pixelformat != mode->v4l_pixfmt) {
|
|
|
+ log_error("Capture driver changed pixfmt to %c%c%c%c\n",
|
|
|
+ format->fmt.pix.pixelformat & 0xff,
|
|
|
+ format->fmt.pix.pixelformat >> 8 & 0xff,
|
|
|
+ format->fmt.pix.pixelformat >> 16 & 0xff,
|
|
|
+ format->fmt.pix.pixelformat >> 24 & 0xff);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
camera->current_mode = mode;
|
|
|
|
|
|
- return format.fmt.pix.sizeimage;
|
|
|
+ return 1;
|
|
|
}
|