summaryrefslogtreecommitdiffstats
path: root/libvo/vo_vesa.c
blob: d1b531411fcd861dd6bb4db84e7bb66ed65b97c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/* 
 *  video_out_vesa.c
 *
 *	Copyright (C) Nick Kurshev <nickols_k@mail.ru> - Oct 2001
 *
 *  You can redistribute this file under terms and conditions
 *  GNU General Public licence v2.
 *  This file is partly based on vbetest.c from lrmi distributive.
 */

/*
  TODO:
  - DGA support (need volunteers who have corresponding hardware)
  - hw YUV support (need volunteers who have corresponding hardware)
  - double (triple) buffering (if it will really speedup playback).
  - refresh rate support (need additional info from mplayer)
*/
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <limits.h>

#include "config.h"
#include "video_out.h"
#include "video_out_internal.h"

#include "fastmemcpy.h"
#include "yuv2rgb.h"
#include "sub.h"
#include "linux/vbelib.h"
#include "bswap.h"

#include "../postproc/swscale.h"

LIBVO_EXTERN(vesa)
extern int verbose;

#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif


static vo_info_t vo_info = 
{
	"VESA VBE 2.0 video output",
	"vesa",
	"Nick Kurshev <nickols_k@mail.ru>",
        "Requires ROOT privileges"
};

/* driver data */

/*
   TODO: for linear framebuffer mode:
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   win.ptr = linear address of frame buffer;
   win.low = 0;
   win.high = vide_memory_size;
*/
struct win_frame
{
  uint8_t   *ptr;   /* pointer to window's frame memory */
  uint32_t   low;   /* lowest boundary of frame */
  uint32_t   high;  /* highest boundary of frame */
  uint8_t    idx;   /* indicates index of relocatable frame (A or B) */
};

static int vesa_zoom=0; /* software scaling */
static unsigned int scale_xinc=0;
static unsigned int scale_yinc=0;

static uint32_t image_width, image_height; /* source image dimension */
static int32_t x_offset,y_offset; /* to center image on screen */
static unsigned init_mode; /* mode before run of mplayer */
static void *init_state = NULL; /* state before run of mplayer */
static struct win_frame win; /* real-mode window to video memory */
static uint8_t *yuv_buffer = NULL; /* for yuv2rgb and sw_scaling */
static unsigned video_mode; /* selected video mode for playback */
static struct VesaModeInfoBlock video_mode_info;
static int flip_trigger = 0;
static void (*draw_alpha_fnc)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);

#define MOVIE_MODE (MODE_ATTR_COLOR | MODE_ATTR_GRAPHICS)
#define FRAME_MODE (MODE_WIN_RELOCATABLE | MODE_WIN_READABLE | MODE_WIN_WRITEABLE)
static char * vbeErrToStr(int err)
{
  char *retval;
  static char sbuff[80];
  if((err & VBE_VESA_ERROR_MASK) == VBE_VESA_ERROR_MASK)
  {
    sprintf(sbuff,"VESA failed = 0x4f%x",(err & VBE_VESA_ERRCODE_MASK)>>8);
    retval = sbuff;
  }
  else
  switch(err)
  { 
    case VBE_OK: retval = "No error"; break;
    case VBE_VM86_FAIL: retval = "vm86() syscall failed"; break;
    case VBE_OUT_OF_DOS_MEM: retval = "Out of DOS memory"; break;
    case VBE_OUT_OF_MEM: retval = "Out of memory"; break;
    case VBE_BROKEN_BIOS: retval = "Broken BIOS or DOS TSR"; break;
    default: sprintf(sbuff,"Uknown error: %i",err); retval=sbuff; break;
  }
  return retval;
}

#define PRINT_VBE_ERR(name,err) { printf("vo_vesa: %s returns: %s\n",name,vbeErrToStr(err)); fflush(stdout); }

static void vesa_term( void )
{
  int err;
  if((err=vbeRestoreState(init_state)) != VBE_OK) PRINT_VBE_ERR("vbeRestoreState",err);
  if((err=vbeSetMode(init_mode,NULL)) != VBE_OK) PRINT_VBE_ERR("vbeSetMode",err);
  if(yuv_buffer) free(yuv_buffer);
  vbeDestroy();
}

#define VALID_WIN_FRAME(offset) (offset >= win.low && offset < win.high)
#define VIDEO_PTR(offset) (win.ptr + offset - win.low)

static inline void __vbeSwitchBank(unsigned long offset)
{
  unsigned long gran;
  unsigned new_offset;
  int err;
  gran = video_mode_info.WinGranularity*1024;
  new_offset = offset / gran;
  if((err=vbeSetWindow(win.idx,new_offset)) != VBE_OK)
  {
    PRINT_VBE_ERR("vbeSetWindow",err);
    printf("vo_vesa: Fatal error occured! Can't continue\n");
    vesa_term();
    exit(-1);
  }
  win.low = new_offset * gran;
  win.high = win.low + video_mode_info.WinSize*1024;
}

static void __vbeSetPixel(int x, int y, int r, int g, int b)
{
	int x_res = video_mode_info.XResolution;
	int y_res = video_mode_info.YResolution;
	int shift_r = video_mode_info.RedFieldPosition;
	int shift_g = video_mode_info.GreenFieldPosition;
	int shift_b = video_mode_info.BlueFieldPosition;
	int pixel_size = (video_mode_info.BitsPerPixel+7)/8;
	int bpl = video_mode_info.BytesPerScanLine;
	int color, offset;

	if (x < 0 || x >= x_res || y < 0 || y >= y_res)	return;
	r >>= 8 - video_mode_info.RedMaskSize;
	g >>= 8 - video_mode_info.GreenMaskSize;
	b >>= 8 - video_mode_info.BlueMaskSize;
	color = (r << shift_r) | (g << shift_g) | (b << shift_b);
	offset = y * bpl + (x * pixel_size);
        if(!VALID_WIN_FRAME(offset)) __vbeSwitchBank(offset);
	memcpy(VIDEO_PTR(offset), &color, pixel_size);
}

/*
  Copies line of frame to video memory. Data should be in the same format as video
  memory.
*/
static void __vbeCopyBlock(unsigned long offset,uint8_t *image,unsigned long size)
{
   unsigned long delta,src_idx = 0;
   while(size)
   {
     if(!VALID_WIN_FRAME(offset)) __vbeSwitchBank(offset);
     delta = min(size,win.high - offset);
     memcpy(VIDEO_PTR(offset),&image[src_idx],delta);
     src_idx += delta;
     offset += delta;
     size -= delta;
   }
}

static void __vbeCopyBlockSwap(unsigned long offset,uint8_t *image,unsigned long size)
{
   unsigned byte_len;
   uint8_t ch;
   while(size)
   {
       switch(video_mode_info.BitsPerPixel)
       {
	case 8: byte_len = 1; break;
	default:
	case 15:
		printf("vo_vesa: Can't swap non byte aligned data\n");
		vesa_term();
		exit(-1);
	case 16: *(image + offset) = ByteSwap16(*(image + offset));
		 byte_len = 2; break;
	case 24: ch = *(image+offset);
		 *(image+offset) = *(image+offset+3);
                 *(image+offset+3) = ch;
		 byte_len = 3; break;
	case 32: *(image + offset) = ByteSwap32(*(image + offset));
		 byte_len = 4; break;
       }
       __vbeCopyBlock(offset,image,byte_len);
       size   -= byte_len;
       image  += byte_len;
       offset += byte_len;
   }
}

/*
  Copies frame to video memory. Data should be in the same format as video
  memory.
*/
static void __vbeCopyData(uint8_t *image)
{
   unsigned long i,j,image_offset,offset;
   unsigned pixel_size,image_line_size,screen_line_size,x_shift;
   pixel_size = (video_mode_info.BitsPerPixel+7)/8;
   screen_line_size = video_mode_info.XResolution*pixel_size;
   image_line_size = image_width*pixel_size;
   if(image_width == video_mode_info.XResolution)
   {
     /* Special case for zooming */
     __vbeCopyBlock(y_offset*screen_line_size,image,image_line_size*image_height);
   }
   else
   {
     x_shift = x_offset*pixel_size;
     for(j=0,i=y_offset;j<image_height;i++,j++)
     {
       offset = i*screen_line_size+x_shift;
       image_offset = j*image_line_size;
       __vbeCopyBlock(offset,&image[image_offset],image_line_size);
     }
   }
}

/* is called for yuv only */
static uint32_t draw_slice(uint8_t *image[], int stride[], int w,int h,int x,int y)
{
    if(vesa_zoom)
    {
	 SwScale_YV12slice_brg24(image,stride,y,h,
	                     yuv_buffer,
			     image_width*((video_mode_info.BitsPerPixel+7)/8),
			     image_width, video_mode_info.BitsPerPixel,
	    		     scale_xinc, scale_yinc);
    }
    else
    {
	uint8_t *yuv_slice;
	yuv_slice=yuv_buffer+(image_width*y+x)*((video_mode_info.BitsPerPixel+7)/8);
	yuv2rgb(yuv_slice, image[0], image[1], image[2], w, h,
		image_width * ((video_mode_info.BitsPerPixel+7)/8),
		stride[0], stride[1]);
    }
    flip_trigger = 1;
    return 0;
}

static void draw_alpha_32(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
   vo_draw_alpha_rgb32(w,h,src,srca,stride,yuv_buffer+4*(y0*image_width+x0),4*image_width);
}

static void draw_alpha_24(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
   vo_draw_alpha_rgb24(w,h,src,srca,stride,yuv_buffer+3*(y0*image_width+x0),3*image_width);
}

static void draw_alpha_16(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
   vo_draw_alpha_rgb16(w,h,src,srca,stride,yuv_buffer+2*(y0*image_width+x0),2*image_width);
}

static void draw_alpha_15(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
   vo_draw_alpha_rgb15(w,h,src,srca,stride,yuv_buffer+2*(y0*image_width+x0),2*image_width);
}

static void draw_alpha_null(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
}


static void draw_osd(void)
{
 if(yuv_buffer) vo_draw_text(image_width,image_height,draw_alpha_fnc);
}

static void flip_page(void)
{
  if(flip_trigger) { __vbeCopyData(yuv_buffer); flip_trigger = 0; }
}

/* is called for rgb only */
static uint32_t draw_frame(uint8_t *src[])
{
	__vbeCopyData(src[0]);
	return 0;
}

static uint32_t query_format(uint32_t format)
{
  uint32_t retval;
	switch(format)
	{
		case IMGFMT_YV12:
#if 0 /* Should be tested better */
		case IMGFMT_I420:
		case IMGFMT_IYUV:
#endif
		case IMGFMT_RGB8:
		case IMGFMT_RGB15:
		case IMGFMT_RGB16:
		case IMGFMT_RGB24:
		case IMGFMT_RGB32:
		case IMGFMT_BGR8:
		case IMGFMT_BGR15:
		case IMGFMT_BGR16:
		case IMGFMT_BGR24:
		case IMGFMT_BGR32:
				retval = 1; break;
		default:
			if(verbose)
				printf("vo_vesa: unknown format: %x = %s\n",format,vo_format_name(format));
			retval = 0;
	}
	return retval;
}

static void vesa_aspect(uint32_t width,uint32_t height,  
		    	uint32_t xres,uint32_t yres,
			uint32_t *image_width,uint32_t *image_height)
{
  float aspect_factor;
  aspect_factor = (float)width / height;
  *image_width = xres;
  *image_height = xres /aspect_factor;
  if(verbose) printf("vo_vesa: aspect factor = %f(%ux%u) *image=%ux%u screen=%ux%u\n",aspect_factor,width,height,*image_width,*image_height,xres,yres);
  if((*image_height) > yres)
  {
    *image_height = yres;
    *image_width = yres * aspect_factor;
    if(verbose) printf("vo_vesa: Y > X therefore *image=%ux%u\n",*image_width,*image_height);
  }
}

static char *model2str(unsigned char type)
{
  char *retval;
  switch(type)
  {
    case memText: retval = "Text"; break;
    case memCGA:  retval="CGA"; break;
    case memHercules: retval="Hercules"; break;
    case memPL: retval="Planar"; break;
    case memPK: retval="Packed pixel"; break;
    case mem256: retval="256"; break;
    case memRGB: retval="Direct color RGB"; break;
    case memYUV: retval="Direct color YUV"; break;
    default: retval="Unknown"; break;
  }
  return retval;
}

/* fullscreen:
 * bit 0 (0x01) means fullscreen (-fs)
 * bit 1 (0x02) means mode switching (-vm)
 * bit 2 (0x04) enables software scaling (-zoom)
 * bit 3 (0x08) enables flipping (-flip) (NK: and for what?)
 */
static uint32_t
init(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
{
  struct VbeInfoBlock vib;
  struct VesaModeInfoBlock vmib;
  size_t i,num_modes;
  uint32_t w,h;
  unsigned short *mode_ptr,win_seg;
  unsigned bpp,best_x = UINT_MAX,best_y=UINT_MAX,best_mode_idx = UINT_MAX;
  int err,fs_mode,yuv_fmt;
	image_width = width;
	image_height = height;
	fs_mode = 0;
	if(flags & 0x8)
	{
	  printf("vo_vesa: switch -flip is not supported\n");
	}
	if(flags & 0x04) vesa_zoom = 1;
	if(flags & 0x01)
	{
	  if(vesa_zoom) vesa_zoom = 2;
	  else          fs_mode = 1;
	} 
	if((err=vbeInit()) != VBE_OK) { PRINT_VBE_ERR("vbeInit",err); return -1; }
	memcpy(vib.VESASignature,"VBE2",4);
	if((err=vbeGetControllerInfo(&vib)) != VBE_OK)
	{
	  PRINT_VBE_ERR("vbeGetControllerInfo",err);
	  printf("vo_vesa: possible reason: No VBE2 BIOS found\n");
	  return -1;
	}
	/* Print general info here */
	printf("vo_vesa: Found VESA VBE BIOS Version %x.%x Revision: %x\n",
		(int)(vib.VESAVersion >> 8) & 0xff,
		(int)(vib.VESAVersion & 0xff),
		(int)(vib.OemSoftwareRev & 0xffff));
	printf("vo_vesa: Video memory: %u Kb\n",vib.TotalMemory*64);
	printf("vo_vesa: VESA Capabilities: %s %s %s %s %s\n"
		,vib.Capabilities & VBE_DAC_8BIT ? "8-bit DAC," : "6-bit DAC,"
		,vib.Capabilities & VBE_NONVGA_CRTC ? "non-VGA CRTC,":"VGA CRTC,"
		,vib.Capabilities & VBE_SNOWED_RAMDAC ? "snowed RAMDAC,":"normal RAMDAC,"
		,vib.Capabilities & VBE_STEREOSCOPIC ? "stereoscopic,":"no stereoscopic,"
		,vib.Capabilities & VBE_STEREO_EVC ? "Stereo EVC":"no stereo");
	printf("vo_vesa: !!! Below will be printed OEM info. !!!\n");
	printf("vo_vesa: You should watch 5 OEM related lines below else you've broken vm86\n");
	printf("vo_vesa: OEM info: %s\n",vib.OemStringPtr);
	printf("vo_vesa: OEM Revision: %x\n",vib.OemSoftwareRev);
	printf("vo_vesa: OEM vendor: %s\n",vib.OemVendorNamePtr);
	printf("vo_vesa: OEM Product Name: %s\n",vib.OemProductNamePtr);
	printf("vo_vesa: OEM Product Rev: %s\n",vib.OemProductRevPtr);
	printf("vo_vesa: Hint: To get workable TV-Out you should have plugged tv-connector in\n"
	       "vo_vesa: before booting PC since VESA BIOS initializes itself only during POST\n");
	/* Find best mode here */
	num_modes = 0;
	mode_ptr = vib.VideoModePtr;
	while(*mode_ptr++ != 0xffff) num_modes++;
	yuv_fmt = format == IMGFMT_YV12 || format == IMGFMT_I420 || format == IMGFMT_IYUV;
	if(vo_dbpp)
	{
	  bpp = vo_dbpp;
	  if(yuv_fmt)	yuv2rgb_init(bpp, MODE_RGB);
	}
	else
	switch(format)
	{
		case IMGFMT_BGR8:
		case IMGFMT_RGB8:  bpp = 8; break;
		case IMGFMT_BGR15:
                case IMGFMT_RGB15: bpp = 15; break;
		case IMGFMT_YV12:
		case IMGFMT_I420:
		case IMGFMT_IYUV: bpp=16;
			yuv2rgb_init(bpp, MODE_RGB);
		default:
		case IMGFMT_BGR16:
		case IMGFMT_RGB16: bpp = 16; break;
		case IMGFMT_BGR24:
		case IMGFMT_RGB24: bpp = 24; break;
		case IMGFMT_BGR32:
		case IMGFMT_RGB32: bpp = 32; break;
	}
	switch(bpp)
	{
	  case 15: draw_alpha_fnc = draw_alpha_15; break;
	  case 16: draw_alpha_fnc = draw_alpha_16; break;
	  case 24: draw_alpha_fnc = draw_alpha_24; break;
	  case 32: draw_alpha_fnc = draw_alpha_32; break;
	  default: draw_alpha_fnc = draw_alpha_null; break;
	}
	if(verbose)
	{
	  printf("vo_vesa: Requested mode: %ux%u@%u (%s)\n",width,height,bpp,vo_format_name(format));
	  printf("vo_vesa: Total modes found: %u\n",num_modes);
	  mode_ptr = vib.VideoModePtr;
	  printf("vo_vesa: Mode list:");
	  for(i = 0;i < num_modes;i++)
	  {
	    printf(" %04X",mode_ptr[i]);
	  }
	  printf("\nvo_vesa: Modes in detail:\n");
	}
	mode_ptr = vib.VideoModePtr;
	if(vesa_zoom)
	{
	    image_width = d_width;
	    image_height= d_height;
	}
	if(vo_screenwidth) w = vo_screenwidth;
	else w = max(image_width,width);
	if(vo_screenheight) h = vo_screenheight;
	else h = max(image_height,height);
        for(i=0;i < num_modes;i++)
	{
		if((err=vbeGetModeInfo(mode_ptr[i],&vmib)) != VBE_OK)
		{
			PRINT_VBE_ERR("vbeGetModeInfo",err);
			return -1;
		}
		if(vmib.XResolution >= w &&
		   vmib.YResolution >= h &&
		   (vmib.ModeAttributes & MOVIE_MODE) == MOVIE_MODE &&
		   vmib.BitsPerPixel == bpp &&
		   vmib.MemoryModel == memRGB)
		   {
			if(vmib.XResolution <= best_x &&
			   vmib.YResolution <= best_y)
			   {
				best_x = vmib.XResolution;
				best_y = vmib.YResolution;
				best_mode_idx = i;
			   }
		   }
		if(verbose)
		{
		  printf("vo_vesa: Mode (%03u): mode=%04X %ux%u@%u attr=%04X\n"
			 "vo_vesa:             #planes=%u model=%u(%s) #pages=%u\n"
			 "vo_vesa:             winA=%X(attr=%u) winB=%X(attr=%u) winSize=%u winGran=%u\n"
			 "vo_vesa:             direct_color=%u DGA_phys_addr=%08X\n"
			 ,i,mode_ptr[i],vmib.XResolution,vmib.YResolution,vmib.BitsPerPixel,vmib.ModeAttributes
			 ,vmib.NumberOfPlanes,vmib.MemoryModel,model2str(vmib.MemoryModel),vmib.NumberOfImagePages
			 ,vmib.WinASegment,vmib.WinAAttributes,vmib.WinBSegment,vmib.WinBAttributes,vmib.WinSize,vmib.WinGranularity
			 ,vmib.DirectColorModeInfo,vmib.PhysBasePtr);
		  if(vmib.MemoryModel == 6 || vmib.MemoryModel == 7)
			printf("vo_vesa:             direct_color_info = %u:%u:%u:%u\n"
				,vmib.RedMaskSize,vmib.GreenMaskSize,vmib.BlueMaskSize,vmib.RsvdMaskSize);
		  fflush(stdout);
		}
	}
	if(best_mode_idx != UINT_MAX)
	{
		video_mode = vib.VideoModePtr[best_mode_idx];
		fflush(stdout);
		if((err=vbeGetMode(&init_mode)) != VBE_OK)
		{
			PRINT_VBE_ERR("vbeGetMode",err);
			return -1;
		}
		if(verbose) printf("vo_vesa: Initial video mode: %x\n",init_mode);
		if((err=vbeGetModeInfo(video_mode,&video_mode_info)) != VBE_OK)
		{
			PRINT_VBE_ERR("vbeGetModeInfo",err);
			return -1;
		}
		printf("vo_vesa: Using VESA mode (%u) = %x [%ux%u@%u]\n"
			,best_mode_idx,video_mode,video_mode_info.XResolution
			,video_mode_info.YResolution,video_mode_info.BitsPerPixel);
		if( vesa_zoom || fs_mode )
		{
		  if( format==IMGFMT_YV12 )
		  {
		      /* software scale */
		      if(vesa_zoom > 1)
		      vesa_aspect(image_width,image_height,  
		    		  video_mode_info.XResolution,video_mode_info.YResolution,
				  &image_width,&image_height);
		      else
		      if(fs_mode)
		      {
			image_width = video_mode_info.XResolution;
			image_height = video_mode_info.YResolution;
			vesa_zoom = 1;
		      }
		      scale_xinc=(width << 16) / image_width - 2;  /* needed for proper rounding */
	    	      scale_yinc=(height << 16) / image_height + 2;
		      SwScale_Init();
		      if(verbose) printf("vo_vesa: Using SCALE\n");
		  }      
    		  else
		  {
		      printf("vo_vesa: Can't apply zooming to non YV12 formats\n");
		      return -1;
		  }
		}
		if((video_mode_info.WinAAttributes & FRAME_MODE) == FRAME_MODE)
		   win.idx = 0; /* frame A */
		else
		if((video_mode_info.WinBAttributes & FRAME_MODE) == FRAME_MODE)
		   win.idx = 1; /* frame B */
		else { printf("vo_vesa: Can't find usable frame of window\n"); return -1; }
		if(!(win_seg = win.idx == 0 ? video_mode_info.WinASegment:video_mode_info.WinBSegment))
		{
		  printf("vo_vesa: Can't find valid window address\n");
		  if(video_mode_info.ModeAttributes & MODE_ATTR_LINEAR)
		  	printf("vo_vesa: Your BIOS supports DGA access which is not implemented for now\n");
		  return -1;
		}
		win.ptr = PhysToVirtSO(win_seg,0);
		win.low = 0L;
		win.high= video_mode_info.WinSize*1024;
		if(video_mode_info.XResolution > image_width)
		    x_offset = (video_mode_info.XResolution - image_width) / 2;
		else x_offset = 0;
		if(video_mode_info.YResolution > image_height)
		    y_offset = (video_mode_info.YResolution - image_height) / 2;
		else y_offset = 0;
		if(verbose)
		  printf("vo_vesa: image: %ux%u screen = %ux%u x_offset = %u y_offset = %u\n"
			,image_width,image_height
			,video_mode_info.XResolution,video_mode_info.YResolution
			,x_offset,y_offset);
		if(yuv_fmt)
		  if(!(yuv_buffer = malloc(image_width*image_height*bpp)))
		  {
		    printf("vo_vesa: Can't allocate temporary buffer\n");
		    return -1;
		  }
		if((err=vbeSaveState(&init_state)) != VBE_OK)
		{
			PRINT_VBE_ERR("vbeSaveState",err);
			return -1;
		}
		if((err=vbeSetMode(video_mode,NULL)) != VBE_OK)
		{
			PRINT_VBE_ERR("vbeSetMode",err);
			return -1;
		}
		/* Now we are in video mode!!!*/
		/* Below 'return -1' is impossible */
		if(verbose)
		{
		  printf("vo_vesa: Graphics mode was activated\n");
		  fflush(stdout);
		}
	}
	else
	{
	  printf("vo_vesa: Can't find mode for: %ux%u@%u\n",width,height,bpp);
	  return -1;
	}
	if(verbose)
	{
	  printf("vo_vesa: VESA initialization complete\n");
	  fflush(stdout);
	}
	if(verbose)
	{
		int x_res = video_mode_info.XResolution;
		int y_res = video_mode_info.YResolution;
		int x, y;

		for (y = 0; y < y_res; ++y)
			{
			for (x = 0; x < x_res; ++x)
				{
				int r, g, b;
				if ((x & 16) ^ (y & 16))
					{
					r = x * 255 / x_res;
					g = y * 255 / y_res;
					b = 255 - x * 255 / x_res;
					}
				else
					{
					r = 255 - x * 255 / x_res;
					g = y * 255 / y_res;
					b = 255 - y * 255 / y_res;
					}

				__vbeSetPixel(x, y, r, g, b);
				}
			}
	}
	return 0;
}

static const vo_info_t*
get_info(void)
{
	return &vo_info;
}

static void
uninit(void)
{
	vesa_term();
}


static void check_events(void)
{
/* Nothing to do */
}