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 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 static void mainloop(void)
196 while (eof
< audio
+ video
) {
205 while (audio
&& !eof
&& !a_prodwait()) {
206 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFSZ
);
211 a_prod
= (a_prod
+ 1) & (AUDIOBUFS
- 1);
214 if (video
&& (!audio
|| eof
|| ffs_vsync(vffs
, affs
, AUDIOBUFS
))) {
215 int ignore
= jump
&& (vnum
++ % (jump
+ 1));
217 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
221 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() || paused
) && !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
);
352 if (magnify
!= 1 && sizeof(fbval_t
) != FBM_BPP(fb_mode()))
353 fprintf(stderr
, "fbff: magnify != 1 and fbval_t doesn't match\n");
355 zoom
= (float) fb_cols() / w
/ magnify
;
356 ffs_vsetup(vffs
, zoom
, fb_mode());
359 signal(SIGCONT
, sigcont
);
368 pthread_join(a_thread
, NULL
);