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;
37 static int video
= 1; /* video stream; 0=none, 1=auto, >2=idx */
38 static int audio
= 1; /* audio stream; 0=none, 1=auto, >2=idx */
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
= 32; /* 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
);
135 ffs_seek(affs
, pos
, frame_jmp
);
137 ffs_seek(vffs
, pos
, frame_jmp
);
140 static void printinfo(void)
142 struct ffs
*ffs
= video
? vffs
: affs
;
143 printf("fbff: %8lx \t (AV: %d)\r",
144 ffs_pos(ffs
, 0), video
&& audio
? ffs_avdiff(vffs
, affs
) : 0);
148 #define JMP1 (1 << 5)
149 #define JMP2 (JMP1 << 3)
150 #define JMP3 (JMP2 << 5)
152 static void execkey(void)
155 while ((c
= readkey()) != -1) {
161 ffjmp(ffarg(1) * JMP1
, 1);
164 ffjmp(-ffarg(1) * JMP1
, 1);
167 ffjmp(ffarg(1) * JMP2
, 1);
170 ffjmp(-ffarg(1) * JMP2
, 1);
173 ffjmp(ffarg(1) * JMP3
, 1);
176 ffjmp(-ffarg(1) * JMP3
, 1);
191 sync_diff
= -ffarg(0);
194 sync_diff
= ffarg(0);
197 sync_diff
= ffs_avdiff(vffs
, affs
);
203 sync_cur
= ffarg(sync_cnt
);
210 arg
= arg
* 10 + c
- '0';
215 /* return nonzero if one more video frame can be decoded */
216 static int vsync(void)
220 return ffs_avdiff(vffs
, affs
) >= sync_diff
;
226 static void mainloop(void)
229 while (eof
< audio
+ video
) {
238 while (audio
&& !eof
&& !a_prodwait()) {
239 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFSZ
);
244 a_prod
= (a_prod
+ 1) & (AUDIOBUFS
- 1);
247 if (video
&& (!audio
|| eof
|| vsync())) {
248 int ignore
= jump
&& (vnum
++ % (jump
+ 1));
250 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
254 draw_frame((void *) buf
, ret
);
262 static void oss_init(void)
265 afd
= open("/dev/dsp", O_RDWR
);
267 fprintf(stderr
, "cannot open /dev/dsp\n");
270 ffs_ainfo(affs
, &rate
, &bps
, &ch
);
271 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
272 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
273 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bps
);
276 static void oss_close(void)
281 static void *process_audio(void *dat
)
285 while (!a_reset
&& (a_conswait() || paused
) && !exited
)
295 write(afd
, a_buf
[a_cons
], a_len
[a_cons
]);
296 a_cons
= (a_cons
+ 1) & (AUDIOBUFS
- 1);
303 static void term_setup(void)
305 struct termios newtermios
;
306 tcgetattr(STDIN_FILENO
, &termios
);
307 newtermios
= termios
;
308 newtermios
.c_lflag
&= ~ICANON
;
309 newtermios
.c_lflag
&= ~ECHO
;
310 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &newtermios
);
311 fcntl(STDIN_FILENO
, F_SETFL
, fcntl(STDIN_FILENO
, F_GETFL
) | O_NONBLOCK
);
314 static void term_cleanup(void)
316 tcsetattr(STDIN_FILENO
, 0, &termios
);
319 static void sigcont(int sig
)
324 static char *usage
= "usage: fbff [options] file\n"
326 " -z x zoom the screen using ffmpeg\n"
327 " -m x magnify the screen by repeating pixels\n"
328 " -j x jump every x video frames; for slow machines\n"
329 " -f start full screen\n"
330 " -v x select video stream; '-' disables video\n"
331 " -a x select audio stream; '-' disables audio\n"
332 " -s always synchronize; useful for files with bad video framerate\n"
333 " -t use time based seeking; only if the default does't work\n"
334 " -R adjust the video to the right of the screen\n\n";
336 static void read_args(int argc
, char *argv
[])
340 if (!strcmp(argv
[i
], "-m"))
341 magnify
= atoi(argv
[++i
]);
342 if (!strcmp(argv
[i
], "-z"))
343 zoom
= atof(argv
[++i
]);
344 if (!strcmp(argv
[i
], "-j"))
345 jump
= atoi(argv
[++i
]);
346 if (!strcmp(argv
[i
], "-f"))
348 if (!strcmp(argv
[i
], "-s"))
349 sync_cnt
= sync_cur
= (1 << 30);
350 if (!strcmp(argv
[i
], "-v"))
351 video
= argv
[++i
][0] == '-' ? 0 : atoi(argv
[i
]) + 2;
352 if (!strcmp(argv
[i
], "-a"))
353 audio
= argv
[++i
][0] == '-' ? 0 : atoi(argv
[i
]) + 2;
354 if (!strcmp(argv
[i
], "-t"))
356 if (!strcmp(argv
[i
], "-h"))
358 if (!strcmp(argv
[i
], "-R"))
364 int main(int argc
, char *argv
[])
367 char *path
= argv
[argc
- 1];
369 printf("usage: %s [options] filename\n", argv
[0]);
372 read_args(argc
, argv
);
374 if (video
&& !(vffs
= ffs_alloc(path
, FFS_VIDEO
| video
- 1)))
376 if (audio
&& !(affs
= ffs_alloc(path
, FFS_AUDIO
| audio
- 1)))
378 if (!video
&& !audio
)
381 pthread_create(&a_thread
, NULL
, process_audio
, NULL
);
386 ffs_vinfo(vffs
, &w
, &h
);
387 if (magnify
!= 1 && sizeof(fbval_t
) != FBM_BPP(fb_mode()))
388 fprintf(stderr
, "fbff: magnify != 1 and fbval_t doesn't match\n");
390 zoom
= (float) fb_cols() / w
/ magnify
;
391 ffs_vsetup(vffs
, zoom
, fb_mode());
394 signal(SIGCONT
, sigcont
);
403 pthread_join(a_thread
, NULL
);