Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / libvo / vo_fbdev.c
blobcfa7f522bc5d225ca0c20e7903539b27e9bd59f4
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"
49 #include "libavutil/common.h"
51 static const vo_info_t info = {
52 "Framebuffer Device",
53 "fbdev",
54 "Szabolcs Berecz <szabi@inf.elte.hu>",
58 LIBVO_EXTERN(fbdev)
60 #ifdef CONFIG_VIDIX
61 /* Name of VIDIX driver */
62 static const char *vidix_name = NULL;
63 static vidix_grkey_t gr_key;
64 #endif
65 static signed int pre_init_err = -2;
66 /******************************
67 * fb.modes support *
68 ******************************/
70 static range_t *monitor_hfreq = NULL;
71 static range_t *monitor_vfreq = NULL;
72 static range_t *monitor_dotclock = NULL;
74 typedef struct {
75 char *name;
76 uint32_t xres, yres, vxres, vyres, depth;
77 uint32_t pixclock, left, right, upper, lower, hslen, vslen;
78 uint32_t sync;
79 uint32_t vmode;
80 } fb_mode_t;
82 #define MAX_NR_TOKEN 16
84 #define MAX_LINE_LEN 1000
86 #define RET_EOF -1
87 #define RET_EOL -2
89 static int validate_mode(fb_mode_t *m)
91 if (!m->xres) {
92 mp_msg(MSGT_VO, MSGL_V, "needs geometry ");
93 return 0;
95 if (!m->pixclock) {
96 mp_msg(MSGT_VO, MSGL_V, "needs timings ");
97 return 0;
99 return 1;
102 static FILE *fp;
103 static int line_num = 0;
104 static char *line;
105 static char *token[MAX_NR_TOKEN];
107 static int get_token(int num)
109 static int read_nextline = 1;
110 static int line_pos;
111 int i;
112 char c;
114 if (num >= MAX_NR_TOKEN) {
115 mp_msg(MSGT_VO, MSGL_V, "get_token(): max >= MAX_NR_TOKEN!\n");
116 goto out_eof;
119 if (read_nextline) {
120 if (!fgets(line, MAX_LINE_LEN, fp))
121 goto out_eof;
122 line_pos = 0;
123 ++line_num;
124 read_nextline = 0;
126 for (i = 0; i < num; i++) {
127 while (isspace(line[line_pos]))
128 ++line_pos;
129 if (line[line_pos] == '\0' || line[line_pos] == '#') {
130 read_nextline = 1;
131 goto out_eol;
133 token[i] = line + line_pos;
134 c = line[line_pos];
135 if (c == '"' || c == '\'') {
136 token[i]++;
137 while (line[++line_pos] != c && line[line_pos])
138 /* NOTHING */;
139 if (!line[line_pos])
140 goto out_eol;
141 line[line_pos] = ' ';
142 } else {
143 for (/* NOTHING */; !isspace(line[line_pos]) &&
144 line[line_pos]; line_pos++)
145 /* NOTHING */;
147 if (!line[line_pos]) {
148 read_nextline = 1;
149 if (i == num - 1)
150 goto out_ok;
151 goto out_eol;
153 line[line_pos++] = '\0';
155 out_ok:
156 return i;
157 out_eof:
158 return RET_EOF;
159 out_eol:
160 return RET_EOL;
163 static fb_mode_t *fb_modes = NULL;
164 static int nr_modes = 0;
166 static int parse_fbmode_cfg(char *cfgfile)
168 #define CHECK_IN_MODE_DEF\
169 if (!in_mode_def) {\
170 mp_msg(MSGT_VO, MSGL_V, "'needs 'mode' first");\
171 goto err_out_print_linenum;\
173 fb_mode_t *mode = NULL;
174 char *endptr; // strtoul()...
175 int in_mode_def = 0;
176 int tmp, i;
178 /* If called more than once, reuse parsed data */
179 if (nr_modes)
180 return nr_modes;
182 mp_msg(MSGT_VO, MSGL_V, "Reading %s: ", cfgfile);
184 if ((fp = fopen(cfgfile, "r")) == NULL) {
185 mp_msg(MSGT_VO, MSGL_V, "can't open '%s': %s\n", cfgfile, strerror(errno));
186 return -1;
189 if ((line = malloc(MAX_LINE_LEN + 1)) == NULL) {
190 mp_msg(MSGT_VO, MSGL_V, "can't get memory for 'line': %s\n", strerror(errno));
191 return -2;
195 * check if the cfgfile starts with 'mode'
197 while ((tmp = get_token(1)) == RET_EOL)
198 /* NOTHING */;
199 if (tmp == RET_EOF)
200 goto out;
201 if (!strcmp(token[0], "mode"))
202 goto loop_enter;
203 goto err_out_parse_error;
205 while ((tmp = get_token(1)) != RET_EOF) {
206 if (tmp == RET_EOL)
207 continue;
208 if (!strcmp(token[0], "mode")) {
209 if (in_mode_def) {
210 mp_msg(MSGT_VO, MSGL_V, "'endmode' required");
211 goto err_out_print_linenum;
213 if (!validate_mode(mode))
214 goto err_out_not_valid;
215 loop_enter:
216 if (!(fb_modes =
217 realloc(fb_modes, sizeof(fb_mode_t) * (nr_modes + 1)))) {
218 mp_msg(MSGT_VO, MSGL_V, "can't realloc 'fb_modes' (nr_modes = %d):"
219 " %s\n", nr_modes, strerror(errno));
220 goto err_out;
222 mode = fb_modes + nr_modes;
223 ++nr_modes;
224 memset(mode, 0, sizeof(fb_mode_t));
226 if (get_token(1) < 0)
227 goto err_out_parse_error;
228 for (i = 0; i < nr_modes - 1; i++) {
229 if (!strcmp(token[0], fb_modes[i].name)) {
230 mp_msg(MSGT_VO, MSGL_V, "mode name '%s' isn't unique", token[0]);
231 goto err_out_print_linenum;
234 if (!(mode->name = strdup(token[0]))) {
235 mp_msg(MSGT_VO, MSGL_V, "can't strdup -> 'name': %s\n", strerror(errno));
236 goto err_out;
238 in_mode_def = 1;
239 } else if (!strcmp(token[0], "geometry")) {
240 CHECK_IN_MODE_DEF;
241 if (get_token(5) < 0)
242 goto err_out_parse_error;
243 mode->xres = strtoul(token[0], &endptr, 0);
244 if (*endptr)
245 goto err_out_parse_error;
246 mode->yres = strtoul(token[1], &endptr, 0);
247 if (*endptr)
248 goto err_out_parse_error;
249 mode->vxres = strtoul(token[2], &endptr, 0);
250 if (*endptr)
251 goto err_out_parse_error;
252 mode->vyres = strtoul(token[3], &endptr, 0);
253 if (*endptr)
254 goto err_out_parse_error;
255 mode->depth = strtoul(token[4], &endptr, 0);
256 if (*endptr)
257 goto err_out_parse_error;
258 } else if (!strcmp(token[0], "timings")) {
259 CHECK_IN_MODE_DEF;
260 if (get_token(7) < 0)
261 goto err_out_parse_error;
262 mode->pixclock = strtoul(token[0], &endptr, 0);
263 if (*endptr)
264 goto err_out_parse_error;
265 mode->left = strtoul(token[1], &endptr, 0);
266 if (*endptr)
267 goto err_out_parse_error;
268 mode->right = strtoul(token[2], &endptr, 0);
269 if (*endptr)
270 goto err_out_parse_error;
271 mode->upper = strtoul(token[3], &endptr, 0);
272 if (*endptr)
273 goto err_out_parse_error;
274 mode->lower = strtoul(token[4], &endptr, 0);
275 if (*endptr)
276 goto err_out_parse_error;
277 mode->hslen = strtoul(token[5], &endptr, 0);
278 if (*endptr)
279 goto err_out_parse_error;
280 mode->vslen = strtoul(token[6], &endptr, 0);
281 if (*endptr)
282 goto err_out_parse_error;
283 } else if (!strcmp(token[0], "endmode")) {
284 CHECK_IN_MODE_DEF;
285 in_mode_def = 0;
286 } else if (!strcmp(token[0], "accel")) {
287 CHECK_IN_MODE_DEF;
288 if (get_token(1) < 0)
289 goto err_out_parse_error;
291 * it's only used for text acceleration
292 * so we just ignore it.
294 } else if (!strcmp(token[0], "hsync")) {
295 CHECK_IN_MODE_DEF;
296 if (get_token(1) < 0)
297 goto err_out_parse_error;
298 if (!strcmp(token[0], "low"))
299 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
300 else if (!strcmp(token[0], "high"))
301 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
302 else
303 goto err_out_parse_error;
304 } else if (!strcmp(token[0], "vsync")) {
305 CHECK_IN_MODE_DEF;
306 if (get_token(1) < 0)
307 goto err_out_parse_error;
308 if (!strcmp(token[0], "low"))
309 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
310 else if (!strcmp(token[0], "high"))
311 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
312 else
313 goto err_out_parse_error;
314 } else if (!strcmp(token[0], "csync")) {
315 CHECK_IN_MODE_DEF;
316 if (get_token(1) < 0)
317 goto err_out_parse_error;
318 if (!strcmp(token[0], "low"))
319 mode->sync &= ~FB_SYNC_COMP_HIGH_ACT;
320 else if (!strcmp(token[0], "high"))
321 mode->sync |= FB_SYNC_COMP_HIGH_ACT;
322 else
323 goto err_out_parse_error;
324 } else if (!strcmp(token[0], "extsync")) {
325 CHECK_IN_MODE_DEF;
326 if (get_token(1) < 0)
327 goto err_out_parse_error;
328 if (!strcmp(token[0], "false"))
329 mode->sync &= ~FB_SYNC_EXT;
330 else if (!strcmp(token[0], "true"))
331 mode->sync |= FB_SYNC_EXT;
332 else
333 goto err_out_parse_error;
334 } else if (!strcmp(token[0], "laced")) {
335 CHECK_IN_MODE_DEF;
336 if (get_token(1) < 0)
337 goto err_out_parse_error;
338 if (!strcmp(token[0], "false"))
339 mode->vmode = FB_VMODE_NONINTERLACED;
340 else if (!strcmp(token[0], "true"))
341 mode->vmode = FB_VMODE_INTERLACED;
342 else
343 goto err_out_parse_error;
344 } else if (!strcmp(token[0], "double")) {
345 CHECK_IN_MODE_DEF;
346 if (get_token(1) < 0)
347 goto err_out_parse_error;
348 if (!strcmp(token[0], "false"))
350 else if (!strcmp(token[0], "true"))
351 mode->vmode = FB_VMODE_DOUBLE;
352 else
353 goto err_out_parse_error;
354 } else
355 goto err_out_parse_error;
357 if (!validate_mode(mode))
358 goto err_out_not_valid;
359 out:
360 mp_msg(MSGT_VO, MSGL_V, "%d modes\n", nr_modes);
361 free(line);
362 fclose(fp);
363 return nr_modes;
364 err_out_parse_error:
365 mp_msg(MSGT_VO, MSGL_V, "parse error");
366 err_out_print_linenum:
367 mp_msg(MSGT_VO, MSGL_V, " at line %d\n", line_num);
368 err_out:
369 if (fb_modes) {
370 free(fb_modes);
371 fb_modes = NULL;
373 nr_modes = 0;
374 free(line);
375 free(fp);
376 return -2;
377 err_out_not_valid:
378 mp_msg(MSGT_VO, MSGL_V, "previous mode is not correct");
379 goto err_out_print_linenum;
382 static fb_mode_t *find_mode_by_name(char *name)
384 int i;
386 for (i = 0; i < nr_modes; i++)
387 if (!strcmp(name, fb_modes[i].name))
388 return fb_modes + i;
389 return NULL;
392 static float dcf(fb_mode_t *m) //driving clock frequency
394 return 1e12f / m->pixclock;
397 static float hsf(fb_mode_t *m) //horizontal scan frequency
399 int htotal = m->left + m->xres + m->right + m->hslen;
400 return dcf(m) / htotal;
403 static float vsf(fb_mode_t *m) //vertical scan frequency
405 int vtotal = m->upper + m->yres + m->lower + m->vslen;
406 return hsf(m) / vtotal;
410 static int mode_works(fb_mode_t *m, range_t *hfreq, range_t *vfreq,
411 range_t *dotclock)
413 float h = hsf(m);
414 float v = vsf(m);
415 float d = dcf(m);
416 int ret = 1;
418 mp_msg(MSGT_VO, MSGL_DBG2, "mode %dx%d:", m->xres, m->yres);
419 if (!in_range(hfreq, h)) {
420 ret = 0;
421 mp_msg(MSGT_VO, MSGL_DBG2, " hsync out of range.");
423 if (!in_range(vfreq, v)) {
424 ret = 0;
425 mp_msg(MSGT_VO, MSGL_DBG2, " vsync out of range.");
427 if (!in_range(dotclock, d)) {
428 ret = 0;
429 mp_msg(MSGT_VO, MSGL_DBG2, " dotclock out of range.");
431 if (ret)
432 mp_msg(MSGT_VO, MSGL_DBG2, " hsync, vsync, dotclock ok.\n");
433 else
434 mp_msg(MSGT_VO, MSGL_DBG2, "\n");
436 return ret;
439 static fb_mode_t *find_best_mode(int xres, int yres, range_t *hfreq,
440 range_t *vfreq, range_t *dotclock)
442 int i;
443 fb_mode_t *best = fb_modes;
444 fb_mode_t *curr;
446 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for first working mode\n");
448 for (i = 0; i < nr_modes; i++, best++)
449 if (mode_works(best, hfreq, vfreq, dotclock))
450 break;
452 if (i == nr_modes)
453 return NULL;
454 if (i == nr_modes - 1)
455 return best;
457 mp_msg(MSGT_VO, MSGL_DBG2, "First working mode: %dx%d\n", best->xres, best->yres);
458 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for better modes\n");
460 for (curr = best + 1; i < nr_modes - 1; i++, curr++) {
461 if (!mode_works(curr, hfreq, vfreq, dotclock))
462 continue;
464 if (best->xres < xres || best->yres < yres) {
465 if (curr->xres > best->xres || curr->yres > best->yres) {
466 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too small.\n",
467 best->xres, best->yres);
468 best = curr;
469 } else
470 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
471 } else if (curr->xres == best->xres && curr->yres == best->yres &&
472 vsf(curr) > vsf(best)) {
473 mp_msg(MSGT_VO, MSGL_DBG2, "faster screen refresh.\n");
474 best = curr;
475 } else if ((curr->xres <= best->xres && curr->yres <= best->yres) &&
476 (curr->xres >= xres && curr->yres >= yres)) {
477 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too large.\n",
478 best->xres, best->yres);
479 best = curr;
480 } else {
481 if (curr->xres < xres || curr->yres < yres)
482 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
483 else if (curr->xres > best->xres || curr->yres > best->yres)
484 mp_msg(MSGT_VO, MSGL_DBG2, "too large.\n");
485 else
486 mp_msg(MSGT_VO, MSGL_DBG2, "it's worse, don't know why.\n");
490 return best;
493 static void set_bpp(struct fb_var_screeninfo *p, int bpp)
495 p->bits_per_pixel = FFALIGN(bpp, 2);
496 p->red.msb_right = p->green.msb_right = p->blue.msb_right = p->transp.msb_right = 0;
497 p->transp.offset = p->transp.length = 0;
498 p->blue.offset = 0;
499 switch (bpp) {
500 case 32:
501 p->transp.offset = 24;
502 p->transp.length = 8;
503 case 24:
504 p->red.offset = 16;
505 p->red.length = 8;
506 p->green.offset = 8;
507 p->green.length = 8;
508 p->blue.length = 8;
509 break;
510 case 16:
511 p->red.offset = 11;
512 p->green.length = 6;
513 p->red.length = 5;
514 p->green.offset = 5;
515 p->blue.length = 5;
516 break;
517 case 15:
518 p->red.offset = 10;
519 p->green.length = 5;
520 p->red.length = 5;
521 p->green.offset = 5;
522 p->blue.length = 5;
523 break;
524 case 12:
525 p->red.offset = 8;
526 p->green.length = 4;
527 p->red.length = 4;
528 p->green.offset = 4;
529 p->blue.length = 4;
530 break;
534 static void fb_mode2fb_vinfo(fb_mode_t *m, struct fb_var_screeninfo *v)
536 v->xres = m->xres;
537 v->yres = m->yres;
538 v->xres_virtual = m->vxres;
539 v->yres_virtual = m->vyres;
540 set_bpp(v, m->depth);
541 v->pixclock = m->pixclock;
542 v->left_margin = m->left;
543 v->right_margin = m->right;
544 v->upper_margin = m->upper;
545 v->lower_margin = m->lower;
546 v->hsync_len = m->hslen;
547 v->vsync_len = m->vslen;
548 v->sync = m->sync;
549 v->vmode = m->vmode;
553 /******************************
554 * vo_fbdev *
555 ******************************/
557 /* command line/config file options */
558 static char *fb_dev_name = NULL;
559 char *fb_mode_cfgfile = NULL;
560 char *fb_mode_name = NULL;
562 static fb_mode_t *fb_mode = NULL;
564 /* vo_fbdev related variables */
565 static int fb_dev_fd;
566 static int fb_tty_fd = -1;
567 static size_t fb_size;
568 static uint8_t *frame_buffer;
569 static uint8_t *center;
570 static struct fb_fix_screeninfo fb_finfo;
571 static struct fb_var_screeninfo fb_orig_vinfo;
572 static struct fb_var_screeninfo fb_vinfo;
573 static unsigned short fb_ored[256], fb_ogreen[256], fb_oblue[256];
574 static struct fb_cmap fb_oldcmap = { 0, 256, fb_ored, fb_ogreen, fb_oblue };
575 static int fb_cmap_changed = 0;
576 static int fb_pixel_size; // 32: 4 24: 3 16: 2 15: 2
577 static int fb_bpp; // 32: 32 24: 24 16: 16 15: 15
578 static int fb_bpp_we_want; // 32: 32 24: 24 16: 16 15: 15
579 static int fb_line_len;
580 static int fb_xres;
581 static int fb_yres;
582 static int fb_page;
583 static void (*draw_alpha_p)(int w, int h, unsigned char *src,
584 unsigned char *srca, int stride,
585 unsigned char *dst, int dstride);
587 static int in_width;
588 static int in_height;
589 static int out_width;
590 static int out_height;
591 static int first_row;
592 static int last_row;
593 static uint32_t pixel_format;
594 static int fs;
597 * Note: this function is completely cut'n'pasted from
598 * Chris Lawrence's code.
599 * (modified a bit to fit in my code...)
601 static struct fb_cmap *make_directcolor_cmap(struct fb_var_screeninfo *var)
603 /* Hopefully any DIRECTCOLOR device will have a big enough palette
604 * to handle mapping the full color depth.
605 * e.g. 8 bpp -> 256 entry palette
607 * We could handle some sort of gamma here
609 int i, cols, rcols, gcols, bcols;
610 uint16_t *red, *green, *blue;
611 struct fb_cmap *cmap;
613 rcols = 1 << var->red.length;
614 gcols = 1 << var->green.length;
615 bcols = 1 << var->blue.length;
617 /* Make our palette the length of the deepest color */
618 cols = FFMAX3(rcols, gcols, bcols);
620 red = malloc(cols * sizeof(red[0]));
621 if (!red) {
622 mp_msg(MSGT_VO, MSGL_V, "Can't allocate red palette with %d entries.\n", cols);
623 return NULL;
625 for (i = 0; i < rcols; i++)
626 red[i] = (65535 / (rcols - 1)) * i;
628 green = malloc(cols * sizeof(green[0]));
629 if (!green) {
630 mp_msg(MSGT_VO, MSGL_V, "Can't allocate green palette with %d entries.\n", cols);
631 free(red);
632 return NULL;
634 for (i = 0; i < gcols; i++)
635 green[i] = (65535 / (gcols - 1)) * i;
637 blue = malloc(cols * sizeof(blue[0]));
638 if (!blue) {
639 mp_msg(MSGT_VO, MSGL_V, "Can't allocate blue palette with %d entries.\n", cols);
640 free(red);
641 free(green);
642 return NULL;
644 for (i = 0; i < bcols; i++)
645 blue[i] = (65535 / (bcols - 1)) * i;
647 cmap = malloc(sizeof(struct fb_cmap));
648 if (!cmap) {
649 mp_msg(MSGT_VO, MSGL_V, "Can't allocate color map\n");
650 free(red);
651 free(green);
652 free(blue);
653 return NULL;
655 cmap->start = 0;
656 cmap->transp = 0;
657 cmap->len = cols;
658 cmap->red = red;
659 cmap->blue = blue;
660 cmap->green = green;
661 cmap->transp = NULL;
663 return cmap;
667 static int fb_preinit(int reset)
669 static int fb_preinit_done = 0;
670 static int fb_works = 0;
672 if (reset) {
673 fb_preinit_done = 0;
674 return 0;
677 if (fb_preinit_done)
678 return fb_works;
680 fb_dev_fd = fb_tty_fd = -1;
682 if (!fb_dev_name && !(fb_dev_name = getenv("FRAMEBUFFER")))
683 fb_dev_name = strdup("/dev/fb0");
684 mp_msg(MSGT_VO, MSGL_V, "using %s\n", fb_dev_name);
686 if ((fb_dev_fd = open(fb_dev_name, O_RDWR)) == -1) {
687 mp_msg(MSGT_VO, MSGL_ERR, "Can't open %s: %s\n", fb_dev_name, strerror(errno));
688 goto err_out;
690 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
691 mp_msg(MSGT_VO, MSGL_ERR, "Can't get VSCREENINFO: %s\n", strerror(errno));
692 goto err_out;
694 fb_orig_vinfo = fb_vinfo;
696 if ((fb_tty_fd = open("/dev/tty", O_RDWR)) < 0) {
697 mp_msg(MSGT_VO, MSGL_ERR, "notice: Can't open /dev/tty: %s\n", strerror(errno));
700 fb_bpp = fb_vinfo.bits_per_pixel;
701 if (fb_bpp == 16)
702 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length + fb_vinfo.blue.length;
704 if (fb_bpp == 8 && !vo_dbpp) {
705 mp_msg(MSGT_VO, MSGL_ERR, "8 bpp output is not supported.\n");
706 goto err_out;
709 if (vo_dbpp) {
710 if (vo_dbpp != 12 && vo_dbpp != 15 && vo_dbpp != 16
711 && vo_dbpp != 24 && vo_dbpp != 32) {
712 mp_msg(MSGT_VO, MSGL_ERR, "can't switch to %d bpp\n", vo_dbpp);
713 goto err_out;
715 fb_bpp = vo_dbpp;
718 if (!fb_mode_cfgfile)
719 fb_mode_cfgfile = strdup("/etc/fb.modes");
721 fb_preinit_done = 1;
722 fb_works = 1;
723 return 1;
724 err_out:
725 if (fb_tty_fd != -1)
726 close(fb_tty_fd);
727 fb_tty_fd = -1;
728 if (fb_dev_fd != -1)
729 close(fb_dev_fd);
730 fb_dev_fd = -1;
731 fb_preinit_done = 1;
732 fb_works = 0;
733 return 0;
736 static void vt_set_textarea(int u, int l)
738 /* how can I determine the font height?
739 * just use 16 for now
741 int urow = ((u + 15) / 16) + 1;
742 int lrow = l / 16;
744 mp_msg(MSGT_VO, MSGL_DBG2, "vt_set_textarea(%d,%d): %d,%d\n", u, l, urow, lrow);
745 if (fb_tty_fd >= 0) {
746 char modestring[100];
747 snprintf(modestring, sizeof(modestring), "\33[%d;%dr\33[%d;%dH", urow, lrow, lrow, 0);
748 write(fb_tty_fd, modestring, strlen(modestring));
749 fsync(fb_tty_fd);
753 static int config(uint32_t width, uint32_t height, uint32_t d_width,
754 uint32_t d_height, uint32_t flags, char *title,
755 uint32_t format)
757 struct fb_cmap *cmap;
758 int vm = flags & VOFLAG_MODESWITCHING;
759 int zoom = flags & VOFLAG_SWSCALE;
761 fs = flags & VOFLAG_FULLSCREEN;
763 if (pre_init_err == -2) {
764 mp_msg(MSGT_VO, MSGL_ERR, "Internal fatal error: config() was called before preinit()\n");
765 return -1;
768 if (pre_init_err)
769 return 1;
771 if (fb_mode_name && !vm) {
772 mp_msg(MSGT_VO, MSGL_ERR, "-fbmode can only be used with -vm\n");
773 return 1;
775 if (vm && parse_fbmode_cfg(fb_mode_cfgfile) < 0)
776 return 1;
777 if (d_width && (fs || vm)) {
778 out_width = d_width;
779 out_height = d_height;
780 } else {
781 out_width = width;
782 out_height = height;
784 in_width = width;
785 in_height = height;
786 pixel_format = format;
788 if (fb_mode_name) {
789 if (!(fb_mode = find_mode_by_name(fb_mode_name))) {
790 mp_msg(MSGT_VO, MSGL_ERR, "can't find requested video mode\n");
791 return 1;
793 fb_mode2fb_vinfo(fb_mode, &fb_vinfo);
794 } else if (vm) {
795 monitor_hfreq = str2range(monitor_hfreq_str);
796 monitor_vfreq = str2range(monitor_vfreq_str);
797 monitor_dotclock = str2range(monitor_dotclock_str);
798 if (!monitor_hfreq || !monitor_vfreq || !monitor_dotclock) {
799 mp_msg(MSGT_VO, MSGL_ERR, "you have to specify the capabilities of"
800 " the monitor.\n");
801 return 1;
803 if (!(fb_mode = find_best_mode(out_width, out_height, monitor_hfreq,
804 monitor_vfreq, monitor_dotclock))) {
805 mp_msg(MSGT_VO, MSGL_ERR, "can't find best video mode\n");
806 return 1;
808 mp_msg(MSGT_VO, MSGL_V, "using mode %dx%d @ %.1fHz\n", fb_mode->xres,
809 fb_mode->yres, vsf(fb_mode));
810 fb_mode2fb_vinfo(fb_mode, &fb_vinfo);
812 fb_bpp_we_want = fb_bpp;
813 set_bpp(&fb_vinfo, fb_bpp);
814 fb_vinfo.xres_virtual = fb_vinfo.xres;
815 fb_vinfo.yres_virtual = fb_vinfo.yres;
816 fb_page = 0;
817 if (vo_doublebuffering) {
818 fb_vinfo.yres_virtual <<= 1;
819 fb_vinfo.yoffset = 0;
820 fb_page = 1; // start writing into the page we don't display
823 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_GRAPHICS) < 0) {
824 mp_msg(MSGT_VO, MSGL_V, "Can't set graphics mode: %s\n", strerror(errno));
825 close(fb_tty_fd);
826 fb_tty_fd = -1;
829 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo))
830 // Intel drivers fail if we request a transparency channel
831 fb_vinfo.transp.length = fb_vinfo.transp.offset = 0;
832 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo)) {
833 mp_msg(MSGT_VO, MSGL_ERR, "Can't put VSCREENINFO: %s\n", strerror(errno));
834 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0) {
835 mp_msg(MSGT_VO, MSGL_ERR, "Can't restore text mode: %s\n", strerror(errno));
837 return 1;
840 fb_pixel_size = fb_vinfo.bits_per_pixel / 8;
841 fb_bpp = fb_vinfo.bits_per_pixel;
842 if (fb_bpp == 16)
843 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length + fb_vinfo.blue.length;
844 if (fb_bpp_we_want != fb_bpp)
845 mp_msg(MSGT_VO, MSGL_WARN, "requested %d bpp, got %d bpp!!!\n",
846 fb_bpp_we_want, fb_bpp);
848 switch (fb_bpp) {
849 case 32:
850 draw_alpha_p = vo_draw_alpha_rgb32;
851 break;
852 case 24:
853 draw_alpha_p = vo_draw_alpha_rgb24;
854 break;
855 case 16:
856 draw_alpha_p = vo_draw_alpha_rgb16;
857 break;
858 case 15:
859 draw_alpha_p = vo_draw_alpha_rgb15;
860 break;
861 case 12:
862 draw_alpha_p = vo_draw_alpha_rgb12;
863 break;
864 default:
865 return 1;
868 fb_xres = fb_vinfo.xres;
869 fb_yres = fb_vinfo.yres;
871 if (vm || fs) {
872 out_width = fb_xres;
873 out_height = fb_yres;
875 if (out_width < in_width || out_height < in_height) {
876 mp_msg(MSGT_VO, MSGL_ERR, "screensize is smaller than video size\n");
877 return 1;
880 first_row = (out_height - in_height) / 2;
881 last_row = (out_height + in_height) / 2;
883 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
884 mp_msg(MSGT_VO, MSGL_ERR, "Can't get FSCREENINFO: %s\n", strerror(errno));
885 return 1;
888 if (fb_finfo.type != FB_TYPE_PACKED_PIXELS) {
889 mp_msg(MSGT_VO, MSGL_ERR, "type %d not supported\n", fb_finfo.type);
890 return 1;
893 switch (fb_finfo.visual) {
894 case FB_VISUAL_TRUECOLOR:
895 break;
896 case FB_VISUAL_DIRECTCOLOR:
897 mp_msg(MSGT_VO, MSGL_V, "creating cmap for directcolor\n");
898 if (ioctl(fb_dev_fd, FBIOGETCMAP, &fb_oldcmap)) {
899 mp_msg(MSGT_VO, MSGL_ERR, "can't get cmap: %s\n",
900 strerror(errno));
901 return 1;
903 if (!(cmap = make_directcolor_cmap(&fb_vinfo)))
904 return 1;
905 if (ioctl(fb_dev_fd, FBIOPUTCMAP, cmap)) {
906 mp_msg(MSGT_VO, MSGL_ERR, "can't put cmap: %s\n",
907 strerror(errno));
908 return 1;
910 fb_cmap_changed = 1;
911 free(cmap->red);
912 free(cmap->green);
913 free(cmap->blue);
914 free(cmap);
915 break;
916 default:
917 mp_msg(MSGT_VO, MSGL_ERR, "visual: %d not yet supported\n",
918 fb_finfo.visual);
919 return 1;
922 fb_line_len = fb_finfo.line_length;
923 fb_size = fb_finfo.smem_len;
924 if (vo_doublebuffering && fb_size < 2 * fb_yres * fb_line_len)
926 mp_msg(MSGT_VO, MSGL_WARN, "framebuffer too small for double-buffering, disabling\n");
927 vo_doublebuffering = 0;
928 fb_page = 0;
931 #ifdef CONFIG_VIDIX
932 if (vidix_name) {
933 unsigned image_width, image_height, x_offset, y_offset;
934 if (zoom || fs) {
935 aspect_save_orig(width, height);
936 aspect_save_prescale(d_width, d_height);
937 aspect_save_screenres(fb_xres, fb_yres);
938 aspect(&image_width, &image_height, fs ? A_ZOOM : A_NOZOOM);
939 } else {
940 image_width = width;
941 image_height = height;
944 if (fb_xres > image_width)
945 x_offset = (fb_xres - image_width) / 2;
946 else
947 x_offset = 0;
948 if (fb_yres > image_height)
949 y_offset = (fb_yres - image_height) / 2;
950 else
951 y_offset = 0;
953 if (vidix_init(width, height, x_offset, y_offset, image_width,
954 image_height, format, fb_bpp, fb_xres, fb_yres) != 0) {
955 mp_msg(MSGT_VO, MSGL_ERR, "Can't initialize VIDIX driver\n");
956 vidix_name = NULL;
957 vidix_term();
958 return -1;
959 } else
960 mp_msg(MSGT_VO, MSGL_V, "Using VIDIX\n");
961 vidix_start();
962 if (vidix_grkey_support()) {
963 vidix_grkey_get(&gr_key);
964 gr_key.key_op = KEYS_PUT;
965 if (!(vo_colorkey & 0xff000000)) {
966 gr_key.ckey.op = CKEY_TRUE;
967 gr_key.ckey.red = (vo_colorkey & 0x00ff0000) >> 16;
968 gr_key.ckey.green = (vo_colorkey & 0x0000ff00) >> 8;
969 gr_key.ckey.blue = vo_colorkey & 0x000000ff;
970 } else
971 gr_key.ckey.op = CKEY_FALSE;
972 vidix_grkey_set(&gr_key);
974 } else
975 #endif
977 int x_offset = 0, y_offset = 0;
978 geometry(&x_offset, &y_offset, &out_width, &out_height, fb_xres, fb_yres);
980 frame_buffer = mmap(0, fb_size, PROT_READ | PROT_WRITE,
981 MAP_SHARED, fb_dev_fd, 0);
982 if (frame_buffer == (uint8_t *) -1) {
983 mp_msg(MSGT_VO, MSGL_ERR, "Can't mmap %s: %s\n", fb_dev_name, strerror(errno));
984 return 1;
987 center = frame_buffer +
988 ( (out_width - in_width) / 2 ) * fb_pixel_size +
989 ( (out_height - in_height) / 2 ) * fb_line_len +
990 x_offset * fb_pixel_size + y_offset * fb_line_len +
991 fb_page * fb_yres * fb_line_len;
993 mp_msg(MSGT_VO, MSGL_DBG2, "frame_buffer @ %p\n", frame_buffer);
994 mp_msg(MSGT_VO, MSGL_DBG2, "center @ %p\n", center);
995 mp_msg(MSGT_VO, MSGL_V, "pixel per line: %d\n", fb_line_len / fb_pixel_size);
997 if (fs || vm) {
998 int clear_size = fb_line_len * fb_yres;
999 if (vo_doublebuffering)
1000 clear_size <<= 1;
1001 memset(frame_buffer, 0, clear_size);
1005 vt_set_textarea(last_row, fb_yres);
1007 return 0;
1010 static int query_format(uint32_t format)
1012 if (!fb_preinit(0))
1013 return 0;
1014 #ifdef CONFIG_VIDIX
1015 if (vidix_name)
1016 return vidix_query_fourcc(format);
1017 #endif
1018 if ((format & IMGFMT_BGR_MASK) == IMGFMT_BGR) {
1019 int bpp = format & 0xff;
1021 if (bpp == fb_bpp)
1022 return VFCAP_ACCEPT_STRIDE | VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
1024 return 0;
1027 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
1028 unsigned char *srca, int stride)
1030 unsigned char *dst;
1032 dst = center + fb_line_len * y0 + fb_pixel_size * x0;
1034 (*draw_alpha_p)(w, h, src, srca, stride, dst, fb_line_len);
1037 static int draw_frame(uint8_t *src[])
1039 return 1;
1042 static int draw_slice(uint8_t *src[], int stride[], int w, int h, int x, int y)
1044 uint8_t *d;
1046 d = center + fb_line_len * y + fb_pixel_size * x;
1048 memcpy_pic2(d, src[0], w * fb_pixel_size, h, fb_line_len, stride[0], 1);
1050 return 0;
1053 static void check_events(void)
1057 static void flip_page(void)
1059 int next_page = !fb_page;
1060 int page_delta = next_page - fb_page;
1061 #ifdef CONFIG_VIDIX
1062 if (vidix_name)
1063 return;
1064 #endif
1065 if (!vo_doublebuffering)
1066 return;
1068 fb_vinfo.yoffset = fb_page * fb_yres;
1069 ioctl(fb_dev_fd, FBIOPAN_DISPLAY, &fb_vinfo);
1071 center += page_delta * fb_yres * fb_line_len;
1072 fb_page = next_page;
1075 static void draw_osd(void)
1077 vo_draw_text(in_width, in_height, draw_alpha);
1080 static void uninit(void)
1082 if (fb_cmap_changed) {
1083 if (ioctl(fb_dev_fd, FBIOPUTCMAP, &fb_oldcmap))
1084 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore original cmap\n");
1085 fb_cmap_changed = 0;
1087 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo))
1088 mp_msg(MSGT_VO, MSGL_WARN, "ioctl FBIOGET_VSCREENINFO: %s\n", strerror(errno));
1089 fb_orig_vinfo.xoffset = fb_vinfo.xoffset;
1090 fb_orig_vinfo.yoffset = fb_vinfo.yoffset;
1091 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_orig_vinfo))
1092 mp_msg(MSGT_VO, MSGL_WARN, "Can't reset original fb_var_screeninfo: %s\n", strerror(errno));
1093 if (fb_tty_fd >= 0) {
1094 if (ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0)
1095 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore text mode: %s\n", strerror(errno));
1097 vt_set_textarea(0, fb_orig_vinfo.yres);
1098 close(fb_tty_fd);
1099 close(fb_dev_fd);
1100 if (frame_buffer)
1101 munmap(frame_buffer, fb_size);
1102 frame_buffer = NULL;
1103 #ifdef CONFIG_VIDIX
1104 if (vidix_name)
1105 vidix_term();
1106 #endif
1107 fb_preinit(1);
1110 static int preinit(const char *vo_subdevice)
1112 pre_init_err = 0;
1114 if (vo_subdevice) {
1115 #ifdef CONFIG_VIDIX
1116 if (memcmp(vo_subdevice, "vidix", 5) == 0)
1117 vidix_name = &vo_subdevice[5];
1118 if (vidix_name)
1119 pre_init_err = vidix_preinit(vidix_name, &video_out_fbdev);
1120 else
1121 #endif
1123 if (fb_dev_name)
1124 free(fb_dev_name);
1125 fb_dev_name = strdup(vo_subdevice);
1128 if (!pre_init_err)
1129 return pre_init_err = fb_preinit(0) ? 0 : -1;
1130 return -1;
1133 static uint32_t get_image(mp_image_t *mpi)
1135 if (!IMGFMT_IS_BGR(mpi->imgfmt) ||
1136 IMGFMT_BGR_DEPTH(mpi->imgfmt) != fb_bpp ||
1137 (mpi->type != MP_IMGTYPE_STATIC && mpi->type != MP_IMGTYPE_TEMP) ||
1138 (mpi->flags & MP_IMGFLAG_PLANAR) ||
1139 (mpi->flags & MP_IMGFLAG_YUV) ||
1140 mpi->width != in_width ||
1141 mpi->height != in_height
1143 return VO_FALSE;
1145 mpi->planes[0] = center;
1146 mpi->stride[0] = fb_line_len;
1147 mpi->flags |= MP_IMGFLAG_DIRECT;
1148 return VO_TRUE;
1151 static int control(uint32_t request, void *data, ...)
1153 switch (request) {
1154 case VOCTRL_GET_IMAGE:
1155 return get_image(data);
1156 case VOCTRL_QUERY_FORMAT:
1157 return query_format(*(uint32_t*)data);
1160 #ifdef CONFIG_VIDIX
1161 if (vidix_name) {
1162 switch (request) {
1163 case VOCTRL_SET_EQUALIZER:
1165 va_list ap;
1166 int value;
1168 va_start(ap, data);
1169 value = va_arg(ap, int);
1170 va_end(ap);
1172 return vidix_control(request, data, value);
1174 case VOCTRL_GET_EQUALIZER:
1176 va_list ap;
1177 int *value;
1179 va_start(ap, data);
1180 value = va_arg(ap, int*);
1181 va_end(ap);
1183 return vidix_control(request, data, value);
1187 #endif
1189 return VO_NOTIMPL;