#include <sys/stat.h>
#include <sys/types.h>
+#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
+#include <stdbool.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/v4l2-subdev.h>
#include "mediactl.h"
-#include "v4l2subdev.h"
#include "tools.h"
+#include "v4l2subdev.h"
int v4l2_subdev_open(struct media_entity *entity)
{
return 0;
}
-int v4l2_subdev_get_crop(struct media_entity *entity, struct v4l2_rect *rect,
- unsigned int pad, enum v4l2_subdev_format_whence which)
+int v4l2_subdev_get_selection(struct media_entity *entity,
+ struct v4l2_rect *rect, unsigned int pad, unsigned int target,
+ enum v4l2_subdev_format_whence which)
{
- struct v4l2_subdev_crop crop;
+ union {
+ struct v4l2_subdev_selection sel;
+ struct v4l2_subdev_crop crop;
+ } u;
int ret;
ret = v4l2_subdev_open(entity);
if (ret < 0)
return ret;
- memset(&crop, 0, sizeof(crop));
- crop.pad = pad;
- crop.which = which;
+ memset(&u.sel, 0, sizeof(u.sel));
+ u.sel.pad = pad;
+ u.sel.target = target;
+ u.sel.which = which;
- ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &crop);
+ ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_SELECTION, &u.sel);
+ if (ret >= 0) {
+ *rect = u.sel.r;
+ return 0;
+ }
+ if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
+ return -errno;
+
+ memset(&u.crop, 0, sizeof(u.crop));
+ u.crop.pad = pad;
+ u.crop.which = which;
+
+ ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &u.crop);
if (ret < 0)
return -errno;
- *rect = crop.rect;
+ *rect = u.crop.rect;
return 0;
}
-int v4l2_subdev_set_crop(struct media_entity *entity, struct v4l2_rect *rect,
- unsigned int pad, enum v4l2_subdev_format_whence which)
+int v4l2_subdev_set_selection(struct media_entity *entity,
+ struct v4l2_rect *rect, unsigned int pad, unsigned int target,
+ enum v4l2_subdev_format_whence which)
{
- struct v4l2_subdev_crop crop;
+ union {
+ struct v4l2_subdev_selection sel;
+ struct v4l2_subdev_crop crop;
+ } u;
int ret;
ret = v4l2_subdev_open(entity);
if (ret < 0)
return ret;
- memset(&crop, 0, sizeof(crop));
- crop.pad = pad;
- crop.which = which;
- crop.rect = *rect;
+ memset(&u.sel, 0, sizeof(u.sel));
+ u.sel.pad = pad;
+ u.sel.target = target;
+ u.sel.which = which;
+ u.sel.r = *rect;
+
+ ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_SELECTION, &u.sel);
+ if (ret >= 0) {
+ *rect = u.sel.r;
+ return 0;
+ }
+ if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
+ return -errno;
+
+ memset(&u.crop, 0, sizeof(u.crop));
+ u.crop.pad = pad;
+ u.crop.which = which;
+ u.crop.rect = *rect;
- ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &crop);
+ ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &u.crop);
if (ret < 0)
return -errno;
- *rect = crop.rect;
+ *rect = u.crop.rect;
return 0;
}
unsigned int width, height;
char *end;
+ /*
+ * Compatibility with the old syntax: consider space as valid
+ * separator between the media bus pixel code and the size.
+ */
for (; isspace(*p); ++p);
- for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
+ for (end = (char *)p;
+ *end != '/' && *end != ' ' && *end != '\0'; ++end);
code = v4l2_subdev_string_to_pixelcode(p, end - p);
if (code == (enum v4l2_mbus_pixelcode)-1)
return -EINVAL;
- for (p = end; isspace(*p); ++p);
+ p = end + 1;
width = strtoul(p, &end, 10);
if (*end != 'x')
return -EINVAL;
return 0;
}
-static int v4l2_subdev_parse_crop(
- struct v4l2_rect *crop, const char *p, char **endp)
+static int v4l2_subdev_parse_rectangle(
+ struct v4l2_rect *r, const char *p, char **endp)
{
char *end;
if (*p++ != '(')
return -EINVAL;
- crop->left = strtoul(p, &end, 10);
+ r->left = strtoul(p, &end, 10);
if (*end != ',')
return -EINVAL;
p = end + 1;
- crop->top = strtoul(p, &end, 10);
+ r->top = strtoul(p, &end, 10);
if (*end++ != ')')
return -EINVAL;
if (*end != '/')
return -EINVAL;
p = end + 1;
- crop->width = strtoul(p, &end, 10);
+ r->width = strtoul(p, &end, 10);
if (*end != 'x')
return -EINVAL;
p = end + 1;
- crop->height = strtoul(p, &end, 10);
+ r->height = strtoul(p, &end, 10);
*endp = end;
return 0;
return 0;
}
+/*
+ * The debate over whether this function should be named icanhasstr() instead
+ * has been strong and heated. If you feel like this would be an important
+ * change, patches are welcome (or not).
+ */
+static bool strhazit(const char *str, const char **p)
+{
+ int len = strlen(str);
+
+ if (strncmp(str, *p, len))
+ return false;
+
+ for (*p += len; isspace(**p); ++*p);
+ return true;
+}
+
static struct media_pad *v4l2_subdev_parse_pad_format(
struct media_device *media, struct v4l2_mbus_framefmt *format,
- struct v4l2_rect *crop, struct v4l2_fract *interval, const char *p,
- char **endp)
+ struct v4l2_rect *crop, struct v4l2_rect *compose,
+ struct v4l2_fract *interval, const char *p, char **endp)
{
struct media_pad *pad;
+ bool first;
char *end;
int ret;
if (*p++ != '[')
return NULL;
- for (; isspace(*p); ++p);
+ for (first = true; ; first = false) {
+ for (; isspace(*p); p++);
- if (isalnum(*p)) {
- ret = v4l2_subdev_parse_format(format, p, &end);
- if (ret < 0)
- return NULL;
+ /*
+ * Backward compatibility: if the first property starts with an
+ * uppercase later, process it as a format description.
+ */
+ if (strhazit("fmt:", &p) || (first && isupper(*p))) {
+ ret = v4l2_subdev_parse_format(format, p, &end);
+ if (ret < 0)
+ return NULL;
- for (p = end; isspace(*p); p++);
- }
+ p = end;
+ continue;
+ }
- if (*p == '(') {
- ret = v4l2_subdev_parse_crop(crop, p, &end);
- if (ret < 0)
- return NULL;
+ /*
+ * Backward compatibility: crop rectangles can be specified
+ * implicitly without the 'crop:' property name.
+ */
+ if (strhazit("crop:", &p) || *p == '(') {
+ ret = v4l2_subdev_parse_rectangle(crop, p, &end);
+ if (ret < 0)
+ return NULL;
+
+ p = end;
+ continue;
+ }
- for (p = end; isspace(*p); p++);
- }
+ if (strhazit("compose:", &p)) {
+ ret = v4l2_subdev_parse_rectangle(compose, p, &end);
+ if (ret < 0)
+ return NULL;
- if (*p == '@') {
- ret = v4l2_subdev_parse_frame_interval(interval, ++p, &end);
- if (ret < 0)
- return NULL;
+ for (p = end; isspace(*p); p++);
+ continue;
+ }
- for (p = end; isspace(*p); p++);
+ if (*p == '@') {
+ ret = v4l2_subdev_parse_frame_interval(interval, ++p, &end);
+ if (ret < 0)
+ return NULL;
+
+ p = end;
+ continue;
+ }
+
+ break;
}
if (*p != ']')
return 0;
}
-static int set_crop(struct media_pad *pad, struct v4l2_rect *crop)
+static int set_selection(struct media_pad *pad, unsigned int target,
+ struct v4l2_rect *rect)
{
int ret;
- if (crop->left == -1 || crop->top == -1)
+ if (rect->left == -1 || rect->top == -1)
return 0;
media_dbg(pad->entity->media,
- "Setting up crop rectangle (%u,%u)/%ux%u on pad %s/%u\n",
- crop->left, crop->top, crop->width, crop->height,
+ "Setting up selection target %u rectangle (%u,%u)/%ux%u on pad %s/%u\n",
+ target, rect->left, rect->top, rect->width, rect->height,
pad->entity->info.name, pad->index);
- ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
- V4L2_SUBDEV_FORMAT_ACTIVE);
+ ret = v4l2_subdev_set_selection(pad->entity, rect, pad->index,
+ target, V4L2_SUBDEV_FORMAT_ACTIVE);
if (ret < 0) {
media_dbg(pad->entity->media,
- "Unable to set crop rectangle: %s (%d)\n",
+ "Unable to set selection rectangle: %s (%d)\n",
strerror(-ret), ret);
return ret;
}
media_dbg(pad->entity->media,
- "Crop rectangle set: (%u,%u)/%ux%u\n",
- crop->left, crop->top, crop->width, crop->height);
+ "Selection rectangle set: (%u,%u)/%ux%u\n",
+ rect->left, rect->top, rect->width, rect->height);
return 0;
}
struct v4l2_mbus_framefmt format = { 0, 0, 0 };
struct media_pad *pad;
struct v4l2_rect crop = { -1, -1, -1, -1 };
+ struct v4l2_rect compose = crop;
struct v4l2_fract interval = { 0, 0 };
unsigned int i;
char *end;
int ret;
- pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &interval,
- p, &end);
+ pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &compose,
+ &interval, p, &end);
if (pad == NULL) {
media_dbg(media, "Unable to parse format\n");
return -EINVAL;
}
- if (pad->flags & MEDIA_PAD_FL_SOURCE) {
- ret = set_crop(pad, &crop);
+ if (pad->flags & MEDIA_PAD_FL_SINK) {
+ ret = set_format(pad, &format);
if (ret < 0)
return ret;
}
- ret = set_format(pad, &format);
+ ret = set_selection(pad, V4L2_SEL_TGT_CROP, &crop);
if (ret < 0)
return ret;
- if (pad->flags & MEDIA_PAD_FL_SINK) {
- ret = set_crop(pad, &crop);
+ ret = set_selection(pad, V4L2_SEL_TGT_COMPOSE, &compose);
+ if (ret < 0)
+ return ret;
+
+ if (pad->flags & MEDIA_PAD_FL_SOURCE) {
+ ret = set_format(pad, &format);
if (ret < 0)
return ret;
}
{ "Y10", V4L2_MBUS_FMT_Y10_1X10 },
{ "Y12", V4L2_MBUS_FMT_Y12_1X12 },
{ "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
+ { "YUYV1_5X8", V4L2_MBUS_FMT_YUYV8_1_5X8 },
{ "YUYV2X8", V4L2_MBUS_FMT_YUYV8_2X8 },
{ "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
+ { "UYVY1_5X8", V4L2_MBUS_FMT_UYVY8_1_5X8 },
{ "UYVY2X8", V4L2_MBUS_FMT_UYVY8_2X8 },
{ "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
{ "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },