2 * fbff - a small ffmpeg-based framebuffer/oss media player
4 * Copyright (C) 2009-2021 Ali Gholami Rudi
6 * This program is released under the Modified BSD license.
19 #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, >1:idx */
39 static int audio
= 1; /* audio stream; 0:none, 1:auto, >1:idx */
40 static int posx
, posy
; /* video position */
41 static int rjust
, bjust
; /* justify video to screen right/bottom */
42 static int nodraw
; /* stop drawing */
43 static char *ossdsp
; /* OSS device */
45 static struct ffs
*affs
; /* audio ffmpeg stream */
46 static struct ffs
*vffs
; /* video ffmpeg stream */
47 static int afd
; /* oss fd */
48 static int vnum
; /* decoded video frame count */
49 static long mark
[256]; /* marks */
51 static int sync_diff
; /* audio/video frame position diff */
52 static int sync_period
; /* sync after every this many number of frames */
53 static int sync_since
; /* frames since th last sync */
54 static int sync_cnt
= 32; /* synchronization steps */
55 static int sync_cur
; /* synchronization steps left */
56 static int sync_first
; /* first frame to record sync_diff */
58 static void stroll(void)
63 static void draw_row(int rb
, int cb
, void *img
, int cn
)
65 int bpp
= FBM_BPP(fb_mode());
66 if (rb
< 0 || rb
>= fb_rows())
69 cn
= -cb
< cn
? cn
+ cb
: 0;
73 if (cb
+ cn
>= fb_cols())
74 cn
= cb
< fb_cols() ? fb_cols() - cb
: 0;
75 memcpy(fb_mem(rb
) + cb
* bpp
, img
, cn
* bpp
);
78 static void draw_frame(void *img
, int linelen
)
80 int w
, h
, rn
, cn
, cb
, rb
;
82 int bpp
= FBM_BPP(fb_mode());
85 ffs_vinfo(vffs
, &w
, &h
);
88 cb
= rjust
? fb_cols() - cn
* magnify
+ posx
: posx
;
89 rb
= bjust
? fb_rows() - rn
* magnify
+ posy
: posy
;
91 for (r
= 0; r
< rn
; r
++)
92 draw_row(rb
+ r
, cb
, img
+ r
* linelen
, cn
);
94 char *brow
= malloc(cn
* magnify
* bpp
);
95 for (r
= 0; r
< rn
; r
++) {
96 char *src
= img
+ r
* linelen
;
98 for (c
= 0; c
< cn
; c
++)
99 for (i
= 0; i
< magnify
; i
++)
100 for (k
= 0; k
< bpp
; k
++)
101 *dst
++ = src
[c
* bpp
+ k
];
102 for (i
= 0; i
< magnify
; i
++)
103 draw_row((rb
+ r
) * magnify
+ i
, cb
, brow
, cn
* magnify
);
109 static int oss_open(void)
112 int frag
= 0x0003000b; /* 0xmmmmssss: 2^m fragments of size 2^s each */
113 afd
= open(ossdsp
? ossdsp
: "/dev/dsp", O_WRONLY
);
116 ffs_ainfo(affs
, &rate
, &bps
, &ch
);
117 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
118 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bps
);
119 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
120 ioctl(afd
, SOUND_PCM_SETFRAGMENT
, &frag
);
124 static void oss_close(void)
133 #define ABUFCNT (1 << 3) /* number of audio buffers */
134 #define ABUFLEN (1 << 18) /* audio buffer length */
138 static char a_buf
[ABUFCNT
][ABUFLEN
];
139 static int a_len
[ABUFCNT
];
142 static int a_conswait(void)
144 return a_cons
== a_prod
;
147 static int a_prodwait(void)
149 return ((a_prod
+ 1) & (ABUFCNT
- 1)) == a_cons
;
152 static void a_doreset(int pause
)
155 while (audio
&& a_reset
)
159 /* subtitle handling */
161 #define SUBSCNT 2048 /* number of subtitles */
162 #define SUBSLEN 80 /* maximum subtitle length */
164 static char *sub_path
; /* subtitles file */
165 static char sub_text
[SUBSCNT
][SUBSLEN
]; /* subtitle text */
166 static long sub_beg
[SUBSCNT
]; /* printing position */
167 static long sub_end
[SUBSCNT
]; /* hiding position */
168 static int sub_n
; /* subtitle count */
169 static int sub_last
; /* last printed subtitle */
171 static void sub_read(void)
173 struct ffs
*sffs
= ffs_alloc(sub_path
, FFS_SUBTS
);
176 while (sub_n
< SUBSCNT
&& !ffs_sdec(sffs
, sub_text
[sub_n
], SUBSLEN
,
177 &sub_beg
[sub_n
], &sub_end
[sub_n
])) {
183 static void sub_print(void)
185 struct ffs
*ffs
= video
? vffs
: affs
;
188 long pos
= ffs_pos(ffs
);
190 int m
= (l
+ h
) >> 1;
191 if (pos
>= sub_beg
[m
] && pos
<= sub_end
[m
]) {
193 printf("\r\33[K%s", sub_text
[m
]);
198 if (pos
< sub_beg
[m
])
212 static int cmdread(void)
215 if (read(0, &b
, 1) <= 0)
220 static void cmdwait(void)
222 struct pollfd ufds
[1];
224 ufds
[0].events
= POLLIN
;
228 static void cmdjmp(int n
, int rel
)
230 struct ffs
*ffs
= video
? vffs
: affs
;
231 long pos
= (rel
? ffs_pos(ffs
) : 0) + n
* 1000;
237 mark
['\''] = ffs_pos(ffs
);
239 ffs_seek(affs
, ffs
, pos
);
241 ffs_seek(vffs
, ffs
, pos
);
244 static void cmdinfo(void)
246 struct ffs
*ffs
= video
? vffs
: affs
;
247 long pos
= ffs_pos(ffs
);
248 long percent
= ffs_duration(ffs
) ? pos
* 10 / (ffs_duration(ffs
) / 100) : 0;
249 printf("\r\33[K%c %3ld.%01ld%% %3ld:%02ld.%01ld (AV:%4d) [%s] \r",
250 paused
? (afd
< 0 ? '*' : ' ') : '>',
251 percent
/ 10, percent
% 10,
252 pos
/ 60000, (pos
% 60000) / 1000, (pos
% 1000) / 100,
253 video
&& audio
? ffs_avdiff(vffs
, affs
) : 0,
258 static int cmdarg(int def
)
265 static void cmdexec(void)
268 while ((c
= cmdread()) >= 0) {
271 mark
[c
] = ffs_pos(video
? vffs
: affs
);
277 cmdjmp(mark
[c
] / 1000, 0);
285 cmdjmp(cmdarg(1) * 10, 1);
288 cmdjmp(-cmdarg(1) * 10, 1);
291 cmdjmp(cmdarg(1) * 60, 1);
294 cmdjmp(-cmdarg(1) * 60, 1);
297 cmdjmp(cmdarg(1) * 600, 1);
300 cmdjmp(-cmdarg(1) * 600, 1);
303 cmdjmp(cmdarg(0) * 60, 0);
306 cmdjmp(cmdarg(0) * ffs_duration(vffs
? vffs
: affs
) / 100000, 0);
322 if (audio
&& !paused
)
328 sync_diff
= -cmdarg(0);
331 sync_diff
= cmdarg(0);
334 sync_diff
= ffs_avdiff(vffs
, affs
);
337 sync_cnt
= cmdarg(0);
340 sync_cur
= cmdarg(sync_cnt
);
347 arg
= arg
* 10 + c
- '0';
352 /* return nonzero if one more video frame can be decoded */
353 static int vsync(void)
355 if (sync_period
&& sync_since
++ >= sync_period
) {
361 if (sync_first
< vnum
) {
363 sync_diff
= ffs_avdiff(vffs
, affs
);
368 return ffs_avdiff(vffs
, affs
) >= sync_diff
;
374 static void mainloop(void)
377 while (eof
< audio
+ video
) {
386 while (audio
&& !eof
&& !a_prodwait()) {
387 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFLEN
);
392 a_prod
= (a_prod
+ 1) & (ABUFCNT
- 1);
395 if (video
&& (!audio
|| eof
|| vsync())) {
396 int ignore
= jump
&& (vnum
% (jump
+ 1));
398 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
403 draw_frame((void *) buf
, ret
);
412 static void *process_audio(void *dat
)
415 while (!a_reset
&& (a_conswait() || paused
) && !exited
)
426 write(afd
, a_buf
[a_cons
], a_len
[a_cons
]);
427 a_cons
= (a_cons
+ 1) & (ABUFCNT
- 1);
433 static char *usage
= "usage: fbff [options] file\n"
435 " -z n zoom the video\n"
436 " -m n magnify the video by duplicating pixels\n"
437 " -j n jump every n video frames; for slow machines\n"
438 " -f start full screen\n"
439 " -v n select video stream; '-' disables video\n"
440 " -a n select audio stream; '-' disables audio\n"
441 " -s always synchronize (-sx for every x frames)\n"
442 " -u record A/V delay after the first few frames\n"
443 " -t path subtitles file\n"
444 " -x n horizontal video position\n"
445 " -y n vertical video position\n"
446 " -r adjust the video to the right of the screen\n"
447 " -b adjust the video to the bottom of the screen\n\n";
449 static void read_args(int argc
, char *argv
[])
457 magnify
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
459 zoom
= c
[2] ? atof(c
+ 2) : atof(argv
[++i
]);
461 jump
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
465 sync_period
= c
[2] ? atoi(c
+ 2) : 1;
467 sub_path
= c
[2] ? c
+ 2 : argv
[++i
];
471 posx
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
473 posy
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
481 char *arg
= c
[2] ? c
+ 2 : argv
[++i
];
482 video
= arg
[0] == '-' ? 0 : atoi(arg
) + 2;
485 char *arg
= c
[2] ? c
+ 2 : argv
[++i
];
486 audio
= arg
[0] == '-' ? 0 : atoi(arg
) + 2;
492 static void term_init(struct termios
*termios
)
494 struct termios newtermios
;
495 tcgetattr(0, termios
);
496 newtermios
= *termios
;
497 newtermios
.c_lflag
&= ~ICANON
;
498 newtermios
.c_lflag
&= ~ECHO
;
499 tcsetattr(0, TCSAFLUSH
, &newtermios
);
500 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) | O_NONBLOCK
);
503 static void term_done(struct termios
*termios
)
505 tcsetattr(0, 0, termios
);
508 static void signalreceived(int sig
)
516 int main(int argc
, char *argv
[])
518 struct termios termios
;
520 char *path
= argv
[argc
- 1];
521 char *fbdev
= getenv("FBDEV");
523 printf("usage: %s [-u -s60 ...] file\n", argv
[0]);
526 ossdsp
= getenv("OSSDSP");
527 read_args(argc
, argv
);
529 snprintf(filename
, sizeof(filename
), "%s", path
);
530 if (video
&& !(vffs
= ffs_alloc(path
, FFS_VIDEO
| (video
- 1))))
532 if (audio
&& !(affs
= ffs_alloc(path
, FFS_AUDIO
| (audio
- 1))))
534 if (!video
&& !audio
)
541 fprintf(stderr
, "fbff: /dev/dsp busy?\n");
544 pthread_create(&a_thread
, NULL
, process_audio
, NULL
);
550 ffs_vinfo(vffs
, &w
, &h
);
552 float hz
= (float) fb_rows() / h
/ magnify
;
553 float wz
= (float) fb_cols() / w
/ magnify
;
554 zoom
= hz
< wz
? hz
: wz
;
556 ffs_vconf(vffs
, zoom
, fb_mode());
558 if (getenv("TERM_PGID") != NULL
&& atoi(getenv("TERM_PGID")) == getppid())
559 if (tcsetpgrp(0, getppid()) == 0)
560 setpgid(0, getppid());
562 signal(SIGUSR1
, signalreceived
);
563 signal(SIGUSR2
, signalreceived
);
573 pthread_join(a_thread
, NULL
);