Change crop rectangle format from L,T/WxH to (L,T)/WxH
[media-ctl.git] / main.c
1 /*
2  * Media controller test application
3  *
4  * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  */
19
20 #include <sys/mman.h>
21 #include <sys/ioctl.h>
22 #include <sys/time.h>
23 #include <sys/stat.h>
24
25 #include <ctype.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include <linux/types.h>
34 #include <linux/media.h>
35 #include <linux/v4l2-mediabus.h>
36 #include <linux/v4l2-subdev.h>
37 #include <linux/videodev2.h>
38
39 #include "media.h"
40 #include "options.h"
41 #include "subdev.h"
42
43 /* -----------------------------------------------------------------------------
44  * Links setup
45  */
46
47 static struct media_entity_pad *parse_pad(struct media_device *media, const char *p, char **endp)
48 {
49         unsigned int entity_id, pad;
50         struct media_entity *entity;
51         char *end;
52
53         for (; isspace(*p); ++p);
54
55         if (*p == '"') {
56                 for (end = (char *)p + 1; *end && *end != '"'; ++end);
57                 if (*end != '"')
58                         return NULL;
59
60                 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
61                 if (entity == NULL)
62                         return NULL;
63
64                 ++end;
65         } else {
66                 entity_id = strtoul(p, &end, 10);
67                 entity = media_get_entity_by_id(media, entity_id);
68                 if (entity == NULL)
69                         return NULL;
70         }
71         for (; isspace(*end); ++end);
72
73         if (*end != ':')
74                 return NULL;
75         for (p = end + 1; isspace(*p); ++p);
76
77         pad = strtoul(p, &end, 10);
78         for (p = end; isspace(*p); ++p);
79
80         if (pad >= entity->info.pads)
81                 return NULL;
82
83         for (p = end; isspace(*p); ++p);
84         if (endp)
85                 *endp = (char *)p;
86
87         return &entity->pads[pad];
88 }
89
90 static struct media_entity_link *parse_link(struct media_device *media, const char *p, char **endp)
91 {
92         struct media_entity_link *link;
93         struct media_entity_pad *source;
94         struct media_entity_pad *sink;
95         unsigned int i;
96         char *end;
97
98         source = parse_pad(media, p, &end);
99         if (source == NULL)
100                 return NULL;
101
102         if (end[0] != '-' || end[1] != '>')
103                 return NULL;
104         p = end + 2;
105
106         sink = parse_pad(media, p, &end);
107         if (sink == NULL)
108                 return NULL;
109
110         *endp = end;
111
112         for (i = 0; i < source->entity->info.links; i++) {
113                 link = &source->entity->links[i];
114
115                 if (link->source == source && link->sink == sink)
116                         return link;
117         }
118
119         return NULL;
120 }
121
122 static int setup_link(struct media_device *media, const char *p, char **endp)
123 {
124         struct media_entity_link *link;
125         __u32 flags;
126         char *end;
127
128         link = parse_link(media, p, &end);
129         if (link == NULL) {
130                 printf("Unable to parse link\n");
131                 return -EINVAL;
132         }
133
134         p = end;
135         if (*p++ != '[') {
136                 printf("Unable to parse link flags\n");
137                 return -EINVAL;
138         }
139
140         flags = strtoul(p, &end, 10);
141         for (p = end; isspace(*p); p++);
142         if (*p++ != ']') {
143                 printf("Unable to parse link flags\n");
144                 return -EINVAL;
145         }
146
147         for (; isspace(*p); p++);
148         *endp = (char *)p;
149
150         printf("Setting up link %u:%u -> %u:%u [%u]\n",
151                 link->source->entity->info.id, link->source->index,
152                 link->sink->entity->info.id, link->sink->index,
153                 flags);
154
155         return media_setup_link(media, link->source, link->sink, flags);
156 }
157
158 static int setup_links(struct media_device *media, const char *p)
159 {
160         char *end;
161         int ret;
162
163         do {
164                 ret = setup_link(media, p, &end);
165                 if (ret < 0)
166                         return ret;
167
168                 p = end + 1;
169         } while (*end == ',');
170
171         return *end ? -EINVAL : 0;
172 }
173
174 /* -----------------------------------------------------------------------------
175  * Formats setup
176  */
177
178 static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
179 {
180         enum v4l2_mbus_pixelcode code;
181         unsigned int width, height;
182         char *end;
183
184         for (; isspace(*p); ++p);
185         for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
186
187         code = string_to_pixelcode(p, end - p);
188         if (code == (enum v4l2_mbus_pixelcode)-1)
189                 return -EINVAL;
190
191         for (p = end; isspace(*p); ++p);
192         width = strtoul(p, &end, 10);
193         if (*end != 'x')
194                 return -EINVAL;
195
196         p = end + 1;
197         height = strtoul(p, &end, 10);
198         *endp = end;
199
200         memset(format, 0, sizeof(*format));
201         format->width = width;
202         format->height = height;
203         format->code = code;
204
205         return 0;
206 }
207
208 static int parse_crop(struct v4l2_rect *crop, const char *p, char **endp)
209 {
210         char *end;
211
212         if (*p++ != '(')
213                 return -EINVAL;
214
215         crop->left = strtoul(p, &end, 10);
216         if (*end != ',')
217                 return -EINVAL;
218
219         p = end + 1;
220         crop->top = strtoul(p, &end, 10);
221         if (*end++ != ')')
222                 return -EINVAL;
223         if (*end != '/')
224                 return -EINVAL;
225
226         p = end + 1;
227         crop->width = strtoul(p, &end, 10);
228         if (*end != 'x')
229                 return -EINVAL;
230
231         p = end + 1;
232         crop->height = strtoul(p, &end, 10);
233         *endp = end;
234
235         return 0;
236 }
237
238 static int parse_frame_interval(struct v4l2_fract *interval, const char *p, char **endp)
239 {
240         char *end;
241
242         for (; isspace(*p); ++p);
243
244         interval->numerator = strtoul(p, &end, 10);
245
246         for (p = end; isspace(*p); ++p);
247         if (*p++ != '/')
248                 return -EINVAL;
249
250         for (; isspace(*p); ++p);
251         interval->denominator = strtoul(p, &end, 10);
252
253         *endp = end;
254         return 0;
255 }
256
257 static struct media_entity_pad *parse_pad_format(struct media_device *media,
258         struct v4l2_mbus_framefmt *format, struct v4l2_rect *crop,
259         struct v4l2_fract *interval, const char *p, char **endp)
260 {
261         struct media_entity_pad *pad;
262         char *end;
263         int ret;
264
265         for (; isspace(*p); ++p);
266
267         pad = parse_pad(media, p, &end);
268         if (pad == NULL)
269                 return NULL;
270
271         for (p = end; isspace(*p); ++p);
272         if (*p++ != '[')
273                 return NULL;
274
275         ret = parse_format(format, p, &end);
276         if (ret < 0)
277                 return NULL;
278
279         for (p = end; isspace(*p); p++);
280         if (*p == '(') {
281                 ret = parse_crop(crop, p, &end);
282                 if (ret < 0)
283                         return NULL;
284
285                 for (p = end; isspace(*p); p++);
286         }
287
288         if (*p == '@') {
289                 ret = parse_frame_interval(interval, ++p, &end);
290                 if (ret < 0)
291                         return NULL;
292
293                 for (p = end; isspace(*p); p++);
294         }
295
296         if (*p != ']')
297                 return NULL;
298
299         *endp = (char *)p + 1;
300         return pad;
301 }
302
303 static int set_format(struct media_entity_pad *pad, struct v4l2_mbus_framefmt *format)
304 {
305         int ret;
306
307         printf("Setting up format %s %ux%u on pad %s/%u\n",
308                 pixelcode_to_string(format->code), format->width, format->height,
309                 pad->entity->info.name, pad->index);
310
311         ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
312                                      V4L2_SUBDEV_FORMAT_ACTIVE);
313         if (ret < 0) {
314                 printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
315                 return ret;
316         }
317
318         printf("Format set: %s %ux%u\n",
319                 pixelcode_to_string(format->code), format->width, format->height);
320
321         return 0;
322 }
323
324 static int set_crop(struct media_entity_pad *pad, struct v4l2_rect *crop)
325 {
326         int ret;
327
328         if (crop->left == -1 || crop->top == -1)
329                 return 0;
330
331         printf("Setting up crop rectangle %u,%u/%ux%u on pad %s/%u\n",
332                 crop->left, crop->top, crop->width, crop->height,
333                 pad->entity->info.name, pad->index);
334
335         ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
336                                    V4L2_SUBDEV_FORMAT_ACTIVE);
337         if (ret < 0) {
338                 printf("Unable to set crop rectangle: %s (%d)", strerror(-ret), ret);
339                 return ret;
340         }
341
342         printf("Crop rectangle set: %u,%u/%ux%u\n",
343                 crop->left, crop->top, crop->width, crop->height);
344
345         return 0;
346 }
347
348 static int set_frame_interval(struct media_entity *entity, struct v4l2_fract *interval)
349 {
350         int ret;
351
352         printf("Setting up frame interval %u/%u on entity %s\n",
353                 interval->numerator, interval->denominator, entity->info.name);
354
355         ret = v4l2_subdev_set_frame_interval(entity, interval);
356         if (ret < 0) {
357                 printf("Unable to set frame interval: %s (%d)", strerror(-ret), ret);
358                 return ret;
359         }
360
361         printf("Frame interval set: %u/%u\n",
362                 interval->numerator, interval->denominator);
363
364         return 0;
365 }
366
367
368 static int setup_format(struct media_device *media, const char *p, char **endp)
369 {
370         struct v4l2_mbus_framefmt format;
371         struct media_entity_pad *pad;
372         struct v4l2_rect crop = { -1, -1, -1, -1 };
373         struct v4l2_fract interval = { 0, 0 };
374         unsigned int i;
375         char *end;
376         int ret;
377
378         pad = parse_pad_format(media, &format, &crop, &interval, p, &end);
379         if (pad == NULL) {
380                 printf("Unable to parse format\n");
381                 return -EINVAL;
382         }
383
384         if (pad->type == MEDIA_PAD_TYPE_OUTPUT) {
385                 ret = set_crop(pad, &crop);
386                 if (ret < 0)
387                         return ret;
388         }
389
390         ret = set_format(pad, &format);
391         if (ret < 0)
392                 return ret;
393
394         if (pad->type == MEDIA_PAD_TYPE_INPUT) {
395                 ret = set_crop(pad, &crop);
396                 if (ret < 0)
397                         return ret;
398         }
399
400         if (interval.numerator != 0) {
401                 ret = set_frame_interval(pad->entity, &interval);
402                 if (ret < 0) {
403                         printf("Unable to set frame interval\n");
404                         return ret;
405                 }
406         }
407
408         /* If the pad is an output pad, automatically set the same format on
409          * the remote subdev input pads, if any.
410          */
411         if (pad->type == MEDIA_PAD_TYPE_OUTPUT) {
412                 for (i = 0; i < pad->entity->info.links; ++i) {
413                         struct media_entity_link *link = &pad->entity->links[i];
414                         struct v4l2_mbus_framefmt remote_format;
415
416                         if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
417                                 continue;
418
419                         if (link->source == pad &&
420                             link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV) {
421                                 remote_format = format;
422                                 set_format(link->sink, &remote_format);
423                         }
424                 }
425         }
426
427         *endp = end;
428         return 0;
429 }
430
431 static int setup_formats(struct media_device *media, const char *p)
432 {
433         char *end;
434         int ret;
435
436         do {
437                 ret = setup_format(media, p, &end);
438                 if (ret < 0)
439                         return ret;
440
441                 p = end + 1;
442         } while (*end == ',');
443
444         return *end ? -EINVAL : 0;
445 }
446
447 int main(int argc, char **argv)
448 {
449         struct media_device *media;
450         int ret = -1;
451
452         if (parse_cmdline(argc, argv))
453                 return EXIT_FAILURE;
454
455         /* Open the media device and enumerate entities, pads and links. */
456         media = media_open(media_opts.devname, media_opts.verbose);
457         if (media == NULL)
458                 goto out;
459
460         if (media_opts.entity) {
461                 struct media_entity *entity;
462
463                 entity = media_get_entity_by_name(media, media_opts.entity,
464                                                   strlen(media_opts.entity));
465                 if (entity == NULL) {
466                         printf("Entity '%s' not found\n", media_opts.entity);
467                         goto out;
468                 }
469
470                 printf("%s\n", entity->devname);
471         }
472
473         if (media_opts.pad) {
474                 struct media_entity_pad *pad;
475
476                 pad = parse_pad(media, media_opts.pad, NULL);
477                 if (pad == NULL) {
478                         printf("Pad '%s' not found\n", media_opts.pad);
479                         goto out;
480                 }
481
482                 v4l2_subdev_print_format(pad->entity, pad->index,
483                                          V4L2_SUBDEV_FORMAT_ACTIVE);
484                 printf("\n");
485         }
486
487         if (media_opts.print || media_opts.print_dot) {
488                 media_print_topology(media, media_opts.print_dot);
489                 printf("\n");
490         }
491
492         if (media_opts.reset) {
493                 printf("Resetting all links to inactive\n");
494                 media_reset_links(media);
495         }
496
497         if (media_opts.links)
498                 setup_links(media, media_opts.links);
499
500         if (media_opts.formats)
501                 setup_formats(media, media_opts.formats);
502
503         if (media_opts.interactive) {
504                 while (1) {
505                         char buffer[32];
506                         char *end;
507
508                         printf("Enter a link to modify or enter to stop\n");
509                         if (fgets(buffer, sizeof buffer, stdin) == NULL)
510                                 break;
511
512                         if (buffer[0] == '\n')
513                                 break;
514
515                         setup_link(media, buffer, &end);
516                 }
517         }
518
519         ret = 0;
520
521 out:
522         if (media)
523                 media_close(media);
524
525         return ret ? EXIT_FAILURE : EXIT_SUCCESS;
526 }
527