Replace informal GPL license header by standard GPL header.
[mplayer/glamo.git] / libvo / vo_fbdev.c
blob5f621967cfc7391851f0a144fa1271f74935d504
1 /*
2 * video driver for framebuffer device
3 * copyright (C) 2001 Szabolcs Berecz <szabi@inf.elte.hu>
5 * Some idea and code borrowed from Chris Lawrence's ppmtofb-0.27
6 * Some fixes and small improvements by Joey Parrish <joey@nicewarrior.org>
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <ctype.h>
33 #include <sys/mman.h>
34 #include <sys/ioctl.h>
35 #include <sys/kd.h>
36 #include <linux/fb.h>
38 #include "config.h"
39 #include "video_out.h"
40 #include "video_out_internal.h"
41 #include "fastmemcpy.h"
42 #include "sub.h"
43 #include "geometry.h"
44 #ifdef CONFIG_VIDIX
45 #include "vosub_vidix.h"
46 #endif
47 #include "aspect.h"
48 #include "mp_msg.h"
50 static const vo_info_t info = {
51 "Framebuffer Device",
52 "fbdev",
53 "Szabolcs Berecz <szabi@inf.elte.hu>",
57 LIBVO_EXTERN(fbdev)
59 #ifdef CONFIG_VIDIX
60 /* Name of VIDIX driver */
61 static const char *vidix_name = NULL;
62 static vidix_grkey_t gr_key;
63 #endif
64 static signed int pre_init_err = -2;
65 /******************************
66 * fb.modes support *
67 ******************************/
69 static range_t *monitor_hfreq = NULL;
70 static range_t *monitor_vfreq = NULL;
71 static range_t *monitor_dotclock = NULL;
73 typedef struct {
74 char *name;
75 uint32_t xres, yres, vxres, vyres, depth;
76 uint32_t pixclock, left, right, upper, lower, hslen, vslen;
77 uint32_t sync;
78 uint32_t vmode;
79 } fb_mode_t;
81 #define MAX_NR_TOKEN 16
83 #define MAX_LINE_LEN 1000
85 #define RET_EOF -1
86 #define RET_EOL -2
88 static int validate_mode(fb_mode_t *m)
90 if (!m->xres) {
91 mp_msg(MSGT_VO, MSGL_V, "needs geometry ");
92 return 0;
94 if (!m->pixclock) {
95 mp_msg(MSGT_VO, MSGL_V, "needs timings ");
96 return 0;
98 return 1;
101 static FILE *fp;
102 static int line_num = 0;
103 static char *line;
104 static char *token[MAX_NR_TOKEN];
106 static int get_token(int num)
108 static int read_nextline = 1;
109 static int line_pos;
110 int i;
111 char c;
113 if (num >= MAX_NR_TOKEN) {
114 mp_msg(MSGT_VO, MSGL_V, "get_token(): max >= MAX_NR_TOKEN!\n");
115 goto out_eof;
118 if (read_nextline) {
119 if (!fgets(line, MAX_LINE_LEN, fp))
120 goto out_eof;
121 line_pos = 0;
122 ++line_num;
123 read_nextline = 0;
125 for (i = 0; i < num; i++) {
126 while (isspace(line[line_pos]))
127 ++line_pos;
128 if (line[line_pos] == '\0' || line[line_pos] == '#') {
129 read_nextline = 1;
130 goto out_eol;
132 token[i] = line + line_pos;
133 c = line[line_pos];
134 if (c == '"' || c == '\'') {
135 token[i]++;
136 while (line[++line_pos] != c && line[line_pos])
137 /* NOTHING */;
138 if (!line[line_pos])
139 goto out_eol;
140 line[line_pos] = ' ';
141 } else {
142 for (/* NOTHING */; !isspace(line[line_pos]) &&
143 line[line_pos]; line_pos++)
144 /* NOTHING */;
146 if (!line[line_pos]) {
147 read_nextline = 1;
148 if (i == num - 1)
149 goto out_ok;
150 goto out_eol;
152 line[line_pos++] = '\0';
154 out_ok:
155 return i;
156 out_eof:
157 return RET_EOF;
158 out_eol:
159 return RET_EOL;
162 static fb_mode_t *fb_modes = NULL;
163 static int nr_modes = 0;
165 static int parse_fbmode_cfg(char *cfgfile)
167 #define CHECK_IN_MODE_DEF\
168 if (!in_mode_def) {\
169 mp_msg(MSGT_VO, MSGL_V, "'needs 'mode' first");\
170 goto err_out_print_linenum;\
172 fb_mode_t *mode = NULL;
173 char *endptr; // strtoul()...
174 int in_mode_def = 0;
175 int tmp, i;
177 /* If called more than once, reuse parsed data */
178 if (nr_modes)
179 return nr_modes;
181 mp_msg(MSGT_VO, MSGL_V, "Reading %s: ", cfgfile);
183 if ((fp = fopen(cfgfile, "r")) == NULL) {
184 mp_msg(MSGT_VO, MSGL_V, "can't open '%s': %s\n", cfgfile, strerror(errno));
185 return -1;
188 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) {
189 mp_msg(MSGT_VO, MSGL_V, "can't get memory for 'line': %s\n", strerror(errno));
190 return -2;
194 * check if the cfgfile starts with 'mode'
196 while ((tmp = get_token(1)) == RET_EOL)
197 /* NOTHING */;
198 if (tmp == RET_EOF)
199 goto out;
200 if (!strcmp(token[0], "mode"))
201 goto loop_enter;
202 goto err_out_parse_error;
204 while ((tmp = get_token(1)) != RET_EOF) {
205 if (tmp == RET_EOL)
206 continue;
207 if (!strcmp(token[0], "mode")) {
208 if (in_mode_def) {
209 mp_msg(MSGT_VO, MSGL_V, "'endmode' required");
210 goto err_out_print_linenum;
212 if (!validate_mode(mode))
213 goto err_out_not_valid;
214 loop_enter:
215 if (!(fb_modes = (fb_mode_t *)
216 realloc(fb_modes, sizeof(fb_mode_t) * (nr_modes + 1)))) {
217 mp_msg(MSGT_VO, MSGL_V, "can't realloc 'fb_modes' (nr_modes = %d):"
218 " %s\n", nr_modes, strerror(errno));
219 goto err_out;
221 mode = fb_modes + nr_modes;
222 ++nr_modes;
223 memset(mode, 0, sizeof(fb_mode_t));
225 if (get_token(1) < 0)
226 goto err_out_parse_error;
227 for (i = 0; i < nr_modes - 1; i++) {
228 if (!strcmp(token[0], fb_modes[i].name)) {
229 mp_msg(MSGT_VO, MSGL_V, "mode name '%s' isn't unique", token[0]);
230 goto err_out_print_linenum;
233 if (!(mode->name = strdup(token[0]))) {
234 mp_msg(MSGT_VO, MSGL_V, "can't strdup -> 'name': %s\n", strerror(errno));
235 goto err_out;
237 in_mode_def = 1;
238 } else if (!strcmp(token[0], "geometry")) {
239 CHECK_IN_MODE_DEF;
240 if (get_token(5) < 0)
241 goto err_out_parse_error;
242 mode->xres = strtoul(token[0], &endptr, 0);
243 if (*endptr)
244 goto err_out_parse_error;
245 mode->yres = strtoul(token[1], &endptr, 0);
246 if (*endptr)
247 goto err_out_parse_error;
248 mode->vxres = strtoul(token[2], &endptr, 0);
249 if (*endptr)
250 goto err_out_parse_error;
251 mode->vyres = strtoul(token[3], &endptr, 0);
252 if (*endptr)
253 goto err_out_parse_error;
254 mode->depth = strtoul(token[4], &endptr, 0);
255 if (*endptr)
256 goto err_out_parse_error;
257 } else if (!strcmp(token[0], "timings")) {
258 CHECK_IN_MODE_DEF;
259 if (get_token(7) < 0)
260 goto err_out_parse_error;
261 mode->pixclock = strtoul(token[0], &endptr, 0);
262 if (*endptr)
263 goto err_out_parse_error;
264 mode->left = strtoul(token[1], &endptr, 0);
265 if (*endptr)
266 goto err_out_parse_error;
267 mode->right = strtoul(token[2], &endptr, 0);
268 if (*endptr)
269 goto err_out_parse_error;
270 mode->upper = strtoul(token[3], &endptr, 0);
271 if (*endptr)
272 goto err_out_parse_error;
273 mode->lower = strtoul(token[4], &endptr, 0);
274 if (*endptr)
275 goto err_out_parse_error;
276 mode->hslen = strtoul(token[5], &endptr, 0);
277 if (*endptr)
278 goto err_out_parse_error;
279 mode->vslen = strtoul(token[6], &endptr, 0);
280 if (*endptr)
281 goto err_out_parse_error;
282 } else if (!strcmp(token[0], "endmode")) {
283 CHECK_IN_MODE_DEF;
284 in_mode_def = 0;
285 } else if (!strcmp(token[0], "accel")) {
286 CHECK_IN_MODE_DEF;
287 if (get_token(1) < 0)
288 goto err_out_parse_error;
290 * it's only used for text acceleration
291 * so we just ignore it.
293 } else if (!strcmp(token[0], "hsync")) {
294 CHECK_IN_MODE_DEF;
295 if (get_token(1) < 0)
296 goto err_out_parse_error;
297 if (!strcmp(token[0], "low"))
298 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
299 else if (!strcmp(token[0], "high"))
300 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
301 else
302 goto err_out_parse_error;
303 } else if (!strcmp(token[0], "vsync")) {
304 CHECK_IN_MODE_DEF;
305 if (get_token(1) < 0)
306 goto err_out_parse_error;
307 if (!strcmp(token[0], "low"))
308 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
309 else if (!strcmp(token[0], "high"))
310 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
311 else
312 goto err_out_parse_error;
313 } else if (!strcmp(token[0], "csync")) {
314 CHECK_IN_MODE_DEF;
315 if (get_token(1) < 0)
316 goto err_out_parse_error;
317 if (!strcmp(token[0], "low"))
318 mode->sync &= ~FB_SYNC_COMP_HIGH_ACT;
319 else if (!strcmp(token[0], "high"))
320 mode->sync |= FB_SYNC_COMP_HIGH_ACT;
321 else
322 goto err_out_parse_error;
323 } else if (!strcmp(token[0], "extsync")) {
324 CHECK_IN_MODE_DEF;
325 if (get_token(1) < 0)
326 goto err_out_parse_error;
327 if (!strcmp(token[0], "false"))
328 mode->sync &= ~FB_SYNC_EXT;
329 else if (!strcmp(token[0], "true"))
330 mode->sync |= FB_SYNC_EXT;
331 else
332 goto err_out_parse_error;
333 } else if (!strcmp(token[0], "laced")) {
334 CHECK_IN_MODE_DEF;
335 if (get_token(1) < 0)
336 goto err_out_parse_error;
337 if (!strcmp(token[0], "false"))
338 mode->vmode = FB_VMODE_NONINTERLACED;
339 else if (!strcmp(token[0], "true"))
340 mode->vmode = FB_VMODE_INTERLACED;
341 else
342 goto err_out_parse_error;
343 } else if (!strcmp(token[0], "double")) {
344 CHECK_IN_MODE_DEF;
345 if (get_token(1) < 0)
346 goto err_out_parse_error;
347 if (!strcmp(token[0], "false"))
349 else if (!strcmp(token[0], "true"))
350 mode->vmode = FB_VMODE_DOUBLE;
351 else
352 goto err_out_parse_error;
353 } else
354 goto err_out_parse_error;
356 if (!validate_mode(mode))
357 goto err_out_not_valid;
358 out:
359 mp_msg(MSGT_VO, MSGL_V, "%d modes\n", nr_modes);
360 free(line);
361 fclose(fp);
362 return nr_modes;
363 err_out_parse_error:
364 mp_msg(MSGT_VO, MSGL_V, "parse error");
365 err_out_print_linenum:
366 mp_msg(MSGT_VO, MSGL_V, " at line %d\n", line_num);
367 err_out:
368 if (fb_modes) {
369 free(fb_modes);
370 fb_modes = NULL;
372 nr_modes = 0;
373 free(line);
374 free(fp);
375 return -2;
376 err_out_not_valid:
377 mp_msg(MSGT_VO, MSGL_V, "previous mode is not correct");
378 goto err_out_print_linenum;
381 static fb_mode_t *find_mode_by_name(char *name)
383 int i;
385 for (i = 0; i < nr_modes; i++)
386 if (!strcmp(name, fb_modes[i].name))
387 return fb_modes + i;
388 return NULL;
391 static float dcf(fb_mode_t *m) //driving clock frequency
393 return 1e12f / m->pixclock;
396 static float hsf(fb_mode_t *m) //horizontal scan frequency
398 int htotal = m->left + m->xres + m->right + m->hslen;
399 return dcf(m) / htotal;
402 static float vsf(fb_mode_t *m) //vertical scan frequency
404 int vtotal = m->upper + m->yres + m->lower + m->vslen;
405 return hsf(m) / vtotal;
409 static int mode_works(fb_mode_t *m, range_t *hfreq, range_t *vfreq,
410 range_t *dotclock)
412 float h = hsf(m);
413 float v = vsf(m);
414 float d = dcf(m);
415 int ret = 1;
417 mp_msg(MSGT_VO, MSGL_DBG2, "mode %dx%d:", m->xres, m->yres);
418 if (!in_range(hfreq, h)) {
419 ret = 0;
420 mp_msg(MSGT_VO, MSGL_DBG2, " hsync out of range.");
422 if (!in_range(vfreq, v)) {
423 ret = 0;
424 mp_msg(MSGT_VO, MSGL_DBG2, " vsync out of range.");
426 if (!in_range(dotclock, d)) {
427 ret = 0;
428 mp_msg(MSGT_VO, MSGL_DBG2, " dotclock out of range.");
430 if (ret)
431 mp_msg(MSGT_VO, MSGL_DBG2, " hsync, vsync, dotclock ok.\n");
432 else
433 mp_msg(MSGT_VO, MSGL_DBG2, "\n");
435 return ret;
438 static fb_mode_t *find_best_mode(int xres, int yres, range_t *hfreq,
439 range_t *vfreq, range_t *dotclock)
441 int i;
442 fb_mode_t *best = fb_modes;
443 fb_mode_t *curr;
445 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for first working mode\n");
447 for (i = 0; i < nr_modes; i++, best++)
448 if (mode_works(best, hfreq, vfreq, dotclock))
449 break;
451 if (i == nr_modes)
452 return NULL;
453 if (i == nr_modes - 1)
454 return best;
456 mp_msg(MSGT_VO, MSGL_DBG2, "First working mode: %dx%d\n", best->xres, best->yres);
457 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for better modes\n");
459 for (curr = best + 1; i < nr_modes - 1; i++, curr++) {
460 if (!mode_works(curr, hfreq, vfreq, dotclock))
461 continue;
463 if (best->xres < xres || best->yres < yres) {
464 if (curr->xres > best->xres || curr->yres > best->yres) {
465 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too small.\n",
466 best->xres, best->yres);
467 best = curr;
468 } else
469 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
470 } else if (curr->xres == best->xres && curr->yres == best->yres &&
471 vsf(curr) > vsf(best)) {
472 mp_msg(MSGT_VO, MSGL_DBG2, "faster screen refresh.\n");
473 best = curr;
474 } else if ((curr->xres <= best->xres && curr->yres <= best->yres) &&
475 (curr->xres >= xres && curr->yres >= yres)) {
476 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too large.\n",
477 best->xres, best->yres);
478 best = curr;
479 } else {
480 if (curr->xres < xres || curr->yres < yres)
481 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
482 else if (curr->xres > best->xres || curr->yres > best->yres)
483 mp_msg(MSGT_VO, MSGL_DBG2, "too large.\n");
484 else
485 mp_msg(MSGT_VO, MSGL_DBG2, "it's worse, don't know why.\n");
489 return best;
492 static void set_bpp(struct fb_var_screeninfo *p, int bpp)
494 p->bits_per_pixel = (bpp + 1) & ~1;
495 p->red.msb_right = p->green.msb_right = p->blue.msb_right = p->transp.msb_right = 0;
496 p->transp.offset = p->transp.length = 0;
497 p->blue.offset = 0;
498 switch (bpp) {
499 case 32:
500 p->transp.offset = 24;
501 p->transp.length = 8;
502 case 24:
503 p->red.offset = 16;
504 p->red.length = 8;
505 p->green.offset = 8;
506 p->green.length = 8;
507 p->blue.length = 8;
508 break;
509 case 16:
510 p->red.offset = 11;
511 p->green.length = 6;
512 p->red.length = 5;
513 p->green.offset = 5;
514 p->blue.length = 5;
515 break;
516 case 15:
517 p->red.offset = 10;
518 p->green.length = 5;
519 p->red.length = 5;
520 p->green.offset = 5;
521 p->blue.length = 5;
522 break;
526 static void fb_mode2fb_vinfo(fb_mode_t *m, struct fb_var_screeninfo *v)
528 v->xres = m->xres;
529 v->yres = m->yres;
530 v->xres_virtual = m->vxres;
531 v->yres_virtual = m->vyres;
532 set_bpp(v, m->depth);
533 v->pixclock = m->pixclock;
534 v->left_margin = m->left;
535 v->right_margin = m->right;
536 v->upper_margin = m->upper;
537 v->lower_margin = m->lower;
538 v->hsync_len = m->hslen;
539 v->vsync_len = m->vslen;
540 v->sync = m->sync;
541 v->vmode = m->vmode;
545 /******************************
546 * vo_fbdev *
547 ******************************/
549 /* command line/config file options */
550 static char *fb_dev_name = NULL;
551 char *fb_mode_cfgfile = NULL;
552 char *fb_mode_name = NULL;
554 static fb_mode_t *fb_mode = NULL;
556 /* vt related variables */
557 static FILE *vt_fp = NULL;
558 static int vt_doit = 1;
560 /* vo_fbdev related variables */
561 static int fb_dev_fd;
562 static int fb_tty_fd = -1;
563 static size_t fb_size;
564 static uint8_t *frame_buffer;
565 static uint8_t *center;
566 static struct fb_fix_screeninfo fb_finfo;
567 static struct fb_var_screeninfo fb_orig_vinfo;
568 static struct fb_var_screeninfo fb_vinfo;
569 static unsigned short fb_ored[256], fb_ogreen[256], fb_oblue[256];
570 static struct fb_cmap fb_oldcmap = { 0, 256, fb_ored, fb_ogreen, fb_oblue };
571 static int fb_cmap_changed = 0;
572 static int fb_pixel_size; // 32: 4 24: 3 16: 2 15: 2
573 static int fb_bpp; // 32: 32 24: 24 16: 16 15: 15
574 static int fb_bpp_we_want; // 32: 32 24: 24 16: 16 15: 15
575 static int fb_line_len;
576 static int fb_xres;
577 static int fb_yres;
578 static void (*draw_alpha_p)(int w, int h, unsigned char *src,
579 unsigned char *srca, int stride,
580 unsigned char *dst, int dstride);
582 static int in_width;
583 static int in_height;
584 static int out_width;
585 static int out_height;
586 static int first_row;
587 static int last_row;
588 static uint32_t pixel_format;
589 static int fs;
592 * Note: this function is completely cut'n'pasted from
593 * Chris Lawrence's code.
594 * (modified a bit to fit in my code...)
596 static struct fb_cmap *make_directcolor_cmap(struct fb_var_screeninfo *var)
598 /* Hopefully any DIRECTCOLOR device will have a big enough palette
599 * to handle mapping the full color depth.
600 * e.g. 8 bpp -> 256 entry palette
602 * We could handle some sort of gamma here
604 int i, cols, rcols, gcols, bcols;
605 uint16_t *red, *green, *blue;
606 struct fb_cmap *cmap;
608 rcols = 1 << var->red.length;
609 gcols = 1 << var->green.length;
610 bcols = 1 << var->blue.length;
612 /* Make our palette the length of the deepest color */
613 cols = (rcols > gcols ? rcols : gcols);
614 cols = (cols > bcols ? cols : bcols);
616 red = malloc(cols * sizeof(red[0]));
617 if (!red) {
618 mp_msg(MSGT_VO, MSGL_V, "Can't allocate red palette with %d entries.\n", cols);
619 return NULL;
621 for (i = 0; i < rcols; i++)
622 red[i] = (65535 / (rcols - 1)) * i;
624 green = malloc(cols * sizeof(green[0]));
625 if (!green) {
626 mp_msg(MSGT_VO, MSGL_V, "Can't allocate green palette with %d entries.\n", cols);
627 free(red);
628 return NULL;
630 for (i = 0; i < gcols; i++)
631 green[i] = (65535 / (gcols - 1)) * i;
633 blue = malloc(cols * sizeof(blue[0]));
634 if (!blue) {
635 mp_msg(MSGT_VO, MSGL_V, "Can't allocate blue palette with %d entries.\n", cols);
636 free(red);
637 free(green);
638 return NULL;
640 for (i = 0; i < bcols; i++)
641 blue[i] = (65535 / (bcols - 1)) * i;
643 cmap = malloc(sizeof(struct fb_cmap));
644 if (!cmap) {
645 mp_msg(MSGT_VO, MSGL_V, "Can't allocate color map\n");
646 free(red);
647 free(green);
648 free(blue);
649 return NULL;
651 cmap->start = 0;
652 cmap->transp = 0;
653 cmap->len = cols;
654 cmap->red = red;
655 cmap->blue = blue;
656 cmap->green = green;
657 cmap->transp = NULL;
659 return cmap;
663 static int fb_preinit(int reset)
665 static int fb_preinit_done = 0;
666 static int fb_works = 0;
668 if (reset) {
669 fb_preinit_done = 0;
670 return 0;
673 if (fb_preinit_done)
674 return fb_works;
676 if (!fb_dev_name && !(fb_dev_name = getenv("FRAMEBUFFER")))
677 fb_dev_name = strdup("/dev/fb0");
678 mp_msg(MSGT_VO, MSGL_V, "using %s\n", fb_dev_name);
680 if ((fb_dev_fd = open(fb_dev_name, O_RDWR)) == -1) {
681 mp_msg(MSGT_VO, MSGL_ERR, "Can't open %s: %s\n", fb_dev_name, strerror(errno));
682 goto err_out;
684 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
685 mp_msg(MSGT_VO, MSGL_ERR, "Can't get VSCREENINFO: %s\n", strerror(errno));
686 goto err_out_fd;
688 fb_orig_vinfo = fb_vinfo;
690 if ((fb_tty_fd = open("/dev/tty", O_RDWR)) < 0) {
691 mp_msg(MSGT_VO, MSGL_ERR, "notice: Can't open /dev/tty: %s\n", strerror(errno));
694 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length +
695 fb_vinfo.blue.length + fb_vinfo.transp.length;
697 if (fb_bpp == 8 && !vo_dbpp) {
698 mp_msg(MSGT_VO, MSGL_ERR, "8 bpp output is not supported.\n");
699 goto err_out_tty_fd;
702 if (vo_dbpp) {
703 if (vo_dbpp != 15 && vo_dbpp != 16 && vo_dbpp != 24 && vo_dbpp != 32) {
704 mp_msg(MSGT_VO, MSGL_ERR, "can't switch to %d bpp\n", vo_dbpp);
705 goto err_out_fd;
707 fb_bpp = vo_dbpp;
710 if (!fb_mode_cfgfile)
711 fb_mode_cfgfile = strdup("/etc/fb.modes");
713 fb_preinit_done = 1;
714 fb_works = 1;
715 return 1;
716 err_out_tty_fd:
717 close(fb_tty_fd);
718 fb_tty_fd = -1;
719 err_out_fd:
720 close(fb_dev_fd);
721 fb_dev_fd = -1;
722 err_out:
723 fb_preinit_done = 1;
724 fb_works = 0;
725 return 0;
728 static void vt_set_textarea(int u, int l)
730 /* how can I determine the font height?
731 * just use 16 for now
733 int urow = ((u + 15) / 16) + 1;
734 int lrow = l / 16;
736 mp_msg(MSGT_VO, MSGL_DBG2, "vt_set_textarea(%d,%d): %d,%d\n", u, l, urow, lrow);
737 if (vt_fp) {
738 fprintf(vt_fp, "\33[%d;%dr\33[%d;%dH", urow, lrow, lrow, 0);
739 fflush(vt_fp);
743 static int config(uint32_t width, uint32_t height, uint32_t d_width,
744 uint32_t d_height, uint32_t flags, char *title,
745 uint32_t format)
747 struct fb_cmap *cmap;
748 int vm = flags & VOFLAG_MODESWITCHING;
749 int zoom = flags & VOFLAG_SWSCALE;
750 int vt_fd;
752 fs = flags & VOFLAG_FULLSCREEN;
754 if (pre_init_err == -2) {
755 mp_msg(MSGT_VO, MSGL_ERR, "Internal fatal error: config() was called before preinit()\n");
756 return -1;
759 if (pre_init_err)
760 return 1;
762 if (fb_mode_name && !vm) {
763 mp_msg(MSGT_VO, MSGL_ERR, "-fbmode can only be used with -vm\n");
764 return 1;
766 if (vm && (parse_fbmode_cfg(fb_mode_cfgfile) < 0))
767 return 1;
768 if (d_width && (fs || vm)) {
769 out_width = d_width;
770 out_height = d_height;
771 } else {
772 out_width = width;
773 out_height = height;
775 in_width = width;
776 in_height = height;
777 pixel_format = format;
779 if (fb_mode_name) {
780 if (!(fb_mode = find_mode_by_name(fb_mode_name))) {
781 mp_msg(MSGT_VO, MSGL_ERR, "can't find requested video mode\n");
782 return 1;
784 fb_mode2fb_vinfo(fb_mode, &fb_vinfo);
785 } else if (vm) {
786 monitor_hfreq = str2range(monitor_hfreq_str);
787 monitor_vfreq = str2range(monitor_vfreq_str);
788 monitor_dotclock = str2range(monitor_dotclock_str);
789 if (!monitor_hfreq || !monitor_vfreq || !monitor_dotclock) {
790 mp_msg(MSGT_VO, MSGL_ERR, "you have to specify the capabilities of"
791 " the monitor.\n");
792 return 1;
794 if (!(fb_mode = find_best_mode(out_width, out_height, monitor_hfreq,
795 monitor_vfreq, monitor_dotclock))) {
796 mp_msg(MSGT_VO, MSGL_ERR, "can't find best video mode\n");
797 return 1;
799 mp_msg(MSGT_VO, MSGL_V, "using mode %dx%d @ %.1fHz\n", fb_mode->xres,
800 fb_mode->yres, vsf(fb_mode));
801 fb_mode2fb_vinfo(fb_mode, &fb_vinfo);
803 fb_bpp_we_want = fb_bpp;
804 set_bpp(&fb_vinfo, fb_bpp);
805 fb_vinfo.xres_virtual = fb_vinfo.xres;
806 fb_vinfo.yres_virtual = fb_vinfo.yres;
808 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_GRAPHICS) < 0) {
809 mp_msg(MSGT_VO, MSGL_V, "Can't set graphics mode: %s\n", strerror(errno));
810 close(fb_tty_fd);
811 fb_tty_fd = -1;
814 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo)) {
815 mp_msg(MSGT_VO, MSGL_ERR, "Can't put VSCREENINFO: %s\n", strerror(errno));
816 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0) {
817 mp_msg(MSGT_VO, MSGL_ERR, "Can't restore text mode: %s\n", strerror(errno));
819 return 1;
822 fb_pixel_size = fb_vinfo.bits_per_pixel / 8;
823 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length +
824 fb_vinfo.blue.length + fb_vinfo.transp.length;
825 if (fb_bpp_we_want != fb_bpp)
826 mp_msg(MSGT_VO, MSGL_WARN, "requested %d bpp, got %d bpp!!!\n",
827 fb_bpp_we_want, fb_bpp);
829 switch (fb_bpp) {
830 case 32:
831 draw_alpha_p = vo_draw_alpha_rgb32;
832 break;
833 case 24:
834 draw_alpha_p = vo_draw_alpha_rgb24;
835 break;
836 case 16:
837 draw_alpha_p = vo_draw_alpha_rgb16;
838 break;
839 case 15:
840 draw_alpha_p = vo_draw_alpha_rgb15;
841 break;
842 default:
843 return 1;
846 fb_xres = fb_vinfo.xres;
847 fb_yres = fb_vinfo.yres;
849 if (vm || fs) {
850 out_width = fb_xres;
851 out_height = fb_yres;
853 if (out_width < in_width || out_height < in_height) {
854 mp_msg(MSGT_VO, MSGL_ERR, "screensize is smaller than video size\n");
855 return 1;
858 first_row = (out_height - in_height) / 2;
859 last_row = (out_height + in_height) / 2;
861 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
862 mp_msg(MSGT_VO, MSGL_ERR, "Can't get FSCREENINFO: %s\n", strerror(errno));
863 return 1;
866 if (fb_finfo.type != FB_TYPE_PACKED_PIXELS) {
867 mp_msg(MSGT_VO, MSGL_ERR, "type %d not supported\n", fb_finfo.type);
868 return 1;
871 switch (fb_finfo.visual) {
872 case FB_VISUAL_TRUECOLOR:
873 break;
874 case FB_VISUAL_DIRECTCOLOR:
875 mp_msg(MSGT_VO, MSGL_V, "creating cmap for directcolor\n");
876 if (ioctl(fb_dev_fd, FBIOGETCMAP, &fb_oldcmap)) {
877 mp_msg(MSGT_VO, MSGL_ERR, "can't get cmap: %s\n",
878 strerror(errno));
879 return 1;
881 if (!(cmap = make_directcolor_cmap(&fb_vinfo)))
882 return 1;
883 if (ioctl(fb_dev_fd, FBIOPUTCMAP, cmap)) {
884 mp_msg(MSGT_VO, MSGL_ERR, "can't put cmap: %s\n",
885 strerror(errno));
886 return 1;
888 fb_cmap_changed = 1;
889 free(cmap->red);
890 free(cmap->green);
891 free(cmap->blue);
892 free(cmap);
893 break;
894 default:
895 mp_msg(MSGT_VO, MSGL_ERR, "visual: %d not yet supported\n",
896 fb_finfo.visual);
897 return 1;
900 fb_line_len = fb_finfo.line_length;
901 fb_size = fb_finfo.smem_len;
903 #ifdef CONFIG_VIDIX
904 if (vidix_name) {
905 unsigned image_width, image_height, x_offset, y_offset;
906 if (zoom || fs) {
907 aspect_save_orig(width, height);
908 aspect_save_prescale(d_width, d_height);
909 aspect_save_screenres(fb_xres, fb_yres);
910 aspect(&image_width, &image_height, fs ? A_ZOOM : A_NOZOOM);
911 } else {
912 image_width = width;
913 image_height = height;
916 if (fb_xres > image_width)
917 x_offset = (fb_xres - image_width) / 2;
918 else
919 x_offset = 0;
920 if (fb_yres > image_height)
921 y_offset = (fb_yres - image_height) / 2;
922 else
923 y_offset = 0;
925 if (vidix_init(width, height, x_offset, y_offset, image_width,
926 image_height, format, fb_bpp, fb_xres, fb_yres) != 0) {
927 mp_msg(MSGT_VO, MSGL_ERR, "Can't initialize VIDIX driver\n");
928 vidix_name = NULL;
929 vidix_term();
930 return -1;
931 } else
932 mp_msg(MSGT_VO, MSGL_V, "Using VIDIX\n");
933 vidix_start();
934 if (vidix_grkey_support()) {
935 vidix_grkey_get(&gr_key);
936 gr_key.key_op = KEYS_PUT;
937 if (!(vo_colorkey & 0xff000000)) {
938 gr_key.ckey.op = CKEY_TRUE;
939 gr_key.ckey.red = (vo_colorkey & 0x00ff0000) >> 16;
940 gr_key.ckey.green = (vo_colorkey & 0x0000ff00) >> 8;
941 gr_key.ckey.blue = vo_colorkey & 0x000000ff;
942 } else
943 gr_key.ckey.op = CKEY_FALSE;
944 vidix_grkey_set(&gr_key);
946 } else
947 #endif
949 int x_offset = 0, y_offset = 0;
950 geometry(&x_offset, &y_offset, &out_width, &out_height, fb_xres, fb_yres);
952 frame_buffer = (uint8_t *) mmap(0, fb_size, PROT_READ | PROT_WRITE,
953 MAP_SHARED, fb_dev_fd, 0);
954 if (frame_buffer == (uint8_t *) -1) {
955 mp_msg(MSGT_VO, MSGL_ERR, "Can't mmap %s: %s\n", fb_dev_name, strerror(errno));
956 return 1;
959 center = frame_buffer +
960 ( (out_width - in_width) / 2 ) * fb_pixel_size +
961 ( (out_height - in_height) / 2 ) * fb_line_len +
962 x_offset * fb_pixel_size + y_offset * fb_line_len;
964 mp_msg(MSGT_VO, MSGL_DBG2, "frame_buffer @ %p\n", frame_buffer);
965 mp_msg(MSGT_VO, MSGL_DBG2, "center @ %p\n", center);
966 mp_msg(MSGT_VO, MSGL_V, "pixel per line: %d\n", fb_line_len / fb_pixel_size);
968 if (fs || vm)
969 memset(frame_buffer, '\0', fb_line_len * fb_yres);
971 if (vt_doit && (vt_fd = open("/dev/tty", O_WRONLY)) == -1) {
972 mp_msg(MSGT_VO, MSGL_ERR, "can't open /dev/tty: %s\n", strerror(errno));
973 vt_doit = 0;
975 if (vt_doit && !(vt_fp = fdopen(vt_fd, "w"))) {
976 mp_msg(MSGT_VO, MSGL_ERR, "can't fdopen /dev/tty: %s\n", strerror(errno));
977 vt_doit = 0;
980 if (vt_doit)
981 vt_set_textarea(last_row, fb_yres);
983 return 0;
986 static int query_format(uint32_t format)
988 if (!fb_preinit(0))
989 return 0;
990 #ifdef CONFIG_VIDIX
991 if (vidix_name)
992 return vidix_query_fourcc(format);
993 #endif
994 if ((format & IMGFMT_BGR_MASK) == IMGFMT_BGR) {
995 int bpp = format & 0xff;
997 if (bpp == fb_bpp)
998 return VFCAP_ACCEPT_STRIDE | VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
1000 return 0;
1003 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
1004 unsigned char *srca, int stride)
1006 unsigned char *dst;
1008 dst = center + fb_line_len * y0 + fb_pixel_size * x0;
1010 (*draw_alpha_p)(w, h, src, srca, stride, dst, fb_line_len);
1013 static int draw_frame(uint8_t *src[])
1015 return 1;
1018 static int draw_slice(uint8_t *src[], int stride[], int w, int h, int x, int y)
1020 uint8_t *d, *s;
1022 d = center + fb_line_len * y + fb_pixel_size * x;
1024 s = src[0];
1025 while (h) {
1026 fast_memcpy(d, s, w * fb_pixel_size);
1027 d += fb_line_len;
1028 s += stride[0];
1029 h--;
1032 return 0;
1035 static void check_events(void)
1039 static void flip_page(void)
1043 static void draw_osd(void)
1045 vo_draw_text(in_width, in_height, draw_alpha);
1048 static void uninit(void)
1050 if (fb_cmap_changed) {
1051 if (ioctl(fb_dev_fd, FBIOPUTCMAP, &fb_oldcmap))
1052 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore original cmap\n");
1053 fb_cmap_changed = 0;
1055 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo))
1056 mp_msg(MSGT_VO, MSGL_WARN, "ioctl FBIOGET_VSCREENINFO: %s\n", strerror(errno));
1057 fb_orig_vinfo.xoffset = fb_vinfo.xoffset;
1058 fb_orig_vinfo.yoffset = fb_vinfo.yoffset;
1059 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_orig_vinfo))
1060 mp_msg(MSGT_VO, MSGL_WARN, "Can't reset original fb_var_screeninfo: %s\n", strerror(errno));
1061 if (fb_tty_fd >= 0) {
1062 if (ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0)
1063 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore text mode: %s\n", strerror(errno));
1065 if (vt_doit)
1066 vt_set_textarea(0, fb_orig_vinfo.yres);
1067 close(fb_tty_fd);
1068 close(fb_dev_fd);
1069 if (frame_buffer)
1070 munmap(frame_buffer, fb_size);
1071 frame_buffer = NULL;
1072 #ifdef CONFIG_VIDIX
1073 if (vidix_name)
1074 vidix_term();
1075 #endif
1076 fb_preinit(1);
1079 static int preinit(const char *vo_subdevice)
1081 pre_init_err = 0;
1083 if (vo_subdevice) {
1084 #ifdef CONFIG_VIDIX
1085 if (memcmp(vo_subdevice, "vidix", 5) == 0)
1086 vidix_name = &vo_subdevice[5];
1087 if (vidix_name)
1088 pre_init_err = vidix_preinit(vidix_name, &video_out_fbdev);
1089 else
1090 #endif
1092 if (fb_dev_name)
1093 free(fb_dev_name);
1094 fb_dev_name = strdup(vo_subdevice);
1097 if (!pre_init_err)
1098 return pre_init_err = (fb_preinit(0) ? 0 : -1);
1099 return -1;
1102 static uint32_t get_image(mp_image_t *mpi)
1104 if (!IMGFMT_IS_BGR(mpi->imgfmt) ||
1105 (IMGFMT_BGR_DEPTH(mpi->imgfmt) != fb_bpp) ||
1106 ((mpi->type != MP_IMGTYPE_STATIC) && (mpi->type != MP_IMGTYPE_TEMP)) ||
1107 (mpi->flags & MP_IMGFLAG_PLANAR) ||
1108 (mpi->flags & MP_IMGFLAG_YUV) ||
1109 (mpi->width != in_width) ||
1110 (mpi->height != in_height)
1112 return VO_FALSE;
1114 mpi->planes[0] = center;
1115 mpi->stride[0] = fb_line_len;
1116 mpi->flags |= MP_IMGFLAG_DIRECT;
1117 return VO_TRUE;
1120 static int control(uint32_t request, void *data, ...)
1122 switch (request) {
1123 case VOCTRL_GET_IMAGE:
1124 return get_image(data);
1125 case VOCTRL_QUERY_FORMAT:
1126 return query_format(*((uint32_t*)data));
1129 #ifdef CONFIG_VIDIX
1130 if (vidix_name) {
1131 switch (request) {
1132 case VOCTRL_SET_EQUALIZER:
1134 va_list ap;
1135 int value;
1137 va_start(ap, data);
1138 value = va_arg(ap, int);
1139 va_end(ap);
1141 return vidix_control(request, data, value);
1143 case VOCTRL_GET_EQUALIZER:
1145 va_list ap;
1146 int *value;
1148 va_start(ap, data);
1149 value = va_arg(ap, int*);
1150 va_end(ap);
1152 return vidix_control(request, data, value);
1156 #endif
1158 return VO_NOTIMPL;