#include "media.h"
#include "options.h"
#include "subdev.h"
-#include "tools.h"
/* -----------------------------------------------------------------------------
* Links setup
if (entity == NULL)
return NULL;
- for (++end; isspace(*end); ++end);
+ ++end;
} else {
- entity_id = strtoul(p, &end, 10) - 1;
- if (entity_id >= media->entities_count)
+ entity_id = strtoul(p, &end, 10);
+ entity = media_get_entity_by_id(media, entity_id);
+ if (entity == NULL)
return NULL;
-
- entity = &media->entities[entity_id];
}
+ for (; isspace(*end); ++end);
if (*end != ':')
return NULL;
* Formats setup
*/
-static struct {
- const char *name;
- enum v4l2_mbus_pixelcode code;
-} mbus_formats[] = {
- { "YUYV", V4L2_MBUS_FMT_YUYV16_1X16 },
- { "UYVY", V4L2_MBUS_FMT_UYVY16_1X16 },
- { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
- { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
-};
-
-static const char *pixelcode_to_string(enum v4l2_mbus_pixelcode code)
-{
- unsigned int i;
-
- for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
- if (mbus_formats[i].code == code)
- return mbus_formats[i].name;
- }
-
- return "unknown";
-}
-
static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
{
+ enum v4l2_mbus_pixelcode code;
unsigned int width, height;
- unsigned int i;
char *end;
for (; isspace(*p); ++p);
for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
- for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
- if (strncmp(mbus_formats[i].name, p, end - p) == 0)
- break;
- }
-
- if (i == ARRAY_SIZE(mbus_formats))
+ code = string_to_pixelcode(p, end - p);
+ if (code == (enum v4l2_mbus_pixelcode)-1)
return -EINVAL;
for (p = end; isspace(*p); ++p);
memset(format, 0, sizeof(*format));
format->width = width;
format->height = height;
- format->code = mbus_formats[i].code;
+ format->code = code;
+
+ return 0;
+}
+
+static int parse_crop(struct v4l2_rect *crop, const char *p, char **endp)
+{
+ char *end;
+
+ for (; isspace(*p); ++p);
+
+ crop->left = strtoul(p, &end, 10);
+ if (*end != ',')
+ return -EINVAL;
+
+ p = end + 1;
+ crop->top = strtoul(p, &end, 10);
+ if (*end != '/')
+ return -EINVAL;
+
+ p = end + 1;
+ crop->width = strtoul(p, &end, 10);
+ if (*end != 'x')
+ return -EINVAL;
+
+ p = end + 1;
+ crop->height = strtoul(p, &end, 10);
+ *endp = end;
+
+ return 0;
+}
+
+static int parse_frame_interval(struct v4l2_fract *interval, const char *p, char **endp)
+{
+ char *end;
+
+ for (; isspace(*p); ++p);
+
+ interval->numerator = strtoul(p, &end, 10);
+
+ for (p = end; isspace(*p); ++p);
+ if (*p++ != '/')
+ return -EINVAL;
+ for (; isspace(*p); ++p);
+ interval->denominator = strtoul(p, &end, 10);
+
+ *endp = end;
return 0;
}
static struct media_entity_pad *parse_pad_format(struct media_device *media,
- struct v4l2_mbus_framefmt *format, const char *p, char **endp)
+ struct v4l2_mbus_framefmt *format, struct v4l2_rect *crop,
+ struct v4l2_fract *interval, const char *p, char **endp)
{
struct media_entity_pad *pad;
char *end;
return NULL;
for (p = end; isspace(*p); p++);
- if (*p++ != ']')
+ if (isdigit(*p)) {
+ ret = parse_crop(crop, p, &end);
+ if (ret < 0)
+ return NULL;
+
+ for (p = end; isspace(*p); p++);
+ }
+
+ if (*p == '@') {
+ ret = parse_frame_interval(interval, ++p, &end);
+ if (ret < 0)
+ return NULL;
+
+ for (p = end; isspace(*p); p++);
+ }
+
+ if (*p != ']')
return NULL;
- *endp = (char *)p;
+ *endp = (char *)p + 1;
return pad;
}
ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
V4L2_SUBDEV_FORMAT_ACTIVE);
if (ret < 0) {
- printf("Unable to set format: %s(%u)", strerror(ret), ret);
+ printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
return ret;
}
return 0;
}
+static int set_crop(struct media_entity_pad *pad, struct v4l2_rect *crop)
+{
+ int ret;
+
+ if (crop->left == -1 || crop->top == -1)
+ return 0;
+
+ printf("Setting up crop rectangle %u,%u/%ux%u on pad %s/%u\n",
+ crop->left, crop->top, crop->width, crop->height,
+ pad->entity->info.name, pad->index);
+
+ ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
+ V4L2_SUBDEV_FORMAT_ACTIVE);
+ if (ret < 0) {
+ printf("Unable to set crop rectangle: %s (%d)", strerror(-ret), ret);
+ return ret;
+ }
+
+ printf("Crop rectangle set: %u,%u/%ux%u\n",
+ crop->left, crop->top, crop->width, crop->height);
+
+ return 0;
+}
+
+static int set_frame_interval(struct media_entity *entity, struct v4l2_fract *interval)
+{
+ int ret;
+
+ printf("Setting up frame interval %u/%u on entity %s\n",
+ interval->numerator, interval->denominator, entity->info.name);
+
+ ret = v4l2_subdev_set_frame_interval(entity, interval);
+ if (ret < 0) {
+ printf("Unable to set frame interval: %s (%d)", strerror(-ret), ret);
+ return ret;
+ }
+
+ printf("Frame interval set: %u/%u\n",
+ interval->numerator, interval->denominator);
+
+ return 0;
+}
+
+
static int setup_format(struct media_device *media, const char *p, char **endp)
{
struct v4l2_mbus_framefmt format;
struct media_entity_pad *pad;
+ struct v4l2_rect crop = { -1, -1, -1, -1 };
+ struct v4l2_fract interval = { 0, 0 };
unsigned int i;
char *end;
int ret;
- pad = parse_pad_format(media, &format, p, &end);
+ pad = parse_pad_format(media, &format, &crop, &interval, p, &end);
if (pad == NULL) {
printf("Unable to parse format\n");
return -EINVAL;
}
+ if (pad->type == MEDIA_PAD_TYPE_OUTPUT) {
+ ret = set_crop(pad, &crop);
+ if (ret < 0)
+ return ret;
+ }
+
ret = set_format(pad, &format);
if (ret < 0)
return ret;
+ if (pad->type == MEDIA_PAD_TYPE_INPUT) {
+ ret = set_crop(pad, &crop);
+ if (ret < 0)
+ return ret;
+ }
+
+ if (interval.numerator != 0) {
+ ret = set_frame_interval(pad->entity, &interval);
+ if (ret < 0) {
+ printf("Unable to set frame interval\n");
+ return ret;
+ }
+ }
+
/* If the pad is an output pad, automatically set the same format on
* the remote subdev input pads, if any.
*/
do {
ret = setup_format(media, p, &end);
- if (ret < 0) {
- printf("Unable to parse format\n");
+ if (ret < 0)
return ret;
- }
p = end + 1;
} while (*end == ',');
printf("Entity '%s' not found\n", media_opts.entity);
}
- if (media_opts.print) {
- media_print_topology(media);
+ if (media_opts.print || media_opts.print_dot) {
+ media_print_topology(media, media_opts.print_dot);
printf("\n");
}