2 * fbff - a small ffmpeg-based framebuffer/oss media player
4 * Copyright (C) 2009-2015 Ali Gholami Rudi
6 * This program is released under the Modified BSD license.
18 #include <sys/soundcard.h>
24 #define MIN(a, b) ((a) < (b) ? (a) : (b))
25 #define MAX(a, b) ((a) > (b) ? (a) : (b))
32 static char filename
[32];
34 static float zoom
= 1;
35 static int magnify
= 1;
37 static int fullscreen
= 0;
38 static int video
= 1; /* video stream; 0=none, 1=auto, >2=idx */
39 static int audio
= 1; /* audio stream; 0=none, 1=auto, >2=idx */
40 static int rjust
= 0; /* justify video to the right */
41 static int bjust
= 0; /* justify video to the bottom */
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 */
47 static long mark
[256]; /* marks */
49 static int sync_diff
; /* audio/video frame position diff */
50 static int sync_period
; /* sync after every this many number of frames */
51 static int sync_since
; /* frames since th last sync */
52 static int sync_cnt
= 32; /* synchronization steps */
53 static int sync_cur
; /* synchronization steps left */
54 static int sync_first
; /* first frame to record sync_diff */
56 static void stroll(void)
61 static void draw_frame(void *img
, int linelen
)
67 ffs_vinfo(vffs
, &w
, &h
);
68 nr
= MIN(h
* zoom
, fb_rows() / magnify
);
69 nc
= MIN(w
* zoom
, fb_cols() / magnify
);
70 cb
= rjust
? fb_cols() - nc
* magnify
: 0;
71 rb
= bjust
? fb_rows() - nr
* magnify
: 0;
72 for (r
= 0; r
< nr
; r
++) {
73 fbval_t
*row
= img
+ r
* linelen
;
75 fb_set(rb
+ r
, cb
, row
, nc
);
78 for (c
= 0; c
< nc
; c
++)
79 for (i
= 0; i
< magnify
; i
++)
80 buf
[c
* magnify
+ i
] = row
[c
];
81 for (i
= 0; i
< magnify
; i
++)
82 fb_set((rb
+ r
) * magnify
+ i
, cb
, buf
, nc
* magnify
);
86 static int oss_open(void)
89 afd
= open("/dev/dsp", O_WRONLY
);
92 ffs_ainfo(affs
, &rate
, &bps
, &ch
);
93 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
94 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bps
);
95 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
99 static void oss_close(void)
106 #define ABUFSZ (1 << 18)
110 static char a_buf
[AUDIOBUFS
][ABUFSZ
];
111 static int a_len
[AUDIOBUFS
];
114 static int a_conswait(void)
116 return a_cons
== a_prod
;
119 static int a_prodwait(void)
121 return ((a_prod
+ 1) & (AUDIOBUFS
- 1)) == a_cons
;
124 static void a_doreset(int pause
)
127 while (audio
&& a_reset
)
131 static int cmdread(void)
134 if (read(0, &b
, 1) <= 0)
139 static void cmdwait(void)
141 struct pollfd ufds
[1];
143 ufds
[0].events
= POLLIN
;
147 static void cmdjmp(int n
, int rel
)
149 struct ffs
*ffs
= video
? vffs
: affs
;
150 long pos
= (rel
? ffs_pos(ffs
) : 0) + n
* 1000;
156 mark
['\''] = ffs_pos(ffs
);
158 ffs_seek(affs
, ffs
, pos
);
160 ffs_seek(vffs
, ffs
, pos
);
163 static void cmdinfo(void)
165 struct ffs
*ffs
= video
? vffs
: affs
;
166 long pos
= ffs_pos(ffs
);
167 long percent
= ffs_duration(ffs
) ? pos
* 1000 / ffs_duration(ffs
) : 0;
168 printf("%c %3ld.%01ld%% %3ld:%02ld.%01ld (AV:%4d) [%s] \r",
169 paused
? (afd
< 0 ? '*' : ' ') : '>',
170 percent
/ 10, percent
% 10,
171 pos
/ 60000, (pos
% 60000) / 1000, (pos
% 1000) / 100,
172 video
&& audio
? ffs_avdiff(vffs
, affs
) : 0,
177 static int cmdarg(int def
)
184 static void cmdexec(void)
187 while ((c
= cmdread()) >= 0) {
190 mark
[c
] = ffs_pos(video
? vffs
: affs
);
196 cmdjmp(mark
[c
] / 1000, 0);
204 cmdjmp(cmdarg(1) * 10, 1);
207 cmdjmp(-cmdarg(1) * 10, 1);
210 cmdjmp(cmdarg(1) * 60, 1);
213 cmdjmp(-cmdarg(1) * 60, 1);
216 cmdjmp(cmdarg(1) * 600, 1);
219 cmdjmp(-cmdarg(1) * 600, 1);
222 cmdjmp(cmdarg(0) * 60, 0);
225 cmdjmp(cmdarg(0) * ffs_duration(vffs
? vffs
: affs
) / 100000, 0);
241 if (audio
&& !paused
)
247 sync_diff
= -cmdarg(0);
250 sync_diff
= cmdarg(0);
253 sync_diff
= ffs_avdiff(vffs
, affs
);
256 sync_cnt
= cmdarg(0);
259 sync_cur
= cmdarg(sync_cnt
);
266 arg
= arg
* 10 + c
- '0';
271 /* return nonzero if one more video frame can be decoded */
272 static int vsync(void)
274 if (sync_period
&& sync_since
++ >= sync_period
) {
280 if (sync_first
< vnum
) {
282 sync_diff
= ffs_avdiff(vffs
, affs
);
287 return ffs_avdiff(vffs
, affs
) >= sync_diff
;
293 static void mainloop(void)
296 while (eof
< audio
+ video
) {
305 while (audio
&& !eof
&& !a_prodwait()) {
306 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFSZ
);
311 a_prod
= (a_prod
+ 1) & (AUDIOBUFS
- 1);
314 if (video
&& (!audio
|| eof
|| vsync())) {
315 int ignore
= jump
&& (vnum
% (jump
+ 1));
317 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
322 draw_frame((void *) buf
, ret
);
330 static void *process_audio(void *dat
)
333 while (!a_reset
&& (a_conswait() || paused
) && !exited
)
344 write(afd
, a_buf
[a_cons
], a_len
[a_cons
]);
345 a_cons
= (a_cons
+ 1) & (AUDIOBUFS
- 1);
351 static char *usage
= "usage: fbff [options] file\n"
353 " -z x zoom the video\n"
354 " -m x magnify the video by duplicating pixels\n"
355 " -j x jump every x video frames; for slow machines\n"
356 " -f start full screen\n"
357 " -v x select video stream; '-' disables video\n"
358 " -a x select audio stream; '-' disables audio\n"
359 " -s always synchronize (-sx for every x frames)\n"
360 " -u record A/V delay after the first few frames\n"
361 " -r adjust the video to the right of the screen\n"
362 " -b adjust the video to the bottom of the screen\n\n";
364 static void read_args(int argc
, char *argv
[])
372 magnify
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
374 zoom
= c
[2] ? atof(c
+ 2) : atof(argv
[++i
]);
376 jump
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
380 sync_period
= c
[2] ? atoi(c
+ 2) : 1;
390 char *arg
= c
[2] ? c
+ 2 : argv
[++i
];
391 video
= arg
[0] == '-' ? 0 : atoi(arg
) + 2;
394 char *arg
= c
[2] ? c
+ 2 : argv
[++i
];
395 audio
= arg
[0] == '-' ? 0 : atoi(arg
) + 2;
401 static void term_init(struct termios
*termios
)
403 struct termios newtermios
;
404 tcgetattr(0, termios
);
405 newtermios
= *termios
;
406 newtermios
.c_lflag
&= ~ICANON
;
407 newtermios
.c_lflag
&= ~ECHO
;
408 tcsetattr(0, TCSAFLUSH
, &newtermios
);
409 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) | O_NONBLOCK
);
412 static void term_done(struct termios
*termios
)
414 tcsetattr(0, 0, termios
);
417 int main(int argc
, char *argv
[])
419 struct termios termios
;
421 char *path
= argv
[argc
- 1];
423 printf("usage: %s [-u -s60 ...] file\n", argv
[0]);
426 read_args(argc
, argv
);
428 snprintf(filename
, sizeof(filename
), "%s", path
);
429 if (video
&& !(vffs
= ffs_alloc(path
, FFS_VIDEO
| (video
- 1))))
431 if (audio
&& !(affs
= ffs_alloc(path
, FFS_AUDIO
| (audio
- 1))))
433 if (!video
&& !audio
)
438 fprintf(stderr
, "fbff: /dev/dsp busy?\n");
441 pthread_create(&a_thread
, NULL
, process_audio
, NULL
);
447 ffs_vinfo(vffs
, &w
, &h
);
448 if (magnify
!= 1 && sizeof(fbval_t
) != FBM_BPP(fb_mode()))
449 fprintf(stderr
, "fbff: magnify != 1 and fbval_t doesn't match\n");
451 float hz
= (float) fb_rows() / h
/ magnify
;
452 float wz
= (float) fb_cols() / w
/ magnify
;
453 zoom
= hz
< wz
? hz
: wz
;
455 ffs_vconf(vffs
, zoom
, fb_mode());
467 pthread_join(a_thread
, NULL
);