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.
19 #include <tinyalsa/asoundlib.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 struct pcm
*card
; /* alsa card */
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)
104 struct pcm_config cfg
;
107 ffs_ainfo(affs
, &rate
, &bps
, &ch
);
109 cfg
.channels
= (unsigned int) ch
;
110 cfg
.rate
= (unsigned int)rate
;
111 cfg
.period_size
= 1024;
112 cfg
.period_count
= 4;
115 cfg
.format
= PCM_FORMAT_S8
;
118 cfg
.format
= PCM_FORMAT_S16_LE
;
121 cfg
.format
= PCM_FORMAT_S24_LE
;
124 cfg
.format
= PCM_FORMAT_S32_LE
;
129 cfg
.start_threshold
= 0;
130 cfg
.stop_threshold
= 0;
131 cfg
.silence_threshold
= 0;
132 card
= pcm_open(0, 0, PCM_OUT
, &cfg
);
139 static void oss_close(void)
148 #define ABUFCNT (1 << 3) /* number of audio buffers */
149 #define ABUFLEN (1 << 18) /* audio buffer length */
153 static char a_buf
[ABUFCNT
][ABUFLEN
];
154 static int a_len
[ABUFCNT
];
157 static int a_conswait(void)
159 return a_cons
== a_prod
;
162 static int a_prodwait(void)
164 return ((a_prod
+ 1) & (ABUFCNT
- 1)) == a_cons
;
167 static void a_doreset(int pause
)
170 while (audio
&& a_reset
)
174 /* subtitle handling */
176 #define SUBSCNT 2048 /* number of subtitles */
177 #define SUBSLEN 80 /* maximum subtitle length */
179 static char *sub_path
; /* subtitles file */
180 static char sub_text
[SUBSCNT
][SUBSLEN
]; /* subtitle text */
181 static long sub_beg
[SUBSCNT
]; /* printing position */
182 static long sub_end
[SUBSCNT
]; /* hiding position */
183 static int sub_n
; /* subtitle count */
184 static int sub_last
; /* last printed subtitle */
186 static void sub_read(void)
188 struct ffs
*sffs
= ffs_alloc(sub_path
, FFS_SUBTS
);
191 while (sub_n
< SUBSCNT
&& !ffs_sdec(sffs
, &sub_text
[sub_n
][0], SUBSLEN
,
192 &sub_beg
[sub_n
], &sub_end
[sub_n
])) {
198 static void sub_print(void)
200 struct ffs
*ffs
= video
? vffs
: affs
;
203 long pos
= ffs_pos(ffs
);
205 int m
= (l
+ h
) >> 1;
206 if (pos
>= sub_beg
[m
] && pos
<= sub_end
[m
]) {
208 printf("\r\33[K%s", sub_text
[m
]);
213 if (pos
< sub_beg
[m
])
227 static int cmdread(void)
230 if (read(0, &b
, 1) <= 0)
235 static void cmdwait(void)
237 struct pollfd ufds
[1];
239 ufds
[0].events
= POLLIN
;
243 static void cmdjmp(int n
, int rel
)
245 struct ffs
*ffs
= video
? vffs
: affs
;
246 long pos
= (rel
? ffs_pos(ffs
) : 0) + n
* 1000;
252 mark
['\''] = ffs_pos(ffs
);
254 ffs_seek(affs
, ffs
, pos
);
256 ffs_seek(vffs
, ffs
, pos
);
259 static void cmdinfo(void)
261 struct ffs
*ffs
= video
? vffs
: affs
;
262 long pos
= ffs_pos(ffs
);
263 long percent
= ffs_duration(ffs
) ? pos
* 10 / (ffs_duration(ffs
) / 100) : 0;
264 printf("\r\33[K%c %3ld.%01ld%% %3ld:%02ld.%01ld (AV:%4d) [%s] \r",
265 paused
? (card
? '*' : ' ') : '>',
266 percent
/ 10, percent
% 10,
267 pos
/ 60000, (pos
% 60000) / 1000, (pos
% 1000) / 100,
268 video
&& audio
? ffs_avdiff(vffs
, affs
) : 0,
273 static int cmdarg(int def
)
280 static void cmdexec(void)
283 while ((c
= cmdread()) >= 0) {
286 mark
[c
] = ffs_pos(video
? vffs
: affs
);
292 cmdjmp(mark
[c
] / 1000, 0);
300 cmdjmp(cmdarg(1) * 10, 1);
303 cmdjmp(-cmdarg(1) * 10, 1);
306 cmdjmp(cmdarg(1) * 60, 1);
309 cmdjmp(-cmdarg(1) * 60, 1);
312 cmdjmp(cmdarg(1) * 600, 1);
315 cmdjmp(-cmdarg(1) * 600, 1);
318 cmdjmp(cmdarg(0) * 60, 0);
321 cmdjmp(cmdarg(0) * ffs_duration(vffs
? vffs
: affs
) / 100000, 0);
337 if (audio
&& !paused
)
343 sync_diff
= -cmdarg(0);
346 sync_diff
= cmdarg(0);
349 sync_diff
= ffs_avdiff(vffs
, affs
);
352 sync_cnt
= cmdarg(0);
355 sync_cur
= cmdarg(sync_cnt
);
362 arg
= arg
* 10 + c
- '0';
367 /* return nonzero if one more video frame can be decoded */
368 static int vsync(void)
370 if (sync_period
&& sync_since
++ >= sync_period
) {
376 if (sync_first
< vnum
) {
378 sync_diff
= ffs_avdiff(vffs
, affs
);
383 return ffs_avdiff(vffs
, affs
) >= sync_diff
;
389 static void mainloop(void)
392 while (eof
< audio
+ video
) {
401 while (audio
&& !eof
&& !a_prodwait()) {
402 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFLEN
);
407 a_prod
= (a_prod
+ 1) & (ABUFCNT
- 1);
410 if (video
&& (!audio
|| eof
|| vsync())) {
411 int ignore
= jump
&& (vnum
% (jump
+ 1));
413 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
418 draw_frame((void *) buf
, ret
);
427 static void *process_audio(void *dat
)
430 while (!a_reset
&& (a_conswait() || paused
) && !exited
)
441 pcm_write(card
, a_buf
[a_cons
], (unsigned int) a_len
[a_cons
]);
442 a_cons
= (a_cons
+ 1) & (ABUFCNT
- 1);
448 static char *usage
= "usage: fbff [options] file\n"
450 " -z n zoom the video\n"
451 " -m n magnify the video by duplicating pixels\n"
452 " -j n jump every n video frames; for slow machines\n"
453 " -f start full screen\n"
454 " -v n select video stream; '-' disables video\n"
455 " -a n select audio stream; '-' disables audio\n"
456 " -s always synchronize (-sx for every x frames)\n"
457 " -u record A/V delay after the first few frames\n"
458 " -t path subtitles file\n"
459 " -x n horizontal video position\n"
460 " -y n vertical video position\n"
461 " -r adjust the video to the right of the screen\n"
462 " -b adjust the video to the bottom of the screen\n\n";
464 static void read_args(int argc
, char *argv
[])
472 magnify
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
474 zoom
= c
[2] ? atof(c
+ 2) : atof(argv
[++i
]);
476 jump
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
480 sync_period
= c
[2] ? atoi(c
+ 2) : 1;
482 sub_path
= c
[2] ? c
+ 2 : argv
[++i
];
486 posx
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
488 posy
= c
[2] ? atoi(c
+ 2) : atoi(argv
[++i
]);
496 char *arg
= c
[2] ? c
+ 2 : argv
[++i
];
497 video
= arg
[0] == '-' ? 0 : atoi(arg
) + 2;
500 char *arg
= c
[2] ? c
+ 2 : argv
[++i
];
501 audio
= arg
[0] == '-' ? 0 : atoi(arg
) + 2;
507 static void term_init(struct termios
*termios
)
509 struct termios newtermios
;
510 tcgetattr(0, termios
);
511 newtermios
= *termios
;
512 newtermios
.c_lflag
&= ~ICANON
;
513 newtermios
.c_lflag
&= ~ECHO
;
514 tcsetattr(0, TCSAFLUSH
, &newtermios
);
515 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) | O_NONBLOCK
);
518 static void term_done(struct termios
*termios
)
520 tcsetattr(0, 0, termios
);
523 int main(int argc
, char *argv
[])
525 struct termios termios
;
527 char *path
= argv
[argc
- 1];
529 printf("usage: %s [-u -s60 ...] file\n", argv
[0]);
532 read_args(argc
, argv
);
534 snprintf(filename
, sizeof(filename
), "%s", path
);
535 if (video
&& !(vffs
= ffs_alloc(path
, FFS_VIDEO
| (video
- 1))))
537 if (audio
&& !(affs
= ffs_alloc(path
, FFS_AUDIO
| (audio
- 1))))
539 if (!video
&& !audio
)
546 fprintf(stderr
, "fbff: /dev/dsp busy?\n");
549 pthread_create(&a_thread
, NULL
, process_audio
, NULL
);
555 ffs_vinfo(vffs
, &w
, &h
);
556 if (magnify
!= 1 && sizeof(fbval_t
) != FBM_BPP(fb_mode()))
557 fprintf(stderr
, "fbff: fbval_t does not match\n");
559 float hz
= (float) fb_rows() / h
/ magnify
;
560 float wz
= (float) fb_cols() / w
/ magnify
;
561 zoom
= hz
< wz
? hz
: wz
;
563 ffs_vconf(vffs
, zoom
, fb_mode());
575 pthread_join(a_thread
, NULL
);