75e2ca421bf1cfcfb156f9c2dec48b000753f972
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.
19 #include <sys/soundcard.h>
25 #define MIN(a, b) ((a) < (b) ? (a) : (b))
26 #define MAX(a, b) ((a) > (b) ? (a) : (b))
29 static struct termios termios
;
33 static float zoom
= 1;
34 static int magnify
= 1;
36 static int fullscreen
= 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
= 16; /* synchronization steps */
49 static int sync_cur
; /* synchronization steps left */
51 static void stroll(void)
56 static void draw_frame(void *img
, int linelen
)
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
;
69 fb_set(r
, cb
, row
, nc
);
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)
84 static char a_buf
[AUDIOBUFS
][ABUFSZ
];
85 static int a_len
[AUDIOBUFS
];
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
)
101 while (audio
&& a_reset
)
105 static int readkey(void)
108 if (read(STDIN_FILENO
, &b
, 1) <= 0)
113 static void waitkey(void)
115 struct pollfd ufds
[1];
116 ufds
[0].fd
= STDIN_FILENO
;
117 ufds
[0].events
= POLLIN
;
121 static int ffarg(int def
)
128 static void ffjmp(int n
, int rel
)
130 struct ffs
*ffs
= video
? vffs
: affs
;
131 long pos
= ffs_pos(ffs
, n
);
134 ffs_seek(affs
, pos
, frame_jmp
);
136 ffs_seek(vffs
, pos
, frame_jmp
);
139 static void printinfo(void)
141 struct ffs
*ffs
= video
? vffs
: affs
;
142 printf("fbff: %8lx \t (AV: %d)\r",
143 ffs_pos(ffs
, 0), video
&& audio
? ffs_avdiff(vffs
, affs
) : 0);
147 #define JMP1 (1 << 5)
148 #define JMP2 (JMP1 << 3)
149 #define JMP3 (JMP2 << 5)
151 static void execkey(void)
154 while ((c
= readkey()) != -1) {
160 ffjmp(ffarg(1) * JMP1
, 1);
163 ffjmp(-ffarg(1) * JMP1
, 1);
166 ffjmp(ffarg(1) * JMP2
, 1);
169 ffjmp(-ffarg(1) * JMP2
, 1);
172 ffjmp(ffarg(1) * JMP3
, 1);
175 ffjmp(-ffarg(1) * JMP3
, 1);
190 sync_diff
= -ffarg(0);
193 sync_diff
= ffarg(0);
196 sync_diff
= ffs_avdiff(vffs
, affs
);
202 sync_cur
= ffarg(sync_cnt
);
209 arg
= arg
* 10 + c
- '0';
214 /* return nonzero if one more video frame can be decoded */
215 static int vsync(void)
219 return ffs_avdiff(vffs
, affs
) >= sync_diff
;
225 static void mainloop(void)
228 while (eof
< audio
+ video
) {
237 while (audio
&& !eof
&& !a_prodwait()) {
238 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFSZ
);
243 a_prod
= (a_prod
+ 1) & (AUDIOBUFS
- 1);
246 if (video
&& (!audio
|| eof
|| vsync())) {
247 int ignore
= jump
&& (vnum
++ % (jump
+ 1));
249 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
253 draw_frame((void *) buf
, ret
);
261 static void oss_init(void)
264 afd
= open("/dev/dsp", O_RDWR
);
266 fprintf(stderr
, "cannot open /dev/dsp\n");
269 ffs_ainfo(affs
, &rate
, &bps
, &ch
);
270 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
271 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
272 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bps
);
275 static void oss_close(void)
280 static void *process_audio(void *dat
)
284 while (!a_reset
&& (a_conswait() || paused
) && !exited
)
294 write(afd
, a_buf
[a_cons
], a_len
[a_cons
]);
295 a_cons
= (a_cons
+ 1) & (AUDIOBUFS
- 1);
302 static void term_setup(void)
304 struct termios newtermios
;
305 tcgetattr(STDIN_FILENO
, &termios
);
306 newtermios
= termios
;
307 newtermios
.c_lflag
&= ~ICANON
;
308 newtermios
.c_lflag
&= ~ECHO
;
309 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &newtermios
);
310 fcntl(STDIN_FILENO
, F_SETFL
, fcntl(STDIN_FILENO
, F_GETFL
) | O_NONBLOCK
);
313 static void term_cleanup(void)
315 tcsetattr(STDIN_FILENO
, 0, &termios
);
318 static void sigcont(int sig
)
323 static char *usage
= "usage: fbff [options] file\n"
325 " -z x zoom the screen using ffmpeg\n"
326 " -m x magnify the screen by repeating pixels\n"
327 " -j x jump every x video frames; for slow machines\n"
328 " -f start full screen\n"
329 " -v video only playback\n"
330 " -a audio only playback\n"
331 " -s always synchronize; useful for files with bad video framerate\n"
332 " -t use time based seeking; only if the default does't work\n"
333 " -R adjust the video to the right of the screen\n\n";
335 static void read_args(int argc
, char *argv
[])
339 if (!strcmp(argv
[i
], "-m"))
340 magnify
= atoi(argv
[++i
]);
341 if (!strcmp(argv
[i
], "-z"))
342 zoom
= atof(argv
[++i
]);
343 if (!strcmp(argv
[i
], "-j"))
344 jump
= atoi(argv
[++i
]);
345 if (!strcmp(argv
[i
], "-f"))
347 if (!strcmp(argv
[i
], "-a"))
349 if (!strcmp(argv
[i
], "-s"))
350 sync_cnt
= sync_cur
= (1 << 30);
351 if (!strcmp(argv
[i
], "-v"))
353 if (!strcmp(argv
[i
], "-t"))
355 if (!strcmp(argv
[i
], "-h"))
357 if (!strcmp(argv
[i
], "-R"))
363 int main(int argc
, char *argv
[])
366 char *path
= argv
[argc
- 1];
368 printf("usage: %s [options] filename\n", argv
[0]);
371 read_args(argc
, argv
);
373 if (video
&& !(vffs
= ffs_alloc(path
, 1)))
375 if (audio
&& !(affs
= ffs_alloc(path
, 0)))
377 if (!video
&& !audio
)
380 pthread_create(&a_thread
, NULL
, process_audio
, NULL
);
385 ffs_vinfo(vffs
, &w
, &h
);
386 if (magnify
!= 1 && sizeof(fbval_t
) != FBM_BPP(fb_mode()))
387 fprintf(stderr
, "fbff: magnify != 1 and fbval_t doesn't match\n");
389 zoom
= (float) fb_cols() / w
/ magnify
;
390 ffs_vsetup(vffs
, zoom
, fb_mode());
393 signal(SIGCONT
, sigcont
);
402 pthread_join(a_thread
, NULL
);