2 * fbff - a small ffmpeg-based framebuffer/oss media player
4 * Copyright (C) 2009-2013 Ali Gholami Rudi
6 * This program is released under the modified BSD license.
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 */
39 static int rjust
= 0; /* justify video to the right */
40 static int bjust
= 0; /* justify video to the bottom */
41 static int frame_jmp
= 1; /* the changes to pos_cur for each frame */
43 static struct ffs
*affs
; /* audio ffmpeg stream */
44 static struct ffs
*vffs
; /* video ffmpeg stream */
45 static int afd
; /* oss fd */
46 static int vnum
; /* decoded video frame count */
48 static int sync_diff
; /* audio/video frame position diff */
49 static int sync_period
; /* sync after every this many number of frames */
50 static int sync_since
; /* frames since th last sync */
51 static int sync_cnt
= 32; /* synchronization steps */
52 static int sync_cur
; /* synchronization steps left */
53 static int sync_first
; /* first frame to record sync_diff */
55 static void stroll(void)
60 static void draw_frame(void *img
, int linelen
)
66 ffs_vinfo(vffs
, &w
, &h
);
67 nr
= MIN(h
* zoom
, fb_rows() / magnify
);
68 nc
= MIN(w
* zoom
, fb_cols() / magnify
);
69 cb
= rjust
? fb_cols() - nc
* magnify
: 0;
70 rb
= bjust
? fb_rows() - nr
* magnify
: 0;
71 for (r
= 0; r
< nr
; r
++) {
72 fbval_t
*row
= img
+ r
* linelen
;
74 fb_set(rb
+ r
, cb
, row
, nc
);
77 for (c
= 0; c
< nc
; c
++)
78 for (i
= 0; i
< magnify
; i
++)
79 buf
[c
* magnify
+ i
] = row
[c
];
80 for (i
= 0; i
< magnify
; i
++)
81 fb_set((rb
+ r
) * magnify
+ i
, cb
, buf
, nc
* magnify
);
85 static int oss_open(void)
88 afd
= open("/dev/dsp", O_WRONLY
);
91 ffs_ainfo(affs
, &rate
, &bps
, &ch
);
92 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
93 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bps
);
94 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
98 static void oss_close(void)
105 #define ABUFSZ (1 << 18)
109 static char a_buf
[AUDIOBUFS
][ABUFSZ
];
110 static int a_len
[AUDIOBUFS
];
113 static int a_conswait(void)
115 return a_cons
== a_prod
;
118 static int a_prodwait(void)
120 return ((a_prod
+ 1) & (AUDIOBUFS
- 1)) == a_cons
;
123 static void a_doreset(int pause
)
126 while (audio
&& a_reset
)
130 static int readkey(void)
133 if (read(0, &b
, 1) <= 0)
138 static void waitkey(void)
140 struct pollfd ufds
[1];
142 ufds
[0].events
= POLLIN
;
146 static int ffarg(int def
)
153 static void ffjmp(int n
, int rel
)
155 struct ffs
*ffs
= video
? vffs
: affs
;
156 long pos
= ffs_pos(ffs
, n
);
160 ffs_seek(affs
, pos
, frame_jmp
);
162 ffs_seek(vffs
, pos
, frame_jmp
);
165 static void printinfo(void)
167 struct ffs
*ffs
= video
? vffs
: affs
;
168 printf("fbff: %8lx \t (AV: %d)\r",
169 ffs_pos(ffs
, 0), video
&& audio
? ffs_avdiff(vffs
, affs
) : 0);
173 #define JMP1 (1 << 5)
174 #define JMP2 (JMP1 << 3)
175 #define JMP3 (JMP2 << 5)
177 static void execkey(void)
180 while ((c
= readkey()) != -1) {
186 ffjmp(ffarg(1) * JMP1
, 1);
189 ffjmp(-ffarg(1) * JMP1
, 1);
192 ffjmp(ffarg(1) * JMP2
, 1);
195 ffjmp(-ffarg(1) * JMP2
, 1);
198 ffjmp(ffarg(1) * JMP3
, 1);
201 ffjmp(-ffarg(1) * JMP3
, 1);
215 if (audio
&& !paused
)
221 sync_diff
= -ffarg(0);
224 sync_diff
= ffarg(0);
227 sync_diff
= ffs_avdiff(vffs
, affs
);
233 sync_cur
= ffarg(sync_cnt
);
240 arg
= arg
* 10 + c
- '0';
245 /* return nonzero if one more video frame can be decoded */
246 static int vsync(void)
248 if (sync_period
&& sync_since
++ >= sync_period
) {
254 if (sync_first
< vnum
) {
256 sync_diff
= ffs_avdiff(vffs
, affs
);
261 return ffs_avdiff(vffs
, affs
) >= sync_diff
;
267 static void mainloop(void)
270 while (eof
< audio
+ video
) {
279 while (audio
&& !eof
&& !a_prodwait()) {
280 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFSZ
);
285 a_prod
= (a_prod
+ 1) & (AUDIOBUFS
- 1);
288 if (video
&& (!audio
|| eof
|| vsync())) {
289 int ignore
= jump
&& (vnum
% (jump
+ 1));
291 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
296 draw_frame((void *) buf
, ret
);
304 static void *process_audio(void *dat
)
307 while (!a_reset
&& (a_conswait() || paused
) && !exited
)
318 write(afd
, a_buf
[a_cons
], a_len
[a_cons
]);
319 a_cons
= (a_cons
+ 1) & (AUDIOBUFS
- 1);
325 static void term_setup(void)
327 struct termios newtermios
;
328 tcgetattr(0, &termios
);
329 newtermios
= termios
;
330 newtermios
.c_lflag
&= ~ICANON
;
331 newtermios
.c_lflag
&= ~ECHO
;
332 tcsetattr(0, TCSAFLUSH
, &newtermios
);
333 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) | O_NONBLOCK
);
336 static void term_cleanup(void)
338 tcsetattr(0, 0, &termios
);
341 static void sigcont(int sig
)
346 static char *usage
= "usage: fbff [options] file\n"
348 " -z x zoom the screen using ffmpeg\n"
349 " -m x magnify the screen by repeating pixels\n"
350 " -j x jump every x video frames; for slow machines\n"
351 " -f start full screen\n"
352 " -v x select video stream; '-' disables video\n"
353 " -a x select audio stream; '-' disables audio\n"
354 " -s always synchronize; useful for files with bad video framerate\n"
355 " -u record avdiff after a few frames\n"
356 " -t use time based seeking; only if the default does't work\n"
357 " -r adjust the video to the right of the screen\n"
358 " -b adjust the video to the bottom of the screen\n\n";
360 static void read_args(int argc
, char *argv
[])
368 magnify
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
370 zoom
= c
[2] ? atof(c
+ 2) : atof(argv
[++i
]);
372 jump
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
376 sync_period
= c
[2] ? atoi(c
+ 2) : 1;
388 char *arg
= c
[2] ? c
+ 2 : argv
[++i
];
389 video
= arg
[0] == '-' ? 0 : atoi(arg
) + 2;
392 char *arg
= c
[2] ? c
+ 2 : argv
[++i
];
393 audio
= arg
[0] == '-' ? 0 : atoi(arg
) + 2;
399 int main(int argc
, char *argv
[])
402 char *path
= argv
[argc
- 1];
404 printf("usage: %s [options] filename\n", argv
[0]);
407 read_args(argc
, argv
);
409 if (video
&& !(vffs
= ffs_alloc(path
, FFS_VIDEO
| (video
- 1))))
411 if (audio
&& !(affs
= ffs_alloc(path
, FFS_AUDIO
| (audio
- 1))))
413 if (!video
&& !audio
)
418 fprintf(stderr
, "fbff: /dev/dsp busy?\n");
421 pthread_create(&a_thread
, NULL
, process_audio
, NULL
);
427 ffs_vinfo(vffs
, &w
, &h
);
428 if (magnify
!= 1 && sizeof(fbval_t
) != FBM_BPP(fb_mode()))
429 fprintf(stderr
, "fbff: magnify != 1 and fbval_t doesn't match\n");
431 float hz
= (float) fb_rows() / h
/ magnify
;
432 float wz
= (float) fb_cols() / w
/ magnify
;
433 zoom
= hz
< wz
? hz
: wz
;
435 ffs_vconf(vffs
, zoom
, fb_mode());
438 signal(SIGCONT
, sigcont
);
447 pthread_join(a_thread
, NULL
);