typo fixes
[mplayer/greg.git] / libvo / vo_fbdev.c
blobd4ed626d8db8ce151df6a9c6b68f8a62c8b0b1b9
1 /*
2 * Video driver for Framebuffer device
3 * by Szabolcs Berecz <szabi@inf.elte.hu>
4 * (C) 2001
5 *
6 * Some idea and code borrowed from Chris Lawrence's ppmtofb-0.27
7 * Some fixes and small improvements by Joey Parrish <joey@nicewarrior.org>
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <ctype.h>
18 #include <sys/mman.h>
19 #include <sys/ioctl.h>
20 #include <sys/kd.h>
21 #include <linux/fb.h>
23 #include "config.h"
24 #include "video_out.h"
25 #include "video_out_internal.h"
26 #include "fastmemcpy.h"
27 #include "sub.h"
28 #ifdef CONFIG_VIDIX
29 #include "vosub_vidix.h"
30 #endif
31 #include "aspect.h"
32 #include "mp_msg.h"
34 static vo_info_t info = {
35 "Framebuffer Device",
36 "fbdev",
37 "Szabolcs Berecz <szabi@inf.elte.hu>",
41 LIBVO_EXTERN(fbdev)
43 #ifdef CONFIG_VIDIX
44 /* Name of VIDIX driver */
45 static const char *vidix_name = NULL;
46 static vidix_grkey_t gr_key;
47 #endif
48 static signed int pre_init_err = -2;
49 /******************************
50 * fb.modes support *
51 ******************************/
53 extern char *monitor_hfreq_str;
54 extern char *monitor_vfreq_str;
55 extern char *monitor_dotclock_str;
57 static range_t *monitor_hfreq = NULL;
58 static range_t *monitor_vfreq = NULL;
59 static range_t *monitor_dotclock = NULL;
61 typedef struct {
62 char *name;
63 uint32_t xres, yres, vxres, vyres, depth;
64 uint32_t pixclock, left, right, upper, lower, hslen, vslen;
65 uint32_t sync;
66 uint32_t vmode;
67 } fb_mode_t;
69 #define MAX_NR_TOKEN 16
71 #define MAX_LINE_LEN 1000
73 #define RET_EOF -1
74 #define RET_EOL -2
76 static int validate_mode(fb_mode_t *m)
78 if (!m->xres) {
79 mp_msg(MSGT_VO, MSGL_V, "needs geometry ");
80 return 0;
82 if (!m->pixclock) {
83 mp_msg(MSGT_VO, MSGL_V, "needs timings ");
84 return 0;
86 return 1;
89 static FILE *fp;
90 static int line_num = 0;
91 static char *line;
92 static char *token[MAX_NR_TOKEN];
94 static int get_token(int num)
96 static int read_nextline = 1;
97 static int line_pos;
98 int i;
99 char c;
101 if (num >= MAX_NR_TOKEN) {
102 mp_msg(MSGT_VO, MSGL_V, "get_token(): max >= MAX_NR_TOKEN!\n");
103 goto out_eof;
106 if (read_nextline) {
107 if (!fgets(line, MAX_LINE_LEN, fp))
108 goto out_eof;
109 line_pos = 0;
110 ++line_num;
111 read_nextline = 0;
113 for (i = 0; i < num; i++) {
114 while (isspace(line[line_pos]))
115 ++line_pos;
116 if (line[line_pos] == '\0' || line[line_pos] == '#') {
117 read_nextline = 1;
118 goto out_eol;
120 token[i] = line + line_pos;
121 c = line[line_pos];
122 if (c == '"' || c == '\'') {
123 token[i]++;
124 while (line[++line_pos] != c && line[line_pos])
125 /* NOTHING */;
126 if (!line[line_pos])
127 goto out_eol;
128 line[line_pos] = ' ';
129 } else {
130 for (/* NOTHING */; !isspace(line[line_pos]) &&
131 line[line_pos]; line_pos++)
132 /* NOTHING */;
134 if (!line[line_pos]) {
135 read_nextline = 1;
136 if (i == num - 1)
137 goto out_ok;
138 goto out_eol;
140 line[line_pos++] = '\0';
142 out_ok:
143 return i;
144 out_eof:
145 return RET_EOF;
146 out_eol:
147 return RET_EOL;
150 static fb_mode_t *fb_modes = NULL;
151 static int nr_modes = 0;
153 static int parse_fbmode_cfg(char *cfgfile)
155 #define CHECK_IN_MODE_DEF\
156 if (!in_mode_def) {\
157 mp_msg(MSGT_VO, MSGL_V, "'needs 'mode' first");\
158 goto err_out_print_linenum;\
160 fb_mode_t *mode = NULL;
161 char *endptr; // strtoul()...
162 int in_mode_def = 0;
163 int tmp, i;
165 /* If called more than once, reuse parsed data */
166 if (nr_modes)
167 return nr_modes;
169 mp_msg(MSGT_VO, MSGL_V, "Reading %s: ", cfgfile);
171 if ((fp = fopen(cfgfile, "r")) == NULL) {
172 mp_msg(MSGT_VO, MSGL_V, "can't open '%s': %s\n", cfgfile, strerror(errno));
173 return -1;
176 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) {
177 mp_msg(MSGT_VO, MSGL_V, "can't get memory for 'line': %s\n", strerror(errno));
178 return -2;
182 * check if the cfgfile starts with 'mode'
184 while ((tmp = get_token(1)) == RET_EOL)
185 /* NOTHING */;
186 if (tmp == RET_EOF)
187 goto out;
188 if (!strcmp(token[0], "mode"))
189 goto loop_enter;
190 goto err_out_parse_error;
192 while ((tmp = get_token(1)) != RET_EOF) {
193 if (tmp == RET_EOL)
194 continue;
195 if (!strcmp(token[0], "mode")) {
196 if (in_mode_def) {
197 mp_msg(MSGT_VO, MSGL_V, "'endmode' required");
198 goto err_out_print_linenum;
200 if (!validate_mode(mode))
201 goto err_out_not_valid;
202 loop_enter:
203 if (!(fb_modes = (fb_mode_t *) realloc(fb_modes,
204 sizeof(fb_mode_t) * (nr_modes + 1)))) {
205 mp_msg(MSGT_VO, MSGL_V, "can't realloc 'fb_modes' (nr_modes = %d):"
206 " %s\n", nr_modes, strerror(errno));
207 goto err_out;
209 mode=fb_modes + nr_modes;
210 ++nr_modes;
211 memset(mode,0,sizeof(fb_mode_t));
213 if (get_token(1) < 0)
214 goto err_out_parse_error;
215 for (i = 0; i < nr_modes - 1; i++) {
216 if (!strcmp(token[0], fb_modes[i].name)) {
217 mp_msg(MSGT_VO, MSGL_V, "mode name '%s' isn't unique", token[0]);
218 goto err_out_print_linenum;
221 if (!(mode->name = strdup(token[0]))) {
222 mp_msg(MSGT_VO, MSGL_V, "can't strdup -> 'name': %s\n", strerror(errno));
223 goto err_out;
225 in_mode_def = 1;
226 } else if (!strcmp(token[0], "geometry")) {
227 CHECK_IN_MODE_DEF;
228 if (get_token(5) < 0)
229 goto err_out_parse_error;
230 mode->xres = strtoul(token[0], &endptr, 0);
231 if (*endptr)
232 goto err_out_parse_error;
233 mode->yres = strtoul(token[1], &endptr, 0);
234 if (*endptr)
235 goto err_out_parse_error;
236 mode->vxres = strtoul(token[2], &endptr, 0);
237 if (*endptr)
238 goto err_out_parse_error;
239 mode->vyres = strtoul(token[3], &endptr, 0);
240 if (*endptr)
241 goto err_out_parse_error;
242 mode->depth = strtoul(token[4], &endptr, 0);
243 if (*endptr)
244 goto err_out_parse_error;
245 } else if (!strcmp(token[0], "timings")) {
246 CHECK_IN_MODE_DEF;
247 if (get_token(7) < 0)
248 goto err_out_parse_error;
249 mode->pixclock = strtoul(token[0], &endptr, 0);
250 if (*endptr)
251 goto err_out_parse_error;
252 mode->left = strtoul(token[1], &endptr, 0);
253 if (*endptr)
254 goto err_out_parse_error;
255 mode->right = strtoul(token[2], &endptr, 0);
256 if (*endptr)
257 goto err_out_parse_error;
258 mode->upper = strtoul(token[3], &endptr, 0);
259 if (*endptr)
260 goto err_out_parse_error;
261 mode->lower = strtoul(token[4], &endptr, 0);
262 if (*endptr)
263 goto err_out_parse_error;
264 mode->hslen = strtoul(token[5], &endptr, 0);
265 if (*endptr)
266 goto err_out_parse_error;
267 mode->vslen = strtoul(token[6], &endptr, 0);
268 if (*endptr)
269 goto err_out_parse_error;
270 } else if (!strcmp(token[0], "endmode")) {
271 CHECK_IN_MODE_DEF;
272 in_mode_def = 0;
273 } else if (!strcmp(token[0], "accel")) {
274 CHECK_IN_MODE_DEF;
275 if (get_token(1) < 0)
276 goto err_out_parse_error;
278 * it's only used for text acceleration
279 * so we just ignore it.
281 } else if (!strcmp(token[0], "hsync")) {
282 CHECK_IN_MODE_DEF;
283 if (get_token(1) < 0)
284 goto err_out_parse_error;
285 if (!strcmp(token[0], "low"))
286 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
287 else if(!strcmp(token[0], "high"))
288 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
289 else
290 goto err_out_parse_error;
291 } else if (!strcmp(token[0], "vsync")) {
292 CHECK_IN_MODE_DEF;
293 if (get_token(1) < 0)
294 goto err_out_parse_error;
295 if (!strcmp(token[0], "low"))
296 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
297 else if(!strcmp(token[0], "high"))
298 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
299 else
300 goto err_out_parse_error;
301 } else if (!strcmp(token[0], "csync")) {
302 CHECK_IN_MODE_DEF;
303 if (get_token(1) < 0)
304 goto err_out_parse_error;
305 if (!strcmp(token[0], "low"))
306 mode->sync &= ~FB_SYNC_COMP_HIGH_ACT;
307 else if(!strcmp(token[0], "high"))
308 mode->sync |= FB_SYNC_COMP_HIGH_ACT;
309 else
310 goto err_out_parse_error;
311 } else if (!strcmp(token[0], "extsync")) {
312 CHECK_IN_MODE_DEF;
313 if (get_token(1) < 0)
314 goto err_out_parse_error;
315 if (!strcmp(token[0], "false"))
316 mode->sync &= ~FB_SYNC_EXT;
317 else if(!strcmp(token[0], "true"))
318 mode->sync |= FB_SYNC_EXT;
319 else
320 goto err_out_parse_error;
321 } else if (!strcmp(token[0], "laced")) {
322 CHECK_IN_MODE_DEF;
323 if (get_token(1) < 0)
324 goto err_out_parse_error;
325 if (!strcmp(token[0], "false"))
326 mode->vmode = FB_VMODE_NONINTERLACED;
327 else if (!strcmp(token[0], "true"))
328 mode->vmode = FB_VMODE_INTERLACED;
329 else
330 goto err_out_parse_error;
331 } else if (!strcmp(token[0], "double")) {
332 CHECK_IN_MODE_DEF;
333 if (get_token(1) < 0)
334 goto err_out_parse_error;
335 if (!strcmp(token[0], "false"))
337 else if (!strcmp(token[0], "true"))
338 mode->vmode = FB_VMODE_DOUBLE;
339 else
340 goto err_out_parse_error;
341 } else
342 goto err_out_parse_error;
344 if (!validate_mode(mode))
345 goto err_out_not_valid;
346 out:
347 mp_msg(MSGT_VO, MSGL_V, "%d modes\n", nr_modes);
348 free(line);
349 fclose(fp);
350 return nr_modes;
351 err_out_parse_error:
352 mp_msg(MSGT_VO, MSGL_V, "parse error");
353 err_out_print_linenum:
354 mp_msg(MSGT_VO, MSGL_V, " at line %d\n", line_num);
355 err_out:
356 if (fb_modes) {
357 free(fb_modes);
358 fb_modes = NULL;
360 nr_modes = 0;
361 free(line);
362 free(fp);
363 return -2;
364 err_out_not_valid:
365 mp_msg(MSGT_VO, MSGL_V, "previous mode is not correct");
366 goto err_out_print_linenum;
369 static fb_mode_t *find_mode_by_name(char *name)
371 int i;
373 for (i = 0; i < nr_modes; i++)
374 if (!strcmp(name, fb_modes[i].name))
375 return fb_modes + i;
376 return NULL;
379 static float dcf(fb_mode_t *m) //driving clock frequency
381 return 1e12f / m->pixclock;
384 static float hsf(fb_mode_t *m) //horizontal scan frequency
386 int htotal = m->left + m->xres + m->right + m->hslen;
387 return dcf(m) / htotal;
390 static float vsf(fb_mode_t *m) //vertical scan frequency
392 int vtotal = m->upper + m->yres + m->lower + m->vslen;
393 return hsf(m) / vtotal;
397 static int mode_works(fb_mode_t *m, range_t *hfreq, range_t *vfreq,
398 range_t *dotclock)
400 float h = hsf(m);
401 float v = vsf(m);
402 float d = dcf(m);
403 int ret = 1;
405 mp_msg(MSGT_VO, MSGL_DBG2, "mode %dx%d:", m->xres, m->yres);
406 if (!in_range(hfreq, h)) {
407 ret = 0;
408 mp_msg(MSGT_VO, MSGL_DBG2, " hsync out of range.");
410 if (!in_range(vfreq, v)) {
411 ret = 0;
412 mp_msg(MSGT_VO, MSGL_DBG2, " vsync out of range.");
414 if (!in_range(dotclock, d)) {
415 ret = 0;
416 mp_msg(MSGT_VO, MSGL_DBG2, " dotclock out of range.");
418 if (ret)
419 mp_msg(MSGT_VO, MSGL_DBG2, " hsync, vsync, dotclock ok.\n");
420 else
421 mp_msg(MSGT_VO, MSGL_DBG2, "\n");
423 return ret;
426 static fb_mode_t *find_best_mode(int xres, int yres, range_t *hfreq,
427 range_t *vfreq, range_t *dotclock)
429 int i;
430 fb_mode_t *best = fb_modes;
431 fb_mode_t *curr;
433 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for first working mode\n");
435 for (i = 0; i < nr_modes; i++, best++)
436 if (mode_works(best, hfreq, vfreq, dotclock))
437 break;
439 if (i == nr_modes)
440 return NULL;
441 if (i == nr_modes - 1)
442 return best;
444 mp_msg(MSGT_VO, MSGL_DBG2, "First working mode: %dx%d\n", best->xres, best->yres);
445 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for better modes\n");
447 for (curr = best + 1; i < nr_modes - 1; i++, curr++) {
448 if (!mode_works(curr, hfreq, vfreq, dotclock))
449 continue;
451 if (best->xres < xres || best->yres < yres) {
452 if (curr->xres > best->xres || curr->yres > best->yres) {
453 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too small.\n",
454 best->xres, best->yres);
455 best = curr;
456 } else
457 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
458 } else if (curr->xres == best->xres && curr->yres == best->yres &&
459 vsf(curr) > vsf(best)) {
460 mp_msg(MSGT_VO, MSGL_DBG2, "faster screen refresh.\n");
461 best = curr;
462 } else if ((curr->xres <= best->xres && curr->yres <= best->yres) &&
463 (curr->xres >= xres && curr->yres >= yres)) {
464 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too large.\n",
465 best->xres, best->yres);
466 best = curr;
467 } else {
468 if (curr->xres < xres || curr->yres < yres)
469 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
470 else if (curr->xres > best->xres || curr->yres > best->yres)
471 mp_msg(MSGT_VO, MSGL_DBG2, "too large.\n");
472 else mp_msg(MSGT_VO, MSGL_DBG2, "it's worse, don't know why.\n");
476 return best;
479 static void set_bpp(struct fb_var_screeninfo *p, int bpp)
481 p->bits_per_pixel = (bpp + 1) & ~1;
482 p->red.msb_right = p->green.msb_right = p->blue.msb_right = p->transp.msb_right = 0;
483 p->transp.offset = p->transp.length = 0;
484 p->blue.offset = 0;
485 switch (bpp) {
486 case 32:
487 p->transp.offset = 24;
488 p->transp.length = 8;
489 case 24:
490 p->red.offset = 16;
491 p->red.length = 8;
492 p->green.offset = 8;
493 p->green.length = 8;
494 p->blue.length = 8;
495 break;
496 case 16:
497 p->red.offset = 11;
498 p->green.length = 6;
499 p->red.length = 5;
500 p->green.offset = 5;
501 p->blue.length = 5;
502 break;
503 case 15:
504 p->red.offset = 10;
505 p->green.length = 5;
506 p->red.length = 5;
507 p->green.offset = 5;
508 p->blue.length = 5;
509 break;
513 static void fb_mode2fb_vinfo(fb_mode_t *m, struct fb_var_screeninfo *v)
515 v->xres = m->xres;
516 v->yres = m->yres;
517 v->xres_virtual = m->vxres;
518 v->yres_virtual = m->vyres;
519 set_bpp(v, m->depth);
520 v->pixclock = m->pixclock;
521 v->left_margin = m->left;
522 v->right_margin = m->right;
523 v->upper_margin = m->upper;
524 v->lower_margin = m->lower;
525 v->hsync_len = m->hslen;
526 v->vsync_len = m->vslen;
527 v->sync = m->sync;
528 v->vmode = m->vmode;
532 /******************************
533 * vo_fbdev *
534 ******************************/
536 /* command line/config file options */
537 char *fb_dev_name = NULL;
538 char *fb_mode_cfgfile = NULL;
539 char *fb_mode_name = NULL;
541 static fb_mode_t *fb_mode = NULL;
543 /* vt related variables */
544 static FILE *vt_fp = NULL;
545 static int vt_doit = 1;
547 /* vo_fbdev related variables */
548 static int fb_dev_fd;
549 static int fb_tty_fd = -1;
550 static size_t fb_size;
551 static uint8_t *frame_buffer;
552 static uint8_t *center; /* thx .so :) */
553 static struct fb_fix_screeninfo fb_finfo;
554 static struct fb_var_screeninfo fb_orig_vinfo;
555 static struct fb_var_screeninfo fb_vinfo;
556 static unsigned short fb_ored[256], fb_ogreen[256], fb_oblue[256];
557 static struct fb_cmap fb_oldcmap = { 0, 256, fb_ored, fb_ogreen, fb_oblue };
558 static int fb_cmap_changed = 0;
559 static int fb_pixel_size; // 32: 4 24: 3 16: 2 15: 2
560 static int fb_bpp; // 32: 32 24: 24 16: 16 15: 15
561 static int fb_bpp_we_want; // 32: 32 24: 24 16: 16 15: 15
562 static int fb_line_len;
563 static int fb_xres;
564 static int fb_yres;
565 static void (*draw_alpha_p)(int w, int h, unsigned char *src,
566 unsigned char *srca, int stride, unsigned char *dst,
567 int dstride);
569 static int in_width;
570 static int in_height;
571 static int out_width;
572 static int out_height;
573 static int first_row;
574 static int last_row;
575 static uint32_t pixel_format;
576 static int fs;
579 * Note: this function is completely cut'n'pasted from
580 * Chris Lawrence's code.
581 * (modified a bit to fit in my code...)
583 static struct fb_cmap *make_directcolor_cmap(struct fb_var_screeninfo *var)
585 /* Hopefully any DIRECTCOLOR device will have a big enough palette
586 * to handle mapping the full color depth.
587 * e.g. 8 bpp -> 256 entry palette
589 * We could handle some sort of gamma here
591 int i, cols, rcols, gcols, bcols;
592 uint16_t *red, *green, *blue;
593 struct fb_cmap *cmap;
595 rcols = 1 << var->red.length;
596 gcols = 1 << var->green.length;
597 bcols = 1 << var->blue.length;
599 /* Make our palette the length of the deepest color */
600 cols = (rcols > gcols ? rcols : gcols);
601 cols = (cols > bcols ? cols : bcols);
603 red = malloc(cols * sizeof(red[0]));
604 if(!red) {
605 mp_msg(MSGT_VO, MSGL_V, "Can't allocate red palette with %d entries.\n", cols);
606 return NULL;
608 for(i=0; i< rcols; i++)
609 red[i] = (65535/(rcols-1)) * i;
611 green = malloc(cols * sizeof(green[0]));
612 if(!green) {
613 mp_msg(MSGT_VO, MSGL_V, "Can't allocate green palette with %d entries.\n", cols);
614 free(red);
615 return NULL;
617 for(i=0; i< gcols; i++)
618 green[i] = (65535/(gcols-1)) * i;
620 blue = malloc(cols * sizeof(blue[0]));
621 if(!blue) {
622 mp_msg(MSGT_VO, MSGL_V, "Can't allocate blue palette with %d entries.\n", cols);
623 free(red);
624 free(green);
625 return NULL;
627 for(i=0; i< bcols; i++)
628 blue[i] = (65535/(bcols-1)) * i;
630 cmap = malloc(sizeof(struct fb_cmap));
631 if(!cmap) {
632 mp_msg(MSGT_VO, MSGL_V, "Can't allocate color map\n");
633 free(red);
634 free(green);
635 free(blue);
636 return NULL;
638 cmap->start = 0;
639 cmap->transp = 0;
640 cmap->len = cols;
641 cmap->red = red;
642 cmap->blue = blue;
643 cmap->green = green;
644 cmap->transp = NULL;
646 return cmap;
650 static int fb_preinit(int reset)
652 static int fb_preinit_done = 0;
653 static int fb_works = 0;
655 if (reset)
657 fb_preinit_done = 0;
658 return 0;
661 if (fb_preinit_done)
662 return fb_works;
664 if (!fb_dev_name && !(fb_dev_name = getenv("FRAMEBUFFER")))
665 fb_dev_name = strdup("/dev/fb0");
666 mp_msg(MSGT_VO, MSGL_V, "using %s\n", fb_dev_name);
668 if ((fb_dev_fd = open(fb_dev_name, O_RDWR)) == -1) {
669 mp_msg(MSGT_VO, MSGL_ERR, "Can't open %s: %s\n", fb_dev_name, strerror(errno));
670 goto err_out;
672 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
673 mp_msg(MSGT_VO, MSGL_ERR, "Can't get VSCREENINFO: %s\n", strerror(errno));
674 goto err_out_fd;
676 fb_orig_vinfo = fb_vinfo;
678 if ((fb_tty_fd = open("/dev/tty", O_RDWR)) < 0) {
679 mp_msg(MSGT_VO, MSGL_ERR, "notice: Can't open /dev/tty: %s\n", strerror(errno));
682 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length +
683 fb_vinfo.blue.length + fb_vinfo.transp.length;
685 if (fb_bpp == 8 && !vo_dbpp) {
686 mp_msg(MSGT_VO, MSGL_ERR, "8 bpp output is not supported.\n");
687 goto err_out_tty_fd;
690 if (vo_dbpp) {
691 if (vo_dbpp != 15 && vo_dbpp != 16 && vo_dbpp != 24 &&
692 vo_dbpp != 32) {
693 mp_msg(MSGT_VO, MSGL_ERR, "can't switch to %d bpp\n", vo_dbpp);
694 goto err_out_fd;
696 fb_bpp = vo_dbpp;
699 if (!fb_mode_cfgfile)
700 fb_mode_cfgfile = strdup("/etc/fb.modes");
702 fb_preinit_done = 1;
703 fb_works = 1;
704 return 1;
705 err_out_tty_fd:
706 close(fb_tty_fd);
707 fb_tty_fd = -1;
708 err_out_fd:
709 close(fb_dev_fd);
710 fb_dev_fd = -1;
711 err_out:
712 fb_preinit_done = 1;
713 fb_works = 0;
714 return 0;
717 static void lots_of_printf(void)
719 mp_msg(MSGT_VO, MSGL_V, "var info:\n");
720 mp_msg(MSGT_VO, MSGL_V, "xres: %u\n", fb_vinfo.xres);
721 mp_msg(MSGT_VO, MSGL_V, "yres: %u\n", fb_vinfo.yres);
722 mp_msg(MSGT_VO, MSGL_V, "xres_virtual: %u\n", fb_vinfo.xres_virtual);
723 mp_msg(MSGT_VO, MSGL_V, "yres_virtual: %u\n", fb_vinfo.yres_virtual);
724 mp_msg(MSGT_VO, MSGL_V, "xoffset: %u\n", fb_vinfo.xoffset);
725 mp_msg(MSGT_VO, MSGL_V, "yoffset: %u\n", fb_vinfo.yoffset);
726 mp_msg(MSGT_VO, MSGL_V, "bits_per_pixel: %u\n", fb_vinfo.bits_per_pixel);
727 mp_msg(MSGT_VO, MSGL_V, "grayscale: %u\n", fb_vinfo.grayscale);
728 mp_msg(MSGT_VO, MSGL_V, "red: %lu %lu %lu\n",
729 (unsigned long) fb_vinfo.red.offset,
730 (unsigned long) fb_vinfo.red.length,
731 (unsigned long) fb_vinfo.red.msb_right);
732 mp_msg(MSGT_VO, MSGL_V, "green: %lu %lu %lu\n",
733 (unsigned long) fb_vinfo.green.offset,
734 (unsigned long) fb_vinfo.green.length,
735 (unsigned long) fb_vinfo.green.msb_right);
736 mp_msg(MSGT_VO, MSGL_V, "blue: %lu %lu %lu\n",
737 (unsigned long) fb_vinfo.blue.offset,
738 (unsigned long) fb_vinfo.blue.length,
739 (unsigned long) fb_vinfo.blue.msb_right);
740 mp_msg(MSGT_VO, MSGL_V, "transp: %lu %lu %lu\n",
741 (unsigned long) fb_vinfo.transp.offset,
742 (unsigned long) fb_vinfo.transp.length,
743 (unsigned long) fb_vinfo.transp.msb_right);
744 mp_msg(MSGT_VO, MSGL_V, "nonstd: %u\n", fb_vinfo.nonstd);
745 mp_msg(MSGT_VO, MSGL_DBG2, "activate: %u\n", fb_vinfo.activate);
746 mp_msg(MSGT_VO, MSGL_DBG2, "height: %u\n", fb_vinfo.height);
747 mp_msg(MSGT_VO, MSGL_DBG2, "width: %u\n", fb_vinfo.width);
748 mp_msg(MSGT_VO, MSGL_DBG2, "accel_flags: %u\n", fb_vinfo.accel_flags);
749 mp_msg(MSGT_VO, MSGL_DBG2, "timing:\n");
750 mp_msg(MSGT_VO, MSGL_DBG2, "pixclock: %u\n", fb_vinfo.pixclock);
751 mp_msg(MSGT_VO, MSGL_DBG2, "left_margin: %u\n", fb_vinfo.left_margin);
752 mp_msg(MSGT_VO, MSGL_DBG2, "right_margin: %u\n", fb_vinfo.right_margin);
753 mp_msg(MSGT_VO, MSGL_DBG2, "upper_margin: %u\n", fb_vinfo.upper_margin);
754 mp_msg(MSGT_VO, MSGL_DBG2, "lower_margin: %u\n", fb_vinfo.lower_margin);
755 mp_msg(MSGT_VO, MSGL_DBG2, "hsync_len: %u\n", fb_vinfo.hsync_len);
756 mp_msg(MSGT_VO, MSGL_DBG2, "vsync_len: %u\n", fb_vinfo.vsync_len);
757 mp_msg(MSGT_VO, MSGL_DBG2, "sync: %u\n", fb_vinfo.sync);
758 mp_msg(MSGT_VO, MSGL_DBG2, "vmode: %u\n", fb_vinfo.vmode);
759 mp_msg(MSGT_VO, MSGL_V, "fix info:\n");
760 mp_msg(MSGT_VO, MSGL_V, "framebuffer size: %d bytes\n", fb_finfo.smem_len);
761 mp_msg(MSGT_VO, MSGL_V, "type: %lu\n", (unsigned long) fb_finfo.type);
762 mp_msg(MSGT_VO, MSGL_V, "type_aux: %lu\n", (unsigned long) fb_finfo.type_aux);
763 mp_msg(MSGT_VO, MSGL_V, "visual: %lu\n", (unsigned long) fb_finfo.visual);
764 mp_msg(MSGT_VO, MSGL_V, "line_length: %lu bytes\n", (unsigned long) fb_finfo.line_length);
765 mp_msg(MSGT_VO, MSGL_DBG2, "id: %.16s\n", fb_finfo.id);
766 mp_msg(MSGT_VO, MSGL_DBG2, "smem_start: %p\n", (void *) fb_finfo.smem_start);
767 mp_msg(MSGT_VO, MSGL_DBG2, "xpanstep: %u\n", fb_finfo.xpanstep);
768 mp_msg(MSGT_VO, MSGL_DBG2, "ypanstep: %u\n", fb_finfo.ypanstep);
769 mp_msg(MSGT_VO, MSGL_DBG2, "ywrapstep: %u\n", fb_finfo.ywrapstep);
770 mp_msg(MSGT_VO, MSGL_DBG2, "mmio_start: %p\n", (void *) fb_finfo.mmio_start);
771 mp_msg(MSGT_VO, MSGL_DBG2, "mmio_len: %u bytes\n", fb_finfo.mmio_len);
772 mp_msg(MSGT_VO, MSGL_DBG2, "accel: %u\n", fb_finfo.accel);
773 mp_msg(MSGT_VO, MSGL_V, "fb_bpp: %d\n", fb_bpp);
774 mp_msg(MSGT_VO, MSGL_V, "fb_pixel_size: %d bytes\n", fb_pixel_size);
775 mp_msg(MSGT_VO, MSGL_V, "other:\n");
776 mp_msg(MSGT_VO, MSGL_V, "in_width: %d\n", in_width);
777 mp_msg(MSGT_VO, MSGL_V, "in_height: %d\n", in_height);
778 mp_msg(MSGT_VO, MSGL_V, "out_width: %d\n", out_width);
779 mp_msg(MSGT_VO, MSGL_V, "out_height: %d\n", out_height);
780 mp_msg(MSGT_VO, MSGL_V, "first_row: %d\n", first_row);
781 mp_msg(MSGT_VO, MSGL_V, "last_row: %d\n", last_row);
782 mp_msg(MSGT_VO, MSGL_DBG2, "draw_alpha_p:%dbpp = %p\n", fb_bpp, draw_alpha_p);
785 static void vt_set_textarea(int u, int l)
787 /* how can I determine the font height?
788 * just use 16 for now
790 int urow = ((u + 15) / 16) + 1;
791 int lrow = l / 16;
793 mp_msg(MSGT_VO, MSGL_DBG2, "vt_set_textarea(%d,%d): %d,%d\n", u, l, urow, lrow);
794 if(vt_fp) {
795 fprintf(vt_fp, "\33[%d;%dr\33[%d;%dH", urow, lrow, lrow, 0);
796 fflush(vt_fp);
800 static int config(uint32_t width, uint32_t height, uint32_t d_width,
801 uint32_t d_height, uint32_t flags, char *title,
802 uint32_t format)
804 struct fb_cmap *cmap;
805 int vm = flags & VOFLAG_MODESWITCHING;
806 int zoom = flags & VOFLAG_SWSCALE;
807 int vt_fd;
809 fs = flags & VOFLAG_FULLSCREEN;
811 if(pre_init_err == -2)
813 mp_msg(MSGT_VO, MSGL_ERR, "Internal fatal error: config() was called before preinit()\n");
814 return -1;
817 if (pre_init_err) return 1;
819 if (fb_mode_name && !vm) {
820 mp_msg(MSGT_VO, MSGL_ERR, "-fbmode can only be used with -vm\n");
821 return 1;
823 if (vm && (parse_fbmode_cfg(fb_mode_cfgfile) < 0))
824 return 1;
825 if (d_width && (fs || vm)) {
826 out_width = d_width;
827 out_height = d_height;
828 } else {
829 out_width = width;
830 out_height = height;
832 in_width = width;
833 in_height = height;
834 pixel_format = format;
836 if (fb_mode_name) {
837 if (!(fb_mode = find_mode_by_name(fb_mode_name))) {
838 mp_msg(MSGT_VO, MSGL_ERR, "can't find requested video mode\n");
839 return 1;
841 fb_mode2fb_vinfo(fb_mode, &fb_vinfo);
842 } else if (vm) {
843 monitor_hfreq = str2range(monitor_hfreq_str);
844 monitor_vfreq = str2range(monitor_vfreq_str);
845 monitor_dotclock = str2range(monitor_dotclock_str);
846 if (!monitor_hfreq || !monitor_vfreq || !monitor_dotclock) {
847 mp_msg(MSGT_VO, MSGL_ERR, "you have to specify the capabilities of"
848 " the monitor.\n");
849 return 1;
851 if (!(fb_mode = find_best_mode(out_width, out_height,
852 monitor_hfreq, monitor_vfreq,
853 monitor_dotclock))) {
854 mp_msg(MSGT_VO, MSGL_ERR, "can't find best video mode\n");
855 return 1;
857 mp_msg(MSGT_VO, MSGL_V, "using mode %dx%d @ %.1fHz\n", fb_mode->xres,
858 fb_mode->yres, vsf(fb_mode));
859 fb_mode2fb_vinfo(fb_mode, &fb_vinfo);
861 fb_bpp_we_want = fb_bpp;
862 set_bpp(&fb_vinfo, fb_bpp);
863 fb_vinfo.xres_virtual = fb_vinfo.xres;
864 fb_vinfo.yres_virtual = fb_vinfo.yres;
866 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_GRAPHICS) < 0) {
867 mp_msg(MSGT_VO, MSGL_V, "Can't set graphics mode: %s\n", strerror(errno));
868 close(fb_tty_fd);
869 fb_tty_fd = -1;
872 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo)) {
873 mp_msg(MSGT_VO, MSGL_ERR, "Can't put VSCREENINFO: %s\n", strerror(errno));
874 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0) {
875 mp_msg(MSGT_VO, MSGL_ERR, "Can't restore text mode: %s\n", strerror(errno));
877 return 1;
880 fb_pixel_size = fb_vinfo.bits_per_pixel / 8;
881 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length +
882 fb_vinfo.blue.length + fb_vinfo.transp.length;
883 if (fb_bpp_we_want != fb_bpp)
884 mp_msg(MSGT_VO, MSGL_WARN, "requested %d bpp, got %d bpp!!!\n",
885 fb_bpp_we_want, fb_bpp);
887 switch (fb_bpp) {
888 case 32: draw_alpha_p = vo_draw_alpha_rgb32; break;
889 case 24: draw_alpha_p = vo_draw_alpha_rgb24; break;
890 case 16: draw_alpha_p = vo_draw_alpha_rgb16; break;
891 case 15: draw_alpha_p = vo_draw_alpha_rgb15; break;
892 default: return 1;
895 fb_xres = fb_vinfo.xres;
896 fb_yres = fb_vinfo.yres;
898 if (vm || fs) {
899 out_width = fb_xres;
900 out_height = fb_yres;
902 if (out_width < in_width || out_height < in_height) {
903 mp_msg(MSGT_VO, MSGL_ERR, "screensize is smaller than video size\n");
904 return 1;
907 first_row = (out_height - in_height) / 2;
908 last_row = (out_height + in_height) / 2;
910 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
911 mp_msg(MSGT_VO, MSGL_ERR, "Can't get FSCREENINFO: %s\n", strerror(errno));
912 return 1;
915 lots_of_printf();
917 if (fb_finfo.type != FB_TYPE_PACKED_PIXELS) {
918 mp_msg(MSGT_VO, MSGL_ERR, "type %d not supported\n", fb_finfo.type);
919 return 1;
922 switch (fb_finfo.visual) {
923 case FB_VISUAL_TRUECOLOR:
924 break;
925 case FB_VISUAL_DIRECTCOLOR:
926 mp_msg(MSGT_VO, MSGL_V, "creating cmap for directcolor\n");
927 if (ioctl(fb_dev_fd, FBIOGETCMAP, &fb_oldcmap)) {
928 mp_msg(MSGT_VO, MSGL_ERR, "can't get cmap: %s\n",
929 strerror(errno));
930 return 1;
932 if (!(cmap = make_directcolor_cmap(&fb_vinfo)))
933 return 1;
934 if (ioctl(fb_dev_fd, FBIOPUTCMAP, cmap)) {
935 mp_msg(MSGT_VO, MSGL_ERR, "can't put cmap: %s\n",
936 strerror(errno));
937 return 1;
939 fb_cmap_changed = 1;
940 free(cmap->red);
941 free(cmap->green);
942 free(cmap->blue);
943 free(cmap);
944 break;
945 default:
946 mp_msg(MSGT_VO, MSGL_ERR, "visual: %d not yet supported\n",
947 fb_finfo.visual);
948 return 1;
951 fb_line_len = fb_finfo.line_length;
952 fb_size = fb_finfo.smem_len;
953 frame_buffer = NULL;
954 #ifdef CONFIG_VIDIX
955 if(vidix_name)
957 unsigned image_width,image_height,x_offset,y_offset;
958 if(zoom || fs){
959 aspect_save_orig(width,height);
960 aspect_save_prescale(d_width,d_height);
961 aspect_save_screenres(fb_xres,fb_yres);
962 aspect(&image_width,&image_height,fs ? A_ZOOM : A_NOZOOM);
963 } else {
964 image_width=width;
965 image_height=height;
968 if(fb_xres > image_width)
969 x_offset = (fb_xres - image_width) / 2;
970 else x_offset = 0;
971 if(fb_yres > image_height)
972 y_offset = (fb_yres - image_height) / 2;
973 else y_offset = 0;
975 if(vidix_init(width,height,x_offset,y_offset,image_width,
976 image_height,format,fb_bpp,
977 fb_xres,fb_yres) != 0)
979 mp_msg(MSGT_VO, MSGL_ERR, "Can't initialize VIDIX driver\n");
980 vidix_name = NULL;
981 vidix_term();
982 return -1;
984 else mp_msg(MSGT_VO, MSGL_V, "Using VIDIX\n");
985 vidix_start();
986 if (vidix_grkey_support())
988 vidix_grkey_get(&gr_key);
989 gr_key.key_op = KEYS_PUT;
990 if (!(vo_colorkey & 0xff000000))
992 gr_key.ckey.op = CKEY_TRUE;
993 gr_key.ckey.red = (vo_colorkey & 0x00ff0000) >> 16;
994 gr_key.ckey.green = (vo_colorkey & 0x0000ff00) >> 8;
995 gr_key.ckey.blue = vo_colorkey & 0x000000ff;
997 else
998 gr_key.ckey.op = CKEY_FALSE;
999 vidix_grkey_set(&gr_key);
1002 else
1003 #endif
1005 int x_offset=0,y_offset=0;
1006 if ((frame_buffer = (uint8_t *) mmap(0, fb_size, PROT_READ | PROT_WRITE,
1007 MAP_SHARED, fb_dev_fd, 0)) == (uint8_t *) -1) {
1008 mp_msg(MSGT_VO, MSGL_ERR, "Can't mmap %s: %s\n", fb_dev_name, strerror(errno));
1009 return 1;
1012 center = frame_buffer +
1013 ( (out_width - in_width) / 2 ) * fb_pixel_size +
1014 ( (out_height - in_height) / 2 ) * fb_line_len +
1015 x_offset * fb_pixel_size + y_offset * fb_line_len;
1017 mp_msg(MSGT_VO, MSGL_DBG2, "frame_buffer @ %p\n", frame_buffer);
1018 mp_msg(MSGT_VO, MSGL_DBG2, "center @ %p\n", center);
1019 mp_msg(MSGT_VO, MSGL_V, "pixel per line: %d\n", fb_line_len / fb_pixel_size);
1021 if (fs || vm)
1022 memset(frame_buffer, '\0', fb_line_len * fb_yres);
1024 if (vt_doit && (vt_fd = open("/dev/tty", O_WRONLY)) == -1) {
1025 mp_msg(MSGT_VO, MSGL_ERR, "can't open /dev/tty: %s\n", strerror(errno));
1026 vt_doit = 0;
1028 if (vt_doit && !(vt_fp = fdopen(vt_fd, "w"))) {
1029 mp_msg(MSGT_VO, MSGL_ERR, "can't fdopen /dev/tty: %s\n", strerror(errno));
1030 vt_doit = 0;
1033 if (vt_doit)
1034 vt_set_textarea(last_row, fb_yres);
1036 return 0;
1039 static int query_format(uint32_t format)
1041 if (!fb_preinit(0))
1042 return 0;
1043 #ifdef CONFIG_VIDIX
1044 if(vidix_name)
1045 return (vidix_query_fourcc(format));
1046 #endif
1047 if ((format & IMGFMT_BGR_MASK) == IMGFMT_BGR) {
1048 int bpp = format & 0xff;
1050 if (bpp == fb_bpp)
1051 return VFCAP_ACCEPT_STRIDE | VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
1053 return 0;
1056 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
1057 unsigned char *srca, int stride)
1059 unsigned char *dst;
1061 dst = center + fb_line_len * y0 + fb_pixel_size * x0;
1063 (*draw_alpha_p)(w, h, src, srca, stride, dst, fb_line_len);
1066 static int draw_frame(uint8_t *src[]) { return 1; }
1068 static int draw_slice(uint8_t *src[], int stride[], int w, int h, int x,
1069 int y)
1071 uint8_t *d;
1072 uint8_t *s;
1074 d = center + fb_line_len * y + fb_pixel_size * x;
1076 s = src[0];
1077 while (h) {
1078 memcpy(d, s, w * fb_pixel_size);
1079 d += fb_line_len;
1080 s += stride[0];
1081 h--;
1084 return 0;
1087 static void check_events(void)
1091 static void flip_page(void)
1095 static void draw_osd(void)
1097 vo_draw_text(in_width, in_height, draw_alpha);
1100 static void uninit(void)
1102 if (fb_cmap_changed) {
1103 if (ioctl(fb_dev_fd, FBIOPUTCMAP, &fb_oldcmap))
1104 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore original cmap\n");
1105 fb_cmap_changed = 0;
1107 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo))
1108 mp_msg(MSGT_VO, MSGL_WARN, "ioctl FBIOGET_VSCREENINFO: %s\n", strerror(errno));
1109 fb_orig_vinfo.xoffset = fb_vinfo.xoffset;
1110 fb_orig_vinfo.yoffset = fb_vinfo.yoffset;
1111 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_orig_vinfo))
1112 mp_msg(MSGT_VO, MSGL_WARN, "Can't reset original fb_var_screeninfo: %s\n", strerror(errno));
1113 if (fb_tty_fd >= 0) {
1114 if (ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0)
1115 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore text mode: %s\n", strerror(errno));
1117 if (vt_doit)
1118 vt_set_textarea(0, fb_orig_vinfo.yres);
1119 close(fb_tty_fd);
1120 close(fb_dev_fd);
1121 if(frame_buffer) munmap(frame_buffer, fb_size);
1122 frame_buffer = NULL;
1123 #ifdef CONFIG_VIDIX
1124 if(vidix_name) vidix_term();
1125 #endif
1126 fb_preinit(1);
1129 static int preinit(const char *vo_subdevice)
1131 pre_init_err = 0;
1133 if(vo_subdevice)
1135 #ifdef CONFIG_VIDIX
1136 if (memcmp(vo_subdevice, "vidix", 5) == 0)
1137 vidix_name = &vo_subdevice[5];
1138 if(vidix_name)
1139 pre_init_err = vidix_preinit(vidix_name,&video_out_fbdev);
1140 else
1141 #endif
1143 if (fb_dev_name) free(fb_dev_name);
1144 fb_dev_name = strdup(vo_subdevice);
1147 if(!pre_init_err) return (pre_init_err=(fb_preinit(0)?0:-1));
1148 return(-1);
1151 static uint32_t get_image(mp_image_t *mpi)
1153 if (
1154 !IMGFMT_IS_BGR(mpi->imgfmt) ||
1155 (IMGFMT_BGR_DEPTH(mpi->imgfmt) != fb_bpp) ||
1156 ((mpi->type != MP_IMGTYPE_STATIC) && (mpi->type != MP_IMGTYPE_TEMP)) ||
1157 (mpi->flags & MP_IMGFLAG_PLANAR) ||
1158 (mpi->flags & MP_IMGFLAG_YUV) ||
1159 (mpi->width != in_width) ||
1160 (mpi->height != in_height)
1162 return(VO_FALSE);
1164 mpi->planes[0] = center;
1165 mpi->stride[0] = fb_line_len;
1166 mpi->flags |= MP_IMGFLAG_DIRECT;
1167 return(VO_TRUE);
1170 static int control(uint32_t request, void *data, ...)
1172 switch (request) {
1173 case VOCTRL_GET_IMAGE:
1174 return get_image(data);
1175 case VOCTRL_QUERY_FORMAT:
1176 return query_format(*((uint32_t*)data));
1179 #ifdef CONFIG_VIDIX
1180 if (vidix_name) {
1181 switch (request) {
1182 case VOCTRL_SET_EQUALIZER:
1184 va_list ap;
1185 int value;
1187 va_start(ap, data);
1188 value = va_arg(ap, int);
1189 va_end(ap);
1191 return vidix_control(request, data, (int *)value);
1193 case VOCTRL_GET_EQUALIZER:
1195 va_list ap;
1196 int *value;
1198 va_start(ap, data);
1199 value = va_arg(ap, int*);
1200 va_end(ap);
1202 return vidix_control(request, data, value);
1206 #endif
1208 return VO_NOTIMPL;