1 /*****************************************************************************
2 * vout.c: Windows DirectX video output display method
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
5 * $Id: directx.c,v 1.34 2004/01/26 16:45:02 zorglub Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
27 * This plugin will use YUV overlay if supported, using overlay will result in
28 * the best video quality (hardware interpolation when rescaling the picture)
29 * and the fastest display as it requires less processing.
31 * If YUV overlay is not supported this plugin will use RGB offscreen video
32 * surfaces that will be blitted onto the primary surface (display) to
33 * effectively display the pictures. This fallback method also enables us to
34 * display video in window mode.
36 *****************************************************************************/
37 #include <errno.h> /* ENOMEM */
38 #include <stdlib.h> /* free() */
39 #include <string.h> /* strerror() */
49 #undef GetSystemMetrics
51 #ifndef MONITOR_DEFAULTTONEAREST
52 # define MONITOR_DEFAULTTONEAREST 2
57 /*****************************************************************************
59 * Defining them here allows us to get rid of the dxguid library during
61 *****************************************************************************/
63 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
64 DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
66 /*****************************************************************************
68 *****************************************************************************/
69 static int OpenVideo ( vlc_object_t * );
70 static void CloseVideo ( vlc_object_t * );
72 static int Init ( vout_thread_t * );
73 static void End ( vout_thread_t * );
74 static int Manage ( vout_thread_t * );
75 static void Display ( vout_thread_t *, picture_t * );
77 static int NewPictureVec ( vout_thread_t *, picture_t *, int );
78 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
79 static int UpdatePictureStruct( vout_thread_t *, picture_t *, int );
81 static int DirectXInitDDraw ( vout_thread_t *p_vout );
82 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
83 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
84 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
85 static int DirectXCreateSurface ( vout_thread_t *p_vout,
86 LPDIRECTDRAWSURFACE2 *, int, int, int );
87 static void DirectXCloseSurface ( vout_thread_t *p_vout,
88 LPDIRECTDRAWSURFACE2 );
89 static int DirectXCreateClipper ( vout_thread_t *p_vout );
90 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
91 static int DirectXLockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
92 static int DirectXUnlockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
94 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t i_color );
96 /* Object variables callbacks */
97 static int OnTopCallback( vlc_object_t *, char const *,
98 vlc_value_t, vlc_value_t, void * );
99 static int FindDevicesCallback( vlc_object_t *, char const *,
100 vlc_value_t, vlc_value_t, void * );
102 /*****************************************************************************
104 *****************************************************************************/
105 #define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
106 #define HW_YUV_LONGTEXT N_( \
107 "Try to use hardware acceleration for YUV->RGB conversions. " \
108 "This option doesn't have any effect when using overlays." )
110 #define SYSMEM_TEXT N_("Use video buffers in system memory")
111 #define SYSMEM_LONGTEXT N_( \
112 "Create video buffers in system memory instead of video memory. This " \
113 "isn't recommended as usually using video memory allows to benefit from " \
114 "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
115 "This option doesn't have any effect when using overlays." )
117 #define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
118 #define TRIPLEBUF_LONGTEXT N_( \
119 "Try to use triple bufferring when using YUV overlays. That results in " \
120 "much better video quality (no flickering)." )
122 #define DEVICE_TEXT N_("Name of desired display device")
123 #define DEVICE_LONGTEXT N_("In a multimonitor configuration, you can specify "\
124 "the Windows device name of the display that you want the video window " \
125 "to open on. For example, \"\\\\.\\DISPLAY1\" or \"\\\\.\\DISPLAY2\"." )
127 static char *ppsz_dev[] = { "" };
128 static char *ppsz_dev_text[] = { N_("Default") };
131 add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT,
133 add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT,
135 add_bool( "directx-3buffering", 1, NULL, TRIPLEBUF_TEXT,
136 TRIPLEBUF_LONGTEXT, VLC_TRUE );
138 add_string( "directx-device", "", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
140 change_string_list( ppsz_dev, ppsz_dev_text, FindDevicesCallback );
142 set_description( _("DirectX video output") );
143 set_capability( "video output", 100 );
144 add_shortcut( "directx" );
145 set_callbacks( OpenVideo, CloseVideo );
149 /* check if we registered a window class because we need to
152 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
153 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
156 /*****************************************************************************
157 * OpenVideo: allocate DirectX video thread output method
158 *****************************************************************************
159 * This function allocates and initialize the DirectX vout method.
160 *****************************************************************************/
161 static int OpenVideo( vlc_object_t *p_this )
163 vout_thread_t * p_vout = (vout_thread_t *)p_this;
164 vlc_value_t val, text;
167 /* Allocate structure */
168 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
169 if( p_vout->p_sys == NULL )
171 msg_Err( p_vout, "out of memory" );
175 /* Initialisations */
176 p_vout->pf_init = Init;
177 p_vout->pf_end = End;
178 p_vout->pf_manage = Manage;
179 p_vout->pf_render = NULL;
180 p_vout->pf_display = Display;
182 p_vout->p_sys->p_ddobject = NULL;
183 p_vout->p_sys->p_display = NULL;
184 p_vout->p_sys->p_current_surface = NULL;
185 p_vout->p_sys->p_clipper = NULL;
186 p_vout->p_sys->hwnd = NULL;
187 p_vout->p_sys->hparent = NULL;
188 p_vout->p_sys->i_changes = 0;
190 /* Multimonitor stuff */
191 p_vout->p_sys->hmonitor = NULL;
192 p_vout->p_sys->p_display_driver = NULL;
193 p_vout->p_sys->MonitorFromWindow = NULL;
194 p_vout->p_sys->GetMonitorInfo = NULL;
195 if( (huser32 = GetModuleHandle( "USER32" ) ) )
197 p_vout->p_sys->MonitorFromWindow =
198 GetProcAddress( huser32, "MonitorFromWindow" );
199 p_vout->p_sys->GetMonitorInfo =
200 GetProcAddress( huser32, "GetMonitorInfoA" );
203 var_Create( p_vout, "overlay", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
204 var_Create( p_vout, "directx-use-sysmem", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
205 var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
206 var_Create( p_vout, "directx-3buffering", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
207 var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
209 p_vout->p_sys->b_cursor_hidden = 0;
210 p_vout->p_sys->i_lastmoved = mdate();
212 /* Set main window's size */
213 p_vout->p_sys->i_window_width = p_vout->i_window_width;
214 p_vout->p_sys->i_window_height = p_vout->i_window_height;
216 /* Create the DirectXEventThread, this thread is created by us to isolate
217 * the Win32 PeekMessage function calls. We want to do this because
218 * Windows can stay blocked inside this call for a long time, and when
219 * this happens it thus blocks vlc's video_output thread.
220 * DirectXEventThread will take care of the creation of the video
221 * window (because PeekMessage has to be called from the same thread which
222 * created the window). */
223 msg_Dbg( p_vout, "creating DirectXEventThread" );
224 p_vout->p_sys->p_event =
225 vlc_object_create( p_vout, sizeof(event_thread_t) );
226 p_vout->p_sys->p_event->p_vout = p_vout;
227 if( vlc_thread_create( p_vout->p_sys->p_event, "DirectX Events Thread",
228 DirectXEventThread, 0, 1 ) )
230 msg_Err( p_vout, "cannot create DirectXEventThread" );
231 vlc_object_destroy( p_vout->p_sys->p_event );
232 p_vout->p_sys->p_event = NULL;
236 if( p_vout->p_sys->p_event->b_error )
238 msg_Err( p_vout, "DirectXEventThread failed" );
242 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
244 msg_Dbg( p_vout, "DirectXEventThread running" );
246 /* Initialise DirectDraw */
247 if( DirectXInitDDraw( p_vout ) )
249 msg_Err( p_vout, "cannot initialize DirectDraw" );
253 /* Create the directx display */
254 if( DirectXCreateDisplay( p_vout ) )
256 msg_Err( p_vout, "cannot initialize DirectDraw" );
260 /* Add a variable to indicate if the window should be on top of others */
261 var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
262 text.psz_string = _("Always on top");
263 var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
264 var_Get( p_vout, "video-on-top", &val );
265 p_vout->p_sys->b_on_top_change = val.b_bool;
266 var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
271 CloseVideo( VLC_OBJECT(p_vout) );
275 /*****************************************************************************
276 * Init: initialize DirectX video thread output method
277 *****************************************************************************
278 * This function create the directx surfaces needed by the output thread.
279 * It is called at the beginning of the thread.
280 *****************************************************************************/
281 static int Init( vout_thread_t *p_vout )
286 /* Get a few default parameters */
287 var_Get( p_vout, "overlay", &val );
288 p_vout->p_sys->b_using_overlay = val.b_bool;
289 var_Get( p_vout, "directx-use-sysmem", &val );
290 p_vout->p_sys->b_use_sysmem = val.b_bool;
291 var_Get( p_vout, "directx-hw-yuv", &val );
292 p_vout->p_sys->b_hw_yuv = val.b_bool;
293 var_Get( p_vout, "directx-3buffering", &val );
294 p_vout->p_sys->b_3buf_overlay = val.b_bool;
296 /* Initialise DirectDraw if not already done.
297 * We do this here because on multi-monitor systems we may have to
298 * re-create the directdraw surfaces. */
299 if( !p_vout->p_sys->p_ddobject &&
300 DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
302 msg_Err( p_vout, "cannot initialize DirectDraw" );
306 /* Create the directx display */
307 if( !p_vout->p_sys->p_display &&
308 DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
310 msg_Err( p_vout, "cannot initialize DirectDraw" );
314 /* Initialize the output structure.
315 * Since DirectDraw can do rescaling for us, stick to the default
316 * coordinates and aspect. */
317 p_vout->output.i_width = p_vout->render.i_width;
318 p_vout->output.i_height = p_vout->render.i_height;
319 p_vout->output.i_aspect = p_vout->render.i_aspect;
321 #define MAX_DIRECTBUFFERS 1
322 /* Right now we use only 1 directbuffer because we don't want the
323 * video decoder to decode directly into direct buffers as they are
324 * created into video memory and video memory is _really_ slow */
326 /* Choose the chroma we will try first. */
327 switch( p_vout->render.i_chroma )
329 case VLC_FOURCC('Y','U','Y','2'):
330 case VLC_FOURCC('Y','U','N','V'):
331 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
333 case VLC_FOURCC('U','Y','V','Y'):
334 case VLC_FOURCC('U','Y','N','V'):
335 case VLC_FOURCC('Y','4','2','2'):
336 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
338 case VLC_FOURCC('Y','V','Y','U'):
339 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
342 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
346 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
348 i_chroma_backup = p_vout->output.i_chroma;
350 if( !I_OUTPUTPICTURES )
352 /* hmmm, it didn't work! Let's try commonly supported chromas */
353 if( p_vout->output.i_chroma != VLC_FOURCC('Y','V','1','2') )
355 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
356 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
358 if( !I_OUTPUTPICTURES )
360 /* hmmm, it still didn't work! Let's try another one */
361 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
362 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
366 if( !I_OUTPUTPICTURES )
368 /* If it still didn't work then don't try to use an overlay */
369 p_vout->output.i_chroma = i_chroma_backup;
370 p_vout->p_sys->b_using_overlay = 0;
371 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
374 /* Change the window title bar text */
375 if( p_vout->p_sys->hparent ) ; /* Do nothing */
376 else PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
381 /*****************************************************************************
382 * End: terminate Sys video thread output method
383 *****************************************************************************
384 * Terminate an output method created by Create.
385 * It is called at the end of the thread.
386 *****************************************************************************/
387 static void End( vout_thread_t *p_vout )
389 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
391 DirectXCloseDisplay( p_vout );
392 DirectXCloseDDraw( p_vout );
397 /*****************************************************************************
398 * CloseVideo: destroy Sys video thread output method
399 *****************************************************************************
400 * Terminate an output method created by Create
401 *****************************************************************************/
402 static void CloseVideo( vlc_object_t *p_this )
404 vout_thread_t * p_vout = (vout_thread_t *)p_this;
406 msg_Dbg( p_vout, "CloseVideo" );
408 var_Destroy( p_vout, "video-on-top" );
410 if( p_vout->p_sys->p_event )
412 vlc_object_detach( p_vout->p_sys->p_event );
414 /* Kill DirectXEventThread */
415 p_vout->p_sys->p_event->b_die = VLC_TRUE;
417 /* we need to be sure DirectXEventThread won't stay stuck in
418 * GetMessage, so we send a fake message */
419 if( p_vout->p_sys->hwnd )
421 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
424 vlc_thread_join( p_vout->p_sys->p_event );
425 vlc_object_destroy( p_vout->p_sys->p_event );
430 free( p_vout->p_sys );
431 p_vout->p_sys = NULL;
435 /*****************************************************************************
436 * Manage: handle Sys events
437 *****************************************************************************
438 * This function should be called regularly by the video output thread.
439 * It returns a non null value if an error occured.
440 *****************************************************************************/
441 static int Manage( vout_thread_t *p_vout )
443 WINDOWPLACEMENT window_placement;
445 /* If we do not control our window, we check for geometry changes
446 * ourselves because the parent might not send us its events. */
447 if( p_vout->p_sys->hparent )
449 DirectXUpdateRects( p_vout, VLC_FALSE );
455 if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
457 if( p_vout->p_sys->b_using_overlay )
458 DirectXUpdateOverlay( p_vout );
460 /* Check if we are still on the same monitor */
461 if( p_vout->p_sys->MonitorFromWindow &&
462 p_vout->p_sys->hmonitor !=
463 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
464 MONITOR_DEFAULTTONEAREST ) )
466 /* This will force the vout core to recreate the picture buffers */
467 p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
470 p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
473 /* We used to call the Win32 PeekMessage function here to read the window
474 * messages. But since window can stay blocked into this function for a
475 * long time (for example when you move your window on the screen), I
476 * decided to isolate PeekMessage in another thread. */
481 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
482 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
486 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
488 /* We need to switch between Maximized and Normal sized window */
489 window_placement.length = sizeof(WINDOWPLACEMENT);
490 GetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
491 if( p_vout->b_fullscreen )
493 /* Maximized window */
494 window_placement.showCmd = SW_SHOWMAXIMIZED;
495 /* Change window style, no borders and no title bar */
496 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, WS_CLIPCHILDREN );
502 window_placement.showCmd = SW_SHOWNORMAL;
503 /* Change window style, borders and title bar */
504 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, WS_CLIPCHILDREN |
505 WS_OVERLAPPEDWINDOW | WS_SIZEBOX | WS_VISIBLE );
508 SetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
510 /* Update the object variable and trigger callback */
511 val.b_bool = p_vout->b_fullscreen;
512 var_Set( p_vout, "fullscreen", val );
514 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
515 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
521 if( (!p_vout->p_sys->b_cursor_hidden) &&
522 ( (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 ) )
524 /* Hide the mouse automatically */
525 if( p_vout->p_sys->hwnd != p_vout->p_sys->hparent )
527 p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
528 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
533 * "Always on top" status change
535 if( p_vout->p_sys->b_on_top_change )
538 HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
540 var_Get( p_vout, "video-on-top", &val );
542 /* Set the window on top if necessary */
543 if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
546 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
547 MF_BYCOMMAND | MFS_CHECKED );
548 SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
549 SWP_NOSIZE | SWP_NOMOVE );
552 /* The window shouldn't be on top */
553 if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
556 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
557 MF_BYCOMMAND | MFS_UNCHECKED );
558 SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
559 SWP_NOSIZE | SWP_NOMOVE );
562 p_vout->p_sys->b_on_top_change = VLC_FALSE;
565 /* Check if the event thread is still running */
566 if( p_vout->p_sys->p_event->b_die )
568 return VLC_EGENERIC; /* exit */
574 /*****************************************************************************
575 * Display: displays previously rendered output
576 *****************************************************************************
577 * This function sends the currently rendered image to the display, wait until
578 * it is displayed and switch the two rendering buffers, preparing next frame.
579 *****************************************************************************/
580 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
584 if( (p_vout->p_sys->p_display == NULL) )
586 msg_Warn( p_vout, "no display!" );
590 /* Our surface can be lost so be sure to check this
591 * and restore it if need be */
592 if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
593 == DDERR_SURFACELOST )
595 if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
596 p_vout->p_sys->b_using_overlay )
597 DirectXUpdateOverlay( p_vout );
600 if( !p_vout->p_sys->b_using_overlay )
604 /* We ask for the "NOTEARING" option */
605 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
606 ddbltfx.dwSize = sizeof(DDBLTFX);
607 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
609 /* Blit video surface to display */
610 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
611 &p_vout->p_sys->rect_dest_clipped,
612 p_pic->p_sys->p_surface,
613 &p_vout->p_sys->rect_src_clipped,
614 DDBLT_ASYNC, &ddbltfx );
615 if( dxresult != DD_OK )
617 msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
622 else /* using overlay */
624 /* Flip the overlay buffers if we are using back buffers */
625 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
630 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
632 if( dxresult != DD_OK )
634 msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
637 /* set currently displayed pic */
638 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
640 /* Lock surface to get all the required info */
641 if( DirectXLockSurface( p_vout, p_pic ) )
644 msg_Warn( p_vout, "cannot lock surface" );
647 DirectXUnlockSurface( p_vout, p_pic );
651 /* following functions are local */
653 /*****************************************************************************
654 * DirectXEnumCallback: Device enumeration
655 *****************************************************************************
656 * This callback function is called by DirectDraw once for each
657 * available DirectDraw device.
658 *****************************************************************************/
659 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
660 LPTSTR psz_drivername, VOID* p_context,
663 vout_thread_t *p_vout = (vout_thread_t *)p_context;
666 var_Get( p_vout, "directx-device", &device );
668 msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
672 if( ( !device.psz_string || !device.psz_string ) &&
673 hmon == p_vout->p_sys->hmonitor )
677 else if( strcmp( psz_drivername, device.psz_string ) == 0 )
679 MONITORINFO monitor_info;
680 monitor_info.cbSize = sizeof( MONITORINFO );
682 if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
686 /* Move window to the right screen */
687 GetWindowRect( p_vout->p_sys->hwnd, &rect );
688 if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
690 rect.left = monitor_info.rcWork.left;
691 rect.top = monitor_info.rcWork.top;
692 msg_Dbg( p_vout, "DirectXEnumCallback: Setting window "
693 "position to %d,%d", rect.left, rect.top );
694 SetWindowPos( p_vout->p_sys->hwnd, NULL,
695 rect.left, rect.top, 0, 0,
696 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
700 p_vout->p_sys->hmonitor = hmon;
702 else return TRUE; /* Keep enumerating */
704 msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
705 p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
706 if( p_vout->p_sys->p_display_driver )
707 memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
710 return TRUE; /* Keep enumerating */
713 /*****************************************************************************
714 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
715 *****************************************************************************
716 * This function initialise and allocate resources for DirectDraw.
717 *****************************************************************************/
718 static int DirectXInitDDraw( vout_thread_t *p_vout )
721 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
722 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
724 LPDIRECTDRAW p_ddobject;
726 msg_Dbg( p_vout, "DirectXInitDDraw" );
728 /* Load direct draw DLL */
729 p_vout->p_sys->hddraw_dll = LoadLibrary("DDRAW.DLL");
730 if( p_vout->p_sys->hddraw_dll == NULL )
732 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
736 OurDirectDrawCreate =
737 (void *)GetProcAddress(p_vout->p_sys->hddraw_dll, "DirectDrawCreate");
738 if( OurDirectDrawCreate == NULL )
740 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
744 OurDirectDrawEnumerateEx =
745 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
746 "DirectDrawEnumerateExA" );
748 if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
752 var_Get( p_vout, "directx-device", &device );
753 msg_Dbg( p_vout, "directx-device: %s", device.psz_string );
755 p_vout->p_sys->hmonitor =
756 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
757 MONITOR_DEFAULTTONEAREST );
759 /* Enumerate displays */
760 OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
761 DDENUM_ATTACHEDSECONDARYDEVICES );
764 /* Initialize DirectDraw now */
765 dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
767 if( dxresult != DD_OK )
769 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
773 /* Get the IDirectDraw2 interface */
774 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
775 (LPVOID *)&p_vout->p_sys->p_ddobject );
776 /* Release the unused interface */
777 IDirectDraw_Release( p_ddobject );
778 if( dxresult != DD_OK )
780 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
784 /* Set DirectDraw Cooperative level, ie what control we want over Windows
786 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
787 NULL, DDSCL_NORMAL );
788 if( dxresult != DD_OK )
790 msg_Err( p_vout, "cannot set direct draw cooperative level" );
794 /* Get the size of the current display device */
795 if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
797 MONITORINFO monitor_info;
798 monitor_info.cbSize = sizeof( MONITORINFO );
799 p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
801 p_vout->p_sys->rect_display = monitor_info.rcMonitor;
805 p_vout->p_sys->rect_display.left = 0;
806 p_vout->p_sys->rect_display.top = 0;
807 p_vout->p_sys->rect_display.right = GetSystemMetrics(SM_CXSCREEN);
808 p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
811 msg_Dbg( p_vout, "screen dimensions (%ix%i,%ix%i)",
812 p_vout->p_sys->rect_display.left,
813 p_vout->p_sys->rect_display.top,
814 p_vout->p_sys->rect_display.right,
815 p_vout->p_sys->rect_display.bottom );
817 /* Probe the capabilities of the hardware */
818 DirectXGetDDrawCaps( p_vout );
820 msg_Dbg( p_vout, "End DirectXInitDDraw" );
824 if( p_vout->p_sys->p_ddobject )
825 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
826 if( p_vout->p_sys->hddraw_dll )
827 FreeLibrary( p_vout->p_sys->hddraw_dll );
828 p_vout->p_sys->hddraw_dll = NULL;
829 p_vout->p_sys->p_ddobject = NULL;
833 /*****************************************************************************
834 * DirectXCreateDisplay: create the DirectDraw display.
835 *****************************************************************************
836 * Create and initialize display according to preferences specified in the vout
838 *****************************************************************************/
839 static int DirectXCreateDisplay( vout_thread_t *p_vout )
843 LPDIRECTDRAWSURFACE p_display;
845 msg_Dbg( p_vout, "DirectXCreateDisplay" );
847 /* Now get the primary surface. This surface is what you actually see
849 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
850 ddsd.dwSize = sizeof(DDSURFACEDESC);
851 ddsd.dwFlags = DDSD_CAPS;
852 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
854 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
855 &ddsd, &p_display, NULL );
856 if( dxresult != DD_OK )
858 msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
862 dxresult = IDirectDrawSurface_QueryInterface( p_display,
863 &IID_IDirectDrawSurface2,
864 (LPVOID *)&p_vout->p_sys->p_display );
865 /* Release the old interface */
866 IDirectDrawSurface_Release( p_display );
867 if ( dxresult != DD_OK )
869 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
870 "(error %li)", dxresult );
874 /* The clipper will be used only in non-overlay mode */
875 DirectXCreateClipper( p_vout );
877 /* Make sure the colorkey will be painted */
878 p_vout->p_sys->i_colorkey = 1;
879 p_vout->p_sys->i_rgb_colorkey =
880 DirectXFindColorkey( p_vout, p_vout->p_sys->i_colorkey );
882 /* Create the actual brush */
883 SetClassLong( p_vout->p_sys->hvideownd, GCL_HBRBACKGROUND,
884 (LONG)CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey ) );
885 InvalidateRect( p_vout->p_sys->hvideownd, NULL, TRUE );
886 DirectXUpdateRects( p_vout, VLC_TRUE );
891 /*****************************************************************************
892 * DirectXCreateClipper: Create a clipper that will be used when blitting the
893 * RGB surface to the main display.
894 *****************************************************************************
895 * This clipper prevents us to modify by mistake anything on the screen
896 * which doesn't belong to our window. For example when a part of our video
897 * window is hidden by another window.
898 *****************************************************************************/
899 static int DirectXCreateClipper( vout_thread_t *p_vout )
903 msg_Dbg( p_vout, "DirectXCreateClipper" );
905 /* Create the clipper */
906 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
907 &p_vout->p_sys->p_clipper, NULL );
908 if( dxresult != DD_OK )
910 msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
914 /* Associate the clipper to the window */
915 dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
916 p_vout->p_sys->hvideownd );
917 if( dxresult != DD_OK )
919 msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
924 /* associate the clipper with the surface */
925 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
926 p_vout->p_sys->p_clipper);
927 if( dxresult != DD_OK )
929 msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
937 if( p_vout->p_sys->p_clipper )
939 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
941 p_vout->p_sys->p_clipper = NULL;
945 /*****************************************************************************
946 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
947 *****************************************************************************
948 * The best method of display is with an YUV overlay because the YUV->RGB
949 * conversion is done in hardware.
950 * You can also create a plain RGB surface.
951 * ( Maybe we could also try an RGB overlay surface, which could have hardware
952 * scaling and which would also be faster in window mode because you don't
953 * need to do any blitting to the main display...)
954 *****************************************************************************/
955 static int DirectXCreateSurface( vout_thread_t *p_vout,
956 LPDIRECTDRAWSURFACE2 *pp_surface_final,
957 int i_chroma, int b_overlay,
961 LPDIRECTDRAWSURFACE p_surface;
964 /* Create the video surface */
967 /* Now try to create the YUV overlay surface.
968 * This overlay will be displayed on top of the primary surface.
969 * A color key is used to determine whether or not the overlay will be
970 * displayed, ie the overlay will be displayed in place of the primary
971 * surface wherever the primary surface will have this color.
972 * The video window has been created with a background of this color so
973 * the overlay will be only displayed on top of this window */
975 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
976 ddsd.dwSize = sizeof(DDSURFACEDESC);
977 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
978 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
979 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
980 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
981 ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
982 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
983 ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
985 ddsd.dwHeight = p_vout->render.i_height;
986 ddsd.dwWidth = p_vout->render.i_width;
987 ddsd.dwBackBufferCount = i_backbuffers;
989 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
990 &ddsd, &p_surface, NULL );
991 if( dxresult != DD_OK )
993 *pp_surface_final = NULL;
1000 vlc_bool_t b_rgb_surface =
1001 ( i_chroma == VLC_FOURCC('R','G','B','2') )
1002 || ( i_chroma == VLC_FOURCC('R','V','1','5') )
1003 || ( i_chroma == VLC_FOURCC('R','V','1','6') )
1004 || ( i_chroma == VLC_FOURCC('R','V','2','4') )
1005 || ( i_chroma == VLC_FOURCC('R','V','3','2') );
1007 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
1008 ddsd.dwSize = sizeof(DDSURFACEDESC);
1009 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1010 ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
1011 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1012 ddsd.dwHeight = p_vout->render.i_height;
1013 ddsd.dwWidth = p_vout->render.i_width;
1015 if( p_vout->p_sys->b_use_sysmem )
1016 ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1018 ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
1020 if( !b_rgb_surface )
1022 ddsd.dwFlags |= DDSD_PIXELFORMAT;
1023 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
1024 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
1027 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1028 &ddsd, &p_surface, NULL );
1029 if( dxresult != DD_OK )
1031 *pp_surface_final = NULL;
1032 return VLC_EGENERIC;
1036 /* Now that the surface is created, try to get a newer DirectX interface */
1037 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
1038 &IID_IDirectDrawSurface2,
1039 (LPVOID *)pp_surface_final );
1040 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
1041 if ( dxresult != DD_OK )
1043 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
1044 "(error %li)", dxresult );
1045 *pp_surface_final = NULL;
1046 return VLC_EGENERIC;
1051 /* Check the overlay is useable as some graphics cards allow creating
1052 * several overlays but only one can be used at one time. */
1053 p_vout->p_sys->p_current_surface = *pp_surface_final;
1054 if( DirectXUpdateOverlay( p_vout ) != VLC_SUCCESS )
1056 IDirectDrawSurface2_Release( *pp_surface_final );
1057 *pp_surface_final = NULL;
1058 msg_Err( p_vout, "overlay unuseable (might already be in use)" );
1059 return VLC_EGENERIC;
1066 /*****************************************************************************
1067 * DirectXUpdateOverlay: Move or resize overlay surface on video display.
1068 *****************************************************************************
1069 * This function is used to move or resize an overlay surface on the screen.
1070 * Ususally the overlay is moved by the user and thus, by a move or resize
1071 * event (in Manage).
1072 *****************************************************************************/
1073 int DirectXUpdateOverlay( vout_thread_t *p_vout )
1079 if( p_vout->p_sys->p_current_surface == NULL ||
1080 !p_vout->p_sys->b_using_overlay )
1081 return VLC_EGENERIC;
1083 /* The new window dimensions should already have been computed by the
1084 * caller of this function */
1086 /* Position and show the overlay */
1087 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
1088 ddofx.dwSize = sizeof(DDOVERLAYFX);
1089 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
1090 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
1092 dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1094 dxresult = IDirectDrawSurface2_UpdateOverlay(
1095 p_vout->p_sys->p_current_surface,
1096 &p_vout->p_sys->rect_src_clipped,
1097 p_vout->p_sys->p_display,
1098 &p_vout->p_sys->rect_dest_clipped,
1100 if(dxresult != DD_OK)
1103 "DirectXUpdateOverlay cannot move or resize overlay" );
1104 return VLC_EGENERIC;
1110 /*****************************************************************************
1111 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
1112 *****************************************************************************
1113 * This function returns all resources allocated by DirectXInitDDraw.
1114 *****************************************************************************/
1115 static void DirectXCloseDDraw( vout_thread_t *p_vout )
1117 msg_Dbg( p_vout, "DirectXCloseDDraw" );
1118 if( p_vout->p_sys->p_ddobject != NULL )
1120 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1121 p_vout->p_sys->p_ddobject = NULL;
1124 if( p_vout->p_sys->hddraw_dll != NULL )
1126 FreeLibrary( p_vout->p_sys->hddraw_dll );
1127 p_vout->p_sys->hddraw_dll = NULL;
1130 if( p_vout->p_sys->p_display_driver != NULL )
1132 free( p_vout->p_sys->p_display_driver );
1133 p_vout->p_sys->p_display_driver = NULL;
1136 p_vout->p_sys->hmonitor = NULL;
1139 /*****************************************************************************
1140 * DirectXCloseDisplay: close and reset the DirectX display device
1141 *****************************************************************************
1142 * This function returns all resources allocated by DirectXCreateDisplay.
1143 *****************************************************************************/
1144 static void DirectXCloseDisplay( vout_thread_t *p_vout )
1146 msg_Dbg( p_vout, "DirectXCloseDisplay" );
1148 if( p_vout->p_sys->p_clipper != NULL )
1150 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1151 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1152 p_vout->p_sys->p_clipper = NULL;
1155 if( p_vout->p_sys->p_display != NULL )
1157 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1158 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1159 p_vout->p_sys->p_display = NULL;
1163 /*****************************************************************************
1164 * DirectXCloseSurface: close the YUV overlay or RGB surface.
1165 *****************************************************************************
1166 * This function returns all resources allocated for the surface.
1167 *****************************************************************************/
1168 static void DirectXCloseSurface( vout_thread_t *p_vout,
1169 LPDIRECTDRAWSURFACE2 p_surface )
1171 msg_Dbg( p_vout, "DirectXCloseSurface" );
1172 if( p_surface != NULL )
1174 IDirectDrawSurface2_Release( p_surface );
1178 /*****************************************************************************
1179 * NewPictureVec: allocate a vector of identical pictures
1180 *****************************************************************************
1181 * Returns 0 on success, -1 otherwise
1182 *****************************************************************************/
1183 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1187 int i_ret = VLC_SUCCESS;
1188 LPDIRECTDRAWSURFACE2 p_surface;
1190 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1191 p_vout->p_sys->b_using_overlay ? "yes" : "no",
1192 (char *)&p_vout->output.i_chroma );
1194 I_OUTPUTPICTURES = 0;
1196 /* First we try to use an YUV overlay surface.
1197 * The overlay surface that we create won't be used to decode directly
1198 * into it because accessing video memory directly is way to slow (remember
1199 * that pictures are decoded macroblock per macroblock). Instead the video
1200 * will be decoded in picture buffers in system memory which will then be
1201 * memcpy() to the overlay surface. */
1202 if( p_vout->p_sys->b_using_overlay )
1204 /* Triple buffering rocks! it doesn't have any processing overhead
1205 * (you don't have to wait for the vsync) and provides for a very nice
1206 * video quality (no tearing). */
1207 if( p_vout->p_sys->b_3buf_overlay )
1208 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1209 p_vout->output.i_chroma,
1210 p_vout->p_sys->b_using_overlay,
1211 2 /* number of backbuffers */ );
1213 if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1215 /* Try to reduce the number of backbuffers */
1216 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1217 p_vout->output.i_chroma,
1218 p_vout->p_sys->b_using_overlay,
1219 0 /* number of backbuffers */ );
1222 if( i_ret == VLC_SUCCESS )
1225 picture_t front_pic;
1226 picture_sys_t front_pic_sys;
1227 front_pic.p_sys = &front_pic_sys;
1229 /* Allocate internal structure */
1230 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1231 if( p_pic[0].p_sys == NULL )
1233 DirectXCloseSurface( p_vout, p_surface );
1237 /* set front buffer */
1238 p_pic[0].p_sys->p_front_surface = p_surface;
1240 /* Get the back buffer */
1241 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1242 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1243 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1244 p_surface, &dds_caps,
1245 &p_pic[0].p_sys->p_surface ) )
1247 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1248 /* front buffer is the same as back buffer */
1249 p_pic[0].p_sys->p_surface = p_surface;
1253 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1254 p_pic[0].p_sys->p_front_surface;
1256 /* Reset the front buffer memory */
1257 if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1260 for( i = 0; i < front_pic.i_planes; i++ )
1261 for( j = 0; j < front_pic.p[i].i_lines; j++)
1262 memset( front_pic.p[i].p_pixels + j *
1263 front_pic.p[i].i_pitch, 127,
1264 front_pic.p[i].i_visible_pitch );
1266 DirectXUnlockSurface( p_vout, &front_pic );
1269 DirectXUpdateOverlay( p_vout );
1270 I_OUTPUTPICTURES = 1;
1271 msg_Dbg( p_vout, "YUV overlay created successfully" );
1275 /* As we can't have an overlay, we'll try to create a plain offscreen
1276 * surface. This surface will reside in video memory because there's a
1277 * better chance then that we'll be able to use some kind of hardware
1278 * acceleration like rescaling, blitting or YUV->RGB conversions.
1279 * We then only need to blit this surface onto the main display when we
1280 * want to display it */
1281 if( !p_vout->p_sys->b_using_overlay )
1283 if( p_vout->p_sys->b_hw_yuv )
1287 vlc_bool_t b_result = VLC_FALSE;
1289 /* Check if the chroma is supported first. This is required
1290 * because a few buggy drivers don't mind creating the surface
1291 * even if they don't know about the chroma. */
1292 if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1293 &i_codes, NULL ) == DD_OK )
1295 pi_codes = malloc( i_codes * sizeof(DWORD) );
1296 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1297 p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
1299 for( i = 0; i < (int)i_codes; i++ )
1301 if( p_vout->output.i_chroma == pi_codes[i] )
1303 b_result = VLC_TRUE;
1311 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1312 p_vout->output.i_chroma,
1314 0 /* no back buffers */ );
1316 p_vout->p_sys->b_hw_yuv = VLC_FALSE;
1319 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1321 /* Our last choice is to use a plain RGB surface */
1322 DDPIXELFORMAT ddpfPixelFormat;
1324 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1325 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1328 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1330 switch( ddpfPixelFormat.dwRGBBitCount )
1332 case 8: /* FIXME: set the palette */
1333 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
1336 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
1339 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
1342 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
1345 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
1348 msg_Err( p_vout, "unknown screen depth" );
1349 return VLC_EGENERIC;
1351 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1352 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1353 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1356 p_vout->p_sys->b_hw_yuv = 0;
1358 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1359 p_vout->output.i_chroma,
1361 0 /* no back buffers */ );
1363 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1365 /* Give it a last try with b_use_sysmem enabled */
1366 p_vout->p_sys->b_use_sysmem = 1;
1368 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1369 p_vout->output.i_chroma,
1371 0 /* no back buffers */ );
1375 if( i_ret == VLC_SUCCESS )
1377 /* Allocate internal structure */
1378 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1379 if( p_pic[0].p_sys == NULL )
1381 DirectXCloseSurface( p_vout, p_surface );
1385 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1388 I_OUTPUTPICTURES = 1;
1390 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1391 (char *)&p_vout->output.i_chroma );
1396 /* Now that we've got all our direct-buffers, we can finish filling in the
1397 * picture_t structures */
1398 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1400 p_pic[i].i_status = DESTROYED_PICTURE;
1401 p_pic[i].i_type = DIRECT_PICTURE;
1402 p_pic[i].pf_lock = DirectXLockSurface;
1403 p_pic[i].pf_unlock = DirectXUnlockSurface;
1404 PP_OUTPUTPICTURE[i] = &p_pic[i];
1406 if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1409 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1410 I_OUTPUTPICTURES = 0;
1411 msg_Err( p_vout, "cannot lock surface" );
1412 return VLC_EGENERIC;
1414 DirectXUnlockSurface( p_vout, &p_pic[i] );
1417 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1418 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1423 /*****************************************************************************
1424 * FreePicture: destroy a picture vector allocated with NewPictureVec
1425 *****************************************************************************
1427 *****************************************************************************/
1428 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1433 for( i = 0; i < i_num_pics; i++ )
1435 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1437 for( i = 0; i < i_num_pics; i++ )
1439 free( p_pic[i].p_sys );
1443 p_vout->p_sys->p_current_surface = 0;
1446 /*****************************************************************************
1447 * UpdatePictureStruct: updates the internal data in the picture_t structure
1448 *****************************************************************************
1449 * This will setup stuff for use by the video_output thread
1450 *****************************************************************************/
1451 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1454 switch( p_vout->output.i_chroma )
1456 case VLC_FOURCC('R','G','B','2'):
1457 case VLC_FOURCC('R','V','1','5'):
1458 case VLC_FOURCC('R','V','1','6'):
1459 case VLC_FOURCC('R','V','2','4'):
1460 case VLC_FOURCC('R','V','3','2'):
1461 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1462 p_pic->p->i_lines = p_vout->output.i_height;
1463 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1464 switch( p_vout->output.i_chroma )
1466 case VLC_FOURCC('R','G','B','2'):
1467 p_pic->p->i_pixel_pitch = 1;
1469 case VLC_FOURCC('R','V','1','5'):
1470 case VLC_FOURCC('R','V','1','6'):
1471 p_pic->p->i_pixel_pitch = 2;
1473 case VLC_FOURCC('R','V','2','4'):
1474 p_pic->p->i_pixel_pitch = 3;
1476 case VLC_FOURCC('R','V','3','2'):
1477 p_pic->p->i_pixel_pitch = 4;
1480 return VLC_EGENERIC;
1482 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1483 p_pic->p->i_pixel_pitch;
1484 p_pic->i_planes = 1;
1487 case VLC_FOURCC('Y','V','1','2'):
1489 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1490 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1491 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1492 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1493 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1494 p_pic->p[Y_PLANE].i_pixel_pitch;
1496 p_pic->V_PIXELS = p_pic->Y_PIXELS
1497 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1498 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1499 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1500 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1501 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1502 p_pic->p[V_PLANE].i_pixel_pitch;
1504 p_pic->U_PIXELS = p_pic->V_PIXELS
1505 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1506 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1507 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1508 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1509 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1510 p_pic->p[U_PLANE].i_pixel_pitch;
1512 p_pic->i_planes = 3;
1515 case VLC_FOURCC('I','Y','U','V'):
1517 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1518 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1519 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1520 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1521 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1522 p_pic->p[Y_PLANE].i_pixel_pitch;
1524 p_pic->U_PIXELS = p_pic->Y_PIXELS
1525 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1526 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1527 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1528 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1529 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1530 p_pic->p[U_PLANE].i_pixel_pitch;
1532 p_pic->V_PIXELS = p_pic->U_PIXELS
1533 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1534 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1535 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1536 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1537 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1538 p_pic->p[V_PLANE].i_pixel_pitch;
1540 p_pic->i_planes = 3;
1543 case VLC_FOURCC('Y','U','Y','2'):
1545 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1546 p_pic->p->i_lines = p_vout->output.i_height;
1547 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1548 p_pic->p->i_pixel_pitch = 2;
1549 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1550 p_pic->p->i_pixel_pitch;
1552 p_pic->i_planes = 1;
1556 /* Unknown chroma, tell the guy to get lost */
1557 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1558 p_vout->output.i_chroma,
1559 (char*)&p_vout->output.i_chroma );
1560 return VLC_EGENERIC;
1566 /*****************************************************************************
1567 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1568 *****************************************************************************
1569 * It is nice to know which features are supported by the hardware so we can
1570 * find ways to optimize our rendering.
1571 *****************************************************************************/
1572 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1577 /* This is just an indication of whether or not we'll support overlay,
1578 * but with this test we don't know if we support YUV overlay */
1579 memset( &ddcaps, 0, sizeof( DDCAPS ));
1580 ddcaps.dwSize = sizeof(DDCAPS);
1581 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1583 if(dxresult != DD_OK )
1585 msg_Warn( p_vout, "cannot get caps" );
1589 BOOL bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1590 bHasColorKey, bCanStretch, bCanBltFourcc;
1592 /* Determine if the hardware supports overlay surfaces */
1593 bHasOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAY) ==
1594 DDCAPS_OVERLAY) ? TRUE : FALSE;
1595 /* Determine if the hardware supports overlay surfaces */
1596 bHasOverlayFourCC = ((ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ==
1597 DDCAPS_OVERLAYFOURCC) ? TRUE : FALSE;
1598 /* Determine if the hardware supports overlay deinterlacing */
1599 bCanDeinterlace = ((ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ==
1601 /* Determine if the hardware supports colorkeying */
1602 bHasColorKey = ((ddcaps.dwCaps & DDCAPS_COLORKEY) ==
1603 DDCAPS_COLORKEY) ? TRUE : FALSE;
1604 /* Determine if the hardware supports scaling of the overlay surface */
1605 bCanStretch = ((ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ==
1606 DDCAPS_OVERLAYSTRETCH) ? TRUE : FALSE;
1607 /* Determine if the hardware supports color conversion during a blit */
1608 bCanBltFourcc = ((ddcaps.dwCaps & DDCAPS_BLTFOURCC ) ==
1609 DDCAPS_BLTFOURCC) ? TRUE : FALSE;
1611 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1612 "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1614 bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1615 bHasColorKey, bCanStretch, bCanBltFourcc );
1617 /* Don't ask for troubles */
1618 if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1622 /*****************************************************************************
1623 * DirectXLockSurface: Lock surface and get picture data pointer
1624 *****************************************************************************
1625 * This function locks a surface and get the surface descriptor which amongst
1626 * other things has the pointer to the picture data.
1627 *****************************************************************************/
1628 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1632 /* Lock the surface to get a valid pointer to the picture buffer */
1633 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1634 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1635 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1636 NULL, &p_pic->p_sys->ddsd,
1637 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1639 if( dxresult != DD_OK )
1641 if( dxresult == DDERR_INVALIDPARAMS )
1643 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1644 * in an invalid params error */
1645 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1646 &p_pic->p_sys->ddsd,
1649 if( dxresult == DDERR_SURFACELOST )
1651 /* Your surface can be lost so be sure
1652 * to check this and restore it if needed */
1654 /* When using overlays with back-buffers, we need to restore
1655 * the front buffer so the back-buffers get restored as well. */
1656 if( p_vout->p_sys->b_using_overlay )
1657 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1659 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1661 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1662 &p_pic->p_sys->ddsd,
1664 if( dxresult == DDERR_SURFACELOST )
1665 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1667 if( dxresult != DD_OK )
1669 return VLC_EGENERIC;
1673 /* Now we have a pointer to the surface memory, we can update our picture
1675 if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma )
1678 DirectXUnlockSurface( p_vout, p_pic );
1679 return VLC_EGENERIC;
1685 /*****************************************************************************
1686 * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1687 *****************************************************************************/
1688 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1690 /* Unlock the Surface */
1691 if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1694 return VLC_EGENERIC;
1697 /*****************************************************************************
1698 * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
1699 *****************************************************************************/
1700 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t i_color )
1705 uint32_t i_pixel_backup;
1708 ddsd.dwSize = sizeof(ddsd);
1709 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1710 &ddsd, DDLOCK_WAIT, NULL );
1711 if( dxresult != DD_OK ) return 0;
1713 i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1715 switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1718 *(uint8_t *)ddsd.lpSurface = 0x11;
1721 *(uint8_t *)ddsd.lpSurface = 0x01;
1724 *(uint16_t *)ddsd.lpSurface = 0x01;
1727 *(uint32_t *)ddsd.lpSurface = 0x01;
1731 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1733 if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1735 i_rgb = GetPixel( hdc, 0, 0 );
1736 IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1739 ddsd.dwSize = sizeof(ddsd);
1740 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1741 &ddsd, DDLOCK_WAIT, NULL );
1742 if( dxresult != DD_OK ) return i_rgb;
1744 *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1746 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1751 /*****************************************************************************
1752 * object variables callbacks: a bunch of object variables are used by the
1753 * interfaces to interact with the vout.
1754 *****************************************************************************/
1755 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1756 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1758 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1759 p_vout->p_sys->b_on_top_change = VLC_TRUE;
1763 /*****************************************************************************
1764 * config variable callback
1765 *****************************************************************************/
1766 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
1767 LPTSTR psz_drivername, VOID* p_context,
1770 module_config_t *p_item = (module_config_t *)p_context;
1773 (char **)realloc( p_item->ppsz_list,
1774 (p_item->i_list+2) * sizeof(char *) );
1775 p_item->ppsz_list_text =
1776 (char **)realloc( p_item->ppsz_list_text,
1777 (p_item->i_list+2) * sizeof(char *) );
1779 p_item->ppsz_list[p_item->i_list] = strdup( psz_drivername );
1780 p_item->ppsz_list_text[p_item->i_list] = NULL;
1782 p_item->ppsz_list[p_item->i_list] = NULL;
1783 p_item->ppsz_list_text[p_item->i_list] = NULL;
1785 return TRUE; /* Keep enumerating */
1788 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1789 vlc_value_t newval, vlc_value_t oldval, void *d)
1791 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
1793 HINSTANCE hddraw_dll;
1795 module_config_t *p_item;
1798 p_item = config_FindConfig( p_this, psz_name );
1799 if( !p_item ) return VLC_SUCCESS;
1801 /* Clear-up the current list */
1802 if( p_item->i_list )
1804 /* Keep the first entry */
1805 for( i = 1; i < p_item->i_list; i++ )
1807 free( p_item->ppsz_list[i] );
1808 free( p_item->ppsz_list_text[i] );
1810 /* TODO: Remove when no more needed */
1811 p_item->ppsz_list[i] = NULL;
1812 p_item->ppsz_list_text[i] = NULL;
1816 /* Load direct draw DLL */
1817 hddraw_dll = LoadLibrary("DDRAW.DLL");
1818 if( hddraw_dll == NULL ) return VLC_SUCCESS;
1820 OurDirectDrawEnumerateEx =
1821 (void *)GetProcAddress( hddraw_dll, "DirectDrawEnumerateExA" );
1823 if( OurDirectDrawEnumerateEx )
1825 /* Enumerate displays */
1826 OurDirectDrawEnumerateEx( DirectXEnumCallback2, p_item,
1827 DDENUM_ATTACHEDSECONDARYDEVICES );
1830 FreeLibrary( hddraw_dll );