vpost

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