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>
23 #define MIN(a, b) ((a) < (b) ? (a) : (b))
24 #define MAX(a, b) ((a) > (b) ? (a) : (b))
26 typedef unsigned int fbval_t
; /* framebuffer depth */
33 static char filename
[32];
35 static float zoom
= 1;
36 static int magnify
= 1;
38 static int fullscreen
= 0;
39 static int video
= 1; /* video stream; 0:none, 1:auto, >1:idx */
40 static int audio
= 1; /* audio stream; 0:none, 1:auto, >1:idx */
41 static int posx
, posy
; /* video position */
42 static int rjust
, bjust
; /* justify video to screen right/bottom */
44 static struct ffs
*affs
; /* audio ffmpeg stream */
45 static struct ffs
*vffs
; /* video ffmpeg stream */
46 static int afd
; /* oss fd */
47 static int vnum
; /* decoded video frame count */
48 static long mark
[256]; /* marks */
50 static int sync_diff
; /* audio/video frame position diff */
51 static int sync_period
; /* sync after every this many number of frames */
52 static int sync_since
; /* frames since th last sync */
53 static int sync_cnt
= 32; /* synchronization steps */
54 static int sync_cur
; /* synchronization steps left */
55 static int sync_first
; /* first frame to record sync_diff */
57 static void stroll(void)
62 static void draw_row(int rb
, int cb
, void *img
, int cn
)
64 if (rb
< 0 || rb
>= fb_rows())
67 cn
= -cb
< cn
? cn
+ cb
: 0;
71 if (cb
+ cn
>= fb_cols())
72 cn
= cb
< fb_cols() ? fb_cols() - cb
: 0;
73 fb_set(rb
, cb
, img
, cn
);
76 static void draw_frame(void *img
, int linelen
)
78 int w
, h
, rn
, cn
, cb
, rb
;
80 ffs_vinfo(vffs
, &w
, &h
);
83 cb
= rjust
? fb_cols() - cn
* magnify
+ posx
: posx
;
84 rb
= bjust
? fb_rows() - rn
* magnify
+ posy
: posy
;
86 for (r
= 0; r
< rn
; r
++)
87 draw_row(rb
+ r
, cb
, img
+ r
* linelen
, cn
);
89 fbval_t
*brow
= malloc(cn
* magnify
* sizeof(fbval_t
));
90 for (r
= 0; r
< rn
; r
++) {
91 fbval_t
*row
= img
+ r
* linelen
;
92 for (c
= 0; c
< cn
; c
++)
93 for (i
= 0; i
< magnify
; i
++)
94 brow
[c
* magnify
+ i
] = row
[c
];
95 for (i
= 0; i
< magnify
; i
++)
96 draw_row((rb
+ r
) * magnify
+ i
, cb
, brow
, cn
* magnify
);
102 static int oss_open(void)
105 afd
= open("/dev/dsp", O_WRONLY
);
108 ffs_ainfo(affs
, &rate
, &bps
, &ch
);
109 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
110 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bps
);
111 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
115 static void oss_close(void)
124 #define ABUFCNT (1 << 3) /* number of audio buffers */
125 #define ABUFLEN (1 << 18) /* audio buffer length */
129 static char a_buf
[ABUFCNT
][ABUFLEN
];
130 static int a_len
[ABUFCNT
];
133 static int a_conswait(void)
135 return a_cons
== a_prod
;
138 static int a_prodwait(void)
140 return ((a_prod
+ 1) & (ABUFCNT
- 1)) == a_cons
;
143 static void a_doreset(int pause
)
146 while (audio
&& a_reset
)
150 /* subtitle handling */
152 #define SUBSCNT 2048 /* number of subtitles */
153 #define SUBSLEN 80 /* maximum subtitle length */
155 static char *sub_path
; /* subtitles file */
156 static char sub_text
[SUBSCNT
][SUBSLEN
]; /* subtitle text */
157 static long sub_beg
[SUBSCNT
]; /* printing position */
158 static long sub_end
[SUBSCNT
]; /* hiding position */
159 static int sub_n
; /* subtitle count */
160 static int sub_last
; /* last printed subtitle */
162 static void sub_read(void)
164 struct ffs
*sffs
= ffs_alloc(sub_path
, FFS_SUBTS
);
167 while (sub_n
< SUBSCNT
&& !ffs_sdec(sffs
, &sub_text
[sub_n
][0], SUBSLEN
,
168 &sub_beg
[sub_n
], &sub_end
[sub_n
])) {
174 static void sub_print(void)
176 struct ffs
*ffs
= video
? vffs
: affs
;
179 long pos
= ffs_pos(ffs
);
181 int m
= (l
+ h
) >> 1;
182 if (pos
>= sub_beg
[m
] && pos
<= sub_end
[m
]) {
184 printf("\r\33[K%s", sub_text
[m
]);
189 if (pos
< sub_beg
[m
])
203 static int cmdread(void)
206 if (read(0, &b
, 1) <= 0)
211 static void cmdwait(void)
213 struct pollfd ufds
[1];
215 ufds
[0].events
= POLLIN
;
219 static void cmdjmp(int n
, int rel
)
221 struct ffs
*ffs
= video
? vffs
: affs
;
222 long pos
= (rel
? ffs_pos(ffs
) : 0) + n
* 1000;
228 mark
['\''] = ffs_pos(ffs
);
230 ffs_seek(affs
, ffs
, pos
);
232 ffs_seek(vffs
, ffs
, pos
);
235 static void cmdinfo(void)
237 struct ffs
*ffs
= video
? vffs
: affs
;
238 long pos
= ffs_pos(ffs
);
239 long percent
= ffs_duration(ffs
) ? pos
* 10 / (ffs_duration(ffs
) / 100) : 0;
240 printf("\r\33[K%c %3ld.%01ld%% %3ld:%02ld.%01ld (AV:%4d) [%s] \r",
241 paused
? (afd
< 0 ? '*' : ' ') : '>',
242 percent
/ 10, percent
% 10,
243 pos
/ 60000, (pos
% 60000) / 1000, (pos
% 1000) / 100,
244 video
&& audio
? ffs_avdiff(vffs
, affs
) : 0,
249 static int cmdarg(int def
)
256 static void cmdexec(void)
259 while ((c
= cmdread()) >= 0) {
262 mark
[c
] = ffs_pos(video
? vffs
: affs
);
268 cmdjmp(mark
[c
] / 1000, 0);
276 cmdjmp(cmdarg(1) * 10, 1);
279 cmdjmp(-cmdarg(1) * 10, 1);
282 cmdjmp(cmdarg(1) * 60, 1);
285 cmdjmp(-cmdarg(1) * 60, 1);
288 cmdjmp(cmdarg(1) * 600, 1);
291 cmdjmp(-cmdarg(1) * 600, 1);
294 cmdjmp(cmdarg(0) * 60, 0);
297 cmdjmp(cmdarg(0) * ffs_duration(vffs
? vffs
: affs
) / 100000, 0);
313 if (audio
&& !paused
)
319 sync_diff
= -cmdarg(0);
322 sync_diff
= cmdarg(0);
325 sync_diff
= ffs_avdiff(vffs
, affs
);
328 sync_cnt
= cmdarg(0);
331 sync_cur
= cmdarg(sync_cnt
);
338 arg
= arg
* 10 + c
- '0';
343 /* return nonzero if one more video frame can be decoded */
344 static int vsync(void)
346 if (sync_period
&& sync_since
++ >= sync_period
) {
352 if (sync_first
< vnum
) {
354 sync_diff
= ffs_avdiff(vffs
, affs
);
359 return ffs_avdiff(vffs
, affs
) >= sync_diff
;
365 static void mainloop(void)
368 while (eof
< audio
+ video
) {
377 while (audio
&& !eof
&& !a_prodwait()) {
378 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFLEN
);
383 a_prod
= (a_prod
+ 1) & (ABUFCNT
- 1);
386 if (video
&& (!audio
|| eof
|| vsync())) {
387 int ignore
= jump
&& (vnum
% (jump
+ 1));
389 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
394 draw_frame((void *) buf
, ret
);
403 static void *process_audio(void *dat
)
406 while (!a_reset
&& (a_conswait() || paused
) && !exited
)
417 write(afd
, a_buf
[a_cons
], a_len
[a_cons
]);
418 a_cons
= (a_cons
+ 1) & (ABUFCNT
- 1);
424 static char *usage
= "usage: fbff [options] file\n"
426 " -z n zoom the video\n"
427 " -m n magnify the video by duplicating pixels\n"
428 " -j n jump every n video frames; for slow machines\n"
429 " -f start full screen\n"
430 " -v n select video stream; '-' disables video\n"
431 " -a n select audio stream; '-' disables audio\n"
432 " -s always synchronize (-sx for every x frames)\n"
433 " -u record A/V delay after the first few frames\n"
434 " -t path subtitles file\n"
435 " -x n horizontal video position\n"
436 " -y n vertical video position\n"
437 " -r adjust the video to the right of the screen\n"
438 " -b adjust the video to the bottom of the screen\n\n";
440 static void read_args(int argc
, char *argv
[])
448 magnify
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
450 zoom
= c
[2] ? atof(c
+ 2) : atof(argv
[++i
]);
452 jump
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
456 sync_period
= c
[2] ? atoi(c
+ 2) : 1;
458 sub_path
= c
[2] ? c
+ 2 : argv
[++i
];
462 posx
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
464 posy
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
472 char *arg
= c
[2] ? c
+ 2 : argv
[++i
];
473 video
= arg
[0] == '-' ? 0 : atoi(arg
) + 2;
476 char *arg
= c
[2] ? c
+ 2 : argv
[++i
];
477 audio
= arg
[0] == '-' ? 0 : atoi(arg
) + 2;
483 static void term_init(struct termios
*termios
)
485 struct termios newtermios
;
486 tcgetattr(0, termios
);
487 newtermios
= *termios
;
488 newtermios
.c_lflag
&= ~ICANON
;
489 newtermios
.c_lflag
&= ~ECHO
;
490 tcsetattr(0, TCSAFLUSH
, &newtermios
);
491 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) | O_NONBLOCK
);
494 static void term_done(struct termios
*termios
)
496 tcsetattr(0, 0, termios
);
499 int main(int argc
, char *argv
[])
501 struct termios termios
;
503 char *path
= argv
[argc
- 1];
505 printf("usage: %s [-u -s60 ...] file\n", argv
[0]);
508 read_args(argc
, argv
);
510 snprintf(filename
, sizeof(filename
), "%s", path
);
511 if (video
&& !(vffs
= ffs_alloc(path
, FFS_VIDEO
| (video
- 1))))
513 if (audio
&& !(affs
= ffs_alloc(path
, FFS_AUDIO
| (audio
- 1))))
515 if (!video
&& !audio
)
522 fprintf(stderr
, "fbff: /dev/dsp busy?\n");
525 pthread_create(&a_thread
, NULL
, process_audio
, NULL
);
531 ffs_vinfo(vffs
, &w
, &h
);
532 if (magnify
!= 1 && sizeof(fbval_t
) != FBM_BPP(fb_mode()))
533 fprintf(stderr
, "fbff: fbval_t does not match\n");
535 float hz
= (float) fb_rows() / h
/ magnify
;
536 float wz
= (float) fb_cols() / w
/ magnify
;
537 zoom
= hz
< wz
? hz
: wz
;
539 ffs_vconf(vffs
, zoom
, fb_mode());
551 pthread_join(a_thread
, NULL
);