52e6252f338a46679f7abcffaf25c0560e71f304
2 * fbff - a small ffmpeg-based framebuffer/oss media player
4 * Copyright (C) 2009-2012 Ali Gholami Rudi
6 * This program is released under GNU GPL version 2.
19 #include <sys/soundcard.h>
25 #define MIN(a, b) ((a) < (b) ? (a) : (b))
26 #define MAX(a, b) ((a) > (b) ? (a) : (b))
29 static struct termios termios
;
33 static float zoom
= 1;
34 static int magnify
= 1;
36 static int fullscreen
= 0;
37 static int video
= 1; /* video stream; 0=none, 1=auto, >2=idx */
38 static int audio
= 1; /* audio stream; 0=none, 1=auto, >2=idx */
39 static int rjust
= 0; /* justify video to the right */
40 static int bjust
= 0; /* justify video to the bottom */
41 static int frame_jmp
= 1; /* the changes to pos_cur for each frame */
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 */
48 static int sync_diff
; /* audio/video frame position diff */
49 static int sync_cnt
= 32; /* synchronization steps */
50 static int sync_cur
; /* synchronization steps left */
52 static void stroll(void)
57 static void draw_frame(void *img
, int linelen
)
63 ffs_vinfo(vffs
, &w
, &h
);
64 nr
= MIN(h
* zoom
, fb_rows() / magnify
);
65 nc
= MIN(w
* zoom
, fb_cols() / magnify
);
66 cb
= rjust
? fb_cols() - nc
* magnify
: 0;
67 rb
= bjust
? fb_rows() - nr
* magnify
: 0;
68 for (r
= 0; r
< nr
; r
++) {
69 fbval_t
*row
= img
+ r
* linelen
;
71 fb_set(rb
+ r
, cb
, row
, nc
);
74 for (c
= 0; c
< nc
; c
++)
75 for (i
= 0; i
< magnify
; i
++)
76 buf
[c
* magnify
+ i
] = row
[c
];
77 for (i
= 0; i
< magnify
; i
++)
78 fb_set((rb
+ r
) * magnify
+ i
, cb
, buf
, nc
* magnify
);
82 #define ABUFSZ (1 << 18)
86 static char a_buf
[AUDIOBUFS
][ABUFSZ
];
87 static int a_len
[AUDIOBUFS
];
90 static int a_conswait(void)
92 return a_cons
== a_prod
;
95 static int a_prodwait(void)
97 return ((a_prod
+ 1) & (AUDIOBUFS
- 1)) == a_cons
;
100 static void a_doreset(int pause
)
103 while (audio
&& a_reset
)
107 static int readkey(void)
110 if (read(STDIN_FILENO
, &b
, 1) <= 0)
115 static void waitkey(void)
117 struct pollfd ufds
[1];
118 ufds
[0].fd
= STDIN_FILENO
;
119 ufds
[0].events
= POLLIN
;
123 static int ffarg(int def
)
130 static void ffjmp(int n
, int rel
)
132 struct ffs
*ffs
= video
? vffs
: affs
;
133 long pos
= ffs_pos(ffs
, n
);
137 ffs_seek(affs
, pos
, frame_jmp
);
139 ffs_seek(vffs
, pos
, frame_jmp
);
142 static void printinfo(void)
144 struct ffs
*ffs
= video
? vffs
: affs
;
145 printf("fbff: %8lx \t (AV: %d)\r",
146 ffs_pos(ffs
, 0), video
&& audio
? ffs_avdiff(vffs
, affs
) : 0);
150 #define JMP1 (1 << 5)
151 #define JMP2 (JMP1 << 3)
152 #define JMP3 (JMP2 << 5)
154 static void execkey(void)
157 while ((c
= readkey()) != -1) {
163 ffjmp(ffarg(1) * JMP1
, 1);
166 ffjmp(-ffarg(1) * JMP1
, 1);
169 ffjmp(ffarg(1) * JMP2
, 1);
172 ffjmp(-ffarg(1) * JMP2
, 1);
175 ffjmp(ffarg(1) * JMP3
, 1);
178 ffjmp(-ffarg(1) * JMP3
, 1);
193 sync_diff
= -ffarg(0);
196 sync_diff
= ffarg(0);
199 sync_diff
= ffs_avdiff(vffs
, affs
);
205 sync_cur
= ffarg(sync_cnt
);
212 arg
= arg
* 10 + c
- '0';
217 /* return nonzero if one more video frame can be decoded */
218 static int vsync(void)
222 return ffs_avdiff(vffs
, affs
) >= sync_diff
;
228 static void mainloop(void)
231 while (eof
< audio
+ video
) {
240 while (audio
&& !eof
&& !a_prodwait()) {
241 int ret
= ffs_adec(affs
, a_buf
[a_prod
], ABUFSZ
);
246 a_prod
= (a_prod
+ 1) & (AUDIOBUFS
- 1);
249 if (video
&& (!audio
|| eof
|| vsync())) {
250 int ignore
= jump
&& (vnum
++ % (jump
+ 1));
252 int ret
= ffs_vdec(vffs
, ignore
? NULL
: &buf
);
256 draw_frame((void *) buf
, ret
);
264 static void oss_init(void)
267 afd
= open("/dev/dsp", O_RDWR
);
269 fprintf(stderr
, "cannot open /dev/dsp\n");
272 ffs_ainfo(affs
, &rate
, &bps
, &ch
);
273 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
274 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
275 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bps
);
278 static void oss_close(void)
283 static void *process_audio(void *dat
)
287 while (!a_reset
&& (a_conswait() || paused
) && !exited
)
297 write(afd
, a_buf
[a_cons
], a_len
[a_cons
]);
298 a_cons
= (a_cons
+ 1) & (AUDIOBUFS
- 1);
305 static void term_setup(void)
307 struct termios newtermios
;
308 tcgetattr(STDIN_FILENO
, &termios
);
309 newtermios
= termios
;
310 newtermios
.c_lflag
&= ~ICANON
;
311 newtermios
.c_lflag
&= ~ECHO
;
312 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &newtermios
);
313 fcntl(STDIN_FILENO
, F_SETFL
, fcntl(STDIN_FILENO
, F_GETFL
) | O_NONBLOCK
);
316 static void term_cleanup(void)
318 tcsetattr(STDIN_FILENO
, 0, &termios
);
321 static void sigcont(int sig
)
326 static char *usage
= "usage: fbff [options] file\n"
328 " -z x zoom the screen using ffmpeg\n"
329 " -m x magnify the screen by repeating pixels\n"
330 " -j x jump every x video frames; for slow machines\n"
331 " -f start full screen\n"
332 " -v x select video stream; '-' disables video\n"
333 " -a x select audio stream; '-' disables audio\n"
334 " -s always synchronize; useful for files with bad video framerate\n"
335 " -t use time based seeking; only if the default does't work\n"
336 " -r adjust the video to the right of the screen\n"
337 " -b adjust the video to the bottom of the screen\n\n";
339 static void read_args(int argc
, char *argv
[])
343 if (!strcmp(argv
[i
], "-m"))
344 magnify
= atoi(argv
[++i
]);
345 if (!strcmp(argv
[i
], "-z"))
346 zoom
= atof(argv
[++i
]);
347 if (!strcmp(argv
[i
], "-j"))
348 jump
= atoi(argv
[++i
]);
349 if (!strcmp(argv
[i
], "-f"))
351 if (!strcmp(argv
[i
], "-s"))
352 sync_cnt
= sync_cur
= (1 << 30);
353 if (!strcmp(argv
[i
], "-v"))
354 video
= argv
[++i
][0] == '-' ? 0 : atoi(argv
[i
]) + 2;
355 if (!strcmp(argv
[i
], "-a"))
356 audio
= argv
[++i
][0] == '-' ? 0 : atoi(argv
[i
]) + 2;
357 if (!strcmp(argv
[i
], "-t"))
359 if (!strcmp(argv
[i
], "-h"))
361 if (!strcmp(argv
[i
], "-r"))
363 if (!strcmp(argv
[i
], "-b"))
369 int main(int argc
, char *argv
[])
372 char *path
= argv
[argc
- 1];
374 printf("usage: %s [options] filename\n", argv
[0]);
377 read_args(argc
, argv
);
379 if (video
&& !(vffs
= ffs_alloc(path
, FFS_VIDEO
| (video
- 1))))
381 if (audio
&& !(affs
= ffs_alloc(path
, FFS_AUDIO
| (audio
- 1))))
383 if (!video
&& !audio
)
386 pthread_create(&a_thread
, NULL
, process_audio
, NULL
);
391 ffs_vinfo(vffs
, &w
, &h
);
392 if (magnify
!= 1 && sizeof(fbval_t
) != FBM_BPP(fb_mode()))
393 fprintf(stderr
, "fbff: magnify != 1 and fbval_t doesn't match\n");
395 float hz
= (float) fb_rows() / h
/ magnify
;
396 float wz
= (float) fb_cols() / w
/ magnify
;
397 zoom
= hz
< wz
? hz
: wz
;
399 ffs_vsetup(vffs
, zoom
, fb_mode());
402 signal(SIGCONT
, sigcont
);
411 pthread_join(a_thread
, NULL
);