fbff: clear only the current line for printing subtitles
[fbff.git] / fbff.c
blob5c6af248e057fb5daff3f5e19884bdd01fb68878
1 /*
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.
7 */
8 #include <ctype.h>
9 #include <fcntl.h>
10 #include <pty.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <termios.h>
16 #include <unistd.h>
17 #include <sys/poll.h>
18 #include <sys/soundcard.h>
19 #include <pthread.h>
20 #include "ffs.h"
21 #include "draw.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 */
28 static int paused;
29 static int exited;
30 static int domark;
31 static int dojump;
32 static int arg;
33 static char filename[32];
35 static float zoom = 1;
36 static int magnify = 1;
37 static int jump = 0;
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)
59 usleep(10000);
62 static void draw_row(int rb, int cb, void *img, int cn)
64 if (rb < 0 || rb >= fb_rows())
65 return;
66 if (cb < 0) {
67 cn = -cb < cn ? cn + cb : 0;
68 img += -cb;
69 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;
79 int i, r, c;
80 ffs_vinfo(vffs, &w, &h);
81 rn = h * zoom;
82 cn = w * zoom;
83 cb = rjust ? fb_cols() - cn * magnify + posx : posx;
84 rb = bjust ? fb_rows() - rn * magnify + posy : posy;
85 if (magnify == 1) {
86 for (r = 0; r < rn; r++)
87 draw_row(rb + r, cb, img + r * linelen, cn);
88 } else {
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);
98 free(brow);
102 static int oss_open(void)
104 int rate, ch, bps;
105 afd = open("/dev/dsp", O_WRONLY);
106 if (afd < 0)
107 return 1;
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);
112 return 0;
115 static void oss_close(void)
117 if (afd > 0)
118 close(afd);
119 afd = 0;
122 /* audio buffers */
124 #define ABUFCNT (1 << 3) /* number of audio buffers */
125 #define ABUFLEN (1 << 18) /* audio buffer length */
127 static int a_cons;
128 static int a_prod;
129 static char a_buf[ABUFCNT][ABUFLEN];
130 static int a_len[ABUFCNT];
131 static int a_reset;
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)
145 a_reset = 1 + pause;
146 while (audio && a_reset)
147 stroll();
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);
165 if (!sffs)
166 return;
167 while (sub_n < SUBSCNT && !ffs_sdec(sffs, &sub_text[sub_n][0], SUBSLEN,
168 &sub_beg[sub_n], &sub_end[sub_n])) {
169 sub_n++;
171 ffs_free(sffs);
174 static void sub_print(void)
176 struct ffs *ffs = video ? vffs : affs;
177 int l = 0;
178 int h = sub_n;
179 long pos = ffs_pos(ffs);
180 while (l < h) {
181 int m = (l + h) >> 1;
182 if (pos >= sub_beg[m] && pos <= sub_end[m]) {
183 if (sub_last != m)
184 printf("\r\33[K%s", sub_text[m]);
185 sub_last = m;
186 fflush(stdout);
187 return;
189 if (pos < sub_beg[m])
190 h = m;
191 else
192 l = m + 1;
194 if (sub_last >= 0) {
195 printf("\r\33[K");
196 fflush(stdout);
197 sub_last = -1;
201 /* fbff commands */
203 static int cmdread(void)
205 char b;
206 if (read(0, &b, 1) <= 0)
207 return -1;
208 return b;
211 static void cmdwait(void)
213 struct pollfd ufds[1];
214 ufds[0].fd = 0;
215 ufds[0].events = POLLIN;
216 poll(ufds, 1, -1);
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;
223 a_doreset(0);
224 sync_cur = sync_cnt;
225 if (pos < 0)
226 pos = 0;
227 if (!rel)
228 mark['\''] = ffs_pos(ffs);
229 if (audio)
230 ffs_seek(affs, ffs, pos);
231 if (video)
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,
245 filename);
246 fflush(stdout);
249 static int cmdarg(int def)
251 int n = arg;
252 arg = 0;
253 return n ? n : def;
256 static void cmdexec(void)
258 int c;
259 while ((c = cmdread()) >= 0) {
260 if (domark) {
261 domark = 0;
262 mark[c] = ffs_pos(video ? vffs : affs);
263 continue;
265 if (dojump) {
266 dojump = 0;
267 if (mark[c] > 0)
268 cmdjmp(mark[c] / 1000, 0);
269 continue;
271 switch (c) {
272 case 'q':
273 exited = 1;
274 break;
275 case 'l':
276 cmdjmp(cmdarg(1) * 10, 1);
277 break;
278 case 'h':
279 cmdjmp(-cmdarg(1) * 10, 1);
280 break;
281 case 'j':
282 cmdjmp(cmdarg(1) * 60, 1);
283 break;
284 case 'k':
285 cmdjmp(-cmdarg(1) * 60, 1);
286 break;
287 case 'J':
288 cmdjmp(cmdarg(1) * 600, 1);
289 break;
290 case 'K':
291 cmdjmp(-cmdarg(1) * 600, 1);
292 break;
293 case 'G':
294 cmdjmp(cmdarg(0) * 60, 0);
295 break;
296 case '%':
297 cmdjmp(cmdarg(0) * ffs_duration(vffs ? vffs : affs) / 100000, 0);
298 break;
299 case 'm':
300 domark = 1;
301 break;
302 case '\'':
303 dojump = 1;
304 break;
305 case 'i':
306 cmdinfo();
307 break;
308 case ' ':
309 case 'p':
310 if (audio && paused)
311 if (oss_open())
312 break;
313 if (audio && !paused)
314 oss_close();
315 paused = !paused;
316 sync_cur = sync_cnt;
317 break;
318 case '-':
319 sync_diff = -cmdarg(0);
320 break;
321 case '+':
322 sync_diff = cmdarg(0);
323 break;
324 case 'a':
325 sync_diff = ffs_avdiff(vffs, affs);
326 break;
327 case 'c':
328 sync_cnt = cmdarg(0);
329 break;
330 case 's':
331 sync_cur = cmdarg(sync_cnt);
332 break;
333 case 27:
334 arg = 0;
335 break;
336 default:
337 if (isdigit(c))
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) {
347 sync_cur = sync_cnt;
348 sync_since = 0;
350 if (sync_first) {
351 sync_cur = 0;
352 if (sync_first < vnum) {
353 sync_first = 0;
354 sync_diff = ffs_avdiff(vffs, affs);
357 if (sync_cur > 0) {
358 sync_cur--;
359 return ffs_avdiff(vffs, affs) >= sync_diff;
361 ffs_wait(vffs);
362 return 1;
365 static void mainloop(void)
367 int eof = 0;
368 while (eof < audio + video) {
369 cmdexec();
370 if (exited)
371 break;
372 if (paused) {
373 a_doreset(1);
374 cmdwait();
375 continue;
377 while (audio && !eof && !a_prodwait()) {
378 int ret = ffs_adec(affs, a_buf[a_prod], ABUFLEN);
379 if (ret < 0)
380 eof++;
381 if (ret > 0) {
382 a_len[a_prod] = ret;
383 a_prod = (a_prod + 1) & (ABUFCNT - 1);
386 if (video && (!audio || eof || vsync())) {
387 int ignore = jump && (vnum % (jump + 1));
388 void *buf;
389 int ret = ffs_vdec(vffs, ignore ? NULL : &buf);
390 vnum++;
391 if (ret < 0)
392 eof++;
393 if (ret > 0)
394 draw_frame((void *) buf, ret);
395 sub_print();
396 } else {
397 stroll();
400 exited = 1;
403 static void *process_audio(void *dat)
405 while (1) {
406 while (!a_reset && (a_conswait() || paused) && !exited)
407 stroll();
408 if (exited)
409 return NULL;
410 if (a_reset) {
411 if (a_reset == 1)
412 a_cons = a_prod;
413 a_reset = 0;
414 continue;
416 if (afd > 0) {
417 write(afd, a_buf[a_cons], a_len[a_cons]);
418 a_cons = (a_cons + 1) & (ABUFCNT - 1);
421 return NULL;
424 static char *usage = "usage: fbff [options] file\n"
425 "\noptions:\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[])
442 int i = 1;
443 while (i < argc) {
444 char *c = argv[i];
445 if (c[0] != '-')
446 break;
447 if (c[1] == 'm')
448 magnify = c[2] ? atoi(c + 2) : atoi(argv[++i]);
449 if (c[1] == 'z')
450 zoom = c[2] ? atof(c + 2) : atof(argv[++i]);
451 if (c[1] == 'j')
452 jump = c[2] ? atoi(c + 2) : atoi(argv[++i]);
453 if (c[1] == 'f')
454 fullscreen = 1;
455 if (c[1] == 's')
456 sync_period = c[2] ? atoi(c + 2) : 1;
457 if (c[1] == 't')
458 sub_path = c[2] ? c + 2 : argv[++i];
459 if (c[1] == 'h')
460 printf(usage);
461 if (c[1] == 'x')
462 posx = c[2] ? atoi(c + 2) : atoi(argv[++i]);
463 if (c[1] == 'y')
464 posy = c[2] ? atoi(c + 2) : atoi(argv[++i]);
465 if (c[1] == 'r')
466 rjust = 1;
467 if (c[1] == 'b')
468 bjust = 1;
469 if (c[1] == 'u')
470 sync_first = 32;
471 if (c[1] == 'v') {
472 char *arg = c[2] ? c + 2 : argv[++i];
473 video = arg[0] == '-' ? 0 : atoi(arg) + 2;
475 if (c[1] == 'a') {
476 char *arg = c[2] ? c + 2 : argv[++i];
477 audio = arg[0] == '-' ? 0 : atoi(arg) + 2;
479 i++;
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;
502 pthread_t a_thread;
503 char *path = argv[argc - 1];
504 if (argc < 2) {
505 printf("usage: %s [-u -s60 ...] file\n", argv[0]);
506 return 1;
508 read_args(argc, argv);
509 ffs_globinit();
510 snprintf(filename, sizeof(filename), "%s", path);
511 if (video && !(vffs = ffs_alloc(path, FFS_VIDEO | (video - 1))))
512 video = 0;
513 if (audio && !(affs = ffs_alloc(path, FFS_AUDIO | (audio - 1))))
514 audio = 0;
515 if (!video && !audio)
516 return 1;
517 if (sub_path)
518 sub_read();
519 if (audio) {
520 ffs_aconf(affs);
521 if (oss_open()) {
522 fprintf(stderr, "fbff: /dev/dsp busy?\n");
523 return 1;
525 pthread_create(&a_thread, NULL, process_audio, NULL);
527 if (video) {
528 int w, h;
529 if (fb_init())
530 return 1;
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");
534 if (fullscreen) {
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());
541 term_init(&termios);
542 mainloop();
543 term_done(&termios);
544 printf("\n");
546 if (video) {
547 fb_free();
548 ffs_free(vffs);
550 if (audio) {
551 pthread_join(a_thread, NULL);
552 oss_close();
553 ffs_free(affs);
555 return 0;