vpost
view src/vpost.c @ 0:12f2c6e4934f
video post effect thingy with plugins... nice show gov'nor
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 19 Oct 2011 06:22:31 +0300 |
parents | |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <limits.h>
5 #include <dlfcn.h>
6 #include <imago2.h>
8 int parse_opt(int argc, char **argv);
10 char *inpat, *outpat = "out%4d.png";
11 int range_min, range_max = INT_MAX;
12 float fps = 30;
13 char *plugin_name;
15 void *dl;
16 int (*init)(void);
17 void (*shutdown)(void);
18 int (*process)(struct img_pixmap*, int, float);
20 int main(int argc, char **argv)
21 {
22 char *fname;
23 int i, frame_width = -1, frame_height;
25 if(parse_opt(argc, argv) == -1) {
26 return 1;
27 }
29 if(!(dl = dlopen(plugin_name, RTLD_LAZY))) {
30 fprintf(stderr, "failed to open post plugin: %s: %s\n", plugin_name, dlerror());
31 return 1;
32 }
33 init = dlsym(dl, "init");
34 shutdown = dlsym(dl, "shutdown");
35 if(!(process = dlsym(dl, "process"))) {
36 fprintf(stderr, "invalid post plugin, failed to load process func: %s\n", dlerror());
37 return 1;
38 }
40 if(init) {
41 if(init() == -1) {
42 fprintf(stderr, "init failed\n");
43 return 1;
44 }
45 }
47 fname = alloca(strlen(inpat) + 11);
49 for(i=range_min; i<range_max; i++) {
50 struct img_pixmap frame;
51 float tmsec = (float)i / fps;
53 sprintf(fname, inpat, i);
55 img_init(&frame);
56 if(img_load(&frame, fname) == -1) {
57 fprintf(stderr, "file not found: %s\n", fname);
58 img_destroy(&frame);
59 break;
60 }
61 if(frame_width == -1) {
62 frame_width = frame.width;
63 frame_height = frame.height;
64 }
65 if(frame_width != frame.width || frame_height != frame.height) {
66 fprintf(stderr, "frame %d differs in dimensions! (%dx%d instead of %dx%d)\n", i, frame.width, frame.height, frame_width, frame_height);
67 img_destroy(&frame);
68 break;
69 }
71 img_convert(&frame, IMG_FMT_RGB24);
72 if(process(&frame, i, tmsec) == -1) {
73 fprintf(stderr, "the post plugin signalled an error\n");
74 img_destroy(&frame);
75 break;
76 }
78 sprintf(fname, outpat, i);
79 if(img_save(&frame, fname) == -1) {
80 fprintf(stderr, "failed to save frame %d, aborting\n", i);
81 img_destroy(&frame);
82 break;
83 }
84 img_destroy(&frame);
85 }
87 if(shutdown) {
88 shutdown();
89 }
90 return 0;
91 }
93 int parse_opt(int argc, char **argv)
94 {
95 int i;
96 char *endp;
98 for(i=1; i<argc; i++) {
99 if(argv[i][0] == '-') {
100 if(argv[i][2] == 0) {
101 switch(argv[i][1]) {
102 case 'i':
103 inpat = argv[++i];
104 break;
106 case 'o':
107 outpat = argv[++i];
108 break;
110 case 'r':
111 fps = strtod(argv[++i], &endp);
112 if(endp == argv[i]) {
113 fprintf(stderr, "-r must be followed by a number\n");
114 return -1;
115 }
116 break;
118 case 's':
119 range_min = strtol(argv[++i], &endp, 10);
120 if(0) {
121 case 'e':
122 range_max = strtol(argv[++i], &endp, 10);
123 }
124 if(endp == argv[i]) {
125 fprintf(stderr, "-%c must be followed by a number\n", argv[i - 1][1]);
126 return -1;
127 }
128 break;
130 case 'p':
131 plugin_name = argv[++i];
132 break;
134 default:
135 goto invalid;
136 }
137 } else {
138 goto invalid;
139 }
140 } else {
141 goto invalid;
142 }
143 }
145 if(!inpat) {
146 fprintf(stderr, "you must specify an input image sequence\n");
147 return -1;
148 }
149 if(!plugin_name) {
150 fprintf(stderr, "you must specify a post plugin\n");
151 return -1;
152 }
154 printf("%s -> [%s] -> %s\n", inpat, plugin_name, outpat);
155 /*if(range_min > 0 || range_max < INT_MAX) {*/
156 printf("frames: [%d, ", range_min);
157 if(range_max < INT_MAX) {
158 printf("%d]\n", range_max);
159 } else {
160 printf("inf]\n");
161 }
162 /*}*/
163 printf("framerate: %f fps\n", fps);
165 return 0;
167 invalid:
168 fprintf(stderr, "invalid argument: %s\n", argv[i]);
169 return -1;
170 }