]> git.sesse.net Git - glcapture/blob - glcapture.c
Add clock_gettime() emulation.
[glcapture] / glcapture.c
1 /*
2  * Use something like this:
3  *
4  * gcc -shared -fPIC -o glcapture.so glcapture.c -ldl
5  * LD_PRELOAD=`pwd`/glcapture.so ./demo
6  * mencoder -noskip -endpos 76.920 -ovc x264 -x264encopts bitrate=3000:pass=1 -audiofile data/Juledemo.ogg -o tufs.avi mf://\*.tga -mf fps=50 -oac mp3lame -lameopts cbr:br=128
7  * mencoder -noskip -endpos 76.920 -ovc x264 -x264encopts bitrate=3000:pass=2 -audiofile data/Juledemo.ogg -o tufs.avi mf://\*.tga -mf fps=50 -oac mp3lame -lameopts cbr:br=128
8  */
9
10 #include <stdio.h>
11 #include <GL/glx.h>
12 #include <sys/time.h>
13 #include <unistd.h>
14 #include <dlfcn.h>
15
16 #ifndef RTLD_NEXT
17 #define RTLD_NEXT ((void *) -1l)
18 #endif
19
20 #define WIDTH 640
21 #define HEIGHT 480
22 #define FPS 50
23
24 static char buf[WIDTH * HEIGHT * 3];
25 static int frameno = 0;
26
27 void glXSwapBuffers( Display *dpy, GLXDrawable drawable )
28 {
29         void (*real_swap)(Display*, GLXDrawable);
30         real_swap = (void(*)(Display*, GLXDrawable))dlsym(RTLD_NEXT, "glXSwapBuffers");
31
32         FILE *ppm;
33         char filename[256];
34         glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGR, GL_UNSIGNED_BYTE, buf);
35
36         sprintf(filename, "frame%05u.tga", frameno++);
37         ppm = fopen(filename, "w");
38         fwrite("\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12, 1, ppm);
39         {
40                 unsigned short w = WIDTH;
41                 fwrite(&w, 2, 1, ppm);
42         }
43         {
44                 unsigned short h = HEIGHT;
45                 fwrite(&h, 2, 1, ppm);
46         }
47         fwrite("\x18\x00", 2, 1, ppm);
48         fwrite(buf, WIDTH*HEIGHT*3, 1, ppm);
49         fclose(ppm);
50
51         real_swap(dpy, drawable);
52 }
53
54 int gettimeofday(struct timeval *tv, struct timezone *tz)
55 {
56         printf("gettimeofday frame %u\n", frameno);
57         tv->tv_sec = frameno / FPS;
58         tv->tv_usec = ((double)(frameno % FPS) / FPS) * 1000000;
59         return 0;
60 }
61
62 int clock_gettime(clockid_t clk_id, struct timespec *tp)
63 {
64         tp->tv_sec = frameno / FPS;
65         tp->tv_nsec = ((double)(frameno % FPS) / FPS) * 1e9;
66
67         return 0;
68 }