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
= 1;
39 static int fullscreen
= 0;
44 static struct ffs
*affs
; /* audio ffmpeg stream */
45 static struct ffs
*vffs
; /* video ffmpeg stream */
47 static void stroll(void)
52 static void draw_frame(void *img
, int linelen
)
58 ffs_vinfo(vffs
, &w
, &h
);
59 nr
= MIN(h
* zoom
, fb_rows() / magnify
);
60 nc
= MIN(w
* zoom
, fb_cols() / magnify
);
61 cb
= just
? fb_cols() - nc
* magnify
: 0;
62 for (r
= 0; r
< nr
; r
++) {
63 fbval_t
*row
= img
+ r
* linelen
;
65 fb_set(r
, cb
, row
, nc
);
68 for (c
= 0; c
< nc
; c
++)
69 for (i
= 0; i
< magnify
; i
++)
70 buf
[c
* magnify
+ i
] = row
[c
];
71 for (i
= 0; i
< magnify
; i
++)
72 fb_set(r
* magnify
+ i
, cb
, buf
, nc
* magnify
);
76 #define ABUFSZ (1 << 18)
80 static char a_buf
[AUDIOBUFS
][ABUFSZ
];
81 static int a_len
[AUDIOBUFS
];
84 static int a_conswait(void)
86 return a_cons
== a_prod
;
89 static int a_prodwait(void)
91 return ((a_prod
+ 1) & (AUDIOBUFS
- 1)) == a_cons
;
94 static void a_doreset(int pause
)
97 while (audio
&& a_reset
)
101 static int readkey(void)
104 if (read(STDIN_FILENO
, &b
, 1) <= 0)
109 static void waitkey(void)
111 struct pollfd ufds
[1];
112 ufds
[0].fd
= STDIN_FILENO
;
113 ufds
[0].events
= POLLIN
;
117 static int ffarg(void)
124 static void ffjmp(int n
, int rel
)
126 struct ffs
*ffs
= video
? vffs
: affs
;
127 long pos
= ffs_pos(ffs
, n
);
130 ffs_seek(affs
, pos
, frame_jmp
);
132 ffs_seek(vffs
, pos
, frame_jmp
);
135 static void printinfo(void)
137 struct ffs
*ffs
= video
? vffs
: affs
;
138 printf("fbff: %8lx\r", ffs_pos(ffs
, 0));
142 #define JMP1 (1 << 5)
143 #define JMP2 (JMP1 << 3)
144 #define JMP3 (JMP2 << 5)
146 static void execkey(void)
149 while ((c
= readkey()) != -1) {
155 ffjmp(ffarg() * JMP1
, 1);
158 ffjmp(-ffarg() * JMP1
, 1);
161 ffjmp(ffarg() * JMP2
, 1);
164 ffjmp(-ffarg() * JMP2
, 1);
167 ffjmp(ffarg() * JMP3
, 1);
170 ffjmp(-ffarg() * JMP3
, 1);
188 arg
= arg
* 10 + c
- '0';
193 /* can more video packets be read */
194 static int is_vsync(void)
196 int cur
= ffs_seq(affs
, 0);
197 int all
= ffs_seq(affs
, 1);
198 int video
= cur
? (all
- cur
) * AUDIOBUFS
/ cur
: AUDIOBUFS
;
199 /* video ffs should wait for audio ffs (ignoring buffered packets) */
200 return ffs_seq(vffs
, 1) + AUDIOBUFS
+ video
< ffs_seq(affs
, 1);
203 static void mainloop(void)
215 while (audio
&& !eof
&& !a_prodwait()) {
216 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFSZ
);
221 a_prod
= (a_prod
+ 1) & (AUDIOBUFS
- 1);
224 if (video
&& (!audio
|| eof
|| is_vsync())) {
225 int ignore
= jump
&& !(ffs_seq(vffs
, 0) % (jump
+ 1));
227 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
231 draw_frame((void *) buf
, ret
);
240 static void oss_init(void)
243 afd
= open("/dev/dsp", O_RDWR
);
245 fprintf(stderr
, "cannot open /dev/dsp\n");
248 ffs_ainfo(affs
, &rate
, &bps
, &ch
);
249 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
250 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
251 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bps
);
254 static void oss_close(void)
259 static void *process_audio(void *dat
)
263 while (!a_reset
&& a_conswait() && !exited
)
273 write(afd
, a_buf
[a_cons
], a_len
[a_cons
]);
274 a_cons
= (a_cons
+ 1) & (AUDIOBUFS
- 1);
281 static void term_setup(void)
283 struct termios newtermios
;
284 tcgetattr(STDIN_FILENO
, &termios
);
285 newtermios
= termios
;
286 newtermios
.c_lflag
&= ~ICANON
;
287 newtermios
.c_lflag
&= ~ECHO
;
288 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &newtermios
);
289 fcntl(STDIN_FILENO
, F_SETFL
, fcntl(STDIN_FILENO
, F_GETFL
) | O_NONBLOCK
);
292 static void term_cleanup(void)
294 tcsetattr(STDIN_FILENO
, 0, &termios
);
297 static void sigcont(int sig
)
302 static char *usage
= "usage: fbff [options] file\n"
304 " -m x magnify the screen by repeating pixels\n"
305 " -z x zoom the screen using ffmpeg\n"
306 " -j x jump every x video frames; for slow machines\n"
307 " -f start full screen\n"
308 " -v video only playback\n"
309 " -a audio only playback\n"
310 " -t use time based seeking; only if the default does't work\n"
311 " -R adjust the video to the right of the screen\n\n";
313 static void read_args(int argc
, char *argv
[])
317 if (!strcmp(argv
[i
], "-m"))
318 magnify
= atoi(argv
[++i
]);
319 if (!strcmp(argv
[i
], "-z"))
320 zoom
= atof(argv
[++i
]);
321 if (!strcmp(argv
[i
], "-j"))
322 jump
= atoi(argv
[++i
]);
323 if (!strcmp(argv
[i
], "-f"))
325 if (!strcmp(argv
[i
], "-a"))
327 if (!strcmp(argv
[i
], "-v"))
329 if (!strcmp(argv
[i
], "-t"))
330 frame_jmp
= 1024 / 32;
331 if (!strcmp(argv
[i
], "-h"))
333 if (!strcmp(argv
[i
], "-R"))
339 int main(int argc
, char *argv
[])
342 char *path
= argv
[argc
- 1];
344 printf("usage: %s [options] filename\n", argv
[0]);
347 read_args(argc
, argv
);
349 if (video
&& !(vffs
= ffs_alloc(path
, 1)))
351 if (audio
&& !(affs
= ffs_alloc(path
, 0)))
353 if (!video
&& !audio
)
356 pthread_create(&a_thread
, NULL
, process_audio
, NULL
);
361 ffs_vinfo(vffs
, &w
, &h
);
362 if (magnify
!= 1 && sizeof(fbval_t
) != FBM_BPP(fb_mode()))
363 fprintf(stderr
, "fbff: magnify != 1 and fbval_t doesn't match\n");
365 zoom
= (float) fb_cols() / w
/ magnify
;
366 ffs_vsetup(vffs
, zoom
, fb_mode());
369 signal(SIGCONT
, sigcont
);
378 pthread_join(a_thread
, NULL
);