1 /*****************************************************************************
2 * directx.c: Windows DirectDraw video output
3 *****************************************************************************
4 * Copyright (C) 2001-2009 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 *****************************************************************************/
43 #include <vlc_common.h>
44 #include <vlc_plugin.h>
46 #include <vlc_playlist.h> /* needed for wallpaper */
47 #include <vlc_memory.h>
50 #include <commctrl.h> /* ListView_(Get|Set)* */
53 # include <multimon.h>
55 #undef GetSystemMetrics
57 #ifndef MONITOR_DEFAULTTONEAREST
58 # define MONITOR_DEFAULTTONEAREST 2
63 /*****************************************************************************
64 * picture_sys_t: direct buffer method descriptor
65 *****************************************************************************
66 * This structure is part of the picture descriptor, it describes the
67 * DirectX specific properties of a direct buffer.
68 *****************************************************************************/
71 LPDIRECTDRAWSURFACE2 p_surface;
72 LPDIRECTDRAWSURFACE2 p_front_surface;
76 /*****************************************************************************
78 * Defining them here allows us to get rid of the dxguid library during
80 *****************************************************************************/
84 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
85 DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
87 /*****************************************************************************
89 *****************************************************************************/
90 static int OpenVideo ( vlc_object_t * );
91 static void CloseVideo ( vlc_object_t * );
93 static int Init ( vout_thread_t * );
94 static void End ( vout_thread_t * );
95 static int Manage ( vout_thread_t * );
96 static void Display ( vout_thread_t *, picture_t * );
97 static void FirstDisplay( vout_thread_t *, picture_t * );
98 static void SetPalette( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
100 static int NewPictureVec ( vout_thread_t *, picture_t * );
101 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
102 static int UpdatePictureStruct( vout_thread_t *, picture_t * );
104 static int DirectXInitDDraw ( vout_thread_t *p_vout );
105 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
106 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
107 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
108 static int DirectXCreateSurface ( vout_thread_t *p_vout,
109 LPDIRECTDRAWSURFACE2 *, int, int, int );
110 static void DirectXCloseSurface ( vout_thread_t *p_vout,
111 LPDIRECTDRAWSURFACE2 );
112 static int DirectXCreateClipper ( vout_thread_t *p_vout );
113 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
114 static int DirectXLockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
115 static int DirectXUnlockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
117 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *i_color );
119 void SwitchWallpaperMode( vout_thread_t *, bool );
121 /* Object variables callbacks */
122 static int FindDevicesCallback( vlc_object_t *, char const *,
123 vlc_value_t, vlc_value_t, void * );
124 static int WallpaperCallback( vlc_object_t *, char const *,
125 vlc_value_t, vlc_value_t, void * );
127 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
128 LPTSTR psz_drivername, VOID* p_context,
131 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
132 LPTSTR psz_drivername, VOID* p_context,
135 /*****************************************************************************
137 *****************************************************************************/
138 #define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
139 #define HW_YUV_LONGTEXT N_( \
140 "Try to use hardware acceleration for YUV->RGB conversions. " \
141 "This option doesn't have any effect when using overlays." )
143 #define SYSMEM_TEXT N_("Use video buffers in system memory")
144 #define SYSMEM_LONGTEXT N_( \
145 "Create video buffers in system memory instead of video memory. This " \
146 "isn't recommended as usually using video memory allows to benefit from " \
147 "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
148 "This option doesn't have any effect when using overlays." )
150 #define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
151 #define TRIPLEBUF_LONGTEXT N_( \
152 "Try to use triple buffering when using YUV overlays. That results in " \
153 "much better video quality (no flickering)." )
155 #define DEVICE_TEXT N_("Name of desired display device")
156 #define DEVICE_LONGTEXT N_("In a multiple monitor configuration, you can " \
157 "specify the Windows device name of the display that you want the video " \
158 "window to open on. For example, \"\\\\.\\DISPLAY1\" or " \
159 "\"\\\\.\\DISPLAY2\"." )
161 #define WALLPAPER_TEXT N_("Enable wallpaper mode ")
162 #define WALLPAPER_LONGTEXT N_( \
163 "The wallpaper mode allows you to display the video as the desktop " \
164 "background. Note that this feature only works in overlay mode and " \
165 "the desktop must not already have a wallpaper." )
167 static const char *const ppsz_dev[] = { "" };
168 static const char *const ppsz_dev_text[] = { N_("Default") };
171 set_shortname( "DirectX" )
172 set_category( CAT_VIDEO )
173 set_subcategory( SUBCAT_VIDEO_VOUT )
174 add_bool( "directx-hw-yuv", true, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT,
176 add_bool( "directx-use-sysmem", false, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT,
178 add_bool( "directx-3buffering", true, NULL, TRIPLEBUF_TEXT,
179 TRIPLEBUF_LONGTEXT, true )
181 add_string( "directx-device", "", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
183 change_string_list( ppsz_dev, ppsz_dev_text, FindDevicesCallback )
184 change_action_add( FindDevicesCallback, N_("Refresh list") )
186 add_bool( "directx-wallpaper", false, NULL, WALLPAPER_TEXT, WALLPAPER_LONGTEXT,
189 set_description( N_("DirectX (DirectDraw) video output") )
190 set_capability( "video output", 100 )
191 add_shortcut( "directx" )
192 set_callbacks( OpenVideo, CloseVideo )
194 /* FIXME: Hack to avoid unregistering our window class */
195 linked_with_a_crap_library_which_uses_atexit ()
199 /* check if we registered a window class because we need to
202 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
203 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
206 /*****************************************************************************
207 * OpenVideo: allocate DirectX video thread output method
208 *****************************************************************************
209 * This function allocates and initialize the DirectX vout method.
210 *****************************************************************************/
211 static int OpenVideo( vlc_object_t *p_this )
213 vout_thread_t * p_vout = (vout_thread_t *)p_this;
217 /* Allocate structure */
218 p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
219 if( p_vout->p_sys == NULL )
221 vlc_mutex_init( &p_vout->p_sys->lock );
223 /* Initialisations */
224 p_vout->pf_init = Init;
225 p_vout->pf_end = End;
226 p_vout->pf_manage = Manage;
227 p_vout->pf_render = NULL;
228 p_vout->pf_display = FirstDisplay;
229 p_vout->pf_control = Control;
231 if( CommonInit( p_vout ) )
235 p_vout->p_sys->p_ddobject = NULL;
236 p_vout->p_sys->p_display = NULL;
237 p_vout->p_sys->p_current_surface = NULL;
238 p_vout->p_sys->p_clipper = NULL;
239 p_vout->p_sys->b_wallpaper = 0;
241 /* Multimonitor stuff */
242 p_vout->p_sys->hmonitor = NULL;
243 p_vout->p_sys->p_display_driver = NULL;
244 p_vout->p_sys->MonitorFromWindow = NULL;
245 p_vout->p_sys->GetMonitorInfo = NULL;
246 if( (huser32 = GetModuleHandle( _T("USER32") ) ) )
248 p_vout->p_sys->MonitorFromWindow = (HMONITOR (WINAPI *)( HWND, DWORD ))
249 GetProcAddress( huser32, _T("MonitorFromWindow") );
250 p_vout->p_sys->GetMonitorInfo =
251 GetProcAddress( huser32, _T("GetMonitorInfoW") );
254 var_Create( p_vout, "overlay", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
255 var_Create( p_vout, "directx-use-sysmem", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
256 var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
257 var_Create( p_vout, "directx-3buffering", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
258 var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
260 /* Initialise DirectDraw */
261 if( DirectXInitDDraw( p_vout ) )
263 msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
267 /* Create the directx display */
268 if( DirectXCreateDisplay( p_vout ) )
270 msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
274 /* Variable to indicate if the window should be on top of others */
275 /* Trigger a callback right now */
276 var_Create( p_vout, "directx-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
277 val.psz_string = _("Wallpaper");
278 var_Change( p_vout, "directx-wallpaper", VLC_VAR_SETTEXT, &val, NULL );
279 var_AddCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
280 var_TriggerCallback( p_vout, "directx-wallpaper" );
285 CloseVideo( VLC_OBJECT(p_vout) );
289 /*****************************************************************************
290 * Init: initialize DirectX video thread output method
291 *****************************************************************************
292 * This function create the directx surfaces needed by the output thread.
293 * It is called at the beginning of the thread.
294 *****************************************************************************/
295 static int Init( vout_thread_t *p_vout )
299 /* Get a few default parameters */
300 p_vout->p_sys->b_using_overlay = var_GetBool( p_vout, "overlay" );
301 p_vout->p_sys->b_use_sysmem = var_GetBool( p_vout, "directx-use-sysmem" );
302 p_vout->p_sys->b_hw_yuv = var_GetBool( p_vout, "directx-hw-yuv" );
303 p_vout->p_sys->b_3buf_overlay = var_GetBool( p_vout, "directx-3buffering" );
305 /* Initialise DirectDraw if not already done.
306 * We do this here because on multi-monitor systems we may have to
307 * re-create the directdraw surfaces. */
308 if( !p_vout->p_sys->p_ddobject &&
309 DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
311 msg_Err( p_vout, "cannot initialize DirectDraw" );
315 /* Create the directx display */
316 if( !p_vout->p_sys->p_display &&
317 DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
319 msg_Err( p_vout, "cannot initialize DirectDraw" );
323 /* Initialize the output structure.
324 * Since DirectDraw can do rescaling for us, stick to the default
325 * coordinates and aspect. */
326 p_vout->output.i_width = p_vout->render.i_width;
327 p_vout->output.i_height = p_vout->render.i_height;
328 p_vout->output.i_aspect = p_vout->render.i_aspect;
329 p_vout->fmt_out = p_vout->fmt_in;
330 UpdateRects( p_vout, true );
332 #define MAX_DIRECTBUFFERS 1
333 /* Right now we use only 1 directbuffer because we don't want the
334 * video decoder to decode directly into direct buffers as they are
335 * created into video memory and video memory is _really_ slow */
337 /* Choose the chroma we will try first. */
338 switch( p_vout->render.i_chroma )
341 p_vout->output.i_chroma = VLC_CODEC_YUYV;
344 p_vout->output.i_chroma = VLC_CODEC_UYVY;
347 p_vout->output.i_chroma = VLC_CODEC_YVYU;
350 p_vout->output.i_chroma = VLC_CODEC_I420;
353 msg_Dbg( p_vout, "use default chroma YV12 for render " \
355 (char *)&p_vout->render.i_chroma);
356 p_vout->output.i_chroma = VLC_CODEC_YV12;
360 NewPictureVec( p_vout, p_vout->p_picture );
362 i_chroma_backup = p_vout->output.i_chroma;
364 if( !I_OUTPUTPICTURES )
366 /* hmmm, it didn't work! Let's try commonly supported chromas */
367 if( p_vout->output.i_chroma != VLC_CODEC_I420 )
369 p_vout->output.i_chroma = VLC_CODEC_YV12;
370 NewPictureVec( p_vout, p_vout->p_picture );
372 if( !I_OUTPUTPICTURES )
374 /* hmmm, it still didn't work! Let's try another one */
375 p_vout->output.i_chroma = VLC_CODEC_YUYV;
376 NewPictureVec( p_vout, p_vout->p_picture );
380 if( !I_OUTPUTPICTURES )
382 /* If it still didn't work then don't try to use an overlay */
383 p_vout->output.i_chroma = i_chroma_backup;
384 p_vout->p_sys->b_using_overlay = false;
385 msg_Warn( p_vout, "Could not initialize directx overlay" ) ;
386 NewPictureVec( p_vout, p_vout->p_picture );
389 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
391 /* Change the window title bar text */
392 const char *psz_fallback;
393 if( p_vout->p_sys->b_using_overlay )
394 psz_fallback = VOUT_TITLE " (hardware YUV overlay DirectX output)";
395 else if( p_vout->p_sys->b_hw_yuv )
396 psz_fallback = VOUT_TITLE " (hardware YUV DirectX output)";
398 psz_fallback = VOUT_TITLE " (software RGB DirectX output)";
399 EventThreadUpdateTitle( p_vout->p_sys->p_event, psz_fallback );
400 EventThreadUseOverlay( p_vout->p_sys->p_event, p_vout->p_sys->b_using_overlay );
405 /*****************************************************************************
406 * End: terminate Sys video thread output method
407 *****************************************************************************
408 * Terminate an output method created by Create.
409 * It is called at the end of the thread.
410 *****************************************************************************/
411 static void End( vout_thread_t *p_vout )
413 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
415 DirectXCloseDisplay( p_vout );
416 DirectXCloseDDraw( p_vout );
421 /*****************************************************************************
422 * CloseVideo: destroy Sys video thread output method
423 *****************************************************************************
424 * Terminate an output method created by Create
425 *****************************************************************************/
426 static void CloseVideo( vlc_object_t *p_this )
428 vout_thread_t * p_vout = (vout_thread_t *)p_this;
430 /* Make sure the wallpaper is restored */
431 var_DelCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
432 SwitchWallpaperMode( p_vout, false );
434 CommonClean( p_vout );
436 vlc_mutex_destroy( &p_vout->p_sys->lock );
437 free( p_vout->p_sys );
440 /*****************************************************************************
441 * Manage: handle Sys events
442 *****************************************************************************
443 * This function should be called regularly by the video output thread.
444 * It returns a non null value if an error occurred.
445 *****************************************************************************/
446 static int Manage( vout_thread_t *p_vout )
448 CommonManage( p_vout );
453 if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
455 p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
457 /* Check if we are still on the same monitor */
458 if( p_vout->p_sys->MonitorFromWindow &&
459 p_vout->p_sys->hmonitor !=
460 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
461 MONITOR_DEFAULTTONEAREST ) )
463 /* This will force the vout core to recreate the picture buffers */
464 p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
468 if( p_vout->p_sys->i_changes & DX_WALLPAPER_CHANGE )
470 SwitchWallpaperMode( p_vout, !p_vout->p_sys->b_wallpaper );
471 p_vout->p_sys->i_changes &= ~DX_WALLPAPER_CHANGE;
472 DirectDrawUpdateOverlay( p_vout );
478 /*****************************************************************************
479 * Display: displays previously rendered output
480 *****************************************************************************
481 * This function sends the currently rendered image to the display, wait until
482 * it is displayed and switch the two rendering buffers, preparing next frame.
483 *****************************************************************************/
484 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
488 if( (p_vout->p_sys->p_display == NULL) )
490 msg_Warn( p_vout, "no display!" );
494 /* Our surface can be lost so be sure to check this
495 * and restore it if need be */
496 if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
497 == DDERR_SURFACELOST )
499 if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
500 p_vout->p_sys->b_using_overlay )
501 DirectDrawUpdateOverlay( p_vout );
504 if( !p_vout->p_sys->b_using_overlay )
508 /* We ask for the "NOTEARING" option */
509 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
510 ddbltfx.dwSize = sizeof(DDBLTFX);
511 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
513 /* Blit video surface to display */
514 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
515 &p_vout->p_sys->rect_dest_clipped,
516 p_pic->p_sys->p_surface,
517 &p_vout->p_sys->rect_src_clipped,
518 DDBLT_ASYNC, &ddbltfx );
519 if( dxresult != DD_OK )
521 msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
525 else /* using overlay */
527 /* Flip the overlay buffers if we are using back buffers */
528 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
533 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
535 if( dxresult != DD_OK )
537 msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
540 /* set currently displayed pic */
541 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
543 /* Lock surface to get all the required info */
544 if( DirectXLockSurface( p_vout, p_pic ) )
547 msg_Warn( p_vout, "cannot lock surface" );
550 DirectXUnlockSurface( p_vout, p_pic );
555 ** this function is only used once when the first picture is received
556 ** this function will show the video window once video is ready for
560 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
562 /* get initial picture rendered on overlay surface */
563 Display(p_vout, p_pic);
565 IDirectDraw_WaitForVerticalBlank(p_vout->p_sys->p_ddobject,
566 DDWAITVB_BLOCKBEGIN, NULL);
568 if( p_vout->p_sys->b_using_overlay )
570 HBRUSH brush = CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey );
571 /* set the colorkey as the backgound brush for the video window */
572 SetClassLongPtr( p_vout->p_sys->hvideownd, GCLP_HBRBACKGROUND, (LONG_PTR)brush );
575 ** Video window is initially hidden, show it now since we got a
578 SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
586 /* use and restores proper display function for further pictures */
587 p_vout->pf_display = Display;
590 /* following functions are local */
592 /*****************************************************************************
593 * DirectXEnumCallback: Device enumeration
594 *****************************************************************************
595 * This callback function is called by DirectDraw once for each
596 * available DirectDraw device.
597 *****************************************************************************/
598 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
599 LPTSTR psz_drivername, VOID* p_context,
602 vout_thread_t *p_vout = (vout_thread_t *)p_context;
605 msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
609 psz_device = var_GetString( p_vout, "directx-device" );
611 if( ( !psz_device || !*psz_device ) &&
612 hmon == p_vout->p_sys->hmonitor )
616 else if( strcmp( psz_drivername, psz_device ) == 0 )
618 MONITORINFO monitor_info;
619 monitor_info.cbSize = sizeof( MONITORINFO );
621 if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
625 /* Move window to the right screen */
626 GetWindowRect( p_vout->p_sys->hwnd, &rect );
627 if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
629 rect.left = monitor_info.rcWork.left;
630 rect.top = monitor_info.rcWork.top;
631 msg_Dbg( p_vout, "DirectXEnumCallback: setting window "
632 "position to %ld,%ld", rect.left, rect.top );
633 SetWindowPos( p_vout->p_sys->hwnd, NULL,
634 rect.left, rect.top, 0, 0,
635 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
639 p_vout->p_sys->hmonitor = hmon;
645 return TRUE; /* Keep enumerating */
648 msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
649 p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
650 if( p_vout->p_sys->p_display_driver )
651 memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
654 return TRUE; /* Keep enumerating */
657 /*****************************************************************************
658 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
659 *****************************************************************************
660 * This function initialise and allocate resources for DirectDraw.
661 *****************************************************************************/
662 static int DirectXInitDDraw( vout_thread_t *p_vout )
665 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
666 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
668 LPDIRECTDRAW p_ddobject;
670 msg_Dbg( p_vout, "DirectXInitDDraw" );
672 /* Load direct draw DLL */
673 p_vout->p_sys->hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
674 if( p_vout->p_sys->hddraw_dll == NULL )
676 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
680 OurDirectDrawCreate =
681 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
682 _T("DirectDrawCreate") );
683 if( OurDirectDrawCreate == NULL )
685 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
689 OurDirectDrawEnumerateEx =
690 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
691 _T("DirectDrawEnumerateExW") );
693 if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
697 psz_device = var_GetString( p_vout, "directx-device" );
700 msg_Dbg( p_vout, "directx-device: %s", psz_device );
704 p_vout->p_sys->hmonitor =
705 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
706 MONITOR_DEFAULTTONEAREST );
708 /* Enumerate displays */
709 OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
710 DDENUM_ATTACHEDSECONDARYDEVICES );
713 /* Initialize DirectDraw now */
714 dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
716 if( dxresult != DD_OK )
718 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
722 /* Get the IDirectDraw2 interface */
723 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
724 &p_vout->p_sys->p_ddobject );
725 /* Release the unused interface */
726 IDirectDraw_Release( p_ddobject );
727 if( dxresult != DD_OK )
729 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
733 /* Set DirectDraw Cooperative level, ie what control we want over Windows
735 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
736 NULL, DDSCL_NORMAL );
737 if( dxresult != DD_OK )
739 msg_Err( p_vout, "cannot set direct draw cooperative level" );
743 /* Get the size of the current display device */
744 if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
746 MONITORINFO monitor_info;
747 monitor_info.cbSize = sizeof( MONITORINFO );
748 p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
750 p_vout->p_sys->rect_display = monitor_info.rcMonitor;
754 p_vout->p_sys->rect_display.left = 0;
755 p_vout->p_sys->rect_display.top = 0;
756 p_vout->p_sys->rect_display.right = GetSystemMetrics(SM_CXSCREEN);
757 p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
760 msg_Dbg( p_vout, "screen dimensions (%lix%li,%lix%li)",
761 p_vout->p_sys->rect_display.left,
762 p_vout->p_sys->rect_display.top,
763 p_vout->p_sys->rect_display.right,
764 p_vout->p_sys->rect_display.bottom );
766 /* Probe the capabilities of the hardware */
767 DirectXGetDDrawCaps( p_vout );
769 msg_Dbg( p_vout, "End DirectXInitDDraw" );
773 if( p_vout->p_sys->p_ddobject )
774 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
775 if( p_vout->p_sys->hddraw_dll )
776 FreeLibrary( p_vout->p_sys->hddraw_dll );
777 p_vout->p_sys->hddraw_dll = NULL;
778 p_vout->p_sys->p_ddobject = NULL;
782 /*****************************************************************************
783 * DirectXCreateDisplay: create the DirectDraw display.
784 *****************************************************************************
785 * Create and initialize display according to preferences specified in the vout
787 *****************************************************************************/
788 static int DirectXCreateDisplay( vout_thread_t *p_vout )
792 LPDIRECTDRAWSURFACE p_display;
794 msg_Dbg( p_vout, "DirectXCreateDisplay" );
796 /* Now get the primary surface. This surface is what you actually see
798 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
799 ddsd.dwSize = sizeof(DDSURFACEDESC);
800 ddsd.dwFlags = DDSD_CAPS;
801 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
803 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
804 &ddsd, &p_display, NULL );
805 if( dxresult != DD_OK )
807 msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
811 dxresult = IDirectDrawSurface_QueryInterface( p_display,
812 &IID_IDirectDrawSurface2,
813 &p_vout->p_sys->p_display );
814 /* Release the old interface */
815 IDirectDrawSurface_Release( p_display );
816 if ( dxresult != DD_OK )
818 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
819 "(error %li)", dxresult );
823 /* The clipper will be used only in non-overlay mode */
824 DirectXCreateClipper( p_vout );
826 /* Make sure the colorkey will be painted */
827 p_vout->p_sys->i_colorkey = 1;
828 p_vout->p_sys->i_rgb_colorkey =
829 DirectXFindColorkey( p_vout, &p_vout->p_sys->i_colorkey );
831 UpdateRects( p_vout, true );
836 /*****************************************************************************
837 * DirectXCreateClipper: Create a clipper that will be used when blitting the
838 * RGB surface to the main display.
839 *****************************************************************************
840 * This clipper prevents us to modify by mistake anything on the screen
841 * which doesn't belong to our window. For example when a part of our video
842 * window is hidden by another window.
843 *****************************************************************************/
844 static int DirectXCreateClipper( vout_thread_t *p_vout )
848 msg_Dbg( p_vout, "DirectXCreateClipper" );
850 /* Create the clipper */
851 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
852 &p_vout->p_sys->p_clipper, NULL );
853 if( dxresult != DD_OK )
855 msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
859 /* Associate the clipper to the window */
860 dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
861 p_vout->p_sys->hvideownd );
862 if( dxresult != DD_OK )
864 msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
869 /* associate the clipper with the surface */
870 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
871 p_vout->p_sys->p_clipper);
872 if( dxresult != DD_OK )
874 msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
882 if( p_vout->p_sys->p_clipper )
884 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
886 p_vout->p_sys->p_clipper = NULL;
890 /*****************************************************************************
891 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
892 *****************************************************************************
893 * The best method of display is with an YUV overlay because the YUV->RGB
894 * conversion is done in hardware.
895 * You can also create a plain RGB surface.
896 * ( Maybe we could also try an RGB overlay surface, which could have hardware
897 * scaling and which would also be faster in window mode because you don't
898 * need to do any blitting to the main display...)
899 *****************************************************************************/
900 static int DirectXCreateSurface( vout_thread_t *p_vout,
901 LPDIRECTDRAWSURFACE2 *pp_surface_final,
902 int i_chroma, int b_overlay,
906 LPDIRECTDRAWSURFACE p_surface;
909 /* Create the video surface */
912 /* Now try to create the YUV overlay surface.
913 * This overlay will be displayed on top of the primary surface.
914 * A color key is used to determine whether or not the overlay will be
915 * displayed, ie the overlay will be displayed in place of the primary
916 * surface wherever the primary surface will have this color.
917 * The video window has been created with a background of this color so
918 * the overlay will be only displayed on top of this window */
920 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
921 ddsd.dwSize = sizeof(DDSURFACEDESC);
922 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
923 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
924 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
925 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
926 ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
927 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
928 ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
930 ddsd.dwHeight = p_vout->render.i_height;
931 ddsd.dwWidth = p_vout->render.i_width;
932 ddsd.dwBackBufferCount = i_backbuffers;
934 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
935 &ddsd, &p_surface, NULL );
936 if( dxresult != DD_OK )
938 *pp_surface_final = NULL;
946 ( i_chroma == VLC_CODEC_RGB8 )
947 || ( i_chroma == VLC_CODEC_RGB15 )
948 || ( i_chroma == VLC_CODEC_RGB16 )
949 || ( i_chroma == VLC_CODEC_RGB24 )
950 || ( i_chroma == VLC_CODEC_RGB32 );
952 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
953 ddsd.dwSize = sizeof(DDSURFACEDESC);
954 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
955 ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
956 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
957 ddsd.dwHeight = p_vout->render.i_height;
958 ddsd.dwWidth = p_vout->render.i_width;
960 if( p_vout->p_sys->b_use_sysmem )
961 ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
963 ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
967 ddsd.dwFlags |= DDSD_PIXELFORMAT;
968 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
969 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
972 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
973 &ddsd, &p_surface, NULL );
974 if( dxresult != DD_OK )
976 *pp_surface_final = NULL;
981 /* Now that the surface is created, try to get a newer DirectX interface */
982 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
983 &IID_IDirectDrawSurface2,
984 (LPVOID *)pp_surface_final );
985 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
986 if ( dxresult != DD_OK )
988 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
989 "(error %li)", dxresult );
990 *pp_surface_final = NULL;
996 /* Check the overlay is useable as some graphics cards allow creating
997 * several overlays but only one can be used at one time. */
998 p_vout->p_sys->p_current_surface = *pp_surface_final;
999 if( DirectDrawUpdateOverlay( p_vout ) != VLC_SUCCESS )
1001 IDirectDrawSurface2_Release( *pp_surface_final );
1002 *pp_surface_final = NULL;
1003 msg_Err( p_vout, "overlay unuseable (might already be in use)" );
1004 return VLC_EGENERIC;
1011 /*****************************************************************************
1012 * DirectDrawUpdateOverlay: Move or resize overlay surface on video display.
1013 *****************************************************************************
1014 * This function is used to move or resize an overlay surface on the screen.
1015 * Ususally the overlay is moved by the user and thus, by a move or resize
1016 * event (in Manage).
1017 *****************************************************************************/
1018 int DirectDrawUpdateOverlay( vout_thread_t *p_vout )
1023 RECT rect_src = p_vout->p_sys->rect_src_clipped;
1024 RECT rect_dest = p_vout->p_sys->rect_dest_clipped;
1026 if( !p_vout->p_sys->b_using_overlay ) return VLC_EGENERIC;
1028 if( p_vout->p_sys->b_wallpaper )
1030 unsigned i_x, i_y, i_width, i_height;
1032 rect_src.left = p_vout->fmt_out.i_x_offset;
1033 rect_src.top = p_vout->fmt_out.i_y_offset;
1034 rect_src.right = rect_src.left + p_vout->fmt_out.i_visible_width;
1035 rect_src.bottom = rect_src.top + p_vout->fmt_out.i_visible_height;
1037 rect_dest = p_vout->p_sys->rect_display;
1038 vout_PlacePicture( p_vout, rect_dest.right, rect_dest.bottom,
1039 &i_x, &i_y, &i_width, &i_height );
1041 rect_dest.left += i_x;
1042 rect_dest.right = rect_dest.left + i_width;
1043 rect_dest.top += i_y;
1044 rect_dest.bottom = rect_dest.top + i_height;
1047 vlc_mutex_lock( &p_vout->p_sys->lock );
1048 if( p_vout->p_sys->p_current_surface == NULL )
1050 vlc_mutex_unlock( &p_vout->p_sys->lock );
1051 return VLC_EGENERIC;
1054 /* The new window dimensions should already have been computed by the
1055 * caller of this function */
1057 /* Position and show the overlay */
1058 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
1059 ddofx.dwSize = sizeof(DDOVERLAYFX);
1060 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
1061 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
1063 dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1065 dxresult = IDirectDrawSurface2_UpdateOverlay(
1066 p_vout->p_sys->p_current_surface,
1067 &rect_src, p_vout->p_sys->p_display, &rect_dest,
1070 vlc_mutex_unlock( &p_vout->p_sys->lock );
1072 if(dxresult != DD_OK)
1074 msg_Warn( p_vout, "DirectDrawUpdateOverlay cannot move/resize overlay" );
1075 return VLC_EGENERIC;
1081 /*****************************************************************************
1082 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
1083 *****************************************************************************
1084 * This function returns all resources allocated by DirectXInitDDraw.
1085 *****************************************************************************/
1086 static void DirectXCloseDDraw( vout_thread_t *p_vout )
1088 msg_Dbg( p_vout, "DirectXCloseDDraw" );
1089 if( p_vout->p_sys->p_ddobject != NULL )
1091 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1092 p_vout->p_sys->p_ddobject = NULL;
1095 if( p_vout->p_sys->hddraw_dll != NULL )
1097 FreeLibrary( p_vout->p_sys->hddraw_dll );
1098 p_vout->p_sys->hddraw_dll = NULL;
1101 free( p_vout->p_sys->p_display_driver );
1102 p_vout->p_sys->p_display_driver = NULL;
1104 p_vout->p_sys->hmonitor = NULL;
1107 /*****************************************************************************
1108 * DirectXCloseDisplay: close and reset the DirectX display device
1109 *****************************************************************************
1110 * This function returns all resources allocated by DirectXCreateDisplay.
1111 *****************************************************************************/
1112 static void DirectXCloseDisplay( vout_thread_t *p_vout )
1114 msg_Dbg( p_vout, "DirectXCloseDisplay" );
1116 if( p_vout->p_sys->p_clipper != NULL )
1118 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1119 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1120 p_vout->p_sys->p_clipper = NULL;
1123 if( p_vout->p_sys->p_display != NULL )
1125 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1126 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1127 p_vout->p_sys->p_display = NULL;
1131 /*****************************************************************************
1132 * DirectXCloseSurface: close the YUV overlay or RGB surface.
1133 *****************************************************************************
1134 * This function returns all resources allocated for the surface.
1135 *****************************************************************************/
1136 static void DirectXCloseSurface( vout_thread_t *p_vout,
1137 LPDIRECTDRAWSURFACE2 p_surface )
1139 msg_Dbg( p_vout, "DirectXCloseSurface" );
1140 if( p_surface != NULL )
1142 IDirectDrawSurface2_Release( p_surface );
1146 /*****************************************************************************
1147 * NewPictureVec: allocate a picture
1148 * FIXME? make it work for i_num_pic pictures...
1149 *****************************************************************************
1150 * Returns 0 on success, -1 otherwise
1151 *****************************************************************************/
1152 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic )
1155 int i_ret = VLC_SUCCESS;
1156 LPDIRECTDRAWSURFACE2 p_surface;
1158 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1159 p_vout->p_sys->b_using_overlay ? "yes" : "no",
1160 (char *)&p_vout->output.i_chroma );
1162 I_OUTPUTPICTURES = 0;
1164 /* First we try to use an YUV overlay surface.
1165 * The overlay surface that we create won't be used to decode directly
1166 * into it because accessing video memory directly is way to slow (remember
1167 * that pictures are decoded macroblock per macroblock). Instead the video
1168 * will be decoded in picture buffers in system memory which will then be
1169 * memcpy() to the overlay surface. */
1170 if( p_vout->p_sys->b_using_overlay )
1172 /* Triple buffering rocks! it doesn't have any processing overhead
1173 * (you don't have to wait for the vsync) and provides for a very nice
1174 * video quality (no tearing). */
1175 if( p_vout->p_sys->b_3buf_overlay )
1176 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1177 p_vout->output.i_chroma,
1178 p_vout->p_sys->b_using_overlay,
1179 2 /* number of backbuffers */ );
1181 if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1183 /* Try to reduce the number of backbuffers */
1184 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1185 p_vout->output.i_chroma,
1186 p_vout->p_sys->b_using_overlay,
1187 0 /* number of backbuffers */ );
1190 if( i_ret == VLC_SUCCESS )
1193 picture_t front_pic;
1194 picture_sys_t front_pic_sys;
1195 front_pic.p_sys = &front_pic_sys;
1197 /* Allocate internal structure */
1198 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1199 if( p_pic[0].p_sys == NULL )
1201 DirectXCloseSurface( p_vout, p_surface );
1205 /* set front buffer */
1206 p_pic[0].p_sys->p_front_surface = p_surface;
1208 /* Get the back buffer */
1209 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1210 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1211 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1212 p_surface, &dds_caps,
1213 &p_pic[0].p_sys->p_surface ) )
1215 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1216 /* front buffer is the same as back buffer */
1217 p_pic[0].p_sys->p_surface = p_surface;
1221 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1222 p_pic[0].p_sys->p_front_surface;
1224 /* Reset the front buffer memory */
1225 if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1228 for( i = 0; i < front_pic.i_planes; i++ )
1229 for( j = 0; j < front_pic.p[i].i_visible_lines; j++)
1230 memset( front_pic.p[i].p_pixels + j *
1231 front_pic.p[i].i_pitch, 127,
1232 front_pic.p[i].i_visible_pitch );
1234 DirectXUnlockSurface( p_vout, &front_pic );
1237 DirectDrawUpdateOverlay( p_vout );
1238 I_OUTPUTPICTURES = 1;
1239 msg_Dbg( p_vout, "YUV overlay created successfully" );
1243 /* As we can't have an overlay, we'll try to create a plain offscreen
1244 * surface. This surface will reside in video memory because there's a
1245 * better chance then that we'll be able to use some kind of hardware
1246 * acceleration like rescaling, blitting or YUV->RGB conversions.
1247 * We then only need to blit this surface onto the main display when we
1248 * want to display it */
1249 if( !p_vout->p_sys->b_using_overlay )
1251 if( p_vout->p_sys->b_hw_yuv )
1255 bool b_result = false;
1257 /* Check if the chroma is supported first. This is required
1258 * because a few buggy drivers don't mind creating the surface
1259 * even if they don't know about the chroma. */
1260 if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1261 &i_codes, NULL ) == DD_OK )
1263 pi_codes = malloc( i_codes * sizeof(DWORD) );
1264 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1265 p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
1267 for( i = 0; i < (int)i_codes; i++ )
1269 if( p_vout->output.i_chroma == pi_codes[i] )
1280 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1281 p_vout->output.i_chroma,
1283 0 /* no back buffers */ );
1285 p_vout->p_sys->b_hw_yuv = false;
1288 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1290 /* Our last choice is to use a plain RGB surface */
1291 DDPIXELFORMAT ddpfPixelFormat;
1293 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1294 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1297 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1299 switch( ddpfPixelFormat.dwRGBBitCount )
1302 p_vout->output.i_chroma = VLC_CODEC_RGB8;
1303 p_vout->output.pf_setpalette = SetPalette;
1306 p_vout->output.i_chroma = VLC_CODEC_RGB15;
1309 p_vout->output.i_chroma = VLC_CODEC_RGB16;
1312 p_vout->output.i_chroma = VLC_CODEC_RGB24;
1315 p_vout->output.i_chroma = VLC_CODEC_RGB32;
1318 msg_Err( p_vout, "unknown screen depth" );
1319 return VLC_EGENERIC;
1321 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1322 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1323 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1326 p_vout->p_sys->b_hw_yuv = 0;
1328 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1329 p_vout->output.i_chroma,
1331 0 /* no back buffers */ );
1333 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1335 /* Give it a last try with b_use_sysmem enabled */
1336 p_vout->p_sys->b_use_sysmem = 1;
1338 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1339 p_vout->output.i_chroma,
1341 0 /* no back buffers */ );
1345 if( i_ret == VLC_SUCCESS )
1347 /* Allocate internal structure */
1348 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1349 if( p_pic[0].p_sys == NULL )
1351 DirectXCloseSurface( p_vout, p_surface );
1355 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1358 I_OUTPUTPICTURES = 1;
1360 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1361 (char *)&p_vout->output.i_chroma );
1366 /* Now that we've got all our direct-buffers, we can finish filling in the
1367 * picture_t structures */
1368 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1370 p_pic[i].i_status = DESTROYED_PICTURE;
1371 p_pic[i].i_type = DIRECT_PICTURE;
1372 p_pic[i].b_slow = true;
1373 p_pic[i].pf_lock = DirectXLockSurface;
1374 p_pic[i].pf_unlock = DirectXUnlockSurface;
1375 PP_OUTPUTPICTURE[i] = &p_pic[i];
1377 if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1380 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1381 I_OUTPUTPICTURES = 0;
1382 msg_Err( p_vout, "cannot lock surface" );
1383 return VLC_EGENERIC;
1385 DirectXUnlockSurface( p_vout, &p_pic[i] );
1388 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1389 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1394 /*****************************************************************************
1395 * FreePicture: destroy a picture vector allocated with NewPictureVec
1396 *****************************************************************************
1398 *****************************************************************************/
1399 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1404 vlc_mutex_lock( &p_vout->p_sys->lock );
1405 p_vout->p_sys->p_current_surface = 0;
1406 vlc_mutex_unlock( &p_vout->p_sys->lock );
1408 for( i = 0; i < i_num_pics; i++ )
1410 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1412 for( i = 0; i < i_num_pics; i++ )
1414 free( p_pic[i].p_sys );
1419 /*****************************************************************************
1420 * UpdatePictureStruct: updates the internal data in the picture_t structure
1421 *****************************************************************************
1422 * This will setup stuff for use by the video_output thread
1423 *****************************************************************************/
1424 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic )
1426 switch( p_vout->output.i_chroma )
1428 case VLC_CODEC_RGB8:
1429 case VLC_CODEC_RGB15:
1430 case VLC_CODEC_RGB16:
1431 case VLC_CODEC_RGB24:
1432 case VLC_CODEC_RGB32:
1433 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1434 p_pic->p->i_lines = p_vout->output.i_height;
1435 p_pic->p->i_visible_lines = p_vout->output.i_height;
1436 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1437 switch( p_vout->output.i_chroma )
1439 case VLC_CODEC_RGB8:
1440 p_pic->p->i_pixel_pitch = 1;
1442 case VLC_CODEC_RGB15:
1443 case VLC_CODEC_RGB16:
1444 p_pic->p->i_pixel_pitch = 2;
1446 case VLC_CODEC_RGB24:
1447 p_pic->p->i_pixel_pitch = 3;
1449 case VLC_CODEC_RGB32:
1450 p_pic->p->i_pixel_pitch = 4;
1453 return VLC_EGENERIC;
1455 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1456 p_pic->p->i_pixel_pitch;
1457 p_pic->i_planes = 1;
1460 case VLC_CODEC_YV12:
1462 /* U and V inverted compared to I420
1463 * Fixme: this should be handled by the vout core */
1464 /* could this be right? */
1465 p_vout->output.i_chroma = VLC_CODEC_I420;
1467 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1468 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1469 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1470 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1471 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1472 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1473 p_pic->p[Y_PLANE].i_pixel_pitch;
1475 p_pic->V_PIXELS = p_pic->Y_PIXELS
1476 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1477 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1478 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1479 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1480 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1481 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1482 p_pic->p[V_PLANE].i_pixel_pitch;
1484 p_pic->U_PIXELS = p_pic->V_PIXELS
1485 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1486 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1487 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1488 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1489 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1490 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1491 p_pic->p[U_PLANE].i_pixel_pitch;
1493 p_pic->i_planes = 3;
1496 case VLC_CODEC_I420:
1498 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1499 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1500 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1501 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1502 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1503 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1504 p_pic->p[Y_PLANE].i_pixel_pitch;
1506 p_pic->U_PIXELS = p_pic->Y_PIXELS
1507 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1508 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1509 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1510 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1511 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1512 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1513 p_pic->p[U_PLANE].i_pixel_pitch;
1515 p_pic->V_PIXELS = p_pic->U_PIXELS
1516 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1517 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1518 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1519 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1520 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1521 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1522 p_pic->p[V_PLANE].i_pixel_pitch;
1524 p_pic->i_planes = 3;
1527 case VLC_CODEC_UYVY:
1528 case VLC_CODEC_YUYV:
1530 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1531 p_pic->p->i_lines = p_vout->output.i_height;
1532 p_pic->p->i_visible_lines = p_vout->output.i_height;
1533 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1534 p_pic->p->i_pixel_pitch = 2;
1535 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1536 p_pic->p->i_pixel_pitch;
1538 p_pic->i_planes = 1;
1542 /* Unknown chroma, tell the guy to get lost */
1543 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1544 p_vout->output.i_chroma,
1545 (char*)&p_vout->output.i_chroma );
1546 return VLC_EGENERIC;
1552 /*****************************************************************************
1553 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1554 *****************************************************************************
1555 * It is nice to know which features are supported by the hardware so we can
1556 * find ways to optimize our rendering.
1557 *****************************************************************************/
1558 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1563 /* This is just an indication of whether or not we'll support overlay,
1564 * but with this test we don't know if we support YUV overlay */
1565 memset( &ddcaps, 0, sizeof( DDCAPS ));
1566 ddcaps.dwSize = sizeof(DDCAPS);
1567 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1569 if(dxresult != DD_OK )
1571 msg_Warn( p_vout, "cannot get caps" );
1575 bool bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1576 bHasColorKey, bCanStretch, bCanBltFourcc,
1577 bAlignBoundarySrc, bAlignBoundaryDest,
1578 bAlignSizeSrc, bAlignSizeDest;
1580 /* Determine if the hardware supports overlay surfaces */
1581 bHasOverlay = (ddcaps.dwCaps & DDCAPS_OVERLAY) ? 1 : 0;
1582 /* Determine if the hardware supports overlay surfaces */
1583 bHasOverlayFourCC = (ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ? 1 : 0;
1584 /* Determine if the hardware supports overlay deinterlacing */
1585 bCanDeinterlace = (ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ? 1 : 0;
1586 /* Determine if the hardware supports colorkeying */
1587 bHasColorKey = (ddcaps.dwCaps & DDCAPS_COLORKEY) ? 1 : 0;
1588 /* Determine if the hardware supports scaling of the overlay surface */
1589 bCanStretch = (ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ? 1 : 0;
1590 /* Determine if the hardware supports color conversion during a blit */
1591 bCanBltFourcc = (ddcaps.dwCaps & DDCAPS_BLTFOURCC) ? 1 : 0;
1592 /* Determine overlay source boundary alignment */
1593 bAlignBoundarySrc = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) ? 1 : 0;
1594 /* Determine overlay destination boundary alignment */
1595 bAlignBoundaryDest = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) ? 1:0;
1596 /* Determine overlay destination size alignment */
1597 bAlignSizeSrc = (ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC) ? 1 : 0;
1598 /* Determine overlay destination size alignment */
1599 bAlignSizeDest = (ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST) ? 1 : 0;
1601 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1602 "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1604 bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1605 bHasColorKey, bCanStretch, bCanBltFourcc );
1607 if( bAlignBoundarySrc || bAlignBoundaryDest ||
1608 bAlignSizeSrc || bAlignSizeDest )
1610 if( bAlignBoundarySrc ) p_vout->p_sys->i_align_src_boundary =
1611 ddcaps.dwAlignBoundarySrc;
1612 if( bAlignBoundaryDest ) p_vout->p_sys->i_align_dest_boundary =
1613 ddcaps.dwAlignBoundaryDest;
1614 if( bAlignSizeDest ) p_vout->p_sys->i_align_src_size =
1615 ddcaps.dwAlignSizeSrc;
1616 if( bAlignSizeDest ) p_vout->p_sys->i_align_dest_size =
1617 ddcaps.dwAlignSizeDest;
1619 msg_Dbg( p_vout, "align_boundary_src=%i,%i "
1620 "align_boundary_dest=%i,%i "
1621 "align_size_src=%i,%i align_size_dest=%i,%i",
1622 bAlignBoundarySrc, p_vout->p_sys->i_align_src_boundary,
1623 bAlignBoundaryDest, p_vout->p_sys->i_align_dest_boundary,
1624 bAlignSizeSrc, p_vout->p_sys->i_align_src_size,
1625 bAlignSizeDest, p_vout->p_sys->i_align_dest_size );
1628 /* Don't ask for troubles */
1629 if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1633 /*****************************************************************************
1634 * DirectXLockSurface: Lock surface and get picture data pointer
1635 *****************************************************************************
1636 * This function locks a surface and get the surface descriptor which amongst
1637 * other things has the pointer to the picture data.
1638 *****************************************************************************/
1639 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1643 /* Lock the surface to get a valid pointer to the picture buffer */
1644 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1645 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1646 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1647 NULL, &p_pic->p_sys->ddsd,
1648 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1650 if( dxresult != DD_OK )
1652 if( dxresult == DDERR_INVALIDPARAMS )
1654 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1655 * in an invalid params error */
1656 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1657 &p_pic->p_sys->ddsd,
1660 if( dxresult == DDERR_SURFACELOST )
1662 /* Your surface can be lost so be sure
1663 * to check this and restore it if needed */
1665 /* When using overlays with back-buffers, we need to restore
1666 * the front buffer so the back-buffers get restored as well. */
1667 if( p_vout->p_sys->b_using_overlay )
1668 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1670 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1672 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1673 &p_pic->p_sys->ddsd,
1676 if( dxresult == DDERR_SURFACELOST )
1677 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1680 if( dxresult != DD_OK )
1682 return VLC_EGENERIC;
1686 /* Now we have a pointer to the surface memory, we can update our picture
1688 if( UpdatePictureStruct( p_vout, p_pic )
1691 DirectXUnlockSurface( p_vout, p_pic );
1692 return VLC_EGENERIC;
1698 /*****************************************************************************
1699 * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1700 *****************************************************************************/
1701 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1703 VLC_UNUSED( p_vout );
1705 /* Unlock the Surface */
1706 if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1709 return VLC_EGENERIC;
1712 /*****************************************************************************
1713 * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
1714 *****************************************************************************/
1715 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *pi_color )
1720 uint32_t i_pixel_backup;
1723 ddsd.dwSize = sizeof(ddsd);
1724 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1725 &ddsd, DDLOCK_WAIT, NULL );
1726 if( dxresult != DD_OK ) return 0;
1728 i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1730 switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1733 *(uint8_t *)ddsd.lpSurface = *pi_color | (*pi_color << 4);
1736 *(uint8_t *)ddsd.lpSurface = *pi_color;
1740 *(uint16_t *)ddsd.lpSurface = *pi_color;
1743 /* Seems to be problematic so we'll just put black as the colorkey */
1746 *(uint32_t *)ddsd.lpSurface = *pi_color;
1750 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1752 if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1754 i_rgb = GetPixel( hdc, 0, 0 );
1755 IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1758 ddsd.dwSize = sizeof(ddsd);
1759 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1760 &ddsd, DDLOCK_WAIT, NULL );
1761 if( dxresult != DD_OK ) return i_rgb;
1763 *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1765 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1770 /*****************************************************************************
1771 * A few toolbox functions
1772 *****************************************************************************/
1773 void SwitchWallpaperMode( vout_thread_t *p_vout, bool b_on )
1777 if( p_vout->p_sys->b_wallpaper == b_on ) return; /* Nothing to do */
1779 hwnd = FindWindow( _T("Progman"), NULL );
1780 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
1781 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
1784 msg_Warn( p_vout, "couldn't find \"SysListView32\" window, "
1785 "wallpaper mode not supported" );
1789 p_vout->p_sys->b_wallpaper = b_on;
1791 msg_Dbg( p_vout, "wallpaper mode %s", b_on ? "enabled" : "disabled" );
1793 if( p_vout->p_sys->b_wallpaper )
1795 p_vout->p_sys->color_bkg = ListView_GetBkColor( hwnd );
1796 p_vout->p_sys->color_bkgtxt = ListView_GetTextBkColor( hwnd );
1798 ListView_SetBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1799 ListView_SetTextBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1803 ListView_SetBkColor( hwnd, p_vout->p_sys->color_bkg );
1804 ListView_SetTextBkColor( hwnd, p_vout->p_sys->color_bkgtxt );
1807 /* Update desktop */
1808 InvalidateRect( hwnd, NULL, TRUE );
1809 UpdateWindow( hwnd );
1812 /*****************************************************************************
1813 * config variable callback
1814 *****************************************************************************/
1815 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
1816 LPTSTR psz_drivername, VOID* p_context,
1819 VLC_UNUSED( p_guid ); VLC_UNUSED( psz_desc ); VLC_UNUSED( hmon );
1821 module_config_t *p_item = (module_config_t *)p_context;
1823 p_item->ppsz_list = realloc_or_free( p_item->ppsz_list,
1824 (p_item->i_list+2) * sizeof(char *) );
1825 assert( p_item->ppsz_list );
1826 p_item->ppsz_list_text = realloc_or_free( p_item->ppsz_list_text,
1827 (p_item->i_list+2) * sizeof(char *) );
1828 assert( p_item->ppsz_list_text );
1830 p_item->ppsz_list[p_item->i_list] = strdup( psz_drivername );
1831 p_item->ppsz_list_text[p_item->i_list] = NULL;
1833 p_item->ppsz_list[p_item->i_list] = NULL;
1834 p_item->ppsz_list_text[p_item->i_list] = NULL;
1836 return TRUE; /* Keep enumerating */
1839 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1840 vlc_value_t newval, vlc_value_t oldval, void *d)
1842 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( d );
1844 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
1846 HINSTANCE hddraw_dll;
1848 module_config_t *p_item;
1851 p_item = config_FindConfig( p_this, psz_name );
1852 if( !p_item ) return VLC_SUCCESS;
1854 /* Clear-up the current list */
1855 if( p_item->i_list )
1857 /* Keep the first entry */
1858 for( i = 1; i < p_item->i_list; i++ )
1860 free( p_item->ppsz_list[i] );
1861 free( p_item->ppsz_list_text[i] );
1863 /* TODO: Remove when no more needed */
1864 p_item->ppsz_list[i] = NULL;
1865 p_item->ppsz_list_text[i] = NULL;
1869 /* Load direct draw DLL */
1870 hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
1871 if( hddraw_dll == NULL ) return VLC_SUCCESS;
1873 OurDirectDrawEnumerateEx =
1874 (void *)GetProcAddress( hddraw_dll, _T("DirectDrawEnumerateExW") );
1876 if( OurDirectDrawEnumerateEx )
1878 /* Enumerate displays */
1879 OurDirectDrawEnumerateEx( DirectXEnumCallback2, p_item,
1880 DDENUM_ATTACHEDSECONDARYDEVICES );
1883 FreeLibrary( hddraw_dll );
1885 /* Signal change to the interface */
1886 p_item->b_dirty = true;
1891 static int WallpaperCallback( vlc_object_t *p_this, char const *psz_cmd,
1892 vlc_value_t oldval, vlc_value_t newval,
1895 VLC_UNUSED( psz_cmd ); VLC_UNUSED( oldval ); VLC_UNUSED( p_data );
1896 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1898 if( (newval.b_bool && !p_vout->p_sys->b_wallpaper) ||
1899 (!newval.b_bool && p_vout->p_sys->b_wallpaper) )
1901 playlist_t *p_playlist = pl_Hold( p_vout );
1905 /* Modify playlist as well because the vout might have to be
1907 var_Create( p_playlist, "directx-wallpaper", VLC_VAR_BOOL );
1908 var_Set( p_playlist, "directx-wallpaper", newval );
1909 pl_Release( p_vout );
1912 p_vout->p_sys->i_changes |= DX_WALLPAPER_CHANGE;
1918 /*****************************************************************************
1919 * SetPalette: sets an 8 bpp palette
1920 *****************************************************************************/
1921 static void SetPalette( vout_thread_t *p_vout,
1922 uint16_t *red, uint16_t *green, uint16_t *blue )
1924 VLC_UNUSED( red ); VLC_UNUSED( green );VLC_UNUSED( blue );
1925 msg_Err( p_vout, "FIXME: SetPalette unimplemented" );