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 *****************************************************************************/
38 #include <errno.h> /* ENOMEM */
44 #include <vlc_common.h>
45 #include <vlc_plugin.h>
47 #include <vlc_playlist.h> /* needed for wallpaper */
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 = 0;
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 );
404 /*****************************************************************************
405 * End: terminate Sys video thread output method
406 *****************************************************************************
407 * Terminate an output method created by Create.
408 * It is called at the end of the thread.
409 *****************************************************************************/
410 static void End( vout_thread_t *p_vout )
412 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
414 DirectXCloseDisplay( p_vout );
415 DirectXCloseDDraw( p_vout );
420 /*****************************************************************************
421 * CloseVideo: destroy Sys video thread output method
422 *****************************************************************************
423 * Terminate an output method created by Create
424 *****************************************************************************/
425 static void CloseVideo( vlc_object_t *p_this )
427 vout_thread_t * p_vout = (vout_thread_t *)p_this;
429 /* Make sure the wallpaper is restored */
430 var_DelCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
431 SwitchWallpaperMode( p_vout, false );
433 CommonClean( p_vout );
435 vlc_mutex_destroy( &p_vout->p_sys->lock );
436 free( p_vout->p_sys );
439 /*****************************************************************************
440 * Manage: handle Sys events
441 *****************************************************************************
442 * This function should be called regularly by the video output thread.
443 * It returns a non null value if an error occurred.
444 *****************************************************************************/
445 static int Manage( vout_thread_t *p_vout )
447 CommonManage( p_vout );
452 if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
454 p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
456 /* Check if we are still on the same monitor */
457 if( p_vout->p_sys->MonitorFromWindow &&
458 p_vout->p_sys->hmonitor !=
459 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
460 MONITOR_DEFAULTTONEAREST ) )
462 /* This will force the vout core to recreate the picture buffers */
463 p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
467 if( p_vout->p_sys->i_changes & DX_WALLPAPER_CHANGE )
469 SwitchWallpaperMode( p_vout, !p_vout->p_sys->b_wallpaper );
470 p_vout->p_sys->i_changes &= ~DX_WALLPAPER_CHANGE;
471 DirectDrawUpdateOverlay( p_vout );
477 /*****************************************************************************
478 * Display: displays previously rendered output
479 *****************************************************************************
480 * This function sends the currently rendered image to the display, wait until
481 * it is displayed and switch the two rendering buffers, preparing next frame.
482 *****************************************************************************/
483 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
487 if( (p_vout->p_sys->p_display == NULL) )
489 msg_Warn( p_vout, "no display!" );
493 /* Our surface can be lost so be sure to check this
494 * and restore it if need be */
495 if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
496 == DDERR_SURFACELOST )
498 if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
499 p_vout->p_sys->b_using_overlay )
500 DirectDrawUpdateOverlay( p_vout );
503 if( !p_vout->p_sys->b_using_overlay )
507 /* We ask for the "NOTEARING" option */
508 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
509 ddbltfx.dwSize = sizeof(DDBLTFX);
510 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
512 /* Blit video surface to display */
513 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
514 &p_vout->p_sys->rect_dest_clipped,
515 p_pic->p_sys->p_surface,
516 &p_vout->p_sys->rect_src_clipped,
517 DDBLT_ASYNC, &ddbltfx );
518 if( dxresult != DD_OK )
520 msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
524 else /* using overlay */
526 /* Flip the overlay buffers if we are using back buffers */
527 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
532 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
534 if( dxresult != DD_OK )
536 msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
539 /* set currently displayed pic */
540 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
542 /* Lock surface to get all the required info */
543 if( DirectXLockSurface( p_vout, p_pic ) )
546 msg_Warn( p_vout, "cannot lock surface" );
549 DirectXUnlockSurface( p_vout, p_pic );
554 ** this function is only used once when the first picture is received
555 ** this function will show the video window once video is ready for
559 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
561 /* get initial picture rendered on overlay surface */
562 Display(p_vout, p_pic);
564 IDirectDraw_WaitForVerticalBlank(p_vout->p_sys->p_ddobject,
565 DDWAITVB_BLOCKBEGIN, NULL);
567 if( p_vout->p_sys->b_using_overlay )
569 HBRUSH brush = CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey );
570 /* set the colorkey as the backgound brush for the video window */
571 SetClassLongPtr( p_vout->p_sys->hvideownd, GCLP_HBRBACKGROUND, (LONG_PTR)brush );
574 ** Video window is initially hidden, show it now since we got a
577 SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
585 /* use and restores proper display function for further pictures */
586 p_vout->pf_display = Display;
589 /* following functions are local */
591 /*****************************************************************************
592 * DirectXEnumCallback: Device enumeration
593 *****************************************************************************
594 * This callback function is called by DirectDraw once for each
595 * available DirectDraw device.
596 *****************************************************************************/
597 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
598 LPTSTR psz_drivername, VOID* p_context,
601 vout_thread_t *p_vout = (vout_thread_t *)p_context;
604 msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
608 var_Get( p_vout, "directx-device", &device );
610 if( ( !device.psz_string || !*device.psz_string ) &&
611 hmon == p_vout->p_sys->hmonitor )
613 free( device.psz_string );
615 else if( strcmp( psz_drivername, device.psz_string ) == 0 )
617 MONITORINFO monitor_info;
618 monitor_info.cbSize = sizeof( MONITORINFO );
620 if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
624 /* Move window to the right screen */
625 GetWindowRect( p_vout->p_sys->hwnd, &rect );
626 if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
628 rect.left = monitor_info.rcWork.left;
629 rect.top = monitor_info.rcWork.top;
630 msg_Dbg( p_vout, "DirectXEnumCallback: setting window "
631 "position to %ld,%ld", rect.left, rect.top );
632 SetWindowPos( p_vout->p_sys->hwnd, NULL,
633 rect.left, rect.top, 0, 0,
634 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
638 p_vout->p_sys->hmonitor = hmon;
639 free( device.psz_string );
643 free( device.psz_string );
644 return TRUE; /* Keep enumerating */
647 msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
648 p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
649 if( p_vout->p_sys->p_display_driver )
650 memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
653 return TRUE; /* Keep enumerating */
656 /*****************************************************************************
657 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
658 *****************************************************************************
659 * This function initialise and allocate resources for DirectDraw.
660 *****************************************************************************/
661 static int DirectXInitDDraw( vout_thread_t *p_vout )
664 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
665 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
667 LPDIRECTDRAW p_ddobject;
669 msg_Dbg( p_vout, "DirectXInitDDraw" );
671 /* Load direct draw DLL */
672 p_vout->p_sys->hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
673 if( p_vout->p_sys->hddraw_dll == NULL )
675 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
679 OurDirectDrawCreate =
680 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
681 _T("DirectDrawCreate") );
682 if( OurDirectDrawCreate == NULL )
684 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
688 OurDirectDrawEnumerateEx =
689 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
690 _T("DirectDrawEnumerateExW") );
692 if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
696 var_Get( p_vout, "directx-device", &device );
697 if( device.psz_string )
699 msg_Dbg( p_vout, "directx-device: %s", device.psz_string );
700 free( device.psz_string );
703 p_vout->p_sys->hmonitor =
704 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
705 MONITOR_DEFAULTTONEAREST );
707 /* Enumerate displays */
708 OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
709 DDENUM_ATTACHEDSECONDARYDEVICES );
712 /* Initialize DirectDraw now */
713 dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
715 if( dxresult != DD_OK )
717 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
721 /* Get the IDirectDraw2 interface */
722 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
723 &p_vout->p_sys->p_ddobject );
724 /* Release the unused interface */
725 IDirectDraw_Release( p_ddobject );
726 if( dxresult != DD_OK )
728 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
732 /* Set DirectDraw Cooperative level, ie what control we want over Windows
734 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
735 NULL, DDSCL_NORMAL );
736 if( dxresult != DD_OK )
738 msg_Err( p_vout, "cannot set direct draw cooperative level" );
742 /* Get the size of the current display device */
743 if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
745 MONITORINFO monitor_info;
746 monitor_info.cbSize = sizeof( MONITORINFO );
747 p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
749 p_vout->p_sys->rect_display = monitor_info.rcMonitor;
753 p_vout->p_sys->rect_display.left = 0;
754 p_vout->p_sys->rect_display.top = 0;
755 p_vout->p_sys->rect_display.right = GetSystemMetrics(SM_CXSCREEN);
756 p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
759 msg_Dbg( p_vout, "screen dimensions (%lix%li,%lix%li)",
760 p_vout->p_sys->rect_display.left,
761 p_vout->p_sys->rect_display.top,
762 p_vout->p_sys->rect_display.right,
763 p_vout->p_sys->rect_display.bottom );
765 /* Probe the capabilities of the hardware */
766 DirectXGetDDrawCaps( p_vout );
768 msg_Dbg( p_vout, "End DirectXInitDDraw" );
772 if( p_vout->p_sys->p_ddobject )
773 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
774 if( p_vout->p_sys->hddraw_dll )
775 FreeLibrary( p_vout->p_sys->hddraw_dll );
776 p_vout->p_sys->hddraw_dll = NULL;
777 p_vout->p_sys->p_ddobject = NULL;
781 /*****************************************************************************
782 * DirectXCreateDisplay: create the DirectDraw display.
783 *****************************************************************************
784 * Create and initialize display according to preferences specified in the vout
786 *****************************************************************************/
787 static int DirectXCreateDisplay( vout_thread_t *p_vout )
791 LPDIRECTDRAWSURFACE p_display;
793 msg_Dbg( p_vout, "DirectXCreateDisplay" );
795 /* Now get the primary surface. This surface is what you actually see
797 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
798 ddsd.dwSize = sizeof(DDSURFACEDESC);
799 ddsd.dwFlags = DDSD_CAPS;
800 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
802 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
803 &ddsd, &p_display, NULL );
804 if( dxresult != DD_OK )
806 msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
810 dxresult = IDirectDrawSurface_QueryInterface( p_display,
811 &IID_IDirectDrawSurface2,
812 &p_vout->p_sys->p_display );
813 /* Release the old interface */
814 IDirectDrawSurface_Release( p_display );
815 if ( dxresult != DD_OK )
817 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
818 "(error %li)", dxresult );
822 /* The clipper will be used only in non-overlay mode */
823 DirectXCreateClipper( p_vout );
825 /* Make sure the colorkey will be painted */
826 p_vout->p_sys->i_colorkey = 1;
827 p_vout->p_sys->i_rgb_colorkey =
828 DirectXFindColorkey( p_vout, &p_vout->p_sys->i_colorkey );
830 UpdateRects( p_vout, true );
835 /*****************************************************************************
836 * DirectXCreateClipper: Create a clipper that will be used when blitting the
837 * RGB surface to the main display.
838 *****************************************************************************
839 * This clipper prevents us to modify by mistake anything on the screen
840 * which doesn't belong to our window. For example when a part of our video
841 * window is hidden by another window.
842 *****************************************************************************/
843 static int DirectXCreateClipper( vout_thread_t *p_vout )
847 msg_Dbg( p_vout, "DirectXCreateClipper" );
849 /* Create the clipper */
850 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
851 &p_vout->p_sys->p_clipper, NULL );
852 if( dxresult != DD_OK )
854 msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
858 /* Associate the clipper to the window */
859 dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
860 p_vout->p_sys->hvideownd );
861 if( dxresult != DD_OK )
863 msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
868 /* associate the clipper with the surface */
869 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
870 p_vout->p_sys->p_clipper);
871 if( dxresult != DD_OK )
873 msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
881 if( p_vout->p_sys->p_clipper )
883 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
885 p_vout->p_sys->p_clipper = NULL;
889 /*****************************************************************************
890 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
891 *****************************************************************************
892 * The best method of display is with an YUV overlay because the YUV->RGB
893 * conversion is done in hardware.
894 * You can also create a plain RGB surface.
895 * ( Maybe we could also try an RGB overlay surface, which could have hardware
896 * scaling and which would also be faster in window mode because you don't
897 * need to do any blitting to the main display...)
898 *****************************************************************************/
899 static int DirectXCreateSurface( vout_thread_t *p_vout,
900 LPDIRECTDRAWSURFACE2 *pp_surface_final,
901 int i_chroma, int b_overlay,
905 LPDIRECTDRAWSURFACE p_surface;
908 /* Create the video surface */
911 /* Now try to create the YUV overlay surface.
912 * This overlay will be displayed on top of the primary surface.
913 * A color key is used to determine whether or not the overlay will be
914 * displayed, ie the overlay will be displayed in place of the primary
915 * surface wherever the primary surface will have this color.
916 * The video window has been created with a background of this color so
917 * the overlay will be only displayed on top of this window */
919 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
920 ddsd.dwSize = sizeof(DDSURFACEDESC);
921 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
922 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
923 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
924 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
925 ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
926 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
927 ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
929 ddsd.dwHeight = p_vout->render.i_height;
930 ddsd.dwWidth = p_vout->render.i_width;
931 ddsd.dwBackBufferCount = i_backbuffers;
933 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
934 &ddsd, &p_surface, NULL );
935 if( dxresult != DD_OK )
937 *pp_surface_final = NULL;
945 ( i_chroma == VLC_CODEC_RGB8 )
946 || ( i_chroma == VLC_CODEC_RGB15 )
947 || ( i_chroma == VLC_CODEC_RGB16 )
948 || ( i_chroma == VLC_CODEC_RGB24 )
949 || ( i_chroma == VLC_CODEC_RGB32 );
951 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
952 ddsd.dwSize = sizeof(DDSURFACEDESC);
953 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
954 ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
955 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
956 ddsd.dwHeight = p_vout->render.i_height;
957 ddsd.dwWidth = p_vout->render.i_width;
959 if( p_vout->p_sys->b_use_sysmem )
960 ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
962 ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
966 ddsd.dwFlags |= DDSD_PIXELFORMAT;
967 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
968 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
971 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
972 &ddsd, &p_surface, NULL );
973 if( dxresult != DD_OK )
975 *pp_surface_final = NULL;
980 /* Now that the surface is created, try to get a newer DirectX interface */
981 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
982 &IID_IDirectDrawSurface2,
983 (LPVOID *)pp_surface_final );
984 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
985 if ( dxresult != DD_OK )
987 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
988 "(error %li)", dxresult );
989 *pp_surface_final = NULL;
995 /* Check the overlay is useable as some graphics cards allow creating
996 * several overlays but only one can be used at one time. */
997 p_vout->p_sys->p_current_surface = *pp_surface_final;
998 if( DirectDrawUpdateOverlay( p_vout ) != VLC_SUCCESS )
1000 IDirectDrawSurface2_Release( *pp_surface_final );
1001 *pp_surface_final = NULL;
1002 msg_Err( p_vout, "overlay unuseable (might already be in use)" );
1003 return VLC_EGENERIC;
1010 /*****************************************************************************
1011 * DirectDrawUpdateOverlay: Move or resize overlay surface on video display.
1012 *****************************************************************************
1013 * This function is used to move or resize an overlay surface on the screen.
1014 * Ususally the overlay is moved by the user and thus, by a move or resize
1015 * event (in Manage).
1016 *****************************************************************************/
1017 int DirectDrawUpdateOverlay( vout_thread_t *p_vout )
1022 RECT rect_src = p_vout->p_sys->rect_src_clipped;
1023 RECT rect_dest = p_vout->p_sys->rect_dest_clipped;
1025 if( !p_vout->p_sys->b_using_overlay ) return VLC_EGENERIC;
1027 if( p_vout->p_sys->b_wallpaper )
1029 unsigned i_x, i_y, i_width, i_height;
1031 rect_src.left = p_vout->fmt_out.i_x_offset;
1032 rect_src.top = p_vout->fmt_out.i_y_offset;
1033 rect_src.right = rect_src.left + p_vout->fmt_out.i_visible_width;
1034 rect_src.bottom = rect_src.top + p_vout->fmt_out.i_visible_height;
1036 rect_dest = p_vout->p_sys->rect_display;
1037 vout_PlacePicture( p_vout, rect_dest.right, rect_dest.bottom,
1038 &i_x, &i_y, &i_width, &i_height );
1040 rect_dest.left += i_x;
1041 rect_dest.right = rect_dest.left + i_width;
1042 rect_dest.top += i_y;
1043 rect_dest.bottom = rect_dest.top + i_height;
1046 vlc_mutex_lock( &p_vout->p_sys->lock );
1047 if( p_vout->p_sys->p_current_surface == NULL )
1049 vlc_mutex_unlock( &p_vout->p_sys->lock );
1050 return VLC_EGENERIC;
1053 /* The new window dimensions should already have been computed by the
1054 * caller of this function */
1056 /* Position and show the overlay */
1057 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
1058 ddofx.dwSize = sizeof(DDOVERLAYFX);
1059 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
1060 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
1062 dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1064 dxresult = IDirectDrawSurface2_UpdateOverlay(
1065 p_vout->p_sys->p_current_surface,
1066 &rect_src, p_vout->p_sys->p_display, &rect_dest,
1069 vlc_mutex_unlock( &p_vout->p_sys->lock );
1071 if(dxresult != DD_OK)
1073 msg_Warn( p_vout, "DirectDrawUpdateOverlay cannot move/resize overlay" );
1074 return VLC_EGENERIC;
1080 /*****************************************************************************
1081 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
1082 *****************************************************************************
1083 * This function returns all resources allocated by DirectXInitDDraw.
1084 *****************************************************************************/
1085 static void DirectXCloseDDraw( vout_thread_t *p_vout )
1087 msg_Dbg( p_vout, "DirectXCloseDDraw" );
1088 if( p_vout->p_sys->p_ddobject != NULL )
1090 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1091 p_vout->p_sys->p_ddobject = NULL;
1094 if( p_vout->p_sys->hddraw_dll != NULL )
1096 FreeLibrary( p_vout->p_sys->hddraw_dll );
1097 p_vout->p_sys->hddraw_dll = NULL;
1100 free( p_vout->p_sys->p_display_driver );
1101 p_vout->p_sys->p_display_driver = NULL;
1103 p_vout->p_sys->hmonitor = NULL;
1106 /*****************************************************************************
1107 * DirectXCloseDisplay: close and reset the DirectX display device
1108 *****************************************************************************
1109 * This function returns all resources allocated by DirectXCreateDisplay.
1110 *****************************************************************************/
1111 static void DirectXCloseDisplay( vout_thread_t *p_vout )
1113 msg_Dbg( p_vout, "DirectXCloseDisplay" );
1115 if( p_vout->p_sys->p_clipper != NULL )
1117 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1118 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1119 p_vout->p_sys->p_clipper = NULL;
1122 if( p_vout->p_sys->p_display != NULL )
1124 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1125 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1126 p_vout->p_sys->p_display = NULL;
1130 /*****************************************************************************
1131 * DirectXCloseSurface: close the YUV overlay or RGB surface.
1132 *****************************************************************************
1133 * This function returns all resources allocated for the surface.
1134 *****************************************************************************/
1135 static void DirectXCloseSurface( vout_thread_t *p_vout,
1136 LPDIRECTDRAWSURFACE2 p_surface )
1138 msg_Dbg( p_vout, "DirectXCloseSurface" );
1139 if( p_surface != NULL )
1141 IDirectDrawSurface2_Release( p_surface );
1145 /*****************************************************************************
1146 * NewPictureVec: allocate a picture
1147 * FIXME? make it work for i_num_pic pictures...
1148 *****************************************************************************
1149 * Returns 0 on success, -1 otherwise
1150 *****************************************************************************/
1151 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic )
1154 int i_ret = VLC_SUCCESS;
1155 LPDIRECTDRAWSURFACE2 p_surface;
1157 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1158 p_vout->p_sys->b_using_overlay ? "yes" : "no",
1159 (char *)&p_vout->output.i_chroma );
1161 I_OUTPUTPICTURES = 0;
1163 /* First we try to use an YUV overlay surface.
1164 * The overlay surface that we create won't be used to decode directly
1165 * into it because accessing video memory directly is way to slow (remember
1166 * that pictures are decoded macroblock per macroblock). Instead the video
1167 * will be decoded in picture buffers in system memory which will then be
1168 * memcpy() to the overlay surface. */
1169 if( p_vout->p_sys->b_using_overlay )
1171 /* Triple buffering rocks! it doesn't have any processing overhead
1172 * (you don't have to wait for the vsync) and provides for a very nice
1173 * video quality (no tearing). */
1174 if( p_vout->p_sys->b_3buf_overlay )
1175 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1176 p_vout->output.i_chroma,
1177 p_vout->p_sys->b_using_overlay,
1178 2 /* number of backbuffers */ );
1180 if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1182 /* Try to reduce the number of backbuffers */
1183 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1184 p_vout->output.i_chroma,
1185 p_vout->p_sys->b_using_overlay,
1186 0 /* number of backbuffers */ );
1189 if( i_ret == VLC_SUCCESS )
1192 picture_t front_pic;
1193 picture_sys_t front_pic_sys;
1194 front_pic.p_sys = &front_pic_sys;
1196 /* Allocate internal structure */
1197 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1198 if( p_pic[0].p_sys == NULL )
1200 DirectXCloseSurface( p_vout, p_surface );
1204 /* set front buffer */
1205 p_pic[0].p_sys->p_front_surface = p_surface;
1207 /* Get the back buffer */
1208 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1209 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1210 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1211 p_surface, &dds_caps,
1212 &p_pic[0].p_sys->p_surface ) )
1214 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1215 /* front buffer is the same as back buffer */
1216 p_pic[0].p_sys->p_surface = p_surface;
1220 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1221 p_pic[0].p_sys->p_front_surface;
1223 /* Reset the front buffer memory */
1224 if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1227 for( i = 0; i < front_pic.i_planes; i++ )
1228 for( j = 0; j < front_pic.p[i].i_visible_lines; j++)
1229 memset( front_pic.p[i].p_pixels + j *
1230 front_pic.p[i].i_pitch, 127,
1231 front_pic.p[i].i_visible_pitch );
1233 DirectXUnlockSurface( p_vout, &front_pic );
1236 DirectDrawUpdateOverlay( p_vout );
1237 I_OUTPUTPICTURES = 1;
1238 msg_Dbg( p_vout, "YUV overlay created successfully" );
1242 /* As we can't have an overlay, we'll try to create a plain offscreen
1243 * surface. This surface will reside in video memory because there's a
1244 * better chance then that we'll be able to use some kind of hardware
1245 * acceleration like rescaling, blitting or YUV->RGB conversions.
1246 * We then only need to blit this surface onto the main display when we
1247 * want to display it */
1248 if( !p_vout->p_sys->b_using_overlay )
1250 if( p_vout->p_sys->b_hw_yuv )
1254 bool b_result = false;
1256 /* Check if the chroma is supported first. This is required
1257 * because a few buggy drivers don't mind creating the surface
1258 * even if they don't know about the chroma. */
1259 if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1260 &i_codes, NULL ) == DD_OK )
1262 pi_codes = malloc( i_codes * sizeof(DWORD) );
1263 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1264 p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
1266 for( i = 0; i < (int)i_codes; i++ )
1268 if( p_vout->output.i_chroma == pi_codes[i] )
1279 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1280 p_vout->output.i_chroma,
1282 0 /* no back buffers */ );
1284 p_vout->p_sys->b_hw_yuv = false;
1287 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1289 /* Our last choice is to use a plain RGB surface */
1290 DDPIXELFORMAT ddpfPixelFormat;
1292 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1293 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1296 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1298 switch( ddpfPixelFormat.dwRGBBitCount )
1301 p_vout->output.i_chroma = VLC_CODEC_RGB8;
1302 p_vout->output.pf_setpalette = SetPalette;
1305 p_vout->output.i_chroma = VLC_CODEC_RGB15;
1308 p_vout->output.i_chroma = VLC_CODEC_RGB16;
1311 p_vout->output.i_chroma = VLC_CODEC_RGB24;
1314 p_vout->output.i_chroma = VLC_CODEC_RGB32;
1317 msg_Err( p_vout, "unknown screen depth" );
1318 return VLC_EGENERIC;
1320 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1321 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1322 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1325 p_vout->p_sys->b_hw_yuv = 0;
1327 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1328 p_vout->output.i_chroma,
1330 0 /* no back buffers */ );
1332 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1334 /* Give it a last try with b_use_sysmem enabled */
1335 p_vout->p_sys->b_use_sysmem = 1;
1337 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1338 p_vout->output.i_chroma,
1340 0 /* no back buffers */ );
1344 if( i_ret == VLC_SUCCESS )
1346 /* Allocate internal structure */
1347 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1348 if( p_pic[0].p_sys == NULL )
1350 DirectXCloseSurface( p_vout, p_surface );
1354 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1357 I_OUTPUTPICTURES = 1;
1359 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1360 (char *)&p_vout->output.i_chroma );
1365 /* Now that we've got all our direct-buffers, we can finish filling in the
1366 * picture_t structures */
1367 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1369 p_pic[i].i_status = DESTROYED_PICTURE;
1370 p_pic[i].i_type = DIRECT_PICTURE;
1371 p_pic[i].b_slow = true;
1372 p_pic[i].pf_lock = DirectXLockSurface;
1373 p_pic[i].pf_unlock = DirectXUnlockSurface;
1374 PP_OUTPUTPICTURE[i] = &p_pic[i];
1376 if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1379 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1380 I_OUTPUTPICTURES = 0;
1381 msg_Err( p_vout, "cannot lock surface" );
1382 return VLC_EGENERIC;
1384 DirectXUnlockSurface( p_vout, &p_pic[i] );
1387 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1388 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1393 /*****************************************************************************
1394 * FreePicture: destroy a picture vector allocated with NewPictureVec
1395 *****************************************************************************
1397 *****************************************************************************/
1398 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1403 vlc_mutex_lock( &p_vout->p_sys->lock );
1404 p_vout->p_sys->p_current_surface = 0;
1405 vlc_mutex_unlock( &p_vout->p_sys->lock );
1407 for( i = 0; i < i_num_pics; i++ )
1409 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1411 for( i = 0; i < i_num_pics; i++ )
1413 free( p_pic[i].p_sys );
1418 /*****************************************************************************
1419 * UpdatePictureStruct: updates the internal data in the picture_t structure
1420 *****************************************************************************
1421 * This will setup stuff for use by the video_output thread
1422 *****************************************************************************/
1423 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic )
1425 switch( p_vout->output.i_chroma )
1427 case VLC_CODEC_RGB8:
1428 case VLC_CODEC_RGB15:
1429 case VLC_CODEC_RGB16:
1430 case VLC_CODEC_RGB24:
1431 case VLC_CODEC_RGB32:
1432 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1433 p_pic->p->i_lines = p_vout->output.i_height;
1434 p_pic->p->i_visible_lines = p_vout->output.i_height;
1435 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1436 switch( p_vout->output.i_chroma )
1438 case VLC_CODEC_RGB8:
1439 p_pic->p->i_pixel_pitch = 1;
1441 case VLC_CODEC_RGB15:
1442 case VLC_CODEC_RGB16:
1443 p_pic->p->i_pixel_pitch = 2;
1445 case VLC_CODEC_RGB24:
1446 p_pic->p->i_pixel_pitch = 3;
1448 case VLC_CODEC_RGB32:
1449 p_pic->p->i_pixel_pitch = 4;
1452 return VLC_EGENERIC;
1454 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1455 p_pic->p->i_pixel_pitch;
1456 p_pic->i_planes = 1;
1459 case VLC_CODEC_YV12:
1461 /* U and V inverted compared to I420
1462 * Fixme: this should be handled by the vout core */
1463 /* could this be right? */
1464 p_vout->output.i_chroma = VLC_CODEC_I420;
1466 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1467 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1468 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1469 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1470 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1471 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1472 p_pic->p[Y_PLANE].i_pixel_pitch;
1474 p_pic->V_PIXELS = p_pic->Y_PIXELS
1475 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1476 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1477 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1478 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1479 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1480 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1481 p_pic->p[V_PLANE].i_pixel_pitch;
1483 p_pic->U_PIXELS = p_pic->V_PIXELS
1484 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1485 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1486 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1487 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1488 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1489 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1490 p_pic->p[U_PLANE].i_pixel_pitch;
1492 p_pic->i_planes = 3;
1495 case VLC_CODEC_I420:
1497 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1498 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1499 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1500 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1501 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1502 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1503 p_pic->p[Y_PLANE].i_pixel_pitch;
1505 p_pic->U_PIXELS = p_pic->Y_PIXELS
1506 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1507 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1508 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1509 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1510 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1511 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1512 p_pic->p[U_PLANE].i_pixel_pitch;
1514 p_pic->V_PIXELS = p_pic->U_PIXELS
1515 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1516 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1517 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1518 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1519 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1520 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1521 p_pic->p[V_PLANE].i_pixel_pitch;
1523 p_pic->i_planes = 3;
1526 case VLC_CODEC_UYVY:
1527 case VLC_CODEC_YUYV:
1529 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1530 p_pic->p->i_lines = p_vout->output.i_height;
1531 p_pic->p->i_visible_lines = p_vout->output.i_height;
1532 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1533 p_pic->p->i_pixel_pitch = 2;
1534 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1535 p_pic->p->i_pixel_pitch;
1537 p_pic->i_planes = 1;
1541 /* Unknown chroma, tell the guy to get lost */
1542 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1543 p_vout->output.i_chroma,
1544 (char*)&p_vout->output.i_chroma );
1545 return VLC_EGENERIC;
1551 /*****************************************************************************
1552 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1553 *****************************************************************************
1554 * It is nice to know which features are supported by the hardware so we can
1555 * find ways to optimize our rendering.
1556 *****************************************************************************/
1557 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1562 /* This is just an indication of whether or not we'll support overlay,
1563 * but with this test we don't know if we support YUV overlay */
1564 memset( &ddcaps, 0, sizeof( DDCAPS ));
1565 ddcaps.dwSize = sizeof(DDCAPS);
1566 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1568 if(dxresult != DD_OK )
1570 msg_Warn( p_vout, "cannot get caps" );
1574 bool bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1575 bHasColorKey, bCanStretch, bCanBltFourcc,
1576 bAlignBoundarySrc, bAlignBoundaryDest,
1577 bAlignSizeSrc, bAlignSizeDest;
1579 /* Determine if the hardware supports overlay surfaces */
1580 bHasOverlay = (ddcaps.dwCaps & DDCAPS_OVERLAY) ? 1 : 0;
1581 /* Determine if the hardware supports overlay surfaces */
1582 bHasOverlayFourCC = (ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ? 1 : 0;
1583 /* Determine if the hardware supports overlay deinterlacing */
1584 bCanDeinterlace = (ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ? 1 : 0;
1585 /* Determine if the hardware supports colorkeying */
1586 bHasColorKey = (ddcaps.dwCaps & DDCAPS_COLORKEY) ? 1 : 0;
1587 /* Determine if the hardware supports scaling of the overlay surface */
1588 bCanStretch = (ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ? 1 : 0;
1589 /* Determine if the hardware supports color conversion during a blit */
1590 bCanBltFourcc = (ddcaps.dwCaps & DDCAPS_BLTFOURCC) ? 1 : 0;
1591 /* Determine overlay source boundary alignment */
1592 bAlignBoundarySrc = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) ? 1 : 0;
1593 /* Determine overlay destination boundary alignment */
1594 bAlignBoundaryDest = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) ? 1:0;
1595 /* Determine overlay destination size alignment */
1596 bAlignSizeSrc = (ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC) ? 1 : 0;
1597 /* Determine overlay destination size alignment */
1598 bAlignSizeDest = (ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST) ? 1 : 0;
1600 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1601 "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1603 bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1604 bHasColorKey, bCanStretch, bCanBltFourcc );
1606 if( bAlignBoundarySrc || bAlignBoundaryDest ||
1607 bAlignSizeSrc || bAlignSizeDest )
1609 if( bAlignBoundarySrc ) p_vout->p_sys->i_align_src_boundary =
1610 ddcaps.dwAlignBoundarySrc;
1611 if( bAlignBoundaryDest ) p_vout->p_sys->i_align_dest_boundary =
1612 ddcaps.dwAlignBoundaryDest;
1613 if( bAlignSizeDest ) p_vout->p_sys->i_align_src_size =
1614 ddcaps.dwAlignSizeSrc;
1615 if( bAlignSizeDest ) p_vout->p_sys->i_align_dest_size =
1616 ddcaps.dwAlignSizeDest;
1618 msg_Dbg( p_vout, "align_boundary_src=%i,%i "
1619 "align_boundary_dest=%i,%i "
1620 "align_size_src=%i,%i align_size_dest=%i,%i",
1621 bAlignBoundarySrc, p_vout->p_sys->i_align_src_boundary,
1622 bAlignBoundaryDest, p_vout->p_sys->i_align_dest_boundary,
1623 bAlignSizeSrc, p_vout->p_sys->i_align_src_size,
1624 bAlignSizeDest, p_vout->p_sys->i_align_dest_size );
1627 /* Don't ask for troubles */
1628 if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1632 /*****************************************************************************
1633 * DirectXLockSurface: Lock surface and get picture data pointer
1634 *****************************************************************************
1635 * This function locks a surface and get the surface descriptor which amongst
1636 * other things has the pointer to the picture data.
1637 *****************************************************************************/
1638 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1642 /* Lock the surface to get a valid pointer to the picture buffer */
1643 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1644 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1645 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1646 NULL, &p_pic->p_sys->ddsd,
1647 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1649 if( dxresult != DD_OK )
1651 if( dxresult == DDERR_INVALIDPARAMS )
1653 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1654 * in an invalid params error */
1655 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1656 &p_pic->p_sys->ddsd,
1659 if( dxresult == DDERR_SURFACELOST )
1661 /* Your surface can be lost so be sure
1662 * to check this and restore it if needed */
1664 /* When using overlays with back-buffers, we need to restore
1665 * the front buffer so the back-buffers get restored as well. */
1666 if( p_vout->p_sys->b_using_overlay )
1667 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1669 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1671 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1672 &p_pic->p_sys->ddsd,
1675 if( dxresult == DDERR_SURFACELOST )
1676 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1679 if( dxresult != DD_OK )
1681 return VLC_EGENERIC;
1685 /* Now we have a pointer to the surface memory, we can update our picture
1687 if( UpdatePictureStruct( p_vout, p_pic )
1690 DirectXUnlockSurface( p_vout, p_pic );
1691 return VLC_EGENERIC;
1697 /*****************************************************************************
1698 * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1699 *****************************************************************************/
1700 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1702 VLC_UNUSED( p_vout );
1704 /* Unlock the Surface */
1705 if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1708 return VLC_EGENERIC;
1711 /*****************************************************************************
1712 * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
1713 *****************************************************************************/
1714 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *pi_color )
1719 uint32_t i_pixel_backup;
1722 ddsd.dwSize = sizeof(ddsd);
1723 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1724 &ddsd, DDLOCK_WAIT, NULL );
1725 if( dxresult != DD_OK ) return 0;
1727 i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1729 switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1732 *(uint8_t *)ddsd.lpSurface = *pi_color | (*pi_color << 4);
1735 *(uint8_t *)ddsd.lpSurface = *pi_color;
1739 *(uint16_t *)ddsd.lpSurface = *pi_color;
1742 /* Seems to be problematic so we'll just put black as the colorkey */
1745 *(uint32_t *)ddsd.lpSurface = *pi_color;
1749 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1751 if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1753 i_rgb = GetPixel( hdc, 0, 0 );
1754 IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1757 ddsd.dwSize = sizeof(ddsd);
1758 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1759 &ddsd, DDLOCK_WAIT, NULL );
1760 if( dxresult != DD_OK ) return i_rgb;
1762 *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1764 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1769 /*****************************************************************************
1770 * A few toolbox functions
1771 *****************************************************************************/
1772 void SwitchWallpaperMode( vout_thread_t *p_vout, bool b_on )
1776 if( p_vout->p_sys->b_wallpaper == b_on ) return; /* Nothing to do */
1778 hwnd = FindWindow( _T("Progman"), NULL );
1779 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
1780 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
1783 msg_Warn( p_vout, "couldn't find \"SysListView32\" window, "
1784 "wallpaper mode not supported" );
1788 p_vout->p_sys->b_wallpaper = b_on;
1790 msg_Dbg( p_vout, "wallpaper mode %s", b_on ? "enabled" : "disabled" );
1792 if( p_vout->p_sys->b_wallpaper )
1794 p_vout->p_sys->color_bkg = ListView_GetBkColor( hwnd );
1795 p_vout->p_sys->color_bkgtxt = ListView_GetTextBkColor( hwnd );
1797 ListView_SetBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1798 ListView_SetTextBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1802 ListView_SetBkColor( hwnd, p_vout->p_sys->color_bkg );
1803 ListView_SetTextBkColor( hwnd, p_vout->p_sys->color_bkgtxt );
1806 /* Update desktop */
1807 InvalidateRect( hwnd, NULL, TRUE );
1808 UpdateWindow( hwnd );
1811 /*****************************************************************************
1812 * config variable callback
1813 *****************************************************************************/
1814 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
1815 LPTSTR psz_drivername, VOID* p_context,
1818 VLC_UNUSED( p_guid ); VLC_UNUSED( psz_desc ); VLC_UNUSED( hmon );
1820 module_config_t *p_item = (module_config_t *)p_context;
1823 (char **)realloc( p_item->ppsz_list,
1824 (p_item->i_list+2) * sizeof(char *) );
1825 p_item->ppsz_list_text =
1826 (char **)realloc( p_item->ppsz_list_text,
1827 (p_item->i_list+2) * sizeof(char *) );
1829 p_item->ppsz_list[p_item->i_list] = strdup( psz_drivername );
1830 p_item->ppsz_list_text[p_item->i_list] = NULL;
1832 p_item->ppsz_list[p_item->i_list] = NULL;
1833 p_item->ppsz_list_text[p_item->i_list] = NULL;
1835 return TRUE; /* Keep enumerating */
1838 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1839 vlc_value_t newval, vlc_value_t oldval, void *d)
1841 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( d );
1843 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
1845 HINSTANCE hddraw_dll;
1847 module_config_t *p_item;
1850 p_item = config_FindConfig( p_this, psz_name );
1851 if( !p_item ) return VLC_SUCCESS;
1853 /* Clear-up the current list */
1854 if( p_item->i_list )
1856 /* Keep the first entry */
1857 for( i = 1; i < p_item->i_list; i++ )
1859 free( p_item->ppsz_list[i] );
1860 free( p_item->ppsz_list_text[i] );
1862 /* TODO: Remove when no more needed */
1863 p_item->ppsz_list[i] = NULL;
1864 p_item->ppsz_list_text[i] = NULL;
1868 /* Load direct draw DLL */
1869 hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
1870 if( hddraw_dll == NULL ) return VLC_SUCCESS;
1872 OurDirectDrawEnumerateEx =
1873 (void *)GetProcAddress( hddraw_dll, _T("DirectDrawEnumerateExW") );
1875 if( OurDirectDrawEnumerateEx )
1877 /* Enumerate displays */
1878 OurDirectDrawEnumerateEx( DirectXEnumCallback2, p_item,
1879 DDENUM_ATTACHEDSECONDARYDEVICES );
1882 FreeLibrary( hddraw_dll );
1884 /* Signal change to the interface */
1885 p_item->b_dirty = true;
1890 static int WallpaperCallback( vlc_object_t *p_this, char const *psz_cmd,
1891 vlc_value_t oldval, vlc_value_t newval,
1894 VLC_UNUSED( psz_cmd ); VLC_UNUSED( oldval ); VLC_UNUSED( p_data );
1895 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1897 if( (newval.b_bool && !p_vout->p_sys->b_wallpaper) ||
1898 (!newval.b_bool && p_vout->p_sys->b_wallpaper) )
1900 playlist_t *p_playlist = pl_Hold( p_vout );
1904 /* Modify playlist as well because the vout might have to be
1906 var_Create( p_playlist, "directx-wallpaper", VLC_VAR_BOOL );
1907 var_Set( p_playlist, "directx-wallpaper", newval );
1908 pl_Release( p_vout );
1911 p_vout->p_sys->i_changes |= DX_WALLPAPER_CHANGE;
1917 /*****************************************************************************
1918 * SetPalette: sets an 8 bpp palette
1919 *****************************************************************************/
1920 static void SetPalette( vout_thread_t *p_vout,
1921 uint16_t *red, uint16_t *green, uint16_t *blue )
1923 VLC_UNUSED( red ); VLC_UNUSED( green );VLC_UNUSED( blue );
1924 msg_Err( p_vout, "FIXME: SetPalette unimplemented" );