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.
34 #include <sys/ioctl.h>
37 #include <libavutil/common.h>
40 #include "video_out.h"
41 #include "video_out_internal.h"
42 #include "fastmemcpy.h"
48 static const vo_info_t info
= {
51 "Szabolcs Berecz <szabi@inf.elte.hu>",
57 static signed int pre_init_err
= -2;
58 /******************************
60 ******************************/
62 static range_t
*monitor_hfreq
= NULL
;
63 static range_t
*monitor_vfreq
= NULL
;
64 static range_t
*monitor_dotclock
= NULL
;
68 uint32_t xres
, yres
, vxres
, vyres
, depth
;
69 uint32_t pixclock
, left
, right
, upper
, lower
, hslen
, vslen
;
74 #define MAX_NR_TOKEN 16
76 #define MAX_LINE_LEN 1000
81 static int validate_mode(fb_mode_t
*m
)
84 mp_msg(MSGT_VO
, MSGL_V
, "needs geometry ");
88 mp_msg(MSGT_VO
, MSGL_V
, "needs timings ");
95 static int line_num
= 0;
97 static char *token
[MAX_NR_TOKEN
];
99 static int get_token(int num
)
101 static int read_nextline
= 1;
106 if (num
>= MAX_NR_TOKEN
) {
107 mp_msg(MSGT_VO
, MSGL_V
, "get_token(): max >= MAX_NR_TOKEN!\n");
112 if (!fgets(line
, MAX_LINE_LEN
, fp
))
118 for (i
= 0; i
< num
; i
++) {
119 while (isspace(line
[line_pos
]))
121 if (line
[line_pos
] == '\0' || line
[line_pos
] == '#') {
125 token
[i
] = line
+ line_pos
;
127 if (c
== '"' || c
== '\'') {
129 while (line
[++line_pos
] != c
&& line
[line_pos
])
133 line
[line_pos
] = ' ';
135 for (/* NOTHING */; !isspace(line
[line_pos
]) &&
136 line
[line_pos
]; line_pos
++)
139 if (!line
[line_pos
]) {
145 line
[line_pos
++] = '\0';
155 static fb_mode_t
*fb_modes
= NULL
;
156 static int nr_modes
= 0;
158 static int parse_fbmode_cfg(char *cfgfile
)
160 #define CHECK_IN_MODE_DEF\
162 mp_msg(MSGT_VO, MSGL_V, "'needs 'mode' first");\
163 goto err_out_print_linenum;\
165 fb_mode_t
*mode
= NULL
;
166 char *endptr
; // strtoul()...
170 /* If called more than once, reuse parsed data */
174 mp_msg(MSGT_VO
, MSGL_V
, "Reading %s: ", cfgfile
);
176 if ((fp
= fopen(cfgfile
, "r")) == NULL
) {
177 mp_msg(MSGT_VO
, MSGL_V
, "can't open '%s': %s\n", cfgfile
, strerror(errno
));
181 if ((line
= malloc(MAX_LINE_LEN
+ 1)) == NULL
) {
182 mp_msg(MSGT_VO
, MSGL_V
, "can't get memory for 'line': %s\n", strerror(errno
));
187 * check if the cfgfile starts with 'mode'
189 while ((tmp
= get_token(1)) == RET_EOL
)
193 if (!strcmp(token
[0], "mode"))
195 goto err_out_parse_error
;
197 while ((tmp
= get_token(1)) != RET_EOF
) {
200 if (!strcmp(token
[0], "mode")) {
202 mp_msg(MSGT_VO
, MSGL_V
, "'endmode' required");
203 goto err_out_print_linenum
;
205 if (!validate_mode(mode
))
206 goto err_out_not_valid
;
209 realloc(fb_modes
, sizeof(fb_mode_t
) * (nr_modes
+ 1)))) {
210 mp_msg(MSGT_VO
, MSGL_V
, "can't realloc 'fb_modes' (nr_modes = %d):"
211 " %s\n", nr_modes
, strerror(errno
));
214 mode
= fb_modes
+ nr_modes
;
216 memset(mode
, 0, sizeof(fb_mode_t
));
218 if (get_token(1) < 0)
219 goto err_out_parse_error
;
220 for (i
= 0; i
< nr_modes
- 1; i
++) {
221 if (!strcmp(token
[0], fb_modes
[i
].name
)) {
222 mp_msg(MSGT_VO
, MSGL_V
, "mode name '%s' isn't unique", token
[0]);
223 goto err_out_print_linenum
;
226 if (!(mode
->name
= strdup(token
[0]))) {
227 mp_msg(MSGT_VO
, MSGL_V
, "can't strdup -> 'name': %s\n", strerror(errno
));
231 } else if (!strcmp(token
[0], "geometry")) {
233 if (get_token(5) < 0)
234 goto err_out_parse_error
;
235 mode
->xres
= strtoul(token
[0], &endptr
, 0);
237 goto err_out_parse_error
;
238 mode
->yres
= strtoul(token
[1], &endptr
, 0);
240 goto err_out_parse_error
;
241 mode
->vxres
= strtoul(token
[2], &endptr
, 0);
243 goto err_out_parse_error
;
244 mode
->vyres
= strtoul(token
[3], &endptr
, 0);
246 goto err_out_parse_error
;
247 mode
->depth
= strtoul(token
[4], &endptr
, 0);
249 goto err_out_parse_error
;
250 } else if (!strcmp(token
[0], "timings")) {
252 if (get_token(7) < 0)
253 goto err_out_parse_error
;
254 mode
->pixclock
= strtoul(token
[0], &endptr
, 0);
256 goto err_out_parse_error
;
257 mode
->left
= strtoul(token
[1], &endptr
, 0);
259 goto err_out_parse_error
;
260 mode
->right
= strtoul(token
[2], &endptr
, 0);
262 goto err_out_parse_error
;
263 mode
->upper
= strtoul(token
[3], &endptr
, 0);
265 goto err_out_parse_error
;
266 mode
->lower
= strtoul(token
[4], &endptr
, 0);
268 goto err_out_parse_error
;
269 mode
->hslen
= strtoul(token
[5], &endptr
, 0);
271 goto err_out_parse_error
;
272 mode
->vslen
= strtoul(token
[6], &endptr
, 0);
274 goto err_out_parse_error
;
275 } else if (!strcmp(token
[0], "endmode")) {
278 } else if (!strcmp(token
[0], "accel")) {
280 if (get_token(1) < 0)
281 goto err_out_parse_error
;
283 * it's only used for text acceleration
284 * so we just ignore it.
286 } else if (!strcmp(token
[0], "hsync")) {
288 if (get_token(1) < 0)
289 goto err_out_parse_error
;
290 if (!strcmp(token
[0], "low"))
291 mode
->sync
&= ~FB_SYNC_HOR_HIGH_ACT
;
292 else if (!strcmp(token
[0], "high"))
293 mode
->sync
|= FB_SYNC_HOR_HIGH_ACT
;
295 goto err_out_parse_error
;
296 } else if (!strcmp(token
[0], "vsync")) {
298 if (get_token(1) < 0)
299 goto err_out_parse_error
;
300 if (!strcmp(token
[0], "low"))
301 mode
->sync
&= ~FB_SYNC_VERT_HIGH_ACT
;
302 else if (!strcmp(token
[0], "high"))
303 mode
->sync
|= FB_SYNC_VERT_HIGH_ACT
;
305 goto err_out_parse_error
;
306 } else if (!strcmp(token
[0], "csync")) {
308 if (get_token(1) < 0)
309 goto err_out_parse_error
;
310 if (!strcmp(token
[0], "low"))
311 mode
->sync
&= ~FB_SYNC_COMP_HIGH_ACT
;
312 else if (!strcmp(token
[0], "high"))
313 mode
->sync
|= FB_SYNC_COMP_HIGH_ACT
;
315 goto err_out_parse_error
;
316 } else if (!strcmp(token
[0], "extsync")) {
318 if (get_token(1) < 0)
319 goto err_out_parse_error
;
320 if (!strcmp(token
[0], "false"))
321 mode
->sync
&= ~FB_SYNC_EXT
;
322 else if (!strcmp(token
[0], "true"))
323 mode
->sync
|= FB_SYNC_EXT
;
325 goto err_out_parse_error
;
326 } else if (!strcmp(token
[0], "laced")) {
328 if (get_token(1) < 0)
329 goto err_out_parse_error
;
330 if (!strcmp(token
[0], "false"))
331 mode
->vmode
= FB_VMODE_NONINTERLACED
;
332 else if (!strcmp(token
[0], "true"))
333 mode
->vmode
= FB_VMODE_INTERLACED
;
335 goto err_out_parse_error
;
336 } else if (!strcmp(token
[0], "double")) {
338 if (get_token(1) < 0)
339 goto err_out_parse_error
;
340 if (!strcmp(token
[0], "false"))
342 else if (!strcmp(token
[0], "true"))
343 mode
->vmode
= FB_VMODE_DOUBLE
;
345 goto err_out_parse_error
;
347 goto err_out_parse_error
;
349 if (!validate_mode(mode
))
350 goto err_out_not_valid
;
352 mp_msg(MSGT_VO
, MSGL_V
, "%d modes\n", nr_modes
);
357 mp_msg(MSGT_VO
, MSGL_V
, "parse error");
358 err_out_print_linenum
:
359 mp_msg(MSGT_VO
, MSGL_V
, " at line %d\n", line_num
);
368 mp_msg(MSGT_VO
, MSGL_V
, "previous mode is not correct");
369 goto err_out_print_linenum
;
372 static fb_mode_t
*find_mode_by_name(char *name
)
376 for (i
= 0; i
< nr_modes
; i
++)
377 if (!strcmp(name
, fb_modes
[i
].name
))
382 static float dcf(fb_mode_t
*m
) //driving clock frequency
384 return 1e12f
/ m
->pixclock
;
387 static float hsf(fb_mode_t
*m
) //horizontal scan frequency
389 int htotal
= m
->left
+ m
->xres
+ m
->right
+ m
->hslen
;
390 return dcf(m
) / htotal
;
393 static float vsf(fb_mode_t
*m
) //vertical scan frequency
395 int vtotal
= m
->upper
+ m
->yres
+ m
->lower
+ m
->vslen
;
396 return hsf(m
) / vtotal
;
400 static int mode_works(fb_mode_t
*m
, range_t
*hfreq
, range_t
*vfreq
,
408 mp_msg(MSGT_VO
, MSGL_DBG2
, "mode %dx%d:", m
->xres
, m
->yres
);
409 if (!in_range(hfreq
, h
)) {
411 mp_msg(MSGT_VO
, MSGL_DBG2
, " hsync out of range.");
413 if (!in_range(vfreq
, v
)) {
415 mp_msg(MSGT_VO
, MSGL_DBG2
, " vsync out of range.");
417 if (!in_range(dotclock
, d
)) {
419 mp_msg(MSGT_VO
, MSGL_DBG2
, " dotclock out of range.");
422 mp_msg(MSGT_VO
, MSGL_DBG2
, " hsync, vsync, dotclock ok.\n");
424 mp_msg(MSGT_VO
, MSGL_DBG2
, "\n");
429 static fb_mode_t
*find_best_mode(int xres
, int yres
, range_t
*hfreq
,
430 range_t
*vfreq
, range_t
*dotclock
)
433 fb_mode_t
*best
= fb_modes
;
436 mp_msg(MSGT_VO
, MSGL_DBG2
, "Searching for first working mode\n");
438 for (i
= 0; i
< nr_modes
; i
++, best
++)
439 if (mode_works(best
, hfreq
, vfreq
, dotclock
))
444 if (i
== nr_modes
- 1)
447 mp_msg(MSGT_VO
, MSGL_DBG2
, "First working mode: %dx%d\n", best
->xres
, best
->yres
);
448 mp_msg(MSGT_VO
, MSGL_DBG2
, "Searching for better modes\n");
450 for (curr
= best
+ 1; i
< nr_modes
- 1; i
++, curr
++) {
451 if (!mode_works(curr
, hfreq
, vfreq
, dotclock
))
454 if (best
->xres
< xres
|| best
->yres
< yres
) {
455 if (curr
->xres
> best
->xres
|| curr
->yres
> best
->yres
) {
456 mp_msg(MSGT_VO
, MSGL_DBG2
, "better than %dx%d, which is too small.\n",
457 best
->xres
, best
->yres
);
460 mp_msg(MSGT_VO
, MSGL_DBG2
, "too small.\n");
461 } else if (curr
->xres
== best
->xres
&& curr
->yres
== best
->yres
&&
462 vsf(curr
) > vsf(best
)) {
463 mp_msg(MSGT_VO
, MSGL_DBG2
, "faster screen refresh.\n");
465 } else if ((curr
->xres
<= best
->xres
&& curr
->yres
<= best
->yres
) &&
466 (curr
->xres
>= xres
&& curr
->yres
>= yres
)) {
467 mp_msg(MSGT_VO
, MSGL_DBG2
, "better than %dx%d, which is too large.\n",
468 best
->xres
, best
->yres
);
471 if (curr
->xres
< xres
|| curr
->yres
< yres
)
472 mp_msg(MSGT_VO
, MSGL_DBG2
, "too small.\n");
473 else if (curr
->xres
> best
->xres
|| curr
->yres
> best
->yres
)
474 mp_msg(MSGT_VO
, MSGL_DBG2
, "too large.\n");
476 mp_msg(MSGT_VO
, MSGL_DBG2
, "it's worse, don't know why.\n");
483 static void set_bpp(struct fb_var_screeninfo
*p
, int bpp
, int rgb
)
485 p
->bits_per_pixel
= FFALIGN(bpp
, 2);
486 p
->red
.msb_right
= p
->green
.msb_right
= p
->blue
.msb_right
= p
->transp
.msb_right
= 0;
487 p
->transp
.offset
= p
->transp
.length
= 0;
491 p
->transp
.offset
= 24;
492 p
->transp
.length
= 8;
523 p
->blue
.offset
= p
->red
.offset
;
528 static void fb_mode2fb_vinfo(fb_mode_t
*m
, struct fb_var_screeninfo
*v
, int rgb
)
532 v
->xres_virtual
= m
->vxres
;
533 v
->yres_virtual
= m
->vyres
;
534 set_bpp(v
, m
->depth
, rgb
);
535 v
->pixclock
= m
->pixclock
;
536 v
->left_margin
= m
->left
;
537 v
->right_margin
= m
->right
;
538 v
->upper_margin
= m
->upper
;
539 v
->lower_margin
= m
->lower
;
540 v
->hsync_len
= m
->hslen
;
541 v
->vsync_len
= m
->vslen
;
547 /******************************
549 ******************************/
551 /* command line/config file options */
552 static char *fb_dev_name
= NULL
;
553 char *fb_mode_cfgfile
= NULL
;
554 char *fb_mode_name
= NULL
;
556 static fb_mode_t
*fb_mode
= NULL
;
558 /* vo_fbdev related variables */
559 static int fb_dev_fd
;
560 static int fb_tty_fd
= -1;
561 static size_t fb_size
;
562 static uint8_t *frame_buffer
;
563 static uint8_t *center
;
564 static struct fb_fix_screeninfo fb_finfo
;
565 static struct fb_var_screeninfo fb_orig_vinfo
;
566 static struct fb_var_screeninfo fb_vinfo
;
567 static unsigned short fb_ored
[256], fb_ogreen
[256], fb_oblue
[256];
568 static struct fb_cmap fb_oldcmap
= { 0, 256, fb_ored
, fb_ogreen
, fb_oblue
};
569 static int fb_cmap_changed
= 0;
571 static int fb_pixel_size
; // 32: 4 24: 3 16: 2 15: 2
572 static int fb_bpp
; // 32: 32 24: 24 16: 16 15: 15
573 static int fb_bpp_we_want
; // 32: 32 24: 24 16: 16 15: 15
574 static int fb_line_len
;
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
);
583 static int in_height
;
584 static int out_width
;
585 static int out_height
;
586 static int first_row
;
588 static uint32_t pixel_format
;
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
= FFMAX3(rcols
, gcols
, bcols
);
615 red
= malloc(cols
* sizeof(red
[0]));
617 mp_msg(MSGT_VO
, MSGL_V
, "Can't allocate red palette with %d entries.\n", cols
);
620 for (i
= 0; i
< rcols
; i
++)
621 red
[i
] = (65535 / (rcols
- 1)) * i
;
623 green
= malloc(cols
* sizeof(green
[0]));
625 mp_msg(MSGT_VO
, MSGL_V
, "Can't allocate green palette with %d entries.\n", cols
);
629 for (i
= 0; i
< gcols
; i
++)
630 green
[i
] = (65535 / (gcols
- 1)) * i
;
632 blue
= malloc(cols
* sizeof(blue
[0]));
634 mp_msg(MSGT_VO
, MSGL_V
, "Can't allocate blue palette with %d entries.\n", cols
);
639 for (i
= 0; i
< bcols
; i
++)
640 blue
[i
] = (65535 / (bcols
- 1)) * i
;
642 cmap
= malloc(sizeof(struct fb_cmap
));
644 mp_msg(MSGT_VO
, MSGL_V
, "Can't allocate color map\n");
662 static int fb_preinit(int reset
)
664 static int fb_preinit_done
= 0;
665 static int fb_works
= 0;
675 fb_dev_fd
= fb_tty_fd
= -1;
677 if (!fb_dev_name
&& !(fb_dev_name
= getenv("FRAMEBUFFER")))
678 fb_dev_name
= strdup("/dev/fb0");
679 mp_msg(MSGT_VO
, MSGL_V
, "using %s\n", fb_dev_name
);
681 if ((fb_dev_fd
= open(fb_dev_name
, O_RDWR
)) == -1) {
682 mp_msg(MSGT_VO
, MSGL_ERR
, "Can't open %s: %s\n", fb_dev_name
, strerror(errno
));
685 if (ioctl(fb_dev_fd
, FBIOGET_VSCREENINFO
, &fb_vinfo
)) {
686 mp_msg(MSGT_VO
, MSGL_ERR
, "Can't get VSCREENINFO: %s\n", strerror(errno
));
689 fb_orig_vinfo
= fb_vinfo
;
691 if ((fb_tty_fd
= open("/dev/tty", O_RDWR
)) < 0) {
692 mp_msg(MSGT_VO
, MSGL_ERR
, "notice: Can't open /dev/tty: %s\n", strerror(errno
));
695 fb_rgb
= !fb_vinfo
.red
.offset
;
696 fb_bpp
= fb_vinfo
.bits_per_pixel
;
698 fb_bpp
= fb_vinfo
.red
.length
+ fb_vinfo
.green
.length
+ fb_vinfo
.blue
.length
;
700 if (fb_bpp
== 8 && !vo_dbpp
) {
701 mp_msg(MSGT_VO
, MSGL_ERR
, "8 bpp output is not supported.\n");
706 if (vo_dbpp
!= 12 && vo_dbpp
!= 15 && vo_dbpp
!= 16
707 && vo_dbpp
!= 24 && vo_dbpp
!= 32) {
708 mp_msg(MSGT_VO
, MSGL_ERR
, "can't switch to %d bpp\n", vo_dbpp
);
714 if (!fb_mode_cfgfile
)
715 fb_mode_cfgfile
= strdup("/etc/fb.modes");
732 static void vt_set_textarea(int u
, int l
)
734 /* how can I determine the font height?
735 * just use 16 for now
737 int urow
= ((u
+ 15) / 16) + 1;
740 mp_msg(MSGT_VO
, MSGL_DBG2
, "vt_set_textarea(%d,%d): %d,%d\n", u
, l
, urow
, lrow
);
741 if (fb_tty_fd
>= 0) {
742 char modestring
[100];
743 snprintf(modestring
, sizeof(modestring
), "\33[%d;%dr\33[%d;%dH", urow
, lrow
, lrow
, 0);
744 write(fb_tty_fd
, modestring
, strlen(modestring
));
749 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
750 uint32_t d_height
, uint32_t flags
, char *title
,
753 struct fb_cmap
*cmap
;
754 int vm
= flags
& VOFLAG_MODESWITCHING
;
756 fs
= flags
& VOFLAG_FULLSCREEN
;
758 if (pre_init_err
== -2) {
759 mp_msg(MSGT_VO
, MSGL_ERR
, "Internal fatal error: config() was called before preinit()\n");
766 if (fb_mode_name
&& !vm
) {
767 mp_msg(MSGT_VO
, MSGL_ERR
, "-fbmode can only be used with -vm\n");
770 if (vm
&& parse_fbmode_cfg(fb_mode_cfgfile
) < 0)
772 if (d_width
&& (fs
|| vm
)) {
774 out_height
= d_height
;
781 pixel_format
= format
;
784 if (!(fb_mode
= find_mode_by_name(fb_mode_name
))) {
785 mp_msg(MSGT_VO
, MSGL_ERR
, "can't find requested video mode\n");
788 fb_mode2fb_vinfo(fb_mode
, &fb_vinfo
, fb_rgb
);
790 monitor_hfreq
= str2range(monitor_hfreq_str
);
791 monitor_vfreq
= str2range(monitor_vfreq_str
);
792 monitor_dotclock
= str2range(monitor_dotclock_str
);
793 if (!monitor_hfreq
|| !monitor_vfreq
|| !monitor_dotclock
) {
794 mp_msg(MSGT_VO
, MSGL_ERR
, "you have to specify the capabilities of"
798 if (!(fb_mode
= find_best_mode(out_width
, out_height
, monitor_hfreq
,
799 monitor_vfreq
, monitor_dotclock
))) {
800 mp_msg(MSGT_VO
, MSGL_ERR
, "can't find best video mode\n");
803 mp_msg(MSGT_VO
, MSGL_V
, "using mode %dx%d @ %.1fHz\n", fb_mode
->xres
,
804 fb_mode
->yres
, vsf(fb_mode
));
805 fb_mode2fb_vinfo(fb_mode
, &fb_vinfo
, fb_rgb
);
807 fb_bpp_we_want
= fb_bpp
;
808 set_bpp(&fb_vinfo
, fb_bpp
, fb_rgb
);
809 fb_vinfo
.xres_virtual
= fb_vinfo
.xres
;
810 fb_vinfo
.yres_virtual
= fb_vinfo
.yres
;
812 if (vo_doublebuffering
) {
813 fb_vinfo
.yres_virtual
<<= 1;
814 fb_vinfo
.yoffset
= 0;
815 fb_page
= 1; // start writing into the page we don't display
818 if (fb_tty_fd
>= 0 && ioctl(fb_tty_fd
, KDSETMODE
, KD_GRAPHICS
) < 0) {
819 mp_msg(MSGT_VO
, MSGL_V
, "Can't set graphics mode: %s\n", strerror(errno
));
824 if (ioctl(fb_dev_fd
, FBIOPUT_VSCREENINFO
, &fb_vinfo
))
825 // Intel drivers fail if we request a transparency channel
826 fb_vinfo
.transp
.length
= fb_vinfo
.transp
.offset
= 0;
827 if (ioctl(fb_dev_fd
, FBIOPUT_VSCREENINFO
, &fb_vinfo
)) {
828 mp_msg(MSGT_VO
, MSGL_ERR
, "Can't put VSCREENINFO: %s\n", strerror(errno
));
829 if (fb_tty_fd
>= 0 && ioctl(fb_tty_fd
, KDSETMODE
, KD_TEXT
) < 0) {
830 mp_msg(MSGT_VO
, MSGL_ERR
, "Can't restore text mode: %s\n", strerror(errno
));
835 fb_pixel_size
= fb_vinfo
.bits_per_pixel
/ 8;
836 fb_bpp
= fb_vinfo
.bits_per_pixel
;
838 fb_bpp
= fb_vinfo
.red
.length
+ fb_vinfo
.green
.length
+ fb_vinfo
.blue
.length
;
839 if (fb_bpp_we_want
!= fb_bpp
)
840 mp_msg(MSGT_VO
, MSGL_WARN
, "requested %d bpp, got %d bpp!!!\n",
841 fb_bpp_we_want
, fb_bpp
);
845 draw_alpha_p
= vo_draw_alpha_rgb32
;
848 draw_alpha_p
= vo_draw_alpha_rgb24
;
851 draw_alpha_p
= vo_draw_alpha_rgb16
;
854 draw_alpha_p
= vo_draw_alpha_rgb15
;
857 draw_alpha_p
= vo_draw_alpha_rgb12
;
863 fb_xres
= fb_vinfo
.xres
;
864 fb_yres
= fb_vinfo
.yres
;
868 out_height
= fb_yres
;
870 if (out_width
< in_width
|| out_height
< in_height
) {
871 mp_msg(MSGT_VO
, MSGL_ERR
, "screensize is smaller than video size\n");
875 first_row
= (out_height
- in_height
) / 2;
876 last_row
= (out_height
+ in_height
) / 2;
878 if (ioctl(fb_dev_fd
, FBIOGET_FSCREENINFO
, &fb_finfo
)) {
879 mp_msg(MSGT_VO
, MSGL_ERR
, "Can't get FSCREENINFO: %s\n", strerror(errno
));
883 if (fb_finfo
.type
!= FB_TYPE_PACKED_PIXELS
) {
884 mp_msg(MSGT_VO
, MSGL_ERR
, "type %d not supported\n", fb_finfo
.type
);
888 switch (fb_finfo
.visual
) {
889 case FB_VISUAL_TRUECOLOR
:
891 case FB_VISUAL_DIRECTCOLOR
:
892 mp_msg(MSGT_VO
, MSGL_V
, "creating cmap for directcolor\n");
893 if (ioctl(fb_dev_fd
, FBIOGETCMAP
, &fb_oldcmap
)) {
894 mp_msg(MSGT_VO
, MSGL_ERR
, "can't get cmap: %s\n",
898 if (!(cmap
= make_directcolor_cmap(&fb_vinfo
)))
900 if (ioctl(fb_dev_fd
, FBIOPUTCMAP
, cmap
)) {
901 mp_msg(MSGT_VO
, MSGL_ERR
, "can't put cmap: %s\n",
912 mp_msg(MSGT_VO
, MSGL_ERR
, "visual: %d not yet supported\n",
917 fb_line_len
= fb_finfo
.line_length
;
918 fb_size
= fb_finfo
.smem_len
;
919 if (vo_doublebuffering
&& fb_size
< 2 * fb_yres
* fb_line_len
)
921 mp_msg(MSGT_VO
, MSGL_WARN
, "framebuffer too small for double-buffering, disabling\n");
922 vo_doublebuffering
= 0;
927 int x_offset
= 0, y_offset
= 0;
928 geometry(&x_offset
, &y_offset
, &out_width
, &out_height
, fb_xres
, fb_yres
);
930 frame_buffer
= mmap(0, fb_size
, PROT_READ
| PROT_WRITE
,
931 MAP_SHARED
, fb_dev_fd
, 0);
932 if (frame_buffer
== (uint8_t *) -1) {
933 mp_msg(MSGT_VO
, MSGL_ERR
, "Can't mmap %s: %s\n", fb_dev_name
, strerror(errno
));
937 center
= frame_buffer
+
938 ( (out_width
- in_width
) / 2 ) * fb_pixel_size
+
939 ( (out_height
- in_height
) / 2 ) * fb_line_len
+
940 x_offset
* fb_pixel_size
+ y_offset
* fb_line_len
+
941 fb_page
* fb_yres
* fb_line_len
;
943 mp_msg(MSGT_VO
, MSGL_DBG2
, "frame_buffer @ %p\n", frame_buffer
);
944 mp_msg(MSGT_VO
, MSGL_DBG2
, "center @ %p\n", center
);
945 mp_msg(MSGT_VO
, MSGL_V
, "pixel per line: %d\n", fb_line_len
/ fb_pixel_size
);
948 int clear_size
= fb_line_len
* fb_yres
;
949 if (vo_doublebuffering
)
951 memset(frame_buffer
, 0, clear_size
);
955 vt_set_textarea(last_row
, fb_yres
);
960 static int query_format(uint32_t format
)
964 if ((format
& IMGFMT_BGR_MASK
) == (fb_rgb
? IMGFMT_RGB
: IMGFMT_BGR
)) {
965 int bpp
= format
& 0xff;
968 return VFCAP_ACCEPT_STRIDE
| VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
;
973 static void draw_alpha(int x0
, int y0
, int w
, int h
, unsigned char *src
,
974 unsigned char *srca
, int stride
)
978 dst
= center
+ fb_line_len
* y0
+ fb_pixel_size
* x0
;
980 (*draw_alpha_p
)(w
, h
, src
, srca
, stride
, dst
, fb_line_len
);
983 static int draw_frame(uint8_t *src
[])
988 static int draw_slice(uint8_t *src
[], int stride
[], int w
, int h
, int x
, int y
)
992 d
= center
+ fb_line_len
* y
+ fb_pixel_size
* x
;
994 memcpy_pic2(d
, src
[0], w
* fb_pixel_size
, h
, fb_line_len
, stride
[0], 1);
999 static void check_events(void)
1003 static void flip_page(void)
1005 int next_page
= !fb_page
;
1006 int page_delta
= next_page
- fb_page
;
1007 if (!vo_doublebuffering
)
1010 fb_vinfo
.yoffset
= fb_page
* fb_yres
;
1011 ioctl(fb_dev_fd
, FBIOPAN_DISPLAY
, &fb_vinfo
);
1013 center
+= page_delta
* fb_yres
* fb_line_len
;
1014 fb_page
= next_page
;
1017 static void draw_osd(void)
1019 vo_draw_text(in_width
, in_height
, draw_alpha
);
1022 static void uninit(void)
1024 if (fb_cmap_changed
) {
1025 if (ioctl(fb_dev_fd
, FBIOPUTCMAP
, &fb_oldcmap
))
1026 mp_msg(MSGT_VO
, MSGL_WARN
, "Can't restore original cmap\n");
1027 fb_cmap_changed
= 0;
1029 if (ioctl(fb_dev_fd
, FBIOGET_VSCREENINFO
, &fb_vinfo
))
1030 mp_msg(MSGT_VO
, MSGL_WARN
, "ioctl FBIOGET_VSCREENINFO: %s\n", strerror(errno
));
1031 fb_orig_vinfo
.xoffset
= fb_vinfo
.xoffset
;
1032 fb_orig_vinfo
.yoffset
= fb_vinfo
.yoffset
;
1033 if (ioctl(fb_dev_fd
, FBIOPUT_VSCREENINFO
, &fb_orig_vinfo
))
1034 mp_msg(MSGT_VO
, MSGL_WARN
, "Can't reset original fb_var_screeninfo: %s\n", strerror(errno
));
1035 if (fb_tty_fd
>= 0) {
1036 if (ioctl(fb_tty_fd
, KDSETMODE
, KD_TEXT
) < 0)
1037 mp_msg(MSGT_VO
, MSGL_WARN
, "Can't restore text mode: %s\n", strerror(errno
));
1039 vt_set_textarea(0, fb_orig_vinfo
.yres
);
1043 munmap(frame_buffer
, fb_size
);
1044 frame_buffer
= NULL
;
1048 static int preinit(const char *vo_subdevice
)
1055 fb_dev_name
= strdup(vo_subdevice
);
1059 return pre_init_err
= fb_preinit(0) ? 0 : -1;
1063 static uint32_t get_image(mp_image_t
*mpi
)
1065 if (!IMGFMT_IS_BGR(mpi
->imgfmt
) ||
1066 IMGFMT_BGR_DEPTH(mpi
->imgfmt
) != fb_bpp
||
1067 (mpi
->type
!= MP_IMGTYPE_STATIC
&& mpi
->type
!= MP_IMGTYPE_TEMP
) ||
1068 (mpi
->flags
& MP_IMGFLAG_PLANAR
) ||
1069 (mpi
->flags
& MP_IMGFLAG_YUV
) ||
1070 mpi
->width
!= in_width
||
1071 mpi
->height
!= in_height
1075 mpi
->planes
[0] = center
;
1076 mpi
->stride
[0] = fb_line_len
;
1077 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
1081 static int control(uint32_t request
, void *data
)
1084 case VOCTRL_GET_IMAGE
:
1085 return get_image(data
);
1086 case VOCTRL_QUERY_FORMAT
:
1087 return query_format(*(uint32_t*)data
);