3 Video driver for SVGAlib
4 by Zoltan Mark Vician <se7en@sch.bme.hu>
5 Code started: Mon Apr 1 23:25:47 2001
6 Some changes by Matan Ziv-Av <matan@svgalib.org>
7 Compleat rewrite by Ivan Kalvachev 19 Mar 2003:
10 - 1bpp doesn't work right for me with '-double' and svgalib 1.4.3,
11 but works OK with svgalib 1.9.17
12 - The HW acceleration is not tested - svgalibs supports few chipsets,
13 and i don't have any of them. If it works for you then let me know.
14 I will remove this warning after confirm its status.
15 - retrace sync works only in doublebuffer mode.
16 - the retrace sync may slow down decoding a lot - mplayer is blocked while
18 - denoise3d fails to find common colorspace, use -vf denoise3d,scale
21 - let choose_best_mode take aspect into account
22 - set palette from mpi->palette or mpi->plane[1]
23 - make faster OSD black bars clear - need some OSD changes
24 - Make nicer CONFIG parsing
25 - change video mode logical width to match img->stride[0] - for HW only
37 #include "video_out.h"
38 #include "video_out_internal.h"
39 #include "fastmemcpy.h"
40 #include "osdep/getch2.h"
42 #include "vosub_vidix.h"
48 //#include "mp_image.h"
52 //silence warnings, probably it have to go in some global header
53 #define UNUSED(x) ((void)(x))
55 extern int vo_doublebuffering
;
56 extern int vo_directrendering
;
60 static uint32_t query_format(uint32_t format
);
61 static void draw_alpha(int x0
, int y0
, int w
, int h
, unsigned char *src
,
62 unsigned char *srca
, int stride
);
63 static uint32_t get_image(mp_image_t
*mpi
);
69 #define CAP_ACCEL_CLEAR 8
70 #define CAP_ACCEL_PUTIMAGE 4
71 #define CAP_ACCEL_BACKGR 2
74 static uint8_t zerobuf
[8192];//used when clear screen with vga_draw
77 static int force_vm
=0;
78 static int force_native
=0;
79 static int sync_flip
=0;
80 static int blackbar_osd
=0;
81 static int cpage
,max_pages
,old_page
;
83 static vga_modeinfo
* modeinfo
;
84 static int mode_stride
; //keep it in case of vga_setlogicalwidth
85 static int stride_granularity
; //not yet used
87 static int mode_capabilities
;
89 static int image_width
,image_height
; // used by OSD
90 static int x_pos
, y_pos
;
93 int yoffset
;//y position of the page
94 int doffset
;//display start of the page
95 void * vbase
;//memory start address of the page
99 static vo_info_t info
= {
102 "Ivan Kalvachev <iive@users.sf.net>",
107 static char vidix_name
[32] = "";
108 static vidix_grkey_t gr_key
;
114 //return number of 1'st free page or -1 if no free one
115 static inline int page_find_free(){
117 for(i
=0;i
<max_pages
;i
++)
118 if(PageStore
[i
].locks
== PAGE_EMPTY
) return i
;
122 static uint32_t preinit(const char *arg
)
128 memset(zerobuf
,0,sizeof(zerobuf
));
129 force_vm
=force_native
=squarepix
=0;
135 if(memcmp(arg
,"vidix",5)==0) {
137 while(arg
[i
] && arg
[i
]!=':') i
++;
138 strncpy(vidix_name
, arg
+6, i
-6);
142 vidix_preinit(vidix_name
, &video_out_svga
);
145 if(!strncmp(arg
,"sq",2)) {
148 if( *arg
== ':' ) arg
++;
151 if(!strncmp(arg
,"native",6)) {
154 if( *arg
== ':' ) arg
++;
157 if(!strncmp(arg
,"bbosd",5)) {
160 if( *arg
== ':' ) arg
++;
163 if(!strncmp(arg
,"retrace",7)) {
166 if( *arg
== ':' ) arg
++;
171 while(arg
[i
] && arg
[i
]!=':')i
++;
176 force_vm
=vga_getmodenumber(s
);
178 if(verbose
) printf("vo_svga: Forcing mode %i\n",force_vm
);
192 static void svga_clear_box(int x
,int y
,int w
,int h
){
196 if (mode_capabilities
&CAP_ACCEL_CLEAR
){
198 printf("vo_svga: clearing box %d,%d - %d,%d with HW acceleration\n",
200 if(mode_capabilities
&CAP_ACCEL_BACKGR
)
201 vga_accel(ACCEL_SYNC
);
202 vga_accel(ACCEL_SETFGCOLOR
,0);//black
203 vga_accel(ACCEL_FILLBOX
,x
,y
,w
,h
);
206 if (mode_capabilities
& CAP_LINEAR
){
208 printf("vo_svga: clearing box %d,%d - %d,%d with memset\n",x
,y
,w
,h
);
209 rgbplane
=PageStore
[0].vbase
+ (y
*mode_stride
) + (x
*modeinfo
->bytesperpixel
);
211 //i'm afraid that memcpy is better optimized than memset;)
212 memcpy(rgbplane
,zerobuf
,w
*modeinfo
->bytesperpixel
);
213 // memset(rgbplane,0,w*modeinfo->bytesperpixel);
214 rgbplane
+=mode_stride
;
220 printf("vo_svga: clearing box %d,%d - %d,%d with native draw \n",x
,y
,w
,h
);
221 if(modeinfo
->bytesperpixel
!=0) w
*=modeinfo
->bytesperpixel
;
223 vga_drawscansegment(zerobuf
,x
,y
+i
,w
);
227 static uint32_t svga_draw_image(mp_image_t
*mpi
){
230 uint8_t *rgbplane
, *base
;
234 if(mpi
->flags
& MP_IMGFLAG_DIRECT
){
236 printf("vo_svga: drawing direct rendered surface\n");
237 cpage
=(uint32_t)mpi
->priv
;
238 assert((cpage
>=0)&&(cpage
<max_pages
));
239 return VO_TRUE
; //it's already done
241 // if (mpi->flags&MP_IMGFLAGS_DRAWBACK)
242 // return VO_TRUE;//direct render method 2
244 //find a free page to draw into
245 //if there is no one then use the current one
246 page
= page_find_free();
247 if(page
>=0) cpage
=page
;
248 PageStore
[cpage
].locks
=PAGE_BUSY
;
250 // these variables are used in loops
255 stride
= mpi
->stride
[0];
256 rgbplane
= mpi
->planes
[0] + y
*stride
+ (x
*mpi
->bpp
)/8;
260 if(mpi
->bpp
>= 8){//for modes<8 use only native
261 if( (mode_capabilities
&CAP_ACCEL_PUTIMAGE
) && (x
==0) && (w
==mpi
->width
) &&
262 (stride
== mode_stride
) ){ //only monolite image can be accelerated
263 w
=(stride
*8)/mpi
->bpp
;//we transfer pixels in the stride so the source
266 printf("vo_svga: using HW PutImage (x=%d,y=%d,w=%d,h=%d)\n",x
,y
,w
,h
);
267 if(mode_capabilities
& CAP_ACCEL_BACKGR
)
268 vga_accel(ACCEL_SYNC
);
270 vga_accel(ACCEL_PUTIMAGE
,x
,y
+PageStore
[cpage
].yoffset
,w
,h
,rgbplane
);
274 if( mode_capabilities
&CAP_LINEAR
){
277 printf("vo_svga: using Direct memcpy (x=%d,y=%d,w=%d,h=%d)\n",x
,y
,w
,h
);
278 bytesperline
=(w
*mpi
->bpp
)/8;
279 base
=PageStore
[cpage
].vbase
+ (y
*mode_stride
) + (x
*mpi
->bpp
)/8;
282 mem2agpcpy(base
,rgbplane
,bytesperline
);
294 length
=(w
*mpi
->bpp
)/8;
295 //one byte per pixel! svgalib innovation
296 if(mpi
->imgfmt
==IMGFMT_RG4B
|| mpi
->imgfmt
==IMGFMT_BG4B
) length
=w
;
299 printf("vo_svga: using Native vga_draw(x=%d,y=%d,w=%d,h=%d)\n",x
,y
,w
,h
);
300 y
+=PageStore
[cpage
].yoffset
;//y position of the page beggining
302 vga_drawscansegment(rgbplane
,x
,y
+i
,length
);
309 int bpp_from_vminfo(vga_modeinfo
*vminfo
){
310 switch(vminfo
->colors
){
314 case 32768: return 15;
315 case 65536: return 16;
316 case 1<<24: return 8*vminfo
->bytesperpixel
;
321 int find_best_svga_mode(int req_w
,int req_h
, int req_bpp
){
322 int badness
,prev_badness
;
323 int bestmode
,lastmode
;
325 vga_modeinfo
*vminfo
;
326 //int best aspect mode // best linear mode // best normal mode (no modeX)
328 prev_badness
= 0;//take care of special case below
329 bestmode
= 0; //0 is the TEXT mode
330 lastmode
= vga_lastmodenumber();
331 for(i
=1;i
<=lastmode
;i
++){
332 vminfo
= vga_getmodeinfo(i
);
333 if( vminfo
== NULL
) continue;
335 printf("vo_svga: testing mode %d (%s)\n",i
,vga_getmodename(i
));
336 if( vga_hasmode(i
) == 0 ) continue;
337 if( req_bpp
!= bpp_from_vminfo(vminfo
) )continue;
338 if( (vminfo
->width
< req_w
) || (vminfo
->height
< req_h
) ) continue;
339 badness
=(vminfo
->width
* vminfo
->height
) - (req_h
* req_w
);
340 //put here aspect calculations
342 if( vminfo
->width
*3 != vminfo
->height
*4 ) continue;
344 if( bestmode
==0 || prev_badness
>= badness
){//modeX etc...
345 prev_badness
=badness
;
348 printf("vo_svga: found good mode %d with badness %d\n",i
,badness
);
354 static uint32_t control(uint32_t request
, void *data
, ...)
357 case VOCTRL_QUERY_FORMAT
:
358 return query_format(*((uint32_t*)data
));
359 case VOCTRL_DRAW_IMAGE
:
360 return svga_draw_image( (mp_image_t
*)data
);
361 case VOCTRL_GET_IMAGE
:
362 return get_image(data
);
368 case VOCTRL_SET_EQUALIZER
:
374 value
= va_arg(ap
, int);
377 return vidix_control(request
, data
, (int *)value
);
379 case VOCTRL_GET_EQUALIZER
:
385 value
= va_arg(ap
, int*);
388 return vidix_control(request
, data
, value
);
391 return vidix_control(request
, data
);
399 // This function is called to init the video driver for specific mode
401 static uint32_t config(uint32_t width
, uint32_t height
, uint32_t d_width
,
402 uint32_t d_height
, uint32_t flags
, char *title
,
404 int32_t req_w
= width
;// (d_width > 0 ? d_width : width);
405 int32_t req_h
= height
;// (d_height > 0 ? d_height : height);
406 uint16_t vid_mode
= 0;
411 printf("vo_svga: config(%i, %i, %i, %i, %08x, %s, %08x)\n", width
, height
,
412 d_width
, d_height
, flags
, title
, format
);
413 //Only RGB modes supported
414 if (!IMGFMT_IS_RGB(format
) && !IMGFMT_IS_BGR(format
)) {assert(0);return -1;}
415 req_bpp
= IMGFMT_BGR_DEPTH(format
);
417 if( vo_dbpp
!=0 && vo_dbpp
!=req_bpp
) {assert(0);return-1;}
421 printf("vo_svga: Looking for the best resolution...\n");
422 printf("vo_svga: req_w: %d, req_h: %d, bpp: %d\n",req_w
,req_h
,req_bpp
);
424 vid_mode
=find_best_svga_mode(req_w
,req_h
,req_bpp
);
427 modeinfo
=vga_getmodeinfo(vid_mode
);
430 if(vga_hasmode(vid_mode
) == 0){
431 printf("vo_svga: forced vid_mode %d (%s) not available\n",
432 vid_mode
,vga_getmodename(vid_mode
));
435 modeinfo
=vga_getmodeinfo(vid_mode
);
436 if( (modeinfo
->width
< req_w
) || (modeinfo
->height
< req_h
) ){
437 printf("vo_svga: forced vid_mode %d (%s) too small\n",
438 vid_mode
,vga_getmodename(vid_mode
));
442 mode_bpp
=bpp_from_vminfo(modeinfo
);
444 printf("vo_svga: vid_mode: %d, %dx%d %dbpp\n",
445 vid_mode
,modeinfo
->width
,modeinfo
->height
,mode_bpp
);
447 if (vga_setmode(vid_mode
) == -1) {
448 printf("vo_svga: vga_setmode(%d) failed.\n",vid_mode
);
452 /* set 332 palette for 8 bpp */
456 vga_setpalette(i
, ((i
>>5)&7)*9, ((i
>>2)&7)*9, (i
&3)*21);
458 /* set 121 palette for 4 bpp */
459 else if(mode_bpp
==4){
462 vga_setpalette(i
, ((i
>>3)&1)*63, ((i
>>1)&3)*21, (i
&1)*63);
464 //if we change the logical width, we should know the granularity
465 stride_granularity
=8;//according to man vga_logicalwidth
466 if(modeinfo
->flags
& EXT_INFO_AVAILABLE
){
467 stride_granularity
=modeinfo
->linewidth_unit
;
469 //look for hardware acceleration
470 mode_capabilities
=0;//NATIVE;
471 if(!force_native
){//if we want to use only native drawers
472 if(modeinfo
->flags
& HAVE_EXT_SET
){//support for hwaccel interface
473 accflags
=vga_ext_set(VGA_EXT_AVAILABLE
,VGA_AVAIL_ACCEL
);
474 if(accflags
& ACCELFLAG_FILLBOX
) // clear screen
475 mode_capabilities
|=CAP_ACCEL_CLEAR
;
476 if(accflags
& ACCELFLAG_PUTIMAGE
)//support for mem->vid transfer
477 mode_capabilities
|=CAP_ACCEL_PUTIMAGE
;
478 if((accflags
& ACCELFLAG_SETMODE
) && (accflags
& ACCELFLAG_SYNC
)){
479 vga_accel(ACCEL_SETMODE
,BLITS_IN_BACKGROUND
);
480 mode_capabilities
|=CAP_ACCEL_BACKGR
;//can draw in backgraund
483 if(modeinfo
->flags
& IS_LINEAR
){
484 mode_capabilities
|=CAP_LINEAR
; //don't use bank & vga_draw
487 if(modeinfo
->flags
& CAPABLE_LINEAR
){
489 vid_mem_size
= vga_setlinearaddressing();
490 if(vid_mem_size
!= -1){
491 modeinfo
=vga_getmodeinfo(vid_mode
);//sometimes they change parameters
492 mode_capabilities
|=CAP_LINEAR
;
497 if(mode_capabilities
&CAP_LINEAR
){
498 printf("vo_svga: video mode is linear and memcpy could be used for image transfer\n");
500 if(mode_capabilities
&CAP_ACCEL_PUTIMAGE
){
501 printf("vo_svga: video mode have hardware acceleration and put_image could be used\n");
502 printf("vo_svga: If it works for you i would like to know \nvo_svga: (send log with `mplayer test.avi -v -v -v -v &> svga.log`). Thx\n");
505 //here is the place to handle strides for accel_ modes;
506 mode_stride
=modeinfo
->linewidth
;
507 //we may try to set a bigger stride for video mode that will match the mpi->stride,
508 //this way we will transfer more data, but HW put_image can do it in backgraund!
510 //now let's see how many pages we can use
511 max_pages
= modeinfo
->maxpixels
/(modeinfo
->height
* modeinfo
->width
);
512 if(max_pages
> MAXPAGES
) max_pages
= MAXPAGES
;
513 if(!vo_doublebuffering
) max_pages
=1;
514 //fill PageStore structs
519 GRAPH_MEM
=vga_getgraphmem();
520 for(i
=0;i
<max_pages
;i
++){
521 //calculate display offset
522 dof
= i
* modeinfo
->height
* modeinfo
->width
;
523 if(modeinfo
->bytesperpixel
!= 0) dof
*=modeinfo
->bytesperpixel
;
524 //check video chip limitations
525 if( dof
!= (dof
& modeinfo
->startaddressrange
) ){
526 max_pages
=i
;//page 0 will never come here
529 PageStore
[i
].yoffset
= i
* modeinfo
->height
;//starting y offset
530 PageStore
[i
].vbase
= GRAPH_MEM
+ i
*modeinfo
->height
*mode_stride
; //memory base address
531 PageStore
[i
].doffset
= dof
; //display offset
532 PageStore
[i
].locks
= PAGE_EMPTY
;
536 printf("vo_svga: video mode have %d page(s)\n",max_pages
);
538 if(modeinfo
->bytesperpixel
!=0)
539 vga_claimvideomemory(max_pages
* modeinfo
->height
* modeinfo
->width
* modeinfo
->bytesperpixel
);
541 vga_claimvideomemory(max_pages
* modeinfo
->height
* modeinfo
->width
* mode_bpp
/ 8);
544 svga_clear_box(0,0,modeinfo
->width
,modeinfo
->height
* max_pages
);
548 x_pos
= (modeinfo
->width
- req_w
) / 2;
549 y_pos
= (modeinfo
->height
- req_h
) / 2;
550 x_pos
&= ~(15); //align x offset position to 16 pixels
551 printf("vo_svga: centering image. start at (%d,%d)\n",x_pos
,y_pos
);
556 vidix_init(width
, height
, x_pos
, y_pos
, modeinfo
->width
, modeinfo
->height
,
557 format
, mode_bpp
, modeinfo
->width
,modeinfo
->height
);
558 printf("vo_svga: Using VIDIX. w=%i h=%i mw=%i mh=%i\n",width
,height
,
559 modeinfo
->width
,modeinfo
->height
);
562 if(vidix_grkey_support()){
563 vidix_grkey_get(&gr_key
);
564 gr_key
.key_op
= KEYS_PUT
;
565 if (!(vo_colorkey
& 0xFF000000)) {
566 gr_key
.ckey
.op
= CKEY_TRUE
;
567 gr_key
.ckey
.red
= (vo_colorkey
& 0x00FF0000) >> 16;
568 gr_key
.ckey
.green
= (vo_colorkey
& 0x0000FF00) >> 8;
569 gr_key
.ckey
.blue
= vo_colorkey
& 0x000000FF;
571 gr_key
.ckey
.op
= CKEY_FALSE
;
572 vidix_grkey_set(&gr_key
);
577 vga_setdisplaystart(0);
581 static uint32_t draw_slice(uint8_t *image
[],int stride
[],
582 int w
, int h
, int x
, int y
) {
584 UNUSED(image
);UNUSED(stride
);
588 return VO_ERROR
;//this is yv12 only -> vf_scale should do all transforms
591 static uint32_t draw_frame(uint8_t *src
[]) {
594 return VO_ERROR
;//this one should not be called
597 static void draw_osd(void)
600 printf("vo_svga: draw_osd()\n");
601 //only modes with bytesperpixel>0 can draw OSD
602 if(modeinfo
->bytesperpixel
==0) return;
603 if(!(mode_capabilities
&CAP_LINEAR
)) return;//force_native will remove OSD
609 svga_clear_box(0,0 + PageStore
[cpage
].yoffset
,
610 modeinfo
->width
, y_pos
);
611 svga_clear_box(0, image_height
+ y_pos
+ PageStore
[cpage
].yoffset
,
612 modeinfo
->width
, modeinfo
->height
-(image_height
+ y_pos
));
613 svga_clear_box(0, y_pos
+ PageStore
[cpage
].yoffset
,
614 x_pos
, image_height
);
615 svga_clear_box(image_width
+ x_pos
, y_pos
+ PageStore
[cpage
].yoffset
,
616 modeinfo
->width
-(x_pos
+image_width
), image_height
);
617 // vo_remove_text(modeinfo->width, modeinfo->height, clear_alpha);
618 vo_draw_text(modeinfo
->width
, modeinfo
->height
, draw_alpha
);
620 vo_draw_text(image_width
, image_height
, draw_alpha
);
624 static void flip_page(void) {
625 PageStore
[old_page
].locks
=PAGE_EMPTY
;
626 PageStore
[cpage
].locks
=PAGE_BUSY
;
629 printf("vo_svga: viewing page %d\n",cpage
);
630 if(sync_flip
&& old_page
!=cpage
){
631 if(verbose
> 2) printf("vo_svga:vga_waitretrace\n");
634 vga_setdisplaystart(PageStore
[cpage
].doffset
);
636 old_page
=cpage
;//cpage will be overwriten on next draw_image
639 static void check_events(void) {
642 static void uninit(void) {
645 if(vidix_name
[0])vidix_term();
650 /* --------------------------------------------------------------------- */
651 static uint32_t query_format(uint32_t format
) {
652 int32_t req_bpp
,flags
;
654 vga_modeinfo
* vminfo
;
657 printf("vo_svga: query_format=%X \n",format
);
658 //only RGB modes supported
659 if( (!IMGFMT_IS_RGB(format
)) && (!IMGFMT_IS_BGR(format
)) ) return 0;
661 // Reject different endian
662 #ifdef WORDS_BIGENDIAN
663 if (IMGFMT_IS_BGR(format
)) return 0;
665 if (IMGFMT_IS_RGB(format
)) return 0;
668 //svgalib supports only BG4B! if we want BGR4 we have to emulate it (sw)
669 if( format
==IMGFMT_BGR4
|| format
==IMGFMT_RGB4
) return 0;
670 req_bpp
= IMGFMT_RGB_DEPTH(format
);
671 if( vo_dbpp
>0 && vo_dbpp
!=req_bpp
) return 0; //support -bpp options
673 lastmode
= vga_lastmodenumber();
674 for(i
=1;i
<=lastmode
;i
++){
675 vminfo
= vga_getmodeinfo(i
);
676 if( vminfo
== NULL
) continue;
677 if( vga_hasmode(i
) == 0 ) continue;
678 if( req_bpp
!= bpp_from_vminfo(vminfo
) ) continue;
679 if( (force_vm
> 0) && (force_vm
!= i
) ) continue;//quick hack
680 flags
= VFCAP_CSP_SUPPORTED
|
681 VFCAP_CSP_SUPPORTED_BY_HW
|
684 if(req_bpp
>8) flags
|=VFCAP_OSD
;
690 static void draw_alpha(int x0
, int y0
, int w
, int h
, unsigned char *src
,
691 unsigned char *srca
, int stride
) {
695 printf("vo_svga: draw_alpha(x0=%d,y0=%d,w=%d,h=%d,src=%p,srca=%p,stride=%d\n",
696 x0
,y0
,w
,h
,src
,srca
,stride
);
698 //drawing in the image, so place the stuff there
704 printf("vo_svga: OSD draw in page %d\n",cpage
);
705 base
=PageStore
[cpage
].vbase
+ y0
*mode_stride
+ x0
*modeinfo
->bytesperpixel
;
708 vo_draw_alpha_rgb32(w
, h
, src
, srca
, stride
, base
, mode_stride
);
711 vo_draw_alpha_rgb24(w
, h
, src
, srca
, stride
, base
, mode_stride
);
714 vo_draw_alpha_rgb16(w
, h
, src
, srca
, stride
, base
, mode_stride
);
717 vo_draw_alpha_rgb15(w
, h
, src
, srca
, stride
, base
, mode_stride
);
722 static uint32_t get_image(mp_image_t
*mpi
){
725 if(!IMGFMT_IS_BGR(mpi
->imgfmt
) && !IMGFMT_IS_RGB(mpi
->imgfmt
) ){
726 assert(0);//should never happen
731 ( (mpi
->type
!= MP_IMGTYPE_STATIC
) && (mpi
->type
!= MP_IMGTYPE_TEMP
)) ||
732 (mpi
->flags
& MP_IMGFLAG_PLANAR
) ||
733 (mpi
->flags
& MP_IMGFLAG_YUV
)
737 //reading from video memory is horribly slow
738 if( !(mpi
->flags
& MP_IMGFLAG_READABLE
) && vo_directrendering
&&
739 (mode_capabilities
& CAP_LINEAR
) ){
741 //find free page and reserve it
742 page
=page_find_free();
744 PageStore
[page
].locks
=PAGE_BUSY
;
746 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
747 mpi
->stride
[0] = mode_stride
;
748 mpi
->planes
[0] = PageStore
[page
].vbase
+
749 y_pos
*mode_stride
+ (x_pos
*mpi
->bpp
)/8;
750 mpi
->priv
=(void *)page
;
752 printf("vo_svga: direct render allocated! page=%d\n",page
);