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))
28 static int frame_jmp
= 1; /* the changes to pos_cur for each frame */
29 static int afd
; /* oss fd */
32 static struct termios termios
;
36 static float zoom
= 1;
37 static int magnify
= 0;
39 static int fullscreen
= 0;
44 static struct ffs
*affs
; /* audio ffmpeg stream */
45 static struct ffs
*vffs
; /* video ffmpeg stream */
47 static void draw_frame(void *img
, int linelen
)
53 ffs_vinfo(vffs
, &w
, &h
);
54 nr
= MIN(h
* zoom
, fb_rows() / magnify
);
55 nc
= MIN(w
* zoom
, fb_cols() / magnify
);
56 cb
= just
? fb_cols() - nc
* magnify
: 0;
57 for (r
= 0; r
< nr
; r
++) {
58 fbval_t
*row
= img
+ r
* linelen
;
60 fb_set(r
, cb
, row
, nc
);
63 for (c
= 0; c
< nc
; c
++)
64 for (i
= 0; i
< magnify
; i
++)
65 buf
[c
* magnify
+ i
] = row
[c
];
66 for (i
= 0; i
< magnify
; i
++)
67 fb_set(r
* magnify
+ i
, cb
, buf
, nc
* magnify
);
71 #define ABUFSZ (1 << 18)
75 static char a_buf
[AUDIOBUFS
][ABUFSZ
];
76 static int a_len
[AUDIOBUFS
];
79 static int a_conswait(void)
81 return a_cons
== a_prod
;
84 static int a_prodwait(void)
86 return ((a_prod
+ 1) & (AUDIOBUFS
- 1)) == a_cons
;
89 static void a_doreset(int pause
)
92 while (audio
&& a_reset
)
96 static int readkey(void)
99 if (read(STDIN_FILENO
, &b
, 1) <= 0)
104 static void waitkey(void)
106 struct pollfd ufds
[1];
107 ufds
[0].fd
= STDIN_FILENO
;
108 ufds
[0].events
= POLLIN
;
112 static int ffarg(void)
119 static void ffjmp(int n
, int rel
)
121 struct ffs
*ffs
= video
? vffs
: affs
;
122 long pos
= ffs_pos(ffs
, n
);
125 ffs_seek(affs
, pos
, frame_jmp
);
127 ffs_seek(vffs
, pos
, frame_jmp
);
130 static void printinfo(void)
132 struct ffs
*ffs
= video
? vffs
: affs
;
133 printf("fbff: %8lx\r", ffs_pos(ffs
, 0));
137 #define JMP1 (1 << 5)
138 #define JMP2 (JMP1 << 3)
139 #define JMP3 (JMP2 << 5)
141 static void execkey(void)
144 while ((c
= readkey()) != -1) {
150 ffjmp(ffarg() * JMP1
, 1);
153 ffjmp(-ffarg() * JMP1
, 1);
156 ffjmp(ffarg() * JMP2
, 1);
159 ffjmp(-ffarg() * JMP2
, 1);
162 ffjmp(ffarg() * JMP3
, 1);
165 ffjmp(-ffarg() * JMP3
, 1);
183 arg
= arg
* 10 + c
- '0';
188 static int is_vsync(void)
190 int cur
= ffs_seq(affs
, 0);
191 int all
= ffs_seq(affs
, 1);
192 int ratio
= all
? (all
- cur
) * 1024 / all
: 512;
193 int avdiff
= AUDIOBUFS
* 4 * ratio
/ 1024;
194 return ffs_seq(vffs
, 1) + avdiff
< ffs_seq(affs
, 1);
197 static void mainloop(void)
207 while (audio
&& !eof
&& !a_prodwait()) {
208 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFSZ
);
213 a_prod
= (a_prod
+ 1) & (AUDIOBUFS
- 1);
216 if (video
&& (!audio
|| eof
|| is_vsync())) {
217 int ignore
= jump
&& !(ffs_seq(vffs
, 0) % (jump
+ 1));
219 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
223 draw_frame((void *) buf
, ret
);
230 static void oss_init(void)
233 afd
= open("/dev/dsp", O_RDWR
);
235 fprintf(stderr
, "cannot open /dev/dsp\n");
238 ffs_ainfo(affs
, &rate
, &bps
, &ch
);
239 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
240 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
241 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bps
);
244 static void oss_close(void)
249 static void *process_audio(void *dat
)
253 while (!a_reset
&& a_conswait() && !exited
)
263 write(afd
, a_buf
[a_cons
], a_len
[a_cons
]);
264 a_cons
= (a_cons
+ 1) & (AUDIOBUFS
- 1);
271 static void term_setup(void)
273 struct termios newtermios
;
274 tcgetattr(STDIN_FILENO
, &termios
);
275 newtermios
= termios
;
276 newtermios
.c_lflag
&= ~ICANON
;
277 newtermios
.c_lflag
&= ~ECHO
;
278 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &newtermios
);
279 fcntl(STDIN_FILENO
, F_SETFL
, fcntl(STDIN_FILENO
, F_GETFL
) | O_NONBLOCK
);
282 static void term_cleanup(void)
284 tcsetattr(STDIN_FILENO
, 0, &termios
);
287 static void sigcont(int sig
)
292 static char *usage
= "usage: fbff [options] file\n"
294 " -m x magnify the screen by repeating pixels\n"
295 " -z x zoom the screen using ffmpeg\n"
296 " -j x jump every x video frames; for slow machines\n"
297 " -f start full screen\n"
298 " -v video only playback\n"
299 " -a audio only playback\n"
300 " -t use time based seeking; only if the default does't work\n"
301 " -R adjust the video to the right of the screen\n\n";
303 static void read_args(int argc
, char *argv
[])
307 if (!strcmp(argv
[i
], "-m"))
308 magnify
= atoi(argv
[++i
]);
309 if (!strcmp(argv
[i
], "-z"))
310 zoom
= atof(argv
[++i
]);
311 if (!strcmp(argv
[i
], "-j"))
312 jump
= atoi(argv
[++i
]);
313 if (!strcmp(argv
[i
], "-f"))
315 if (!strcmp(argv
[i
], "-a"))
317 if (!strcmp(argv
[i
], "-v"))
319 if (!strcmp(argv
[i
], "-t"))
320 frame_jmp
= 1024 / 32;
321 if (!strcmp(argv
[i
], "-h"))
323 if (!strcmp(argv
[i
], "-R"))
329 int main(int argc
, char *argv
[])
332 char *path
= argv
[argc
- 1];
334 printf("usage: %s [options] filename\n", argv
[0]);
337 read_args(argc
, argv
);
339 if (video
&& !(vffs
= ffs_alloc(path
, 1)))
341 if (audio
&& !(affs
= ffs_alloc(path
, 0)))
343 if (!video
&& !audio
)
346 pthread_create(&a_thread
, NULL
, process_audio
, NULL
);
351 ffs_vinfo(vffs
, &w
, &h
);
353 magnify
= MAX(1, fb_cols() / w
/ zoom
);
355 zoom
= (float) fb_cols() / w
/ magnify
;
356 ffs_vsetup(vffs
, zoom
, fb_mode());
359 signal(SIGCONT
, sigcont
);
368 pthread_join(a_thread
, NULL
);