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
; /* synchronize if nonzero */
50 static void stroll(void)
55 static void draw_frame(void *img
, int linelen
)
61 ffs_vinfo(vffs
, &w
, &h
);
62 nr
= MIN(h
* zoom
, fb_rows() / magnify
);
63 nc
= MIN(w
* zoom
, fb_cols() / magnify
);
64 cb
= just
? fb_cols() - nc
* magnify
: 0;
65 for (r
= 0; r
< nr
; r
++) {
66 fbval_t
*row
= img
+ r
* linelen
;
68 fb_set(r
, cb
, row
, nc
);
71 for (c
= 0; c
< nc
; c
++)
72 for (i
= 0; i
< magnify
; i
++)
73 buf
[c
* magnify
+ i
] = row
[c
];
74 for (i
= 0; i
< magnify
; i
++)
75 fb_set(r
* magnify
+ i
, cb
, buf
, nc
* magnify
);
79 #define ABUFSZ (1 << 18)
83 static char a_buf
[AUDIOBUFS
][ABUFSZ
];
84 static int a_len
[AUDIOBUFS
];
87 static int a_conswait(void)
89 return a_cons
== a_prod
;
92 static int a_prodwait(void)
94 return ((a_prod
+ 1) & (AUDIOBUFS
- 1)) == a_cons
;
97 static void a_doreset(int pause
)
100 while (audio
&& a_reset
)
104 static int readkey(void)
107 if (read(STDIN_FILENO
, &b
, 1) <= 0)
112 static void waitkey(void)
114 struct pollfd ufds
[1];
115 ufds
[0].fd
= STDIN_FILENO
;
116 ufds
[0].events
= POLLIN
;
120 static int ffarg(int def
)
127 static void ffjmp(int n
, int rel
)
129 struct ffs
*ffs
= video
? vffs
: affs
;
130 long pos
= ffs_pos(ffs
, n
);
133 ffs_seek(affs
, pos
, frame_jmp
);
135 ffs_seek(vffs
, pos
, frame_jmp
);
138 static void printinfo(void)
140 struct ffs
*ffs
= video
? vffs
: affs
;
141 printf("fbff: %8lx \t (AV: %d)\r",
142 ffs_pos(ffs
, 0), ffs_avdiff(vffs
, affs
));
146 #define JMP1 (1 << 5)
147 #define JMP2 (JMP1 << 3)
148 #define JMP3 (JMP2 << 5)
150 static void execkey(void)
153 while ((c
= readkey()) != -1) {
159 ffjmp(ffarg(1) * JMP1
, 1);
162 ffjmp(-ffarg(1) * JMP1
, 1);
165 ffjmp(ffarg(1) * JMP2
, 1);
168 ffjmp(-ffarg(1) * JMP2
, 1);
171 ffjmp(ffarg(1) * JMP3
, 1);
174 ffjmp(-ffarg(1) * JMP3
, 1);
189 sync_diff
= -ffarg(0);
192 sync_diff
= ffarg(0);
195 sync_diff
= ffs_avdiff(vffs
, affs
);
198 sync_cnt
= ffarg(10);
205 arg
= arg
* 10 + c
- '0';
210 /* return nonzero if one more video frame can be decoded */
211 static int vsync(void)
215 return ffs_avdiff(vffs
, affs
) >= sync_diff
;
221 static void mainloop(void)
224 while (eof
< audio
+ video
) {
233 while (audio
&& !eof
&& !a_prodwait()) {
234 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFSZ
);
239 a_prod
= (a_prod
+ 1) & (AUDIOBUFS
- 1);
242 if (video
&& (!audio
|| eof
|| vsync())) {
243 int ignore
= jump
&& (vnum
++ % (jump
+ 1));
245 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
249 draw_frame((void *) buf
, ret
);
257 static void oss_init(void)
260 afd
= open("/dev/dsp", O_RDWR
);
262 fprintf(stderr
, "cannot open /dev/dsp\n");
265 ffs_ainfo(affs
, &rate
, &bps
, &ch
);
266 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
267 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
268 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bps
);
271 static void oss_close(void)
276 static void *process_audio(void *dat
)
280 while (!a_reset
&& (a_conswait() || paused
) && !exited
)
290 write(afd
, a_buf
[a_cons
], a_len
[a_cons
]);
291 a_cons
= (a_cons
+ 1) & (AUDIOBUFS
- 1);
298 static void term_setup(void)
300 struct termios newtermios
;
301 tcgetattr(STDIN_FILENO
, &termios
);
302 newtermios
= termios
;
303 newtermios
.c_lflag
&= ~ICANON
;
304 newtermios
.c_lflag
&= ~ECHO
;
305 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &newtermios
);
306 fcntl(STDIN_FILENO
, F_SETFL
, fcntl(STDIN_FILENO
, F_GETFL
) | O_NONBLOCK
);
309 static void term_cleanup(void)
311 tcsetattr(STDIN_FILENO
, 0, &termios
);
314 static void sigcont(int sig
)
319 static char *usage
= "usage: fbff [options] file\n"
321 " -m x magnify the screen by repeating pixels\n"
322 " -z x zoom the screen using ffmpeg\n"
323 " -j x jump every x video frames; for slow machines\n"
324 " -f start full screen\n"
325 " -v video only playback\n"
326 " -a audio only playback\n"
327 " -t use time based seeking; only if the default does't work\n"
328 " -R adjust the video to the right of the screen\n\n";
330 static void read_args(int argc
, char *argv
[])
334 if (!strcmp(argv
[i
], "-m"))
335 magnify
= atoi(argv
[++i
]);
336 if (!strcmp(argv
[i
], "-z"))
337 zoom
= atof(argv
[++i
]);
338 if (!strcmp(argv
[i
], "-j"))
339 jump
= atoi(argv
[++i
]);
340 if (!strcmp(argv
[i
], "-f"))
342 if (!strcmp(argv
[i
], "-a"))
344 if (!strcmp(argv
[i
], "-v"))
346 if (!strcmp(argv
[i
], "-t"))
347 frame_jmp
= 1024 / 32;
348 if (!strcmp(argv
[i
], "-h"))
350 if (!strcmp(argv
[i
], "-R"))
356 int main(int argc
, char *argv
[])
359 char *path
= argv
[argc
- 1];
361 printf("usage: %s [options] filename\n", argv
[0]);
364 read_args(argc
, argv
);
366 if (video
&& !(vffs
= ffs_alloc(path
, 1)))
368 if (audio
&& !(affs
= ffs_alloc(path
, 0)))
370 if (!video
&& !audio
)
373 pthread_create(&a_thread
, NULL
, process_audio
, NULL
);
378 ffs_vinfo(vffs
, &w
, &h
);
379 if (magnify
!= 1 && sizeof(fbval_t
) != FBM_BPP(fb_mode()))
380 fprintf(stderr
, "fbff: magnify != 1 and fbval_t doesn't match\n");
382 zoom
= (float) fb_cols() / w
/ magnify
;
383 ffs_vsetup(vffs
, zoom
, fb_mode());
386 signal(SIGCONT
, sigcont
);
395 pthread_join(a_thread
, NULL
);