ffs: fix video frame rate delay on x86_64
[fbff.git] / fbff.c
blob6ed60f76e32c60b97a8923113c1f8ffb0e3374ff
1 /*
2 * fbff - a small ffmpeg-based framebuffer/oss media player
4 * Copyright (C) 2009-2011 Ali Gholami Rudi
6 * This program is released under GNU GPL version 2.
7 */
8 #include <fcntl.h>
9 #include <pty.h>
10 #include <ctype.h>
11 #include <signal.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <termios.h>
17 #include <unistd.h>
18 #include <sys/poll.h>
19 #include <sys/soundcard.h>
20 #include <pthread.h>
21 #include "config.h"
22 #include "ffs.h"
23 #include "draw.h"
25 #define MIN(a, b) ((a) < (b) ? (a) : (b))
26 #define MAX(a, b) ((a) > (b) ? (a) : (b))
28 static int arg;
29 static struct termios termios;
30 static int paused;
31 static int exited;
33 static float zoom = 1;
34 static int magnify = 1;
35 static int jump = 0;
36 static int fullscreen = 0;
37 static int video = 1; /* video stream; 0=none, 1=auto, >2=idx */
38 static int audio = 1; /* audio stream; 0=none, 1=auto, >2=idx */
39 static int just = 0;
40 static int frame_jmp = 1; /* the changes to pos_cur for each frame */
42 static struct ffs *affs; /* audio ffmpeg stream */
43 static struct ffs *vffs; /* video ffmpeg stream */
44 static int afd; /* oss fd */
45 static int vnum; /* decoded video frame count */
47 static int sync_diff; /* audio/video frame position diff */
48 static int sync_cnt = 32; /* synchronization steps */
49 static int sync_cur; /* synchronization steps left */
51 static void stroll(void)
53 usleep(10000);
56 static void draw_frame(void *img, int linelen)
58 int w, h;
59 fbval_t buf[1 << 14];
60 int nr, nc, cb;
61 int i, r, c;
62 ffs_vinfo(vffs, &w, &h);
63 nr = MIN(h * zoom, fb_rows() / magnify);
64 nc = MIN(w * zoom, fb_cols() / magnify);
65 cb = just ? fb_cols() - nc * magnify : 0;
66 for (r = 0; r < nr; r++) {
67 fbval_t *row = img + r * linelen;
68 if (magnify == 1) {
69 fb_set(r, cb, row, nc);
70 continue;
72 for (c = 0; c < nc; c++)
73 for (i = 0; i < magnify; i++)
74 buf[c * magnify + i] = row[c];
75 for (i = 0; i < magnify; i++)
76 fb_set(r * magnify + i, cb, buf, nc * magnify);
80 #define ABUFSZ (1 << 18)
82 static int a_cons;
83 static int a_prod;
84 static char a_buf[AUDIOBUFS][ABUFSZ];
85 static int a_len[AUDIOBUFS];
86 static int a_reset;
88 static int a_conswait(void)
90 return a_cons == a_prod;
93 static int a_prodwait(void)
95 return ((a_prod + 1) & (AUDIOBUFS - 1)) == a_cons;
98 static void a_doreset(int pause)
100 a_reset = 1 + pause;
101 while (audio && a_reset)
102 stroll();
105 static int readkey(void)
107 char b;
108 if (read(STDIN_FILENO, &b, 1) <= 0)
109 return -1;
110 return b;
113 static void waitkey(void)
115 struct pollfd ufds[1];
116 ufds[0].fd = STDIN_FILENO;
117 ufds[0].events = POLLIN;
118 poll(ufds, 1, -1);
121 static int ffarg(int def)
123 int n = arg;
124 arg = 0;
125 return n ? n : def;
128 static void ffjmp(int n, int rel)
130 struct ffs *ffs = video ? vffs : affs;
131 long pos = ffs_pos(ffs, n);
132 a_doreset(0);
133 sync_cur = sync_cnt;
134 if (audio)
135 ffs_seek(affs, pos, frame_jmp);
136 if (video)
137 ffs_seek(vffs, pos, frame_jmp);
140 static void printinfo(void)
142 struct ffs *ffs = video ? vffs : affs;
143 printf("fbff: %8lx \t (AV: %d)\r",
144 ffs_pos(ffs, 0), video && audio ? ffs_avdiff(vffs, affs) : 0);
145 fflush(stdout);
148 #define JMP1 (1 << 5)
149 #define JMP2 (JMP1 << 3)
150 #define JMP3 (JMP2 << 5)
152 static void execkey(void)
154 int c;
155 while ((c = readkey()) != -1) {
156 switch (c) {
157 case 'q':
158 exited = 1;
159 break;
160 case 'l':
161 ffjmp(ffarg(1) * JMP1, 1);
162 break;
163 case 'h':
164 ffjmp(-ffarg(1) * JMP1, 1);
165 break;
166 case 'j':
167 ffjmp(ffarg(1) * JMP2, 1);
168 break;
169 case 'k':
170 ffjmp(-ffarg(1) * JMP2, 1);
171 break;
172 case 'J':
173 ffjmp(ffarg(1) * JMP3, 1);
174 break;
175 case 'K':
176 ffjmp(-ffarg(1) * JMP3, 1);
177 break;
178 case '%':
179 if (arg)
180 ffjmp(100, 0);
181 break;
182 case 'i':
183 printinfo();
184 break;
185 case ' ':
186 case 'p':
187 paused = !paused;
188 sync_cur = sync_cnt;
189 break;
190 case '-':
191 sync_diff = -ffarg(0);
192 break;
193 case '+':
194 sync_diff = ffarg(0);
195 break;
196 case 'a':
197 sync_diff = ffs_avdiff(vffs, affs);
198 break;
199 case 'c':
200 sync_cnt = ffarg(0);
201 break;
202 case 's':
203 sync_cur = ffarg(sync_cnt);
204 break;
205 case 27:
206 arg = 0;
207 break;
208 default:
209 if (isdigit(c))
210 arg = arg * 10 + c - '0';
215 /* return nonzero if one more video frame can be decoded */
216 static int vsync(void)
218 if (sync_cur > 0) {
219 sync_cur--;
220 return ffs_avdiff(vffs, affs) >= sync_diff;
222 ffs_wait(vffs);
223 return 1;
226 static void mainloop(void)
228 int eof = 0;
229 while (eof < audio + video) {
230 execkey();
231 if (exited)
232 break;
233 if (paused) {
234 a_doreset(1);
235 waitkey();
236 continue;
238 while (audio && !eof && !a_prodwait()) {
239 int ret = ffs_adec(affs, a_buf[a_prod], ABUFSZ);
240 if (ret < 0)
241 eof++;
242 if (ret > 0) {
243 a_len[a_prod] = ret;
244 a_prod = (a_prod + 1) & (AUDIOBUFS - 1);
247 if (video && (!audio || eof || vsync())) {
248 int ignore = jump && (vnum++ % (jump + 1));
249 void *buf;
250 int ret = ffs_vdec(vffs, ignore ? NULL : &buf);
251 if (ret < 0)
252 eof++;
253 if (ret > 0)
254 draw_frame((void *) buf, ret);
255 } else {
256 stroll();
259 exited = 1;
262 static void oss_init(void)
264 int rate, ch, bps;
265 afd = open("/dev/dsp", O_RDWR);
266 if (afd < 0) {
267 fprintf(stderr, "cannot open /dev/dsp\n");
268 exit(1);
270 ffs_ainfo(affs, &rate, &bps, &ch);
271 ioctl(afd, SOUND_PCM_WRITE_RATE, &rate);
272 ioctl(afd, SOUND_PCM_WRITE_CHANNELS, &ch);
273 ioctl(afd, SOUND_PCM_WRITE_BITS, &bps);
276 static void oss_close(void)
278 close(afd);
281 static void *process_audio(void *dat)
283 oss_init();
284 while (1) {
285 while (!a_reset && (a_conswait() || paused) && !exited)
286 stroll();
287 if (exited)
288 goto ret;
289 if (a_reset) {
290 if (a_reset == 1)
291 a_cons = a_prod;
292 a_reset = 0;
293 continue;
295 write(afd, a_buf[a_cons], a_len[a_cons]);
296 a_cons = (a_cons + 1) & (AUDIOBUFS - 1);
298 ret:
299 oss_close();
300 return NULL;
303 static void term_setup(void)
305 struct termios newtermios;
306 tcgetattr(STDIN_FILENO, &termios);
307 newtermios = termios;
308 newtermios.c_lflag &= ~ICANON;
309 newtermios.c_lflag &= ~ECHO;
310 tcsetattr(STDIN_FILENO, TCSAFLUSH, &newtermios);
311 fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK);
314 static void term_cleanup(void)
316 tcsetattr(STDIN_FILENO, 0, &termios);
319 static void sigcont(int sig)
321 term_setup();
324 static char *usage = "usage: fbff [options] file\n"
325 "\noptions:\n"
326 " -z x zoom the screen using ffmpeg\n"
327 " -m x magnify the screen by repeating pixels\n"
328 " -j x jump every x video frames; for slow machines\n"
329 " -f start full screen\n"
330 " -v x select video stream; '-' disables video\n"
331 " -a x select audio stream; '-' disables audio\n"
332 " -s always synchronize; useful for files with bad video framerate\n"
333 " -t use time based seeking; only if the default does't work\n"
334 " -R adjust the video to the right of the screen\n\n";
336 static void read_args(int argc, char *argv[])
338 int i = 1;
339 while (i < argc) {
340 if (!strcmp(argv[i], "-m"))
341 magnify = atoi(argv[++i]);
342 if (!strcmp(argv[i], "-z"))
343 zoom = atof(argv[++i]);
344 if (!strcmp(argv[i], "-j"))
345 jump = atoi(argv[++i]);
346 if (!strcmp(argv[i], "-f"))
347 fullscreen = 1;
348 if (!strcmp(argv[i], "-s"))
349 sync_cnt = sync_cur = (1 << 30);
350 if (!strcmp(argv[i], "-v"))
351 video = argv[++i][0] == '-' ? 0 : atoi(argv[i]) + 2;
352 if (!strcmp(argv[i], "-a"))
353 audio = argv[++i][0] == '-' ? 0 : atoi(argv[i]) + 2;
354 if (!strcmp(argv[i], "-t"))
355 frame_jmp = 1024;
356 if (!strcmp(argv[i], "-h"))
357 printf(usage);
358 if (!strcmp(argv[i], "-R"))
359 just = 1;
360 i++;
364 int main(int argc, char *argv[])
366 pthread_t a_thread;
367 char *path = argv[argc - 1];
368 if (argc < 2) {
369 printf("usage: %s [options] filename\n", argv[0]);
370 return 1;
372 read_args(argc, argv);
373 ffs_globinit();
374 if (video && !(vffs = ffs_alloc(path, FFS_VIDEO | video - 1)))
375 video = 0;
376 if (audio && !(affs = ffs_alloc(path, FFS_AUDIO | audio - 1)))
377 audio = 0;
378 if (!video && !audio)
379 return 1;
380 if (audio)
381 pthread_create(&a_thread, NULL, process_audio, NULL);
382 if (video) {
383 int w, h;
384 if (fb_init())
385 return 1;
386 ffs_vinfo(vffs, &w, &h);
387 if (magnify != 1 && sizeof(fbval_t) != FBM_BPP(fb_mode()))
388 fprintf(stderr, "fbff: magnify != 1 and fbval_t doesn't match\n");
389 if (fullscreen)
390 zoom = (float) fb_cols() / w / magnify;
391 ffs_vsetup(vffs, zoom, fb_mode());
393 term_setup();
394 signal(SIGCONT, sigcont);
395 mainloop();
396 term_cleanup();
398 if (video) {
399 fb_free();
400 ffs_free(vffs);
402 if (audio) {
403 pthread_join(a_thread, NULL);
404 ffs_free(affs);
406 return 0;