vpost

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/vpost.c	Wed Oct 19 06:22:31 2011 +0300
     1.3 @@ -0,0 +1,170 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <limits.h>
     1.8 +#include <dlfcn.h>
     1.9 +#include <imago2.h>
    1.10 +
    1.11 +int parse_opt(int argc, char **argv);
    1.12 +
    1.13 +char *inpat, *outpat = "out%4d.png";
    1.14 +int range_min, range_max = INT_MAX;
    1.15 +float fps = 30;
    1.16 +char *plugin_name;
    1.17 +
    1.18 +void *dl;
    1.19 +int (*init)(void);
    1.20 +void (*shutdown)(void);
    1.21 +int (*process)(struct img_pixmap*, int, float);
    1.22 +
    1.23 +int main(int argc, char **argv)
    1.24 +{
    1.25 +	char *fname;
    1.26 +	int i, frame_width = -1, frame_height;
    1.27 +
    1.28 +	if(parse_opt(argc, argv) == -1) {
    1.29 +		return 1;
    1.30 +	}
    1.31 +
    1.32 +	if(!(dl = dlopen(plugin_name, RTLD_LAZY))) {
    1.33 +		fprintf(stderr, "failed to open post plugin: %s: %s\n", plugin_name, dlerror());
    1.34 +		return 1;
    1.35 +	}
    1.36 +	init = dlsym(dl, "init");
    1.37 +	shutdown = dlsym(dl, "shutdown");
    1.38 +	if(!(process = dlsym(dl, "process"))) {
    1.39 +		fprintf(stderr, "invalid post plugin, failed to load process func: %s\n", dlerror());
    1.40 +		return 1;
    1.41 +	}
    1.42 +
    1.43 +	if(init) {
    1.44 +		if(init() == -1) {
    1.45 +			fprintf(stderr, "init failed\n");
    1.46 +			return 1;
    1.47 +		}
    1.48 +	}
    1.49 +
    1.50 +	fname = alloca(strlen(inpat) + 11);
    1.51 +
    1.52 +	for(i=range_min; i<range_max; i++) {
    1.53 +		struct img_pixmap frame;
    1.54 +		float tmsec = (float)i / fps;
    1.55 +
    1.56 +		sprintf(fname, inpat, i);
    1.57 +
    1.58 +		img_init(&frame);
    1.59 +		if(img_load(&frame, fname) == -1) {
    1.60 +			fprintf(stderr, "file not found: %s\n", fname);
    1.61 +			img_destroy(&frame);
    1.62 +			break;
    1.63 +		}
    1.64 +		if(frame_width == -1) {
    1.65 +			frame_width = frame.width;
    1.66 +			frame_height = frame.height;
    1.67 +		}
    1.68 +		if(frame_width != frame.width || frame_height != frame.height) {
    1.69 +			fprintf(stderr, "frame %d differs in dimensions! (%dx%d instead of %dx%d)\n", i, frame.width, frame.height, frame_width, frame_height);
    1.70 +			img_destroy(&frame);
    1.71 +			break;
    1.72 +		}
    1.73 +
    1.74 +		img_convert(&frame, IMG_FMT_RGB24);
    1.75 +		if(process(&frame, i, tmsec) == -1) {
    1.76 +			fprintf(stderr, "the post plugin signalled an error\n");
    1.77 +			img_destroy(&frame);
    1.78 +			break;
    1.79 +		}
    1.80 +
    1.81 +		sprintf(fname, outpat, i);
    1.82 +		if(img_save(&frame, fname) == -1) {
    1.83 +			fprintf(stderr, "failed to save frame %d, aborting\n", i);
    1.84 +			img_destroy(&frame);
    1.85 +			break;
    1.86 +		}
    1.87 +		img_destroy(&frame);
    1.88 +	}
    1.89 +
    1.90 +	if(shutdown) {
    1.91 +		shutdown();
    1.92 +	}
    1.93 +	return 0;
    1.94 +}
    1.95 +
    1.96 +int parse_opt(int argc, char **argv)
    1.97 +{
    1.98 +	int i;
    1.99 +	char *endp;
   1.100 +
   1.101 +	for(i=1; i<argc; i++) {
   1.102 +		if(argv[i][0] == '-') {
   1.103 +			if(argv[i][2] == 0) {
   1.104 +				switch(argv[i][1]) {
   1.105 +				case 'i':
   1.106 +					inpat = argv[++i];
   1.107 +					break;
   1.108 +
   1.109 +				case 'o':
   1.110 +					outpat = argv[++i];
   1.111 +					break;
   1.112 +
   1.113 +				case 'r':
   1.114 +					fps = strtod(argv[++i], &endp);
   1.115 +					if(endp == argv[i]) {
   1.116 +						fprintf(stderr, "-r must be followed by a number\n");
   1.117 +						return -1;
   1.118 +					}
   1.119 +					break;
   1.120 +
   1.121 +				case 's':
   1.122 +					range_min = strtol(argv[++i], &endp, 10);
   1.123 +					if(0) {
   1.124 +				case 'e':
   1.125 +						range_max = strtol(argv[++i], &endp, 10);
   1.126 +					}
   1.127 +					if(endp == argv[i]) {
   1.128 +						fprintf(stderr, "-%c must be followed by a number\n", argv[i - 1][1]);
   1.129 +						return -1;
   1.130 +					}
   1.131 +					break;
   1.132 +
   1.133 +				case 'p':
   1.134 +					plugin_name = argv[++i];
   1.135 +					break;
   1.136 +
   1.137 +				default:
   1.138 +					goto invalid;
   1.139 +				}
   1.140 +			} else {
   1.141 +				goto invalid;
   1.142 +			}
   1.143 +		} else {
   1.144 +			goto invalid;
   1.145 +		}
   1.146 +	}
   1.147 +
   1.148 +	if(!inpat) {
   1.149 +		fprintf(stderr, "you must specify an input image sequence\n");
   1.150 +		return -1;
   1.151 +	}
   1.152 +	if(!plugin_name) {
   1.153 +		fprintf(stderr, "you must specify a post plugin\n");
   1.154 +		return -1;
   1.155 +	}
   1.156 +
   1.157 +	printf("%s -> [%s] -> %s\n", inpat, plugin_name, outpat);
   1.158 +	/*if(range_min > 0 || range_max < INT_MAX) {*/
   1.159 +		printf("frames: [%d, ", range_min);
   1.160 +		if(range_max < INT_MAX) {
   1.161 +			printf("%d]\n", range_max);
   1.162 +		} else {
   1.163 +			printf("inf]\n");
   1.164 +		}
   1.165 +	/*}*/
   1.166 +	printf("framerate: %f fps\n", fps);
   1.167 +
   1.168 +	return 0;
   1.169 +
   1.170 +invalid:
   1.171 +	fprintf(stderr, "invalid argument: %s\n", argv[i]);
   1.172 +	return -1;
   1.173 +}