1 /******************************************************************************
2 * vo_directx.c: Directx v2 or later DirectDraw interface for MPlayer
3 * Copyright (c) 2002 - 2004 Sascha Sommer <saschasommer@freenet.de>.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 *****************************************************************************/
28 #include "video_out.h"
29 #include "video_out_internal.h"
30 #include "fastmemcpy.h"
31 #include "input/input.h"
32 #include "osdep/keycodes.h"
33 #include "input/mouse.h"
38 #ifndef WM_XBUTTONDOWN
39 # define WM_XBUTTONDOWN 0x020B
40 # define WM_XBUTTONUP 0x020C
41 # define WM_XBUTTONDBLCLK 0x020D
44 static LPDIRECTDRAWCOLORCONTROL g_cc
= NULL
; //color control interface
45 static LPDIRECTDRAW7 g_lpdd
= NULL
; //DirectDraw Object
46 static LPDIRECTDRAWSURFACE7 g_lpddsPrimary
= NULL
; //Primary Surface: viewport through the Desktop
47 static LPDIRECTDRAWSURFACE7 g_lpddsOverlay
= NULL
; //Overlay Surface
48 static LPDIRECTDRAWSURFACE7 g_lpddsBack
= NULL
; //Back surface
49 static LPDIRECTDRAWCLIPPER g_lpddclipper
; //clipper object, can only be used without overlay
50 static DDSURFACEDESC2 ddsdsf
; //surface descripiton needed for locking
51 static HINSTANCE hddraw_dll
; //handle to ddraw.dll
52 static RECT rd
; //rect of our stretched image
53 static RECT rs
; //rect of our source image
54 static HWND hWnd
=NULL
; //handle to the window
55 static HWND hWndFS
=NULL
; //fullscreen window
56 static uint32_t image_width
, image_height
; //image width and height
57 static uint32_t d_image_width
, d_image_height
; //image width and height zoomed
58 static uint8_t *image
=NULL
; //image data
59 static uint32_t image_format
=0; //image format
60 static uint32_t primary_image_format
;
61 static uint32_t vm_height
=0;
62 static uint32_t vm_width
=0;
63 static uint32_t vm_bpp
=0;
64 static uint32_t dstride
; //surface stride
65 static uint32_t nooverlay
= 0; //NonOverlay mode
66 static DWORD destcolorkey
; //colorkey for our surface
67 static COLORREF windowcolor
= RGB(0,0,16); //windowcolor == colorkey
68 static int adapter_count
=0;
69 static GUID selected_guid
;
70 static GUID
*selected_guid_ptr
= NULL
;
71 static RECT monitor_rect
; //monitor coordinates
72 static float window_aspect
;
73 static BOOL (WINAPI
* myGetMonitorInfo
)(HMONITOR
, LPMONITORINFO
) = NULL
;
75 extern void mplayer_put_key(int code
); //let mplayer handel the keyevents
76 extern void vo_draw_text(int dxs
,int dys
,void (*draw_alpha
)(int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
));
77 extern int vo_doublebuffering
; //tribblebuffering
79 extern int vo_directrendering
;
81 extern int vo_rootwin
;
83 extern int vo_colorkey
;
86 /*****************************************************************************
88 * Defining them here allows us to get rid of the dxguid library during
90 *****************************************************************************/
91 static const GUID IID_IDirectDraw7
=
93 0x15e65ec0,0x3b9c,0x11d2,{0xb9,0x2f,0x00,0x60,0x97,0x97,0xea,0x5b}
96 static const GUID IID_IDirectDrawColorControl
=
98 0x4b9f0ee0,0x0d7e,0x11d0,{0x9b,0x06,0x00,0xa0,0xc9,0x03,0xa3,0xb8}
102 typedef struct directx_fourcc_caps
104 char* img_format_name
; //human readable name
105 uint32_t img_format
; //as MPlayer image format
106 uint32_t drv_caps
; //what hw supports with this format
107 DDPIXELFORMAT g_ddpfOverlay
; //as Directx Sourface description
108 } directx_fourcc_caps
;
111 static directx_fourcc_caps g_ddpf
[] =
113 {"YV12 ",IMGFMT_YV12
,0,{sizeof(DDPIXELFORMAT
), DDPF_FOURCC
,MAKEFOURCC('Y','V','1','2'),0,0,0,0,0}},
114 {"I420 ",IMGFMT_I420
,0,{sizeof(DDPIXELFORMAT
), DDPF_FOURCC
,MAKEFOURCC('I','4','2','0'),0,0,0,0,0}}, //yv12 with swapped uv
115 {"IYUV ",IMGFMT_IYUV
,0,{sizeof(DDPIXELFORMAT
), DDPF_FOURCC
,MAKEFOURCC('I','Y','U','V'),0,0,0,0,0}}, //same as i420
116 {"YVU9 ",IMGFMT_YVU9
,0,{sizeof(DDPIXELFORMAT
), DDPF_FOURCC
,MAKEFOURCC('Y','V','U','9'),0,0,0,0,0}},
117 {"YUY2 ",IMGFMT_YUY2
,0,{sizeof(DDPIXELFORMAT
), DDPF_FOURCC
,MAKEFOURCC('Y','U','Y','2'),0,0,0,0,0}},
118 {"UYVY ",IMGFMT_UYVY
,0,{sizeof(DDPIXELFORMAT
), DDPF_FOURCC
,MAKEFOURCC('U','Y','V','Y'),0,0,0,0,0}},
119 {"RGB15",IMGFMT_RGB15
,0,{sizeof(DDPIXELFORMAT
), DDPF_RGB
, 0, 16, 0x0000001F, 0x000003E0, 0x00007C00, 0}}, //RGB 5:5:5
120 {"BGR15",IMGFMT_BGR15
,0,{sizeof(DDPIXELFORMAT
), DDPF_RGB
, 0, 16, 0x00007C00, 0x000003E0, 0x0000001F, 0}},
121 {"RGB16",IMGFMT_RGB16
,0,{sizeof(DDPIXELFORMAT
), DDPF_RGB
, 0, 16, 0x0000001F, 0x000007E0, 0x0000F800, 0}}, //RGB 5:6:5
122 {"BGR16",IMGFMT_BGR16
,0,{sizeof(DDPIXELFORMAT
), DDPF_RGB
, 0, 16, 0x0000F800, 0x000007E0, 0x0000001F, 0}},
123 {"RGB24",IMGFMT_RGB24
,0,{sizeof(DDPIXELFORMAT
), DDPF_RGB
, 0, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0}},
124 {"BGR24",IMGFMT_BGR24
,0,{sizeof(DDPIXELFORMAT
), DDPF_RGB
, 0, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0}},
125 {"RGB32",IMGFMT_RGB32
,0,{sizeof(DDPIXELFORMAT
), DDPF_RGB
, 0, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0}},
126 {"BGR32",IMGFMT_BGR32
,0,{sizeof(DDPIXELFORMAT
), DDPF_RGB
, 0, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0}}
128 #define NUM_FORMATS (sizeof(g_ddpf) / sizeof(g_ddpf[0]))
130 static vo_info_t info
=
132 "Directx DDraw YUV/RGB/BGR renderer",
134 "Sascha Sommer <saschasommer@freenet.de>",
138 LIBVO_EXTERN(directx
)
140 static void draw_alpha(int x0
, int y0
, int w
, int h
, unsigned char *src
,
141 unsigned char *srca
, int stride
)
143 switch(image_format
) {
148 vo_draw_alpha_yv12(w
,h
,src
,srca
,stride
,((uint8_t *) image
) + dstride
*y0
+ x0
,dstride
);
151 vo_draw_alpha_yuy2(w
,h
,src
,srca
,stride
,((uint8_t *) image
)+ dstride
*y0
+ 2*x0
,dstride
);
154 vo_draw_alpha_yuy2(w
,h
,src
,srca
,stride
,((uint8_t *) image
) + dstride
*y0
+ 2*x0
+ 1,dstride
);
158 vo_draw_alpha_rgb15(w
,h
,src
,srca
,stride
,((uint8_t *) image
)+dstride
*y0
+2*x0
,dstride
);
162 vo_draw_alpha_rgb16(w
,h
,src
,srca
,stride
,((uint8_t *) image
)+dstride
*y0
+2*x0
,dstride
);
166 vo_draw_alpha_rgb24(w
,h
,src
,srca
,stride
,((uint8_t *) image
)+dstride
*y0
+4*x0
,dstride
);
170 vo_draw_alpha_rgb32(w
,h
,src
,srca
,stride
,((uint8_t *) image
)+dstride
*y0
+4*x0
,dstride
);
175 static void draw_osd(void)
177 vo_draw_text(image_width
,image_height
,draw_alpha
);
181 query_format(uint32_t format
)
184 while ( i
< NUM_FORMATS
)
186 if (g_ddpf
[i
].img_format
== format
)
187 return g_ddpf
[i
].drv_caps
;
193 static uint32_t Directx_CreatePrimarySurface()
197 if(g_lpddsPrimary
)g_lpddsPrimary
->lpVtbl
->Release(g_lpddsPrimary
);
200 if(vidmode
)g_lpdd
->lpVtbl
->SetDisplayMode(g_lpdd
,vm_width
,vm_height
,vm_bpp
,vo_refresh_rate
,0);
201 ZeroMemory(&ddsd
, sizeof(ddsd
));
202 ddsd
.dwSize
= sizeof(ddsd
);
203 //set flags and create a primary surface.
204 ddsd
.dwFlags
= DDSD_CAPS
;
205 ddsd
.ddsCaps
.dwCaps
= DDSCAPS_PRIMARYSURFACE
;
206 if(g_lpdd
->lpVtbl
->CreateSurface(g_lpdd
,&ddsd
, &g_lpddsPrimary
, NULL
)== DD_OK
)
207 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>primary surface created\n");
210 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>could not create primary surface\n");
216 static uint32_t Directx_CreateOverlay(uint32_t imgfmt
)
219 DDSURFACEDESC2 ddsdOverlay
;
221 while ( i
< NUM_FORMATS
+1 && imgfmt
!= g_ddpf
[i
].img_format
)
225 if (!g_lpdd
|| !g_lpddsPrimary
)
228 if (g_lpddsOverlay
)g_lpddsOverlay
->lpVtbl
->Release(g_lpddsOverlay
);
229 if (g_lpddsBack
)g_lpddsBack
->lpVtbl
->Release(g_lpddsBack
);
230 g_lpddsOverlay
= NULL
;
233 ZeroMemory(&ddsdOverlay
, sizeof(ddsdOverlay
));
234 ddsdOverlay
.dwSize
= sizeof(ddsdOverlay
);
235 ddsdOverlay
.ddsCaps
.dwCaps
=DDSCAPS_OVERLAY
| DDSCAPS_FLIP
| DDSCAPS_COMPLEX
| DDSCAPS_VIDEOMEMORY
;
236 ddsdOverlay
.dwFlags
= DDSD_CAPS
|DDSD_HEIGHT
|DDSD_WIDTH
|DDSD_BACKBUFFERCOUNT
| DDSD_PIXELFORMAT
;
237 ddsdOverlay
.dwWidth
=image_width
;
238 ddsdOverlay
.dwHeight
=image_height
;
239 ddsdOverlay
.dwBackBufferCount
=2;
240 ddsdOverlay
.ddpfPixelFormat
=g_ddpf
[i
].g_ddpfOverlay
;
241 if(vo_doublebuffering
) //tribblebuffering
243 if (g_lpdd
->lpVtbl
->CreateSurface(g_lpdd
,&ddsdOverlay
, &g_lpddsOverlay
, NULL
)== DD_OK
)
245 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>overlay with format %s created\n",g_ddpf
[i
].img_format_name
);
246 //get the surface directly attached to the primary (the back buffer)
247 ddsdOverlay
.ddsCaps
.dwCaps
= DDSCAPS_BACKBUFFER
;
248 if(g_lpddsOverlay
->lpVtbl
->GetAttachedSurface(g_lpddsOverlay
,&ddsdOverlay
.ddsCaps
, &g_lpddsBack
) != DD_OK
)
250 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>can't get attached surface\n");
255 vo_doublebuffering
=0; //disable tribblebuffering
256 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><WARN>cannot create tribblebuffer overlay with format %s\n",g_ddpf
[i
].img_format_name
);
259 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>using singlebuffer overlay\n");
260 ddsdOverlay
.dwBackBufferCount
=0;
261 ddsdOverlay
.ddsCaps
.dwCaps
=DDSCAPS_OVERLAY
| DDSCAPS_VIDEOMEMORY
;
262 ddsdOverlay
.dwFlags
= DDSD_CAPS
|DDSD_HEIGHT
|DDSD_WIDTH
|DDSD_PIXELFORMAT
;
263 ddsdOverlay
.ddpfPixelFormat
=g_ddpf
[i
].g_ddpfOverlay
;
264 // try to create the overlay surface
265 ddrval
= g_lpdd
->lpVtbl
->CreateSurface(g_lpdd
,&ddsdOverlay
, &g_lpddsOverlay
, NULL
);
268 if(ddrval
== DDERR_INVALIDPIXELFORMAT
)mp_msg(MSGT_VO
,MSGL_V
,"<vo_directx><ERROR> invalid pixelformat: %s\n",g_ddpf
[i
].img_format_name
);
269 else mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>");
272 case DDERR_INCOMPATIBLEPRIMARY
:
273 {mp_msg(MSGT_VO
, MSGL_ERR
,"incompatible primary surface\n");break;}
274 case DDERR_INVALIDCAPS
:
275 {mp_msg(MSGT_VO
, MSGL_ERR
,"invalid caps\n");break;}
276 case DDERR_INVALIDOBJECT
:
277 {mp_msg(MSGT_VO
, MSGL_ERR
,"invalid object\n");break;}
278 case DDERR_INVALIDPARAMS
:
279 {mp_msg(MSGT_VO
, MSGL_ERR
,"invalid parameters\n");break;}
280 case DDERR_NODIRECTDRAWHW
:
281 {mp_msg(MSGT_VO
, MSGL_ERR
,"no directdraw hardware\n");break;}
282 case DDERR_NOEMULATION
:
283 {mp_msg(MSGT_VO
, MSGL_ERR
,"can't emulate\n");break;}
285 {mp_msg(MSGT_VO
, MSGL_ERR
,"hardware can't do flip\n");break;}
286 case DDERR_NOOVERLAYHW
:
287 {mp_msg(MSGT_VO
, MSGL_ERR
,"hardware can't do overlay\n");break;}
288 case DDERR_OUTOFMEMORY
:
289 {mp_msg(MSGT_VO
, MSGL_ERR
,"not enough system memory\n");break;}
290 case DDERR_UNSUPPORTEDMODE
:
291 {mp_msg(MSGT_VO
, MSGL_ERR
,"unsupported mode\n");break;}
292 case DDERR_OUTOFVIDEOMEMORY
:
293 {mp_msg(MSGT_VO
, MSGL_ERR
,"not enough video memory\n");break;}
295 mp_msg(MSGT_VO
, MSGL_ERR
,"create surface failed with 0x%x\n",ddrval
);
299 g_lpddsBack
= g_lpddsOverlay
;
303 static uint32_t Directx_CreateBackpuffer()
307 if (g_lpddsBack
)g_lpddsBack
->lpVtbl
->Release(g_lpddsBack
);
309 ZeroMemory(&ddsd
, sizeof(ddsd
));
310 ddsd
.dwSize
= sizeof(ddsd
);
311 ddsd
.ddsCaps
.dwCaps
= DDSCAPS_OFFSCREENPLAIN
| DDSCAPS_SYSTEMMEMORY
;
312 ddsd
.dwFlags
= DDSD_CAPS
| DDSD_WIDTH
| DDSD_HEIGHT
;
313 ddsd
.dwWidth
=image_width
;
314 ddsd
.dwHeight
=image_height
;
315 if(g_lpdd
->lpVtbl
->CreateSurface( g_lpdd
, &ddsd
, &g_lpddsBack
, 0 ) != DD_OK
)
317 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>can't create backpuffer\n");
320 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>backbuffer created\n");
324 static void uninit(void)
328 g_cc
->lpVtbl
->Release(g_cc
);
331 if (g_lpddclipper
!= NULL
) g_lpddclipper
->lpVtbl
->Release(g_lpddclipper
);
332 g_lpddclipper
= NULL
;
333 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>clipper released\n");
334 if (g_lpddsBack
!= NULL
) g_lpddsBack
->lpVtbl
->Release(g_lpddsBack
);
336 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>back surface released\n");
337 if(vo_doublebuffering
&& !nooverlay
)
339 if (g_lpddsOverlay
!= NULL
)g_lpddsOverlay
->lpVtbl
->Release(g_lpddsOverlay
);
340 g_lpddsOverlay
= NULL
;
341 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>overlay surface released\n");
343 if (g_lpddsPrimary
!= NULL
) g_lpddsPrimary
->lpVtbl
->Release(g_lpddsPrimary
);
344 g_lpddsPrimary
= NULL
;
345 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>primary released\n");
346 if(hWndFS
)DestroyWindow(hWndFS
);
347 if(hWnd
!= NULL
)DestroyWindow(hWnd
);
348 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>window destroyed\n");
350 if(vidmode
)g_lpdd
->lpVtbl
->RestoreDisplayMode(g_lpdd
);
351 g_lpdd
->lpVtbl
->Release(g_lpdd
);
353 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>directdrawobject released\n");
354 FreeLibrary( hddraw_dll
);
356 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>ddraw.dll freed\n");
357 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>uninited\n");
360 static BOOL WINAPI
EnumCallbackEx(GUID FAR
*lpGUID
, LPSTR lpDriverDescription
, LPSTR lpDriverName
, LPVOID lpContext
, HMONITOR hm
)
362 mp_msg(MSGT_VO
, MSGL_INFO
,"<vo_directx> adapter %d: ", adapter_count
);
366 mp_msg(MSGT_VO
, MSGL_INFO
,"%s", "Primary Display Adapter");
370 mp_msg(MSGT_VO
, MSGL_INFO
,"%s", lpDriverDescription
);
373 if(adapter_count
== vo_adapter_num
){
376 selected_guid_ptr
= NULL
;
379 selected_guid
= *lpGUID
;
380 selected_guid_ptr
= &selected_guid
;
382 mi
.cbSize
= sizeof(mi
);
384 if (myGetMonitorInfo(hm
, &mi
)) {
385 monitor_rect
= mi
.rcMonitor
;
387 mp_msg(MSGT_VO
, MSGL_INFO
,"\t\t<--");
389 mp_msg(MSGT_VO
, MSGL_INFO
,"\n");
393 return 1; // list all adapters
396 static uint32_t Directx_InitDirectDraw()
398 HRESULT (WINAPI
*OurDirectDrawCreateEx
)(GUID
*,LPVOID
*, REFIID
,IUnknown FAR
*);
399 LPDIRECTDRAW lpDDraw
;
401 LPDIRECTDRAWENUMERATEEX OurDirectDrawEnumerateEx
;
402 HINSTANCE user32dll
=LoadLibrary("user32.dll");
405 myGetMonitorInfo
=GetProcAddress(user32dll
,"GetMonitorInfoA");
406 if(!myGetMonitorInfo
&& vo_adapter_num
){
407 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_directx> -adapter is not supported on Win95\n");
412 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>Initing DirectDraw\n" );
414 //load direct draw DLL: based on videolans code
415 hddraw_dll
= LoadLibrary("DDRAW.DLL");
416 if( hddraw_dll
== NULL
)
418 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>failed loading ddraw.dll\n" );
422 if(vo_adapter_num
){ //display other than default
423 OurDirectDrawEnumerateEx
= (LPDIRECTDRAWENUMERATEEX
) GetProcAddress(hddraw_dll
,"DirectDrawEnumerateExA");
424 if (!OurDirectDrawEnumerateEx
){
425 FreeLibrary( hddraw_dll
);
427 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>failed geting proc address: DirectDrawEnumerateEx\n");
428 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>no directx 7 or higher installed\n");
432 // enumerate all display devices attached to the desktop
433 OurDirectDrawEnumerateEx(EnumCallbackEx
, NULL
, DDENUM_ATTACHEDSECONDARYDEVICES
);
435 if(vo_adapter_num
>= adapter_count
)
436 mp_msg(MSGT_VO
, MSGL_ERR
,"Selected adapter (%d) doesn't exist: Default Display Adapter selected\n",vo_adapter_num
);
438 FreeLibrary(user32dll
);
440 OurDirectDrawCreateEx
= (void *)GetProcAddress(hddraw_dll
, "DirectDrawCreateEx");
441 if ( OurDirectDrawCreateEx
== NULL
)
443 FreeLibrary( hddraw_dll
);
445 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>failed geting proc address: DirectDrawCreateEx\n");
449 // initialize DirectDraw and create directx v7 object
450 if (OurDirectDrawCreateEx(selected_guid_ptr
, (VOID
**)&g_lpdd
, &IID_IDirectDraw7
, NULL
) != DD_OK
)
452 FreeLibrary( hddraw_dll
);
454 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>can't initialize ddraw\n");
458 //get current screen siz for selected monitor ...
459 ddsd
.dwSize
=sizeof(ddsd
);
460 ddsd
.dwFlags
=DDSD_WIDTH
|DDSD_HEIGHT
|DDSD_PIXELFORMAT
;
461 g_lpdd
->lpVtbl
->GetDisplayMode(g_lpdd
, &ddsd
);
462 if(vo_screenwidth
&& vo_screenheight
)
464 vm_height
=vo_screenheight
;
465 vm_width
=vo_screenwidth
;
469 vm_height
=ddsd
.dwHeight
;
470 vm_width
=ddsd
.dwWidth
;
474 if(vo_dbpp
)vm_bpp
=vo_dbpp
;
475 else vm_bpp
=ddsd
.ddpfPixelFormat
.dwRGBBitCount
;
478 if (g_lpdd
->lpVtbl
->SetCooperativeLevel(g_lpdd
, hWnd
, DDSCL_EXCLUSIVE
|DDSCL_FULLSCREEN
) != DD_OK
)
480 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>can't set cooperativelevel for exclusive mode\n");
483 /*SetDisplayMode(ddobject,width,height,bpp,refreshrate,aditionalflags)*/
484 if(g_lpdd
->lpVtbl
->SetDisplayMode(g_lpdd
,vm_width
, vm_height
, vm_bpp
,0,0) != DD_OK
)
486 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>can't set displaymode\n");
489 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>Inited adapter %i for %i x %i @ %i \n",vo_adapter_num
,vm_width
,vm_height
,vm_bpp
);
492 if (g_lpdd
->lpVtbl
->SetCooperativeLevel(g_lpdd
, hWnd
, DDSCL_NORMAL
) != DD_OK
) // or DDSCL_SETFOCUSWINDOW
494 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>could not set cooperativelevel for hardwarecheck\n");
497 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>DirectDraw Inited\n");
501 static void check_events(void)
504 while (PeekMessage(&msg
, NULL
, 0, 0,PM_REMOVE
))
506 TranslateMessage(&msg
);
507 DispatchMessage(&msg
);
511 static uint32_t Directx_ManageDisplay()
516 DWORD dwUpdateFlags
=0;
519 if(vo_fs
|| vidmode
){
520 aspect(&width
,&height
,A_ZOOM
);
521 rd
.left
=(vo_screenwidth
-width
)/2;
522 rd
.top
=(vo_screenheight
-height
)/2;
523 if(ShowCursor(FALSE
)>=0)while(ShowCursor(FALSE
)>=0){}
525 else if (WinID
!= -1 && vo_geometry
) {
529 ClientToScreen(hWnd
,&pt
);
531 height
=d_image_height
;
534 while(ShowCursor(TRUE
)<=0){}
538 pt
.x
= 0; //overlayposition relative to the window
540 ClientToScreen(hWnd
,&pt
);
541 GetClientRect(hWnd
, &rd
);
542 width
=rd
.right
- rd
.left
;
543 height
=rd
.bottom
- rd
.top
;
544 pt
.x
-= monitor_rect
.left
; /* move coordinates from global to local monitor space */
545 pt
.y
-= monitor_rect
.top
;
546 rd
.right
-= monitor_rect
.left
;
547 rd
.bottom
-= monitor_rect
.top
;
550 if(!nooverlay
&& (!width
|| !height
)){
551 /*window is minimized*/
552 ddrval
= g_lpddsOverlay
->lpVtbl
->UpdateOverlay(g_lpddsOverlay
,NULL
, g_lpddsPrimary
, NULL
, DDOVER_HIDE
, NULL
);
556 int tmpheight
=((float)width
/window_aspect
);
557 tmpheight
+=tmpheight
%2;
558 if(tmpheight
> height
){
559 width
=((float)height
*window_aspect
);
562 else height
=tmpheight
;
564 while(ShowCursor(TRUE
)<=0){}
566 rd
.right
=rd
.left
+width
;
567 rd
.bottom
=rd
.top
+height
;
569 /*ok, let's workaround some overlay limitations*/
572 uint32_t uStretchFactor1000
; //minimum stretch
573 uint32_t xstretch1000
,ystretch1000
;
574 /*get driver capabilities*/
575 ZeroMemory(&capsDrv
, sizeof(capsDrv
));
576 capsDrv
.dwSize
= sizeof(capsDrv
);
577 if(g_lpdd
->lpVtbl
->GetCaps(g_lpdd
,&capsDrv
, NULL
) != DD_OK
)return 1;
578 /*get minimum stretch, depends on display adaptor and mode (refresh rate!) */
579 uStretchFactor1000
= capsDrv
.dwMinOverlayStretch
>1000 ? capsDrv
.dwMinOverlayStretch
: 1000;
580 rd
.right
= ((width
+rd
.left
)*uStretchFactor1000
+999)/1000;
581 rd
.bottom
= (height
+rd
.top
)*uStretchFactor1000
/1000;
582 /*calculate xstretch1000 and ystretch1000*/
583 xstretch1000
= ((rd
.right
- rd
.left
)* 1000)/image_width
;
584 ystretch1000
= ((rd
.bottom
- rd
.top
)* 1000)/image_height
;
586 rs
.right
=image_width
;
588 rs
.bottom
=image_height
;
589 if(rd
.left
< 0)rs
.left
=(-rd
.left
*1000)/xstretch1000
;
590 if(rd
.top
< 0)rs
.top
=(-rd
.top
*1000)/ystretch1000
;
591 if(rd
.right
> vo_screenwidth
)rs
.right
=((vo_screenwidth
-rd
.left
)*1000)/xstretch1000
;
592 if(rd
.bottom
> vo_screenheight
)rs
.bottom
=((vo_screenheight
-rd
.top
)*1000)/ystretch1000
;
593 /*do not allow to zoom or shrink if hardware isn't able to do so*/
594 if((width
< image_width
)&& !(capsDrv
.dwFXCaps
& DDFXCAPS_OVERLAYSHRINKX
))
596 if(capsDrv
.dwFXCaps
& DDFXCAPS_OVERLAYSHRINKXN
)mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>can only shrinkN\n");
597 else mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>can't shrink x\n");
598 rd
.right
=rd
.left
+image_width
;
600 else if((width
> image_width
)&& !(capsDrv
.dwFXCaps
& DDFXCAPS_OVERLAYSTRETCHX
))
602 if(capsDrv
.dwFXCaps
& DDFXCAPS_OVERLAYSTRETCHXN
)mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>can only stretchN\n");
603 else mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>can't stretch x\n");
604 rd
.right
= rd
.left
+image_width
;
606 if((height
< image_height
) && !(capsDrv
.dwFXCaps
& DDFXCAPS_OVERLAYSHRINKY
))
608 if(capsDrv
.dwFXCaps
& DDFXCAPS_OVERLAYSHRINKYN
)mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>can only shrinkN\n");
609 else mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>can't shrink y\n");
610 rd
.bottom
= rd
.top
+ image_height
;
612 else if((height
> image_height
) && !(capsDrv
.dwFXCaps
& DDFXCAPS_OVERLAYSTRETCHY
))
614 if(capsDrv
.dwFXCaps
& DDFXCAPS_OVERLAYSTRETCHYN
)mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>can only stretchN\n");
615 else mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>can't stretch y\n");
616 rd
.bottom
= rd
.top
+ image_height
;
618 /*the last thing to check are alignment restrictions
619 these expressions (x & -y) just do alignment by dropping low order bits...
620 so to round up, we add first, then truncate*/
621 if((capsDrv
.dwCaps
& DDCAPS_ALIGNBOUNDARYSRC
) && capsDrv
.dwAlignBoundarySrc
)
622 rs
.left
= (rs
.left
+ capsDrv
.dwAlignBoundarySrc
/ 2) & -(signed)(capsDrv
.dwAlignBoundarySrc
);
623 if((capsDrv
.dwCaps
& DDCAPS_ALIGNSIZESRC
) && capsDrv
.dwAlignSizeSrc
)
624 rs
.right
= rs
.left
+ ((rs
.right
- rs
.left
+ capsDrv
.dwAlignSizeSrc
/ 2) & -(signed) (capsDrv
.dwAlignSizeSrc
));
625 if((capsDrv
.dwCaps
& DDCAPS_ALIGNBOUNDARYDEST
) && capsDrv
.dwAlignBoundaryDest
)
626 rd
.left
= (rd
.left
+ capsDrv
.dwAlignBoundaryDest
/ 2) & -(signed)(capsDrv
.dwAlignBoundaryDest
);
627 if((capsDrv
.dwCaps
& DDCAPS_ALIGNSIZEDEST
) && capsDrv
.dwAlignSizeDest
)
628 rd
.right
= rd
.left
+ ((rd
.right
- rd
.left
) & -(signed) (capsDrv
.dwAlignSizeDest
));
629 /*create an overlay FX structure to specify a destination color key*/
630 ZeroMemory(&ovfx
, sizeof(ovfx
));
631 ovfx
.dwSize
= sizeof(ovfx
);
634 ovfx
.dckDestColorkey
.dwColorSpaceLowValue
= 0;
635 ovfx
.dckDestColorkey
.dwColorSpaceHighValue
= 0;
639 ovfx
.dckDestColorkey
.dwColorSpaceLowValue
= destcolorkey
;
640 ovfx
.dckDestColorkey
.dwColorSpaceHighValue
= destcolorkey
;
642 // set the flags we'll send to UpdateOverlay //DDOVER_AUTOFLIP|DDOVERFX_MIRRORLEFTRIGHT|DDOVERFX_MIRRORUPDOWN could be usefull?;
643 dwUpdateFlags
= DDOVER_SHOW
| DDOVER_DDFX
;
644 /*if hardware can't do colorkeying set the window on top*/
645 if(capsDrv
.dwCKeyCaps
& DDCKEYCAPS_DESTOVERLAY
) dwUpdateFlags
|= DDOVER_KEYDESTOVERRIDE
;
650 g_lpddclipper
->lpVtbl
->SetHWnd(g_lpddclipper
, 0,(vo_fs
&& !vidmode
)?hWndFS
: hWnd
);
653 if(!vidmode
&& !vo_fs
){
656 AdjustWindowRect(&rdw
,WS_OVERLAPPEDWINDOW
|WS_SIZEBOX
,FALSE
);
657 // printf("window: %i %i %ix%i\n",rdw.left,rdw.top,rdw.right - rdw.left,rdw.bottom - rdw.top);
658 rdw
.left
+= monitor_rect
.left
; /* move to global coordinate space */
659 rdw
.top
+= monitor_rect
.top
;
660 rdw
.right
+= monitor_rect
.left
;
661 rdw
.bottom
+= monitor_rect
.top
;
662 SetWindowPos(hWnd
,(vo_ontop
)?HWND_TOPMOST
:(vo_rootwin
?HWND_BOTTOM
:HWND_NOTOPMOST
),rdw
.left
,rdw
.top
,rdw
.right
-rdw
.left
,rdw
.bottom
-rdw
.top
,SWP_NOOWNERZORDER
);
665 else SetWindowPos(vidmode
?hWnd
:hWndFS
,vo_rootwin
?HWND_BOTTOM
:HWND_TOPMOST
,0,0,0,0,SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOOWNERZORDER
);
667 /*make sure the overlay is inside the screen*/
668 if(rd
.left
<0)rd
.left
=0;
669 if(rd
.right
>vo_screenwidth
)rd
.right
=vo_screenwidth
;
670 if(rd
.top
<0)rd
.top
=0;
671 if(rd
.bottom
>vo_screenheight
)rd
.bottom
=vo_screenheight
;
673 /*for nonoverlay mode we are finished, for overlay mode we have to display the overlay first*/
674 if(nooverlay
)return 0;
676 // printf("overlay: %i %i %ix%i\n",rd.left,rd.top,rd.right - rd.left,rd.bottom - rd.top);
677 ddrval
= g_lpddsOverlay
->lpVtbl
->UpdateOverlay(g_lpddsOverlay
,&rs
, g_lpddsPrimary
, &rd
, dwUpdateFlags
, &ovfx
);
680 // one cause might be the driver lied about minimum stretch
681 // we should try upping the destination size a bit, or
682 // perhaps shrinking the source size
683 mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>UpdateOverlay failed\n" );
684 mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>Overlay:x1:%i,y1:%i,x2:%i,y2:%i,w:%i,h:%i\n",rd
.left
,rd
.top
,rd
.right
,rd
.bottom
,rd
.right
- rd
.left
,rd
.bottom
- rd
.top
);
685 mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>");
688 case DDERR_NOSTRETCHHW
:
689 {mp_msg(MSGT_VO
, MSGL_ERR
,"hardware can't stretch: try to size the window back\n");break;}
690 case DDERR_INVALIDRECT
:
691 {mp_msg(MSGT_VO
, MSGL_ERR
,"invalid rectangle\n");break;}
692 case DDERR_INVALIDPARAMS
:
693 {mp_msg(MSGT_VO
, MSGL_ERR
,"invalid parameters\n");break;}
694 case DDERR_HEIGHTALIGN
:
695 {mp_msg(MSGT_VO
, MSGL_ERR
,"height align\n");break;}
697 {mp_msg(MSGT_VO
, MSGL_ERR
,"x align\n");break;}
698 case DDERR_UNSUPPORTED
:
699 {mp_msg(MSGT_VO
, MSGL_ERR
,"unsupported\n");break;}
700 case DDERR_INVALIDSURFACETYPE
:
701 {mp_msg(MSGT_VO
, MSGL_ERR
,"invalid surfacetype\n");break;}
702 case DDERR_INVALIDOBJECT
:
703 {mp_msg(MSGT_VO
, MSGL_ERR
,"invalid object\n");break;}
704 case DDERR_SURFACELOST
:
706 mp_msg(MSGT_VO
, MSGL_ERR
,"surfaces lost\n");
707 g_lpddsOverlay
->lpVtbl
->Restore( g_lpddsOverlay
); //restore and try again
708 g_lpddsPrimary
->lpVtbl
->Restore( g_lpddsPrimary
);
709 ddrval
= g_lpddsOverlay
->lpVtbl
->UpdateOverlay(g_lpddsOverlay
,&rs
, g_lpddsPrimary
, &rd
, dwUpdateFlags
, &ovfx
);
710 if(ddrval
!=DD_OK
)mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>UpdateOverlay failed again\n" );
714 mp_msg(MSGT_VO
, MSGL_ERR
," 0x%x\n",ddrval
);
716 /*ok we can't do anything about it -> hide overlay*/
719 ddrval
= g_lpddsOverlay
->lpVtbl
->UpdateOverlay(g_lpddsOverlay
,NULL
, g_lpddsPrimary
, NULL
, DDOVER_HIDE
, NULL
);
726 //find out supported overlay pixelformats
727 static uint32_t Directx_CheckOverlayPixelformats()
731 DDSURFACEDESC2 ddsdOverlay
;
733 uint32_t formatcount
= 0;
734 //get driver caps to determine overlay support
735 ZeroMemory(&capsDrv
, sizeof(capsDrv
));
736 capsDrv
.dwSize
= sizeof(capsDrv
);
737 ddrval
= g_lpdd
->lpVtbl
->GetCaps(g_lpdd
,&capsDrv
, NULL
);
740 mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>failed getting ddrawcaps\n");
743 if (!(capsDrv
.dwCaps
& DDCAPS_OVERLAY
))
745 mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>Your card doesn't support overlay\n");
748 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>testing supported overlay pixelformats\n");
749 //it is not possible to query for pixel formats supported by the
750 //overlay hardware: try out various formats till one works
751 ZeroMemory(&ddsdOverlay
, sizeof(ddsdOverlay
));
752 ddsdOverlay
.dwSize
= sizeof(ddsdOverlay
);
753 ddsdOverlay
.ddsCaps
.dwCaps
=DDSCAPS_OVERLAY
| DDSCAPS_VIDEOMEMORY
;
754 ddsdOverlay
.dwFlags
= DDSD_CAPS
|DDSD_HEIGHT
|DDSD_WIDTH
| DDSD_PIXELFORMAT
;
755 ddsdOverlay
.dwWidth
=300;
756 ddsdOverlay
.dwHeight
=280;
757 ddsdOverlay
.dwBackBufferCount
=0;
758 //try to create an overlay surface using one of the pixel formats in our global list
762 ddsdOverlay
.ddpfPixelFormat
=g_ddpf
[i
].g_ddpfOverlay
;
763 ddrval
= g_lpdd
->lpVtbl
->CreateSurface(g_lpdd
,&ddsdOverlay
, &g_lpddsOverlay
, NULL
);
766 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><FORMAT OVERLAY>%i %s supported\n",i
,g_ddpf
[i
].img_format_name
);
767 g_ddpf
[i
].drv_caps
= VFCAP_CSP_SUPPORTED
|VFCAP_OSD
|VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_HWSCALE_UP
;
769 else mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><FORMAT OVERLAY>%i %s not supported\n",i
,g_ddpf
[i
].img_format_name
);
770 if (g_lpddsOverlay
!= NULL
) {g_lpddsOverlay
->lpVtbl
->Release(g_lpddsOverlay
);g_lpddsOverlay
= NULL
;}
771 } while( ++i
< NUM_FORMATS
);
772 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>Your card supports %i of %i overlayformats\n",formatcount
, NUM_FORMATS
);
773 if (formatcount
== 0)
775 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><WARN>Your card supports overlay, but we couldn't create one\n");
776 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>This can have the following reasons:\n");
777 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>- you are already using an overlay with another app\n");
778 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>- you don't have enough videomemory\n");
779 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>- vo_directx doesn't support the cards overlay pixelformat\n");
782 if(capsDrv
.dwFXCaps
& DDFXCAPS_OVERLAYMIRRORLEFTRIGHT
)mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>can mirror left right\n"); //I don't have hardware which
783 if(capsDrv
.dwFXCaps
& DDFXCAPS_OVERLAYMIRRORUPDOWN
)mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>can mirror up down\n"); //supports those send me one and I'll implement ;)
787 //find out the Pixelformat of the Primary Surface
788 static uint32_t Directx_CheckPrimaryPixelformat()
791 uint32_t formatcount
= 0;
796 COLORREF rgbT
=RGB(0,0,0);
797 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>checking primary surface\n");
798 memset( &ddpf
, 0, sizeof( DDPIXELFORMAT
));
799 ddpf
.dwSize
= sizeof( DDPIXELFORMAT
);
800 //we have to create a primary surface first
801 if(Directx_CreatePrimarySurface()!=0)return 1;
802 if(g_lpddsPrimary
->lpVtbl
->GetPixelFormat( g_lpddsPrimary
, &ddpf
) != DD_OK
)
804 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>can't get pixelformat\n");
807 while ( i
< NUM_FORMATS
)
809 if (g_ddpf
[i
].g_ddpfOverlay
.dwRGBBitCount
== ddpf
.dwRGBBitCount
)
811 if (g_ddpf
[i
].g_ddpfOverlay
.dwRBitMask
== ddpf
.dwRBitMask
)
813 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><FORMAT PRIMARY>%i %s supported\n",i
,g_ddpf
[i
].img_format_name
);
814 g_ddpf
[i
].drv_caps
= VFCAP_CSP_SUPPORTED
|VFCAP_OSD
;
816 primary_image_format
=g_ddpf
[i
].img_format
;
821 //get the colorkey for overlay mode
822 destcolorkey
= CLR_INVALID
;
823 if (windowcolor
!= CLR_INVALID
&& g_lpddsPrimary
->lpVtbl
->GetDC(g_lpddsPrimary
,&hdc
) == DD_OK
)
825 rgbT
= GetPixel(hdc
, 0, 0);
826 SetPixel(hdc
, 0, 0, windowcolor
);
827 g_lpddsPrimary
->lpVtbl
->ReleaseDC(g_lpddsPrimary
,hdc
);
829 // read back the converted color
830 ddsd
.dwSize
= sizeof(ddsd
);
831 while ((hres
= g_lpddsPrimary
->lpVtbl
->Lock(g_lpddsPrimary
,NULL
, &ddsd
, 0, NULL
)) == DDERR_WASSTILLDRAWING
)
835 destcolorkey
= *(DWORD
*) ddsd
.lpSurface
;
836 if (ddsd
.ddpfPixelFormat
.dwRGBBitCount
< 32)
837 destcolorkey
&= (1 << ddsd
.ddpfPixelFormat
.dwRGBBitCount
) - 1;
838 g_lpddsPrimary
->lpVtbl
->Unlock(g_lpddsPrimary
,NULL
);
840 if (windowcolor
!= CLR_INVALID
&& g_lpddsPrimary
->lpVtbl
->GetDC(g_lpddsPrimary
,&hdc
) == DD_OK
)
842 SetPixel(hdc
, 0, 0, rgbT
);
843 g_lpddsPrimary
->lpVtbl
->ReleaseDC(g_lpddsPrimary
,hdc
);
846 g_lpddsPrimary
->lpVtbl
->Release(g_lpddsPrimary
);
847 g_lpddsPrimary
= NULL
;
850 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>Unknown Pixelformat\n");
856 //function handles input
857 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
861 case WM_MOUSEACTIVATE
:
862 return MA_ACTIVATEANDEAT
;
865 if(vidmode
&& adapter_count
> 2) //only disable if more than one adapter.
876 mp_input_queue_cmd(mp_input_parse_cmd("quit"));
879 case WM_WINDOWPOSCHANGED
:
881 //printf("Windowposchange\n");
882 if(g_lpddsBack
!= NULL
) //or it will crash with -vm
884 Directx_ManageDisplay();
891 { //kill screensaver etc.
892 //note: works only when the window is active
893 //you can workaround this by disabling the allow screensaver option in
894 //the link to the app
896 case SC_MONITORPOWER
:
897 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>killing screensaver\n" );
906 {mplayer_put_key(KEY_LEFT
);break;}
908 {mplayer_put_key(KEY_UP
);break;}
910 {mplayer_put_key(KEY_RIGHT
);break;}
912 {mplayer_put_key(KEY_DOWN
);break;}
914 {mplayer_put_key(KEY_TAB
);break;}
916 {mplayer_put_key(KEY_BS
);break;}
918 {mplayer_put_key(KEY_DELETE
);break;}
920 {mplayer_put_key(KEY_INSERT
);break;}
922 {mplayer_put_key(KEY_HOME
);break;}
924 {mplayer_put_key(KEY_END
);break;}
926 {mplayer_put_key(KEY_PAGE_UP
);break;}
928 {mplayer_put_key(KEY_PAGE_DOWN
);break;}
930 {mplayer_put_key(KEY_ESC
);break;}
936 mplayer_put_key(wParam
);
941 if (!vo_nomouse_input
)
942 mplayer_put_key(MOUSE_BTN0
);
947 if (!vo_nomouse_input
)
948 mplayer_put_key(MOUSE_BTN1
);
953 if (!vo_nomouse_input
)
954 mplayer_put_key(MOUSE_BTN2
);
959 if (vo_nomouse_input
)
961 int x
= GET_WHEEL_DELTA_WPARAM(wParam
);
963 mplayer_put_key(MOUSE_BTN3
);
965 mplayer_put_key(MOUSE_BTN4
);
970 if (vo_nomouse_input
)
972 if (HIWORD(wParam
) == 1)
973 mplayer_put_key(MOUSE_BTN5
);
975 mplayer_put_key(MOUSE_BTN6
);
980 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
984 static uint32_t preinit(const char *arg
)
986 HINSTANCE hInstance
= GetModuleHandle(NULL
);
987 HICON mplayericon
=NULL
;
988 char exedir
[MAX_PATH
];
992 if(strstr(arg
,"noaccel"))
994 mp_msg(MSGT_VO
,MSGL_V
,"<vo_directx><INFO>disabled overlay\n");
998 /*load icon from the main app*/
999 if(GetModuleFileName(NULL
,exedir
,MAX_PATH
))
1001 mplayericon
= ExtractIcon( hInstance
, exedir
, 0 );
1003 if(!mplayericon
)mplayericon
=LoadIcon(NULL
,IDI_APPLICATION
);
1004 monitor_rect
.right
=GetSystemMetrics(SM_CXSCREEN
);
1005 monitor_rect
.bottom
=GetSystemMetrics(SM_CYSCREEN
);
1007 windowcolor
= vo_colorkey
;
1008 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
1009 wc
.lpfnWndProc
= WndProc
;
1012 wc
.hInstance
= hInstance
;
1013 wc
.hCursor
= LoadCursor(NULL
,IDC_ARROW
);
1014 wc
.hIcon
= mplayericon
;
1015 wc
.hbrBackground
= CreateSolidBrush(vidmode
?RGB(0,0,0):windowcolor
);
1016 wc
.lpszClassName
= "MPlayer - The Movie Player";
1017 wc
.lpszMenuName
= NULL
;
1019 if (WinID
!= -1) hWnd
= WinID
;
1021 hWnd
= CreateWindowEx(vidmode
?WS_EX_TOPMOST
:0,
1022 "MPlayer - The Movie Player","",(vidmode
)?WS_POPUP
:WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
,
1023 CW_USEDEFAULT
, CW_USEDEFAULT
, 100, 100,NULL
,NULL
,hInstance
,NULL
);
1024 wc
.hbrBackground
= CreateSolidBrush(RGB(0,0,0));
1025 wc
.lpszClassName
= "MPlayer - Fullscreen";
1028 if (Directx_InitDirectDraw()!= 0)return 1; //init DirectDraw
1030 if(!vidmode
)hWndFS
= CreateWindow("MPlayer - Fullscreen","MPlayer Fullscreen",WS_POPUP
,monitor_rect
.left
,monitor_rect
.top
,monitor_rect
.right
-monitor_rect
.left
,monitor_rect
.bottom
-monitor_rect
.top
,hWnd
,NULL
,hInstance
,NULL
);
1031 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>initial mplayer windows created\n");
1033 if (Directx_CheckPrimaryPixelformat()!=0)return 1;
1034 if (!nooverlay
&& Directx_CheckOverlayPixelformats() == 0) //check for supported hardware
1036 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>hardware supports overlay\n");
1039 else //if we can't have overlay we create a backpuffer with the same imageformat as the primary surface
1041 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>using backpuffer\n");
1044 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>preinit succesfully finished\n");
1048 static uint32_t draw_slice(uint8_t *src
[], int stride
[], int w
,int h
,int x
,int y
)
1052 uint32_t i
=0, uvstride
=dstride
/2;
1054 d
=image
+dstride
*y
+x
;
1062 w
/=2;h
/=2;x
/=2;y
/=2;
1065 d
=image
+dstride
*image_height
+ uvstride
*y
+x
;
1066 if(image_format
== IMGFMT_YV12
)s
=src
[2];
1075 d
=image
+dstride
*image_height
+uvstride
*(image_height
/2) + uvstride
*y
+x
;
1076 if(image_format
== IMGFMT_YV12
)s
=src
[1];
1086 static void flip_page(void)
1089 g_lpddsBack
->lpVtbl
->Unlock (g_lpddsBack
,NULL
);
1090 if (vo_doublebuffering
)
1092 // flip to the next image in the sequence
1093 dxresult
= g_lpddsOverlay
->lpVtbl
->Flip( g_lpddsOverlay
,NULL
, DDFLIP_WAIT
);
1094 if(dxresult
== DDERR_SURFACELOST
)
1096 mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR><vo_directx><INFO>Restoring Surface\n");
1097 g_lpddsBack
->lpVtbl
->Restore( g_lpddsBack
);
1098 dxresult
= g_lpddsOverlay
->lpVtbl
->Flip( g_lpddsOverlay
,NULL
, DDFLIP_WAIT
);
1100 if(dxresult
!= DD_OK
)mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>can't flip page\n");
1105 // ask for the "NOTEARING" option
1106 memset( &ddbltfx
, 0, sizeof(DDBLTFX
) );
1107 ddbltfx
.dwSize
= sizeof(DDBLTFX
);
1108 ddbltfx
.dwDDFX
= DDBLTFX_NOTEARING
;
1109 g_lpddsPrimary
->lpVtbl
->Blt(g_lpddsPrimary
, &rd
, g_lpddsBack
, NULL
, DDBLT_WAIT
, &ddbltfx
);
1111 g_lpddsBack
->lpVtbl
->Lock(g_lpddsBack
,NULL
,&ddsdsf
, DDLOCK_NOSYSLOCK
| DDLOCK_WAIT
, NULL
);
1112 if(vo_directrendering
&& (dstride
!= ddsdsf
.lPitch
)){
1113 mp_msg(MSGT_VO
,MSGL_WARN
,"<vo_directx><WARN>stride changed !!!! disabling direct rendering\n");
1114 vo_directrendering
=0;
1116 dstride
= ddsdsf
.lPitch
;
1117 image
= ddsdsf
.lpSurface
;
1120 static uint32_t draw_frame(uint8_t *src
[])
1122 memcpy( image
, *src
, dstride
* image_height
);
1126 static uint32_t get_image(mp_image_t
*mpi
)
1128 if(mpi
->flags
&MP_IMGFLAG_READABLE
) {mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><ERROR>slow video ram\n");return VO_FALSE
;}
1129 if(mpi
->type
==MP_IMGTYPE_STATIC
) {mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><ERROR>not static\n");return VO_FALSE
;}
1130 if((mpi
->width
==dstride
) || (mpi
->flags
&(MP_IMGFLAG_ACCEPT_STRIDE
|MP_IMGFLAG_ACCEPT_WIDTH
)))
1132 if(mpi
->flags
&MP_IMGFLAG_PLANAR
)
1134 if(image_format
== IMGFMT_YV12
)
1136 mpi
->planes
[2]= image
+ dstride
*image_height
;
1137 mpi
->planes
[1]= image
+ dstride
*image_height
+ dstride
*image_height
/4;
1138 mpi
->stride
[1]=mpi
->stride
[2]=dstride
/2;
1140 else if(image_format
== IMGFMT_IYUV
|| image_format
== IMGFMT_I420
)
1142 mpi
->planes
[1]= image
+ dstride
*image_height
;
1143 mpi
->planes
[2]= image
+ dstride
*image_height
+ dstride
*image_height
/4;
1144 mpi
->stride
[1]=mpi
->stride
[2]=dstride
/2;
1146 else if(image_format
== IMGFMT_YVU9
)
1148 mpi
->planes
[2] = image
+ dstride
*image_height
;
1149 mpi
->planes
[1] = image
+ dstride
*image_height
+ dstride
*image_height
/16;
1150 mpi
->stride
[1]=mpi
->stride
[2]=dstride
/4;
1153 mpi
->planes
[0]=image
;
1154 mpi
->stride
[0]=dstride
;
1155 mpi
->width
=image_width
;
1156 mpi
->height
=image_height
;
1157 mpi
->flags
|=MP_IMGFLAG_DIRECT
;
1158 mp_msg(MSGT_VO
, MSGL_DBG3
, "<vo_directx><INFO>Direct Rendering ENABLED\n");
1164 static uint32_t put_image(mp_image_t
*mpi
){
1169 uint32_t x
= mpi
->x
;
1170 uint32_t y
= mpi
->y
;
1171 uint32_t w
= mpi
->w
;
1172 uint32_t h
= mpi
->h
;
1174 if (WinID
!= -1) Directx_ManageDisplay();
1176 if((mpi
->flags
&MP_IMGFLAG_DIRECT
)||(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
))
1178 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>put_image: nothing to do: drawslices\n");
1182 if (mpi
->flags
&MP_IMGFLAG_PLANAR
)
1185 if(image_format
!=IMGFMT_YVU9
)draw_slice(mpi
->planes
,mpi
->stride
,mpi
->w
,mpi
->h
,0,0);
1189 d
=image
+dstride
*y
+x
;
1196 w
/=4;h
/=4;x
/=4;y
/=4;
1198 d
=image
+dstride
*image_height
+ dstride
*y
/4+x
;
1206 d
=image
+dstride
*image_height
+ dstride
*image_height
/16 + dstride
/4*y
+x
;
1217 memcpy( image
, mpi
->planes
[0], image_height
* dstride
);
1223 config(uint32_t width
, uint32_t height
, uint32_t d_width
, uint32_t d_height
, uint32_t options
, char *title
, uint32_t format
)
1226 vo_screenwidth
= monitor_rect
.right
- monitor_rect
.left
;
1227 vo_screenheight
= monitor_rect
.bottom
- monitor_rect
.top
;
1228 vo_fs
= options
& 0x01;
1229 image_format
= format
;
1230 image_width
= width
;
1231 image_height
= height
;
1232 d_image_width
= d_width
;
1233 d_image_height
= d_height
;
1234 if(format
!= primary_image_format
)nooverlay
= 0;
1235 aspect_save_orig(image_width
,image_height
);
1236 aspect_save_prescale(d_image_width
,d_image_height
);
1238 vo_screenwidth
=vm_width
;
1239 vo_screenheight
=vm_height
;
1241 aspect_save_screenres(vo_screenwidth
,vo_screenheight
);
1242 aspect(&d_image_width
, &d_image_height
, A_NOZOOM
);
1243 window_aspect
= (float)d_image_width
/ (float)d_image_height
;
1248 vo_dx
= ( vo_screenwidth
- d_image_width
) / 2; vo_dy
=( vo_screenheight
- d_image_height
) / 2;
1249 geometry(&vo_dx
, &vo_dy
, &d_image_width
, &d_image_height
, vo_screenwidth
, vo_screenheight
);
1252 GetWindowRect(hWnd
,&rd
);
1256 vo_dx
+= monitor_rect
.left
; /* move position to global window space */
1257 vo_dy
+= monitor_rect
.top
;
1260 rd
.right
= rd
.left
+ d_image_width
;
1261 rd
.bottom
= rd
.top
+ d_image_height
;
1263 AdjustWindowRect(&rd
,WS_OVERLAPPEDWINDOW
|WS_SIZEBOX
,FALSE
);
1264 SetWindowPos(hWnd
,NULL
, vo_dx
, vo_dy
,rd
.right
-rd
.left
,rd
.bottom
-rd
.top
,SWP_SHOWWINDOW
|SWP_NOOWNERZORDER
);
1267 else ShowWindow(hWnd
,SW_SHOW
);
1269 if(vo_fs
&& !vidmode
)ShowWindow(hWndFS
,SW_SHOW
);
1271 SetWindowText(hWnd
,title
);
1275 /*release all surfaces*/
1276 if (g_lpddsBack
!= NULL
) g_lpddsBack
->lpVtbl
->Release(g_lpddsBack
);
1278 if(vo_doublebuffering
)
1280 if (g_lpddsOverlay
!= NULL
)g_lpddsOverlay
->lpVtbl
->Release(g_lpddsOverlay
);
1282 g_lpddsOverlay
= NULL
;
1283 if (g_lpddsPrimary
!= NULL
) g_lpddsPrimary
->lpVtbl
->Release(g_lpddsPrimary
);
1284 g_lpddsPrimary
= NULL
;
1285 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>overlay surfaces released\n");
1288 /*create the surfaces*/
1289 if(Directx_CreatePrimarySurface())return 1;
1290 if (!nooverlay
&& Directx_CreateOverlay(image_format
))
1292 if(format
== primary_image_format
)nooverlay
=1; /*overlay creation failed*/
1294 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>can't use overlay mode: please use -vo directx:noaccel\n");
1300 if(Directx_CreateBackpuffer())
1302 mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>can't get the driver to work on your system :(\n");
1305 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><INFO>back surface created\n");
1306 vo_doublebuffering
= 0;
1307 /*create clipper for nonoverlay mode*/
1308 if(g_lpddclipper
)g_lpddclipper
->lpVtbl
->Release(g_lpddclipper
);
1310 if(g_lpdd
->lpVtbl
->CreateClipper(g_lpdd
, 0, &g_lpddclipper
,NULL
)!= DD_OK
){mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>can't create clipper\n");return 1;}
1311 if(g_lpddclipper
->lpVtbl
->SetHWnd (g_lpddclipper
, 0, hWnd
)!= DD_OK
){mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>can't associate clipper with window\n");return 1;}
1312 if(g_lpddsPrimary
->lpVtbl
->SetClipper (g_lpddsPrimary
,g_lpddclipper
)!=DD_OK
){mp_msg(MSGT_VO
, MSGL_FATAL
,"<vo_directx><FATAL ERROR>can't associate primary surface with clipper\n");return 1;}
1313 mp_msg(MSGT_VO
, MSGL_DBG3
,"<vo_directx><INFO>clipper succesfully created\n");
1315 if(DD_OK
!= g_lpddsOverlay
->lpVtbl
->QueryInterface(g_lpddsOverlay
,&IID_IDirectDrawColorControl
,(void**)&g_cc
))
1316 mp_msg(MSGT_VO
, MSGL_V
,"<vo_directx><WARN>unable to get DirectDraw ColorControl interface\n");
1318 Directx_ManageDisplay();
1319 memset(&ddsdsf
, 0,sizeof(DDSURFACEDESC2
));
1320 ddsdsf
.dwSize
= sizeof (DDSURFACEDESC2
);
1321 g_lpddsBack
->lpVtbl
->Lock(g_lpddsBack
,NULL
,&ddsdsf
, DDLOCK_NOSYSLOCK
| DDLOCK_WAIT
, NULL
);
1322 dstride
= ddsdsf
.lPitch
;
1323 image
= ddsdsf
.lpSurface
;
1327 //function to set color controls
1328 // brightness [0, 10000]
1329 // contrast [0, 20000]
1331 // saturation [0, 20000]
1332 static uint32_t color_ctrl_set(char *what
, int value
)
1334 uint32_t r
= VO_NOTIMPL
;
1336 //printf("\n*** %s = %d\n", what, value);
1338 //printf("\n *** could not get color control interface!!!\n");
1341 ZeroMemory(&dcc
, sizeof(dcc
));
1342 dcc
.dwSize
= sizeof(dcc
);
1344 if (!strcmp(what
, "brightness")) {
1345 dcc
.dwFlags
= DDCOLOR_BRIGHTNESS
;
1346 dcc
.lBrightness
= (value
+ 100) * 10000 / 200;
1348 } else if (!strcmp(what
, "contrast")) {
1349 dcc
.dwFlags
= DDCOLOR_CONTRAST
;
1350 dcc
.lContrast
= (value
+ 100) * 20000 / 200;
1352 } else if (!strcmp(what
, "hue")) {
1353 dcc
.dwFlags
= DDCOLOR_HUE
;
1354 dcc
.lHue
= value
* 180 / 100;
1356 } else if (!strcmp(what
, "saturation")) {
1357 dcc
.dwFlags
= DDCOLOR_SATURATION
;
1358 dcc
.lSaturation
= (value
+ 100) * 20000 / 200;
1363 g_cc
->lpVtbl
->SetColorControls(g_cc
, &dcc
);
1368 //analoguous to color_ctrl_set
1369 static uint32_t color_ctrl_get(char *what
, int *value
)
1371 uint32_t r
= VO_NOTIMPL
;
1374 //printf("\n *** could not get color control interface!!!\n");
1377 ZeroMemory(&dcc
, sizeof(dcc
));
1378 dcc
.dwSize
= sizeof(dcc
);
1380 if (g_cc
->lpVtbl
->GetColorControls(g_cc
, &dcc
) != DD_OK
) {
1384 if (!strcmp(what
, "brightness") && (dcc
.dwFlags
& DDCOLOR_BRIGHTNESS
)) {
1385 *value
= dcc
.lBrightness
* 200 / 10000 - 100;
1387 } else if (!strcmp(what
, "contrast") && (dcc
.dwFlags
& DDCOLOR_CONTRAST
)) {
1388 *value
= dcc
.lContrast
* 200 / 20000 - 100;
1390 } else if (!strcmp(what
, "hue") && (dcc
.dwFlags
& DDCOLOR_HUE
)) {
1391 *value
= dcc
.lHue
* 100 / 180;
1393 } else if (!strcmp(what
, "saturation") && (dcc
.dwFlags
& DDCOLOR_SATURATION
)) {
1394 *value
= dcc
.lSaturation
* 200 / 20000 - 100;
1397 // printf("\n*** %s = %d\n", what, *value);
1402 static uint32_t control(uint32_t request
, void *data
, ...)
1406 case VOCTRL_GET_IMAGE
:
1407 return get_image(data
);
1408 case VOCTRL_QUERY_FORMAT
:
1409 return query_format(*((uint32_t*)data
));
1410 case VOCTRL_DRAW_IMAGE
:
1411 return put_image(data
);
1413 if(WinID
!= -1) return VO_TRUE
;
1416 mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>ontop has no meaning in exclusive mode\n");
1420 if(vo_ontop
) vo_ontop
= 0;
1422 Directx_ManageDisplay();
1425 case VOCTRL_ROOTWIN
:
1426 if(WinID
!= -1) return VO_TRUE
;
1429 mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>rootwin has no meaning in exclusive mode\n");
1433 if(vo_rootwin
) vo_rootwin
= 0;
1434 else vo_rootwin
= 1;
1435 Directx_ManageDisplay();
1438 case VOCTRL_FULLSCREEN
:
1442 mp_msg(MSGT_VO
, MSGL_ERR
,"<vo_directx><ERROR>currently we do not allow to switch from exclusive to windowed mode\n");
1449 ShowWindow(hWndFS
,SW_SHOW
);
1450 ShowWindow(hWnd
,SW_HIDE
);
1455 ShowWindow(hWndFS
,SW_HIDE
);
1456 ShowWindow(hWnd
,SW_SHOW
);
1458 Directx_ManageDisplay();
1463 case VOCTRL_SET_EQUALIZER
: {
1468 value
= va_arg(ap
, int);
1470 return color_ctrl_set(data
, value
);
1472 case VOCTRL_GET_EQUALIZER
: {
1477 value
= va_arg(ap
, int*);
1479 return color_ctrl_get(data
, value
);