stereoplay

view src/stereoplay.c @ 6:8fc045d33d62

updated the code to work with more recent ffmpeg versions
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 26 Sep 2020 14:26:39 +0300
parents acf3d25f23cb
children
line source
1 /*
2 Stereoplay - an OpenGL stereoscopic video player.
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <signal.h>
23 #include <time.h>
24 #include <assert.h>
25 #include <alloca.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/mman.h>
29 #include <sys/wait.h>
30 #include <sys/time.h>
31 #include <sys/select.h>
32 #include <X11/Xlib.h>
33 #include <GL/glew.h>
34 #include <GL/glut.h>
35 #include <GL/freeglut_ext.h>
36 #include <GL/glx.h>
37 #include "vid.h"
38 #include "sdr.h"
39 #include "config.h"
41 void cleanup(void);
42 void decoding_loop(void);
43 void disp(void);
44 void reshape(int x, int y);
45 void keyb(unsigned char key, int x, int y);
46 void skeyb(int key, int x, int y);
47 void sig(int s);
48 void sig_decode(int s);
49 void *shmalloc(size_t sz);
50 int parse_args(int argc, char **argv);
52 struct video_file *vf;
53 uint32_t *img;
54 int vid_xsz, vid_ysz, win_xsz, win_ysz;
55 unsigned int tex, sdr;
56 int decode_pid;
57 int pfd[2], xsock = -1;
58 int upd_frame;
59 int fullscr;
60 int swap_eyes;
62 Display *dpy;
64 const char *vid_fname;
65 int busy_loop;
68 int main(int argc, char **argv)
69 {
70 struct timeval *selwait = 0;
71 /*char *stereo_combiner, *sdrfile;*/
73 if(parse_args(argc, argv) == -1) {
74 return 1;
75 }
77 atexit(cleanup);
79 if(!(vf = vid_open(vid_fname))) {
80 return 1;
81 }
82 vid_xsz = win_xsz = vid_frame_width(vf);
83 vid_ysz = win_ysz = vid_frame_height(vf);
85 if(!(img = shmalloc(vid_frame_size(vf)))) {
86 perror("failed to allocate frame buffer");
87 return 1;
88 }
90 if(pipe(pfd) == -1) {
91 perror("failed to create a self-pipe");
92 return 1;
93 }
95 glutInit(&argc, argv);
96 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_STEREO);
97 glutInitWindowSize(win_xsz, win_ysz);
98 glutCreateWindow(argv[1]);
100 glutDisplayFunc(disp);
101 glutReshapeFunc(reshape);
102 glutKeyboardFunc(keyb);
103 glutSpecialFunc(skeyb);
105 if(busy_loop) {
106 selwait = alloca(sizeof *selwait);
107 selwait->tv_sec = selwait->tv_usec = 0;
108 }
110 glewInit();
112 /* create the frame texture */
113 glGenTextures(1, &tex);
114 glBindTexture(GL_TEXTURE_2D, tex);
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
119 glTexImage2D(GL_TEXTURE_2D, 0, 4, vid_xsz, vid_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
120 glEnable(GL_TEXTURE_2D);
122 /*if(!(stereo_combiner = getenv("STEREO_METHOD"))) {
123 stereo_combiner = "redcyan";
124 }
126 sdrfile = alloca(strlen(stereo_combiner) + strlen(SDRDIR) + 7);
127 sprintf(sdrfile, SDRDIR "/%s.glsl", stereo_combiner);
129 if(!(sdr = create_program_load(0, sdrfile))) {
130 return 1;
131 }
132 bind_program(sdr);
133 set_uniform_float(sdr, "left_offs", 0.5);
134 set_uniform_float(sdr, "right_offs", 0.0);*/
136 signal(SIGCHLD, sig);
138 if((decode_pid = fork()) == -1) {
139 perror("failed to fork video decoding process");
140 return 1;
141 } else if(decode_pid) {
142 close(pfd[1]);
143 } else {
144 close(pfd[0]);
145 decoding_loop();
146 _exit(0);
147 }
149 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
151 dpy = glXGetCurrentDisplay();
152 xsock = ConnectionNumber(dpy);
154 for(;;) {
155 /*int res;
156 fd_set rdset;
158 FD_ZERO(&rdset);
159 FD_SET(xsock, &rdset);
160 FD_SET(pfd[0], &rdset);
162 do {
163 res = select((xsock > pfd[0] ? xsock : pfd[0]) + 1, &rdset, 0, 0, selwait);
164 } while(res == -1 && errno == EINTR);
166 if(FD_ISSET(pfd[0], &rdset)) {
167 unsigned char done;
168 read(pfd[0], &done, 1);
170 if(done) {
171 exit(0);
172 } else {
173 upd_frame = 1;
174 glutPostRedisplay();
175 }
176 }*/
178 upd_frame = 1;
179 glutMainLoopEvent();
180 if(busy_loop) {
181 glutPostRedisplay();
182 }
183 }
185 return 0;
186 }
188 void cleanup(void)
189 {
190 if(tex) {
191 glDeleteTextures(1, &tex);
192 }
193 if(img) {
194 munmap(img, vid_frame_size(vf));
195 }
196 if(vf) {
197 vid_close(vf);
198 }
199 close(pfd[0]);
200 }
203 static int paused;
205 /* decoding_loop() runs in a separate decoding process and communicates
206 * with the parent process through the pfd[1] pipe.
207 */
208 void decoding_loop(void)
209 {
210 unsigned char done = 0;
211 struct timespec ts;
212 struct timeval tv0, tv;
213 unsigned long frame_nsec = vid_frame_interval(vf) * 1000;
214 printf("nanosecs per frame: %lu\n", frame_nsec);
216 signal(SIGUSR1, sig_decode);
218 gettimeofday(&tv0, 0);
220 while(vid_get_frame(vf, img) != -1) {
221 write(pfd[1], &done, 1);
223 gettimeofday(&tv, 0);
225 ts.tv_sec = 0;
226 ts.tv_nsec = frame_nsec - (tv.tv_usec - tv0.tv_usec) * 1000;
227 nanosleep(&ts, 0);
229 while(paused) {
230 pause();
231 }
233 gettimeofday(&tv0, 0);
234 }
236 done = 1;
237 write(pfd[1], &done, 1);
238 }
240 void disp(void)
241 {
242 if(upd_frame) {
243 /* frame changed, we must re-upload the texture */
244 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, vid_xsz, vid_ysz, GL_BGRA, GL_UNSIGNED_BYTE, img);
245 upd_frame = 0;
246 }
248 glDrawBuffer(swap_eyes ? GL_BACK_RIGHT : GL_BACK_LEFT);
250 glBegin(GL_QUADS);
251 glColor3f(1, 1, 1);
252 glTexCoord2f(0, 1); glVertex2f(-1, -1);
253 glTexCoord2f(0.5, 1); glVertex2f(1, -1);
254 glTexCoord2f(0.5, 0); glVertex2f(1, 1);
255 glTexCoord2f(0, 0); glVertex2f(-1, 1);
256 glEnd();
258 glDrawBuffer(swap_eyes ? GL_BACK_LEFT : GL_BACK_RIGHT);
260 glBegin(GL_QUADS);
261 glColor3f(1, 1, 1);
262 glTexCoord2f(0.5, 1); glVertex2f(-1, -1);
263 glTexCoord2f(1, 1); glVertex2f(1, -1);
264 glTexCoord2f(1, 0); glVertex2f(1, 1);
265 glTexCoord2f(0.5, 0); glVertex2f(-1, 1);
266 glEnd();
268 glutSwapBuffers();
269 }
271 void reshape(int x, int y)
272 {
273 if(!fullscr) {
274 win_xsz = x;
275 win_ysz = y;
276 }
278 glViewport(0, 0, x, y);
279 }
281 void keyb(unsigned char key, int x, int y)
282 {
283 switch(key) {
284 case 'q':
285 exit(0);
287 case 'f':
288 fullscr = !fullscr;
289 if(fullscr) {
290 glutFullScreen();
291 } else {
292 glutReshapeWindow(win_xsz, win_ysz);
293 }
294 break;
296 case ' ':
297 kill(decode_pid, SIGUSR1);
298 break;
300 case 's':
301 swap_eyes = !swap_eyes;
302 /*if(swap_eyes) {
303 set_uniform_float(sdr, "left_offs", 0.0);
304 set_uniform_float(sdr, "right_offs", 0.5);
305 } else {
306 set_uniform_float(sdr, "left_offs", 0.5);
307 set_uniform_float(sdr, "right_offs", 0.0);
308 }*/
309 break;
311 default:
312 break;
313 }
314 }
316 void skeyb(int key, int x, int y)
317 {
318 switch(key) {
319 case GLUT_KEY_LEFT:
320 /* TODO skip fwd */
321 break;
323 case GLUT_KEY_RIGHT:
324 /* TODO skip back */
325 break;
327 case GLUT_KEY_UP:
328 /* TODO skip fwd more */
329 break;
331 case GLUT_KEY_DOWN:
332 /* TODO skip back more */
333 break;
335 case GLUT_KEY_PAGE_UP:
336 /* TODO skip fwd a lot */
337 break;
339 case GLUT_KEY_PAGE_DOWN:
340 /* TODO skip back a lot */
341 break;
343 default:
344 break;
345 }
346 }
348 void sig(int s)
349 {
350 if(s == SIGCHLD) {
351 wait(0);
352 }
353 }
355 void sig_decode(int s)
356 {
357 signal(s, sig_decode);
359 if(s == SIGUSR1) {
360 paused = !paused;
361 }
362 }
364 void *shmalloc(size_t sz)
365 {
366 int fd;
367 void *shm;
369 if(!(fd = open("/dev/zero", O_RDWR))) {
370 return 0;
371 }
373 if((shm = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == (void*)-1) {
374 shm = 0;
375 }
377 close(fd);
378 return shm;
379 }
381 int parse_args(int argc, char **argv)
382 {
383 int i;
384 char *method = 0;
386 for(i=1; i<argc; i++) {
387 if(argv[i][0] == '-' && argv[i][2] == 0) {
388 switch(argv[i][1]) {
389 case 'b':
390 busy_loop = 1;
391 printf("busy looping!\n");
392 break;
394 case 's':
395 method = argv[++i];
396 break;
398 case 'h':
399 printf("Usage: %s [options]\n", argv[0]);
400 printf("options:\n");
401 printf(" -b busy loop (redraw continuously)\n");
402 printf(" -s <method> stereo presentation method\n");
403 printf(" -h print usage and exit\n");
404 return 0;
406 default:
407 fprintf(stderr, "invalid option: %s\n", argv[i]);
408 return -1;
409 }
410 } else {
411 if(vid_fname) {
412 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
413 return -1;
414 }
415 vid_fname = argv[i];
416 }
417 }
419 if(!vid_fname) {
420 fprintf(stderr, "you must specify a video file to open\n");
421 return -1;
422 }
424 return 0;
425 }